blob: a8388bcbba46061216803f68bc36a15387c855e2 [file] [log] [blame]
H. Peter Anvin9e6747c2009-06-28 17:13:04 -07001/* ----------------------------------------------------------------------- *
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002 *
H. Peter Anvina6e26d92017-03-07 21:32:37 -08003 * Copyright 1996-2017 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 Anvind7ed89e2002-04-30 20:52:08 +000065#include <stdio.h>
H. Peter Anvinaf535c12002-04-30 20:59:21 +000066#include <stdarg.h>
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000067#include <stdlib.h>
68#include <stddef.h>
69#include <string.h>
70#include <ctype.h>
H. Peter Anvin76690a12002-04-30 20:52:49 +000071#include <limits.h>
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000072
73#include "nasm.h"
74#include "nasmlib.h"
H. Peter Anvinb20bc732017-03-07 19:23:03 -080075#include "error.h"
H. Peter Anvin4169a472007-09-12 01:29:43 +000076#include "preproc.h"
H. Peter Anvin97a23472007-09-16 17:57:25 -070077#include "hashtbl.h"
H. Peter Anvin8cad14b2008-06-01 17:23:51 -070078#include "quote.h"
H. Peter Anvinc2df2822007-10-24 15:29:28 -070079#include "stdscan.h"
H. Peter Anvindbb640b2009-07-18 18:57:16 -070080#include "eval.h"
H. Peter Anvinc2df2822007-10-24 15:29:28 -070081#include "tokens.h"
H. Peter Anvina4835d42008-05-20 14:21:29 -070082#include "tables.h"
H. Peter Anvin8ac25aa2016-02-18 01:16:18 -080083#include "listing.h"
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000084
85typedef struct SMacro SMacro;
H. Peter Anvin36206cd2012-03-03 16:14:51 -080086typedef struct MMacro MMacro;
87typedef struct MMacroInvocation MMacroInvocation;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000088typedef struct Context Context;
89typedef struct Token Token;
H. Peter Anvince616072002-04-30 21:02:23 +000090typedef struct Blocks Blocks;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000091typedef struct Line Line;
92typedef struct Include Include;
H. Peter Anvin36206cd2012-03-03 16:14:51 -080093typedef struct Cond Cond;
H. Peter Anvin6768eb72002-04-30 20:52:26 +000094typedef struct IncPath IncPath;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000095
96/*
H. Peter Anvin97a23472007-09-16 17:57:25 -070097 * Note on the storage of both SMacro and MMacros: the hash table
98 * indexes them case-insensitively, and we then have to go through a
99 * linked list of potential case aliases (and, for MMacros, parameter
100 * ranges); this is to preserve the matching semantics of the earlier
101 * code. If the number of case aliases for a specific macro is a
102 * performance issue, you may want to reconsider your coding style.
103 */
104
105/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000106 * Store the definition of a single-line macro.
107 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000108struct SMacro {
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800109 SMacro *next;
110 char *name;
111 bool casesense;
112 bool in_progress;
113 unsigned int nparam;
114 Token *expansion;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000115};
116
117/*
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800118 * Store the definition of a multi-line macro. This is also used to
119 * store the interiors of `%rep...%endrep' blocks, which are
120 * effectively self-re-invoking multi-line macros which simply
121 * don't have a name or bother to appear in the hash tables. %rep
122 * blocks are signified by having a NULL `name' field.
123 *
124 * In a MMacro describing a `%rep' block, the `in_progress' field
125 * isn't merely boolean, but gives the number of repeats left to
126 * run.
127 *
128 * The `next' field is used for storing MMacros in hash tables; the
129 * `next_active' field is for stacking them on istk entries.
130 *
131 * When a MMacro is being expanded, `params', `iline', `nparam',
132 * `paramlen', `rotate' and `unique' are local to the invocation.
133 */
134struct MMacro {
135 MMacro *next;
136 MMacroInvocation *prev; /* previous invocation */
137 char *name;
138 int nparam_min, nparam_max;
139 bool casesense;
140 bool plus; /* is the last parameter greedy? */
141 bool nolist; /* is this macro listing-inhibited? */
142 int64_t in_progress; /* is this macro currently being expanded? */
143 int32_t max_depth; /* maximum number of recursive expansions allowed */
144 Token *dlist; /* All defaults as one list */
145 Token **defaults; /* Parameter default pointers */
146 int ndefs; /* number of default parameters */
147 Line *expansion;
148
149 MMacro *next_active;
150 MMacro *rep_nest; /* used for nesting %rep */
151 Token **params; /* actual parameters */
152 Token *iline; /* invocation line */
153 unsigned int nparam, rotate;
154 int *paramlen;
155 uint64_t unique;
156 int lineno; /* Current line number on expansion */
157 uint64_t condcnt; /* number of if blocks... */
H. Peter Anvin4def1a82016-05-09 13:59:44 -0700158
H. Peter Anvin274cda82016-05-10 02:56:29 -0700159 const char *fname; /* File where defined */
H. Peter Anvin4def1a82016-05-09 13:59:44 -0700160 int32_t xline; /* First line in macro */
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800161};
162
163
164/* Store the definition of a multi-line macro, as defined in a
165 * previous recursive macro expansion.
166 */
167struct MMacroInvocation {
168 MMacroInvocation *prev; /* previous invocation */
169 Token **params; /* actual parameters */
170 Token *iline; /* invocation line */
171 unsigned int nparam, rotate;
172 int *paramlen;
173 uint64_t unique;
174 uint64_t condcnt;
175};
176
177
178/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000179 * The context stack is composed of a linked list of these.
180 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000181struct Context {
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800182 Context *next;
183 char *name;
184 struct hash_table localmac;
185 uint32_t number;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000186};
187
188/*
189 * This is the internal form which we break input lines up into.
190 * Typically stored in linked lists.
191 *
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000192 * Note that `type' serves a double meaning: TOK_SMAC_PARAM is not
193 * necessarily used as-is, but is intended to denote the number of
194 * the substituted parameter. So in the definition
195 *
196 * %define a(x,y) ( (x) & ~(y) )
H. Peter Anvin70653092007-10-19 14:42:29 -0700197 *
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000198 * the token representing `x' will have its type changed to
199 * TOK_SMAC_PARAM, but the one representing `y' will be
200 * TOK_SMAC_PARAM+1.
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000201 *
202 * TOK_INTERNAL_STRING is a dirty hack: it's a single string token
203 * which doesn't need quotes around it. Used in the pre-include
204 * mechanism as an alternative to trying to find a sensible type of
205 * quote to use on the filename we were passed.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000206 */
H. Peter Anvinda10e7b2007-09-12 04:18:37 +0000207enum pp_token_type {
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800208 TOK_NONE = 0, TOK_WHITESPACE, TOK_COMMENT, TOK_ID,
209 TOK_PREPROC_ID, TOK_STRING,
210 TOK_NUMBER, TOK_FLOAT, TOK_SMAC_END, TOK_OTHER,
H. Peter Anvin6c81f0a2008-05-25 21:46:17 -0700211 TOK_INTERNAL_STRING,
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800212 TOK_PREPROC_Q, TOK_PREPROC_QQ,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300213 TOK_PASTE, /* %+ */
214 TOK_INDIRECT, /* %[...] */
215 TOK_SMAC_PARAM, /* MUST BE LAST IN THE LIST!!! */
216 TOK_MAX = INT_MAX /* Keep compiler from reducing the range */
H. Peter Anvinda10e7b2007-09-12 04:18:37 +0000217};
218
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +0400219#define PP_CONCAT_MASK(x) (1 << (x))
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +0400220#define PP_CONCAT_MATCH(t, mask) (PP_CONCAT_MASK((t)->type) & mask)
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +0400221
Cyrill Gorcunov575d4282010-10-06 00:25:55 +0400222struct tokseq_match {
223 int mask_head;
224 int mask_tail;
225};
226
H. Peter Anvine2c80182005-01-15 22:15:51 +0000227struct Token {
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800228 Token *next;
229 char *text;
H. Peter Anvinf26e0972008-07-01 21:26:27 -0700230 union {
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800231 SMacro *mac; /* associated macro for TOK_SMAC_END */
232 size_t len; /* scratch length field */
233 } a; /* Auxiliary data */
234 enum pp_token_type type;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000235};
236
237/*
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800238 * Multi-line macro definitions are stored as a linked list of
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000239 * these, which is essentially a container to allow several linked
240 * lists of Tokens.
H. Peter Anvin70653092007-10-19 14:42:29 -0700241 *
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000242 * Note that in this module, linked lists are treated as stacks
243 * wherever possible. For this reason, Lines are _pushed_ on to the
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800244 * `expansion' field in MMacro structures, so that the linked list,
245 * if walked, would give the macro lines in reverse order; this
246 * means that we can walk the list when expanding a macro, and thus
247 * push the lines on to the `expansion' field in _istk_ in reverse
248 * order (so that when popped back off they are in the right
249 * order). It may seem cockeyed, and it relies on my design having
250 * an even number of steps in, but it works...
251 *
252 * Some of these structures, rather than being actual lines, are
253 * markers delimiting the end of the expansion of a given macro.
254 * This is for use in the cycle-tracking and %rep-handling code.
255 * Such structures have `finishes' non-NULL, and `first' NULL. All
256 * others have `finishes' NULL, but `first' may still be NULL if
257 * the line is blank.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000258 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000259struct Line {
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800260 Line *next;
261 MMacro *finishes;
262 Token *first;
Keith Kaniosb307a4f2010-11-06 17:41:51 -0500263};
264
265/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000266 * To handle an arbitrary level of file inclusion, we maintain a
267 * stack (ie linked list) of these things.
268 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000269struct Include {
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800270 Include *next;
271 FILE *fp;
272 Cond *conds;
273 Line *expansion;
H. Peter Anvin274cda82016-05-10 02:56:29 -0700274 const char *fname;
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800275 int lineno, lineinc;
276 MMacro *mstk; /* stack of active macros/reps */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000277};
278
279/*
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000280 * Include search path. This is simply a list of strings which get
281 * prepended, in turn, to the name of an include file, in an
282 * attempt to find the file if it's not in the current directory.
283 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000284struct IncPath {
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800285 IncPath *next;
286 char *path;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000287};
288
289/*
H. Peter Anvin169ac7c2016-09-25 17:08:05 -0700290 * File real name hash, so we don't have to re-search the include
291 * path for every pass (and potentially more than that if a file
292 * is used more than once.)
293 */
294struct hash_table FileHash;
295
296/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000297 * Conditional assembly: we maintain a separate stack of these for
298 * each level of file inclusion. (The only reason we keep the
299 * stacks separate is to ensure that a stray `%endif' in a file
300 * included from within the true branch of a `%if' won't terminate
301 * it and cause confusion: instead, rightly, it'll cause an error.)
302 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800303struct Cond {
304 Cond *next;
305 int state;
306};
H. Peter Anvine2c80182005-01-15 22:15:51 +0000307enum {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000308 /*
309 * These states are for use just after %if or %elif: IF_TRUE
310 * means the condition has evaluated to truth so we are
311 * currently emitting, whereas IF_FALSE means we are not
312 * currently emitting but will start doing so if a %else comes
313 * up. In these states, all directives are admissible: %elif,
314 * %else and %endif. (And of course %if.)
315 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800316 COND_IF_TRUE, COND_IF_FALSE,
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000317 /*
318 * These states come up after a %else: ELSE_TRUE means we're
319 * emitting, and ELSE_FALSE means we're not. In ELSE_* states,
320 * any %elif or %else will cause an error.
321 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800322 COND_ELSE_TRUE, COND_ELSE_FALSE,
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000323 /*
Victor van den Elzen3b404c02008-09-18 13:51:36 +0200324 * These states mean that we're not emitting now, and also that
325 * nothing until %endif will be emitted at all. COND_DONE is
326 * used when we've had our moment of emission
327 * and have now started seeing %elifs. COND_NEVER is used when
328 * the condition construct in question is contained within a
329 * non-emitting branch of a larger condition construct,
330 * or if there is an error.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000331 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800332 COND_DONE, COND_NEVER
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000333};
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800334#define emitting(x) ( (x) == COND_IF_TRUE || (x) == COND_ELSE_TRUE )
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000335
H. Peter Anvin70653092007-10-19 14:42:29 -0700336/*
Ed Beroset3ab3f412002-06-11 03:31:49 +0000337 * These defines are used as the possible return values for do_directive
338 */
339#define NO_DIRECTIVE_FOUND 0
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300340#define DIRECTIVE_FOUND 1
Ed Beroset3ab3f412002-06-11 03:31:49 +0000341
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000342/*
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800343 * This define sets the upper limit for smacro and recursive mmacro
344 * expansions
Keith Kanios852f1ee2009-07-12 00:19:55 -0500345 */
346#define DEADMAN_LIMIT (1 << 20)
347
Cyrill Gorcunove091d6e2010-08-09 13:58:22 +0400348/* max reps */
349#define REP_LIMIT ((INT64_C(1) << 62))
350
Keith Kanios852f1ee2009-07-12 00:19:55 -0500351/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000352 * Condition codes. Note that we use c_ prefix not C_ because C_ is
353 * used in nasm.h for the "real" condition codes. At _this_ level,
354 * we treat CXZ and ECXZ as condition codes, albeit non-invertible
355 * ones, so we need a different enum...
356 */
H. Peter Anvin476d2862007-10-02 22:04:15 -0700357static const char * const conditions[] = {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000358 "a", "ae", "b", "be", "c", "cxz", "e", "ecxz", "g", "ge", "l", "le",
359 "na", "nae", "nb", "nbe", "nc", "ne", "ng", "nge", "nl", "nle", "no",
H. Peter Anvince9be342007-09-12 00:22:29 +0000360 "np", "ns", "nz", "o", "p", "pe", "po", "rcxz", "s", "z"
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000361};
H. Peter Anvin476d2862007-10-02 22:04:15 -0700362enum pp_conds {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000363 c_A, c_AE, c_B, c_BE, c_C, c_CXZ, c_E, c_ECXZ, c_G, c_GE, c_L, c_LE,
364 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 -0700365 c_NP, c_NS, c_NZ, c_O, c_P, c_PE, c_PO, c_RCXZ, c_S, c_Z,
366 c_none = -1
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000367};
H. Peter Anvin476d2862007-10-02 22:04:15 -0700368static const enum pp_conds inverse_ccs[] = {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000369 c_NA, c_NAE, c_NB, c_NBE, c_NC, -1, c_NE, -1, c_NG, c_NGE, c_NL, c_NLE,
370 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 +0000371 c_Z, c_NO, c_NP, c_PO, c_PE, -1, c_NS, c_NZ
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000372};
373
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800374/*
375 * Directive names.
376 */
377/* If this is a an IF, ELIF, ELSE or ENDIF keyword */
378static int is_condition(enum preproc_token arg)
379{
380 return PP_IS_COND(arg) || (arg == PP_ELSE) || (arg == PP_ENDIF);
381}
382
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000383/* For TASM compatibility we need to be able to recognise TASM compatible
384 * conditional compilation directives. Using the NASM pre-processor does
385 * not work, so we look for them specifically from the following list and
386 * then jam in the equivalent NASM directive into the input stream.
387 */
388
H. Peter Anvine2c80182005-01-15 22:15:51 +0000389enum {
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000390 TM_ARG, TM_ELIF, TM_ELSE, TM_ENDIF, TM_IF, TM_IFDEF, TM_IFDIFI,
391 TM_IFNDEF, TM_INCLUDE, TM_LOCAL
392};
393
H. Peter Anvin476d2862007-10-02 22:04:15 -0700394static const char * const tasm_directives[] = {
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000395 "arg", "elif", "else", "endif", "if", "ifdef", "ifdifi",
396 "ifndef", "include", "local"
397};
398
399static int StackSize = 4;
H. Peter Anvin6c8b2be2016-05-24 23:46:50 -0700400static const char *StackPointer = "ebp";
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000401static int ArgOffset = 8;
H. Peter Anvin8781cb02007-11-08 20:01:11 -0800402static int LocalOffset = 0;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000403
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000404static Context *cstk;
405static Include *istk;
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800406static IncPath *ipath = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000407
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300408static int pass; /* HACK: pass 0 = generate dependencies only */
H. Peter Anvin9924d1e2016-10-04 00:59:39 -0700409static StrList **dephead;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000410
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300411static uint64_t unique; /* unique identifier numbers */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000412
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800413static Line *predef = NULL;
H. Peter Anvind2456592008-06-19 15:04:18 -0700414static bool do_predef;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000415
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000416/*
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800417 * The current set of multi-line macros we have defined.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000418 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800419static struct hash_table mmacros;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000420
421/*
422 * The current set of single-line macros we have defined.
423 */
H. Peter Anvin166c2472008-05-28 12:28:58 -0700424static struct hash_table smacros;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000425
426/*
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800427 * The multi-line macro we are currently defining, or the %rep
428 * block we are currently reading, if any.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000429 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800430static MMacro *defining;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000431
Charles Crayned4200be2008-07-12 16:42:33 -0700432static uint64_t nested_mac_count;
433static uint64_t nested_rep_count;
434
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000435/*
436 * The number of macro parameters to allocate space for at a time.
437 */
438#define PARAM_DELTA 16
439
440/*
H. Peter Anvinf7606612016-07-13 14:23:48 -0700441 * The standard macro set: defined in macros.c in a set of arrays.
442 * This gives our position in any macro set, while we are processing it.
443 * The stdmacset is an array of such macro sets.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000444 */
H. Peter Anvina70547f2008-07-19 21:44:26 -0700445static macros_t *stdmacpos;
H. Peter Anvinf7606612016-07-13 14:23:48 -0700446static macros_t **stdmacnext;
447static macros_t *stdmacros[8];
Cyrill Gorcunov15ce78f2017-01-06 20:21:28 +0300448static macros_t *extrastdmac;
H. Peter Anvin76690a12002-04-30 20:52:49 +0000449
450/*
H. Peter Anvin734b1882002-04-30 21:01:08 +0000451 * Tokens are allocated in blocks to improve speed
452 */
453#define TOKEN_BLOCKSIZE 4096
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800454static Token *freeTokens = NULL;
H. Peter Anvince616072002-04-30 21:02:23 +0000455struct Blocks {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000456 Blocks *next;
457 void *chunk;
H. Peter Anvince616072002-04-30 21:02:23 +0000458};
459
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800460static Blocks blocks = { NULL, NULL };
H. Peter Anvin734b1882002-04-30 21:01:08 +0000461
462/*
H. Peter Anvin76690a12002-04-30 20:52:49 +0000463 * Forward declarations.
464 */
H. Peter Anvinf7606612016-07-13 14:23:48 -0700465static void pp_add_stdmac(macros_t *macros);
H. Peter Anvin734b1882002-04-30 21:01:08 +0000466static Token *expand_mmac_params(Token * tline);
467static Token *expand_smacro(Token * tline);
468static Token *expand_id(Token * tline);
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +0400469static Context *get_ctx(const char *name, const char **namep);
Keith Kaniosa5fc6462007-10-13 07:09:22 -0700470static void make_tok_num(Token * tok, int64_t val);
H. Peter Anvin130736c2016-02-17 20:27:41 -0800471static void pp_verror(int severity, const char *fmt, va_list ap);
472static vefunc real_verror;
H. Peter Anvince616072002-04-30 21:02:23 +0000473static void *new_Block(size_t size);
474static void delete_Blocks(void);
H. Peter Anvinc751e862008-06-09 10:18:45 -0700475static Token *new_Token(Token * next, enum pp_token_type type,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300476 const char *text, int txtlen);
H. Peter Anvin734b1882002-04-30 21:01:08 +0000477static Token *delete_Token(Token * t);
H. Peter Anvineba20a72002-04-30 20:53:55 +0000478
479/*
480 * Macros for safe checking of token pointers, avoid *(NULL)
481 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300482#define tok_type_(x,t) ((x) && (x)->type == (t))
483#define skip_white_(x) if (tok_type_((x), TOK_WHITESPACE)) (x)=(x)->next
484#define tok_is_(x,v) (tok_type_((x), TOK_OTHER) && !strcmp((x)->text,(v)))
485#define tok_isnt_(x,v) ((x) && ((x)->type!=TOK_OTHER || strcmp((x)->text,(v))))
H. Peter Anvin76690a12002-04-30 20:52:49 +0000486
Cyrill Gorcunov194ba892011-06-30 01:16:35 +0400487/*
H. Peter Anvin077fb932010-07-20 14:56:30 -0700488 * nasm_unquote with error if the string contains NUL characters.
489 * If the string contains NUL characters, issue an error and return
490 * the C len, i.e. truncate at the NUL.
491 */
492static size_t nasm_unquote_cstr(char *qstr, enum preproc_token directive)
493{
494 size_t len = nasm_unquote(qstr, NULL);
495 size_t clen = strlen(qstr);
496
497 if (len != clen)
H. Peter Anvin130736c2016-02-17 20:27:41 -0800498 nasm_error(ERR_NONFATAL, "NUL character in `%s' directive",
H. Peter Anvin077fb932010-07-20 14:56:30 -0700499 pp_directives[directive]);
500
501 return clen;
502}
503
504/*
H. Peter Anvinb40992c2010-09-15 08:57:21 -0700505 * In-place reverse a list of tokens.
506 */
507static Token *reverse_tokens(Token *t)
508{
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800509 Token *prev = NULL;
510 Token *next;
H. Peter Anvinb40992c2010-09-15 08:57:21 -0700511
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800512 while (t) {
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +0400513 next = t->next;
514 t->next = prev;
515 prev = t;
516 t = next;
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800517 }
H. Peter Anvinb40992c2010-09-15 08:57:21 -0700518
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800519 return prev;
H. Peter Anvinb40992c2010-09-15 08:57:21 -0700520}
521
522/*
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300523 * Handle TASM specific directives, which do not contain a % in
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000524 * front of them. We do it here because I could not find any other
525 * place to do it for the moment, and it is a hack (ideally it would
526 * be nice to be able to use the NASM pre-processor to do it).
527 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000528static char *check_tasm_directive(char *line)
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000529{
Keith Kaniosb7a89542007-04-12 02:40:54 +0000530 int32_t i, j, k, m, len;
Cyrill Gorcunovf66ac7d2009-10-12 20:41:13 +0400531 char *p, *q, *oldline, oldchar;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000532
Cyrill Gorcunovf66ac7d2009-10-12 20:41:13 +0400533 p = nasm_skip_spaces(line);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000534
535 /* Binary search for the directive name */
536 i = -1;
Cyrill Gorcunova7319242010-06-03 22:04:36 +0400537 j = ARRAY_SIZE(tasm_directives);
Cyrill Gorcunovf66ac7d2009-10-12 20:41:13 +0400538 q = nasm_skip_word(p);
539 len = q - p;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000540 if (len) {
541 oldchar = p[len];
542 p[len] = 0;
543 while (j - i > 1) {
544 k = (j + i) / 2;
545 m = nasm_stricmp(p, tasm_directives[k]);
546 if (m == 0) {
547 /* We have found a directive, so jam a % in front of it
548 * so that NASM will then recognise it as one if it's own.
549 */
550 p[len] = oldchar;
551 len = strlen(p);
552 oldline = line;
553 line = nasm_malloc(len + 2);
554 line[0] = '%';
555 if (k == TM_IFDIFI) {
H. Peter Anvin18f48792009-06-27 15:56:27 -0700556 /*
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300557 * NASM does not recognise IFDIFI, so we convert
558 * it to %if 0. This is not used in NASM
559 * compatible code, but does need to parse for the
560 * TASM macro package.
H. Peter Anvine2c80182005-01-15 22:15:51 +0000561 */
H. Peter Anvin18f48792009-06-27 15:56:27 -0700562 strcpy(line + 1, "if 0");
H. Peter Anvine2c80182005-01-15 22:15:51 +0000563 } else {
564 memcpy(line + 1, p, len + 1);
565 }
566 nasm_free(oldline);
567 return line;
568 } else if (m < 0) {
569 j = k;
570 } else
571 i = k;
572 }
573 p[len] = oldchar;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000574 }
575 return line;
576}
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000577
H. Peter Anvin76690a12002-04-30 20:52:49 +0000578/*
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000579 * The pre-preprocessing stage... This function translates line
580 * number indications as they emerge from GNU cpp (`# lineno "file"
581 * flags') into NASM preprocessor line number indications (`%line
582 * lineno file').
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000583 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000584static char *prepreproc(char *line)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000585{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000586 int lineno, fnlen;
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000587 char *fname, *oldline;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000588
H. Peter Anvine2c80182005-01-15 22:15:51 +0000589 if (line[0] == '#' && line[1] == ' ') {
590 oldline = line;
591 fname = oldline + 2;
592 lineno = atoi(fname);
593 fname += strspn(fname, "0123456789 ");
594 if (*fname == '"')
595 fname++;
596 fnlen = strcspn(fname, "\"");
597 line = nasm_malloc(20 + fnlen);
598 snprintf(line, 20 + fnlen, "%%line %d %.*s", lineno, fnlen, fname);
599 nasm_free(oldline);
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000600 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000601 if (tasm_compatible_mode)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000602 return check_tasm_directive(line);
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000603 return line;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000604}
605
606/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000607 * Free a linked list of tokens.
608 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000609static void free_tlist(Token * list)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000610{
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +0400611 while (list)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000612 list = delete_Token(list);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000613}
614
615/*
616 * Free a linked list of lines.
617 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000618static void free_llist(Line * list)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000619{
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +0400620 Line *l, *tmp;
621 list_for_each_safe(l, tmp, list) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000622 free_tlist(l->first);
623 nasm_free(l);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000624 }
625}
626
627/*
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800628 * Free an MMacro
H. Peter Anvineba20a72002-04-30 20:53:55 +0000629 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800630static void free_mmacro(MMacro * m)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000631{
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800632 nasm_free(m->name);
633 free_tlist(m->dlist);
634 nasm_free(m->defaults);
635 free_llist(m->expansion);
636 nasm_free(m);
H. Peter Anvineba20a72002-04-30 20:53:55 +0000637}
638
639/*
H. Peter Anvin97a23472007-09-16 17:57:25 -0700640 * Free all currently defined macros, and free the hash tables
641 */
H. Peter Anvin072771e2008-05-22 13:17:51 -0700642static void free_smacro_table(struct hash_table *smt)
H. Peter Anvin97a23472007-09-16 17:57:25 -0700643{
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +0400644 SMacro *s, *tmp;
H. Peter Anvin072771e2008-05-22 13:17:51 -0700645 const char *key;
646 struct hash_tbl_node *it = NULL;
H. Peter Anvin97a23472007-09-16 17:57:25 -0700647
H. Peter Anvin072771e2008-05-22 13:17:51 -0700648 while ((s = hash_iterate(smt, &it, &key)) != NULL) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300649 nasm_free((void *)key);
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +0400650 list_for_each_safe(s, tmp, s) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300651 nasm_free(s->name);
652 free_tlist(s->expansion);
653 nasm_free(s);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300654 }
H. Peter Anvin97a23472007-09-16 17:57:25 -0700655 }
H. Peter Anvin072771e2008-05-22 13:17:51 -0700656 hash_free(smt);
657}
658
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800659static void free_mmacro_table(struct hash_table *mmt)
H. Peter Anvin072771e2008-05-22 13:17:51 -0700660{
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800661 MMacro *m, *tmp;
H. Peter Anvin072771e2008-05-22 13:17:51 -0700662 const char *key;
663 struct hash_tbl_node *it = NULL;
H. Peter Anvin97a23472007-09-16 17:57:25 -0700664
665 it = NULL;
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800666 while ((m = hash_iterate(mmt, &it, &key)) != NULL) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300667 nasm_free((void *)key);
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800668 list_for_each_safe(m ,tmp, m)
669 free_mmacro(m);
H. Peter Anvin97a23472007-09-16 17:57:25 -0700670 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800671 hash_free(mmt);
H. Peter Anvin072771e2008-05-22 13:17:51 -0700672}
673
674static void free_macros(void)
675{
H. Peter Anvin166c2472008-05-28 12:28:58 -0700676 free_smacro_table(&smacros);
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800677 free_mmacro_table(&mmacros);
H. Peter Anvin97a23472007-09-16 17:57:25 -0700678}
679
680/*
681 * Initialize the hash tables
682 */
683static void init_macros(void)
684{
H. Peter Anvin166c2472008-05-28 12:28:58 -0700685 hash_init(&smacros, HASH_LARGE);
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800686 hash_init(&mmacros, HASH_LARGE);
H. Peter Anvin97a23472007-09-16 17:57:25 -0700687}
688
689/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000690 * Pop the context stack.
691 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000692static void ctx_pop(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000693{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000694 Context *c = cstk;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000695
696 cstk = cstk->next;
H. Peter Anvin166c2472008-05-28 12:28:58 -0700697 free_smacro_table(&c->localmac);
H. Peter Anvin734b1882002-04-30 21:01:08 +0000698 nasm_free(c->name);
699 nasm_free(c);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000700}
701
H. Peter Anvin072771e2008-05-22 13:17:51 -0700702/*
703 * Search for a key in the hash index; adding it if necessary
704 * (in which case we initialize the data pointer to NULL.)
705 */
706static void **
707hash_findi_add(struct hash_table *hash, const char *str)
708{
709 struct hash_insert hi;
710 void **r;
711 char *strx;
712
713 r = hash_findi(hash, str, &hi);
714 if (r)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300715 return r;
H. Peter Anvin072771e2008-05-22 13:17:51 -0700716
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300717 strx = nasm_strdup(str); /* Use a more efficient allocator here? */
H. Peter Anvin072771e2008-05-22 13:17:51 -0700718 return hash_add(&hi, strx, NULL);
719}
720
721/*
722 * Like hash_findi, but returns the data element rather than a pointer
723 * to it. Used only when not adding a new element, hence no third
724 * argument.
725 */
726static void *
727hash_findix(struct hash_table *hash, const char *str)
728{
729 void **p;
730
731 p = hash_findi(hash, str, NULL);
732 return p ? *p : NULL;
733}
734
Cyrill Gorcunov15bdc512010-07-13 11:27:41 +0400735/*
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800736 * read line from standart macros set,
Cyrill Gorcunov15bdc512010-07-13 11:27:41 +0400737 * if there no more left -- return NULL
738 */
739static char *line_from_stdmac(void)
740{
741 unsigned char c;
742 const unsigned char *p = stdmacpos;
743 char *line, *q;
744 size_t len = 0;
745
746 if (!stdmacpos)
747 return NULL;
748
749 while ((c = *p++)) {
750 if (c >= 0x80)
751 len += pp_directives_len[c - 0x80] + 1;
752 else
753 len++;
754 }
755
756 line = nasm_malloc(len + 1);
757 q = line;
758 while ((c = *stdmacpos++)) {
759 if (c >= 0x80) {
760 memcpy(q, pp_directives[c - 0x80], pp_directives_len[c - 0x80]);
761 q += pp_directives_len[c - 0x80];
762 *q++ = ' ';
763 } else {
764 *q++ = c;
765 }
766 }
767 stdmacpos = p;
768 *q = '\0';
769
770 if (!*stdmacpos) {
H. Peter Anvinf7606612016-07-13 14:23:48 -0700771 /* This was the last of this particular macro set */
Cyrill Gorcunov15bdc512010-07-13 11:27:41 +0400772 stdmacpos = NULL;
H. Peter Anvinf7606612016-07-13 14:23:48 -0700773 if (*stdmacnext) {
774 stdmacpos = *stdmacnext++;
Cyrill Gorcunov15bdc512010-07-13 11:27:41 +0400775 } else if (do_predef) {
776 Line *pd, *l;
777 Token *head, **tail, *t;
778
779 /*
780 * Nasty hack: here we push the contents of
781 * `predef' on to the top-level expansion stack,
782 * since this is the most convenient way to
783 * implement the pre-include and pre-define
784 * features.
785 */
786 list_for_each(pd, predef) {
787 head = NULL;
788 tail = &head;
789 list_for_each(t, pd->first) {
790 *tail = new_Token(NULL, t->type, t->text, 0);
791 tail = &(*tail)->next;
792 }
793
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800794 l = nasm_malloc(sizeof(Line));
795 l->next = istk->expansion;
796 l->first = head;
797 l->finishes = NULL;
798
799 istk->expansion = l;
Cyrill Gorcunov15bdc512010-07-13 11:27:41 +0400800 }
801 do_predef = false;
802 }
803 }
804
805 return line;
806}
807
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000808static char *read_line(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000809{
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +0400810 unsigned int size, c, next;
811 const unsigned int delta = 512;
812 const unsigned int pad = 8;
813 unsigned int nr_cont = 0;
814 bool cont = false;
815 char *buffer, *p;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000816
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +0400817 /* Standart macros set (predefined) goes first */
Cyrill Gorcunov15bdc512010-07-13 11:27:41 +0400818 p = line_from_stdmac();
819 if (p)
820 return p;
H. Peter Anvin72edbb82008-06-19 16:00:04 -0700821
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +0400822 size = delta;
823 p = buffer = nasm_malloc(size);
824
825 for (;;) {
826 c = fgetc(istk->fp);
827 if ((int)(c) == EOF) {
828 p[0] = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000829 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000830 }
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +0400831
832 switch (c) {
833 case '\r':
834 next = fgetc(istk->fp);
835 if (next != '\n')
836 ungetc(next, istk->fp);
837 if (cont) {
838 cont = false;
839 continue;
840 }
841 break;
842
843 case '\n':
844 if (cont) {
845 cont = false;
846 continue;
847 }
848 break;
849
850 case '\\':
851 next = fgetc(istk->fp);
852 ungetc(next, istk->fp);
Cyrill Gorcunov490f85e2012-12-27 20:02:17 +0400853 if (next == '\r' || next == '\n') {
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +0400854 cont = true;
855 nr_cont++;
856 continue;
857 }
858 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000859 }
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +0400860
861 if (c == '\r' || c == '\n') {
862 *p++ = 0;
863 break;
864 }
865
866 if (p >= (buffer + size - pad)) {
867 buffer = nasm_realloc(buffer, size + delta);
868 p = buffer + size - pad;
869 size += delta;
870 }
871
872 *p++ = (unsigned char)c;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000873 }
874
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +0400875 if (p == buffer) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000876 nasm_free(buffer);
877 return NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000878 }
879
Cyrill Gorcunova5aea572010-11-11 10:14:45 +0300880 src_set_linnum(src_get_linnum() + istk->lineinc +
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +0400881 (nr_cont * istk->lineinc));
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000882
883 /*
884 * Handle spurious ^Z, which may be inserted into source files
885 * by some file transfer utilities.
886 */
887 buffer[strcspn(buffer, "\032")] = '\0';
888
H. Peter Anvin8ac25aa2016-02-18 01:16:18 -0800889 lfmt->line(LIST_READ, buffer);
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000890
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000891 return buffer;
892}
893
894/*
Keith Kaniosb7a89542007-04-12 02:40:54 +0000895 * Tokenize a line of text. This is a very simple process since we
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000896 * don't need to parse the value out of e.g. numeric tokens: we
897 * simply split one string into many.
898 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000899static Token *tokenize(char *line)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000900{
H. Peter Anvinca544db2008-10-19 19:30:11 -0700901 char c, *p = line;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +0000902 enum pp_token_type type;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000903 Token *list = NULL;
904 Token *t, **tail = &list;
905
H. Peter Anvine2c80182005-01-15 22:15:51 +0000906 while (*line) {
907 p = line;
908 if (*p == '%') {
909 p++;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300910 if (*p == '+' && !nasm_isdigit(p[1])) {
911 p++;
912 type = TOK_PASTE;
913 } else if (nasm_isdigit(*p) ||
914 ((*p == '-' || *p == '+') && nasm_isdigit(p[1]))) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000915 do {
916 p++;
917 }
H. Peter Anvinbda7a6e2008-06-21 10:23:17 -0700918 while (nasm_isdigit(*p));
H. Peter Anvine2c80182005-01-15 22:15:51 +0000919 type = TOK_PREPROC_ID;
920 } else if (*p == '{') {
921 p++;
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800922 while (*p) {
923 if (*p == '}')
924 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000925 p[-1] = *p;
926 p++;
927 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800928 if (*p != '}')
H. Peter Anvin130736c2016-02-17 20:27:41 -0800929 nasm_error(ERR_WARNING | ERR_PASS1,
930 "unterminated %%{ construct");
H. Peter Anvine2c80182005-01-15 22:15:51 +0000931 p[-1] = '\0';
932 if (*p)
933 p++;
934 type = TOK_PREPROC_ID;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300935 } else if (*p == '[') {
936 int lvl = 1;
937 line += 2; /* Skip the leading %[ */
938 p++;
939 while (lvl && (c = *p++)) {
940 switch (c) {
941 case ']':
942 lvl--;
943 break;
944 case '%':
945 if (*p == '[')
946 lvl++;
947 break;
948 case '\'':
949 case '\"':
950 case '`':
Cyrill Gorcunovc6360a72010-07-13 13:32:19 +0400951 p = nasm_skip_string(p - 1) + 1;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300952 break;
953 default:
954 break;
955 }
956 }
957 p--;
958 if (*p)
959 *p++ = '\0';
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800960 if (lvl)
H. Peter Anvin130736c2016-02-17 20:27:41 -0800961 nasm_error(ERR_NONFATAL|ERR_PASS1,
962 "unterminated %%[ construct");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300963 type = TOK_INDIRECT;
964 } else if (*p == '?') {
965 type = TOK_PREPROC_Q; /* %? */
966 p++;
967 if (*p == '?') {
968 type = TOK_PREPROC_QQ; /* %?? */
969 p++;
970 }
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +0400971 } else if (*p == '!') {
972 type = TOK_PREPROC_ID;
973 p++;
974 if (isidchar(*p)) {
975 do {
976 p++;
977 }
978 while (isidchar(*p));
979 } else if (*p == '\'' || *p == '\"' || *p == '`') {
980 p = nasm_skip_string(p);
981 if (*p)
982 p++;
983 else
H. Peter Anvin130736c2016-02-17 20:27:41 -0800984 nasm_error(ERR_NONFATAL|ERR_PASS1,
985 "unterminated %%! string");
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +0400986 } else {
987 /* %! without string or identifier */
988 type = TOK_OTHER; /* Legacy behavior... */
989 }
H. Peter Anvine2c80182005-01-15 22:15:51 +0000990 } else if (isidchar(*p) ||
991 ((*p == '!' || *p == '%' || *p == '$') &&
992 isidchar(p[1]))) {
993 do {
994 p++;
995 }
996 while (isidchar(*p));
997 type = TOK_PREPROC_ID;
998 } else {
999 type = TOK_OTHER;
1000 if (*p == '%')
1001 p++;
1002 }
1003 } else if (isidstart(*p) || (*p == '$' && isidstart(p[1]))) {
1004 type = TOK_ID;
1005 p++;
1006 while (*p && isidchar(*p))
1007 p++;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07001008 } else if (*p == '\'' || *p == '"' || *p == '`') {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001009 /*
1010 * A string token.
1011 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001012 type = TOK_STRING;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001013 p = nasm_skip_string(p);
Nickolay Yurchenkof3b3ce22003-09-21 20:38:43 +00001014
H. Peter Anvine2c80182005-01-15 22:15:51 +00001015 if (*p) {
1016 p++;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001017 } else {
H. Peter Anvin130736c2016-02-17 20:27:41 -08001018 nasm_error(ERR_WARNING|ERR_PASS1, "unterminated string");
H. Peter Anvine2c80182005-01-15 22:15:51 +00001019 /* Handling unterminated strings by UNV */
1020 /* type = -1; */
1021 }
Victor van den Elzenfb5f2512009-04-17 16:17:59 +02001022 } else if (p[0] == '$' && p[1] == '$') {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001023 type = TOK_OTHER; /* TOKEN_BASE */
Victor van den Elzenfb5f2512009-04-17 16:17:59 +02001024 p += 2;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001025 } else if (isnumstart(*p)) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001026 bool is_hex = false;
1027 bool is_float = false;
1028 bool has_e = false;
1029 char c, *r;
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001030
H. Peter Anvine2c80182005-01-15 22:15:51 +00001031 /*
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001032 * A numeric token.
H. Peter Anvine2c80182005-01-15 22:15:51 +00001033 */
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001034
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001035 if (*p == '$') {
1036 p++;
1037 is_hex = true;
1038 }
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001039
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001040 for (;;) {
1041 c = *p++;
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001042
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001043 if (!is_hex && (c == 'e' || c == 'E')) {
1044 has_e = true;
1045 if (*p == '+' || *p == '-') {
1046 /*
1047 * e can only be followed by +/- if it is either a
1048 * prefixed hex number or a floating-point number
1049 */
1050 p++;
1051 is_float = true;
1052 }
1053 } else if (c == 'H' || c == 'h' || c == 'X' || c == 'x') {
1054 is_hex = true;
1055 } else if (c == 'P' || c == 'p') {
1056 is_float = true;
1057 if (*p == '+' || *p == '-')
1058 p++;
Martin Lindheb150e382016-11-16 15:36:53 +01001059 } else if (isnumchar(c))
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001060 ; /* just advance */
1061 else if (c == '.') {
1062 /*
1063 * we need to deal with consequences of the legacy
1064 * parser, like "1.nolist" being two tokens
1065 * (TOK_NUMBER, TOK_ID) here; at least give it
1066 * a shot for now. In the future, we probably need
1067 * a flex-based scanner with proper pattern matching
1068 * to do it as well as it can be done. Nothing in
1069 * the world is going to help the person who wants
1070 * 0x123.p16 interpreted as two tokens, though.
1071 */
1072 r = p;
1073 while (*r == '_')
1074 r++;
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001075
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001076 if (nasm_isdigit(*r) || (is_hex && nasm_isxdigit(*r)) ||
1077 (!is_hex && (*r == 'e' || *r == 'E')) ||
1078 (*r == 'p' || *r == 'P')) {
1079 p = r;
1080 is_float = true;
1081 } else
1082 break; /* Terminate the token */
1083 } else
1084 break;
1085 }
1086 p--; /* Point to first character beyond number */
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001087
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001088 if (p == line+1 && *line == '$') {
1089 type = TOK_OTHER; /* TOKEN_HERE */
1090 } else {
1091 if (has_e && !is_hex) {
1092 /* 1e13 is floating-point, but 1e13h is not */
1093 is_float = true;
1094 }
H. Peter Anvind784a082009-04-20 14:01:18 -07001095
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001096 type = is_float ? TOK_FLOAT : TOK_NUMBER;
1097 }
H. Peter Anvinbda7a6e2008-06-21 10:23:17 -07001098 } else if (nasm_isspace(*p)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001099 type = TOK_WHITESPACE;
Cyrill Gorcunovf66ac7d2009-10-12 20:41:13 +04001100 p = nasm_skip_spaces(p);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001101 /*
1102 * Whitespace just before end-of-line is discarded by
1103 * pretending it's a comment; whitespace just before a
1104 * comment gets lumped into the comment.
1105 */
1106 if (!*p || *p == ';') {
1107 type = TOK_COMMENT;
1108 while (*p)
1109 p++;
1110 }
1111 } else if (*p == ';') {
1112 type = TOK_COMMENT;
1113 while (*p)
1114 p++;
1115 } else {
1116 /*
1117 * Anything else is an operator of some kind. We check
1118 * for all the double-character operators (>>, <<, //,
1119 * %%, <=, >=, ==, !=, <>, &&, ||, ^^), but anything
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001120 * else is a single-character operator.
H. Peter Anvine2c80182005-01-15 22:15:51 +00001121 */
1122 type = TOK_OTHER;
1123 if ((p[0] == '>' && p[1] == '>') ||
1124 (p[0] == '<' && p[1] == '<') ||
1125 (p[0] == '/' && p[1] == '/') ||
1126 (p[0] == '<' && p[1] == '=') ||
1127 (p[0] == '>' && p[1] == '=') ||
1128 (p[0] == '=' && p[1] == '=') ||
1129 (p[0] == '!' && p[1] == '=') ||
1130 (p[0] == '<' && p[1] == '>') ||
1131 (p[0] == '&' && p[1] == '&') ||
1132 (p[0] == '|' && p[1] == '|') ||
1133 (p[0] == '^' && p[1] == '^')) {
1134 p++;
1135 }
1136 p++;
1137 }
Nickolay Yurchenkof3b3ce22003-09-21 20:38:43 +00001138
H. Peter Anvine2c80182005-01-15 22:15:51 +00001139 /* Handling unterminated string by UNV */
1140 /*if (type == -1)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001141 {
1142 *tail = t = new_Token(NULL, TOK_STRING, line, p-line+1);
1143 t->text[p-line] = *line;
1144 tail = &t->next;
1145 }
1146 else */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001147 if (type != TOK_COMMENT) {
1148 *tail = t = new_Token(NULL, type, line, p - line);
1149 tail = &t->next;
1150 }
1151 line = p;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001152 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001153 return list;
1154}
1155
H. Peter Anvince616072002-04-30 21:02:23 +00001156/*
1157 * this function allocates a new managed block of memory and
H. Peter Anvin70653092007-10-19 14:42:29 -07001158 * returns a pointer to the block. The managed blocks are
H. Peter Anvince616072002-04-30 21:02:23 +00001159 * deleted only all at once by the delete_Blocks function.
1160 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001161static void *new_Block(size_t size)
H. Peter Anvince616072002-04-30 21:02:23 +00001162{
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001163 Blocks *b = &blocks;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001164
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001165 /* first, get to the end of the linked list */
1166 while (b->next)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001167 b = b->next;
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001168 /* now allocate the requested chunk */
1169 b->chunk = nasm_malloc(size);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001170
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001171 /* now allocate a new block for the next request */
Cyrill Gorcunovc31767c2014-06-28 02:22:17 +04001172 b->next = nasm_zalloc(sizeof(Blocks));
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001173 return b->chunk;
H. Peter Anvince616072002-04-30 21:02:23 +00001174}
1175
1176/*
1177 * this function deletes all managed blocks of memory
1178 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001179static void delete_Blocks(void)
H. Peter Anvince616072002-04-30 21:02:23 +00001180{
H. Peter Anvine2c80182005-01-15 22:15:51 +00001181 Blocks *a, *b = &blocks;
H. Peter Anvince616072002-04-30 21:02:23 +00001182
H. Peter Anvin70653092007-10-19 14:42:29 -07001183 /*
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001184 * keep in mind that the first block, pointed to by blocks
H. Peter Anvin70653092007-10-19 14:42:29 -07001185 * is a static and not dynamically allocated, so we don't
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001186 * free it.
1187 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001188 while (b) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001189 if (b->chunk)
1190 nasm_free(b->chunk);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001191 a = b;
1192 b = b->next;
1193 if (a != &blocks)
1194 nasm_free(a);
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001195 }
Cyrill Gorcunovdae24d72014-06-28 10:17:39 +04001196 memset(&blocks, 0, sizeof(blocks));
H. Peter Anvine2c80182005-01-15 22:15:51 +00001197}
H. Peter Anvin734b1882002-04-30 21:01:08 +00001198
1199/*
H. Peter Anvin70653092007-10-19 14:42:29 -07001200 * this function creates a new Token and passes a pointer to it
H. Peter Anvin734b1882002-04-30 21:01:08 +00001201 * back to the caller. It sets the type and text elements, and
H. Peter Anvinf26e0972008-07-01 21:26:27 -07001202 * also the a.mac and next elements to NULL.
H. Peter Anvin734b1882002-04-30 21:01:08 +00001203 */
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07001204static Token *new_Token(Token * next, enum pp_token_type type,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001205 const char *text, int txtlen)
H. Peter Anvin734b1882002-04-30 21:01:08 +00001206{
1207 Token *t;
1208 int i;
1209
H. Peter Anvin89cee572009-07-15 09:16:54 -04001210 if (!freeTokens) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001211 freeTokens = (Token *) new_Block(TOKEN_BLOCKSIZE * sizeof(Token));
1212 for (i = 0; i < TOKEN_BLOCKSIZE - 1; i++)
1213 freeTokens[i].next = &freeTokens[i + 1];
1214 freeTokens[i].next = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001215 }
1216 t = freeTokens;
1217 freeTokens = t->next;
1218 t->next = next;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07001219 t->a.mac = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001220 t->type = type;
H. Peter Anvin89cee572009-07-15 09:16:54 -04001221 if (type == TOK_WHITESPACE || !text) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001222 t->text = NULL;
1223 } else {
1224 if (txtlen == 0)
1225 txtlen = strlen(text);
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07001226 t->text = nasm_malloc(txtlen+1);
1227 memcpy(t->text, text, txtlen);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001228 t->text[txtlen] = '\0';
H. Peter Anvin734b1882002-04-30 21:01:08 +00001229 }
1230 return t;
1231}
1232
H. Peter Anvine2c80182005-01-15 22:15:51 +00001233static Token *delete_Token(Token * t)
H. Peter Anvin734b1882002-04-30 21:01:08 +00001234{
1235 Token *next = t->next;
1236 nasm_free(t->text);
H. Peter Anvin788e6c12002-04-30 21:02:01 +00001237 t->next = freeTokens;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001238 freeTokens = t;
1239 return next;
1240}
1241
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001242/*
1243 * Convert a line of tokens back into text.
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001244 * If expand_locals is not zero, identifiers of the form "%$*xxx"
1245 * will be transformed into ..@ctxnum.xxx
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001246 */
H. Peter Anvin9e200162008-06-04 17:23:14 -07001247static char *detoken(Token * tlist, bool expand_locals)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001248{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001249 Token *t;
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001250 char *line, *p;
H. Peter Anvinb4daadc2008-01-21 16:31:57 -08001251 const char *q;
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001252 int len = 0;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001253
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001254 list_for_each(t, tlist) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001255 if (t->type == TOK_PREPROC_ID && t->text[1] == '!') {
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001256 char *v;
1257 char *q = t->text;
H. Peter Anvin077fb932010-07-20 14:56:30 -07001258
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001259 v = t->text + 2;
1260 if (*v == '\'' || *v == '\"' || *v == '`') {
1261 size_t len = nasm_unquote(v, NULL);
1262 size_t clen = strlen(v);
H. Peter Anvin077fb932010-07-20 14:56:30 -07001263
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001264 if (len != clen) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08001265 nasm_error(ERR_NONFATAL | ERR_PASS1,
1266 "NUL character in %%! string");
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001267 v = NULL;
1268 }
1269 }
H. Peter Anvin077fb932010-07-20 14:56:30 -07001270
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001271 if (v) {
1272 char *p = getenv(v);
1273 if (!p) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08001274 nasm_error(ERR_NONFATAL | ERR_PASS1,
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001275 "nonexistent environment variable `%s'", v);
Cyrill Gorcunovbbb7a1a2016-06-19 12:15:24 +03001276 /*
1277 * FIXME We better should investigate if accessing
1278 * ->text[1] without ->text[0] is safe enough.
1279 */
1280 t->text = nasm_zalloc(2);
1281 } else
1282 t->text = nasm_strdup(p);
Cyrill Gorcunov75004872017-07-26 01:21:16 +03001283 nasm_free(q);
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001284 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001285 }
H. Peter Anvin077fb932010-07-20 14:56:30 -07001286
H. Peter Anvine2c80182005-01-15 22:15:51 +00001287 /* Expand local macros here and not during preprocessing */
1288 if (expand_locals &&
1289 t->type == TOK_PREPROC_ID && t->text &&
1290 t->text[0] == '%' && t->text[1] == '$') {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001291 const char *q;
1292 char *p;
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04001293 Context *ctx = get_ctx(t->text, &q);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001294 if (ctx) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001295 char buffer[40];
Keith Kanios93f2e9a2007-04-14 00:10:59 +00001296 snprintf(buffer, sizeof(buffer), "..@%"PRIu32".", ctx->number);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001297 p = nasm_strcat(buffer, q);
1298 nasm_free(t->text);
1299 t->text = p;
1300 }
1301 }
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001302 if (t->type == TOK_WHITESPACE)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001303 len++;
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001304 else if (t->text)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001305 len += strlen(t->text);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001306 }
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001307
H. Peter Anvin734b1882002-04-30 21:01:08 +00001308 p = line = nasm_malloc(len + 1);
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001309
1310 list_for_each(t, tlist) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001311 if (t->type == TOK_WHITESPACE) {
H. Peter Anvinb4daadc2008-01-21 16:31:57 -08001312 *p++ = ' ';
H. Peter Anvine2c80182005-01-15 22:15:51 +00001313 } else if (t->text) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001314 q = t->text;
1315 while (*q)
1316 *p++ = *q++;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001317 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001318 }
1319 *p = '\0';
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001320
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001321 return line;
1322}
1323
1324/*
H. Peter Anvin76690a12002-04-30 20:52:49 +00001325 * A scanner, suitable for use by the expression evaluator, which
1326 * operates on a line of Tokens. Expects a pointer to a pointer to
1327 * the first token in the line to be passed in as its private_data
1328 * field.
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001329 *
1330 * FIX: This really needs to be unified with stdscan.
H. Peter Anvin76690a12002-04-30 20:52:49 +00001331 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001332static int ppscan(void *private_data, struct tokenval *tokval)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001333{
H. Peter Anvin76690a12002-04-30 20:52:49 +00001334 Token **tlineptr = private_data;
1335 Token *tline;
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001336 char ourcopy[MAX_KEYWORD+1], *p, *r, *s;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001337
H. Peter Anvine2c80182005-01-15 22:15:51 +00001338 do {
1339 tline = *tlineptr;
1340 *tlineptr = tline ? tline->next : NULL;
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04001341 } while (tline && (tline->type == TOK_WHITESPACE ||
1342 tline->type == TOK_COMMENT));
H. Peter Anvin76690a12002-04-30 20:52:49 +00001343
1344 if (!tline)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001345 return tokval->t_type = TOKEN_EOS;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001346
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001347 tokval->t_charptr = tline->text;
1348
H. Peter Anvin76690a12002-04-30 20:52:49 +00001349 if (tline->text[0] == '$' && !tline->text[1])
H. Peter Anvine2c80182005-01-15 22:15:51 +00001350 return tokval->t_type = TOKEN_HERE;
H. Peter Anvin7cf897e2002-05-30 21:30:33 +00001351 if (tline->text[0] == '$' && tline->text[1] == '$' && !tline->text[2])
H. Peter Anvine2c80182005-01-15 22:15:51 +00001352 return tokval->t_type = TOKEN_BASE;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001353
H. Peter Anvine2c80182005-01-15 22:15:51 +00001354 if (tline->type == TOK_ID) {
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001355 p = tokval->t_charptr = tline->text;
1356 if (p[0] == '$') {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001357 tokval->t_charptr++;
1358 return tokval->t_type = TOKEN_ID;
1359 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00001360
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001361 for (r = p, s = ourcopy; *r; r++) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001362 if (r >= p+MAX_KEYWORD)
1363 return tokval->t_type = TOKEN_ID; /* Not a keyword */
H. Peter Anvinac8f8fc2008-06-11 15:49:41 -07001364 *s++ = nasm_tolower(*r);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001365 }
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001366 *s = '\0';
1367 /* right, so we have an identifier sitting in temp storage. now,
1368 * is it actually a register or instruction name, or what? */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001369 return nasm_token_hash(ourcopy, tokval);
H. Peter Anvin76690a12002-04-30 20:52:49 +00001370 }
1371
H. Peter Anvine2c80182005-01-15 22:15:51 +00001372 if (tline->type == TOK_NUMBER) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001373 bool rn_error;
1374 tokval->t_integer = readnum(tline->text, &rn_error);
1375 tokval->t_charptr = tline->text;
1376 if (rn_error)
1377 return tokval->t_type = TOKEN_ERRNUM;
1378 else
1379 return tokval->t_type = TOKEN_NUM;
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001380 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00001381
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001382 if (tline->type == TOK_FLOAT) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001383 return tokval->t_type = TOKEN_FLOAT;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001384 }
1385
H. Peter Anvine2c80182005-01-15 22:15:51 +00001386 if (tline->type == TOK_STRING) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001387 char bq, *ep;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001388
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001389 bq = tline->text[0];
H. Peter Anvin11627042008-06-09 20:45:19 -07001390 tokval->t_charptr = tline->text;
1391 tokval->t_inttwo = nasm_unquote(tline->text, &ep);
H. Peter Anvind2456592008-06-19 15:04:18 -07001392
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001393 if (ep[0] != bq || ep[1] != '\0')
1394 return tokval->t_type = TOKEN_ERRSTR;
1395 else
1396 return tokval->t_type = TOKEN_STR;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001397 }
1398
H. Peter Anvine2c80182005-01-15 22:15:51 +00001399 if (tline->type == TOK_OTHER) {
1400 if (!strcmp(tline->text, "<<"))
1401 return tokval->t_type = TOKEN_SHL;
1402 if (!strcmp(tline->text, ">>"))
1403 return tokval->t_type = TOKEN_SHR;
1404 if (!strcmp(tline->text, "//"))
1405 return tokval->t_type = TOKEN_SDIV;
1406 if (!strcmp(tline->text, "%%"))
1407 return tokval->t_type = TOKEN_SMOD;
1408 if (!strcmp(tline->text, "=="))
1409 return tokval->t_type = TOKEN_EQ;
1410 if (!strcmp(tline->text, "<>"))
1411 return tokval->t_type = TOKEN_NE;
1412 if (!strcmp(tline->text, "!="))
1413 return tokval->t_type = TOKEN_NE;
1414 if (!strcmp(tline->text, "<="))
1415 return tokval->t_type = TOKEN_LE;
1416 if (!strcmp(tline->text, ">="))
1417 return tokval->t_type = TOKEN_GE;
1418 if (!strcmp(tline->text, "&&"))
1419 return tokval->t_type = TOKEN_DBL_AND;
1420 if (!strcmp(tline->text, "^^"))
1421 return tokval->t_type = TOKEN_DBL_XOR;
1422 if (!strcmp(tline->text, "||"))
1423 return tokval->t_type = TOKEN_DBL_OR;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001424 }
1425
1426 /*
1427 * We have no other options: just return the first character of
1428 * the token text.
1429 */
1430 return tokval->t_type = tline->text[0];
1431}
1432
1433/*
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001434 * Compare a string to the name of an existing macro; this is a
1435 * simple wrapper which calls either strcmp or nasm_stricmp
1436 * depending on the value of the `casesense' parameter.
1437 */
H. Peter Anvin4db5a162007-10-11 13:42:09 -07001438static int mstrcmp(const char *p, const char *q, bool casesense)
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001439{
H. Peter Anvin734b1882002-04-30 21:01:08 +00001440 return casesense ? strcmp(p, q) : nasm_stricmp(p, q);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001441}
1442
1443/*
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07001444 * Compare a string to the name of an existing macro; this is a
1445 * simple wrapper which calls either strcmp or nasm_stricmp
1446 * depending on the value of the `casesense' parameter.
1447 */
1448static int mmemcmp(const char *p, const char *q, size_t l, bool casesense)
1449{
1450 return casesense ? memcmp(p, q, l) : nasm_memicmp(p, q, l);
1451}
1452
1453/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001454 * Return the Context structure associated with a %$ token. Return
1455 * NULL, having _already_ reported an error condition, if the
1456 * context stack isn't deep enough for the supplied number of $
1457 * signs.
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001458 *
1459 * If "namep" is non-NULL, set it to the pointer to the macro name
1460 * tail, i.e. the part beyond %$...
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001461 */
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04001462static Context *get_ctx(const char *name, const char **namep)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001463{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001464 Context *ctx;
1465 int i;
1466
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001467 if (namep)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001468 *namep = name;
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001469
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001470 if (!name || name[0] != '%' || name[1] != '$')
H. Peter Anvine2c80182005-01-15 22:15:51 +00001471 return NULL;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001472
H. Peter Anvine2c80182005-01-15 22:15:51 +00001473 if (!cstk) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08001474 nasm_error(ERR_NONFATAL, "`%s': context stack is empty", name);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001475 return NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001476 }
1477
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001478 name += 2;
1479 ctx = cstk;
1480 i = 0;
1481 while (ctx && *name == '$') {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001482 name++;
1483 i++;
1484 ctx = ctx->next;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001485 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001486 if (!ctx) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08001487 nasm_error(ERR_NONFATAL, "`%s': context stack is only"
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001488 " %d level%s deep", name, i, (i == 1 ? "" : "s"));
H. Peter Anvine2c80182005-01-15 22:15:51 +00001489 return NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001490 }
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001491
1492 if (namep)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001493 *namep = name;
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001494
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04001495 return ctx;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001496}
1497
1498/*
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001499 * Open an include file. This routine must always return a valid
1500 * file pointer if it returns - it's responsible for throwing an
1501 * ERR_FATAL and bombing out completely if not. It should also try
1502 * the include path one by one until it finds the file or reaches
1503 * the end of the path.
H. Peter Anvind81a2352016-09-21 14:03:18 -07001504 *
1505 * Note: for INC_PROBE the function returns NULL at all times;
1506 * instead look for the
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001507 */
H. Peter Anvind81a2352016-09-21 14:03:18 -07001508enum incopen_mode {
1509 INC_NEEDED, /* File must exist */
1510 INC_OPTIONAL, /* Missing is OK */
1511 INC_PROBE /* Only an existence probe */
1512};
1513
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001514/* This is conducts a full pathname search */
1515static FILE *inc_fopen_search(const char *file, StrList **slpath,
1516 enum incopen_mode omode, enum file_flags fmode)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001517{
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001518 FILE *fp;
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001519 char *prefix = "";
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001520 const IncPath *ip = ipath;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001521 int len = strlen(file);
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001522 size_t prefix_len = 0;
1523 StrList *sl;
Jim Kukunas65a8afc2016-06-13 16:00:42 -04001524 size_t path_len;
H. Peter Anvind81a2352016-09-21 14:03:18 -07001525 bool found;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001526
H. Peter Anvine2c80182005-01-15 22:15:51 +00001527 while (1) {
Jim Kukunas65a8afc2016-06-13 16:00:42 -04001528 path_len = prefix_len + len + 1;
1529
1530 sl = nasm_malloc(path_len + sizeof sl->next);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001531 memcpy(sl->str, prefix, prefix_len);
1532 memcpy(sl->str+prefix_len, file, len+1);
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001533 sl->next = NULL;
Jim Kukunas65a8afc2016-06-13 16:00:42 -04001534
H. Peter Anvind81a2352016-09-21 14:03:18 -07001535 if (omode == INC_PROBE) {
1536 fp = NULL;
1537 found = nasm_file_exists(sl->str);
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001538 } else {
H. Peter Anvind81a2352016-09-21 14:03:18 -07001539 fp = nasm_open_read(sl->str, fmode);
1540 found = (fp != NULL);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001541 }
H. Peter Anvind81a2352016-09-21 14:03:18 -07001542 if (found) {
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001543 *slpath = sl;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001544 return fp;
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001545 }
Jim Kukunas65a8afc2016-06-13 16:00:42 -04001546
H. Peter Anvind81a2352016-09-21 14:03:18 -07001547 nasm_free(sl);
Jim Kukunas65a8afc2016-06-13 16:00:42 -04001548
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001549 if (!ip)
1550 return NULL;
1551
1552 prefix = ip->path;
1553 prefix_len = strlen(prefix);
1554 ip = ip->next;
1555 }
1556}
1557
1558/*
1559 * Open a file, or test for the presence of one (depending on omode),
1560 * considering the include path.
1561 */
1562static FILE *inc_fopen(const char *file,
H. Peter Anvin9924d1e2016-10-04 00:59:39 -07001563 StrList **dhead,
H. Peter Anvinccad6f92016-10-04 00:34:35 -07001564 const char **found_path,
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001565 enum incopen_mode omode,
1566 enum file_flags fmode)
1567{
1568 StrList *sl;
1569 struct hash_insert hi;
1570 void **hp;
1571 char *path;
1572 FILE *fp = NULL;
1573
1574 hp = hash_find(&FileHash, file, &hi);
1575 if (hp) {
1576 path = *hp;
Martin Storsjöf283c8f2017-08-13 17:28:46 +03001577 if (path || omode != INC_NEEDED) {
H. Peter Anvin97fda4c2017-08-16 15:52:51 -07001578 nasm_add_string_to_strlist(dhead, path ? path : file);
Martin Storsjöf283c8f2017-08-13 17:28:46 +03001579 }
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001580 } else {
1581 /* Need to do the actual path search */
1582 size_t file_len;
1583
1584 sl = NULL;
1585 fp = inc_fopen_search(file, &sl, omode, fmode);
1586
1587 file_len = strlen(file);
1588
1589 if (!sl) {
1590 /* Store negative result for this file */
1591 sl = nasm_malloc(file_len + 1 + sizeof sl->next);
1592 memcpy(sl->str, file, file_len+1);
1593 sl->next = NULL;
1594 file = sl->str;
1595 path = NULL;
1596 } else {
1597 path = sl->str;
1598 file = strchr(path, '\0') - file_len;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001599 }
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001600
1601 hash_add(&hi, file, path); /* Positive or negative result */
1602
H. Peter Anvin9924d1e2016-10-04 00:59:39 -07001603 /*
1604 * Add file to dependency path. The in_list() is needed
1605 * in case the file was already added with %depend.
1606 */
1607 if (path || omode != INC_NEEDED)
H. Peter Anvin436e3672016-10-04 01:12:28 -07001608 nasm_add_to_strlist(dhead, sl);
H. Peter Anvineba20a72002-04-30 20:53:55 +00001609 }
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001610
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001611 if (!path) {
1612 if (omode == INC_NEEDED)
1613 nasm_fatal(0, "unable to open include file `%s'", file);
1614
1615 if (found_path)
1616 *found_path = NULL;
1617
1618 return NULL;
1619 }
1620
1621 if (!fp && omode != INC_PROBE)
Cyrill Gorcunov4ff8c632017-01-06 00:36:23 +03001622 fp = nasm_open_read(path, fmode);
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001623
1624 if (found_path)
H. Peter Anvinccad6f92016-10-04 00:34:35 -07001625 *found_path = path;
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001626
1627 return fp;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001628}
1629
1630/*
Fabian Giesen0bbc38d2016-04-28 13:48:14 -07001631 * Opens an include or input file. Public version, for use by modules
1632 * that get a file:lineno pair and need to look at the file again
1633 * (e.g. the CodeView debug backend). Returns NULL on failure.
1634 */
H. Peter Anvin3e83cec2016-05-25 04:28:46 -07001635FILE *pp_input_fopen(const char *filename, enum file_flags mode)
Fabian Giesen0bbc38d2016-04-28 13:48:14 -07001636{
H. Peter Anvin9924d1e2016-10-04 00:59:39 -07001637 return inc_fopen(filename, NULL, NULL, INC_OPTIONAL, mode);
Fabian Giesen0bbc38d2016-04-28 13:48:14 -07001638}
1639
1640/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001641 * Determine if we should warn on defining a single-line macro of
H. Peter Anvinef7468f2002-04-30 20:57:59 +00001642 * name `name', with `nparam' parameters. If nparam is 0 or -1, will
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001643 * return true if _any_ single-line macro of that name is defined.
1644 * Otherwise, will return true if a single-line macro with either
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001645 * `nparam' or no parameters is defined.
1646 *
1647 * If a macro with precisely the right number of parameters is
H. Peter Anvinef7468f2002-04-30 20:57:59 +00001648 * defined, or nparam is -1, the address of the definition structure
1649 * will be returned in `defn'; otherwise NULL will be returned. If `defn'
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001650 * is NULL, no action will be taken regarding its contents, and no
1651 * error will occur.
1652 *
1653 * Note that this is also called with nparam zero to resolve
1654 * `ifdef'.
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001655 *
1656 * If you already know which context macro belongs to, you can pass
1657 * the context pointer as first parameter; if you won't but name begins
1658 * with %$ the context will be automatically computed. If all_contexts
1659 * is true, macro will be searched in outer contexts as well.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001660 */
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07001661static bool
H. Peter Anvinb2a5fda2008-06-19 21:42:42 -07001662smacro_defined(Context * ctx, const char *name, int nparam, SMacro ** defn,
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07001663 bool nocase)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001664{
H. Peter Anvin166c2472008-05-28 12:28:58 -07001665 struct hash_table *smtbl;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001666 SMacro *m;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001667
H. Peter Anvin97a23472007-09-16 17:57:25 -07001668 if (ctx) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001669 smtbl = &ctx->localmac;
H. Peter Anvin97a23472007-09-16 17:57:25 -07001670 } else if (name[0] == '%' && name[1] == '$') {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001671 if (cstk)
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04001672 ctx = get_ctx(name, &name);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001673 if (!ctx)
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001674 return false; /* got to return _something_ */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001675 smtbl = &ctx->localmac;
H. Peter Anvin97a23472007-09-16 17:57:25 -07001676 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001677 smtbl = &smacros;
H. Peter Anvin97a23472007-09-16 17:57:25 -07001678 }
H. Peter Anvin166c2472008-05-28 12:28:58 -07001679 m = (SMacro *) hash_findix(smtbl, name);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001680
H. Peter Anvine2c80182005-01-15 22:15:51 +00001681 while (m) {
1682 if (!mstrcmp(m->name, name, m->casesense && nocase) &&
Charles Crayne192d5b52007-10-18 19:02:42 -07001683 (nparam <= 0 || m->nparam == 0 || nparam == (int) m->nparam)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001684 if (defn) {
Charles Crayne192d5b52007-10-18 19:02:42 -07001685 if (nparam == (int) m->nparam || nparam == -1)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001686 *defn = m;
1687 else
1688 *defn = NULL;
1689 }
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001690 return true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001691 }
1692 m = m->next;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001693 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001694
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001695 return false;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001696}
1697
1698/*
1699 * Count and mark off the parameters in a multi-line macro call.
1700 * This is called both from within the multi-line macro expansion
1701 * code, and also to mark off the default parameters when provided
1702 * in a %macro definition line.
1703 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001704static void count_mmac_params(Token * t, int *nparam, Token *** params)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001705{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001706 int paramsize, brace;
1707
1708 *nparam = paramsize = 0;
1709 *params = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001710 while (t) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001711 /* +1: we need space for the final NULL */
H. Peter Anvin91fb6f12008-09-01 10:56:33 -07001712 if (*nparam+1 >= paramsize) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001713 paramsize += PARAM_DELTA;
1714 *params = nasm_realloc(*params, sizeof(**params) * paramsize);
1715 }
1716 skip_white_(t);
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08001717 brace = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001718 if (tok_is_(t, "{"))
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08001719 brace++;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001720 (*params)[(*nparam)++] = t;
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08001721 if (brace) {
1722 while (brace && (t = t->next) != NULL) {
1723 if (tok_is_(t, "{"))
1724 brace++;
1725 else if (tok_is_(t, "}"))
1726 brace--;
1727 }
1728
1729 if (t) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001730 /*
1731 * Now we've found the closing brace, look further
1732 * for the comma.
1733 */
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08001734 t = t->next;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001735 skip_white_(t);
1736 if (tok_isnt_(t, ",")) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08001737 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00001738 "braces do not enclose all of macro parameter");
1739 while (tok_isnt_(t, ","))
1740 t = t->next;
1741 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001742 }
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08001743 } else {
1744 while (tok_isnt_(t, ","))
1745 t = t->next;
1746 }
1747 if (t) { /* got a comma/brace */
1748 t = t->next; /* eat the comma */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001749 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001750 }
1751}
1752
1753/*
H. Peter Anvin76690a12002-04-30 20:52:49 +00001754 * Determine whether one of the various `if' conditions is true or
1755 * not.
1756 *
1757 * We must free the tline we get passed.
1758 */
H. Peter Anvin70055962007-10-11 00:05:31 -07001759static bool if_condition(Token * tline, enum preproc_token ct)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001760{
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001761 enum pp_conditional i = PP_COND(ct);
H. Peter Anvin70055962007-10-11 00:05:31 -07001762 bool j;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001763 Token *t, *tt, **tptr, *origline;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001764 struct tokenval tokval;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001765 expr *evalresult;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001766 enum pp_token_type needtype;
H. Peter Anvin077fb932010-07-20 14:56:30 -07001767 char *p;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001768
1769 origline = tline;
1770
H. Peter Anvine2c80182005-01-15 22:15:51 +00001771 switch (i) {
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001772 case PPC_IFCTX:
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001773 j = false; /* have we matched yet? */
Victor van den Elzen0e857f12008-07-23 13:21:29 +02001774 while (true) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001775 skip_white_(tline);
Victor van den Elzen0e857f12008-07-23 13:21:29 +02001776 if (!tline)
1777 break;
1778 if (tline->type != TOK_ID) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08001779 nasm_error(ERR_NONFATAL,
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001780 "`%s' expects context identifiers", pp_directives[ct]);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001781 free_tlist(origline);
1782 return -1;
1783 }
Victor van den Elzen0e857f12008-07-23 13:21:29 +02001784 if (cstk && cstk->name && !nasm_stricmp(tline->text, cstk->name))
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001785 j = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001786 tline = tline->next;
1787 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001788 break;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001789
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001790 case PPC_IFDEF:
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001791 j = false; /* have we matched yet? */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001792 while (tline) {
1793 skip_white_(tline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001794 if (!tline || (tline->type != TOK_ID &&
1795 (tline->type != TOK_PREPROC_ID ||
1796 tline->text[1] != '$'))) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08001797 nasm_error(ERR_NONFATAL,
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001798 "`%s' expects macro identifiers", pp_directives[ct]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001799 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001800 }
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07001801 if (smacro_defined(NULL, tline->text, 0, NULL, true))
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001802 j = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001803 tline = tline->next;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001804 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001805 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001806
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001807 case PPC_IFENV:
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001808 tline = expand_smacro(tline);
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001809 j = false; /* have we matched yet? */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001810 while (tline) {
1811 skip_white_(tline);
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001812 if (!tline || (tline->type != TOK_ID &&
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001813 tline->type != TOK_STRING &&
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001814 (tline->type != TOK_PREPROC_ID ||
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001815 tline->text[1] != '!'))) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08001816 nasm_error(ERR_NONFATAL,
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001817 "`%s' expects environment variable names",
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001818 pp_directives[ct]);
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001819 goto fail;
1820 }
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001821 p = tline->text;
1822 if (tline->type == TOK_PREPROC_ID)
1823 p += 2; /* Skip leading %! */
1824 if (*p == '\'' || *p == '\"' || *p == '`')
1825 nasm_unquote_cstr(p, ct);
1826 if (getenv(p))
1827 j = true;
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001828 tline = tline->next;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001829 }
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001830 break;
1831
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001832 case PPC_IFIDN:
1833 case PPC_IFIDNI:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001834 tline = expand_smacro(tline);
1835 t = tt = tline;
1836 while (tok_isnt_(tt, ","))
1837 tt = tt->next;
1838 if (!tt) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08001839 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00001840 "`%s' expects two comma-separated arguments",
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001841 pp_directives[ct]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001842 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001843 }
1844 tt = tt->next;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001845 j = true; /* assume equality unless proved not */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001846 while ((t->type != TOK_OTHER || strcmp(t->text, ",")) && tt) {
1847 if (tt->type == TOK_OTHER && !strcmp(tt->text, ",")) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08001848 nasm_error(ERR_NONFATAL, "`%s': more than one comma on line",
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001849 pp_directives[ct]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001850 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001851 }
1852 if (t->type == TOK_WHITESPACE) {
1853 t = t->next;
1854 continue;
1855 }
1856 if (tt->type == TOK_WHITESPACE) {
1857 tt = tt->next;
1858 continue;
1859 }
1860 if (tt->type != t->type) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001861 j = false; /* found mismatching tokens */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001862 break;
1863 }
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07001864 /* When comparing strings, need to unquote them first */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001865 if (t->type == TOK_STRING) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001866 size_t l1 = nasm_unquote(t->text, NULL);
1867 size_t l2 = nasm_unquote(tt->text, NULL);
H. Peter Anvind2456592008-06-19 15:04:18 -07001868
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001869 if (l1 != l2) {
1870 j = false;
1871 break;
1872 }
1873 if (mmemcmp(t->text, tt->text, l1, i == PPC_IFIDN)) {
1874 j = false;
1875 break;
1876 }
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07001877 } else if (mstrcmp(tt->text, t->text, i == PPC_IFIDN) != 0) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001878 j = false; /* found mismatching tokens */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001879 break;
1880 }
Nickolay Yurchenkof3b3ce22003-09-21 20:38:43 +00001881
H. Peter Anvine2c80182005-01-15 22:15:51 +00001882 t = t->next;
1883 tt = tt->next;
1884 }
1885 if ((t->type != TOK_OTHER || strcmp(t->text, ",")) || tt)
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001886 j = false; /* trailing gunk on one end or other */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001887 break;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001888
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001889 case PPC_IFMACRO:
H. Peter Anvin89cee572009-07-15 09:16:54 -04001890 {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001891 bool found = false;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001892 MMacro searching, *mmac;
H. Peter Anvin65747262002-05-07 00:10:05 +00001893
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001894 skip_white_(tline);
1895 tline = expand_id(tline);
1896 if (!tok_type_(tline, TOK_ID)) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08001897 nasm_error(ERR_NONFATAL,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001898 "`%s' expects a macro name", pp_directives[ct]);
1899 goto fail;
1900 }
1901 searching.name = nasm_strdup(tline->text);
1902 searching.casesense = true;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001903 searching.plus = false;
1904 searching.nolist = false;
1905 searching.in_progress = 0;
1906 searching.max_depth = 0;
1907 searching.rep_nest = NULL;
1908 searching.nparam_min = 0;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001909 searching.nparam_max = INT_MAX;
1910 tline = expand_smacro(tline->next);
1911 skip_white_(tline);
1912 if (!tline) {
1913 } else if (!tok_type_(tline, TOK_NUMBER)) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08001914 nasm_error(ERR_NONFATAL,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001915 "`%s' expects a parameter count or nothing",
1916 pp_directives[ct]);
1917 } else {
1918 searching.nparam_min = searching.nparam_max =
1919 readnum(tline->text, &j);
1920 if (j)
H. Peter Anvin130736c2016-02-17 20:27:41 -08001921 nasm_error(ERR_NONFATAL,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001922 "unable to parse parameter count `%s'",
1923 tline->text);
1924 }
1925 if (tline && tok_is_(tline->next, "-")) {
1926 tline = tline->next->next;
1927 if (tok_is_(tline, "*"))
1928 searching.nparam_max = INT_MAX;
1929 else if (!tok_type_(tline, TOK_NUMBER))
H. Peter Anvin130736c2016-02-17 20:27:41 -08001930 nasm_error(ERR_NONFATAL,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001931 "`%s' expects a parameter count after `-'",
1932 pp_directives[ct]);
1933 else {
1934 searching.nparam_max = readnum(tline->text, &j);
1935 if (j)
H. Peter Anvin130736c2016-02-17 20:27:41 -08001936 nasm_error(ERR_NONFATAL,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001937 "unable to parse parameter count `%s'",
1938 tline->text);
1939 if (searching.nparam_min > searching.nparam_max)
H. Peter Anvin130736c2016-02-17 20:27:41 -08001940 nasm_error(ERR_NONFATAL,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001941 "minimum parameter count exceeds maximum");
1942 }
1943 }
1944 if (tline && tok_is_(tline->next, "+")) {
1945 tline = tline->next;
1946 searching.plus = true;
1947 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001948 mmac = (MMacro *) hash_findix(&mmacros, searching.name);
1949 while (mmac) {
1950 if (!strcmp(mmac->name, searching.name) &&
1951 (mmac->nparam_min <= searching.nparam_max
1952 || searching.plus)
1953 && (searching.nparam_min <= mmac->nparam_max
1954 || mmac->plus)) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001955 found = true;
1956 break;
1957 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001958 mmac = mmac->next;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001959 }
1960 if (tline && tline->next)
H. Peter Anvin130736c2016-02-17 20:27:41 -08001961 nasm_error(ERR_WARNING|ERR_PASS1,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001962 "trailing garbage after %%ifmacro ignored");
1963 nasm_free(searching.name);
1964 j = found;
1965 break;
H. Peter Anvin89cee572009-07-15 09:16:54 -04001966 }
H. Peter Anvin65747262002-05-07 00:10:05 +00001967
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001968 case PPC_IFID:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001969 needtype = TOK_ID;
1970 goto iftype;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001971 case PPC_IFNUM:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001972 needtype = TOK_NUMBER;
1973 goto iftype;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001974 case PPC_IFSTR:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001975 needtype = TOK_STRING;
1976 goto iftype;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001977
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001978iftype:
1979 t = tline = expand_smacro(tline);
H. Peter Anvind85d2502008-05-04 17:53:31 -07001980
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001981 while (tok_type_(t, TOK_WHITESPACE) ||
1982 (needtype == TOK_NUMBER &&
1983 tok_type_(t, TOK_OTHER) &&
1984 (t->text[0] == '-' || t->text[0] == '+') &&
1985 !t->text[1]))
1986 t = t->next;
H. Peter Anvind85d2502008-05-04 17:53:31 -07001987
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001988 j = tok_type_(t, needtype);
1989 break;
H. Peter Anvincbf768d2008-02-16 16:41:25 -08001990
1991 case PPC_IFTOKEN:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001992 t = tline = expand_smacro(tline);
1993 while (tok_type_(t, TOK_WHITESPACE))
1994 t = t->next;
H. Peter Anvincbf768d2008-02-16 16:41:25 -08001995
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001996 j = false;
1997 if (t) {
1998 t = t->next; /* Skip the actual token */
1999 while (tok_type_(t, TOK_WHITESPACE))
2000 t = t->next;
2001 j = !t; /* Should be nothing left */
2002 }
2003 break;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002004
H. Peter Anvin134b9462008-02-16 17:01:40 -08002005 case PPC_IFEMPTY:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002006 t = tline = expand_smacro(tline);
2007 while (tok_type_(t, TOK_WHITESPACE))
2008 t = t->next;
H. Peter Anvin134b9462008-02-16 17:01:40 -08002009
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002010 j = !t; /* Should be empty */
2011 break;
H. Peter Anvin134b9462008-02-16 17:01:40 -08002012
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002013 case PPC_IF:
H. Peter Anvine2c80182005-01-15 22:15:51 +00002014 t = tline = expand_smacro(tline);
2015 tptr = &t;
2016 tokval.t_type = TOKEN_INVALID;
2017 evalresult = evaluate(ppscan, tptr, &tokval,
H. Peter Anvin130736c2016-02-17 20:27:41 -08002018 NULL, pass | CRITICAL, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002019 if (!evalresult)
2020 return -1;
2021 if (tokval.t_type)
H. Peter Anvin130736c2016-02-17 20:27:41 -08002022 nasm_error(ERR_WARNING|ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +00002023 "trailing garbage after expression ignored");
2024 if (!is_simple(evalresult)) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002025 nasm_error(ERR_NONFATAL,
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002026 "non-constant value given to `%s'", pp_directives[ct]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002027 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002028 }
Chuck Crayne60ae75d2007-05-02 01:59:16 +00002029 j = reloc_value(evalresult) != 0;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002030 break;
H. Peter Anvin95e28822007-09-12 04:20:08 +00002031
H. Peter Anvine2c80182005-01-15 22:15:51 +00002032 default:
H. Peter Anvin130736c2016-02-17 20:27:41 -08002033 nasm_error(ERR_FATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00002034 "preprocessor directive `%s' not yet implemented",
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002035 pp_directives[ct]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002036 goto fail;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002037 }
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002038
2039 free_tlist(origline);
2040 return j ^ PP_NEGATIVE(ct);
H. Peter Anvin70653092007-10-19 14:42:29 -07002041
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002042fail:
2043 free_tlist(origline);
2044 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002045}
2046
2047/*
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002048 * Common code for defining an smacro
2049 */
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08002050static bool define_smacro(Context *ctx, const char *mname, bool casesense,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002051 int nparam, Token *expansion)
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002052{
2053 SMacro *smac, **smhead;
H. Peter Anvin166c2472008-05-28 12:28:58 -07002054 struct hash_table *smtbl;
H. Peter Anvin70653092007-10-19 14:42:29 -07002055
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002056 if (smacro_defined(ctx, mname, nparam, &smac, casesense)) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002057 if (!smac) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002058 nasm_error(ERR_WARNING|ERR_PASS1,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002059 "single-line macro `%s' defined both with and"
2060 " without parameters", mname);
2061 /*
2062 * Some instances of the old code considered this a failure,
2063 * some others didn't. What is the right thing to do here?
2064 */
2065 free_tlist(expansion);
2066 return false; /* Failure */
2067 } else {
2068 /*
2069 * We're redefining, so we have to take over an
2070 * existing SMacro structure. This means freeing
2071 * what was already in it.
2072 */
2073 nasm_free(smac->name);
2074 free_tlist(smac->expansion);
2075 }
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002076 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002077 smtbl = ctx ? &ctx->localmac : &smacros;
2078 smhead = (SMacro **) hash_findi_add(smtbl, mname);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002079 smac = nasm_malloc(sizeof(SMacro));
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002080 smac->next = *smhead;
2081 *smhead = smac;
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002082 }
2083 smac->name = nasm_strdup(mname);
2084 smac->casesense = casesense;
2085 smac->nparam = nparam;
2086 smac->expansion = expansion;
2087 smac->in_progress = false;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002088 return true; /* Success */
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002089}
2090
2091/*
2092 * Undefine an smacro
2093 */
2094static void undef_smacro(Context *ctx, const char *mname)
2095{
2096 SMacro **smhead, *s, **sp;
H. Peter Anvin166c2472008-05-28 12:28:58 -07002097 struct hash_table *smtbl;
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002098
H. Peter Anvin166c2472008-05-28 12:28:58 -07002099 smtbl = ctx ? &ctx->localmac : &smacros;
2100 smhead = (SMacro **)hash_findi(smtbl, mname, NULL);
H. Peter Anvin70653092007-10-19 14:42:29 -07002101
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002102 if (smhead) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002103 /*
2104 * We now have a macro name... go hunt for it.
2105 */
2106 sp = smhead;
2107 while ((s = *sp) != NULL) {
2108 if (!mstrcmp(s->name, mname, s->casesense)) {
2109 *sp = s->next;
2110 nasm_free(s->name);
2111 free_tlist(s->expansion);
2112 nasm_free(s);
2113 } else {
2114 sp = &s->next;
2115 }
2116 }
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002117 }
2118}
2119
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002120/*
H. Peter Anvina26433d2008-07-16 14:40:01 -07002121 * Parse a mmacro specification.
2122 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002123static bool parse_mmacro_spec(Token *tline, MMacro *def, const char *directive)
H. Peter Anvina26433d2008-07-16 14:40:01 -07002124{
2125 bool err;
2126
2127 tline = tline->next;
2128 skip_white_(tline);
2129 tline = expand_id(tline);
2130 if (!tok_type_(tline, TOK_ID)) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002131 nasm_error(ERR_NONFATAL, "`%s' expects a macro name", directive);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002132 return false;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002133 }
Victor van den Elzenb916ede2008-07-23 15:14:22 +02002134
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002135 def->prev = NULL;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002136 def->name = nasm_strdup(tline->text);
2137 def->plus = false;
2138 def->nolist = false;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002139 def->in_progress = 0;
2140 def->rep_nest = NULL;
Victor van den Elzenb916ede2008-07-23 15:14:22 +02002141 def->nparam_min = 0;
2142 def->nparam_max = 0;
2143
H. Peter Anvina26433d2008-07-16 14:40:01 -07002144 tline = expand_smacro(tline->next);
2145 skip_white_(tline);
2146 if (!tok_type_(tline, TOK_NUMBER)) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002147 nasm_error(ERR_NONFATAL, "`%s' expects a parameter count", directive);
H. Peter Anvina26433d2008-07-16 14:40:01 -07002148 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002149 def->nparam_min = def->nparam_max =
2150 readnum(tline->text, &err);
2151 if (err)
H. Peter Anvin130736c2016-02-17 20:27:41 -08002152 nasm_error(ERR_NONFATAL,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002153 "unable to parse parameter count `%s'", tline->text);
H. Peter Anvina26433d2008-07-16 14:40:01 -07002154 }
2155 if (tline && tok_is_(tline->next, "-")) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002156 tline = tline->next->next;
2157 if (tok_is_(tline, "*")) {
2158 def->nparam_max = INT_MAX;
2159 } else if (!tok_type_(tline, TOK_NUMBER)) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002160 nasm_error(ERR_NONFATAL,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002161 "`%s' expects a parameter count after `-'", directive);
2162 } else {
2163 def->nparam_max = readnum(tline->text, &err);
2164 if (err) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002165 nasm_error(ERR_NONFATAL, "unable to parse parameter count `%s'",
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002166 tline->text);
2167 }
2168 if (def->nparam_min > def->nparam_max) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002169 nasm_error(ERR_NONFATAL, "minimum parameter count exceeds maximum");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002170 }
2171 }
H. Peter Anvina26433d2008-07-16 14:40:01 -07002172 }
2173 if (tline && tok_is_(tline->next, "+")) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002174 tline = tline->next;
2175 def->plus = true;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002176 }
2177 if (tline && tok_type_(tline->next, TOK_ID) &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002178 !nasm_stricmp(tline->next->text, ".nolist")) {
2179 tline = tline->next;
2180 def->nolist = true;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002181 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002182
H. Peter Anvina26433d2008-07-16 14:40:01 -07002183 /*
2184 * Handle default parameters.
2185 */
2186 if (tline && tline->next) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002187 def->dlist = tline->next;
2188 tline->next = NULL;
2189 count_mmac_params(def->dlist, &def->ndefs, &def->defaults);
H. Peter Anvina26433d2008-07-16 14:40:01 -07002190 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002191 def->dlist = NULL;
2192 def->defaults = NULL;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002193 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002194 def->expansion = NULL;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002195
H. Peter Anvin89cee572009-07-15 09:16:54 -04002196 if (def->defaults && def->ndefs > def->nparam_max - def->nparam_min &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002197 !def->plus)
H. Peter Anvin130736c2016-02-17 20:27:41 -08002198 nasm_error(ERR_WARNING|ERR_PASS1|ERR_WARN_MDP,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002199 "too many default macro parameters");
Victor van den Elzenb916ede2008-07-23 15:14:22 +02002200
H. Peter Anvina26433d2008-07-16 14:40:01 -07002201 return true;
2202}
2203
2204
2205/*
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002206 * Decode a size directive
2207 */
2208static int parse_size(const char *str) {
2209 static const char *size_names[] =
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002210 { "byte", "dword", "oword", "qword", "tword", "word", "yword" };
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002211 static const int sizes[] =
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002212 { 0, 1, 4, 16, 8, 10, 2, 32 };
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002213
Cyrill Gorcunova7319242010-06-03 22:04:36 +04002214 return sizes[bsii(str, size_names, ARRAY_SIZE(size_names))+1];
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002215}
2216
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07002217/*
2218 * Process a preprocessor %pragma directive. Currently there are none.
2219 * Gets passed the token list starting with the "preproc" token from
2220 * "%pragma preproc".
2221 */
2222static void do_pragma_preproc(Token *tline)
2223{
2224 /* Skip to the real stuff */
2225 tline = tline->next;
2226 skip_white_(tline);
2227 if (!tline)
2228 return;
2229
2230 (void)tline; /* Nothing else to do at present */
2231}
2232
Ed Beroset3ab3f412002-06-11 03:31:49 +00002233/**
2234 * find and process preprocessor directive in passed line
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002235 * Find out if a line contains a preprocessor directive, and deal
2236 * with it if so.
H. Peter Anvin70653092007-10-19 14:42:29 -07002237 *
Ed Beroset3ab3f412002-06-11 03:31:49 +00002238 * If a directive _is_ found, it is the responsibility of this routine
2239 * (and not the caller) to free_tlist() the line.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002240 *
Ed Beroset3ab3f412002-06-11 03:31:49 +00002241 * @param tline a pointer to the current tokeninzed line linked list
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07002242 * @param output if this directive generated output
Ed Beroset3ab3f412002-06-11 03:31:49 +00002243 * @return DIRECTIVE_FOUND or NO_DIRECTIVE_FOUND
H. Peter Anvin70653092007-10-19 14:42:29 -07002244 *
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002245 */
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07002246static int do_directive(Token *tline, char **output)
H. Peter Anvineba20a72002-04-30 20:53:55 +00002247{
H. Peter Anvin4169a472007-09-12 01:29:43 +00002248 enum preproc_token i;
2249 int j;
H. Peter Anvin70055962007-10-11 00:05:31 -07002250 bool err;
2251 int nparam;
2252 bool nolist;
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07002253 bool casesense;
H. Peter Anvin8cfdb9d2007-09-14 18:36:01 -07002254 int k, m;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002255 int offset;
H. Peter Anvinccad6f92016-10-04 00:34:35 -07002256 char *p, *pp;
2257 const char *found_path;
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08002258 const char *mname;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002259 Include *inc;
2260 Context *ctx;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002261 Cond *cond;
2262 MMacro *mmac, **mmhead;
Jin Kyu Songc9486b92013-10-28 17:07:57 -07002263 Token *t = NULL, *tt, *param_start, *macro_start, *last, **tptr, *origline;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002264 Line *l;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002265 struct tokenval tokval;
2266 expr *evalresult;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002267 MMacro *tmp_defining; /* Used when manipulating rep_nest */
H. Peter Anvinf8ba53e2007-10-11 10:11:57 -07002268 int64_t count;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07002269 size_t len;
H. Peter Anvin8e3f75e2008-09-24 00:21:58 -07002270 int severity;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002271
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07002272 *output = NULL; /* No output generated */
H. Peter Anvin76690a12002-04-30 20:52:49 +00002273 origline = tline;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002274
H. Peter Anvineba20a72002-04-30 20:53:55 +00002275 skip_white_(tline);
H. Peter Anvinf2936d72008-06-04 15:11:23 -07002276 if (!tline || !tok_type_(tline, TOK_PREPROC_ID) ||
H. Peter Anvine2c80182005-01-15 22:15:51 +00002277 (tline->text[1] == '%' || tline->text[1] == '$'
2278 || tline->text[1] == '!'))
2279 return NO_DIRECTIVE_FOUND;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002280
H. Peter Anvin4169a472007-09-12 01:29:43 +00002281 i = pp_token_hash(tline->text);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002282
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002283 /*
2284 * FIXME: We zap execution of PP_RMACRO, PP_IRMACRO, PP_EXITMACRO
2285 * since they are known to be buggy at moment, we need to fix them
2286 * in future release (2.09-2.10)
2287 */
Philipp Klokeb432f572013-03-31 12:01:23 +02002288 if (i == PP_RMACRO || i == PP_IRMACRO || i == PP_EXITMACRO) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002289 nasm_error(ERR_NONFATAL, "unknown preprocessor directive `%s'",
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002290 tline->text);
2291 return NO_DIRECTIVE_FOUND;
2292 }
2293
2294 /*
2295 * If we're in a non-emitting branch of a condition construct,
2296 * or walking to the end of an already terminated %rep block,
2297 * we should ignore all directives except for condition
2298 * directives.
2299 */
2300 if (((istk->conds && !emitting(istk->conds->state)) ||
2301 (istk->mstk && !istk->mstk->in_progress)) && !is_condition(i)) {
2302 return NO_DIRECTIVE_FOUND;
2303 }
2304
2305 /*
2306 * If we're defining a macro or reading a %rep block, we should
2307 * ignore all directives except for %macro/%imacro (which nest),
2308 * %endm/%endmacro, and (only if we're in a %rep block) %endrep.
2309 * If we're in a %rep block, another %rep nests, so should be let through.
2310 */
2311 if (defining && i != PP_MACRO && i != PP_IMACRO &&
2312 i != PP_RMACRO && i != PP_IRMACRO &&
2313 i != PP_ENDMACRO && i != PP_ENDM &&
2314 (defining->name || (i != PP_ENDREP && i != PP_REP))) {
2315 return NO_DIRECTIVE_FOUND;
2316 }
2317
2318 if (defining) {
2319 if (i == PP_MACRO || i == PP_IMACRO ||
2320 i == PP_RMACRO || i == PP_IRMACRO) {
2321 nested_mac_count++;
2322 return NO_DIRECTIVE_FOUND;
2323 } else if (nested_mac_count > 0) {
2324 if (i == PP_ENDMACRO) {
2325 nested_mac_count--;
2326 return NO_DIRECTIVE_FOUND;
2327 }
2328 }
2329 if (!defining->name) {
2330 if (i == PP_REP) {
2331 nested_rep_count++;
2332 return NO_DIRECTIVE_FOUND;
2333 } else if (nested_rep_count > 0) {
2334 if (i == PP_ENDREP) {
2335 nested_rep_count--;
2336 return NO_DIRECTIVE_FOUND;
2337 }
2338 }
2339 }
2340 }
2341
H. Peter Anvin4169a472007-09-12 01:29:43 +00002342 switch (i) {
2343 case PP_INVALID:
H. Peter Anvin130736c2016-02-17 20:27:41 -08002344 nasm_error(ERR_NONFATAL, "unknown preprocessor directive `%s'",
H. Peter Anvine2c80182005-01-15 22:15:51 +00002345 tline->text);
2346 return NO_DIRECTIVE_FOUND; /* didn't get it */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002347
H. Peter Anvin3f87a2a2016-10-04 14:07:19 -07002348 case PP_PRAGMA:
2349 /*
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07002350 * %pragma namespace options...
2351 *
2352 * The namespace "preproc" is reserved for the preprocessor;
2353 * all other namespaces generate a [pragma] assembly directive.
2354 *
2355 * Invalid %pragmas are ignored and may have different
2356 * meaning in future versions of NASM.
H. Peter Anvin3f87a2a2016-10-04 14:07:19 -07002357 */
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07002358 tline = tline->next;
2359 skip_white_(tline);
2360 tline = expand_smacro(tline);
2361 if (tok_type_(tline, TOK_ID)) {
2362 if (!nasm_stricmp(tline->text, "preproc")) {
2363 /* Preprocessor pragma */
2364 do_pragma_preproc(tline);
2365 } else {
2366 /* Build the assembler directive */
2367 t = new_Token(NULL, TOK_OTHER, "[", 1);
2368 t->next = new_Token(NULL, TOK_ID, "pragma", 6);
2369 t->next->next = new_Token(tline, TOK_WHITESPACE, NULL, 0);
2370 tline = t;
2371 for (t = tline; t->next; t = t->next)
2372 ;
2373 t->next = new_Token(NULL, TOK_OTHER, "]", 1);
2374 /* true here can be revisited in the future */
2375 *output = detoken(tline, true);
2376 }
2377 }
H. Peter Anvin3f87a2a2016-10-04 14:07:19 -07002378 free_tlist(origline);
2379 return DIRECTIVE_FOUND;
2380
H. Peter Anvine2c80182005-01-15 22:15:51 +00002381 case PP_STACKSIZE:
2382 /* Directive to tell NASM what the default stack size is. The
2383 * default is for a 16-bit stack, and this can be overriden with
2384 * %stacksize large.
H. Peter Anvine2c80182005-01-15 22:15:51 +00002385 */
2386 tline = tline->next;
2387 if (tline && tline->type == TOK_WHITESPACE)
2388 tline = tline->next;
2389 if (!tline || tline->type != TOK_ID) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002390 nasm_error(ERR_NONFATAL, "`%%stacksize' missing size parameter");
H. Peter Anvine2c80182005-01-15 22:15:51 +00002391 free_tlist(origline);
2392 return DIRECTIVE_FOUND;
2393 }
2394 if (nasm_stricmp(tline->text, "flat") == 0) {
2395 /* All subsequent ARG directives are for a 32-bit stack */
2396 StackSize = 4;
2397 StackPointer = "ebp";
2398 ArgOffset = 8;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002399 LocalOffset = 0;
Charles Crayne7eaf9192007-11-08 22:11:14 -08002400 } else if (nasm_stricmp(tline->text, "flat64") == 0) {
2401 /* All subsequent ARG directives are for a 64-bit stack */
2402 StackSize = 8;
2403 StackPointer = "rbp";
Per Jessen53252e02010-02-11 00:16:59 +03002404 ArgOffset = 16;
Charles Crayne7eaf9192007-11-08 22:11:14 -08002405 LocalOffset = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002406 } else if (nasm_stricmp(tline->text, "large") == 0) {
2407 /* All subsequent ARG directives are for a 16-bit stack,
2408 * far function call.
2409 */
2410 StackSize = 2;
2411 StackPointer = "bp";
2412 ArgOffset = 4;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002413 LocalOffset = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002414 } else if (nasm_stricmp(tline->text, "small") == 0) {
2415 /* All subsequent ARG directives are for a 16-bit stack,
2416 * far function call. We don't support near functions.
2417 */
2418 StackSize = 2;
2419 StackPointer = "bp";
2420 ArgOffset = 6;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002421 LocalOffset = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002422 } else {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002423 nasm_error(ERR_NONFATAL, "`%%stacksize' invalid size type");
H. Peter Anvine2c80182005-01-15 22:15:51 +00002424 free_tlist(origline);
2425 return DIRECTIVE_FOUND;
2426 }
2427 free_tlist(origline);
2428 return DIRECTIVE_FOUND;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002429
H. Peter Anvine2c80182005-01-15 22:15:51 +00002430 case PP_ARG:
2431 /* TASM like ARG directive to define arguments to functions, in
2432 * the following form:
2433 *
2434 * ARG arg1:WORD, arg2:DWORD, arg4:QWORD
2435 */
2436 offset = ArgOffset;
2437 do {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00002438 char *arg, directive[256];
H. Peter Anvine2c80182005-01-15 22:15:51 +00002439 int size = StackSize;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002440
H. Peter Anvine2c80182005-01-15 22:15:51 +00002441 /* Find the argument name */
2442 tline = tline->next;
2443 if (tline && tline->type == TOK_WHITESPACE)
2444 tline = tline->next;
2445 if (!tline || tline->type != TOK_ID) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002446 nasm_error(ERR_NONFATAL, "`%%arg' missing argument parameter");
H. Peter Anvine2c80182005-01-15 22:15:51 +00002447 free_tlist(origline);
2448 return DIRECTIVE_FOUND;
2449 }
2450 arg = tline->text;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002451
H. Peter Anvine2c80182005-01-15 22:15:51 +00002452 /* Find the argument size type */
2453 tline = tline->next;
2454 if (!tline || tline->type != TOK_OTHER
2455 || tline->text[0] != ':') {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002456 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00002457 "Syntax error processing `%%arg' directive");
2458 free_tlist(origline);
2459 return DIRECTIVE_FOUND;
2460 }
2461 tline = tline->next;
2462 if (!tline || tline->type != TOK_ID) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002463 nasm_error(ERR_NONFATAL, "`%%arg' missing size type parameter");
H. Peter Anvine2c80182005-01-15 22:15:51 +00002464 free_tlist(origline);
2465 return DIRECTIVE_FOUND;
2466 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002467
H. Peter Anvine2c80182005-01-15 22:15:51 +00002468 /* Allow macro expansion of type parameter */
Keith Kaniosb7a89542007-04-12 02:40:54 +00002469 tt = tokenize(tline->text);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002470 tt = expand_smacro(tt);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002471 size = parse_size(tt->text);
2472 if (!size) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002473 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00002474 "Invalid size type for `%%arg' missing directive");
2475 free_tlist(tt);
2476 free_tlist(origline);
2477 return DIRECTIVE_FOUND;
2478 }
2479 free_tlist(tt);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002480
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002481 /* Round up to even stack slots */
2482 size = ALIGN(size, StackSize);
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002483
H. Peter Anvine2c80182005-01-15 22:15:51 +00002484 /* Now define the macro for the argument */
2485 snprintf(directive, sizeof(directive), "%%define %s (%s+%d)",
2486 arg, StackPointer, offset);
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07002487 do_directive(tokenize(directive), output);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002488 offset += size;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002489
H. Peter Anvine2c80182005-01-15 22:15:51 +00002490 /* Move to the next argument in the list */
2491 tline = tline->next;
2492 if (tline && tline->type == TOK_WHITESPACE)
2493 tline = tline->next;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002494 } while (tline && tline->type == TOK_OTHER && tline->text[0] == ',');
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002495 ArgOffset = offset;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002496 free_tlist(origline);
2497 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002498
H. Peter Anvine2c80182005-01-15 22:15:51 +00002499 case PP_LOCAL:
2500 /* TASM like LOCAL directive to define local variables for a
2501 * function, in the following form:
2502 *
2503 * LOCAL local1:WORD, local2:DWORD, local4:QWORD = LocalSize
2504 *
2505 * The '= LocalSize' at the end is ignored by NASM, but is
2506 * required by TASM to define the local parameter size (and used
2507 * by the TASM macro package).
2508 */
2509 offset = LocalOffset;
2510 do {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00002511 char *local, directive[256];
H. Peter Anvine2c80182005-01-15 22:15:51 +00002512 int size = StackSize;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002513
H. Peter Anvine2c80182005-01-15 22:15:51 +00002514 /* Find the argument name */
2515 tline = tline->next;
2516 if (tline && tline->type == TOK_WHITESPACE)
2517 tline = tline->next;
2518 if (!tline || tline->type != TOK_ID) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002519 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00002520 "`%%local' missing argument parameter");
2521 free_tlist(origline);
2522 return DIRECTIVE_FOUND;
2523 }
2524 local = tline->text;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002525
H. Peter Anvine2c80182005-01-15 22:15:51 +00002526 /* Find the argument size type */
2527 tline = tline->next;
2528 if (!tline || tline->type != TOK_OTHER
2529 || tline->text[0] != ':') {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002530 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00002531 "Syntax error processing `%%local' directive");
2532 free_tlist(origline);
2533 return DIRECTIVE_FOUND;
2534 }
2535 tline = tline->next;
2536 if (!tline || tline->type != TOK_ID) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002537 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00002538 "`%%local' missing size type parameter");
2539 free_tlist(origline);
2540 return DIRECTIVE_FOUND;
2541 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00002542
H. Peter Anvine2c80182005-01-15 22:15:51 +00002543 /* Allow macro expansion of type parameter */
Keith Kaniosb7a89542007-04-12 02:40:54 +00002544 tt = tokenize(tline->text);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002545 tt = expand_smacro(tt);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002546 size = parse_size(tt->text);
2547 if (!size) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002548 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00002549 "Invalid size type for `%%local' missing directive");
2550 free_tlist(tt);
2551 free_tlist(origline);
2552 return DIRECTIVE_FOUND;
2553 }
2554 free_tlist(tt);
H. Peter Anvin734b1882002-04-30 21:01:08 +00002555
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002556 /* Round up to even stack slots */
2557 size = ALIGN(size, StackSize);
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002558
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002559 offset += size; /* Negative offset, increment before */
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002560
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002561 /* Now define the macro for the argument */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002562 snprintf(directive, sizeof(directive), "%%define %s (%s-%d)",
2563 local, StackPointer, offset);
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07002564 do_directive(tokenize(directive), output);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002565
H. Peter Anvine2c80182005-01-15 22:15:51 +00002566 /* Now define the assign to setup the enter_c macro correctly */
2567 snprintf(directive, sizeof(directive),
2568 "%%assign %%$localsize %%$localsize+%d", size);
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07002569 do_directive(tokenize(directive), output);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002570
H. Peter Anvine2c80182005-01-15 22:15:51 +00002571 /* Move to the next argument in the list */
2572 tline = tline->next;
2573 if (tline && tline->type == TOK_WHITESPACE)
2574 tline = tline->next;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002575 } while (tline && tline->type == TOK_OTHER && tline->text[0] == ',');
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002576 LocalOffset = offset;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002577 free_tlist(origline);
2578 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002579
H. Peter Anvine2c80182005-01-15 22:15:51 +00002580 case PP_CLEAR:
2581 if (tline->next)
H. Peter Anvin130736c2016-02-17 20:27:41 -08002582 nasm_error(ERR_WARNING|ERR_PASS1,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002583 "trailing garbage after `%%clear' ignored");
2584 free_macros();
2585 init_macros();
H. Peter Anvine2c80182005-01-15 22:15:51 +00002586 free_tlist(origline);
2587 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002588
H. Peter Anvin418ca702008-05-30 10:42:30 -07002589 case PP_DEPEND:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002590 t = tline->next = expand_smacro(tline->next);
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002591 skip_white_(t);
2592 if (!t || (t->type != TOK_STRING &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002593 t->type != TOK_INTERNAL_STRING)) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002594 nasm_error(ERR_NONFATAL, "`%%depend' expects a file name");
H. Peter Anvin418ca702008-05-30 10:42:30 -07002595 free_tlist(origline);
2596 return DIRECTIVE_FOUND; /* but we did _something_ */
2597 }
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002598 if (t->next)
H. Peter Anvin130736c2016-02-17 20:27:41 -08002599 nasm_error(ERR_WARNING|ERR_PASS1,
H. Peter Anvin418ca702008-05-30 10:42:30 -07002600 "trailing garbage after `%%depend' ignored");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002601 p = t->text;
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002602 if (t->type != TOK_INTERNAL_STRING)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002603 nasm_unquote_cstr(p, i);
H. Peter Anvin436e3672016-10-04 01:12:28 -07002604 nasm_add_string_to_strlist(dephead, p);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002605 free_tlist(origline);
H. Peter Anvin418ca702008-05-30 10:42:30 -07002606 return DIRECTIVE_FOUND;
2607
2608 case PP_INCLUDE:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002609 t = tline->next = expand_smacro(tline->next);
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002610 skip_white_(t);
H. Peter Anvind2456592008-06-19 15:04:18 -07002611
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002612 if (!t || (t->type != TOK_STRING &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002613 t->type != TOK_INTERNAL_STRING)) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002614 nasm_error(ERR_NONFATAL, "`%%include' expects a file name");
H. Peter Anvine2c80182005-01-15 22:15:51 +00002615 free_tlist(origline);
2616 return DIRECTIVE_FOUND; /* but we did _something_ */
2617 }
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002618 if (t->next)
H. Peter Anvin130736c2016-02-17 20:27:41 -08002619 nasm_error(ERR_WARNING|ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +00002620 "trailing garbage after `%%include' ignored");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002621 p = t->text;
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002622 if (t->type != TOK_INTERNAL_STRING)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002623 nasm_unquote_cstr(p, i);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002624 inc = nasm_malloc(sizeof(Include));
H. Peter Anvine2c80182005-01-15 22:15:51 +00002625 inc->next = istk;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002626 inc->conds = NULL;
Jim Kukunas65a8afc2016-06-13 16:00:42 -04002627 found_path = NULL;
H. Peter Anvin9924d1e2016-10-04 00:59:39 -07002628 inc->fp = inc_fopen(p, dephead, &found_path,
H. Peter Anvind81a2352016-09-21 14:03:18 -07002629 pass == 0 ? INC_OPTIONAL : INC_NEEDED, NF_TEXT);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002630 if (!inc->fp) {
2631 /* -MG given but file not found */
2632 nasm_free(inc);
2633 } else {
Jim Kukunas65a8afc2016-06-13 16:00:42 -04002634 inc->fname = src_set_fname(found_path ? found_path : p);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002635 inc->lineno = src_set_linnum(0);
2636 inc->lineinc = 1;
2637 inc->expansion = NULL;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002638 inc->mstk = NULL;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002639 istk = inc;
H. Peter Anvin8ac25aa2016-02-18 01:16:18 -08002640 lfmt->uplevel(LIST_INCLUDE);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002641 }
2642 free_tlist(origline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002643 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002644
H. Peter Anvind2456592008-06-19 15:04:18 -07002645 case PP_USE:
H. Peter Anvinf4ae5ad2008-06-19 18:39:24 -07002646 {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002647 static macros_t *use_pkg;
2648 const char *pkg_macro = NULL;
H. Peter Anvinf4ae5ad2008-06-19 18:39:24 -07002649
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002650 tline = tline->next;
2651 skip_white_(tline);
2652 tline = expand_id(tline);
H. Peter Anvind2456592008-06-19 15:04:18 -07002653
H. Peter Anvin264b7b92008-10-24 16:38:17 -07002654 if (!tline || (tline->type != TOK_STRING &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002655 tline->type != TOK_INTERNAL_STRING &&
2656 tline->type != TOK_ID)) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002657 nasm_error(ERR_NONFATAL, "`%%use' expects a package name");
H. Peter Anvind2456592008-06-19 15:04:18 -07002658 free_tlist(origline);
2659 return DIRECTIVE_FOUND; /* but we did _something_ */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002660 }
H. Peter Anvin264b7b92008-10-24 16:38:17 -07002661 if (tline->next)
H. Peter Anvin130736c2016-02-17 20:27:41 -08002662 nasm_error(ERR_WARNING|ERR_PASS1,
H. Peter Anvind2456592008-06-19 15:04:18 -07002663 "trailing garbage after `%%use' ignored");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002664 if (tline->type == TOK_STRING)
2665 nasm_unquote_cstr(tline->text, i);
2666 use_pkg = nasm_stdmac_find_package(tline->text);
2667 if (!use_pkg)
H. Peter Anvin130736c2016-02-17 20:27:41 -08002668 nasm_error(ERR_NONFATAL, "unknown `%%use' package: %s", tline->text);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002669 else
2670 pkg_macro = (char *)use_pkg + 1; /* The first string will be <%define>__USE_*__ */
Victor van den Elzen35eb2ea2010-03-10 22:33:48 +01002671 if (use_pkg && ! smacro_defined(NULL, pkg_macro, 0, NULL, true)) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002672 /* Not already included, go ahead and include it */
2673 stdmacpos = use_pkg;
2674 }
2675 free_tlist(origline);
H. Peter Anvind2456592008-06-19 15:04:18 -07002676 return DIRECTIVE_FOUND;
H. Peter Anvinf4ae5ad2008-06-19 18:39:24 -07002677 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002678 case PP_PUSH:
H. Peter Anvine2c80182005-01-15 22:15:51 +00002679 case PP_REPL:
H. Peter Anvin42b56392008-10-24 16:24:21 -07002680 case PP_POP:
H. Peter Anvine2c80182005-01-15 22:15:51 +00002681 tline = tline->next;
2682 skip_white_(tline);
2683 tline = expand_id(tline);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002684 if (tline) {
2685 if (!tok_type_(tline, TOK_ID)) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002686 nasm_error(ERR_NONFATAL, "`%s' expects a context identifier",
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002687 pp_directives[i]);
2688 free_tlist(origline);
2689 return DIRECTIVE_FOUND; /* but we did _something_ */
2690 }
2691 if (tline->next)
H. Peter Anvin130736c2016-02-17 20:27:41 -08002692 nasm_error(ERR_WARNING|ERR_PASS1,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002693 "trailing garbage after `%s' ignored",
2694 pp_directives[i]);
2695 p = nasm_strdup(tline->text);
2696 } else {
2697 p = NULL; /* Anonymous */
2698 }
H. Peter Anvin42b56392008-10-24 16:24:21 -07002699
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002700 if (i == PP_PUSH) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002701 ctx = nasm_malloc(sizeof(Context));
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002702 ctx->next = cstk;
2703 hash_init(&ctx->localmac, HASH_SMALL);
2704 ctx->name = p;
2705 ctx->number = unique++;
2706 cstk = ctx;
2707 } else {
2708 /* %pop or %repl */
2709 if (!cstk) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002710 nasm_error(ERR_NONFATAL, "`%s': context stack is empty",
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002711 pp_directives[i]);
2712 } else if (i == PP_POP) {
2713 if (p && (!cstk->name || nasm_stricmp(p, cstk->name)))
H. Peter Anvin130736c2016-02-17 20:27:41 -08002714 nasm_error(ERR_NONFATAL, "`%%pop' in wrong context: %s, "
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002715 "expected %s",
2716 cstk->name ? cstk->name : "anonymous", p);
2717 else
2718 ctx_pop();
2719 } else {
2720 /* i == PP_REPL */
2721 nasm_free(cstk->name);
2722 cstk->name = p;
2723 p = NULL;
2724 }
2725 nasm_free(p);
2726 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002727 free_tlist(origline);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002728 return DIRECTIVE_FOUND;
H. Peter Anvin8e3f75e2008-09-24 00:21:58 -07002729 case PP_FATAL:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002730 severity = ERR_FATAL;
2731 goto issue_error;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002732 case PP_ERROR:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002733 severity = ERR_NONFATAL;
2734 goto issue_error;
H. Peter Anvin7df04172008-06-10 18:27:38 -07002735 case PP_WARNING:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002736 severity = ERR_WARNING|ERR_WARN_USER;
2737 goto issue_error;
H. Peter Anvin8e3f75e2008-09-24 00:21:58 -07002738
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002739issue_error:
H. Peter Anvin7df04172008-06-10 18:27:38 -07002740 {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002741 /* Only error out if this is the final pass */
2742 if (pass != 2 && i != PP_FATAL)
2743 return DIRECTIVE_FOUND;
2744
2745 tline->next = expand_smacro(tline->next);
2746 tline = tline->next;
2747 skip_white_(tline);
2748 t = tline ? tline->next : NULL;
2749 skip_white_(t);
2750 if (tok_type_(tline, TOK_STRING) && !t) {
2751 /* The line contains only a quoted string */
2752 p = tline->text;
2753 nasm_unquote(p, NULL); /* Ignore NUL character truncation */
H. Peter Anvin130736c2016-02-17 20:27:41 -08002754 nasm_error(severity, "%s", p);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002755 } else {
2756 /* Not a quoted string, or more than a quoted string */
2757 p = detoken(tline, false);
H. Peter Anvin130736c2016-02-17 20:27:41 -08002758 nasm_error(severity, "%s", p);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002759 nasm_free(p);
2760 }
2761 free_tlist(origline);
2762 return DIRECTIVE_FOUND;
H. Peter Anvin7df04172008-06-10 18:27:38 -07002763 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00002764
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002765 CASE_PP_IF:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002766 if (istk->conds && !emitting(istk->conds->state))
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002767 j = COND_NEVER;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002768 else {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002769 j = if_condition(tline->next, i);
2770 tline->next = NULL; /* it got freed */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002771 j = j < 0 ? COND_NEVER : j ? COND_IF_TRUE : COND_IF_FALSE;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002772 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002773 cond = nasm_malloc(sizeof(Cond));
2774 cond->next = istk->conds;
2775 cond->state = j;
2776 istk->conds = cond;
2777 if(istk->mstk)
2778 istk->mstk->condcnt ++;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002779 free_tlist(origline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002780 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002781
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002782 CASE_PP_ELIF:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002783 if (!istk->conds)
H. Peter Anvin130736c2016-02-17 20:27:41 -08002784 nasm_error(ERR_FATAL, "`%s': no matching `%%if'", pp_directives[i]);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002785 switch(istk->conds->state) {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002786 case COND_IF_TRUE:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002787 istk->conds->state = COND_DONE;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002788 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002789
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002790 case COND_DONE:
2791 case COND_NEVER:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002792 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002793
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002794 case COND_ELSE_TRUE:
2795 case COND_ELSE_FALSE:
H. Peter Anvin130736c2016-02-17 20:27:41 -08002796 nasm_error(ERR_WARNING|ERR_PASS1|ERR_PP_PRECOND,
2797 "`%%elif' after `%%else' ignored");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002798 istk->conds->state = COND_NEVER;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002799 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002800
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002801 case COND_IF_FALSE:
2802 /*
2803 * IMPORTANT: In the case of %if, we will already have
2804 * called expand_mmac_params(); however, if we're
2805 * processing an %elif we must have been in a
2806 * non-emitting mode, which would have inhibited
2807 * the normal invocation of expand_mmac_params().
2808 * Therefore, we have to do it explicitly here.
2809 */
2810 j = if_condition(expand_mmac_params(tline->next), i);
2811 tline->next = NULL; /* it got freed */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002812 istk->conds->state =
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002813 j < 0 ? COND_NEVER : j ? COND_IF_TRUE : COND_IF_FALSE;
2814 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002815 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002816 free_tlist(origline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002817 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002818
H. Peter Anvine2c80182005-01-15 22:15:51 +00002819 case PP_ELSE:
2820 if (tline->next)
H. Peter Anvin130736c2016-02-17 20:27:41 -08002821 nasm_error(ERR_WARNING|ERR_PASS1|ERR_PP_PRECOND,
2822 "trailing garbage after `%%else' ignored");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002823 if (!istk->conds)
H. Peter Anvin130736c2016-02-17 20:27:41 -08002824 nasm_fatal(0, "`%%else: no matching `%%if'");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002825 switch(istk->conds->state) {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002826 case COND_IF_TRUE:
2827 case COND_DONE:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002828 istk->conds->state = COND_ELSE_FALSE;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002829 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002830
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002831 case COND_NEVER:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002832 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002833
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002834 case COND_IF_FALSE:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002835 istk->conds->state = COND_ELSE_TRUE;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002836 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002837
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002838 case COND_ELSE_TRUE:
2839 case COND_ELSE_FALSE:
H. Peter Anvin130736c2016-02-17 20:27:41 -08002840 nasm_error(ERR_WARNING|ERR_PASS1|ERR_PP_PRECOND,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002841 "`%%else' after `%%else' ignored.");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002842 istk->conds->state = COND_NEVER;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002843 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002844 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002845 free_tlist(origline);
2846 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002847
H. Peter Anvine2c80182005-01-15 22:15:51 +00002848 case PP_ENDIF:
2849 if (tline->next)
H. Peter Anvin130736c2016-02-17 20:27:41 -08002850 nasm_error(ERR_WARNING|ERR_PASS1|ERR_PP_PRECOND,
2851 "trailing garbage after `%%endif' ignored");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002852 if (!istk->conds)
H. Peter Anvin130736c2016-02-17 20:27:41 -08002853 nasm_error(ERR_FATAL, "`%%endif': no matching `%%if'");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002854 cond = istk->conds;
2855 istk->conds = cond->next;
2856 nasm_free(cond);
2857 if(istk->mstk)
2858 istk->mstk->condcnt --;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002859 free_tlist(origline);
2860 return DIRECTIVE_FOUND;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002861
H. Peter Anvindb8f96e2009-07-15 09:07:29 -04002862 case PP_RMACRO:
2863 case PP_IRMACRO:
H. Peter Anvine2c80182005-01-15 22:15:51 +00002864 case PP_MACRO:
2865 case PP_IMACRO:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002866 if (defining) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002867 nasm_error(ERR_FATAL, "`%s': already defining a macro",
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002868 pp_directives[i]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002869 return DIRECTIVE_FOUND;
2870 }
H. Peter Anvin4def1a82016-05-09 13:59:44 -07002871 defining = nasm_zalloc(sizeof(MMacro));
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002872 defining->max_depth =
2873 (i == PP_RMACRO) || (i == PP_IRMACRO) ? DEADMAN_LIMIT : 0;
2874 defining->casesense = (i == PP_MACRO) || (i == PP_RMACRO);
2875 if (!parse_mmacro_spec(tline, defining, pp_directives[i])) {
2876 nasm_free(defining);
2877 defining = NULL;
2878 return DIRECTIVE_FOUND;
2879 }
H. Peter Anvina26433d2008-07-16 14:40:01 -07002880
H. Peter Anvin4def1a82016-05-09 13:59:44 -07002881 src_get(&defining->xline, &defining->fname);
2882
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002883 mmac = (MMacro *) hash_findix(&mmacros, defining->name);
2884 while (mmac) {
2885 if (!strcmp(mmac->name, defining->name) &&
2886 (mmac->nparam_min <= defining->nparam_max
2887 || defining->plus)
2888 && (defining->nparam_min <= mmac->nparam_max
2889 || mmac->plus)) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002890 nasm_error(ERR_WARNING|ERR_PASS1,
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002891 "redefining multi-line macro `%s'", defining->name);
2892 return DIRECTIVE_FOUND;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002893 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002894 mmac = mmac->next;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002895 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002896 free_tlist(origline);
2897 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002898
H. Peter Anvine2c80182005-01-15 22:15:51 +00002899 case PP_ENDM:
2900 case PP_ENDMACRO:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002901 if (! (defining && defining->name)) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002902 nasm_error(ERR_NONFATAL, "`%s': not defining a macro", tline->text);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002903 return DIRECTIVE_FOUND;
2904 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002905 mmhead = (MMacro **) hash_findi_add(&mmacros, defining->name);
2906 defining->next = *mmhead;
2907 *mmhead = defining;
2908 defining = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002909 free_tlist(origline);
2910 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002911
H. Peter Anvin89cee572009-07-15 09:16:54 -04002912 case PP_EXITMACRO:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002913 /*
2914 * We must search along istk->expansion until we hit a
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002915 * macro-end marker for a macro with a name. Then we
2916 * bypass all lines between exitmacro and endmacro.
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002917 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002918 list_for_each(l, istk->expansion)
2919 if (l->finishes && l->finishes->name)
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002920 break;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002921
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002922 if (l) {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002923 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002924 * Remove all conditional entries relative to this
2925 * macro invocation. (safe to do in this context)
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002926 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002927 for ( ; l->finishes->condcnt > 0; l->finishes->condcnt --) {
2928 cond = istk->conds;
2929 istk->conds = cond->next;
2930 nasm_free(cond);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002931 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002932 istk->expansion = l;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002933 } else {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002934 nasm_error(ERR_NONFATAL, "`%%exitmacro' not within `%%macro' block");
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002935 }
Keith Kanios852f1ee2009-07-12 00:19:55 -05002936 free_tlist(origline);
2937 return DIRECTIVE_FOUND;
2938
H. Peter Anvina26433d2008-07-16 14:40:01 -07002939 case PP_UNMACRO:
2940 case PP_UNIMACRO:
2941 {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002942 MMacro **mmac_p;
2943 MMacro spec;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002944
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002945 spec.casesense = (i == PP_UNMACRO);
2946 if (!parse_mmacro_spec(tline, &spec, pp_directives[i])) {
2947 return DIRECTIVE_FOUND;
2948 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002949 mmac_p = (MMacro **) hash_findi(&mmacros, spec.name, NULL);
2950 while (mmac_p && *mmac_p) {
2951 mmac = *mmac_p;
2952 if (mmac->casesense == spec.casesense &&
2953 !mstrcmp(mmac->name, spec.name, spec.casesense) &&
2954 mmac->nparam_min == spec.nparam_min &&
2955 mmac->nparam_max == spec.nparam_max &&
2956 mmac->plus == spec.plus) {
2957 *mmac_p = mmac->next;
2958 free_mmacro(mmac);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002959 } else {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002960 mmac_p = &mmac->next;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002961 }
2962 }
2963 free_tlist(origline);
2964 free_tlist(spec.dlist);
2965 return DIRECTIVE_FOUND;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002966 }
2967
H. Peter Anvine2c80182005-01-15 22:15:51 +00002968 case PP_ROTATE:
2969 if (tline->next && tline->next->type == TOK_WHITESPACE)
2970 tline = tline->next;
H. Peter Anvin89cee572009-07-15 09:16:54 -04002971 if (!tline->next) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002972 free_tlist(origline);
H. Peter Anvin130736c2016-02-17 20:27:41 -08002973 nasm_error(ERR_NONFATAL, "`%%rotate' missing rotate count");
H. Peter Anvine2c80182005-01-15 22:15:51 +00002974 return DIRECTIVE_FOUND;
2975 }
2976 t = expand_smacro(tline->next);
2977 tline->next = NULL;
2978 free_tlist(origline);
2979 tline = t;
2980 tptr = &t;
2981 tokval.t_type = TOKEN_INVALID;
2982 evalresult =
H. Peter Anvin130736c2016-02-17 20:27:41 -08002983 evaluate(ppscan, tptr, &tokval, NULL, pass, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002984 free_tlist(tline);
2985 if (!evalresult)
2986 return DIRECTIVE_FOUND;
2987 if (tokval.t_type)
H. Peter Anvin130736c2016-02-17 20:27:41 -08002988 nasm_error(ERR_WARNING|ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +00002989 "trailing garbage after expression ignored");
2990 if (!is_simple(evalresult)) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002991 nasm_error(ERR_NONFATAL, "non-constant value given to `%%rotate'");
H. Peter Anvine2c80182005-01-15 22:15:51 +00002992 return DIRECTIVE_FOUND;
2993 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002994 mmac = istk->mstk;
2995 while (mmac && !mmac->name) /* avoid mistaking %reps for macros */
2996 mmac = mmac->next_active;
2997 if (!mmac) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002998 nasm_error(ERR_NONFATAL, "`%%rotate' invoked outside a macro call");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002999 } else if (mmac->nparam == 0) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08003000 nasm_error(ERR_NONFATAL,
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003001 "`%%rotate' invoked within macro without parameters");
3002 } else {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003003 int rotate = mmac->rotate + reloc_value(evalresult);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003004
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003005 rotate %= (int)mmac->nparam;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003006 if (rotate < 0)
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003007 rotate += mmac->nparam;
3008
3009 mmac->rotate = rotate;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003010 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00003011 return DIRECTIVE_FOUND;
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00003012
H. Peter Anvine2c80182005-01-15 22:15:51 +00003013 case PP_REP:
H. Peter Anvin6867acc2007-10-10 14:58:45 -07003014 nolist = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003015 do {
3016 tline = tline->next;
3017 } while (tok_type_(tline, TOK_WHITESPACE));
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00003018
H. Peter Anvine2c80182005-01-15 22:15:51 +00003019 if (tok_type_(tline, TOK_ID) &&
3020 nasm_stricmp(tline->text, ".nolist") == 0) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07003021 nolist = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003022 do {
3023 tline = tline->next;
3024 } while (tok_type_(tline, TOK_WHITESPACE));
3025 }
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00003026
H. Peter Anvine2c80182005-01-15 22:15:51 +00003027 if (tline) {
3028 t = expand_smacro(tline);
3029 tptr = &t;
3030 tokval.t_type = TOKEN_INVALID;
3031 evalresult =
H. Peter Anvin130736c2016-02-17 20:27:41 -08003032 evaluate(ppscan, tptr, &tokval, NULL, pass, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003033 if (!evalresult) {
3034 free_tlist(origline);
3035 return DIRECTIVE_FOUND;
3036 }
3037 if (tokval.t_type)
H. Peter Anvin130736c2016-02-17 20:27:41 -08003038 nasm_error(ERR_WARNING|ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +00003039 "trailing garbage after expression ignored");
3040 if (!is_simple(evalresult)) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08003041 nasm_error(ERR_NONFATAL, "non-constant value given to `%%rep'");
H. Peter Anvine2c80182005-01-15 22:15:51 +00003042 return DIRECTIVE_FOUND;
3043 }
Cyrill Gorcunove091d6e2010-08-09 13:58:22 +04003044 count = reloc_value(evalresult);
3045 if (count >= REP_LIMIT) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08003046 nasm_error(ERR_NONFATAL, "`%%rep' value exceeds limit");
Cyrill Gorcunove091d6e2010-08-09 13:58:22 +04003047 count = 0;
3048 } else
3049 count++;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003050 } else {
H. Peter Anvin130736c2016-02-17 20:27:41 -08003051 nasm_error(ERR_NONFATAL, "`%%rep' expects a repeat count");
H. Peter Anvinf8ba53e2007-10-11 10:11:57 -07003052 count = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003053 }
3054 free_tlist(origline);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003055
3056 tmp_defining = defining;
3057 defining = nasm_malloc(sizeof(MMacro));
3058 defining->prev = NULL;
3059 defining->name = NULL; /* flags this macro as a %rep block */
3060 defining->casesense = false;
3061 defining->plus = false;
3062 defining->nolist = nolist;
3063 defining->in_progress = count;
3064 defining->max_depth = 0;
3065 defining->nparam_min = defining->nparam_max = 0;
3066 defining->defaults = NULL;
3067 defining->dlist = NULL;
3068 defining->expansion = NULL;
3069 defining->next_active = istk->mstk;
3070 defining->rep_nest = tmp_defining;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003071 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003072
H. Peter Anvine2c80182005-01-15 22:15:51 +00003073 case PP_ENDREP:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003074 if (!defining || defining->name) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08003075 nasm_error(ERR_NONFATAL, "`%%endrep': no matching `%%rep'");
H. Peter Anvine2c80182005-01-15 22:15:51 +00003076 return DIRECTIVE_FOUND;
3077 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003078
H. Peter Anvine2c80182005-01-15 22:15:51 +00003079 /*
3080 * Now we have a "macro" defined - although it has no name
3081 * and we won't be entering it in the hash tables - we must
3082 * push a macro-end marker for it on to istk->expansion.
3083 * After that, it will take care of propagating itself (a
3084 * macro-end marker line for a macro which is really a %rep
3085 * block will cause the macro to be re-expanded, complete
3086 * with another macro-end marker to ensure the process
3087 * continues) until the whole expansion is forcibly removed
3088 * from istk->expansion by a %exitrep.
3089 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003090 l = nasm_malloc(sizeof(Line));
3091 l->next = istk->expansion;
3092 l->finishes = defining;
3093 l->first = NULL;
3094 istk->expansion = l;
3095
3096 istk->mstk = defining;
3097
H. Peter Anvin8ac25aa2016-02-18 01:16:18 -08003098 lfmt->uplevel(defining->nolist ? LIST_MACRO_NOLIST : LIST_MACRO);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003099 tmp_defining = defining;
3100 defining = defining->rep_nest;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003101 free_tlist(origline);
3102 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003103
H. Peter Anvine2c80182005-01-15 22:15:51 +00003104 case PP_EXITREP:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003105 /*
3106 * We must search along istk->expansion until we hit a
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003107 * macro-end marker for a macro with no name. Then we set
3108 * its `in_progress' flag to 0.
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003109 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003110 list_for_each(l, istk->expansion)
3111 if (l->finishes && !l->finishes->name)
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003112 break;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003113
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003114 if (l)
3115 l->finishes->in_progress = 1;
3116 else
H. Peter Anvin130736c2016-02-17 20:27:41 -08003117 nasm_error(ERR_NONFATAL, "`%%exitrep' not within `%%rep' block");
H. Peter Anvine2c80182005-01-15 22:15:51 +00003118 free_tlist(origline);
3119 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003120
H. Peter Anvine2c80182005-01-15 22:15:51 +00003121 case PP_XDEFINE:
3122 case PP_IXDEFINE:
3123 case PP_DEFINE:
3124 case PP_IDEFINE:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003125 casesense = (i == PP_DEFINE || i == PP_XDEFINE);
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07003126
H. Peter Anvine2c80182005-01-15 22:15:51 +00003127 tline = tline->next;
3128 skip_white_(tline);
3129 tline = expand_id(tline);
3130 if (!tline || (tline->type != TOK_ID &&
3131 (tline->type != TOK_PREPROC_ID ||
3132 tline->text[1] != '$'))) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08003133 nasm_error(ERR_NONFATAL, "`%s' expects a macro identifier",
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003134 pp_directives[i]);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003135 free_tlist(origline);
3136 return DIRECTIVE_FOUND;
3137 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003138
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04003139 ctx = get_ctx(tline->text, &mname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003140 last = tline;
3141 param_start = tline = tline->next;
3142 nparam = 0;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003143
H. Peter Anvine2c80182005-01-15 22:15:51 +00003144 /* Expand the macro definition now for %xdefine and %ixdefine */
3145 if ((i == PP_XDEFINE) || (i == PP_IXDEFINE))
3146 tline = expand_smacro(tline);
H. Peter Anvin734b1882002-04-30 21:01:08 +00003147
H. Peter Anvine2c80182005-01-15 22:15:51 +00003148 if (tok_is_(tline, "(")) {
3149 /*
3150 * This macro has parameters.
3151 */
H. Peter Anvin734b1882002-04-30 21:01:08 +00003152
H. Peter Anvine2c80182005-01-15 22:15:51 +00003153 tline = tline->next;
3154 while (1) {
3155 skip_white_(tline);
3156 if (!tline) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08003157 nasm_error(ERR_NONFATAL, "parameter identifier expected");
H. Peter Anvine2c80182005-01-15 22:15:51 +00003158 free_tlist(origline);
3159 return DIRECTIVE_FOUND;
3160 }
3161 if (tline->type != TOK_ID) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08003162 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00003163 "`%s': parameter identifier expected",
3164 tline->text);
3165 free_tlist(origline);
3166 return DIRECTIVE_FOUND;
3167 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003168 tline->type = TOK_SMAC_PARAM + nparam++;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003169 tline = tline->next;
3170 skip_white_(tline);
3171 if (tok_is_(tline, ",")) {
3172 tline = tline->next;
H. Peter Anvinca348b62008-07-23 10:49:26 -04003173 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003174 if (!tok_is_(tline, ")")) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08003175 nasm_error(ERR_NONFATAL,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003176 "`)' expected to terminate macro template");
3177 free_tlist(origline);
3178 return DIRECTIVE_FOUND;
3179 }
3180 break;
3181 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00003182 }
3183 last = tline;
3184 tline = tline->next;
3185 }
3186 if (tok_type_(tline, TOK_WHITESPACE))
3187 last = tline, tline = tline->next;
3188 macro_start = NULL;
3189 last->next = NULL;
3190 t = tline;
3191 while (t) {
3192 if (t->type == TOK_ID) {
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04003193 list_for_each(tt, param_start)
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003194 if (tt->type >= TOK_SMAC_PARAM &&
H. Peter Anvine2c80182005-01-15 22:15:51 +00003195 !strcmp(tt->text, t->text))
3196 t->type = tt->type;
3197 }
3198 tt = t->next;
3199 t->next = macro_start;
3200 macro_start = t;
3201 t = tt;
3202 }
3203 /*
3204 * Good. We now have a macro name, a parameter count, and a
3205 * token list (in reverse order) for an expansion. We ought
3206 * to be OK just to create an SMacro, store it, and let
3207 * free_tlist have the rest of the line (which we have
3208 * carefully re-terminated after chopping off the expansion
3209 * from the end).
3210 */
H. Peter Anvin4db5a162007-10-11 13:42:09 -07003211 define_smacro(ctx, mname, casesense, nparam, macro_start);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003212 free_tlist(origline);
3213 return DIRECTIVE_FOUND;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003214
H. Peter Anvine2c80182005-01-15 22:15:51 +00003215 case PP_UNDEF:
3216 tline = tline->next;
3217 skip_white_(tline);
3218 tline = expand_id(tline);
3219 if (!tline || (tline->type != TOK_ID &&
3220 (tline->type != TOK_PREPROC_ID ||
3221 tline->text[1] != '$'))) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08003222 nasm_error(ERR_NONFATAL, "`%%undef' expects a macro identifier");
H. Peter Anvine2c80182005-01-15 22:15:51 +00003223 free_tlist(origline);
3224 return DIRECTIVE_FOUND;
3225 }
3226 if (tline->next) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08003227 nasm_error(ERR_WARNING|ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +00003228 "trailing garbage after macro name ignored");
3229 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003230
H. Peter Anvine2c80182005-01-15 22:15:51 +00003231 /* Find the context that symbol belongs to */
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04003232 ctx = get_ctx(tline->text, &mname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003233 undef_smacro(ctx, mname);
3234 free_tlist(origline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003235 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003236
H. Peter Anvin9e200162008-06-04 17:23:14 -07003237 case PP_DEFSTR:
3238 case PP_IDEFSTR:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003239 casesense = (i == PP_DEFSTR);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003240
3241 tline = tline->next;
3242 skip_white_(tline);
3243 tline = expand_id(tline);
3244 if (!tline || (tline->type != TOK_ID &&
3245 (tline->type != TOK_PREPROC_ID ||
3246 tline->text[1] != '$'))) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08003247 nasm_error(ERR_NONFATAL, "`%s' expects a macro identifier",
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003248 pp_directives[i]);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003249 free_tlist(origline);
3250 return DIRECTIVE_FOUND;
3251 }
3252
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04003253 ctx = get_ctx(tline->text, &mname);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003254 last = tline;
3255 tline = expand_smacro(tline->next);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003256 last->next = NULL;
H. Peter Anvin9e200162008-06-04 17:23:14 -07003257
3258 while (tok_type_(tline, TOK_WHITESPACE))
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003259 tline = delete_Token(tline);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003260
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003261 p = detoken(tline, false);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003262 macro_start = nasm_malloc(sizeof(*macro_start));
3263 macro_start->next = NULL;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003264 macro_start->text = nasm_quote(p, strlen(p));
3265 macro_start->type = TOK_STRING;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003266 macro_start->a.mac = NULL;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003267 nasm_free(p);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003268
3269 /*
3270 * We now have a macro name, an implicit parameter count of
3271 * zero, and a string token to use as an expansion. Create
3272 * and store an SMacro.
3273 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003274 define_smacro(ctx, mname, casesense, 0, macro_start);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003275 free_tlist(origline);
3276 return DIRECTIVE_FOUND;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003277
H. Peter Anvin2f55bda2009-07-14 15:04:04 -04003278 case PP_DEFTOK:
3279 case PP_IDEFTOK:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003280 casesense = (i == PP_DEFTOK);
3281
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003282 tline = tline->next;
3283 skip_white_(tline);
3284 tline = expand_id(tline);
3285 if (!tline || (tline->type != TOK_ID &&
3286 (tline->type != TOK_PREPROC_ID ||
3287 tline->text[1] != '$'))) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08003288 nasm_error(ERR_NONFATAL,
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003289 "`%s' expects a macro identifier as first parameter",
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003290 pp_directives[i]);
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003291 free_tlist(origline);
3292 return DIRECTIVE_FOUND;
3293 }
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04003294 ctx = get_ctx(tline->text, &mname);
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003295 last = tline;
3296 tline = expand_smacro(tline->next);
3297 last->next = NULL;
3298
3299 t = tline;
3300 while (tok_type_(t, TOK_WHITESPACE))
3301 t = t->next;
3302 /* t should now point to the string */
Cyrill Gorcunov6908e582010-09-06 19:36:15 +04003303 if (!tok_type_(t, TOK_STRING)) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08003304 nasm_error(ERR_NONFATAL,
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003305 "`%s` requires string as second parameter",
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003306 pp_directives[i]);
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003307 free_tlist(tline);
3308 free_tlist(origline);
3309 return DIRECTIVE_FOUND;
3310 }
3311
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04003312 /*
3313 * Convert the string to a token stream. Note that smacros
3314 * are stored with the token stream reversed, so we have to
3315 * reverse the output of tokenize().
3316 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003317 nasm_unquote_cstr(t->text, i);
H. Peter Anvinb40992c2010-09-15 08:57:21 -07003318 macro_start = reverse_tokens(tokenize(t->text));
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003319
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003320 /*
3321 * We now have a macro name, an implicit parameter count of
3322 * zero, and a numeric token to use as an expansion. Create
3323 * and store an SMacro.
3324 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003325 define_smacro(ctx, mname, casesense, 0, macro_start);
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003326 free_tlist(tline);
3327 free_tlist(origline);
3328 return DIRECTIVE_FOUND;
H. Peter Anvin9e200162008-06-04 17:23:14 -07003329
H. Peter Anvin418ca702008-05-30 10:42:30 -07003330 case PP_PATHSEARCH:
3331 {
H. Peter Anvinccad6f92016-10-04 00:34:35 -07003332 const char *found_path;
H. Peter Anvin418ca702008-05-30 10:42:30 -07003333
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003334 casesense = true;
H. Peter Anvin418ca702008-05-30 10:42:30 -07003335
3336 tline = tline->next;
3337 skip_white_(tline);
3338 tline = expand_id(tline);
3339 if (!tline || (tline->type != TOK_ID &&
3340 (tline->type != TOK_PREPROC_ID ||
3341 tline->text[1] != '$'))) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08003342 nasm_error(ERR_NONFATAL,
H. Peter Anvin418ca702008-05-30 10:42:30 -07003343 "`%%pathsearch' expects a macro identifier as first parameter");
3344 free_tlist(origline);
3345 return DIRECTIVE_FOUND;
3346 }
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04003347 ctx = get_ctx(tline->text, &mname);
H. Peter Anvin418ca702008-05-30 10:42:30 -07003348 last = tline;
3349 tline = expand_smacro(tline->next);
3350 last->next = NULL;
3351
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003352 t = tline;
H. Peter Anvin418ca702008-05-30 10:42:30 -07003353 while (tok_type_(t, TOK_WHITESPACE))
3354 t = t->next;
3355
3356 if (!t || (t->type != TOK_STRING &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003357 t->type != TOK_INTERNAL_STRING)) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08003358 nasm_error(ERR_NONFATAL, "`%%pathsearch' expects a file name");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003359 free_tlist(tline);
H. Peter Anvin418ca702008-05-30 10:42:30 -07003360 free_tlist(origline);
3361 return DIRECTIVE_FOUND; /* but we did _something_ */
3362 }
3363 if (t->next)
H. Peter Anvin130736c2016-02-17 20:27:41 -08003364 nasm_error(ERR_WARNING|ERR_PASS1,
H. Peter Anvin418ca702008-05-30 10:42:30 -07003365 "trailing garbage after `%%pathsearch' ignored");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003366 p = t->text;
H. Peter Anvin427cc912008-06-01 21:43:03 -07003367 if (t->type != TOK_INTERNAL_STRING)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003368 nasm_unquote(p, NULL);
H. Peter Anvin418ca702008-05-30 10:42:30 -07003369
H. Peter Anvin9924d1e2016-10-04 00:59:39 -07003370 inc_fopen(p, NULL, &found_path, INC_PROBE, NF_BINARY);
H. Peter Anvinccad6f92016-10-04 00:34:35 -07003371 if (!found_path)
3372 found_path = p;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003373 macro_start = nasm_malloc(sizeof(*macro_start));
3374 macro_start->next = NULL;
H. Peter Anvinccad6f92016-10-04 00:34:35 -07003375 macro_start->text = nasm_quote(found_path, strlen(found_path));
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003376 macro_start->type = TOK_STRING;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003377 macro_start->a.mac = NULL;
H. Peter Anvin418ca702008-05-30 10:42:30 -07003378
3379 /*
3380 * We now have a macro name, an implicit parameter count of
3381 * zero, and a string token to use as an expansion. Create
3382 * and store an SMacro.
3383 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003384 define_smacro(ctx, mname, casesense, 0, macro_start);
3385 free_tlist(tline);
H. Peter Anvin418ca702008-05-30 10:42:30 -07003386 free_tlist(origline);
3387 return DIRECTIVE_FOUND;
3388 }
3389
H. Peter Anvine2c80182005-01-15 22:15:51 +00003390 case PP_STRLEN:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003391 casesense = true;
H. Peter Anvin70653092007-10-19 14:42:29 -07003392
H. Peter Anvine2c80182005-01-15 22:15:51 +00003393 tline = tline->next;
3394 skip_white_(tline);
3395 tline = expand_id(tline);
3396 if (!tline || (tline->type != TOK_ID &&
3397 (tline->type != TOK_PREPROC_ID ||
3398 tline->text[1] != '$'))) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08003399 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00003400 "`%%strlen' expects a macro identifier as first parameter");
3401 free_tlist(origline);
3402 return DIRECTIVE_FOUND;
3403 }
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04003404 ctx = get_ctx(tline->text, &mname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003405 last = tline;
3406 tline = expand_smacro(tline->next);
3407 last->next = NULL;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003408
H. Peter Anvine2c80182005-01-15 22:15:51 +00003409 t = tline;
3410 while (tok_type_(t, TOK_WHITESPACE))
3411 t = t->next;
3412 /* t should now point to the string */
Cyrill Gorcunov4e1d5ab2010-07-23 18:51:51 +04003413 if (!tok_type_(t, TOK_STRING)) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08003414 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00003415 "`%%strlen` requires string as second parameter");
3416 free_tlist(tline);
3417 free_tlist(origline);
3418 return DIRECTIVE_FOUND;
3419 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003420
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003421 macro_start = nasm_malloc(sizeof(*macro_start));
3422 macro_start->next = NULL;
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07003423 make_tok_num(macro_start, nasm_unquote(t->text, NULL));
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003424 macro_start->a.mac = NULL;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003425
H. Peter Anvine2c80182005-01-15 22:15:51 +00003426 /*
3427 * We now have a macro name, an implicit parameter count of
3428 * zero, and a numeric token to use as an expansion. Create
3429 * and store an SMacro.
3430 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003431 define_smacro(ctx, mname, casesense, 0, macro_start);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003432 free_tlist(tline);
3433 free_tlist(origline);
3434 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003435
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003436 case PP_STRCAT:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003437 casesense = true;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003438
3439 tline = tline->next;
3440 skip_white_(tline);
3441 tline = expand_id(tline);
3442 if (!tline || (tline->type != TOK_ID &&
3443 (tline->type != TOK_PREPROC_ID ||
3444 tline->text[1] != '$'))) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08003445 nasm_error(ERR_NONFATAL,
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003446 "`%%strcat' expects a macro identifier as first parameter");
3447 free_tlist(origline);
3448 return DIRECTIVE_FOUND;
3449 }
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04003450 ctx = get_ctx(tline->text, &mname);
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003451 last = tline;
3452 tline = expand_smacro(tline->next);
3453 last->next = NULL;
3454
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003455 len = 0;
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04003456 list_for_each(t, tline) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003457 switch (t->type) {
3458 case TOK_WHITESPACE:
3459 break;
3460 case TOK_STRING:
3461 len += t->a.len = nasm_unquote(t->text, NULL);
3462 break;
3463 case TOK_OTHER:
3464 if (!strcmp(t->text, ",")) /* permit comma separators */
3465 break;
3466 /* else fall through */
3467 default:
H. Peter Anvin130736c2016-02-17 20:27:41 -08003468 nasm_error(ERR_NONFATAL,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003469 "non-string passed to `%%strcat' (%d)", t->type);
3470 free_tlist(tline);
3471 free_tlist(origline);
3472 return DIRECTIVE_FOUND;
3473 }
3474 }
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003475
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003476 p = pp = nasm_malloc(len);
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04003477 list_for_each(t, tline) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003478 if (t->type == TOK_STRING) {
3479 memcpy(p, t->text, t->a.len);
3480 p += t->a.len;
3481 }
3482 }
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003483
3484 /*
3485 * We now have a macro name, an implicit parameter count of
3486 * zero, and a numeric token to use as an expansion. Create
3487 * and store an SMacro.
3488 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003489 macro_start = new_Token(NULL, TOK_STRING, NULL, 0);
3490 macro_start->text = nasm_quote(pp, len);
3491 nasm_free(pp);
3492 define_smacro(ctx, mname, casesense, 0, macro_start);
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003493 free_tlist(tline);
3494 free_tlist(origline);
3495 return DIRECTIVE_FOUND;
3496
H. Peter Anvine2c80182005-01-15 22:15:51 +00003497 case PP_SUBSTR:
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003498 {
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003499 int64_t start, count;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003500 size_t len;
H. Peter Anvind2456592008-06-19 15:04:18 -07003501
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003502 casesense = true;
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07003503
H. Peter Anvine2c80182005-01-15 22:15:51 +00003504 tline = tline->next;
3505 skip_white_(tline);
3506 tline = expand_id(tline);
3507 if (!tline || (tline->type != TOK_ID &&
3508 (tline->type != TOK_PREPROC_ID ||
3509 tline->text[1] != '$'))) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08003510 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00003511 "`%%substr' expects a macro identifier as first parameter");
3512 free_tlist(origline);
3513 return DIRECTIVE_FOUND;
3514 }
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04003515 ctx = get_ctx(tline->text, &mname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003516 last = tline;
3517 tline = expand_smacro(tline->next);
3518 last->next = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003519
Cyrill Gorcunov35519d62010-09-06 23:49:52 +04003520 if (tline) /* skip expanded id */
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003521 t = tline->next;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003522 while (tok_type_(t, TOK_WHITESPACE))
3523 t = t->next;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003524
H. Peter Anvine2c80182005-01-15 22:15:51 +00003525 /* t should now point to the string */
Cyrill Gorcunov35519d62010-09-06 23:49:52 +04003526 if (!tok_type_(t, TOK_STRING)) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08003527 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00003528 "`%%substr` requires string as second parameter");
3529 free_tlist(tline);
3530 free_tlist(origline);
3531 return DIRECTIVE_FOUND;
3532 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003533
H. Peter Anvine2c80182005-01-15 22:15:51 +00003534 tt = t->next;
3535 tptr = &tt;
3536 tokval.t_type = TOKEN_INVALID;
H. Peter Anvin130736c2016-02-17 20:27:41 -08003537 evalresult = evaluate(ppscan, tptr, &tokval, NULL, pass, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003538 if (!evalresult) {
3539 free_tlist(tline);
3540 free_tlist(origline);
3541 return DIRECTIVE_FOUND;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003542 } else if (!is_simple(evalresult)) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08003543 nasm_error(ERR_NONFATAL, "non-constant value given to `%%substr`");
H. Peter Anvine2c80182005-01-15 22:15:51 +00003544 free_tlist(tline);
3545 free_tlist(origline);
3546 return DIRECTIVE_FOUND;
3547 }
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003548 start = evalresult->value - 1;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003549
3550 while (tok_type_(tt, TOK_WHITESPACE))
3551 tt = tt->next;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003552 if (!tt) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003553 count = 1; /* Backwards compatibility: one character */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003554 } else {
3555 tokval.t_type = TOKEN_INVALID;
H. Peter Anvin130736c2016-02-17 20:27:41 -08003556 evalresult = evaluate(ppscan, tptr, &tokval, NULL, pass, NULL);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003557 if (!evalresult) {
3558 free_tlist(tline);
3559 free_tlist(origline);
3560 return DIRECTIVE_FOUND;
3561 } else if (!is_simple(evalresult)) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08003562 nasm_error(ERR_NONFATAL, "non-constant value given to `%%substr`");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003563 free_tlist(tline);
3564 free_tlist(origline);
3565 return DIRECTIVE_FOUND;
3566 }
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003567 count = evalresult->value;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003568 }
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003569
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003570 len = nasm_unquote(t->text, NULL);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003571
Cyrill Gorcunovcff031e2010-09-07 20:31:11 +04003572 /* make start and count being in range */
3573 if (start < 0)
3574 start = 0;
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003575 if (count < 0)
3576 count = len + count + 1 - start;
3577 if (start + count > (int64_t)len)
Cyrill Gorcunovcff031e2010-09-07 20:31:11 +04003578 count = len - start;
3579 if (!len || count < 0 || start >=(int64_t)len)
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003580 start = -1, count = 0; /* empty string */
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003581
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003582 macro_start = nasm_malloc(sizeof(*macro_start));
3583 macro_start->next = NULL;
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003584 macro_start->text = nasm_quote((start < 0) ? "" : t->text + start, count);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003585 macro_start->type = TOK_STRING;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003586 macro_start->a.mac = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003587
H. Peter Anvine2c80182005-01-15 22:15:51 +00003588 /*
3589 * We now have a macro name, an implicit parameter count of
3590 * zero, and a numeric token to use as an expansion. Create
3591 * and store an SMacro.
3592 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003593 define_smacro(ctx, mname, casesense, 0, macro_start);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003594 free_tlist(tline);
3595 free_tlist(origline);
3596 return DIRECTIVE_FOUND;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003597 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003598
H. Peter Anvine2c80182005-01-15 22:15:51 +00003599 case PP_ASSIGN:
3600 case PP_IASSIGN:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003601 casesense = (i == PP_ASSIGN);
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07003602
H. Peter Anvine2c80182005-01-15 22:15:51 +00003603 tline = tline->next;
3604 skip_white_(tline);
3605 tline = expand_id(tline);
3606 if (!tline || (tline->type != TOK_ID &&
3607 (tline->type != TOK_PREPROC_ID ||
3608 tline->text[1] != '$'))) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08003609 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00003610 "`%%%sassign' expects a macro identifier",
3611 (i == PP_IASSIGN ? "i" : ""));
3612 free_tlist(origline);
3613 return DIRECTIVE_FOUND;
3614 }
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04003615 ctx = get_ctx(tline->text, &mname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003616 last = tline;
3617 tline = expand_smacro(tline->next);
3618 last->next = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003619
H. Peter Anvine2c80182005-01-15 22:15:51 +00003620 t = tline;
3621 tptr = &t;
3622 tokval.t_type = TOKEN_INVALID;
H. Peter Anvin130736c2016-02-17 20:27:41 -08003623 evalresult = evaluate(ppscan, tptr, &tokval, NULL, pass, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003624 free_tlist(tline);
3625 if (!evalresult) {
3626 free_tlist(origline);
3627 return DIRECTIVE_FOUND;
3628 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003629
H. Peter Anvine2c80182005-01-15 22:15:51 +00003630 if (tokval.t_type)
H. Peter Anvin130736c2016-02-17 20:27:41 -08003631 nasm_error(ERR_WARNING|ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +00003632 "trailing garbage after expression ignored");
H. Peter Anvin734b1882002-04-30 21:01:08 +00003633
H. Peter Anvine2c80182005-01-15 22:15:51 +00003634 if (!is_simple(evalresult)) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08003635 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00003636 "non-constant value given to `%%%sassign'",
3637 (i == PP_IASSIGN ? "i" : ""));
3638 free_tlist(origline);
3639 return DIRECTIVE_FOUND;
3640 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003641
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003642 macro_start = nasm_malloc(sizeof(*macro_start));
3643 macro_start->next = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003644 make_tok_num(macro_start, reloc_value(evalresult));
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003645 macro_start->a.mac = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003646
H. Peter Anvine2c80182005-01-15 22:15:51 +00003647 /*
3648 * We now have a macro name, an implicit parameter count of
3649 * zero, and a numeric token to use as an expansion. Create
3650 * and store an SMacro.
3651 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003652 define_smacro(ctx, mname, casesense, 0, macro_start);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003653 free_tlist(origline);
3654 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003655
H. Peter Anvine2c80182005-01-15 22:15:51 +00003656 case PP_LINE:
3657 /*
3658 * Syntax is `%line nnn[+mmm] [filename]'
3659 */
3660 tline = tline->next;
3661 skip_white_(tline);
3662 if (!tok_type_(tline, TOK_NUMBER)) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08003663 nasm_error(ERR_NONFATAL, "`%%line' expects line number");
H. Peter Anvine2c80182005-01-15 22:15:51 +00003664 free_tlist(origline);
3665 return DIRECTIVE_FOUND;
3666 }
H. Peter Anvin70055962007-10-11 00:05:31 -07003667 k = readnum(tline->text, &err);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003668 m = 1;
3669 tline = tline->next;
3670 if (tok_is_(tline, "+")) {
3671 tline = tline->next;
3672 if (!tok_type_(tline, TOK_NUMBER)) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08003673 nasm_error(ERR_NONFATAL, "`%%line' expects line increment");
H. Peter Anvine2c80182005-01-15 22:15:51 +00003674 free_tlist(origline);
3675 return DIRECTIVE_FOUND;
3676 }
H. Peter Anvin70055962007-10-11 00:05:31 -07003677 m = readnum(tline->text, &err);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003678 tline = tline->next;
3679 }
3680 skip_white_(tline);
3681 src_set_linnum(k);
3682 istk->lineinc = m;
3683 if (tline) {
H. Peter Anvin274cda82016-05-10 02:56:29 -07003684 char *fname = detoken(tline, false);
3685 src_set_fname(fname);
3686 nasm_free(fname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003687 }
3688 free_tlist(origline);
3689 return DIRECTIVE_FOUND;
Keith Kaniosb307a4f2010-11-06 17:41:51 -05003690
H. Peter Anvine2c80182005-01-15 22:15:51 +00003691 default:
H. Peter Anvin130736c2016-02-17 20:27:41 -08003692 nasm_error(ERR_FATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00003693 "preprocessor directive `%s' not yet implemented",
H. Peter Anvin4169a472007-09-12 01:29:43 +00003694 pp_directives[i]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003695 return DIRECTIVE_FOUND;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003696 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003697}
3698
3699/*
H. Peter Anvin76690a12002-04-30 20:52:49 +00003700 * Ensure that a macro parameter contains a condition code and
3701 * nothing else. Return the condition code index if so, or -1
3702 * otherwise.
3703 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003704static int find_cc(Token * t)
H. Peter Anvineba20a72002-04-30 20:53:55 +00003705{
H. Peter Anvin76690a12002-04-30 20:52:49 +00003706 Token *tt;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003707
H. Peter Anvin25a99342007-09-22 17:45:45 -07003708 if (!t)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003709 return -1; /* Probably a %+ without a space */
H. Peter Anvin25a99342007-09-22 17:45:45 -07003710
H. Peter Anvineba20a72002-04-30 20:53:55 +00003711 skip_white_(t);
H. Peter Anvin76690a12002-04-30 20:52:49 +00003712 if (t->type != TOK_ID)
H. Peter Anvine2c80182005-01-15 22:15:51 +00003713 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003714 tt = t->next;
H. Peter Anvineba20a72002-04-30 20:53:55 +00003715 skip_white_(tt);
H. Peter Anvin76690a12002-04-30 20:52:49 +00003716 if (tt && (tt->type != TOK_OTHER || strcmp(tt->text, ",")))
H. Peter Anvine2c80182005-01-15 22:15:51 +00003717 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003718
Cyrill Gorcunov19456392012-05-02 00:18:56 +04003719 return bsii(t->text, (const char **)conditions, ARRAY_SIZE(conditions));
H. Peter Anvin76690a12002-04-30 20:52:49 +00003720}
3721
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003722/*
3723 * This routines walks over tokens strem and hadnles tokens
3724 * pasting, if @handle_explicit passed then explicit pasting
3725 * term is handled, otherwise -- implicit pastings only.
3726 */
Cyrill Gorcunov575d4282010-10-06 00:25:55 +04003727static bool paste_tokens(Token **head, const struct tokseq_match *m,
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003728 size_t mnum, bool handle_explicit)
H. Peter Anvind784a082009-04-20 14:01:18 -07003729{
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003730 Token *tok, *next, **prev_next, **prev_nonspace;
3731 bool pasted = false;
3732 char *buf, *p;
3733 size_t len, i;
H. Peter Anvind784a082009-04-20 14:01:18 -07003734
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003735 /*
3736 * The last token before pasting. We need it
3737 * to be able to connect new handled tokens.
3738 * In other words if there were a tokens stream
3739 *
3740 * A -> B -> C -> D
3741 *
3742 * and we've joined tokens B and C, the resulting
3743 * stream should be
3744 *
3745 * A -> BC -> D
3746 */
3747 tok = *head;
3748 prev_next = NULL;
3749
3750 if (!tok_type_(tok, TOK_WHITESPACE) && !tok_type_(tok, TOK_PASTE))
3751 prev_nonspace = head;
3752 else
3753 prev_nonspace = NULL;
3754
3755 while (tok && (next = tok->next)) {
3756
3757 switch (tok->type) {
H. Peter Anvind784a082009-04-20 14:01:18 -07003758 case TOK_WHITESPACE:
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003759 /* Zap redundant whitespaces */
3760 while (tok_type_(next, TOK_WHITESPACE))
3761 next = delete_Token(next);
3762 tok->next = next;
H. Peter Anvind784a082009-04-20 14:01:18 -07003763 break;
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003764
3765 case TOK_PASTE:
3766 /* Explicit pasting */
3767 if (!handle_explicit)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003768 break;
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003769 next = delete_Token(tok);
3770
3771 while (tok_type_(next, TOK_WHITESPACE))
3772 next = delete_Token(next);
3773
3774 if (!pasted)
3775 pasted = true;
3776
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003777 /* Left pasting token is start of line */
3778 if (!prev_nonspace)
H. Peter Anvin130736c2016-02-17 20:27:41 -08003779 nasm_error(ERR_FATAL, "No lvalue found on pasting");
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003780
Cyrill Gorcunov8b5c9fb2013-02-04 01:24:54 +04003781 /*
3782 * No ending token, this might happen in two
3783 * cases
3784 *
3785 * 1) There indeed no right token at all
3786 * 2) There is a bare "%define ID" statement,
3787 * and @ID does expand to whitespace.
3788 *
3789 * So technically we need to do a grammar analysis
3790 * in another stage of parsing, but for now lets don't
3791 * change the behaviour people used to. Simply allow
3792 * whitespace after paste token.
3793 */
3794 if (!next) {
3795 /*
3796 * Zap ending space tokens and that's all.
3797 */
3798 tok = (*prev_nonspace)->next;
3799 while (tok_type_(tok, TOK_WHITESPACE))
3800 tok = delete_Token(tok);
3801 tok = *prev_nonspace;
3802 tok->next = NULL;
3803 break;
3804 }
3805
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003806 tok = *prev_nonspace;
3807 while (tok_type_(tok, TOK_WHITESPACE))
3808 tok = delete_Token(tok);
3809 len = strlen(tok->text);
3810 len += strlen(next->text);
3811
3812 p = buf = nasm_malloc(len + 1);
3813 strcpy(p, tok->text);
3814 p = strchr(p, '\0');
3815 strcpy(p, next->text);
3816
3817 delete_Token(tok);
3818
3819 tok = tokenize(buf);
3820 nasm_free(buf);
3821
3822 *prev_nonspace = tok;
3823 while (tok && tok->next)
3824 tok = tok->next;
3825
3826 tok->next = delete_Token(next);
3827
3828 /* Restart from pasted tokens head */
3829 tok = *prev_nonspace;
3830 break;
3831
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003832 default:
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003833 /* implicit pasting */
Cyrill Gorcunov575d4282010-10-06 00:25:55 +04003834 for (i = 0; i < mnum; i++) {
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003835 if (!(PP_CONCAT_MATCH(tok, m[i].mask_head)))
3836 continue;
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +04003837
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003838 len = 0;
3839 while (next && PP_CONCAT_MATCH(next, m[i].mask_tail)) {
3840 len += strlen(next->text);
3841 next = next->next;
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +04003842 }
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003843
3844 /* No match */
3845 if (tok == next)
3846 break;
3847
3848 len += strlen(tok->text);
3849 p = buf = nasm_malloc(len + 1);
3850
Adam Majer1a069432017-07-25 11:12:35 +02003851 strcpy(p, tok->text);
3852 p = strchr(p, '\0');
3853 tok = delete_Token(tok);
3854
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003855 while (tok != next) {
Adam Majer1a069432017-07-25 11:12:35 +02003856 if (PP_CONCAT_MATCH(tok, m[i].mask_tail)) {
3857 strcpy(p, tok->text);
3858 p = strchr(p, '\0');
3859 }
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003860 tok = delete_Token(tok);
3861 }
3862
3863 tok = tokenize(buf);
3864 nasm_free(buf);
3865
3866 if (prev_next)
3867 *prev_next = tok;
3868 else
3869 *head = tok;
3870
3871 /*
3872 * Connect pasted into original stream,
3873 * ie A -> new-tokens -> B
3874 */
3875 while (tok && tok->next)
3876 tok = tok->next;
3877 tok->next = next;
3878
3879 if (!pasted)
3880 pasted = true;
3881
3882 /* Restart from pasted tokens head */
3883 tok = prev_next ? *prev_next : *head;
Cyrill Gorcunov575d4282010-10-06 00:25:55 +04003884 }
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003885
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003886 break;
H. Peter Anvind784a082009-04-20 14:01:18 -07003887 }
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003888
3889 prev_next = &tok->next;
3890
3891 if (tok->next &&
3892 !tok_type_(tok->next, TOK_WHITESPACE) &&
3893 !tok_type_(tok->next, TOK_PASTE))
3894 prev_nonspace = prev_next;
3895
3896 tok = tok->next;
H. Peter Anvind784a082009-04-20 14:01:18 -07003897 }
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003898
3899 return pasted;
H. Peter Anvind784a082009-04-20 14:01:18 -07003900}
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003901
3902/*
3903 * expands to a list of tokens from %{x:y}
3904 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003905static Token *expand_mmac_params_range(MMacro *mac, Token *tline, Token ***last)
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003906{
3907 Token *t = tline, **tt, *tm, *head;
3908 char *pos;
3909 int fst, lst, j, i;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003910
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003911 pos = strchr(tline->text, ':');
3912 nasm_assert(pos);
3913
3914 lst = atoi(pos + 1);
3915 fst = atoi(tline->text + 1);
3916
3917 /*
3918 * only macros params are accounted so
3919 * if someone passes %0 -- we reject such
3920 * value(s)
3921 */
3922 if (lst == 0 || fst == 0)
3923 goto err;
3924
3925 /* the values should be sane */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003926 if ((fst > (int)mac->nparam || fst < (-(int)mac->nparam)) ||
3927 (lst > (int)mac->nparam || lst < (-(int)mac->nparam)))
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003928 goto err;
3929
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003930 fst = fst < 0 ? fst + (int)mac->nparam + 1: fst;
3931 lst = lst < 0 ? lst + (int)mac->nparam + 1: lst;
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003932
3933 /* counted from zero */
3934 fst--, lst--;
3935
3936 /*
Cyrill Gorcunove75331c2013-11-09 12:02:15 +04003937 * It will be at least one token. Note we
3938 * need to scan params until separator, otherwise
3939 * only first token will be passed.
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003940 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003941 tm = mac->params[(fst + mac->rotate) % mac->nparam];
Cyrill Gorcunove75331c2013-11-09 12:02:15 +04003942 head = new_Token(NULL, tm->type, tm->text, 0);
3943 tt = &head->next, tm = tm->next;
3944 while (tok_isnt_(tm, ",")) {
3945 t = new_Token(NULL, tm->type, tm->text, 0);
3946 *tt = t, tt = &t->next, tm = tm->next;
3947 }
3948
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003949 if (fst < lst) {
3950 for (i = fst + 1; i <= lst; i++) {
3951 t = new_Token(NULL, TOK_OTHER, ",", 0);
3952 *tt = t, tt = &t->next;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003953 j = (i + mac->rotate) % mac->nparam;
3954 tm = mac->params[j];
Cyrill Gorcunove75331c2013-11-09 12:02:15 +04003955 while (tok_isnt_(tm, ",")) {
3956 t = new_Token(NULL, tm->type, tm->text, 0);
3957 *tt = t, tt = &t->next, tm = tm->next;
3958 }
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003959 }
3960 } else {
3961 for (i = fst - 1; i >= lst; i--) {
3962 t = new_Token(NULL, TOK_OTHER, ",", 0);
3963 *tt = t, tt = &t->next;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003964 j = (i + mac->rotate) % mac->nparam;
3965 tm = mac->params[j];
Cyrill Gorcunove75331c2013-11-09 12:02:15 +04003966 while (tok_isnt_(tm, ",")) {
3967 t = new_Token(NULL, tm->type, tm->text, 0);
3968 *tt = t, tt = &t->next, tm = tm->next;
3969 }
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003970 }
3971 }
3972
3973 *last = tt;
3974 return head;
3975
3976err:
H. Peter Anvin130736c2016-02-17 20:27:41 -08003977 nasm_error(ERR_NONFATAL, "`%%{%s}': macro parameters out of range",
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003978 &tline->text[1]);
3979 return tline;
3980}
3981
H. Peter Anvin76690a12002-04-30 20:52:49 +00003982/*
3983 * Expand MMacro-local things: parameter references (%0, %n, %+n,
H. Peter Anvin67c63722008-10-26 23:49:00 -07003984 * %-n) and MMacro-local identifiers (%%foo) as well as
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003985 * macro indirection (%[...]) and range (%{..:..}).
H. Peter Anvin76690a12002-04-30 20:52:49 +00003986 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003987static Token *expand_mmac_params(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00003988{
H. Peter Anvin734b1882002-04-30 21:01:08 +00003989 Token *t, *tt, **tail, *thead;
H. Peter Anvin6125b622009-04-08 14:02:25 -07003990 bool changed = false;
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003991 char *pos;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003992
3993 tail = &thead;
3994 thead = NULL;
3995
H. Peter Anvine2c80182005-01-15 22:15:51 +00003996 while (tline) {
3997 if (tline->type == TOK_PREPROC_ID &&
Cyrill Gorcunovca611192010-06-04 09:22:12 +04003998 (((tline->text[1] == '+' || tline->text[1] == '-') && tline->text[2]) ||
3999 (tline->text[1] >= '0' && tline->text[1] <= '9') ||
4000 tline->text[1] == '%')) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004001 char *text = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004002 int type = 0, cc; /* type = 0 to placate optimisers */
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004003 char tmpbuf[30];
H. Peter Anvin25a99342007-09-22 17:45:45 -07004004 unsigned int n;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004005 int i;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004006 MMacro *mac;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004007
H. Peter Anvine2c80182005-01-15 22:15:51 +00004008 t = tline;
4009 tline = tline->next;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004010
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004011 mac = istk->mstk;
4012 while (mac && !mac->name) /* avoid mistaking %reps for macros */
4013 mac = mac->next_active;
4014 if (!mac) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08004015 nasm_error(ERR_NONFATAL, "`%s': not in a macro call", t->text);
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004016 } else {
4017 pos = strchr(t->text, ':');
4018 if (!pos) {
4019 switch (t->text[1]) {
4020 /*
4021 * We have to make a substitution of one of the
4022 * forms %1, %-1, %+1, %%foo, %0.
4023 */
4024 case '0':
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004025 type = TOK_NUMBER;
4026 snprintf(tmpbuf, sizeof(tmpbuf), "%d", mac->nparam);
4027 text = nasm_strdup(tmpbuf);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004028 break;
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004029 case '%':
H. Peter Anvine2c80182005-01-15 22:15:51 +00004030 type = TOK_ID;
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004031 snprintf(tmpbuf, sizeof(tmpbuf), "..@%"PRIu64".",
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004032 mac->unique);
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004033 text = nasm_strcat(tmpbuf, t->text + 2);
4034 break;
4035 case '-':
4036 n = atoi(t->text + 2) - 1;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004037 if (n >= mac->nparam)
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004038 tt = NULL;
4039 else {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004040 if (mac->nparam > 1)
4041 n = (n + mac->rotate) % mac->nparam;
4042 tt = mac->params[n];
H. Peter Anvine2c80182005-01-15 22:15:51 +00004043 }
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004044 cc = find_cc(tt);
4045 if (cc == -1) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08004046 nasm_error(ERR_NONFATAL,
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004047 "macro parameter %d is not a condition code",
4048 n + 1);
4049 text = NULL;
4050 } else {
4051 type = TOK_ID;
4052 if (inverse_ccs[cc] == -1) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08004053 nasm_error(ERR_NONFATAL,
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004054 "condition code `%s' is not invertible",
4055 conditions[cc]);
4056 text = NULL;
4057 } else
4058 text = nasm_strdup(conditions[inverse_ccs[cc]]);
4059 }
4060 break;
4061 case '+':
4062 n = atoi(t->text + 2) - 1;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004063 if (n >= mac->nparam)
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004064 tt = NULL;
4065 else {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004066 if (mac->nparam > 1)
4067 n = (n + mac->rotate) % mac->nparam;
4068 tt = mac->params[n];
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004069 }
4070 cc = find_cc(tt);
4071 if (cc == -1) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08004072 nasm_error(ERR_NONFATAL,
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004073 "macro parameter %d is not a condition code",
4074 n + 1);
4075 text = NULL;
4076 } else {
4077 type = TOK_ID;
4078 text = nasm_strdup(conditions[cc]);
4079 }
4080 break;
4081 default:
4082 n = atoi(t->text + 1) - 1;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004083 if (n >= mac->nparam)
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004084 tt = NULL;
4085 else {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004086 if (mac->nparam > 1)
4087 n = (n + mac->rotate) % mac->nparam;
4088 tt = mac->params[n];
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004089 }
4090 if (tt) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004091 for (i = 0; i < mac->paramlen[n]; i++) {
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004092 *tail = new_Token(NULL, tt->type, tt->text, 0);
4093 tail = &(*tail)->next;
4094 tt = tt->next;
4095 }
4096 }
4097 text = NULL; /* we've done it here */
4098 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004099 }
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004100 } else {
4101 /*
4102 * seems we have a parameters range here
4103 */
4104 Token *head, **last;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004105 head = expand_mmac_params_range(mac, t, &last);
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004106 if (head != t) {
4107 *tail = head;
4108 *last = tline;
4109 tline = head;
4110 text = NULL;
4111 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00004112 }
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004113 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00004114 if (!text) {
4115 delete_Token(t);
4116 } else {
4117 *tail = t;
4118 tail = &t->next;
4119 t->type = type;
4120 nasm_free(t->text);
4121 t->text = text;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004122 t->a.mac = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004123 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004124 changed = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004125 continue;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004126 } else if (tline->type == TOK_INDIRECT) {
4127 t = tline;
4128 tline = tline->next;
4129 tt = tokenize(t->text);
4130 tt = expand_mmac_params(tt);
4131 tt = expand_smacro(tt);
4132 *tail = tt;
4133 while (tt) {
4134 tt->a.mac = NULL; /* Necessary? */
4135 tail = &tt->next;
4136 tt = tt->next;
4137 }
4138 delete_Token(t);
4139 changed = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004140 } else {
4141 t = *tail = tline;
4142 tline = tline->next;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004143 t->a.mac = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004144 tail = &t->next;
4145 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00004146 }
H. Peter Anvineba20a72002-04-30 20:53:55 +00004147 *tail = NULL;
H. Peter Anvin67c63722008-10-26 23:49:00 -07004148
Cyrill Gorcunovc6a742c2011-06-27 01:23:09 +04004149 if (changed) {
4150 const struct tokseq_match t[] = {
4151 {
4152 PP_CONCAT_MASK(TOK_ID) |
4153 PP_CONCAT_MASK(TOK_FLOAT), /* head */
4154 PP_CONCAT_MASK(TOK_ID) |
4155 PP_CONCAT_MASK(TOK_NUMBER) |
4156 PP_CONCAT_MASK(TOK_FLOAT) |
4157 PP_CONCAT_MASK(TOK_OTHER) /* tail */
4158 },
4159 {
4160 PP_CONCAT_MASK(TOK_NUMBER), /* head */
4161 PP_CONCAT_MASK(TOK_NUMBER) /* tail */
4162 }
4163 };
4164 paste_tokens(&thead, t, ARRAY_SIZE(t), false);
4165 }
H. Peter Anvin6125b622009-04-08 14:02:25 -07004166
H. Peter Anvin76690a12002-04-30 20:52:49 +00004167 return thead;
4168}
4169
4170/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004171 * Expand all single-line macro calls made in the given line.
4172 * Return the expanded version of the line. The original is deemed
4173 * to be destroyed in the process. (In reality we'll just move
4174 * Tokens from input to output a lot of the time, rather than
4175 * actually bothering to destroy and replicate.)
4176 */
H. Peter Anvincb1cf592007-11-19 12:26:50 -08004177
H. Peter Anvine2c80182005-01-15 22:15:51 +00004178static Token *expand_smacro(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004179{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004180 Token *t, *tt, *mstart, **tail, *thead;
H. Peter Anvineba20a72002-04-30 20:53:55 +00004181 SMacro *head = NULL, *m;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004182 Token **params;
4183 int *paramsize;
H. Peter Anvin25a99342007-09-22 17:45:45 -07004184 unsigned int nparam, sparam;
H. Peter Anvind784a082009-04-20 14:01:18 -07004185 int brackets;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004186 Token *org_tline = tline;
4187 Context *ctx;
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08004188 const char *mname;
H. Peter Anvin2a15e692007-11-19 13:14:59 -08004189 int deadman = DEADMAN_LIMIT;
H. Peter Anvin8287daf2009-07-07 16:00:58 -07004190 bool expanded;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004191
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004192 /*
4193 * Trick: we should avoid changing the start token pointer since it can
4194 * be contained in "next" field of other token. Because of this
4195 * we allocate a copy of first token and work with it; at the end of
4196 * routine we copy it back
4197 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004198 if (org_tline) {
Cyrill Gorcunoved4a8052010-04-09 15:40:35 +04004199 tline = new_Token(org_tline->next, org_tline->type,
4200 org_tline->text, 0);
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004201 tline->a.mac = org_tline->a.mac;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004202 nasm_free(org_tline->text);
4203 org_tline->text = NULL;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004204 }
4205
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004206 expanded = true; /* Always expand %+ at least once */
H. Peter Anvin8287daf2009-07-07 16:00:58 -07004207
H. Peter Anvincb1cf592007-11-19 12:26:50 -08004208again:
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004209 thead = NULL;
Cyrill Gorcunoved4a8052010-04-09 15:40:35 +04004210 tail = &thead;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004211
H. Peter Anvine2c80182005-01-15 22:15:51 +00004212 while (tline) { /* main token loop */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004213 if (!--deadman) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08004214 nasm_error(ERR_NONFATAL, "interminable macro recursion");
Cyrill Gorcunovbd38c8f2009-11-21 11:11:23 +03004215 goto err;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004216 }
H. Peter Anvincb1cf592007-11-19 12:26:50 -08004217
H. Peter Anvine2c80182005-01-15 22:15:51 +00004218 if ((mname = tline->text)) {
4219 /* if this token is a local macro, look in local context */
Cyrill Gorcunovc56d9ad2010-02-11 15:12:19 +03004220 if (tline->type == TOK_ID) {
4221 head = (SMacro *)hash_findix(&smacros, mname);
4222 } else if (tline->type == TOK_PREPROC_ID) {
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04004223 ctx = get_ctx(mname, &mname);
Cyrill Gorcunovc56d9ad2010-02-11 15:12:19 +03004224 head = ctx ? (SMacro *)hash_findix(&ctx->localmac, mname) : NULL;
4225 } else
4226 head = NULL;
H. Peter Anvin072771e2008-05-22 13:17:51 -07004227
H. Peter Anvine2c80182005-01-15 22:15:51 +00004228 /*
4229 * We've hit an identifier. As in is_mmacro below, we first
4230 * check whether the identifier is a single-line macro at
4231 * all, then think about checking for parameters if
4232 * necessary.
4233 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004234 list_for_each(m, head)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004235 if (!mstrcmp(m->name, mname, m->casesense))
4236 break;
4237 if (m) {
4238 mstart = tline;
4239 params = NULL;
4240 paramsize = NULL;
4241 if (m->nparam == 0) {
4242 /*
4243 * Simple case: the macro is parameterless. Discard the
4244 * one token that the macro call took, and push the
4245 * expansion back on the to-do stack.
4246 */
4247 if (!m->expansion) {
4248 if (!strcmp("__FILE__", m->name)) {
H. Peter Anvin274cda82016-05-10 02:56:29 -07004249 const char *file = src_get_fname();
4250 /* nasm_free(tline->text); here? */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004251 tline->text = nasm_quote(file, strlen(file));
4252 tline->type = TOK_STRING;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004253 continue;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004254 }
4255 if (!strcmp("__LINE__", m->name)) {
4256 nasm_free(tline->text);
4257 make_tok_num(tline, src_get_linnum());
4258 continue;
4259 }
4260 if (!strcmp("__BITS__", m->name)) {
4261 nasm_free(tline->text);
4262 make_tok_num(tline, globalbits);
4263 continue;
4264 }
4265 tline = delete_Token(tline);
4266 continue;
4267 }
4268 } else {
4269 /*
4270 * Complicated case: at least one macro with this name
H. Peter Anvine2c80182005-01-15 22:15:51 +00004271 * exists and takes parameters. We must find the
4272 * parameters in the call, count them, find the SMacro
4273 * that corresponds to that form of the macro call, and
4274 * substitute for the parameters when we expand. What a
4275 * pain.
4276 */
4277 /*tline = tline->next;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004278 skip_white_(tline); */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004279 do {
4280 t = tline->next;
4281 while (tok_type_(t, TOK_SMAC_END)) {
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004282 t->a.mac->in_progress = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004283 t->text = NULL;
4284 t = tline->next = delete_Token(t);
4285 }
4286 tline = t;
4287 } while (tok_type_(tline, TOK_WHITESPACE));
4288 if (!tok_is_(tline, "(")) {
4289 /*
4290 * This macro wasn't called with parameters: ignore
4291 * the call. (Behaviour borrowed from gnu cpp.)
4292 */
4293 tline = mstart;
4294 m = NULL;
4295 } else {
4296 int paren = 0;
4297 int white = 0;
4298 brackets = 0;
4299 nparam = 0;
4300 sparam = PARAM_DELTA;
4301 params = nasm_malloc(sparam * sizeof(Token *));
4302 params[0] = tline->next;
4303 paramsize = nasm_malloc(sparam * sizeof(int));
4304 paramsize[0] = 0;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07004305 while (true) { /* parameter loop */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004306 /*
4307 * For some unusual expansions
4308 * which concatenates function call
4309 */
4310 t = tline->next;
4311 while (tok_type_(t, TOK_SMAC_END)) {
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004312 t->a.mac->in_progress = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004313 t->text = NULL;
4314 t = tline->next = delete_Token(t);
4315 }
4316 tline = t;
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00004317
H. Peter Anvine2c80182005-01-15 22:15:51 +00004318 if (!tline) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08004319 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00004320 "macro call expects terminating `)'");
4321 break;
4322 }
4323 if (tline->type == TOK_WHITESPACE
4324 && brackets <= 0) {
4325 if (paramsize[nparam])
4326 white++;
4327 else
4328 params[nparam] = tline->next;
4329 continue; /* parameter loop */
4330 }
4331 if (tline->type == TOK_OTHER
4332 && tline->text[1] == 0) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004333 char ch = tline->text[0];
H. Peter Anvine2c80182005-01-15 22:15:51 +00004334 if (ch == ',' && !paren && brackets <= 0) {
4335 if (++nparam >= sparam) {
4336 sparam += PARAM_DELTA;
4337 params = nasm_realloc(params,
Cyrill Gorcunoved4a8052010-04-09 15:40:35 +04004338 sparam * sizeof(Token *));
4339 paramsize = nasm_realloc(paramsize,
4340 sparam * sizeof(int));
H. Peter Anvine2c80182005-01-15 22:15:51 +00004341 }
4342 params[nparam] = tline->next;
4343 paramsize[nparam] = 0;
4344 white = 0;
4345 continue; /* parameter loop */
4346 }
4347 if (ch == '{' &&
4348 (brackets > 0 || (brackets == 0 &&
4349 !paramsize[nparam])))
4350 {
4351 if (!(brackets++)) {
4352 params[nparam] = tline->next;
4353 continue; /* parameter loop */
4354 }
4355 }
4356 if (ch == '}' && brackets > 0)
4357 if (--brackets == 0) {
4358 brackets = -1;
4359 continue; /* parameter loop */
4360 }
4361 if (ch == '(' && !brackets)
4362 paren++;
4363 if (ch == ')' && brackets <= 0)
4364 if (--paren < 0)
4365 break;
4366 }
4367 if (brackets < 0) {
4368 brackets = 0;
H. Peter Anvin130736c2016-02-17 20:27:41 -08004369 nasm_error(ERR_NONFATAL, "braces do not "
H. Peter Anvine2c80182005-01-15 22:15:51 +00004370 "enclose all of macro parameter");
4371 }
4372 paramsize[nparam] += white + 1;
4373 white = 0;
4374 } /* parameter loop */
4375 nparam++;
4376 while (m && (m->nparam != nparam ||
4377 mstrcmp(m->name, mname,
4378 m->casesense)))
4379 m = m->next;
4380 if (!m)
H. Peter Anvin130736c2016-02-17 20:27:41 -08004381 nasm_error(ERR_WARNING|ERR_PASS1|ERR_WARN_MNP,
H. Peter Anvine2c80182005-01-15 22:15:51 +00004382 "macro `%s' exists, "
4383 "but not taking %d parameters",
4384 mstart->text, nparam);
4385 }
4386 }
4387 if (m && m->in_progress)
4388 m = NULL;
4389 if (!m) { /* in progess or didn't find '(' or wrong nparam */
H. Peter Anvin70653092007-10-19 14:42:29 -07004390 /*
H. Peter Anvine2c80182005-01-15 22:15:51 +00004391 * Design question: should we handle !tline, which
4392 * indicates missing ')' here, or expand those
4393 * macros anyway, which requires the (t) test a few
H. Peter Anvin70653092007-10-19 14:42:29 -07004394 * lines down?
H. Peter Anvine2c80182005-01-15 22:15:51 +00004395 */
4396 nasm_free(params);
4397 nasm_free(paramsize);
4398 tline = mstart;
4399 } else {
4400 /*
4401 * Expand the macro: we are placed on the last token of the
4402 * call, so that we can easily split the call from the
4403 * following tokens. We also start by pushing an SMAC_END
4404 * token for the cycle removal.
4405 */
4406 t = tline;
4407 if (t) {
4408 tline = t->next;
4409 t->next = NULL;
4410 }
4411 tt = new_Token(tline, TOK_SMAC_END, NULL, 0);
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004412 tt->a.mac = m;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07004413 m->in_progress = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004414 tline = tt;
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04004415 list_for_each(t, m->expansion) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004416 if (t->type >= TOK_SMAC_PARAM) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00004417 Token *pcopy = tline, **ptail = &pcopy;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004418 Token *ttt, *pt;
4419 int i;
H. Peter Anvin734b1882002-04-30 21:01:08 +00004420
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004421 ttt = params[t->type - TOK_SMAC_PARAM];
4422 i = paramsize[t->type - TOK_SMAC_PARAM];
4423 while (--i >= 0) {
4424 pt = *ptail = new_Token(tline, ttt->type,
4425 ttt->text, 0);
4426 ptail = &pt->next;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004427 ttt = ttt->next;
4428 }
4429 tline = pcopy;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004430 } else if (t->type == TOK_PREPROC_Q) {
4431 tt = new_Token(tline, TOK_ID, mname, 0);
4432 tline = tt;
4433 } else if (t->type == TOK_PREPROC_QQ) {
4434 tt = new_Token(tline, TOK_ID, m->name, 0);
4435 tline = tt;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004436 } else {
4437 tt = new_Token(tline, t->type, t->text, 0);
4438 tline = tt;
4439 }
4440 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00004441
H. Peter Anvine2c80182005-01-15 22:15:51 +00004442 /*
4443 * Having done that, get rid of the macro call, and clean
4444 * up the parameters.
4445 */
4446 nasm_free(params);
4447 nasm_free(paramsize);
4448 free_tlist(mstart);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004449 expanded = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004450 continue; /* main token loop */
4451 }
4452 }
4453 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004454
H. Peter Anvine2c80182005-01-15 22:15:51 +00004455 if (tline->type == TOK_SMAC_END) {
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004456 tline->a.mac->in_progress = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004457 tline = delete_Token(tline);
4458 } else {
4459 t = *tail = tline;
4460 tline = tline->next;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004461 t->a.mac = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004462 t->next = NULL;
4463 tail = &t->next;
4464 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004465 }
4466
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004467 /*
4468 * Now scan the entire line and look for successive TOK_IDs that resulted
Keith Kaniosb7a89542007-04-12 02:40:54 +00004469 * after expansion (they can't be produced by tokenize()). The successive
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004470 * TOK_IDs should be concatenated.
4471 * Also we look for %+ tokens and concatenate the tokens before and after
4472 * them (without white spaces in between).
4473 */
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +04004474 if (expanded) {
Cyrill Gorcunovc6a742c2011-06-27 01:23:09 +04004475 const struct tokseq_match t[] = {
4476 {
4477 PP_CONCAT_MASK(TOK_ID) |
4478 PP_CONCAT_MASK(TOK_PREPROC_ID), /* head */
4479 PP_CONCAT_MASK(TOK_ID) |
4480 PP_CONCAT_MASK(TOK_PREPROC_ID) |
4481 PP_CONCAT_MASK(TOK_NUMBER) /* tail */
4482 }
4483 };
4484 if (paste_tokens(&thead, t, ARRAY_SIZE(t), true)) {
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +04004485 /*
4486 * If we concatenated something, *and* we had previously expanded
4487 * an actual macro, scan the lines again for macros...
4488 */
4489 tline = thead;
4490 expanded = false;
4491 goto again;
4492 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00004493 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004494
Cyrill Gorcunovbd38c8f2009-11-21 11:11:23 +03004495err:
H. Peter Anvine2c80182005-01-15 22:15:51 +00004496 if (org_tline) {
4497 if (thead) {
4498 *org_tline = *thead;
4499 /* since we just gave text to org_line, don't free it */
4500 thead->text = NULL;
4501 delete_Token(thead);
4502 } else {
4503 /* the expression expanded to empty line;
4504 we can't return NULL for some reasons
4505 we just set the line to a single WHITESPACE token. */
4506 memset(org_tline, 0, sizeof(*org_tline));
4507 org_tline->text = NULL;
4508 org_tline->type = TOK_WHITESPACE;
4509 }
4510 thead = org_tline;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004511 }
4512
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004513 return thead;
4514}
4515
4516/*
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004517 * Similar to expand_smacro but used exclusively with macro identifiers
4518 * right before they are fetched in. The reason is that there can be
4519 * identifiers consisting of several subparts. We consider that if there
4520 * are more than one element forming the name, user wants a expansion,
4521 * otherwise it will be left as-is. Example:
4522 *
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004523 * %define %$abc cde
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004524 *
4525 * the identifier %$abc will be left as-is so that the handler for %define
4526 * will suck it and define the corresponding value. Other case:
4527 *
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004528 * %define _%$abc cde
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004529 *
4530 * In this case user wants name to be expanded *before* %define starts
4531 * working, so we'll expand %$abc into something (if it has a value;
4532 * otherwise it will be left as-is) then concatenate all successive
4533 * PP_IDs into one.
4534 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004535static Token *expand_id(Token * tline)
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004536{
4537 Token *cur, *oldnext = NULL;
4538
H. Peter Anvin734b1882002-04-30 21:01:08 +00004539 if (!tline || !tline->next)
H. Peter Anvine2c80182005-01-15 22:15:51 +00004540 return tline;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004541
4542 cur = tline;
4543 while (cur->next &&
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004544 (cur->next->type == TOK_ID ||
4545 cur->next->type == TOK_PREPROC_ID
4546 || cur->next->type == TOK_NUMBER))
H. Peter Anvine2c80182005-01-15 22:15:51 +00004547 cur = cur->next;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004548
4549 /* If identifier consists of just one token, don't expand */
4550 if (cur == tline)
H. Peter Anvine2c80182005-01-15 22:15:51 +00004551 return tline;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004552
H. Peter Anvine2c80182005-01-15 22:15:51 +00004553 if (cur) {
4554 oldnext = cur->next; /* Detach the tail past identifier */
4555 cur->next = NULL; /* so that expand_smacro stops here */
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004556 }
4557
H. Peter Anvin734b1882002-04-30 21:01:08 +00004558 tline = expand_smacro(tline);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004559
H. Peter Anvine2c80182005-01-15 22:15:51 +00004560 if (cur) {
4561 /* expand_smacro possibly changhed tline; re-scan for EOL */
4562 cur = tline;
4563 while (cur && cur->next)
4564 cur = cur->next;
4565 if (cur)
4566 cur->next = oldnext;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004567 }
4568
4569 return tline;
4570}
4571
4572/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004573 * Determine whether the given line constitutes a multi-line macro
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004574 * call, and return the MMacro structure called if so. Doesn't have
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004575 * to check for an initial label - that's taken care of in
4576 * expand_mmacro - but must check numbers of parameters. Guaranteed
4577 * to be called with tline->type == TOK_ID, so the putative macro
4578 * name is easy to find.
4579 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004580static MMacro *is_mmacro(Token * tline, Token *** params_array)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004581{
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004582 MMacro *head, *m;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004583 Token **params;
4584 int nparam;
4585
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004586 head = (MMacro *) hash_findix(&mmacros, tline->text);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004587
4588 /*
4589 * Efficiency: first we see if any macro exists with the given
4590 * name. If not, we can return NULL immediately. _Then_ we
4591 * count the parameters, and then we look further along the
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004592 * list if necessary to find the proper MMacro.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004593 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004594 list_for_each(m, head)
4595 if (!mstrcmp(m->name, tline->text, m->casesense))
H. Peter Anvine2c80182005-01-15 22:15:51 +00004596 break;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004597 if (!m)
H. Peter Anvine2c80182005-01-15 22:15:51 +00004598 return NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004599
4600 /*
4601 * OK, we have a potential macro. Count and demarcate the
4602 * parameters.
4603 */
H. Peter Anvin734b1882002-04-30 21:01:08 +00004604 count_mmac_params(tline->next, &nparam, &params);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004605
4606 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004607 * So we know how many parameters we've got. Find the MMacro
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004608 * structure that handles this number.
4609 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004610 while (m) {
4611 if (m->nparam_min <= nparam
4612 && (m->plus || nparam <= m->nparam_max)) {
4613 /*
4614 * This one is right. Just check if cycle removal
4615 * prohibits us using it before we actually celebrate...
4616 */
4617 if (m->in_progress > m->max_depth) {
4618 if (m->max_depth > 0) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08004619 nasm_error(ERR_WARNING,
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004620 "reached maximum recursion depth of %i",
4621 m->max_depth);
4622 }
4623 nasm_free(params);
4624 return NULL;
4625 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00004626 /*
4627 * It's right, and we can use it. Add its default
4628 * parameters to the end of our list if necessary.
4629 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004630 if (m->defaults && nparam < m->nparam_min + m->ndefs) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00004631 params =
4632 nasm_realloc(params,
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004633 ((m->nparam_min + m->ndefs +
H. Peter Anvine2c80182005-01-15 22:15:51 +00004634 1) * sizeof(*params)));
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004635 while (nparam < m->nparam_min + m->ndefs) {
4636 params[nparam] = m->defaults[nparam - m->nparam_min];
H. Peter Anvine2c80182005-01-15 22:15:51 +00004637 nparam++;
4638 }
4639 }
4640 /*
4641 * If we've gone over the maximum parameter count (and
4642 * we're in Plus mode), ignore parameters beyond
4643 * nparam_max.
4644 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004645 if (m->plus && nparam > m->nparam_max)
4646 nparam = m->nparam_max;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004647 /*
4648 * Then terminate the parameter list, and leave.
4649 */
4650 if (!params) { /* need this special case */
4651 params = nasm_malloc(sizeof(*params));
4652 nparam = 0;
4653 }
4654 params[nparam] = NULL;
4655 *params_array = params;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004656 return m;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004657 }
4658 /*
4659 * This one wasn't right: look for the next one with the
4660 * same name.
4661 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004662 list_for_each(m, m->next)
4663 if (!mstrcmp(m->name, tline->text, m->casesense))
H. Peter Anvine2c80182005-01-15 22:15:51 +00004664 break;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004665 }
4666
4667 /*
4668 * After all that, we didn't find one with the right number of
4669 * parameters. Issue a warning, and fail to expand the macro.
4670 */
H. Peter Anvin130736c2016-02-17 20:27:41 -08004671 nasm_error(ERR_WARNING|ERR_PASS1|ERR_WARN_MNP,
H. Peter Anvine2c80182005-01-15 22:15:51 +00004672 "macro `%s' exists, but not taking %d parameters",
4673 tline->text, nparam);
H. Peter Anvin734b1882002-04-30 21:01:08 +00004674 nasm_free(params);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004675 return NULL;
4676}
4677
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004678
4679/*
4680 * Save MMacro invocation specific fields in
4681 * preparation for a recursive macro expansion
4682 */
4683static void push_mmacro(MMacro *m)
4684{
4685 MMacroInvocation *i;
4686
4687 i = nasm_malloc(sizeof(MMacroInvocation));
4688 i->prev = m->prev;
4689 i->params = m->params;
4690 i->iline = m->iline;
4691 i->nparam = m->nparam;
4692 i->rotate = m->rotate;
4693 i->paramlen = m->paramlen;
4694 i->unique = m->unique;
4695 i->condcnt = m->condcnt;
4696 m->prev = i;
4697}
4698
4699
4700/*
4701 * Restore MMacro invocation specific fields that were
4702 * saved during a previous recursive macro expansion
4703 */
4704static void pop_mmacro(MMacro *m)
4705{
4706 MMacroInvocation *i;
4707
4708 if (m->prev) {
4709 i = m->prev;
4710 m->prev = i->prev;
4711 m->params = i->params;
4712 m->iline = i->iline;
4713 m->nparam = i->nparam;
4714 m->rotate = i->rotate;
4715 m->paramlen = i->paramlen;
4716 m->unique = i->unique;
4717 m->condcnt = i->condcnt;
4718 nasm_free(i);
4719 }
4720}
4721
4722
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004723/*
4724 * Expand the multi-line macro call made by the given line, if
4725 * there is one to be expanded. If there is, push the expansion on
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004726 * istk->expansion and return 1. Otherwise return 0.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004727 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004728static int expand_mmacro(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004729{
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004730 Token *startline = tline;
H. Peter Anvineba20a72002-04-30 20:53:55 +00004731 Token *label = NULL;
4732 int dont_prepend = 0;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004733 Token **params, *t, *tt;
4734 MMacro *m;
4735 Line *l, *ll;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004736 int i, nparam, *paramlen;
H. Peter Anvinc751e862008-06-09 10:18:45 -07004737 const char *mname;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004738
4739 t = tline;
H. Peter Anvineba20a72002-04-30 20:53:55 +00004740 skip_white_(t);
H. Peter Anvince2233b2008-05-25 21:57:00 -07004741 /* if (!tok_type_(t, TOK_ID)) Lino 02/25/02 */
H. Peter Anvindce1e2f2002-04-30 21:06:37 +00004742 if (!tok_type_(t, TOK_ID) && !tok_type_(t, TOK_PREPROC_ID))
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004743 return 0;
4744 m = is_mmacro(t, &params);
4745 if (m) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004746 mname = t->text;
H. Peter Anvinc751e862008-06-09 10:18:45 -07004747 } else {
H. Peter Anvine2c80182005-01-15 22:15:51 +00004748 Token *last;
4749 /*
4750 * We have an id which isn't a macro call. We'll assume
4751 * it might be a label; we'll also check to see if a
4752 * colon follows it. Then, if there's another id after
4753 * that lot, we'll check it again for macro-hood.
4754 */
4755 label = last = t;
4756 t = t->next;
4757 if (tok_type_(t, TOK_WHITESPACE))
4758 last = t, t = t->next;
4759 if (tok_is_(t, ":")) {
4760 dont_prepend = 1;
4761 last = t, t = t->next;
4762 if (tok_type_(t, TOK_WHITESPACE))
4763 last = t, t = t->next;
4764 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004765 if (!tok_type_(t, TOK_ID) || !(m = is_mmacro(t, &params)))
4766 return 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004767 last->next = NULL;
Keith Kanios891775e2009-07-11 06:08:54 -05004768 mname = t->text;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004769 tline = t;
H. Peter Anvineba20a72002-04-30 20:53:55 +00004770 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004771
4772 /*
4773 * Fix up the parameters: this involves stripping leading and
4774 * trailing whitespace, then stripping braces if they are
4775 * present.
4776 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004777 for (nparam = 0; params[nparam]; nparam++) ;
H. Peter Anvin734b1882002-04-30 21:01:08 +00004778 paramlen = nparam ? nasm_malloc(nparam * sizeof(*paramlen)) : NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004779
H. Peter Anvine2c80182005-01-15 22:15:51 +00004780 for (i = 0; params[i]; i++) {
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08004781 int brace = 0;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004782 int comma = (!m->plus || i < nparam - 1);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004783
H. Peter Anvine2c80182005-01-15 22:15:51 +00004784 t = params[i];
4785 skip_white_(t);
4786 if (tok_is_(t, "{"))
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08004787 t = t->next, brace++, comma = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004788 params[i] = t;
4789 paramlen[i] = 0;
4790 while (t) {
4791 if (comma && t->type == TOK_OTHER && !strcmp(t->text, ","))
4792 break; /* ... because we have hit a comma */
4793 if (comma && t->type == TOK_WHITESPACE
4794 && tok_is_(t->next, ","))
4795 break; /* ... or a space then a comma */
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08004796 if (brace && t->type == TOK_OTHER) {
4797 if (t->text[0] == '{')
4798 brace++; /* ... or a nested opening brace */
4799 else if (t->text[0] == '}')
4800 if (!--brace)
4801 break; /* ... or a brace */
4802 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00004803 t = t->next;
4804 paramlen[i]++;
4805 }
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08004806 if (brace)
H. Peter Anvin130736c2016-02-17 20:27:41 -08004807 nasm_error(ERR_NONFATAL, "macro params should be enclosed in braces");
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004808 }
4809
4810 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004811 * OK, we have a MMacro structure together with a set of
4812 * parameters. We must now go through the expansion and push
4813 * copies of each Line on to istk->expansion. Substitution of
H. Peter Anvin76690a12002-04-30 20:52:49 +00004814 * parameter tokens and macro-local tokens doesn't get done
4815 * until the single-line macro substitution process; this is
4816 * because delaying them allows us to change the semantics
4817 * later through %rotate.
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004818 *
4819 * First, push an end marker on to istk->expansion, mark this
4820 * macro as in progress, and set up its invocation-specific
4821 * variables.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004822 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004823 ll = nasm_malloc(sizeof(Line));
4824 ll->next = istk->expansion;
4825 ll->finishes = m;
4826 ll->first = NULL;
4827 istk->expansion = ll;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004828
4829 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004830 * Save the previous MMacro expansion in the case of
4831 * macro recursion
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004832 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004833 if (m->max_depth && m->in_progress)
4834 push_mmacro(m);
4835
4836 m->in_progress ++;
4837 m->params = params;
4838 m->iline = tline;
4839 m->nparam = nparam;
4840 m->rotate = 0;
4841 m->paramlen = paramlen;
4842 m->unique = unique++;
4843 m->lineno = 0;
4844 m->condcnt = 0;
4845
4846 m->next_active = istk->mstk;
4847 istk->mstk = m;
4848
4849 list_for_each(l, m->expansion) {
4850 Token **tail;
4851
4852 ll = nasm_malloc(sizeof(Line));
4853 ll->finishes = NULL;
4854 ll->next = istk->expansion;
4855 istk->expansion = ll;
4856 tail = &ll->first;
4857
4858 list_for_each(t, l->first) {
4859 Token *x = t;
4860 switch (t->type) {
4861 case TOK_PREPROC_Q:
4862 tt = *tail = new_Token(NULL, TOK_ID, mname, 0);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004863 break;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004864 case TOK_PREPROC_QQ:
4865 tt = *tail = new_Token(NULL, TOK_ID, m->name, 0);
4866 break;
4867 case TOK_PREPROC_ID:
4868 if (t->text[1] == '0' && t->text[2] == '0') {
4869 dont_prepend = -1;
4870 x = label;
4871 if (!x)
4872 continue;
4873 }
4874 /* fall through */
4875 default:
4876 tt = *tail = new_Token(NULL, x->type, x->text, 0);
4877 break;
4878 }
4879 tail = &tt->next;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004880 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004881 *tail = NULL;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004882 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004883
4884 /*
H. Peter Anvineba20a72002-04-30 20:53:55 +00004885 * If we had a label, push it on as the first line of
4886 * the macro expansion.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004887 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004888 if (label) {
4889 if (dont_prepend < 0)
4890 free_tlist(startline);
4891 else {
4892 ll = nasm_malloc(sizeof(Line));
4893 ll->finishes = NULL;
4894 ll->next = istk->expansion;
4895 istk->expansion = ll;
4896 ll->first = startline;
4897 if (!dont_prepend) {
4898 while (label->next)
4899 label = label->next;
4900 label->next = tt = new_Token(NULL, TOK_OTHER, ":", 0);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004901 }
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004902 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004903 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004904
H. Peter Anvin8ac25aa2016-02-18 01:16:18 -08004905 lfmt->uplevel(m->nolist ? LIST_MACRO_NOLIST : LIST_MACRO);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004906
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004907 return 1;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004908}
4909
H. Peter Anvin130736c2016-02-17 20:27:41 -08004910/*
4911 * This function adds macro names to error messages, and suppresses
4912 * them if necessary.
4913 */
4914static void pp_verror(int severity, const char *fmt, va_list arg)
Victor van den Elzen3b404c02008-09-18 13:51:36 +02004915{
H. Peter Anvin130736c2016-02-17 20:27:41 -08004916 char buff[BUFSIZ];
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004917 MMacro *mmac = NULL;
4918 int delta = 0;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02004919
H. Peter Anvin130736c2016-02-17 20:27:41 -08004920 /*
4921 * If we're in a dead branch of IF or something like it, ignore the error.
4922 * However, because %else etc are evaluated in the state context
4923 * of the previous branch, errors might get lost:
4924 * %if 0 ... %else trailing garbage ... %endif
4925 * So %else etc should set the ERR_PP_PRECOND flag.
4926 */
4927 if ((severity & ERR_MASK) < ERR_FATAL &&
4928 istk && istk->conds &&
4929 ((severity & ERR_PP_PRECOND) ?
4930 istk->conds->state == COND_NEVER :
H. Peter Anvineb6653f2016-04-05 13:03:10 -07004931 !emitting(istk->conds->state)))
H. Peter Anvin130736c2016-02-17 20:27:41 -08004932 return;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02004933
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004934 /* get %macro name */
H. Peter Anvin130736c2016-02-17 20:27:41 -08004935 if (!(severity & ERR_NOFILE) && istk && istk->mstk) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004936 mmac = istk->mstk;
4937 /* but %rep blocks should be skipped */
4938 while (mmac && !mmac->name)
4939 mmac = mmac->next_active, delta++;
Cyrill Gorcunov9900c6b2011-10-09 18:58:46 +04004940 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004941
H. Peter Anvin130736c2016-02-17 20:27:41 -08004942 if (mmac) {
4943 vsnprintf(buff, sizeof(buff), fmt, arg);
Victor van den Elzen3b404c02008-09-18 13:51:36 +02004944
H. Peter Anvin130736c2016-02-17 20:27:41 -08004945 nasm_set_verror(real_verror);
4946 nasm_error(severity, "(%s:%d) %s",
4947 mmac->name, mmac->lineno - delta, buff);
4948 nasm_set_verror(pp_verror);
4949 } else {
4950 real_verror(severity, fmt, arg);
4951 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004952}
4953
H. Peter Anvin734b1882002-04-30 21:01:08 +00004954static void
H. Peter Anvin130736c2016-02-17 20:27:41 -08004955pp_reset(char *file, int apass, StrList **deplist)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004956{
H. Peter Anvin7383b402008-09-24 10:20:40 -07004957 Token *t;
4958
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004959 cstk = NULL;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004960 istk = nasm_malloc(sizeof(Include));
4961 istk->next = NULL;
4962 istk->conds = NULL;
4963 istk->expansion = NULL;
4964 istk->mstk = NULL;
H. Peter Anvin3e83cec2016-05-25 04:28:46 -07004965 istk->fp = nasm_open_read(file, NF_TEXT);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004966 istk->fname = NULL;
H. Peter Anvin274cda82016-05-10 02:56:29 -07004967 src_set(0, file);
H. Peter Anvineba20a72002-04-30 20:53:55 +00004968 istk->lineinc = 1;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004969 if (!istk->fp)
H. Peter Anvin130736c2016-02-17 20:27:41 -08004970 nasm_fatal(ERR_NOFILE, "unable to open input file `%s'", file);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004971 defining = NULL;
Charles Crayned4200be2008-07-12 16:42:33 -07004972 nested_mac_count = 0;
4973 nested_rep_count = 0;
H. Peter Anvin97a23472007-09-16 17:57:25 -07004974 init_macros();
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004975 unique = 0;
H. Peter Anvinf7606612016-07-13 14:23:48 -07004976
4977 if (tasm_compatible_mode)
4978 pp_add_stdmac(nasm_stdmac_tasm);
4979
4980 pp_add_stdmac(nasm_stdmac_nasm);
4981 pp_add_stdmac(nasm_stdmac_version);
4982
Cyrill Gorcunov15ce78f2017-01-06 20:21:28 +03004983 if (extrastdmac)
4984 pp_add_stdmac(extrastdmac);
4985
H. Peter Anvinf7606612016-07-13 14:23:48 -07004986 stdmacpos = stdmacros[0];
4987 stdmacnext = &stdmacros[1];
4988
H. Peter Anvind2456592008-06-19 15:04:18 -07004989 do_predef = true;
H. Peter Anvin61f130f2008-09-25 15:45:06 -07004990
4991 /*
4992 * 0 for dependencies, 1 for preparatory passes, 2 for final pass.
4993 * The caller, however, will also pass in 3 for preprocess-only so
4994 * we can set __PASS__ accordingly.
4995 */
4996 pass = apass > 2 ? 2 : apass;
4997
H. Peter Anvin9924d1e2016-10-04 00:59:39 -07004998 dephead = deplist;
H. Peter Anvin436e3672016-10-04 01:12:28 -07004999 nasm_add_string_to_strlist(dephead, file);
H. Peter Anvin9924d1e2016-10-04 00:59:39 -07005000
H. Peter Anvin61f130f2008-09-25 15:45:06 -07005001 /*
5002 * Define the __PASS__ macro. This is defined here unlike
5003 * all the other builtins, because it is special -- it varies between
5004 * passes.
5005 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005006 t = nasm_malloc(sizeof(*t));
5007 t->next = NULL;
H. Peter Anvin61f130f2008-09-25 15:45:06 -07005008 make_tok_num(t, apass);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005009 t->a.mac = NULL;
H. Peter Anvin7383b402008-09-24 10:20:40 -07005010 define_smacro(NULL, "__PASS__", true, 0, t);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005011}
5012
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07005013static void pp_init(void)
5014{
5015 hash_init(&FileHash, HASH_MEDIUM);
5016}
5017
Keith Kaniosa6dfa782007-04-13 16:47:53 +00005018static char *pp_getline(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005019{
Keith Kaniosa6dfa782007-04-13 16:47:53 +00005020 char *line;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005021 Token *tline;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005022
H. Peter Anvin130736c2016-02-17 20:27:41 -08005023 real_verror = nasm_set_verror(pp_verror);
H. Peter Anvin215186f2016-02-17 20:27:41 -08005024
H. Peter Anvine2c80182005-01-15 22:15:51 +00005025 while (1) {
5026 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005027 * Fetch a tokenized line, either from the macro-expansion
H. Peter Anvine2c80182005-01-15 22:15:51 +00005028 * buffer or from the input file.
5029 */
5030 tline = NULL;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005031 while (istk->expansion && istk->expansion->finishes) {
5032 Line *l = istk->expansion;
5033 if (!l->finishes->name && l->finishes->in_progress > 1) {
5034 Line *ll;
H. Peter Anvineba20a72002-04-30 20:53:55 +00005035
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005036 /*
5037 * This is a macro-end marker for a macro with no
5038 * name, which means it's not really a macro at all
5039 * but a %rep block, and the `in_progress' field is
5040 * more than 1, meaning that we still need to
5041 * repeat. (1 means the natural last repetition; 0
5042 * means termination by %exitrep.) We have
5043 * therefore expanded up to the %endrep, and must
5044 * push the whole block on to the expansion buffer
5045 * again. We don't bother to remove the macro-end
5046 * marker: we'd only have to generate another one
5047 * if we did.
5048 */
5049 l->finishes->in_progress--;
5050 list_for_each(l, l->finishes->expansion) {
5051 Token *t, *tt, **tail;
5052
5053 ll = nasm_malloc(sizeof(Line));
5054 ll->next = istk->expansion;
5055 ll->finishes = NULL;
5056 ll->first = NULL;
5057 tail = &ll->first;
5058
5059 list_for_each(t, l->first) {
5060 if (t->text || t->type == TOK_WHITESPACE) {
5061 tt = *tail = new_Token(NULL, t->type, t->text, 0);
5062 tail = &tt->next;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005063 }
5064 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005065
5066 istk->expansion = ll;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005067 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005068 } else {
5069 /*
5070 * Check whether a `%rep' was started and not ended
5071 * within this macro expansion. This can happen and
5072 * should be detected. It's a fatal error because
5073 * I'm too confused to work out how to recover
5074 * sensibly from it.
5075 */
5076 if (defining) {
5077 if (defining->name)
H. Peter Anvin130736c2016-02-17 20:27:41 -08005078 nasm_panic(0, "defining with name in expansion");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005079 else if (istk->mstk->name)
H. Peter Anvin130736c2016-02-17 20:27:41 -08005080 nasm_fatal(0, "`%%rep' without `%%endrep' within"
5081 " expansion of macro `%s'",
5082 istk->mstk->name);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005083 }
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005084
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005085 /*
5086 * FIXME: investigate the relationship at this point between
5087 * istk->mstk and l->finishes
5088 */
5089 {
5090 MMacro *m = istk->mstk;
5091 istk->mstk = m->next_active;
5092 if (m->name) {
5093 /*
5094 * This was a real macro call, not a %rep, and
5095 * therefore the parameter information needs to
5096 * be freed.
5097 */
5098 if (m->prev) {
5099 pop_mmacro(m);
5100 l->finishes->in_progress --;
5101 } else {
5102 nasm_free(m->params);
5103 free_tlist(m->iline);
5104 nasm_free(m->paramlen);
5105 l->finishes->in_progress = 0;
5106 }
Adam Majer91e72402017-07-25 10:42:01 +02005107 }
5108
5109 /*
5110 * FIXME It is incorrect to always free_mmacro here.
5111 * It leads to usage-after-free.
5112 *
5113 * https://bugzilla.nasm.us/show_bug.cgi?id=3392414
5114 */
5115#if 0
5116 else
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005117 free_mmacro(m);
Adam Majer91e72402017-07-25 10:42:01 +02005118#endif
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005119 }
5120 istk->expansion = l->next;
5121 nasm_free(l);
H. Peter Anvin8ac25aa2016-02-18 01:16:18 -08005122 lfmt->downlevel(LIST_MACRO);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005123 }
5124 }
5125 while (1) { /* until we get a line we can use */
5126
5127 if (istk->expansion) { /* from a macro expansion */
5128 char *p;
5129 Line *l = istk->expansion;
5130 if (istk->mstk)
5131 istk->mstk->lineno++;
5132 tline = l->first;
5133 istk->expansion = l->next;
5134 nasm_free(l);
5135 p = detoken(tline, false);
H. Peter Anvin8ac25aa2016-02-18 01:16:18 -08005136 lfmt->line(LIST_MACRO, p);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005137 nasm_free(p);
5138 break;
5139 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00005140 line = read_line();
5141 if (line) { /* from the current input file */
5142 line = prepreproc(line);
Keith Kaniosb7a89542007-04-12 02:40:54 +00005143 tline = tokenize(line);
H. Peter Anvine2c80182005-01-15 22:15:51 +00005144 nasm_free(line);
5145 break;
5146 }
5147 /*
5148 * The current file has ended; work down the istk
5149 */
5150 {
5151 Include *i = istk;
5152 fclose(i->fp);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005153 if (i->conds) {
5154 /* nasm_error can't be conditionally suppressed */
H. Peter Anvin41087062016-03-03 14:27:34 -08005155 nasm_fatal(0,
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005156 "expected `%%endif' before end of file");
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005157 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00005158 /* only set line and file name if there's a next node */
H. Peter Anvin274cda82016-05-10 02:56:29 -07005159 if (i->next)
5160 src_set(i->lineno, i->fname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00005161 istk = i->next;
H. Peter Anvin8ac25aa2016-02-18 01:16:18 -08005162 lfmt->downlevel(LIST_INCLUDE);
H. Peter Anvine2c80182005-01-15 22:15:51 +00005163 nasm_free(i);
H. Peter Anvin130736c2016-02-17 20:27:41 -08005164 if (!istk) {
5165 line = NULL;
5166 goto done;
5167 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005168 if (istk->expansion && istk->expansion->finishes)
5169 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005170 }
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005171 }
5172
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005173 /*
5174 * We must expand MMacro parameters and MMacro-local labels
5175 * _before_ we plunge into directive processing, to cope
5176 * with things like `%define something %1' such as STRUC
5177 * uses. Unless we're _defining_ a MMacro, in which case
5178 * those tokens should be left alone to go into the
5179 * definition; and unless we're in a non-emitting
5180 * condition, in which case we don't want to meddle with
5181 * anything.
5182 */
5183 if (!defining && !(istk->conds && !emitting(istk->conds->state))
5184 && !(istk->mstk && !istk->mstk->in_progress)) {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005185 tline = expand_mmac_params(tline);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005186 }
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005187
H. Peter Anvine2c80182005-01-15 22:15:51 +00005188 /*
5189 * Check the line to see if it's a preprocessor directive.
5190 */
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07005191 if (do_directive(tline, &line) == DIRECTIVE_FOUND) {
5192 if (line)
5193 break; /* Directive generated output */
5194 else
5195 continue;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005196 } else if (defining) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00005197 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005198 * We're defining a multi-line macro. We emit nothing
5199 * at all, and just
5200 * shove the tokenized line on to the macro definition.
H. Peter Anvine2c80182005-01-15 22:15:51 +00005201 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005202 Line *l = nasm_malloc(sizeof(Line));
5203 l->next = defining->expansion;
5204 l->first = tline;
5205 l->finishes = NULL;
5206 defining->expansion = l;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005207 continue;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005208 } else if (istk->conds && !emitting(istk->conds->state)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00005209 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005210 * We're in a non-emitting branch of a condition block.
H. Peter Anvine2c80182005-01-15 22:15:51 +00005211 * Emit nothing at all, not even a blank line: when we
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005212 * emerge from the condition we'll give a line-number
H. Peter Anvine2c80182005-01-15 22:15:51 +00005213 * directive so we keep our place correctly.
5214 */
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005215 free_tlist(tline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00005216 continue;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005217 } else if (istk->mstk && !istk->mstk->in_progress) {
5218 /*
5219 * We're in a %rep block which has been terminated, so
5220 * we're walking through to the %endrep without
5221 * emitting anything. Emit nothing at all, not even a
5222 * blank line: when we emerge from the %rep block we'll
5223 * give a line-number directive so we keep our place
5224 * correctly.
5225 */
5226 free_tlist(tline);
5227 continue;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005228 } else {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005229 tline = expand_smacro(tline);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005230 if (!expand_mmacro(tline)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00005231 /*
Keith Kaniosb7a89542007-04-12 02:40:54 +00005232 * De-tokenize the line again, and emit it.
H. Peter Anvine2c80182005-01-15 22:15:51 +00005233 */
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005234 line = detoken(tline, true);
5235 free_tlist(tline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00005236 break;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005237 } else {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005238 continue; /* expand_mmacro calls free_tlist */
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005239 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00005240 }
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005241 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005242
H. Peter Anvin130736c2016-02-17 20:27:41 -08005243done:
5244 nasm_set_verror(real_verror);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005245 return line;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005246}
5247
H. Peter Anvine2c80182005-01-15 22:15:51 +00005248static void pp_cleanup(int pass)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005249{
H. Peter Anvin130736c2016-02-17 20:27:41 -08005250 real_verror = nasm_set_verror(pp_verror);
H. Peter Anvin215186f2016-02-17 20:27:41 -08005251
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005252 if (defining) {
5253 if (defining->name) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08005254 nasm_error(ERR_NONFATAL,
5255 "end of file while still defining macro `%s'",
5256 defining->name);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005257 } else {
H. Peter Anvin130736c2016-02-17 20:27:41 -08005258 nasm_error(ERR_NONFATAL, "end of file while still in %%rep");
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005259 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005260
5261 free_mmacro(defining);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005262 defining = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005263 }
H. Peter Anvin130736c2016-02-17 20:27:41 -08005264
5265 nasm_set_verror(real_verror);
H. Peter Anvin215186f2016-02-17 20:27:41 -08005266
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005267 while (cstk)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005268 ctx_pop();
H. Peter Anvin97a23472007-09-16 17:57:25 -07005269 free_macros();
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005270 while (istk) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00005271 Include *i = istk;
5272 istk = istk->next;
5273 fclose(i->fp);
Cyrill Gorcunov8dcfd882011-03-03 09:18:56 +03005274 nasm_free(i);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005275 }
5276 while (cstk)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005277 ctx_pop();
H. Peter Anvin274cda82016-05-10 02:56:29 -07005278 src_set_fname(NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00005279 if (pass == 0) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03005280 IncPath *i;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005281 free_llist(predef);
Cyrill Gorcunovdae24d72014-06-28 10:17:39 +04005282 predef = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005283 delete_Blocks();
Cyrill Gorcunovdae24d72014-06-28 10:17:39 +04005284 freeTokens = NULL;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03005285 while ((i = ipath)) {
5286 ipath = i->next;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005287 if (i->path)
5288 nasm_free(i->path);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03005289 nasm_free(i);
5290 }
H. Peter Anvin11dfa1a2008-07-02 18:11:04 -07005291 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005292}
5293
Cyrill Gorcunov0b78bff2012-05-07 01:57:55 +04005294static void pp_include_path(char *path)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005295{
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005296 IncPath *i;
H. Peter Anvin37a321f2007-09-24 13:41:58 -07005297
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005298 i = nasm_malloc(sizeof(IncPath));
5299 i->path = path ? nasm_strdup(path) : NULL;
5300 i->next = NULL;
Frank Kotlerd0ed6fd2003-08-27 11:33:56 +00005301
H. Peter Anvin89cee572009-07-15 09:16:54 -04005302 if (ipath) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00005303 IncPath *j = ipath;
H. Peter Anvin89cee572009-07-15 09:16:54 -04005304 while (j->next)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005305 j = j->next;
5306 j->next = i;
5307 } else {
5308 ipath = i;
Frank Kotlerd0ed6fd2003-08-27 11:33:56 +00005309 }
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005310}
Frank Kotlerd0ed6fd2003-08-27 11:33:56 +00005311
Cyrill Gorcunov0b78bff2012-05-07 01:57:55 +04005312static void pp_pre_include(char *fname)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005313{
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005314 Token *inc, *space, *name;
5315 Line *l;
5316
H. Peter Anvin734b1882002-04-30 21:01:08 +00005317 name = new_Token(NULL, TOK_INTERNAL_STRING, fname, 0);
5318 space = new_Token(name, TOK_WHITESPACE, NULL, 0);
5319 inc = new_Token(space, TOK_PREPROC_ID, "%include", 0);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005320
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005321 l = nasm_malloc(sizeof(Line));
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005322 l->next = predef;
5323 l->first = inc;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005324 l->finishes = NULL;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005325 predef = l;
5326}
5327
Cyrill Gorcunov0b78bff2012-05-07 01:57:55 +04005328static void pp_pre_define(char *definition)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005329{
5330 Token *def, *space;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005331 Line *l;
Keith Kaniosa6dfa782007-04-13 16:47:53 +00005332 char *equals;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005333
H. Peter Anvin130736c2016-02-17 20:27:41 -08005334 real_verror = nasm_set_verror(pp_verror);
H. Peter Anvin215186f2016-02-17 20:27:41 -08005335
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005336 equals = strchr(definition, '=');
H. Peter Anvin734b1882002-04-30 21:01:08 +00005337 space = new_Token(NULL, TOK_WHITESPACE, NULL, 0);
5338 def = new_Token(space, TOK_PREPROC_ID, "%define", 0);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005339 if (equals)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005340 *equals = ' ';
Keith Kaniosb7a89542007-04-12 02:40:54 +00005341 space->next = tokenize(definition);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005342 if (equals)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005343 *equals = '=';
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005344
Cyrill Gorcunov6d42e9b2015-02-08 11:07:17 +03005345 if (space->next->type != TOK_PREPROC_ID &&
5346 space->next->type != TOK_ID)
H. Peter Anvin130736c2016-02-17 20:27:41 -08005347 nasm_error(ERR_WARNING, "pre-defining non ID `%s\'\n", definition);
Cyrill Gorcunov6d42e9b2015-02-08 11:07:17 +03005348
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005349 l = nasm_malloc(sizeof(Line));
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005350 l->next = predef;
5351 l->first = def;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005352 l->finishes = NULL;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005353 predef = l;
H. Peter Anvin130736c2016-02-17 20:27:41 -08005354
5355 nasm_set_verror(real_verror);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005356}
5357
Cyrill Gorcunov0b78bff2012-05-07 01:57:55 +04005358static void pp_pre_undefine(char *definition)
H. Peter Anvin620515a2002-04-30 20:57:38 +00005359{
5360 Token *def, *space;
5361 Line *l;
5362
H. Peter Anvin734b1882002-04-30 21:01:08 +00005363 space = new_Token(NULL, TOK_WHITESPACE, NULL, 0);
5364 def = new_Token(space, TOK_PREPROC_ID, "%undef", 0);
Keith Kaniosb7a89542007-04-12 02:40:54 +00005365 space->next = tokenize(definition);
H. Peter Anvin620515a2002-04-30 20:57:38 +00005366
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005367 l = nasm_malloc(sizeof(Line));
H. Peter Anvin620515a2002-04-30 20:57:38 +00005368 l->next = predef;
5369 l->first = def;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005370 l->finishes = NULL;
H. Peter Anvin620515a2002-04-30 20:57:38 +00005371 predef = l;
5372}
5373
H. Peter Anvinf7606612016-07-13 14:23:48 -07005374static void pp_add_stdmac(macros_t *macros)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005375{
H. Peter Anvinf7606612016-07-13 14:23:48 -07005376 macros_t **mp;
5377
5378 /* Find the end of the list and avoid duplicates */
5379 for (mp = stdmacros; *mp; mp++) {
5380 if (*mp == macros)
5381 return; /* Nothing to do */
5382 }
5383
5384 nasm_assert(mp < &stdmacros[ARRAY_SIZE(stdmacros)-1]);
5385
5386 *mp = macros;
H. Peter Anvin76690a12002-04-30 20:52:49 +00005387}
5388
Cyrill Gorcunov15ce78f2017-01-06 20:21:28 +03005389static void pp_extra_stdmac(macros_t *macros)
5390{
5391 extrastdmac = macros;
5392}
5393
Keith Kaniosa5fc6462007-10-13 07:09:22 -07005394static void make_tok_num(Token * tok, int64_t val)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005395{
Cyrill Gorcunovce652742013-05-06 23:43:43 +04005396 char numbuf[32];
Keith Kaniosa5fc6462007-10-13 07:09:22 -07005397 snprintf(numbuf, sizeof(numbuf), "%"PRId64"", val);
H. Peter Anvineba20a72002-04-30 20:53:55 +00005398 tok->text = nasm_strdup(numbuf);
5399 tok->type = TOK_NUMBER;
5400}
5401
H. Peter Anvin37368952016-05-09 14:10:32 -07005402static void pp_list_one_macro(MMacro *m, int severity)
5403{
5404 if (!m)
5405 return;
5406
5407 /* We need to print the next_active list in reverse order */
5408 pp_list_one_macro(m->next_active, severity);
5409
5410 if (m->name && !m->nolist) {
H. Peter Anvin274cda82016-05-10 02:56:29 -07005411 src_set(m->xline + m->lineno, m->fname);
H. Peter Anvin37368952016-05-09 14:10:32 -07005412 nasm_error(severity, "... from macro `%s' defined here", m->name);
5413 }
5414}
5415
H. Peter Anvin4def1a82016-05-09 13:59:44 -07005416static void pp_error_list_macros(int severity)
5417{
H. Peter Anvin4def1a82016-05-09 13:59:44 -07005418 int32_t saved_line;
5419 const char *saved_fname = NULL;
5420
5421 severity |= ERR_PP_LISTMACRO | ERR_NO_SEVERITY;
H. Peter Anvin274cda82016-05-10 02:56:29 -07005422 src_get(&saved_line, &saved_fname);
H. Peter Anvin4def1a82016-05-09 13:59:44 -07005423
Cyrill Gorcunov771d04e2016-05-10 23:27:03 +03005424 if (istk)
5425 pp_list_one_macro(istk->mstk, severity);
H. Peter Anvin4def1a82016-05-09 13:59:44 -07005426
H. Peter Anvin274cda82016-05-10 02:56:29 -07005427 src_set(saved_line, saved_fname);
H. Peter Anvin4def1a82016-05-09 13:59:44 -07005428}
5429
H. Peter Anvine7469712016-02-18 02:20:59 -08005430const struct preproc_ops nasmpp = {
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07005431 pp_init,
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005432 pp_reset,
5433 pp_getline,
Cyrill Gorcunov0b78bff2012-05-07 01:57:55 +04005434 pp_cleanup,
Cyrill Gorcunov15ce78f2017-01-06 20:21:28 +03005435 pp_extra_stdmac,
Cyrill Gorcunov0b78bff2012-05-07 01:57:55 +04005436 pp_pre_define,
5437 pp_pre_undefine,
5438 pp_pre_include,
H. Peter Anvin4def1a82016-05-09 13:59:44 -07005439 pp_include_path,
5440 pp_error_list_macros,
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005441};