blob: 744747fd69ec0d3ff43426d9ee4553ad311b0282 [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) {
Cyrill Gorcunov9b7ee092017-10-22 21:42:59 +03001257 if (t->type == TOK_PREPROC_ID && t->text &&
1258 t->text[0] && t->text[1] == '!') {
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001259 char *v;
1260 char *q = t->text;
H. Peter Anvin077fb932010-07-20 14:56:30 -07001261
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001262 v = t->text + 2;
1263 if (*v == '\'' || *v == '\"' || *v == '`') {
1264 size_t len = nasm_unquote(v, NULL);
1265 size_t clen = strlen(v);
H. Peter Anvin077fb932010-07-20 14:56:30 -07001266
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001267 if (len != clen) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08001268 nasm_error(ERR_NONFATAL | ERR_PASS1,
1269 "NUL character in %%! string");
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001270 v = NULL;
1271 }
1272 }
H. Peter Anvin077fb932010-07-20 14:56:30 -07001273
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001274 if (v) {
1275 char *p = getenv(v);
1276 if (!p) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08001277 nasm_error(ERR_NONFATAL | ERR_PASS1,
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001278 "nonexistent environment variable `%s'", v);
Cyrill Gorcunovbbb7a1a2016-06-19 12:15:24 +03001279 /*
1280 * FIXME We better should investigate if accessing
1281 * ->text[1] without ->text[0] is safe enough.
1282 */
1283 t->text = nasm_zalloc(2);
1284 } else
1285 t->text = nasm_strdup(p);
Cyrill Gorcunov75004872017-07-26 01:21:16 +03001286 nasm_free(q);
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001287 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001288 }
H. Peter Anvin077fb932010-07-20 14:56:30 -07001289
H. Peter Anvine2c80182005-01-15 22:15:51 +00001290 /* Expand local macros here and not during preprocessing */
1291 if (expand_locals &&
1292 t->type == TOK_PREPROC_ID && t->text &&
1293 t->text[0] == '%' && t->text[1] == '$') {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001294 const char *q;
1295 char *p;
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04001296 Context *ctx = get_ctx(t->text, &q);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001297 if (ctx) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001298 char buffer[40];
Keith Kanios93f2e9a2007-04-14 00:10:59 +00001299 snprintf(buffer, sizeof(buffer), "..@%"PRIu32".", ctx->number);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001300 p = nasm_strcat(buffer, q);
1301 nasm_free(t->text);
1302 t->text = p;
1303 }
1304 }
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001305 if (t->type == TOK_WHITESPACE)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001306 len++;
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001307 else if (t->text)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001308 len += strlen(t->text);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001309 }
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001310
H. Peter Anvin734b1882002-04-30 21:01:08 +00001311 p = line = nasm_malloc(len + 1);
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001312
1313 list_for_each(t, tlist) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001314 if (t->type == TOK_WHITESPACE) {
H. Peter Anvinb4daadc2008-01-21 16:31:57 -08001315 *p++ = ' ';
H. Peter Anvine2c80182005-01-15 22:15:51 +00001316 } else if (t->text) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001317 q = t->text;
1318 while (*q)
1319 *p++ = *q++;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001320 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001321 }
1322 *p = '\0';
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001323
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001324 return line;
1325}
1326
1327/*
H. Peter Anvin76690a12002-04-30 20:52:49 +00001328 * A scanner, suitable for use by the expression evaluator, which
1329 * operates on a line of Tokens. Expects a pointer to a pointer to
1330 * the first token in the line to be passed in as its private_data
1331 * field.
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001332 *
1333 * FIX: This really needs to be unified with stdscan.
H. Peter Anvin76690a12002-04-30 20:52:49 +00001334 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001335static int ppscan(void *private_data, struct tokenval *tokval)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001336{
H. Peter Anvin76690a12002-04-30 20:52:49 +00001337 Token **tlineptr = private_data;
1338 Token *tline;
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001339 char ourcopy[MAX_KEYWORD+1], *p, *r, *s;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001340
H. Peter Anvine2c80182005-01-15 22:15:51 +00001341 do {
1342 tline = *tlineptr;
1343 *tlineptr = tline ? tline->next : NULL;
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04001344 } while (tline && (tline->type == TOK_WHITESPACE ||
1345 tline->type == TOK_COMMENT));
H. Peter Anvin76690a12002-04-30 20:52:49 +00001346
1347 if (!tline)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001348 return tokval->t_type = TOKEN_EOS;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001349
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001350 tokval->t_charptr = tline->text;
1351
H. Peter Anvin76690a12002-04-30 20:52:49 +00001352 if (tline->text[0] == '$' && !tline->text[1])
H. Peter Anvine2c80182005-01-15 22:15:51 +00001353 return tokval->t_type = TOKEN_HERE;
H. Peter Anvin7cf897e2002-05-30 21:30:33 +00001354 if (tline->text[0] == '$' && tline->text[1] == '$' && !tline->text[2])
H. Peter Anvine2c80182005-01-15 22:15:51 +00001355 return tokval->t_type = TOKEN_BASE;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001356
H. Peter Anvine2c80182005-01-15 22:15:51 +00001357 if (tline->type == TOK_ID) {
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001358 p = tokval->t_charptr = tline->text;
1359 if (p[0] == '$') {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001360 tokval->t_charptr++;
1361 return tokval->t_type = TOKEN_ID;
1362 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00001363
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001364 for (r = p, s = ourcopy; *r; r++) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001365 if (r >= p+MAX_KEYWORD)
1366 return tokval->t_type = TOKEN_ID; /* Not a keyword */
H. Peter Anvinac8f8fc2008-06-11 15:49:41 -07001367 *s++ = nasm_tolower(*r);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001368 }
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001369 *s = '\0';
1370 /* right, so we have an identifier sitting in temp storage. now,
1371 * is it actually a register or instruction name, or what? */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001372 return nasm_token_hash(ourcopy, tokval);
H. Peter Anvin76690a12002-04-30 20:52:49 +00001373 }
1374
H. Peter Anvine2c80182005-01-15 22:15:51 +00001375 if (tline->type == TOK_NUMBER) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001376 bool rn_error;
1377 tokval->t_integer = readnum(tline->text, &rn_error);
1378 tokval->t_charptr = tline->text;
1379 if (rn_error)
1380 return tokval->t_type = TOKEN_ERRNUM;
1381 else
1382 return tokval->t_type = TOKEN_NUM;
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001383 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00001384
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001385 if (tline->type == TOK_FLOAT) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001386 return tokval->t_type = TOKEN_FLOAT;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001387 }
1388
H. Peter Anvine2c80182005-01-15 22:15:51 +00001389 if (tline->type == TOK_STRING) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001390 char bq, *ep;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001391
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001392 bq = tline->text[0];
H. Peter Anvin11627042008-06-09 20:45:19 -07001393 tokval->t_charptr = tline->text;
1394 tokval->t_inttwo = nasm_unquote(tline->text, &ep);
H. Peter Anvind2456592008-06-19 15:04:18 -07001395
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001396 if (ep[0] != bq || ep[1] != '\0')
1397 return tokval->t_type = TOKEN_ERRSTR;
1398 else
1399 return tokval->t_type = TOKEN_STR;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001400 }
1401
H. Peter Anvine2c80182005-01-15 22:15:51 +00001402 if (tline->type == TOK_OTHER) {
1403 if (!strcmp(tline->text, "<<"))
1404 return tokval->t_type = TOKEN_SHL;
1405 if (!strcmp(tline->text, ">>"))
1406 return tokval->t_type = TOKEN_SHR;
1407 if (!strcmp(tline->text, "//"))
1408 return tokval->t_type = TOKEN_SDIV;
1409 if (!strcmp(tline->text, "%%"))
1410 return tokval->t_type = TOKEN_SMOD;
1411 if (!strcmp(tline->text, "=="))
1412 return tokval->t_type = TOKEN_EQ;
1413 if (!strcmp(tline->text, "<>"))
1414 return tokval->t_type = TOKEN_NE;
1415 if (!strcmp(tline->text, "!="))
1416 return tokval->t_type = TOKEN_NE;
1417 if (!strcmp(tline->text, "<="))
1418 return tokval->t_type = TOKEN_LE;
1419 if (!strcmp(tline->text, ">="))
1420 return tokval->t_type = TOKEN_GE;
1421 if (!strcmp(tline->text, "&&"))
1422 return tokval->t_type = TOKEN_DBL_AND;
1423 if (!strcmp(tline->text, "^^"))
1424 return tokval->t_type = TOKEN_DBL_XOR;
1425 if (!strcmp(tline->text, "||"))
1426 return tokval->t_type = TOKEN_DBL_OR;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001427 }
1428
1429 /*
1430 * We have no other options: just return the first character of
1431 * the token text.
1432 */
1433 return tokval->t_type = tline->text[0];
1434}
1435
1436/*
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001437 * Compare a string to the name of an existing macro; this is a
1438 * simple wrapper which calls either strcmp or nasm_stricmp
1439 * depending on the value of the `casesense' parameter.
1440 */
H. Peter Anvin4db5a162007-10-11 13:42:09 -07001441static int mstrcmp(const char *p, const char *q, bool casesense)
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001442{
H. Peter Anvin734b1882002-04-30 21:01:08 +00001443 return casesense ? strcmp(p, q) : nasm_stricmp(p, q);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001444}
1445
1446/*
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07001447 * Compare a string to the name of an existing macro; this is a
1448 * simple wrapper which calls either strcmp or nasm_stricmp
1449 * depending on the value of the `casesense' parameter.
1450 */
1451static int mmemcmp(const char *p, const char *q, size_t l, bool casesense)
1452{
1453 return casesense ? memcmp(p, q, l) : nasm_memicmp(p, q, l);
1454}
1455
1456/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001457 * Return the Context structure associated with a %$ token. Return
1458 * NULL, having _already_ reported an error condition, if the
1459 * context stack isn't deep enough for the supplied number of $
1460 * signs.
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001461 *
1462 * If "namep" is non-NULL, set it to the pointer to the macro name
1463 * tail, i.e. the part beyond %$...
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001464 */
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04001465static Context *get_ctx(const char *name, const char **namep)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001466{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001467 Context *ctx;
1468 int i;
1469
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001470 if (namep)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001471 *namep = name;
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001472
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001473 if (!name || name[0] != '%' || name[1] != '$')
H. Peter Anvine2c80182005-01-15 22:15:51 +00001474 return NULL;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001475
H. Peter Anvine2c80182005-01-15 22:15:51 +00001476 if (!cstk) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08001477 nasm_error(ERR_NONFATAL, "`%s': context stack is empty", name);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001478 return NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001479 }
1480
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001481 name += 2;
1482 ctx = cstk;
1483 i = 0;
1484 while (ctx && *name == '$') {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001485 name++;
1486 i++;
1487 ctx = ctx->next;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001488 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001489 if (!ctx) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08001490 nasm_error(ERR_NONFATAL, "`%s': context stack is only"
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001491 " %d level%s deep", name, i, (i == 1 ? "" : "s"));
H. Peter Anvine2c80182005-01-15 22:15:51 +00001492 return NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001493 }
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001494
1495 if (namep)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001496 *namep = name;
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001497
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04001498 return ctx;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001499}
1500
1501/*
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001502 * Open an include file. This routine must always return a valid
1503 * file pointer if it returns - it's responsible for throwing an
1504 * ERR_FATAL and bombing out completely if not. It should also try
1505 * the include path one by one until it finds the file or reaches
1506 * the end of the path.
H. Peter Anvind81a2352016-09-21 14:03:18 -07001507 *
1508 * Note: for INC_PROBE the function returns NULL at all times;
1509 * instead look for the
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001510 */
H. Peter Anvind81a2352016-09-21 14:03:18 -07001511enum incopen_mode {
1512 INC_NEEDED, /* File must exist */
1513 INC_OPTIONAL, /* Missing is OK */
1514 INC_PROBE /* Only an existence probe */
1515};
1516
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001517/* This is conducts a full pathname search */
1518static FILE *inc_fopen_search(const char *file, StrList **slpath,
1519 enum incopen_mode omode, enum file_flags fmode)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001520{
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001521 FILE *fp;
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001522 char *prefix = "";
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001523 const IncPath *ip = ipath;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001524 int len = strlen(file);
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001525 size_t prefix_len = 0;
1526 StrList *sl;
Jim Kukunas65a8afc2016-06-13 16:00:42 -04001527 size_t path_len;
H. Peter Anvind81a2352016-09-21 14:03:18 -07001528 bool found;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001529
H. Peter Anvine2c80182005-01-15 22:15:51 +00001530 while (1) {
Jim Kukunas65a8afc2016-06-13 16:00:42 -04001531 path_len = prefix_len + len + 1;
1532
1533 sl = nasm_malloc(path_len + sizeof sl->next);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001534 memcpy(sl->str, prefix, prefix_len);
1535 memcpy(sl->str+prefix_len, file, len+1);
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001536 sl->next = NULL;
Jim Kukunas65a8afc2016-06-13 16:00:42 -04001537
H. Peter Anvind81a2352016-09-21 14:03:18 -07001538 if (omode == INC_PROBE) {
1539 fp = NULL;
1540 found = nasm_file_exists(sl->str);
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001541 } else {
H. Peter Anvind81a2352016-09-21 14:03:18 -07001542 fp = nasm_open_read(sl->str, fmode);
1543 found = (fp != NULL);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001544 }
H. Peter Anvind81a2352016-09-21 14:03:18 -07001545 if (found) {
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001546 *slpath = sl;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001547 return fp;
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001548 }
Jim Kukunas65a8afc2016-06-13 16:00:42 -04001549
H. Peter Anvind81a2352016-09-21 14:03:18 -07001550 nasm_free(sl);
Jim Kukunas65a8afc2016-06-13 16:00:42 -04001551
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001552 if (!ip)
1553 return NULL;
1554
1555 prefix = ip->path;
1556 prefix_len = strlen(prefix);
1557 ip = ip->next;
1558 }
1559}
1560
1561/*
1562 * Open a file, or test for the presence of one (depending on omode),
1563 * considering the include path.
1564 */
1565static FILE *inc_fopen(const char *file,
H. Peter Anvin9924d1e2016-10-04 00:59:39 -07001566 StrList **dhead,
H. Peter Anvinccad6f92016-10-04 00:34:35 -07001567 const char **found_path,
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001568 enum incopen_mode omode,
1569 enum file_flags fmode)
1570{
1571 StrList *sl;
1572 struct hash_insert hi;
1573 void **hp;
1574 char *path;
1575 FILE *fp = NULL;
1576
1577 hp = hash_find(&FileHash, file, &hi);
1578 if (hp) {
1579 path = *hp;
Martin Storsjöf283c8f2017-08-13 17:28:46 +03001580 if (path || omode != INC_NEEDED) {
H. Peter Anvin97fda4c2017-08-16 15:52:51 -07001581 nasm_add_string_to_strlist(dhead, path ? path : file);
Martin Storsjöf283c8f2017-08-13 17:28:46 +03001582 }
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001583 } else {
1584 /* Need to do the actual path search */
1585 size_t file_len;
1586
1587 sl = NULL;
1588 fp = inc_fopen_search(file, &sl, omode, fmode);
1589
1590 file_len = strlen(file);
1591
1592 if (!sl) {
1593 /* Store negative result for this file */
1594 sl = nasm_malloc(file_len + 1 + sizeof sl->next);
1595 memcpy(sl->str, file, file_len+1);
1596 sl->next = NULL;
1597 file = sl->str;
1598 path = NULL;
1599 } else {
1600 path = sl->str;
1601 file = strchr(path, '\0') - file_len;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001602 }
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001603
1604 hash_add(&hi, file, path); /* Positive or negative result */
1605
H. Peter Anvin9924d1e2016-10-04 00:59:39 -07001606 /*
1607 * Add file to dependency path. The in_list() is needed
1608 * in case the file was already added with %depend.
1609 */
1610 if (path || omode != INC_NEEDED)
H. Peter Anvin436e3672016-10-04 01:12:28 -07001611 nasm_add_to_strlist(dhead, sl);
H. Peter Anvineba20a72002-04-30 20:53:55 +00001612 }
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001613
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001614 if (!path) {
1615 if (omode == INC_NEEDED)
1616 nasm_fatal(0, "unable to open include file `%s'", file);
1617
1618 if (found_path)
1619 *found_path = NULL;
1620
1621 return NULL;
1622 }
1623
1624 if (!fp && omode != INC_PROBE)
Cyrill Gorcunov4ff8c632017-01-06 00:36:23 +03001625 fp = nasm_open_read(path, fmode);
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001626
1627 if (found_path)
H. Peter Anvinccad6f92016-10-04 00:34:35 -07001628 *found_path = path;
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001629
1630 return fp;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001631}
1632
1633/*
Fabian Giesen0bbc38d2016-04-28 13:48:14 -07001634 * Opens an include or input file. Public version, for use by modules
1635 * that get a file:lineno pair and need to look at the file again
1636 * (e.g. the CodeView debug backend). Returns NULL on failure.
1637 */
H. Peter Anvin3e83cec2016-05-25 04:28:46 -07001638FILE *pp_input_fopen(const char *filename, enum file_flags mode)
Fabian Giesen0bbc38d2016-04-28 13:48:14 -07001639{
H. Peter Anvin9924d1e2016-10-04 00:59:39 -07001640 return inc_fopen(filename, NULL, NULL, INC_OPTIONAL, mode);
Fabian Giesen0bbc38d2016-04-28 13:48:14 -07001641}
1642
1643/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001644 * Determine if we should warn on defining a single-line macro of
H. Peter Anvinef7468f2002-04-30 20:57:59 +00001645 * name `name', with `nparam' parameters. If nparam is 0 or -1, will
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001646 * return true if _any_ single-line macro of that name is defined.
1647 * Otherwise, will return true if a single-line macro with either
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001648 * `nparam' or no parameters is defined.
1649 *
1650 * If a macro with precisely the right number of parameters is
H. Peter Anvinef7468f2002-04-30 20:57:59 +00001651 * defined, or nparam is -1, the address of the definition structure
1652 * will be returned in `defn'; otherwise NULL will be returned. If `defn'
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001653 * is NULL, no action will be taken regarding its contents, and no
1654 * error will occur.
1655 *
1656 * Note that this is also called with nparam zero to resolve
1657 * `ifdef'.
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001658 *
1659 * If you already know which context macro belongs to, you can pass
1660 * the context pointer as first parameter; if you won't but name begins
1661 * with %$ the context will be automatically computed. If all_contexts
1662 * is true, macro will be searched in outer contexts as well.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001663 */
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07001664static bool
H. Peter Anvinb2a5fda2008-06-19 21:42:42 -07001665smacro_defined(Context * ctx, const char *name, int nparam, SMacro ** defn,
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07001666 bool nocase)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001667{
H. Peter Anvin166c2472008-05-28 12:28:58 -07001668 struct hash_table *smtbl;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001669 SMacro *m;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001670
H. Peter Anvin97a23472007-09-16 17:57:25 -07001671 if (ctx) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001672 smtbl = &ctx->localmac;
H. Peter Anvin97a23472007-09-16 17:57:25 -07001673 } else if (name[0] == '%' && name[1] == '$') {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001674 if (cstk)
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04001675 ctx = get_ctx(name, &name);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001676 if (!ctx)
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001677 return false; /* got to return _something_ */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001678 smtbl = &ctx->localmac;
H. Peter Anvin97a23472007-09-16 17:57:25 -07001679 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001680 smtbl = &smacros;
H. Peter Anvin97a23472007-09-16 17:57:25 -07001681 }
H. Peter Anvin166c2472008-05-28 12:28:58 -07001682 m = (SMacro *) hash_findix(smtbl, name);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001683
H. Peter Anvine2c80182005-01-15 22:15:51 +00001684 while (m) {
1685 if (!mstrcmp(m->name, name, m->casesense && nocase) &&
Charles Crayne192d5b52007-10-18 19:02:42 -07001686 (nparam <= 0 || m->nparam == 0 || nparam == (int) m->nparam)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001687 if (defn) {
Charles Crayne192d5b52007-10-18 19:02:42 -07001688 if (nparam == (int) m->nparam || nparam == -1)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001689 *defn = m;
1690 else
1691 *defn = NULL;
1692 }
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001693 return true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001694 }
1695 m = m->next;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001696 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001697
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001698 return false;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001699}
1700
1701/*
1702 * Count and mark off the parameters in a multi-line macro call.
1703 * This is called both from within the multi-line macro expansion
1704 * code, and also to mark off the default parameters when provided
1705 * in a %macro definition line.
1706 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001707static void count_mmac_params(Token * t, int *nparam, Token *** params)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001708{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001709 int paramsize, brace;
1710
1711 *nparam = paramsize = 0;
1712 *params = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001713 while (t) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001714 /* +1: we need space for the final NULL */
H. Peter Anvin91fb6f12008-09-01 10:56:33 -07001715 if (*nparam+1 >= paramsize) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001716 paramsize += PARAM_DELTA;
1717 *params = nasm_realloc(*params, sizeof(**params) * paramsize);
1718 }
1719 skip_white_(t);
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08001720 brace = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001721 if (tok_is_(t, "{"))
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08001722 brace++;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001723 (*params)[(*nparam)++] = t;
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08001724 if (brace) {
1725 while (brace && (t = t->next) != NULL) {
1726 if (tok_is_(t, "{"))
1727 brace++;
1728 else if (tok_is_(t, "}"))
1729 brace--;
1730 }
1731
1732 if (t) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001733 /*
1734 * Now we've found the closing brace, look further
1735 * for the comma.
1736 */
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08001737 t = t->next;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001738 skip_white_(t);
1739 if (tok_isnt_(t, ",")) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08001740 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00001741 "braces do not enclose all of macro parameter");
1742 while (tok_isnt_(t, ","))
1743 t = t->next;
1744 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001745 }
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08001746 } else {
1747 while (tok_isnt_(t, ","))
1748 t = t->next;
1749 }
1750 if (t) { /* got a comma/brace */
1751 t = t->next; /* eat the comma */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001752 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001753 }
1754}
1755
1756/*
H. Peter Anvin76690a12002-04-30 20:52:49 +00001757 * Determine whether one of the various `if' conditions is true or
1758 * not.
1759 *
1760 * We must free the tline we get passed.
1761 */
H. Peter Anvin70055962007-10-11 00:05:31 -07001762static bool if_condition(Token * tline, enum preproc_token ct)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001763{
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001764 enum pp_conditional i = PP_COND(ct);
H. Peter Anvin70055962007-10-11 00:05:31 -07001765 bool j;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001766 Token *t, *tt, **tptr, *origline;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001767 struct tokenval tokval;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001768 expr *evalresult;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001769 enum pp_token_type needtype;
H. Peter Anvin077fb932010-07-20 14:56:30 -07001770 char *p;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001771
1772 origline = tline;
1773
H. Peter Anvine2c80182005-01-15 22:15:51 +00001774 switch (i) {
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001775 case PPC_IFCTX:
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001776 j = false; /* have we matched yet? */
Victor van den Elzen0e857f12008-07-23 13:21:29 +02001777 while (true) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001778 skip_white_(tline);
Victor van den Elzen0e857f12008-07-23 13:21:29 +02001779 if (!tline)
1780 break;
1781 if (tline->type != TOK_ID) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08001782 nasm_error(ERR_NONFATAL,
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001783 "`%s' expects context identifiers", pp_directives[ct]);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001784 free_tlist(origline);
1785 return -1;
1786 }
Victor van den Elzen0e857f12008-07-23 13:21:29 +02001787 if (cstk && cstk->name && !nasm_stricmp(tline->text, cstk->name))
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001788 j = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001789 tline = tline->next;
1790 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001791 break;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001792
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001793 case PPC_IFDEF:
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001794 j = false; /* have we matched yet? */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001795 while (tline) {
1796 skip_white_(tline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001797 if (!tline || (tline->type != TOK_ID &&
1798 (tline->type != TOK_PREPROC_ID ||
1799 tline->text[1] != '$'))) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08001800 nasm_error(ERR_NONFATAL,
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001801 "`%s' expects macro identifiers", pp_directives[ct]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001802 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001803 }
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07001804 if (smacro_defined(NULL, tline->text, 0, NULL, true))
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001805 j = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001806 tline = tline->next;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001807 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001808 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001809
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001810 case PPC_IFENV:
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001811 tline = expand_smacro(tline);
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001812 j = false; /* have we matched yet? */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001813 while (tline) {
1814 skip_white_(tline);
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001815 if (!tline || (tline->type != TOK_ID &&
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001816 tline->type != TOK_STRING &&
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001817 (tline->type != TOK_PREPROC_ID ||
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001818 tline->text[1] != '!'))) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08001819 nasm_error(ERR_NONFATAL,
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001820 "`%s' expects environment variable names",
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001821 pp_directives[ct]);
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001822 goto fail;
1823 }
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001824 p = tline->text;
1825 if (tline->type == TOK_PREPROC_ID)
1826 p += 2; /* Skip leading %! */
1827 if (*p == '\'' || *p == '\"' || *p == '`')
1828 nasm_unquote_cstr(p, ct);
1829 if (getenv(p))
1830 j = true;
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001831 tline = tline->next;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001832 }
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001833 break;
1834
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001835 case PPC_IFIDN:
1836 case PPC_IFIDNI:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001837 tline = expand_smacro(tline);
1838 t = tt = tline;
1839 while (tok_isnt_(tt, ","))
1840 tt = tt->next;
1841 if (!tt) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08001842 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00001843 "`%s' expects two comma-separated arguments",
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001844 pp_directives[ct]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001845 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001846 }
1847 tt = tt->next;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001848 j = true; /* assume equality unless proved not */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001849 while ((t->type != TOK_OTHER || strcmp(t->text, ",")) && tt) {
1850 if (tt->type == TOK_OTHER && !strcmp(tt->text, ",")) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08001851 nasm_error(ERR_NONFATAL, "`%s': more than one comma on line",
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001852 pp_directives[ct]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001853 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001854 }
1855 if (t->type == TOK_WHITESPACE) {
1856 t = t->next;
1857 continue;
1858 }
1859 if (tt->type == TOK_WHITESPACE) {
1860 tt = tt->next;
1861 continue;
1862 }
1863 if (tt->type != t->type) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001864 j = false; /* found mismatching tokens */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001865 break;
1866 }
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07001867 /* When comparing strings, need to unquote them first */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001868 if (t->type == TOK_STRING) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001869 size_t l1 = nasm_unquote(t->text, NULL);
1870 size_t l2 = nasm_unquote(tt->text, NULL);
H. Peter Anvind2456592008-06-19 15:04:18 -07001871
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001872 if (l1 != l2) {
1873 j = false;
1874 break;
1875 }
1876 if (mmemcmp(t->text, tt->text, l1, i == PPC_IFIDN)) {
1877 j = false;
1878 break;
1879 }
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07001880 } else if (mstrcmp(tt->text, t->text, i == PPC_IFIDN) != 0) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001881 j = false; /* found mismatching tokens */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001882 break;
1883 }
Nickolay Yurchenkof3b3ce22003-09-21 20:38:43 +00001884
H. Peter Anvine2c80182005-01-15 22:15:51 +00001885 t = t->next;
1886 tt = tt->next;
1887 }
1888 if ((t->type != TOK_OTHER || strcmp(t->text, ",")) || tt)
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001889 j = false; /* trailing gunk on one end or other */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001890 break;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001891
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001892 case PPC_IFMACRO:
H. Peter Anvin89cee572009-07-15 09:16:54 -04001893 {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001894 bool found = false;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001895 MMacro searching, *mmac;
H. Peter Anvin65747262002-05-07 00:10:05 +00001896
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001897 skip_white_(tline);
1898 tline = expand_id(tline);
1899 if (!tok_type_(tline, TOK_ID)) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08001900 nasm_error(ERR_NONFATAL,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001901 "`%s' expects a macro name", pp_directives[ct]);
1902 goto fail;
1903 }
1904 searching.name = nasm_strdup(tline->text);
1905 searching.casesense = true;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001906 searching.plus = false;
1907 searching.nolist = false;
1908 searching.in_progress = 0;
1909 searching.max_depth = 0;
1910 searching.rep_nest = NULL;
1911 searching.nparam_min = 0;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001912 searching.nparam_max = INT_MAX;
1913 tline = expand_smacro(tline->next);
1914 skip_white_(tline);
1915 if (!tline) {
1916 } else if (!tok_type_(tline, TOK_NUMBER)) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08001917 nasm_error(ERR_NONFATAL,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001918 "`%s' expects a parameter count or nothing",
1919 pp_directives[ct]);
1920 } else {
1921 searching.nparam_min = searching.nparam_max =
1922 readnum(tline->text, &j);
1923 if (j)
H. Peter Anvin130736c2016-02-17 20:27:41 -08001924 nasm_error(ERR_NONFATAL,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001925 "unable to parse parameter count `%s'",
1926 tline->text);
1927 }
1928 if (tline && tok_is_(tline->next, "-")) {
1929 tline = tline->next->next;
1930 if (tok_is_(tline, "*"))
1931 searching.nparam_max = INT_MAX;
1932 else if (!tok_type_(tline, TOK_NUMBER))
H. Peter Anvin130736c2016-02-17 20:27:41 -08001933 nasm_error(ERR_NONFATAL,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001934 "`%s' expects a parameter count after `-'",
1935 pp_directives[ct]);
1936 else {
1937 searching.nparam_max = readnum(tline->text, &j);
1938 if (j)
H. Peter Anvin130736c2016-02-17 20:27:41 -08001939 nasm_error(ERR_NONFATAL,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001940 "unable to parse parameter count `%s'",
1941 tline->text);
Cyrill Gorcunovc9244ea2017-10-22 15:25:48 +03001942 if (searching.nparam_min > searching.nparam_max) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08001943 nasm_error(ERR_NONFATAL,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001944 "minimum parameter count exceeds maximum");
Cyrill Gorcunovc9244ea2017-10-22 15:25:48 +03001945 searching.nparam_max = searching.nparam_min;
1946 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001947 }
1948 }
1949 if (tline && tok_is_(tline->next, "+")) {
1950 tline = tline->next;
1951 searching.plus = true;
1952 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001953 mmac = (MMacro *) hash_findix(&mmacros, searching.name);
1954 while (mmac) {
1955 if (!strcmp(mmac->name, searching.name) &&
1956 (mmac->nparam_min <= searching.nparam_max
1957 || searching.plus)
1958 && (searching.nparam_min <= mmac->nparam_max
1959 || mmac->plus)) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001960 found = true;
1961 break;
1962 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001963 mmac = mmac->next;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001964 }
1965 if (tline && tline->next)
H. Peter Anvin130736c2016-02-17 20:27:41 -08001966 nasm_error(ERR_WARNING|ERR_PASS1,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001967 "trailing garbage after %%ifmacro ignored");
1968 nasm_free(searching.name);
1969 j = found;
1970 break;
H. Peter Anvin89cee572009-07-15 09:16:54 -04001971 }
H. Peter Anvin65747262002-05-07 00:10:05 +00001972
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001973 case PPC_IFID:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001974 needtype = TOK_ID;
1975 goto iftype;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001976 case PPC_IFNUM:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001977 needtype = TOK_NUMBER;
1978 goto iftype;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001979 case PPC_IFSTR:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001980 needtype = TOK_STRING;
1981 goto iftype;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001982
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001983iftype:
1984 t = tline = expand_smacro(tline);
H. Peter Anvind85d2502008-05-04 17:53:31 -07001985
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001986 while (tok_type_(t, TOK_WHITESPACE) ||
1987 (needtype == TOK_NUMBER &&
1988 tok_type_(t, TOK_OTHER) &&
1989 (t->text[0] == '-' || t->text[0] == '+') &&
1990 !t->text[1]))
1991 t = t->next;
H. Peter Anvind85d2502008-05-04 17:53:31 -07001992
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001993 j = tok_type_(t, needtype);
1994 break;
H. Peter Anvincbf768d2008-02-16 16:41:25 -08001995
1996 case PPC_IFTOKEN:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001997 t = tline = expand_smacro(tline);
1998 while (tok_type_(t, TOK_WHITESPACE))
1999 t = t->next;
H. Peter Anvincbf768d2008-02-16 16:41:25 -08002000
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002001 j = false;
2002 if (t) {
2003 t = t->next; /* Skip the actual token */
2004 while (tok_type_(t, TOK_WHITESPACE))
2005 t = t->next;
2006 j = !t; /* Should be nothing left */
2007 }
2008 break;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002009
H. Peter Anvin134b9462008-02-16 17:01:40 -08002010 case PPC_IFEMPTY:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002011 t = tline = expand_smacro(tline);
2012 while (tok_type_(t, TOK_WHITESPACE))
2013 t = t->next;
H. Peter Anvin134b9462008-02-16 17:01:40 -08002014
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002015 j = !t; /* Should be empty */
2016 break;
H. Peter Anvin134b9462008-02-16 17:01:40 -08002017
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002018 case PPC_IF:
H. Peter Anvine2c80182005-01-15 22:15:51 +00002019 t = tline = expand_smacro(tline);
2020 tptr = &t;
2021 tokval.t_type = TOKEN_INVALID;
2022 evalresult = evaluate(ppscan, tptr, &tokval,
H. Peter Anvin130736c2016-02-17 20:27:41 -08002023 NULL, pass | CRITICAL, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002024 if (!evalresult)
2025 return -1;
2026 if (tokval.t_type)
H. Peter Anvin130736c2016-02-17 20:27:41 -08002027 nasm_error(ERR_WARNING|ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +00002028 "trailing garbage after expression ignored");
2029 if (!is_simple(evalresult)) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002030 nasm_error(ERR_NONFATAL,
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002031 "non-constant value given to `%s'", pp_directives[ct]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002032 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002033 }
Chuck Crayne60ae75d2007-05-02 01:59:16 +00002034 j = reloc_value(evalresult) != 0;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002035 break;
H. Peter Anvin95e28822007-09-12 04:20:08 +00002036
H. Peter Anvine2c80182005-01-15 22:15:51 +00002037 default:
H. Peter Anvin130736c2016-02-17 20:27:41 -08002038 nasm_error(ERR_FATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00002039 "preprocessor directive `%s' not yet implemented",
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002040 pp_directives[ct]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002041 goto fail;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002042 }
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002043
2044 free_tlist(origline);
2045 return j ^ PP_NEGATIVE(ct);
H. Peter Anvin70653092007-10-19 14:42:29 -07002046
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002047fail:
2048 free_tlist(origline);
2049 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002050}
2051
2052/*
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002053 * Common code for defining an smacro
2054 */
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08002055static bool define_smacro(Context *ctx, const char *mname, bool casesense,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002056 int nparam, Token *expansion)
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002057{
2058 SMacro *smac, **smhead;
H. Peter Anvin166c2472008-05-28 12:28:58 -07002059 struct hash_table *smtbl;
H. Peter Anvin70653092007-10-19 14:42:29 -07002060
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002061 if (smacro_defined(ctx, mname, nparam, &smac, casesense)) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002062 if (!smac) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002063 nasm_error(ERR_WARNING|ERR_PASS1,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002064 "single-line macro `%s' defined both with and"
2065 " without parameters", mname);
2066 /*
2067 * Some instances of the old code considered this a failure,
2068 * some others didn't. What is the right thing to do here?
2069 */
2070 free_tlist(expansion);
2071 return false; /* Failure */
2072 } else {
2073 /*
2074 * We're redefining, so we have to take over an
2075 * existing SMacro structure. This means freeing
2076 * what was already in it.
2077 */
2078 nasm_free(smac->name);
2079 free_tlist(smac->expansion);
2080 }
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002081 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002082 smtbl = ctx ? &ctx->localmac : &smacros;
2083 smhead = (SMacro **) hash_findi_add(smtbl, mname);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002084 smac = nasm_malloc(sizeof(SMacro));
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002085 smac->next = *smhead;
2086 *smhead = smac;
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002087 }
2088 smac->name = nasm_strdup(mname);
2089 smac->casesense = casesense;
2090 smac->nparam = nparam;
2091 smac->expansion = expansion;
2092 smac->in_progress = false;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002093 return true; /* Success */
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002094}
2095
2096/*
2097 * Undefine an smacro
2098 */
2099static void undef_smacro(Context *ctx, const char *mname)
2100{
2101 SMacro **smhead, *s, **sp;
H. Peter Anvin166c2472008-05-28 12:28:58 -07002102 struct hash_table *smtbl;
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002103
H. Peter Anvin166c2472008-05-28 12:28:58 -07002104 smtbl = ctx ? &ctx->localmac : &smacros;
2105 smhead = (SMacro **)hash_findi(smtbl, mname, NULL);
H. Peter Anvin70653092007-10-19 14:42:29 -07002106
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002107 if (smhead) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002108 /*
2109 * We now have a macro name... go hunt for it.
2110 */
2111 sp = smhead;
2112 while ((s = *sp) != NULL) {
2113 if (!mstrcmp(s->name, mname, s->casesense)) {
2114 *sp = s->next;
2115 nasm_free(s->name);
2116 free_tlist(s->expansion);
2117 nasm_free(s);
2118 } else {
2119 sp = &s->next;
2120 }
2121 }
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002122 }
2123}
2124
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002125/*
H. Peter Anvina26433d2008-07-16 14:40:01 -07002126 * Parse a mmacro specification.
2127 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002128static bool parse_mmacro_spec(Token *tline, MMacro *def, const char *directive)
H. Peter Anvina26433d2008-07-16 14:40:01 -07002129{
2130 bool err;
2131
2132 tline = tline->next;
2133 skip_white_(tline);
2134 tline = expand_id(tline);
2135 if (!tok_type_(tline, TOK_ID)) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002136 nasm_error(ERR_NONFATAL, "`%s' expects a macro name", directive);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002137 return false;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002138 }
Victor van den Elzenb916ede2008-07-23 15:14:22 +02002139
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002140 def->prev = NULL;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002141 def->name = nasm_strdup(tline->text);
2142 def->plus = false;
2143 def->nolist = false;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002144 def->in_progress = 0;
2145 def->rep_nest = NULL;
Victor van den Elzenb916ede2008-07-23 15:14:22 +02002146 def->nparam_min = 0;
2147 def->nparam_max = 0;
2148
H. Peter Anvina26433d2008-07-16 14:40:01 -07002149 tline = expand_smacro(tline->next);
2150 skip_white_(tline);
2151 if (!tok_type_(tline, TOK_NUMBER)) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002152 nasm_error(ERR_NONFATAL, "`%s' expects a parameter count", directive);
H. Peter Anvina26433d2008-07-16 14:40:01 -07002153 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002154 def->nparam_min = def->nparam_max =
2155 readnum(tline->text, &err);
2156 if (err)
H. Peter Anvin130736c2016-02-17 20:27:41 -08002157 nasm_error(ERR_NONFATAL,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002158 "unable to parse parameter count `%s'", tline->text);
H. Peter Anvina26433d2008-07-16 14:40:01 -07002159 }
2160 if (tline && tok_is_(tline->next, "-")) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002161 tline = tline->next->next;
2162 if (tok_is_(tline, "*")) {
2163 def->nparam_max = INT_MAX;
2164 } else if (!tok_type_(tline, TOK_NUMBER)) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002165 nasm_error(ERR_NONFATAL,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002166 "`%s' expects a parameter count after `-'", directive);
2167 } else {
2168 def->nparam_max = readnum(tline->text, &err);
2169 if (err) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002170 nasm_error(ERR_NONFATAL, "unable to parse parameter count `%s'",
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002171 tline->text);
2172 }
2173 if (def->nparam_min > def->nparam_max) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002174 nasm_error(ERR_NONFATAL, "minimum parameter count exceeds maximum");
Cyrill Gorcunovc9244ea2017-10-22 15:25:48 +03002175 def->nparam_max = def->nparam_min;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002176 }
2177 }
H. Peter Anvina26433d2008-07-16 14:40:01 -07002178 }
2179 if (tline && tok_is_(tline->next, "+")) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002180 tline = tline->next;
2181 def->plus = true;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002182 }
2183 if (tline && tok_type_(tline->next, TOK_ID) &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002184 !nasm_stricmp(tline->next->text, ".nolist")) {
2185 tline = tline->next;
2186 def->nolist = true;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002187 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002188
H. Peter Anvina26433d2008-07-16 14:40:01 -07002189 /*
2190 * Handle default parameters.
2191 */
2192 if (tline && tline->next) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002193 def->dlist = tline->next;
2194 tline->next = NULL;
2195 count_mmac_params(def->dlist, &def->ndefs, &def->defaults);
H. Peter Anvina26433d2008-07-16 14:40:01 -07002196 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002197 def->dlist = NULL;
2198 def->defaults = NULL;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002199 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002200 def->expansion = NULL;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002201
H. Peter Anvin89cee572009-07-15 09:16:54 -04002202 if (def->defaults && def->ndefs > def->nparam_max - def->nparam_min &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002203 !def->plus)
H. Peter Anvin130736c2016-02-17 20:27:41 -08002204 nasm_error(ERR_WARNING|ERR_PASS1|ERR_WARN_MDP,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002205 "too many default macro parameters");
Victor van den Elzenb916ede2008-07-23 15:14:22 +02002206
H. Peter Anvina26433d2008-07-16 14:40:01 -07002207 return true;
2208}
2209
2210
2211/*
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002212 * Decode a size directive
2213 */
2214static int parse_size(const char *str) {
2215 static const char *size_names[] =
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002216 { "byte", "dword", "oword", "qword", "tword", "word", "yword" };
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002217 static const int sizes[] =
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002218 { 0, 1, 4, 16, 8, 10, 2, 32 };
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002219
Cyrill Gorcunova7319242010-06-03 22:04:36 +04002220 return sizes[bsii(str, size_names, ARRAY_SIZE(size_names))+1];
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002221}
2222
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07002223/*
2224 * Process a preprocessor %pragma directive. Currently there are none.
2225 * Gets passed the token list starting with the "preproc" token from
2226 * "%pragma preproc".
2227 */
2228static void do_pragma_preproc(Token *tline)
2229{
2230 /* Skip to the real stuff */
2231 tline = tline->next;
2232 skip_white_(tline);
2233 if (!tline)
2234 return;
2235
2236 (void)tline; /* Nothing else to do at present */
2237}
2238
Ed Beroset3ab3f412002-06-11 03:31:49 +00002239/**
2240 * find and process preprocessor directive in passed line
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002241 * Find out if a line contains a preprocessor directive, and deal
2242 * with it if so.
H. Peter Anvin70653092007-10-19 14:42:29 -07002243 *
Ed Beroset3ab3f412002-06-11 03:31:49 +00002244 * If a directive _is_ found, it is the responsibility of this routine
2245 * (and not the caller) to free_tlist() the line.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002246 *
Ed Beroset3ab3f412002-06-11 03:31:49 +00002247 * @param tline a pointer to the current tokeninzed line linked list
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07002248 * @param output if this directive generated output
Ed Beroset3ab3f412002-06-11 03:31:49 +00002249 * @return DIRECTIVE_FOUND or NO_DIRECTIVE_FOUND
H. Peter Anvin70653092007-10-19 14:42:29 -07002250 *
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002251 */
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07002252static int do_directive(Token *tline, char **output)
H. Peter Anvineba20a72002-04-30 20:53:55 +00002253{
H. Peter Anvin4169a472007-09-12 01:29:43 +00002254 enum preproc_token i;
2255 int j;
H. Peter Anvin70055962007-10-11 00:05:31 -07002256 bool err;
2257 int nparam;
2258 bool nolist;
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07002259 bool casesense;
H. Peter Anvin8cfdb9d2007-09-14 18:36:01 -07002260 int k, m;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002261 int offset;
H. Peter Anvinccad6f92016-10-04 00:34:35 -07002262 char *p, *pp;
2263 const char *found_path;
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08002264 const char *mname;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002265 Include *inc;
2266 Context *ctx;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002267 Cond *cond;
2268 MMacro *mmac, **mmhead;
Jin Kyu Songc9486b92013-10-28 17:07:57 -07002269 Token *t = NULL, *tt, *param_start, *macro_start, *last, **tptr, *origline;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002270 Line *l;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002271 struct tokenval tokval;
2272 expr *evalresult;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002273 MMacro *tmp_defining; /* Used when manipulating rep_nest */
H. Peter Anvinf8ba53e2007-10-11 10:11:57 -07002274 int64_t count;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07002275 size_t len;
H. Peter Anvin8e3f75e2008-09-24 00:21:58 -07002276 int severity;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002277
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07002278 *output = NULL; /* No output generated */
H. Peter Anvin76690a12002-04-30 20:52:49 +00002279 origline = tline;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002280
H. Peter Anvineba20a72002-04-30 20:53:55 +00002281 skip_white_(tline);
H. Peter Anvinf2936d72008-06-04 15:11:23 -07002282 if (!tline || !tok_type_(tline, TOK_PREPROC_ID) ||
H. Peter Anvine2c80182005-01-15 22:15:51 +00002283 (tline->text[1] == '%' || tline->text[1] == '$'
2284 || tline->text[1] == '!'))
2285 return NO_DIRECTIVE_FOUND;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002286
H. Peter Anvin4169a472007-09-12 01:29:43 +00002287 i = pp_token_hash(tline->text);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002288
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002289 /*
2290 * FIXME: We zap execution of PP_RMACRO, PP_IRMACRO, PP_EXITMACRO
2291 * since they are known to be buggy at moment, we need to fix them
2292 * in future release (2.09-2.10)
2293 */
Philipp Klokeb432f572013-03-31 12:01:23 +02002294 if (i == PP_RMACRO || i == PP_IRMACRO || i == PP_EXITMACRO) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002295 nasm_error(ERR_NONFATAL, "unknown preprocessor directive `%s'",
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002296 tline->text);
2297 return NO_DIRECTIVE_FOUND;
2298 }
2299
2300 /*
2301 * If we're in a non-emitting branch of a condition construct,
2302 * or walking to the end of an already terminated %rep block,
2303 * we should ignore all directives except for condition
2304 * directives.
2305 */
2306 if (((istk->conds && !emitting(istk->conds->state)) ||
2307 (istk->mstk && !istk->mstk->in_progress)) && !is_condition(i)) {
2308 return NO_DIRECTIVE_FOUND;
2309 }
2310
2311 /*
2312 * If we're defining a macro or reading a %rep block, we should
2313 * ignore all directives except for %macro/%imacro (which nest),
2314 * %endm/%endmacro, and (only if we're in a %rep block) %endrep.
2315 * If we're in a %rep block, another %rep nests, so should be let through.
2316 */
2317 if (defining && i != PP_MACRO && i != PP_IMACRO &&
2318 i != PP_RMACRO && i != PP_IRMACRO &&
2319 i != PP_ENDMACRO && i != PP_ENDM &&
2320 (defining->name || (i != PP_ENDREP && i != PP_REP))) {
2321 return NO_DIRECTIVE_FOUND;
2322 }
2323
2324 if (defining) {
2325 if (i == PP_MACRO || i == PP_IMACRO ||
2326 i == PP_RMACRO || i == PP_IRMACRO) {
2327 nested_mac_count++;
2328 return NO_DIRECTIVE_FOUND;
2329 } else if (nested_mac_count > 0) {
2330 if (i == PP_ENDMACRO) {
2331 nested_mac_count--;
2332 return NO_DIRECTIVE_FOUND;
2333 }
2334 }
2335 if (!defining->name) {
2336 if (i == PP_REP) {
2337 nested_rep_count++;
2338 return NO_DIRECTIVE_FOUND;
2339 } else if (nested_rep_count > 0) {
2340 if (i == PP_ENDREP) {
2341 nested_rep_count--;
2342 return NO_DIRECTIVE_FOUND;
2343 }
2344 }
2345 }
2346 }
2347
H. Peter Anvin4169a472007-09-12 01:29:43 +00002348 switch (i) {
2349 case PP_INVALID:
H. Peter Anvin130736c2016-02-17 20:27:41 -08002350 nasm_error(ERR_NONFATAL, "unknown preprocessor directive `%s'",
H. Peter Anvine2c80182005-01-15 22:15:51 +00002351 tline->text);
2352 return NO_DIRECTIVE_FOUND; /* didn't get it */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002353
H. Peter Anvin3f87a2a2016-10-04 14:07:19 -07002354 case PP_PRAGMA:
2355 /*
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07002356 * %pragma namespace options...
2357 *
2358 * The namespace "preproc" is reserved for the preprocessor;
2359 * all other namespaces generate a [pragma] assembly directive.
2360 *
2361 * Invalid %pragmas are ignored and may have different
2362 * meaning in future versions of NASM.
H. Peter Anvin3f87a2a2016-10-04 14:07:19 -07002363 */
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07002364 tline = tline->next;
2365 skip_white_(tline);
2366 tline = expand_smacro(tline);
2367 if (tok_type_(tline, TOK_ID)) {
2368 if (!nasm_stricmp(tline->text, "preproc")) {
2369 /* Preprocessor pragma */
2370 do_pragma_preproc(tline);
2371 } else {
2372 /* Build the assembler directive */
2373 t = new_Token(NULL, TOK_OTHER, "[", 1);
2374 t->next = new_Token(NULL, TOK_ID, "pragma", 6);
2375 t->next->next = new_Token(tline, TOK_WHITESPACE, NULL, 0);
2376 tline = t;
2377 for (t = tline; t->next; t = t->next)
2378 ;
2379 t->next = new_Token(NULL, TOK_OTHER, "]", 1);
2380 /* true here can be revisited in the future */
2381 *output = detoken(tline, true);
2382 }
2383 }
H. Peter Anvin3f87a2a2016-10-04 14:07:19 -07002384 free_tlist(origline);
2385 return DIRECTIVE_FOUND;
2386
H. Peter Anvine2c80182005-01-15 22:15:51 +00002387 case PP_STACKSIZE:
2388 /* Directive to tell NASM what the default stack size is. The
2389 * default is for a 16-bit stack, and this can be overriden with
2390 * %stacksize large.
H. Peter Anvine2c80182005-01-15 22:15:51 +00002391 */
2392 tline = tline->next;
2393 if (tline && tline->type == TOK_WHITESPACE)
2394 tline = tline->next;
2395 if (!tline || tline->type != TOK_ID) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002396 nasm_error(ERR_NONFATAL, "`%%stacksize' missing size parameter");
H. Peter Anvine2c80182005-01-15 22:15:51 +00002397 free_tlist(origline);
2398 return DIRECTIVE_FOUND;
2399 }
2400 if (nasm_stricmp(tline->text, "flat") == 0) {
2401 /* All subsequent ARG directives are for a 32-bit stack */
2402 StackSize = 4;
2403 StackPointer = "ebp";
2404 ArgOffset = 8;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002405 LocalOffset = 0;
Charles Crayne7eaf9192007-11-08 22:11:14 -08002406 } else if (nasm_stricmp(tline->text, "flat64") == 0) {
2407 /* All subsequent ARG directives are for a 64-bit stack */
2408 StackSize = 8;
2409 StackPointer = "rbp";
Per Jessen53252e02010-02-11 00:16:59 +03002410 ArgOffset = 16;
Charles Crayne7eaf9192007-11-08 22:11:14 -08002411 LocalOffset = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002412 } else if (nasm_stricmp(tline->text, "large") == 0) {
2413 /* All subsequent ARG directives are for a 16-bit stack,
2414 * far function call.
2415 */
2416 StackSize = 2;
2417 StackPointer = "bp";
2418 ArgOffset = 4;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002419 LocalOffset = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002420 } else if (nasm_stricmp(tline->text, "small") == 0) {
2421 /* All subsequent ARG directives are for a 16-bit stack,
2422 * far function call. We don't support near functions.
2423 */
2424 StackSize = 2;
2425 StackPointer = "bp";
2426 ArgOffset = 6;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002427 LocalOffset = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002428 } else {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002429 nasm_error(ERR_NONFATAL, "`%%stacksize' invalid size type");
H. Peter Anvine2c80182005-01-15 22:15:51 +00002430 free_tlist(origline);
2431 return DIRECTIVE_FOUND;
2432 }
2433 free_tlist(origline);
2434 return DIRECTIVE_FOUND;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002435
H. Peter Anvine2c80182005-01-15 22:15:51 +00002436 case PP_ARG:
2437 /* TASM like ARG directive to define arguments to functions, in
2438 * the following form:
2439 *
2440 * ARG arg1:WORD, arg2:DWORD, arg4:QWORD
2441 */
2442 offset = ArgOffset;
2443 do {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00002444 char *arg, directive[256];
H. Peter Anvine2c80182005-01-15 22:15:51 +00002445 int size = StackSize;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002446
H. Peter Anvine2c80182005-01-15 22:15:51 +00002447 /* Find the argument name */
2448 tline = tline->next;
2449 if (tline && tline->type == TOK_WHITESPACE)
2450 tline = tline->next;
2451 if (!tline || tline->type != TOK_ID) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002452 nasm_error(ERR_NONFATAL, "`%%arg' missing argument parameter");
H. Peter Anvine2c80182005-01-15 22:15:51 +00002453 free_tlist(origline);
2454 return DIRECTIVE_FOUND;
2455 }
2456 arg = tline->text;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002457
H. Peter Anvine2c80182005-01-15 22:15:51 +00002458 /* Find the argument size type */
2459 tline = tline->next;
2460 if (!tline || tline->type != TOK_OTHER
2461 || tline->text[0] != ':') {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002462 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00002463 "Syntax error processing `%%arg' directive");
2464 free_tlist(origline);
2465 return DIRECTIVE_FOUND;
2466 }
2467 tline = tline->next;
2468 if (!tline || tline->type != TOK_ID) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002469 nasm_error(ERR_NONFATAL, "`%%arg' missing size type parameter");
H. Peter Anvine2c80182005-01-15 22:15:51 +00002470 free_tlist(origline);
2471 return DIRECTIVE_FOUND;
2472 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002473
H. Peter Anvine2c80182005-01-15 22:15:51 +00002474 /* Allow macro expansion of type parameter */
Keith Kaniosb7a89542007-04-12 02:40:54 +00002475 tt = tokenize(tline->text);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002476 tt = expand_smacro(tt);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002477 size = parse_size(tt->text);
2478 if (!size) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002479 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00002480 "Invalid size type for `%%arg' missing directive");
2481 free_tlist(tt);
2482 free_tlist(origline);
2483 return DIRECTIVE_FOUND;
2484 }
2485 free_tlist(tt);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002486
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002487 /* Round up to even stack slots */
2488 size = ALIGN(size, StackSize);
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002489
H. Peter Anvine2c80182005-01-15 22:15:51 +00002490 /* Now define the macro for the argument */
2491 snprintf(directive, sizeof(directive), "%%define %s (%s+%d)",
2492 arg, StackPointer, offset);
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07002493 do_directive(tokenize(directive), output);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002494 offset += size;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002495
H. Peter Anvine2c80182005-01-15 22:15:51 +00002496 /* Move to the next argument in the list */
2497 tline = tline->next;
2498 if (tline && tline->type == TOK_WHITESPACE)
2499 tline = tline->next;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002500 } while (tline && tline->type == TOK_OTHER && tline->text[0] == ',');
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002501 ArgOffset = offset;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002502 free_tlist(origline);
2503 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002504
H. Peter Anvine2c80182005-01-15 22:15:51 +00002505 case PP_LOCAL:
2506 /* TASM like LOCAL directive to define local variables for a
2507 * function, in the following form:
2508 *
2509 * LOCAL local1:WORD, local2:DWORD, local4:QWORD = LocalSize
2510 *
2511 * The '= LocalSize' at the end is ignored by NASM, but is
2512 * required by TASM to define the local parameter size (and used
2513 * by the TASM macro package).
2514 */
2515 offset = LocalOffset;
2516 do {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00002517 char *local, directive[256];
H. Peter Anvine2c80182005-01-15 22:15:51 +00002518 int size = StackSize;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002519
H. Peter Anvine2c80182005-01-15 22:15:51 +00002520 /* Find the argument name */
2521 tline = tline->next;
2522 if (tline && tline->type == TOK_WHITESPACE)
2523 tline = tline->next;
2524 if (!tline || tline->type != TOK_ID) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002525 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00002526 "`%%local' missing argument parameter");
2527 free_tlist(origline);
2528 return DIRECTIVE_FOUND;
2529 }
2530 local = tline->text;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002531
H. Peter Anvine2c80182005-01-15 22:15:51 +00002532 /* Find the argument size type */
2533 tline = tline->next;
2534 if (!tline || tline->type != TOK_OTHER
2535 || tline->text[0] != ':') {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002536 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00002537 "Syntax error processing `%%local' directive");
2538 free_tlist(origline);
2539 return DIRECTIVE_FOUND;
2540 }
2541 tline = tline->next;
2542 if (!tline || tline->type != TOK_ID) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002543 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00002544 "`%%local' missing size type parameter");
2545 free_tlist(origline);
2546 return DIRECTIVE_FOUND;
2547 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00002548
H. Peter Anvine2c80182005-01-15 22:15:51 +00002549 /* Allow macro expansion of type parameter */
Keith Kaniosb7a89542007-04-12 02:40:54 +00002550 tt = tokenize(tline->text);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002551 tt = expand_smacro(tt);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002552 size = parse_size(tt->text);
2553 if (!size) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002554 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00002555 "Invalid size type for `%%local' missing directive");
2556 free_tlist(tt);
2557 free_tlist(origline);
2558 return DIRECTIVE_FOUND;
2559 }
2560 free_tlist(tt);
H. Peter Anvin734b1882002-04-30 21:01:08 +00002561
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002562 /* Round up to even stack slots */
2563 size = ALIGN(size, StackSize);
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002564
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002565 offset += size; /* Negative offset, increment before */
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002566
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002567 /* Now define the macro for the argument */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002568 snprintf(directive, sizeof(directive), "%%define %s (%s-%d)",
2569 local, StackPointer, offset);
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07002570 do_directive(tokenize(directive), output);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002571
H. Peter Anvine2c80182005-01-15 22:15:51 +00002572 /* Now define the assign to setup the enter_c macro correctly */
2573 snprintf(directive, sizeof(directive),
2574 "%%assign %%$localsize %%$localsize+%d", size);
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07002575 do_directive(tokenize(directive), output);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002576
H. Peter Anvine2c80182005-01-15 22:15:51 +00002577 /* Move to the next argument in the list */
2578 tline = tline->next;
2579 if (tline && tline->type == TOK_WHITESPACE)
2580 tline = tline->next;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002581 } while (tline && tline->type == TOK_OTHER && tline->text[0] == ',');
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002582 LocalOffset = offset;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002583 free_tlist(origline);
2584 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002585
H. Peter Anvine2c80182005-01-15 22:15:51 +00002586 case PP_CLEAR:
2587 if (tline->next)
H. Peter Anvin130736c2016-02-17 20:27:41 -08002588 nasm_error(ERR_WARNING|ERR_PASS1,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002589 "trailing garbage after `%%clear' ignored");
2590 free_macros();
2591 init_macros();
H. Peter Anvine2c80182005-01-15 22:15:51 +00002592 free_tlist(origline);
2593 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002594
H. Peter Anvin418ca702008-05-30 10:42:30 -07002595 case PP_DEPEND:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002596 t = tline->next = expand_smacro(tline->next);
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002597 skip_white_(t);
2598 if (!t || (t->type != TOK_STRING &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002599 t->type != TOK_INTERNAL_STRING)) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002600 nasm_error(ERR_NONFATAL, "`%%depend' expects a file name");
H. Peter Anvin418ca702008-05-30 10:42:30 -07002601 free_tlist(origline);
2602 return DIRECTIVE_FOUND; /* but we did _something_ */
2603 }
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002604 if (t->next)
H. Peter Anvin130736c2016-02-17 20:27:41 -08002605 nasm_error(ERR_WARNING|ERR_PASS1,
H. Peter Anvin418ca702008-05-30 10:42:30 -07002606 "trailing garbage after `%%depend' ignored");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002607 p = t->text;
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002608 if (t->type != TOK_INTERNAL_STRING)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002609 nasm_unquote_cstr(p, i);
H. Peter Anvin436e3672016-10-04 01:12:28 -07002610 nasm_add_string_to_strlist(dephead, p);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002611 free_tlist(origline);
H. Peter Anvin418ca702008-05-30 10:42:30 -07002612 return DIRECTIVE_FOUND;
2613
2614 case PP_INCLUDE:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002615 t = tline->next = expand_smacro(tline->next);
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002616 skip_white_(t);
H. Peter Anvind2456592008-06-19 15:04:18 -07002617
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002618 if (!t || (t->type != TOK_STRING &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002619 t->type != TOK_INTERNAL_STRING)) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002620 nasm_error(ERR_NONFATAL, "`%%include' expects a file name");
H. Peter Anvine2c80182005-01-15 22:15:51 +00002621 free_tlist(origline);
2622 return DIRECTIVE_FOUND; /* but we did _something_ */
2623 }
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002624 if (t->next)
H. Peter Anvin130736c2016-02-17 20:27:41 -08002625 nasm_error(ERR_WARNING|ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +00002626 "trailing garbage after `%%include' ignored");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002627 p = t->text;
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002628 if (t->type != TOK_INTERNAL_STRING)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002629 nasm_unquote_cstr(p, i);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002630 inc = nasm_malloc(sizeof(Include));
H. Peter Anvine2c80182005-01-15 22:15:51 +00002631 inc->next = istk;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002632 inc->conds = NULL;
Jim Kukunas65a8afc2016-06-13 16:00:42 -04002633 found_path = NULL;
H. Peter Anvin9924d1e2016-10-04 00:59:39 -07002634 inc->fp = inc_fopen(p, dephead, &found_path,
H. Peter Anvind81a2352016-09-21 14:03:18 -07002635 pass == 0 ? INC_OPTIONAL : INC_NEEDED, NF_TEXT);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002636 if (!inc->fp) {
2637 /* -MG given but file not found */
2638 nasm_free(inc);
2639 } else {
Jim Kukunas65a8afc2016-06-13 16:00:42 -04002640 inc->fname = src_set_fname(found_path ? found_path : p);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002641 inc->lineno = src_set_linnum(0);
2642 inc->lineinc = 1;
2643 inc->expansion = NULL;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002644 inc->mstk = NULL;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002645 istk = inc;
H. Peter Anvin8ac25aa2016-02-18 01:16:18 -08002646 lfmt->uplevel(LIST_INCLUDE);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002647 }
2648 free_tlist(origline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002649 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002650
H. Peter Anvind2456592008-06-19 15:04:18 -07002651 case PP_USE:
H. Peter Anvinf4ae5ad2008-06-19 18:39:24 -07002652 {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002653 static macros_t *use_pkg;
2654 const char *pkg_macro = NULL;
H. Peter Anvinf4ae5ad2008-06-19 18:39:24 -07002655
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002656 tline = tline->next;
2657 skip_white_(tline);
2658 tline = expand_id(tline);
H. Peter Anvind2456592008-06-19 15:04:18 -07002659
H. Peter Anvin264b7b92008-10-24 16:38:17 -07002660 if (!tline || (tline->type != TOK_STRING &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002661 tline->type != TOK_INTERNAL_STRING &&
2662 tline->type != TOK_ID)) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002663 nasm_error(ERR_NONFATAL, "`%%use' expects a package name");
H. Peter Anvind2456592008-06-19 15:04:18 -07002664 free_tlist(origline);
2665 return DIRECTIVE_FOUND; /* but we did _something_ */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002666 }
H. Peter Anvin264b7b92008-10-24 16:38:17 -07002667 if (tline->next)
H. Peter Anvin130736c2016-02-17 20:27:41 -08002668 nasm_error(ERR_WARNING|ERR_PASS1,
H. Peter Anvind2456592008-06-19 15:04:18 -07002669 "trailing garbage after `%%use' ignored");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002670 if (tline->type == TOK_STRING)
2671 nasm_unquote_cstr(tline->text, i);
2672 use_pkg = nasm_stdmac_find_package(tline->text);
2673 if (!use_pkg)
H. Peter Anvin130736c2016-02-17 20:27:41 -08002674 nasm_error(ERR_NONFATAL, "unknown `%%use' package: %s", tline->text);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002675 else
2676 pkg_macro = (char *)use_pkg + 1; /* The first string will be <%define>__USE_*__ */
Victor van den Elzen35eb2ea2010-03-10 22:33:48 +01002677 if (use_pkg && ! smacro_defined(NULL, pkg_macro, 0, NULL, true)) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002678 /* Not already included, go ahead and include it */
2679 stdmacpos = use_pkg;
2680 }
2681 free_tlist(origline);
H. Peter Anvind2456592008-06-19 15:04:18 -07002682 return DIRECTIVE_FOUND;
H. Peter Anvinf4ae5ad2008-06-19 18:39:24 -07002683 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002684 case PP_PUSH:
H. Peter Anvine2c80182005-01-15 22:15:51 +00002685 case PP_REPL:
H. Peter Anvin42b56392008-10-24 16:24:21 -07002686 case PP_POP:
H. Peter Anvine2c80182005-01-15 22:15:51 +00002687 tline = tline->next;
2688 skip_white_(tline);
2689 tline = expand_id(tline);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002690 if (tline) {
2691 if (!tok_type_(tline, TOK_ID)) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002692 nasm_error(ERR_NONFATAL, "`%s' expects a context identifier",
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002693 pp_directives[i]);
2694 free_tlist(origline);
2695 return DIRECTIVE_FOUND; /* but we did _something_ */
2696 }
2697 if (tline->next)
H. Peter Anvin130736c2016-02-17 20:27:41 -08002698 nasm_error(ERR_WARNING|ERR_PASS1,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002699 "trailing garbage after `%s' ignored",
2700 pp_directives[i]);
2701 p = nasm_strdup(tline->text);
2702 } else {
2703 p = NULL; /* Anonymous */
2704 }
H. Peter Anvin42b56392008-10-24 16:24:21 -07002705
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002706 if (i == PP_PUSH) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002707 ctx = nasm_malloc(sizeof(Context));
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002708 ctx->next = cstk;
2709 hash_init(&ctx->localmac, HASH_SMALL);
2710 ctx->name = p;
2711 ctx->number = unique++;
2712 cstk = ctx;
2713 } else {
2714 /* %pop or %repl */
2715 if (!cstk) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002716 nasm_error(ERR_NONFATAL, "`%s': context stack is empty",
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002717 pp_directives[i]);
2718 } else if (i == PP_POP) {
2719 if (p && (!cstk->name || nasm_stricmp(p, cstk->name)))
H. Peter Anvin130736c2016-02-17 20:27:41 -08002720 nasm_error(ERR_NONFATAL, "`%%pop' in wrong context: %s, "
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002721 "expected %s",
2722 cstk->name ? cstk->name : "anonymous", p);
2723 else
2724 ctx_pop();
2725 } else {
2726 /* i == PP_REPL */
2727 nasm_free(cstk->name);
2728 cstk->name = p;
2729 p = NULL;
2730 }
2731 nasm_free(p);
2732 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002733 free_tlist(origline);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002734 return DIRECTIVE_FOUND;
H. Peter Anvin8e3f75e2008-09-24 00:21:58 -07002735 case PP_FATAL:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002736 severity = ERR_FATAL;
2737 goto issue_error;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002738 case PP_ERROR:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002739 severity = ERR_NONFATAL;
2740 goto issue_error;
H. Peter Anvin7df04172008-06-10 18:27:38 -07002741 case PP_WARNING:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002742 severity = ERR_WARNING|ERR_WARN_USER;
2743 goto issue_error;
H. Peter Anvin8e3f75e2008-09-24 00:21:58 -07002744
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002745issue_error:
H. Peter Anvin7df04172008-06-10 18:27:38 -07002746 {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002747 /* Only error out if this is the final pass */
2748 if (pass != 2 && i != PP_FATAL)
2749 return DIRECTIVE_FOUND;
2750
2751 tline->next = expand_smacro(tline->next);
2752 tline = tline->next;
2753 skip_white_(tline);
2754 t = tline ? tline->next : NULL;
2755 skip_white_(t);
2756 if (tok_type_(tline, TOK_STRING) && !t) {
2757 /* The line contains only a quoted string */
2758 p = tline->text;
2759 nasm_unquote(p, NULL); /* Ignore NUL character truncation */
H. Peter Anvin130736c2016-02-17 20:27:41 -08002760 nasm_error(severity, "%s", p);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002761 } else {
2762 /* Not a quoted string, or more than a quoted string */
2763 p = detoken(tline, false);
H. Peter Anvin130736c2016-02-17 20:27:41 -08002764 nasm_error(severity, "%s", p);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002765 nasm_free(p);
2766 }
2767 free_tlist(origline);
2768 return DIRECTIVE_FOUND;
H. Peter Anvin7df04172008-06-10 18:27:38 -07002769 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00002770
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002771 CASE_PP_IF:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002772 if (istk->conds && !emitting(istk->conds->state))
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002773 j = COND_NEVER;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002774 else {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002775 j = if_condition(tline->next, i);
2776 tline->next = NULL; /* it got freed */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002777 j = j < 0 ? COND_NEVER : j ? COND_IF_TRUE : COND_IF_FALSE;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002778 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002779 cond = nasm_malloc(sizeof(Cond));
2780 cond->next = istk->conds;
2781 cond->state = j;
2782 istk->conds = cond;
2783 if(istk->mstk)
2784 istk->mstk->condcnt ++;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002785 free_tlist(origline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002786 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002787
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002788 CASE_PP_ELIF:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002789 if (!istk->conds)
H. Peter Anvin130736c2016-02-17 20:27:41 -08002790 nasm_error(ERR_FATAL, "`%s': no matching `%%if'", pp_directives[i]);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002791 switch(istk->conds->state) {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002792 case COND_IF_TRUE:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002793 istk->conds->state = COND_DONE;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002794 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002795
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002796 case COND_DONE:
2797 case COND_NEVER:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002798 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002799
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002800 case COND_ELSE_TRUE:
2801 case COND_ELSE_FALSE:
H. Peter Anvin130736c2016-02-17 20:27:41 -08002802 nasm_error(ERR_WARNING|ERR_PASS1|ERR_PP_PRECOND,
2803 "`%%elif' after `%%else' ignored");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002804 istk->conds->state = COND_NEVER;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002805 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002806
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002807 case COND_IF_FALSE:
2808 /*
2809 * IMPORTANT: In the case of %if, we will already have
2810 * called expand_mmac_params(); however, if we're
2811 * processing an %elif we must have been in a
2812 * non-emitting mode, which would have inhibited
2813 * the normal invocation of expand_mmac_params().
2814 * Therefore, we have to do it explicitly here.
2815 */
2816 j = if_condition(expand_mmac_params(tline->next), i);
2817 tline->next = NULL; /* it got freed */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002818 istk->conds->state =
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002819 j < 0 ? COND_NEVER : j ? COND_IF_TRUE : COND_IF_FALSE;
2820 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002821 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002822 free_tlist(origline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002823 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002824
H. Peter Anvine2c80182005-01-15 22:15:51 +00002825 case PP_ELSE:
2826 if (tline->next)
H. Peter Anvin130736c2016-02-17 20:27:41 -08002827 nasm_error(ERR_WARNING|ERR_PASS1|ERR_PP_PRECOND,
2828 "trailing garbage after `%%else' ignored");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002829 if (!istk->conds)
H. Peter Anvin130736c2016-02-17 20:27:41 -08002830 nasm_fatal(0, "`%%else: no matching `%%if'");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002831 switch(istk->conds->state) {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002832 case COND_IF_TRUE:
2833 case COND_DONE:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002834 istk->conds->state = COND_ELSE_FALSE;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002835 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002836
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002837 case COND_NEVER:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002838 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002839
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002840 case COND_IF_FALSE:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002841 istk->conds->state = COND_ELSE_TRUE;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002842 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002843
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002844 case COND_ELSE_TRUE:
2845 case COND_ELSE_FALSE:
H. Peter Anvin130736c2016-02-17 20:27:41 -08002846 nasm_error(ERR_WARNING|ERR_PASS1|ERR_PP_PRECOND,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002847 "`%%else' after `%%else' ignored.");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002848 istk->conds->state = COND_NEVER;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002849 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002850 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002851 free_tlist(origline);
2852 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002853
H. Peter Anvine2c80182005-01-15 22:15:51 +00002854 case PP_ENDIF:
2855 if (tline->next)
H. Peter Anvin130736c2016-02-17 20:27:41 -08002856 nasm_error(ERR_WARNING|ERR_PASS1|ERR_PP_PRECOND,
2857 "trailing garbage after `%%endif' ignored");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002858 if (!istk->conds)
H. Peter Anvin130736c2016-02-17 20:27:41 -08002859 nasm_error(ERR_FATAL, "`%%endif': no matching `%%if'");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002860 cond = istk->conds;
2861 istk->conds = cond->next;
2862 nasm_free(cond);
2863 if(istk->mstk)
2864 istk->mstk->condcnt --;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002865 free_tlist(origline);
2866 return DIRECTIVE_FOUND;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002867
H. Peter Anvindb8f96e2009-07-15 09:07:29 -04002868 case PP_RMACRO:
2869 case PP_IRMACRO:
H. Peter Anvine2c80182005-01-15 22:15:51 +00002870 case PP_MACRO:
2871 case PP_IMACRO:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002872 if (defining) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002873 nasm_error(ERR_FATAL, "`%s': already defining a macro",
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002874 pp_directives[i]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002875 return DIRECTIVE_FOUND;
2876 }
H. Peter Anvin4def1a82016-05-09 13:59:44 -07002877 defining = nasm_zalloc(sizeof(MMacro));
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002878 defining->max_depth =
2879 (i == PP_RMACRO) || (i == PP_IRMACRO) ? DEADMAN_LIMIT : 0;
2880 defining->casesense = (i == PP_MACRO) || (i == PP_RMACRO);
2881 if (!parse_mmacro_spec(tline, defining, pp_directives[i])) {
2882 nasm_free(defining);
2883 defining = NULL;
2884 return DIRECTIVE_FOUND;
2885 }
H. Peter Anvina26433d2008-07-16 14:40:01 -07002886
H. Peter Anvin4def1a82016-05-09 13:59:44 -07002887 src_get(&defining->xline, &defining->fname);
2888
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002889 mmac = (MMacro *) hash_findix(&mmacros, defining->name);
2890 while (mmac) {
2891 if (!strcmp(mmac->name, defining->name) &&
2892 (mmac->nparam_min <= defining->nparam_max
2893 || defining->plus)
2894 && (defining->nparam_min <= mmac->nparam_max
2895 || mmac->plus)) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002896 nasm_error(ERR_WARNING|ERR_PASS1,
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002897 "redefining multi-line macro `%s'", defining->name);
2898 return DIRECTIVE_FOUND;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002899 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002900 mmac = mmac->next;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002901 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002902 free_tlist(origline);
2903 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002904
H. Peter Anvine2c80182005-01-15 22:15:51 +00002905 case PP_ENDM:
2906 case PP_ENDMACRO:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002907 if (! (defining && defining->name)) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002908 nasm_error(ERR_NONFATAL, "`%s': not defining a macro", tline->text);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002909 return DIRECTIVE_FOUND;
2910 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002911 mmhead = (MMacro **) hash_findi_add(&mmacros, defining->name);
2912 defining->next = *mmhead;
2913 *mmhead = defining;
2914 defining = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002915 free_tlist(origline);
2916 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002917
H. Peter Anvin89cee572009-07-15 09:16:54 -04002918 case PP_EXITMACRO:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002919 /*
2920 * We must search along istk->expansion until we hit a
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002921 * macro-end marker for a macro with a name. Then we
2922 * bypass all lines between exitmacro and endmacro.
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002923 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002924 list_for_each(l, istk->expansion)
2925 if (l->finishes && l->finishes->name)
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002926 break;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002927
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002928 if (l) {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002929 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002930 * Remove all conditional entries relative to this
2931 * macro invocation. (safe to do in this context)
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002932 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002933 for ( ; l->finishes->condcnt > 0; l->finishes->condcnt --) {
2934 cond = istk->conds;
2935 istk->conds = cond->next;
2936 nasm_free(cond);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002937 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002938 istk->expansion = l;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002939 } else {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002940 nasm_error(ERR_NONFATAL, "`%%exitmacro' not within `%%macro' block");
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002941 }
Keith Kanios852f1ee2009-07-12 00:19:55 -05002942 free_tlist(origline);
2943 return DIRECTIVE_FOUND;
2944
H. Peter Anvina26433d2008-07-16 14:40:01 -07002945 case PP_UNMACRO:
2946 case PP_UNIMACRO:
2947 {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002948 MMacro **mmac_p;
2949 MMacro spec;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002950
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002951 spec.casesense = (i == PP_UNMACRO);
2952 if (!parse_mmacro_spec(tline, &spec, pp_directives[i])) {
2953 return DIRECTIVE_FOUND;
2954 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002955 mmac_p = (MMacro **) hash_findi(&mmacros, spec.name, NULL);
2956 while (mmac_p && *mmac_p) {
2957 mmac = *mmac_p;
2958 if (mmac->casesense == spec.casesense &&
2959 !mstrcmp(mmac->name, spec.name, spec.casesense) &&
2960 mmac->nparam_min == spec.nparam_min &&
2961 mmac->nparam_max == spec.nparam_max &&
2962 mmac->plus == spec.plus) {
2963 *mmac_p = mmac->next;
2964 free_mmacro(mmac);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002965 } else {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002966 mmac_p = &mmac->next;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002967 }
2968 }
2969 free_tlist(origline);
2970 free_tlist(spec.dlist);
2971 return DIRECTIVE_FOUND;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002972 }
2973
H. Peter Anvine2c80182005-01-15 22:15:51 +00002974 case PP_ROTATE:
2975 if (tline->next && tline->next->type == TOK_WHITESPACE)
2976 tline = tline->next;
H. Peter Anvin89cee572009-07-15 09:16:54 -04002977 if (!tline->next) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002978 free_tlist(origline);
H. Peter Anvin130736c2016-02-17 20:27:41 -08002979 nasm_error(ERR_NONFATAL, "`%%rotate' missing rotate count");
H. Peter Anvine2c80182005-01-15 22:15:51 +00002980 return DIRECTIVE_FOUND;
2981 }
2982 t = expand_smacro(tline->next);
2983 tline->next = NULL;
2984 free_tlist(origline);
2985 tline = t;
2986 tptr = &t;
2987 tokval.t_type = TOKEN_INVALID;
2988 evalresult =
H. Peter Anvin130736c2016-02-17 20:27:41 -08002989 evaluate(ppscan, tptr, &tokval, NULL, pass, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002990 free_tlist(tline);
2991 if (!evalresult)
2992 return DIRECTIVE_FOUND;
2993 if (tokval.t_type)
H. Peter Anvin130736c2016-02-17 20:27:41 -08002994 nasm_error(ERR_WARNING|ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +00002995 "trailing garbage after expression ignored");
2996 if (!is_simple(evalresult)) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002997 nasm_error(ERR_NONFATAL, "non-constant value given to `%%rotate'");
H. Peter Anvine2c80182005-01-15 22:15:51 +00002998 return DIRECTIVE_FOUND;
2999 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003000 mmac = istk->mstk;
3001 while (mmac && !mmac->name) /* avoid mistaking %reps for macros */
3002 mmac = mmac->next_active;
3003 if (!mmac) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08003004 nasm_error(ERR_NONFATAL, "`%%rotate' invoked outside a macro call");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003005 } else if (mmac->nparam == 0) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08003006 nasm_error(ERR_NONFATAL,
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003007 "`%%rotate' invoked within macro without parameters");
3008 } else {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003009 int rotate = mmac->rotate + reloc_value(evalresult);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003010
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003011 rotate %= (int)mmac->nparam;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003012 if (rotate < 0)
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003013 rotate += mmac->nparam;
3014
3015 mmac->rotate = rotate;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003016 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00003017 return DIRECTIVE_FOUND;
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00003018
H. Peter Anvine2c80182005-01-15 22:15:51 +00003019 case PP_REP:
H. Peter Anvin6867acc2007-10-10 14:58:45 -07003020 nolist = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003021 do {
3022 tline = tline->next;
3023 } while (tok_type_(tline, TOK_WHITESPACE));
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00003024
H. Peter Anvine2c80182005-01-15 22:15:51 +00003025 if (tok_type_(tline, TOK_ID) &&
3026 nasm_stricmp(tline->text, ".nolist") == 0) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07003027 nolist = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003028 do {
3029 tline = tline->next;
3030 } while (tok_type_(tline, TOK_WHITESPACE));
3031 }
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00003032
H. Peter Anvine2c80182005-01-15 22:15:51 +00003033 if (tline) {
3034 t = expand_smacro(tline);
3035 tptr = &t;
3036 tokval.t_type = TOKEN_INVALID;
3037 evalresult =
H. Peter Anvin130736c2016-02-17 20:27:41 -08003038 evaluate(ppscan, tptr, &tokval, NULL, pass, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003039 if (!evalresult) {
3040 free_tlist(origline);
3041 return DIRECTIVE_FOUND;
3042 }
3043 if (tokval.t_type)
H. Peter Anvin130736c2016-02-17 20:27:41 -08003044 nasm_error(ERR_WARNING|ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +00003045 "trailing garbage after expression ignored");
3046 if (!is_simple(evalresult)) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08003047 nasm_error(ERR_NONFATAL, "non-constant value given to `%%rep'");
H. Peter Anvine2c80182005-01-15 22:15:51 +00003048 return DIRECTIVE_FOUND;
3049 }
Cyrill Gorcunove091d6e2010-08-09 13:58:22 +04003050 count = reloc_value(evalresult);
3051 if (count >= REP_LIMIT) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08003052 nasm_error(ERR_NONFATAL, "`%%rep' value exceeds limit");
Cyrill Gorcunove091d6e2010-08-09 13:58:22 +04003053 count = 0;
3054 } else
3055 count++;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003056 } else {
H. Peter Anvin130736c2016-02-17 20:27:41 -08003057 nasm_error(ERR_NONFATAL, "`%%rep' expects a repeat count");
H. Peter Anvinf8ba53e2007-10-11 10:11:57 -07003058 count = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003059 }
3060 free_tlist(origline);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003061
3062 tmp_defining = defining;
3063 defining = nasm_malloc(sizeof(MMacro));
3064 defining->prev = NULL;
3065 defining->name = NULL; /* flags this macro as a %rep block */
3066 defining->casesense = false;
3067 defining->plus = false;
3068 defining->nolist = nolist;
3069 defining->in_progress = count;
3070 defining->max_depth = 0;
3071 defining->nparam_min = defining->nparam_max = 0;
3072 defining->defaults = NULL;
3073 defining->dlist = NULL;
3074 defining->expansion = NULL;
3075 defining->next_active = istk->mstk;
3076 defining->rep_nest = tmp_defining;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003077 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003078
H. Peter Anvine2c80182005-01-15 22:15:51 +00003079 case PP_ENDREP:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003080 if (!defining || defining->name) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08003081 nasm_error(ERR_NONFATAL, "`%%endrep': no matching `%%rep'");
H. Peter Anvine2c80182005-01-15 22:15:51 +00003082 return DIRECTIVE_FOUND;
3083 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003084
H. Peter Anvine2c80182005-01-15 22:15:51 +00003085 /*
3086 * Now we have a "macro" defined - although it has no name
3087 * and we won't be entering it in the hash tables - we must
3088 * push a macro-end marker for it on to istk->expansion.
3089 * After that, it will take care of propagating itself (a
3090 * macro-end marker line for a macro which is really a %rep
3091 * block will cause the macro to be re-expanded, complete
3092 * with another macro-end marker to ensure the process
3093 * continues) until the whole expansion is forcibly removed
3094 * from istk->expansion by a %exitrep.
3095 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003096 l = nasm_malloc(sizeof(Line));
3097 l->next = istk->expansion;
3098 l->finishes = defining;
3099 l->first = NULL;
3100 istk->expansion = l;
3101
3102 istk->mstk = defining;
3103
H. Peter Anvin8ac25aa2016-02-18 01:16:18 -08003104 lfmt->uplevel(defining->nolist ? LIST_MACRO_NOLIST : LIST_MACRO);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003105 tmp_defining = defining;
3106 defining = defining->rep_nest;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003107 free_tlist(origline);
3108 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003109
H. Peter Anvine2c80182005-01-15 22:15:51 +00003110 case PP_EXITREP:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003111 /*
3112 * We must search along istk->expansion until we hit a
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003113 * macro-end marker for a macro with no name. Then we set
3114 * its `in_progress' flag to 0.
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003115 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003116 list_for_each(l, istk->expansion)
3117 if (l->finishes && !l->finishes->name)
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003118 break;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003119
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003120 if (l)
3121 l->finishes->in_progress = 1;
3122 else
H. Peter Anvin130736c2016-02-17 20:27:41 -08003123 nasm_error(ERR_NONFATAL, "`%%exitrep' not within `%%rep' block");
H. Peter Anvine2c80182005-01-15 22:15:51 +00003124 free_tlist(origline);
3125 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003126
H. Peter Anvine2c80182005-01-15 22:15:51 +00003127 case PP_XDEFINE:
3128 case PP_IXDEFINE:
3129 case PP_DEFINE:
3130 case PP_IDEFINE:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003131 casesense = (i == PP_DEFINE || i == PP_XDEFINE);
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07003132
H. Peter Anvine2c80182005-01-15 22:15:51 +00003133 tline = tline->next;
3134 skip_white_(tline);
3135 tline = expand_id(tline);
3136 if (!tline || (tline->type != TOK_ID &&
3137 (tline->type != TOK_PREPROC_ID ||
3138 tline->text[1] != '$'))) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08003139 nasm_error(ERR_NONFATAL, "`%s' expects a macro identifier",
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003140 pp_directives[i]);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003141 free_tlist(origline);
3142 return DIRECTIVE_FOUND;
3143 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003144
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04003145 ctx = get_ctx(tline->text, &mname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003146 last = tline;
3147 param_start = tline = tline->next;
3148 nparam = 0;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003149
H. Peter Anvine2c80182005-01-15 22:15:51 +00003150 /* Expand the macro definition now for %xdefine and %ixdefine */
3151 if ((i == PP_XDEFINE) || (i == PP_IXDEFINE))
3152 tline = expand_smacro(tline);
H. Peter Anvin734b1882002-04-30 21:01:08 +00003153
H. Peter Anvine2c80182005-01-15 22:15:51 +00003154 if (tok_is_(tline, "(")) {
3155 /*
3156 * This macro has parameters.
3157 */
H. Peter Anvin734b1882002-04-30 21:01:08 +00003158
H. Peter Anvine2c80182005-01-15 22:15:51 +00003159 tline = tline->next;
3160 while (1) {
3161 skip_white_(tline);
3162 if (!tline) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08003163 nasm_error(ERR_NONFATAL, "parameter identifier expected");
H. Peter Anvine2c80182005-01-15 22:15:51 +00003164 free_tlist(origline);
3165 return DIRECTIVE_FOUND;
3166 }
3167 if (tline->type != TOK_ID) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08003168 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00003169 "`%s': parameter identifier expected",
3170 tline->text);
3171 free_tlist(origline);
3172 return DIRECTIVE_FOUND;
3173 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003174 tline->type = TOK_SMAC_PARAM + nparam++;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003175 tline = tline->next;
3176 skip_white_(tline);
3177 if (tok_is_(tline, ",")) {
3178 tline = tline->next;
H. Peter Anvinca348b62008-07-23 10:49:26 -04003179 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003180 if (!tok_is_(tline, ")")) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08003181 nasm_error(ERR_NONFATAL,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003182 "`)' expected to terminate macro template");
3183 free_tlist(origline);
3184 return DIRECTIVE_FOUND;
3185 }
3186 break;
3187 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00003188 }
3189 last = tline;
3190 tline = tline->next;
3191 }
3192 if (tok_type_(tline, TOK_WHITESPACE))
3193 last = tline, tline = tline->next;
3194 macro_start = NULL;
3195 last->next = NULL;
3196 t = tline;
3197 while (t) {
3198 if (t->type == TOK_ID) {
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04003199 list_for_each(tt, param_start)
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003200 if (tt->type >= TOK_SMAC_PARAM &&
H. Peter Anvine2c80182005-01-15 22:15:51 +00003201 !strcmp(tt->text, t->text))
3202 t->type = tt->type;
3203 }
3204 tt = t->next;
3205 t->next = macro_start;
3206 macro_start = t;
3207 t = tt;
3208 }
3209 /*
3210 * Good. We now have a macro name, a parameter count, and a
3211 * token list (in reverse order) for an expansion. We ought
3212 * to be OK just to create an SMacro, store it, and let
3213 * free_tlist have the rest of the line (which we have
3214 * carefully re-terminated after chopping off the expansion
3215 * from the end).
3216 */
H. Peter Anvin4db5a162007-10-11 13:42:09 -07003217 define_smacro(ctx, mname, casesense, nparam, macro_start);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003218 free_tlist(origline);
3219 return DIRECTIVE_FOUND;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003220
H. Peter Anvine2c80182005-01-15 22:15:51 +00003221 case PP_UNDEF:
3222 tline = tline->next;
3223 skip_white_(tline);
3224 tline = expand_id(tline);
3225 if (!tline || (tline->type != TOK_ID &&
3226 (tline->type != TOK_PREPROC_ID ||
3227 tline->text[1] != '$'))) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08003228 nasm_error(ERR_NONFATAL, "`%%undef' expects a macro identifier");
H. Peter Anvine2c80182005-01-15 22:15:51 +00003229 free_tlist(origline);
3230 return DIRECTIVE_FOUND;
3231 }
3232 if (tline->next) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08003233 nasm_error(ERR_WARNING|ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +00003234 "trailing garbage after macro name ignored");
3235 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003236
H. Peter Anvine2c80182005-01-15 22:15:51 +00003237 /* Find the context that symbol belongs to */
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04003238 ctx = get_ctx(tline->text, &mname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003239 undef_smacro(ctx, mname);
3240 free_tlist(origline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003241 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003242
H. Peter Anvin9e200162008-06-04 17:23:14 -07003243 case PP_DEFSTR:
3244 case PP_IDEFSTR:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003245 casesense = (i == PP_DEFSTR);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003246
3247 tline = tline->next;
3248 skip_white_(tline);
3249 tline = expand_id(tline);
3250 if (!tline || (tline->type != TOK_ID &&
3251 (tline->type != TOK_PREPROC_ID ||
3252 tline->text[1] != '$'))) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08003253 nasm_error(ERR_NONFATAL, "`%s' expects a macro identifier",
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003254 pp_directives[i]);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003255 free_tlist(origline);
3256 return DIRECTIVE_FOUND;
3257 }
3258
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04003259 ctx = get_ctx(tline->text, &mname);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003260 last = tline;
3261 tline = expand_smacro(tline->next);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003262 last->next = NULL;
H. Peter Anvin9e200162008-06-04 17:23:14 -07003263
3264 while (tok_type_(tline, TOK_WHITESPACE))
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003265 tline = delete_Token(tline);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003266
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003267 p = detoken(tline, false);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003268 macro_start = nasm_malloc(sizeof(*macro_start));
3269 macro_start->next = NULL;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003270 macro_start->text = nasm_quote(p, strlen(p));
3271 macro_start->type = TOK_STRING;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003272 macro_start->a.mac = NULL;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003273 nasm_free(p);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003274
3275 /*
3276 * We now have a macro name, an implicit parameter count of
3277 * zero, and a string token to use as an expansion. Create
3278 * and store an SMacro.
3279 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003280 define_smacro(ctx, mname, casesense, 0, macro_start);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003281 free_tlist(origline);
3282 return DIRECTIVE_FOUND;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003283
H. Peter Anvin2f55bda2009-07-14 15:04:04 -04003284 case PP_DEFTOK:
3285 case PP_IDEFTOK:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003286 casesense = (i == PP_DEFTOK);
3287
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003288 tline = tline->next;
3289 skip_white_(tline);
3290 tline = expand_id(tline);
3291 if (!tline || (tline->type != TOK_ID &&
3292 (tline->type != TOK_PREPROC_ID ||
3293 tline->text[1] != '$'))) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08003294 nasm_error(ERR_NONFATAL,
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003295 "`%s' expects a macro identifier as first parameter",
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003296 pp_directives[i]);
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003297 free_tlist(origline);
3298 return DIRECTIVE_FOUND;
3299 }
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04003300 ctx = get_ctx(tline->text, &mname);
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003301 last = tline;
3302 tline = expand_smacro(tline->next);
3303 last->next = NULL;
3304
3305 t = tline;
3306 while (tok_type_(t, TOK_WHITESPACE))
3307 t = t->next;
3308 /* t should now point to the string */
Cyrill Gorcunov6908e582010-09-06 19:36:15 +04003309 if (!tok_type_(t, TOK_STRING)) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08003310 nasm_error(ERR_NONFATAL,
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003311 "`%s` requires string as second parameter",
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003312 pp_directives[i]);
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003313 free_tlist(tline);
3314 free_tlist(origline);
3315 return DIRECTIVE_FOUND;
3316 }
3317
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04003318 /*
3319 * Convert the string to a token stream. Note that smacros
3320 * are stored with the token stream reversed, so we have to
3321 * reverse the output of tokenize().
3322 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003323 nasm_unquote_cstr(t->text, i);
H. Peter Anvinb40992c2010-09-15 08:57:21 -07003324 macro_start = reverse_tokens(tokenize(t->text));
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003325
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003326 /*
3327 * We now have a macro name, an implicit parameter count of
3328 * zero, and a numeric token to use as an expansion. Create
3329 * and store an SMacro.
3330 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003331 define_smacro(ctx, mname, casesense, 0, macro_start);
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003332 free_tlist(tline);
3333 free_tlist(origline);
3334 return DIRECTIVE_FOUND;
H. Peter Anvin9e200162008-06-04 17:23:14 -07003335
H. Peter Anvin418ca702008-05-30 10:42:30 -07003336 case PP_PATHSEARCH:
3337 {
H. Peter Anvinccad6f92016-10-04 00:34:35 -07003338 const char *found_path;
H. Peter Anvin418ca702008-05-30 10:42:30 -07003339
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003340 casesense = true;
H. Peter Anvin418ca702008-05-30 10:42:30 -07003341
3342 tline = tline->next;
3343 skip_white_(tline);
3344 tline = expand_id(tline);
3345 if (!tline || (tline->type != TOK_ID &&
3346 (tline->type != TOK_PREPROC_ID ||
3347 tline->text[1] != '$'))) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08003348 nasm_error(ERR_NONFATAL,
H. Peter Anvin418ca702008-05-30 10:42:30 -07003349 "`%%pathsearch' expects a macro identifier as first parameter");
3350 free_tlist(origline);
3351 return DIRECTIVE_FOUND;
3352 }
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04003353 ctx = get_ctx(tline->text, &mname);
H. Peter Anvin418ca702008-05-30 10:42:30 -07003354 last = tline;
3355 tline = expand_smacro(tline->next);
3356 last->next = NULL;
3357
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003358 t = tline;
H. Peter Anvin418ca702008-05-30 10:42:30 -07003359 while (tok_type_(t, TOK_WHITESPACE))
3360 t = t->next;
3361
3362 if (!t || (t->type != TOK_STRING &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003363 t->type != TOK_INTERNAL_STRING)) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08003364 nasm_error(ERR_NONFATAL, "`%%pathsearch' expects a file name");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003365 free_tlist(tline);
H. Peter Anvin418ca702008-05-30 10:42:30 -07003366 free_tlist(origline);
3367 return DIRECTIVE_FOUND; /* but we did _something_ */
3368 }
3369 if (t->next)
H. Peter Anvin130736c2016-02-17 20:27:41 -08003370 nasm_error(ERR_WARNING|ERR_PASS1,
H. Peter Anvin418ca702008-05-30 10:42:30 -07003371 "trailing garbage after `%%pathsearch' ignored");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003372 p = t->text;
H. Peter Anvin427cc912008-06-01 21:43:03 -07003373 if (t->type != TOK_INTERNAL_STRING)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003374 nasm_unquote(p, NULL);
H. Peter Anvin418ca702008-05-30 10:42:30 -07003375
H. Peter Anvin9924d1e2016-10-04 00:59:39 -07003376 inc_fopen(p, NULL, &found_path, INC_PROBE, NF_BINARY);
H. Peter Anvinccad6f92016-10-04 00:34:35 -07003377 if (!found_path)
3378 found_path = p;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003379 macro_start = nasm_malloc(sizeof(*macro_start));
3380 macro_start->next = NULL;
H. Peter Anvinccad6f92016-10-04 00:34:35 -07003381 macro_start->text = nasm_quote(found_path, strlen(found_path));
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003382 macro_start->type = TOK_STRING;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003383 macro_start->a.mac = NULL;
H. Peter Anvin418ca702008-05-30 10:42:30 -07003384
3385 /*
3386 * We now have a macro name, an implicit parameter count of
3387 * zero, and a string token to use as an expansion. Create
3388 * and store an SMacro.
3389 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003390 define_smacro(ctx, mname, casesense, 0, macro_start);
3391 free_tlist(tline);
H. Peter Anvin418ca702008-05-30 10:42:30 -07003392 free_tlist(origline);
3393 return DIRECTIVE_FOUND;
3394 }
3395
H. Peter Anvine2c80182005-01-15 22:15:51 +00003396 case PP_STRLEN:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003397 casesense = true;
H. Peter Anvin70653092007-10-19 14:42:29 -07003398
H. Peter Anvine2c80182005-01-15 22:15:51 +00003399 tline = tline->next;
3400 skip_white_(tline);
3401 tline = expand_id(tline);
3402 if (!tline || (tline->type != TOK_ID &&
3403 (tline->type != TOK_PREPROC_ID ||
3404 tline->text[1] != '$'))) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08003405 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00003406 "`%%strlen' expects a macro identifier as first parameter");
3407 free_tlist(origline);
3408 return DIRECTIVE_FOUND;
3409 }
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04003410 ctx = get_ctx(tline->text, &mname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003411 last = tline;
3412 tline = expand_smacro(tline->next);
3413 last->next = NULL;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003414
H. Peter Anvine2c80182005-01-15 22:15:51 +00003415 t = tline;
3416 while (tok_type_(t, TOK_WHITESPACE))
3417 t = t->next;
3418 /* t should now point to the string */
Cyrill Gorcunov4e1d5ab2010-07-23 18:51:51 +04003419 if (!tok_type_(t, TOK_STRING)) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08003420 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00003421 "`%%strlen` requires string as second parameter");
3422 free_tlist(tline);
3423 free_tlist(origline);
3424 return DIRECTIVE_FOUND;
3425 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003426
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003427 macro_start = nasm_malloc(sizeof(*macro_start));
3428 macro_start->next = NULL;
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07003429 make_tok_num(macro_start, nasm_unquote(t->text, NULL));
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003430 macro_start->a.mac = NULL;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003431
H. Peter Anvine2c80182005-01-15 22:15:51 +00003432 /*
3433 * We now have a macro name, an implicit parameter count of
3434 * zero, and a numeric token to use as an expansion. Create
3435 * and store an SMacro.
3436 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003437 define_smacro(ctx, mname, casesense, 0, macro_start);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003438 free_tlist(tline);
3439 free_tlist(origline);
3440 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003441
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003442 case PP_STRCAT:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003443 casesense = true;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003444
3445 tline = tline->next;
3446 skip_white_(tline);
3447 tline = expand_id(tline);
3448 if (!tline || (tline->type != TOK_ID &&
3449 (tline->type != TOK_PREPROC_ID ||
3450 tline->text[1] != '$'))) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08003451 nasm_error(ERR_NONFATAL,
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003452 "`%%strcat' expects a macro identifier as first parameter");
3453 free_tlist(origline);
3454 return DIRECTIVE_FOUND;
3455 }
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04003456 ctx = get_ctx(tline->text, &mname);
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003457 last = tline;
3458 tline = expand_smacro(tline->next);
3459 last->next = NULL;
3460
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003461 len = 0;
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04003462 list_for_each(t, tline) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003463 switch (t->type) {
3464 case TOK_WHITESPACE:
3465 break;
3466 case TOK_STRING:
3467 len += t->a.len = nasm_unquote(t->text, NULL);
3468 break;
3469 case TOK_OTHER:
3470 if (!strcmp(t->text, ",")) /* permit comma separators */
3471 break;
3472 /* else fall through */
3473 default:
H. Peter Anvin130736c2016-02-17 20:27:41 -08003474 nasm_error(ERR_NONFATAL,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003475 "non-string passed to `%%strcat' (%d)", t->type);
3476 free_tlist(tline);
3477 free_tlist(origline);
3478 return DIRECTIVE_FOUND;
3479 }
3480 }
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003481
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003482 p = pp = nasm_malloc(len);
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04003483 list_for_each(t, tline) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003484 if (t->type == TOK_STRING) {
3485 memcpy(p, t->text, t->a.len);
3486 p += t->a.len;
3487 }
3488 }
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003489
3490 /*
3491 * We now have a macro name, an implicit parameter count of
3492 * zero, and a numeric token to use as an expansion. Create
3493 * and store an SMacro.
3494 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003495 macro_start = new_Token(NULL, TOK_STRING, NULL, 0);
3496 macro_start->text = nasm_quote(pp, len);
3497 nasm_free(pp);
3498 define_smacro(ctx, mname, casesense, 0, macro_start);
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003499 free_tlist(tline);
3500 free_tlist(origline);
3501 return DIRECTIVE_FOUND;
3502
H. Peter Anvine2c80182005-01-15 22:15:51 +00003503 case PP_SUBSTR:
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003504 {
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003505 int64_t start, count;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003506 size_t len;
H. Peter Anvind2456592008-06-19 15:04:18 -07003507
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003508 casesense = true;
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07003509
H. Peter Anvine2c80182005-01-15 22:15:51 +00003510 tline = tline->next;
3511 skip_white_(tline);
3512 tline = expand_id(tline);
3513 if (!tline || (tline->type != TOK_ID &&
3514 (tline->type != TOK_PREPROC_ID ||
3515 tline->text[1] != '$'))) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08003516 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00003517 "`%%substr' expects a macro identifier as first parameter");
3518 free_tlist(origline);
3519 return DIRECTIVE_FOUND;
3520 }
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04003521 ctx = get_ctx(tline->text, &mname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003522 last = tline;
3523 tline = expand_smacro(tline->next);
3524 last->next = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003525
Cyrill Gorcunov35519d62010-09-06 23:49:52 +04003526 if (tline) /* skip expanded id */
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003527 t = tline->next;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003528 while (tok_type_(t, TOK_WHITESPACE))
3529 t = t->next;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003530
H. Peter Anvine2c80182005-01-15 22:15:51 +00003531 /* t should now point to the string */
Cyrill Gorcunov35519d62010-09-06 23:49:52 +04003532 if (!tok_type_(t, TOK_STRING)) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08003533 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00003534 "`%%substr` requires string as second parameter");
3535 free_tlist(tline);
3536 free_tlist(origline);
3537 return DIRECTIVE_FOUND;
3538 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003539
H. Peter Anvine2c80182005-01-15 22:15:51 +00003540 tt = t->next;
3541 tptr = &tt;
3542 tokval.t_type = TOKEN_INVALID;
H. Peter Anvin130736c2016-02-17 20:27:41 -08003543 evalresult = evaluate(ppscan, tptr, &tokval, NULL, pass, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003544 if (!evalresult) {
3545 free_tlist(tline);
3546 free_tlist(origline);
3547 return DIRECTIVE_FOUND;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003548 } else if (!is_simple(evalresult)) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08003549 nasm_error(ERR_NONFATAL, "non-constant value given to `%%substr`");
H. Peter Anvine2c80182005-01-15 22:15:51 +00003550 free_tlist(tline);
3551 free_tlist(origline);
3552 return DIRECTIVE_FOUND;
3553 }
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003554 start = evalresult->value - 1;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003555
3556 while (tok_type_(tt, TOK_WHITESPACE))
3557 tt = tt->next;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003558 if (!tt) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003559 count = 1; /* Backwards compatibility: one character */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003560 } else {
3561 tokval.t_type = TOKEN_INVALID;
H. Peter Anvin130736c2016-02-17 20:27:41 -08003562 evalresult = evaluate(ppscan, tptr, &tokval, NULL, pass, NULL);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003563 if (!evalresult) {
3564 free_tlist(tline);
3565 free_tlist(origline);
3566 return DIRECTIVE_FOUND;
3567 } else if (!is_simple(evalresult)) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08003568 nasm_error(ERR_NONFATAL, "non-constant value given to `%%substr`");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003569 free_tlist(tline);
3570 free_tlist(origline);
3571 return DIRECTIVE_FOUND;
3572 }
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003573 count = evalresult->value;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003574 }
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003575
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003576 len = nasm_unquote(t->text, NULL);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003577
Cyrill Gorcunovcff031e2010-09-07 20:31:11 +04003578 /* make start and count being in range */
3579 if (start < 0)
3580 start = 0;
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003581 if (count < 0)
3582 count = len + count + 1 - start;
3583 if (start + count > (int64_t)len)
Cyrill Gorcunovcff031e2010-09-07 20:31:11 +04003584 count = len - start;
3585 if (!len || count < 0 || start >=(int64_t)len)
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003586 start = -1, count = 0; /* empty string */
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003587
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003588 macro_start = nasm_malloc(sizeof(*macro_start));
3589 macro_start->next = NULL;
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003590 macro_start->text = nasm_quote((start < 0) ? "" : t->text + start, count);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003591 macro_start->type = TOK_STRING;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003592 macro_start->a.mac = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003593
H. Peter Anvine2c80182005-01-15 22:15:51 +00003594 /*
3595 * We now have a macro name, an implicit parameter count of
3596 * zero, and a numeric token to use as an expansion. Create
3597 * and store an SMacro.
3598 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003599 define_smacro(ctx, mname, casesense, 0, macro_start);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003600 free_tlist(tline);
3601 free_tlist(origline);
3602 return DIRECTIVE_FOUND;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003603 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003604
H. Peter Anvine2c80182005-01-15 22:15:51 +00003605 case PP_ASSIGN:
3606 case PP_IASSIGN:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003607 casesense = (i == PP_ASSIGN);
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07003608
H. Peter Anvine2c80182005-01-15 22:15:51 +00003609 tline = tline->next;
3610 skip_white_(tline);
3611 tline = expand_id(tline);
3612 if (!tline || (tline->type != TOK_ID &&
3613 (tline->type != TOK_PREPROC_ID ||
3614 tline->text[1] != '$'))) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08003615 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00003616 "`%%%sassign' expects a macro identifier",
3617 (i == PP_IASSIGN ? "i" : ""));
3618 free_tlist(origline);
3619 return DIRECTIVE_FOUND;
3620 }
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04003621 ctx = get_ctx(tline->text, &mname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003622 last = tline;
3623 tline = expand_smacro(tline->next);
3624 last->next = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003625
H. Peter Anvine2c80182005-01-15 22:15:51 +00003626 t = tline;
3627 tptr = &t;
3628 tokval.t_type = TOKEN_INVALID;
H. Peter Anvin130736c2016-02-17 20:27:41 -08003629 evalresult = evaluate(ppscan, tptr, &tokval, NULL, pass, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003630 free_tlist(tline);
3631 if (!evalresult) {
3632 free_tlist(origline);
3633 return DIRECTIVE_FOUND;
3634 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003635
H. Peter Anvine2c80182005-01-15 22:15:51 +00003636 if (tokval.t_type)
H. Peter Anvin130736c2016-02-17 20:27:41 -08003637 nasm_error(ERR_WARNING|ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +00003638 "trailing garbage after expression ignored");
H. Peter Anvin734b1882002-04-30 21:01:08 +00003639
H. Peter Anvine2c80182005-01-15 22:15:51 +00003640 if (!is_simple(evalresult)) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08003641 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00003642 "non-constant value given to `%%%sassign'",
3643 (i == PP_IASSIGN ? "i" : ""));
3644 free_tlist(origline);
3645 return DIRECTIVE_FOUND;
3646 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003647
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003648 macro_start = nasm_malloc(sizeof(*macro_start));
3649 macro_start->next = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003650 make_tok_num(macro_start, reloc_value(evalresult));
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003651 macro_start->a.mac = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003652
H. Peter Anvine2c80182005-01-15 22:15:51 +00003653 /*
3654 * We now have a macro name, an implicit parameter count of
3655 * zero, and a numeric token to use as an expansion. Create
3656 * and store an SMacro.
3657 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003658 define_smacro(ctx, mname, casesense, 0, macro_start);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003659 free_tlist(origline);
3660 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003661
H. Peter Anvine2c80182005-01-15 22:15:51 +00003662 case PP_LINE:
3663 /*
3664 * Syntax is `%line nnn[+mmm] [filename]'
3665 */
3666 tline = tline->next;
3667 skip_white_(tline);
3668 if (!tok_type_(tline, TOK_NUMBER)) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08003669 nasm_error(ERR_NONFATAL, "`%%line' expects line number");
H. Peter Anvine2c80182005-01-15 22:15:51 +00003670 free_tlist(origline);
3671 return DIRECTIVE_FOUND;
3672 }
H. Peter Anvin70055962007-10-11 00:05:31 -07003673 k = readnum(tline->text, &err);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003674 m = 1;
3675 tline = tline->next;
3676 if (tok_is_(tline, "+")) {
3677 tline = tline->next;
3678 if (!tok_type_(tline, TOK_NUMBER)) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08003679 nasm_error(ERR_NONFATAL, "`%%line' expects line increment");
H. Peter Anvine2c80182005-01-15 22:15:51 +00003680 free_tlist(origline);
3681 return DIRECTIVE_FOUND;
3682 }
H. Peter Anvin70055962007-10-11 00:05:31 -07003683 m = readnum(tline->text, &err);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003684 tline = tline->next;
3685 }
3686 skip_white_(tline);
3687 src_set_linnum(k);
3688 istk->lineinc = m;
3689 if (tline) {
H. Peter Anvin274cda82016-05-10 02:56:29 -07003690 char *fname = detoken(tline, false);
3691 src_set_fname(fname);
3692 nasm_free(fname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003693 }
3694 free_tlist(origline);
3695 return DIRECTIVE_FOUND;
Keith Kaniosb307a4f2010-11-06 17:41:51 -05003696
H. Peter Anvine2c80182005-01-15 22:15:51 +00003697 default:
H. Peter Anvin130736c2016-02-17 20:27:41 -08003698 nasm_error(ERR_FATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00003699 "preprocessor directive `%s' not yet implemented",
H. Peter Anvin4169a472007-09-12 01:29:43 +00003700 pp_directives[i]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003701 return DIRECTIVE_FOUND;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003702 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003703}
3704
3705/*
H. Peter Anvin76690a12002-04-30 20:52:49 +00003706 * Ensure that a macro parameter contains a condition code and
3707 * nothing else. Return the condition code index if so, or -1
3708 * otherwise.
3709 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003710static int find_cc(Token * t)
H. Peter Anvineba20a72002-04-30 20:53:55 +00003711{
H. Peter Anvin76690a12002-04-30 20:52:49 +00003712 Token *tt;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003713
H. Peter Anvin25a99342007-09-22 17:45:45 -07003714 if (!t)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003715 return -1; /* Probably a %+ without a space */
H. Peter Anvin25a99342007-09-22 17:45:45 -07003716
H. Peter Anvineba20a72002-04-30 20:53:55 +00003717 skip_white_(t);
Cyrill Gorcunov7524cfd2017-10-22 19:01:16 +03003718 if (!t)
3719 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003720 if (t->type != TOK_ID)
H. Peter Anvine2c80182005-01-15 22:15:51 +00003721 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003722 tt = t->next;
H. Peter Anvineba20a72002-04-30 20:53:55 +00003723 skip_white_(tt);
H. Peter Anvin76690a12002-04-30 20:52:49 +00003724 if (tt && (tt->type != TOK_OTHER || strcmp(tt->text, ",")))
H. Peter Anvine2c80182005-01-15 22:15:51 +00003725 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003726
Cyrill Gorcunov19456392012-05-02 00:18:56 +04003727 return bsii(t->text, (const char **)conditions, ARRAY_SIZE(conditions));
H. Peter Anvin76690a12002-04-30 20:52:49 +00003728}
3729
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003730/*
3731 * This routines walks over tokens strem and hadnles tokens
3732 * pasting, if @handle_explicit passed then explicit pasting
3733 * term is handled, otherwise -- implicit pastings only.
3734 */
Cyrill Gorcunov575d4282010-10-06 00:25:55 +04003735static bool paste_tokens(Token **head, const struct tokseq_match *m,
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003736 size_t mnum, bool handle_explicit)
H. Peter Anvind784a082009-04-20 14:01:18 -07003737{
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003738 Token *tok, *next, **prev_next, **prev_nonspace;
3739 bool pasted = false;
3740 char *buf, *p;
3741 size_t len, i;
H. Peter Anvind784a082009-04-20 14:01:18 -07003742
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003743 /*
3744 * The last token before pasting. We need it
3745 * to be able to connect new handled tokens.
3746 * In other words if there were a tokens stream
3747 *
3748 * A -> B -> C -> D
3749 *
3750 * and we've joined tokens B and C, the resulting
3751 * stream should be
3752 *
3753 * A -> BC -> D
3754 */
3755 tok = *head;
3756 prev_next = NULL;
3757
3758 if (!tok_type_(tok, TOK_WHITESPACE) && !tok_type_(tok, TOK_PASTE))
3759 prev_nonspace = head;
3760 else
3761 prev_nonspace = NULL;
3762
3763 while (tok && (next = tok->next)) {
3764
3765 switch (tok->type) {
H. Peter Anvind784a082009-04-20 14:01:18 -07003766 case TOK_WHITESPACE:
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003767 /* Zap redundant whitespaces */
3768 while (tok_type_(next, TOK_WHITESPACE))
3769 next = delete_Token(next);
3770 tok->next = next;
H. Peter Anvind784a082009-04-20 14:01:18 -07003771 break;
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003772
3773 case TOK_PASTE:
3774 /* Explicit pasting */
3775 if (!handle_explicit)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003776 break;
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003777 next = delete_Token(tok);
3778
3779 while (tok_type_(next, TOK_WHITESPACE))
3780 next = delete_Token(next);
3781
3782 if (!pasted)
3783 pasted = true;
3784
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003785 /* Left pasting token is start of line */
3786 if (!prev_nonspace)
H. Peter Anvin130736c2016-02-17 20:27:41 -08003787 nasm_error(ERR_FATAL, "No lvalue found on pasting");
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003788
Cyrill Gorcunov8b5c9fb2013-02-04 01:24:54 +04003789 /*
3790 * No ending token, this might happen in two
3791 * cases
3792 *
3793 * 1) There indeed no right token at all
3794 * 2) There is a bare "%define ID" statement,
3795 * and @ID does expand to whitespace.
3796 *
3797 * So technically we need to do a grammar analysis
3798 * in another stage of parsing, but for now lets don't
3799 * change the behaviour people used to. Simply allow
3800 * whitespace after paste token.
3801 */
3802 if (!next) {
3803 /*
3804 * Zap ending space tokens and that's all.
3805 */
3806 tok = (*prev_nonspace)->next;
3807 while (tok_type_(tok, TOK_WHITESPACE))
3808 tok = delete_Token(tok);
3809 tok = *prev_nonspace;
3810 tok->next = NULL;
3811 break;
3812 }
3813
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003814 tok = *prev_nonspace;
3815 while (tok_type_(tok, TOK_WHITESPACE))
3816 tok = delete_Token(tok);
3817 len = strlen(tok->text);
3818 len += strlen(next->text);
3819
3820 p = buf = nasm_malloc(len + 1);
3821 strcpy(p, tok->text);
3822 p = strchr(p, '\0');
3823 strcpy(p, next->text);
3824
3825 delete_Token(tok);
3826
3827 tok = tokenize(buf);
3828 nasm_free(buf);
3829
3830 *prev_nonspace = tok;
3831 while (tok && tok->next)
3832 tok = tok->next;
3833
3834 tok->next = delete_Token(next);
3835
3836 /* Restart from pasted tokens head */
3837 tok = *prev_nonspace;
3838 break;
3839
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003840 default:
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003841 /* implicit pasting */
Cyrill Gorcunov575d4282010-10-06 00:25:55 +04003842 for (i = 0; i < mnum; i++) {
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003843 if (!(PP_CONCAT_MATCH(tok, m[i].mask_head)))
3844 continue;
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +04003845
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003846 len = 0;
3847 while (next && PP_CONCAT_MATCH(next, m[i].mask_tail)) {
3848 len += strlen(next->text);
3849 next = next->next;
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +04003850 }
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003851
Cyrill Gorcunov6f8109e2017-10-22 21:26:36 +03003852 /* No match or no text to process */
3853 if (tok == next || len == 0)
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003854 break;
3855
3856 len += strlen(tok->text);
3857 p = buf = nasm_malloc(len + 1);
3858
Adam Majer1a069432017-07-25 11:12:35 +02003859 strcpy(p, tok->text);
3860 p = strchr(p, '\0');
3861 tok = delete_Token(tok);
3862
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003863 while (tok != next) {
Adam Majer1a069432017-07-25 11:12:35 +02003864 if (PP_CONCAT_MATCH(tok, m[i].mask_tail)) {
3865 strcpy(p, tok->text);
3866 p = strchr(p, '\0');
3867 }
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003868 tok = delete_Token(tok);
3869 }
3870
3871 tok = tokenize(buf);
3872 nasm_free(buf);
3873
3874 if (prev_next)
3875 *prev_next = tok;
3876 else
3877 *head = tok;
3878
3879 /*
3880 * Connect pasted into original stream,
3881 * ie A -> new-tokens -> B
3882 */
3883 while (tok && tok->next)
3884 tok = tok->next;
3885 tok->next = next;
3886
3887 if (!pasted)
3888 pasted = true;
3889
3890 /* Restart from pasted tokens head */
3891 tok = prev_next ? *prev_next : *head;
Cyrill Gorcunov575d4282010-10-06 00:25:55 +04003892 }
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003893
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003894 break;
H. Peter Anvind784a082009-04-20 14:01:18 -07003895 }
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003896
3897 prev_next = &tok->next;
3898
3899 if (tok->next &&
3900 !tok_type_(tok->next, TOK_WHITESPACE) &&
3901 !tok_type_(tok->next, TOK_PASTE))
3902 prev_nonspace = prev_next;
3903
3904 tok = tok->next;
H. Peter Anvind784a082009-04-20 14:01:18 -07003905 }
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003906
3907 return pasted;
H. Peter Anvind784a082009-04-20 14:01:18 -07003908}
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003909
3910/*
3911 * expands to a list of tokens from %{x:y}
3912 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003913static Token *expand_mmac_params_range(MMacro *mac, Token *tline, Token ***last)
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003914{
3915 Token *t = tline, **tt, *tm, *head;
3916 char *pos;
3917 int fst, lst, j, i;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003918
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003919 pos = strchr(tline->text, ':');
3920 nasm_assert(pos);
3921
3922 lst = atoi(pos + 1);
3923 fst = atoi(tline->text + 1);
3924
3925 /*
3926 * only macros params are accounted so
3927 * if someone passes %0 -- we reject such
3928 * value(s)
3929 */
3930 if (lst == 0 || fst == 0)
3931 goto err;
3932
3933 /* the values should be sane */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003934 if ((fst > (int)mac->nparam || fst < (-(int)mac->nparam)) ||
3935 (lst > (int)mac->nparam || lst < (-(int)mac->nparam)))
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003936 goto err;
3937
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003938 fst = fst < 0 ? fst + (int)mac->nparam + 1: fst;
3939 lst = lst < 0 ? lst + (int)mac->nparam + 1: lst;
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003940
3941 /* counted from zero */
3942 fst--, lst--;
3943
3944 /*
Cyrill Gorcunove75331c2013-11-09 12:02:15 +04003945 * It will be at least one token. Note we
3946 * need to scan params until separator, otherwise
3947 * only first token will be passed.
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003948 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003949 tm = mac->params[(fst + mac->rotate) % mac->nparam];
Cyrill Gorcunove75331c2013-11-09 12:02:15 +04003950 head = new_Token(NULL, tm->type, tm->text, 0);
3951 tt = &head->next, tm = tm->next;
3952 while (tok_isnt_(tm, ",")) {
3953 t = new_Token(NULL, tm->type, tm->text, 0);
3954 *tt = t, tt = &t->next, tm = tm->next;
3955 }
3956
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003957 if (fst < lst) {
3958 for (i = fst + 1; i <= lst; i++) {
3959 t = new_Token(NULL, TOK_OTHER, ",", 0);
3960 *tt = t, tt = &t->next;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003961 j = (i + mac->rotate) % mac->nparam;
3962 tm = mac->params[j];
Cyrill Gorcunove75331c2013-11-09 12:02:15 +04003963 while (tok_isnt_(tm, ",")) {
3964 t = new_Token(NULL, tm->type, tm->text, 0);
3965 *tt = t, tt = &t->next, tm = tm->next;
3966 }
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003967 }
3968 } else {
3969 for (i = fst - 1; i >= lst; i--) {
3970 t = new_Token(NULL, TOK_OTHER, ",", 0);
3971 *tt = t, tt = &t->next;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003972 j = (i + mac->rotate) % mac->nparam;
3973 tm = mac->params[j];
Cyrill Gorcunove75331c2013-11-09 12:02:15 +04003974 while (tok_isnt_(tm, ",")) {
3975 t = new_Token(NULL, tm->type, tm->text, 0);
3976 *tt = t, tt = &t->next, tm = tm->next;
3977 }
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003978 }
3979 }
3980
3981 *last = tt;
3982 return head;
3983
3984err:
H. Peter Anvin130736c2016-02-17 20:27:41 -08003985 nasm_error(ERR_NONFATAL, "`%%{%s}': macro parameters out of range",
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003986 &tline->text[1]);
3987 return tline;
3988}
3989
H. Peter Anvin76690a12002-04-30 20:52:49 +00003990/*
3991 * Expand MMacro-local things: parameter references (%0, %n, %+n,
H. Peter Anvin67c63722008-10-26 23:49:00 -07003992 * %-n) and MMacro-local identifiers (%%foo) as well as
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003993 * macro indirection (%[...]) and range (%{..:..}).
H. Peter Anvin76690a12002-04-30 20:52:49 +00003994 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003995static Token *expand_mmac_params(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00003996{
H. Peter Anvin734b1882002-04-30 21:01:08 +00003997 Token *t, *tt, **tail, *thead;
H. Peter Anvin6125b622009-04-08 14:02:25 -07003998 bool changed = false;
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003999 char *pos;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004000
4001 tail = &thead;
4002 thead = NULL;
4003
H. Peter Anvine2c80182005-01-15 22:15:51 +00004004 while (tline) {
4005 if (tline->type == TOK_PREPROC_ID &&
Cyrill Gorcunovca611192010-06-04 09:22:12 +04004006 (((tline->text[1] == '+' || tline->text[1] == '-') && tline->text[2]) ||
4007 (tline->text[1] >= '0' && tline->text[1] <= '9') ||
4008 tline->text[1] == '%')) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004009 char *text = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004010 int type = 0, cc; /* type = 0 to placate optimisers */
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004011 char tmpbuf[30];
H. Peter Anvin25a99342007-09-22 17:45:45 -07004012 unsigned int n;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004013 int i;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004014 MMacro *mac;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004015
H. Peter Anvine2c80182005-01-15 22:15:51 +00004016 t = tline;
4017 tline = tline->next;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004018
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004019 mac = istk->mstk;
4020 while (mac && !mac->name) /* avoid mistaking %reps for macros */
4021 mac = mac->next_active;
4022 if (!mac) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08004023 nasm_error(ERR_NONFATAL, "`%s': not in a macro call", t->text);
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004024 } else {
4025 pos = strchr(t->text, ':');
4026 if (!pos) {
4027 switch (t->text[1]) {
4028 /*
4029 * We have to make a substitution of one of the
4030 * forms %1, %-1, %+1, %%foo, %0.
4031 */
4032 case '0':
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004033 type = TOK_NUMBER;
4034 snprintf(tmpbuf, sizeof(tmpbuf), "%d", mac->nparam);
4035 text = nasm_strdup(tmpbuf);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004036 break;
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004037 case '%':
H. Peter Anvine2c80182005-01-15 22:15:51 +00004038 type = TOK_ID;
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004039 snprintf(tmpbuf, sizeof(tmpbuf), "..@%"PRIu64".",
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004040 mac->unique);
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004041 text = nasm_strcat(tmpbuf, t->text + 2);
4042 break;
4043 case '-':
4044 n = atoi(t->text + 2) - 1;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004045 if (n >= mac->nparam)
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004046 tt = NULL;
4047 else {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004048 if (mac->nparam > 1)
4049 n = (n + mac->rotate) % mac->nparam;
4050 tt = mac->params[n];
H. Peter Anvine2c80182005-01-15 22:15:51 +00004051 }
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004052 cc = find_cc(tt);
4053 if (cc == -1) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08004054 nasm_error(ERR_NONFATAL,
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004055 "macro parameter %d is not a condition code",
4056 n + 1);
4057 text = NULL;
4058 } else {
4059 type = TOK_ID;
4060 if (inverse_ccs[cc] == -1) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08004061 nasm_error(ERR_NONFATAL,
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004062 "condition code `%s' is not invertible",
4063 conditions[cc]);
4064 text = NULL;
4065 } else
4066 text = nasm_strdup(conditions[inverse_ccs[cc]]);
4067 }
4068 break;
4069 case '+':
4070 n = atoi(t->text + 2) - 1;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004071 if (n >= mac->nparam)
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004072 tt = NULL;
4073 else {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004074 if (mac->nparam > 1)
4075 n = (n + mac->rotate) % mac->nparam;
4076 tt = mac->params[n];
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004077 }
4078 cc = find_cc(tt);
4079 if (cc == -1) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08004080 nasm_error(ERR_NONFATAL,
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004081 "macro parameter %d is not a condition code",
4082 n + 1);
4083 text = NULL;
4084 } else {
4085 type = TOK_ID;
4086 text = nasm_strdup(conditions[cc]);
4087 }
4088 break;
4089 default:
4090 n = atoi(t->text + 1) - 1;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004091 if (n >= mac->nparam)
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004092 tt = NULL;
4093 else {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004094 if (mac->nparam > 1)
4095 n = (n + mac->rotate) % mac->nparam;
4096 tt = mac->params[n];
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004097 }
4098 if (tt) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004099 for (i = 0; i < mac->paramlen[n]; i++) {
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004100 *tail = new_Token(NULL, tt->type, tt->text, 0);
4101 tail = &(*tail)->next;
4102 tt = tt->next;
4103 }
4104 }
4105 text = NULL; /* we've done it here */
4106 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004107 }
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004108 } else {
4109 /*
4110 * seems we have a parameters range here
4111 */
4112 Token *head, **last;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004113 head = expand_mmac_params_range(mac, t, &last);
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004114 if (head != t) {
4115 *tail = head;
4116 *last = tline;
4117 tline = head;
4118 text = NULL;
4119 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00004120 }
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004121 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00004122 if (!text) {
4123 delete_Token(t);
4124 } else {
4125 *tail = t;
4126 tail = &t->next;
4127 t->type = type;
4128 nasm_free(t->text);
4129 t->text = text;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004130 t->a.mac = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004131 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004132 changed = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004133 continue;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004134 } else if (tline->type == TOK_INDIRECT) {
4135 t = tline;
4136 tline = tline->next;
4137 tt = tokenize(t->text);
4138 tt = expand_mmac_params(tt);
4139 tt = expand_smacro(tt);
4140 *tail = tt;
4141 while (tt) {
4142 tt->a.mac = NULL; /* Necessary? */
4143 tail = &tt->next;
4144 tt = tt->next;
4145 }
4146 delete_Token(t);
4147 changed = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004148 } else {
4149 t = *tail = tline;
4150 tline = tline->next;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004151 t->a.mac = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004152 tail = &t->next;
4153 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00004154 }
H. Peter Anvineba20a72002-04-30 20:53:55 +00004155 *tail = NULL;
H. Peter Anvin67c63722008-10-26 23:49:00 -07004156
Cyrill Gorcunovc6a742c2011-06-27 01:23:09 +04004157 if (changed) {
4158 const struct tokseq_match t[] = {
4159 {
4160 PP_CONCAT_MASK(TOK_ID) |
4161 PP_CONCAT_MASK(TOK_FLOAT), /* head */
4162 PP_CONCAT_MASK(TOK_ID) |
4163 PP_CONCAT_MASK(TOK_NUMBER) |
4164 PP_CONCAT_MASK(TOK_FLOAT) |
4165 PP_CONCAT_MASK(TOK_OTHER) /* tail */
4166 },
4167 {
4168 PP_CONCAT_MASK(TOK_NUMBER), /* head */
4169 PP_CONCAT_MASK(TOK_NUMBER) /* tail */
4170 }
4171 };
4172 paste_tokens(&thead, t, ARRAY_SIZE(t), false);
4173 }
H. Peter Anvin6125b622009-04-08 14:02:25 -07004174
H. Peter Anvin76690a12002-04-30 20:52:49 +00004175 return thead;
4176}
4177
4178/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004179 * Expand all single-line macro calls made in the given line.
4180 * Return the expanded version of the line. The original is deemed
4181 * to be destroyed in the process. (In reality we'll just move
4182 * Tokens from input to output a lot of the time, rather than
4183 * actually bothering to destroy and replicate.)
4184 */
H. Peter Anvincb1cf592007-11-19 12:26:50 -08004185
H. Peter Anvine2c80182005-01-15 22:15:51 +00004186static Token *expand_smacro(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004187{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004188 Token *t, *tt, *mstart, **tail, *thead;
H. Peter Anvineba20a72002-04-30 20:53:55 +00004189 SMacro *head = NULL, *m;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004190 Token **params;
4191 int *paramsize;
H. Peter Anvin25a99342007-09-22 17:45:45 -07004192 unsigned int nparam, sparam;
H. Peter Anvind784a082009-04-20 14:01:18 -07004193 int brackets;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004194 Token *org_tline = tline;
4195 Context *ctx;
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08004196 const char *mname;
H. Peter Anvin2a15e692007-11-19 13:14:59 -08004197 int deadman = DEADMAN_LIMIT;
H. Peter Anvin8287daf2009-07-07 16:00:58 -07004198 bool expanded;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004199
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004200 /*
4201 * Trick: we should avoid changing the start token pointer since it can
4202 * be contained in "next" field of other token. Because of this
4203 * we allocate a copy of first token and work with it; at the end of
4204 * routine we copy it back
4205 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004206 if (org_tline) {
Cyrill Gorcunoved4a8052010-04-09 15:40:35 +04004207 tline = new_Token(org_tline->next, org_tline->type,
4208 org_tline->text, 0);
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004209 tline->a.mac = org_tline->a.mac;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004210 nasm_free(org_tline->text);
4211 org_tline->text = NULL;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004212 }
4213
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004214 expanded = true; /* Always expand %+ at least once */
H. Peter Anvin8287daf2009-07-07 16:00:58 -07004215
H. Peter Anvincb1cf592007-11-19 12:26:50 -08004216again:
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004217 thead = NULL;
Cyrill Gorcunoved4a8052010-04-09 15:40:35 +04004218 tail = &thead;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004219
H. Peter Anvine2c80182005-01-15 22:15:51 +00004220 while (tline) { /* main token loop */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004221 if (!--deadman) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08004222 nasm_error(ERR_NONFATAL, "interminable macro recursion");
Cyrill Gorcunovbd38c8f2009-11-21 11:11:23 +03004223 goto err;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004224 }
H. Peter Anvincb1cf592007-11-19 12:26:50 -08004225
H. Peter Anvine2c80182005-01-15 22:15:51 +00004226 if ((mname = tline->text)) {
4227 /* if this token is a local macro, look in local context */
Cyrill Gorcunovc56d9ad2010-02-11 15:12:19 +03004228 if (tline->type == TOK_ID) {
4229 head = (SMacro *)hash_findix(&smacros, mname);
4230 } else if (tline->type == TOK_PREPROC_ID) {
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04004231 ctx = get_ctx(mname, &mname);
Cyrill Gorcunovc56d9ad2010-02-11 15:12:19 +03004232 head = ctx ? (SMacro *)hash_findix(&ctx->localmac, mname) : NULL;
4233 } else
4234 head = NULL;
H. Peter Anvin072771e2008-05-22 13:17:51 -07004235
H. Peter Anvine2c80182005-01-15 22:15:51 +00004236 /*
4237 * We've hit an identifier. As in is_mmacro below, we first
4238 * check whether the identifier is a single-line macro at
4239 * all, then think about checking for parameters if
4240 * necessary.
4241 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004242 list_for_each(m, head)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004243 if (!mstrcmp(m->name, mname, m->casesense))
4244 break;
4245 if (m) {
4246 mstart = tline;
4247 params = NULL;
4248 paramsize = NULL;
4249 if (m->nparam == 0) {
4250 /*
4251 * Simple case: the macro is parameterless. Discard the
4252 * one token that the macro call took, and push the
4253 * expansion back on the to-do stack.
4254 */
4255 if (!m->expansion) {
4256 if (!strcmp("__FILE__", m->name)) {
H. Peter Anvin274cda82016-05-10 02:56:29 -07004257 const char *file = src_get_fname();
4258 /* nasm_free(tline->text); here? */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004259 tline->text = nasm_quote(file, strlen(file));
4260 tline->type = TOK_STRING;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004261 continue;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004262 }
4263 if (!strcmp("__LINE__", m->name)) {
4264 nasm_free(tline->text);
4265 make_tok_num(tline, src_get_linnum());
4266 continue;
4267 }
4268 if (!strcmp("__BITS__", m->name)) {
4269 nasm_free(tline->text);
4270 make_tok_num(tline, globalbits);
4271 continue;
4272 }
4273 tline = delete_Token(tline);
4274 continue;
4275 }
4276 } else {
4277 /*
4278 * Complicated case: at least one macro with this name
H. Peter Anvine2c80182005-01-15 22:15:51 +00004279 * exists and takes parameters. We must find the
4280 * parameters in the call, count them, find the SMacro
4281 * that corresponds to that form of the macro call, and
4282 * substitute for the parameters when we expand. What a
4283 * pain.
4284 */
4285 /*tline = tline->next;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004286 skip_white_(tline); */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004287 do {
4288 t = tline->next;
4289 while (tok_type_(t, TOK_SMAC_END)) {
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004290 t->a.mac->in_progress = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004291 t->text = NULL;
4292 t = tline->next = delete_Token(t);
4293 }
4294 tline = t;
4295 } while (tok_type_(tline, TOK_WHITESPACE));
4296 if (!tok_is_(tline, "(")) {
4297 /*
4298 * This macro wasn't called with parameters: ignore
4299 * the call. (Behaviour borrowed from gnu cpp.)
4300 */
4301 tline = mstart;
4302 m = NULL;
4303 } else {
4304 int paren = 0;
4305 int white = 0;
4306 brackets = 0;
4307 nparam = 0;
4308 sparam = PARAM_DELTA;
4309 params = nasm_malloc(sparam * sizeof(Token *));
4310 params[0] = tline->next;
4311 paramsize = nasm_malloc(sparam * sizeof(int));
4312 paramsize[0] = 0;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07004313 while (true) { /* parameter loop */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004314 /*
4315 * For some unusual expansions
4316 * which concatenates function call
4317 */
4318 t = tline->next;
4319 while (tok_type_(t, TOK_SMAC_END)) {
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004320 t->a.mac->in_progress = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004321 t->text = NULL;
4322 t = tline->next = delete_Token(t);
4323 }
4324 tline = t;
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00004325
H. Peter Anvine2c80182005-01-15 22:15:51 +00004326 if (!tline) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08004327 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00004328 "macro call expects terminating `)'");
4329 break;
4330 }
4331 if (tline->type == TOK_WHITESPACE
4332 && brackets <= 0) {
4333 if (paramsize[nparam])
4334 white++;
4335 else
4336 params[nparam] = tline->next;
4337 continue; /* parameter loop */
4338 }
4339 if (tline->type == TOK_OTHER
4340 && tline->text[1] == 0) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004341 char ch = tline->text[0];
H. Peter Anvine2c80182005-01-15 22:15:51 +00004342 if (ch == ',' && !paren && brackets <= 0) {
4343 if (++nparam >= sparam) {
4344 sparam += PARAM_DELTA;
4345 params = nasm_realloc(params,
Cyrill Gorcunoved4a8052010-04-09 15:40:35 +04004346 sparam * sizeof(Token *));
4347 paramsize = nasm_realloc(paramsize,
4348 sparam * sizeof(int));
H. Peter Anvine2c80182005-01-15 22:15:51 +00004349 }
4350 params[nparam] = tline->next;
4351 paramsize[nparam] = 0;
4352 white = 0;
4353 continue; /* parameter loop */
4354 }
4355 if (ch == '{' &&
4356 (brackets > 0 || (brackets == 0 &&
4357 !paramsize[nparam])))
4358 {
4359 if (!(brackets++)) {
4360 params[nparam] = tline->next;
4361 continue; /* parameter loop */
4362 }
4363 }
4364 if (ch == '}' && brackets > 0)
4365 if (--brackets == 0) {
4366 brackets = -1;
4367 continue; /* parameter loop */
4368 }
4369 if (ch == '(' && !brackets)
4370 paren++;
4371 if (ch == ')' && brackets <= 0)
4372 if (--paren < 0)
4373 break;
4374 }
4375 if (brackets < 0) {
4376 brackets = 0;
H. Peter Anvin130736c2016-02-17 20:27:41 -08004377 nasm_error(ERR_NONFATAL, "braces do not "
H. Peter Anvine2c80182005-01-15 22:15:51 +00004378 "enclose all of macro parameter");
4379 }
4380 paramsize[nparam] += white + 1;
4381 white = 0;
4382 } /* parameter loop */
4383 nparam++;
4384 while (m && (m->nparam != nparam ||
4385 mstrcmp(m->name, mname,
4386 m->casesense)))
4387 m = m->next;
4388 if (!m)
H. Peter Anvin130736c2016-02-17 20:27:41 -08004389 nasm_error(ERR_WARNING|ERR_PASS1|ERR_WARN_MNP,
H. Peter Anvine2c80182005-01-15 22:15:51 +00004390 "macro `%s' exists, "
4391 "but not taking %d parameters",
4392 mstart->text, nparam);
4393 }
4394 }
4395 if (m && m->in_progress)
4396 m = NULL;
4397 if (!m) { /* in progess or didn't find '(' or wrong nparam */
H. Peter Anvin70653092007-10-19 14:42:29 -07004398 /*
H. Peter Anvine2c80182005-01-15 22:15:51 +00004399 * Design question: should we handle !tline, which
4400 * indicates missing ')' here, or expand those
4401 * macros anyway, which requires the (t) test a few
H. Peter Anvin70653092007-10-19 14:42:29 -07004402 * lines down?
H. Peter Anvine2c80182005-01-15 22:15:51 +00004403 */
4404 nasm_free(params);
4405 nasm_free(paramsize);
4406 tline = mstart;
4407 } else {
4408 /*
4409 * Expand the macro: we are placed on the last token of the
4410 * call, so that we can easily split the call from the
4411 * following tokens. We also start by pushing an SMAC_END
4412 * token for the cycle removal.
4413 */
4414 t = tline;
4415 if (t) {
4416 tline = t->next;
4417 t->next = NULL;
4418 }
4419 tt = new_Token(tline, TOK_SMAC_END, NULL, 0);
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004420 tt->a.mac = m;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07004421 m->in_progress = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004422 tline = tt;
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04004423 list_for_each(t, m->expansion) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004424 if (t->type >= TOK_SMAC_PARAM) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00004425 Token *pcopy = tline, **ptail = &pcopy;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004426 Token *ttt, *pt;
4427 int i;
H. Peter Anvin734b1882002-04-30 21:01:08 +00004428
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004429 ttt = params[t->type - TOK_SMAC_PARAM];
4430 i = paramsize[t->type - TOK_SMAC_PARAM];
4431 while (--i >= 0) {
4432 pt = *ptail = new_Token(tline, ttt->type,
4433 ttt->text, 0);
4434 ptail = &pt->next;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004435 ttt = ttt->next;
Cyrill Gorcunov59ce1c62017-10-22 18:42:07 +03004436 if (!ttt && i > 0) {
4437 /*
4438 * FIXME: Need to handle more gracefully,
4439 * exiting early on agruments analysis.
4440 */
4441 nasm_error(ERR_FATAL,
4442 "macro `%s' expects %d args",
4443 mstart->text,
4444 (int)paramsize[t->type - TOK_SMAC_PARAM]);
4445 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00004446 }
4447 tline = pcopy;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004448 } else if (t->type == TOK_PREPROC_Q) {
4449 tt = new_Token(tline, TOK_ID, mname, 0);
4450 tline = tt;
4451 } else if (t->type == TOK_PREPROC_QQ) {
4452 tt = new_Token(tline, TOK_ID, m->name, 0);
4453 tline = tt;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004454 } else {
4455 tt = new_Token(tline, t->type, t->text, 0);
4456 tline = tt;
4457 }
4458 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00004459
H. Peter Anvine2c80182005-01-15 22:15:51 +00004460 /*
4461 * Having done that, get rid of the macro call, and clean
4462 * up the parameters.
4463 */
4464 nasm_free(params);
4465 nasm_free(paramsize);
4466 free_tlist(mstart);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004467 expanded = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004468 continue; /* main token loop */
4469 }
4470 }
4471 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004472
H. Peter Anvine2c80182005-01-15 22:15:51 +00004473 if (tline->type == TOK_SMAC_END) {
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004474 tline->a.mac->in_progress = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004475 tline = delete_Token(tline);
4476 } else {
4477 t = *tail = tline;
4478 tline = tline->next;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004479 t->a.mac = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004480 t->next = NULL;
4481 tail = &t->next;
4482 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004483 }
4484
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004485 /*
4486 * Now scan the entire line and look for successive TOK_IDs that resulted
Keith Kaniosb7a89542007-04-12 02:40:54 +00004487 * after expansion (they can't be produced by tokenize()). The successive
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004488 * TOK_IDs should be concatenated.
4489 * Also we look for %+ tokens and concatenate the tokens before and after
4490 * them (without white spaces in between).
4491 */
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +04004492 if (expanded) {
Cyrill Gorcunovc6a742c2011-06-27 01:23:09 +04004493 const struct tokseq_match t[] = {
4494 {
4495 PP_CONCAT_MASK(TOK_ID) |
4496 PP_CONCAT_MASK(TOK_PREPROC_ID), /* head */
4497 PP_CONCAT_MASK(TOK_ID) |
4498 PP_CONCAT_MASK(TOK_PREPROC_ID) |
4499 PP_CONCAT_MASK(TOK_NUMBER) /* tail */
4500 }
4501 };
4502 if (paste_tokens(&thead, t, ARRAY_SIZE(t), true)) {
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +04004503 /*
4504 * If we concatenated something, *and* we had previously expanded
4505 * an actual macro, scan the lines again for macros...
4506 */
4507 tline = thead;
4508 expanded = false;
4509 goto again;
4510 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00004511 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004512
Cyrill Gorcunovbd38c8f2009-11-21 11:11:23 +03004513err:
H. Peter Anvine2c80182005-01-15 22:15:51 +00004514 if (org_tline) {
4515 if (thead) {
4516 *org_tline = *thead;
4517 /* since we just gave text to org_line, don't free it */
4518 thead->text = NULL;
4519 delete_Token(thead);
4520 } else {
4521 /* the expression expanded to empty line;
4522 we can't return NULL for some reasons
4523 we just set the line to a single WHITESPACE token. */
4524 memset(org_tline, 0, sizeof(*org_tline));
4525 org_tline->text = NULL;
4526 org_tline->type = TOK_WHITESPACE;
4527 }
4528 thead = org_tline;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004529 }
4530
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004531 return thead;
4532}
4533
4534/*
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004535 * Similar to expand_smacro but used exclusively with macro identifiers
4536 * right before they are fetched in. The reason is that there can be
4537 * identifiers consisting of several subparts. We consider that if there
4538 * are more than one element forming the name, user wants a expansion,
4539 * otherwise it will be left as-is. Example:
4540 *
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004541 * %define %$abc cde
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004542 *
4543 * the identifier %$abc will be left as-is so that the handler for %define
4544 * will suck it and define the corresponding value. Other case:
4545 *
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004546 * %define _%$abc cde
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004547 *
4548 * In this case user wants name to be expanded *before* %define starts
4549 * working, so we'll expand %$abc into something (if it has a value;
4550 * otherwise it will be left as-is) then concatenate all successive
4551 * PP_IDs into one.
4552 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004553static Token *expand_id(Token * tline)
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004554{
4555 Token *cur, *oldnext = NULL;
4556
H. Peter Anvin734b1882002-04-30 21:01:08 +00004557 if (!tline || !tline->next)
H. Peter Anvine2c80182005-01-15 22:15:51 +00004558 return tline;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004559
4560 cur = tline;
4561 while (cur->next &&
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004562 (cur->next->type == TOK_ID ||
4563 cur->next->type == TOK_PREPROC_ID
4564 || cur->next->type == TOK_NUMBER))
H. Peter Anvine2c80182005-01-15 22:15:51 +00004565 cur = cur->next;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004566
4567 /* If identifier consists of just one token, don't expand */
4568 if (cur == tline)
H. Peter Anvine2c80182005-01-15 22:15:51 +00004569 return tline;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004570
H. Peter Anvine2c80182005-01-15 22:15:51 +00004571 if (cur) {
4572 oldnext = cur->next; /* Detach the tail past identifier */
4573 cur->next = NULL; /* so that expand_smacro stops here */
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004574 }
4575
H. Peter Anvin734b1882002-04-30 21:01:08 +00004576 tline = expand_smacro(tline);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004577
H. Peter Anvine2c80182005-01-15 22:15:51 +00004578 if (cur) {
4579 /* expand_smacro possibly changhed tline; re-scan for EOL */
4580 cur = tline;
4581 while (cur && cur->next)
4582 cur = cur->next;
4583 if (cur)
4584 cur->next = oldnext;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004585 }
4586
4587 return tline;
4588}
4589
4590/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004591 * Determine whether the given line constitutes a multi-line macro
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004592 * call, and return the MMacro structure called if so. Doesn't have
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004593 * to check for an initial label - that's taken care of in
4594 * expand_mmacro - but must check numbers of parameters. Guaranteed
4595 * to be called with tline->type == TOK_ID, so the putative macro
4596 * name is easy to find.
4597 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004598static MMacro *is_mmacro(Token * tline, Token *** params_array)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004599{
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004600 MMacro *head, *m;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004601 Token **params;
4602 int nparam;
4603
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004604 head = (MMacro *) hash_findix(&mmacros, tline->text);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004605
4606 /*
4607 * Efficiency: first we see if any macro exists with the given
4608 * name. If not, we can return NULL immediately. _Then_ we
4609 * count the parameters, and then we look further along the
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004610 * list if necessary to find the proper MMacro.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004611 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004612 list_for_each(m, head)
4613 if (!mstrcmp(m->name, tline->text, m->casesense))
H. Peter Anvine2c80182005-01-15 22:15:51 +00004614 break;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004615 if (!m)
H. Peter Anvine2c80182005-01-15 22:15:51 +00004616 return NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004617
4618 /*
4619 * OK, we have a potential macro. Count and demarcate the
4620 * parameters.
4621 */
H. Peter Anvin734b1882002-04-30 21:01:08 +00004622 count_mmac_params(tline->next, &nparam, &params);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004623
4624 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004625 * So we know how many parameters we've got. Find the MMacro
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004626 * structure that handles this number.
4627 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004628 while (m) {
4629 if (m->nparam_min <= nparam
4630 && (m->plus || nparam <= m->nparam_max)) {
4631 /*
4632 * This one is right. Just check if cycle removal
4633 * prohibits us using it before we actually celebrate...
4634 */
4635 if (m->in_progress > m->max_depth) {
4636 if (m->max_depth > 0) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08004637 nasm_error(ERR_WARNING,
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004638 "reached maximum recursion depth of %i",
4639 m->max_depth);
4640 }
4641 nasm_free(params);
4642 return NULL;
4643 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00004644 /*
4645 * It's right, and we can use it. Add its default
4646 * parameters to the end of our list if necessary.
4647 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004648 if (m->defaults && nparam < m->nparam_min + m->ndefs) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00004649 params =
4650 nasm_realloc(params,
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004651 ((m->nparam_min + m->ndefs +
H. Peter Anvine2c80182005-01-15 22:15:51 +00004652 1) * sizeof(*params)));
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004653 while (nparam < m->nparam_min + m->ndefs) {
4654 params[nparam] = m->defaults[nparam - m->nparam_min];
H. Peter Anvine2c80182005-01-15 22:15:51 +00004655 nparam++;
4656 }
4657 }
4658 /*
4659 * If we've gone over the maximum parameter count (and
4660 * we're in Plus mode), ignore parameters beyond
4661 * nparam_max.
4662 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004663 if (m->plus && nparam > m->nparam_max)
4664 nparam = m->nparam_max;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004665 /*
4666 * Then terminate the parameter list, and leave.
4667 */
4668 if (!params) { /* need this special case */
4669 params = nasm_malloc(sizeof(*params));
4670 nparam = 0;
4671 }
4672 params[nparam] = NULL;
4673 *params_array = params;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004674 return m;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004675 }
4676 /*
4677 * This one wasn't right: look for the next one with the
4678 * same name.
4679 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004680 list_for_each(m, m->next)
4681 if (!mstrcmp(m->name, tline->text, m->casesense))
H. Peter Anvine2c80182005-01-15 22:15:51 +00004682 break;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004683 }
4684
4685 /*
4686 * After all that, we didn't find one with the right number of
4687 * parameters. Issue a warning, and fail to expand the macro.
4688 */
H. Peter Anvin130736c2016-02-17 20:27:41 -08004689 nasm_error(ERR_WARNING|ERR_PASS1|ERR_WARN_MNP,
H. Peter Anvine2c80182005-01-15 22:15:51 +00004690 "macro `%s' exists, but not taking %d parameters",
4691 tline->text, nparam);
H. Peter Anvin734b1882002-04-30 21:01:08 +00004692 nasm_free(params);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004693 return NULL;
4694}
4695
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004696
4697/*
4698 * Save MMacro invocation specific fields in
4699 * preparation for a recursive macro expansion
4700 */
4701static void push_mmacro(MMacro *m)
4702{
4703 MMacroInvocation *i;
4704
4705 i = nasm_malloc(sizeof(MMacroInvocation));
4706 i->prev = m->prev;
4707 i->params = m->params;
4708 i->iline = m->iline;
4709 i->nparam = m->nparam;
4710 i->rotate = m->rotate;
4711 i->paramlen = m->paramlen;
4712 i->unique = m->unique;
4713 i->condcnt = m->condcnt;
4714 m->prev = i;
4715}
4716
4717
4718/*
4719 * Restore MMacro invocation specific fields that were
4720 * saved during a previous recursive macro expansion
4721 */
4722static void pop_mmacro(MMacro *m)
4723{
4724 MMacroInvocation *i;
4725
4726 if (m->prev) {
4727 i = m->prev;
4728 m->prev = i->prev;
4729 m->params = i->params;
4730 m->iline = i->iline;
4731 m->nparam = i->nparam;
4732 m->rotate = i->rotate;
4733 m->paramlen = i->paramlen;
4734 m->unique = i->unique;
4735 m->condcnt = i->condcnt;
4736 nasm_free(i);
4737 }
4738}
4739
4740
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004741/*
4742 * Expand the multi-line macro call made by the given line, if
4743 * there is one to be expanded. If there is, push the expansion on
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004744 * istk->expansion and return 1. Otherwise return 0.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004745 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004746static int expand_mmacro(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004747{
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004748 Token *startline = tline;
H. Peter Anvineba20a72002-04-30 20:53:55 +00004749 Token *label = NULL;
4750 int dont_prepend = 0;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004751 Token **params, *t, *tt;
4752 MMacro *m;
4753 Line *l, *ll;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004754 int i, nparam, *paramlen;
H. Peter Anvinc751e862008-06-09 10:18:45 -07004755 const char *mname;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004756
4757 t = tline;
H. Peter Anvineba20a72002-04-30 20:53:55 +00004758 skip_white_(t);
H. Peter Anvince2233b2008-05-25 21:57:00 -07004759 /* if (!tok_type_(t, TOK_ID)) Lino 02/25/02 */
H. Peter Anvindce1e2f2002-04-30 21:06:37 +00004760 if (!tok_type_(t, TOK_ID) && !tok_type_(t, TOK_PREPROC_ID))
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004761 return 0;
4762 m = is_mmacro(t, &params);
4763 if (m) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004764 mname = t->text;
H. Peter Anvinc751e862008-06-09 10:18:45 -07004765 } else {
H. Peter Anvine2c80182005-01-15 22:15:51 +00004766 Token *last;
4767 /*
4768 * We have an id which isn't a macro call. We'll assume
4769 * it might be a label; we'll also check to see if a
4770 * colon follows it. Then, if there's another id after
4771 * that lot, we'll check it again for macro-hood.
4772 */
4773 label = last = t;
4774 t = t->next;
4775 if (tok_type_(t, TOK_WHITESPACE))
4776 last = t, t = t->next;
4777 if (tok_is_(t, ":")) {
4778 dont_prepend = 1;
4779 last = t, t = t->next;
4780 if (tok_type_(t, TOK_WHITESPACE))
4781 last = t, t = t->next;
4782 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004783 if (!tok_type_(t, TOK_ID) || !(m = is_mmacro(t, &params)))
4784 return 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004785 last->next = NULL;
Keith Kanios891775e2009-07-11 06:08:54 -05004786 mname = t->text;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004787 tline = t;
H. Peter Anvineba20a72002-04-30 20:53:55 +00004788 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004789
4790 /*
4791 * Fix up the parameters: this involves stripping leading and
4792 * trailing whitespace, then stripping braces if they are
4793 * present.
4794 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004795 for (nparam = 0; params[nparam]; nparam++) ;
H. Peter Anvin734b1882002-04-30 21:01:08 +00004796 paramlen = nparam ? nasm_malloc(nparam * sizeof(*paramlen)) : NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004797
H. Peter Anvine2c80182005-01-15 22:15:51 +00004798 for (i = 0; params[i]; i++) {
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08004799 int brace = 0;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004800 int comma = (!m->plus || i < nparam - 1);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004801
H. Peter Anvine2c80182005-01-15 22:15:51 +00004802 t = params[i];
4803 skip_white_(t);
4804 if (tok_is_(t, "{"))
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08004805 t = t->next, brace++, comma = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004806 params[i] = t;
4807 paramlen[i] = 0;
4808 while (t) {
4809 if (comma && t->type == TOK_OTHER && !strcmp(t->text, ","))
4810 break; /* ... because we have hit a comma */
4811 if (comma && t->type == TOK_WHITESPACE
4812 && tok_is_(t->next, ","))
4813 break; /* ... or a space then a comma */
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08004814 if (brace && t->type == TOK_OTHER) {
4815 if (t->text[0] == '{')
4816 brace++; /* ... or a nested opening brace */
4817 else if (t->text[0] == '}')
4818 if (!--brace)
4819 break; /* ... or a brace */
4820 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00004821 t = t->next;
4822 paramlen[i]++;
4823 }
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08004824 if (brace)
H. Peter Anvin130736c2016-02-17 20:27:41 -08004825 nasm_error(ERR_NONFATAL, "macro params should be enclosed in braces");
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004826 }
4827
4828 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004829 * OK, we have a MMacro structure together with a set of
4830 * parameters. We must now go through the expansion and push
4831 * copies of each Line on to istk->expansion. Substitution of
H. Peter Anvin76690a12002-04-30 20:52:49 +00004832 * parameter tokens and macro-local tokens doesn't get done
4833 * until the single-line macro substitution process; this is
4834 * because delaying them allows us to change the semantics
4835 * later through %rotate.
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004836 *
4837 * First, push an end marker on to istk->expansion, mark this
4838 * macro as in progress, and set up its invocation-specific
4839 * variables.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004840 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004841 ll = nasm_malloc(sizeof(Line));
4842 ll->next = istk->expansion;
4843 ll->finishes = m;
4844 ll->first = NULL;
4845 istk->expansion = ll;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004846
4847 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004848 * Save the previous MMacro expansion in the case of
4849 * macro recursion
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004850 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004851 if (m->max_depth && m->in_progress)
4852 push_mmacro(m);
4853
4854 m->in_progress ++;
4855 m->params = params;
4856 m->iline = tline;
4857 m->nparam = nparam;
4858 m->rotate = 0;
4859 m->paramlen = paramlen;
4860 m->unique = unique++;
4861 m->lineno = 0;
4862 m->condcnt = 0;
4863
4864 m->next_active = istk->mstk;
4865 istk->mstk = m;
4866
4867 list_for_each(l, m->expansion) {
4868 Token **tail;
4869
4870 ll = nasm_malloc(sizeof(Line));
4871 ll->finishes = NULL;
4872 ll->next = istk->expansion;
4873 istk->expansion = ll;
4874 tail = &ll->first;
4875
4876 list_for_each(t, l->first) {
4877 Token *x = t;
4878 switch (t->type) {
4879 case TOK_PREPROC_Q:
4880 tt = *tail = new_Token(NULL, TOK_ID, mname, 0);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004881 break;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004882 case TOK_PREPROC_QQ:
4883 tt = *tail = new_Token(NULL, TOK_ID, m->name, 0);
4884 break;
4885 case TOK_PREPROC_ID:
4886 if (t->text[1] == '0' && t->text[2] == '0') {
4887 dont_prepend = -1;
4888 x = label;
4889 if (!x)
4890 continue;
4891 }
4892 /* fall through */
4893 default:
4894 tt = *tail = new_Token(NULL, x->type, x->text, 0);
4895 break;
4896 }
4897 tail = &tt->next;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004898 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004899 *tail = NULL;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004900 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004901
4902 /*
H. Peter Anvineba20a72002-04-30 20:53:55 +00004903 * If we had a label, push it on as the first line of
4904 * the macro expansion.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004905 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004906 if (label) {
4907 if (dont_prepend < 0)
4908 free_tlist(startline);
4909 else {
4910 ll = nasm_malloc(sizeof(Line));
4911 ll->finishes = NULL;
4912 ll->next = istk->expansion;
4913 istk->expansion = ll;
4914 ll->first = startline;
4915 if (!dont_prepend) {
4916 while (label->next)
4917 label = label->next;
4918 label->next = tt = new_Token(NULL, TOK_OTHER, ":", 0);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004919 }
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004920 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004921 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004922
H. Peter Anvin8ac25aa2016-02-18 01:16:18 -08004923 lfmt->uplevel(m->nolist ? LIST_MACRO_NOLIST : LIST_MACRO);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004924
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004925 return 1;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004926}
4927
H. Peter Anvin130736c2016-02-17 20:27:41 -08004928/*
4929 * This function adds macro names to error messages, and suppresses
4930 * them if necessary.
4931 */
4932static void pp_verror(int severity, const char *fmt, va_list arg)
Victor van den Elzen3b404c02008-09-18 13:51:36 +02004933{
H. Peter Anvin130736c2016-02-17 20:27:41 -08004934 char buff[BUFSIZ];
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004935 MMacro *mmac = NULL;
4936 int delta = 0;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02004937
H. Peter Anvin130736c2016-02-17 20:27:41 -08004938 /*
4939 * If we're in a dead branch of IF or something like it, ignore the error.
4940 * However, because %else etc are evaluated in the state context
4941 * of the previous branch, errors might get lost:
4942 * %if 0 ... %else trailing garbage ... %endif
4943 * So %else etc should set the ERR_PP_PRECOND flag.
4944 */
4945 if ((severity & ERR_MASK) < ERR_FATAL &&
4946 istk && istk->conds &&
4947 ((severity & ERR_PP_PRECOND) ?
4948 istk->conds->state == COND_NEVER :
H. Peter Anvineb6653f2016-04-05 13:03:10 -07004949 !emitting(istk->conds->state)))
H. Peter Anvin130736c2016-02-17 20:27:41 -08004950 return;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02004951
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004952 /* get %macro name */
H. Peter Anvin130736c2016-02-17 20:27:41 -08004953 if (!(severity & ERR_NOFILE) && istk && istk->mstk) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004954 mmac = istk->mstk;
4955 /* but %rep blocks should be skipped */
4956 while (mmac && !mmac->name)
4957 mmac = mmac->next_active, delta++;
Cyrill Gorcunov9900c6b2011-10-09 18:58:46 +04004958 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004959
H. Peter Anvin130736c2016-02-17 20:27:41 -08004960 if (mmac) {
4961 vsnprintf(buff, sizeof(buff), fmt, arg);
Victor van den Elzen3b404c02008-09-18 13:51:36 +02004962
H. Peter Anvin130736c2016-02-17 20:27:41 -08004963 nasm_set_verror(real_verror);
4964 nasm_error(severity, "(%s:%d) %s",
4965 mmac->name, mmac->lineno - delta, buff);
4966 nasm_set_verror(pp_verror);
4967 } else {
4968 real_verror(severity, fmt, arg);
4969 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004970}
4971
H. Peter Anvin734b1882002-04-30 21:01:08 +00004972static void
H. Peter Anvin81b62b92017-12-20 13:33:49 -08004973pp_reset(const char *file, int apass, StrList **deplist)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004974{
H. Peter Anvin7383b402008-09-24 10:20:40 -07004975 Token *t;
4976
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004977 cstk = NULL;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004978 istk = nasm_malloc(sizeof(Include));
4979 istk->next = NULL;
4980 istk->conds = NULL;
4981 istk->expansion = NULL;
4982 istk->mstk = NULL;
H. Peter Anvin3e83cec2016-05-25 04:28:46 -07004983 istk->fp = nasm_open_read(file, NF_TEXT);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004984 istk->fname = NULL;
H. Peter Anvin274cda82016-05-10 02:56:29 -07004985 src_set(0, file);
H. Peter Anvineba20a72002-04-30 20:53:55 +00004986 istk->lineinc = 1;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004987 if (!istk->fp)
H. Peter Anvin130736c2016-02-17 20:27:41 -08004988 nasm_fatal(ERR_NOFILE, "unable to open input file `%s'", file);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004989 defining = NULL;
Charles Crayned4200be2008-07-12 16:42:33 -07004990 nested_mac_count = 0;
4991 nested_rep_count = 0;
H. Peter Anvin97a23472007-09-16 17:57:25 -07004992 init_macros();
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004993 unique = 0;
H. Peter Anvinf7606612016-07-13 14:23:48 -07004994
4995 if (tasm_compatible_mode)
4996 pp_add_stdmac(nasm_stdmac_tasm);
4997
4998 pp_add_stdmac(nasm_stdmac_nasm);
4999 pp_add_stdmac(nasm_stdmac_version);
5000
Cyrill Gorcunov15ce78f2017-01-06 20:21:28 +03005001 if (extrastdmac)
5002 pp_add_stdmac(extrastdmac);
5003
H. Peter Anvinf7606612016-07-13 14:23:48 -07005004 stdmacpos = stdmacros[0];
5005 stdmacnext = &stdmacros[1];
5006
H. Peter Anvind2456592008-06-19 15:04:18 -07005007 do_predef = true;
H. Peter Anvin61f130f2008-09-25 15:45:06 -07005008
5009 /*
5010 * 0 for dependencies, 1 for preparatory passes, 2 for final pass.
5011 * The caller, however, will also pass in 3 for preprocess-only so
5012 * we can set __PASS__ accordingly.
5013 */
5014 pass = apass > 2 ? 2 : apass;
5015
H. Peter Anvin9924d1e2016-10-04 00:59:39 -07005016 dephead = deplist;
H. Peter Anvin436e3672016-10-04 01:12:28 -07005017 nasm_add_string_to_strlist(dephead, file);
H. Peter Anvin9924d1e2016-10-04 00:59:39 -07005018
H. Peter Anvin61f130f2008-09-25 15:45:06 -07005019 /*
5020 * Define the __PASS__ macro. This is defined here unlike
5021 * all the other builtins, because it is special -- it varies between
5022 * passes.
5023 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005024 t = nasm_malloc(sizeof(*t));
5025 t->next = NULL;
H. Peter Anvin61f130f2008-09-25 15:45:06 -07005026 make_tok_num(t, apass);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005027 t->a.mac = NULL;
H. Peter Anvin7383b402008-09-24 10:20:40 -07005028 define_smacro(NULL, "__PASS__", true, 0, t);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005029}
5030
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07005031static void pp_init(void)
5032{
5033 hash_init(&FileHash, HASH_MEDIUM);
5034}
5035
Keith Kaniosa6dfa782007-04-13 16:47:53 +00005036static char *pp_getline(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005037{
Keith Kaniosa6dfa782007-04-13 16:47:53 +00005038 char *line;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005039 Token *tline;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005040
H. Peter Anvin130736c2016-02-17 20:27:41 -08005041 real_verror = nasm_set_verror(pp_verror);
H. Peter Anvin215186f2016-02-17 20:27:41 -08005042
H. Peter Anvine2c80182005-01-15 22:15:51 +00005043 while (1) {
5044 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005045 * Fetch a tokenized line, either from the macro-expansion
H. Peter Anvine2c80182005-01-15 22:15:51 +00005046 * buffer or from the input file.
5047 */
5048 tline = NULL;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005049 while (istk->expansion && istk->expansion->finishes) {
5050 Line *l = istk->expansion;
5051 if (!l->finishes->name && l->finishes->in_progress > 1) {
5052 Line *ll;
H. Peter Anvineba20a72002-04-30 20:53:55 +00005053
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005054 /*
5055 * This is a macro-end marker for a macro with no
5056 * name, which means it's not really a macro at all
5057 * but a %rep block, and the `in_progress' field is
5058 * more than 1, meaning that we still need to
5059 * repeat. (1 means the natural last repetition; 0
5060 * means termination by %exitrep.) We have
5061 * therefore expanded up to the %endrep, and must
5062 * push the whole block on to the expansion buffer
5063 * again. We don't bother to remove the macro-end
5064 * marker: we'd only have to generate another one
5065 * if we did.
5066 */
5067 l->finishes->in_progress--;
5068 list_for_each(l, l->finishes->expansion) {
5069 Token *t, *tt, **tail;
5070
5071 ll = nasm_malloc(sizeof(Line));
5072 ll->next = istk->expansion;
5073 ll->finishes = NULL;
5074 ll->first = NULL;
5075 tail = &ll->first;
5076
5077 list_for_each(t, l->first) {
5078 if (t->text || t->type == TOK_WHITESPACE) {
5079 tt = *tail = new_Token(NULL, t->type, t->text, 0);
5080 tail = &tt->next;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005081 }
5082 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005083
5084 istk->expansion = ll;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005085 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005086 } else {
5087 /*
5088 * Check whether a `%rep' was started and not ended
5089 * within this macro expansion. This can happen and
5090 * should be detected. It's a fatal error because
5091 * I'm too confused to work out how to recover
5092 * sensibly from it.
5093 */
5094 if (defining) {
5095 if (defining->name)
H. Peter Anvin130736c2016-02-17 20:27:41 -08005096 nasm_panic(0, "defining with name in expansion");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005097 else if (istk->mstk->name)
H. Peter Anvin130736c2016-02-17 20:27:41 -08005098 nasm_fatal(0, "`%%rep' without `%%endrep' within"
5099 " expansion of macro `%s'",
5100 istk->mstk->name);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005101 }
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005102
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005103 /*
5104 * FIXME: investigate the relationship at this point between
5105 * istk->mstk and l->finishes
5106 */
5107 {
5108 MMacro *m = istk->mstk;
5109 istk->mstk = m->next_active;
5110 if (m->name) {
5111 /*
5112 * This was a real macro call, not a %rep, and
5113 * therefore the parameter information needs to
5114 * be freed.
5115 */
5116 if (m->prev) {
5117 pop_mmacro(m);
5118 l->finishes->in_progress --;
5119 } else {
5120 nasm_free(m->params);
5121 free_tlist(m->iline);
5122 nasm_free(m->paramlen);
5123 l->finishes->in_progress = 0;
5124 }
Adam Majer91e72402017-07-25 10:42:01 +02005125 }
5126
5127 /*
5128 * FIXME It is incorrect to always free_mmacro here.
5129 * It leads to usage-after-free.
5130 *
5131 * https://bugzilla.nasm.us/show_bug.cgi?id=3392414
5132 */
5133#if 0
5134 else
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005135 free_mmacro(m);
Adam Majer91e72402017-07-25 10:42:01 +02005136#endif
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005137 }
5138 istk->expansion = l->next;
5139 nasm_free(l);
H. Peter Anvin8ac25aa2016-02-18 01:16:18 -08005140 lfmt->downlevel(LIST_MACRO);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005141 }
5142 }
5143 while (1) { /* until we get a line we can use */
5144
5145 if (istk->expansion) { /* from a macro expansion */
5146 char *p;
5147 Line *l = istk->expansion;
5148 if (istk->mstk)
5149 istk->mstk->lineno++;
5150 tline = l->first;
5151 istk->expansion = l->next;
5152 nasm_free(l);
5153 p = detoken(tline, false);
H. Peter Anvin8ac25aa2016-02-18 01:16:18 -08005154 lfmt->line(LIST_MACRO, p);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005155 nasm_free(p);
5156 break;
5157 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00005158 line = read_line();
5159 if (line) { /* from the current input file */
5160 line = prepreproc(line);
Keith Kaniosb7a89542007-04-12 02:40:54 +00005161 tline = tokenize(line);
H. Peter Anvine2c80182005-01-15 22:15:51 +00005162 nasm_free(line);
5163 break;
5164 }
5165 /*
5166 * The current file has ended; work down the istk
5167 */
5168 {
5169 Include *i = istk;
5170 fclose(i->fp);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005171 if (i->conds) {
5172 /* nasm_error can't be conditionally suppressed */
H. Peter Anvin41087062016-03-03 14:27:34 -08005173 nasm_fatal(0,
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005174 "expected `%%endif' before end of file");
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005175 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00005176 /* only set line and file name if there's a next node */
H. Peter Anvin274cda82016-05-10 02:56:29 -07005177 if (i->next)
5178 src_set(i->lineno, i->fname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00005179 istk = i->next;
H. Peter Anvin8ac25aa2016-02-18 01:16:18 -08005180 lfmt->downlevel(LIST_INCLUDE);
H. Peter Anvine2c80182005-01-15 22:15:51 +00005181 nasm_free(i);
H. Peter Anvin130736c2016-02-17 20:27:41 -08005182 if (!istk) {
5183 line = NULL;
5184 goto done;
5185 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005186 if (istk->expansion && istk->expansion->finishes)
5187 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005188 }
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005189 }
5190
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005191 /*
5192 * We must expand MMacro parameters and MMacro-local labels
5193 * _before_ we plunge into directive processing, to cope
5194 * with things like `%define something %1' such as STRUC
5195 * uses. Unless we're _defining_ a MMacro, in which case
5196 * those tokens should be left alone to go into the
5197 * definition; and unless we're in a non-emitting
5198 * condition, in which case we don't want to meddle with
5199 * anything.
5200 */
5201 if (!defining && !(istk->conds && !emitting(istk->conds->state))
5202 && !(istk->mstk && !istk->mstk->in_progress)) {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005203 tline = expand_mmac_params(tline);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005204 }
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005205
H. Peter Anvine2c80182005-01-15 22:15:51 +00005206 /*
5207 * Check the line to see if it's a preprocessor directive.
5208 */
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07005209 if (do_directive(tline, &line) == DIRECTIVE_FOUND) {
5210 if (line)
5211 break; /* Directive generated output */
5212 else
5213 continue;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005214 } else if (defining) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00005215 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005216 * We're defining a multi-line macro. We emit nothing
5217 * at all, and just
5218 * shove the tokenized line on to the macro definition.
H. Peter Anvine2c80182005-01-15 22:15:51 +00005219 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005220 Line *l = nasm_malloc(sizeof(Line));
5221 l->next = defining->expansion;
5222 l->first = tline;
5223 l->finishes = NULL;
5224 defining->expansion = l;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005225 continue;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005226 } else if (istk->conds && !emitting(istk->conds->state)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00005227 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005228 * We're in a non-emitting branch of a condition block.
H. Peter Anvine2c80182005-01-15 22:15:51 +00005229 * Emit nothing at all, not even a blank line: when we
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005230 * emerge from the condition we'll give a line-number
H. Peter Anvine2c80182005-01-15 22:15:51 +00005231 * directive so we keep our place correctly.
5232 */
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005233 free_tlist(tline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00005234 continue;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005235 } else if (istk->mstk && !istk->mstk->in_progress) {
5236 /*
5237 * We're in a %rep block which has been terminated, so
5238 * we're walking through to the %endrep without
5239 * emitting anything. Emit nothing at all, not even a
5240 * blank line: when we emerge from the %rep block we'll
5241 * give a line-number directive so we keep our place
5242 * correctly.
5243 */
5244 free_tlist(tline);
5245 continue;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005246 } else {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005247 tline = expand_smacro(tline);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005248 if (!expand_mmacro(tline)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00005249 /*
Keith Kaniosb7a89542007-04-12 02:40:54 +00005250 * De-tokenize the line again, and emit it.
H. Peter Anvine2c80182005-01-15 22:15:51 +00005251 */
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005252 line = detoken(tline, true);
5253 free_tlist(tline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00005254 break;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005255 } else {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005256 continue; /* expand_mmacro calls free_tlist */
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005257 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00005258 }
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005259 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005260
H. Peter Anvin130736c2016-02-17 20:27:41 -08005261done:
5262 nasm_set_verror(real_verror);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005263 return line;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005264}
5265
H. Peter Anvine2c80182005-01-15 22:15:51 +00005266static void pp_cleanup(int pass)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005267{
H. Peter Anvin130736c2016-02-17 20:27:41 -08005268 real_verror = nasm_set_verror(pp_verror);
H. Peter Anvin215186f2016-02-17 20:27:41 -08005269
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005270 if (defining) {
5271 if (defining->name) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08005272 nasm_error(ERR_NONFATAL,
5273 "end of file while still defining macro `%s'",
5274 defining->name);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005275 } else {
H. Peter Anvin130736c2016-02-17 20:27:41 -08005276 nasm_error(ERR_NONFATAL, "end of file while still in %%rep");
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005277 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005278
5279 free_mmacro(defining);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005280 defining = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005281 }
H. Peter Anvin130736c2016-02-17 20:27:41 -08005282
5283 nasm_set_verror(real_verror);
H. Peter Anvin215186f2016-02-17 20:27:41 -08005284
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005285 while (cstk)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005286 ctx_pop();
H. Peter Anvin97a23472007-09-16 17:57:25 -07005287 free_macros();
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005288 while (istk) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00005289 Include *i = istk;
5290 istk = istk->next;
5291 fclose(i->fp);
Cyrill Gorcunov8dcfd882011-03-03 09:18:56 +03005292 nasm_free(i);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005293 }
5294 while (cstk)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005295 ctx_pop();
H. Peter Anvin274cda82016-05-10 02:56:29 -07005296 src_set_fname(NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00005297 if (pass == 0) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03005298 IncPath *i;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005299 free_llist(predef);
Cyrill Gorcunovdae24d72014-06-28 10:17:39 +04005300 predef = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005301 delete_Blocks();
Cyrill Gorcunovdae24d72014-06-28 10:17:39 +04005302 freeTokens = NULL;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03005303 while ((i = ipath)) {
5304 ipath = i->next;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005305 if (i->path)
5306 nasm_free(i->path);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03005307 nasm_free(i);
5308 }
H. Peter Anvin11dfa1a2008-07-02 18:11:04 -07005309 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005310}
5311
Cyrill Gorcunov0b78bff2012-05-07 01:57:55 +04005312static void pp_include_path(char *path)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005313{
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005314 IncPath *i;
H. Peter Anvin37a321f2007-09-24 13:41:58 -07005315
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005316 i = nasm_malloc(sizeof(IncPath));
5317 i->path = path ? nasm_strdup(path) : NULL;
5318 i->next = NULL;
Frank Kotlerd0ed6fd2003-08-27 11:33:56 +00005319
H. Peter Anvin89cee572009-07-15 09:16:54 -04005320 if (ipath) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00005321 IncPath *j = ipath;
H. Peter Anvin89cee572009-07-15 09:16:54 -04005322 while (j->next)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005323 j = j->next;
5324 j->next = i;
5325 } else {
5326 ipath = i;
Frank Kotlerd0ed6fd2003-08-27 11:33:56 +00005327 }
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005328}
Frank Kotlerd0ed6fd2003-08-27 11:33:56 +00005329
Cyrill Gorcunov0b78bff2012-05-07 01:57:55 +04005330static void pp_pre_include(char *fname)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005331{
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005332 Token *inc, *space, *name;
5333 Line *l;
5334
H. Peter Anvin734b1882002-04-30 21:01:08 +00005335 name = new_Token(NULL, TOK_INTERNAL_STRING, fname, 0);
5336 space = new_Token(name, TOK_WHITESPACE, NULL, 0);
5337 inc = new_Token(space, TOK_PREPROC_ID, "%include", 0);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005338
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005339 l = nasm_malloc(sizeof(Line));
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005340 l->next = predef;
5341 l->first = inc;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005342 l->finishes = NULL;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005343 predef = l;
5344}
5345
Cyrill Gorcunov0b78bff2012-05-07 01:57:55 +04005346static void pp_pre_define(char *definition)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005347{
5348 Token *def, *space;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005349 Line *l;
Keith Kaniosa6dfa782007-04-13 16:47:53 +00005350 char *equals;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005351
H. Peter Anvin130736c2016-02-17 20:27:41 -08005352 real_verror = nasm_set_verror(pp_verror);
H. Peter Anvin215186f2016-02-17 20:27:41 -08005353
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005354 equals = strchr(definition, '=');
H. Peter Anvin734b1882002-04-30 21:01:08 +00005355 space = new_Token(NULL, TOK_WHITESPACE, NULL, 0);
5356 def = new_Token(space, TOK_PREPROC_ID, "%define", 0);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005357 if (equals)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005358 *equals = ' ';
Keith Kaniosb7a89542007-04-12 02:40:54 +00005359 space->next = tokenize(definition);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005360 if (equals)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005361 *equals = '=';
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005362
Cyrill Gorcunov6d42e9b2015-02-08 11:07:17 +03005363 if (space->next->type != TOK_PREPROC_ID &&
5364 space->next->type != TOK_ID)
H. Peter Anvin130736c2016-02-17 20:27:41 -08005365 nasm_error(ERR_WARNING, "pre-defining non ID `%s\'\n", definition);
Cyrill Gorcunov6d42e9b2015-02-08 11:07:17 +03005366
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005367 l = nasm_malloc(sizeof(Line));
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005368 l->next = predef;
5369 l->first = def;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005370 l->finishes = NULL;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005371 predef = l;
H. Peter Anvin130736c2016-02-17 20:27:41 -08005372
5373 nasm_set_verror(real_verror);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005374}
5375
Cyrill Gorcunov0b78bff2012-05-07 01:57:55 +04005376static void pp_pre_undefine(char *definition)
H. Peter Anvin620515a2002-04-30 20:57:38 +00005377{
5378 Token *def, *space;
5379 Line *l;
5380
H. Peter Anvin734b1882002-04-30 21:01:08 +00005381 space = new_Token(NULL, TOK_WHITESPACE, NULL, 0);
5382 def = new_Token(space, TOK_PREPROC_ID, "%undef", 0);
Keith Kaniosb7a89542007-04-12 02:40:54 +00005383 space->next = tokenize(definition);
H. Peter Anvin620515a2002-04-30 20:57:38 +00005384
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005385 l = nasm_malloc(sizeof(Line));
H. Peter Anvin620515a2002-04-30 20:57:38 +00005386 l->next = predef;
5387 l->first = def;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005388 l->finishes = NULL;
H. Peter Anvin620515a2002-04-30 20:57:38 +00005389 predef = l;
5390}
5391
H. Peter Anvinf7606612016-07-13 14:23:48 -07005392static void pp_add_stdmac(macros_t *macros)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005393{
H. Peter Anvinf7606612016-07-13 14:23:48 -07005394 macros_t **mp;
5395
5396 /* Find the end of the list and avoid duplicates */
5397 for (mp = stdmacros; *mp; mp++) {
5398 if (*mp == macros)
5399 return; /* Nothing to do */
5400 }
5401
5402 nasm_assert(mp < &stdmacros[ARRAY_SIZE(stdmacros)-1]);
5403
5404 *mp = macros;
H. Peter Anvin76690a12002-04-30 20:52:49 +00005405}
5406
Cyrill Gorcunov15ce78f2017-01-06 20:21:28 +03005407static void pp_extra_stdmac(macros_t *macros)
5408{
5409 extrastdmac = macros;
5410}
5411
Keith Kaniosa5fc6462007-10-13 07:09:22 -07005412static void make_tok_num(Token * tok, int64_t val)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005413{
Cyrill Gorcunovce652742013-05-06 23:43:43 +04005414 char numbuf[32];
Keith Kaniosa5fc6462007-10-13 07:09:22 -07005415 snprintf(numbuf, sizeof(numbuf), "%"PRId64"", val);
H. Peter Anvineba20a72002-04-30 20:53:55 +00005416 tok->text = nasm_strdup(numbuf);
5417 tok->type = TOK_NUMBER;
5418}
5419
H. Peter Anvin37368952016-05-09 14:10:32 -07005420static void pp_list_one_macro(MMacro *m, int severity)
5421{
5422 if (!m)
5423 return;
5424
5425 /* We need to print the next_active list in reverse order */
5426 pp_list_one_macro(m->next_active, severity);
5427
5428 if (m->name && !m->nolist) {
H. Peter Anvin274cda82016-05-10 02:56:29 -07005429 src_set(m->xline + m->lineno, m->fname);
H. Peter Anvin37368952016-05-09 14:10:32 -07005430 nasm_error(severity, "... from macro `%s' defined here", m->name);
5431 }
5432}
5433
H. Peter Anvin4def1a82016-05-09 13:59:44 -07005434static void pp_error_list_macros(int severity)
5435{
H. Peter Anvin4def1a82016-05-09 13:59:44 -07005436 int32_t saved_line;
5437 const char *saved_fname = NULL;
5438
5439 severity |= ERR_PP_LISTMACRO | ERR_NO_SEVERITY;
H. Peter Anvin274cda82016-05-10 02:56:29 -07005440 src_get(&saved_line, &saved_fname);
H. Peter Anvin4def1a82016-05-09 13:59:44 -07005441
Cyrill Gorcunov771d04e2016-05-10 23:27:03 +03005442 if (istk)
5443 pp_list_one_macro(istk->mstk, severity);
H. Peter Anvin4def1a82016-05-09 13:59:44 -07005444
H. Peter Anvin274cda82016-05-10 02:56:29 -07005445 src_set(saved_line, saved_fname);
H. Peter Anvin4def1a82016-05-09 13:59:44 -07005446}
5447
H. Peter Anvine7469712016-02-18 02:20:59 -08005448const struct preproc_ops nasmpp = {
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07005449 pp_init,
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005450 pp_reset,
5451 pp_getline,
Cyrill Gorcunov0b78bff2012-05-07 01:57:55 +04005452 pp_cleanup,
Cyrill Gorcunov15ce78f2017-01-06 20:21:28 +03005453 pp_extra_stdmac,
Cyrill Gorcunov0b78bff2012-05-07 01:57:55 +04005454 pp_pre_define,
5455 pp_pre_undefine,
5456 pp_pre_include,
H. Peter Anvin4def1a82016-05-09 13:59:44 -07005457 pp_include_path,
5458 pp_error_list_macros,
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005459};