blob: cdf03bc168bb5140674d2a49830b3074b6282aba [file] [log] [blame]
H. Peter Anvin9e6747c2009-06-28 17:13:04 -07001/* ----------------------------------------------------------------------- *
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002 *
H. Peter Anvin05990342018-06-11 13:32:42 -07003 * Copyright 1996-2018 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
Cyrill Gorcunove091d6e2010-08-09 13:58:22 +0400342/* max reps */
343#define REP_LIMIT ((INT64_C(1) << 62))
344
Keith Kanios852f1ee2009-07-12 00:19:55 -0500345/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000346 * Condition codes. Note that we use c_ prefix not C_ because C_ is
347 * used in nasm.h for the "real" condition codes. At _this_ level,
348 * we treat CXZ and ECXZ as condition codes, albeit non-invertible
349 * ones, so we need a different enum...
350 */
H. Peter Anvin476d2862007-10-02 22:04:15 -0700351static const char * const conditions[] = {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000352 "a", "ae", "b", "be", "c", "cxz", "e", "ecxz", "g", "ge", "l", "le",
353 "na", "nae", "nb", "nbe", "nc", "ne", "ng", "nge", "nl", "nle", "no",
H. Peter Anvince9be342007-09-12 00:22:29 +0000354 "np", "ns", "nz", "o", "p", "pe", "po", "rcxz", "s", "z"
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000355};
H. Peter Anvin476d2862007-10-02 22:04:15 -0700356enum pp_conds {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000357 c_A, c_AE, c_B, c_BE, c_C, c_CXZ, c_E, c_ECXZ, c_G, c_GE, c_L, c_LE,
358 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 -0700359 c_NP, c_NS, c_NZ, c_O, c_P, c_PE, c_PO, c_RCXZ, c_S, c_Z,
360 c_none = -1
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000361};
H. Peter Anvin476d2862007-10-02 22:04:15 -0700362static const enum pp_conds inverse_ccs[] = {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000363 c_NA, c_NAE, c_NB, c_NBE, c_NC, -1, c_NE, -1, c_NG, c_NGE, c_NL, c_NLE,
364 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 +0000365 c_Z, c_NO, c_NP, c_PO, c_PE, -1, c_NS, c_NZ
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000366};
367
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800368/*
369 * Directive names.
370 */
371/* If this is a an IF, ELIF, ELSE or ENDIF keyword */
372static int is_condition(enum preproc_token arg)
373{
374 return PP_IS_COND(arg) || (arg == PP_ELSE) || (arg == PP_ENDIF);
375}
376
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000377/* For TASM compatibility we need to be able to recognise TASM compatible
378 * conditional compilation directives. Using the NASM pre-processor does
379 * not work, so we look for them specifically from the following list and
380 * then jam in the equivalent NASM directive into the input stream.
381 */
382
H. Peter Anvine2c80182005-01-15 22:15:51 +0000383enum {
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000384 TM_ARG, TM_ELIF, TM_ELSE, TM_ENDIF, TM_IF, TM_IFDEF, TM_IFDIFI,
385 TM_IFNDEF, TM_INCLUDE, TM_LOCAL
386};
387
H. Peter Anvin476d2862007-10-02 22:04:15 -0700388static const char * const tasm_directives[] = {
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000389 "arg", "elif", "else", "endif", "if", "ifdef", "ifdifi",
390 "ifndef", "include", "local"
391};
392
393static int StackSize = 4;
H. Peter Anvin6c8b2be2016-05-24 23:46:50 -0700394static const char *StackPointer = "ebp";
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000395static int ArgOffset = 8;
H. Peter Anvin8781cb02007-11-08 20:01:11 -0800396static int LocalOffset = 0;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000397
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000398static Context *cstk;
399static Include *istk;
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800400static IncPath *ipath = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000401
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300402static int pass; /* HACK: pass 0 = generate dependencies only */
H. Peter Anvin9924d1e2016-10-04 00:59:39 -0700403static StrList **dephead;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000404
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300405static uint64_t unique; /* unique identifier numbers */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000406
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800407static Line *predef = NULL;
H. Peter Anvind2456592008-06-19 15:04:18 -0700408static bool do_predef;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000409
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000410/*
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800411 * The current set of multi-line macros we have defined.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000412 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800413static struct hash_table mmacros;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000414
415/*
416 * The current set of single-line macros we have defined.
417 */
H. Peter Anvin166c2472008-05-28 12:28:58 -0700418static struct hash_table smacros;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000419
420/*
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800421 * The multi-line macro we are currently defining, or the %rep
422 * block we are currently reading, if any.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000423 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800424static MMacro *defining;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000425
Charles Crayned4200be2008-07-12 16:42:33 -0700426static uint64_t nested_mac_count;
427static uint64_t nested_rep_count;
428
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000429/*
430 * The number of macro parameters to allocate space for at a time.
431 */
432#define PARAM_DELTA 16
433
434/*
H. Peter Anvinf7606612016-07-13 14:23:48 -0700435 * The standard macro set: defined in macros.c in a set of arrays.
436 * This gives our position in any macro set, while we are processing it.
437 * The stdmacset is an array of such macro sets.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000438 */
H. Peter Anvina70547f2008-07-19 21:44:26 -0700439static macros_t *stdmacpos;
H. Peter Anvinf7606612016-07-13 14:23:48 -0700440static macros_t **stdmacnext;
441static macros_t *stdmacros[8];
Cyrill Gorcunov15ce78f2017-01-06 20:21:28 +0300442static macros_t *extrastdmac;
H. Peter Anvin76690a12002-04-30 20:52:49 +0000443
444/*
H. Peter Anvin734b1882002-04-30 21:01:08 +0000445 * Tokens are allocated in blocks to improve speed
446 */
447#define TOKEN_BLOCKSIZE 4096
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800448static Token *freeTokens = NULL;
H. Peter Anvince616072002-04-30 21:02:23 +0000449struct Blocks {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000450 Blocks *next;
451 void *chunk;
H. Peter Anvince616072002-04-30 21:02:23 +0000452};
453
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800454static Blocks blocks = { NULL, NULL };
H. Peter Anvin734b1882002-04-30 21:01:08 +0000455
456/*
H. Peter Anvin76690a12002-04-30 20:52:49 +0000457 * Forward declarations.
458 */
H. Peter Anvinf7606612016-07-13 14:23:48 -0700459static void pp_add_stdmac(macros_t *macros);
H. Peter Anvin734b1882002-04-30 21:01:08 +0000460static Token *expand_mmac_params(Token * tline);
461static Token *expand_smacro(Token * tline);
462static Token *expand_id(Token * tline);
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +0400463static Context *get_ctx(const char *name, const char **namep);
Keith Kaniosa5fc6462007-10-13 07:09:22 -0700464static void make_tok_num(Token * tok, int64_t val);
H. Peter Anvin130736c2016-02-17 20:27:41 -0800465static void pp_verror(int severity, const char *fmt, va_list ap);
466static vefunc real_verror;
H. Peter Anvince616072002-04-30 21:02:23 +0000467static void *new_Block(size_t size);
468static void delete_Blocks(void);
H. Peter Anvinc751e862008-06-09 10:18:45 -0700469static Token *new_Token(Token * next, enum pp_token_type type,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300470 const char *text, int txtlen);
H. Peter Anvin734b1882002-04-30 21:01:08 +0000471static Token *delete_Token(Token * t);
H. Peter Anvineba20a72002-04-30 20:53:55 +0000472
473/*
474 * Macros for safe checking of token pointers, avoid *(NULL)
475 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300476#define tok_type_(x,t) ((x) && (x)->type == (t))
477#define skip_white_(x) if (tok_type_((x), TOK_WHITESPACE)) (x)=(x)->next
478#define tok_is_(x,v) (tok_type_((x), TOK_OTHER) && !strcmp((x)->text,(v)))
479#define tok_isnt_(x,v) ((x) && ((x)->type!=TOK_OTHER || strcmp((x)->text,(v))))
H. Peter Anvin76690a12002-04-30 20:52:49 +0000480
Cyrill Gorcunov194ba892011-06-30 01:16:35 +0400481/*
H. Peter Anvin077fb932010-07-20 14:56:30 -0700482 * nasm_unquote with error if the string contains NUL characters.
483 * If the string contains NUL characters, issue an error and return
484 * the C len, i.e. truncate at the NUL.
485 */
486static size_t nasm_unquote_cstr(char *qstr, enum preproc_token directive)
487{
488 size_t len = nasm_unquote(qstr, NULL);
489 size_t clen = strlen(qstr);
490
491 if (len != clen)
H. Peter Anvin130736c2016-02-17 20:27:41 -0800492 nasm_error(ERR_NONFATAL, "NUL character in `%s' directive",
H. Peter Anvin077fb932010-07-20 14:56:30 -0700493 pp_directives[directive]);
494
495 return clen;
496}
497
498/*
H. Peter Anvinb40992c2010-09-15 08:57:21 -0700499 * In-place reverse a list of tokens.
500 */
501static Token *reverse_tokens(Token *t)
502{
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800503 Token *prev = NULL;
504 Token *next;
H. Peter Anvinb40992c2010-09-15 08:57:21 -0700505
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800506 while (t) {
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +0400507 next = t->next;
508 t->next = prev;
509 prev = t;
510 t = next;
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800511 }
H. Peter Anvinb40992c2010-09-15 08:57:21 -0700512
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800513 return prev;
H. Peter Anvinb40992c2010-09-15 08:57:21 -0700514}
515
516/*
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300517 * Handle TASM specific directives, which do not contain a % in
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000518 * front of them. We do it here because I could not find any other
519 * place to do it for the moment, and it is a hack (ideally it would
520 * be nice to be able to use the NASM pre-processor to do it).
521 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000522static char *check_tasm_directive(char *line)
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000523{
Keith Kaniosb7a89542007-04-12 02:40:54 +0000524 int32_t i, j, k, m, len;
Cyrill Gorcunovf66ac7d2009-10-12 20:41:13 +0400525 char *p, *q, *oldline, oldchar;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000526
Cyrill Gorcunovf66ac7d2009-10-12 20:41:13 +0400527 p = nasm_skip_spaces(line);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000528
529 /* Binary search for the directive name */
530 i = -1;
Cyrill Gorcunova7319242010-06-03 22:04:36 +0400531 j = ARRAY_SIZE(tasm_directives);
Cyrill Gorcunovf66ac7d2009-10-12 20:41:13 +0400532 q = nasm_skip_word(p);
533 len = q - p;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000534 if (len) {
535 oldchar = p[len];
536 p[len] = 0;
537 while (j - i > 1) {
538 k = (j + i) / 2;
539 m = nasm_stricmp(p, tasm_directives[k]);
540 if (m == 0) {
541 /* We have found a directive, so jam a % in front of it
542 * so that NASM will then recognise it as one if it's own.
543 */
544 p[len] = oldchar;
545 len = strlen(p);
546 oldline = line;
547 line = nasm_malloc(len + 2);
548 line[0] = '%';
549 if (k == TM_IFDIFI) {
H. Peter Anvin18f48792009-06-27 15:56:27 -0700550 /*
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300551 * NASM does not recognise IFDIFI, so we convert
552 * it to %if 0. This is not used in NASM
553 * compatible code, but does need to parse for the
554 * TASM macro package.
H. Peter Anvine2c80182005-01-15 22:15:51 +0000555 */
H. Peter Anvin18f48792009-06-27 15:56:27 -0700556 strcpy(line + 1, "if 0");
H. Peter Anvine2c80182005-01-15 22:15:51 +0000557 } else {
558 memcpy(line + 1, p, len + 1);
559 }
560 nasm_free(oldline);
561 return line;
562 } else if (m < 0) {
563 j = k;
564 } else
565 i = k;
566 }
567 p[len] = oldchar;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000568 }
569 return line;
570}
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000571
H. Peter Anvin76690a12002-04-30 20:52:49 +0000572/*
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000573 * The pre-preprocessing stage... This function translates line
574 * number indications as they emerge from GNU cpp (`# lineno "file"
575 * flags') into NASM preprocessor line number indications (`%line
576 * lineno file').
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000577 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000578static char *prepreproc(char *line)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000579{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000580 int lineno, fnlen;
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000581 char *fname, *oldline;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000582
H. Peter Anvine2c80182005-01-15 22:15:51 +0000583 if (line[0] == '#' && line[1] == ' ') {
584 oldline = line;
585 fname = oldline + 2;
586 lineno = atoi(fname);
587 fname += strspn(fname, "0123456789 ");
588 if (*fname == '"')
589 fname++;
590 fnlen = strcspn(fname, "\"");
591 line = nasm_malloc(20 + fnlen);
592 snprintf(line, 20 + fnlen, "%%line %d %.*s", lineno, fnlen, fname);
593 nasm_free(oldline);
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000594 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000595 if (tasm_compatible_mode)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000596 return check_tasm_directive(line);
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000597 return line;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000598}
599
600/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000601 * Free a linked list of tokens.
602 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000603static void free_tlist(Token * list)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000604{
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +0400605 while (list)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000606 list = delete_Token(list);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000607}
608
609/*
610 * Free a linked list of lines.
611 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000612static void free_llist(Line * list)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000613{
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +0400614 Line *l, *tmp;
615 list_for_each_safe(l, tmp, list) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000616 free_tlist(l->first);
617 nasm_free(l);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000618 }
619}
620
621/*
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800622 * Free an MMacro
H. Peter Anvineba20a72002-04-30 20:53:55 +0000623 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800624static void free_mmacro(MMacro * m)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000625{
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800626 nasm_free(m->name);
627 free_tlist(m->dlist);
628 nasm_free(m->defaults);
629 free_llist(m->expansion);
630 nasm_free(m);
H. Peter Anvineba20a72002-04-30 20:53:55 +0000631}
632
633/*
H. Peter Anvin97a23472007-09-16 17:57:25 -0700634 * Free all currently defined macros, and free the hash tables
635 */
H. Peter Anvin072771e2008-05-22 13:17:51 -0700636static void free_smacro_table(struct hash_table *smt)
H. Peter Anvin97a23472007-09-16 17:57:25 -0700637{
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +0400638 SMacro *s, *tmp;
H. Peter Anvin072771e2008-05-22 13:17:51 -0700639 const char *key;
640 struct hash_tbl_node *it = NULL;
H. Peter Anvin97a23472007-09-16 17:57:25 -0700641
H. Peter Anvin072771e2008-05-22 13:17:51 -0700642 while ((s = hash_iterate(smt, &it, &key)) != NULL) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300643 nasm_free((void *)key);
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +0400644 list_for_each_safe(s, tmp, s) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300645 nasm_free(s->name);
646 free_tlist(s->expansion);
647 nasm_free(s);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300648 }
H. Peter Anvin97a23472007-09-16 17:57:25 -0700649 }
H. Peter Anvin072771e2008-05-22 13:17:51 -0700650 hash_free(smt);
651}
652
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800653static void free_mmacro_table(struct hash_table *mmt)
H. Peter Anvin072771e2008-05-22 13:17:51 -0700654{
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800655 MMacro *m, *tmp;
H. Peter Anvin072771e2008-05-22 13:17:51 -0700656 const char *key;
657 struct hash_tbl_node *it = NULL;
H. Peter Anvin97a23472007-09-16 17:57:25 -0700658
659 it = NULL;
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800660 while ((m = hash_iterate(mmt, &it, &key)) != NULL) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300661 nasm_free((void *)key);
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800662 list_for_each_safe(m ,tmp, m)
663 free_mmacro(m);
H. Peter Anvin97a23472007-09-16 17:57:25 -0700664 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800665 hash_free(mmt);
H. Peter Anvin072771e2008-05-22 13:17:51 -0700666}
667
668static void free_macros(void)
669{
H. Peter Anvin166c2472008-05-28 12:28:58 -0700670 free_smacro_table(&smacros);
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800671 free_mmacro_table(&mmacros);
H. Peter Anvin97a23472007-09-16 17:57:25 -0700672}
673
674/*
675 * Initialize the hash tables
676 */
677static void init_macros(void)
678{
H. Peter Anvin166c2472008-05-28 12:28:58 -0700679 hash_init(&smacros, HASH_LARGE);
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800680 hash_init(&mmacros, HASH_LARGE);
H. Peter Anvin97a23472007-09-16 17:57:25 -0700681}
682
683/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000684 * Pop the context stack.
685 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000686static void ctx_pop(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000687{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000688 Context *c = cstk;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000689
690 cstk = cstk->next;
H. Peter Anvin166c2472008-05-28 12:28:58 -0700691 free_smacro_table(&c->localmac);
H. Peter Anvin734b1882002-04-30 21:01:08 +0000692 nasm_free(c->name);
693 nasm_free(c);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000694}
695
H. Peter Anvin072771e2008-05-22 13:17:51 -0700696/*
697 * Search for a key in the hash index; adding it if necessary
698 * (in which case we initialize the data pointer to NULL.)
699 */
700static void **
701hash_findi_add(struct hash_table *hash, const char *str)
702{
703 struct hash_insert hi;
704 void **r;
705 char *strx;
706
707 r = hash_findi(hash, str, &hi);
708 if (r)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300709 return r;
H. Peter Anvin072771e2008-05-22 13:17:51 -0700710
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300711 strx = nasm_strdup(str); /* Use a more efficient allocator here? */
H. Peter Anvin072771e2008-05-22 13:17:51 -0700712 return hash_add(&hi, strx, NULL);
713}
714
715/*
716 * Like hash_findi, but returns the data element rather than a pointer
717 * to it. Used only when not adding a new element, hence no third
718 * argument.
719 */
720static void *
721hash_findix(struct hash_table *hash, const char *str)
722{
723 void **p;
724
725 p = hash_findi(hash, str, NULL);
726 return p ? *p : NULL;
727}
728
Cyrill Gorcunov15bdc512010-07-13 11:27:41 +0400729/*
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800730 * read line from standart macros set,
Cyrill Gorcunov15bdc512010-07-13 11:27:41 +0400731 * if there no more left -- return NULL
732 */
733static char *line_from_stdmac(void)
734{
735 unsigned char c;
736 const unsigned char *p = stdmacpos;
737 char *line, *q;
738 size_t len = 0;
739
740 if (!stdmacpos)
741 return NULL;
742
743 while ((c = *p++)) {
744 if (c >= 0x80)
745 len += pp_directives_len[c - 0x80] + 1;
746 else
747 len++;
748 }
749
750 line = nasm_malloc(len + 1);
751 q = line;
752 while ((c = *stdmacpos++)) {
753 if (c >= 0x80) {
754 memcpy(q, pp_directives[c - 0x80], pp_directives_len[c - 0x80]);
755 q += pp_directives_len[c - 0x80];
756 *q++ = ' ';
757 } else {
758 *q++ = c;
759 }
760 }
761 stdmacpos = p;
762 *q = '\0';
763
764 if (!*stdmacpos) {
H. Peter Anvinf7606612016-07-13 14:23:48 -0700765 /* This was the last of this particular macro set */
Cyrill Gorcunov15bdc512010-07-13 11:27:41 +0400766 stdmacpos = NULL;
H. Peter Anvinf7606612016-07-13 14:23:48 -0700767 if (*stdmacnext) {
768 stdmacpos = *stdmacnext++;
Cyrill Gorcunov15bdc512010-07-13 11:27:41 +0400769 } else if (do_predef) {
770 Line *pd, *l;
771 Token *head, **tail, *t;
772
773 /*
774 * Nasty hack: here we push the contents of
775 * `predef' on to the top-level expansion stack,
776 * since this is the most convenient way to
777 * implement the pre-include and pre-define
778 * features.
779 */
780 list_for_each(pd, predef) {
781 head = NULL;
782 tail = &head;
783 list_for_each(t, pd->first) {
784 *tail = new_Token(NULL, t->type, t->text, 0);
785 tail = &(*tail)->next;
786 }
787
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800788 l = nasm_malloc(sizeof(Line));
789 l->next = istk->expansion;
790 l->first = head;
791 l->finishes = NULL;
792
793 istk->expansion = l;
Cyrill Gorcunov15bdc512010-07-13 11:27:41 +0400794 }
795 do_predef = false;
796 }
797 }
798
799 return line;
800}
801
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000802static char *read_line(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000803{
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +0400804 unsigned int size, c, next;
805 const unsigned int delta = 512;
806 const unsigned int pad = 8;
807 unsigned int nr_cont = 0;
808 bool cont = false;
809 char *buffer, *p;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000810
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +0400811 /* Standart macros set (predefined) goes first */
Cyrill Gorcunov15bdc512010-07-13 11:27:41 +0400812 p = line_from_stdmac();
813 if (p)
814 return p;
H. Peter Anvin72edbb82008-06-19 16:00:04 -0700815
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +0400816 size = delta;
817 p = buffer = nasm_malloc(size);
818
819 for (;;) {
820 c = fgetc(istk->fp);
821 if ((int)(c) == EOF) {
822 p[0] = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000823 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000824 }
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +0400825
826 switch (c) {
827 case '\r':
828 next = fgetc(istk->fp);
829 if (next != '\n')
830 ungetc(next, istk->fp);
831 if (cont) {
832 cont = false;
833 continue;
834 }
835 break;
836
837 case '\n':
838 if (cont) {
839 cont = false;
840 continue;
841 }
842 break;
843
844 case '\\':
845 next = fgetc(istk->fp);
846 ungetc(next, istk->fp);
Cyrill Gorcunov490f85e2012-12-27 20:02:17 +0400847 if (next == '\r' || next == '\n') {
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +0400848 cont = true;
849 nr_cont++;
850 continue;
851 }
852 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000853 }
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +0400854
855 if (c == '\r' || c == '\n') {
856 *p++ = 0;
857 break;
858 }
859
860 if (p >= (buffer + size - pad)) {
861 buffer = nasm_realloc(buffer, size + delta);
862 p = buffer + size - pad;
863 size += delta;
864 }
865
866 *p++ = (unsigned char)c;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000867 }
868
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +0400869 if (p == buffer) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000870 nasm_free(buffer);
871 return NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000872 }
873
Cyrill Gorcunova5aea572010-11-11 10:14:45 +0300874 src_set_linnum(src_get_linnum() + istk->lineinc +
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +0400875 (nr_cont * istk->lineinc));
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000876
877 /*
878 * Handle spurious ^Z, which may be inserted into source files
879 * by some file transfer utilities.
880 */
881 buffer[strcspn(buffer, "\032")] = '\0';
882
H. Peter Anvin8ac25aa2016-02-18 01:16:18 -0800883 lfmt->line(LIST_READ, buffer);
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000884
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000885 return buffer;
886}
887
888/*
Keith Kaniosb7a89542007-04-12 02:40:54 +0000889 * Tokenize a line of text. This is a very simple process since we
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000890 * don't need to parse the value out of e.g. numeric tokens: we
891 * simply split one string into many.
892 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000893static Token *tokenize(char *line)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000894{
H. Peter Anvinca544db2008-10-19 19:30:11 -0700895 char c, *p = line;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +0000896 enum pp_token_type type;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000897 Token *list = NULL;
898 Token *t, **tail = &list;
899
H. Peter Anvine2c80182005-01-15 22:15:51 +0000900 while (*line) {
901 p = line;
902 if (*p == '%') {
903 p++;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300904 if (*p == '+' && !nasm_isdigit(p[1])) {
905 p++;
906 type = TOK_PASTE;
907 } else if (nasm_isdigit(*p) ||
908 ((*p == '-' || *p == '+') && nasm_isdigit(p[1]))) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000909 do {
910 p++;
911 }
H. Peter Anvinbda7a6e2008-06-21 10:23:17 -0700912 while (nasm_isdigit(*p));
H. Peter Anvine2c80182005-01-15 22:15:51 +0000913 type = TOK_PREPROC_ID;
914 } else if (*p == '{') {
915 p++;
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800916 while (*p) {
917 if (*p == '}')
918 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000919 p[-1] = *p;
920 p++;
921 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800922 if (*p != '}')
H. Peter Anvin130736c2016-02-17 20:27:41 -0800923 nasm_error(ERR_WARNING | ERR_PASS1,
924 "unterminated %%{ construct");
H. Peter Anvine2c80182005-01-15 22:15:51 +0000925 p[-1] = '\0';
926 if (*p)
927 p++;
928 type = TOK_PREPROC_ID;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300929 } else if (*p == '[') {
930 int lvl = 1;
931 line += 2; /* Skip the leading %[ */
932 p++;
933 while (lvl && (c = *p++)) {
934 switch (c) {
935 case ']':
936 lvl--;
937 break;
938 case '%':
939 if (*p == '[')
940 lvl++;
941 break;
942 case '\'':
943 case '\"':
944 case '`':
Cyrill Gorcunov3144e842017-10-22 10:50:55 +0300945 p = nasm_skip_string(p - 1);
946 if (*p)
947 p++;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300948 break;
949 default:
950 break;
951 }
952 }
953 p--;
954 if (*p)
955 *p++ = '\0';
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800956 if (lvl)
H. Peter Anvin130736c2016-02-17 20:27:41 -0800957 nasm_error(ERR_NONFATAL|ERR_PASS1,
958 "unterminated %%[ construct");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300959 type = TOK_INDIRECT;
960 } else if (*p == '?') {
961 type = TOK_PREPROC_Q; /* %? */
962 p++;
963 if (*p == '?') {
964 type = TOK_PREPROC_QQ; /* %?? */
965 p++;
966 }
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +0400967 } else if (*p == '!') {
968 type = TOK_PREPROC_ID;
969 p++;
970 if (isidchar(*p)) {
971 do {
972 p++;
973 }
974 while (isidchar(*p));
975 } else if (*p == '\'' || *p == '\"' || *p == '`') {
976 p = nasm_skip_string(p);
977 if (*p)
978 p++;
979 else
H. Peter Anvin130736c2016-02-17 20:27:41 -0800980 nasm_error(ERR_NONFATAL|ERR_PASS1,
981 "unterminated %%! string");
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +0400982 } else {
983 /* %! without string or identifier */
984 type = TOK_OTHER; /* Legacy behavior... */
985 }
H. Peter Anvine2c80182005-01-15 22:15:51 +0000986 } else if (isidchar(*p) ||
987 ((*p == '!' || *p == '%' || *p == '$') &&
988 isidchar(p[1]))) {
989 do {
990 p++;
991 }
992 while (isidchar(*p));
993 type = TOK_PREPROC_ID;
994 } else {
995 type = TOK_OTHER;
996 if (*p == '%')
997 p++;
998 }
999 } else if (isidstart(*p) || (*p == '$' && isidstart(p[1]))) {
1000 type = TOK_ID;
1001 p++;
1002 while (*p && isidchar(*p))
1003 p++;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07001004 } else if (*p == '\'' || *p == '"' || *p == '`') {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001005 /*
1006 * A string token.
1007 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001008 type = TOK_STRING;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001009 p = nasm_skip_string(p);
Nickolay Yurchenkof3b3ce22003-09-21 20:38:43 +00001010
H. Peter Anvine2c80182005-01-15 22:15:51 +00001011 if (*p) {
1012 p++;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001013 } else {
H. Peter Anvin130736c2016-02-17 20:27:41 -08001014 nasm_error(ERR_WARNING|ERR_PASS1, "unterminated string");
H. Peter Anvine2c80182005-01-15 22:15:51 +00001015 /* Handling unterminated strings by UNV */
1016 /* type = -1; */
1017 }
Victor van den Elzenfb5f2512009-04-17 16:17:59 +02001018 } else if (p[0] == '$' && p[1] == '$') {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001019 type = TOK_OTHER; /* TOKEN_BASE */
Victor van den Elzenfb5f2512009-04-17 16:17:59 +02001020 p += 2;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001021 } else if (isnumstart(*p)) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001022 bool is_hex = false;
1023 bool is_float = false;
1024 bool has_e = false;
1025 char c, *r;
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001026
H. Peter Anvine2c80182005-01-15 22:15:51 +00001027 /*
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001028 * A numeric token.
H. Peter Anvine2c80182005-01-15 22:15:51 +00001029 */
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001030
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001031 if (*p == '$') {
1032 p++;
1033 is_hex = true;
1034 }
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001035
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001036 for (;;) {
1037 c = *p++;
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001038
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001039 if (!is_hex && (c == 'e' || c == 'E')) {
1040 has_e = true;
1041 if (*p == '+' || *p == '-') {
1042 /*
1043 * e can only be followed by +/- if it is either a
1044 * prefixed hex number or a floating-point number
1045 */
1046 p++;
1047 is_float = true;
1048 }
1049 } else if (c == 'H' || c == 'h' || c == 'X' || c == 'x') {
1050 is_hex = true;
1051 } else if (c == 'P' || c == 'p') {
1052 is_float = true;
1053 if (*p == '+' || *p == '-')
1054 p++;
Martin Lindheb150e382016-11-16 15:36:53 +01001055 } else if (isnumchar(c))
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001056 ; /* just advance */
1057 else if (c == '.') {
1058 /*
1059 * we need to deal with consequences of the legacy
1060 * parser, like "1.nolist" being two tokens
1061 * (TOK_NUMBER, TOK_ID) here; at least give it
1062 * a shot for now. In the future, we probably need
1063 * a flex-based scanner with proper pattern matching
1064 * to do it as well as it can be done. Nothing in
1065 * the world is going to help the person who wants
1066 * 0x123.p16 interpreted as two tokens, though.
1067 */
1068 r = p;
1069 while (*r == '_')
1070 r++;
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001071
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001072 if (nasm_isdigit(*r) || (is_hex && nasm_isxdigit(*r)) ||
1073 (!is_hex && (*r == 'e' || *r == 'E')) ||
1074 (*r == 'p' || *r == 'P')) {
1075 p = r;
1076 is_float = true;
1077 } else
1078 break; /* Terminate the token */
1079 } else
1080 break;
1081 }
1082 p--; /* Point to first character beyond number */
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001083
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001084 if (p == line+1 && *line == '$') {
1085 type = TOK_OTHER; /* TOKEN_HERE */
1086 } else {
1087 if (has_e && !is_hex) {
1088 /* 1e13 is floating-point, but 1e13h is not */
1089 is_float = true;
1090 }
H. Peter Anvind784a082009-04-20 14:01:18 -07001091
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001092 type = is_float ? TOK_FLOAT : TOK_NUMBER;
1093 }
H. Peter Anvinbda7a6e2008-06-21 10:23:17 -07001094 } else if (nasm_isspace(*p)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001095 type = TOK_WHITESPACE;
Cyrill Gorcunovf66ac7d2009-10-12 20:41:13 +04001096 p = nasm_skip_spaces(p);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001097 /*
1098 * Whitespace just before end-of-line is discarded by
1099 * pretending it's a comment; whitespace just before a
1100 * comment gets lumped into the comment.
1101 */
1102 if (!*p || *p == ';') {
1103 type = TOK_COMMENT;
1104 while (*p)
1105 p++;
1106 }
1107 } else if (*p == ';') {
1108 type = TOK_COMMENT;
1109 while (*p)
1110 p++;
1111 } else {
1112 /*
1113 * Anything else is an operator of some kind. We check
1114 * for all the double-character operators (>>, <<, //,
1115 * %%, <=, >=, ==, !=, <>, &&, ||, ^^), but anything
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001116 * else is a single-character operator.
H. Peter Anvine2c80182005-01-15 22:15:51 +00001117 */
1118 type = TOK_OTHER;
1119 if ((p[0] == '>' && p[1] == '>') ||
1120 (p[0] == '<' && p[1] == '<') ||
1121 (p[0] == '/' && p[1] == '/') ||
1122 (p[0] == '<' && p[1] == '=') ||
1123 (p[0] == '>' && p[1] == '=') ||
1124 (p[0] == '=' && p[1] == '=') ||
1125 (p[0] == '!' && p[1] == '=') ||
1126 (p[0] == '<' && p[1] == '>') ||
1127 (p[0] == '&' && p[1] == '&') ||
1128 (p[0] == '|' && p[1] == '|') ||
1129 (p[0] == '^' && p[1] == '^')) {
1130 p++;
1131 }
1132 p++;
1133 }
Nickolay Yurchenkof3b3ce22003-09-21 20:38:43 +00001134
H. Peter Anvine2c80182005-01-15 22:15:51 +00001135 /* Handling unterminated string by UNV */
1136 /*if (type == -1)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001137 {
1138 *tail = t = new_Token(NULL, TOK_STRING, line, p-line+1);
1139 t->text[p-line] = *line;
1140 tail = &t->next;
1141 }
1142 else */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001143 if (type != TOK_COMMENT) {
1144 *tail = t = new_Token(NULL, type, line, p - line);
1145 tail = &t->next;
1146 }
1147 line = p;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001148 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001149 return list;
1150}
1151
H. Peter Anvince616072002-04-30 21:02:23 +00001152/*
1153 * this function allocates a new managed block of memory and
H. Peter Anvin70653092007-10-19 14:42:29 -07001154 * returns a pointer to the block. The managed blocks are
H. Peter Anvince616072002-04-30 21:02:23 +00001155 * deleted only all at once by the delete_Blocks function.
1156 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001157static void *new_Block(size_t size)
H. Peter Anvince616072002-04-30 21:02:23 +00001158{
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001159 Blocks *b = &blocks;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001160
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001161 /* first, get to the end of the linked list */
1162 while (b->next)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001163 b = b->next;
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001164 /* now allocate the requested chunk */
1165 b->chunk = nasm_malloc(size);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001166
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001167 /* now allocate a new block for the next request */
Cyrill Gorcunovc31767c2014-06-28 02:22:17 +04001168 b->next = nasm_zalloc(sizeof(Blocks));
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001169 return b->chunk;
H. Peter Anvince616072002-04-30 21:02:23 +00001170}
1171
1172/*
1173 * this function deletes all managed blocks of memory
1174 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001175static void delete_Blocks(void)
H. Peter Anvince616072002-04-30 21:02:23 +00001176{
H. Peter Anvine2c80182005-01-15 22:15:51 +00001177 Blocks *a, *b = &blocks;
H. Peter Anvince616072002-04-30 21:02:23 +00001178
H. Peter Anvin70653092007-10-19 14:42:29 -07001179 /*
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001180 * keep in mind that the first block, pointed to by blocks
H. Peter Anvin70653092007-10-19 14:42:29 -07001181 * is a static and not dynamically allocated, so we don't
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001182 * free it.
1183 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001184 while (b) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001185 if (b->chunk)
1186 nasm_free(b->chunk);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001187 a = b;
1188 b = b->next;
1189 if (a != &blocks)
1190 nasm_free(a);
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001191 }
Cyrill Gorcunovdae24d72014-06-28 10:17:39 +04001192 memset(&blocks, 0, sizeof(blocks));
H. Peter Anvine2c80182005-01-15 22:15:51 +00001193}
H. Peter Anvin734b1882002-04-30 21:01:08 +00001194
1195/*
H. Peter Anvin70653092007-10-19 14:42:29 -07001196 * this function creates a new Token and passes a pointer to it
H. Peter Anvin734b1882002-04-30 21:01:08 +00001197 * back to the caller. It sets the type and text elements, and
H. Peter Anvinf26e0972008-07-01 21:26:27 -07001198 * also the a.mac and next elements to NULL.
H. Peter Anvin734b1882002-04-30 21:01:08 +00001199 */
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07001200static Token *new_Token(Token * next, enum pp_token_type type,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001201 const char *text, int txtlen)
H. Peter Anvin734b1882002-04-30 21:01:08 +00001202{
1203 Token *t;
1204 int i;
1205
H. Peter Anvin89cee572009-07-15 09:16:54 -04001206 if (!freeTokens) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001207 freeTokens = (Token *) new_Block(TOKEN_BLOCKSIZE * sizeof(Token));
1208 for (i = 0; i < TOKEN_BLOCKSIZE - 1; i++)
1209 freeTokens[i].next = &freeTokens[i + 1];
1210 freeTokens[i].next = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001211 }
1212 t = freeTokens;
1213 freeTokens = t->next;
1214 t->next = next;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07001215 t->a.mac = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001216 t->type = type;
H. Peter Anvin89cee572009-07-15 09:16:54 -04001217 if (type == TOK_WHITESPACE || !text) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001218 t->text = NULL;
1219 } else {
1220 if (txtlen == 0)
1221 txtlen = strlen(text);
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07001222 t->text = nasm_malloc(txtlen+1);
1223 memcpy(t->text, text, txtlen);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001224 t->text[txtlen] = '\0';
H. Peter Anvin734b1882002-04-30 21:01:08 +00001225 }
1226 return t;
1227}
1228
H. Peter Anvine2c80182005-01-15 22:15:51 +00001229static Token *delete_Token(Token * t)
H. Peter Anvin734b1882002-04-30 21:01:08 +00001230{
1231 Token *next = t->next;
1232 nasm_free(t->text);
H. Peter Anvin788e6c12002-04-30 21:02:01 +00001233 t->next = freeTokens;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001234 freeTokens = t;
1235 return next;
1236}
1237
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001238/*
1239 * Convert a line of tokens back into text.
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001240 * If expand_locals is not zero, identifiers of the form "%$*xxx"
1241 * will be transformed into ..@ctxnum.xxx
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001242 */
H. Peter Anvin9e200162008-06-04 17:23:14 -07001243static char *detoken(Token * tlist, bool expand_locals)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001244{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001245 Token *t;
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001246 char *line, *p;
H. Peter Anvinb4daadc2008-01-21 16:31:57 -08001247 const char *q;
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001248 int len = 0;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001249
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001250 list_for_each(t, tlist) {
Cyrill Gorcunov9b7ee092017-10-22 21:42:59 +03001251 if (t->type == TOK_PREPROC_ID && t->text &&
1252 t->text[0] && t->text[1] == '!') {
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001253 char *v;
1254 char *q = t->text;
H. Peter Anvin077fb932010-07-20 14:56:30 -07001255
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001256 v = t->text + 2;
1257 if (*v == '\'' || *v == '\"' || *v == '`') {
1258 size_t len = nasm_unquote(v, NULL);
1259 size_t clen = strlen(v);
H. Peter Anvin077fb932010-07-20 14:56:30 -07001260
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001261 if (len != clen) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08001262 nasm_error(ERR_NONFATAL | ERR_PASS1,
1263 "NUL character in %%! string");
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001264 v = NULL;
1265 }
1266 }
H. Peter Anvin077fb932010-07-20 14:56:30 -07001267
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001268 if (v) {
1269 char *p = getenv(v);
1270 if (!p) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08001271 nasm_error(ERR_NONFATAL | ERR_PASS1,
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001272 "nonexistent environment variable `%s'", v);
Cyrill Gorcunovbbb7a1a2016-06-19 12:15:24 +03001273 /*
1274 * FIXME We better should investigate if accessing
1275 * ->text[1] without ->text[0] is safe enough.
1276 */
1277 t->text = nasm_zalloc(2);
1278 } else
1279 t->text = nasm_strdup(p);
Cyrill Gorcunov75004872017-07-26 01:21:16 +03001280 nasm_free(q);
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001281 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001282 }
H. Peter Anvin077fb932010-07-20 14:56:30 -07001283
H. Peter Anvine2c80182005-01-15 22:15:51 +00001284 /* Expand local macros here and not during preprocessing */
1285 if (expand_locals &&
1286 t->type == TOK_PREPROC_ID && t->text &&
1287 t->text[0] == '%' && t->text[1] == '$') {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001288 const char *q;
1289 char *p;
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04001290 Context *ctx = get_ctx(t->text, &q);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001291 if (ctx) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001292 char buffer[40];
Keith Kanios93f2e9a2007-04-14 00:10:59 +00001293 snprintf(buffer, sizeof(buffer), "..@%"PRIu32".", ctx->number);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001294 p = nasm_strcat(buffer, q);
1295 nasm_free(t->text);
1296 t->text = p;
1297 }
1298 }
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001299 if (t->type == TOK_WHITESPACE)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001300 len++;
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001301 else if (t->text)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001302 len += strlen(t->text);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001303 }
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001304
H. Peter Anvin734b1882002-04-30 21:01:08 +00001305 p = line = nasm_malloc(len + 1);
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001306
1307 list_for_each(t, tlist) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001308 if (t->type == TOK_WHITESPACE) {
H. Peter Anvinb4daadc2008-01-21 16:31:57 -08001309 *p++ = ' ';
H. Peter Anvine2c80182005-01-15 22:15:51 +00001310 } else if (t->text) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001311 q = t->text;
1312 while (*q)
1313 *p++ = *q++;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001314 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001315 }
1316 *p = '\0';
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001317
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001318 return line;
1319}
1320
1321/*
H. Peter Anvin76690a12002-04-30 20:52:49 +00001322 * A scanner, suitable for use by the expression evaluator, which
1323 * operates on a line of Tokens. Expects a pointer to a pointer to
1324 * the first token in the line to be passed in as its private_data
1325 * field.
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001326 *
1327 * FIX: This really needs to be unified with stdscan.
H. Peter Anvin76690a12002-04-30 20:52:49 +00001328 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001329static int ppscan(void *private_data, struct tokenval *tokval)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001330{
H. Peter Anvin76690a12002-04-30 20:52:49 +00001331 Token **tlineptr = private_data;
1332 Token *tline;
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001333 char ourcopy[MAX_KEYWORD+1], *p, *r, *s;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001334
H. Peter Anvine2c80182005-01-15 22:15:51 +00001335 do {
1336 tline = *tlineptr;
1337 *tlineptr = tline ? tline->next : NULL;
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04001338 } while (tline && (tline->type == TOK_WHITESPACE ||
1339 tline->type == TOK_COMMENT));
H. Peter Anvin76690a12002-04-30 20:52:49 +00001340
1341 if (!tline)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001342 return tokval->t_type = TOKEN_EOS;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001343
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001344 tokval->t_charptr = tline->text;
1345
H. Peter Anvin76690a12002-04-30 20:52:49 +00001346 if (tline->text[0] == '$' && !tline->text[1])
H. Peter Anvine2c80182005-01-15 22:15:51 +00001347 return tokval->t_type = TOKEN_HERE;
H. Peter Anvin7cf897e2002-05-30 21:30:33 +00001348 if (tline->text[0] == '$' && tline->text[1] == '$' && !tline->text[2])
H. Peter Anvine2c80182005-01-15 22:15:51 +00001349 return tokval->t_type = TOKEN_BASE;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001350
H. Peter Anvine2c80182005-01-15 22:15:51 +00001351 if (tline->type == TOK_ID) {
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001352 p = tokval->t_charptr = tline->text;
1353 if (p[0] == '$') {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001354 tokval->t_charptr++;
1355 return tokval->t_type = TOKEN_ID;
1356 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00001357
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001358 for (r = p, s = ourcopy; *r; r++) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001359 if (r >= p+MAX_KEYWORD)
1360 return tokval->t_type = TOKEN_ID; /* Not a keyword */
H. Peter Anvinac8f8fc2008-06-11 15:49:41 -07001361 *s++ = nasm_tolower(*r);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001362 }
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001363 *s = '\0';
1364 /* right, so we have an identifier sitting in temp storage. now,
1365 * is it actually a register or instruction name, or what? */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001366 return nasm_token_hash(ourcopy, tokval);
H. Peter Anvin76690a12002-04-30 20:52:49 +00001367 }
1368
H. Peter Anvine2c80182005-01-15 22:15:51 +00001369 if (tline->type == TOK_NUMBER) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001370 bool rn_error;
1371 tokval->t_integer = readnum(tline->text, &rn_error);
1372 tokval->t_charptr = tline->text;
1373 if (rn_error)
1374 return tokval->t_type = TOKEN_ERRNUM;
1375 else
1376 return tokval->t_type = TOKEN_NUM;
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001377 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00001378
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001379 if (tline->type == TOK_FLOAT) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001380 return tokval->t_type = TOKEN_FLOAT;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001381 }
1382
H. Peter Anvine2c80182005-01-15 22:15:51 +00001383 if (tline->type == TOK_STRING) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001384 char bq, *ep;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001385
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001386 bq = tline->text[0];
H. Peter Anvin11627042008-06-09 20:45:19 -07001387 tokval->t_charptr = tline->text;
1388 tokval->t_inttwo = nasm_unquote(tline->text, &ep);
H. Peter Anvind2456592008-06-19 15:04:18 -07001389
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001390 if (ep[0] != bq || ep[1] != '\0')
1391 return tokval->t_type = TOKEN_ERRSTR;
1392 else
1393 return tokval->t_type = TOKEN_STR;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001394 }
1395
H. Peter Anvine2c80182005-01-15 22:15:51 +00001396 if (tline->type == TOK_OTHER) {
1397 if (!strcmp(tline->text, "<<"))
1398 return tokval->t_type = TOKEN_SHL;
1399 if (!strcmp(tline->text, ">>"))
1400 return tokval->t_type = TOKEN_SHR;
1401 if (!strcmp(tline->text, "//"))
1402 return tokval->t_type = TOKEN_SDIV;
1403 if (!strcmp(tline->text, "%%"))
1404 return tokval->t_type = TOKEN_SMOD;
1405 if (!strcmp(tline->text, "=="))
1406 return tokval->t_type = TOKEN_EQ;
1407 if (!strcmp(tline->text, "<>"))
1408 return tokval->t_type = TOKEN_NE;
1409 if (!strcmp(tline->text, "!="))
1410 return tokval->t_type = TOKEN_NE;
1411 if (!strcmp(tline->text, "<="))
1412 return tokval->t_type = TOKEN_LE;
1413 if (!strcmp(tline->text, ">="))
1414 return tokval->t_type = TOKEN_GE;
1415 if (!strcmp(tline->text, "&&"))
1416 return tokval->t_type = TOKEN_DBL_AND;
1417 if (!strcmp(tline->text, "^^"))
1418 return tokval->t_type = TOKEN_DBL_XOR;
1419 if (!strcmp(tline->text, "||"))
1420 return tokval->t_type = TOKEN_DBL_OR;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001421 }
1422
1423 /*
1424 * We have no other options: just return the first character of
1425 * the token text.
1426 */
1427 return tokval->t_type = tline->text[0];
1428}
1429
1430/*
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001431 * Compare a string to the name of an existing macro; this is a
1432 * simple wrapper which calls either strcmp or nasm_stricmp
1433 * depending on the value of the `casesense' parameter.
1434 */
H. Peter Anvin4db5a162007-10-11 13:42:09 -07001435static int mstrcmp(const char *p, const char *q, bool casesense)
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001436{
H. Peter Anvin734b1882002-04-30 21:01:08 +00001437 return casesense ? strcmp(p, q) : nasm_stricmp(p, q);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001438}
1439
1440/*
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07001441 * Compare a string to the name of an existing macro; this is a
1442 * simple wrapper which calls either strcmp or nasm_stricmp
1443 * depending on the value of the `casesense' parameter.
1444 */
1445static int mmemcmp(const char *p, const char *q, size_t l, bool casesense)
1446{
1447 return casesense ? memcmp(p, q, l) : nasm_memicmp(p, q, l);
1448}
1449
1450/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001451 * Return the Context structure associated with a %$ token. Return
1452 * NULL, having _already_ reported an error condition, if the
1453 * context stack isn't deep enough for the supplied number of $
1454 * signs.
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001455 *
1456 * If "namep" is non-NULL, set it to the pointer to the macro name
1457 * tail, i.e. the part beyond %$...
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001458 */
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04001459static Context *get_ctx(const char *name, const char **namep)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001460{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001461 Context *ctx;
1462 int i;
1463
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001464 if (namep)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001465 *namep = name;
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001466
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001467 if (!name || name[0] != '%' || name[1] != '$')
H. Peter Anvine2c80182005-01-15 22:15:51 +00001468 return NULL;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001469
H. Peter Anvine2c80182005-01-15 22:15:51 +00001470 if (!cstk) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08001471 nasm_error(ERR_NONFATAL, "`%s': context stack is empty", name);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001472 return NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001473 }
1474
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001475 name += 2;
1476 ctx = cstk;
1477 i = 0;
1478 while (ctx && *name == '$') {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001479 name++;
1480 i++;
1481 ctx = ctx->next;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001482 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001483 if (!ctx) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08001484 nasm_error(ERR_NONFATAL, "`%s': context stack is only"
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001485 " %d level%s deep", name, i, (i == 1 ? "" : "s"));
H. Peter Anvine2c80182005-01-15 22:15:51 +00001486 return NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001487 }
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001488
1489 if (namep)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001490 *namep = name;
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001491
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04001492 return ctx;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001493}
1494
1495/*
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001496 * Open an include file. This routine must always return a valid
1497 * file pointer if it returns - it's responsible for throwing an
1498 * ERR_FATAL and bombing out completely if not. It should also try
1499 * the include path one by one until it finds the file or reaches
1500 * the end of the path.
H. Peter Anvind81a2352016-09-21 14:03:18 -07001501 *
1502 * Note: for INC_PROBE the function returns NULL at all times;
1503 * instead look for the
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001504 */
H. Peter Anvind81a2352016-09-21 14:03:18 -07001505enum incopen_mode {
1506 INC_NEEDED, /* File must exist */
1507 INC_OPTIONAL, /* Missing is OK */
1508 INC_PROBE /* Only an existence probe */
1509};
1510
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001511/* This is conducts a full pathname search */
1512static FILE *inc_fopen_search(const char *file, StrList **slpath,
1513 enum incopen_mode omode, enum file_flags fmode)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001514{
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001515 FILE *fp;
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001516 char *prefix = "";
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001517 const IncPath *ip = ipath;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001518 int len = strlen(file);
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001519 size_t prefix_len = 0;
1520 StrList *sl;
Jim Kukunas65a8afc2016-06-13 16:00:42 -04001521 size_t path_len;
H. Peter Anvind81a2352016-09-21 14:03:18 -07001522 bool found;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001523
H. Peter Anvine2c80182005-01-15 22:15:51 +00001524 while (1) {
Jim Kukunas65a8afc2016-06-13 16:00:42 -04001525 path_len = prefix_len + len + 1;
1526
1527 sl = nasm_malloc(path_len + sizeof sl->next);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001528 memcpy(sl->str, prefix, prefix_len);
1529 memcpy(sl->str+prefix_len, file, len+1);
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001530 sl->next = NULL;
Jim Kukunas65a8afc2016-06-13 16:00:42 -04001531
H. Peter Anvind81a2352016-09-21 14:03:18 -07001532 if (omode == INC_PROBE) {
1533 fp = NULL;
1534 found = nasm_file_exists(sl->str);
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001535 } else {
H. Peter Anvind81a2352016-09-21 14:03:18 -07001536 fp = nasm_open_read(sl->str, fmode);
1537 found = (fp != NULL);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001538 }
H. Peter Anvind81a2352016-09-21 14:03:18 -07001539 if (found) {
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001540 *slpath = sl;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001541 return fp;
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001542 }
Jim Kukunas65a8afc2016-06-13 16:00:42 -04001543
H. Peter Anvind81a2352016-09-21 14:03:18 -07001544 nasm_free(sl);
Jim Kukunas65a8afc2016-06-13 16:00:42 -04001545
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001546 if (!ip)
1547 return NULL;
1548
1549 prefix = ip->path;
1550 prefix_len = strlen(prefix);
1551 ip = ip->next;
1552 }
1553}
1554
1555/*
1556 * Open a file, or test for the presence of one (depending on omode),
1557 * considering the include path.
1558 */
1559static FILE *inc_fopen(const char *file,
H. Peter Anvin9924d1e2016-10-04 00:59:39 -07001560 StrList **dhead,
H. Peter Anvinccad6f92016-10-04 00:34:35 -07001561 const char **found_path,
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001562 enum incopen_mode omode,
1563 enum file_flags fmode)
1564{
1565 StrList *sl;
1566 struct hash_insert hi;
1567 void **hp;
1568 char *path;
1569 FILE *fp = NULL;
1570
1571 hp = hash_find(&FileHash, file, &hi);
1572 if (hp) {
1573 path = *hp;
Martin Storsjöf283c8f2017-08-13 17:28:46 +03001574 if (path || omode != INC_NEEDED) {
H. Peter Anvin97fda4c2017-08-16 15:52:51 -07001575 nasm_add_string_to_strlist(dhead, path ? path : file);
Martin Storsjöf283c8f2017-08-13 17:28:46 +03001576 }
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001577 } else {
1578 /* Need to do the actual path search */
1579 size_t file_len;
1580
1581 sl = NULL;
1582 fp = inc_fopen_search(file, &sl, omode, fmode);
1583
1584 file_len = strlen(file);
1585
1586 if (!sl) {
1587 /* Store negative result for this file */
1588 sl = nasm_malloc(file_len + 1 + sizeof sl->next);
1589 memcpy(sl->str, file, file_len+1);
1590 sl->next = NULL;
1591 file = sl->str;
1592 path = NULL;
1593 } else {
1594 path = sl->str;
1595 file = strchr(path, '\0') - file_len;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001596 }
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001597
1598 hash_add(&hi, file, path); /* Positive or negative result */
1599
H. Peter Anvin9924d1e2016-10-04 00:59:39 -07001600 /*
1601 * Add file to dependency path. The in_list() is needed
1602 * in case the file was already added with %depend.
1603 */
1604 if (path || omode != INC_NEEDED)
H. Peter Anvin436e3672016-10-04 01:12:28 -07001605 nasm_add_to_strlist(dhead, sl);
H. Peter Anvineba20a72002-04-30 20:53:55 +00001606 }
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001607
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001608 if (!path) {
1609 if (omode == INC_NEEDED)
1610 nasm_fatal(0, "unable to open include file `%s'", file);
1611
1612 if (found_path)
1613 *found_path = NULL;
1614
1615 return NULL;
1616 }
1617
1618 if (!fp && omode != INC_PROBE)
Cyrill Gorcunov4ff8c632017-01-06 00:36:23 +03001619 fp = nasm_open_read(path, fmode);
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001620
1621 if (found_path)
H. Peter Anvinccad6f92016-10-04 00:34:35 -07001622 *found_path = path;
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001623
1624 return fp;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001625}
1626
1627/*
Fabian Giesen0bbc38d2016-04-28 13:48:14 -07001628 * Opens an include or input file. Public version, for use by modules
1629 * that get a file:lineno pair and need to look at the file again
1630 * (e.g. the CodeView debug backend). Returns NULL on failure.
1631 */
H. Peter Anvin3e83cec2016-05-25 04:28:46 -07001632FILE *pp_input_fopen(const char *filename, enum file_flags mode)
Fabian Giesen0bbc38d2016-04-28 13:48:14 -07001633{
H. Peter Anvin9924d1e2016-10-04 00:59:39 -07001634 return inc_fopen(filename, NULL, NULL, INC_OPTIONAL, mode);
Fabian Giesen0bbc38d2016-04-28 13:48:14 -07001635}
1636
1637/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001638 * Determine if we should warn on defining a single-line macro of
H. Peter Anvinef7468f2002-04-30 20:57:59 +00001639 * name `name', with `nparam' parameters. If nparam is 0 or -1, will
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001640 * return true if _any_ single-line macro of that name is defined.
1641 * Otherwise, will return true if a single-line macro with either
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001642 * `nparam' or no parameters is defined.
1643 *
1644 * If a macro with precisely the right number of parameters is
H. Peter Anvinef7468f2002-04-30 20:57:59 +00001645 * defined, or nparam is -1, the address of the definition structure
1646 * will be returned in `defn'; otherwise NULL will be returned. If `defn'
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001647 * is NULL, no action will be taken regarding its contents, and no
1648 * error will occur.
1649 *
1650 * Note that this is also called with nparam zero to resolve
1651 * `ifdef'.
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001652 *
1653 * If you already know which context macro belongs to, you can pass
1654 * the context pointer as first parameter; if you won't but name begins
1655 * with %$ the context will be automatically computed. If all_contexts
1656 * is true, macro will be searched in outer contexts as well.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001657 */
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07001658static bool
H. Peter Anvinb2a5fda2008-06-19 21:42:42 -07001659smacro_defined(Context * ctx, const char *name, int nparam, SMacro ** defn,
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07001660 bool nocase)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001661{
H. Peter Anvin166c2472008-05-28 12:28:58 -07001662 struct hash_table *smtbl;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001663 SMacro *m;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001664
H. Peter Anvin97a23472007-09-16 17:57:25 -07001665 if (ctx) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001666 smtbl = &ctx->localmac;
H. Peter Anvin97a23472007-09-16 17:57:25 -07001667 } else if (name[0] == '%' && name[1] == '$') {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001668 if (cstk)
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04001669 ctx = get_ctx(name, &name);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001670 if (!ctx)
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001671 return false; /* got to return _something_ */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001672 smtbl = &ctx->localmac;
H. Peter Anvin97a23472007-09-16 17:57:25 -07001673 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001674 smtbl = &smacros;
H. Peter Anvin97a23472007-09-16 17:57:25 -07001675 }
H. Peter Anvin166c2472008-05-28 12:28:58 -07001676 m = (SMacro *) hash_findix(smtbl, name);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001677
H. Peter Anvine2c80182005-01-15 22:15:51 +00001678 while (m) {
1679 if (!mstrcmp(m->name, name, m->casesense && nocase) &&
Charles Crayne192d5b52007-10-18 19:02:42 -07001680 (nparam <= 0 || m->nparam == 0 || nparam == (int) m->nparam)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001681 if (defn) {
Charles Crayne192d5b52007-10-18 19:02:42 -07001682 if (nparam == (int) m->nparam || nparam == -1)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001683 *defn = m;
1684 else
1685 *defn = NULL;
1686 }
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001687 return true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001688 }
1689 m = m->next;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001690 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001691
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001692 return false;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001693}
1694
1695/*
1696 * Count and mark off the parameters in a multi-line macro call.
1697 * This is called both from within the multi-line macro expansion
1698 * code, and also to mark off the default parameters when provided
1699 * in a %macro definition line.
1700 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001701static void count_mmac_params(Token * t, int *nparam, Token *** params)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001702{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001703 int paramsize, brace;
1704
1705 *nparam = paramsize = 0;
1706 *params = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001707 while (t) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001708 /* +1: we need space for the final NULL */
H. Peter Anvin91fb6f12008-09-01 10:56:33 -07001709 if (*nparam+1 >= paramsize) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001710 paramsize += PARAM_DELTA;
1711 *params = nasm_realloc(*params, sizeof(**params) * paramsize);
1712 }
1713 skip_white_(t);
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08001714 brace = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001715 if (tok_is_(t, "{"))
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08001716 brace++;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001717 (*params)[(*nparam)++] = t;
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08001718 if (brace) {
1719 while (brace && (t = t->next) != NULL) {
1720 if (tok_is_(t, "{"))
1721 brace++;
1722 else if (tok_is_(t, "}"))
1723 brace--;
1724 }
1725
1726 if (t) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001727 /*
1728 * Now we've found the closing brace, look further
1729 * for the comma.
1730 */
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08001731 t = t->next;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001732 skip_white_(t);
1733 if (tok_isnt_(t, ",")) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08001734 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00001735 "braces do not enclose all of macro parameter");
1736 while (tok_isnt_(t, ","))
1737 t = t->next;
1738 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001739 }
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08001740 } else {
1741 while (tok_isnt_(t, ","))
1742 t = t->next;
1743 }
1744 if (t) { /* got a comma/brace */
1745 t = t->next; /* eat the comma */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001746 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001747 }
1748}
1749
1750/*
H. Peter Anvin76690a12002-04-30 20:52:49 +00001751 * Determine whether one of the various `if' conditions is true or
1752 * not.
1753 *
1754 * We must free the tline we get passed.
1755 */
H. Peter Anvin70055962007-10-11 00:05:31 -07001756static bool if_condition(Token * tline, enum preproc_token ct)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001757{
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001758 enum pp_conditional i = PP_COND(ct);
H. Peter Anvin70055962007-10-11 00:05:31 -07001759 bool j;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001760 Token *t, *tt, **tptr, *origline;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001761 struct tokenval tokval;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001762 expr *evalresult;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001763 enum pp_token_type needtype;
H. Peter Anvin077fb932010-07-20 14:56:30 -07001764 char *p;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001765
1766 origline = tline;
1767
H. Peter Anvine2c80182005-01-15 22:15:51 +00001768 switch (i) {
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001769 case PPC_IFCTX:
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001770 j = false; /* have we matched yet? */
Victor van den Elzen0e857f12008-07-23 13:21:29 +02001771 while (true) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001772 skip_white_(tline);
Victor van den Elzen0e857f12008-07-23 13:21:29 +02001773 if (!tline)
1774 break;
1775 if (tline->type != TOK_ID) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08001776 nasm_error(ERR_NONFATAL,
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001777 "`%s' expects context identifiers", pp_directives[ct]);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001778 free_tlist(origline);
1779 return -1;
1780 }
Victor van den Elzen0e857f12008-07-23 13:21:29 +02001781 if (cstk && cstk->name && !nasm_stricmp(tline->text, cstk->name))
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001782 j = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001783 tline = tline->next;
1784 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001785 break;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001786
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001787 case PPC_IFDEF:
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001788 j = false; /* have we matched yet? */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001789 while (tline) {
1790 skip_white_(tline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001791 if (!tline || (tline->type != TOK_ID &&
1792 (tline->type != TOK_PREPROC_ID ||
1793 tline->text[1] != '$'))) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08001794 nasm_error(ERR_NONFATAL,
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001795 "`%s' expects macro identifiers", pp_directives[ct]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001796 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001797 }
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07001798 if (smacro_defined(NULL, tline->text, 0, NULL, true))
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001799 j = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001800 tline = tline->next;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001801 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001802 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001803
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001804 case PPC_IFENV:
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001805 tline = expand_smacro(tline);
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001806 j = false; /* have we matched yet? */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001807 while (tline) {
1808 skip_white_(tline);
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001809 if (!tline || (tline->type != TOK_ID &&
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001810 tline->type != TOK_STRING &&
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001811 (tline->type != TOK_PREPROC_ID ||
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001812 tline->text[1] != '!'))) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08001813 nasm_error(ERR_NONFATAL,
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001814 "`%s' expects environment variable names",
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001815 pp_directives[ct]);
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001816 goto fail;
1817 }
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001818 p = tline->text;
1819 if (tline->type == TOK_PREPROC_ID)
1820 p += 2; /* Skip leading %! */
1821 if (*p == '\'' || *p == '\"' || *p == '`')
1822 nasm_unquote_cstr(p, ct);
1823 if (getenv(p))
1824 j = true;
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001825 tline = tline->next;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001826 }
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001827 break;
1828
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001829 case PPC_IFIDN:
1830 case PPC_IFIDNI:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001831 tline = expand_smacro(tline);
1832 t = tt = tline;
1833 while (tok_isnt_(tt, ","))
1834 tt = tt->next;
1835 if (!tt) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08001836 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00001837 "`%s' expects two comma-separated arguments",
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001838 pp_directives[ct]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001839 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001840 }
1841 tt = tt->next;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001842 j = true; /* assume equality unless proved not */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001843 while ((t->type != TOK_OTHER || strcmp(t->text, ",")) && tt) {
1844 if (tt->type == TOK_OTHER && !strcmp(tt->text, ",")) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08001845 nasm_error(ERR_NONFATAL, "`%s': more than one comma on line",
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001846 pp_directives[ct]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001847 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001848 }
1849 if (t->type == TOK_WHITESPACE) {
1850 t = t->next;
1851 continue;
1852 }
1853 if (tt->type == TOK_WHITESPACE) {
1854 tt = tt->next;
1855 continue;
1856 }
1857 if (tt->type != t->type) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001858 j = false; /* found mismatching tokens */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001859 break;
1860 }
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07001861 /* When comparing strings, need to unquote them first */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001862 if (t->type == TOK_STRING) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001863 size_t l1 = nasm_unquote(t->text, NULL);
1864 size_t l2 = nasm_unquote(tt->text, NULL);
H. Peter Anvind2456592008-06-19 15:04:18 -07001865
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001866 if (l1 != l2) {
1867 j = false;
1868 break;
1869 }
1870 if (mmemcmp(t->text, tt->text, l1, i == PPC_IFIDN)) {
1871 j = false;
1872 break;
1873 }
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07001874 } else if (mstrcmp(tt->text, t->text, i == PPC_IFIDN) != 0) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001875 j = false; /* found mismatching tokens */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001876 break;
1877 }
Nickolay Yurchenkof3b3ce22003-09-21 20:38:43 +00001878
H. Peter Anvine2c80182005-01-15 22:15:51 +00001879 t = t->next;
1880 tt = tt->next;
1881 }
1882 if ((t->type != TOK_OTHER || strcmp(t->text, ",")) || tt)
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001883 j = false; /* trailing gunk on one end or other */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001884 break;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001885
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001886 case PPC_IFMACRO:
H. Peter Anvin89cee572009-07-15 09:16:54 -04001887 {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001888 bool found = false;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001889 MMacro searching, *mmac;
H. Peter Anvin65747262002-05-07 00:10:05 +00001890
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001891 skip_white_(tline);
1892 tline = expand_id(tline);
1893 if (!tok_type_(tline, TOK_ID)) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08001894 nasm_error(ERR_NONFATAL,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001895 "`%s' expects a macro name", pp_directives[ct]);
1896 goto fail;
1897 }
1898 searching.name = nasm_strdup(tline->text);
1899 searching.casesense = true;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001900 searching.plus = false;
1901 searching.nolist = false;
1902 searching.in_progress = 0;
1903 searching.max_depth = 0;
1904 searching.rep_nest = NULL;
1905 searching.nparam_min = 0;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001906 searching.nparam_max = INT_MAX;
1907 tline = expand_smacro(tline->next);
1908 skip_white_(tline);
1909 if (!tline) {
1910 } else if (!tok_type_(tline, TOK_NUMBER)) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08001911 nasm_error(ERR_NONFATAL,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001912 "`%s' expects a parameter count or nothing",
1913 pp_directives[ct]);
1914 } else {
1915 searching.nparam_min = searching.nparam_max =
1916 readnum(tline->text, &j);
1917 if (j)
H. Peter Anvin130736c2016-02-17 20:27:41 -08001918 nasm_error(ERR_NONFATAL,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001919 "unable to parse parameter count `%s'",
1920 tline->text);
1921 }
1922 if (tline && tok_is_(tline->next, "-")) {
1923 tline = tline->next->next;
1924 if (tok_is_(tline, "*"))
1925 searching.nparam_max = INT_MAX;
1926 else if (!tok_type_(tline, TOK_NUMBER))
H. Peter Anvin130736c2016-02-17 20:27:41 -08001927 nasm_error(ERR_NONFATAL,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001928 "`%s' expects a parameter count after `-'",
1929 pp_directives[ct]);
1930 else {
1931 searching.nparam_max = readnum(tline->text, &j);
1932 if (j)
H. Peter Anvin130736c2016-02-17 20:27:41 -08001933 nasm_error(ERR_NONFATAL,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001934 "unable to parse parameter count `%s'",
1935 tline->text);
Cyrill Gorcunovc9244ea2017-10-22 15:25:48 +03001936 if (searching.nparam_min > searching.nparam_max) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08001937 nasm_error(ERR_NONFATAL,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001938 "minimum parameter count exceeds maximum");
Cyrill Gorcunovc9244ea2017-10-22 15:25:48 +03001939 searching.nparam_max = searching.nparam_min;
1940 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001941 }
1942 }
1943 if (tline && tok_is_(tline->next, "+")) {
1944 tline = tline->next;
1945 searching.plus = true;
1946 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001947 mmac = (MMacro *) hash_findix(&mmacros, searching.name);
1948 while (mmac) {
1949 if (!strcmp(mmac->name, searching.name) &&
1950 (mmac->nparam_min <= searching.nparam_max
1951 || searching.plus)
1952 && (searching.nparam_min <= mmac->nparam_max
1953 || mmac->plus)) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001954 found = true;
1955 break;
1956 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001957 mmac = mmac->next;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001958 }
1959 if (tline && tline->next)
H. Peter Anvin130736c2016-02-17 20:27:41 -08001960 nasm_error(ERR_WARNING|ERR_PASS1,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001961 "trailing garbage after %%ifmacro ignored");
1962 nasm_free(searching.name);
1963 j = found;
1964 break;
H. Peter Anvin89cee572009-07-15 09:16:54 -04001965 }
H. Peter Anvin65747262002-05-07 00:10:05 +00001966
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001967 case PPC_IFID:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001968 needtype = TOK_ID;
1969 goto iftype;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001970 case PPC_IFNUM:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001971 needtype = TOK_NUMBER;
1972 goto iftype;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001973 case PPC_IFSTR:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001974 needtype = TOK_STRING;
1975 goto iftype;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001976
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001977iftype:
1978 t = tline = expand_smacro(tline);
H. Peter Anvind85d2502008-05-04 17:53:31 -07001979
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001980 while (tok_type_(t, TOK_WHITESPACE) ||
1981 (needtype == TOK_NUMBER &&
1982 tok_type_(t, TOK_OTHER) &&
1983 (t->text[0] == '-' || t->text[0] == '+') &&
1984 !t->text[1]))
1985 t = t->next;
H. Peter Anvind85d2502008-05-04 17:53:31 -07001986
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001987 j = tok_type_(t, needtype);
1988 break;
H. Peter Anvincbf768d2008-02-16 16:41:25 -08001989
1990 case PPC_IFTOKEN:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001991 t = tline = expand_smacro(tline);
1992 while (tok_type_(t, TOK_WHITESPACE))
1993 t = t->next;
H. Peter Anvincbf768d2008-02-16 16:41:25 -08001994
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001995 j = false;
1996 if (t) {
1997 t = t->next; /* Skip the actual token */
1998 while (tok_type_(t, TOK_WHITESPACE))
1999 t = t->next;
2000 j = !t; /* Should be nothing left */
2001 }
2002 break;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002003
H. Peter Anvin134b9462008-02-16 17:01:40 -08002004 case PPC_IFEMPTY:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002005 t = tline = expand_smacro(tline);
2006 while (tok_type_(t, TOK_WHITESPACE))
2007 t = t->next;
H. Peter Anvin134b9462008-02-16 17:01:40 -08002008
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002009 j = !t; /* Should be empty */
2010 break;
H. Peter Anvin134b9462008-02-16 17:01:40 -08002011
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002012 case PPC_IF:
H. Peter Anvine2c80182005-01-15 22:15:51 +00002013 t = tline = expand_smacro(tline);
2014 tptr = &t;
2015 tokval.t_type = TOKEN_INVALID;
2016 evalresult = evaluate(ppscan, tptr, &tokval,
H. Peter Anvin130736c2016-02-17 20:27:41 -08002017 NULL, pass | CRITICAL, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002018 if (!evalresult)
2019 return -1;
2020 if (tokval.t_type)
H. Peter Anvin130736c2016-02-17 20:27:41 -08002021 nasm_error(ERR_WARNING|ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +00002022 "trailing garbage after expression ignored");
2023 if (!is_simple(evalresult)) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002024 nasm_error(ERR_NONFATAL,
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002025 "non-constant value given to `%s'", pp_directives[ct]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002026 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002027 }
Chuck Crayne60ae75d2007-05-02 01:59:16 +00002028 j = reloc_value(evalresult) != 0;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002029 break;
H. Peter Anvin95e28822007-09-12 04:20:08 +00002030
H. Peter Anvine2c80182005-01-15 22:15:51 +00002031 default:
H. Peter Anvin130736c2016-02-17 20:27:41 -08002032 nasm_error(ERR_FATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00002033 "preprocessor directive `%s' not yet implemented",
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002034 pp_directives[ct]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002035 goto fail;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002036 }
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002037
2038 free_tlist(origline);
2039 return j ^ PP_NEGATIVE(ct);
H. Peter Anvin70653092007-10-19 14:42:29 -07002040
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002041fail:
2042 free_tlist(origline);
2043 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002044}
2045
2046/*
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002047 * Common code for defining an smacro
2048 */
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08002049static bool define_smacro(Context *ctx, const char *mname, bool casesense,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002050 int nparam, Token *expansion)
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002051{
2052 SMacro *smac, **smhead;
H. Peter Anvin166c2472008-05-28 12:28:58 -07002053 struct hash_table *smtbl;
H. Peter Anvin70653092007-10-19 14:42:29 -07002054
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002055 if (smacro_defined(ctx, mname, nparam, &smac, casesense)) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002056 if (!smac) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002057 nasm_error(ERR_WARNING|ERR_PASS1,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002058 "single-line macro `%s' defined both with and"
2059 " without parameters", mname);
2060 /*
2061 * Some instances of the old code considered this a failure,
2062 * some others didn't. What is the right thing to do here?
2063 */
2064 free_tlist(expansion);
2065 return false; /* Failure */
2066 } else {
2067 /*
2068 * We're redefining, so we have to take over an
2069 * existing SMacro structure. This means freeing
2070 * what was already in it.
2071 */
2072 nasm_free(smac->name);
2073 free_tlist(smac->expansion);
2074 }
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002075 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002076 smtbl = ctx ? &ctx->localmac : &smacros;
2077 smhead = (SMacro **) hash_findi_add(smtbl, mname);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002078 smac = nasm_malloc(sizeof(SMacro));
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002079 smac->next = *smhead;
2080 *smhead = smac;
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002081 }
2082 smac->name = nasm_strdup(mname);
2083 smac->casesense = casesense;
2084 smac->nparam = nparam;
2085 smac->expansion = expansion;
2086 smac->in_progress = false;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002087 return true; /* Success */
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002088}
2089
2090/*
2091 * Undefine an smacro
2092 */
2093static void undef_smacro(Context *ctx, const char *mname)
2094{
2095 SMacro **smhead, *s, **sp;
H. Peter Anvin166c2472008-05-28 12:28:58 -07002096 struct hash_table *smtbl;
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002097
H. Peter Anvin166c2472008-05-28 12:28:58 -07002098 smtbl = ctx ? &ctx->localmac : &smacros;
2099 smhead = (SMacro **)hash_findi(smtbl, mname, NULL);
H. Peter Anvin70653092007-10-19 14:42:29 -07002100
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002101 if (smhead) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002102 /*
2103 * We now have a macro name... go hunt for it.
2104 */
2105 sp = smhead;
2106 while ((s = *sp) != NULL) {
2107 if (!mstrcmp(s->name, mname, s->casesense)) {
2108 *sp = s->next;
2109 nasm_free(s->name);
2110 free_tlist(s->expansion);
2111 nasm_free(s);
2112 } else {
2113 sp = &s->next;
2114 }
2115 }
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002116 }
2117}
2118
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002119/*
H. Peter Anvina26433d2008-07-16 14:40:01 -07002120 * Parse a mmacro specification.
2121 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002122static bool parse_mmacro_spec(Token *tline, MMacro *def, const char *directive)
H. Peter Anvina26433d2008-07-16 14:40:01 -07002123{
2124 bool err;
2125
2126 tline = tline->next;
2127 skip_white_(tline);
2128 tline = expand_id(tline);
2129 if (!tok_type_(tline, TOK_ID)) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002130 nasm_error(ERR_NONFATAL, "`%s' expects a macro name", directive);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002131 return false;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002132 }
Victor van den Elzenb916ede2008-07-23 15:14:22 +02002133
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002134 def->prev = NULL;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002135 def->name = nasm_strdup(tline->text);
2136 def->plus = false;
2137 def->nolist = false;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002138 def->in_progress = 0;
2139 def->rep_nest = NULL;
Victor van den Elzenb916ede2008-07-23 15:14:22 +02002140 def->nparam_min = 0;
2141 def->nparam_max = 0;
2142
H. Peter Anvina26433d2008-07-16 14:40:01 -07002143 tline = expand_smacro(tline->next);
2144 skip_white_(tline);
2145 if (!tok_type_(tline, TOK_NUMBER)) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002146 nasm_error(ERR_NONFATAL, "`%s' expects a parameter count", directive);
H. Peter Anvina26433d2008-07-16 14:40:01 -07002147 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002148 def->nparam_min = def->nparam_max =
2149 readnum(tline->text, &err);
2150 if (err)
H. Peter Anvin130736c2016-02-17 20:27:41 -08002151 nasm_error(ERR_NONFATAL,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002152 "unable to parse parameter count `%s'", tline->text);
H. Peter Anvina26433d2008-07-16 14:40:01 -07002153 }
2154 if (tline && tok_is_(tline->next, "-")) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002155 tline = tline->next->next;
2156 if (tok_is_(tline, "*")) {
2157 def->nparam_max = INT_MAX;
2158 } else if (!tok_type_(tline, TOK_NUMBER)) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002159 nasm_error(ERR_NONFATAL,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002160 "`%s' expects a parameter count after `-'", directive);
2161 } else {
2162 def->nparam_max = readnum(tline->text, &err);
2163 if (err) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002164 nasm_error(ERR_NONFATAL, "unable to parse parameter count `%s'",
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002165 tline->text);
2166 }
2167 if (def->nparam_min > def->nparam_max) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002168 nasm_error(ERR_NONFATAL, "minimum parameter count exceeds maximum");
Cyrill Gorcunovc9244ea2017-10-22 15:25:48 +03002169 def->nparam_max = def->nparam_min;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002170 }
2171 }
H. Peter Anvina26433d2008-07-16 14:40:01 -07002172 }
2173 if (tline && tok_is_(tline->next, "+")) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002174 tline = tline->next;
2175 def->plus = true;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002176 }
2177 if (tline && tok_type_(tline->next, TOK_ID) &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002178 !nasm_stricmp(tline->next->text, ".nolist")) {
2179 tline = tline->next;
2180 def->nolist = true;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002181 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002182
H. Peter Anvina26433d2008-07-16 14:40:01 -07002183 /*
2184 * Handle default parameters.
2185 */
2186 if (tline && tline->next) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002187 def->dlist = tline->next;
2188 tline->next = NULL;
2189 count_mmac_params(def->dlist, &def->ndefs, &def->defaults);
H. Peter Anvina26433d2008-07-16 14:40:01 -07002190 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002191 def->dlist = NULL;
2192 def->defaults = NULL;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002193 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002194 def->expansion = NULL;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002195
H. Peter Anvin89cee572009-07-15 09:16:54 -04002196 if (def->defaults && def->ndefs > def->nparam_max - def->nparam_min &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002197 !def->plus)
H. Peter Anvin130736c2016-02-17 20:27:41 -08002198 nasm_error(ERR_WARNING|ERR_PASS1|ERR_WARN_MDP,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002199 "too many default macro parameters");
Victor van den Elzenb916ede2008-07-23 15:14:22 +02002200
H. Peter Anvina26433d2008-07-16 14:40:01 -07002201 return true;
2202}
2203
2204
2205/*
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002206 * Decode a size directive
2207 */
2208static int parse_size(const char *str) {
2209 static const char *size_names[] =
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002210 { "byte", "dword", "oword", "qword", "tword", "word", "yword" };
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002211 static const int sizes[] =
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002212 { 0, 1, 4, 16, 8, 10, 2, 32 };
Cyrill Gorcunovc713b5f2018-09-29 14:30:14 +03002213 return str ? sizes[bsii(str, size_names, ARRAY_SIZE(size_names))+1] : 0;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002214}
2215
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07002216/*
2217 * Process a preprocessor %pragma directive. Currently there are none.
2218 * Gets passed the token list starting with the "preproc" token from
2219 * "%pragma preproc".
2220 */
2221static void do_pragma_preproc(Token *tline)
2222{
2223 /* Skip to the real stuff */
2224 tline = tline->next;
2225 skip_white_(tline);
2226 if (!tline)
2227 return;
2228
2229 (void)tline; /* Nothing else to do at present */
2230}
2231
Ed Beroset3ab3f412002-06-11 03:31:49 +00002232/**
2233 * find and process preprocessor directive in passed line
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002234 * Find out if a line contains a preprocessor directive, and deal
2235 * with it if so.
H. Peter Anvin70653092007-10-19 14:42:29 -07002236 *
Ed Beroset3ab3f412002-06-11 03:31:49 +00002237 * If a directive _is_ found, it is the responsibility of this routine
2238 * (and not the caller) to free_tlist() the line.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002239 *
Ed Beroset3ab3f412002-06-11 03:31:49 +00002240 * @param tline a pointer to the current tokeninzed line linked list
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07002241 * @param output if this directive generated output
Ed Beroset3ab3f412002-06-11 03:31:49 +00002242 * @return DIRECTIVE_FOUND or NO_DIRECTIVE_FOUND
H. Peter Anvin70653092007-10-19 14:42:29 -07002243 *
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002244 */
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07002245static int do_directive(Token *tline, char **output)
H. Peter Anvineba20a72002-04-30 20:53:55 +00002246{
H. Peter Anvin4169a472007-09-12 01:29:43 +00002247 enum preproc_token i;
2248 int j;
H. Peter Anvin70055962007-10-11 00:05:31 -07002249 bool err;
2250 int nparam;
2251 bool nolist;
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07002252 bool casesense;
H. Peter Anvin8cfdb9d2007-09-14 18:36:01 -07002253 int k, m;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002254 int offset;
H. Peter Anvinccad6f92016-10-04 00:34:35 -07002255 char *p, *pp;
2256 const char *found_path;
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08002257 const char *mname;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002258 Include *inc;
2259 Context *ctx;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002260 Cond *cond;
2261 MMacro *mmac, **mmhead;
Jin Kyu Songc9486b92013-10-28 17:07:57 -07002262 Token *t = NULL, *tt, *param_start, *macro_start, *last, **tptr, *origline;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002263 Line *l;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002264 struct tokenval tokval;
2265 expr *evalresult;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002266 MMacro *tmp_defining; /* Used when manipulating rep_nest */
H. Peter Anvinf8ba53e2007-10-11 10:11:57 -07002267 int64_t count;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07002268 size_t len;
H. Peter Anvin8e3f75e2008-09-24 00:21:58 -07002269 int severity;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002270
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07002271 *output = NULL; /* No output generated */
H. Peter Anvin76690a12002-04-30 20:52:49 +00002272 origline = tline;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002273
H. Peter Anvineba20a72002-04-30 20:53:55 +00002274 skip_white_(tline);
H. Peter Anvinf2936d72008-06-04 15:11:23 -07002275 if (!tline || !tok_type_(tline, TOK_PREPROC_ID) ||
H. Peter Anvine2c80182005-01-15 22:15:51 +00002276 (tline->text[1] == '%' || tline->text[1] == '$'
2277 || tline->text[1] == '!'))
2278 return NO_DIRECTIVE_FOUND;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002279
H. Peter Anvin4169a472007-09-12 01:29:43 +00002280 i = pp_token_hash(tline->text);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002281
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002282 /*
2283 * FIXME: We zap execution of PP_RMACRO, PP_IRMACRO, PP_EXITMACRO
2284 * since they are known to be buggy at moment, we need to fix them
2285 * in future release (2.09-2.10)
2286 */
Philipp Klokeb432f572013-03-31 12:01:23 +02002287 if (i == PP_RMACRO || i == PP_IRMACRO || i == PP_EXITMACRO) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002288 nasm_error(ERR_NONFATAL, "unknown preprocessor directive `%s'",
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002289 tline->text);
2290 return NO_DIRECTIVE_FOUND;
2291 }
2292
2293 /*
2294 * If we're in a non-emitting branch of a condition construct,
2295 * or walking to the end of an already terminated %rep block,
2296 * we should ignore all directives except for condition
2297 * directives.
2298 */
2299 if (((istk->conds && !emitting(istk->conds->state)) ||
2300 (istk->mstk && !istk->mstk->in_progress)) && !is_condition(i)) {
2301 return NO_DIRECTIVE_FOUND;
2302 }
2303
2304 /*
2305 * If we're defining a macro or reading a %rep block, we should
2306 * ignore all directives except for %macro/%imacro (which nest),
2307 * %endm/%endmacro, and (only if we're in a %rep block) %endrep.
2308 * If we're in a %rep block, another %rep nests, so should be let through.
2309 */
2310 if (defining && i != PP_MACRO && i != PP_IMACRO &&
2311 i != PP_RMACRO && i != PP_IRMACRO &&
2312 i != PP_ENDMACRO && i != PP_ENDM &&
2313 (defining->name || (i != PP_ENDREP && i != PP_REP))) {
2314 return NO_DIRECTIVE_FOUND;
2315 }
2316
2317 if (defining) {
2318 if (i == PP_MACRO || i == PP_IMACRO ||
2319 i == PP_RMACRO || i == PP_IRMACRO) {
2320 nested_mac_count++;
2321 return NO_DIRECTIVE_FOUND;
2322 } else if (nested_mac_count > 0) {
2323 if (i == PP_ENDMACRO) {
2324 nested_mac_count--;
2325 return NO_DIRECTIVE_FOUND;
2326 }
2327 }
2328 if (!defining->name) {
2329 if (i == PP_REP) {
2330 nested_rep_count++;
2331 return NO_DIRECTIVE_FOUND;
2332 } else if (nested_rep_count > 0) {
2333 if (i == PP_ENDREP) {
2334 nested_rep_count--;
2335 return NO_DIRECTIVE_FOUND;
2336 }
2337 }
2338 }
2339 }
2340
H. Peter Anvin4169a472007-09-12 01:29:43 +00002341 switch (i) {
2342 case PP_INVALID:
H. Peter Anvin130736c2016-02-17 20:27:41 -08002343 nasm_error(ERR_NONFATAL, "unknown preprocessor directive `%s'",
H. Peter Anvine2c80182005-01-15 22:15:51 +00002344 tline->text);
2345 return NO_DIRECTIVE_FOUND; /* didn't get it */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002346
H. Peter Anvin3f87a2a2016-10-04 14:07:19 -07002347 case PP_PRAGMA:
2348 /*
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07002349 * %pragma namespace options...
2350 *
2351 * The namespace "preproc" is reserved for the preprocessor;
2352 * all other namespaces generate a [pragma] assembly directive.
2353 *
2354 * Invalid %pragmas are ignored and may have different
2355 * meaning in future versions of NASM.
H. Peter Anvin3f87a2a2016-10-04 14:07:19 -07002356 */
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07002357 tline = tline->next;
2358 skip_white_(tline);
2359 tline = expand_smacro(tline);
2360 if (tok_type_(tline, TOK_ID)) {
2361 if (!nasm_stricmp(tline->text, "preproc")) {
2362 /* Preprocessor pragma */
2363 do_pragma_preproc(tline);
2364 } else {
2365 /* Build the assembler directive */
2366 t = new_Token(NULL, TOK_OTHER, "[", 1);
2367 t->next = new_Token(NULL, TOK_ID, "pragma", 6);
2368 t->next->next = new_Token(tline, TOK_WHITESPACE, NULL, 0);
2369 tline = t;
2370 for (t = tline; t->next; t = t->next)
2371 ;
2372 t->next = new_Token(NULL, TOK_OTHER, "]", 1);
2373 /* true here can be revisited in the future */
2374 *output = detoken(tline, true);
2375 }
2376 }
H. Peter Anvin3f87a2a2016-10-04 14:07:19 -07002377 free_tlist(origline);
2378 return DIRECTIVE_FOUND;
2379
H. Peter Anvine2c80182005-01-15 22:15:51 +00002380 case PP_STACKSIZE:
2381 /* Directive to tell NASM what the default stack size is. The
2382 * default is for a 16-bit stack, and this can be overriden with
2383 * %stacksize large.
H. Peter Anvine2c80182005-01-15 22:15:51 +00002384 */
2385 tline = tline->next;
2386 if (tline && tline->type == TOK_WHITESPACE)
2387 tline = tline->next;
2388 if (!tline || tline->type != TOK_ID) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002389 nasm_error(ERR_NONFATAL, "`%%stacksize' missing size parameter");
H. Peter Anvine2c80182005-01-15 22:15:51 +00002390 free_tlist(origline);
2391 return DIRECTIVE_FOUND;
2392 }
2393 if (nasm_stricmp(tline->text, "flat") == 0) {
2394 /* All subsequent ARG directives are for a 32-bit stack */
2395 StackSize = 4;
2396 StackPointer = "ebp";
2397 ArgOffset = 8;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002398 LocalOffset = 0;
Charles Crayne7eaf9192007-11-08 22:11:14 -08002399 } else if (nasm_stricmp(tline->text, "flat64") == 0) {
2400 /* All subsequent ARG directives are for a 64-bit stack */
2401 StackSize = 8;
2402 StackPointer = "rbp";
Per Jessen53252e02010-02-11 00:16:59 +03002403 ArgOffset = 16;
Charles Crayne7eaf9192007-11-08 22:11:14 -08002404 LocalOffset = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002405 } else if (nasm_stricmp(tline->text, "large") == 0) {
2406 /* All subsequent ARG directives are for a 16-bit stack,
2407 * far function call.
2408 */
2409 StackSize = 2;
2410 StackPointer = "bp";
2411 ArgOffset = 4;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002412 LocalOffset = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002413 } else if (nasm_stricmp(tline->text, "small") == 0) {
2414 /* All subsequent ARG directives are for a 16-bit stack,
2415 * far function call. We don't support near functions.
2416 */
2417 StackSize = 2;
2418 StackPointer = "bp";
2419 ArgOffset = 6;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002420 LocalOffset = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002421 } else {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002422 nasm_error(ERR_NONFATAL, "`%%stacksize' invalid size type");
H. Peter Anvine2c80182005-01-15 22:15:51 +00002423 free_tlist(origline);
2424 return DIRECTIVE_FOUND;
2425 }
2426 free_tlist(origline);
2427 return DIRECTIVE_FOUND;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002428
H. Peter Anvine2c80182005-01-15 22:15:51 +00002429 case PP_ARG:
2430 /* TASM like ARG directive to define arguments to functions, in
2431 * the following form:
2432 *
2433 * ARG arg1:WORD, arg2:DWORD, arg4:QWORD
2434 */
2435 offset = ArgOffset;
2436 do {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00002437 char *arg, directive[256];
H. Peter Anvine2c80182005-01-15 22:15:51 +00002438 int size = StackSize;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002439
H. Peter Anvine2c80182005-01-15 22:15:51 +00002440 /* Find the argument name */
2441 tline = tline->next;
2442 if (tline && tline->type == TOK_WHITESPACE)
2443 tline = tline->next;
2444 if (!tline || tline->type != TOK_ID) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002445 nasm_error(ERR_NONFATAL, "`%%arg' missing argument parameter");
H. Peter Anvine2c80182005-01-15 22:15:51 +00002446 free_tlist(origline);
2447 return DIRECTIVE_FOUND;
2448 }
2449 arg = tline->text;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002450
H. Peter Anvine2c80182005-01-15 22:15:51 +00002451 /* Find the argument size type */
2452 tline = tline->next;
2453 if (!tline || tline->type != TOK_OTHER
2454 || tline->text[0] != ':') {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002455 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00002456 "Syntax error processing `%%arg' directive");
2457 free_tlist(origline);
2458 return DIRECTIVE_FOUND;
2459 }
2460 tline = tline->next;
2461 if (!tline || tline->type != TOK_ID) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002462 nasm_error(ERR_NONFATAL, "`%%arg' missing size type parameter");
H. Peter Anvine2c80182005-01-15 22:15:51 +00002463 free_tlist(origline);
2464 return DIRECTIVE_FOUND;
2465 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002466
H. Peter Anvine2c80182005-01-15 22:15:51 +00002467 /* Allow macro expansion of type parameter */
Keith Kaniosb7a89542007-04-12 02:40:54 +00002468 tt = tokenize(tline->text);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002469 tt = expand_smacro(tt);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002470 size = parse_size(tt->text);
2471 if (!size) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002472 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00002473 "Invalid size type for `%%arg' missing directive");
2474 free_tlist(tt);
2475 free_tlist(origline);
2476 return DIRECTIVE_FOUND;
2477 }
2478 free_tlist(tt);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002479
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002480 /* Round up to even stack slots */
2481 size = ALIGN(size, StackSize);
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002482
H. Peter Anvine2c80182005-01-15 22:15:51 +00002483 /* Now define the macro for the argument */
2484 snprintf(directive, sizeof(directive), "%%define %s (%s+%d)",
2485 arg, StackPointer, offset);
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07002486 do_directive(tokenize(directive), output);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002487 offset += size;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002488
H. Peter Anvine2c80182005-01-15 22:15:51 +00002489 /* Move to the next argument in the list */
2490 tline = tline->next;
2491 if (tline && tline->type == TOK_WHITESPACE)
2492 tline = tline->next;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002493 } while (tline && tline->type == TOK_OTHER && tline->text[0] == ',');
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002494 ArgOffset = offset;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002495 free_tlist(origline);
2496 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002497
H. Peter Anvine2c80182005-01-15 22:15:51 +00002498 case PP_LOCAL:
2499 /* TASM like LOCAL directive to define local variables for a
2500 * function, in the following form:
2501 *
2502 * LOCAL local1:WORD, local2:DWORD, local4:QWORD = LocalSize
2503 *
2504 * The '= LocalSize' at the end is ignored by NASM, but is
2505 * required by TASM to define the local parameter size (and used
2506 * by the TASM macro package).
2507 */
2508 offset = LocalOffset;
2509 do {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00002510 char *local, directive[256];
H. Peter Anvine2c80182005-01-15 22:15:51 +00002511 int size = StackSize;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002512
H. Peter Anvine2c80182005-01-15 22:15:51 +00002513 /* Find the argument name */
2514 tline = tline->next;
2515 if (tline && tline->type == TOK_WHITESPACE)
2516 tline = tline->next;
2517 if (!tline || tline->type != TOK_ID) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002518 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00002519 "`%%local' missing argument parameter");
2520 free_tlist(origline);
2521 return DIRECTIVE_FOUND;
2522 }
2523 local = tline->text;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002524
H. Peter Anvine2c80182005-01-15 22:15:51 +00002525 /* Find the argument size type */
2526 tline = tline->next;
2527 if (!tline || tline->type != TOK_OTHER
2528 || tline->text[0] != ':') {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002529 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00002530 "Syntax error processing `%%local' directive");
2531 free_tlist(origline);
2532 return DIRECTIVE_FOUND;
2533 }
2534 tline = tline->next;
2535 if (!tline || tline->type != TOK_ID) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002536 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00002537 "`%%local' missing size type parameter");
2538 free_tlist(origline);
2539 return DIRECTIVE_FOUND;
2540 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00002541
H. Peter Anvine2c80182005-01-15 22:15:51 +00002542 /* Allow macro expansion of type parameter */
Keith Kaniosb7a89542007-04-12 02:40:54 +00002543 tt = tokenize(tline->text);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002544 tt = expand_smacro(tt);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002545 size = parse_size(tt->text);
2546 if (!size) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002547 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00002548 "Invalid size type for `%%local' missing directive");
2549 free_tlist(tt);
2550 free_tlist(origline);
2551 return DIRECTIVE_FOUND;
2552 }
2553 free_tlist(tt);
H. Peter Anvin734b1882002-04-30 21:01:08 +00002554
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002555 /* Round up to even stack slots */
2556 size = ALIGN(size, StackSize);
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002557
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002558 offset += size; /* Negative offset, increment before */
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002559
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002560 /* Now define the macro for the argument */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002561 snprintf(directive, sizeof(directive), "%%define %s (%s-%d)",
2562 local, StackPointer, offset);
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07002563 do_directive(tokenize(directive), output);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002564
H. Peter Anvine2c80182005-01-15 22:15:51 +00002565 /* Now define the assign to setup the enter_c macro correctly */
2566 snprintf(directive, sizeof(directive),
2567 "%%assign %%$localsize %%$localsize+%d", size);
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07002568 do_directive(tokenize(directive), output);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002569
H. Peter Anvine2c80182005-01-15 22:15:51 +00002570 /* Move to the next argument in the list */
2571 tline = tline->next;
2572 if (tline && tline->type == TOK_WHITESPACE)
2573 tline = tline->next;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002574 } while (tline && tline->type == TOK_OTHER && tline->text[0] == ',');
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002575 LocalOffset = offset;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002576 free_tlist(origline);
2577 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002578
H. Peter Anvine2c80182005-01-15 22:15:51 +00002579 case PP_CLEAR:
2580 if (tline->next)
H. Peter Anvin130736c2016-02-17 20:27:41 -08002581 nasm_error(ERR_WARNING|ERR_PASS1,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002582 "trailing garbage after `%%clear' ignored");
2583 free_macros();
2584 init_macros();
H. Peter Anvine2c80182005-01-15 22:15:51 +00002585 free_tlist(origline);
2586 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002587
H. Peter Anvin418ca702008-05-30 10:42:30 -07002588 case PP_DEPEND:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002589 t = tline->next = expand_smacro(tline->next);
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002590 skip_white_(t);
2591 if (!t || (t->type != TOK_STRING &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002592 t->type != TOK_INTERNAL_STRING)) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002593 nasm_error(ERR_NONFATAL, "`%%depend' expects a file name");
H. Peter Anvin418ca702008-05-30 10:42:30 -07002594 free_tlist(origline);
2595 return DIRECTIVE_FOUND; /* but we did _something_ */
2596 }
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002597 if (t->next)
H. Peter Anvin130736c2016-02-17 20:27:41 -08002598 nasm_error(ERR_WARNING|ERR_PASS1,
H. Peter Anvin418ca702008-05-30 10:42:30 -07002599 "trailing garbage after `%%depend' ignored");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002600 p = t->text;
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002601 if (t->type != TOK_INTERNAL_STRING)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002602 nasm_unquote_cstr(p, i);
H. Peter Anvin436e3672016-10-04 01:12:28 -07002603 nasm_add_string_to_strlist(dephead, p);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002604 free_tlist(origline);
H. Peter Anvin418ca702008-05-30 10:42:30 -07002605 return DIRECTIVE_FOUND;
2606
2607 case PP_INCLUDE:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002608 t = tline->next = expand_smacro(tline->next);
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002609 skip_white_(t);
H. Peter Anvind2456592008-06-19 15:04:18 -07002610
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002611 if (!t || (t->type != TOK_STRING &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002612 t->type != TOK_INTERNAL_STRING)) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002613 nasm_error(ERR_NONFATAL, "`%%include' expects a file name");
H. Peter Anvine2c80182005-01-15 22:15:51 +00002614 free_tlist(origline);
2615 return DIRECTIVE_FOUND; /* but we did _something_ */
2616 }
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002617 if (t->next)
H. Peter Anvin130736c2016-02-17 20:27:41 -08002618 nasm_error(ERR_WARNING|ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +00002619 "trailing garbage after `%%include' ignored");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002620 p = t->text;
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002621 if (t->type != TOK_INTERNAL_STRING)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002622 nasm_unquote_cstr(p, i);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002623 inc = nasm_malloc(sizeof(Include));
H. Peter Anvine2c80182005-01-15 22:15:51 +00002624 inc->next = istk;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002625 inc->conds = NULL;
Jim Kukunas65a8afc2016-06-13 16:00:42 -04002626 found_path = NULL;
H. Peter Anvin9924d1e2016-10-04 00:59:39 -07002627 inc->fp = inc_fopen(p, dephead, &found_path,
H. Peter Anvind81a2352016-09-21 14:03:18 -07002628 pass == 0 ? INC_OPTIONAL : INC_NEEDED, NF_TEXT);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002629 if (!inc->fp) {
2630 /* -MG given but file not found */
2631 nasm_free(inc);
2632 } else {
Jim Kukunas65a8afc2016-06-13 16:00:42 -04002633 inc->fname = src_set_fname(found_path ? found_path : p);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002634 inc->lineno = src_set_linnum(0);
2635 inc->lineinc = 1;
2636 inc->expansion = NULL;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002637 inc->mstk = NULL;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002638 istk = inc;
H. Peter Anvin8ac25aa2016-02-18 01:16:18 -08002639 lfmt->uplevel(LIST_INCLUDE);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002640 }
2641 free_tlist(origline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002642 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002643
H. Peter Anvind2456592008-06-19 15:04:18 -07002644 case PP_USE:
H. Peter Anvinf4ae5ad2008-06-19 18:39:24 -07002645 {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002646 static macros_t *use_pkg;
2647 const char *pkg_macro = NULL;
H. Peter Anvinf4ae5ad2008-06-19 18:39:24 -07002648
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002649 tline = tline->next;
2650 skip_white_(tline);
2651 tline = expand_id(tline);
H. Peter Anvind2456592008-06-19 15:04:18 -07002652
H. Peter Anvin264b7b92008-10-24 16:38:17 -07002653 if (!tline || (tline->type != TOK_STRING &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002654 tline->type != TOK_INTERNAL_STRING &&
2655 tline->type != TOK_ID)) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002656 nasm_error(ERR_NONFATAL, "`%%use' expects a package name");
H. Peter Anvind2456592008-06-19 15:04:18 -07002657 free_tlist(origline);
2658 return DIRECTIVE_FOUND; /* but we did _something_ */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002659 }
H. Peter Anvin264b7b92008-10-24 16:38:17 -07002660 if (tline->next)
H. Peter Anvin130736c2016-02-17 20:27:41 -08002661 nasm_error(ERR_WARNING|ERR_PASS1,
H. Peter Anvind2456592008-06-19 15:04:18 -07002662 "trailing garbage after `%%use' ignored");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002663 if (tline->type == TOK_STRING)
2664 nasm_unquote_cstr(tline->text, i);
2665 use_pkg = nasm_stdmac_find_package(tline->text);
2666 if (!use_pkg)
H. Peter Anvin130736c2016-02-17 20:27:41 -08002667 nasm_error(ERR_NONFATAL, "unknown `%%use' package: %s", tline->text);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002668 else
2669 pkg_macro = (char *)use_pkg + 1; /* The first string will be <%define>__USE_*__ */
Victor van den Elzen35eb2ea2010-03-10 22:33:48 +01002670 if (use_pkg && ! smacro_defined(NULL, pkg_macro, 0, NULL, true)) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002671 /* Not already included, go ahead and include it */
2672 stdmacpos = use_pkg;
2673 }
2674 free_tlist(origline);
H. Peter Anvind2456592008-06-19 15:04:18 -07002675 return DIRECTIVE_FOUND;
H. Peter Anvinf4ae5ad2008-06-19 18:39:24 -07002676 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002677 case PP_PUSH:
H. Peter Anvine2c80182005-01-15 22:15:51 +00002678 case PP_REPL:
H. Peter Anvin42b56392008-10-24 16:24:21 -07002679 case PP_POP:
H. Peter Anvine2c80182005-01-15 22:15:51 +00002680 tline = tline->next;
2681 skip_white_(tline);
2682 tline = expand_id(tline);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002683 if (tline) {
2684 if (!tok_type_(tline, TOK_ID)) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002685 nasm_error(ERR_NONFATAL, "`%s' expects a context identifier",
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002686 pp_directives[i]);
2687 free_tlist(origline);
2688 return DIRECTIVE_FOUND; /* but we did _something_ */
2689 }
2690 if (tline->next)
H. Peter Anvin130736c2016-02-17 20:27:41 -08002691 nasm_error(ERR_WARNING|ERR_PASS1,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002692 "trailing garbage after `%s' ignored",
2693 pp_directives[i]);
2694 p = nasm_strdup(tline->text);
2695 } else {
2696 p = NULL; /* Anonymous */
2697 }
H. Peter Anvin42b56392008-10-24 16:24:21 -07002698
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002699 if (i == PP_PUSH) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002700 ctx = nasm_malloc(sizeof(Context));
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002701 ctx->next = cstk;
2702 hash_init(&ctx->localmac, HASH_SMALL);
2703 ctx->name = p;
2704 ctx->number = unique++;
2705 cstk = ctx;
2706 } else {
2707 /* %pop or %repl */
2708 if (!cstk) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002709 nasm_error(ERR_NONFATAL, "`%s': context stack is empty",
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002710 pp_directives[i]);
2711 } else if (i == PP_POP) {
2712 if (p && (!cstk->name || nasm_stricmp(p, cstk->name)))
H. Peter Anvin130736c2016-02-17 20:27:41 -08002713 nasm_error(ERR_NONFATAL, "`%%pop' in wrong context: %s, "
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002714 "expected %s",
2715 cstk->name ? cstk->name : "anonymous", p);
2716 else
2717 ctx_pop();
2718 } else {
2719 /* i == PP_REPL */
2720 nasm_free(cstk->name);
2721 cstk->name = p;
2722 p = NULL;
2723 }
2724 nasm_free(p);
2725 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002726 free_tlist(origline);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002727 return DIRECTIVE_FOUND;
H. Peter Anvin8e3f75e2008-09-24 00:21:58 -07002728 case PP_FATAL:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002729 severity = ERR_FATAL;
2730 goto issue_error;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002731 case PP_ERROR:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002732 severity = ERR_NONFATAL;
2733 goto issue_error;
H. Peter Anvin7df04172008-06-10 18:27:38 -07002734 case PP_WARNING:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002735 severity = ERR_WARNING|ERR_WARN_USER;
2736 goto issue_error;
H. Peter Anvin8e3f75e2008-09-24 00:21:58 -07002737
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002738issue_error:
H. Peter Anvin7df04172008-06-10 18:27:38 -07002739 {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002740 /* Only error out if this is the final pass */
2741 if (pass != 2 && i != PP_FATAL)
2742 return DIRECTIVE_FOUND;
2743
2744 tline->next = expand_smacro(tline->next);
2745 tline = tline->next;
2746 skip_white_(tline);
2747 t = tline ? tline->next : NULL;
2748 skip_white_(t);
2749 if (tok_type_(tline, TOK_STRING) && !t) {
2750 /* The line contains only a quoted string */
2751 p = tline->text;
2752 nasm_unquote(p, NULL); /* Ignore NUL character truncation */
H. Peter Anvin130736c2016-02-17 20:27:41 -08002753 nasm_error(severity, "%s", p);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002754 } else {
2755 /* Not a quoted string, or more than a quoted string */
2756 p = detoken(tline, false);
H. Peter Anvin130736c2016-02-17 20:27:41 -08002757 nasm_error(severity, "%s", p);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002758 nasm_free(p);
2759 }
2760 free_tlist(origline);
2761 return DIRECTIVE_FOUND;
H. Peter Anvin7df04172008-06-10 18:27:38 -07002762 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00002763
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002764 CASE_PP_IF:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002765 if (istk->conds && !emitting(istk->conds->state))
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002766 j = COND_NEVER;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002767 else {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002768 j = if_condition(tline->next, i);
2769 tline->next = NULL; /* it got freed */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002770 j = j < 0 ? COND_NEVER : j ? COND_IF_TRUE : COND_IF_FALSE;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002771 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002772 cond = nasm_malloc(sizeof(Cond));
2773 cond->next = istk->conds;
2774 cond->state = j;
2775 istk->conds = cond;
2776 if(istk->mstk)
2777 istk->mstk->condcnt ++;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002778 free_tlist(origline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002779 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002780
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002781 CASE_PP_ELIF:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002782 if (!istk->conds)
H. Peter Anvin130736c2016-02-17 20:27:41 -08002783 nasm_error(ERR_FATAL, "`%s': no matching `%%if'", pp_directives[i]);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002784 switch(istk->conds->state) {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002785 case COND_IF_TRUE:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002786 istk->conds->state = COND_DONE;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002787 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002788
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002789 case COND_DONE:
2790 case COND_NEVER:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002791 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002792
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002793 case COND_ELSE_TRUE:
2794 case COND_ELSE_FALSE:
H. Peter Anvin130736c2016-02-17 20:27:41 -08002795 nasm_error(ERR_WARNING|ERR_PASS1|ERR_PP_PRECOND,
2796 "`%%elif' after `%%else' ignored");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002797 istk->conds->state = COND_NEVER;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002798 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002799
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002800 case COND_IF_FALSE:
2801 /*
2802 * IMPORTANT: In the case of %if, we will already have
2803 * called expand_mmac_params(); however, if we're
2804 * processing an %elif we must have been in a
2805 * non-emitting mode, which would have inhibited
2806 * the normal invocation of expand_mmac_params().
2807 * Therefore, we have to do it explicitly here.
2808 */
2809 j = if_condition(expand_mmac_params(tline->next), i);
2810 tline->next = NULL; /* it got freed */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002811 istk->conds->state =
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002812 j < 0 ? COND_NEVER : j ? COND_IF_TRUE : COND_IF_FALSE;
2813 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002814 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002815 free_tlist(origline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002816 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002817
H. Peter Anvine2c80182005-01-15 22:15:51 +00002818 case PP_ELSE:
2819 if (tline->next)
H. Peter Anvin130736c2016-02-17 20:27:41 -08002820 nasm_error(ERR_WARNING|ERR_PASS1|ERR_PP_PRECOND,
2821 "trailing garbage after `%%else' ignored");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002822 if (!istk->conds)
H. Peter Anvin130736c2016-02-17 20:27:41 -08002823 nasm_fatal(0, "`%%else: no matching `%%if'");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002824 switch(istk->conds->state) {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002825 case COND_IF_TRUE:
2826 case COND_DONE:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002827 istk->conds->state = COND_ELSE_FALSE;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002828 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002829
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002830 case COND_NEVER:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002831 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002832
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002833 case COND_IF_FALSE:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002834 istk->conds->state = COND_ELSE_TRUE;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002835 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002836
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002837 case COND_ELSE_TRUE:
2838 case COND_ELSE_FALSE:
H. Peter Anvin130736c2016-02-17 20:27:41 -08002839 nasm_error(ERR_WARNING|ERR_PASS1|ERR_PP_PRECOND,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002840 "`%%else' after `%%else' ignored.");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002841 istk->conds->state = COND_NEVER;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002842 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002843 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002844 free_tlist(origline);
2845 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002846
H. Peter Anvine2c80182005-01-15 22:15:51 +00002847 case PP_ENDIF:
2848 if (tline->next)
H. Peter Anvin130736c2016-02-17 20:27:41 -08002849 nasm_error(ERR_WARNING|ERR_PASS1|ERR_PP_PRECOND,
2850 "trailing garbage after `%%endif' ignored");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002851 if (!istk->conds)
H. Peter Anvin130736c2016-02-17 20:27:41 -08002852 nasm_error(ERR_FATAL, "`%%endif': no matching `%%if'");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002853 cond = istk->conds;
2854 istk->conds = cond->next;
2855 nasm_free(cond);
2856 if(istk->mstk)
2857 istk->mstk->condcnt --;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002858 free_tlist(origline);
2859 return DIRECTIVE_FOUND;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002860
H. Peter Anvindb8f96e2009-07-15 09:07:29 -04002861 case PP_RMACRO:
2862 case PP_IRMACRO:
H. Peter Anvine2c80182005-01-15 22:15:51 +00002863 case PP_MACRO:
2864 case PP_IMACRO:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002865 if (defining) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002866 nasm_error(ERR_FATAL, "`%s': already defining a macro",
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002867 pp_directives[i]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002868 return DIRECTIVE_FOUND;
2869 }
H. Peter Anvin4def1a82016-05-09 13:59:44 -07002870 defining = nasm_zalloc(sizeof(MMacro));
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07002871 defining->max_depth = ((i == PP_RMACRO) || (i == PP_IRMACRO))
2872 ? nasm_limit[LIMIT_MACROS] : 0;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002873 defining->casesense = (i == PP_MACRO) || (i == PP_RMACRO);
2874 if (!parse_mmacro_spec(tline, defining, pp_directives[i])) {
2875 nasm_free(defining);
2876 defining = NULL;
2877 return DIRECTIVE_FOUND;
2878 }
H. Peter Anvina26433d2008-07-16 14:40:01 -07002879
H. Peter Anvin4def1a82016-05-09 13:59:44 -07002880 src_get(&defining->xline, &defining->fname);
2881
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002882 mmac = (MMacro *) hash_findix(&mmacros, defining->name);
2883 while (mmac) {
2884 if (!strcmp(mmac->name, defining->name) &&
2885 (mmac->nparam_min <= defining->nparam_max
2886 || defining->plus)
2887 && (defining->nparam_min <= mmac->nparam_max
2888 || mmac->plus)) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002889 nasm_error(ERR_WARNING|ERR_PASS1,
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002890 "redefining multi-line macro `%s'", defining->name);
2891 return DIRECTIVE_FOUND;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002892 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002893 mmac = mmac->next;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002894 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002895 free_tlist(origline);
2896 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002897
H. Peter Anvine2c80182005-01-15 22:15:51 +00002898 case PP_ENDM:
2899 case PP_ENDMACRO:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002900 if (! (defining && defining->name)) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002901 nasm_error(ERR_NONFATAL, "`%s': not defining a macro", tline->text);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002902 return DIRECTIVE_FOUND;
2903 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002904 mmhead = (MMacro **) hash_findi_add(&mmacros, defining->name);
2905 defining->next = *mmhead;
2906 *mmhead = defining;
2907 defining = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002908 free_tlist(origline);
2909 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002910
H. Peter Anvin89cee572009-07-15 09:16:54 -04002911 case PP_EXITMACRO:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002912 /*
2913 * We must search along istk->expansion until we hit a
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002914 * macro-end marker for a macro with a name. Then we
2915 * bypass all lines between exitmacro and endmacro.
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002916 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002917 list_for_each(l, istk->expansion)
2918 if (l->finishes && l->finishes->name)
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002919 break;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002920
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002921 if (l) {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002922 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002923 * Remove all conditional entries relative to this
2924 * macro invocation. (safe to do in this context)
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002925 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002926 for ( ; l->finishes->condcnt > 0; l->finishes->condcnt --) {
2927 cond = istk->conds;
2928 istk->conds = cond->next;
2929 nasm_free(cond);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002930 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002931 istk->expansion = l;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002932 } else {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002933 nasm_error(ERR_NONFATAL, "`%%exitmacro' not within `%%macro' block");
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002934 }
Keith Kanios852f1ee2009-07-12 00:19:55 -05002935 free_tlist(origline);
2936 return DIRECTIVE_FOUND;
2937
H. Peter Anvina26433d2008-07-16 14:40:01 -07002938 case PP_UNMACRO:
2939 case PP_UNIMACRO:
2940 {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002941 MMacro **mmac_p;
2942 MMacro spec;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002943
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002944 spec.casesense = (i == PP_UNMACRO);
2945 if (!parse_mmacro_spec(tline, &spec, pp_directives[i])) {
2946 return DIRECTIVE_FOUND;
2947 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002948 mmac_p = (MMacro **) hash_findi(&mmacros, spec.name, NULL);
2949 while (mmac_p && *mmac_p) {
2950 mmac = *mmac_p;
2951 if (mmac->casesense == spec.casesense &&
2952 !mstrcmp(mmac->name, spec.name, spec.casesense) &&
2953 mmac->nparam_min == spec.nparam_min &&
2954 mmac->nparam_max == spec.nparam_max &&
2955 mmac->plus == spec.plus) {
2956 *mmac_p = mmac->next;
2957 free_mmacro(mmac);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002958 } else {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002959 mmac_p = &mmac->next;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002960 }
2961 }
2962 free_tlist(origline);
2963 free_tlist(spec.dlist);
2964 return DIRECTIVE_FOUND;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002965 }
2966
H. Peter Anvine2c80182005-01-15 22:15:51 +00002967 case PP_ROTATE:
2968 if (tline->next && tline->next->type == TOK_WHITESPACE)
2969 tline = tline->next;
H. Peter Anvin89cee572009-07-15 09:16:54 -04002970 if (!tline->next) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002971 free_tlist(origline);
H. Peter Anvin130736c2016-02-17 20:27:41 -08002972 nasm_error(ERR_NONFATAL, "`%%rotate' missing rotate count");
H. Peter Anvine2c80182005-01-15 22:15:51 +00002973 return DIRECTIVE_FOUND;
2974 }
2975 t = expand_smacro(tline->next);
2976 tline->next = NULL;
2977 free_tlist(origline);
2978 tline = t;
2979 tptr = &t;
2980 tokval.t_type = TOKEN_INVALID;
2981 evalresult =
H. Peter Anvin130736c2016-02-17 20:27:41 -08002982 evaluate(ppscan, tptr, &tokval, NULL, pass, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002983 free_tlist(tline);
2984 if (!evalresult)
2985 return DIRECTIVE_FOUND;
2986 if (tokval.t_type)
H. Peter Anvin130736c2016-02-17 20:27:41 -08002987 nasm_error(ERR_WARNING|ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +00002988 "trailing garbage after expression ignored");
2989 if (!is_simple(evalresult)) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002990 nasm_error(ERR_NONFATAL, "non-constant value given to `%%rotate'");
H. Peter Anvine2c80182005-01-15 22:15:51 +00002991 return DIRECTIVE_FOUND;
2992 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002993 mmac = istk->mstk;
2994 while (mmac && !mmac->name) /* avoid mistaking %reps for macros */
2995 mmac = mmac->next_active;
2996 if (!mmac) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002997 nasm_error(ERR_NONFATAL, "`%%rotate' invoked outside a macro call");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002998 } else if (mmac->nparam == 0) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002999 nasm_error(ERR_NONFATAL,
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003000 "`%%rotate' invoked within macro without parameters");
3001 } else {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003002 int rotate = mmac->rotate + reloc_value(evalresult);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003003
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003004 rotate %= (int)mmac->nparam;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003005 if (rotate < 0)
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003006 rotate += mmac->nparam;
3007
3008 mmac->rotate = rotate;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003009 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00003010 return DIRECTIVE_FOUND;
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00003011
H. Peter Anvine2c80182005-01-15 22:15:51 +00003012 case PP_REP:
H. Peter Anvin6867acc2007-10-10 14:58:45 -07003013 nolist = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003014 do {
3015 tline = tline->next;
3016 } while (tok_type_(tline, TOK_WHITESPACE));
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00003017
H. Peter Anvine2c80182005-01-15 22:15:51 +00003018 if (tok_type_(tline, TOK_ID) &&
3019 nasm_stricmp(tline->text, ".nolist") == 0) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07003020 nolist = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003021 do {
3022 tline = tline->next;
3023 } while (tok_type_(tline, TOK_WHITESPACE));
3024 }
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00003025
H. Peter Anvine2c80182005-01-15 22:15:51 +00003026 if (tline) {
3027 t = expand_smacro(tline);
3028 tptr = &t;
3029 tokval.t_type = TOKEN_INVALID;
3030 evalresult =
H. Peter Anvin130736c2016-02-17 20:27:41 -08003031 evaluate(ppscan, tptr, &tokval, NULL, pass, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003032 if (!evalresult) {
3033 free_tlist(origline);
3034 return DIRECTIVE_FOUND;
3035 }
3036 if (tokval.t_type)
H. Peter Anvin130736c2016-02-17 20:27:41 -08003037 nasm_error(ERR_WARNING|ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +00003038 "trailing garbage after expression ignored");
3039 if (!is_simple(evalresult)) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08003040 nasm_error(ERR_NONFATAL, "non-constant value given to `%%rep'");
H. Peter Anvine2c80182005-01-15 22:15:51 +00003041 return DIRECTIVE_FOUND;
3042 }
Cyrill Gorcunove091d6e2010-08-09 13:58:22 +04003043 count = reloc_value(evalresult);
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07003044 if (count > nasm_limit[LIMIT_REP]) {
3045 nasm_error(ERR_NONFATAL,
H. Peter Anvin79561022018-06-15 17:51:39 -07003046 "`%%rep' count %"PRId64" exceeds limit (currently %"PRId64")",
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07003047 count, nasm_limit[LIMIT_REP]);
Cyrill Gorcunove091d6e2010-08-09 13:58:22 +04003048 count = 0;
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07003049 } else if (count < 0) {
3050 nasm_error(ERR_WARNING|ERR_PASS2|ERR_WARN_NEG_REP,
3051 "negative `%%rep' count: %"PRId64, count);
3052 count = 0;
3053 } else {
Cyrill Gorcunove091d6e2010-08-09 13:58:22 +04003054 count++;
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07003055 }
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 Gorcunov67f2ca22018-10-13 19:41:01 +03003950 if (!tm)
3951 goto err;
Cyrill Gorcunove75331c2013-11-09 12:02:15 +04003952 head = new_Token(NULL, tm->type, tm->text, 0);
3953 tt = &head->next, tm = tm->next;
3954 while (tok_isnt_(tm, ",")) {
3955 t = new_Token(NULL, tm->type, tm->text, 0);
3956 *tt = t, tt = &t->next, tm = tm->next;
3957 }
3958
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003959 if (fst < lst) {
3960 for (i = fst + 1; i <= lst; i++) {
3961 t = new_Token(NULL, TOK_OTHER, ",", 0);
3962 *tt = t, tt = &t->next;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003963 j = (i + mac->rotate) % mac->nparam;
3964 tm = mac->params[j];
Cyrill Gorcunove75331c2013-11-09 12:02:15 +04003965 while (tok_isnt_(tm, ",")) {
3966 t = new_Token(NULL, tm->type, tm->text, 0);
3967 *tt = t, tt = &t->next, tm = tm->next;
3968 }
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003969 }
3970 } else {
3971 for (i = fst - 1; i >= lst; i--) {
3972 t = new_Token(NULL, TOK_OTHER, ",", 0);
3973 *tt = t, tt = &t->next;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003974 j = (i + mac->rotate) % mac->nparam;
3975 tm = mac->params[j];
Cyrill Gorcunove75331c2013-11-09 12:02:15 +04003976 while (tok_isnt_(tm, ",")) {
3977 t = new_Token(NULL, tm->type, tm->text, 0);
3978 *tt = t, tt = &t->next, tm = tm->next;
3979 }
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003980 }
3981 }
3982
3983 *last = tt;
3984 return head;
3985
3986err:
H. Peter Anvin130736c2016-02-17 20:27:41 -08003987 nasm_error(ERR_NONFATAL, "`%%{%s}': macro parameters out of range",
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003988 &tline->text[1]);
3989 return tline;
3990}
3991
H. Peter Anvin76690a12002-04-30 20:52:49 +00003992/*
3993 * Expand MMacro-local things: parameter references (%0, %n, %+n,
H. Peter Anvin67c63722008-10-26 23:49:00 -07003994 * %-n) and MMacro-local identifiers (%%foo) as well as
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003995 * macro indirection (%[...]) and range (%{..:..}).
H. Peter Anvin76690a12002-04-30 20:52:49 +00003996 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003997static Token *expand_mmac_params(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00003998{
H. Peter Anvin734b1882002-04-30 21:01:08 +00003999 Token *t, *tt, **tail, *thead;
H. Peter Anvin6125b622009-04-08 14:02:25 -07004000 bool changed = false;
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004001 char *pos;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004002
4003 tail = &thead;
4004 thead = NULL;
4005
H. Peter Anvine2c80182005-01-15 22:15:51 +00004006 while (tline) {
4007 if (tline->type == TOK_PREPROC_ID &&
Cyrill Gorcunovca611192010-06-04 09:22:12 +04004008 (((tline->text[1] == '+' || tline->text[1] == '-') && tline->text[2]) ||
4009 (tline->text[1] >= '0' && tline->text[1] <= '9') ||
4010 tline->text[1] == '%')) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004011 char *text = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004012 int type = 0, cc; /* type = 0 to placate optimisers */
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004013 char tmpbuf[30];
H. Peter Anvin25a99342007-09-22 17:45:45 -07004014 unsigned int n;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004015 int i;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004016 MMacro *mac;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004017
H. Peter Anvine2c80182005-01-15 22:15:51 +00004018 t = tline;
4019 tline = tline->next;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004020
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004021 mac = istk->mstk;
4022 while (mac && !mac->name) /* avoid mistaking %reps for macros */
4023 mac = mac->next_active;
4024 if (!mac) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08004025 nasm_error(ERR_NONFATAL, "`%s': not in a macro call", t->text);
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004026 } else {
4027 pos = strchr(t->text, ':');
4028 if (!pos) {
4029 switch (t->text[1]) {
4030 /*
4031 * We have to make a substitution of one of the
4032 * forms %1, %-1, %+1, %%foo, %0.
4033 */
4034 case '0':
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004035 type = TOK_NUMBER;
4036 snprintf(tmpbuf, sizeof(tmpbuf), "%d", mac->nparam);
4037 text = nasm_strdup(tmpbuf);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004038 break;
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004039 case '%':
H. Peter Anvine2c80182005-01-15 22:15:51 +00004040 type = TOK_ID;
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004041 snprintf(tmpbuf, sizeof(tmpbuf), "..@%"PRIu64".",
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004042 mac->unique);
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004043 text = nasm_strcat(tmpbuf, t->text + 2);
4044 break;
4045 case '-':
4046 n = atoi(t->text + 2) - 1;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004047 if (n >= mac->nparam)
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004048 tt = NULL;
4049 else {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004050 if (mac->nparam > 1)
4051 n = (n + mac->rotate) % mac->nparam;
4052 tt = mac->params[n];
H. Peter Anvine2c80182005-01-15 22:15:51 +00004053 }
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004054 cc = find_cc(tt);
4055 if (cc == -1) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08004056 nasm_error(ERR_NONFATAL,
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004057 "macro parameter %d is not a condition code",
4058 n + 1);
4059 text = NULL;
4060 } else {
4061 type = TOK_ID;
4062 if (inverse_ccs[cc] == -1) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08004063 nasm_error(ERR_NONFATAL,
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004064 "condition code `%s' is not invertible",
4065 conditions[cc]);
4066 text = NULL;
4067 } else
4068 text = nasm_strdup(conditions[inverse_ccs[cc]]);
4069 }
4070 break;
4071 case '+':
4072 n = atoi(t->text + 2) - 1;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004073 if (n >= mac->nparam)
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004074 tt = NULL;
4075 else {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004076 if (mac->nparam > 1)
4077 n = (n + mac->rotate) % mac->nparam;
4078 tt = mac->params[n];
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004079 }
4080 cc = find_cc(tt);
4081 if (cc == -1) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08004082 nasm_error(ERR_NONFATAL,
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004083 "macro parameter %d is not a condition code",
4084 n + 1);
4085 text = NULL;
4086 } else {
4087 type = TOK_ID;
4088 text = nasm_strdup(conditions[cc]);
4089 }
4090 break;
4091 default:
4092 n = atoi(t->text + 1) - 1;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004093 if (n >= mac->nparam)
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004094 tt = NULL;
4095 else {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004096 if (mac->nparam > 1)
4097 n = (n + mac->rotate) % mac->nparam;
4098 tt = mac->params[n];
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004099 }
4100 if (tt) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004101 for (i = 0; i < mac->paramlen[n]; i++) {
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004102 *tail = new_Token(NULL, tt->type, tt->text, 0);
4103 tail = &(*tail)->next;
4104 tt = tt->next;
4105 }
4106 }
4107 text = NULL; /* we've done it here */
4108 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004109 }
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004110 } else {
4111 /*
4112 * seems we have a parameters range here
4113 */
4114 Token *head, **last;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004115 head = expand_mmac_params_range(mac, t, &last);
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004116 if (head != t) {
4117 *tail = head;
4118 *last = tline;
4119 tline = head;
4120 text = NULL;
4121 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00004122 }
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004123 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00004124 if (!text) {
4125 delete_Token(t);
4126 } else {
4127 *tail = t;
4128 tail = &t->next;
4129 t->type = type;
4130 nasm_free(t->text);
4131 t->text = text;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004132 t->a.mac = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004133 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004134 changed = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004135 continue;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004136 } else if (tline->type == TOK_INDIRECT) {
4137 t = tline;
4138 tline = tline->next;
4139 tt = tokenize(t->text);
4140 tt = expand_mmac_params(tt);
4141 tt = expand_smacro(tt);
4142 *tail = tt;
4143 while (tt) {
4144 tt->a.mac = NULL; /* Necessary? */
4145 tail = &tt->next;
4146 tt = tt->next;
4147 }
4148 delete_Token(t);
4149 changed = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004150 } else {
4151 t = *tail = tline;
4152 tline = tline->next;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004153 t->a.mac = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004154 tail = &t->next;
4155 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00004156 }
H. Peter Anvineba20a72002-04-30 20:53:55 +00004157 *tail = NULL;
H. Peter Anvin67c63722008-10-26 23:49:00 -07004158
Cyrill Gorcunovc6a742c2011-06-27 01:23:09 +04004159 if (changed) {
4160 const struct tokseq_match t[] = {
4161 {
4162 PP_CONCAT_MASK(TOK_ID) |
4163 PP_CONCAT_MASK(TOK_FLOAT), /* head */
4164 PP_CONCAT_MASK(TOK_ID) |
4165 PP_CONCAT_MASK(TOK_NUMBER) |
4166 PP_CONCAT_MASK(TOK_FLOAT) |
4167 PP_CONCAT_MASK(TOK_OTHER) /* tail */
4168 },
4169 {
4170 PP_CONCAT_MASK(TOK_NUMBER), /* head */
4171 PP_CONCAT_MASK(TOK_NUMBER) /* tail */
4172 }
4173 };
4174 paste_tokens(&thead, t, ARRAY_SIZE(t), false);
4175 }
H. Peter Anvin6125b622009-04-08 14:02:25 -07004176
H. Peter Anvin76690a12002-04-30 20:52:49 +00004177 return thead;
4178}
4179
4180/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004181 * Expand all single-line macro calls made in the given line.
4182 * Return the expanded version of the line. The original is deemed
4183 * to be destroyed in the process. (In reality we'll just move
4184 * Tokens from input to output a lot of the time, rather than
4185 * actually bothering to destroy and replicate.)
4186 */
H. Peter Anvincb1cf592007-11-19 12:26:50 -08004187
H. Peter Anvine2c80182005-01-15 22:15:51 +00004188static Token *expand_smacro(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004189{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004190 Token *t, *tt, *mstart, **tail, *thead;
H. Peter Anvineba20a72002-04-30 20:53:55 +00004191 SMacro *head = NULL, *m;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004192 Token **params;
4193 int *paramsize;
H. Peter Anvin25a99342007-09-22 17:45:45 -07004194 unsigned int nparam, sparam;
H. Peter Anvind784a082009-04-20 14:01:18 -07004195 int brackets;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004196 Token *org_tline = tline;
4197 Context *ctx;
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08004198 const char *mname;
H. Peter Anvin79561022018-06-15 17:51:39 -07004199 int64_t deadman = nasm_limit[LIMIT_MACROS];
H. Peter Anvin8287daf2009-07-07 16:00:58 -07004200 bool expanded;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004201
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004202 /*
4203 * Trick: we should avoid changing the start token pointer since it can
4204 * be contained in "next" field of other token. Because of this
4205 * we allocate a copy of first token and work with it; at the end of
4206 * routine we copy it back
4207 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004208 if (org_tline) {
Cyrill Gorcunoved4a8052010-04-09 15:40:35 +04004209 tline = new_Token(org_tline->next, org_tline->type,
4210 org_tline->text, 0);
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004211 tline->a.mac = org_tline->a.mac;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004212 nasm_free(org_tline->text);
4213 org_tline->text = NULL;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004214 }
4215
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004216 expanded = true; /* Always expand %+ at least once */
H. Peter Anvin8287daf2009-07-07 16:00:58 -07004217
H. Peter Anvincb1cf592007-11-19 12:26:50 -08004218again:
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004219 thead = NULL;
Cyrill Gorcunoved4a8052010-04-09 15:40:35 +04004220 tail = &thead;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004221
H. Peter Anvine2c80182005-01-15 22:15:51 +00004222 while (tline) { /* main token loop */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004223 if (!--deadman) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08004224 nasm_error(ERR_NONFATAL, "interminable macro recursion");
Cyrill Gorcunovbd38c8f2009-11-21 11:11:23 +03004225 goto err;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004226 }
H. Peter Anvincb1cf592007-11-19 12:26:50 -08004227
H. Peter Anvine2c80182005-01-15 22:15:51 +00004228 if ((mname = tline->text)) {
4229 /* if this token is a local macro, look in local context */
Cyrill Gorcunovc56d9ad2010-02-11 15:12:19 +03004230 if (tline->type == TOK_ID) {
4231 head = (SMacro *)hash_findix(&smacros, mname);
4232 } else if (tline->type == TOK_PREPROC_ID) {
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04004233 ctx = get_ctx(mname, &mname);
Cyrill Gorcunovc56d9ad2010-02-11 15:12:19 +03004234 head = ctx ? (SMacro *)hash_findix(&ctx->localmac, mname) : NULL;
4235 } else
4236 head = NULL;
H. Peter Anvin072771e2008-05-22 13:17:51 -07004237
H. Peter Anvine2c80182005-01-15 22:15:51 +00004238 /*
4239 * We've hit an identifier. As in is_mmacro below, we first
4240 * check whether the identifier is a single-line macro at
4241 * all, then think about checking for parameters if
4242 * necessary.
4243 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004244 list_for_each(m, head)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004245 if (!mstrcmp(m->name, mname, m->casesense))
4246 break;
4247 if (m) {
4248 mstart = tline;
4249 params = NULL;
4250 paramsize = NULL;
4251 if (m->nparam == 0) {
4252 /*
4253 * Simple case: the macro is parameterless. Discard the
4254 * one token that the macro call took, and push the
4255 * expansion back on the to-do stack.
4256 */
4257 if (!m->expansion) {
4258 if (!strcmp("__FILE__", m->name)) {
H. Peter Anvin274cda82016-05-10 02:56:29 -07004259 const char *file = src_get_fname();
4260 /* nasm_free(tline->text); here? */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004261 tline->text = nasm_quote(file, strlen(file));
4262 tline->type = TOK_STRING;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004263 continue;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004264 }
4265 if (!strcmp("__LINE__", m->name)) {
4266 nasm_free(tline->text);
4267 make_tok_num(tline, src_get_linnum());
4268 continue;
4269 }
4270 if (!strcmp("__BITS__", m->name)) {
4271 nasm_free(tline->text);
4272 make_tok_num(tline, globalbits);
4273 continue;
4274 }
4275 tline = delete_Token(tline);
4276 continue;
4277 }
4278 } else {
4279 /*
4280 * Complicated case: at least one macro with this name
H. Peter Anvine2c80182005-01-15 22:15:51 +00004281 * exists and takes parameters. We must find the
4282 * parameters in the call, count them, find the SMacro
4283 * that corresponds to that form of the macro call, and
4284 * substitute for the parameters when we expand. What a
4285 * pain.
4286 */
4287 /*tline = tline->next;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004288 skip_white_(tline); */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004289 do {
4290 t = tline->next;
4291 while (tok_type_(t, TOK_SMAC_END)) {
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004292 t->a.mac->in_progress = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004293 t->text = NULL;
4294 t = tline->next = delete_Token(t);
4295 }
4296 tline = t;
4297 } while (tok_type_(tline, TOK_WHITESPACE));
4298 if (!tok_is_(tline, "(")) {
4299 /*
4300 * This macro wasn't called with parameters: ignore
4301 * the call. (Behaviour borrowed from gnu cpp.)
4302 */
4303 tline = mstart;
4304 m = NULL;
4305 } else {
4306 int paren = 0;
4307 int white = 0;
4308 brackets = 0;
4309 nparam = 0;
4310 sparam = PARAM_DELTA;
4311 params = nasm_malloc(sparam * sizeof(Token *));
4312 params[0] = tline->next;
4313 paramsize = nasm_malloc(sparam * sizeof(int));
4314 paramsize[0] = 0;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07004315 while (true) { /* parameter loop */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004316 /*
4317 * For some unusual expansions
4318 * which concatenates function call
4319 */
4320 t = tline->next;
4321 while (tok_type_(t, TOK_SMAC_END)) {
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004322 t->a.mac->in_progress = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004323 t->text = NULL;
4324 t = tline->next = delete_Token(t);
4325 }
4326 tline = t;
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00004327
H. Peter Anvine2c80182005-01-15 22:15:51 +00004328 if (!tline) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08004329 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00004330 "macro call expects terminating `)'");
4331 break;
4332 }
4333 if (tline->type == TOK_WHITESPACE
4334 && brackets <= 0) {
4335 if (paramsize[nparam])
4336 white++;
4337 else
4338 params[nparam] = tline->next;
4339 continue; /* parameter loop */
4340 }
4341 if (tline->type == TOK_OTHER
4342 && tline->text[1] == 0) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004343 char ch = tline->text[0];
H. Peter Anvine2c80182005-01-15 22:15:51 +00004344 if (ch == ',' && !paren && brackets <= 0) {
4345 if (++nparam >= sparam) {
4346 sparam += PARAM_DELTA;
4347 params = nasm_realloc(params,
Cyrill Gorcunoved4a8052010-04-09 15:40:35 +04004348 sparam * sizeof(Token *));
4349 paramsize = nasm_realloc(paramsize,
4350 sparam * sizeof(int));
H. Peter Anvine2c80182005-01-15 22:15:51 +00004351 }
4352 params[nparam] = tline->next;
4353 paramsize[nparam] = 0;
4354 white = 0;
4355 continue; /* parameter loop */
4356 }
4357 if (ch == '{' &&
4358 (brackets > 0 || (brackets == 0 &&
4359 !paramsize[nparam])))
4360 {
4361 if (!(brackets++)) {
4362 params[nparam] = tline->next;
4363 continue; /* parameter loop */
4364 }
4365 }
4366 if (ch == '}' && brackets > 0)
4367 if (--brackets == 0) {
4368 brackets = -1;
4369 continue; /* parameter loop */
4370 }
4371 if (ch == '(' && !brackets)
4372 paren++;
4373 if (ch == ')' && brackets <= 0)
4374 if (--paren < 0)
4375 break;
4376 }
4377 if (brackets < 0) {
4378 brackets = 0;
H. Peter Anvin130736c2016-02-17 20:27:41 -08004379 nasm_error(ERR_NONFATAL, "braces do not "
H. Peter Anvine2c80182005-01-15 22:15:51 +00004380 "enclose all of macro parameter");
4381 }
4382 paramsize[nparam] += white + 1;
4383 white = 0;
4384 } /* parameter loop */
4385 nparam++;
4386 while (m && (m->nparam != nparam ||
4387 mstrcmp(m->name, mname,
4388 m->casesense)))
4389 m = m->next;
4390 if (!m)
H. Peter Anvin130736c2016-02-17 20:27:41 -08004391 nasm_error(ERR_WARNING|ERR_PASS1|ERR_WARN_MNP,
H. Peter Anvine2c80182005-01-15 22:15:51 +00004392 "macro `%s' exists, "
4393 "but not taking %d parameters",
4394 mstart->text, nparam);
4395 }
4396 }
4397 if (m && m->in_progress)
4398 m = NULL;
4399 if (!m) { /* in progess or didn't find '(' or wrong nparam */
H. Peter Anvin70653092007-10-19 14:42:29 -07004400 /*
H. Peter Anvine2c80182005-01-15 22:15:51 +00004401 * Design question: should we handle !tline, which
4402 * indicates missing ')' here, or expand those
4403 * macros anyway, which requires the (t) test a few
H. Peter Anvin70653092007-10-19 14:42:29 -07004404 * lines down?
H. Peter Anvine2c80182005-01-15 22:15:51 +00004405 */
4406 nasm_free(params);
4407 nasm_free(paramsize);
4408 tline = mstart;
4409 } else {
4410 /*
4411 * Expand the macro: we are placed on the last token of the
4412 * call, so that we can easily split the call from the
4413 * following tokens. We also start by pushing an SMAC_END
4414 * token for the cycle removal.
4415 */
4416 t = tline;
4417 if (t) {
4418 tline = t->next;
4419 t->next = NULL;
4420 }
4421 tt = new_Token(tline, TOK_SMAC_END, NULL, 0);
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004422 tt->a.mac = m;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07004423 m->in_progress = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004424 tline = tt;
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04004425 list_for_each(t, m->expansion) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004426 if (t->type >= TOK_SMAC_PARAM) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00004427 Token *pcopy = tline, **ptail = &pcopy;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004428 Token *ttt, *pt;
4429 int i;
H. Peter Anvin734b1882002-04-30 21:01:08 +00004430
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004431 ttt = params[t->type - TOK_SMAC_PARAM];
4432 i = paramsize[t->type - TOK_SMAC_PARAM];
4433 while (--i >= 0) {
4434 pt = *ptail = new_Token(tline, ttt->type,
4435 ttt->text, 0);
4436 ptail = &pt->next;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004437 ttt = ttt->next;
Cyrill Gorcunov59ce1c62017-10-22 18:42:07 +03004438 if (!ttt && i > 0) {
4439 /*
4440 * FIXME: Need to handle more gracefully,
4441 * exiting early on agruments analysis.
4442 */
4443 nasm_error(ERR_FATAL,
4444 "macro `%s' expects %d args",
4445 mstart->text,
4446 (int)paramsize[t->type - TOK_SMAC_PARAM]);
4447 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00004448 }
4449 tline = pcopy;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004450 } else if (t->type == TOK_PREPROC_Q) {
4451 tt = new_Token(tline, TOK_ID, mname, 0);
4452 tline = tt;
4453 } else if (t->type == TOK_PREPROC_QQ) {
4454 tt = new_Token(tline, TOK_ID, m->name, 0);
4455 tline = tt;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004456 } else {
4457 tt = new_Token(tline, t->type, t->text, 0);
4458 tline = tt;
4459 }
4460 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00004461
H. Peter Anvine2c80182005-01-15 22:15:51 +00004462 /*
4463 * Having done that, get rid of the macro call, and clean
4464 * up the parameters.
4465 */
4466 nasm_free(params);
4467 nasm_free(paramsize);
4468 free_tlist(mstart);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004469 expanded = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004470 continue; /* main token loop */
4471 }
4472 }
4473 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004474
H. Peter Anvine2c80182005-01-15 22:15:51 +00004475 if (tline->type == TOK_SMAC_END) {
Cyrill Gorcunov980dd652018-10-14 19:25:32 +03004476 /* On error path it might already be dropped */
4477 if (tline->a.mac)
4478 tline->a.mac->in_progress = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004479 tline = delete_Token(tline);
4480 } else {
4481 t = *tail = tline;
4482 tline = tline->next;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004483 t->a.mac = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004484 t->next = NULL;
4485 tail = &t->next;
4486 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004487 }
4488
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004489 /*
4490 * Now scan the entire line and look for successive TOK_IDs that resulted
Keith Kaniosb7a89542007-04-12 02:40:54 +00004491 * after expansion (they can't be produced by tokenize()). The successive
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004492 * TOK_IDs should be concatenated.
4493 * Also we look for %+ tokens and concatenate the tokens before and after
4494 * them (without white spaces in between).
4495 */
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +04004496 if (expanded) {
Cyrill Gorcunovc6a742c2011-06-27 01:23:09 +04004497 const struct tokseq_match t[] = {
4498 {
4499 PP_CONCAT_MASK(TOK_ID) |
4500 PP_CONCAT_MASK(TOK_PREPROC_ID), /* head */
4501 PP_CONCAT_MASK(TOK_ID) |
4502 PP_CONCAT_MASK(TOK_PREPROC_ID) |
4503 PP_CONCAT_MASK(TOK_NUMBER) /* tail */
4504 }
4505 };
4506 if (paste_tokens(&thead, t, ARRAY_SIZE(t), true)) {
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +04004507 /*
4508 * If we concatenated something, *and* we had previously expanded
4509 * an actual macro, scan the lines again for macros...
4510 */
4511 tline = thead;
4512 expanded = false;
4513 goto again;
4514 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00004515 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004516
Cyrill Gorcunovbd38c8f2009-11-21 11:11:23 +03004517err:
H. Peter Anvine2c80182005-01-15 22:15:51 +00004518 if (org_tline) {
4519 if (thead) {
4520 *org_tline = *thead;
4521 /* since we just gave text to org_line, don't free it */
4522 thead->text = NULL;
4523 delete_Token(thead);
4524 } else {
4525 /* the expression expanded to empty line;
4526 we can't return NULL for some reasons
4527 we just set the line to a single WHITESPACE token. */
4528 memset(org_tline, 0, sizeof(*org_tline));
4529 org_tline->text = NULL;
4530 org_tline->type = TOK_WHITESPACE;
4531 }
4532 thead = org_tline;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004533 }
4534
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004535 return thead;
4536}
4537
4538/*
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004539 * Similar to expand_smacro but used exclusively with macro identifiers
4540 * right before they are fetched in. The reason is that there can be
4541 * identifiers consisting of several subparts. We consider that if there
4542 * are more than one element forming the name, user wants a expansion,
4543 * otherwise it will be left as-is. Example:
4544 *
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004545 * %define %$abc cde
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004546 *
4547 * the identifier %$abc will be left as-is so that the handler for %define
4548 * will suck it and define the corresponding value. Other case:
4549 *
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004550 * %define _%$abc cde
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004551 *
4552 * In this case user wants name to be expanded *before* %define starts
4553 * working, so we'll expand %$abc into something (if it has a value;
4554 * otherwise it will be left as-is) then concatenate all successive
4555 * PP_IDs into one.
4556 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004557static Token *expand_id(Token * tline)
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004558{
4559 Token *cur, *oldnext = NULL;
4560
H. Peter Anvin734b1882002-04-30 21:01:08 +00004561 if (!tline || !tline->next)
H. Peter Anvine2c80182005-01-15 22:15:51 +00004562 return tline;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004563
4564 cur = tline;
4565 while (cur->next &&
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004566 (cur->next->type == TOK_ID ||
4567 cur->next->type == TOK_PREPROC_ID
4568 || cur->next->type == TOK_NUMBER))
H. Peter Anvine2c80182005-01-15 22:15:51 +00004569 cur = cur->next;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004570
4571 /* If identifier consists of just one token, don't expand */
4572 if (cur == tline)
H. Peter Anvine2c80182005-01-15 22:15:51 +00004573 return tline;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004574
H. Peter Anvine2c80182005-01-15 22:15:51 +00004575 if (cur) {
4576 oldnext = cur->next; /* Detach the tail past identifier */
4577 cur->next = NULL; /* so that expand_smacro stops here */
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004578 }
4579
H. Peter Anvin734b1882002-04-30 21:01:08 +00004580 tline = expand_smacro(tline);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004581
H. Peter Anvine2c80182005-01-15 22:15:51 +00004582 if (cur) {
4583 /* expand_smacro possibly changhed tline; re-scan for EOL */
4584 cur = tline;
4585 while (cur && cur->next)
4586 cur = cur->next;
4587 if (cur)
4588 cur->next = oldnext;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004589 }
4590
4591 return tline;
4592}
4593
4594/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004595 * Determine whether the given line constitutes a multi-line macro
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004596 * call, and return the MMacro structure called if so. Doesn't have
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004597 * to check for an initial label - that's taken care of in
4598 * expand_mmacro - but must check numbers of parameters. Guaranteed
4599 * to be called with tline->type == TOK_ID, so the putative macro
4600 * name is easy to find.
4601 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004602static MMacro *is_mmacro(Token * tline, Token *** params_array)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004603{
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004604 MMacro *head, *m;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004605 Token **params;
4606 int nparam;
4607
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004608 head = (MMacro *) hash_findix(&mmacros, tline->text);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004609
4610 /*
4611 * Efficiency: first we see if any macro exists with the given
4612 * name. If not, we can return NULL immediately. _Then_ we
4613 * count the parameters, and then we look further along the
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004614 * list if necessary to find the proper MMacro.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004615 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004616 list_for_each(m, head)
4617 if (!mstrcmp(m->name, tline->text, m->casesense))
H. Peter Anvine2c80182005-01-15 22:15:51 +00004618 break;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004619 if (!m)
H. Peter Anvine2c80182005-01-15 22:15:51 +00004620 return NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004621
4622 /*
4623 * OK, we have a potential macro. Count and demarcate the
4624 * parameters.
4625 */
H. Peter Anvin734b1882002-04-30 21:01:08 +00004626 count_mmac_params(tline->next, &nparam, &params);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004627
4628 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004629 * So we know how many parameters we've got. Find the MMacro
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004630 * structure that handles this number.
4631 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004632 while (m) {
4633 if (m->nparam_min <= nparam
4634 && (m->plus || nparam <= m->nparam_max)) {
4635 /*
4636 * This one is right. Just check if cycle removal
4637 * prohibits us using it before we actually celebrate...
4638 */
4639 if (m->in_progress > m->max_depth) {
4640 if (m->max_depth > 0) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08004641 nasm_error(ERR_WARNING,
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004642 "reached maximum recursion depth of %i",
4643 m->max_depth);
4644 }
4645 nasm_free(params);
4646 return NULL;
4647 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00004648 /*
4649 * It's right, and we can use it. Add its default
4650 * parameters to the end of our list if necessary.
4651 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004652 if (m->defaults && nparam < m->nparam_min + m->ndefs) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00004653 params =
4654 nasm_realloc(params,
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004655 ((m->nparam_min + m->ndefs +
H. Peter Anvine2c80182005-01-15 22:15:51 +00004656 1) * sizeof(*params)));
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004657 while (nparam < m->nparam_min + m->ndefs) {
4658 params[nparam] = m->defaults[nparam - m->nparam_min];
H. Peter Anvine2c80182005-01-15 22:15:51 +00004659 nparam++;
4660 }
4661 }
4662 /*
4663 * If we've gone over the maximum parameter count (and
4664 * we're in Plus mode), ignore parameters beyond
4665 * nparam_max.
4666 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004667 if (m->plus && nparam > m->nparam_max)
4668 nparam = m->nparam_max;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004669 /*
4670 * Then terminate the parameter list, and leave.
4671 */
4672 if (!params) { /* need this special case */
4673 params = nasm_malloc(sizeof(*params));
4674 nparam = 0;
4675 }
4676 params[nparam] = NULL;
4677 *params_array = params;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004678 return m;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004679 }
4680 /*
4681 * This one wasn't right: look for the next one with the
4682 * same name.
4683 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004684 list_for_each(m, m->next)
4685 if (!mstrcmp(m->name, tline->text, m->casesense))
H. Peter Anvine2c80182005-01-15 22:15:51 +00004686 break;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004687 }
4688
4689 /*
4690 * After all that, we didn't find one with the right number of
4691 * parameters. Issue a warning, and fail to expand the macro.
4692 */
H. Peter Anvin130736c2016-02-17 20:27:41 -08004693 nasm_error(ERR_WARNING|ERR_PASS1|ERR_WARN_MNP,
H. Peter Anvine2c80182005-01-15 22:15:51 +00004694 "macro `%s' exists, but not taking %d parameters",
4695 tline->text, nparam);
H. Peter Anvin734b1882002-04-30 21:01:08 +00004696 nasm_free(params);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004697 return NULL;
4698}
4699
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004700
4701/*
4702 * Save MMacro invocation specific fields in
4703 * preparation for a recursive macro expansion
4704 */
4705static void push_mmacro(MMacro *m)
4706{
4707 MMacroInvocation *i;
4708
4709 i = nasm_malloc(sizeof(MMacroInvocation));
4710 i->prev = m->prev;
4711 i->params = m->params;
4712 i->iline = m->iline;
4713 i->nparam = m->nparam;
4714 i->rotate = m->rotate;
4715 i->paramlen = m->paramlen;
4716 i->unique = m->unique;
4717 i->condcnt = m->condcnt;
4718 m->prev = i;
4719}
4720
4721
4722/*
4723 * Restore MMacro invocation specific fields that were
4724 * saved during a previous recursive macro expansion
4725 */
4726static void pop_mmacro(MMacro *m)
4727{
4728 MMacroInvocation *i;
4729
4730 if (m->prev) {
4731 i = m->prev;
4732 m->prev = i->prev;
4733 m->params = i->params;
4734 m->iline = i->iline;
4735 m->nparam = i->nparam;
4736 m->rotate = i->rotate;
4737 m->paramlen = i->paramlen;
4738 m->unique = i->unique;
4739 m->condcnt = i->condcnt;
4740 nasm_free(i);
4741 }
4742}
4743
4744
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004745/*
4746 * Expand the multi-line macro call made by the given line, if
4747 * there is one to be expanded. If there is, push the expansion on
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004748 * istk->expansion and return 1. Otherwise return 0.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004749 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004750static int expand_mmacro(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004751{
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004752 Token *startline = tline;
H. Peter Anvineba20a72002-04-30 20:53:55 +00004753 Token *label = NULL;
4754 int dont_prepend = 0;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004755 Token **params, *t, *tt;
4756 MMacro *m;
4757 Line *l, *ll;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004758 int i, nparam, *paramlen;
H. Peter Anvinc751e862008-06-09 10:18:45 -07004759 const char *mname;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004760
4761 t = tline;
H. Peter Anvineba20a72002-04-30 20:53:55 +00004762 skip_white_(t);
H. Peter Anvince2233b2008-05-25 21:57:00 -07004763 /* if (!tok_type_(t, TOK_ID)) Lino 02/25/02 */
H. Peter Anvindce1e2f2002-04-30 21:06:37 +00004764 if (!tok_type_(t, TOK_ID) && !tok_type_(t, TOK_PREPROC_ID))
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004765 return 0;
4766 m = is_mmacro(t, &params);
4767 if (m) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004768 mname = t->text;
H. Peter Anvinc751e862008-06-09 10:18:45 -07004769 } else {
H. Peter Anvine2c80182005-01-15 22:15:51 +00004770 Token *last;
4771 /*
4772 * We have an id which isn't a macro call. We'll assume
4773 * it might be a label; we'll also check to see if a
4774 * colon follows it. Then, if there's another id after
4775 * that lot, we'll check it again for macro-hood.
4776 */
4777 label = last = t;
4778 t = t->next;
4779 if (tok_type_(t, TOK_WHITESPACE))
4780 last = t, t = t->next;
4781 if (tok_is_(t, ":")) {
4782 dont_prepend = 1;
4783 last = t, t = t->next;
4784 if (tok_type_(t, TOK_WHITESPACE))
4785 last = t, t = t->next;
4786 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004787 if (!tok_type_(t, TOK_ID) || !(m = is_mmacro(t, &params)))
4788 return 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004789 last->next = NULL;
Keith Kanios891775e2009-07-11 06:08:54 -05004790 mname = t->text;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004791 tline = t;
H. Peter Anvineba20a72002-04-30 20:53:55 +00004792 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004793
4794 /*
4795 * Fix up the parameters: this involves stripping leading and
4796 * trailing whitespace, then stripping braces if they are
4797 * present.
4798 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004799 for (nparam = 0; params[nparam]; nparam++) ;
H. Peter Anvin734b1882002-04-30 21:01:08 +00004800 paramlen = nparam ? nasm_malloc(nparam * sizeof(*paramlen)) : NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004801
H. Peter Anvine2c80182005-01-15 22:15:51 +00004802 for (i = 0; params[i]; i++) {
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08004803 int brace = 0;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004804 int comma = (!m->plus || i < nparam - 1);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004805
H. Peter Anvine2c80182005-01-15 22:15:51 +00004806 t = params[i];
4807 skip_white_(t);
4808 if (tok_is_(t, "{"))
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08004809 t = t->next, brace++, comma = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004810 params[i] = t;
4811 paramlen[i] = 0;
4812 while (t) {
4813 if (comma && t->type == TOK_OTHER && !strcmp(t->text, ","))
4814 break; /* ... because we have hit a comma */
4815 if (comma && t->type == TOK_WHITESPACE
4816 && tok_is_(t->next, ","))
4817 break; /* ... or a space then a comma */
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08004818 if (brace && t->type == TOK_OTHER) {
4819 if (t->text[0] == '{')
4820 brace++; /* ... or a nested opening brace */
4821 else if (t->text[0] == '}')
4822 if (!--brace)
4823 break; /* ... or a brace */
4824 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00004825 t = t->next;
4826 paramlen[i]++;
4827 }
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08004828 if (brace)
H. Peter Anvin130736c2016-02-17 20:27:41 -08004829 nasm_error(ERR_NONFATAL, "macro params should be enclosed in braces");
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004830 }
4831
4832 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004833 * OK, we have a MMacro structure together with a set of
4834 * parameters. We must now go through the expansion and push
4835 * copies of each Line on to istk->expansion. Substitution of
H. Peter Anvin76690a12002-04-30 20:52:49 +00004836 * parameter tokens and macro-local tokens doesn't get done
4837 * until the single-line macro substitution process; this is
4838 * because delaying them allows us to change the semantics
4839 * later through %rotate.
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004840 *
4841 * First, push an end marker on to istk->expansion, mark this
4842 * macro as in progress, and set up its invocation-specific
4843 * variables.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004844 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004845 ll = nasm_malloc(sizeof(Line));
4846 ll->next = istk->expansion;
4847 ll->finishes = m;
4848 ll->first = NULL;
4849 istk->expansion = ll;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004850
4851 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004852 * Save the previous MMacro expansion in the case of
4853 * macro recursion
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004854 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004855 if (m->max_depth && m->in_progress)
4856 push_mmacro(m);
4857
4858 m->in_progress ++;
4859 m->params = params;
4860 m->iline = tline;
4861 m->nparam = nparam;
4862 m->rotate = 0;
4863 m->paramlen = paramlen;
4864 m->unique = unique++;
4865 m->lineno = 0;
4866 m->condcnt = 0;
4867
4868 m->next_active = istk->mstk;
4869 istk->mstk = m;
4870
4871 list_for_each(l, m->expansion) {
4872 Token **tail;
4873
4874 ll = nasm_malloc(sizeof(Line));
4875 ll->finishes = NULL;
4876 ll->next = istk->expansion;
4877 istk->expansion = ll;
4878 tail = &ll->first;
4879
4880 list_for_each(t, l->first) {
4881 Token *x = t;
4882 switch (t->type) {
4883 case TOK_PREPROC_Q:
4884 tt = *tail = new_Token(NULL, TOK_ID, mname, 0);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004885 break;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004886 case TOK_PREPROC_QQ:
4887 tt = *tail = new_Token(NULL, TOK_ID, m->name, 0);
4888 break;
4889 case TOK_PREPROC_ID:
4890 if (t->text[1] == '0' && t->text[2] == '0') {
4891 dont_prepend = -1;
4892 x = label;
4893 if (!x)
4894 continue;
4895 }
4896 /* fall through */
4897 default:
4898 tt = *tail = new_Token(NULL, x->type, x->text, 0);
4899 break;
4900 }
4901 tail = &tt->next;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004902 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004903 *tail = NULL;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004904 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004905
4906 /*
H. Peter Anvineba20a72002-04-30 20:53:55 +00004907 * If we had a label, push it on as the first line of
4908 * the macro expansion.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004909 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004910 if (label) {
4911 if (dont_prepend < 0)
4912 free_tlist(startline);
4913 else {
4914 ll = nasm_malloc(sizeof(Line));
4915 ll->finishes = NULL;
4916 ll->next = istk->expansion;
4917 istk->expansion = ll;
4918 ll->first = startline;
4919 if (!dont_prepend) {
4920 while (label->next)
4921 label = label->next;
4922 label->next = tt = new_Token(NULL, TOK_OTHER, ":", 0);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004923 }
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004924 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004925 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004926
H. Peter Anvin8ac25aa2016-02-18 01:16:18 -08004927 lfmt->uplevel(m->nolist ? LIST_MACRO_NOLIST : LIST_MACRO);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004928
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004929 return 1;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004930}
4931
H. Peter Anvin130736c2016-02-17 20:27:41 -08004932/*
4933 * This function adds macro names to error messages, and suppresses
4934 * them if necessary.
4935 */
4936static void pp_verror(int severity, const char *fmt, va_list arg)
Victor van den Elzen3b404c02008-09-18 13:51:36 +02004937{
H. Peter Anvin130736c2016-02-17 20:27:41 -08004938 char buff[BUFSIZ];
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004939 MMacro *mmac = NULL;
4940 int delta = 0;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02004941
H. Peter Anvin130736c2016-02-17 20:27:41 -08004942 /*
4943 * If we're in a dead branch of IF or something like it, ignore the error.
4944 * However, because %else etc are evaluated in the state context
4945 * of the previous branch, errors might get lost:
4946 * %if 0 ... %else trailing garbage ... %endif
4947 * So %else etc should set the ERR_PP_PRECOND flag.
4948 */
4949 if ((severity & ERR_MASK) < ERR_FATAL &&
4950 istk && istk->conds &&
4951 ((severity & ERR_PP_PRECOND) ?
4952 istk->conds->state == COND_NEVER :
H. Peter Anvineb6653f2016-04-05 13:03:10 -07004953 !emitting(istk->conds->state)))
H. Peter Anvin130736c2016-02-17 20:27:41 -08004954 return;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02004955
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004956 /* get %macro name */
H. Peter Anvin130736c2016-02-17 20:27:41 -08004957 if (!(severity & ERR_NOFILE) && istk && istk->mstk) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004958 mmac = istk->mstk;
4959 /* but %rep blocks should be skipped */
4960 while (mmac && !mmac->name)
4961 mmac = mmac->next_active, delta++;
Cyrill Gorcunov9900c6b2011-10-09 18:58:46 +04004962 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004963
H. Peter Anvin130736c2016-02-17 20:27:41 -08004964 if (mmac) {
4965 vsnprintf(buff, sizeof(buff), fmt, arg);
Victor van den Elzen3b404c02008-09-18 13:51:36 +02004966
H. Peter Anvin130736c2016-02-17 20:27:41 -08004967 nasm_set_verror(real_verror);
4968 nasm_error(severity, "(%s:%d) %s",
4969 mmac->name, mmac->lineno - delta, buff);
4970 nasm_set_verror(pp_verror);
4971 } else {
4972 real_verror(severity, fmt, arg);
4973 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004974}
4975
H. Peter Anvin734b1882002-04-30 21:01:08 +00004976static void
H. Peter Anvin81b62b92017-12-20 13:33:49 -08004977pp_reset(const char *file, int apass, StrList **deplist)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004978{
H. Peter Anvin7383b402008-09-24 10:20:40 -07004979 Token *t;
4980
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004981 cstk = NULL;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004982 istk = nasm_malloc(sizeof(Include));
4983 istk->next = NULL;
4984 istk->conds = NULL;
4985 istk->expansion = NULL;
4986 istk->mstk = NULL;
H. Peter Anvin3e83cec2016-05-25 04:28:46 -07004987 istk->fp = nasm_open_read(file, NF_TEXT);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004988 istk->fname = NULL;
H. Peter Anvin274cda82016-05-10 02:56:29 -07004989 src_set(0, file);
H. Peter Anvineba20a72002-04-30 20:53:55 +00004990 istk->lineinc = 1;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004991 if (!istk->fp)
H. Peter Anvin130736c2016-02-17 20:27:41 -08004992 nasm_fatal(ERR_NOFILE, "unable to open input file `%s'", file);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004993 defining = NULL;
Charles Crayned4200be2008-07-12 16:42:33 -07004994 nested_mac_count = 0;
4995 nested_rep_count = 0;
H. Peter Anvin97a23472007-09-16 17:57:25 -07004996 init_macros();
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004997 unique = 0;
H. Peter Anvinf7606612016-07-13 14:23:48 -07004998
4999 if (tasm_compatible_mode)
5000 pp_add_stdmac(nasm_stdmac_tasm);
5001
5002 pp_add_stdmac(nasm_stdmac_nasm);
5003 pp_add_stdmac(nasm_stdmac_version);
5004
Cyrill Gorcunov15ce78f2017-01-06 20:21:28 +03005005 if (extrastdmac)
5006 pp_add_stdmac(extrastdmac);
5007
H. Peter Anvinf7606612016-07-13 14:23:48 -07005008 stdmacpos = stdmacros[0];
5009 stdmacnext = &stdmacros[1];
5010
H. Peter Anvind2456592008-06-19 15:04:18 -07005011 do_predef = true;
H. Peter Anvin61f130f2008-09-25 15:45:06 -07005012
5013 /*
5014 * 0 for dependencies, 1 for preparatory passes, 2 for final pass.
5015 * The caller, however, will also pass in 3 for preprocess-only so
5016 * we can set __PASS__ accordingly.
5017 */
5018 pass = apass > 2 ? 2 : apass;
5019
H. Peter Anvin9924d1e2016-10-04 00:59:39 -07005020 dephead = deplist;
H. Peter Anvin436e3672016-10-04 01:12:28 -07005021 nasm_add_string_to_strlist(dephead, file);
H. Peter Anvin9924d1e2016-10-04 00:59:39 -07005022
H. Peter Anvin61f130f2008-09-25 15:45:06 -07005023 /*
5024 * Define the __PASS__ macro. This is defined here unlike
5025 * all the other builtins, because it is special -- it varies between
5026 * passes.
5027 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005028 t = nasm_malloc(sizeof(*t));
5029 t->next = NULL;
H. Peter Anvin61f130f2008-09-25 15:45:06 -07005030 make_tok_num(t, apass);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005031 t->a.mac = NULL;
H. Peter Anvin7383b402008-09-24 10:20:40 -07005032 define_smacro(NULL, "__PASS__", true, 0, t);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005033}
5034
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07005035static void pp_init(void)
5036{
5037 hash_init(&FileHash, HASH_MEDIUM);
5038}
5039
Keith Kaniosa6dfa782007-04-13 16:47:53 +00005040static char *pp_getline(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005041{
Keith Kaniosa6dfa782007-04-13 16:47:53 +00005042 char *line;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005043 Token *tline;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005044
H. Peter Anvin130736c2016-02-17 20:27:41 -08005045 real_verror = nasm_set_verror(pp_verror);
H. Peter Anvin215186f2016-02-17 20:27:41 -08005046
H. Peter Anvine2c80182005-01-15 22:15:51 +00005047 while (1) {
5048 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005049 * Fetch a tokenized line, either from the macro-expansion
H. Peter Anvine2c80182005-01-15 22:15:51 +00005050 * buffer or from the input file.
5051 */
5052 tline = NULL;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005053 while (istk->expansion && istk->expansion->finishes) {
5054 Line *l = istk->expansion;
5055 if (!l->finishes->name && l->finishes->in_progress > 1) {
5056 Line *ll;
H. Peter Anvineba20a72002-04-30 20:53:55 +00005057
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005058 /*
5059 * This is a macro-end marker for a macro with no
5060 * name, which means it's not really a macro at all
5061 * but a %rep block, and the `in_progress' field is
5062 * more than 1, meaning that we still need to
5063 * repeat. (1 means the natural last repetition; 0
5064 * means termination by %exitrep.) We have
5065 * therefore expanded up to the %endrep, and must
5066 * push the whole block on to the expansion buffer
5067 * again. We don't bother to remove the macro-end
5068 * marker: we'd only have to generate another one
5069 * if we did.
5070 */
5071 l->finishes->in_progress--;
5072 list_for_each(l, l->finishes->expansion) {
5073 Token *t, *tt, **tail;
5074
5075 ll = nasm_malloc(sizeof(Line));
5076 ll->next = istk->expansion;
5077 ll->finishes = NULL;
5078 ll->first = NULL;
5079 tail = &ll->first;
5080
5081 list_for_each(t, l->first) {
5082 if (t->text || t->type == TOK_WHITESPACE) {
5083 tt = *tail = new_Token(NULL, t->type, t->text, 0);
5084 tail = &tt->next;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005085 }
5086 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005087
5088 istk->expansion = ll;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005089 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005090 } else {
5091 /*
5092 * Check whether a `%rep' was started and not ended
5093 * within this macro expansion. This can happen and
5094 * should be detected. It's a fatal error because
5095 * I'm too confused to work out how to recover
5096 * sensibly from it.
5097 */
5098 if (defining) {
5099 if (defining->name)
H. Peter Anvin130736c2016-02-17 20:27:41 -08005100 nasm_panic(0, "defining with name in expansion");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005101 else if (istk->mstk->name)
H. Peter Anvin130736c2016-02-17 20:27:41 -08005102 nasm_fatal(0, "`%%rep' without `%%endrep' within"
5103 " expansion of macro `%s'",
5104 istk->mstk->name);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005105 }
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005106
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005107 /*
5108 * FIXME: investigate the relationship at this point between
5109 * istk->mstk and l->finishes
5110 */
5111 {
5112 MMacro *m = istk->mstk;
5113 istk->mstk = m->next_active;
5114 if (m->name) {
5115 /*
5116 * This was a real macro call, not a %rep, and
5117 * therefore the parameter information needs to
5118 * be freed.
5119 */
5120 if (m->prev) {
5121 pop_mmacro(m);
5122 l->finishes->in_progress --;
5123 } else {
5124 nasm_free(m->params);
5125 free_tlist(m->iline);
5126 nasm_free(m->paramlen);
5127 l->finishes->in_progress = 0;
5128 }
Adam Majer91e72402017-07-25 10:42:01 +02005129 }
5130
5131 /*
5132 * FIXME It is incorrect to always free_mmacro here.
5133 * It leads to usage-after-free.
5134 *
5135 * https://bugzilla.nasm.us/show_bug.cgi?id=3392414
5136 */
5137#if 0
5138 else
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005139 free_mmacro(m);
Adam Majer91e72402017-07-25 10:42:01 +02005140#endif
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005141 }
5142 istk->expansion = l->next;
5143 nasm_free(l);
H. Peter Anvin8ac25aa2016-02-18 01:16:18 -08005144 lfmt->downlevel(LIST_MACRO);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005145 }
5146 }
5147 while (1) { /* until we get a line we can use */
5148
5149 if (istk->expansion) { /* from a macro expansion */
5150 char *p;
5151 Line *l = istk->expansion;
5152 if (istk->mstk)
5153 istk->mstk->lineno++;
5154 tline = l->first;
5155 istk->expansion = l->next;
5156 nasm_free(l);
5157 p = detoken(tline, false);
H. Peter Anvin8ac25aa2016-02-18 01:16:18 -08005158 lfmt->line(LIST_MACRO, p);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005159 nasm_free(p);
5160 break;
5161 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00005162 line = read_line();
5163 if (line) { /* from the current input file */
5164 line = prepreproc(line);
Keith Kaniosb7a89542007-04-12 02:40:54 +00005165 tline = tokenize(line);
H. Peter Anvine2c80182005-01-15 22:15:51 +00005166 nasm_free(line);
5167 break;
5168 }
5169 /*
5170 * The current file has ended; work down the istk
5171 */
5172 {
5173 Include *i = istk;
5174 fclose(i->fp);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005175 if (i->conds) {
5176 /* nasm_error can't be conditionally suppressed */
H. Peter Anvin41087062016-03-03 14:27:34 -08005177 nasm_fatal(0,
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005178 "expected `%%endif' before end of file");
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005179 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00005180 /* only set line and file name if there's a next node */
H. Peter Anvin274cda82016-05-10 02:56:29 -07005181 if (i->next)
5182 src_set(i->lineno, i->fname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00005183 istk = i->next;
H. Peter Anvin8ac25aa2016-02-18 01:16:18 -08005184 lfmt->downlevel(LIST_INCLUDE);
H. Peter Anvine2c80182005-01-15 22:15:51 +00005185 nasm_free(i);
H. Peter Anvin130736c2016-02-17 20:27:41 -08005186 if (!istk) {
5187 line = NULL;
5188 goto done;
5189 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005190 if (istk->expansion && istk->expansion->finishes)
5191 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005192 }
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005193 }
5194
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005195 /*
5196 * We must expand MMacro parameters and MMacro-local labels
5197 * _before_ we plunge into directive processing, to cope
5198 * with things like `%define something %1' such as STRUC
5199 * uses. Unless we're _defining_ a MMacro, in which case
5200 * those tokens should be left alone to go into the
5201 * definition; and unless we're in a non-emitting
5202 * condition, in which case we don't want to meddle with
5203 * anything.
5204 */
5205 if (!defining && !(istk->conds && !emitting(istk->conds->state))
5206 && !(istk->mstk && !istk->mstk->in_progress)) {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005207 tline = expand_mmac_params(tline);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005208 }
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005209
H. Peter Anvine2c80182005-01-15 22:15:51 +00005210 /*
5211 * Check the line to see if it's a preprocessor directive.
5212 */
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07005213 if (do_directive(tline, &line) == DIRECTIVE_FOUND) {
5214 if (line)
5215 break; /* Directive generated output */
5216 else
5217 continue;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005218 } else if (defining) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00005219 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005220 * We're defining a multi-line macro. We emit nothing
5221 * at all, and just
5222 * shove the tokenized line on to the macro definition.
H. Peter Anvine2c80182005-01-15 22:15:51 +00005223 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005224 Line *l = nasm_malloc(sizeof(Line));
5225 l->next = defining->expansion;
5226 l->first = tline;
5227 l->finishes = NULL;
5228 defining->expansion = l;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005229 continue;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005230 } else if (istk->conds && !emitting(istk->conds->state)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00005231 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005232 * We're in a non-emitting branch of a condition block.
H. Peter Anvine2c80182005-01-15 22:15:51 +00005233 * Emit nothing at all, not even a blank line: when we
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005234 * emerge from the condition we'll give a line-number
H. Peter Anvine2c80182005-01-15 22:15:51 +00005235 * directive so we keep our place correctly.
5236 */
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005237 free_tlist(tline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00005238 continue;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005239 } else if (istk->mstk && !istk->mstk->in_progress) {
5240 /*
5241 * We're in a %rep block which has been terminated, so
5242 * we're walking through to the %endrep without
5243 * emitting anything. Emit nothing at all, not even a
5244 * blank line: when we emerge from the %rep block we'll
5245 * give a line-number directive so we keep our place
5246 * correctly.
5247 */
5248 free_tlist(tline);
5249 continue;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005250 } else {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005251 tline = expand_smacro(tline);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005252 if (!expand_mmacro(tline)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00005253 /*
Keith Kaniosb7a89542007-04-12 02:40:54 +00005254 * De-tokenize the line again, and emit it.
H. Peter Anvine2c80182005-01-15 22:15:51 +00005255 */
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005256 line = detoken(tline, true);
5257 free_tlist(tline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00005258 break;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005259 } else {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005260 continue; /* expand_mmacro calls free_tlist */
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005261 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00005262 }
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005263 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005264
H. Peter Anvin130736c2016-02-17 20:27:41 -08005265done:
5266 nasm_set_verror(real_verror);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005267 return line;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005268}
5269
H. Peter Anvine2c80182005-01-15 22:15:51 +00005270static void pp_cleanup(int pass)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005271{
H. Peter Anvin130736c2016-02-17 20:27:41 -08005272 real_verror = nasm_set_verror(pp_verror);
H. Peter Anvin215186f2016-02-17 20:27:41 -08005273
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005274 if (defining) {
5275 if (defining->name) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08005276 nasm_error(ERR_NONFATAL,
5277 "end of file while still defining macro `%s'",
5278 defining->name);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005279 } else {
H. Peter Anvin130736c2016-02-17 20:27:41 -08005280 nasm_error(ERR_NONFATAL, "end of file while still in %%rep");
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005281 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005282
5283 free_mmacro(defining);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005284 defining = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005285 }
H. Peter Anvin130736c2016-02-17 20:27:41 -08005286
5287 nasm_set_verror(real_verror);
H. Peter Anvin215186f2016-02-17 20:27:41 -08005288
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005289 while (cstk)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005290 ctx_pop();
H. Peter Anvin97a23472007-09-16 17:57:25 -07005291 free_macros();
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005292 while (istk) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00005293 Include *i = istk;
5294 istk = istk->next;
5295 fclose(i->fp);
Cyrill Gorcunov8dcfd882011-03-03 09:18:56 +03005296 nasm_free(i);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005297 }
5298 while (cstk)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005299 ctx_pop();
H. Peter Anvin274cda82016-05-10 02:56:29 -07005300 src_set_fname(NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00005301 if (pass == 0) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03005302 IncPath *i;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005303 free_llist(predef);
Cyrill Gorcunovdae24d72014-06-28 10:17:39 +04005304 predef = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005305 delete_Blocks();
Cyrill Gorcunovdae24d72014-06-28 10:17:39 +04005306 freeTokens = NULL;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03005307 while ((i = ipath)) {
5308 ipath = i->next;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005309 if (i->path)
5310 nasm_free(i->path);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03005311 nasm_free(i);
5312 }
H. Peter Anvin11dfa1a2008-07-02 18:11:04 -07005313 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005314}
5315
Cyrill Gorcunov0b78bff2012-05-07 01:57:55 +04005316static void pp_include_path(char *path)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005317{
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005318 IncPath *i;
H. Peter Anvin37a321f2007-09-24 13:41:58 -07005319
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005320 i = nasm_malloc(sizeof(IncPath));
5321 i->path = path ? nasm_strdup(path) : NULL;
5322 i->next = NULL;
Frank Kotlerd0ed6fd2003-08-27 11:33:56 +00005323
H. Peter Anvin89cee572009-07-15 09:16:54 -04005324 if (ipath) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00005325 IncPath *j = ipath;
H. Peter Anvin89cee572009-07-15 09:16:54 -04005326 while (j->next)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005327 j = j->next;
5328 j->next = i;
5329 } else {
5330 ipath = i;
Frank Kotlerd0ed6fd2003-08-27 11:33:56 +00005331 }
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005332}
Frank Kotlerd0ed6fd2003-08-27 11:33:56 +00005333
Cyrill Gorcunov0b78bff2012-05-07 01:57:55 +04005334static void pp_pre_include(char *fname)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005335{
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005336 Token *inc, *space, *name;
5337 Line *l;
5338
H. Peter Anvin734b1882002-04-30 21:01:08 +00005339 name = new_Token(NULL, TOK_INTERNAL_STRING, fname, 0);
5340 space = new_Token(name, TOK_WHITESPACE, NULL, 0);
5341 inc = new_Token(space, TOK_PREPROC_ID, "%include", 0);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005342
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005343 l = nasm_malloc(sizeof(Line));
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005344 l->next = predef;
5345 l->first = inc;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005346 l->finishes = NULL;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005347 predef = l;
5348}
5349
Cyrill Gorcunov0b78bff2012-05-07 01:57:55 +04005350static void pp_pre_define(char *definition)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005351{
5352 Token *def, *space;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005353 Line *l;
Keith Kaniosa6dfa782007-04-13 16:47:53 +00005354 char *equals;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005355
H. Peter Anvin130736c2016-02-17 20:27:41 -08005356 real_verror = nasm_set_verror(pp_verror);
H. Peter Anvin215186f2016-02-17 20:27:41 -08005357
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005358 equals = strchr(definition, '=');
H. Peter Anvin734b1882002-04-30 21:01:08 +00005359 space = new_Token(NULL, TOK_WHITESPACE, NULL, 0);
5360 def = new_Token(space, TOK_PREPROC_ID, "%define", 0);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005361 if (equals)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005362 *equals = ' ';
Keith Kaniosb7a89542007-04-12 02:40:54 +00005363 space->next = tokenize(definition);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005364 if (equals)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005365 *equals = '=';
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005366
Cyrill Gorcunov6d42e9b2015-02-08 11:07:17 +03005367 if (space->next->type != TOK_PREPROC_ID &&
5368 space->next->type != TOK_ID)
H. Peter Anvin130736c2016-02-17 20:27:41 -08005369 nasm_error(ERR_WARNING, "pre-defining non ID `%s\'\n", definition);
Cyrill Gorcunov6d42e9b2015-02-08 11:07:17 +03005370
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005371 l = nasm_malloc(sizeof(Line));
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005372 l->next = predef;
5373 l->first = def;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005374 l->finishes = NULL;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005375 predef = l;
H. Peter Anvin130736c2016-02-17 20:27:41 -08005376
5377 nasm_set_verror(real_verror);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005378}
5379
Cyrill Gorcunov0b78bff2012-05-07 01:57:55 +04005380static void pp_pre_undefine(char *definition)
H. Peter Anvin620515a2002-04-30 20:57:38 +00005381{
5382 Token *def, *space;
5383 Line *l;
5384
H. Peter Anvin734b1882002-04-30 21:01:08 +00005385 space = new_Token(NULL, TOK_WHITESPACE, NULL, 0);
5386 def = new_Token(space, TOK_PREPROC_ID, "%undef", 0);
Keith Kaniosb7a89542007-04-12 02:40:54 +00005387 space->next = tokenize(definition);
H. Peter Anvin620515a2002-04-30 20:57:38 +00005388
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005389 l = nasm_malloc(sizeof(Line));
H. Peter Anvin620515a2002-04-30 20:57:38 +00005390 l->next = predef;
5391 l->first = def;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005392 l->finishes = NULL;
H. Peter Anvin620515a2002-04-30 20:57:38 +00005393 predef = l;
5394}
5395
H. Peter Anvin05990342018-06-11 13:32:42 -07005396/* Insert an early preprocessor command that doesn't need special handling */
5397static void pp_pre_command(const char *what, char *string)
5398{
5399 char *cmd;
5400 Token *def, *space;
5401 Line *l;
5402
5403 def = tokenize(string);
5404 if (what) {
5405 cmd = nasm_strcat(what[0] == '%' ? "" : "%", what);
5406 space = new_Token(def, TOK_WHITESPACE, NULL, 0);
5407 def = new_Token(space, TOK_PREPROC_ID, cmd, 0);
5408 }
5409
5410 l = nasm_malloc(sizeof(Line));
5411 l->next = predef;
5412 l->first = def;
5413 l->finishes = NULL;
5414 predef = l;
5415}
5416
H. Peter Anvinf7606612016-07-13 14:23:48 -07005417static void pp_add_stdmac(macros_t *macros)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005418{
H. Peter Anvinf7606612016-07-13 14:23:48 -07005419 macros_t **mp;
5420
5421 /* Find the end of the list and avoid duplicates */
5422 for (mp = stdmacros; *mp; mp++) {
5423 if (*mp == macros)
5424 return; /* Nothing to do */
5425 }
5426
5427 nasm_assert(mp < &stdmacros[ARRAY_SIZE(stdmacros)-1]);
5428
5429 *mp = macros;
H. Peter Anvin76690a12002-04-30 20:52:49 +00005430}
5431
Cyrill Gorcunov15ce78f2017-01-06 20:21:28 +03005432static void pp_extra_stdmac(macros_t *macros)
5433{
5434 extrastdmac = macros;
5435}
5436
Keith Kaniosa5fc6462007-10-13 07:09:22 -07005437static void make_tok_num(Token * tok, int64_t val)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005438{
Cyrill Gorcunovce652742013-05-06 23:43:43 +04005439 char numbuf[32];
Keith Kaniosa5fc6462007-10-13 07:09:22 -07005440 snprintf(numbuf, sizeof(numbuf), "%"PRId64"", val);
H. Peter Anvineba20a72002-04-30 20:53:55 +00005441 tok->text = nasm_strdup(numbuf);
5442 tok->type = TOK_NUMBER;
5443}
5444
H. Peter Anvin37368952016-05-09 14:10:32 -07005445static void pp_list_one_macro(MMacro *m, int severity)
5446{
5447 if (!m)
5448 return;
5449
5450 /* We need to print the next_active list in reverse order */
5451 pp_list_one_macro(m->next_active, severity);
5452
5453 if (m->name && !m->nolist) {
H. Peter Anvin274cda82016-05-10 02:56:29 -07005454 src_set(m->xline + m->lineno, m->fname);
H. Peter Anvin37368952016-05-09 14:10:32 -07005455 nasm_error(severity, "... from macro `%s' defined here", m->name);
5456 }
5457}
5458
H. Peter Anvin4def1a82016-05-09 13:59:44 -07005459static void pp_error_list_macros(int severity)
5460{
H. Peter Anvin4def1a82016-05-09 13:59:44 -07005461 int32_t saved_line;
5462 const char *saved_fname = NULL;
5463
5464 severity |= ERR_PP_LISTMACRO | ERR_NO_SEVERITY;
H. Peter Anvin274cda82016-05-10 02:56:29 -07005465 src_get(&saved_line, &saved_fname);
H. Peter Anvin4def1a82016-05-09 13:59:44 -07005466
Cyrill Gorcunov771d04e2016-05-10 23:27:03 +03005467 if (istk)
5468 pp_list_one_macro(istk->mstk, severity);
H. Peter Anvin4def1a82016-05-09 13:59:44 -07005469
H. Peter Anvin274cda82016-05-10 02:56:29 -07005470 src_set(saved_line, saved_fname);
H. Peter Anvin4def1a82016-05-09 13:59:44 -07005471}
5472
H. Peter Anvine7469712016-02-18 02:20:59 -08005473const struct preproc_ops nasmpp = {
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07005474 pp_init,
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005475 pp_reset,
5476 pp_getline,
Cyrill Gorcunov0b78bff2012-05-07 01:57:55 +04005477 pp_cleanup,
Cyrill Gorcunov15ce78f2017-01-06 20:21:28 +03005478 pp_extra_stdmac,
Cyrill Gorcunov0b78bff2012-05-07 01:57:55 +04005479 pp_pre_define,
5480 pp_pre_undefine,
5481 pp_pre_include,
H. Peter Anvin05990342018-06-11 13:32:42 -07005482 pp_pre_command,
H. Peter Anvin4def1a82016-05-09 13:59:44 -07005483 pp_include_path,
5484 pp_error_list_macros,
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005485};