blob: e33a6d7d0c30f999393fd4448094af23242220b3 [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/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001563 * Determine if we should warn on defining a single-line macro of
H. Peter Anvinef7468f2002-04-30 20:57:59 +00001564 * name `name', with `nparam' parameters. If nparam is 0 or -1, will
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001565 * return true if _any_ single-line macro of that name is defined.
1566 * Otherwise, will return true if a single-line macro with either
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001567 * `nparam' or no parameters is defined.
1568 *
1569 * If a macro with precisely the right number of parameters is
H. Peter Anvinef7468f2002-04-30 20:57:59 +00001570 * defined, or nparam is -1, the address of the definition structure
1571 * will be returned in `defn'; otherwise NULL will be returned. If `defn'
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001572 * is NULL, no action will be taken regarding its contents, and no
1573 * error will occur.
1574 *
1575 * Note that this is also called with nparam zero to resolve
1576 * `ifdef'.
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001577 *
1578 * If you already know which context macro belongs to, you can pass
1579 * the context pointer as first parameter; if you won't but name begins
1580 * with %$ the context will be automatically computed. If all_contexts
1581 * is true, macro will be searched in outer contexts as well.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001582 */
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07001583static bool
H. Peter Anvinb2a5fda2008-06-19 21:42:42 -07001584smacro_defined(Context * ctx, const char *name, int nparam, SMacro ** defn,
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07001585 bool nocase)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001586{
H. Peter Anvin166c2472008-05-28 12:28:58 -07001587 struct hash_table *smtbl;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001588 SMacro *m;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001589
H. Peter Anvin97a23472007-09-16 17:57:25 -07001590 if (ctx) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001591 smtbl = &ctx->localmac;
H. Peter Anvin97a23472007-09-16 17:57:25 -07001592 } else if (name[0] == '%' && name[1] == '$') {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001593 if (cstk)
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04001594 ctx = get_ctx(name, &name);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001595 if (!ctx)
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001596 return false; /* got to return _something_ */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001597 smtbl = &ctx->localmac;
H. Peter Anvin97a23472007-09-16 17:57:25 -07001598 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001599 smtbl = &smacros;
H. Peter Anvin97a23472007-09-16 17:57:25 -07001600 }
H. Peter Anvin166c2472008-05-28 12:28:58 -07001601 m = (SMacro *) hash_findix(smtbl, name);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001602
H. Peter Anvine2c80182005-01-15 22:15:51 +00001603 while (m) {
1604 if (!mstrcmp(m->name, name, m->casesense && nocase) &&
Charles Crayne192d5b52007-10-18 19:02:42 -07001605 (nparam <= 0 || m->nparam == 0 || nparam == (int) m->nparam)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001606 if (defn) {
Charles Crayne192d5b52007-10-18 19:02:42 -07001607 if (nparam == (int) m->nparam || nparam == -1)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001608 *defn = m;
1609 else
1610 *defn = NULL;
1611 }
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001612 return true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001613 }
1614 m = m->next;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001615 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001616
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001617 return false;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001618}
1619
1620/*
1621 * Count and mark off the parameters in a multi-line macro call.
1622 * This is called both from within the multi-line macro expansion
1623 * code, and also to mark off the default parameters when provided
1624 * in a %macro definition line.
1625 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001626static void count_mmac_params(Token * t, int *nparam, Token *** params)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001627{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001628 int paramsize, brace;
1629
1630 *nparam = paramsize = 0;
1631 *params = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001632 while (t) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001633 /* +1: we need space for the final NULL */
H. Peter Anvin91fb6f12008-09-01 10:56:33 -07001634 if (*nparam+1 >= paramsize) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001635 paramsize += PARAM_DELTA;
1636 *params = nasm_realloc(*params, sizeof(**params) * paramsize);
1637 }
1638 skip_white_(t);
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08001639 brace = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001640 if (tok_is_(t, "{"))
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08001641 brace++;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001642 (*params)[(*nparam)++] = t;
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08001643 if (brace) {
1644 while (brace && (t = t->next) != NULL) {
1645 if (tok_is_(t, "{"))
1646 brace++;
1647 else if (tok_is_(t, "}"))
1648 brace--;
1649 }
1650
1651 if (t) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001652 /*
1653 * Now we've found the closing brace, look further
1654 * for the comma.
1655 */
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08001656 t = t->next;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001657 skip_white_(t);
1658 if (tok_isnt_(t, ",")) {
H. Peter Anvin215186f2016-02-17 20:27:41 -08001659 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00001660 "braces do not enclose all of macro parameter");
1661 while (tok_isnt_(t, ","))
1662 t = t->next;
1663 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001664 }
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08001665 } else {
1666 while (tok_isnt_(t, ","))
1667 t = t->next;
1668 }
1669 if (t) { /* got a comma/brace */
1670 t = t->next; /* eat the comma */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001671 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001672 }
1673}
1674
1675/*
H. Peter Anvin76690a12002-04-30 20:52:49 +00001676 * Determine whether one of the various `if' conditions is true or
1677 * not.
1678 *
1679 * We must free the tline we get passed.
1680 */
H. Peter Anvin70055962007-10-11 00:05:31 -07001681static bool if_condition(Token * tline, enum preproc_token ct)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001682{
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001683 enum pp_conditional i = PP_COND(ct);
H. Peter Anvin70055962007-10-11 00:05:31 -07001684 bool j;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001685 Token *t, *tt, **tptr, *origline;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001686 struct tokenval tokval;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001687 expr *evalresult;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001688 enum pp_token_type needtype;
H. Peter Anvin077fb932010-07-20 14:56:30 -07001689 char *p;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001690
1691 origline = tline;
1692
H. Peter Anvine2c80182005-01-15 22:15:51 +00001693 switch (i) {
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001694 case PPC_IFCTX:
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001695 j = false; /* have we matched yet? */
Victor van den Elzen0e857f12008-07-23 13:21:29 +02001696 while (true) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001697 skip_white_(tline);
Victor van den Elzen0e857f12008-07-23 13:21:29 +02001698 if (!tline)
1699 break;
1700 if (tline->type != TOK_ID) {
H. Peter Anvin215186f2016-02-17 20:27:41 -08001701 nasm_error(ERR_NONFATAL,
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001702 "`%s' expects context identifiers", pp_directives[ct]);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001703 free_tlist(origline);
1704 return -1;
1705 }
Victor van den Elzen0e857f12008-07-23 13:21:29 +02001706 if (cstk && cstk->name && !nasm_stricmp(tline->text, cstk->name))
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001707 j = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001708 tline = tline->next;
1709 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001710 break;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001711
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001712 case PPC_IFDEF:
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001713 j = false; /* have we matched yet? */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001714 while (tline) {
1715 skip_white_(tline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001716 if (!tline || (tline->type != TOK_ID &&
1717 (tline->type != TOK_PREPROC_ID ||
1718 tline->text[1] != '$'))) {
H. Peter Anvin215186f2016-02-17 20:27:41 -08001719 nasm_error(ERR_NONFATAL,
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001720 "`%s' expects macro identifiers", pp_directives[ct]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001721 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001722 }
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07001723 if (smacro_defined(NULL, tline->text, 0, NULL, true))
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001724 j = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001725 tline = tline->next;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001726 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001727 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001728
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001729 case PPC_IFENV:
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001730 tline = expand_smacro(tline);
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001731 j = false; /* have we matched yet? */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001732 while (tline) {
1733 skip_white_(tline);
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001734 if (!tline || (tline->type != TOK_ID &&
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001735 tline->type != TOK_STRING &&
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001736 (tline->type != TOK_PREPROC_ID ||
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001737 tline->text[1] != '!'))) {
H. Peter Anvin215186f2016-02-17 20:27:41 -08001738 nasm_error(ERR_NONFATAL,
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001739 "`%s' expects environment variable names",
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001740 pp_directives[ct]);
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001741 goto fail;
1742 }
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001743 p = tline->text;
1744 if (tline->type == TOK_PREPROC_ID)
1745 p += 2; /* Skip leading %! */
1746 if (*p == '\'' || *p == '\"' || *p == '`')
1747 nasm_unquote_cstr(p, ct);
1748 if (getenv(p))
1749 j = true;
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001750 tline = tline->next;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001751 }
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001752 break;
1753
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001754 case PPC_IFIDN:
1755 case PPC_IFIDNI:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001756 tline = expand_smacro(tline);
1757 t = tt = tline;
1758 while (tok_isnt_(tt, ","))
1759 tt = tt->next;
1760 if (!tt) {
H. Peter Anvin215186f2016-02-17 20:27:41 -08001761 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00001762 "`%s' expects two comma-separated arguments",
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001763 pp_directives[ct]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001764 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001765 }
1766 tt = tt->next;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001767 j = true; /* assume equality unless proved not */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001768 while ((t->type != TOK_OTHER || strcmp(t->text, ",")) && tt) {
1769 if (tt->type == TOK_OTHER && !strcmp(tt->text, ",")) {
H. Peter Anvin215186f2016-02-17 20:27:41 -08001770 nasm_error(ERR_NONFATAL, "`%s': more than one comma on line",
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001771 pp_directives[ct]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001772 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001773 }
1774 if (t->type == TOK_WHITESPACE) {
1775 t = t->next;
1776 continue;
1777 }
1778 if (tt->type == TOK_WHITESPACE) {
1779 tt = tt->next;
1780 continue;
1781 }
1782 if (tt->type != t->type) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001783 j = false; /* found mismatching tokens */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001784 break;
1785 }
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07001786 /* When comparing strings, need to unquote them first */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001787 if (t->type == TOK_STRING) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001788 size_t l1 = nasm_unquote(t->text, NULL);
1789 size_t l2 = nasm_unquote(tt->text, NULL);
H. Peter Anvind2456592008-06-19 15:04:18 -07001790
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001791 if (l1 != l2) {
1792 j = false;
1793 break;
1794 }
1795 if (mmemcmp(t->text, tt->text, l1, i == PPC_IFIDN)) {
1796 j = false;
1797 break;
1798 }
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07001799 } else if (mstrcmp(tt->text, t->text, i == PPC_IFIDN) != 0) {
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 }
Nickolay Yurchenkof3b3ce22003-09-21 20:38:43 +00001803
H. Peter Anvine2c80182005-01-15 22:15:51 +00001804 t = t->next;
1805 tt = tt->next;
1806 }
1807 if ((t->type != TOK_OTHER || strcmp(t->text, ",")) || tt)
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001808 j = false; /* trailing gunk on one end or other */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001809 break;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001810
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001811 case PPC_IFMACRO:
H. Peter Anvin89cee572009-07-15 09:16:54 -04001812 {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001813 bool found = false;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001814 MMacro searching, *mmac;
H. Peter Anvin65747262002-05-07 00:10:05 +00001815
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001816 skip_white_(tline);
1817 tline = expand_id(tline);
1818 if (!tok_type_(tline, TOK_ID)) {
H. Peter Anvin215186f2016-02-17 20:27:41 -08001819 nasm_error(ERR_NONFATAL,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001820 "`%s' expects a macro name", pp_directives[ct]);
1821 goto fail;
1822 }
1823 searching.name = nasm_strdup(tline->text);
1824 searching.casesense = true;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001825 searching.plus = false;
1826 searching.nolist = false;
1827 searching.in_progress = 0;
1828 searching.max_depth = 0;
1829 searching.rep_nest = NULL;
1830 searching.nparam_min = 0;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001831 searching.nparam_max = INT_MAX;
1832 tline = expand_smacro(tline->next);
1833 skip_white_(tline);
1834 if (!tline) {
1835 } else if (!tok_type_(tline, TOK_NUMBER)) {
H. Peter Anvin215186f2016-02-17 20:27:41 -08001836 nasm_error(ERR_NONFATAL,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001837 "`%s' expects a parameter count or nothing",
1838 pp_directives[ct]);
1839 } else {
1840 searching.nparam_min = searching.nparam_max =
1841 readnum(tline->text, &j);
1842 if (j)
H. Peter Anvin215186f2016-02-17 20:27:41 -08001843 nasm_error(ERR_NONFATAL,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001844 "unable to parse parameter count `%s'",
1845 tline->text);
1846 }
1847 if (tline && tok_is_(tline->next, "-")) {
1848 tline = tline->next->next;
1849 if (tok_is_(tline, "*"))
1850 searching.nparam_max = INT_MAX;
1851 else if (!tok_type_(tline, TOK_NUMBER))
H. Peter Anvin215186f2016-02-17 20:27:41 -08001852 nasm_error(ERR_NONFATAL,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001853 "`%s' expects a parameter count after `-'",
1854 pp_directives[ct]);
1855 else {
1856 searching.nparam_max = readnum(tline->text, &j);
1857 if (j)
H. Peter Anvin215186f2016-02-17 20:27:41 -08001858 nasm_error(ERR_NONFATAL,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001859 "unable to parse parameter count `%s'",
1860 tline->text);
1861 if (searching.nparam_min > searching.nparam_max)
H. Peter Anvin215186f2016-02-17 20:27:41 -08001862 nasm_error(ERR_NONFATAL,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001863 "minimum parameter count exceeds maximum");
1864 }
1865 }
1866 if (tline && tok_is_(tline->next, "+")) {
1867 tline = tline->next;
1868 searching.plus = true;
1869 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001870 mmac = (MMacro *) hash_findix(&mmacros, searching.name);
1871 while (mmac) {
1872 if (!strcmp(mmac->name, searching.name) &&
1873 (mmac->nparam_min <= searching.nparam_max
1874 || searching.plus)
1875 && (searching.nparam_min <= mmac->nparam_max
1876 || mmac->plus)) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001877 found = true;
1878 break;
1879 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001880 mmac = mmac->next;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001881 }
1882 if (tline && tline->next)
H. Peter Anvin215186f2016-02-17 20:27:41 -08001883 nasm_error(ERR_WARNING|ERR_PASS1,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001884 "trailing garbage after %%ifmacro ignored");
1885 nasm_free(searching.name);
1886 j = found;
1887 break;
H. Peter Anvin89cee572009-07-15 09:16:54 -04001888 }
H. Peter Anvin65747262002-05-07 00:10:05 +00001889
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001890 case PPC_IFID:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001891 needtype = TOK_ID;
1892 goto iftype;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001893 case PPC_IFNUM:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001894 needtype = TOK_NUMBER;
1895 goto iftype;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001896 case PPC_IFSTR:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001897 needtype = TOK_STRING;
1898 goto iftype;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001899
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001900iftype:
1901 t = tline = expand_smacro(tline);
H. Peter Anvind85d2502008-05-04 17:53:31 -07001902
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001903 while (tok_type_(t, TOK_WHITESPACE) ||
1904 (needtype == TOK_NUMBER &&
1905 tok_type_(t, TOK_OTHER) &&
1906 (t->text[0] == '-' || t->text[0] == '+') &&
1907 !t->text[1]))
1908 t = t->next;
H. Peter Anvind85d2502008-05-04 17:53:31 -07001909
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001910 j = tok_type_(t, needtype);
1911 break;
H. Peter Anvincbf768d2008-02-16 16:41:25 -08001912
1913 case PPC_IFTOKEN:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001914 t = tline = expand_smacro(tline);
1915 while (tok_type_(t, TOK_WHITESPACE))
1916 t = t->next;
H. Peter Anvincbf768d2008-02-16 16:41:25 -08001917
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001918 j = false;
1919 if (t) {
1920 t = t->next; /* Skip the actual token */
1921 while (tok_type_(t, TOK_WHITESPACE))
1922 t = t->next;
1923 j = !t; /* Should be nothing left */
1924 }
1925 break;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001926
H. Peter Anvin134b9462008-02-16 17:01:40 -08001927 case PPC_IFEMPTY:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001928 t = tline = expand_smacro(tline);
1929 while (tok_type_(t, TOK_WHITESPACE))
1930 t = t->next;
H. Peter Anvin134b9462008-02-16 17:01:40 -08001931
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001932 j = !t; /* Should be empty */
1933 break;
H. Peter Anvin134b9462008-02-16 17:01:40 -08001934
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001935 case PPC_IF:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001936 t = tline = expand_smacro(tline);
1937 tptr = &t;
1938 tokval.t_type = TOKEN_INVALID;
1939 evalresult = evaluate(ppscan, tptr, &tokval,
H. Peter Anvin215186f2016-02-17 20:27:41 -08001940 NULL, pass | CRITICAL, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001941 if (!evalresult)
1942 return -1;
1943 if (tokval.t_type)
H. Peter Anvin215186f2016-02-17 20:27:41 -08001944 nasm_error(ERR_WARNING|ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +00001945 "trailing garbage after expression ignored");
1946 if (!is_simple(evalresult)) {
H. Peter Anvin215186f2016-02-17 20:27:41 -08001947 nasm_error(ERR_NONFATAL,
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001948 "non-constant value given to `%s'", pp_directives[ct]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001949 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001950 }
Chuck Crayne60ae75d2007-05-02 01:59:16 +00001951 j = reloc_value(evalresult) != 0;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001952 break;
H. Peter Anvin95e28822007-09-12 04:20:08 +00001953
H. Peter Anvine2c80182005-01-15 22:15:51 +00001954 default:
H. Peter Anvin215186f2016-02-17 20:27:41 -08001955 nasm_error(ERR_FATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00001956 "preprocessor directive `%s' not yet implemented",
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001957 pp_directives[ct]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001958 goto fail;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001959 }
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001960
1961 free_tlist(origline);
1962 return j ^ PP_NEGATIVE(ct);
H. Peter Anvin70653092007-10-19 14:42:29 -07001963
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001964fail:
1965 free_tlist(origline);
1966 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001967}
1968
1969/*
H. Peter Anvin4db5a162007-10-11 13:42:09 -07001970 * Common code for defining an smacro
1971 */
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001972static bool define_smacro(Context *ctx, const char *mname, bool casesense,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001973 int nparam, Token *expansion)
H. Peter Anvin4db5a162007-10-11 13:42:09 -07001974{
1975 SMacro *smac, **smhead;
H. Peter Anvin166c2472008-05-28 12:28:58 -07001976 struct hash_table *smtbl;
H. Peter Anvin70653092007-10-19 14:42:29 -07001977
H. Peter Anvin4db5a162007-10-11 13:42:09 -07001978 if (smacro_defined(ctx, mname, nparam, &smac, casesense)) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001979 if (!smac) {
H. Peter Anvin215186f2016-02-17 20:27:41 -08001980 nasm_error(ERR_WARNING|ERR_PASS1,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001981 "single-line macro `%s' defined both with and"
1982 " without parameters", mname);
1983 /*
1984 * Some instances of the old code considered this a failure,
1985 * some others didn't. What is the right thing to do here?
1986 */
1987 free_tlist(expansion);
1988 return false; /* Failure */
1989 } else {
1990 /*
1991 * We're redefining, so we have to take over an
1992 * existing SMacro structure. This means freeing
1993 * what was already in it.
1994 */
1995 nasm_free(smac->name);
1996 free_tlist(smac->expansion);
1997 }
H. Peter Anvin4db5a162007-10-11 13:42:09 -07001998 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001999 smtbl = ctx ? &ctx->localmac : &smacros;
2000 smhead = (SMacro **) hash_findi_add(smtbl, mname);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002001 smac = nasm_malloc(sizeof(SMacro));
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002002 smac->next = *smhead;
2003 *smhead = smac;
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002004 }
2005 smac->name = nasm_strdup(mname);
2006 smac->casesense = casesense;
2007 smac->nparam = nparam;
2008 smac->expansion = expansion;
2009 smac->in_progress = false;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002010 return true; /* Success */
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002011}
2012
2013/*
2014 * Undefine an smacro
2015 */
2016static void undef_smacro(Context *ctx, const char *mname)
2017{
2018 SMacro **smhead, *s, **sp;
H. Peter Anvin166c2472008-05-28 12:28:58 -07002019 struct hash_table *smtbl;
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002020
H. Peter Anvin166c2472008-05-28 12:28:58 -07002021 smtbl = ctx ? &ctx->localmac : &smacros;
2022 smhead = (SMacro **)hash_findi(smtbl, mname, NULL);
H. Peter Anvin70653092007-10-19 14:42:29 -07002023
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002024 if (smhead) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002025 /*
2026 * We now have a macro name... go hunt for it.
2027 */
2028 sp = smhead;
2029 while ((s = *sp) != NULL) {
2030 if (!mstrcmp(s->name, mname, s->casesense)) {
2031 *sp = s->next;
2032 nasm_free(s->name);
2033 free_tlist(s->expansion);
2034 nasm_free(s);
2035 } else {
2036 sp = &s->next;
2037 }
2038 }
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002039 }
2040}
2041
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002042/*
H. Peter Anvina26433d2008-07-16 14:40:01 -07002043 * Parse a mmacro specification.
2044 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002045static bool parse_mmacro_spec(Token *tline, MMacro *def, const char *directive)
H. Peter Anvina26433d2008-07-16 14:40:01 -07002046{
2047 bool err;
2048
2049 tline = tline->next;
2050 skip_white_(tline);
2051 tline = expand_id(tline);
2052 if (!tok_type_(tline, TOK_ID)) {
H. Peter Anvin215186f2016-02-17 20:27:41 -08002053 nasm_error(ERR_NONFATAL, "`%s' expects a macro name", directive);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002054 return false;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002055 }
Victor van den Elzenb916ede2008-07-23 15:14:22 +02002056
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002057 def->prev = NULL;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002058 def->name = nasm_strdup(tline->text);
2059 def->plus = false;
2060 def->nolist = false;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002061 def->in_progress = 0;
2062 def->rep_nest = NULL;
Victor van den Elzenb916ede2008-07-23 15:14:22 +02002063 def->nparam_min = 0;
2064 def->nparam_max = 0;
2065
H. Peter Anvina26433d2008-07-16 14:40:01 -07002066 tline = expand_smacro(tline->next);
2067 skip_white_(tline);
2068 if (!tok_type_(tline, TOK_NUMBER)) {
H. Peter Anvin215186f2016-02-17 20:27:41 -08002069 nasm_error(ERR_NONFATAL, "`%s' expects a parameter count", directive);
H. Peter Anvina26433d2008-07-16 14:40:01 -07002070 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002071 def->nparam_min = def->nparam_max =
2072 readnum(tline->text, &err);
2073 if (err)
H. Peter Anvin215186f2016-02-17 20:27:41 -08002074 nasm_error(ERR_NONFATAL,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002075 "unable to parse parameter count `%s'", tline->text);
H. Peter Anvina26433d2008-07-16 14:40:01 -07002076 }
2077 if (tline && tok_is_(tline->next, "-")) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002078 tline = tline->next->next;
2079 if (tok_is_(tline, "*")) {
2080 def->nparam_max = INT_MAX;
2081 } else if (!tok_type_(tline, TOK_NUMBER)) {
H. Peter Anvin215186f2016-02-17 20:27:41 -08002082 nasm_error(ERR_NONFATAL,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002083 "`%s' expects a parameter count after `-'", directive);
2084 } else {
2085 def->nparam_max = readnum(tline->text, &err);
2086 if (err) {
H. Peter Anvin215186f2016-02-17 20:27:41 -08002087 nasm_error(ERR_NONFATAL, "unable to parse parameter count `%s'",
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002088 tline->text);
2089 }
2090 if (def->nparam_min > def->nparam_max) {
H. Peter Anvin215186f2016-02-17 20:27:41 -08002091 nasm_error(ERR_NONFATAL, "minimum parameter count exceeds maximum");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002092 }
2093 }
H. Peter Anvina26433d2008-07-16 14:40:01 -07002094 }
2095 if (tline && tok_is_(tline->next, "+")) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002096 tline = tline->next;
2097 def->plus = true;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002098 }
2099 if (tline && tok_type_(tline->next, TOK_ID) &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002100 !nasm_stricmp(tline->next->text, ".nolist")) {
2101 tline = tline->next;
2102 def->nolist = true;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002103 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002104
H. Peter Anvina26433d2008-07-16 14:40:01 -07002105 /*
2106 * Handle default parameters.
2107 */
2108 if (tline && tline->next) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002109 def->dlist = tline->next;
2110 tline->next = NULL;
2111 count_mmac_params(def->dlist, &def->ndefs, &def->defaults);
H. Peter Anvina26433d2008-07-16 14:40:01 -07002112 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002113 def->dlist = NULL;
2114 def->defaults = NULL;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002115 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002116 def->expansion = NULL;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002117
H. Peter Anvin89cee572009-07-15 09:16:54 -04002118 if (def->defaults && def->ndefs > def->nparam_max - def->nparam_min &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002119 !def->plus)
H. Peter Anvin215186f2016-02-17 20:27:41 -08002120 nasm_error(ERR_WARNING|ERR_PASS1|ERR_WARN_MDP,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002121 "too many default macro parameters");
Victor van den Elzenb916ede2008-07-23 15:14:22 +02002122
H. Peter Anvina26433d2008-07-16 14:40:01 -07002123 return true;
2124}
2125
2126
2127/*
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002128 * Decode a size directive
2129 */
2130static int parse_size(const char *str) {
2131 static const char *size_names[] =
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002132 { "byte", "dword", "oword", "qword", "tword", "word", "yword" };
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002133 static const int sizes[] =
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002134 { 0, 1, 4, 16, 8, 10, 2, 32 };
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002135
Cyrill Gorcunova7319242010-06-03 22:04:36 +04002136 return sizes[bsii(str, size_names, ARRAY_SIZE(size_names))+1];
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002137}
2138
Ed Beroset3ab3f412002-06-11 03:31:49 +00002139/**
2140 * find and process preprocessor directive in passed line
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002141 * Find out if a line contains a preprocessor directive, and deal
2142 * with it if so.
H. Peter Anvin70653092007-10-19 14:42:29 -07002143 *
Ed Beroset3ab3f412002-06-11 03:31:49 +00002144 * If a directive _is_ found, it is the responsibility of this routine
2145 * (and not the caller) to free_tlist() the line.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002146 *
Ed Beroset3ab3f412002-06-11 03:31:49 +00002147 * @param tline a pointer to the current tokeninzed line linked list
2148 * @return DIRECTIVE_FOUND or NO_DIRECTIVE_FOUND
H. Peter Anvin70653092007-10-19 14:42:29 -07002149 *
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002150 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002151static int do_directive(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00002152{
H. Peter Anvin4169a472007-09-12 01:29:43 +00002153 enum preproc_token i;
2154 int j;
H. Peter Anvin70055962007-10-11 00:05:31 -07002155 bool err;
2156 int nparam;
2157 bool nolist;
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07002158 bool casesense;
H. Peter Anvin8cfdb9d2007-09-14 18:36:01 -07002159 int k, m;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002160 int offset;
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08002161 char *p, *pp;
2162 const char *mname;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002163 Include *inc;
2164 Context *ctx;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002165 Cond *cond;
2166 MMacro *mmac, **mmhead;
Jin Kyu Songc9486b92013-10-28 17:07:57 -07002167 Token *t = NULL, *tt, *param_start, *macro_start, *last, **tptr, *origline;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002168 Line *l;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002169 struct tokenval tokval;
2170 expr *evalresult;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002171 MMacro *tmp_defining; /* Used when manipulating rep_nest */
H. Peter Anvinf8ba53e2007-10-11 10:11:57 -07002172 int64_t count;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07002173 size_t len;
H. Peter Anvin8e3f75e2008-09-24 00:21:58 -07002174 int severity;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002175
2176 origline = tline;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002177
H. Peter Anvineba20a72002-04-30 20:53:55 +00002178 skip_white_(tline);
H. Peter Anvinf2936d72008-06-04 15:11:23 -07002179 if (!tline || !tok_type_(tline, TOK_PREPROC_ID) ||
H. Peter Anvine2c80182005-01-15 22:15:51 +00002180 (tline->text[1] == '%' || tline->text[1] == '$'
2181 || tline->text[1] == '!'))
2182 return NO_DIRECTIVE_FOUND;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002183
H. Peter Anvin4169a472007-09-12 01:29:43 +00002184 i = pp_token_hash(tline->text);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002185
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002186 /*
2187 * FIXME: We zap execution of PP_RMACRO, PP_IRMACRO, PP_EXITMACRO
2188 * since they are known to be buggy at moment, we need to fix them
2189 * in future release (2.09-2.10)
2190 */
Philipp Klokeb432f572013-03-31 12:01:23 +02002191 if (i == PP_RMACRO || i == PP_IRMACRO || i == PP_EXITMACRO) {
H. Peter Anvin215186f2016-02-17 20:27:41 -08002192 nasm_error(ERR_NONFATAL, "unknown preprocessor directive `%s'",
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002193 tline->text);
2194 return NO_DIRECTIVE_FOUND;
2195 }
2196
2197 /*
2198 * If we're in a non-emitting branch of a condition construct,
2199 * or walking to the end of an already terminated %rep block,
2200 * we should ignore all directives except for condition
2201 * directives.
2202 */
2203 if (((istk->conds && !emitting(istk->conds->state)) ||
2204 (istk->mstk && !istk->mstk->in_progress)) && !is_condition(i)) {
2205 return NO_DIRECTIVE_FOUND;
2206 }
2207
2208 /*
2209 * If we're defining a macro or reading a %rep block, we should
2210 * ignore all directives except for %macro/%imacro (which nest),
2211 * %endm/%endmacro, and (only if we're in a %rep block) %endrep.
2212 * If we're in a %rep block, another %rep nests, so should be let through.
2213 */
2214 if (defining && i != PP_MACRO && i != PP_IMACRO &&
2215 i != PP_RMACRO && i != PP_IRMACRO &&
2216 i != PP_ENDMACRO && i != PP_ENDM &&
2217 (defining->name || (i != PP_ENDREP && i != PP_REP))) {
2218 return NO_DIRECTIVE_FOUND;
2219 }
2220
2221 if (defining) {
2222 if (i == PP_MACRO || i == PP_IMACRO ||
2223 i == PP_RMACRO || i == PP_IRMACRO) {
2224 nested_mac_count++;
2225 return NO_DIRECTIVE_FOUND;
2226 } else if (nested_mac_count > 0) {
2227 if (i == PP_ENDMACRO) {
2228 nested_mac_count--;
2229 return NO_DIRECTIVE_FOUND;
2230 }
2231 }
2232 if (!defining->name) {
2233 if (i == PP_REP) {
2234 nested_rep_count++;
2235 return NO_DIRECTIVE_FOUND;
2236 } else if (nested_rep_count > 0) {
2237 if (i == PP_ENDREP) {
2238 nested_rep_count--;
2239 return NO_DIRECTIVE_FOUND;
2240 }
2241 }
2242 }
2243 }
2244
H. Peter Anvin4169a472007-09-12 01:29:43 +00002245 switch (i) {
2246 case PP_INVALID:
H. Peter Anvin215186f2016-02-17 20:27:41 -08002247 nasm_error(ERR_NONFATAL, "unknown preprocessor directive `%s'",
H. Peter Anvine2c80182005-01-15 22:15:51 +00002248 tline->text);
2249 return NO_DIRECTIVE_FOUND; /* didn't get it */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002250
H. Peter Anvine2c80182005-01-15 22:15:51 +00002251 case PP_STACKSIZE:
2252 /* Directive to tell NASM what the default stack size is. The
2253 * default is for a 16-bit stack, and this can be overriden with
2254 * %stacksize large.
H. Peter Anvine2c80182005-01-15 22:15:51 +00002255 */
2256 tline = tline->next;
2257 if (tline && tline->type == TOK_WHITESPACE)
2258 tline = tline->next;
2259 if (!tline || tline->type != TOK_ID) {
H. Peter Anvin215186f2016-02-17 20:27:41 -08002260 nasm_error(ERR_NONFATAL, "`%%stacksize' missing size parameter");
H. Peter Anvine2c80182005-01-15 22:15:51 +00002261 free_tlist(origline);
2262 return DIRECTIVE_FOUND;
2263 }
2264 if (nasm_stricmp(tline->text, "flat") == 0) {
2265 /* All subsequent ARG directives are for a 32-bit stack */
2266 StackSize = 4;
2267 StackPointer = "ebp";
2268 ArgOffset = 8;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002269 LocalOffset = 0;
Charles Crayne7eaf9192007-11-08 22:11:14 -08002270 } else if (nasm_stricmp(tline->text, "flat64") == 0) {
2271 /* All subsequent ARG directives are for a 64-bit stack */
2272 StackSize = 8;
2273 StackPointer = "rbp";
Per Jessen53252e02010-02-11 00:16:59 +03002274 ArgOffset = 16;
Charles Crayne7eaf9192007-11-08 22:11:14 -08002275 LocalOffset = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002276 } else if (nasm_stricmp(tline->text, "large") == 0) {
2277 /* All subsequent ARG directives are for a 16-bit stack,
2278 * far function call.
2279 */
2280 StackSize = 2;
2281 StackPointer = "bp";
2282 ArgOffset = 4;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002283 LocalOffset = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002284 } else if (nasm_stricmp(tline->text, "small") == 0) {
2285 /* All subsequent ARG directives are for a 16-bit stack,
2286 * far function call. We don't support near functions.
2287 */
2288 StackSize = 2;
2289 StackPointer = "bp";
2290 ArgOffset = 6;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002291 LocalOffset = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002292 } else {
H. Peter Anvin215186f2016-02-17 20:27:41 -08002293 nasm_error(ERR_NONFATAL, "`%%stacksize' invalid size type");
H. Peter Anvine2c80182005-01-15 22:15:51 +00002294 free_tlist(origline);
2295 return DIRECTIVE_FOUND;
2296 }
2297 free_tlist(origline);
2298 return DIRECTIVE_FOUND;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002299
H. Peter Anvine2c80182005-01-15 22:15:51 +00002300 case PP_ARG:
2301 /* TASM like ARG directive to define arguments to functions, in
2302 * the following form:
2303 *
2304 * ARG arg1:WORD, arg2:DWORD, arg4:QWORD
2305 */
2306 offset = ArgOffset;
2307 do {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00002308 char *arg, directive[256];
H. Peter Anvine2c80182005-01-15 22:15:51 +00002309 int size = StackSize;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002310
H. Peter Anvine2c80182005-01-15 22:15:51 +00002311 /* Find the argument name */
2312 tline = tline->next;
2313 if (tline && tline->type == TOK_WHITESPACE)
2314 tline = tline->next;
2315 if (!tline || tline->type != TOK_ID) {
H. Peter Anvin215186f2016-02-17 20:27:41 -08002316 nasm_error(ERR_NONFATAL, "`%%arg' missing argument parameter");
H. Peter Anvine2c80182005-01-15 22:15:51 +00002317 free_tlist(origline);
2318 return DIRECTIVE_FOUND;
2319 }
2320 arg = tline->text;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002321
H. Peter Anvine2c80182005-01-15 22:15:51 +00002322 /* Find the argument size type */
2323 tline = tline->next;
2324 if (!tline || tline->type != TOK_OTHER
2325 || tline->text[0] != ':') {
H. Peter Anvin215186f2016-02-17 20:27:41 -08002326 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00002327 "Syntax error processing `%%arg' directive");
2328 free_tlist(origline);
2329 return DIRECTIVE_FOUND;
2330 }
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 size type parameter");
H. Peter Anvine2c80182005-01-15 22:15:51 +00002334 free_tlist(origline);
2335 return DIRECTIVE_FOUND;
2336 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002337
H. Peter Anvine2c80182005-01-15 22:15:51 +00002338 /* Allow macro expansion of type parameter */
Keith Kaniosb7a89542007-04-12 02:40:54 +00002339 tt = tokenize(tline->text);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002340 tt = expand_smacro(tt);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002341 size = parse_size(tt->text);
2342 if (!size) {
H. Peter Anvin215186f2016-02-17 20:27:41 -08002343 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00002344 "Invalid size type for `%%arg' missing directive");
2345 free_tlist(tt);
2346 free_tlist(origline);
2347 return DIRECTIVE_FOUND;
2348 }
2349 free_tlist(tt);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002350
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002351 /* Round up to even stack slots */
2352 size = ALIGN(size, StackSize);
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002353
H. Peter Anvine2c80182005-01-15 22:15:51 +00002354 /* Now define the macro for the argument */
2355 snprintf(directive, sizeof(directive), "%%define %s (%s+%d)",
2356 arg, StackPointer, offset);
Keith Kaniosb7a89542007-04-12 02:40:54 +00002357 do_directive(tokenize(directive));
H. Peter Anvine2c80182005-01-15 22:15:51 +00002358 offset += size;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002359
H. Peter Anvine2c80182005-01-15 22:15:51 +00002360 /* Move to the next argument in the list */
2361 tline = tline->next;
2362 if (tline && tline->type == TOK_WHITESPACE)
2363 tline = tline->next;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002364 } while (tline && tline->type == TOK_OTHER && tline->text[0] == ',');
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002365 ArgOffset = offset;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002366 free_tlist(origline);
2367 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002368
H. Peter Anvine2c80182005-01-15 22:15:51 +00002369 case PP_LOCAL:
2370 /* TASM like LOCAL directive to define local variables for a
2371 * function, in the following form:
2372 *
2373 * LOCAL local1:WORD, local2:DWORD, local4:QWORD = LocalSize
2374 *
2375 * The '= LocalSize' at the end is ignored by NASM, but is
2376 * required by TASM to define the local parameter size (and used
2377 * by the TASM macro package).
2378 */
2379 offset = LocalOffset;
2380 do {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00002381 char *local, directive[256];
H. Peter Anvine2c80182005-01-15 22:15:51 +00002382 int size = StackSize;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002383
H. Peter Anvine2c80182005-01-15 22:15:51 +00002384 /* Find the argument name */
2385 tline = tline->next;
2386 if (tline && tline->type == TOK_WHITESPACE)
2387 tline = tline->next;
2388 if (!tline || tline->type != TOK_ID) {
H. Peter Anvin215186f2016-02-17 20:27:41 -08002389 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00002390 "`%%local' missing argument parameter");
2391 free_tlist(origline);
2392 return DIRECTIVE_FOUND;
2393 }
2394 local = tline->text;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002395
H. Peter Anvine2c80182005-01-15 22:15:51 +00002396 /* Find the argument size type */
2397 tline = tline->next;
2398 if (!tline || tline->type != TOK_OTHER
2399 || tline->text[0] != ':') {
H. Peter Anvin215186f2016-02-17 20:27:41 -08002400 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00002401 "Syntax error processing `%%local' directive");
2402 free_tlist(origline);
2403 return DIRECTIVE_FOUND;
2404 }
2405 tline = tline->next;
2406 if (!tline || tline->type != TOK_ID) {
H. Peter Anvin215186f2016-02-17 20:27:41 -08002407 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00002408 "`%%local' missing size type parameter");
2409 free_tlist(origline);
2410 return DIRECTIVE_FOUND;
2411 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00002412
H. Peter Anvine2c80182005-01-15 22:15:51 +00002413 /* Allow macro expansion of type parameter */
Keith Kaniosb7a89542007-04-12 02:40:54 +00002414 tt = tokenize(tline->text);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002415 tt = expand_smacro(tt);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002416 size = parse_size(tt->text);
2417 if (!size) {
H. Peter Anvin215186f2016-02-17 20:27:41 -08002418 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00002419 "Invalid size type for `%%local' missing directive");
2420 free_tlist(tt);
2421 free_tlist(origline);
2422 return DIRECTIVE_FOUND;
2423 }
2424 free_tlist(tt);
H. Peter Anvin734b1882002-04-30 21:01:08 +00002425
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002426 /* Round up to even stack slots */
2427 size = ALIGN(size, StackSize);
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002428
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002429 offset += size; /* Negative offset, increment before */
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002430
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002431 /* Now define the macro for the argument */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002432 snprintf(directive, sizeof(directive), "%%define %s (%s-%d)",
2433 local, StackPointer, offset);
Keith Kaniosb7a89542007-04-12 02:40:54 +00002434 do_directive(tokenize(directive));
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002435
H. Peter Anvine2c80182005-01-15 22:15:51 +00002436 /* Now define the assign to setup the enter_c macro correctly */
2437 snprintf(directive, sizeof(directive),
2438 "%%assign %%$localsize %%$localsize+%d", size);
Keith Kaniosb7a89542007-04-12 02:40:54 +00002439 do_directive(tokenize(directive));
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002440
H. Peter Anvine2c80182005-01-15 22:15:51 +00002441 /* Move to the next argument in the list */
2442 tline = tline->next;
2443 if (tline && tline->type == TOK_WHITESPACE)
2444 tline = tline->next;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002445 } while (tline && tline->type == TOK_OTHER && tline->text[0] == ',');
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002446 LocalOffset = offset;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002447 free_tlist(origline);
2448 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002449
H. Peter Anvine2c80182005-01-15 22:15:51 +00002450 case PP_CLEAR:
2451 if (tline->next)
H. Peter Anvin215186f2016-02-17 20:27:41 -08002452 nasm_error(ERR_WARNING|ERR_PASS1,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002453 "trailing garbage after `%%clear' ignored");
2454 free_macros();
2455 init_macros();
H. Peter Anvine2c80182005-01-15 22:15:51 +00002456 free_tlist(origline);
2457 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002458
H. Peter Anvin418ca702008-05-30 10:42:30 -07002459 case PP_DEPEND:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002460 t = tline->next = expand_smacro(tline->next);
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002461 skip_white_(t);
2462 if (!t || (t->type != TOK_STRING &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002463 t->type != TOK_INTERNAL_STRING)) {
H. Peter Anvin215186f2016-02-17 20:27:41 -08002464 nasm_error(ERR_NONFATAL, "`%%depend' expects a file name");
H. Peter Anvin418ca702008-05-30 10:42:30 -07002465 free_tlist(origline);
2466 return DIRECTIVE_FOUND; /* but we did _something_ */
2467 }
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002468 if (t->next)
H. Peter Anvin215186f2016-02-17 20:27:41 -08002469 nasm_error(ERR_WARNING|ERR_PASS1,
H. Peter Anvin418ca702008-05-30 10:42:30 -07002470 "trailing garbage after `%%depend' ignored");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002471 p = t->text;
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002472 if (t->type != TOK_INTERNAL_STRING)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002473 nasm_unquote_cstr(p, i);
2474 if (dephead && !in_list(*dephead, p)) {
2475 StrList *sl = nasm_malloc(strlen(p)+1+sizeof sl->next);
2476 sl->next = NULL;
2477 strcpy(sl->str, p);
2478 *deptail = sl;
2479 deptail = &sl->next;
2480 }
2481 free_tlist(origline);
H. Peter Anvin418ca702008-05-30 10:42:30 -07002482 return DIRECTIVE_FOUND;
2483
2484 case PP_INCLUDE:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002485 t = tline->next = expand_smacro(tline->next);
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002486 skip_white_(t);
H. Peter Anvind2456592008-06-19 15:04:18 -07002487
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002488 if (!t || (t->type != TOK_STRING &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002489 t->type != TOK_INTERNAL_STRING)) {
H. Peter Anvin215186f2016-02-17 20:27:41 -08002490 nasm_error(ERR_NONFATAL, "`%%include' expects a file name");
H. Peter Anvine2c80182005-01-15 22:15:51 +00002491 free_tlist(origline);
2492 return DIRECTIVE_FOUND; /* but we did _something_ */
2493 }
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002494 if (t->next)
H. Peter Anvin215186f2016-02-17 20:27:41 -08002495 nasm_error(ERR_WARNING|ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +00002496 "trailing garbage after `%%include' ignored");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002497 p = t->text;
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002498 if (t->type != TOK_INTERNAL_STRING)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002499 nasm_unquote_cstr(p, i);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002500 inc = nasm_malloc(sizeof(Include));
H. Peter Anvine2c80182005-01-15 22:15:51 +00002501 inc->next = istk;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002502 inc->conds = NULL;
H. Peter Anvin2b1c3b92008-06-06 10:38:46 -07002503 inc->fp = inc_fopen(p, dephead, &deptail, pass == 0);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002504 if (!inc->fp) {
2505 /* -MG given but file not found */
2506 nasm_free(inc);
2507 } else {
2508 inc->fname = src_set_fname(nasm_strdup(p));
2509 inc->lineno = src_set_linnum(0);
2510 inc->lineinc = 1;
2511 inc->expansion = NULL;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002512 inc->mstk = NULL;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002513 istk = inc;
H. Peter Anvin172b8402016-02-18 01:16:18 -08002514 lfmt->uplevel(LIST_INCLUDE);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002515 }
2516 free_tlist(origline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002517 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002518
H. Peter Anvind2456592008-06-19 15:04:18 -07002519 case PP_USE:
H. Peter Anvinf4ae5ad2008-06-19 18:39:24 -07002520 {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002521 static macros_t *use_pkg;
2522 const char *pkg_macro = NULL;
H. Peter Anvinf4ae5ad2008-06-19 18:39:24 -07002523
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002524 tline = tline->next;
2525 skip_white_(tline);
2526 tline = expand_id(tline);
H. Peter Anvind2456592008-06-19 15:04:18 -07002527
H. Peter Anvin264b7b92008-10-24 16:38:17 -07002528 if (!tline || (tline->type != TOK_STRING &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002529 tline->type != TOK_INTERNAL_STRING &&
2530 tline->type != TOK_ID)) {
H. Peter Anvin215186f2016-02-17 20:27:41 -08002531 nasm_error(ERR_NONFATAL, "`%%use' expects a package name");
H. Peter Anvind2456592008-06-19 15:04:18 -07002532 free_tlist(origline);
2533 return DIRECTIVE_FOUND; /* but we did _something_ */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002534 }
H. Peter Anvin264b7b92008-10-24 16:38:17 -07002535 if (tline->next)
H. Peter Anvin215186f2016-02-17 20:27:41 -08002536 nasm_error(ERR_WARNING|ERR_PASS1,
H. Peter Anvind2456592008-06-19 15:04:18 -07002537 "trailing garbage after `%%use' ignored");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002538 if (tline->type == TOK_STRING)
2539 nasm_unquote_cstr(tline->text, i);
2540 use_pkg = nasm_stdmac_find_package(tline->text);
2541 if (!use_pkg)
H. Peter Anvin215186f2016-02-17 20:27:41 -08002542 nasm_error(ERR_NONFATAL, "unknown `%%use' package: %s", tline->text);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002543 else
2544 pkg_macro = (char *)use_pkg + 1; /* The first string will be <%define>__USE_*__ */
Victor van den Elzen35eb2ea2010-03-10 22:33:48 +01002545 if (use_pkg && ! smacro_defined(NULL, pkg_macro, 0, NULL, true)) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002546 /* Not already included, go ahead and include it */
2547 stdmacpos = use_pkg;
2548 }
2549 free_tlist(origline);
H. Peter Anvind2456592008-06-19 15:04:18 -07002550 return DIRECTIVE_FOUND;
H. Peter Anvinf4ae5ad2008-06-19 18:39:24 -07002551 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002552 case PP_PUSH:
H. Peter Anvine2c80182005-01-15 22:15:51 +00002553 case PP_REPL:
H. Peter Anvin42b56392008-10-24 16:24:21 -07002554 case PP_POP:
H. Peter Anvine2c80182005-01-15 22:15:51 +00002555 tline = tline->next;
2556 skip_white_(tline);
2557 tline = expand_id(tline);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002558 if (tline) {
2559 if (!tok_type_(tline, TOK_ID)) {
H. Peter Anvin215186f2016-02-17 20:27:41 -08002560 nasm_error(ERR_NONFATAL, "`%s' expects a context identifier",
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002561 pp_directives[i]);
2562 free_tlist(origline);
2563 return DIRECTIVE_FOUND; /* but we did _something_ */
2564 }
2565 if (tline->next)
H. Peter Anvin215186f2016-02-17 20:27:41 -08002566 nasm_error(ERR_WARNING|ERR_PASS1,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002567 "trailing garbage after `%s' ignored",
2568 pp_directives[i]);
2569 p = nasm_strdup(tline->text);
2570 } else {
2571 p = NULL; /* Anonymous */
2572 }
H. Peter Anvin42b56392008-10-24 16:24:21 -07002573
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002574 if (i == PP_PUSH) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002575 ctx = nasm_malloc(sizeof(Context));
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002576 ctx->next = cstk;
2577 hash_init(&ctx->localmac, HASH_SMALL);
2578 ctx->name = p;
2579 ctx->number = unique++;
2580 cstk = ctx;
2581 } else {
2582 /* %pop or %repl */
2583 if (!cstk) {
H. Peter Anvin215186f2016-02-17 20:27:41 -08002584 nasm_error(ERR_NONFATAL, "`%s': context stack is empty",
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002585 pp_directives[i]);
2586 } else if (i == PP_POP) {
2587 if (p && (!cstk->name || nasm_stricmp(p, cstk->name)))
H. Peter Anvin215186f2016-02-17 20:27:41 -08002588 nasm_error(ERR_NONFATAL, "`%%pop' in wrong context: %s, "
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002589 "expected %s",
2590 cstk->name ? cstk->name : "anonymous", p);
2591 else
2592 ctx_pop();
2593 } else {
2594 /* i == PP_REPL */
2595 nasm_free(cstk->name);
2596 cstk->name = p;
2597 p = NULL;
2598 }
2599 nasm_free(p);
2600 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002601 free_tlist(origline);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002602 return DIRECTIVE_FOUND;
H. Peter Anvin8e3f75e2008-09-24 00:21:58 -07002603 case PP_FATAL:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002604 severity = ERR_FATAL;
2605 goto issue_error;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002606 case PP_ERROR:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002607 severity = ERR_NONFATAL;
2608 goto issue_error;
H. Peter Anvin7df04172008-06-10 18:27:38 -07002609 case PP_WARNING:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002610 severity = ERR_WARNING|ERR_WARN_USER;
2611 goto issue_error;
H. Peter Anvin8e3f75e2008-09-24 00:21:58 -07002612
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002613issue_error:
H. Peter Anvin7df04172008-06-10 18:27:38 -07002614 {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002615 /* Only error out if this is the final pass */
2616 if (pass != 2 && i != PP_FATAL)
2617 return DIRECTIVE_FOUND;
2618
2619 tline->next = expand_smacro(tline->next);
2620 tline = tline->next;
2621 skip_white_(tline);
2622 t = tline ? tline->next : NULL;
2623 skip_white_(t);
2624 if (tok_type_(tline, TOK_STRING) && !t) {
2625 /* The line contains only a quoted string */
2626 p = tline->text;
2627 nasm_unquote(p, NULL); /* Ignore NUL character truncation */
H. Peter Anvin215186f2016-02-17 20:27:41 -08002628 nasm_error(severity, "%s", p);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002629 } else {
2630 /* Not a quoted string, or more than a quoted string */
2631 p = detoken(tline, false);
H. Peter Anvin215186f2016-02-17 20:27:41 -08002632 nasm_error(severity, "%s", p);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002633 nasm_free(p);
2634 }
2635 free_tlist(origline);
2636 return DIRECTIVE_FOUND;
H. Peter Anvin7df04172008-06-10 18:27:38 -07002637 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00002638
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002639 CASE_PP_IF:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002640 if (istk->conds && !emitting(istk->conds->state))
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002641 j = COND_NEVER;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002642 else {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002643 j = if_condition(tline->next, i);
2644 tline->next = NULL; /* it got freed */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002645 j = j < 0 ? COND_NEVER : j ? COND_IF_TRUE : COND_IF_FALSE;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002646 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002647 cond = nasm_malloc(sizeof(Cond));
2648 cond->next = istk->conds;
2649 cond->state = j;
2650 istk->conds = cond;
2651 if(istk->mstk)
2652 istk->mstk->condcnt ++;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002653 free_tlist(origline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002654 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002655
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002656 CASE_PP_ELIF:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002657 if (!istk->conds)
H. Peter Anvin215186f2016-02-17 20:27:41 -08002658 nasm_error(ERR_FATAL, "`%s': no matching `%%if'", pp_directives[i]);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002659 switch(istk->conds->state) {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002660 case COND_IF_TRUE:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002661 istk->conds->state = COND_DONE;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002662 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002663
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002664 case COND_DONE:
2665 case COND_NEVER:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002666 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002667
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002668 case COND_ELSE_TRUE:
2669 case COND_ELSE_FALSE:
H. Peter Anvin215186f2016-02-17 20:27:41 -08002670 nasm_error(ERR_WARNING|ERR_PASS1|ERR_PP_PRECOND,
2671 "`%%elif' after `%%else' ignored");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002672 istk->conds->state = COND_NEVER;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002673 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002674
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002675 case COND_IF_FALSE:
2676 /*
2677 * IMPORTANT: In the case of %if, we will already have
2678 * called expand_mmac_params(); however, if we're
2679 * processing an %elif we must have been in a
2680 * non-emitting mode, which would have inhibited
2681 * the normal invocation of expand_mmac_params().
2682 * Therefore, we have to do it explicitly here.
2683 */
2684 j = if_condition(expand_mmac_params(tline->next), i);
2685 tline->next = NULL; /* it got freed */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002686 istk->conds->state =
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002687 j < 0 ? COND_NEVER : j ? COND_IF_TRUE : COND_IF_FALSE;
2688 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002689 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002690 free_tlist(origline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002691 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002692
H. Peter Anvine2c80182005-01-15 22:15:51 +00002693 case PP_ELSE:
2694 if (tline->next)
H. Peter Anvin215186f2016-02-17 20:27:41 -08002695 nasm_error(ERR_WARNING|ERR_PASS1|ERR_PP_PRECOND,
2696 "trailing garbage after `%%else' ignored");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002697 if (!istk->conds)
H. Peter Anvin215186f2016-02-17 20:27:41 -08002698 nasm_fatal(0, "`%%else: no matching `%%if'");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002699 switch(istk->conds->state) {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002700 case COND_IF_TRUE:
2701 case COND_DONE:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002702 istk->conds->state = COND_ELSE_FALSE;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002703 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002704
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002705 case COND_NEVER:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002706 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002707
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002708 case COND_IF_FALSE:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002709 istk->conds->state = COND_ELSE_TRUE;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002710 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002711
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002712 case COND_ELSE_TRUE:
2713 case COND_ELSE_FALSE:
H. Peter Anvin215186f2016-02-17 20:27:41 -08002714 nasm_error(ERR_WARNING|ERR_PASS1|ERR_PP_PRECOND,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002715 "`%%else' after `%%else' ignored.");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002716 istk->conds->state = COND_NEVER;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002717 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002718 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002719 free_tlist(origline);
2720 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002721
H. Peter Anvine2c80182005-01-15 22:15:51 +00002722 case PP_ENDIF:
2723 if (tline->next)
H. Peter Anvin215186f2016-02-17 20:27:41 -08002724 nasm_error(ERR_WARNING|ERR_PASS1|ERR_PP_PRECOND,
2725 "trailing garbage after `%%endif' ignored");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002726 if (!istk->conds)
H. Peter Anvin215186f2016-02-17 20:27:41 -08002727 nasm_error(ERR_FATAL, "`%%endif': no matching `%%if'");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002728 cond = istk->conds;
2729 istk->conds = cond->next;
2730 nasm_free(cond);
2731 if(istk->mstk)
2732 istk->mstk->condcnt --;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002733 free_tlist(origline);
2734 return DIRECTIVE_FOUND;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002735
H. Peter Anvindb8f96e2009-07-15 09:07:29 -04002736 case PP_RMACRO:
2737 case PP_IRMACRO:
H. Peter Anvine2c80182005-01-15 22:15:51 +00002738 case PP_MACRO:
2739 case PP_IMACRO:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002740 if (defining) {
H. Peter Anvin215186f2016-02-17 20:27:41 -08002741 nasm_error(ERR_FATAL, "`%s': already defining a macro",
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002742 pp_directives[i]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002743 return DIRECTIVE_FOUND;
2744 }
H. Peter Anvin4def1a82016-05-09 13:59:44 -07002745 defining = nasm_zalloc(sizeof(MMacro));
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002746 defining->max_depth =
2747 (i == PP_RMACRO) || (i == PP_IRMACRO) ? DEADMAN_LIMIT : 0;
2748 defining->casesense = (i == PP_MACRO) || (i == PP_RMACRO);
2749 if (!parse_mmacro_spec(tline, defining, pp_directives[i])) {
2750 nasm_free(defining);
2751 defining = NULL;
2752 return DIRECTIVE_FOUND;
2753 }
H. Peter Anvina26433d2008-07-16 14:40:01 -07002754
H. Peter Anvin4def1a82016-05-09 13:59:44 -07002755 src_get(&defining->xline, &defining->fname);
2756
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002757 mmac = (MMacro *) hash_findix(&mmacros, defining->name);
2758 while (mmac) {
2759 if (!strcmp(mmac->name, defining->name) &&
2760 (mmac->nparam_min <= defining->nparam_max
2761 || defining->plus)
2762 && (defining->nparam_min <= mmac->nparam_max
2763 || mmac->plus)) {
H. Peter Anvin215186f2016-02-17 20:27:41 -08002764 nasm_error(ERR_WARNING|ERR_PASS1,
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002765 "redefining multi-line macro `%s'", defining->name);
2766 return DIRECTIVE_FOUND;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002767 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002768 mmac = mmac->next;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002769 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002770 free_tlist(origline);
2771 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002772
H. Peter Anvine2c80182005-01-15 22:15:51 +00002773 case PP_ENDM:
2774 case PP_ENDMACRO:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002775 if (! (defining && defining->name)) {
H. Peter Anvin215186f2016-02-17 20:27:41 -08002776 nasm_error(ERR_NONFATAL, "`%s': not defining a macro", tline->text);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002777 return DIRECTIVE_FOUND;
2778 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002779 mmhead = (MMacro **) hash_findi_add(&mmacros, defining->name);
2780 defining->next = *mmhead;
2781 *mmhead = defining;
2782 defining = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002783 free_tlist(origline);
2784 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002785
H. Peter Anvin89cee572009-07-15 09:16:54 -04002786 case PP_EXITMACRO:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002787 /*
2788 * We must search along istk->expansion until we hit a
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002789 * macro-end marker for a macro with a name. Then we
2790 * bypass all lines between exitmacro and endmacro.
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002791 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002792 list_for_each(l, istk->expansion)
2793 if (l->finishes && l->finishes->name)
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002794 break;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002795
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002796 if (l) {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002797 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002798 * Remove all conditional entries relative to this
2799 * macro invocation. (safe to do in this context)
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002800 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002801 for ( ; l->finishes->condcnt > 0; l->finishes->condcnt --) {
2802 cond = istk->conds;
2803 istk->conds = cond->next;
2804 nasm_free(cond);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002805 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002806 istk->expansion = l;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002807 } else {
H. Peter Anvin215186f2016-02-17 20:27:41 -08002808 nasm_error(ERR_NONFATAL, "`%%exitmacro' not within `%%macro' block");
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002809 }
Keith Kanios852f1ee2009-07-12 00:19:55 -05002810 free_tlist(origline);
2811 return DIRECTIVE_FOUND;
2812
H. Peter Anvina26433d2008-07-16 14:40:01 -07002813 case PP_UNMACRO:
2814 case PP_UNIMACRO:
2815 {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002816 MMacro **mmac_p;
2817 MMacro spec;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002818
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002819 spec.casesense = (i == PP_UNMACRO);
2820 if (!parse_mmacro_spec(tline, &spec, pp_directives[i])) {
2821 return DIRECTIVE_FOUND;
2822 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002823 mmac_p = (MMacro **) hash_findi(&mmacros, spec.name, NULL);
2824 while (mmac_p && *mmac_p) {
2825 mmac = *mmac_p;
2826 if (mmac->casesense == spec.casesense &&
2827 !mstrcmp(mmac->name, spec.name, spec.casesense) &&
2828 mmac->nparam_min == spec.nparam_min &&
2829 mmac->nparam_max == spec.nparam_max &&
2830 mmac->plus == spec.plus) {
2831 *mmac_p = mmac->next;
2832 free_mmacro(mmac);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002833 } else {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002834 mmac_p = &mmac->next;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002835 }
2836 }
2837 free_tlist(origline);
2838 free_tlist(spec.dlist);
2839 return DIRECTIVE_FOUND;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002840 }
2841
H. Peter Anvine2c80182005-01-15 22:15:51 +00002842 case PP_ROTATE:
2843 if (tline->next && tline->next->type == TOK_WHITESPACE)
2844 tline = tline->next;
H. Peter Anvin89cee572009-07-15 09:16:54 -04002845 if (!tline->next) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002846 free_tlist(origline);
H. Peter Anvin215186f2016-02-17 20:27:41 -08002847 nasm_error(ERR_NONFATAL, "`%%rotate' missing rotate count");
H. Peter Anvine2c80182005-01-15 22:15:51 +00002848 return DIRECTIVE_FOUND;
2849 }
2850 t = expand_smacro(tline->next);
2851 tline->next = NULL;
2852 free_tlist(origline);
2853 tline = t;
2854 tptr = &t;
2855 tokval.t_type = TOKEN_INVALID;
2856 evalresult =
H. Peter Anvin215186f2016-02-17 20:27:41 -08002857 evaluate(ppscan, tptr, &tokval, NULL, pass, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002858 free_tlist(tline);
2859 if (!evalresult)
2860 return DIRECTIVE_FOUND;
2861 if (tokval.t_type)
H. Peter Anvin215186f2016-02-17 20:27:41 -08002862 nasm_error(ERR_WARNING|ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +00002863 "trailing garbage after expression ignored");
2864 if (!is_simple(evalresult)) {
H. Peter Anvin215186f2016-02-17 20:27:41 -08002865 nasm_error(ERR_NONFATAL, "non-constant value given to `%%rotate'");
H. Peter Anvine2c80182005-01-15 22:15:51 +00002866 return DIRECTIVE_FOUND;
2867 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002868 mmac = istk->mstk;
2869 while (mmac && !mmac->name) /* avoid mistaking %reps for macros */
2870 mmac = mmac->next_active;
2871 if (!mmac) {
H. Peter Anvin215186f2016-02-17 20:27:41 -08002872 nasm_error(ERR_NONFATAL, "`%%rotate' invoked outside a macro call");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002873 } else if (mmac->nparam == 0) {
H. Peter Anvin215186f2016-02-17 20:27:41 -08002874 nasm_error(ERR_NONFATAL,
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002875 "`%%rotate' invoked within macro without parameters");
2876 } else {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002877 int rotate = mmac->rotate + reloc_value(evalresult);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002878
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002879 rotate %= (int)mmac->nparam;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002880 if (rotate < 0)
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002881 rotate += mmac->nparam;
2882
2883 mmac->rotate = rotate;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002884 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002885 return DIRECTIVE_FOUND;
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00002886
H. Peter Anvine2c80182005-01-15 22:15:51 +00002887 case PP_REP:
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002888 nolist = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002889 do {
2890 tline = tline->next;
2891 } while (tok_type_(tline, TOK_WHITESPACE));
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00002892
H. Peter Anvine2c80182005-01-15 22:15:51 +00002893 if (tok_type_(tline, TOK_ID) &&
2894 nasm_stricmp(tline->text, ".nolist") == 0) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002895 nolist = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002896 do {
2897 tline = tline->next;
2898 } while (tok_type_(tline, TOK_WHITESPACE));
2899 }
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00002900
H. Peter Anvine2c80182005-01-15 22:15:51 +00002901 if (tline) {
2902 t = expand_smacro(tline);
2903 tptr = &t;
2904 tokval.t_type = TOKEN_INVALID;
2905 evalresult =
H. Peter Anvin215186f2016-02-17 20:27:41 -08002906 evaluate(ppscan, tptr, &tokval, NULL, pass, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002907 if (!evalresult) {
2908 free_tlist(origline);
2909 return DIRECTIVE_FOUND;
2910 }
2911 if (tokval.t_type)
H. Peter Anvin215186f2016-02-17 20:27:41 -08002912 nasm_error(ERR_WARNING|ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +00002913 "trailing garbage after expression ignored");
2914 if (!is_simple(evalresult)) {
H. Peter Anvin215186f2016-02-17 20:27:41 -08002915 nasm_error(ERR_NONFATAL, "non-constant value given to `%%rep'");
H. Peter Anvine2c80182005-01-15 22:15:51 +00002916 return DIRECTIVE_FOUND;
2917 }
Cyrill Gorcunove091d6e2010-08-09 13:58:22 +04002918 count = reloc_value(evalresult);
2919 if (count >= REP_LIMIT) {
H. Peter Anvin215186f2016-02-17 20:27:41 -08002920 nasm_error(ERR_NONFATAL, "`%%rep' value exceeds limit");
Cyrill Gorcunove091d6e2010-08-09 13:58:22 +04002921 count = 0;
2922 } else
2923 count++;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002924 } else {
H. Peter Anvin215186f2016-02-17 20:27:41 -08002925 nasm_error(ERR_NONFATAL, "`%%rep' expects a repeat count");
H. Peter Anvinf8ba53e2007-10-11 10:11:57 -07002926 count = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002927 }
2928 free_tlist(origline);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002929
2930 tmp_defining = defining;
2931 defining = nasm_malloc(sizeof(MMacro));
2932 defining->prev = NULL;
2933 defining->name = NULL; /* flags this macro as a %rep block */
2934 defining->casesense = false;
2935 defining->plus = false;
2936 defining->nolist = nolist;
2937 defining->in_progress = count;
2938 defining->max_depth = 0;
2939 defining->nparam_min = defining->nparam_max = 0;
2940 defining->defaults = NULL;
2941 defining->dlist = NULL;
2942 defining->expansion = NULL;
2943 defining->next_active = istk->mstk;
2944 defining->rep_nest = tmp_defining;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002945 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002946
H. Peter Anvine2c80182005-01-15 22:15:51 +00002947 case PP_ENDREP:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002948 if (!defining || defining->name) {
H. Peter Anvin215186f2016-02-17 20:27:41 -08002949 nasm_error(ERR_NONFATAL, "`%%endrep': no matching `%%rep'");
H. Peter Anvine2c80182005-01-15 22:15:51 +00002950 return DIRECTIVE_FOUND;
2951 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00002952
H. Peter Anvine2c80182005-01-15 22:15:51 +00002953 /*
2954 * Now we have a "macro" defined - although it has no name
2955 * and we won't be entering it in the hash tables - we must
2956 * push a macro-end marker for it on to istk->expansion.
2957 * After that, it will take care of propagating itself (a
2958 * macro-end marker line for a macro which is really a %rep
2959 * block will cause the macro to be re-expanded, complete
2960 * with another macro-end marker to ensure the process
2961 * continues) until the whole expansion is forcibly removed
2962 * from istk->expansion by a %exitrep.
2963 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002964 l = nasm_malloc(sizeof(Line));
2965 l->next = istk->expansion;
2966 l->finishes = defining;
2967 l->first = NULL;
2968 istk->expansion = l;
2969
2970 istk->mstk = defining;
2971
H. Peter Anvin172b8402016-02-18 01:16:18 -08002972 lfmt->uplevel(defining->nolist ? LIST_MACRO_NOLIST : LIST_MACRO);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002973 tmp_defining = defining;
2974 defining = defining->rep_nest;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002975 free_tlist(origline);
2976 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002977
H. Peter Anvine2c80182005-01-15 22:15:51 +00002978 case PP_EXITREP:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002979 /*
2980 * We must search along istk->expansion until we hit a
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002981 * macro-end marker for a macro with no name. Then we set
2982 * its `in_progress' flag to 0.
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002983 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002984 list_for_each(l, istk->expansion)
2985 if (l->finishes && !l->finishes->name)
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002986 break;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002987
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002988 if (l)
2989 l->finishes->in_progress = 1;
2990 else
H. Peter Anvin215186f2016-02-17 20:27:41 -08002991 nasm_error(ERR_NONFATAL, "`%%exitrep' not within `%%rep' block");
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_XDEFINE:
2996 case PP_IXDEFINE:
2997 case PP_DEFINE:
2998 case PP_IDEFINE:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002999 casesense = (i == PP_DEFINE || i == PP_XDEFINE);
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07003000
H. Peter Anvine2c80182005-01-15 22:15:51 +00003001 tline = tline->next;
3002 skip_white_(tline);
3003 tline = expand_id(tline);
3004 if (!tline || (tline->type != TOK_ID &&
3005 (tline->type != TOK_PREPROC_ID ||
3006 tline->text[1] != '$'))) {
H. Peter Anvin215186f2016-02-17 20:27:41 -08003007 nasm_error(ERR_NONFATAL, "`%s' expects a macro identifier",
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003008 pp_directives[i]);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003009 free_tlist(origline);
3010 return DIRECTIVE_FOUND;
3011 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003012
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04003013 ctx = get_ctx(tline->text, &mname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003014 last = tline;
3015 param_start = tline = tline->next;
3016 nparam = 0;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003017
H. Peter Anvine2c80182005-01-15 22:15:51 +00003018 /* Expand the macro definition now for %xdefine and %ixdefine */
3019 if ((i == PP_XDEFINE) || (i == PP_IXDEFINE))
3020 tline = expand_smacro(tline);
H. Peter Anvin734b1882002-04-30 21:01:08 +00003021
H. Peter Anvine2c80182005-01-15 22:15:51 +00003022 if (tok_is_(tline, "(")) {
3023 /*
3024 * This macro has parameters.
3025 */
H. Peter Anvin734b1882002-04-30 21:01:08 +00003026
H. Peter Anvine2c80182005-01-15 22:15:51 +00003027 tline = tline->next;
3028 while (1) {
3029 skip_white_(tline);
3030 if (!tline) {
H. Peter Anvin215186f2016-02-17 20:27:41 -08003031 nasm_error(ERR_NONFATAL, "parameter identifier expected");
H. Peter Anvine2c80182005-01-15 22:15:51 +00003032 free_tlist(origline);
3033 return DIRECTIVE_FOUND;
3034 }
3035 if (tline->type != TOK_ID) {
H. Peter Anvin215186f2016-02-17 20:27:41 -08003036 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00003037 "`%s': parameter identifier expected",
3038 tline->text);
3039 free_tlist(origline);
3040 return DIRECTIVE_FOUND;
3041 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003042 tline->type = TOK_SMAC_PARAM + nparam++;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003043 tline = tline->next;
3044 skip_white_(tline);
3045 if (tok_is_(tline, ",")) {
3046 tline = tline->next;
H. Peter Anvinca348b62008-07-23 10:49:26 -04003047 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003048 if (!tok_is_(tline, ")")) {
H. Peter Anvin215186f2016-02-17 20:27:41 -08003049 nasm_error(ERR_NONFATAL,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003050 "`)' expected to terminate macro template");
3051 free_tlist(origline);
3052 return DIRECTIVE_FOUND;
3053 }
3054 break;
3055 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00003056 }
3057 last = tline;
3058 tline = tline->next;
3059 }
3060 if (tok_type_(tline, TOK_WHITESPACE))
3061 last = tline, tline = tline->next;
3062 macro_start = NULL;
3063 last->next = NULL;
3064 t = tline;
3065 while (t) {
3066 if (t->type == TOK_ID) {
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04003067 list_for_each(tt, param_start)
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003068 if (tt->type >= TOK_SMAC_PARAM &&
H. Peter Anvine2c80182005-01-15 22:15:51 +00003069 !strcmp(tt->text, t->text))
3070 t->type = tt->type;
3071 }
3072 tt = t->next;
3073 t->next = macro_start;
3074 macro_start = t;
3075 t = tt;
3076 }
3077 /*
3078 * Good. We now have a macro name, a parameter count, and a
3079 * token list (in reverse order) for an expansion. We ought
3080 * to be OK just to create an SMacro, store it, and let
3081 * free_tlist have the rest of the line (which we have
3082 * carefully re-terminated after chopping off the expansion
3083 * from the end).
3084 */
H. Peter Anvin4db5a162007-10-11 13:42:09 -07003085 define_smacro(ctx, mname, casesense, nparam, macro_start);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003086 free_tlist(origline);
3087 return DIRECTIVE_FOUND;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003088
H. Peter Anvine2c80182005-01-15 22:15:51 +00003089 case PP_UNDEF:
3090 tline = tline->next;
3091 skip_white_(tline);
3092 tline = expand_id(tline);
3093 if (!tline || (tline->type != TOK_ID &&
3094 (tline->type != TOK_PREPROC_ID ||
3095 tline->text[1] != '$'))) {
H. Peter Anvin215186f2016-02-17 20:27:41 -08003096 nasm_error(ERR_NONFATAL, "`%%undef' expects a macro identifier");
H. Peter Anvine2c80182005-01-15 22:15:51 +00003097 free_tlist(origline);
3098 return DIRECTIVE_FOUND;
3099 }
3100 if (tline->next) {
H. Peter Anvin215186f2016-02-17 20:27:41 -08003101 nasm_error(ERR_WARNING|ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +00003102 "trailing garbage after macro name ignored");
3103 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003104
H. Peter Anvine2c80182005-01-15 22:15:51 +00003105 /* Find the context that symbol belongs to */
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04003106 ctx = get_ctx(tline->text, &mname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003107 undef_smacro(ctx, mname);
3108 free_tlist(origline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003109 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003110
H. Peter Anvin9e200162008-06-04 17:23:14 -07003111 case PP_DEFSTR:
3112 case PP_IDEFSTR:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003113 casesense = (i == PP_DEFSTR);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003114
3115 tline = tline->next;
3116 skip_white_(tline);
3117 tline = expand_id(tline);
3118 if (!tline || (tline->type != TOK_ID &&
3119 (tline->type != TOK_PREPROC_ID ||
3120 tline->text[1] != '$'))) {
H. Peter Anvin215186f2016-02-17 20:27:41 -08003121 nasm_error(ERR_NONFATAL, "`%s' expects a macro identifier",
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003122 pp_directives[i]);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003123 free_tlist(origline);
3124 return DIRECTIVE_FOUND;
3125 }
3126
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04003127 ctx = get_ctx(tline->text, &mname);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003128 last = tline;
3129 tline = expand_smacro(tline->next);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003130 last->next = NULL;
H. Peter Anvin9e200162008-06-04 17:23:14 -07003131
3132 while (tok_type_(tline, TOK_WHITESPACE))
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003133 tline = delete_Token(tline);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003134
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003135 p = detoken(tline, false);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003136 macro_start = nasm_malloc(sizeof(*macro_start));
3137 macro_start->next = NULL;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003138 macro_start->text = nasm_quote(p, strlen(p));
3139 macro_start->type = TOK_STRING;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003140 macro_start->a.mac = NULL;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003141 nasm_free(p);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003142
3143 /*
3144 * We now have a macro name, an implicit parameter count of
3145 * zero, and a string token to use as an expansion. Create
3146 * and store an SMacro.
3147 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003148 define_smacro(ctx, mname, casesense, 0, macro_start);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003149 free_tlist(origline);
3150 return DIRECTIVE_FOUND;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003151
H. Peter Anvin2f55bda2009-07-14 15:04:04 -04003152 case PP_DEFTOK:
3153 case PP_IDEFTOK:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003154 casesense = (i == PP_DEFTOK);
3155
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003156 tline = tline->next;
3157 skip_white_(tline);
3158 tline = expand_id(tline);
3159 if (!tline || (tline->type != TOK_ID &&
3160 (tline->type != TOK_PREPROC_ID ||
3161 tline->text[1] != '$'))) {
H. Peter Anvin215186f2016-02-17 20:27:41 -08003162 nasm_error(ERR_NONFATAL,
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003163 "`%s' expects a macro identifier as first parameter",
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003164 pp_directives[i]);
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003165 free_tlist(origline);
3166 return DIRECTIVE_FOUND;
3167 }
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04003168 ctx = get_ctx(tline->text, &mname);
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003169 last = tline;
3170 tline = expand_smacro(tline->next);
3171 last->next = NULL;
3172
3173 t = tline;
3174 while (tok_type_(t, TOK_WHITESPACE))
3175 t = t->next;
3176 /* t should now point to the string */
Cyrill Gorcunov6908e582010-09-06 19:36:15 +04003177 if (!tok_type_(t, TOK_STRING)) {
H. Peter Anvin215186f2016-02-17 20:27:41 -08003178 nasm_error(ERR_NONFATAL,
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003179 "`%s` requires string as second parameter",
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003180 pp_directives[i]);
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003181 free_tlist(tline);
3182 free_tlist(origline);
3183 return DIRECTIVE_FOUND;
3184 }
3185
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04003186 /*
3187 * Convert the string to a token stream. Note that smacros
3188 * are stored with the token stream reversed, so we have to
3189 * reverse the output of tokenize().
3190 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003191 nasm_unquote_cstr(t->text, i);
H. Peter Anvinb40992c2010-09-15 08:57:21 -07003192 macro_start = reverse_tokens(tokenize(t->text));
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003193
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003194 /*
3195 * We now have a macro name, an implicit parameter count of
3196 * zero, and a numeric token to use as an expansion. Create
3197 * and store an SMacro.
3198 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003199 define_smacro(ctx, mname, casesense, 0, macro_start);
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003200 free_tlist(tline);
3201 free_tlist(origline);
3202 return DIRECTIVE_FOUND;
H. Peter Anvin9e200162008-06-04 17:23:14 -07003203
H. Peter Anvin418ca702008-05-30 10:42:30 -07003204 case PP_PATHSEARCH:
3205 {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003206 FILE *fp;
3207 StrList *xsl = NULL;
3208 StrList **xst = &xsl;
H. Peter Anvin418ca702008-05-30 10:42:30 -07003209
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003210 casesense = true;
H. Peter Anvin418ca702008-05-30 10:42:30 -07003211
3212 tline = tline->next;
3213 skip_white_(tline);
3214 tline = expand_id(tline);
3215 if (!tline || (tline->type != TOK_ID &&
3216 (tline->type != TOK_PREPROC_ID ||
3217 tline->text[1] != '$'))) {
H. Peter Anvin215186f2016-02-17 20:27:41 -08003218 nasm_error(ERR_NONFATAL,
H. Peter Anvin418ca702008-05-30 10:42:30 -07003219 "`%%pathsearch' expects a macro identifier as first parameter");
3220 free_tlist(origline);
3221 return DIRECTIVE_FOUND;
3222 }
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04003223 ctx = get_ctx(tline->text, &mname);
H. Peter Anvin418ca702008-05-30 10:42:30 -07003224 last = tline;
3225 tline = expand_smacro(tline->next);
3226 last->next = NULL;
3227
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003228 t = tline;
H. Peter Anvin418ca702008-05-30 10:42:30 -07003229 while (tok_type_(t, TOK_WHITESPACE))
3230 t = t->next;
3231
3232 if (!t || (t->type != TOK_STRING &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003233 t->type != TOK_INTERNAL_STRING)) {
H. Peter Anvin215186f2016-02-17 20:27:41 -08003234 nasm_error(ERR_NONFATAL, "`%%pathsearch' expects a file name");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003235 free_tlist(tline);
H. Peter Anvin418ca702008-05-30 10:42:30 -07003236 free_tlist(origline);
3237 return DIRECTIVE_FOUND; /* but we did _something_ */
3238 }
3239 if (t->next)
H. Peter Anvin215186f2016-02-17 20:27:41 -08003240 nasm_error(ERR_WARNING|ERR_PASS1,
H. Peter Anvin418ca702008-05-30 10:42:30 -07003241 "trailing garbage after `%%pathsearch' ignored");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003242 p = t->text;
H. Peter Anvin427cc912008-06-01 21:43:03 -07003243 if (t->type != TOK_INTERNAL_STRING)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003244 nasm_unquote(p, NULL);
H. Peter Anvin418ca702008-05-30 10:42:30 -07003245
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003246 fp = inc_fopen(p, &xsl, &xst, true);
3247 if (fp) {
3248 p = xsl->str;
3249 fclose(fp); /* Don't actually care about the file */
3250 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003251 macro_start = nasm_malloc(sizeof(*macro_start));
3252 macro_start->next = NULL;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003253 macro_start->text = nasm_quote(p, strlen(p));
3254 macro_start->type = TOK_STRING;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003255 macro_start->a.mac = NULL;
3256 if (xsl)
3257 nasm_free(xsl);
H. Peter Anvin418ca702008-05-30 10:42:30 -07003258
3259 /*
3260 * We now have a macro name, an implicit parameter count of
3261 * zero, and a string token to use as an expansion. Create
3262 * and store an SMacro.
3263 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003264 define_smacro(ctx, mname, casesense, 0, macro_start);
3265 free_tlist(tline);
H. Peter Anvin418ca702008-05-30 10:42:30 -07003266 free_tlist(origline);
3267 return DIRECTIVE_FOUND;
3268 }
3269
H. Peter Anvine2c80182005-01-15 22:15:51 +00003270 case PP_STRLEN:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003271 casesense = true;
H. Peter Anvin70653092007-10-19 14:42:29 -07003272
H. Peter Anvine2c80182005-01-15 22:15:51 +00003273 tline = tline->next;
3274 skip_white_(tline);
3275 tline = expand_id(tline);
3276 if (!tline || (tline->type != TOK_ID &&
3277 (tline->type != TOK_PREPROC_ID ||
3278 tline->text[1] != '$'))) {
H. Peter Anvin215186f2016-02-17 20:27:41 -08003279 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00003280 "`%%strlen' expects a macro identifier as first parameter");
3281 free_tlist(origline);
3282 return DIRECTIVE_FOUND;
3283 }
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04003284 ctx = get_ctx(tline->text, &mname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003285 last = tline;
3286 tline = expand_smacro(tline->next);
3287 last->next = NULL;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003288
H. Peter Anvine2c80182005-01-15 22:15:51 +00003289 t = tline;
3290 while (tok_type_(t, TOK_WHITESPACE))
3291 t = t->next;
3292 /* t should now point to the string */
Cyrill Gorcunov4e1d5ab2010-07-23 18:51:51 +04003293 if (!tok_type_(t, TOK_STRING)) {
H. Peter Anvin215186f2016-02-17 20:27:41 -08003294 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00003295 "`%%strlen` requires string as second parameter");
3296 free_tlist(tline);
3297 free_tlist(origline);
3298 return DIRECTIVE_FOUND;
3299 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003300
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003301 macro_start = nasm_malloc(sizeof(*macro_start));
3302 macro_start->next = NULL;
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07003303 make_tok_num(macro_start, nasm_unquote(t->text, NULL));
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003304 macro_start->a.mac = NULL;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003305
H. Peter Anvine2c80182005-01-15 22:15:51 +00003306 /*
3307 * We now have a macro name, an implicit parameter count of
3308 * zero, and a numeric token to use as an expansion. Create
3309 * and store an SMacro.
3310 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003311 define_smacro(ctx, mname, casesense, 0, macro_start);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003312 free_tlist(tline);
3313 free_tlist(origline);
3314 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003315
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003316 case PP_STRCAT:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003317 casesense = true;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003318
3319 tline = tline->next;
3320 skip_white_(tline);
3321 tline = expand_id(tline);
3322 if (!tline || (tline->type != TOK_ID &&
3323 (tline->type != TOK_PREPROC_ID ||
3324 tline->text[1] != '$'))) {
H. Peter Anvin215186f2016-02-17 20:27:41 -08003325 nasm_error(ERR_NONFATAL,
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003326 "`%%strcat' expects a macro identifier as first parameter");
3327 free_tlist(origline);
3328 return DIRECTIVE_FOUND;
3329 }
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04003330 ctx = get_ctx(tline->text, &mname);
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003331 last = tline;
3332 tline = expand_smacro(tline->next);
3333 last->next = NULL;
3334
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003335 len = 0;
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04003336 list_for_each(t, tline) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003337 switch (t->type) {
3338 case TOK_WHITESPACE:
3339 break;
3340 case TOK_STRING:
3341 len += t->a.len = nasm_unquote(t->text, NULL);
3342 break;
3343 case TOK_OTHER:
3344 if (!strcmp(t->text, ",")) /* permit comma separators */
3345 break;
3346 /* else fall through */
3347 default:
H. Peter Anvin215186f2016-02-17 20:27:41 -08003348 nasm_error(ERR_NONFATAL,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003349 "non-string passed to `%%strcat' (%d)", t->type);
3350 free_tlist(tline);
3351 free_tlist(origline);
3352 return DIRECTIVE_FOUND;
3353 }
3354 }
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003355
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003356 p = pp = nasm_malloc(len);
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04003357 list_for_each(t, tline) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003358 if (t->type == TOK_STRING) {
3359 memcpy(p, t->text, t->a.len);
3360 p += t->a.len;
3361 }
3362 }
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003363
3364 /*
3365 * We now have a macro name, an implicit parameter count of
3366 * zero, and a numeric token to use as an expansion. Create
3367 * and store an SMacro.
3368 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003369 macro_start = new_Token(NULL, TOK_STRING, NULL, 0);
3370 macro_start->text = nasm_quote(pp, len);
3371 nasm_free(pp);
3372 define_smacro(ctx, mname, casesense, 0, macro_start);
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003373 free_tlist(tline);
3374 free_tlist(origline);
3375 return DIRECTIVE_FOUND;
3376
H. Peter Anvine2c80182005-01-15 22:15:51 +00003377 case PP_SUBSTR:
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003378 {
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003379 int64_t start, count;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003380 size_t len;
H. Peter Anvind2456592008-06-19 15:04:18 -07003381
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003382 casesense = true;
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07003383
H. Peter Anvine2c80182005-01-15 22:15:51 +00003384 tline = tline->next;
3385 skip_white_(tline);
3386 tline = expand_id(tline);
3387 if (!tline || (tline->type != TOK_ID &&
3388 (tline->type != TOK_PREPROC_ID ||
3389 tline->text[1] != '$'))) {
H. Peter Anvin215186f2016-02-17 20:27:41 -08003390 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00003391 "`%%substr' expects a macro identifier as first parameter");
3392 free_tlist(origline);
3393 return DIRECTIVE_FOUND;
3394 }
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04003395 ctx = get_ctx(tline->text, &mname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003396 last = tline;
3397 tline = expand_smacro(tline->next);
3398 last->next = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003399
Cyrill Gorcunov35519d62010-09-06 23:49:52 +04003400 if (tline) /* skip expanded id */
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003401 t = tline->next;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003402 while (tok_type_(t, TOK_WHITESPACE))
3403 t = t->next;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003404
H. Peter Anvine2c80182005-01-15 22:15:51 +00003405 /* t should now point to the string */
Cyrill Gorcunov35519d62010-09-06 23:49:52 +04003406 if (!tok_type_(t, TOK_STRING)) {
H. Peter Anvin215186f2016-02-17 20:27:41 -08003407 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00003408 "`%%substr` requires string as second parameter");
3409 free_tlist(tline);
3410 free_tlist(origline);
3411 return DIRECTIVE_FOUND;
3412 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003413
H. Peter Anvine2c80182005-01-15 22:15:51 +00003414 tt = t->next;
3415 tptr = &tt;
3416 tokval.t_type = TOKEN_INVALID;
H. Peter Anvin215186f2016-02-17 20:27:41 -08003417 evalresult = evaluate(ppscan, tptr, &tokval, NULL, pass, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003418 if (!evalresult) {
3419 free_tlist(tline);
3420 free_tlist(origline);
3421 return DIRECTIVE_FOUND;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003422 } else if (!is_simple(evalresult)) {
H. Peter Anvin215186f2016-02-17 20:27:41 -08003423 nasm_error(ERR_NONFATAL, "non-constant value given to `%%substr`");
H. Peter Anvine2c80182005-01-15 22:15:51 +00003424 free_tlist(tline);
3425 free_tlist(origline);
3426 return DIRECTIVE_FOUND;
3427 }
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003428 start = evalresult->value - 1;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003429
3430 while (tok_type_(tt, TOK_WHITESPACE))
3431 tt = tt->next;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003432 if (!tt) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003433 count = 1; /* Backwards compatibility: one character */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003434 } else {
3435 tokval.t_type = TOKEN_INVALID;
H. Peter Anvin215186f2016-02-17 20:27:41 -08003436 evalresult = evaluate(ppscan, tptr, &tokval, NULL, pass, NULL);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003437 if (!evalresult) {
3438 free_tlist(tline);
3439 free_tlist(origline);
3440 return DIRECTIVE_FOUND;
3441 } else if (!is_simple(evalresult)) {
H. Peter Anvin215186f2016-02-17 20:27:41 -08003442 nasm_error(ERR_NONFATAL, "non-constant value given to `%%substr`");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003443 free_tlist(tline);
3444 free_tlist(origline);
3445 return DIRECTIVE_FOUND;
3446 }
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003447 count = evalresult->value;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003448 }
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003449
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003450 len = nasm_unquote(t->text, NULL);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003451
Cyrill Gorcunovcff031e2010-09-07 20:31:11 +04003452 /* make start and count being in range */
3453 if (start < 0)
3454 start = 0;
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003455 if (count < 0)
3456 count = len + count + 1 - start;
3457 if (start + count > (int64_t)len)
Cyrill Gorcunovcff031e2010-09-07 20:31:11 +04003458 count = len - start;
3459 if (!len || count < 0 || start >=(int64_t)len)
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003460 start = -1, count = 0; /* empty string */
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003461
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003462 macro_start = nasm_malloc(sizeof(*macro_start));
3463 macro_start->next = NULL;
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003464 macro_start->text = nasm_quote((start < 0) ? "" : t->text + start, count);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003465 macro_start->type = TOK_STRING;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003466 macro_start->a.mac = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003467
H. Peter Anvine2c80182005-01-15 22:15:51 +00003468 /*
3469 * We now have a macro name, an implicit parameter count of
3470 * zero, and a numeric token to use as an expansion. Create
3471 * and store an SMacro.
3472 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003473 define_smacro(ctx, mname, casesense, 0, macro_start);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003474 free_tlist(tline);
3475 free_tlist(origline);
3476 return DIRECTIVE_FOUND;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003477 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003478
H. Peter Anvine2c80182005-01-15 22:15:51 +00003479 case PP_ASSIGN:
3480 case PP_IASSIGN:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003481 casesense = (i == PP_ASSIGN);
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07003482
H. Peter Anvine2c80182005-01-15 22:15:51 +00003483 tline = tline->next;
3484 skip_white_(tline);
3485 tline = expand_id(tline);
3486 if (!tline || (tline->type != TOK_ID &&
3487 (tline->type != TOK_PREPROC_ID ||
3488 tline->text[1] != '$'))) {
H. Peter Anvin215186f2016-02-17 20:27:41 -08003489 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00003490 "`%%%sassign' expects a macro identifier",
3491 (i == PP_IASSIGN ? "i" : ""));
3492 free_tlist(origline);
3493 return DIRECTIVE_FOUND;
3494 }
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04003495 ctx = get_ctx(tline->text, &mname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003496 last = tline;
3497 tline = expand_smacro(tline->next);
3498 last->next = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003499
H. Peter Anvine2c80182005-01-15 22:15:51 +00003500 t = tline;
3501 tptr = &t;
3502 tokval.t_type = TOKEN_INVALID;
H. Peter Anvin215186f2016-02-17 20:27:41 -08003503 evalresult = evaluate(ppscan, tptr, &tokval, NULL, pass, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003504 free_tlist(tline);
3505 if (!evalresult) {
3506 free_tlist(origline);
3507 return DIRECTIVE_FOUND;
3508 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003509
H. Peter Anvine2c80182005-01-15 22:15:51 +00003510 if (tokval.t_type)
H. Peter Anvin215186f2016-02-17 20:27:41 -08003511 nasm_error(ERR_WARNING|ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +00003512 "trailing garbage after expression ignored");
H. Peter Anvin734b1882002-04-30 21:01:08 +00003513
H. Peter Anvine2c80182005-01-15 22:15:51 +00003514 if (!is_simple(evalresult)) {
H. Peter Anvin215186f2016-02-17 20:27:41 -08003515 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00003516 "non-constant value given to `%%%sassign'",
3517 (i == PP_IASSIGN ? "i" : ""));
3518 free_tlist(origline);
3519 return DIRECTIVE_FOUND;
3520 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003521
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003522 macro_start = nasm_malloc(sizeof(*macro_start));
3523 macro_start->next = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003524 make_tok_num(macro_start, reloc_value(evalresult));
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003525 macro_start->a.mac = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003526
H. Peter Anvine2c80182005-01-15 22:15:51 +00003527 /*
3528 * We now have a macro name, an implicit parameter count of
3529 * zero, and a numeric token to use as an expansion. Create
3530 * and store an SMacro.
3531 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003532 define_smacro(ctx, mname, casesense, 0, macro_start);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003533 free_tlist(origline);
3534 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003535
H. Peter Anvine2c80182005-01-15 22:15:51 +00003536 case PP_LINE:
3537 /*
3538 * Syntax is `%line nnn[+mmm] [filename]'
3539 */
3540 tline = tline->next;
3541 skip_white_(tline);
3542 if (!tok_type_(tline, TOK_NUMBER)) {
H. Peter Anvin215186f2016-02-17 20:27:41 -08003543 nasm_error(ERR_NONFATAL, "`%%line' expects line number");
H. Peter Anvine2c80182005-01-15 22:15:51 +00003544 free_tlist(origline);
3545 return DIRECTIVE_FOUND;
3546 }
H. Peter Anvin70055962007-10-11 00:05:31 -07003547 k = readnum(tline->text, &err);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003548 m = 1;
3549 tline = tline->next;
3550 if (tok_is_(tline, "+")) {
3551 tline = tline->next;
3552 if (!tok_type_(tline, TOK_NUMBER)) {
H. Peter Anvin215186f2016-02-17 20:27:41 -08003553 nasm_error(ERR_NONFATAL, "`%%line' expects line increment");
H. Peter Anvine2c80182005-01-15 22:15:51 +00003554 free_tlist(origline);
3555 return DIRECTIVE_FOUND;
3556 }
H. Peter Anvin70055962007-10-11 00:05:31 -07003557 m = readnum(tline->text, &err);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003558 tline = tline->next;
3559 }
3560 skip_white_(tline);
3561 src_set_linnum(k);
3562 istk->lineinc = m;
3563 if (tline) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07003564 nasm_free(src_set_fname(detoken(tline, false)));
H. Peter Anvine2c80182005-01-15 22:15:51 +00003565 }
3566 free_tlist(origline);
3567 return DIRECTIVE_FOUND;
Keith Kaniosb307a4f2010-11-06 17:41:51 -05003568
H. Peter Anvine2c80182005-01-15 22:15:51 +00003569 default:
H. Peter Anvin215186f2016-02-17 20:27:41 -08003570 nasm_error(ERR_FATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00003571 "preprocessor directive `%s' not yet implemented",
H. Peter Anvin4169a472007-09-12 01:29:43 +00003572 pp_directives[i]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003573 return DIRECTIVE_FOUND;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003574 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003575}
3576
3577/*
H. Peter Anvin76690a12002-04-30 20:52:49 +00003578 * Ensure that a macro parameter contains a condition code and
3579 * nothing else. Return the condition code index if so, or -1
3580 * otherwise.
3581 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003582static int find_cc(Token * t)
H. Peter Anvineba20a72002-04-30 20:53:55 +00003583{
H. Peter Anvin76690a12002-04-30 20:52:49 +00003584 Token *tt;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003585
H. Peter Anvin25a99342007-09-22 17:45:45 -07003586 if (!t)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003587 return -1; /* Probably a %+ without a space */
H. Peter Anvin25a99342007-09-22 17:45:45 -07003588
H. Peter Anvineba20a72002-04-30 20:53:55 +00003589 skip_white_(t);
H. Peter Anvin76690a12002-04-30 20:52:49 +00003590 if (t->type != TOK_ID)
H. Peter Anvine2c80182005-01-15 22:15:51 +00003591 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003592 tt = t->next;
H. Peter Anvineba20a72002-04-30 20:53:55 +00003593 skip_white_(tt);
H. Peter Anvin76690a12002-04-30 20:52:49 +00003594 if (tt && (tt->type != TOK_OTHER || strcmp(tt->text, ",")))
H. Peter Anvine2c80182005-01-15 22:15:51 +00003595 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003596
Cyrill Gorcunov19456392012-05-02 00:18:56 +04003597 return bsii(t->text, (const char **)conditions, ARRAY_SIZE(conditions));
H. Peter Anvin76690a12002-04-30 20:52:49 +00003598}
3599
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003600/*
3601 * This routines walks over tokens strem and hadnles tokens
3602 * pasting, if @handle_explicit passed then explicit pasting
3603 * term is handled, otherwise -- implicit pastings only.
3604 */
Cyrill Gorcunov575d4282010-10-06 00:25:55 +04003605static bool paste_tokens(Token **head, const struct tokseq_match *m,
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003606 size_t mnum, bool handle_explicit)
H. Peter Anvind784a082009-04-20 14:01:18 -07003607{
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003608 Token *tok, *next, **prev_next, **prev_nonspace;
3609 bool pasted = false;
3610 char *buf, *p;
3611 size_t len, i;
H. Peter Anvind784a082009-04-20 14:01:18 -07003612
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003613 /*
3614 * The last token before pasting. We need it
3615 * to be able to connect new handled tokens.
3616 * In other words if there were a tokens stream
3617 *
3618 * A -> B -> C -> D
3619 *
3620 * and we've joined tokens B and C, the resulting
3621 * stream should be
3622 *
3623 * A -> BC -> D
3624 */
3625 tok = *head;
3626 prev_next = NULL;
3627
3628 if (!tok_type_(tok, TOK_WHITESPACE) && !tok_type_(tok, TOK_PASTE))
3629 prev_nonspace = head;
3630 else
3631 prev_nonspace = NULL;
3632
3633 while (tok && (next = tok->next)) {
3634
3635 switch (tok->type) {
H. Peter Anvind784a082009-04-20 14:01:18 -07003636 case TOK_WHITESPACE:
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003637 /* Zap redundant whitespaces */
3638 while (tok_type_(next, TOK_WHITESPACE))
3639 next = delete_Token(next);
3640 tok->next = next;
H. Peter Anvind784a082009-04-20 14:01:18 -07003641 break;
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003642
3643 case TOK_PASTE:
3644 /* Explicit pasting */
3645 if (!handle_explicit)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003646 break;
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003647 next = delete_Token(tok);
3648
3649 while (tok_type_(next, TOK_WHITESPACE))
3650 next = delete_Token(next);
3651
3652 if (!pasted)
3653 pasted = true;
3654
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003655 /* Left pasting token is start of line */
3656 if (!prev_nonspace)
H. Peter Anvin215186f2016-02-17 20:27:41 -08003657 nasm_error(ERR_FATAL, "No lvalue found on pasting");
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003658
Cyrill Gorcunov8b5c9fb2013-02-04 01:24:54 +04003659 /*
3660 * No ending token, this might happen in two
3661 * cases
3662 *
3663 * 1) There indeed no right token at all
3664 * 2) There is a bare "%define ID" statement,
3665 * and @ID does expand to whitespace.
3666 *
3667 * So technically we need to do a grammar analysis
3668 * in another stage of parsing, but for now lets don't
3669 * change the behaviour people used to. Simply allow
3670 * whitespace after paste token.
3671 */
3672 if (!next) {
3673 /*
3674 * Zap ending space tokens and that's all.
3675 */
3676 tok = (*prev_nonspace)->next;
3677 while (tok_type_(tok, TOK_WHITESPACE))
3678 tok = delete_Token(tok);
3679 tok = *prev_nonspace;
3680 tok->next = NULL;
3681 break;
3682 }
3683
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003684 tok = *prev_nonspace;
3685 while (tok_type_(tok, TOK_WHITESPACE))
3686 tok = delete_Token(tok);
3687 len = strlen(tok->text);
3688 len += strlen(next->text);
3689
3690 p = buf = nasm_malloc(len + 1);
3691 strcpy(p, tok->text);
3692 p = strchr(p, '\0');
3693 strcpy(p, next->text);
3694
3695 delete_Token(tok);
3696
3697 tok = tokenize(buf);
3698 nasm_free(buf);
3699
3700 *prev_nonspace = tok;
3701 while (tok && tok->next)
3702 tok = tok->next;
3703
3704 tok->next = delete_Token(next);
3705
3706 /* Restart from pasted tokens head */
3707 tok = *prev_nonspace;
3708 break;
3709
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003710 default:
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003711 /* implicit pasting */
Cyrill Gorcunov575d4282010-10-06 00:25:55 +04003712 for (i = 0; i < mnum; i++) {
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003713 if (!(PP_CONCAT_MATCH(tok, m[i].mask_head)))
3714 continue;
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +04003715
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003716 len = 0;
3717 while (next && PP_CONCAT_MATCH(next, m[i].mask_tail)) {
3718 len += strlen(next->text);
3719 next = next->next;
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +04003720 }
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003721
3722 /* No match */
3723 if (tok == next)
3724 break;
3725
3726 len += strlen(tok->text);
3727 p = buf = nasm_malloc(len + 1);
3728
3729 while (tok != next) {
3730 strcpy(p, tok->text);
3731 p = strchr(p, '\0');
3732 tok = delete_Token(tok);
3733 }
3734
3735 tok = tokenize(buf);
3736 nasm_free(buf);
3737
3738 if (prev_next)
3739 *prev_next = tok;
3740 else
3741 *head = tok;
3742
3743 /*
3744 * Connect pasted into original stream,
3745 * ie A -> new-tokens -> B
3746 */
3747 while (tok && tok->next)
3748 tok = tok->next;
3749 tok->next = next;
3750
3751 if (!pasted)
3752 pasted = true;
3753
3754 /* Restart from pasted tokens head */
3755 tok = prev_next ? *prev_next : *head;
Cyrill Gorcunov575d4282010-10-06 00:25:55 +04003756 }
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003757
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003758 break;
H. Peter Anvind784a082009-04-20 14:01:18 -07003759 }
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003760
3761 prev_next = &tok->next;
3762
3763 if (tok->next &&
3764 !tok_type_(tok->next, TOK_WHITESPACE) &&
3765 !tok_type_(tok->next, TOK_PASTE))
3766 prev_nonspace = prev_next;
3767
3768 tok = tok->next;
H. Peter Anvind784a082009-04-20 14:01:18 -07003769 }
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003770
3771 return pasted;
H. Peter Anvind784a082009-04-20 14:01:18 -07003772}
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003773
3774/*
3775 * expands to a list of tokens from %{x:y}
3776 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003777static Token *expand_mmac_params_range(MMacro *mac, Token *tline, Token ***last)
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003778{
3779 Token *t = tline, **tt, *tm, *head;
3780 char *pos;
3781 int fst, lst, j, i;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003782
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003783 pos = strchr(tline->text, ':');
3784 nasm_assert(pos);
3785
3786 lst = atoi(pos + 1);
3787 fst = atoi(tline->text + 1);
3788
3789 /*
3790 * only macros params are accounted so
3791 * if someone passes %0 -- we reject such
3792 * value(s)
3793 */
3794 if (lst == 0 || fst == 0)
3795 goto err;
3796
3797 /* the values should be sane */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003798 if ((fst > (int)mac->nparam || fst < (-(int)mac->nparam)) ||
3799 (lst > (int)mac->nparam || lst < (-(int)mac->nparam)))
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003800 goto err;
3801
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003802 fst = fst < 0 ? fst + (int)mac->nparam + 1: fst;
3803 lst = lst < 0 ? lst + (int)mac->nparam + 1: lst;
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003804
3805 /* counted from zero */
3806 fst--, lst--;
3807
3808 /*
Cyrill Gorcunove75331c2013-11-09 12:02:15 +04003809 * It will be at least one token. Note we
3810 * need to scan params until separator, otherwise
3811 * only first token will be passed.
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003812 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003813 tm = mac->params[(fst + mac->rotate) % mac->nparam];
Cyrill Gorcunove75331c2013-11-09 12:02:15 +04003814 head = new_Token(NULL, tm->type, tm->text, 0);
3815 tt = &head->next, tm = tm->next;
3816 while (tok_isnt_(tm, ",")) {
3817 t = new_Token(NULL, tm->type, tm->text, 0);
3818 *tt = t, tt = &t->next, tm = tm->next;
3819 }
3820
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003821 if (fst < lst) {
3822 for (i = fst + 1; i <= lst; i++) {
3823 t = new_Token(NULL, TOK_OTHER, ",", 0);
3824 *tt = t, tt = &t->next;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003825 j = (i + mac->rotate) % mac->nparam;
3826 tm = mac->params[j];
Cyrill Gorcunove75331c2013-11-09 12:02:15 +04003827 while (tok_isnt_(tm, ",")) {
3828 t = new_Token(NULL, tm->type, tm->text, 0);
3829 *tt = t, tt = &t->next, tm = tm->next;
3830 }
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003831 }
3832 } else {
3833 for (i = fst - 1; i >= lst; i--) {
3834 t = new_Token(NULL, TOK_OTHER, ",", 0);
3835 *tt = t, tt = &t->next;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003836 j = (i + mac->rotate) % mac->nparam;
3837 tm = mac->params[j];
Cyrill Gorcunove75331c2013-11-09 12:02:15 +04003838 while (tok_isnt_(tm, ",")) {
3839 t = new_Token(NULL, tm->type, tm->text, 0);
3840 *tt = t, tt = &t->next, tm = tm->next;
3841 }
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003842 }
3843 }
3844
3845 *last = tt;
3846 return head;
3847
3848err:
H. Peter Anvin215186f2016-02-17 20:27:41 -08003849 nasm_error(ERR_NONFATAL, "`%%{%s}': macro parameters out of range",
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003850 &tline->text[1]);
3851 return tline;
3852}
3853
H. Peter Anvin76690a12002-04-30 20:52:49 +00003854/*
3855 * Expand MMacro-local things: parameter references (%0, %n, %+n,
H. Peter Anvin67c63722008-10-26 23:49:00 -07003856 * %-n) and MMacro-local identifiers (%%foo) as well as
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003857 * macro indirection (%[...]) and range (%{..:..}).
H. Peter Anvin76690a12002-04-30 20:52:49 +00003858 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003859static Token *expand_mmac_params(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00003860{
H. Peter Anvin734b1882002-04-30 21:01:08 +00003861 Token *t, *tt, **tail, *thead;
H. Peter Anvin6125b622009-04-08 14:02:25 -07003862 bool changed = false;
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003863 char *pos;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003864
3865 tail = &thead;
3866 thead = NULL;
3867
H. Peter Anvine2c80182005-01-15 22:15:51 +00003868 while (tline) {
3869 if (tline->type == TOK_PREPROC_ID &&
Cyrill Gorcunovca611192010-06-04 09:22:12 +04003870 (((tline->text[1] == '+' || tline->text[1] == '-') && tline->text[2]) ||
3871 (tline->text[1] >= '0' && tline->text[1] <= '9') ||
3872 tline->text[1] == '%')) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00003873 char *text = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003874 int type = 0, cc; /* type = 0 to placate optimisers */
Keith Kaniosa6dfa782007-04-13 16:47:53 +00003875 char tmpbuf[30];
H. Peter Anvin25a99342007-09-22 17:45:45 -07003876 unsigned int n;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003877 int i;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003878 MMacro *mac;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003879
H. Peter Anvine2c80182005-01-15 22:15:51 +00003880 t = tline;
3881 tline = tline->next;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003882
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003883 mac = istk->mstk;
3884 while (mac && !mac->name) /* avoid mistaking %reps for macros */
3885 mac = mac->next_active;
3886 if (!mac) {
H. Peter Anvin215186f2016-02-17 20:27:41 -08003887 nasm_error(ERR_NONFATAL, "`%s': not in a macro call", t->text);
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003888 } else {
3889 pos = strchr(t->text, ':');
3890 if (!pos) {
3891 switch (t->text[1]) {
3892 /*
3893 * We have to make a substitution of one of the
3894 * forms %1, %-1, %+1, %%foo, %0.
3895 */
3896 case '0':
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003897 type = TOK_NUMBER;
3898 snprintf(tmpbuf, sizeof(tmpbuf), "%d", mac->nparam);
3899 text = nasm_strdup(tmpbuf);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003900 break;
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003901 case '%':
H. Peter Anvine2c80182005-01-15 22:15:51 +00003902 type = TOK_ID;
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003903 snprintf(tmpbuf, sizeof(tmpbuf), "..@%"PRIu64".",
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003904 mac->unique);
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003905 text = nasm_strcat(tmpbuf, t->text + 2);
3906 break;
3907 case '-':
3908 n = atoi(t->text + 2) - 1;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003909 if (n >= mac->nparam)
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003910 tt = NULL;
3911 else {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003912 if (mac->nparam > 1)
3913 n = (n + mac->rotate) % mac->nparam;
3914 tt = mac->params[n];
H. Peter Anvine2c80182005-01-15 22:15:51 +00003915 }
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003916 cc = find_cc(tt);
3917 if (cc == -1) {
H. Peter Anvin215186f2016-02-17 20:27:41 -08003918 nasm_error(ERR_NONFATAL,
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003919 "macro parameter %d is not a condition code",
3920 n + 1);
3921 text = NULL;
3922 } else {
3923 type = TOK_ID;
3924 if (inverse_ccs[cc] == -1) {
H. Peter Anvin215186f2016-02-17 20:27:41 -08003925 nasm_error(ERR_NONFATAL,
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003926 "condition code `%s' is not invertible",
3927 conditions[cc]);
3928 text = NULL;
3929 } else
3930 text = nasm_strdup(conditions[inverse_ccs[cc]]);
3931 }
3932 break;
3933 case '+':
3934 n = atoi(t->text + 2) - 1;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003935 if (n >= mac->nparam)
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003936 tt = NULL;
3937 else {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003938 if (mac->nparam > 1)
3939 n = (n + mac->rotate) % mac->nparam;
3940 tt = mac->params[n];
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003941 }
3942 cc = find_cc(tt);
3943 if (cc == -1) {
H. Peter Anvin215186f2016-02-17 20:27:41 -08003944 nasm_error(ERR_NONFATAL,
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003945 "macro parameter %d is not a condition code",
3946 n + 1);
3947 text = NULL;
3948 } else {
3949 type = TOK_ID;
3950 text = nasm_strdup(conditions[cc]);
3951 }
3952 break;
3953 default:
3954 n = atoi(t->text + 1) - 1;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003955 if (n >= mac->nparam)
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003956 tt = NULL;
3957 else {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003958 if (mac->nparam > 1)
3959 n = (n + mac->rotate) % mac->nparam;
3960 tt = mac->params[n];
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003961 }
3962 if (tt) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003963 for (i = 0; i < mac->paramlen[n]; i++) {
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003964 *tail = new_Token(NULL, tt->type, tt->text, 0);
3965 tail = &(*tail)->next;
3966 tt = tt->next;
3967 }
3968 }
3969 text = NULL; /* we've done it here */
3970 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003971 }
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003972 } else {
3973 /*
3974 * seems we have a parameters range here
3975 */
3976 Token *head, **last;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003977 head = expand_mmac_params_range(mac, t, &last);
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003978 if (head != t) {
3979 *tail = head;
3980 *last = tline;
3981 tline = head;
3982 text = NULL;
3983 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00003984 }
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003985 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00003986 if (!text) {
3987 delete_Token(t);
3988 } else {
3989 *tail = t;
3990 tail = &t->next;
3991 t->type = type;
3992 nasm_free(t->text);
3993 t->text = text;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003994 t->a.mac = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003995 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003996 changed = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003997 continue;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003998 } else if (tline->type == TOK_INDIRECT) {
3999 t = tline;
4000 tline = tline->next;
4001 tt = tokenize(t->text);
4002 tt = expand_mmac_params(tt);
4003 tt = expand_smacro(tt);
4004 *tail = tt;
4005 while (tt) {
4006 tt->a.mac = NULL; /* Necessary? */
4007 tail = &tt->next;
4008 tt = tt->next;
4009 }
4010 delete_Token(t);
4011 changed = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004012 } else {
4013 t = *tail = tline;
4014 tline = tline->next;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004015 t->a.mac = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004016 tail = &t->next;
4017 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00004018 }
H. Peter Anvineba20a72002-04-30 20:53:55 +00004019 *tail = NULL;
H. Peter Anvin67c63722008-10-26 23:49:00 -07004020
Cyrill Gorcunovc6a742c2011-06-27 01:23:09 +04004021 if (changed) {
4022 const struct tokseq_match t[] = {
4023 {
4024 PP_CONCAT_MASK(TOK_ID) |
4025 PP_CONCAT_MASK(TOK_FLOAT), /* head */
4026 PP_CONCAT_MASK(TOK_ID) |
4027 PP_CONCAT_MASK(TOK_NUMBER) |
4028 PP_CONCAT_MASK(TOK_FLOAT) |
4029 PP_CONCAT_MASK(TOK_OTHER) /* tail */
4030 },
4031 {
4032 PP_CONCAT_MASK(TOK_NUMBER), /* head */
4033 PP_CONCAT_MASK(TOK_NUMBER) /* tail */
4034 }
4035 };
4036 paste_tokens(&thead, t, ARRAY_SIZE(t), false);
4037 }
H. Peter Anvin6125b622009-04-08 14:02:25 -07004038
H. Peter Anvin76690a12002-04-30 20:52:49 +00004039 return thead;
4040}
4041
4042/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004043 * Expand all single-line macro calls made in the given line.
4044 * Return the expanded version of the line. The original is deemed
4045 * to be destroyed in the process. (In reality we'll just move
4046 * Tokens from input to output a lot of the time, rather than
4047 * actually bothering to destroy and replicate.)
4048 */
H. Peter Anvincb1cf592007-11-19 12:26:50 -08004049
H. Peter Anvine2c80182005-01-15 22:15:51 +00004050static Token *expand_smacro(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004051{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004052 Token *t, *tt, *mstart, **tail, *thead;
H. Peter Anvineba20a72002-04-30 20:53:55 +00004053 SMacro *head = NULL, *m;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004054 Token **params;
4055 int *paramsize;
H. Peter Anvin25a99342007-09-22 17:45:45 -07004056 unsigned int nparam, sparam;
H. Peter Anvind784a082009-04-20 14:01:18 -07004057 int brackets;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004058 Token *org_tline = tline;
4059 Context *ctx;
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08004060 const char *mname;
H. Peter Anvin2a15e692007-11-19 13:14:59 -08004061 int deadman = DEADMAN_LIMIT;
H. Peter Anvin8287daf2009-07-07 16:00:58 -07004062 bool expanded;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004063
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004064 /*
4065 * Trick: we should avoid changing the start token pointer since it can
4066 * be contained in "next" field of other token. Because of this
4067 * we allocate a copy of first token and work with it; at the end of
4068 * routine we copy it back
4069 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004070 if (org_tline) {
Cyrill Gorcunoved4a8052010-04-09 15:40:35 +04004071 tline = new_Token(org_tline->next, org_tline->type,
4072 org_tline->text, 0);
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004073 tline->a.mac = org_tline->a.mac;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004074 nasm_free(org_tline->text);
4075 org_tline->text = NULL;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004076 }
4077
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004078 expanded = true; /* Always expand %+ at least once */
H. Peter Anvin8287daf2009-07-07 16:00:58 -07004079
H. Peter Anvincb1cf592007-11-19 12:26:50 -08004080again:
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004081 thead = NULL;
Cyrill Gorcunoved4a8052010-04-09 15:40:35 +04004082 tail = &thead;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004083
H. Peter Anvine2c80182005-01-15 22:15:51 +00004084 while (tline) { /* main token loop */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004085 if (!--deadman) {
H. Peter Anvin215186f2016-02-17 20:27:41 -08004086 nasm_error(ERR_NONFATAL, "interminable macro recursion");
Cyrill Gorcunovbd38c8f2009-11-21 11:11:23 +03004087 goto err;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004088 }
H. Peter Anvincb1cf592007-11-19 12:26:50 -08004089
H. Peter Anvine2c80182005-01-15 22:15:51 +00004090 if ((mname = tline->text)) {
4091 /* if this token is a local macro, look in local context */
Cyrill Gorcunovc56d9ad2010-02-11 15:12:19 +03004092 if (tline->type == TOK_ID) {
4093 head = (SMacro *)hash_findix(&smacros, mname);
4094 } else if (tline->type == TOK_PREPROC_ID) {
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04004095 ctx = get_ctx(mname, &mname);
Cyrill Gorcunovc56d9ad2010-02-11 15:12:19 +03004096 head = ctx ? (SMacro *)hash_findix(&ctx->localmac, mname) : NULL;
4097 } else
4098 head = NULL;
H. Peter Anvin072771e2008-05-22 13:17:51 -07004099
H. Peter Anvine2c80182005-01-15 22:15:51 +00004100 /*
4101 * We've hit an identifier. As in is_mmacro below, we first
4102 * check whether the identifier is a single-line macro at
4103 * all, then think about checking for parameters if
4104 * necessary.
4105 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004106 list_for_each(m, head)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004107 if (!mstrcmp(m->name, mname, m->casesense))
4108 break;
4109 if (m) {
4110 mstart = tline;
4111 params = NULL;
4112 paramsize = NULL;
4113 if (m->nparam == 0) {
4114 /*
4115 * Simple case: the macro is parameterless. Discard the
4116 * one token that the macro call took, and push the
4117 * expansion back on the to-do stack.
4118 */
4119 if (!m->expansion) {
4120 if (!strcmp("__FILE__", m->name)) {
4121 int32_t num = 0;
4122 char *file = NULL;
4123 src_get(&num, &file);
4124 tline->text = nasm_quote(file, strlen(file));
4125 tline->type = TOK_STRING;
4126 nasm_free(file);
H. Peter Anvine2c80182005-01-15 22:15:51 +00004127 continue;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004128 }
4129 if (!strcmp("__LINE__", m->name)) {
4130 nasm_free(tline->text);
4131 make_tok_num(tline, src_get_linnum());
4132 continue;
4133 }
4134 if (!strcmp("__BITS__", m->name)) {
4135 nasm_free(tline->text);
4136 make_tok_num(tline, globalbits);
4137 continue;
4138 }
4139 tline = delete_Token(tline);
4140 continue;
4141 }
4142 } else {
4143 /*
4144 * Complicated case: at least one macro with this name
H. Peter Anvine2c80182005-01-15 22:15:51 +00004145 * exists and takes parameters. We must find the
4146 * parameters in the call, count them, find the SMacro
4147 * that corresponds to that form of the macro call, and
4148 * substitute for the parameters when we expand. What a
4149 * pain.
4150 */
4151 /*tline = tline->next;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004152 skip_white_(tline); */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004153 do {
4154 t = tline->next;
4155 while (tok_type_(t, TOK_SMAC_END)) {
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004156 t->a.mac->in_progress = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004157 t->text = NULL;
4158 t = tline->next = delete_Token(t);
4159 }
4160 tline = t;
4161 } while (tok_type_(tline, TOK_WHITESPACE));
4162 if (!tok_is_(tline, "(")) {
4163 /*
4164 * This macro wasn't called with parameters: ignore
4165 * the call. (Behaviour borrowed from gnu cpp.)
4166 */
4167 tline = mstart;
4168 m = NULL;
4169 } else {
4170 int paren = 0;
4171 int white = 0;
4172 brackets = 0;
4173 nparam = 0;
4174 sparam = PARAM_DELTA;
4175 params = nasm_malloc(sparam * sizeof(Token *));
4176 params[0] = tline->next;
4177 paramsize = nasm_malloc(sparam * sizeof(int));
4178 paramsize[0] = 0;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07004179 while (true) { /* parameter loop */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004180 /*
4181 * For some unusual expansions
4182 * which concatenates function call
4183 */
4184 t = tline->next;
4185 while (tok_type_(t, TOK_SMAC_END)) {
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004186 t->a.mac->in_progress = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004187 t->text = NULL;
4188 t = tline->next = delete_Token(t);
4189 }
4190 tline = t;
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00004191
H. Peter Anvine2c80182005-01-15 22:15:51 +00004192 if (!tline) {
H. Peter Anvin215186f2016-02-17 20:27:41 -08004193 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00004194 "macro call expects terminating `)'");
4195 break;
4196 }
4197 if (tline->type == TOK_WHITESPACE
4198 && brackets <= 0) {
4199 if (paramsize[nparam])
4200 white++;
4201 else
4202 params[nparam] = tline->next;
4203 continue; /* parameter loop */
4204 }
4205 if (tline->type == TOK_OTHER
4206 && tline->text[1] == 0) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004207 char ch = tline->text[0];
H. Peter Anvine2c80182005-01-15 22:15:51 +00004208 if (ch == ',' && !paren && brackets <= 0) {
4209 if (++nparam >= sparam) {
4210 sparam += PARAM_DELTA;
4211 params = nasm_realloc(params,
Cyrill Gorcunoved4a8052010-04-09 15:40:35 +04004212 sparam * sizeof(Token *));
4213 paramsize = nasm_realloc(paramsize,
4214 sparam * sizeof(int));
H. Peter Anvine2c80182005-01-15 22:15:51 +00004215 }
4216 params[nparam] = tline->next;
4217 paramsize[nparam] = 0;
4218 white = 0;
4219 continue; /* parameter loop */
4220 }
4221 if (ch == '{' &&
4222 (brackets > 0 || (brackets == 0 &&
4223 !paramsize[nparam])))
4224 {
4225 if (!(brackets++)) {
4226 params[nparam] = tline->next;
4227 continue; /* parameter loop */
4228 }
4229 }
4230 if (ch == '}' && brackets > 0)
4231 if (--brackets == 0) {
4232 brackets = -1;
4233 continue; /* parameter loop */
4234 }
4235 if (ch == '(' && !brackets)
4236 paren++;
4237 if (ch == ')' && brackets <= 0)
4238 if (--paren < 0)
4239 break;
4240 }
4241 if (brackets < 0) {
4242 brackets = 0;
H. Peter Anvin215186f2016-02-17 20:27:41 -08004243 nasm_error(ERR_NONFATAL, "braces do not "
H. Peter Anvine2c80182005-01-15 22:15:51 +00004244 "enclose all of macro parameter");
4245 }
4246 paramsize[nparam] += white + 1;
4247 white = 0;
4248 } /* parameter loop */
4249 nparam++;
4250 while (m && (m->nparam != nparam ||
4251 mstrcmp(m->name, mname,
4252 m->casesense)))
4253 m = m->next;
4254 if (!m)
H. Peter Anvin215186f2016-02-17 20:27:41 -08004255 nasm_error(ERR_WARNING|ERR_PASS1|ERR_WARN_MNP,
H. Peter Anvine2c80182005-01-15 22:15:51 +00004256 "macro `%s' exists, "
4257 "but not taking %d parameters",
4258 mstart->text, nparam);
4259 }
4260 }
4261 if (m && m->in_progress)
4262 m = NULL;
4263 if (!m) { /* in progess or didn't find '(' or wrong nparam */
H. Peter Anvin70653092007-10-19 14:42:29 -07004264 /*
H. Peter Anvine2c80182005-01-15 22:15:51 +00004265 * Design question: should we handle !tline, which
4266 * indicates missing ')' here, or expand those
4267 * macros anyway, which requires the (t) test a few
H. Peter Anvin70653092007-10-19 14:42:29 -07004268 * lines down?
H. Peter Anvine2c80182005-01-15 22:15:51 +00004269 */
4270 nasm_free(params);
4271 nasm_free(paramsize);
4272 tline = mstart;
4273 } else {
4274 /*
4275 * Expand the macro: we are placed on the last token of the
4276 * call, so that we can easily split the call from the
4277 * following tokens. We also start by pushing an SMAC_END
4278 * token for the cycle removal.
4279 */
4280 t = tline;
4281 if (t) {
4282 tline = t->next;
4283 t->next = NULL;
4284 }
4285 tt = new_Token(tline, TOK_SMAC_END, NULL, 0);
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004286 tt->a.mac = m;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07004287 m->in_progress = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004288 tline = tt;
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04004289 list_for_each(t, m->expansion) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004290 if (t->type >= TOK_SMAC_PARAM) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00004291 Token *pcopy = tline, **ptail = &pcopy;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004292 Token *ttt, *pt;
4293 int i;
H. Peter Anvin734b1882002-04-30 21:01:08 +00004294
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004295 ttt = params[t->type - TOK_SMAC_PARAM];
4296 i = paramsize[t->type - TOK_SMAC_PARAM];
4297 while (--i >= 0) {
4298 pt = *ptail = new_Token(tline, ttt->type,
4299 ttt->text, 0);
4300 ptail = &pt->next;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004301 ttt = ttt->next;
4302 }
4303 tline = pcopy;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004304 } else if (t->type == TOK_PREPROC_Q) {
4305 tt = new_Token(tline, TOK_ID, mname, 0);
4306 tline = tt;
4307 } else if (t->type == TOK_PREPROC_QQ) {
4308 tt = new_Token(tline, TOK_ID, m->name, 0);
4309 tline = tt;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004310 } else {
4311 tt = new_Token(tline, t->type, t->text, 0);
4312 tline = tt;
4313 }
4314 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00004315
H. Peter Anvine2c80182005-01-15 22:15:51 +00004316 /*
4317 * Having done that, get rid of the macro call, and clean
4318 * up the parameters.
4319 */
4320 nasm_free(params);
4321 nasm_free(paramsize);
4322 free_tlist(mstart);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004323 expanded = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004324 continue; /* main token loop */
4325 }
4326 }
4327 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004328
H. Peter Anvine2c80182005-01-15 22:15:51 +00004329 if (tline->type == TOK_SMAC_END) {
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004330 tline->a.mac->in_progress = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004331 tline = delete_Token(tline);
4332 } else {
4333 t = *tail = tline;
4334 tline = tline->next;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004335 t->a.mac = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004336 t->next = NULL;
4337 tail = &t->next;
4338 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004339 }
4340
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004341 /*
4342 * Now scan the entire line and look for successive TOK_IDs that resulted
Keith Kaniosb7a89542007-04-12 02:40:54 +00004343 * after expansion (they can't be produced by tokenize()). The successive
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004344 * TOK_IDs should be concatenated.
4345 * Also we look for %+ tokens and concatenate the tokens before and after
4346 * them (without white spaces in between).
4347 */
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +04004348 if (expanded) {
Cyrill Gorcunovc6a742c2011-06-27 01:23:09 +04004349 const struct tokseq_match t[] = {
4350 {
4351 PP_CONCAT_MASK(TOK_ID) |
4352 PP_CONCAT_MASK(TOK_PREPROC_ID), /* head */
4353 PP_CONCAT_MASK(TOK_ID) |
4354 PP_CONCAT_MASK(TOK_PREPROC_ID) |
4355 PP_CONCAT_MASK(TOK_NUMBER) /* tail */
4356 }
4357 };
4358 if (paste_tokens(&thead, t, ARRAY_SIZE(t), true)) {
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +04004359 /*
4360 * If we concatenated something, *and* we had previously expanded
4361 * an actual macro, scan the lines again for macros...
4362 */
4363 tline = thead;
4364 expanded = false;
4365 goto again;
4366 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00004367 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004368
Cyrill Gorcunovbd38c8f2009-11-21 11:11:23 +03004369err:
H. Peter Anvine2c80182005-01-15 22:15:51 +00004370 if (org_tline) {
4371 if (thead) {
4372 *org_tline = *thead;
4373 /* since we just gave text to org_line, don't free it */
4374 thead->text = NULL;
4375 delete_Token(thead);
4376 } else {
4377 /* the expression expanded to empty line;
4378 we can't return NULL for some reasons
4379 we just set the line to a single WHITESPACE token. */
4380 memset(org_tline, 0, sizeof(*org_tline));
4381 org_tline->text = NULL;
4382 org_tline->type = TOK_WHITESPACE;
4383 }
4384 thead = org_tline;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004385 }
4386
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004387 return thead;
4388}
4389
4390/*
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004391 * Similar to expand_smacro but used exclusively with macro identifiers
4392 * right before they are fetched in. The reason is that there can be
4393 * identifiers consisting of several subparts. We consider that if there
4394 * are more than one element forming the name, user wants a expansion,
4395 * otherwise it will be left as-is. Example:
4396 *
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004397 * %define %$abc cde
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004398 *
4399 * the identifier %$abc will be left as-is so that the handler for %define
4400 * will suck it and define the corresponding value. Other case:
4401 *
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004402 * %define _%$abc cde
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004403 *
4404 * In this case user wants name to be expanded *before* %define starts
4405 * working, so we'll expand %$abc into something (if it has a value;
4406 * otherwise it will be left as-is) then concatenate all successive
4407 * PP_IDs into one.
4408 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004409static Token *expand_id(Token * tline)
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004410{
4411 Token *cur, *oldnext = NULL;
4412
H. Peter Anvin734b1882002-04-30 21:01:08 +00004413 if (!tline || !tline->next)
H. Peter Anvine2c80182005-01-15 22:15:51 +00004414 return tline;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004415
4416 cur = tline;
4417 while (cur->next &&
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004418 (cur->next->type == TOK_ID ||
4419 cur->next->type == TOK_PREPROC_ID
4420 || cur->next->type == TOK_NUMBER))
H. Peter Anvine2c80182005-01-15 22:15:51 +00004421 cur = cur->next;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004422
4423 /* If identifier consists of just one token, don't expand */
4424 if (cur == tline)
H. Peter Anvine2c80182005-01-15 22:15:51 +00004425 return tline;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004426
H. Peter Anvine2c80182005-01-15 22:15:51 +00004427 if (cur) {
4428 oldnext = cur->next; /* Detach the tail past identifier */
4429 cur->next = NULL; /* so that expand_smacro stops here */
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004430 }
4431
H. Peter Anvin734b1882002-04-30 21:01:08 +00004432 tline = expand_smacro(tline);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004433
H. Peter Anvine2c80182005-01-15 22:15:51 +00004434 if (cur) {
4435 /* expand_smacro possibly changhed tline; re-scan for EOL */
4436 cur = tline;
4437 while (cur && cur->next)
4438 cur = cur->next;
4439 if (cur)
4440 cur->next = oldnext;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004441 }
4442
4443 return tline;
4444}
4445
4446/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004447 * Determine whether the given line constitutes a multi-line macro
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004448 * call, and return the MMacro structure called if so. Doesn't have
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004449 * to check for an initial label - that's taken care of in
4450 * expand_mmacro - but must check numbers of parameters. Guaranteed
4451 * to be called with tline->type == TOK_ID, so the putative macro
4452 * name is easy to find.
4453 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004454static MMacro *is_mmacro(Token * tline, Token *** params_array)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004455{
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004456 MMacro *head, *m;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004457 Token **params;
4458 int nparam;
4459
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004460 head = (MMacro *) hash_findix(&mmacros, tline->text);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004461
4462 /*
4463 * Efficiency: first we see if any macro exists with the given
4464 * name. If not, we can return NULL immediately. _Then_ we
4465 * count the parameters, and then we look further along the
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004466 * list if necessary to find the proper MMacro.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004467 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004468 list_for_each(m, head)
4469 if (!mstrcmp(m->name, tline->text, m->casesense))
H. Peter Anvine2c80182005-01-15 22:15:51 +00004470 break;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004471 if (!m)
H. Peter Anvine2c80182005-01-15 22:15:51 +00004472 return NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004473
4474 /*
4475 * OK, we have a potential macro. Count and demarcate the
4476 * parameters.
4477 */
H. Peter Anvin734b1882002-04-30 21:01:08 +00004478 count_mmac_params(tline->next, &nparam, &params);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004479
4480 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004481 * So we know how many parameters we've got. Find the MMacro
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004482 * structure that handles this number.
4483 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004484 while (m) {
4485 if (m->nparam_min <= nparam
4486 && (m->plus || nparam <= m->nparam_max)) {
4487 /*
4488 * This one is right. Just check if cycle removal
4489 * prohibits us using it before we actually celebrate...
4490 */
4491 if (m->in_progress > m->max_depth) {
4492 if (m->max_depth > 0) {
H. Peter Anvin215186f2016-02-17 20:27:41 -08004493 nasm_error(ERR_WARNING,
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004494 "reached maximum recursion depth of %i",
4495 m->max_depth);
4496 }
4497 nasm_free(params);
4498 return NULL;
4499 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00004500 /*
4501 * It's right, and we can use it. Add its default
4502 * parameters to the end of our list if necessary.
4503 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004504 if (m->defaults && nparam < m->nparam_min + m->ndefs) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00004505 params =
4506 nasm_realloc(params,
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004507 ((m->nparam_min + m->ndefs +
H. Peter Anvine2c80182005-01-15 22:15:51 +00004508 1) * sizeof(*params)));
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004509 while (nparam < m->nparam_min + m->ndefs) {
4510 params[nparam] = m->defaults[nparam - m->nparam_min];
H. Peter Anvine2c80182005-01-15 22:15:51 +00004511 nparam++;
4512 }
4513 }
4514 /*
4515 * If we've gone over the maximum parameter count (and
4516 * we're in Plus mode), ignore parameters beyond
4517 * nparam_max.
4518 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004519 if (m->plus && nparam > m->nparam_max)
4520 nparam = m->nparam_max;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004521 /*
4522 * Then terminate the parameter list, and leave.
4523 */
4524 if (!params) { /* need this special case */
4525 params = nasm_malloc(sizeof(*params));
4526 nparam = 0;
4527 }
4528 params[nparam] = NULL;
4529 *params_array = params;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004530 return m;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004531 }
4532 /*
4533 * This one wasn't right: look for the next one with the
4534 * same name.
4535 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004536 list_for_each(m, m->next)
4537 if (!mstrcmp(m->name, tline->text, m->casesense))
H. Peter Anvine2c80182005-01-15 22:15:51 +00004538 break;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004539 }
4540
4541 /*
4542 * After all that, we didn't find one with the right number of
4543 * parameters. Issue a warning, and fail to expand the macro.
4544 */
H. Peter Anvin215186f2016-02-17 20:27:41 -08004545 nasm_error(ERR_WARNING|ERR_PASS1|ERR_WARN_MNP,
H. Peter Anvine2c80182005-01-15 22:15:51 +00004546 "macro `%s' exists, but not taking %d parameters",
4547 tline->text, nparam);
H. Peter Anvin734b1882002-04-30 21:01:08 +00004548 nasm_free(params);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004549 return NULL;
4550}
4551
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004552
4553/*
4554 * Save MMacro invocation specific fields in
4555 * preparation for a recursive macro expansion
4556 */
4557static void push_mmacro(MMacro *m)
4558{
4559 MMacroInvocation *i;
4560
4561 i = nasm_malloc(sizeof(MMacroInvocation));
4562 i->prev = m->prev;
4563 i->params = m->params;
4564 i->iline = m->iline;
4565 i->nparam = m->nparam;
4566 i->rotate = m->rotate;
4567 i->paramlen = m->paramlen;
4568 i->unique = m->unique;
4569 i->condcnt = m->condcnt;
4570 m->prev = i;
4571}
4572
4573
4574/*
4575 * Restore MMacro invocation specific fields that were
4576 * saved during a previous recursive macro expansion
4577 */
4578static void pop_mmacro(MMacro *m)
4579{
4580 MMacroInvocation *i;
4581
4582 if (m->prev) {
4583 i = m->prev;
4584 m->prev = i->prev;
4585 m->params = i->params;
4586 m->iline = i->iline;
4587 m->nparam = i->nparam;
4588 m->rotate = i->rotate;
4589 m->paramlen = i->paramlen;
4590 m->unique = i->unique;
4591 m->condcnt = i->condcnt;
4592 nasm_free(i);
4593 }
4594}
4595
4596
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004597/*
4598 * Expand the multi-line macro call made by the given line, if
4599 * there is one to be expanded. If there is, push the expansion on
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004600 * istk->expansion and return 1. Otherwise return 0.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004601 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004602static int expand_mmacro(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004603{
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004604 Token *startline = tline;
H. Peter Anvineba20a72002-04-30 20:53:55 +00004605 Token *label = NULL;
4606 int dont_prepend = 0;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004607 Token **params, *t, *tt;
4608 MMacro *m;
4609 Line *l, *ll;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004610 int i, nparam, *paramlen;
H. Peter Anvinc751e862008-06-09 10:18:45 -07004611 const char *mname;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004612
4613 t = tline;
H. Peter Anvineba20a72002-04-30 20:53:55 +00004614 skip_white_(t);
H. Peter Anvince2233b2008-05-25 21:57:00 -07004615 /* if (!tok_type_(t, TOK_ID)) Lino 02/25/02 */
H. Peter Anvindce1e2f2002-04-30 21:06:37 +00004616 if (!tok_type_(t, TOK_ID) && !tok_type_(t, TOK_PREPROC_ID))
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004617 return 0;
4618 m = is_mmacro(t, &params);
4619 if (m) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004620 mname = t->text;
H. Peter Anvinc751e862008-06-09 10:18:45 -07004621 } else {
H. Peter Anvine2c80182005-01-15 22:15:51 +00004622 Token *last;
4623 /*
4624 * We have an id which isn't a macro call. We'll assume
4625 * it might be a label; we'll also check to see if a
4626 * colon follows it. Then, if there's another id after
4627 * that lot, we'll check it again for macro-hood.
4628 */
4629 label = last = t;
4630 t = t->next;
4631 if (tok_type_(t, TOK_WHITESPACE))
4632 last = t, t = t->next;
4633 if (tok_is_(t, ":")) {
4634 dont_prepend = 1;
4635 last = t, t = t->next;
4636 if (tok_type_(t, TOK_WHITESPACE))
4637 last = t, t = t->next;
4638 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004639 if (!tok_type_(t, TOK_ID) || !(m = is_mmacro(t, &params)))
4640 return 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004641 last->next = NULL;
Keith Kanios891775e2009-07-11 06:08:54 -05004642 mname = t->text;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004643 tline = t;
H. Peter Anvineba20a72002-04-30 20:53:55 +00004644 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004645
4646 /*
4647 * Fix up the parameters: this involves stripping leading and
4648 * trailing whitespace, then stripping braces if they are
4649 * present.
4650 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004651 for (nparam = 0; params[nparam]; nparam++) ;
H. Peter Anvin734b1882002-04-30 21:01:08 +00004652 paramlen = nparam ? nasm_malloc(nparam * sizeof(*paramlen)) : NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004653
H. Peter Anvine2c80182005-01-15 22:15:51 +00004654 for (i = 0; params[i]; i++) {
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08004655 int brace = 0;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004656 int comma = (!m->plus || i < nparam - 1);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004657
H. Peter Anvine2c80182005-01-15 22:15:51 +00004658 t = params[i];
4659 skip_white_(t);
4660 if (tok_is_(t, "{"))
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08004661 t = t->next, brace++, comma = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004662 params[i] = t;
4663 paramlen[i] = 0;
4664 while (t) {
4665 if (comma && t->type == TOK_OTHER && !strcmp(t->text, ","))
4666 break; /* ... because we have hit a comma */
4667 if (comma && t->type == TOK_WHITESPACE
4668 && tok_is_(t->next, ","))
4669 break; /* ... or a space then a comma */
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08004670 if (brace && t->type == TOK_OTHER) {
4671 if (t->text[0] == '{')
4672 brace++; /* ... or a nested opening brace */
4673 else if (t->text[0] == '}')
4674 if (!--brace)
4675 break; /* ... or a brace */
4676 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00004677 t = t->next;
4678 paramlen[i]++;
4679 }
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08004680 if (brace)
H. Peter Anvin215186f2016-02-17 20:27:41 -08004681 nasm_error(ERR_NONFATAL, "macro params should be enclosed in braces");
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004682 }
4683
4684 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004685 * OK, we have a MMacro structure together with a set of
4686 * parameters. We must now go through the expansion and push
4687 * copies of each Line on to istk->expansion. Substitution of
H. Peter Anvin76690a12002-04-30 20:52:49 +00004688 * parameter tokens and macro-local tokens doesn't get done
4689 * until the single-line macro substitution process; this is
4690 * because delaying them allows us to change the semantics
4691 * later through %rotate.
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004692 *
4693 * First, push an end marker on to istk->expansion, mark this
4694 * macro as in progress, and set up its invocation-specific
4695 * variables.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004696 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004697 ll = nasm_malloc(sizeof(Line));
4698 ll->next = istk->expansion;
4699 ll->finishes = m;
4700 ll->first = NULL;
4701 istk->expansion = ll;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004702
4703 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004704 * Save the previous MMacro expansion in the case of
4705 * macro recursion
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004706 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004707 if (m->max_depth && m->in_progress)
4708 push_mmacro(m);
4709
4710 m->in_progress ++;
4711 m->params = params;
4712 m->iline = tline;
4713 m->nparam = nparam;
4714 m->rotate = 0;
4715 m->paramlen = paramlen;
4716 m->unique = unique++;
4717 m->lineno = 0;
4718 m->condcnt = 0;
4719
4720 m->next_active = istk->mstk;
4721 istk->mstk = m;
4722
4723 list_for_each(l, m->expansion) {
4724 Token **tail;
4725
4726 ll = nasm_malloc(sizeof(Line));
4727 ll->finishes = NULL;
4728 ll->next = istk->expansion;
4729 istk->expansion = ll;
4730 tail = &ll->first;
4731
4732 list_for_each(t, l->first) {
4733 Token *x = t;
4734 switch (t->type) {
4735 case TOK_PREPROC_Q:
4736 tt = *tail = new_Token(NULL, TOK_ID, mname, 0);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004737 break;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004738 case TOK_PREPROC_QQ:
4739 tt = *tail = new_Token(NULL, TOK_ID, m->name, 0);
4740 break;
4741 case TOK_PREPROC_ID:
4742 if (t->text[1] == '0' && t->text[2] == '0') {
4743 dont_prepend = -1;
4744 x = label;
4745 if (!x)
4746 continue;
4747 }
4748 /* fall through */
4749 default:
4750 tt = *tail = new_Token(NULL, x->type, x->text, 0);
4751 break;
4752 }
4753 tail = &tt->next;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004754 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004755 *tail = NULL;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004756 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004757
4758 /*
H. Peter Anvineba20a72002-04-30 20:53:55 +00004759 * If we had a label, push it on as the first line of
4760 * the macro expansion.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004761 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004762 if (label) {
4763 if (dont_prepend < 0)
4764 free_tlist(startline);
4765 else {
4766 ll = nasm_malloc(sizeof(Line));
4767 ll->finishes = NULL;
4768 ll->next = istk->expansion;
4769 istk->expansion = ll;
4770 ll->first = startline;
4771 if (!dont_prepend) {
4772 while (label->next)
4773 label = label->next;
4774 label->next = tt = new_Token(NULL, TOK_OTHER, ":", 0);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004775 }
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004776 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004777 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004778
H. Peter Anvin172b8402016-02-18 01:16:18 -08004779 lfmt->uplevel(m->nolist ? LIST_MACRO_NOLIST : LIST_MACRO);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004780
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004781 return 1;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004782}
4783
H. Peter Anvin215186f2016-02-17 20:27:41 -08004784/*
4785 * This function adds macro names to error messages, and suppresses
4786 * them if necessary.
4787 */
4788static void pp_verror(int severity, const char *fmt, va_list arg)
Victor van den Elzen3b404c02008-09-18 13:51:36 +02004789{
H. Peter Anvin215186f2016-02-17 20:27:41 -08004790 char buff[BUFSIZ];
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004791 MMacro *mmac = NULL;
4792 int delta = 0;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02004793
H. Peter Anvin215186f2016-02-17 20:27:41 -08004794 /*
4795 * If we're in a dead branch of IF or something like it, ignore the error.
4796 * However, because %else etc are evaluated in the state context
4797 * of the previous branch, errors might get lost:
4798 * %if 0 ... %else trailing garbage ... %endif
4799 * So %else etc should set the ERR_PP_PRECOND flag.
4800 */
4801 if ((severity & ERR_MASK) < ERR_FATAL &&
4802 istk && istk->conds &&
4803 ((severity & ERR_PP_PRECOND) ?
4804 istk->conds->state == COND_NEVER :
H. Peter Anvineb6653f2016-04-05 13:03:10 -07004805 !emitting(istk->conds->state)))
H. Peter Anvin215186f2016-02-17 20:27:41 -08004806 return;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02004807
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004808 /* get %macro name */
H. Peter Anvin215186f2016-02-17 20:27:41 -08004809 if (!(severity & ERR_NOFILE) && istk && istk->mstk) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004810 mmac = istk->mstk;
4811 /* but %rep blocks should be skipped */
4812 while (mmac && !mmac->name)
4813 mmac = mmac->next_active, delta++;
Cyrill Gorcunov9900c6b2011-10-09 18:58:46 +04004814 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004815
H. Peter Anvin215186f2016-02-17 20:27:41 -08004816 if (mmac) {
4817 vsnprintf(buff, sizeof(buff), fmt, arg);
Victor van den Elzen3b404c02008-09-18 13:51:36 +02004818
H. Peter Anvin215186f2016-02-17 20:27:41 -08004819 nasm_set_verror(real_verror);
4820 nasm_error(severity, "(%s:%d) %s",
4821 mmac->name, mmac->lineno - delta, buff);
4822 nasm_set_verror(pp_verror);
4823 } else {
4824 real_verror(severity, fmt, arg);
4825 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004826}
4827
H. Peter Anvin734b1882002-04-30 21:01:08 +00004828static void
H. Peter Anvin215186f2016-02-17 20:27:41 -08004829pp_reset(char *file, int apass, StrList **deplist)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004830{
H. Peter Anvin7383b402008-09-24 10:20:40 -07004831 Token *t;
4832
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004833 cstk = NULL;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004834 istk = nasm_malloc(sizeof(Include));
4835 istk->next = NULL;
4836 istk->conds = NULL;
4837 istk->expansion = NULL;
4838 istk->mstk = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004839 istk->fp = fopen(file, "r");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004840 istk->fname = NULL;
H. Peter Anvineba20a72002-04-30 20:53:55 +00004841 src_set_fname(nasm_strdup(file));
4842 src_set_linnum(0);
4843 istk->lineinc = 1;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004844 if (!istk->fp)
H. Peter Anvin215186f2016-02-17 20:27:41 -08004845 nasm_fatal(ERR_NOFILE, "unable to open input file `%s'", file);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004846 defining = NULL;
Charles Crayned4200be2008-07-12 16:42:33 -07004847 nested_mac_count = 0;
4848 nested_rep_count = 0;
H. Peter Anvin97a23472007-09-16 17:57:25 -07004849 init_macros();
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004850 unique = 0;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004851 if (tasm_compatible_mode) {
H. Peter Anvina4835d42008-05-20 14:21:29 -07004852 stdmacpos = nasm_stdmac;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004853 } else {
H. Peter Anvina4835d42008-05-20 14:21:29 -07004854 stdmacpos = nasm_stdmac_after_tasm;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004855 }
H. Peter Anvind2456592008-06-19 15:04:18 -07004856 any_extrastdmac = extrastdmac && *extrastdmac;
4857 do_predef = true;
H. Peter Anvin61f130f2008-09-25 15:45:06 -07004858
4859 /*
4860 * 0 for dependencies, 1 for preparatory passes, 2 for final pass.
4861 * The caller, however, will also pass in 3 for preprocess-only so
4862 * we can set __PASS__ accordingly.
4863 */
4864 pass = apass > 2 ? 2 : apass;
4865
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07004866 dephead = deptail = deplist;
4867 if (deplist) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004868 StrList *sl = nasm_malloc(strlen(file)+1+sizeof sl->next);
4869 sl->next = NULL;
4870 strcpy(sl->str, file);
4871 *deptail = sl;
4872 deptail = &sl->next;
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07004873 }
H. Peter Anvin7383b402008-09-24 10:20:40 -07004874
H. Peter Anvin61f130f2008-09-25 15:45:06 -07004875 /*
4876 * Define the __PASS__ macro. This is defined here unlike
4877 * all the other builtins, because it is special -- it varies between
4878 * passes.
4879 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004880 t = nasm_malloc(sizeof(*t));
4881 t->next = NULL;
H. Peter Anvin61f130f2008-09-25 15:45:06 -07004882 make_tok_num(t, apass);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004883 t->a.mac = NULL;
H. Peter Anvin7383b402008-09-24 10:20:40 -07004884 define_smacro(NULL, "__PASS__", true, 0, t);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004885}
4886
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004887static char *pp_getline(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004888{
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004889 char *line;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004890 Token *tline;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004891
H. Peter Anvin215186f2016-02-17 20:27:41 -08004892 real_verror = nasm_set_verror(pp_verror);
4893
H. Peter Anvine2c80182005-01-15 22:15:51 +00004894 while (1) {
4895 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004896 * Fetch a tokenized line, either from the macro-expansion
H. Peter Anvine2c80182005-01-15 22:15:51 +00004897 * buffer or from the input file.
4898 */
4899 tline = NULL;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004900 while (istk->expansion && istk->expansion->finishes) {
4901 Line *l = istk->expansion;
4902 if (!l->finishes->name && l->finishes->in_progress > 1) {
4903 Line *ll;
H. Peter Anvineba20a72002-04-30 20:53:55 +00004904
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004905 /*
4906 * This is a macro-end marker for a macro with no
4907 * name, which means it's not really a macro at all
4908 * but a %rep block, and the `in_progress' field is
4909 * more than 1, meaning that we still need to
4910 * repeat. (1 means the natural last repetition; 0
4911 * means termination by %exitrep.) We have
4912 * therefore expanded up to the %endrep, and must
4913 * push the whole block on to the expansion buffer
4914 * again. We don't bother to remove the macro-end
4915 * marker: we'd only have to generate another one
4916 * if we did.
4917 */
4918 l->finishes->in_progress--;
4919 list_for_each(l, l->finishes->expansion) {
4920 Token *t, *tt, **tail;
4921
4922 ll = nasm_malloc(sizeof(Line));
4923 ll->next = istk->expansion;
4924 ll->finishes = NULL;
4925 ll->first = NULL;
4926 tail = &ll->first;
4927
4928 list_for_each(t, l->first) {
4929 if (t->text || t->type == TOK_WHITESPACE) {
4930 tt = *tail = new_Token(NULL, t->type, t->text, 0);
4931 tail = &tt->next;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004932 }
4933 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004934
4935 istk->expansion = ll;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004936 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004937 } else {
4938 /*
4939 * Check whether a `%rep' was started and not ended
4940 * within this macro expansion. This can happen and
4941 * should be detected. It's a fatal error because
4942 * I'm too confused to work out how to recover
4943 * sensibly from it.
4944 */
4945 if (defining) {
4946 if (defining->name)
H. Peter Anvin215186f2016-02-17 20:27:41 -08004947 nasm_panic(0, "defining with name in expansion");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004948 else if (istk->mstk->name)
H. Peter Anvin215186f2016-02-17 20:27:41 -08004949 nasm_fatal(0, "`%%rep' without `%%endrep' within"
4950 " expansion of macro `%s'",
4951 istk->mstk->name);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004952 }
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004953
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004954 /*
4955 * FIXME: investigate the relationship at this point between
4956 * istk->mstk and l->finishes
4957 */
4958 {
4959 MMacro *m = istk->mstk;
4960 istk->mstk = m->next_active;
4961 if (m->name) {
4962 /*
4963 * This was a real macro call, not a %rep, and
4964 * therefore the parameter information needs to
4965 * be freed.
4966 */
4967 if (m->prev) {
4968 pop_mmacro(m);
4969 l->finishes->in_progress --;
4970 } else {
4971 nasm_free(m->params);
4972 free_tlist(m->iline);
4973 nasm_free(m->paramlen);
4974 l->finishes->in_progress = 0;
4975 }
4976 } else
4977 free_mmacro(m);
4978 }
4979 istk->expansion = l->next;
4980 nasm_free(l);
H. Peter Anvin172b8402016-02-18 01:16:18 -08004981 lfmt->downlevel(LIST_MACRO);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004982 }
4983 }
4984 while (1) { /* until we get a line we can use */
4985
4986 if (istk->expansion) { /* from a macro expansion */
4987 char *p;
4988 Line *l = istk->expansion;
4989 if (istk->mstk)
4990 istk->mstk->lineno++;
4991 tline = l->first;
4992 istk->expansion = l->next;
4993 nasm_free(l);
4994 p = detoken(tline, false);
H. Peter Anvin172b8402016-02-18 01:16:18 -08004995 lfmt->line(LIST_MACRO, p);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004996 nasm_free(p);
4997 break;
4998 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00004999 line = read_line();
5000 if (line) { /* from the current input file */
5001 line = prepreproc(line);
Keith Kaniosb7a89542007-04-12 02:40:54 +00005002 tline = tokenize(line);
H. Peter Anvine2c80182005-01-15 22:15:51 +00005003 nasm_free(line);
5004 break;
5005 }
5006 /*
5007 * The current file has ended; work down the istk
5008 */
5009 {
5010 Include *i = istk;
5011 fclose(i->fp);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005012 if (i->conds) {
5013 /* nasm_error can't be conditionally suppressed */
H. Peter Anvin41087062016-03-03 14:27:34 -08005014 nasm_fatal(0,
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005015 "expected `%%endif' before end of file");
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005016 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00005017 /* only set line and file name if there's a next node */
5018 if (i->next) {
5019 src_set_linnum(i->lineno);
H. Peter Anvin4def1a82016-05-09 13:59:44 -07005020 src_set_fname(nasm_strdup(i->fname));
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03005021 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00005022 istk = i->next;
H. Peter Anvin172b8402016-02-18 01:16:18 -08005023 lfmt->downlevel(LIST_INCLUDE);
H. Peter Anvine2c80182005-01-15 22:15:51 +00005024 nasm_free(i);
H. Peter Anvin215186f2016-02-17 20:27:41 -08005025 if (!istk) {
5026 line = NULL;
5027 goto done;
5028 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005029 if (istk->expansion && istk->expansion->finishes)
5030 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005031 }
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005032 }
5033
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005034 /*
5035 * We must expand MMacro parameters and MMacro-local labels
5036 * _before_ we plunge into directive processing, to cope
5037 * with things like `%define something %1' such as STRUC
5038 * uses. Unless we're _defining_ a MMacro, in which case
5039 * those tokens should be left alone to go into the
5040 * definition; and unless we're in a non-emitting
5041 * condition, in which case we don't want to meddle with
5042 * anything.
5043 */
5044 if (!defining && !(istk->conds && !emitting(istk->conds->state))
5045 && !(istk->mstk && !istk->mstk->in_progress)) {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005046 tline = expand_mmac_params(tline);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005047 }
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005048
H. Peter Anvine2c80182005-01-15 22:15:51 +00005049 /*
5050 * Check the line to see if it's a preprocessor directive.
5051 */
5052 if (do_directive(tline) == DIRECTIVE_FOUND) {
5053 continue;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005054 } else if (defining) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00005055 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005056 * We're defining a multi-line macro. We emit nothing
5057 * at all, and just
5058 * shove the tokenized line on to the macro definition.
H. Peter Anvine2c80182005-01-15 22:15:51 +00005059 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005060 Line *l = nasm_malloc(sizeof(Line));
5061 l->next = defining->expansion;
5062 l->first = tline;
5063 l->finishes = NULL;
5064 defining->expansion = l;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005065 continue;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005066 } else if (istk->conds && !emitting(istk->conds->state)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00005067 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005068 * We're in a non-emitting branch of a condition block.
H. Peter Anvine2c80182005-01-15 22:15:51 +00005069 * Emit nothing at all, not even a blank line: when we
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005070 * emerge from the condition we'll give a line-number
H. Peter Anvine2c80182005-01-15 22:15:51 +00005071 * directive so we keep our place correctly.
5072 */
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005073 free_tlist(tline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00005074 continue;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005075 } else if (istk->mstk && !istk->mstk->in_progress) {
5076 /*
5077 * We're in a %rep block which has been terminated, so
5078 * we're walking through to the %endrep without
5079 * emitting anything. Emit nothing at all, not even a
5080 * blank line: when we emerge from the %rep block we'll
5081 * give a line-number directive so we keep our place
5082 * correctly.
5083 */
5084 free_tlist(tline);
5085 continue;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005086 } else {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005087 tline = expand_smacro(tline);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005088 if (!expand_mmacro(tline)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00005089 /*
Keith Kaniosb7a89542007-04-12 02:40:54 +00005090 * De-tokenize the line again, and emit it.
H. Peter Anvine2c80182005-01-15 22:15:51 +00005091 */
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005092 line = detoken(tline, true);
5093 free_tlist(tline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00005094 break;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005095 } else {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005096 continue; /* expand_mmacro calls free_tlist */
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005097 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00005098 }
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005099 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005100
H. Peter Anvin215186f2016-02-17 20:27:41 -08005101done:
5102 nasm_set_verror(real_verror);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005103 return line;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005104}
5105
H. Peter Anvine2c80182005-01-15 22:15:51 +00005106static void pp_cleanup(int pass)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005107{
H. Peter Anvin215186f2016-02-17 20:27:41 -08005108 real_verror = nasm_set_verror(pp_verror);
5109
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005110 if (defining) {
5111 if (defining->name) {
H. Peter Anvin215186f2016-02-17 20:27:41 -08005112 nasm_error(ERR_NONFATAL,
5113 "end of file while still defining macro `%s'",
5114 defining->name);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005115 } else {
H. Peter Anvin215186f2016-02-17 20:27:41 -08005116 nasm_error(ERR_NONFATAL, "end of file while still in %%rep");
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005117 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005118
5119 free_mmacro(defining);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005120 defining = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005121 }
H. Peter Anvin215186f2016-02-17 20:27:41 -08005122
5123 nasm_set_verror(real_verror);
5124
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005125 while (cstk)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005126 ctx_pop();
H. Peter Anvin97a23472007-09-16 17:57:25 -07005127 free_macros();
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005128 while (istk) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00005129 Include *i = istk;
5130 istk = istk->next;
5131 fclose(i->fp);
5132 nasm_free(i->fname);
Cyrill Gorcunov8dcfd882011-03-03 09:18:56 +03005133 nasm_free(i);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005134 }
5135 while (cstk)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005136 ctx_pop();
H. Peter Anvin86877b22008-06-20 15:55:45 -07005137 nasm_free(src_set_fname(NULL));
H. Peter Anvine2c80182005-01-15 22:15:51 +00005138 if (pass == 0) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03005139 IncPath *i;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005140 free_llist(predef);
Cyrill Gorcunovdae24d72014-06-28 10:17:39 +04005141 predef = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005142 delete_Blocks();
Cyrill Gorcunovdae24d72014-06-28 10:17:39 +04005143 freeTokens = NULL;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03005144 while ((i = ipath)) {
5145 ipath = i->next;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005146 if (i->path)
5147 nasm_free(i->path);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03005148 nasm_free(i);
5149 }
H. Peter Anvin11dfa1a2008-07-02 18:11:04 -07005150 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005151}
5152
Cyrill Gorcunov0b78bff2012-05-07 01:57:55 +04005153static void pp_include_path(char *path)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005154{
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005155 IncPath *i;
H. Peter Anvin37a321f2007-09-24 13:41:58 -07005156
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005157 i = nasm_malloc(sizeof(IncPath));
5158 i->path = path ? nasm_strdup(path) : NULL;
5159 i->next = NULL;
Frank Kotlerd0ed6fd2003-08-27 11:33:56 +00005160
H. Peter Anvin89cee572009-07-15 09:16:54 -04005161 if (ipath) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00005162 IncPath *j = ipath;
H. Peter Anvin89cee572009-07-15 09:16:54 -04005163 while (j->next)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005164 j = j->next;
5165 j->next = i;
5166 } else {
5167 ipath = i;
Frank Kotlerd0ed6fd2003-08-27 11:33:56 +00005168 }
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005169}
Frank Kotlerd0ed6fd2003-08-27 11:33:56 +00005170
Cyrill Gorcunov0b78bff2012-05-07 01:57:55 +04005171static void pp_pre_include(char *fname)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005172{
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005173 Token *inc, *space, *name;
5174 Line *l;
5175
H. Peter Anvin734b1882002-04-30 21:01:08 +00005176 name = new_Token(NULL, TOK_INTERNAL_STRING, fname, 0);
5177 space = new_Token(name, TOK_WHITESPACE, NULL, 0);
5178 inc = new_Token(space, TOK_PREPROC_ID, "%include", 0);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005179
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005180 l = nasm_malloc(sizeof(Line));
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005181 l->next = predef;
5182 l->first = inc;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005183 l->finishes = NULL;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005184 predef = l;
5185}
5186
Cyrill Gorcunov0b78bff2012-05-07 01:57:55 +04005187static void pp_pre_define(char *definition)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005188{
5189 Token *def, *space;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005190 Line *l;
Keith Kaniosa6dfa782007-04-13 16:47:53 +00005191 char *equals;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005192
H. Peter Anvin215186f2016-02-17 20:27:41 -08005193 real_verror = nasm_set_verror(pp_verror);
5194
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005195 equals = strchr(definition, '=');
H. Peter Anvin734b1882002-04-30 21:01:08 +00005196 space = new_Token(NULL, TOK_WHITESPACE, NULL, 0);
5197 def = new_Token(space, TOK_PREPROC_ID, "%define", 0);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005198 if (equals)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005199 *equals = ' ';
Keith Kaniosb7a89542007-04-12 02:40:54 +00005200 space->next = tokenize(definition);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005201 if (equals)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005202 *equals = '=';
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005203
Cyrill Gorcunov6d42e9b2015-02-08 11:07:17 +03005204 if (space->next->type != TOK_PREPROC_ID &&
5205 space->next->type != TOK_ID)
H. Peter Anvin215186f2016-02-17 20:27:41 -08005206 nasm_error(ERR_WARNING, "pre-defining non ID `%s\'\n", definition);
Cyrill Gorcunov6d42e9b2015-02-08 11:07:17 +03005207
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005208 l = nasm_malloc(sizeof(Line));
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005209 l->next = predef;
5210 l->first = def;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005211 l->finishes = NULL;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005212 predef = l;
H. Peter Anvin215186f2016-02-17 20:27:41 -08005213
5214 nasm_set_verror(real_verror);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005215}
5216
Cyrill Gorcunov0b78bff2012-05-07 01:57:55 +04005217static void pp_pre_undefine(char *definition)
H. Peter Anvin620515a2002-04-30 20:57:38 +00005218{
5219 Token *def, *space;
5220 Line *l;
5221
H. Peter Anvin734b1882002-04-30 21:01:08 +00005222 space = new_Token(NULL, TOK_WHITESPACE, NULL, 0);
5223 def = new_Token(space, TOK_PREPROC_ID, "%undef", 0);
Keith Kaniosb7a89542007-04-12 02:40:54 +00005224 space->next = tokenize(definition);
H. Peter Anvin620515a2002-04-30 20:57:38 +00005225
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005226 l = nasm_malloc(sizeof(Line));
H. Peter Anvin620515a2002-04-30 20:57:38 +00005227 l->next = predef;
5228 l->first = def;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005229 l->finishes = NULL;
H. Peter Anvin620515a2002-04-30 20:57:38 +00005230 predef = l;
5231}
5232
Cyrill Gorcunov0b78bff2012-05-07 01:57:55 +04005233static void pp_extra_stdmac(macros_t *macros)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005234{
H. Peter Anvin76690a12002-04-30 20:52:49 +00005235 extrastdmac = macros;
5236}
5237
Keith Kaniosa5fc6462007-10-13 07:09:22 -07005238static void make_tok_num(Token * tok, int64_t val)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005239{
Cyrill Gorcunovce652742013-05-06 23:43:43 +04005240 char numbuf[32];
Keith Kaniosa5fc6462007-10-13 07:09:22 -07005241 snprintf(numbuf, sizeof(numbuf), "%"PRId64"", val);
H. Peter Anvineba20a72002-04-30 20:53:55 +00005242 tok->text = nasm_strdup(numbuf);
5243 tok->type = TOK_NUMBER;
5244}
5245
H. Peter Anvin37368952016-05-09 14:10:32 -07005246static void pp_list_one_macro(MMacro *m, int severity)
5247{
5248 if (!m)
5249 return;
5250
5251 /* We need to print the next_active list in reverse order */
5252 pp_list_one_macro(m->next_active, severity);
5253
5254 if (m->name && !m->nolist) {
5255 src_set_linnum(m->xline + m->lineno);
5256 src_set_fname(m->fname);
5257 nasm_error(severity, "... from macro `%s' defined here", m->name);
5258 }
5259}
5260
H. Peter Anvin4def1a82016-05-09 13:59:44 -07005261static void pp_error_list_macros(int severity)
5262{
H. Peter Anvin4def1a82016-05-09 13:59:44 -07005263 int32_t saved_line;
5264 const char *saved_fname = NULL;
5265
5266 severity |= ERR_PP_LISTMACRO | ERR_NO_SEVERITY;
5267 saved_line = src_get_linnum();
5268 saved_fname = src_get_fname();
5269
H. Peter Anvin37368952016-05-09 14:10:32 -07005270 pp_list_one_macro(istk->mstk, severity);
H. Peter Anvin4def1a82016-05-09 13:59:44 -07005271
5272 src_set_fname((char *)saved_fname);
5273 src_set_linnum(saved_line);
5274}
5275
5276const struct preproc_ops nasmpp = {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005277 pp_reset,
5278 pp_getline,
Cyrill Gorcunov0b78bff2012-05-07 01:57:55 +04005279 pp_cleanup,
5280 pp_extra_stdmac,
5281 pp_pre_define,
5282 pp_pre_undefine,
5283 pp_pre_include,
H. Peter Anvin4def1a82016-05-09 13:59:44 -07005284 pp_include_path,
5285 pp_error_list_macros,
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005286};