blob: 7eb7834336cf2736f8a5e2d12223a664804802a4 [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>
H. Peter Anvinc2f3f262018-12-27 12:37:25 -080069#include "nctype.h"
H. Peter Anvin76690a12002-04-30 20:52:49 +000070#include <limits.h>
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000071
72#include "nasm.h"
73#include "nasmlib.h"
H. Peter Anvinb20bc732017-03-07 19:23:03 -080074#include "error.h"
H. Peter Anvin4169a472007-09-12 01:29:43 +000075#include "preproc.h"
H. Peter Anvin97a23472007-09-16 17:57:25 -070076#include "hashtbl.h"
H. Peter Anvin8cad14b2008-06-01 17:23:51 -070077#include "quote.h"
H. Peter Anvinc2df2822007-10-24 15:29:28 -070078#include "stdscan.h"
H. Peter Anvindbb640b2009-07-18 18:57:16 -070079#include "eval.h"
H. Peter Anvinc2df2822007-10-24 15:29:28 -070080#include "tokens.h"
H. Peter Anvina4835d42008-05-20 14:21:29 -070081#include "tables.h"
H. Peter Anvin8ac25aa2016-02-18 01:16:18 -080082#include "listing.h"
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000083
84typedef struct SMacro SMacro;
H. Peter Anvin36206cd2012-03-03 16:14:51 -080085typedef struct MMacro MMacro;
86typedef struct MMacroInvocation MMacroInvocation;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000087typedef struct Context Context;
88typedef struct Token Token;
H. Peter Anvince616072002-04-30 21:02:23 +000089typedef struct Blocks Blocks;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000090typedef struct Line Line;
91typedef struct Include Include;
H. Peter Anvin36206cd2012-03-03 16:14:51 -080092typedef struct Cond Cond;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000093
94/*
H. Peter Anvin97a23472007-09-16 17:57:25 -070095 * Note on the storage of both SMacro and MMacros: the hash table
96 * indexes them case-insensitively, and we then have to go through a
97 * linked list of potential case aliases (and, for MMacros, parameter
98 * ranges); this is to preserve the matching semantics of the earlier
99 * code. If the number of case aliases for a specific macro is a
100 * performance issue, you may want to reconsider your coding style.
101 */
102
103/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000104 * Store the definition of a single-line macro.
105 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000106struct SMacro {
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800107 SMacro *next;
108 char *name;
109 bool casesense;
110 bool in_progress;
111 unsigned int nparam;
112 Token *expansion;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000113};
114
115/*
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800116 * Store the definition of a multi-line macro. This is also used to
117 * store the interiors of `%rep...%endrep' blocks, which are
118 * effectively self-re-invoking multi-line macros which simply
119 * don't have a name or bother to appear in the hash tables. %rep
120 * blocks are signified by having a NULL `name' field.
121 *
122 * In a MMacro describing a `%rep' block, the `in_progress' field
123 * isn't merely boolean, but gives the number of repeats left to
124 * run.
125 *
126 * The `next' field is used for storing MMacros in hash tables; the
127 * `next_active' field is for stacking them on istk entries.
128 *
129 * When a MMacro is being expanded, `params', `iline', `nparam',
130 * `paramlen', `rotate' and `unique' are local to the invocation.
131 */
132struct MMacro {
133 MMacro *next;
134 MMacroInvocation *prev; /* previous invocation */
135 char *name;
136 int nparam_min, nparam_max;
137 bool casesense;
138 bool plus; /* is the last parameter greedy? */
139 bool nolist; /* is this macro listing-inhibited? */
140 int64_t in_progress; /* is this macro currently being expanded? */
141 int32_t max_depth; /* maximum number of recursive expansions allowed */
142 Token *dlist; /* All defaults as one list */
143 Token **defaults; /* Parameter default pointers */
144 int ndefs; /* number of default parameters */
145 Line *expansion;
146
147 MMacro *next_active;
148 MMacro *rep_nest; /* used for nesting %rep */
149 Token **params; /* actual parameters */
150 Token *iline; /* invocation line */
151 unsigned int nparam, rotate;
152 int *paramlen;
153 uint64_t unique;
154 int lineno; /* Current line number on expansion */
155 uint64_t condcnt; /* number of if blocks... */
H. Peter Anvin4def1a82016-05-09 13:59:44 -0700156
H. Peter Anvin274cda82016-05-10 02:56:29 -0700157 const char *fname; /* File where defined */
H. Peter Anvin4def1a82016-05-09 13:59:44 -0700158 int32_t xline; /* First line in macro */
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800159};
160
161
162/* Store the definition of a multi-line macro, as defined in a
163 * previous recursive macro expansion.
164 */
165struct MMacroInvocation {
166 MMacroInvocation *prev; /* previous invocation */
167 Token **params; /* actual parameters */
168 Token *iline; /* invocation line */
169 unsigned int nparam, rotate;
170 int *paramlen;
171 uint64_t unique;
172 uint64_t condcnt;
173};
174
175
176/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000177 * The context stack is composed of a linked list of these.
178 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000179struct Context {
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800180 Context *next;
181 char *name;
182 struct hash_table localmac;
183 uint32_t number;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000184};
185
186/*
187 * This is the internal form which we break input lines up into.
188 * Typically stored in linked lists.
189 *
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000190 * Note that `type' serves a double meaning: TOK_SMAC_PARAM is not
191 * necessarily used as-is, but is intended to denote the number of
192 * the substituted parameter. So in the definition
193 *
194 * %define a(x,y) ( (x) & ~(y) )
H. Peter Anvin70653092007-10-19 14:42:29 -0700195 *
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000196 * the token representing `x' will have its type changed to
197 * TOK_SMAC_PARAM, but the one representing `y' will be
198 * TOK_SMAC_PARAM+1.
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000199 *
200 * TOK_INTERNAL_STRING is a dirty hack: it's a single string token
201 * which doesn't need quotes around it. Used in the pre-include
202 * mechanism as an alternative to trying to find a sensible type of
203 * quote to use on the filename we were passed.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000204 */
H. Peter Anvinda10e7b2007-09-12 04:18:37 +0000205enum pp_token_type {
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800206 TOK_NONE = 0, TOK_WHITESPACE, TOK_COMMENT, TOK_ID,
207 TOK_PREPROC_ID, TOK_STRING,
208 TOK_NUMBER, TOK_FLOAT, TOK_SMAC_END, TOK_OTHER,
H. Peter Anvin6c81f0a2008-05-25 21:46:17 -0700209 TOK_INTERNAL_STRING,
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800210 TOK_PREPROC_Q, TOK_PREPROC_QQ,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300211 TOK_PASTE, /* %+ */
212 TOK_INDIRECT, /* %[...] */
213 TOK_SMAC_PARAM, /* MUST BE LAST IN THE LIST!!! */
214 TOK_MAX = INT_MAX /* Keep compiler from reducing the range */
H. Peter Anvinda10e7b2007-09-12 04:18:37 +0000215};
216
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +0400217#define PP_CONCAT_MASK(x) (1 << (x))
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +0400218#define PP_CONCAT_MATCH(t, mask) (PP_CONCAT_MASK((t)->type) & mask)
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +0400219
Cyrill Gorcunov575d4282010-10-06 00:25:55 +0400220struct tokseq_match {
221 int mask_head;
222 int mask_tail;
223};
224
H. Peter Anvine2c80182005-01-15 22:15:51 +0000225struct Token {
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800226 Token *next;
227 char *text;
H. Peter Anvinf26e0972008-07-01 21:26:27 -0700228 union {
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800229 SMacro *mac; /* associated macro for TOK_SMAC_END */
230 size_t len; /* scratch length field */
231 } a; /* Auxiliary data */
232 enum pp_token_type type;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000233};
234
235/*
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800236 * Multi-line macro definitions are stored as a linked list of
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000237 * these, which is essentially a container to allow several linked
238 * lists of Tokens.
H. Peter Anvin70653092007-10-19 14:42:29 -0700239 *
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000240 * Note that in this module, linked lists are treated as stacks
241 * wherever possible. For this reason, Lines are _pushed_ on to the
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800242 * `expansion' field in MMacro structures, so that the linked list,
243 * if walked, would give the macro lines in reverse order; this
244 * means that we can walk the list when expanding a macro, and thus
245 * push the lines on to the `expansion' field in _istk_ in reverse
246 * order (so that when popped back off they are in the right
247 * order). It may seem cockeyed, and it relies on my design having
248 * an even number of steps in, but it works...
249 *
250 * Some of these structures, rather than being actual lines, are
251 * markers delimiting the end of the expansion of a given macro.
252 * This is for use in the cycle-tracking and %rep-handling code.
253 * Such structures have `finishes' non-NULL, and `first' NULL. All
254 * others have `finishes' NULL, but `first' may still be NULL if
255 * the line is blank.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000256 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000257struct Line {
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800258 Line *next;
259 MMacro *finishes;
260 Token *first;
Keith Kaniosb307a4f2010-11-06 17:41:51 -0500261};
262
263/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000264 * To handle an arbitrary level of file inclusion, we maintain a
265 * stack (ie linked list) of these things.
266 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000267struct Include {
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800268 Include *next;
269 FILE *fp;
270 Cond *conds;
271 Line *expansion;
H. Peter Anvin274cda82016-05-10 02:56:29 -0700272 const char *fname;
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800273 int lineno, lineinc;
274 MMacro *mstk; /* stack of active macros/reps */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000275};
276
277/*
H. Peter Anvin169ac7c2016-09-25 17:08:05 -0700278 * File real name hash, so we don't have to re-search the include
279 * path for every pass (and potentially more than that if a file
280 * is used more than once.)
281 */
282struct hash_table FileHash;
283
284/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000285 * Conditional assembly: we maintain a separate stack of these for
286 * each level of file inclusion. (The only reason we keep the
287 * stacks separate is to ensure that a stray `%endif' in a file
288 * included from within the true branch of a `%if' won't terminate
289 * it and cause confusion: instead, rightly, it'll cause an error.)
290 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800291struct Cond {
292 Cond *next;
293 int state;
294};
H. Peter Anvine2c80182005-01-15 22:15:51 +0000295enum {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000296 /*
297 * These states are for use just after %if or %elif: IF_TRUE
298 * means the condition has evaluated to truth so we are
299 * currently emitting, whereas IF_FALSE means we are not
300 * currently emitting but will start doing so if a %else comes
301 * up. In these states, all directives are admissible: %elif,
302 * %else and %endif. (And of course %if.)
303 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800304 COND_IF_TRUE, COND_IF_FALSE,
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000305 /*
306 * These states come up after a %else: ELSE_TRUE means we're
307 * emitting, and ELSE_FALSE means we're not. In ELSE_* states,
308 * any %elif or %else will cause an error.
309 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800310 COND_ELSE_TRUE, COND_ELSE_FALSE,
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000311 /*
Victor van den Elzen3b404c02008-09-18 13:51:36 +0200312 * These states mean that we're not emitting now, and also that
313 * nothing until %endif will be emitted at all. COND_DONE is
314 * used when we've had our moment of emission
315 * and have now started seeing %elifs. COND_NEVER is used when
316 * the condition construct in question is contained within a
317 * non-emitting branch of a larger condition construct,
318 * or if there is an error.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000319 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800320 COND_DONE, COND_NEVER
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000321};
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800322#define emitting(x) ( (x) == COND_IF_TRUE || (x) == COND_ELSE_TRUE )
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000323
H. Peter Anvin70653092007-10-19 14:42:29 -0700324/*
Ed Beroset3ab3f412002-06-11 03:31:49 +0000325 * These defines are used as the possible return values for do_directive
326 */
327#define NO_DIRECTIVE_FOUND 0
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300328#define DIRECTIVE_FOUND 1
Ed Beroset3ab3f412002-06-11 03:31:49 +0000329
Cyrill Gorcunove091d6e2010-08-09 13:58:22 +0400330/* max reps */
331#define REP_LIMIT ((INT64_C(1) << 62))
332
Keith Kanios852f1ee2009-07-12 00:19:55 -0500333/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000334 * Condition codes. Note that we use c_ prefix not C_ because C_ is
335 * used in nasm.h for the "real" condition codes. At _this_ level,
336 * we treat CXZ and ECXZ as condition codes, albeit non-invertible
337 * ones, so we need a different enum...
338 */
H. Peter Anvin476d2862007-10-02 22:04:15 -0700339static const char * const conditions[] = {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000340 "a", "ae", "b", "be", "c", "cxz", "e", "ecxz", "g", "ge", "l", "le",
341 "na", "nae", "nb", "nbe", "nc", "ne", "ng", "nge", "nl", "nle", "no",
H. Peter Anvince9be342007-09-12 00:22:29 +0000342 "np", "ns", "nz", "o", "p", "pe", "po", "rcxz", "s", "z"
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000343};
H. Peter Anvin476d2862007-10-02 22:04:15 -0700344enum pp_conds {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000345 c_A, c_AE, c_B, c_BE, c_C, c_CXZ, c_E, c_ECXZ, c_G, c_GE, c_L, c_LE,
346 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 -0700347 c_NP, c_NS, c_NZ, c_O, c_P, c_PE, c_PO, c_RCXZ, c_S, c_Z,
348 c_none = -1
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000349};
H. Peter Anvin476d2862007-10-02 22:04:15 -0700350static const enum pp_conds inverse_ccs[] = {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000351 c_NA, c_NAE, c_NB, c_NBE, c_NC, -1, c_NE, -1, c_NG, c_NGE, c_NL, c_NLE,
352 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 +0000353 c_Z, c_NO, c_NP, c_PO, c_PE, -1, c_NS, c_NZ
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000354};
355
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800356/*
357 * Directive names.
358 */
359/* If this is a an IF, ELIF, ELSE or ENDIF keyword */
360static int is_condition(enum preproc_token arg)
361{
362 return PP_IS_COND(arg) || (arg == PP_ELSE) || (arg == PP_ENDIF);
363}
364
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000365/* For TASM compatibility we need to be able to recognise TASM compatible
366 * conditional compilation directives. Using the NASM pre-processor does
367 * not work, so we look for them specifically from the following list and
368 * then jam in the equivalent NASM directive into the input stream.
369 */
370
H. Peter Anvine2c80182005-01-15 22:15:51 +0000371enum {
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000372 TM_ARG, TM_ELIF, TM_ELSE, TM_ENDIF, TM_IF, TM_IFDEF, TM_IFDIFI,
373 TM_IFNDEF, TM_INCLUDE, TM_LOCAL
374};
375
H. Peter Anvin476d2862007-10-02 22:04:15 -0700376static const char * const tasm_directives[] = {
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000377 "arg", "elif", "else", "endif", "if", "ifdef", "ifdifi",
378 "ifndef", "include", "local"
379};
380
381static int StackSize = 4;
H. Peter Anvin6c8b2be2016-05-24 23:46:50 -0700382static const char *StackPointer = "ebp";
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000383static int ArgOffset = 8;
H. Peter Anvin8781cb02007-11-08 20:01:11 -0800384static int LocalOffset = 0;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000385
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000386static Context *cstk;
387static Include *istk;
Cyrill Gorcunov8c0666b2018-11-24 14:33:48 +0300388static const struct strlist *ipath_list;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000389
Cyrill Gorcunovb7bb5ac2018-11-11 21:33:52 +0300390static struct strlist *deplist;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000391
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300392static uint64_t unique; /* unique identifier numbers */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000393
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800394static Line *predef = NULL;
H. Peter Anvind2456592008-06-19 15:04:18 -0700395static bool do_predef;
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -0800396static enum preproc_mode pp_mode;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000397
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000398/*
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800399 * The current set of multi-line macros we have defined.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000400 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800401static struct hash_table mmacros;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000402
403/*
404 * The current set of single-line macros we have defined.
405 */
H. Peter Anvin166c2472008-05-28 12:28:58 -0700406static struct hash_table smacros;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000407
408/*
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800409 * The multi-line macro we are currently defining, or the %rep
410 * block we are currently reading, if any.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000411 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800412static MMacro *defining;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000413
Charles Crayned4200be2008-07-12 16:42:33 -0700414static uint64_t nested_mac_count;
415static uint64_t nested_rep_count;
416
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000417/*
418 * The number of macro parameters to allocate space for at a time.
419 */
420#define PARAM_DELTA 16
421
422/*
H. Peter Anvinf7606612016-07-13 14:23:48 -0700423 * The standard macro set: defined in macros.c in a set of arrays.
424 * This gives our position in any macro set, while we are processing it.
425 * The stdmacset is an array of such macro sets.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000426 */
H. Peter Anvina70547f2008-07-19 21:44:26 -0700427static macros_t *stdmacpos;
H. Peter Anvinf7606612016-07-13 14:23:48 -0700428static macros_t **stdmacnext;
429static macros_t *stdmacros[8];
Cyrill Gorcunov15ce78f2017-01-06 20:21:28 +0300430static macros_t *extrastdmac;
H. Peter Anvin76690a12002-04-30 20:52:49 +0000431
432/*
H. Peter Anvin734b1882002-04-30 21:01:08 +0000433 * Tokens are allocated in blocks to improve speed
434 */
435#define TOKEN_BLOCKSIZE 4096
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800436static Token *freeTokens = NULL;
H. Peter Anvince616072002-04-30 21:02:23 +0000437struct Blocks {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000438 Blocks *next;
439 void *chunk;
H. Peter Anvince616072002-04-30 21:02:23 +0000440};
441
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800442static Blocks blocks = { NULL, NULL };
H. Peter Anvin734b1882002-04-30 21:01:08 +0000443
444/*
H. Peter Anvin76690a12002-04-30 20:52:49 +0000445 * Forward declarations.
446 */
H. Peter Anvinf7606612016-07-13 14:23:48 -0700447static void pp_add_stdmac(macros_t *macros);
H. Peter Anvin734b1882002-04-30 21:01:08 +0000448static Token *expand_mmac_params(Token * tline);
449static Token *expand_smacro(Token * tline);
450static Token *expand_id(Token * tline);
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +0400451static Context *get_ctx(const char *name, const char **namep);
Keith Kaniosa5fc6462007-10-13 07:09:22 -0700452static void make_tok_num(Token * tok, int64_t val);
H. Peter Anvin (Intel)6bde2ed2018-12-13 19:39:41 -0800453static void pp_verror(errflags severity, const char *fmt, va_list ap);
H. Peter Anvin130736c2016-02-17 20:27:41 -0800454static vefunc real_verror;
H. Peter Anvince616072002-04-30 21:02:23 +0000455static void *new_Block(size_t size);
456static void delete_Blocks(void);
H. Peter Anvinc751e862008-06-09 10:18:45 -0700457static Token *new_Token(Token * next, enum pp_token_type type,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300458 const char *text, int txtlen);
H. Peter Anvin734b1882002-04-30 21:01:08 +0000459static Token *delete_Token(Token * t);
H. Peter Anvineba20a72002-04-30 20:53:55 +0000460
461/*
462 * Macros for safe checking of token pointers, avoid *(NULL)
463 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300464#define tok_type_(x,t) ((x) && (x)->type == (t))
465#define skip_white_(x) if (tok_type_((x), TOK_WHITESPACE)) (x)=(x)->next
466#define tok_is_(x,v) (tok_type_((x), TOK_OTHER) && !strcmp((x)->text,(v)))
467#define tok_isnt_(x,v) ((x) && ((x)->type!=TOK_OTHER || strcmp((x)->text,(v))))
H. Peter Anvin76690a12002-04-30 20:52:49 +0000468
Cyrill Gorcunov194ba892011-06-30 01:16:35 +0400469/*
H. Peter Anvin077fb932010-07-20 14:56:30 -0700470 * nasm_unquote with error if the string contains NUL characters.
471 * If the string contains NUL characters, issue an error and return
472 * the C len, i.e. truncate at the NUL.
473 */
474static size_t nasm_unquote_cstr(char *qstr, enum preproc_token directive)
475{
476 size_t len = nasm_unquote(qstr, NULL);
477 size_t clen = strlen(qstr);
478
479 if (len != clen)
Cyrill Gorcunov295b7952018-11-25 12:55:48 +0300480 nasm_nonfatal("NUL character in `%s' directive",
481 pp_directives[directive]);
H. Peter Anvin077fb932010-07-20 14:56:30 -0700482 return clen;
483}
484
485/*
H. Peter Anvinb40992c2010-09-15 08:57:21 -0700486 * In-place reverse a list of tokens.
487 */
488static Token *reverse_tokens(Token *t)
489{
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800490 Token *prev = NULL;
491 Token *next;
H. Peter Anvinb40992c2010-09-15 08:57:21 -0700492
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800493 while (t) {
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +0400494 next = t->next;
495 t->next = prev;
496 prev = t;
497 t = next;
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800498 }
H. Peter Anvinb40992c2010-09-15 08:57:21 -0700499
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800500 return prev;
H. Peter Anvinb40992c2010-09-15 08:57:21 -0700501}
502
503/*
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300504 * Handle TASM specific directives, which do not contain a % in
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000505 * front of them. We do it here because I could not find any other
506 * place to do it for the moment, and it is a hack (ideally it would
507 * be nice to be able to use the NASM pre-processor to do it).
508 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000509static char *check_tasm_directive(char *line)
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000510{
Keith Kaniosb7a89542007-04-12 02:40:54 +0000511 int32_t i, j, k, m, len;
Cyrill Gorcunovf66ac7d2009-10-12 20:41:13 +0400512 char *p, *q, *oldline, oldchar;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000513
Cyrill Gorcunovf66ac7d2009-10-12 20:41:13 +0400514 p = nasm_skip_spaces(line);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000515
516 /* Binary search for the directive name */
517 i = -1;
Cyrill Gorcunova7319242010-06-03 22:04:36 +0400518 j = ARRAY_SIZE(tasm_directives);
Cyrill Gorcunovf66ac7d2009-10-12 20:41:13 +0400519 q = nasm_skip_word(p);
520 len = q - p;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000521 if (len) {
522 oldchar = p[len];
523 p[len] = 0;
524 while (j - i > 1) {
525 k = (j + i) / 2;
526 m = nasm_stricmp(p, tasm_directives[k]);
527 if (m == 0) {
528 /* We have found a directive, so jam a % in front of it
529 * so that NASM will then recognise it as one if it's own.
530 */
531 p[len] = oldchar;
532 len = strlen(p);
533 oldline = line;
534 line = nasm_malloc(len + 2);
535 line[0] = '%';
536 if (k == TM_IFDIFI) {
H. Peter Anvin18f48792009-06-27 15:56:27 -0700537 /*
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300538 * NASM does not recognise IFDIFI, so we convert
539 * it to %if 0. This is not used in NASM
540 * compatible code, but does need to parse for the
541 * TASM macro package.
H. Peter Anvine2c80182005-01-15 22:15:51 +0000542 */
H. Peter Anvin18f48792009-06-27 15:56:27 -0700543 strcpy(line + 1, "if 0");
H. Peter Anvine2c80182005-01-15 22:15:51 +0000544 } else {
545 memcpy(line + 1, p, len + 1);
546 }
547 nasm_free(oldline);
548 return line;
549 } else if (m < 0) {
550 j = k;
551 } else
552 i = k;
553 }
554 p[len] = oldchar;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000555 }
556 return line;
557}
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000558
H. Peter Anvin76690a12002-04-30 20:52:49 +0000559/*
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000560 * The pre-preprocessing stage... This function translates line
561 * number indications as they emerge from GNU cpp (`# lineno "file"
562 * flags') into NASM preprocessor line number indications (`%line
563 * lineno file').
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000564 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000565static char *prepreproc(char *line)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000566{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000567 int lineno, fnlen;
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000568 char *fname, *oldline;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000569
H. Peter Anvine2c80182005-01-15 22:15:51 +0000570 if (line[0] == '#' && line[1] == ' ') {
571 oldline = line;
572 fname = oldline + 2;
573 lineno = atoi(fname);
574 fname += strspn(fname, "0123456789 ");
575 if (*fname == '"')
576 fname++;
577 fnlen = strcspn(fname, "\"");
578 line = nasm_malloc(20 + fnlen);
579 snprintf(line, 20 + fnlen, "%%line %d %.*s", lineno, fnlen, fname);
580 nasm_free(oldline);
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000581 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000582 if (tasm_compatible_mode)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000583 return check_tasm_directive(line);
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000584 return line;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000585}
586
587/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000588 * Free a linked list of tokens.
589 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000590static void free_tlist(Token * list)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000591{
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +0400592 while (list)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000593 list = delete_Token(list);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000594}
595
596/*
597 * Free a linked list of lines.
598 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000599static void free_llist(Line * list)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000600{
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +0400601 Line *l, *tmp;
602 list_for_each_safe(l, tmp, list) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000603 free_tlist(l->first);
604 nasm_free(l);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000605 }
606}
607
608/*
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800609 * Free an MMacro
H. Peter Anvineba20a72002-04-30 20:53:55 +0000610 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800611static void free_mmacro(MMacro * m)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000612{
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800613 nasm_free(m->name);
614 free_tlist(m->dlist);
615 nasm_free(m->defaults);
616 free_llist(m->expansion);
617 nasm_free(m);
H. Peter Anvineba20a72002-04-30 20:53:55 +0000618}
619
620/*
H. Peter Anvin97a23472007-09-16 17:57:25 -0700621 * Free all currently defined macros, and free the hash tables
622 */
H. Peter Anvin072771e2008-05-22 13:17:51 -0700623static void free_smacro_table(struct hash_table *smt)
H. Peter Anvin97a23472007-09-16 17:57:25 -0700624{
H. Peter Anvin (Intel)ebb05a02018-12-11 12:30:25 -0800625 struct hash_iterator it;
626 const struct hash_node *np;
H. Peter Anvin97a23472007-09-16 17:57:25 -0700627
H. Peter Anvin (Intel)ebb05a02018-12-11 12:30:25 -0800628 hash_for_each(smt, it, np) {
629 SMacro *tmp;
630 SMacro *s = np->data;
631 nasm_free((void *)np->key);
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +0400632 list_for_each_safe(s, tmp, s) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300633 nasm_free(s->name);
634 free_tlist(s->expansion);
635 nasm_free(s);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300636 }
H. Peter Anvin97a23472007-09-16 17:57:25 -0700637 }
H. Peter Anvin072771e2008-05-22 13:17:51 -0700638 hash_free(smt);
639}
640
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800641static void free_mmacro_table(struct hash_table *mmt)
H. Peter Anvin072771e2008-05-22 13:17:51 -0700642{
H. Peter Anvin (Intel)ebb05a02018-12-11 12:30:25 -0800643 struct hash_iterator it;
644 const struct hash_node *np;
H. Peter Anvin97a23472007-09-16 17:57:25 -0700645
H. Peter Anvin (Intel)ebb05a02018-12-11 12:30:25 -0800646 hash_for_each(mmt, it, np) {
647 MMacro *tmp;
648 MMacro *m = np->data;
649 nasm_free((void *)np->key);
650 list_for_each_safe(m, tmp, m)
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800651 free_mmacro(m);
H. Peter Anvin97a23472007-09-16 17:57:25 -0700652 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800653 hash_free(mmt);
H. Peter Anvin072771e2008-05-22 13:17:51 -0700654}
655
656static void free_macros(void)
657{
H. Peter Anvin166c2472008-05-28 12:28:58 -0700658 free_smacro_table(&smacros);
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800659 free_mmacro_table(&mmacros);
H. Peter Anvin97a23472007-09-16 17:57:25 -0700660}
661
662/*
663 * Initialize the hash tables
664 */
665static void init_macros(void)
666{
H. Peter Anvin97a23472007-09-16 17:57:25 -0700667}
668
669/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000670 * Pop the context stack.
671 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000672static void ctx_pop(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000673{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000674 Context *c = cstk;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000675
676 cstk = cstk->next;
H. Peter Anvin166c2472008-05-28 12:28:58 -0700677 free_smacro_table(&c->localmac);
H. Peter Anvin734b1882002-04-30 21:01:08 +0000678 nasm_free(c->name);
679 nasm_free(c);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000680}
681
H. Peter Anvin072771e2008-05-22 13:17:51 -0700682/*
683 * Search for a key in the hash index; adding it if necessary
684 * (in which case we initialize the data pointer to NULL.)
685 */
686static void **
687hash_findi_add(struct hash_table *hash, const char *str)
688{
689 struct hash_insert hi;
690 void **r;
691 char *strx;
H. Peter Anvin (Intel)ebb05a02018-12-11 12:30:25 -0800692 size_t l = strlen(str) + 1;
H. Peter Anvin072771e2008-05-22 13:17:51 -0700693
H. Peter Anvin (Intel)ebb05a02018-12-11 12:30:25 -0800694 r = hash_findib(hash, str, l, &hi);
H. Peter Anvin072771e2008-05-22 13:17:51 -0700695 if (r)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300696 return r;
H. Peter Anvin072771e2008-05-22 13:17:51 -0700697
H. Peter Anvin (Intel)ebb05a02018-12-11 12:30:25 -0800698 strx = nasm_malloc(l); /* Use a more efficient allocator here? */
699 memcpy(strx, str, l);
H. Peter Anvin072771e2008-05-22 13:17:51 -0700700 return hash_add(&hi, strx, NULL);
701}
702
703/*
704 * Like hash_findi, but returns the data element rather than a pointer
705 * to it. Used only when not adding a new element, hence no third
706 * argument.
707 */
708static void *
709hash_findix(struct hash_table *hash, const char *str)
710{
711 void **p;
712
713 p = hash_findi(hash, str, NULL);
714 return p ? *p : NULL;
715}
716
Cyrill Gorcunov15bdc512010-07-13 11:27:41 +0400717/*
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800718 * read line from standart macros set,
Cyrill Gorcunov15bdc512010-07-13 11:27:41 +0400719 * if there no more left -- return NULL
720 */
721static char *line_from_stdmac(void)
722{
723 unsigned char c;
724 const unsigned char *p = stdmacpos;
725 char *line, *q;
726 size_t len = 0;
727
728 if (!stdmacpos)
729 return NULL;
730
731 while ((c = *p++)) {
732 if (c >= 0x80)
733 len += pp_directives_len[c - 0x80] + 1;
734 else
735 len++;
736 }
737
738 line = nasm_malloc(len + 1);
739 q = line;
740 while ((c = *stdmacpos++)) {
741 if (c >= 0x80) {
742 memcpy(q, pp_directives[c - 0x80], pp_directives_len[c - 0x80]);
743 q += pp_directives_len[c - 0x80];
744 *q++ = ' ';
745 } else {
746 *q++ = c;
747 }
748 }
749 stdmacpos = p;
750 *q = '\0';
751
752 if (!*stdmacpos) {
H. Peter Anvinf7606612016-07-13 14:23:48 -0700753 /* This was the last of this particular macro set */
Cyrill Gorcunov15bdc512010-07-13 11:27:41 +0400754 stdmacpos = NULL;
H. Peter Anvinf7606612016-07-13 14:23:48 -0700755 if (*stdmacnext) {
756 stdmacpos = *stdmacnext++;
Cyrill Gorcunov15bdc512010-07-13 11:27:41 +0400757 } else if (do_predef) {
758 Line *pd, *l;
759 Token *head, **tail, *t;
760
761 /*
762 * Nasty hack: here we push the contents of
763 * `predef' on to the top-level expansion stack,
764 * since this is the most convenient way to
765 * implement the pre-include and pre-define
766 * features.
767 */
768 list_for_each(pd, predef) {
769 head = NULL;
770 tail = &head;
771 list_for_each(t, pd->first) {
772 *tail = new_Token(NULL, t->type, t->text, 0);
773 tail = &(*tail)->next;
774 }
775
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800776 l = nasm_malloc(sizeof(Line));
777 l->next = istk->expansion;
778 l->first = head;
779 l->finishes = NULL;
780
781 istk->expansion = l;
Cyrill Gorcunov15bdc512010-07-13 11:27:41 +0400782 }
783 do_predef = false;
784 }
785 }
786
787 return line;
788}
789
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000790static char *read_line(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000791{
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +0400792 unsigned int size, c, next;
793 const unsigned int delta = 512;
794 const unsigned int pad = 8;
795 unsigned int nr_cont = 0;
796 bool cont = false;
797 char *buffer, *p;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000798
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +0400799 /* Standart macros set (predefined) goes first */
Cyrill Gorcunov15bdc512010-07-13 11:27:41 +0400800 p = line_from_stdmac();
801 if (p)
802 return p;
H. Peter Anvin72edbb82008-06-19 16:00:04 -0700803
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +0400804 size = delta;
805 p = buffer = nasm_malloc(size);
806
807 for (;;) {
808 c = fgetc(istk->fp);
809 if ((int)(c) == EOF) {
810 p[0] = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000811 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000812 }
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +0400813
814 switch (c) {
815 case '\r':
816 next = fgetc(istk->fp);
817 if (next != '\n')
818 ungetc(next, istk->fp);
819 if (cont) {
820 cont = false;
821 continue;
822 }
823 break;
824
825 case '\n':
826 if (cont) {
827 cont = false;
828 continue;
829 }
830 break;
831
832 case '\\':
833 next = fgetc(istk->fp);
834 ungetc(next, istk->fp);
Cyrill Gorcunov490f85e2012-12-27 20:02:17 +0400835 if (next == '\r' || next == '\n') {
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +0400836 cont = true;
837 nr_cont++;
838 continue;
839 }
840 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000841 }
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +0400842
843 if (c == '\r' || c == '\n') {
844 *p++ = 0;
845 break;
846 }
847
848 if (p >= (buffer + size - pad)) {
849 buffer = nasm_realloc(buffer, size + delta);
850 p = buffer + size - pad;
851 size += delta;
852 }
853
854 *p++ = (unsigned char)c;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000855 }
856
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +0400857 if (p == buffer) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000858 nasm_free(buffer);
859 return NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000860 }
861
Cyrill Gorcunova5aea572010-11-11 10:14:45 +0300862 src_set_linnum(src_get_linnum() + istk->lineinc +
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +0400863 (nr_cont * istk->lineinc));
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000864
865 /*
866 * Handle spurious ^Z, which may be inserted into source files
867 * by some file transfer utilities.
868 */
869 buffer[strcspn(buffer, "\032")] = '\0';
870
H. Peter Anvin8ac25aa2016-02-18 01:16:18 -0800871 lfmt->line(LIST_READ, buffer);
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000872
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000873 return buffer;
874}
875
876/*
Keith Kaniosb7a89542007-04-12 02:40:54 +0000877 * Tokenize a line of text. This is a very simple process since we
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000878 * don't need to parse the value out of e.g. numeric tokens: we
879 * simply split one string into many.
880 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000881static Token *tokenize(char *line)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000882{
H. Peter Anvinca544db2008-10-19 19:30:11 -0700883 char c, *p = line;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +0000884 enum pp_token_type type;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000885 Token *list = NULL;
886 Token *t, **tail = &list;
887
H. Peter Anvine2c80182005-01-15 22:15:51 +0000888 while (*line) {
889 p = line;
890 if (*p == '%') {
891 p++;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300892 if (*p == '+' && !nasm_isdigit(p[1])) {
893 p++;
894 type = TOK_PASTE;
895 } else if (nasm_isdigit(*p) ||
896 ((*p == '-' || *p == '+') && nasm_isdigit(p[1]))) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000897 do {
898 p++;
899 }
H. Peter Anvinbda7a6e2008-06-21 10:23:17 -0700900 while (nasm_isdigit(*p));
H. Peter Anvine2c80182005-01-15 22:15:51 +0000901 type = TOK_PREPROC_ID;
902 } else if (*p == '{') {
903 p++;
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800904 while (*p) {
905 if (*p == '}')
906 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000907 p[-1] = *p;
908 p++;
909 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800910 if (*p != '}')
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -0800911 nasm_warn(WARN_OTHER, "unterminated %%{ construct");
H. Peter Anvine2c80182005-01-15 22:15:51 +0000912 p[-1] = '\0';
913 if (*p)
914 p++;
915 type = TOK_PREPROC_ID;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300916 } else if (*p == '[') {
917 int lvl = 1;
918 line += 2; /* Skip the leading %[ */
919 p++;
920 while (lvl && (c = *p++)) {
921 switch (c) {
922 case ']':
923 lvl--;
924 break;
925 case '%':
926 if (*p == '[')
927 lvl++;
928 break;
929 case '\'':
930 case '\"':
931 case '`':
Cyrill Gorcunov3144e842017-10-22 10:50:55 +0300932 p = nasm_skip_string(p - 1);
933 if (*p)
934 p++;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300935 break;
936 default:
937 break;
938 }
939 }
940 p--;
941 if (*p)
942 *p++ = '\0';
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800943 if (lvl)
Cyrill Gorcunov295b7952018-11-25 12:55:48 +0300944 nasm_nonfatalf(ERR_PASS1, "unterminated %%[ construct");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300945 type = TOK_INDIRECT;
946 } else if (*p == '?') {
947 type = TOK_PREPROC_Q; /* %? */
948 p++;
949 if (*p == '?') {
950 type = TOK_PREPROC_QQ; /* %?? */
951 p++;
952 }
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +0400953 } else if (*p == '!') {
954 type = TOK_PREPROC_ID;
955 p++;
H. Peter Anvin13506202018-11-28 14:55:58 -0800956 if (nasm_isidchar(*p)) {
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +0400957 do {
958 p++;
959 }
H. Peter Anvin13506202018-11-28 14:55:58 -0800960 while (nasm_isidchar(*p));
H. Peter Anvin53e2e4c2018-11-28 15:01:40 -0800961 } else if (nasm_isquote(*p)) {
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +0400962 p = nasm_skip_string(p);
963 if (*p)
964 p++;
965 else
Cyrill Gorcunov295b7952018-11-25 12:55:48 +0300966 nasm_nonfatalf(ERR_PASS1, "unterminated %%! string");
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +0400967 } else {
968 /* %! without string or identifier */
969 type = TOK_OTHER; /* Legacy behavior... */
970 }
H. Peter Anvin13506202018-11-28 14:55:58 -0800971 } else if (nasm_isidchar(*p) ||
H. Peter Anvine2c80182005-01-15 22:15:51 +0000972 ((*p == '!' || *p == '%' || *p == '$') &&
H. Peter Anvin13506202018-11-28 14:55:58 -0800973 nasm_isidchar(p[1]))) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000974 do {
975 p++;
976 }
H. Peter Anvin13506202018-11-28 14:55:58 -0800977 while (nasm_isidchar(*p));
H. Peter Anvine2c80182005-01-15 22:15:51 +0000978 type = TOK_PREPROC_ID;
979 } else {
980 type = TOK_OTHER;
981 if (*p == '%')
982 p++;
983 }
H. Peter Anvin13506202018-11-28 14:55:58 -0800984 } else if (nasm_isidstart(*p) || (*p == '$' && nasm_isidstart(p[1]))) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000985 type = TOK_ID;
986 p++;
H. Peter Anvin13506202018-11-28 14:55:58 -0800987 while (*p && nasm_isidchar(*p))
H. Peter Anvine2c80182005-01-15 22:15:51 +0000988 p++;
H. Peter Anvin53e2e4c2018-11-28 15:01:40 -0800989 } else if (nasm_isquote(*p)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000990 /*
991 * A string token.
992 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000993 type = TOK_STRING;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300994 p = nasm_skip_string(p);
Nickolay Yurchenkof3b3ce22003-09-21 20:38:43 +0000995
H. Peter Anvine2c80182005-01-15 22:15:51 +0000996 if (*p) {
997 p++;
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800998 } else {
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -0800999 nasm_warn(WARN_OTHER, "unterminated string");
H. Peter Anvine2c80182005-01-15 22:15:51 +00001000 /* Handling unterminated strings by UNV */
1001 /* type = -1; */
1002 }
Victor van den Elzenfb5f2512009-04-17 16:17:59 +02001003 } else if (p[0] == '$' && p[1] == '$') {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001004 type = TOK_OTHER; /* TOKEN_BASE */
Victor van den Elzenfb5f2512009-04-17 16:17:59 +02001005 p += 2;
H. Peter Anvin13506202018-11-28 14:55:58 -08001006 } else if (nasm_isnumstart(*p)) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001007 bool is_hex = false;
1008 bool is_float = false;
1009 bool has_e = false;
1010 char c, *r;
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001011
H. Peter Anvine2c80182005-01-15 22:15:51 +00001012 /*
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001013 * A numeric token.
H. Peter Anvine2c80182005-01-15 22:15:51 +00001014 */
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001015
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001016 if (*p == '$') {
1017 p++;
1018 is_hex = true;
1019 }
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001020
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001021 for (;;) {
1022 c = *p++;
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001023
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001024 if (!is_hex && (c == 'e' || c == 'E')) {
1025 has_e = true;
1026 if (*p == '+' || *p == '-') {
1027 /*
1028 * e can only be followed by +/- if it is either a
1029 * prefixed hex number or a floating-point number
1030 */
1031 p++;
1032 is_float = true;
1033 }
1034 } else if (c == 'H' || c == 'h' || c == 'X' || c == 'x') {
1035 is_hex = true;
1036 } else if (c == 'P' || c == 'p') {
1037 is_float = true;
1038 if (*p == '+' || *p == '-')
1039 p++;
H. Peter Anvin13506202018-11-28 14:55:58 -08001040 } else if (nasm_isnumchar(c))
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001041 ; /* just advance */
1042 else if (c == '.') {
1043 /*
1044 * we need to deal with consequences of the legacy
1045 * parser, like "1.nolist" being two tokens
1046 * (TOK_NUMBER, TOK_ID) here; at least give it
1047 * a shot for now. In the future, we probably need
1048 * a flex-based scanner with proper pattern matching
1049 * to do it as well as it can be done. Nothing in
1050 * the world is going to help the person who wants
1051 * 0x123.p16 interpreted as two tokens, though.
1052 */
1053 r = p;
1054 while (*r == '_')
1055 r++;
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001056
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001057 if (nasm_isdigit(*r) || (is_hex && nasm_isxdigit(*r)) ||
1058 (!is_hex && (*r == 'e' || *r == 'E')) ||
1059 (*r == 'p' || *r == 'P')) {
1060 p = r;
1061 is_float = true;
1062 } else
1063 break; /* Terminate the token */
1064 } else
1065 break;
1066 }
1067 p--; /* Point to first character beyond number */
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001068
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001069 if (p == line+1 && *line == '$') {
1070 type = TOK_OTHER; /* TOKEN_HERE */
1071 } else {
1072 if (has_e && !is_hex) {
1073 /* 1e13 is floating-point, but 1e13h is not */
1074 is_float = true;
1075 }
H. Peter Anvind784a082009-04-20 14:01:18 -07001076
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001077 type = is_float ? TOK_FLOAT : TOK_NUMBER;
1078 }
H. Peter Anvinbda7a6e2008-06-21 10:23:17 -07001079 } else if (nasm_isspace(*p)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001080 type = TOK_WHITESPACE;
Cyrill Gorcunovf66ac7d2009-10-12 20:41:13 +04001081 p = nasm_skip_spaces(p);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001082 /*
1083 * Whitespace just before end-of-line is discarded by
1084 * pretending it's a comment; whitespace just before a
1085 * comment gets lumped into the comment.
1086 */
1087 if (!*p || *p == ';') {
1088 type = TOK_COMMENT;
1089 while (*p)
1090 p++;
1091 }
1092 } else if (*p == ';') {
1093 type = TOK_COMMENT;
1094 while (*p)
1095 p++;
1096 } else {
1097 /*
1098 * Anything else is an operator of some kind. We check
1099 * for all the double-character operators (>>, <<, //,
1100 * %%, <=, >=, ==, !=, <>, &&, ||, ^^), but anything
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001101 * else is a single-character operator.
H. Peter Anvine2c80182005-01-15 22:15:51 +00001102 */
1103 type = TOK_OTHER;
1104 if ((p[0] == '>' && p[1] == '>') ||
1105 (p[0] == '<' && p[1] == '<') ||
1106 (p[0] == '/' && p[1] == '/') ||
1107 (p[0] == '<' && p[1] == '=') ||
1108 (p[0] == '>' && p[1] == '=') ||
1109 (p[0] == '=' && p[1] == '=') ||
1110 (p[0] == '!' && p[1] == '=') ||
1111 (p[0] == '<' && p[1] == '>') ||
1112 (p[0] == '&' && p[1] == '&') ||
1113 (p[0] == '|' && p[1] == '|') ||
1114 (p[0] == '^' && p[1] == '^')) {
1115 p++;
1116 }
1117 p++;
1118 }
Nickolay Yurchenkof3b3ce22003-09-21 20:38:43 +00001119
H. Peter Anvine2c80182005-01-15 22:15:51 +00001120 /* Handling unterminated string by UNV */
1121 /*if (type == -1)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001122 {
1123 *tail = t = new_Token(NULL, TOK_STRING, line, p-line+1);
1124 t->text[p-line] = *line;
1125 tail = &t->next;
1126 }
1127 else */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001128 if (type != TOK_COMMENT) {
1129 *tail = t = new_Token(NULL, type, line, p - line);
1130 tail = &t->next;
1131 }
1132 line = p;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001133 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001134 return list;
1135}
1136
H. Peter Anvince616072002-04-30 21:02:23 +00001137/*
1138 * this function allocates a new managed block of memory and
H. Peter Anvin70653092007-10-19 14:42:29 -07001139 * returns a pointer to the block. The managed blocks are
H. Peter Anvince616072002-04-30 21:02:23 +00001140 * deleted only all at once by the delete_Blocks function.
1141 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001142static void *new_Block(size_t size)
H. Peter Anvince616072002-04-30 21:02:23 +00001143{
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001144 Blocks *b = &blocks;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001145
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001146 /* first, get to the end of the linked list */
1147 while (b->next)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001148 b = b->next;
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001149 /* now allocate the requested chunk */
1150 b->chunk = nasm_malloc(size);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001151
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001152 /* now allocate a new block for the next request */
Cyrill Gorcunovc31767c2014-06-28 02:22:17 +04001153 b->next = nasm_zalloc(sizeof(Blocks));
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001154 return b->chunk;
H. Peter Anvince616072002-04-30 21:02:23 +00001155}
1156
1157/*
1158 * this function deletes all managed blocks of memory
1159 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001160static void delete_Blocks(void)
H. Peter Anvince616072002-04-30 21:02:23 +00001161{
H. Peter Anvine2c80182005-01-15 22:15:51 +00001162 Blocks *a, *b = &blocks;
H. Peter Anvince616072002-04-30 21:02:23 +00001163
H. Peter Anvin70653092007-10-19 14:42:29 -07001164 /*
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001165 * keep in mind that the first block, pointed to by blocks
H. Peter Anvin70653092007-10-19 14:42:29 -07001166 * is a static and not dynamically allocated, so we don't
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001167 * free it.
1168 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001169 while (b) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001170 if (b->chunk)
1171 nasm_free(b->chunk);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001172 a = b;
1173 b = b->next;
1174 if (a != &blocks)
1175 nasm_free(a);
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001176 }
Cyrill Gorcunovdae24d72014-06-28 10:17:39 +04001177 memset(&blocks, 0, sizeof(blocks));
H. Peter Anvine2c80182005-01-15 22:15:51 +00001178}
H. Peter Anvin734b1882002-04-30 21:01:08 +00001179
1180/*
H. Peter Anvin70653092007-10-19 14:42:29 -07001181 * this function creates a new Token and passes a pointer to it
H. Peter Anvin734b1882002-04-30 21:01:08 +00001182 * back to the caller. It sets the type and text elements, and
H. Peter Anvinf26e0972008-07-01 21:26:27 -07001183 * also the a.mac and next elements to NULL.
H. Peter Anvin734b1882002-04-30 21:01:08 +00001184 */
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07001185static Token *new_Token(Token * next, enum pp_token_type type,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001186 const char *text, int txtlen)
H. Peter Anvin734b1882002-04-30 21:01:08 +00001187{
1188 Token *t;
1189 int i;
1190
H. Peter Anvin89cee572009-07-15 09:16:54 -04001191 if (!freeTokens) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001192 freeTokens = (Token *) new_Block(TOKEN_BLOCKSIZE * sizeof(Token));
1193 for (i = 0; i < TOKEN_BLOCKSIZE - 1; i++)
1194 freeTokens[i].next = &freeTokens[i + 1];
1195 freeTokens[i].next = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001196 }
1197 t = freeTokens;
1198 freeTokens = t->next;
1199 t->next = next;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07001200 t->a.mac = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001201 t->type = type;
H. Peter Anvin89cee572009-07-15 09:16:54 -04001202 if (type == TOK_WHITESPACE || !text) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001203 t->text = NULL;
1204 } else {
1205 if (txtlen == 0)
1206 txtlen = strlen(text);
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07001207 t->text = nasm_malloc(txtlen+1);
1208 memcpy(t->text, text, txtlen);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001209 t->text[txtlen] = '\0';
H. Peter Anvin734b1882002-04-30 21:01:08 +00001210 }
1211 return t;
1212}
1213
H. Peter Anvine2c80182005-01-15 22:15:51 +00001214static Token *delete_Token(Token * t)
H. Peter Anvin734b1882002-04-30 21:01:08 +00001215{
1216 Token *next = t->next;
1217 nasm_free(t->text);
H. Peter Anvin788e6c12002-04-30 21:02:01 +00001218 t->next = freeTokens;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001219 freeTokens = t;
1220 return next;
1221}
1222
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001223/*
1224 * Convert a line of tokens back into text.
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001225 * If expand_locals is not zero, identifiers of the form "%$*xxx"
1226 * will be transformed into ..@ctxnum.xxx
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001227 */
H. Peter Anvin9e200162008-06-04 17:23:14 -07001228static char *detoken(Token * tlist, bool expand_locals)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001229{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001230 Token *t;
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001231 char *line, *p;
H. Peter Anvinb4daadc2008-01-21 16:31:57 -08001232 const char *q;
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001233 int len = 0;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001234
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001235 list_for_each(t, tlist) {
Cyrill Gorcunov9b7ee092017-10-22 21:42:59 +03001236 if (t->type == TOK_PREPROC_ID && t->text &&
1237 t->text[0] && t->text[1] == '!') {
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001238 char *v;
1239 char *q = t->text;
H. Peter Anvin077fb932010-07-20 14:56:30 -07001240
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001241 v = t->text + 2;
H. Peter Anvin53e2e4c2018-11-28 15:01:40 -08001242 if (nasm_isquote(*v)) {
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001243 size_t len = nasm_unquote(v, NULL);
1244 size_t clen = strlen(v);
H. Peter Anvin077fb932010-07-20 14:56:30 -07001245
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001246 if (len != clen) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03001247 nasm_nonfatalf(ERR_PASS1, "NUL character in %%! string");
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001248 v = NULL;
1249 }
1250 }
H. Peter Anvin077fb932010-07-20 14:56:30 -07001251
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001252 if (v) {
1253 char *p = getenv(v);
1254 if (!p) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03001255 nasm_nonfatalf(ERR_PASS1, "nonexistent environment variable `%s'", v);
Cyrill Gorcunovbbb7a1a2016-06-19 12:15:24 +03001256 /*
1257 * FIXME We better should investigate if accessing
1258 * ->text[1] without ->text[0] is safe enough.
1259 */
1260 t->text = nasm_zalloc(2);
1261 } else
1262 t->text = nasm_strdup(p);
Cyrill Gorcunov75004872017-07-26 01:21:16 +03001263 nasm_free(q);
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001264 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001265 }
H. Peter Anvin077fb932010-07-20 14:56:30 -07001266
H. Peter Anvine2c80182005-01-15 22:15:51 +00001267 /* Expand local macros here and not during preprocessing */
1268 if (expand_locals &&
1269 t->type == TOK_PREPROC_ID && t->text &&
1270 t->text[0] == '%' && t->text[1] == '$') {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001271 const char *q;
1272 char *p;
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04001273 Context *ctx = get_ctx(t->text, &q);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001274 if (ctx) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001275 char buffer[40];
Keith Kanios93f2e9a2007-04-14 00:10:59 +00001276 snprintf(buffer, sizeof(buffer), "..@%"PRIu32".", ctx->number);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001277 p = nasm_strcat(buffer, q);
1278 nasm_free(t->text);
1279 t->text = p;
1280 }
1281 }
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001282 if (t->type == TOK_WHITESPACE)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001283 len++;
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001284 else if (t->text)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001285 len += strlen(t->text);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001286 }
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001287
H. Peter Anvin734b1882002-04-30 21:01:08 +00001288 p = line = nasm_malloc(len + 1);
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001289
1290 list_for_each(t, tlist) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001291 if (t->type == TOK_WHITESPACE) {
H. Peter Anvinb4daadc2008-01-21 16:31:57 -08001292 *p++ = ' ';
H. Peter Anvine2c80182005-01-15 22:15:51 +00001293 } else if (t->text) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001294 q = t->text;
1295 while (*q)
1296 *p++ = *q++;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001297 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001298 }
1299 *p = '\0';
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001300
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001301 return line;
1302}
1303
1304/*
H. Peter Anvin76690a12002-04-30 20:52:49 +00001305 * A scanner, suitable for use by the expression evaluator, which
1306 * operates on a line of Tokens. Expects a pointer to a pointer to
1307 * the first token in the line to be passed in as its private_data
1308 * field.
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001309 *
1310 * FIX: This really needs to be unified with stdscan.
H. Peter Anvin76690a12002-04-30 20:52:49 +00001311 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001312static int ppscan(void *private_data, struct tokenval *tokval)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001313{
H. Peter Anvin76690a12002-04-30 20:52:49 +00001314 Token **tlineptr = private_data;
1315 Token *tline;
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001316 char ourcopy[MAX_KEYWORD+1], *p, *r, *s;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001317
H. Peter Anvine2c80182005-01-15 22:15:51 +00001318 do {
1319 tline = *tlineptr;
1320 *tlineptr = tline ? tline->next : NULL;
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04001321 } while (tline && (tline->type == TOK_WHITESPACE ||
1322 tline->type == TOK_COMMENT));
H. Peter Anvin76690a12002-04-30 20:52:49 +00001323
1324 if (!tline)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001325 return tokval->t_type = TOKEN_EOS;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001326
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001327 tokval->t_charptr = tline->text;
1328
H. Peter Anvin76690a12002-04-30 20:52:49 +00001329 if (tline->text[0] == '$' && !tline->text[1])
H. Peter Anvine2c80182005-01-15 22:15:51 +00001330 return tokval->t_type = TOKEN_HERE;
H. Peter Anvin7cf897e2002-05-30 21:30:33 +00001331 if (tline->text[0] == '$' && tline->text[1] == '$' && !tline->text[2])
H. Peter Anvine2c80182005-01-15 22:15:51 +00001332 return tokval->t_type = TOKEN_BASE;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001333
H. Peter Anvine2c80182005-01-15 22:15:51 +00001334 if (tline->type == TOK_ID) {
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001335 p = tokval->t_charptr = tline->text;
1336 if (p[0] == '$') {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001337 tokval->t_charptr++;
1338 return tokval->t_type = TOKEN_ID;
1339 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00001340
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001341 for (r = p, s = ourcopy; *r; r++) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001342 if (r >= p+MAX_KEYWORD)
1343 return tokval->t_type = TOKEN_ID; /* Not a keyword */
H. Peter Anvinac8f8fc2008-06-11 15:49:41 -07001344 *s++ = nasm_tolower(*r);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001345 }
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001346 *s = '\0';
1347 /* right, so we have an identifier sitting in temp storage. now,
1348 * is it actually a register or instruction name, or what? */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001349 return nasm_token_hash(ourcopy, tokval);
H. Peter Anvin76690a12002-04-30 20:52:49 +00001350 }
1351
H. Peter Anvine2c80182005-01-15 22:15:51 +00001352 if (tline->type == TOK_NUMBER) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001353 bool rn_error;
1354 tokval->t_integer = readnum(tline->text, &rn_error);
1355 tokval->t_charptr = tline->text;
1356 if (rn_error)
1357 return tokval->t_type = TOKEN_ERRNUM;
1358 else
1359 return tokval->t_type = TOKEN_NUM;
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001360 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00001361
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001362 if (tline->type == TOK_FLOAT) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001363 return tokval->t_type = TOKEN_FLOAT;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001364 }
1365
H. Peter Anvine2c80182005-01-15 22:15:51 +00001366 if (tline->type == TOK_STRING) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001367 char bq, *ep;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001368
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001369 bq = tline->text[0];
H. Peter Anvin11627042008-06-09 20:45:19 -07001370 tokval->t_charptr = tline->text;
1371 tokval->t_inttwo = nasm_unquote(tline->text, &ep);
H. Peter Anvind2456592008-06-19 15:04:18 -07001372
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001373 if (ep[0] != bq || ep[1] != '\0')
1374 return tokval->t_type = TOKEN_ERRSTR;
1375 else
1376 return tokval->t_type = TOKEN_STR;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001377 }
1378
H. Peter Anvine2c80182005-01-15 22:15:51 +00001379 if (tline->type == TOK_OTHER) {
1380 if (!strcmp(tline->text, "<<"))
1381 return tokval->t_type = TOKEN_SHL;
1382 if (!strcmp(tline->text, ">>"))
1383 return tokval->t_type = TOKEN_SHR;
1384 if (!strcmp(tline->text, "//"))
1385 return tokval->t_type = TOKEN_SDIV;
1386 if (!strcmp(tline->text, "%%"))
1387 return tokval->t_type = TOKEN_SMOD;
1388 if (!strcmp(tline->text, "=="))
1389 return tokval->t_type = TOKEN_EQ;
1390 if (!strcmp(tline->text, "<>"))
1391 return tokval->t_type = TOKEN_NE;
1392 if (!strcmp(tline->text, "!="))
1393 return tokval->t_type = TOKEN_NE;
1394 if (!strcmp(tline->text, "<="))
1395 return tokval->t_type = TOKEN_LE;
1396 if (!strcmp(tline->text, ">="))
1397 return tokval->t_type = TOKEN_GE;
1398 if (!strcmp(tline->text, "&&"))
1399 return tokval->t_type = TOKEN_DBL_AND;
1400 if (!strcmp(tline->text, "^^"))
1401 return tokval->t_type = TOKEN_DBL_XOR;
1402 if (!strcmp(tline->text, "||"))
1403 return tokval->t_type = TOKEN_DBL_OR;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001404 }
1405
1406 /*
1407 * We have no other options: just return the first character of
1408 * the token text.
1409 */
1410 return tokval->t_type = tline->text[0];
1411}
1412
1413/*
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001414 * Compare a string to the name of an existing macro; this is a
1415 * simple wrapper which calls either strcmp or nasm_stricmp
1416 * depending on the value of the `casesense' parameter.
1417 */
H. Peter Anvin4db5a162007-10-11 13:42:09 -07001418static int mstrcmp(const char *p, const char *q, bool casesense)
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001419{
H. Peter Anvin734b1882002-04-30 21:01:08 +00001420 return casesense ? strcmp(p, q) : nasm_stricmp(p, q);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001421}
1422
1423/*
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07001424 * Compare a string to the name of an existing macro; this is a
1425 * simple wrapper which calls either strcmp or nasm_stricmp
1426 * depending on the value of the `casesense' parameter.
1427 */
1428static int mmemcmp(const char *p, const char *q, size_t l, bool casesense)
1429{
1430 return casesense ? memcmp(p, q, l) : nasm_memicmp(p, q, l);
1431}
1432
1433/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001434 * Return the Context structure associated with a %$ token. Return
1435 * NULL, having _already_ reported an error condition, if the
1436 * context stack isn't deep enough for the supplied number of $
1437 * signs.
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001438 *
1439 * If "namep" is non-NULL, set it to the pointer to the macro name
1440 * tail, i.e. the part beyond %$...
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001441 */
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04001442static Context *get_ctx(const char *name, const char **namep)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001443{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001444 Context *ctx;
1445 int i;
1446
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001447 if (namep)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001448 *namep = name;
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001449
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001450 if (!name || name[0] != '%' || name[1] != '$')
H. Peter Anvine2c80182005-01-15 22:15:51 +00001451 return NULL;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001452
H. Peter Anvine2c80182005-01-15 22:15:51 +00001453 if (!cstk) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03001454 nasm_nonfatal("`%s': context stack is empty", name);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001455 return NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001456 }
1457
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001458 name += 2;
1459 ctx = cstk;
1460 i = 0;
1461 while (ctx && *name == '$') {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001462 name++;
1463 i++;
1464 ctx = ctx->next;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001465 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001466 if (!ctx) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03001467 nasm_nonfatal("`%s': context stack is only"
1468 " %d level%s deep", name, i, (i == 1 ? "" : "s"));
H. Peter Anvine2c80182005-01-15 22:15:51 +00001469 return NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001470 }
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001471
1472 if (namep)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001473 *namep = name;
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001474
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04001475 return ctx;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001476}
1477
1478/*
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001479 * Open an include file. This routine must always return a valid
1480 * file pointer if it returns - it's responsible for throwing an
1481 * ERR_FATAL and bombing out completely if not. It should also try
1482 * the include path one by one until it finds the file or reaches
1483 * the end of the path.
H. Peter Anvind81a2352016-09-21 14:03:18 -07001484 *
1485 * Note: for INC_PROBE the function returns NULL at all times;
1486 * instead look for the
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001487 */
H. Peter Anvind81a2352016-09-21 14:03:18 -07001488enum incopen_mode {
1489 INC_NEEDED, /* File must exist */
1490 INC_OPTIONAL, /* Missing is OK */
1491 INC_PROBE /* Only an existence probe */
1492};
1493
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001494/* This is conducts a full pathname search */
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07001495static FILE *inc_fopen_search(const char *file, char **slpath,
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001496 enum incopen_mode omode, enum file_flags fmode)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001497{
H. Peter Anvin (Intel)64471092018-12-11 13:06:14 -08001498 const struct strlist_entry *ip = strlist_head(ipath_list);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001499 FILE *fp;
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07001500 const char *prefix = "";
night199ukfdb1a1b2018-10-18 23:19:47 +02001501 char *sp;
H. Peter Anvind81a2352016-09-21 14:03:18 -07001502 bool found;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001503
H. Peter Anvine2c80182005-01-15 22:15:51 +00001504 while (1) {
night199ukfdb1a1b2018-10-18 23:19:47 +02001505 sp = nasm_catfile(prefix, file);
H. Peter Anvind81a2352016-09-21 14:03:18 -07001506 if (omode == INC_PROBE) {
1507 fp = NULL;
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07001508 found = nasm_file_exists(sp);
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001509 } else {
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07001510 fp = nasm_open_read(sp, fmode);
H. Peter Anvind81a2352016-09-21 14:03:18 -07001511 found = (fp != NULL);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001512 }
H. Peter Anvind81a2352016-09-21 14:03:18 -07001513 if (found) {
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07001514 *slpath = sp;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001515 return fp;
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001516 }
Jim Kukunas65a8afc2016-06-13 16:00:42 -04001517
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07001518 nasm_free(sp);
Jim Kukunas65a8afc2016-06-13 16:00:42 -04001519
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07001520 if (!ip) {
1521 *slpath = NULL;
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001522 return NULL;
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07001523 }
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001524
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07001525 prefix = ip->str;
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001526 ip = ip->next;
1527 }
1528}
1529
1530/*
1531 * Open a file, or test for the presence of one (depending on omode),
1532 * considering the include path.
1533 */
1534static FILE *inc_fopen(const char *file,
Cyrill Gorcunovb7bb5ac2018-11-11 21:33:52 +03001535 struct strlist *dhead,
H. Peter Anvinccad6f92016-10-04 00:34:35 -07001536 const char **found_path,
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001537 enum incopen_mode omode,
1538 enum file_flags fmode)
1539{
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001540 struct hash_insert hi;
1541 void **hp;
1542 char *path;
1543 FILE *fp = NULL;
1544
1545 hp = hash_find(&FileHash, file, &hi);
1546 if (hp) {
1547 path = *hp;
Martin Storsjöf283c8f2017-08-13 17:28:46 +03001548 if (path || omode != INC_NEEDED) {
Cyrill Gorcunovb7bb5ac2018-11-11 21:33:52 +03001549 strlist_add(dhead, path ? path : file);
Martin Storsjöf283c8f2017-08-13 17:28:46 +03001550 }
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001551 } else {
1552 /* Need to do the actual path search */
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07001553 fp = inc_fopen_search(file, &path, omode, fmode);
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001554
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07001555 /* Positive or negative result */
1556 hash_add(&hi, nasm_strdup(file), path);
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001557
H. Peter Anvin9924d1e2016-10-04 00:59:39 -07001558 /*
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07001559 * Add file to dependency path.
H. Peter Anvin9924d1e2016-10-04 00:59:39 -07001560 */
1561 if (path || omode != INC_NEEDED)
Cyrill Gorcunovb7bb5ac2018-11-11 21:33:52 +03001562 strlist_add(dhead, file);
H. Peter Anvineba20a72002-04-30 20:53:55 +00001563 }
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001564
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001565 if (!path) {
1566 if (omode == INC_NEEDED)
H. Peter Anvinc5136902018-06-15 18:20:17 -07001567 nasm_fatal("unable to open include file `%s'", file);
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07001568 } else {
1569 if (!fp && omode != INC_PROBE)
1570 fp = nasm_open_read(path, fmode);
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001571 }
1572
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001573 if (found_path)
H. Peter Anvinccad6f92016-10-04 00:34:35 -07001574 *found_path = path;
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001575
1576 return fp;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001577}
1578
1579/*
Fabian Giesen0bbc38d2016-04-28 13:48:14 -07001580 * Opens an include or input file. Public version, for use by modules
1581 * that get a file:lineno pair and need to look at the file again
1582 * (e.g. the CodeView debug backend). Returns NULL on failure.
1583 */
H. Peter Anvin3e83cec2016-05-25 04:28:46 -07001584FILE *pp_input_fopen(const char *filename, enum file_flags mode)
Fabian Giesen0bbc38d2016-04-28 13:48:14 -07001585{
H. Peter Anvin9924d1e2016-10-04 00:59:39 -07001586 return inc_fopen(filename, NULL, NULL, INC_OPTIONAL, mode);
Fabian Giesen0bbc38d2016-04-28 13:48:14 -07001587}
1588
1589/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001590 * Determine if we should warn on defining a single-line macro of
H. Peter Anvinef7468f2002-04-30 20:57:59 +00001591 * name `name', with `nparam' parameters. If nparam is 0 or -1, will
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001592 * return true if _any_ single-line macro of that name is defined.
1593 * Otherwise, will return true if a single-line macro with either
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001594 * `nparam' or no parameters is defined.
1595 *
1596 * If a macro with precisely the right number of parameters is
H. Peter Anvinef7468f2002-04-30 20:57:59 +00001597 * defined, or nparam is -1, the address of the definition structure
1598 * will be returned in `defn'; otherwise NULL will be returned. If `defn'
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001599 * is NULL, no action will be taken regarding its contents, and no
1600 * error will occur.
1601 *
1602 * Note that this is also called with nparam zero to resolve
1603 * `ifdef'.
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001604 *
1605 * If you already know which context macro belongs to, you can pass
1606 * the context pointer as first parameter; if you won't but name begins
1607 * with %$ the context will be automatically computed. If all_contexts
1608 * is true, macro will be searched in outer contexts as well.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001609 */
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07001610static bool
H. Peter Anvinb2a5fda2008-06-19 21:42:42 -07001611smacro_defined(Context * ctx, const char *name, int nparam, SMacro ** defn,
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07001612 bool nocase)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001613{
H. Peter Anvin166c2472008-05-28 12:28:58 -07001614 struct hash_table *smtbl;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001615 SMacro *m;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001616
H. Peter Anvin97a23472007-09-16 17:57:25 -07001617 if (ctx) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001618 smtbl = &ctx->localmac;
H. Peter Anvin97a23472007-09-16 17:57:25 -07001619 } else if (name[0] == '%' && name[1] == '$') {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001620 if (cstk)
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04001621 ctx = get_ctx(name, &name);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001622 if (!ctx)
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001623 return false; /* got to return _something_ */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001624 smtbl = &ctx->localmac;
H. Peter Anvin97a23472007-09-16 17:57:25 -07001625 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001626 smtbl = &smacros;
H. Peter Anvin97a23472007-09-16 17:57:25 -07001627 }
H. Peter Anvin166c2472008-05-28 12:28:58 -07001628 m = (SMacro *) hash_findix(smtbl, name);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001629
H. Peter Anvine2c80182005-01-15 22:15:51 +00001630 while (m) {
1631 if (!mstrcmp(m->name, name, m->casesense && nocase) &&
Charles Crayne192d5b52007-10-18 19:02:42 -07001632 (nparam <= 0 || m->nparam == 0 || nparam == (int) m->nparam)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001633 if (defn) {
Charles Crayne192d5b52007-10-18 19:02:42 -07001634 if (nparam == (int) m->nparam || nparam == -1)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001635 *defn = m;
1636 else
1637 *defn = NULL;
1638 }
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001639 return true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001640 }
1641 m = m->next;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001642 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001643
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001644 return false;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001645}
1646
Cyrill Gorcunov3079f792018-11-14 10:03:42 +03001647/* param should be a natural number [0; INT_MAX] */
1648static int read_param_count(const char *str)
1649{
1650 int result;
1651 bool err;
1652
1653 result = readnum(str, &err);
1654 if (result < 0 || result > INT_MAX) {
1655 result = 0;
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03001656 nasm_nonfatal("parameter count `%s' is out of bounds [%d; %d]",
1657 str, 0, INT_MAX);
1658 } else if (err)
1659 nasm_nonfatal("unable to parse parameter count `%s'", str);
Cyrill Gorcunov3079f792018-11-14 10:03:42 +03001660 return result;
1661}
1662
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001663/*
1664 * Count and mark off the parameters in a multi-line macro call.
1665 * This is called both from within the multi-line macro expansion
1666 * code, and also to mark off the default parameters when provided
1667 * in a %macro definition line.
1668 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001669static void count_mmac_params(Token * t, int *nparam, Token *** params)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001670{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001671 int paramsize, brace;
1672
1673 *nparam = paramsize = 0;
1674 *params = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001675 while (t) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001676 /* +1: we need space for the final NULL */
H. Peter Anvin91fb6f12008-09-01 10:56:33 -07001677 if (*nparam+1 >= paramsize) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001678 paramsize += PARAM_DELTA;
1679 *params = nasm_realloc(*params, sizeof(**params) * paramsize);
1680 }
1681 skip_white_(t);
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08001682 brace = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001683 if (tok_is_(t, "{"))
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08001684 brace++;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001685 (*params)[(*nparam)++] = t;
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08001686 if (brace) {
1687 while (brace && (t = t->next) != NULL) {
1688 if (tok_is_(t, "{"))
1689 brace++;
1690 else if (tok_is_(t, "}"))
1691 brace--;
1692 }
1693
1694 if (t) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001695 /*
1696 * Now we've found the closing brace, look further
1697 * for the comma.
1698 */
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08001699 t = t->next;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001700 skip_white_(t);
1701 if (tok_isnt_(t, ",")) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03001702 nasm_nonfatal("braces do not enclose all of macro parameter");
H. Peter Anvine2c80182005-01-15 22:15:51 +00001703 while (tok_isnt_(t, ","))
1704 t = t->next;
1705 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001706 }
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08001707 } else {
1708 while (tok_isnt_(t, ","))
1709 t = t->next;
1710 }
1711 if (t) { /* got a comma/brace */
1712 t = t->next; /* eat the comma */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001713 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001714 }
1715}
1716
1717/*
H. Peter Anvin76690a12002-04-30 20:52:49 +00001718 * Determine whether one of the various `if' conditions is true or
1719 * not.
1720 *
1721 * We must free the tline we get passed.
1722 */
H. Peter Anvin70055962007-10-11 00:05:31 -07001723static bool if_condition(Token * tline, enum preproc_token ct)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001724{
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001725 enum pp_conditional i = PP_COND(ct);
H. Peter Anvin70055962007-10-11 00:05:31 -07001726 bool j;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001727 Token *t, *tt, **tptr, *origline;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001728 struct tokenval tokval;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001729 expr *evalresult;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001730 enum pp_token_type needtype;
H. Peter Anvin077fb932010-07-20 14:56:30 -07001731 char *p;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001732
1733 origline = tline;
1734
H. Peter Anvine2c80182005-01-15 22:15:51 +00001735 switch (i) {
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001736 case PPC_IFCTX:
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001737 j = false; /* have we matched yet? */
Victor van den Elzen0e857f12008-07-23 13:21:29 +02001738 while (true) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001739 skip_white_(tline);
Victor van den Elzen0e857f12008-07-23 13:21:29 +02001740 if (!tline)
1741 break;
1742 if (tline->type != TOK_ID) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03001743 nasm_nonfatal("`%s' expects context identifiers",
1744 pp_directives[ct]);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001745 free_tlist(origline);
1746 return -1;
1747 }
Victor van den Elzen0e857f12008-07-23 13:21:29 +02001748 if (cstk && cstk->name && !nasm_stricmp(tline->text, cstk->name))
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001749 j = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001750 tline = tline->next;
1751 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001752 break;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001753
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001754 case PPC_IFDEF:
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001755 j = false; /* have we matched yet? */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001756 while (tline) {
1757 skip_white_(tline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001758 if (!tline || (tline->type != TOK_ID &&
1759 (tline->type != TOK_PREPROC_ID ||
1760 tline->text[1] != '$'))) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03001761 nasm_nonfatal("`%s' expects macro identifiers",
1762 pp_directives[ct]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001763 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001764 }
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07001765 if (smacro_defined(NULL, tline->text, 0, NULL, true))
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001766 j = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001767 tline = tline->next;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001768 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001769 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001770
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001771 case PPC_IFENV:
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001772 tline = expand_smacro(tline);
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001773 j = false; /* have we matched yet? */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001774 while (tline) {
1775 skip_white_(tline);
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001776 if (!tline || (tline->type != TOK_ID &&
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001777 tline->type != TOK_STRING &&
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001778 (tline->type != TOK_PREPROC_ID ||
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001779 tline->text[1] != '!'))) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03001780 nasm_nonfatal("`%s' expects environment variable names",
1781 pp_directives[ct]);
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001782 goto fail;
1783 }
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001784 p = tline->text;
1785 if (tline->type == TOK_PREPROC_ID)
1786 p += 2; /* Skip leading %! */
H. Peter Anvin53e2e4c2018-11-28 15:01:40 -08001787 if (nasm_isquote(*p))
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001788 nasm_unquote_cstr(p, ct);
1789 if (getenv(p))
1790 j = true;
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001791 tline = tline->next;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001792 }
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001793 break;
1794
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001795 case PPC_IFIDN:
1796 case PPC_IFIDNI:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001797 tline = expand_smacro(tline);
1798 t = tt = tline;
1799 while (tok_isnt_(tt, ","))
1800 tt = tt->next;
1801 if (!tt) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03001802 nasm_nonfatal("`%s' expects two comma-separated arguments",
1803 pp_directives[ct]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001804 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001805 }
1806 tt = tt->next;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001807 j = true; /* assume equality unless proved not */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001808 while ((t->type != TOK_OTHER || strcmp(t->text, ",")) && tt) {
1809 if (tt->type == TOK_OTHER && !strcmp(tt->text, ",")) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03001810 nasm_nonfatal("`%s': more than one comma on line",
1811 pp_directives[ct]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001812 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001813 }
1814 if (t->type == TOK_WHITESPACE) {
1815 t = t->next;
1816 continue;
1817 }
1818 if (tt->type == TOK_WHITESPACE) {
1819 tt = tt->next;
1820 continue;
1821 }
1822 if (tt->type != t->type) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001823 j = false; /* found mismatching tokens */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001824 break;
1825 }
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07001826 /* When comparing strings, need to unquote them first */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001827 if (t->type == TOK_STRING) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001828 size_t l1 = nasm_unquote(t->text, NULL);
1829 size_t l2 = nasm_unquote(tt->text, NULL);
H. Peter Anvind2456592008-06-19 15:04:18 -07001830
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001831 if (l1 != l2) {
1832 j = false;
1833 break;
1834 }
1835 if (mmemcmp(t->text, tt->text, l1, i == PPC_IFIDN)) {
1836 j = false;
1837 break;
1838 }
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07001839 } else if (mstrcmp(tt->text, t->text, i == PPC_IFIDN) != 0) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001840 j = false; /* found mismatching tokens */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001841 break;
1842 }
Nickolay Yurchenkof3b3ce22003-09-21 20:38:43 +00001843
H. Peter Anvine2c80182005-01-15 22:15:51 +00001844 t = t->next;
1845 tt = tt->next;
1846 }
1847 if ((t->type != TOK_OTHER || strcmp(t->text, ",")) || tt)
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001848 j = false; /* trailing gunk on one end or other */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001849 break;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001850
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001851 case PPC_IFMACRO:
H. Peter Anvin89cee572009-07-15 09:16:54 -04001852 {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001853 bool found = false;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001854 MMacro searching, *mmac;
H. Peter Anvin65747262002-05-07 00:10:05 +00001855
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001856 skip_white_(tline);
1857 tline = expand_id(tline);
1858 if (!tok_type_(tline, TOK_ID)) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03001859 nasm_nonfatal("`%s' expects a macro name", pp_directives[ct]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001860 goto fail;
1861 }
1862 searching.name = nasm_strdup(tline->text);
1863 searching.casesense = true;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001864 searching.plus = false;
1865 searching.nolist = false;
1866 searching.in_progress = 0;
1867 searching.max_depth = 0;
1868 searching.rep_nest = NULL;
1869 searching.nparam_min = 0;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001870 searching.nparam_max = INT_MAX;
1871 tline = expand_smacro(tline->next);
1872 skip_white_(tline);
1873 if (!tline) {
1874 } else if (!tok_type_(tline, TOK_NUMBER)) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03001875 nasm_nonfatal("`%s' expects a parameter count or nothing",
1876 pp_directives[ct]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001877 } else {
1878 searching.nparam_min = searching.nparam_max =
Cyrill Gorcunov3079f792018-11-14 10:03:42 +03001879 read_param_count(tline->text);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001880 }
1881 if (tline && tok_is_(tline->next, "-")) {
1882 tline = tline->next->next;
1883 if (tok_is_(tline, "*"))
1884 searching.nparam_max = INT_MAX;
1885 else if (!tok_type_(tline, TOK_NUMBER))
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03001886 nasm_nonfatal("`%s' expects a parameter count after `-'",
1887 pp_directives[ct]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001888 else {
Cyrill Gorcunov3079f792018-11-14 10:03:42 +03001889 searching.nparam_max = read_param_count(tline->text);
Cyrill Gorcunovc9244ea2017-10-22 15:25:48 +03001890 if (searching.nparam_min > searching.nparam_max) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03001891 nasm_nonfatal("minimum parameter count exceeds maximum");
Cyrill Gorcunovc9244ea2017-10-22 15:25:48 +03001892 searching.nparam_max = searching.nparam_min;
1893 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001894 }
1895 }
1896 if (tline && tok_is_(tline->next, "+")) {
1897 tline = tline->next;
1898 searching.plus = true;
1899 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001900 mmac = (MMacro *) hash_findix(&mmacros, searching.name);
1901 while (mmac) {
1902 if (!strcmp(mmac->name, searching.name) &&
1903 (mmac->nparam_min <= searching.nparam_max
1904 || searching.plus)
1905 && (searching.nparam_min <= mmac->nparam_max
1906 || mmac->plus)) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001907 found = true;
1908 break;
1909 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001910 mmac = mmac->next;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001911 }
1912 if (tline && tline->next)
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08001913 nasm_warn(WARN_OTHER, "trailing garbage after %%ifmacro ignored");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001914 nasm_free(searching.name);
1915 j = found;
1916 break;
H. Peter Anvin89cee572009-07-15 09:16:54 -04001917 }
H. Peter Anvin65747262002-05-07 00:10:05 +00001918
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001919 case PPC_IFID:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001920 needtype = TOK_ID;
1921 goto iftype;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001922 case PPC_IFNUM:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001923 needtype = TOK_NUMBER;
1924 goto iftype;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001925 case PPC_IFSTR:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001926 needtype = TOK_STRING;
1927 goto iftype;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001928
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001929iftype:
1930 t = tline = expand_smacro(tline);
H. Peter Anvind85d2502008-05-04 17:53:31 -07001931
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001932 while (tok_type_(t, TOK_WHITESPACE) ||
1933 (needtype == TOK_NUMBER &&
1934 tok_type_(t, TOK_OTHER) &&
1935 (t->text[0] == '-' || t->text[0] == '+') &&
1936 !t->text[1]))
1937 t = t->next;
H. Peter Anvind85d2502008-05-04 17:53:31 -07001938
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001939 j = tok_type_(t, needtype);
1940 break;
H. Peter Anvincbf768d2008-02-16 16:41:25 -08001941
1942 case PPC_IFTOKEN:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001943 t = tline = expand_smacro(tline);
1944 while (tok_type_(t, TOK_WHITESPACE))
1945 t = t->next;
H. Peter Anvincbf768d2008-02-16 16:41:25 -08001946
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001947 j = false;
1948 if (t) {
1949 t = t->next; /* Skip the actual token */
1950 while (tok_type_(t, TOK_WHITESPACE))
1951 t = t->next;
1952 j = !t; /* Should be nothing left */
1953 }
1954 break;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001955
H. Peter Anvin134b9462008-02-16 17:01:40 -08001956 case PPC_IFEMPTY:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001957 t = tline = expand_smacro(tline);
1958 while (tok_type_(t, TOK_WHITESPACE))
1959 t = t->next;
H. Peter Anvin134b9462008-02-16 17:01:40 -08001960
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001961 j = !t; /* Should be empty */
1962 break;
H. Peter Anvin134b9462008-02-16 17:01:40 -08001963
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001964 case PPC_IF:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001965 t = tline = expand_smacro(tline);
1966 tptr = &t;
1967 tokval.t_type = TOKEN_INVALID;
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08001968 evalresult = evaluate(ppscan, tptr, &tokval, NULL, true, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001969 if (!evalresult)
1970 return -1;
1971 if (tokval.t_type)
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08001972 nasm_warn(WARN_OTHER, "trailing garbage after expression ignored");
H. Peter Anvine2c80182005-01-15 22:15:51 +00001973 if (!is_simple(evalresult)) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03001974 nasm_nonfatal("non-constant value given to `%s'",
1975 pp_directives[ct]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001976 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001977 }
Chuck Crayne60ae75d2007-05-02 01:59:16 +00001978 j = reloc_value(evalresult) != 0;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001979 break;
H. Peter Anvin95e28822007-09-12 04:20:08 +00001980
H. Peter Anvine2c80182005-01-15 22:15:51 +00001981 default:
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03001982 nasm_fatal("preprocessor directive `%s' not yet implemented",
1983 pp_directives[ct]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001984 goto fail;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001985 }
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001986
1987 free_tlist(origline);
1988 return j ^ PP_NEGATIVE(ct);
H. Peter Anvin70653092007-10-19 14:42:29 -07001989
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001990fail:
1991 free_tlist(origline);
1992 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001993}
1994
1995/*
H. Peter Anvin4db5a162007-10-11 13:42:09 -07001996 * Common code for defining an smacro
1997 */
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001998static bool define_smacro(Context *ctx, const char *mname, bool casesense,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001999 int nparam, Token *expansion)
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002000{
2001 SMacro *smac, **smhead;
H. Peter Anvin166c2472008-05-28 12:28:58 -07002002 struct hash_table *smtbl;
H. Peter Anvin70653092007-10-19 14:42:29 -07002003
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002004 if (smacro_defined(ctx, mname, nparam, &smac, casesense)) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002005 if (!smac) {
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08002006 nasm_warn(WARN_OTHER, "single-line macro `%s' defined both with and"
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002007 " without parameters", mname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002008 /*
2009 * Some instances of the old code considered this a failure,
2010 * some others didn't. What is the right thing to do here?
2011 */
2012 free_tlist(expansion);
2013 return false; /* Failure */
2014 } else {
2015 /*
2016 * We're redefining, so we have to take over an
2017 * existing SMacro structure. This means freeing
2018 * what was already in it.
2019 */
2020 nasm_free(smac->name);
2021 free_tlist(smac->expansion);
2022 }
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002023 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002024 smtbl = ctx ? &ctx->localmac : &smacros;
2025 smhead = (SMacro **) hash_findi_add(smtbl, mname);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002026 smac = nasm_malloc(sizeof(SMacro));
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002027 smac->next = *smhead;
2028 *smhead = smac;
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002029 }
2030 smac->name = nasm_strdup(mname);
2031 smac->casesense = casesense;
2032 smac->nparam = nparam;
2033 smac->expansion = expansion;
2034 smac->in_progress = false;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002035 return true; /* Success */
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002036}
2037
2038/*
2039 * Undefine an smacro
2040 */
2041static void undef_smacro(Context *ctx, const char *mname)
2042{
2043 SMacro **smhead, *s, **sp;
H. Peter Anvin166c2472008-05-28 12:28:58 -07002044 struct hash_table *smtbl;
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002045
H. Peter Anvin166c2472008-05-28 12:28:58 -07002046 smtbl = ctx ? &ctx->localmac : &smacros;
2047 smhead = (SMacro **)hash_findi(smtbl, mname, NULL);
H. Peter Anvin70653092007-10-19 14:42:29 -07002048
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002049 if (smhead) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002050 /*
2051 * We now have a macro name... go hunt for it.
2052 */
2053 sp = smhead;
2054 while ((s = *sp) != NULL) {
2055 if (!mstrcmp(s->name, mname, s->casesense)) {
2056 *sp = s->next;
2057 nasm_free(s->name);
2058 free_tlist(s->expansion);
2059 nasm_free(s);
2060 } else {
2061 sp = &s->next;
2062 }
2063 }
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002064 }
2065}
2066
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002067/*
H. Peter Anvina26433d2008-07-16 14:40:01 -07002068 * Parse a mmacro specification.
2069 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002070static bool parse_mmacro_spec(Token *tline, MMacro *def, const char *directive)
H. Peter Anvina26433d2008-07-16 14:40:01 -07002071{
H. Peter Anvina26433d2008-07-16 14:40:01 -07002072 tline = tline->next;
2073 skip_white_(tline);
2074 tline = expand_id(tline);
2075 if (!tok_type_(tline, TOK_ID)) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002076 nasm_nonfatal("`%s' expects a macro name", directive);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002077 return false;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002078 }
Victor van den Elzenb916ede2008-07-23 15:14:22 +02002079
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002080 def->prev = NULL;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002081 def->name = nasm_strdup(tline->text);
2082 def->plus = false;
2083 def->nolist = false;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002084 def->in_progress = 0;
2085 def->rep_nest = NULL;
Victor van den Elzenb916ede2008-07-23 15:14:22 +02002086 def->nparam_min = 0;
2087 def->nparam_max = 0;
2088
H. Peter Anvina26433d2008-07-16 14:40:01 -07002089 tline = expand_smacro(tline->next);
2090 skip_white_(tline);
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002091 if (!tok_type_(tline, TOK_NUMBER))
2092 nasm_nonfatal("`%s' expects a parameter count", directive);
2093 else
Cyrill Gorcunov3079f792018-11-14 10:03:42 +03002094 def->nparam_min = def->nparam_max = read_param_count(tline->text);
H. Peter Anvina26433d2008-07-16 14:40:01 -07002095 if (tline && tok_is_(tline->next, "-")) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002096 tline = tline->next->next;
2097 if (tok_is_(tline, "*")) {
2098 def->nparam_max = INT_MAX;
2099 } else if (!tok_type_(tline, TOK_NUMBER)) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002100 nasm_nonfatal("`%s' expects a parameter count after `-'", directive);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002101 } else {
Cyrill Gorcunov3079f792018-11-14 10:03:42 +03002102 def->nparam_max = read_param_count(tline->text);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002103 if (def->nparam_min > def->nparam_max) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002104 nasm_nonfatal("minimum parameter count exceeds maximum");
Cyrill Gorcunovc9244ea2017-10-22 15:25:48 +03002105 def->nparam_max = def->nparam_min;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002106 }
2107 }
H. Peter Anvina26433d2008-07-16 14:40:01 -07002108 }
2109 if (tline && tok_is_(tline->next, "+")) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002110 tline = tline->next;
2111 def->plus = true;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002112 }
2113 if (tline && tok_type_(tline->next, TOK_ID) &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002114 !nasm_stricmp(tline->next->text, ".nolist")) {
2115 tline = tline->next;
2116 def->nolist = true;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002117 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002118
H. Peter Anvina26433d2008-07-16 14:40:01 -07002119 /*
2120 * Handle default parameters.
2121 */
2122 if (tline && tline->next) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002123 def->dlist = tline->next;
2124 tline->next = NULL;
2125 count_mmac_params(def->dlist, &def->ndefs, &def->defaults);
H. Peter Anvina26433d2008-07-16 14:40:01 -07002126 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002127 def->dlist = NULL;
2128 def->defaults = NULL;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002129 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002130 def->expansion = NULL;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002131
H. Peter Anvin89cee572009-07-15 09:16:54 -04002132 if (def->defaults && def->ndefs > def->nparam_max - def->nparam_min &&
H. Peter Anvin (Intel)723ab482018-12-13 21:53:31 -08002133 !def->plus) {
2134 /*
2135 *!macro-defaults [on] macros with more default than optional parameters
2136 *! warns when a macro has more default parameters than optional parameters.
2137 *! See \k{mlmacdef} for why might want to disable this warning.
2138 */
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08002139 nasm_warn(WARN_MACRO_DEFAULTS,
H. Peter Anvin (Intel)723ab482018-12-13 21:53:31 -08002140 "too many default macro parameters in macro `%s'", def->name);
2141 }
Victor van den Elzenb916ede2008-07-23 15:14:22 +02002142
H. Peter Anvina26433d2008-07-16 14:40:01 -07002143 return true;
2144}
2145
2146
2147/*
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002148 * Decode a size directive
2149 */
2150static int parse_size(const char *str) {
2151 static const char *size_names[] =
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002152 { "byte", "dword", "oword", "qword", "tword", "word", "yword" };
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002153 static const int sizes[] =
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002154 { 0, 1, 4, 16, 8, 10, 2, 32 };
Cyrill Gorcunovc713b5f2018-09-29 14:30:14 +03002155 return str ? sizes[bsii(str, size_names, ARRAY_SIZE(size_names))+1] : 0;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002156}
2157
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07002158/*
2159 * Process a preprocessor %pragma directive. Currently there are none.
2160 * Gets passed the token list starting with the "preproc" token from
2161 * "%pragma preproc".
2162 */
2163static void do_pragma_preproc(Token *tline)
2164{
2165 /* Skip to the real stuff */
2166 tline = tline->next;
2167 skip_white_(tline);
2168 if (!tline)
2169 return;
2170
2171 (void)tline; /* Nothing else to do at present */
2172}
2173
Ed Beroset3ab3f412002-06-11 03:31:49 +00002174/**
2175 * find and process preprocessor directive in passed line
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002176 * Find out if a line contains a preprocessor directive, and deal
2177 * with it if so.
H. Peter Anvin70653092007-10-19 14:42:29 -07002178 *
Ed Beroset3ab3f412002-06-11 03:31:49 +00002179 * If a directive _is_ found, it is the responsibility of this routine
2180 * (and not the caller) to free_tlist() the line.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002181 *
Ed Beroset3ab3f412002-06-11 03:31:49 +00002182 * @param tline a pointer to the current tokeninzed line linked list
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07002183 * @param output if this directive generated output
Ed Beroset3ab3f412002-06-11 03:31:49 +00002184 * @return DIRECTIVE_FOUND or NO_DIRECTIVE_FOUND
H. Peter Anvin70653092007-10-19 14:42:29 -07002185 *
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002186 */
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07002187static int do_directive(Token *tline, char **output)
H. Peter Anvineba20a72002-04-30 20:53:55 +00002188{
H. Peter Anvin4169a472007-09-12 01:29:43 +00002189 enum preproc_token i;
2190 int j;
H. Peter Anvin70055962007-10-11 00:05:31 -07002191 bool err;
2192 int nparam;
2193 bool nolist;
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07002194 bool casesense;
H. Peter Anvin8cfdb9d2007-09-14 18:36:01 -07002195 int k, m;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002196 int offset;
H. Peter Anvinccad6f92016-10-04 00:34:35 -07002197 char *p, *pp;
2198 const char *found_path;
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08002199 const char *mname;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002200 Include *inc;
2201 Context *ctx;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002202 Cond *cond;
2203 MMacro *mmac, **mmhead;
Jin Kyu Songc9486b92013-10-28 17:07:57 -07002204 Token *t = NULL, *tt, *param_start, *macro_start, *last, **tptr, *origline;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002205 Line *l;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002206 struct tokenval tokval;
2207 expr *evalresult;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002208 MMacro *tmp_defining; /* Used when manipulating rep_nest */
H. Peter Anvinf8ba53e2007-10-11 10:11:57 -07002209 int64_t count;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07002210 size_t len;
H. Peter Anvin (Intel)6bde2ed2018-12-13 19:39:41 -08002211 errflags severity;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002212
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07002213 *output = NULL; /* No output generated */
H. Peter Anvin76690a12002-04-30 20:52:49 +00002214 origline = tline;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002215
H. Peter Anvineba20a72002-04-30 20:53:55 +00002216 skip_white_(tline);
H. Peter Anvinf2936d72008-06-04 15:11:23 -07002217 if (!tline || !tok_type_(tline, TOK_PREPROC_ID) ||
Cyrill Gorcunov4b5b7372018-10-29 22:54:08 +03002218 (tline->text[0] && (tline->text[1] == '%' ||
2219 tline->text[1] == '$' ||
2220 tline->text[1] == '!')))
H. Peter Anvine2c80182005-01-15 22:15:51 +00002221 return NO_DIRECTIVE_FOUND;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002222
H. Peter Anvin4169a472007-09-12 01:29:43 +00002223 i = pp_token_hash(tline->text);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002224
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002225 /*
2226 * FIXME: We zap execution of PP_RMACRO, PP_IRMACRO, PP_EXITMACRO
2227 * since they are known to be buggy at moment, we need to fix them
2228 * in future release (2.09-2.10)
2229 */
Philipp Klokeb432f572013-03-31 12:01:23 +02002230 if (i == PP_RMACRO || i == PP_IRMACRO || i == PP_EXITMACRO) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002231 nasm_nonfatal("unknown preprocessor directive `%s'", tline->text);
2232 return NO_DIRECTIVE_FOUND;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002233 }
2234
2235 /*
2236 * If we're in a non-emitting branch of a condition construct,
2237 * or walking to the end of an already terminated %rep block,
2238 * we should ignore all directives except for condition
2239 * directives.
2240 */
2241 if (((istk->conds && !emitting(istk->conds->state)) ||
2242 (istk->mstk && !istk->mstk->in_progress)) && !is_condition(i)) {
2243 return NO_DIRECTIVE_FOUND;
2244 }
2245
2246 /*
2247 * If we're defining a macro or reading a %rep block, we should
2248 * ignore all directives except for %macro/%imacro (which nest),
2249 * %endm/%endmacro, and (only if we're in a %rep block) %endrep.
2250 * If we're in a %rep block, another %rep nests, so should be let through.
2251 */
2252 if (defining && i != PP_MACRO && i != PP_IMACRO &&
2253 i != PP_RMACRO && i != PP_IRMACRO &&
2254 i != PP_ENDMACRO && i != PP_ENDM &&
2255 (defining->name || (i != PP_ENDREP && i != PP_REP))) {
2256 return NO_DIRECTIVE_FOUND;
2257 }
2258
2259 if (defining) {
2260 if (i == PP_MACRO || i == PP_IMACRO ||
2261 i == PP_RMACRO || i == PP_IRMACRO) {
2262 nested_mac_count++;
2263 return NO_DIRECTIVE_FOUND;
2264 } else if (nested_mac_count > 0) {
2265 if (i == PP_ENDMACRO) {
2266 nested_mac_count--;
2267 return NO_DIRECTIVE_FOUND;
2268 }
2269 }
2270 if (!defining->name) {
2271 if (i == PP_REP) {
2272 nested_rep_count++;
2273 return NO_DIRECTIVE_FOUND;
2274 } else if (nested_rep_count > 0) {
2275 if (i == PP_ENDREP) {
2276 nested_rep_count--;
2277 return NO_DIRECTIVE_FOUND;
2278 }
2279 }
2280 }
2281 }
2282
H. Peter Anvin4169a472007-09-12 01:29:43 +00002283 switch (i) {
2284 case PP_INVALID:
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002285 nasm_nonfatal("unknown preprocessor directive `%s'", tline->text);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002286 return NO_DIRECTIVE_FOUND; /* didn't get it */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002287
H. Peter Anvin3f87a2a2016-10-04 14:07:19 -07002288 case PP_PRAGMA:
2289 /*
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07002290 * %pragma namespace options...
2291 *
2292 * The namespace "preproc" is reserved for the preprocessor;
2293 * all other namespaces generate a [pragma] assembly directive.
2294 *
2295 * Invalid %pragmas are ignored and may have different
2296 * meaning in future versions of NASM.
H. Peter Anvin3f87a2a2016-10-04 14:07:19 -07002297 */
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07002298 tline = tline->next;
2299 skip_white_(tline);
2300 tline = expand_smacro(tline);
2301 if (tok_type_(tline, TOK_ID)) {
2302 if (!nasm_stricmp(tline->text, "preproc")) {
2303 /* Preprocessor pragma */
2304 do_pragma_preproc(tline);
2305 } else {
2306 /* Build the assembler directive */
2307 t = new_Token(NULL, TOK_OTHER, "[", 1);
2308 t->next = new_Token(NULL, TOK_ID, "pragma", 6);
2309 t->next->next = new_Token(tline, TOK_WHITESPACE, NULL, 0);
2310 tline = t;
2311 for (t = tline; t->next; t = t->next)
2312 ;
2313 t->next = new_Token(NULL, TOK_OTHER, "]", 1);
2314 /* true here can be revisited in the future */
2315 *output = detoken(tline, true);
2316 }
2317 }
H. Peter Anvin3f87a2a2016-10-04 14:07:19 -07002318 free_tlist(origline);
2319 return DIRECTIVE_FOUND;
2320
H. Peter Anvine2c80182005-01-15 22:15:51 +00002321 case PP_STACKSIZE:
2322 /* Directive to tell NASM what the default stack size is. The
2323 * default is for a 16-bit stack, and this can be overriden with
2324 * %stacksize large.
H. Peter Anvine2c80182005-01-15 22:15:51 +00002325 */
2326 tline = tline->next;
2327 if (tline && tline->type == TOK_WHITESPACE)
2328 tline = tline->next;
2329 if (!tline || tline->type != TOK_ID) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002330 nasm_nonfatal("`%%stacksize' missing size parameter");
H. Peter Anvine2c80182005-01-15 22:15:51 +00002331 free_tlist(origline);
2332 return DIRECTIVE_FOUND;
2333 }
2334 if (nasm_stricmp(tline->text, "flat") == 0) {
2335 /* All subsequent ARG directives are for a 32-bit stack */
2336 StackSize = 4;
2337 StackPointer = "ebp";
2338 ArgOffset = 8;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002339 LocalOffset = 0;
Charles Crayne7eaf9192007-11-08 22:11:14 -08002340 } else if (nasm_stricmp(tline->text, "flat64") == 0) {
2341 /* All subsequent ARG directives are for a 64-bit stack */
2342 StackSize = 8;
2343 StackPointer = "rbp";
Per Jessen53252e02010-02-11 00:16:59 +03002344 ArgOffset = 16;
Charles Crayne7eaf9192007-11-08 22:11:14 -08002345 LocalOffset = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002346 } else if (nasm_stricmp(tline->text, "large") == 0) {
2347 /* All subsequent ARG directives are for a 16-bit stack,
2348 * far function call.
2349 */
2350 StackSize = 2;
2351 StackPointer = "bp";
2352 ArgOffset = 4;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002353 LocalOffset = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002354 } else if (nasm_stricmp(tline->text, "small") == 0) {
2355 /* All subsequent ARG directives are for a 16-bit stack,
2356 * far function call. We don't support near functions.
2357 */
2358 StackSize = 2;
2359 StackPointer = "bp";
2360 ArgOffset = 6;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002361 LocalOffset = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002362 } else {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002363 nasm_nonfatal("`%%stacksize' invalid size type");
H. Peter Anvine2c80182005-01-15 22:15:51 +00002364 free_tlist(origline);
2365 return DIRECTIVE_FOUND;
2366 }
2367 free_tlist(origline);
2368 return DIRECTIVE_FOUND;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002369
H. Peter Anvine2c80182005-01-15 22:15:51 +00002370 case PP_ARG:
2371 /* TASM like ARG directive to define arguments to functions, in
2372 * the following form:
2373 *
2374 * ARG arg1:WORD, arg2:DWORD, arg4:QWORD
2375 */
2376 offset = ArgOffset;
2377 do {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00002378 char *arg, directive[256];
H. Peter Anvine2c80182005-01-15 22:15:51 +00002379 int size = StackSize;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002380
H. Peter Anvine2c80182005-01-15 22:15:51 +00002381 /* Find the argument name */
2382 tline = tline->next;
2383 if (tline && tline->type == TOK_WHITESPACE)
2384 tline = tline->next;
2385 if (!tline || tline->type != TOK_ID) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002386 nasm_nonfatal("`%%arg' missing argument parameter");
H. Peter Anvine2c80182005-01-15 22:15:51 +00002387 free_tlist(origline);
2388 return DIRECTIVE_FOUND;
2389 }
2390 arg = tline->text;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002391
H. Peter Anvine2c80182005-01-15 22:15:51 +00002392 /* Find the argument size type */
2393 tline = tline->next;
2394 if (!tline || tline->type != TOK_OTHER
2395 || tline->text[0] != ':') {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002396 nasm_nonfatal("Syntax error processing `%%arg' directive");
H. Peter Anvine2c80182005-01-15 22:15:51 +00002397 free_tlist(origline);
2398 return DIRECTIVE_FOUND;
2399 }
2400 tline = tline->next;
2401 if (!tline || tline->type != TOK_ID) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002402 nasm_nonfatal("`%%arg' missing size type parameter");
H. Peter Anvine2c80182005-01-15 22:15:51 +00002403 free_tlist(origline);
2404 return DIRECTIVE_FOUND;
2405 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002406
H. Peter Anvine2c80182005-01-15 22:15:51 +00002407 /* Allow macro expansion of type parameter */
Keith Kaniosb7a89542007-04-12 02:40:54 +00002408 tt = tokenize(tline->text);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002409 tt = expand_smacro(tt);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002410 size = parse_size(tt->text);
2411 if (!size) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002412 nasm_nonfatal("Invalid size type for `%%arg' missing directive");
H. Peter Anvine2c80182005-01-15 22:15:51 +00002413 free_tlist(tt);
2414 free_tlist(origline);
2415 return DIRECTIVE_FOUND;
2416 }
2417 free_tlist(tt);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002418
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002419 /* Round up to even stack slots */
2420 size = ALIGN(size, StackSize);
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002421
H. Peter Anvine2c80182005-01-15 22:15:51 +00002422 /* Now define the macro for the argument */
2423 snprintf(directive, sizeof(directive), "%%define %s (%s+%d)",
2424 arg, StackPointer, offset);
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07002425 do_directive(tokenize(directive), output);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002426 offset += size;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002427
H. Peter Anvine2c80182005-01-15 22:15:51 +00002428 /* Move to the next argument in the list */
2429 tline = tline->next;
2430 if (tline && tline->type == TOK_WHITESPACE)
2431 tline = tline->next;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002432 } while (tline && tline->type == TOK_OTHER && tline->text[0] == ',');
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002433 ArgOffset = offset;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002434 free_tlist(origline);
2435 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002436
H. Peter Anvine2c80182005-01-15 22:15:51 +00002437 case PP_LOCAL:
2438 /* TASM like LOCAL directive to define local variables for a
2439 * function, in the following form:
2440 *
2441 * LOCAL local1:WORD, local2:DWORD, local4:QWORD = LocalSize
2442 *
2443 * The '= LocalSize' at the end is ignored by NASM, but is
2444 * required by TASM to define the local parameter size (and used
2445 * by the TASM macro package).
2446 */
2447 offset = LocalOffset;
2448 do {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00002449 char *local, directive[256];
H. Peter Anvine2c80182005-01-15 22:15:51 +00002450 int size = StackSize;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002451
H. Peter Anvine2c80182005-01-15 22:15:51 +00002452 /* Find the argument name */
2453 tline = tline->next;
2454 if (tline && tline->type == TOK_WHITESPACE)
2455 tline = tline->next;
2456 if (!tline || tline->type != TOK_ID) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002457 nasm_nonfatal("`%%local' missing argument parameter");
H. Peter Anvine2c80182005-01-15 22:15:51 +00002458 free_tlist(origline);
2459 return DIRECTIVE_FOUND;
2460 }
2461 local = tline->text;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002462
H. Peter Anvine2c80182005-01-15 22:15:51 +00002463 /* Find the argument size type */
2464 tline = tline->next;
2465 if (!tline || tline->type != TOK_OTHER
2466 || tline->text[0] != ':') {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002467 nasm_nonfatal("Syntax error processing `%%local' directive");
H. Peter Anvine2c80182005-01-15 22:15:51 +00002468 free_tlist(origline);
2469 return DIRECTIVE_FOUND;
2470 }
2471 tline = tline->next;
2472 if (!tline || tline->type != TOK_ID) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002473 nasm_nonfatal("`%%local' missing size type parameter");
H. Peter Anvine2c80182005-01-15 22:15:51 +00002474 free_tlist(origline);
2475 return DIRECTIVE_FOUND;
2476 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00002477
H. Peter Anvine2c80182005-01-15 22:15:51 +00002478 /* Allow macro expansion of type parameter */
Keith Kaniosb7a89542007-04-12 02:40:54 +00002479 tt = tokenize(tline->text);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002480 tt = expand_smacro(tt);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002481 size = parse_size(tt->text);
2482 if (!size) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002483 nasm_nonfatal("Invalid size type for `%%local' missing directive");
H. Peter Anvine2c80182005-01-15 22:15:51 +00002484 free_tlist(tt);
2485 free_tlist(origline);
2486 return DIRECTIVE_FOUND;
2487 }
2488 free_tlist(tt);
H. Peter Anvin734b1882002-04-30 21:01:08 +00002489
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002490 /* Round up to even stack slots */
2491 size = ALIGN(size, StackSize);
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002492
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002493 offset += size; /* Negative offset, increment before */
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002494
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002495 /* Now define the macro for the argument */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002496 snprintf(directive, sizeof(directive), "%%define %s (%s-%d)",
2497 local, StackPointer, offset);
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07002498 do_directive(tokenize(directive), output);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002499
H. Peter Anvine2c80182005-01-15 22:15:51 +00002500 /* Now define the assign to setup the enter_c macro correctly */
2501 snprintf(directive, sizeof(directive),
2502 "%%assign %%$localsize %%$localsize+%d", size);
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07002503 do_directive(tokenize(directive), output);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002504
H. Peter Anvine2c80182005-01-15 22:15:51 +00002505 /* Move to the next argument in the list */
2506 tline = tline->next;
2507 if (tline && tline->type == TOK_WHITESPACE)
2508 tline = tline->next;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002509 } while (tline && tline->type == TOK_OTHER && tline->text[0] == ',');
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002510 LocalOffset = offset;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002511 free_tlist(origline);
2512 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002513
H. Peter Anvine2c80182005-01-15 22:15:51 +00002514 case PP_CLEAR:
2515 if (tline->next)
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08002516 nasm_warn(WARN_OTHER, "trailing garbage after `%%clear' ignored");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002517 free_macros();
2518 init_macros();
H. Peter Anvine2c80182005-01-15 22:15:51 +00002519 free_tlist(origline);
2520 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002521
H. Peter Anvin418ca702008-05-30 10:42:30 -07002522 case PP_DEPEND:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002523 t = tline->next = expand_smacro(tline->next);
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002524 skip_white_(t);
2525 if (!t || (t->type != TOK_STRING &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002526 t->type != TOK_INTERNAL_STRING)) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002527 nasm_nonfatal("`%%depend' expects a file name");
H. Peter Anvin418ca702008-05-30 10:42:30 -07002528 free_tlist(origline);
2529 return DIRECTIVE_FOUND; /* but we did _something_ */
2530 }
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002531 if (t->next)
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08002532 nasm_warn(WARN_OTHER, "trailing garbage after `%%depend' ignored");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002533 p = t->text;
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002534 if (t->type != TOK_INTERNAL_STRING)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002535 nasm_unquote_cstr(p, i);
Cyrill Gorcunovb7bb5ac2018-11-11 21:33:52 +03002536 strlist_add(deplist, p);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002537 free_tlist(origline);
H. Peter Anvin418ca702008-05-30 10:42:30 -07002538 return DIRECTIVE_FOUND;
2539
2540 case PP_INCLUDE:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002541 t = tline->next = expand_smacro(tline->next);
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002542 skip_white_(t);
H. Peter Anvind2456592008-06-19 15:04:18 -07002543
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002544 if (!t || (t->type != TOK_STRING &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002545 t->type != TOK_INTERNAL_STRING)) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002546 nasm_nonfatal("`%%include' expects a file name");
H. Peter Anvine2c80182005-01-15 22:15:51 +00002547 free_tlist(origline);
2548 return DIRECTIVE_FOUND; /* but we did _something_ */
2549 }
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002550 if (t->next)
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08002551 nasm_warn(WARN_OTHER, "trailing garbage after `%%include' ignored");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002552 p = t->text;
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002553 if (t->type != TOK_INTERNAL_STRING)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002554 nasm_unquote_cstr(p, i);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002555 inc = nasm_malloc(sizeof(Include));
H. Peter Anvine2c80182005-01-15 22:15:51 +00002556 inc->next = istk;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002557 inc->conds = NULL;
Jim Kukunas65a8afc2016-06-13 16:00:42 -04002558 found_path = NULL;
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07002559 inc->fp = inc_fopen(p, deplist, &found_path,
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08002560 (pp_mode == PP_DEPS)
2561 ? INC_OPTIONAL : INC_NEEDED, NF_TEXT);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002562 if (!inc->fp) {
2563 /* -MG given but file not found */
2564 nasm_free(inc);
2565 } else {
Jim Kukunas65a8afc2016-06-13 16:00:42 -04002566 inc->fname = src_set_fname(found_path ? found_path : p);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002567 inc->lineno = src_set_linnum(0);
2568 inc->lineinc = 1;
2569 inc->expansion = NULL;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002570 inc->mstk = NULL;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002571 istk = inc;
H. Peter Anvin8ac25aa2016-02-18 01:16:18 -08002572 lfmt->uplevel(LIST_INCLUDE);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002573 }
2574 free_tlist(origline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002575 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002576
H. Peter Anvind2456592008-06-19 15:04:18 -07002577 case PP_USE:
H. Peter Anvinf4ae5ad2008-06-19 18:39:24 -07002578 {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002579 static macros_t *use_pkg;
2580 const char *pkg_macro = NULL;
H. Peter Anvinf4ae5ad2008-06-19 18:39:24 -07002581
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002582 tline = tline->next;
2583 skip_white_(tline);
2584 tline = expand_id(tline);
H. Peter Anvind2456592008-06-19 15:04:18 -07002585
H. Peter Anvin264b7b92008-10-24 16:38:17 -07002586 if (!tline || (tline->type != TOK_STRING &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002587 tline->type != TOK_INTERNAL_STRING &&
2588 tline->type != TOK_ID)) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002589 nasm_nonfatal("`%%use' expects a package name");
H. Peter Anvind2456592008-06-19 15:04:18 -07002590 free_tlist(origline);
2591 return DIRECTIVE_FOUND; /* but we did _something_ */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002592 }
H. Peter Anvin264b7b92008-10-24 16:38:17 -07002593 if (tline->next)
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08002594 nasm_warn(WARN_OTHER, "trailing garbage after `%%use' ignored");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002595 if (tline->type == TOK_STRING)
2596 nasm_unquote_cstr(tline->text, i);
2597 use_pkg = nasm_stdmac_find_package(tline->text);
2598 if (!use_pkg)
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002599 nasm_nonfatal("unknown `%%use' package: %s", tline->text);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002600 else
2601 pkg_macro = (char *)use_pkg + 1; /* The first string will be <%define>__USE_*__ */
Victor van den Elzen35eb2ea2010-03-10 22:33:48 +01002602 if (use_pkg && ! smacro_defined(NULL, pkg_macro, 0, NULL, true)) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002603 /* Not already included, go ahead and include it */
2604 stdmacpos = use_pkg;
2605 }
2606 free_tlist(origline);
H. Peter Anvind2456592008-06-19 15:04:18 -07002607 return DIRECTIVE_FOUND;
H. Peter Anvinf4ae5ad2008-06-19 18:39:24 -07002608 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002609 case PP_PUSH:
H. Peter Anvine2c80182005-01-15 22:15:51 +00002610 case PP_REPL:
H. Peter Anvin42b56392008-10-24 16:24:21 -07002611 case PP_POP:
H. Peter Anvine2c80182005-01-15 22:15:51 +00002612 tline = tline->next;
2613 skip_white_(tline);
2614 tline = expand_id(tline);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002615 if (tline) {
2616 if (!tok_type_(tline, TOK_ID)) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002617 nasm_nonfatal("`%s' expects a context identifier",
2618 pp_directives[i]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002619 free_tlist(origline);
2620 return DIRECTIVE_FOUND; /* but we did _something_ */
2621 }
2622 if (tline->next)
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08002623 nasm_warn(WARN_OTHER, "trailing garbage after `%s' ignored",
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002624 pp_directives[i]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002625 p = nasm_strdup(tline->text);
2626 } else {
2627 p = NULL; /* Anonymous */
2628 }
H. Peter Anvin42b56392008-10-24 16:24:21 -07002629
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002630 if (i == PP_PUSH) {
H. Peter Anvin (Intel)ebb05a02018-12-11 12:30:25 -08002631 nasm_new(ctx);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002632 ctx->next = cstk;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002633 ctx->name = p;
2634 ctx->number = unique++;
2635 cstk = ctx;
2636 } else {
2637 /* %pop or %repl */
2638 if (!cstk) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002639 nasm_nonfatal("`%s': context stack is empty",
2640 pp_directives[i]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002641 } else if (i == PP_POP) {
2642 if (p && (!cstk->name || nasm_stricmp(p, cstk->name)))
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002643 nasm_nonfatal("`%%pop' in wrong context: %s, expected %s",
2644 cstk->name ? cstk->name : "anonymous", p);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002645 else
2646 ctx_pop();
2647 } else {
2648 /* i == PP_REPL */
2649 nasm_free(cstk->name);
2650 cstk->name = p;
2651 p = NULL;
2652 }
2653 nasm_free(p);
2654 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002655 free_tlist(origline);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002656 return DIRECTIVE_FOUND;
H. Peter Anvin8e3f75e2008-09-24 00:21:58 -07002657 case PP_FATAL:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002658 severity = ERR_FATAL;
2659 goto issue_error;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002660 case PP_ERROR:
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08002661 severity = ERR_NONFATAL|ERR_PASS2;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002662 goto issue_error;
H. Peter Anvin7df04172008-06-10 18:27:38 -07002663 case PP_WARNING:
H. Peter Anvin (Intel)723ab482018-12-13 21:53:31 -08002664 /*!
2665 *!user [on] %warning directives
2666 *! controls output of \c{%warning} directives (see \k{pperror}).
2667 */
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08002668 severity = ERR_WARNING|WARN_USER|ERR_PASS2;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002669 goto issue_error;
H. Peter Anvin8e3f75e2008-09-24 00:21:58 -07002670
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002671issue_error:
H. Peter Anvin7df04172008-06-10 18:27:38 -07002672 {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002673 /* Only error out if this is the final pass */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002674 tline->next = expand_smacro(tline->next);
2675 tline = tline->next;
2676 skip_white_(tline);
2677 t = tline ? tline->next : NULL;
2678 skip_white_(t);
2679 if (tok_type_(tline, TOK_STRING) && !t) {
2680 /* The line contains only a quoted string */
2681 p = tline->text;
2682 nasm_unquote(p, NULL); /* Ignore NUL character truncation */
H. Peter Anvin130736c2016-02-17 20:27:41 -08002683 nasm_error(severity, "%s", p);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002684 } else {
2685 /* Not a quoted string, or more than a quoted string */
2686 p = detoken(tline, false);
H. Peter Anvin130736c2016-02-17 20:27:41 -08002687 nasm_error(severity, "%s", p);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002688 nasm_free(p);
2689 }
2690 free_tlist(origline);
2691 return DIRECTIVE_FOUND;
H. Peter Anvin7df04172008-06-10 18:27:38 -07002692 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00002693
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002694 CASE_PP_IF:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002695 if (istk->conds && !emitting(istk->conds->state))
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002696 j = COND_NEVER;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002697 else {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002698 j = if_condition(tline->next, i);
2699 tline->next = NULL; /* it got freed */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002700 j = j < 0 ? COND_NEVER : j ? COND_IF_TRUE : COND_IF_FALSE;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002701 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002702 cond = nasm_malloc(sizeof(Cond));
2703 cond->next = istk->conds;
2704 cond->state = j;
2705 istk->conds = cond;
2706 if(istk->mstk)
2707 istk->mstk->condcnt ++;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002708 free_tlist(origline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002709 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002710
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002711 CASE_PP_ELIF:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002712 if (!istk->conds)
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002713 nasm_fatal("`%s': no matching `%%if'", pp_directives[i]);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002714 switch(istk->conds->state) {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002715 case COND_IF_TRUE:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002716 istk->conds->state = COND_DONE;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002717 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002718
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002719 case COND_DONE:
2720 case COND_NEVER:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002721 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002722
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002723 case COND_ELSE_TRUE:
2724 case COND_ELSE_FALSE:
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08002725 nasm_warn(WARN_OTHER|ERR_PP_PRECOND,
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002726 "`%%elif' after `%%else' ignored");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002727 istk->conds->state = COND_NEVER;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002728 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002729
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002730 case COND_IF_FALSE:
2731 /*
2732 * IMPORTANT: In the case of %if, we will already have
2733 * called expand_mmac_params(); however, if we're
2734 * processing an %elif we must have been in a
2735 * non-emitting mode, which would have inhibited
2736 * the normal invocation of expand_mmac_params().
2737 * Therefore, we have to do it explicitly here.
2738 */
2739 j = if_condition(expand_mmac_params(tline->next), i);
2740 tline->next = NULL; /* it got freed */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002741 istk->conds->state =
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002742 j < 0 ? COND_NEVER : j ? COND_IF_TRUE : COND_IF_FALSE;
2743 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002744 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002745 free_tlist(origline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002746 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002747
H. Peter Anvine2c80182005-01-15 22:15:51 +00002748 case PP_ELSE:
2749 if (tline->next)
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08002750 nasm_warn(WARN_OTHER|ERR_PP_PRECOND,
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002751 "trailing garbage after `%%else' ignored");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002752 if (!istk->conds)
H. Peter Anvinc5136902018-06-15 18:20:17 -07002753 nasm_fatal("`%%else: no matching `%%if'");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002754 switch(istk->conds->state) {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002755 case COND_IF_TRUE:
2756 case COND_DONE:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002757 istk->conds->state = COND_ELSE_FALSE;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002758 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002759
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002760 case COND_NEVER:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002761 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002762
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002763 case COND_IF_FALSE:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002764 istk->conds->state = COND_ELSE_TRUE;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002765 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002766
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002767 case COND_ELSE_TRUE:
2768 case COND_ELSE_FALSE:
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08002769 nasm_warn(WARN_OTHER|ERR_PP_PRECOND,
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002770 "`%%else' after `%%else' ignored.");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002771 istk->conds->state = COND_NEVER;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002772 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002773 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002774 free_tlist(origline);
2775 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002776
H. Peter Anvine2c80182005-01-15 22:15:51 +00002777 case PP_ENDIF:
2778 if (tline->next)
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08002779 nasm_warn(WARN_OTHER|ERR_PP_PRECOND,
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002780 "trailing garbage after `%%endif' ignored");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002781 if (!istk->conds)
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002782 nasm_fatal("`%%endif': no matching `%%if'");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002783 cond = istk->conds;
2784 istk->conds = cond->next;
2785 nasm_free(cond);
2786 if(istk->mstk)
2787 istk->mstk->condcnt --;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002788 free_tlist(origline);
2789 return DIRECTIVE_FOUND;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002790
H. Peter Anvindb8f96e2009-07-15 09:07:29 -04002791 case PP_RMACRO:
2792 case PP_IRMACRO:
H. Peter Anvine2c80182005-01-15 22:15:51 +00002793 case PP_MACRO:
2794 case PP_IMACRO:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002795 if (defining) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002796 nasm_fatal("`%s': already defining a macro",
2797 pp_directives[i]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002798 return DIRECTIVE_FOUND;
2799 }
H. Peter Anvin4def1a82016-05-09 13:59:44 -07002800 defining = nasm_zalloc(sizeof(MMacro));
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07002801 defining->max_depth = ((i == PP_RMACRO) || (i == PP_IRMACRO))
2802 ? nasm_limit[LIMIT_MACROS] : 0;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002803 defining->casesense = (i == PP_MACRO) || (i == PP_RMACRO);
2804 if (!parse_mmacro_spec(tline, defining, pp_directives[i])) {
2805 nasm_free(defining);
2806 defining = NULL;
2807 return DIRECTIVE_FOUND;
2808 }
H. Peter Anvina26433d2008-07-16 14:40:01 -07002809
H. Peter Anvin4def1a82016-05-09 13:59:44 -07002810 src_get(&defining->xline, &defining->fname);
2811
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002812 mmac = (MMacro *) hash_findix(&mmacros, defining->name);
2813 while (mmac) {
2814 if (!strcmp(mmac->name, defining->name) &&
2815 (mmac->nparam_min <= defining->nparam_max
2816 || defining->plus)
2817 && (defining->nparam_min <= mmac->nparam_max
2818 || mmac->plus)) {
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08002819 nasm_warn(WARN_OTHER, "redefining multi-line macro `%s'",
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002820 defining->name);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002821 return DIRECTIVE_FOUND;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002822 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002823 mmac = mmac->next;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002824 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002825 free_tlist(origline);
2826 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002827
H. Peter Anvine2c80182005-01-15 22:15:51 +00002828 case PP_ENDM:
2829 case PP_ENDMACRO:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002830 if (! (defining && defining->name)) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002831 nasm_nonfatal("`%s': not defining a macro", tline->text);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002832 return DIRECTIVE_FOUND;
2833 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002834 mmhead = (MMacro **) hash_findi_add(&mmacros, defining->name);
2835 defining->next = *mmhead;
2836 *mmhead = defining;
2837 defining = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002838 free_tlist(origline);
2839 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002840
H. Peter Anvin89cee572009-07-15 09:16:54 -04002841 case PP_EXITMACRO:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002842 /*
2843 * We must search along istk->expansion until we hit a
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002844 * macro-end marker for a macro with a name. Then we
2845 * bypass all lines between exitmacro and endmacro.
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002846 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002847 list_for_each(l, istk->expansion)
2848 if (l->finishes && l->finishes->name)
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002849 break;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002850
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002851 if (l) {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002852 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002853 * Remove all conditional entries relative to this
2854 * macro invocation. (safe to do in this context)
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002855 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002856 for ( ; l->finishes->condcnt > 0; l->finishes->condcnt --) {
2857 cond = istk->conds;
2858 istk->conds = cond->next;
2859 nasm_free(cond);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002860 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002861 istk->expansion = l;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002862 } else {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002863 nasm_nonfatal("`%%exitmacro' not within `%%macro' block");
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002864 }
Keith Kanios852f1ee2009-07-12 00:19:55 -05002865 free_tlist(origline);
2866 return DIRECTIVE_FOUND;
2867
H. Peter Anvina26433d2008-07-16 14:40:01 -07002868 case PP_UNMACRO:
2869 case PP_UNIMACRO:
2870 {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002871 MMacro **mmac_p;
2872 MMacro spec;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002873
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002874 spec.casesense = (i == PP_UNMACRO);
2875 if (!parse_mmacro_spec(tline, &spec, pp_directives[i])) {
2876 return DIRECTIVE_FOUND;
2877 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002878 mmac_p = (MMacro **) hash_findi(&mmacros, spec.name, NULL);
2879 while (mmac_p && *mmac_p) {
2880 mmac = *mmac_p;
2881 if (mmac->casesense == spec.casesense &&
2882 !mstrcmp(mmac->name, spec.name, spec.casesense) &&
2883 mmac->nparam_min == spec.nparam_min &&
2884 mmac->nparam_max == spec.nparam_max &&
2885 mmac->plus == spec.plus) {
2886 *mmac_p = mmac->next;
2887 free_mmacro(mmac);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002888 } else {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002889 mmac_p = &mmac->next;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002890 }
2891 }
2892 free_tlist(origline);
2893 free_tlist(spec.dlist);
2894 return DIRECTIVE_FOUND;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002895 }
2896
H. Peter Anvine2c80182005-01-15 22:15:51 +00002897 case PP_ROTATE:
2898 if (tline->next && tline->next->type == TOK_WHITESPACE)
2899 tline = tline->next;
H. Peter Anvin89cee572009-07-15 09:16:54 -04002900 if (!tline->next) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002901 free_tlist(origline);
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002902 nasm_nonfatal("`%%rotate' missing rotate count");
H. Peter Anvine2c80182005-01-15 22:15:51 +00002903 return DIRECTIVE_FOUND;
2904 }
2905 t = expand_smacro(tline->next);
2906 tline->next = NULL;
2907 free_tlist(origline);
2908 tline = t;
2909 tptr = &t;
2910 tokval.t_type = TOKEN_INVALID;
2911 evalresult =
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08002912 evaluate(ppscan, tptr, &tokval, NULL, true, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002913 free_tlist(tline);
2914 if (!evalresult)
2915 return DIRECTIVE_FOUND;
2916 if (tokval.t_type)
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08002917 nasm_warn(WARN_OTHER, "trailing garbage after expression ignored");
H. Peter Anvine2c80182005-01-15 22:15:51 +00002918 if (!is_simple(evalresult)) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002919 nasm_nonfatal("non-constant value given to `%%rotate'");
H. Peter Anvine2c80182005-01-15 22:15:51 +00002920 return DIRECTIVE_FOUND;
2921 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002922 mmac = istk->mstk;
2923 while (mmac && !mmac->name) /* avoid mistaking %reps for macros */
2924 mmac = mmac->next_active;
2925 if (!mmac) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002926 nasm_nonfatal("`%%rotate' invoked outside a macro call");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002927 } else if (mmac->nparam == 0) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002928 nasm_nonfatal("`%%rotate' invoked within macro without parameters");
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002929 } else {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002930 int rotate = mmac->rotate + reloc_value(evalresult);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002931
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002932 rotate %= (int)mmac->nparam;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002933 if (rotate < 0)
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002934 rotate += mmac->nparam;
2935
2936 mmac->rotate = rotate;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002937 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002938 return DIRECTIVE_FOUND;
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00002939
H. Peter Anvine2c80182005-01-15 22:15:51 +00002940 case PP_REP:
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002941 nolist = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002942 do {
2943 tline = tline->next;
2944 } while (tok_type_(tline, TOK_WHITESPACE));
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00002945
H. Peter Anvine2c80182005-01-15 22:15:51 +00002946 if (tok_type_(tline, TOK_ID) &&
2947 nasm_stricmp(tline->text, ".nolist") == 0) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002948 nolist = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002949 do {
2950 tline = tline->next;
2951 } while (tok_type_(tline, TOK_WHITESPACE));
2952 }
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00002953
H. Peter Anvine2c80182005-01-15 22:15:51 +00002954 if (tline) {
2955 t = expand_smacro(tline);
2956 tptr = &t;
2957 tokval.t_type = TOKEN_INVALID;
2958 evalresult =
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08002959 evaluate(ppscan, tptr, &tokval, NULL, true, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002960 if (!evalresult) {
2961 free_tlist(origline);
2962 return DIRECTIVE_FOUND;
2963 }
2964 if (tokval.t_type)
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08002965 nasm_warn(WARN_OTHER, "trailing garbage after expression ignored");
H. Peter Anvine2c80182005-01-15 22:15:51 +00002966 if (!is_simple(evalresult)) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002967 nasm_nonfatal("non-constant value given to `%%rep'");
H. Peter Anvine2c80182005-01-15 22:15:51 +00002968 return DIRECTIVE_FOUND;
2969 }
Cyrill Gorcunove091d6e2010-08-09 13:58:22 +04002970 count = reloc_value(evalresult);
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07002971 if (count > nasm_limit[LIMIT_REP]) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002972 nasm_nonfatal("`%%rep' count %"PRId64" exceeds limit (currently %"PRId64")",
2973 count, nasm_limit[LIMIT_REP]);
Cyrill Gorcunove091d6e2010-08-09 13:58:22 +04002974 count = 0;
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07002975 } else if (count < 0) {
H. Peter Anvin (Intel)723ab482018-12-13 21:53:31 -08002976 /*!
2977 *!negative-rep [on] regative %rep count
2978 *! warns about negative counts given to the \c{%rep}
2979 *! preprocessor directive.
2980 */
H. Peter Anvin (Intel)80c4f232018-12-14 13:33:24 -08002981 nasm_warn(ERR_PASS2|WARN_NEGATIVE_REP,
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07002982 "negative `%%rep' count: %"PRId64, count);
2983 count = 0;
2984 } else {
Cyrill Gorcunove091d6e2010-08-09 13:58:22 +04002985 count++;
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07002986 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002987 } else {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002988 nasm_nonfatal("`%%rep' expects a repeat count");
H. Peter Anvinf8ba53e2007-10-11 10:11:57 -07002989 count = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002990 }
2991 free_tlist(origline);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002992
2993 tmp_defining = defining;
2994 defining = nasm_malloc(sizeof(MMacro));
2995 defining->prev = NULL;
2996 defining->name = NULL; /* flags this macro as a %rep block */
2997 defining->casesense = false;
2998 defining->plus = false;
2999 defining->nolist = nolist;
3000 defining->in_progress = count;
3001 defining->max_depth = 0;
3002 defining->nparam_min = defining->nparam_max = 0;
3003 defining->defaults = NULL;
3004 defining->dlist = NULL;
3005 defining->expansion = NULL;
3006 defining->next_active = istk->mstk;
3007 defining->rep_nest = tmp_defining;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003008 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003009
H. Peter Anvine2c80182005-01-15 22:15:51 +00003010 case PP_ENDREP:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003011 if (!defining || defining->name) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003012 nasm_nonfatal("`%%endrep': no matching `%%rep'");
H. Peter Anvine2c80182005-01-15 22:15:51 +00003013 return DIRECTIVE_FOUND;
3014 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003015
H. Peter Anvine2c80182005-01-15 22:15:51 +00003016 /*
3017 * Now we have a "macro" defined - although it has no name
3018 * and we won't be entering it in the hash tables - we must
3019 * push a macro-end marker for it on to istk->expansion.
3020 * After that, it will take care of propagating itself (a
3021 * macro-end marker line for a macro which is really a %rep
3022 * block will cause the macro to be re-expanded, complete
3023 * with another macro-end marker to ensure the process
3024 * continues) until the whole expansion is forcibly removed
3025 * from istk->expansion by a %exitrep.
3026 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003027 l = nasm_malloc(sizeof(Line));
3028 l->next = istk->expansion;
3029 l->finishes = defining;
3030 l->first = NULL;
3031 istk->expansion = l;
3032
3033 istk->mstk = defining;
3034
H. Peter Anvin8ac25aa2016-02-18 01:16:18 -08003035 lfmt->uplevel(defining->nolist ? LIST_MACRO_NOLIST : LIST_MACRO);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003036 tmp_defining = defining;
3037 defining = defining->rep_nest;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003038 free_tlist(origline);
3039 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003040
H. Peter Anvine2c80182005-01-15 22:15:51 +00003041 case PP_EXITREP:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003042 /*
3043 * We must search along istk->expansion until we hit a
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003044 * macro-end marker for a macro with no name. Then we set
3045 * its `in_progress' flag to 0.
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003046 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003047 list_for_each(l, istk->expansion)
3048 if (l->finishes && !l->finishes->name)
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003049 break;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003050
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003051 if (l)
3052 l->finishes->in_progress = 1;
3053 else
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003054 nasm_nonfatal("`%%exitrep' not within `%%rep' block");
H. Peter Anvine2c80182005-01-15 22:15:51 +00003055 free_tlist(origline);
3056 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003057
H. Peter Anvine2c80182005-01-15 22:15:51 +00003058 case PP_XDEFINE:
3059 case PP_IXDEFINE:
3060 case PP_DEFINE:
3061 case PP_IDEFINE:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003062 casesense = (i == PP_DEFINE || i == PP_XDEFINE);
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07003063
H. Peter Anvine2c80182005-01-15 22:15:51 +00003064 tline = tline->next;
3065 skip_white_(tline);
3066 tline = expand_id(tline);
3067 if (!tline || (tline->type != TOK_ID &&
3068 (tline->type != TOK_PREPROC_ID ||
3069 tline->text[1] != '$'))) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003070 nasm_nonfatal("`%s' expects a macro identifier",
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003071 pp_directives[i]);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003072 free_tlist(origline);
3073 return DIRECTIVE_FOUND;
3074 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003075
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04003076 ctx = get_ctx(tline->text, &mname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003077 last = tline;
3078 param_start = tline = tline->next;
3079 nparam = 0;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003080
H. Peter Anvine2c80182005-01-15 22:15:51 +00003081 /* Expand the macro definition now for %xdefine and %ixdefine */
3082 if ((i == PP_XDEFINE) || (i == PP_IXDEFINE))
3083 tline = expand_smacro(tline);
H. Peter Anvin734b1882002-04-30 21:01:08 +00003084
H. Peter Anvine2c80182005-01-15 22:15:51 +00003085 if (tok_is_(tline, "(")) {
3086 /*
3087 * This macro has parameters.
3088 */
H. Peter Anvin734b1882002-04-30 21:01:08 +00003089
H. Peter Anvine2c80182005-01-15 22:15:51 +00003090 tline = tline->next;
3091 while (1) {
3092 skip_white_(tline);
3093 if (!tline) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003094 nasm_nonfatal("parameter identifier expected");
H. Peter Anvine2c80182005-01-15 22:15:51 +00003095 free_tlist(origline);
3096 return DIRECTIVE_FOUND;
3097 }
3098 if (tline->type != TOK_ID) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003099 nasm_nonfatal("`%s': parameter identifier expected",
3100 tline->text);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003101 free_tlist(origline);
3102 return DIRECTIVE_FOUND;
3103 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003104 tline->type = TOK_SMAC_PARAM + nparam++;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003105 tline = tline->next;
3106 skip_white_(tline);
3107 if (tok_is_(tline, ",")) {
3108 tline = tline->next;
H. Peter Anvinca348b62008-07-23 10:49:26 -04003109 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003110 if (!tok_is_(tline, ")")) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003111 nasm_nonfatal("`)' expected to terminate macro template");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003112 free_tlist(origline);
3113 return DIRECTIVE_FOUND;
3114 }
3115 break;
3116 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00003117 }
3118 last = tline;
3119 tline = tline->next;
3120 }
3121 if (tok_type_(tline, TOK_WHITESPACE))
3122 last = tline, tline = tline->next;
3123 macro_start = NULL;
3124 last->next = NULL;
3125 t = tline;
3126 while (t) {
3127 if (t->type == TOK_ID) {
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04003128 list_for_each(tt, param_start)
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003129 if (tt->type >= TOK_SMAC_PARAM &&
H. Peter Anvine2c80182005-01-15 22:15:51 +00003130 !strcmp(tt->text, t->text))
3131 t->type = tt->type;
3132 }
3133 tt = t->next;
3134 t->next = macro_start;
3135 macro_start = t;
3136 t = tt;
3137 }
3138 /*
3139 * Good. We now have a macro name, a parameter count, and a
3140 * token list (in reverse order) for an expansion. We ought
3141 * to be OK just to create an SMacro, store it, and let
3142 * free_tlist have the rest of the line (which we have
3143 * carefully re-terminated after chopping off the expansion
3144 * from the end).
3145 */
H. Peter Anvin4db5a162007-10-11 13:42:09 -07003146 define_smacro(ctx, mname, casesense, nparam, macro_start);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003147 free_tlist(origline);
3148 return DIRECTIVE_FOUND;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003149
H. Peter Anvine2c80182005-01-15 22:15:51 +00003150 case PP_UNDEF:
3151 tline = tline->next;
3152 skip_white_(tline);
3153 tline = expand_id(tline);
3154 if (!tline || (tline->type != TOK_ID &&
3155 (tline->type != TOK_PREPROC_ID ||
3156 tline->text[1] != '$'))) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003157 nasm_nonfatal("`%%undef' expects a macro identifier");
H. Peter Anvine2c80182005-01-15 22:15:51 +00003158 free_tlist(origline);
3159 return DIRECTIVE_FOUND;
3160 }
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003161 if (tline->next)
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08003162 nasm_warn(WARN_OTHER, "trailing garbage after macro name ignored");
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003163
H. Peter Anvine2c80182005-01-15 22:15:51 +00003164 /* Find the context that symbol belongs to */
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04003165 ctx = get_ctx(tline->text, &mname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003166 undef_smacro(ctx, mname);
3167 free_tlist(origline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003168 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003169
H. Peter Anvin9e200162008-06-04 17:23:14 -07003170 case PP_DEFSTR:
3171 case PP_IDEFSTR:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003172 casesense = (i == PP_DEFSTR);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003173
3174 tline = tline->next;
3175 skip_white_(tline);
3176 tline = expand_id(tline);
3177 if (!tline || (tline->type != TOK_ID &&
3178 (tline->type != TOK_PREPROC_ID ||
3179 tline->text[1] != '$'))) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003180 nasm_nonfatal("`%s' expects a macro identifier",
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003181 pp_directives[i]);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003182 free_tlist(origline);
3183 return DIRECTIVE_FOUND;
3184 }
3185
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04003186 ctx = get_ctx(tline->text, &mname);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003187 last = tline;
3188 tline = expand_smacro(tline->next);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003189 last->next = NULL;
H. Peter Anvin9e200162008-06-04 17:23:14 -07003190
3191 while (tok_type_(tline, TOK_WHITESPACE))
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003192 tline = delete_Token(tline);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003193
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003194 p = detoken(tline, false);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003195 macro_start = nasm_malloc(sizeof(*macro_start));
3196 macro_start->next = NULL;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003197 macro_start->text = nasm_quote(p, strlen(p));
3198 macro_start->type = TOK_STRING;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003199 macro_start->a.mac = NULL;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003200 nasm_free(p);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003201
3202 /*
3203 * We now have a macro name, an implicit parameter count of
3204 * zero, and a string token to use as an expansion. Create
3205 * and store an SMacro.
3206 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003207 define_smacro(ctx, mname, casesense, 0, macro_start);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003208 free_tlist(origline);
3209 return DIRECTIVE_FOUND;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003210
H. Peter Anvin2f55bda2009-07-14 15:04:04 -04003211 case PP_DEFTOK:
3212 case PP_IDEFTOK:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003213 casesense = (i == PP_DEFTOK);
3214
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003215 tline = tline->next;
3216 skip_white_(tline);
3217 tline = expand_id(tline);
3218 if (!tline || (tline->type != TOK_ID &&
3219 (tline->type != TOK_PREPROC_ID ||
3220 tline->text[1] != '$'))) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003221 nasm_nonfatal("`%s' expects a macro identifier as first parameter",
3222 pp_directives[i]);
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003223 free_tlist(origline);
3224 return DIRECTIVE_FOUND;
3225 }
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04003226 ctx = get_ctx(tline->text, &mname);
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003227 last = tline;
3228 tline = expand_smacro(tline->next);
3229 last->next = NULL;
3230
3231 t = tline;
3232 while (tok_type_(t, TOK_WHITESPACE))
3233 t = t->next;
3234 /* t should now point to the string */
Cyrill Gorcunov6908e582010-09-06 19:36:15 +04003235 if (!tok_type_(t, TOK_STRING)) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003236 nasm_nonfatal("`%s` requires string as second parameter",
3237 pp_directives[i]);
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003238 free_tlist(tline);
3239 free_tlist(origline);
3240 return DIRECTIVE_FOUND;
3241 }
3242
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04003243 /*
3244 * Convert the string to a token stream. Note that smacros
3245 * are stored with the token stream reversed, so we have to
3246 * reverse the output of tokenize().
3247 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003248 nasm_unquote_cstr(t->text, i);
H. Peter Anvinb40992c2010-09-15 08:57:21 -07003249 macro_start = reverse_tokens(tokenize(t->text));
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003250
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003251 /*
3252 * We now have a macro name, an implicit parameter count of
3253 * zero, and a numeric token to use as an expansion. Create
3254 * and store an SMacro.
3255 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003256 define_smacro(ctx, mname, casesense, 0, macro_start);
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003257 free_tlist(tline);
3258 free_tlist(origline);
3259 return DIRECTIVE_FOUND;
H. Peter Anvin9e200162008-06-04 17:23:14 -07003260
H. Peter Anvin418ca702008-05-30 10:42:30 -07003261 case PP_PATHSEARCH:
3262 {
H. Peter Anvinccad6f92016-10-04 00:34:35 -07003263 const char *found_path;
H. Peter Anvin418ca702008-05-30 10:42:30 -07003264
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003265 casesense = true;
H. Peter Anvin418ca702008-05-30 10:42:30 -07003266
3267 tline = tline->next;
3268 skip_white_(tline);
3269 tline = expand_id(tline);
3270 if (!tline || (tline->type != TOK_ID &&
3271 (tline->type != TOK_PREPROC_ID ||
3272 tline->text[1] != '$'))) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003273 nasm_nonfatal("`%%pathsearch' expects a macro identifier as first parameter");
H. Peter Anvin418ca702008-05-30 10:42:30 -07003274 free_tlist(origline);
3275 return DIRECTIVE_FOUND;
3276 }
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04003277 ctx = get_ctx(tline->text, &mname);
H. Peter Anvin418ca702008-05-30 10:42:30 -07003278 last = tline;
3279 tline = expand_smacro(tline->next);
3280 last->next = NULL;
3281
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003282 t = tline;
H. Peter Anvin418ca702008-05-30 10:42:30 -07003283 while (tok_type_(t, TOK_WHITESPACE))
3284 t = t->next;
3285
3286 if (!t || (t->type != TOK_STRING &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003287 t->type != TOK_INTERNAL_STRING)) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003288 nasm_nonfatal("`%%pathsearch' expects a file name");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003289 free_tlist(tline);
H. Peter Anvin418ca702008-05-30 10:42:30 -07003290 free_tlist(origline);
3291 return DIRECTIVE_FOUND; /* but we did _something_ */
3292 }
3293 if (t->next)
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08003294 nasm_warn(WARN_OTHER, "trailing garbage after `%%pathsearch' ignored");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003295 p = t->text;
H. Peter Anvin427cc912008-06-01 21:43:03 -07003296 if (t->type != TOK_INTERNAL_STRING)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003297 nasm_unquote(p, NULL);
H. Peter Anvin418ca702008-05-30 10:42:30 -07003298
H. Peter Anvin9924d1e2016-10-04 00:59:39 -07003299 inc_fopen(p, NULL, &found_path, INC_PROBE, NF_BINARY);
H. Peter Anvinccad6f92016-10-04 00:34:35 -07003300 if (!found_path)
3301 found_path = p;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003302 macro_start = nasm_malloc(sizeof(*macro_start));
3303 macro_start->next = NULL;
H. Peter Anvinccad6f92016-10-04 00:34:35 -07003304 macro_start->text = nasm_quote(found_path, strlen(found_path));
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003305 macro_start->type = TOK_STRING;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003306 macro_start->a.mac = NULL;
H. Peter Anvin418ca702008-05-30 10:42:30 -07003307
3308 /*
3309 * We now have a macro name, an implicit parameter count of
3310 * zero, and a string token to use as an expansion. Create
3311 * and store an SMacro.
3312 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003313 define_smacro(ctx, mname, casesense, 0, macro_start);
3314 free_tlist(tline);
H. Peter Anvin418ca702008-05-30 10:42:30 -07003315 free_tlist(origline);
3316 return DIRECTIVE_FOUND;
3317 }
3318
H. Peter Anvine2c80182005-01-15 22:15:51 +00003319 case PP_STRLEN:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003320 casesense = true;
H. Peter Anvin70653092007-10-19 14:42:29 -07003321
H. Peter Anvine2c80182005-01-15 22:15:51 +00003322 tline = tline->next;
3323 skip_white_(tline);
3324 tline = expand_id(tline);
3325 if (!tline || (tline->type != TOK_ID &&
3326 (tline->type != TOK_PREPROC_ID ||
3327 tline->text[1] != '$'))) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003328 nasm_nonfatal("`%%strlen' expects a macro identifier as first parameter");
H. Peter Anvine2c80182005-01-15 22:15:51 +00003329 free_tlist(origline);
3330 return DIRECTIVE_FOUND;
3331 }
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04003332 ctx = get_ctx(tline->text, &mname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003333 last = tline;
3334 tline = expand_smacro(tline->next);
3335 last->next = NULL;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003336
H. Peter Anvine2c80182005-01-15 22:15:51 +00003337 t = tline;
3338 while (tok_type_(t, TOK_WHITESPACE))
3339 t = t->next;
3340 /* t should now point to the string */
Cyrill Gorcunov4e1d5ab2010-07-23 18:51:51 +04003341 if (!tok_type_(t, TOK_STRING)) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003342 nasm_nonfatal("`%%strlen` requires string as second parameter");
H. Peter Anvine2c80182005-01-15 22:15:51 +00003343 free_tlist(tline);
3344 free_tlist(origline);
3345 return DIRECTIVE_FOUND;
3346 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003347
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003348 macro_start = nasm_malloc(sizeof(*macro_start));
3349 macro_start->next = NULL;
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07003350 make_tok_num(macro_start, nasm_unquote(t->text, NULL));
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003351 macro_start->a.mac = NULL;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003352
H. Peter Anvine2c80182005-01-15 22:15:51 +00003353 /*
3354 * We now have a macro name, an implicit parameter count of
3355 * zero, and a numeric token to use as an expansion. Create
3356 * and store an SMacro.
3357 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003358 define_smacro(ctx, mname, casesense, 0, macro_start);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003359 free_tlist(tline);
3360 free_tlist(origline);
3361 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003362
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003363 case PP_STRCAT:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003364 casesense = true;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003365
3366 tline = tline->next;
3367 skip_white_(tline);
3368 tline = expand_id(tline);
3369 if (!tline || (tline->type != TOK_ID &&
3370 (tline->type != TOK_PREPROC_ID ||
3371 tline->text[1] != '$'))) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003372 nasm_nonfatal("`%%strcat' expects a macro identifier as first parameter");
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003373 free_tlist(origline);
3374 return DIRECTIVE_FOUND;
3375 }
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04003376 ctx = get_ctx(tline->text, &mname);
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003377 last = tline;
3378 tline = expand_smacro(tline->next);
3379 last->next = NULL;
3380
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003381 len = 0;
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04003382 list_for_each(t, tline) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003383 switch (t->type) {
3384 case TOK_WHITESPACE:
3385 break;
3386 case TOK_STRING:
3387 len += t->a.len = nasm_unquote(t->text, NULL);
3388 break;
3389 case TOK_OTHER:
3390 if (!strcmp(t->text, ",")) /* permit comma separators */
3391 break;
3392 /* else fall through */
3393 default:
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003394 nasm_nonfatal("non-string passed to `%%strcat' (%d)", t->type);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003395 free_tlist(tline);
3396 free_tlist(origline);
3397 return DIRECTIVE_FOUND;
3398 }
3399 }
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003400
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003401 p = pp = nasm_malloc(len);
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04003402 list_for_each(t, tline) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003403 if (t->type == TOK_STRING) {
3404 memcpy(p, t->text, t->a.len);
3405 p += t->a.len;
3406 }
3407 }
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003408
3409 /*
3410 * We now have a macro name, an implicit parameter count of
3411 * zero, and a numeric token to use as an expansion. Create
3412 * and store an SMacro.
3413 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003414 macro_start = new_Token(NULL, TOK_STRING, NULL, 0);
3415 macro_start->text = nasm_quote(pp, len);
3416 nasm_free(pp);
3417 define_smacro(ctx, mname, casesense, 0, macro_start);
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003418 free_tlist(tline);
3419 free_tlist(origline);
3420 return DIRECTIVE_FOUND;
3421
H. Peter Anvine2c80182005-01-15 22:15:51 +00003422 case PP_SUBSTR:
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003423 {
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003424 int64_t start, count;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003425 size_t len;
H. Peter Anvind2456592008-06-19 15:04:18 -07003426
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003427 casesense = true;
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07003428
H. Peter Anvine2c80182005-01-15 22:15:51 +00003429 tline = tline->next;
3430 skip_white_(tline);
3431 tline = expand_id(tline);
3432 if (!tline || (tline->type != TOK_ID &&
3433 (tline->type != TOK_PREPROC_ID ||
3434 tline->text[1] != '$'))) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003435 nasm_nonfatal("`%%substr' expects a macro identifier as first parameter");
H. Peter Anvine2c80182005-01-15 22:15:51 +00003436 free_tlist(origline);
3437 return DIRECTIVE_FOUND;
3438 }
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04003439 ctx = get_ctx(tline->text, &mname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003440 last = tline;
3441 tline = expand_smacro(tline->next);
3442 last->next = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003443
Cyrill Gorcunov35519d62010-09-06 23:49:52 +04003444 if (tline) /* skip expanded id */
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003445 t = tline->next;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003446 while (tok_type_(t, TOK_WHITESPACE))
3447 t = t->next;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003448
H. Peter Anvine2c80182005-01-15 22:15:51 +00003449 /* t should now point to the string */
Cyrill Gorcunov35519d62010-09-06 23:49:52 +04003450 if (!tok_type_(t, TOK_STRING)) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003451 nasm_nonfatal("`%%substr` requires string as second parameter");
H. Peter Anvine2c80182005-01-15 22:15:51 +00003452 free_tlist(tline);
3453 free_tlist(origline);
3454 return DIRECTIVE_FOUND;
3455 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003456
H. Peter Anvine2c80182005-01-15 22:15:51 +00003457 tt = t->next;
3458 tptr = &tt;
3459 tokval.t_type = TOKEN_INVALID;
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08003460 evalresult = evaluate(ppscan, tptr, &tokval, NULL, true, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003461 if (!evalresult) {
3462 free_tlist(tline);
3463 free_tlist(origline);
3464 return DIRECTIVE_FOUND;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003465 } else if (!is_simple(evalresult)) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003466 nasm_nonfatal("non-constant value given to `%%substr`");
H. Peter Anvine2c80182005-01-15 22:15:51 +00003467 free_tlist(tline);
3468 free_tlist(origline);
3469 return DIRECTIVE_FOUND;
3470 }
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003471 start = evalresult->value - 1;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003472
3473 while (tok_type_(tt, TOK_WHITESPACE))
3474 tt = tt->next;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003475 if (!tt) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003476 count = 1; /* Backwards compatibility: one character */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003477 } else {
3478 tokval.t_type = TOKEN_INVALID;
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08003479 evalresult = evaluate(ppscan, tptr, &tokval, NULL, true, NULL);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003480 if (!evalresult) {
3481 free_tlist(tline);
3482 free_tlist(origline);
3483 return DIRECTIVE_FOUND;
3484 } else if (!is_simple(evalresult)) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003485 nasm_nonfatal("non-constant value given to `%%substr`");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003486 free_tlist(tline);
3487 free_tlist(origline);
3488 return DIRECTIVE_FOUND;
3489 }
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003490 count = evalresult->value;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003491 }
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003492
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003493 len = nasm_unquote(t->text, NULL);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003494
Cyrill Gorcunovcff031e2010-09-07 20:31:11 +04003495 /* make start and count being in range */
3496 if (start < 0)
3497 start = 0;
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003498 if (count < 0)
3499 count = len + count + 1 - start;
3500 if (start + count > (int64_t)len)
Cyrill Gorcunovcff031e2010-09-07 20:31:11 +04003501 count = len - start;
3502 if (!len || count < 0 || start >=(int64_t)len)
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003503 start = -1, count = 0; /* empty string */
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003504
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003505 macro_start = nasm_malloc(sizeof(*macro_start));
3506 macro_start->next = NULL;
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003507 macro_start->text = nasm_quote((start < 0) ? "" : t->text + start, count);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003508 macro_start->type = TOK_STRING;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003509 macro_start->a.mac = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003510
H. Peter Anvine2c80182005-01-15 22:15:51 +00003511 /*
3512 * We now have a macro name, an implicit parameter count of
3513 * zero, and a numeric token to use as an expansion. Create
3514 * and store an SMacro.
3515 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003516 define_smacro(ctx, mname, casesense, 0, macro_start);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003517 free_tlist(tline);
3518 free_tlist(origline);
3519 return DIRECTIVE_FOUND;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003520 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003521
H. Peter Anvine2c80182005-01-15 22:15:51 +00003522 case PP_ASSIGN:
3523 case PP_IASSIGN:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003524 casesense = (i == PP_ASSIGN);
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07003525
H. Peter Anvine2c80182005-01-15 22:15:51 +00003526 tline = tline->next;
3527 skip_white_(tline);
3528 tline = expand_id(tline);
3529 if (!tline || (tline->type != TOK_ID &&
3530 (tline->type != TOK_PREPROC_ID ||
3531 tline->text[1] != '$'))) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003532 nasm_nonfatal("`%%%sassign' expects a macro identifier",
3533 (i == PP_IASSIGN ? "i" : ""));
H. Peter Anvine2c80182005-01-15 22:15:51 +00003534 free_tlist(origline);
3535 return DIRECTIVE_FOUND;
3536 }
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04003537 ctx = get_ctx(tline->text, &mname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003538 last = tline;
3539 tline = expand_smacro(tline->next);
3540 last->next = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003541
H. Peter Anvine2c80182005-01-15 22:15:51 +00003542 t = tline;
3543 tptr = &t;
3544 tokval.t_type = TOKEN_INVALID;
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08003545 evalresult = evaluate(ppscan, tptr, &tokval, NULL, true, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003546 free_tlist(tline);
3547 if (!evalresult) {
3548 free_tlist(origline);
3549 return DIRECTIVE_FOUND;
3550 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003551
H. Peter Anvine2c80182005-01-15 22:15:51 +00003552 if (tokval.t_type)
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08003553 nasm_warn(WARN_OTHER, "trailing garbage after expression ignored");
H. Peter Anvin734b1882002-04-30 21:01:08 +00003554
H. Peter Anvine2c80182005-01-15 22:15:51 +00003555 if (!is_simple(evalresult)) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003556 nasm_nonfatal("non-constant value given to `%%%sassign'",
3557 (i == PP_IASSIGN ? "i" : ""));
H. Peter Anvine2c80182005-01-15 22:15:51 +00003558 free_tlist(origline);
3559 return DIRECTIVE_FOUND;
3560 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003561
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003562 macro_start = nasm_malloc(sizeof(*macro_start));
3563 macro_start->next = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003564 make_tok_num(macro_start, reloc_value(evalresult));
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003565 macro_start->a.mac = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003566
H. Peter Anvine2c80182005-01-15 22:15:51 +00003567 /*
3568 * We now have a macro name, an implicit parameter count of
3569 * zero, and a numeric token to use as an expansion. Create
3570 * and store an SMacro.
3571 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003572 define_smacro(ctx, mname, casesense, 0, macro_start);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003573 free_tlist(origline);
3574 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003575
H. Peter Anvine2c80182005-01-15 22:15:51 +00003576 case PP_LINE:
3577 /*
3578 * Syntax is `%line nnn[+mmm] [filename]'
3579 */
H. Peter Anvin (Intel)800c1682018-12-14 12:22:11 -08003580 if (unlikely(pp_noline)) {
3581 free_tlist(origline);
3582 return DIRECTIVE_FOUND;
3583 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00003584 tline = tline->next;
3585 skip_white_(tline);
3586 if (!tok_type_(tline, TOK_NUMBER)) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003587 nasm_nonfatal("`%%line' expects line number");
H. Peter Anvine2c80182005-01-15 22:15:51 +00003588 free_tlist(origline);
3589 return DIRECTIVE_FOUND;
3590 }
H. Peter Anvin70055962007-10-11 00:05:31 -07003591 k = readnum(tline->text, &err);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003592 m = 1;
3593 tline = tline->next;
3594 if (tok_is_(tline, "+")) {
3595 tline = tline->next;
3596 if (!tok_type_(tline, TOK_NUMBER)) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003597 nasm_nonfatal("`%%line' expects line increment");
H. Peter Anvine2c80182005-01-15 22:15:51 +00003598 free_tlist(origline);
3599 return DIRECTIVE_FOUND;
3600 }
H. Peter Anvin70055962007-10-11 00:05:31 -07003601 m = readnum(tline->text, &err);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003602 tline = tline->next;
3603 }
3604 skip_white_(tline);
3605 src_set_linnum(k);
3606 istk->lineinc = m;
3607 if (tline) {
H. Peter Anvin274cda82016-05-10 02:56:29 -07003608 char *fname = detoken(tline, false);
3609 src_set_fname(fname);
3610 nasm_free(fname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003611 }
3612 free_tlist(origline);
3613 return DIRECTIVE_FOUND;
Keith Kaniosb307a4f2010-11-06 17:41:51 -05003614
H. Peter Anvine2c80182005-01-15 22:15:51 +00003615 default:
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003616 nasm_fatal("preprocessor directive `%s' not yet implemented",
3617 pp_directives[i]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003618 return DIRECTIVE_FOUND;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003619 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003620}
3621
3622/*
H. Peter Anvin76690a12002-04-30 20:52:49 +00003623 * Ensure that a macro parameter contains a condition code and
3624 * nothing else. Return the condition code index if so, or -1
3625 * otherwise.
3626 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003627static int find_cc(Token * t)
H. Peter Anvineba20a72002-04-30 20:53:55 +00003628{
H. Peter Anvin76690a12002-04-30 20:52:49 +00003629 Token *tt;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003630
H. Peter Anvin25a99342007-09-22 17:45:45 -07003631 if (!t)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003632 return -1; /* Probably a %+ without a space */
H. Peter Anvin25a99342007-09-22 17:45:45 -07003633
H. Peter Anvineba20a72002-04-30 20:53:55 +00003634 skip_white_(t);
Cyrill Gorcunov7524cfd2017-10-22 19:01:16 +03003635 if (!t)
3636 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003637 if (t->type != TOK_ID)
H. Peter Anvine2c80182005-01-15 22:15:51 +00003638 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003639 tt = t->next;
H. Peter Anvineba20a72002-04-30 20:53:55 +00003640 skip_white_(tt);
H. Peter Anvin76690a12002-04-30 20:52:49 +00003641 if (tt && (tt->type != TOK_OTHER || strcmp(tt->text, ",")))
H. Peter Anvine2c80182005-01-15 22:15:51 +00003642 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003643
Cyrill Gorcunov19456392012-05-02 00:18:56 +04003644 return bsii(t->text, (const char **)conditions, ARRAY_SIZE(conditions));
H. Peter Anvin76690a12002-04-30 20:52:49 +00003645}
3646
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003647/*
3648 * This routines walks over tokens strem and hadnles tokens
3649 * pasting, if @handle_explicit passed then explicit pasting
3650 * term is handled, otherwise -- implicit pastings only.
3651 */
Cyrill Gorcunov575d4282010-10-06 00:25:55 +04003652static bool paste_tokens(Token **head, const struct tokseq_match *m,
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003653 size_t mnum, bool handle_explicit)
H. Peter Anvind784a082009-04-20 14:01:18 -07003654{
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003655 Token *tok, *next, **prev_next, **prev_nonspace;
3656 bool pasted = false;
3657 char *buf, *p;
3658 size_t len, i;
H. Peter Anvind784a082009-04-20 14:01:18 -07003659
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003660 /*
3661 * The last token before pasting. We need it
3662 * to be able to connect new handled tokens.
3663 * In other words if there were a tokens stream
3664 *
3665 * A -> B -> C -> D
3666 *
3667 * and we've joined tokens B and C, the resulting
3668 * stream should be
3669 *
3670 * A -> BC -> D
3671 */
3672 tok = *head;
3673 prev_next = NULL;
3674
3675 if (!tok_type_(tok, TOK_WHITESPACE) && !tok_type_(tok, TOK_PASTE))
3676 prev_nonspace = head;
3677 else
3678 prev_nonspace = NULL;
3679
3680 while (tok && (next = tok->next)) {
3681
3682 switch (tok->type) {
H. Peter Anvind784a082009-04-20 14:01:18 -07003683 case TOK_WHITESPACE:
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003684 /* Zap redundant whitespaces */
3685 while (tok_type_(next, TOK_WHITESPACE))
3686 next = delete_Token(next);
3687 tok->next = next;
H. Peter Anvind784a082009-04-20 14:01:18 -07003688 break;
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003689
3690 case TOK_PASTE:
3691 /* Explicit pasting */
3692 if (!handle_explicit)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003693 break;
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003694 next = delete_Token(tok);
3695
3696 while (tok_type_(next, TOK_WHITESPACE))
3697 next = delete_Token(next);
3698
3699 if (!pasted)
3700 pasted = true;
3701
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003702 /* Left pasting token is start of line */
3703 if (!prev_nonspace)
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003704 nasm_fatal("No lvalue found on pasting");
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003705
Cyrill Gorcunov8b5c9fb2013-02-04 01:24:54 +04003706 /*
3707 * No ending token, this might happen in two
3708 * cases
3709 *
3710 * 1) There indeed no right token at all
3711 * 2) There is a bare "%define ID" statement,
3712 * and @ID does expand to whitespace.
3713 *
3714 * So technically we need to do a grammar analysis
3715 * in another stage of parsing, but for now lets don't
3716 * change the behaviour people used to. Simply allow
3717 * whitespace after paste token.
3718 */
3719 if (!next) {
3720 /*
3721 * Zap ending space tokens and that's all.
3722 */
3723 tok = (*prev_nonspace)->next;
3724 while (tok_type_(tok, TOK_WHITESPACE))
3725 tok = delete_Token(tok);
3726 tok = *prev_nonspace;
3727 tok->next = NULL;
3728 break;
3729 }
3730
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003731 tok = *prev_nonspace;
3732 while (tok_type_(tok, TOK_WHITESPACE))
3733 tok = delete_Token(tok);
3734 len = strlen(tok->text);
3735 len += strlen(next->text);
3736
3737 p = buf = nasm_malloc(len + 1);
3738 strcpy(p, tok->text);
3739 p = strchr(p, '\0');
3740 strcpy(p, next->text);
3741
3742 delete_Token(tok);
3743
3744 tok = tokenize(buf);
3745 nasm_free(buf);
3746
3747 *prev_nonspace = tok;
3748 while (tok && tok->next)
3749 tok = tok->next;
3750
3751 tok->next = delete_Token(next);
3752
3753 /* Restart from pasted tokens head */
3754 tok = *prev_nonspace;
3755 break;
3756
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003757 default:
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003758 /* implicit pasting */
Cyrill Gorcunov575d4282010-10-06 00:25:55 +04003759 for (i = 0; i < mnum; i++) {
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003760 if (!(PP_CONCAT_MATCH(tok, m[i].mask_head)))
3761 continue;
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +04003762
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003763 len = 0;
3764 while (next && PP_CONCAT_MATCH(next, m[i].mask_tail)) {
3765 len += strlen(next->text);
3766 next = next->next;
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +04003767 }
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003768
Cyrill Gorcunov6f8109e2017-10-22 21:26:36 +03003769 /* No match or no text to process */
3770 if (tok == next || len == 0)
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003771 break;
3772
3773 len += strlen(tok->text);
3774 p = buf = nasm_malloc(len + 1);
3775
Adam Majer1a069432017-07-25 11:12:35 +02003776 strcpy(p, tok->text);
3777 p = strchr(p, '\0');
3778 tok = delete_Token(tok);
3779
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003780 while (tok != next) {
Adam Majer1a069432017-07-25 11:12:35 +02003781 if (PP_CONCAT_MATCH(tok, m[i].mask_tail)) {
3782 strcpy(p, tok->text);
3783 p = strchr(p, '\0');
3784 }
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003785 tok = delete_Token(tok);
3786 }
3787
3788 tok = tokenize(buf);
3789 nasm_free(buf);
3790
3791 if (prev_next)
3792 *prev_next = tok;
3793 else
3794 *head = tok;
3795
3796 /*
3797 * Connect pasted into original stream,
3798 * ie A -> new-tokens -> B
3799 */
3800 while (tok && tok->next)
3801 tok = tok->next;
3802 tok->next = next;
3803
3804 if (!pasted)
3805 pasted = true;
3806
3807 /* Restart from pasted tokens head */
3808 tok = prev_next ? *prev_next : *head;
Cyrill Gorcunov575d4282010-10-06 00:25:55 +04003809 }
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003810
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003811 break;
H. Peter Anvind784a082009-04-20 14:01:18 -07003812 }
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003813
3814 prev_next = &tok->next;
3815
3816 if (tok->next &&
3817 !tok_type_(tok->next, TOK_WHITESPACE) &&
3818 !tok_type_(tok->next, TOK_PASTE))
3819 prev_nonspace = prev_next;
3820
3821 tok = tok->next;
H. Peter Anvind784a082009-04-20 14:01:18 -07003822 }
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003823
3824 return pasted;
H. Peter Anvind784a082009-04-20 14:01:18 -07003825}
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003826
3827/*
3828 * expands to a list of tokens from %{x:y}
3829 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003830static Token *expand_mmac_params_range(MMacro *mac, Token *tline, Token ***last)
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003831{
3832 Token *t = tline, **tt, *tm, *head;
3833 char *pos;
3834 int fst, lst, j, i;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003835
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003836 pos = strchr(tline->text, ':');
3837 nasm_assert(pos);
3838
3839 lst = atoi(pos + 1);
3840 fst = atoi(tline->text + 1);
3841
3842 /*
3843 * only macros params are accounted so
3844 * if someone passes %0 -- we reject such
3845 * value(s)
3846 */
3847 if (lst == 0 || fst == 0)
3848 goto err;
3849
3850 /* the values should be sane */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003851 if ((fst > (int)mac->nparam || fst < (-(int)mac->nparam)) ||
3852 (lst > (int)mac->nparam || lst < (-(int)mac->nparam)))
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003853 goto err;
3854
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003855 fst = fst < 0 ? fst + (int)mac->nparam + 1: fst;
3856 lst = lst < 0 ? lst + (int)mac->nparam + 1: lst;
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003857
3858 /* counted from zero */
3859 fst--, lst--;
3860
3861 /*
Cyrill Gorcunove75331c2013-11-09 12:02:15 +04003862 * It will be at least one token. Note we
3863 * need to scan params until separator, otherwise
3864 * only first token will be passed.
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003865 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003866 tm = mac->params[(fst + mac->rotate) % mac->nparam];
Cyrill Gorcunov67f2ca22018-10-13 19:41:01 +03003867 if (!tm)
3868 goto err;
Cyrill Gorcunove75331c2013-11-09 12:02:15 +04003869 head = new_Token(NULL, tm->type, tm->text, 0);
3870 tt = &head->next, tm = tm->next;
3871 while (tok_isnt_(tm, ",")) {
3872 t = new_Token(NULL, tm->type, tm->text, 0);
3873 *tt = t, tt = &t->next, tm = tm->next;
3874 }
3875
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003876 if (fst < lst) {
3877 for (i = fst + 1; i <= lst; i++) {
3878 t = new_Token(NULL, TOK_OTHER, ",", 0);
3879 *tt = t, tt = &t->next;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003880 j = (i + mac->rotate) % mac->nparam;
3881 tm = mac->params[j];
Cyrill Gorcunove75331c2013-11-09 12:02:15 +04003882 while (tok_isnt_(tm, ",")) {
3883 t = new_Token(NULL, tm->type, tm->text, 0);
3884 *tt = t, tt = &t->next, tm = tm->next;
3885 }
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003886 }
3887 } else {
3888 for (i = fst - 1; i >= lst; i--) {
3889 t = new_Token(NULL, TOK_OTHER, ",", 0);
3890 *tt = t, tt = &t->next;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003891 j = (i + mac->rotate) % mac->nparam;
3892 tm = mac->params[j];
Cyrill Gorcunove75331c2013-11-09 12:02:15 +04003893 while (tok_isnt_(tm, ",")) {
3894 t = new_Token(NULL, tm->type, tm->text, 0);
3895 *tt = t, tt = &t->next, tm = tm->next;
3896 }
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003897 }
3898 }
3899
3900 *last = tt;
3901 return head;
3902
3903err:
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003904 nasm_nonfatal("`%%{%s}': macro parameters out of range",
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003905 &tline->text[1]);
3906 return tline;
3907}
3908
H. Peter Anvin76690a12002-04-30 20:52:49 +00003909/*
3910 * Expand MMacro-local things: parameter references (%0, %n, %+n,
H. Peter Anvin67c63722008-10-26 23:49:00 -07003911 * %-n) and MMacro-local identifiers (%%foo) as well as
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003912 * macro indirection (%[...]) and range (%{..:..}).
H. Peter Anvin76690a12002-04-30 20:52:49 +00003913 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003914static Token *expand_mmac_params(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00003915{
H. Peter Anvin734b1882002-04-30 21:01:08 +00003916 Token *t, *tt, **tail, *thead;
H. Peter Anvin6125b622009-04-08 14:02:25 -07003917 bool changed = false;
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003918 char *pos;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003919
3920 tail = &thead;
3921 thead = NULL;
3922
H. Peter Anvine2c80182005-01-15 22:15:51 +00003923 while (tline) {
Cyrill Gorcunov661f7232018-10-28 20:39:34 +03003924 if (tline->type == TOK_PREPROC_ID && tline->text && tline->text[0] &&
Cyrill Gorcunovca611192010-06-04 09:22:12 +04003925 (((tline->text[1] == '+' || tline->text[1] == '-') && tline->text[2]) ||
3926 (tline->text[1] >= '0' && tline->text[1] <= '9') ||
3927 tline->text[1] == '%')) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00003928 char *text = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003929 int type = 0, cc; /* type = 0 to placate optimisers */
Keith Kaniosa6dfa782007-04-13 16:47:53 +00003930 char tmpbuf[30];
H. Peter Anvin25a99342007-09-22 17:45:45 -07003931 unsigned int n;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003932 int i;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003933 MMacro *mac;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003934
H. Peter Anvine2c80182005-01-15 22:15:51 +00003935 t = tline;
3936 tline = tline->next;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003937
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003938 mac = istk->mstk;
3939 while (mac && !mac->name) /* avoid mistaking %reps for macros */
3940 mac = mac->next_active;
3941 if (!mac) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003942 nasm_nonfatal("`%s': not in a macro call", t->text);
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003943 } else {
3944 pos = strchr(t->text, ':');
3945 if (!pos) {
3946 switch (t->text[1]) {
3947 /*
3948 * We have to make a substitution of one of the
3949 * forms %1, %-1, %+1, %%foo, %0.
3950 */
3951 case '0':
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003952 type = TOK_NUMBER;
3953 snprintf(tmpbuf, sizeof(tmpbuf), "%d", mac->nparam);
3954 text = nasm_strdup(tmpbuf);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003955 break;
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003956 case '%':
H. Peter Anvine2c80182005-01-15 22:15:51 +00003957 type = TOK_ID;
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003958 snprintf(tmpbuf, sizeof(tmpbuf), "..@%"PRIu64".",
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003959 mac->unique);
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003960 text = nasm_strcat(tmpbuf, t->text + 2);
3961 break;
3962 case '-':
3963 n = atoi(t->text + 2) - 1;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003964 if (n >= mac->nparam)
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003965 tt = NULL;
3966 else {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003967 if (mac->nparam > 1)
3968 n = (n + mac->rotate) % mac->nparam;
3969 tt = mac->params[n];
H. Peter Anvine2c80182005-01-15 22:15:51 +00003970 }
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003971 cc = find_cc(tt);
3972 if (cc == -1) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003973 nasm_nonfatal("macro parameter %d is not a condition code",
3974 n + 1);
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003975 text = NULL;
3976 } else {
3977 type = TOK_ID;
3978 if (inverse_ccs[cc] == -1) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003979 nasm_nonfatal("condition code `%s' is not invertible",
3980 conditions[cc]);
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003981 text = NULL;
3982 } else
3983 text = nasm_strdup(conditions[inverse_ccs[cc]]);
3984 }
3985 break;
3986 case '+':
3987 n = atoi(t->text + 2) - 1;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003988 if (n >= mac->nparam)
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003989 tt = NULL;
3990 else {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003991 if (mac->nparam > 1)
3992 n = (n + mac->rotate) % mac->nparam;
3993 tt = mac->params[n];
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003994 }
3995 cc = find_cc(tt);
3996 if (cc == -1) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003997 nasm_nonfatal("macro parameter %d is not a condition code",
3998 n + 1);
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003999 text = NULL;
4000 } else {
4001 type = TOK_ID;
4002 text = nasm_strdup(conditions[cc]);
4003 }
4004 break;
4005 default:
4006 n = atoi(t->text + 1) - 1;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004007 if (n >= mac->nparam)
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004008 tt = NULL;
4009 else {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004010 if (mac->nparam > 1)
4011 n = (n + mac->rotate) % mac->nparam;
4012 tt = mac->params[n];
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004013 }
4014 if (tt) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004015 for (i = 0; i < mac->paramlen[n]; i++) {
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004016 *tail = new_Token(NULL, tt->type, tt->text, 0);
4017 tail = &(*tail)->next;
4018 tt = tt->next;
4019 }
4020 }
4021 text = NULL; /* we've done it here */
4022 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004023 }
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004024 } else {
4025 /*
4026 * seems we have a parameters range here
4027 */
4028 Token *head, **last;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004029 head = expand_mmac_params_range(mac, t, &last);
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004030 if (head != t) {
4031 *tail = head;
4032 *last = tline;
4033 tline = head;
4034 text = NULL;
4035 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00004036 }
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004037 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00004038 if (!text) {
4039 delete_Token(t);
4040 } else {
4041 *tail = t;
4042 tail = &t->next;
4043 t->type = type;
4044 nasm_free(t->text);
4045 t->text = text;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004046 t->a.mac = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004047 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004048 changed = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004049 continue;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004050 } else if (tline->type == TOK_INDIRECT) {
4051 t = tline;
4052 tline = tline->next;
4053 tt = tokenize(t->text);
4054 tt = expand_mmac_params(tt);
4055 tt = expand_smacro(tt);
4056 *tail = tt;
4057 while (tt) {
4058 tt->a.mac = NULL; /* Necessary? */
4059 tail = &tt->next;
4060 tt = tt->next;
4061 }
4062 delete_Token(t);
4063 changed = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004064 } else {
4065 t = *tail = tline;
4066 tline = tline->next;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004067 t->a.mac = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004068 tail = &t->next;
4069 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00004070 }
H. Peter Anvineba20a72002-04-30 20:53:55 +00004071 *tail = NULL;
H. Peter Anvin67c63722008-10-26 23:49:00 -07004072
Cyrill Gorcunovc6a742c2011-06-27 01:23:09 +04004073 if (changed) {
4074 const struct tokseq_match t[] = {
4075 {
4076 PP_CONCAT_MASK(TOK_ID) |
4077 PP_CONCAT_MASK(TOK_FLOAT), /* head */
4078 PP_CONCAT_MASK(TOK_ID) |
4079 PP_CONCAT_MASK(TOK_NUMBER) |
4080 PP_CONCAT_MASK(TOK_FLOAT) |
4081 PP_CONCAT_MASK(TOK_OTHER) /* tail */
4082 },
4083 {
4084 PP_CONCAT_MASK(TOK_NUMBER), /* head */
4085 PP_CONCAT_MASK(TOK_NUMBER) /* tail */
4086 }
4087 };
4088 paste_tokens(&thead, t, ARRAY_SIZE(t), false);
4089 }
H. Peter Anvin6125b622009-04-08 14:02:25 -07004090
H. Peter Anvin76690a12002-04-30 20:52:49 +00004091 return thead;
4092}
4093
4094/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004095 * Expand all single-line macro calls made in the given line.
4096 * Return the expanded version of the line. The original is deemed
4097 * to be destroyed in the process. (In reality we'll just move
4098 * Tokens from input to output a lot of the time, rather than
4099 * actually bothering to destroy and replicate.)
4100 */
H. Peter Anvincb1cf592007-11-19 12:26:50 -08004101
H. Peter Anvine2c80182005-01-15 22:15:51 +00004102static Token *expand_smacro(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004103{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004104 Token *t, *tt, *mstart, **tail, *thead;
H. Peter Anvineba20a72002-04-30 20:53:55 +00004105 SMacro *head = NULL, *m;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004106 Token **params;
4107 int *paramsize;
H. Peter Anvin25a99342007-09-22 17:45:45 -07004108 unsigned int nparam, sparam;
H. Peter Anvind784a082009-04-20 14:01:18 -07004109 int brackets;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004110 Token *org_tline = tline;
4111 Context *ctx;
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08004112 const char *mname;
H. Peter Anvina3d96d02018-06-15 17:51:39 -07004113 int64_t deadman = nasm_limit[LIMIT_MACROS];
H. Peter Anvin8287daf2009-07-07 16:00:58 -07004114 bool expanded;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004115
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004116 /*
4117 * Trick: we should avoid changing the start token pointer since it can
4118 * be contained in "next" field of other token. Because of this
4119 * we allocate a copy of first token and work with it; at the end of
4120 * routine we copy it back
4121 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004122 if (org_tline) {
Cyrill Gorcunoved4a8052010-04-09 15:40:35 +04004123 tline = new_Token(org_tline->next, org_tline->type,
4124 org_tline->text, 0);
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004125 tline->a.mac = org_tline->a.mac;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004126 nasm_free(org_tline->text);
4127 org_tline->text = NULL;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004128 }
4129
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004130 expanded = true; /* Always expand %+ at least once */
H. Peter Anvin8287daf2009-07-07 16:00:58 -07004131
H. Peter Anvincb1cf592007-11-19 12:26:50 -08004132again:
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004133 thead = NULL;
Cyrill Gorcunoved4a8052010-04-09 15:40:35 +04004134 tail = &thead;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004135
H. Peter Anvine2c80182005-01-15 22:15:51 +00004136 while (tline) { /* main token loop */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004137 if (!--deadman) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03004138 nasm_nonfatal("interminable macro recursion");
Cyrill Gorcunovbd38c8f2009-11-21 11:11:23 +03004139 goto err;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004140 }
H. Peter Anvincb1cf592007-11-19 12:26:50 -08004141
H. Peter Anvine2c80182005-01-15 22:15:51 +00004142 if ((mname = tline->text)) {
4143 /* if this token is a local macro, look in local context */
Cyrill Gorcunovc56d9ad2010-02-11 15:12:19 +03004144 if (tline->type == TOK_ID) {
4145 head = (SMacro *)hash_findix(&smacros, mname);
4146 } else if (tline->type == TOK_PREPROC_ID) {
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04004147 ctx = get_ctx(mname, &mname);
Cyrill Gorcunovc56d9ad2010-02-11 15:12:19 +03004148 head = ctx ? (SMacro *)hash_findix(&ctx->localmac, mname) : NULL;
4149 } else
4150 head = NULL;
H. Peter Anvin072771e2008-05-22 13:17:51 -07004151
H. Peter Anvine2c80182005-01-15 22:15:51 +00004152 /*
4153 * We've hit an identifier. As in is_mmacro below, we first
4154 * check whether the identifier is a single-line macro at
4155 * all, then think about checking for parameters if
4156 * necessary.
4157 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004158 list_for_each(m, head)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004159 if (!mstrcmp(m->name, mname, m->casesense))
4160 break;
4161 if (m) {
4162 mstart = tline;
4163 params = NULL;
4164 paramsize = NULL;
4165 if (m->nparam == 0) {
4166 /*
4167 * Simple case: the macro is parameterless. Discard the
4168 * one token that the macro call took, and push the
4169 * expansion back on the to-do stack.
4170 */
4171 if (!m->expansion) {
4172 if (!strcmp("__FILE__", m->name)) {
H. Peter Anvin274cda82016-05-10 02:56:29 -07004173 const char *file = src_get_fname();
4174 /* nasm_free(tline->text); here? */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004175 tline->text = nasm_quote(file, strlen(file));
4176 tline->type = TOK_STRING;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004177 continue;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004178 }
4179 if (!strcmp("__LINE__", m->name)) {
4180 nasm_free(tline->text);
4181 make_tok_num(tline, src_get_linnum());
4182 continue;
4183 }
4184 if (!strcmp("__BITS__", m->name)) {
4185 nasm_free(tline->text);
4186 make_tok_num(tline, globalbits);
4187 continue;
4188 }
4189 tline = delete_Token(tline);
4190 continue;
4191 }
4192 } else {
4193 /*
4194 * Complicated case: at least one macro with this name
H. Peter Anvine2c80182005-01-15 22:15:51 +00004195 * exists and takes parameters. We must find the
4196 * parameters in the call, count them, find the SMacro
4197 * that corresponds to that form of the macro call, and
4198 * substitute for the parameters when we expand. What a
4199 * pain.
4200 */
4201 /*tline = tline->next;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004202 skip_white_(tline); */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004203 do {
4204 t = tline->next;
4205 while (tok_type_(t, TOK_SMAC_END)) {
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004206 t->a.mac->in_progress = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004207 t->text = NULL;
4208 t = tline->next = delete_Token(t);
4209 }
4210 tline = t;
4211 } while (tok_type_(tline, TOK_WHITESPACE));
4212 if (!tok_is_(tline, "(")) {
4213 /*
4214 * This macro wasn't called with parameters: ignore
4215 * the call. (Behaviour borrowed from gnu cpp.)
4216 */
4217 tline = mstart;
4218 m = NULL;
4219 } else {
4220 int paren = 0;
4221 int white = 0;
4222 brackets = 0;
4223 nparam = 0;
4224 sparam = PARAM_DELTA;
4225 params = nasm_malloc(sparam * sizeof(Token *));
4226 params[0] = tline->next;
4227 paramsize = nasm_malloc(sparam * sizeof(int));
4228 paramsize[0] = 0;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07004229 while (true) { /* parameter loop */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004230 /*
4231 * For some unusual expansions
4232 * which concatenates function call
4233 */
4234 t = tline->next;
4235 while (tok_type_(t, TOK_SMAC_END)) {
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004236 t->a.mac->in_progress = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004237 t->text = NULL;
4238 t = tline->next = delete_Token(t);
4239 }
4240 tline = t;
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00004241
H. Peter Anvine2c80182005-01-15 22:15:51 +00004242 if (!tline) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03004243 nasm_nonfatal("macro call expects terminating `)'");
H. Peter Anvine2c80182005-01-15 22:15:51 +00004244 break;
4245 }
4246 if (tline->type == TOK_WHITESPACE
4247 && brackets <= 0) {
4248 if (paramsize[nparam])
4249 white++;
4250 else
4251 params[nparam] = tline->next;
4252 continue; /* parameter loop */
4253 }
4254 if (tline->type == TOK_OTHER
4255 && tline->text[1] == 0) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004256 char ch = tline->text[0];
H. Peter Anvine2c80182005-01-15 22:15:51 +00004257 if (ch == ',' && !paren && brackets <= 0) {
4258 if (++nparam >= sparam) {
4259 sparam += PARAM_DELTA;
4260 params = nasm_realloc(params,
Cyrill Gorcunoved4a8052010-04-09 15:40:35 +04004261 sparam * sizeof(Token *));
4262 paramsize = nasm_realloc(paramsize,
4263 sparam * sizeof(int));
H. Peter Anvine2c80182005-01-15 22:15:51 +00004264 }
4265 params[nparam] = tline->next;
4266 paramsize[nparam] = 0;
4267 white = 0;
4268 continue; /* parameter loop */
4269 }
4270 if (ch == '{' &&
4271 (brackets > 0 || (brackets == 0 &&
4272 !paramsize[nparam])))
4273 {
4274 if (!(brackets++)) {
4275 params[nparam] = tline->next;
4276 continue; /* parameter loop */
4277 }
4278 }
4279 if (ch == '}' && brackets > 0)
4280 if (--brackets == 0) {
4281 brackets = -1;
4282 continue; /* parameter loop */
4283 }
4284 if (ch == '(' && !brackets)
4285 paren++;
4286 if (ch == ')' && brackets <= 0)
4287 if (--paren < 0)
4288 break;
4289 }
4290 if (brackets < 0) {
4291 brackets = 0;
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03004292 nasm_nonfatal("braces do not "
H. Peter Anvine2c80182005-01-15 22:15:51 +00004293 "enclose all of macro parameter");
4294 }
4295 paramsize[nparam] += white + 1;
4296 white = 0;
4297 } /* parameter loop */
4298 nparam++;
4299 while (m && (m->nparam != nparam ||
4300 mstrcmp(m->name, mname,
4301 m->casesense)))
4302 m = m->next;
H. Peter Anvin (Intel)723ab482018-12-13 21:53:31 -08004303 if (!m) {
4304 /*!
4305 *!macro-params [on] macro calls with wrong parameter count
4306 *! covers warnings about \i{multi-line macros} being invoked
4307 *! with the wrong number of parameters. See \k{mlmacover} for an
4308 *! example of why you might want to disable this warning.
4309 */
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08004310 nasm_warn(WARN_MACRO_PARAMS,
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03004311 "macro `%s' exists, "
4312 "but not taking %d parameters",
4313 mstart->text, nparam);
H. Peter Anvin (Intel)723ab482018-12-13 21:53:31 -08004314 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00004315 }
4316 }
4317 if (m && m->in_progress)
4318 m = NULL;
4319 if (!m) { /* in progess or didn't find '(' or wrong nparam */
H. Peter Anvin70653092007-10-19 14:42:29 -07004320 /*
H. Peter Anvine2c80182005-01-15 22:15:51 +00004321 * Design question: should we handle !tline, which
4322 * indicates missing ')' here, or expand those
4323 * macros anyway, which requires the (t) test a few
H. Peter Anvin70653092007-10-19 14:42:29 -07004324 * lines down?
H. Peter Anvine2c80182005-01-15 22:15:51 +00004325 */
4326 nasm_free(params);
4327 nasm_free(paramsize);
4328 tline = mstart;
4329 } else {
4330 /*
4331 * Expand the macro: we are placed on the last token of the
4332 * call, so that we can easily split the call from the
4333 * following tokens. We also start by pushing an SMAC_END
4334 * token for the cycle removal.
4335 */
4336 t = tline;
4337 if (t) {
4338 tline = t->next;
4339 t->next = NULL;
4340 }
4341 tt = new_Token(tline, TOK_SMAC_END, NULL, 0);
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004342 tt->a.mac = m;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07004343 m->in_progress = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004344 tline = tt;
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04004345 list_for_each(t, m->expansion) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004346 if (t->type >= TOK_SMAC_PARAM) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00004347 Token *pcopy = tline, **ptail = &pcopy;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004348 Token *ttt, *pt;
4349 int i;
H. Peter Anvin734b1882002-04-30 21:01:08 +00004350
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004351 ttt = params[t->type - TOK_SMAC_PARAM];
4352 i = paramsize[t->type - TOK_SMAC_PARAM];
4353 while (--i >= 0) {
4354 pt = *ptail = new_Token(tline, ttt->type,
4355 ttt->text, 0);
4356 ptail = &pt->next;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004357 ttt = ttt->next;
Cyrill Gorcunov59ce1c62017-10-22 18:42:07 +03004358 if (!ttt && i > 0) {
4359 /*
4360 * FIXME: Need to handle more gracefully,
4361 * exiting early on agruments analysis.
4362 */
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03004363 nasm_fatal("macro `%s' expects %d args",
Cyrill Gorcunov59ce1c62017-10-22 18:42:07 +03004364 mstart->text,
4365 (int)paramsize[t->type - TOK_SMAC_PARAM]);
4366 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00004367 }
4368 tline = pcopy;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004369 } else if (t->type == TOK_PREPROC_Q) {
4370 tt = new_Token(tline, TOK_ID, mname, 0);
4371 tline = tt;
4372 } else if (t->type == TOK_PREPROC_QQ) {
4373 tt = new_Token(tline, TOK_ID, m->name, 0);
4374 tline = tt;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004375 } else {
4376 tt = new_Token(tline, t->type, t->text, 0);
4377 tline = tt;
4378 }
4379 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00004380
H. Peter Anvine2c80182005-01-15 22:15:51 +00004381 /*
4382 * Having done that, get rid of the macro call, and clean
4383 * up the parameters.
4384 */
4385 nasm_free(params);
4386 nasm_free(paramsize);
4387 free_tlist(mstart);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004388 expanded = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004389 continue; /* main token loop */
4390 }
4391 }
4392 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004393
H. Peter Anvine2c80182005-01-15 22:15:51 +00004394 if (tline->type == TOK_SMAC_END) {
Cyrill Gorcunov980dd652018-10-14 19:25:32 +03004395 /* On error path it might already be dropped */
4396 if (tline->a.mac)
4397 tline->a.mac->in_progress = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004398 tline = delete_Token(tline);
4399 } else {
4400 t = *tail = tline;
4401 tline = tline->next;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004402 t->a.mac = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004403 t->next = NULL;
4404 tail = &t->next;
4405 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004406 }
4407
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004408 /*
4409 * Now scan the entire line and look for successive TOK_IDs that resulted
Keith Kaniosb7a89542007-04-12 02:40:54 +00004410 * after expansion (they can't be produced by tokenize()). The successive
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004411 * TOK_IDs should be concatenated.
4412 * Also we look for %+ tokens and concatenate the tokens before and after
4413 * them (without white spaces in between).
4414 */
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +04004415 if (expanded) {
Cyrill Gorcunovc6a742c2011-06-27 01:23:09 +04004416 const struct tokseq_match t[] = {
4417 {
4418 PP_CONCAT_MASK(TOK_ID) |
4419 PP_CONCAT_MASK(TOK_PREPROC_ID), /* head */
4420 PP_CONCAT_MASK(TOK_ID) |
4421 PP_CONCAT_MASK(TOK_PREPROC_ID) |
4422 PP_CONCAT_MASK(TOK_NUMBER) /* tail */
4423 }
4424 };
4425 if (paste_tokens(&thead, t, ARRAY_SIZE(t), true)) {
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +04004426 /*
4427 * If we concatenated something, *and* we had previously expanded
4428 * an actual macro, scan the lines again for macros...
4429 */
4430 tline = thead;
4431 expanded = false;
4432 goto again;
4433 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00004434 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004435
Cyrill Gorcunovbd38c8f2009-11-21 11:11:23 +03004436err:
H. Peter Anvine2c80182005-01-15 22:15:51 +00004437 if (org_tline) {
4438 if (thead) {
4439 *org_tline = *thead;
4440 /* since we just gave text to org_line, don't free it */
4441 thead->text = NULL;
4442 delete_Token(thead);
4443 } else {
4444 /* the expression expanded to empty line;
4445 we can't return NULL for some reasons
4446 we just set the line to a single WHITESPACE token. */
4447 memset(org_tline, 0, sizeof(*org_tline));
4448 org_tline->text = NULL;
4449 org_tline->type = TOK_WHITESPACE;
4450 }
4451 thead = org_tline;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004452 }
4453
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004454 return thead;
4455}
4456
4457/*
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004458 * Similar to expand_smacro but used exclusively with macro identifiers
4459 * right before they are fetched in. The reason is that there can be
4460 * identifiers consisting of several subparts. We consider that if there
4461 * are more than one element forming the name, user wants a expansion,
4462 * otherwise it will be left as-is. Example:
4463 *
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004464 * %define %$abc cde
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004465 *
4466 * the identifier %$abc will be left as-is so that the handler for %define
4467 * will suck it and define the corresponding value. Other case:
4468 *
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004469 * %define _%$abc cde
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004470 *
4471 * In this case user wants name to be expanded *before* %define starts
4472 * working, so we'll expand %$abc into something (if it has a value;
4473 * otherwise it will be left as-is) then concatenate all successive
4474 * PP_IDs into one.
4475 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004476static Token *expand_id(Token * tline)
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004477{
4478 Token *cur, *oldnext = NULL;
4479
H. Peter Anvin734b1882002-04-30 21:01:08 +00004480 if (!tline || !tline->next)
H. Peter Anvine2c80182005-01-15 22:15:51 +00004481 return tline;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004482
4483 cur = tline;
4484 while (cur->next &&
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004485 (cur->next->type == TOK_ID ||
4486 cur->next->type == TOK_PREPROC_ID
4487 || cur->next->type == TOK_NUMBER))
H. Peter Anvine2c80182005-01-15 22:15:51 +00004488 cur = cur->next;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004489
4490 /* If identifier consists of just one token, don't expand */
4491 if (cur == tline)
H. Peter Anvine2c80182005-01-15 22:15:51 +00004492 return tline;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004493
H. Peter Anvine2c80182005-01-15 22:15:51 +00004494 if (cur) {
4495 oldnext = cur->next; /* Detach the tail past identifier */
4496 cur->next = NULL; /* so that expand_smacro stops here */
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004497 }
4498
H. Peter Anvin734b1882002-04-30 21:01:08 +00004499 tline = expand_smacro(tline);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004500
H. Peter Anvine2c80182005-01-15 22:15:51 +00004501 if (cur) {
4502 /* expand_smacro possibly changhed tline; re-scan for EOL */
4503 cur = tline;
4504 while (cur && cur->next)
4505 cur = cur->next;
4506 if (cur)
4507 cur->next = oldnext;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004508 }
4509
4510 return tline;
4511}
4512
4513/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004514 * Determine whether the given line constitutes a multi-line macro
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004515 * call, and return the MMacro structure called if so. Doesn't have
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004516 * to check for an initial label - that's taken care of in
4517 * expand_mmacro - but must check numbers of parameters. Guaranteed
4518 * to be called with tline->type == TOK_ID, so the putative macro
4519 * name is easy to find.
4520 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004521static MMacro *is_mmacro(Token * tline, Token *** params_array)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004522{
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004523 MMacro *head, *m;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004524 Token **params;
4525 int nparam;
4526
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004527 head = (MMacro *) hash_findix(&mmacros, tline->text);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004528
4529 /*
4530 * Efficiency: first we see if any macro exists with the given
4531 * name. If not, we can return NULL immediately. _Then_ we
4532 * count the parameters, and then we look further along the
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004533 * list if necessary to find the proper MMacro.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004534 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004535 list_for_each(m, head)
4536 if (!mstrcmp(m->name, tline->text, m->casesense))
H. Peter Anvine2c80182005-01-15 22:15:51 +00004537 break;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004538 if (!m)
H. Peter Anvine2c80182005-01-15 22:15:51 +00004539 return NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004540
4541 /*
4542 * OK, we have a potential macro. Count and demarcate the
4543 * parameters.
4544 */
H. Peter Anvin734b1882002-04-30 21:01:08 +00004545 count_mmac_params(tline->next, &nparam, &params);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004546
4547 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004548 * So we know how many parameters we've got. Find the MMacro
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004549 * structure that handles this number.
4550 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004551 while (m) {
4552 if (m->nparam_min <= nparam
4553 && (m->plus || nparam <= m->nparam_max)) {
4554 /*
4555 * This one is right. Just check if cycle removal
4556 * prohibits us using it before we actually celebrate...
4557 */
4558 if (m->in_progress > m->max_depth) {
4559 if (m->max_depth > 0) {
H. Peter Anvin (Intel)80c4f232018-12-14 13:33:24 -08004560 nasm_warn(WARN_OTHER, "reached maximum recursion depth of %i",
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03004561 m->max_depth);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004562 }
4563 nasm_free(params);
4564 return NULL;
4565 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00004566 /*
4567 * It's right, and we can use it. Add its default
4568 * parameters to the end of our list if necessary.
4569 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004570 if (m->defaults && nparam < m->nparam_min + m->ndefs) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00004571 params =
4572 nasm_realloc(params,
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004573 ((m->nparam_min + m->ndefs +
H. Peter Anvine2c80182005-01-15 22:15:51 +00004574 1) * sizeof(*params)));
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004575 while (nparam < m->nparam_min + m->ndefs) {
4576 params[nparam] = m->defaults[nparam - m->nparam_min];
H. Peter Anvine2c80182005-01-15 22:15:51 +00004577 nparam++;
4578 }
4579 }
4580 /*
4581 * If we've gone over the maximum parameter count (and
4582 * we're in Plus mode), ignore parameters beyond
4583 * nparam_max.
4584 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004585 if (m->plus && nparam > m->nparam_max)
4586 nparam = m->nparam_max;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004587 /*
4588 * Then terminate the parameter list, and leave.
4589 */
4590 if (!params) { /* need this special case */
4591 params = nasm_malloc(sizeof(*params));
4592 nparam = 0;
4593 }
4594 params[nparam] = NULL;
4595 *params_array = params;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004596 return m;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004597 }
4598 /*
4599 * This one wasn't right: look for the next one with the
4600 * same name.
4601 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004602 list_for_each(m, m->next)
4603 if (!mstrcmp(m->name, tline->text, m->casesense))
H. Peter Anvine2c80182005-01-15 22:15:51 +00004604 break;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004605 }
4606
4607 /*
4608 * After all that, we didn't find one with the right number of
4609 * parameters. Issue a warning, and fail to expand the macro.
4610 */
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08004611 nasm_warn(WARN_MACRO_PARAMS,
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03004612 "macro `%s' exists, but not taking %d parameters",
4613 tline->text, nparam);
H. Peter Anvin734b1882002-04-30 21:01:08 +00004614 nasm_free(params);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004615 return NULL;
4616}
4617
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004618
4619/*
4620 * Save MMacro invocation specific fields in
4621 * preparation for a recursive macro expansion
4622 */
4623static void push_mmacro(MMacro *m)
4624{
4625 MMacroInvocation *i;
4626
4627 i = nasm_malloc(sizeof(MMacroInvocation));
4628 i->prev = m->prev;
4629 i->params = m->params;
4630 i->iline = m->iline;
4631 i->nparam = m->nparam;
4632 i->rotate = m->rotate;
4633 i->paramlen = m->paramlen;
4634 i->unique = m->unique;
4635 i->condcnt = m->condcnt;
4636 m->prev = i;
4637}
4638
4639
4640/*
4641 * Restore MMacro invocation specific fields that were
4642 * saved during a previous recursive macro expansion
4643 */
4644static void pop_mmacro(MMacro *m)
4645{
4646 MMacroInvocation *i;
4647
4648 if (m->prev) {
4649 i = m->prev;
4650 m->prev = i->prev;
4651 m->params = i->params;
4652 m->iline = i->iline;
4653 m->nparam = i->nparam;
4654 m->rotate = i->rotate;
4655 m->paramlen = i->paramlen;
4656 m->unique = i->unique;
4657 m->condcnt = i->condcnt;
4658 nasm_free(i);
4659 }
4660}
4661
4662
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004663/*
4664 * Expand the multi-line macro call made by the given line, if
4665 * there is one to be expanded. If there is, push the expansion on
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004666 * istk->expansion and return 1. Otherwise return 0.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004667 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004668static int expand_mmacro(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004669{
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004670 Token *startline = tline;
H. Peter Anvineba20a72002-04-30 20:53:55 +00004671 Token *label = NULL;
4672 int dont_prepend = 0;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004673 Token **params, *t, *tt;
4674 MMacro *m;
4675 Line *l, *ll;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004676 int i, nparam, *paramlen;
H. Peter Anvinc751e862008-06-09 10:18:45 -07004677 const char *mname;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004678
4679 t = tline;
H. Peter Anvineba20a72002-04-30 20:53:55 +00004680 skip_white_(t);
H. Peter Anvince2233b2008-05-25 21:57:00 -07004681 /* if (!tok_type_(t, TOK_ID)) Lino 02/25/02 */
H. Peter Anvindce1e2f2002-04-30 21:06:37 +00004682 if (!tok_type_(t, TOK_ID) && !tok_type_(t, TOK_PREPROC_ID))
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004683 return 0;
4684 m = is_mmacro(t, &params);
4685 if (m) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004686 mname = t->text;
H. Peter Anvinc751e862008-06-09 10:18:45 -07004687 } else {
H. Peter Anvine2c80182005-01-15 22:15:51 +00004688 Token *last;
4689 /*
4690 * We have an id which isn't a macro call. We'll assume
4691 * it might be a label; we'll also check to see if a
4692 * colon follows it. Then, if there's another id after
4693 * that lot, we'll check it again for macro-hood.
4694 */
4695 label = last = t;
4696 t = t->next;
4697 if (tok_type_(t, TOK_WHITESPACE))
4698 last = t, t = t->next;
4699 if (tok_is_(t, ":")) {
4700 dont_prepend = 1;
4701 last = t, t = t->next;
4702 if (tok_type_(t, TOK_WHITESPACE))
4703 last = t, t = t->next;
4704 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004705 if (!tok_type_(t, TOK_ID) || !(m = is_mmacro(t, &params)))
4706 return 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004707 last->next = NULL;
Keith Kanios891775e2009-07-11 06:08:54 -05004708 mname = t->text;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004709 tline = t;
H. Peter Anvineba20a72002-04-30 20:53:55 +00004710 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004711
4712 /*
4713 * Fix up the parameters: this involves stripping leading and
4714 * trailing whitespace, then stripping braces if they are
4715 * present.
4716 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004717 for (nparam = 0; params[nparam]; nparam++) ;
H. Peter Anvin734b1882002-04-30 21:01:08 +00004718 paramlen = nparam ? nasm_malloc(nparam * sizeof(*paramlen)) : NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004719
H. Peter Anvine2c80182005-01-15 22:15:51 +00004720 for (i = 0; params[i]; i++) {
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08004721 int brace = 0;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004722 int comma = (!m->plus || i < nparam - 1);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004723
H. Peter Anvine2c80182005-01-15 22:15:51 +00004724 t = params[i];
4725 skip_white_(t);
4726 if (tok_is_(t, "{"))
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08004727 t = t->next, brace++, comma = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004728 params[i] = t;
4729 paramlen[i] = 0;
4730 while (t) {
4731 if (comma && t->type == TOK_OTHER && !strcmp(t->text, ","))
4732 break; /* ... because we have hit a comma */
4733 if (comma && t->type == TOK_WHITESPACE
4734 && tok_is_(t->next, ","))
4735 break; /* ... or a space then a comma */
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08004736 if (brace && t->type == TOK_OTHER) {
4737 if (t->text[0] == '{')
4738 brace++; /* ... or a nested opening brace */
4739 else if (t->text[0] == '}')
4740 if (!--brace)
4741 break; /* ... or a brace */
4742 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00004743 t = t->next;
4744 paramlen[i]++;
4745 }
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08004746 if (brace)
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03004747 nasm_nonfatal("macro params should be enclosed in braces");
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004748 }
4749
4750 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004751 * OK, we have a MMacro structure together with a set of
4752 * parameters. We must now go through the expansion and push
4753 * copies of each Line on to istk->expansion. Substitution of
H. Peter Anvin76690a12002-04-30 20:52:49 +00004754 * parameter tokens and macro-local tokens doesn't get done
4755 * until the single-line macro substitution process; this is
4756 * because delaying them allows us to change the semantics
4757 * later through %rotate.
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004758 *
4759 * First, push an end marker on to istk->expansion, mark this
4760 * macro as in progress, and set up its invocation-specific
4761 * variables.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004762 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004763 ll = nasm_malloc(sizeof(Line));
4764 ll->next = istk->expansion;
4765 ll->finishes = m;
4766 ll->first = NULL;
4767 istk->expansion = ll;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004768
4769 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004770 * Save the previous MMacro expansion in the case of
4771 * macro recursion
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004772 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004773 if (m->max_depth && m->in_progress)
4774 push_mmacro(m);
4775
4776 m->in_progress ++;
4777 m->params = params;
4778 m->iline = tline;
4779 m->nparam = nparam;
4780 m->rotate = 0;
4781 m->paramlen = paramlen;
4782 m->unique = unique++;
4783 m->lineno = 0;
4784 m->condcnt = 0;
4785
4786 m->next_active = istk->mstk;
4787 istk->mstk = m;
4788
4789 list_for_each(l, m->expansion) {
4790 Token **tail;
4791
4792 ll = nasm_malloc(sizeof(Line));
4793 ll->finishes = NULL;
4794 ll->next = istk->expansion;
4795 istk->expansion = ll;
4796 tail = &ll->first;
4797
4798 list_for_each(t, l->first) {
4799 Token *x = t;
4800 switch (t->type) {
4801 case TOK_PREPROC_Q:
4802 tt = *tail = new_Token(NULL, TOK_ID, mname, 0);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004803 break;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004804 case TOK_PREPROC_QQ:
4805 tt = *tail = new_Token(NULL, TOK_ID, m->name, 0);
4806 break;
4807 case TOK_PREPROC_ID:
4808 if (t->text[1] == '0' && t->text[2] == '0') {
4809 dont_prepend = -1;
4810 x = label;
4811 if (!x)
4812 continue;
4813 }
4814 /* fall through */
4815 default:
4816 tt = *tail = new_Token(NULL, x->type, x->text, 0);
4817 break;
4818 }
4819 tail = &tt->next;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004820 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004821 *tail = NULL;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004822 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004823
4824 /*
H. Peter Anvineba20a72002-04-30 20:53:55 +00004825 * If we had a label, push it on as the first line of
4826 * the macro expansion.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004827 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004828 if (label) {
4829 if (dont_prepend < 0)
4830 free_tlist(startline);
4831 else {
4832 ll = nasm_malloc(sizeof(Line));
4833 ll->finishes = NULL;
4834 ll->next = istk->expansion;
4835 istk->expansion = ll;
4836 ll->first = startline;
4837 if (!dont_prepend) {
4838 while (label->next)
4839 label = label->next;
4840 label->next = tt = new_Token(NULL, TOK_OTHER, ":", 0);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004841 }
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004842 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004843 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004844
H. Peter Anvin8ac25aa2016-02-18 01:16:18 -08004845 lfmt->uplevel(m->nolist ? LIST_MACRO_NOLIST : LIST_MACRO);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004846
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004847 return 1;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004848}
4849
H. Peter Anvin130736c2016-02-17 20:27:41 -08004850/*
4851 * This function adds macro names to error messages, and suppresses
4852 * them if necessary.
4853 */
H. Peter Anvin (Intel)6bde2ed2018-12-13 19:39:41 -08004854static void pp_verror(errflags severity, const char *fmt, va_list arg)
Victor van den Elzen3b404c02008-09-18 13:51:36 +02004855{
H. Peter Anvin130736c2016-02-17 20:27:41 -08004856 char buff[BUFSIZ];
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004857 MMacro *mmac = NULL;
4858 int delta = 0;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02004859
H. Peter Anvin130736c2016-02-17 20:27:41 -08004860 /*
4861 * If we're in a dead branch of IF or something like it, ignore the error.
4862 * However, because %else etc are evaluated in the state context
4863 * of the previous branch, errors might get lost:
4864 * %if 0 ... %else trailing garbage ... %endif
4865 * So %else etc should set the ERR_PP_PRECOND flag.
4866 */
4867 if ((severity & ERR_MASK) < ERR_FATAL &&
4868 istk && istk->conds &&
4869 ((severity & ERR_PP_PRECOND) ?
4870 istk->conds->state == COND_NEVER :
H. Peter Anvineb6653f2016-04-05 13:03:10 -07004871 !emitting(istk->conds->state)))
H. Peter Anvin130736c2016-02-17 20:27:41 -08004872 return;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02004873
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004874 /* get %macro name */
H. Peter Anvin130736c2016-02-17 20:27:41 -08004875 if (!(severity & ERR_NOFILE) && istk && istk->mstk) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004876 mmac = istk->mstk;
4877 /* but %rep blocks should be skipped */
4878 while (mmac && !mmac->name)
4879 mmac = mmac->next_active, delta++;
Cyrill Gorcunov9900c6b2011-10-09 18:58:46 +04004880 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004881
H. Peter Anvin130736c2016-02-17 20:27:41 -08004882 if (mmac) {
4883 vsnprintf(buff, sizeof(buff), fmt, arg);
Victor van den Elzen3b404c02008-09-18 13:51:36 +02004884
H. Peter Anvin130736c2016-02-17 20:27:41 -08004885 nasm_set_verror(real_verror);
4886 nasm_error(severity, "(%s:%d) %s",
4887 mmac->name, mmac->lineno - delta, buff);
4888 nasm_set_verror(pp_verror);
4889 } else {
4890 real_verror(severity, fmt, arg);
4891 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004892}
4893
H. Peter Anvin734b1882002-04-30 21:01:08 +00004894static void
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08004895pp_reset(const char *file, enum preproc_mode mode, struct strlist *dep_list)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004896{
H. Peter Anvin7383b402008-09-24 10:20:40 -07004897 Token *t;
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08004898 int apass;
H. Peter Anvin7383b402008-09-24 10:20:40 -07004899
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004900 cstk = NULL;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004901 istk = nasm_malloc(sizeof(Include));
4902 istk->next = NULL;
4903 istk->conds = NULL;
4904 istk->expansion = NULL;
4905 istk->mstk = NULL;
H. Peter Anvin3e83cec2016-05-25 04:28:46 -07004906 istk->fp = nasm_open_read(file, NF_TEXT);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004907 istk->fname = NULL;
H. Peter Anvin274cda82016-05-10 02:56:29 -07004908 src_set(0, file);
H. Peter Anvineba20a72002-04-30 20:53:55 +00004909 istk->lineinc = 1;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004910 if (!istk->fp)
Cyrill Gorcunovc3527dd2018-11-24 22:17:47 +03004911 nasm_fatalf(ERR_NOFILE, "unable to open input file `%s'", file);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004912 defining = NULL;
Charles Crayned4200be2008-07-12 16:42:33 -07004913 nested_mac_count = 0;
4914 nested_rep_count = 0;
H. Peter Anvin97a23472007-09-16 17:57:25 -07004915 init_macros();
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004916 unique = 0;
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07004917 deplist = dep_list;
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08004918 pp_mode = mode;
H. Peter Anvinf7606612016-07-13 14:23:48 -07004919
4920 if (tasm_compatible_mode)
4921 pp_add_stdmac(nasm_stdmac_tasm);
4922
4923 pp_add_stdmac(nasm_stdmac_nasm);
4924 pp_add_stdmac(nasm_stdmac_version);
4925
Cyrill Gorcunov15ce78f2017-01-06 20:21:28 +03004926 if (extrastdmac)
4927 pp_add_stdmac(extrastdmac);
4928
H. Peter Anvinf7606612016-07-13 14:23:48 -07004929 stdmacpos = stdmacros[0];
4930 stdmacnext = &stdmacros[1];
4931
H. Peter Anvind2456592008-06-19 15:04:18 -07004932 do_predef = true;
H. Peter Anvin61f130f2008-09-25 15:45:06 -07004933
Cyrill Gorcunovb7bb5ac2018-11-11 21:33:52 +03004934 strlist_add(deplist, file);
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07004935
H. Peter Anvin61f130f2008-09-25 15:45:06 -07004936 /*
4937 * Define the __PASS__ macro. This is defined here unlike
4938 * all the other builtins, because it is special -- it varies between
4939 * passes.
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08004940 *
4941 * 0 = dependencies only
4942 * 1 = preparatory passes
4943 * 2 = final pass
4944 * 3 = preproces only
H. Peter Anvin61f130f2008-09-25 15:45:06 -07004945 */
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08004946 switch (mode) {
4947 case PP_NORMAL:
4948 apass = pass_final() ? 2 : 1;
4949 break;
4950 case PP_DEPS:
4951 apass = 0;
4952 break;
4953 case PP_PREPROC:
4954 apass = 3;
4955 break;
4956 default:
4957 panic();
4958 }
4959
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004960 t = nasm_malloc(sizeof(*t));
4961 t->next = NULL;
H. Peter Anvin61f130f2008-09-25 15:45:06 -07004962 make_tok_num(t, apass);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004963 t->a.mac = NULL;
H. Peter Anvin7383b402008-09-24 10:20:40 -07004964 define_smacro(NULL, "__PASS__", true, 0, t);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004965}
4966
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07004967static void pp_init(void)
4968{
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07004969}
4970
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004971static char *pp_getline(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004972{
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004973 char *line;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004974 Token *tline;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004975
H. Peter Anvin130736c2016-02-17 20:27:41 -08004976 real_verror = nasm_set_verror(pp_verror);
H. Peter Anvin215186f2016-02-17 20:27:41 -08004977
H. Peter Anvine2c80182005-01-15 22:15:51 +00004978 while (1) {
4979 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004980 * Fetch a tokenized line, either from the macro-expansion
H. Peter Anvine2c80182005-01-15 22:15:51 +00004981 * buffer or from the input file.
4982 */
4983 tline = NULL;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004984 while (istk->expansion && istk->expansion->finishes) {
4985 Line *l = istk->expansion;
4986 if (!l->finishes->name && l->finishes->in_progress > 1) {
4987 Line *ll;
H. Peter Anvineba20a72002-04-30 20:53:55 +00004988
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004989 /*
4990 * This is a macro-end marker for a macro with no
4991 * name, which means it's not really a macro at all
4992 * but a %rep block, and the `in_progress' field is
4993 * more than 1, meaning that we still need to
4994 * repeat. (1 means the natural last repetition; 0
4995 * means termination by %exitrep.) We have
4996 * therefore expanded up to the %endrep, and must
4997 * push the whole block on to the expansion buffer
4998 * again. We don't bother to remove the macro-end
4999 * marker: we'd only have to generate another one
5000 * if we did.
5001 */
5002 l->finishes->in_progress--;
5003 list_for_each(l, l->finishes->expansion) {
5004 Token *t, *tt, **tail;
5005
5006 ll = nasm_malloc(sizeof(Line));
5007 ll->next = istk->expansion;
5008 ll->finishes = NULL;
5009 ll->first = NULL;
5010 tail = &ll->first;
5011
5012 list_for_each(t, l->first) {
5013 if (t->text || t->type == TOK_WHITESPACE) {
5014 tt = *tail = new_Token(NULL, t->type, t->text, 0);
5015 tail = &tt->next;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005016 }
5017 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005018
5019 istk->expansion = ll;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005020 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005021 } else {
5022 /*
5023 * Check whether a `%rep' was started and not ended
5024 * within this macro expansion. This can happen and
5025 * should be detected. It's a fatal error because
5026 * I'm too confused to work out how to recover
5027 * sensibly from it.
5028 */
5029 if (defining) {
5030 if (defining->name)
H. Peter Anvinc5136902018-06-15 18:20:17 -07005031 nasm_panic("defining with name in expansion");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005032 else if (istk->mstk->name)
H. Peter Anvinc5136902018-06-15 18:20:17 -07005033 nasm_fatal("`%%rep' without `%%endrep' within"
H. Peter Anvin130736c2016-02-17 20:27:41 -08005034 " expansion of macro `%s'",
5035 istk->mstk->name);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005036 }
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005037
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005038 /*
5039 * FIXME: investigate the relationship at this point between
5040 * istk->mstk and l->finishes
5041 */
5042 {
5043 MMacro *m = istk->mstk;
5044 istk->mstk = m->next_active;
5045 if (m->name) {
5046 /*
5047 * This was a real macro call, not a %rep, and
5048 * therefore the parameter information needs to
5049 * be freed.
5050 */
5051 if (m->prev) {
5052 pop_mmacro(m);
5053 l->finishes->in_progress --;
5054 } else {
5055 nasm_free(m->params);
5056 free_tlist(m->iline);
5057 nasm_free(m->paramlen);
5058 l->finishes->in_progress = 0;
5059 }
Adam Majer91e72402017-07-25 10:42:01 +02005060 }
5061
5062 /*
5063 * FIXME It is incorrect to always free_mmacro here.
5064 * It leads to usage-after-free.
5065 *
5066 * https://bugzilla.nasm.us/show_bug.cgi?id=3392414
5067 */
5068#if 0
5069 else
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005070 free_mmacro(m);
Adam Majer91e72402017-07-25 10:42:01 +02005071#endif
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005072 }
5073 istk->expansion = l->next;
5074 nasm_free(l);
H. Peter Anvin8ac25aa2016-02-18 01:16:18 -08005075 lfmt->downlevel(LIST_MACRO);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005076 }
5077 }
5078 while (1) { /* until we get a line we can use */
5079
5080 if (istk->expansion) { /* from a macro expansion */
5081 char *p;
5082 Line *l = istk->expansion;
5083 if (istk->mstk)
5084 istk->mstk->lineno++;
5085 tline = l->first;
5086 istk->expansion = l->next;
5087 nasm_free(l);
5088 p = detoken(tline, false);
H. Peter Anvin8ac25aa2016-02-18 01:16:18 -08005089 lfmt->line(LIST_MACRO, p);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005090 nasm_free(p);
5091 break;
5092 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00005093 line = read_line();
5094 if (line) { /* from the current input file */
5095 line = prepreproc(line);
Keith Kaniosb7a89542007-04-12 02:40:54 +00005096 tline = tokenize(line);
H. Peter Anvine2c80182005-01-15 22:15:51 +00005097 nasm_free(line);
5098 break;
5099 }
5100 /*
5101 * The current file has ended; work down the istk
5102 */
5103 {
5104 Include *i = istk;
5105 fclose(i->fp);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005106 if (i->conds) {
5107 /* nasm_error can't be conditionally suppressed */
H. Peter Anvinc5136902018-06-15 18:20:17 -07005108 nasm_fatal("expected `%%endif' before end of file");
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005109 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00005110 /* only set line and file name if there's a next node */
H. Peter Anvin274cda82016-05-10 02:56:29 -07005111 if (i->next)
5112 src_set(i->lineno, i->fname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00005113 istk = i->next;
H. Peter Anvin8ac25aa2016-02-18 01:16:18 -08005114 lfmt->downlevel(LIST_INCLUDE);
H. Peter Anvine2c80182005-01-15 22:15:51 +00005115 nasm_free(i);
H. Peter Anvin130736c2016-02-17 20:27:41 -08005116 if (!istk) {
5117 line = NULL;
5118 goto done;
5119 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005120 if (istk->expansion && istk->expansion->finishes)
5121 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005122 }
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005123 }
5124
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005125 /*
5126 * We must expand MMacro parameters and MMacro-local labels
5127 * _before_ we plunge into directive processing, to cope
5128 * with things like `%define something %1' such as STRUC
5129 * uses. Unless we're _defining_ a MMacro, in which case
5130 * those tokens should be left alone to go into the
5131 * definition; and unless we're in a non-emitting
5132 * condition, in which case we don't want to meddle with
5133 * anything.
5134 */
5135 if (!defining && !(istk->conds && !emitting(istk->conds->state))
5136 && !(istk->mstk && !istk->mstk->in_progress)) {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005137 tline = expand_mmac_params(tline);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005138 }
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005139
H. Peter Anvine2c80182005-01-15 22:15:51 +00005140 /*
5141 * Check the line to see if it's a preprocessor directive.
5142 */
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07005143 if (do_directive(tline, &line) == DIRECTIVE_FOUND) {
5144 if (line)
5145 break; /* Directive generated output */
5146 else
5147 continue;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005148 } else if (defining) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00005149 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005150 * We're defining a multi-line macro. We emit nothing
5151 * at all, and just
5152 * shove the tokenized line on to the macro definition.
H. Peter Anvine2c80182005-01-15 22:15:51 +00005153 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005154 Line *l = nasm_malloc(sizeof(Line));
5155 l->next = defining->expansion;
5156 l->first = tline;
5157 l->finishes = NULL;
5158 defining->expansion = l;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005159 continue;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005160 } else if (istk->conds && !emitting(istk->conds->state)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00005161 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005162 * We're in a non-emitting branch of a condition block.
H. Peter Anvine2c80182005-01-15 22:15:51 +00005163 * Emit nothing at all, not even a blank line: when we
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005164 * emerge from the condition we'll give a line-number
H. Peter Anvine2c80182005-01-15 22:15:51 +00005165 * directive so we keep our place correctly.
5166 */
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005167 free_tlist(tline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00005168 continue;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005169 } else if (istk->mstk && !istk->mstk->in_progress) {
5170 /*
5171 * We're in a %rep block which has been terminated, so
5172 * we're walking through to the %endrep without
5173 * emitting anything. Emit nothing at all, not even a
5174 * blank line: when we emerge from the %rep block we'll
5175 * give a line-number directive so we keep our place
5176 * correctly.
5177 */
5178 free_tlist(tline);
5179 continue;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005180 } else {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005181 tline = expand_smacro(tline);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005182 if (!expand_mmacro(tline)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00005183 /*
Keith Kaniosb7a89542007-04-12 02:40:54 +00005184 * De-tokenize the line again, and emit it.
H. Peter Anvine2c80182005-01-15 22:15:51 +00005185 */
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005186 line = detoken(tline, true);
5187 free_tlist(tline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00005188 break;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005189 } else {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005190 continue; /* expand_mmacro calls free_tlist */
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005191 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00005192 }
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005193 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005194
H. Peter Anvin130736c2016-02-17 20:27:41 -08005195done:
5196 nasm_set_verror(real_verror);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005197 return line;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005198}
5199
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08005200static void pp_cleanup_pass(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005201{
H. Peter Anvin130736c2016-02-17 20:27:41 -08005202 real_verror = nasm_set_verror(pp_verror);
H. Peter Anvin215186f2016-02-17 20:27:41 -08005203
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005204 if (defining) {
5205 if (defining->name) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03005206 nasm_nonfatal("end of file while still defining macro `%s'",
5207 defining->name);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005208 } else {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03005209 nasm_nonfatal("end of file while still in %%rep");
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005210 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005211
5212 free_mmacro(defining);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005213 defining = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005214 }
H. Peter Anvin130736c2016-02-17 20:27:41 -08005215
5216 nasm_set_verror(real_verror);
H. Peter Anvin215186f2016-02-17 20:27:41 -08005217
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005218 while (cstk)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005219 ctx_pop();
H. Peter Anvin97a23472007-09-16 17:57:25 -07005220 free_macros();
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005221 while (istk) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00005222 Include *i = istk;
5223 istk = istk->next;
5224 fclose(i->fp);
Cyrill Gorcunov8dcfd882011-03-03 09:18:56 +03005225 nasm_free(i);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005226 }
5227 while (cstk)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005228 ctx_pop();
H. Peter Anvin274cda82016-05-10 02:56:29 -07005229 src_set_fname(NULL);
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08005230}
5231
5232static void pp_cleanup_session(void)
5233{
5234 free_llist(predef);
5235 predef = NULL;
5236 delete_Blocks();
5237 freeTokens = NULL;
5238 ipath_list = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005239}
5240
Cyrill Gorcunov8c0666b2018-11-24 14:33:48 +03005241static void pp_include_path(struct strlist *list)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005242{
Cyrill Gorcunov8c0666b2018-11-24 14:33:48 +03005243 ipath_list = list;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005244}
Frank Kotlerd0ed6fd2003-08-27 11:33:56 +00005245
Cyrill Gorcunov0b78bff2012-05-07 01:57:55 +04005246static void pp_pre_include(char *fname)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005247{
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005248 Token *inc, *space, *name;
5249 Line *l;
5250
H. Peter Anvin734b1882002-04-30 21:01:08 +00005251 name = new_Token(NULL, TOK_INTERNAL_STRING, fname, 0);
5252 space = new_Token(name, TOK_WHITESPACE, NULL, 0);
5253 inc = new_Token(space, TOK_PREPROC_ID, "%include", 0);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005254
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005255 l = nasm_malloc(sizeof(Line));
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005256 l->next = predef;
5257 l->first = inc;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005258 l->finishes = NULL;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005259 predef = l;
5260}
5261
Cyrill Gorcunov0b78bff2012-05-07 01:57:55 +04005262static void pp_pre_define(char *definition)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005263{
5264 Token *def, *space;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005265 Line *l;
Keith Kaniosa6dfa782007-04-13 16:47:53 +00005266 char *equals;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005267
H. Peter Anvin130736c2016-02-17 20:27:41 -08005268 real_verror = nasm_set_verror(pp_verror);
H. Peter Anvin215186f2016-02-17 20:27:41 -08005269
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005270 equals = strchr(definition, '=');
H. Peter Anvin734b1882002-04-30 21:01:08 +00005271 space = new_Token(NULL, TOK_WHITESPACE, NULL, 0);
5272 def = new_Token(space, TOK_PREPROC_ID, "%define", 0);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005273 if (equals)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005274 *equals = ' ';
Keith Kaniosb7a89542007-04-12 02:40:54 +00005275 space->next = tokenize(definition);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005276 if (equals)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005277 *equals = '=';
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005278
Cyrill Gorcunov6d42e9b2015-02-08 11:07:17 +03005279 if (space->next->type != TOK_PREPROC_ID &&
5280 space->next->type != TOK_ID)
H. Peter Anvin (Intel)80c4f232018-12-14 13:33:24 -08005281 nasm_warn(WARN_OTHER, "pre-defining non ID `%s\'\n", definition);
Cyrill Gorcunov6d42e9b2015-02-08 11:07:17 +03005282
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005283 l = nasm_malloc(sizeof(Line));
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005284 l->next = predef;
5285 l->first = def;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005286 l->finishes = NULL;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005287 predef = l;
H. Peter Anvin130736c2016-02-17 20:27:41 -08005288
5289 nasm_set_verror(real_verror);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005290}
5291
Cyrill Gorcunov0b78bff2012-05-07 01:57:55 +04005292static void pp_pre_undefine(char *definition)
H. Peter Anvin620515a2002-04-30 20:57:38 +00005293{
5294 Token *def, *space;
5295 Line *l;
5296
H. Peter Anvin734b1882002-04-30 21:01:08 +00005297 space = new_Token(NULL, TOK_WHITESPACE, NULL, 0);
5298 def = new_Token(space, TOK_PREPROC_ID, "%undef", 0);
Keith Kaniosb7a89542007-04-12 02:40:54 +00005299 space->next = tokenize(definition);
H. Peter Anvin620515a2002-04-30 20:57:38 +00005300
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005301 l = nasm_malloc(sizeof(Line));
H. Peter Anvin620515a2002-04-30 20:57:38 +00005302 l->next = predef;
5303 l->first = def;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005304 l->finishes = NULL;
H. Peter Anvin620515a2002-04-30 20:57:38 +00005305 predef = l;
5306}
5307
H. Peter Anvin05990342018-06-11 13:32:42 -07005308/* Insert an early preprocessor command that doesn't need special handling */
5309static void pp_pre_command(const char *what, char *string)
5310{
5311 char *cmd;
5312 Token *def, *space;
5313 Line *l;
5314
5315 def = tokenize(string);
5316 if (what) {
5317 cmd = nasm_strcat(what[0] == '%' ? "" : "%", what);
5318 space = new_Token(def, TOK_WHITESPACE, NULL, 0);
5319 def = new_Token(space, TOK_PREPROC_ID, cmd, 0);
5320 }
5321
5322 l = nasm_malloc(sizeof(Line));
5323 l->next = predef;
5324 l->first = def;
5325 l->finishes = NULL;
5326 predef = l;
5327}
5328
H. Peter Anvinf7606612016-07-13 14:23:48 -07005329static void pp_add_stdmac(macros_t *macros)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005330{
H. Peter Anvinf7606612016-07-13 14:23:48 -07005331 macros_t **mp;
5332
5333 /* Find the end of the list and avoid duplicates */
5334 for (mp = stdmacros; *mp; mp++) {
5335 if (*mp == macros)
5336 return; /* Nothing to do */
5337 }
5338
5339 nasm_assert(mp < &stdmacros[ARRAY_SIZE(stdmacros)-1]);
5340
5341 *mp = macros;
H. Peter Anvin76690a12002-04-30 20:52:49 +00005342}
5343
Cyrill Gorcunov15ce78f2017-01-06 20:21:28 +03005344static void pp_extra_stdmac(macros_t *macros)
5345{
5346 extrastdmac = macros;
5347}
5348
Keith Kaniosa5fc6462007-10-13 07:09:22 -07005349static void make_tok_num(Token * tok, int64_t val)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005350{
Cyrill Gorcunovce652742013-05-06 23:43:43 +04005351 char numbuf[32];
Keith Kaniosa5fc6462007-10-13 07:09:22 -07005352 snprintf(numbuf, sizeof(numbuf), "%"PRId64"", val);
H. Peter Anvineba20a72002-04-30 20:53:55 +00005353 tok->text = nasm_strdup(numbuf);
5354 tok->type = TOK_NUMBER;
5355}
5356
H. Peter Anvin (Intel)6bde2ed2018-12-13 19:39:41 -08005357static void pp_list_one_macro(MMacro *m, errflags severity)
H. Peter Anvin37368952016-05-09 14:10:32 -07005358{
5359 if (!m)
5360 return;
5361
5362 /* We need to print the next_active list in reverse order */
5363 pp_list_one_macro(m->next_active, severity);
5364
5365 if (m->name && !m->nolist) {
H. Peter Anvin274cda82016-05-10 02:56:29 -07005366 src_set(m->xline + m->lineno, m->fname);
H. Peter Anvinddb29062018-12-11 00:06:29 -08005367 nasm_error(severity, "... from macro `%s' defined", m->name);
H. Peter Anvin37368952016-05-09 14:10:32 -07005368 }
5369}
5370
H. Peter Anvin (Intel)6bde2ed2018-12-13 19:39:41 -08005371static void pp_error_list_macros(errflags severity)
H. Peter Anvin4def1a82016-05-09 13:59:44 -07005372{
H. Peter Anvinddb29062018-12-11 00:06:29 -08005373 struct src_location saved;
H. Peter Anvin4def1a82016-05-09 13:59:44 -07005374
H. Peter Anvinddb29062018-12-11 00:06:29 -08005375 severity |= ERR_PP_LISTMACRO | ERR_NO_SEVERITY | ERR_HERE;
5376 saved = src_where();
H. Peter Anvin4def1a82016-05-09 13:59:44 -07005377
Cyrill Gorcunov771d04e2016-05-10 23:27:03 +03005378 if (istk)
5379 pp_list_one_macro(istk->mstk, severity);
H. Peter Anvin4def1a82016-05-09 13:59:44 -07005380
H. Peter Anvinddb29062018-12-11 00:06:29 -08005381 src_update(saved);
H. Peter Anvin4def1a82016-05-09 13:59:44 -07005382}
5383
H. Peter Anvine7469712016-02-18 02:20:59 -08005384const struct preproc_ops nasmpp = {
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07005385 pp_init,
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005386 pp_reset,
5387 pp_getline,
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08005388 pp_cleanup_pass,
5389 pp_cleanup_session,
Cyrill Gorcunov15ce78f2017-01-06 20:21:28 +03005390 pp_extra_stdmac,
Cyrill Gorcunov0b78bff2012-05-07 01:57:55 +04005391 pp_pre_define,
5392 pp_pre_undefine,
5393 pp_pre_include,
H. Peter Anvin05990342018-06-11 13:32:42 -07005394 pp_pre_command,
H. Peter Anvin4def1a82016-05-09 13:59:44 -07005395 pp_include_path,
5396 pp_error_list_macros,
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005397};