blob: 78263fb3457c6ec4add85ce403a9a2d3da85e34e [file] [log] [blame]
H. Peter Anvin9e6747c2009-06-28 17:13:04 -07001/* ----------------------------------------------------------------------- *
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002 *
H. Peter Anvin215186f2016-02-17 20:27:41 -08003 * Copyright 1996-2016 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>
Keith Kaniosb7a89542007-04-12 02:40:54 +000072#include <inttypes.h>
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000073
74#include "nasm.h"
75#include "nasmlib.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 Anvin172b8402016-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
159 char *fname; /* File where defined */
160 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;
274 char *fname;
275 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 Anvind7ed89e2002-04-30 20:52:08 +0000290 * Conditional assembly: we maintain a separate stack of these for
291 * each level of file inclusion. (The only reason we keep the
292 * stacks separate is to ensure that a stray `%endif' in a file
293 * included from within the true branch of a `%if' won't terminate
294 * it and cause confusion: instead, rightly, it'll cause an error.)
295 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800296struct Cond {
297 Cond *next;
298 int state;
299};
H. Peter Anvine2c80182005-01-15 22:15:51 +0000300enum {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000301 /*
302 * These states are for use just after %if or %elif: IF_TRUE
303 * means the condition has evaluated to truth so we are
304 * currently emitting, whereas IF_FALSE means we are not
305 * currently emitting but will start doing so if a %else comes
306 * up. In these states, all directives are admissible: %elif,
307 * %else and %endif. (And of course %if.)
308 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800309 COND_IF_TRUE, COND_IF_FALSE,
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000310 /*
311 * These states come up after a %else: ELSE_TRUE means we're
312 * emitting, and ELSE_FALSE means we're not. In ELSE_* states,
313 * any %elif or %else will cause an error.
314 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800315 COND_ELSE_TRUE, COND_ELSE_FALSE,
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000316 /*
Victor van den Elzen3b404c02008-09-18 13:51:36 +0200317 * These states mean that we're not emitting now, and also that
318 * nothing until %endif will be emitted at all. COND_DONE is
319 * used when we've had our moment of emission
320 * and have now started seeing %elifs. COND_NEVER is used when
321 * the condition construct in question is contained within a
322 * non-emitting branch of a larger condition construct,
323 * or if there is an error.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000324 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800325 COND_DONE, COND_NEVER
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000326};
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800327#define emitting(x) ( (x) == COND_IF_TRUE || (x) == COND_ELSE_TRUE )
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000328
H. Peter Anvin70653092007-10-19 14:42:29 -0700329/*
Ed Beroset3ab3f412002-06-11 03:31:49 +0000330 * These defines are used as the possible return values for do_directive
331 */
332#define NO_DIRECTIVE_FOUND 0
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300333#define DIRECTIVE_FOUND 1
Ed Beroset3ab3f412002-06-11 03:31:49 +0000334
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000335/*
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800336 * This define sets the upper limit for smacro and recursive mmacro
337 * expansions
Keith Kanios852f1ee2009-07-12 00:19:55 -0500338 */
339#define DEADMAN_LIMIT (1 << 20)
340
Cyrill Gorcunove091d6e2010-08-09 13:58:22 +0400341/* max reps */
342#define REP_LIMIT ((INT64_C(1) << 62))
343
Keith Kanios852f1ee2009-07-12 00:19:55 -0500344/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000345 * Condition codes. Note that we use c_ prefix not C_ because C_ is
346 * used in nasm.h for the "real" condition codes. At _this_ level,
347 * we treat CXZ and ECXZ as condition codes, albeit non-invertible
348 * ones, so we need a different enum...
349 */
H. Peter Anvin476d2862007-10-02 22:04:15 -0700350static const char * const conditions[] = {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000351 "a", "ae", "b", "be", "c", "cxz", "e", "ecxz", "g", "ge", "l", "le",
352 "na", "nae", "nb", "nbe", "nc", "ne", "ng", "nge", "nl", "nle", "no",
H. Peter Anvince9be342007-09-12 00:22:29 +0000353 "np", "ns", "nz", "o", "p", "pe", "po", "rcxz", "s", "z"
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000354};
H. Peter Anvin476d2862007-10-02 22:04:15 -0700355enum pp_conds {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000356 c_A, c_AE, c_B, c_BE, c_C, c_CXZ, c_E, c_ECXZ, c_G, c_GE, c_L, c_LE,
357 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 -0700358 c_NP, c_NS, c_NZ, c_O, c_P, c_PE, c_PO, c_RCXZ, c_S, c_Z,
359 c_none = -1
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000360};
H. Peter Anvin476d2862007-10-02 22:04:15 -0700361static const enum pp_conds inverse_ccs[] = {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000362 c_NA, c_NAE, c_NB, c_NBE, c_NC, -1, c_NE, -1, c_NG, c_NGE, c_NL, c_NLE,
363 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 +0000364 c_Z, c_NO, c_NP, c_PO, c_PE, -1, c_NS, c_NZ
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000365};
366
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800367/*
368 * Directive names.
369 */
370/* If this is a an IF, ELIF, ELSE or ENDIF keyword */
371static int is_condition(enum preproc_token arg)
372{
373 return PP_IS_COND(arg) || (arg == PP_ELSE) || (arg == PP_ENDIF);
374}
375
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000376/* For TASM compatibility we need to be able to recognise TASM compatible
377 * conditional compilation directives. Using the NASM pre-processor does
378 * not work, so we look for them specifically from the following list and
379 * then jam in the equivalent NASM directive into the input stream.
380 */
381
H. Peter Anvine2c80182005-01-15 22:15:51 +0000382enum {
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000383 TM_ARG, TM_ELIF, TM_ELSE, TM_ENDIF, TM_IF, TM_IFDEF, TM_IFDIFI,
384 TM_IFNDEF, TM_INCLUDE, TM_LOCAL
385};
386
H. Peter Anvin476d2862007-10-02 22:04:15 -0700387static const char * const tasm_directives[] = {
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000388 "arg", "elif", "else", "endif", "if", "ifdef", "ifdifi",
389 "ifndef", "include", "local"
390};
391
392static int StackSize = 4;
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000393static char *StackPointer = "ebp";
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000394static int ArgOffset = 8;
H. Peter Anvin8781cb02007-11-08 20:01:11 -0800395static int LocalOffset = 0;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000396
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000397static Context *cstk;
398static Include *istk;
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800399static IncPath *ipath = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000400
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300401static int pass; /* HACK: pass 0 = generate dependencies only */
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700402static StrList **dephead, **deptail; /* Dependency list */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000403
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300404static uint64_t unique; /* unique identifier numbers */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000405
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800406static Line *predef = NULL;
H. Peter Anvind2456592008-06-19 15:04:18 -0700407static bool do_predef;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000408
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000409/*
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800410 * The current set of multi-line macros we have defined.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000411 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800412static struct hash_table mmacros;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000413
414/*
415 * The current set of single-line macros we have defined.
416 */
H. Peter Anvin166c2472008-05-28 12:28:58 -0700417static struct hash_table smacros;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000418
419/*
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800420 * The multi-line macro we are currently defining, or the %rep
421 * block we are currently reading, if any.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000422 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800423static MMacro *defining;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000424
Charles Crayned4200be2008-07-12 16:42:33 -0700425static uint64_t nested_mac_count;
426static uint64_t nested_rep_count;
427
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000428/*
429 * The number of macro parameters to allocate space for at a time.
430 */
431#define PARAM_DELTA 16
432
433/*
H. Peter Anvina4835d42008-05-20 14:21:29 -0700434 * The standard macro set: defined in macros.c in the array nasm_stdmac.
435 * This gives our position in the macro set, when we're processing it.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000436 */
H. Peter Anvina70547f2008-07-19 21:44:26 -0700437static macros_t *stdmacpos;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000438
439/*
H. Peter Anvin76690a12002-04-30 20:52:49 +0000440 * The extra standard macros that come from the object format, if
441 * any.
442 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800443static macros_t *extrastdmac = NULL;
H. Peter Anvincfb71762008-06-20 15:20:16 -0700444static bool any_extrastdmac;
H. Peter Anvin76690a12002-04-30 20:52:49 +0000445
446/*
H. Peter Anvin734b1882002-04-30 21:01:08 +0000447 * Tokens are allocated in blocks to improve speed
448 */
449#define TOKEN_BLOCKSIZE 4096
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800450static Token *freeTokens = NULL;
H. Peter Anvince616072002-04-30 21:02:23 +0000451struct Blocks {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000452 Blocks *next;
453 void *chunk;
H. Peter Anvince616072002-04-30 21:02:23 +0000454};
455
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800456static Blocks blocks = { NULL, NULL };
H. Peter Anvin734b1882002-04-30 21:01:08 +0000457
458/*
H. Peter Anvin76690a12002-04-30 20:52:49 +0000459 * Forward declarations.
460 */
H. Peter Anvin734b1882002-04-30 21:01:08 +0000461static Token *expand_mmac_params(Token * tline);
462static Token *expand_smacro(Token * tline);
463static Token *expand_id(Token * tline);
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +0400464static Context *get_ctx(const char *name, const char **namep);
Keith Kaniosa5fc6462007-10-13 07:09:22 -0700465static void make_tok_num(Token * tok, int64_t val);
H. Peter Anvin215186f2016-02-17 20:27:41 -0800466static void pp_verror(int severity, const char *fmt, va_list ap);
467static vefunc real_verror;
H. Peter Anvince616072002-04-30 21:02:23 +0000468static void *new_Block(size_t size);
469static void delete_Blocks(void);
H. Peter Anvinc751e862008-06-09 10:18:45 -0700470static Token *new_Token(Token * next, enum pp_token_type type,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300471 const char *text, int txtlen);
H. Peter Anvin734b1882002-04-30 21:01:08 +0000472static Token *delete_Token(Token * t);
H. Peter Anvineba20a72002-04-30 20:53:55 +0000473
474/*
475 * Macros for safe checking of token pointers, avoid *(NULL)
476 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300477#define tok_type_(x,t) ((x) && (x)->type == (t))
478#define skip_white_(x) if (tok_type_((x), TOK_WHITESPACE)) (x)=(x)->next
479#define tok_is_(x,v) (tok_type_((x), TOK_OTHER) && !strcmp((x)->text,(v)))
480#define tok_isnt_(x,v) ((x) && ((x)->type!=TOK_OTHER || strcmp((x)->text,(v))))
H. Peter Anvin76690a12002-04-30 20:52:49 +0000481
Cyrill Gorcunov194ba892011-06-30 01:16:35 +0400482/*
H. Peter Anvin077fb932010-07-20 14:56:30 -0700483 * nasm_unquote with error if the string contains NUL characters.
484 * If the string contains NUL characters, issue an error and return
485 * the C len, i.e. truncate at the NUL.
486 */
487static size_t nasm_unquote_cstr(char *qstr, enum preproc_token directive)
488{
489 size_t len = nasm_unquote(qstr, NULL);
490 size_t clen = strlen(qstr);
491
492 if (len != clen)
H. Peter Anvin215186f2016-02-17 20:27:41 -0800493 nasm_error(ERR_NONFATAL, "NUL character in `%s' directive",
H. Peter Anvin077fb932010-07-20 14:56:30 -0700494 pp_directives[directive]);
495
496 return clen;
497}
498
499/*
H. Peter Anvinb40992c2010-09-15 08:57:21 -0700500 * In-place reverse a list of tokens.
501 */
502static Token *reverse_tokens(Token *t)
503{
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800504 Token *prev = NULL;
505 Token *next;
H. Peter Anvinb40992c2010-09-15 08:57:21 -0700506
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800507 while (t) {
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +0400508 next = t->next;
509 t->next = prev;
510 prev = t;
511 t = next;
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800512 }
H. Peter Anvinb40992c2010-09-15 08:57:21 -0700513
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800514 return prev;
H. Peter Anvinb40992c2010-09-15 08:57:21 -0700515}
516
517/*
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300518 * Handle TASM specific directives, which do not contain a % in
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000519 * front of them. We do it here because I could not find any other
520 * place to do it for the moment, and it is a hack (ideally it would
521 * be nice to be able to use the NASM pre-processor to do it).
522 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000523static char *check_tasm_directive(char *line)
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000524{
Keith Kaniosb7a89542007-04-12 02:40:54 +0000525 int32_t i, j, k, m, len;
Cyrill Gorcunovf66ac7d2009-10-12 20:41:13 +0400526 char *p, *q, *oldline, oldchar;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000527
Cyrill Gorcunovf66ac7d2009-10-12 20:41:13 +0400528 p = nasm_skip_spaces(line);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000529
530 /* Binary search for the directive name */
531 i = -1;
Cyrill Gorcunova7319242010-06-03 22:04:36 +0400532 j = ARRAY_SIZE(tasm_directives);
Cyrill Gorcunovf66ac7d2009-10-12 20:41:13 +0400533 q = nasm_skip_word(p);
534 len = q - p;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000535 if (len) {
536 oldchar = p[len];
537 p[len] = 0;
538 while (j - i > 1) {
539 k = (j + i) / 2;
540 m = nasm_stricmp(p, tasm_directives[k]);
541 if (m == 0) {
542 /* We have found a directive, so jam a % in front of it
543 * so that NASM will then recognise it as one if it's own.
544 */
545 p[len] = oldchar;
546 len = strlen(p);
547 oldline = line;
548 line = nasm_malloc(len + 2);
549 line[0] = '%';
550 if (k == TM_IFDIFI) {
H. Peter Anvin18f48792009-06-27 15:56:27 -0700551 /*
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300552 * NASM does not recognise IFDIFI, so we convert
553 * it to %if 0. This is not used in NASM
554 * compatible code, but does need to parse for the
555 * TASM macro package.
H. Peter Anvine2c80182005-01-15 22:15:51 +0000556 */
H. Peter Anvin18f48792009-06-27 15:56:27 -0700557 strcpy(line + 1, "if 0");
H. Peter Anvine2c80182005-01-15 22:15:51 +0000558 } else {
559 memcpy(line + 1, p, len + 1);
560 }
561 nasm_free(oldline);
562 return line;
563 } else if (m < 0) {
564 j = k;
565 } else
566 i = k;
567 }
568 p[len] = oldchar;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000569 }
570 return line;
571}
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000572
H. Peter Anvin76690a12002-04-30 20:52:49 +0000573/*
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000574 * The pre-preprocessing stage... This function translates line
575 * number indications as they emerge from GNU cpp (`# lineno "file"
576 * flags') into NASM preprocessor line number indications (`%line
577 * lineno file').
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000578 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000579static char *prepreproc(char *line)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000580{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000581 int lineno, fnlen;
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000582 char *fname, *oldline;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000583
H. Peter Anvine2c80182005-01-15 22:15:51 +0000584 if (line[0] == '#' && line[1] == ' ') {
585 oldline = line;
586 fname = oldline + 2;
587 lineno = atoi(fname);
588 fname += strspn(fname, "0123456789 ");
589 if (*fname == '"')
590 fname++;
591 fnlen = strcspn(fname, "\"");
592 line = nasm_malloc(20 + fnlen);
593 snprintf(line, 20 + fnlen, "%%line %d %.*s", lineno, fnlen, fname);
594 nasm_free(oldline);
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000595 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000596 if (tasm_compatible_mode)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000597 return check_tasm_directive(line);
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000598 return line;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000599}
600
601/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000602 * Free a linked list of tokens.
603 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000604static void free_tlist(Token * list)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000605{
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +0400606 while (list)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000607 list = delete_Token(list);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000608}
609
610/*
611 * Free a linked list of lines.
612 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000613static void free_llist(Line * list)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000614{
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +0400615 Line *l, *tmp;
616 list_for_each_safe(l, tmp, list) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000617 free_tlist(l->first);
618 nasm_free(l);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000619 }
620}
621
622/*
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800623 * Free an MMacro
H. Peter Anvineba20a72002-04-30 20:53:55 +0000624 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800625static void free_mmacro(MMacro * m)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000626{
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800627 nasm_free(m->name);
628 free_tlist(m->dlist);
629 nasm_free(m->defaults);
630 free_llist(m->expansion);
H. Peter Anvin4def1a82016-05-09 13:59:44 -0700631 nasm_free(m->fname);
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800632 nasm_free(m);
H. Peter Anvineba20a72002-04-30 20:53:55 +0000633}
634
635/*
H. Peter Anvin97a23472007-09-16 17:57:25 -0700636 * Free all currently defined macros, and free the hash tables
637 */
H. Peter Anvin072771e2008-05-22 13:17:51 -0700638static void free_smacro_table(struct hash_table *smt)
H. Peter Anvin97a23472007-09-16 17:57:25 -0700639{
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +0400640 SMacro *s, *tmp;
H. Peter Anvin072771e2008-05-22 13:17:51 -0700641 const char *key;
642 struct hash_tbl_node *it = NULL;
H. Peter Anvin97a23472007-09-16 17:57:25 -0700643
H. Peter Anvin072771e2008-05-22 13:17:51 -0700644 while ((s = hash_iterate(smt, &it, &key)) != NULL) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300645 nasm_free((void *)key);
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +0400646 list_for_each_safe(s, tmp, s) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300647 nasm_free(s->name);
648 free_tlist(s->expansion);
649 nasm_free(s);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300650 }
H. Peter Anvin97a23472007-09-16 17:57:25 -0700651 }
H. Peter Anvin072771e2008-05-22 13:17:51 -0700652 hash_free(smt);
653}
654
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800655static void free_mmacro_table(struct hash_table *mmt)
H. Peter Anvin072771e2008-05-22 13:17:51 -0700656{
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800657 MMacro *m, *tmp;
H. Peter Anvin072771e2008-05-22 13:17:51 -0700658 const char *key;
659 struct hash_tbl_node *it = NULL;
H. Peter Anvin97a23472007-09-16 17:57:25 -0700660
661 it = NULL;
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800662 while ((m = hash_iterate(mmt, &it, &key)) != NULL) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300663 nasm_free((void *)key);
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800664 list_for_each_safe(m ,tmp, m)
665 free_mmacro(m);
H. Peter Anvin97a23472007-09-16 17:57:25 -0700666 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800667 hash_free(mmt);
H. Peter Anvin072771e2008-05-22 13:17:51 -0700668}
669
670static void free_macros(void)
671{
H. Peter Anvin166c2472008-05-28 12:28:58 -0700672 free_smacro_table(&smacros);
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800673 free_mmacro_table(&mmacros);
H. Peter Anvin97a23472007-09-16 17:57:25 -0700674}
675
676/*
677 * Initialize the hash tables
678 */
679static void init_macros(void)
680{
H. Peter Anvin166c2472008-05-28 12:28:58 -0700681 hash_init(&smacros, HASH_LARGE);
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800682 hash_init(&mmacros, HASH_LARGE);
H. Peter Anvin97a23472007-09-16 17:57:25 -0700683}
684
685/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000686 * Pop the context stack.
687 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000688static void ctx_pop(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000689{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000690 Context *c = cstk;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000691
692 cstk = cstk->next;
H. Peter Anvin166c2472008-05-28 12:28:58 -0700693 free_smacro_table(&c->localmac);
H. Peter Anvin734b1882002-04-30 21:01:08 +0000694 nasm_free(c->name);
695 nasm_free(c);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000696}
697
H. Peter Anvin072771e2008-05-22 13:17:51 -0700698/*
699 * Search for a key in the hash index; adding it if necessary
700 * (in which case we initialize the data pointer to NULL.)
701 */
702static void **
703hash_findi_add(struct hash_table *hash, const char *str)
704{
705 struct hash_insert hi;
706 void **r;
707 char *strx;
708
709 r = hash_findi(hash, str, &hi);
710 if (r)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300711 return r;
H. Peter Anvin072771e2008-05-22 13:17:51 -0700712
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300713 strx = nasm_strdup(str); /* Use a more efficient allocator here? */
H. Peter Anvin072771e2008-05-22 13:17:51 -0700714 return hash_add(&hi, strx, NULL);
715}
716
717/*
718 * Like hash_findi, but returns the data element rather than a pointer
719 * to it. Used only when not adding a new element, hence no third
720 * argument.
721 */
722static void *
723hash_findix(struct hash_table *hash, const char *str)
724{
725 void **p;
726
727 p = hash_findi(hash, str, NULL);
728 return p ? *p : NULL;
729}
730
Cyrill Gorcunov15bdc512010-07-13 11:27:41 +0400731/*
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800732 * read line from standart macros set,
Cyrill Gorcunov15bdc512010-07-13 11:27:41 +0400733 * if there no more left -- return NULL
734 */
735static char *line_from_stdmac(void)
736{
737 unsigned char c;
738 const unsigned char *p = stdmacpos;
739 char *line, *q;
740 size_t len = 0;
741
742 if (!stdmacpos)
743 return NULL;
744
745 while ((c = *p++)) {
746 if (c >= 0x80)
747 len += pp_directives_len[c - 0x80] + 1;
748 else
749 len++;
750 }
751
752 line = nasm_malloc(len + 1);
753 q = line;
754 while ((c = *stdmacpos++)) {
755 if (c >= 0x80) {
756 memcpy(q, pp_directives[c - 0x80], pp_directives_len[c - 0x80]);
757 q += pp_directives_len[c - 0x80];
758 *q++ = ' ';
759 } else {
760 *q++ = c;
761 }
762 }
763 stdmacpos = p;
764 *q = '\0';
765
766 if (!*stdmacpos) {
767 /* This was the last of the standard macro chain... */
768 stdmacpos = NULL;
769 if (any_extrastdmac) {
770 stdmacpos = extrastdmac;
771 any_extrastdmac = false;
772 } else if (do_predef) {
773 Line *pd, *l;
774 Token *head, **tail, *t;
775
776 /*
777 * Nasty hack: here we push the contents of
778 * `predef' on to the top-level expansion stack,
779 * since this is the most convenient way to
780 * implement the pre-include and pre-define
781 * features.
782 */
783 list_for_each(pd, predef) {
784 head = NULL;
785 tail = &head;
786 list_for_each(t, pd->first) {
787 *tail = new_Token(NULL, t->type, t->text, 0);
788 tail = &(*tail)->next;
789 }
790
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800791 l = nasm_malloc(sizeof(Line));
792 l->next = istk->expansion;
793 l->first = head;
794 l->finishes = NULL;
795
796 istk->expansion = l;
Cyrill Gorcunov15bdc512010-07-13 11:27:41 +0400797 }
798 do_predef = false;
799 }
800 }
801
802 return line;
803}
804
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000805static char *read_line(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000806{
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +0400807 unsigned int size, c, next;
808 const unsigned int delta = 512;
809 const unsigned int pad = 8;
810 unsigned int nr_cont = 0;
811 bool cont = false;
812 char *buffer, *p;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000813
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +0400814 /* Standart macros set (predefined) goes first */
Cyrill Gorcunov15bdc512010-07-13 11:27:41 +0400815 p = line_from_stdmac();
816 if (p)
817 return p;
H. Peter Anvin72edbb82008-06-19 16:00:04 -0700818
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +0400819 size = delta;
820 p = buffer = nasm_malloc(size);
821
822 for (;;) {
823 c = fgetc(istk->fp);
824 if ((int)(c) == EOF) {
825 p[0] = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000826 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000827 }
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +0400828
829 switch (c) {
830 case '\r':
831 next = fgetc(istk->fp);
832 if (next != '\n')
833 ungetc(next, istk->fp);
834 if (cont) {
835 cont = false;
836 continue;
837 }
838 break;
839
840 case '\n':
841 if (cont) {
842 cont = false;
843 continue;
844 }
845 break;
846
847 case '\\':
848 next = fgetc(istk->fp);
849 ungetc(next, istk->fp);
Cyrill Gorcunov490f85e2012-12-27 20:02:17 +0400850 if (next == '\r' || next == '\n') {
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +0400851 cont = true;
852 nr_cont++;
853 continue;
854 }
855 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000856 }
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +0400857
858 if (c == '\r' || c == '\n') {
859 *p++ = 0;
860 break;
861 }
862
863 if (p >= (buffer + size - pad)) {
864 buffer = nasm_realloc(buffer, size + delta);
865 p = buffer + size - pad;
866 size += delta;
867 }
868
869 *p++ = (unsigned char)c;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000870 }
871
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +0400872 if (p == buffer) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000873 nasm_free(buffer);
874 return NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000875 }
876
Cyrill Gorcunova5aea572010-11-11 10:14:45 +0300877 src_set_linnum(src_get_linnum() + istk->lineinc +
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +0400878 (nr_cont * istk->lineinc));
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000879
880 /*
881 * Handle spurious ^Z, which may be inserted into source files
882 * by some file transfer utilities.
883 */
884 buffer[strcspn(buffer, "\032")] = '\0';
885
H. Peter Anvin172b8402016-02-18 01:16:18 -0800886 lfmt->line(LIST_READ, buffer);
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000887
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000888 return buffer;
889}
890
891/*
Keith Kaniosb7a89542007-04-12 02:40:54 +0000892 * Tokenize a line of text. This is a very simple process since we
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000893 * don't need to parse the value out of e.g. numeric tokens: we
894 * simply split one string into many.
895 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000896static Token *tokenize(char *line)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000897{
H. Peter Anvinca544db2008-10-19 19:30:11 -0700898 char c, *p = line;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +0000899 enum pp_token_type type;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000900 Token *list = NULL;
901 Token *t, **tail = &list;
902
H. Peter Anvine2c80182005-01-15 22:15:51 +0000903 while (*line) {
904 p = line;
905 if (*p == '%') {
906 p++;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300907 if (*p == '+' && !nasm_isdigit(p[1])) {
908 p++;
909 type = TOK_PASTE;
910 } else if (nasm_isdigit(*p) ||
911 ((*p == '-' || *p == '+') && nasm_isdigit(p[1]))) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000912 do {
913 p++;
914 }
H. Peter Anvinbda7a6e2008-06-21 10:23:17 -0700915 while (nasm_isdigit(*p));
H. Peter Anvine2c80182005-01-15 22:15:51 +0000916 type = TOK_PREPROC_ID;
917 } else if (*p == '{') {
918 p++;
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800919 while (*p) {
920 if (*p == '}')
921 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000922 p[-1] = *p;
923 p++;
924 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800925 if (*p != '}')
H. Peter Anvin215186f2016-02-17 20:27:41 -0800926 nasm_error(ERR_WARNING | ERR_PASS1,
927 "unterminated %%{ construct");
H. Peter Anvine2c80182005-01-15 22:15:51 +0000928 p[-1] = '\0';
929 if (*p)
930 p++;
931 type = TOK_PREPROC_ID;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300932 } else if (*p == '[') {
933 int lvl = 1;
934 line += 2; /* Skip the leading %[ */
935 p++;
936 while (lvl && (c = *p++)) {
937 switch (c) {
938 case ']':
939 lvl--;
940 break;
941 case '%':
942 if (*p == '[')
943 lvl++;
944 break;
945 case '\'':
946 case '\"':
947 case '`':
Cyrill Gorcunovc6360a72010-07-13 13:32:19 +0400948 p = nasm_skip_string(p - 1) + 1;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300949 break;
950 default:
951 break;
952 }
953 }
954 p--;
955 if (*p)
956 *p++ = '\0';
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800957 if (lvl)
H. Peter Anvin215186f2016-02-17 20:27:41 -0800958 nasm_error(ERR_NONFATAL|ERR_PASS1,
959 "unterminated %%[ construct");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300960 type = TOK_INDIRECT;
961 } else if (*p == '?') {
962 type = TOK_PREPROC_Q; /* %? */
963 p++;
964 if (*p == '?') {
965 type = TOK_PREPROC_QQ; /* %?? */
966 p++;
967 }
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +0400968 } else if (*p == '!') {
969 type = TOK_PREPROC_ID;
970 p++;
971 if (isidchar(*p)) {
972 do {
973 p++;
974 }
975 while (isidchar(*p));
976 } else if (*p == '\'' || *p == '\"' || *p == '`') {
977 p = nasm_skip_string(p);
978 if (*p)
979 p++;
980 else
H. Peter Anvin215186f2016-02-17 20:27:41 -0800981 nasm_error(ERR_NONFATAL|ERR_PASS1,
982 "unterminated %%! string");
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +0400983 } else {
984 /* %! without string or identifier */
985 type = TOK_OTHER; /* Legacy behavior... */
986 }
H. Peter Anvine2c80182005-01-15 22:15:51 +0000987 } else if (isidchar(*p) ||
988 ((*p == '!' || *p == '%' || *p == '$') &&
989 isidchar(p[1]))) {
990 do {
991 p++;
992 }
993 while (isidchar(*p));
994 type = TOK_PREPROC_ID;
995 } else {
996 type = TOK_OTHER;
997 if (*p == '%')
998 p++;
999 }
1000 } else if (isidstart(*p) || (*p == '$' && isidstart(p[1]))) {
1001 type = TOK_ID;
1002 p++;
1003 while (*p && isidchar(*p))
1004 p++;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07001005 } else if (*p == '\'' || *p == '"' || *p == '`') {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001006 /*
1007 * A string token.
1008 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001009 type = TOK_STRING;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001010 p = nasm_skip_string(p);
Nickolay Yurchenkof3b3ce22003-09-21 20:38:43 +00001011
H. Peter Anvine2c80182005-01-15 22:15:51 +00001012 if (*p) {
1013 p++;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001014 } else {
H. Peter Anvin215186f2016-02-17 20:27:41 -08001015 nasm_error(ERR_WARNING|ERR_PASS1, "unterminated string");
H. Peter Anvine2c80182005-01-15 22:15:51 +00001016 /* Handling unterminated strings by UNV */
1017 /* type = -1; */
1018 }
Victor van den Elzenfb5f2512009-04-17 16:17:59 +02001019 } else if (p[0] == '$' && p[1] == '$') {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001020 type = TOK_OTHER; /* TOKEN_BASE */
Victor van den Elzenfb5f2512009-04-17 16:17:59 +02001021 p += 2;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001022 } else if (isnumstart(*p)) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001023 bool is_hex = false;
1024 bool is_float = false;
1025 bool has_e = false;
1026 char c, *r;
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001027
H. Peter Anvine2c80182005-01-15 22:15:51 +00001028 /*
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001029 * A numeric token.
H. Peter Anvine2c80182005-01-15 22:15:51 +00001030 */
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001031
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001032 if (*p == '$') {
1033 p++;
1034 is_hex = true;
1035 }
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001036
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001037 for (;;) {
1038 c = *p++;
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001039
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001040 if (!is_hex && (c == 'e' || c == 'E')) {
1041 has_e = true;
1042 if (*p == '+' || *p == '-') {
1043 /*
1044 * e can only be followed by +/- if it is either a
1045 * prefixed hex number or a floating-point number
1046 */
1047 p++;
1048 is_float = true;
1049 }
1050 } else if (c == 'H' || c == 'h' || c == 'X' || c == 'x') {
1051 is_hex = true;
1052 } else if (c == 'P' || c == 'p') {
1053 is_float = true;
1054 if (*p == '+' || *p == '-')
1055 p++;
1056 } else if (isnumchar(c) || c == '_')
1057 ; /* just advance */
1058 else if (c == '.') {
1059 /*
1060 * we need to deal with consequences of the legacy
1061 * parser, like "1.nolist" being two tokens
1062 * (TOK_NUMBER, TOK_ID) here; at least give it
1063 * a shot for now. In the future, we probably need
1064 * a flex-based scanner with proper pattern matching
1065 * to do it as well as it can be done. Nothing in
1066 * the world is going to help the person who wants
1067 * 0x123.p16 interpreted as two tokens, though.
1068 */
1069 r = p;
1070 while (*r == '_')
1071 r++;
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001072
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001073 if (nasm_isdigit(*r) || (is_hex && nasm_isxdigit(*r)) ||
1074 (!is_hex && (*r == 'e' || *r == 'E')) ||
1075 (*r == 'p' || *r == 'P')) {
1076 p = r;
1077 is_float = true;
1078 } else
1079 break; /* Terminate the token */
1080 } else
1081 break;
1082 }
1083 p--; /* Point to first character beyond number */
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001084
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001085 if (p == line+1 && *line == '$') {
1086 type = TOK_OTHER; /* TOKEN_HERE */
1087 } else {
1088 if (has_e && !is_hex) {
1089 /* 1e13 is floating-point, but 1e13h is not */
1090 is_float = true;
1091 }
H. Peter Anvind784a082009-04-20 14:01:18 -07001092
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001093 type = is_float ? TOK_FLOAT : TOK_NUMBER;
1094 }
H. Peter Anvinbda7a6e2008-06-21 10:23:17 -07001095 } else if (nasm_isspace(*p)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001096 type = TOK_WHITESPACE;
Cyrill Gorcunovf66ac7d2009-10-12 20:41:13 +04001097 p = nasm_skip_spaces(p);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001098 /*
1099 * Whitespace just before end-of-line is discarded by
1100 * pretending it's a comment; whitespace just before a
1101 * comment gets lumped into the comment.
1102 */
1103 if (!*p || *p == ';') {
1104 type = TOK_COMMENT;
1105 while (*p)
1106 p++;
1107 }
1108 } else if (*p == ';') {
1109 type = TOK_COMMENT;
1110 while (*p)
1111 p++;
1112 } else {
1113 /*
1114 * Anything else is an operator of some kind. We check
1115 * for all the double-character operators (>>, <<, //,
1116 * %%, <=, >=, ==, !=, <>, &&, ||, ^^), but anything
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001117 * else is a single-character operator.
H. Peter Anvine2c80182005-01-15 22:15:51 +00001118 */
1119 type = TOK_OTHER;
1120 if ((p[0] == '>' && p[1] == '>') ||
1121 (p[0] == '<' && p[1] == '<') ||
1122 (p[0] == '/' && p[1] == '/') ||
1123 (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++;
1132 }
1133 p++;
1134 }
Nickolay Yurchenkof3b3ce22003-09-21 20:38:43 +00001135
H. Peter Anvine2c80182005-01-15 22:15:51 +00001136 /* Handling unterminated string by UNV */
1137 /*if (type == -1)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001138 {
1139 *tail = t = new_Token(NULL, TOK_STRING, line, p-line+1);
1140 t->text[p-line] = *line;
1141 tail = &t->next;
1142 }
1143 else */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001144 if (type != TOK_COMMENT) {
1145 *tail = t = new_Token(NULL, type, line, p - line);
1146 tail = &t->next;
1147 }
1148 line = p;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001149 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001150 return list;
1151}
1152
H. Peter Anvince616072002-04-30 21:02:23 +00001153/*
1154 * this function allocates a new managed block of memory and
H. Peter Anvin70653092007-10-19 14:42:29 -07001155 * returns a pointer to the block. The managed blocks are
H. Peter Anvince616072002-04-30 21:02:23 +00001156 * deleted only all at once by the delete_Blocks function.
1157 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001158static void *new_Block(size_t size)
H. Peter Anvince616072002-04-30 21:02:23 +00001159{
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001160 Blocks *b = &blocks;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001161
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001162 /* first, get to the end of the linked list */
1163 while (b->next)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001164 b = b->next;
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001165 /* now allocate the requested chunk */
1166 b->chunk = nasm_malloc(size);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001167
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001168 /* now allocate a new block for the next request */
Cyrill Gorcunovc31767c2014-06-28 02:22:17 +04001169 b->next = nasm_zalloc(sizeof(Blocks));
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001170 return b->chunk;
H. Peter Anvince616072002-04-30 21:02:23 +00001171}
1172
1173/*
1174 * this function deletes all managed blocks of memory
1175 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001176static void delete_Blocks(void)
H. Peter Anvince616072002-04-30 21:02:23 +00001177{
H. Peter Anvine2c80182005-01-15 22:15:51 +00001178 Blocks *a, *b = &blocks;
H. Peter Anvince616072002-04-30 21:02:23 +00001179
H. Peter Anvin70653092007-10-19 14:42:29 -07001180 /*
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001181 * keep in mind that the first block, pointed to by blocks
H. Peter Anvin70653092007-10-19 14:42:29 -07001182 * is a static and not dynamically allocated, so we don't
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001183 * free it.
1184 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001185 while (b) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001186 if (b->chunk)
1187 nasm_free(b->chunk);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001188 a = b;
1189 b = b->next;
1190 if (a != &blocks)
1191 nasm_free(a);
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001192 }
Cyrill Gorcunovdae24d72014-06-28 10:17:39 +04001193 memset(&blocks, 0, sizeof(blocks));
H. Peter Anvine2c80182005-01-15 22:15:51 +00001194}
H. Peter Anvin734b1882002-04-30 21:01:08 +00001195
1196/*
H. Peter Anvin70653092007-10-19 14:42:29 -07001197 * this function creates a new Token and passes a pointer to it
H. Peter Anvin734b1882002-04-30 21:01:08 +00001198 * back to the caller. It sets the type and text elements, and
H. Peter Anvinf26e0972008-07-01 21:26:27 -07001199 * also the a.mac and next elements to NULL.
H. Peter Anvin734b1882002-04-30 21:01:08 +00001200 */
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07001201static Token *new_Token(Token * next, enum pp_token_type type,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001202 const char *text, int txtlen)
H. Peter Anvin734b1882002-04-30 21:01:08 +00001203{
1204 Token *t;
1205 int i;
1206
H. Peter Anvin89cee572009-07-15 09:16:54 -04001207 if (!freeTokens) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001208 freeTokens = (Token *) new_Block(TOKEN_BLOCKSIZE * sizeof(Token));
1209 for (i = 0; i < TOKEN_BLOCKSIZE - 1; i++)
1210 freeTokens[i].next = &freeTokens[i + 1];
1211 freeTokens[i].next = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001212 }
1213 t = freeTokens;
1214 freeTokens = t->next;
1215 t->next = next;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07001216 t->a.mac = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001217 t->type = type;
H. Peter Anvin89cee572009-07-15 09:16:54 -04001218 if (type == TOK_WHITESPACE || !text) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001219 t->text = NULL;
1220 } else {
1221 if (txtlen == 0)
1222 txtlen = strlen(text);
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07001223 t->text = nasm_malloc(txtlen+1);
1224 memcpy(t->text, text, txtlen);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001225 t->text[txtlen] = '\0';
H. Peter Anvin734b1882002-04-30 21:01:08 +00001226 }
1227 return t;
1228}
1229
H. Peter Anvine2c80182005-01-15 22:15:51 +00001230static Token *delete_Token(Token * t)
H. Peter Anvin734b1882002-04-30 21:01:08 +00001231{
1232 Token *next = t->next;
1233 nasm_free(t->text);
H. Peter Anvin788e6c12002-04-30 21:02:01 +00001234 t->next = freeTokens;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001235 freeTokens = t;
1236 return next;
1237}
1238
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001239/*
1240 * Convert a line of tokens back into text.
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001241 * If expand_locals is not zero, identifiers of the form "%$*xxx"
1242 * will be transformed into ..@ctxnum.xxx
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001243 */
H. Peter Anvin9e200162008-06-04 17:23:14 -07001244static char *detoken(Token * tlist, bool expand_locals)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001245{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001246 Token *t;
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001247 char *line, *p;
H. Peter Anvinb4daadc2008-01-21 16:31:57 -08001248 const char *q;
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001249 int len = 0;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001250
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001251 list_for_each(t, tlist) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001252 if (t->type == TOK_PREPROC_ID && t->text[1] == '!') {
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001253 char *v;
1254 char *q = t->text;
H. Peter Anvin077fb932010-07-20 14:56:30 -07001255
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001256 v = t->text + 2;
1257 if (*v == '\'' || *v == '\"' || *v == '`') {
1258 size_t len = nasm_unquote(v, NULL);
1259 size_t clen = strlen(v);
H. Peter Anvin077fb932010-07-20 14:56:30 -07001260
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001261 if (len != clen) {
H. Peter Anvin215186f2016-02-17 20:27:41 -08001262 nasm_error(ERR_NONFATAL | ERR_PASS1,
1263 "NUL character in %%! string");
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001264 v = NULL;
1265 }
1266 }
H. Peter Anvin077fb932010-07-20 14:56:30 -07001267
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001268 if (v) {
1269 char *p = getenv(v);
1270 if (!p) {
H. Peter Anvin215186f2016-02-17 20:27:41 -08001271 nasm_error(ERR_NONFATAL | ERR_PASS1,
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001272 "nonexistent environment variable `%s'", v);
1273 p = "";
1274 }
1275 t->text = nasm_strdup(p);
1276 }
1277 nasm_free(q);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001278 }
H. Peter Anvin077fb932010-07-20 14:56:30 -07001279
H. Peter Anvine2c80182005-01-15 22:15:51 +00001280 /* Expand local macros here and not during preprocessing */
1281 if (expand_locals &&
1282 t->type == TOK_PREPROC_ID && t->text &&
1283 t->text[0] == '%' && t->text[1] == '$') {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001284 const char *q;
1285 char *p;
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04001286 Context *ctx = get_ctx(t->text, &q);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001287 if (ctx) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001288 char buffer[40];
Keith Kanios93f2e9a2007-04-14 00:10:59 +00001289 snprintf(buffer, sizeof(buffer), "..@%"PRIu32".", ctx->number);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001290 p = nasm_strcat(buffer, q);
1291 nasm_free(t->text);
1292 t->text = p;
1293 }
1294 }
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001295 if (t->type == TOK_WHITESPACE)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001296 len++;
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001297 else if (t->text)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001298 len += strlen(t->text);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001299 }
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001300
H. Peter Anvin734b1882002-04-30 21:01:08 +00001301 p = line = nasm_malloc(len + 1);
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001302
1303 list_for_each(t, tlist) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001304 if (t->type == TOK_WHITESPACE) {
H. Peter Anvinb4daadc2008-01-21 16:31:57 -08001305 *p++ = ' ';
H. Peter Anvine2c80182005-01-15 22:15:51 +00001306 } else if (t->text) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001307 q = t->text;
1308 while (*q)
1309 *p++ = *q++;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001310 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001311 }
1312 *p = '\0';
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001313
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001314 return line;
1315}
1316
1317/*
H. Peter Anvin76690a12002-04-30 20:52:49 +00001318 * A scanner, suitable for use by the expression evaluator, which
1319 * operates on a line of Tokens. Expects a pointer to a pointer to
1320 * the first token in the line to be passed in as its private_data
1321 * field.
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001322 *
1323 * FIX: This really needs to be unified with stdscan.
H. Peter Anvin76690a12002-04-30 20:52:49 +00001324 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001325static int ppscan(void *private_data, struct tokenval *tokval)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001326{
H. Peter Anvin76690a12002-04-30 20:52:49 +00001327 Token **tlineptr = private_data;
1328 Token *tline;
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001329 char ourcopy[MAX_KEYWORD+1], *p, *r, *s;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001330
H. Peter Anvine2c80182005-01-15 22:15:51 +00001331 do {
1332 tline = *tlineptr;
1333 *tlineptr = tline ? tline->next : NULL;
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04001334 } while (tline && (tline->type == TOK_WHITESPACE ||
1335 tline->type == TOK_COMMENT));
H. Peter Anvin76690a12002-04-30 20:52:49 +00001336
1337 if (!tline)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001338 return tokval->t_type = TOKEN_EOS;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001339
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001340 tokval->t_charptr = tline->text;
1341
H. Peter Anvin76690a12002-04-30 20:52:49 +00001342 if (tline->text[0] == '$' && !tline->text[1])
H. Peter Anvine2c80182005-01-15 22:15:51 +00001343 return tokval->t_type = TOKEN_HERE;
H. Peter Anvin7cf897e2002-05-30 21:30:33 +00001344 if (tline->text[0] == '$' && tline->text[1] == '$' && !tline->text[2])
H. Peter Anvine2c80182005-01-15 22:15:51 +00001345 return tokval->t_type = TOKEN_BASE;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001346
H. Peter Anvine2c80182005-01-15 22:15:51 +00001347 if (tline->type == TOK_ID) {
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001348 p = tokval->t_charptr = tline->text;
1349 if (p[0] == '$') {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001350 tokval->t_charptr++;
1351 return tokval->t_type = TOKEN_ID;
1352 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00001353
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001354 for (r = p, s = ourcopy; *r; r++) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001355 if (r >= p+MAX_KEYWORD)
1356 return tokval->t_type = TOKEN_ID; /* Not a keyword */
H. Peter Anvinac8f8fc2008-06-11 15:49:41 -07001357 *s++ = nasm_tolower(*r);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001358 }
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001359 *s = '\0';
1360 /* right, so we have an identifier sitting in temp storage. now,
1361 * is it actually a register or instruction name, or what? */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001362 return nasm_token_hash(ourcopy, tokval);
H. Peter Anvin76690a12002-04-30 20:52:49 +00001363 }
1364
H. Peter Anvine2c80182005-01-15 22:15:51 +00001365 if (tline->type == TOK_NUMBER) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001366 bool rn_error;
1367 tokval->t_integer = readnum(tline->text, &rn_error);
1368 tokval->t_charptr = tline->text;
1369 if (rn_error)
1370 return tokval->t_type = TOKEN_ERRNUM;
1371 else
1372 return tokval->t_type = TOKEN_NUM;
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001373 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00001374
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001375 if (tline->type == TOK_FLOAT) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001376 return tokval->t_type = TOKEN_FLOAT;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001377 }
1378
H. Peter Anvine2c80182005-01-15 22:15:51 +00001379 if (tline->type == TOK_STRING) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001380 char bq, *ep;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001381
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001382 bq = tline->text[0];
H. Peter Anvin11627042008-06-09 20:45:19 -07001383 tokval->t_charptr = tline->text;
1384 tokval->t_inttwo = nasm_unquote(tline->text, &ep);
H. Peter Anvind2456592008-06-19 15:04:18 -07001385
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001386 if (ep[0] != bq || ep[1] != '\0')
1387 return tokval->t_type = TOKEN_ERRSTR;
1388 else
1389 return tokval->t_type = TOKEN_STR;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001390 }
1391
H. Peter Anvine2c80182005-01-15 22:15:51 +00001392 if (tline->type == TOK_OTHER) {
1393 if (!strcmp(tline->text, "<<"))
1394 return tokval->t_type = TOKEN_SHL;
1395 if (!strcmp(tline->text, ">>"))
1396 return tokval->t_type = TOKEN_SHR;
1397 if (!strcmp(tline->text, "//"))
1398 return tokval->t_type = TOKEN_SDIV;
1399 if (!strcmp(tline->text, "%%"))
1400 return tokval->t_type = TOKEN_SMOD;
1401 if (!strcmp(tline->text, "=="))
1402 return tokval->t_type = TOKEN_EQ;
1403 if (!strcmp(tline->text, "<>"))
1404 return tokval->t_type = TOKEN_NE;
1405 if (!strcmp(tline->text, "!="))
1406 return tokval->t_type = TOKEN_NE;
1407 if (!strcmp(tline->text, "<="))
1408 return tokval->t_type = TOKEN_LE;
1409 if (!strcmp(tline->text, ">="))
1410 return tokval->t_type = TOKEN_GE;
1411 if (!strcmp(tline->text, "&&"))
1412 return tokval->t_type = TOKEN_DBL_AND;
1413 if (!strcmp(tline->text, "^^"))
1414 return tokval->t_type = TOKEN_DBL_XOR;
1415 if (!strcmp(tline->text, "||"))
1416 return tokval->t_type = TOKEN_DBL_OR;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001417 }
1418
1419 /*
1420 * We have no other options: just return the first character of
1421 * the token text.
1422 */
1423 return tokval->t_type = tline->text[0];
1424}
1425
1426/*
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001427 * Compare a string to the name of an existing macro; this is a
1428 * simple wrapper which calls either strcmp or nasm_stricmp
1429 * depending on the value of the `casesense' parameter.
1430 */
H. Peter Anvin4db5a162007-10-11 13:42:09 -07001431static int mstrcmp(const char *p, const char *q, bool casesense)
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001432{
H. Peter Anvin734b1882002-04-30 21:01:08 +00001433 return casesense ? strcmp(p, q) : nasm_stricmp(p, q);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001434}
1435
1436/*
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07001437 * Compare a string to the name of an existing macro; this is a
1438 * simple wrapper which calls either strcmp or nasm_stricmp
1439 * depending on the value of the `casesense' parameter.
1440 */
1441static int mmemcmp(const char *p, const char *q, size_t l, bool casesense)
1442{
1443 return casesense ? memcmp(p, q, l) : nasm_memicmp(p, q, l);
1444}
1445
1446/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001447 * Return the Context structure associated with a %$ token. Return
1448 * NULL, having _already_ reported an error condition, if the
1449 * context stack isn't deep enough for the supplied number of $
1450 * signs.
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001451 *
1452 * If "namep" is non-NULL, set it to the pointer to the macro name
1453 * tail, i.e. the part beyond %$...
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001454 */
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04001455static Context *get_ctx(const char *name, const char **namep)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001456{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001457 Context *ctx;
1458 int i;
1459
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001460 if (namep)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001461 *namep = name;
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001462
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001463 if (!name || name[0] != '%' || name[1] != '$')
H. Peter Anvine2c80182005-01-15 22:15:51 +00001464 return NULL;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001465
H. Peter Anvine2c80182005-01-15 22:15:51 +00001466 if (!cstk) {
H. Peter Anvin215186f2016-02-17 20:27:41 -08001467 nasm_error(ERR_NONFATAL, "`%s': context stack is empty", name);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001468 return NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001469 }
1470
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001471 name += 2;
1472 ctx = cstk;
1473 i = 0;
1474 while (ctx && *name == '$') {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001475 name++;
1476 i++;
1477 ctx = ctx->next;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001478 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001479 if (!ctx) {
H. Peter Anvin215186f2016-02-17 20:27:41 -08001480 nasm_error(ERR_NONFATAL, "`%s': context stack is only"
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001481 " %d level%s deep", name, i, (i == 1 ? "" : "s"));
H. Peter Anvine2c80182005-01-15 22:15:51 +00001482 return NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001483 }
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001484
1485 if (namep)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001486 *namep = name;
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001487
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04001488 return ctx;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001489}
1490
1491/*
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001492 * Check to see if a file is already in a string list
1493 */
1494static bool in_list(const StrList *list, const char *str)
1495{
1496 while (list) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001497 if (!strcmp(list->str, str))
1498 return true;
1499 list = list->next;
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001500 }
1501 return false;
1502}
1503
1504/*
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001505 * Open an include file. This routine must always return a valid
1506 * file pointer if it returns - it's responsible for throwing an
1507 * ERR_FATAL and bombing out completely if not. It should also try
1508 * the include path one by one until it finds the file or reaches
1509 * the end of the path.
1510 */
H. Peter Anvin2b1c3b92008-06-06 10:38:46 -07001511static FILE *inc_fopen(const char *file, StrList **dhead, StrList ***dtail,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001512 bool missing_ok)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001513{
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001514 FILE *fp;
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001515 char *prefix = "";
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001516 IncPath *ip = ipath;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001517 int len = strlen(file);
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001518 size_t prefix_len = 0;
1519 StrList *sl;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001520
H. Peter Anvine2c80182005-01-15 22:15:51 +00001521 while (1) {
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001522 sl = nasm_malloc(prefix_len+len+1+sizeof sl->next);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001523 memcpy(sl->str, prefix, prefix_len);
1524 memcpy(sl->str+prefix_len, file, len+1);
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001525 fp = fopen(sl->str, "r");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001526 if (fp && dhead && !in_list(*dhead, sl->str)) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001527 sl->next = NULL;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001528 **dtail = sl;
1529 *dtail = &sl->next;
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001530 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001531 nasm_free(sl);
1532 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001533 if (fp)
1534 return fp;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001535 if (!ip) {
1536 if (!missing_ok)
1537 break;
1538 prefix = NULL;
1539 } else {
1540 prefix = ip->path;
1541 ip = ip->next;
1542 }
1543 if (prefix) {
1544 prefix_len = strlen(prefix);
1545 } else {
1546 /* -MG given and file not found */
1547 if (dhead && !in_list(*dhead, file)) {
1548 sl = nasm_malloc(len+1+sizeof sl->next);
1549 sl->next = NULL;
1550 strcpy(sl->str, file);
1551 **dtail = sl;
1552 *dtail = &sl->next;
1553 }
1554 return NULL;
1555 }
H. Peter Anvineba20a72002-04-30 20:53:55 +00001556 }
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001557
H. Peter Anvin215186f2016-02-17 20:27:41 -08001558 nasm_error(ERR_FATAL, "unable to open include file `%s'", file);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001559 return NULL;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001560}
1561
1562/*
Fabian Giesen86d87562016-04-28 13:48:14 -07001563 * Opens an include or input file. Public version, for use by modules
1564 * that get a file:lineno pair and need to look at the file again
1565 * (e.g. the CodeView debug backend). Returns NULL on failure.
1566 */
1567FILE *pp_input_fopen(const char *filename)
1568{
1569 FILE *fp;
1570 StrList *xsl = NULL;
1571 StrList **xst = &xsl;
1572
1573 fp = inc_fopen(filename, &xsl, &xst, true);
1574 if (xsl)
1575 nasm_free(xsl);
1576 return fp;
1577}
1578
1579/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001580 * Determine if we should warn on defining a single-line macro of
H. Peter Anvinef7468f2002-04-30 20:57:59 +00001581 * name `name', with `nparam' parameters. If nparam is 0 or -1, will
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001582 * return true if _any_ single-line macro of that name is defined.
1583 * Otherwise, will return true if a single-line macro with either
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001584 * `nparam' or no parameters is defined.
1585 *
1586 * If a macro with precisely the right number of parameters is
H. Peter Anvinef7468f2002-04-30 20:57:59 +00001587 * defined, or nparam is -1, the address of the definition structure
1588 * will be returned in `defn'; otherwise NULL will be returned. If `defn'
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001589 * is NULL, no action will be taken regarding its contents, and no
1590 * error will occur.
1591 *
1592 * Note that this is also called with nparam zero to resolve
1593 * `ifdef'.
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001594 *
1595 * If you already know which context macro belongs to, you can pass
1596 * the context pointer as first parameter; if you won't but name begins
1597 * with %$ the context will be automatically computed. If all_contexts
1598 * is true, macro will be searched in outer contexts as well.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001599 */
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07001600static bool
H. Peter Anvinb2a5fda2008-06-19 21:42:42 -07001601smacro_defined(Context * ctx, const char *name, int nparam, SMacro ** defn,
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07001602 bool nocase)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001603{
H. Peter Anvin166c2472008-05-28 12:28:58 -07001604 struct hash_table *smtbl;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001605 SMacro *m;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001606
H. Peter Anvin97a23472007-09-16 17:57:25 -07001607 if (ctx) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001608 smtbl = &ctx->localmac;
H. Peter Anvin97a23472007-09-16 17:57:25 -07001609 } else if (name[0] == '%' && name[1] == '$') {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001610 if (cstk)
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04001611 ctx = get_ctx(name, &name);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001612 if (!ctx)
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001613 return false; /* got to return _something_ */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001614 smtbl = &ctx->localmac;
H. Peter Anvin97a23472007-09-16 17:57:25 -07001615 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001616 smtbl = &smacros;
H. Peter Anvin97a23472007-09-16 17:57:25 -07001617 }
H. Peter Anvin166c2472008-05-28 12:28:58 -07001618 m = (SMacro *) hash_findix(smtbl, name);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001619
H. Peter Anvine2c80182005-01-15 22:15:51 +00001620 while (m) {
1621 if (!mstrcmp(m->name, name, m->casesense && nocase) &&
Charles Crayne192d5b52007-10-18 19:02:42 -07001622 (nparam <= 0 || m->nparam == 0 || nparam == (int) m->nparam)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001623 if (defn) {
Charles Crayne192d5b52007-10-18 19:02:42 -07001624 if (nparam == (int) m->nparam || nparam == -1)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001625 *defn = m;
1626 else
1627 *defn = NULL;
1628 }
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001629 return true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001630 }
1631 m = m->next;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001632 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001633
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001634 return false;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001635}
1636
1637/*
1638 * Count and mark off the parameters in a multi-line macro call.
1639 * This is called both from within the multi-line macro expansion
1640 * code, and also to mark off the default parameters when provided
1641 * in a %macro definition line.
1642 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001643static void count_mmac_params(Token * t, int *nparam, Token *** params)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001644{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001645 int paramsize, brace;
1646
1647 *nparam = paramsize = 0;
1648 *params = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001649 while (t) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001650 /* +1: we need space for the final NULL */
H. Peter Anvin91fb6f12008-09-01 10:56:33 -07001651 if (*nparam+1 >= paramsize) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001652 paramsize += PARAM_DELTA;
1653 *params = nasm_realloc(*params, sizeof(**params) * paramsize);
1654 }
1655 skip_white_(t);
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08001656 brace = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001657 if (tok_is_(t, "{"))
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08001658 brace++;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001659 (*params)[(*nparam)++] = t;
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08001660 if (brace) {
1661 while (brace && (t = t->next) != NULL) {
1662 if (tok_is_(t, "{"))
1663 brace++;
1664 else if (tok_is_(t, "}"))
1665 brace--;
1666 }
1667
1668 if (t) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001669 /*
1670 * Now we've found the closing brace, look further
1671 * for the comma.
1672 */
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08001673 t = t->next;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001674 skip_white_(t);
1675 if (tok_isnt_(t, ",")) {
H. Peter Anvin215186f2016-02-17 20:27:41 -08001676 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00001677 "braces do not enclose all of macro parameter");
1678 while (tok_isnt_(t, ","))
1679 t = t->next;
1680 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001681 }
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08001682 } else {
1683 while (tok_isnt_(t, ","))
1684 t = t->next;
1685 }
1686 if (t) { /* got a comma/brace */
1687 t = t->next; /* eat the comma */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001688 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001689 }
1690}
1691
1692/*
H. Peter Anvin76690a12002-04-30 20:52:49 +00001693 * Determine whether one of the various `if' conditions is true or
1694 * not.
1695 *
1696 * We must free the tline we get passed.
1697 */
H. Peter Anvin70055962007-10-11 00:05:31 -07001698static bool if_condition(Token * tline, enum preproc_token ct)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001699{
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001700 enum pp_conditional i = PP_COND(ct);
H. Peter Anvin70055962007-10-11 00:05:31 -07001701 bool j;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001702 Token *t, *tt, **tptr, *origline;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001703 struct tokenval tokval;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001704 expr *evalresult;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001705 enum pp_token_type needtype;
H. Peter Anvin077fb932010-07-20 14:56:30 -07001706 char *p;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001707
1708 origline = tline;
1709
H. Peter Anvine2c80182005-01-15 22:15:51 +00001710 switch (i) {
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001711 case PPC_IFCTX:
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001712 j = false; /* have we matched yet? */
Victor van den Elzen0e857f12008-07-23 13:21:29 +02001713 while (true) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001714 skip_white_(tline);
Victor van den Elzen0e857f12008-07-23 13:21:29 +02001715 if (!tline)
1716 break;
1717 if (tline->type != TOK_ID) {
H. Peter Anvin215186f2016-02-17 20:27:41 -08001718 nasm_error(ERR_NONFATAL,
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001719 "`%s' expects context identifiers", pp_directives[ct]);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001720 free_tlist(origline);
1721 return -1;
1722 }
Victor van den Elzen0e857f12008-07-23 13:21:29 +02001723 if (cstk && cstk->name && !nasm_stricmp(tline->text, cstk->name))
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001724 j = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001725 tline = tline->next;
1726 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001727 break;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001728
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001729 case PPC_IFDEF:
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001730 j = false; /* have we matched yet? */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001731 while (tline) {
1732 skip_white_(tline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001733 if (!tline || (tline->type != TOK_ID &&
1734 (tline->type != TOK_PREPROC_ID ||
1735 tline->text[1] != '$'))) {
H. Peter Anvin215186f2016-02-17 20:27:41 -08001736 nasm_error(ERR_NONFATAL,
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001737 "`%s' expects macro identifiers", pp_directives[ct]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001738 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001739 }
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07001740 if (smacro_defined(NULL, tline->text, 0, NULL, true))
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001741 j = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001742 tline = tline->next;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001743 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001744 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001745
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001746 case PPC_IFENV:
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001747 tline = expand_smacro(tline);
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001748 j = false; /* have we matched yet? */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001749 while (tline) {
1750 skip_white_(tline);
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001751 if (!tline || (tline->type != TOK_ID &&
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001752 tline->type != TOK_STRING &&
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001753 (tline->type != TOK_PREPROC_ID ||
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001754 tline->text[1] != '!'))) {
H. Peter Anvin215186f2016-02-17 20:27:41 -08001755 nasm_error(ERR_NONFATAL,
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001756 "`%s' expects environment variable names",
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001757 pp_directives[ct]);
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001758 goto fail;
1759 }
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001760 p = tline->text;
1761 if (tline->type == TOK_PREPROC_ID)
1762 p += 2; /* Skip leading %! */
1763 if (*p == '\'' || *p == '\"' || *p == '`')
1764 nasm_unquote_cstr(p, ct);
1765 if (getenv(p))
1766 j = true;
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001767 tline = tline->next;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001768 }
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001769 break;
1770
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001771 case PPC_IFIDN:
1772 case PPC_IFIDNI:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001773 tline = expand_smacro(tline);
1774 t = tt = tline;
1775 while (tok_isnt_(tt, ","))
1776 tt = tt->next;
1777 if (!tt) {
H. Peter Anvin215186f2016-02-17 20:27:41 -08001778 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00001779 "`%s' expects two comma-separated arguments",
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001780 pp_directives[ct]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001781 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001782 }
1783 tt = tt->next;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001784 j = true; /* assume equality unless proved not */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001785 while ((t->type != TOK_OTHER || strcmp(t->text, ",")) && tt) {
1786 if (tt->type == TOK_OTHER && !strcmp(tt->text, ",")) {
H. Peter Anvin215186f2016-02-17 20:27:41 -08001787 nasm_error(ERR_NONFATAL, "`%s': more than one comma on line",
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001788 pp_directives[ct]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001789 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001790 }
1791 if (t->type == TOK_WHITESPACE) {
1792 t = t->next;
1793 continue;
1794 }
1795 if (tt->type == TOK_WHITESPACE) {
1796 tt = tt->next;
1797 continue;
1798 }
1799 if (tt->type != t->type) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001800 j = false; /* found mismatching tokens */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001801 break;
1802 }
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07001803 /* When comparing strings, need to unquote them first */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001804 if (t->type == TOK_STRING) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001805 size_t l1 = nasm_unquote(t->text, NULL);
1806 size_t l2 = nasm_unquote(tt->text, NULL);
H. Peter Anvind2456592008-06-19 15:04:18 -07001807
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001808 if (l1 != l2) {
1809 j = false;
1810 break;
1811 }
1812 if (mmemcmp(t->text, tt->text, l1, i == PPC_IFIDN)) {
1813 j = false;
1814 break;
1815 }
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07001816 } else if (mstrcmp(tt->text, t->text, i == PPC_IFIDN) != 0) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001817 j = false; /* found mismatching tokens */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001818 break;
1819 }
Nickolay Yurchenkof3b3ce22003-09-21 20:38:43 +00001820
H. Peter Anvine2c80182005-01-15 22:15:51 +00001821 t = t->next;
1822 tt = tt->next;
1823 }
1824 if ((t->type != TOK_OTHER || strcmp(t->text, ",")) || tt)
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001825 j = false; /* trailing gunk on one end or other */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001826 break;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001827
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001828 case PPC_IFMACRO:
H. Peter Anvin89cee572009-07-15 09:16:54 -04001829 {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001830 bool found = false;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001831 MMacro searching, *mmac;
H. Peter Anvin65747262002-05-07 00:10:05 +00001832
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001833 skip_white_(tline);
1834 tline = expand_id(tline);
1835 if (!tok_type_(tline, TOK_ID)) {
H. Peter Anvin215186f2016-02-17 20:27:41 -08001836 nasm_error(ERR_NONFATAL,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001837 "`%s' expects a macro name", pp_directives[ct]);
1838 goto fail;
1839 }
1840 searching.name = nasm_strdup(tline->text);
1841 searching.casesense = true;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001842 searching.plus = false;
1843 searching.nolist = false;
1844 searching.in_progress = 0;
1845 searching.max_depth = 0;
1846 searching.rep_nest = NULL;
1847 searching.nparam_min = 0;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001848 searching.nparam_max = INT_MAX;
1849 tline = expand_smacro(tline->next);
1850 skip_white_(tline);
1851 if (!tline) {
1852 } else if (!tok_type_(tline, TOK_NUMBER)) {
H. Peter Anvin215186f2016-02-17 20:27:41 -08001853 nasm_error(ERR_NONFATAL,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001854 "`%s' expects a parameter count or nothing",
1855 pp_directives[ct]);
1856 } else {
1857 searching.nparam_min = searching.nparam_max =
1858 readnum(tline->text, &j);
1859 if (j)
H. Peter Anvin215186f2016-02-17 20:27:41 -08001860 nasm_error(ERR_NONFATAL,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001861 "unable to parse parameter count `%s'",
1862 tline->text);
1863 }
1864 if (tline && tok_is_(tline->next, "-")) {
1865 tline = tline->next->next;
1866 if (tok_is_(tline, "*"))
1867 searching.nparam_max = INT_MAX;
1868 else if (!tok_type_(tline, TOK_NUMBER))
H. Peter Anvin215186f2016-02-17 20:27:41 -08001869 nasm_error(ERR_NONFATAL,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001870 "`%s' expects a parameter count after `-'",
1871 pp_directives[ct]);
1872 else {
1873 searching.nparam_max = readnum(tline->text, &j);
1874 if (j)
H. Peter Anvin215186f2016-02-17 20:27:41 -08001875 nasm_error(ERR_NONFATAL,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001876 "unable to parse parameter count `%s'",
1877 tline->text);
1878 if (searching.nparam_min > searching.nparam_max)
H. Peter Anvin215186f2016-02-17 20:27:41 -08001879 nasm_error(ERR_NONFATAL,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001880 "minimum parameter count exceeds maximum");
1881 }
1882 }
1883 if (tline && tok_is_(tline->next, "+")) {
1884 tline = tline->next;
1885 searching.plus = true;
1886 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001887 mmac = (MMacro *) hash_findix(&mmacros, searching.name);
1888 while (mmac) {
1889 if (!strcmp(mmac->name, searching.name) &&
1890 (mmac->nparam_min <= searching.nparam_max
1891 || searching.plus)
1892 && (searching.nparam_min <= mmac->nparam_max
1893 || mmac->plus)) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001894 found = true;
1895 break;
1896 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001897 mmac = mmac->next;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001898 }
1899 if (tline && tline->next)
H. Peter Anvin215186f2016-02-17 20:27:41 -08001900 nasm_error(ERR_WARNING|ERR_PASS1,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001901 "trailing garbage after %%ifmacro ignored");
1902 nasm_free(searching.name);
1903 j = found;
1904 break;
H. Peter Anvin89cee572009-07-15 09:16:54 -04001905 }
H. Peter Anvin65747262002-05-07 00:10:05 +00001906
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001907 case PPC_IFID:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001908 needtype = TOK_ID;
1909 goto iftype;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001910 case PPC_IFNUM:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001911 needtype = TOK_NUMBER;
1912 goto iftype;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001913 case PPC_IFSTR:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001914 needtype = TOK_STRING;
1915 goto iftype;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001916
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001917iftype:
1918 t = tline = expand_smacro(tline);
H. Peter Anvind85d2502008-05-04 17:53:31 -07001919
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001920 while (tok_type_(t, TOK_WHITESPACE) ||
1921 (needtype == TOK_NUMBER &&
1922 tok_type_(t, TOK_OTHER) &&
1923 (t->text[0] == '-' || t->text[0] == '+') &&
1924 !t->text[1]))
1925 t = t->next;
H. Peter Anvind85d2502008-05-04 17:53:31 -07001926
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001927 j = tok_type_(t, needtype);
1928 break;
H. Peter Anvincbf768d2008-02-16 16:41:25 -08001929
1930 case PPC_IFTOKEN:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001931 t = tline = expand_smacro(tline);
1932 while (tok_type_(t, TOK_WHITESPACE))
1933 t = t->next;
H. Peter Anvincbf768d2008-02-16 16:41:25 -08001934
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001935 j = false;
1936 if (t) {
1937 t = t->next; /* Skip the actual token */
1938 while (tok_type_(t, TOK_WHITESPACE))
1939 t = t->next;
1940 j = !t; /* Should be nothing left */
1941 }
1942 break;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001943
H. Peter Anvin134b9462008-02-16 17:01:40 -08001944 case PPC_IFEMPTY:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001945 t = tline = expand_smacro(tline);
1946 while (tok_type_(t, TOK_WHITESPACE))
1947 t = t->next;
H. Peter Anvin134b9462008-02-16 17:01:40 -08001948
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001949 j = !t; /* Should be empty */
1950 break;
H. Peter Anvin134b9462008-02-16 17:01:40 -08001951
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001952 case PPC_IF:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001953 t = tline = expand_smacro(tline);
1954 tptr = &t;
1955 tokval.t_type = TOKEN_INVALID;
1956 evalresult = evaluate(ppscan, tptr, &tokval,
H. Peter Anvin215186f2016-02-17 20:27:41 -08001957 NULL, pass | CRITICAL, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001958 if (!evalresult)
1959 return -1;
1960 if (tokval.t_type)
H. Peter Anvin215186f2016-02-17 20:27:41 -08001961 nasm_error(ERR_WARNING|ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +00001962 "trailing garbage after expression ignored");
1963 if (!is_simple(evalresult)) {
H. Peter Anvin215186f2016-02-17 20:27:41 -08001964 nasm_error(ERR_NONFATAL,
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001965 "non-constant value given to `%s'", pp_directives[ct]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001966 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001967 }
Chuck Crayne60ae75d2007-05-02 01:59:16 +00001968 j = reloc_value(evalresult) != 0;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001969 break;
H. Peter Anvin95e28822007-09-12 04:20:08 +00001970
H. Peter Anvine2c80182005-01-15 22:15:51 +00001971 default:
H. Peter Anvin215186f2016-02-17 20:27:41 -08001972 nasm_error(ERR_FATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00001973 "preprocessor directive `%s' not yet implemented",
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001974 pp_directives[ct]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001975 goto fail;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001976 }
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001977
1978 free_tlist(origline);
1979 return j ^ PP_NEGATIVE(ct);
H. Peter Anvin70653092007-10-19 14:42:29 -07001980
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001981fail:
1982 free_tlist(origline);
1983 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001984}
1985
1986/*
H. Peter Anvin4db5a162007-10-11 13:42:09 -07001987 * Common code for defining an smacro
1988 */
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001989static bool define_smacro(Context *ctx, const char *mname, bool casesense,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001990 int nparam, Token *expansion)
H. Peter Anvin4db5a162007-10-11 13:42:09 -07001991{
1992 SMacro *smac, **smhead;
H. Peter Anvin166c2472008-05-28 12:28:58 -07001993 struct hash_table *smtbl;
H. Peter Anvin70653092007-10-19 14:42:29 -07001994
H. Peter Anvin4db5a162007-10-11 13:42:09 -07001995 if (smacro_defined(ctx, mname, nparam, &smac, casesense)) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001996 if (!smac) {
H. Peter Anvin215186f2016-02-17 20:27:41 -08001997 nasm_error(ERR_WARNING|ERR_PASS1,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001998 "single-line macro `%s' defined both with and"
1999 " without parameters", mname);
2000 /*
2001 * Some instances of the old code considered this a failure,
2002 * some others didn't. What is the right thing to do here?
2003 */
2004 free_tlist(expansion);
2005 return false; /* Failure */
2006 } else {
2007 /*
2008 * We're redefining, so we have to take over an
2009 * existing SMacro structure. This means freeing
2010 * what was already in it.
2011 */
2012 nasm_free(smac->name);
2013 free_tlist(smac->expansion);
2014 }
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002015 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002016 smtbl = ctx ? &ctx->localmac : &smacros;
2017 smhead = (SMacro **) hash_findi_add(smtbl, mname);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002018 smac = nasm_malloc(sizeof(SMacro));
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002019 smac->next = *smhead;
2020 *smhead = smac;
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002021 }
2022 smac->name = nasm_strdup(mname);
2023 smac->casesense = casesense;
2024 smac->nparam = nparam;
2025 smac->expansion = expansion;
2026 smac->in_progress = false;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002027 return true; /* Success */
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002028}
2029
2030/*
2031 * Undefine an smacro
2032 */
2033static void undef_smacro(Context *ctx, const char *mname)
2034{
2035 SMacro **smhead, *s, **sp;
H. Peter Anvin166c2472008-05-28 12:28:58 -07002036 struct hash_table *smtbl;
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002037
H. Peter Anvin166c2472008-05-28 12:28:58 -07002038 smtbl = ctx ? &ctx->localmac : &smacros;
2039 smhead = (SMacro **)hash_findi(smtbl, mname, NULL);
H. Peter Anvin70653092007-10-19 14:42:29 -07002040
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002041 if (smhead) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002042 /*
2043 * We now have a macro name... go hunt for it.
2044 */
2045 sp = smhead;
2046 while ((s = *sp) != NULL) {
2047 if (!mstrcmp(s->name, mname, s->casesense)) {
2048 *sp = s->next;
2049 nasm_free(s->name);
2050 free_tlist(s->expansion);
2051 nasm_free(s);
2052 } else {
2053 sp = &s->next;
2054 }
2055 }
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002056 }
2057}
2058
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002059/*
H. Peter Anvina26433d2008-07-16 14:40:01 -07002060 * Parse a mmacro specification.
2061 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002062static bool parse_mmacro_spec(Token *tline, MMacro *def, const char *directive)
H. Peter Anvina26433d2008-07-16 14:40:01 -07002063{
2064 bool err;
2065
2066 tline = tline->next;
2067 skip_white_(tline);
2068 tline = expand_id(tline);
2069 if (!tok_type_(tline, TOK_ID)) {
H. Peter Anvin215186f2016-02-17 20:27:41 -08002070 nasm_error(ERR_NONFATAL, "`%s' expects a macro name", directive);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002071 return false;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002072 }
Victor van den Elzenb916ede2008-07-23 15:14:22 +02002073
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002074 def->prev = NULL;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002075 def->name = nasm_strdup(tline->text);
2076 def->plus = false;
2077 def->nolist = false;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002078 def->in_progress = 0;
2079 def->rep_nest = NULL;
Victor van den Elzenb916ede2008-07-23 15:14:22 +02002080 def->nparam_min = 0;
2081 def->nparam_max = 0;
2082
H. Peter Anvina26433d2008-07-16 14:40:01 -07002083 tline = expand_smacro(tline->next);
2084 skip_white_(tline);
2085 if (!tok_type_(tline, TOK_NUMBER)) {
H. Peter Anvin215186f2016-02-17 20:27:41 -08002086 nasm_error(ERR_NONFATAL, "`%s' expects a parameter count", directive);
H. Peter Anvina26433d2008-07-16 14:40:01 -07002087 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002088 def->nparam_min = def->nparam_max =
2089 readnum(tline->text, &err);
2090 if (err)
H. Peter Anvin215186f2016-02-17 20:27:41 -08002091 nasm_error(ERR_NONFATAL,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002092 "unable to parse parameter count `%s'", tline->text);
H. Peter Anvina26433d2008-07-16 14:40:01 -07002093 }
2094 if (tline && tok_is_(tline->next, "-")) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002095 tline = tline->next->next;
2096 if (tok_is_(tline, "*")) {
2097 def->nparam_max = INT_MAX;
2098 } else if (!tok_type_(tline, TOK_NUMBER)) {
H. Peter Anvin215186f2016-02-17 20:27:41 -08002099 nasm_error(ERR_NONFATAL,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002100 "`%s' expects a parameter count after `-'", directive);
2101 } else {
2102 def->nparam_max = readnum(tline->text, &err);
2103 if (err) {
H. Peter Anvin215186f2016-02-17 20:27:41 -08002104 nasm_error(ERR_NONFATAL, "unable to parse parameter count `%s'",
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002105 tline->text);
2106 }
2107 if (def->nparam_min > def->nparam_max) {
H. Peter Anvin215186f2016-02-17 20:27:41 -08002108 nasm_error(ERR_NONFATAL, "minimum parameter count exceeds maximum");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002109 }
2110 }
H. Peter Anvina26433d2008-07-16 14:40:01 -07002111 }
2112 if (tline && tok_is_(tline->next, "+")) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002113 tline = tline->next;
2114 def->plus = true;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002115 }
2116 if (tline && tok_type_(tline->next, TOK_ID) &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002117 !nasm_stricmp(tline->next->text, ".nolist")) {
2118 tline = tline->next;
2119 def->nolist = true;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002120 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002121
H. Peter Anvina26433d2008-07-16 14:40:01 -07002122 /*
2123 * Handle default parameters.
2124 */
2125 if (tline && tline->next) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002126 def->dlist = tline->next;
2127 tline->next = NULL;
2128 count_mmac_params(def->dlist, &def->ndefs, &def->defaults);
H. Peter Anvina26433d2008-07-16 14:40:01 -07002129 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002130 def->dlist = NULL;
2131 def->defaults = NULL;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002132 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002133 def->expansion = NULL;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002134
H. Peter Anvin89cee572009-07-15 09:16:54 -04002135 if (def->defaults && def->ndefs > def->nparam_max - def->nparam_min &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002136 !def->plus)
H. Peter Anvin215186f2016-02-17 20:27:41 -08002137 nasm_error(ERR_WARNING|ERR_PASS1|ERR_WARN_MDP,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002138 "too many default macro parameters");
Victor van den Elzenb916ede2008-07-23 15:14:22 +02002139
H. Peter Anvina26433d2008-07-16 14:40:01 -07002140 return true;
2141}
2142
2143
2144/*
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002145 * Decode a size directive
2146 */
2147static int parse_size(const char *str) {
2148 static const char *size_names[] =
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002149 { "byte", "dword", "oword", "qword", "tword", "word", "yword" };
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002150 static const int sizes[] =
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002151 { 0, 1, 4, 16, 8, 10, 2, 32 };
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002152
Cyrill Gorcunova7319242010-06-03 22:04:36 +04002153 return sizes[bsii(str, size_names, ARRAY_SIZE(size_names))+1];
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002154}
2155
Ed Beroset3ab3f412002-06-11 03:31:49 +00002156/**
2157 * find and process preprocessor directive in passed line
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002158 * Find out if a line contains a preprocessor directive, and deal
2159 * with it if so.
H. Peter Anvin70653092007-10-19 14:42:29 -07002160 *
Ed Beroset3ab3f412002-06-11 03:31:49 +00002161 * If a directive _is_ found, it is the responsibility of this routine
2162 * (and not the caller) to free_tlist() the line.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002163 *
Ed Beroset3ab3f412002-06-11 03:31:49 +00002164 * @param tline a pointer to the current tokeninzed line linked list
2165 * @return DIRECTIVE_FOUND or NO_DIRECTIVE_FOUND
H. Peter Anvin70653092007-10-19 14:42:29 -07002166 *
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002167 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002168static int do_directive(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00002169{
H. Peter Anvin4169a472007-09-12 01:29:43 +00002170 enum preproc_token i;
2171 int j;
H. Peter Anvin70055962007-10-11 00:05:31 -07002172 bool err;
2173 int nparam;
2174 bool nolist;
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07002175 bool casesense;
H. Peter Anvin8cfdb9d2007-09-14 18:36:01 -07002176 int k, m;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002177 int offset;
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08002178 char *p, *pp;
2179 const char *mname;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002180 Include *inc;
2181 Context *ctx;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002182 Cond *cond;
2183 MMacro *mmac, **mmhead;
Jin Kyu Songc9486b92013-10-28 17:07:57 -07002184 Token *t = NULL, *tt, *param_start, *macro_start, *last, **tptr, *origline;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002185 Line *l;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002186 struct tokenval tokval;
2187 expr *evalresult;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002188 MMacro *tmp_defining; /* Used when manipulating rep_nest */
H. Peter Anvinf8ba53e2007-10-11 10:11:57 -07002189 int64_t count;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07002190 size_t len;
H. Peter Anvin8e3f75e2008-09-24 00:21:58 -07002191 int severity;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002192
2193 origline = tline;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002194
H. Peter Anvineba20a72002-04-30 20:53:55 +00002195 skip_white_(tline);
H. Peter Anvinf2936d72008-06-04 15:11:23 -07002196 if (!tline || !tok_type_(tline, TOK_PREPROC_ID) ||
H. Peter Anvine2c80182005-01-15 22:15:51 +00002197 (tline->text[1] == '%' || tline->text[1] == '$'
2198 || tline->text[1] == '!'))
2199 return NO_DIRECTIVE_FOUND;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002200
H. Peter Anvin4169a472007-09-12 01:29:43 +00002201 i = pp_token_hash(tline->text);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002202
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002203 /*
2204 * FIXME: We zap execution of PP_RMACRO, PP_IRMACRO, PP_EXITMACRO
2205 * since they are known to be buggy at moment, we need to fix them
2206 * in future release (2.09-2.10)
2207 */
Philipp Klokeb432f572013-03-31 12:01:23 +02002208 if (i == PP_RMACRO || i == PP_IRMACRO || i == PP_EXITMACRO) {
H. Peter Anvin215186f2016-02-17 20:27:41 -08002209 nasm_error(ERR_NONFATAL, "unknown preprocessor directive `%s'",
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002210 tline->text);
2211 return NO_DIRECTIVE_FOUND;
2212 }
2213
2214 /*
2215 * If we're in a non-emitting branch of a condition construct,
2216 * or walking to the end of an already terminated %rep block,
2217 * we should ignore all directives except for condition
2218 * directives.
2219 */
2220 if (((istk->conds && !emitting(istk->conds->state)) ||
2221 (istk->mstk && !istk->mstk->in_progress)) && !is_condition(i)) {
2222 return NO_DIRECTIVE_FOUND;
2223 }
2224
2225 /*
2226 * If we're defining a macro or reading a %rep block, we should
2227 * ignore all directives except for %macro/%imacro (which nest),
2228 * %endm/%endmacro, and (only if we're in a %rep block) %endrep.
2229 * If we're in a %rep block, another %rep nests, so should be let through.
2230 */
2231 if (defining && i != PP_MACRO && i != PP_IMACRO &&
2232 i != PP_RMACRO && i != PP_IRMACRO &&
2233 i != PP_ENDMACRO && i != PP_ENDM &&
2234 (defining->name || (i != PP_ENDREP && i != PP_REP))) {
2235 return NO_DIRECTIVE_FOUND;
2236 }
2237
2238 if (defining) {
2239 if (i == PP_MACRO || i == PP_IMACRO ||
2240 i == PP_RMACRO || i == PP_IRMACRO) {
2241 nested_mac_count++;
2242 return NO_DIRECTIVE_FOUND;
2243 } else if (nested_mac_count > 0) {
2244 if (i == PP_ENDMACRO) {
2245 nested_mac_count--;
2246 return NO_DIRECTIVE_FOUND;
2247 }
2248 }
2249 if (!defining->name) {
2250 if (i == PP_REP) {
2251 nested_rep_count++;
2252 return NO_DIRECTIVE_FOUND;
2253 } else if (nested_rep_count > 0) {
2254 if (i == PP_ENDREP) {
2255 nested_rep_count--;
2256 return NO_DIRECTIVE_FOUND;
2257 }
2258 }
2259 }
2260 }
2261
H. Peter Anvin4169a472007-09-12 01:29:43 +00002262 switch (i) {
2263 case PP_INVALID:
H. Peter Anvin215186f2016-02-17 20:27:41 -08002264 nasm_error(ERR_NONFATAL, "unknown preprocessor directive `%s'",
H. Peter Anvine2c80182005-01-15 22:15:51 +00002265 tline->text);
2266 return NO_DIRECTIVE_FOUND; /* didn't get it */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002267
H. Peter Anvine2c80182005-01-15 22:15:51 +00002268 case PP_STACKSIZE:
2269 /* Directive to tell NASM what the default stack size is. The
2270 * default is for a 16-bit stack, and this can be overriden with
2271 * %stacksize large.
H. Peter Anvine2c80182005-01-15 22:15:51 +00002272 */
2273 tline = tline->next;
2274 if (tline && tline->type == TOK_WHITESPACE)
2275 tline = tline->next;
2276 if (!tline || tline->type != TOK_ID) {
H. Peter Anvin215186f2016-02-17 20:27:41 -08002277 nasm_error(ERR_NONFATAL, "`%%stacksize' missing size parameter");
H. Peter Anvine2c80182005-01-15 22:15:51 +00002278 free_tlist(origline);
2279 return DIRECTIVE_FOUND;
2280 }
2281 if (nasm_stricmp(tline->text, "flat") == 0) {
2282 /* All subsequent ARG directives are for a 32-bit stack */
2283 StackSize = 4;
2284 StackPointer = "ebp";
2285 ArgOffset = 8;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002286 LocalOffset = 0;
Charles Crayne7eaf9192007-11-08 22:11:14 -08002287 } else if (nasm_stricmp(tline->text, "flat64") == 0) {
2288 /* All subsequent ARG directives are for a 64-bit stack */
2289 StackSize = 8;
2290 StackPointer = "rbp";
Per Jessen53252e02010-02-11 00:16:59 +03002291 ArgOffset = 16;
Charles Crayne7eaf9192007-11-08 22:11:14 -08002292 LocalOffset = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002293 } else if (nasm_stricmp(tline->text, "large") == 0) {
2294 /* All subsequent ARG directives are for a 16-bit stack,
2295 * far function call.
2296 */
2297 StackSize = 2;
2298 StackPointer = "bp";
2299 ArgOffset = 4;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002300 LocalOffset = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002301 } else if (nasm_stricmp(tline->text, "small") == 0) {
2302 /* All subsequent ARG directives are for a 16-bit stack,
2303 * far function call. We don't support near functions.
2304 */
2305 StackSize = 2;
2306 StackPointer = "bp";
2307 ArgOffset = 6;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002308 LocalOffset = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002309 } else {
H. Peter Anvin215186f2016-02-17 20:27:41 -08002310 nasm_error(ERR_NONFATAL, "`%%stacksize' invalid size type");
H. Peter Anvine2c80182005-01-15 22:15:51 +00002311 free_tlist(origline);
2312 return DIRECTIVE_FOUND;
2313 }
2314 free_tlist(origline);
2315 return DIRECTIVE_FOUND;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002316
H. Peter Anvine2c80182005-01-15 22:15:51 +00002317 case PP_ARG:
2318 /* TASM like ARG directive to define arguments to functions, in
2319 * the following form:
2320 *
2321 * ARG arg1:WORD, arg2:DWORD, arg4:QWORD
2322 */
2323 offset = ArgOffset;
2324 do {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00002325 char *arg, directive[256];
H. Peter Anvine2c80182005-01-15 22:15:51 +00002326 int size = StackSize;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002327
H. Peter Anvine2c80182005-01-15 22:15:51 +00002328 /* Find the argument name */
2329 tline = tline->next;
2330 if (tline && tline->type == TOK_WHITESPACE)
2331 tline = tline->next;
2332 if (!tline || tline->type != TOK_ID) {
H. Peter Anvin215186f2016-02-17 20:27:41 -08002333 nasm_error(ERR_NONFATAL, "`%%arg' missing argument parameter");
H. Peter Anvine2c80182005-01-15 22:15:51 +00002334 free_tlist(origline);
2335 return DIRECTIVE_FOUND;
2336 }
2337 arg = tline->text;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002338
H. Peter Anvine2c80182005-01-15 22:15:51 +00002339 /* Find the argument size type */
2340 tline = tline->next;
2341 if (!tline || tline->type != TOK_OTHER
2342 || tline->text[0] != ':') {
H. Peter Anvin215186f2016-02-17 20:27:41 -08002343 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00002344 "Syntax error processing `%%arg' directive");
2345 free_tlist(origline);
2346 return DIRECTIVE_FOUND;
2347 }
2348 tline = tline->next;
2349 if (!tline || tline->type != TOK_ID) {
H. Peter Anvin215186f2016-02-17 20:27:41 -08002350 nasm_error(ERR_NONFATAL, "`%%arg' missing size type parameter");
H. Peter Anvine2c80182005-01-15 22:15:51 +00002351 free_tlist(origline);
2352 return DIRECTIVE_FOUND;
2353 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002354
H. Peter Anvine2c80182005-01-15 22:15:51 +00002355 /* Allow macro expansion of type parameter */
Keith Kaniosb7a89542007-04-12 02:40:54 +00002356 tt = tokenize(tline->text);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002357 tt = expand_smacro(tt);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002358 size = parse_size(tt->text);
2359 if (!size) {
H. Peter Anvin215186f2016-02-17 20:27:41 -08002360 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00002361 "Invalid size type for `%%arg' missing directive");
2362 free_tlist(tt);
2363 free_tlist(origline);
2364 return DIRECTIVE_FOUND;
2365 }
2366 free_tlist(tt);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002367
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002368 /* Round up to even stack slots */
2369 size = ALIGN(size, StackSize);
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002370
H. Peter Anvine2c80182005-01-15 22:15:51 +00002371 /* Now define the macro for the argument */
2372 snprintf(directive, sizeof(directive), "%%define %s (%s+%d)",
2373 arg, StackPointer, offset);
Keith Kaniosb7a89542007-04-12 02:40:54 +00002374 do_directive(tokenize(directive));
H. Peter Anvine2c80182005-01-15 22:15:51 +00002375 offset += size;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002376
H. Peter Anvine2c80182005-01-15 22:15:51 +00002377 /* Move to the next argument in the list */
2378 tline = tline->next;
2379 if (tline && tline->type == TOK_WHITESPACE)
2380 tline = tline->next;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002381 } while (tline && tline->type == TOK_OTHER && tline->text[0] == ',');
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002382 ArgOffset = offset;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002383 free_tlist(origline);
2384 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002385
H. Peter Anvine2c80182005-01-15 22:15:51 +00002386 case PP_LOCAL:
2387 /* TASM like LOCAL directive to define local variables for a
2388 * function, in the following form:
2389 *
2390 * LOCAL local1:WORD, local2:DWORD, local4:QWORD = LocalSize
2391 *
2392 * The '= LocalSize' at the end is ignored by NASM, but is
2393 * required by TASM to define the local parameter size (and used
2394 * by the TASM macro package).
2395 */
2396 offset = LocalOffset;
2397 do {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00002398 char *local, directive[256];
H. Peter Anvine2c80182005-01-15 22:15:51 +00002399 int size = StackSize;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002400
H. Peter Anvine2c80182005-01-15 22:15:51 +00002401 /* Find the argument name */
2402 tline = tline->next;
2403 if (tline && tline->type == TOK_WHITESPACE)
2404 tline = tline->next;
2405 if (!tline || tline->type != TOK_ID) {
H. Peter Anvin215186f2016-02-17 20:27:41 -08002406 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00002407 "`%%local' missing argument parameter");
2408 free_tlist(origline);
2409 return DIRECTIVE_FOUND;
2410 }
2411 local = tline->text;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002412
H. Peter Anvine2c80182005-01-15 22:15:51 +00002413 /* Find the argument size type */
2414 tline = tline->next;
2415 if (!tline || tline->type != TOK_OTHER
2416 || tline->text[0] != ':') {
H. Peter Anvin215186f2016-02-17 20:27:41 -08002417 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00002418 "Syntax error processing `%%local' directive");
2419 free_tlist(origline);
2420 return DIRECTIVE_FOUND;
2421 }
2422 tline = tline->next;
2423 if (!tline || tline->type != TOK_ID) {
H. Peter Anvin215186f2016-02-17 20:27:41 -08002424 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00002425 "`%%local' missing size type parameter");
2426 free_tlist(origline);
2427 return DIRECTIVE_FOUND;
2428 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00002429
H. Peter Anvine2c80182005-01-15 22:15:51 +00002430 /* Allow macro expansion of type parameter */
Keith Kaniosb7a89542007-04-12 02:40:54 +00002431 tt = tokenize(tline->text);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002432 tt = expand_smacro(tt);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002433 size = parse_size(tt->text);
2434 if (!size) {
H. Peter Anvin215186f2016-02-17 20:27:41 -08002435 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00002436 "Invalid size type for `%%local' missing directive");
2437 free_tlist(tt);
2438 free_tlist(origline);
2439 return DIRECTIVE_FOUND;
2440 }
2441 free_tlist(tt);
H. Peter Anvin734b1882002-04-30 21:01:08 +00002442
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002443 /* Round up to even stack slots */
2444 size = ALIGN(size, StackSize);
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002445
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002446 offset += size; /* Negative offset, increment before */
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002447
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002448 /* Now define the macro for the argument */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002449 snprintf(directive, sizeof(directive), "%%define %s (%s-%d)",
2450 local, StackPointer, offset);
Keith Kaniosb7a89542007-04-12 02:40:54 +00002451 do_directive(tokenize(directive));
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002452
H. Peter Anvine2c80182005-01-15 22:15:51 +00002453 /* Now define the assign to setup the enter_c macro correctly */
2454 snprintf(directive, sizeof(directive),
2455 "%%assign %%$localsize %%$localsize+%d", size);
Keith Kaniosb7a89542007-04-12 02:40:54 +00002456 do_directive(tokenize(directive));
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002457
H. Peter Anvine2c80182005-01-15 22:15:51 +00002458 /* Move to the next argument in the list */
2459 tline = tline->next;
2460 if (tline && tline->type == TOK_WHITESPACE)
2461 tline = tline->next;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002462 } while (tline && tline->type == TOK_OTHER && tline->text[0] == ',');
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002463 LocalOffset = offset;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002464 free_tlist(origline);
2465 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002466
H. Peter Anvine2c80182005-01-15 22:15:51 +00002467 case PP_CLEAR:
2468 if (tline->next)
H. Peter Anvin215186f2016-02-17 20:27:41 -08002469 nasm_error(ERR_WARNING|ERR_PASS1,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002470 "trailing garbage after `%%clear' ignored");
2471 free_macros();
2472 init_macros();
H. Peter Anvine2c80182005-01-15 22:15:51 +00002473 free_tlist(origline);
2474 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002475
H. Peter Anvin418ca702008-05-30 10:42:30 -07002476 case PP_DEPEND:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002477 t = tline->next = expand_smacro(tline->next);
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002478 skip_white_(t);
2479 if (!t || (t->type != TOK_STRING &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002480 t->type != TOK_INTERNAL_STRING)) {
H. Peter Anvin215186f2016-02-17 20:27:41 -08002481 nasm_error(ERR_NONFATAL, "`%%depend' expects a file name");
H. Peter Anvin418ca702008-05-30 10:42:30 -07002482 free_tlist(origline);
2483 return DIRECTIVE_FOUND; /* but we did _something_ */
2484 }
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002485 if (t->next)
H. Peter Anvin215186f2016-02-17 20:27:41 -08002486 nasm_error(ERR_WARNING|ERR_PASS1,
H. Peter Anvin418ca702008-05-30 10:42:30 -07002487 "trailing garbage after `%%depend' ignored");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002488 p = t->text;
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002489 if (t->type != TOK_INTERNAL_STRING)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002490 nasm_unquote_cstr(p, i);
2491 if (dephead && !in_list(*dephead, p)) {
2492 StrList *sl = nasm_malloc(strlen(p)+1+sizeof sl->next);
2493 sl->next = NULL;
2494 strcpy(sl->str, p);
2495 *deptail = sl;
2496 deptail = &sl->next;
2497 }
2498 free_tlist(origline);
H. Peter Anvin418ca702008-05-30 10:42:30 -07002499 return DIRECTIVE_FOUND;
2500
2501 case PP_INCLUDE:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002502 t = tline->next = expand_smacro(tline->next);
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002503 skip_white_(t);
H. Peter Anvind2456592008-06-19 15:04:18 -07002504
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002505 if (!t || (t->type != TOK_STRING &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002506 t->type != TOK_INTERNAL_STRING)) {
H. Peter Anvin215186f2016-02-17 20:27:41 -08002507 nasm_error(ERR_NONFATAL, "`%%include' expects a file name");
H. Peter Anvine2c80182005-01-15 22:15:51 +00002508 free_tlist(origline);
2509 return DIRECTIVE_FOUND; /* but we did _something_ */
2510 }
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002511 if (t->next)
H. Peter Anvin215186f2016-02-17 20:27:41 -08002512 nasm_error(ERR_WARNING|ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +00002513 "trailing garbage after `%%include' ignored");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002514 p = t->text;
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002515 if (t->type != TOK_INTERNAL_STRING)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002516 nasm_unquote_cstr(p, i);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002517 inc = nasm_malloc(sizeof(Include));
H. Peter Anvine2c80182005-01-15 22:15:51 +00002518 inc->next = istk;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002519 inc->conds = NULL;
H. Peter Anvin2b1c3b92008-06-06 10:38:46 -07002520 inc->fp = inc_fopen(p, dephead, &deptail, pass == 0);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002521 if (!inc->fp) {
2522 /* -MG given but file not found */
2523 nasm_free(inc);
2524 } else {
2525 inc->fname = src_set_fname(nasm_strdup(p));
2526 inc->lineno = src_set_linnum(0);
2527 inc->lineinc = 1;
2528 inc->expansion = NULL;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002529 inc->mstk = NULL;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002530 istk = inc;
H. Peter Anvin172b8402016-02-18 01:16:18 -08002531 lfmt->uplevel(LIST_INCLUDE);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002532 }
2533 free_tlist(origline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002534 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002535
H. Peter Anvind2456592008-06-19 15:04:18 -07002536 case PP_USE:
H. Peter Anvinf4ae5ad2008-06-19 18:39:24 -07002537 {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002538 static macros_t *use_pkg;
2539 const char *pkg_macro = NULL;
H. Peter Anvinf4ae5ad2008-06-19 18:39:24 -07002540
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002541 tline = tline->next;
2542 skip_white_(tline);
2543 tline = expand_id(tline);
H. Peter Anvind2456592008-06-19 15:04:18 -07002544
H. Peter Anvin264b7b92008-10-24 16:38:17 -07002545 if (!tline || (tline->type != TOK_STRING &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002546 tline->type != TOK_INTERNAL_STRING &&
2547 tline->type != TOK_ID)) {
H. Peter Anvin215186f2016-02-17 20:27:41 -08002548 nasm_error(ERR_NONFATAL, "`%%use' expects a package name");
H. Peter Anvind2456592008-06-19 15:04:18 -07002549 free_tlist(origline);
2550 return DIRECTIVE_FOUND; /* but we did _something_ */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002551 }
H. Peter Anvin264b7b92008-10-24 16:38:17 -07002552 if (tline->next)
H. Peter Anvin215186f2016-02-17 20:27:41 -08002553 nasm_error(ERR_WARNING|ERR_PASS1,
H. Peter Anvind2456592008-06-19 15:04:18 -07002554 "trailing garbage after `%%use' ignored");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002555 if (tline->type == TOK_STRING)
2556 nasm_unquote_cstr(tline->text, i);
2557 use_pkg = nasm_stdmac_find_package(tline->text);
2558 if (!use_pkg)
H. Peter Anvin215186f2016-02-17 20:27:41 -08002559 nasm_error(ERR_NONFATAL, "unknown `%%use' package: %s", tline->text);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002560 else
2561 pkg_macro = (char *)use_pkg + 1; /* The first string will be <%define>__USE_*__ */
Victor van den Elzen35eb2ea2010-03-10 22:33:48 +01002562 if (use_pkg && ! smacro_defined(NULL, pkg_macro, 0, NULL, true)) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002563 /* Not already included, go ahead and include it */
2564 stdmacpos = use_pkg;
2565 }
2566 free_tlist(origline);
H. Peter Anvind2456592008-06-19 15:04:18 -07002567 return DIRECTIVE_FOUND;
H. Peter Anvinf4ae5ad2008-06-19 18:39:24 -07002568 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002569 case PP_PUSH:
H. Peter Anvine2c80182005-01-15 22:15:51 +00002570 case PP_REPL:
H. Peter Anvin42b56392008-10-24 16:24:21 -07002571 case PP_POP:
H. Peter Anvine2c80182005-01-15 22:15:51 +00002572 tline = tline->next;
2573 skip_white_(tline);
2574 tline = expand_id(tline);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002575 if (tline) {
2576 if (!tok_type_(tline, TOK_ID)) {
H. Peter Anvin215186f2016-02-17 20:27:41 -08002577 nasm_error(ERR_NONFATAL, "`%s' expects a context identifier",
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002578 pp_directives[i]);
2579 free_tlist(origline);
2580 return DIRECTIVE_FOUND; /* but we did _something_ */
2581 }
2582 if (tline->next)
H. Peter Anvin215186f2016-02-17 20:27:41 -08002583 nasm_error(ERR_WARNING|ERR_PASS1,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002584 "trailing garbage after `%s' ignored",
2585 pp_directives[i]);
2586 p = nasm_strdup(tline->text);
2587 } else {
2588 p = NULL; /* Anonymous */
2589 }
H. Peter Anvin42b56392008-10-24 16:24:21 -07002590
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002591 if (i == PP_PUSH) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002592 ctx = nasm_malloc(sizeof(Context));
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002593 ctx->next = cstk;
2594 hash_init(&ctx->localmac, HASH_SMALL);
2595 ctx->name = p;
2596 ctx->number = unique++;
2597 cstk = ctx;
2598 } else {
2599 /* %pop or %repl */
2600 if (!cstk) {
H. Peter Anvin215186f2016-02-17 20:27:41 -08002601 nasm_error(ERR_NONFATAL, "`%s': context stack is empty",
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002602 pp_directives[i]);
2603 } else if (i == PP_POP) {
2604 if (p && (!cstk->name || nasm_stricmp(p, cstk->name)))
H. Peter Anvin215186f2016-02-17 20:27:41 -08002605 nasm_error(ERR_NONFATAL, "`%%pop' in wrong context: %s, "
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002606 "expected %s",
2607 cstk->name ? cstk->name : "anonymous", p);
2608 else
2609 ctx_pop();
2610 } else {
2611 /* i == PP_REPL */
2612 nasm_free(cstk->name);
2613 cstk->name = p;
2614 p = NULL;
2615 }
2616 nasm_free(p);
2617 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002618 free_tlist(origline);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002619 return DIRECTIVE_FOUND;
H. Peter Anvin8e3f75e2008-09-24 00:21:58 -07002620 case PP_FATAL:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002621 severity = ERR_FATAL;
2622 goto issue_error;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002623 case PP_ERROR:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002624 severity = ERR_NONFATAL;
2625 goto issue_error;
H. Peter Anvin7df04172008-06-10 18:27:38 -07002626 case PP_WARNING:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002627 severity = ERR_WARNING|ERR_WARN_USER;
2628 goto issue_error;
H. Peter Anvin8e3f75e2008-09-24 00:21:58 -07002629
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002630issue_error:
H. Peter Anvin7df04172008-06-10 18:27:38 -07002631 {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002632 /* Only error out if this is the final pass */
2633 if (pass != 2 && i != PP_FATAL)
2634 return DIRECTIVE_FOUND;
2635
2636 tline->next = expand_smacro(tline->next);
2637 tline = tline->next;
2638 skip_white_(tline);
2639 t = tline ? tline->next : NULL;
2640 skip_white_(t);
2641 if (tok_type_(tline, TOK_STRING) && !t) {
2642 /* The line contains only a quoted string */
2643 p = tline->text;
2644 nasm_unquote(p, NULL); /* Ignore NUL character truncation */
H. Peter Anvin215186f2016-02-17 20:27:41 -08002645 nasm_error(severity, "%s", p);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002646 } else {
2647 /* Not a quoted string, or more than a quoted string */
2648 p = detoken(tline, false);
H. Peter Anvin215186f2016-02-17 20:27:41 -08002649 nasm_error(severity, "%s", p);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002650 nasm_free(p);
2651 }
2652 free_tlist(origline);
2653 return DIRECTIVE_FOUND;
H. Peter Anvin7df04172008-06-10 18:27:38 -07002654 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00002655
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002656 CASE_PP_IF:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002657 if (istk->conds && !emitting(istk->conds->state))
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002658 j = COND_NEVER;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002659 else {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002660 j = if_condition(tline->next, i);
2661 tline->next = NULL; /* it got freed */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002662 j = j < 0 ? COND_NEVER : j ? COND_IF_TRUE : COND_IF_FALSE;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002663 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002664 cond = nasm_malloc(sizeof(Cond));
2665 cond->next = istk->conds;
2666 cond->state = j;
2667 istk->conds = cond;
2668 if(istk->mstk)
2669 istk->mstk->condcnt ++;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002670 free_tlist(origline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002671 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002672
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002673 CASE_PP_ELIF:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002674 if (!istk->conds)
H. Peter Anvin215186f2016-02-17 20:27:41 -08002675 nasm_error(ERR_FATAL, "`%s': no matching `%%if'", pp_directives[i]);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002676 switch(istk->conds->state) {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002677 case COND_IF_TRUE:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002678 istk->conds->state = COND_DONE;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002679 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002680
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002681 case COND_DONE:
2682 case COND_NEVER:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002683 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002684
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002685 case COND_ELSE_TRUE:
2686 case COND_ELSE_FALSE:
H. Peter Anvin215186f2016-02-17 20:27:41 -08002687 nasm_error(ERR_WARNING|ERR_PASS1|ERR_PP_PRECOND,
2688 "`%%elif' after `%%else' ignored");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002689 istk->conds->state = COND_NEVER;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002690 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002691
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002692 case COND_IF_FALSE:
2693 /*
2694 * IMPORTANT: In the case of %if, we will already have
2695 * called expand_mmac_params(); however, if we're
2696 * processing an %elif we must have been in a
2697 * non-emitting mode, which would have inhibited
2698 * the normal invocation of expand_mmac_params().
2699 * Therefore, we have to do it explicitly here.
2700 */
2701 j = if_condition(expand_mmac_params(tline->next), i);
2702 tline->next = NULL; /* it got freed */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002703 istk->conds->state =
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002704 j < 0 ? COND_NEVER : j ? COND_IF_TRUE : COND_IF_FALSE;
2705 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002706 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002707 free_tlist(origline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002708 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002709
H. Peter Anvine2c80182005-01-15 22:15:51 +00002710 case PP_ELSE:
2711 if (tline->next)
H. Peter Anvin215186f2016-02-17 20:27:41 -08002712 nasm_error(ERR_WARNING|ERR_PASS1|ERR_PP_PRECOND,
2713 "trailing garbage after `%%else' ignored");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002714 if (!istk->conds)
H. Peter Anvin215186f2016-02-17 20:27:41 -08002715 nasm_fatal(0, "`%%else: no matching `%%if'");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002716 switch(istk->conds->state) {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002717 case COND_IF_TRUE:
2718 case COND_DONE:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002719 istk->conds->state = COND_ELSE_FALSE;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002720 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002721
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002722 case COND_NEVER:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002723 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002724
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002725 case COND_IF_FALSE:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002726 istk->conds->state = COND_ELSE_TRUE;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002727 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002728
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002729 case COND_ELSE_TRUE:
2730 case COND_ELSE_FALSE:
H. Peter Anvin215186f2016-02-17 20:27:41 -08002731 nasm_error(ERR_WARNING|ERR_PASS1|ERR_PP_PRECOND,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002732 "`%%else' after `%%else' ignored.");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002733 istk->conds->state = COND_NEVER;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002734 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002735 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002736 free_tlist(origline);
2737 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002738
H. Peter Anvine2c80182005-01-15 22:15:51 +00002739 case PP_ENDIF:
2740 if (tline->next)
H. Peter Anvin215186f2016-02-17 20:27:41 -08002741 nasm_error(ERR_WARNING|ERR_PASS1|ERR_PP_PRECOND,
2742 "trailing garbage after `%%endif' ignored");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002743 if (!istk->conds)
H. Peter Anvin215186f2016-02-17 20:27:41 -08002744 nasm_error(ERR_FATAL, "`%%endif': no matching `%%if'");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002745 cond = istk->conds;
2746 istk->conds = cond->next;
2747 nasm_free(cond);
2748 if(istk->mstk)
2749 istk->mstk->condcnt --;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002750 free_tlist(origline);
2751 return DIRECTIVE_FOUND;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002752
H. Peter Anvindb8f96e2009-07-15 09:07:29 -04002753 case PP_RMACRO:
2754 case PP_IRMACRO:
H. Peter Anvine2c80182005-01-15 22:15:51 +00002755 case PP_MACRO:
2756 case PP_IMACRO:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002757 if (defining) {
H. Peter Anvin215186f2016-02-17 20:27:41 -08002758 nasm_error(ERR_FATAL, "`%s': already defining a macro",
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002759 pp_directives[i]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002760 return DIRECTIVE_FOUND;
2761 }
H. Peter Anvin4def1a82016-05-09 13:59:44 -07002762 defining = nasm_zalloc(sizeof(MMacro));
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002763 defining->max_depth =
2764 (i == PP_RMACRO) || (i == PP_IRMACRO) ? DEADMAN_LIMIT : 0;
2765 defining->casesense = (i == PP_MACRO) || (i == PP_RMACRO);
2766 if (!parse_mmacro_spec(tline, defining, pp_directives[i])) {
2767 nasm_free(defining);
2768 defining = NULL;
2769 return DIRECTIVE_FOUND;
2770 }
H. Peter Anvina26433d2008-07-16 14:40:01 -07002771
H. Peter Anvin4def1a82016-05-09 13:59:44 -07002772 src_get(&defining->xline, &defining->fname);
2773
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002774 mmac = (MMacro *) hash_findix(&mmacros, defining->name);
2775 while (mmac) {
2776 if (!strcmp(mmac->name, defining->name) &&
2777 (mmac->nparam_min <= defining->nparam_max
2778 || defining->plus)
2779 && (defining->nparam_min <= mmac->nparam_max
2780 || mmac->plus)) {
H. Peter Anvin215186f2016-02-17 20:27:41 -08002781 nasm_error(ERR_WARNING|ERR_PASS1,
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002782 "redefining multi-line macro `%s'", defining->name);
2783 return DIRECTIVE_FOUND;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002784 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002785 mmac = mmac->next;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002786 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002787 free_tlist(origline);
2788 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002789
H. Peter Anvine2c80182005-01-15 22:15:51 +00002790 case PP_ENDM:
2791 case PP_ENDMACRO:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002792 if (! (defining && defining->name)) {
H. Peter Anvin215186f2016-02-17 20:27:41 -08002793 nasm_error(ERR_NONFATAL, "`%s': not defining a macro", tline->text);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002794 return DIRECTIVE_FOUND;
2795 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002796 mmhead = (MMacro **) hash_findi_add(&mmacros, defining->name);
2797 defining->next = *mmhead;
2798 *mmhead = defining;
2799 defining = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002800 free_tlist(origline);
2801 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002802
H. Peter Anvin89cee572009-07-15 09:16:54 -04002803 case PP_EXITMACRO:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002804 /*
2805 * We must search along istk->expansion until we hit a
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002806 * macro-end marker for a macro with a name. Then we
2807 * bypass all lines between exitmacro and endmacro.
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002808 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002809 list_for_each(l, istk->expansion)
2810 if (l->finishes && l->finishes->name)
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002811 break;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002812
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002813 if (l) {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002814 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002815 * Remove all conditional entries relative to this
2816 * macro invocation. (safe to do in this context)
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002817 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002818 for ( ; l->finishes->condcnt > 0; l->finishes->condcnt --) {
2819 cond = istk->conds;
2820 istk->conds = cond->next;
2821 nasm_free(cond);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002822 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002823 istk->expansion = l;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002824 } else {
H. Peter Anvin215186f2016-02-17 20:27:41 -08002825 nasm_error(ERR_NONFATAL, "`%%exitmacro' not within `%%macro' block");
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002826 }
Keith Kanios852f1ee2009-07-12 00:19:55 -05002827 free_tlist(origline);
2828 return DIRECTIVE_FOUND;
2829
H. Peter Anvina26433d2008-07-16 14:40:01 -07002830 case PP_UNMACRO:
2831 case PP_UNIMACRO:
2832 {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002833 MMacro **mmac_p;
2834 MMacro spec;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002835
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002836 spec.casesense = (i == PP_UNMACRO);
2837 if (!parse_mmacro_spec(tline, &spec, pp_directives[i])) {
2838 return DIRECTIVE_FOUND;
2839 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002840 mmac_p = (MMacro **) hash_findi(&mmacros, spec.name, NULL);
2841 while (mmac_p && *mmac_p) {
2842 mmac = *mmac_p;
2843 if (mmac->casesense == spec.casesense &&
2844 !mstrcmp(mmac->name, spec.name, spec.casesense) &&
2845 mmac->nparam_min == spec.nparam_min &&
2846 mmac->nparam_max == spec.nparam_max &&
2847 mmac->plus == spec.plus) {
2848 *mmac_p = mmac->next;
2849 free_mmacro(mmac);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002850 } else {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002851 mmac_p = &mmac->next;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002852 }
2853 }
2854 free_tlist(origline);
2855 free_tlist(spec.dlist);
2856 return DIRECTIVE_FOUND;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002857 }
2858
H. Peter Anvine2c80182005-01-15 22:15:51 +00002859 case PP_ROTATE:
2860 if (tline->next && tline->next->type == TOK_WHITESPACE)
2861 tline = tline->next;
H. Peter Anvin89cee572009-07-15 09:16:54 -04002862 if (!tline->next) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002863 free_tlist(origline);
H. Peter Anvin215186f2016-02-17 20:27:41 -08002864 nasm_error(ERR_NONFATAL, "`%%rotate' missing rotate count");
H. Peter Anvine2c80182005-01-15 22:15:51 +00002865 return DIRECTIVE_FOUND;
2866 }
2867 t = expand_smacro(tline->next);
2868 tline->next = NULL;
2869 free_tlist(origline);
2870 tline = t;
2871 tptr = &t;
2872 tokval.t_type = TOKEN_INVALID;
2873 evalresult =
H. Peter Anvin215186f2016-02-17 20:27:41 -08002874 evaluate(ppscan, tptr, &tokval, NULL, pass, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002875 free_tlist(tline);
2876 if (!evalresult)
2877 return DIRECTIVE_FOUND;
2878 if (tokval.t_type)
H. Peter Anvin215186f2016-02-17 20:27:41 -08002879 nasm_error(ERR_WARNING|ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +00002880 "trailing garbage after expression ignored");
2881 if (!is_simple(evalresult)) {
H. Peter Anvin215186f2016-02-17 20:27:41 -08002882 nasm_error(ERR_NONFATAL, "non-constant value given to `%%rotate'");
H. Peter Anvine2c80182005-01-15 22:15:51 +00002883 return DIRECTIVE_FOUND;
2884 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002885 mmac = istk->mstk;
2886 while (mmac && !mmac->name) /* avoid mistaking %reps for macros */
2887 mmac = mmac->next_active;
2888 if (!mmac) {
H. Peter Anvin215186f2016-02-17 20:27:41 -08002889 nasm_error(ERR_NONFATAL, "`%%rotate' invoked outside a macro call");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002890 } else if (mmac->nparam == 0) {
H. Peter Anvin215186f2016-02-17 20:27:41 -08002891 nasm_error(ERR_NONFATAL,
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002892 "`%%rotate' invoked within macro without parameters");
2893 } else {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002894 int rotate = mmac->rotate + reloc_value(evalresult);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002895
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002896 rotate %= (int)mmac->nparam;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002897 if (rotate < 0)
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002898 rotate += mmac->nparam;
2899
2900 mmac->rotate = rotate;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002901 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002902 return DIRECTIVE_FOUND;
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00002903
H. Peter Anvine2c80182005-01-15 22:15:51 +00002904 case PP_REP:
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002905 nolist = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002906 do {
2907 tline = tline->next;
2908 } while (tok_type_(tline, TOK_WHITESPACE));
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00002909
H. Peter Anvine2c80182005-01-15 22:15:51 +00002910 if (tok_type_(tline, TOK_ID) &&
2911 nasm_stricmp(tline->text, ".nolist") == 0) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002912 nolist = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002913 do {
2914 tline = tline->next;
2915 } while (tok_type_(tline, TOK_WHITESPACE));
2916 }
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00002917
H. Peter Anvine2c80182005-01-15 22:15:51 +00002918 if (tline) {
2919 t = expand_smacro(tline);
2920 tptr = &t;
2921 tokval.t_type = TOKEN_INVALID;
2922 evalresult =
H. Peter Anvin215186f2016-02-17 20:27:41 -08002923 evaluate(ppscan, tptr, &tokval, NULL, pass, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002924 if (!evalresult) {
2925 free_tlist(origline);
2926 return DIRECTIVE_FOUND;
2927 }
2928 if (tokval.t_type)
H. Peter Anvin215186f2016-02-17 20:27:41 -08002929 nasm_error(ERR_WARNING|ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +00002930 "trailing garbage after expression ignored");
2931 if (!is_simple(evalresult)) {
H. Peter Anvin215186f2016-02-17 20:27:41 -08002932 nasm_error(ERR_NONFATAL, "non-constant value given to `%%rep'");
H. Peter Anvine2c80182005-01-15 22:15:51 +00002933 return DIRECTIVE_FOUND;
2934 }
Cyrill Gorcunove091d6e2010-08-09 13:58:22 +04002935 count = reloc_value(evalresult);
2936 if (count >= REP_LIMIT) {
H. Peter Anvin215186f2016-02-17 20:27:41 -08002937 nasm_error(ERR_NONFATAL, "`%%rep' value exceeds limit");
Cyrill Gorcunove091d6e2010-08-09 13:58:22 +04002938 count = 0;
2939 } else
2940 count++;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002941 } else {
H. Peter Anvin215186f2016-02-17 20:27:41 -08002942 nasm_error(ERR_NONFATAL, "`%%rep' expects a repeat count");
H. Peter Anvinf8ba53e2007-10-11 10:11:57 -07002943 count = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002944 }
2945 free_tlist(origline);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002946
2947 tmp_defining = defining;
2948 defining = nasm_malloc(sizeof(MMacro));
2949 defining->prev = NULL;
2950 defining->name = NULL; /* flags this macro as a %rep block */
2951 defining->casesense = false;
2952 defining->plus = false;
2953 defining->nolist = nolist;
2954 defining->in_progress = count;
2955 defining->max_depth = 0;
2956 defining->nparam_min = defining->nparam_max = 0;
2957 defining->defaults = NULL;
2958 defining->dlist = NULL;
2959 defining->expansion = NULL;
2960 defining->next_active = istk->mstk;
2961 defining->rep_nest = tmp_defining;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002962 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002963
H. Peter Anvine2c80182005-01-15 22:15:51 +00002964 case PP_ENDREP:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002965 if (!defining || defining->name) {
H. Peter Anvin215186f2016-02-17 20:27:41 -08002966 nasm_error(ERR_NONFATAL, "`%%endrep': no matching `%%rep'");
H. Peter Anvine2c80182005-01-15 22:15:51 +00002967 return DIRECTIVE_FOUND;
2968 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00002969
H. Peter Anvine2c80182005-01-15 22:15:51 +00002970 /*
2971 * Now we have a "macro" defined - although it has no name
2972 * and we won't be entering it in the hash tables - we must
2973 * push a macro-end marker for it on to istk->expansion.
2974 * After that, it will take care of propagating itself (a
2975 * macro-end marker line for a macro which is really a %rep
2976 * block will cause the macro to be re-expanded, complete
2977 * with another macro-end marker to ensure the process
2978 * continues) until the whole expansion is forcibly removed
2979 * from istk->expansion by a %exitrep.
2980 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002981 l = nasm_malloc(sizeof(Line));
2982 l->next = istk->expansion;
2983 l->finishes = defining;
2984 l->first = NULL;
2985 istk->expansion = l;
2986
2987 istk->mstk = defining;
2988
H. Peter Anvin172b8402016-02-18 01:16:18 -08002989 lfmt->uplevel(defining->nolist ? LIST_MACRO_NOLIST : LIST_MACRO);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002990 tmp_defining = defining;
2991 defining = defining->rep_nest;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002992 free_tlist(origline);
2993 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002994
H. Peter Anvine2c80182005-01-15 22:15:51 +00002995 case PP_EXITREP:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002996 /*
2997 * We must search along istk->expansion until we hit a
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002998 * macro-end marker for a macro with no name. Then we set
2999 * its `in_progress' flag to 0.
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003000 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003001 list_for_each(l, istk->expansion)
3002 if (l->finishes && !l->finishes->name)
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003003 break;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003004
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003005 if (l)
3006 l->finishes->in_progress = 1;
3007 else
H. Peter Anvin215186f2016-02-17 20:27:41 -08003008 nasm_error(ERR_NONFATAL, "`%%exitrep' not within `%%rep' block");
H. Peter Anvine2c80182005-01-15 22:15:51 +00003009 free_tlist(origline);
3010 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003011
H. Peter Anvine2c80182005-01-15 22:15:51 +00003012 case PP_XDEFINE:
3013 case PP_IXDEFINE:
3014 case PP_DEFINE:
3015 case PP_IDEFINE:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003016 casesense = (i == PP_DEFINE || i == PP_XDEFINE);
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07003017
H. Peter Anvine2c80182005-01-15 22:15:51 +00003018 tline = tline->next;
3019 skip_white_(tline);
3020 tline = expand_id(tline);
3021 if (!tline || (tline->type != TOK_ID &&
3022 (tline->type != TOK_PREPROC_ID ||
3023 tline->text[1] != '$'))) {
H. Peter Anvin215186f2016-02-17 20:27:41 -08003024 nasm_error(ERR_NONFATAL, "`%s' expects a macro identifier",
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003025 pp_directives[i]);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003026 free_tlist(origline);
3027 return DIRECTIVE_FOUND;
3028 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003029
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04003030 ctx = get_ctx(tline->text, &mname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003031 last = tline;
3032 param_start = tline = tline->next;
3033 nparam = 0;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003034
H. Peter Anvine2c80182005-01-15 22:15:51 +00003035 /* Expand the macro definition now for %xdefine and %ixdefine */
3036 if ((i == PP_XDEFINE) || (i == PP_IXDEFINE))
3037 tline = expand_smacro(tline);
H. Peter Anvin734b1882002-04-30 21:01:08 +00003038
H. Peter Anvine2c80182005-01-15 22:15:51 +00003039 if (tok_is_(tline, "(")) {
3040 /*
3041 * This macro has parameters.
3042 */
H. Peter Anvin734b1882002-04-30 21:01:08 +00003043
H. Peter Anvine2c80182005-01-15 22:15:51 +00003044 tline = tline->next;
3045 while (1) {
3046 skip_white_(tline);
3047 if (!tline) {
H. Peter Anvin215186f2016-02-17 20:27:41 -08003048 nasm_error(ERR_NONFATAL, "parameter identifier expected");
H. Peter Anvine2c80182005-01-15 22:15:51 +00003049 free_tlist(origline);
3050 return DIRECTIVE_FOUND;
3051 }
3052 if (tline->type != TOK_ID) {
H. Peter Anvin215186f2016-02-17 20:27:41 -08003053 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00003054 "`%s': parameter identifier expected",
3055 tline->text);
3056 free_tlist(origline);
3057 return DIRECTIVE_FOUND;
3058 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003059 tline->type = TOK_SMAC_PARAM + nparam++;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003060 tline = tline->next;
3061 skip_white_(tline);
3062 if (tok_is_(tline, ",")) {
3063 tline = tline->next;
H. Peter Anvinca348b62008-07-23 10:49:26 -04003064 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003065 if (!tok_is_(tline, ")")) {
H. Peter Anvin215186f2016-02-17 20:27:41 -08003066 nasm_error(ERR_NONFATAL,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003067 "`)' expected to terminate macro template");
3068 free_tlist(origline);
3069 return DIRECTIVE_FOUND;
3070 }
3071 break;
3072 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00003073 }
3074 last = tline;
3075 tline = tline->next;
3076 }
3077 if (tok_type_(tline, TOK_WHITESPACE))
3078 last = tline, tline = tline->next;
3079 macro_start = NULL;
3080 last->next = NULL;
3081 t = tline;
3082 while (t) {
3083 if (t->type == TOK_ID) {
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04003084 list_for_each(tt, param_start)
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003085 if (tt->type >= TOK_SMAC_PARAM &&
H. Peter Anvine2c80182005-01-15 22:15:51 +00003086 !strcmp(tt->text, t->text))
3087 t->type = tt->type;
3088 }
3089 tt = t->next;
3090 t->next = macro_start;
3091 macro_start = t;
3092 t = tt;
3093 }
3094 /*
3095 * Good. We now have a macro name, a parameter count, and a
3096 * token list (in reverse order) for an expansion. We ought
3097 * to be OK just to create an SMacro, store it, and let
3098 * free_tlist have the rest of the line (which we have
3099 * carefully re-terminated after chopping off the expansion
3100 * from the end).
3101 */
H. Peter Anvin4db5a162007-10-11 13:42:09 -07003102 define_smacro(ctx, mname, casesense, nparam, macro_start);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003103 free_tlist(origline);
3104 return DIRECTIVE_FOUND;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003105
H. Peter Anvine2c80182005-01-15 22:15:51 +00003106 case PP_UNDEF:
3107 tline = tline->next;
3108 skip_white_(tline);
3109 tline = expand_id(tline);
3110 if (!tline || (tline->type != TOK_ID &&
3111 (tline->type != TOK_PREPROC_ID ||
3112 tline->text[1] != '$'))) {
H. Peter Anvin215186f2016-02-17 20:27:41 -08003113 nasm_error(ERR_NONFATAL, "`%%undef' expects a macro identifier");
H. Peter Anvine2c80182005-01-15 22:15:51 +00003114 free_tlist(origline);
3115 return DIRECTIVE_FOUND;
3116 }
3117 if (tline->next) {
H. Peter Anvin215186f2016-02-17 20:27:41 -08003118 nasm_error(ERR_WARNING|ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +00003119 "trailing garbage after macro name ignored");
3120 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003121
H. Peter Anvine2c80182005-01-15 22:15:51 +00003122 /* Find the context that symbol belongs to */
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04003123 ctx = get_ctx(tline->text, &mname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003124 undef_smacro(ctx, mname);
3125 free_tlist(origline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003126 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003127
H. Peter Anvin9e200162008-06-04 17:23:14 -07003128 case PP_DEFSTR:
3129 case PP_IDEFSTR:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003130 casesense = (i == PP_DEFSTR);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003131
3132 tline = tline->next;
3133 skip_white_(tline);
3134 tline = expand_id(tline);
3135 if (!tline || (tline->type != TOK_ID &&
3136 (tline->type != TOK_PREPROC_ID ||
3137 tline->text[1] != '$'))) {
H. Peter Anvin215186f2016-02-17 20:27:41 -08003138 nasm_error(ERR_NONFATAL, "`%s' expects a macro identifier",
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003139 pp_directives[i]);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003140 free_tlist(origline);
3141 return DIRECTIVE_FOUND;
3142 }
3143
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04003144 ctx = get_ctx(tline->text, &mname);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003145 last = tline;
3146 tline = expand_smacro(tline->next);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003147 last->next = NULL;
H. Peter Anvin9e200162008-06-04 17:23:14 -07003148
3149 while (tok_type_(tline, TOK_WHITESPACE))
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003150 tline = delete_Token(tline);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003151
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003152 p = detoken(tline, false);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003153 macro_start = nasm_malloc(sizeof(*macro_start));
3154 macro_start->next = NULL;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003155 macro_start->text = nasm_quote(p, strlen(p));
3156 macro_start->type = TOK_STRING;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003157 macro_start->a.mac = NULL;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003158 nasm_free(p);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003159
3160 /*
3161 * We now have a macro name, an implicit parameter count of
3162 * zero, and a string token to use as an expansion. Create
3163 * and store an SMacro.
3164 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003165 define_smacro(ctx, mname, casesense, 0, macro_start);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003166 free_tlist(origline);
3167 return DIRECTIVE_FOUND;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003168
H. Peter Anvin2f55bda2009-07-14 15:04:04 -04003169 case PP_DEFTOK:
3170 case PP_IDEFTOK:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003171 casesense = (i == PP_DEFTOK);
3172
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003173 tline = tline->next;
3174 skip_white_(tline);
3175 tline = expand_id(tline);
3176 if (!tline || (tline->type != TOK_ID &&
3177 (tline->type != TOK_PREPROC_ID ||
3178 tline->text[1] != '$'))) {
H. Peter Anvin215186f2016-02-17 20:27:41 -08003179 nasm_error(ERR_NONFATAL,
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003180 "`%s' expects a macro identifier as first parameter",
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003181 pp_directives[i]);
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003182 free_tlist(origline);
3183 return DIRECTIVE_FOUND;
3184 }
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04003185 ctx = get_ctx(tline->text, &mname);
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003186 last = tline;
3187 tline = expand_smacro(tline->next);
3188 last->next = NULL;
3189
3190 t = tline;
3191 while (tok_type_(t, TOK_WHITESPACE))
3192 t = t->next;
3193 /* t should now point to the string */
Cyrill Gorcunov6908e582010-09-06 19:36:15 +04003194 if (!tok_type_(t, TOK_STRING)) {
H. Peter Anvin215186f2016-02-17 20:27:41 -08003195 nasm_error(ERR_NONFATAL,
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003196 "`%s` requires string as second parameter",
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003197 pp_directives[i]);
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003198 free_tlist(tline);
3199 free_tlist(origline);
3200 return DIRECTIVE_FOUND;
3201 }
3202
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04003203 /*
3204 * Convert the string to a token stream. Note that smacros
3205 * are stored with the token stream reversed, so we have to
3206 * reverse the output of tokenize().
3207 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003208 nasm_unquote_cstr(t->text, i);
H. Peter Anvinb40992c2010-09-15 08:57:21 -07003209 macro_start = reverse_tokens(tokenize(t->text));
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003210
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003211 /*
3212 * We now have a macro name, an implicit parameter count of
3213 * zero, and a numeric token to use as an expansion. Create
3214 * and store an SMacro.
3215 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003216 define_smacro(ctx, mname, casesense, 0, macro_start);
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003217 free_tlist(tline);
3218 free_tlist(origline);
3219 return DIRECTIVE_FOUND;
H. Peter Anvin9e200162008-06-04 17:23:14 -07003220
H. Peter Anvin418ca702008-05-30 10:42:30 -07003221 case PP_PATHSEARCH:
3222 {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003223 FILE *fp;
3224 StrList *xsl = NULL;
3225 StrList **xst = &xsl;
H. Peter Anvin418ca702008-05-30 10:42:30 -07003226
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003227 casesense = true;
H. Peter Anvin418ca702008-05-30 10:42:30 -07003228
3229 tline = tline->next;
3230 skip_white_(tline);
3231 tline = expand_id(tline);
3232 if (!tline || (tline->type != TOK_ID &&
3233 (tline->type != TOK_PREPROC_ID ||
3234 tline->text[1] != '$'))) {
H. Peter Anvin215186f2016-02-17 20:27:41 -08003235 nasm_error(ERR_NONFATAL,
H. Peter Anvin418ca702008-05-30 10:42:30 -07003236 "`%%pathsearch' expects a macro identifier as first parameter");
3237 free_tlist(origline);
3238 return DIRECTIVE_FOUND;
3239 }
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04003240 ctx = get_ctx(tline->text, &mname);
H. Peter Anvin418ca702008-05-30 10:42:30 -07003241 last = tline;
3242 tline = expand_smacro(tline->next);
3243 last->next = NULL;
3244
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003245 t = tline;
H. Peter Anvin418ca702008-05-30 10:42:30 -07003246 while (tok_type_(t, TOK_WHITESPACE))
3247 t = t->next;
3248
3249 if (!t || (t->type != TOK_STRING &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003250 t->type != TOK_INTERNAL_STRING)) {
H. Peter Anvin215186f2016-02-17 20:27:41 -08003251 nasm_error(ERR_NONFATAL, "`%%pathsearch' expects a file name");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003252 free_tlist(tline);
H. Peter Anvin418ca702008-05-30 10:42:30 -07003253 free_tlist(origline);
3254 return DIRECTIVE_FOUND; /* but we did _something_ */
3255 }
3256 if (t->next)
H. Peter Anvin215186f2016-02-17 20:27:41 -08003257 nasm_error(ERR_WARNING|ERR_PASS1,
H. Peter Anvin418ca702008-05-30 10:42:30 -07003258 "trailing garbage after `%%pathsearch' ignored");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003259 p = t->text;
H. Peter Anvin427cc912008-06-01 21:43:03 -07003260 if (t->type != TOK_INTERNAL_STRING)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003261 nasm_unquote(p, NULL);
H. Peter Anvin418ca702008-05-30 10:42:30 -07003262
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003263 fp = inc_fopen(p, &xsl, &xst, true);
3264 if (fp) {
3265 p = xsl->str;
3266 fclose(fp); /* Don't actually care about the file */
3267 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003268 macro_start = nasm_malloc(sizeof(*macro_start));
3269 macro_start->next = NULL;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003270 macro_start->text = nasm_quote(p, strlen(p));
3271 macro_start->type = TOK_STRING;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003272 macro_start->a.mac = NULL;
3273 if (xsl)
3274 nasm_free(xsl);
H. Peter Anvin418ca702008-05-30 10:42:30 -07003275
3276 /*
3277 * We now have a macro name, an implicit parameter count of
3278 * zero, and a string token to use as an expansion. Create
3279 * and store an SMacro.
3280 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003281 define_smacro(ctx, mname, casesense, 0, macro_start);
3282 free_tlist(tline);
H. Peter Anvin418ca702008-05-30 10:42:30 -07003283 free_tlist(origline);
3284 return DIRECTIVE_FOUND;
3285 }
3286
H. Peter Anvine2c80182005-01-15 22:15:51 +00003287 case PP_STRLEN:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003288 casesense = true;
H. Peter Anvin70653092007-10-19 14:42:29 -07003289
H. Peter Anvine2c80182005-01-15 22:15:51 +00003290 tline = tline->next;
3291 skip_white_(tline);
3292 tline = expand_id(tline);
3293 if (!tline || (tline->type != TOK_ID &&
3294 (tline->type != TOK_PREPROC_ID ||
3295 tline->text[1] != '$'))) {
H. Peter Anvin215186f2016-02-17 20:27:41 -08003296 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00003297 "`%%strlen' expects a macro identifier as first parameter");
3298 free_tlist(origline);
3299 return DIRECTIVE_FOUND;
3300 }
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04003301 ctx = get_ctx(tline->text, &mname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003302 last = tline;
3303 tline = expand_smacro(tline->next);
3304 last->next = NULL;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003305
H. Peter Anvine2c80182005-01-15 22:15:51 +00003306 t = tline;
3307 while (tok_type_(t, TOK_WHITESPACE))
3308 t = t->next;
3309 /* t should now point to the string */
Cyrill Gorcunov4e1d5ab2010-07-23 18:51:51 +04003310 if (!tok_type_(t, TOK_STRING)) {
H. Peter Anvin215186f2016-02-17 20:27:41 -08003311 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00003312 "`%%strlen` requires string as second parameter");
3313 free_tlist(tline);
3314 free_tlist(origline);
3315 return DIRECTIVE_FOUND;
3316 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003317
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003318 macro_start = nasm_malloc(sizeof(*macro_start));
3319 macro_start->next = NULL;
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07003320 make_tok_num(macro_start, nasm_unquote(t->text, NULL));
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003321 macro_start->a.mac = NULL;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003322
H. Peter Anvine2c80182005-01-15 22:15:51 +00003323 /*
3324 * We now have a macro name, an implicit parameter count of
3325 * zero, and a numeric token to use as an expansion. Create
3326 * and store an SMacro.
3327 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003328 define_smacro(ctx, mname, casesense, 0, macro_start);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003329 free_tlist(tline);
3330 free_tlist(origline);
3331 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003332
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003333 case PP_STRCAT:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003334 casesense = true;
H. Peter Anvinf26e0972008-07-01 21:26:27 -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 Anvin215186f2016-02-17 20:27:41 -08003342 nasm_error(ERR_NONFATAL,
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003343 "`%%strcat' 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 Anvinf26e0972008-07-01 21:26:27 -07003348 last = tline;
3349 tline = expand_smacro(tline->next);
3350 last->next = NULL;
3351
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003352 len = 0;
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04003353 list_for_each(t, tline) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003354 switch (t->type) {
3355 case TOK_WHITESPACE:
3356 break;
3357 case TOK_STRING:
3358 len += t->a.len = nasm_unquote(t->text, NULL);
3359 break;
3360 case TOK_OTHER:
3361 if (!strcmp(t->text, ",")) /* permit comma separators */
3362 break;
3363 /* else fall through */
3364 default:
H. Peter Anvin215186f2016-02-17 20:27:41 -08003365 nasm_error(ERR_NONFATAL,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003366 "non-string passed to `%%strcat' (%d)", t->type);
3367 free_tlist(tline);
3368 free_tlist(origline);
3369 return DIRECTIVE_FOUND;
3370 }
3371 }
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003372
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003373 p = pp = nasm_malloc(len);
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04003374 list_for_each(t, tline) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003375 if (t->type == TOK_STRING) {
3376 memcpy(p, t->text, t->a.len);
3377 p += t->a.len;
3378 }
3379 }
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003380
3381 /*
3382 * We now have a macro name, an implicit parameter count of
3383 * zero, and a numeric token to use as an expansion. Create
3384 * and store an SMacro.
3385 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003386 macro_start = new_Token(NULL, TOK_STRING, NULL, 0);
3387 macro_start->text = nasm_quote(pp, len);
3388 nasm_free(pp);
3389 define_smacro(ctx, mname, casesense, 0, macro_start);
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003390 free_tlist(tline);
3391 free_tlist(origline);
3392 return DIRECTIVE_FOUND;
3393
H. Peter Anvine2c80182005-01-15 22:15:51 +00003394 case PP_SUBSTR:
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003395 {
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003396 int64_t start, count;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003397 size_t len;
H. Peter Anvind2456592008-06-19 15:04:18 -07003398
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003399 casesense = true;
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07003400
H. Peter Anvine2c80182005-01-15 22:15:51 +00003401 tline = tline->next;
3402 skip_white_(tline);
3403 tline = expand_id(tline);
3404 if (!tline || (tline->type != TOK_ID &&
3405 (tline->type != TOK_PREPROC_ID ||
3406 tline->text[1] != '$'))) {
H. Peter Anvin215186f2016-02-17 20:27:41 -08003407 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00003408 "`%%substr' expects a macro identifier as first parameter");
3409 free_tlist(origline);
3410 return DIRECTIVE_FOUND;
3411 }
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04003412 ctx = get_ctx(tline->text, &mname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003413 last = tline;
3414 tline = expand_smacro(tline->next);
3415 last->next = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003416
Cyrill Gorcunov35519d62010-09-06 23:49:52 +04003417 if (tline) /* skip expanded id */
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003418 t = tline->next;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003419 while (tok_type_(t, TOK_WHITESPACE))
3420 t = t->next;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003421
H. Peter Anvine2c80182005-01-15 22:15:51 +00003422 /* t should now point to the string */
Cyrill Gorcunov35519d62010-09-06 23:49:52 +04003423 if (!tok_type_(t, TOK_STRING)) {
H. Peter Anvin215186f2016-02-17 20:27:41 -08003424 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00003425 "`%%substr` requires string as second parameter");
3426 free_tlist(tline);
3427 free_tlist(origline);
3428 return DIRECTIVE_FOUND;
3429 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003430
H. Peter Anvine2c80182005-01-15 22:15:51 +00003431 tt = t->next;
3432 tptr = &tt;
3433 tokval.t_type = TOKEN_INVALID;
H. Peter Anvin215186f2016-02-17 20:27:41 -08003434 evalresult = evaluate(ppscan, tptr, &tokval, NULL, pass, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003435 if (!evalresult) {
3436 free_tlist(tline);
3437 free_tlist(origline);
3438 return DIRECTIVE_FOUND;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003439 } else if (!is_simple(evalresult)) {
H. Peter Anvin215186f2016-02-17 20:27:41 -08003440 nasm_error(ERR_NONFATAL, "non-constant value given to `%%substr`");
H. Peter Anvine2c80182005-01-15 22:15:51 +00003441 free_tlist(tline);
3442 free_tlist(origline);
3443 return DIRECTIVE_FOUND;
3444 }
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003445 start = evalresult->value - 1;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003446
3447 while (tok_type_(tt, TOK_WHITESPACE))
3448 tt = tt->next;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003449 if (!tt) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003450 count = 1; /* Backwards compatibility: one character */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003451 } else {
3452 tokval.t_type = TOKEN_INVALID;
H. Peter Anvin215186f2016-02-17 20:27:41 -08003453 evalresult = evaluate(ppscan, tptr, &tokval, NULL, pass, NULL);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003454 if (!evalresult) {
3455 free_tlist(tline);
3456 free_tlist(origline);
3457 return DIRECTIVE_FOUND;
3458 } else if (!is_simple(evalresult)) {
H. Peter Anvin215186f2016-02-17 20:27:41 -08003459 nasm_error(ERR_NONFATAL, "non-constant value given to `%%substr`");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003460 free_tlist(tline);
3461 free_tlist(origline);
3462 return DIRECTIVE_FOUND;
3463 }
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003464 count = evalresult->value;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003465 }
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003466
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003467 len = nasm_unquote(t->text, NULL);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003468
Cyrill Gorcunovcff031e2010-09-07 20:31:11 +04003469 /* make start and count being in range */
3470 if (start < 0)
3471 start = 0;
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003472 if (count < 0)
3473 count = len + count + 1 - start;
3474 if (start + count > (int64_t)len)
Cyrill Gorcunovcff031e2010-09-07 20:31:11 +04003475 count = len - start;
3476 if (!len || count < 0 || start >=(int64_t)len)
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003477 start = -1, count = 0; /* empty string */
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003478
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003479 macro_start = nasm_malloc(sizeof(*macro_start));
3480 macro_start->next = NULL;
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003481 macro_start->text = nasm_quote((start < 0) ? "" : t->text + start, count);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003482 macro_start->type = TOK_STRING;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003483 macro_start->a.mac = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003484
H. Peter Anvine2c80182005-01-15 22:15:51 +00003485 /*
3486 * We now have a macro name, an implicit parameter count of
3487 * zero, and a numeric token to use as an expansion. Create
3488 * and store an SMacro.
3489 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003490 define_smacro(ctx, mname, casesense, 0, macro_start);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003491 free_tlist(tline);
3492 free_tlist(origline);
3493 return DIRECTIVE_FOUND;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003494 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003495
H. Peter Anvine2c80182005-01-15 22:15:51 +00003496 case PP_ASSIGN:
3497 case PP_IASSIGN:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003498 casesense = (i == PP_ASSIGN);
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07003499
H. Peter Anvine2c80182005-01-15 22:15:51 +00003500 tline = tline->next;
3501 skip_white_(tline);
3502 tline = expand_id(tline);
3503 if (!tline || (tline->type != TOK_ID &&
3504 (tline->type != TOK_PREPROC_ID ||
3505 tline->text[1] != '$'))) {
H. Peter Anvin215186f2016-02-17 20:27:41 -08003506 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00003507 "`%%%sassign' expects a macro identifier",
3508 (i == PP_IASSIGN ? "i" : ""));
3509 free_tlist(origline);
3510 return DIRECTIVE_FOUND;
3511 }
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04003512 ctx = get_ctx(tline->text, &mname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003513 last = tline;
3514 tline = expand_smacro(tline->next);
3515 last->next = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003516
H. Peter Anvine2c80182005-01-15 22:15:51 +00003517 t = tline;
3518 tptr = &t;
3519 tokval.t_type = TOKEN_INVALID;
H. Peter Anvin215186f2016-02-17 20:27:41 -08003520 evalresult = evaluate(ppscan, tptr, &tokval, NULL, pass, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003521 free_tlist(tline);
3522 if (!evalresult) {
3523 free_tlist(origline);
3524 return DIRECTIVE_FOUND;
3525 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003526
H. Peter Anvine2c80182005-01-15 22:15:51 +00003527 if (tokval.t_type)
H. Peter Anvin215186f2016-02-17 20:27:41 -08003528 nasm_error(ERR_WARNING|ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +00003529 "trailing garbage after expression ignored");
H. Peter Anvin734b1882002-04-30 21:01:08 +00003530
H. Peter Anvine2c80182005-01-15 22:15:51 +00003531 if (!is_simple(evalresult)) {
H. Peter Anvin215186f2016-02-17 20:27:41 -08003532 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00003533 "non-constant value given to `%%%sassign'",
3534 (i == PP_IASSIGN ? "i" : ""));
3535 free_tlist(origline);
3536 return DIRECTIVE_FOUND;
3537 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003538
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003539 macro_start = nasm_malloc(sizeof(*macro_start));
3540 macro_start->next = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003541 make_tok_num(macro_start, reloc_value(evalresult));
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003542 macro_start->a.mac = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003543
H. Peter Anvine2c80182005-01-15 22:15:51 +00003544 /*
3545 * We now have a macro name, an implicit parameter count of
3546 * zero, and a numeric token to use as an expansion. Create
3547 * and store an SMacro.
3548 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003549 define_smacro(ctx, mname, casesense, 0, macro_start);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003550 free_tlist(origline);
3551 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003552
H. Peter Anvine2c80182005-01-15 22:15:51 +00003553 case PP_LINE:
3554 /*
3555 * Syntax is `%line nnn[+mmm] [filename]'
3556 */
3557 tline = tline->next;
3558 skip_white_(tline);
3559 if (!tok_type_(tline, TOK_NUMBER)) {
H. Peter Anvin215186f2016-02-17 20:27:41 -08003560 nasm_error(ERR_NONFATAL, "`%%line' expects line number");
H. Peter Anvine2c80182005-01-15 22:15:51 +00003561 free_tlist(origline);
3562 return DIRECTIVE_FOUND;
3563 }
H. Peter Anvin70055962007-10-11 00:05:31 -07003564 k = readnum(tline->text, &err);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003565 m = 1;
3566 tline = tline->next;
3567 if (tok_is_(tline, "+")) {
3568 tline = tline->next;
3569 if (!tok_type_(tline, TOK_NUMBER)) {
H. Peter Anvin215186f2016-02-17 20:27:41 -08003570 nasm_error(ERR_NONFATAL, "`%%line' expects line increment");
H. Peter Anvine2c80182005-01-15 22:15:51 +00003571 free_tlist(origline);
3572 return DIRECTIVE_FOUND;
3573 }
H. Peter Anvin70055962007-10-11 00:05:31 -07003574 m = readnum(tline->text, &err);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003575 tline = tline->next;
3576 }
3577 skip_white_(tline);
3578 src_set_linnum(k);
3579 istk->lineinc = m;
3580 if (tline) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07003581 nasm_free(src_set_fname(detoken(tline, false)));
H. Peter Anvine2c80182005-01-15 22:15:51 +00003582 }
3583 free_tlist(origline);
3584 return DIRECTIVE_FOUND;
Keith Kaniosb307a4f2010-11-06 17:41:51 -05003585
H. Peter Anvine2c80182005-01-15 22:15:51 +00003586 default:
H. Peter Anvin215186f2016-02-17 20:27:41 -08003587 nasm_error(ERR_FATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00003588 "preprocessor directive `%s' not yet implemented",
H. Peter Anvin4169a472007-09-12 01:29:43 +00003589 pp_directives[i]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003590 return DIRECTIVE_FOUND;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003591 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003592}
3593
3594/*
H. Peter Anvin76690a12002-04-30 20:52:49 +00003595 * Ensure that a macro parameter contains a condition code and
3596 * nothing else. Return the condition code index if so, or -1
3597 * otherwise.
3598 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003599static int find_cc(Token * t)
H. Peter Anvineba20a72002-04-30 20:53:55 +00003600{
H. Peter Anvin76690a12002-04-30 20:52:49 +00003601 Token *tt;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003602
H. Peter Anvin25a99342007-09-22 17:45:45 -07003603 if (!t)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003604 return -1; /* Probably a %+ without a space */
H. Peter Anvin25a99342007-09-22 17:45:45 -07003605
H. Peter Anvineba20a72002-04-30 20:53:55 +00003606 skip_white_(t);
H. Peter Anvin76690a12002-04-30 20:52:49 +00003607 if (t->type != TOK_ID)
H. Peter Anvine2c80182005-01-15 22:15:51 +00003608 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003609 tt = t->next;
H. Peter Anvineba20a72002-04-30 20:53:55 +00003610 skip_white_(tt);
H. Peter Anvin76690a12002-04-30 20:52:49 +00003611 if (tt && (tt->type != TOK_OTHER || strcmp(tt->text, ",")))
H. Peter Anvine2c80182005-01-15 22:15:51 +00003612 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003613
Cyrill Gorcunov19456392012-05-02 00:18:56 +04003614 return bsii(t->text, (const char **)conditions, ARRAY_SIZE(conditions));
H. Peter Anvin76690a12002-04-30 20:52:49 +00003615}
3616
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003617/*
3618 * This routines walks over tokens strem and hadnles tokens
3619 * pasting, if @handle_explicit passed then explicit pasting
3620 * term is handled, otherwise -- implicit pastings only.
3621 */
Cyrill Gorcunov575d4282010-10-06 00:25:55 +04003622static bool paste_tokens(Token **head, const struct tokseq_match *m,
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003623 size_t mnum, bool handle_explicit)
H. Peter Anvind784a082009-04-20 14:01:18 -07003624{
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003625 Token *tok, *next, **prev_next, **prev_nonspace;
3626 bool pasted = false;
3627 char *buf, *p;
3628 size_t len, i;
H. Peter Anvind784a082009-04-20 14:01:18 -07003629
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003630 /*
3631 * The last token before pasting. We need it
3632 * to be able to connect new handled tokens.
3633 * In other words if there were a tokens stream
3634 *
3635 * A -> B -> C -> D
3636 *
3637 * and we've joined tokens B and C, the resulting
3638 * stream should be
3639 *
3640 * A -> BC -> D
3641 */
3642 tok = *head;
3643 prev_next = NULL;
3644
3645 if (!tok_type_(tok, TOK_WHITESPACE) && !tok_type_(tok, TOK_PASTE))
3646 prev_nonspace = head;
3647 else
3648 prev_nonspace = NULL;
3649
3650 while (tok && (next = tok->next)) {
3651
3652 switch (tok->type) {
H. Peter Anvind784a082009-04-20 14:01:18 -07003653 case TOK_WHITESPACE:
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003654 /* Zap redundant whitespaces */
3655 while (tok_type_(next, TOK_WHITESPACE))
3656 next = delete_Token(next);
3657 tok->next = next;
H. Peter Anvind784a082009-04-20 14:01:18 -07003658 break;
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003659
3660 case TOK_PASTE:
3661 /* Explicit pasting */
3662 if (!handle_explicit)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003663 break;
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003664 next = delete_Token(tok);
3665
3666 while (tok_type_(next, TOK_WHITESPACE))
3667 next = delete_Token(next);
3668
3669 if (!pasted)
3670 pasted = true;
3671
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003672 /* Left pasting token is start of line */
3673 if (!prev_nonspace)
H. Peter Anvin215186f2016-02-17 20:27:41 -08003674 nasm_error(ERR_FATAL, "No lvalue found on pasting");
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003675
Cyrill Gorcunov8b5c9fb2013-02-04 01:24:54 +04003676 /*
3677 * No ending token, this might happen in two
3678 * cases
3679 *
3680 * 1) There indeed no right token at all
3681 * 2) There is a bare "%define ID" statement,
3682 * and @ID does expand to whitespace.
3683 *
3684 * So technically we need to do a grammar analysis
3685 * in another stage of parsing, but for now lets don't
3686 * change the behaviour people used to. Simply allow
3687 * whitespace after paste token.
3688 */
3689 if (!next) {
3690 /*
3691 * Zap ending space tokens and that's all.
3692 */
3693 tok = (*prev_nonspace)->next;
3694 while (tok_type_(tok, TOK_WHITESPACE))
3695 tok = delete_Token(tok);
3696 tok = *prev_nonspace;
3697 tok->next = NULL;
3698 break;
3699 }
3700
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003701 tok = *prev_nonspace;
3702 while (tok_type_(tok, TOK_WHITESPACE))
3703 tok = delete_Token(tok);
3704 len = strlen(tok->text);
3705 len += strlen(next->text);
3706
3707 p = buf = nasm_malloc(len + 1);
3708 strcpy(p, tok->text);
3709 p = strchr(p, '\0');
3710 strcpy(p, next->text);
3711
3712 delete_Token(tok);
3713
3714 tok = tokenize(buf);
3715 nasm_free(buf);
3716
3717 *prev_nonspace = tok;
3718 while (tok && tok->next)
3719 tok = tok->next;
3720
3721 tok->next = delete_Token(next);
3722
3723 /* Restart from pasted tokens head */
3724 tok = *prev_nonspace;
3725 break;
3726
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003727 default:
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003728 /* implicit pasting */
Cyrill Gorcunov575d4282010-10-06 00:25:55 +04003729 for (i = 0; i < mnum; i++) {
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003730 if (!(PP_CONCAT_MATCH(tok, m[i].mask_head)))
3731 continue;
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +04003732
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003733 len = 0;
3734 while (next && PP_CONCAT_MATCH(next, m[i].mask_tail)) {
3735 len += strlen(next->text);
3736 next = next->next;
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +04003737 }
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003738
3739 /* No match */
3740 if (tok == next)
3741 break;
3742
3743 len += strlen(tok->text);
3744 p = buf = nasm_malloc(len + 1);
3745
3746 while (tok != next) {
3747 strcpy(p, tok->text);
3748 p = strchr(p, '\0');
3749 tok = delete_Token(tok);
3750 }
3751
3752 tok = tokenize(buf);
3753 nasm_free(buf);
3754
3755 if (prev_next)
3756 *prev_next = tok;
3757 else
3758 *head = tok;
3759
3760 /*
3761 * Connect pasted into original stream,
3762 * ie A -> new-tokens -> B
3763 */
3764 while (tok && tok->next)
3765 tok = tok->next;
3766 tok->next = next;
3767
3768 if (!pasted)
3769 pasted = true;
3770
3771 /* Restart from pasted tokens head */
3772 tok = prev_next ? *prev_next : *head;
Cyrill Gorcunov575d4282010-10-06 00:25:55 +04003773 }
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003774
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003775 break;
H. Peter Anvind784a082009-04-20 14:01:18 -07003776 }
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003777
3778 prev_next = &tok->next;
3779
3780 if (tok->next &&
3781 !tok_type_(tok->next, TOK_WHITESPACE) &&
3782 !tok_type_(tok->next, TOK_PASTE))
3783 prev_nonspace = prev_next;
3784
3785 tok = tok->next;
H. Peter Anvind784a082009-04-20 14:01:18 -07003786 }
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003787
3788 return pasted;
H. Peter Anvind784a082009-04-20 14:01:18 -07003789}
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003790
3791/*
3792 * expands to a list of tokens from %{x:y}
3793 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003794static Token *expand_mmac_params_range(MMacro *mac, Token *tline, Token ***last)
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003795{
3796 Token *t = tline, **tt, *tm, *head;
3797 char *pos;
3798 int fst, lst, j, i;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003799
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003800 pos = strchr(tline->text, ':');
3801 nasm_assert(pos);
3802
3803 lst = atoi(pos + 1);
3804 fst = atoi(tline->text + 1);
3805
3806 /*
3807 * only macros params are accounted so
3808 * if someone passes %0 -- we reject such
3809 * value(s)
3810 */
3811 if (lst == 0 || fst == 0)
3812 goto err;
3813
3814 /* the values should be sane */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003815 if ((fst > (int)mac->nparam || fst < (-(int)mac->nparam)) ||
3816 (lst > (int)mac->nparam || lst < (-(int)mac->nparam)))
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003817 goto err;
3818
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003819 fst = fst < 0 ? fst + (int)mac->nparam + 1: fst;
3820 lst = lst < 0 ? lst + (int)mac->nparam + 1: lst;
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003821
3822 /* counted from zero */
3823 fst--, lst--;
3824
3825 /*
Cyrill Gorcunove75331c2013-11-09 12:02:15 +04003826 * It will be at least one token. Note we
3827 * need to scan params until separator, otherwise
3828 * only first token will be passed.
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003829 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003830 tm = mac->params[(fst + mac->rotate) % mac->nparam];
Cyrill Gorcunove75331c2013-11-09 12:02:15 +04003831 head = new_Token(NULL, tm->type, tm->text, 0);
3832 tt = &head->next, tm = tm->next;
3833 while (tok_isnt_(tm, ",")) {
3834 t = new_Token(NULL, tm->type, tm->text, 0);
3835 *tt = t, tt = &t->next, tm = tm->next;
3836 }
3837
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003838 if (fst < lst) {
3839 for (i = fst + 1; i <= lst; i++) {
3840 t = new_Token(NULL, TOK_OTHER, ",", 0);
3841 *tt = t, tt = &t->next;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003842 j = (i + mac->rotate) % mac->nparam;
3843 tm = mac->params[j];
Cyrill Gorcunove75331c2013-11-09 12:02:15 +04003844 while (tok_isnt_(tm, ",")) {
3845 t = new_Token(NULL, tm->type, tm->text, 0);
3846 *tt = t, tt = &t->next, tm = tm->next;
3847 }
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003848 }
3849 } else {
3850 for (i = fst - 1; i >= lst; i--) {
3851 t = new_Token(NULL, TOK_OTHER, ",", 0);
3852 *tt = t, tt = &t->next;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003853 j = (i + mac->rotate) % mac->nparam;
3854 tm = mac->params[j];
Cyrill Gorcunove75331c2013-11-09 12:02:15 +04003855 while (tok_isnt_(tm, ",")) {
3856 t = new_Token(NULL, tm->type, tm->text, 0);
3857 *tt = t, tt = &t->next, tm = tm->next;
3858 }
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003859 }
3860 }
3861
3862 *last = tt;
3863 return head;
3864
3865err:
H. Peter Anvin215186f2016-02-17 20:27:41 -08003866 nasm_error(ERR_NONFATAL, "`%%{%s}': macro parameters out of range",
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003867 &tline->text[1]);
3868 return tline;
3869}
3870
H. Peter Anvin76690a12002-04-30 20:52:49 +00003871/*
3872 * Expand MMacro-local things: parameter references (%0, %n, %+n,
H. Peter Anvin67c63722008-10-26 23:49:00 -07003873 * %-n) and MMacro-local identifiers (%%foo) as well as
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003874 * macro indirection (%[...]) and range (%{..:..}).
H. Peter Anvin76690a12002-04-30 20:52:49 +00003875 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003876static Token *expand_mmac_params(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00003877{
H. Peter Anvin734b1882002-04-30 21:01:08 +00003878 Token *t, *tt, **tail, *thead;
H. Peter Anvin6125b622009-04-08 14:02:25 -07003879 bool changed = false;
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003880 char *pos;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003881
3882 tail = &thead;
3883 thead = NULL;
3884
H. Peter Anvine2c80182005-01-15 22:15:51 +00003885 while (tline) {
3886 if (tline->type == TOK_PREPROC_ID &&
Cyrill Gorcunovca611192010-06-04 09:22:12 +04003887 (((tline->text[1] == '+' || tline->text[1] == '-') && tline->text[2]) ||
3888 (tline->text[1] >= '0' && tline->text[1] <= '9') ||
3889 tline->text[1] == '%')) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00003890 char *text = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003891 int type = 0, cc; /* type = 0 to placate optimisers */
Keith Kaniosa6dfa782007-04-13 16:47:53 +00003892 char tmpbuf[30];
H. Peter Anvin25a99342007-09-22 17:45:45 -07003893 unsigned int n;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003894 int i;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003895 MMacro *mac;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003896
H. Peter Anvine2c80182005-01-15 22:15:51 +00003897 t = tline;
3898 tline = tline->next;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003899
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003900 mac = istk->mstk;
3901 while (mac && !mac->name) /* avoid mistaking %reps for macros */
3902 mac = mac->next_active;
3903 if (!mac) {
H. Peter Anvin215186f2016-02-17 20:27:41 -08003904 nasm_error(ERR_NONFATAL, "`%s': not in a macro call", t->text);
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003905 } else {
3906 pos = strchr(t->text, ':');
3907 if (!pos) {
3908 switch (t->text[1]) {
3909 /*
3910 * We have to make a substitution of one of the
3911 * forms %1, %-1, %+1, %%foo, %0.
3912 */
3913 case '0':
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003914 type = TOK_NUMBER;
3915 snprintf(tmpbuf, sizeof(tmpbuf), "%d", mac->nparam);
3916 text = nasm_strdup(tmpbuf);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003917 break;
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003918 case '%':
H. Peter Anvine2c80182005-01-15 22:15:51 +00003919 type = TOK_ID;
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003920 snprintf(tmpbuf, sizeof(tmpbuf), "..@%"PRIu64".",
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003921 mac->unique);
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003922 text = nasm_strcat(tmpbuf, t->text + 2);
3923 break;
3924 case '-':
3925 n = atoi(t->text + 2) - 1;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003926 if (n >= mac->nparam)
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003927 tt = NULL;
3928 else {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003929 if (mac->nparam > 1)
3930 n = (n + mac->rotate) % mac->nparam;
3931 tt = mac->params[n];
H. Peter Anvine2c80182005-01-15 22:15:51 +00003932 }
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003933 cc = find_cc(tt);
3934 if (cc == -1) {
H. Peter Anvin215186f2016-02-17 20:27:41 -08003935 nasm_error(ERR_NONFATAL,
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003936 "macro parameter %d is not a condition code",
3937 n + 1);
3938 text = NULL;
3939 } else {
3940 type = TOK_ID;
3941 if (inverse_ccs[cc] == -1) {
H. Peter Anvin215186f2016-02-17 20:27:41 -08003942 nasm_error(ERR_NONFATAL,
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003943 "condition code `%s' is not invertible",
3944 conditions[cc]);
3945 text = NULL;
3946 } else
3947 text = nasm_strdup(conditions[inverse_ccs[cc]]);
3948 }
3949 break;
3950 case '+':
3951 n = atoi(t->text + 2) - 1;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003952 if (n >= mac->nparam)
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003953 tt = NULL;
3954 else {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003955 if (mac->nparam > 1)
3956 n = (n + mac->rotate) % mac->nparam;
3957 tt = mac->params[n];
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003958 }
3959 cc = find_cc(tt);
3960 if (cc == -1) {
H. Peter Anvin215186f2016-02-17 20:27:41 -08003961 nasm_error(ERR_NONFATAL,
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003962 "macro parameter %d is not a condition code",
3963 n + 1);
3964 text = NULL;
3965 } else {
3966 type = TOK_ID;
3967 text = nasm_strdup(conditions[cc]);
3968 }
3969 break;
3970 default:
3971 n = atoi(t->text + 1) - 1;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003972 if (n >= mac->nparam)
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003973 tt = NULL;
3974 else {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003975 if (mac->nparam > 1)
3976 n = (n + mac->rotate) % mac->nparam;
3977 tt = mac->params[n];
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003978 }
3979 if (tt) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003980 for (i = 0; i < mac->paramlen[n]; i++) {
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003981 *tail = new_Token(NULL, tt->type, tt->text, 0);
3982 tail = &(*tail)->next;
3983 tt = tt->next;
3984 }
3985 }
3986 text = NULL; /* we've done it here */
3987 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003988 }
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003989 } else {
3990 /*
3991 * seems we have a parameters range here
3992 */
3993 Token *head, **last;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003994 head = expand_mmac_params_range(mac, t, &last);
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003995 if (head != t) {
3996 *tail = head;
3997 *last = tline;
3998 tline = head;
3999 text = NULL;
4000 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00004001 }
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004002 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00004003 if (!text) {
4004 delete_Token(t);
4005 } else {
4006 *tail = t;
4007 tail = &t->next;
4008 t->type = type;
4009 nasm_free(t->text);
4010 t->text = text;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004011 t->a.mac = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004012 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004013 changed = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004014 continue;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004015 } else if (tline->type == TOK_INDIRECT) {
4016 t = tline;
4017 tline = tline->next;
4018 tt = tokenize(t->text);
4019 tt = expand_mmac_params(tt);
4020 tt = expand_smacro(tt);
4021 *tail = tt;
4022 while (tt) {
4023 tt->a.mac = NULL; /* Necessary? */
4024 tail = &tt->next;
4025 tt = tt->next;
4026 }
4027 delete_Token(t);
4028 changed = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004029 } else {
4030 t = *tail = tline;
4031 tline = tline->next;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004032 t->a.mac = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004033 tail = &t->next;
4034 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00004035 }
H. Peter Anvineba20a72002-04-30 20:53:55 +00004036 *tail = NULL;
H. Peter Anvin67c63722008-10-26 23:49:00 -07004037
Cyrill Gorcunovc6a742c2011-06-27 01:23:09 +04004038 if (changed) {
4039 const struct tokseq_match t[] = {
4040 {
4041 PP_CONCAT_MASK(TOK_ID) |
4042 PP_CONCAT_MASK(TOK_FLOAT), /* head */
4043 PP_CONCAT_MASK(TOK_ID) |
4044 PP_CONCAT_MASK(TOK_NUMBER) |
4045 PP_CONCAT_MASK(TOK_FLOAT) |
4046 PP_CONCAT_MASK(TOK_OTHER) /* tail */
4047 },
4048 {
4049 PP_CONCAT_MASK(TOK_NUMBER), /* head */
4050 PP_CONCAT_MASK(TOK_NUMBER) /* tail */
4051 }
4052 };
4053 paste_tokens(&thead, t, ARRAY_SIZE(t), false);
4054 }
H. Peter Anvin6125b622009-04-08 14:02:25 -07004055
H. Peter Anvin76690a12002-04-30 20:52:49 +00004056 return thead;
4057}
4058
4059/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004060 * Expand all single-line macro calls made in the given line.
4061 * Return the expanded version of the line. The original is deemed
4062 * to be destroyed in the process. (In reality we'll just move
4063 * Tokens from input to output a lot of the time, rather than
4064 * actually bothering to destroy and replicate.)
4065 */
H. Peter Anvincb1cf592007-11-19 12:26:50 -08004066
H. Peter Anvine2c80182005-01-15 22:15:51 +00004067static Token *expand_smacro(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004068{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004069 Token *t, *tt, *mstart, **tail, *thead;
H. Peter Anvineba20a72002-04-30 20:53:55 +00004070 SMacro *head = NULL, *m;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004071 Token **params;
4072 int *paramsize;
H. Peter Anvin25a99342007-09-22 17:45:45 -07004073 unsigned int nparam, sparam;
H. Peter Anvind784a082009-04-20 14:01:18 -07004074 int brackets;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004075 Token *org_tline = tline;
4076 Context *ctx;
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08004077 const char *mname;
H. Peter Anvin2a15e692007-11-19 13:14:59 -08004078 int deadman = DEADMAN_LIMIT;
H. Peter Anvin8287daf2009-07-07 16:00:58 -07004079 bool expanded;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004080
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004081 /*
4082 * Trick: we should avoid changing the start token pointer since it can
4083 * be contained in "next" field of other token. Because of this
4084 * we allocate a copy of first token and work with it; at the end of
4085 * routine we copy it back
4086 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004087 if (org_tline) {
Cyrill Gorcunoved4a8052010-04-09 15:40:35 +04004088 tline = new_Token(org_tline->next, org_tline->type,
4089 org_tline->text, 0);
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004090 tline->a.mac = org_tline->a.mac;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004091 nasm_free(org_tline->text);
4092 org_tline->text = NULL;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004093 }
4094
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004095 expanded = true; /* Always expand %+ at least once */
H. Peter Anvin8287daf2009-07-07 16:00:58 -07004096
H. Peter Anvincb1cf592007-11-19 12:26:50 -08004097again:
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004098 thead = NULL;
Cyrill Gorcunoved4a8052010-04-09 15:40:35 +04004099 tail = &thead;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004100
H. Peter Anvine2c80182005-01-15 22:15:51 +00004101 while (tline) { /* main token loop */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004102 if (!--deadman) {
H. Peter Anvin215186f2016-02-17 20:27:41 -08004103 nasm_error(ERR_NONFATAL, "interminable macro recursion");
Cyrill Gorcunovbd38c8f2009-11-21 11:11:23 +03004104 goto err;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004105 }
H. Peter Anvincb1cf592007-11-19 12:26:50 -08004106
H. Peter Anvine2c80182005-01-15 22:15:51 +00004107 if ((mname = tline->text)) {
4108 /* if this token is a local macro, look in local context */
Cyrill Gorcunovc56d9ad2010-02-11 15:12:19 +03004109 if (tline->type == TOK_ID) {
4110 head = (SMacro *)hash_findix(&smacros, mname);
4111 } else if (tline->type == TOK_PREPROC_ID) {
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04004112 ctx = get_ctx(mname, &mname);
Cyrill Gorcunovc56d9ad2010-02-11 15:12:19 +03004113 head = ctx ? (SMacro *)hash_findix(&ctx->localmac, mname) : NULL;
4114 } else
4115 head = NULL;
H. Peter Anvin072771e2008-05-22 13:17:51 -07004116
H. Peter Anvine2c80182005-01-15 22:15:51 +00004117 /*
4118 * We've hit an identifier. As in is_mmacro below, we first
4119 * check whether the identifier is a single-line macro at
4120 * all, then think about checking for parameters if
4121 * necessary.
4122 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004123 list_for_each(m, head)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004124 if (!mstrcmp(m->name, mname, m->casesense))
4125 break;
4126 if (m) {
4127 mstart = tline;
4128 params = NULL;
4129 paramsize = NULL;
4130 if (m->nparam == 0) {
4131 /*
4132 * Simple case: the macro is parameterless. Discard the
4133 * one token that the macro call took, and push the
4134 * expansion back on the to-do stack.
4135 */
4136 if (!m->expansion) {
4137 if (!strcmp("__FILE__", m->name)) {
4138 int32_t num = 0;
4139 char *file = NULL;
4140 src_get(&num, &file);
4141 tline->text = nasm_quote(file, strlen(file));
4142 tline->type = TOK_STRING;
4143 nasm_free(file);
H. Peter Anvine2c80182005-01-15 22:15:51 +00004144 continue;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004145 }
4146 if (!strcmp("__LINE__", m->name)) {
4147 nasm_free(tline->text);
4148 make_tok_num(tline, src_get_linnum());
4149 continue;
4150 }
4151 if (!strcmp("__BITS__", m->name)) {
4152 nasm_free(tline->text);
4153 make_tok_num(tline, globalbits);
4154 continue;
4155 }
4156 tline = delete_Token(tline);
4157 continue;
4158 }
4159 } else {
4160 /*
4161 * Complicated case: at least one macro with this name
H. Peter Anvine2c80182005-01-15 22:15:51 +00004162 * exists and takes parameters. We must find the
4163 * parameters in the call, count them, find the SMacro
4164 * that corresponds to that form of the macro call, and
4165 * substitute for the parameters when we expand. What a
4166 * pain.
4167 */
4168 /*tline = tline->next;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004169 skip_white_(tline); */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004170 do {
4171 t = tline->next;
4172 while (tok_type_(t, TOK_SMAC_END)) {
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004173 t->a.mac->in_progress = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004174 t->text = NULL;
4175 t = tline->next = delete_Token(t);
4176 }
4177 tline = t;
4178 } while (tok_type_(tline, TOK_WHITESPACE));
4179 if (!tok_is_(tline, "(")) {
4180 /*
4181 * This macro wasn't called with parameters: ignore
4182 * the call. (Behaviour borrowed from gnu cpp.)
4183 */
4184 tline = mstart;
4185 m = NULL;
4186 } else {
4187 int paren = 0;
4188 int white = 0;
4189 brackets = 0;
4190 nparam = 0;
4191 sparam = PARAM_DELTA;
4192 params = nasm_malloc(sparam * sizeof(Token *));
4193 params[0] = tline->next;
4194 paramsize = nasm_malloc(sparam * sizeof(int));
4195 paramsize[0] = 0;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07004196 while (true) { /* parameter loop */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004197 /*
4198 * For some unusual expansions
4199 * which concatenates function call
4200 */
4201 t = tline->next;
4202 while (tok_type_(t, TOK_SMAC_END)) {
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004203 t->a.mac->in_progress = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004204 t->text = NULL;
4205 t = tline->next = delete_Token(t);
4206 }
4207 tline = t;
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00004208
H. Peter Anvine2c80182005-01-15 22:15:51 +00004209 if (!tline) {
H. Peter Anvin215186f2016-02-17 20:27:41 -08004210 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00004211 "macro call expects terminating `)'");
4212 break;
4213 }
4214 if (tline->type == TOK_WHITESPACE
4215 && brackets <= 0) {
4216 if (paramsize[nparam])
4217 white++;
4218 else
4219 params[nparam] = tline->next;
4220 continue; /* parameter loop */
4221 }
4222 if (tline->type == TOK_OTHER
4223 && tline->text[1] == 0) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004224 char ch = tline->text[0];
H. Peter Anvine2c80182005-01-15 22:15:51 +00004225 if (ch == ',' && !paren && brackets <= 0) {
4226 if (++nparam >= sparam) {
4227 sparam += PARAM_DELTA;
4228 params = nasm_realloc(params,
Cyrill Gorcunoved4a8052010-04-09 15:40:35 +04004229 sparam * sizeof(Token *));
4230 paramsize = nasm_realloc(paramsize,
4231 sparam * sizeof(int));
H. Peter Anvine2c80182005-01-15 22:15:51 +00004232 }
4233 params[nparam] = tline->next;
4234 paramsize[nparam] = 0;
4235 white = 0;
4236 continue; /* parameter loop */
4237 }
4238 if (ch == '{' &&
4239 (brackets > 0 || (brackets == 0 &&
4240 !paramsize[nparam])))
4241 {
4242 if (!(brackets++)) {
4243 params[nparam] = tline->next;
4244 continue; /* parameter loop */
4245 }
4246 }
4247 if (ch == '}' && brackets > 0)
4248 if (--brackets == 0) {
4249 brackets = -1;
4250 continue; /* parameter loop */
4251 }
4252 if (ch == '(' && !brackets)
4253 paren++;
4254 if (ch == ')' && brackets <= 0)
4255 if (--paren < 0)
4256 break;
4257 }
4258 if (brackets < 0) {
4259 brackets = 0;
H. Peter Anvin215186f2016-02-17 20:27:41 -08004260 nasm_error(ERR_NONFATAL, "braces do not "
H. Peter Anvine2c80182005-01-15 22:15:51 +00004261 "enclose all of macro parameter");
4262 }
4263 paramsize[nparam] += white + 1;
4264 white = 0;
4265 } /* parameter loop */
4266 nparam++;
4267 while (m && (m->nparam != nparam ||
4268 mstrcmp(m->name, mname,
4269 m->casesense)))
4270 m = m->next;
4271 if (!m)
H. Peter Anvin215186f2016-02-17 20:27:41 -08004272 nasm_error(ERR_WARNING|ERR_PASS1|ERR_WARN_MNP,
H. Peter Anvine2c80182005-01-15 22:15:51 +00004273 "macro `%s' exists, "
4274 "but not taking %d parameters",
4275 mstart->text, nparam);
4276 }
4277 }
4278 if (m && m->in_progress)
4279 m = NULL;
4280 if (!m) { /* in progess or didn't find '(' or wrong nparam */
H. Peter Anvin70653092007-10-19 14:42:29 -07004281 /*
H. Peter Anvine2c80182005-01-15 22:15:51 +00004282 * Design question: should we handle !tline, which
4283 * indicates missing ')' here, or expand those
4284 * macros anyway, which requires the (t) test a few
H. Peter Anvin70653092007-10-19 14:42:29 -07004285 * lines down?
H. Peter Anvine2c80182005-01-15 22:15:51 +00004286 */
4287 nasm_free(params);
4288 nasm_free(paramsize);
4289 tline = mstart;
4290 } else {
4291 /*
4292 * Expand the macro: we are placed on the last token of the
4293 * call, so that we can easily split the call from the
4294 * following tokens. We also start by pushing an SMAC_END
4295 * token for the cycle removal.
4296 */
4297 t = tline;
4298 if (t) {
4299 tline = t->next;
4300 t->next = NULL;
4301 }
4302 tt = new_Token(tline, TOK_SMAC_END, NULL, 0);
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004303 tt->a.mac = m;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07004304 m->in_progress = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004305 tline = tt;
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04004306 list_for_each(t, m->expansion) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004307 if (t->type >= TOK_SMAC_PARAM) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00004308 Token *pcopy = tline, **ptail = &pcopy;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004309 Token *ttt, *pt;
4310 int i;
H. Peter Anvin734b1882002-04-30 21:01:08 +00004311
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004312 ttt = params[t->type - TOK_SMAC_PARAM];
4313 i = paramsize[t->type - TOK_SMAC_PARAM];
4314 while (--i >= 0) {
4315 pt = *ptail = new_Token(tline, ttt->type,
4316 ttt->text, 0);
4317 ptail = &pt->next;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004318 ttt = ttt->next;
4319 }
4320 tline = pcopy;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004321 } else if (t->type == TOK_PREPROC_Q) {
4322 tt = new_Token(tline, TOK_ID, mname, 0);
4323 tline = tt;
4324 } else if (t->type == TOK_PREPROC_QQ) {
4325 tt = new_Token(tline, TOK_ID, m->name, 0);
4326 tline = tt;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004327 } else {
4328 tt = new_Token(tline, t->type, t->text, 0);
4329 tline = tt;
4330 }
4331 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00004332
H. Peter Anvine2c80182005-01-15 22:15:51 +00004333 /*
4334 * Having done that, get rid of the macro call, and clean
4335 * up the parameters.
4336 */
4337 nasm_free(params);
4338 nasm_free(paramsize);
4339 free_tlist(mstart);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004340 expanded = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004341 continue; /* main token loop */
4342 }
4343 }
4344 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004345
H. Peter Anvine2c80182005-01-15 22:15:51 +00004346 if (tline->type == TOK_SMAC_END) {
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004347 tline->a.mac->in_progress = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004348 tline = delete_Token(tline);
4349 } else {
4350 t = *tail = tline;
4351 tline = tline->next;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004352 t->a.mac = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004353 t->next = NULL;
4354 tail = &t->next;
4355 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004356 }
4357
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004358 /*
4359 * Now scan the entire line and look for successive TOK_IDs that resulted
Keith Kaniosb7a89542007-04-12 02:40:54 +00004360 * after expansion (they can't be produced by tokenize()). The successive
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004361 * TOK_IDs should be concatenated.
4362 * Also we look for %+ tokens and concatenate the tokens before and after
4363 * them (without white spaces in between).
4364 */
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +04004365 if (expanded) {
Cyrill Gorcunovc6a742c2011-06-27 01:23:09 +04004366 const struct tokseq_match t[] = {
4367 {
4368 PP_CONCAT_MASK(TOK_ID) |
4369 PP_CONCAT_MASK(TOK_PREPROC_ID), /* head */
4370 PP_CONCAT_MASK(TOK_ID) |
4371 PP_CONCAT_MASK(TOK_PREPROC_ID) |
4372 PP_CONCAT_MASK(TOK_NUMBER) /* tail */
4373 }
4374 };
4375 if (paste_tokens(&thead, t, ARRAY_SIZE(t), true)) {
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +04004376 /*
4377 * If we concatenated something, *and* we had previously expanded
4378 * an actual macro, scan the lines again for macros...
4379 */
4380 tline = thead;
4381 expanded = false;
4382 goto again;
4383 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00004384 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004385
Cyrill Gorcunovbd38c8f2009-11-21 11:11:23 +03004386err:
H. Peter Anvine2c80182005-01-15 22:15:51 +00004387 if (org_tline) {
4388 if (thead) {
4389 *org_tline = *thead;
4390 /* since we just gave text to org_line, don't free it */
4391 thead->text = NULL;
4392 delete_Token(thead);
4393 } else {
4394 /* the expression expanded to empty line;
4395 we can't return NULL for some reasons
4396 we just set the line to a single WHITESPACE token. */
4397 memset(org_tline, 0, sizeof(*org_tline));
4398 org_tline->text = NULL;
4399 org_tline->type = TOK_WHITESPACE;
4400 }
4401 thead = org_tline;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004402 }
4403
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004404 return thead;
4405}
4406
4407/*
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004408 * Similar to expand_smacro but used exclusively with macro identifiers
4409 * right before they are fetched in. The reason is that there can be
4410 * identifiers consisting of several subparts. We consider that if there
4411 * are more than one element forming the name, user wants a expansion,
4412 * otherwise it will be left as-is. Example:
4413 *
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004414 * %define %$abc cde
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004415 *
4416 * the identifier %$abc will be left as-is so that the handler for %define
4417 * will suck it and define the corresponding value. Other case:
4418 *
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004419 * %define _%$abc cde
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004420 *
4421 * In this case user wants name to be expanded *before* %define starts
4422 * working, so we'll expand %$abc into something (if it has a value;
4423 * otherwise it will be left as-is) then concatenate all successive
4424 * PP_IDs into one.
4425 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004426static Token *expand_id(Token * tline)
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004427{
4428 Token *cur, *oldnext = NULL;
4429
H. Peter Anvin734b1882002-04-30 21:01:08 +00004430 if (!tline || !tline->next)
H. Peter Anvine2c80182005-01-15 22:15:51 +00004431 return tline;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004432
4433 cur = tline;
4434 while (cur->next &&
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004435 (cur->next->type == TOK_ID ||
4436 cur->next->type == TOK_PREPROC_ID
4437 || cur->next->type == TOK_NUMBER))
H. Peter Anvine2c80182005-01-15 22:15:51 +00004438 cur = cur->next;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004439
4440 /* If identifier consists of just one token, don't expand */
4441 if (cur == tline)
H. Peter Anvine2c80182005-01-15 22:15:51 +00004442 return tline;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004443
H. Peter Anvine2c80182005-01-15 22:15:51 +00004444 if (cur) {
4445 oldnext = cur->next; /* Detach the tail past identifier */
4446 cur->next = NULL; /* so that expand_smacro stops here */
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004447 }
4448
H. Peter Anvin734b1882002-04-30 21:01:08 +00004449 tline = expand_smacro(tline);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004450
H. Peter Anvine2c80182005-01-15 22:15:51 +00004451 if (cur) {
4452 /* expand_smacro possibly changhed tline; re-scan for EOL */
4453 cur = tline;
4454 while (cur && cur->next)
4455 cur = cur->next;
4456 if (cur)
4457 cur->next = oldnext;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004458 }
4459
4460 return tline;
4461}
4462
4463/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004464 * Determine whether the given line constitutes a multi-line macro
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004465 * call, and return the MMacro structure called if so. Doesn't have
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004466 * to check for an initial label - that's taken care of in
4467 * expand_mmacro - but must check numbers of parameters. Guaranteed
4468 * to be called with tline->type == TOK_ID, so the putative macro
4469 * name is easy to find.
4470 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004471static MMacro *is_mmacro(Token * tline, Token *** params_array)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004472{
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004473 MMacro *head, *m;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004474 Token **params;
4475 int nparam;
4476
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004477 head = (MMacro *) hash_findix(&mmacros, tline->text);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004478
4479 /*
4480 * Efficiency: first we see if any macro exists with the given
4481 * name. If not, we can return NULL immediately. _Then_ we
4482 * count the parameters, and then we look further along the
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004483 * list if necessary to find the proper MMacro.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004484 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004485 list_for_each(m, head)
4486 if (!mstrcmp(m->name, tline->text, m->casesense))
H. Peter Anvine2c80182005-01-15 22:15:51 +00004487 break;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004488 if (!m)
H. Peter Anvine2c80182005-01-15 22:15:51 +00004489 return NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004490
4491 /*
4492 * OK, we have a potential macro. Count and demarcate the
4493 * parameters.
4494 */
H. Peter Anvin734b1882002-04-30 21:01:08 +00004495 count_mmac_params(tline->next, &nparam, &params);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004496
4497 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004498 * So we know how many parameters we've got. Find the MMacro
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004499 * structure that handles this number.
4500 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004501 while (m) {
4502 if (m->nparam_min <= nparam
4503 && (m->plus || nparam <= m->nparam_max)) {
4504 /*
4505 * This one is right. Just check if cycle removal
4506 * prohibits us using it before we actually celebrate...
4507 */
4508 if (m->in_progress > m->max_depth) {
4509 if (m->max_depth > 0) {
H. Peter Anvin215186f2016-02-17 20:27:41 -08004510 nasm_error(ERR_WARNING,
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004511 "reached maximum recursion depth of %i",
4512 m->max_depth);
4513 }
4514 nasm_free(params);
4515 return NULL;
4516 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00004517 /*
4518 * It's right, and we can use it. Add its default
4519 * parameters to the end of our list if necessary.
4520 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004521 if (m->defaults && nparam < m->nparam_min + m->ndefs) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00004522 params =
4523 nasm_realloc(params,
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004524 ((m->nparam_min + m->ndefs +
H. Peter Anvine2c80182005-01-15 22:15:51 +00004525 1) * sizeof(*params)));
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004526 while (nparam < m->nparam_min + m->ndefs) {
4527 params[nparam] = m->defaults[nparam - m->nparam_min];
H. Peter Anvine2c80182005-01-15 22:15:51 +00004528 nparam++;
4529 }
4530 }
4531 /*
4532 * If we've gone over the maximum parameter count (and
4533 * we're in Plus mode), ignore parameters beyond
4534 * nparam_max.
4535 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004536 if (m->plus && nparam > m->nparam_max)
4537 nparam = m->nparam_max;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004538 /*
4539 * Then terminate the parameter list, and leave.
4540 */
4541 if (!params) { /* need this special case */
4542 params = nasm_malloc(sizeof(*params));
4543 nparam = 0;
4544 }
4545 params[nparam] = NULL;
4546 *params_array = params;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004547 return m;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004548 }
4549 /*
4550 * This one wasn't right: look for the next one with the
4551 * same name.
4552 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004553 list_for_each(m, m->next)
4554 if (!mstrcmp(m->name, tline->text, m->casesense))
H. Peter Anvine2c80182005-01-15 22:15:51 +00004555 break;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004556 }
4557
4558 /*
4559 * After all that, we didn't find one with the right number of
4560 * parameters. Issue a warning, and fail to expand the macro.
4561 */
H. Peter Anvin215186f2016-02-17 20:27:41 -08004562 nasm_error(ERR_WARNING|ERR_PASS1|ERR_WARN_MNP,
H. Peter Anvine2c80182005-01-15 22:15:51 +00004563 "macro `%s' exists, but not taking %d parameters",
4564 tline->text, nparam);
H. Peter Anvin734b1882002-04-30 21:01:08 +00004565 nasm_free(params);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004566 return NULL;
4567}
4568
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004569
4570/*
4571 * Save MMacro invocation specific fields in
4572 * preparation for a recursive macro expansion
4573 */
4574static void push_mmacro(MMacro *m)
4575{
4576 MMacroInvocation *i;
4577
4578 i = nasm_malloc(sizeof(MMacroInvocation));
4579 i->prev = m->prev;
4580 i->params = m->params;
4581 i->iline = m->iline;
4582 i->nparam = m->nparam;
4583 i->rotate = m->rotate;
4584 i->paramlen = m->paramlen;
4585 i->unique = m->unique;
4586 i->condcnt = m->condcnt;
4587 m->prev = i;
4588}
4589
4590
4591/*
4592 * Restore MMacro invocation specific fields that were
4593 * saved during a previous recursive macro expansion
4594 */
4595static void pop_mmacro(MMacro *m)
4596{
4597 MMacroInvocation *i;
4598
4599 if (m->prev) {
4600 i = m->prev;
4601 m->prev = i->prev;
4602 m->params = i->params;
4603 m->iline = i->iline;
4604 m->nparam = i->nparam;
4605 m->rotate = i->rotate;
4606 m->paramlen = i->paramlen;
4607 m->unique = i->unique;
4608 m->condcnt = i->condcnt;
4609 nasm_free(i);
4610 }
4611}
4612
4613
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004614/*
4615 * Expand the multi-line macro call made by the given line, if
4616 * there is one to be expanded. If there is, push the expansion on
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004617 * istk->expansion and return 1. Otherwise return 0.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004618 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004619static int expand_mmacro(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004620{
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004621 Token *startline = tline;
H. Peter Anvineba20a72002-04-30 20:53:55 +00004622 Token *label = NULL;
4623 int dont_prepend = 0;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004624 Token **params, *t, *tt;
4625 MMacro *m;
4626 Line *l, *ll;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004627 int i, nparam, *paramlen;
H. Peter Anvinc751e862008-06-09 10:18:45 -07004628 const char *mname;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004629
4630 t = tline;
H. Peter Anvineba20a72002-04-30 20:53:55 +00004631 skip_white_(t);
H. Peter Anvince2233b2008-05-25 21:57:00 -07004632 /* if (!tok_type_(t, TOK_ID)) Lino 02/25/02 */
H. Peter Anvindce1e2f2002-04-30 21:06:37 +00004633 if (!tok_type_(t, TOK_ID) && !tok_type_(t, TOK_PREPROC_ID))
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004634 return 0;
4635 m = is_mmacro(t, &params);
4636 if (m) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004637 mname = t->text;
H. Peter Anvinc751e862008-06-09 10:18:45 -07004638 } else {
H. Peter Anvine2c80182005-01-15 22:15:51 +00004639 Token *last;
4640 /*
4641 * We have an id which isn't a macro call. We'll assume
4642 * it might be a label; we'll also check to see if a
4643 * colon follows it. Then, if there's another id after
4644 * that lot, we'll check it again for macro-hood.
4645 */
4646 label = last = t;
4647 t = t->next;
4648 if (tok_type_(t, TOK_WHITESPACE))
4649 last = t, t = t->next;
4650 if (tok_is_(t, ":")) {
4651 dont_prepend = 1;
4652 last = t, t = t->next;
4653 if (tok_type_(t, TOK_WHITESPACE))
4654 last = t, t = t->next;
4655 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004656 if (!tok_type_(t, TOK_ID) || !(m = is_mmacro(t, &params)))
4657 return 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004658 last->next = NULL;
Keith Kanios891775e2009-07-11 06:08:54 -05004659 mname = t->text;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004660 tline = t;
H. Peter Anvineba20a72002-04-30 20:53:55 +00004661 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004662
4663 /*
4664 * Fix up the parameters: this involves stripping leading and
4665 * trailing whitespace, then stripping braces if they are
4666 * present.
4667 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004668 for (nparam = 0; params[nparam]; nparam++) ;
H. Peter Anvin734b1882002-04-30 21:01:08 +00004669 paramlen = nparam ? nasm_malloc(nparam * sizeof(*paramlen)) : NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004670
H. Peter Anvine2c80182005-01-15 22:15:51 +00004671 for (i = 0; params[i]; i++) {
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08004672 int brace = 0;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004673 int comma = (!m->plus || i < nparam - 1);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004674
H. Peter Anvine2c80182005-01-15 22:15:51 +00004675 t = params[i];
4676 skip_white_(t);
4677 if (tok_is_(t, "{"))
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08004678 t = t->next, brace++, comma = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004679 params[i] = t;
4680 paramlen[i] = 0;
4681 while (t) {
4682 if (comma && t->type == TOK_OTHER && !strcmp(t->text, ","))
4683 break; /* ... because we have hit a comma */
4684 if (comma && t->type == TOK_WHITESPACE
4685 && tok_is_(t->next, ","))
4686 break; /* ... or a space then a comma */
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08004687 if (brace && t->type == TOK_OTHER) {
4688 if (t->text[0] == '{')
4689 brace++; /* ... or a nested opening brace */
4690 else if (t->text[0] == '}')
4691 if (!--brace)
4692 break; /* ... or a brace */
4693 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00004694 t = t->next;
4695 paramlen[i]++;
4696 }
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08004697 if (brace)
H. Peter Anvin215186f2016-02-17 20:27:41 -08004698 nasm_error(ERR_NONFATAL, "macro params should be enclosed in braces");
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004699 }
4700
4701 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004702 * OK, we have a MMacro structure together with a set of
4703 * parameters. We must now go through the expansion and push
4704 * copies of each Line on to istk->expansion. Substitution of
H. Peter Anvin76690a12002-04-30 20:52:49 +00004705 * parameter tokens and macro-local tokens doesn't get done
4706 * until the single-line macro substitution process; this is
4707 * because delaying them allows us to change the semantics
4708 * later through %rotate.
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004709 *
4710 * First, push an end marker on to istk->expansion, mark this
4711 * macro as in progress, and set up its invocation-specific
4712 * variables.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004713 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004714 ll = nasm_malloc(sizeof(Line));
4715 ll->next = istk->expansion;
4716 ll->finishes = m;
4717 ll->first = NULL;
4718 istk->expansion = ll;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004719
4720 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004721 * Save the previous MMacro expansion in the case of
4722 * macro recursion
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004723 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004724 if (m->max_depth && m->in_progress)
4725 push_mmacro(m);
4726
4727 m->in_progress ++;
4728 m->params = params;
4729 m->iline = tline;
4730 m->nparam = nparam;
4731 m->rotate = 0;
4732 m->paramlen = paramlen;
4733 m->unique = unique++;
4734 m->lineno = 0;
4735 m->condcnt = 0;
4736
4737 m->next_active = istk->mstk;
4738 istk->mstk = m;
4739
4740 list_for_each(l, m->expansion) {
4741 Token **tail;
4742
4743 ll = nasm_malloc(sizeof(Line));
4744 ll->finishes = NULL;
4745 ll->next = istk->expansion;
4746 istk->expansion = ll;
4747 tail = &ll->first;
4748
4749 list_for_each(t, l->first) {
4750 Token *x = t;
4751 switch (t->type) {
4752 case TOK_PREPROC_Q:
4753 tt = *tail = new_Token(NULL, TOK_ID, mname, 0);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004754 break;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004755 case TOK_PREPROC_QQ:
4756 tt = *tail = new_Token(NULL, TOK_ID, m->name, 0);
4757 break;
4758 case TOK_PREPROC_ID:
4759 if (t->text[1] == '0' && t->text[2] == '0') {
4760 dont_prepend = -1;
4761 x = label;
4762 if (!x)
4763 continue;
4764 }
4765 /* fall through */
4766 default:
4767 tt = *tail = new_Token(NULL, x->type, x->text, 0);
4768 break;
4769 }
4770 tail = &tt->next;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004771 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004772 *tail = NULL;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004773 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004774
4775 /*
H. Peter Anvineba20a72002-04-30 20:53:55 +00004776 * If we had a label, push it on as the first line of
4777 * the macro expansion.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004778 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004779 if (label) {
4780 if (dont_prepend < 0)
4781 free_tlist(startline);
4782 else {
4783 ll = nasm_malloc(sizeof(Line));
4784 ll->finishes = NULL;
4785 ll->next = istk->expansion;
4786 istk->expansion = ll;
4787 ll->first = startline;
4788 if (!dont_prepend) {
4789 while (label->next)
4790 label = label->next;
4791 label->next = tt = new_Token(NULL, TOK_OTHER, ":", 0);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004792 }
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004793 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004794 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004795
H. Peter Anvin172b8402016-02-18 01:16:18 -08004796 lfmt->uplevel(m->nolist ? LIST_MACRO_NOLIST : LIST_MACRO);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004797
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004798 return 1;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004799}
4800
H. Peter Anvin215186f2016-02-17 20:27:41 -08004801/*
4802 * This function adds macro names to error messages, and suppresses
4803 * them if necessary.
4804 */
4805static void pp_verror(int severity, const char *fmt, va_list arg)
Victor van den Elzen3b404c02008-09-18 13:51:36 +02004806{
H. Peter Anvin215186f2016-02-17 20:27:41 -08004807 char buff[BUFSIZ];
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004808 MMacro *mmac = NULL;
4809 int delta = 0;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02004810
H. Peter Anvin215186f2016-02-17 20:27:41 -08004811 /*
4812 * If we're in a dead branch of IF or something like it, ignore the error.
4813 * However, because %else etc are evaluated in the state context
4814 * of the previous branch, errors might get lost:
4815 * %if 0 ... %else trailing garbage ... %endif
4816 * So %else etc should set the ERR_PP_PRECOND flag.
4817 */
4818 if ((severity & ERR_MASK) < ERR_FATAL &&
4819 istk && istk->conds &&
4820 ((severity & ERR_PP_PRECOND) ?
4821 istk->conds->state == COND_NEVER :
H. Peter Anvineb6653f2016-04-05 13:03:10 -07004822 !emitting(istk->conds->state)))
H. Peter Anvin215186f2016-02-17 20:27:41 -08004823 return;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02004824
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004825 /* get %macro name */
H. Peter Anvin215186f2016-02-17 20:27:41 -08004826 if (!(severity & ERR_NOFILE) && istk && istk->mstk) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004827 mmac = istk->mstk;
4828 /* but %rep blocks should be skipped */
4829 while (mmac && !mmac->name)
4830 mmac = mmac->next_active, delta++;
Cyrill Gorcunov9900c6b2011-10-09 18:58:46 +04004831 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004832
H. Peter Anvin215186f2016-02-17 20:27:41 -08004833 if (mmac) {
4834 vsnprintf(buff, sizeof(buff), fmt, arg);
Victor van den Elzen3b404c02008-09-18 13:51:36 +02004835
H. Peter Anvin215186f2016-02-17 20:27:41 -08004836 nasm_set_verror(real_verror);
4837 nasm_error(severity, "(%s:%d) %s",
4838 mmac->name, mmac->lineno - delta, buff);
4839 nasm_set_verror(pp_verror);
4840 } else {
4841 real_verror(severity, fmt, arg);
4842 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004843}
4844
H. Peter Anvin734b1882002-04-30 21:01:08 +00004845static void
H. Peter Anvin215186f2016-02-17 20:27:41 -08004846pp_reset(char *file, int apass, StrList **deplist)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004847{
H. Peter Anvin7383b402008-09-24 10:20:40 -07004848 Token *t;
4849
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004850 cstk = NULL;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004851 istk = nasm_malloc(sizeof(Include));
4852 istk->next = NULL;
4853 istk->conds = NULL;
4854 istk->expansion = NULL;
4855 istk->mstk = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004856 istk->fp = fopen(file, "r");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004857 istk->fname = NULL;
H. Peter Anvineba20a72002-04-30 20:53:55 +00004858 src_set_fname(nasm_strdup(file));
4859 src_set_linnum(0);
4860 istk->lineinc = 1;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004861 if (!istk->fp)
H. Peter Anvin215186f2016-02-17 20:27:41 -08004862 nasm_fatal(ERR_NOFILE, "unable to open input file `%s'", file);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004863 defining = NULL;
Charles Crayned4200be2008-07-12 16:42:33 -07004864 nested_mac_count = 0;
4865 nested_rep_count = 0;
H. Peter Anvin97a23472007-09-16 17:57:25 -07004866 init_macros();
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004867 unique = 0;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004868 if (tasm_compatible_mode) {
H. Peter Anvina4835d42008-05-20 14:21:29 -07004869 stdmacpos = nasm_stdmac;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004870 } else {
H. Peter Anvina4835d42008-05-20 14:21:29 -07004871 stdmacpos = nasm_stdmac_after_tasm;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004872 }
H. Peter Anvind2456592008-06-19 15:04:18 -07004873 any_extrastdmac = extrastdmac && *extrastdmac;
4874 do_predef = true;
H. Peter Anvin61f130f2008-09-25 15:45:06 -07004875
4876 /*
4877 * 0 for dependencies, 1 for preparatory passes, 2 for final pass.
4878 * The caller, however, will also pass in 3 for preprocess-only so
4879 * we can set __PASS__ accordingly.
4880 */
4881 pass = apass > 2 ? 2 : apass;
4882
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07004883 dephead = deptail = deplist;
4884 if (deplist) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004885 StrList *sl = nasm_malloc(strlen(file)+1+sizeof sl->next);
4886 sl->next = NULL;
4887 strcpy(sl->str, file);
4888 *deptail = sl;
4889 deptail = &sl->next;
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07004890 }
H. Peter Anvin7383b402008-09-24 10:20:40 -07004891
H. Peter Anvin61f130f2008-09-25 15:45:06 -07004892 /*
4893 * Define the __PASS__ macro. This is defined here unlike
4894 * all the other builtins, because it is special -- it varies between
4895 * passes.
4896 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004897 t = nasm_malloc(sizeof(*t));
4898 t->next = NULL;
H. Peter Anvin61f130f2008-09-25 15:45:06 -07004899 make_tok_num(t, apass);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004900 t->a.mac = NULL;
H. Peter Anvin7383b402008-09-24 10:20:40 -07004901 define_smacro(NULL, "__PASS__", true, 0, t);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004902}
4903
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004904static char *pp_getline(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004905{
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004906 char *line;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004907 Token *tline;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004908
H. Peter Anvin215186f2016-02-17 20:27:41 -08004909 real_verror = nasm_set_verror(pp_verror);
4910
H. Peter Anvine2c80182005-01-15 22:15:51 +00004911 while (1) {
4912 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004913 * Fetch a tokenized line, either from the macro-expansion
H. Peter Anvine2c80182005-01-15 22:15:51 +00004914 * buffer or from the input file.
4915 */
4916 tline = NULL;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004917 while (istk->expansion && istk->expansion->finishes) {
4918 Line *l = istk->expansion;
4919 if (!l->finishes->name && l->finishes->in_progress > 1) {
4920 Line *ll;
H. Peter Anvineba20a72002-04-30 20:53:55 +00004921
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004922 /*
4923 * This is a macro-end marker for a macro with no
4924 * name, which means it's not really a macro at all
4925 * but a %rep block, and the `in_progress' field is
4926 * more than 1, meaning that we still need to
4927 * repeat. (1 means the natural last repetition; 0
4928 * means termination by %exitrep.) We have
4929 * therefore expanded up to the %endrep, and must
4930 * push the whole block on to the expansion buffer
4931 * again. We don't bother to remove the macro-end
4932 * marker: we'd only have to generate another one
4933 * if we did.
4934 */
4935 l->finishes->in_progress--;
4936 list_for_each(l, l->finishes->expansion) {
4937 Token *t, *tt, **tail;
4938
4939 ll = nasm_malloc(sizeof(Line));
4940 ll->next = istk->expansion;
4941 ll->finishes = NULL;
4942 ll->first = NULL;
4943 tail = &ll->first;
4944
4945 list_for_each(t, l->first) {
4946 if (t->text || t->type == TOK_WHITESPACE) {
4947 tt = *tail = new_Token(NULL, t->type, t->text, 0);
4948 tail = &tt->next;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004949 }
4950 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004951
4952 istk->expansion = ll;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004953 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004954 } else {
4955 /*
4956 * Check whether a `%rep' was started and not ended
4957 * within this macro expansion. This can happen and
4958 * should be detected. It's a fatal error because
4959 * I'm too confused to work out how to recover
4960 * sensibly from it.
4961 */
4962 if (defining) {
4963 if (defining->name)
H. Peter Anvin215186f2016-02-17 20:27:41 -08004964 nasm_panic(0, "defining with name in expansion");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004965 else if (istk->mstk->name)
H. Peter Anvin215186f2016-02-17 20:27:41 -08004966 nasm_fatal(0, "`%%rep' without `%%endrep' within"
4967 " expansion of macro `%s'",
4968 istk->mstk->name);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004969 }
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004970
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004971 /*
4972 * FIXME: investigate the relationship at this point between
4973 * istk->mstk and l->finishes
4974 */
4975 {
4976 MMacro *m = istk->mstk;
4977 istk->mstk = m->next_active;
4978 if (m->name) {
4979 /*
4980 * This was a real macro call, not a %rep, and
4981 * therefore the parameter information needs to
4982 * be freed.
4983 */
4984 if (m->prev) {
4985 pop_mmacro(m);
4986 l->finishes->in_progress --;
4987 } else {
4988 nasm_free(m->params);
4989 free_tlist(m->iline);
4990 nasm_free(m->paramlen);
4991 l->finishes->in_progress = 0;
4992 }
4993 } else
4994 free_mmacro(m);
4995 }
4996 istk->expansion = l->next;
4997 nasm_free(l);
H. Peter Anvin172b8402016-02-18 01:16:18 -08004998 lfmt->downlevel(LIST_MACRO);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004999 }
5000 }
5001 while (1) { /* until we get a line we can use */
5002
5003 if (istk->expansion) { /* from a macro expansion */
5004 char *p;
5005 Line *l = istk->expansion;
5006 if (istk->mstk)
5007 istk->mstk->lineno++;
5008 tline = l->first;
5009 istk->expansion = l->next;
5010 nasm_free(l);
5011 p = detoken(tline, false);
H. Peter Anvin172b8402016-02-18 01:16:18 -08005012 lfmt->line(LIST_MACRO, p);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005013 nasm_free(p);
5014 break;
5015 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00005016 line = read_line();
5017 if (line) { /* from the current input file */
5018 line = prepreproc(line);
Keith Kaniosb7a89542007-04-12 02:40:54 +00005019 tline = tokenize(line);
H. Peter Anvine2c80182005-01-15 22:15:51 +00005020 nasm_free(line);
5021 break;
5022 }
5023 /*
5024 * The current file has ended; work down the istk
5025 */
5026 {
5027 Include *i = istk;
5028 fclose(i->fp);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005029 if (i->conds) {
5030 /* nasm_error can't be conditionally suppressed */
H. Peter Anvin41087062016-03-03 14:27:34 -08005031 nasm_fatal(0,
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005032 "expected `%%endif' before end of file");
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005033 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00005034 /* only set line and file name if there's a next node */
5035 if (i->next) {
5036 src_set_linnum(i->lineno);
H. Peter Anvin4def1a82016-05-09 13:59:44 -07005037 src_set_fname(nasm_strdup(i->fname));
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03005038 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00005039 istk = i->next;
H. Peter Anvin172b8402016-02-18 01:16:18 -08005040 lfmt->downlevel(LIST_INCLUDE);
H. Peter Anvine2c80182005-01-15 22:15:51 +00005041 nasm_free(i);
H. Peter Anvin215186f2016-02-17 20:27:41 -08005042 if (!istk) {
5043 line = NULL;
5044 goto done;
5045 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005046 if (istk->expansion && istk->expansion->finishes)
5047 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005048 }
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005049 }
5050
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005051 /*
5052 * We must expand MMacro parameters and MMacro-local labels
5053 * _before_ we plunge into directive processing, to cope
5054 * with things like `%define something %1' such as STRUC
5055 * uses. Unless we're _defining_ a MMacro, in which case
5056 * those tokens should be left alone to go into the
5057 * definition; and unless we're in a non-emitting
5058 * condition, in which case we don't want to meddle with
5059 * anything.
5060 */
5061 if (!defining && !(istk->conds && !emitting(istk->conds->state))
5062 && !(istk->mstk && !istk->mstk->in_progress)) {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005063 tline = expand_mmac_params(tline);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005064 }
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005065
H. Peter Anvine2c80182005-01-15 22:15:51 +00005066 /*
5067 * Check the line to see if it's a preprocessor directive.
5068 */
5069 if (do_directive(tline) == DIRECTIVE_FOUND) {
5070 continue;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005071 } else if (defining) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00005072 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005073 * We're defining a multi-line macro. We emit nothing
5074 * at all, and just
5075 * shove the tokenized line on to the macro definition.
H. Peter Anvine2c80182005-01-15 22:15:51 +00005076 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005077 Line *l = nasm_malloc(sizeof(Line));
5078 l->next = defining->expansion;
5079 l->first = tline;
5080 l->finishes = NULL;
5081 defining->expansion = l;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005082 continue;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005083 } else if (istk->conds && !emitting(istk->conds->state)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00005084 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005085 * We're in a non-emitting branch of a condition block.
H. Peter Anvine2c80182005-01-15 22:15:51 +00005086 * Emit nothing at all, not even a blank line: when we
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005087 * emerge from the condition we'll give a line-number
H. Peter Anvine2c80182005-01-15 22:15:51 +00005088 * directive so we keep our place correctly.
5089 */
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005090 free_tlist(tline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00005091 continue;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005092 } else if (istk->mstk && !istk->mstk->in_progress) {
5093 /*
5094 * We're in a %rep block which has been terminated, so
5095 * we're walking through to the %endrep without
5096 * emitting anything. Emit nothing at all, not even a
5097 * blank line: when we emerge from the %rep block we'll
5098 * give a line-number directive so we keep our place
5099 * correctly.
5100 */
5101 free_tlist(tline);
5102 continue;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005103 } else {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005104 tline = expand_smacro(tline);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005105 if (!expand_mmacro(tline)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00005106 /*
Keith Kaniosb7a89542007-04-12 02:40:54 +00005107 * De-tokenize the line again, and emit it.
H. Peter Anvine2c80182005-01-15 22:15:51 +00005108 */
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005109 line = detoken(tline, true);
5110 free_tlist(tline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00005111 break;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005112 } else {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005113 continue; /* expand_mmacro calls free_tlist */
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005114 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00005115 }
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005116 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005117
H. Peter Anvin215186f2016-02-17 20:27:41 -08005118done:
5119 nasm_set_verror(real_verror);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005120 return line;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005121}
5122
H. Peter Anvine2c80182005-01-15 22:15:51 +00005123static void pp_cleanup(int pass)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005124{
H. Peter Anvin215186f2016-02-17 20:27:41 -08005125 real_verror = nasm_set_verror(pp_verror);
5126
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005127 if (defining) {
5128 if (defining->name) {
H. Peter Anvin215186f2016-02-17 20:27:41 -08005129 nasm_error(ERR_NONFATAL,
5130 "end of file while still defining macro `%s'",
5131 defining->name);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005132 } else {
H. Peter Anvin215186f2016-02-17 20:27:41 -08005133 nasm_error(ERR_NONFATAL, "end of file while still in %%rep");
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005134 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005135
5136 free_mmacro(defining);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005137 defining = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005138 }
H. Peter Anvin215186f2016-02-17 20:27:41 -08005139
5140 nasm_set_verror(real_verror);
5141
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005142 while (cstk)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005143 ctx_pop();
H. Peter Anvin97a23472007-09-16 17:57:25 -07005144 free_macros();
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005145 while (istk) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00005146 Include *i = istk;
5147 istk = istk->next;
5148 fclose(i->fp);
5149 nasm_free(i->fname);
Cyrill Gorcunov8dcfd882011-03-03 09:18:56 +03005150 nasm_free(i);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005151 }
5152 while (cstk)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005153 ctx_pop();
H. Peter Anvin86877b22008-06-20 15:55:45 -07005154 nasm_free(src_set_fname(NULL));
H. Peter Anvine2c80182005-01-15 22:15:51 +00005155 if (pass == 0) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03005156 IncPath *i;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005157 free_llist(predef);
Cyrill Gorcunovdae24d72014-06-28 10:17:39 +04005158 predef = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005159 delete_Blocks();
Cyrill Gorcunovdae24d72014-06-28 10:17:39 +04005160 freeTokens = NULL;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03005161 while ((i = ipath)) {
5162 ipath = i->next;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005163 if (i->path)
5164 nasm_free(i->path);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03005165 nasm_free(i);
5166 }
H. Peter Anvin11dfa1a2008-07-02 18:11:04 -07005167 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005168}
5169
Cyrill Gorcunov0b78bff2012-05-07 01:57:55 +04005170static void pp_include_path(char *path)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005171{
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005172 IncPath *i;
H. Peter Anvin37a321f2007-09-24 13:41:58 -07005173
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005174 i = nasm_malloc(sizeof(IncPath));
5175 i->path = path ? nasm_strdup(path) : NULL;
5176 i->next = NULL;
Frank Kotlerd0ed6fd2003-08-27 11:33:56 +00005177
H. Peter Anvin89cee572009-07-15 09:16:54 -04005178 if (ipath) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00005179 IncPath *j = ipath;
H. Peter Anvin89cee572009-07-15 09:16:54 -04005180 while (j->next)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005181 j = j->next;
5182 j->next = i;
5183 } else {
5184 ipath = i;
Frank Kotlerd0ed6fd2003-08-27 11:33:56 +00005185 }
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005186}
Frank Kotlerd0ed6fd2003-08-27 11:33:56 +00005187
Cyrill Gorcunov0b78bff2012-05-07 01:57:55 +04005188static void pp_pre_include(char *fname)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005189{
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005190 Token *inc, *space, *name;
5191 Line *l;
5192
H. Peter Anvin734b1882002-04-30 21:01:08 +00005193 name = new_Token(NULL, TOK_INTERNAL_STRING, fname, 0);
5194 space = new_Token(name, TOK_WHITESPACE, NULL, 0);
5195 inc = new_Token(space, TOK_PREPROC_ID, "%include", 0);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005196
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005197 l = nasm_malloc(sizeof(Line));
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005198 l->next = predef;
5199 l->first = inc;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005200 l->finishes = NULL;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005201 predef = l;
5202}
5203
Cyrill Gorcunov0b78bff2012-05-07 01:57:55 +04005204static void pp_pre_define(char *definition)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005205{
5206 Token *def, *space;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005207 Line *l;
Keith Kaniosa6dfa782007-04-13 16:47:53 +00005208 char *equals;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005209
H. Peter Anvin215186f2016-02-17 20:27:41 -08005210 real_verror = nasm_set_verror(pp_verror);
5211
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005212 equals = strchr(definition, '=');
H. Peter Anvin734b1882002-04-30 21:01:08 +00005213 space = new_Token(NULL, TOK_WHITESPACE, NULL, 0);
5214 def = new_Token(space, TOK_PREPROC_ID, "%define", 0);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005215 if (equals)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005216 *equals = ' ';
Keith Kaniosb7a89542007-04-12 02:40:54 +00005217 space->next = tokenize(definition);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005218 if (equals)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005219 *equals = '=';
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005220
Cyrill Gorcunov6d42e9b2015-02-08 11:07:17 +03005221 if (space->next->type != TOK_PREPROC_ID &&
5222 space->next->type != TOK_ID)
H. Peter Anvin215186f2016-02-17 20:27:41 -08005223 nasm_error(ERR_WARNING, "pre-defining non ID `%s\'\n", definition);
Cyrill Gorcunov6d42e9b2015-02-08 11:07:17 +03005224
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005225 l = nasm_malloc(sizeof(Line));
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005226 l->next = predef;
5227 l->first = def;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005228 l->finishes = NULL;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005229 predef = l;
H. Peter Anvin215186f2016-02-17 20:27:41 -08005230
5231 nasm_set_verror(real_verror);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005232}
5233
Cyrill Gorcunov0b78bff2012-05-07 01:57:55 +04005234static void pp_pre_undefine(char *definition)
H. Peter Anvin620515a2002-04-30 20:57:38 +00005235{
5236 Token *def, *space;
5237 Line *l;
5238
H. Peter Anvin734b1882002-04-30 21:01:08 +00005239 space = new_Token(NULL, TOK_WHITESPACE, NULL, 0);
5240 def = new_Token(space, TOK_PREPROC_ID, "%undef", 0);
Keith Kaniosb7a89542007-04-12 02:40:54 +00005241 space->next = tokenize(definition);
H. Peter Anvin620515a2002-04-30 20:57:38 +00005242
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005243 l = nasm_malloc(sizeof(Line));
H. Peter Anvin620515a2002-04-30 20:57:38 +00005244 l->next = predef;
5245 l->first = def;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005246 l->finishes = NULL;
H. Peter Anvin620515a2002-04-30 20:57:38 +00005247 predef = l;
5248}
5249
Cyrill Gorcunov0b78bff2012-05-07 01:57:55 +04005250static void pp_extra_stdmac(macros_t *macros)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005251{
H. Peter Anvin76690a12002-04-30 20:52:49 +00005252 extrastdmac = macros;
5253}
5254
Keith Kaniosa5fc6462007-10-13 07:09:22 -07005255static void make_tok_num(Token * tok, int64_t val)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005256{
Cyrill Gorcunovce652742013-05-06 23:43:43 +04005257 char numbuf[32];
Keith Kaniosa5fc6462007-10-13 07:09:22 -07005258 snprintf(numbuf, sizeof(numbuf), "%"PRId64"", val);
H. Peter Anvineba20a72002-04-30 20:53:55 +00005259 tok->text = nasm_strdup(numbuf);
5260 tok->type = TOK_NUMBER;
5261}
5262
H. Peter Anvin37368952016-05-09 14:10:32 -07005263static void pp_list_one_macro(MMacro *m, int severity)
5264{
5265 if (!m)
5266 return;
5267
5268 /* We need to print the next_active list in reverse order */
5269 pp_list_one_macro(m->next_active, severity);
5270
5271 if (m->name && !m->nolist) {
5272 src_set_linnum(m->xline + m->lineno);
5273 src_set_fname(m->fname);
5274 nasm_error(severity, "... from macro `%s' defined here", m->name);
5275 }
5276}
5277
H. Peter Anvin4def1a82016-05-09 13:59:44 -07005278static void pp_error_list_macros(int severity)
5279{
H. Peter Anvin4def1a82016-05-09 13:59:44 -07005280 int32_t saved_line;
5281 const char *saved_fname = NULL;
5282
5283 severity |= ERR_PP_LISTMACRO | ERR_NO_SEVERITY;
5284 saved_line = src_get_linnum();
5285 saved_fname = src_get_fname();
5286
H. Peter Anvin37368952016-05-09 14:10:32 -07005287 pp_list_one_macro(istk->mstk, severity);
H. Peter Anvin4def1a82016-05-09 13:59:44 -07005288
5289 src_set_fname((char *)saved_fname);
5290 src_set_linnum(saved_line);
5291}
5292
5293const struct preproc_ops nasmpp = {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005294 pp_reset,
5295 pp_getline,
Cyrill Gorcunov0b78bff2012-05-07 01:57:55 +04005296 pp_cleanup,
5297 pp_extra_stdmac,
5298 pp_pre_define,
5299 pp_pre_undefine,
5300 pp_pre_include,
H. Peter Anvin4def1a82016-05-09 13:59:44 -07005301 pp_include_path,
5302 pp_error_list_macros,
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005303};