blob: 5f6b0a628fe542e68a94a3d82e2f235d9ccad04c [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 Anvin077fb932010-07-20 14:56:30 -0700465 * nasm_unquote with error if the string contains NUL characters.
466 * If the string contains NUL characters, issue an error and return
467 * the C len, i.e. truncate at the NUL.
468 */
469static size_t nasm_unquote_cstr(char *qstr, enum preproc_token directive)
470{
471 size_t len = nasm_unquote(qstr, NULL);
472 size_t clen = strlen(qstr);
473
474 if (len != clen)
Cyrill Gorcunov295b7952018-11-25 12:55:48 +0300475 nasm_nonfatal("NUL character in `%s' directive",
476 pp_directives[directive]);
H. Peter Anvin077fb932010-07-20 14:56:30 -0700477 return clen;
478}
479
480/*
H. Peter Anvinb40992c2010-09-15 08:57:21 -0700481 * In-place reverse a list of tokens.
482 */
483static Token *reverse_tokens(Token *t)
484{
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800485 Token *prev = NULL;
486 Token *next;
H. Peter Anvinb40992c2010-09-15 08:57:21 -0700487
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800488 while (t) {
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +0400489 next = t->next;
490 t->next = prev;
491 prev = t;
492 t = next;
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800493 }
H. Peter Anvinb40992c2010-09-15 08:57:21 -0700494
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800495 return prev;
H. Peter Anvinb40992c2010-09-15 08:57:21 -0700496}
497
498/*
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300499 * Handle TASM specific directives, which do not contain a % in
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000500 * front of them. We do it here because I could not find any other
501 * place to do it for the moment, and it is a hack (ideally it would
502 * be nice to be able to use the NASM pre-processor to do it).
503 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000504static char *check_tasm_directive(char *line)
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000505{
Keith Kaniosb7a89542007-04-12 02:40:54 +0000506 int32_t i, j, k, m, len;
Cyrill Gorcunovf66ac7d2009-10-12 20:41:13 +0400507 char *p, *q, *oldline, oldchar;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000508
Cyrill Gorcunovf66ac7d2009-10-12 20:41:13 +0400509 p = nasm_skip_spaces(line);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000510
511 /* Binary search for the directive name */
512 i = -1;
Cyrill Gorcunova7319242010-06-03 22:04:36 +0400513 j = ARRAY_SIZE(tasm_directives);
Cyrill Gorcunovf66ac7d2009-10-12 20:41:13 +0400514 q = nasm_skip_word(p);
515 len = q - p;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000516 if (len) {
517 oldchar = p[len];
518 p[len] = 0;
519 while (j - i > 1) {
520 k = (j + i) / 2;
521 m = nasm_stricmp(p, tasm_directives[k]);
522 if (m == 0) {
523 /* We have found a directive, so jam a % in front of it
524 * so that NASM will then recognise it as one if it's own.
525 */
526 p[len] = oldchar;
527 len = strlen(p);
528 oldline = line;
529 line = nasm_malloc(len + 2);
530 line[0] = '%';
531 if (k == TM_IFDIFI) {
H. Peter Anvin18f48792009-06-27 15:56:27 -0700532 /*
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300533 * NASM does not recognise IFDIFI, so we convert
534 * it to %if 0. This is not used in NASM
535 * compatible code, but does need to parse for the
536 * TASM macro package.
H. Peter Anvine2c80182005-01-15 22:15:51 +0000537 */
H. Peter Anvin18f48792009-06-27 15:56:27 -0700538 strcpy(line + 1, "if 0");
H. Peter Anvine2c80182005-01-15 22:15:51 +0000539 } else {
540 memcpy(line + 1, p, len + 1);
541 }
542 nasm_free(oldline);
543 return line;
544 } else if (m < 0) {
545 j = k;
546 } else
547 i = k;
548 }
549 p[len] = oldchar;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000550 }
551 return line;
552}
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000553
H. Peter Anvin76690a12002-04-30 20:52:49 +0000554/*
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000555 * The pre-preprocessing stage... This function translates line
556 * number indications as they emerge from GNU cpp (`# lineno "file"
557 * flags') into NASM preprocessor line number indications (`%line
558 * lineno file').
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000559 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000560static char *prepreproc(char *line)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000561{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000562 int lineno, fnlen;
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000563 char *fname, *oldline;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000564
H. Peter Anvine2c80182005-01-15 22:15:51 +0000565 if (line[0] == '#' && line[1] == ' ') {
566 oldline = line;
567 fname = oldline + 2;
568 lineno = atoi(fname);
569 fname += strspn(fname, "0123456789 ");
570 if (*fname == '"')
571 fname++;
572 fnlen = strcspn(fname, "\"");
573 line = nasm_malloc(20 + fnlen);
574 snprintf(line, 20 + fnlen, "%%line %d %.*s", lineno, fnlen, fname);
575 nasm_free(oldline);
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000576 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000577 if (tasm_compatible_mode)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000578 return check_tasm_directive(line);
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000579 return line;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000580}
581
582/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000583 * Free a linked list of tokens.
584 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000585static void free_tlist(Token * list)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000586{
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +0400587 while (list)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000588 list = delete_Token(list);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000589}
590
591/*
592 * Free a linked list of lines.
593 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000594static void free_llist(Line * list)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000595{
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +0400596 Line *l, *tmp;
597 list_for_each_safe(l, tmp, list) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000598 free_tlist(l->first);
599 nasm_free(l);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000600 }
601}
602
603/*
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800604 * Free an MMacro
H. Peter Anvineba20a72002-04-30 20:53:55 +0000605 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800606static void free_mmacro(MMacro * m)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000607{
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800608 nasm_free(m->name);
609 free_tlist(m->dlist);
610 nasm_free(m->defaults);
611 free_llist(m->expansion);
612 nasm_free(m);
H. Peter Anvineba20a72002-04-30 20:53:55 +0000613}
614
615/*
H. Peter Anvin97a23472007-09-16 17:57:25 -0700616 * Free all currently defined macros, and free the hash tables
617 */
H. Peter Anvin072771e2008-05-22 13:17:51 -0700618static void free_smacro_table(struct hash_table *smt)
H. Peter Anvin97a23472007-09-16 17:57:25 -0700619{
H. Peter Anvin (Intel)ebb05a02018-12-11 12:30:25 -0800620 struct hash_iterator it;
621 const struct hash_node *np;
H. Peter Anvin97a23472007-09-16 17:57:25 -0700622
H. Peter Anvin (Intel)ebb05a02018-12-11 12:30:25 -0800623 hash_for_each(smt, it, np) {
624 SMacro *tmp;
625 SMacro *s = np->data;
626 nasm_free((void *)np->key);
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +0400627 list_for_each_safe(s, tmp, s) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300628 nasm_free(s->name);
629 free_tlist(s->expansion);
630 nasm_free(s);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300631 }
H. Peter Anvin97a23472007-09-16 17:57:25 -0700632 }
H. Peter Anvin072771e2008-05-22 13:17:51 -0700633 hash_free(smt);
634}
635
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800636static void free_mmacro_table(struct hash_table *mmt)
H. Peter Anvin072771e2008-05-22 13:17:51 -0700637{
H. Peter Anvin (Intel)ebb05a02018-12-11 12:30:25 -0800638 struct hash_iterator it;
639 const struct hash_node *np;
H. Peter Anvin97a23472007-09-16 17:57:25 -0700640
H. Peter Anvin (Intel)ebb05a02018-12-11 12:30:25 -0800641 hash_for_each(mmt, it, np) {
642 MMacro *tmp;
643 MMacro *m = np->data;
644 nasm_free((void *)np->key);
645 list_for_each_safe(m, tmp, m)
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800646 free_mmacro(m);
H. Peter Anvin97a23472007-09-16 17:57:25 -0700647 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800648 hash_free(mmt);
H. Peter Anvin072771e2008-05-22 13:17:51 -0700649}
650
651static void free_macros(void)
652{
H. Peter Anvin166c2472008-05-28 12:28:58 -0700653 free_smacro_table(&smacros);
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800654 free_mmacro_table(&mmacros);
H. Peter Anvin97a23472007-09-16 17:57:25 -0700655}
656
657/*
658 * Initialize the hash tables
659 */
660static void init_macros(void)
661{
H. Peter Anvin97a23472007-09-16 17:57:25 -0700662}
663
664/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000665 * Pop the context stack.
666 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000667static void ctx_pop(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000668{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000669 Context *c = cstk;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000670
671 cstk = cstk->next;
H. Peter Anvin166c2472008-05-28 12:28:58 -0700672 free_smacro_table(&c->localmac);
H. Peter Anvin734b1882002-04-30 21:01:08 +0000673 nasm_free(c->name);
674 nasm_free(c);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000675}
676
H. Peter Anvin072771e2008-05-22 13:17:51 -0700677/*
678 * Search for a key in the hash index; adding it if necessary
679 * (in which case we initialize the data pointer to NULL.)
680 */
681static void **
682hash_findi_add(struct hash_table *hash, const char *str)
683{
684 struct hash_insert hi;
685 void **r;
686 char *strx;
H. Peter Anvin (Intel)ebb05a02018-12-11 12:30:25 -0800687 size_t l = strlen(str) + 1;
H. Peter Anvin072771e2008-05-22 13:17:51 -0700688
H. Peter Anvin (Intel)ebb05a02018-12-11 12:30:25 -0800689 r = hash_findib(hash, str, l, &hi);
H. Peter Anvin072771e2008-05-22 13:17:51 -0700690 if (r)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300691 return r;
H. Peter Anvin072771e2008-05-22 13:17:51 -0700692
H. Peter Anvin (Intel)ebb05a02018-12-11 12:30:25 -0800693 strx = nasm_malloc(l); /* Use a more efficient allocator here? */
694 memcpy(strx, str, l);
H. Peter Anvin072771e2008-05-22 13:17:51 -0700695 return hash_add(&hi, strx, NULL);
696}
697
698/*
699 * Like hash_findi, but returns the data element rather than a pointer
700 * to it. Used only when not adding a new element, hence no third
701 * argument.
702 */
703static void *
704hash_findix(struct hash_table *hash, const char *str)
705{
706 void **p;
707
708 p = hash_findi(hash, str, NULL);
709 return p ? *p : NULL;
710}
711
Cyrill Gorcunov15bdc512010-07-13 11:27:41 +0400712/*
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800713 * read line from standart macros set,
Cyrill Gorcunov15bdc512010-07-13 11:27:41 +0400714 * if there no more left -- return NULL
715 */
716static char *line_from_stdmac(void)
717{
718 unsigned char c;
719 const unsigned char *p = stdmacpos;
720 char *line, *q;
721 size_t len = 0;
722
723 if (!stdmacpos)
724 return NULL;
725
726 while ((c = *p++)) {
727 if (c >= 0x80)
728 len += pp_directives_len[c - 0x80] + 1;
729 else
730 len++;
731 }
732
733 line = nasm_malloc(len + 1);
734 q = line;
735 while ((c = *stdmacpos++)) {
736 if (c >= 0x80) {
737 memcpy(q, pp_directives[c - 0x80], pp_directives_len[c - 0x80]);
738 q += pp_directives_len[c - 0x80];
739 *q++ = ' ';
740 } else {
741 *q++ = c;
742 }
743 }
744 stdmacpos = p;
745 *q = '\0';
746
747 if (!*stdmacpos) {
H. Peter Anvinf7606612016-07-13 14:23:48 -0700748 /* This was the last of this particular macro set */
Cyrill Gorcunov15bdc512010-07-13 11:27:41 +0400749 stdmacpos = NULL;
H. Peter Anvinf7606612016-07-13 14:23:48 -0700750 if (*stdmacnext) {
751 stdmacpos = *stdmacnext++;
Cyrill Gorcunov15bdc512010-07-13 11:27:41 +0400752 } else if (do_predef) {
753 Line *pd, *l;
754 Token *head, **tail, *t;
755
756 /*
757 * Nasty hack: here we push the contents of
758 * `predef' on to the top-level expansion stack,
759 * since this is the most convenient way to
760 * implement the pre-include and pre-define
761 * features.
762 */
763 list_for_each(pd, predef) {
764 head = NULL;
765 tail = &head;
766 list_for_each(t, pd->first) {
767 *tail = new_Token(NULL, t->type, t->text, 0);
768 tail = &(*tail)->next;
769 }
770
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800771 l = nasm_malloc(sizeof(Line));
772 l->next = istk->expansion;
773 l->first = head;
774 l->finishes = NULL;
775
776 istk->expansion = l;
Cyrill Gorcunov15bdc512010-07-13 11:27:41 +0400777 }
778 do_predef = false;
779 }
780 }
781
782 return line;
783}
784
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000785static char *read_line(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000786{
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +0400787 unsigned int size, c, next;
788 const unsigned int delta = 512;
789 const unsigned int pad = 8;
790 unsigned int nr_cont = 0;
791 bool cont = false;
792 char *buffer, *p;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000793
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +0400794 /* Standart macros set (predefined) goes first */
Cyrill Gorcunov15bdc512010-07-13 11:27:41 +0400795 p = line_from_stdmac();
796 if (p)
797 return p;
H. Peter Anvin72edbb82008-06-19 16:00:04 -0700798
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +0400799 size = delta;
800 p = buffer = nasm_malloc(size);
801
802 for (;;) {
803 c = fgetc(istk->fp);
804 if ((int)(c) == EOF) {
805 p[0] = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000806 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000807 }
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +0400808
809 switch (c) {
810 case '\r':
811 next = fgetc(istk->fp);
812 if (next != '\n')
813 ungetc(next, istk->fp);
814 if (cont) {
815 cont = false;
816 continue;
817 }
818 break;
819
820 case '\n':
821 if (cont) {
822 cont = false;
823 continue;
824 }
825 break;
826
827 case '\\':
828 next = fgetc(istk->fp);
829 ungetc(next, istk->fp);
Cyrill Gorcunov490f85e2012-12-27 20:02:17 +0400830 if (next == '\r' || next == '\n') {
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +0400831 cont = true;
832 nr_cont++;
833 continue;
834 }
835 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000836 }
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +0400837
838 if (c == '\r' || c == '\n') {
839 *p++ = 0;
840 break;
841 }
842
843 if (p >= (buffer + size - pad)) {
844 buffer = nasm_realloc(buffer, size + delta);
845 p = buffer + size - pad;
846 size += delta;
847 }
848
849 *p++ = (unsigned char)c;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000850 }
851
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +0400852 if (p == buffer) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000853 nasm_free(buffer);
854 return NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000855 }
856
Cyrill Gorcunova5aea572010-11-11 10:14:45 +0300857 src_set_linnum(src_get_linnum() + istk->lineinc +
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +0400858 (nr_cont * istk->lineinc));
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000859
860 /*
861 * Handle spurious ^Z, which may be inserted into source files
862 * by some file transfer utilities.
863 */
864 buffer[strcspn(buffer, "\032")] = '\0';
865
H. Peter Anvin8ac25aa2016-02-18 01:16:18 -0800866 lfmt->line(LIST_READ, buffer);
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000867
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000868 return buffer;
869}
870
871/*
Keith Kaniosb7a89542007-04-12 02:40:54 +0000872 * Tokenize a line of text. This is a very simple process since we
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000873 * don't need to parse the value out of e.g. numeric tokens: we
874 * simply split one string into many.
875 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000876static Token *tokenize(char *line)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000877{
H. Peter Anvinca544db2008-10-19 19:30:11 -0700878 char c, *p = line;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +0000879 enum pp_token_type type;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000880 Token *list = NULL;
881 Token *t, **tail = &list;
882
H. Peter Anvine2c80182005-01-15 22:15:51 +0000883 while (*line) {
884 p = line;
885 if (*p == '%') {
886 p++;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300887 if (*p == '+' && !nasm_isdigit(p[1])) {
888 p++;
889 type = TOK_PASTE;
890 } else if (nasm_isdigit(*p) ||
891 ((*p == '-' || *p == '+') && nasm_isdigit(p[1]))) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000892 do {
893 p++;
894 }
H. Peter Anvinbda7a6e2008-06-21 10:23:17 -0700895 while (nasm_isdigit(*p));
H. Peter Anvine2c80182005-01-15 22:15:51 +0000896 type = TOK_PREPROC_ID;
897 } else if (*p == '{') {
898 p++;
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800899 while (*p) {
900 if (*p == '}')
901 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000902 p[-1] = *p;
903 p++;
904 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800905 if (*p != '}')
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -0800906 nasm_warn(WARN_OTHER, "unterminated %%{ construct");
H. Peter Anvine2c80182005-01-15 22:15:51 +0000907 p[-1] = '\0';
908 if (*p)
909 p++;
910 type = TOK_PREPROC_ID;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300911 } else if (*p == '[') {
912 int lvl = 1;
913 line += 2; /* Skip the leading %[ */
914 p++;
915 while (lvl && (c = *p++)) {
916 switch (c) {
917 case ']':
918 lvl--;
919 break;
920 case '%':
921 if (*p == '[')
922 lvl++;
923 break;
924 case '\'':
925 case '\"':
926 case '`':
Cyrill Gorcunov3144e842017-10-22 10:50:55 +0300927 p = nasm_skip_string(p - 1);
928 if (*p)
929 p++;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300930 break;
931 default:
932 break;
933 }
934 }
935 p--;
936 if (*p)
937 *p++ = '\0';
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800938 if (lvl)
Cyrill Gorcunov295b7952018-11-25 12:55:48 +0300939 nasm_nonfatalf(ERR_PASS1, "unterminated %%[ construct");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300940 type = TOK_INDIRECT;
941 } else if (*p == '?') {
942 type = TOK_PREPROC_Q; /* %? */
943 p++;
944 if (*p == '?') {
945 type = TOK_PREPROC_QQ; /* %?? */
946 p++;
947 }
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +0400948 } else if (*p == '!') {
949 type = TOK_PREPROC_ID;
950 p++;
H. Peter Anvin13506202018-11-28 14:55:58 -0800951 if (nasm_isidchar(*p)) {
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +0400952 do {
953 p++;
954 }
H. Peter Anvin13506202018-11-28 14:55:58 -0800955 while (nasm_isidchar(*p));
H. Peter Anvin53e2e4c2018-11-28 15:01:40 -0800956 } else if (nasm_isquote(*p)) {
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +0400957 p = nasm_skip_string(p);
958 if (*p)
959 p++;
960 else
Cyrill Gorcunov295b7952018-11-25 12:55:48 +0300961 nasm_nonfatalf(ERR_PASS1, "unterminated %%! string");
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +0400962 } else {
963 /* %! without string or identifier */
964 type = TOK_OTHER; /* Legacy behavior... */
965 }
H. Peter Anvin13506202018-11-28 14:55:58 -0800966 } else if (nasm_isidchar(*p) ||
H. Peter Anvine2c80182005-01-15 22:15:51 +0000967 ((*p == '!' || *p == '%' || *p == '$') &&
H. Peter Anvin13506202018-11-28 14:55:58 -0800968 nasm_isidchar(p[1]))) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000969 do {
970 p++;
971 }
H. Peter Anvin13506202018-11-28 14:55:58 -0800972 while (nasm_isidchar(*p));
H. Peter Anvine2c80182005-01-15 22:15:51 +0000973 type = TOK_PREPROC_ID;
974 } else {
975 type = TOK_OTHER;
976 if (*p == '%')
977 p++;
978 }
H. Peter Anvin13506202018-11-28 14:55:58 -0800979 } else if (nasm_isidstart(*p) || (*p == '$' && nasm_isidstart(p[1]))) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000980 type = TOK_ID;
981 p++;
H. Peter Anvin13506202018-11-28 14:55:58 -0800982 while (*p && nasm_isidchar(*p))
H. Peter Anvine2c80182005-01-15 22:15:51 +0000983 p++;
H. Peter Anvin53e2e4c2018-11-28 15:01:40 -0800984 } else if (nasm_isquote(*p)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000985 /*
986 * A string token.
987 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000988 type = TOK_STRING;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300989 p = nasm_skip_string(p);
Nickolay Yurchenkof3b3ce22003-09-21 20:38:43 +0000990
H. Peter Anvine2c80182005-01-15 22:15:51 +0000991 if (*p) {
992 p++;
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800993 } else {
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -0800994 nasm_warn(WARN_OTHER, "unterminated string");
H. Peter Anvine2c80182005-01-15 22:15:51 +0000995 /* Handling unterminated strings by UNV */
996 /* type = -1; */
997 }
Victor van den Elzenfb5f2512009-04-17 16:17:59 +0200998 } else if (p[0] == '$' && p[1] == '$') {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300999 type = TOK_OTHER; /* TOKEN_BASE */
Victor van den Elzenfb5f2512009-04-17 16:17:59 +02001000 p += 2;
H. Peter Anvin13506202018-11-28 14:55:58 -08001001 } else if (nasm_isnumstart(*p)) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001002 bool is_hex = false;
1003 bool is_float = false;
1004 bool has_e = false;
1005 char c, *r;
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001006
H. Peter Anvine2c80182005-01-15 22:15:51 +00001007 /*
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001008 * A numeric token.
H. Peter Anvine2c80182005-01-15 22:15:51 +00001009 */
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001010
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001011 if (*p == '$') {
1012 p++;
1013 is_hex = true;
1014 }
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001015
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001016 for (;;) {
1017 c = *p++;
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001018
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001019 if (!is_hex && (c == 'e' || c == 'E')) {
1020 has_e = true;
1021 if (*p == '+' || *p == '-') {
1022 /*
1023 * e can only be followed by +/- if it is either a
1024 * prefixed hex number or a floating-point number
1025 */
1026 p++;
1027 is_float = true;
1028 }
1029 } else if (c == 'H' || c == 'h' || c == 'X' || c == 'x') {
1030 is_hex = true;
1031 } else if (c == 'P' || c == 'p') {
1032 is_float = true;
1033 if (*p == '+' || *p == '-')
1034 p++;
H. Peter Anvin13506202018-11-28 14:55:58 -08001035 } else if (nasm_isnumchar(c))
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001036 ; /* just advance */
1037 else if (c == '.') {
1038 /*
1039 * we need to deal with consequences of the legacy
1040 * parser, like "1.nolist" being two tokens
1041 * (TOK_NUMBER, TOK_ID) here; at least give it
1042 * a shot for now. In the future, we probably need
1043 * a flex-based scanner with proper pattern matching
1044 * to do it as well as it can be done. Nothing in
1045 * the world is going to help the person who wants
1046 * 0x123.p16 interpreted as two tokens, though.
1047 */
1048 r = p;
1049 while (*r == '_')
1050 r++;
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001051
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001052 if (nasm_isdigit(*r) || (is_hex && nasm_isxdigit(*r)) ||
1053 (!is_hex && (*r == 'e' || *r == 'E')) ||
1054 (*r == 'p' || *r == 'P')) {
1055 p = r;
1056 is_float = true;
1057 } else
1058 break; /* Terminate the token */
1059 } else
1060 break;
1061 }
1062 p--; /* Point to first character beyond number */
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001063
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001064 if (p == line+1 && *line == '$') {
1065 type = TOK_OTHER; /* TOKEN_HERE */
1066 } else {
1067 if (has_e && !is_hex) {
1068 /* 1e13 is floating-point, but 1e13h is not */
1069 is_float = true;
1070 }
H. Peter Anvind784a082009-04-20 14:01:18 -07001071
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001072 type = is_float ? TOK_FLOAT : TOK_NUMBER;
1073 }
H. Peter Anvinbda7a6e2008-06-21 10:23:17 -07001074 } else if (nasm_isspace(*p)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001075 type = TOK_WHITESPACE;
Cyrill Gorcunovf66ac7d2009-10-12 20:41:13 +04001076 p = nasm_skip_spaces(p);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001077 /*
1078 * Whitespace just before end-of-line is discarded by
1079 * pretending it's a comment; whitespace just before a
1080 * comment gets lumped into the comment.
1081 */
1082 if (!*p || *p == ';') {
1083 type = TOK_COMMENT;
1084 while (*p)
1085 p++;
1086 }
1087 } else if (*p == ';') {
1088 type = TOK_COMMENT;
1089 while (*p)
1090 p++;
1091 } else {
1092 /*
1093 * Anything else is an operator of some kind. We check
1094 * for all the double-character operators (>>, <<, //,
1095 * %%, <=, >=, ==, !=, <>, &&, ||, ^^), but anything
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001096 * else is a single-character operator.
H. Peter Anvine2c80182005-01-15 22:15:51 +00001097 */
1098 type = TOK_OTHER;
1099 if ((p[0] == '>' && p[1] == '>') ||
1100 (p[0] == '<' && p[1] == '<') ||
1101 (p[0] == '/' && p[1] == '/') ||
1102 (p[0] == '<' && p[1] == '=') ||
1103 (p[0] == '>' && p[1] == '=') ||
1104 (p[0] == '=' && p[1] == '=') ||
1105 (p[0] == '!' && p[1] == '=') ||
1106 (p[0] == '<' && p[1] == '>') ||
1107 (p[0] == '&' && p[1] == '&') ||
1108 (p[0] == '|' && p[1] == '|') ||
1109 (p[0] == '^' && p[1] == '^')) {
1110 p++;
1111 }
1112 p++;
1113 }
Nickolay Yurchenkof3b3ce22003-09-21 20:38:43 +00001114
H. Peter Anvine2c80182005-01-15 22:15:51 +00001115 /* Handling unterminated string by UNV */
1116 /*if (type == -1)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001117 {
1118 *tail = t = new_Token(NULL, TOK_STRING, line, p-line+1);
1119 t->text[p-line] = *line;
1120 tail = &t->next;
1121 }
1122 else */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001123 if (type != TOK_COMMENT) {
1124 *tail = t = new_Token(NULL, type, line, p - line);
1125 tail = &t->next;
1126 }
1127 line = p;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001128 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001129 return list;
1130}
1131
H. Peter Anvince616072002-04-30 21:02:23 +00001132/*
1133 * this function allocates a new managed block of memory and
H. Peter Anvin70653092007-10-19 14:42:29 -07001134 * returns a pointer to the block. The managed blocks are
H. Peter Anvince616072002-04-30 21:02:23 +00001135 * deleted only all at once by the delete_Blocks function.
1136 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001137static void *new_Block(size_t size)
H. Peter Anvince616072002-04-30 21:02:23 +00001138{
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001139 Blocks *b = &blocks;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001140
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001141 /* first, get to the end of the linked list */
1142 while (b->next)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001143 b = b->next;
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001144 /* now allocate the requested chunk */
1145 b->chunk = nasm_malloc(size);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001146
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001147 /* now allocate a new block for the next request */
Cyrill Gorcunovc31767c2014-06-28 02:22:17 +04001148 b->next = nasm_zalloc(sizeof(Blocks));
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001149 return b->chunk;
H. Peter Anvince616072002-04-30 21:02:23 +00001150}
1151
1152/*
1153 * this function deletes all managed blocks of memory
1154 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001155static void delete_Blocks(void)
H. Peter Anvince616072002-04-30 21:02:23 +00001156{
H. Peter Anvine2c80182005-01-15 22:15:51 +00001157 Blocks *a, *b = &blocks;
H. Peter Anvince616072002-04-30 21:02:23 +00001158
H. Peter Anvin70653092007-10-19 14:42:29 -07001159 /*
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001160 * keep in mind that the first block, pointed to by blocks
H. Peter Anvin70653092007-10-19 14:42:29 -07001161 * is a static and not dynamically allocated, so we don't
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001162 * free it.
1163 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001164 while (b) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001165 if (b->chunk)
1166 nasm_free(b->chunk);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001167 a = b;
1168 b = b->next;
1169 if (a != &blocks)
1170 nasm_free(a);
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001171 }
Cyrill Gorcunovdae24d72014-06-28 10:17:39 +04001172 memset(&blocks, 0, sizeof(blocks));
H. Peter Anvine2c80182005-01-15 22:15:51 +00001173}
H. Peter Anvin734b1882002-04-30 21:01:08 +00001174
1175/*
H. Peter Anvin70653092007-10-19 14:42:29 -07001176 * this function creates a new Token and passes a pointer to it
H. Peter Anvin734b1882002-04-30 21:01:08 +00001177 * back to the caller. It sets the type and text elements, and
H. Peter Anvinf26e0972008-07-01 21:26:27 -07001178 * also the a.mac and next elements to NULL.
H. Peter Anvin734b1882002-04-30 21:01:08 +00001179 */
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07001180static Token *new_Token(Token * next, enum pp_token_type type,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001181 const char *text, int txtlen)
H. Peter Anvin734b1882002-04-30 21:01:08 +00001182{
1183 Token *t;
1184 int i;
1185
H. Peter Anvin89cee572009-07-15 09:16:54 -04001186 if (!freeTokens) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001187 freeTokens = (Token *) new_Block(TOKEN_BLOCKSIZE * sizeof(Token));
1188 for (i = 0; i < TOKEN_BLOCKSIZE - 1; i++)
1189 freeTokens[i].next = &freeTokens[i + 1];
1190 freeTokens[i].next = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001191 }
1192 t = freeTokens;
1193 freeTokens = t->next;
1194 t->next = next;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07001195 t->a.mac = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001196 t->type = type;
H. Peter Anvin89cee572009-07-15 09:16:54 -04001197 if (type == TOK_WHITESPACE || !text) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001198 t->text = NULL;
1199 } else {
1200 if (txtlen == 0)
1201 txtlen = strlen(text);
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07001202 t->text = nasm_malloc(txtlen+1);
1203 memcpy(t->text, text, txtlen);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001204 t->text[txtlen] = '\0';
H. Peter Anvin734b1882002-04-30 21:01:08 +00001205 }
1206 return t;
1207}
1208
H. Peter Anvine2c80182005-01-15 22:15:51 +00001209static Token *delete_Token(Token * t)
H. Peter Anvin734b1882002-04-30 21:01:08 +00001210{
1211 Token *next = t->next;
1212 nasm_free(t->text);
H. Peter Anvin788e6c12002-04-30 21:02:01 +00001213 t->next = freeTokens;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001214 freeTokens = t;
1215 return next;
1216}
1217
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001218/*
1219 * Convert a line of tokens back into text.
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001220 * If expand_locals is not zero, identifiers of the form "%$*xxx"
1221 * will be transformed into ..@ctxnum.xxx
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001222 */
H. Peter Anvin9e200162008-06-04 17:23:14 -07001223static char *detoken(Token * tlist, bool expand_locals)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001224{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001225 Token *t;
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001226 char *line, *p;
H. Peter Anvinb4daadc2008-01-21 16:31:57 -08001227 const char *q;
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001228 int len = 0;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001229
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001230 list_for_each(t, tlist) {
Cyrill Gorcunov9b7ee092017-10-22 21:42:59 +03001231 if (t->type == TOK_PREPROC_ID && t->text &&
1232 t->text[0] && t->text[1] == '!') {
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001233 char *v;
1234 char *q = t->text;
H. Peter Anvin077fb932010-07-20 14:56:30 -07001235
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001236 v = t->text + 2;
H. Peter Anvin53e2e4c2018-11-28 15:01:40 -08001237 if (nasm_isquote(*v)) {
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001238 size_t len = nasm_unquote(v, NULL);
1239 size_t clen = strlen(v);
H. Peter Anvin077fb932010-07-20 14:56:30 -07001240
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001241 if (len != clen) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03001242 nasm_nonfatalf(ERR_PASS1, "NUL character in %%! string");
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001243 v = NULL;
1244 }
1245 }
H. Peter Anvin077fb932010-07-20 14:56:30 -07001246
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001247 if (v) {
1248 char *p = getenv(v);
1249 if (!p) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03001250 nasm_nonfatalf(ERR_PASS1, "nonexistent environment variable `%s'", v);
Cyrill Gorcunovbbb7a1a2016-06-19 12:15:24 +03001251 /*
1252 * FIXME We better should investigate if accessing
1253 * ->text[1] without ->text[0] is safe enough.
1254 */
1255 t->text = nasm_zalloc(2);
1256 } else
1257 t->text = nasm_strdup(p);
Cyrill Gorcunov75004872017-07-26 01:21:16 +03001258 nasm_free(q);
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001259 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001260 }
H. Peter Anvin077fb932010-07-20 14:56:30 -07001261
H. Peter Anvine2c80182005-01-15 22:15:51 +00001262 /* Expand local macros here and not during preprocessing */
1263 if (expand_locals &&
1264 t->type == TOK_PREPROC_ID && t->text &&
1265 t->text[0] == '%' && t->text[1] == '$') {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001266 const char *q;
1267 char *p;
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04001268 Context *ctx = get_ctx(t->text, &q);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001269 if (ctx) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001270 char buffer[40];
Keith Kanios93f2e9a2007-04-14 00:10:59 +00001271 snprintf(buffer, sizeof(buffer), "..@%"PRIu32".", ctx->number);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001272 p = nasm_strcat(buffer, q);
1273 nasm_free(t->text);
1274 t->text = p;
1275 }
1276 }
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001277 if (t->type == TOK_WHITESPACE)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001278 len++;
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001279 else if (t->text)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001280 len += strlen(t->text);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001281 }
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001282
H. Peter Anvin734b1882002-04-30 21:01:08 +00001283 p = line = nasm_malloc(len + 1);
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001284
1285 list_for_each(t, tlist) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001286 if (t->type == TOK_WHITESPACE) {
H. Peter Anvinb4daadc2008-01-21 16:31:57 -08001287 *p++ = ' ';
H. Peter Anvine2c80182005-01-15 22:15:51 +00001288 } else if (t->text) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001289 q = t->text;
1290 while (*q)
1291 *p++ = *q++;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001292 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001293 }
1294 *p = '\0';
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001295
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001296 return line;
1297}
1298
1299/*
H. Peter Anvin76690a12002-04-30 20:52:49 +00001300 * A scanner, suitable for use by the expression evaluator, which
1301 * operates on a line of Tokens. Expects a pointer to a pointer to
1302 * the first token in the line to be passed in as its private_data
1303 * field.
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001304 *
1305 * FIX: This really needs to be unified with stdscan.
H. Peter Anvin76690a12002-04-30 20:52:49 +00001306 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001307static int ppscan(void *private_data, struct tokenval *tokval)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001308{
H. Peter Anvin76690a12002-04-30 20:52:49 +00001309 Token **tlineptr = private_data;
1310 Token *tline;
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001311 char ourcopy[MAX_KEYWORD+1], *p, *r, *s;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001312
H. Peter Anvine2c80182005-01-15 22:15:51 +00001313 do {
1314 tline = *tlineptr;
1315 *tlineptr = tline ? tline->next : NULL;
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04001316 } while (tline && (tline->type == TOK_WHITESPACE ||
1317 tline->type == TOK_COMMENT));
H. Peter Anvin76690a12002-04-30 20:52:49 +00001318
1319 if (!tline)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001320 return tokval->t_type = TOKEN_EOS;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001321
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001322 tokval->t_charptr = tline->text;
1323
H. Peter Anvin76690a12002-04-30 20:52:49 +00001324 if (tline->text[0] == '$' && !tline->text[1])
H. Peter Anvine2c80182005-01-15 22:15:51 +00001325 return tokval->t_type = TOKEN_HERE;
H. Peter Anvin7cf897e2002-05-30 21:30:33 +00001326 if (tline->text[0] == '$' && tline->text[1] == '$' && !tline->text[2])
H. Peter Anvine2c80182005-01-15 22:15:51 +00001327 return tokval->t_type = TOKEN_BASE;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001328
H. Peter Anvine2c80182005-01-15 22:15:51 +00001329 if (tline->type == TOK_ID) {
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001330 p = tokval->t_charptr = tline->text;
1331 if (p[0] == '$') {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001332 tokval->t_charptr++;
1333 return tokval->t_type = TOKEN_ID;
1334 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00001335
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001336 for (r = p, s = ourcopy; *r; r++) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001337 if (r >= p+MAX_KEYWORD)
1338 return tokval->t_type = TOKEN_ID; /* Not a keyword */
H. Peter Anvinac8f8fc2008-06-11 15:49:41 -07001339 *s++ = nasm_tolower(*r);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001340 }
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001341 *s = '\0';
1342 /* right, so we have an identifier sitting in temp storage. now,
1343 * is it actually a register or instruction name, or what? */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001344 return nasm_token_hash(ourcopy, tokval);
H. Peter Anvin76690a12002-04-30 20:52:49 +00001345 }
1346
H. Peter Anvine2c80182005-01-15 22:15:51 +00001347 if (tline->type == TOK_NUMBER) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001348 bool rn_error;
1349 tokval->t_integer = readnum(tline->text, &rn_error);
1350 tokval->t_charptr = tline->text;
1351 if (rn_error)
1352 return tokval->t_type = TOKEN_ERRNUM;
1353 else
1354 return tokval->t_type = TOKEN_NUM;
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001355 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00001356
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001357 if (tline->type == TOK_FLOAT) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001358 return tokval->t_type = TOKEN_FLOAT;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001359 }
1360
H. Peter Anvine2c80182005-01-15 22:15:51 +00001361 if (tline->type == TOK_STRING) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001362 char bq, *ep;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001363
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001364 bq = tline->text[0];
H. Peter Anvin11627042008-06-09 20:45:19 -07001365 tokval->t_charptr = tline->text;
1366 tokval->t_inttwo = nasm_unquote(tline->text, &ep);
H. Peter Anvind2456592008-06-19 15:04:18 -07001367
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001368 if (ep[0] != bq || ep[1] != '\0')
1369 return tokval->t_type = TOKEN_ERRSTR;
1370 else
1371 return tokval->t_type = TOKEN_STR;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001372 }
1373
H. Peter Anvine2c80182005-01-15 22:15:51 +00001374 if (tline->type == TOK_OTHER) {
1375 if (!strcmp(tline->text, "<<"))
1376 return tokval->t_type = TOKEN_SHL;
1377 if (!strcmp(tline->text, ">>"))
1378 return tokval->t_type = TOKEN_SHR;
1379 if (!strcmp(tline->text, "//"))
1380 return tokval->t_type = TOKEN_SDIV;
1381 if (!strcmp(tline->text, "%%"))
1382 return tokval->t_type = TOKEN_SMOD;
1383 if (!strcmp(tline->text, "=="))
1384 return tokval->t_type = TOKEN_EQ;
1385 if (!strcmp(tline->text, "<>"))
1386 return tokval->t_type = TOKEN_NE;
1387 if (!strcmp(tline->text, "!="))
1388 return tokval->t_type = TOKEN_NE;
1389 if (!strcmp(tline->text, "<="))
1390 return tokval->t_type = TOKEN_LE;
1391 if (!strcmp(tline->text, ">="))
1392 return tokval->t_type = TOKEN_GE;
1393 if (!strcmp(tline->text, "&&"))
1394 return tokval->t_type = TOKEN_DBL_AND;
1395 if (!strcmp(tline->text, "^^"))
1396 return tokval->t_type = TOKEN_DBL_XOR;
1397 if (!strcmp(tline->text, "||"))
1398 return tokval->t_type = TOKEN_DBL_OR;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001399 }
1400
1401 /*
1402 * We have no other options: just return the first character of
1403 * the token text.
1404 */
1405 return tokval->t_type = tline->text[0];
1406}
1407
1408/*
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001409 * Compare a string to the name of an existing macro; this is a
1410 * simple wrapper which calls either strcmp or nasm_stricmp
1411 * depending on the value of the `casesense' parameter.
1412 */
H. Peter Anvin4db5a162007-10-11 13:42:09 -07001413static int mstrcmp(const char *p, const char *q, bool casesense)
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001414{
H. Peter Anvin734b1882002-04-30 21:01:08 +00001415 return casesense ? strcmp(p, q) : nasm_stricmp(p, q);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001416}
1417
1418/*
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07001419 * Compare a string to the name of an existing macro; this is a
1420 * simple wrapper which calls either strcmp or nasm_stricmp
1421 * depending on the value of the `casesense' parameter.
1422 */
1423static int mmemcmp(const char *p, const char *q, size_t l, bool casesense)
1424{
1425 return casesense ? memcmp(p, q, l) : nasm_memicmp(p, q, l);
1426}
1427
1428/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001429 * Return the Context structure associated with a %$ token. Return
1430 * NULL, having _already_ reported an error condition, if the
1431 * context stack isn't deep enough for the supplied number of $
1432 * signs.
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001433 *
1434 * If "namep" is non-NULL, set it to the pointer to the macro name
1435 * tail, i.e. the part beyond %$...
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001436 */
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04001437static Context *get_ctx(const char *name, const char **namep)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001438{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001439 Context *ctx;
1440 int i;
1441
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001442 if (namep)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001443 *namep = name;
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001444
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001445 if (!name || name[0] != '%' || name[1] != '$')
H. Peter Anvine2c80182005-01-15 22:15:51 +00001446 return NULL;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001447
H. Peter Anvine2c80182005-01-15 22:15:51 +00001448 if (!cstk) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03001449 nasm_nonfatal("`%s': context stack is empty", name);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001450 return NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001451 }
1452
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001453 name += 2;
1454 ctx = cstk;
1455 i = 0;
1456 while (ctx && *name == '$') {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001457 name++;
1458 i++;
1459 ctx = ctx->next;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001460 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001461 if (!ctx) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03001462 nasm_nonfatal("`%s': context stack is only"
1463 " %d level%s deep", name, i, (i == 1 ? "" : "s"));
H. Peter Anvine2c80182005-01-15 22:15:51 +00001464 return NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001465 }
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001466
1467 if (namep)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001468 *namep = name;
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001469
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04001470 return ctx;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001471}
1472
1473/*
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001474 * Open an include file. This routine must always return a valid
1475 * file pointer if it returns - it's responsible for throwing an
1476 * ERR_FATAL and bombing out completely if not. It should also try
1477 * the include path one by one until it finds the file or reaches
1478 * the end of the path.
H. Peter Anvind81a2352016-09-21 14:03:18 -07001479 *
1480 * Note: for INC_PROBE the function returns NULL at all times;
1481 * instead look for the
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001482 */
H. Peter Anvind81a2352016-09-21 14:03:18 -07001483enum incopen_mode {
1484 INC_NEEDED, /* File must exist */
1485 INC_OPTIONAL, /* Missing is OK */
1486 INC_PROBE /* Only an existence probe */
1487};
1488
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001489/* This is conducts a full pathname search */
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07001490static FILE *inc_fopen_search(const char *file, char **slpath,
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001491 enum incopen_mode omode, enum file_flags fmode)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001492{
H. Peter Anvin (Intel)64471092018-12-11 13:06:14 -08001493 const struct strlist_entry *ip = strlist_head(ipath_list);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001494 FILE *fp;
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07001495 const char *prefix = "";
night199ukfdb1a1b2018-10-18 23:19:47 +02001496 char *sp;
H. Peter Anvind81a2352016-09-21 14:03:18 -07001497 bool found;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001498
H. Peter Anvine2c80182005-01-15 22:15:51 +00001499 while (1) {
night199ukfdb1a1b2018-10-18 23:19:47 +02001500 sp = nasm_catfile(prefix, file);
H. Peter Anvind81a2352016-09-21 14:03:18 -07001501 if (omode == INC_PROBE) {
1502 fp = NULL;
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07001503 found = nasm_file_exists(sp);
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001504 } else {
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07001505 fp = nasm_open_read(sp, fmode);
H. Peter Anvind81a2352016-09-21 14:03:18 -07001506 found = (fp != NULL);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001507 }
H. Peter Anvind81a2352016-09-21 14:03:18 -07001508 if (found) {
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07001509 *slpath = sp;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001510 return fp;
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001511 }
Jim Kukunas65a8afc2016-06-13 16:00:42 -04001512
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07001513 nasm_free(sp);
Jim Kukunas65a8afc2016-06-13 16:00:42 -04001514
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07001515 if (!ip) {
1516 *slpath = NULL;
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001517 return NULL;
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07001518 }
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001519
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07001520 prefix = ip->str;
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001521 ip = ip->next;
1522 }
1523}
1524
1525/*
1526 * Open a file, or test for the presence of one (depending on omode),
1527 * considering the include path.
1528 */
1529static FILE *inc_fopen(const char *file,
Cyrill Gorcunovb7bb5ac2018-11-11 21:33:52 +03001530 struct strlist *dhead,
H. Peter Anvinccad6f92016-10-04 00:34:35 -07001531 const char **found_path,
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001532 enum incopen_mode omode,
1533 enum file_flags fmode)
1534{
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001535 struct hash_insert hi;
1536 void **hp;
1537 char *path;
1538 FILE *fp = NULL;
1539
1540 hp = hash_find(&FileHash, file, &hi);
1541 if (hp) {
1542 path = *hp;
Martin Storsjöf283c8f2017-08-13 17:28:46 +03001543 if (path || omode != INC_NEEDED) {
Cyrill Gorcunovb7bb5ac2018-11-11 21:33:52 +03001544 strlist_add(dhead, path ? path : file);
Martin Storsjöf283c8f2017-08-13 17:28:46 +03001545 }
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001546 } else {
1547 /* Need to do the actual path search */
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07001548 fp = inc_fopen_search(file, &path, omode, fmode);
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001549
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07001550 /* Positive or negative result */
1551 hash_add(&hi, nasm_strdup(file), path);
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001552
H. Peter Anvin9924d1e2016-10-04 00:59:39 -07001553 /*
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07001554 * Add file to dependency path.
H. Peter Anvin9924d1e2016-10-04 00:59:39 -07001555 */
1556 if (path || omode != INC_NEEDED)
Cyrill Gorcunovb7bb5ac2018-11-11 21:33:52 +03001557 strlist_add(dhead, file);
H. Peter Anvineba20a72002-04-30 20:53:55 +00001558 }
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001559
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001560 if (!path) {
1561 if (omode == INC_NEEDED)
H. Peter Anvinc5136902018-06-15 18:20:17 -07001562 nasm_fatal("unable to open include file `%s'", file);
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07001563 } else {
1564 if (!fp && omode != INC_PROBE)
1565 fp = nasm_open_read(path, fmode);
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001566 }
1567
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001568 if (found_path)
H. Peter Anvinccad6f92016-10-04 00:34:35 -07001569 *found_path = path;
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001570
1571 return fp;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001572}
1573
1574/*
Fabian Giesen0bbc38d2016-04-28 13:48:14 -07001575 * Opens an include or input file. Public version, for use by modules
1576 * that get a file:lineno pair and need to look at the file again
1577 * (e.g. the CodeView debug backend). Returns NULL on failure.
1578 */
H. Peter Anvin3e83cec2016-05-25 04:28:46 -07001579FILE *pp_input_fopen(const char *filename, enum file_flags mode)
Fabian Giesen0bbc38d2016-04-28 13:48:14 -07001580{
H. Peter Anvin9924d1e2016-10-04 00:59:39 -07001581 return inc_fopen(filename, NULL, NULL, INC_OPTIONAL, mode);
Fabian Giesen0bbc38d2016-04-28 13:48:14 -07001582}
1583
1584/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001585 * Determine if we should warn on defining a single-line macro of
H. Peter Anvinef7468f2002-04-30 20:57:59 +00001586 * name `name', with `nparam' parameters. If nparam is 0 or -1, will
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001587 * return true if _any_ single-line macro of that name is defined.
1588 * Otherwise, will return true if a single-line macro with either
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001589 * `nparam' or no parameters is defined.
1590 *
1591 * If a macro with precisely the right number of parameters is
H. Peter Anvinef7468f2002-04-30 20:57:59 +00001592 * defined, or nparam is -1, the address of the definition structure
1593 * will be returned in `defn'; otherwise NULL will be returned. If `defn'
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001594 * is NULL, no action will be taken regarding its contents, and no
1595 * error will occur.
1596 *
1597 * Note that this is also called with nparam zero to resolve
1598 * `ifdef'.
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001599 *
1600 * If you already know which context macro belongs to, you can pass
1601 * the context pointer as first parameter; if you won't but name begins
1602 * with %$ the context will be automatically computed. If all_contexts
1603 * is true, macro will be searched in outer contexts as well.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001604 */
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07001605static bool
H. Peter Anvinb2a5fda2008-06-19 21:42:42 -07001606smacro_defined(Context * ctx, const char *name, int nparam, SMacro ** defn,
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07001607 bool nocase)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001608{
H. Peter Anvin166c2472008-05-28 12:28:58 -07001609 struct hash_table *smtbl;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001610 SMacro *m;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001611
H. Peter Anvin97a23472007-09-16 17:57:25 -07001612 if (ctx) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001613 smtbl = &ctx->localmac;
H. Peter Anvin97a23472007-09-16 17:57:25 -07001614 } else if (name[0] == '%' && name[1] == '$') {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001615 if (cstk)
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04001616 ctx = get_ctx(name, &name);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001617 if (!ctx)
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001618 return false; /* got to return _something_ */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001619 smtbl = &ctx->localmac;
H. Peter Anvin97a23472007-09-16 17:57:25 -07001620 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001621 smtbl = &smacros;
H. Peter Anvin97a23472007-09-16 17:57:25 -07001622 }
H. Peter Anvin166c2472008-05-28 12:28:58 -07001623 m = (SMacro *) hash_findix(smtbl, name);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001624
H. Peter Anvine2c80182005-01-15 22:15:51 +00001625 while (m) {
1626 if (!mstrcmp(m->name, name, m->casesense && nocase) &&
Charles Crayne192d5b52007-10-18 19:02:42 -07001627 (nparam <= 0 || m->nparam == 0 || nparam == (int) m->nparam)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001628 if (defn) {
Charles Crayne192d5b52007-10-18 19:02:42 -07001629 if (nparam == (int) m->nparam || nparam == -1)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001630 *defn = m;
1631 else
1632 *defn = NULL;
1633 }
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001634 return true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001635 }
1636 m = m->next;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001637 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001638
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001639 return false;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001640}
1641
Cyrill Gorcunov3079f792018-11-14 10:03:42 +03001642/* param should be a natural number [0; INT_MAX] */
1643static int read_param_count(const char *str)
1644{
1645 int result;
1646 bool err;
1647
1648 result = readnum(str, &err);
1649 if (result < 0 || result > INT_MAX) {
1650 result = 0;
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03001651 nasm_nonfatal("parameter count `%s' is out of bounds [%d; %d]",
1652 str, 0, INT_MAX);
1653 } else if (err)
1654 nasm_nonfatal("unable to parse parameter count `%s'", str);
Cyrill Gorcunov3079f792018-11-14 10:03:42 +03001655 return result;
1656}
1657
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001658/*
1659 * Count and mark off the parameters in a multi-line macro call.
1660 * This is called both from within the multi-line macro expansion
1661 * code, and also to mark off the default parameters when provided
1662 * in a %macro definition line.
1663 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001664static void count_mmac_params(Token * t, int *nparam, Token *** params)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001665{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001666 int paramsize, brace;
1667
1668 *nparam = paramsize = 0;
1669 *params = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001670 while (t) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001671 /* +1: we need space for the final NULL */
H. Peter Anvin91fb6f12008-09-01 10:56:33 -07001672 if (*nparam+1 >= paramsize) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001673 paramsize += PARAM_DELTA;
1674 *params = nasm_realloc(*params, sizeof(**params) * paramsize);
1675 }
1676 skip_white_(t);
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08001677 brace = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001678 if (tok_is_(t, "{"))
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08001679 brace++;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001680 (*params)[(*nparam)++] = t;
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08001681 if (brace) {
1682 while (brace && (t = t->next) != NULL) {
1683 if (tok_is_(t, "{"))
1684 brace++;
1685 else if (tok_is_(t, "}"))
1686 brace--;
1687 }
1688
1689 if (t) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001690 /*
1691 * Now we've found the closing brace, look further
1692 * for the comma.
1693 */
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08001694 t = t->next;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001695 skip_white_(t);
1696 if (tok_isnt_(t, ",")) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03001697 nasm_nonfatal("braces do not enclose all of macro parameter");
H. Peter Anvine2c80182005-01-15 22:15:51 +00001698 while (tok_isnt_(t, ","))
1699 t = t->next;
1700 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001701 }
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08001702 } else {
1703 while (tok_isnt_(t, ","))
1704 t = t->next;
1705 }
1706 if (t) { /* got a comma/brace */
1707 t = t->next; /* eat the comma */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001708 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001709 }
1710}
1711
1712/*
H. Peter Anvin76690a12002-04-30 20:52:49 +00001713 * Determine whether one of the various `if' conditions is true or
1714 * not.
1715 *
1716 * We must free the tline we get passed.
1717 */
H. Peter Anvin70055962007-10-11 00:05:31 -07001718static bool if_condition(Token * tline, enum preproc_token ct)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001719{
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001720 enum pp_conditional i = PP_COND(ct);
H. Peter Anvin70055962007-10-11 00:05:31 -07001721 bool j;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001722 Token *t, *tt, **tptr, *origline;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001723 struct tokenval tokval;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001724 expr *evalresult;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001725 enum pp_token_type needtype;
H. Peter Anvin077fb932010-07-20 14:56:30 -07001726 char *p;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001727
1728 origline = tline;
1729
H. Peter Anvine2c80182005-01-15 22:15:51 +00001730 switch (i) {
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001731 case PPC_IFCTX:
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001732 j = false; /* have we matched yet? */
Victor van den Elzen0e857f12008-07-23 13:21:29 +02001733 while (true) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001734 skip_white_(tline);
Victor van den Elzen0e857f12008-07-23 13:21:29 +02001735 if (!tline)
1736 break;
1737 if (tline->type != TOK_ID) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03001738 nasm_nonfatal("`%s' expects context identifiers",
1739 pp_directives[ct]);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001740 free_tlist(origline);
1741 return -1;
1742 }
Victor van den Elzen0e857f12008-07-23 13:21:29 +02001743 if (cstk && cstk->name && !nasm_stricmp(tline->text, cstk->name))
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001744 j = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001745 tline = tline->next;
1746 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001747 break;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001748
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001749 case PPC_IFDEF:
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001750 j = false; /* have we matched yet? */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001751 while (tline) {
1752 skip_white_(tline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001753 if (!tline || (tline->type != TOK_ID &&
1754 (tline->type != TOK_PREPROC_ID ||
1755 tline->text[1] != '$'))) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03001756 nasm_nonfatal("`%s' expects macro identifiers",
1757 pp_directives[ct]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001758 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001759 }
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07001760 if (smacro_defined(NULL, tline->text, 0, NULL, true))
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001761 j = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001762 tline = tline->next;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001763 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001764 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001765
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001766 case PPC_IFENV:
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001767 tline = expand_smacro(tline);
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001768 j = false; /* have we matched yet? */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001769 while (tline) {
1770 skip_white_(tline);
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001771 if (!tline || (tline->type != TOK_ID &&
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001772 tline->type != TOK_STRING &&
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001773 (tline->type != TOK_PREPROC_ID ||
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001774 tline->text[1] != '!'))) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03001775 nasm_nonfatal("`%s' expects environment variable names",
1776 pp_directives[ct]);
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001777 goto fail;
1778 }
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001779 p = tline->text;
1780 if (tline->type == TOK_PREPROC_ID)
1781 p += 2; /* Skip leading %! */
H. Peter Anvin53e2e4c2018-11-28 15:01:40 -08001782 if (nasm_isquote(*p))
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001783 nasm_unquote_cstr(p, ct);
1784 if (getenv(p))
1785 j = true;
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001786 tline = tline->next;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001787 }
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001788 break;
1789
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001790 case PPC_IFIDN:
1791 case PPC_IFIDNI:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001792 tline = expand_smacro(tline);
1793 t = tt = tline;
1794 while (tok_isnt_(tt, ","))
1795 tt = tt->next;
1796 if (!tt) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03001797 nasm_nonfatal("`%s' expects two comma-separated arguments",
1798 pp_directives[ct]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001799 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001800 }
1801 tt = tt->next;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001802 j = true; /* assume equality unless proved not */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001803 while ((t->type != TOK_OTHER || strcmp(t->text, ",")) && tt) {
1804 if (tt->type == TOK_OTHER && !strcmp(tt->text, ",")) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03001805 nasm_nonfatal("`%s': more than one comma on line",
1806 pp_directives[ct]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001807 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001808 }
1809 if (t->type == TOK_WHITESPACE) {
1810 t = t->next;
1811 continue;
1812 }
1813 if (tt->type == TOK_WHITESPACE) {
1814 tt = tt->next;
1815 continue;
1816 }
1817 if (tt->type != t->type) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001818 j = false; /* found mismatching tokens */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001819 break;
1820 }
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07001821 /* When comparing strings, need to unquote them first */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001822 if (t->type == TOK_STRING) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001823 size_t l1 = nasm_unquote(t->text, NULL);
1824 size_t l2 = nasm_unquote(tt->text, NULL);
H. Peter Anvind2456592008-06-19 15:04:18 -07001825
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001826 if (l1 != l2) {
1827 j = false;
1828 break;
1829 }
1830 if (mmemcmp(t->text, tt->text, l1, i == PPC_IFIDN)) {
1831 j = false;
1832 break;
1833 }
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07001834 } else if (mstrcmp(tt->text, t->text, i == PPC_IFIDN) != 0) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001835 j = false; /* found mismatching tokens */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001836 break;
1837 }
Nickolay Yurchenkof3b3ce22003-09-21 20:38:43 +00001838
H. Peter Anvine2c80182005-01-15 22:15:51 +00001839 t = t->next;
1840 tt = tt->next;
1841 }
1842 if ((t->type != TOK_OTHER || strcmp(t->text, ",")) || tt)
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001843 j = false; /* trailing gunk on one end or other */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001844 break;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001845
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001846 case PPC_IFMACRO:
H. Peter Anvin89cee572009-07-15 09:16:54 -04001847 {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001848 bool found = false;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001849 MMacro searching, *mmac;
H. Peter Anvin65747262002-05-07 00:10:05 +00001850
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001851 skip_white_(tline);
1852 tline = expand_id(tline);
1853 if (!tok_type_(tline, TOK_ID)) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03001854 nasm_nonfatal("`%s' expects a macro name", pp_directives[ct]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001855 goto fail;
1856 }
1857 searching.name = nasm_strdup(tline->text);
1858 searching.casesense = true;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001859 searching.plus = false;
1860 searching.nolist = false;
1861 searching.in_progress = 0;
1862 searching.max_depth = 0;
1863 searching.rep_nest = NULL;
1864 searching.nparam_min = 0;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001865 searching.nparam_max = INT_MAX;
1866 tline = expand_smacro(tline->next);
1867 skip_white_(tline);
1868 if (!tline) {
1869 } else if (!tok_type_(tline, TOK_NUMBER)) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03001870 nasm_nonfatal("`%s' expects a parameter count or nothing",
1871 pp_directives[ct]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001872 } else {
1873 searching.nparam_min = searching.nparam_max =
Cyrill Gorcunov3079f792018-11-14 10:03:42 +03001874 read_param_count(tline->text);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001875 }
1876 if (tline && tok_is_(tline->next, "-")) {
1877 tline = tline->next->next;
1878 if (tok_is_(tline, "*"))
1879 searching.nparam_max = INT_MAX;
1880 else if (!tok_type_(tline, TOK_NUMBER))
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03001881 nasm_nonfatal("`%s' expects a parameter count after `-'",
1882 pp_directives[ct]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001883 else {
Cyrill Gorcunov3079f792018-11-14 10:03:42 +03001884 searching.nparam_max = read_param_count(tline->text);
Cyrill Gorcunovc9244ea2017-10-22 15:25:48 +03001885 if (searching.nparam_min > searching.nparam_max) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03001886 nasm_nonfatal("minimum parameter count exceeds maximum");
Cyrill Gorcunovc9244ea2017-10-22 15:25:48 +03001887 searching.nparam_max = searching.nparam_min;
1888 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001889 }
1890 }
1891 if (tline && tok_is_(tline->next, "+")) {
1892 tline = tline->next;
1893 searching.plus = true;
1894 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001895 mmac = (MMacro *) hash_findix(&mmacros, searching.name);
1896 while (mmac) {
1897 if (!strcmp(mmac->name, searching.name) &&
1898 (mmac->nparam_min <= searching.nparam_max
1899 || searching.plus)
1900 && (searching.nparam_min <= mmac->nparam_max
1901 || mmac->plus)) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001902 found = true;
1903 break;
1904 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001905 mmac = mmac->next;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001906 }
1907 if (tline && tline->next)
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08001908 nasm_warn(WARN_OTHER, "trailing garbage after %%ifmacro ignored");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001909 nasm_free(searching.name);
1910 j = found;
1911 break;
H. Peter Anvin89cee572009-07-15 09:16:54 -04001912 }
H. Peter Anvin65747262002-05-07 00:10:05 +00001913
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001914 case PPC_IFID:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001915 needtype = TOK_ID;
1916 goto iftype;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001917 case PPC_IFNUM:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001918 needtype = TOK_NUMBER;
1919 goto iftype;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001920 case PPC_IFSTR:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001921 needtype = TOK_STRING;
1922 goto iftype;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001923
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001924iftype:
1925 t = tline = expand_smacro(tline);
H. Peter Anvind85d2502008-05-04 17:53:31 -07001926
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001927 while (tok_type_(t, TOK_WHITESPACE) ||
1928 (needtype == TOK_NUMBER &&
1929 tok_type_(t, TOK_OTHER) &&
1930 (t->text[0] == '-' || t->text[0] == '+') &&
1931 !t->text[1]))
1932 t = t->next;
H. Peter Anvind85d2502008-05-04 17:53:31 -07001933
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001934 j = tok_type_(t, needtype);
1935 break;
H. Peter Anvincbf768d2008-02-16 16:41:25 -08001936
1937 case PPC_IFTOKEN:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001938 t = tline = expand_smacro(tline);
1939 while (tok_type_(t, TOK_WHITESPACE))
1940 t = t->next;
H. Peter Anvincbf768d2008-02-16 16:41:25 -08001941
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001942 j = false;
1943 if (t) {
1944 t = t->next; /* Skip the actual token */
1945 while (tok_type_(t, TOK_WHITESPACE))
1946 t = t->next;
1947 j = !t; /* Should be nothing left */
1948 }
1949 break;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001950
H. Peter Anvin134b9462008-02-16 17:01:40 -08001951 case PPC_IFEMPTY:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001952 t = tline = expand_smacro(tline);
1953 while (tok_type_(t, TOK_WHITESPACE))
1954 t = t->next;
H. Peter Anvin134b9462008-02-16 17:01:40 -08001955
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001956 j = !t; /* Should be empty */
1957 break;
H. Peter Anvin134b9462008-02-16 17:01:40 -08001958
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001959 case PPC_IF:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001960 t = tline = expand_smacro(tline);
1961 tptr = &t;
1962 tokval.t_type = TOKEN_INVALID;
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08001963 evalresult = evaluate(ppscan, tptr, &tokval, NULL, true, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001964 if (!evalresult)
1965 return -1;
1966 if (tokval.t_type)
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08001967 nasm_warn(WARN_OTHER, "trailing garbage after expression ignored");
H. Peter Anvine2c80182005-01-15 22:15:51 +00001968 if (!is_simple(evalresult)) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03001969 nasm_nonfatal("non-constant value given to `%s'",
1970 pp_directives[ct]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001971 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001972 }
Chuck Crayne60ae75d2007-05-02 01:59:16 +00001973 j = reloc_value(evalresult) != 0;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001974 break;
H. Peter Anvin95e28822007-09-12 04:20:08 +00001975
H. Peter Anvine2c80182005-01-15 22:15:51 +00001976 default:
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03001977 nasm_fatal("preprocessor directive `%s' not yet implemented",
1978 pp_directives[ct]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001979 goto fail;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001980 }
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001981
1982 free_tlist(origline);
1983 return j ^ PP_NEGATIVE(ct);
H. Peter Anvin70653092007-10-19 14:42:29 -07001984
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001985fail:
1986 free_tlist(origline);
1987 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001988}
1989
1990/*
H. Peter Anvin4db5a162007-10-11 13:42:09 -07001991 * Common code for defining an smacro
1992 */
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001993static bool define_smacro(Context *ctx, const char *mname, bool casesense,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001994 int nparam, Token *expansion)
H. Peter Anvin4db5a162007-10-11 13:42:09 -07001995{
1996 SMacro *smac, **smhead;
H. Peter Anvin166c2472008-05-28 12:28:58 -07001997 struct hash_table *smtbl;
H. Peter Anvin70653092007-10-19 14:42:29 -07001998
H. Peter Anvin4db5a162007-10-11 13:42:09 -07001999 if (smacro_defined(ctx, mname, nparam, &smac, casesense)) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002000 if (!smac) {
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08002001 nasm_warn(WARN_OTHER, "single-line macro `%s' defined both with and"
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002002 " without parameters", mname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002003 /*
2004 * Some instances of the old code considered this a failure,
2005 * some others didn't. What is the right thing to do here?
2006 */
2007 free_tlist(expansion);
2008 return false; /* Failure */
2009 } else {
2010 /*
2011 * We're redefining, so we have to take over an
2012 * existing SMacro structure. This means freeing
2013 * what was already in it.
2014 */
2015 nasm_free(smac->name);
2016 free_tlist(smac->expansion);
2017 }
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002018 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002019 smtbl = ctx ? &ctx->localmac : &smacros;
2020 smhead = (SMacro **) hash_findi_add(smtbl, mname);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002021 smac = nasm_malloc(sizeof(SMacro));
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002022 smac->next = *smhead;
2023 *smhead = smac;
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002024 }
2025 smac->name = nasm_strdup(mname);
2026 smac->casesense = casesense;
2027 smac->nparam = nparam;
2028 smac->expansion = expansion;
2029 smac->in_progress = false;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002030 return true; /* Success */
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002031}
2032
2033/*
2034 * Undefine an smacro
2035 */
2036static void undef_smacro(Context *ctx, const char *mname)
2037{
2038 SMacro **smhead, *s, **sp;
H. Peter Anvin166c2472008-05-28 12:28:58 -07002039 struct hash_table *smtbl;
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002040
H. Peter Anvin166c2472008-05-28 12:28:58 -07002041 smtbl = ctx ? &ctx->localmac : &smacros;
2042 smhead = (SMacro **)hash_findi(smtbl, mname, NULL);
H. Peter Anvin70653092007-10-19 14:42:29 -07002043
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002044 if (smhead) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002045 /*
2046 * We now have a macro name... go hunt for it.
2047 */
2048 sp = smhead;
2049 while ((s = *sp) != NULL) {
2050 if (!mstrcmp(s->name, mname, s->casesense)) {
2051 *sp = s->next;
2052 nasm_free(s->name);
2053 free_tlist(s->expansion);
2054 nasm_free(s);
2055 } else {
2056 sp = &s->next;
2057 }
2058 }
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002059 }
2060}
2061
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002062/*
H. Peter Anvina26433d2008-07-16 14:40:01 -07002063 * Parse a mmacro specification.
2064 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002065static bool parse_mmacro_spec(Token *tline, MMacro *def, const char *directive)
H. Peter Anvina26433d2008-07-16 14:40:01 -07002066{
H. Peter Anvina26433d2008-07-16 14:40:01 -07002067 tline = tline->next;
2068 skip_white_(tline);
2069 tline = expand_id(tline);
2070 if (!tok_type_(tline, TOK_ID)) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002071 nasm_nonfatal("`%s' expects a macro name", directive);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002072 return false;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002073 }
Victor van den Elzenb916ede2008-07-23 15:14:22 +02002074
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002075 def->prev = NULL;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002076 def->name = nasm_strdup(tline->text);
2077 def->plus = false;
2078 def->nolist = false;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002079 def->in_progress = 0;
2080 def->rep_nest = NULL;
Victor van den Elzenb916ede2008-07-23 15:14:22 +02002081 def->nparam_min = 0;
2082 def->nparam_max = 0;
2083
H. Peter Anvina26433d2008-07-16 14:40:01 -07002084 tline = expand_smacro(tline->next);
2085 skip_white_(tline);
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002086 if (!tok_type_(tline, TOK_NUMBER))
2087 nasm_nonfatal("`%s' expects a parameter count", directive);
2088 else
Cyrill Gorcunov3079f792018-11-14 10:03:42 +03002089 def->nparam_min = def->nparam_max = read_param_count(tline->text);
H. Peter Anvina26433d2008-07-16 14:40:01 -07002090 if (tline && tok_is_(tline->next, "-")) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002091 tline = tline->next->next;
2092 if (tok_is_(tline, "*")) {
2093 def->nparam_max = INT_MAX;
2094 } else if (!tok_type_(tline, TOK_NUMBER)) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002095 nasm_nonfatal("`%s' expects a parameter count after `-'", directive);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002096 } else {
Cyrill Gorcunov3079f792018-11-14 10:03:42 +03002097 def->nparam_max = read_param_count(tline->text);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002098 if (def->nparam_min > def->nparam_max) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002099 nasm_nonfatal("minimum parameter count exceeds maximum");
Cyrill Gorcunovc9244ea2017-10-22 15:25:48 +03002100 def->nparam_max = def->nparam_min;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002101 }
2102 }
H. Peter Anvina26433d2008-07-16 14:40:01 -07002103 }
2104 if (tline && tok_is_(tline->next, "+")) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002105 tline = tline->next;
2106 def->plus = true;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002107 }
2108 if (tline && tok_type_(tline->next, TOK_ID) &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002109 !nasm_stricmp(tline->next->text, ".nolist")) {
2110 tline = tline->next;
2111 def->nolist = true;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002112 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002113
H. Peter Anvina26433d2008-07-16 14:40:01 -07002114 /*
2115 * Handle default parameters.
2116 */
2117 if (tline && tline->next) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002118 def->dlist = tline->next;
2119 tline->next = NULL;
2120 count_mmac_params(def->dlist, &def->ndefs, &def->defaults);
H. Peter Anvina26433d2008-07-16 14:40:01 -07002121 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002122 def->dlist = NULL;
2123 def->defaults = NULL;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002124 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002125 def->expansion = NULL;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002126
H. Peter Anvin89cee572009-07-15 09:16:54 -04002127 if (def->defaults && def->ndefs > def->nparam_max - def->nparam_min &&
H. Peter Anvin (Intel)723ab482018-12-13 21:53:31 -08002128 !def->plus) {
2129 /*
2130 *!macro-defaults [on] macros with more default than optional parameters
2131 *! warns when a macro has more default parameters than optional parameters.
2132 *! See \k{mlmacdef} for why might want to disable this warning.
2133 */
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08002134 nasm_warn(WARN_MACRO_DEFAULTS,
H. Peter Anvin (Intel)723ab482018-12-13 21:53:31 -08002135 "too many default macro parameters in macro `%s'", def->name);
2136 }
Victor van den Elzenb916ede2008-07-23 15:14:22 +02002137
H. Peter Anvina26433d2008-07-16 14:40:01 -07002138 return true;
2139}
2140
2141
2142/*
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002143 * Decode a size directive
2144 */
2145static int parse_size(const char *str) {
2146 static const char *size_names[] =
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002147 { "byte", "dword", "oword", "qword", "tword", "word", "yword" };
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002148 static const int sizes[] =
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002149 { 0, 1, 4, 16, 8, 10, 2, 32 };
Cyrill Gorcunovc713b5f2018-09-29 14:30:14 +03002150 return str ? sizes[bsii(str, size_names, ARRAY_SIZE(size_names))+1] : 0;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002151}
2152
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07002153/*
2154 * Process a preprocessor %pragma directive. Currently there are none.
2155 * Gets passed the token list starting with the "preproc" token from
2156 * "%pragma preproc".
2157 */
2158static void do_pragma_preproc(Token *tline)
2159{
2160 /* Skip to the real stuff */
2161 tline = tline->next;
2162 skip_white_(tline);
2163 if (!tline)
2164 return;
2165
2166 (void)tline; /* Nothing else to do at present */
2167}
2168
Ed Beroset3ab3f412002-06-11 03:31:49 +00002169/**
2170 * find and process preprocessor directive in passed line
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002171 * Find out if a line contains a preprocessor directive, and deal
2172 * with it if so.
H. Peter Anvin70653092007-10-19 14:42:29 -07002173 *
Ed Beroset3ab3f412002-06-11 03:31:49 +00002174 * If a directive _is_ found, it is the responsibility of this routine
2175 * (and not the caller) to free_tlist() the line.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002176 *
Ed Beroset3ab3f412002-06-11 03:31:49 +00002177 * @param tline a pointer to the current tokeninzed line linked list
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07002178 * @param output if this directive generated output
Ed Beroset3ab3f412002-06-11 03:31:49 +00002179 * @return DIRECTIVE_FOUND or NO_DIRECTIVE_FOUND
H. Peter Anvin70653092007-10-19 14:42:29 -07002180 *
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002181 */
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07002182static int do_directive(Token *tline, char **output)
H. Peter Anvineba20a72002-04-30 20:53:55 +00002183{
H. Peter Anvin4169a472007-09-12 01:29:43 +00002184 enum preproc_token i;
2185 int j;
H. Peter Anvin70055962007-10-11 00:05:31 -07002186 bool err;
2187 int nparam;
2188 bool nolist;
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07002189 bool casesense;
H. Peter Anvin8cfdb9d2007-09-14 18:36:01 -07002190 int k, m;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002191 int offset;
H. Peter Anvinccad6f92016-10-04 00:34:35 -07002192 char *p, *pp;
2193 const char *found_path;
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08002194 const char *mname;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002195 Include *inc;
2196 Context *ctx;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002197 Cond *cond;
2198 MMacro *mmac, **mmhead;
Jin Kyu Songc9486b92013-10-28 17:07:57 -07002199 Token *t = NULL, *tt, *param_start, *macro_start, *last, **tptr, *origline;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002200 Line *l;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002201 struct tokenval tokval;
2202 expr *evalresult;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002203 MMacro *tmp_defining; /* Used when manipulating rep_nest */
H. Peter Anvinf8ba53e2007-10-11 10:11:57 -07002204 int64_t count;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07002205 size_t len;
H. Peter Anvin (Intel)6bde2ed2018-12-13 19:39:41 -08002206 errflags severity;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002207
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07002208 *output = NULL; /* No output generated */
H. Peter Anvin76690a12002-04-30 20:52:49 +00002209 origline = tline;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002210
H. Peter Anvineba20a72002-04-30 20:53:55 +00002211 skip_white_(tline);
H. Peter Anvinf2936d72008-06-04 15:11:23 -07002212 if (!tline || !tok_type_(tline, TOK_PREPROC_ID) ||
Cyrill Gorcunov4b5b7372018-10-29 22:54:08 +03002213 (tline->text[0] && (tline->text[1] == '%' ||
2214 tline->text[1] == '$' ||
2215 tline->text[1] == '!')))
H. Peter Anvine2c80182005-01-15 22:15:51 +00002216 return NO_DIRECTIVE_FOUND;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002217
H. Peter Anvin4169a472007-09-12 01:29:43 +00002218 i = pp_token_hash(tline->text);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002219
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002220 /*
2221 * FIXME: We zap execution of PP_RMACRO, PP_IRMACRO, PP_EXITMACRO
2222 * since they are known to be buggy at moment, we need to fix them
2223 * in future release (2.09-2.10)
2224 */
Philipp Klokeb432f572013-03-31 12:01:23 +02002225 if (i == PP_RMACRO || i == PP_IRMACRO || i == PP_EXITMACRO) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002226 nasm_nonfatal("unknown preprocessor directive `%s'", tline->text);
2227 return NO_DIRECTIVE_FOUND;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002228 }
2229
2230 /*
2231 * If we're in a non-emitting branch of a condition construct,
2232 * or walking to the end of an already terminated %rep block,
2233 * we should ignore all directives except for condition
2234 * directives.
2235 */
2236 if (((istk->conds && !emitting(istk->conds->state)) ||
2237 (istk->mstk && !istk->mstk->in_progress)) && !is_condition(i)) {
2238 return NO_DIRECTIVE_FOUND;
2239 }
2240
2241 /*
2242 * If we're defining a macro or reading a %rep block, we should
2243 * ignore all directives except for %macro/%imacro (which nest),
2244 * %endm/%endmacro, and (only if we're in a %rep block) %endrep.
2245 * If we're in a %rep block, another %rep nests, so should be let through.
2246 */
2247 if (defining && i != PP_MACRO && i != PP_IMACRO &&
2248 i != PP_RMACRO && i != PP_IRMACRO &&
2249 i != PP_ENDMACRO && i != PP_ENDM &&
2250 (defining->name || (i != PP_ENDREP && i != PP_REP))) {
2251 return NO_DIRECTIVE_FOUND;
2252 }
2253
2254 if (defining) {
2255 if (i == PP_MACRO || i == PP_IMACRO ||
2256 i == PP_RMACRO || i == PP_IRMACRO) {
2257 nested_mac_count++;
2258 return NO_DIRECTIVE_FOUND;
2259 } else if (nested_mac_count > 0) {
2260 if (i == PP_ENDMACRO) {
2261 nested_mac_count--;
2262 return NO_DIRECTIVE_FOUND;
2263 }
2264 }
2265 if (!defining->name) {
2266 if (i == PP_REP) {
2267 nested_rep_count++;
2268 return NO_DIRECTIVE_FOUND;
2269 } else if (nested_rep_count > 0) {
2270 if (i == PP_ENDREP) {
2271 nested_rep_count--;
2272 return NO_DIRECTIVE_FOUND;
2273 }
2274 }
2275 }
2276 }
2277
H. Peter Anvin4169a472007-09-12 01:29:43 +00002278 switch (i) {
2279 case PP_INVALID:
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002280 nasm_nonfatal("unknown preprocessor directive `%s'", tline->text);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002281 return NO_DIRECTIVE_FOUND; /* didn't get it */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002282
H. Peter Anvin3f87a2a2016-10-04 14:07:19 -07002283 case PP_PRAGMA:
2284 /*
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07002285 * %pragma namespace options...
2286 *
2287 * The namespace "preproc" is reserved for the preprocessor;
2288 * all other namespaces generate a [pragma] assembly directive.
2289 *
2290 * Invalid %pragmas are ignored and may have different
2291 * meaning in future versions of NASM.
H. Peter Anvin3f87a2a2016-10-04 14:07:19 -07002292 */
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07002293 tline = tline->next;
2294 skip_white_(tline);
2295 tline = expand_smacro(tline);
2296 if (tok_type_(tline, TOK_ID)) {
2297 if (!nasm_stricmp(tline->text, "preproc")) {
2298 /* Preprocessor pragma */
2299 do_pragma_preproc(tline);
2300 } else {
2301 /* Build the assembler directive */
2302 t = new_Token(NULL, TOK_OTHER, "[", 1);
2303 t->next = new_Token(NULL, TOK_ID, "pragma", 6);
2304 t->next->next = new_Token(tline, TOK_WHITESPACE, NULL, 0);
2305 tline = t;
2306 for (t = tline; t->next; t = t->next)
2307 ;
2308 t->next = new_Token(NULL, TOK_OTHER, "]", 1);
2309 /* true here can be revisited in the future */
2310 *output = detoken(tline, true);
2311 }
2312 }
H. Peter Anvin3f87a2a2016-10-04 14:07:19 -07002313 free_tlist(origline);
2314 return DIRECTIVE_FOUND;
2315
H. Peter Anvine2c80182005-01-15 22:15:51 +00002316 case PP_STACKSIZE:
2317 /* Directive to tell NASM what the default stack size is. The
2318 * default is for a 16-bit stack, and this can be overriden with
2319 * %stacksize large.
H. Peter Anvine2c80182005-01-15 22:15:51 +00002320 */
2321 tline = tline->next;
2322 if (tline && tline->type == TOK_WHITESPACE)
2323 tline = tline->next;
2324 if (!tline || tline->type != TOK_ID) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002325 nasm_nonfatal("`%%stacksize' missing size parameter");
H. Peter Anvine2c80182005-01-15 22:15:51 +00002326 free_tlist(origline);
2327 return DIRECTIVE_FOUND;
2328 }
2329 if (nasm_stricmp(tline->text, "flat") == 0) {
2330 /* All subsequent ARG directives are for a 32-bit stack */
2331 StackSize = 4;
2332 StackPointer = "ebp";
2333 ArgOffset = 8;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002334 LocalOffset = 0;
Charles Crayne7eaf9192007-11-08 22:11:14 -08002335 } else if (nasm_stricmp(tline->text, "flat64") == 0) {
2336 /* All subsequent ARG directives are for a 64-bit stack */
2337 StackSize = 8;
2338 StackPointer = "rbp";
Per Jessen53252e02010-02-11 00:16:59 +03002339 ArgOffset = 16;
Charles Crayne7eaf9192007-11-08 22:11:14 -08002340 LocalOffset = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002341 } else if (nasm_stricmp(tline->text, "large") == 0) {
2342 /* All subsequent ARG directives are for a 16-bit stack,
2343 * far function call.
2344 */
2345 StackSize = 2;
2346 StackPointer = "bp";
2347 ArgOffset = 4;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002348 LocalOffset = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002349 } else if (nasm_stricmp(tline->text, "small") == 0) {
2350 /* All subsequent ARG directives are for a 16-bit stack,
2351 * far function call. We don't support near functions.
2352 */
2353 StackSize = 2;
2354 StackPointer = "bp";
2355 ArgOffset = 6;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002356 LocalOffset = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002357 } else {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002358 nasm_nonfatal("`%%stacksize' invalid size type");
H. Peter Anvine2c80182005-01-15 22:15:51 +00002359 free_tlist(origline);
2360 return DIRECTIVE_FOUND;
2361 }
2362 free_tlist(origline);
2363 return DIRECTIVE_FOUND;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002364
H. Peter Anvine2c80182005-01-15 22:15:51 +00002365 case PP_ARG:
2366 /* TASM like ARG directive to define arguments to functions, in
2367 * the following form:
2368 *
2369 * ARG arg1:WORD, arg2:DWORD, arg4:QWORD
2370 */
2371 offset = ArgOffset;
2372 do {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00002373 char *arg, directive[256];
H. Peter Anvine2c80182005-01-15 22:15:51 +00002374 int size = StackSize;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002375
H. Peter Anvine2c80182005-01-15 22:15:51 +00002376 /* Find the argument name */
2377 tline = tline->next;
2378 if (tline && tline->type == TOK_WHITESPACE)
2379 tline = tline->next;
2380 if (!tline || tline->type != TOK_ID) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002381 nasm_nonfatal("`%%arg' missing argument parameter");
H. Peter Anvine2c80182005-01-15 22:15:51 +00002382 free_tlist(origline);
2383 return DIRECTIVE_FOUND;
2384 }
2385 arg = tline->text;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002386
H. Peter Anvine2c80182005-01-15 22:15:51 +00002387 /* Find the argument size type */
2388 tline = tline->next;
2389 if (!tline || tline->type != TOK_OTHER
2390 || tline->text[0] != ':') {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002391 nasm_nonfatal("Syntax error processing `%%arg' directive");
H. Peter Anvine2c80182005-01-15 22:15:51 +00002392 free_tlist(origline);
2393 return DIRECTIVE_FOUND;
2394 }
2395 tline = tline->next;
2396 if (!tline || tline->type != TOK_ID) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002397 nasm_nonfatal("`%%arg' missing size type parameter");
H. Peter Anvine2c80182005-01-15 22:15:51 +00002398 free_tlist(origline);
2399 return DIRECTIVE_FOUND;
2400 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002401
H. Peter Anvine2c80182005-01-15 22:15:51 +00002402 /* Allow macro expansion of type parameter */
Keith Kaniosb7a89542007-04-12 02:40:54 +00002403 tt = tokenize(tline->text);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002404 tt = expand_smacro(tt);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002405 size = parse_size(tt->text);
2406 if (!size) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002407 nasm_nonfatal("Invalid size type for `%%arg' missing directive");
H. Peter Anvine2c80182005-01-15 22:15:51 +00002408 free_tlist(tt);
2409 free_tlist(origline);
2410 return DIRECTIVE_FOUND;
2411 }
2412 free_tlist(tt);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002413
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002414 /* Round up to even stack slots */
2415 size = ALIGN(size, StackSize);
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002416
H. Peter Anvine2c80182005-01-15 22:15:51 +00002417 /* Now define the macro for the argument */
2418 snprintf(directive, sizeof(directive), "%%define %s (%s+%d)",
2419 arg, StackPointer, offset);
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07002420 do_directive(tokenize(directive), output);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002421 offset += size;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002422
H. Peter Anvine2c80182005-01-15 22:15:51 +00002423 /* Move to the next argument in the list */
2424 tline = tline->next;
2425 if (tline && tline->type == TOK_WHITESPACE)
2426 tline = tline->next;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002427 } while (tline && tline->type == TOK_OTHER && tline->text[0] == ',');
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002428 ArgOffset = offset;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002429 free_tlist(origline);
2430 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002431
H. Peter Anvine2c80182005-01-15 22:15:51 +00002432 case PP_LOCAL:
2433 /* TASM like LOCAL directive to define local variables for a
2434 * function, in the following form:
2435 *
2436 * LOCAL local1:WORD, local2:DWORD, local4:QWORD = LocalSize
2437 *
2438 * The '= LocalSize' at the end is ignored by NASM, but is
2439 * required by TASM to define the local parameter size (and used
2440 * by the TASM macro package).
2441 */
2442 offset = LocalOffset;
2443 do {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00002444 char *local, directive[256];
H. Peter Anvine2c80182005-01-15 22:15:51 +00002445 int size = StackSize;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002446
H. Peter Anvine2c80182005-01-15 22:15:51 +00002447 /* Find the argument name */
2448 tline = tline->next;
2449 if (tline && tline->type == TOK_WHITESPACE)
2450 tline = tline->next;
2451 if (!tline || tline->type != TOK_ID) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002452 nasm_nonfatal("`%%local' missing argument parameter");
H. Peter Anvine2c80182005-01-15 22:15:51 +00002453 free_tlist(origline);
2454 return DIRECTIVE_FOUND;
2455 }
2456 local = tline->text;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002457
H. Peter Anvine2c80182005-01-15 22:15:51 +00002458 /* Find the argument size type */
2459 tline = tline->next;
2460 if (!tline || tline->type != TOK_OTHER
2461 || tline->text[0] != ':') {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002462 nasm_nonfatal("Syntax error processing `%%local' directive");
H. Peter Anvine2c80182005-01-15 22:15:51 +00002463 free_tlist(origline);
2464 return DIRECTIVE_FOUND;
2465 }
2466 tline = tline->next;
2467 if (!tline || tline->type != TOK_ID) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002468 nasm_nonfatal("`%%local' missing size type parameter");
H. Peter Anvine2c80182005-01-15 22:15:51 +00002469 free_tlist(origline);
2470 return DIRECTIVE_FOUND;
2471 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00002472
H. Peter Anvine2c80182005-01-15 22:15:51 +00002473 /* Allow macro expansion of type parameter */
Keith Kaniosb7a89542007-04-12 02:40:54 +00002474 tt = tokenize(tline->text);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002475 tt = expand_smacro(tt);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002476 size = parse_size(tt->text);
2477 if (!size) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002478 nasm_nonfatal("Invalid size type for `%%local' missing directive");
H. Peter Anvine2c80182005-01-15 22:15:51 +00002479 free_tlist(tt);
2480 free_tlist(origline);
2481 return DIRECTIVE_FOUND;
2482 }
2483 free_tlist(tt);
H. Peter Anvin734b1882002-04-30 21:01:08 +00002484
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002485 /* Round up to even stack slots */
2486 size = ALIGN(size, StackSize);
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002487
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002488 offset += size; /* Negative offset, increment before */
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002489
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002490 /* Now define the macro for the argument */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002491 snprintf(directive, sizeof(directive), "%%define %s (%s-%d)",
2492 local, StackPointer, offset);
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07002493 do_directive(tokenize(directive), output);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002494
H. Peter Anvine2c80182005-01-15 22:15:51 +00002495 /* Now define the assign to setup the enter_c macro correctly */
2496 snprintf(directive, sizeof(directive),
2497 "%%assign %%$localsize %%$localsize+%d", size);
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07002498 do_directive(tokenize(directive), output);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002499
H. Peter Anvine2c80182005-01-15 22:15:51 +00002500 /* Move to the next argument in the list */
2501 tline = tline->next;
2502 if (tline && tline->type == TOK_WHITESPACE)
2503 tline = tline->next;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002504 } while (tline && tline->type == TOK_OTHER && tline->text[0] == ',');
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002505 LocalOffset = offset;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002506 free_tlist(origline);
2507 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002508
H. Peter Anvine2c80182005-01-15 22:15:51 +00002509 case PP_CLEAR:
2510 if (tline->next)
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08002511 nasm_warn(WARN_OTHER, "trailing garbage after `%%clear' ignored");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002512 free_macros();
2513 init_macros();
H. Peter Anvine2c80182005-01-15 22:15:51 +00002514 free_tlist(origline);
2515 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002516
H. Peter Anvin418ca702008-05-30 10:42:30 -07002517 case PP_DEPEND:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002518 t = tline->next = expand_smacro(tline->next);
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002519 skip_white_(t);
2520 if (!t || (t->type != TOK_STRING &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002521 t->type != TOK_INTERNAL_STRING)) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002522 nasm_nonfatal("`%%depend' expects a file name");
H. Peter Anvin418ca702008-05-30 10:42:30 -07002523 free_tlist(origline);
2524 return DIRECTIVE_FOUND; /* but we did _something_ */
2525 }
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002526 if (t->next)
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08002527 nasm_warn(WARN_OTHER, "trailing garbage after `%%depend' ignored");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002528 p = t->text;
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002529 if (t->type != TOK_INTERNAL_STRING)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002530 nasm_unquote_cstr(p, i);
Cyrill Gorcunovb7bb5ac2018-11-11 21:33:52 +03002531 strlist_add(deplist, p);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002532 free_tlist(origline);
H. Peter Anvin418ca702008-05-30 10:42:30 -07002533 return DIRECTIVE_FOUND;
2534
2535 case PP_INCLUDE:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002536 t = tline->next = expand_smacro(tline->next);
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002537 skip_white_(t);
H. Peter Anvind2456592008-06-19 15:04:18 -07002538
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002539 if (!t || (t->type != TOK_STRING &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002540 t->type != TOK_INTERNAL_STRING)) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002541 nasm_nonfatal("`%%include' expects a file name");
H. Peter Anvine2c80182005-01-15 22:15:51 +00002542 free_tlist(origline);
2543 return DIRECTIVE_FOUND; /* but we did _something_ */
2544 }
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002545 if (t->next)
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08002546 nasm_warn(WARN_OTHER, "trailing garbage after `%%include' ignored");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002547 p = t->text;
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002548 if (t->type != TOK_INTERNAL_STRING)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002549 nasm_unquote_cstr(p, i);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002550 inc = nasm_malloc(sizeof(Include));
H. Peter Anvine2c80182005-01-15 22:15:51 +00002551 inc->next = istk;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002552 inc->conds = NULL;
Jim Kukunas65a8afc2016-06-13 16:00:42 -04002553 found_path = NULL;
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07002554 inc->fp = inc_fopen(p, deplist, &found_path,
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08002555 (pp_mode == PP_DEPS)
2556 ? INC_OPTIONAL : INC_NEEDED, NF_TEXT);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002557 if (!inc->fp) {
2558 /* -MG given but file not found */
2559 nasm_free(inc);
2560 } else {
Jim Kukunas65a8afc2016-06-13 16:00:42 -04002561 inc->fname = src_set_fname(found_path ? found_path : p);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002562 inc->lineno = src_set_linnum(0);
2563 inc->lineinc = 1;
2564 inc->expansion = NULL;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002565 inc->mstk = NULL;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002566 istk = inc;
H. Peter Anvin8ac25aa2016-02-18 01:16:18 -08002567 lfmt->uplevel(LIST_INCLUDE);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002568 }
2569 free_tlist(origline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002570 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002571
H. Peter Anvind2456592008-06-19 15:04:18 -07002572 case PP_USE:
H. Peter Anvinf4ae5ad2008-06-19 18:39:24 -07002573 {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002574 static macros_t *use_pkg;
2575 const char *pkg_macro = NULL;
H. Peter Anvinf4ae5ad2008-06-19 18:39:24 -07002576
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002577 tline = tline->next;
2578 skip_white_(tline);
2579 tline = expand_id(tline);
H. Peter Anvind2456592008-06-19 15:04:18 -07002580
H. Peter Anvin264b7b92008-10-24 16:38:17 -07002581 if (!tline || (tline->type != TOK_STRING &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002582 tline->type != TOK_INTERNAL_STRING &&
2583 tline->type != TOK_ID)) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002584 nasm_nonfatal("`%%use' expects a package name");
H. Peter Anvind2456592008-06-19 15:04:18 -07002585 free_tlist(origline);
2586 return DIRECTIVE_FOUND; /* but we did _something_ */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002587 }
H. Peter Anvin264b7b92008-10-24 16:38:17 -07002588 if (tline->next)
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08002589 nasm_warn(WARN_OTHER, "trailing garbage after `%%use' ignored");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002590 if (tline->type == TOK_STRING)
2591 nasm_unquote_cstr(tline->text, i);
2592 use_pkg = nasm_stdmac_find_package(tline->text);
2593 if (!use_pkg)
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002594 nasm_nonfatal("unknown `%%use' package: %s", tline->text);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002595 else
2596 pkg_macro = (char *)use_pkg + 1; /* The first string will be <%define>__USE_*__ */
Victor van den Elzen35eb2ea2010-03-10 22:33:48 +01002597 if (use_pkg && ! smacro_defined(NULL, pkg_macro, 0, NULL, true)) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002598 /* Not already included, go ahead and include it */
2599 stdmacpos = use_pkg;
2600 }
2601 free_tlist(origline);
H. Peter Anvind2456592008-06-19 15:04:18 -07002602 return DIRECTIVE_FOUND;
H. Peter Anvinf4ae5ad2008-06-19 18:39:24 -07002603 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002604 case PP_PUSH:
H. Peter Anvine2c80182005-01-15 22:15:51 +00002605 case PP_REPL:
H. Peter Anvin42b56392008-10-24 16:24:21 -07002606 case PP_POP:
H. Peter Anvine2c80182005-01-15 22:15:51 +00002607 tline = tline->next;
2608 skip_white_(tline);
2609 tline = expand_id(tline);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002610 if (tline) {
2611 if (!tok_type_(tline, TOK_ID)) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002612 nasm_nonfatal("`%s' expects a context identifier",
2613 pp_directives[i]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002614 free_tlist(origline);
2615 return DIRECTIVE_FOUND; /* but we did _something_ */
2616 }
2617 if (tline->next)
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08002618 nasm_warn(WARN_OTHER, "trailing garbage after `%s' ignored",
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002619 pp_directives[i]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002620 p = nasm_strdup(tline->text);
2621 } else {
2622 p = NULL; /* Anonymous */
2623 }
H. Peter Anvin42b56392008-10-24 16:24:21 -07002624
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002625 if (i == PP_PUSH) {
H. Peter Anvin (Intel)ebb05a02018-12-11 12:30:25 -08002626 nasm_new(ctx);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002627 ctx->next = cstk;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002628 ctx->name = p;
2629 ctx->number = unique++;
2630 cstk = ctx;
2631 } else {
2632 /* %pop or %repl */
2633 if (!cstk) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002634 nasm_nonfatal("`%s': context stack is empty",
2635 pp_directives[i]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002636 } else if (i == PP_POP) {
2637 if (p && (!cstk->name || nasm_stricmp(p, cstk->name)))
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002638 nasm_nonfatal("`%%pop' in wrong context: %s, expected %s",
2639 cstk->name ? cstk->name : "anonymous", p);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002640 else
2641 ctx_pop();
2642 } else {
2643 /* i == PP_REPL */
2644 nasm_free(cstk->name);
2645 cstk->name = p;
2646 p = NULL;
2647 }
2648 nasm_free(p);
2649 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002650 free_tlist(origline);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002651 return DIRECTIVE_FOUND;
H. Peter Anvin8e3f75e2008-09-24 00:21:58 -07002652 case PP_FATAL:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002653 severity = ERR_FATAL;
2654 goto issue_error;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002655 case PP_ERROR:
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08002656 severity = ERR_NONFATAL|ERR_PASS2;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002657 goto issue_error;
H. Peter Anvin7df04172008-06-10 18:27:38 -07002658 case PP_WARNING:
H. Peter Anvin (Intel)723ab482018-12-13 21:53:31 -08002659 /*!
2660 *!user [on] %warning directives
2661 *! controls output of \c{%warning} directives (see \k{pperror}).
2662 */
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08002663 severity = ERR_WARNING|WARN_USER|ERR_PASS2;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002664 goto issue_error;
H. Peter Anvin8e3f75e2008-09-24 00:21:58 -07002665
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002666issue_error:
H. Peter Anvin7df04172008-06-10 18:27:38 -07002667 {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002668 /* Only error out if this is the final pass */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002669 tline->next = expand_smacro(tline->next);
2670 tline = tline->next;
2671 skip_white_(tline);
2672 t = tline ? tline->next : NULL;
2673 skip_white_(t);
2674 if (tok_type_(tline, TOK_STRING) && !t) {
2675 /* The line contains only a quoted string */
2676 p = tline->text;
2677 nasm_unquote(p, NULL); /* Ignore NUL character truncation */
H. Peter Anvin130736c2016-02-17 20:27:41 -08002678 nasm_error(severity, "%s", p);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002679 } else {
2680 /* Not a quoted string, or more than a quoted string */
2681 p = detoken(tline, false);
H. Peter Anvin130736c2016-02-17 20:27:41 -08002682 nasm_error(severity, "%s", p);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002683 nasm_free(p);
2684 }
2685 free_tlist(origline);
2686 return DIRECTIVE_FOUND;
H. Peter Anvin7df04172008-06-10 18:27:38 -07002687 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00002688
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002689 CASE_PP_IF:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002690 if (istk->conds && !emitting(istk->conds->state))
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002691 j = COND_NEVER;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002692 else {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002693 j = if_condition(tline->next, i);
2694 tline->next = NULL; /* it got freed */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002695 j = j < 0 ? COND_NEVER : j ? COND_IF_TRUE : COND_IF_FALSE;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002696 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002697 cond = nasm_malloc(sizeof(Cond));
2698 cond->next = istk->conds;
2699 cond->state = j;
2700 istk->conds = cond;
2701 if(istk->mstk)
2702 istk->mstk->condcnt ++;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002703 free_tlist(origline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002704 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002705
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002706 CASE_PP_ELIF:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002707 if (!istk->conds)
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002708 nasm_fatal("`%s': no matching `%%if'", pp_directives[i]);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002709 switch(istk->conds->state) {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002710 case COND_IF_TRUE:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002711 istk->conds->state = COND_DONE;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002712 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002713
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002714 case COND_DONE:
2715 case COND_NEVER:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002716 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002717
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002718 case COND_ELSE_TRUE:
2719 case COND_ELSE_FALSE:
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08002720 nasm_warn(WARN_OTHER|ERR_PP_PRECOND,
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002721 "`%%elif' after `%%else' ignored");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002722 istk->conds->state = COND_NEVER;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002723 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002724
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002725 case COND_IF_FALSE:
2726 /*
2727 * IMPORTANT: In the case of %if, we will already have
2728 * called expand_mmac_params(); however, if we're
2729 * processing an %elif we must have been in a
2730 * non-emitting mode, which would have inhibited
2731 * the normal invocation of expand_mmac_params().
2732 * Therefore, we have to do it explicitly here.
2733 */
2734 j = if_condition(expand_mmac_params(tline->next), i);
2735 tline->next = NULL; /* it got freed */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002736 istk->conds->state =
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002737 j < 0 ? COND_NEVER : j ? COND_IF_TRUE : COND_IF_FALSE;
2738 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002739 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002740 free_tlist(origline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002741 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002742
H. Peter Anvine2c80182005-01-15 22:15:51 +00002743 case PP_ELSE:
2744 if (tline->next)
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08002745 nasm_warn(WARN_OTHER|ERR_PP_PRECOND,
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002746 "trailing garbage after `%%else' ignored");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002747 if (!istk->conds)
H. Peter Anvinc5136902018-06-15 18:20:17 -07002748 nasm_fatal("`%%else: no matching `%%if'");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002749 switch(istk->conds->state) {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002750 case COND_IF_TRUE:
2751 case COND_DONE:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002752 istk->conds->state = COND_ELSE_FALSE;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002753 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002754
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002755 case COND_NEVER:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002756 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002757
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002758 case COND_IF_FALSE:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002759 istk->conds->state = COND_ELSE_TRUE;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002760 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002761
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002762 case COND_ELSE_TRUE:
2763 case COND_ELSE_FALSE:
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08002764 nasm_warn(WARN_OTHER|ERR_PP_PRECOND,
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002765 "`%%else' after `%%else' ignored.");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002766 istk->conds->state = COND_NEVER;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002767 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002768 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002769 free_tlist(origline);
2770 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002771
H. Peter Anvine2c80182005-01-15 22:15:51 +00002772 case PP_ENDIF:
2773 if (tline->next)
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08002774 nasm_warn(WARN_OTHER|ERR_PP_PRECOND,
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002775 "trailing garbage after `%%endif' ignored");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002776 if (!istk->conds)
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002777 nasm_fatal("`%%endif': no matching `%%if'");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002778 cond = istk->conds;
2779 istk->conds = cond->next;
2780 nasm_free(cond);
2781 if(istk->mstk)
2782 istk->mstk->condcnt --;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002783 free_tlist(origline);
2784 return DIRECTIVE_FOUND;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002785
H. Peter Anvindb8f96e2009-07-15 09:07:29 -04002786 case PP_RMACRO:
2787 case PP_IRMACRO:
H. Peter Anvine2c80182005-01-15 22:15:51 +00002788 case PP_MACRO:
2789 case PP_IMACRO:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002790 if (defining) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002791 nasm_fatal("`%s': already defining a macro",
2792 pp_directives[i]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002793 return DIRECTIVE_FOUND;
2794 }
H. Peter Anvin4def1a82016-05-09 13:59:44 -07002795 defining = nasm_zalloc(sizeof(MMacro));
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07002796 defining->max_depth = ((i == PP_RMACRO) || (i == PP_IRMACRO))
2797 ? nasm_limit[LIMIT_MACROS] : 0;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002798 defining->casesense = (i == PP_MACRO) || (i == PP_RMACRO);
2799 if (!parse_mmacro_spec(tline, defining, pp_directives[i])) {
2800 nasm_free(defining);
2801 defining = NULL;
2802 return DIRECTIVE_FOUND;
2803 }
H. Peter Anvina26433d2008-07-16 14:40:01 -07002804
H. Peter Anvin4def1a82016-05-09 13:59:44 -07002805 src_get(&defining->xline, &defining->fname);
2806
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002807 mmac = (MMacro *) hash_findix(&mmacros, defining->name);
2808 while (mmac) {
2809 if (!strcmp(mmac->name, defining->name) &&
2810 (mmac->nparam_min <= defining->nparam_max
2811 || defining->plus)
2812 && (defining->nparam_min <= mmac->nparam_max
2813 || mmac->plus)) {
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08002814 nasm_warn(WARN_OTHER, "redefining multi-line macro `%s'",
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002815 defining->name);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002816 return DIRECTIVE_FOUND;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002817 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002818 mmac = mmac->next;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002819 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002820 free_tlist(origline);
2821 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002822
H. Peter Anvine2c80182005-01-15 22:15:51 +00002823 case PP_ENDM:
2824 case PP_ENDMACRO:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002825 if (! (defining && defining->name)) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002826 nasm_nonfatal("`%s': not defining a macro", tline->text);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002827 return DIRECTIVE_FOUND;
2828 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002829 mmhead = (MMacro **) hash_findi_add(&mmacros, defining->name);
2830 defining->next = *mmhead;
2831 *mmhead = defining;
2832 defining = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002833 free_tlist(origline);
2834 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002835
H. Peter Anvin89cee572009-07-15 09:16:54 -04002836 case PP_EXITMACRO:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002837 /*
2838 * We must search along istk->expansion until we hit a
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002839 * macro-end marker for a macro with a name. Then we
2840 * bypass all lines between exitmacro and endmacro.
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002841 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002842 list_for_each(l, istk->expansion)
2843 if (l->finishes && l->finishes->name)
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002844 break;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002845
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002846 if (l) {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002847 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002848 * Remove all conditional entries relative to this
2849 * macro invocation. (safe to do in this context)
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002850 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002851 for ( ; l->finishes->condcnt > 0; l->finishes->condcnt --) {
2852 cond = istk->conds;
2853 istk->conds = cond->next;
2854 nasm_free(cond);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002855 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002856 istk->expansion = l;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002857 } else {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002858 nasm_nonfatal("`%%exitmacro' not within `%%macro' block");
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002859 }
Keith Kanios852f1ee2009-07-12 00:19:55 -05002860 free_tlist(origline);
2861 return DIRECTIVE_FOUND;
2862
H. Peter Anvina26433d2008-07-16 14:40:01 -07002863 case PP_UNMACRO:
2864 case PP_UNIMACRO:
2865 {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002866 MMacro **mmac_p;
2867 MMacro spec;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002868
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002869 spec.casesense = (i == PP_UNMACRO);
2870 if (!parse_mmacro_spec(tline, &spec, pp_directives[i])) {
2871 return DIRECTIVE_FOUND;
2872 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002873 mmac_p = (MMacro **) hash_findi(&mmacros, spec.name, NULL);
2874 while (mmac_p && *mmac_p) {
2875 mmac = *mmac_p;
2876 if (mmac->casesense == spec.casesense &&
2877 !mstrcmp(mmac->name, spec.name, spec.casesense) &&
2878 mmac->nparam_min == spec.nparam_min &&
2879 mmac->nparam_max == spec.nparam_max &&
2880 mmac->plus == spec.plus) {
2881 *mmac_p = mmac->next;
2882 free_mmacro(mmac);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002883 } else {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002884 mmac_p = &mmac->next;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002885 }
2886 }
2887 free_tlist(origline);
2888 free_tlist(spec.dlist);
2889 return DIRECTIVE_FOUND;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002890 }
2891
H. Peter Anvine2c80182005-01-15 22:15:51 +00002892 case PP_ROTATE:
2893 if (tline->next && tline->next->type == TOK_WHITESPACE)
2894 tline = tline->next;
H. Peter Anvin89cee572009-07-15 09:16:54 -04002895 if (!tline->next) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002896 free_tlist(origline);
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002897 nasm_nonfatal("`%%rotate' missing rotate count");
H. Peter Anvine2c80182005-01-15 22:15:51 +00002898 return DIRECTIVE_FOUND;
2899 }
2900 t = expand_smacro(tline->next);
2901 tline->next = NULL;
2902 free_tlist(origline);
2903 tline = t;
2904 tptr = &t;
2905 tokval.t_type = TOKEN_INVALID;
2906 evalresult =
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08002907 evaluate(ppscan, tptr, &tokval, NULL, true, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002908 free_tlist(tline);
2909 if (!evalresult)
2910 return DIRECTIVE_FOUND;
2911 if (tokval.t_type)
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08002912 nasm_warn(WARN_OTHER, "trailing garbage after expression ignored");
H. Peter Anvine2c80182005-01-15 22:15:51 +00002913 if (!is_simple(evalresult)) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002914 nasm_nonfatal("non-constant value given to `%%rotate'");
H. Peter Anvine2c80182005-01-15 22:15:51 +00002915 return DIRECTIVE_FOUND;
2916 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002917 mmac = istk->mstk;
2918 while (mmac && !mmac->name) /* avoid mistaking %reps for macros */
2919 mmac = mmac->next_active;
2920 if (!mmac) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002921 nasm_nonfatal("`%%rotate' invoked outside a macro call");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002922 } else if (mmac->nparam == 0) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002923 nasm_nonfatal("`%%rotate' invoked within macro without parameters");
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002924 } else {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002925 int rotate = mmac->rotate + reloc_value(evalresult);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002926
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002927 rotate %= (int)mmac->nparam;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002928 if (rotate < 0)
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002929 rotate += mmac->nparam;
2930
2931 mmac->rotate = rotate;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002932 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002933 return DIRECTIVE_FOUND;
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00002934
H. Peter Anvine2c80182005-01-15 22:15:51 +00002935 case PP_REP:
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002936 nolist = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002937 do {
2938 tline = tline->next;
2939 } while (tok_type_(tline, TOK_WHITESPACE));
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00002940
H. Peter Anvine2c80182005-01-15 22:15:51 +00002941 if (tok_type_(tline, TOK_ID) &&
2942 nasm_stricmp(tline->text, ".nolist") == 0) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002943 nolist = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002944 do {
2945 tline = tline->next;
2946 } while (tok_type_(tline, TOK_WHITESPACE));
2947 }
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00002948
H. Peter Anvine2c80182005-01-15 22:15:51 +00002949 if (tline) {
2950 t = expand_smacro(tline);
2951 tptr = &t;
2952 tokval.t_type = TOKEN_INVALID;
2953 evalresult =
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08002954 evaluate(ppscan, tptr, &tokval, NULL, true, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002955 if (!evalresult) {
2956 free_tlist(origline);
2957 return DIRECTIVE_FOUND;
2958 }
2959 if (tokval.t_type)
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08002960 nasm_warn(WARN_OTHER, "trailing garbage after expression ignored");
H. Peter Anvine2c80182005-01-15 22:15:51 +00002961 if (!is_simple(evalresult)) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002962 nasm_nonfatal("non-constant value given to `%%rep'");
H. Peter Anvine2c80182005-01-15 22:15:51 +00002963 return DIRECTIVE_FOUND;
2964 }
Cyrill Gorcunove091d6e2010-08-09 13:58:22 +04002965 count = reloc_value(evalresult);
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07002966 if (count > nasm_limit[LIMIT_REP]) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002967 nasm_nonfatal("`%%rep' count %"PRId64" exceeds limit (currently %"PRId64")",
2968 count, nasm_limit[LIMIT_REP]);
Cyrill Gorcunove091d6e2010-08-09 13:58:22 +04002969 count = 0;
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07002970 } else if (count < 0) {
H. Peter Anvin (Intel)723ab482018-12-13 21:53:31 -08002971 /*!
2972 *!negative-rep [on] regative %rep count
2973 *! warns about negative counts given to the \c{%rep}
2974 *! preprocessor directive.
2975 */
H. Peter Anvin (Intel)80c4f232018-12-14 13:33:24 -08002976 nasm_warn(ERR_PASS2|WARN_NEGATIVE_REP,
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07002977 "negative `%%rep' count: %"PRId64, count);
2978 count = 0;
2979 } else {
Cyrill Gorcunove091d6e2010-08-09 13:58:22 +04002980 count++;
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07002981 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002982 } else {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002983 nasm_nonfatal("`%%rep' expects a repeat count");
H. Peter Anvinf8ba53e2007-10-11 10:11:57 -07002984 count = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002985 }
2986 free_tlist(origline);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002987
2988 tmp_defining = defining;
2989 defining = nasm_malloc(sizeof(MMacro));
2990 defining->prev = NULL;
2991 defining->name = NULL; /* flags this macro as a %rep block */
2992 defining->casesense = false;
2993 defining->plus = false;
2994 defining->nolist = nolist;
2995 defining->in_progress = count;
2996 defining->max_depth = 0;
2997 defining->nparam_min = defining->nparam_max = 0;
2998 defining->defaults = NULL;
2999 defining->dlist = NULL;
3000 defining->expansion = NULL;
3001 defining->next_active = istk->mstk;
3002 defining->rep_nest = tmp_defining;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003003 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003004
H. Peter Anvine2c80182005-01-15 22:15:51 +00003005 case PP_ENDREP:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003006 if (!defining || defining->name) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003007 nasm_nonfatal("`%%endrep': no matching `%%rep'");
H. Peter Anvine2c80182005-01-15 22:15:51 +00003008 return DIRECTIVE_FOUND;
3009 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003010
H. Peter Anvine2c80182005-01-15 22:15:51 +00003011 /*
3012 * Now we have a "macro" defined - although it has no name
3013 * and we won't be entering it in the hash tables - we must
3014 * push a macro-end marker for it on to istk->expansion.
3015 * After that, it will take care of propagating itself (a
3016 * macro-end marker line for a macro which is really a %rep
3017 * block will cause the macro to be re-expanded, complete
3018 * with another macro-end marker to ensure the process
3019 * continues) until the whole expansion is forcibly removed
3020 * from istk->expansion by a %exitrep.
3021 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003022 l = nasm_malloc(sizeof(Line));
3023 l->next = istk->expansion;
3024 l->finishes = defining;
3025 l->first = NULL;
3026 istk->expansion = l;
3027
3028 istk->mstk = defining;
3029
H. Peter Anvin8ac25aa2016-02-18 01:16:18 -08003030 lfmt->uplevel(defining->nolist ? LIST_MACRO_NOLIST : LIST_MACRO);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003031 tmp_defining = defining;
3032 defining = defining->rep_nest;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003033 free_tlist(origline);
3034 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003035
H. Peter Anvine2c80182005-01-15 22:15:51 +00003036 case PP_EXITREP:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003037 /*
3038 * We must search along istk->expansion until we hit a
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003039 * macro-end marker for a macro with no name. Then we set
3040 * its `in_progress' flag to 0.
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003041 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003042 list_for_each(l, istk->expansion)
3043 if (l->finishes && !l->finishes->name)
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003044 break;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003045
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003046 if (l)
3047 l->finishes->in_progress = 1;
3048 else
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003049 nasm_nonfatal("`%%exitrep' not within `%%rep' block");
H. Peter Anvine2c80182005-01-15 22:15:51 +00003050 free_tlist(origline);
3051 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003052
H. Peter Anvine2c80182005-01-15 22:15:51 +00003053 case PP_XDEFINE:
3054 case PP_IXDEFINE:
3055 case PP_DEFINE:
3056 case PP_IDEFINE:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003057 casesense = (i == PP_DEFINE || i == PP_XDEFINE);
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07003058
H. Peter Anvine2c80182005-01-15 22:15:51 +00003059 tline = tline->next;
3060 skip_white_(tline);
3061 tline = expand_id(tline);
3062 if (!tline || (tline->type != TOK_ID &&
3063 (tline->type != TOK_PREPROC_ID ||
3064 tline->text[1] != '$'))) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003065 nasm_nonfatal("`%s' expects a macro identifier",
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003066 pp_directives[i]);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003067 free_tlist(origline);
3068 return DIRECTIVE_FOUND;
3069 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003070
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04003071 ctx = get_ctx(tline->text, &mname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003072 last = tline;
3073 param_start = tline = tline->next;
3074 nparam = 0;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003075
H. Peter Anvine2c80182005-01-15 22:15:51 +00003076 /* Expand the macro definition now for %xdefine and %ixdefine */
3077 if ((i == PP_XDEFINE) || (i == PP_IXDEFINE))
3078 tline = expand_smacro(tline);
H. Peter Anvin734b1882002-04-30 21:01:08 +00003079
H. Peter Anvine2c80182005-01-15 22:15:51 +00003080 if (tok_is_(tline, "(")) {
3081 /*
3082 * This macro has parameters.
3083 */
H. Peter Anvin734b1882002-04-30 21:01:08 +00003084
H. Peter Anvine2c80182005-01-15 22:15:51 +00003085 tline = tline->next;
3086 while (1) {
3087 skip_white_(tline);
3088 if (!tline) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003089 nasm_nonfatal("parameter identifier expected");
H. Peter Anvine2c80182005-01-15 22:15:51 +00003090 free_tlist(origline);
3091 return DIRECTIVE_FOUND;
3092 }
3093 if (tline->type != TOK_ID) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003094 nasm_nonfatal("`%s': parameter identifier expected",
3095 tline->text);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003096 free_tlist(origline);
3097 return DIRECTIVE_FOUND;
3098 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003099 tline->type = TOK_SMAC_PARAM + nparam++;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003100 tline = tline->next;
3101 skip_white_(tline);
3102 if (tok_is_(tline, ",")) {
3103 tline = tline->next;
H. Peter Anvinca348b62008-07-23 10:49:26 -04003104 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003105 if (!tok_is_(tline, ")")) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003106 nasm_nonfatal("`)' expected to terminate macro template");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003107 free_tlist(origline);
3108 return DIRECTIVE_FOUND;
3109 }
3110 break;
3111 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00003112 }
3113 last = tline;
3114 tline = tline->next;
3115 }
3116 if (tok_type_(tline, TOK_WHITESPACE))
3117 last = tline, tline = tline->next;
3118 macro_start = NULL;
3119 last->next = NULL;
3120 t = tline;
3121 while (t) {
3122 if (t->type == TOK_ID) {
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04003123 list_for_each(tt, param_start)
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003124 if (tt->type >= TOK_SMAC_PARAM &&
H. Peter Anvine2c80182005-01-15 22:15:51 +00003125 !strcmp(tt->text, t->text))
3126 t->type = tt->type;
3127 }
3128 tt = t->next;
3129 t->next = macro_start;
3130 macro_start = t;
3131 t = tt;
3132 }
3133 /*
3134 * Good. We now have a macro name, a parameter count, and a
3135 * token list (in reverse order) for an expansion. We ought
3136 * to be OK just to create an SMacro, store it, and let
3137 * free_tlist have the rest of the line (which we have
3138 * carefully re-terminated after chopping off the expansion
3139 * from the end).
3140 */
H. Peter Anvin4db5a162007-10-11 13:42:09 -07003141 define_smacro(ctx, mname, casesense, nparam, macro_start);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003142 free_tlist(origline);
3143 return DIRECTIVE_FOUND;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003144
H. Peter Anvine2c80182005-01-15 22:15:51 +00003145 case PP_UNDEF:
3146 tline = tline->next;
3147 skip_white_(tline);
3148 tline = expand_id(tline);
3149 if (!tline || (tline->type != TOK_ID &&
3150 (tline->type != TOK_PREPROC_ID ||
3151 tline->text[1] != '$'))) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003152 nasm_nonfatal("`%%undef' expects a macro identifier");
H. Peter Anvine2c80182005-01-15 22:15:51 +00003153 free_tlist(origline);
3154 return DIRECTIVE_FOUND;
3155 }
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003156 if (tline->next)
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08003157 nasm_warn(WARN_OTHER, "trailing garbage after macro name ignored");
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003158
H. Peter Anvine2c80182005-01-15 22:15:51 +00003159 /* Find the context that symbol belongs to */
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04003160 ctx = get_ctx(tline->text, &mname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003161 undef_smacro(ctx, mname);
3162 free_tlist(origline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003163 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003164
H. Peter Anvin9e200162008-06-04 17:23:14 -07003165 case PP_DEFSTR:
3166 case PP_IDEFSTR:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003167 casesense = (i == PP_DEFSTR);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003168
3169 tline = tline->next;
3170 skip_white_(tline);
3171 tline = expand_id(tline);
3172 if (!tline || (tline->type != TOK_ID &&
3173 (tline->type != TOK_PREPROC_ID ||
3174 tline->text[1] != '$'))) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003175 nasm_nonfatal("`%s' expects a macro identifier",
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003176 pp_directives[i]);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003177 free_tlist(origline);
3178 return DIRECTIVE_FOUND;
3179 }
3180
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04003181 ctx = get_ctx(tline->text, &mname);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003182 last = tline;
3183 tline = expand_smacro(tline->next);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003184 last->next = NULL;
H. Peter Anvin9e200162008-06-04 17:23:14 -07003185
3186 while (tok_type_(tline, TOK_WHITESPACE))
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003187 tline = delete_Token(tline);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003188
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003189 p = detoken(tline, false);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003190 macro_start = nasm_malloc(sizeof(*macro_start));
3191 macro_start->next = NULL;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003192 macro_start->text = nasm_quote(p, strlen(p));
3193 macro_start->type = TOK_STRING;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003194 macro_start->a.mac = NULL;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003195 nasm_free(p);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003196
3197 /*
3198 * We now have a macro name, an implicit parameter count of
3199 * zero, and a string token to use as an expansion. Create
3200 * and store an SMacro.
3201 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003202 define_smacro(ctx, mname, casesense, 0, macro_start);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003203 free_tlist(origline);
3204 return DIRECTIVE_FOUND;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003205
H. Peter Anvin2f55bda2009-07-14 15:04:04 -04003206 case PP_DEFTOK:
3207 case PP_IDEFTOK:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003208 casesense = (i == PP_DEFTOK);
3209
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003210 tline = tline->next;
3211 skip_white_(tline);
3212 tline = expand_id(tline);
3213 if (!tline || (tline->type != TOK_ID &&
3214 (tline->type != TOK_PREPROC_ID ||
3215 tline->text[1] != '$'))) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003216 nasm_nonfatal("`%s' expects a macro identifier as first parameter",
3217 pp_directives[i]);
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003218 free_tlist(origline);
3219 return DIRECTIVE_FOUND;
3220 }
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04003221 ctx = get_ctx(tline->text, &mname);
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003222 last = tline;
3223 tline = expand_smacro(tline->next);
3224 last->next = NULL;
3225
3226 t = tline;
3227 while (tok_type_(t, TOK_WHITESPACE))
3228 t = t->next;
3229 /* t should now point to the string */
Cyrill Gorcunov6908e582010-09-06 19:36:15 +04003230 if (!tok_type_(t, TOK_STRING)) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003231 nasm_nonfatal("`%s` requires string as second parameter",
3232 pp_directives[i]);
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003233 free_tlist(tline);
3234 free_tlist(origline);
3235 return DIRECTIVE_FOUND;
3236 }
3237
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04003238 /*
3239 * Convert the string to a token stream. Note that smacros
3240 * are stored with the token stream reversed, so we have to
3241 * reverse the output of tokenize().
3242 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003243 nasm_unquote_cstr(t->text, i);
H. Peter Anvinb40992c2010-09-15 08:57:21 -07003244 macro_start = reverse_tokens(tokenize(t->text));
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003245
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003246 /*
3247 * We now have a macro name, an implicit parameter count of
3248 * zero, and a numeric token to use as an expansion. Create
3249 * and store an SMacro.
3250 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003251 define_smacro(ctx, mname, casesense, 0, macro_start);
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003252 free_tlist(tline);
3253 free_tlist(origline);
3254 return DIRECTIVE_FOUND;
H. Peter Anvin9e200162008-06-04 17:23:14 -07003255
H. Peter Anvin418ca702008-05-30 10:42:30 -07003256 case PP_PATHSEARCH:
3257 {
H. Peter Anvinccad6f92016-10-04 00:34:35 -07003258 const char *found_path;
H. Peter Anvin418ca702008-05-30 10:42:30 -07003259
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003260 casesense = true;
H. Peter Anvin418ca702008-05-30 10:42:30 -07003261
3262 tline = tline->next;
3263 skip_white_(tline);
3264 tline = expand_id(tline);
3265 if (!tline || (tline->type != TOK_ID &&
3266 (tline->type != TOK_PREPROC_ID ||
3267 tline->text[1] != '$'))) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003268 nasm_nonfatal("`%%pathsearch' expects a macro identifier as first parameter");
H. Peter Anvin418ca702008-05-30 10:42:30 -07003269 free_tlist(origline);
3270 return DIRECTIVE_FOUND;
3271 }
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04003272 ctx = get_ctx(tline->text, &mname);
H. Peter Anvin418ca702008-05-30 10:42:30 -07003273 last = tline;
3274 tline = expand_smacro(tline->next);
3275 last->next = NULL;
3276
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003277 t = tline;
H. Peter Anvin418ca702008-05-30 10:42:30 -07003278 while (tok_type_(t, TOK_WHITESPACE))
3279 t = t->next;
3280
3281 if (!t || (t->type != TOK_STRING &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003282 t->type != TOK_INTERNAL_STRING)) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003283 nasm_nonfatal("`%%pathsearch' expects a file name");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003284 free_tlist(tline);
H. Peter Anvin418ca702008-05-30 10:42:30 -07003285 free_tlist(origline);
3286 return DIRECTIVE_FOUND; /* but we did _something_ */
3287 }
3288 if (t->next)
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08003289 nasm_warn(WARN_OTHER, "trailing garbage after `%%pathsearch' ignored");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003290 p = t->text;
H. Peter Anvin427cc912008-06-01 21:43:03 -07003291 if (t->type != TOK_INTERNAL_STRING)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003292 nasm_unquote(p, NULL);
H. Peter Anvin418ca702008-05-30 10:42:30 -07003293
H. Peter Anvin9924d1e2016-10-04 00:59:39 -07003294 inc_fopen(p, NULL, &found_path, INC_PROBE, NF_BINARY);
H. Peter Anvinccad6f92016-10-04 00:34:35 -07003295 if (!found_path)
3296 found_path = p;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003297 macro_start = nasm_malloc(sizeof(*macro_start));
3298 macro_start->next = NULL;
H. Peter Anvinccad6f92016-10-04 00:34:35 -07003299 macro_start->text = nasm_quote(found_path, strlen(found_path));
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003300 macro_start->type = TOK_STRING;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003301 macro_start->a.mac = NULL;
H. Peter Anvin418ca702008-05-30 10:42:30 -07003302
3303 /*
3304 * We now have a macro name, an implicit parameter count of
3305 * zero, and a string token to use as an expansion. Create
3306 * and store an SMacro.
3307 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003308 define_smacro(ctx, mname, casesense, 0, macro_start);
3309 free_tlist(tline);
H. Peter Anvin418ca702008-05-30 10:42:30 -07003310 free_tlist(origline);
3311 return DIRECTIVE_FOUND;
3312 }
3313
H. Peter Anvine2c80182005-01-15 22:15:51 +00003314 case PP_STRLEN:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003315 casesense = true;
H. Peter Anvin70653092007-10-19 14:42:29 -07003316
H. Peter Anvine2c80182005-01-15 22:15:51 +00003317 tline = tline->next;
3318 skip_white_(tline);
3319 tline = expand_id(tline);
3320 if (!tline || (tline->type != TOK_ID &&
3321 (tline->type != TOK_PREPROC_ID ||
3322 tline->text[1] != '$'))) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003323 nasm_nonfatal("`%%strlen' expects a macro identifier as first parameter");
H. Peter Anvine2c80182005-01-15 22:15:51 +00003324 free_tlist(origline);
3325 return DIRECTIVE_FOUND;
3326 }
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04003327 ctx = get_ctx(tline->text, &mname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003328 last = tline;
3329 tline = expand_smacro(tline->next);
3330 last->next = NULL;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003331
H. Peter Anvine2c80182005-01-15 22:15:51 +00003332 t = tline;
3333 while (tok_type_(t, TOK_WHITESPACE))
3334 t = t->next;
3335 /* t should now point to the string */
Cyrill Gorcunov4e1d5ab2010-07-23 18:51:51 +04003336 if (!tok_type_(t, TOK_STRING)) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003337 nasm_nonfatal("`%%strlen` requires string as second parameter");
H. Peter Anvine2c80182005-01-15 22:15:51 +00003338 free_tlist(tline);
3339 free_tlist(origline);
3340 return DIRECTIVE_FOUND;
3341 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003342
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003343 macro_start = nasm_malloc(sizeof(*macro_start));
3344 macro_start->next = NULL;
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07003345 make_tok_num(macro_start, nasm_unquote(t->text, NULL));
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003346 macro_start->a.mac = NULL;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003347
H. Peter Anvine2c80182005-01-15 22:15:51 +00003348 /*
3349 * We now have a macro name, an implicit parameter count of
3350 * zero, and a numeric token to use as an expansion. Create
3351 * and store an SMacro.
3352 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003353 define_smacro(ctx, mname, casesense, 0, macro_start);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003354 free_tlist(tline);
3355 free_tlist(origline);
3356 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003357
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003358 case PP_STRCAT:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003359 casesense = true;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003360
3361 tline = tline->next;
3362 skip_white_(tline);
3363 tline = expand_id(tline);
3364 if (!tline || (tline->type != TOK_ID &&
3365 (tline->type != TOK_PREPROC_ID ||
3366 tline->text[1] != '$'))) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003367 nasm_nonfatal("`%%strcat' expects a macro identifier as first parameter");
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003368 free_tlist(origline);
3369 return DIRECTIVE_FOUND;
3370 }
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04003371 ctx = get_ctx(tline->text, &mname);
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003372 last = tline;
3373 tline = expand_smacro(tline->next);
3374 last->next = NULL;
3375
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003376 len = 0;
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04003377 list_for_each(t, tline) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003378 switch (t->type) {
3379 case TOK_WHITESPACE:
3380 break;
3381 case TOK_STRING:
3382 len += t->a.len = nasm_unquote(t->text, NULL);
3383 break;
3384 case TOK_OTHER:
3385 if (!strcmp(t->text, ",")) /* permit comma separators */
3386 break;
3387 /* else fall through */
3388 default:
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003389 nasm_nonfatal("non-string passed to `%%strcat' (%d)", t->type);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003390 free_tlist(tline);
3391 free_tlist(origline);
3392 return DIRECTIVE_FOUND;
3393 }
3394 }
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003395
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003396 p = pp = nasm_malloc(len);
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04003397 list_for_each(t, tline) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003398 if (t->type == TOK_STRING) {
3399 memcpy(p, t->text, t->a.len);
3400 p += t->a.len;
3401 }
3402 }
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003403
3404 /*
3405 * We now have a macro name, an implicit parameter count of
3406 * zero, and a numeric token to use as an expansion. Create
3407 * and store an SMacro.
3408 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003409 macro_start = new_Token(NULL, TOK_STRING, NULL, 0);
3410 macro_start->text = nasm_quote(pp, len);
3411 nasm_free(pp);
3412 define_smacro(ctx, mname, casesense, 0, macro_start);
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003413 free_tlist(tline);
3414 free_tlist(origline);
3415 return DIRECTIVE_FOUND;
3416
H. Peter Anvine2c80182005-01-15 22:15:51 +00003417 case PP_SUBSTR:
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003418 {
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003419 int64_t start, count;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003420 size_t len;
H. Peter Anvind2456592008-06-19 15:04:18 -07003421
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003422 casesense = true;
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07003423
H. Peter Anvine2c80182005-01-15 22:15:51 +00003424 tline = tline->next;
3425 skip_white_(tline);
3426 tline = expand_id(tline);
3427 if (!tline || (tline->type != TOK_ID &&
3428 (tline->type != TOK_PREPROC_ID ||
3429 tline->text[1] != '$'))) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003430 nasm_nonfatal("`%%substr' expects a macro identifier as first parameter");
H. Peter Anvine2c80182005-01-15 22:15:51 +00003431 free_tlist(origline);
3432 return DIRECTIVE_FOUND;
3433 }
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04003434 ctx = get_ctx(tline->text, &mname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003435 last = tline;
3436 tline = expand_smacro(tline->next);
3437 last->next = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003438
Cyrill Gorcunov35519d62010-09-06 23:49:52 +04003439 if (tline) /* skip expanded id */
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003440 t = tline->next;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003441 while (tok_type_(t, TOK_WHITESPACE))
3442 t = t->next;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003443
H. Peter Anvine2c80182005-01-15 22:15:51 +00003444 /* t should now point to the string */
Cyrill Gorcunov35519d62010-09-06 23:49:52 +04003445 if (!tok_type_(t, TOK_STRING)) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003446 nasm_nonfatal("`%%substr` requires string as second parameter");
H. Peter Anvine2c80182005-01-15 22:15:51 +00003447 free_tlist(tline);
3448 free_tlist(origline);
3449 return DIRECTIVE_FOUND;
3450 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003451
H. Peter Anvine2c80182005-01-15 22:15:51 +00003452 tt = t->next;
3453 tptr = &tt;
3454 tokval.t_type = TOKEN_INVALID;
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08003455 evalresult = evaluate(ppscan, tptr, &tokval, NULL, true, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003456 if (!evalresult) {
3457 free_tlist(tline);
3458 free_tlist(origline);
3459 return DIRECTIVE_FOUND;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003460 } else if (!is_simple(evalresult)) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003461 nasm_nonfatal("non-constant value given to `%%substr`");
H. Peter Anvine2c80182005-01-15 22:15:51 +00003462 free_tlist(tline);
3463 free_tlist(origline);
3464 return DIRECTIVE_FOUND;
3465 }
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003466 start = evalresult->value - 1;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003467
3468 while (tok_type_(tt, TOK_WHITESPACE))
3469 tt = tt->next;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003470 if (!tt) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003471 count = 1; /* Backwards compatibility: one character */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003472 } else {
3473 tokval.t_type = TOKEN_INVALID;
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08003474 evalresult = evaluate(ppscan, tptr, &tokval, NULL, true, NULL);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003475 if (!evalresult) {
3476 free_tlist(tline);
3477 free_tlist(origline);
3478 return DIRECTIVE_FOUND;
3479 } else if (!is_simple(evalresult)) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003480 nasm_nonfatal("non-constant value given to `%%substr`");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003481 free_tlist(tline);
3482 free_tlist(origline);
3483 return DIRECTIVE_FOUND;
3484 }
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003485 count = evalresult->value;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003486 }
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003487
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003488 len = nasm_unquote(t->text, NULL);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003489
Cyrill Gorcunovcff031e2010-09-07 20:31:11 +04003490 /* make start and count being in range */
3491 if (start < 0)
3492 start = 0;
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003493 if (count < 0)
3494 count = len + count + 1 - start;
3495 if (start + count > (int64_t)len)
Cyrill Gorcunovcff031e2010-09-07 20:31:11 +04003496 count = len - start;
3497 if (!len || count < 0 || start >=(int64_t)len)
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003498 start = -1, count = 0; /* empty string */
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003499
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003500 macro_start = nasm_malloc(sizeof(*macro_start));
3501 macro_start->next = NULL;
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003502 macro_start->text = nasm_quote((start < 0) ? "" : t->text + start, count);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003503 macro_start->type = TOK_STRING;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003504 macro_start->a.mac = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003505
H. Peter Anvine2c80182005-01-15 22:15:51 +00003506 /*
3507 * We now have a macro name, an implicit parameter count of
3508 * zero, and a numeric token to use as an expansion. Create
3509 * and store an SMacro.
3510 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003511 define_smacro(ctx, mname, casesense, 0, macro_start);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003512 free_tlist(tline);
3513 free_tlist(origline);
3514 return DIRECTIVE_FOUND;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003515 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003516
H. Peter Anvine2c80182005-01-15 22:15:51 +00003517 case PP_ASSIGN:
3518 case PP_IASSIGN:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003519 casesense = (i == PP_ASSIGN);
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07003520
H. Peter Anvine2c80182005-01-15 22:15:51 +00003521 tline = tline->next;
3522 skip_white_(tline);
3523 tline = expand_id(tline);
3524 if (!tline || (tline->type != TOK_ID &&
3525 (tline->type != TOK_PREPROC_ID ||
3526 tline->text[1] != '$'))) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003527 nasm_nonfatal("`%%%sassign' expects a macro identifier",
3528 (i == PP_IASSIGN ? "i" : ""));
H. Peter Anvine2c80182005-01-15 22:15:51 +00003529 free_tlist(origline);
3530 return DIRECTIVE_FOUND;
3531 }
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04003532 ctx = get_ctx(tline->text, &mname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003533 last = tline;
3534 tline = expand_smacro(tline->next);
3535 last->next = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003536
H. Peter Anvine2c80182005-01-15 22:15:51 +00003537 t = tline;
3538 tptr = &t;
3539 tokval.t_type = TOKEN_INVALID;
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08003540 evalresult = evaluate(ppscan, tptr, &tokval, NULL, true, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003541 free_tlist(tline);
3542 if (!evalresult) {
3543 free_tlist(origline);
3544 return DIRECTIVE_FOUND;
3545 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003546
H. Peter Anvine2c80182005-01-15 22:15:51 +00003547 if (tokval.t_type)
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08003548 nasm_warn(WARN_OTHER, "trailing garbage after expression ignored");
H. Peter Anvin734b1882002-04-30 21:01:08 +00003549
H. Peter Anvine2c80182005-01-15 22:15:51 +00003550 if (!is_simple(evalresult)) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003551 nasm_nonfatal("non-constant value given to `%%%sassign'",
3552 (i == PP_IASSIGN ? "i" : ""));
H. Peter Anvine2c80182005-01-15 22:15:51 +00003553 free_tlist(origline);
3554 return DIRECTIVE_FOUND;
3555 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003556
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003557 macro_start = nasm_malloc(sizeof(*macro_start));
3558 macro_start->next = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003559 make_tok_num(macro_start, reloc_value(evalresult));
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003560 macro_start->a.mac = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003561
H. Peter Anvine2c80182005-01-15 22:15:51 +00003562 /*
3563 * We now have a macro name, an implicit parameter count of
3564 * zero, and a numeric token to use as an expansion. Create
3565 * and store an SMacro.
3566 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003567 define_smacro(ctx, mname, casesense, 0, macro_start);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003568 free_tlist(origline);
3569 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003570
H. Peter Anvine2c80182005-01-15 22:15:51 +00003571 case PP_LINE:
3572 /*
3573 * Syntax is `%line nnn[+mmm] [filename]'
3574 */
H. Peter Anvin (Intel)800c1682018-12-14 12:22:11 -08003575 if (unlikely(pp_noline)) {
3576 free_tlist(origline);
3577 return DIRECTIVE_FOUND;
3578 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00003579 tline = tline->next;
3580 skip_white_(tline);
3581 if (!tok_type_(tline, TOK_NUMBER)) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003582 nasm_nonfatal("`%%line' expects line number");
H. Peter Anvine2c80182005-01-15 22:15:51 +00003583 free_tlist(origline);
3584 return DIRECTIVE_FOUND;
3585 }
H. Peter Anvin70055962007-10-11 00:05:31 -07003586 k = readnum(tline->text, &err);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003587 m = 1;
3588 tline = tline->next;
3589 if (tok_is_(tline, "+")) {
3590 tline = tline->next;
3591 if (!tok_type_(tline, TOK_NUMBER)) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003592 nasm_nonfatal("`%%line' expects line increment");
H. Peter Anvine2c80182005-01-15 22:15:51 +00003593 free_tlist(origline);
3594 return DIRECTIVE_FOUND;
3595 }
H. Peter Anvin70055962007-10-11 00:05:31 -07003596 m = readnum(tline->text, &err);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003597 tline = tline->next;
3598 }
3599 skip_white_(tline);
3600 src_set_linnum(k);
3601 istk->lineinc = m;
3602 if (tline) {
H. Peter Anvin274cda82016-05-10 02:56:29 -07003603 char *fname = detoken(tline, false);
3604 src_set_fname(fname);
3605 nasm_free(fname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003606 }
3607 free_tlist(origline);
3608 return DIRECTIVE_FOUND;
Keith Kaniosb307a4f2010-11-06 17:41:51 -05003609
H. Peter Anvine2c80182005-01-15 22:15:51 +00003610 default:
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003611 nasm_fatal("preprocessor directive `%s' not yet implemented",
3612 pp_directives[i]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003613 return DIRECTIVE_FOUND;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003614 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003615}
3616
3617/*
H. Peter Anvin76690a12002-04-30 20:52:49 +00003618 * Ensure that a macro parameter contains a condition code and
3619 * nothing else. Return the condition code index if so, or -1
3620 * otherwise.
3621 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003622static int find_cc(Token * t)
H. Peter Anvineba20a72002-04-30 20:53:55 +00003623{
H. Peter Anvin76690a12002-04-30 20:52:49 +00003624 Token *tt;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003625
H. Peter Anvin25a99342007-09-22 17:45:45 -07003626 if (!t)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003627 return -1; /* Probably a %+ without a space */
H. Peter Anvin25a99342007-09-22 17:45:45 -07003628
H. Peter Anvineba20a72002-04-30 20:53:55 +00003629 skip_white_(t);
Cyrill Gorcunov7524cfd2017-10-22 19:01:16 +03003630 if (!t)
3631 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003632 if (t->type != TOK_ID)
H. Peter Anvine2c80182005-01-15 22:15:51 +00003633 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003634 tt = t->next;
H. Peter Anvineba20a72002-04-30 20:53:55 +00003635 skip_white_(tt);
H. Peter Anvin76690a12002-04-30 20:52:49 +00003636 if (tt && (tt->type != TOK_OTHER || strcmp(tt->text, ",")))
H. Peter Anvine2c80182005-01-15 22:15:51 +00003637 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003638
Cyrill Gorcunov19456392012-05-02 00:18:56 +04003639 return bsii(t->text, (const char **)conditions, ARRAY_SIZE(conditions));
H. Peter Anvin76690a12002-04-30 20:52:49 +00003640}
3641
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003642/*
3643 * This routines walks over tokens strem and hadnles tokens
3644 * pasting, if @handle_explicit passed then explicit pasting
3645 * term is handled, otherwise -- implicit pastings only.
3646 */
Cyrill Gorcunov575d4282010-10-06 00:25:55 +04003647static bool paste_tokens(Token **head, const struct tokseq_match *m,
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003648 size_t mnum, bool handle_explicit)
H. Peter Anvind784a082009-04-20 14:01:18 -07003649{
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003650 Token *tok, *next, **prev_next, **prev_nonspace;
3651 bool pasted = false;
3652 char *buf, *p;
3653 size_t len, i;
H. Peter Anvind784a082009-04-20 14:01:18 -07003654
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003655 /*
3656 * The last token before pasting. We need it
3657 * to be able to connect new handled tokens.
3658 * In other words if there were a tokens stream
3659 *
3660 * A -> B -> C -> D
3661 *
3662 * and we've joined tokens B and C, the resulting
3663 * stream should be
3664 *
3665 * A -> BC -> D
3666 */
3667 tok = *head;
3668 prev_next = NULL;
3669
3670 if (!tok_type_(tok, TOK_WHITESPACE) && !tok_type_(tok, TOK_PASTE))
3671 prev_nonspace = head;
3672 else
3673 prev_nonspace = NULL;
3674
3675 while (tok && (next = tok->next)) {
3676
3677 switch (tok->type) {
H. Peter Anvind784a082009-04-20 14:01:18 -07003678 case TOK_WHITESPACE:
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003679 /* Zap redundant whitespaces */
3680 while (tok_type_(next, TOK_WHITESPACE))
3681 next = delete_Token(next);
3682 tok->next = next;
H. Peter Anvind784a082009-04-20 14:01:18 -07003683 break;
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003684
3685 case TOK_PASTE:
3686 /* Explicit pasting */
3687 if (!handle_explicit)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003688 break;
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003689 next = delete_Token(tok);
3690
3691 while (tok_type_(next, TOK_WHITESPACE))
3692 next = delete_Token(next);
3693
3694 if (!pasted)
3695 pasted = true;
3696
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003697 /* Left pasting token is start of line */
3698 if (!prev_nonspace)
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003699 nasm_fatal("No lvalue found on pasting");
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003700
Cyrill Gorcunov8b5c9fb2013-02-04 01:24:54 +04003701 /*
3702 * No ending token, this might happen in two
3703 * cases
3704 *
3705 * 1) There indeed no right token at all
3706 * 2) There is a bare "%define ID" statement,
3707 * and @ID does expand to whitespace.
3708 *
3709 * So technically we need to do a grammar analysis
3710 * in another stage of parsing, but for now lets don't
3711 * change the behaviour people used to. Simply allow
3712 * whitespace after paste token.
3713 */
3714 if (!next) {
3715 /*
3716 * Zap ending space tokens and that's all.
3717 */
3718 tok = (*prev_nonspace)->next;
3719 while (tok_type_(tok, TOK_WHITESPACE))
3720 tok = delete_Token(tok);
3721 tok = *prev_nonspace;
3722 tok->next = NULL;
3723 break;
3724 }
3725
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003726 tok = *prev_nonspace;
3727 while (tok_type_(tok, TOK_WHITESPACE))
3728 tok = delete_Token(tok);
3729 len = strlen(tok->text);
3730 len += strlen(next->text);
3731
3732 p = buf = nasm_malloc(len + 1);
3733 strcpy(p, tok->text);
3734 p = strchr(p, '\0');
3735 strcpy(p, next->text);
3736
3737 delete_Token(tok);
3738
3739 tok = tokenize(buf);
3740 nasm_free(buf);
3741
3742 *prev_nonspace = tok;
3743 while (tok && tok->next)
3744 tok = tok->next;
3745
3746 tok->next = delete_Token(next);
3747
3748 /* Restart from pasted tokens head */
3749 tok = *prev_nonspace;
3750 break;
3751
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003752 default:
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003753 /* implicit pasting */
Cyrill Gorcunov575d4282010-10-06 00:25:55 +04003754 for (i = 0; i < mnum; i++) {
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003755 if (!(PP_CONCAT_MATCH(tok, m[i].mask_head)))
3756 continue;
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +04003757
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003758 len = 0;
3759 while (next && PP_CONCAT_MATCH(next, m[i].mask_tail)) {
3760 len += strlen(next->text);
3761 next = next->next;
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +04003762 }
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003763
Cyrill Gorcunov6f8109e2017-10-22 21:26:36 +03003764 /* No match or no text to process */
3765 if (tok == next || len == 0)
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003766 break;
3767
3768 len += strlen(tok->text);
3769 p = buf = nasm_malloc(len + 1);
3770
Adam Majer1a069432017-07-25 11:12:35 +02003771 strcpy(p, tok->text);
3772 p = strchr(p, '\0');
3773 tok = delete_Token(tok);
3774
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003775 while (tok != next) {
Adam Majer1a069432017-07-25 11:12:35 +02003776 if (PP_CONCAT_MATCH(tok, m[i].mask_tail)) {
3777 strcpy(p, tok->text);
3778 p = strchr(p, '\0');
3779 }
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003780 tok = delete_Token(tok);
3781 }
3782
3783 tok = tokenize(buf);
3784 nasm_free(buf);
3785
3786 if (prev_next)
3787 *prev_next = tok;
3788 else
3789 *head = tok;
3790
3791 /*
3792 * Connect pasted into original stream,
3793 * ie A -> new-tokens -> B
3794 */
3795 while (tok && tok->next)
3796 tok = tok->next;
3797 tok->next = next;
3798
3799 if (!pasted)
3800 pasted = true;
3801
3802 /* Restart from pasted tokens head */
3803 tok = prev_next ? *prev_next : *head;
Cyrill Gorcunov575d4282010-10-06 00:25:55 +04003804 }
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003805
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003806 break;
H. Peter Anvind784a082009-04-20 14:01:18 -07003807 }
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003808
3809 prev_next = &tok->next;
3810
3811 if (tok->next &&
3812 !tok_type_(tok->next, TOK_WHITESPACE) &&
3813 !tok_type_(tok->next, TOK_PASTE))
3814 prev_nonspace = prev_next;
3815
3816 tok = tok->next;
H. Peter Anvind784a082009-04-20 14:01:18 -07003817 }
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003818
3819 return pasted;
H. Peter Anvind784a082009-04-20 14:01:18 -07003820}
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003821
3822/*
3823 * expands to a list of tokens from %{x:y}
3824 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003825static Token *expand_mmac_params_range(MMacro *mac, Token *tline, Token ***last)
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003826{
3827 Token *t = tline, **tt, *tm, *head;
3828 char *pos;
3829 int fst, lst, j, i;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003830
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003831 pos = strchr(tline->text, ':');
3832 nasm_assert(pos);
3833
3834 lst = atoi(pos + 1);
3835 fst = atoi(tline->text + 1);
3836
3837 /*
3838 * only macros params are accounted so
3839 * if someone passes %0 -- we reject such
3840 * value(s)
3841 */
3842 if (lst == 0 || fst == 0)
3843 goto err;
3844
3845 /* the values should be sane */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003846 if ((fst > (int)mac->nparam || fst < (-(int)mac->nparam)) ||
3847 (lst > (int)mac->nparam || lst < (-(int)mac->nparam)))
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003848 goto err;
3849
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003850 fst = fst < 0 ? fst + (int)mac->nparam + 1: fst;
3851 lst = lst < 0 ? lst + (int)mac->nparam + 1: lst;
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003852
3853 /* counted from zero */
3854 fst--, lst--;
3855
3856 /*
Cyrill Gorcunove75331c2013-11-09 12:02:15 +04003857 * It will be at least one token. Note we
3858 * need to scan params until separator, otherwise
3859 * only first token will be passed.
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003860 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003861 tm = mac->params[(fst + mac->rotate) % mac->nparam];
Cyrill Gorcunov67f2ca22018-10-13 19:41:01 +03003862 if (!tm)
3863 goto err;
Cyrill Gorcunove75331c2013-11-09 12:02:15 +04003864 head = new_Token(NULL, tm->type, tm->text, 0);
3865 tt = &head->next, tm = tm->next;
3866 while (tok_isnt_(tm, ",")) {
3867 t = new_Token(NULL, tm->type, tm->text, 0);
3868 *tt = t, tt = &t->next, tm = tm->next;
3869 }
3870
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003871 if (fst < lst) {
3872 for (i = fst + 1; i <= lst; i++) {
3873 t = new_Token(NULL, TOK_OTHER, ",", 0);
3874 *tt = t, tt = &t->next;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003875 j = (i + mac->rotate) % mac->nparam;
3876 tm = mac->params[j];
Cyrill Gorcunove75331c2013-11-09 12:02:15 +04003877 while (tok_isnt_(tm, ",")) {
3878 t = new_Token(NULL, tm->type, tm->text, 0);
3879 *tt = t, tt = &t->next, tm = tm->next;
3880 }
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003881 }
3882 } else {
3883 for (i = fst - 1; i >= lst; i--) {
3884 t = new_Token(NULL, TOK_OTHER, ",", 0);
3885 *tt = t, tt = &t->next;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003886 j = (i + mac->rotate) % mac->nparam;
3887 tm = mac->params[j];
Cyrill Gorcunove75331c2013-11-09 12:02:15 +04003888 while (tok_isnt_(tm, ",")) {
3889 t = new_Token(NULL, tm->type, tm->text, 0);
3890 *tt = t, tt = &t->next, tm = tm->next;
3891 }
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003892 }
3893 }
3894
3895 *last = tt;
3896 return head;
3897
3898err:
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003899 nasm_nonfatal("`%%{%s}': macro parameters out of range",
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003900 &tline->text[1]);
3901 return tline;
3902}
3903
H. Peter Anvin76690a12002-04-30 20:52:49 +00003904/*
3905 * Expand MMacro-local things: parameter references (%0, %n, %+n,
H. Peter Anvin67c63722008-10-26 23:49:00 -07003906 * %-n) and MMacro-local identifiers (%%foo) as well as
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003907 * macro indirection (%[...]) and range (%{..:..}).
H. Peter Anvin76690a12002-04-30 20:52:49 +00003908 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003909static Token *expand_mmac_params(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00003910{
H. Peter Anvin734b1882002-04-30 21:01:08 +00003911 Token *t, *tt, **tail, *thead;
H. Peter Anvin6125b622009-04-08 14:02:25 -07003912 bool changed = false;
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003913 char *pos;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003914
3915 tail = &thead;
3916 thead = NULL;
3917
H. Peter Anvine2c80182005-01-15 22:15:51 +00003918 while (tline) {
Cyrill Gorcunov661f7232018-10-28 20:39:34 +03003919 if (tline->type == TOK_PREPROC_ID && tline->text && tline->text[0] &&
Cyrill Gorcunovca611192010-06-04 09:22:12 +04003920 (((tline->text[1] == '+' || tline->text[1] == '-') && tline->text[2]) ||
3921 (tline->text[1] >= '0' && tline->text[1] <= '9') ||
3922 tline->text[1] == '%')) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00003923 char *text = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003924 int type = 0, cc; /* type = 0 to placate optimisers */
Keith Kaniosa6dfa782007-04-13 16:47:53 +00003925 char tmpbuf[30];
H. Peter Anvin25a99342007-09-22 17:45:45 -07003926 unsigned int n;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003927 int i;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003928 MMacro *mac;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003929
H. Peter Anvine2c80182005-01-15 22:15:51 +00003930 t = tline;
3931 tline = tline->next;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003932
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003933 mac = istk->mstk;
3934 while (mac && !mac->name) /* avoid mistaking %reps for macros */
3935 mac = mac->next_active;
3936 if (!mac) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003937 nasm_nonfatal("`%s': not in a macro call", t->text);
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003938 } else {
3939 pos = strchr(t->text, ':');
3940 if (!pos) {
3941 switch (t->text[1]) {
3942 /*
3943 * We have to make a substitution of one of the
3944 * forms %1, %-1, %+1, %%foo, %0.
3945 */
3946 case '0':
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003947 type = TOK_NUMBER;
3948 snprintf(tmpbuf, sizeof(tmpbuf), "%d", mac->nparam);
3949 text = nasm_strdup(tmpbuf);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003950 break;
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003951 case '%':
H. Peter Anvine2c80182005-01-15 22:15:51 +00003952 type = TOK_ID;
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003953 snprintf(tmpbuf, sizeof(tmpbuf), "..@%"PRIu64".",
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003954 mac->unique);
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003955 text = nasm_strcat(tmpbuf, t->text + 2);
3956 break;
3957 case '-':
3958 n = atoi(t->text + 2) - 1;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003959 if (n >= mac->nparam)
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003960 tt = NULL;
3961 else {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003962 if (mac->nparam > 1)
3963 n = (n + mac->rotate) % mac->nparam;
3964 tt = mac->params[n];
H. Peter Anvine2c80182005-01-15 22:15:51 +00003965 }
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003966 cc = find_cc(tt);
3967 if (cc == -1) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003968 nasm_nonfatal("macro parameter %d is not a condition code",
3969 n + 1);
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003970 text = NULL;
3971 } else {
3972 type = TOK_ID;
3973 if (inverse_ccs[cc] == -1) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003974 nasm_nonfatal("condition code `%s' is not invertible",
3975 conditions[cc]);
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003976 text = NULL;
3977 } else
3978 text = nasm_strdup(conditions[inverse_ccs[cc]]);
3979 }
3980 break;
3981 case '+':
3982 n = atoi(t->text + 2) - 1;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003983 if (n >= mac->nparam)
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003984 tt = NULL;
3985 else {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003986 if (mac->nparam > 1)
3987 n = (n + mac->rotate) % mac->nparam;
3988 tt = mac->params[n];
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003989 }
3990 cc = find_cc(tt);
3991 if (cc == -1) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003992 nasm_nonfatal("macro parameter %d is not a condition code",
3993 n + 1);
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003994 text = NULL;
3995 } else {
3996 type = TOK_ID;
3997 text = nasm_strdup(conditions[cc]);
3998 }
3999 break;
4000 default:
4001 n = atoi(t->text + 1) - 1;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004002 if (n >= mac->nparam)
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004003 tt = NULL;
4004 else {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004005 if (mac->nparam > 1)
4006 n = (n + mac->rotate) % mac->nparam;
4007 tt = mac->params[n];
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004008 }
4009 if (tt) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004010 for (i = 0; i < mac->paramlen[n]; i++) {
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004011 *tail = new_Token(NULL, tt->type, tt->text, 0);
4012 tail = &(*tail)->next;
4013 tt = tt->next;
4014 }
4015 }
4016 text = NULL; /* we've done it here */
4017 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004018 }
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004019 } else {
4020 /*
4021 * seems we have a parameters range here
4022 */
4023 Token *head, **last;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004024 head = expand_mmac_params_range(mac, t, &last);
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004025 if (head != t) {
4026 *tail = head;
4027 *last = tline;
4028 tline = head;
4029 text = NULL;
4030 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00004031 }
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004032 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00004033 if (!text) {
4034 delete_Token(t);
4035 } else {
4036 *tail = t;
4037 tail = &t->next;
4038 t->type = type;
4039 nasm_free(t->text);
4040 t->text = text;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004041 t->a.mac = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004042 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004043 changed = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004044 continue;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004045 } else if (tline->type == TOK_INDIRECT) {
4046 t = tline;
4047 tline = tline->next;
4048 tt = tokenize(t->text);
4049 tt = expand_mmac_params(tt);
4050 tt = expand_smacro(tt);
4051 *tail = tt;
4052 while (tt) {
4053 tt->a.mac = NULL; /* Necessary? */
4054 tail = &tt->next;
4055 tt = tt->next;
4056 }
4057 delete_Token(t);
4058 changed = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004059 } else {
4060 t = *tail = tline;
4061 tline = tline->next;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004062 t->a.mac = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004063 tail = &t->next;
4064 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00004065 }
H. Peter Anvineba20a72002-04-30 20:53:55 +00004066 *tail = NULL;
H. Peter Anvin67c63722008-10-26 23:49:00 -07004067
Cyrill Gorcunovc6a742c2011-06-27 01:23:09 +04004068 if (changed) {
4069 const struct tokseq_match t[] = {
4070 {
4071 PP_CONCAT_MASK(TOK_ID) |
4072 PP_CONCAT_MASK(TOK_FLOAT), /* head */
4073 PP_CONCAT_MASK(TOK_ID) |
4074 PP_CONCAT_MASK(TOK_NUMBER) |
4075 PP_CONCAT_MASK(TOK_FLOAT) |
4076 PP_CONCAT_MASK(TOK_OTHER) /* tail */
4077 },
4078 {
4079 PP_CONCAT_MASK(TOK_NUMBER), /* head */
4080 PP_CONCAT_MASK(TOK_NUMBER) /* tail */
4081 }
4082 };
4083 paste_tokens(&thead, t, ARRAY_SIZE(t), false);
4084 }
H. Peter Anvin6125b622009-04-08 14:02:25 -07004085
H. Peter Anvin76690a12002-04-30 20:52:49 +00004086 return thead;
4087}
4088
4089/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004090 * Expand all single-line macro calls made in the given line.
4091 * Return the expanded version of the line. The original is deemed
4092 * to be destroyed in the process. (In reality we'll just move
4093 * Tokens from input to output a lot of the time, rather than
4094 * actually bothering to destroy and replicate.)
4095 */
H. Peter Anvincb1cf592007-11-19 12:26:50 -08004096
H. Peter Anvine2c80182005-01-15 22:15:51 +00004097static Token *expand_smacro(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004098{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004099 Token *t, *tt, *mstart, **tail, *thead;
H. Peter Anvineba20a72002-04-30 20:53:55 +00004100 SMacro *head = NULL, *m;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004101 Token **params;
4102 int *paramsize;
H. Peter Anvin25a99342007-09-22 17:45:45 -07004103 unsigned int nparam, sparam;
H. Peter Anvind784a082009-04-20 14:01:18 -07004104 int brackets;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004105 Token *org_tline = tline;
4106 Context *ctx;
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08004107 const char *mname;
H. Peter Anvina3d96d02018-06-15 17:51:39 -07004108 int64_t deadman = nasm_limit[LIMIT_MACROS];
H. Peter Anvin8287daf2009-07-07 16:00:58 -07004109 bool expanded;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004110
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004111 /*
4112 * Trick: we should avoid changing the start token pointer since it can
4113 * be contained in "next" field of other token. Because of this
4114 * we allocate a copy of first token and work with it; at the end of
4115 * routine we copy it back
4116 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004117 if (org_tline) {
Cyrill Gorcunoved4a8052010-04-09 15:40:35 +04004118 tline = new_Token(org_tline->next, org_tline->type,
4119 org_tline->text, 0);
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004120 tline->a.mac = org_tline->a.mac;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004121 nasm_free(org_tline->text);
4122 org_tline->text = NULL;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004123 }
4124
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004125 expanded = true; /* Always expand %+ at least once */
H. Peter Anvin8287daf2009-07-07 16:00:58 -07004126
H. Peter Anvincb1cf592007-11-19 12:26:50 -08004127again:
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004128 thead = NULL;
Cyrill Gorcunoved4a8052010-04-09 15:40:35 +04004129 tail = &thead;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004130
H. Peter Anvine2c80182005-01-15 22:15:51 +00004131 while (tline) { /* main token loop */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004132 if (!--deadman) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03004133 nasm_nonfatal("interminable macro recursion");
Cyrill Gorcunovbd38c8f2009-11-21 11:11:23 +03004134 goto err;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004135 }
H. Peter Anvincb1cf592007-11-19 12:26:50 -08004136
H. Peter Anvine2c80182005-01-15 22:15:51 +00004137 if ((mname = tline->text)) {
4138 /* if this token is a local macro, look in local context */
Cyrill Gorcunovc56d9ad2010-02-11 15:12:19 +03004139 if (tline->type == TOK_ID) {
4140 head = (SMacro *)hash_findix(&smacros, mname);
4141 } else if (tline->type == TOK_PREPROC_ID) {
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04004142 ctx = get_ctx(mname, &mname);
Cyrill Gorcunovc56d9ad2010-02-11 15:12:19 +03004143 head = ctx ? (SMacro *)hash_findix(&ctx->localmac, mname) : NULL;
4144 } else
4145 head = NULL;
H. Peter Anvin072771e2008-05-22 13:17:51 -07004146
H. Peter Anvine2c80182005-01-15 22:15:51 +00004147 /*
4148 * We've hit an identifier. As in is_mmacro below, we first
4149 * check whether the identifier is a single-line macro at
4150 * all, then think about checking for parameters if
4151 * necessary.
4152 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004153 list_for_each(m, head)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004154 if (!mstrcmp(m->name, mname, m->casesense))
4155 break;
4156 if (m) {
4157 mstart = tline;
4158 params = NULL;
4159 paramsize = NULL;
4160 if (m->nparam == 0) {
4161 /*
4162 * Simple case: the macro is parameterless. Discard the
4163 * one token that the macro call took, and push the
4164 * expansion back on the to-do stack.
4165 */
4166 if (!m->expansion) {
4167 if (!strcmp("__FILE__", m->name)) {
H. Peter Anvin274cda82016-05-10 02:56:29 -07004168 const char *file = src_get_fname();
4169 /* nasm_free(tline->text); here? */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004170 tline->text = nasm_quote(file, strlen(file));
4171 tline->type = TOK_STRING;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004172 continue;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004173 }
4174 if (!strcmp("__LINE__", m->name)) {
4175 nasm_free(tline->text);
4176 make_tok_num(tline, src_get_linnum());
4177 continue;
4178 }
4179 if (!strcmp("__BITS__", m->name)) {
4180 nasm_free(tline->text);
4181 make_tok_num(tline, globalbits);
4182 continue;
4183 }
4184 tline = delete_Token(tline);
4185 continue;
4186 }
4187 } else {
4188 /*
4189 * Complicated case: at least one macro with this name
H. Peter Anvine2c80182005-01-15 22:15:51 +00004190 * exists and takes parameters. We must find the
4191 * parameters in the call, count them, find the SMacro
4192 * that corresponds to that form of the macro call, and
4193 * substitute for the parameters when we expand. What a
4194 * pain.
4195 */
4196 /*tline = tline->next;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004197 skip_white_(tline); */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004198 do {
4199 t = tline->next;
4200 while (tok_type_(t, TOK_SMAC_END)) {
Cyrill Gorcunov982186a2019-03-16 23:05:50 +03004201 if (t->a.mac)
4202 t->a.mac->in_progress = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004203 t->text = NULL;
4204 t = tline->next = delete_Token(t);
4205 }
4206 tline = t;
4207 } while (tok_type_(tline, TOK_WHITESPACE));
4208 if (!tok_is_(tline, "(")) {
4209 /*
4210 * This macro wasn't called with parameters: ignore
4211 * the call. (Behaviour borrowed from gnu cpp.)
4212 */
4213 tline = mstart;
4214 m = NULL;
4215 } else {
4216 int paren = 0;
4217 int white = 0;
4218 brackets = 0;
4219 nparam = 0;
4220 sparam = PARAM_DELTA;
4221 params = nasm_malloc(sparam * sizeof(Token *));
4222 params[0] = tline->next;
4223 paramsize = nasm_malloc(sparam * sizeof(int));
4224 paramsize[0] = 0;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07004225 while (true) { /* parameter loop */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004226 /*
4227 * For some unusual expansions
4228 * which concatenates function call
4229 */
4230 t = tline->next;
4231 while (tok_type_(t, TOK_SMAC_END)) {
Cyrill Gorcunov982186a2019-03-16 23:05:50 +03004232 if (t->a.mac)
4233 t->a.mac->in_progress = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004234 t->text = NULL;
4235 t = tline->next = delete_Token(t);
4236 }
4237 tline = t;
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00004238
H. Peter Anvine2c80182005-01-15 22:15:51 +00004239 if (!tline) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03004240 nasm_nonfatal("macro call expects terminating `)'");
H. Peter Anvine2c80182005-01-15 22:15:51 +00004241 break;
4242 }
4243 if (tline->type == TOK_WHITESPACE
4244 && brackets <= 0) {
4245 if (paramsize[nparam])
4246 white++;
4247 else
4248 params[nparam] = tline->next;
4249 continue; /* parameter loop */
4250 }
4251 if (tline->type == TOK_OTHER
4252 && tline->text[1] == 0) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004253 char ch = tline->text[0];
H. Peter Anvine2c80182005-01-15 22:15:51 +00004254 if (ch == ',' && !paren && brackets <= 0) {
4255 if (++nparam >= sparam) {
4256 sparam += PARAM_DELTA;
4257 params = nasm_realloc(params,
Cyrill Gorcunoved4a8052010-04-09 15:40:35 +04004258 sparam * sizeof(Token *));
4259 paramsize = nasm_realloc(paramsize,
4260 sparam * sizeof(int));
H. Peter Anvine2c80182005-01-15 22:15:51 +00004261 }
4262 params[nparam] = tline->next;
4263 paramsize[nparam] = 0;
4264 white = 0;
4265 continue; /* parameter loop */
4266 }
4267 if (ch == '{' &&
4268 (brackets > 0 || (brackets == 0 &&
4269 !paramsize[nparam])))
4270 {
4271 if (!(brackets++)) {
4272 params[nparam] = tline->next;
4273 continue; /* parameter loop */
4274 }
4275 }
4276 if (ch == '}' && brackets > 0)
4277 if (--brackets == 0) {
4278 brackets = -1;
4279 continue; /* parameter loop */
4280 }
4281 if (ch == '(' && !brackets)
4282 paren++;
4283 if (ch == ')' && brackets <= 0)
4284 if (--paren < 0)
4285 break;
4286 }
4287 if (brackets < 0) {
4288 brackets = 0;
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03004289 nasm_nonfatal("braces do not "
H. Peter Anvine2c80182005-01-15 22:15:51 +00004290 "enclose all of macro parameter");
4291 }
4292 paramsize[nparam] += white + 1;
4293 white = 0;
4294 } /* parameter loop */
4295 nparam++;
4296 while (m && (m->nparam != nparam ||
4297 mstrcmp(m->name, mname,
4298 m->casesense)))
4299 m = m->next;
H. Peter Anvin (Intel)723ab482018-12-13 21:53:31 -08004300 if (!m) {
4301 /*!
4302 *!macro-params [on] macro calls with wrong parameter count
4303 *! covers warnings about \i{multi-line macros} being invoked
4304 *! with the wrong number of parameters. See \k{mlmacover} for an
4305 *! example of why you might want to disable this warning.
4306 */
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08004307 nasm_warn(WARN_MACRO_PARAMS,
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03004308 "macro `%s' exists, "
4309 "but not taking %d parameters",
4310 mstart->text, nparam);
H. Peter Anvin (Intel)723ab482018-12-13 21:53:31 -08004311 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00004312 }
4313 }
4314 if (m && m->in_progress)
4315 m = NULL;
4316 if (!m) { /* in progess or didn't find '(' or wrong nparam */
H. Peter Anvin70653092007-10-19 14:42:29 -07004317 /*
H. Peter Anvine2c80182005-01-15 22:15:51 +00004318 * Design question: should we handle !tline, which
4319 * indicates missing ')' here, or expand those
4320 * macros anyway, which requires the (t) test a few
H. Peter Anvin70653092007-10-19 14:42:29 -07004321 * lines down?
H. Peter Anvine2c80182005-01-15 22:15:51 +00004322 */
4323 nasm_free(params);
4324 nasm_free(paramsize);
4325 tline = mstart;
4326 } else {
4327 /*
4328 * Expand the macro: we are placed on the last token of the
4329 * call, so that we can easily split the call from the
4330 * following tokens. We also start by pushing an SMAC_END
4331 * token for the cycle removal.
4332 */
4333 t = tline;
4334 if (t) {
4335 tline = t->next;
4336 t->next = NULL;
4337 }
4338 tt = new_Token(tline, TOK_SMAC_END, NULL, 0);
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004339 tt->a.mac = m;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07004340 m->in_progress = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004341 tline = tt;
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04004342 list_for_each(t, m->expansion) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004343 if (t->type >= TOK_SMAC_PARAM) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00004344 Token *pcopy = tline, **ptail = &pcopy;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004345 Token *ttt, *pt;
4346 int i;
H. Peter Anvin734b1882002-04-30 21:01:08 +00004347
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004348 ttt = params[t->type - TOK_SMAC_PARAM];
4349 i = paramsize[t->type - TOK_SMAC_PARAM];
4350 while (--i >= 0) {
4351 pt = *ptail = new_Token(tline, ttt->type,
4352 ttt->text, 0);
4353 ptail = &pt->next;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004354 ttt = ttt->next;
Cyrill Gorcunov59ce1c62017-10-22 18:42:07 +03004355 if (!ttt && i > 0) {
4356 /*
4357 * FIXME: Need to handle more gracefully,
4358 * exiting early on agruments analysis.
4359 */
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03004360 nasm_fatal("macro `%s' expects %d args",
Cyrill Gorcunov59ce1c62017-10-22 18:42:07 +03004361 mstart->text,
4362 (int)paramsize[t->type - TOK_SMAC_PARAM]);
4363 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00004364 }
4365 tline = pcopy;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004366 } else if (t->type == TOK_PREPROC_Q) {
4367 tt = new_Token(tline, TOK_ID, mname, 0);
4368 tline = tt;
4369 } else if (t->type == TOK_PREPROC_QQ) {
4370 tt = new_Token(tline, TOK_ID, m->name, 0);
4371 tline = tt;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004372 } else {
4373 tt = new_Token(tline, t->type, t->text, 0);
4374 tline = tt;
4375 }
4376 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00004377
H. Peter Anvine2c80182005-01-15 22:15:51 +00004378 /*
4379 * Having done that, get rid of the macro call, and clean
4380 * up the parameters.
4381 */
4382 nasm_free(params);
4383 nasm_free(paramsize);
4384 free_tlist(mstart);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004385 expanded = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004386 continue; /* main token loop */
4387 }
4388 }
4389 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004390
H. Peter Anvine2c80182005-01-15 22:15:51 +00004391 if (tline->type == TOK_SMAC_END) {
Cyrill Gorcunov980dd652018-10-14 19:25:32 +03004392 /* On error path it might already be dropped */
4393 if (tline->a.mac)
4394 tline->a.mac->in_progress = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004395 tline = delete_Token(tline);
4396 } else {
4397 t = *tail = tline;
4398 tline = tline->next;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004399 t->a.mac = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004400 t->next = NULL;
4401 tail = &t->next;
4402 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004403 }
4404
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004405 /*
4406 * Now scan the entire line and look for successive TOK_IDs that resulted
Keith Kaniosb7a89542007-04-12 02:40:54 +00004407 * after expansion (they can't be produced by tokenize()). The successive
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004408 * TOK_IDs should be concatenated.
4409 * Also we look for %+ tokens and concatenate the tokens before and after
4410 * them (without white spaces in between).
4411 */
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +04004412 if (expanded) {
Cyrill Gorcunovc6a742c2011-06-27 01:23:09 +04004413 const struct tokseq_match t[] = {
4414 {
4415 PP_CONCAT_MASK(TOK_ID) |
4416 PP_CONCAT_MASK(TOK_PREPROC_ID), /* head */
4417 PP_CONCAT_MASK(TOK_ID) |
4418 PP_CONCAT_MASK(TOK_PREPROC_ID) |
4419 PP_CONCAT_MASK(TOK_NUMBER) /* tail */
4420 }
4421 };
4422 if (paste_tokens(&thead, t, ARRAY_SIZE(t), true)) {
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +04004423 /*
4424 * If we concatenated something, *and* we had previously expanded
4425 * an actual macro, scan the lines again for macros...
4426 */
4427 tline = thead;
4428 expanded = false;
4429 goto again;
4430 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00004431 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004432
Cyrill Gorcunovbd38c8f2009-11-21 11:11:23 +03004433err:
H. Peter Anvine2c80182005-01-15 22:15:51 +00004434 if (org_tline) {
4435 if (thead) {
4436 *org_tline = *thead;
4437 /* since we just gave text to org_line, don't free it */
4438 thead->text = NULL;
4439 delete_Token(thead);
4440 } else {
4441 /* the expression expanded to empty line;
4442 we can't return NULL for some reasons
4443 we just set the line to a single WHITESPACE token. */
4444 memset(org_tline, 0, sizeof(*org_tline));
4445 org_tline->text = NULL;
4446 org_tline->type = TOK_WHITESPACE;
4447 }
4448 thead = org_tline;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004449 }
4450
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004451 return thead;
4452}
4453
4454/*
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004455 * Similar to expand_smacro but used exclusively with macro identifiers
4456 * right before they are fetched in. The reason is that there can be
4457 * identifiers consisting of several subparts. We consider that if there
4458 * are more than one element forming the name, user wants a expansion,
4459 * otherwise it will be left as-is. Example:
4460 *
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004461 * %define %$abc cde
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004462 *
4463 * the identifier %$abc will be left as-is so that the handler for %define
4464 * will suck it and define the corresponding value. Other case:
4465 *
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004466 * %define _%$abc cde
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004467 *
4468 * In this case user wants name to be expanded *before* %define starts
4469 * working, so we'll expand %$abc into something (if it has a value;
4470 * otherwise it will be left as-is) then concatenate all successive
4471 * PP_IDs into one.
4472 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004473static Token *expand_id(Token * tline)
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004474{
4475 Token *cur, *oldnext = NULL;
4476
H. Peter Anvin734b1882002-04-30 21:01:08 +00004477 if (!tline || !tline->next)
H. Peter Anvine2c80182005-01-15 22:15:51 +00004478 return tline;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004479
4480 cur = tline;
4481 while (cur->next &&
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004482 (cur->next->type == TOK_ID ||
4483 cur->next->type == TOK_PREPROC_ID
4484 || cur->next->type == TOK_NUMBER))
H. Peter Anvine2c80182005-01-15 22:15:51 +00004485 cur = cur->next;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004486
4487 /* If identifier consists of just one token, don't expand */
4488 if (cur == tline)
H. Peter Anvine2c80182005-01-15 22:15:51 +00004489 return tline;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004490
H. Peter Anvine2c80182005-01-15 22:15:51 +00004491 if (cur) {
4492 oldnext = cur->next; /* Detach the tail past identifier */
4493 cur->next = NULL; /* so that expand_smacro stops here */
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004494 }
4495
H. Peter Anvin734b1882002-04-30 21:01:08 +00004496 tline = expand_smacro(tline);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004497
H. Peter Anvine2c80182005-01-15 22:15:51 +00004498 if (cur) {
4499 /* expand_smacro possibly changhed tline; re-scan for EOL */
4500 cur = tline;
4501 while (cur && cur->next)
4502 cur = cur->next;
4503 if (cur)
4504 cur->next = oldnext;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004505 }
4506
4507 return tline;
4508}
4509
4510/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004511 * Determine whether the given line constitutes a multi-line macro
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004512 * call, and return the MMacro structure called if so. Doesn't have
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004513 * to check for an initial label - that's taken care of in
4514 * expand_mmacro - but must check numbers of parameters. Guaranteed
4515 * to be called with tline->type == TOK_ID, so the putative macro
4516 * name is easy to find.
4517 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004518static MMacro *is_mmacro(Token * tline, Token *** params_array)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004519{
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004520 MMacro *head, *m;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004521 Token **params;
4522 int nparam;
4523
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004524 head = (MMacro *) hash_findix(&mmacros, tline->text);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004525
4526 /*
4527 * Efficiency: first we see if any macro exists with the given
4528 * name. If not, we can return NULL immediately. _Then_ we
4529 * count the parameters, and then we look further along the
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004530 * list if necessary to find the proper MMacro.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004531 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004532 list_for_each(m, head)
4533 if (!mstrcmp(m->name, tline->text, m->casesense))
H. Peter Anvine2c80182005-01-15 22:15:51 +00004534 break;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004535 if (!m)
H. Peter Anvine2c80182005-01-15 22:15:51 +00004536 return NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004537
4538 /*
4539 * OK, we have a potential macro. Count and demarcate the
4540 * parameters.
4541 */
H. Peter Anvin734b1882002-04-30 21:01:08 +00004542 count_mmac_params(tline->next, &nparam, &params);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004543
4544 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004545 * So we know how many parameters we've got. Find the MMacro
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004546 * structure that handles this number.
4547 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004548 while (m) {
4549 if (m->nparam_min <= nparam
4550 && (m->plus || nparam <= m->nparam_max)) {
4551 /*
4552 * This one is right. Just check if cycle removal
4553 * prohibits us using it before we actually celebrate...
4554 */
4555 if (m->in_progress > m->max_depth) {
4556 if (m->max_depth > 0) {
H. Peter Anvin (Intel)80c4f232018-12-14 13:33:24 -08004557 nasm_warn(WARN_OTHER, "reached maximum recursion depth of %i",
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03004558 m->max_depth);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004559 }
4560 nasm_free(params);
4561 return NULL;
4562 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00004563 /*
4564 * It's right, and we can use it. Add its default
4565 * parameters to the end of our list if necessary.
4566 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004567 if (m->defaults && nparam < m->nparam_min + m->ndefs) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00004568 params =
4569 nasm_realloc(params,
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004570 ((m->nparam_min + m->ndefs +
H. Peter Anvine2c80182005-01-15 22:15:51 +00004571 1) * sizeof(*params)));
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004572 while (nparam < m->nparam_min + m->ndefs) {
4573 params[nparam] = m->defaults[nparam - m->nparam_min];
H. Peter Anvine2c80182005-01-15 22:15:51 +00004574 nparam++;
4575 }
4576 }
4577 /*
4578 * If we've gone over the maximum parameter count (and
4579 * we're in Plus mode), ignore parameters beyond
4580 * nparam_max.
4581 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004582 if (m->plus && nparam > m->nparam_max)
4583 nparam = m->nparam_max;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004584 /*
4585 * Then terminate the parameter list, and leave.
4586 */
4587 if (!params) { /* need this special case */
4588 params = nasm_malloc(sizeof(*params));
4589 nparam = 0;
4590 }
4591 params[nparam] = NULL;
4592 *params_array = params;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004593 return m;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004594 }
4595 /*
4596 * This one wasn't right: look for the next one with the
4597 * same name.
4598 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004599 list_for_each(m, m->next)
4600 if (!mstrcmp(m->name, tline->text, m->casesense))
H. Peter Anvine2c80182005-01-15 22:15:51 +00004601 break;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004602 }
4603
4604 /*
4605 * After all that, we didn't find one with the right number of
4606 * parameters. Issue a warning, and fail to expand the macro.
4607 */
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08004608 nasm_warn(WARN_MACRO_PARAMS,
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03004609 "macro `%s' exists, but not taking %d parameters",
4610 tline->text, nparam);
H. Peter Anvin734b1882002-04-30 21:01:08 +00004611 nasm_free(params);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004612 return NULL;
4613}
4614
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004615
4616/*
4617 * Save MMacro invocation specific fields in
4618 * preparation for a recursive macro expansion
4619 */
4620static void push_mmacro(MMacro *m)
4621{
4622 MMacroInvocation *i;
4623
4624 i = nasm_malloc(sizeof(MMacroInvocation));
4625 i->prev = m->prev;
4626 i->params = m->params;
4627 i->iline = m->iline;
4628 i->nparam = m->nparam;
4629 i->rotate = m->rotate;
4630 i->paramlen = m->paramlen;
4631 i->unique = m->unique;
4632 i->condcnt = m->condcnt;
4633 m->prev = i;
4634}
4635
4636
4637/*
4638 * Restore MMacro invocation specific fields that were
4639 * saved during a previous recursive macro expansion
4640 */
4641static void pop_mmacro(MMacro *m)
4642{
4643 MMacroInvocation *i;
4644
4645 if (m->prev) {
4646 i = m->prev;
4647 m->prev = i->prev;
4648 m->params = i->params;
4649 m->iline = i->iline;
4650 m->nparam = i->nparam;
4651 m->rotate = i->rotate;
4652 m->paramlen = i->paramlen;
4653 m->unique = i->unique;
4654 m->condcnt = i->condcnt;
4655 nasm_free(i);
4656 }
4657}
4658
4659
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004660/*
4661 * Expand the multi-line macro call made by the given line, if
4662 * there is one to be expanded. If there is, push the expansion on
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004663 * istk->expansion and return 1. Otherwise return 0.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004664 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004665static int expand_mmacro(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004666{
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004667 Token *startline = tline;
H. Peter Anvineba20a72002-04-30 20:53:55 +00004668 Token *label = NULL;
4669 int dont_prepend = 0;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004670 Token **params, *t, *tt;
4671 MMacro *m;
4672 Line *l, *ll;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004673 int i, nparam, *paramlen;
H. Peter Anvinc751e862008-06-09 10:18:45 -07004674 const char *mname;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004675
4676 t = tline;
H. Peter Anvineba20a72002-04-30 20:53:55 +00004677 skip_white_(t);
H. Peter Anvince2233b2008-05-25 21:57:00 -07004678 /* if (!tok_type_(t, TOK_ID)) Lino 02/25/02 */
H. Peter Anvindce1e2f2002-04-30 21:06:37 +00004679 if (!tok_type_(t, TOK_ID) && !tok_type_(t, TOK_PREPROC_ID))
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004680 return 0;
4681 m = is_mmacro(t, &params);
4682 if (m) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004683 mname = t->text;
H. Peter Anvinc751e862008-06-09 10:18:45 -07004684 } else {
H. Peter Anvine2c80182005-01-15 22:15:51 +00004685 Token *last;
4686 /*
4687 * We have an id which isn't a macro call. We'll assume
4688 * it might be a label; we'll also check to see if a
4689 * colon follows it. Then, if there's another id after
4690 * that lot, we'll check it again for macro-hood.
4691 */
4692 label = last = t;
4693 t = t->next;
4694 if (tok_type_(t, TOK_WHITESPACE))
4695 last = t, t = t->next;
4696 if (tok_is_(t, ":")) {
4697 dont_prepend = 1;
4698 last = t, t = t->next;
4699 if (tok_type_(t, TOK_WHITESPACE))
4700 last = t, t = t->next;
4701 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004702 if (!tok_type_(t, TOK_ID) || !(m = is_mmacro(t, &params)))
4703 return 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004704 last->next = NULL;
Keith Kanios891775e2009-07-11 06:08:54 -05004705 mname = t->text;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004706 tline = t;
H. Peter Anvineba20a72002-04-30 20:53:55 +00004707 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004708
4709 /*
4710 * Fix up the parameters: this involves stripping leading and
4711 * trailing whitespace, then stripping braces if they are
4712 * present.
4713 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004714 for (nparam = 0; params[nparam]; nparam++) ;
H. Peter Anvin734b1882002-04-30 21:01:08 +00004715 paramlen = nparam ? nasm_malloc(nparam * sizeof(*paramlen)) : NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004716
H. Peter Anvine2c80182005-01-15 22:15:51 +00004717 for (i = 0; params[i]; i++) {
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08004718 int brace = 0;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004719 int comma = (!m->plus || i < nparam - 1);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004720
H. Peter Anvine2c80182005-01-15 22:15:51 +00004721 t = params[i];
4722 skip_white_(t);
4723 if (tok_is_(t, "{"))
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08004724 t = t->next, brace++, comma = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004725 params[i] = t;
4726 paramlen[i] = 0;
4727 while (t) {
4728 if (comma && t->type == TOK_OTHER && !strcmp(t->text, ","))
4729 break; /* ... because we have hit a comma */
4730 if (comma && t->type == TOK_WHITESPACE
4731 && tok_is_(t->next, ","))
4732 break; /* ... or a space then a comma */
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08004733 if (brace && t->type == TOK_OTHER) {
4734 if (t->text[0] == '{')
4735 brace++; /* ... or a nested opening brace */
4736 else if (t->text[0] == '}')
4737 if (!--brace)
4738 break; /* ... or a brace */
4739 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00004740 t = t->next;
4741 paramlen[i]++;
4742 }
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08004743 if (brace)
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03004744 nasm_nonfatal("macro params should be enclosed in braces");
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004745 }
4746
4747 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004748 * OK, we have a MMacro structure together with a set of
4749 * parameters. We must now go through the expansion and push
4750 * copies of each Line on to istk->expansion. Substitution of
H. Peter Anvin76690a12002-04-30 20:52:49 +00004751 * parameter tokens and macro-local tokens doesn't get done
4752 * until the single-line macro substitution process; this is
4753 * because delaying them allows us to change the semantics
4754 * later through %rotate.
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004755 *
4756 * First, push an end marker on to istk->expansion, mark this
4757 * macro as in progress, and set up its invocation-specific
4758 * variables.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004759 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004760 ll = nasm_malloc(sizeof(Line));
4761 ll->next = istk->expansion;
4762 ll->finishes = m;
4763 ll->first = NULL;
4764 istk->expansion = ll;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004765
4766 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004767 * Save the previous MMacro expansion in the case of
4768 * macro recursion
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004769 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004770 if (m->max_depth && m->in_progress)
4771 push_mmacro(m);
4772
4773 m->in_progress ++;
4774 m->params = params;
4775 m->iline = tline;
4776 m->nparam = nparam;
4777 m->rotate = 0;
4778 m->paramlen = paramlen;
4779 m->unique = unique++;
4780 m->lineno = 0;
4781 m->condcnt = 0;
4782
4783 m->next_active = istk->mstk;
4784 istk->mstk = m;
4785
4786 list_for_each(l, m->expansion) {
4787 Token **tail;
4788
4789 ll = nasm_malloc(sizeof(Line));
4790 ll->finishes = NULL;
4791 ll->next = istk->expansion;
4792 istk->expansion = ll;
4793 tail = &ll->first;
4794
4795 list_for_each(t, l->first) {
4796 Token *x = t;
4797 switch (t->type) {
4798 case TOK_PREPROC_Q:
4799 tt = *tail = new_Token(NULL, TOK_ID, mname, 0);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004800 break;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004801 case TOK_PREPROC_QQ:
4802 tt = *tail = new_Token(NULL, TOK_ID, m->name, 0);
4803 break;
4804 case TOK_PREPROC_ID:
4805 if (t->text[1] == '0' && t->text[2] == '0') {
4806 dont_prepend = -1;
4807 x = label;
4808 if (!x)
4809 continue;
4810 }
4811 /* fall through */
4812 default:
4813 tt = *tail = new_Token(NULL, x->type, x->text, 0);
4814 break;
4815 }
4816 tail = &tt->next;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004817 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004818 *tail = NULL;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004819 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004820
4821 /*
H. Peter Anvineba20a72002-04-30 20:53:55 +00004822 * If we had a label, push it on as the first line of
4823 * the macro expansion.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004824 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004825 if (label) {
4826 if (dont_prepend < 0)
4827 free_tlist(startline);
4828 else {
4829 ll = nasm_malloc(sizeof(Line));
4830 ll->finishes = NULL;
4831 ll->next = istk->expansion;
4832 istk->expansion = ll;
4833 ll->first = startline;
4834 if (!dont_prepend) {
4835 while (label->next)
4836 label = label->next;
4837 label->next = tt = new_Token(NULL, TOK_OTHER, ":", 0);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004838 }
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004839 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004840 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004841
H. Peter Anvin8ac25aa2016-02-18 01:16:18 -08004842 lfmt->uplevel(m->nolist ? LIST_MACRO_NOLIST : LIST_MACRO);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004843
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004844 return 1;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004845}
4846
H. Peter Anvin130736c2016-02-17 20:27:41 -08004847/*
4848 * This function adds macro names to error messages, and suppresses
4849 * them if necessary.
4850 */
H. Peter Anvin (Intel)6bde2ed2018-12-13 19:39:41 -08004851static void pp_verror(errflags severity, const char *fmt, va_list arg)
Victor van den Elzen3b404c02008-09-18 13:51:36 +02004852{
H. Peter Anvin130736c2016-02-17 20:27:41 -08004853 char buff[BUFSIZ];
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004854 MMacro *mmac = NULL;
4855 int delta = 0;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02004856
H. Peter Anvin130736c2016-02-17 20:27:41 -08004857 /*
4858 * If we're in a dead branch of IF or something like it, ignore the error.
4859 * However, because %else etc are evaluated in the state context
4860 * of the previous branch, errors might get lost:
4861 * %if 0 ... %else trailing garbage ... %endif
4862 * So %else etc should set the ERR_PP_PRECOND flag.
4863 */
4864 if ((severity & ERR_MASK) < ERR_FATAL &&
4865 istk && istk->conds &&
4866 ((severity & ERR_PP_PRECOND) ?
4867 istk->conds->state == COND_NEVER :
H. Peter Anvineb6653f2016-04-05 13:03:10 -07004868 !emitting(istk->conds->state)))
H. Peter Anvin130736c2016-02-17 20:27:41 -08004869 return;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02004870
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004871 /* get %macro name */
H. Peter Anvin130736c2016-02-17 20:27:41 -08004872 if (!(severity & ERR_NOFILE) && istk && istk->mstk) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004873 mmac = istk->mstk;
4874 /* but %rep blocks should be skipped */
4875 while (mmac && !mmac->name)
4876 mmac = mmac->next_active, delta++;
Cyrill Gorcunov9900c6b2011-10-09 18:58:46 +04004877 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004878
H. Peter Anvin130736c2016-02-17 20:27:41 -08004879 if (mmac) {
4880 vsnprintf(buff, sizeof(buff), fmt, arg);
Victor van den Elzen3b404c02008-09-18 13:51:36 +02004881
H. Peter Anvin130736c2016-02-17 20:27:41 -08004882 nasm_set_verror(real_verror);
4883 nasm_error(severity, "(%s:%d) %s",
4884 mmac->name, mmac->lineno - delta, buff);
4885 nasm_set_verror(pp_verror);
4886 } else {
4887 real_verror(severity, fmt, arg);
4888 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004889}
4890
H. Peter Anvin734b1882002-04-30 21:01:08 +00004891static void
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08004892pp_reset(const char *file, enum preproc_mode mode, struct strlist *dep_list)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004893{
H. Peter Anvin7383b402008-09-24 10:20:40 -07004894 Token *t;
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08004895 int apass;
H. Peter Anvin7383b402008-09-24 10:20:40 -07004896
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004897 cstk = NULL;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004898 istk = nasm_malloc(sizeof(Include));
4899 istk->next = NULL;
4900 istk->conds = NULL;
4901 istk->expansion = NULL;
4902 istk->mstk = NULL;
H. Peter Anvin3e83cec2016-05-25 04:28:46 -07004903 istk->fp = nasm_open_read(file, NF_TEXT);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004904 istk->fname = NULL;
H. Peter Anvin274cda82016-05-10 02:56:29 -07004905 src_set(0, file);
H. Peter Anvineba20a72002-04-30 20:53:55 +00004906 istk->lineinc = 1;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004907 if (!istk->fp)
Cyrill Gorcunovc3527dd2018-11-24 22:17:47 +03004908 nasm_fatalf(ERR_NOFILE, "unable to open input file `%s'", file);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004909 defining = NULL;
Charles Crayned4200be2008-07-12 16:42:33 -07004910 nested_mac_count = 0;
4911 nested_rep_count = 0;
H. Peter Anvin97a23472007-09-16 17:57:25 -07004912 init_macros();
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004913 unique = 0;
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07004914 deplist = dep_list;
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08004915 pp_mode = mode;
H. Peter Anvinf7606612016-07-13 14:23:48 -07004916
4917 if (tasm_compatible_mode)
4918 pp_add_stdmac(nasm_stdmac_tasm);
4919
4920 pp_add_stdmac(nasm_stdmac_nasm);
4921 pp_add_stdmac(nasm_stdmac_version);
4922
Cyrill Gorcunov15ce78f2017-01-06 20:21:28 +03004923 if (extrastdmac)
4924 pp_add_stdmac(extrastdmac);
4925
H. Peter Anvinf7606612016-07-13 14:23:48 -07004926 stdmacpos = stdmacros[0];
4927 stdmacnext = &stdmacros[1];
4928
H. Peter Anvind2456592008-06-19 15:04:18 -07004929 do_predef = true;
H. Peter Anvin61f130f2008-09-25 15:45:06 -07004930
Cyrill Gorcunovb7bb5ac2018-11-11 21:33:52 +03004931 strlist_add(deplist, file);
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07004932
H. Peter Anvin61f130f2008-09-25 15:45:06 -07004933 /*
4934 * Define the __PASS__ macro. This is defined here unlike
4935 * all the other builtins, because it is special -- it varies between
4936 * passes.
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08004937 *
4938 * 0 = dependencies only
4939 * 1 = preparatory passes
4940 * 2 = final pass
4941 * 3 = preproces only
H. Peter Anvin61f130f2008-09-25 15:45:06 -07004942 */
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08004943 switch (mode) {
4944 case PP_NORMAL:
4945 apass = pass_final() ? 2 : 1;
4946 break;
4947 case PP_DEPS:
4948 apass = 0;
4949 break;
4950 case PP_PREPROC:
4951 apass = 3;
4952 break;
4953 default:
4954 panic();
4955 }
4956
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004957 t = nasm_malloc(sizeof(*t));
4958 t->next = NULL;
H. Peter Anvin61f130f2008-09-25 15:45:06 -07004959 make_tok_num(t, apass);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004960 t->a.mac = NULL;
H. Peter Anvin7383b402008-09-24 10:20:40 -07004961 define_smacro(NULL, "__PASS__", true, 0, t);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004962}
4963
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07004964static void pp_init(void)
4965{
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07004966}
4967
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004968static char *pp_getline(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004969{
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004970 char *line;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004971 Token *tline;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004972
H. Peter Anvin130736c2016-02-17 20:27:41 -08004973 real_verror = nasm_set_verror(pp_verror);
H. Peter Anvin215186f2016-02-17 20:27:41 -08004974
H. Peter Anvine2c80182005-01-15 22:15:51 +00004975 while (1) {
4976 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004977 * Fetch a tokenized line, either from the macro-expansion
H. Peter Anvine2c80182005-01-15 22:15:51 +00004978 * buffer or from the input file.
4979 */
4980 tline = NULL;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004981 while (istk->expansion && istk->expansion->finishes) {
4982 Line *l = istk->expansion;
4983 if (!l->finishes->name && l->finishes->in_progress > 1) {
4984 Line *ll;
H. Peter Anvineba20a72002-04-30 20:53:55 +00004985
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004986 /*
4987 * This is a macro-end marker for a macro with no
4988 * name, which means it's not really a macro at all
4989 * but a %rep block, and the `in_progress' field is
4990 * more than 1, meaning that we still need to
4991 * repeat. (1 means the natural last repetition; 0
4992 * means termination by %exitrep.) We have
4993 * therefore expanded up to the %endrep, and must
4994 * push the whole block on to the expansion buffer
4995 * again. We don't bother to remove the macro-end
4996 * marker: we'd only have to generate another one
4997 * if we did.
4998 */
4999 l->finishes->in_progress--;
5000 list_for_each(l, l->finishes->expansion) {
5001 Token *t, *tt, **tail;
5002
5003 ll = nasm_malloc(sizeof(Line));
5004 ll->next = istk->expansion;
5005 ll->finishes = NULL;
5006 ll->first = NULL;
5007 tail = &ll->first;
5008
5009 list_for_each(t, l->first) {
5010 if (t->text || t->type == TOK_WHITESPACE) {
5011 tt = *tail = new_Token(NULL, t->type, t->text, 0);
5012 tail = &tt->next;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005013 }
5014 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005015
5016 istk->expansion = ll;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005017 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005018 } else {
5019 /*
5020 * Check whether a `%rep' was started and not ended
5021 * within this macro expansion. This can happen and
5022 * should be detected. It's a fatal error because
5023 * I'm too confused to work out how to recover
5024 * sensibly from it.
5025 */
5026 if (defining) {
5027 if (defining->name)
H. Peter Anvinc5136902018-06-15 18:20:17 -07005028 nasm_panic("defining with name in expansion");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005029 else if (istk->mstk->name)
H. Peter Anvinc5136902018-06-15 18:20:17 -07005030 nasm_fatal("`%%rep' without `%%endrep' within"
H. Peter Anvin130736c2016-02-17 20:27:41 -08005031 " expansion of macro `%s'",
5032 istk->mstk->name);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005033 }
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005034
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005035 /*
5036 * FIXME: investigate the relationship at this point between
5037 * istk->mstk and l->finishes
5038 */
5039 {
5040 MMacro *m = istk->mstk;
5041 istk->mstk = m->next_active;
5042 if (m->name) {
5043 /*
5044 * This was a real macro call, not a %rep, and
5045 * therefore the parameter information needs to
5046 * be freed.
5047 */
5048 if (m->prev) {
5049 pop_mmacro(m);
5050 l->finishes->in_progress --;
5051 } else {
5052 nasm_free(m->params);
5053 free_tlist(m->iline);
5054 nasm_free(m->paramlen);
5055 l->finishes->in_progress = 0;
5056 }
Adam Majer91e72402017-07-25 10:42:01 +02005057 }
5058
5059 /*
5060 * FIXME It is incorrect to always free_mmacro here.
5061 * It leads to usage-after-free.
5062 *
5063 * https://bugzilla.nasm.us/show_bug.cgi?id=3392414
5064 */
5065#if 0
5066 else
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005067 free_mmacro(m);
Adam Majer91e72402017-07-25 10:42:01 +02005068#endif
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005069 }
5070 istk->expansion = l->next;
5071 nasm_free(l);
H. Peter Anvin8ac25aa2016-02-18 01:16:18 -08005072 lfmt->downlevel(LIST_MACRO);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005073 }
5074 }
5075 while (1) { /* until we get a line we can use */
5076
5077 if (istk->expansion) { /* from a macro expansion */
5078 char *p;
5079 Line *l = istk->expansion;
5080 if (istk->mstk)
5081 istk->mstk->lineno++;
5082 tline = l->first;
5083 istk->expansion = l->next;
5084 nasm_free(l);
5085 p = detoken(tline, false);
H. Peter Anvin8ac25aa2016-02-18 01:16:18 -08005086 lfmt->line(LIST_MACRO, p);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005087 nasm_free(p);
5088 break;
5089 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00005090 line = read_line();
5091 if (line) { /* from the current input file */
5092 line = prepreproc(line);
Keith Kaniosb7a89542007-04-12 02:40:54 +00005093 tline = tokenize(line);
H. Peter Anvine2c80182005-01-15 22:15:51 +00005094 nasm_free(line);
5095 break;
5096 }
5097 /*
5098 * The current file has ended; work down the istk
5099 */
5100 {
5101 Include *i = istk;
5102 fclose(i->fp);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005103 if (i->conds) {
5104 /* nasm_error can't be conditionally suppressed */
H. Peter Anvinc5136902018-06-15 18:20:17 -07005105 nasm_fatal("expected `%%endif' before end of file");
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005106 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00005107 /* only set line and file name if there's a next node */
H. Peter Anvin274cda82016-05-10 02:56:29 -07005108 if (i->next)
5109 src_set(i->lineno, i->fname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00005110 istk = i->next;
H. Peter Anvin8ac25aa2016-02-18 01:16:18 -08005111 lfmt->downlevel(LIST_INCLUDE);
H. Peter Anvine2c80182005-01-15 22:15:51 +00005112 nasm_free(i);
H. Peter Anvin130736c2016-02-17 20:27:41 -08005113 if (!istk) {
5114 line = NULL;
5115 goto done;
5116 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005117 if (istk->expansion && istk->expansion->finishes)
5118 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005119 }
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005120 }
5121
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005122 /*
5123 * We must expand MMacro parameters and MMacro-local labels
5124 * _before_ we plunge into directive processing, to cope
5125 * with things like `%define something %1' such as STRUC
5126 * uses. Unless we're _defining_ a MMacro, in which case
5127 * those tokens should be left alone to go into the
5128 * definition; and unless we're in a non-emitting
5129 * condition, in which case we don't want to meddle with
5130 * anything.
5131 */
5132 if (!defining && !(istk->conds && !emitting(istk->conds->state))
5133 && !(istk->mstk && !istk->mstk->in_progress)) {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005134 tline = expand_mmac_params(tline);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005135 }
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005136
H. Peter Anvine2c80182005-01-15 22:15:51 +00005137 /*
5138 * Check the line to see if it's a preprocessor directive.
5139 */
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07005140 if (do_directive(tline, &line) == DIRECTIVE_FOUND) {
5141 if (line)
5142 break; /* Directive generated output */
5143 else
5144 continue;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005145 } else if (defining) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00005146 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005147 * We're defining a multi-line macro. We emit nothing
5148 * at all, and just
5149 * shove the tokenized line on to the macro definition.
H. Peter Anvine2c80182005-01-15 22:15:51 +00005150 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005151 Line *l = nasm_malloc(sizeof(Line));
5152 l->next = defining->expansion;
5153 l->first = tline;
5154 l->finishes = NULL;
5155 defining->expansion = l;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005156 continue;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005157 } else if (istk->conds && !emitting(istk->conds->state)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00005158 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005159 * We're in a non-emitting branch of a condition block.
H. Peter Anvine2c80182005-01-15 22:15:51 +00005160 * Emit nothing at all, not even a blank line: when we
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005161 * emerge from the condition we'll give a line-number
H. Peter Anvine2c80182005-01-15 22:15:51 +00005162 * directive so we keep our place correctly.
5163 */
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005164 free_tlist(tline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00005165 continue;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005166 } else if (istk->mstk && !istk->mstk->in_progress) {
5167 /*
5168 * We're in a %rep block which has been terminated, so
5169 * we're walking through to the %endrep without
5170 * emitting anything. Emit nothing at all, not even a
5171 * blank line: when we emerge from the %rep block we'll
5172 * give a line-number directive so we keep our place
5173 * correctly.
5174 */
5175 free_tlist(tline);
5176 continue;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005177 } else {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005178 tline = expand_smacro(tline);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005179 if (!expand_mmacro(tline)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00005180 /*
Keith Kaniosb7a89542007-04-12 02:40:54 +00005181 * De-tokenize the line again, and emit it.
H. Peter Anvine2c80182005-01-15 22:15:51 +00005182 */
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005183 line = detoken(tline, true);
5184 free_tlist(tline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00005185 break;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005186 } else {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005187 continue; /* expand_mmacro calls free_tlist */
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005188 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00005189 }
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005190 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005191
H. Peter Anvin130736c2016-02-17 20:27:41 -08005192done:
5193 nasm_set_verror(real_verror);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005194 return line;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005195}
5196
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08005197static void pp_cleanup_pass(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005198{
H. Peter Anvin130736c2016-02-17 20:27:41 -08005199 real_verror = nasm_set_verror(pp_verror);
H. Peter Anvin215186f2016-02-17 20:27:41 -08005200
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005201 if (defining) {
5202 if (defining->name) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03005203 nasm_nonfatal("end of file while still defining macro `%s'",
5204 defining->name);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005205 } else {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03005206 nasm_nonfatal("end of file while still in %%rep");
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005207 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005208
5209 free_mmacro(defining);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005210 defining = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005211 }
H. Peter Anvin130736c2016-02-17 20:27:41 -08005212
5213 nasm_set_verror(real_verror);
H. Peter Anvin215186f2016-02-17 20:27:41 -08005214
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005215 while (cstk)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005216 ctx_pop();
H. Peter Anvin97a23472007-09-16 17:57:25 -07005217 free_macros();
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005218 while (istk) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00005219 Include *i = istk;
5220 istk = istk->next;
5221 fclose(i->fp);
Cyrill Gorcunov8dcfd882011-03-03 09:18:56 +03005222 nasm_free(i);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005223 }
5224 while (cstk)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005225 ctx_pop();
H. Peter Anvin274cda82016-05-10 02:56:29 -07005226 src_set_fname(NULL);
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08005227}
5228
5229static void pp_cleanup_session(void)
5230{
5231 free_llist(predef);
5232 predef = NULL;
5233 delete_Blocks();
5234 freeTokens = NULL;
5235 ipath_list = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005236}
5237
Cyrill Gorcunov8c0666b2018-11-24 14:33:48 +03005238static void pp_include_path(struct strlist *list)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005239{
Cyrill Gorcunov8c0666b2018-11-24 14:33:48 +03005240 ipath_list = list;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005241}
Frank Kotlerd0ed6fd2003-08-27 11:33:56 +00005242
Cyrill Gorcunov0b78bff2012-05-07 01:57:55 +04005243static void pp_pre_include(char *fname)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005244{
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005245 Token *inc, *space, *name;
5246 Line *l;
5247
H. Peter Anvin734b1882002-04-30 21:01:08 +00005248 name = new_Token(NULL, TOK_INTERNAL_STRING, fname, 0);
5249 space = new_Token(name, TOK_WHITESPACE, NULL, 0);
5250 inc = new_Token(space, TOK_PREPROC_ID, "%include", 0);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005251
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005252 l = nasm_malloc(sizeof(Line));
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005253 l->next = predef;
5254 l->first = inc;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005255 l->finishes = NULL;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005256 predef = l;
5257}
5258
Cyrill Gorcunov0b78bff2012-05-07 01:57:55 +04005259static void pp_pre_define(char *definition)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005260{
5261 Token *def, *space;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005262 Line *l;
Keith Kaniosa6dfa782007-04-13 16:47:53 +00005263 char *equals;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005264
H. Peter Anvin130736c2016-02-17 20:27:41 -08005265 real_verror = nasm_set_verror(pp_verror);
H. Peter Anvin215186f2016-02-17 20:27:41 -08005266
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005267 equals = strchr(definition, '=');
H. Peter Anvin734b1882002-04-30 21:01:08 +00005268 space = new_Token(NULL, TOK_WHITESPACE, NULL, 0);
5269 def = new_Token(space, TOK_PREPROC_ID, "%define", 0);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005270 if (equals)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005271 *equals = ' ';
Keith Kaniosb7a89542007-04-12 02:40:54 +00005272 space->next = tokenize(definition);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005273 if (equals)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005274 *equals = '=';
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005275
Cyrill Gorcunov6d42e9b2015-02-08 11:07:17 +03005276 if (space->next->type != TOK_PREPROC_ID &&
5277 space->next->type != TOK_ID)
H. Peter Anvin (Intel)80c4f232018-12-14 13:33:24 -08005278 nasm_warn(WARN_OTHER, "pre-defining non ID `%s\'\n", definition);
Cyrill Gorcunov6d42e9b2015-02-08 11:07:17 +03005279
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005280 l = nasm_malloc(sizeof(Line));
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005281 l->next = predef;
5282 l->first = def;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005283 l->finishes = NULL;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005284 predef = l;
H. Peter Anvin130736c2016-02-17 20:27:41 -08005285
5286 nasm_set_verror(real_verror);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005287}
5288
Cyrill Gorcunov0b78bff2012-05-07 01:57:55 +04005289static void pp_pre_undefine(char *definition)
H. Peter Anvin620515a2002-04-30 20:57:38 +00005290{
5291 Token *def, *space;
5292 Line *l;
5293
H. Peter Anvin734b1882002-04-30 21:01:08 +00005294 space = new_Token(NULL, TOK_WHITESPACE, NULL, 0);
5295 def = new_Token(space, TOK_PREPROC_ID, "%undef", 0);
Keith Kaniosb7a89542007-04-12 02:40:54 +00005296 space->next = tokenize(definition);
H. Peter Anvin620515a2002-04-30 20:57:38 +00005297
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005298 l = nasm_malloc(sizeof(Line));
H. Peter Anvin620515a2002-04-30 20:57:38 +00005299 l->next = predef;
5300 l->first = def;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005301 l->finishes = NULL;
H. Peter Anvin620515a2002-04-30 20:57:38 +00005302 predef = l;
5303}
5304
H. Peter Anvin05990342018-06-11 13:32:42 -07005305/* Insert an early preprocessor command that doesn't need special handling */
5306static void pp_pre_command(const char *what, char *string)
5307{
5308 char *cmd;
5309 Token *def, *space;
5310 Line *l;
5311
5312 def = tokenize(string);
5313 if (what) {
5314 cmd = nasm_strcat(what[0] == '%' ? "" : "%", what);
5315 space = new_Token(def, TOK_WHITESPACE, NULL, 0);
5316 def = new_Token(space, TOK_PREPROC_ID, cmd, 0);
5317 }
5318
5319 l = nasm_malloc(sizeof(Line));
5320 l->next = predef;
5321 l->first = def;
5322 l->finishes = NULL;
5323 predef = l;
5324}
5325
H. Peter Anvinf7606612016-07-13 14:23:48 -07005326static void pp_add_stdmac(macros_t *macros)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005327{
H. Peter Anvinf7606612016-07-13 14:23:48 -07005328 macros_t **mp;
5329
5330 /* Find the end of the list and avoid duplicates */
5331 for (mp = stdmacros; *mp; mp++) {
5332 if (*mp == macros)
5333 return; /* Nothing to do */
5334 }
5335
5336 nasm_assert(mp < &stdmacros[ARRAY_SIZE(stdmacros)-1]);
5337
5338 *mp = macros;
H. Peter Anvin76690a12002-04-30 20:52:49 +00005339}
5340
Cyrill Gorcunov15ce78f2017-01-06 20:21:28 +03005341static void pp_extra_stdmac(macros_t *macros)
5342{
5343 extrastdmac = macros;
5344}
5345
Keith Kaniosa5fc6462007-10-13 07:09:22 -07005346static void make_tok_num(Token * tok, int64_t val)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005347{
Cyrill Gorcunovce652742013-05-06 23:43:43 +04005348 char numbuf[32];
Keith Kaniosa5fc6462007-10-13 07:09:22 -07005349 snprintf(numbuf, sizeof(numbuf), "%"PRId64"", val);
H. Peter Anvineba20a72002-04-30 20:53:55 +00005350 tok->text = nasm_strdup(numbuf);
5351 tok->type = TOK_NUMBER;
5352}
5353
H. Peter Anvin (Intel)6bde2ed2018-12-13 19:39:41 -08005354static void pp_list_one_macro(MMacro *m, errflags severity)
H. Peter Anvin37368952016-05-09 14:10:32 -07005355{
5356 if (!m)
5357 return;
5358
5359 /* We need to print the next_active list in reverse order */
5360 pp_list_one_macro(m->next_active, severity);
5361
5362 if (m->name && !m->nolist) {
H. Peter Anvin274cda82016-05-10 02:56:29 -07005363 src_set(m->xline + m->lineno, m->fname);
H. Peter Anvinddb29062018-12-11 00:06:29 -08005364 nasm_error(severity, "... from macro `%s' defined", m->name);
H. Peter Anvin37368952016-05-09 14:10:32 -07005365 }
5366}
5367
H. Peter Anvin (Intel)6bde2ed2018-12-13 19:39:41 -08005368static void pp_error_list_macros(errflags severity)
H. Peter Anvin4def1a82016-05-09 13:59:44 -07005369{
H. Peter Anvinddb29062018-12-11 00:06:29 -08005370 struct src_location saved;
H. Peter Anvin4def1a82016-05-09 13:59:44 -07005371
H. Peter Anvinddb29062018-12-11 00:06:29 -08005372 severity |= ERR_PP_LISTMACRO | ERR_NO_SEVERITY | ERR_HERE;
5373 saved = src_where();
H. Peter Anvin4def1a82016-05-09 13:59:44 -07005374
Cyrill Gorcunov771d04e2016-05-10 23:27:03 +03005375 if (istk)
5376 pp_list_one_macro(istk->mstk, severity);
H. Peter Anvin4def1a82016-05-09 13:59:44 -07005377
H. Peter Anvinddb29062018-12-11 00:06:29 -08005378 src_update(saved);
H. Peter Anvin4def1a82016-05-09 13:59:44 -07005379}
5380
H. Peter Anvine7469712016-02-18 02:20:59 -08005381const struct preproc_ops nasmpp = {
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07005382 pp_init,
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005383 pp_reset,
5384 pp_getline,
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08005385 pp_cleanup_pass,
5386 pp_cleanup_session,
Cyrill Gorcunov15ce78f2017-01-06 20:21:28 +03005387 pp_extra_stdmac,
Cyrill Gorcunov0b78bff2012-05-07 01:57:55 +04005388 pp_pre_define,
5389 pp_pre_undefine,
5390 pp_pre_include,
H. Peter Anvin05990342018-06-11 13:32:42 -07005391 pp_pre_command,
H. Peter Anvin4def1a82016-05-09 13:59:44 -07005392 pp_include_path,
5393 pp_error_list_macros,
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005394};