blob: 0c9bf2d381980b778f9308843b0875c23be22438 [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
H. Peter Anvin274cda82016-05-10 02:56:29 -0700159 const char *fname; /* File where defined */
H. Peter Anvin4def1a82016-05-09 13:59:44 -0700160 int32_t xline; /* First line in macro */
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800161};
162
163
164/* Store the definition of a multi-line macro, as defined in a
165 * previous recursive macro expansion.
166 */
167struct MMacroInvocation {
168 MMacroInvocation *prev; /* previous invocation */
169 Token **params; /* actual parameters */
170 Token *iline; /* invocation line */
171 unsigned int nparam, rotate;
172 int *paramlen;
173 uint64_t unique;
174 uint64_t condcnt;
175};
176
177
178/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000179 * The context stack is composed of a linked list of these.
180 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000181struct Context {
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800182 Context *next;
183 char *name;
184 struct hash_table localmac;
185 uint32_t number;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000186};
187
188/*
189 * This is the internal form which we break input lines up into.
190 * Typically stored in linked lists.
191 *
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000192 * Note that `type' serves a double meaning: TOK_SMAC_PARAM is not
193 * necessarily used as-is, but is intended to denote the number of
194 * the substituted parameter. So in the definition
195 *
196 * %define a(x,y) ( (x) & ~(y) )
H. Peter Anvin70653092007-10-19 14:42:29 -0700197 *
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000198 * the token representing `x' will have its type changed to
199 * TOK_SMAC_PARAM, but the one representing `y' will be
200 * TOK_SMAC_PARAM+1.
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000201 *
202 * TOK_INTERNAL_STRING is a dirty hack: it's a single string token
203 * which doesn't need quotes around it. Used in the pre-include
204 * mechanism as an alternative to trying to find a sensible type of
205 * quote to use on the filename we were passed.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000206 */
H. Peter Anvinda10e7b2007-09-12 04:18:37 +0000207enum pp_token_type {
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800208 TOK_NONE = 0, TOK_WHITESPACE, TOK_COMMENT, TOK_ID,
209 TOK_PREPROC_ID, TOK_STRING,
210 TOK_NUMBER, TOK_FLOAT, TOK_SMAC_END, TOK_OTHER,
H. Peter Anvin6c81f0a2008-05-25 21:46:17 -0700211 TOK_INTERNAL_STRING,
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800212 TOK_PREPROC_Q, TOK_PREPROC_QQ,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300213 TOK_PASTE, /* %+ */
214 TOK_INDIRECT, /* %[...] */
215 TOK_SMAC_PARAM, /* MUST BE LAST IN THE LIST!!! */
216 TOK_MAX = INT_MAX /* Keep compiler from reducing the range */
H. Peter Anvinda10e7b2007-09-12 04:18:37 +0000217};
218
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +0400219#define PP_CONCAT_MASK(x) (1 << (x))
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +0400220#define PP_CONCAT_MATCH(t, mask) (PP_CONCAT_MASK((t)->type) & mask)
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +0400221
Cyrill Gorcunov575d4282010-10-06 00:25:55 +0400222struct tokseq_match {
223 int mask_head;
224 int mask_tail;
225};
226
H. Peter Anvine2c80182005-01-15 22:15:51 +0000227struct Token {
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800228 Token *next;
229 char *text;
H. Peter Anvinf26e0972008-07-01 21:26:27 -0700230 union {
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800231 SMacro *mac; /* associated macro for TOK_SMAC_END */
232 size_t len; /* scratch length field */
233 } a; /* Auxiliary data */
234 enum pp_token_type type;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000235};
236
237/*
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800238 * Multi-line macro definitions are stored as a linked list of
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000239 * these, which is essentially a container to allow several linked
240 * lists of Tokens.
H. Peter Anvin70653092007-10-19 14:42:29 -0700241 *
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000242 * Note that in this module, linked lists are treated as stacks
243 * wherever possible. For this reason, Lines are _pushed_ on to the
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800244 * `expansion' field in MMacro structures, so that the linked list,
245 * if walked, would give the macro lines in reverse order; this
246 * means that we can walk the list when expanding a macro, and thus
247 * push the lines on to the `expansion' field in _istk_ in reverse
248 * order (so that when popped back off they are in the right
249 * order). It may seem cockeyed, and it relies on my design having
250 * an even number of steps in, but it works...
251 *
252 * Some of these structures, rather than being actual lines, are
253 * markers delimiting the end of the expansion of a given macro.
254 * This is for use in the cycle-tracking and %rep-handling code.
255 * Such structures have `finishes' non-NULL, and `first' NULL. All
256 * others have `finishes' NULL, but `first' may still be NULL if
257 * the line is blank.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000258 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000259struct Line {
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800260 Line *next;
261 MMacro *finishes;
262 Token *first;
Keith Kaniosb307a4f2010-11-06 17:41:51 -0500263};
264
265/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000266 * To handle an arbitrary level of file inclusion, we maintain a
267 * stack (ie linked list) of these things.
268 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000269struct Include {
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800270 Include *next;
271 FILE *fp;
272 Cond *conds;
273 Line *expansion;
H. Peter Anvin274cda82016-05-10 02:56:29 -0700274 const char *fname;
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800275 int lineno, lineinc;
276 MMacro *mstk; /* stack of active macros/reps */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000277};
278
279/*
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000280 * Include search path. This is simply a list of strings which get
281 * prepended, in turn, to the name of an include file, in an
282 * attempt to find the file if it's not in the current directory.
283 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000284struct IncPath {
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800285 IncPath *next;
286 char *path;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000287};
288
289/*
H. Peter 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);
631 nasm_free(m);
H. Peter Anvineba20a72002-04-30 20:53:55 +0000632}
633
634/*
H. Peter Anvin97a23472007-09-16 17:57:25 -0700635 * Free all currently defined macros, and free the hash tables
636 */
H. Peter Anvin072771e2008-05-22 13:17:51 -0700637static void free_smacro_table(struct hash_table *smt)
H. Peter Anvin97a23472007-09-16 17:57:25 -0700638{
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +0400639 SMacro *s, *tmp;
H. Peter Anvin072771e2008-05-22 13:17:51 -0700640 const char *key;
641 struct hash_tbl_node *it = NULL;
H. Peter Anvin97a23472007-09-16 17:57:25 -0700642
H. Peter Anvin072771e2008-05-22 13:17:51 -0700643 while ((s = hash_iterate(smt, &it, &key)) != NULL) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300644 nasm_free((void *)key);
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +0400645 list_for_each_safe(s, tmp, s) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300646 nasm_free(s->name);
647 free_tlist(s->expansion);
648 nasm_free(s);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300649 }
H. Peter Anvin97a23472007-09-16 17:57:25 -0700650 }
H. Peter Anvin072771e2008-05-22 13:17:51 -0700651 hash_free(smt);
652}
653
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800654static void free_mmacro_table(struct hash_table *mmt)
H. Peter Anvin072771e2008-05-22 13:17:51 -0700655{
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800656 MMacro *m, *tmp;
H. Peter Anvin072771e2008-05-22 13:17:51 -0700657 const char *key;
658 struct hash_tbl_node *it = NULL;
H. Peter Anvin97a23472007-09-16 17:57:25 -0700659
660 it = NULL;
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800661 while ((m = hash_iterate(mmt, &it, &key)) != NULL) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300662 nasm_free((void *)key);
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800663 list_for_each_safe(m ,tmp, m)
664 free_mmacro(m);
H. Peter Anvin97a23472007-09-16 17:57:25 -0700665 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800666 hash_free(mmt);
H. Peter Anvin072771e2008-05-22 13:17:51 -0700667}
668
669static void free_macros(void)
670{
H. Peter Anvin166c2472008-05-28 12:28:58 -0700671 free_smacro_table(&smacros);
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800672 free_mmacro_table(&mmacros);
H. Peter Anvin97a23472007-09-16 17:57:25 -0700673}
674
675/*
676 * Initialize the hash tables
677 */
678static void init_macros(void)
679{
H. Peter Anvin166c2472008-05-28 12:28:58 -0700680 hash_init(&smacros, HASH_LARGE);
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800681 hash_init(&mmacros, HASH_LARGE);
H. Peter Anvin97a23472007-09-16 17:57:25 -0700682}
683
684/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000685 * Pop the context stack.
686 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000687static void ctx_pop(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000688{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000689 Context *c = cstk;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000690
691 cstk = cstk->next;
H. Peter Anvin166c2472008-05-28 12:28:58 -0700692 free_smacro_table(&c->localmac);
H. Peter Anvin734b1882002-04-30 21:01:08 +0000693 nasm_free(c->name);
694 nasm_free(c);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000695}
696
H. Peter Anvin072771e2008-05-22 13:17:51 -0700697/*
698 * Search for a key in the hash index; adding it if necessary
699 * (in which case we initialize the data pointer to NULL.)
700 */
701static void **
702hash_findi_add(struct hash_table *hash, const char *str)
703{
704 struct hash_insert hi;
705 void **r;
706 char *strx;
707
708 r = hash_findi(hash, str, &hi);
709 if (r)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300710 return r;
H. Peter Anvin072771e2008-05-22 13:17:51 -0700711
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300712 strx = nasm_strdup(str); /* Use a more efficient allocator here? */
H. Peter Anvin072771e2008-05-22 13:17:51 -0700713 return hash_add(&hi, strx, NULL);
714}
715
716/*
717 * Like hash_findi, but returns the data element rather than a pointer
718 * to it. Used only when not adding a new element, hence no third
719 * argument.
720 */
721static void *
722hash_findix(struct hash_table *hash, const char *str)
723{
724 void **p;
725
726 p = hash_findi(hash, str, NULL);
727 return p ? *p : NULL;
728}
729
Cyrill Gorcunov15bdc512010-07-13 11:27:41 +0400730/*
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800731 * read line from standart macros set,
Cyrill Gorcunov15bdc512010-07-13 11:27:41 +0400732 * if there no more left -- return NULL
733 */
734static char *line_from_stdmac(void)
735{
736 unsigned char c;
737 const unsigned char *p = stdmacpos;
738 char *line, *q;
739 size_t len = 0;
740
741 if (!stdmacpos)
742 return NULL;
743
744 while ((c = *p++)) {
745 if (c >= 0x80)
746 len += pp_directives_len[c - 0x80] + 1;
747 else
748 len++;
749 }
750
751 line = nasm_malloc(len + 1);
752 q = line;
753 while ((c = *stdmacpos++)) {
754 if (c >= 0x80) {
755 memcpy(q, pp_directives[c - 0x80], pp_directives_len[c - 0x80]);
756 q += pp_directives_len[c - 0x80];
757 *q++ = ' ';
758 } else {
759 *q++ = c;
760 }
761 }
762 stdmacpos = p;
763 *q = '\0';
764
765 if (!*stdmacpos) {
766 /* This was the last of the standard macro chain... */
767 stdmacpos = NULL;
768 if (any_extrastdmac) {
769 stdmacpos = extrastdmac;
770 any_extrastdmac = false;
771 } else if (do_predef) {
772 Line *pd, *l;
773 Token *head, **tail, *t;
774
775 /*
776 * Nasty hack: here we push the contents of
777 * `predef' on to the top-level expansion stack,
778 * since this is the most convenient way to
779 * implement the pre-include and pre-define
780 * features.
781 */
782 list_for_each(pd, predef) {
783 head = NULL;
784 tail = &head;
785 list_for_each(t, pd->first) {
786 *tail = new_Token(NULL, t->type, t->text, 0);
787 tail = &(*tail)->next;
788 }
789
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800790 l = nasm_malloc(sizeof(Line));
791 l->next = istk->expansion;
792 l->first = head;
793 l->finishes = NULL;
794
795 istk->expansion = l;
Cyrill Gorcunov15bdc512010-07-13 11:27:41 +0400796 }
797 do_predef = false;
798 }
799 }
800
801 return line;
802}
803
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000804static char *read_line(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000805{
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +0400806 unsigned int size, c, next;
807 const unsigned int delta = 512;
808 const unsigned int pad = 8;
809 unsigned int nr_cont = 0;
810 bool cont = false;
811 char *buffer, *p;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000812
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +0400813 /* Standart macros set (predefined) goes first */
Cyrill Gorcunov15bdc512010-07-13 11:27:41 +0400814 p = line_from_stdmac();
815 if (p)
816 return p;
H. Peter Anvin72edbb82008-06-19 16:00:04 -0700817
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +0400818 size = delta;
819 p = buffer = nasm_malloc(size);
820
821 for (;;) {
822 c = fgetc(istk->fp);
823 if ((int)(c) == EOF) {
824 p[0] = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000825 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000826 }
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +0400827
828 switch (c) {
829 case '\r':
830 next = fgetc(istk->fp);
831 if (next != '\n')
832 ungetc(next, istk->fp);
833 if (cont) {
834 cont = false;
835 continue;
836 }
837 break;
838
839 case '\n':
840 if (cont) {
841 cont = false;
842 continue;
843 }
844 break;
845
846 case '\\':
847 next = fgetc(istk->fp);
848 ungetc(next, istk->fp);
Cyrill Gorcunov490f85e2012-12-27 20:02:17 +0400849 if (next == '\r' || next == '\n') {
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +0400850 cont = true;
851 nr_cont++;
852 continue;
853 }
854 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000855 }
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +0400856
857 if (c == '\r' || c == '\n') {
858 *p++ = 0;
859 break;
860 }
861
862 if (p >= (buffer + size - pad)) {
863 buffer = nasm_realloc(buffer, size + delta);
864 p = buffer + size - pad;
865 size += delta;
866 }
867
868 *p++ = (unsigned char)c;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000869 }
870
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +0400871 if (p == buffer) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000872 nasm_free(buffer);
873 return NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000874 }
875
Cyrill Gorcunova5aea572010-11-11 10:14:45 +0300876 src_set_linnum(src_get_linnum() + istk->lineinc +
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +0400877 (nr_cont * istk->lineinc));
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000878
879 /*
880 * Handle spurious ^Z, which may be inserted into source files
881 * by some file transfer utilities.
882 */
883 buffer[strcspn(buffer, "\032")] = '\0';
884
H. Peter Anvin172b8402016-02-18 01:16:18 -0800885 lfmt->line(LIST_READ, buffer);
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000886
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000887 return buffer;
888}
889
890/*
Keith Kaniosb7a89542007-04-12 02:40:54 +0000891 * Tokenize a line of text. This is a very simple process since we
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000892 * don't need to parse the value out of e.g. numeric tokens: we
893 * simply split one string into many.
894 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000895static Token *tokenize(char *line)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000896{
H. Peter Anvinca544db2008-10-19 19:30:11 -0700897 char c, *p = line;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +0000898 enum pp_token_type type;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000899 Token *list = NULL;
900 Token *t, **tail = &list;
901
H. Peter Anvine2c80182005-01-15 22:15:51 +0000902 while (*line) {
903 p = line;
904 if (*p == '%') {
905 p++;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300906 if (*p == '+' && !nasm_isdigit(p[1])) {
907 p++;
908 type = TOK_PASTE;
909 } else if (nasm_isdigit(*p) ||
910 ((*p == '-' || *p == '+') && nasm_isdigit(p[1]))) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000911 do {
912 p++;
913 }
H. Peter Anvinbda7a6e2008-06-21 10:23:17 -0700914 while (nasm_isdigit(*p));
H. Peter Anvine2c80182005-01-15 22:15:51 +0000915 type = TOK_PREPROC_ID;
916 } else if (*p == '{') {
917 p++;
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800918 while (*p) {
919 if (*p == '}')
920 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000921 p[-1] = *p;
922 p++;
923 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800924 if (*p != '}')
H. Peter Anvin215186f2016-02-17 20:27:41 -0800925 nasm_error(ERR_WARNING | ERR_PASS1,
926 "unterminated %%{ construct");
H. Peter Anvine2c80182005-01-15 22:15:51 +0000927 p[-1] = '\0';
928 if (*p)
929 p++;
930 type = TOK_PREPROC_ID;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300931 } else if (*p == '[') {
932 int lvl = 1;
933 line += 2; /* Skip the leading %[ */
934 p++;
935 while (lvl && (c = *p++)) {
936 switch (c) {
937 case ']':
938 lvl--;
939 break;
940 case '%':
941 if (*p == '[')
942 lvl++;
943 break;
944 case '\'':
945 case '\"':
946 case '`':
Cyrill Gorcunovc6360a72010-07-13 13:32:19 +0400947 p = nasm_skip_string(p - 1) + 1;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300948 break;
949 default:
950 break;
951 }
952 }
953 p--;
954 if (*p)
955 *p++ = '\0';
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800956 if (lvl)
H. Peter Anvin215186f2016-02-17 20:27:41 -0800957 nasm_error(ERR_NONFATAL|ERR_PASS1,
958 "unterminated %%[ construct");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300959 type = TOK_INDIRECT;
960 } else if (*p == '?') {
961 type = TOK_PREPROC_Q; /* %? */
962 p++;
963 if (*p == '?') {
964 type = TOK_PREPROC_QQ; /* %?? */
965 p++;
966 }
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +0400967 } else if (*p == '!') {
968 type = TOK_PREPROC_ID;
969 p++;
970 if (isidchar(*p)) {
971 do {
972 p++;
973 }
974 while (isidchar(*p));
975 } else if (*p == '\'' || *p == '\"' || *p == '`') {
976 p = nasm_skip_string(p);
977 if (*p)
978 p++;
979 else
H. Peter Anvin215186f2016-02-17 20:27:41 -0800980 nasm_error(ERR_NONFATAL|ERR_PASS1,
981 "unterminated %%! string");
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +0400982 } else {
983 /* %! without string or identifier */
984 type = TOK_OTHER; /* Legacy behavior... */
985 }
H. Peter Anvine2c80182005-01-15 22:15:51 +0000986 } else if (isidchar(*p) ||
987 ((*p == '!' || *p == '%' || *p == '$') &&
988 isidchar(p[1]))) {
989 do {
990 p++;
991 }
992 while (isidchar(*p));
993 type = TOK_PREPROC_ID;
994 } else {
995 type = TOK_OTHER;
996 if (*p == '%')
997 p++;
998 }
999 } else if (isidstart(*p) || (*p == '$' && isidstart(p[1]))) {
1000 type = TOK_ID;
1001 p++;
1002 while (*p && isidchar(*p))
1003 p++;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07001004 } else if (*p == '\'' || *p == '"' || *p == '`') {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001005 /*
1006 * A string token.
1007 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001008 type = TOK_STRING;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001009 p = nasm_skip_string(p);
Nickolay Yurchenkof3b3ce22003-09-21 20:38:43 +00001010
H. Peter Anvine2c80182005-01-15 22:15:51 +00001011 if (*p) {
1012 p++;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001013 } else {
H. Peter Anvin215186f2016-02-17 20:27:41 -08001014 nasm_error(ERR_WARNING|ERR_PASS1, "unterminated string");
H. Peter Anvine2c80182005-01-15 22:15:51 +00001015 /* Handling unterminated strings by UNV */
1016 /* type = -1; */
1017 }
Victor van den Elzenfb5f2512009-04-17 16:17:59 +02001018 } else if (p[0] == '$' && p[1] == '$') {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001019 type = TOK_OTHER; /* TOKEN_BASE */
Victor van den Elzenfb5f2512009-04-17 16:17:59 +02001020 p += 2;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001021 } else if (isnumstart(*p)) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001022 bool is_hex = false;
1023 bool is_float = false;
1024 bool has_e = false;
1025 char c, *r;
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001026
H. Peter Anvine2c80182005-01-15 22:15:51 +00001027 /*
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001028 * A numeric token.
H. Peter Anvine2c80182005-01-15 22:15:51 +00001029 */
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001030
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001031 if (*p == '$') {
1032 p++;
1033 is_hex = true;
1034 }
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001035
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001036 for (;;) {
1037 c = *p++;
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001038
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001039 if (!is_hex && (c == 'e' || c == 'E')) {
1040 has_e = true;
1041 if (*p == '+' || *p == '-') {
1042 /*
1043 * e can only be followed by +/- if it is either a
1044 * prefixed hex number or a floating-point number
1045 */
1046 p++;
1047 is_float = true;
1048 }
1049 } else if (c == 'H' || c == 'h' || c == 'X' || c == 'x') {
1050 is_hex = true;
1051 } else if (c == 'P' || c == 'p') {
1052 is_float = true;
1053 if (*p == '+' || *p == '-')
1054 p++;
1055 } else if (isnumchar(c) || c == '_')
1056 ; /* just advance */
1057 else if (c == '.') {
1058 /*
1059 * we need to deal with consequences of the legacy
1060 * parser, like "1.nolist" being two tokens
1061 * (TOK_NUMBER, TOK_ID) here; at least give it
1062 * a shot for now. In the future, we probably need
1063 * a flex-based scanner with proper pattern matching
1064 * to do it as well as it can be done. Nothing in
1065 * the world is going to help the person who wants
1066 * 0x123.p16 interpreted as two tokens, though.
1067 */
1068 r = p;
1069 while (*r == '_')
1070 r++;
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001071
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001072 if (nasm_isdigit(*r) || (is_hex && nasm_isxdigit(*r)) ||
1073 (!is_hex && (*r == 'e' || *r == 'E')) ||
1074 (*r == 'p' || *r == 'P')) {
1075 p = r;
1076 is_float = true;
1077 } else
1078 break; /* Terminate the token */
1079 } else
1080 break;
1081 }
1082 p--; /* Point to first character beyond number */
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001083
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001084 if (p == line+1 && *line == '$') {
1085 type = TOK_OTHER; /* TOKEN_HERE */
1086 } else {
1087 if (has_e && !is_hex) {
1088 /* 1e13 is floating-point, but 1e13h is not */
1089 is_float = true;
1090 }
H. Peter Anvind784a082009-04-20 14:01:18 -07001091
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001092 type = is_float ? TOK_FLOAT : TOK_NUMBER;
1093 }
H. Peter Anvinbda7a6e2008-06-21 10:23:17 -07001094 } else if (nasm_isspace(*p)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001095 type = TOK_WHITESPACE;
Cyrill Gorcunovf66ac7d2009-10-12 20:41:13 +04001096 p = nasm_skip_spaces(p);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001097 /*
1098 * Whitespace just before end-of-line is discarded by
1099 * pretending it's a comment; whitespace just before a
1100 * comment gets lumped into the comment.
1101 */
1102 if (!*p || *p == ';') {
1103 type = TOK_COMMENT;
1104 while (*p)
1105 p++;
1106 }
1107 } else if (*p == ';') {
1108 type = TOK_COMMENT;
1109 while (*p)
1110 p++;
1111 } else {
1112 /*
1113 * Anything else is an operator of some kind. We check
1114 * for all the double-character operators (>>, <<, //,
1115 * %%, <=, >=, ==, !=, <>, &&, ||, ^^), but anything
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001116 * else is a single-character operator.
H. Peter Anvine2c80182005-01-15 22:15:51 +00001117 */
1118 type = TOK_OTHER;
1119 if ((p[0] == '>' && p[1] == '>') ||
1120 (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++;
1131 }
1132 p++;
1133 }
Nickolay Yurchenkof3b3ce22003-09-21 20:38:43 +00001134
H. Peter Anvine2c80182005-01-15 22:15:51 +00001135 /* Handling unterminated string by UNV */
1136 /*if (type == -1)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001137 {
1138 *tail = t = new_Token(NULL, TOK_STRING, line, p-line+1);
1139 t->text[p-line] = *line;
1140 tail = &t->next;
1141 }
1142 else */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001143 if (type != TOK_COMMENT) {
1144 *tail = t = new_Token(NULL, type, line, p - line);
1145 tail = &t->next;
1146 }
1147 line = p;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001148 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001149 return list;
1150}
1151
H. Peter Anvince616072002-04-30 21:02:23 +00001152/*
1153 * this function allocates a new managed block of memory and
H. Peter Anvin70653092007-10-19 14:42:29 -07001154 * returns a pointer to the block. The managed blocks are
H. Peter Anvince616072002-04-30 21:02:23 +00001155 * deleted only all at once by the delete_Blocks function.
1156 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001157static void *new_Block(size_t size)
H. Peter Anvince616072002-04-30 21:02:23 +00001158{
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001159 Blocks *b = &blocks;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001160
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001161 /* first, get to the end of the linked list */
1162 while (b->next)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001163 b = b->next;
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001164 /* now allocate the requested chunk */
1165 b->chunk = nasm_malloc(size);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001166
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001167 /* now allocate a new block for the next request */
Cyrill Gorcunovc31767c2014-06-28 02:22:17 +04001168 b->next = nasm_zalloc(sizeof(Blocks));
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001169 return b->chunk;
H. Peter Anvince616072002-04-30 21:02:23 +00001170}
1171
1172/*
1173 * this function deletes all managed blocks of memory
1174 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001175static void delete_Blocks(void)
H. Peter Anvince616072002-04-30 21:02:23 +00001176{
H. Peter Anvine2c80182005-01-15 22:15:51 +00001177 Blocks *a, *b = &blocks;
H. Peter Anvince616072002-04-30 21:02:23 +00001178
H. Peter Anvin70653092007-10-19 14:42:29 -07001179 /*
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001180 * keep in mind that the first block, pointed to by blocks
H. Peter Anvin70653092007-10-19 14:42:29 -07001181 * is a static and not dynamically allocated, so we don't
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001182 * free it.
1183 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001184 while (b) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001185 if (b->chunk)
1186 nasm_free(b->chunk);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001187 a = b;
1188 b = b->next;
1189 if (a != &blocks)
1190 nasm_free(a);
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001191 }
Cyrill Gorcunovdae24d72014-06-28 10:17:39 +04001192 memset(&blocks, 0, sizeof(blocks));
H. Peter Anvine2c80182005-01-15 22:15:51 +00001193}
H. Peter Anvin734b1882002-04-30 21:01:08 +00001194
1195/*
H. Peter Anvin70653092007-10-19 14:42:29 -07001196 * this function creates a new Token and passes a pointer to it
H. Peter Anvin734b1882002-04-30 21:01:08 +00001197 * back to the caller. It sets the type and text elements, and
H. Peter Anvinf26e0972008-07-01 21:26:27 -07001198 * also the a.mac and next elements to NULL.
H. Peter Anvin734b1882002-04-30 21:01:08 +00001199 */
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07001200static Token *new_Token(Token * next, enum pp_token_type type,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001201 const char *text, int txtlen)
H. Peter Anvin734b1882002-04-30 21:01:08 +00001202{
1203 Token *t;
1204 int i;
1205
H. Peter Anvin89cee572009-07-15 09:16:54 -04001206 if (!freeTokens) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001207 freeTokens = (Token *) new_Block(TOKEN_BLOCKSIZE * sizeof(Token));
1208 for (i = 0; i < TOKEN_BLOCKSIZE - 1; i++)
1209 freeTokens[i].next = &freeTokens[i + 1];
1210 freeTokens[i].next = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001211 }
1212 t = freeTokens;
1213 freeTokens = t->next;
1214 t->next = next;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07001215 t->a.mac = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001216 t->type = type;
H. Peter Anvin89cee572009-07-15 09:16:54 -04001217 if (type == TOK_WHITESPACE || !text) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001218 t->text = NULL;
1219 } else {
1220 if (txtlen == 0)
1221 txtlen = strlen(text);
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07001222 t->text = nasm_malloc(txtlen+1);
1223 memcpy(t->text, text, txtlen);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001224 t->text[txtlen] = '\0';
H. Peter Anvin734b1882002-04-30 21:01:08 +00001225 }
1226 return t;
1227}
1228
H. Peter Anvine2c80182005-01-15 22:15:51 +00001229static Token *delete_Token(Token * t)
H. Peter Anvin734b1882002-04-30 21:01:08 +00001230{
1231 Token *next = t->next;
1232 nasm_free(t->text);
H. Peter Anvin788e6c12002-04-30 21:02:01 +00001233 t->next = freeTokens;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001234 freeTokens = t;
1235 return next;
1236}
1237
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001238/*
1239 * Convert a line of tokens back into text.
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001240 * If expand_locals is not zero, identifiers of the form "%$*xxx"
1241 * will be transformed into ..@ctxnum.xxx
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001242 */
H. Peter Anvin9e200162008-06-04 17:23:14 -07001243static char *detoken(Token * tlist, bool expand_locals)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001244{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001245 Token *t;
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001246 char *line, *p;
H. Peter Anvinb4daadc2008-01-21 16:31:57 -08001247 const char *q;
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001248 int len = 0;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001249
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001250 list_for_each(t, tlist) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001251 if (t->type == TOK_PREPROC_ID && t->text[1] == '!') {
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001252 char *v;
1253 char *q = t->text;
H. Peter Anvin077fb932010-07-20 14:56:30 -07001254
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001255 v = t->text + 2;
1256 if (*v == '\'' || *v == '\"' || *v == '`') {
1257 size_t len = nasm_unquote(v, NULL);
1258 size_t clen = strlen(v);
H. Peter Anvin077fb932010-07-20 14:56:30 -07001259
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001260 if (len != clen) {
H. Peter Anvin215186f2016-02-17 20:27:41 -08001261 nasm_error(ERR_NONFATAL | ERR_PASS1,
1262 "NUL character in %%! string");
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001263 v = NULL;
1264 }
1265 }
H. Peter Anvin077fb932010-07-20 14:56:30 -07001266
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001267 if (v) {
1268 char *p = getenv(v);
1269 if (!p) {
H. Peter Anvin215186f2016-02-17 20:27:41 -08001270 nasm_error(ERR_NONFATAL | ERR_PASS1,
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001271 "nonexistent environment variable `%s'", v);
1272 p = "";
1273 }
1274 t->text = nasm_strdup(p);
1275 }
1276 nasm_free(q);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001277 }
H. Peter Anvin077fb932010-07-20 14:56:30 -07001278
H. Peter Anvine2c80182005-01-15 22:15:51 +00001279 /* Expand local macros here and not during preprocessing */
1280 if (expand_locals &&
1281 t->type == TOK_PREPROC_ID && t->text &&
1282 t->text[0] == '%' && t->text[1] == '$') {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001283 const char *q;
1284 char *p;
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04001285 Context *ctx = get_ctx(t->text, &q);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001286 if (ctx) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001287 char buffer[40];
Keith Kanios93f2e9a2007-04-14 00:10:59 +00001288 snprintf(buffer, sizeof(buffer), "..@%"PRIu32".", ctx->number);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001289 p = nasm_strcat(buffer, q);
1290 nasm_free(t->text);
1291 t->text = p;
1292 }
1293 }
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001294 if (t->type == TOK_WHITESPACE)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001295 len++;
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001296 else if (t->text)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001297 len += strlen(t->text);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001298 }
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001299
H. Peter Anvin734b1882002-04-30 21:01:08 +00001300 p = line = nasm_malloc(len + 1);
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001301
1302 list_for_each(t, tlist) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001303 if (t->type == TOK_WHITESPACE) {
H. Peter Anvinb4daadc2008-01-21 16:31:57 -08001304 *p++ = ' ';
H. Peter Anvine2c80182005-01-15 22:15:51 +00001305 } else if (t->text) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001306 q = t->text;
1307 while (*q)
1308 *p++ = *q++;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001309 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001310 }
1311 *p = '\0';
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001312
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001313 return line;
1314}
1315
1316/*
H. Peter Anvin76690a12002-04-30 20:52:49 +00001317 * A scanner, suitable for use by the expression evaluator, which
1318 * operates on a line of Tokens. Expects a pointer to a pointer to
1319 * the first token in the line to be passed in as its private_data
1320 * field.
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001321 *
1322 * FIX: This really needs to be unified with stdscan.
H. Peter Anvin76690a12002-04-30 20:52:49 +00001323 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001324static int ppscan(void *private_data, struct tokenval *tokval)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001325{
H. Peter Anvin76690a12002-04-30 20:52:49 +00001326 Token **tlineptr = private_data;
1327 Token *tline;
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001328 char ourcopy[MAX_KEYWORD+1], *p, *r, *s;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001329
H. Peter Anvine2c80182005-01-15 22:15:51 +00001330 do {
1331 tline = *tlineptr;
1332 *tlineptr = tline ? tline->next : NULL;
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04001333 } while (tline && (tline->type == TOK_WHITESPACE ||
1334 tline->type == TOK_COMMENT));
H. Peter Anvin76690a12002-04-30 20:52:49 +00001335
1336 if (!tline)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001337 return tokval->t_type = TOKEN_EOS;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001338
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001339 tokval->t_charptr = tline->text;
1340
H. Peter Anvin76690a12002-04-30 20:52:49 +00001341 if (tline->text[0] == '$' && !tline->text[1])
H. Peter Anvine2c80182005-01-15 22:15:51 +00001342 return tokval->t_type = TOKEN_HERE;
H. Peter Anvin7cf897e2002-05-30 21:30:33 +00001343 if (tline->text[0] == '$' && tline->text[1] == '$' && !tline->text[2])
H. Peter Anvine2c80182005-01-15 22:15:51 +00001344 return tokval->t_type = TOKEN_BASE;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001345
H. Peter Anvine2c80182005-01-15 22:15:51 +00001346 if (tline->type == TOK_ID) {
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001347 p = tokval->t_charptr = tline->text;
1348 if (p[0] == '$') {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001349 tokval->t_charptr++;
1350 return tokval->t_type = TOKEN_ID;
1351 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00001352
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001353 for (r = p, s = ourcopy; *r; r++) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001354 if (r >= p+MAX_KEYWORD)
1355 return tokval->t_type = TOKEN_ID; /* Not a keyword */
H. Peter Anvinac8f8fc2008-06-11 15:49:41 -07001356 *s++ = nasm_tolower(*r);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001357 }
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001358 *s = '\0';
1359 /* right, so we have an identifier sitting in temp storage. now,
1360 * is it actually a register or instruction name, or what? */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001361 return nasm_token_hash(ourcopy, tokval);
H. Peter Anvin76690a12002-04-30 20:52:49 +00001362 }
1363
H. Peter Anvine2c80182005-01-15 22:15:51 +00001364 if (tline->type == TOK_NUMBER) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001365 bool rn_error;
1366 tokval->t_integer = readnum(tline->text, &rn_error);
1367 tokval->t_charptr = tline->text;
1368 if (rn_error)
1369 return tokval->t_type = TOKEN_ERRNUM;
1370 else
1371 return tokval->t_type = TOKEN_NUM;
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001372 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00001373
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001374 if (tline->type == TOK_FLOAT) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001375 return tokval->t_type = TOKEN_FLOAT;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001376 }
1377
H. Peter Anvine2c80182005-01-15 22:15:51 +00001378 if (tline->type == TOK_STRING) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001379 char bq, *ep;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001380
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001381 bq = tline->text[0];
H. Peter Anvin11627042008-06-09 20:45:19 -07001382 tokval->t_charptr = tline->text;
1383 tokval->t_inttwo = nasm_unquote(tline->text, &ep);
H. Peter Anvind2456592008-06-19 15:04:18 -07001384
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001385 if (ep[0] != bq || ep[1] != '\0')
1386 return tokval->t_type = TOKEN_ERRSTR;
1387 else
1388 return tokval->t_type = TOKEN_STR;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001389 }
1390
H. Peter Anvine2c80182005-01-15 22:15:51 +00001391 if (tline->type == TOK_OTHER) {
1392 if (!strcmp(tline->text, "<<"))
1393 return tokval->t_type = TOKEN_SHL;
1394 if (!strcmp(tline->text, ">>"))
1395 return tokval->t_type = TOKEN_SHR;
1396 if (!strcmp(tline->text, "//"))
1397 return tokval->t_type = TOKEN_SDIV;
1398 if (!strcmp(tline->text, "%%"))
1399 return tokval->t_type = TOKEN_SMOD;
1400 if (!strcmp(tline->text, "=="))
1401 return tokval->t_type = TOKEN_EQ;
1402 if (!strcmp(tline->text, "<>"))
1403 return tokval->t_type = TOKEN_NE;
1404 if (!strcmp(tline->text, "!="))
1405 return tokval->t_type = TOKEN_NE;
1406 if (!strcmp(tline->text, "<="))
1407 return tokval->t_type = TOKEN_LE;
1408 if (!strcmp(tline->text, ">="))
1409 return tokval->t_type = TOKEN_GE;
1410 if (!strcmp(tline->text, "&&"))
1411 return tokval->t_type = TOKEN_DBL_AND;
1412 if (!strcmp(tline->text, "^^"))
1413 return tokval->t_type = TOKEN_DBL_XOR;
1414 if (!strcmp(tline->text, "||"))
1415 return tokval->t_type = TOKEN_DBL_OR;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001416 }
1417
1418 /*
1419 * We have no other options: just return the first character of
1420 * the token text.
1421 */
1422 return tokval->t_type = tline->text[0];
1423}
1424
1425/*
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001426 * Compare a string to the name of an existing macro; this is a
1427 * simple wrapper which calls either strcmp or nasm_stricmp
1428 * depending on the value of the `casesense' parameter.
1429 */
H. Peter Anvin4db5a162007-10-11 13:42:09 -07001430static int mstrcmp(const char *p, const char *q, bool casesense)
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001431{
H. Peter Anvin734b1882002-04-30 21:01:08 +00001432 return casesense ? strcmp(p, q) : nasm_stricmp(p, q);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001433}
1434
1435/*
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07001436 * Compare a string to the name of an existing macro; this is a
1437 * simple wrapper which calls either strcmp or nasm_stricmp
1438 * depending on the value of the `casesense' parameter.
1439 */
1440static int mmemcmp(const char *p, const char *q, size_t l, bool casesense)
1441{
1442 return casesense ? memcmp(p, q, l) : nasm_memicmp(p, q, l);
1443}
1444
1445/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001446 * Return the Context structure associated with a %$ token. Return
1447 * NULL, having _already_ reported an error condition, if the
1448 * context stack isn't deep enough for the supplied number of $
1449 * signs.
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001450 *
1451 * If "namep" is non-NULL, set it to the pointer to the macro name
1452 * tail, i.e. the part beyond %$...
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001453 */
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04001454static Context *get_ctx(const char *name, const char **namep)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001455{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001456 Context *ctx;
1457 int i;
1458
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001459 if (namep)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001460 *namep = name;
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001461
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001462 if (!name || name[0] != '%' || name[1] != '$')
H. Peter Anvine2c80182005-01-15 22:15:51 +00001463 return NULL;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001464
H. Peter Anvine2c80182005-01-15 22:15:51 +00001465 if (!cstk) {
H. Peter Anvin215186f2016-02-17 20:27:41 -08001466 nasm_error(ERR_NONFATAL, "`%s': context stack is empty", name);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001467 return NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001468 }
1469
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001470 name += 2;
1471 ctx = cstk;
1472 i = 0;
1473 while (ctx && *name == '$') {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001474 name++;
1475 i++;
1476 ctx = ctx->next;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001477 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001478 if (!ctx) {
H. Peter Anvin215186f2016-02-17 20:27:41 -08001479 nasm_error(ERR_NONFATAL, "`%s': context stack is only"
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001480 " %d level%s deep", name, i, (i == 1 ? "" : "s"));
H. Peter Anvine2c80182005-01-15 22:15:51 +00001481 return NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001482 }
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001483
1484 if (namep)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001485 *namep = name;
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001486
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04001487 return ctx;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001488}
1489
1490/*
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001491 * Check to see if a file is already in a string list
1492 */
1493static bool in_list(const StrList *list, const char *str)
1494{
1495 while (list) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001496 if (!strcmp(list->str, str))
1497 return true;
1498 list = list->next;
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001499 }
1500 return false;
1501}
1502
1503/*
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001504 * Open an include file. This routine must always return a valid
1505 * file pointer if it returns - it's responsible for throwing an
1506 * ERR_FATAL and bombing out completely if not. It should also try
1507 * the include path one by one until it finds the file or reaches
1508 * the end of the path.
1509 */
H. Peter Anvin2b1c3b92008-06-06 10:38:46 -07001510static FILE *inc_fopen(const char *file, StrList **dhead, StrList ***dtail,
Fabian Giesen142285d2016-04-28 13:48:15 -07001511 bool missing_ok, const char *mode)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001512{
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001513 FILE *fp;
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001514 char *prefix = "";
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001515 IncPath *ip = ipath;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001516 int len = strlen(file);
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001517 size_t prefix_len = 0;
1518 StrList *sl;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001519
H. Peter Anvine2c80182005-01-15 22:15:51 +00001520 while (1) {
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001521 sl = nasm_malloc(prefix_len+len+1+sizeof sl->next);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001522 memcpy(sl->str, prefix, prefix_len);
1523 memcpy(sl->str+prefix_len, file, len+1);
Fabian Giesen142285d2016-04-28 13:48:15 -07001524 fp = fopen(sl->str, mode);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001525 if (fp && dhead && !in_list(*dhead, sl->str)) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001526 sl->next = NULL;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001527 **dtail = sl;
1528 *dtail = &sl->next;
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001529 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001530 nasm_free(sl);
1531 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001532 if (fp)
1533 return fp;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001534 if (!ip) {
1535 if (!missing_ok)
1536 break;
1537 prefix = NULL;
1538 } else {
1539 prefix = ip->path;
1540 ip = ip->next;
1541 }
1542 if (prefix) {
1543 prefix_len = strlen(prefix);
1544 } else {
1545 /* -MG given and file not found */
1546 if (dhead && !in_list(*dhead, file)) {
1547 sl = nasm_malloc(len+1+sizeof sl->next);
1548 sl->next = NULL;
1549 strcpy(sl->str, file);
1550 **dtail = sl;
1551 *dtail = &sl->next;
1552 }
1553 return NULL;
1554 }
H. Peter Anvineba20a72002-04-30 20:53:55 +00001555 }
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001556
H. Peter Anvin215186f2016-02-17 20:27:41 -08001557 nasm_error(ERR_FATAL, "unable to open include file `%s'", file);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001558 return NULL;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001559}
1560
1561/*
Fabian Giesen86d87562016-04-28 13:48:14 -07001562 * Opens an include or input file. Public version, for use by modules
1563 * that get a file:lineno pair and need to look at the file again
1564 * (e.g. the CodeView debug backend). Returns NULL on failure.
1565 */
Fabian Giesen142285d2016-04-28 13:48:15 -07001566FILE *pp_input_fopen(const char *filename, const char *mode)
Fabian Giesen86d87562016-04-28 13:48:14 -07001567{
1568 FILE *fp;
1569 StrList *xsl = NULL;
1570 StrList **xst = &xsl;
1571
Fabian Giesen142285d2016-04-28 13:48:15 -07001572 fp = inc_fopen(filename, &xsl, &xst, true, mode);
Fabian Giesen86d87562016-04-28 13:48:14 -07001573 if (xsl)
1574 nasm_free(xsl);
1575 return fp;
1576}
1577
1578/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001579 * Determine if we should warn on defining a single-line macro of
H. Peter Anvinef7468f2002-04-30 20:57:59 +00001580 * name `name', with `nparam' parameters. If nparam is 0 or -1, will
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001581 * return true if _any_ single-line macro of that name is defined.
1582 * Otherwise, will return true if a single-line macro with either
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001583 * `nparam' or no parameters is defined.
1584 *
1585 * If a macro with precisely the right number of parameters is
H. Peter Anvinef7468f2002-04-30 20:57:59 +00001586 * defined, or nparam is -1, the address of the definition structure
1587 * will be returned in `defn'; otherwise NULL will be returned. If `defn'
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001588 * is NULL, no action will be taken regarding its contents, and no
1589 * error will occur.
1590 *
1591 * Note that this is also called with nparam zero to resolve
1592 * `ifdef'.
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001593 *
1594 * If you already know which context macro belongs to, you can pass
1595 * the context pointer as first parameter; if you won't but name begins
1596 * with %$ the context will be automatically computed. If all_contexts
1597 * is true, macro will be searched in outer contexts as well.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001598 */
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07001599static bool
H. Peter Anvinb2a5fda2008-06-19 21:42:42 -07001600smacro_defined(Context * ctx, const char *name, int nparam, SMacro ** defn,
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07001601 bool nocase)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001602{
H. Peter Anvin166c2472008-05-28 12:28:58 -07001603 struct hash_table *smtbl;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001604 SMacro *m;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001605
H. Peter Anvin97a23472007-09-16 17:57:25 -07001606 if (ctx) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001607 smtbl = &ctx->localmac;
H. Peter Anvin97a23472007-09-16 17:57:25 -07001608 } else if (name[0] == '%' && name[1] == '$') {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001609 if (cstk)
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04001610 ctx = get_ctx(name, &name);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001611 if (!ctx)
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001612 return false; /* got to return _something_ */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001613 smtbl = &ctx->localmac;
H. Peter Anvin97a23472007-09-16 17:57:25 -07001614 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001615 smtbl = &smacros;
H. Peter Anvin97a23472007-09-16 17:57:25 -07001616 }
H. Peter Anvin166c2472008-05-28 12:28:58 -07001617 m = (SMacro *) hash_findix(smtbl, name);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001618
H. Peter Anvine2c80182005-01-15 22:15:51 +00001619 while (m) {
1620 if (!mstrcmp(m->name, name, m->casesense && nocase) &&
Charles Crayne192d5b52007-10-18 19:02:42 -07001621 (nparam <= 0 || m->nparam == 0 || nparam == (int) m->nparam)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001622 if (defn) {
Charles Crayne192d5b52007-10-18 19:02:42 -07001623 if (nparam == (int) m->nparam || nparam == -1)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001624 *defn = m;
1625 else
1626 *defn = NULL;
1627 }
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001628 return true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001629 }
1630 m = m->next;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001631 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001632
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001633 return false;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001634}
1635
1636/*
1637 * Count and mark off the parameters in a multi-line macro call.
1638 * This is called both from within the multi-line macro expansion
1639 * code, and also to mark off the default parameters when provided
1640 * in a %macro definition line.
1641 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001642static void count_mmac_params(Token * t, int *nparam, Token *** params)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001643{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001644 int paramsize, brace;
1645
1646 *nparam = paramsize = 0;
1647 *params = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001648 while (t) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001649 /* +1: we need space for the final NULL */
H. Peter Anvin91fb6f12008-09-01 10:56:33 -07001650 if (*nparam+1 >= paramsize) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001651 paramsize += PARAM_DELTA;
1652 *params = nasm_realloc(*params, sizeof(**params) * paramsize);
1653 }
1654 skip_white_(t);
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08001655 brace = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001656 if (tok_is_(t, "{"))
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08001657 brace++;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001658 (*params)[(*nparam)++] = t;
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08001659 if (brace) {
1660 while (brace && (t = t->next) != NULL) {
1661 if (tok_is_(t, "{"))
1662 brace++;
1663 else if (tok_is_(t, "}"))
1664 brace--;
1665 }
1666
1667 if (t) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001668 /*
1669 * Now we've found the closing brace, look further
1670 * for the comma.
1671 */
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08001672 t = t->next;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001673 skip_white_(t);
1674 if (tok_isnt_(t, ",")) {
H. Peter Anvin215186f2016-02-17 20:27:41 -08001675 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00001676 "braces do not enclose all of macro parameter");
1677 while (tok_isnt_(t, ","))
1678 t = t->next;
1679 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001680 }
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08001681 } else {
1682 while (tok_isnt_(t, ","))
1683 t = t->next;
1684 }
1685 if (t) { /* got a comma/brace */
1686 t = t->next; /* eat the comma */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001687 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001688 }
1689}
1690
1691/*
H. Peter Anvin76690a12002-04-30 20:52:49 +00001692 * Determine whether one of the various `if' conditions is true or
1693 * not.
1694 *
1695 * We must free the tline we get passed.
1696 */
H. Peter Anvin70055962007-10-11 00:05:31 -07001697static bool if_condition(Token * tline, enum preproc_token ct)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001698{
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001699 enum pp_conditional i = PP_COND(ct);
H. Peter Anvin70055962007-10-11 00:05:31 -07001700 bool j;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001701 Token *t, *tt, **tptr, *origline;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001702 struct tokenval tokval;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001703 expr *evalresult;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001704 enum pp_token_type needtype;
H. Peter Anvin077fb932010-07-20 14:56:30 -07001705 char *p;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001706
1707 origline = tline;
1708
H. Peter Anvine2c80182005-01-15 22:15:51 +00001709 switch (i) {
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001710 case PPC_IFCTX:
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001711 j = false; /* have we matched yet? */
Victor van den Elzen0e857f12008-07-23 13:21:29 +02001712 while (true) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001713 skip_white_(tline);
Victor van den Elzen0e857f12008-07-23 13:21:29 +02001714 if (!tline)
1715 break;
1716 if (tline->type != TOK_ID) {
H. Peter Anvin215186f2016-02-17 20:27:41 -08001717 nasm_error(ERR_NONFATAL,
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001718 "`%s' expects context identifiers", pp_directives[ct]);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001719 free_tlist(origline);
1720 return -1;
1721 }
Victor van den Elzen0e857f12008-07-23 13:21:29 +02001722 if (cstk && cstk->name && !nasm_stricmp(tline->text, cstk->name))
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001723 j = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001724 tline = tline->next;
1725 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001726 break;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001727
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001728 case PPC_IFDEF:
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001729 j = false; /* have we matched yet? */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001730 while (tline) {
1731 skip_white_(tline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001732 if (!tline || (tline->type != TOK_ID &&
1733 (tline->type != TOK_PREPROC_ID ||
1734 tline->text[1] != '$'))) {
H. Peter Anvin215186f2016-02-17 20:27:41 -08001735 nasm_error(ERR_NONFATAL,
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001736 "`%s' expects macro identifiers", pp_directives[ct]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001737 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001738 }
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07001739 if (smacro_defined(NULL, tline->text, 0, NULL, true))
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001740 j = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001741 tline = tline->next;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001742 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001743 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001744
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001745 case PPC_IFENV:
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001746 tline = expand_smacro(tline);
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001747 j = false; /* have we matched yet? */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001748 while (tline) {
1749 skip_white_(tline);
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001750 if (!tline || (tline->type != TOK_ID &&
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001751 tline->type != TOK_STRING &&
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001752 (tline->type != TOK_PREPROC_ID ||
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001753 tline->text[1] != '!'))) {
H. Peter Anvin215186f2016-02-17 20:27:41 -08001754 nasm_error(ERR_NONFATAL,
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001755 "`%s' expects environment variable names",
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001756 pp_directives[ct]);
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001757 goto fail;
1758 }
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001759 p = tline->text;
1760 if (tline->type == TOK_PREPROC_ID)
1761 p += 2; /* Skip leading %! */
1762 if (*p == '\'' || *p == '\"' || *p == '`')
1763 nasm_unquote_cstr(p, ct);
1764 if (getenv(p))
1765 j = true;
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001766 tline = tline->next;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001767 }
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001768 break;
1769
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001770 case PPC_IFIDN:
1771 case PPC_IFIDNI:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001772 tline = expand_smacro(tline);
1773 t = tt = tline;
1774 while (tok_isnt_(tt, ","))
1775 tt = tt->next;
1776 if (!tt) {
H. Peter Anvin215186f2016-02-17 20:27:41 -08001777 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00001778 "`%s' expects two comma-separated arguments",
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001779 pp_directives[ct]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001780 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001781 }
1782 tt = tt->next;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001783 j = true; /* assume equality unless proved not */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001784 while ((t->type != TOK_OTHER || strcmp(t->text, ",")) && tt) {
1785 if (tt->type == TOK_OTHER && !strcmp(tt->text, ",")) {
H. Peter Anvin215186f2016-02-17 20:27:41 -08001786 nasm_error(ERR_NONFATAL, "`%s': more than one comma on line",
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001787 pp_directives[ct]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001788 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001789 }
1790 if (t->type == TOK_WHITESPACE) {
1791 t = t->next;
1792 continue;
1793 }
1794 if (tt->type == TOK_WHITESPACE) {
1795 tt = tt->next;
1796 continue;
1797 }
1798 if (tt->type != t->type) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001799 j = false; /* found mismatching tokens */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001800 break;
1801 }
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07001802 /* When comparing strings, need to unquote them first */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001803 if (t->type == TOK_STRING) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001804 size_t l1 = nasm_unquote(t->text, NULL);
1805 size_t l2 = nasm_unquote(tt->text, NULL);
H. Peter Anvind2456592008-06-19 15:04:18 -07001806
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001807 if (l1 != l2) {
1808 j = false;
1809 break;
1810 }
1811 if (mmemcmp(t->text, tt->text, l1, i == PPC_IFIDN)) {
1812 j = false;
1813 break;
1814 }
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07001815 } else if (mstrcmp(tt->text, t->text, i == PPC_IFIDN) != 0) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001816 j = false; /* found mismatching tokens */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001817 break;
1818 }
Nickolay Yurchenkof3b3ce22003-09-21 20:38:43 +00001819
H. Peter Anvine2c80182005-01-15 22:15:51 +00001820 t = t->next;
1821 tt = tt->next;
1822 }
1823 if ((t->type != TOK_OTHER || strcmp(t->text, ",")) || tt)
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001824 j = false; /* trailing gunk on one end or other */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001825 break;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001826
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001827 case PPC_IFMACRO:
H. Peter Anvin89cee572009-07-15 09:16:54 -04001828 {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001829 bool found = false;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001830 MMacro searching, *mmac;
H. Peter Anvin65747262002-05-07 00:10:05 +00001831
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001832 skip_white_(tline);
1833 tline = expand_id(tline);
1834 if (!tok_type_(tline, TOK_ID)) {
H. Peter Anvin215186f2016-02-17 20:27:41 -08001835 nasm_error(ERR_NONFATAL,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001836 "`%s' expects a macro name", pp_directives[ct]);
1837 goto fail;
1838 }
1839 searching.name = nasm_strdup(tline->text);
1840 searching.casesense = true;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001841 searching.plus = false;
1842 searching.nolist = false;
1843 searching.in_progress = 0;
1844 searching.max_depth = 0;
1845 searching.rep_nest = NULL;
1846 searching.nparam_min = 0;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001847 searching.nparam_max = INT_MAX;
1848 tline = expand_smacro(tline->next);
1849 skip_white_(tline);
1850 if (!tline) {
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 or nothing",
1854 pp_directives[ct]);
1855 } else {
1856 searching.nparam_min = searching.nparam_max =
1857 readnum(tline->text, &j);
1858 if (j)
H. Peter Anvin215186f2016-02-17 20:27:41 -08001859 nasm_error(ERR_NONFATAL,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001860 "unable to parse parameter count `%s'",
1861 tline->text);
1862 }
1863 if (tline && tok_is_(tline->next, "-")) {
1864 tline = tline->next->next;
1865 if (tok_is_(tline, "*"))
1866 searching.nparam_max = INT_MAX;
1867 else if (!tok_type_(tline, TOK_NUMBER))
H. Peter Anvin215186f2016-02-17 20:27:41 -08001868 nasm_error(ERR_NONFATAL,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001869 "`%s' expects a parameter count after `-'",
1870 pp_directives[ct]);
1871 else {
1872 searching.nparam_max = readnum(tline->text, &j);
1873 if (j)
H. Peter Anvin215186f2016-02-17 20:27:41 -08001874 nasm_error(ERR_NONFATAL,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001875 "unable to parse parameter count `%s'",
1876 tline->text);
1877 if (searching.nparam_min > searching.nparam_max)
H. Peter Anvin215186f2016-02-17 20:27:41 -08001878 nasm_error(ERR_NONFATAL,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001879 "minimum parameter count exceeds maximum");
1880 }
1881 }
1882 if (tline && tok_is_(tline->next, "+")) {
1883 tline = tline->next;
1884 searching.plus = true;
1885 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001886 mmac = (MMacro *) hash_findix(&mmacros, searching.name);
1887 while (mmac) {
1888 if (!strcmp(mmac->name, searching.name) &&
1889 (mmac->nparam_min <= searching.nparam_max
1890 || searching.plus)
1891 && (searching.nparam_min <= mmac->nparam_max
1892 || mmac->plus)) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001893 found = true;
1894 break;
1895 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001896 mmac = mmac->next;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001897 }
1898 if (tline && tline->next)
H. Peter Anvin215186f2016-02-17 20:27:41 -08001899 nasm_error(ERR_WARNING|ERR_PASS1,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001900 "trailing garbage after %%ifmacro ignored");
1901 nasm_free(searching.name);
1902 j = found;
1903 break;
H. Peter Anvin89cee572009-07-15 09:16:54 -04001904 }
H. Peter Anvin65747262002-05-07 00:10:05 +00001905
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001906 case PPC_IFID:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001907 needtype = TOK_ID;
1908 goto iftype;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001909 case PPC_IFNUM:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001910 needtype = TOK_NUMBER;
1911 goto iftype;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001912 case PPC_IFSTR:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001913 needtype = TOK_STRING;
1914 goto iftype;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001915
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001916iftype:
1917 t = tline = expand_smacro(tline);
H. Peter Anvind85d2502008-05-04 17:53:31 -07001918
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001919 while (tok_type_(t, TOK_WHITESPACE) ||
1920 (needtype == TOK_NUMBER &&
1921 tok_type_(t, TOK_OTHER) &&
1922 (t->text[0] == '-' || t->text[0] == '+') &&
1923 !t->text[1]))
1924 t = t->next;
H. Peter Anvind85d2502008-05-04 17:53:31 -07001925
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001926 j = tok_type_(t, needtype);
1927 break;
H. Peter Anvincbf768d2008-02-16 16:41:25 -08001928
1929 case PPC_IFTOKEN:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001930 t = tline = expand_smacro(tline);
1931 while (tok_type_(t, TOK_WHITESPACE))
1932 t = t->next;
H. Peter Anvincbf768d2008-02-16 16:41:25 -08001933
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001934 j = false;
1935 if (t) {
1936 t = t->next; /* Skip the actual token */
1937 while (tok_type_(t, TOK_WHITESPACE))
1938 t = t->next;
1939 j = !t; /* Should be nothing left */
1940 }
1941 break;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001942
H. Peter Anvin134b9462008-02-16 17:01:40 -08001943 case PPC_IFEMPTY:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001944 t = tline = expand_smacro(tline);
1945 while (tok_type_(t, TOK_WHITESPACE))
1946 t = t->next;
H. Peter Anvin134b9462008-02-16 17:01:40 -08001947
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001948 j = !t; /* Should be empty */
1949 break;
H. Peter Anvin134b9462008-02-16 17:01:40 -08001950
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001951 case PPC_IF:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001952 t = tline = expand_smacro(tline);
1953 tptr = &t;
1954 tokval.t_type = TOKEN_INVALID;
1955 evalresult = evaluate(ppscan, tptr, &tokval,
H. Peter Anvin215186f2016-02-17 20:27:41 -08001956 NULL, pass | CRITICAL, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001957 if (!evalresult)
1958 return -1;
1959 if (tokval.t_type)
H. Peter Anvin215186f2016-02-17 20:27:41 -08001960 nasm_error(ERR_WARNING|ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +00001961 "trailing garbage after expression ignored");
1962 if (!is_simple(evalresult)) {
H. Peter Anvin215186f2016-02-17 20:27:41 -08001963 nasm_error(ERR_NONFATAL,
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001964 "non-constant value given to `%s'", pp_directives[ct]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001965 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001966 }
Chuck Crayne60ae75d2007-05-02 01:59:16 +00001967 j = reloc_value(evalresult) != 0;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001968 break;
H. Peter Anvin95e28822007-09-12 04:20:08 +00001969
H. Peter Anvine2c80182005-01-15 22:15:51 +00001970 default:
H. Peter Anvin215186f2016-02-17 20:27:41 -08001971 nasm_error(ERR_FATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00001972 "preprocessor directive `%s' not yet implemented",
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001973 pp_directives[ct]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001974 goto fail;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001975 }
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001976
1977 free_tlist(origline);
1978 return j ^ PP_NEGATIVE(ct);
H. Peter Anvin70653092007-10-19 14:42:29 -07001979
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001980fail:
1981 free_tlist(origline);
1982 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001983}
1984
1985/*
H. Peter Anvin4db5a162007-10-11 13:42:09 -07001986 * Common code for defining an smacro
1987 */
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001988static bool define_smacro(Context *ctx, const char *mname, bool casesense,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001989 int nparam, Token *expansion)
H. Peter Anvin4db5a162007-10-11 13:42:09 -07001990{
1991 SMacro *smac, **smhead;
H. Peter Anvin166c2472008-05-28 12:28:58 -07001992 struct hash_table *smtbl;
H. Peter Anvin70653092007-10-19 14:42:29 -07001993
H. Peter Anvin4db5a162007-10-11 13:42:09 -07001994 if (smacro_defined(ctx, mname, nparam, &smac, casesense)) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001995 if (!smac) {
H. Peter Anvin215186f2016-02-17 20:27:41 -08001996 nasm_error(ERR_WARNING|ERR_PASS1,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001997 "single-line macro `%s' defined both with and"
1998 " without parameters", mname);
1999 /*
2000 * Some instances of the old code considered this a failure,
2001 * some others didn't. What is the right thing to do here?
2002 */
2003 free_tlist(expansion);
2004 return false; /* Failure */
2005 } else {
2006 /*
2007 * We're redefining, so we have to take over an
2008 * existing SMacro structure. This means freeing
2009 * what was already in it.
2010 */
2011 nasm_free(smac->name);
2012 free_tlist(smac->expansion);
2013 }
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002014 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002015 smtbl = ctx ? &ctx->localmac : &smacros;
2016 smhead = (SMacro **) hash_findi_add(smtbl, mname);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002017 smac = nasm_malloc(sizeof(SMacro));
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002018 smac->next = *smhead;
2019 *smhead = smac;
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002020 }
2021 smac->name = nasm_strdup(mname);
2022 smac->casesense = casesense;
2023 smac->nparam = nparam;
2024 smac->expansion = expansion;
2025 smac->in_progress = false;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002026 return true; /* Success */
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002027}
2028
2029/*
2030 * Undefine an smacro
2031 */
2032static void undef_smacro(Context *ctx, const char *mname)
2033{
2034 SMacro **smhead, *s, **sp;
H. Peter Anvin166c2472008-05-28 12:28:58 -07002035 struct hash_table *smtbl;
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002036
H. Peter Anvin166c2472008-05-28 12:28:58 -07002037 smtbl = ctx ? &ctx->localmac : &smacros;
2038 smhead = (SMacro **)hash_findi(smtbl, mname, NULL);
H. Peter Anvin70653092007-10-19 14:42:29 -07002039
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002040 if (smhead) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002041 /*
2042 * We now have a macro name... go hunt for it.
2043 */
2044 sp = smhead;
2045 while ((s = *sp) != NULL) {
2046 if (!mstrcmp(s->name, mname, s->casesense)) {
2047 *sp = s->next;
2048 nasm_free(s->name);
2049 free_tlist(s->expansion);
2050 nasm_free(s);
2051 } else {
2052 sp = &s->next;
2053 }
2054 }
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002055 }
2056}
2057
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002058/*
H. Peter Anvina26433d2008-07-16 14:40:01 -07002059 * Parse a mmacro specification.
2060 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002061static bool parse_mmacro_spec(Token *tline, MMacro *def, const char *directive)
H. Peter Anvina26433d2008-07-16 14:40:01 -07002062{
2063 bool err;
2064
2065 tline = tline->next;
2066 skip_white_(tline);
2067 tline = expand_id(tline);
2068 if (!tok_type_(tline, TOK_ID)) {
H. Peter Anvin215186f2016-02-17 20:27:41 -08002069 nasm_error(ERR_NONFATAL, "`%s' expects a macro name", directive);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002070 return false;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002071 }
Victor van den Elzenb916ede2008-07-23 15:14:22 +02002072
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002073 def->prev = NULL;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002074 def->name = nasm_strdup(tline->text);
2075 def->plus = false;
2076 def->nolist = false;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002077 def->in_progress = 0;
2078 def->rep_nest = NULL;
Victor van den Elzenb916ede2008-07-23 15:14:22 +02002079 def->nparam_min = 0;
2080 def->nparam_max = 0;
2081
H. Peter Anvina26433d2008-07-16 14:40:01 -07002082 tline = expand_smacro(tline->next);
2083 skip_white_(tline);
2084 if (!tok_type_(tline, TOK_NUMBER)) {
H. Peter Anvin215186f2016-02-17 20:27:41 -08002085 nasm_error(ERR_NONFATAL, "`%s' expects a parameter count", directive);
H. Peter Anvina26433d2008-07-16 14:40:01 -07002086 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002087 def->nparam_min = def->nparam_max =
2088 readnum(tline->text, &err);
2089 if (err)
H. Peter Anvin215186f2016-02-17 20:27:41 -08002090 nasm_error(ERR_NONFATAL,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002091 "unable to parse parameter count `%s'", tline->text);
H. Peter Anvina26433d2008-07-16 14:40:01 -07002092 }
2093 if (tline && tok_is_(tline->next, "-")) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002094 tline = tline->next->next;
2095 if (tok_is_(tline, "*")) {
2096 def->nparam_max = INT_MAX;
2097 } else if (!tok_type_(tline, TOK_NUMBER)) {
H. Peter Anvin215186f2016-02-17 20:27:41 -08002098 nasm_error(ERR_NONFATAL,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002099 "`%s' expects a parameter count after `-'", directive);
2100 } else {
2101 def->nparam_max = readnum(tline->text, &err);
2102 if (err) {
H. Peter Anvin215186f2016-02-17 20:27:41 -08002103 nasm_error(ERR_NONFATAL, "unable to parse parameter count `%s'",
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002104 tline->text);
2105 }
2106 if (def->nparam_min > def->nparam_max) {
H. Peter Anvin215186f2016-02-17 20:27:41 -08002107 nasm_error(ERR_NONFATAL, "minimum parameter count exceeds maximum");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002108 }
2109 }
H. Peter Anvina26433d2008-07-16 14:40:01 -07002110 }
2111 if (tline && tok_is_(tline->next, "+")) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002112 tline = tline->next;
2113 def->plus = true;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002114 }
2115 if (tline && tok_type_(tline->next, TOK_ID) &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002116 !nasm_stricmp(tline->next->text, ".nolist")) {
2117 tline = tline->next;
2118 def->nolist = true;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002119 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002120
H. Peter Anvina26433d2008-07-16 14:40:01 -07002121 /*
2122 * Handle default parameters.
2123 */
2124 if (tline && tline->next) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002125 def->dlist = tline->next;
2126 tline->next = NULL;
2127 count_mmac_params(def->dlist, &def->ndefs, &def->defaults);
H. Peter Anvina26433d2008-07-16 14:40:01 -07002128 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002129 def->dlist = NULL;
2130 def->defaults = NULL;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002131 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002132 def->expansion = NULL;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002133
H. Peter Anvin89cee572009-07-15 09:16:54 -04002134 if (def->defaults && def->ndefs > def->nparam_max - def->nparam_min &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002135 !def->plus)
H. Peter Anvin215186f2016-02-17 20:27:41 -08002136 nasm_error(ERR_WARNING|ERR_PASS1|ERR_WARN_MDP,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002137 "too many default macro parameters");
Victor van den Elzenb916ede2008-07-23 15:14:22 +02002138
H. Peter Anvina26433d2008-07-16 14:40:01 -07002139 return true;
2140}
2141
2142
2143/*
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002144 * Decode a size directive
2145 */
2146static int parse_size(const char *str) {
2147 static const char *size_names[] =
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002148 { "byte", "dword", "oword", "qword", "tword", "word", "yword" };
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002149 static const int sizes[] =
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002150 { 0, 1, 4, 16, 8, 10, 2, 32 };
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002151
Cyrill Gorcunova7319242010-06-03 22:04:36 +04002152 return sizes[bsii(str, size_names, ARRAY_SIZE(size_names))+1];
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002153}
2154
Ed Beroset3ab3f412002-06-11 03:31:49 +00002155/**
2156 * find and process preprocessor directive in passed line
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002157 * Find out if a line contains a preprocessor directive, and deal
2158 * with it if so.
H. Peter Anvin70653092007-10-19 14:42:29 -07002159 *
Ed Beroset3ab3f412002-06-11 03:31:49 +00002160 * If a directive _is_ found, it is the responsibility of this routine
2161 * (and not the caller) to free_tlist() the line.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002162 *
Ed Beroset3ab3f412002-06-11 03:31:49 +00002163 * @param tline a pointer to the current tokeninzed line linked list
2164 * @return DIRECTIVE_FOUND or NO_DIRECTIVE_FOUND
H. Peter Anvin70653092007-10-19 14:42:29 -07002165 *
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002166 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002167static int do_directive(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00002168{
H. Peter Anvin4169a472007-09-12 01:29:43 +00002169 enum preproc_token i;
2170 int j;
H. Peter Anvin70055962007-10-11 00:05:31 -07002171 bool err;
2172 int nparam;
2173 bool nolist;
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07002174 bool casesense;
H. Peter Anvin8cfdb9d2007-09-14 18:36:01 -07002175 int k, m;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002176 int offset;
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08002177 char *p, *pp;
2178 const char *mname;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002179 Include *inc;
2180 Context *ctx;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002181 Cond *cond;
2182 MMacro *mmac, **mmhead;
Jin Kyu Songc9486b92013-10-28 17:07:57 -07002183 Token *t = NULL, *tt, *param_start, *macro_start, *last, **tptr, *origline;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002184 Line *l;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002185 struct tokenval tokval;
2186 expr *evalresult;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002187 MMacro *tmp_defining; /* Used when manipulating rep_nest */
H. Peter Anvinf8ba53e2007-10-11 10:11:57 -07002188 int64_t count;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07002189 size_t len;
H. Peter Anvin8e3f75e2008-09-24 00:21:58 -07002190 int severity;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002191
2192 origline = tline;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002193
H. Peter Anvineba20a72002-04-30 20:53:55 +00002194 skip_white_(tline);
H. Peter Anvinf2936d72008-06-04 15:11:23 -07002195 if (!tline || !tok_type_(tline, TOK_PREPROC_ID) ||
H. Peter Anvine2c80182005-01-15 22:15:51 +00002196 (tline->text[1] == '%' || tline->text[1] == '$'
2197 || tline->text[1] == '!'))
2198 return NO_DIRECTIVE_FOUND;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002199
H. Peter Anvin4169a472007-09-12 01:29:43 +00002200 i = pp_token_hash(tline->text);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002201
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002202 /*
2203 * FIXME: We zap execution of PP_RMACRO, PP_IRMACRO, PP_EXITMACRO
2204 * since they are known to be buggy at moment, we need to fix them
2205 * in future release (2.09-2.10)
2206 */
Philipp Klokeb432f572013-03-31 12:01:23 +02002207 if (i == PP_RMACRO || i == PP_IRMACRO || i == PP_EXITMACRO) {
H. Peter Anvin215186f2016-02-17 20:27:41 -08002208 nasm_error(ERR_NONFATAL, "unknown preprocessor directive `%s'",
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002209 tline->text);
2210 return NO_DIRECTIVE_FOUND;
2211 }
2212
2213 /*
2214 * If we're in a non-emitting branch of a condition construct,
2215 * or walking to the end of an already terminated %rep block,
2216 * we should ignore all directives except for condition
2217 * directives.
2218 */
2219 if (((istk->conds && !emitting(istk->conds->state)) ||
2220 (istk->mstk && !istk->mstk->in_progress)) && !is_condition(i)) {
2221 return NO_DIRECTIVE_FOUND;
2222 }
2223
2224 /*
2225 * If we're defining a macro or reading a %rep block, we should
2226 * ignore all directives except for %macro/%imacro (which nest),
2227 * %endm/%endmacro, and (only if we're in a %rep block) %endrep.
2228 * If we're in a %rep block, another %rep nests, so should be let through.
2229 */
2230 if (defining && i != PP_MACRO && i != PP_IMACRO &&
2231 i != PP_RMACRO && i != PP_IRMACRO &&
2232 i != PP_ENDMACRO && i != PP_ENDM &&
2233 (defining->name || (i != PP_ENDREP && i != PP_REP))) {
2234 return NO_DIRECTIVE_FOUND;
2235 }
2236
2237 if (defining) {
2238 if (i == PP_MACRO || i == PP_IMACRO ||
2239 i == PP_RMACRO || i == PP_IRMACRO) {
2240 nested_mac_count++;
2241 return NO_DIRECTIVE_FOUND;
2242 } else if (nested_mac_count > 0) {
2243 if (i == PP_ENDMACRO) {
2244 nested_mac_count--;
2245 return NO_DIRECTIVE_FOUND;
2246 }
2247 }
2248 if (!defining->name) {
2249 if (i == PP_REP) {
2250 nested_rep_count++;
2251 return NO_DIRECTIVE_FOUND;
2252 } else if (nested_rep_count > 0) {
2253 if (i == PP_ENDREP) {
2254 nested_rep_count--;
2255 return NO_DIRECTIVE_FOUND;
2256 }
2257 }
2258 }
2259 }
2260
H. Peter Anvin4169a472007-09-12 01:29:43 +00002261 switch (i) {
2262 case PP_INVALID:
H. Peter Anvin215186f2016-02-17 20:27:41 -08002263 nasm_error(ERR_NONFATAL, "unknown preprocessor directive `%s'",
H. Peter Anvine2c80182005-01-15 22:15:51 +00002264 tline->text);
2265 return NO_DIRECTIVE_FOUND; /* didn't get it */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002266
H. Peter Anvine2c80182005-01-15 22:15:51 +00002267 case PP_STACKSIZE:
2268 /* Directive to tell NASM what the default stack size is. The
2269 * default is for a 16-bit stack, and this can be overriden with
2270 * %stacksize large.
H. Peter Anvine2c80182005-01-15 22:15:51 +00002271 */
2272 tline = tline->next;
2273 if (tline && tline->type == TOK_WHITESPACE)
2274 tline = tline->next;
2275 if (!tline || tline->type != TOK_ID) {
H. Peter Anvin215186f2016-02-17 20:27:41 -08002276 nasm_error(ERR_NONFATAL, "`%%stacksize' missing size parameter");
H. Peter Anvine2c80182005-01-15 22:15:51 +00002277 free_tlist(origline);
2278 return DIRECTIVE_FOUND;
2279 }
2280 if (nasm_stricmp(tline->text, "flat") == 0) {
2281 /* All subsequent ARG directives are for a 32-bit stack */
2282 StackSize = 4;
2283 StackPointer = "ebp";
2284 ArgOffset = 8;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002285 LocalOffset = 0;
Charles Crayne7eaf9192007-11-08 22:11:14 -08002286 } else if (nasm_stricmp(tline->text, "flat64") == 0) {
2287 /* All subsequent ARG directives are for a 64-bit stack */
2288 StackSize = 8;
2289 StackPointer = "rbp";
Per Jessen53252e02010-02-11 00:16:59 +03002290 ArgOffset = 16;
Charles Crayne7eaf9192007-11-08 22:11:14 -08002291 LocalOffset = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002292 } else if (nasm_stricmp(tline->text, "large") == 0) {
2293 /* All subsequent ARG directives are for a 16-bit stack,
2294 * far function call.
2295 */
2296 StackSize = 2;
2297 StackPointer = "bp";
2298 ArgOffset = 4;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002299 LocalOffset = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002300 } else if (nasm_stricmp(tline->text, "small") == 0) {
2301 /* All subsequent ARG directives are for a 16-bit stack,
2302 * far function call. We don't support near functions.
2303 */
2304 StackSize = 2;
2305 StackPointer = "bp";
2306 ArgOffset = 6;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002307 LocalOffset = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002308 } else {
H. Peter Anvin215186f2016-02-17 20:27:41 -08002309 nasm_error(ERR_NONFATAL, "`%%stacksize' invalid size type");
H. Peter Anvine2c80182005-01-15 22:15:51 +00002310 free_tlist(origline);
2311 return DIRECTIVE_FOUND;
2312 }
2313 free_tlist(origline);
2314 return DIRECTIVE_FOUND;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002315
H. Peter Anvine2c80182005-01-15 22:15:51 +00002316 case PP_ARG:
2317 /* TASM like ARG directive to define arguments to functions, in
2318 * the following form:
2319 *
2320 * ARG arg1:WORD, arg2:DWORD, arg4:QWORD
2321 */
2322 offset = ArgOffset;
2323 do {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00002324 char *arg, directive[256];
H. Peter Anvine2c80182005-01-15 22:15:51 +00002325 int size = StackSize;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002326
H. Peter Anvine2c80182005-01-15 22:15:51 +00002327 /* Find the argument name */
2328 tline = tline->next;
2329 if (tline && tline->type == TOK_WHITESPACE)
2330 tline = tline->next;
2331 if (!tline || tline->type != TOK_ID) {
H. Peter Anvin215186f2016-02-17 20:27:41 -08002332 nasm_error(ERR_NONFATAL, "`%%arg' missing argument parameter");
H. Peter Anvine2c80182005-01-15 22:15:51 +00002333 free_tlist(origline);
2334 return DIRECTIVE_FOUND;
2335 }
2336 arg = tline->text;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002337
H. Peter Anvine2c80182005-01-15 22:15:51 +00002338 /* Find the argument size type */
2339 tline = tline->next;
2340 if (!tline || tline->type != TOK_OTHER
2341 || tline->text[0] != ':') {
H. Peter Anvin215186f2016-02-17 20:27:41 -08002342 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00002343 "Syntax error processing `%%arg' directive");
2344 free_tlist(origline);
2345 return DIRECTIVE_FOUND;
2346 }
2347 tline = tline->next;
2348 if (!tline || tline->type != TOK_ID) {
H. Peter Anvin215186f2016-02-17 20:27:41 -08002349 nasm_error(ERR_NONFATAL, "`%%arg' missing size type parameter");
H. Peter Anvine2c80182005-01-15 22:15:51 +00002350 free_tlist(origline);
2351 return DIRECTIVE_FOUND;
2352 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002353
H. Peter Anvine2c80182005-01-15 22:15:51 +00002354 /* Allow macro expansion of type parameter */
Keith Kaniosb7a89542007-04-12 02:40:54 +00002355 tt = tokenize(tline->text);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002356 tt = expand_smacro(tt);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002357 size = parse_size(tt->text);
2358 if (!size) {
H. Peter Anvin215186f2016-02-17 20:27:41 -08002359 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00002360 "Invalid size type for `%%arg' missing directive");
2361 free_tlist(tt);
2362 free_tlist(origline);
2363 return DIRECTIVE_FOUND;
2364 }
2365 free_tlist(tt);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002366
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002367 /* Round up to even stack slots */
2368 size = ALIGN(size, StackSize);
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002369
H. Peter Anvine2c80182005-01-15 22:15:51 +00002370 /* Now define the macro for the argument */
2371 snprintf(directive, sizeof(directive), "%%define %s (%s+%d)",
2372 arg, StackPointer, offset);
Keith Kaniosb7a89542007-04-12 02:40:54 +00002373 do_directive(tokenize(directive));
H. Peter Anvine2c80182005-01-15 22:15:51 +00002374 offset += size;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002375
H. Peter Anvine2c80182005-01-15 22:15:51 +00002376 /* Move to the next argument in the list */
2377 tline = tline->next;
2378 if (tline && tline->type == TOK_WHITESPACE)
2379 tline = tline->next;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002380 } while (tline && tline->type == TOK_OTHER && tline->text[0] == ',');
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002381 ArgOffset = offset;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002382 free_tlist(origline);
2383 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002384
H. Peter Anvine2c80182005-01-15 22:15:51 +00002385 case PP_LOCAL:
2386 /* TASM like LOCAL directive to define local variables for a
2387 * function, in the following form:
2388 *
2389 * LOCAL local1:WORD, local2:DWORD, local4:QWORD = LocalSize
2390 *
2391 * The '= LocalSize' at the end is ignored by NASM, but is
2392 * required by TASM to define the local parameter size (and used
2393 * by the TASM macro package).
2394 */
2395 offset = LocalOffset;
2396 do {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00002397 char *local, directive[256];
H. Peter Anvine2c80182005-01-15 22:15:51 +00002398 int size = StackSize;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002399
H. Peter Anvine2c80182005-01-15 22:15:51 +00002400 /* Find the argument name */
2401 tline = tline->next;
2402 if (tline && tline->type == TOK_WHITESPACE)
2403 tline = tline->next;
2404 if (!tline || tline->type != TOK_ID) {
H. Peter Anvin215186f2016-02-17 20:27:41 -08002405 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00002406 "`%%local' missing argument parameter");
2407 free_tlist(origline);
2408 return DIRECTIVE_FOUND;
2409 }
2410 local = tline->text;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002411
H. Peter Anvine2c80182005-01-15 22:15:51 +00002412 /* Find the argument size type */
2413 tline = tline->next;
2414 if (!tline || tline->type != TOK_OTHER
2415 || tline->text[0] != ':') {
H. Peter Anvin215186f2016-02-17 20:27:41 -08002416 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00002417 "Syntax error processing `%%local' directive");
2418 free_tlist(origline);
2419 return DIRECTIVE_FOUND;
2420 }
2421 tline = tline->next;
2422 if (!tline || tline->type != TOK_ID) {
H. Peter Anvin215186f2016-02-17 20:27:41 -08002423 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00002424 "`%%local' missing size type parameter");
2425 free_tlist(origline);
2426 return DIRECTIVE_FOUND;
2427 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00002428
H. Peter Anvine2c80182005-01-15 22:15:51 +00002429 /* Allow macro expansion of type parameter */
Keith Kaniosb7a89542007-04-12 02:40:54 +00002430 tt = tokenize(tline->text);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002431 tt = expand_smacro(tt);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002432 size = parse_size(tt->text);
2433 if (!size) {
H. Peter Anvin215186f2016-02-17 20:27:41 -08002434 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00002435 "Invalid size type for `%%local' missing directive");
2436 free_tlist(tt);
2437 free_tlist(origline);
2438 return DIRECTIVE_FOUND;
2439 }
2440 free_tlist(tt);
H. Peter Anvin734b1882002-04-30 21:01:08 +00002441
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002442 /* Round up to even stack slots */
2443 size = ALIGN(size, StackSize);
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002444
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002445 offset += size; /* Negative offset, increment before */
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002446
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002447 /* Now define the macro for the argument */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002448 snprintf(directive, sizeof(directive), "%%define %s (%s-%d)",
2449 local, StackPointer, offset);
Keith Kaniosb7a89542007-04-12 02:40:54 +00002450 do_directive(tokenize(directive));
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002451
H. Peter Anvine2c80182005-01-15 22:15:51 +00002452 /* Now define the assign to setup the enter_c macro correctly */
2453 snprintf(directive, sizeof(directive),
2454 "%%assign %%$localsize %%$localsize+%d", size);
Keith Kaniosb7a89542007-04-12 02:40:54 +00002455 do_directive(tokenize(directive));
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002456
H. Peter Anvine2c80182005-01-15 22:15:51 +00002457 /* Move to the next argument in the list */
2458 tline = tline->next;
2459 if (tline && tline->type == TOK_WHITESPACE)
2460 tline = tline->next;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002461 } while (tline && tline->type == TOK_OTHER && tline->text[0] == ',');
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002462 LocalOffset = offset;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002463 free_tlist(origline);
2464 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002465
H. Peter Anvine2c80182005-01-15 22:15:51 +00002466 case PP_CLEAR:
2467 if (tline->next)
H. Peter Anvin215186f2016-02-17 20:27:41 -08002468 nasm_error(ERR_WARNING|ERR_PASS1,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002469 "trailing garbage after `%%clear' ignored");
2470 free_macros();
2471 init_macros();
H. Peter Anvine2c80182005-01-15 22:15:51 +00002472 free_tlist(origline);
2473 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002474
H. Peter Anvin418ca702008-05-30 10:42:30 -07002475 case PP_DEPEND:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002476 t = tline->next = expand_smacro(tline->next);
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002477 skip_white_(t);
2478 if (!t || (t->type != TOK_STRING &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002479 t->type != TOK_INTERNAL_STRING)) {
H. Peter Anvin215186f2016-02-17 20:27:41 -08002480 nasm_error(ERR_NONFATAL, "`%%depend' expects a file name");
H. Peter Anvin418ca702008-05-30 10:42:30 -07002481 free_tlist(origline);
2482 return DIRECTIVE_FOUND; /* but we did _something_ */
2483 }
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002484 if (t->next)
H. Peter Anvin215186f2016-02-17 20:27:41 -08002485 nasm_error(ERR_WARNING|ERR_PASS1,
H. Peter Anvin418ca702008-05-30 10:42:30 -07002486 "trailing garbage after `%%depend' ignored");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002487 p = t->text;
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002488 if (t->type != TOK_INTERNAL_STRING)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002489 nasm_unquote_cstr(p, i);
2490 if (dephead && !in_list(*dephead, p)) {
2491 StrList *sl = nasm_malloc(strlen(p)+1+sizeof sl->next);
2492 sl->next = NULL;
2493 strcpy(sl->str, p);
2494 *deptail = sl;
2495 deptail = &sl->next;
2496 }
2497 free_tlist(origline);
H. Peter Anvin418ca702008-05-30 10:42:30 -07002498 return DIRECTIVE_FOUND;
2499
2500 case PP_INCLUDE:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002501 t = tline->next = expand_smacro(tline->next);
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002502 skip_white_(t);
H. Peter Anvind2456592008-06-19 15:04:18 -07002503
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002504 if (!t || (t->type != TOK_STRING &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002505 t->type != TOK_INTERNAL_STRING)) {
H. Peter Anvin215186f2016-02-17 20:27:41 -08002506 nasm_error(ERR_NONFATAL, "`%%include' expects a file name");
H. Peter Anvine2c80182005-01-15 22:15:51 +00002507 free_tlist(origline);
2508 return DIRECTIVE_FOUND; /* but we did _something_ */
2509 }
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002510 if (t->next)
H. Peter Anvin215186f2016-02-17 20:27:41 -08002511 nasm_error(ERR_WARNING|ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +00002512 "trailing garbage after `%%include' ignored");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002513 p = t->text;
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002514 if (t->type != TOK_INTERNAL_STRING)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002515 nasm_unquote_cstr(p, i);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002516 inc = nasm_malloc(sizeof(Include));
H. Peter Anvine2c80182005-01-15 22:15:51 +00002517 inc->next = istk;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002518 inc->conds = NULL;
Fabian Giesen142285d2016-04-28 13:48:15 -07002519 inc->fp = inc_fopen(p, dephead, &deptail, pass == 0, "r");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002520 if (!inc->fp) {
2521 /* -MG given but file not found */
2522 nasm_free(inc);
2523 } else {
H. Peter Anvin274cda82016-05-10 02:56:29 -07002524 inc->fname = src_set_fname(p);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002525 inc->lineno = src_set_linnum(0);
2526 inc->lineinc = 1;
2527 inc->expansion = NULL;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002528 inc->mstk = NULL;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002529 istk = inc;
H. Peter Anvin172b8402016-02-18 01:16:18 -08002530 lfmt->uplevel(LIST_INCLUDE);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002531 }
2532 free_tlist(origline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002533 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002534
H. Peter Anvind2456592008-06-19 15:04:18 -07002535 case PP_USE:
H. Peter Anvinf4ae5ad2008-06-19 18:39:24 -07002536 {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002537 static macros_t *use_pkg;
2538 const char *pkg_macro = NULL;
H. Peter Anvinf4ae5ad2008-06-19 18:39:24 -07002539
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002540 tline = tline->next;
2541 skip_white_(tline);
2542 tline = expand_id(tline);
H. Peter Anvind2456592008-06-19 15:04:18 -07002543
H. Peter Anvin264b7b92008-10-24 16:38:17 -07002544 if (!tline || (tline->type != TOK_STRING &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002545 tline->type != TOK_INTERNAL_STRING &&
2546 tline->type != TOK_ID)) {
H. Peter Anvin215186f2016-02-17 20:27:41 -08002547 nasm_error(ERR_NONFATAL, "`%%use' expects a package name");
H. Peter Anvind2456592008-06-19 15:04:18 -07002548 free_tlist(origline);
2549 return DIRECTIVE_FOUND; /* but we did _something_ */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002550 }
H. Peter Anvin264b7b92008-10-24 16:38:17 -07002551 if (tline->next)
H. Peter Anvin215186f2016-02-17 20:27:41 -08002552 nasm_error(ERR_WARNING|ERR_PASS1,
H. Peter Anvind2456592008-06-19 15:04:18 -07002553 "trailing garbage after `%%use' ignored");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002554 if (tline->type == TOK_STRING)
2555 nasm_unquote_cstr(tline->text, i);
2556 use_pkg = nasm_stdmac_find_package(tline->text);
2557 if (!use_pkg)
H. Peter Anvin215186f2016-02-17 20:27:41 -08002558 nasm_error(ERR_NONFATAL, "unknown `%%use' package: %s", tline->text);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002559 else
2560 pkg_macro = (char *)use_pkg + 1; /* The first string will be <%define>__USE_*__ */
Victor van den Elzen35eb2ea2010-03-10 22:33:48 +01002561 if (use_pkg && ! smacro_defined(NULL, pkg_macro, 0, NULL, true)) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002562 /* Not already included, go ahead and include it */
2563 stdmacpos = use_pkg;
2564 }
2565 free_tlist(origline);
H. Peter Anvind2456592008-06-19 15:04:18 -07002566 return DIRECTIVE_FOUND;
H. Peter Anvinf4ae5ad2008-06-19 18:39:24 -07002567 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002568 case PP_PUSH:
H. Peter Anvine2c80182005-01-15 22:15:51 +00002569 case PP_REPL:
H. Peter Anvin42b56392008-10-24 16:24:21 -07002570 case PP_POP:
H. Peter Anvine2c80182005-01-15 22:15:51 +00002571 tline = tline->next;
2572 skip_white_(tline);
2573 tline = expand_id(tline);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002574 if (tline) {
2575 if (!tok_type_(tline, TOK_ID)) {
H. Peter Anvin215186f2016-02-17 20:27:41 -08002576 nasm_error(ERR_NONFATAL, "`%s' expects a context identifier",
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002577 pp_directives[i]);
2578 free_tlist(origline);
2579 return DIRECTIVE_FOUND; /* but we did _something_ */
2580 }
2581 if (tline->next)
H. Peter Anvin215186f2016-02-17 20:27:41 -08002582 nasm_error(ERR_WARNING|ERR_PASS1,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002583 "trailing garbage after `%s' ignored",
2584 pp_directives[i]);
2585 p = nasm_strdup(tline->text);
2586 } else {
2587 p = NULL; /* Anonymous */
2588 }
H. Peter Anvin42b56392008-10-24 16:24:21 -07002589
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002590 if (i == PP_PUSH) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002591 ctx = nasm_malloc(sizeof(Context));
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002592 ctx->next = cstk;
2593 hash_init(&ctx->localmac, HASH_SMALL);
2594 ctx->name = p;
2595 ctx->number = unique++;
2596 cstk = ctx;
2597 } else {
2598 /* %pop or %repl */
2599 if (!cstk) {
H. Peter Anvin215186f2016-02-17 20:27:41 -08002600 nasm_error(ERR_NONFATAL, "`%s': context stack is empty",
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002601 pp_directives[i]);
2602 } else if (i == PP_POP) {
2603 if (p && (!cstk->name || nasm_stricmp(p, cstk->name)))
H. Peter Anvin215186f2016-02-17 20:27:41 -08002604 nasm_error(ERR_NONFATAL, "`%%pop' in wrong context: %s, "
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002605 "expected %s",
2606 cstk->name ? cstk->name : "anonymous", p);
2607 else
2608 ctx_pop();
2609 } else {
2610 /* i == PP_REPL */
2611 nasm_free(cstk->name);
2612 cstk->name = p;
2613 p = NULL;
2614 }
2615 nasm_free(p);
2616 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002617 free_tlist(origline);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002618 return DIRECTIVE_FOUND;
H. Peter Anvin8e3f75e2008-09-24 00:21:58 -07002619 case PP_FATAL:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002620 severity = ERR_FATAL;
2621 goto issue_error;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002622 case PP_ERROR:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002623 severity = ERR_NONFATAL;
2624 goto issue_error;
H. Peter Anvin7df04172008-06-10 18:27:38 -07002625 case PP_WARNING:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002626 severity = ERR_WARNING|ERR_WARN_USER;
2627 goto issue_error;
H. Peter Anvin8e3f75e2008-09-24 00:21:58 -07002628
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002629issue_error:
H. Peter Anvin7df04172008-06-10 18:27:38 -07002630 {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002631 /* Only error out if this is the final pass */
2632 if (pass != 2 && i != PP_FATAL)
2633 return DIRECTIVE_FOUND;
2634
2635 tline->next = expand_smacro(tline->next);
2636 tline = tline->next;
2637 skip_white_(tline);
2638 t = tline ? tline->next : NULL;
2639 skip_white_(t);
2640 if (tok_type_(tline, TOK_STRING) && !t) {
2641 /* The line contains only a quoted string */
2642 p = tline->text;
2643 nasm_unquote(p, NULL); /* Ignore NUL character truncation */
H. Peter Anvin215186f2016-02-17 20:27:41 -08002644 nasm_error(severity, "%s", p);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002645 } else {
2646 /* Not a quoted string, or more than a quoted string */
2647 p = detoken(tline, false);
H. Peter Anvin215186f2016-02-17 20:27:41 -08002648 nasm_error(severity, "%s", p);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002649 nasm_free(p);
2650 }
2651 free_tlist(origline);
2652 return DIRECTIVE_FOUND;
H. Peter Anvin7df04172008-06-10 18:27:38 -07002653 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00002654
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002655 CASE_PP_IF:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002656 if (istk->conds && !emitting(istk->conds->state))
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002657 j = COND_NEVER;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002658 else {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002659 j = if_condition(tline->next, i);
2660 tline->next = NULL; /* it got freed */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002661 j = j < 0 ? COND_NEVER : j ? COND_IF_TRUE : COND_IF_FALSE;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002662 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002663 cond = nasm_malloc(sizeof(Cond));
2664 cond->next = istk->conds;
2665 cond->state = j;
2666 istk->conds = cond;
2667 if(istk->mstk)
2668 istk->mstk->condcnt ++;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002669 free_tlist(origline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002670 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002671
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002672 CASE_PP_ELIF:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002673 if (!istk->conds)
H. Peter Anvin215186f2016-02-17 20:27:41 -08002674 nasm_error(ERR_FATAL, "`%s': no matching `%%if'", pp_directives[i]);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002675 switch(istk->conds->state) {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002676 case COND_IF_TRUE:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002677 istk->conds->state = COND_DONE;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002678 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002679
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002680 case COND_DONE:
2681 case COND_NEVER:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002682 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002683
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002684 case COND_ELSE_TRUE:
2685 case COND_ELSE_FALSE:
H. Peter Anvin215186f2016-02-17 20:27:41 -08002686 nasm_error(ERR_WARNING|ERR_PASS1|ERR_PP_PRECOND,
2687 "`%%elif' after `%%else' ignored");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002688 istk->conds->state = COND_NEVER;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002689 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002690
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002691 case COND_IF_FALSE:
2692 /*
2693 * IMPORTANT: In the case of %if, we will already have
2694 * called expand_mmac_params(); however, if we're
2695 * processing an %elif we must have been in a
2696 * non-emitting mode, which would have inhibited
2697 * the normal invocation of expand_mmac_params().
2698 * Therefore, we have to do it explicitly here.
2699 */
2700 j = if_condition(expand_mmac_params(tline->next), i);
2701 tline->next = NULL; /* it got freed */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002702 istk->conds->state =
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002703 j < 0 ? COND_NEVER : j ? COND_IF_TRUE : COND_IF_FALSE;
2704 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002705 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002706 free_tlist(origline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002707 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002708
H. Peter Anvine2c80182005-01-15 22:15:51 +00002709 case PP_ELSE:
2710 if (tline->next)
H. Peter Anvin215186f2016-02-17 20:27:41 -08002711 nasm_error(ERR_WARNING|ERR_PASS1|ERR_PP_PRECOND,
2712 "trailing garbage after `%%else' ignored");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002713 if (!istk->conds)
H. Peter Anvin215186f2016-02-17 20:27:41 -08002714 nasm_fatal(0, "`%%else: no matching `%%if'");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002715 switch(istk->conds->state) {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002716 case COND_IF_TRUE:
2717 case COND_DONE:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002718 istk->conds->state = COND_ELSE_FALSE;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002719 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002720
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002721 case COND_NEVER:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002722 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002723
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002724 case COND_IF_FALSE:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002725 istk->conds->state = COND_ELSE_TRUE;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002726 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002727
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002728 case COND_ELSE_TRUE:
2729 case COND_ELSE_FALSE:
H. Peter Anvin215186f2016-02-17 20:27:41 -08002730 nasm_error(ERR_WARNING|ERR_PASS1|ERR_PP_PRECOND,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002731 "`%%else' after `%%else' ignored.");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002732 istk->conds->state = COND_NEVER;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002733 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002734 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002735 free_tlist(origline);
2736 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002737
H. Peter Anvine2c80182005-01-15 22:15:51 +00002738 case PP_ENDIF:
2739 if (tline->next)
H. Peter Anvin215186f2016-02-17 20:27:41 -08002740 nasm_error(ERR_WARNING|ERR_PASS1|ERR_PP_PRECOND,
2741 "trailing garbage after `%%endif' ignored");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002742 if (!istk->conds)
H. Peter Anvin215186f2016-02-17 20:27:41 -08002743 nasm_error(ERR_FATAL, "`%%endif': no matching `%%if'");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002744 cond = istk->conds;
2745 istk->conds = cond->next;
2746 nasm_free(cond);
2747 if(istk->mstk)
2748 istk->mstk->condcnt --;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002749 free_tlist(origline);
2750 return DIRECTIVE_FOUND;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002751
H. Peter Anvindb8f96e2009-07-15 09:07:29 -04002752 case PP_RMACRO:
2753 case PP_IRMACRO:
H. Peter Anvine2c80182005-01-15 22:15:51 +00002754 case PP_MACRO:
2755 case PP_IMACRO:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002756 if (defining) {
H. Peter Anvin215186f2016-02-17 20:27:41 -08002757 nasm_error(ERR_FATAL, "`%s': already defining a macro",
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002758 pp_directives[i]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002759 return DIRECTIVE_FOUND;
2760 }
H. Peter Anvin4def1a82016-05-09 13:59:44 -07002761 defining = nasm_zalloc(sizeof(MMacro));
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002762 defining->max_depth =
2763 (i == PP_RMACRO) || (i == PP_IRMACRO) ? DEADMAN_LIMIT : 0;
2764 defining->casesense = (i == PP_MACRO) || (i == PP_RMACRO);
2765 if (!parse_mmacro_spec(tline, defining, pp_directives[i])) {
2766 nasm_free(defining);
2767 defining = NULL;
2768 return DIRECTIVE_FOUND;
2769 }
H. Peter Anvina26433d2008-07-16 14:40:01 -07002770
H. Peter Anvin4def1a82016-05-09 13:59:44 -07002771 src_get(&defining->xline, &defining->fname);
2772
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002773 mmac = (MMacro *) hash_findix(&mmacros, defining->name);
2774 while (mmac) {
2775 if (!strcmp(mmac->name, defining->name) &&
2776 (mmac->nparam_min <= defining->nparam_max
2777 || defining->plus)
2778 && (defining->nparam_min <= mmac->nparam_max
2779 || mmac->plus)) {
H. Peter Anvin215186f2016-02-17 20:27:41 -08002780 nasm_error(ERR_WARNING|ERR_PASS1,
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002781 "redefining multi-line macro `%s'", defining->name);
2782 return DIRECTIVE_FOUND;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002783 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002784 mmac = mmac->next;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002785 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002786 free_tlist(origline);
2787 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002788
H. Peter Anvine2c80182005-01-15 22:15:51 +00002789 case PP_ENDM:
2790 case PP_ENDMACRO:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002791 if (! (defining && defining->name)) {
H. Peter Anvin215186f2016-02-17 20:27:41 -08002792 nasm_error(ERR_NONFATAL, "`%s': not defining a macro", tline->text);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002793 return DIRECTIVE_FOUND;
2794 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002795 mmhead = (MMacro **) hash_findi_add(&mmacros, defining->name);
2796 defining->next = *mmhead;
2797 *mmhead = defining;
2798 defining = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002799 free_tlist(origline);
2800 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002801
H. Peter Anvin89cee572009-07-15 09:16:54 -04002802 case PP_EXITMACRO:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002803 /*
2804 * We must search along istk->expansion until we hit a
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002805 * macro-end marker for a macro with a name. Then we
2806 * bypass all lines between exitmacro and endmacro.
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002807 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002808 list_for_each(l, istk->expansion)
2809 if (l->finishes && l->finishes->name)
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002810 break;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002811
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002812 if (l) {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002813 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002814 * Remove all conditional entries relative to this
2815 * macro invocation. (safe to do in this context)
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002816 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002817 for ( ; l->finishes->condcnt > 0; l->finishes->condcnt --) {
2818 cond = istk->conds;
2819 istk->conds = cond->next;
2820 nasm_free(cond);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002821 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002822 istk->expansion = l;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002823 } else {
H. Peter Anvin215186f2016-02-17 20:27:41 -08002824 nasm_error(ERR_NONFATAL, "`%%exitmacro' not within `%%macro' block");
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002825 }
Keith Kanios852f1ee2009-07-12 00:19:55 -05002826 free_tlist(origline);
2827 return DIRECTIVE_FOUND;
2828
H. Peter Anvina26433d2008-07-16 14:40:01 -07002829 case PP_UNMACRO:
2830 case PP_UNIMACRO:
2831 {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002832 MMacro **mmac_p;
2833 MMacro spec;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002834
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002835 spec.casesense = (i == PP_UNMACRO);
2836 if (!parse_mmacro_spec(tline, &spec, pp_directives[i])) {
2837 return DIRECTIVE_FOUND;
2838 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002839 mmac_p = (MMacro **) hash_findi(&mmacros, spec.name, NULL);
2840 while (mmac_p && *mmac_p) {
2841 mmac = *mmac_p;
2842 if (mmac->casesense == spec.casesense &&
2843 !mstrcmp(mmac->name, spec.name, spec.casesense) &&
2844 mmac->nparam_min == spec.nparam_min &&
2845 mmac->nparam_max == spec.nparam_max &&
2846 mmac->plus == spec.plus) {
2847 *mmac_p = mmac->next;
2848 free_mmacro(mmac);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002849 } else {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002850 mmac_p = &mmac->next;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002851 }
2852 }
2853 free_tlist(origline);
2854 free_tlist(spec.dlist);
2855 return DIRECTIVE_FOUND;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002856 }
2857
H. Peter Anvine2c80182005-01-15 22:15:51 +00002858 case PP_ROTATE:
2859 if (tline->next && tline->next->type == TOK_WHITESPACE)
2860 tline = tline->next;
H. Peter Anvin89cee572009-07-15 09:16:54 -04002861 if (!tline->next) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002862 free_tlist(origline);
H. Peter Anvin215186f2016-02-17 20:27:41 -08002863 nasm_error(ERR_NONFATAL, "`%%rotate' missing rotate count");
H. Peter Anvine2c80182005-01-15 22:15:51 +00002864 return DIRECTIVE_FOUND;
2865 }
2866 t = expand_smacro(tline->next);
2867 tline->next = NULL;
2868 free_tlist(origline);
2869 tline = t;
2870 tptr = &t;
2871 tokval.t_type = TOKEN_INVALID;
2872 evalresult =
H. Peter Anvin215186f2016-02-17 20:27:41 -08002873 evaluate(ppscan, tptr, &tokval, NULL, pass, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002874 free_tlist(tline);
2875 if (!evalresult)
2876 return DIRECTIVE_FOUND;
2877 if (tokval.t_type)
H. Peter Anvin215186f2016-02-17 20:27:41 -08002878 nasm_error(ERR_WARNING|ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +00002879 "trailing garbage after expression ignored");
2880 if (!is_simple(evalresult)) {
H. Peter Anvin215186f2016-02-17 20:27:41 -08002881 nasm_error(ERR_NONFATAL, "non-constant value given to `%%rotate'");
H. Peter Anvine2c80182005-01-15 22:15:51 +00002882 return DIRECTIVE_FOUND;
2883 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002884 mmac = istk->mstk;
2885 while (mmac && !mmac->name) /* avoid mistaking %reps for macros */
2886 mmac = mmac->next_active;
2887 if (!mmac) {
H. Peter Anvin215186f2016-02-17 20:27:41 -08002888 nasm_error(ERR_NONFATAL, "`%%rotate' invoked outside a macro call");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002889 } else if (mmac->nparam == 0) {
H. Peter Anvin215186f2016-02-17 20:27:41 -08002890 nasm_error(ERR_NONFATAL,
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002891 "`%%rotate' invoked within macro without parameters");
2892 } else {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002893 int rotate = mmac->rotate + reloc_value(evalresult);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002894
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002895 rotate %= (int)mmac->nparam;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002896 if (rotate < 0)
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002897 rotate += mmac->nparam;
2898
2899 mmac->rotate = rotate;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002900 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002901 return DIRECTIVE_FOUND;
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00002902
H. Peter Anvine2c80182005-01-15 22:15:51 +00002903 case PP_REP:
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002904 nolist = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002905 do {
2906 tline = tline->next;
2907 } while (tok_type_(tline, TOK_WHITESPACE));
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00002908
H. Peter Anvine2c80182005-01-15 22:15:51 +00002909 if (tok_type_(tline, TOK_ID) &&
2910 nasm_stricmp(tline->text, ".nolist") == 0) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002911 nolist = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002912 do {
2913 tline = tline->next;
2914 } while (tok_type_(tline, TOK_WHITESPACE));
2915 }
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00002916
H. Peter Anvine2c80182005-01-15 22:15:51 +00002917 if (tline) {
2918 t = expand_smacro(tline);
2919 tptr = &t;
2920 tokval.t_type = TOKEN_INVALID;
2921 evalresult =
H. Peter Anvin215186f2016-02-17 20:27:41 -08002922 evaluate(ppscan, tptr, &tokval, NULL, pass, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002923 if (!evalresult) {
2924 free_tlist(origline);
2925 return DIRECTIVE_FOUND;
2926 }
2927 if (tokval.t_type)
H. Peter Anvin215186f2016-02-17 20:27:41 -08002928 nasm_error(ERR_WARNING|ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +00002929 "trailing garbage after expression ignored");
2930 if (!is_simple(evalresult)) {
H. Peter Anvin215186f2016-02-17 20:27:41 -08002931 nasm_error(ERR_NONFATAL, "non-constant value given to `%%rep'");
H. Peter Anvine2c80182005-01-15 22:15:51 +00002932 return DIRECTIVE_FOUND;
2933 }
Cyrill Gorcunove091d6e2010-08-09 13:58:22 +04002934 count = reloc_value(evalresult);
2935 if (count >= REP_LIMIT) {
H. Peter Anvin215186f2016-02-17 20:27:41 -08002936 nasm_error(ERR_NONFATAL, "`%%rep' value exceeds limit");
Cyrill Gorcunove091d6e2010-08-09 13:58:22 +04002937 count = 0;
2938 } else
2939 count++;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002940 } else {
H. Peter Anvin215186f2016-02-17 20:27:41 -08002941 nasm_error(ERR_NONFATAL, "`%%rep' expects a repeat count");
H. Peter Anvinf8ba53e2007-10-11 10:11:57 -07002942 count = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002943 }
2944 free_tlist(origline);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002945
2946 tmp_defining = defining;
2947 defining = nasm_malloc(sizeof(MMacro));
2948 defining->prev = NULL;
2949 defining->name = NULL; /* flags this macro as a %rep block */
2950 defining->casesense = false;
2951 defining->plus = false;
2952 defining->nolist = nolist;
2953 defining->in_progress = count;
2954 defining->max_depth = 0;
2955 defining->nparam_min = defining->nparam_max = 0;
2956 defining->defaults = NULL;
2957 defining->dlist = NULL;
2958 defining->expansion = NULL;
2959 defining->next_active = istk->mstk;
2960 defining->rep_nest = tmp_defining;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002961 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002962
H. Peter Anvine2c80182005-01-15 22:15:51 +00002963 case PP_ENDREP:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002964 if (!defining || defining->name) {
H. Peter Anvin215186f2016-02-17 20:27:41 -08002965 nasm_error(ERR_NONFATAL, "`%%endrep': no matching `%%rep'");
H. Peter Anvine2c80182005-01-15 22:15:51 +00002966 return DIRECTIVE_FOUND;
2967 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00002968
H. Peter Anvine2c80182005-01-15 22:15:51 +00002969 /*
2970 * Now we have a "macro" defined - although it has no name
2971 * and we won't be entering it in the hash tables - we must
2972 * push a macro-end marker for it on to istk->expansion.
2973 * After that, it will take care of propagating itself (a
2974 * macro-end marker line for a macro which is really a %rep
2975 * block will cause the macro to be re-expanded, complete
2976 * with another macro-end marker to ensure the process
2977 * continues) until the whole expansion is forcibly removed
2978 * from istk->expansion by a %exitrep.
2979 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002980 l = nasm_malloc(sizeof(Line));
2981 l->next = istk->expansion;
2982 l->finishes = defining;
2983 l->first = NULL;
2984 istk->expansion = l;
2985
2986 istk->mstk = defining;
2987
H. Peter Anvin172b8402016-02-18 01:16:18 -08002988 lfmt->uplevel(defining->nolist ? LIST_MACRO_NOLIST : LIST_MACRO);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002989 tmp_defining = defining;
2990 defining = defining->rep_nest;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002991 free_tlist(origline);
2992 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002993
H. Peter Anvine2c80182005-01-15 22:15:51 +00002994 case PP_EXITREP:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002995 /*
2996 * We must search along istk->expansion until we hit a
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002997 * macro-end marker for a macro with no name. Then we set
2998 * its `in_progress' flag to 0.
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002999 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003000 list_for_each(l, istk->expansion)
3001 if (l->finishes && !l->finishes->name)
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003002 break;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003003
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003004 if (l)
3005 l->finishes->in_progress = 1;
3006 else
H. Peter Anvin215186f2016-02-17 20:27:41 -08003007 nasm_error(ERR_NONFATAL, "`%%exitrep' not within `%%rep' block");
H. Peter Anvine2c80182005-01-15 22:15:51 +00003008 free_tlist(origline);
3009 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003010
H. Peter Anvine2c80182005-01-15 22:15:51 +00003011 case PP_XDEFINE:
3012 case PP_IXDEFINE:
3013 case PP_DEFINE:
3014 case PP_IDEFINE:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003015 casesense = (i == PP_DEFINE || i == PP_XDEFINE);
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07003016
H. Peter Anvine2c80182005-01-15 22:15:51 +00003017 tline = tline->next;
3018 skip_white_(tline);
3019 tline = expand_id(tline);
3020 if (!tline || (tline->type != TOK_ID &&
3021 (tline->type != TOK_PREPROC_ID ||
3022 tline->text[1] != '$'))) {
H. Peter Anvin215186f2016-02-17 20:27:41 -08003023 nasm_error(ERR_NONFATAL, "`%s' expects a macro identifier",
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003024 pp_directives[i]);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003025 free_tlist(origline);
3026 return DIRECTIVE_FOUND;
3027 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003028
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04003029 ctx = get_ctx(tline->text, &mname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003030 last = tline;
3031 param_start = tline = tline->next;
3032 nparam = 0;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003033
H. Peter Anvine2c80182005-01-15 22:15:51 +00003034 /* Expand the macro definition now for %xdefine and %ixdefine */
3035 if ((i == PP_XDEFINE) || (i == PP_IXDEFINE))
3036 tline = expand_smacro(tline);
H. Peter Anvin734b1882002-04-30 21:01:08 +00003037
H. Peter Anvine2c80182005-01-15 22:15:51 +00003038 if (tok_is_(tline, "(")) {
3039 /*
3040 * This macro has parameters.
3041 */
H. Peter Anvin734b1882002-04-30 21:01:08 +00003042
H. Peter Anvine2c80182005-01-15 22:15:51 +00003043 tline = tline->next;
3044 while (1) {
3045 skip_white_(tline);
3046 if (!tline) {
H. Peter Anvin215186f2016-02-17 20:27:41 -08003047 nasm_error(ERR_NONFATAL, "parameter identifier expected");
H. Peter Anvine2c80182005-01-15 22:15:51 +00003048 free_tlist(origline);
3049 return DIRECTIVE_FOUND;
3050 }
3051 if (tline->type != TOK_ID) {
H. Peter Anvin215186f2016-02-17 20:27:41 -08003052 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00003053 "`%s': parameter identifier expected",
3054 tline->text);
3055 free_tlist(origline);
3056 return DIRECTIVE_FOUND;
3057 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003058 tline->type = TOK_SMAC_PARAM + nparam++;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003059 tline = tline->next;
3060 skip_white_(tline);
3061 if (tok_is_(tline, ",")) {
3062 tline = tline->next;
H. Peter Anvinca348b62008-07-23 10:49:26 -04003063 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003064 if (!tok_is_(tline, ")")) {
H. Peter Anvin215186f2016-02-17 20:27:41 -08003065 nasm_error(ERR_NONFATAL,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003066 "`)' expected to terminate macro template");
3067 free_tlist(origline);
3068 return DIRECTIVE_FOUND;
3069 }
3070 break;
3071 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00003072 }
3073 last = tline;
3074 tline = tline->next;
3075 }
3076 if (tok_type_(tline, TOK_WHITESPACE))
3077 last = tline, tline = tline->next;
3078 macro_start = NULL;
3079 last->next = NULL;
3080 t = tline;
3081 while (t) {
3082 if (t->type == TOK_ID) {
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04003083 list_for_each(tt, param_start)
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003084 if (tt->type >= TOK_SMAC_PARAM &&
H. Peter Anvine2c80182005-01-15 22:15:51 +00003085 !strcmp(tt->text, t->text))
3086 t->type = tt->type;
3087 }
3088 tt = t->next;
3089 t->next = macro_start;
3090 macro_start = t;
3091 t = tt;
3092 }
3093 /*
3094 * Good. We now have a macro name, a parameter count, and a
3095 * token list (in reverse order) for an expansion. We ought
3096 * to be OK just to create an SMacro, store it, and let
3097 * free_tlist have the rest of the line (which we have
3098 * carefully re-terminated after chopping off the expansion
3099 * from the end).
3100 */
H. Peter Anvin4db5a162007-10-11 13:42:09 -07003101 define_smacro(ctx, mname, casesense, nparam, macro_start);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003102 free_tlist(origline);
3103 return DIRECTIVE_FOUND;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003104
H. Peter Anvine2c80182005-01-15 22:15:51 +00003105 case PP_UNDEF:
3106 tline = tline->next;
3107 skip_white_(tline);
3108 tline = expand_id(tline);
3109 if (!tline || (tline->type != TOK_ID &&
3110 (tline->type != TOK_PREPROC_ID ||
3111 tline->text[1] != '$'))) {
H. Peter Anvin215186f2016-02-17 20:27:41 -08003112 nasm_error(ERR_NONFATAL, "`%%undef' expects a macro identifier");
H. Peter Anvine2c80182005-01-15 22:15:51 +00003113 free_tlist(origline);
3114 return DIRECTIVE_FOUND;
3115 }
3116 if (tline->next) {
H. Peter Anvin215186f2016-02-17 20:27:41 -08003117 nasm_error(ERR_WARNING|ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +00003118 "trailing garbage after macro name ignored");
3119 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003120
H. Peter Anvine2c80182005-01-15 22:15:51 +00003121 /* Find the context that symbol belongs to */
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04003122 ctx = get_ctx(tline->text, &mname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003123 undef_smacro(ctx, mname);
3124 free_tlist(origline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003125 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003126
H. Peter Anvin9e200162008-06-04 17:23:14 -07003127 case PP_DEFSTR:
3128 case PP_IDEFSTR:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003129 casesense = (i == PP_DEFSTR);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003130
3131 tline = tline->next;
3132 skip_white_(tline);
3133 tline = expand_id(tline);
3134 if (!tline || (tline->type != TOK_ID &&
3135 (tline->type != TOK_PREPROC_ID ||
3136 tline->text[1] != '$'))) {
H. Peter Anvin215186f2016-02-17 20:27:41 -08003137 nasm_error(ERR_NONFATAL, "`%s' expects a macro identifier",
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003138 pp_directives[i]);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003139 free_tlist(origline);
3140 return DIRECTIVE_FOUND;
3141 }
3142
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04003143 ctx = get_ctx(tline->text, &mname);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003144 last = tline;
3145 tline = expand_smacro(tline->next);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003146 last->next = NULL;
H. Peter Anvin9e200162008-06-04 17:23:14 -07003147
3148 while (tok_type_(tline, TOK_WHITESPACE))
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003149 tline = delete_Token(tline);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003150
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003151 p = detoken(tline, false);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003152 macro_start = nasm_malloc(sizeof(*macro_start));
3153 macro_start->next = NULL;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003154 macro_start->text = nasm_quote(p, strlen(p));
3155 macro_start->type = TOK_STRING;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003156 macro_start->a.mac = NULL;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003157 nasm_free(p);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003158
3159 /*
3160 * We now have a macro name, an implicit parameter count of
3161 * zero, and a string token to use as an expansion. Create
3162 * and store an SMacro.
3163 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003164 define_smacro(ctx, mname, casesense, 0, macro_start);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003165 free_tlist(origline);
3166 return DIRECTIVE_FOUND;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003167
H. Peter Anvin2f55bda2009-07-14 15:04:04 -04003168 case PP_DEFTOK:
3169 case PP_IDEFTOK:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003170 casesense = (i == PP_DEFTOK);
3171
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003172 tline = tline->next;
3173 skip_white_(tline);
3174 tline = expand_id(tline);
3175 if (!tline || (tline->type != TOK_ID &&
3176 (tline->type != TOK_PREPROC_ID ||
3177 tline->text[1] != '$'))) {
H. Peter Anvin215186f2016-02-17 20:27:41 -08003178 nasm_error(ERR_NONFATAL,
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003179 "`%s' expects a macro identifier as first parameter",
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003180 pp_directives[i]);
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003181 free_tlist(origline);
3182 return DIRECTIVE_FOUND;
3183 }
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04003184 ctx = get_ctx(tline->text, &mname);
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003185 last = tline;
3186 tline = expand_smacro(tline->next);
3187 last->next = NULL;
3188
3189 t = tline;
3190 while (tok_type_(t, TOK_WHITESPACE))
3191 t = t->next;
3192 /* t should now point to the string */
Cyrill Gorcunov6908e582010-09-06 19:36:15 +04003193 if (!tok_type_(t, TOK_STRING)) {
H. Peter Anvin215186f2016-02-17 20:27:41 -08003194 nasm_error(ERR_NONFATAL,
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003195 "`%s` requires string as second parameter",
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003196 pp_directives[i]);
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003197 free_tlist(tline);
3198 free_tlist(origline);
3199 return DIRECTIVE_FOUND;
3200 }
3201
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04003202 /*
3203 * Convert the string to a token stream. Note that smacros
3204 * are stored with the token stream reversed, so we have to
3205 * reverse the output of tokenize().
3206 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003207 nasm_unquote_cstr(t->text, i);
H. Peter Anvinb40992c2010-09-15 08:57:21 -07003208 macro_start = reverse_tokens(tokenize(t->text));
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003209
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003210 /*
3211 * We now have a macro name, an implicit parameter count of
3212 * zero, and a numeric token to use as an expansion. Create
3213 * and store an SMacro.
3214 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003215 define_smacro(ctx, mname, casesense, 0, macro_start);
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003216 free_tlist(tline);
3217 free_tlist(origline);
3218 return DIRECTIVE_FOUND;
H. Peter Anvin9e200162008-06-04 17:23:14 -07003219
H. Peter Anvin418ca702008-05-30 10:42:30 -07003220 case PP_PATHSEARCH:
3221 {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003222 FILE *fp;
3223 StrList *xsl = NULL;
3224 StrList **xst = &xsl;
H. Peter Anvin418ca702008-05-30 10:42:30 -07003225
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003226 casesense = true;
H. Peter Anvin418ca702008-05-30 10:42:30 -07003227
3228 tline = tline->next;
3229 skip_white_(tline);
3230 tline = expand_id(tline);
3231 if (!tline || (tline->type != TOK_ID &&
3232 (tline->type != TOK_PREPROC_ID ||
3233 tline->text[1] != '$'))) {
H. Peter Anvin215186f2016-02-17 20:27:41 -08003234 nasm_error(ERR_NONFATAL,
H. Peter Anvin418ca702008-05-30 10:42:30 -07003235 "`%%pathsearch' expects a macro identifier as first parameter");
3236 free_tlist(origline);
3237 return DIRECTIVE_FOUND;
3238 }
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04003239 ctx = get_ctx(tline->text, &mname);
H. Peter Anvin418ca702008-05-30 10:42:30 -07003240 last = tline;
3241 tline = expand_smacro(tline->next);
3242 last->next = NULL;
3243
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003244 t = tline;
H. Peter Anvin418ca702008-05-30 10:42:30 -07003245 while (tok_type_(t, TOK_WHITESPACE))
3246 t = t->next;
3247
3248 if (!t || (t->type != TOK_STRING &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003249 t->type != TOK_INTERNAL_STRING)) {
H. Peter Anvin215186f2016-02-17 20:27:41 -08003250 nasm_error(ERR_NONFATAL, "`%%pathsearch' expects a file name");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003251 free_tlist(tline);
H. Peter Anvin418ca702008-05-30 10:42:30 -07003252 free_tlist(origline);
3253 return DIRECTIVE_FOUND; /* but we did _something_ */
3254 }
3255 if (t->next)
H. Peter Anvin215186f2016-02-17 20:27:41 -08003256 nasm_error(ERR_WARNING|ERR_PASS1,
H. Peter Anvin418ca702008-05-30 10:42:30 -07003257 "trailing garbage after `%%pathsearch' ignored");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003258 p = t->text;
H. Peter Anvin427cc912008-06-01 21:43:03 -07003259 if (t->type != TOK_INTERNAL_STRING)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003260 nasm_unquote(p, NULL);
H. Peter Anvin418ca702008-05-30 10:42:30 -07003261
Fabian Giesen142285d2016-04-28 13:48:15 -07003262 fp = inc_fopen(p, &xsl, &xst, true, "r");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003263 if (fp) {
3264 p = xsl->str;
3265 fclose(fp); /* Don't actually care about the file */
3266 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003267 macro_start = nasm_malloc(sizeof(*macro_start));
3268 macro_start->next = NULL;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003269 macro_start->text = nasm_quote(p, strlen(p));
3270 macro_start->type = TOK_STRING;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003271 macro_start->a.mac = NULL;
3272 if (xsl)
3273 nasm_free(xsl);
H. Peter Anvin418ca702008-05-30 10:42:30 -07003274
3275 /*
3276 * We now have a macro name, an implicit parameter count of
3277 * zero, and a string token to use as an expansion. Create
3278 * and store an SMacro.
3279 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003280 define_smacro(ctx, mname, casesense, 0, macro_start);
3281 free_tlist(tline);
H. Peter Anvin418ca702008-05-30 10:42:30 -07003282 free_tlist(origline);
3283 return DIRECTIVE_FOUND;
3284 }
3285
H. Peter Anvine2c80182005-01-15 22:15:51 +00003286 case PP_STRLEN:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003287 casesense = true;
H. Peter Anvin70653092007-10-19 14:42:29 -07003288
H. Peter Anvine2c80182005-01-15 22:15:51 +00003289 tline = tline->next;
3290 skip_white_(tline);
3291 tline = expand_id(tline);
3292 if (!tline || (tline->type != TOK_ID &&
3293 (tline->type != TOK_PREPROC_ID ||
3294 tline->text[1] != '$'))) {
H. Peter Anvin215186f2016-02-17 20:27:41 -08003295 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00003296 "`%%strlen' expects a macro identifier as first parameter");
3297 free_tlist(origline);
3298 return DIRECTIVE_FOUND;
3299 }
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04003300 ctx = get_ctx(tline->text, &mname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003301 last = tline;
3302 tline = expand_smacro(tline->next);
3303 last->next = NULL;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003304
H. Peter Anvine2c80182005-01-15 22:15:51 +00003305 t = tline;
3306 while (tok_type_(t, TOK_WHITESPACE))
3307 t = t->next;
3308 /* t should now point to the string */
Cyrill Gorcunov4e1d5ab2010-07-23 18:51:51 +04003309 if (!tok_type_(t, TOK_STRING)) {
H. Peter Anvin215186f2016-02-17 20:27:41 -08003310 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00003311 "`%%strlen` requires string as second parameter");
3312 free_tlist(tline);
3313 free_tlist(origline);
3314 return DIRECTIVE_FOUND;
3315 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003316
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003317 macro_start = nasm_malloc(sizeof(*macro_start));
3318 macro_start->next = NULL;
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07003319 make_tok_num(macro_start, nasm_unquote(t->text, NULL));
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003320 macro_start->a.mac = NULL;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003321
H. Peter Anvine2c80182005-01-15 22:15:51 +00003322 /*
3323 * We now have a macro name, an implicit parameter count of
3324 * zero, and a numeric token to use as an expansion. Create
3325 * and store an SMacro.
3326 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003327 define_smacro(ctx, mname, casesense, 0, macro_start);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003328 free_tlist(tline);
3329 free_tlist(origline);
3330 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003331
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003332 case PP_STRCAT:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003333 casesense = true;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003334
3335 tline = tline->next;
3336 skip_white_(tline);
3337 tline = expand_id(tline);
3338 if (!tline || (tline->type != TOK_ID &&
3339 (tline->type != TOK_PREPROC_ID ||
3340 tline->text[1] != '$'))) {
H. Peter Anvin215186f2016-02-17 20:27:41 -08003341 nasm_error(ERR_NONFATAL,
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003342 "`%%strcat' expects a macro identifier as first parameter");
3343 free_tlist(origline);
3344 return DIRECTIVE_FOUND;
3345 }
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04003346 ctx = get_ctx(tline->text, &mname);
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003347 last = tline;
3348 tline = expand_smacro(tline->next);
3349 last->next = NULL;
3350
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003351 len = 0;
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04003352 list_for_each(t, tline) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003353 switch (t->type) {
3354 case TOK_WHITESPACE:
3355 break;
3356 case TOK_STRING:
3357 len += t->a.len = nasm_unquote(t->text, NULL);
3358 break;
3359 case TOK_OTHER:
3360 if (!strcmp(t->text, ",")) /* permit comma separators */
3361 break;
3362 /* else fall through */
3363 default:
H. Peter Anvin215186f2016-02-17 20:27:41 -08003364 nasm_error(ERR_NONFATAL,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003365 "non-string passed to `%%strcat' (%d)", t->type);
3366 free_tlist(tline);
3367 free_tlist(origline);
3368 return DIRECTIVE_FOUND;
3369 }
3370 }
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003371
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003372 p = pp = nasm_malloc(len);
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04003373 list_for_each(t, tline) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003374 if (t->type == TOK_STRING) {
3375 memcpy(p, t->text, t->a.len);
3376 p += t->a.len;
3377 }
3378 }
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003379
3380 /*
3381 * We now have a macro name, an implicit parameter count of
3382 * zero, and a numeric token to use as an expansion. Create
3383 * and store an SMacro.
3384 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003385 macro_start = new_Token(NULL, TOK_STRING, NULL, 0);
3386 macro_start->text = nasm_quote(pp, len);
3387 nasm_free(pp);
3388 define_smacro(ctx, mname, casesense, 0, macro_start);
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003389 free_tlist(tline);
3390 free_tlist(origline);
3391 return DIRECTIVE_FOUND;
3392
H. Peter Anvine2c80182005-01-15 22:15:51 +00003393 case PP_SUBSTR:
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003394 {
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003395 int64_t start, count;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003396 size_t len;
H. Peter Anvind2456592008-06-19 15:04:18 -07003397
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003398 casesense = true;
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07003399
H. Peter Anvine2c80182005-01-15 22:15:51 +00003400 tline = tline->next;
3401 skip_white_(tline);
3402 tline = expand_id(tline);
3403 if (!tline || (tline->type != TOK_ID &&
3404 (tline->type != TOK_PREPROC_ID ||
3405 tline->text[1] != '$'))) {
H. Peter Anvin215186f2016-02-17 20:27:41 -08003406 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00003407 "`%%substr' expects a macro identifier as first parameter");
3408 free_tlist(origline);
3409 return DIRECTIVE_FOUND;
3410 }
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04003411 ctx = get_ctx(tline->text, &mname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003412 last = tline;
3413 tline = expand_smacro(tline->next);
3414 last->next = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003415
Cyrill Gorcunov35519d62010-09-06 23:49:52 +04003416 if (tline) /* skip expanded id */
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003417 t = tline->next;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003418 while (tok_type_(t, TOK_WHITESPACE))
3419 t = t->next;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003420
H. Peter Anvine2c80182005-01-15 22:15:51 +00003421 /* t should now point to the string */
Cyrill Gorcunov35519d62010-09-06 23:49:52 +04003422 if (!tok_type_(t, TOK_STRING)) {
H. Peter Anvin215186f2016-02-17 20:27:41 -08003423 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00003424 "`%%substr` requires string as second parameter");
3425 free_tlist(tline);
3426 free_tlist(origline);
3427 return DIRECTIVE_FOUND;
3428 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003429
H. Peter Anvine2c80182005-01-15 22:15:51 +00003430 tt = t->next;
3431 tptr = &tt;
3432 tokval.t_type = TOKEN_INVALID;
H. Peter Anvin215186f2016-02-17 20:27:41 -08003433 evalresult = evaluate(ppscan, tptr, &tokval, NULL, pass, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003434 if (!evalresult) {
3435 free_tlist(tline);
3436 free_tlist(origline);
3437 return DIRECTIVE_FOUND;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003438 } else if (!is_simple(evalresult)) {
H. Peter Anvin215186f2016-02-17 20:27:41 -08003439 nasm_error(ERR_NONFATAL, "non-constant value given to `%%substr`");
H. Peter Anvine2c80182005-01-15 22:15:51 +00003440 free_tlist(tline);
3441 free_tlist(origline);
3442 return DIRECTIVE_FOUND;
3443 }
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003444 start = evalresult->value - 1;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003445
3446 while (tok_type_(tt, TOK_WHITESPACE))
3447 tt = tt->next;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003448 if (!tt) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003449 count = 1; /* Backwards compatibility: one character */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003450 } else {
3451 tokval.t_type = TOKEN_INVALID;
H. Peter Anvin215186f2016-02-17 20:27:41 -08003452 evalresult = evaluate(ppscan, tptr, &tokval, NULL, pass, NULL);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003453 if (!evalresult) {
3454 free_tlist(tline);
3455 free_tlist(origline);
3456 return DIRECTIVE_FOUND;
3457 } else if (!is_simple(evalresult)) {
H. Peter Anvin215186f2016-02-17 20:27:41 -08003458 nasm_error(ERR_NONFATAL, "non-constant value given to `%%substr`");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003459 free_tlist(tline);
3460 free_tlist(origline);
3461 return DIRECTIVE_FOUND;
3462 }
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003463 count = evalresult->value;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003464 }
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003465
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003466 len = nasm_unquote(t->text, NULL);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003467
Cyrill Gorcunovcff031e2010-09-07 20:31:11 +04003468 /* make start and count being in range */
3469 if (start < 0)
3470 start = 0;
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003471 if (count < 0)
3472 count = len + count + 1 - start;
3473 if (start + count > (int64_t)len)
Cyrill Gorcunovcff031e2010-09-07 20:31:11 +04003474 count = len - start;
3475 if (!len || count < 0 || start >=(int64_t)len)
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003476 start = -1, count = 0; /* empty string */
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003477
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003478 macro_start = nasm_malloc(sizeof(*macro_start));
3479 macro_start->next = NULL;
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003480 macro_start->text = nasm_quote((start < 0) ? "" : t->text + start, count);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003481 macro_start->type = TOK_STRING;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003482 macro_start->a.mac = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003483
H. Peter Anvine2c80182005-01-15 22:15:51 +00003484 /*
3485 * We now have a macro name, an implicit parameter count of
3486 * zero, and a numeric token to use as an expansion. Create
3487 * and store an SMacro.
3488 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003489 define_smacro(ctx, mname, casesense, 0, macro_start);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003490 free_tlist(tline);
3491 free_tlist(origline);
3492 return DIRECTIVE_FOUND;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003493 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003494
H. Peter Anvine2c80182005-01-15 22:15:51 +00003495 case PP_ASSIGN:
3496 case PP_IASSIGN:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003497 casesense = (i == PP_ASSIGN);
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07003498
H. Peter Anvine2c80182005-01-15 22:15:51 +00003499 tline = tline->next;
3500 skip_white_(tline);
3501 tline = expand_id(tline);
3502 if (!tline || (tline->type != TOK_ID &&
3503 (tline->type != TOK_PREPROC_ID ||
3504 tline->text[1] != '$'))) {
H. Peter Anvin215186f2016-02-17 20:27:41 -08003505 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00003506 "`%%%sassign' expects a macro identifier",
3507 (i == PP_IASSIGN ? "i" : ""));
3508 free_tlist(origline);
3509 return DIRECTIVE_FOUND;
3510 }
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04003511 ctx = get_ctx(tline->text, &mname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003512 last = tline;
3513 tline = expand_smacro(tline->next);
3514 last->next = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003515
H. Peter Anvine2c80182005-01-15 22:15:51 +00003516 t = tline;
3517 tptr = &t;
3518 tokval.t_type = TOKEN_INVALID;
H. Peter Anvin215186f2016-02-17 20:27:41 -08003519 evalresult = evaluate(ppscan, tptr, &tokval, NULL, pass, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003520 free_tlist(tline);
3521 if (!evalresult) {
3522 free_tlist(origline);
3523 return DIRECTIVE_FOUND;
3524 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003525
H. Peter Anvine2c80182005-01-15 22:15:51 +00003526 if (tokval.t_type)
H. Peter Anvin215186f2016-02-17 20:27:41 -08003527 nasm_error(ERR_WARNING|ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +00003528 "trailing garbage after expression ignored");
H. Peter Anvin734b1882002-04-30 21:01:08 +00003529
H. Peter Anvine2c80182005-01-15 22:15:51 +00003530 if (!is_simple(evalresult)) {
H. Peter Anvin215186f2016-02-17 20:27:41 -08003531 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00003532 "non-constant value given to `%%%sassign'",
3533 (i == PP_IASSIGN ? "i" : ""));
3534 free_tlist(origline);
3535 return DIRECTIVE_FOUND;
3536 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003537
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003538 macro_start = nasm_malloc(sizeof(*macro_start));
3539 macro_start->next = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003540 make_tok_num(macro_start, reloc_value(evalresult));
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003541 macro_start->a.mac = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003542
H. Peter Anvine2c80182005-01-15 22:15:51 +00003543 /*
3544 * We now have a macro name, an implicit parameter count of
3545 * zero, and a numeric token to use as an expansion. Create
3546 * and store an SMacro.
3547 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003548 define_smacro(ctx, mname, casesense, 0, macro_start);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003549 free_tlist(origline);
3550 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003551
H. Peter Anvine2c80182005-01-15 22:15:51 +00003552 case PP_LINE:
3553 /*
3554 * Syntax is `%line nnn[+mmm] [filename]'
3555 */
3556 tline = tline->next;
3557 skip_white_(tline);
3558 if (!tok_type_(tline, TOK_NUMBER)) {
H. Peter Anvin215186f2016-02-17 20:27:41 -08003559 nasm_error(ERR_NONFATAL, "`%%line' expects line number");
H. Peter Anvine2c80182005-01-15 22:15:51 +00003560 free_tlist(origline);
3561 return DIRECTIVE_FOUND;
3562 }
H. Peter Anvin70055962007-10-11 00:05:31 -07003563 k = readnum(tline->text, &err);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003564 m = 1;
3565 tline = tline->next;
3566 if (tok_is_(tline, "+")) {
3567 tline = tline->next;
3568 if (!tok_type_(tline, TOK_NUMBER)) {
H. Peter Anvin215186f2016-02-17 20:27:41 -08003569 nasm_error(ERR_NONFATAL, "`%%line' expects line increment");
H. Peter Anvine2c80182005-01-15 22:15:51 +00003570 free_tlist(origline);
3571 return DIRECTIVE_FOUND;
3572 }
H. Peter Anvin70055962007-10-11 00:05:31 -07003573 m = readnum(tline->text, &err);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003574 tline = tline->next;
3575 }
3576 skip_white_(tline);
3577 src_set_linnum(k);
3578 istk->lineinc = m;
3579 if (tline) {
H. Peter Anvin274cda82016-05-10 02:56:29 -07003580 char *fname = detoken(tline, false);
3581 src_set_fname(fname);
3582 nasm_free(fname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003583 }
3584 free_tlist(origline);
3585 return DIRECTIVE_FOUND;
Keith Kaniosb307a4f2010-11-06 17:41:51 -05003586
H. Peter Anvine2c80182005-01-15 22:15:51 +00003587 default:
H. Peter Anvin215186f2016-02-17 20:27:41 -08003588 nasm_error(ERR_FATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00003589 "preprocessor directive `%s' not yet implemented",
H. Peter Anvin4169a472007-09-12 01:29:43 +00003590 pp_directives[i]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003591 return DIRECTIVE_FOUND;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003592 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003593}
3594
3595/*
H. Peter Anvin76690a12002-04-30 20:52:49 +00003596 * Ensure that a macro parameter contains a condition code and
3597 * nothing else. Return the condition code index if so, or -1
3598 * otherwise.
3599 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003600static int find_cc(Token * t)
H. Peter Anvineba20a72002-04-30 20:53:55 +00003601{
H. Peter Anvin76690a12002-04-30 20:52:49 +00003602 Token *tt;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003603
H. Peter Anvin25a99342007-09-22 17:45:45 -07003604 if (!t)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003605 return -1; /* Probably a %+ without a space */
H. Peter Anvin25a99342007-09-22 17:45:45 -07003606
H. Peter Anvineba20a72002-04-30 20:53:55 +00003607 skip_white_(t);
H. Peter Anvin76690a12002-04-30 20:52:49 +00003608 if (t->type != TOK_ID)
H. Peter Anvine2c80182005-01-15 22:15:51 +00003609 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003610 tt = t->next;
H. Peter Anvineba20a72002-04-30 20:53:55 +00003611 skip_white_(tt);
H. Peter Anvin76690a12002-04-30 20:52:49 +00003612 if (tt && (tt->type != TOK_OTHER || strcmp(tt->text, ",")))
H. Peter Anvine2c80182005-01-15 22:15:51 +00003613 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003614
Cyrill Gorcunov19456392012-05-02 00:18:56 +04003615 return bsii(t->text, (const char **)conditions, ARRAY_SIZE(conditions));
H. Peter Anvin76690a12002-04-30 20:52:49 +00003616}
3617
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003618/*
3619 * This routines walks over tokens strem and hadnles tokens
3620 * pasting, if @handle_explicit passed then explicit pasting
3621 * term is handled, otherwise -- implicit pastings only.
3622 */
Cyrill Gorcunov575d4282010-10-06 00:25:55 +04003623static bool paste_tokens(Token **head, const struct tokseq_match *m,
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003624 size_t mnum, bool handle_explicit)
H. Peter Anvind784a082009-04-20 14:01:18 -07003625{
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003626 Token *tok, *next, **prev_next, **prev_nonspace;
3627 bool pasted = false;
3628 char *buf, *p;
3629 size_t len, i;
H. Peter Anvind784a082009-04-20 14:01:18 -07003630
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003631 /*
3632 * The last token before pasting. We need it
3633 * to be able to connect new handled tokens.
3634 * In other words if there were a tokens stream
3635 *
3636 * A -> B -> C -> D
3637 *
3638 * and we've joined tokens B and C, the resulting
3639 * stream should be
3640 *
3641 * A -> BC -> D
3642 */
3643 tok = *head;
3644 prev_next = NULL;
3645
3646 if (!tok_type_(tok, TOK_WHITESPACE) && !tok_type_(tok, TOK_PASTE))
3647 prev_nonspace = head;
3648 else
3649 prev_nonspace = NULL;
3650
3651 while (tok && (next = tok->next)) {
3652
3653 switch (tok->type) {
H. Peter Anvind784a082009-04-20 14:01:18 -07003654 case TOK_WHITESPACE:
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003655 /* Zap redundant whitespaces */
3656 while (tok_type_(next, TOK_WHITESPACE))
3657 next = delete_Token(next);
3658 tok->next = next;
H. Peter Anvind784a082009-04-20 14:01:18 -07003659 break;
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003660
3661 case TOK_PASTE:
3662 /* Explicit pasting */
3663 if (!handle_explicit)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003664 break;
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003665 next = delete_Token(tok);
3666
3667 while (tok_type_(next, TOK_WHITESPACE))
3668 next = delete_Token(next);
3669
3670 if (!pasted)
3671 pasted = true;
3672
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003673 /* Left pasting token is start of line */
3674 if (!prev_nonspace)
H. Peter Anvin215186f2016-02-17 20:27:41 -08003675 nasm_error(ERR_FATAL, "No lvalue found on pasting");
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003676
Cyrill Gorcunov8b5c9fb2013-02-04 01:24:54 +04003677 /*
3678 * No ending token, this might happen in two
3679 * cases
3680 *
3681 * 1) There indeed no right token at all
3682 * 2) There is a bare "%define ID" statement,
3683 * and @ID does expand to whitespace.
3684 *
3685 * So technically we need to do a grammar analysis
3686 * in another stage of parsing, but for now lets don't
3687 * change the behaviour people used to. Simply allow
3688 * whitespace after paste token.
3689 */
3690 if (!next) {
3691 /*
3692 * Zap ending space tokens and that's all.
3693 */
3694 tok = (*prev_nonspace)->next;
3695 while (tok_type_(tok, TOK_WHITESPACE))
3696 tok = delete_Token(tok);
3697 tok = *prev_nonspace;
3698 tok->next = NULL;
3699 break;
3700 }
3701
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003702 tok = *prev_nonspace;
3703 while (tok_type_(tok, TOK_WHITESPACE))
3704 tok = delete_Token(tok);
3705 len = strlen(tok->text);
3706 len += strlen(next->text);
3707
3708 p = buf = nasm_malloc(len + 1);
3709 strcpy(p, tok->text);
3710 p = strchr(p, '\0');
3711 strcpy(p, next->text);
3712
3713 delete_Token(tok);
3714
3715 tok = tokenize(buf);
3716 nasm_free(buf);
3717
3718 *prev_nonspace = tok;
3719 while (tok && tok->next)
3720 tok = tok->next;
3721
3722 tok->next = delete_Token(next);
3723
3724 /* Restart from pasted tokens head */
3725 tok = *prev_nonspace;
3726 break;
3727
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003728 default:
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003729 /* implicit pasting */
Cyrill Gorcunov575d4282010-10-06 00:25:55 +04003730 for (i = 0; i < mnum; i++) {
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003731 if (!(PP_CONCAT_MATCH(tok, m[i].mask_head)))
3732 continue;
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +04003733
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003734 len = 0;
3735 while (next && PP_CONCAT_MATCH(next, m[i].mask_tail)) {
3736 len += strlen(next->text);
3737 next = next->next;
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +04003738 }
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003739
3740 /* No match */
3741 if (tok == next)
3742 break;
3743
3744 len += strlen(tok->text);
3745 p = buf = nasm_malloc(len + 1);
3746
3747 while (tok != next) {
3748 strcpy(p, tok->text);
3749 p = strchr(p, '\0');
3750 tok = delete_Token(tok);
3751 }
3752
3753 tok = tokenize(buf);
3754 nasm_free(buf);
3755
3756 if (prev_next)
3757 *prev_next = tok;
3758 else
3759 *head = tok;
3760
3761 /*
3762 * Connect pasted into original stream,
3763 * ie A -> new-tokens -> B
3764 */
3765 while (tok && tok->next)
3766 tok = tok->next;
3767 tok->next = next;
3768
3769 if (!pasted)
3770 pasted = true;
3771
3772 /* Restart from pasted tokens head */
3773 tok = prev_next ? *prev_next : *head;
Cyrill Gorcunov575d4282010-10-06 00:25:55 +04003774 }
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003775
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003776 break;
H. Peter Anvind784a082009-04-20 14:01:18 -07003777 }
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003778
3779 prev_next = &tok->next;
3780
3781 if (tok->next &&
3782 !tok_type_(tok->next, TOK_WHITESPACE) &&
3783 !tok_type_(tok->next, TOK_PASTE))
3784 prev_nonspace = prev_next;
3785
3786 tok = tok->next;
H. Peter Anvind784a082009-04-20 14:01:18 -07003787 }
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003788
3789 return pasted;
H. Peter Anvind784a082009-04-20 14:01:18 -07003790}
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003791
3792/*
3793 * expands to a list of tokens from %{x:y}
3794 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003795static Token *expand_mmac_params_range(MMacro *mac, Token *tline, Token ***last)
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003796{
3797 Token *t = tline, **tt, *tm, *head;
3798 char *pos;
3799 int fst, lst, j, i;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003800
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003801 pos = strchr(tline->text, ':');
3802 nasm_assert(pos);
3803
3804 lst = atoi(pos + 1);
3805 fst = atoi(tline->text + 1);
3806
3807 /*
3808 * only macros params are accounted so
3809 * if someone passes %0 -- we reject such
3810 * value(s)
3811 */
3812 if (lst == 0 || fst == 0)
3813 goto err;
3814
3815 /* the values should be sane */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003816 if ((fst > (int)mac->nparam || fst < (-(int)mac->nparam)) ||
3817 (lst > (int)mac->nparam || lst < (-(int)mac->nparam)))
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003818 goto err;
3819
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003820 fst = fst < 0 ? fst + (int)mac->nparam + 1: fst;
3821 lst = lst < 0 ? lst + (int)mac->nparam + 1: lst;
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003822
3823 /* counted from zero */
3824 fst--, lst--;
3825
3826 /*
Cyrill Gorcunove75331c2013-11-09 12:02:15 +04003827 * It will be at least one token. Note we
3828 * need to scan params until separator, otherwise
3829 * only first token will be passed.
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003830 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003831 tm = mac->params[(fst + mac->rotate) % mac->nparam];
Cyrill Gorcunove75331c2013-11-09 12:02:15 +04003832 head = new_Token(NULL, tm->type, tm->text, 0);
3833 tt = &head->next, tm = tm->next;
3834 while (tok_isnt_(tm, ",")) {
3835 t = new_Token(NULL, tm->type, tm->text, 0);
3836 *tt = t, tt = &t->next, tm = tm->next;
3837 }
3838
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003839 if (fst < lst) {
3840 for (i = fst + 1; i <= lst; i++) {
3841 t = new_Token(NULL, TOK_OTHER, ",", 0);
3842 *tt = t, tt = &t->next;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003843 j = (i + mac->rotate) % mac->nparam;
3844 tm = mac->params[j];
Cyrill Gorcunove75331c2013-11-09 12:02:15 +04003845 while (tok_isnt_(tm, ",")) {
3846 t = new_Token(NULL, tm->type, tm->text, 0);
3847 *tt = t, tt = &t->next, tm = tm->next;
3848 }
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003849 }
3850 } else {
3851 for (i = fst - 1; i >= lst; i--) {
3852 t = new_Token(NULL, TOK_OTHER, ",", 0);
3853 *tt = t, tt = &t->next;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003854 j = (i + mac->rotate) % mac->nparam;
3855 tm = mac->params[j];
Cyrill Gorcunove75331c2013-11-09 12:02:15 +04003856 while (tok_isnt_(tm, ",")) {
3857 t = new_Token(NULL, tm->type, tm->text, 0);
3858 *tt = t, tt = &t->next, tm = tm->next;
3859 }
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003860 }
3861 }
3862
3863 *last = tt;
3864 return head;
3865
3866err:
H. Peter Anvin215186f2016-02-17 20:27:41 -08003867 nasm_error(ERR_NONFATAL, "`%%{%s}': macro parameters out of range",
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003868 &tline->text[1]);
3869 return tline;
3870}
3871
H. Peter Anvin76690a12002-04-30 20:52:49 +00003872/*
3873 * Expand MMacro-local things: parameter references (%0, %n, %+n,
H. Peter Anvin67c63722008-10-26 23:49:00 -07003874 * %-n) and MMacro-local identifiers (%%foo) as well as
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003875 * macro indirection (%[...]) and range (%{..:..}).
H. Peter Anvin76690a12002-04-30 20:52:49 +00003876 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003877static Token *expand_mmac_params(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00003878{
H. Peter Anvin734b1882002-04-30 21:01:08 +00003879 Token *t, *tt, **tail, *thead;
H. Peter Anvin6125b622009-04-08 14:02:25 -07003880 bool changed = false;
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003881 char *pos;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003882
3883 tail = &thead;
3884 thead = NULL;
3885
H. Peter Anvine2c80182005-01-15 22:15:51 +00003886 while (tline) {
3887 if (tline->type == TOK_PREPROC_ID &&
Cyrill Gorcunovca611192010-06-04 09:22:12 +04003888 (((tline->text[1] == '+' || tline->text[1] == '-') && tline->text[2]) ||
3889 (tline->text[1] >= '0' && tline->text[1] <= '9') ||
3890 tline->text[1] == '%')) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00003891 char *text = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003892 int type = 0, cc; /* type = 0 to placate optimisers */
Keith Kaniosa6dfa782007-04-13 16:47:53 +00003893 char tmpbuf[30];
H. Peter Anvin25a99342007-09-22 17:45:45 -07003894 unsigned int n;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003895 int i;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003896 MMacro *mac;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003897
H. Peter Anvine2c80182005-01-15 22:15:51 +00003898 t = tline;
3899 tline = tline->next;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003900
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003901 mac = istk->mstk;
3902 while (mac && !mac->name) /* avoid mistaking %reps for macros */
3903 mac = mac->next_active;
3904 if (!mac) {
H. Peter Anvin215186f2016-02-17 20:27:41 -08003905 nasm_error(ERR_NONFATAL, "`%s': not in a macro call", t->text);
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003906 } else {
3907 pos = strchr(t->text, ':');
3908 if (!pos) {
3909 switch (t->text[1]) {
3910 /*
3911 * We have to make a substitution of one of the
3912 * forms %1, %-1, %+1, %%foo, %0.
3913 */
3914 case '0':
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003915 type = TOK_NUMBER;
3916 snprintf(tmpbuf, sizeof(tmpbuf), "%d", mac->nparam);
3917 text = nasm_strdup(tmpbuf);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003918 break;
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003919 case '%':
H. Peter Anvine2c80182005-01-15 22:15:51 +00003920 type = TOK_ID;
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003921 snprintf(tmpbuf, sizeof(tmpbuf), "..@%"PRIu64".",
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003922 mac->unique);
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003923 text = nasm_strcat(tmpbuf, t->text + 2);
3924 break;
3925 case '-':
3926 n = atoi(t->text + 2) - 1;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003927 if (n >= mac->nparam)
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003928 tt = NULL;
3929 else {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003930 if (mac->nparam > 1)
3931 n = (n + mac->rotate) % mac->nparam;
3932 tt = mac->params[n];
H. Peter Anvine2c80182005-01-15 22:15:51 +00003933 }
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003934 cc = find_cc(tt);
3935 if (cc == -1) {
H. Peter Anvin215186f2016-02-17 20:27:41 -08003936 nasm_error(ERR_NONFATAL,
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003937 "macro parameter %d is not a condition code",
3938 n + 1);
3939 text = NULL;
3940 } else {
3941 type = TOK_ID;
3942 if (inverse_ccs[cc] == -1) {
H. Peter Anvin215186f2016-02-17 20:27:41 -08003943 nasm_error(ERR_NONFATAL,
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003944 "condition code `%s' is not invertible",
3945 conditions[cc]);
3946 text = NULL;
3947 } else
3948 text = nasm_strdup(conditions[inverse_ccs[cc]]);
3949 }
3950 break;
3951 case '+':
3952 n = atoi(t->text + 2) - 1;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003953 if (n >= mac->nparam)
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003954 tt = NULL;
3955 else {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003956 if (mac->nparam > 1)
3957 n = (n + mac->rotate) % mac->nparam;
3958 tt = mac->params[n];
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003959 }
3960 cc = find_cc(tt);
3961 if (cc == -1) {
H. Peter Anvin215186f2016-02-17 20:27:41 -08003962 nasm_error(ERR_NONFATAL,
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003963 "macro parameter %d is not a condition code",
3964 n + 1);
3965 text = NULL;
3966 } else {
3967 type = TOK_ID;
3968 text = nasm_strdup(conditions[cc]);
3969 }
3970 break;
3971 default:
3972 n = atoi(t->text + 1) - 1;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003973 if (n >= mac->nparam)
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003974 tt = NULL;
3975 else {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003976 if (mac->nparam > 1)
3977 n = (n + mac->rotate) % mac->nparam;
3978 tt = mac->params[n];
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003979 }
3980 if (tt) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003981 for (i = 0; i < mac->paramlen[n]; i++) {
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003982 *tail = new_Token(NULL, tt->type, tt->text, 0);
3983 tail = &(*tail)->next;
3984 tt = tt->next;
3985 }
3986 }
3987 text = NULL; /* we've done it here */
3988 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003989 }
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003990 } else {
3991 /*
3992 * seems we have a parameters range here
3993 */
3994 Token *head, **last;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003995 head = expand_mmac_params_range(mac, t, &last);
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003996 if (head != t) {
3997 *tail = head;
3998 *last = tline;
3999 tline = head;
4000 text = NULL;
4001 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00004002 }
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004003 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00004004 if (!text) {
4005 delete_Token(t);
4006 } else {
4007 *tail = t;
4008 tail = &t->next;
4009 t->type = type;
4010 nasm_free(t->text);
4011 t->text = text;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004012 t->a.mac = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004013 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004014 changed = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004015 continue;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004016 } else if (tline->type == TOK_INDIRECT) {
4017 t = tline;
4018 tline = tline->next;
4019 tt = tokenize(t->text);
4020 tt = expand_mmac_params(tt);
4021 tt = expand_smacro(tt);
4022 *tail = tt;
4023 while (tt) {
4024 tt->a.mac = NULL; /* Necessary? */
4025 tail = &tt->next;
4026 tt = tt->next;
4027 }
4028 delete_Token(t);
4029 changed = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004030 } else {
4031 t = *tail = tline;
4032 tline = tline->next;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004033 t->a.mac = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004034 tail = &t->next;
4035 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00004036 }
H. Peter Anvineba20a72002-04-30 20:53:55 +00004037 *tail = NULL;
H. Peter Anvin67c63722008-10-26 23:49:00 -07004038
Cyrill Gorcunovc6a742c2011-06-27 01:23:09 +04004039 if (changed) {
4040 const struct tokseq_match t[] = {
4041 {
4042 PP_CONCAT_MASK(TOK_ID) |
4043 PP_CONCAT_MASK(TOK_FLOAT), /* head */
4044 PP_CONCAT_MASK(TOK_ID) |
4045 PP_CONCAT_MASK(TOK_NUMBER) |
4046 PP_CONCAT_MASK(TOK_FLOAT) |
4047 PP_CONCAT_MASK(TOK_OTHER) /* tail */
4048 },
4049 {
4050 PP_CONCAT_MASK(TOK_NUMBER), /* head */
4051 PP_CONCAT_MASK(TOK_NUMBER) /* tail */
4052 }
4053 };
4054 paste_tokens(&thead, t, ARRAY_SIZE(t), false);
4055 }
H. Peter Anvin6125b622009-04-08 14:02:25 -07004056
H. Peter Anvin76690a12002-04-30 20:52:49 +00004057 return thead;
4058}
4059
4060/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004061 * Expand all single-line macro calls made in the given line.
4062 * Return the expanded version of the line. The original is deemed
4063 * to be destroyed in the process. (In reality we'll just move
4064 * Tokens from input to output a lot of the time, rather than
4065 * actually bothering to destroy and replicate.)
4066 */
H. Peter Anvincb1cf592007-11-19 12:26:50 -08004067
H. Peter Anvine2c80182005-01-15 22:15:51 +00004068static Token *expand_smacro(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004069{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004070 Token *t, *tt, *mstart, **tail, *thead;
H. Peter Anvineba20a72002-04-30 20:53:55 +00004071 SMacro *head = NULL, *m;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004072 Token **params;
4073 int *paramsize;
H. Peter Anvin25a99342007-09-22 17:45:45 -07004074 unsigned int nparam, sparam;
H. Peter Anvind784a082009-04-20 14:01:18 -07004075 int brackets;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004076 Token *org_tline = tline;
4077 Context *ctx;
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08004078 const char *mname;
H. Peter Anvin2a15e692007-11-19 13:14:59 -08004079 int deadman = DEADMAN_LIMIT;
H. Peter Anvin8287daf2009-07-07 16:00:58 -07004080 bool expanded;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004081
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004082 /*
4083 * Trick: we should avoid changing the start token pointer since it can
4084 * be contained in "next" field of other token. Because of this
4085 * we allocate a copy of first token and work with it; at the end of
4086 * routine we copy it back
4087 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004088 if (org_tline) {
Cyrill Gorcunoved4a8052010-04-09 15:40:35 +04004089 tline = new_Token(org_tline->next, org_tline->type,
4090 org_tline->text, 0);
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004091 tline->a.mac = org_tline->a.mac;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004092 nasm_free(org_tline->text);
4093 org_tline->text = NULL;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004094 }
4095
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004096 expanded = true; /* Always expand %+ at least once */
H. Peter Anvin8287daf2009-07-07 16:00:58 -07004097
H. Peter Anvincb1cf592007-11-19 12:26:50 -08004098again:
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004099 thead = NULL;
Cyrill Gorcunoved4a8052010-04-09 15:40:35 +04004100 tail = &thead;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004101
H. Peter Anvine2c80182005-01-15 22:15:51 +00004102 while (tline) { /* main token loop */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004103 if (!--deadman) {
H. Peter Anvin215186f2016-02-17 20:27:41 -08004104 nasm_error(ERR_NONFATAL, "interminable macro recursion");
Cyrill Gorcunovbd38c8f2009-11-21 11:11:23 +03004105 goto err;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004106 }
H. Peter Anvincb1cf592007-11-19 12:26:50 -08004107
H. Peter Anvine2c80182005-01-15 22:15:51 +00004108 if ((mname = tline->text)) {
4109 /* if this token is a local macro, look in local context */
Cyrill Gorcunovc56d9ad2010-02-11 15:12:19 +03004110 if (tline->type == TOK_ID) {
4111 head = (SMacro *)hash_findix(&smacros, mname);
4112 } else if (tline->type == TOK_PREPROC_ID) {
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04004113 ctx = get_ctx(mname, &mname);
Cyrill Gorcunovc56d9ad2010-02-11 15:12:19 +03004114 head = ctx ? (SMacro *)hash_findix(&ctx->localmac, mname) : NULL;
4115 } else
4116 head = NULL;
H. Peter Anvin072771e2008-05-22 13:17:51 -07004117
H. Peter Anvine2c80182005-01-15 22:15:51 +00004118 /*
4119 * We've hit an identifier. As in is_mmacro below, we first
4120 * check whether the identifier is a single-line macro at
4121 * all, then think about checking for parameters if
4122 * necessary.
4123 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004124 list_for_each(m, head)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004125 if (!mstrcmp(m->name, mname, m->casesense))
4126 break;
4127 if (m) {
4128 mstart = tline;
4129 params = NULL;
4130 paramsize = NULL;
4131 if (m->nparam == 0) {
4132 /*
4133 * Simple case: the macro is parameterless. Discard the
4134 * one token that the macro call took, and push the
4135 * expansion back on the to-do stack.
4136 */
4137 if (!m->expansion) {
4138 if (!strcmp("__FILE__", m->name)) {
H. Peter Anvin274cda82016-05-10 02:56:29 -07004139 const char *file = src_get_fname();
4140 /* nasm_free(tline->text); here? */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004141 tline->text = nasm_quote(file, strlen(file));
4142 tline->type = TOK_STRING;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004143 continue;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004144 }
4145 if (!strcmp("__LINE__", m->name)) {
4146 nasm_free(tline->text);
4147 make_tok_num(tline, src_get_linnum());
4148 continue;
4149 }
4150 if (!strcmp("__BITS__", m->name)) {
4151 nasm_free(tline->text);
4152 make_tok_num(tline, globalbits);
4153 continue;
4154 }
4155 tline = delete_Token(tline);
4156 continue;
4157 }
4158 } else {
4159 /*
4160 * Complicated case: at least one macro with this name
H. Peter Anvine2c80182005-01-15 22:15:51 +00004161 * exists and takes parameters. We must find the
4162 * parameters in the call, count them, find the SMacro
4163 * that corresponds to that form of the macro call, and
4164 * substitute for the parameters when we expand. What a
4165 * pain.
4166 */
4167 /*tline = tline->next;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004168 skip_white_(tline); */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004169 do {
4170 t = tline->next;
4171 while (tok_type_(t, TOK_SMAC_END)) {
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004172 t->a.mac->in_progress = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004173 t->text = NULL;
4174 t = tline->next = delete_Token(t);
4175 }
4176 tline = t;
4177 } while (tok_type_(tline, TOK_WHITESPACE));
4178 if (!tok_is_(tline, "(")) {
4179 /*
4180 * This macro wasn't called with parameters: ignore
4181 * the call. (Behaviour borrowed from gnu cpp.)
4182 */
4183 tline = mstart;
4184 m = NULL;
4185 } else {
4186 int paren = 0;
4187 int white = 0;
4188 brackets = 0;
4189 nparam = 0;
4190 sparam = PARAM_DELTA;
4191 params = nasm_malloc(sparam * sizeof(Token *));
4192 params[0] = tline->next;
4193 paramsize = nasm_malloc(sparam * sizeof(int));
4194 paramsize[0] = 0;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07004195 while (true) { /* parameter loop */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004196 /*
4197 * For some unusual expansions
4198 * which concatenates function call
4199 */
4200 t = tline->next;
4201 while (tok_type_(t, TOK_SMAC_END)) {
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004202 t->a.mac->in_progress = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004203 t->text = NULL;
4204 t = tline->next = delete_Token(t);
4205 }
4206 tline = t;
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00004207
H. Peter Anvine2c80182005-01-15 22:15:51 +00004208 if (!tline) {
H. Peter Anvin215186f2016-02-17 20:27:41 -08004209 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00004210 "macro call expects terminating `)'");
4211 break;
4212 }
4213 if (tline->type == TOK_WHITESPACE
4214 && brackets <= 0) {
4215 if (paramsize[nparam])
4216 white++;
4217 else
4218 params[nparam] = tline->next;
4219 continue; /* parameter loop */
4220 }
4221 if (tline->type == TOK_OTHER
4222 && tline->text[1] == 0) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004223 char ch = tline->text[0];
H. Peter Anvine2c80182005-01-15 22:15:51 +00004224 if (ch == ',' && !paren && brackets <= 0) {
4225 if (++nparam >= sparam) {
4226 sparam += PARAM_DELTA;
4227 params = nasm_realloc(params,
Cyrill Gorcunoved4a8052010-04-09 15:40:35 +04004228 sparam * sizeof(Token *));
4229 paramsize = nasm_realloc(paramsize,
4230 sparam * sizeof(int));
H. Peter Anvine2c80182005-01-15 22:15:51 +00004231 }
4232 params[nparam] = tline->next;
4233 paramsize[nparam] = 0;
4234 white = 0;
4235 continue; /* parameter loop */
4236 }
4237 if (ch == '{' &&
4238 (brackets > 0 || (brackets == 0 &&
4239 !paramsize[nparam])))
4240 {
4241 if (!(brackets++)) {
4242 params[nparam] = tline->next;
4243 continue; /* parameter loop */
4244 }
4245 }
4246 if (ch == '}' && brackets > 0)
4247 if (--brackets == 0) {
4248 brackets = -1;
4249 continue; /* parameter loop */
4250 }
4251 if (ch == '(' && !brackets)
4252 paren++;
4253 if (ch == ')' && brackets <= 0)
4254 if (--paren < 0)
4255 break;
4256 }
4257 if (brackets < 0) {
4258 brackets = 0;
H. Peter Anvin215186f2016-02-17 20:27:41 -08004259 nasm_error(ERR_NONFATAL, "braces do not "
H. Peter Anvine2c80182005-01-15 22:15:51 +00004260 "enclose all of macro parameter");
4261 }
4262 paramsize[nparam] += white + 1;
4263 white = 0;
4264 } /* parameter loop */
4265 nparam++;
4266 while (m && (m->nparam != nparam ||
4267 mstrcmp(m->name, mname,
4268 m->casesense)))
4269 m = m->next;
4270 if (!m)
H. Peter Anvin215186f2016-02-17 20:27:41 -08004271 nasm_error(ERR_WARNING|ERR_PASS1|ERR_WARN_MNP,
H. Peter Anvine2c80182005-01-15 22:15:51 +00004272 "macro `%s' exists, "
4273 "but not taking %d parameters",
4274 mstart->text, nparam);
4275 }
4276 }
4277 if (m && m->in_progress)
4278 m = NULL;
4279 if (!m) { /* in progess or didn't find '(' or wrong nparam */
H. Peter Anvin70653092007-10-19 14:42:29 -07004280 /*
H. Peter Anvine2c80182005-01-15 22:15:51 +00004281 * Design question: should we handle !tline, which
4282 * indicates missing ')' here, or expand those
4283 * macros anyway, which requires the (t) test a few
H. Peter Anvin70653092007-10-19 14:42:29 -07004284 * lines down?
H. Peter Anvine2c80182005-01-15 22:15:51 +00004285 */
4286 nasm_free(params);
4287 nasm_free(paramsize);
4288 tline = mstart;
4289 } else {
4290 /*
4291 * Expand the macro: we are placed on the last token of the
4292 * call, so that we can easily split the call from the
4293 * following tokens. We also start by pushing an SMAC_END
4294 * token for the cycle removal.
4295 */
4296 t = tline;
4297 if (t) {
4298 tline = t->next;
4299 t->next = NULL;
4300 }
4301 tt = new_Token(tline, TOK_SMAC_END, NULL, 0);
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004302 tt->a.mac = m;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07004303 m->in_progress = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004304 tline = tt;
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04004305 list_for_each(t, m->expansion) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004306 if (t->type >= TOK_SMAC_PARAM) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00004307 Token *pcopy = tline, **ptail = &pcopy;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004308 Token *ttt, *pt;
4309 int i;
H. Peter Anvin734b1882002-04-30 21:01:08 +00004310
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004311 ttt = params[t->type - TOK_SMAC_PARAM];
4312 i = paramsize[t->type - TOK_SMAC_PARAM];
4313 while (--i >= 0) {
4314 pt = *ptail = new_Token(tline, ttt->type,
4315 ttt->text, 0);
4316 ptail = &pt->next;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004317 ttt = ttt->next;
4318 }
4319 tline = pcopy;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004320 } else if (t->type == TOK_PREPROC_Q) {
4321 tt = new_Token(tline, TOK_ID, mname, 0);
4322 tline = tt;
4323 } else if (t->type == TOK_PREPROC_QQ) {
4324 tt = new_Token(tline, TOK_ID, m->name, 0);
4325 tline = tt;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004326 } else {
4327 tt = new_Token(tline, t->type, t->text, 0);
4328 tline = tt;
4329 }
4330 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00004331
H. Peter Anvine2c80182005-01-15 22:15:51 +00004332 /*
4333 * Having done that, get rid of the macro call, and clean
4334 * up the parameters.
4335 */
4336 nasm_free(params);
4337 nasm_free(paramsize);
4338 free_tlist(mstart);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004339 expanded = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004340 continue; /* main token loop */
4341 }
4342 }
4343 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004344
H. Peter Anvine2c80182005-01-15 22:15:51 +00004345 if (tline->type == TOK_SMAC_END) {
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004346 tline->a.mac->in_progress = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004347 tline = delete_Token(tline);
4348 } else {
4349 t = *tail = tline;
4350 tline = tline->next;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004351 t->a.mac = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004352 t->next = NULL;
4353 tail = &t->next;
4354 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004355 }
4356
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004357 /*
4358 * Now scan the entire line and look for successive TOK_IDs that resulted
Keith Kaniosb7a89542007-04-12 02:40:54 +00004359 * after expansion (they can't be produced by tokenize()). The successive
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004360 * TOK_IDs should be concatenated.
4361 * Also we look for %+ tokens and concatenate the tokens before and after
4362 * them (without white spaces in between).
4363 */
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +04004364 if (expanded) {
Cyrill Gorcunovc6a742c2011-06-27 01:23:09 +04004365 const struct tokseq_match t[] = {
4366 {
4367 PP_CONCAT_MASK(TOK_ID) |
4368 PP_CONCAT_MASK(TOK_PREPROC_ID), /* head */
4369 PP_CONCAT_MASK(TOK_ID) |
4370 PP_CONCAT_MASK(TOK_PREPROC_ID) |
4371 PP_CONCAT_MASK(TOK_NUMBER) /* tail */
4372 }
4373 };
4374 if (paste_tokens(&thead, t, ARRAY_SIZE(t), true)) {
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +04004375 /*
4376 * If we concatenated something, *and* we had previously expanded
4377 * an actual macro, scan the lines again for macros...
4378 */
4379 tline = thead;
4380 expanded = false;
4381 goto again;
4382 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00004383 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004384
Cyrill Gorcunovbd38c8f2009-11-21 11:11:23 +03004385err:
H. Peter Anvine2c80182005-01-15 22:15:51 +00004386 if (org_tline) {
4387 if (thead) {
4388 *org_tline = *thead;
4389 /* since we just gave text to org_line, don't free it */
4390 thead->text = NULL;
4391 delete_Token(thead);
4392 } else {
4393 /* the expression expanded to empty line;
4394 we can't return NULL for some reasons
4395 we just set the line to a single WHITESPACE token. */
4396 memset(org_tline, 0, sizeof(*org_tline));
4397 org_tline->text = NULL;
4398 org_tline->type = TOK_WHITESPACE;
4399 }
4400 thead = org_tline;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004401 }
4402
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004403 return thead;
4404}
4405
4406/*
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004407 * Similar to expand_smacro but used exclusively with macro identifiers
4408 * right before they are fetched in. The reason is that there can be
4409 * identifiers consisting of several subparts. We consider that if there
4410 * are more than one element forming the name, user wants a expansion,
4411 * otherwise it will be left as-is. Example:
4412 *
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004413 * %define %$abc cde
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004414 *
4415 * the identifier %$abc will be left as-is so that the handler for %define
4416 * will suck it and define the corresponding value. Other case:
4417 *
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004418 * %define _%$abc cde
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004419 *
4420 * In this case user wants name to be expanded *before* %define starts
4421 * working, so we'll expand %$abc into something (if it has a value;
4422 * otherwise it will be left as-is) then concatenate all successive
4423 * PP_IDs into one.
4424 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004425static Token *expand_id(Token * tline)
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004426{
4427 Token *cur, *oldnext = NULL;
4428
H. Peter Anvin734b1882002-04-30 21:01:08 +00004429 if (!tline || !tline->next)
H. Peter Anvine2c80182005-01-15 22:15:51 +00004430 return tline;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004431
4432 cur = tline;
4433 while (cur->next &&
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004434 (cur->next->type == TOK_ID ||
4435 cur->next->type == TOK_PREPROC_ID
4436 || cur->next->type == TOK_NUMBER))
H. Peter Anvine2c80182005-01-15 22:15:51 +00004437 cur = cur->next;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004438
4439 /* If identifier consists of just one token, don't expand */
4440 if (cur == tline)
H. Peter Anvine2c80182005-01-15 22:15:51 +00004441 return tline;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004442
H. Peter Anvine2c80182005-01-15 22:15:51 +00004443 if (cur) {
4444 oldnext = cur->next; /* Detach the tail past identifier */
4445 cur->next = NULL; /* so that expand_smacro stops here */
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004446 }
4447
H. Peter Anvin734b1882002-04-30 21:01:08 +00004448 tline = expand_smacro(tline);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004449
H. Peter Anvine2c80182005-01-15 22:15:51 +00004450 if (cur) {
4451 /* expand_smacro possibly changhed tline; re-scan for EOL */
4452 cur = tline;
4453 while (cur && cur->next)
4454 cur = cur->next;
4455 if (cur)
4456 cur->next = oldnext;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004457 }
4458
4459 return tline;
4460}
4461
4462/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004463 * Determine whether the given line constitutes a multi-line macro
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004464 * call, and return the MMacro structure called if so. Doesn't have
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004465 * to check for an initial label - that's taken care of in
4466 * expand_mmacro - but must check numbers of parameters. Guaranteed
4467 * to be called with tline->type == TOK_ID, so the putative macro
4468 * name is easy to find.
4469 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004470static MMacro *is_mmacro(Token * tline, Token *** params_array)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004471{
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004472 MMacro *head, *m;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004473 Token **params;
4474 int nparam;
4475
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004476 head = (MMacro *) hash_findix(&mmacros, tline->text);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004477
4478 /*
4479 * Efficiency: first we see if any macro exists with the given
4480 * name. If not, we can return NULL immediately. _Then_ we
4481 * count the parameters, and then we look further along the
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004482 * list if necessary to find the proper MMacro.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004483 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004484 list_for_each(m, head)
4485 if (!mstrcmp(m->name, tline->text, m->casesense))
H. Peter Anvine2c80182005-01-15 22:15:51 +00004486 break;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004487 if (!m)
H. Peter Anvine2c80182005-01-15 22:15:51 +00004488 return NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004489
4490 /*
4491 * OK, we have a potential macro. Count and demarcate the
4492 * parameters.
4493 */
H. Peter Anvin734b1882002-04-30 21:01:08 +00004494 count_mmac_params(tline->next, &nparam, &params);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004495
4496 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004497 * So we know how many parameters we've got. Find the MMacro
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004498 * structure that handles this number.
4499 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004500 while (m) {
4501 if (m->nparam_min <= nparam
4502 && (m->plus || nparam <= m->nparam_max)) {
4503 /*
4504 * This one is right. Just check if cycle removal
4505 * prohibits us using it before we actually celebrate...
4506 */
4507 if (m->in_progress > m->max_depth) {
4508 if (m->max_depth > 0) {
H. Peter Anvin215186f2016-02-17 20:27:41 -08004509 nasm_error(ERR_WARNING,
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004510 "reached maximum recursion depth of %i",
4511 m->max_depth);
4512 }
4513 nasm_free(params);
4514 return NULL;
4515 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00004516 /*
4517 * It's right, and we can use it. Add its default
4518 * parameters to the end of our list if necessary.
4519 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004520 if (m->defaults && nparam < m->nparam_min + m->ndefs) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00004521 params =
4522 nasm_realloc(params,
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004523 ((m->nparam_min + m->ndefs +
H. Peter Anvine2c80182005-01-15 22:15:51 +00004524 1) * sizeof(*params)));
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004525 while (nparam < m->nparam_min + m->ndefs) {
4526 params[nparam] = m->defaults[nparam - m->nparam_min];
H. Peter Anvine2c80182005-01-15 22:15:51 +00004527 nparam++;
4528 }
4529 }
4530 /*
4531 * If we've gone over the maximum parameter count (and
4532 * we're in Plus mode), ignore parameters beyond
4533 * nparam_max.
4534 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004535 if (m->plus && nparam > m->nparam_max)
4536 nparam = m->nparam_max;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004537 /*
4538 * Then terminate the parameter list, and leave.
4539 */
4540 if (!params) { /* need this special case */
4541 params = nasm_malloc(sizeof(*params));
4542 nparam = 0;
4543 }
4544 params[nparam] = NULL;
4545 *params_array = params;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004546 return m;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004547 }
4548 /*
4549 * This one wasn't right: look for the next one with the
4550 * same name.
4551 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004552 list_for_each(m, m->next)
4553 if (!mstrcmp(m->name, tline->text, m->casesense))
H. Peter Anvine2c80182005-01-15 22:15:51 +00004554 break;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004555 }
4556
4557 /*
4558 * After all that, we didn't find one with the right number of
4559 * parameters. Issue a warning, and fail to expand the macro.
4560 */
H. Peter Anvin215186f2016-02-17 20:27:41 -08004561 nasm_error(ERR_WARNING|ERR_PASS1|ERR_WARN_MNP,
H. Peter Anvine2c80182005-01-15 22:15:51 +00004562 "macro `%s' exists, but not taking %d parameters",
4563 tline->text, nparam);
H. Peter Anvin734b1882002-04-30 21:01:08 +00004564 nasm_free(params);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004565 return NULL;
4566}
4567
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004568
4569/*
4570 * Save MMacro invocation specific fields in
4571 * preparation for a recursive macro expansion
4572 */
4573static void push_mmacro(MMacro *m)
4574{
4575 MMacroInvocation *i;
4576
4577 i = nasm_malloc(sizeof(MMacroInvocation));
4578 i->prev = m->prev;
4579 i->params = m->params;
4580 i->iline = m->iline;
4581 i->nparam = m->nparam;
4582 i->rotate = m->rotate;
4583 i->paramlen = m->paramlen;
4584 i->unique = m->unique;
4585 i->condcnt = m->condcnt;
4586 m->prev = i;
4587}
4588
4589
4590/*
4591 * Restore MMacro invocation specific fields that were
4592 * saved during a previous recursive macro expansion
4593 */
4594static void pop_mmacro(MMacro *m)
4595{
4596 MMacroInvocation *i;
4597
4598 if (m->prev) {
4599 i = m->prev;
4600 m->prev = i->prev;
4601 m->params = i->params;
4602 m->iline = i->iline;
4603 m->nparam = i->nparam;
4604 m->rotate = i->rotate;
4605 m->paramlen = i->paramlen;
4606 m->unique = i->unique;
4607 m->condcnt = i->condcnt;
4608 nasm_free(i);
4609 }
4610}
4611
4612
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004613/*
4614 * Expand the multi-line macro call made by the given line, if
4615 * there is one to be expanded. If there is, push the expansion on
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004616 * istk->expansion and return 1. Otherwise return 0.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004617 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004618static int expand_mmacro(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004619{
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004620 Token *startline = tline;
H. Peter Anvineba20a72002-04-30 20:53:55 +00004621 Token *label = NULL;
4622 int dont_prepend = 0;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004623 Token **params, *t, *tt;
4624 MMacro *m;
4625 Line *l, *ll;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004626 int i, nparam, *paramlen;
H. Peter Anvinc751e862008-06-09 10:18:45 -07004627 const char *mname;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004628
4629 t = tline;
H. Peter Anvineba20a72002-04-30 20:53:55 +00004630 skip_white_(t);
H. Peter Anvince2233b2008-05-25 21:57:00 -07004631 /* if (!tok_type_(t, TOK_ID)) Lino 02/25/02 */
H. Peter Anvindce1e2f2002-04-30 21:06:37 +00004632 if (!tok_type_(t, TOK_ID) && !tok_type_(t, TOK_PREPROC_ID))
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004633 return 0;
4634 m = is_mmacro(t, &params);
4635 if (m) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004636 mname = t->text;
H. Peter Anvinc751e862008-06-09 10:18:45 -07004637 } else {
H. Peter Anvine2c80182005-01-15 22:15:51 +00004638 Token *last;
4639 /*
4640 * We have an id which isn't a macro call. We'll assume
4641 * it might be a label; we'll also check to see if a
4642 * colon follows it. Then, if there's another id after
4643 * that lot, we'll check it again for macro-hood.
4644 */
4645 label = last = t;
4646 t = t->next;
4647 if (tok_type_(t, TOK_WHITESPACE))
4648 last = t, t = t->next;
4649 if (tok_is_(t, ":")) {
4650 dont_prepend = 1;
4651 last = t, t = t->next;
4652 if (tok_type_(t, TOK_WHITESPACE))
4653 last = t, t = t->next;
4654 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004655 if (!tok_type_(t, TOK_ID) || !(m = is_mmacro(t, &params)))
4656 return 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004657 last->next = NULL;
Keith Kanios891775e2009-07-11 06:08:54 -05004658 mname = t->text;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004659 tline = t;
H. Peter Anvineba20a72002-04-30 20:53:55 +00004660 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004661
4662 /*
4663 * Fix up the parameters: this involves stripping leading and
4664 * trailing whitespace, then stripping braces if they are
4665 * present.
4666 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004667 for (nparam = 0; params[nparam]; nparam++) ;
H. Peter Anvin734b1882002-04-30 21:01:08 +00004668 paramlen = nparam ? nasm_malloc(nparam * sizeof(*paramlen)) : NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004669
H. Peter Anvine2c80182005-01-15 22:15:51 +00004670 for (i = 0; params[i]; i++) {
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08004671 int brace = 0;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004672 int comma = (!m->plus || i < nparam - 1);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004673
H. Peter Anvine2c80182005-01-15 22:15:51 +00004674 t = params[i];
4675 skip_white_(t);
4676 if (tok_is_(t, "{"))
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08004677 t = t->next, brace++, comma = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004678 params[i] = t;
4679 paramlen[i] = 0;
4680 while (t) {
4681 if (comma && t->type == TOK_OTHER && !strcmp(t->text, ","))
4682 break; /* ... because we have hit a comma */
4683 if (comma && t->type == TOK_WHITESPACE
4684 && tok_is_(t->next, ","))
4685 break; /* ... or a space then a comma */
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08004686 if (brace && t->type == TOK_OTHER) {
4687 if (t->text[0] == '{')
4688 brace++; /* ... or a nested opening brace */
4689 else if (t->text[0] == '}')
4690 if (!--brace)
4691 break; /* ... or a brace */
4692 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00004693 t = t->next;
4694 paramlen[i]++;
4695 }
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08004696 if (brace)
H. Peter Anvin215186f2016-02-17 20:27:41 -08004697 nasm_error(ERR_NONFATAL, "macro params should be enclosed in braces");
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004698 }
4699
4700 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004701 * OK, we have a MMacro structure together with a set of
4702 * parameters. We must now go through the expansion and push
4703 * copies of each Line on to istk->expansion. Substitution of
H. Peter Anvin76690a12002-04-30 20:52:49 +00004704 * parameter tokens and macro-local tokens doesn't get done
4705 * until the single-line macro substitution process; this is
4706 * because delaying them allows us to change the semantics
4707 * later through %rotate.
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004708 *
4709 * First, push an end marker on to istk->expansion, mark this
4710 * macro as in progress, and set up its invocation-specific
4711 * variables.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004712 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004713 ll = nasm_malloc(sizeof(Line));
4714 ll->next = istk->expansion;
4715 ll->finishes = m;
4716 ll->first = NULL;
4717 istk->expansion = ll;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004718
4719 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004720 * Save the previous MMacro expansion in the case of
4721 * macro recursion
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004722 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004723 if (m->max_depth && m->in_progress)
4724 push_mmacro(m);
4725
4726 m->in_progress ++;
4727 m->params = params;
4728 m->iline = tline;
4729 m->nparam = nparam;
4730 m->rotate = 0;
4731 m->paramlen = paramlen;
4732 m->unique = unique++;
4733 m->lineno = 0;
4734 m->condcnt = 0;
4735
4736 m->next_active = istk->mstk;
4737 istk->mstk = m;
4738
4739 list_for_each(l, m->expansion) {
4740 Token **tail;
4741
4742 ll = nasm_malloc(sizeof(Line));
4743 ll->finishes = NULL;
4744 ll->next = istk->expansion;
4745 istk->expansion = ll;
4746 tail = &ll->first;
4747
4748 list_for_each(t, l->first) {
4749 Token *x = t;
4750 switch (t->type) {
4751 case TOK_PREPROC_Q:
4752 tt = *tail = new_Token(NULL, TOK_ID, mname, 0);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004753 break;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004754 case TOK_PREPROC_QQ:
4755 tt = *tail = new_Token(NULL, TOK_ID, m->name, 0);
4756 break;
4757 case TOK_PREPROC_ID:
4758 if (t->text[1] == '0' && t->text[2] == '0') {
4759 dont_prepend = -1;
4760 x = label;
4761 if (!x)
4762 continue;
4763 }
4764 /* fall through */
4765 default:
4766 tt = *tail = new_Token(NULL, x->type, x->text, 0);
4767 break;
4768 }
4769 tail = &tt->next;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004770 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004771 *tail = NULL;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004772 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004773
4774 /*
H. Peter Anvineba20a72002-04-30 20:53:55 +00004775 * If we had a label, push it on as the first line of
4776 * the macro expansion.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004777 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004778 if (label) {
4779 if (dont_prepend < 0)
4780 free_tlist(startline);
4781 else {
4782 ll = nasm_malloc(sizeof(Line));
4783 ll->finishes = NULL;
4784 ll->next = istk->expansion;
4785 istk->expansion = ll;
4786 ll->first = startline;
4787 if (!dont_prepend) {
4788 while (label->next)
4789 label = label->next;
4790 label->next = tt = new_Token(NULL, TOK_OTHER, ":", 0);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004791 }
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004792 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004793 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004794
H. Peter Anvin172b8402016-02-18 01:16:18 -08004795 lfmt->uplevel(m->nolist ? LIST_MACRO_NOLIST : LIST_MACRO);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004796
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004797 return 1;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004798}
4799
H. Peter Anvin215186f2016-02-17 20:27:41 -08004800/*
4801 * This function adds macro names to error messages, and suppresses
4802 * them if necessary.
4803 */
4804static void pp_verror(int severity, const char *fmt, va_list arg)
Victor van den Elzen3b404c02008-09-18 13:51:36 +02004805{
H. Peter Anvin215186f2016-02-17 20:27:41 -08004806 char buff[BUFSIZ];
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004807 MMacro *mmac = NULL;
4808 int delta = 0;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02004809
H. Peter Anvin215186f2016-02-17 20:27:41 -08004810 /*
4811 * If we're in a dead branch of IF or something like it, ignore the error.
4812 * However, because %else etc are evaluated in the state context
4813 * of the previous branch, errors might get lost:
4814 * %if 0 ... %else trailing garbage ... %endif
4815 * So %else etc should set the ERR_PP_PRECOND flag.
4816 */
4817 if ((severity & ERR_MASK) < ERR_FATAL &&
4818 istk && istk->conds &&
4819 ((severity & ERR_PP_PRECOND) ?
4820 istk->conds->state == COND_NEVER :
H. Peter Anvineb6653f2016-04-05 13:03:10 -07004821 !emitting(istk->conds->state)))
H. Peter Anvin215186f2016-02-17 20:27:41 -08004822 return;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02004823
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004824 /* get %macro name */
H. Peter Anvin215186f2016-02-17 20:27:41 -08004825 if (!(severity & ERR_NOFILE) && istk && istk->mstk) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004826 mmac = istk->mstk;
4827 /* but %rep blocks should be skipped */
4828 while (mmac && !mmac->name)
4829 mmac = mmac->next_active, delta++;
Cyrill Gorcunov9900c6b2011-10-09 18:58:46 +04004830 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004831
H. Peter Anvin215186f2016-02-17 20:27:41 -08004832 if (mmac) {
4833 vsnprintf(buff, sizeof(buff), fmt, arg);
Victor van den Elzen3b404c02008-09-18 13:51:36 +02004834
H. Peter Anvin215186f2016-02-17 20:27:41 -08004835 nasm_set_verror(real_verror);
4836 nasm_error(severity, "(%s:%d) %s",
4837 mmac->name, mmac->lineno - delta, buff);
4838 nasm_set_verror(pp_verror);
4839 } else {
4840 real_verror(severity, fmt, arg);
4841 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004842}
4843
H. Peter Anvin734b1882002-04-30 21:01:08 +00004844static void
H. Peter Anvin215186f2016-02-17 20:27:41 -08004845pp_reset(char *file, int apass, StrList **deplist)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004846{
H. Peter Anvin7383b402008-09-24 10:20:40 -07004847 Token *t;
4848
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004849 cstk = NULL;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004850 istk = nasm_malloc(sizeof(Include));
4851 istk->next = NULL;
4852 istk->conds = NULL;
4853 istk->expansion = NULL;
4854 istk->mstk = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004855 istk->fp = fopen(file, "r");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004856 istk->fname = NULL;
H. Peter Anvin274cda82016-05-10 02:56:29 -07004857 src_set(0, file);
H. Peter Anvineba20a72002-04-30 20:53:55 +00004858 istk->lineinc = 1;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004859 if (!istk->fp)
H. Peter Anvin215186f2016-02-17 20:27:41 -08004860 nasm_fatal(ERR_NOFILE, "unable to open input file `%s'", file);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004861 defining = NULL;
Charles Crayned4200be2008-07-12 16:42:33 -07004862 nested_mac_count = 0;
4863 nested_rep_count = 0;
H. Peter Anvin97a23472007-09-16 17:57:25 -07004864 init_macros();
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004865 unique = 0;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004866 if (tasm_compatible_mode) {
H. Peter Anvina4835d42008-05-20 14:21:29 -07004867 stdmacpos = nasm_stdmac;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004868 } else {
H. Peter Anvina4835d42008-05-20 14:21:29 -07004869 stdmacpos = nasm_stdmac_after_tasm;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004870 }
H. Peter Anvind2456592008-06-19 15:04:18 -07004871 any_extrastdmac = extrastdmac && *extrastdmac;
4872 do_predef = true;
H. Peter Anvin61f130f2008-09-25 15:45:06 -07004873
4874 /*
4875 * 0 for dependencies, 1 for preparatory passes, 2 for final pass.
4876 * The caller, however, will also pass in 3 for preprocess-only so
4877 * we can set __PASS__ accordingly.
4878 */
4879 pass = apass > 2 ? 2 : apass;
4880
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07004881 dephead = deptail = deplist;
4882 if (deplist) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004883 StrList *sl = nasm_malloc(strlen(file)+1+sizeof sl->next);
4884 sl->next = NULL;
4885 strcpy(sl->str, file);
4886 *deptail = sl;
4887 deptail = &sl->next;
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07004888 }
H. Peter Anvin7383b402008-09-24 10:20:40 -07004889
H. Peter Anvin61f130f2008-09-25 15:45:06 -07004890 /*
4891 * Define the __PASS__ macro. This is defined here unlike
4892 * all the other builtins, because it is special -- it varies between
4893 * passes.
4894 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004895 t = nasm_malloc(sizeof(*t));
4896 t->next = NULL;
H. Peter Anvin61f130f2008-09-25 15:45:06 -07004897 make_tok_num(t, apass);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004898 t->a.mac = NULL;
H. Peter Anvin7383b402008-09-24 10:20:40 -07004899 define_smacro(NULL, "__PASS__", true, 0, t);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004900}
4901
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004902static char *pp_getline(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004903{
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004904 char *line;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004905 Token *tline;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004906
H. Peter Anvin215186f2016-02-17 20:27:41 -08004907 real_verror = nasm_set_verror(pp_verror);
4908
H. Peter Anvine2c80182005-01-15 22:15:51 +00004909 while (1) {
4910 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004911 * Fetch a tokenized line, either from the macro-expansion
H. Peter Anvine2c80182005-01-15 22:15:51 +00004912 * buffer or from the input file.
4913 */
4914 tline = NULL;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004915 while (istk->expansion && istk->expansion->finishes) {
4916 Line *l = istk->expansion;
4917 if (!l->finishes->name && l->finishes->in_progress > 1) {
4918 Line *ll;
H. Peter Anvineba20a72002-04-30 20:53:55 +00004919
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004920 /*
4921 * This is a macro-end marker for a macro with no
4922 * name, which means it's not really a macro at all
4923 * but a %rep block, and the `in_progress' field is
4924 * more than 1, meaning that we still need to
4925 * repeat. (1 means the natural last repetition; 0
4926 * means termination by %exitrep.) We have
4927 * therefore expanded up to the %endrep, and must
4928 * push the whole block on to the expansion buffer
4929 * again. We don't bother to remove the macro-end
4930 * marker: we'd only have to generate another one
4931 * if we did.
4932 */
4933 l->finishes->in_progress--;
4934 list_for_each(l, l->finishes->expansion) {
4935 Token *t, *tt, **tail;
4936
4937 ll = nasm_malloc(sizeof(Line));
4938 ll->next = istk->expansion;
4939 ll->finishes = NULL;
4940 ll->first = NULL;
4941 tail = &ll->first;
4942
4943 list_for_each(t, l->first) {
4944 if (t->text || t->type == TOK_WHITESPACE) {
4945 tt = *tail = new_Token(NULL, t->type, t->text, 0);
4946 tail = &tt->next;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004947 }
4948 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004949
4950 istk->expansion = ll;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004951 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004952 } else {
4953 /*
4954 * Check whether a `%rep' was started and not ended
4955 * within this macro expansion. This can happen and
4956 * should be detected. It's a fatal error because
4957 * I'm too confused to work out how to recover
4958 * sensibly from it.
4959 */
4960 if (defining) {
4961 if (defining->name)
H. Peter Anvin215186f2016-02-17 20:27:41 -08004962 nasm_panic(0, "defining with name in expansion");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004963 else if (istk->mstk->name)
H. Peter Anvin215186f2016-02-17 20:27:41 -08004964 nasm_fatal(0, "`%%rep' without `%%endrep' within"
4965 " expansion of macro `%s'",
4966 istk->mstk->name);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004967 }
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004968
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004969 /*
4970 * FIXME: investigate the relationship at this point between
4971 * istk->mstk and l->finishes
4972 */
4973 {
4974 MMacro *m = istk->mstk;
4975 istk->mstk = m->next_active;
4976 if (m->name) {
4977 /*
4978 * This was a real macro call, not a %rep, and
4979 * therefore the parameter information needs to
4980 * be freed.
4981 */
4982 if (m->prev) {
4983 pop_mmacro(m);
4984 l->finishes->in_progress --;
4985 } else {
4986 nasm_free(m->params);
4987 free_tlist(m->iline);
4988 nasm_free(m->paramlen);
4989 l->finishes->in_progress = 0;
4990 }
4991 } else
4992 free_mmacro(m);
4993 }
4994 istk->expansion = l->next;
4995 nasm_free(l);
H. Peter Anvin172b8402016-02-18 01:16:18 -08004996 lfmt->downlevel(LIST_MACRO);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004997 }
4998 }
4999 while (1) { /* until we get a line we can use */
5000
5001 if (istk->expansion) { /* from a macro expansion */
5002 char *p;
5003 Line *l = istk->expansion;
5004 if (istk->mstk)
5005 istk->mstk->lineno++;
5006 tline = l->first;
5007 istk->expansion = l->next;
5008 nasm_free(l);
5009 p = detoken(tline, false);
H. Peter Anvin172b8402016-02-18 01:16:18 -08005010 lfmt->line(LIST_MACRO, p);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005011 nasm_free(p);
5012 break;
5013 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00005014 line = read_line();
5015 if (line) { /* from the current input file */
5016 line = prepreproc(line);
Keith Kaniosb7a89542007-04-12 02:40:54 +00005017 tline = tokenize(line);
H. Peter Anvine2c80182005-01-15 22:15:51 +00005018 nasm_free(line);
5019 break;
5020 }
5021 /*
5022 * The current file has ended; work down the istk
5023 */
5024 {
5025 Include *i = istk;
5026 fclose(i->fp);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005027 if (i->conds) {
5028 /* nasm_error can't be conditionally suppressed */
H. Peter Anvin41087062016-03-03 14:27:34 -08005029 nasm_fatal(0,
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005030 "expected `%%endif' before end of file");
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005031 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00005032 /* only set line and file name if there's a next node */
H. Peter Anvin274cda82016-05-10 02:56:29 -07005033 if (i->next)
5034 src_set(i->lineno, i->fname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00005035 istk = i->next;
H. Peter Anvin172b8402016-02-18 01:16:18 -08005036 lfmt->downlevel(LIST_INCLUDE);
H. Peter Anvine2c80182005-01-15 22:15:51 +00005037 nasm_free(i);
H. Peter Anvin215186f2016-02-17 20:27:41 -08005038 if (!istk) {
5039 line = NULL;
5040 goto done;
5041 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005042 if (istk->expansion && istk->expansion->finishes)
5043 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005044 }
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005045 }
5046
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005047 /*
5048 * We must expand MMacro parameters and MMacro-local labels
5049 * _before_ we plunge into directive processing, to cope
5050 * with things like `%define something %1' such as STRUC
5051 * uses. Unless we're _defining_ a MMacro, in which case
5052 * those tokens should be left alone to go into the
5053 * definition; and unless we're in a non-emitting
5054 * condition, in which case we don't want to meddle with
5055 * anything.
5056 */
5057 if (!defining && !(istk->conds && !emitting(istk->conds->state))
5058 && !(istk->mstk && !istk->mstk->in_progress)) {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005059 tline = expand_mmac_params(tline);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005060 }
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005061
H. Peter Anvine2c80182005-01-15 22:15:51 +00005062 /*
5063 * Check the line to see if it's a preprocessor directive.
5064 */
5065 if (do_directive(tline) == DIRECTIVE_FOUND) {
5066 continue;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005067 } else if (defining) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00005068 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005069 * We're defining a multi-line macro. We emit nothing
5070 * at all, and just
5071 * shove the tokenized line on to the macro definition.
H. Peter Anvine2c80182005-01-15 22:15:51 +00005072 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005073 Line *l = nasm_malloc(sizeof(Line));
5074 l->next = defining->expansion;
5075 l->first = tline;
5076 l->finishes = NULL;
5077 defining->expansion = l;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005078 continue;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005079 } else if (istk->conds && !emitting(istk->conds->state)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00005080 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005081 * We're in a non-emitting branch of a condition block.
H. Peter Anvine2c80182005-01-15 22:15:51 +00005082 * Emit nothing at all, not even a blank line: when we
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005083 * emerge from the condition we'll give a line-number
H. Peter Anvine2c80182005-01-15 22:15:51 +00005084 * directive so we keep our place correctly.
5085 */
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005086 free_tlist(tline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00005087 continue;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005088 } else if (istk->mstk && !istk->mstk->in_progress) {
5089 /*
5090 * We're in a %rep block which has been terminated, so
5091 * we're walking through to the %endrep without
5092 * emitting anything. Emit nothing at all, not even a
5093 * blank line: when we emerge from the %rep block we'll
5094 * give a line-number directive so we keep our place
5095 * correctly.
5096 */
5097 free_tlist(tline);
5098 continue;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005099 } else {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005100 tline = expand_smacro(tline);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005101 if (!expand_mmacro(tline)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00005102 /*
Keith Kaniosb7a89542007-04-12 02:40:54 +00005103 * De-tokenize the line again, and emit it.
H. Peter Anvine2c80182005-01-15 22:15:51 +00005104 */
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005105 line = detoken(tline, true);
5106 free_tlist(tline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00005107 break;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005108 } else {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005109 continue; /* expand_mmacro calls free_tlist */
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005110 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00005111 }
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005112 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005113
H. Peter Anvin215186f2016-02-17 20:27:41 -08005114done:
5115 nasm_set_verror(real_verror);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005116 return line;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005117}
5118
H. Peter Anvine2c80182005-01-15 22:15:51 +00005119static void pp_cleanup(int pass)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005120{
H. Peter Anvin215186f2016-02-17 20:27:41 -08005121 real_verror = nasm_set_verror(pp_verror);
5122
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005123 if (defining) {
5124 if (defining->name) {
H. Peter Anvin215186f2016-02-17 20:27:41 -08005125 nasm_error(ERR_NONFATAL,
5126 "end of file while still defining macro `%s'",
5127 defining->name);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005128 } else {
H. Peter Anvin215186f2016-02-17 20:27:41 -08005129 nasm_error(ERR_NONFATAL, "end of file while still in %%rep");
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005130 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005131
5132 free_mmacro(defining);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005133 defining = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005134 }
H. Peter Anvin215186f2016-02-17 20:27:41 -08005135
5136 nasm_set_verror(real_verror);
5137
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005138 while (cstk)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005139 ctx_pop();
H. Peter Anvin97a23472007-09-16 17:57:25 -07005140 free_macros();
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005141 while (istk) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00005142 Include *i = istk;
5143 istk = istk->next;
5144 fclose(i->fp);
Cyrill Gorcunov8dcfd882011-03-03 09:18:56 +03005145 nasm_free(i);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005146 }
5147 while (cstk)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005148 ctx_pop();
H. Peter Anvin274cda82016-05-10 02:56:29 -07005149 src_set_fname(NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00005150 if (pass == 0) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03005151 IncPath *i;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005152 free_llist(predef);
Cyrill Gorcunovdae24d72014-06-28 10:17:39 +04005153 predef = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005154 delete_Blocks();
Cyrill Gorcunovdae24d72014-06-28 10:17:39 +04005155 freeTokens = NULL;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03005156 while ((i = ipath)) {
5157 ipath = i->next;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005158 if (i->path)
5159 nasm_free(i->path);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03005160 nasm_free(i);
5161 }
H. Peter Anvin11dfa1a2008-07-02 18:11:04 -07005162 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005163}
5164
Cyrill Gorcunov0b78bff2012-05-07 01:57:55 +04005165static void pp_include_path(char *path)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005166{
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005167 IncPath *i;
H. Peter Anvin37a321f2007-09-24 13:41:58 -07005168
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005169 i = nasm_malloc(sizeof(IncPath));
5170 i->path = path ? nasm_strdup(path) : NULL;
5171 i->next = NULL;
Frank Kotlerd0ed6fd2003-08-27 11:33:56 +00005172
H. Peter Anvin89cee572009-07-15 09:16:54 -04005173 if (ipath) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00005174 IncPath *j = ipath;
H. Peter Anvin89cee572009-07-15 09:16:54 -04005175 while (j->next)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005176 j = j->next;
5177 j->next = i;
5178 } else {
5179 ipath = i;
Frank Kotlerd0ed6fd2003-08-27 11:33:56 +00005180 }
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005181}
Frank Kotlerd0ed6fd2003-08-27 11:33:56 +00005182
Cyrill Gorcunov0b78bff2012-05-07 01:57:55 +04005183static void pp_pre_include(char *fname)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005184{
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005185 Token *inc, *space, *name;
5186 Line *l;
5187
H. Peter Anvin734b1882002-04-30 21:01:08 +00005188 name = new_Token(NULL, TOK_INTERNAL_STRING, fname, 0);
5189 space = new_Token(name, TOK_WHITESPACE, NULL, 0);
5190 inc = new_Token(space, TOK_PREPROC_ID, "%include", 0);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005191
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005192 l = nasm_malloc(sizeof(Line));
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005193 l->next = predef;
5194 l->first = inc;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005195 l->finishes = NULL;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005196 predef = l;
5197}
5198
Cyrill Gorcunov0b78bff2012-05-07 01:57:55 +04005199static void pp_pre_define(char *definition)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005200{
5201 Token *def, *space;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005202 Line *l;
Keith Kaniosa6dfa782007-04-13 16:47:53 +00005203 char *equals;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005204
H. Peter Anvin215186f2016-02-17 20:27:41 -08005205 real_verror = nasm_set_verror(pp_verror);
5206
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005207 equals = strchr(definition, '=');
H. Peter Anvin734b1882002-04-30 21:01:08 +00005208 space = new_Token(NULL, TOK_WHITESPACE, NULL, 0);
5209 def = new_Token(space, TOK_PREPROC_ID, "%define", 0);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005210 if (equals)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005211 *equals = ' ';
Keith Kaniosb7a89542007-04-12 02:40:54 +00005212 space->next = tokenize(definition);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005213 if (equals)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005214 *equals = '=';
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005215
Cyrill Gorcunov6d42e9b2015-02-08 11:07:17 +03005216 if (space->next->type != TOK_PREPROC_ID &&
5217 space->next->type != TOK_ID)
H. Peter Anvin215186f2016-02-17 20:27:41 -08005218 nasm_error(ERR_WARNING, "pre-defining non ID `%s\'\n", definition);
Cyrill Gorcunov6d42e9b2015-02-08 11:07:17 +03005219
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005220 l = nasm_malloc(sizeof(Line));
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005221 l->next = predef;
5222 l->first = def;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005223 l->finishes = NULL;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005224 predef = l;
H. Peter Anvin215186f2016-02-17 20:27:41 -08005225
5226 nasm_set_verror(real_verror);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005227}
5228
Cyrill Gorcunov0b78bff2012-05-07 01:57:55 +04005229static void pp_pre_undefine(char *definition)
H. Peter Anvin620515a2002-04-30 20:57:38 +00005230{
5231 Token *def, *space;
5232 Line *l;
5233
H. Peter Anvin734b1882002-04-30 21:01:08 +00005234 space = new_Token(NULL, TOK_WHITESPACE, NULL, 0);
5235 def = new_Token(space, TOK_PREPROC_ID, "%undef", 0);
Keith Kaniosb7a89542007-04-12 02:40:54 +00005236 space->next = tokenize(definition);
H. Peter Anvin620515a2002-04-30 20:57:38 +00005237
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005238 l = nasm_malloc(sizeof(Line));
H. Peter Anvin620515a2002-04-30 20:57:38 +00005239 l->next = predef;
5240 l->first = def;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005241 l->finishes = NULL;
H. Peter Anvin620515a2002-04-30 20:57:38 +00005242 predef = l;
5243}
5244
Cyrill Gorcunov0b78bff2012-05-07 01:57:55 +04005245static void pp_extra_stdmac(macros_t *macros)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005246{
H. Peter Anvin76690a12002-04-30 20:52:49 +00005247 extrastdmac = macros;
5248}
5249
Keith Kaniosa5fc6462007-10-13 07:09:22 -07005250static void make_tok_num(Token * tok, int64_t val)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005251{
Cyrill Gorcunovce652742013-05-06 23:43:43 +04005252 char numbuf[32];
Keith Kaniosa5fc6462007-10-13 07:09:22 -07005253 snprintf(numbuf, sizeof(numbuf), "%"PRId64"", val);
H. Peter Anvineba20a72002-04-30 20:53:55 +00005254 tok->text = nasm_strdup(numbuf);
5255 tok->type = TOK_NUMBER;
5256}
5257
H. Peter Anvin37368952016-05-09 14:10:32 -07005258static void pp_list_one_macro(MMacro *m, int severity)
5259{
5260 if (!m)
5261 return;
5262
5263 /* We need to print the next_active list in reverse order */
5264 pp_list_one_macro(m->next_active, severity);
5265
5266 if (m->name && !m->nolist) {
H. Peter Anvin274cda82016-05-10 02:56:29 -07005267 src_set(m->xline + m->lineno, m->fname);
H. Peter Anvin37368952016-05-09 14:10:32 -07005268 nasm_error(severity, "... from macro `%s' defined here", m->name);
5269 }
5270}
5271
H. Peter Anvin4def1a82016-05-09 13:59:44 -07005272static void pp_error_list_macros(int severity)
5273{
H. Peter Anvin4def1a82016-05-09 13:59:44 -07005274 int32_t saved_line;
5275 const char *saved_fname = NULL;
5276
5277 severity |= ERR_PP_LISTMACRO | ERR_NO_SEVERITY;
H. Peter Anvin274cda82016-05-10 02:56:29 -07005278 src_get(&saved_line, &saved_fname);
H. Peter Anvin4def1a82016-05-09 13:59:44 -07005279
H. Peter Anvin37368952016-05-09 14:10:32 -07005280 pp_list_one_macro(istk->mstk, severity);
H. Peter Anvin4def1a82016-05-09 13:59:44 -07005281
H. Peter Anvin274cda82016-05-10 02:56:29 -07005282 src_set(saved_line, saved_fname);
H. Peter Anvin4def1a82016-05-09 13:59:44 -07005283}
5284
5285const struct preproc_ops nasmpp = {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005286 pp_reset,
5287 pp_getline,
Cyrill Gorcunov0b78bff2012-05-07 01:57:55 +04005288 pp_cleanup,
5289 pp_extra_stdmac,
5290 pp_pre_define,
5291 pp_pre_undefine,
5292 pp_pre_include,
H. Peter Anvin4def1a82016-05-09 13:59:44 -07005293 pp_include_path,
5294 pp_error_list_macros,
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005295};