blob: 7b1e2bff88a2c7f16bd1f284efc8c9bbb2b20bee [file] [log] [blame]
H. Peter Anvin9e6747c2009-06-28 17:13:04 -07001/* ----------------------------------------------------------------------- *
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002 *
H. Peter Anvina6e26d92017-03-07 21:32:37 -08003 * Copyright 1996-2017 The NASM Authors - All Rights Reserved
H. Peter Anvin9e6747c2009-06-28 17:13:04 -07004 * See the file AUTHORS included with the NASM distribution for
5 * the specific copyright holders.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00006 *
H. Peter Anvin9e6747c2009-06-28 17:13:04 -07007 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following
9 * conditions are met:
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000010 *
H. Peter Anvin9e6747c2009-06-28 17:13:04 -070011 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above
14 * copyright notice, this list of conditions and the following
15 * disclaimer in the documentation and/or other materials provided
16 * with the distribution.
Cyrill Gorcunovaccda192010-02-16 10:27:56 +030017 *
H. Peter Anvin9e6747c2009-06-28 17:13:04 -070018 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
19 * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
20 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
21 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
23 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
26 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
29 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
30 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 *
32 * ----------------------------------------------------------------------- */
33
34/*
35 * preproc.c macro preprocessor for the Netwide Assembler
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000036 */
37
H. Peter Anvin4836e332002-04-30 20:56:43 +000038/* Typical flow of text through preproc
39 *
Keith Kaniosb7a89542007-04-12 02:40:54 +000040 * pp_getline gets tokenized lines, either
H. Peter Anvin4836e332002-04-30 20:56:43 +000041 *
42 * from a macro expansion
43 *
44 * or
45 * {
46 * read_line gets raw text from stdmacpos, or predef, or current input file
Keith Kaniosb7a89542007-04-12 02:40:54 +000047 * tokenize converts to tokens
H. Peter Anvin4836e332002-04-30 20:56:43 +000048 * }
49 *
50 * expand_mmac_params is used to expand %1 etc., unless a macro is being
51 * defined or a false conditional is being processed
52 * (%0, %1, %+1, %-1, %%foo
53 *
54 * do_directive checks for directives
55 *
56 * expand_smacro is used to expand single line macros
57 *
58 * expand_mmacro is used to expand multi-line macros
59 *
60 * detoken is used to convert the line back to text
61 */
H. Peter Anvineba20a72002-04-30 20:53:55 +000062
H. Peter Anvinfe501952007-10-02 21:53:51 -070063#include "compiler.h"
64
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000065#include <stdio.h>
H. Peter Anvinaf535c12002-04-30 20:59:21 +000066#include <stdarg.h>
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000067#include <stdlib.h>
68#include <stddef.h>
69#include <string.h>
70#include <ctype.h>
H. Peter Anvin76690a12002-04-30 20:52:49 +000071#include <limits.h>
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000072
73#include "nasm.h"
74#include "nasmlib.h"
H. Peter Anvinb20bc732017-03-07 19:23:03 -080075#include "error.h"
H. Peter Anvin4169a472007-09-12 01:29:43 +000076#include "preproc.h"
H. Peter Anvin97a23472007-09-16 17:57:25 -070077#include "hashtbl.h"
H. Peter Anvin8cad14b2008-06-01 17:23:51 -070078#include "quote.h"
H. Peter Anvinc2df2822007-10-24 15:29:28 -070079#include "stdscan.h"
H. Peter Anvindbb640b2009-07-18 18:57:16 -070080#include "eval.h"
H. Peter Anvinc2df2822007-10-24 15:29:28 -070081#include "tokens.h"
H. Peter Anvina4835d42008-05-20 14:21:29 -070082#include "tables.h"
H. Peter Anvin8ac25aa2016-02-18 01:16:18 -080083#include "listing.h"
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000084
85typedef struct SMacro SMacro;
H. Peter Anvin36206cd2012-03-03 16:14:51 -080086typedef struct MMacro MMacro;
87typedef struct MMacroInvocation MMacroInvocation;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000088typedef struct Context Context;
89typedef struct Token Token;
H. Peter Anvince616072002-04-30 21:02:23 +000090typedef struct Blocks Blocks;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000091typedef struct Line Line;
92typedef struct Include Include;
H. Peter Anvin36206cd2012-03-03 16:14:51 -080093typedef struct Cond Cond;
H. Peter Anvin6768eb72002-04-30 20:52:26 +000094typedef struct IncPath IncPath;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000095
96/*
H. Peter Anvin97a23472007-09-16 17:57:25 -070097 * Note on the storage of both SMacro and MMacros: the hash table
98 * indexes them case-insensitively, and we then have to go through a
99 * linked list of potential case aliases (and, for MMacros, parameter
100 * ranges); this is to preserve the matching semantics of the earlier
101 * code. If the number of case aliases for a specific macro is a
102 * performance issue, you may want to reconsider your coding style.
103 */
104
105/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000106 * Store the definition of a single-line macro.
107 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000108struct SMacro {
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800109 SMacro *next;
110 char *name;
111 bool casesense;
112 bool in_progress;
113 unsigned int nparam;
114 Token *expansion;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000115};
116
117/*
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800118 * Store the definition of a multi-line macro. This is also used to
119 * store the interiors of `%rep...%endrep' blocks, which are
120 * effectively self-re-invoking multi-line macros which simply
121 * don't have a name or bother to appear in the hash tables. %rep
122 * blocks are signified by having a NULL `name' field.
123 *
124 * In a MMacro describing a `%rep' block, the `in_progress' field
125 * isn't merely boolean, but gives the number of repeats left to
126 * run.
127 *
128 * The `next' field is used for storing MMacros in hash tables; the
129 * `next_active' field is for stacking them on istk entries.
130 *
131 * When a MMacro is being expanded, `params', `iline', `nparam',
132 * `paramlen', `rotate' and `unique' are local to the invocation.
133 */
134struct MMacro {
135 MMacro *next;
136 MMacroInvocation *prev; /* previous invocation */
137 char *name;
138 int nparam_min, nparam_max;
139 bool casesense;
140 bool plus; /* is the last parameter greedy? */
141 bool nolist; /* is this macro listing-inhibited? */
142 int64_t in_progress; /* is this macro currently being expanded? */
143 int32_t max_depth; /* maximum number of recursive expansions allowed */
144 Token *dlist; /* All defaults as one list */
145 Token **defaults; /* Parameter default pointers */
146 int ndefs; /* number of default parameters */
147 Line *expansion;
148
149 MMacro *next_active;
150 MMacro *rep_nest; /* used for nesting %rep */
151 Token **params; /* actual parameters */
152 Token *iline; /* invocation line */
153 unsigned int nparam, rotate;
154 int *paramlen;
155 uint64_t unique;
156 int lineno; /* Current line number on expansion */
157 uint64_t condcnt; /* number of if blocks... */
H. Peter Anvin4def1a82016-05-09 13:59:44 -0700158
H. Peter Anvin274cda82016-05-10 02:56:29 -0700159 const char *fname; /* File where defined */
H. Peter Anvin4def1a82016-05-09 13:59:44 -0700160 int32_t xline; /* First line in macro */
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800161};
162
163
164/* Store the definition of a multi-line macro, as defined in a
165 * previous recursive macro expansion.
166 */
167struct MMacroInvocation {
168 MMacroInvocation *prev; /* previous invocation */
169 Token **params; /* actual parameters */
170 Token *iline; /* invocation line */
171 unsigned int nparam, rotate;
172 int *paramlen;
173 uint64_t unique;
174 uint64_t condcnt;
175};
176
177
178/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000179 * The context stack is composed of a linked list of these.
180 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000181struct Context {
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800182 Context *next;
183 char *name;
184 struct hash_table localmac;
185 uint32_t number;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000186};
187
188/*
189 * This is the internal form which we break input lines up into.
190 * Typically stored in linked lists.
191 *
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000192 * Note that `type' serves a double meaning: TOK_SMAC_PARAM is not
193 * necessarily used as-is, but is intended to denote the number of
194 * the substituted parameter. So in the definition
195 *
196 * %define a(x,y) ( (x) & ~(y) )
H. Peter Anvin70653092007-10-19 14:42:29 -0700197 *
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000198 * the token representing `x' will have its type changed to
199 * TOK_SMAC_PARAM, but the one representing `y' will be
200 * TOK_SMAC_PARAM+1.
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000201 *
202 * TOK_INTERNAL_STRING is a dirty hack: it's a single string token
203 * which doesn't need quotes around it. Used in the pre-include
204 * mechanism as an alternative to trying to find a sensible type of
205 * quote to use on the filename we were passed.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000206 */
H. Peter Anvinda10e7b2007-09-12 04:18:37 +0000207enum pp_token_type {
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800208 TOK_NONE = 0, TOK_WHITESPACE, TOK_COMMENT, TOK_ID,
209 TOK_PREPROC_ID, TOK_STRING,
210 TOK_NUMBER, TOK_FLOAT, TOK_SMAC_END, TOK_OTHER,
H. Peter Anvin6c81f0a2008-05-25 21:46:17 -0700211 TOK_INTERNAL_STRING,
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800212 TOK_PREPROC_Q, TOK_PREPROC_QQ,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300213 TOK_PASTE, /* %+ */
214 TOK_INDIRECT, /* %[...] */
215 TOK_SMAC_PARAM, /* MUST BE LAST IN THE LIST!!! */
216 TOK_MAX = INT_MAX /* Keep compiler from reducing the range */
H. Peter Anvinda10e7b2007-09-12 04:18:37 +0000217};
218
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +0400219#define PP_CONCAT_MASK(x) (1 << (x))
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +0400220#define PP_CONCAT_MATCH(t, mask) (PP_CONCAT_MASK((t)->type) & mask)
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +0400221
Cyrill Gorcunov575d4282010-10-06 00:25:55 +0400222struct tokseq_match {
223 int mask_head;
224 int mask_tail;
225};
226
H. Peter Anvine2c80182005-01-15 22:15:51 +0000227struct Token {
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800228 Token *next;
229 char *text;
H. Peter Anvinf26e0972008-07-01 21:26:27 -0700230 union {
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800231 SMacro *mac; /* associated macro for TOK_SMAC_END */
232 size_t len; /* scratch length field */
233 } a; /* Auxiliary data */
234 enum pp_token_type type;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000235};
236
237/*
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800238 * Multi-line macro definitions are stored as a linked list of
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000239 * these, which is essentially a container to allow several linked
240 * lists of Tokens.
H. Peter Anvin70653092007-10-19 14:42:29 -0700241 *
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000242 * Note that in this module, linked lists are treated as stacks
243 * wherever possible. For this reason, Lines are _pushed_ on to the
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800244 * `expansion' field in MMacro structures, so that the linked list,
245 * if walked, would give the macro lines in reverse order; this
246 * means that we can walk the list when expanding a macro, and thus
247 * push the lines on to the `expansion' field in _istk_ in reverse
248 * order (so that when popped back off they are in the right
249 * order). It may seem cockeyed, and it relies on my design having
250 * an even number of steps in, but it works...
251 *
252 * Some of these structures, rather than being actual lines, are
253 * markers delimiting the end of the expansion of a given macro.
254 * This is for use in the cycle-tracking and %rep-handling code.
255 * Such structures have `finishes' non-NULL, and `first' NULL. All
256 * others have `finishes' NULL, but `first' may still be NULL if
257 * the line is blank.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000258 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000259struct Line {
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800260 Line *next;
261 MMacro *finishes;
262 Token *first;
Keith Kaniosb307a4f2010-11-06 17:41:51 -0500263};
264
265/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000266 * To handle an arbitrary level of file inclusion, we maintain a
267 * stack (ie linked list) of these things.
268 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000269struct Include {
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800270 Include *next;
271 FILE *fp;
272 Cond *conds;
273 Line *expansion;
H. Peter Anvin274cda82016-05-10 02:56:29 -0700274 const char *fname;
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800275 int lineno, lineinc;
276 MMacro *mstk; /* stack of active macros/reps */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000277};
278
279/*
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000280 * Include search path. This is simply a list of strings which get
281 * prepended, in turn, to the name of an include file, in an
282 * attempt to find the file if it's not in the current directory.
283 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000284struct IncPath {
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800285 IncPath *next;
286 char *path;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000287};
288
289/*
H. Peter Anvin169ac7c2016-09-25 17:08:05 -0700290 * File real name hash, so we don't have to re-search the include
291 * path for every pass (and potentially more than that if a file
292 * is used more than once.)
293 */
294struct hash_table FileHash;
295
296/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000297 * Conditional assembly: we maintain a separate stack of these for
298 * each level of file inclusion. (The only reason we keep the
299 * stacks separate is to ensure that a stray `%endif' in a file
300 * included from within the true branch of a `%if' won't terminate
301 * it and cause confusion: instead, rightly, it'll cause an error.)
302 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800303struct Cond {
304 Cond *next;
305 int state;
306};
H. Peter Anvine2c80182005-01-15 22:15:51 +0000307enum {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000308 /*
309 * These states are for use just after %if or %elif: IF_TRUE
310 * means the condition has evaluated to truth so we are
311 * currently emitting, whereas IF_FALSE means we are not
312 * currently emitting but will start doing so if a %else comes
313 * up. In these states, all directives are admissible: %elif,
314 * %else and %endif. (And of course %if.)
315 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800316 COND_IF_TRUE, COND_IF_FALSE,
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000317 /*
318 * These states come up after a %else: ELSE_TRUE means we're
319 * emitting, and ELSE_FALSE means we're not. In ELSE_* states,
320 * any %elif or %else will cause an error.
321 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800322 COND_ELSE_TRUE, COND_ELSE_FALSE,
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000323 /*
Victor van den Elzen3b404c02008-09-18 13:51:36 +0200324 * These states mean that we're not emitting now, and also that
325 * nothing until %endif will be emitted at all. COND_DONE is
326 * used when we've had our moment of emission
327 * and have now started seeing %elifs. COND_NEVER is used when
328 * the condition construct in question is contained within a
329 * non-emitting branch of a larger condition construct,
330 * or if there is an error.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000331 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800332 COND_DONE, COND_NEVER
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000333};
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800334#define emitting(x) ( (x) == COND_IF_TRUE || (x) == COND_ELSE_TRUE )
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000335
H. Peter Anvin70653092007-10-19 14:42:29 -0700336/*
Ed Beroset3ab3f412002-06-11 03:31:49 +0000337 * These defines are used as the possible return values for do_directive
338 */
339#define NO_DIRECTIVE_FOUND 0
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300340#define DIRECTIVE_FOUND 1
Ed Beroset3ab3f412002-06-11 03:31:49 +0000341
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000342/*
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800343 * This define sets the upper limit for smacro and recursive mmacro
344 * expansions
Keith Kanios852f1ee2009-07-12 00:19:55 -0500345 */
346#define DEADMAN_LIMIT (1 << 20)
347
Cyrill Gorcunove091d6e2010-08-09 13:58:22 +0400348/* max reps */
349#define REP_LIMIT ((INT64_C(1) << 62))
350
Keith Kanios852f1ee2009-07-12 00:19:55 -0500351/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000352 * Condition codes. Note that we use c_ prefix not C_ because C_ is
353 * used in nasm.h for the "real" condition codes. At _this_ level,
354 * we treat CXZ and ECXZ as condition codes, albeit non-invertible
355 * ones, so we need a different enum...
356 */
H. Peter Anvin476d2862007-10-02 22:04:15 -0700357static const char * const conditions[] = {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000358 "a", "ae", "b", "be", "c", "cxz", "e", "ecxz", "g", "ge", "l", "le",
359 "na", "nae", "nb", "nbe", "nc", "ne", "ng", "nge", "nl", "nle", "no",
H. Peter Anvince9be342007-09-12 00:22:29 +0000360 "np", "ns", "nz", "o", "p", "pe", "po", "rcxz", "s", "z"
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000361};
H. Peter Anvin476d2862007-10-02 22:04:15 -0700362enum pp_conds {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000363 c_A, c_AE, c_B, c_BE, c_C, c_CXZ, c_E, c_ECXZ, c_G, c_GE, c_L, c_LE,
364 c_NA, c_NAE, c_NB, c_NBE, c_NC, c_NE, c_NG, c_NGE, c_NL, c_NLE, c_NO,
H. Peter Anvin476d2862007-10-02 22:04:15 -0700365 c_NP, c_NS, c_NZ, c_O, c_P, c_PE, c_PO, c_RCXZ, c_S, c_Z,
366 c_none = -1
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000367};
H. Peter Anvin476d2862007-10-02 22:04:15 -0700368static const enum pp_conds inverse_ccs[] = {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000369 c_NA, c_NAE, c_NB, c_NBE, c_NC, -1, c_NE, -1, c_NG, c_NGE, c_NL, c_NLE,
370 c_A, c_AE, c_B, c_BE, c_C, c_E, c_G, c_GE, c_L, c_LE, c_O, c_P, c_S,
H. Peter Anvince9be342007-09-12 00:22:29 +0000371 c_Z, c_NO, c_NP, c_PO, c_PE, -1, c_NS, c_NZ
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000372};
373
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800374/*
375 * Directive names.
376 */
377/* If this is a an IF, ELIF, ELSE or ENDIF keyword */
378static int is_condition(enum preproc_token arg)
379{
380 return PP_IS_COND(arg) || (arg == PP_ELSE) || (arg == PP_ENDIF);
381}
382
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000383/* For TASM compatibility we need to be able to recognise TASM compatible
384 * conditional compilation directives. Using the NASM pre-processor does
385 * not work, so we look for them specifically from the following list and
386 * then jam in the equivalent NASM directive into the input stream.
387 */
388
H. Peter Anvine2c80182005-01-15 22:15:51 +0000389enum {
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000390 TM_ARG, TM_ELIF, TM_ELSE, TM_ENDIF, TM_IF, TM_IFDEF, TM_IFDIFI,
391 TM_IFNDEF, TM_INCLUDE, TM_LOCAL
392};
393
H. Peter Anvin476d2862007-10-02 22:04:15 -0700394static const char * const tasm_directives[] = {
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000395 "arg", "elif", "else", "endif", "if", "ifdef", "ifdifi",
396 "ifndef", "include", "local"
397};
398
399static int StackSize = 4;
H. Peter Anvin6c8b2be2016-05-24 23:46:50 -0700400static const char *StackPointer = "ebp";
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000401static int ArgOffset = 8;
H. Peter Anvin8781cb02007-11-08 20:01:11 -0800402static int LocalOffset = 0;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000403
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000404static Context *cstk;
405static Include *istk;
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800406static IncPath *ipath = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000407
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300408static int pass; /* HACK: pass 0 = generate dependencies only */
H. Peter Anvin9924d1e2016-10-04 00:59:39 -0700409static StrList **dephead;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000410
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300411static uint64_t unique; /* unique identifier numbers */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000412
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800413static Line *predef = NULL;
H. Peter Anvind2456592008-06-19 15:04:18 -0700414static bool do_predef;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000415
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000416/*
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800417 * The current set of multi-line macros we have defined.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000418 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800419static struct hash_table mmacros;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000420
421/*
422 * The current set of single-line macros we have defined.
423 */
H. Peter Anvin166c2472008-05-28 12:28:58 -0700424static struct hash_table smacros;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000425
426/*
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800427 * The multi-line macro we are currently defining, or the %rep
428 * block we are currently reading, if any.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000429 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800430static MMacro *defining;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000431
Charles Crayned4200be2008-07-12 16:42:33 -0700432static uint64_t nested_mac_count;
433static uint64_t nested_rep_count;
434
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000435/*
436 * The number of macro parameters to allocate space for at a time.
437 */
438#define PARAM_DELTA 16
439
440/*
H. Peter Anvinf7606612016-07-13 14:23:48 -0700441 * The standard macro set: defined in macros.c in a set of arrays.
442 * This gives our position in any macro set, while we are processing it.
443 * The stdmacset is an array of such macro sets.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000444 */
H. Peter Anvina70547f2008-07-19 21:44:26 -0700445static macros_t *stdmacpos;
H. Peter Anvinf7606612016-07-13 14:23:48 -0700446static macros_t **stdmacnext;
447static macros_t *stdmacros[8];
Cyrill Gorcunov15ce78f2017-01-06 20:21:28 +0300448static macros_t *extrastdmac;
H. Peter Anvin76690a12002-04-30 20:52:49 +0000449
450/*
H. Peter Anvin734b1882002-04-30 21:01:08 +0000451 * Tokens are allocated in blocks to improve speed
452 */
453#define TOKEN_BLOCKSIZE 4096
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800454static Token *freeTokens = NULL;
H. Peter Anvince616072002-04-30 21:02:23 +0000455struct Blocks {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000456 Blocks *next;
457 void *chunk;
H. Peter Anvince616072002-04-30 21:02:23 +0000458};
459
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800460static Blocks blocks = { NULL, NULL };
H. Peter Anvin734b1882002-04-30 21:01:08 +0000461
462/*
H. Peter Anvin76690a12002-04-30 20:52:49 +0000463 * Forward declarations.
464 */
H. Peter Anvinf7606612016-07-13 14:23:48 -0700465static void pp_add_stdmac(macros_t *macros);
H. Peter Anvin734b1882002-04-30 21:01:08 +0000466static Token *expand_mmac_params(Token * tline);
467static Token *expand_smacro(Token * tline);
468static Token *expand_id(Token * tline);
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +0400469static Context *get_ctx(const char *name, const char **namep);
Keith Kaniosa5fc6462007-10-13 07:09:22 -0700470static void make_tok_num(Token * tok, int64_t val);
H. Peter Anvin130736c2016-02-17 20:27:41 -0800471static void pp_verror(int severity, const char *fmt, va_list ap);
472static vefunc real_verror;
H. Peter Anvince616072002-04-30 21:02:23 +0000473static void *new_Block(size_t size);
474static void delete_Blocks(void);
H. Peter Anvinc751e862008-06-09 10:18:45 -0700475static Token *new_Token(Token * next, enum pp_token_type type,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300476 const char *text, int txtlen);
H. Peter Anvin734b1882002-04-30 21:01:08 +0000477static Token *delete_Token(Token * t);
H. Peter Anvineba20a72002-04-30 20:53:55 +0000478
479/*
480 * Macros for safe checking of token pointers, avoid *(NULL)
481 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300482#define tok_type_(x,t) ((x) && (x)->type == (t))
483#define skip_white_(x) if (tok_type_((x), TOK_WHITESPACE)) (x)=(x)->next
484#define tok_is_(x,v) (tok_type_((x), TOK_OTHER) && !strcmp((x)->text,(v)))
485#define tok_isnt_(x,v) ((x) && ((x)->type!=TOK_OTHER || strcmp((x)->text,(v))))
H. Peter Anvin76690a12002-04-30 20:52:49 +0000486
Cyrill Gorcunov194ba892011-06-30 01:16:35 +0400487/*
H. Peter Anvin077fb932010-07-20 14:56:30 -0700488 * nasm_unquote with error if the string contains NUL characters.
489 * If the string contains NUL characters, issue an error and return
490 * the C len, i.e. truncate at the NUL.
491 */
492static size_t nasm_unquote_cstr(char *qstr, enum preproc_token directive)
493{
494 size_t len = nasm_unquote(qstr, NULL);
495 size_t clen = strlen(qstr);
496
497 if (len != clen)
H. Peter Anvin130736c2016-02-17 20:27:41 -0800498 nasm_error(ERR_NONFATAL, "NUL character in `%s' directive",
H. Peter Anvin077fb932010-07-20 14:56:30 -0700499 pp_directives[directive]);
500
501 return clen;
502}
503
504/*
H. Peter Anvinb40992c2010-09-15 08:57:21 -0700505 * In-place reverse a list of tokens.
506 */
507static Token *reverse_tokens(Token *t)
508{
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800509 Token *prev = NULL;
510 Token *next;
H. Peter Anvinb40992c2010-09-15 08:57:21 -0700511
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800512 while (t) {
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +0400513 next = t->next;
514 t->next = prev;
515 prev = t;
516 t = next;
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800517 }
H. Peter Anvinb40992c2010-09-15 08:57:21 -0700518
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800519 return prev;
H. Peter Anvinb40992c2010-09-15 08:57:21 -0700520}
521
522/*
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300523 * Handle TASM specific directives, which do not contain a % in
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000524 * front of them. We do it here because I could not find any other
525 * place to do it for the moment, and it is a hack (ideally it would
526 * be nice to be able to use the NASM pre-processor to do it).
527 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000528static char *check_tasm_directive(char *line)
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000529{
Keith Kaniosb7a89542007-04-12 02:40:54 +0000530 int32_t i, j, k, m, len;
Cyrill Gorcunovf66ac7d2009-10-12 20:41:13 +0400531 char *p, *q, *oldline, oldchar;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000532
Cyrill Gorcunovf66ac7d2009-10-12 20:41:13 +0400533 p = nasm_skip_spaces(line);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000534
535 /* Binary search for the directive name */
536 i = -1;
Cyrill Gorcunova7319242010-06-03 22:04:36 +0400537 j = ARRAY_SIZE(tasm_directives);
Cyrill Gorcunovf66ac7d2009-10-12 20:41:13 +0400538 q = nasm_skip_word(p);
539 len = q - p;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000540 if (len) {
541 oldchar = p[len];
542 p[len] = 0;
543 while (j - i > 1) {
544 k = (j + i) / 2;
545 m = nasm_stricmp(p, tasm_directives[k]);
546 if (m == 0) {
547 /* We have found a directive, so jam a % in front of it
548 * so that NASM will then recognise it as one if it's own.
549 */
550 p[len] = oldchar;
551 len = strlen(p);
552 oldline = line;
553 line = nasm_malloc(len + 2);
554 line[0] = '%';
555 if (k == TM_IFDIFI) {
H. Peter Anvin18f48792009-06-27 15:56:27 -0700556 /*
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300557 * NASM does not recognise IFDIFI, so we convert
558 * it to %if 0. This is not used in NASM
559 * compatible code, but does need to parse for the
560 * TASM macro package.
H. Peter Anvine2c80182005-01-15 22:15:51 +0000561 */
H. Peter Anvin18f48792009-06-27 15:56:27 -0700562 strcpy(line + 1, "if 0");
H. Peter Anvine2c80182005-01-15 22:15:51 +0000563 } else {
564 memcpy(line + 1, p, len + 1);
565 }
566 nasm_free(oldline);
567 return line;
568 } else if (m < 0) {
569 j = k;
570 } else
571 i = k;
572 }
573 p[len] = oldchar;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000574 }
575 return line;
576}
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000577
H. Peter Anvin76690a12002-04-30 20:52:49 +0000578/*
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000579 * The pre-preprocessing stage... This function translates line
580 * number indications as they emerge from GNU cpp (`# lineno "file"
581 * flags') into NASM preprocessor line number indications (`%line
582 * lineno file').
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000583 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000584static char *prepreproc(char *line)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000585{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000586 int lineno, fnlen;
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000587 char *fname, *oldline;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000588
H. Peter Anvine2c80182005-01-15 22:15:51 +0000589 if (line[0] == '#' && line[1] == ' ') {
590 oldline = line;
591 fname = oldline + 2;
592 lineno = atoi(fname);
593 fname += strspn(fname, "0123456789 ");
594 if (*fname == '"')
595 fname++;
596 fnlen = strcspn(fname, "\"");
597 line = nasm_malloc(20 + fnlen);
598 snprintf(line, 20 + fnlen, "%%line %d %.*s", lineno, fnlen, fname);
599 nasm_free(oldline);
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000600 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000601 if (tasm_compatible_mode)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000602 return check_tasm_directive(line);
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000603 return line;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000604}
605
606/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000607 * Free a linked list of tokens.
608 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000609static void free_tlist(Token * list)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000610{
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +0400611 while (list)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000612 list = delete_Token(list);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000613}
614
615/*
616 * Free a linked list of lines.
617 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000618static void free_llist(Line * list)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000619{
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +0400620 Line *l, *tmp;
621 list_for_each_safe(l, tmp, list) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000622 free_tlist(l->first);
623 nasm_free(l);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000624 }
625}
626
627/*
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800628 * Free an MMacro
H. Peter Anvineba20a72002-04-30 20:53:55 +0000629 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800630static void free_mmacro(MMacro * m)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000631{
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800632 nasm_free(m->name);
633 free_tlist(m->dlist);
634 nasm_free(m->defaults);
635 free_llist(m->expansion);
636 nasm_free(m);
H. Peter Anvineba20a72002-04-30 20:53:55 +0000637}
638
639/*
H. Peter Anvin97a23472007-09-16 17:57:25 -0700640 * Free all currently defined macros, and free the hash tables
641 */
H. Peter Anvin072771e2008-05-22 13:17:51 -0700642static void free_smacro_table(struct hash_table *smt)
H. Peter Anvin97a23472007-09-16 17:57:25 -0700643{
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +0400644 SMacro *s, *tmp;
H. Peter Anvin072771e2008-05-22 13:17:51 -0700645 const char *key;
646 struct hash_tbl_node *it = NULL;
H. Peter Anvin97a23472007-09-16 17:57:25 -0700647
H. Peter Anvin072771e2008-05-22 13:17:51 -0700648 while ((s = hash_iterate(smt, &it, &key)) != NULL) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300649 nasm_free((void *)key);
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +0400650 list_for_each_safe(s, tmp, s) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300651 nasm_free(s->name);
652 free_tlist(s->expansion);
653 nasm_free(s);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300654 }
H. Peter Anvin97a23472007-09-16 17:57:25 -0700655 }
H. Peter Anvin072771e2008-05-22 13:17:51 -0700656 hash_free(smt);
657}
658
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800659static void free_mmacro_table(struct hash_table *mmt)
H. Peter Anvin072771e2008-05-22 13:17:51 -0700660{
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800661 MMacro *m, *tmp;
H. Peter Anvin072771e2008-05-22 13:17:51 -0700662 const char *key;
663 struct hash_tbl_node *it = NULL;
H. Peter Anvin97a23472007-09-16 17:57:25 -0700664
665 it = NULL;
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800666 while ((m = hash_iterate(mmt, &it, &key)) != NULL) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300667 nasm_free((void *)key);
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800668 list_for_each_safe(m ,tmp, m)
669 free_mmacro(m);
H. Peter Anvin97a23472007-09-16 17:57:25 -0700670 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800671 hash_free(mmt);
H. Peter Anvin072771e2008-05-22 13:17:51 -0700672}
673
674static void free_macros(void)
675{
H. Peter Anvin166c2472008-05-28 12:28:58 -0700676 free_smacro_table(&smacros);
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800677 free_mmacro_table(&mmacros);
H. Peter Anvin97a23472007-09-16 17:57:25 -0700678}
679
680/*
681 * Initialize the hash tables
682 */
683static void init_macros(void)
684{
H. Peter Anvin166c2472008-05-28 12:28:58 -0700685 hash_init(&smacros, HASH_LARGE);
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800686 hash_init(&mmacros, HASH_LARGE);
H. Peter Anvin97a23472007-09-16 17:57:25 -0700687}
688
689/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000690 * Pop the context stack.
691 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000692static void ctx_pop(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000693{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000694 Context *c = cstk;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000695
696 cstk = cstk->next;
H. Peter Anvin166c2472008-05-28 12:28:58 -0700697 free_smacro_table(&c->localmac);
H. Peter Anvin734b1882002-04-30 21:01:08 +0000698 nasm_free(c->name);
699 nasm_free(c);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000700}
701
H. Peter Anvin072771e2008-05-22 13:17:51 -0700702/*
703 * Search for a key in the hash index; adding it if necessary
704 * (in which case we initialize the data pointer to NULL.)
705 */
706static void **
707hash_findi_add(struct hash_table *hash, const char *str)
708{
709 struct hash_insert hi;
710 void **r;
711 char *strx;
712
713 r = hash_findi(hash, str, &hi);
714 if (r)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300715 return r;
H. Peter Anvin072771e2008-05-22 13:17:51 -0700716
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300717 strx = nasm_strdup(str); /* Use a more efficient allocator here? */
H. Peter Anvin072771e2008-05-22 13:17:51 -0700718 return hash_add(&hi, strx, NULL);
719}
720
721/*
722 * Like hash_findi, but returns the data element rather than a pointer
723 * to it. Used only when not adding a new element, hence no third
724 * argument.
725 */
726static void *
727hash_findix(struct hash_table *hash, const char *str)
728{
729 void **p;
730
731 p = hash_findi(hash, str, NULL);
732 return p ? *p : NULL;
733}
734
Cyrill Gorcunov15bdc512010-07-13 11:27:41 +0400735/*
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800736 * read line from standart macros set,
Cyrill Gorcunov15bdc512010-07-13 11:27:41 +0400737 * if there no more left -- return NULL
738 */
739static char *line_from_stdmac(void)
740{
741 unsigned char c;
742 const unsigned char *p = stdmacpos;
743 char *line, *q;
744 size_t len = 0;
745
746 if (!stdmacpos)
747 return NULL;
748
749 while ((c = *p++)) {
750 if (c >= 0x80)
751 len += pp_directives_len[c - 0x80] + 1;
752 else
753 len++;
754 }
755
756 line = nasm_malloc(len + 1);
757 q = line;
758 while ((c = *stdmacpos++)) {
759 if (c >= 0x80) {
760 memcpy(q, pp_directives[c - 0x80], pp_directives_len[c - 0x80]);
761 q += pp_directives_len[c - 0x80];
762 *q++ = ' ';
763 } else {
764 *q++ = c;
765 }
766 }
767 stdmacpos = p;
768 *q = '\0';
769
770 if (!*stdmacpos) {
H. Peter Anvinf7606612016-07-13 14:23:48 -0700771 /* This was the last of this particular macro set */
Cyrill Gorcunov15bdc512010-07-13 11:27:41 +0400772 stdmacpos = NULL;
H. Peter Anvinf7606612016-07-13 14:23:48 -0700773 if (*stdmacnext) {
774 stdmacpos = *stdmacnext++;
Cyrill Gorcunov15bdc512010-07-13 11:27:41 +0400775 } else if (do_predef) {
776 Line *pd, *l;
777 Token *head, **tail, *t;
778
779 /*
780 * Nasty hack: here we push the contents of
781 * `predef' on to the top-level expansion stack,
782 * since this is the most convenient way to
783 * implement the pre-include and pre-define
784 * features.
785 */
786 list_for_each(pd, predef) {
787 head = NULL;
788 tail = &head;
789 list_for_each(t, pd->first) {
790 *tail = new_Token(NULL, t->type, t->text, 0);
791 tail = &(*tail)->next;
792 }
793
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800794 l = nasm_malloc(sizeof(Line));
795 l->next = istk->expansion;
796 l->first = head;
797 l->finishes = NULL;
798
799 istk->expansion = l;
Cyrill Gorcunov15bdc512010-07-13 11:27:41 +0400800 }
801 do_predef = false;
802 }
803 }
804
805 return line;
806}
807
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000808static char *read_line(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000809{
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +0400810 unsigned int size, c, next;
811 const unsigned int delta = 512;
812 const unsigned int pad = 8;
813 unsigned int nr_cont = 0;
814 bool cont = false;
815 char *buffer, *p;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000816
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +0400817 /* Standart macros set (predefined) goes first */
Cyrill Gorcunov15bdc512010-07-13 11:27:41 +0400818 p = line_from_stdmac();
819 if (p)
820 return p;
H. Peter Anvin72edbb82008-06-19 16:00:04 -0700821
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +0400822 size = delta;
823 p = buffer = nasm_malloc(size);
824
825 for (;;) {
826 c = fgetc(istk->fp);
827 if ((int)(c) == EOF) {
828 p[0] = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000829 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000830 }
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +0400831
832 switch (c) {
833 case '\r':
834 next = fgetc(istk->fp);
835 if (next != '\n')
836 ungetc(next, istk->fp);
837 if (cont) {
838 cont = false;
839 continue;
840 }
841 break;
842
843 case '\n':
844 if (cont) {
845 cont = false;
846 continue;
847 }
848 break;
849
850 case '\\':
851 next = fgetc(istk->fp);
852 ungetc(next, istk->fp);
Cyrill Gorcunov490f85e2012-12-27 20:02:17 +0400853 if (next == '\r' || next == '\n') {
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +0400854 cont = true;
855 nr_cont++;
856 continue;
857 }
858 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000859 }
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +0400860
861 if (c == '\r' || c == '\n') {
862 *p++ = 0;
863 break;
864 }
865
866 if (p >= (buffer + size - pad)) {
867 buffer = nasm_realloc(buffer, size + delta);
868 p = buffer + size - pad;
869 size += delta;
870 }
871
872 *p++ = (unsigned char)c;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000873 }
874
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +0400875 if (p == buffer) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000876 nasm_free(buffer);
877 return NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000878 }
879
Cyrill Gorcunova5aea572010-11-11 10:14:45 +0300880 src_set_linnum(src_get_linnum() + istk->lineinc +
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +0400881 (nr_cont * istk->lineinc));
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000882
883 /*
884 * Handle spurious ^Z, which may be inserted into source files
885 * by some file transfer utilities.
886 */
887 buffer[strcspn(buffer, "\032")] = '\0';
888
H. Peter Anvin8ac25aa2016-02-18 01:16:18 -0800889 lfmt->line(LIST_READ, buffer);
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000890
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000891 return buffer;
892}
893
894/*
Keith Kaniosb7a89542007-04-12 02:40:54 +0000895 * Tokenize a line of text. This is a very simple process since we
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000896 * don't need to parse the value out of e.g. numeric tokens: we
897 * simply split one string into many.
898 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000899static Token *tokenize(char *line)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000900{
H. Peter Anvinca544db2008-10-19 19:30:11 -0700901 char c, *p = line;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +0000902 enum pp_token_type type;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000903 Token *list = NULL;
904 Token *t, **tail = &list;
905
H. Peter Anvine2c80182005-01-15 22:15:51 +0000906 while (*line) {
907 p = line;
908 if (*p == '%') {
909 p++;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300910 if (*p == '+' && !nasm_isdigit(p[1])) {
911 p++;
912 type = TOK_PASTE;
913 } else if (nasm_isdigit(*p) ||
914 ((*p == '-' || *p == '+') && nasm_isdigit(p[1]))) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000915 do {
916 p++;
917 }
H. Peter Anvinbda7a6e2008-06-21 10:23:17 -0700918 while (nasm_isdigit(*p));
H. Peter Anvine2c80182005-01-15 22:15:51 +0000919 type = TOK_PREPROC_ID;
920 } else if (*p == '{') {
921 p++;
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800922 while (*p) {
923 if (*p == '}')
924 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000925 p[-1] = *p;
926 p++;
927 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800928 if (*p != '}')
H. Peter Anvin130736c2016-02-17 20:27:41 -0800929 nasm_error(ERR_WARNING | ERR_PASS1,
930 "unterminated %%{ construct");
H. Peter Anvine2c80182005-01-15 22:15:51 +0000931 p[-1] = '\0';
932 if (*p)
933 p++;
934 type = TOK_PREPROC_ID;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300935 } else if (*p == '[') {
936 int lvl = 1;
937 line += 2; /* Skip the leading %[ */
938 p++;
939 while (lvl && (c = *p++)) {
940 switch (c) {
941 case ']':
942 lvl--;
943 break;
944 case '%':
945 if (*p == '[')
946 lvl++;
947 break;
948 case '\'':
949 case '\"':
950 case '`':
Cyrill Gorcunov3144e842017-10-22 10:50:55 +0300951 p = nasm_skip_string(p - 1);
952 if (*p)
953 p++;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300954 break;
955 default:
956 break;
957 }
958 }
959 p--;
960 if (*p)
961 *p++ = '\0';
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800962 if (lvl)
H. Peter Anvin130736c2016-02-17 20:27:41 -0800963 nasm_error(ERR_NONFATAL|ERR_PASS1,
964 "unterminated %%[ construct");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300965 type = TOK_INDIRECT;
966 } else if (*p == '?') {
967 type = TOK_PREPROC_Q; /* %? */
968 p++;
969 if (*p == '?') {
970 type = TOK_PREPROC_QQ; /* %?? */
971 p++;
972 }
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +0400973 } else if (*p == '!') {
974 type = TOK_PREPROC_ID;
975 p++;
976 if (isidchar(*p)) {
977 do {
978 p++;
979 }
980 while (isidchar(*p));
981 } else if (*p == '\'' || *p == '\"' || *p == '`') {
982 p = nasm_skip_string(p);
983 if (*p)
984 p++;
985 else
H. Peter Anvin130736c2016-02-17 20:27:41 -0800986 nasm_error(ERR_NONFATAL|ERR_PASS1,
987 "unterminated %%! string");
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +0400988 } else {
989 /* %! without string or identifier */
990 type = TOK_OTHER; /* Legacy behavior... */
991 }
H. Peter Anvine2c80182005-01-15 22:15:51 +0000992 } else if (isidchar(*p) ||
993 ((*p == '!' || *p == '%' || *p == '$') &&
994 isidchar(p[1]))) {
995 do {
996 p++;
997 }
998 while (isidchar(*p));
999 type = TOK_PREPROC_ID;
1000 } else {
1001 type = TOK_OTHER;
1002 if (*p == '%')
1003 p++;
1004 }
1005 } else if (isidstart(*p) || (*p == '$' && isidstart(p[1]))) {
1006 type = TOK_ID;
1007 p++;
1008 while (*p && isidchar(*p))
1009 p++;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07001010 } else if (*p == '\'' || *p == '"' || *p == '`') {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001011 /*
1012 * A string token.
1013 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001014 type = TOK_STRING;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001015 p = nasm_skip_string(p);
Nickolay Yurchenkof3b3ce22003-09-21 20:38:43 +00001016
H. Peter Anvine2c80182005-01-15 22:15:51 +00001017 if (*p) {
1018 p++;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001019 } else {
H. Peter Anvin130736c2016-02-17 20:27:41 -08001020 nasm_error(ERR_WARNING|ERR_PASS1, "unterminated string");
H. Peter Anvine2c80182005-01-15 22:15:51 +00001021 /* Handling unterminated strings by UNV */
1022 /* type = -1; */
1023 }
Victor van den Elzenfb5f2512009-04-17 16:17:59 +02001024 } else if (p[0] == '$' && p[1] == '$') {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001025 type = TOK_OTHER; /* TOKEN_BASE */
Victor van den Elzenfb5f2512009-04-17 16:17:59 +02001026 p += 2;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001027 } else if (isnumstart(*p)) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001028 bool is_hex = false;
1029 bool is_float = false;
1030 bool has_e = false;
1031 char c, *r;
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001032
H. Peter Anvine2c80182005-01-15 22:15:51 +00001033 /*
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001034 * A numeric token.
H. Peter Anvine2c80182005-01-15 22:15:51 +00001035 */
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001036
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001037 if (*p == '$') {
1038 p++;
1039 is_hex = true;
1040 }
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001041
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001042 for (;;) {
1043 c = *p++;
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001044
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001045 if (!is_hex && (c == 'e' || c == 'E')) {
1046 has_e = true;
1047 if (*p == '+' || *p == '-') {
1048 /*
1049 * e can only be followed by +/- if it is either a
1050 * prefixed hex number or a floating-point number
1051 */
1052 p++;
1053 is_float = true;
1054 }
1055 } else if (c == 'H' || c == 'h' || c == 'X' || c == 'x') {
1056 is_hex = true;
1057 } else if (c == 'P' || c == 'p') {
1058 is_float = true;
1059 if (*p == '+' || *p == '-')
1060 p++;
Martin Lindheb150e382016-11-16 15:36:53 +01001061 } else if (isnumchar(c))
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001062 ; /* just advance */
1063 else if (c == '.') {
1064 /*
1065 * we need to deal with consequences of the legacy
1066 * parser, like "1.nolist" being two tokens
1067 * (TOK_NUMBER, TOK_ID) here; at least give it
1068 * a shot for now. In the future, we probably need
1069 * a flex-based scanner with proper pattern matching
1070 * to do it as well as it can be done. Nothing in
1071 * the world is going to help the person who wants
1072 * 0x123.p16 interpreted as two tokens, though.
1073 */
1074 r = p;
1075 while (*r == '_')
1076 r++;
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001077
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001078 if (nasm_isdigit(*r) || (is_hex && nasm_isxdigit(*r)) ||
1079 (!is_hex && (*r == 'e' || *r == 'E')) ||
1080 (*r == 'p' || *r == 'P')) {
1081 p = r;
1082 is_float = true;
1083 } else
1084 break; /* Terminate the token */
1085 } else
1086 break;
1087 }
1088 p--; /* Point to first character beyond number */
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001089
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001090 if (p == line+1 && *line == '$') {
1091 type = TOK_OTHER; /* TOKEN_HERE */
1092 } else {
1093 if (has_e && !is_hex) {
1094 /* 1e13 is floating-point, but 1e13h is not */
1095 is_float = true;
1096 }
H. Peter Anvind784a082009-04-20 14:01:18 -07001097
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001098 type = is_float ? TOK_FLOAT : TOK_NUMBER;
1099 }
H. Peter Anvinbda7a6e2008-06-21 10:23:17 -07001100 } else if (nasm_isspace(*p)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001101 type = TOK_WHITESPACE;
Cyrill Gorcunovf66ac7d2009-10-12 20:41:13 +04001102 p = nasm_skip_spaces(p);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001103 /*
1104 * Whitespace just before end-of-line is discarded by
1105 * pretending it's a comment; whitespace just before a
1106 * comment gets lumped into the comment.
1107 */
1108 if (!*p || *p == ';') {
1109 type = TOK_COMMENT;
1110 while (*p)
1111 p++;
1112 }
1113 } else if (*p == ';') {
1114 type = TOK_COMMENT;
1115 while (*p)
1116 p++;
1117 } else {
1118 /*
1119 * Anything else is an operator of some kind. We check
1120 * for all the double-character operators (>>, <<, //,
1121 * %%, <=, >=, ==, !=, <>, &&, ||, ^^), but anything
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001122 * else is a single-character operator.
H. Peter Anvine2c80182005-01-15 22:15:51 +00001123 */
1124 type = TOK_OTHER;
1125 if ((p[0] == '>' && p[1] == '>') ||
1126 (p[0] == '<' && p[1] == '<') ||
1127 (p[0] == '/' && p[1] == '/') ||
1128 (p[0] == '<' && p[1] == '=') ||
1129 (p[0] == '>' && p[1] == '=') ||
1130 (p[0] == '=' && p[1] == '=') ||
1131 (p[0] == '!' && p[1] == '=') ||
1132 (p[0] == '<' && p[1] == '>') ||
1133 (p[0] == '&' && p[1] == '&') ||
1134 (p[0] == '|' && p[1] == '|') ||
1135 (p[0] == '^' && p[1] == '^')) {
1136 p++;
1137 }
1138 p++;
1139 }
Nickolay Yurchenkof3b3ce22003-09-21 20:38:43 +00001140
H. Peter Anvine2c80182005-01-15 22:15:51 +00001141 /* Handling unterminated string by UNV */
1142 /*if (type == -1)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001143 {
1144 *tail = t = new_Token(NULL, TOK_STRING, line, p-line+1);
1145 t->text[p-line] = *line;
1146 tail = &t->next;
1147 }
1148 else */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001149 if (type != TOK_COMMENT) {
1150 *tail = t = new_Token(NULL, type, line, p - line);
1151 tail = &t->next;
1152 }
1153 line = p;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001154 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001155 return list;
1156}
1157
H. Peter Anvince616072002-04-30 21:02:23 +00001158/*
1159 * this function allocates a new managed block of memory and
H. Peter Anvin70653092007-10-19 14:42:29 -07001160 * returns a pointer to the block. The managed blocks are
H. Peter Anvince616072002-04-30 21:02:23 +00001161 * deleted only all at once by the delete_Blocks function.
1162 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001163static void *new_Block(size_t size)
H. Peter Anvince616072002-04-30 21:02:23 +00001164{
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001165 Blocks *b = &blocks;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001166
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001167 /* first, get to the end of the linked list */
1168 while (b->next)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001169 b = b->next;
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001170 /* now allocate the requested chunk */
1171 b->chunk = nasm_malloc(size);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001172
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001173 /* now allocate a new block for the next request */
Cyrill Gorcunovc31767c2014-06-28 02:22:17 +04001174 b->next = nasm_zalloc(sizeof(Blocks));
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001175 return b->chunk;
H. Peter Anvince616072002-04-30 21:02:23 +00001176}
1177
1178/*
1179 * this function deletes all managed blocks of memory
1180 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001181static void delete_Blocks(void)
H. Peter Anvince616072002-04-30 21:02:23 +00001182{
H. Peter Anvine2c80182005-01-15 22:15:51 +00001183 Blocks *a, *b = &blocks;
H. Peter Anvince616072002-04-30 21:02:23 +00001184
H. Peter Anvin70653092007-10-19 14:42:29 -07001185 /*
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001186 * keep in mind that the first block, pointed to by blocks
H. Peter Anvin70653092007-10-19 14:42:29 -07001187 * is a static and not dynamically allocated, so we don't
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001188 * free it.
1189 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001190 while (b) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001191 if (b->chunk)
1192 nasm_free(b->chunk);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001193 a = b;
1194 b = b->next;
1195 if (a != &blocks)
1196 nasm_free(a);
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001197 }
Cyrill Gorcunovdae24d72014-06-28 10:17:39 +04001198 memset(&blocks, 0, sizeof(blocks));
H. Peter Anvine2c80182005-01-15 22:15:51 +00001199}
H. Peter Anvin734b1882002-04-30 21:01:08 +00001200
1201/*
H. Peter Anvin70653092007-10-19 14:42:29 -07001202 * this function creates a new Token and passes a pointer to it
H. Peter Anvin734b1882002-04-30 21:01:08 +00001203 * back to the caller. It sets the type and text elements, and
H. Peter Anvinf26e0972008-07-01 21:26:27 -07001204 * also the a.mac and next elements to NULL.
H. Peter Anvin734b1882002-04-30 21:01:08 +00001205 */
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07001206static Token *new_Token(Token * next, enum pp_token_type type,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001207 const char *text, int txtlen)
H. Peter Anvin734b1882002-04-30 21:01:08 +00001208{
1209 Token *t;
1210 int i;
1211
H. Peter Anvin89cee572009-07-15 09:16:54 -04001212 if (!freeTokens) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001213 freeTokens = (Token *) new_Block(TOKEN_BLOCKSIZE * sizeof(Token));
1214 for (i = 0; i < TOKEN_BLOCKSIZE - 1; i++)
1215 freeTokens[i].next = &freeTokens[i + 1];
1216 freeTokens[i].next = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001217 }
1218 t = freeTokens;
1219 freeTokens = t->next;
1220 t->next = next;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07001221 t->a.mac = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001222 t->type = type;
H. Peter Anvin89cee572009-07-15 09:16:54 -04001223 if (type == TOK_WHITESPACE || !text) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001224 t->text = NULL;
1225 } else {
1226 if (txtlen == 0)
1227 txtlen = strlen(text);
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07001228 t->text = nasm_malloc(txtlen+1);
1229 memcpy(t->text, text, txtlen);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001230 t->text[txtlen] = '\0';
H. Peter Anvin734b1882002-04-30 21:01:08 +00001231 }
1232 return t;
1233}
1234
H. Peter Anvine2c80182005-01-15 22:15:51 +00001235static Token *delete_Token(Token * t)
H. Peter Anvin734b1882002-04-30 21:01:08 +00001236{
1237 Token *next = t->next;
1238 nasm_free(t->text);
H. Peter Anvin788e6c12002-04-30 21:02:01 +00001239 t->next = freeTokens;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001240 freeTokens = t;
1241 return next;
1242}
1243
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001244/*
1245 * Convert a line of tokens back into text.
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001246 * If expand_locals is not zero, identifiers of the form "%$*xxx"
1247 * will be transformed into ..@ctxnum.xxx
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001248 */
H. Peter Anvin9e200162008-06-04 17:23:14 -07001249static char *detoken(Token * tlist, bool expand_locals)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001250{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001251 Token *t;
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001252 char *line, *p;
H. Peter Anvinb4daadc2008-01-21 16:31:57 -08001253 const char *q;
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001254 int len = 0;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001255
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001256 list_for_each(t, tlist) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001257 if (t->type == TOK_PREPROC_ID && t->text[1] == '!') {
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001258 char *v;
1259 char *q = t->text;
H. Peter Anvin077fb932010-07-20 14:56:30 -07001260
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001261 v = t->text + 2;
1262 if (*v == '\'' || *v == '\"' || *v == '`') {
1263 size_t len = nasm_unquote(v, NULL);
1264 size_t clen = strlen(v);
H. Peter Anvin077fb932010-07-20 14:56:30 -07001265
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001266 if (len != clen) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08001267 nasm_error(ERR_NONFATAL | ERR_PASS1,
1268 "NUL character in %%! string");
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001269 v = NULL;
1270 }
1271 }
H. Peter Anvin077fb932010-07-20 14:56:30 -07001272
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001273 if (v) {
1274 char *p = getenv(v);
1275 if (!p) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08001276 nasm_error(ERR_NONFATAL | ERR_PASS1,
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001277 "nonexistent environment variable `%s'", v);
Cyrill Gorcunovbbb7a1a2016-06-19 12:15:24 +03001278 /*
1279 * FIXME We better should investigate if accessing
1280 * ->text[1] without ->text[0] is safe enough.
1281 */
1282 t->text = nasm_zalloc(2);
1283 } else
1284 t->text = nasm_strdup(p);
Cyrill Gorcunov75004872017-07-26 01:21:16 +03001285 nasm_free(q);
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001286 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001287 }
H. Peter Anvin077fb932010-07-20 14:56:30 -07001288
H. Peter Anvine2c80182005-01-15 22:15:51 +00001289 /* Expand local macros here and not during preprocessing */
1290 if (expand_locals &&
1291 t->type == TOK_PREPROC_ID && t->text &&
1292 t->text[0] == '%' && t->text[1] == '$') {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001293 const char *q;
1294 char *p;
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04001295 Context *ctx = get_ctx(t->text, &q);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001296 if (ctx) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001297 char buffer[40];
Keith Kanios93f2e9a2007-04-14 00:10:59 +00001298 snprintf(buffer, sizeof(buffer), "..@%"PRIu32".", ctx->number);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001299 p = nasm_strcat(buffer, q);
1300 nasm_free(t->text);
1301 t->text = p;
1302 }
1303 }
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001304 if (t->type == TOK_WHITESPACE)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001305 len++;
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001306 else if (t->text)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001307 len += strlen(t->text);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001308 }
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001309
H. Peter Anvin734b1882002-04-30 21:01:08 +00001310 p = line = nasm_malloc(len + 1);
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001311
1312 list_for_each(t, tlist) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001313 if (t->type == TOK_WHITESPACE) {
H. Peter Anvinb4daadc2008-01-21 16:31:57 -08001314 *p++ = ' ';
H. Peter Anvine2c80182005-01-15 22:15:51 +00001315 } else if (t->text) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001316 q = t->text;
1317 while (*q)
1318 *p++ = *q++;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001319 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001320 }
1321 *p = '\0';
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001322
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001323 return line;
1324}
1325
1326/*
H. Peter Anvin76690a12002-04-30 20:52:49 +00001327 * A scanner, suitable for use by the expression evaluator, which
1328 * operates on a line of Tokens. Expects a pointer to a pointer to
1329 * the first token in the line to be passed in as its private_data
1330 * field.
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001331 *
1332 * FIX: This really needs to be unified with stdscan.
H. Peter Anvin76690a12002-04-30 20:52:49 +00001333 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001334static int ppscan(void *private_data, struct tokenval *tokval)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001335{
H. Peter Anvin76690a12002-04-30 20:52:49 +00001336 Token **tlineptr = private_data;
1337 Token *tline;
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001338 char ourcopy[MAX_KEYWORD+1], *p, *r, *s;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001339
H. Peter Anvine2c80182005-01-15 22:15:51 +00001340 do {
1341 tline = *tlineptr;
1342 *tlineptr = tline ? tline->next : NULL;
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04001343 } while (tline && (tline->type == TOK_WHITESPACE ||
1344 tline->type == TOK_COMMENT));
H. Peter Anvin76690a12002-04-30 20:52:49 +00001345
1346 if (!tline)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001347 return tokval->t_type = TOKEN_EOS;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001348
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001349 tokval->t_charptr = tline->text;
1350
H. Peter Anvin76690a12002-04-30 20:52:49 +00001351 if (tline->text[0] == '$' && !tline->text[1])
H. Peter Anvine2c80182005-01-15 22:15:51 +00001352 return tokval->t_type = TOKEN_HERE;
H. Peter Anvin7cf897e2002-05-30 21:30:33 +00001353 if (tline->text[0] == '$' && tline->text[1] == '$' && !tline->text[2])
H. Peter Anvine2c80182005-01-15 22:15:51 +00001354 return tokval->t_type = TOKEN_BASE;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001355
H. Peter Anvine2c80182005-01-15 22:15:51 +00001356 if (tline->type == TOK_ID) {
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001357 p = tokval->t_charptr = tline->text;
1358 if (p[0] == '$') {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001359 tokval->t_charptr++;
1360 return tokval->t_type = TOKEN_ID;
1361 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00001362
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001363 for (r = p, s = ourcopy; *r; r++) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001364 if (r >= p+MAX_KEYWORD)
1365 return tokval->t_type = TOKEN_ID; /* Not a keyword */
H. Peter Anvinac8f8fc2008-06-11 15:49:41 -07001366 *s++ = nasm_tolower(*r);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001367 }
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001368 *s = '\0';
1369 /* right, so we have an identifier sitting in temp storage. now,
1370 * is it actually a register or instruction name, or what? */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001371 return nasm_token_hash(ourcopy, tokval);
H. Peter Anvin76690a12002-04-30 20:52:49 +00001372 }
1373
H. Peter Anvine2c80182005-01-15 22:15:51 +00001374 if (tline->type == TOK_NUMBER) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001375 bool rn_error;
1376 tokval->t_integer = readnum(tline->text, &rn_error);
1377 tokval->t_charptr = tline->text;
1378 if (rn_error)
1379 return tokval->t_type = TOKEN_ERRNUM;
1380 else
1381 return tokval->t_type = TOKEN_NUM;
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001382 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00001383
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001384 if (tline->type == TOK_FLOAT) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001385 return tokval->t_type = TOKEN_FLOAT;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001386 }
1387
H. Peter Anvine2c80182005-01-15 22:15:51 +00001388 if (tline->type == TOK_STRING) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001389 char bq, *ep;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001390
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001391 bq = tline->text[0];
H. Peter Anvin11627042008-06-09 20:45:19 -07001392 tokval->t_charptr = tline->text;
1393 tokval->t_inttwo = nasm_unquote(tline->text, &ep);
H. Peter Anvind2456592008-06-19 15:04:18 -07001394
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001395 if (ep[0] != bq || ep[1] != '\0')
1396 return tokval->t_type = TOKEN_ERRSTR;
1397 else
1398 return tokval->t_type = TOKEN_STR;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001399 }
1400
H. Peter Anvine2c80182005-01-15 22:15:51 +00001401 if (tline->type == TOK_OTHER) {
1402 if (!strcmp(tline->text, "<<"))
1403 return tokval->t_type = TOKEN_SHL;
1404 if (!strcmp(tline->text, ">>"))
1405 return tokval->t_type = TOKEN_SHR;
1406 if (!strcmp(tline->text, "//"))
1407 return tokval->t_type = TOKEN_SDIV;
1408 if (!strcmp(tline->text, "%%"))
1409 return tokval->t_type = TOKEN_SMOD;
1410 if (!strcmp(tline->text, "=="))
1411 return tokval->t_type = TOKEN_EQ;
1412 if (!strcmp(tline->text, "<>"))
1413 return tokval->t_type = TOKEN_NE;
1414 if (!strcmp(tline->text, "!="))
1415 return tokval->t_type = TOKEN_NE;
1416 if (!strcmp(tline->text, "<="))
1417 return tokval->t_type = TOKEN_LE;
1418 if (!strcmp(tline->text, ">="))
1419 return tokval->t_type = TOKEN_GE;
1420 if (!strcmp(tline->text, "&&"))
1421 return tokval->t_type = TOKEN_DBL_AND;
1422 if (!strcmp(tline->text, "^^"))
1423 return tokval->t_type = TOKEN_DBL_XOR;
1424 if (!strcmp(tline->text, "||"))
1425 return tokval->t_type = TOKEN_DBL_OR;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001426 }
1427
1428 /*
1429 * We have no other options: just return the first character of
1430 * the token text.
1431 */
1432 return tokval->t_type = tline->text[0];
1433}
1434
1435/*
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001436 * 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 */
H. Peter Anvin4db5a162007-10-11 13:42:09 -07001440static int mstrcmp(const char *p, const char *q, bool casesense)
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001441{
H. Peter Anvin734b1882002-04-30 21:01:08 +00001442 return casesense ? strcmp(p, q) : nasm_stricmp(p, q);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001443}
1444
1445/*
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07001446 * Compare a string to the name of an existing macro; this is a
1447 * simple wrapper which calls either strcmp or nasm_stricmp
1448 * depending on the value of the `casesense' parameter.
1449 */
1450static int mmemcmp(const char *p, const char *q, size_t l, bool casesense)
1451{
1452 return casesense ? memcmp(p, q, l) : nasm_memicmp(p, q, l);
1453}
1454
1455/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001456 * Return the Context structure associated with a %$ token. Return
1457 * NULL, having _already_ reported an error condition, if the
1458 * context stack isn't deep enough for the supplied number of $
1459 * signs.
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001460 *
1461 * If "namep" is non-NULL, set it to the pointer to the macro name
1462 * tail, i.e. the part beyond %$...
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001463 */
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04001464static Context *get_ctx(const char *name, const char **namep)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001465{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001466 Context *ctx;
1467 int i;
1468
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001469 if (namep)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001470 *namep = name;
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001471
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001472 if (!name || name[0] != '%' || name[1] != '$')
H. Peter Anvine2c80182005-01-15 22:15:51 +00001473 return NULL;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001474
H. Peter Anvine2c80182005-01-15 22:15:51 +00001475 if (!cstk) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08001476 nasm_error(ERR_NONFATAL, "`%s': context stack is empty", name);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001477 return NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001478 }
1479
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001480 name += 2;
1481 ctx = cstk;
1482 i = 0;
1483 while (ctx && *name == '$') {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001484 name++;
1485 i++;
1486 ctx = ctx->next;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001487 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001488 if (!ctx) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08001489 nasm_error(ERR_NONFATAL, "`%s': context stack is only"
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001490 " %d level%s deep", name, i, (i == 1 ? "" : "s"));
H. Peter Anvine2c80182005-01-15 22:15:51 +00001491 return NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001492 }
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001493
1494 if (namep)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001495 *namep = name;
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001496
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04001497 return ctx;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001498}
1499
1500/*
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001501 * Open an include file. This routine must always return a valid
1502 * file pointer if it returns - it's responsible for throwing an
1503 * ERR_FATAL and bombing out completely if not. It should also try
1504 * the include path one by one until it finds the file or reaches
1505 * the end of the path.
H. Peter Anvind81a2352016-09-21 14:03:18 -07001506 *
1507 * Note: for INC_PROBE the function returns NULL at all times;
1508 * instead look for the
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001509 */
H. Peter Anvind81a2352016-09-21 14:03:18 -07001510enum incopen_mode {
1511 INC_NEEDED, /* File must exist */
1512 INC_OPTIONAL, /* Missing is OK */
1513 INC_PROBE /* Only an existence probe */
1514};
1515
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001516/* This is conducts a full pathname search */
1517static FILE *inc_fopen_search(const char *file, StrList **slpath,
1518 enum incopen_mode omode, enum file_flags fmode)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001519{
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001520 FILE *fp;
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001521 char *prefix = "";
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001522 const IncPath *ip = ipath;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001523 int len = strlen(file);
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001524 size_t prefix_len = 0;
1525 StrList *sl;
Jim Kukunas65a8afc2016-06-13 16:00:42 -04001526 size_t path_len;
H. Peter Anvind81a2352016-09-21 14:03:18 -07001527 bool found;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001528
H. Peter Anvine2c80182005-01-15 22:15:51 +00001529 while (1) {
Jim Kukunas65a8afc2016-06-13 16:00:42 -04001530 path_len = prefix_len + len + 1;
1531
1532 sl = nasm_malloc(path_len + sizeof sl->next);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001533 memcpy(sl->str, prefix, prefix_len);
1534 memcpy(sl->str+prefix_len, file, len+1);
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001535 sl->next = NULL;
Jim Kukunas65a8afc2016-06-13 16:00:42 -04001536
H. Peter Anvind81a2352016-09-21 14:03:18 -07001537 if (omode == INC_PROBE) {
1538 fp = NULL;
1539 found = nasm_file_exists(sl->str);
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001540 } else {
H. Peter Anvind81a2352016-09-21 14:03:18 -07001541 fp = nasm_open_read(sl->str, fmode);
1542 found = (fp != NULL);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001543 }
H. Peter Anvind81a2352016-09-21 14:03:18 -07001544 if (found) {
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001545 *slpath = sl;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001546 return fp;
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001547 }
Jim Kukunas65a8afc2016-06-13 16:00:42 -04001548
H. Peter Anvind81a2352016-09-21 14:03:18 -07001549 nasm_free(sl);
Jim Kukunas65a8afc2016-06-13 16:00:42 -04001550
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001551 if (!ip)
1552 return NULL;
1553
1554 prefix = ip->path;
1555 prefix_len = strlen(prefix);
1556 ip = ip->next;
1557 }
1558}
1559
1560/*
1561 * Open a file, or test for the presence of one (depending on omode),
1562 * considering the include path.
1563 */
1564static FILE *inc_fopen(const char *file,
H. Peter Anvin9924d1e2016-10-04 00:59:39 -07001565 StrList **dhead,
H. Peter Anvinccad6f92016-10-04 00:34:35 -07001566 const char **found_path,
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001567 enum incopen_mode omode,
1568 enum file_flags fmode)
1569{
1570 StrList *sl;
1571 struct hash_insert hi;
1572 void **hp;
1573 char *path;
1574 FILE *fp = NULL;
1575
1576 hp = hash_find(&FileHash, file, &hi);
1577 if (hp) {
1578 path = *hp;
Martin Storsjöf283c8f2017-08-13 17:28:46 +03001579 if (path || omode != INC_NEEDED) {
H. Peter Anvin97fda4c2017-08-16 15:52:51 -07001580 nasm_add_string_to_strlist(dhead, path ? path : file);
Martin Storsjöf283c8f2017-08-13 17:28:46 +03001581 }
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001582 } else {
1583 /* Need to do the actual path search */
1584 size_t file_len;
1585
1586 sl = NULL;
1587 fp = inc_fopen_search(file, &sl, omode, fmode);
1588
1589 file_len = strlen(file);
1590
1591 if (!sl) {
1592 /* Store negative result for this file */
1593 sl = nasm_malloc(file_len + 1 + sizeof sl->next);
1594 memcpy(sl->str, file, file_len+1);
1595 sl->next = NULL;
1596 file = sl->str;
1597 path = NULL;
1598 } else {
1599 path = sl->str;
1600 file = strchr(path, '\0') - file_len;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001601 }
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001602
1603 hash_add(&hi, file, path); /* Positive or negative result */
1604
H. Peter Anvin9924d1e2016-10-04 00:59:39 -07001605 /*
1606 * Add file to dependency path. The in_list() is needed
1607 * in case the file was already added with %depend.
1608 */
1609 if (path || omode != INC_NEEDED)
H. Peter Anvin436e3672016-10-04 01:12:28 -07001610 nasm_add_to_strlist(dhead, sl);
H. Peter Anvineba20a72002-04-30 20:53:55 +00001611 }
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001612
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001613 if (!path) {
1614 if (omode == INC_NEEDED)
1615 nasm_fatal(0, "unable to open include file `%s'", file);
1616
1617 if (found_path)
1618 *found_path = NULL;
1619
1620 return NULL;
1621 }
1622
1623 if (!fp && omode != INC_PROBE)
Cyrill Gorcunov4ff8c632017-01-06 00:36:23 +03001624 fp = nasm_open_read(path, fmode);
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001625
1626 if (found_path)
H. Peter Anvinccad6f92016-10-04 00:34:35 -07001627 *found_path = path;
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001628
1629 return fp;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001630}
1631
1632/*
Fabian Giesen0bbc38d2016-04-28 13:48:14 -07001633 * Opens an include or input file. Public version, for use by modules
1634 * that get a file:lineno pair and need to look at the file again
1635 * (e.g. the CodeView debug backend). Returns NULL on failure.
1636 */
H. Peter Anvin3e83cec2016-05-25 04:28:46 -07001637FILE *pp_input_fopen(const char *filename, enum file_flags mode)
Fabian Giesen0bbc38d2016-04-28 13:48:14 -07001638{
H. Peter Anvin9924d1e2016-10-04 00:59:39 -07001639 return inc_fopen(filename, NULL, NULL, INC_OPTIONAL, mode);
Fabian Giesen0bbc38d2016-04-28 13:48:14 -07001640}
1641
1642/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001643 * Determine if we should warn on defining a single-line macro of
H. Peter Anvinef7468f2002-04-30 20:57:59 +00001644 * name `name', with `nparam' parameters. If nparam is 0 or -1, will
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001645 * return true if _any_ single-line macro of that name is defined.
1646 * Otherwise, will return true if a single-line macro with either
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001647 * `nparam' or no parameters is defined.
1648 *
1649 * If a macro with precisely the right number of parameters is
H. Peter Anvinef7468f2002-04-30 20:57:59 +00001650 * defined, or nparam is -1, the address of the definition structure
1651 * will be returned in `defn'; otherwise NULL will be returned. If `defn'
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001652 * is NULL, no action will be taken regarding its contents, and no
1653 * error will occur.
1654 *
1655 * Note that this is also called with nparam zero to resolve
1656 * `ifdef'.
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001657 *
1658 * If you already know which context macro belongs to, you can pass
1659 * the context pointer as first parameter; if you won't but name begins
1660 * with %$ the context will be automatically computed. If all_contexts
1661 * is true, macro will be searched in outer contexts as well.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001662 */
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07001663static bool
H. Peter Anvinb2a5fda2008-06-19 21:42:42 -07001664smacro_defined(Context * ctx, const char *name, int nparam, SMacro ** defn,
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07001665 bool nocase)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001666{
H. Peter Anvin166c2472008-05-28 12:28:58 -07001667 struct hash_table *smtbl;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001668 SMacro *m;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001669
H. Peter Anvin97a23472007-09-16 17:57:25 -07001670 if (ctx) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001671 smtbl = &ctx->localmac;
H. Peter Anvin97a23472007-09-16 17:57:25 -07001672 } else if (name[0] == '%' && name[1] == '$') {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001673 if (cstk)
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04001674 ctx = get_ctx(name, &name);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001675 if (!ctx)
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001676 return false; /* got to return _something_ */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001677 smtbl = &ctx->localmac;
H. Peter Anvin97a23472007-09-16 17:57:25 -07001678 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001679 smtbl = &smacros;
H. Peter Anvin97a23472007-09-16 17:57:25 -07001680 }
H. Peter Anvin166c2472008-05-28 12:28:58 -07001681 m = (SMacro *) hash_findix(smtbl, name);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001682
H. Peter Anvine2c80182005-01-15 22:15:51 +00001683 while (m) {
1684 if (!mstrcmp(m->name, name, m->casesense && nocase) &&
Charles Crayne192d5b52007-10-18 19:02:42 -07001685 (nparam <= 0 || m->nparam == 0 || nparam == (int) m->nparam)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001686 if (defn) {
Charles Crayne192d5b52007-10-18 19:02:42 -07001687 if (nparam == (int) m->nparam || nparam == -1)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001688 *defn = m;
1689 else
1690 *defn = NULL;
1691 }
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001692 return true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001693 }
1694 m = m->next;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001695 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001696
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001697 return false;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001698}
1699
1700/*
1701 * Count and mark off the parameters in a multi-line macro call.
1702 * This is called both from within the multi-line macro expansion
1703 * code, and also to mark off the default parameters when provided
1704 * in a %macro definition line.
1705 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001706static void count_mmac_params(Token * t, int *nparam, Token *** params)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001707{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001708 int paramsize, brace;
1709
1710 *nparam = paramsize = 0;
1711 *params = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001712 while (t) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001713 /* +1: we need space for the final NULL */
H. Peter Anvin91fb6f12008-09-01 10:56:33 -07001714 if (*nparam+1 >= paramsize) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001715 paramsize += PARAM_DELTA;
1716 *params = nasm_realloc(*params, sizeof(**params) * paramsize);
1717 }
1718 skip_white_(t);
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08001719 brace = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001720 if (tok_is_(t, "{"))
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08001721 brace++;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001722 (*params)[(*nparam)++] = t;
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08001723 if (brace) {
1724 while (brace && (t = t->next) != NULL) {
1725 if (tok_is_(t, "{"))
1726 brace++;
1727 else if (tok_is_(t, "}"))
1728 brace--;
1729 }
1730
1731 if (t) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001732 /*
1733 * Now we've found the closing brace, look further
1734 * for the comma.
1735 */
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08001736 t = t->next;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001737 skip_white_(t);
1738 if (tok_isnt_(t, ",")) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08001739 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00001740 "braces do not enclose all of macro parameter");
1741 while (tok_isnt_(t, ","))
1742 t = t->next;
1743 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001744 }
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08001745 } else {
1746 while (tok_isnt_(t, ","))
1747 t = t->next;
1748 }
1749 if (t) { /* got a comma/brace */
1750 t = t->next; /* eat the comma */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001751 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001752 }
1753}
1754
1755/*
H. Peter Anvin76690a12002-04-30 20:52:49 +00001756 * Determine whether one of the various `if' conditions is true or
1757 * not.
1758 *
1759 * We must free the tline we get passed.
1760 */
H. Peter Anvin70055962007-10-11 00:05:31 -07001761static bool if_condition(Token * tline, enum preproc_token ct)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001762{
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001763 enum pp_conditional i = PP_COND(ct);
H. Peter Anvin70055962007-10-11 00:05:31 -07001764 bool j;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001765 Token *t, *tt, **tptr, *origline;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001766 struct tokenval tokval;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001767 expr *evalresult;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001768 enum pp_token_type needtype;
H. Peter Anvin077fb932010-07-20 14:56:30 -07001769 char *p;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001770
1771 origline = tline;
1772
H. Peter Anvine2c80182005-01-15 22:15:51 +00001773 switch (i) {
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001774 case PPC_IFCTX:
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001775 j = false; /* have we matched yet? */
Victor van den Elzen0e857f12008-07-23 13:21:29 +02001776 while (true) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001777 skip_white_(tline);
Victor van den Elzen0e857f12008-07-23 13:21:29 +02001778 if (!tline)
1779 break;
1780 if (tline->type != TOK_ID) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08001781 nasm_error(ERR_NONFATAL,
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001782 "`%s' expects context identifiers", pp_directives[ct]);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001783 free_tlist(origline);
1784 return -1;
1785 }
Victor van den Elzen0e857f12008-07-23 13:21:29 +02001786 if (cstk && cstk->name && !nasm_stricmp(tline->text, cstk->name))
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001787 j = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001788 tline = tline->next;
1789 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001790 break;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001791
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001792 case PPC_IFDEF:
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001793 j = false; /* have we matched yet? */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001794 while (tline) {
1795 skip_white_(tline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001796 if (!tline || (tline->type != TOK_ID &&
1797 (tline->type != TOK_PREPROC_ID ||
1798 tline->text[1] != '$'))) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08001799 nasm_error(ERR_NONFATAL,
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001800 "`%s' expects macro identifiers", pp_directives[ct]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001801 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001802 }
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07001803 if (smacro_defined(NULL, tline->text, 0, NULL, true))
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001804 j = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001805 tline = tline->next;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001806 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001807 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001808
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001809 case PPC_IFENV:
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001810 tline = expand_smacro(tline);
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001811 j = false; /* have we matched yet? */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001812 while (tline) {
1813 skip_white_(tline);
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001814 if (!tline || (tline->type != TOK_ID &&
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001815 tline->type != TOK_STRING &&
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001816 (tline->type != TOK_PREPROC_ID ||
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001817 tline->text[1] != '!'))) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08001818 nasm_error(ERR_NONFATAL,
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001819 "`%s' expects environment variable names",
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001820 pp_directives[ct]);
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001821 goto fail;
1822 }
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001823 p = tline->text;
1824 if (tline->type == TOK_PREPROC_ID)
1825 p += 2; /* Skip leading %! */
1826 if (*p == '\'' || *p == '\"' || *p == '`')
1827 nasm_unquote_cstr(p, ct);
1828 if (getenv(p))
1829 j = true;
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001830 tline = tline->next;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001831 }
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001832 break;
1833
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001834 case PPC_IFIDN:
1835 case PPC_IFIDNI:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001836 tline = expand_smacro(tline);
1837 t = tt = tline;
1838 while (tok_isnt_(tt, ","))
1839 tt = tt->next;
1840 if (!tt) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08001841 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00001842 "`%s' expects two comma-separated arguments",
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001843 pp_directives[ct]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001844 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001845 }
1846 tt = tt->next;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001847 j = true; /* assume equality unless proved not */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001848 while ((t->type != TOK_OTHER || strcmp(t->text, ",")) && tt) {
1849 if (tt->type == TOK_OTHER && !strcmp(tt->text, ",")) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08001850 nasm_error(ERR_NONFATAL, "`%s': more than one comma on line",
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001851 pp_directives[ct]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001852 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001853 }
1854 if (t->type == TOK_WHITESPACE) {
1855 t = t->next;
1856 continue;
1857 }
1858 if (tt->type == TOK_WHITESPACE) {
1859 tt = tt->next;
1860 continue;
1861 }
1862 if (tt->type != t->type) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001863 j = false; /* found mismatching tokens */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001864 break;
1865 }
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07001866 /* When comparing strings, need to unquote them first */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001867 if (t->type == TOK_STRING) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001868 size_t l1 = nasm_unquote(t->text, NULL);
1869 size_t l2 = nasm_unquote(tt->text, NULL);
H. Peter Anvind2456592008-06-19 15:04:18 -07001870
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001871 if (l1 != l2) {
1872 j = false;
1873 break;
1874 }
1875 if (mmemcmp(t->text, tt->text, l1, i == PPC_IFIDN)) {
1876 j = false;
1877 break;
1878 }
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07001879 } else if (mstrcmp(tt->text, t->text, i == PPC_IFIDN) != 0) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001880 j = false; /* found mismatching tokens */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001881 break;
1882 }
Nickolay Yurchenkof3b3ce22003-09-21 20:38:43 +00001883
H. Peter Anvine2c80182005-01-15 22:15:51 +00001884 t = t->next;
1885 tt = tt->next;
1886 }
1887 if ((t->type != TOK_OTHER || strcmp(t->text, ",")) || tt)
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001888 j = false; /* trailing gunk on one end or other */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001889 break;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001890
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001891 case PPC_IFMACRO:
H. Peter Anvin89cee572009-07-15 09:16:54 -04001892 {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001893 bool found = false;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001894 MMacro searching, *mmac;
H. Peter Anvin65747262002-05-07 00:10:05 +00001895
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001896 skip_white_(tline);
1897 tline = expand_id(tline);
1898 if (!tok_type_(tline, TOK_ID)) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08001899 nasm_error(ERR_NONFATAL,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001900 "`%s' expects a macro name", pp_directives[ct]);
1901 goto fail;
1902 }
1903 searching.name = nasm_strdup(tline->text);
1904 searching.casesense = true;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001905 searching.plus = false;
1906 searching.nolist = false;
1907 searching.in_progress = 0;
1908 searching.max_depth = 0;
1909 searching.rep_nest = NULL;
1910 searching.nparam_min = 0;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001911 searching.nparam_max = INT_MAX;
1912 tline = expand_smacro(tline->next);
1913 skip_white_(tline);
1914 if (!tline) {
1915 } else if (!tok_type_(tline, TOK_NUMBER)) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08001916 nasm_error(ERR_NONFATAL,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001917 "`%s' expects a parameter count or nothing",
1918 pp_directives[ct]);
1919 } else {
1920 searching.nparam_min = searching.nparam_max =
1921 readnum(tline->text, &j);
1922 if (j)
H. Peter Anvin130736c2016-02-17 20:27:41 -08001923 nasm_error(ERR_NONFATAL,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001924 "unable to parse parameter count `%s'",
1925 tline->text);
1926 }
1927 if (tline && tok_is_(tline->next, "-")) {
1928 tline = tline->next->next;
1929 if (tok_is_(tline, "*"))
1930 searching.nparam_max = INT_MAX;
1931 else if (!tok_type_(tline, TOK_NUMBER))
H. Peter Anvin130736c2016-02-17 20:27:41 -08001932 nasm_error(ERR_NONFATAL,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001933 "`%s' expects a parameter count after `-'",
1934 pp_directives[ct]);
1935 else {
1936 searching.nparam_max = readnum(tline->text, &j);
1937 if (j)
H. Peter Anvin130736c2016-02-17 20:27:41 -08001938 nasm_error(ERR_NONFATAL,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001939 "unable to parse parameter count `%s'",
1940 tline->text);
1941 if (searching.nparam_min > searching.nparam_max)
H. Peter Anvin130736c2016-02-17 20:27:41 -08001942 nasm_error(ERR_NONFATAL,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001943 "minimum parameter count exceeds maximum");
1944 }
1945 }
1946 if (tline && tok_is_(tline->next, "+")) {
1947 tline = tline->next;
1948 searching.plus = true;
1949 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001950 mmac = (MMacro *) hash_findix(&mmacros, searching.name);
1951 while (mmac) {
1952 if (!strcmp(mmac->name, searching.name) &&
1953 (mmac->nparam_min <= searching.nparam_max
1954 || searching.plus)
1955 && (searching.nparam_min <= mmac->nparam_max
1956 || mmac->plus)) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001957 found = true;
1958 break;
1959 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001960 mmac = mmac->next;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001961 }
1962 if (tline && tline->next)
H. Peter Anvin130736c2016-02-17 20:27:41 -08001963 nasm_error(ERR_WARNING|ERR_PASS1,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001964 "trailing garbage after %%ifmacro ignored");
1965 nasm_free(searching.name);
1966 j = found;
1967 break;
H. Peter Anvin89cee572009-07-15 09:16:54 -04001968 }
H. Peter Anvin65747262002-05-07 00:10:05 +00001969
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001970 case PPC_IFID:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001971 needtype = TOK_ID;
1972 goto iftype;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001973 case PPC_IFNUM:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001974 needtype = TOK_NUMBER;
1975 goto iftype;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001976 case PPC_IFSTR:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001977 needtype = TOK_STRING;
1978 goto iftype;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001979
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001980iftype:
1981 t = tline = expand_smacro(tline);
H. Peter Anvind85d2502008-05-04 17:53:31 -07001982
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001983 while (tok_type_(t, TOK_WHITESPACE) ||
1984 (needtype == TOK_NUMBER &&
1985 tok_type_(t, TOK_OTHER) &&
1986 (t->text[0] == '-' || t->text[0] == '+') &&
1987 !t->text[1]))
1988 t = t->next;
H. Peter Anvind85d2502008-05-04 17:53:31 -07001989
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001990 j = tok_type_(t, needtype);
1991 break;
H. Peter Anvincbf768d2008-02-16 16:41:25 -08001992
1993 case PPC_IFTOKEN:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001994 t = tline = expand_smacro(tline);
1995 while (tok_type_(t, TOK_WHITESPACE))
1996 t = t->next;
H. Peter Anvincbf768d2008-02-16 16:41:25 -08001997
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001998 j = false;
1999 if (t) {
2000 t = t->next; /* Skip the actual token */
2001 while (tok_type_(t, TOK_WHITESPACE))
2002 t = t->next;
2003 j = !t; /* Should be nothing left */
2004 }
2005 break;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002006
H. Peter Anvin134b9462008-02-16 17:01:40 -08002007 case PPC_IFEMPTY:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002008 t = tline = expand_smacro(tline);
2009 while (tok_type_(t, TOK_WHITESPACE))
2010 t = t->next;
H. Peter Anvin134b9462008-02-16 17:01:40 -08002011
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002012 j = !t; /* Should be empty */
2013 break;
H. Peter Anvin134b9462008-02-16 17:01:40 -08002014
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002015 case PPC_IF:
H. Peter Anvine2c80182005-01-15 22:15:51 +00002016 t = tline = expand_smacro(tline);
2017 tptr = &t;
2018 tokval.t_type = TOKEN_INVALID;
2019 evalresult = evaluate(ppscan, tptr, &tokval,
H. Peter Anvin130736c2016-02-17 20:27:41 -08002020 NULL, pass | CRITICAL, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002021 if (!evalresult)
2022 return -1;
2023 if (tokval.t_type)
H. Peter Anvin130736c2016-02-17 20:27:41 -08002024 nasm_error(ERR_WARNING|ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +00002025 "trailing garbage after expression ignored");
2026 if (!is_simple(evalresult)) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002027 nasm_error(ERR_NONFATAL,
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002028 "non-constant value given to `%s'", pp_directives[ct]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002029 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002030 }
Chuck Crayne60ae75d2007-05-02 01:59:16 +00002031 j = reloc_value(evalresult) != 0;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002032 break;
H. Peter Anvin95e28822007-09-12 04:20:08 +00002033
H. Peter Anvine2c80182005-01-15 22:15:51 +00002034 default:
H. Peter Anvin130736c2016-02-17 20:27:41 -08002035 nasm_error(ERR_FATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00002036 "preprocessor directive `%s' not yet implemented",
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002037 pp_directives[ct]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002038 goto fail;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002039 }
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002040
2041 free_tlist(origline);
2042 return j ^ PP_NEGATIVE(ct);
H. Peter Anvin70653092007-10-19 14:42:29 -07002043
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002044fail:
2045 free_tlist(origline);
2046 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002047}
2048
2049/*
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002050 * Common code for defining an smacro
2051 */
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08002052static bool define_smacro(Context *ctx, const char *mname, bool casesense,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002053 int nparam, Token *expansion)
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002054{
2055 SMacro *smac, **smhead;
H. Peter Anvin166c2472008-05-28 12:28:58 -07002056 struct hash_table *smtbl;
H. Peter Anvin70653092007-10-19 14:42:29 -07002057
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002058 if (smacro_defined(ctx, mname, nparam, &smac, casesense)) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002059 if (!smac) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002060 nasm_error(ERR_WARNING|ERR_PASS1,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002061 "single-line macro `%s' defined both with and"
2062 " without parameters", mname);
2063 /*
2064 * Some instances of the old code considered this a failure,
2065 * some others didn't. What is the right thing to do here?
2066 */
2067 free_tlist(expansion);
2068 return false; /* Failure */
2069 } else {
2070 /*
2071 * We're redefining, so we have to take over an
2072 * existing SMacro structure. This means freeing
2073 * what was already in it.
2074 */
2075 nasm_free(smac->name);
2076 free_tlist(smac->expansion);
2077 }
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002078 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002079 smtbl = ctx ? &ctx->localmac : &smacros;
2080 smhead = (SMacro **) hash_findi_add(smtbl, mname);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002081 smac = nasm_malloc(sizeof(SMacro));
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002082 smac->next = *smhead;
2083 *smhead = smac;
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002084 }
2085 smac->name = nasm_strdup(mname);
2086 smac->casesense = casesense;
2087 smac->nparam = nparam;
2088 smac->expansion = expansion;
2089 smac->in_progress = false;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002090 return true; /* Success */
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002091}
2092
2093/*
2094 * Undefine an smacro
2095 */
2096static void undef_smacro(Context *ctx, const char *mname)
2097{
2098 SMacro **smhead, *s, **sp;
H. Peter Anvin166c2472008-05-28 12:28:58 -07002099 struct hash_table *smtbl;
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002100
H. Peter Anvin166c2472008-05-28 12:28:58 -07002101 smtbl = ctx ? &ctx->localmac : &smacros;
2102 smhead = (SMacro **)hash_findi(smtbl, mname, NULL);
H. Peter Anvin70653092007-10-19 14:42:29 -07002103
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002104 if (smhead) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002105 /*
2106 * We now have a macro name... go hunt for it.
2107 */
2108 sp = smhead;
2109 while ((s = *sp) != NULL) {
2110 if (!mstrcmp(s->name, mname, s->casesense)) {
2111 *sp = s->next;
2112 nasm_free(s->name);
2113 free_tlist(s->expansion);
2114 nasm_free(s);
2115 } else {
2116 sp = &s->next;
2117 }
2118 }
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002119 }
2120}
2121
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002122/*
H. Peter Anvina26433d2008-07-16 14:40:01 -07002123 * Parse a mmacro specification.
2124 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002125static bool parse_mmacro_spec(Token *tline, MMacro *def, const char *directive)
H. Peter Anvina26433d2008-07-16 14:40:01 -07002126{
2127 bool err;
2128
2129 tline = tline->next;
2130 skip_white_(tline);
2131 tline = expand_id(tline);
2132 if (!tok_type_(tline, TOK_ID)) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002133 nasm_error(ERR_NONFATAL, "`%s' expects a macro name", directive);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002134 return false;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002135 }
Victor van den Elzenb916ede2008-07-23 15:14:22 +02002136
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002137 def->prev = NULL;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002138 def->name = nasm_strdup(tline->text);
2139 def->plus = false;
2140 def->nolist = false;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002141 def->in_progress = 0;
2142 def->rep_nest = NULL;
Victor van den Elzenb916ede2008-07-23 15:14:22 +02002143 def->nparam_min = 0;
2144 def->nparam_max = 0;
2145
H. Peter Anvina26433d2008-07-16 14:40:01 -07002146 tline = expand_smacro(tline->next);
2147 skip_white_(tline);
2148 if (!tok_type_(tline, TOK_NUMBER)) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002149 nasm_error(ERR_NONFATAL, "`%s' expects a parameter count", directive);
H. Peter Anvina26433d2008-07-16 14:40:01 -07002150 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002151 def->nparam_min = def->nparam_max =
2152 readnum(tline->text, &err);
2153 if (err)
H. Peter Anvin130736c2016-02-17 20:27:41 -08002154 nasm_error(ERR_NONFATAL,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002155 "unable to parse parameter count `%s'", tline->text);
H. Peter Anvina26433d2008-07-16 14:40:01 -07002156 }
2157 if (tline && tok_is_(tline->next, "-")) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002158 tline = tline->next->next;
2159 if (tok_is_(tline, "*")) {
2160 def->nparam_max = INT_MAX;
2161 } else if (!tok_type_(tline, TOK_NUMBER)) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002162 nasm_error(ERR_NONFATAL,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002163 "`%s' expects a parameter count after `-'", directive);
2164 } else {
2165 def->nparam_max = readnum(tline->text, &err);
2166 if (err) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002167 nasm_error(ERR_NONFATAL, "unable to parse parameter count `%s'",
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002168 tline->text);
2169 }
2170 if (def->nparam_min > def->nparam_max) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002171 nasm_error(ERR_NONFATAL, "minimum parameter count exceeds maximum");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002172 }
2173 }
H. Peter Anvina26433d2008-07-16 14:40:01 -07002174 }
2175 if (tline && tok_is_(tline->next, "+")) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002176 tline = tline->next;
2177 def->plus = true;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002178 }
2179 if (tline && tok_type_(tline->next, TOK_ID) &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002180 !nasm_stricmp(tline->next->text, ".nolist")) {
2181 tline = tline->next;
2182 def->nolist = true;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002183 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002184
H. Peter Anvina26433d2008-07-16 14:40:01 -07002185 /*
2186 * Handle default parameters.
2187 */
2188 if (tline && tline->next) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002189 def->dlist = tline->next;
2190 tline->next = NULL;
2191 count_mmac_params(def->dlist, &def->ndefs, &def->defaults);
H. Peter Anvina26433d2008-07-16 14:40:01 -07002192 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002193 def->dlist = NULL;
2194 def->defaults = NULL;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002195 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002196 def->expansion = NULL;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002197
H. Peter Anvin89cee572009-07-15 09:16:54 -04002198 if (def->defaults && def->ndefs > def->nparam_max - def->nparam_min &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002199 !def->plus)
H. Peter Anvin130736c2016-02-17 20:27:41 -08002200 nasm_error(ERR_WARNING|ERR_PASS1|ERR_WARN_MDP,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002201 "too many default macro parameters");
Victor van den Elzenb916ede2008-07-23 15:14:22 +02002202
H. Peter Anvina26433d2008-07-16 14:40:01 -07002203 return true;
2204}
2205
2206
2207/*
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002208 * Decode a size directive
2209 */
2210static int parse_size(const char *str) {
2211 static const char *size_names[] =
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002212 { "byte", "dword", "oword", "qword", "tword", "word", "yword" };
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002213 static const int sizes[] =
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002214 { 0, 1, 4, 16, 8, 10, 2, 32 };
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002215
Cyrill Gorcunova7319242010-06-03 22:04:36 +04002216 return sizes[bsii(str, size_names, ARRAY_SIZE(size_names))+1];
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002217}
2218
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07002219/*
2220 * Process a preprocessor %pragma directive. Currently there are none.
2221 * Gets passed the token list starting with the "preproc" token from
2222 * "%pragma preproc".
2223 */
2224static void do_pragma_preproc(Token *tline)
2225{
2226 /* Skip to the real stuff */
2227 tline = tline->next;
2228 skip_white_(tline);
2229 if (!tline)
2230 return;
2231
2232 (void)tline; /* Nothing else to do at present */
2233}
2234
Ed Beroset3ab3f412002-06-11 03:31:49 +00002235/**
2236 * find and process preprocessor directive in passed line
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002237 * Find out if a line contains a preprocessor directive, and deal
2238 * with it if so.
H. Peter Anvin70653092007-10-19 14:42:29 -07002239 *
Ed Beroset3ab3f412002-06-11 03:31:49 +00002240 * If a directive _is_ found, it is the responsibility of this routine
2241 * (and not the caller) to free_tlist() the line.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002242 *
Ed Beroset3ab3f412002-06-11 03:31:49 +00002243 * @param tline a pointer to the current tokeninzed line linked list
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07002244 * @param output if this directive generated output
Ed Beroset3ab3f412002-06-11 03:31:49 +00002245 * @return DIRECTIVE_FOUND or NO_DIRECTIVE_FOUND
H. Peter Anvin70653092007-10-19 14:42:29 -07002246 *
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002247 */
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07002248static int do_directive(Token *tline, char **output)
H. Peter Anvineba20a72002-04-30 20:53:55 +00002249{
H. Peter Anvin4169a472007-09-12 01:29:43 +00002250 enum preproc_token i;
2251 int j;
H. Peter Anvin70055962007-10-11 00:05:31 -07002252 bool err;
2253 int nparam;
2254 bool nolist;
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07002255 bool casesense;
H. Peter Anvin8cfdb9d2007-09-14 18:36:01 -07002256 int k, m;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002257 int offset;
H. Peter Anvinccad6f92016-10-04 00:34:35 -07002258 char *p, *pp;
2259 const char *found_path;
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08002260 const char *mname;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002261 Include *inc;
2262 Context *ctx;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002263 Cond *cond;
2264 MMacro *mmac, **mmhead;
Jin Kyu Songc9486b92013-10-28 17:07:57 -07002265 Token *t = NULL, *tt, *param_start, *macro_start, *last, **tptr, *origline;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002266 Line *l;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002267 struct tokenval tokval;
2268 expr *evalresult;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002269 MMacro *tmp_defining; /* Used when manipulating rep_nest */
H. Peter Anvinf8ba53e2007-10-11 10:11:57 -07002270 int64_t count;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07002271 size_t len;
H. Peter Anvin8e3f75e2008-09-24 00:21:58 -07002272 int severity;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002273
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07002274 *output = NULL; /* No output generated */
H. Peter Anvin76690a12002-04-30 20:52:49 +00002275 origline = tline;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002276
H. Peter Anvineba20a72002-04-30 20:53:55 +00002277 skip_white_(tline);
H. Peter Anvinf2936d72008-06-04 15:11:23 -07002278 if (!tline || !tok_type_(tline, TOK_PREPROC_ID) ||
H. Peter Anvine2c80182005-01-15 22:15:51 +00002279 (tline->text[1] == '%' || tline->text[1] == '$'
2280 || tline->text[1] == '!'))
2281 return NO_DIRECTIVE_FOUND;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002282
H. Peter Anvin4169a472007-09-12 01:29:43 +00002283 i = pp_token_hash(tline->text);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002284
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002285 /*
2286 * FIXME: We zap execution of PP_RMACRO, PP_IRMACRO, PP_EXITMACRO
2287 * since they are known to be buggy at moment, we need to fix them
2288 * in future release (2.09-2.10)
2289 */
Philipp Klokeb432f572013-03-31 12:01:23 +02002290 if (i == PP_RMACRO || i == PP_IRMACRO || i == PP_EXITMACRO) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002291 nasm_error(ERR_NONFATAL, "unknown preprocessor directive `%s'",
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002292 tline->text);
2293 return NO_DIRECTIVE_FOUND;
2294 }
2295
2296 /*
2297 * If we're in a non-emitting branch of a condition construct,
2298 * or walking to the end of an already terminated %rep block,
2299 * we should ignore all directives except for condition
2300 * directives.
2301 */
2302 if (((istk->conds && !emitting(istk->conds->state)) ||
2303 (istk->mstk && !istk->mstk->in_progress)) && !is_condition(i)) {
2304 return NO_DIRECTIVE_FOUND;
2305 }
2306
2307 /*
2308 * If we're defining a macro or reading a %rep block, we should
2309 * ignore all directives except for %macro/%imacro (which nest),
2310 * %endm/%endmacro, and (only if we're in a %rep block) %endrep.
2311 * If we're in a %rep block, another %rep nests, so should be let through.
2312 */
2313 if (defining && i != PP_MACRO && i != PP_IMACRO &&
2314 i != PP_RMACRO && i != PP_IRMACRO &&
2315 i != PP_ENDMACRO && i != PP_ENDM &&
2316 (defining->name || (i != PP_ENDREP && i != PP_REP))) {
2317 return NO_DIRECTIVE_FOUND;
2318 }
2319
2320 if (defining) {
2321 if (i == PP_MACRO || i == PP_IMACRO ||
2322 i == PP_RMACRO || i == PP_IRMACRO) {
2323 nested_mac_count++;
2324 return NO_DIRECTIVE_FOUND;
2325 } else if (nested_mac_count > 0) {
2326 if (i == PP_ENDMACRO) {
2327 nested_mac_count--;
2328 return NO_DIRECTIVE_FOUND;
2329 }
2330 }
2331 if (!defining->name) {
2332 if (i == PP_REP) {
2333 nested_rep_count++;
2334 return NO_DIRECTIVE_FOUND;
2335 } else if (nested_rep_count > 0) {
2336 if (i == PP_ENDREP) {
2337 nested_rep_count--;
2338 return NO_DIRECTIVE_FOUND;
2339 }
2340 }
2341 }
2342 }
2343
H. Peter Anvin4169a472007-09-12 01:29:43 +00002344 switch (i) {
2345 case PP_INVALID:
H. Peter Anvin130736c2016-02-17 20:27:41 -08002346 nasm_error(ERR_NONFATAL, "unknown preprocessor directive `%s'",
H. Peter Anvine2c80182005-01-15 22:15:51 +00002347 tline->text);
2348 return NO_DIRECTIVE_FOUND; /* didn't get it */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002349
H. Peter Anvin3f87a2a2016-10-04 14:07:19 -07002350 case PP_PRAGMA:
2351 /*
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07002352 * %pragma namespace options...
2353 *
2354 * The namespace "preproc" is reserved for the preprocessor;
2355 * all other namespaces generate a [pragma] assembly directive.
2356 *
2357 * Invalid %pragmas are ignored and may have different
2358 * meaning in future versions of NASM.
H. Peter Anvin3f87a2a2016-10-04 14:07:19 -07002359 */
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07002360 tline = tline->next;
2361 skip_white_(tline);
2362 tline = expand_smacro(tline);
2363 if (tok_type_(tline, TOK_ID)) {
2364 if (!nasm_stricmp(tline->text, "preproc")) {
2365 /* Preprocessor pragma */
2366 do_pragma_preproc(tline);
2367 } else {
2368 /* Build the assembler directive */
2369 t = new_Token(NULL, TOK_OTHER, "[", 1);
2370 t->next = new_Token(NULL, TOK_ID, "pragma", 6);
2371 t->next->next = new_Token(tline, TOK_WHITESPACE, NULL, 0);
2372 tline = t;
2373 for (t = tline; t->next; t = t->next)
2374 ;
2375 t->next = new_Token(NULL, TOK_OTHER, "]", 1);
2376 /* true here can be revisited in the future */
2377 *output = detoken(tline, true);
2378 }
2379 }
H. Peter Anvin3f87a2a2016-10-04 14:07:19 -07002380 free_tlist(origline);
2381 return DIRECTIVE_FOUND;
2382
H. Peter Anvine2c80182005-01-15 22:15:51 +00002383 case PP_STACKSIZE:
2384 /* Directive to tell NASM what the default stack size is. The
2385 * default is for a 16-bit stack, and this can be overriden with
2386 * %stacksize large.
H. Peter Anvine2c80182005-01-15 22:15:51 +00002387 */
2388 tline = tline->next;
2389 if (tline && tline->type == TOK_WHITESPACE)
2390 tline = tline->next;
2391 if (!tline || tline->type != TOK_ID) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002392 nasm_error(ERR_NONFATAL, "`%%stacksize' missing size parameter");
H. Peter Anvine2c80182005-01-15 22:15:51 +00002393 free_tlist(origline);
2394 return DIRECTIVE_FOUND;
2395 }
2396 if (nasm_stricmp(tline->text, "flat") == 0) {
2397 /* All subsequent ARG directives are for a 32-bit stack */
2398 StackSize = 4;
2399 StackPointer = "ebp";
2400 ArgOffset = 8;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002401 LocalOffset = 0;
Charles Crayne7eaf9192007-11-08 22:11:14 -08002402 } else if (nasm_stricmp(tline->text, "flat64") == 0) {
2403 /* All subsequent ARG directives are for a 64-bit stack */
2404 StackSize = 8;
2405 StackPointer = "rbp";
Per Jessen53252e02010-02-11 00:16:59 +03002406 ArgOffset = 16;
Charles Crayne7eaf9192007-11-08 22:11:14 -08002407 LocalOffset = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002408 } else if (nasm_stricmp(tline->text, "large") == 0) {
2409 /* All subsequent ARG directives are for a 16-bit stack,
2410 * far function call.
2411 */
2412 StackSize = 2;
2413 StackPointer = "bp";
2414 ArgOffset = 4;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002415 LocalOffset = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002416 } else if (nasm_stricmp(tline->text, "small") == 0) {
2417 /* All subsequent ARG directives are for a 16-bit stack,
2418 * far function call. We don't support near functions.
2419 */
2420 StackSize = 2;
2421 StackPointer = "bp";
2422 ArgOffset = 6;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002423 LocalOffset = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002424 } else {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002425 nasm_error(ERR_NONFATAL, "`%%stacksize' invalid size type");
H. Peter Anvine2c80182005-01-15 22:15:51 +00002426 free_tlist(origline);
2427 return DIRECTIVE_FOUND;
2428 }
2429 free_tlist(origline);
2430 return DIRECTIVE_FOUND;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002431
H. Peter Anvine2c80182005-01-15 22:15:51 +00002432 case PP_ARG:
2433 /* TASM like ARG directive to define arguments to functions, in
2434 * the following form:
2435 *
2436 * ARG arg1:WORD, arg2:DWORD, arg4:QWORD
2437 */
2438 offset = ArgOffset;
2439 do {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00002440 char *arg, directive[256];
H. Peter Anvine2c80182005-01-15 22:15:51 +00002441 int size = StackSize;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002442
H. Peter Anvine2c80182005-01-15 22:15:51 +00002443 /* Find the argument name */
2444 tline = tline->next;
2445 if (tline && tline->type == TOK_WHITESPACE)
2446 tline = tline->next;
2447 if (!tline || tline->type != TOK_ID) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002448 nasm_error(ERR_NONFATAL, "`%%arg' missing argument parameter");
H. Peter Anvine2c80182005-01-15 22:15:51 +00002449 free_tlist(origline);
2450 return DIRECTIVE_FOUND;
2451 }
2452 arg = tline->text;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002453
H. Peter Anvine2c80182005-01-15 22:15:51 +00002454 /* Find the argument size type */
2455 tline = tline->next;
2456 if (!tline || tline->type != TOK_OTHER
2457 || tline->text[0] != ':') {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002458 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00002459 "Syntax error processing `%%arg' directive");
2460 free_tlist(origline);
2461 return DIRECTIVE_FOUND;
2462 }
2463 tline = tline->next;
2464 if (!tline || tline->type != TOK_ID) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002465 nasm_error(ERR_NONFATAL, "`%%arg' missing size type parameter");
H. Peter Anvine2c80182005-01-15 22:15:51 +00002466 free_tlist(origline);
2467 return DIRECTIVE_FOUND;
2468 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002469
H. Peter Anvine2c80182005-01-15 22:15:51 +00002470 /* Allow macro expansion of type parameter */
Keith Kaniosb7a89542007-04-12 02:40:54 +00002471 tt = tokenize(tline->text);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002472 tt = expand_smacro(tt);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002473 size = parse_size(tt->text);
2474 if (!size) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002475 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00002476 "Invalid size type for `%%arg' missing directive");
2477 free_tlist(tt);
2478 free_tlist(origline);
2479 return DIRECTIVE_FOUND;
2480 }
2481 free_tlist(tt);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002482
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002483 /* Round up to even stack slots */
2484 size = ALIGN(size, StackSize);
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002485
H. Peter Anvine2c80182005-01-15 22:15:51 +00002486 /* Now define the macro for the argument */
2487 snprintf(directive, sizeof(directive), "%%define %s (%s+%d)",
2488 arg, StackPointer, offset);
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07002489 do_directive(tokenize(directive), output);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002490 offset += size;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002491
H. Peter Anvine2c80182005-01-15 22:15:51 +00002492 /* Move to the next argument in the list */
2493 tline = tline->next;
2494 if (tline && tline->type == TOK_WHITESPACE)
2495 tline = tline->next;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002496 } while (tline && tline->type == TOK_OTHER && tline->text[0] == ',');
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002497 ArgOffset = offset;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002498 free_tlist(origline);
2499 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002500
H. Peter Anvine2c80182005-01-15 22:15:51 +00002501 case PP_LOCAL:
2502 /* TASM like LOCAL directive to define local variables for a
2503 * function, in the following form:
2504 *
2505 * LOCAL local1:WORD, local2:DWORD, local4:QWORD = LocalSize
2506 *
2507 * The '= LocalSize' at the end is ignored by NASM, but is
2508 * required by TASM to define the local parameter size (and used
2509 * by the TASM macro package).
2510 */
2511 offset = LocalOffset;
2512 do {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00002513 char *local, directive[256];
H. Peter Anvine2c80182005-01-15 22:15:51 +00002514 int size = StackSize;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002515
H. Peter Anvine2c80182005-01-15 22:15:51 +00002516 /* Find the argument name */
2517 tline = tline->next;
2518 if (tline && tline->type == TOK_WHITESPACE)
2519 tline = tline->next;
2520 if (!tline || tline->type != TOK_ID) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002521 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00002522 "`%%local' missing argument parameter");
2523 free_tlist(origline);
2524 return DIRECTIVE_FOUND;
2525 }
2526 local = tline->text;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002527
H. Peter Anvine2c80182005-01-15 22:15:51 +00002528 /* Find the argument size type */
2529 tline = tline->next;
2530 if (!tline || tline->type != TOK_OTHER
2531 || tline->text[0] != ':') {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002532 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00002533 "Syntax error processing `%%local' directive");
2534 free_tlist(origline);
2535 return DIRECTIVE_FOUND;
2536 }
2537 tline = tline->next;
2538 if (!tline || tline->type != TOK_ID) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002539 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00002540 "`%%local' missing size type parameter");
2541 free_tlist(origline);
2542 return DIRECTIVE_FOUND;
2543 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00002544
H. Peter Anvine2c80182005-01-15 22:15:51 +00002545 /* Allow macro expansion of type parameter */
Keith Kaniosb7a89542007-04-12 02:40:54 +00002546 tt = tokenize(tline->text);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002547 tt = expand_smacro(tt);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002548 size = parse_size(tt->text);
2549 if (!size) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002550 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00002551 "Invalid size type for `%%local' missing directive");
2552 free_tlist(tt);
2553 free_tlist(origline);
2554 return DIRECTIVE_FOUND;
2555 }
2556 free_tlist(tt);
H. Peter Anvin734b1882002-04-30 21:01:08 +00002557
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002558 /* Round up to even stack slots */
2559 size = ALIGN(size, StackSize);
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002560
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002561 offset += size; /* Negative offset, increment before */
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002562
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002563 /* Now define the macro for the argument */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002564 snprintf(directive, sizeof(directive), "%%define %s (%s-%d)",
2565 local, StackPointer, offset);
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07002566 do_directive(tokenize(directive), output);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002567
H. Peter Anvine2c80182005-01-15 22:15:51 +00002568 /* Now define the assign to setup the enter_c macro correctly */
2569 snprintf(directive, sizeof(directive),
2570 "%%assign %%$localsize %%$localsize+%d", size);
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07002571 do_directive(tokenize(directive), output);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002572
H. Peter Anvine2c80182005-01-15 22:15:51 +00002573 /* Move to the next argument in the list */
2574 tline = tline->next;
2575 if (tline && tline->type == TOK_WHITESPACE)
2576 tline = tline->next;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002577 } while (tline && tline->type == TOK_OTHER && tline->text[0] == ',');
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002578 LocalOffset = offset;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002579 free_tlist(origline);
2580 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002581
H. Peter Anvine2c80182005-01-15 22:15:51 +00002582 case PP_CLEAR:
2583 if (tline->next)
H. Peter Anvin130736c2016-02-17 20:27:41 -08002584 nasm_error(ERR_WARNING|ERR_PASS1,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002585 "trailing garbage after `%%clear' ignored");
2586 free_macros();
2587 init_macros();
H. Peter Anvine2c80182005-01-15 22:15:51 +00002588 free_tlist(origline);
2589 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002590
H. Peter Anvin418ca702008-05-30 10:42:30 -07002591 case PP_DEPEND:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002592 t = tline->next = expand_smacro(tline->next);
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002593 skip_white_(t);
2594 if (!t || (t->type != TOK_STRING &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002595 t->type != TOK_INTERNAL_STRING)) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002596 nasm_error(ERR_NONFATAL, "`%%depend' expects a file name");
H. Peter Anvin418ca702008-05-30 10:42:30 -07002597 free_tlist(origline);
2598 return DIRECTIVE_FOUND; /* but we did _something_ */
2599 }
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002600 if (t->next)
H. Peter Anvin130736c2016-02-17 20:27:41 -08002601 nasm_error(ERR_WARNING|ERR_PASS1,
H. Peter Anvin418ca702008-05-30 10:42:30 -07002602 "trailing garbage after `%%depend' ignored");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002603 p = t->text;
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002604 if (t->type != TOK_INTERNAL_STRING)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002605 nasm_unquote_cstr(p, i);
H. Peter Anvin436e3672016-10-04 01:12:28 -07002606 nasm_add_string_to_strlist(dephead, p);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002607 free_tlist(origline);
H. Peter Anvin418ca702008-05-30 10:42:30 -07002608 return DIRECTIVE_FOUND;
2609
2610 case PP_INCLUDE:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002611 t = tline->next = expand_smacro(tline->next);
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002612 skip_white_(t);
H. Peter Anvind2456592008-06-19 15:04:18 -07002613
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002614 if (!t || (t->type != TOK_STRING &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002615 t->type != TOK_INTERNAL_STRING)) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002616 nasm_error(ERR_NONFATAL, "`%%include' expects a file name");
H. Peter Anvine2c80182005-01-15 22:15:51 +00002617 free_tlist(origline);
2618 return DIRECTIVE_FOUND; /* but we did _something_ */
2619 }
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002620 if (t->next)
H. Peter Anvin130736c2016-02-17 20:27:41 -08002621 nasm_error(ERR_WARNING|ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +00002622 "trailing garbage after `%%include' ignored");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002623 p = t->text;
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002624 if (t->type != TOK_INTERNAL_STRING)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002625 nasm_unquote_cstr(p, i);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002626 inc = nasm_malloc(sizeof(Include));
H. Peter Anvine2c80182005-01-15 22:15:51 +00002627 inc->next = istk;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002628 inc->conds = NULL;
Jim Kukunas65a8afc2016-06-13 16:00:42 -04002629 found_path = NULL;
H. Peter Anvin9924d1e2016-10-04 00:59:39 -07002630 inc->fp = inc_fopen(p, dephead, &found_path,
H. Peter Anvind81a2352016-09-21 14:03:18 -07002631 pass == 0 ? INC_OPTIONAL : INC_NEEDED, NF_TEXT);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002632 if (!inc->fp) {
2633 /* -MG given but file not found */
2634 nasm_free(inc);
2635 } else {
Jim Kukunas65a8afc2016-06-13 16:00:42 -04002636 inc->fname = src_set_fname(found_path ? found_path : p);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002637 inc->lineno = src_set_linnum(0);
2638 inc->lineinc = 1;
2639 inc->expansion = NULL;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002640 inc->mstk = NULL;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002641 istk = inc;
H. Peter Anvin8ac25aa2016-02-18 01:16:18 -08002642 lfmt->uplevel(LIST_INCLUDE);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002643 }
2644 free_tlist(origline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002645 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002646
H. Peter Anvind2456592008-06-19 15:04:18 -07002647 case PP_USE:
H. Peter Anvinf4ae5ad2008-06-19 18:39:24 -07002648 {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002649 static macros_t *use_pkg;
2650 const char *pkg_macro = NULL;
H. Peter Anvinf4ae5ad2008-06-19 18:39:24 -07002651
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002652 tline = tline->next;
2653 skip_white_(tline);
2654 tline = expand_id(tline);
H. Peter Anvind2456592008-06-19 15:04:18 -07002655
H. Peter Anvin264b7b92008-10-24 16:38:17 -07002656 if (!tline || (tline->type != TOK_STRING &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002657 tline->type != TOK_INTERNAL_STRING &&
2658 tline->type != TOK_ID)) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002659 nasm_error(ERR_NONFATAL, "`%%use' expects a package name");
H. Peter Anvind2456592008-06-19 15:04:18 -07002660 free_tlist(origline);
2661 return DIRECTIVE_FOUND; /* but we did _something_ */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002662 }
H. Peter Anvin264b7b92008-10-24 16:38:17 -07002663 if (tline->next)
H. Peter Anvin130736c2016-02-17 20:27:41 -08002664 nasm_error(ERR_WARNING|ERR_PASS1,
H. Peter Anvind2456592008-06-19 15:04:18 -07002665 "trailing garbage after `%%use' ignored");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002666 if (tline->type == TOK_STRING)
2667 nasm_unquote_cstr(tline->text, i);
2668 use_pkg = nasm_stdmac_find_package(tline->text);
2669 if (!use_pkg)
H. Peter Anvin130736c2016-02-17 20:27:41 -08002670 nasm_error(ERR_NONFATAL, "unknown `%%use' package: %s", tline->text);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002671 else
2672 pkg_macro = (char *)use_pkg + 1; /* The first string will be <%define>__USE_*__ */
Victor van den Elzen35eb2ea2010-03-10 22:33:48 +01002673 if (use_pkg && ! smacro_defined(NULL, pkg_macro, 0, NULL, true)) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002674 /* Not already included, go ahead and include it */
2675 stdmacpos = use_pkg;
2676 }
2677 free_tlist(origline);
H. Peter Anvind2456592008-06-19 15:04:18 -07002678 return DIRECTIVE_FOUND;
H. Peter Anvinf4ae5ad2008-06-19 18:39:24 -07002679 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002680 case PP_PUSH:
H. Peter Anvine2c80182005-01-15 22:15:51 +00002681 case PP_REPL:
H. Peter Anvin42b56392008-10-24 16:24:21 -07002682 case PP_POP:
H. Peter Anvine2c80182005-01-15 22:15:51 +00002683 tline = tline->next;
2684 skip_white_(tline);
2685 tline = expand_id(tline);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002686 if (tline) {
2687 if (!tok_type_(tline, TOK_ID)) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002688 nasm_error(ERR_NONFATAL, "`%s' expects a context identifier",
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002689 pp_directives[i]);
2690 free_tlist(origline);
2691 return DIRECTIVE_FOUND; /* but we did _something_ */
2692 }
2693 if (tline->next)
H. Peter Anvin130736c2016-02-17 20:27:41 -08002694 nasm_error(ERR_WARNING|ERR_PASS1,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002695 "trailing garbage after `%s' ignored",
2696 pp_directives[i]);
2697 p = nasm_strdup(tline->text);
2698 } else {
2699 p = NULL; /* Anonymous */
2700 }
H. Peter Anvin42b56392008-10-24 16:24:21 -07002701
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002702 if (i == PP_PUSH) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002703 ctx = nasm_malloc(sizeof(Context));
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002704 ctx->next = cstk;
2705 hash_init(&ctx->localmac, HASH_SMALL);
2706 ctx->name = p;
2707 ctx->number = unique++;
2708 cstk = ctx;
2709 } else {
2710 /* %pop or %repl */
2711 if (!cstk) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002712 nasm_error(ERR_NONFATAL, "`%s': context stack is empty",
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002713 pp_directives[i]);
2714 } else if (i == PP_POP) {
2715 if (p && (!cstk->name || nasm_stricmp(p, cstk->name)))
H. Peter Anvin130736c2016-02-17 20:27:41 -08002716 nasm_error(ERR_NONFATAL, "`%%pop' in wrong context: %s, "
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002717 "expected %s",
2718 cstk->name ? cstk->name : "anonymous", p);
2719 else
2720 ctx_pop();
2721 } else {
2722 /* i == PP_REPL */
2723 nasm_free(cstk->name);
2724 cstk->name = p;
2725 p = NULL;
2726 }
2727 nasm_free(p);
2728 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002729 free_tlist(origline);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002730 return DIRECTIVE_FOUND;
H. Peter Anvin8e3f75e2008-09-24 00:21:58 -07002731 case PP_FATAL:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002732 severity = ERR_FATAL;
2733 goto issue_error;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002734 case PP_ERROR:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002735 severity = ERR_NONFATAL;
2736 goto issue_error;
H. Peter Anvin7df04172008-06-10 18:27:38 -07002737 case PP_WARNING:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002738 severity = ERR_WARNING|ERR_WARN_USER;
2739 goto issue_error;
H. Peter Anvin8e3f75e2008-09-24 00:21:58 -07002740
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002741issue_error:
H. Peter Anvin7df04172008-06-10 18:27:38 -07002742 {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002743 /* Only error out if this is the final pass */
2744 if (pass != 2 && i != PP_FATAL)
2745 return DIRECTIVE_FOUND;
2746
2747 tline->next = expand_smacro(tline->next);
2748 tline = tline->next;
2749 skip_white_(tline);
2750 t = tline ? tline->next : NULL;
2751 skip_white_(t);
2752 if (tok_type_(tline, TOK_STRING) && !t) {
2753 /* The line contains only a quoted string */
2754 p = tline->text;
2755 nasm_unquote(p, NULL); /* Ignore NUL character truncation */
H. Peter Anvin130736c2016-02-17 20:27:41 -08002756 nasm_error(severity, "%s", p);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002757 } else {
2758 /* Not a quoted string, or more than a quoted string */
2759 p = detoken(tline, false);
H. Peter Anvin130736c2016-02-17 20:27:41 -08002760 nasm_error(severity, "%s", p);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002761 nasm_free(p);
2762 }
2763 free_tlist(origline);
2764 return DIRECTIVE_FOUND;
H. Peter Anvin7df04172008-06-10 18:27:38 -07002765 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00002766
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002767 CASE_PP_IF:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002768 if (istk->conds && !emitting(istk->conds->state))
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002769 j = COND_NEVER;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002770 else {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002771 j = if_condition(tline->next, i);
2772 tline->next = NULL; /* it got freed */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002773 j = j < 0 ? COND_NEVER : j ? COND_IF_TRUE : COND_IF_FALSE;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002774 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002775 cond = nasm_malloc(sizeof(Cond));
2776 cond->next = istk->conds;
2777 cond->state = j;
2778 istk->conds = cond;
2779 if(istk->mstk)
2780 istk->mstk->condcnt ++;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002781 free_tlist(origline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002782 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002783
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002784 CASE_PP_ELIF:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002785 if (!istk->conds)
H. Peter Anvin130736c2016-02-17 20:27:41 -08002786 nasm_error(ERR_FATAL, "`%s': no matching `%%if'", pp_directives[i]);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002787 switch(istk->conds->state) {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002788 case COND_IF_TRUE:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002789 istk->conds->state = COND_DONE;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002790 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002791
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002792 case COND_DONE:
2793 case COND_NEVER:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002794 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002795
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002796 case COND_ELSE_TRUE:
2797 case COND_ELSE_FALSE:
H. Peter Anvin130736c2016-02-17 20:27:41 -08002798 nasm_error(ERR_WARNING|ERR_PASS1|ERR_PP_PRECOND,
2799 "`%%elif' after `%%else' ignored");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002800 istk->conds->state = COND_NEVER;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002801 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002802
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002803 case COND_IF_FALSE:
2804 /*
2805 * IMPORTANT: In the case of %if, we will already have
2806 * called expand_mmac_params(); however, if we're
2807 * processing an %elif we must have been in a
2808 * non-emitting mode, which would have inhibited
2809 * the normal invocation of expand_mmac_params().
2810 * Therefore, we have to do it explicitly here.
2811 */
2812 j = if_condition(expand_mmac_params(tline->next), i);
2813 tline->next = NULL; /* it got freed */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002814 istk->conds->state =
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002815 j < 0 ? COND_NEVER : j ? COND_IF_TRUE : COND_IF_FALSE;
2816 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002817 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002818 free_tlist(origline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002819 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002820
H. Peter Anvine2c80182005-01-15 22:15:51 +00002821 case PP_ELSE:
2822 if (tline->next)
H. Peter Anvin130736c2016-02-17 20:27:41 -08002823 nasm_error(ERR_WARNING|ERR_PASS1|ERR_PP_PRECOND,
2824 "trailing garbage after `%%else' ignored");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002825 if (!istk->conds)
H. Peter Anvin130736c2016-02-17 20:27:41 -08002826 nasm_fatal(0, "`%%else: no matching `%%if'");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002827 switch(istk->conds->state) {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002828 case COND_IF_TRUE:
2829 case COND_DONE:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002830 istk->conds->state = COND_ELSE_FALSE;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002831 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002832
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002833 case COND_NEVER:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002834 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002835
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002836 case COND_IF_FALSE:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002837 istk->conds->state = COND_ELSE_TRUE;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002838 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002839
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002840 case COND_ELSE_TRUE:
2841 case COND_ELSE_FALSE:
H. Peter Anvin130736c2016-02-17 20:27:41 -08002842 nasm_error(ERR_WARNING|ERR_PASS1|ERR_PP_PRECOND,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002843 "`%%else' after `%%else' ignored.");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002844 istk->conds->state = COND_NEVER;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002845 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002846 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002847 free_tlist(origline);
2848 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002849
H. Peter Anvine2c80182005-01-15 22:15:51 +00002850 case PP_ENDIF:
2851 if (tline->next)
H. Peter Anvin130736c2016-02-17 20:27:41 -08002852 nasm_error(ERR_WARNING|ERR_PASS1|ERR_PP_PRECOND,
2853 "trailing garbage after `%%endif' ignored");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002854 if (!istk->conds)
H. Peter Anvin130736c2016-02-17 20:27:41 -08002855 nasm_error(ERR_FATAL, "`%%endif': no matching `%%if'");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002856 cond = istk->conds;
2857 istk->conds = cond->next;
2858 nasm_free(cond);
2859 if(istk->mstk)
2860 istk->mstk->condcnt --;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002861 free_tlist(origline);
2862 return DIRECTIVE_FOUND;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002863
H. Peter Anvindb8f96e2009-07-15 09:07:29 -04002864 case PP_RMACRO:
2865 case PP_IRMACRO:
H. Peter Anvine2c80182005-01-15 22:15:51 +00002866 case PP_MACRO:
2867 case PP_IMACRO:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002868 if (defining) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002869 nasm_error(ERR_FATAL, "`%s': already defining a macro",
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002870 pp_directives[i]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002871 return DIRECTIVE_FOUND;
2872 }
H. Peter Anvin4def1a82016-05-09 13:59:44 -07002873 defining = nasm_zalloc(sizeof(MMacro));
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002874 defining->max_depth =
2875 (i == PP_RMACRO) || (i == PP_IRMACRO) ? DEADMAN_LIMIT : 0;
2876 defining->casesense = (i == PP_MACRO) || (i == PP_RMACRO);
2877 if (!parse_mmacro_spec(tline, defining, pp_directives[i])) {
2878 nasm_free(defining);
2879 defining = NULL;
2880 return DIRECTIVE_FOUND;
2881 }
H. Peter Anvina26433d2008-07-16 14:40:01 -07002882
H. Peter Anvin4def1a82016-05-09 13:59:44 -07002883 src_get(&defining->xline, &defining->fname);
2884
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002885 mmac = (MMacro *) hash_findix(&mmacros, defining->name);
2886 while (mmac) {
2887 if (!strcmp(mmac->name, defining->name) &&
2888 (mmac->nparam_min <= defining->nparam_max
2889 || defining->plus)
2890 && (defining->nparam_min <= mmac->nparam_max
2891 || mmac->plus)) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002892 nasm_error(ERR_WARNING|ERR_PASS1,
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002893 "redefining multi-line macro `%s'", defining->name);
2894 return DIRECTIVE_FOUND;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002895 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002896 mmac = mmac->next;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002897 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002898 free_tlist(origline);
2899 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002900
H. Peter Anvine2c80182005-01-15 22:15:51 +00002901 case PP_ENDM:
2902 case PP_ENDMACRO:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002903 if (! (defining && defining->name)) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002904 nasm_error(ERR_NONFATAL, "`%s': not defining a macro", tline->text);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002905 return DIRECTIVE_FOUND;
2906 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002907 mmhead = (MMacro **) hash_findi_add(&mmacros, defining->name);
2908 defining->next = *mmhead;
2909 *mmhead = defining;
2910 defining = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002911 free_tlist(origline);
2912 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002913
H. Peter Anvin89cee572009-07-15 09:16:54 -04002914 case PP_EXITMACRO:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002915 /*
2916 * We must search along istk->expansion until we hit a
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002917 * macro-end marker for a macro with a name. Then we
2918 * bypass all lines between exitmacro and endmacro.
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002919 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002920 list_for_each(l, istk->expansion)
2921 if (l->finishes && l->finishes->name)
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002922 break;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002923
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002924 if (l) {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002925 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002926 * Remove all conditional entries relative to this
2927 * macro invocation. (safe to do in this context)
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002928 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002929 for ( ; l->finishes->condcnt > 0; l->finishes->condcnt --) {
2930 cond = istk->conds;
2931 istk->conds = cond->next;
2932 nasm_free(cond);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002933 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002934 istk->expansion = l;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002935 } else {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002936 nasm_error(ERR_NONFATAL, "`%%exitmacro' not within `%%macro' block");
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002937 }
Keith Kanios852f1ee2009-07-12 00:19:55 -05002938 free_tlist(origline);
2939 return DIRECTIVE_FOUND;
2940
H. Peter Anvina26433d2008-07-16 14:40:01 -07002941 case PP_UNMACRO:
2942 case PP_UNIMACRO:
2943 {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002944 MMacro **mmac_p;
2945 MMacro spec;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002946
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002947 spec.casesense = (i == PP_UNMACRO);
2948 if (!parse_mmacro_spec(tline, &spec, pp_directives[i])) {
2949 return DIRECTIVE_FOUND;
2950 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002951 mmac_p = (MMacro **) hash_findi(&mmacros, spec.name, NULL);
2952 while (mmac_p && *mmac_p) {
2953 mmac = *mmac_p;
2954 if (mmac->casesense == spec.casesense &&
2955 !mstrcmp(mmac->name, spec.name, spec.casesense) &&
2956 mmac->nparam_min == spec.nparam_min &&
2957 mmac->nparam_max == spec.nparam_max &&
2958 mmac->plus == spec.plus) {
2959 *mmac_p = mmac->next;
2960 free_mmacro(mmac);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002961 } else {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002962 mmac_p = &mmac->next;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002963 }
2964 }
2965 free_tlist(origline);
2966 free_tlist(spec.dlist);
2967 return DIRECTIVE_FOUND;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002968 }
2969
H. Peter Anvine2c80182005-01-15 22:15:51 +00002970 case PP_ROTATE:
2971 if (tline->next && tline->next->type == TOK_WHITESPACE)
2972 tline = tline->next;
H. Peter Anvin89cee572009-07-15 09:16:54 -04002973 if (!tline->next) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002974 free_tlist(origline);
H. Peter Anvin130736c2016-02-17 20:27:41 -08002975 nasm_error(ERR_NONFATAL, "`%%rotate' missing rotate count");
H. Peter Anvine2c80182005-01-15 22:15:51 +00002976 return DIRECTIVE_FOUND;
2977 }
2978 t = expand_smacro(tline->next);
2979 tline->next = NULL;
2980 free_tlist(origline);
2981 tline = t;
2982 tptr = &t;
2983 tokval.t_type = TOKEN_INVALID;
2984 evalresult =
H. Peter Anvin130736c2016-02-17 20:27:41 -08002985 evaluate(ppscan, tptr, &tokval, NULL, pass, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002986 free_tlist(tline);
2987 if (!evalresult)
2988 return DIRECTIVE_FOUND;
2989 if (tokval.t_type)
H. Peter Anvin130736c2016-02-17 20:27:41 -08002990 nasm_error(ERR_WARNING|ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +00002991 "trailing garbage after expression ignored");
2992 if (!is_simple(evalresult)) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002993 nasm_error(ERR_NONFATAL, "non-constant value given to `%%rotate'");
H. Peter Anvine2c80182005-01-15 22:15:51 +00002994 return DIRECTIVE_FOUND;
2995 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002996 mmac = istk->mstk;
2997 while (mmac && !mmac->name) /* avoid mistaking %reps for macros */
2998 mmac = mmac->next_active;
2999 if (!mmac) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08003000 nasm_error(ERR_NONFATAL, "`%%rotate' invoked outside a macro call");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003001 } else if (mmac->nparam == 0) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08003002 nasm_error(ERR_NONFATAL,
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003003 "`%%rotate' invoked within macro without parameters");
3004 } else {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003005 int rotate = mmac->rotate + reloc_value(evalresult);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003006
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003007 rotate %= (int)mmac->nparam;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003008 if (rotate < 0)
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003009 rotate += mmac->nparam;
3010
3011 mmac->rotate = rotate;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003012 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00003013 return DIRECTIVE_FOUND;
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00003014
H. Peter Anvine2c80182005-01-15 22:15:51 +00003015 case PP_REP:
H. Peter Anvin6867acc2007-10-10 14:58:45 -07003016 nolist = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003017 do {
3018 tline = tline->next;
3019 } while (tok_type_(tline, TOK_WHITESPACE));
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00003020
H. Peter Anvine2c80182005-01-15 22:15:51 +00003021 if (tok_type_(tline, TOK_ID) &&
3022 nasm_stricmp(tline->text, ".nolist") == 0) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07003023 nolist = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003024 do {
3025 tline = tline->next;
3026 } while (tok_type_(tline, TOK_WHITESPACE));
3027 }
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00003028
H. Peter Anvine2c80182005-01-15 22:15:51 +00003029 if (tline) {
3030 t = expand_smacro(tline);
3031 tptr = &t;
3032 tokval.t_type = TOKEN_INVALID;
3033 evalresult =
H. Peter Anvin130736c2016-02-17 20:27:41 -08003034 evaluate(ppscan, tptr, &tokval, NULL, pass, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003035 if (!evalresult) {
3036 free_tlist(origline);
3037 return DIRECTIVE_FOUND;
3038 }
3039 if (tokval.t_type)
H. Peter Anvin130736c2016-02-17 20:27:41 -08003040 nasm_error(ERR_WARNING|ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +00003041 "trailing garbage after expression ignored");
3042 if (!is_simple(evalresult)) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08003043 nasm_error(ERR_NONFATAL, "non-constant value given to `%%rep'");
H. Peter Anvine2c80182005-01-15 22:15:51 +00003044 return DIRECTIVE_FOUND;
3045 }
Cyrill Gorcunove091d6e2010-08-09 13:58:22 +04003046 count = reloc_value(evalresult);
3047 if (count >= REP_LIMIT) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08003048 nasm_error(ERR_NONFATAL, "`%%rep' value exceeds limit");
Cyrill Gorcunove091d6e2010-08-09 13:58:22 +04003049 count = 0;
3050 } else
3051 count++;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003052 } else {
H. Peter Anvin130736c2016-02-17 20:27:41 -08003053 nasm_error(ERR_NONFATAL, "`%%rep' expects a repeat count");
H. Peter Anvinf8ba53e2007-10-11 10:11:57 -07003054 count = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003055 }
3056 free_tlist(origline);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003057
3058 tmp_defining = defining;
3059 defining = nasm_malloc(sizeof(MMacro));
3060 defining->prev = NULL;
3061 defining->name = NULL; /* flags this macro as a %rep block */
3062 defining->casesense = false;
3063 defining->plus = false;
3064 defining->nolist = nolist;
3065 defining->in_progress = count;
3066 defining->max_depth = 0;
3067 defining->nparam_min = defining->nparam_max = 0;
3068 defining->defaults = NULL;
3069 defining->dlist = NULL;
3070 defining->expansion = NULL;
3071 defining->next_active = istk->mstk;
3072 defining->rep_nest = tmp_defining;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003073 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003074
H. Peter Anvine2c80182005-01-15 22:15:51 +00003075 case PP_ENDREP:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003076 if (!defining || defining->name) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08003077 nasm_error(ERR_NONFATAL, "`%%endrep': no matching `%%rep'");
H. Peter Anvine2c80182005-01-15 22:15:51 +00003078 return DIRECTIVE_FOUND;
3079 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003080
H. Peter Anvine2c80182005-01-15 22:15:51 +00003081 /*
3082 * Now we have a "macro" defined - although it has no name
3083 * and we won't be entering it in the hash tables - we must
3084 * push a macro-end marker for it on to istk->expansion.
3085 * After that, it will take care of propagating itself (a
3086 * macro-end marker line for a macro which is really a %rep
3087 * block will cause the macro to be re-expanded, complete
3088 * with another macro-end marker to ensure the process
3089 * continues) until the whole expansion is forcibly removed
3090 * from istk->expansion by a %exitrep.
3091 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003092 l = nasm_malloc(sizeof(Line));
3093 l->next = istk->expansion;
3094 l->finishes = defining;
3095 l->first = NULL;
3096 istk->expansion = l;
3097
3098 istk->mstk = defining;
3099
H. Peter Anvin8ac25aa2016-02-18 01:16:18 -08003100 lfmt->uplevel(defining->nolist ? LIST_MACRO_NOLIST : LIST_MACRO);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003101 tmp_defining = defining;
3102 defining = defining->rep_nest;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003103 free_tlist(origline);
3104 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003105
H. Peter Anvine2c80182005-01-15 22:15:51 +00003106 case PP_EXITREP:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003107 /*
3108 * We must search along istk->expansion until we hit a
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003109 * macro-end marker for a macro with no name. Then we set
3110 * its `in_progress' flag to 0.
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003111 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003112 list_for_each(l, istk->expansion)
3113 if (l->finishes && !l->finishes->name)
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003114 break;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003115
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003116 if (l)
3117 l->finishes->in_progress = 1;
3118 else
H. Peter Anvin130736c2016-02-17 20:27:41 -08003119 nasm_error(ERR_NONFATAL, "`%%exitrep' not within `%%rep' block");
H. Peter Anvine2c80182005-01-15 22:15:51 +00003120 free_tlist(origline);
3121 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003122
H. Peter Anvine2c80182005-01-15 22:15:51 +00003123 case PP_XDEFINE:
3124 case PP_IXDEFINE:
3125 case PP_DEFINE:
3126 case PP_IDEFINE:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003127 casesense = (i == PP_DEFINE || i == PP_XDEFINE);
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07003128
H. Peter Anvine2c80182005-01-15 22:15:51 +00003129 tline = tline->next;
3130 skip_white_(tline);
3131 tline = expand_id(tline);
3132 if (!tline || (tline->type != TOK_ID &&
3133 (tline->type != TOK_PREPROC_ID ||
3134 tline->text[1] != '$'))) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08003135 nasm_error(ERR_NONFATAL, "`%s' expects a macro identifier",
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003136 pp_directives[i]);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003137 free_tlist(origline);
3138 return DIRECTIVE_FOUND;
3139 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003140
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04003141 ctx = get_ctx(tline->text, &mname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003142 last = tline;
3143 param_start = tline = tline->next;
3144 nparam = 0;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003145
H. Peter Anvine2c80182005-01-15 22:15:51 +00003146 /* Expand the macro definition now for %xdefine and %ixdefine */
3147 if ((i == PP_XDEFINE) || (i == PP_IXDEFINE))
3148 tline = expand_smacro(tline);
H. Peter Anvin734b1882002-04-30 21:01:08 +00003149
H. Peter Anvine2c80182005-01-15 22:15:51 +00003150 if (tok_is_(tline, "(")) {
3151 /*
3152 * This macro has parameters.
3153 */
H. Peter Anvin734b1882002-04-30 21:01:08 +00003154
H. Peter Anvine2c80182005-01-15 22:15:51 +00003155 tline = tline->next;
3156 while (1) {
3157 skip_white_(tline);
3158 if (!tline) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08003159 nasm_error(ERR_NONFATAL, "parameter identifier expected");
H. Peter Anvine2c80182005-01-15 22:15:51 +00003160 free_tlist(origline);
3161 return DIRECTIVE_FOUND;
3162 }
3163 if (tline->type != TOK_ID) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08003164 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00003165 "`%s': parameter identifier expected",
3166 tline->text);
3167 free_tlist(origline);
3168 return DIRECTIVE_FOUND;
3169 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003170 tline->type = TOK_SMAC_PARAM + nparam++;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003171 tline = tline->next;
3172 skip_white_(tline);
3173 if (tok_is_(tline, ",")) {
3174 tline = tline->next;
H. Peter Anvinca348b62008-07-23 10:49:26 -04003175 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003176 if (!tok_is_(tline, ")")) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08003177 nasm_error(ERR_NONFATAL,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003178 "`)' expected to terminate macro template");
3179 free_tlist(origline);
3180 return DIRECTIVE_FOUND;
3181 }
3182 break;
3183 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00003184 }
3185 last = tline;
3186 tline = tline->next;
3187 }
3188 if (tok_type_(tline, TOK_WHITESPACE))
3189 last = tline, tline = tline->next;
3190 macro_start = NULL;
3191 last->next = NULL;
3192 t = tline;
3193 while (t) {
3194 if (t->type == TOK_ID) {
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04003195 list_for_each(tt, param_start)
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003196 if (tt->type >= TOK_SMAC_PARAM &&
H. Peter Anvine2c80182005-01-15 22:15:51 +00003197 !strcmp(tt->text, t->text))
3198 t->type = tt->type;
3199 }
3200 tt = t->next;
3201 t->next = macro_start;
3202 macro_start = t;
3203 t = tt;
3204 }
3205 /*
3206 * Good. We now have a macro name, a parameter count, and a
3207 * token list (in reverse order) for an expansion. We ought
3208 * to be OK just to create an SMacro, store it, and let
3209 * free_tlist have the rest of the line (which we have
3210 * carefully re-terminated after chopping off the expansion
3211 * from the end).
3212 */
H. Peter Anvin4db5a162007-10-11 13:42:09 -07003213 define_smacro(ctx, mname, casesense, nparam, macro_start);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003214 free_tlist(origline);
3215 return DIRECTIVE_FOUND;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003216
H. Peter Anvine2c80182005-01-15 22:15:51 +00003217 case PP_UNDEF:
3218 tline = tline->next;
3219 skip_white_(tline);
3220 tline = expand_id(tline);
3221 if (!tline || (tline->type != TOK_ID &&
3222 (tline->type != TOK_PREPROC_ID ||
3223 tline->text[1] != '$'))) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08003224 nasm_error(ERR_NONFATAL, "`%%undef' expects a macro identifier");
H. Peter Anvine2c80182005-01-15 22:15:51 +00003225 free_tlist(origline);
3226 return DIRECTIVE_FOUND;
3227 }
3228 if (tline->next) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08003229 nasm_error(ERR_WARNING|ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +00003230 "trailing garbage after macro name ignored");
3231 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003232
H. Peter Anvine2c80182005-01-15 22:15:51 +00003233 /* Find the context that symbol belongs to */
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04003234 ctx = get_ctx(tline->text, &mname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003235 undef_smacro(ctx, mname);
3236 free_tlist(origline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003237 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003238
H. Peter Anvin9e200162008-06-04 17:23:14 -07003239 case PP_DEFSTR:
3240 case PP_IDEFSTR:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003241 casesense = (i == PP_DEFSTR);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003242
3243 tline = tline->next;
3244 skip_white_(tline);
3245 tline = expand_id(tline);
3246 if (!tline || (tline->type != TOK_ID &&
3247 (tline->type != TOK_PREPROC_ID ||
3248 tline->text[1] != '$'))) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08003249 nasm_error(ERR_NONFATAL, "`%s' expects a macro identifier",
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003250 pp_directives[i]);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003251 free_tlist(origline);
3252 return DIRECTIVE_FOUND;
3253 }
3254
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04003255 ctx = get_ctx(tline->text, &mname);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003256 last = tline;
3257 tline = expand_smacro(tline->next);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003258 last->next = NULL;
H. Peter Anvin9e200162008-06-04 17:23:14 -07003259
3260 while (tok_type_(tline, TOK_WHITESPACE))
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003261 tline = delete_Token(tline);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003262
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003263 p = detoken(tline, false);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003264 macro_start = nasm_malloc(sizeof(*macro_start));
3265 macro_start->next = NULL;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003266 macro_start->text = nasm_quote(p, strlen(p));
3267 macro_start->type = TOK_STRING;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003268 macro_start->a.mac = NULL;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003269 nasm_free(p);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003270
3271 /*
3272 * We now have a macro name, an implicit parameter count of
3273 * zero, and a string token to use as an expansion. Create
3274 * and store an SMacro.
3275 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003276 define_smacro(ctx, mname, casesense, 0, macro_start);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003277 free_tlist(origline);
3278 return DIRECTIVE_FOUND;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003279
H. Peter Anvin2f55bda2009-07-14 15:04:04 -04003280 case PP_DEFTOK:
3281 case PP_IDEFTOK:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003282 casesense = (i == PP_DEFTOK);
3283
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003284 tline = tline->next;
3285 skip_white_(tline);
3286 tline = expand_id(tline);
3287 if (!tline || (tline->type != TOK_ID &&
3288 (tline->type != TOK_PREPROC_ID ||
3289 tline->text[1] != '$'))) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08003290 nasm_error(ERR_NONFATAL,
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003291 "`%s' expects a macro identifier as first parameter",
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003292 pp_directives[i]);
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003293 free_tlist(origline);
3294 return DIRECTIVE_FOUND;
3295 }
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04003296 ctx = get_ctx(tline->text, &mname);
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003297 last = tline;
3298 tline = expand_smacro(tline->next);
3299 last->next = NULL;
3300
3301 t = tline;
3302 while (tok_type_(t, TOK_WHITESPACE))
3303 t = t->next;
3304 /* t should now point to the string */
Cyrill Gorcunov6908e582010-09-06 19:36:15 +04003305 if (!tok_type_(t, TOK_STRING)) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08003306 nasm_error(ERR_NONFATAL,
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003307 "`%s` requires string as second parameter",
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003308 pp_directives[i]);
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003309 free_tlist(tline);
3310 free_tlist(origline);
3311 return DIRECTIVE_FOUND;
3312 }
3313
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04003314 /*
3315 * Convert the string to a token stream. Note that smacros
3316 * are stored with the token stream reversed, so we have to
3317 * reverse the output of tokenize().
3318 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003319 nasm_unquote_cstr(t->text, i);
H. Peter Anvinb40992c2010-09-15 08:57:21 -07003320 macro_start = reverse_tokens(tokenize(t->text));
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003321
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003322 /*
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);
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003328 free_tlist(tline);
3329 free_tlist(origline);
3330 return DIRECTIVE_FOUND;
H. Peter Anvin9e200162008-06-04 17:23:14 -07003331
H. Peter Anvin418ca702008-05-30 10:42:30 -07003332 case PP_PATHSEARCH:
3333 {
H. Peter Anvinccad6f92016-10-04 00:34:35 -07003334 const char *found_path;
H. Peter Anvin418ca702008-05-30 10:42:30 -07003335
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003336 casesense = true;
H. Peter Anvin418ca702008-05-30 10:42:30 -07003337
3338 tline = tline->next;
3339 skip_white_(tline);
3340 tline = expand_id(tline);
3341 if (!tline || (tline->type != TOK_ID &&
3342 (tline->type != TOK_PREPROC_ID ||
3343 tline->text[1] != '$'))) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08003344 nasm_error(ERR_NONFATAL,
H. Peter Anvin418ca702008-05-30 10:42:30 -07003345 "`%%pathsearch' expects a macro identifier as first parameter");
3346 free_tlist(origline);
3347 return DIRECTIVE_FOUND;
3348 }
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04003349 ctx = get_ctx(tline->text, &mname);
H. Peter Anvin418ca702008-05-30 10:42:30 -07003350 last = tline;
3351 tline = expand_smacro(tline->next);
3352 last->next = NULL;
3353
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003354 t = tline;
H. Peter Anvin418ca702008-05-30 10:42:30 -07003355 while (tok_type_(t, TOK_WHITESPACE))
3356 t = t->next;
3357
3358 if (!t || (t->type != TOK_STRING &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003359 t->type != TOK_INTERNAL_STRING)) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08003360 nasm_error(ERR_NONFATAL, "`%%pathsearch' expects a file name");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003361 free_tlist(tline);
H. Peter Anvin418ca702008-05-30 10:42:30 -07003362 free_tlist(origline);
3363 return DIRECTIVE_FOUND; /* but we did _something_ */
3364 }
3365 if (t->next)
H. Peter Anvin130736c2016-02-17 20:27:41 -08003366 nasm_error(ERR_WARNING|ERR_PASS1,
H. Peter Anvin418ca702008-05-30 10:42:30 -07003367 "trailing garbage after `%%pathsearch' ignored");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003368 p = t->text;
H. Peter Anvin427cc912008-06-01 21:43:03 -07003369 if (t->type != TOK_INTERNAL_STRING)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003370 nasm_unquote(p, NULL);
H. Peter Anvin418ca702008-05-30 10:42:30 -07003371
H. Peter Anvin9924d1e2016-10-04 00:59:39 -07003372 inc_fopen(p, NULL, &found_path, INC_PROBE, NF_BINARY);
H. Peter Anvinccad6f92016-10-04 00:34:35 -07003373 if (!found_path)
3374 found_path = p;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003375 macro_start = nasm_malloc(sizeof(*macro_start));
3376 macro_start->next = NULL;
H. Peter Anvinccad6f92016-10-04 00:34:35 -07003377 macro_start->text = nasm_quote(found_path, strlen(found_path));
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003378 macro_start->type = TOK_STRING;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003379 macro_start->a.mac = NULL;
H. Peter Anvin418ca702008-05-30 10:42:30 -07003380
3381 /*
3382 * We now have a macro name, an implicit parameter count of
3383 * zero, and a string token to use as an expansion. Create
3384 * and store an SMacro.
3385 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003386 define_smacro(ctx, mname, casesense, 0, macro_start);
3387 free_tlist(tline);
H. Peter Anvin418ca702008-05-30 10:42:30 -07003388 free_tlist(origline);
3389 return DIRECTIVE_FOUND;
3390 }
3391
H. Peter Anvine2c80182005-01-15 22:15:51 +00003392 case PP_STRLEN:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003393 casesense = true;
H. Peter Anvin70653092007-10-19 14:42:29 -07003394
H. Peter Anvine2c80182005-01-15 22:15:51 +00003395 tline = tline->next;
3396 skip_white_(tline);
3397 tline = expand_id(tline);
3398 if (!tline || (tline->type != TOK_ID &&
3399 (tline->type != TOK_PREPROC_ID ||
3400 tline->text[1] != '$'))) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08003401 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00003402 "`%%strlen' expects a macro identifier as first parameter");
3403 free_tlist(origline);
3404 return DIRECTIVE_FOUND;
3405 }
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04003406 ctx = get_ctx(tline->text, &mname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003407 last = tline;
3408 tline = expand_smacro(tline->next);
3409 last->next = NULL;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003410
H. Peter Anvine2c80182005-01-15 22:15:51 +00003411 t = tline;
3412 while (tok_type_(t, TOK_WHITESPACE))
3413 t = t->next;
3414 /* t should now point to the string */
Cyrill Gorcunov4e1d5ab2010-07-23 18:51:51 +04003415 if (!tok_type_(t, TOK_STRING)) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08003416 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00003417 "`%%strlen` requires string as second parameter");
3418 free_tlist(tline);
3419 free_tlist(origline);
3420 return DIRECTIVE_FOUND;
3421 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003422
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003423 macro_start = nasm_malloc(sizeof(*macro_start));
3424 macro_start->next = NULL;
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07003425 make_tok_num(macro_start, nasm_unquote(t->text, NULL));
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003426 macro_start->a.mac = NULL;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003427
H. Peter Anvine2c80182005-01-15 22:15:51 +00003428 /*
3429 * We now have a macro name, an implicit parameter count of
3430 * zero, and a numeric token to use as an expansion. Create
3431 * and store an SMacro.
3432 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003433 define_smacro(ctx, mname, casesense, 0, macro_start);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003434 free_tlist(tline);
3435 free_tlist(origline);
3436 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003437
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003438 case PP_STRCAT:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003439 casesense = true;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003440
3441 tline = tline->next;
3442 skip_white_(tline);
3443 tline = expand_id(tline);
3444 if (!tline || (tline->type != TOK_ID &&
3445 (tline->type != TOK_PREPROC_ID ||
3446 tline->text[1] != '$'))) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08003447 nasm_error(ERR_NONFATAL,
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003448 "`%%strcat' expects a macro identifier as first parameter");
3449 free_tlist(origline);
3450 return DIRECTIVE_FOUND;
3451 }
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04003452 ctx = get_ctx(tline->text, &mname);
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003453 last = tline;
3454 tline = expand_smacro(tline->next);
3455 last->next = NULL;
3456
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003457 len = 0;
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04003458 list_for_each(t, tline) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003459 switch (t->type) {
3460 case TOK_WHITESPACE:
3461 break;
3462 case TOK_STRING:
3463 len += t->a.len = nasm_unquote(t->text, NULL);
3464 break;
3465 case TOK_OTHER:
3466 if (!strcmp(t->text, ",")) /* permit comma separators */
3467 break;
3468 /* else fall through */
3469 default:
H. Peter Anvin130736c2016-02-17 20:27:41 -08003470 nasm_error(ERR_NONFATAL,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003471 "non-string passed to `%%strcat' (%d)", t->type);
3472 free_tlist(tline);
3473 free_tlist(origline);
3474 return DIRECTIVE_FOUND;
3475 }
3476 }
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003477
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003478 p = pp = nasm_malloc(len);
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04003479 list_for_each(t, tline) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003480 if (t->type == TOK_STRING) {
3481 memcpy(p, t->text, t->a.len);
3482 p += t->a.len;
3483 }
3484 }
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003485
3486 /*
3487 * We now have a macro name, an implicit parameter count of
3488 * zero, and a numeric token to use as an expansion. Create
3489 * and store an SMacro.
3490 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003491 macro_start = new_Token(NULL, TOK_STRING, NULL, 0);
3492 macro_start->text = nasm_quote(pp, len);
3493 nasm_free(pp);
3494 define_smacro(ctx, mname, casesense, 0, macro_start);
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003495 free_tlist(tline);
3496 free_tlist(origline);
3497 return DIRECTIVE_FOUND;
3498
H. Peter Anvine2c80182005-01-15 22:15:51 +00003499 case PP_SUBSTR:
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003500 {
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003501 int64_t start, count;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003502 size_t len;
H. Peter Anvind2456592008-06-19 15:04:18 -07003503
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003504 casesense = true;
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07003505
H. Peter Anvine2c80182005-01-15 22:15:51 +00003506 tline = tline->next;
3507 skip_white_(tline);
3508 tline = expand_id(tline);
3509 if (!tline || (tline->type != TOK_ID &&
3510 (tline->type != TOK_PREPROC_ID ||
3511 tline->text[1] != '$'))) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08003512 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00003513 "`%%substr' expects a macro identifier as first parameter");
3514 free_tlist(origline);
3515 return DIRECTIVE_FOUND;
3516 }
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04003517 ctx = get_ctx(tline->text, &mname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003518 last = tline;
3519 tline = expand_smacro(tline->next);
3520 last->next = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003521
Cyrill Gorcunov35519d62010-09-06 23:49:52 +04003522 if (tline) /* skip expanded id */
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003523 t = tline->next;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003524 while (tok_type_(t, TOK_WHITESPACE))
3525 t = t->next;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003526
H. Peter Anvine2c80182005-01-15 22:15:51 +00003527 /* t should now point to the string */
Cyrill Gorcunov35519d62010-09-06 23:49:52 +04003528 if (!tok_type_(t, TOK_STRING)) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08003529 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00003530 "`%%substr` requires string as second parameter");
3531 free_tlist(tline);
3532 free_tlist(origline);
3533 return DIRECTIVE_FOUND;
3534 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003535
H. Peter Anvine2c80182005-01-15 22:15:51 +00003536 tt = t->next;
3537 tptr = &tt;
3538 tokval.t_type = TOKEN_INVALID;
H. Peter Anvin130736c2016-02-17 20:27:41 -08003539 evalresult = evaluate(ppscan, tptr, &tokval, NULL, pass, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003540 if (!evalresult) {
3541 free_tlist(tline);
3542 free_tlist(origline);
3543 return DIRECTIVE_FOUND;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003544 } else if (!is_simple(evalresult)) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08003545 nasm_error(ERR_NONFATAL, "non-constant value given to `%%substr`");
H. Peter Anvine2c80182005-01-15 22:15:51 +00003546 free_tlist(tline);
3547 free_tlist(origline);
3548 return DIRECTIVE_FOUND;
3549 }
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003550 start = evalresult->value - 1;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003551
3552 while (tok_type_(tt, TOK_WHITESPACE))
3553 tt = tt->next;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003554 if (!tt) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003555 count = 1; /* Backwards compatibility: one character */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003556 } else {
3557 tokval.t_type = TOKEN_INVALID;
H. Peter Anvin130736c2016-02-17 20:27:41 -08003558 evalresult = evaluate(ppscan, tptr, &tokval, NULL, pass, NULL);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003559 if (!evalresult) {
3560 free_tlist(tline);
3561 free_tlist(origline);
3562 return DIRECTIVE_FOUND;
3563 } else if (!is_simple(evalresult)) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08003564 nasm_error(ERR_NONFATAL, "non-constant value given to `%%substr`");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003565 free_tlist(tline);
3566 free_tlist(origline);
3567 return DIRECTIVE_FOUND;
3568 }
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003569 count = evalresult->value;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003570 }
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003571
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003572 len = nasm_unquote(t->text, NULL);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003573
Cyrill Gorcunovcff031e2010-09-07 20:31:11 +04003574 /* make start and count being in range */
3575 if (start < 0)
3576 start = 0;
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003577 if (count < 0)
3578 count = len + count + 1 - start;
3579 if (start + count > (int64_t)len)
Cyrill Gorcunovcff031e2010-09-07 20:31:11 +04003580 count = len - start;
3581 if (!len || count < 0 || start >=(int64_t)len)
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003582 start = -1, count = 0; /* empty string */
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003583
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003584 macro_start = nasm_malloc(sizeof(*macro_start));
3585 macro_start->next = NULL;
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003586 macro_start->text = nasm_quote((start < 0) ? "" : t->text + start, count);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003587 macro_start->type = TOK_STRING;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003588 macro_start->a.mac = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003589
H. Peter Anvine2c80182005-01-15 22:15:51 +00003590 /*
3591 * We now have a macro name, an implicit parameter count of
3592 * zero, and a numeric token to use as an expansion. Create
3593 * and store an SMacro.
3594 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003595 define_smacro(ctx, mname, casesense, 0, macro_start);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003596 free_tlist(tline);
3597 free_tlist(origline);
3598 return DIRECTIVE_FOUND;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003599 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003600
H. Peter Anvine2c80182005-01-15 22:15:51 +00003601 case PP_ASSIGN:
3602 case PP_IASSIGN:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003603 casesense = (i == PP_ASSIGN);
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07003604
H. Peter Anvine2c80182005-01-15 22:15:51 +00003605 tline = tline->next;
3606 skip_white_(tline);
3607 tline = expand_id(tline);
3608 if (!tline || (tline->type != TOK_ID &&
3609 (tline->type != TOK_PREPROC_ID ||
3610 tline->text[1] != '$'))) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08003611 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00003612 "`%%%sassign' expects a macro identifier",
3613 (i == PP_IASSIGN ? "i" : ""));
3614 free_tlist(origline);
3615 return DIRECTIVE_FOUND;
3616 }
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04003617 ctx = get_ctx(tline->text, &mname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003618 last = tline;
3619 tline = expand_smacro(tline->next);
3620 last->next = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003621
H. Peter Anvine2c80182005-01-15 22:15:51 +00003622 t = tline;
3623 tptr = &t;
3624 tokval.t_type = TOKEN_INVALID;
H. Peter Anvin130736c2016-02-17 20:27:41 -08003625 evalresult = evaluate(ppscan, tptr, &tokval, NULL, pass, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003626 free_tlist(tline);
3627 if (!evalresult) {
3628 free_tlist(origline);
3629 return DIRECTIVE_FOUND;
3630 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003631
H. Peter Anvine2c80182005-01-15 22:15:51 +00003632 if (tokval.t_type)
H. Peter Anvin130736c2016-02-17 20:27:41 -08003633 nasm_error(ERR_WARNING|ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +00003634 "trailing garbage after expression ignored");
H. Peter Anvin734b1882002-04-30 21:01:08 +00003635
H. Peter Anvine2c80182005-01-15 22:15:51 +00003636 if (!is_simple(evalresult)) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08003637 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00003638 "non-constant value given to `%%%sassign'",
3639 (i == PP_IASSIGN ? "i" : ""));
3640 free_tlist(origline);
3641 return DIRECTIVE_FOUND;
3642 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003643
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003644 macro_start = nasm_malloc(sizeof(*macro_start));
3645 macro_start->next = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003646 make_tok_num(macro_start, reloc_value(evalresult));
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003647 macro_start->a.mac = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003648
H. Peter Anvine2c80182005-01-15 22:15:51 +00003649 /*
3650 * We now have a macro name, an implicit parameter count of
3651 * zero, and a numeric token to use as an expansion. Create
3652 * and store an SMacro.
3653 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003654 define_smacro(ctx, mname, casesense, 0, macro_start);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003655 free_tlist(origline);
3656 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003657
H. Peter Anvine2c80182005-01-15 22:15:51 +00003658 case PP_LINE:
3659 /*
3660 * Syntax is `%line nnn[+mmm] [filename]'
3661 */
3662 tline = tline->next;
3663 skip_white_(tline);
3664 if (!tok_type_(tline, TOK_NUMBER)) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08003665 nasm_error(ERR_NONFATAL, "`%%line' expects line number");
H. Peter Anvine2c80182005-01-15 22:15:51 +00003666 free_tlist(origline);
3667 return DIRECTIVE_FOUND;
3668 }
H. Peter Anvin70055962007-10-11 00:05:31 -07003669 k = readnum(tline->text, &err);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003670 m = 1;
3671 tline = tline->next;
3672 if (tok_is_(tline, "+")) {
3673 tline = tline->next;
3674 if (!tok_type_(tline, TOK_NUMBER)) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08003675 nasm_error(ERR_NONFATAL, "`%%line' expects line increment");
H. Peter Anvine2c80182005-01-15 22:15:51 +00003676 free_tlist(origline);
3677 return DIRECTIVE_FOUND;
3678 }
H. Peter Anvin70055962007-10-11 00:05:31 -07003679 m = readnum(tline->text, &err);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003680 tline = tline->next;
3681 }
3682 skip_white_(tline);
3683 src_set_linnum(k);
3684 istk->lineinc = m;
3685 if (tline) {
H. Peter Anvin274cda82016-05-10 02:56:29 -07003686 char *fname = detoken(tline, false);
3687 src_set_fname(fname);
3688 nasm_free(fname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003689 }
3690 free_tlist(origline);
3691 return DIRECTIVE_FOUND;
Keith Kaniosb307a4f2010-11-06 17:41:51 -05003692
H. Peter Anvine2c80182005-01-15 22:15:51 +00003693 default:
H. Peter Anvin130736c2016-02-17 20:27:41 -08003694 nasm_error(ERR_FATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00003695 "preprocessor directive `%s' not yet implemented",
H. Peter Anvin4169a472007-09-12 01:29:43 +00003696 pp_directives[i]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003697 return DIRECTIVE_FOUND;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003698 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003699}
3700
3701/*
H. Peter Anvin76690a12002-04-30 20:52:49 +00003702 * Ensure that a macro parameter contains a condition code and
3703 * nothing else. Return the condition code index if so, or -1
3704 * otherwise.
3705 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003706static int find_cc(Token * t)
H. Peter Anvineba20a72002-04-30 20:53:55 +00003707{
H. Peter Anvin76690a12002-04-30 20:52:49 +00003708 Token *tt;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003709
H. Peter Anvin25a99342007-09-22 17:45:45 -07003710 if (!t)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003711 return -1; /* Probably a %+ without a space */
H. Peter Anvin25a99342007-09-22 17:45:45 -07003712
H. Peter Anvineba20a72002-04-30 20:53:55 +00003713 skip_white_(t);
H. Peter Anvin76690a12002-04-30 20:52:49 +00003714 if (t->type != TOK_ID)
H. Peter Anvine2c80182005-01-15 22:15:51 +00003715 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003716 tt = t->next;
H. Peter Anvineba20a72002-04-30 20:53:55 +00003717 skip_white_(tt);
H. Peter Anvin76690a12002-04-30 20:52:49 +00003718 if (tt && (tt->type != TOK_OTHER || strcmp(tt->text, ",")))
H. Peter Anvine2c80182005-01-15 22:15:51 +00003719 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003720
Cyrill Gorcunov19456392012-05-02 00:18:56 +04003721 return bsii(t->text, (const char **)conditions, ARRAY_SIZE(conditions));
H. Peter Anvin76690a12002-04-30 20:52:49 +00003722}
3723
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003724/*
3725 * This routines walks over tokens strem and hadnles tokens
3726 * pasting, if @handle_explicit passed then explicit pasting
3727 * term is handled, otherwise -- implicit pastings only.
3728 */
Cyrill Gorcunov575d4282010-10-06 00:25:55 +04003729static bool paste_tokens(Token **head, const struct tokseq_match *m,
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003730 size_t mnum, bool handle_explicit)
H. Peter Anvind784a082009-04-20 14:01:18 -07003731{
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003732 Token *tok, *next, **prev_next, **prev_nonspace;
3733 bool pasted = false;
3734 char *buf, *p;
3735 size_t len, i;
H. Peter Anvind784a082009-04-20 14:01:18 -07003736
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003737 /*
3738 * The last token before pasting. We need it
3739 * to be able to connect new handled tokens.
3740 * In other words if there were a tokens stream
3741 *
3742 * A -> B -> C -> D
3743 *
3744 * and we've joined tokens B and C, the resulting
3745 * stream should be
3746 *
3747 * A -> BC -> D
3748 */
3749 tok = *head;
3750 prev_next = NULL;
3751
3752 if (!tok_type_(tok, TOK_WHITESPACE) && !tok_type_(tok, TOK_PASTE))
3753 prev_nonspace = head;
3754 else
3755 prev_nonspace = NULL;
3756
3757 while (tok && (next = tok->next)) {
3758
3759 switch (tok->type) {
H. Peter Anvind784a082009-04-20 14:01:18 -07003760 case TOK_WHITESPACE:
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003761 /* Zap redundant whitespaces */
3762 while (tok_type_(next, TOK_WHITESPACE))
3763 next = delete_Token(next);
3764 tok->next = next;
H. Peter Anvind784a082009-04-20 14:01:18 -07003765 break;
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003766
3767 case TOK_PASTE:
3768 /* Explicit pasting */
3769 if (!handle_explicit)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003770 break;
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003771 next = delete_Token(tok);
3772
3773 while (tok_type_(next, TOK_WHITESPACE))
3774 next = delete_Token(next);
3775
3776 if (!pasted)
3777 pasted = true;
3778
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003779 /* Left pasting token is start of line */
3780 if (!prev_nonspace)
H. Peter Anvin130736c2016-02-17 20:27:41 -08003781 nasm_error(ERR_FATAL, "No lvalue found on pasting");
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003782
Cyrill Gorcunov8b5c9fb2013-02-04 01:24:54 +04003783 /*
3784 * No ending token, this might happen in two
3785 * cases
3786 *
3787 * 1) There indeed no right token at all
3788 * 2) There is a bare "%define ID" statement,
3789 * and @ID does expand to whitespace.
3790 *
3791 * So technically we need to do a grammar analysis
3792 * in another stage of parsing, but for now lets don't
3793 * change the behaviour people used to. Simply allow
3794 * whitespace after paste token.
3795 */
3796 if (!next) {
3797 /*
3798 * Zap ending space tokens and that's all.
3799 */
3800 tok = (*prev_nonspace)->next;
3801 while (tok_type_(tok, TOK_WHITESPACE))
3802 tok = delete_Token(tok);
3803 tok = *prev_nonspace;
3804 tok->next = NULL;
3805 break;
3806 }
3807
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003808 tok = *prev_nonspace;
3809 while (tok_type_(tok, TOK_WHITESPACE))
3810 tok = delete_Token(tok);
3811 len = strlen(tok->text);
3812 len += strlen(next->text);
3813
3814 p = buf = nasm_malloc(len + 1);
3815 strcpy(p, tok->text);
3816 p = strchr(p, '\0');
3817 strcpy(p, next->text);
3818
3819 delete_Token(tok);
3820
3821 tok = tokenize(buf);
3822 nasm_free(buf);
3823
3824 *prev_nonspace = tok;
3825 while (tok && tok->next)
3826 tok = tok->next;
3827
3828 tok->next = delete_Token(next);
3829
3830 /* Restart from pasted tokens head */
3831 tok = *prev_nonspace;
3832 break;
3833
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003834 default:
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003835 /* implicit pasting */
Cyrill Gorcunov575d4282010-10-06 00:25:55 +04003836 for (i = 0; i < mnum; i++) {
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003837 if (!(PP_CONCAT_MATCH(tok, m[i].mask_head)))
3838 continue;
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +04003839
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003840 len = 0;
3841 while (next && PP_CONCAT_MATCH(next, m[i].mask_tail)) {
3842 len += strlen(next->text);
3843 next = next->next;
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +04003844 }
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003845
3846 /* No match */
3847 if (tok == next)
3848 break;
3849
3850 len += strlen(tok->text);
3851 p = buf = nasm_malloc(len + 1);
3852
Adam Majer1a069432017-07-25 11:12:35 +02003853 strcpy(p, tok->text);
3854 p = strchr(p, '\0');
3855 tok = delete_Token(tok);
3856
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003857 while (tok != next) {
Adam Majer1a069432017-07-25 11:12:35 +02003858 if (PP_CONCAT_MATCH(tok, m[i].mask_tail)) {
3859 strcpy(p, tok->text);
3860 p = strchr(p, '\0');
3861 }
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003862 tok = delete_Token(tok);
3863 }
3864
3865 tok = tokenize(buf);
3866 nasm_free(buf);
3867
3868 if (prev_next)
3869 *prev_next = tok;
3870 else
3871 *head = tok;
3872
3873 /*
3874 * Connect pasted into original stream,
3875 * ie A -> new-tokens -> B
3876 */
3877 while (tok && tok->next)
3878 tok = tok->next;
3879 tok->next = next;
3880
3881 if (!pasted)
3882 pasted = true;
3883
3884 /* Restart from pasted tokens head */
3885 tok = prev_next ? *prev_next : *head;
Cyrill Gorcunov575d4282010-10-06 00:25:55 +04003886 }
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003887
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003888 break;
H. Peter Anvind784a082009-04-20 14:01:18 -07003889 }
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003890
3891 prev_next = &tok->next;
3892
3893 if (tok->next &&
3894 !tok_type_(tok->next, TOK_WHITESPACE) &&
3895 !tok_type_(tok->next, TOK_PASTE))
3896 prev_nonspace = prev_next;
3897
3898 tok = tok->next;
H. Peter Anvind784a082009-04-20 14:01:18 -07003899 }
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003900
3901 return pasted;
H. Peter Anvind784a082009-04-20 14:01:18 -07003902}
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003903
3904/*
3905 * expands to a list of tokens from %{x:y}
3906 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003907static Token *expand_mmac_params_range(MMacro *mac, Token *tline, Token ***last)
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003908{
3909 Token *t = tline, **tt, *tm, *head;
3910 char *pos;
3911 int fst, lst, j, i;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003912
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003913 pos = strchr(tline->text, ':');
3914 nasm_assert(pos);
3915
3916 lst = atoi(pos + 1);
3917 fst = atoi(tline->text + 1);
3918
3919 /*
3920 * only macros params are accounted so
3921 * if someone passes %0 -- we reject such
3922 * value(s)
3923 */
3924 if (lst == 0 || fst == 0)
3925 goto err;
3926
3927 /* the values should be sane */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003928 if ((fst > (int)mac->nparam || fst < (-(int)mac->nparam)) ||
3929 (lst > (int)mac->nparam || lst < (-(int)mac->nparam)))
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003930 goto err;
3931
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003932 fst = fst < 0 ? fst + (int)mac->nparam + 1: fst;
3933 lst = lst < 0 ? lst + (int)mac->nparam + 1: lst;
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003934
3935 /* counted from zero */
3936 fst--, lst--;
3937
3938 /*
Cyrill Gorcunove75331c2013-11-09 12:02:15 +04003939 * It will be at least one token. Note we
3940 * need to scan params until separator, otherwise
3941 * only first token will be passed.
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003942 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003943 tm = mac->params[(fst + mac->rotate) % mac->nparam];
Cyrill Gorcunove75331c2013-11-09 12:02:15 +04003944 head = new_Token(NULL, tm->type, tm->text, 0);
3945 tt = &head->next, tm = tm->next;
3946 while (tok_isnt_(tm, ",")) {
3947 t = new_Token(NULL, tm->type, tm->text, 0);
3948 *tt = t, tt = &t->next, tm = tm->next;
3949 }
3950
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003951 if (fst < lst) {
3952 for (i = fst + 1; i <= lst; i++) {
3953 t = new_Token(NULL, TOK_OTHER, ",", 0);
3954 *tt = t, tt = &t->next;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003955 j = (i + mac->rotate) % mac->nparam;
3956 tm = mac->params[j];
Cyrill Gorcunove75331c2013-11-09 12:02:15 +04003957 while (tok_isnt_(tm, ",")) {
3958 t = new_Token(NULL, tm->type, tm->text, 0);
3959 *tt = t, tt = &t->next, tm = tm->next;
3960 }
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003961 }
3962 } else {
3963 for (i = fst - 1; i >= lst; i--) {
3964 t = new_Token(NULL, TOK_OTHER, ",", 0);
3965 *tt = t, tt = &t->next;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003966 j = (i + mac->rotate) % mac->nparam;
3967 tm = mac->params[j];
Cyrill Gorcunove75331c2013-11-09 12:02:15 +04003968 while (tok_isnt_(tm, ",")) {
3969 t = new_Token(NULL, tm->type, tm->text, 0);
3970 *tt = t, tt = &t->next, tm = tm->next;
3971 }
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003972 }
3973 }
3974
3975 *last = tt;
3976 return head;
3977
3978err:
H. Peter Anvin130736c2016-02-17 20:27:41 -08003979 nasm_error(ERR_NONFATAL, "`%%{%s}': macro parameters out of range",
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003980 &tline->text[1]);
3981 return tline;
3982}
3983
H. Peter Anvin76690a12002-04-30 20:52:49 +00003984/*
3985 * Expand MMacro-local things: parameter references (%0, %n, %+n,
H. Peter Anvin67c63722008-10-26 23:49:00 -07003986 * %-n) and MMacro-local identifiers (%%foo) as well as
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003987 * macro indirection (%[...]) and range (%{..:..}).
H. Peter Anvin76690a12002-04-30 20:52:49 +00003988 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003989static Token *expand_mmac_params(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00003990{
H. Peter Anvin734b1882002-04-30 21:01:08 +00003991 Token *t, *tt, **tail, *thead;
H. Peter Anvin6125b622009-04-08 14:02:25 -07003992 bool changed = false;
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003993 char *pos;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003994
3995 tail = &thead;
3996 thead = NULL;
3997
H. Peter Anvine2c80182005-01-15 22:15:51 +00003998 while (tline) {
3999 if (tline->type == TOK_PREPROC_ID &&
Cyrill Gorcunovca611192010-06-04 09:22:12 +04004000 (((tline->text[1] == '+' || tline->text[1] == '-') && tline->text[2]) ||
4001 (tline->text[1] >= '0' && tline->text[1] <= '9') ||
4002 tline->text[1] == '%')) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004003 char *text = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004004 int type = 0, cc; /* type = 0 to placate optimisers */
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004005 char tmpbuf[30];
H. Peter Anvin25a99342007-09-22 17:45:45 -07004006 unsigned int n;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004007 int i;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004008 MMacro *mac;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004009
H. Peter Anvine2c80182005-01-15 22:15:51 +00004010 t = tline;
4011 tline = tline->next;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004012
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004013 mac = istk->mstk;
4014 while (mac && !mac->name) /* avoid mistaking %reps for macros */
4015 mac = mac->next_active;
4016 if (!mac) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08004017 nasm_error(ERR_NONFATAL, "`%s': not in a macro call", t->text);
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004018 } else {
4019 pos = strchr(t->text, ':');
4020 if (!pos) {
4021 switch (t->text[1]) {
4022 /*
4023 * We have to make a substitution of one of the
4024 * forms %1, %-1, %+1, %%foo, %0.
4025 */
4026 case '0':
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004027 type = TOK_NUMBER;
4028 snprintf(tmpbuf, sizeof(tmpbuf), "%d", mac->nparam);
4029 text = nasm_strdup(tmpbuf);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004030 break;
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004031 case '%':
H. Peter Anvine2c80182005-01-15 22:15:51 +00004032 type = TOK_ID;
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004033 snprintf(tmpbuf, sizeof(tmpbuf), "..@%"PRIu64".",
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004034 mac->unique);
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004035 text = nasm_strcat(tmpbuf, t->text + 2);
4036 break;
4037 case '-':
4038 n = atoi(t->text + 2) - 1;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004039 if (n >= mac->nparam)
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004040 tt = NULL;
4041 else {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004042 if (mac->nparam > 1)
4043 n = (n + mac->rotate) % mac->nparam;
4044 tt = mac->params[n];
H. Peter Anvine2c80182005-01-15 22:15:51 +00004045 }
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004046 cc = find_cc(tt);
4047 if (cc == -1) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08004048 nasm_error(ERR_NONFATAL,
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004049 "macro parameter %d is not a condition code",
4050 n + 1);
4051 text = NULL;
4052 } else {
4053 type = TOK_ID;
4054 if (inverse_ccs[cc] == -1) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08004055 nasm_error(ERR_NONFATAL,
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004056 "condition code `%s' is not invertible",
4057 conditions[cc]);
4058 text = NULL;
4059 } else
4060 text = nasm_strdup(conditions[inverse_ccs[cc]]);
4061 }
4062 break;
4063 case '+':
4064 n = atoi(t->text + 2) - 1;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004065 if (n >= mac->nparam)
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004066 tt = NULL;
4067 else {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004068 if (mac->nparam > 1)
4069 n = (n + mac->rotate) % mac->nparam;
4070 tt = mac->params[n];
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004071 }
4072 cc = find_cc(tt);
4073 if (cc == -1) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08004074 nasm_error(ERR_NONFATAL,
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004075 "macro parameter %d is not a condition code",
4076 n + 1);
4077 text = NULL;
4078 } else {
4079 type = TOK_ID;
4080 text = nasm_strdup(conditions[cc]);
4081 }
4082 break;
4083 default:
4084 n = atoi(t->text + 1) - 1;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004085 if (n >= mac->nparam)
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004086 tt = NULL;
4087 else {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004088 if (mac->nparam > 1)
4089 n = (n + mac->rotate) % mac->nparam;
4090 tt = mac->params[n];
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004091 }
4092 if (tt) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004093 for (i = 0; i < mac->paramlen[n]; i++) {
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004094 *tail = new_Token(NULL, tt->type, tt->text, 0);
4095 tail = &(*tail)->next;
4096 tt = tt->next;
4097 }
4098 }
4099 text = NULL; /* we've done it here */
4100 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004101 }
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004102 } else {
4103 /*
4104 * seems we have a parameters range here
4105 */
4106 Token *head, **last;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004107 head = expand_mmac_params_range(mac, t, &last);
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004108 if (head != t) {
4109 *tail = head;
4110 *last = tline;
4111 tline = head;
4112 text = NULL;
4113 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00004114 }
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004115 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00004116 if (!text) {
4117 delete_Token(t);
4118 } else {
4119 *tail = t;
4120 tail = &t->next;
4121 t->type = type;
4122 nasm_free(t->text);
4123 t->text = text;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004124 t->a.mac = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004125 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004126 changed = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004127 continue;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004128 } else if (tline->type == TOK_INDIRECT) {
4129 t = tline;
4130 tline = tline->next;
4131 tt = tokenize(t->text);
4132 tt = expand_mmac_params(tt);
4133 tt = expand_smacro(tt);
4134 *tail = tt;
4135 while (tt) {
4136 tt->a.mac = NULL; /* Necessary? */
4137 tail = &tt->next;
4138 tt = tt->next;
4139 }
4140 delete_Token(t);
4141 changed = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004142 } else {
4143 t = *tail = tline;
4144 tline = tline->next;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004145 t->a.mac = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004146 tail = &t->next;
4147 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00004148 }
H. Peter Anvineba20a72002-04-30 20:53:55 +00004149 *tail = NULL;
H. Peter Anvin67c63722008-10-26 23:49:00 -07004150
Cyrill Gorcunovc6a742c2011-06-27 01:23:09 +04004151 if (changed) {
4152 const struct tokseq_match t[] = {
4153 {
4154 PP_CONCAT_MASK(TOK_ID) |
4155 PP_CONCAT_MASK(TOK_FLOAT), /* head */
4156 PP_CONCAT_MASK(TOK_ID) |
4157 PP_CONCAT_MASK(TOK_NUMBER) |
4158 PP_CONCAT_MASK(TOK_FLOAT) |
4159 PP_CONCAT_MASK(TOK_OTHER) /* tail */
4160 },
4161 {
4162 PP_CONCAT_MASK(TOK_NUMBER), /* head */
4163 PP_CONCAT_MASK(TOK_NUMBER) /* tail */
4164 }
4165 };
4166 paste_tokens(&thead, t, ARRAY_SIZE(t), false);
4167 }
H. Peter Anvin6125b622009-04-08 14:02:25 -07004168
H. Peter Anvin76690a12002-04-30 20:52:49 +00004169 return thead;
4170}
4171
4172/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004173 * Expand all single-line macro calls made in the given line.
4174 * Return the expanded version of the line. The original is deemed
4175 * to be destroyed in the process. (In reality we'll just move
4176 * Tokens from input to output a lot of the time, rather than
4177 * actually bothering to destroy and replicate.)
4178 */
H. Peter Anvincb1cf592007-11-19 12:26:50 -08004179
H. Peter Anvine2c80182005-01-15 22:15:51 +00004180static Token *expand_smacro(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004181{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004182 Token *t, *tt, *mstart, **tail, *thead;
H. Peter Anvineba20a72002-04-30 20:53:55 +00004183 SMacro *head = NULL, *m;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004184 Token **params;
4185 int *paramsize;
H. Peter Anvin25a99342007-09-22 17:45:45 -07004186 unsigned int nparam, sparam;
H. Peter Anvind784a082009-04-20 14:01:18 -07004187 int brackets;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004188 Token *org_tline = tline;
4189 Context *ctx;
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08004190 const char *mname;
H. Peter Anvin2a15e692007-11-19 13:14:59 -08004191 int deadman = DEADMAN_LIMIT;
H. Peter Anvin8287daf2009-07-07 16:00:58 -07004192 bool expanded;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004193
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004194 /*
4195 * Trick: we should avoid changing the start token pointer since it can
4196 * be contained in "next" field of other token. Because of this
4197 * we allocate a copy of first token and work with it; at the end of
4198 * routine we copy it back
4199 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004200 if (org_tline) {
Cyrill Gorcunoved4a8052010-04-09 15:40:35 +04004201 tline = new_Token(org_tline->next, org_tline->type,
4202 org_tline->text, 0);
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004203 tline->a.mac = org_tline->a.mac;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004204 nasm_free(org_tline->text);
4205 org_tline->text = NULL;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004206 }
4207
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004208 expanded = true; /* Always expand %+ at least once */
H. Peter Anvin8287daf2009-07-07 16:00:58 -07004209
H. Peter Anvincb1cf592007-11-19 12:26:50 -08004210again:
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004211 thead = NULL;
Cyrill Gorcunoved4a8052010-04-09 15:40:35 +04004212 tail = &thead;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004213
H. Peter Anvine2c80182005-01-15 22:15:51 +00004214 while (tline) { /* main token loop */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004215 if (!--deadman) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08004216 nasm_error(ERR_NONFATAL, "interminable macro recursion");
Cyrill Gorcunovbd38c8f2009-11-21 11:11:23 +03004217 goto err;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004218 }
H. Peter Anvincb1cf592007-11-19 12:26:50 -08004219
H. Peter Anvine2c80182005-01-15 22:15:51 +00004220 if ((mname = tline->text)) {
4221 /* if this token is a local macro, look in local context */
Cyrill Gorcunovc56d9ad2010-02-11 15:12:19 +03004222 if (tline->type == TOK_ID) {
4223 head = (SMacro *)hash_findix(&smacros, mname);
4224 } else if (tline->type == TOK_PREPROC_ID) {
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04004225 ctx = get_ctx(mname, &mname);
Cyrill Gorcunovc56d9ad2010-02-11 15:12:19 +03004226 head = ctx ? (SMacro *)hash_findix(&ctx->localmac, mname) : NULL;
4227 } else
4228 head = NULL;
H. Peter Anvin072771e2008-05-22 13:17:51 -07004229
H. Peter Anvine2c80182005-01-15 22:15:51 +00004230 /*
4231 * We've hit an identifier. As in is_mmacro below, we first
4232 * check whether the identifier is a single-line macro at
4233 * all, then think about checking for parameters if
4234 * necessary.
4235 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004236 list_for_each(m, head)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004237 if (!mstrcmp(m->name, mname, m->casesense))
4238 break;
4239 if (m) {
4240 mstart = tline;
4241 params = NULL;
4242 paramsize = NULL;
4243 if (m->nparam == 0) {
4244 /*
4245 * Simple case: the macro is parameterless. Discard the
4246 * one token that the macro call took, and push the
4247 * expansion back on the to-do stack.
4248 */
4249 if (!m->expansion) {
4250 if (!strcmp("__FILE__", m->name)) {
H. Peter Anvin274cda82016-05-10 02:56:29 -07004251 const char *file = src_get_fname();
4252 /* nasm_free(tline->text); here? */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004253 tline->text = nasm_quote(file, strlen(file));
4254 tline->type = TOK_STRING;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004255 continue;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004256 }
4257 if (!strcmp("__LINE__", m->name)) {
4258 nasm_free(tline->text);
4259 make_tok_num(tline, src_get_linnum());
4260 continue;
4261 }
4262 if (!strcmp("__BITS__", m->name)) {
4263 nasm_free(tline->text);
4264 make_tok_num(tline, globalbits);
4265 continue;
4266 }
4267 tline = delete_Token(tline);
4268 continue;
4269 }
4270 } else {
4271 /*
4272 * Complicated case: at least one macro with this name
H. Peter Anvine2c80182005-01-15 22:15:51 +00004273 * exists and takes parameters. We must find the
4274 * parameters in the call, count them, find the SMacro
4275 * that corresponds to that form of the macro call, and
4276 * substitute for the parameters when we expand. What a
4277 * pain.
4278 */
4279 /*tline = tline->next;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004280 skip_white_(tline); */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004281 do {
4282 t = tline->next;
4283 while (tok_type_(t, TOK_SMAC_END)) {
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004284 t->a.mac->in_progress = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004285 t->text = NULL;
4286 t = tline->next = delete_Token(t);
4287 }
4288 tline = t;
4289 } while (tok_type_(tline, TOK_WHITESPACE));
4290 if (!tok_is_(tline, "(")) {
4291 /*
4292 * This macro wasn't called with parameters: ignore
4293 * the call. (Behaviour borrowed from gnu cpp.)
4294 */
4295 tline = mstart;
4296 m = NULL;
4297 } else {
4298 int paren = 0;
4299 int white = 0;
4300 brackets = 0;
4301 nparam = 0;
4302 sparam = PARAM_DELTA;
4303 params = nasm_malloc(sparam * sizeof(Token *));
4304 params[0] = tline->next;
4305 paramsize = nasm_malloc(sparam * sizeof(int));
4306 paramsize[0] = 0;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07004307 while (true) { /* parameter loop */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004308 /*
4309 * For some unusual expansions
4310 * which concatenates function call
4311 */
4312 t = tline->next;
4313 while (tok_type_(t, TOK_SMAC_END)) {
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004314 t->a.mac->in_progress = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004315 t->text = NULL;
4316 t = tline->next = delete_Token(t);
4317 }
4318 tline = t;
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00004319
H. Peter Anvine2c80182005-01-15 22:15:51 +00004320 if (!tline) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08004321 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00004322 "macro call expects terminating `)'");
4323 break;
4324 }
4325 if (tline->type == TOK_WHITESPACE
4326 && brackets <= 0) {
4327 if (paramsize[nparam])
4328 white++;
4329 else
4330 params[nparam] = tline->next;
4331 continue; /* parameter loop */
4332 }
4333 if (tline->type == TOK_OTHER
4334 && tline->text[1] == 0) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004335 char ch = tline->text[0];
H. Peter Anvine2c80182005-01-15 22:15:51 +00004336 if (ch == ',' && !paren && brackets <= 0) {
4337 if (++nparam >= sparam) {
4338 sparam += PARAM_DELTA;
4339 params = nasm_realloc(params,
Cyrill Gorcunoved4a8052010-04-09 15:40:35 +04004340 sparam * sizeof(Token *));
4341 paramsize = nasm_realloc(paramsize,
4342 sparam * sizeof(int));
H. Peter Anvine2c80182005-01-15 22:15:51 +00004343 }
4344 params[nparam] = tline->next;
4345 paramsize[nparam] = 0;
4346 white = 0;
4347 continue; /* parameter loop */
4348 }
4349 if (ch == '{' &&
4350 (brackets > 0 || (brackets == 0 &&
4351 !paramsize[nparam])))
4352 {
4353 if (!(brackets++)) {
4354 params[nparam] = tline->next;
4355 continue; /* parameter loop */
4356 }
4357 }
4358 if (ch == '}' && brackets > 0)
4359 if (--brackets == 0) {
4360 brackets = -1;
4361 continue; /* parameter loop */
4362 }
4363 if (ch == '(' && !brackets)
4364 paren++;
4365 if (ch == ')' && brackets <= 0)
4366 if (--paren < 0)
4367 break;
4368 }
4369 if (brackets < 0) {
4370 brackets = 0;
H. Peter Anvin130736c2016-02-17 20:27:41 -08004371 nasm_error(ERR_NONFATAL, "braces do not "
H. Peter Anvine2c80182005-01-15 22:15:51 +00004372 "enclose all of macro parameter");
4373 }
4374 paramsize[nparam] += white + 1;
4375 white = 0;
4376 } /* parameter loop */
4377 nparam++;
4378 while (m && (m->nparam != nparam ||
4379 mstrcmp(m->name, mname,
4380 m->casesense)))
4381 m = m->next;
4382 if (!m)
H. Peter Anvin130736c2016-02-17 20:27:41 -08004383 nasm_error(ERR_WARNING|ERR_PASS1|ERR_WARN_MNP,
H. Peter Anvine2c80182005-01-15 22:15:51 +00004384 "macro `%s' exists, "
4385 "but not taking %d parameters",
4386 mstart->text, nparam);
4387 }
4388 }
4389 if (m && m->in_progress)
4390 m = NULL;
4391 if (!m) { /* in progess or didn't find '(' or wrong nparam */
H. Peter Anvin70653092007-10-19 14:42:29 -07004392 /*
H. Peter Anvine2c80182005-01-15 22:15:51 +00004393 * Design question: should we handle !tline, which
4394 * indicates missing ')' here, or expand those
4395 * macros anyway, which requires the (t) test a few
H. Peter Anvin70653092007-10-19 14:42:29 -07004396 * lines down?
H. Peter Anvine2c80182005-01-15 22:15:51 +00004397 */
4398 nasm_free(params);
4399 nasm_free(paramsize);
4400 tline = mstart;
4401 } else {
4402 /*
4403 * Expand the macro: we are placed on the last token of the
4404 * call, so that we can easily split the call from the
4405 * following tokens. We also start by pushing an SMAC_END
4406 * token for the cycle removal.
4407 */
4408 t = tline;
4409 if (t) {
4410 tline = t->next;
4411 t->next = NULL;
4412 }
4413 tt = new_Token(tline, TOK_SMAC_END, NULL, 0);
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004414 tt->a.mac = m;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07004415 m->in_progress = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004416 tline = tt;
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04004417 list_for_each(t, m->expansion) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004418 if (t->type >= TOK_SMAC_PARAM) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00004419 Token *pcopy = tline, **ptail = &pcopy;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004420 Token *ttt, *pt;
4421 int i;
H. Peter Anvin734b1882002-04-30 21:01:08 +00004422
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004423 ttt = params[t->type - TOK_SMAC_PARAM];
4424 i = paramsize[t->type - TOK_SMAC_PARAM];
4425 while (--i >= 0) {
4426 pt = *ptail = new_Token(tline, ttt->type,
4427 ttt->text, 0);
4428 ptail = &pt->next;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004429 ttt = ttt->next;
4430 }
4431 tline = pcopy;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004432 } else if (t->type == TOK_PREPROC_Q) {
4433 tt = new_Token(tline, TOK_ID, mname, 0);
4434 tline = tt;
4435 } else if (t->type == TOK_PREPROC_QQ) {
4436 tt = new_Token(tline, TOK_ID, m->name, 0);
4437 tline = tt;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004438 } else {
4439 tt = new_Token(tline, t->type, t->text, 0);
4440 tline = tt;
4441 }
4442 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00004443
H. Peter Anvine2c80182005-01-15 22:15:51 +00004444 /*
4445 * Having done that, get rid of the macro call, and clean
4446 * up the parameters.
4447 */
4448 nasm_free(params);
4449 nasm_free(paramsize);
4450 free_tlist(mstart);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004451 expanded = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004452 continue; /* main token loop */
4453 }
4454 }
4455 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004456
H. Peter Anvine2c80182005-01-15 22:15:51 +00004457 if (tline->type == TOK_SMAC_END) {
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004458 tline->a.mac->in_progress = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004459 tline = delete_Token(tline);
4460 } else {
4461 t = *tail = tline;
4462 tline = tline->next;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004463 t->a.mac = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004464 t->next = NULL;
4465 tail = &t->next;
4466 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004467 }
4468
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004469 /*
4470 * Now scan the entire line and look for successive TOK_IDs that resulted
Keith Kaniosb7a89542007-04-12 02:40:54 +00004471 * after expansion (they can't be produced by tokenize()). The successive
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004472 * TOK_IDs should be concatenated.
4473 * Also we look for %+ tokens and concatenate the tokens before and after
4474 * them (without white spaces in between).
4475 */
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +04004476 if (expanded) {
Cyrill Gorcunovc6a742c2011-06-27 01:23:09 +04004477 const struct tokseq_match t[] = {
4478 {
4479 PP_CONCAT_MASK(TOK_ID) |
4480 PP_CONCAT_MASK(TOK_PREPROC_ID), /* head */
4481 PP_CONCAT_MASK(TOK_ID) |
4482 PP_CONCAT_MASK(TOK_PREPROC_ID) |
4483 PP_CONCAT_MASK(TOK_NUMBER) /* tail */
4484 }
4485 };
4486 if (paste_tokens(&thead, t, ARRAY_SIZE(t), true)) {
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +04004487 /*
4488 * If we concatenated something, *and* we had previously expanded
4489 * an actual macro, scan the lines again for macros...
4490 */
4491 tline = thead;
4492 expanded = false;
4493 goto again;
4494 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00004495 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004496
Cyrill Gorcunovbd38c8f2009-11-21 11:11:23 +03004497err:
H. Peter Anvine2c80182005-01-15 22:15:51 +00004498 if (org_tline) {
4499 if (thead) {
4500 *org_tline = *thead;
4501 /* since we just gave text to org_line, don't free it */
4502 thead->text = NULL;
4503 delete_Token(thead);
4504 } else {
4505 /* the expression expanded to empty line;
4506 we can't return NULL for some reasons
4507 we just set the line to a single WHITESPACE token. */
4508 memset(org_tline, 0, sizeof(*org_tline));
4509 org_tline->text = NULL;
4510 org_tline->type = TOK_WHITESPACE;
4511 }
4512 thead = org_tline;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004513 }
4514
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004515 return thead;
4516}
4517
4518/*
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004519 * Similar to expand_smacro but used exclusively with macro identifiers
4520 * right before they are fetched in. The reason is that there can be
4521 * identifiers consisting of several subparts. We consider that if there
4522 * are more than one element forming the name, user wants a expansion,
4523 * otherwise it will be left as-is. Example:
4524 *
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004525 * %define %$abc cde
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004526 *
4527 * the identifier %$abc will be left as-is so that the handler for %define
4528 * will suck it and define the corresponding value. Other case:
4529 *
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004530 * %define _%$abc cde
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004531 *
4532 * In this case user wants name to be expanded *before* %define starts
4533 * working, so we'll expand %$abc into something (if it has a value;
4534 * otherwise it will be left as-is) then concatenate all successive
4535 * PP_IDs into one.
4536 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004537static Token *expand_id(Token * tline)
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004538{
4539 Token *cur, *oldnext = NULL;
4540
H. Peter Anvin734b1882002-04-30 21:01:08 +00004541 if (!tline || !tline->next)
H. Peter Anvine2c80182005-01-15 22:15:51 +00004542 return tline;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004543
4544 cur = tline;
4545 while (cur->next &&
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004546 (cur->next->type == TOK_ID ||
4547 cur->next->type == TOK_PREPROC_ID
4548 || cur->next->type == TOK_NUMBER))
H. Peter Anvine2c80182005-01-15 22:15:51 +00004549 cur = cur->next;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004550
4551 /* If identifier consists of just one token, don't expand */
4552 if (cur == tline)
H. Peter Anvine2c80182005-01-15 22:15:51 +00004553 return tline;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004554
H. Peter Anvine2c80182005-01-15 22:15:51 +00004555 if (cur) {
4556 oldnext = cur->next; /* Detach the tail past identifier */
4557 cur->next = NULL; /* so that expand_smacro stops here */
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004558 }
4559
H. Peter Anvin734b1882002-04-30 21:01:08 +00004560 tline = expand_smacro(tline);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004561
H. Peter Anvine2c80182005-01-15 22:15:51 +00004562 if (cur) {
4563 /* expand_smacro possibly changhed tline; re-scan for EOL */
4564 cur = tline;
4565 while (cur && cur->next)
4566 cur = cur->next;
4567 if (cur)
4568 cur->next = oldnext;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004569 }
4570
4571 return tline;
4572}
4573
4574/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004575 * Determine whether the given line constitutes a multi-line macro
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004576 * call, and return the MMacro structure called if so. Doesn't have
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004577 * to check for an initial label - that's taken care of in
4578 * expand_mmacro - but must check numbers of parameters. Guaranteed
4579 * to be called with tline->type == TOK_ID, so the putative macro
4580 * name is easy to find.
4581 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004582static MMacro *is_mmacro(Token * tline, Token *** params_array)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004583{
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004584 MMacro *head, *m;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004585 Token **params;
4586 int nparam;
4587
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004588 head = (MMacro *) hash_findix(&mmacros, tline->text);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004589
4590 /*
4591 * Efficiency: first we see if any macro exists with the given
4592 * name. If not, we can return NULL immediately. _Then_ we
4593 * count the parameters, and then we look further along the
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004594 * list if necessary to find the proper MMacro.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004595 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004596 list_for_each(m, head)
4597 if (!mstrcmp(m->name, tline->text, m->casesense))
H. Peter Anvine2c80182005-01-15 22:15:51 +00004598 break;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004599 if (!m)
H. Peter Anvine2c80182005-01-15 22:15:51 +00004600 return NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004601
4602 /*
4603 * OK, we have a potential macro. Count and demarcate the
4604 * parameters.
4605 */
H. Peter Anvin734b1882002-04-30 21:01:08 +00004606 count_mmac_params(tline->next, &nparam, &params);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004607
4608 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004609 * So we know how many parameters we've got. Find the MMacro
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004610 * structure that handles this number.
4611 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004612 while (m) {
4613 if (m->nparam_min <= nparam
4614 && (m->plus || nparam <= m->nparam_max)) {
4615 /*
4616 * This one is right. Just check if cycle removal
4617 * prohibits us using it before we actually celebrate...
4618 */
4619 if (m->in_progress > m->max_depth) {
4620 if (m->max_depth > 0) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08004621 nasm_error(ERR_WARNING,
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004622 "reached maximum recursion depth of %i",
4623 m->max_depth);
4624 }
4625 nasm_free(params);
4626 return NULL;
4627 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00004628 /*
4629 * It's right, and we can use it. Add its default
4630 * parameters to the end of our list if necessary.
4631 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004632 if (m->defaults && nparam < m->nparam_min + m->ndefs) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00004633 params =
4634 nasm_realloc(params,
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004635 ((m->nparam_min + m->ndefs +
H. Peter Anvine2c80182005-01-15 22:15:51 +00004636 1) * sizeof(*params)));
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004637 while (nparam < m->nparam_min + m->ndefs) {
4638 params[nparam] = m->defaults[nparam - m->nparam_min];
H. Peter Anvine2c80182005-01-15 22:15:51 +00004639 nparam++;
4640 }
4641 }
4642 /*
4643 * If we've gone over the maximum parameter count (and
4644 * we're in Plus mode), ignore parameters beyond
4645 * nparam_max.
4646 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004647 if (m->plus && nparam > m->nparam_max)
4648 nparam = m->nparam_max;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004649 /*
4650 * Then terminate the parameter list, and leave.
4651 */
4652 if (!params) { /* need this special case */
4653 params = nasm_malloc(sizeof(*params));
4654 nparam = 0;
4655 }
4656 params[nparam] = NULL;
4657 *params_array = params;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004658 return m;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004659 }
4660 /*
4661 * This one wasn't right: look for the next one with the
4662 * same name.
4663 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004664 list_for_each(m, m->next)
4665 if (!mstrcmp(m->name, tline->text, m->casesense))
H. Peter Anvine2c80182005-01-15 22:15:51 +00004666 break;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004667 }
4668
4669 /*
4670 * After all that, we didn't find one with the right number of
4671 * parameters. Issue a warning, and fail to expand the macro.
4672 */
H. Peter Anvin130736c2016-02-17 20:27:41 -08004673 nasm_error(ERR_WARNING|ERR_PASS1|ERR_WARN_MNP,
H. Peter Anvine2c80182005-01-15 22:15:51 +00004674 "macro `%s' exists, but not taking %d parameters",
4675 tline->text, nparam);
H. Peter Anvin734b1882002-04-30 21:01:08 +00004676 nasm_free(params);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004677 return NULL;
4678}
4679
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004680
4681/*
4682 * Save MMacro invocation specific fields in
4683 * preparation for a recursive macro expansion
4684 */
4685static void push_mmacro(MMacro *m)
4686{
4687 MMacroInvocation *i;
4688
4689 i = nasm_malloc(sizeof(MMacroInvocation));
4690 i->prev = m->prev;
4691 i->params = m->params;
4692 i->iline = m->iline;
4693 i->nparam = m->nparam;
4694 i->rotate = m->rotate;
4695 i->paramlen = m->paramlen;
4696 i->unique = m->unique;
4697 i->condcnt = m->condcnt;
4698 m->prev = i;
4699}
4700
4701
4702/*
4703 * Restore MMacro invocation specific fields that were
4704 * saved during a previous recursive macro expansion
4705 */
4706static void pop_mmacro(MMacro *m)
4707{
4708 MMacroInvocation *i;
4709
4710 if (m->prev) {
4711 i = m->prev;
4712 m->prev = i->prev;
4713 m->params = i->params;
4714 m->iline = i->iline;
4715 m->nparam = i->nparam;
4716 m->rotate = i->rotate;
4717 m->paramlen = i->paramlen;
4718 m->unique = i->unique;
4719 m->condcnt = i->condcnt;
4720 nasm_free(i);
4721 }
4722}
4723
4724
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004725/*
4726 * Expand the multi-line macro call made by the given line, if
4727 * there is one to be expanded. If there is, push the expansion on
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004728 * istk->expansion and return 1. Otherwise return 0.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004729 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004730static int expand_mmacro(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004731{
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004732 Token *startline = tline;
H. Peter Anvineba20a72002-04-30 20:53:55 +00004733 Token *label = NULL;
4734 int dont_prepend = 0;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004735 Token **params, *t, *tt;
4736 MMacro *m;
4737 Line *l, *ll;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004738 int i, nparam, *paramlen;
H. Peter Anvinc751e862008-06-09 10:18:45 -07004739 const char *mname;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004740
4741 t = tline;
H. Peter Anvineba20a72002-04-30 20:53:55 +00004742 skip_white_(t);
H. Peter Anvince2233b2008-05-25 21:57:00 -07004743 /* if (!tok_type_(t, TOK_ID)) Lino 02/25/02 */
H. Peter Anvindce1e2f2002-04-30 21:06:37 +00004744 if (!tok_type_(t, TOK_ID) && !tok_type_(t, TOK_PREPROC_ID))
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004745 return 0;
4746 m = is_mmacro(t, &params);
4747 if (m) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004748 mname = t->text;
H. Peter Anvinc751e862008-06-09 10:18:45 -07004749 } else {
H. Peter Anvine2c80182005-01-15 22:15:51 +00004750 Token *last;
4751 /*
4752 * We have an id which isn't a macro call. We'll assume
4753 * it might be a label; we'll also check to see if a
4754 * colon follows it. Then, if there's another id after
4755 * that lot, we'll check it again for macro-hood.
4756 */
4757 label = last = t;
4758 t = t->next;
4759 if (tok_type_(t, TOK_WHITESPACE))
4760 last = t, t = t->next;
4761 if (tok_is_(t, ":")) {
4762 dont_prepend = 1;
4763 last = t, t = t->next;
4764 if (tok_type_(t, TOK_WHITESPACE))
4765 last = t, t = t->next;
4766 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004767 if (!tok_type_(t, TOK_ID) || !(m = is_mmacro(t, &params)))
4768 return 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004769 last->next = NULL;
Keith Kanios891775e2009-07-11 06:08:54 -05004770 mname = t->text;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004771 tline = t;
H. Peter Anvineba20a72002-04-30 20:53:55 +00004772 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004773
4774 /*
4775 * Fix up the parameters: this involves stripping leading and
4776 * trailing whitespace, then stripping braces if they are
4777 * present.
4778 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004779 for (nparam = 0; params[nparam]; nparam++) ;
H. Peter Anvin734b1882002-04-30 21:01:08 +00004780 paramlen = nparam ? nasm_malloc(nparam * sizeof(*paramlen)) : NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004781
H. Peter Anvine2c80182005-01-15 22:15:51 +00004782 for (i = 0; params[i]; i++) {
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08004783 int brace = 0;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004784 int comma = (!m->plus || i < nparam - 1);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004785
H. Peter Anvine2c80182005-01-15 22:15:51 +00004786 t = params[i];
4787 skip_white_(t);
4788 if (tok_is_(t, "{"))
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08004789 t = t->next, brace++, comma = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004790 params[i] = t;
4791 paramlen[i] = 0;
4792 while (t) {
4793 if (comma && t->type == TOK_OTHER && !strcmp(t->text, ","))
4794 break; /* ... because we have hit a comma */
4795 if (comma && t->type == TOK_WHITESPACE
4796 && tok_is_(t->next, ","))
4797 break; /* ... or a space then a comma */
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08004798 if (brace && t->type == TOK_OTHER) {
4799 if (t->text[0] == '{')
4800 brace++; /* ... or a nested opening brace */
4801 else if (t->text[0] == '}')
4802 if (!--brace)
4803 break; /* ... or a brace */
4804 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00004805 t = t->next;
4806 paramlen[i]++;
4807 }
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08004808 if (brace)
H. Peter Anvin130736c2016-02-17 20:27:41 -08004809 nasm_error(ERR_NONFATAL, "macro params should be enclosed in braces");
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004810 }
4811
4812 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004813 * OK, we have a MMacro structure together with a set of
4814 * parameters. We must now go through the expansion and push
4815 * copies of each Line on to istk->expansion. Substitution of
H. Peter Anvin76690a12002-04-30 20:52:49 +00004816 * parameter tokens and macro-local tokens doesn't get done
4817 * until the single-line macro substitution process; this is
4818 * because delaying them allows us to change the semantics
4819 * later through %rotate.
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004820 *
4821 * First, push an end marker on to istk->expansion, mark this
4822 * macro as in progress, and set up its invocation-specific
4823 * variables.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004824 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004825 ll = nasm_malloc(sizeof(Line));
4826 ll->next = istk->expansion;
4827 ll->finishes = m;
4828 ll->first = NULL;
4829 istk->expansion = ll;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004830
4831 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004832 * Save the previous MMacro expansion in the case of
4833 * macro recursion
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004834 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004835 if (m->max_depth && m->in_progress)
4836 push_mmacro(m);
4837
4838 m->in_progress ++;
4839 m->params = params;
4840 m->iline = tline;
4841 m->nparam = nparam;
4842 m->rotate = 0;
4843 m->paramlen = paramlen;
4844 m->unique = unique++;
4845 m->lineno = 0;
4846 m->condcnt = 0;
4847
4848 m->next_active = istk->mstk;
4849 istk->mstk = m;
4850
4851 list_for_each(l, m->expansion) {
4852 Token **tail;
4853
4854 ll = nasm_malloc(sizeof(Line));
4855 ll->finishes = NULL;
4856 ll->next = istk->expansion;
4857 istk->expansion = ll;
4858 tail = &ll->first;
4859
4860 list_for_each(t, l->first) {
4861 Token *x = t;
4862 switch (t->type) {
4863 case TOK_PREPROC_Q:
4864 tt = *tail = new_Token(NULL, TOK_ID, mname, 0);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004865 break;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004866 case TOK_PREPROC_QQ:
4867 tt = *tail = new_Token(NULL, TOK_ID, m->name, 0);
4868 break;
4869 case TOK_PREPROC_ID:
4870 if (t->text[1] == '0' && t->text[2] == '0') {
4871 dont_prepend = -1;
4872 x = label;
4873 if (!x)
4874 continue;
4875 }
4876 /* fall through */
4877 default:
4878 tt = *tail = new_Token(NULL, x->type, x->text, 0);
4879 break;
4880 }
4881 tail = &tt->next;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004882 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004883 *tail = NULL;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004884 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004885
4886 /*
H. Peter Anvineba20a72002-04-30 20:53:55 +00004887 * If we had a label, push it on as the first line of
4888 * the macro expansion.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004889 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004890 if (label) {
4891 if (dont_prepend < 0)
4892 free_tlist(startline);
4893 else {
4894 ll = nasm_malloc(sizeof(Line));
4895 ll->finishes = NULL;
4896 ll->next = istk->expansion;
4897 istk->expansion = ll;
4898 ll->first = startline;
4899 if (!dont_prepend) {
4900 while (label->next)
4901 label = label->next;
4902 label->next = tt = new_Token(NULL, TOK_OTHER, ":", 0);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004903 }
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004904 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004905 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004906
H. Peter Anvin8ac25aa2016-02-18 01:16:18 -08004907 lfmt->uplevel(m->nolist ? LIST_MACRO_NOLIST : LIST_MACRO);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004908
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004909 return 1;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004910}
4911
H. Peter Anvin130736c2016-02-17 20:27:41 -08004912/*
4913 * This function adds macro names to error messages, and suppresses
4914 * them if necessary.
4915 */
4916static void pp_verror(int severity, const char *fmt, va_list arg)
Victor van den Elzen3b404c02008-09-18 13:51:36 +02004917{
H. Peter Anvin130736c2016-02-17 20:27:41 -08004918 char buff[BUFSIZ];
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004919 MMacro *mmac = NULL;
4920 int delta = 0;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02004921
H. Peter Anvin130736c2016-02-17 20:27:41 -08004922 /*
4923 * If we're in a dead branch of IF or something like it, ignore the error.
4924 * However, because %else etc are evaluated in the state context
4925 * of the previous branch, errors might get lost:
4926 * %if 0 ... %else trailing garbage ... %endif
4927 * So %else etc should set the ERR_PP_PRECOND flag.
4928 */
4929 if ((severity & ERR_MASK) < ERR_FATAL &&
4930 istk && istk->conds &&
4931 ((severity & ERR_PP_PRECOND) ?
4932 istk->conds->state == COND_NEVER :
H. Peter Anvineb6653f2016-04-05 13:03:10 -07004933 !emitting(istk->conds->state)))
H. Peter Anvin130736c2016-02-17 20:27:41 -08004934 return;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02004935
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004936 /* get %macro name */
H. Peter Anvin130736c2016-02-17 20:27:41 -08004937 if (!(severity & ERR_NOFILE) && istk && istk->mstk) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004938 mmac = istk->mstk;
4939 /* but %rep blocks should be skipped */
4940 while (mmac && !mmac->name)
4941 mmac = mmac->next_active, delta++;
Cyrill Gorcunov9900c6b2011-10-09 18:58:46 +04004942 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004943
H. Peter Anvin130736c2016-02-17 20:27:41 -08004944 if (mmac) {
4945 vsnprintf(buff, sizeof(buff), fmt, arg);
Victor van den Elzen3b404c02008-09-18 13:51:36 +02004946
H. Peter Anvin130736c2016-02-17 20:27:41 -08004947 nasm_set_verror(real_verror);
4948 nasm_error(severity, "(%s:%d) %s",
4949 mmac->name, mmac->lineno - delta, buff);
4950 nasm_set_verror(pp_verror);
4951 } else {
4952 real_verror(severity, fmt, arg);
4953 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004954}
4955
H. Peter Anvin734b1882002-04-30 21:01:08 +00004956static void
H. Peter Anvin130736c2016-02-17 20:27:41 -08004957pp_reset(char *file, int apass, StrList **deplist)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004958{
H. Peter Anvin7383b402008-09-24 10:20:40 -07004959 Token *t;
4960
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004961 cstk = NULL;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004962 istk = nasm_malloc(sizeof(Include));
4963 istk->next = NULL;
4964 istk->conds = NULL;
4965 istk->expansion = NULL;
4966 istk->mstk = NULL;
H. Peter Anvin3e83cec2016-05-25 04:28:46 -07004967 istk->fp = nasm_open_read(file, NF_TEXT);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004968 istk->fname = NULL;
H. Peter Anvin274cda82016-05-10 02:56:29 -07004969 src_set(0, file);
H. Peter Anvineba20a72002-04-30 20:53:55 +00004970 istk->lineinc = 1;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004971 if (!istk->fp)
H. Peter Anvin130736c2016-02-17 20:27:41 -08004972 nasm_fatal(ERR_NOFILE, "unable to open input file `%s'", file);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004973 defining = NULL;
Charles Crayned4200be2008-07-12 16:42:33 -07004974 nested_mac_count = 0;
4975 nested_rep_count = 0;
H. Peter Anvin97a23472007-09-16 17:57:25 -07004976 init_macros();
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004977 unique = 0;
H. Peter Anvinf7606612016-07-13 14:23:48 -07004978
4979 if (tasm_compatible_mode)
4980 pp_add_stdmac(nasm_stdmac_tasm);
4981
4982 pp_add_stdmac(nasm_stdmac_nasm);
4983 pp_add_stdmac(nasm_stdmac_version);
4984
Cyrill Gorcunov15ce78f2017-01-06 20:21:28 +03004985 if (extrastdmac)
4986 pp_add_stdmac(extrastdmac);
4987
H. Peter Anvinf7606612016-07-13 14:23:48 -07004988 stdmacpos = stdmacros[0];
4989 stdmacnext = &stdmacros[1];
4990
H. Peter Anvind2456592008-06-19 15:04:18 -07004991 do_predef = true;
H. Peter Anvin61f130f2008-09-25 15:45:06 -07004992
4993 /*
4994 * 0 for dependencies, 1 for preparatory passes, 2 for final pass.
4995 * The caller, however, will also pass in 3 for preprocess-only so
4996 * we can set __PASS__ accordingly.
4997 */
4998 pass = apass > 2 ? 2 : apass;
4999
H. Peter Anvin9924d1e2016-10-04 00:59:39 -07005000 dephead = deplist;
H. Peter Anvin436e3672016-10-04 01:12:28 -07005001 nasm_add_string_to_strlist(dephead, file);
H. Peter Anvin9924d1e2016-10-04 00:59:39 -07005002
H. Peter Anvin61f130f2008-09-25 15:45:06 -07005003 /*
5004 * Define the __PASS__ macro. This is defined here unlike
5005 * all the other builtins, because it is special -- it varies between
5006 * passes.
5007 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005008 t = nasm_malloc(sizeof(*t));
5009 t->next = NULL;
H. Peter Anvin61f130f2008-09-25 15:45:06 -07005010 make_tok_num(t, apass);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005011 t->a.mac = NULL;
H. Peter Anvin7383b402008-09-24 10:20:40 -07005012 define_smacro(NULL, "__PASS__", true, 0, t);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005013}
5014
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07005015static void pp_init(void)
5016{
5017 hash_init(&FileHash, HASH_MEDIUM);
5018}
5019
Keith Kaniosa6dfa782007-04-13 16:47:53 +00005020static char *pp_getline(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005021{
Keith Kaniosa6dfa782007-04-13 16:47:53 +00005022 char *line;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005023 Token *tline;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005024
H. Peter Anvin130736c2016-02-17 20:27:41 -08005025 real_verror = nasm_set_verror(pp_verror);
H. Peter Anvin215186f2016-02-17 20:27:41 -08005026
H. Peter Anvine2c80182005-01-15 22:15:51 +00005027 while (1) {
5028 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005029 * Fetch a tokenized line, either from the macro-expansion
H. Peter Anvine2c80182005-01-15 22:15:51 +00005030 * buffer or from the input file.
5031 */
5032 tline = NULL;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005033 while (istk->expansion && istk->expansion->finishes) {
5034 Line *l = istk->expansion;
5035 if (!l->finishes->name && l->finishes->in_progress > 1) {
5036 Line *ll;
H. Peter Anvineba20a72002-04-30 20:53:55 +00005037
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005038 /*
5039 * This is a macro-end marker for a macro with no
5040 * name, which means it's not really a macro at all
5041 * but a %rep block, and the `in_progress' field is
5042 * more than 1, meaning that we still need to
5043 * repeat. (1 means the natural last repetition; 0
5044 * means termination by %exitrep.) We have
5045 * therefore expanded up to the %endrep, and must
5046 * push the whole block on to the expansion buffer
5047 * again. We don't bother to remove the macro-end
5048 * marker: we'd only have to generate another one
5049 * if we did.
5050 */
5051 l->finishes->in_progress--;
5052 list_for_each(l, l->finishes->expansion) {
5053 Token *t, *tt, **tail;
5054
5055 ll = nasm_malloc(sizeof(Line));
5056 ll->next = istk->expansion;
5057 ll->finishes = NULL;
5058 ll->first = NULL;
5059 tail = &ll->first;
5060
5061 list_for_each(t, l->first) {
5062 if (t->text || t->type == TOK_WHITESPACE) {
5063 tt = *tail = new_Token(NULL, t->type, t->text, 0);
5064 tail = &tt->next;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005065 }
5066 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005067
5068 istk->expansion = ll;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005069 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005070 } else {
5071 /*
5072 * Check whether a `%rep' was started and not ended
5073 * within this macro expansion. This can happen and
5074 * should be detected. It's a fatal error because
5075 * I'm too confused to work out how to recover
5076 * sensibly from it.
5077 */
5078 if (defining) {
5079 if (defining->name)
H. Peter Anvin130736c2016-02-17 20:27:41 -08005080 nasm_panic(0, "defining with name in expansion");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005081 else if (istk->mstk->name)
H. Peter Anvin130736c2016-02-17 20:27:41 -08005082 nasm_fatal(0, "`%%rep' without `%%endrep' within"
5083 " expansion of macro `%s'",
5084 istk->mstk->name);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005085 }
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005086
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005087 /*
5088 * FIXME: investigate the relationship at this point between
5089 * istk->mstk and l->finishes
5090 */
5091 {
5092 MMacro *m = istk->mstk;
5093 istk->mstk = m->next_active;
5094 if (m->name) {
5095 /*
5096 * This was a real macro call, not a %rep, and
5097 * therefore the parameter information needs to
5098 * be freed.
5099 */
5100 if (m->prev) {
5101 pop_mmacro(m);
5102 l->finishes->in_progress --;
5103 } else {
5104 nasm_free(m->params);
5105 free_tlist(m->iline);
5106 nasm_free(m->paramlen);
5107 l->finishes->in_progress = 0;
5108 }
Adam Majer91e72402017-07-25 10:42:01 +02005109 }
5110
5111 /*
5112 * FIXME It is incorrect to always free_mmacro here.
5113 * It leads to usage-after-free.
5114 *
5115 * https://bugzilla.nasm.us/show_bug.cgi?id=3392414
5116 */
5117#if 0
5118 else
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005119 free_mmacro(m);
Adam Majer91e72402017-07-25 10:42:01 +02005120#endif
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005121 }
5122 istk->expansion = l->next;
5123 nasm_free(l);
H. Peter Anvin8ac25aa2016-02-18 01:16:18 -08005124 lfmt->downlevel(LIST_MACRO);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005125 }
5126 }
5127 while (1) { /* until we get a line we can use */
5128
5129 if (istk->expansion) { /* from a macro expansion */
5130 char *p;
5131 Line *l = istk->expansion;
5132 if (istk->mstk)
5133 istk->mstk->lineno++;
5134 tline = l->first;
5135 istk->expansion = l->next;
5136 nasm_free(l);
5137 p = detoken(tline, false);
H. Peter Anvin8ac25aa2016-02-18 01:16:18 -08005138 lfmt->line(LIST_MACRO, p);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005139 nasm_free(p);
5140 break;
5141 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00005142 line = read_line();
5143 if (line) { /* from the current input file */
5144 line = prepreproc(line);
Keith Kaniosb7a89542007-04-12 02:40:54 +00005145 tline = tokenize(line);
H. Peter Anvine2c80182005-01-15 22:15:51 +00005146 nasm_free(line);
5147 break;
5148 }
5149 /*
5150 * The current file has ended; work down the istk
5151 */
5152 {
5153 Include *i = istk;
5154 fclose(i->fp);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005155 if (i->conds) {
5156 /* nasm_error can't be conditionally suppressed */
H. Peter Anvin41087062016-03-03 14:27:34 -08005157 nasm_fatal(0,
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005158 "expected `%%endif' before end of file");
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005159 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00005160 /* only set line and file name if there's a next node */
H. Peter Anvin274cda82016-05-10 02:56:29 -07005161 if (i->next)
5162 src_set(i->lineno, i->fname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00005163 istk = i->next;
H. Peter Anvin8ac25aa2016-02-18 01:16:18 -08005164 lfmt->downlevel(LIST_INCLUDE);
H. Peter Anvine2c80182005-01-15 22:15:51 +00005165 nasm_free(i);
H. Peter Anvin130736c2016-02-17 20:27:41 -08005166 if (!istk) {
5167 line = NULL;
5168 goto done;
5169 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005170 if (istk->expansion && istk->expansion->finishes)
5171 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005172 }
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005173 }
5174
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005175 /*
5176 * We must expand MMacro parameters and MMacro-local labels
5177 * _before_ we plunge into directive processing, to cope
5178 * with things like `%define something %1' such as STRUC
5179 * uses. Unless we're _defining_ a MMacro, in which case
5180 * those tokens should be left alone to go into the
5181 * definition; and unless we're in a non-emitting
5182 * condition, in which case we don't want to meddle with
5183 * anything.
5184 */
5185 if (!defining && !(istk->conds && !emitting(istk->conds->state))
5186 && !(istk->mstk && !istk->mstk->in_progress)) {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005187 tline = expand_mmac_params(tline);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005188 }
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005189
H. Peter Anvine2c80182005-01-15 22:15:51 +00005190 /*
5191 * Check the line to see if it's a preprocessor directive.
5192 */
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07005193 if (do_directive(tline, &line) == DIRECTIVE_FOUND) {
5194 if (line)
5195 break; /* Directive generated output */
5196 else
5197 continue;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005198 } else if (defining) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00005199 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005200 * We're defining a multi-line macro. We emit nothing
5201 * at all, and just
5202 * shove the tokenized line on to the macro definition.
H. Peter Anvine2c80182005-01-15 22:15:51 +00005203 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005204 Line *l = nasm_malloc(sizeof(Line));
5205 l->next = defining->expansion;
5206 l->first = tline;
5207 l->finishes = NULL;
5208 defining->expansion = l;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005209 continue;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005210 } else if (istk->conds && !emitting(istk->conds->state)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00005211 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005212 * We're in a non-emitting branch of a condition block.
H. Peter Anvine2c80182005-01-15 22:15:51 +00005213 * Emit nothing at all, not even a blank line: when we
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005214 * emerge from the condition we'll give a line-number
H. Peter Anvine2c80182005-01-15 22:15:51 +00005215 * directive so we keep our place correctly.
5216 */
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005217 free_tlist(tline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00005218 continue;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005219 } else if (istk->mstk && !istk->mstk->in_progress) {
5220 /*
5221 * We're in a %rep block which has been terminated, so
5222 * we're walking through to the %endrep without
5223 * emitting anything. Emit nothing at all, not even a
5224 * blank line: when we emerge from the %rep block we'll
5225 * give a line-number directive so we keep our place
5226 * correctly.
5227 */
5228 free_tlist(tline);
5229 continue;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005230 } else {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005231 tline = expand_smacro(tline);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005232 if (!expand_mmacro(tline)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00005233 /*
Keith Kaniosb7a89542007-04-12 02:40:54 +00005234 * De-tokenize the line again, and emit it.
H. Peter Anvine2c80182005-01-15 22:15:51 +00005235 */
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005236 line = detoken(tline, true);
5237 free_tlist(tline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00005238 break;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005239 } else {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005240 continue; /* expand_mmacro calls free_tlist */
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005241 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00005242 }
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005243 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005244
H. Peter Anvin130736c2016-02-17 20:27:41 -08005245done:
5246 nasm_set_verror(real_verror);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005247 return line;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005248}
5249
H. Peter Anvine2c80182005-01-15 22:15:51 +00005250static void pp_cleanup(int pass)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005251{
H. Peter Anvin130736c2016-02-17 20:27:41 -08005252 real_verror = nasm_set_verror(pp_verror);
H. Peter Anvin215186f2016-02-17 20:27:41 -08005253
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005254 if (defining) {
5255 if (defining->name) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08005256 nasm_error(ERR_NONFATAL,
5257 "end of file while still defining macro `%s'",
5258 defining->name);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005259 } else {
H. Peter Anvin130736c2016-02-17 20:27:41 -08005260 nasm_error(ERR_NONFATAL, "end of file while still in %%rep");
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005261 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005262
5263 free_mmacro(defining);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005264 defining = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005265 }
H. Peter Anvin130736c2016-02-17 20:27:41 -08005266
5267 nasm_set_verror(real_verror);
H. Peter Anvin215186f2016-02-17 20:27:41 -08005268
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005269 while (cstk)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005270 ctx_pop();
H. Peter Anvin97a23472007-09-16 17:57:25 -07005271 free_macros();
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005272 while (istk) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00005273 Include *i = istk;
5274 istk = istk->next;
5275 fclose(i->fp);
Cyrill Gorcunov8dcfd882011-03-03 09:18:56 +03005276 nasm_free(i);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005277 }
5278 while (cstk)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005279 ctx_pop();
H. Peter Anvin274cda82016-05-10 02:56:29 -07005280 src_set_fname(NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00005281 if (pass == 0) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03005282 IncPath *i;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005283 free_llist(predef);
Cyrill Gorcunovdae24d72014-06-28 10:17:39 +04005284 predef = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005285 delete_Blocks();
Cyrill Gorcunovdae24d72014-06-28 10:17:39 +04005286 freeTokens = NULL;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03005287 while ((i = ipath)) {
5288 ipath = i->next;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005289 if (i->path)
5290 nasm_free(i->path);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03005291 nasm_free(i);
5292 }
H. Peter Anvin11dfa1a2008-07-02 18:11:04 -07005293 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005294}
5295
Cyrill Gorcunov0b78bff2012-05-07 01:57:55 +04005296static void pp_include_path(char *path)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005297{
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005298 IncPath *i;
H. Peter Anvin37a321f2007-09-24 13:41:58 -07005299
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005300 i = nasm_malloc(sizeof(IncPath));
5301 i->path = path ? nasm_strdup(path) : NULL;
5302 i->next = NULL;
Frank Kotlerd0ed6fd2003-08-27 11:33:56 +00005303
H. Peter Anvin89cee572009-07-15 09:16:54 -04005304 if (ipath) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00005305 IncPath *j = ipath;
H. Peter Anvin89cee572009-07-15 09:16:54 -04005306 while (j->next)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005307 j = j->next;
5308 j->next = i;
5309 } else {
5310 ipath = i;
Frank Kotlerd0ed6fd2003-08-27 11:33:56 +00005311 }
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005312}
Frank Kotlerd0ed6fd2003-08-27 11:33:56 +00005313
Cyrill Gorcunov0b78bff2012-05-07 01:57:55 +04005314static void pp_pre_include(char *fname)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005315{
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005316 Token *inc, *space, *name;
5317 Line *l;
5318
H. Peter Anvin734b1882002-04-30 21:01:08 +00005319 name = new_Token(NULL, TOK_INTERNAL_STRING, fname, 0);
5320 space = new_Token(name, TOK_WHITESPACE, NULL, 0);
5321 inc = new_Token(space, TOK_PREPROC_ID, "%include", 0);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005322
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005323 l = nasm_malloc(sizeof(Line));
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005324 l->next = predef;
5325 l->first = inc;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005326 l->finishes = NULL;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005327 predef = l;
5328}
5329
Cyrill Gorcunov0b78bff2012-05-07 01:57:55 +04005330static void pp_pre_define(char *definition)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005331{
5332 Token *def, *space;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005333 Line *l;
Keith Kaniosa6dfa782007-04-13 16:47:53 +00005334 char *equals;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005335
H. Peter Anvin130736c2016-02-17 20:27:41 -08005336 real_verror = nasm_set_verror(pp_verror);
H. Peter Anvin215186f2016-02-17 20:27:41 -08005337
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005338 equals = strchr(definition, '=');
H. Peter Anvin734b1882002-04-30 21:01:08 +00005339 space = new_Token(NULL, TOK_WHITESPACE, NULL, 0);
5340 def = new_Token(space, TOK_PREPROC_ID, "%define", 0);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005341 if (equals)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005342 *equals = ' ';
Keith Kaniosb7a89542007-04-12 02:40:54 +00005343 space->next = tokenize(definition);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005344 if (equals)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005345 *equals = '=';
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005346
Cyrill Gorcunov6d42e9b2015-02-08 11:07:17 +03005347 if (space->next->type != TOK_PREPROC_ID &&
5348 space->next->type != TOK_ID)
H. Peter Anvin130736c2016-02-17 20:27:41 -08005349 nasm_error(ERR_WARNING, "pre-defining non ID `%s\'\n", definition);
Cyrill Gorcunov6d42e9b2015-02-08 11:07:17 +03005350
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005351 l = nasm_malloc(sizeof(Line));
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005352 l->next = predef;
5353 l->first = def;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005354 l->finishes = NULL;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005355 predef = l;
H. Peter Anvin130736c2016-02-17 20:27:41 -08005356
5357 nasm_set_verror(real_verror);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005358}
5359
Cyrill Gorcunov0b78bff2012-05-07 01:57:55 +04005360static void pp_pre_undefine(char *definition)
H. Peter Anvin620515a2002-04-30 20:57:38 +00005361{
5362 Token *def, *space;
5363 Line *l;
5364
H. Peter Anvin734b1882002-04-30 21:01:08 +00005365 space = new_Token(NULL, TOK_WHITESPACE, NULL, 0);
5366 def = new_Token(space, TOK_PREPROC_ID, "%undef", 0);
Keith Kaniosb7a89542007-04-12 02:40:54 +00005367 space->next = tokenize(definition);
H. Peter Anvin620515a2002-04-30 20:57:38 +00005368
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005369 l = nasm_malloc(sizeof(Line));
H. Peter Anvin620515a2002-04-30 20:57:38 +00005370 l->next = predef;
5371 l->first = def;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005372 l->finishes = NULL;
H. Peter Anvin620515a2002-04-30 20:57:38 +00005373 predef = l;
5374}
5375
H. Peter Anvinf7606612016-07-13 14:23:48 -07005376static void pp_add_stdmac(macros_t *macros)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005377{
H. Peter Anvinf7606612016-07-13 14:23:48 -07005378 macros_t **mp;
5379
5380 /* Find the end of the list and avoid duplicates */
5381 for (mp = stdmacros; *mp; mp++) {
5382 if (*mp == macros)
5383 return; /* Nothing to do */
5384 }
5385
5386 nasm_assert(mp < &stdmacros[ARRAY_SIZE(stdmacros)-1]);
5387
5388 *mp = macros;
H. Peter Anvin76690a12002-04-30 20:52:49 +00005389}
5390
Cyrill Gorcunov15ce78f2017-01-06 20:21:28 +03005391static void pp_extra_stdmac(macros_t *macros)
5392{
5393 extrastdmac = macros;
5394}
5395
Keith Kaniosa5fc6462007-10-13 07:09:22 -07005396static void make_tok_num(Token * tok, int64_t val)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005397{
Cyrill Gorcunovce652742013-05-06 23:43:43 +04005398 char numbuf[32];
Keith Kaniosa5fc6462007-10-13 07:09:22 -07005399 snprintf(numbuf, sizeof(numbuf), "%"PRId64"", val);
H. Peter Anvineba20a72002-04-30 20:53:55 +00005400 tok->text = nasm_strdup(numbuf);
5401 tok->type = TOK_NUMBER;
5402}
5403
H. Peter Anvin37368952016-05-09 14:10:32 -07005404static void pp_list_one_macro(MMacro *m, int severity)
5405{
5406 if (!m)
5407 return;
5408
5409 /* We need to print the next_active list in reverse order */
5410 pp_list_one_macro(m->next_active, severity);
5411
5412 if (m->name && !m->nolist) {
H. Peter Anvin274cda82016-05-10 02:56:29 -07005413 src_set(m->xline + m->lineno, m->fname);
H. Peter Anvin37368952016-05-09 14:10:32 -07005414 nasm_error(severity, "... from macro `%s' defined here", m->name);
5415 }
5416}
5417
H. Peter Anvin4def1a82016-05-09 13:59:44 -07005418static void pp_error_list_macros(int severity)
5419{
H. Peter Anvin4def1a82016-05-09 13:59:44 -07005420 int32_t saved_line;
5421 const char *saved_fname = NULL;
5422
5423 severity |= ERR_PP_LISTMACRO | ERR_NO_SEVERITY;
H. Peter Anvin274cda82016-05-10 02:56:29 -07005424 src_get(&saved_line, &saved_fname);
H. Peter Anvin4def1a82016-05-09 13:59:44 -07005425
Cyrill Gorcunov771d04e2016-05-10 23:27:03 +03005426 if (istk)
5427 pp_list_one_macro(istk->mstk, severity);
H. Peter Anvin4def1a82016-05-09 13:59:44 -07005428
H. Peter Anvin274cda82016-05-10 02:56:29 -07005429 src_set(saved_line, saved_fname);
H. Peter Anvin4def1a82016-05-09 13:59:44 -07005430}
5431
H. Peter Anvine7469712016-02-18 02:20:59 -08005432const struct preproc_ops nasmpp = {
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07005433 pp_init,
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005434 pp_reset,
5435 pp_getline,
Cyrill Gorcunov0b78bff2012-05-07 01:57:55 +04005436 pp_cleanup,
Cyrill Gorcunov15ce78f2017-01-06 20:21:28 +03005437 pp_extra_stdmac,
Cyrill Gorcunov0b78bff2012-05-07 01:57:55 +04005438 pp_pre_define,
5439 pp_pre_undefine,
5440 pp_pre_include,
H. Peter Anvin4def1a82016-05-09 13:59:44 -07005441 pp_include_path,
5442 pp_error_list_macros,
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005443};