blob: 9ea24a38f739b680a9dd09143f53f4ab887d4f36 [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 Anvind7ed89e2002-04-30 20:52:08 +000099 * Store the definition of a single-line macro.
100 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000101struct SMacro {
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800102 SMacro *next;
103 char *name;
104 bool casesense;
105 bool in_progress;
106 unsigned int nparam;
107 Token *expansion;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000108};
109
110/*
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800111 * Store the definition of a multi-line macro. This is also used to
112 * store the interiors of `%rep...%endrep' blocks, which are
113 * effectively self-re-invoking multi-line macros which simply
114 * don't have a name or bother to appear in the hash tables. %rep
115 * blocks are signified by having a NULL `name' field.
116 *
117 * In a MMacro describing a `%rep' block, the `in_progress' field
118 * isn't merely boolean, but gives the number of repeats left to
119 * run.
120 *
121 * The `next' field is used for storing MMacros in hash tables; the
122 * `next_active' field is for stacking them on istk entries.
123 *
124 * When a MMacro is being expanded, `params', `iline', `nparam',
125 * `paramlen', `rotate' and `unique' are local to the invocation.
126 */
127struct MMacro {
128 MMacro *next;
129 MMacroInvocation *prev; /* previous invocation */
130 char *name;
131 int nparam_min, nparam_max;
132 bool casesense;
133 bool plus; /* is the last parameter greedy? */
134 bool nolist; /* is this macro listing-inhibited? */
135 int64_t in_progress; /* is this macro currently being expanded? */
136 int32_t max_depth; /* maximum number of recursive expansions allowed */
137 Token *dlist; /* All defaults as one list */
138 Token **defaults; /* Parameter default pointers */
139 int ndefs; /* number of default parameters */
140 Line *expansion;
141
142 MMacro *next_active;
143 MMacro *rep_nest; /* used for nesting %rep */
144 Token **params; /* actual parameters */
145 Token *iline; /* invocation line */
146 unsigned int nparam, rotate;
147 int *paramlen;
148 uint64_t unique;
149 int lineno; /* Current line number on expansion */
150 uint64_t condcnt; /* number of if blocks... */
H. Peter Anvin4def1a82016-05-09 13:59:44 -0700151
H. Peter Anvin274cda82016-05-10 02:56:29 -0700152 const char *fname; /* File where defined */
H. Peter Anvin4def1a82016-05-09 13:59:44 -0700153 int32_t xline; /* First line in macro */
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800154};
155
156
157/* Store the definition of a multi-line macro, as defined in a
158 * previous recursive macro expansion.
159 */
160struct MMacroInvocation {
161 MMacroInvocation *prev; /* previous invocation */
162 Token **params; /* actual parameters */
163 Token *iline; /* invocation line */
164 unsigned int nparam, rotate;
165 int *paramlen;
166 uint64_t unique;
167 uint64_t condcnt;
168};
169
170
171/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000172 * The context stack is composed of a linked list of these.
173 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000174struct Context {
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800175 Context *next;
176 char *name;
177 struct hash_table localmac;
178 uint32_t number;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000179};
180
181/*
182 * This is the internal form which we break input lines up into.
183 * Typically stored in linked lists.
184 *
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000185 * Note that `type' serves a double meaning: TOK_SMAC_PARAM is not
186 * necessarily used as-is, but is intended to denote the number of
187 * the substituted parameter. So in the definition
188 *
189 * %define a(x,y) ( (x) & ~(y) )
H. Peter Anvin70653092007-10-19 14:42:29 -0700190 *
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000191 * the token representing `x' will have its type changed to
192 * TOK_SMAC_PARAM, but the one representing `y' will be
193 * TOK_SMAC_PARAM+1.
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000194 *
195 * TOK_INTERNAL_STRING is a dirty hack: it's a single string token
196 * which doesn't need quotes around it. Used in the pre-include
197 * mechanism as an alternative to trying to find a sensible type of
198 * quote to use on the filename we were passed.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000199 */
H. Peter Anvinda10e7b2007-09-12 04:18:37 +0000200enum pp_token_type {
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800201 TOK_NONE = 0, TOK_WHITESPACE, TOK_COMMENT, TOK_ID,
202 TOK_PREPROC_ID, TOK_STRING,
203 TOK_NUMBER, TOK_FLOAT, TOK_SMAC_END, TOK_OTHER,
H. Peter Anvin6c81f0a2008-05-25 21:46:17 -0700204 TOK_INTERNAL_STRING,
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800205 TOK_PREPROC_Q, TOK_PREPROC_QQ,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300206 TOK_PASTE, /* %+ */
207 TOK_INDIRECT, /* %[...] */
208 TOK_SMAC_PARAM, /* MUST BE LAST IN THE LIST!!! */
209 TOK_MAX = INT_MAX /* Keep compiler from reducing the range */
H. Peter Anvinda10e7b2007-09-12 04:18:37 +0000210};
211
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +0400212#define PP_CONCAT_MASK(x) (1 << (x))
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +0400213#define PP_CONCAT_MATCH(t, mask) (PP_CONCAT_MASK((t)->type) & mask)
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +0400214
Cyrill Gorcunov575d4282010-10-06 00:25:55 +0400215struct tokseq_match {
216 int mask_head;
217 int mask_tail;
218};
219
H. Peter Anvine2c80182005-01-15 22:15:51 +0000220struct Token {
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800221 Token *next;
222 char *text;
H. Peter Anvinf26e0972008-07-01 21:26:27 -0700223 union {
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800224 SMacro *mac; /* associated macro for TOK_SMAC_END */
225 size_t len; /* scratch length field */
226 } a; /* Auxiliary data */
227 enum pp_token_type type;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000228};
229
230/*
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800231 * Multi-line macro definitions are stored as a linked list of
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000232 * these, which is essentially a container to allow several linked
233 * lists of Tokens.
H. Peter Anvin70653092007-10-19 14:42:29 -0700234 *
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000235 * Note that in this module, linked lists are treated as stacks
236 * wherever possible. For this reason, Lines are _pushed_ on to the
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800237 * `expansion' field in MMacro structures, so that the linked list,
238 * if walked, would give the macro lines in reverse order; this
239 * means that we can walk the list when expanding a macro, and thus
240 * push the lines on to the `expansion' field in _istk_ in reverse
241 * order (so that when popped back off they are in the right
242 * order). It may seem cockeyed, and it relies on my design having
243 * an even number of steps in, but it works...
244 *
245 * Some of these structures, rather than being actual lines, are
246 * markers delimiting the end of the expansion of a given macro.
247 * This is for use in the cycle-tracking and %rep-handling code.
248 * Such structures have `finishes' non-NULL, and `first' NULL. All
249 * others have `finishes' NULL, but `first' may still be NULL if
250 * the line is blank.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000251 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000252struct Line {
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800253 Line *next;
254 MMacro *finishes;
255 Token *first;
Keith Kaniosb307a4f2010-11-06 17:41:51 -0500256};
257
258/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000259 * To handle an arbitrary level of file inclusion, we maintain a
260 * stack (ie linked list) of these things.
261 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000262struct Include {
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800263 Include *next;
264 FILE *fp;
265 Cond *conds;
266 Line *expansion;
H. Peter Anvin274cda82016-05-10 02:56:29 -0700267 const char *fname;
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800268 int lineno, lineinc;
269 MMacro *mstk; /* stack of active macros/reps */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000270};
271
272/*
H. Peter Anvin169ac7c2016-09-25 17:08:05 -0700273 * File real name hash, so we don't have to re-search the include
274 * path for every pass (and potentially more than that if a file
275 * is used more than once.)
276 */
277struct hash_table FileHash;
278
279/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000280 * Conditional assembly: we maintain a separate stack of these for
281 * each level of file inclusion. (The only reason we keep the
282 * stacks separate is to ensure that a stray `%endif' in a file
283 * included from within the true branch of a `%if' won't terminate
284 * it and cause confusion: instead, rightly, it'll cause an error.)
285 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800286struct Cond {
287 Cond *next;
288 int state;
289};
H. Peter Anvine2c80182005-01-15 22:15:51 +0000290enum {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000291 /*
292 * These states are for use just after %if or %elif: IF_TRUE
293 * means the condition has evaluated to truth so we are
294 * currently emitting, whereas IF_FALSE means we are not
295 * currently emitting but will start doing so if a %else comes
296 * up. In these states, all directives are admissible: %elif,
297 * %else and %endif. (And of course %if.)
298 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800299 COND_IF_TRUE, COND_IF_FALSE,
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000300 /*
301 * These states come up after a %else: ELSE_TRUE means we're
302 * emitting, and ELSE_FALSE means we're not. In ELSE_* states,
303 * any %elif or %else will cause an error.
304 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800305 COND_ELSE_TRUE, COND_ELSE_FALSE,
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000306 /*
Victor van den Elzen3b404c02008-09-18 13:51:36 +0200307 * These states mean that we're not emitting now, and also that
308 * nothing until %endif will be emitted at all. COND_DONE is
309 * used when we've had our moment of emission
310 * and have now started seeing %elifs. COND_NEVER is used when
311 * the condition construct in question is contained within a
312 * non-emitting branch of a larger condition construct,
313 * or if there is an error.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000314 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800315 COND_DONE, COND_NEVER
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000316};
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800317#define emitting(x) ( (x) == COND_IF_TRUE || (x) == COND_ELSE_TRUE )
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000318
H. Peter Anvin70653092007-10-19 14:42:29 -0700319/*
Ed Beroset3ab3f412002-06-11 03:31:49 +0000320 * These defines are used as the possible return values for do_directive
321 */
322#define NO_DIRECTIVE_FOUND 0
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300323#define DIRECTIVE_FOUND 1
Ed Beroset3ab3f412002-06-11 03:31:49 +0000324
Cyrill Gorcunove091d6e2010-08-09 13:58:22 +0400325/* max reps */
326#define REP_LIMIT ((INT64_C(1) << 62))
327
Keith Kanios852f1ee2009-07-12 00:19:55 -0500328/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000329 * Condition codes. Note that we use c_ prefix not C_ because C_ is
330 * used in nasm.h for the "real" condition codes. At _this_ level,
331 * we treat CXZ and ECXZ as condition codes, albeit non-invertible
332 * ones, so we need a different enum...
333 */
H. Peter Anvin476d2862007-10-02 22:04:15 -0700334static const char * const conditions[] = {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000335 "a", "ae", "b", "be", "c", "cxz", "e", "ecxz", "g", "ge", "l", "le",
336 "na", "nae", "nb", "nbe", "nc", "ne", "ng", "nge", "nl", "nle", "no",
H. Peter Anvince9be342007-09-12 00:22:29 +0000337 "np", "ns", "nz", "o", "p", "pe", "po", "rcxz", "s", "z"
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000338};
H. Peter Anvin476d2862007-10-02 22:04:15 -0700339enum pp_conds {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000340 c_A, c_AE, c_B, c_BE, c_C, c_CXZ, c_E, c_ECXZ, c_G, c_GE, c_L, c_LE,
341 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 -0700342 c_NP, c_NS, c_NZ, c_O, c_P, c_PE, c_PO, c_RCXZ, c_S, c_Z,
343 c_none = -1
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000344};
H. Peter Anvin476d2862007-10-02 22:04:15 -0700345static const enum pp_conds inverse_ccs[] = {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000346 c_NA, c_NAE, c_NB, c_NBE, c_NC, -1, c_NE, -1, c_NG, c_NGE, c_NL, c_NLE,
347 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 +0000348 c_Z, c_NO, c_NP, c_PO, c_PE, -1, c_NS, c_NZ
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000349};
350
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800351/*
352 * Directive names.
353 */
354/* If this is a an IF, ELIF, ELSE or ENDIF keyword */
355static int is_condition(enum preproc_token arg)
356{
357 return PP_IS_COND(arg) || (arg == PP_ELSE) || (arg == PP_ENDIF);
358}
359
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000360/* For TASM compatibility we need to be able to recognise TASM compatible
361 * conditional compilation directives. Using the NASM pre-processor does
362 * not work, so we look for them specifically from the following list and
363 * then jam in the equivalent NASM directive into the input stream.
364 */
365
H. Peter Anvine2c80182005-01-15 22:15:51 +0000366enum {
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000367 TM_ARG, TM_ELIF, TM_ELSE, TM_ENDIF, TM_IF, TM_IFDEF, TM_IFDIFI,
368 TM_IFNDEF, TM_INCLUDE, TM_LOCAL
369};
370
H. Peter Anvin476d2862007-10-02 22:04:15 -0700371static const char * const tasm_directives[] = {
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000372 "arg", "elif", "else", "endif", "if", "ifdef", "ifdifi",
373 "ifndef", "include", "local"
374};
375
376static int StackSize = 4;
H. Peter Anvin6c8b2be2016-05-24 23:46:50 -0700377static const char *StackPointer = "ebp";
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000378static int ArgOffset = 8;
H. Peter Anvin8781cb02007-11-08 20:01:11 -0800379static int LocalOffset = 0;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000380
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000381static Context *cstk;
382static Include *istk;
Cyrill Gorcunov8c0666b2018-11-24 14:33:48 +0300383static const struct strlist *ipath_list;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000384
Cyrill Gorcunovb7bb5ac2018-11-11 21:33:52 +0300385static struct strlist *deplist;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000386
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300387static uint64_t unique; /* unique identifier numbers */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000388
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800389static Line *predef = NULL;
H. Peter Anvind2456592008-06-19 15:04:18 -0700390static bool do_predef;
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -0800391static enum preproc_mode pp_mode;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000392
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000393/*
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800394 * The current set of multi-line macros we have defined.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000395 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800396static struct hash_table mmacros;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000397
398/*
399 * The current set of single-line macros we have defined.
400 */
H. Peter Anvin166c2472008-05-28 12:28:58 -0700401static struct hash_table smacros;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000402
403/*
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800404 * The multi-line macro we are currently defining, or the %rep
405 * block we are currently reading, if any.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000406 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800407static MMacro *defining;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000408
Charles Crayned4200be2008-07-12 16:42:33 -0700409static uint64_t nested_mac_count;
410static uint64_t nested_rep_count;
411
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000412/*
413 * The number of macro parameters to allocate space for at a time.
414 */
415#define PARAM_DELTA 16
416
417/*
H. Peter Anvinf7606612016-07-13 14:23:48 -0700418 * The standard macro set: defined in macros.c in a set of arrays.
419 * This gives our position in any macro set, while we are processing it.
420 * The stdmacset is an array of such macro sets.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000421 */
H. Peter Anvina70547f2008-07-19 21:44:26 -0700422static macros_t *stdmacpos;
H. Peter Anvinf7606612016-07-13 14:23:48 -0700423static macros_t **stdmacnext;
424static macros_t *stdmacros[8];
Cyrill Gorcunov15ce78f2017-01-06 20:21:28 +0300425static macros_t *extrastdmac;
H. Peter Anvin76690a12002-04-30 20:52:49 +0000426
427/*
H. Peter Anvin734b1882002-04-30 21:01:08 +0000428 * Tokens are allocated in blocks to improve speed
429 */
430#define TOKEN_BLOCKSIZE 4096
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800431static Token *freeTokens = NULL;
H. Peter Anvince616072002-04-30 21:02:23 +0000432struct Blocks {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000433 Blocks *next;
434 void *chunk;
H. Peter Anvince616072002-04-30 21:02:23 +0000435};
436
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800437static Blocks blocks = { NULL, NULL };
H. Peter Anvin734b1882002-04-30 21:01:08 +0000438
439/*
H. Peter Anvin76690a12002-04-30 20:52:49 +0000440 * Forward declarations.
441 */
H. Peter Anvinf7606612016-07-13 14:23:48 -0700442static void pp_add_stdmac(macros_t *macros);
H. Peter Anvin734b1882002-04-30 21:01:08 +0000443static Token *expand_mmac_params(Token * tline);
444static Token *expand_smacro(Token * tline);
445static Token *expand_id(Token * tline);
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +0400446static Context *get_ctx(const char *name, const char **namep);
Keith Kaniosa5fc6462007-10-13 07:09:22 -0700447static void make_tok_num(Token * tok, int64_t val);
H. Peter Anvin (Intel)6bde2ed2018-12-13 19:39:41 -0800448static void pp_verror(errflags severity, const char *fmt, va_list ap);
H. Peter Anvin130736c2016-02-17 20:27:41 -0800449static vefunc real_verror;
H. Peter Anvince616072002-04-30 21:02:23 +0000450static void *new_Block(size_t size);
451static void delete_Blocks(void);
H. Peter Anvinc751e862008-06-09 10:18:45 -0700452static Token *new_Token(Token * next, enum pp_token_type type,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300453 const char *text, int txtlen);
H. Peter Anvin734b1882002-04-30 21:01:08 +0000454static Token *delete_Token(Token * t);
H. Peter Anvineba20a72002-04-30 20:53:55 +0000455
456/*
457 * Macros for safe checking of token pointers, avoid *(NULL)
458 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300459#define tok_type_(x,t) ((x) && (x)->type == (t))
460#define skip_white_(x) if (tok_type_((x), TOK_WHITESPACE)) (x)=(x)->next
461#define tok_is_(x,v) (tok_type_((x), TOK_OTHER) && !strcmp((x)->text,(v)))
462#define tok_isnt_(x,v) ((x) && ((x)->type!=TOK_OTHER || strcmp((x)->text,(v))))
H. Peter Anvin76690a12002-04-30 20:52:49 +0000463
Cyrill Gorcunov194ba892011-06-30 01:16:35 +0400464/*
H. Peter Anvinb40992c2010-09-15 08:57:21 -0700465 * In-place reverse a list of tokens.
466 */
467static Token *reverse_tokens(Token *t)
468{
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800469 Token *prev = NULL;
470 Token *next;
H. Peter Anvinb40992c2010-09-15 08:57:21 -0700471
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800472 while (t) {
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +0400473 next = t->next;
474 t->next = prev;
475 prev = t;
476 t = next;
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800477 }
H. Peter Anvinb40992c2010-09-15 08:57:21 -0700478
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800479 return prev;
H. Peter Anvinb40992c2010-09-15 08:57:21 -0700480}
481
482/*
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300483 * Handle TASM specific directives, which do not contain a % in
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000484 * front of them. We do it here because I could not find any other
485 * place to do it for the moment, and it is a hack (ideally it would
486 * be nice to be able to use the NASM pre-processor to do it).
487 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000488static char *check_tasm_directive(char *line)
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000489{
Keith Kaniosb7a89542007-04-12 02:40:54 +0000490 int32_t i, j, k, m, len;
Cyrill Gorcunovf66ac7d2009-10-12 20:41:13 +0400491 char *p, *q, *oldline, oldchar;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000492
Cyrill Gorcunovf66ac7d2009-10-12 20:41:13 +0400493 p = nasm_skip_spaces(line);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000494
495 /* Binary search for the directive name */
496 i = -1;
Cyrill Gorcunova7319242010-06-03 22:04:36 +0400497 j = ARRAY_SIZE(tasm_directives);
Cyrill Gorcunovf66ac7d2009-10-12 20:41:13 +0400498 q = nasm_skip_word(p);
499 len = q - p;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000500 if (len) {
501 oldchar = p[len];
502 p[len] = 0;
503 while (j - i > 1) {
504 k = (j + i) / 2;
505 m = nasm_stricmp(p, tasm_directives[k]);
506 if (m == 0) {
507 /* We have found a directive, so jam a % in front of it
508 * so that NASM will then recognise it as one if it's own.
509 */
510 p[len] = oldchar;
511 len = strlen(p);
512 oldline = line;
513 line = nasm_malloc(len + 2);
514 line[0] = '%';
515 if (k == TM_IFDIFI) {
H. Peter Anvin18f48792009-06-27 15:56:27 -0700516 /*
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300517 * NASM does not recognise IFDIFI, so we convert
518 * it to %if 0. This is not used in NASM
519 * compatible code, but does need to parse for the
520 * TASM macro package.
H. Peter Anvine2c80182005-01-15 22:15:51 +0000521 */
H. Peter Anvin18f48792009-06-27 15:56:27 -0700522 strcpy(line + 1, "if 0");
H. Peter Anvine2c80182005-01-15 22:15:51 +0000523 } else {
524 memcpy(line + 1, p, len + 1);
525 }
526 nasm_free(oldline);
527 return line;
528 } else if (m < 0) {
529 j = k;
530 } else
531 i = k;
532 }
533 p[len] = oldchar;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000534 }
535 return line;
536}
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000537
H. Peter Anvin76690a12002-04-30 20:52:49 +0000538/*
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000539 * The pre-preprocessing stage... This function translates line
540 * number indications as they emerge from GNU cpp (`# lineno "file"
541 * flags') into NASM preprocessor line number indications (`%line
542 * lineno file').
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000543 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000544static char *prepreproc(char *line)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000545{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000546 int lineno, fnlen;
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000547 char *fname, *oldline;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000548
H. Peter Anvine2c80182005-01-15 22:15:51 +0000549 if (line[0] == '#' && line[1] == ' ') {
550 oldline = line;
551 fname = oldline + 2;
552 lineno = atoi(fname);
553 fname += strspn(fname, "0123456789 ");
554 if (*fname == '"')
555 fname++;
556 fnlen = strcspn(fname, "\"");
557 line = nasm_malloc(20 + fnlen);
558 snprintf(line, 20 + fnlen, "%%line %d %.*s", lineno, fnlen, fname);
559 nasm_free(oldline);
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000560 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000561 if (tasm_compatible_mode)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000562 return check_tasm_directive(line);
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000563 return line;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000564}
565
566/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000567 * Free a linked list of tokens.
568 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000569static void free_tlist(Token * list)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000570{
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +0400571 while (list)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000572 list = delete_Token(list);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000573}
574
575/*
576 * Free a linked list of lines.
577 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000578static void free_llist(Line * list)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000579{
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +0400580 Line *l, *tmp;
581 list_for_each_safe(l, tmp, list) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000582 free_tlist(l->first);
583 nasm_free(l);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000584 }
585}
586
587/*
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800588 * Free an MMacro
H. Peter Anvineba20a72002-04-30 20:53:55 +0000589 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800590static void free_mmacro(MMacro * m)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000591{
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800592 nasm_free(m->name);
593 free_tlist(m->dlist);
594 nasm_free(m->defaults);
595 free_llist(m->expansion);
596 nasm_free(m);
H. Peter Anvineba20a72002-04-30 20:53:55 +0000597}
598
599/*
H. Peter Anvin97a23472007-09-16 17:57:25 -0700600 * Free all currently defined macros, and free the hash tables
601 */
H. Peter Anvin072771e2008-05-22 13:17:51 -0700602static void free_smacro_table(struct hash_table *smt)
H. Peter Anvin97a23472007-09-16 17:57:25 -0700603{
H. Peter Anvin (Intel)ebb05a02018-12-11 12:30:25 -0800604 struct hash_iterator it;
605 const struct hash_node *np;
H. Peter Anvin97a23472007-09-16 17:57:25 -0700606
H. Peter Anvin (Intel)ebb05a02018-12-11 12:30:25 -0800607 hash_for_each(smt, it, np) {
608 SMacro *tmp;
609 SMacro *s = np->data;
610 nasm_free((void *)np->key);
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +0400611 list_for_each_safe(s, tmp, s) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300612 nasm_free(s->name);
613 free_tlist(s->expansion);
614 nasm_free(s);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300615 }
H. Peter Anvin97a23472007-09-16 17:57:25 -0700616 }
H. Peter Anvin072771e2008-05-22 13:17:51 -0700617 hash_free(smt);
618}
619
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800620static void free_mmacro_table(struct hash_table *mmt)
H. Peter Anvin072771e2008-05-22 13:17:51 -0700621{
H. Peter Anvin (Intel)ebb05a02018-12-11 12:30:25 -0800622 struct hash_iterator it;
623 const struct hash_node *np;
H. Peter Anvin97a23472007-09-16 17:57:25 -0700624
H. Peter Anvin (Intel)ebb05a02018-12-11 12:30:25 -0800625 hash_for_each(mmt, it, np) {
626 MMacro *tmp;
627 MMacro *m = np->data;
628 nasm_free((void *)np->key);
629 list_for_each_safe(m, tmp, m)
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800630 free_mmacro(m);
H. Peter Anvin97a23472007-09-16 17:57:25 -0700631 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800632 hash_free(mmt);
H. Peter Anvin072771e2008-05-22 13:17:51 -0700633}
634
635static void free_macros(void)
636{
H. Peter Anvin166c2472008-05-28 12:28:58 -0700637 free_smacro_table(&smacros);
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800638 free_mmacro_table(&mmacros);
H. Peter Anvin97a23472007-09-16 17:57:25 -0700639}
640
641/*
642 * Initialize the hash tables
643 */
644static void init_macros(void)
645{
H. Peter Anvin97a23472007-09-16 17:57:25 -0700646}
647
648/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000649 * Pop the context stack.
650 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000651static void ctx_pop(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000652{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000653 Context *c = cstk;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000654
655 cstk = cstk->next;
H. Peter Anvin166c2472008-05-28 12:28:58 -0700656 free_smacro_table(&c->localmac);
H. Peter Anvin734b1882002-04-30 21:01:08 +0000657 nasm_free(c->name);
658 nasm_free(c);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000659}
660
H. Peter Anvin072771e2008-05-22 13:17:51 -0700661/*
662 * Search for a key in the hash index; adding it if necessary
663 * (in which case we initialize the data pointer to NULL.)
664 */
665static void **
666hash_findi_add(struct hash_table *hash, const char *str)
667{
668 struct hash_insert hi;
669 void **r;
670 char *strx;
H. Peter Anvin (Intel)ebb05a02018-12-11 12:30:25 -0800671 size_t l = strlen(str) + 1;
H. Peter Anvin072771e2008-05-22 13:17:51 -0700672
H. Peter Anvin (Intel)ebb05a02018-12-11 12:30:25 -0800673 r = hash_findib(hash, str, l, &hi);
H. Peter Anvin072771e2008-05-22 13:17:51 -0700674 if (r)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300675 return r;
H. Peter Anvin072771e2008-05-22 13:17:51 -0700676
H. Peter Anvin (Intel)ebb05a02018-12-11 12:30:25 -0800677 strx = nasm_malloc(l); /* Use a more efficient allocator here? */
678 memcpy(strx, str, l);
H. Peter Anvin072771e2008-05-22 13:17:51 -0700679 return hash_add(&hi, strx, NULL);
680}
681
682/*
683 * Like hash_findi, but returns the data element rather than a pointer
684 * to it. Used only when not adding a new element, hence no third
685 * argument.
686 */
687static void *
688hash_findix(struct hash_table *hash, const char *str)
689{
690 void **p;
691
692 p = hash_findi(hash, str, NULL);
693 return p ? *p : NULL;
694}
695
Cyrill Gorcunov15bdc512010-07-13 11:27:41 +0400696/*
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800697 * read line from standart macros set,
Cyrill Gorcunov15bdc512010-07-13 11:27:41 +0400698 * if there no more left -- return NULL
699 */
700static char *line_from_stdmac(void)
701{
702 unsigned char c;
703 const unsigned char *p = stdmacpos;
704 char *line, *q;
705 size_t len = 0;
706
707 if (!stdmacpos)
708 return NULL;
709
710 while ((c = *p++)) {
711 if (c >= 0x80)
712 len += pp_directives_len[c - 0x80] + 1;
713 else
714 len++;
715 }
716
717 line = nasm_malloc(len + 1);
718 q = line;
719 while ((c = *stdmacpos++)) {
720 if (c >= 0x80) {
721 memcpy(q, pp_directives[c - 0x80], pp_directives_len[c - 0x80]);
722 q += pp_directives_len[c - 0x80];
723 *q++ = ' ';
724 } else {
725 *q++ = c;
726 }
727 }
728 stdmacpos = p;
729 *q = '\0';
730
731 if (!*stdmacpos) {
H. Peter Anvinf7606612016-07-13 14:23:48 -0700732 /* This was the last of this particular macro set */
Cyrill Gorcunov15bdc512010-07-13 11:27:41 +0400733 stdmacpos = NULL;
H. Peter Anvinf7606612016-07-13 14:23:48 -0700734 if (*stdmacnext) {
735 stdmacpos = *stdmacnext++;
Cyrill Gorcunov15bdc512010-07-13 11:27:41 +0400736 } else if (do_predef) {
737 Line *pd, *l;
738 Token *head, **tail, *t;
739
740 /*
741 * Nasty hack: here we push the contents of
742 * `predef' on to the top-level expansion stack,
743 * since this is the most convenient way to
744 * implement the pre-include and pre-define
745 * features.
746 */
747 list_for_each(pd, predef) {
748 head = NULL;
749 tail = &head;
750 list_for_each(t, pd->first) {
751 *tail = new_Token(NULL, t->type, t->text, 0);
752 tail = &(*tail)->next;
753 }
754
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800755 l = nasm_malloc(sizeof(Line));
756 l->next = istk->expansion;
757 l->first = head;
758 l->finishes = NULL;
759
760 istk->expansion = l;
Cyrill Gorcunov15bdc512010-07-13 11:27:41 +0400761 }
762 do_predef = false;
763 }
764 }
765
766 return line;
767}
768
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000769static char *read_line(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000770{
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +0400771 unsigned int size, c, next;
772 const unsigned int delta = 512;
773 const unsigned int pad = 8;
774 unsigned int nr_cont = 0;
775 bool cont = false;
776 char *buffer, *p;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000777
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +0400778 /* Standart macros set (predefined) goes first */
Cyrill Gorcunov15bdc512010-07-13 11:27:41 +0400779 p = line_from_stdmac();
780 if (p)
781 return p;
H. Peter Anvin72edbb82008-06-19 16:00:04 -0700782
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +0400783 size = delta;
784 p = buffer = nasm_malloc(size);
785
786 for (;;) {
787 c = fgetc(istk->fp);
788 if ((int)(c) == EOF) {
789 p[0] = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000790 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000791 }
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +0400792
793 switch (c) {
794 case '\r':
795 next = fgetc(istk->fp);
796 if (next != '\n')
797 ungetc(next, istk->fp);
798 if (cont) {
799 cont = false;
800 continue;
801 }
802 break;
803
804 case '\n':
805 if (cont) {
806 cont = false;
807 continue;
808 }
809 break;
810
811 case '\\':
812 next = fgetc(istk->fp);
813 ungetc(next, istk->fp);
Cyrill Gorcunov490f85e2012-12-27 20:02:17 +0400814 if (next == '\r' || next == '\n') {
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +0400815 cont = true;
816 nr_cont++;
817 continue;
818 }
819 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000820 }
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +0400821
822 if (c == '\r' || c == '\n') {
823 *p++ = 0;
824 break;
825 }
826
827 if (p >= (buffer + size - pad)) {
828 buffer = nasm_realloc(buffer, size + delta);
829 p = buffer + size - pad;
830 size += delta;
831 }
832
833 *p++ = (unsigned char)c;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000834 }
835
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +0400836 if (p == buffer) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000837 nasm_free(buffer);
838 return NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000839 }
840
Cyrill Gorcunova5aea572010-11-11 10:14:45 +0300841 src_set_linnum(src_get_linnum() + istk->lineinc +
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +0400842 (nr_cont * istk->lineinc));
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000843
844 /*
845 * Handle spurious ^Z, which may be inserted into source files
846 * by some file transfer utilities.
847 */
848 buffer[strcspn(buffer, "\032")] = '\0';
849
H. Peter Anvin8ac25aa2016-02-18 01:16:18 -0800850 lfmt->line(LIST_READ, buffer);
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000851
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000852 return buffer;
853}
854
855/*
Keith Kaniosb7a89542007-04-12 02:40:54 +0000856 * Tokenize a line of text. This is a very simple process since we
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000857 * don't need to parse the value out of e.g. numeric tokens: we
858 * simply split one string into many.
859 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000860static Token *tokenize(char *line)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000861{
H. Peter Anvinca544db2008-10-19 19:30:11 -0700862 char c, *p = line;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +0000863 enum pp_token_type type;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000864 Token *list = NULL;
865 Token *t, **tail = &list;
866
H. Peter Anvine2c80182005-01-15 22:15:51 +0000867 while (*line) {
868 p = line;
869 if (*p == '%') {
870 p++;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300871 if (*p == '+' && !nasm_isdigit(p[1])) {
872 p++;
873 type = TOK_PASTE;
874 } else if (nasm_isdigit(*p) ||
875 ((*p == '-' || *p == '+') && nasm_isdigit(p[1]))) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000876 do {
877 p++;
878 }
H. Peter Anvinbda7a6e2008-06-21 10:23:17 -0700879 while (nasm_isdigit(*p));
H. Peter Anvine2c80182005-01-15 22:15:51 +0000880 type = TOK_PREPROC_ID;
881 } else if (*p == '{') {
882 p++;
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800883 while (*p) {
884 if (*p == '}')
885 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000886 p[-1] = *p;
887 p++;
888 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800889 if (*p != '}')
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -0800890 nasm_warn(WARN_OTHER, "unterminated %%{ construct");
H. Peter Anvine2c80182005-01-15 22:15:51 +0000891 p[-1] = '\0';
892 if (*p)
893 p++;
894 type = TOK_PREPROC_ID;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300895 } else if (*p == '[') {
896 int lvl = 1;
897 line += 2; /* Skip the leading %[ */
898 p++;
899 while (lvl && (c = *p++)) {
900 switch (c) {
901 case ']':
902 lvl--;
903 break;
904 case '%':
905 if (*p == '[')
906 lvl++;
907 break;
908 case '\'':
909 case '\"':
910 case '`':
Cyrill Gorcunov3144e842017-10-22 10:50:55 +0300911 p = nasm_skip_string(p - 1);
912 if (*p)
913 p++;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300914 break;
915 default:
916 break;
917 }
918 }
919 p--;
920 if (*p)
921 *p++ = '\0';
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800922 if (lvl)
Cyrill Gorcunov295b7952018-11-25 12:55:48 +0300923 nasm_nonfatalf(ERR_PASS1, "unterminated %%[ construct");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300924 type = TOK_INDIRECT;
925 } else if (*p == '?') {
926 type = TOK_PREPROC_Q; /* %? */
927 p++;
928 if (*p == '?') {
929 type = TOK_PREPROC_QQ; /* %?? */
930 p++;
931 }
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +0400932 } else if (*p == '!') {
933 type = TOK_PREPROC_ID;
934 p++;
H. Peter Anvin13506202018-11-28 14:55:58 -0800935 if (nasm_isidchar(*p)) {
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +0400936 do {
937 p++;
938 }
H. Peter Anvin13506202018-11-28 14:55:58 -0800939 while (nasm_isidchar(*p));
H. Peter Anvin53e2e4c2018-11-28 15:01:40 -0800940 } else if (nasm_isquote(*p)) {
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +0400941 p = nasm_skip_string(p);
942 if (*p)
943 p++;
944 else
Cyrill Gorcunov295b7952018-11-25 12:55:48 +0300945 nasm_nonfatalf(ERR_PASS1, "unterminated %%! string");
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +0400946 } else {
947 /* %! without string or identifier */
948 type = TOK_OTHER; /* Legacy behavior... */
949 }
H. Peter Anvin13506202018-11-28 14:55:58 -0800950 } else if (nasm_isidchar(*p) ||
H. Peter Anvine2c80182005-01-15 22:15:51 +0000951 ((*p == '!' || *p == '%' || *p == '$') &&
H. Peter Anvin13506202018-11-28 14:55:58 -0800952 nasm_isidchar(p[1]))) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000953 do {
954 p++;
955 }
H. Peter Anvin13506202018-11-28 14:55:58 -0800956 while (nasm_isidchar(*p));
H. Peter Anvine2c80182005-01-15 22:15:51 +0000957 type = TOK_PREPROC_ID;
958 } else {
959 type = TOK_OTHER;
960 if (*p == '%')
961 p++;
962 }
H. Peter Anvin13506202018-11-28 14:55:58 -0800963 } else if (nasm_isidstart(*p) || (*p == '$' && nasm_isidstart(p[1]))) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000964 type = TOK_ID;
965 p++;
H. Peter Anvin13506202018-11-28 14:55:58 -0800966 while (*p && nasm_isidchar(*p))
H. Peter Anvine2c80182005-01-15 22:15:51 +0000967 p++;
H. Peter Anvin53e2e4c2018-11-28 15:01:40 -0800968 } else if (nasm_isquote(*p)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000969 /*
970 * A string token.
971 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000972 type = TOK_STRING;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300973 p = nasm_skip_string(p);
Nickolay Yurchenkof3b3ce22003-09-21 20:38:43 +0000974
H. Peter Anvine2c80182005-01-15 22:15:51 +0000975 if (*p) {
976 p++;
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800977 } else {
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -0800978 nasm_warn(WARN_OTHER, "unterminated string");
H. Peter Anvine2c80182005-01-15 22:15:51 +0000979 /* Handling unterminated strings by UNV */
980 /* type = -1; */
981 }
Victor van den Elzenfb5f2512009-04-17 16:17:59 +0200982 } else if (p[0] == '$' && p[1] == '$') {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300983 type = TOK_OTHER; /* TOKEN_BASE */
Victor van den Elzenfb5f2512009-04-17 16:17:59 +0200984 p += 2;
H. Peter Anvin13506202018-11-28 14:55:58 -0800985 } else if (nasm_isnumstart(*p)) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300986 bool is_hex = false;
987 bool is_float = false;
988 bool has_e = false;
989 char c, *r;
H. Peter Anvinc2df2822007-10-24 15:29:28 -0700990
H. Peter Anvine2c80182005-01-15 22:15:51 +0000991 /*
H. Peter Anvinc2df2822007-10-24 15:29:28 -0700992 * A numeric token.
H. Peter Anvine2c80182005-01-15 22:15:51 +0000993 */
H. Peter Anvinc2df2822007-10-24 15:29:28 -0700994
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300995 if (*p == '$') {
996 p++;
997 is_hex = true;
998 }
H. Peter Anvinc2df2822007-10-24 15:29:28 -0700999
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001000 for (;;) {
1001 c = *p++;
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001002
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001003 if (!is_hex && (c == 'e' || c == 'E')) {
1004 has_e = true;
1005 if (*p == '+' || *p == '-') {
1006 /*
1007 * e can only be followed by +/- if it is either a
1008 * prefixed hex number or a floating-point number
1009 */
1010 p++;
1011 is_float = true;
1012 }
1013 } else if (c == 'H' || c == 'h' || c == 'X' || c == 'x') {
1014 is_hex = true;
1015 } else if (c == 'P' || c == 'p') {
1016 is_float = true;
1017 if (*p == '+' || *p == '-')
1018 p++;
H. Peter Anvin13506202018-11-28 14:55:58 -08001019 } else if (nasm_isnumchar(c))
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001020 ; /* just advance */
1021 else if (c == '.') {
1022 /*
1023 * we need to deal with consequences of the legacy
1024 * parser, like "1.nolist" being two tokens
1025 * (TOK_NUMBER, TOK_ID) here; at least give it
1026 * a shot for now. In the future, we probably need
1027 * a flex-based scanner with proper pattern matching
1028 * to do it as well as it can be done. Nothing in
1029 * the world is going to help the person who wants
1030 * 0x123.p16 interpreted as two tokens, though.
1031 */
1032 r = p;
1033 while (*r == '_')
1034 r++;
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001035
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001036 if (nasm_isdigit(*r) || (is_hex && nasm_isxdigit(*r)) ||
1037 (!is_hex && (*r == 'e' || *r == 'E')) ||
1038 (*r == 'p' || *r == 'P')) {
1039 p = r;
1040 is_float = true;
1041 } else
1042 break; /* Terminate the token */
1043 } else
1044 break;
1045 }
1046 p--; /* Point to first character beyond number */
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001047
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001048 if (p == line+1 && *line == '$') {
1049 type = TOK_OTHER; /* TOKEN_HERE */
1050 } else {
1051 if (has_e && !is_hex) {
1052 /* 1e13 is floating-point, but 1e13h is not */
1053 is_float = true;
1054 }
H. Peter Anvind784a082009-04-20 14:01:18 -07001055
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001056 type = is_float ? TOK_FLOAT : TOK_NUMBER;
1057 }
H. Peter Anvinbda7a6e2008-06-21 10:23:17 -07001058 } else if (nasm_isspace(*p)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001059 type = TOK_WHITESPACE;
Cyrill Gorcunovf66ac7d2009-10-12 20:41:13 +04001060 p = nasm_skip_spaces(p);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001061 /*
1062 * Whitespace just before end-of-line is discarded by
1063 * pretending it's a comment; whitespace just before a
1064 * comment gets lumped into the comment.
1065 */
1066 if (!*p || *p == ';') {
1067 type = TOK_COMMENT;
1068 while (*p)
1069 p++;
1070 }
1071 } else if (*p == ';') {
1072 type = TOK_COMMENT;
1073 while (*p)
1074 p++;
1075 } else {
1076 /*
1077 * Anything else is an operator of some kind. We check
1078 * for all the double-character operators (>>, <<, //,
1079 * %%, <=, >=, ==, !=, <>, &&, ||, ^^), but anything
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001080 * else is a single-character operator.
H. Peter Anvine2c80182005-01-15 22:15:51 +00001081 */
1082 type = TOK_OTHER;
1083 if ((p[0] == '>' && p[1] == '>') ||
1084 (p[0] == '<' && p[1] == '<') ||
1085 (p[0] == '/' && p[1] == '/') ||
1086 (p[0] == '<' && p[1] == '=') ||
1087 (p[0] == '>' && p[1] == '=') ||
1088 (p[0] == '=' && p[1] == '=') ||
1089 (p[0] == '!' && p[1] == '=') ||
1090 (p[0] == '<' && p[1] == '>') ||
1091 (p[0] == '&' && p[1] == '&') ||
1092 (p[0] == '|' && p[1] == '|') ||
1093 (p[0] == '^' && p[1] == '^')) {
1094 p++;
1095 }
1096 p++;
1097 }
Nickolay Yurchenkof3b3ce22003-09-21 20:38:43 +00001098
H. Peter Anvine2c80182005-01-15 22:15:51 +00001099 /* Handling unterminated string by UNV */
1100 /*if (type == -1)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001101 {
1102 *tail = t = new_Token(NULL, TOK_STRING, line, p-line+1);
1103 t->text[p-line] = *line;
1104 tail = &t->next;
1105 }
1106 else */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001107 if (type != TOK_COMMENT) {
1108 *tail = t = new_Token(NULL, type, line, p - line);
1109 tail = &t->next;
1110 }
1111 line = p;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001112 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001113 return list;
1114}
1115
H. Peter Anvince616072002-04-30 21:02:23 +00001116/*
1117 * this function allocates a new managed block of memory and
H. Peter Anvin70653092007-10-19 14:42:29 -07001118 * returns a pointer to the block. The managed blocks are
H. Peter Anvince616072002-04-30 21:02:23 +00001119 * deleted only all at once by the delete_Blocks function.
1120 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001121static void *new_Block(size_t size)
H. Peter Anvince616072002-04-30 21:02:23 +00001122{
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001123 Blocks *b = &blocks;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001124
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001125 /* first, get to the end of the linked list */
1126 while (b->next)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001127 b = b->next;
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001128 /* now allocate the requested chunk */
1129 b->chunk = nasm_malloc(size);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001130
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001131 /* now allocate a new block for the next request */
Cyrill Gorcunovc31767c2014-06-28 02:22:17 +04001132 b->next = nasm_zalloc(sizeof(Blocks));
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001133 return b->chunk;
H. Peter Anvince616072002-04-30 21:02:23 +00001134}
1135
1136/*
1137 * this function deletes all managed blocks of memory
1138 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001139static void delete_Blocks(void)
H. Peter Anvince616072002-04-30 21:02:23 +00001140{
H. Peter Anvine2c80182005-01-15 22:15:51 +00001141 Blocks *a, *b = &blocks;
H. Peter Anvince616072002-04-30 21:02:23 +00001142
H. Peter Anvin70653092007-10-19 14:42:29 -07001143 /*
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001144 * keep in mind that the first block, pointed to by blocks
H. Peter Anvin70653092007-10-19 14:42:29 -07001145 * is a static and not dynamically allocated, so we don't
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001146 * free it.
1147 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001148 while (b) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001149 if (b->chunk)
1150 nasm_free(b->chunk);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001151 a = b;
1152 b = b->next;
1153 if (a != &blocks)
1154 nasm_free(a);
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001155 }
Cyrill Gorcunovdae24d72014-06-28 10:17:39 +04001156 memset(&blocks, 0, sizeof(blocks));
H. Peter Anvine2c80182005-01-15 22:15:51 +00001157}
H. Peter Anvin734b1882002-04-30 21:01:08 +00001158
1159/*
H. Peter Anvin70653092007-10-19 14:42:29 -07001160 * this function creates a new Token and passes a pointer to it
H. Peter Anvin734b1882002-04-30 21:01:08 +00001161 * back to the caller. It sets the type and text elements, and
H. Peter Anvinf26e0972008-07-01 21:26:27 -07001162 * also the a.mac and next elements to NULL.
H. Peter Anvin734b1882002-04-30 21:01:08 +00001163 */
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07001164static Token *new_Token(Token * next, enum pp_token_type type,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001165 const char *text, int txtlen)
H. Peter Anvin734b1882002-04-30 21:01:08 +00001166{
1167 Token *t;
1168 int i;
1169
H. Peter Anvin89cee572009-07-15 09:16:54 -04001170 if (!freeTokens) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001171 freeTokens = (Token *) new_Block(TOKEN_BLOCKSIZE * sizeof(Token));
1172 for (i = 0; i < TOKEN_BLOCKSIZE - 1; i++)
1173 freeTokens[i].next = &freeTokens[i + 1];
1174 freeTokens[i].next = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001175 }
1176 t = freeTokens;
1177 freeTokens = t->next;
1178 t->next = next;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07001179 t->a.mac = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001180 t->type = type;
H. Peter Anvin89cee572009-07-15 09:16:54 -04001181 if (type == TOK_WHITESPACE || !text) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001182 t->text = NULL;
1183 } else {
1184 if (txtlen == 0)
1185 txtlen = strlen(text);
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07001186 t->text = nasm_malloc(txtlen+1);
1187 memcpy(t->text, text, txtlen);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001188 t->text[txtlen] = '\0';
H. Peter Anvin734b1882002-04-30 21:01:08 +00001189 }
1190 return t;
1191}
1192
H. Peter Anvine2c80182005-01-15 22:15:51 +00001193static Token *delete_Token(Token * t)
H. Peter Anvin734b1882002-04-30 21:01:08 +00001194{
1195 Token *next = t->next;
1196 nasm_free(t->text);
H. Peter Anvin788e6c12002-04-30 21:02:01 +00001197 t->next = freeTokens;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001198 freeTokens = t;
1199 return next;
1200}
1201
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001202/*
1203 * Convert a line of tokens back into text.
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001204 * If expand_locals is not zero, identifiers of the form "%$*xxx"
1205 * will be transformed into ..@ctxnum.xxx
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001206 */
H. Peter Anvin9e200162008-06-04 17:23:14 -07001207static char *detoken(Token * tlist, bool expand_locals)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001208{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001209 Token *t;
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001210 char *line, *p;
H. Peter Anvinb4daadc2008-01-21 16:31:57 -08001211 const char *q;
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001212 int len = 0;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001213
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001214 list_for_each(t, tlist) {
Cyrill Gorcunov9b7ee092017-10-22 21:42:59 +03001215 if (t->type == TOK_PREPROC_ID && t->text &&
1216 t->text[0] && t->text[1] == '!') {
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001217 char *v;
1218 char *q = t->text;
H. Peter Anvin077fb932010-07-20 14:56:30 -07001219
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001220 v = t->text + 2;
H. Peter Anvin53e2e4c2018-11-28 15:01:40 -08001221 if (nasm_isquote(*v)) {
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001222 size_t len = nasm_unquote(v, NULL);
1223 size_t clen = strlen(v);
H. Peter Anvin077fb932010-07-20 14:56:30 -07001224
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001225 if (len != clen) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03001226 nasm_nonfatalf(ERR_PASS1, "NUL character in %%! string");
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001227 v = NULL;
1228 }
1229 }
H. Peter Anvin077fb932010-07-20 14:56:30 -07001230
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001231 if (v) {
1232 char *p = getenv(v);
1233 if (!p) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03001234 nasm_nonfatalf(ERR_PASS1, "nonexistent environment variable `%s'", v);
Cyrill Gorcunovbbb7a1a2016-06-19 12:15:24 +03001235 /*
1236 * FIXME We better should investigate if accessing
1237 * ->text[1] without ->text[0] is safe enough.
1238 */
1239 t->text = nasm_zalloc(2);
1240 } else
1241 t->text = nasm_strdup(p);
Cyrill Gorcunov75004872017-07-26 01:21:16 +03001242 nasm_free(q);
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001243 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001244 }
H. Peter Anvin077fb932010-07-20 14:56:30 -07001245
H. Peter Anvine2c80182005-01-15 22:15:51 +00001246 /* Expand local macros here and not during preprocessing */
1247 if (expand_locals &&
1248 t->type == TOK_PREPROC_ID && t->text &&
1249 t->text[0] == '%' && t->text[1] == '$') {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001250 const char *q;
1251 char *p;
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04001252 Context *ctx = get_ctx(t->text, &q);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001253 if (ctx) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001254 char buffer[40];
Keith Kanios93f2e9a2007-04-14 00:10:59 +00001255 snprintf(buffer, sizeof(buffer), "..@%"PRIu32".", ctx->number);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001256 p = nasm_strcat(buffer, q);
1257 nasm_free(t->text);
1258 t->text = p;
1259 }
1260 }
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001261 if (t->type == TOK_WHITESPACE)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001262 len++;
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001263 else if (t->text)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001264 len += strlen(t->text);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001265 }
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001266
H. Peter Anvin734b1882002-04-30 21:01:08 +00001267 p = line = nasm_malloc(len + 1);
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001268
1269 list_for_each(t, tlist) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001270 if (t->type == TOK_WHITESPACE) {
H. Peter Anvinb4daadc2008-01-21 16:31:57 -08001271 *p++ = ' ';
H. Peter Anvine2c80182005-01-15 22:15:51 +00001272 } else if (t->text) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001273 q = t->text;
1274 while (*q)
1275 *p++ = *q++;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001276 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001277 }
1278 *p = '\0';
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001279
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001280 return line;
1281}
1282
1283/*
H. Peter Anvin76690a12002-04-30 20:52:49 +00001284 * A scanner, suitable for use by the expression evaluator, which
1285 * operates on a line of Tokens. Expects a pointer to a pointer to
1286 * the first token in the line to be passed in as its private_data
1287 * field.
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001288 *
1289 * FIX: This really needs to be unified with stdscan.
H. Peter Anvin76690a12002-04-30 20:52:49 +00001290 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001291static int ppscan(void *private_data, struct tokenval *tokval)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001292{
H. Peter Anvin76690a12002-04-30 20:52:49 +00001293 Token **tlineptr = private_data;
1294 Token *tline;
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001295 char ourcopy[MAX_KEYWORD+1], *p, *r, *s;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001296
H. Peter Anvine2c80182005-01-15 22:15:51 +00001297 do {
1298 tline = *tlineptr;
1299 *tlineptr = tline ? tline->next : NULL;
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04001300 } while (tline && (tline->type == TOK_WHITESPACE ||
1301 tline->type == TOK_COMMENT));
H. Peter Anvin76690a12002-04-30 20:52:49 +00001302
1303 if (!tline)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001304 return tokval->t_type = TOKEN_EOS;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001305
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001306 tokval->t_charptr = tline->text;
1307
H. Peter Anvin76690a12002-04-30 20:52:49 +00001308 if (tline->text[0] == '$' && !tline->text[1])
H. Peter Anvine2c80182005-01-15 22:15:51 +00001309 return tokval->t_type = TOKEN_HERE;
H. Peter Anvin7cf897e2002-05-30 21:30:33 +00001310 if (tline->text[0] == '$' && tline->text[1] == '$' && !tline->text[2])
H. Peter Anvine2c80182005-01-15 22:15:51 +00001311 return tokval->t_type = TOKEN_BASE;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001312
H. Peter Anvine2c80182005-01-15 22:15:51 +00001313 if (tline->type == TOK_ID) {
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001314 p = tokval->t_charptr = tline->text;
1315 if (p[0] == '$') {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001316 tokval->t_charptr++;
1317 return tokval->t_type = TOKEN_ID;
1318 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00001319
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001320 for (r = p, s = ourcopy; *r; r++) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001321 if (r >= p+MAX_KEYWORD)
1322 return tokval->t_type = TOKEN_ID; /* Not a keyword */
H. Peter Anvinac8f8fc2008-06-11 15:49:41 -07001323 *s++ = nasm_tolower(*r);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001324 }
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001325 *s = '\0';
1326 /* right, so we have an identifier sitting in temp storage. now,
1327 * is it actually a register or instruction name, or what? */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001328 return nasm_token_hash(ourcopy, tokval);
H. Peter Anvin76690a12002-04-30 20:52:49 +00001329 }
1330
H. Peter Anvine2c80182005-01-15 22:15:51 +00001331 if (tline->type == TOK_NUMBER) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001332 bool rn_error;
1333 tokval->t_integer = readnum(tline->text, &rn_error);
1334 tokval->t_charptr = tline->text;
1335 if (rn_error)
1336 return tokval->t_type = TOKEN_ERRNUM;
1337 else
1338 return tokval->t_type = TOKEN_NUM;
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001339 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00001340
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001341 if (tline->type == TOK_FLOAT) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001342 return tokval->t_type = TOKEN_FLOAT;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001343 }
1344
H. Peter Anvine2c80182005-01-15 22:15:51 +00001345 if (tline->type == TOK_STRING) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001346 char bq, *ep;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001347
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001348 bq = tline->text[0];
H. Peter Anvin11627042008-06-09 20:45:19 -07001349 tokval->t_charptr = tline->text;
1350 tokval->t_inttwo = nasm_unquote(tline->text, &ep);
H. Peter Anvind2456592008-06-19 15:04:18 -07001351
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001352 if (ep[0] != bq || ep[1] != '\0')
1353 return tokval->t_type = TOKEN_ERRSTR;
1354 else
1355 return tokval->t_type = TOKEN_STR;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001356 }
1357
H. Peter Anvine2c80182005-01-15 22:15:51 +00001358 if (tline->type == TOK_OTHER) {
1359 if (!strcmp(tline->text, "<<"))
1360 return tokval->t_type = TOKEN_SHL;
1361 if (!strcmp(tline->text, ">>"))
1362 return tokval->t_type = TOKEN_SHR;
1363 if (!strcmp(tline->text, "//"))
1364 return tokval->t_type = TOKEN_SDIV;
1365 if (!strcmp(tline->text, "%%"))
1366 return tokval->t_type = TOKEN_SMOD;
1367 if (!strcmp(tline->text, "=="))
1368 return tokval->t_type = TOKEN_EQ;
1369 if (!strcmp(tline->text, "<>"))
1370 return tokval->t_type = TOKEN_NE;
1371 if (!strcmp(tline->text, "!="))
1372 return tokval->t_type = TOKEN_NE;
1373 if (!strcmp(tline->text, "<="))
1374 return tokval->t_type = TOKEN_LE;
1375 if (!strcmp(tline->text, ">="))
1376 return tokval->t_type = TOKEN_GE;
1377 if (!strcmp(tline->text, "&&"))
1378 return tokval->t_type = TOKEN_DBL_AND;
1379 if (!strcmp(tline->text, "^^"))
1380 return tokval->t_type = TOKEN_DBL_XOR;
1381 if (!strcmp(tline->text, "||"))
1382 return tokval->t_type = TOKEN_DBL_OR;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001383 }
1384
1385 /*
1386 * We have no other options: just return the first character of
1387 * the token text.
1388 */
1389 return tokval->t_type = tline->text[0];
1390}
1391
1392/*
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001393 * Compare a string to the name of an existing macro; this is a
1394 * simple wrapper which calls either strcmp or nasm_stricmp
1395 * depending on the value of the `casesense' parameter.
1396 */
H. Peter Anvin4db5a162007-10-11 13:42:09 -07001397static int mstrcmp(const char *p, const char *q, bool casesense)
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001398{
H. Peter Anvin734b1882002-04-30 21:01:08 +00001399 return casesense ? strcmp(p, q) : nasm_stricmp(p, q);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001400}
1401
1402/*
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07001403 * Compare a string to the name of an existing macro; this is a
1404 * simple wrapper which calls either strcmp or nasm_stricmp
1405 * depending on the value of the `casesense' parameter.
1406 */
1407static int mmemcmp(const char *p, const char *q, size_t l, bool casesense)
1408{
1409 return casesense ? memcmp(p, q, l) : nasm_memicmp(p, q, l);
1410}
1411
1412/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001413 * Return the Context structure associated with a %$ token. Return
1414 * NULL, having _already_ reported an error condition, if the
1415 * context stack isn't deep enough for the supplied number of $
1416 * signs.
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001417 *
1418 * If "namep" is non-NULL, set it to the pointer to the macro name
1419 * tail, i.e. the part beyond %$...
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001420 */
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04001421static Context *get_ctx(const char *name, const char **namep)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001422{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001423 Context *ctx;
1424 int i;
1425
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001426 if (namep)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001427 *namep = name;
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001428
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001429 if (!name || name[0] != '%' || name[1] != '$')
H. Peter Anvine2c80182005-01-15 22:15:51 +00001430 return NULL;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001431
H. Peter Anvine2c80182005-01-15 22:15:51 +00001432 if (!cstk) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03001433 nasm_nonfatal("`%s': context stack is empty", name);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001434 return NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001435 }
1436
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001437 name += 2;
1438 ctx = cstk;
1439 i = 0;
1440 while (ctx && *name == '$') {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001441 name++;
1442 i++;
1443 ctx = ctx->next;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001444 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001445 if (!ctx) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03001446 nasm_nonfatal("`%s': context stack is only"
1447 " %d level%s deep", name, i, (i == 1 ? "" : "s"));
H. Peter Anvine2c80182005-01-15 22:15:51 +00001448 return NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001449 }
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001450
1451 if (namep)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001452 *namep = name;
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001453
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04001454 return ctx;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001455}
1456
1457/*
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001458 * Open an include file. This routine must always return a valid
1459 * file pointer if it returns - it's responsible for throwing an
1460 * ERR_FATAL and bombing out completely if not. It should also try
1461 * the include path one by one until it finds the file or reaches
1462 * the end of the path.
H. Peter Anvind81a2352016-09-21 14:03:18 -07001463 *
1464 * Note: for INC_PROBE the function returns NULL at all times;
1465 * instead look for the
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001466 */
H. Peter Anvind81a2352016-09-21 14:03:18 -07001467enum incopen_mode {
1468 INC_NEEDED, /* File must exist */
1469 INC_OPTIONAL, /* Missing is OK */
1470 INC_PROBE /* Only an existence probe */
1471};
1472
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001473/* This is conducts a full pathname search */
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07001474static FILE *inc_fopen_search(const char *file, char **slpath,
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001475 enum incopen_mode omode, enum file_flags fmode)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001476{
H. Peter Anvin (Intel)64471092018-12-11 13:06:14 -08001477 const struct strlist_entry *ip = strlist_head(ipath_list);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001478 FILE *fp;
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07001479 const char *prefix = "";
night199ukfdb1a1b2018-10-18 23:19:47 +02001480 char *sp;
H. Peter Anvind81a2352016-09-21 14:03:18 -07001481 bool found;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001482
H. Peter Anvine2c80182005-01-15 22:15:51 +00001483 while (1) {
night199ukfdb1a1b2018-10-18 23:19:47 +02001484 sp = nasm_catfile(prefix, file);
H. Peter Anvind81a2352016-09-21 14:03:18 -07001485 if (omode == INC_PROBE) {
1486 fp = NULL;
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07001487 found = nasm_file_exists(sp);
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001488 } else {
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07001489 fp = nasm_open_read(sp, fmode);
H. Peter Anvind81a2352016-09-21 14:03:18 -07001490 found = (fp != NULL);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001491 }
H. Peter Anvind81a2352016-09-21 14:03:18 -07001492 if (found) {
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07001493 *slpath = sp;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001494 return fp;
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001495 }
Jim Kukunas65a8afc2016-06-13 16:00:42 -04001496
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07001497 nasm_free(sp);
Jim Kukunas65a8afc2016-06-13 16:00:42 -04001498
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07001499 if (!ip) {
1500 *slpath = NULL;
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001501 return NULL;
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07001502 }
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001503
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07001504 prefix = ip->str;
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001505 ip = ip->next;
1506 }
1507}
1508
1509/*
1510 * Open a file, or test for the presence of one (depending on omode),
1511 * considering the include path.
1512 */
1513static FILE *inc_fopen(const char *file,
Cyrill Gorcunovb7bb5ac2018-11-11 21:33:52 +03001514 struct strlist *dhead,
H. Peter Anvinccad6f92016-10-04 00:34:35 -07001515 const char **found_path,
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001516 enum incopen_mode omode,
1517 enum file_flags fmode)
1518{
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001519 struct hash_insert hi;
1520 void **hp;
1521 char *path;
1522 FILE *fp = NULL;
1523
1524 hp = hash_find(&FileHash, file, &hi);
1525 if (hp) {
1526 path = *hp;
Martin Storsjöf283c8f2017-08-13 17:28:46 +03001527 if (path || omode != INC_NEEDED) {
Cyrill Gorcunovb7bb5ac2018-11-11 21:33:52 +03001528 strlist_add(dhead, path ? path : file);
Martin Storsjöf283c8f2017-08-13 17:28:46 +03001529 }
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001530 } else {
1531 /* Need to do the actual path search */
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07001532 fp = inc_fopen_search(file, &path, omode, fmode);
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001533
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07001534 /* Positive or negative result */
1535 hash_add(&hi, nasm_strdup(file), path);
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001536
H. Peter Anvin9924d1e2016-10-04 00:59:39 -07001537 /*
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07001538 * Add file to dependency path.
H. Peter Anvin9924d1e2016-10-04 00:59:39 -07001539 */
1540 if (path || omode != INC_NEEDED)
Cyrill Gorcunovb7bb5ac2018-11-11 21:33:52 +03001541 strlist_add(dhead, file);
H. Peter Anvineba20a72002-04-30 20:53:55 +00001542 }
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001543
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001544 if (!path) {
1545 if (omode == INC_NEEDED)
H. Peter Anvinc5136902018-06-15 18:20:17 -07001546 nasm_fatal("unable to open include file `%s'", file);
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07001547 } else {
1548 if (!fp && omode != INC_PROBE)
1549 fp = nasm_open_read(path, fmode);
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001550 }
1551
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001552 if (found_path)
H. Peter Anvinccad6f92016-10-04 00:34:35 -07001553 *found_path = path;
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001554
1555 return fp;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001556}
1557
1558/*
Fabian Giesen0bbc38d2016-04-28 13:48:14 -07001559 * Opens an include or input file. Public version, for use by modules
1560 * that get a file:lineno pair and need to look at the file again
1561 * (e.g. the CodeView debug backend). Returns NULL on failure.
1562 */
H. Peter Anvin3e83cec2016-05-25 04:28:46 -07001563FILE *pp_input_fopen(const char *filename, enum file_flags mode)
Fabian Giesen0bbc38d2016-04-28 13:48:14 -07001564{
H. Peter Anvin9924d1e2016-10-04 00:59:39 -07001565 return inc_fopen(filename, NULL, NULL, INC_OPTIONAL, mode);
Fabian Giesen0bbc38d2016-04-28 13:48:14 -07001566}
1567
1568/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001569 * Determine if we should warn on defining a single-line macro of
H. Peter Anvinef7468f2002-04-30 20:57:59 +00001570 * name `name', with `nparam' parameters. If nparam is 0 or -1, will
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001571 * return true if _any_ single-line macro of that name is defined.
1572 * Otherwise, will return true if a single-line macro with either
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001573 * `nparam' or no parameters is defined.
1574 *
1575 * If a macro with precisely the right number of parameters is
H. Peter Anvinef7468f2002-04-30 20:57:59 +00001576 * defined, or nparam is -1, the address of the definition structure
1577 * will be returned in `defn'; otherwise NULL will be returned. If `defn'
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001578 * is NULL, no action will be taken regarding its contents, and no
1579 * error will occur.
1580 *
1581 * Note that this is also called with nparam zero to resolve
1582 * `ifdef'.
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001583 *
1584 * If you already know which context macro belongs to, you can pass
1585 * the context pointer as first parameter; if you won't but name begins
1586 * with %$ the context will be automatically computed. If all_contexts
1587 * is true, macro will be searched in outer contexts as well.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001588 */
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07001589static bool
H. Peter Anvinb2a5fda2008-06-19 21:42:42 -07001590smacro_defined(Context * ctx, const char *name, int nparam, SMacro ** defn,
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07001591 bool nocase)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001592{
H. Peter Anvin166c2472008-05-28 12:28:58 -07001593 struct hash_table *smtbl;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001594 SMacro *m;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001595
H. Peter Anvin97a23472007-09-16 17:57:25 -07001596 if (ctx) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001597 smtbl = &ctx->localmac;
H. Peter Anvin97a23472007-09-16 17:57:25 -07001598 } else if (name[0] == '%' && name[1] == '$') {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001599 if (cstk)
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04001600 ctx = get_ctx(name, &name);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001601 if (!ctx)
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001602 return false; /* got to return _something_ */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001603 smtbl = &ctx->localmac;
H. Peter Anvin97a23472007-09-16 17:57:25 -07001604 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001605 smtbl = &smacros;
H. Peter Anvin97a23472007-09-16 17:57:25 -07001606 }
H. Peter Anvin166c2472008-05-28 12:28:58 -07001607 m = (SMacro *) hash_findix(smtbl, name);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001608
H. Peter Anvine2c80182005-01-15 22:15:51 +00001609 while (m) {
1610 if (!mstrcmp(m->name, name, m->casesense && nocase) &&
Charles Crayne192d5b52007-10-18 19:02:42 -07001611 (nparam <= 0 || m->nparam == 0 || nparam == (int) m->nparam)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001612 if (defn) {
Charles Crayne192d5b52007-10-18 19:02:42 -07001613 if (nparam == (int) m->nparam || nparam == -1)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001614 *defn = m;
1615 else
1616 *defn = NULL;
1617 }
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001618 return true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001619 }
1620 m = m->next;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001621 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001622
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001623 return false;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001624}
1625
Cyrill Gorcunov3079f792018-11-14 10:03:42 +03001626/* param should be a natural number [0; INT_MAX] */
1627static int read_param_count(const char *str)
1628{
1629 int result;
1630 bool err;
1631
1632 result = readnum(str, &err);
1633 if (result < 0 || result > INT_MAX) {
1634 result = 0;
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03001635 nasm_nonfatal("parameter count `%s' is out of bounds [%d; %d]",
1636 str, 0, INT_MAX);
1637 } else if (err)
1638 nasm_nonfatal("unable to parse parameter count `%s'", str);
Cyrill Gorcunov3079f792018-11-14 10:03:42 +03001639 return result;
1640}
1641
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001642/*
1643 * Count and mark off the parameters in a multi-line macro call.
1644 * This is called both from within the multi-line macro expansion
1645 * code, and also to mark off the default parameters when provided
1646 * in a %macro definition line.
1647 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001648static void count_mmac_params(Token * t, int *nparam, Token *** params)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001649{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001650 int paramsize, brace;
1651
1652 *nparam = paramsize = 0;
1653 *params = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001654 while (t) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001655 /* +1: we need space for the final NULL */
H. Peter Anvin91fb6f12008-09-01 10:56:33 -07001656 if (*nparam+1 >= paramsize) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001657 paramsize += PARAM_DELTA;
1658 *params = nasm_realloc(*params, sizeof(**params) * paramsize);
1659 }
1660 skip_white_(t);
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08001661 brace = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001662 if (tok_is_(t, "{"))
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08001663 brace++;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001664 (*params)[(*nparam)++] = t;
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08001665 if (brace) {
1666 while (brace && (t = t->next) != NULL) {
1667 if (tok_is_(t, "{"))
1668 brace++;
1669 else if (tok_is_(t, "}"))
1670 brace--;
1671 }
1672
1673 if (t) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001674 /*
1675 * Now we've found the closing brace, look further
1676 * for the comma.
1677 */
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08001678 t = t->next;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001679 skip_white_(t);
1680 if (tok_isnt_(t, ",")) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03001681 nasm_nonfatal("braces do not enclose all of macro parameter");
H. Peter Anvine2c80182005-01-15 22:15:51 +00001682 while (tok_isnt_(t, ","))
1683 t = t->next;
1684 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001685 }
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08001686 } else {
1687 while (tok_isnt_(t, ","))
1688 t = t->next;
1689 }
1690 if (t) { /* got a comma/brace */
1691 t = t->next; /* eat the comma */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001692 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001693 }
1694}
1695
1696/*
H. Peter Anvin76690a12002-04-30 20:52:49 +00001697 * Determine whether one of the various `if' conditions is true or
1698 * not.
1699 *
1700 * We must free the tline we get passed.
1701 */
H. Peter Anvin70055962007-10-11 00:05:31 -07001702static bool if_condition(Token * tline, enum preproc_token ct)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001703{
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001704 enum pp_conditional i = PP_COND(ct);
H. Peter Anvin70055962007-10-11 00:05:31 -07001705 bool j;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001706 Token *t, *tt, **tptr, *origline;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001707 struct tokenval tokval;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001708 expr *evalresult;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001709 enum pp_token_type needtype;
H. Peter Anvin077fb932010-07-20 14:56:30 -07001710 char *p;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001711
1712 origline = tline;
1713
H. Peter Anvine2c80182005-01-15 22:15:51 +00001714 switch (i) {
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001715 case PPC_IFCTX:
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001716 j = false; /* have we matched yet? */
Victor van den Elzen0e857f12008-07-23 13:21:29 +02001717 while (true) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001718 skip_white_(tline);
Victor van den Elzen0e857f12008-07-23 13:21:29 +02001719 if (!tline)
1720 break;
1721 if (tline->type != TOK_ID) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03001722 nasm_nonfatal("`%s' expects context identifiers",
1723 pp_directives[ct]);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001724 free_tlist(origline);
1725 return -1;
1726 }
Victor van den Elzen0e857f12008-07-23 13:21:29 +02001727 if (cstk && cstk->name && !nasm_stricmp(tline->text, cstk->name))
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001728 j = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001729 tline = tline->next;
1730 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001731 break;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001732
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001733 case PPC_IFDEF:
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001734 j = false; /* have we matched yet? */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001735 while (tline) {
1736 skip_white_(tline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001737 if (!tline || (tline->type != TOK_ID &&
1738 (tline->type != TOK_PREPROC_ID ||
1739 tline->text[1] != '$'))) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03001740 nasm_nonfatal("`%s' expects macro identifiers",
1741 pp_directives[ct]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001742 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001743 }
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07001744 if (smacro_defined(NULL, tline->text, 0, NULL, true))
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001745 j = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001746 tline = tline->next;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001747 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001748 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001749
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001750 case PPC_IFENV:
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001751 tline = expand_smacro(tline);
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001752 j = false; /* have we matched yet? */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001753 while (tline) {
1754 skip_white_(tline);
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001755 if (!tline || (tline->type != TOK_ID &&
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001756 tline->type != TOK_STRING &&
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001757 (tline->type != TOK_PREPROC_ID ||
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001758 tline->text[1] != '!'))) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03001759 nasm_nonfatal("`%s' expects environment variable names",
1760 pp_directives[ct]);
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001761 goto fail;
1762 }
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001763 p = tline->text;
1764 if (tline->type == TOK_PREPROC_ID)
1765 p += 2; /* Skip leading %! */
H. Peter Anvin53e2e4c2018-11-28 15:01:40 -08001766 if (nasm_isquote(*p))
H. Peter Anvinbb42d302019-04-22 14:29:29 -07001767 nasm_unquote_cstr(p, NULL);
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001768 if (getenv(p))
1769 j = true;
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001770 tline = tline->next;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001771 }
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001772 break;
1773
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001774 case PPC_IFIDN:
1775 case PPC_IFIDNI:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001776 tline = expand_smacro(tline);
1777 t = tt = tline;
1778 while (tok_isnt_(tt, ","))
1779 tt = tt->next;
1780 if (!tt) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03001781 nasm_nonfatal("`%s' expects two comma-separated arguments",
1782 pp_directives[ct]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001783 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001784 }
1785 tt = tt->next;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001786 j = true; /* assume equality unless proved not */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001787 while ((t->type != TOK_OTHER || strcmp(t->text, ",")) && tt) {
1788 if (tt->type == TOK_OTHER && !strcmp(tt->text, ",")) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03001789 nasm_nonfatal("`%s': more than one comma on line",
1790 pp_directives[ct]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001791 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001792 }
1793 if (t->type == TOK_WHITESPACE) {
1794 t = t->next;
1795 continue;
1796 }
1797 if (tt->type == TOK_WHITESPACE) {
1798 tt = tt->next;
1799 continue;
1800 }
1801 if (tt->type != t->type) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001802 j = false; /* found mismatching tokens */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001803 break;
1804 }
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07001805 /* When comparing strings, need to unquote them first */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001806 if (t->type == TOK_STRING) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001807 size_t l1 = nasm_unquote(t->text, NULL);
1808 size_t l2 = nasm_unquote(tt->text, NULL);
H. Peter Anvind2456592008-06-19 15:04:18 -07001809
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001810 if (l1 != l2) {
1811 j = false;
1812 break;
1813 }
1814 if (mmemcmp(t->text, tt->text, l1, i == PPC_IFIDN)) {
1815 j = false;
1816 break;
1817 }
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07001818 } else if (mstrcmp(tt->text, t->text, i == PPC_IFIDN) != 0) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001819 j = false; /* found mismatching tokens */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001820 break;
1821 }
Nickolay Yurchenkof3b3ce22003-09-21 20:38:43 +00001822
H. Peter Anvine2c80182005-01-15 22:15:51 +00001823 t = t->next;
1824 tt = tt->next;
1825 }
1826 if ((t->type != TOK_OTHER || strcmp(t->text, ",")) || tt)
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001827 j = false; /* trailing gunk on one end or other */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001828 break;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001829
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001830 case PPC_IFMACRO:
H. Peter Anvin89cee572009-07-15 09:16:54 -04001831 {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001832 bool found = false;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001833 MMacro searching, *mmac;
H. Peter Anvin65747262002-05-07 00:10:05 +00001834
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001835 skip_white_(tline);
1836 tline = expand_id(tline);
1837 if (!tok_type_(tline, TOK_ID)) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03001838 nasm_nonfatal("`%s' expects a macro name", pp_directives[ct]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001839 goto fail;
1840 }
1841 searching.name = nasm_strdup(tline->text);
1842 searching.casesense = true;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001843 searching.plus = false;
1844 searching.nolist = false;
1845 searching.in_progress = 0;
1846 searching.max_depth = 0;
1847 searching.rep_nest = NULL;
1848 searching.nparam_min = 0;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001849 searching.nparam_max = INT_MAX;
1850 tline = expand_smacro(tline->next);
1851 skip_white_(tline);
1852 if (!tline) {
1853 } else if (!tok_type_(tline, TOK_NUMBER)) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03001854 nasm_nonfatal("`%s' expects a parameter count or nothing",
1855 pp_directives[ct]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001856 } else {
1857 searching.nparam_min = searching.nparam_max =
Cyrill Gorcunov3079f792018-11-14 10:03:42 +03001858 read_param_count(tline->text);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001859 }
1860 if (tline && tok_is_(tline->next, "-")) {
1861 tline = tline->next->next;
1862 if (tok_is_(tline, "*"))
1863 searching.nparam_max = INT_MAX;
1864 else if (!tok_type_(tline, TOK_NUMBER))
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03001865 nasm_nonfatal("`%s' expects a parameter count after `-'",
1866 pp_directives[ct]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001867 else {
Cyrill Gorcunov3079f792018-11-14 10:03:42 +03001868 searching.nparam_max = read_param_count(tline->text);
Cyrill Gorcunovc9244ea2017-10-22 15:25:48 +03001869 if (searching.nparam_min > searching.nparam_max) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03001870 nasm_nonfatal("minimum parameter count exceeds maximum");
Cyrill Gorcunovc9244ea2017-10-22 15:25:48 +03001871 searching.nparam_max = searching.nparam_min;
1872 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001873 }
1874 }
1875 if (tline && tok_is_(tline->next, "+")) {
1876 tline = tline->next;
1877 searching.plus = true;
1878 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001879 mmac = (MMacro *) hash_findix(&mmacros, searching.name);
1880 while (mmac) {
1881 if (!strcmp(mmac->name, searching.name) &&
1882 (mmac->nparam_min <= searching.nparam_max
1883 || searching.plus)
1884 && (searching.nparam_min <= mmac->nparam_max
1885 || mmac->plus)) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001886 found = true;
1887 break;
1888 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001889 mmac = mmac->next;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001890 }
1891 if (tline && tline->next)
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08001892 nasm_warn(WARN_OTHER, "trailing garbage after %%ifmacro ignored");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001893 nasm_free(searching.name);
1894 j = found;
1895 break;
H. Peter Anvin89cee572009-07-15 09:16:54 -04001896 }
H. Peter Anvin65747262002-05-07 00:10:05 +00001897
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001898 case PPC_IFID:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001899 needtype = TOK_ID;
1900 goto iftype;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001901 case PPC_IFNUM:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001902 needtype = TOK_NUMBER;
1903 goto iftype;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001904 case PPC_IFSTR:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001905 needtype = TOK_STRING;
1906 goto iftype;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001907
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001908iftype:
1909 t = tline = expand_smacro(tline);
H. Peter Anvind85d2502008-05-04 17:53:31 -07001910
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001911 while (tok_type_(t, TOK_WHITESPACE) ||
1912 (needtype == TOK_NUMBER &&
1913 tok_type_(t, TOK_OTHER) &&
1914 (t->text[0] == '-' || t->text[0] == '+') &&
1915 !t->text[1]))
1916 t = t->next;
H. Peter Anvind85d2502008-05-04 17:53:31 -07001917
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001918 j = tok_type_(t, needtype);
1919 break;
H. Peter Anvincbf768d2008-02-16 16:41:25 -08001920
1921 case PPC_IFTOKEN:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001922 t = tline = expand_smacro(tline);
1923 while (tok_type_(t, TOK_WHITESPACE))
1924 t = t->next;
H. Peter Anvincbf768d2008-02-16 16:41:25 -08001925
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001926 j = false;
1927 if (t) {
1928 t = t->next; /* Skip the actual token */
1929 while (tok_type_(t, TOK_WHITESPACE))
1930 t = t->next;
1931 j = !t; /* Should be nothing left */
1932 }
1933 break;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001934
H. Peter Anvin134b9462008-02-16 17:01:40 -08001935 case PPC_IFEMPTY:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001936 t = tline = expand_smacro(tline);
1937 while (tok_type_(t, TOK_WHITESPACE))
1938 t = t->next;
H. Peter Anvin134b9462008-02-16 17:01:40 -08001939
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001940 j = !t; /* Should be empty */
1941 break;
H. Peter Anvin134b9462008-02-16 17:01:40 -08001942
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001943 case PPC_IF:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001944 t = tline = expand_smacro(tline);
1945 tptr = &t;
1946 tokval.t_type = TOKEN_INVALID;
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08001947 evalresult = evaluate(ppscan, tptr, &tokval, NULL, true, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001948 if (!evalresult)
1949 return -1;
1950 if (tokval.t_type)
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08001951 nasm_warn(WARN_OTHER, "trailing garbage after expression ignored");
H. Peter Anvine2c80182005-01-15 22:15:51 +00001952 if (!is_simple(evalresult)) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03001953 nasm_nonfatal("non-constant value given to `%s'",
1954 pp_directives[ct]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001955 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001956 }
Chuck Crayne60ae75d2007-05-02 01:59:16 +00001957 j = reloc_value(evalresult) != 0;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001958 break;
H. Peter Anvin95e28822007-09-12 04:20:08 +00001959
H. Peter Anvine2c80182005-01-15 22:15:51 +00001960 default:
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03001961 nasm_fatal("preprocessor directive `%s' not yet implemented",
1962 pp_directives[ct]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001963 goto fail;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001964 }
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001965
1966 free_tlist(origline);
1967 return j ^ PP_NEGATIVE(ct);
H. Peter Anvin70653092007-10-19 14:42:29 -07001968
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001969fail:
1970 free_tlist(origline);
1971 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001972}
1973
1974/*
H. Peter Anvin4db5a162007-10-11 13:42:09 -07001975 * Common code for defining an smacro
1976 */
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001977static bool define_smacro(Context *ctx, const char *mname, bool casesense,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001978 int nparam, Token *expansion)
H. Peter Anvin4db5a162007-10-11 13:42:09 -07001979{
1980 SMacro *smac, **smhead;
H. Peter Anvin166c2472008-05-28 12:28:58 -07001981 struct hash_table *smtbl;
H. Peter Anvin70653092007-10-19 14:42:29 -07001982
H. Peter Anvin4db5a162007-10-11 13:42:09 -07001983 if (smacro_defined(ctx, mname, nparam, &smac, casesense)) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001984 if (!smac) {
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08001985 nasm_warn(WARN_OTHER, "single-line macro `%s' defined both with and"
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03001986 " without parameters", mname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001987 /*
1988 * Some instances of the old code considered this a failure,
1989 * some others didn't. What is the right thing to do here?
1990 */
1991 free_tlist(expansion);
1992 return false; /* Failure */
1993 } else {
1994 /*
1995 * We're redefining, so we have to take over an
1996 * existing SMacro structure. This means freeing
1997 * what was already in it.
1998 */
1999 nasm_free(smac->name);
2000 free_tlist(smac->expansion);
2001 }
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002002 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002003 smtbl = ctx ? &ctx->localmac : &smacros;
2004 smhead = (SMacro **) hash_findi_add(smtbl, mname);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002005 smac = nasm_malloc(sizeof(SMacro));
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002006 smac->next = *smhead;
2007 *smhead = smac;
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002008 }
2009 smac->name = nasm_strdup(mname);
2010 smac->casesense = casesense;
2011 smac->nparam = nparam;
2012 smac->expansion = expansion;
2013 smac->in_progress = false;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002014 return true; /* Success */
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002015}
2016
2017/*
2018 * Undefine an smacro
2019 */
2020static void undef_smacro(Context *ctx, const char *mname)
2021{
2022 SMacro **smhead, *s, **sp;
H. Peter Anvin166c2472008-05-28 12:28:58 -07002023 struct hash_table *smtbl;
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002024
H. Peter Anvin166c2472008-05-28 12:28:58 -07002025 smtbl = ctx ? &ctx->localmac : &smacros;
2026 smhead = (SMacro **)hash_findi(smtbl, mname, NULL);
H. Peter Anvin70653092007-10-19 14:42:29 -07002027
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002028 if (smhead) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002029 /*
2030 * We now have a macro name... go hunt for it.
2031 */
2032 sp = smhead;
2033 while ((s = *sp) != NULL) {
2034 if (!mstrcmp(s->name, mname, s->casesense)) {
2035 *sp = s->next;
2036 nasm_free(s->name);
2037 free_tlist(s->expansion);
2038 nasm_free(s);
2039 } else {
2040 sp = &s->next;
2041 }
2042 }
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002043 }
2044}
2045
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002046/*
H. Peter Anvina26433d2008-07-16 14:40:01 -07002047 * Parse a mmacro specification.
2048 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002049static bool parse_mmacro_spec(Token *tline, MMacro *def, const char *directive)
H. Peter Anvina26433d2008-07-16 14:40:01 -07002050{
H. Peter Anvina26433d2008-07-16 14:40:01 -07002051 tline = tline->next;
2052 skip_white_(tline);
2053 tline = expand_id(tline);
2054 if (!tok_type_(tline, TOK_ID)) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002055 nasm_nonfatal("`%s' expects a macro name", directive);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002056 return false;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002057 }
Victor van den Elzenb916ede2008-07-23 15:14:22 +02002058
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002059 def->prev = NULL;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002060 def->name = nasm_strdup(tline->text);
2061 def->plus = false;
2062 def->nolist = false;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002063 def->in_progress = 0;
2064 def->rep_nest = NULL;
Victor van den Elzenb916ede2008-07-23 15:14:22 +02002065 def->nparam_min = 0;
2066 def->nparam_max = 0;
2067
H. Peter Anvina26433d2008-07-16 14:40:01 -07002068 tline = expand_smacro(tline->next);
2069 skip_white_(tline);
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002070 if (!tok_type_(tline, TOK_NUMBER))
2071 nasm_nonfatal("`%s' expects a parameter count", directive);
2072 else
Cyrill Gorcunov3079f792018-11-14 10:03:42 +03002073 def->nparam_min = def->nparam_max = read_param_count(tline->text);
H. Peter Anvina26433d2008-07-16 14:40:01 -07002074 if (tline && tok_is_(tline->next, "-")) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002075 tline = tline->next->next;
2076 if (tok_is_(tline, "*")) {
2077 def->nparam_max = INT_MAX;
2078 } else if (!tok_type_(tline, TOK_NUMBER)) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002079 nasm_nonfatal("`%s' expects a parameter count after `-'", directive);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002080 } else {
Cyrill Gorcunov3079f792018-11-14 10:03:42 +03002081 def->nparam_max = read_param_count(tline->text);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002082 if (def->nparam_min > def->nparam_max) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002083 nasm_nonfatal("minimum parameter count exceeds maximum");
Cyrill Gorcunovc9244ea2017-10-22 15:25:48 +03002084 def->nparam_max = def->nparam_min;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002085 }
2086 }
H. Peter Anvina26433d2008-07-16 14:40:01 -07002087 }
2088 if (tline && tok_is_(tline->next, "+")) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002089 tline = tline->next;
2090 def->plus = true;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002091 }
2092 if (tline && tok_type_(tline->next, TOK_ID) &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002093 !nasm_stricmp(tline->next->text, ".nolist")) {
2094 tline = tline->next;
2095 def->nolist = true;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002096 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002097
H. Peter Anvina26433d2008-07-16 14:40:01 -07002098 /*
2099 * Handle default parameters.
2100 */
2101 if (tline && tline->next) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002102 def->dlist = tline->next;
2103 tline->next = NULL;
2104 count_mmac_params(def->dlist, &def->ndefs, &def->defaults);
H. Peter Anvina26433d2008-07-16 14:40:01 -07002105 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002106 def->dlist = NULL;
2107 def->defaults = NULL;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002108 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002109 def->expansion = NULL;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002110
H. Peter Anvin89cee572009-07-15 09:16:54 -04002111 if (def->defaults && def->ndefs > def->nparam_max - def->nparam_min &&
H. Peter Anvin (Intel)723ab482018-12-13 21:53:31 -08002112 !def->plus) {
2113 /*
2114 *!macro-defaults [on] macros with more default than optional parameters
2115 *! warns when a macro has more default parameters than optional parameters.
2116 *! See \k{mlmacdef} for why might want to disable this warning.
2117 */
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08002118 nasm_warn(WARN_MACRO_DEFAULTS,
H. Peter Anvin (Intel)723ab482018-12-13 21:53:31 -08002119 "too many default macro parameters in macro `%s'", def->name);
2120 }
Victor van den Elzenb916ede2008-07-23 15:14:22 +02002121
H. Peter Anvina26433d2008-07-16 14:40:01 -07002122 return true;
2123}
2124
2125
2126/*
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002127 * Decode a size directive
2128 */
2129static int parse_size(const char *str) {
2130 static const char *size_names[] =
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002131 { "byte", "dword", "oword", "qword", "tword", "word", "yword" };
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002132 static const int sizes[] =
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002133 { 0, 1, 4, 16, 8, 10, 2, 32 };
Cyrill Gorcunovc713b5f2018-09-29 14:30:14 +03002134 return str ? sizes[bsii(str, size_names, ARRAY_SIZE(size_names))+1] : 0;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002135}
2136
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07002137/*
2138 * Process a preprocessor %pragma directive. Currently there are none.
2139 * Gets passed the token list starting with the "preproc" token from
2140 * "%pragma preproc".
2141 */
2142static void do_pragma_preproc(Token *tline)
2143{
2144 /* Skip to the real stuff */
2145 tline = tline->next;
2146 skip_white_(tline);
2147 if (!tline)
2148 return;
2149
2150 (void)tline; /* Nothing else to do at present */
2151}
2152
Ed Beroset3ab3f412002-06-11 03:31:49 +00002153/**
2154 * find and process preprocessor directive in passed line
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002155 * Find out if a line contains a preprocessor directive, and deal
2156 * with it if so.
H. Peter Anvin70653092007-10-19 14:42:29 -07002157 *
Ed Beroset3ab3f412002-06-11 03:31:49 +00002158 * If a directive _is_ found, it is the responsibility of this routine
2159 * (and not the caller) to free_tlist() the line.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002160 *
Ed Beroset3ab3f412002-06-11 03:31:49 +00002161 * @param tline a pointer to the current tokeninzed line linked list
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07002162 * @param output if this directive generated output
Ed Beroset3ab3f412002-06-11 03:31:49 +00002163 * @return DIRECTIVE_FOUND or NO_DIRECTIVE_FOUND
H. Peter Anvin70653092007-10-19 14:42:29 -07002164 *
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002165 */
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07002166static int do_directive(Token *tline, char **output)
H. Peter Anvineba20a72002-04-30 20:53:55 +00002167{
H. Peter Anvin4169a472007-09-12 01:29:43 +00002168 enum preproc_token i;
2169 int j;
H. Peter Anvin70055962007-10-11 00:05:31 -07002170 bool err;
2171 int nparam;
2172 bool nolist;
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07002173 bool casesense;
H. Peter Anvin8cfdb9d2007-09-14 18:36:01 -07002174 int k, m;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002175 int offset;
H. Peter Anvinccad6f92016-10-04 00:34:35 -07002176 char *p, *pp;
2177 const char *found_path;
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08002178 const char *mname;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002179 Include *inc;
2180 Context *ctx;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002181 Cond *cond;
2182 MMacro *mmac, **mmhead;
Jin Kyu Songc9486b92013-10-28 17:07:57 -07002183 Token *t = NULL, *tt, *param_start, *macro_start, *last, **tptr, *origline;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002184 Line *l;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002185 struct tokenval tokval;
2186 expr *evalresult;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002187 MMacro *tmp_defining; /* Used when manipulating rep_nest */
H. Peter Anvinf8ba53e2007-10-11 10:11:57 -07002188 int64_t count;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07002189 size_t len;
H. Peter Anvin (Intel)6bde2ed2018-12-13 19:39:41 -08002190 errflags severity;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002191
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07002192 *output = NULL; /* No output generated */
H. Peter Anvin76690a12002-04-30 20:52:49 +00002193 origline = tline;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002194
H. Peter Anvineba20a72002-04-30 20:53:55 +00002195 skip_white_(tline);
H. Peter Anvinf2936d72008-06-04 15:11:23 -07002196 if (!tline || !tok_type_(tline, TOK_PREPROC_ID) ||
Cyrill Gorcunov4b5b7372018-10-29 22:54:08 +03002197 (tline->text[0] && (tline->text[1] == '%' ||
2198 tline->text[1] == '$' ||
2199 tline->text[1] == '!')))
H. Peter Anvine2c80182005-01-15 22:15:51 +00002200 return NO_DIRECTIVE_FOUND;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002201
H. Peter Anvin4169a472007-09-12 01:29:43 +00002202 i = pp_token_hash(tline->text);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002203
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002204 /*
2205 * FIXME: We zap execution of PP_RMACRO, PP_IRMACRO, PP_EXITMACRO
2206 * since they are known to be buggy at moment, we need to fix them
2207 * in future release (2.09-2.10)
2208 */
Philipp Klokeb432f572013-03-31 12:01:23 +02002209 if (i == PP_RMACRO || i == PP_IRMACRO || i == PP_EXITMACRO) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002210 nasm_nonfatal("unknown preprocessor directive `%s'", tline->text);
2211 return NO_DIRECTIVE_FOUND;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002212 }
2213
2214 /*
2215 * If we're in a non-emitting branch of a condition construct,
2216 * or walking to the end of an already terminated %rep block,
2217 * we should ignore all directives except for condition
2218 * directives.
2219 */
2220 if (((istk->conds && !emitting(istk->conds->state)) ||
2221 (istk->mstk && !istk->mstk->in_progress)) && !is_condition(i)) {
2222 return NO_DIRECTIVE_FOUND;
2223 }
2224
2225 /*
2226 * If we're defining a macro or reading a %rep block, we should
2227 * ignore all directives except for %macro/%imacro (which nest),
2228 * %endm/%endmacro, and (only if we're in a %rep block) %endrep.
2229 * If we're in a %rep block, another %rep nests, so should be let through.
2230 */
2231 if (defining && i != PP_MACRO && i != PP_IMACRO &&
2232 i != PP_RMACRO && i != PP_IRMACRO &&
2233 i != PP_ENDMACRO && i != PP_ENDM &&
2234 (defining->name || (i != PP_ENDREP && i != PP_REP))) {
2235 return NO_DIRECTIVE_FOUND;
2236 }
2237
2238 if (defining) {
2239 if (i == PP_MACRO || i == PP_IMACRO ||
2240 i == PP_RMACRO || i == PP_IRMACRO) {
2241 nested_mac_count++;
2242 return NO_DIRECTIVE_FOUND;
2243 } else if (nested_mac_count > 0) {
2244 if (i == PP_ENDMACRO) {
2245 nested_mac_count--;
2246 return NO_DIRECTIVE_FOUND;
2247 }
2248 }
2249 if (!defining->name) {
2250 if (i == PP_REP) {
2251 nested_rep_count++;
2252 return NO_DIRECTIVE_FOUND;
2253 } else if (nested_rep_count > 0) {
2254 if (i == PP_ENDREP) {
2255 nested_rep_count--;
2256 return NO_DIRECTIVE_FOUND;
2257 }
2258 }
2259 }
2260 }
2261
H. Peter Anvin4169a472007-09-12 01:29:43 +00002262 switch (i) {
2263 case PP_INVALID:
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002264 nasm_nonfatal("unknown preprocessor directive `%s'", tline->text);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002265 return NO_DIRECTIVE_FOUND; /* didn't get it */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002266
H. Peter Anvin3f87a2a2016-10-04 14:07:19 -07002267 case PP_PRAGMA:
2268 /*
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07002269 * %pragma namespace options...
2270 *
2271 * The namespace "preproc" is reserved for the preprocessor;
2272 * all other namespaces generate a [pragma] assembly directive.
2273 *
2274 * Invalid %pragmas are ignored and may have different
2275 * meaning in future versions of NASM.
H. Peter Anvin3f87a2a2016-10-04 14:07:19 -07002276 */
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07002277 tline = tline->next;
2278 skip_white_(tline);
2279 tline = expand_smacro(tline);
2280 if (tok_type_(tline, TOK_ID)) {
2281 if (!nasm_stricmp(tline->text, "preproc")) {
2282 /* Preprocessor pragma */
2283 do_pragma_preproc(tline);
2284 } else {
2285 /* Build the assembler directive */
2286 t = new_Token(NULL, TOK_OTHER, "[", 1);
2287 t->next = new_Token(NULL, TOK_ID, "pragma", 6);
2288 t->next->next = new_Token(tline, TOK_WHITESPACE, NULL, 0);
2289 tline = t;
2290 for (t = tline; t->next; t = t->next)
2291 ;
2292 t->next = new_Token(NULL, TOK_OTHER, "]", 1);
2293 /* true here can be revisited in the future */
2294 *output = detoken(tline, true);
2295 }
2296 }
H. Peter Anvin3f87a2a2016-10-04 14:07:19 -07002297 free_tlist(origline);
2298 return DIRECTIVE_FOUND;
2299
H. Peter Anvine2c80182005-01-15 22:15:51 +00002300 case PP_STACKSIZE:
2301 /* Directive to tell NASM what the default stack size is. The
2302 * default is for a 16-bit stack, and this can be overriden with
2303 * %stacksize large.
H. Peter Anvine2c80182005-01-15 22:15:51 +00002304 */
2305 tline = tline->next;
2306 if (tline && tline->type == TOK_WHITESPACE)
2307 tline = tline->next;
2308 if (!tline || tline->type != TOK_ID) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002309 nasm_nonfatal("`%%stacksize' missing size parameter");
H. Peter Anvine2c80182005-01-15 22:15:51 +00002310 free_tlist(origline);
2311 return DIRECTIVE_FOUND;
2312 }
2313 if (nasm_stricmp(tline->text, "flat") == 0) {
2314 /* All subsequent ARG directives are for a 32-bit stack */
2315 StackSize = 4;
2316 StackPointer = "ebp";
2317 ArgOffset = 8;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002318 LocalOffset = 0;
Charles Crayne7eaf9192007-11-08 22:11:14 -08002319 } else if (nasm_stricmp(tline->text, "flat64") == 0) {
2320 /* All subsequent ARG directives are for a 64-bit stack */
2321 StackSize = 8;
2322 StackPointer = "rbp";
Per Jessen53252e02010-02-11 00:16:59 +03002323 ArgOffset = 16;
Charles Crayne7eaf9192007-11-08 22:11:14 -08002324 LocalOffset = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002325 } else if (nasm_stricmp(tline->text, "large") == 0) {
2326 /* All subsequent ARG directives are for a 16-bit stack,
2327 * far function call.
2328 */
2329 StackSize = 2;
2330 StackPointer = "bp";
2331 ArgOffset = 4;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002332 LocalOffset = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002333 } else if (nasm_stricmp(tline->text, "small") == 0) {
2334 /* All subsequent ARG directives are for a 16-bit stack,
2335 * far function call. We don't support near functions.
2336 */
2337 StackSize = 2;
2338 StackPointer = "bp";
2339 ArgOffset = 6;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002340 LocalOffset = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002341 } else {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002342 nasm_nonfatal("`%%stacksize' invalid size type");
H. Peter Anvine2c80182005-01-15 22:15:51 +00002343 free_tlist(origline);
2344 return DIRECTIVE_FOUND;
2345 }
2346 free_tlist(origline);
2347 return DIRECTIVE_FOUND;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002348
H. Peter Anvine2c80182005-01-15 22:15:51 +00002349 case PP_ARG:
2350 /* TASM like ARG directive to define arguments to functions, in
2351 * the following form:
2352 *
2353 * ARG arg1:WORD, arg2:DWORD, arg4:QWORD
2354 */
2355 offset = ArgOffset;
2356 do {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00002357 char *arg, directive[256];
H. Peter Anvine2c80182005-01-15 22:15:51 +00002358 int size = StackSize;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002359
H. Peter Anvine2c80182005-01-15 22:15:51 +00002360 /* Find the argument name */
2361 tline = tline->next;
2362 if (tline && tline->type == TOK_WHITESPACE)
2363 tline = tline->next;
2364 if (!tline || tline->type != TOK_ID) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002365 nasm_nonfatal("`%%arg' missing argument parameter");
H. Peter Anvine2c80182005-01-15 22:15:51 +00002366 free_tlist(origline);
2367 return DIRECTIVE_FOUND;
2368 }
2369 arg = tline->text;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002370
H. Peter Anvine2c80182005-01-15 22:15:51 +00002371 /* Find the argument size type */
2372 tline = tline->next;
2373 if (!tline || tline->type != TOK_OTHER
2374 || tline->text[0] != ':') {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002375 nasm_nonfatal("Syntax error processing `%%arg' directive");
H. Peter Anvine2c80182005-01-15 22:15:51 +00002376 free_tlist(origline);
2377 return DIRECTIVE_FOUND;
2378 }
2379 tline = tline->next;
2380 if (!tline || tline->type != TOK_ID) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002381 nasm_nonfatal("`%%arg' missing size type parameter");
H. Peter Anvine2c80182005-01-15 22:15:51 +00002382 free_tlist(origline);
2383 return DIRECTIVE_FOUND;
2384 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002385
H. Peter Anvine2c80182005-01-15 22:15:51 +00002386 /* Allow macro expansion of type parameter */
Keith Kaniosb7a89542007-04-12 02:40:54 +00002387 tt = tokenize(tline->text);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002388 tt = expand_smacro(tt);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002389 size = parse_size(tt->text);
2390 if (!size) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002391 nasm_nonfatal("Invalid size type for `%%arg' missing directive");
H. Peter Anvine2c80182005-01-15 22:15:51 +00002392 free_tlist(tt);
2393 free_tlist(origline);
2394 return DIRECTIVE_FOUND;
2395 }
2396 free_tlist(tt);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002397
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002398 /* Round up to even stack slots */
2399 size = ALIGN(size, StackSize);
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002400
H. Peter Anvine2c80182005-01-15 22:15:51 +00002401 /* Now define the macro for the argument */
2402 snprintf(directive, sizeof(directive), "%%define %s (%s+%d)",
2403 arg, StackPointer, offset);
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07002404 do_directive(tokenize(directive), output);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002405 offset += size;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002406
H. Peter Anvine2c80182005-01-15 22:15:51 +00002407 /* Move to the next argument in the list */
2408 tline = tline->next;
2409 if (tline && tline->type == TOK_WHITESPACE)
2410 tline = tline->next;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002411 } while (tline && tline->type == TOK_OTHER && tline->text[0] == ',');
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002412 ArgOffset = offset;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002413 free_tlist(origline);
2414 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002415
H. Peter Anvine2c80182005-01-15 22:15:51 +00002416 case PP_LOCAL:
2417 /* TASM like LOCAL directive to define local variables for a
2418 * function, in the following form:
2419 *
2420 * LOCAL local1:WORD, local2:DWORD, local4:QWORD = LocalSize
2421 *
2422 * The '= LocalSize' at the end is ignored by NASM, but is
2423 * required by TASM to define the local parameter size (and used
2424 * by the TASM macro package).
2425 */
2426 offset = LocalOffset;
2427 do {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00002428 char *local, directive[256];
H. Peter Anvine2c80182005-01-15 22:15:51 +00002429 int size = StackSize;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002430
H. Peter Anvine2c80182005-01-15 22:15:51 +00002431 /* Find the argument name */
2432 tline = tline->next;
2433 if (tline && tline->type == TOK_WHITESPACE)
2434 tline = tline->next;
2435 if (!tline || tline->type != TOK_ID) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002436 nasm_nonfatal("`%%local' missing argument parameter");
H. Peter Anvine2c80182005-01-15 22:15:51 +00002437 free_tlist(origline);
2438 return DIRECTIVE_FOUND;
2439 }
2440 local = tline->text;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002441
H. Peter Anvine2c80182005-01-15 22:15:51 +00002442 /* Find the argument size type */
2443 tline = tline->next;
2444 if (!tline || tline->type != TOK_OTHER
2445 || tline->text[0] != ':') {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002446 nasm_nonfatal("Syntax error processing `%%local' directive");
H. Peter Anvine2c80182005-01-15 22:15:51 +00002447 free_tlist(origline);
2448 return DIRECTIVE_FOUND;
2449 }
2450 tline = tline->next;
2451 if (!tline || tline->type != TOK_ID) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002452 nasm_nonfatal("`%%local' missing size type parameter");
H. Peter Anvine2c80182005-01-15 22:15:51 +00002453 free_tlist(origline);
2454 return DIRECTIVE_FOUND;
2455 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00002456
H. Peter Anvine2c80182005-01-15 22:15:51 +00002457 /* Allow macro expansion of type parameter */
Keith Kaniosb7a89542007-04-12 02:40:54 +00002458 tt = tokenize(tline->text);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002459 tt = expand_smacro(tt);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002460 size = parse_size(tt->text);
2461 if (!size) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002462 nasm_nonfatal("Invalid size type for `%%local' missing directive");
H. Peter Anvine2c80182005-01-15 22:15:51 +00002463 free_tlist(tt);
2464 free_tlist(origline);
2465 return DIRECTIVE_FOUND;
2466 }
2467 free_tlist(tt);
H. Peter Anvin734b1882002-04-30 21:01:08 +00002468
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002469 /* Round up to even stack slots */
2470 size = ALIGN(size, StackSize);
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002471
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002472 offset += size; /* Negative offset, increment before */
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002473
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002474 /* Now define the macro for the argument */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002475 snprintf(directive, sizeof(directive), "%%define %s (%s-%d)",
2476 local, StackPointer, offset);
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07002477 do_directive(tokenize(directive), output);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002478
H. Peter Anvine2c80182005-01-15 22:15:51 +00002479 /* Now define the assign to setup the enter_c macro correctly */
2480 snprintf(directive, sizeof(directive),
2481 "%%assign %%$localsize %%$localsize+%d", size);
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07002482 do_directive(tokenize(directive), output);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002483
H. Peter Anvine2c80182005-01-15 22:15:51 +00002484 /* Move to the next argument in the list */
2485 tline = tline->next;
2486 if (tline && tline->type == TOK_WHITESPACE)
2487 tline = tline->next;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002488 } while (tline && tline->type == TOK_OTHER && tline->text[0] == ',');
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002489 LocalOffset = offset;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002490 free_tlist(origline);
2491 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002492
H. Peter Anvine2c80182005-01-15 22:15:51 +00002493 case PP_CLEAR:
2494 if (tline->next)
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08002495 nasm_warn(WARN_OTHER, "trailing garbage after `%%clear' ignored");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002496 free_macros();
2497 init_macros();
H. Peter Anvine2c80182005-01-15 22:15:51 +00002498 free_tlist(origline);
2499 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002500
H. Peter Anvin418ca702008-05-30 10:42:30 -07002501 case PP_DEPEND:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002502 t = tline->next = expand_smacro(tline->next);
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002503 skip_white_(t);
2504 if (!t || (t->type != TOK_STRING &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002505 t->type != TOK_INTERNAL_STRING)) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002506 nasm_nonfatal("`%%depend' expects a file name");
H. Peter Anvin418ca702008-05-30 10:42:30 -07002507 free_tlist(origline);
2508 return DIRECTIVE_FOUND; /* but we did _something_ */
2509 }
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002510 if (t->next)
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08002511 nasm_warn(WARN_OTHER, "trailing garbage after `%%depend' ignored");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002512 p = t->text;
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002513 if (t->type != TOK_INTERNAL_STRING)
H. Peter Anvinbb42d302019-04-22 14:29:29 -07002514 nasm_unquote_cstr(p, NULL);
Cyrill Gorcunovb7bb5ac2018-11-11 21:33:52 +03002515 strlist_add(deplist, p);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002516 free_tlist(origline);
H. Peter Anvin418ca702008-05-30 10:42:30 -07002517 return DIRECTIVE_FOUND;
2518
2519 case PP_INCLUDE:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002520 t = tline->next = expand_smacro(tline->next);
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002521 skip_white_(t);
H. Peter Anvind2456592008-06-19 15:04:18 -07002522
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002523 if (!t || (t->type != TOK_STRING &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002524 t->type != TOK_INTERNAL_STRING)) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002525 nasm_nonfatal("`%%include' expects a file name");
H. Peter Anvine2c80182005-01-15 22:15:51 +00002526 free_tlist(origline);
2527 return DIRECTIVE_FOUND; /* but we did _something_ */
2528 }
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002529 if (t->next)
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08002530 nasm_warn(WARN_OTHER, "trailing garbage after `%%include' ignored");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002531 p = t->text;
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002532 if (t->type != TOK_INTERNAL_STRING)
H. Peter Anvinbb42d302019-04-22 14:29:29 -07002533 nasm_unquote_cstr(p, NULL);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002534 inc = nasm_malloc(sizeof(Include));
H. Peter Anvine2c80182005-01-15 22:15:51 +00002535 inc->next = istk;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002536 inc->conds = NULL;
Jim Kukunas65a8afc2016-06-13 16:00:42 -04002537 found_path = NULL;
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07002538 inc->fp = inc_fopen(p, deplist, &found_path,
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08002539 (pp_mode == PP_DEPS)
2540 ? INC_OPTIONAL : INC_NEEDED, NF_TEXT);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002541 if (!inc->fp) {
2542 /* -MG given but file not found */
2543 nasm_free(inc);
2544 } else {
Jim Kukunas65a8afc2016-06-13 16:00:42 -04002545 inc->fname = src_set_fname(found_path ? found_path : p);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002546 inc->lineno = src_set_linnum(0);
2547 inc->lineinc = 1;
2548 inc->expansion = NULL;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002549 inc->mstk = NULL;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002550 istk = inc;
H. Peter Anvin8ac25aa2016-02-18 01:16:18 -08002551 lfmt->uplevel(LIST_INCLUDE);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002552 }
2553 free_tlist(origline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002554 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002555
H. Peter Anvind2456592008-06-19 15:04:18 -07002556 case PP_USE:
H. Peter Anvinf4ae5ad2008-06-19 18:39:24 -07002557 {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002558 static macros_t *use_pkg;
2559 const char *pkg_macro = NULL;
H. Peter Anvinf4ae5ad2008-06-19 18:39:24 -07002560
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002561 tline = tline->next;
2562 skip_white_(tline);
2563 tline = expand_id(tline);
H. Peter Anvind2456592008-06-19 15:04:18 -07002564
H. Peter Anvin264b7b92008-10-24 16:38:17 -07002565 if (!tline || (tline->type != TOK_STRING &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002566 tline->type != TOK_INTERNAL_STRING &&
2567 tline->type != TOK_ID)) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002568 nasm_nonfatal("`%%use' expects a package name");
H. Peter Anvind2456592008-06-19 15:04:18 -07002569 free_tlist(origline);
2570 return DIRECTIVE_FOUND; /* but we did _something_ */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002571 }
H. Peter Anvin264b7b92008-10-24 16:38:17 -07002572 if (tline->next)
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08002573 nasm_warn(WARN_OTHER, "trailing garbage after `%%use' ignored");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002574 if (tline->type == TOK_STRING)
H. Peter Anvinbb42d302019-04-22 14:29:29 -07002575 nasm_unquote_cstr(tline->text, NULL);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002576 use_pkg = nasm_stdmac_find_package(tline->text);
2577 if (!use_pkg)
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002578 nasm_nonfatal("unknown `%%use' package: %s", tline->text);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002579 else
2580 pkg_macro = (char *)use_pkg + 1; /* The first string will be <%define>__USE_*__ */
Victor van den Elzen35eb2ea2010-03-10 22:33:48 +01002581 if (use_pkg && ! smacro_defined(NULL, pkg_macro, 0, NULL, true)) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002582 /* Not already included, go ahead and include it */
2583 stdmacpos = use_pkg;
2584 }
2585 free_tlist(origline);
H. Peter Anvind2456592008-06-19 15:04:18 -07002586 return DIRECTIVE_FOUND;
H. Peter Anvinf4ae5ad2008-06-19 18:39:24 -07002587 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002588 case PP_PUSH:
H. Peter Anvine2c80182005-01-15 22:15:51 +00002589 case PP_REPL:
H. Peter Anvin42b56392008-10-24 16:24:21 -07002590 case PP_POP:
H. Peter Anvine2c80182005-01-15 22:15:51 +00002591 tline = tline->next;
2592 skip_white_(tline);
2593 tline = expand_id(tline);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002594 if (tline) {
2595 if (!tok_type_(tline, TOK_ID)) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002596 nasm_nonfatal("`%s' expects a context identifier",
2597 pp_directives[i]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002598 free_tlist(origline);
2599 return DIRECTIVE_FOUND; /* but we did _something_ */
2600 }
2601 if (tline->next)
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08002602 nasm_warn(WARN_OTHER, "trailing garbage after `%s' ignored",
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002603 pp_directives[i]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002604 p = nasm_strdup(tline->text);
2605 } else {
2606 p = NULL; /* Anonymous */
2607 }
H. Peter Anvin42b56392008-10-24 16:24:21 -07002608
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002609 if (i == PP_PUSH) {
H. Peter Anvin (Intel)ebb05a02018-12-11 12:30:25 -08002610 nasm_new(ctx);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002611 ctx->next = cstk;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002612 ctx->name = p;
2613 ctx->number = unique++;
2614 cstk = ctx;
2615 } else {
2616 /* %pop or %repl */
2617 if (!cstk) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002618 nasm_nonfatal("`%s': context stack is empty",
2619 pp_directives[i]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002620 } else if (i == PP_POP) {
2621 if (p && (!cstk->name || nasm_stricmp(p, cstk->name)))
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002622 nasm_nonfatal("`%%pop' in wrong context: %s, expected %s",
2623 cstk->name ? cstk->name : "anonymous", p);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002624 else
2625 ctx_pop();
2626 } else {
2627 /* i == PP_REPL */
2628 nasm_free(cstk->name);
2629 cstk->name = p;
2630 p = NULL;
2631 }
2632 nasm_free(p);
2633 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002634 free_tlist(origline);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002635 return DIRECTIVE_FOUND;
H. Peter Anvin8e3f75e2008-09-24 00:21:58 -07002636 case PP_FATAL:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002637 severity = ERR_FATAL;
2638 goto issue_error;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002639 case PP_ERROR:
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08002640 severity = ERR_NONFATAL|ERR_PASS2;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002641 goto issue_error;
H. Peter Anvin7df04172008-06-10 18:27:38 -07002642 case PP_WARNING:
H. Peter Anvin (Intel)723ab482018-12-13 21:53:31 -08002643 /*!
2644 *!user [on] %warning directives
2645 *! controls output of \c{%warning} directives (see \k{pperror}).
2646 */
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08002647 severity = ERR_WARNING|WARN_USER|ERR_PASS2;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002648 goto issue_error;
H. Peter Anvin8e3f75e2008-09-24 00:21:58 -07002649
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002650issue_error:
H. Peter Anvin7df04172008-06-10 18:27:38 -07002651 {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002652 /* Only error out if this is the final pass */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002653 tline->next = expand_smacro(tline->next);
2654 tline = tline->next;
2655 skip_white_(tline);
2656 t = tline ? tline->next : NULL;
2657 skip_white_(t);
2658 if (tok_type_(tline, TOK_STRING) && !t) {
2659 /* The line contains only a quoted string */
2660 p = tline->text;
2661 nasm_unquote(p, NULL); /* Ignore NUL character truncation */
H. Peter Anvin130736c2016-02-17 20:27:41 -08002662 nasm_error(severity, "%s", p);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002663 } else {
2664 /* Not a quoted string, or more than a quoted string */
2665 p = detoken(tline, false);
H. Peter Anvin130736c2016-02-17 20:27:41 -08002666 nasm_error(severity, "%s", p);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002667 nasm_free(p);
2668 }
2669 free_tlist(origline);
2670 return DIRECTIVE_FOUND;
H. Peter Anvin7df04172008-06-10 18:27:38 -07002671 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00002672
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002673 CASE_PP_IF:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002674 if (istk->conds && !emitting(istk->conds->state))
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002675 j = COND_NEVER;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002676 else {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002677 j = if_condition(tline->next, i);
2678 tline->next = NULL; /* it got freed */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002679 j = j < 0 ? COND_NEVER : j ? COND_IF_TRUE : COND_IF_FALSE;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002680 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002681 cond = nasm_malloc(sizeof(Cond));
2682 cond->next = istk->conds;
2683 cond->state = j;
2684 istk->conds = cond;
2685 if(istk->mstk)
2686 istk->mstk->condcnt ++;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002687 free_tlist(origline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002688 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002689
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002690 CASE_PP_ELIF:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002691 if (!istk->conds)
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002692 nasm_fatal("`%s': no matching `%%if'", pp_directives[i]);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002693 switch(istk->conds->state) {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002694 case COND_IF_TRUE:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002695 istk->conds->state = COND_DONE;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002696 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002697
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002698 case COND_DONE:
2699 case COND_NEVER:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002700 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002701
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002702 case COND_ELSE_TRUE:
2703 case COND_ELSE_FALSE:
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08002704 nasm_warn(WARN_OTHER|ERR_PP_PRECOND,
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002705 "`%%elif' after `%%else' ignored");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002706 istk->conds->state = COND_NEVER;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002707 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002708
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002709 case COND_IF_FALSE:
2710 /*
2711 * IMPORTANT: In the case of %if, we will already have
2712 * called expand_mmac_params(); however, if we're
2713 * processing an %elif we must have been in a
2714 * non-emitting mode, which would have inhibited
2715 * the normal invocation of expand_mmac_params().
2716 * Therefore, we have to do it explicitly here.
2717 */
2718 j = if_condition(expand_mmac_params(tline->next), i);
2719 tline->next = NULL; /* it got freed */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002720 istk->conds->state =
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002721 j < 0 ? COND_NEVER : j ? COND_IF_TRUE : COND_IF_FALSE;
2722 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002723 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002724 free_tlist(origline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002725 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002726
H. Peter Anvine2c80182005-01-15 22:15:51 +00002727 case PP_ELSE:
2728 if (tline->next)
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08002729 nasm_warn(WARN_OTHER|ERR_PP_PRECOND,
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002730 "trailing garbage after `%%else' ignored");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002731 if (!istk->conds)
H. Peter Anvinc5136902018-06-15 18:20:17 -07002732 nasm_fatal("`%%else: no matching `%%if'");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002733 switch(istk->conds->state) {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002734 case COND_IF_TRUE:
2735 case COND_DONE:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002736 istk->conds->state = COND_ELSE_FALSE;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002737 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002738
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002739 case COND_NEVER:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002740 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002741
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002742 case COND_IF_FALSE:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002743 istk->conds->state = COND_ELSE_TRUE;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002744 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002745
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002746 case COND_ELSE_TRUE:
2747 case COND_ELSE_FALSE:
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08002748 nasm_warn(WARN_OTHER|ERR_PP_PRECOND,
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002749 "`%%else' after `%%else' ignored.");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002750 istk->conds->state = COND_NEVER;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002751 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002752 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002753 free_tlist(origline);
2754 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002755
H. Peter Anvine2c80182005-01-15 22:15:51 +00002756 case PP_ENDIF:
2757 if (tline->next)
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08002758 nasm_warn(WARN_OTHER|ERR_PP_PRECOND,
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002759 "trailing garbage after `%%endif' ignored");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002760 if (!istk->conds)
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002761 nasm_fatal("`%%endif': no matching `%%if'");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002762 cond = istk->conds;
2763 istk->conds = cond->next;
2764 nasm_free(cond);
2765 if(istk->mstk)
2766 istk->mstk->condcnt --;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002767 free_tlist(origline);
2768 return DIRECTIVE_FOUND;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002769
H. Peter Anvindb8f96e2009-07-15 09:07:29 -04002770 case PP_RMACRO:
2771 case PP_IRMACRO:
H. Peter Anvine2c80182005-01-15 22:15:51 +00002772 case PP_MACRO:
2773 case PP_IMACRO:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002774 if (defining) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002775 nasm_fatal("`%s': already defining a macro",
2776 pp_directives[i]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002777 return DIRECTIVE_FOUND;
2778 }
H. Peter Anvin4def1a82016-05-09 13:59:44 -07002779 defining = nasm_zalloc(sizeof(MMacro));
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07002780 defining->max_depth = ((i == PP_RMACRO) || (i == PP_IRMACRO))
2781 ? nasm_limit[LIMIT_MACROS] : 0;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002782 defining->casesense = (i == PP_MACRO) || (i == PP_RMACRO);
2783 if (!parse_mmacro_spec(tline, defining, pp_directives[i])) {
2784 nasm_free(defining);
2785 defining = NULL;
2786 return DIRECTIVE_FOUND;
2787 }
H. Peter Anvina26433d2008-07-16 14:40:01 -07002788
H. Peter Anvin4def1a82016-05-09 13:59:44 -07002789 src_get(&defining->xline, &defining->fname);
2790
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002791 mmac = (MMacro *) hash_findix(&mmacros, defining->name);
2792 while (mmac) {
2793 if (!strcmp(mmac->name, defining->name) &&
2794 (mmac->nparam_min <= defining->nparam_max
2795 || defining->plus)
2796 && (defining->nparam_min <= mmac->nparam_max
2797 || mmac->plus)) {
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08002798 nasm_warn(WARN_OTHER, "redefining multi-line macro `%s'",
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002799 defining->name);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002800 return DIRECTIVE_FOUND;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002801 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002802 mmac = mmac->next;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002803 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002804 free_tlist(origline);
2805 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002806
H. Peter Anvine2c80182005-01-15 22:15:51 +00002807 case PP_ENDM:
2808 case PP_ENDMACRO:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002809 if (! (defining && defining->name)) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002810 nasm_nonfatal("`%s': not defining a macro", tline->text);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002811 return DIRECTIVE_FOUND;
2812 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002813 mmhead = (MMacro **) hash_findi_add(&mmacros, defining->name);
2814 defining->next = *mmhead;
2815 *mmhead = defining;
2816 defining = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002817 free_tlist(origline);
2818 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002819
H. Peter Anvin89cee572009-07-15 09:16:54 -04002820 case PP_EXITMACRO:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002821 /*
2822 * We must search along istk->expansion until we hit a
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002823 * macro-end marker for a macro with a name. Then we
2824 * bypass all lines between exitmacro and endmacro.
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002825 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002826 list_for_each(l, istk->expansion)
2827 if (l->finishes && l->finishes->name)
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002828 break;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002829
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002830 if (l) {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002831 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002832 * Remove all conditional entries relative to this
2833 * macro invocation. (safe to do in this context)
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002834 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002835 for ( ; l->finishes->condcnt > 0; l->finishes->condcnt --) {
2836 cond = istk->conds;
2837 istk->conds = cond->next;
2838 nasm_free(cond);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002839 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002840 istk->expansion = l;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002841 } else {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002842 nasm_nonfatal("`%%exitmacro' not within `%%macro' block");
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002843 }
Keith Kanios852f1ee2009-07-12 00:19:55 -05002844 free_tlist(origline);
2845 return DIRECTIVE_FOUND;
2846
H. Peter Anvina26433d2008-07-16 14:40:01 -07002847 case PP_UNMACRO:
2848 case PP_UNIMACRO:
2849 {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002850 MMacro **mmac_p;
2851 MMacro spec;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002852
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002853 spec.casesense = (i == PP_UNMACRO);
2854 if (!parse_mmacro_spec(tline, &spec, pp_directives[i])) {
2855 return DIRECTIVE_FOUND;
2856 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002857 mmac_p = (MMacro **) hash_findi(&mmacros, spec.name, NULL);
2858 while (mmac_p && *mmac_p) {
2859 mmac = *mmac_p;
2860 if (mmac->casesense == spec.casesense &&
2861 !mstrcmp(mmac->name, spec.name, spec.casesense) &&
2862 mmac->nparam_min == spec.nparam_min &&
2863 mmac->nparam_max == spec.nparam_max &&
2864 mmac->plus == spec.plus) {
2865 *mmac_p = mmac->next;
2866 free_mmacro(mmac);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002867 } else {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002868 mmac_p = &mmac->next;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002869 }
2870 }
2871 free_tlist(origline);
2872 free_tlist(spec.dlist);
2873 return DIRECTIVE_FOUND;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002874 }
2875
H. Peter Anvine2c80182005-01-15 22:15:51 +00002876 case PP_ROTATE:
2877 if (tline->next && tline->next->type == TOK_WHITESPACE)
2878 tline = tline->next;
H. Peter Anvin89cee572009-07-15 09:16:54 -04002879 if (!tline->next) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002880 free_tlist(origline);
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002881 nasm_nonfatal("`%%rotate' missing rotate count");
H. Peter Anvine2c80182005-01-15 22:15:51 +00002882 return DIRECTIVE_FOUND;
2883 }
2884 t = expand_smacro(tline->next);
2885 tline->next = NULL;
2886 free_tlist(origline);
2887 tline = t;
2888 tptr = &t;
2889 tokval.t_type = TOKEN_INVALID;
2890 evalresult =
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08002891 evaluate(ppscan, tptr, &tokval, NULL, true, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002892 free_tlist(tline);
2893 if (!evalresult)
2894 return DIRECTIVE_FOUND;
2895 if (tokval.t_type)
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08002896 nasm_warn(WARN_OTHER, "trailing garbage after expression ignored");
H. Peter Anvine2c80182005-01-15 22:15:51 +00002897 if (!is_simple(evalresult)) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002898 nasm_nonfatal("non-constant value given to `%%rotate'");
H. Peter Anvine2c80182005-01-15 22:15:51 +00002899 return DIRECTIVE_FOUND;
2900 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002901 mmac = istk->mstk;
2902 while (mmac && !mmac->name) /* avoid mistaking %reps for macros */
2903 mmac = mmac->next_active;
2904 if (!mmac) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002905 nasm_nonfatal("`%%rotate' invoked outside a macro call");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002906 } else if (mmac->nparam == 0) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002907 nasm_nonfatal("`%%rotate' invoked within macro without parameters");
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002908 } else {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002909 int rotate = mmac->rotate + reloc_value(evalresult);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002910
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002911 rotate %= (int)mmac->nparam;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002912 if (rotate < 0)
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002913 rotate += mmac->nparam;
2914
2915 mmac->rotate = rotate;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002916 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002917 return DIRECTIVE_FOUND;
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00002918
H. Peter Anvine2c80182005-01-15 22:15:51 +00002919 case PP_REP:
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002920 nolist = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002921 do {
2922 tline = tline->next;
2923 } while (tok_type_(tline, TOK_WHITESPACE));
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00002924
H. Peter Anvine2c80182005-01-15 22:15:51 +00002925 if (tok_type_(tline, TOK_ID) &&
2926 nasm_stricmp(tline->text, ".nolist") == 0) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002927 nolist = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002928 do {
2929 tline = tline->next;
2930 } while (tok_type_(tline, TOK_WHITESPACE));
2931 }
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00002932
H. Peter Anvine2c80182005-01-15 22:15:51 +00002933 if (tline) {
2934 t = expand_smacro(tline);
2935 tptr = &t;
2936 tokval.t_type = TOKEN_INVALID;
2937 evalresult =
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08002938 evaluate(ppscan, tptr, &tokval, NULL, true, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002939 if (!evalresult) {
2940 free_tlist(origline);
2941 return DIRECTIVE_FOUND;
2942 }
2943 if (tokval.t_type)
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08002944 nasm_warn(WARN_OTHER, "trailing garbage after expression ignored");
H. Peter Anvine2c80182005-01-15 22:15:51 +00002945 if (!is_simple(evalresult)) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002946 nasm_nonfatal("non-constant value given to `%%rep'");
H. Peter Anvine2c80182005-01-15 22:15:51 +00002947 return DIRECTIVE_FOUND;
2948 }
Cyrill Gorcunove091d6e2010-08-09 13:58:22 +04002949 count = reloc_value(evalresult);
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07002950 if (count > nasm_limit[LIMIT_REP]) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002951 nasm_nonfatal("`%%rep' count %"PRId64" exceeds limit (currently %"PRId64")",
2952 count, nasm_limit[LIMIT_REP]);
Cyrill Gorcunove091d6e2010-08-09 13:58:22 +04002953 count = 0;
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07002954 } else if (count < 0) {
H. Peter Anvin (Intel)723ab482018-12-13 21:53:31 -08002955 /*!
2956 *!negative-rep [on] regative %rep count
2957 *! warns about negative counts given to the \c{%rep}
2958 *! preprocessor directive.
2959 */
H. Peter Anvin (Intel)80c4f232018-12-14 13:33:24 -08002960 nasm_warn(ERR_PASS2|WARN_NEGATIVE_REP,
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07002961 "negative `%%rep' count: %"PRId64, count);
2962 count = 0;
2963 } else {
Cyrill Gorcunove091d6e2010-08-09 13:58:22 +04002964 count++;
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07002965 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002966 } else {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002967 nasm_nonfatal("`%%rep' expects a repeat count");
H. Peter Anvinf8ba53e2007-10-11 10:11:57 -07002968 count = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002969 }
2970 free_tlist(origline);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002971
2972 tmp_defining = defining;
2973 defining = nasm_malloc(sizeof(MMacro));
2974 defining->prev = NULL;
2975 defining->name = NULL; /* flags this macro as a %rep block */
2976 defining->casesense = false;
2977 defining->plus = false;
2978 defining->nolist = nolist;
2979 defining->in_progress = count;
2980 defining->max_depth = 0;
2981 defining->nparam_min = defining->nparam_max = 0;
2982 defining->defaults = NULL;
2983 defining->dlist = NULL;
2984 defining->expansion = NULL;
2985 defining->next_active = istk->mstk;
2986 defining->rep_nest = tmp_defining;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002987 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002988
H. Peter Anvine2c80182005-01-15 22:15:51 +00002989 case PP_ENDREP:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002990 if (!defining || defining->name) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002991 nasm_nonfatal("`%%endrep': no matching `%%rep'");
H. Peter Anvine2c80182005-01-15 22:15:51 +00002992 return DIRECTIVE_FOUND;
2993 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00002994
H. Peter Anvine2c80182005-01-15 22:15:51 +00002995 /*
2996 * Now we have a "macro" defined - although it has no name
2997 * and we won't be entering it in the hash tables - we must
2998 * push a macro-end marker for it on to istk->expansion.
2999 * After that, it will take care of propagating itself (a
3000 * macro-end marker line for a macro which is really a %rep
3001 * block will cause the macro to be re-expanded, complete
3002 * with another macro-end marker to ensure the process
3003 * continues) until the whole expansion is forcibly removed
3004 * from istk->expansion by a %exitrep.
3005 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003006 l = nasm_malloc(sizeof(Line));
3007 l->next = istk->expansion;
3008 l->finishes = defining;
3009 l->first = NULL;
3010 istk->expansion = l;
3011
3012 istk->mstk = defining;
3013
H. Peter Anvin8ac25aa2016-02-18 01:16:18 -08003014 lfmt->uplevel(defining->nolist ? LIST_MACRO_NOLIST : LIST_MACRO);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003015 tmp_defining = defining;
3016 defining = defining->rep_nest;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003017 free_tlist(origline);
3018 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003019
H. Peter Anvine2c80182005-01-15 22:15:51 +00003020 case PP_EXITREP:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003021 /*
3022 * We must search along istk->expansion until we hit a
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003023 * macro-end marker for a macro with no name. Then we set
3024 * its `in_progress' flag to 0.
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003025 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003026 list_for_each(l, istk->expansion)
3027 if (l->finishes && !l->finishes->name)
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003028 break;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003029
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003030 if (l)
3031 l->finishes->in_progress = 1;
3032 else
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003033 nasm_nonfatal("`%%exitrep' not within `%%rep' block");
H. Peter Anvine2c80182005-01-15 22:15:51 +00003034 free_tlist(origline);
3035 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003036
H. Peter Anvine2c80182005-01-15 22:15:51 +00003037 case PP_XDEFINE:
3038 case PP_IXDEFINE:
3039 case PP_DEFINE:
3040 case PP_IDEFINE:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003041 casesense = (i == PP_DEFINE || i == PP_XDEFINE);
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07003042
H. Peter Anvine2c80182005-01-15 22:15:51 +00003043 tline = tline->next;
3044 skip_white_(tline);
3045 tline = expand_id(tline);
3046 if (!tline || (tline->type != TOK_ID &&
3047 (tline->type != TOK_PREPROC_ID ||
3048 tline->text[1] != '$'))) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003049 nasm_nonfatal("`%s' expects a macro identifier",
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003050 pp_directives[i]);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003051 free_tlist(origline);
3052 return DIRECTIVE_FOUND;
3053 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003054
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04003055 ctx = get_ctx(tline->text, &mname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003056 last = tline;
3057 param_start = tline = tline->next;
3058 nparam = 0;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003059
H. Peter Anvine2c80182005-01-15 22:15:51 +00003060 /* Expand the macro definition now for %xdefine and %ixdefine */
3061 if ((i == PP_XDEFINE) || (i == PP_IXDEFINE))
3062 tline = expand_smacro(tline);
H. Peter Anvin734b1882002-04-30 21:01:08 +00003063
H. Peter Anvine2c80182005-01-15 22:15:51 +00003064 if (tok_is_(tline, "(")) {
3065 /*
3066 * This macro has parameters.
3067 */
H. Peter Anvin734b1882002-04-30 21:01:08 +00003068
H. Peter Anvine2c80182005-01-15 22:15:51 +00003069 tline = tline->next;
3070 while (1) {
3071 skip_white_(tline);
3072 if (!tline) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003073 nasm_nonfatal("parameter identifier expected");
H. Peter Anvine2c80182005-01-15 22:15:51 +00003074 free_tlist(origline);
3075 return DIRECTIVE_FOUND;
3076 }
3077 if (tline->type != TOK_ID) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003078 nasm_nonfatal("`%s': parameter identifier expected",
3079 tline->text);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003080 free_tlist(origline);
3081 return DIRECTIVE_FOUND;
3082 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003083 tline->type = TOK_SMAC_PARAM + nparam++;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003084 tline = tline->next;
3085 skip_white_(tline);
3086 if (tok_is_(tline, ",")) {
3087 tline = tline->next;
H. Peter Anvinca348b62008-07-23 10:49:26 -04003088 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003089 if (!tok_is_(tline, ")")) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003090 nasm_nonfatal("`)' expected to terminate macro template");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003091 free_tlist(origline);
3092 return DIRECTIVE_FOUND;
3093 }
3094 break;
3095 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00003096 }
3097 last = tline;
3098 tline = tline->next;
3099 }
3100 if (tok_type_(tline, TOK_WHITESPACE))
3101 last = tline, tline = tline->next;
3102 macro_start = NULL;
3103 last->next = NULL;
3104 t = tline;
3105 while (t) {
3106 if (t->type == TOK_ID) {
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04003107 list_for_each(tt, param_start)
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003108 if (tt->type >= TOK_SMAC_PARAM &&
H. Peter Anvine2c80182005-01-15 22:15:51 +00003109 !strcmp(tt->text, t->text))
3110 t->type = tt->type;
3111 }
3112 tt = t->next;
3113 t->next = macro_start;
3114 macro_start = t;
3115 t = tt;
3116 }
3117 /*
3118 * Good. We now have a macro name, a parameter count, and a
3119 * token list (in reverse order) for an expansion. We ought
3120 * to be OK just to create an SMacro, store it, and let
3121 * free_tlist have the rest of the line (which we have
3122 * carefully re-terminated after chopping off the expansion
3123 * from the end).
3124 */
H. Peter Anvin4db5a162007-10-11 13:42:09 -07003125 define_smacro(ctx, mname, casesense, nparam, macro_start);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003126 free_tlist(origline);
3127 return DIRECTIVE_FOUND;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003128
H. Peter Anvine2c80182005-01-15 22:15:51 +00003129 case PP_UNDEF:
3130 tline = tline->next;
3131 skip_white_(tline);
3132 tline = expand_id(tline);
3133 if (!tline || (tline->type != TOK_ID &&
3134 (tline->type != TOK_PREPROC_ID ||
3135 tline->text[1] != '$'))) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003136 nasm_nonfatal("`%%undef' expects a macro identifier");
H. Peter Anvine2c80182005-01-15 22:15:51 +00003137 free_tlist(origline);
3138 return DIRECTIVE_FOUND;
3139 }
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003140 if (tline->next)
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08003141 nasm_warn(WARN_OTHER, "trailing garbage after macro name ignored");
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003142
H. Peter Anvine2c80182005-01-15 22:15:51 +00003143 /* Find the context that symbol belongs to */
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04003144 ctx = get_ctx(tline->text, &mname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003145 undef_smacro(ctx, mname);
3146 free_tlist(origline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003147 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003148
H. Peter Anvin9e200162008-06-04 17:23:14 -07003149 case PP_DEFSTR:
3150 case PP_IDEFSTR:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003151 casesense = (i == PP_DEFSTR);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003152
3153 tline = tline->next;
3154 skip_white_(tline);
3155 tline = expand_id(tline);
3156 if (!tline || (tline->type != TOK_ID &&
3157 (tline->type != TOK_PREPROC_ID ||
3158 tline->text[1] != '$'))) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003159 nasm_nonfatal("`%s' expects a macro identifier",
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003160 pp_directives[i]);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003161 free_tlist(origline);
3162 return DIRECTIVE_FOUND;
3163 }
3164
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04003165 ctx = get_ctx(tline->text, &mname);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003166 last = tline;
3167 tline = expand_smacro(tline->next);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003168 last->next = NULL;
H. Peter Anvin9e200162008-06-04 17:23:14 -07003169
3170 while (tok_type_(tline, TOK_WHITESPACE))
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003171 tline = delete_Token(tline);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003172
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003173 p = detoken(tline, false);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003174 macro_start = nasm_malloc(sizeof(*macro_start));
3175 macro_start->next = NULL;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003176 macro_start->text = nasm_quote(p, strlen(p));
3177 macro_start->type = TOK_STRING;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003178 macro_start->a.mac = NULL;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003179 nasm_free(p);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003180
3181 /*
3182 * We now have a macro name, an implicit parameter count of
3183 * zero, and a string token to use as an expansion. Create
3184 * and store an SMacro.
3185 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003186 define_smacro(ctx, mname, casesense, 0, macro_start);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003187 free_tlist(origline);
3188 return DIRECTIVE_FOUND;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003189
H. Peter Anvin2f55bda2009-07-14 15:04:04 -04003190 case PP_DEFTOK:
3191 case PP_IDEFTOK:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003192 casesense = (i == PP_DEFTOK);
3193
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003194 tline = tline->next;
3195 skip_white_(tline);
3196 tline = expand_id(tline);
3197 if (!tline || (tline->type != TOK_ID &&
3198 (tline->type != TOK_PREPROC_ID ||
3199 tline->text[1] != '$'))) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003200 nasm_nonfatal("`%s' expects a macro identifier as first parameter",
3201 pp_directives[i]);
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003202 free_tlist(origline);
3203 return DIRECTIVE_FOUND;
3204 }
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04003205 ctx = get_ctx(tline->text, &mname);
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003206 last = tline;
3207 tline = expand_smacro(tline->next);
3208 last->next = NULL;
3209
3210 t = tline;
3211 while (tok_type_(t, TOK_WHITESPACE))
3212 t = t->next;
3213 /* t should now point to the string */
Cyrill Gorcunov6908e582010-09-06 19:36:15 +04003214 if (!tok_type_(t, TOK_STRING)) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003215 nasm_nonfatal("`%s` requires string as second parameter",
3216 pp_directives[i]);
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003217 free_tlist(tline);
3218 free_tlist(origline);
3219 return DIRECTIVE_FOUND;
3220 }
3221
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04003222 /*
3223 * Convert the string to a token stream. Note that smacros
3224 * are stored with the token stream reversed, so we have to
3225 * reverse the output of tokenize().
3226 */
H. Peter Anvinbb42d302019-04-22 14:29:29 -07003227 nasm_unquote_cstr(t->text, NULL);
H. Peter Anvinb40992c2010-09-15 08:57:21 -07003228 macro_start = reverse_tokens(tokenize(t->text));
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003229
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003230 /*
3231 * We now have a macro name, an implicit parameter count of
3232 * zero, and a numeric token to use as an expansion. Create
3233 * and store an SMacro.
3234 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003235 define_smacro(ctx, mname, casesense, 0, macro_start);
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003236 free_tlist(tline);
3237 free_tlist(origline);
3238 return DIRECTIVE_FOUND;
H. Peter Anvin9e200162008-06-04 17:23:14 -07003239
H. Peter Anvin418ca702008-05-30 10:42:30 -07003240 case PP_PATHSEARCH:
3241 {
H. Peter Anvinccad6f92016-10-04 00:34:35 -07003242 const char *found_path;
H. Peter Anvin418ca702008-05-30 10:42:30 -07003243
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003244 casesense = true;
H. Peter Anvin418ca702008-05-30 10:42:30 -07003245
3246 tline = tline->next;
3247 skip_white_(tline);
3248 tline = expand_id(tline);
3249 if (!tline || (tline->type != TOK_ID &&
3250 (tline->type != TOK_PREPROC_ID ||
3251 tline->text[1] != '$'))) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003252 nasm_nonfatal("`%%pathsearch' expects a macro identifier as first parameter");
H. Peter Anvin418ca702008-05-30 10:42:30 -07003253 free_tlist(origline);
3254 return DIRECTIVE_FOUND;
3255 }
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04003256 ctx = get_ctx(tline->text, &mname);
H. Peter Anvin418ca702008-05-30 10:42:30 -07003257 last = tline;
3258 tline = expand_smacro(tline->next);
3259 last->next = NULL;
3260
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003261 t = tline;
H. Peter Anvin418ca702008-05-30 10:42:30 -07003262 while (tok_type_(t, TOK_WHITESPACE))
3263 t = t->next;
3264
3265 if (!t || (t->type != TOK_STRING &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003266 t->type != TOK_INTERNAL_STRING)) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003267 nasm_nonfatal("`%%pathsearch' expects a file name");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003268 free_tlist(tline);
H. Peter Anvin418ca702008-05-30 10:42:30 -07003269 free_tlist(origline);
3270 return DIRECTIVE_FOUND; /* but we did _something_ */
3271 }
3272 if (t->next)
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08003273 nasm_warn(WARN_OTHER, "trailing garbage after `%%pathsearch' ignored");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003274 p = t->text;
H. Peter Anvin427cc912008-06-01 21:43:03 -07003275 if (t->type != TOK_INTERNAL_STRING)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003276 nasm_unquote(p, NULL);
H. Peter Anvin418ca702008-05-30 10:42:30 -07003277
H. Peter Anvin9924d1e2016-10-04 00:59:39 -07003278 inc_fopen(p, NULL, &found_path, INC_PROBE, NF_BINARY);
H. Peter Anvinccad6f92016-10-04 00:34:35 -07003279 if (!found_path)
3280 found_path = p;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003281 macro_start = nasm_malloc(sizeof(*macro_start));
3282 macro_start->next = NULL;
H. Peter Anvinccad6f92016-10-04 00:34:35 -07003283 macro_start->text = nasm_quote(found_path, strlen(found_path));
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003284 macro_start->type = TOK_STRING;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003285 macro_start->a.mac = NULL;
H. Peter Anvin418ca702008-05-30 10:42:30 -07003286
3287 /*
3288 * We now have a macro name, an implicit parameter count of
3289 * zero, and a string token to use as an expansion. Create
3290 * and store an SMacro.
3291 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003292 define_smacro(ctx, mname, casesense, 0, macro_start);
3293 free_tlist(tline);
H. Peter Anvin418ca702008-05-30 10:42:30 -07003294 free_tlist(origline);
3295 return DIRECTIVE_FOUND;
3296 }
3297
H. Peter Anvine2c80182005-01-15 22:15:51 +00003298 case PP_STRLEN:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003299 casesense = true;
H. Peter Anvin70653092007-10-19 14:42:29 -07003300
H. Peter Anvine2c80182005-01-15 22:15:51 +00003301 tline = tline->next;
3302 skip_white_(tline);
3303 tline = expand_id(tline);
3304 if (!tline || (tline->type != TOK_ID &&
3305 (tline->type != TOK_PREPROC_ID ||
3306 tline->text[1] != '$'))) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003307 nasm_nonfatal("`%%strlen' expects a macro identifier as first parameter");
H. Peter Anvine2c80182005-01-15 22:15:51 +00003308 free_tlist(origline);
3309 return DIRECTIVE_FOUND;
3310 }
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04003311 ctx = get_ctx(tline->text, &mname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003312 last = tline;
3313 tline = expand_smacro(tline->next);
3314 last->next = NULL;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003315
H. Peter Anvine2c80182005-01-15 22:15:51 +00003316 t = tline;
3317 while (tok_type_(t, TOK_WHITESPACE))
3318 t = t->next;
3319 /* t should now point to the string */
Cyrill Gorcunov4e1d5ab2010-07-23 18:51:51 +04003320 if (!tok_type_(t, TOK_STRING)) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003321 nasm_nonfatal("`%%strlen` requires string as second parameter");
H. Peter Anvine2c80182005-01-15 22:15:51 +00003322 free_tlist(tline);
3323 free_tlist(origline);
3324 return DIRECTIVE_FOUND;
3325 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003326
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003327 macro_start = nasm_malloc(sizeof(*macro_start));
3328 macro_start->next = NULL;
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07003329 make_tok_num(macro_start, nasm_unquote(t->text, NULL));
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003330 macro_start->a.mac = NULL;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003331
H. Peter Anvine2c80182005-01-15 22:15:51 +00003332 /*
3333 * We now have a macro name, an implicit parameter count of
3334 * zero, and a numeric token to use as an expansion. Create
3335 * and store an SMacro.
3336 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003337 define_smacro(ctx, mname, casesense, 0, macro_start);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003338 free_tlist(tline);
3339 free_tlist(origline);
3340 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003341
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003342 case PP_STRCAT:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003343 casesense = true;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003344
3345 tline = tline->next;
3346 skip_white_(tline);
3347 tline = expand_id(tline);
3348 if (!tline || (tline->type != TOK_ID &&
3349 (tline->type != TOK_PREPROC_ID ||
3350 tline->text[1] != '$'))) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003351 nasm_nonfatal("`%%strcat' expects a macro identifier as first parameter");
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003352 free_tlist(origline);
3353 return DIRECTIVE_FOUND;
3354 }
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04003355 ctx = get_ctx(tline->text, &mname);
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003356 last = tline;
3357 tline = expand_smacro(tline->next);
3358 last->next = NULL;
3359
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003360 len = 0;
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04003361 list_for_each(t, tline) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003362 switch (t->type) {
3363 case TOK_WHITESPACE:
3364 break;
3365 case TOK_STRING:
3366 len += t->a.len = nasm_unquote(t->text, NULL);
3367 break;
3368 case TOK_OTHER:
3369 if (!strcmp(t->text, ",")) /* permit comma separators */
3370 break;
3371 /* else fall through */
3372 default:
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003373 nasm_nonfatal("non-string passed to `%%strcat' (%d)", t->type);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003374 free_tlist(tline);
3375 free_tlist(origline);
3376 return DIRECTIVE_FOUND;
3377 }
3378 }
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003379
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003380 p = pp = nasm_malloc(len);
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04003381 list_for_each(t, tline) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003382 if (t->type == TOK_STRING) {
3383 memcpy(p, t->text, t->a.len);
3384 p += t->a.len;
3385 }
3386 }
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003387
3388 /*
3389 * We now have a macro name, an implicit parameter count of
3390 * zero, and a numeric token to use as an expansion. Create
3391 * and store an SMacro.
3392 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003393 macro_start = new_Token(NULL, TOK_STRING, NULL, 0);
3394 macro_start->text = nasm_quote(pp, len);
3395 nasm_free(pp);
3396 define_smacro(ctx, mname, casesense, 0, macro_start);
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003397 free_tlist(tline);
3398 free_tlist(origline);
3399 return DIRECTIVE_FOUND;
3400
H. Peter Anvine2c80182005-01-15 22:15:51 +00003401 case PP_SUBSTR:
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003402 {
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003403 int64_t start, count;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003404 size_t len;
H. Peter Anvind2456592008-06-19 15:04:18 -07003405
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003406 casesense = true;
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07003407
H. Peter Anvine2c80182005-01-15 22:15:51 +00003408 tline = tline->next;
3409 skip_white_(tline);
3410 tline = expand_id(tline);
3411 if (!tline || (tline->type != TOK_ID &&
3412 (tline->type != TOK_PREPROC_ID ||
3413 tline->text[1] != '$'))) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003414 nasm_nonfatal("`%%substr' expects a macro identifier as first parameter");
H. Peter Anvine2c80182005-01-15 22:15:51 +00003415 free_tlist(origline);
3416 return DIRECTIVE_FOUND;
3417 }
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04003418 ctx = get_ctx(tline->text, &mname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003419 last = tline;
3420 tline = expand_smacro(tline->next);
3421 last->next = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003422
Cyrill Gorcunov35519d62010-09-06 23:49:52 +04003423 if (tline) /* skip expanded id */
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003424 t = tline->next;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003425 while (tok_type_(t, TOK_WHITESPACE))
3426 t = t->next;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003427
H. Peter Anvine2c80182005-01-15 22:15:51 +00003428 /* t should now point to the string */
Cyrill Gorcunov35519d62010-09-06 23:49:52 +04003429 if (!tok_type_(t, TOK_STRING)) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003430 nasm_nonfatal("`%%substr` requires string as second parameter");
H. Peter Anvine2c80182005-01-15 22:15:51 +00003431 free_tlist(tline);
3432 free_tlist(origline);
3433 return DIRECTIVE_FOUND;
3434 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003435
H. Peter Anvine2c80182005-01-15 22:15:51 +00003436 tt = t->next;
3437 tptr = &tt;
3438 tokval.t_type = TOKEN_INVALID;
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08003439 evalresult = evaluate(ppscan, tptr, &tokval, NULL, true, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003440 if (!evalresult) {
3441 free_tlist(tline);
3442 free_tlist(origline);
3443 return DIRECTIVE_FOUND;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003444 } else if (!is_simple(evalresult)) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003445 nasm_nonfatal("non-constant value given to `%%substr`");
H. Peter Anvine2c80182005-01-15 22:15:51 +00003446 free_tlist(tline);
3447 free_tlist(origline);
3448 return DIRECTIVE_FOUND;
3449 }
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003450 start = evalresult->value - 1;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003451
3452 while (tok_type_(tt, TOK_WHITESPACE))
3453 tt = tt->next;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003454 if (!tt) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003455 count = 1; /* Backwards compatibility: one character */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003456 } else {
3457 tokval.t_type = TOKEN_INVALID;
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08003458 evalresult = evaluate(ppscan, tptr, &tokval, NULL, true, NULL);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003459 if (!evalresult) {
3460 free_tlist(tline);
3461 free_tlist(origline);
3462 return DIRECTIVE_FOUND;
3463 } else if (!is_simple(evalresult)) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003464 nasm_nonfatal("non-constant value given to `%%substr`");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003465 free_tlist(tline);
3466 free_tlist(origline);
3467 return DIRECTIVE_FOUND;
3468 }
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003469 count = evalresult->value;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003470 }
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003471
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003472 len = nasm_unquote(t->text, NULL);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003473
Cyrill Gorcunovcff031e2010-09-07 20:31:11 +04003474 /* make start and count being in range */
3475 if (start < 0)
3476 start = 0;
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003477 if (count < 0)
3478 count = len + count + 1 - start;
3479 if (start + count > (int64_t)len)
Cyrill Gorcunovcff031e2010-09-07 20:31:11 +04003480 count = len - start;
3481 if (!len || count < 0 || start >=(int64_t)len)
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003482 start = -1, count = 0; /* empty string */
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003483
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003484 macro_start = nasm_malloc(sizeof(*macro_start));
3485 macro_start->next = NULL;
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003486 macro_start->text = nasm_quote((start < 0) ? "" : t->text + start, count);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003487 macro_start->type = TOK_STRING;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003488 macro_start->a.mac = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003489
H. Peter Anvine2c80182005-01-15 22:15:51 +00003490 /*
3491 * We now have a macro name, an implicit parameter count of
3492 * zero, and a numeric token to use as an expansion. Create
3493 * and store an SMacro.
3494 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003495 define_smacro(ctx, mname, casesense, 0, macro_start);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003496 free_tlist(tline);
3497 free_tlist(origline);
3498 return DIRECTIVE_FOUND;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003499 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003500
H. Peter Anvine2c80182005-01-15 22:15:51 +00003501 case PP_ASSIGN:
3502 case PP_IASSIGN:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003503 casesense = (i == PP_ASSIGN);
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -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] != '$'))) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003511 nasm_nonfatal("`%%%sassign' expects a macro identifier",
3512 (i == PP_IASSIGN ? "i" : ""));
H. Peter Anvine2c80182005-01-15 22:15:51 +00003513 free_tlist(origline);
3514 return DIRECTIVE_FOUND;
3515 }
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04003516 ctx = get_ctx(tline->text, &mname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003517 last = tline;
3518 tline = expand_smacro(tline->next);
3519 last->next = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003520
H. Peter Anvine2c80182005-01-15 22:15:51 +00003521 t = tline;
3522 tptr = &t;
3523 tokval.t_type = TOKEN_INVALID;
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08003524 evalresult = evaluate(ppscan, tptr, &tokval, NULL, true, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003525 free_tlist(tline);
3526 if (!evalresult) {
3527 free_tlist(origline);
3528 return DIRECTIVE_FOUND;
3529 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003530
H. Peter Anvine2c80182005-01-15 22:15:51 +00003531 if (tokval.t_type)
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08003532 nasm_warn(WARN_OTHER, "trailing garbage after expression ignored");
H. Peter Anvin734b1882002-04-30 21:01:08 +00003533
H. Peter Anvine2c80182005-01-15 22:15:51 +00003534 if (!is_simple(evalresult)) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003535 nasm_nonfatal("non-constant value given to `%%%sassign'",
3536 (i == PP_IASSIGN ? "i" : ""));
H. Peter Anvine2c80182005-01-15 22:15:51 +00003537 free_tlist(origline);
3538 return DIRECTIVE_FOUND;
3539 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003540
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003541 macro_start = nasm_malloc(sizeof(*macro_start));
3542 macro_start->next = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003543 make_tok_num(macro_start, reloc_value(evalresult));
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003544 macro_start->a.mac = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003545
H. Peter Anvine2c80182005-01-15 22:15:51 +00003546 /*
3547 * We now have a macro name, an implicit parameter count of
3548 * zero, and a numeric token to use as an expansion. Create
3549 * and store an SMacro.
3550 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003551 define_smacro(ctx, mname, casesense, 0, macro_start);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003552 free_tlist(origline);
3553 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003554
H. Peter Anvine2c80182005-01-15 22:15:51 +00003555 case PP_LINE:
3556 /*
3557 * Syntax is `%line nnn[+mmm] [filename]'
3558 */
H. Peter Anvin (Intel)800c1682018-12-14 12:22:11 -08003559 if (unlikely(pp_noline)) {
3560 free_tlist(origline);
3561 return DIRECTIVE_FOUND;
3562 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00003563 tline = tline->next;
3564 skip_white_(tline);
3565 if (!tok_type_(tline, TOK_NUMBER)) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003566 nasm_nonfatal("`%%line' expects line number");
H. Peter Anvine2c80182005-01-15 22:15:51 +00003567 free_tlist(origline);
3568 return DIRECTIVE_FOUND;
3569 }
H. Peter Anvin70055962007-10-11 00:05:31 -07003570 k = readnum(tline->text, &err);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003571 m = 1;
3572 tline = tline->next;
3573 if (tok_is_(tline, "+")) {
3574 tline = tline->next;
3575 if (!tok_type_(tline, TOK_NUMBER)) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003576 nasm_nonfatal("`%%line' expects line increment");
H. Peter Anvine2c80182005-01-15 22:15:51 +00003577 free_tlist(origline);
3578 return DIRECTIVE_FOUND;
3579 }
H. Peter Anvin70055962007-10-11 00:05:31 -07003580 m = readnum(tline->text, &err);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003581 tline = tline->next;
3582 }
3583 skip_white_(tline);
3584 src_set_linnum(k);
3585 istk->lineinc = m;
3586 if (tline) {
H. Peter Anvin274cda82016-05-10 02:56:29 -07003587 char *fname = detoken(tline, false);
3588 src_set_fname(fname);
3589 nasm_free(fname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003590 }
3591 free_tlist(origline);
3592 return DIRECTIVE_FOUND;
Keith Kaniosb307a4f2010-11-06 17:41:51 -05003593
H. Peter Anvine2c80182005-01-15 22:15:51 +00003594 default:
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003595 nasm_fatal("preprocessor directive `%s' not yet implemented",
3596 pp_directives[i]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003597 return DIRECTIVE_FOUND;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003598 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003599}
3600
3601/*
H. Peter Anvin76690a12002-04-30 20:52:49 +00003602 * Ensure that a macro parameter contains a condition code and
3603 * nothing else. Return the condition code index if so, or -1
3604 * otherwise.
3605 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003606static int find_cc(Token * t)
H. Peter Anvineba20a72002-04-30 20:53:55 +00003607{
H. Peter Anvin76690a12002-04-30 20:52:49 +00003608 Token *tt;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003609
H. Peter Anvin25a99342007-09-22 17:45:45 -07003610 if (!t)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003611 return -1; /* Probably a %+ without a space */
H. Peter Anvin25a99342007-09-22 17:45:45 -07003612
H. Peter Anvineba20a72002-04-30 20:53:55 +00003613 skip_white_(t);
Cyrill Gorcunov7524cfd2017-10-22 19:01:16 +03003614 if (!t)
3615 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003616 if (t->type != TOK_ID)
H. Peter Anvine2c80182005-01-15 22:15:51 +00003617 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003618 tt = t->next;
H. Peter Anvineba20a72002-04-30 20:53:55 +00003619 skip_white_(tt);
H. Peter Anvin76690a12002-04-30 20:52:49 +00003620 if (tt && (tt->type != TOK_OTHER || strcmp(tt->text, ",")))
H. Peter Anvine2c80182005-01-15 22:15:51 +00003621 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003622
Cyrill Gorcunov19456392012-05-02 00:18:56 +04003623 return bsii(t->text, (const char **)conditions, ARRAY_SIZE(conditions));
H. Peter Anvin76690a12002-04-30 20:52:49 +00003624}
3625
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003626/*
3627 * This routines walks over tokens strem and hadnles tokens
3628 * pasting, if @handle_explicit passed then explicit pasting
3629 * term is handled, otherwise -- implicit pastings only.
3630 */
Cyrill Gorcunov575d4282010-10-06 00:25:55 +04003631static bool paste_tokens(Token **head, const struct tokseq_match *m,
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003632 size_t mnum, bool handle_explicit)
H. Peter Anvind784a082009-04-20 14:01:18 -07003633{
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003634 Token *tok, *next, **prev_next, **prev_nonspace;
3635 bool pasted = false;
3636 char *buf, *p;
3637 size_t len, i;
H. Peter Anvind784a082009-04-20 14:01:18 -07003638
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003639 /*
3640 * The last token before pasting. We need it
3641 * to be able to connect new handled tokens.
3642 * In other words if there were a tokens stream
3643 *
3644 * A -> B -> C -> D
3645 *
3646 * and we've joined tokens B and C, the resulting
3647 * stream should be
3648 *
3649 * A -> BC -> D
3650 */
3651 tok = *head;
3652 prev_next = NULL;
3653
3654 if (!tok_type_(tok, TOK_WHITESPACE) && !tok_type_(tok, TOK_PASTE))
3655 prev_nonspace = head;
3656 else
3657 prev_nonspace = NULL;
3658
3659 while (tok && (next = tok->next)) {
3660
3661 switch (tok->type) {
H. Peter Anvind784a082009-04-20 14:01:18 -07003662 case TOK_WHITESPACE:
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003663 /* Zap redundant whitespaces */
3664 while (tok_type_(next, TOK_WHITESPACE))
3665 next = delete_Token(next);
3666 tok->next = next;
H. Peter Anvind784a082009-04-20 14:01:18 -07003667 break;
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003668
3669 case TOK_PASTE:
3670 /* Explicit pasting */
3671 if (!handle_explicit)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003672 break;
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003673 next = delete_Token(tok);
3674
3675 while (tok_type_(next, TOK_WHITESPACE))
3676 next = delete_Token(next);
3677
3678 if (!pasted)
3679 pasted = true;
3680
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003681 /* Left pasting token is start of line */
3682 if (!prev_nonspace)
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003683 nasm_fatal("No lvalue found on pasting");
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003684
Cyrill Gorcunov8b5c9fb2013-02-04 01:24:54 +04003685 /*
3686 * No ending token, this might happen in two
3687 * cases
3688 *
3689 * 1) There indeed no right token at all
3690 * 2) There is a bare "%define ID" statement,
3691 * and @ID does expand to whitespace.
3692 *
3693 * So technically we need to do a grammar analysis
3694 * in another stage of parsing, but for now lets don't
3695 * change the behaviour people used to. Simply allow
3696 * whitespace after paste token.
3697 */
3698 if (!next) {
3699 /*
3700 * Zap ending space tokens and that's all.
3701 */
3702 tok = (*prev_nonspace)->next;
3703 while (tok_type_(tok, TOK_WHITESPACE))
3704 tok = delete_Token(tok);
3705 tok = *prev_nonspace;
3706 tok->next = NULL;
3707 break;
3708 }
3709
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003710 tok = *prev_nonspace;
3711 while (tok_type_(tok, TOK_WHITESPACE))
3712 tok = delete_Token(tok);
3713 len = strlen(tok->text);
3714 len += strlen(next->text);
3715
3716 p = buf = nasm_malloc(len + 1);
3717 strcpy(p, tok->text);
3718 p = strchr(p, '\0');
3719 strcpy(p, next->text);
3720
3721 delete_Token(tok);
3722
3723 tok = tokenize(buf);
3724 nasm_free(buf);
3725
3726 *prev_nonspace = tok;
3727 while (tok && tok->next)
3728 tok = tok->next;
3729
3730 tok->next = delete_Token(next);
3731
3732 /* Restart from pasted tokens head */
3733 tok = *prev_nonspace;
3734 break;
3735
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003736 default:
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003737 /* implicit pasting */
Cyrill Gorcunov575d4282010-10-06 00:25:55 +04003738 for (i = 0; i < mnum; i++) {
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003739 if (!(PP_CONCAT_MATCH(tok, m[i].mask_head)))
3740 continue;
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +04003741
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003742 len = 0;
3743 while (next && PP_CONCAT_MATCH(next, m[i].mask_tail)) {
3744 len += strlen(next->text);
3745 next = next->next;
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +04003746 }
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003747
Cyrill Gorcunov6f8109e2017-10-22 21:26:36 +03003748 /* No match or no text to process */
3749 if (tok == next || len == 0)
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003750 break;
3751
3752 len += strlen(tok->text);
3753 p = buf = nasm_malloc(len + 1);
3754
Adam Majer1a069432017-07-25 11:12:35 +02003755 strcpy(p, tok->text);
3756 p = strchr(p, '\0');
3757 tok = delete_Token(tok);
3758
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003759 while (tok != next) {
Adam Majer1a069432017-07-25 11:12:35 +02003760 if (PP_CONCAT_MATCH(tok, m[i].mask_tail)) {
3761 strcpy(p, tok->text);
3762 p = strchr(p, '\0');
3763 }
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003764 tok = delete_Token(tok);
3765 }
3766
3767 tok = tokenize(buf);
3768 nasm_free(buf);
3769
3770 if (prev_next)
3771 *prev_next = tok;
3772 else
3773 *head = tok;
3774
3775 /*
3776 * Connect pasted into original stream,
3777 * ie A -> new-tokens -> B
3778 */
3779 while (tok && tok->next)
3780 tok = tok->next;
3781 tok->next = next;
3782
3783 if (!pasted)
3784 pasted = true;
3785
3786 /* Restart from pasted tokens head */
3787 tok = prev_next ? *prev_next : *head;
Cyrill Gorcunov575d4282010-10-06 00:25:55 +04003788 }
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003789
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003790 break;
H. Peter Anvind784a082009-04-20 14:01:18 -07003791 }
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003792
3793 prev_next = &tok->next;
3794
3795 if (tok->next &&
3796 !tok_type_(tok->next, TOK_WHITESPACE) &&
3797 !tok_type_(tok->next, TOK_PASTE))
3798 prev_nonspace = prev_next;
3799
3800 tok = tok->next;
H. Peter Anvind784a082009-04-20 14:01:18 -07003801 }
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003802
3803 return pasted;
H. Peter Anvind784a082009-04-20 14:01:18 -07003804}
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003805
3806/*
3807 * expands to a list of tokens from %{x:y}
3808 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003809static Token *expand_mmac_params_range(MMacro *mac, Token *tline, Token ***last)
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003810{
3811 Token *t = tline, **tt, *tm, *head;
3812 char *pos;
3813 int fst, lst, j, i;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003814
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003815 pos = strchr(tline->text, ':');
3816 nasm_assert(pos);
3817
3818 lst = atoi(pos + 1);
3819 fst = atoi(tline->text + 1);
3820
3821 /*
3822 * only macros params are accounted so
3823 * if someone passes %0 -- we reject such
3824 * value(s)
3825 */
3826 if (lst == 0 || fst == 0)
3827 goto err;
3828
3829 /* the values should be sane */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003830 if ((fst > (int)mac->nparam || fst < (-(int)mac->nparam)) ||
3831 (lst > (int)mac->nparam || lst < (-(int)mac->nparam)))
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003832 goto err;
3833
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003834 fst = fst < 0 ? fst + (int)mac->nparam + 1: fst;
3835 lst = lst < 0 ? lst + (int)mac->nparam + 1: lst;
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003836
3837 /* counted from zero */
3838 fst--, lst--;
3839
3840 /*
Cyrill Gorcunove75331c2013-11-09 12:02:15 +04003841 * It will be at least one token. Note we
3842 * need to scan params until separator, otherwise
3843 * only first token will be passed.
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003844 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003845 tm = mac->params[(fst + mac->rotate) % mac->nparam];
Cyrill Gorcunov67f2ca22018-10-13 19:41:01 +03003846 if (!tm)
3847 goto err;
Cyrill Gorcunove75331c2013-11-09 12:02:15 +04003848 head = new_Token(NULL, tm->type, tm->text, 0);
3849 tt = &head->next, tm = tm->next;
3850 while (tok_isnt_(tm, ",")) {
3851 t = new_Token(NULL, tm->type, tm->text, 0);
3852 *tt = t, tt = &t->next, tm = tm->next;
3853 }
3854
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003855 if (fst < lst) {
3856 for (i = fst + 1; i <= lst; i++) {
3857 t = new_Token(NULL, TOK_OTHER, ",", 0);
3858 *tt = t, tt = &t->next;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003859 j = (i + mac->rotate) % mac->nparam;
3860 tm = mac->params[j];
Cyrill Gorcunove75331c2013-11-09 12:02:15 +04003861 while (tok_isnt_(tm, ",")) {
3862 t = new_Token(NULL, tm->type, tm->text, 0);
3863 *tt = t, tt = &t->next, tm = tm->next;
3864 }
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003865 }
3866 } else {
3867 for (i = fst - 1; i >= lst; i--) {
3868 t = new_Token(NULL, TOK_OTHER, ",", 0);
3869 *tt = t, tt = &t->next;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003870 j = (i + mac->rotate) % mac->nparam;
3871 tm = mac->params[j];
Cyrill Gorcunove75331c2013-11-09 12:02:15 +04003872 while (tok_isnt_(tm, ",")) {
3873 t = new_Token(NULL, tm->type, tm->text, 0);
3874 *tt = t, tt = &t->next, tm = tm->next;
3875 }
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003876 }
3877 }
3878
3879 *last = tt;
3880 return head;
3881
3882err:
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003883 nasm_nonfatal("`%%{%s}': macro parameters out of range",
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003884 &tline->text[1]);
3885 return tline;
3886}
3887
H. Peter Anvin76690a12002-04-30 20:52:49 +00003888/*
3889 * Expand MMacro-local things: parameter references (%0, %n, %+n,
H. Peter Anvin67c63722008-10-26 23:49:00 -07003890 * %-n) and MMacro-local identifiers (%%foo) as well as
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003891 * macro indirection (%[...]) and range (%{..:..}).
H. Peter Anvin76690a12002-04-30 20:52:49 +00003892 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003893static Token *expand_mmac_params(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00003894{
H. Peter Anvin734b1882002-04-30 21:01:08 +00003895 Token *t, *tt, **tail, *thead;
H. Peter Anvin6125b622009-04-08 14:02:25 -07003896 bool changed = false;
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003897 char *pos;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003898
3899 tail = &thead;
3900 thead = NULL;
3901
H. Peter Anvine2c80182005-01-15 22:15:51 +00003902 while (tline) {
Cyrill Gorcunov661f7232018-10-28 20:39:34 +03003903 if (tline->type == TOK_PREPROC_ID && tline->text && tline->text[0] &&
Cyrill Gorcunovca611192010-06-04 09:22:12 +04003904 (((tline->text[1] == '+' || tline->text[1] == '-') && tline->text[2]) ||
3905 (tline->text[1] >= '0' && tline->text[1] <= '9') ||
3906 tline->text[1] == '%')) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00003907 char *text = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003908 int type = 0, cc; /* type = 0 to placate optimisers */
Keith Kaniosa6dfa782007-04-13 16:47:53 +00003909 char tmpbuf[30];
H. Peter Anvin25a99342007-09-22 17:45:45 -07003910 unsigned int n;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003911 int i;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003912 MMacro *mac;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003913
H. Peter Anvine2c80182005-01-15 22:15:51 +00003914 t = tline;
3915 tline = tline->next;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003916
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003917 mac = istk->mstk;
3918 while (mac && !mac->name) /* avoid mistaking %reps for macros */
3919 mac = mac->next_active;
3920 if (!mac) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003921 nasm_nonfatal("`%s': not in a macro call", t->text);
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003922 } else {
3923 pos = strchr(t->text, ':');
3924 if (!pos) {
3925 switch (t->text[1]) {
3926 /*
3927 * We have to make a substitution of one of the
3928 * forms %1, %-1, %+1, %%foo, %0.
3929 */
3930 case '0':
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003931 type = TOK_NUMBER;
3932 snprintf(tmpbuf, sizeof(tmpbuf), "%d", mac->nparam);
3933 text = nasm_strdup(tmpbuf);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003934 break;
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003935 case '%':
H. Peter Anvine2c80182005-01-15 22:15:51 +00003936 type = TOK_ID;
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003937 snprintf(tmpbuf, sizeof(tmpbuf), "..@%"PRIu64".",
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003938 mac->unique);
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003939 text = nasm_strcat(tmpbuf, t->text + 2);
3940 break;
3941 case '-':
3942 n = atoi(t->text + 2) - 1;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003943 if (n >= mac->nparam)
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003944 tt = NULL;
3945 else {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003946 if (mac->nparam > 1)
3947 n = (n + mac->rotate) % mac->nparam;
3948 tt = mac->params[n];
H. Peter Anvine2c80182005-01-15 22:15:51 +00003949 }
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003950 cc = find_cc(tt);
3951 if (cc == -1) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003952 nasm_nonfatal("macro parameter %d is not a condition code",
3953 n + 1);
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003954 text = NULL;
3955 } else {
3956 type = TOK_ID;
3957 if (inverse_ccs[cc] == -1) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003958 nasm_nonfatal("condition code `%s' is not invertible",
3959 conditions[cc]);
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003960 text = NULL;
3961 } else
3962 text = nasm_strdup(conditions[inverse_ccs[cc]]);
3963 }
3964 break;
3965 case '+':
3966 n = atoi(t->text + 2) - 1;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003967 if (n >= mac->nparam)
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003968 tt = NULL;
3969 else {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003970 if (mac->nparam > 1)
3971 n = (n + mac->rotate) % mac->nparam;
3972 tt = mac->params[n];
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003973 }
3974 cc = find_cc(tt);
3975 if (cc == -1) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003976 nasm_nonfatal("macro parameter %d is not a condition code",
3977 n + 1);
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003978 text = NULL;
3979 } else {
3980 type = TOK_ID;
3981 text = nasm_strdup(conditions[cc]);
3982 }
3983 break;
3984 default:
3985 n = atoi(t->text + 1) - 1;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003986 if (n >= mac->nparam)
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003987 tt = NULL;
3988 else {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003989 if (mac->nparam > 1)
3990 n = (n + mac->rotate) % mac->nparam;
3991 tt = mac->params[n];
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003992 }
3993 if (tt) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003994 for (i = 0; i < mac->paramlen[n]; i++) {
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003995 *tail = new_Token(NULL, tt->type, tt->text, 0);
3996 tail = &(*tail)->next;
3997 tt = tt->next;
3998 }
3999 }
4000 text = NULL; /* we've done it here */
4001 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004002 }
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004003 } else {
4004 /*
4005 * seems we have a parameters range here
4006 */
4007 Token *head, **last;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004008 head = expand_mmac_params_range(mac, t, &last);
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004009 if (head != t) {
4010 *tail = head;
4011 *last = tline;
4012 tline = head;
4013 text = NULL;
4014 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00004015 }
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004016 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00004017 if (!text) {
4018 delete_Token(t);
4019 } else {
4020 *tail = t;
4021 tail = &t->next;
4022 t->type = type;
4023 nasm_free(t->text);
4024 t->text = text;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004025 t->a.mac = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004026 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004027 changed = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004028 continue;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004029 } else if (tline->type == TOK_INDIRECT) {
4030 t = tline;
4031 tline = tline->next;
4032 tt = tokenize(t->text);
4033 tt = expand_mmac_params(tt);
4034 tt = expand_smacro(tt);
4035 *tail = tt;
4036 while (tt) {
4037 tt->a.mac = NULL; /* Necessary? */
4038 tail = &tt->next;
4039 tt = tt->next;
4040 }
4041 delete_Token(t);
4042 changed = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004043 } else {
4044 t = *tail = tline;
4045 tline = tline->next;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004046 t->a.mac = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004047 tail = &t->next;
4048 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00004049 }
H. Peter Anvineba20a72002-04-30 20:53:55 +00004050 *tail = NULL;
H. Peter Anvin67c63722008-10-26 23:49:00 -07004051
Cyrill Gorcunovc6a742c2011-06-27 01:23:09 +04004052 if (changed) {
4053 const struct tokseq_match t[] = {
4054 {
4055 PP_CONCAT_MASK(TOK_ID) |
4056 PP_CONCAT_MASK(TOK_FLOAT), /* head */
4057 PP_CONCAT_MASK(TOK_ID) |
4058 PP_CONCAT_MASK(TOK_NUMBER) |
4059 PP_CONCAT_MASK(TOK_FLOAT) |
4060 PP_CONCAT_MASK(TOK_OTHER) /* tail */
4061 },
4062 {
4063 PP_CONCAT_MASK(TOK_NUMBER), /* head */
4064 PP_CONCAT_MASK(TOK_NUMBER) /* tail */
4065 }
4066 };
4067 paste_tokens(&thead, t, ARRAY_SIZE(t), false);
4068 }
H. Peter Anvin6125b622009-04-08 14:02:25 -07004069
H. Peter Anvin76690a12002-04-30 20:52:49 +00004070 return thead;
4071}
4072
4073/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004074 * Expand all single-line macro calls made in the given line.
4075 * Return the expanded version of the line. The original is deemed
4076 * to be destroyed in the process. (In reality we'll just move
4077 * Tokens from input to output a lot of the time, rather than
4078 * actually bothering to destroy and replicate.)
4079 */
H. Peter Anvincb1cf592007-11-19 12:26:50 -08004080
H. Peter Anvine2c80182005-01-15 22:15:51 +00004081static Token *expand_smacro(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004082{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004083 Token *t, *tt, *mstart, **tail, *thead;
H. Peter Anvineba20a72002-04-30 20:53:55 +00004084 SMacro *head = NULL, *m;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004085 Token **params;
4086 int *paramsize;
H. Peter Anvin25a99342007-09-22 17:45:45 -07004087 unsigned int nparam, sparam;
H. Peter Anvind784a082009-04-20 14:01:18 -07004088 int brackets;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004089 Token *org_tline = tline;
4090 Context *ctx;
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08004091 const char *mname;
H. Peter Anvina3d96d02018-06-15 17:51:39 -07004092 int64_t deadman = nasm_limit[LIMIT_MACROS];
H. Peter Anvin8287daf2009-07-07 16:00:58 -07004093 bool expanded;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004094
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004095 /*
4096 * Trick: we should avoid changing the start token pointer since it can
4097 * be contained in "next" field of other token. Because of this
4098 * we allocate a copy of first token and work with it; at the end of
4099 * routine we copy it back
4100 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004101 if (org_tline) {
Cyrill Gorcunoved4a8052010-04-09 15:40:35 +04004102 tline = new_Token(org_tline->next, org_tline->type,
4103 org_tline->text, 0);
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004104 tline->a.mac = org_tline->a.mac;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004105 nasm_free(org_tline->text);
4106 org_tline->text = NULL;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004107 }
4108
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004109 expanded = true; /* Always expand %+ at least once */
H. Peter Anvin8287daf2009-07-07 16:00:58 -07004110
H. Peter Anvincb1cf592007-11-19 12:26:50 -08004111again:
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004112 thead = NULL;
Cyrill Gorcunoved4a8052010-04-09 15:40:35 +04004113 tail = &thead;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004114
H. Peter Anvine2c80182005-01-15 22:15:51 +00004115 while (tline) { /* main token loop */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004116 if (!--deadman) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03004117 nasm_nonfatal("interminable macro recursion");
Cyrill Gorcunovbd38c8f2009-11-21 11:11:23 +03004118 goto err;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004119 }
H. Peter Anvincb1cf592007-11-19 12:26:50 -08004120
H. Peter Anvine2c80182005-01-15 22:15:51 +00004121 if ((mname = tline->text)) {
4122 /* if this token is a local macro, look in local context */
Cyrill Gorcunovc56d9ad2010-02-11 15:12:19 +03004123 if (tline->type == TOK_ID) {
4124 head = (SMacro *)hash_findix(&smacros, mname);
4125 } else if (tline->type == TOK_PREPROC_ID) {
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04004126 ctx = get_ctx(mname, &mname);
Cyrill Gorcunovc56d9ad2010-02-11 15:12:19 +03004127 head = ctx ? (SMacro *)hash_findix(&ctx->localmac, mname) : NULL;
4128 } else
4129 head = NULL;
H. Peter Anvin072771e2008-05-22 13:17:51 -07004130
H. Peter Anvine2c80182005-01-15 22:15:51 +00004131 /*
4132 * We've hit an identifier. As in is_mmacro below, we first
4133 * check whether the identifier is a single-line macro at
4134 * all, then think about checking for parameters if
4135 * necessary.
4136 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004137 list_for_each(m, head)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004138 if (!mstrcmp(m->name, mname, m->casesense))
4139 break;
4140 if (m) {
4141 mstart = tline;
4142 params = NULL;
4143 paramsize = NULL;
4144 if (m->nparam == 0) {
4145 /*
4146 * Simple case: the macro is parameterless. Discard the
4147 * one token that the macro call took, and push the
4148 * expansion back on the to-do stack.
4149 */
4150 if (!m->expansion) {
4151 if (!strcmp("__FILE__", m->name)) {
H. Peter Anvin274cda82016-05-10 02:56:29 -07004152 const char *file = src_get_fname();
4153 /* nasm_free(tline->text); here? */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004154 tline->text = nasm_quote(file, strlen(file));
4155 tline->type = TOK_STRING;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004156 continue;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004157 }
4158 if (!strcmp("__LINE__", m->name)) {
4159 nasm_free(tline->text);
4160 make_tok_num(tline, src_get_linnum());
4161 continue;
4162 }
4163 if (!strcmp("__BITS__", m->name)) {
4164 nasm_free(tline->text);
4165 make_tok_num(tline, globalbits);
4166 continue;
4167 }
4168 tline = delete_Token(tline);
4169 continue;
4170 }
4171 } else {
4172 /*
4173 * Complicated case: at least one macro with this name
H. Peter Anvine2c80182005-01-15 22:15:51 +00004174 * exists and takes parameters. We must find the
4175 * parameters in the call, count them, find the SMacro
4176 * that corresponds to that form of the macro call, and
4177 * substitute for the parameters when we expand. What a
4178 * pain.
4179 */
4180 /*tline = tline->next;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004181 skip_white_(tline); */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004182 do {
4183 t = tline->next;
4184 while (tok_type_(t, TOK_SMAC_END)) {
Cyrill Gorcunov982186a2019-03-16 23:05:50 +03004185 if (t->a.mac)
4186 t->a.mac->in_progress = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004187 t->text = NULL;
4188 t = tline->next = delete_Token(t);
4189 }
4190 tline = t;
4191 } while (tok_type_(tline, TOK_WHITESPACE));
4192 if (!tok_is_(tline, "(")) {
4193 /*
4194 * This macro wasn't called with parameters: ignore
4195 * the call. (Behaviour borrowed from gnu cpp.)
4196 */
4197 tline = mstart;
4198 m = NULL;
4199 } else {
4200 int paren = 0;
4201 int white = 0;
4202 brackets = 0;
4203 nparam = 0;
4204 sparam = PARAM_DELTA;
4205 params = nasm_malloc(sparam * sizeof(Token *));
4206 params[0] = tline->next;
4207 paramsize = nasm_malloc(sparam * sizeof(int));
4208 paramsize[0] = 0;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07004209 while (true) { /* parameter loop */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004210 /*
4211 * For some unusual expansions
4212 * which concatenates function call
4213 */
4214 t = tline->next;
4215 while (tok_type_(t, TOK_SMAC_END)) {
Cyrill Gorcunov982186a2019-03-16 23:05:50 +03004216 if (t->a.mac)
4217 t->a.mac->in_progress = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004218 t->text = NULL;
4219 t = tline->next = delete_Token(t);
4220 }
4221 tline = t;
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00004222
H. Peter Anvine2c80182005-01-15 22:15:51 +00004223 if (!tline) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03004224 nasm_nonfatal("macro call expects terminating `)'");
H. Peter Anvine2c80182005-01-15 22:15:51 +00004225 break;
4226 }
4227 if (tline->type == TOK_WHITESPACE
4228 && brackets <= 0) {
4229 if (paramsize[nparam])
4230 white++;
4231 else
4232 params[nparam] = tline->next;
4233 continue; /* parameter loop */
4234 }
4235 if (tline->type == TOK_OTHER
4236 && tline->text[1] == 0) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004237 char ch = tline->text[0];
H. Peter Anvine2c80182005-01-15 22:15:51 +00004238 if (ch == ',' && !paren && brackets <= 0) {
4239 if (++nparam >= sparam) {
4240 sparam += PARAM_DELTA;
4241 params = nasm_realloc(params,
Cyrill Gorcunoved4a8052010-04-09 15:40:35 +04004242 sparam * sizeof(Token *));
4243 paramsize = nasm_realloc(paramsize,
4244 sparam * sizeof(int));
H. Peter Anvine2c80182005-01-15 22:15:51 +00004245 }
4246 params[nparam] = tline->next;
4247 paramsize[nparam] = 0;
4248 white = 0;
4249 continue; /* parameter loop */
4250 }
4251 if (ch == '{' &&
4252 (brackets > 0 || (brackets == 0 &&
4253 !paramsize[nparam])))
4254 {
4255 if (!(brackets++)) {
4256 params[nparam] = tline->next;
4257 continue; /* parameter loop */
4258 }
4259 }
4260 if (ch == '}' && brackets > 0)
4261 if (--brackets == 0) {
4262 brackets = -1;
4263 continue; /* parameter loop */
4264 }
4265 if (ch == '(' && !brackets)
4266 paren++;
4267 if (ch == ')' && brackets <= 0)
4268 if (--paren < 0)
4269 break;
4270 }
4271 if (brackets < 0) {
4272 brackets = 0;
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03004273 nasm_nonfatal("braces do not "
H. Peter Anvine2c80182005-01-15 22:15:51 +00004274 "enclose all of macro parameter");
4275 }
4276 paramsize[nparam] += white + 1;
4277 white = 0;
4278 } /* parameter loop */
4279 nparam++;
4280 while (m && (m->nparam != nparam ||
4281 mstrcmp(m->name, mname,
4282 m->casesense)))
4283 m = m->next;
H. Peter Anvin (Intel)723ab482018-12-13 21:53:31 -08004284 if (!m) {
4285 /*!
4286 *!macro-params [on] macro calls with wrong parameter count
4287 *! covers warnings about \i{multi-line macros} being invoked
4288 *! with the wrong number of parameters. See \k{mlmacover} for an
4289 *! example of why you might want to disable this warning.
4290 */
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08004291 nasm_warn(WARN_MACRO_PARAMS,
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03004292 "macro `%s' exists, "
4293 "but not taking %d parameters",
4294 mstart->text, nparam);
H. Peter Anvin (Intel)723ab482018-12-13 21:53:31 -08004295 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00004296 }
4297 }
4298 if (m && m->in_progress)
4299 m = NULL;
4300 if (!m) { /* in progess or didn't find '(' or wrong nparam */
H. Peter Anvin70653092007-10-19 14:42:29 -07004301 /*
H. Peter Anvine2c80182005-01-15 22:15:51 +00004302 * Design question: should we handle !tline, which
4303 * indicates missing ')' here, or expand those
4304 * macros anyway, which requires the (t) test a few
H. Peter Anvin70653092007-10-19 14:42:29 -07004305 * lines down?
H. Peter Anvine2c80182005-01-15 22:15:51 +00004306 */
4307 nasm_free(params);
4308 nasm_free(paramsize);
4309 tline = mstart;
4310 } else {
4311 /*
4312 * Expand the macro: we are placed on the last token of the
4313 * call, so that we can easily split the call from the
4314 * following tokens. We also start by pushing an SMAC_END
4315 * token for the cycle removal.
4316 */
4317 t = tline;
4318 if (t) {
4319 tline = t->next;
4320 t->next = NULL;
4321 }
4322 tt = new_Token(tline, TOK_SMAC_END, NULL, 0);
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004323 tt->a.mac = m;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07004324 m->in_progress = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004325 tline = tt;
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04004326 list_for_each(t, m->expansion) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004327 if (t->type >= TOK_SMAC_PARAM) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00004328 Token *pcopy = tline, **ptail = &pcopy;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004329 Token *ttt, *pt;
4330 int i;
H. Peter Anvin734b1882002-04-30 21:01:08 +00004331
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004332 ttt = params[t->type - TOK_SMAC_PARAM];
4333 i = paramsize[t->type - TOK_SMAC_PARAM];
4334 while (--i >= 0) {
4335 pt = *ptail = new_Token(tline, ttt->type,
4336 ttt->text, 0);
4337 ptail = &pt->next;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004338 ttt = ttt->next;
Cyrill Gorcunov59ce1c62017-10-22 18:42:07 +03004339 if (!ttt && i > 0) {
4340 /*
4341 * FIXME: Need to handle more gracefully,
4342 * exiting early on agruments analysis.
4343 */
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03004344 nasm_fatal("macro `%s' expects %d args",
Cyrill Gorcunov59ce1c62017-10-22 18:42:07 +03004345 mstart->text,
4346 (int)paramsize[t->type - TOK_SMAC_PARAM]);
4347 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00004348 }
4349 tline = pcopy;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004350 } else if (t->type == TOK_PREPROC_Q) {
4351 tt = new_Token(tline, TOK_ID, mname, 0);
4352 tline = tt;
4353 } else if (t->type == TOK_PREPROC_QQ) {
4354 tt = new_Token(tline, TOK_ID, m->name, 0);
4355 tline = tt;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004356 } else {
4357 tt = new_Token(tline, t->type, t->text, 0);
4358 tline = tt;
4359 }
4360 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00004361
H. Peter Anvine2c80182005-01-15 22:15:51 +00004362 /*
4363 * Having done that, get rid of the macro call, and clean
4364 * up the parameters.
4365 */
4366 nasm_free(params);
4367 nasm_free(paramsize);
4368 free_tlist(mstart);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004369 expanded = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004370 continue; /* main token loop */
4371 }
4372 }
4373 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004374
H. Peter Anvine2c80182005-01-15 22:15:51 +00004375 if (tline->type == TOK_SMAC_END) {
Cyrill Gorcunov980dd652018-10-14 19:25:32 +03004376 /* On error path it might already be dropped */
4377 if (tline->a.mac)
4378 tline->a.mac->in_progress = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004379 tline = delete_Token(tline);
4380 } else {
4381 t = *tail = tline;
4382 tline = tline->next;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004383 t->a.mac = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004384 t->next = NULL;
4385 tail = &t->next;
4386 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004387 }
4388
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004389 /*
4390 * Now scan the entire line and look for successive TOK_IDs that resulted
Keith Kaniosb7a89542007-04-12 02:40:54 +00004391 * after expansion (they can't be produced by tokenize()). The successive
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004392 * TOK_IDs should be concatenated.
4393 * Also we look for %+ tokens and concatenate the tokens before and after
4394 * them (without white spaces in between).
4395 */
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +04004396 if (expanded) {
Cyrill Gorcunovc6a742c2011-06-27 01:23:09 +04004397 const struct tokseq_match t[] = {
4398 {
4399 PP_CONCAT_MASK(TOK_ID) |
4400 PP_CONCAT_MASK(TOK_PREPROC_ID), /* head */
4401 PP_CONCAT_MASK(TOK_ID) |
4402 PP_CONCAT_MASK(TOK_PREPROC_ID) |
4403 PP_CONCAT_MASK(TOK_NUMBER) /* tail */
4404 }
4405 };
4406 if (paste_tokens(&thead, t, ARRAY_SIZE(t), true)) {
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +04004407 /*
4408 * If we concatenated something, *and* we had previously expanded
4409 * an actual macro, scan the lines again for macros...
4410 */
4411 tline = thead;
4412 expanded = false;
4413 goto again;
4414 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00004415 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004416
Cyrill Gorcunovbd38c8f2009-11-21 11:11:23 +03004417err:
H. Peter Anvine2c80182005-01-15 22:15:51 +00004418 if (org_tline) {
4419 if (thead) {
4420 *org_tline = *thead;
4421 /* since we just gave text to org_line, don't free it */
4422 thead->text = NULL;
4423 delete_Token(thead);
4424 } else {
4425 /* the expression expanded to empty line;
4426 we can't return NULL for some reasons
4427 we just set the line to a single WHITESPACE token. */
4428 memset(org_tline, 0, sizeof(*org_tline));
4429 org_tline->text = NULL;
4430 org_tline->type = TOK_WHITESPACE;
4431 }
4432 thead = org_tline;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004433 }
4434
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004435 return thead;
4436}
4437
4438/*
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004439 * Similar to expand_smacro but used exclusively with macro identifiers
4440 * right before they are fetched in. The reason is that there can be
4441 * identifiers consisting of several subparts. We consider that if there
4442 * are more than one element forming the name, user wants a expansion,
4443 * otherwise it will be left as-is. Example:
4444 *
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004445 * %define %$abc cde
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004446 *
4447 * the identifier %$abc will be left as-is so that the handler for %define
4448 * will suck it and define the corresponding value. Other case:
4449 *
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004450 * %define _%$abc cde
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004451 *
4452 * In this case user wants name to be expanded *before* %define starts
4453 * working, so we'll expand %$abc into something (if it has a value;
4454 * otherwise it will be left as-is) then concatenate all successive
4455 * PP_IDs into one.
4456 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004457static Token *expand_id(Token * tline)
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004458{
4459 Token *cur, *oldnext = NULL;
4460
H. Peter Anvin734b1882002-04-30 21:01:08 +00004461 if (!tline || !tline->next)
H. Peter Anvine2c80182005-01-15 22:15:51 +00004462 return tline;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004463
4464 cur = tline;
4465 while (cur->next &&
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004466 (cur->next->type == TOK_ID ||
4467 cur->next->type == TOK_PREPROC_ID
4468 || cur->next->type == TOK_NUMBER))
H. Peter Anvine2c80182005-01-15 22:15:51 +00004469 cur = cur->next;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004470
4471 /* If identifier consists of just one token, don't expand */
4472 if (cur == tline)
H. Peter Anvine2c80182005-01-15 22:15:51 +00004473 return tline;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004474
H. Peter Anvine2c80182005-01-15 22:15:51 +00004475 if (cur) {
4476 oldnext = cur->next; /* Detach the tail past identifier */
4477 cur->next = NULL; /* so that expand_smacro stops here */
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004478 }
4479
H. Peter Anvin734b1882002-04-30 21:01:08 +00004480 tline = expand_smacro(tline);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004481
H. Peter Anvine2c80182005-01-15 22:15:51 +00004482 if (cur) {
4483 /* expand_smacro possibly changhed tline; re-scan for EOL */
4484 cur = tline;
4485 while (cur && cur->next)
4486 cur = cur->next;
4487 if (cur)
4488 cur->next = oldnext;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004489 }
4490
4491 return tline;
4492}
4493
4494/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004495 * Determine whether the given line constitutes a multi-line macro
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004496 * call, and return the MMacro structure called if so. Doesn't have
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004497 * to check for an initial label - that's taken care of in
4498 * expand_mmacro - but must check numbers of parameters. Guaranteed
4499 * to be called with tline->type == TOK_ID, so the putative macro
4500 * name is easy to find.
4501 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004502static MMacro *is_mmacro(Token * tline, Token *** params_array)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004503{
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004504 MMacro *head, *m;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004505 Token **params;
4506 int nparam;
4507
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004508 head = (MMacro *) hash_findix(&mmacros, tline->text);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004509
4510 /*
4511 * Efficiency: first we see if any macro exists with the given
4512 * name. If not, we can return NULL immediately. _Then_ we
4513 * count the parameters, and then we look further along the
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004514 * list if necessary to find the proper MMacro.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004515 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004516 list_for_each(m, head)
4517 if (!mstrcmp(m->name, tline->text, m->casesense))
H. Peter Anvine2c80182005-01-15 22:15:51 +00004518 break;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004519 if (!m)
H. Peter Anvine2c80182005-01-15 22:15:51 +00004520 return NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004521
4522 /*
4523 * OK, we have a potential macro. Count and demarcate the
4524 * parameters.
4525 */
H. Peter Anvin734b1882002-04-30 21:01:08 +00004526 count_mmac_params(tline->next, &nparam, &params);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004527
4528 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004529 * So we know how many parameters we've got. Find the MMacro
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004530 * structure that handles this number.
4531 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004532 while (m) {
4533 if (m->nparam_min <= nparam
4534 && (m->plus || nparam <= m->nparam_max)) {
4535 /*
4536 * This one is right. Just check if cycle removal
4537 * prohibits us using it before we actually celebrate...
4538 */
4539 if (m->in_progress > m->max_depth) {
4540 if (m->max_depth > 0) {
H. Peter Anvin (Intel)80c4f232018-12-14 13:33:24 -08004541 nasm_warn(WARN_OTHER, "reached maximum recursion depth of %i",
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03004542 m->max_depth);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004543 }
4544 nasm_free(params);
4545 return NULL;
4546 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00004547 /*
4548 * It's right, and we can use it. Add its default
4549 * parameters to the end of our list if necessary.
4550 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004551 if (m->defaults && nparam < m->nparam_min + m->ndefs) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00004552 params =
4553 nasm_realloc(params,
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004554 ((m->nparam_min + m->ndefs +
H. Peter Anvine2c80182005-01-15 22:15:51 +00004555 1) * sizeof(*params)));
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004556 while (nparam < m->nparam_min + m->ndefs) {
4557 params[nparam] = m->defaults[nparam - m->nparam_min];
H. Peter Anvine2c80182005-01-15 22:15:51 +00004558 nparam++;
4559 }
4560 }
4561 /*
4562 * If we've gone over the maximum parameter count (and
4563 * we're in Plus mode), ignore parameters beyond
4564 * nparam_max.
4565 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004566 if (m->plus && nparam > m->nparam_max)
4567 nparam = m->nparam_max;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004568 /*
4569 * Then terminate the parameter list, and leave.
4570 */
4571 if (!params) { /* need this special case */
4572 params = nasm_malloc(sizeof(*params));
4573 nparam = 0;
4574 }
4575 params[nparam] = NULL;
4576 *params_array = params;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004577 return m;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004578 }
4579 /*
4580 * This one wasn't right: look for the next one with the
4581 * same name.
4582 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004583 list_for_each(m, m->next)
4584 if (!mstrcmp(m->name, tline->text, m->casesense))
H. Peter Anvine2c80182005-01-15 22:15:51 +00004585 break;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004586 }
4587
4588 /*
4589 * After all that, we didn't find one with the right number of
4590 * parameters. Issue a warning, and fail to expand the macro.
4591 */
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08004592 nasm_warn(WARN_MACRO_PARAMS,
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03004593 "macro `%s' exists, but not taking %d parameters",
4594 tline->text, nparam);
H. Peter Anvin734b1882002-04-30 21:01:08 +00004595 nasm_free(params);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004596 return NULL;
4597}
4598
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004599
4600/*
4601 * Save MMacro invocation specific fields in
4602 * preparation for a recursive macro expansion
4603 */
4604static void push_mmacro(MMacro *m)
4605{
4606 MMacroInvocation *i;
4607
4608 i = nasm_malloc(sizeof(MMacroInvocation));
4609 i->prev = m->prev;
4610 i->params = m->params;
4611 i->iline = m->iline;
4612 i->nparam = m->nparam;
4613 i->rotate = m->rotate;
4614 i->paramlen = m->paramlen;
4615 i->unique = m->unique;
4616 i->condcnt = m->condcnt;
4617 m->prev = i;
4618}
4619
4620
4621/*
4622 * Restore MMacro invocation specific fields that were
4623 * saved during a previous recursive macro expansion
4624 */
4625static void pop_mmacro(MMacro *m)
4626{
4627 MMacroInvocation *i;
4628
4629 if (m->prev) {
4630 i = m->prev;
4631 m->prev = i->prev;
4632 m->params = i->params;
4633 m->iline = i->iline;
4634 m->nparam = i->nparam;
4635 m->rotate = i->rotate;
4636 m->paramlen = i->paramlen;
4637 m->unique = i->unique;
4638 m->condcnt = i->condcnt;
4639 nasm_free(i);
4640 }
4641}
4642
4643
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004644/*
4645 * Expand the multi-line macro call made by the given line, if
4646 * there is one to be expanded. If there is, push the expansion on
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004647 * istk->expansion and return 1. Otherwise return 0.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004648 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004649static int expand_mmacro(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004650{
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004651 Token *startline = tline;
H. Peter Anvineba20a72002-04-30 20:53:55 +00004652 Token *label = NULL;
4653 int dont_prepend = 0;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004654 Token **params, *t, *tt;
4655 MMacro *m;
4656 Line *l, *ll;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004657 int i, nparam, *paramlen;
H. Peter Anvinc751e862008-06-09 10:18:45 -07004658 const char *mname;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004659
4660 t = tline;
H. Peter Anvineba20a72002-04-30 20:53:55 +00004661 skip_white_(t);
H. Peter Anvince2233b2008-05-25 21:57:00 -07004662 /* if (!tok_type_(t, TOK_ID)) Lino 02/25/02 */
H. Peter Anvindce1e2f2002-04-30 21:06:37 +00004663 if (!tok_type_(t, TOK_ID) && !tok_type_(t, TOK_PREPROC_ID))
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004664 return 0;
4665 m = is_mmacro(t, &params);
4666 if (m) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004667 mname = t->text;
H. Peter Anvinc751e862008-06-09 10:18:45 -07004668 } else {
H. Peter Anvine2c80182005-01-15 22:15:51 +00004669 Token *last;
4670 /*
4671 * We have an id which isn't a macro call. We'll assume
4672 * it might be a label; we'll also check to see if a
4673 * colon follows it. Then, if there's another id after
4674 * that lot, we'll check it again for macro-hood.
4675 */
4676 label = last = t;
4677 t = t->next;
4678 if (tok_type_(t, TOK_WHITESPACE))
4679 last = t, t = t->next;
4680 if (tok_is_(t, ":")) {
4681 dont_prepend = 1;
4682 last = t, t = t->next;
4683 if (tok_type_(t, TOK_WHITESPACE))
4684 last = t, t = t->next;
4685 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004686 if (!tok_type_(t, TOK_ID) || !(m = is_mmacro(t, &params)))
4687 return 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004688 last->next = NULL;
Keith Kanios891775e2009-07-11 06:08:54 -05004689 mname = t->text;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004690 tline = t;
H. Peter Anvineba20a72002-04-30 20:53:55 +00004691 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004692
4693 /*
4694 * Fix up the parameters: this involves stripping leading and
4695 * trailing whitespace, then stripping braces if they are
4696 * present.
4697 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004698 for (nparam = 0; params[nparam]; nparam++) ;
H. Peter Anvin734b1882002-04-30 21:01:08 +00004699 paramlen = nparam ? nasm_malloc(nparam * sizeof(*paramlen)) : NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004700
H. Peter Anvine2c80182005-01-15 22:15:51 +00004701 for (i = 0; params[i]; i++) {
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08004702 int brace = 0;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004703 int comma = (!m->plus || i < nparam - 1);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004704
H. Peter Anvine2c80182005-01-15 22:15:51 +00004705 t = params[i];
4706 skip_white_(t);
4707 if (tok_is_(t, "{"))
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08004708 t = t->next, brace++, comma = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004709 params[i] = t;
4710 paramlen[i] = 0;
4711 while (t) {
4712 if (comma && t->type == TOK_OTHER && !strcmp(t->text, ","))
4713 break; /* ... because we have hit a comma */
4714 if (comma && t->type == TOK_WHITESPACE
4715 && tok_is_(t->next, ","))
4716 break; /* ... or a space then a comma */
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08004717 if (brace && t->type == TOK_OTHER) {
4718 if (t->text[0] == '{')
4719 brace++; /* ... or a nested opening brace */
4720 else if (t->text[0] == '}')
4721 if (!--brace)
4722 break; /* ... or a brace */
4723 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00004724 t = t->next;
4725 paramlen[i]++;
4726 }
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08004727 if (brace)
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03004728 nasm_nonfatal("macro params should be enclosed in braces");
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004729 }
4730
4731 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004732 * OK, we have a MMacro structure together with a set of
4733 * parameters. We must now go through the expansion and push
4734 * copies of each Line on to istk->expansion. Substitution of
H. Peter Anvin76690a12002-04-30 20:52:49 +00004735 * parameter tokens and macro-local tokens doesn't get done
4736 * until the single-line macro substitution process; this is
4737 * because delaying them allows us to change the semantics
4738 * later through %rotate.
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004739 *
4740 * First, push an end marker on to istk->expansion, mark this
4741 * macro as in progress, and set up its invocation-specific
4742 * variables.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004743 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004744 ll = nasm_malloc(sizeof(Line));
4745 ll->next = istk->expansion;
4746 ll->finishes = m;
4747 ll->first = NULL;
4748 istk->expansion = ll;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004749
4750 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004751 * Save the previous MMacro expansion in the case of
4752 * macro recursion
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004753 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004754 if (m->max_depth && m->in_progress)
4755 push_mmacro(m);
4756
4757 m->in_progress ++;
4758 m->params = params;
4759 m->iline = tline;
4760 m->nparam = nparam;
4761 m->rotate = 0;
4762 m->paramlen = paramlen;
4763 m->unique = unique++;
4764 m->lineno = 0;
4765 m->condcnt = 0;
4766
4767 m->next_active = istk->mstk;
4768 istk->mstk = m;
4769
4770 list_for_each(l, m->expansion) {
4771 Token **tail;
4772
4773 ll = nasm_malloc(sizeof(Line));
4774 ll->finishes = NULL;
4775 ll->next = istk->expansion;
4776 istk->expansion = ll;
4777 tail = &ll->first;
4778
4779 list_for_each(t, l->first) {
4780 Token *x = t;
4781 switch (t->type) {
4782 case TOK_PREPROC_Q:
4783 tt = *tail = new_Token(NULL, TOK_ID, mname, 0);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004784 break;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004785 case TOK_PREPROC_QQ:
4786 tt = *tail = new_Token(NULL, TOK_ID, m->name, 0);
4787 break;
4788 case TOK_PREPROC_ID:
4789 if (t->text[1] == '0' && t->text[2] == '0') {
4790 dont_prepend = -1;
4791 x = label;
4792 if (!x)
4793 continue;
4794 }
4795 /* fall through */
4796 default:
4797 tt = *tail = new_Token(NULL, x->type, x->text, 0);
4798 break;
4799 }
4800 tail = &tt->next;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004801 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004802 *tail = NULL;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004803 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004804
4805 /*
H. Peter Anvineba20a72002-04-30 20:53:55 +00004806 * If we had a label, push it on as the first line of
4807 * the macro expansion.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004808 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004809 if (label) {
4810 if (dont_prepend < 0)
4811 free_tlist(startline);
4812 else {
4813 ll = nasm_malloc(sizeof(Line));
4814 ll->finishes = NULL;
4815 ll->next = istk->expansion;
4816 istk->expansion = ll;
4817 ll->first = startline;
4818 if (!dont_prepend) {
4819 while (label->next)
4820 label = label->next;
4821 label->next = tt = new_Token(NULL, TOK_OTHER, ":", 0);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004822 }
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004823 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004824 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004825
H. Peter Anvin8ac25aa2016-02-18 01:16:18 -08004826 lfmt->uplevel(m->nolist ? LIST_MACRO_NOLIST : LIST_MACRO);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004827
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004828 return 1;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004829}
4830
H. Peter Anvin130736c2016-02-17 20:27:41 -08004831/*
4832 * This function adds macro names to error messages, and suppresses
4833 * them if necessary.
4834 */
H. Peter Anvin (Intel)6bde2ed2018-12-13 19:39:41 -08004835static void pp_verror(errflags severity, const char *fmt, va_list arg)
Victor van den Elzen3b404c02008-09-18 13:51:36 +02004836{
H. Peter Anvin130736c2016-02-17 20:27:41 -08004837 char buff[BUFSIZ];
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004838 MMacro *mmac = NULL;
4839 int delta = 0;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02004840
H. Peter Anvin130736c2016-02-17 20:27:41 -08004841 /*
4842 * If we're in a dead branch of IF or something like it, ignore the error.
4843 * However, because %else etc are evaluated in the state context
4844 * of the previous branch, errors might get lost:
4845 * %if 0 ... %else trailing garbage ... %endif
4846 * So %else etc should set the ERR_PP_PRECOND flag.
4847 */
4848 if ((severity & ERR_MASK) < ERR_FATAL &&
4849 istk && istk->conds &&
4850 ((severity & ERR_PP_PRECOND) ?
4851 istk->conds->state == COND_NEVER :
H. Peter Anvineb6653f2016-04-05 13:03:10 -07004852 !emitting(istk->conds->state)))
H. Peter Anvin130736c2016-02-17 20:27:41 -08004853 return;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02004854
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004855 /* get %macro name */
H. Peter Anvin130736c2016-02-17 20:27:41 -08004856 if (!(severity & ERR_NOFILE) && istk && istk->mstk) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004857 mmac = istk->mstk;
4858 /* but %rep blocks should be skipped */
4859 while (mmac && !mmac->name)
4860 mmac = mmac->next_active, delta++;
Cyrill Gorcunov9900c6b2011-10-09 18:58:46 +04004861 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004862
H. Peter Anvin130736c2016-02-17 20:27:41 -08004863 if (mmac) {
4864 vsnprintf(buff, sizeof(buff), fmt, arg);
Victor van den Elzen3b404c02008-09-18 13:51:36 +02004865
H. Peter Anvin130736c2016-02-17 20:27:41 -08004866 nasm_set_verror(real_verror);
4867 nasm_error(severity, "(%s:%d) %s",
4868 mmac->name, mmac->lineno - delta, buff);
4869 nasm_set_verror(pp_verror);
4870 } else {
4871 real_verror(severity, fmt, arg);
4872 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004873}
4874
H. Peter Anvin734b1882002-04-30 21:01:08 +00004875static void
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08004876pp_reset(const char *file, enum preproc_mode mode, struct strlist *dep_list)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004877{
H. Peter Anvin7383b402008-09-24 10:20:40 -07004878 Token *t;
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08004879 int apass;
H. Peter Anvin7383b402008-09-24 10:20:40 -07004880
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004881 cstk = NULL;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004882 istk = nasm_malloc(sizeof(Include));
4883 istk->next = NULL;
4884 istk->conds = NULL;
4885 istk->expansion = NULL;
4886 istk->mstk = NULL;
H. Peter Anvin3e83cec2016-05-25 04:28:46 -07004887 istk->fp = nasm_open_read(file, NF_TEXT);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004888 istk->fname = NULL;
H. Peter Anvin274cda82016-05-10 02:56:29 -07004889 src_set(0, file);
H. Peter Anvineba20a72002-04-30 20:53:55 +00004890 istk->lineinc = 1;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004891 if (!istk->fp)
Cyrill Gorcunovc3527dd2018-11-24 22:17:47 +03004892 nasm_fatalf(ERR_NOFILE, "unable to open input file `%s'", file);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004893 defining = NULL;
Charles Crayned4200be2008-07-12 16:42:33 -07004894 nested_mac_count = 0;
4895 nested_rep_count = 0;
H. Peter Anvin97a23472007-09-16 17:57:25 -07004896 init_macros();
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004897 unique = 0;
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07004898 deplist = dep_list;
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08004899 pp_mode = mode;
H. Peter Anvinf7606612016-07-13 14:23:48 -07004900
4901 if (tasm_compatible_mode)
4902 pp_add_stdmac(nasm_stdmac_tasm);
4903
4904 pp_add_stdmac(nasm_stdmac_nasm);
4905 pp_add_stdmac(nasm_stdmac_version);
4906
Cyrill Gorcunov15ce78f2017-01-06 20:21:28 +03004907 if (extrastdmac)
4908 pp_add_stdmac(extrastdmac);
4909
H. Peter Anvinf7606612016-07-13 14:23:48 -07004910 stdmacpos = stdmacros[0];
4911 stdmacnext = &stdmacros[1];
4912
H. Peter Anvind2456592008-06-19 15:04:18 -07004913 do_predef = true;
H. Peter Anvin61f130f2008-09-25 15:45:06 -07004914
Cyrill Gorcunovb7bb5ac2018-11-11 21:33:52 +03004915 strlist_add(deplist, file);
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07004916
H. Peter Anvin61f130f2008-09-25 15:45:06 -07004917 /*
4918 * Define the __PASS__ macro. This is defined here unlike
4919 * all the other builtins, because it is special -- it varies between
4920 * passes.
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08004921 *
4922 * 0 = dependencies only
4923 * 1 = preparatory passes
4924 * 2 = final pass
4925 * 3 = preproces only
H. Peter Anvin61f130f2008-09-25 15:45:06 -07004926 */
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08004927 switch (mode) {
4928 case PP_NORMAL:
4929 apass = pass_final() ? 2 : 1;
4930 break;
4931 case PP_DEPS:
4932 apass = 0;
4933 break;
4934 case PP_PREPROC:
4935 apass = 3;
4936 break;
4937 default:
4938 panic();
4939 }
4940
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004941 t = nasm_malloc(sizeof(*t));
4942 t->next = NULL;
H. Peter Anvin61f130f2008-09-25 15:45:06 -07004943 make_tok_num(t, apass);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004944 t->a.mac = NULL;
H. Peter Anvin7383b402008-09-24 10:20:40 -07004945 define_smacro(NULL, "__PASS__", true, 0, t);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004946}
4947
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07004948static void pp_init(void)
4949{
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07004950}
4951
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004952static char *pp_getline(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004953{
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004954 char *line;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004955 Token *tline;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004956
H. Peter Anvin130736c2016-02-17 20:27:41 -08004957 real_verror = nasm_set_verror(pp_verror);
H. Peter Anvin215186f2016-02-17 20:27:41 -08004958
H. Peter Anvine2c80182005-01-15 22:15:51 +00004959 while (1) {
4960 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004961 * Fetch a tokenized line, either from the macro-expansion
H. Peter Anvine2c80182005-01-15 22:15:51 +00004962 * buffer or from the input file.
4963 */
4964 tline = NULL;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004965 while (istk->expansion && istk->expansion->finishes) {
4966 Line *l = istk->expansion;
4967 if (!l->finishes->name && l->finishes->in_progress > 1) {
4968 Line *ll;
H. Peter Anvineba20a72002-04-30 20:53:55 +00004969
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004970 /*
4971 * This is a macro-end marker for a macro with no
4972 * name, which means it's not really a macro at all
4973 * but a %rep block, and the `in_progress' field is
4974 * more than 1, meaning that we still need to
4975 * repeat. (1 means the natural last repetition; 0
4976 * means termination by %exitrep.) We have
4977 * therefore expanded up to the %endrep, and must
4978 * push the whole block on to the expansion buffer
4979 * again. We don't bother to remove the macro-end
4980 * marker: we'd only have to generate another one
4981 * if we did.
4982 */
4983 l->finishes->in_progress--;
4984 list_for_each(l, l->finishes->expansion) {
4985 Token *t, *tt, **tail;
4986
4987 ll = nasm_malloc(sizeof(Line));
4988 ll->next = istk->expansion;
4989 ll->finishes = NULL;
4990 ll->first = NULL;
4991 tail = &ll->first;
4992
4993 list_for_each(t, l->first) {
4994 if (t->text || t->type == TOK_WHITESPACE) {
4995 tt = *tail = new_Token(NULL, t->type, t->text, 0);
4996 tail = &tt->next;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004997 }
4998 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004999
5000 istk->expansion = ll;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005001 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005002 } else {
5003 /*
5004 * Check whether a `%rep' was started and not ended
5005 * within this macro expansion. This can happen and
5006 * should be detected. It's a fatal error because
5007 * I'm too confused to work out how to recover
5008 * sensibly from it.
5009 */
5010 if (defining) {
5011 if (defining->name)
H. Peter Anvinc5136902018-06-15 18:20:17 -07005012 nasm_panic("defining with name in expansion");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005013 else if (istk->mstk->name)
H. Peter Anvinc5136902018-06-15 18:20:17 -07005014 nasm_fatal("`%%rep' without `%%endrep' within"
H. Peter Anvin130736c2016-02-17 20:27:41 -08005015 " expansion of macro `%s'",
5016 istk->mstk->name);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005017 }
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005018
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005019 /*
5020 * FIXME: investigate the relationship at this point between
5021 * istk->mstk and l->finishes
5022 */
5023 {
5024 MMacro *m = istk->mstk;
5025 istk->mstk = m->next_active;
5026 if (m->name) {
5027 /*
5028 * This was a real macro call, not a %rep, and
5029 * therefore the parameter information needs to
5030 * be freed.
5031 */
5032 if (m->prev) {
5033 pop_mmacro(m);
5034 l->finishes->in_progress --;
5035 } else {
5036 nasm_free(m->params);
5037 free_tlist(m->iline);
5038 nasm_free(m->paramlen);
5039 l->finishes->in_progress = 0;
5040 }
Adam Majer91e72402017-07-25 10:42:01 +02005041 }
5042
5043 /*
5044 * FIXME It is incorrect to always free_mmacro here.
5045 * It leads to usage-after-free.
5046 *
5047 * https://bugzilla.nasm.us/show_bug.cgi?id=3392414
5048 */
5049#if 0
5050 else
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005051 free_mmacro(m);
Adam Majer91e72402017-07-25 10:42:01 +02005052#endif
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005053 }
5054 istk->expansion = l->next;
5055 nasm_free(l);
H. Peter Anvin8ac25aa2016-02-18 01:16:18 -08005056 lfmt->downlevel(LIST_MACRO);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005057 }
5058 }
5059 while (1) { /* until we get a line we can use */
5060
5061 if (istk->expansion) { /* from a macro expansion */
5062 char *p;
5063 Line *l = istk->expansion;
5064 if (istk->mstk)
5065 istk->mstk->lineno++;
5066 tline = l->first;
5067 istk->expansion = l->next;
5068 nasm_free(l);
5069 p = detoken(tline, false);
H. Peter Anvin8ac25aa2016-02-18 01:16:18 -08005070 lfmt->line(LIST_MACRO, p);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005071 nasm_free(p);
5072 break;
5073 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00005074 line = read_line();
5075 if (line) { /* from the current input file */
5076 line = prepreproc(line);
Keith Kaniosb7a89542007-04-12 02:40:54 +00005077 tline = tokenize(line);
H. Peter Anvine2c80182005-01-15 22:15:51 +00005078 nasm_free(line);
5079 break;
5080 }
5081 /*
5082 * The current file has ended; work down the istk
5083 */
5084 {
5085 Include *i = istk;
5086 fclose(i->fp);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005087 if (i->conds) {
5088 /* nasm_error can't be conditionally suppressed */
H. Peter Anvinc5136902018-06-15 18:20:17 -07005089 nasm_fatal("expected `%%endif' before end of file");
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005090 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00005091 /* only set line and file name if there's a next node */
H. Peter Anvin274cda82016-05-10 02:56:29 -07005092 if (i->next)
5093 src_set(i->lineno, i->fname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00005094 istk = i->next;
H. Peter Anvin8ac25aa2016-02-18 01:16:18 -08005095 lfmt->downlevel(LIST_INCLUDE);
H. Peter Anvine2c80182005-01-15 22:15:51 +00005096 nasm_free(i);
H. Peter Anvin130736c2016-02-17 20:27:41 -08005097 if (!istk) {
5098 line = NULL;
5099 goto done;
5100 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005101 if (istk->expansion && istk->expansion->finishes)
5102 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005103 }
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005104 }
5105
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005106 /*
5107 * We must expand MMacro parameters and MMacro-local labels
5108 * _before_ we plunge into directive processing, to cope
5109 * with things like `%define something %1' such as STRUC
5110 * uses. Unless we're _defining_ a MMacro, in which case
5111 * those tokens should be left alone to go into the
5112 * definition; and unless we're in a non-emitting
5113 * condition, in which case we don't want to meddle with
5114 * anything.
5115 */
5116 if (!defining && !(istk->conds && !emitting(istk->conds->state))
5117 && !(istk->mstk && !istk->mstk->in_progress)) {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005118 tline = expand_mmac_params(tline);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005119 }
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005120
H. Peter Anvine2c80182005-01-15 22:15:51 +00005121 /*
5122 * Check the line to see if it's a preprocessor directive.
5123 */
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07005124 if (do_directive(tline, &line) == DIRECTIVE_FOUND) {
5125 if (line)
5126 break; /* Directive generated output */
5127 else
5128 continue;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005129 } else if (defining) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00005130 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005131 * We're defining a multi-line macro. We emit nothing
5132 * at all, and just
5133 * shove the tokenized line on to the macro definition.
H. Peter Anvine2c80182005-01-15 22:15:51 +00005134 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005135 Line *l = nasm_malloc(sizeof(Line));
5136 l->next = defining->expansion;
5137 l->first = tline;
5138 l->finishes = NULL;
5139 defining->expansion = l;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005140 continue;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005141 } else if (istk->conds && !emitting(istk->conds->state)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00005142 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005143 * We're in a non-emitting branch of a condition block.
H. Peter Anvine2c80182005-01-15 22:15:51 +00005144 * Emit nothing at all, not even a blank line: when we
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005145 * emerge from the condition we'll give a line-number
H. Peter Anvine2c80182005-01-15 22:15:51 +00005146 * directive so we keep our place correctly.
5147 */
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005148 free_tlist(tline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00005149 continue;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005150 } else if (istk->mstk && !istk->mstk->in_progress) {
5151 /*
5152 * We're in a %rep block which has been terminated, so
5153 * we're walking through to the %endrep without
5154 * emitting anything. Emit nothing at all, not even a
5155 * blank line: when we emerge from the %rep block we'll
5156 * give a line-number directive so we keep our place
5157 * correctly.
5158 */
5159 free_tlist(tline);
5160 continue;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005161 } else {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005162 tline = expand_smacro(tline);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005163 if (!expand_mmacro(tline)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00005164 /*
Keith Kaniosb7a89542007-04-12 02:40:54 +00005165 * De-tokenize the line again, and emit it.
H. Peter Anvine2c80182005-01-15 22:15:51 +00005166 */
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005167 line = detoken(tline, true);
5168 free_tlist(tline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00005169 break;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005170 } else {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005171 continue; /* expand_mmacro calls free_tlist */
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005172 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00005173 }
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005174 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005175
H. Peter Anvin130736c2016-02-17 20:27:41 -08005176done:
5177 nasm_set_verror(real_verror);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005178 return line;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005179}
5180
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08005181static void pp_cleanup_pass(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005182{
H. Peter Anvin130736c2016-02-17 20:27:41 -08005183 real_verror = nasm_set_verror(pp_verror);
H. Peter Anvin215186f2016-02-17 20:27:41 -08005184
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005185 if (defining) {
5186 if (defining->name) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03005187 nasm_nonfatal("end of file while still defining macro `%s'",
5188 defining->name);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005189 } else {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03005190 nasm_nonfatal("end of file while still in %%rep");
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005191 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005192
5193 free_mmacro(defining);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005194 defining = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005195 }
H. Peter Anvin130736c2016-02-17 20:27:41 -08005196
5197 nasm_set_verror(real_verror);
H. Peter Anvin215186f2016-02-17 20:27:41 -08005198
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005199 while (cstk)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005200 ctx_pop();
H. Peter Anvin97a23472007-09-16 17:57:25 -07005201 free_macros();
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005202 while (istk) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00005203 Include *i = istk;
5204 istk = istk->next;
5205 fclose(i->fp);
Cyrill Gorcunov8dcfd882011-03-03 09:18:56 +03005206 nasm_free(i);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005207 }
5208 while (cstk)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005209 ctx_pop();
H. Peter Anvin274cda82016-05-10 02:56:29 -07005210 src_set_fname(NULL);
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08005211}
5212
5213static void pp_cleanup_session(void)
5214{
5215 free_llist(predef);
5216 predef = NULL;
5217 delete_Blocks();
5218 freeTokens = NULL;
5219 ipath_list = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005220}
5221
Cyrill Gorcunov8c0666b2018-11-24 14:33:48 +03005222static void pp_include_path(struct strlist *list)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005223{
Cyrill Gorcunov8c0666b2018-11-24 14:33:48 +03005224 ipath_list = list;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005225}
Frank Kotlerd0ed6fd2003-08-27 11:33:56 +00005226
Cyrill Gorcunov0b78bff2012-05-07 01:57:55 +04005227static void pp_pre_include(char *fname)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005228{
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005229 Token *inc, *space, *name;
5230 Line *l;
5231
H. Peter Anvin734b1882002-04-30 21:01:08 +00005232 name = new_Token(NULL, TOK_INTERNAL_STRING, fname, 0);
5233 space = new_Token(name, TOK_WHITESPACE, NULL, 0);
5234 inc = new_Token(space, TOK_PREPROC_ID, "%include", 0);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005235
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005236 l = nasm_malloc(sizeof(Line));
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005237 l->next = predef;
5238 l->first = inc;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005239 l->finishes = NULL;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005240 predef = l;
5241}
5242
Cyrill Gorcunov0b78bff2012-05-07 01:57:55 +04005243static void pp_pre_define(char *definition)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005244{
5245 Token *def, *space;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005246 Line *l;
Keith Kaniosa6dfa782007-04-13 16:47:53 +00005247 char *equals;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005248
H. Peter Anvin130736c2016-02-17 20:27:41 -08005249 real_verror = nasm_set_verror(pp_verror);
H. Peter Anvin215186f2016-02-17 20:27:41 -08005250
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005251 equals = strchr(definition, '=');
H. Peter Anvin734b1882002-04-30 21:01:08 +00005252 space = new_Token(NULL, TOK_WHITESPACE, NULL, 0);
5253 def = new_Token(space, TOK_PREPROC_ID, "%define", 0);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005254 if (equals)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005255 *equals = ' ';
Keith Kaniosb7a89542007-04-12 02:40:54 +00005256 space->next = tokenize(definition);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005257 if (equals)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005258 *equals = '=';
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005259
Cyrill Gorcunov6d42e9b2015-02-08 11:07:17 +03005260 if (space->next->type != TOK_PREPROC_ID &&
5261 space->next->type != TOK_ID)
H. Peter Anvin (Intel)80c4f232018-12-14 13:33:24 -08005262 nasm_warn(WARN_OTHER, "pre-defining non ID `%s\'\n", definition);
Cyrill Gorcunov6d42e9b2015-02-08 11:07:17 +03005263
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005264 l = nasm_malloc(sizeof(Line));
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005265 l->next = predef;
5266 l->first = def;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005267 l->finishes = NULL;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005268 predef = l;
H. Peter Anvin130736c2016-02-17 20:27:41 -08005269
5270 nasm_set_verror(real_verror);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005271}
5272
Cyrill Gorcunov0b78bff2012-05-07 01:57:55 +04005273static void pp_pre_undefine(char *definition)
H. Peter Anvin620515a2002-04-30 20:57:38 +00005274{
5275 Token *def, *space;
5276 Line *l;
5277
H. Peter Anvin734b1882002-04-30 21:01:08 +00005278 space = new_Token(NULL, TOK_WHITESPACE, NULL, 0);
5279 def = new_Token(space, TOK_PREPROC_ID, "%undef", 0);
Keith Kaniosb7a89542007-04-12 02:40:54 +00005280 space->next = tokenize(definition);
H. Peter Anvin620515a2002-04-30 20:57:38 +00005281
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005282 l = nasm_malloc(sizeof(Line));
H. Peter Anvin620515a2002-04-30 20:57:38 +00005283 l->next = predef;
5284 l->first = def;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005285 l->finishes = NULL;
H. Peter Anvin620515a2002-04-30 20:57:38 +00005286 predef = l;
5287}
5288
H. Peter Anvin05990342018-06-11 13:32:42 -07005289/* Insert an early preprocessor command that doesn't need special handling */
5290static void pp_pre_command(const char *what, char *string)
5291{
5292 char *cmd;
5293 Token *def, *space;
5294 Line *l;
5295
5296 def = tokenize(string);
5297 if (what) {
5298 cmd = nasm_strcat(what[0] == '%' ? "" : "%", what);
5299 space = new_Token(def, TOK_WHITESPACE, NULL, 0);
5300 def = new_Token(space, TOK_PREPROC_ID, cmd, 0);
5301 }
5302
5303 l = nasm_malloc(sizeof(Line));
5304 l->next = predef;
5305 l->first = def;
5306 l->finishes = NULL;
5307 predef = l;
5308}
5309
H. Peter Anvinf7606612016-07-13 14:23:48 -07005310static void pp_add_stdmac(macros_t *macros)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005311{
H. Peter Anvinf7606612016-07-13 14:23:48 -07005312 macros_t **mp;
5313
5314 /* Find the end of the list and avoid duplicates */
5315 for (mp = stdmacros; *mp; mp++) {
5316 if (*mp == macros)
5317 return; /* Nothing to do */
5318 }
5319
5320 nasm_assert(mp < &stdmacros[ARRAY_SIZE(stdmacros)-1]);
5321
5322 *mp = macros;
H. Peter Anvin76690a12002-04-30 20:52:49 +00005323}
5324
Cyrill Gorcunov15ce78f2017-01-06 20:21:28 +03005325static void pp_extra_stdmac(macros_t *macros)
5326{
5327 extrastdmac = macros;
5328}
5329
Keith Kaniosa5fc6462007-10-13 07:09:22 -07005330static void make_tok_num(Token * tok, int64_t val)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005331{
Cyrill Gorcunovce652742013-05-06 23:43:43 +04005332 char numbuf[32];
Keith Kaniosa5fc6462007-10-13 07:09:22 -07005333 snprintf(numbuf, sizeof(numbuf), "%"PRId64"", val);
H. Peter Anvineba20a72002-04-30 20:53:55 +00005334 tok->text = nasm_strdup(numbuf);
5335 tok->type = TOK_NUMBER;
5336}
5337
H. Peter Anvin (Intel)6bde2ed2018-12-13 19:39:41 -08005338static void pp_list_one_macro(MMacro *m, errflags severity)
H. Peter Anvin37368952016-05-09 14:10:32 -07005339{
5340 if (!m)
5341 return;
5342
5343 /* We need to print the next_active list in reverse order */
5344 pp_list_one_macro(m->next_active, severity);
5345
5346 if (m->name && !m->nolist) {
H. Peter Anvin274cda82016-05-10 02:56:29 -07005347 src_set(m->xline + m->lineno, m->fname);
H. Peter Anvinddb29062018-12-11 00:06:29 -08005348 nasm_error(severity, "... from macro `%s' defined", m->name);
H. Peter Anvin37368952016-05-09 14:10:32 -07005349 }
5350}
5351
H. Peter Anvin (Intel)6bde2ed2018-12-13 19:39:41 -08005352static void pp_error_list_macros(errflags severity)
H. Peter Anvin4def1a82016-05-09 13:59:44 -07005353{
H. Peter Anvinddb29062018-12-11 00:06:29 -08005354 struct src_location saved;
H. Peter Anvin4def1a82016-05-09 13:59:44 -07005355
H. Peter Anvinddb29062018-12-11 00:06:29 -08005356 severity |= ERR_PP_LISTMACRO | ERR_NO_SEVERITY | ERR_HERE;
5357 saved = src_where();
H. Peter Anvin4def1a82016-05-09 13:59:44 -07005358
Cyrill Gorcunov771d04e2016-05-10 23:27:03 +03005359 if (istk)
5360 pp_list_one_macro(istk->mstk, severity);
H. Peter Anvin4def1a82016-05-09 13:59:44 -07005361
H. Peter Anvinddb29062018-12-11 00:06:29 -08005362 src_update(saved);
H. Peter Anvin4def1a82016-05-09 13:59:44 -07005363}
5364
H. Peter Anvine7469712016-02-18 02:20:59 -08005365const struct preproc_ops nasmpp = {
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07005366 pp_init,
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005367 pp_reset,
5368 pp_getline,
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08005369 pp_cleanup_pass,
5370 pp_cleanup_session,
Cyrill Gorcunov15ce78f2017-01-06 20:21:28 +03005371 pp_extra_stdmac,
Cyrill Gorcunov0b78bff2012-05-07 01:57:55 +04005372 pp_pre_define,
5373 pp_pre_undefine,
5374 pp_pre_include,
H. Peter Anvin05990342018-06-11 13:32:42 -07005375 pp_pre_command,
H. Peter Anvin4def1a82016-05-09 13:59:44 -07005376 pp_include_path,
5377 pp_error_list_macros,
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005378};