blob: 47a4de2034189b59c62d3d99b0fea2d8a3f99c3d [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 Anvinc2f3f262018-12-27 12:37:25 -080065#include "nctype.h"
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000066
67#include "nasm.h"
68#include "nasmlib.h"
H. Peter Anvinb20bc732017-03-07 19:23:03 -080069#include "error.h"
H. Peter Anvin4169a472007-09-12 01:29:43 +000070#include "preproc.h"
H. Peter Anvin97a23472007-09-16 17:57:25 -070071#include "hashtbl.h"
H. Peter Anvin8cad14b2008-06-01 17:23:51 -070072#include "quote.h"
H. Peter Anvinc2df2822007-10-24 15:29:28 -070073#include "stdscan.h"
H. Peter Anvindbb640b2009-07-18 18:57:16 -070074#include "eval.h"
H. Peter Anvinc2df2822007-10-24 15:29:28 -070075#include "tokens.h"
H. Peter Anvina4835d42008-05-20 14:21:29 -070076#include "tables.h"
H. Peter Anvin8ac25aa2016-02-18 01:16:18 -080077#include "listing.h"
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000078
79typedef struct SMacro SMacro;
H. Peter Anvin36206cd2012-03-03 16:14:51 -080080typedef struct MMacro MMacro;
81typedef struct MMacroInvocation MMacroInvocation;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000082typedef struct Context Context;
83typedef struct Token Token;
H. Peter Anvince616072002-04-30 21:02:23 +000084typedef struct Blocks Blocks;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000085typedef struct Line Line;
86typedef struct Include Include;
H. Peter Anvin36206cd2012-03-03 16:14:51 -080087typedef struct Cond Cond;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000088
89/*
H. Peter Anvin97a23472007-09-16 17:57:25 -070090 * Note on the storage of both SMacro and MMacros: the hash table
91 * indexes them case-insensitively, and we then have to go through a
92 * linked list of potential case aliases (and, for MMacros, parameter
93 * ranges); this is to preserve the matching semantics of the earlier
94 * code. If the number of case aliases for a specific macro is a
95 * performance issue, you may want to reconsider your coding style.
96 */
97
98/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000099 * Store the definition of a single-line macro.
100 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000101struct SMacro {
H. Peter Anvin8b262472019-02-26 14:00:54 -0800102 SMacro *next; /* MUST BE FIRST - see free_smacro() */
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800103 char *name;
H. Peter Anvin8b262472019-02-26 14:00:54 -0800104 union {
105 Token *expansion;
106 Token *(*magic)(const SMacro *s, Token **params, int *paramsize);
107 } e;
108 bool *eval_param;
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800109 unsigned int nparam;
H. Peter Anvin8b262472019-02-26 14:00:54 -0800110 bool casesense;
111 bool magic;
112 bool in_progress;
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 Anvin8b262472019-02-26 14:00:54 -0800190 * Note that `type' serves a double meaning: TOK_SMAC_START_PARAMS is
191 * not necessarily used as-is, but is also used to encode the number
192 * and expansion type of substituted parameter. So in the definition
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000193 *
H. Peter Anvin8b262472019-02-26 14:00:54 -0800194 * %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
H. Peter Anvin8b262472019-02-26 14:00:54 -0800197 * tok_smac_param(0) but the one representing `y' will be
198 * tok_smac_param(1); see the accessor functions below.
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,
H. Peter Anvin8b262472019-02-26 14:00:54 -0800208 TOK_NUMBER, TOK_FLOAT, 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, /* %[...] */
H. Peter Anvin8b262472019-02-26 14:00:54 -0800213 TOK_SMAC_END, /* Marker for the end of smacro expansion */
214 TOK_SMAC_START_PARAMS, /* MUST BE LAST IN THE LIST!!! */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300215 TOK_MAX = INT_MAX /* Keep compiler from reducing the range */
H. Peter Anvinda10e7b2007-09-12 04:18:37 +0000216};
217
H. Peter Anvin8b262472019-02-26 14:00:54 -0800218static inline enum pp_token_type tok_smac_param(int param)
219{
220 return TOK_SMAC_START_PARAMS + param;
221}
222static int smac_nparam(enum pp_token_type toktype)
223{
224 return toktype - TOK_SMAC_START_PARAMS;
225}
226static bool is_smac_param(enum pp_token_type toktype)
227{
228 return toktype >= TOK_SMAC_START_PARAMS;
229}
230
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +0400231#define PP_CONCAT_MASK(x) (1 << (x))
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +0400232#define PP_CONCAT_MATCH(t, mask) (PP_CONCAT_MASK((t)->type) & mask)
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +0400233
Cyrill Gorcunov575d4282010-10-06 00:25:55 +0400234struct tokseq_match {
235 int mask_head;
236 int mask_tail;
237};
238
H. Peter Anvine2c80182005-01-15 22:15:51 +0000239struct Token {
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800240 Token *next;
241 char *text;
H. Peter Anvinf26e0972008-07-01 21:26:27 -0700242 union {
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800243 SMacro *mac; /* associated macro for TOK_SMAC_END */
244 size_t len; /* scratch length field */
245 } a; /* Auxiliary data */
246 enum pp_token_type type;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000247};
248
249/*
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800250 * Multi-line macro definitions are stored as a linked list of
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000251 * these, which is essentially a container to allow several linked
252 * lists of Tokens.
H. Peter Anvin70653092007-10-19 14:42:29 -0700253 *
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000254 * Note that in this module, linked lists are treated as stacks
255 * wherever possible. For this reason, Lines are _pushed_ on to the
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800256 * `expansion' field in MMacro structures, so that the linked list,
257 * if walked, would give the macro lines in reverse order; this
258 * means that we can walk the list when expanding a macro, and thus
259 * push the lines on to the `expansion' field in _istk_ in reverse
260 * order (so that when popped back off they are in the right
261 * order). It may seem cockeyed, and it relies on my design having
262 * an even number of steps in, but it works...
263 *
264 * Some of these structures, rather than being actual lines, are
265 * markers delimiting the end of the expansion of a given macro.
266 * This is for use in the cycle-tracking and %rep-handling code.
267 * Such structures have `finishes' non-NULL, and `first' NULL. All
268 * others have `finishes' NULL, but `first' may still be NULL if
269 * the line is blank.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000270 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000271struct Line {
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800272 Line *next;
273 MMacro *finishes;
274 Token *first;
Keith Kaniosb307a4f2010-11-06 17:41:51 -0500275};
276
277/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000278 * To handle an arbitrary level of file inclusion, we maintain a
279 * stack (ie linked list) of these things.
280 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000281struct Include {
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800282 Include *next;
283 FILE *fp;
284 Cond *conds;
285 Line *expansion;
H. Peter Anvin274cda82016-05-10 02:56:29 -0700286 const char *fname;
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800287 int lineno, lineinc;
288 MMacro *mstk; /* stack of active macros/reps */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000289};
290
291/*
H. Peter Anvin169ac7c2016-09-25 17:08:05 -0700292 * File real name hash, so we don't have to re-search the include
293 * path for every pass (and potentially more than that if a file
294 * is used more than once.)
295 */
296struct hash_table FileHash;
297
298/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000299 * Conditional assembly: we maintain a separate stack of these for
300 * each level of file inclusion. (The only reason we keep the
301 * stacks separate is to ensure that a stray `%endif' in a file
302 * included from within the true branch of a `%if' won't terminate
303 * it and cause confusion: instead, rightly, it'll cause an error.)
304 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800305struct Cond {
306 Cond *next;
307 int state;
308};
H. Peter Anvine2c80182005-01-15 22:15:51 +0000309enum {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000310 /*
311 * These states are for use just after %if or %elif: IF_TRUE
312 * means the condition has evaluated to truth so we are
313 * currently emitting, whereas IF_FALSE means we are not
314 * currently emitting but will start doing so if a %else comes
315 * up. In these states, all directives are admissible: %elif,
316 * %else and %endif. (And of course %if.)
317 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800318 COND_IF_TRUE, COND_IF_FALSE,
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000319 /*
320 * These states come up after a %else: ELSE_TRUE means we're
321 * emitting, and ELSE_FALSE means we're not. In ELSE_* states,
322 * any %elif or %else will cause an error.
323 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800324 COND_ELSE_TRUE, COND_ELSE_FALSE,
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000325 /*
Victor van den Elzen3b404c02008-09-18 13:51:36 +0200326 * These states mean that we're not emitting now, and also that
327 * nothing until %endif will be emitted at all. COND_DONE is
328 * used when we've had our moment of emission
329 * and have now started seeing %elifs. COND_NEVER is used when
330 * the condition construct in question is contained within a
331 * non-emitting branch of a larger condition construct,
332 * or if there is an error.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000333 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800334 COND_DONE, COND_NEVER
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000335};
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800336#define emitting(x) ( (x) == COND_IF_TRUE || (x) == COND_ELSE_TRUE )
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000337
H. Peter Anvin70653092007-10-19 14:42:29 -0700338/*
Ed Beroset3ab3f412002-06-11 03:31:49 +0000339 * These defines are used as the possible return values for do_directive
340 */
341#define NO_DIRECTIVE_FOUND 0
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300342#define DIRECTIVE_FOUND 1
Ed Beroset3ab3f412002-06-11 03:31:49 +0000343
Cyrill Gorcunove091d6e2010-08-09 13:58:22 +0400344/* max reps */
345#define REP_LIMIT ((INT64_C(1) << 62))
346
Keith Kanios852f1ee2009-07-12 00:19:55 -0500347/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000348 * Condition codes. Note that we use c_ prefix not C_ because C_ is
349 * used in nasm.h for the "real" condition codes. At _this_ level,
350 * we treat CXZ and ECXZ as condition codes, albeit non-invertible
351 * ones, so we need a different enum...
352 */
H. Peter Anvin476d2862007-10-02 22:04:15 -0700353static const char * const conditions[] = {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000354 "a", "ae", "b", "be", "c", "cxz", "e", "ecxz", "g", "ge", "l", "le",
355 "na", "nae", "nb", "nbe", "nc", "ne", "ng", "nge", "nl", "nle", "no",
H. Peter Anvince9be342007-09-12 00:22:29 +0000356 "np", "ns", "nz", "o", "p", "pe", "po", "rcxz", "s", "z"
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000357};
H. Peter Anvin476d2862007-10-02 22:04:15 -0700358enum pp_conds {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000359 c_A, c_AE, c_B, c_BE, c_C, c_CXZ, c_E, c_ECXZ, c_G, c_GE, c_L, c_LE,
360 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 -0700361 c_NP, c_NS, c_NZ, c_O, c_P, c_PE, c_PO, c_RCXZ, c_S, c_Z,
362 c_none = -1
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000363};
H. Peter Anvin476d2862007-10-02 22:04:15 -0700364static const enum pp_conds inverse_ccs[] = {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000365 c_NA, c_NAE, c_NB, c_NBE, c_NC, -1, c_NE, -1, c_NG, c_NGE, c_NL, c_NLE,
366 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 +0000367 c_Z, c_NO, c_NP, c_PO, c_PE, -1, c_NS, c_NZ
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000368};
369
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800370/*
371 * Directive names.
372 */
373/* If this is a an IF, ELIF, ELSE or ENDIF keyword */
374static int is_condition(enum preproc_token arg)
375{
376 return PP_IS_COND(arg) || (arg == PP_ELSE) || (arg == PP_ENDIF);
377}
378
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000379/* For TASM compatibility we need to be able to recognise TASM compatible
380 * conditional compilation directives. Using the NASM pre-processor does
381 * not work, so we look for them specifically from the following list and
382 * then jam in the equivalent NASM directive into the input stream.
383 */
384
H. Peter Anvine2c80182005-01-15 22:15:51 +0000385enum {
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000386 TM_ARG, TM_ELIF, TM_ELSE, TM_ENDIF, TM_IF, TM_IFDEF, TM_IFDIFI,
387 TM_IFNDEF, TM_INCLUDE, TM_LOCAL
388};
389
H. Peter Anvin476d2862007-10-02 22:04:15 -0700390static const char * const tasm_directives[] = {
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000391 "arg", "elif", "else", "endif", "if", "ifdef", "ifdifi",
392 "ifndef", "include", "local"
393};
394
395static int StackSize = 4;
H. Peter Anvin6c8b2be2016-05-24 23:46:50 -0700396static const char *StackPointer = "ebp";
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000397static int ArgOffset = 8;
H. Peter Anvin8781cb02007-11-08 20:01:11 -0800398static int LocalOffset = 0;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000399
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000400static Context *cstk;
401static Include *istk;
Cyrill Gorcunov8c0666b2018-11-24 14:33:48 +0300402static const struct strlist *ipath_list;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000403
Cyrill Gorcunovb7bb5ac2018-11-11 21:33:52 +0300404static struct strlist *deplist;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000405
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300406static uint64_t unique; /* unique identifier numbers */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000407
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800408static Line *predef = NULL;
H. Peter Anvind2456592008-06-19 15:04:18 -0700409static bool do_predef;
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -0800410static enum preproc_mode pp_mode;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000411
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000412/*
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800413 * The current set of multi-line macros we have defined.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000414 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800415static struct hash_table mmacros;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000416
417/*
418 * The current set of single-line macros we have defined.
419 */
H. Peter Anvin166c2472008-05-28 12:28:58 -0700420static struct hash_table smacros;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000421
422/*
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800423 * The multi-line macro we are currently defining, or the %rep
424 * block we are currently reading, if any.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000425 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800426static MMacro *defining;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000427
Charles Crayned4200be2008-07-12 16:42:33 -0700428static uint64_t nested_mac_count;
429static uint64_t nested_rep_count;
430
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000431/*
432 * The number of macro parameters to allocate space for at a time.
433 */
434#define PARAM_DELTA 16
435
436/*
H. Peter Anvinf7606612016-07-13 14:23:48 -0700437 * The standard macro set: defined in macros.c in a set of arrays.
438 * This gives our position in any macro set, while we are processing it.
439 * The stdmacset is an array of such macro sets.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000440 */
H. Peter Anvina70547f2008-07-19 21:44:26 -0700441static macros_t *stdmacpos;
H. Peter Anvinf7606612016-07-13 14:23:48 -0700442static macros_t **stdmacnext;
443static macros_t *stdmacros[8];
Cyrill Gorcunov15ce78f2017-01-06 20:21:28 +0300444static macros_t *extrastdmac;
H. Peter Anvin76690a12002-04-30 20:52:49 +0000445
446/*
H. Peter Anvin734b1882002-04-30 21:01:08 +0000447 * Tokens are allocated in blocks to improve speed
448 */
449#define TOKEN_BLOCKSIZE 4096
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800450static Token *freeTokens = NULL;
H. Peter Anvince616072002-04-30 21:02:23 +0000451struct Blocks {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000452 Blocks *next;
453 void *chunk;
H. Peter Anvince616072002-04-30 21:02:23 +0000454};
455
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800456static Blocks blocks = { NULL, NULL };
H. Peter Anvin734b1882002-04-30 21:01:08 +0000457
458/*
H. Peter Anvin76690a12002-04-30 20:52:49 +0000459 * Forward declarations.
460 */
H. Peter Anvinf7606612016-07-13 14:23:48 -0700461static void pp_add_stdmac(macros_t *macros);
H. Peter Anvin734b1882002-04-30 21:01:08 +0000462static Token *expand_mmac_params(Token * tline);
463static Token *expand_smacro(Token * tline);
464static Token *expand_id(Token * tline);
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +0400465static Context *get_ctx(const char *name, const char **namep);
H. Peter Anvin8b262472019-02-26 14:00:54 -0800466static Token *make_tok_num(int64_t val);
467static Token *make_tok_qstr(const char *str);
H. Peter Anvin (Intel)6bde2ed2018-12-13 19:39:41 -0800468static void pp_verror(errflags severity, const char *fmt, va_list ap);
H. Peter Anvin130736c2016-02-17 20:27:41 -0800469static vefunc real_verror;
H. Peter Anvince616072002-04-30 21:02:23 +0000470static void *new_Block(size_t size);
471static void delete_Blocks(void);
H. Peter Anvinc751e862008-06-09 10:18:45 -0700472static Token *new_Token(Token * next, enum pp_token_type type,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300473 const char *text, int txtlen);
H. Peter Anvin734b1882002-04-30 21:01:08 +0000474static Token *delete_Token(Token * t);
H. Peter Anvineba20a72002-04-30 20:53:55 +0000475
476/*
477 * Macros for safe checking of token pointers, avoid *(NULL)
478 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300479#define tok_type_(x,t) ((x) && (x)->type == (t))
480#define skip_white_(x) if (tok_type_((x), TOK_WHITESPACE)) (x)=(x)->next
481#define tok_is_(x,v) (tok_type_((x), TOK_OTHER) && !strcmp((x)->text,(v)))
482#define tok_isnt_(x,v) ((x) && ((x)->type!=TOK_OTHER || strcmp((x)->text,(v))))
H. Peter Anvin76690a12002-04-30 20:52:49 +0000483
Cyrill Gorcunov194ba892011-06-30 01:16:35 +0400484/*
H. Peter Anvin077fb932010-07-20 14:56:30 -0700485 * nasm_unquote with error if the string contains NUL characters.
486 * If the string contains NUL characters, issue an error and return
487 * the C len, i.e. truncate at the NUL.
488 */
489static size_t nasm_unquote_cstr(char *qstr, enum preproc_token directive)
490{
491 size_t len = nasm_unquote(qstr, NULL);
492 size_t clen = strlen(qstr);
493
494 if (len != clen)
Cyrill Gorcunov295b7952018-11-25 12:55:48 +0300495 nasm_nonfatal("NUL character in `%s' directive",
496 pp_directives[directive]);
H. Peter Anvin077fb932010-07-20 14:56:30 -0700497 return clen;
498}
499
500/*
H. Peter Anvinb40992c2010-09-15 08:57:21 -0700501 * In-place reverse a list of tokens.
502 */
503static Token *reverse_tokens(Token *t)
504{
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800505 Token *prev = NULL;
506 Token *next;
H. Peter Anvinb40992c2010-09-15 08:57:21 -0700507
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800508 while (t) {
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +0400509 next = t->next;
510 t->next = prev;
511 prev = t;
512 t = next;
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800513 }
H. Peter Anvinb40992c2010-09-15 08:57:21 -0700514
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800515 return prev;
H. Peter Anvinb40992c2010-09-15 08:57:21 -0700516}
517
518/*
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300519 * Handle TASM specific directives, which do not contain a % in
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000520 * front of them. We do it here because I could not find any other
521 * place to do it for the moment, and it is a hack (ideally it would
522 * be nice to be able to use the NASM pre-processor to do it).
523 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000524static char *check_tasm_directive(char *line)
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000525{
Keith Kaniosb7a89542007-04-12 02:40:54 +0000526 int32_t i, j, k, m, len;
Cyrill Gorcunovf66ac7d2009-10-12 20:41:13 +0400527 char *p, *q, *oldline, oldchar;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000528
Cyrill Gorcunovf66ac7d2009-10-12 20:41:13 +0400529 p = nasm_skip_spaces(line);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000530
531 /* Binary search for the directive name */
532 i = -1;
Cyrill Gorcunova7319242010-06-03 22:04:36 +0400533 j = ARRAY_SIZE(tasm_directives);
Cyrill Gorcunovf66ac7d2009-10-12 20:41:13 +0400534 q = nasm_skip_word(p);
535 len = q - p;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000536 if (len) {
537 oldchar = p[len];
538 p[len] = 0;
539 while (j - i > 1) {
540 k = (j + i) / 2;
541 m = nasm_stricmp(p, tasm_directives[k]);
542 if (m == 0) {
543 /* We have found a directive, so jam a % in front of it
544 * so that NASM will then recognise it as one if it's own.
545 */
546 p[len] = oldchar;
547 len = strlen(p);
548 oldline = line;
549 line = nasm_malloc(len + 2);
550 line[0] = '%';
551 if (k == TM_IFDIFI) {
H. Peter Anvin18f48792009-06-27 15:56:27 -0700552 /*
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300553 * NASM does not recognise IFDIFI, so we convert
554 * it to %if 0. This is not used in NASM
555 * compatible code, but does need to parse for the
556 * TASM macro package.
H. Peter Anvine2c80182005-01-15 22:15:51 +0000557 */
H. Peter Anvin18f48792009-06-27 15:56:27 -0700558 strcpy(line + 1, "if 0");
H. Peter Anvine2c80182005-01-15 22:15:51 +0000559 } else {
560 memcpy(line + 1, p, len + 1);
561 }
562 nasm_free(oldline);
563 return line;
564 } else if (m < 0) {
565 j = k;
566 } else
567 i = k;
568 }
569 p[len] = oldchar;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000570 }
571 return line;
572}
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000573
H. Peter Anvin76690a12002-04-30 20:52:49 +0000574/*
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000575 * The pre-preprocessing stage... This function translates line
576 * number indications as they emerge from GNU cpp (`# lineno "file"
577 * flags') into NASM preprocessor line number indications (`%line
578 * lineno file').
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000579 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000580static char *prepreproc(char *line)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000581{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000582 int lineno, fnlen;
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000583 char *fname, *oldline;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000584
H. Peter Anvine2c80182005-01-15 22:15:51 +0000585 if (line[0] == '#' && line[1] == ' ') {
586 oldline = line;
587 fname = oldline + 2;
588 lineno = atoi(fname);
589 fname += strspn(fname, "0123456789 ");
590 if (*fname == '"')
591 fname++;
592 fnlen = strcspn(fname, "\"");
593 line = nasm_malloc(20 + fnlen);
594 snprintf(line, 20 + fnlen, "%%line %d %.*s", lineno, fnlen, fname);
595 nasm_free(oldline);
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000596 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000597 if (tasm_compatible_mode)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000598 return check_tasm_directive(line);
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000599 return line;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000600}
601
602/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000603 * Free a linked list of tokens.
604 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000605static void free_tlist(Token * list)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000606{
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +0400607 while (list)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000608 list = delete_Token(list);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000609}
610
611/*
612 * Free a linked list of lines.
613 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000614static void free_llist(Line * list)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000615{
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +0400616 Line *l, *tmp;
617 list_for_each_safe(l, tmp, list) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000618 free_tlist(l->first);
619 nasm_free(l);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000620 }
621}
622
623/*
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800624 * Free an MMacro
H. Peter Anvineba20a72002-04-30 20:53:55 +0000625 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800626static void free_mmacro(MMacro * m)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000627{
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800628 nasm_free(m->name);
629 free_tlist(m->dlist);
630 nasm_free(m->defaults);
631 free_llist(m->expansion);
632 nasm_free(m);
H. Peter Anvineba20a72002-04-30 20:53:55 +0000633}
634
635/*
H. Peter Anvin8b262472019-02-26 14:00:54 -0800636 * Free an SMacro
637 */
638static void free_smacro(SMacro *s, bool really)
639{
640 nasm_free(s->name);
641 if (!s->magic)
642 free_tlist(s->e.expansion);
643 nasm_free(s->eval_param);
644 if (really) {
645 nasm_free(s);
646 } else {
647 /* Wipe everything except the next pointer */
648 memset(&s->next + 1, 0, sizeof *s - sizeof s->next);
649 }
650}
651
652/*
H. Peter Anvin97a23472007-09-16 17:57:25 -0700653 * Free all currently defined macros, and free the hash tables
654 */
H. Peter Anvin072771e2008-05-22 13:17:51 -0700655static void free_smacro_table(struct hash_table *smt)
H. Peter Anvin97a23472007-09-16 17:57:25 -0700656{
H. Peter Anvin (Intel)ebb05a02018-12-11 12:30:25 -0800657 struct hash_iterator it;
658 const struct hash_node *np;
H. Peter Anvin97a23472007-09-16 17:57:25 -0700659
H. Peter Anvin (Intel)ebb05a02018-12-11 12:30:25 -0800660 hash_for_each(smt, it, np) {
661 SMacro *tmp;
662 SMacro *s = np->data;
663 nasm_free((void *)np->key);
H. Peter Anvin8b262472019-02-26 14:00:54 -0800664 list_for_each_safe(s, tmp, s)
665 free_smacro(s, true);
H. Peter Anvin97a23472007-09-16 17:57:25 -0700666 }
H. Peter Anvin072771e2008-05-22 13:17:51 -0700667 hash_free(smt);
668}
669
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800670static void free_mmacro_table(struct hash_table *mmt)
H. Peter Anvin072771e2008-05-22 13:17:51 -0700671{
H. Peter Anvin (Intel)ebb05a02018-12-11 12:30:25 -0800672 struct hash_iterator it;
673 const struct hash_node *np;
H. Peter Anvin97a23472007-09-16 17:57:25 -0700674
H. Peter Anvin (Intel)ebb05a02018-12-11 12:30:25 -0800675 hash_for_each(mmt, it, np) {
676 MMacro *tmp;
677 MMacro *m = np->data;
678 nasm_free((void *)np->key);
679 list_for_each_safe(m, tmp, m)
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800680 free_mmacro(m);
H. Peter Anvin97a23472007-09-16 17:57:25 -0700681 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800682 hash_free(mmt);
H. Peter Anvin072771e2008-05-22 13:17:51 -0700683}
684
685static void free_macros(void)
686{
H. Peter Anvin166c2472008-05-28 12:28:58 -0700687 free_smacro_table(&smacros);
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800688 free_mmacro_table(&mmacros);
H. Peter Anvin97a23472007-09-16 17:57:25 -0700689}
690
691/*
692 * Initialize the hash tables
693 */
694static void init_macros(void)
695{
H. Peter Anvin97a23472007-09-16 17:57:25 -0700696}
697
698/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000699 * Pop the context stack.
700 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000701static void ctx_pop(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000702{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000703 Context *c = cstk;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000704
705 cstk = cstk->next;
H. Peter Anvin166c2472008-05-28 12:28:58 -0700706 free_smacro_table(&c->localmac);
H. Peter Anvin734b1882002-04-30 21:01:08 +0000707 nasm_free(c->name);
708 nasm_free(c);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000709}
710
H. Peter Anvin072771e2008-05-22 13:17:51 -0700711/*
712 * Search for a key in the hash index; adding it if necessary
713 * (in which case we initialize the data pointer to NULL.)
714 */
715static void **
716hash_findi_add(struct hash_table *hash, const char *str)
717{
718 struct hash_insert hi;
719 void **r;
720 char *strx;
H. Peter Anvin (Intel)ebb05a02018-12-11 12:30:25 -0800721 size_t l = strlen(str) + 1;
H. Peter Anvin072771e2008-05-22 13:17:51 -0700722
H. Peter Anvin (Intel)ebb05a02018-12-11 12:30:25 -0800723 r = hash_findib(hash, str, l, &hi);
H. Peter Anvin072771e2008-05-22 13:17:51 -0700724 if (r)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300725 return r;
H. Peter Anvin072771e2008-05-22 13:17:51 -0700726
H. Peter Anvin (Intel)ebb05a02018-12-11 12:30:25 -0800727 strx = nasm_malloc(l); /* Use a more efficient allocator here? */
728 memcpy(strx, str, l);
H. Peter Anvin072771e2008-05-22 13:17:51 -0700729 return hash_add(&hi, strx, NULL);
730}
731
732/*
733 * Like hash_findi, but returns the data element rather than a pointer
734 * to it. Used only when not adding a new element, hence no third
735 * argument.
736 */
737static void *
738hash_findix(struct hash_table *hash, const char *str)
739{
740 void **p;
741
742 p = hash_findi(hash, str, NULL);
743 return p ? *p : NULL;
744}
745
Cyrill Gorcunov15bdc512010-07-13 11:27:41 +0400746/*
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800747 * read line from standart macros set,
Cyrill Gorcunov15bdc512010-07-13 11:27:41 +0400748 * if there no more left -- return NULL
749 */
750static char *line_from_stdmac(void)
751{
752 unsigned char c;
753 const unsigned char *p = stdmacpos;
754 char *line, *q;
755 size_t len = 0;
756
757 if (!stdmacpos)
758 return NULL;
759
760 while ((c = *p++)) {
761 if (c >= 0x80)
762 len += pp_directives_len[c - 0x80] + 1;
763 else
764 len++;
765 }
766
767 line = nasm_malloc(len + 1);
768 q = line;
769 while ((c = *stdmacpos++)) {
770 if (c >= 0x80) {
771 memcpy(q, pp_directives[c - 0x80], pp_directives_len[c - 0x80]);
772 q += pp_directives_len[c - 0x80];
773 *q++ = ' ';
774 } else {
775 *q++ = c;
776 }
777 }
778 stdmacpos = p;
779 *q = '\0';
780
781 if (!*stdmacpos) {
H. Peter Anvinf7606612016-07-13 14:23:48 -0700782 /* This was the last of this particular macro set */
Cyrill Gorcunov15bdc512010-07-13 11:27:41 +0400783 stdmacpos = NULL;
H. Peter Anvinf7606612016-07-13 14:23:48 -0700784 if (*stdmacnext) {
785 stdmacpos = *stdmacnext++;
Cyrill Gorcunov15bdc512010-07-13 11:27:41 +0400786 } else if (do_predef) {
787 Line *pd, *l;
788 Token *head, **tail, *t;
789
790 /*
791 * Nasty hack: here we push the contents of
792 * `predef' on to the top-level expansion stack,
793 * since this is the most convenient way to
794 * implement the pre-include and pre-define
795 * features.
796 */
797 list_for_each(pd, predef) {
798 head = NULL;
799 tail = &head;
800 list_for_each(t, pd->first) {
801 *tail = new_Token(NULL, t->type, t->text, 0);
802 tail = &(*tail)->next;
803 }
804
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800805 l = nasm_malloc(sizeof(Line));
806 l->next = istk->expansion;
807 l->first = head;
808 l->finishes = NULL;
809
810 istk->expansion = l;
Cyrill Gorcunov15bdc512010-07-13 11:27:41 +0400811 }
812 do_predef = false;
813 }
814 }
815
816 return line;
817}
818
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000819static char *read_line(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000820{
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +0400821 unsigned int size, c, next;
822 const unsigned int delta = 512;
823 const unsigned int pad = 8;
824 unsigned int nr_cont = 0;
825 bool cont = false;
826 char *buffer, *p;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000827
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +0400828 /* Standart macros set (predefined) goes first */
Cyrill Gorcunov15bdc512010-07-13 11:27:41 +0400829 p = line_from_stdmac();
830 if (p)
831 return p;
H. Peter Anvin72edbb82008-06-19 16:00:04 -0700832
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +0400833 size = delta;
834 p = buffer = nasm_malloc(size);
835
836 for (;;) {
837 c = fgetc(istk->fp);
838 if ((int)(c) == EOF) {
839 p[0] = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000840 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000841 }
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +0400842
843 switch (c) {
844 case '\r':
845 next = fgetc(istk->fp);
846 if (next != '\n')
847 ungetc(next, istk->fp);
848 if (cont) {
849 cont = false;
850 continue;
851 }
852 break;
853
854 case '\n':
855 if (cont) {
856 cont = false;
857 continue;
858 }
859 break;
860
861 case '\\':
862 next = fgetc(istk->fp);
863 ungetc(next, istk->fp);
Cyrill Gorcunov490f85e2012-12-27 20:02:17 +0400864 if (next == '\r' || next == '\n') {
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +0400865 cont = true;
866 nr_cont++;
867 continue;
868 }
869 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000870 }
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +0400871
872 if (c == '\r' || c == '\n') {
873 *p++ = 0;
874 break;
875 }
876
877 if (p >= (buffer + size - pad)) {
878 buffer = nasm_realloc(buffer, size + delta);
879 p = buffer + size - pad;
880 size += delta;
881 }
882
883 *p++ = (unsigned char)c;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000884 }
885
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +0400886 if (p == buffer) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000887 nasm_free(buffer);
888 return NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000889 }
890
Cyrill Gorcunova5aea572010-11-11 10:14:45 +0300891 src_set_linnum(src_get_linnum() + istk->lineinc +
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +0400892 (nr_cont * istk->lineinc));
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000893
894 /*
895 * Handle spurious ^Z, which may be inserted into source files
896 * by some file transfer utilities.
897 */
898 buffer[strcspn(buffer, "\032")] = '\0';
899
H. Peter Anvin8ac25aa2016-02-18 01:16:18 -0800900 lfmt->line(LIST_READ, buffer);
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000901
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000902 return buffer;
903}
904
905/*
Keith Kaniosb7a89542007-04-12 02:40:54 +0000906 * Tokenize a line of text. This is a very simple process since we
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000907 * don't need to parse the value out of e.g. numeric tokens: we
908 * simply split one string into many.
909 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000910static Token *tokenize(char *line)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000911{
H. Peter Anvinca544db2008-10-19 19:30:11 -0700912 char c, *p = line;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +0000913 enum pp_token_type type;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000914 Token *list = NULL;
915 Token *t, **tail = &list;
916
H. Peter Anvine2c80182005-01-15 22:15:51 +0000917 while (*line) {
918 p = line;
919 if (*p == '%') {
920 p++;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300921 if (*p == '+' && !nasm_isdigit(p[1])) {
922 p++;
923 type = TOK_PASTE;
924 } else if (nasm_isdigit(*p) ||
925 ((*p == '-' || *p == '+') && nasm_isdigit(p[1]))) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000926 do {
927 p++;
928 }
H. Peter Anvinbda7a6e2008-06-21 10:23:17 -0700929 while (nasm_isdigit(*p));
H. Peter Anvine2c80182005-01-15 22:15:51 +0000930 type = TOK_PREPROC_ID;
931 } else if (*p == '{') {
932 p++;
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800933 while (*p) {
934 if (*p == '}')
935 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000936 p[-1] = *p;
937 p++;
938 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800939 if (*p != '}')
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -0800940 nasm_warn(WARN_OTHER, "unterminated %%{ construct");
H. Peter Anvine2c80182005-01-15 22:15:51 +0000941 p[-1] = '\0';
942 if (*p)
943 p++;
944 type = TOK_PREPROC_ID;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300945 } else if (*p == '[') {
946 int lvl = 1;
947 line += 2; /* Skip the leading %[ */
948 p++;
949 while (lvl && (c = *p++)) {
950 switch (c) {
951 case ']':
952 lvl--;
953 break;
954 case '%':
955 if (*p == '[')
956 lvl++;
957 break;
958 case '\'':
959 case '\"':
960 case '`':
Cyrill Gorcunov3144e842017-10-22 10:50:55 +0300961 p = nasm_skip_string(p - 1);
962 if (*p)
963 p++;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300964 break;
965 default:
966 break;
967 }
968 }
969 p--;
970 if (*p)
971 *p++ = '\0';
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800972 if (lvl)
Cyrill Gorcunov295b7952018-11-25 12:55:48 +0300973 nasm_nonfatalf(ERR_PASS1, "unterminated %%[ construct");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300974 type = TOK_INDIRECT;
975 } else if (*p == '?') {
976 type = TOK_PREPROC_Q; /* %? */
977 p++;
978 if (*p == '?') {
979 type = TOK_PREPROC_QQ; /* %?? */
980 p++;
981 }
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +0400982 } else if (*p == '!') {
983 type = TOK_PREPROC_ID;
984 p++;
H. Peter Anvin13506202018-11-28 14:55:58 -0800985 if (nasm_isidchar(*p)) {
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +0400986 do {
987 p++;
988 }
H. Peter Anvin13506202018-11-28 14:55:58 -0800989 while (nasm_isidchar(*p));
H. Peter Anvin53e2e4c2018-11-28 15:01:40 -0800990 } else if (nasm_isquote(*p)) {
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +0400991 p = nasm_skip_string(p);
992 if (*p)
993 p++;
994 else
Cyrill Gorcunov295b7952018-11-25 12:55:48 +0300995 nasm_nonfatalf(ERR_PASS1, "unterminated %%! string");
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +0400996 } else {
997 /* %! without string or identifier */
998 type = TOK_OTHER; /* Legacy behavior... */
999 }
H. Peter Anvin13506202018-11-28 14:55:58 -08001000 } else if (nasm_isidchar(*p) ||
H. Peter Anvine2c80182005-01-15 22:15:51 +00001001 ((*p == '!' || *p == '%' || *p == '$') &&
H. Peter Anvin13506202018-11-28 14:55:58 -08001002 nasm_isidchar(p[1]))) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001003 do {
1004 p++;
1005 }
H. Peter Anvin13506202018-11-28 14:55:58 -08001006 while (nasm_isidchar(*p));
H. Peter Anvine2c80182005-01-15 22:15:51 +00001007 type = TOK_PREPROC_ID;
1008 } else {
1009 type = TOK_OTHER;
1010 if (*p == '%')
1011 p++;
1012 }
H. Peter Anvin13506202018-11-28 14:55:58 -08001013 } else if (nasm_isidstart(*p) || (*p == '$' && nasm_isidstart(p[1]))) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001014 type = TOK_ID;
1015 p++;
H. Peter Anvin13506202018-11-28 14:55:58 -08001016 while (*p && nasm_isidchar(*p))
H. Peter Anvine2c80182005-01-15 22:15:51 +00001017 p++;
H. Peter Anvin53e2e4c2018-11-28 15:01:40 -08001018 } else if (nasm_isquote(*p)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001019 /*
1020 * A string token.
1021 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001022 type = TOK_STRING;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001023 p = nasm_skip_string(p);
Nickolay Yurchenkof3b3ce22003-09-21 20:38:43 +00001024
H. Peter Anvine2c80182005-01-15 22:15:51 +00001025 if (*p) {
1026 p++;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001027 } else {
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08001028 nasm_warn(WARN_OTHER, "unterminated string");
H. Peter Anvine2c80182005-01-15 22:15:51 +00001029 /* Handling unterminated strings by UNV */
1030 /* type = -1; */
1031 }
Victor van den Elzenfb5f2512009-04-17 16:17:59 +02001032 } else if (p[0] == '$' && p[1] == '$') {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001033 type = TOK_OTHER; /* TOKEN_BASE */
Victor van den Elzenfb5f2512009-04-17 16:17:59 +02001034 p += 2;
H. Peter Anvin13506202018-11-28 14:55:58 -08001035 } else if (nasm_isnumstart(*p)) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001036 bool is_hex = false;
1037 bool is_float = false;
1038 bool has_e = false;
1039 char c, *r;
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001040
H. Peter Anvine2c80182005-01-15 22:15:51 +00001041 /*
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001042 * A numeric token.
H. Peter Anvine2c80182005-01-15 22:15:51 +00001043 */
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001044
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001045 if (*p == '$') {
1046 p++;
1047 is_hex = true;
1048 }
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001049
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001050 for (;;) {
1051 c = *p++;
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001052
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001053 if (!is_hex && (c == 'e' || c == 'E')) {
1054 has_e = true;
1055 if (*p == '+' || *p == '-') {
1056 /*
1057 * e can only be followed by +/- if it is either a
1058 * prefixed hex number or a floating-point number
1059 */
1060 p++;
1061 is_float = true;
1062 }
1063 } else if (c == 'H' || c == 'h' || c == 'X' || c == 'x') {
1064 is_hex = true;
1065 } else if (c == 'P' || c == 'p') {
1066 is_float = true;
1067 if (*p == '+' || *p == '-')
1068 p++;
H. Peter Anvin13506202018-11-28 14:55:58 -08001069 } else if (nasm_isnumchar(c))
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001070 ; /* just advance */
1071 else if (c == '.') {
1072 /*
1073 * we need to deal with consequences of the legacy
1074 * parser, like "1.nolist" being two tokens
1075 * (TOK_NUMBER, TOK_ID) here; at least give it
1076 * a shot for now. In the future, we probably need
1077 * a flex-based scanner with proper pattern matching
1078 * to do it as well as it can be done. Nothing in
1079 * the world is going to help the person who wants
1080 * 0x123.p16 interpreted as two tokens, though.
1081 */
1082 r = p;
1083 while (*r == '_')
1084 r++;
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001085
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001086 if (nasm_isdigit(*r) || (is_hex && nasm_isxdigit(*r)) ||
1087 (!is_hex && (*r == 'e' || *r == 'E')) ||
1088 (*r == 'p' || *r == 'P')) {
1089 p = r;
1090 is_float = true;
1091 } else
1092 break; /* Terminate the token */
1093 } else
1094 break;
1095 }
1096 p--; /* Point to first character beyond number */
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001097
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001098 if (p == line+1 && *line == '$') {
1099 type = TOK_OTHER; /* TOKEN_HERE */
1100 } else {
1101 if (has_e && !is_hex) {
1102 /* 1e13 is floating-point, but 1e13h is not */
1103 is_float = true;
1104 }
H. Peter Anvind784a082009-04-20 14:01:18 -07001105
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001106 type = is_float ? TOK_FLOAT : TOK_NUMBER;
1107 }
H. Peter Anvinbda7a6e2008-06-21 10:23:17 -07001108 } else if (nasm_isspace(*p)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001109 type = TOK_WHITESPACE;
Cyrill Gorcunovf66ac7d2009-10-12 20:41:13 +04001110 p = nasm_skip_spaces(p);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001111 /*
1112 * Whitespace just before end-of-line is discarded by
1113 * pretending it's a comment; whitespace just before a
1114 * comment gets lumped into the comment.
1115 */
1116 if (!*p || *p == ';') {
1117 type = TOK_COMMENT;
1118 while (*p)
1119 p++;
1120 }
1121 } else if (*p == ';') {
1122 type = TOK_COMMENT;
1123 while (*p)
1124 p++;
1125 } else {
1126 /*
1127 * Anything else is an operator of some kind. We check
1128 * for all the double-character operators (>>, <<, //,
1129 * %%, <=, >=, ==, !=, <>, &&, ||, ^^), but anything
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001130 * else is a single-character operator.
H. Peter Anvine2c80182005-01-15 22:15:51 +00001131 */
1132 type = TOK_OTHER;
1133 if ((p[0] == '>' && p[1] == '>') ||
1134 (p[0] == '<' && p[1] == '<') ||
1135 (p[0] == '/' && p[1] == '/') ||
1136 (p[0] == '<' && p[1] == '=') ||
1137 (p[0] == '>' && p[1] == '=') ||
1138 (p[0] == '=' && p[1] == '=') ||
1139 (p[0] == '!' && p[1] == '=') ||
1140 (p[0] == '<' && p[1] == '>') ||
1141 (p[0] == '&' && p[1] == '&') ||
1142 (p[0] == '|' && p[1] == '|') ||
1143 (p[0] == '^' && p[1] == '^')) {
1144 p++;
1145 }
1146 p++;
1147 }
Nickolay Yurchenkof3b3ce22003-09-21 20:38:43 +00001148
H. Peter Anvine2c80182005-01-15 22:15:51 +00001149 /* Handling unterminated string by UNV */
1150 /*if (type == -1)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001151 {
1152 *tail = t = new_Token(NULL, TOK_STRING, line, p-line+1);
1153 t->text[p-line] = *line;
1154 tail = &t->next;
1155 }
1156 else */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001157 if (type != TOK_COMMENT) {
1158 *tail = t = new_Token(NULL, type, line, p - line);
1159 tail = &t->next;
1160 }
1161 line = p;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001162 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001163 return list;
1164}
1165
H. Peter Anvince616072002-04-30 21:02:23 +00001166/*
1167 * this function allocates a new managed block of memory and
H. Peter Anvin70653092007-10-19 14:42:29 -07001168 * returns a pointer to the block. The managed blocks are
H. Peter Anvince616072002-04-30 21:02:23 +00001169 * deleted only all at once by the delete_Blocks function.
1170 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001171static void *new_Block(size_t size)
H. Peter Anvince616072002-04-30 21:02:23 +00001172{
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001173 Blocks *b = &blocks;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001174
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001175 /* first, get to the end of the linked list */
1176 while (b->next)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001177 b = b->next;
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001178 /* now allocate the requested chunk */
1179 b->chunk = nasm_malloc(size);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001180
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001181 /* now allocate a new block for the next request */
Cyrill Gorcunovc31767c2014-06-28 02:22:17 +04001182 b->next = nasm_zalloc(sizeof(Blocks));
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001183 return b->chunk;
H. Peter Anvince616072002-04-30 21:02:23 +00001184}
1185
1186/*
1187 * this function deletes all managed blocks of memory
1188 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001189static void delete_Blocks(void)
H. Peter Anvince616072002-04-30 21:02:23 +00001190{
H. Peter Anvine2c80182005-01-15 22:15:51 +00001191 Blocks *a, *b = &blocks;
H. Peter Anvince616072002-04-30 21:02:23 +00001192
H. Peter Anvin70653092007-10-19 14:42:29 -07001193 /*
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001194 * keep in mind that the first block, pointed to by blocks
H. Peter Anvin70653092007-10-19 14:42:29 -07001195 * is a static and not dynamically allocated, so we don't
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001196 * free it.
1197 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001198 while (b) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001199 if (b->chunk)
1200 nasm_free(b->chunk);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001201 a = b;
1202 b = b->next;
1203 if (a != &blocks)
1204 nasm_free(a);
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001205 }
Cyrill Gorcunovdae24d72014-06-28 10:17:39 +04001206 memset(&blocks, 0, sizeof(blocks));
H. Peter Anvine2c80182005-01-15 22:15:51 +00001207}
H. Peter Anvin734b1882002-04-30 21:01:08 +00001208
1209/*
H. Peter Anvin70653092007-10-19 14:42:29 -07001210 * this function creates a new Token and passes a pointer to it
H. Peter Anvin734b1882002-04-30 21:01:08 +00001211 * back to the caller. It sets the type and text elements, and
H. Peter Anvinf26e0972008-07-01 21:26:27 -07001212 * also the a.mac and next elements to NULL.
H. Peter Anvin734b1882002-04-30 21:01:08 +00001213 */
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07001214static Token *new_Token(Token * next, enum pp_token_type type,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001215 const char *text, int txtlen)
H. Peter Anvin734b1882002-04-30 21:01:08 +00001216{
1217 Token *t;
1218 int i;
1219
H. Peter Anvin89cee572009-07-15 09:16:54 -04001220 if (!freeTokens) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001221 freeTokens = (Token *) new_Block(TOKEN_BLOCKSIZE * sizeof(Token));
1222 for (i = 0; i < TOKEN_BLOCKSIZE - 1; i++)
1223 freeTokens[i].next = &freeTokens[i + 1];
1224 freeTokens[i].next = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001225 }
1226 t = freeTokens;
1227 freeTokens = t->next;
1228 t->next = next;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07001229 t->a.mac = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001230 t->type = type;
H. Peter Anvin89cee572009-07-15 09:16:54 -04001231 if (type == TOK_WHITESPACE || !text) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001232 t->text = NULL;
1233 } else {
1234 if (txtlen == 0)
1235 txtlen = strlen(text);
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07001236 t->text = nasm_malloc(txtlen+1);
1237 memcpy(t->text, text, txtlen);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001238 t->text[txtlen] = '\0';
H. Peter Anvin734b1882002-04-30 21:01:08 +00001239 }
1240 return t;
1241}
1242
H. Peter Anvine2c80182005-01-15 22:15:51 +00001243static Token *delete_Token(Token * t)
H. Peter Anvin734b1882002-04-30 21:01:08 +00001244{
1245 Token *next = t->next;
1246 nasm_free(t->text);
H. Peter Anvin788e6c12002-04-30 21:02:01 +00001247 t->next = freeTokens;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001248 freeTokens = t;
1249 return next;
1250}
1251
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001252/*
1253 * Convert a line of tokens back into text.
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001254 * If expand_locals is not zero, identifiers of the form "%$*xxx"
1255 * will be transformed into ..@ctxnum.xxx
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001256 */
H. Peter Anvin9e200162008-06-04 17:23:14 -07001257static char *detoken(Token * tlist, bool expand_locals)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001258{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001259 Token *t;
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001260 char *line, *p;
H. Peter Anvinb4daadc2008-01-21 16:31:57 -08001261 const char *q;
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001262 int len = 0;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001263
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001264 list_for_each(t, tlist) {
Cyrill Gorcunov9b7ee092017-10-22 21:42:59 +03001265 if (t->type == TOK_PREPROC_ID && t->text &&
1266 t->text[0] && t->text[1] == '!') {
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001267 char *v;
1268 char *q = t->text;
H. Peter Anvin077fb932010-07-20 14:56:30 -07001269
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001270 v = t->text + 2;
H. Peter Anvin53e2e4c2018-11-28 15:01:40 -08001271 if (nasm_isquote(*v)) {
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001272 size_t len = nasm_unquote(v, NULL);
1273 size_t clen = strlen(v);
H. Peter Anvin077fb932010-07-20 14:56:30 -07001274
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001275 if (len != clen) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03001276 nasm_nonfatalf(ERR_PASS1, "NUL character in %%! string");
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001277 v = NULL;
1278 }
1279 }
H. Peter Anvin077fb932010-07-20 14:56:30 -07001280
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001281 if (v) {
1282 char *p = getenv(v);
1283 if (!p) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03001284 nasm_nonfatalf(ERR_PASS1, "nonexistent environment variable `%s'", v);
Cyrill Gorcunovbbb7a1a2016-06-19 12:15:24 +03001285 /*
1286 * FIXME We better should investigate if accessing
1287 * ->text[1] without ->text[0] is safe enough.
1288 */
1289 t->text = nasm_zalloc(2);
1290 } else
1291 t->text = nasm_strdup(p);
Cyrill Gorcunov75004872017-07-26 01:21:16 +03001292 nasm_free(q);
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001293 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001294 }
H. Peter Anvin077fb932010-07-20 14:56:30 -07001295
H. Peter Anvine2c80182005-01-15 22:15:51 +00001296 /* Expand local macros here and not during preprocessing */
1297 if (expand_locals &&
1298 t->type == TOK_PREPROC_ID && t->text &&
1299 t->text[0] == '%' && t->text[1] == '$') {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001300 const char *q;
1301 char *p;
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04001302 Context *ctx = get_ctx(t->text, &q);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001303 if (ctx) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001304 char buffer[40];
Keith Kanios93f2e9a2007-04-14 00:10:59 +00001305 snprintf(buffer, sizeof(buffer), "..@%"PRIu32".", ctx->number);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001306 p = nasm_strcat(buffer, q);
1307 nasm_free(t->text);
1308 t->text = p;
1309 }
1310 }
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001311 if (t->type == TOK_WHITESPACE)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001312 len++;
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001313 else if (t->text)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001314 len += strlen(t->text);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001315 }
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001316
H. Peter Anvin734b1882002-04-30 21:01:08 +00001317 p = line = nasm_malloc(len + 1);
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001318
1319 list_for_each(t, tlist) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001320 if (t->type == TOK_WHITESPACE) {
H. Peter Anvinb4daadc2008-01-21 16:31:57 -08001321 *p++ = ' ';
H. Peter Anvine2c80182005-01-15 22:15:51 +00001322 } else if (t->text) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001323 q = t->text;
1324 while (*q)
1325 *p++ = *q++;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001326 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001327 }
1328 *p = '\0';
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001329
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001330 return line;
1331}
1332
1333/*
H. Peter Anvin76690a12002-04-30 20:52:49 +00001334 * A scanner, suitable for use by the expression evaluator, which
1335 * operates on a line of Tokens. Expects a pointer to a pointer to
1336 * the first token in the line to be passed in as its private_data
1337 * field.
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001338 *
1339 * FIX: This really needs to be unified with stdscan.
H. Peter Anvin76690a12002-04-30 20:52:49 +00001340 */
H. Peter Anvin8b262472019-02-26 14:00:54 -08001341struct ppscan {
1342 Token *tptr;
1343 int ntokens;
1344};
1345
H. Peter Anvine2c80182005-01-15 22:15:51 +00001346static int ppscan(void *private_data, struct tokenval *tokval)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001347{
H. Peter Anvin8b262472019-02-26 14:00:54 -08001348 struct ppscan *pps = private_data;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001349 Token *tline;
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001350 char ourcopy[MAX_KEYWORD+1], *p, *r, *s;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001351
H. Peter Anvine2c80182005-01-15 22:15:51 +00001352 do {
H. Peter Anvin8b262472019-02-26 14:00:54 -08001353 if (pps->ntokens && (tline = pps->tptr)) {
1354 pps->ntokens--;
1355 pps->tptr = tline->next;
1356 } else {
1357 pps->tptr = NULL;
1358 pps->ntokens = 0;
1359 return tokval->t_type = TOKEN_EOS;
1360 }
1361 } while (tline->type == TOK_WHITESPACE || tline->type == TOK_COMMENT);
H. Peter Anvin76690a12002-04-30 20:52:49 +00001362
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001363 tokval->t_charptr = tline->text;
1364
H. Peter Anvin76690a12002-04-30 20:52:49 +00001365 if (tline->text[0] == '$' && !tline->text[1])
H. Peter Anvine2c80182005-01-15 22:15:51 +00001366 return tokval->t_type = TOKEN_HERE;
H. Peter Anvin7cf897e2002-05-30 21:30:33 +00001367 if (tline->text[0] == '$' && tline->text[1] == '$' && !tline->text[2])
H. Peter Anvine2c80182005-01-15 22:15:51 +00001368 return tokval->t_type = TOKEN_BASE;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001369
H. Peter Anvine2c80182005-01-15 22:15:51 +00001370 if (tline->type == TOK_ID) {
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001371 p = tokval->t_charptr = tline->text;
1372 if (p[0] == '$') {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001373 tokval->t_charptr++;
1374 return tokval->t_type = TOKEN_ID;
1375 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00001376
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001377 for (r = p, s = ourcopy; *r; r++) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001378 if (r >= p+MAX_KEYWORD)
1379 return tokval->t_type = TOKEN_ID; /* Not a keyword */
H. Peter Anvinac8f8fc2008-06-11 15:49:41 -07001380 *s++ = nasm_tolower(*r);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001381 }
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001382 *s = '\0';
1383 /* right, so we have an identifier sitting in temp storage. now,
1384 * is it actually a register or instruction name, or what? */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001385 return nasm_token_hash(ourcopy, tokval);
H. Peter Anvin76690a12002-04-30 20:52:49 +00001386 }
1387
H. Peter Anvine2c80182005-01-15 22:15:51 +00001388 if (tline->type == TOK_NUMBER) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001389 bool rn_error;
1390 tokval->t_integer = readnum(tline->text, &rn_error);
1391 tokval->t_charptr = tline->text;
1392 if (rn_error)
1393 return tokval->t_type = TOKEN_ERRNUM;
1394 else
1395 return tokval->t_type = TOKEN_NUM;
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001396 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00001397
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001398 if (tline->type == TOK_FLOAT) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001399 return tokval->t_type = TOKEN_FLOAT;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001400 }
1401
H. Peter Anvine2c80182005-01-15 22:15:51 +00001402 if (tline->type == TOK_STRING) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001403 char bq, *ep;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001404
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001405 bq = tline->text[0];
H. Peter Anvin11627042008-06-09 20:45:19 -07001406 tokval->t_charptr = tline->text;
1407 tokval->t_inttwo = nasm_unquote(tline->text, &ep);
H. Peter Anvind2456592008-06-19 15:04:18 -07001408
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001409 if (ep[0] != bq || ep[1] != '\0')
1410 return tokval->t_type = TOKEN_ERRSTR;
1411 else
1412 return tokval->t_type = TOKEN_STR;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001413 }
1414
H. Peter Anvine2c80182005-01-15 22:15:51 +00001415 if (tline->type == TOK_OTHER) {
1416 if (!strcmp(tline->text, "<<"))
1417 return tokval->t_type = TOKEN_SHL;
1418 if (!strcmp(tline->text, ">>"))
1419 return tokval->t_type = TOKEN_SHR;
1420 if (!strcmp(tline->text, "//"))
1421 return tokval->t_type = TOKEN_SDIV;
1422 if (!strcmp(tline->text, "%%"))
1423 return tokval->t_type = TOKEN_SMOD;
1424 if (!strcmp(tline->text, "=="))
1425 return tokval->t_type = TOKEN_EQ;
1426 if (!strcmp(tline->text, "<>"))
1427 return tokval->t_type = TOKEN_NE;
1428 if (!strcmp(tline->text, "!="))
1429 return tokval->t_type = TOKEN_NE;
1430 if (!strcmp(tline->text, "<="))
1431 return tokval->t_type = TOKEN_LE;
1432 if (!strcmp(tline->text, ">="))
1433 return tokval->t_type = TOKEN_GE;
1434 if (!strcmp(tline->text, "&&"))
1435 return tokval->t_type = TOKEN_DBL_AND;
1436 if (!strcmp(tline->text, "^^"))
1437 return tokval->t_type = TOKEN_DBL_XOR;
1438 if (!strcmp(tline->text, "||"))
1439 return tokval->t_type = TOKEN_DBL_OR;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001440 }
1441
1442 /*
1443 * We have no other options: just return the first character of
1444 * the token text.
1445 */
1446 return tokval->t_type = tline->text[0];
1447}
1448
1449/*
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001450 * Compare a string to the name of an existing macro; this is a
1451 * simple wrapper which calls either strcmp or nasm_stricmp
1452 * depending on the value of the `casesense' parameter.
1453 */
H. Peter Anvin4db5a162007-10-11 13:42:09 -07001454static int mstrcmp(const char *p, const char *q, bool casesense)
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001455{
H. Peter Anvin734b1882002-04-30 21:01:08 +00001456 return casesense ? strcmp(p, q) : nasm_stricmp(p, q);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001457}
1458
1459/*
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07001460 * Compare a string to the name of an existing macro; this is a
1461 * simple wrapper which calls either strcmp or nasm_stricmp
1462 * depending on the value of the `casesense' parameter.
1463 */
1464static int mmemcmp(const char *p, const char *q, size_t l, bool casesense)
1465{
1466 return casesense ? memcmp(p, q, l) : nasm_memicmp(p, q, l);
1467}
1468
1469/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001470 * Return the Context structure associated with a %$ token. Return
1471 * NULL, having _already_ reported an error condition, if the
1472 * context stack isn't deep enough for the supplied number of $
1473 * signs.
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001474 *
1475 * If "namep" is non-NULL, set it to the pointer to the macro name
1476 * tail, i.e. the part beyond %$...
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001477 */
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04001478static Context *get_ctx(const char *name, const char **namep)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001479{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001480 Context *ctx;
1481 int i;
1482
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001483 if (namep)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001484 *namep = name;
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001485
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001486 if (!name || name[0] != '%' || name[1] != '$')
H. Peter Anvine2c80182005-01-15 22:15:51 +00001487 return NULL;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001488
H. Peter Anvine2c80182005-01-15 22:15:51 +00001489 if (!cstk) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03001490 nasm_nonfatal("`%s': context stack is empty", name);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001491 return NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001492 }
1493
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001494 name += 2;
1495 ctx = cstk;
1496 i = 0;
1497 while (ctx && *name == '$') {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001498 name++;
1499 i++;
1500 ctx = ctx->next;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001501 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001502 if (!ctx) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03001503 nasm_nonfatal("`%s': context stack is only"
1504 " %d level%s deep", name, i, (i == 1 ? "" : "s"));
H. Peter Anvine2c80182005-01-15 22:15:51 +00001505 return NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001506 }
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001507
1508 if (namep)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001509 *namep = name;
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001510
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04001511 return ctx;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001512}
1513
1514/*
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001515 * Open an include file. This routine must always return a valid
1516 * file pointer if it returns - it's responsible for throwing an
1517 * ERR_FATAL and bombing out completely if not. It should also try
1518 * the include path one by one until it finds the file or reaches
1519 * the end of the path.
H. Peter Anvind81a2352016-09-21 14:03:18 -07001520 *
1521 * Note: for INC_PROBE the function returns NULL at all times;
1522 * instead look for the
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001523 */
H. Peter Anvind81a2352016-09-21 14:03:18 -07001524enum incopen_mode {
1525 INC_NEEDED, /* File must exist */
1526 INC_OPTIONAL, /* Missing is OK */
1527 INC_PROBE /* Only an existence probe */
1528};
1529
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001530/* This is conducts a full pathname search */
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07001531static FILE *inc_fopen_search(const char *file, char **slpath,
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001532 enum incopen_mode omode, enum file_flags fmode)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001533{
H. Peter Anvin (Intel)64471092018-12-11 13:06:14 -08001534 const struct strlist_entry *ip = strlist_head(ipath_list);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001535 FILE *fp;
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07001536 const char *prefix = "";
night199ukfdb1a1b2018-10-18 23:19:47 +02001537 char *sp;
H. Peter Anvind81a2352016-09-21 14:03:18 -07001538 bool found;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001539
H. Peter Anvine2c80182005-01-15 22:15:51 +00001540 while (1) {
night199ukfdb1a1b2018-10-18 23:19:47 +02001541 sp = nasm_catfile(prefix, file);
H. Peter Anvind81a2352016-09-21 14:03:18 -07001542 if (omode == INC_PROBE) {
1543 fp = NULL;
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07001544 found = nasm_file_exists(sp);
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001545 } else {
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07001546 fp = nasm_open_read(sp, fmode);
H. Peter Anvind81a2352016-09-21 14:03:18 -07001547 found = (fp != NULL);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001548 }
H. Peter Anvind81a2352016-09-21 14:03:18 -07001549 if (found) {
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07001550 *slpath = sp;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001551 return fp;
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001552 }
Jim Kukunas65a8afc2016-06-13 16:00:42 -04001553
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07001554 nasm_free(sp);
Jim Kukunas65a8afc2016-06-13 16:00:42 -04001555
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07001556 if (!ip) {
1557 *slpath = NULL;
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001558 return NULL;
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07001559 }
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001560
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07001561 prefix = ip->str;
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001562 ip = ip->next;
1563 }
1564}
1565
1566/*
1567 * Open a file, or test for the presence of one (depending on omode),
1568 * considering the include path.
1569 */
1570static FILE *inc_fopen(const char *file,
Cyrill Gorcunovb7bb5ac2018-11-11 21:33:52 +03001571 struct strlist *dhead,
H. Peter Anvinccad6f92016-10-04 00:34:35 -07001572 const char **found_path,
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001573 enum incopen_mode omode,
1574 enum file_flags fmode)
1575{
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001576 struct hash_insert hi;
1577 void **hp;
1578 char *path;
1579 FILE *fp = NULL;
1580
1581 hp = hash_find(&FileHash, file, &hi);
1582 if (hp) {
1583 path = *hp;
Martin Storsjöf283c8f2017-08-13 17:28:46 +03001584 if (path || omode != INC_NEEDED) {
Cyrill Gorcunovb7bb5ac2018-11-11 21:33:52 +03001585 strlist_add(dhead, path ? path : file);
Martin Storsjöf283c8f2017-08-13 17:28:46 +03001586 }
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001587 } else {
1588 /* Need to do the actual path search */
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07001589 fp = inc_fopen_search(file, &path, omode, fmode);
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001590
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07001591 /* Positive or negative result */
1592 hash_add(&hi, nasm_strdup(file), path);
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001593
H. Peter Anvin9924d1e2016-10-04 00:59:39 -07001594 /*
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07001595 * Add file to dependency path.
H. Peter Anvin9924d1e2016-10-04 00:59:39 -07001596 */
1597 if (path || omode != INC_NEEDED)
Cyrill Gorcunovb7bb5ac2018-11-11 21:33:52 +03001598 strlist_add(dhead, file);
H. Peter Anvineba20a72002-04-30 20:53:55 +00001599 }
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001600
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001601 if (!path) {
1602 if (omode == INC_NEEDED)
H. Peter Anvinc5136902018-06-15 18:20:17 -07001603 nasm_fatal("unable to open include file `%s'", file);
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07001604 } else {
1605 if (!fp && omode != INC_PROBE)
1606 fp = nasm_open_read(path, fmode);
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001607 }
1608
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001609 if (found_path)
H. Peter Anvinccad6f92016-10-04 00:34:35 -07001610 *found_path = path;
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001611
1612 return fp;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001613}
1614
1615/*
Fabian Giesen0bbc38d2016-04-28 13:48:14 -07001616 * Opens an include or input file. Public version, for use by modules
1617 * that get a file:lineno pair and need to look at the file again
1618 * (e.g. the CodeView debug backend). Returns NULL on failure.
1619 */
H. Peter Anvin3e83cec2016-05-25 04:28:46 -07001620FILE *pp_input_fopen(const char *filename, enum file_flags mode)
Fabian Giesen0bbc38d2016-04-28 13:48:14 -07001621{
H. Peter Anvin9924d1e2016-10-04 00:59:39 -07001622 return inc_fopen(filename, NULL, NULL, INC_OPTIONAL, mode);
Fabian Giesen0bbc38d2016-04-28 13:48:14 -07001623}
1624
1625/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001626 * Determine if we should warn on defining a single-line macro of
H. Peter Anvinef7468f2002-04-30 20:57:59 +00001627 * name `name', with `nparam' parameters. If nparam is 0 or -1, will
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001628 * return true if _any_ single-line macro of that name is defined.
1629 * Otherwise, will return true if a single-line macro with either
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001630 * `nparam' or no parameters is defined.
1631 *
1632 * If a macro with precisely the right number of parameters is
H. Peter Anvinef7468f2002-04-30 20:57:59 +00001633 * defined, or nparam is -1, the address of the definition structure
1634 * will be returned in `defn'; otherwise NULL will be returned. If `defn'
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001635 * is NULL, no action will be taken regarding its contents, and no
1636 * error will occur.
1637 *
1638 * Note that this is also called with nparam zero to resolve
1639 * `ifdef'.
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001640 *
1641 * If you already know which context macro belongs to, you can pass
1642 * the context pointer as first parameter; if you won't but name begins
1643 * with %$ the context will be automatically computed. If all_contexts
1644 * is true, macro will be searched in outer contexts as well.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001645 */
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07001646static bool
H. Peter Anvinb2a5fda2008-06-19 21:42:42 -07001647smacro_defined(Context * ctx, const char *name, int nparam, SMacro ** defn,
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07001648 bool nocase)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001649{
H. Peter Anvin166c2472008-05-28 12:28:58 -07001650 struct hash_table *smtbl;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001651 SMacro *m;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001652
H. Peter Anvin97a23472007-09-16 17:57:25 -07001653 if (ctx) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001654 smtbl = &ctx->localmac;
H. Peter Anvin97a23472007-09-16 17:57:25 -07001655 } else if (name[0] == '%' && name[1] == '$') {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001656 if (cstk)
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04001657 ctx = get_ctx(name, &name);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001658 if (!ctx)
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001659 return false; /* got to return _something_ */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001660 smtbl = &ctx->localmac;
H. Peter Anvin97a23472007-09-16 17:57:25 -07001661 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001662 smtbl = &smacros;
H. Peter Anvin97a23472007-09-16 17:57:25 -07001663 }
H. Peter Anvin166c2472008-05-28 12:28:58 -07001664 m = (SMacro *) hash_findix(smtbl, name);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001665
H. Peter Anvine2c80182005-01-15 22:15:51 +00001666 while (m) {
1667 if (!mstrcmp(m->name, name, m->casesense && nocase) &&
Charles Crayne192d5b52007-10-18 19:02:42 -07001668 (nparam <= 0 || m->nparam == 0 || nparam == (int) m->nparam)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001669 if (defn) {
Charles Crayne192d5b52007-10-18 19:02:42 -07001670 if (nparam == (int) m->nparam || nparam == -1)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001671 *defn = m;
1672 else
1673 *defn = NULL;
1674 }
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001675 return true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001676 }
1677 m = m->next;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001678 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001679
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001680 return false;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001681}
1682
Cyrill Gorcunov3079f792018-11-14 10:03:42 +03001683/* param should be a natural number [0; INT_MAX] */
1684static int read_param_count(const char *str)
1685{
1686 int result;
1687 bool err;
1688
1689 result = readnum(str, &err);
1690 if (result < 0 || result > INT_MAX) {
1691 result = 0;
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03001692 nasm_nonfatal("parameter count `%s' is out of bounds [%d; %d]",
1693 str, 0, INT_MAX);
1694 } else if (err)
1695 nasm_nonfatal("unable to parse parameter count `%s'", str);
Cyrill Gorcunov3079f792018-11-14 10:03:42 +03001696 return result;
1697}
1698
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001699/*
1700 * Count and mark off the parameters in a multi-line macro call.
1701 * This is called both from within the multi-line macro expansion
1702 * code, and also to mark off the default parameters when provided
1703 * in a %macro definition line.
1704 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001705static void count_mmac_params(Token * t, int *nparam, Token *** params)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001706{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001707 int paramsize, brace;
1708
1709 *nparam = paramsize = 0;
1710 *params = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001711 while (t) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001712 /* +1: we need space for the final NULL */
H. Peter Anvin91fb6f12008-09-01 10:56:33 -07001713 if (*nparam+1 >= paramsize) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001714 paramsize += PARAM_DELTA;
1715 *params = nasm_realloc(*params, sizeof(**params) * paramsize);
1716 }
1717 skip_white_(t);
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08001718 brace = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001719 if (tok_is_(t, "{"))
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08001720 brace++;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001721 (*params)[(*nparam)++] = t;
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08001722 if (brace) {
1723 while (brace && (t = t->next) != NULL) {
1724 if (tok_is_(t, "{"))
1725 brace++;
1726 else if (tok_is_(t, "}"))
1727 brace--;
1728 }
1729
1730 if (t) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001731 /*
1732 * Now we've found the closing brace, look further
1733 * for the comma.
1734 */
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08001735 t = t->next;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001736 skip_white_(t);
1737 if (tok_isnt_(t, ",")) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03001738 nasm_nonfatal("braces do not enclose all of macro parameter");
H. Peter Anvine2c80182005-01-15 22:15:51 +00001739 while (tok_isnt_(t, ","))
1740 t = t->next;
1741 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001742 }
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08001743 } else {
1744 while (tok_isnt_(t, ","))
1745 t = t->next;
1746 }
1747 if (t) { /* got a comma/brace */
1748 t = t->next; /* eat the comma */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001749 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001750 }
1751}
1752
1753/*
H. Peter Anvin76690a12002-04-30 20:52:49 +00001754 * Determine whether one of the various `if' conditions is true or
1755 * not.
1756 *
1757 * We must free the tline we get passed.
1758 */
H. Peter Anvin70055962007-10-11 00:05:31 -07001759static bool if_condition(Token * tline, enum preproc_token ct)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001760{
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001761 enum pp_conditional i = PP_COND(ct);
H. Peter Anvin70055962007-10-11 00:05:31 -07001762 bool j;
H. Peter Anvin8b262472019-02-26 14:00:54 -08001763 Token *t, *tt, *origline;
1764 struct ppscan pps;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001765 struct tokenval tokval;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001766 expr *evalresult;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001767 enum pp_token_type needtype;
H. Peter Anvin077fb932010-07-20 14:56:30 -07001768 char *p;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001769
1770 origline = tline;
1771
H. Peter Anvine2c80182005-01-15 22:15:51 +00001772 switch (i) {
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001773 case PPC_IFCTX:
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001774 j = false; /* have we matched yet? */
Victor van den Elzen0e857f12008-07-23 13:21:29 +02001775 while (true) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001776 skip_white_(tline);
Victor van den Elzen0e857f12008-07-23 13:21:29 +02001777 if (!tline)
1778 break;
1779 if (tline->type != TOK_ID) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03001780 nasm_nonfatal("`%s' expects context identifiers",
1781 pp_directives[ct]);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001782 free_tlist(origline);
1783 return -1;
1784 }
Victor van den Elzen0e857f12008-07-23 13:21:29 +02001785 if (cstk && cstk->name && !nasm_stricmp(tline->text, cstk->name))
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001786 j = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001787 tline = tline->next;
1788 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001789 break;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001790
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001791 case PPC_IFDEF:
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001792 j = false; /* have we matched yet? */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001793 while (tline) {
1794 skip_white_(tline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001795 if (!tline || (tline->type != TOK_ID &&
1796 (tline->type != TOK_PREPROC_ID ||
1797 tline->text[1] != '$'))) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03001798 nasm_nonfatal("`%s' expects macro identifiers",
1799 pp_directives[ct]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001800 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001801 }
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07001802 if (smacro_defined(NULL, tline->text, 0, NULL, true))
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001803 j = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001804 tline = tline->next;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001805 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001806 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001807
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001808 case PPC_IFENV:
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001809 tline = expand_smacro(tline);
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001810 j = false; /* have we matched yet? */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001811 while (tline) {
1812 skip_white_(tline);
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001813 if (!tline || (tline->type != TOK_ID &&
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001814 tline->type != TOK_STRING &&
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001815 (tline->type != TOK_PREPROC_ID ||
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001816 tline->text[1] != '!'))) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03001817 nasm_nonfatal("`%s' expects environment variable names",
1818 pp_directives[ct]);
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001819 goto fail;
1820 }
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001821 p = tline->text;
1822 if (tline->type == TOK_PREPROC_ID)
1823 p += 2; /* Skip leading %! */
H. Peter Anvin53e2e4c2018-11-28 15:01:40 -08001824 if (nasm_isquote(*p))
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001825 nasm_unquote_cstr(p, ct);
1826 if (getenv(p))
1827 j = true;
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001828 tline = tline->next;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001829 }
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001830 break;
1831
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001832 case PPC_IFIDN:
1833 case PPC_IFIDNI:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001834 tline = expand_smacro(tline);
1835 t = tt = tline;
1836 while (tok_isnt_(tt, ","))
1837 tt = tt->next;
1838 if (!tt) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03001839 nasm_nonfatal("`%s' expects two comma-separated arguments",
1840 pp_directives[ct]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001841 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001842 }
1843 tt = tt->next;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001844 j = true; /* assume equality unless proved not */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001845 while ((t->type != TOK_OTHER || strcmp(t->text, ",")) && tt) {
1846 if (tt->type == TOK_OTHER && !strcmp(tt->text, ",")) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03001847 nasm_nonfatal("`%s': more than one comma on line",
1848 pp_directives[ct]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001849 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001850 }
1851 if (t->type == TOK_WHITESPACE) {
1852 t = t->next;
1853 continue;
1854 }
1855 if (tt->type == TOK_WHITESPACE) {
1856 tt = tt->next;
1857 continue;
1858 }
1859 if (tt->type != t->type) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001860 j = false; /* found mismatching tokens */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001861 break;
1862 }
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07001863 /* When comparing strings, need to unquote them first */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001864 if (t->type == TOK_STRING) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001865 size_t l1 = nasm_unquote(t->text, NULL);
1866 size_t l2 = nasm_unquote(tt->text, NULL);
H. Peter Anvind2456592008-06-19 15:04:18 -07001867
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001868 if (l1 != l2) {
1869 j = false;
1870 break;
1871 }
1872 if (mmemcmp(t->text, tt->text, l1, i == PPC_IFIDN)) {
1873 j = false;
1874 break;
1875 }
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07001876 } else if (mstrcmp(tt->text, t->text, i == PPC_IFIDN) != 0) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001877 j = false; /* found mismatching tokens */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001878 break;
1879 }
Nickolay Yurchenkof3b3ce22003-09-21 20:38:43 +00001880
H. Peter Anvine2c80182005-01-15 22:15:51 +00001881 t = t->next;
1882 tt = tt->next;
1883 }
1884 if ((t->type != TOK_OTHER || strcmp(t->text, ",")) || tt)
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001885 j = false; /* trailing gunk on one end or other */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001886 break;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001887
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001888 case PPC_IFMACRO:
H. Peter Anvin89cee572009-07-15 09:16:54 -04001889 {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001890 bool found = false;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001891 MMacro searching, *mmac;
H. Peter Anvin65747262002-05-07 00:10:05 +00001892
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001893 skip_white_(tline);
1894 tline = expand_id(tline);
1895 if (!tok_type_(tline, TOK_ID)) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03001896 nasm_nonfatal("`%s' expects a macro name", pp_directives[ct]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001897 goto fail;
1898 }
1899 searching.name = nasm_strdup(tline->text);
1900 searching.casesense = true;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001901 searching.plus = false;
1902 searching.nolist = false;
1903 searching.in_progress = 0;
1904 searching.max_depth = 0;
1905 searching.rep_nest = NULL;
1906 searching.nparam_min = 0;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001907 searching.nparam_max = INT_MAX;
1908 tline = expand_smacro(tline->next);
1909 skip_white_(tline);
1910 if (!tline) {
1911 } else if (!tok_type_(tline, TOK_NUMBER)) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03001912 nasm_nonfatal("`%s' expects a parameter count or nothing",
1913 pp_directives[ct]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001914 } else {
1915 searching.nparam_min = searching.nparam_max =
Cyrill Gorcunov3079f792018-11-14 10:03:42 +03001916 read_param_count(tline->text);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001917 }
1918 if (tline && tok_is_(tline->next, "-")) {
1919 tline = tline->next->next;
1920 if (tok_is_(tline, "*"))
1921 searching.nparam_max = INT_MAX;
1922 else if (!tok_type_(tline, TOK_NUMBER))
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03001923 nasm_nonfatal("`%s' expects a parameter count after `-'",
1924 pp_directives[ct]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001925 else {
Cyrill Gorcunov3079f792018-11-14 10:03:42 +03001926 searching.nparam_max = read_param_count(tline->text);
Cyrill Gorcunovc9244ea2017-10-22 15:25:48 +03001927 if (searching.nparam_min > searching.nparam_max) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03001928 nasm_nonfatal("minimum parameter count exceeds maximum");
Cyrill Gorcunovc9244ea2017-10-22 15:25:48 +03001929 searching.nparam_max = searching.nparam_min;
1930 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001931 }
1932 }
1933 if (tline && tok_is_(tline->next, "+")) {
1934 tline = tline->next;
1935 searching.plus = true;
1936 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001937 mmac = (MMacro *) hash_findix(&mmacros, searching.name);
1938 while (mmac) {
1939 if (!strcmp(mmac->name, searching.name) &&
1940 (mmac->nparam_min <= searching.nparam_max
1941 || searching.plus)
1942 && (searching.nparam_min <= mmac->nparam_max
1943 || mmac->plus)) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001944 found = true;
1945 break;
1946 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001947 mmac = mmac->next;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001948 }
1949 if (tline && tline->next)
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08001950 nasm_warn(WARN_OTHER, "trailing garbage after %%ifmacro ignored");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001951 nasm_free(searching.name);
1952 j = found;
1953 break;
H. Peter Anvin89cee572009-07-15 09:16:54 -04001954 }
H. Peter Anvin65747262002-05-07 00:10:05 +00001955
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001956 case PPC_IFID:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001957 needtype = TOK_ID;
1958 goto iftype;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001959 case PPC_IFNUM:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001960 needtype = TOK_NUMBER;
1961 goto iftype;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001962 case PPC_IFSTR:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001963 needtype = TOK_STRING;
1964 goto iftype;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001965
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001966iftype:
1967 t = tline = expand_smacro(tline);
H. Peter Anvind85d2502008-05-04 17:53:31 -07001968
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001969 while (tok_type_(t, TOK_WHITESPACE) ||
1970 (needtype == TOK_NUMBER &&
1971 tok_type_(t, TOK_OTHER) &&
1972 (t->text[0] == '-' || t->text[0] == '+') &&
1973 !t->text[1]))
1974 t = t->next;
H. Peter Anvind85d2502008-05-04 17:53:31 -07001975
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001976 j = tok_type_(t, needtype);
1977 break;
H. Peter Anvincbf768d2008-02-16 16:41:25 -08001978
1979 case PPC_IFTOKEN:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001980 t = tline = expand_smacro(tline);
1981 while (tok_type_(t, TOK_WHITESPACE))
1982 t = t->next;
H. Peter Anvincbf768d2008-02-16 16:41:25 -08001983
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001984 j = false;
1985 if (t) {
1986 t = t->next; /* Skip the actual token */
1987 while (tok_type_(t, TOK_WHITESPACE))
1988 t = t->next;
1989 j = !t; /* Should be nothing left */
1990 }
1991 break;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001992
H. Peter Anvin134b9462008-02-16 17:01:40 -08001993 case PPC_IFEMPTY:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001994 t = tline = expand_smacro(tline);
1995 while (tok_type_(t, TOK_WHITESPACE))
1996 t = t->next;
H. Peter Anvin134b9462008-02-16 17:01:40 -08001997
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001998 j = !t; /* Should be empty */
1999 break;
H. Peter Anvin134b9462008-02-16 17:01:40 -08002000
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002001 case PPC_IF:
H. Peter Anvin8b262472019-02-26 14:00:54 -08002002 pps.tptr = tline = expand_smacro(tline);
2003 pps.ntokens = -1;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002004 tokval.t_type = TOKEN_INVALID;
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07002005 evalresult = evaluate(ppscan, &pps, &tokval, NULL, true, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002006 if (!evalresult)
2007 return -1;
2008 if (tokval.t_type)
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08002009 nasm_warn(WARN_OTHER, "trailing garbage after expression ignored");
H. Peter Anvine2c80182005-01-15 22:15:51 +00002010 if (!is_simple(evalresult)) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002011 nasm_nonfatal("non-constant value given to `%s'",
2012 pp_directives[ct]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002013 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002014 }
Chuck Crayne60ae75d2007-05-02 01:59:16 +00002015 j = reloc_value(evalresult) != 0;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002016 break;
H. Peter Anvin95e28822007-09-12 04:20:08 +00002017
H. Peter Anvine2c80182005-01-15 22:15:51 +00002018 default:
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002019 nasm_fatal("preprocessor directive `%s' not yet implemented",
2020 pp_directives[ct]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002021 goto fail;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002022 }
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002023
2024 free_tlist(origline);
2025 return j ^ PP_NEGATIVE(ct);
H. Peter Anvin70653092007-10-19 14:42:29 -07002026
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002027fail:
2028 free_tlist(origline);
2029 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002030}
2031
2032/*
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002033 * Common code for defining an smacro
2034 */
H. Peter Anvin8b262472019-02-26 14:00:54 -08002035static SMacro *define_smacro(Context *ctx, const char *mname,
2036 bool casesense, int nparam, Token *expansion)
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002037{
2038 SMacro *smac, **smhead;
H. Peter Anvin166c2472008-05-28 12:28:58 -07002039 struct hash_table *smtbl;
H. Peter Anvin70653092007-10-19 14:42:29 -07002040
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002041 if (smacro_defined(ctx, mname, nparam, &smac, casesense)) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002042 if (!smac) {
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08002043 nasm_warn(WARN_OTHER, "single-line macro `%s' defined both with and"
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002044 " without parameters", mname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002045 /*
2046 * Some instances of the old code considered this a failure,
2047 * some others didn't. What is the right thing to do here?
2048 */
2049 free_tlist(expansion);
H. Peter Anvin8b262472019-02-26 14:00:54 -08002050 return NULL; /* Failure */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002051 } else {
2052 /*
2053 * We're redefining, so we have to take over an
2054 * existing SMacro structure. This means freeing
H. Peter Anvin8b262472019-02-26 14:00:54 -08002055 * what was already in it, but not the structure itself.
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002056 */
H. Peter Anvin8b262472019-02-26 14:00:54 -08002057 free_smacro(smac, false);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002058 }
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002059 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002060 smtbl = ctx ? &ctx->localmac : &smacros;
2061 smhead = (SMacro **) hash_findi_add(smtbl, mname);
H. Peter Anvin8b262472019-02-26 14:00:54 -08002062 nasm_new(smac);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002063 smac->next = *smhead;
2064 *smhead = smac;
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002065 }
2066 smac->name = nasm_strdup(mname);
2067 smac->casesense = casesense;
2068 smac->nparam = nparam;
H. Peter Anvin8b262472019-02-26 14:00:54 -08002069 smac->e.expansion = expansion;
2070 return smac;
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002071}
2072
2073/*
2074 * Undefine an smacro
2075 */
2076static void undef_smacro(Context *ctx, const char *mname)
2077{
2078 SMacro **smhead, *s, **sp;
H. Peter Anvin166c2472008-05-28 12:28:58 -07002079 struct hash_table *smtbl;
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002080
H. Peter Anvin166c2472008-05-28 12:28:58 -07002081 smtbl = ctx ? &ctx->localmac : &smacros;
2082 smhead = (SMacro **)hash_findi(smtbl, mname, NULL);
H. Peter Anvin70653092007-10-19 14:42:29 -07002083
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002084 if (smhead) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002085 /*
2086 * We now have a macro name... go hunt for it.
2087 */
2088 sp = smhead;
2089 while ((s = *sp) != NULL) {
2090 if (!mstrcmp(s->name, mname, s->casesense)) {
2091 *sp = s->next;
H. Peter Anvin8b262472019-02-26 14:00:54 -08002092 free_smacro(s, true);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002093 } else {
2094 sp = &s->next;
2095 }
2096 }
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002097 }
2098}
2099
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002100/*
H. Peter Anvina26433d2008-07-16 14:40:01 -07002101 * Parse a mmacro specification.
2102 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002103static bool parse_mmacro_spec(Token *tline, MMacro *def, const char *directive)
H. Peter Anvina26433d2008-07-16 14:40:01 -07002104{
H. Peter Anvina26433d2008-07-16 14:40:01 -07002105 tline = tline->next;
2106 skip_white_(tline);
2107 tline = expand_id(tline);
2108 if (!tok_type_(tline, TOK_ID)) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002109 nasm_nonfatal("`%s' expects a macro name", directive);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002110 return false;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002111 }
Victor van den Elzenb916ede2008-07-23 15:14:22 +02002112
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002113 def->prev = NULL;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002114 def->name = nasm_strdup(tline->text);
2115 def->plus = false;
2116 def->nolist = false;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002117 def->in_progress = 0;
2118 def->rep_nest = NULL;
Victor van den Elzenb916ede2008-07-23 15:14:22 +02002119 def->nparam_min = 0;
2120 def->nparam_max = 0;
2121
H. Peter Anvina26433d2008-07-16 14:40:01 -07002122 tline = expand_smacro(tline->next);
2123 skip_white_(tline);
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002124 if (!tok_type_(tline, TOK_NUMBER))
2125 nasm_nonfatal("`%s' expects a parameter count", directive);
2126 else
Cyrill Gorcunov3079f792018-11-14 10:03:42 +03002127 def->nparam_min = def->nparam_max = read_param_count(tline->text);
H. Peter Anvina26433d2008-07-16 14:40:01 -07002128 if (tline && tok_is_(tline->next, "-")) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002129 tline = tline->next->next;
2130 if (tok_is_(tline, "*")) {
2131 def->nparam_max = INT_MAX;
2132 } else if (!tok_type_(tline, TOK_NUMBER)) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002133 nasm_nonfatal("`%s' expects a parameter count after `-'", directive);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002134 } else {
Cyrill Gorcunov3079f792018-11-14 10:03:42 +03002135 def->nparam_max = read_param_count(tline->text);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002136 if (def->nparam_min > def->nparam_max) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002137 nasm_nonfatal("minimum parameter count exceeds maximum");
Cyrill Gorcunovc9244ea2017-10-22 15:25:48 +03002138 def->nparam_max = def->nparam_min;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002139 }
2140 }
H. Peter Anvina26433d2008-07-16 14:40:01 -07002141 }
2142 if (tline && tok_is_(tline->next, "+")) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002143 tline = tline->next;
2144 def->plus = true;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002145 }
2146 if (tline && tok_type_(tline->next, TOK_ID) &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002147 !nasm_stricmp(tline->next->text, ".nolist")) {
2148 tline = tline->next;
2149 def->nolist = true;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002150 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002151
H. Peter Anvina26433d2008-07-16 14:40:01 -07002152 /*
2153 * Handle default parameters.
2154 */
2155 if (tline && tline->next) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002156 def->dlist = tline->next;
2157 tline->next = NULL;
2158 count_mmac_params(def->dlist, &def->ndefs, &def->defaults);
H. Peter Anvina26433d2008-07-16 14:40:01 -07002159 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002160 def->dlist = NULL;
2161 def->defaults = NULL;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002162 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002163 def->expansion = NULL;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002164
H. Peter Anvin89cee572009-07-15 09:16:54 -04002165 if (def->defaults && def->ndefs > def->nparam_max - def->nparam_min &&
H. Peter Anvin (Intel)723ab482018-12-13 21:53:31 -08002166 !def->plus) {
2167 /*
2168 *!macro-defaults [on] macros with more default than optional parameters
2169 *! warns when a macro has more default parameters than optional parameters.
2170 *! See \k{mlmacdef} for why might want to disable this warning.
2171 */
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08002172 nasm_warn(WARN_MACRO_DEFAULTS,
H. Peter Anvin (Intel)723ab482018-12-13 21:53:31 -08002173 "too many default macro parameters in macro `%s'", def->name);
2174 }
Victor van den Elzenb916ede2008-07-23 15:14:22 +02002175
H. Peter Anvina26433d2008-07-16 14:40:01 -07002176 return true;
2177}
2178
2179
2180/*
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002181 * Decode a size directive
2182 */
2183static int parse_size(const char *str) {
2184 static const char *size_names[] =
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002185 { "byte", "dword", "oword", "qword", "tword", "word", "yword" };
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002186 static const int sizes[] =
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002187 { 0, 1, 4, 16, 8, 10, 2, 32 };
Cyrill Gorcunovc713b5f2018-09-29 14:30:14 +03002188 return str ? sizes[bsii(str, size_names, ARRAY_SIZE(size_names))+1] : 0;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002189}
2190
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07002191/*
2192 * Process a preprocessor %pragma directive. Currently there are none.
2193 * Gets passed the token list starting with the "preproc" token from
2194 * "%pragma preproc".
2195 */
2196static void do_pragma_preproc(Token *tline)
2197{
2198 /* Skip to the real stuff */
2199 tline = tline->next;
2200 skip_white_(tline);
2201 if (!tline)
2202 return;
2203
2204 (void)tline; /* Nothing else to do at present */
2205}
2206
Ed Beroset3ab3f412002-06-11 03:31:49 +00002207/**
2208 * find and process preprocessor directive in passed line
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002209 * Find out if a line contains a preprocessor directive, and deal
2210 * with it if so.
H. Peter Anvin70653092007-10-19 14:42:29 -07002211 *
Ed Beroset3ab3f412002-06-11 03:31:49 +00002212 * If a directive _is_ found, it is the responsibility of this routine
2213 * (and not the caller) to free_tlist() the line.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002214 *
Ed Beroset3ab3f412002-06-11 03:31:49 +00002215 * @param tline a pointer to the current tokeninzed line linked list
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07002216 * @param output if this directive generated output
Ed Beroset3ab3f412002-06-11 03:31:49 +00002217 * @return DIRECTIVE_FOUND or NO_DIRECTIVE_FOUND
H. Peter Anvin70653092007-10-19 14:42:29 -07002218 *
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002219 */
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07002220static int do_directive(Token *tline, char **output)
H. Peter Anvineba20a72002-04-30 20:53:55 +00002221{
H. Peter Anvin4169a472007-09-12 01:29:43 +00002222 enum preproc_token i;
2223 int j;
H. Peter Anvin70055962007-10-11 00:05:31 -07002224 bool err;
2225 int nparam;
2226 bool nolist;
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07002227 bool casesense;
H. Peter Anvin8cfdb9d2007-09-14 18:36:01 -07002228 int k, m;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002229 int offset;
H. Peter Anvinccad6f92016-10-04 00:34:35 -07002230 char *p, *pp;
2231 const char *found_path;
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08002232 const char *mname;
H. Peter Anvin8b262472019-02-26 14:00:54 -08002233 struct ppscan pps;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002234 Include *inc;
2235 Context *ctx;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002236 Cond *cond;
2237 MMacro *mmac, **mmhead;
H. Peter Anvin8b262472019-02-26 14:00:54 -08002238 Token *t = NULL, *tt, *param_start, *macro_start, *last, *origline;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002239 Line *l;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002240 struct tokenval tokval;
2241 expr *evalresult;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002242 MMacro *tmp_defining; /* Used when manipulating rep_nest */
H. Peter Anvinf8ba53e2007-10-11 10:11:57 -07002243 int64_t count;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07002244 size_t len;
H. Peter Anvin (Intel)6bde2ed2018-12-13 19:39:41 -08002245 errflags severity;
H. Peter Anvin8b262472019-02-26 14:00:54 -08002246 const char *dname; /* Name of directive, for messages */
H. Peter Anvin76690a12002-04-30 20:52:49 +00002247
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07002248 *output = NULL; /* No output generated */
H. Peter Anvin76690a12002-04-30 20:52:49 +00002249 origline = tline;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002250
H. Peter Anvineba20a72002-04-30 20:53:55 +00002251 skip_white_(tline);
H. Peter Anvinf2936d72008-06-04 15:11:23 -07002252 if (!tline || !tok_type_(tline, TOK_PREPROC_ID) ||
Cyrill Gorcunov4b5b7372018-10-29 22:54:08 +03002253 (tline->text[0] && (tline->text[1] == '%' ||
2254 tline->text[1] == '$' ||
2255 tline->text[1] == '!')))
H. Peter Anvine2c80182005-01-15 22:15:51 +00002256 return NO_DIRECTIVE_FOUND;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002257
H. Peter Anvin4169a472007-09-12 01:29:43 +00002258 i = pp_token_hash(tline->text);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002259
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002260 /*
2261 * FIXME: We zap execution of PP_RMACRO, PP_IRMACRO, PP_EXITMACRO
2262 * since they are known to be buggy at moment, we need to fix them
2263 * in future release (2.09-2.10)
2264 */
Philipp Klokeb432f572013-03-31 12:01:23 +02002265 if (i == PP_RMACRO || i == PP_IRMACRO || i == PP_EXITMACRO) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002266 nasm_nonfatal("unknown preprocessor directive `%s'", tline->text);
2267 return NO_DIRECTIVE_FOUND;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002268 }
2269
2270 /*
2271 * If we're in a non-emitting branch of a condition construct,
2272 * or walking to the end of an already terminated %rep block,
2273 * we should ignore all directives except for condition
2274 * directives.
2275 */
2276 if (((istk->conds && !emitting(istk->conds->state)) ||
2277 (istk->mstk && !istk->mstk->in_progress)) && !is_condition(i)) {
2278 return NO_DIRECTIVE_FOUND;
2279 }
2280
2281 /*
2282 * If we're defining a macro or reading a %rep block, we should
2283 * ignore all directives except for %macro/%imacro (which nest),
2284 * %endm/%endmacro, and (only if we're in a %rep block) %endrep.
2285 * If we're in a %rep block, another %rep nests, so should be let through.
2286 */
2287 if (defining && i != PP_MACRO && i != PP_IMACRO &&
2288 i != PP_RMACRO && i != PP_IRMACRO &&
2289 i != PP_ENDMACRO && i != PP_ENDM &&
2290 (defining->name || (i != PP_ENDREP && i != PP_REP))) {
2291 return NO_DIRECTIVE_FOUND;
2292 }
2293
2294 if (defining) {
2295 if (i == PP_MACRO || i == PP_IMACRO ||
2296 i == PP_RMACRO || i == PP_IRMACRO) {
2297 nested_mac_count++;
2298 return NO_DIRECTIVE_FOUND;
2299 } else if (nested_mac_count > 0) {
2300 if (i == PP_ENDMACRO) {
2301 nested_mac_count--;
2302 return NO_DIRECTIVE_FOUND;
2303 }
2304 }
2305 if (!defining->name) {
2306 if (i == PP_REP) {
2307 nested_rep_count++;
2308 return NO_DIRECTIVE_FOUND;
2309 } else if (nested_rep_count > 0) {
2310 if (i == PP_ENDREP) {
2311 nested_rep_count--;
2312 return NO_DIRECTIVE_FOUND;
2313 }
2314 }
2315 }
2316 }
2317
H. Peter Anvin8b262472019-02-26 14:00:54 -08002318 dname = pp_directives[i]; /* Directive name, for error messages */
2319 casesense = true; /* Default to case sensitive */
H. Peter Anvin4169a472007-09-12 01:29:43 +00002320 switch (i) {
2321 case PP_INVALID:
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002322 nasm_nonfatal("unknown preprocessor directive `%s'", tline->text);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002323 return NO_DIRECTIVE_FOUND; /* didn't get it */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002324
H. Peter Anvin3f87a2a2016-10-04 14:07:19 -07002325 case PP_PRAGMA:
2326 /*
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07002327 * %pragma namespace options...
2328 *
2329 * The namespace "preproc" is reserved for the preprocessor;
2330 * all other namespaces generate a [pragma] assembly directive.
2331 *
2332 * Invalid %pragmas are ignored and may have different
2333 * meaning in future versions of NASM.
H. Peter Anvin3f87a2a2016-10-04 14:07:19 -07002334 */
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07002335 tline = tline->next;
2336 skip_white_(tline);
2337 tline = expand_smacro(tline);
2338 if (tok_type_(tline, TOK_ID)) {
2339 if (!nasm_stricmp(tline->text, "preproc")) {
2340 /* Preprocessor pragma */
2341 do_pragma_preproc(tline);
2342 } else {
2343 /* Build the assembler directive */
2344 t = new_Token(NULL, TOK_OTHER, "[", 1);
2345 t->next = new_Token(NULL, TOK_ID, "pragma", 6);
2346 t->next->next = new_Token(tline, TOK_WHITESPACE, NULL, 0);
2347 tline = t;
2348 for (t = tline; t->next; t = t->next)
2349 ;
2350 t->next = new_Token(NULL, TOK_OTHER, "]", 1);
2351 /* true here can be revisited in the future */
2352 *output = detoken(tline, true);
2353 }
2354 }
H. Peter Anvin3f87a2a2016-10-04 14:07:19 -07002355 free_tlist(origline);
2356 return DIRECTIVE_FOUND;
2357
H. Peter Anvine2c80182005-01-15 22:15:51 +00002358 case PP_STACKSIZE:
2359 /* Directive to tell NASM what the default stack size is. The
2360 * default is for a 16-bit stack, and this can be overriden with
2361 * %stacksize large.
H. Peter Anvine2c80182005-01-15 22:15:51 +00002362 */
2363 tline = tline->next;
2364 if (tline && tline->type == TOK_WHITESPACE)
2365 tline = tline->next;
2366 if (!tline || tline->type != TOK_ID) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07002367 nasm_nonfatal("`%s' missing size parameter", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002368 free_tlist(origline);
2369 return DIRECTIVE_FOUND;
2370 }
2371 if (nasm_stricmp(tline->text, "flat") == 0) {
2372 /* All subsequent ARG directives are for a 32-bit stack */
2373 StackSize = 4;
2374 StackPointer = "ebp";
2375 ArgOffset = 8;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002376 LocalOffset = 0;
Charles Crayne7eaf9192007-11-08 22:11:14 -08002377 } else if (nasm_stricmp(tline->text, "flat64") == 0) {
2378 /* All subsequent ARG directives are for a 64-bit stack */
2379 StackSize = 8;
2380 StackPointer = "rbp";
Per Jessen53252e02010-02-11 00:16:59 +03002381 ArgOffset = 16;
Charles Crayne7eaf9192007-11-08 22:11:14 -08002382 LocalOffset = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002383 } else if (nasm_stricmp(tline->text, "large") == 0) {
2384 /* All subsequent ARG directives are for a 16-bit stack,
2385 * far function call.
2386 */
2387 StackSize = 2;
2388 StackPointer = "bp";
2389 ArgOffset = 4;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002390 LocalOffset = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002391 } else if (nasm_stricmp(tline->text, "small") == 0) {
2392 /* All subsequent ARG directives are for a 16-bit stack,
2393 * far function call. We don't support near functions.
2394 */
2395 StackSize = 2;
2396 StackPointer = "bp";
2397 ArgOffset = 6;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002398 LocalOffset = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002399 } else {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07002400 nasm_nonfatal("`%s' invalid size type", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002401 free_tlist(origline);
2402 return DIRECTIVE_FOUND;
2403 }
2404 free_tlist(origline);
2405 return DIRECTIVE_FOUND;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002406
H. Peter Anvine2c80182005-01-15 22:15:51 +00002407 case PP_ARG:
2408 /* TASM like ARG directive to define arguments to functions, in
2409 * the following form:
2410 *
2411 * ARG arg1:WORD, arg2:DWORD, arg4:QWORD
2412 */
2413 offset = ArgOffset;
2414 do {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00002415 char *arg, directive[256];
H. Peter Anvine2c80182005-01-15 22:15:51 +00002416 int size = StackSize;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002417
H. Peter Anvine2c80182005-01-15 22:15:51 +00002418 /* Find the argument name */
2419 tline = tline->next;
2420 if (tline && tline->type == TOK_WHITESPACE)
2421 tline = tline->next;
2422 if (!tline || tline->type != TOK_ID) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07002423 nasm_nonfatal("`%s' missing argument parameter", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002424 free_tlist(origline);
2425 return DIRECTIVE_FOUND;
2426 }
2427 arg = tline->text;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002428
H. Peter Anvine2c80182005-01-15 22:15:51 +00002429 /* Find the argument size type */
2430 tline = tline->next;
2431 if (!tline || tline->type != TOK_OTHER
2432 || tline->text[0] != ':') {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07002433 nasm_nonfatal("syntax error processing `%s' directive", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002434 free_tlist(origline);
2435 return DIRECTIVE_FOUND;
2436 }
2437 tline = tline->next;
2438 if (!tline || tline->type != TOK_ID) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07002439 nasm_nonfatal("`%s' missing size type parameter", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002440 free_tlist(origline);
2441 return DIRECTIVE_FOUND;
2442 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002443
H. Peter Anvine2c80182005-01-15 22:15:51 +00002444 /* Allow macro expansion of type parameter */
Keith Kaniosb7a89542007-04-12 02:40:54 +00002445 tt = tokenize(tline->text);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002446 tt = expand_smacro(tt);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002447 size = parse_size(tt->text);
2448 if (!size) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07002449 nasm_nonfatal("invalid size type for `%s' missing directive", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002450 free_tlist(tt);
2451 free_tlist(origline);
2452 return DIRECTIVE_FOUND;
2453 }
2454 free_tlist(tt);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002455
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002456 /* Round up to even stack slots */
2457 size = ALIGN(size, StackSize);
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002458
H. Peter Anvine2c80182005-01-15 22:15:51 +00002459 /* Now define the macro for the argument */
2460 snprintf(directive, sizeof(directive), "%%define %s (%s+%d)",
2461 arg, StackPointer, offset);
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07002462 do_directive(tokenize(directive), output);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002463 offset += size;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002464
H. Peter Anvine2c80182005-01-15 22:15:51 +00002465 /* Move to the next argument in the list */
2466 tline = tline->next;
2467 if (tline && tline->type == TOK_WHITESPACE)
2468 tline = tline->next;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002469 } while (tline && tline->type == TOK_OTHER && tline->text[0] == ',');
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002470 ArgOffset = offset;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002471 free_tlist(origline);
2472 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002473
H. Peter Anvine2c80182005-01-15 22:15:51 +00002474 case PP_LOCAL:
2475 /* TASM like LOCAL directive to define local variables for a
2476 * function, in the following form:
2477 *
2478 * LOCAL local1:WORD, local2:DWORD, local4:QWORD = LocalSize
2479 *
2480 * The '= LocalSize' at the end is ignored by NASM, but is
2481 * required by TASM to define the local parameter size (and used
2482 * by the TASM macro package).
2483 */
2484 offset = LocalOffset;
2485 do {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00002486 char *local, directive[256];
H. Peter Anvine2c80182005-01-15 22:15:51 +00002487 int size = StackSize;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002488
H. Peter Anvine2c80182005-01-15 22:15:51 +00002489 /* Find the argument name */
2490 tline = tline->next;
2491 if (tline && tline->type == TOK_WHITESPACE)
2492 tline = tline->next;
2493 if (!tline || tline->type != TOK_ID) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07002494 nasm_nonfatal("`%s' missing argument parameter", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002495 free_tlist(origline);
2496 return DIRECTIVE_FOUND;
2497 }
2498 local = tline->text;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002499
H. Peter Anvine2c80182005-01-15 22:15:51 +00002500 /* Find the argument size type */
2501 tline = tline->next;
2502 if (!tline || tline->type != TOK_OTHER
2503 || tline->text[0] != ':') {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07002504 nasm_nonfatal("syntax error processing `%s' directive", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002505 free_tlist(origline);
2506 return DIRECTIVE_FOUND;
2507 }
2508 tline = tline->next;
2509 if (!tline || tline->type != TOK_ID) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07002510 nasm_nonfatal("`%s' missing size type parameter", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002511 free_tlist(origline);
2512 return DIRECTIVE_FOUND;
2513 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00002514
H. Peter Anvine2c80182005-01-15 22:15:51 +00002515 /* Allow macro expansion of type parameter */
Keith Kaniosb7a89542007-04-12 02:40:54 +00002516 tt = tokenize(tline->text);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002517 tt = expand_smacro(tt);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002518 size = parse_size(tt->text);
2519 if (!size) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07002520 nasm_nonfatal("invalid size type for `%s' missing directive", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002521 free_tlist(tt);
2522 free_tlist(origline);
2523 return DIRECTIVE_FOUND;
2524 }
2525 free_tlist(tt);
H. Peter Anvin734b1882002-04-30 21:01:08 +00002526
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002527 /* Round up to even stack slots */
2528 size = ALIGN(size, StackSize);
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002529
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002530 offset += size; /* Negative offset, increment before */
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002531
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002532 /* Now define the macro for the argument */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002533 snprintf(directive, sizeof(directive), "%%define %s (%s-%d)",
2534 local, StackPointer, offset);
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07002535 do_directive(tokenize(directive), output);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002536
H. Peter Anvine2c80182005-01-15 22:15:51 +00002537 /* Now define the assign to setup the enter_c macro correctly */
2538 snprintf(directive, sizeof(directive),
2539 "%%assign %%$localsize %%$localsize+%d", size);
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07002540 do_directive(tokenize(directive), output);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002541
H. Peter Anvine2c80182005-01-15 22:15:51 +00002542 /* Move to the next argument in the list */
2543 tline = tline->next;
2544 if (tline && tline->type == TOK_WHITESPACE)
2545 tline = tline->next;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002546 } while (tline && tline->type == TOK_OTHER && tline->text[0] == ',');
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002547 LocalOffset = offset;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002548 free_tlist(origline);
2549 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002550
H. Peter Anvine2c80182005-01-15 22:15:51 +00002551 case PP_CLEAR:
2552 if (tline->next)
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07002553 nasm_warn(WARN_OTHER, "trailing garbage after `%s' ignored", dname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002554 free_macros();
2555 init_macros();
H. Peter Anvine2c80182005-01-15 22:15:51 +00002556 free_tlist(origline);
2557 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002558
H. Peter Anvin418ca702008-05-30 10:42:30 -07002559 case PP_DEPEND:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002560 t = tline->next = expand_smacro(tline->next);
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002561 skip_white_(t);
2562 if (!t || (t->type != TOK_STRING &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002563 t->type != TOK_INTERNAL_STRING)) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07002564 nasm_nonfatal("`%s' expects a file name", dname);
H. Peter Anvin418ca702008-05-30 10:42:30 -07002565 free_tlist(origline);
2566 return DIRECTIVE_FOUND; /* but we did _something_ */
2567 }
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002568 if (t->next)
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07002569 nasm_warn(WARN_OTHER, "trailing garbage after `%s' ignored", dname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002570 p = t->text;
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002571 if (t->type != TOK_INTERNAL_STRING)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002572 nasm_unquote_cstr(p, i);
Cyrill Gorcunovb7bb5ac2018-11-11 21:33:52 +03002573 strlist_add(deplist, p);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002574 free_tlist(origline);
H. Peter Anvin418ca702008-05-30 10:42:30 -07002575 return DIRECTIVE_FOUND;
2576
2577 case PP_INCLUDE:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002578 t = tline->next = expand_smacro(tline->next);
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002579 skip_white_(t);
H. Peter Anvind2456592008-06-19 15:04:18 -07002580
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002581 if (!t || (t->type != TOK_STRING &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002582 t->type != TOK_INTERNAL_STRING)) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07002583 nasm_nonfatal("`%s' expects a file name", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002584 free_tlist(origline);
2585 return DIRECTIVE_FOUND; /* but we did _something_ */
2586 }
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002587 if (t->next)
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07002588 nasm_warn(WARN_OTHER, "trailing garbage after `%s' ignored", dname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002589 p = t->text;
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002590 if (t->type != TOK_INTERNAL_STRING)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002591 nasm_unquote_cstr(p, i);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002592 inc = nasm_malloc(sizeof(Include));
H. Peter Anvine2c80182005-01-15 22:15:51 +00002593 inc->next = istk;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002594 inc->conds = NULL;
Jim Kukunas65a8afc2016-06-13 16:00:42 -04002595 found_path = NULL;
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07002596 inc->fp = inc_fopen(p, deplist, &found_path,
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08002597 (pp_mode == PP_DEPS)
2598 ? INC_OPTIONAL : INC_NEEDED, NF_TEXT);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002599 if (!inc->fp) {
2600 /* -MG given but file not found */
2601 nasm_free(inc);
2602 } else {
Jim Kukunas65a8afc2016-06-13 16:00:42 -04002603 inc->fname = src_set_fname(found_path ? found_path : p);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002604 inc->lineno = src_set_linnum(0);
2605 inc->lineinc = 1;
2606 inc->expansion = NULL;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002607 inc->mstk = NULL;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002608 istk = inc;
H. Peter Anvin8ac25aa2016-02-18 01:16:18 -08002609 lfmt->uplevel(LIST_INCLUDE);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002610 }
2611 free_tlist(origline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002612 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002613
H. Peter Anvind2456592008-06-19 15:04:18 -07002614 case PP_USE:
H. Peter Anvinf4ae5ad2008-06-19 18:39:24 -07002615 {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002616 static macros_t *use_pkg;
2617 const char *pkg_macro = NULL;
H. Peter Anvinf4ae5ad2008-06-19 18:39:24 -07002618
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002619 tline = tline->next;
2620 skip_white_(tline);
2621 tline = expand_id(tline);
H. Peter Anvind2456592008-06-19 15:04:18 -07002622
H. Peter Anvin264b7b92008-10-24 16:38:17 -07002623 if (!tline || (tline->type != TOK_STRING &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002624 tline->type != TOK_INTERNAL_STRING &&
2625 tline->type != TOK_ID)) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07002626 nasm_nonfatal("`%s' expects a package name", dname);
H. Peter Anvind2456592008-06-19 15:04:18 -07002627 free_tlist(origline);
2628 return DIRECTIVE_FOUND; /* but we did _something_ */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002629 }
H. Peter Anvin264b7b92008-10-24 16:38:17 -07002630 if (tline->next)
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07002631 nasm_warn(WARN_OTHER, "trailing garbage after `%s' ignored", dname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002632 if (tline->type == TOK_STRING)
2633 nasm_unquote_cstr(tline->text, i);
2634 use_pkg = nasm_stdmac_find_package(tline->text);
2635 if (!use_pkg)
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07002636 nasm_nonfatal("unknown `%s' package: %s", dname, tline->text);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002637 else
2638 pkg_macro = (char *)use_pkg + 1; /* The first string will be <%define>__USE_*__ */
Victor van den Elzen35eb2ea2010-03-10 22:33:48 +01002639 if (use_pkg && ! smacro_defined(NULL, pkg_macro, 0, NULL, true)) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002640 /* Not already included, go ahead and include it */
2641 stdmacpos = use_pkg;
2642 }
2643 free_tlist(origline);
H. Peter Anvind2456592008-06-19 15:04:18 -07002644 return DIRECTIVE_FOUND;
H. Peter Anvinf4ae5ad2008-06-19 18:39:24 -07002645 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002646 case PP_PUSH:
H. Peter Anvine2c80182005-01-15 22:15:51 +00002647 case PP_REPL:
H. Peter Anvin42b56392008-10-24 16:24:21 -07002648 case PP_POP:
H. Peter Anvine2c80182005-01-15 22:15:51 +00002649 tline = tline->next;
2650 skip_white_(tline);
2651 tline = expand_id(tline);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002652 if (tline) {
2653 if (!tok_type_(tline, TOK_ID)) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002654 nasm_nonfatal("`%s' expects a context identifier",
2655 pp_directives[i]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002656 free_tlist(origline);
2657 return DIRECTIVE_FOUND; /* but we did _something_ */
2658 }
2659 if (tline->next)
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08002660 nasm_warn(WARN_OTHER, "trailing garbage after `%s' ignored",
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002661 pp_directives[i]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002662 p = nasm_strdup(tline->text);
2663 } else {
2664 p = NULL; /* Anonymous */
2665 }
H. Peter Anvin42b56392008-10-24 16:24:21 -07002666
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002667 if (i == PP_PUSH) {
H. Peter Anvin (Intel)ebb05a02018-12-11 12:30:25 -08002668 nasm_new(ctx);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002669 ctx->next = cstk;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002670 ctx->name = p;
2671 ctx->number = unique++;
2672 cstk = ctx;
2673 } else {
2674 /* %pop or %repl */
2675 if (!cstk) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002676 nasm_nonfatal("`%s': context stack is empty",
2677 pp_directives[i]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002678 } else if (i == PP_POP) {
2679 if (p && (!cstk->name || nasm_stricmp(p, cstk->name)))
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07002680 nasm_nonfatal("`%s' in wrong context: %s, "
H. Peter Anvin8b262472019-02-26 14:00:54 -08002681 "expected %s",
2682 dname, cstk->name ? cstk->name : "anonymous", p);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002683 else
2684 ctx_pop();
2685 } else {
2686 /* i == PP_REPL */
2687 nasm_free(cstk->name);
2688 cstk->name = p;
2689 p = NULL;
2690 }
2691 nasm_free(p);
2692 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002693 free_tlist(origline);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002694 return DIRECTIVE_FOUND;
H. Peter Anvin8e3f75e2008-09-24 00:21:58 -07002695 case PP_FATAL:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002696 severity = ERR_FATAL;
2697 goto issue_error;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002698 case PP_ERROR:
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08002699 severity = ERR_NONFATAL|ERR_PASS2;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002700 goto issue_error;
H. Peter Anvin7df04172008-06-10 18:27:38 -07002701 case PP_WARNING:
H. Peter Anvin (Intel)723ab482018-12-13 21:53:31 -08002702 /*!
2703 *!user [on] %warning directives
2704 *! controls output of \c{%warning} directives (see \k{pperror}).
2705 */
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08002706 severity = ERR_WARNING|WARN_USER|ERR_PASS2;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002707 goto issue_error;
H. Peter Anvin8e3f75e2008-09-24 00:21:58 -07002708
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002709issue_error:
H. Peter Anvin7df04172008-06-10 18:27:38 -07002710 {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002711 /* Only error out if this is the final pass */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002712 tline->next = expand_smacro(tline->next);
2713 tline = tline->next;
2714 skip_white_(tline);
2715 t = tline ? tline->next : NULL;
2716 skip_white_(t);
2717 if (tok_type_(tline, TOK_STRING) && !t) {
2718 /* The line contains only a quoted string */
2719 p = tline->text;
2720 nasm_unquote(p, NULL); /* Ignore NUL character truncation */
H. Peter Anvin130736c2016-02-17 20:27:41 -08002721 nasm_error(severity, "%s", p);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002722 } else {
2723 /* Not a quoted string, or more than a quoted string */
2724 p = detoken(tline, false);
H. Peter Anvin130736c2016-02-17 20:27:41 -08002725 nasm_error(severity, "%s", p);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002726 nasm_free(p);
2727 }
2728 free_tlist(origline);
2729 return DIRECTIVE_FOUND;
H. Peter Anvin7df04172008-06-10 18:27:38 -07002730 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00002731
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002732 CASE_PP_IF:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002733 if (istk->conds && !emitting(istk->conds->state))
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002734 j = COND_NEVER;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002735 else {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002736 j = if_condition(tline->next, i);
2737 tline->next = NULL; /* it got freed */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002738 j = j < 0 ? COND_NEVER : j ? COND_IF_TRUE : COND_IF_FALSE;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002739 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002740 cond = nasm_malloc(sizeof(Cond));
2741 cond->next = istk->conds;
2742 cond->state = j;
2743 istk->conds = cond;
2744 if(istk->mstk)
2745 istk->mstk->condcnt ++;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002746 free_tlist(origline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002747 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002748
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002749 CASE_PP_ELIF:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002750 if (!istk->conds)
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07002751 nasm_fatal("`%s': no matching `%%if'", dname);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002752 switch(istk->conds->state) {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002753 case COND_IF_TRUE:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002754 istk->conds->state = COND_DONE;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002755 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002756
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002757 case COND_DONE:
2758 case COND_NEVER:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002759 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002760
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002761 case COND_ELSE_TRUE:
2762 case COND_ELSE_FALSE:
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08002763 nasm_warn(WARN_OTHER|ERR_PP_PRECOND,
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002764 "`%%elif' after `%%else' ignored");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002765 istk->conds->state = COND_NEVER;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002766 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002767
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002768 case COND_IF_FALSE:
2769 /*
2770 * IMPORTANT: In the case of %if, we will already have
2771 * called expand_mmac_params(); however, if we're
2772 * processing an %elif we must have been in a
2773 * non-emitting mode, which would have inhibited
2774 * the normal invocation of expand_mmac_params().
2775 * Therefore, we have to do it explicitly here.
2776 */
2777 j = if_condition(expand_mmac_params(tline->next), i);
2778 tline->next = NULL; /* it got freed */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002779 istk->conds->state =
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002780 j < 0 ? COND_NEVER : j ? COND_IF_TRUE : COND_IF_FALSE;
2781 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002782 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002783 free_tlist(origline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002784 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002785
H. Peter Anvine2c80182005-01-15 22:15:51 +00002786 case PP_ELSE:
2787 if (tline->next)
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08002788 nasm_warn(WARN_OTHER|ERR_PP_PRECOND,
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002789 "trailing garbage after `%%else' ignored");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002790 if (!istk->conds)
H. Peter Anvinc5136902018-06-15 18:20:17 -07002791 nasm_fatal("`%%else: no matching `%%if'");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002792 switch(istk->conds->state) {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002793 case COND_IF_TRUE:
2794 case COND_DONE:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002795 istk->conds->state = COND_ELSE_FALSE;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002796 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002797
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002798 case COND_NEVER:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002799 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002800
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002801 case COND_IF_FALSE:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002802 istk->conds->state = COND_ELSE_TRUE;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002803 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002804
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002805 case COND_ELSE_TRUE:
2806 case COND_ELSE_FALSE:
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08002807 nasm_warn(WARN_OTHER|ERR_PP_PRECOND,
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002808 "`%%else' after `%%else' ignored.");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002809 istk->conds->state = COND_NEVER;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002810 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002811 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002812 free_tlist(origline);
2813 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002814
H. Peter Anvine2c80182005-01-15 22:15:51 +00002815 case PP_ENDIF:
2816 if (tline->next)
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08002817 nasm_warn(WARN_OTHER|ERR_PP_PRECOND,
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002818 "trailing garbage after `%%endif' ignored");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002819 if (!istk->conds)
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002820 nasm_fatal("`%%endif': no matching `%%if'");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002821 cond = istk->conds;
2822 istk->conds = cond->next;
2823 nasm_free(cond);
2824 if(istk->mstk)
2825 istk->mstk->condcnt --;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002826 free_tlist(origline);
2827 return DIRECTIVE_FOUND;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002828
H. Peter Anvindb8f96e2009-07-15 09:07:29 -04002829 case PP_IRMACRO:
H. Peter Anvine2c80182005-01-15 22:15:51 +00002830 case PP_IMACRO:
H. Peter Anvin8b262472019-02-26 14:00:54 -08002831 casesense = false;
2832 /* fall through */
2833 case PP_RMACRO:
2834 case PP_MACRO:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002835 if (defining) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07002836 nasm_fatal("`%s': already defining a macro", dname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002837 return DIRECTIVE_FOUND;
2838 }
H. Peter Anvin4def1a82016-05-09 13:59:44 -07002839 defining = nasm_zalloc(sizeof(MMacro));
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07002840 defining->max_depth = ((i == PP_RMACRO) || (i == PP_IRMACRO))
2841 ? nasm_limit[LIMIT_MACROS] : 0;
H. Peter Anvin8b262472019-02-26 14:00:54 -08002842 defining->casesense = casesense;
2843 if (!parse_mmacro_spec(tline, defining, dname)) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002844 nasm_free(defining);
2845 defining = NULL;
2846 return DIRECTIVE_FOUND;
2847 }
H. Peter Anvina26433d2008-07-16 14:40:01 -07002848
H. Peter Anvin4def1a82016-05-09 13:59:44 -07002849 src_get(&defining->xline, &defining->fname);
2850
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002851 mmac = (MMacro *) hash_findix(&mmacros, defining->name);
2852 while (mmac) {
2853 if (!strcmp(mmac->name, defining->name) &&
2854 (mmac->nparam_min <= defining->nparam_max
2855 || defining->plus)
2856 && (defining->nparam_min <= mmac->nparam_max
2857 || mmac->plus)) {
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08002858 nasm_warn(WARN_OTHER, "redefining multi-line macro `%s'",
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002859 defining->name);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002860 return DIRECTIVE_FOUND;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002861 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002862 mmac = mmac->next;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002863 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002864 free_tlist(origline);
2865 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002866
H. Peter Anvine2c80182005-01-15 22:15:51 +00002867 case PP_ENDM:
2868 case PP_ENDMACRO:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002869 if (! (defining && defining->name)) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002870 nasm_nonfatal("`%s': not defining a macro", tline->text);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002871 return DIRECTIVE_FOUND;
2872 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002873 mmhead = (MMacro **) hash_findi_add(&mmacros, defining->name);
2874 defining->next = *mmhead;
2875 *mmhead = defining;
2876 defining = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002877 free_tlist(origline);
2878 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002879
H. Peter Anvin89cee572009-07-15 09:16:54 -04002880 case PP_EXITMACRO:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002881 /*
2882 * We must search along istk->expansion until we hit a
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002883 * macro-end marker for a macro with a name. Then we
2884 * bypass all lines between exitmacro and endmacro.
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002885 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002886 list_for_each(l, istk->expansion)
2887 if (l->finishes && l->finishes->name)
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002888 break;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002889
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002890 if (l) {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002891 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002892 * Remove all conditional entries relative to this
2893 * macro invocation. (safe to do in this context)
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002894 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002895 for ( ; l->finishes->condcnt > 0; l->finishes->condcnt --) {
2896 cond = istk->conds;
2897 istk->conds = cond->next;
2898 nasm_free(cond);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002899 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002900 istk->expansion = l;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002901 } else {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002902 nasm_nonfatal("`%%exitmacro' not within `%%macro' block");
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002903 }
Keith Kanios852f1ee2009-07-12 00:19:55 -05002904 free_tlist(origline);
2905 return DIRECTIVE_FOUND;
2906
H. Peter Anvina26433d2008-07-16 14:40:01 -07002907 case PP_UNIMACRO:
H. Peter Anvin8b262472019-02-26 14:00:54 -08002908 casesense = false;
2909 /* fall through */
2910 case PP_UNMACRO:
H. Peter Anvina26433d2008-07-16 14:40:01 -07002911 {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002912 MMacro **mmac_p;
2913 MMacro spec;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002914
H. Peter Anvin8b262472019-02-26 14:00:54 -08002915 spec.casesense = casesense;
2916 if (!parse_mmacro_spec(tline, &spec, dname)) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002917 return DIRECTIVE_FOUND;
2918 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002919 mmac_p = (MMacro **) hash_findi(&mmacros, spec.name, NULL);
2920 while (mmac_p && *mmac_p) {
2921 mmac = *mmac_p;
2922 if (mmac->casesense == spec.casesense &&
2923 !mstrcmp(mmac->name, spec.name, spec.casesense) &&
2924 mmac->nparam_min == spec.nparam_min &&
2925 mmac->nparam_max == spec.nparam_max &&
2926 mmac->plus == spec.plus) {
2927 *mmac_p = mmac->next;
2928 free_mmacro(mmac);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002929 } else {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002930 mmac_p = &mmac->next;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002931 }
2932 }
2933 free_tlist(origline);
2934 free_tlist(spec.dlist);
2935 return DIRECTIVE_FOUND;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002936 }
2937
H. Peter Anvine2c80182005-01-15 22:15:51 +00002938 case PP_ROTATE:
2939 if (tline->next && tline->next->type == TOK_WHITESPACE)
2940 tline = tline->next;
H. Peter Anvin89cee572009-07-15 09:16:54 -04002941 if (!tline->next) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002942 free_tlist(origline);
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002943 nasm_nonfatal("`%%rotate' missing rotate count");
H. Peter Anvine2c80182005-01-15 22:15:51 +00002944 return DIRECTIVE_FOUND;
2945 }
2946 t = expand_smacro(tline->next);
2947 tline->next = NULL;
2948 free_tlist(origline);
H. Peter Anvin8b262472019-02-26 14:00:54 -08002949 pps.tptr = tline = t;
2950 pps.ntokens = -1;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002951 tokval.t_type = TOKEN_INVALID;
2952 evalresult =
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07002953 evaluate(ppscan, &pps, &tokval, NULL, true, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002954 free_tlist(tline);
2955 if (!evalresult)
2956 return DIRECTIVE_FOUND;
2957 if (tokval.t_type)
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08002958 nasm_warn(WARN_OTHER, "trailing garbage after expression ignored");
H. Peter Anvine2c80182005-01-15 22:15:51 +00002959 if (!is_simple(evalresult)) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002960 nasm_nonfatal("non-constant value given to `%%rotate'");
H. Peter Anvine2c80182005-01-15 22:15:51 +00002961 return DIRECTIVE_FOUND;
2962 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002963 mmac = istk->mstk;
2964 while (mmac && !mmac->name) /* avoid mistaking %reps for macros */
2965 mmac = mmac->next_active;
2966 if (!mmac) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002967 nasm_nonfatal("`%%rotate' invoked outside a macro call");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002968 } else if (mmac->nparam == 0) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002969 nasm_nonfatal("`%%rotate' invoked within macro without parameters");
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002970 } else {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002971 int rotate = mmac->rotate + reloc_value(evalresult);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002972
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002973 rotate %= (int)mmac->nparam;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002974 if (rotate < 0)
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002975 rotate += mmac->nparam;
2976
2977 mmac->rotate = rotate;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002978 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002979 return DIRECTIVE_FOUND;
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00002980
H. Peter Anvine2c80182005-01-15 22:15:51 +00002981 case PP_REP:
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002982 nolist = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002983 do {
2984 tline = tline->next;
2985 } while (tok_type_(tline, TOK_WHITESPACE));
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00002986
H. Peter Anvine2c80182005-01-15 22:15:51 +00002987 if (tok_type_(tline, TOK_ID) &&
2988 nasm_stricmp(tline->text, ".nolist") == 0) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002989 nolist = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002990 do {
2991 tline = tline->next;
2992 } while (tok_type_(tline, TOK_WHITESPACE));
2993 }
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00002994
H. Peter Anvine2c80182005-01-15 22:15:51 +00002995 if (tline) {
H. Peter Anvin8b262472019-02-26 14:00:54 -08002996 pps.tptr = expand_smacro(tline);
2997 pps.ntokens = -1;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002998 tokval.t_type = TOKEN_INVALID;
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07002999 /* XXX: really critical?! */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003000 evalresult =
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003001 evaluate(ppscan, &pps, &tokval, NULL, true, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003002 if (!evalresult) {
3003 free_tlist(origline);
3004 return DIRECTIVE_FOUND;
3005 }
3006 if (tokval.t_type)
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08003007 nasm_warn(WARN_OTHER, "trailing garbage after expression ignored");
H. Peter Anvine2c80182005-01-15 22:15:51 +00003008 if (!is_simple(evalresult)) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003009 nasm_nonfatal("non-constant value given to `%%rep'");
H. Peter Anvine2c80182005-01-15 22:15:51 +00003010 return DIRECTIVE_FOUND;
3011 }
Cyrill Gorcunove091d6e2010-08-09 13:58:22 +04003012 count = reloc_value(evalresult);
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07003013 if (count > nasm_limit[LIMIT_REP]) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003014 nasm_nonfatal("`%%rep' count %"PRId64" exceeds limit (currently %"PRId64")",
3015 count, nasm_limit[LIMIT_REP]);
Cyrill Gorcunove091d6e2010-08-09 13:58:22 +04003016 count = 0;
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07003017 } else if (count < 0) {
H. Peter Anvin (Intel)723ab482018-12-13 21:53:31 -08003018 /*!
3019 *!negative-rep [on] regative %rep count
3020 *! warns about negative counts given to the \c{%rep}
3021 *! preprocessor directive.
3022 */
H. Peter Anvin (Intel)80c4f232018-12-14 13:33:24 -08003023 nasm_warn(ERR_PASS2|WARN_NEGATIVE_REP,
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07003024 "negative `%%rep' count: %"PRId64, count);
3025 count = 0;
3026 } else {
Cyrill Gorcunove091d6e2010-08-09 13:58:22 +04003027 count++;
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07003028 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00003029 } else {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003030 nasm_nonfatal("`%%rep' expects a repeat count");
H. Peter Anvinf8ba53e2007-10-11 10:11:57 -07003031 count = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003032 }
3033 free_tlist(origline);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003034
3035 tmp_defining = defining;
3036 defining = nasm_malloc(sizeof(MMacro));
3037 defining->prev = NULL;
3038 defining->name = NULL; /* flags this macro as a %rep block */
3039 defining->casesense = false;
3040 defining->plus = false;
3041 defining->nolist = nolist;
3042 defining->in_progress = count;
3043 defining->max_depth = 0;
3044 defining->nparam_min = defining->nparam_max = 0;
3045 defining->defaults = NULL;
3046 defining->dlist = NULL;
3047 defining->expansion = NULL;
3048 defining->next_active = istk->mstk;
3049 defining->rep_nest = tmp_defining;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003050 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003051
H. Peter Anvine2c80182005-01-15 22:15:51 +00003052 case PP_ENDREP:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003053 if (!defining || defining->name) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003054 nasm_nonfatal("`%%endrep': no matching `%%rep'");
H. Peter Anvine2c80182005-01-15 22:15:51 +00003055 return DIRECTIVE_FOUND;
3056 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003057
H. Peter Anvine2c80182005-01-15 22:15:51 +00003058 /*
3059 * Now we have a "macro" defined - although it has no name
3060 * and we won't be entering it in the hash tables - we must
3061 * push a macro-end marker for it on to istk->expansion.
3062 * After that, it will take care of propagating itself (a
3063 * macro-end marker line for a macro which is really a %rep
3064 * block will cause the macro to be re-expanded, complete
3065 * with another macro-end marker to ensure the process
3066 * continues) until the whole expansion is forcibly removed
3067 * from istk->expansion by a %exitrep.
3068 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003069 l = nasm_malloc(sizeof(Line));
3070 l->next = istk->expansion;
3071 l->finishes = defining;
3072 l->first = NULL;
3073 istk->expansion = l;
3074
3075 istk->mstk = defining;
3076
H. Peter Anvin8ac25aa2016-02-18 01:16:18 -08003077 lfmt->uplevel(defining->nolist ? LIST_MACRO_NOLIST : LIST_MACRO);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003078 tmp_defining = defining;
3079 defining = defining->rep_nest;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003080 free_tlist(origline);
3081 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003082
H. Peter Anvine2c80182005-01-15 22:15:51 +00003083 case PP_EXITREP:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003084 /*
3085 * We must search along istk->expansion until we hit a
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003086 * macro-end marker for a macro with no name. Then we set
3087 * its `in_progress' flag to 0.
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003088 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003089 list_for_each(l, istk->expansion)
3090 if (l->finishes && !l->finishes->name)
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003091 break;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003092
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003093 if (l)
3094 l->finishes->in_progress = 1;
3095 else
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003096 nasm_nonfatal("`%%exitrep' not within `%%rep' block");
H. Peter Anvine2c80182005-01-15 22:15:51 +00003097 free_tlist(origline);
3098 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003099
H. Peter Anvine2c80182005-01-15 22:15:51 +00003100 case PP_IDEFINE:
H. Peter Anvin8b262472019-02-26 14:00:54 -08003101 case PP_IXDEFINE:
3102 casesense = false;
3103 /* fall through */
3104 case PP_DEFINE:
3105 case PP_XDEFINE:
3106 {
3107 SMacro *s;
3108 bool have_eval_params = false;
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07003109
H. Peter Anvine2c80182005-01-15 22:15:51 +00003110 tline = tline->next;
3111 skip_white_(tline);
3112 tline = expand_id(tline);
3113 if (!tline || (tline->type != TOK_ID &&
3114 (tline->type != TOK_PREPROC_ID ||
3115 tline->text[1] != '$'))) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003116 nasm_nonfatal("`%s' expects a macro identifier", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003117 free_tlist(origline);
3118 return DIRECTIVE_FOUND;
3119 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003120
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04003121 ctx = get_ctx(tline->text, &mname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003122 last = tline;
3123 param_start = tline = tline->next;
3124 nparam = 0;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003125
H. Peter Anvine2c80182005-01-15 22:15:51 +00003126 if (tok_is_(tline, "(")) {
3127 /*
3128 * This macro has parameters.
3129 */
H. Peter Anvin734b1882002-04-30 21:01:08 +00003130
H. Peter Anvine2c80182005-01-15 22:15:51 +00003131 tline = tline->next;
3132 while (1) {
3133 skip_white_(tline);
3134 if (!tline) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003135 nasm_nonfatal("parameter identifier expected");
H. Peter Anvine2c80182005-01-15 22:15:51 +00003136 free_tlist(origline);
3137 return DIRECTIVE_FOUND;
3138 }
H. Peter Anvin8b262472019-02-26 14:00:54 -08003139 if (tok_is_(tline, "=")) {
3140 have_eval_params = true;
3141 tline = tline->next;
3142 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00003143 if (tline->type != TOK_ID) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003144 nasm_nonfatal("`%s': parameter identifier expected",
3145 tline->text);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003146 free_tlist(origline);
3147 return DIRECTIVE_FOUND;
3148 }
H. Peter Anvin8b262472019-02-26 14:00:54 -08003149 tline->type = tok_smac_param(nparam++);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003150 tline = tline->next;
3151 skip_white_(tline);
3152 if (tok_is_(tline, ",")) {
3153 tline = tline->next;
H. Peter Anvinca348b62008-07-23 10:49:26 -04003154 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003155 if (!tok_is_(tline, ")")) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003156 nasm_nonfatal("`)' expected to terminate macro template");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003157 free_tlist(origline);
3158 return DIRECTIVE_FOUND;
3159 }
3160 break;
3161 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00003162 }
3163 last = tline;
3164 tline = tline->next;
3165 }
3166 if (tok_type_(tline, TOK_WHITESPACE))
3167 last = tline, tline = tline->next;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003168 last->next = NULL;
H. Peter Anvin8b262472019-02-26 14:00:54 -08003169
3170 /* Expand the macro definition now for %xdefine and %ixdefine */
3171 if ((i == PP_XDEFINE) || (i == PP_IXDEFINE))
3172 tline = expand_smacro(tline);
3173
3174 macro_start = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003175 t = tline;
3176 while (t) {
3177 if (t->type == TOK_ID) {
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04003178 list_for_each(tt, param_start)
H. Peter Anvin8b262472019-02-26 14:00:54 -08003179 if (is_smac_param(tt->type) && !strcmp(tt->text, t->text))
H. Peter Anvine2c80182005-01-15 22:15:51 +00003180 t->type = tt->type;
3181 }
3182 tt = t->next;
3183 t->next = macro_start;
3184 macro_start = t;
3185 t = tt;
3186 }
3187 /*
3188 * Good. We now have a macro name, a parameter count, and a
3189 * token list (in reverse order) for an expansion. We ought
3190 * to be OK just to create an SMacro, store it, and let
3191 * free_tlist have the rest of the line (which we have
3192 * carefully re-terminated after chopping off the expansion
3193 * from the end).
3194 */
H. Peter Anvin8b262472019-02-26 14:00:54 -08003195 s = define_smacro(ctx, mname, casesense, nparam, macro_start);
3196
3197 if (have_eval_params) {
3198 /* Create evaluated parameters table */
3199 bool is_eval = false;
3200
3201 nasm_newn(s->eval_param, nparam);
3202 list_for_each(tt, param_start) {
3203 if (is_smac_param(tt->type))
3204 s->eval_param[smac_nparam(tt->type)] = is_eval;
3205 is_eval = tok_is_(tt, "=");
3206 }
3207 }
3208
3209
H. Peter Anvine2c80182005-01-15 22:15:51 +00003210 free_tlist(origline);
3211 return DIRECTIVE_FOUND;
H. Peter Anvin8b262472019-02-26 14:00:54 -08003212 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00003213
H. Peter Anvine2c80182005-01-15 22:15:51 +00003214 case PP_UNDEF:
3215 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("`%%undef' expects a macro identifier");
H. Peter Anvine2c80182005-01-15 22:15:51 +00003222 free_tlist(origline);
3223 return DIRECTIVE_FOUND;
3224 }
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003225 if (tline->next)
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08003226 nasm_warn(WARN_OTHER, "trailing garbage after macro name ignored");
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003227
H. Peter Anvine2c80182005-01-15 22:15:51 +00003228 /* Find the context that symbol belongs to */
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04003229 ctx = get_ctx(tline->text, &mname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003230 undef_smacro(ctx, mname);
3231 free_tlist(origline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003232 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003233
H. Peter Anvin9e200162008-06-04 17:23:14 -07003234 case PP_IDEFSTR:
H. Peter Anvin8b262472019-02-26 14:00:54 -08003235 casesense = false;
3236 /* fall through */
3237 case PP_DEFSTR:
H. Peter Anvin9e200162008-06-04 17:23:14 -07003238 tline = tline->next;
3239 skip_white_(tline);
3240 tline = expand_id(tline);
3241 if (!tline || (tline->type != TOK_ID &&
3242 (tline->type != TOK_PREPROC_ID ||
3243 tline->text[1] != '$'))) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003244 nasm_nonfatal("`%s' expects a macro identifier", dname);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003245 free_tlist(origline);
3246 return DIRECTIVE_FOUND;
3247 }
3248
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04003249 ctx = get_ctx(tline->text, &mname);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003250 last = tline;
3251 tline = expand_smacro(tline->next);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003252 last->next = NULL;
H. Peter Anvin9e200162008-06-04 17:23:14 -07003253
3254 while (tok_type_(tline, TOK_WHITESPACE))
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003255 tline = delete_Token(tline);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003256
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003257 p = detoken(tline, false);
H. Peter Anvin8b262472019-02-26 14:00:54 -08003258 macro_start = make_tok_qstr(p);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003259 nasm_free(p);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003260
3261 /*
3262 * We now have a macro name, an implicit parameter count of
3263 * zero, and a string token to use as an expansion. Create
3264 * and store an SMacro.
3265 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003266 define_smacro(ctx, mname, casesense, 0, macro_start);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003267 free_tlist(origline);
3268 return DIRECTIVE_FOUND;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003269
H. Peter Anvin2f55bda2009-07-14 15:04:04 -04003270 case PP_IDEFTOK:
H. Peter Anvin8b262472019-02-26 14:00:54 -08003271 casesense = false;
3272 /* fall through */
3273 case PP_DEFTOK:
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003274 tline = tline->next;
3275 skip_white_(tline);
3276 tline = expand_id(tline);
3277 if (!tline || (tline->type != TOK_ID &&
3278 (tline->type != TOK_PREPROC_ID ||
3279 tline->text[1] != '$'))) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003280 nasm_nonfatal("`%s' expects a macro identifier as first parameter", dname);
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003281 free_tlist(origline);
3282 return DIRECTIVE_FOUND;
3283 }
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04003284 ctx = get_ctx(tline->text, &mname);
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003285 last = tline;
3286 tline = expand_smacro(tline->next);
3287 last->next = NULL;
3288
3289 t = tline;
3290 while (tok_type_(t, TOK_WHITESPACE))
3291 t = t->next;
3292 /* t should now point to the string */
Cyrill Gorcunov6908e582010-09-06 19:36:15 +04003293 if (!tok_type_(t, TOK_STRING)) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003294 nasm_nonfatal("`%s` requires string as second parameter", dname);
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003295 free_tlist(tline);
3296 free_tlist(origline);
3297 return DIRECTIVE_FOUND;
3298 }
3299
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04003300 /*
3301 * Convert the string to a token stream. Note that smacros
3302 * are stored with the token stream reversed, so we have to
3303 * reverse the output of tokenize().
3304 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003305 nasm_unquote_cstr(t->text, i);
H. Peter Anvinb40992c2010-09-15 08:57:21 -07003306 macro_start = reverse_tokens(tokenize(t->text));
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003307
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003308 /*
3309 * We now have a macro name, an implicit parameter count of
3310 * zero, and a numeric 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);
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003314 free_tlist(tline);
3315 free_tlist(origline);
3316 return DIRECTIVE_FOUND;
H. Peter Anvin9e200162008-06-04 17:23:14 -07003317
H. Peter Anvin8b262472019-02-26 14:00:54 -08003318 case PP_IPATHSEARCH:
3319 casesense = false;
3320 /* fall through */
H. Peter Anvin418ca702008-05-30 10:42:30 -07003321 case PP_PATHSEARCH:
3322 {
H. Peter Anvinccad6f92016-10-04 00:34:35 -07003323 const char *found_path;
H. Peter Anvin418ca702008-05-30 10:42:30 -07003324
H. Peter Anvin418ca702008-05-30 10:42:30 -07003325 tline = tline->next;
3326 skip_white_(tline);
3327 tline = expand_id(tline);
3328 if (!tline || (tline->type != TOK_ID &&
3329 (tline->type != TOK_PREPROC_ID ||
3330 tline->text[1] != '$'))) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003331 nasm_nonfatal("`%s' expects a macro identifier as first parameter", dname);
H. Peter Anvin418ca702008-05-30 10:42:30 -07003332 free_tlist(origline);
3333 return DIRECTIVE_FOUND;
3334 }
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04003335 ctx = get_ctx(tline->text, &mname);
H. Peter Anvin418ca702008-05-30 10:42:30 -07003336 last = tline;
3337 tline = expand_smacro(tline->next);
3338 last->next = NULL;
3339
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003340 t = tline;
H. Peter Anvin418ca702008-05-30 10:42:30 -07003341 while (tok_type_(t, TOK_WHITESPACE))
3342 t = t->next;
3343
3344 if (!t || (t->type != TOK_STRING &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003345 t->type != TOK_INTERNAL_STRING)) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003346 nasm_nonfatal("`%s' expects a file name", dname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003347 free_tlist(tline);
H. Peter Anvin418ca702008-05-30 10:42:30 -07003348 free_tlist(origline);
3349 return DIRECTIVE_FOUND; /* but we did _something_ */
3350 }
3351 if (t->next)
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003352 nasm_warn(WARN_OTHER, "trailing garbage after `%s' ignored", dname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003353 p = t->text;
H. Peter Anvin427cc912008-06-01 21:43:03 -07003354 if (t->type != TOK_INTERNAL_STRING)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003355 nasm_unquote(p, NULL);
H. Peter Anvin418ca702008-05-30 10:42:30 -07003356
H. Peter Anvin9924d1e2016-10-04 00:59:39 -07003357 inc_fopen(p, NULL, &found_path, INC_PROBE, NF_BINARY);
H. Peter Anvinccad6f92016-10-04 00:34:35 -07003358 if (!found_path)
3359 found_path = p;
H. Peter Anvin8b262472019-02-26 14:00:54 -08003360 macro_start = make_tok_qstr(found_path);
H. Peter Anvin418ca702008-05-30 10:42:30 -07003361
3362 /*
3363 * We now have a macro name, an implicit parameter count of
3364 * zero, and a string token to use as an expansion. Create
3365 * and store an SMacro.
3366 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003367 define_smacro(ctx, mname, casesense, 0, macro_start);
3368 free_tlist(tline);
H. Peter Anvin418ca702008-05-30 10:42:30 -07003369 free_tlist(origline);
3370 return DIRECTIVE_FOUND;
3371 }
3372
H. Peter Anvin8b262472019-02-26 14:00:54 -08003373 case PP_ISTRLEN:
3374 casesense = false;
3375 /* fall through */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003376 case PP_STRLEN:
3377 tline = tline->next;
3378 skip_white_(tline);
3379 tline = expand_id(tline);
3380 if (!tline || (tline->type != TOK_ID &&
3381 (tline->type != TOK_PREPROC_ID ||
3382 tline->text[1] != '$'))) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003383 nasm_nonfatal("`%s' expects a macro identifier as first parameter", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003384 free_tlist(origline);
3385 return DIRECTIVE_FOUND;
3386 }
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04003387 ctx = get_ctx(tline->text, &mname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003388 last = tline;
3389 tline = expand_smacro(tline->next);
3390 last->next = NULL;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003391
H. Peter Anvine2c80182005-01-15 22:15:51 +00003392 t = tline;
3393 while (tok_type_(t, TOK_WHITESPACE))
3394 t = t->next;
3395 /* t should now point to the string */
Cyrill Gorcunov4e1d5ab2010-07-23 18:51:51 +04003396 if (!tok_type_(t, TOK_STRING)) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003397 nasm_nonfatal("`%s' requires string as second parameter", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003398 free_tlist(tline);
3399 free_tlist(origline);
3400 return DIRECTIVE_FOUND;
3401 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003402
H. Peter Anvin8b262472019-02-26 14:00:54 -08003403 macro_start = make_tok_num(nasm_unquote(t->text, NULL));
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003404
H. Peter Anvine2c80182005-01-15 22:15:51 +00003405 /*
3406 * We now have a macro name, an implicit parameter count of
3407 * zero, and a numeric token to use as an expansion. Create
3408 * and store an SMacro.
3409 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003410 define_smacro(ctx, mname, casesense, 0, macro_start);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003411 free_tlist(tline);
3412 free_tlist(origline);
3413 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003414
H. Peter Anvin8b262472019-02-26 14:00:54 -08003415 case PP_ISTRCAT:
3416 casesense = false;
3417 /* fall through */
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003418 case PP_STRCAT:
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003419 tline = tline->next;
3420 skip_white_(tline);
3421 tline = expand_id(tline);
3422 if (!tline || (tline->type != TOK_ID &&
3423 (tline->type != TOK_PREPROC_ID ||
3424 tline->text[1] != '$'))) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003425 nasm_nonfatal("`%s' expects a macro identifier as first parameter", dname);
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003426 free_tlist(origline);
3427 return DIRECTIVE_FOUND;
3428 }
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04003429 ctx = get_ctx(tline->text, &mname);
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003430 last = tline;
3431 tline = expand_smacro(tline->next);
3432 last->next = NULL;
3433
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003434 len = 0;
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04003435 list_for_each(t, tline) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003436 switch (t->type) {
3437 case TOK_WHITESPACE:
3438 break;
3439 case TOK_STRING:
3440 len += t->a.len = nasm_unquote(t->text, NULL);
3441 break;
3442 case TOK_OTHER:
3443 if (!strcmp(t->text, ",")) /* permit comma separators */
3444 break;
3445 /* else fall through */
3446 default:
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003447 nasm_nonfatal("non-string passed to `%s': %s", dname, t->text);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003448 free_tlist(tline);
3449 free_tlist(origline);
3450 return DIRECTIVE_FOUND;
3451 }
3452 }
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003453
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003454 p = pp = nasm_malloc(len);
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04003455 list_for_each(t, tline) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003456 if (t->type == TOK_STRING) {
3457 memcpy(p, t->text, t->a.len);
3458 p += t->a.len;
3459 }
3460 }
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003461
3462 /*
3463 * We now have a macro name, an implicit parameter count of
3464 * zero, and a numeric token to use as an expansion. Create
3465 * and store an SMacro.
3466 */
H. Peter Anvin8b262472019-02-26 14:00:54 -08003467 macro_start = make_tok_qstr(pp);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003468 nasm_free(pp);
3469 define_smacro(ctx, mname, casesense, 0, macro_start);
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003470 free_tlist(tline);
3471 free_tlist(origline);
3472 return DIRECTIVE_FOUND;
3473
H. Peter Anvin8b262472019-02-26 14:00:54 -08003474 case PP_ISUBSTR:
3475 casesense = false;
3476 /* fall through */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003477 case PP_SUBSTR:
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003478 {
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003479 int64_t start, count;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003480 size_t len;
H. Peter Anvind2456592008-06-19 15:04:18 -07003481
H. Peter Anvine2c80182005-01-15 22:15:51 +00003482 tline = tline->next;
3483 skip_white_(tline);
3484 tline = expand_id(tline);
3485 if (!tline || (tline->type != TOK_ID &&
3486 (tline->type != TOK_PREPROC_ID ||
3487 tline->text[1] != '$'))) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003488 nasm_nonfatal("`%s' expects a macro identifier as first parameter", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003489 free_tlist(origline);
3490 return DIRECTIVE_FOUND;
3491 }
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04003492 ctx = get_ctx(tline->text, &mname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003493 last = tline;
3494 tline = expand_smacro(tline->next);
3495 last->next = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003496
Cyrill Gorcunov35519d62010-09-06 23:49:52 +04003497 if (tline) /* skip expanded id */
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003498 t = tline->next;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003499 while (tok_type_(t, TOK_WHITESPACE))
3500 t = t->next;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003501
H. Peter Anvine2c80182005-01-15 22:15:51 +00003502 /* t should now point to the string */
Cyrill Gorcunov35519d62010-09-06 23:49:52 +04003503 if (!tok_type_(t, TOK_STRING)) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003504 nasm_nonfatal("`%s' requires string as second parameter", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003505 free_tlist(tline);
3506 free_tlist(origline);
3507 return DIRECTIVE_FOUND;
3508 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003509
H. Peter Anvin8b262472019-02-26 14:00:54 -08003510 pps.tptr = t->next;
3511 pps.ntokens = -1;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003512 tokval.t_type = TOKEN_INVALID;
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003513 evalresult = evaluate(ppscan, &pps, &tokval, NULL, true, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003514 if (!evalresult) {
3515 free_tlist(tline);
3516 free_tlist(origline);
3517 return DIRECTIVE_FOUND;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003518 } else if (!is_simple(evalresult)) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003519 nasm_nonfatal("non-constant value given to `%s'", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003520 free_tlist(tline);
3521 free_tlist(origline);
3522 return DIRECTIVE_FOUND;
3523 }
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003524 start = evalresult->value - 1;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003525
H. Peter Anvin8b262472019-02-26 14:00:54 -08003526 while (tok_type_(pps.tptr, TOK_WHITESPACE))
3527 pps.tptr = pps.tptr->next;
3528 if (!pps.tptr) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003529 count = 1; /* Backwards compatibility: one character */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003530 } else {
3531 tokval.t_type = TOKEN_INVALID;
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003532 evalresult = evaluate(ppscan, &pps, &tokval, NULL, true, NULL);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003533 if (!evalresult) {
3534 free_tlist(tline);
3535 free_tlist(origline);
3536 return DIRECTIVE_FOUND;
3537 } else if (!is_simple(evalresult)) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003538 nasm_nonfatal("non-constant value given to `%s'", dname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003539 free_tlist(tline);
3540 free_tlist(origline);
3541 return DIRECTIVE_FOUND;
3542 }
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003543 count = evalresult->value;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003544 }
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003545
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003546 len = nasm_unquote(t->text, NULL);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003547
Cyrill Gorcunovcff031e2010-09-07 20:31:11 +04003548 /* make start and count being in range */
3549 if (start < 0)
3550 start = 0;
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003551 if (count < 0)
3552 count = len + count + 1 - start;
3553 if (start + count > (int64_t)len)
Cyrill Gorcunovcff031e2010-09-07 20:31:11 +04003554 count = len - start;
3555 if (!len || count < 0 || start >=(int64_t)len)
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003556 start = -1, count = 0; /* empty string */
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003557
H. Peter Anvin8b262472019-02-26 14:00:54 -08003558 macro_start = new_Token(NULL, TOK_STRING, NULL, 0);
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003559 macro_start->text = nasm_quote((start < 0) ? "" : t->text + start, count);
H. Peter Anvin734b1882002-04-30 21:01:08 +00003560
H. Peter Anvine2c80182005-01-15 22:15:51 +00003561 /*
3562 * We now have a macro name, an implicit parameter count of
3563 * zero, and a numeric token to use as an expansion. Create
3564 * and store an SMacro.
3565 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003566 define_smacro(ctx, mname, casesense, 0, macro_start);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003567 free_tlist(tline);
3568 free_tlist(origline);
3569 return DIRECTIVE_FOUND;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003570 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003571
H. Peter Anvine2c80182005-01-15 22:15:51 +00003572 case PP_IASSIGN:
H. Peter Anvin8b262472019-02-26 14:00:54 -08003573 casesense = false;
3574 /* fall through */
3575 case PP_ASSIGN:
H. Peter Anvine2c80182005-01-15 22:15:51 +00003576 tline = tline->next;
3577 skip_white_(tline);
3578 tline = expand_id(tline);
3579 if (!tline || (tline->type != TOK_ID &&
3580 (tline->type != TOK_PREPROC_ID ||
3581 tline->text[1] != '$'))) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003582 nasm_nonfatal("`%s' expects a macro identifier", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003583 free_tlist(origline);
3584 return DIRECTIVE_FOUND;
3585 }
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04003586 ctx = get_ctx(tline->text, &mname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003587 last = tline;
3588 tline = expand_smacro(tline->next);
3589 last->next = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003590
H. Peter Anvin8b262472019-02-26 14:00:54 -08003591 pps.tptr = tline;
3592 pps.ntokens = -1;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003593 tokval.t_type = TOKEN_INVALID;
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003594 evalresult = evaluate(ppscan, &pps, &tokval, NULL, true, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003595 free_tlist(tline);
3596 if (!evalresult) {
3597 free_tlist(origline);
3598 return DIRECTIVE_FOUND;
3599 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003600
H. Peter Anvine2c80182005-01-15 22:15:51 +00003601 if (tokval.t_type)
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08003602 nasm_warn(WARN_OTHER, "trailing garbage after expression ignored");
H. Peter Anvin734b1882002-04-30 21:01:08 +00003603
H. Peter Anvine2c80182005-01-15 22:15:51 +00003604 if (!is_simple(evalresult)) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003605 nasm_nonfatal("non-constant value given to `%s'", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003606 free_tlist(origline);
3607 return DIRECTIVE_FOUND;
H. Peter Anvin8b262472019-02-26 14:00:54 -08003608 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003609
H. Peter Anvin8b262472019-02-26 14:00:54 -08003610 macro_start = make_tok_num(reloc_value(evalresult));
H. Peter Anvin734b1882002-04-30 21:01:08 +00003611
H. Peter Anvine2c80182005-01-15 22:15:51 +00003612 /*
3613 * We now have a macro name, an implicit parameter count of
3614 * zero, and a numeric token to use as an expansion. Create
3615 * and store an SMacro.
3616 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003617 define_smacro(ctx, mname, casesense, 0, macro_start);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003618 free_tlist(origline);
3619 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003620
H. Peter Anvine2c80182005-01-15 22:15:51 +00003621 case PP_LINE:
3622 /*
3623 * Syntax is `%line nnn[+mmm] [filename]'
3624 */
H. Peter Anvin (Intel)800c1682018-12-14 12:22:11 -08003625 if (unlikely(pp_noline)) {
3626 free_tlist(origline);
3627 return DIRECTIVE_FOUND;
3628 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00003629 tline = tline->next;
3630 skip_white_(tline);
3631 if (!tok_type_(tline, TOK_NUMBER)) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003632 nasm_nonfatal("`%s' expects line number", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003633 free_tlist(origline);
3634 return DIRECTIVE_FOUND;
3635 }
H. Peter Anvin70055962007-10-11 00:05:31 -07003636 k = readnum(tline->text, &err);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003637 m = 1;
3638 tline = tline->next;
3639 if (tok_is_(tline, "+")) {
3640 tline = tline->next;
3641 if (!tok_type_(tline, TOK_NUMBER)) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003642 nasm_nonfatal("`%s' expects line increment", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003643 free_tlist(origline);
3644 return DIRECTIVE_FOUND;
3645 }
H. Peter Anvin70055962007-10-11 00:05:31 -07003646 m = readnum(tline->text, &err);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003647 tline = tline->next;
3648 }
3649 skip_white_(tline);
3650 src_set_linnum(k);
3651 istk->lineinc = m;
3652 if (tline) {
H. Peter Anvin274cda82016-05-10 02:56:29 -07003653 char *fname = detoken(tline, false);
3654 src_set_fname(fname);
3655 nasm_free(fname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003656 }
3657 free_tlist(origline);
3658 return DIRECTIVE_FOUND;
Keith Kaniosb307a4f2010-11-06 17:41:51 -05003659
H. Peter Anvine2c80182005-01-15 22:15:51 +00003660 default:
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003661 nasm_nonfatal("preprocessor directive `%s' not yet implemented", dname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003662 return DIRECTIVE_FOUND;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003663 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003664}
3665
3666/*
H. Peter Anvin76690a12002-04-30 20:52:49 +00003667 * Ensure that a macro parameter contains a condition code and
3668 * nothing else. Return the condition code index if so, or -1
3669 * otherwise.
3670 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003671static int find_cc(Token * t)
H. Peter Anvineba20a72002-04-30 20:53:55 +00003672{
H. Peter Anvin76690a12002-04-30 20:52:49 +00003673 Token *tt;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003674
H. Peter Anvin25a99342007-09-22 17:45:45 -07003675 if (!t)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003676 return -1; /* Probably a %+ without a space */
H. Peter Anvin25a99342007-09-22 17:45:45 -07003677
H. Peter Anvineba20a72002-04-30 20:53:55 +00003678 skip_white_(t);
Cyrill Gorcunov7524cfd2017-10-22 19:01:16 +03003679 if (!t)
3680 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003681 if (t->type != TOK_ID)
H. Peter Anvine2c80182005-01-15 22:15:51 +00003682 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003683 tt = t->next;
H. Peter Anvineba20a72002-04-30 20:53:55 +00003684 skip_white_(tt);
H. Peter Anvin76690a12002-04-30 20:52:49 +00003685 if (tt && (tt->type != TOK_OTHER || strcmp(tt->text, ",")))
H. Peter Anvine2c80182005-01-15 22:15:51 +00003686 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003687
Cyrill Gorcunov19456392012-05-02 00:18:56 +04003688 return bsii(t->text, (const char **)conditions, ARRAY_SIZE(conditions));
H. Peter Anvin76690a12002-04-30 20:52:49 +00003689}
3690
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003691/*
3692 * This routines walks over tokens strem and hadnles tokens
3693 * pasting, if @handle_explicit passed then explicit pasting
3694 * term is handled, otherwise -- implicit pastings only.
3695 */
Cyrill Gorcunov575d4282010-10-06 00:25:55 +04003696static bool paste_tokens(Token **head, const struct tokseq_match *m,
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003697 size_t mnum, bool handle_explicit)
H. Peter Anvind784a082009-04-20 14:01:18 -07003698{
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003699 Token *tok, *next, **prev_next, **prev_nonspace;
3700 bool pasted = false;
3701 char *buf, *p;
3702 size_t len, i;
H. Peter Anvind784a082009-04-20 14:01:18 -07003703
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003704 /*
3705 * The last token before pasting. We need it
3706 * to be able to connect new handled tokens.
3707 * In other words if there were a tokens stream
3708 *
3709 * A -> B -> C -> D
3710 *
3711 * and we've joined tokens B and C, the resulting
3712 * stream should be
3713 *
3714 * A -> BC -> D
3715 */
3716 tok = *head;
3717 prev_next = NULL;
3718
3719 if (!tok_type_(tok, TOK_WHITESPACE) && !tok_type_(tok, TOK_PASTE))
3720 prev_nonspace = head;
3721 else
3722 prev_nonspace = NULL;
3723
3724 while (tok && (next = tok->next)) {
3725
3726 switch (tok->type) {
H. Peter Anvind784a082009-04-20 14:01:18 -07003727 case TOK_WHITESPACE:
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003728 /* Zap redundant whitespaces */
3729 while (tok_type_(next, TOK_WHITESPACE))
3730 next = delete_Token(next);
3731 tok->next = next;
H. Peter Anvind784a082009-04-20 14:01:18 -07003732 break;
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003733
3734 case TOK_PASTE:
3735 /* Explicit pasting */
3736 if (!handle_explicit)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003737 break;
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003738 next = delete_Token(tok);
3739
3740 while (tok_type_(next, TOK_WHITESPACE))
3741 next = delete_Token(next);
3742
3743 if (!pasted)
3744 pasted = true;
3745
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003746 /* Left pasting token is start of line */
3747 if (!prev_nonspace)
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003748 nasm_fatal("No lvalue found on pasting");
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003749
Cyrill Gorcunov8b5c9fb2013-02-04 01:24:54 +04003750 /*
3751 * No ending token, this might happen in two
3752 * cases
3753 *
3754 * 1) There indeed no right token at all
3755 * 2) There is a bare "%define ID" statement,
3756 * and @ID does expand to whitespace.
3757 *
3758 * So technically we need to do a grammar analysis
3759 * in another stage of parsing, but for now lets don't
3760 * change the behaviour people used to. Simply allow
3761 * whitespace after paste token.
3762 */
3763 if (!next) {
3764 /*
3765 * Zap ending space tokens and that's all.
3766 */
3767 tok = (*prev_nonspace)->next;
3768 while (tok_type_(tok, TOK_WHITESPACE))
3769 tok = delete_Token(tok);
3770 tok = *prev_nonspace;
3771 tok->next = NULL;
3772 break;
3773 }
3774
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003775 tok = *prev_nonspace;
3776 while (tok_type_(tok, TOK_WHITESPACE))
3777 tok = delete_Token(tok);
3778 len = strlen(tok->text);
3779 len += strlen(next->text);
3780
3781 p = buf = nasm_malloc(len + 1);
3782 strcpy(p, tok->text);
3783 p = strchr(p, '\0');
3784 strcpy(p, next->text);
3785
3786 delete_Token(tok);
3787
3788 tok = tokenize(buf);
3789 nasm_free(buf);
3790
3791 *prev_nonspace = tok;
3792 while (tok && tok->next)
3793 tok = tok->next;
3794
3795 tok->next = delete_Token(next);
3796
3797 /* Restart from pasted tokens head */
3798 tok = *prev_nonspace;
3799 break;
3800
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003801 default:
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003802 /* implicit pasting */
Cyrill Gorcunov575d4282010-10-06 00:25:55 +04003803 for (i = 0; i < mnum; i++) {
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003804 if (!(PP_CONCAT_MATCH(tok, m[i].mask_head)))
3805 continue;
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +04003806
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003807 len = 0;
3808 while (next && PP_CONCAT_MATCH(next, m[i].mask_tail)) {
3809 len += strlen(next->text);
3810 next = next->next;
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +04003811 }
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003812
Cyrill Gorcunov6f8109e2017-10-22 21:26:36 +03003813 /* No match or no text to process */
3814 if (tok == next || len == 0)
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003815 break;
3816
3817 len += strlen(tok->text);
3818 p = buf = nasm_malloc(len + 1);
3819
Adam Majer1a069432017-07-25 11:12:35 +02003820 strcpy(p, tok->text);
3821 p = strchr(p, '\0');
3822 tok = delete_Token(tok);
3823
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003824 while (tok != next) {
Adam Majer1a069432017-07-25 11:12:35 +02003825 if (PP_CONCAT_MATCH(tok, m[i].mask_tail)) {
3826 strcpy(p, tok->text);
3827 p = strchr(p, '\0');
3828 }
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003829 tok = delete_Token(tok);
3830 }
3831
3832 tok = tokenize(buf);
3833 nasm_free(buf);
3834
3835 if (prev_next)
3836 *prev_next = tok;
3837 else
3838 *head = tok;
3839
3840 /*
3841 * Connect pasted into original stream,
3842 * ie A -> new-tokens -> B
3843 */
3844 while (tok && tok->next)
3845 tok = tok->next;
3846 tok->next = next;
3847
3848 if (!pasted)
3849 pasted = true;
3850
3851 /* Restart from pasted tokens head */
3852 tok = prev_next ? *prev_next : *head;
Cyrill Gorcunov575d4282010-10-06 00:25:55 +04003853 }
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003854
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003855 break;
H. Peter Anvind784a082009-04-20 14:01:18 -07003856 }
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003857
3858 prev_next = &tok->next;
3859
3860 if (tok->next &&
3861 !tok_type_(tok->next, TOK_WHITESPACE) &&
3862 !tok_type_(tok->next, TOK_PASTE))
3863 prev_nonspace = prev_next;
3864
3865 tok = tok->next;
H. Peter Anvind784a082009-04-20 14:01:18 -07003866 }
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003867
3868 return pasted;
H. Peter Anvind784a082009-04-20 14:01:18 -07003869}
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003870
3871/*
3872 * expands to a list of tokens from %{x:y}
3873 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003874static Token *expand_mmac_params_range(MMacro *mac, Token *tline, Token ***last)
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003875{
3876 Token *t = tline, **tt, *tm, *head;
3877 char *pos;
3878 int fst, lst, j, i;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003879
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003880 pos = strchr(tline->text, ':');
3881 nasm_assert(pos);
3882
3883 lst = atoi(pos + 1);
3884 fst = atoi(tline->text + 1);
3885
3886 /*
3887 * only macros params are accounted so
3888 * if someone passes %0 -- we reject such
3889 * value(s)
3890 */
3891 if (lst == 0 || fst == 0)
3892 goto err;
3893
3894 /* the values should be sane */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003895 if ((fst > (int)mac->nparam || fst < (-(int)mac->nparam)) ||
3896 (lst > (int)mac->nparam || lst < (-(int)mac->nparam)))
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003897 goto err;
3898
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003899 fst = fst < 0 ? fst + (int)mac->nparam + 1: fst;
3900 lst = lst < 0 ? lst + (int)mac->nparam + 1: lst;
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003901
3902 /* counted from zero */
3903 fst--, lst--;
3904
3905 /*
Cyrill Gorcunove75331c2013-11-09 12:02:15 +04003906 * It will be at least one token. Note we
3907 * need to scan params until separator, otherwise
3908 * only first token will be passed.
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003909 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003910 tm = mac->params[(fst + mac->rotate) % mac->nparam];
Cyrill Gorcunov67f2ca22018-10-13 19:41:01 +03003911 if (!tm)
3912 goto err;
Cyrill Gorcunove75331c2013-11-09 12:02:15 +04003913 head = new_Token(NULL, tm->type, tm->text, 0);
3914 tt = &head->next, tm = tm->next;
3915 while (tok_isnt_(tm, ",")) {
3916 t = new_Token(NULL, tm->type, tm->text, 0);
3917 *tt = t, tt = &t->next, tm = tm->next;
3918 }
3919
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003920 if (fst < lst) {
3921 for (i = fst + 1; i <= lst; i++) {
3922 t = new_Token(NULL, TOK_OTHER, ",", 0);
3923 *tt = t, tt = &t->next;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003924 j = (i + mac->rotate) % mac->nparam;
3925 tm = mac->params[j];
Cyrill Gorcunove75331c2013-11-09 12:02:15 +04003926 while (tok_isnt_(tm, ",")) {
3927 t = new_Token(NULL, tm->type, tm->text, 0);
3928 *tt = t, tt = &t->next, tm = tm->next;
3929 }
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003930 }
3931 } else {
3932 for (i = fst - 1; i >= lst; i--) {
3933 t = new_Token(NULL, TOK_OTHER, ",", 0);
3934 *tt = t, tt = &t->next;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003935 j = (i + mac->rotate) % mac->nparam;
3936 tm = mac->params[j];
Cyrill Gorcunove75331c2013-11-09 12:02:15 +04003937 while (tok_isnt_(tm, ",")) {
3938 t = new_Token(NULL, tm->type, tm->text, 0);
3939 *tt = t, tt = &t->next, tm = tm->next;
3940 }
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003941 }
3942 }
3943
3944 *last = tt;
3945 return head;
3946
3947err:
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003948 nasm_nonfatal("`%%{%s}': macro parameters out of range",
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003949 &tline->text[1]);
3950 return tline;
3951}
3952
H. Peter Anvin76690a12002-04-30 20:52:49 +00003953/*
3954 * Expand MMacro-local things: parameter references (%0, %n, %+n,
H. Peter Anvin67c63722008-10-26 23:49:00 -07003955 * %-n) and MMacro-local identifiers (%%foo) as well as
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003956 * macro indirection (%[...]) and range (%{..:..}).
H. Peter Anvin76690a12002-04-30 20:52:49 +00003957 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003958static Token *expand_mmac_params(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00003959{
H. Peter Anvin734b1882002-04-30 21:01:08 +00003960 Token *t, *tt, **tail, *thead;
H. Peter Anvin6125b622009-04-08 14:02:25 -07003961 bool changed = false;
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003962 char *pos;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003963
3964 tail = &thead;
3965 thead = NULL;
3966
H. Peter Anvine2c80182005-01-15 22:15:51 +00003967 while (tline) {
Cyrill Gorcunov661f7232018-10-28 20:39:34 +03003968 if (tline->type == TOK_PREPROC_ID && tline->text && tline->text[0] &&
Cyrill Gorcunovca611192010-06-04 09:22:12 +04003969 (((tline->text[1] == '+' || tline->text[1] == '-') && tline->text[2]) ||
3970 (tline->text[1] >= '0' && tline->text[1] <= '9') ||
3971 tline->text[1] == '%')) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00003972 char *text = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003973 int type = 0, cc; /* type = 0 to placate optimisers */
Keith Kaniosa6dfa782007-04-13 16:47:53 +00003974 char tmpbuf[30];
H. Peter Anvin25a99342007-09-22 17:45:45 -07003975 unsigned int n;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003976 int i;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003977 MMacro *mac;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003978
H. Peter Anvine2c80182005-01-15 22:15:51 +00003979 t = tline;
3980 tline = tline->next;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003981
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003982 mac = istk->mstk;
3983 while (mac && !mac->name) /* avoid mistaking %reps for macros */
3984 mac = mac->next_active;
3985 if (!mac) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003986 nasm_nonfatal("`%s': not in a macro call", t->text);
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003987 } else {
3988 pos = strchr(t->text, ':');
3989 if (!pos) {
3990 switch (t->text[1]) {
3991 /*
3992 * We have to make a substitution of one of the
3993 * forms %1, %-1, %+1, %%foo, %0.
3994 */
3995 case '0':
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003996 type = TOK_NUMBER;
3997 snprintf(tmpbuf, sizeof(tmpbuf), "%d", mac->nparam);
3998 text = nasm_strdup(tmpbuf);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003999 break;
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004000 case '%':
H. Peter Anvine2c80182005-01-15 22:15:51 +00004001 type = TOK_ID;
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004002 snprintf(tmpbuf, sizeof(tmpbuf), "..@%"PRIu64".",
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004003 mac->unique);
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004004 text = nasm_strcat(tmpbuf, t->text + 2);
4005 break;
4006 case '-':
4007 n = atoi(t->text + 2) - 1;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004008 if (n >= mac->nparam)
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004009 tt = NULL;
4010 else {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004011 if (mac->nparam > 1)
4012 n = (n + mac->rotate) % mac->nparam;
4013 tt = mac->params[n];
H. Peter Anvine2c80182005-01-15 22:15:51 +00004014 }
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004015 cc = find_cc(tt);
4016 if (cc == -1) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03004017 nasm_nonfatal("macro parameter %d is not a condition code",
4018 n + 1);
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004019 text = NULL;
4020 } else {
4021 type = TOK_ID;
4022 if (inverse_ccs[cc] == -1) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03004023 nasm_nonfatal("condition code `%s' is not invertible",
4024 conditions[cc]);
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004025 text = NULL;
4026 } else
4027 text = nasm_strdup(conditions[inverse_ccs[cc]]);
4028 }
4029 break;
4030 case '+':
4031 n = atoi(t->text + 2) - 1;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004032 if (n >= mac->nparam)
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004033 tt = NULL;
4034 else {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004035 if (mac->nparam > 1)
4036 n = (n + mac->rotate) % mac->nparam;
4037 tt = mac->params[n];
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004038 }
4039 cc = find_cc(tt);
4040 if (cc == -1) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03004041 nasm_nonfatal("macro parameter %d is not a condition code",
4042 n + 1);
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004043 text = NULL;
4044 } else {
4045 type = TOK_ID;
4046 text = nasm_strdup(conditions[cc]);
4047 }
4048 break;
4049 default:
4050 n = atoi(t->text + 1) - 1;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004051 if (n >= mac->nparam)
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004052 tt = NULL;
4053 else {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004054 if (mac->nparam > 1)
4055 n = (n + mac->rotate) % mac->nparam;
4056 tt = mac->params[n];
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004057 }
4058 if (tt) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004059 for (i = 0; i < mac->paramlen[n]; i++) {
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004060 *tail = new_Token(NULL, tt->type, tt->text, 0);
4061 tail = &(*tail)->next;
4062 tt = tt->next;
4063 }
4064 }
4065 text = NULL; /* we've done it here */
4066 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004067 }
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004068 } else {
4069 /*
4070 * seems we have a parameters range here
4071 */
4072 Token *head, **last;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004073 head = expand_mmac_params_range(mac, t, &last);
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004074 if (head != t) {
4075 *tail = head;
4076 *last = tline;
4077 tline = head;
4078 text = NULL;
4079 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00004080 }
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004081 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00004082 if (!text) {
4083 delete_Token(t);
4084 } else {
4085 *tail = t;
4086 tail = &t->next;
4087 t->type = type;
4088 nasm_free(t->text);
4089 t->text = text;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004090 t->a.mac = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004091 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004092 changed = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004093 continue;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004094 } else if (tline->type == TOK_INDIRECT) {
4095 t = tline;
4096 tline = tline->next;
4097 tt = tokenize(t->text);
4098 tt = expand_mmac_params(tt);
4099 tt = expand_smacro(tt);
4100 *tail = tt;
4101 while (tt) {
4102 tt->a.mac = NULL; /* Necessary? */
4103 tail = &tt->next;
4104 tt = tt->next;
4105 }
4106 delete_Token(t);
4107 changed = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004108 } else {
4109 t = *tail = tline;
4110 tline = tline->next;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004111 t->a.mac = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004112 tail = &t->next;
4113 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00004114 }
H. Peter Anvineba20a72002-04-30 20:53:55 +00004115 *tail = NULL;
H. Peter Anvin67c63722008-10-26 23:49:00 -07004116
Cyrill Gorcunovc6a742c2011-06-27 01:23:09 +04004117 if (changed) {
4118 const struct tokseq_match t[] = {
4119 {
4120 PP_CONCAT_MASK(TOK_ID) |
4121 PP_CONCAT_MASK(TOK_FLOAT), /* head */
4122 PP_CONCAT_MASK(TOK_ID) |
4123 PP_CONCAT_MASK(TOK_NUMBER) |
4124 PP_CONCAT_MASK(TOK_FLOAT) |
4125 PP_CONCAT_MASK(TOK_OTHER) /* tail */
4126 },
4127 {
4128 PP_CONCAT_MASK(TOK_NUMBER), /* head */
4129 PP_CONCAT_MASK(TOK_NUMBER) /* tail */
4130 }
4131 };
4132 paste_tokens(&thead, t, ARRAY_SIZE(t), false);
4133 }
H. Peter Anvin6125b622009-04-08 14:02:25 -07004134
H. Peter Anvin76690a12002-04-30 20:52:49 +00004135 return thead;
4136}
4137
4138/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004139 * Expand all single-line macro calls made in the given line.
4140 * Return the expanded version of the line. The original is deemed
4141 * to be destroyed in the process. (In reality we'll just move
4142 * Tokens from input to output a lot of the time, rather than
4143 * actually bothering to destroy and replicate.)
4144 */
H. Peter Anvincb1cf592007-11-19 12:26:50 -08004145
H. Peter Anvine2c80182005-01-15 22:15:51 +00004146static Token *expand_smacro(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004147{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004148 Token *t, *tt, *mstart, **tail, *thead;
H. Peter Anvineba20a72002-04-30 20:53:55 +00004149 SMacro *head = NULL, *m;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004150 Token **params;
4151 int *paramsize;
H. Peter Anvin8b262472019-02-26 14:00:54 -08004152 Token *eparams;
4153 unsigned int nparam, sparam, i;
H. Peter Anvind784a082009-04-20 14:01:18 -07004154 int brackets;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004155 Token *org_tline = tline;
4156 Context *ctx;
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08004157 const char *mname;
H. Peter Anvina3d96d02018-06-15 17:51:39 -07004158 int64_t deadman = nasm_limit[LIMIT_MACROS];
H. Peter Anvin8287daf2009-07-07 16:00:58 -07004159 bool expanded;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004160
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004161 /*
4162 * Trick: we should avoid changing the start token pointer since it can
4163 * be contained in "next" field of other token. Because of this
4164 * we allocate a copy of first token and work with it; at the end of
4165 * routine we copy it back
4166 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004167 if (org_tline) {
Cyrill Gorcunoved4a8052010-04-09 15:40:35 +04004168 tline = new_Token(org_tline->next, org_tline->type,
4169 org_tline->text, 0);
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004170 tline->a.mac = org_tline->a.mac;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004171 nasm_free(org_tline->text);
4172 org_tline->text = NULL;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004173 }
4174
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004175 expanded = true; /* Always expand %+ at least once */
H. Peter Anvin8287daf2009-07-07 16:00:58 -07004176
H. Peter Anvincb1cf592007-11-19 12:26:50 -08004177again:
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004178 thead = NULL;
Cyrill Gorcunoved4a8052010-04-09 15:40:35 +04004179 tail = &thead;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004180
H. Peter Anvine2c80182005-01-15 22:15:51 +00004181 while (tline) { /* main token loop */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004182 if (!--deadman) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03004183 nasm_nonfatal("interminable macro recursion");
Cyrill Gorcunovbd38c8f2009-11-21 11:11:23 +03004184 goto err;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004185 }
H. Peter Anvincb1cf592007-11-19 12:26:50 -08004186
H. Peter Anvine2c80182005-01-15 22:15:51 +00004187 if ((mname = tline->text)) {
4188 /* if this token is a local macro, look in local context */
Cyrill Gorcunovc56d9ad2010-02-11 15:12:19 +03004189 if (tline->type == TOK_ID) {
4190 head = (SMacro *)hash_findix(&smacros, mname);
4191 } else if (tline->type == TOK_PREPROC_ID) {
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04004192 ctx = get_ctx(mname, &mname);
Cyrill Gorcunovc56d9ad2010-02-11 15:12:19 +03004193 head = ctx ? (SMacro *)hash_findix(&ctx->localmac, mname) : NULL;
4194 } else
4195 head = NULL;
H. Peter Anvin072771e2008-05-22 13:17:51 -07004196
H. Peter Anvine2c80182005-01-15 22:15:51 +00004197 /*
4198 * We've hit an identifier. As in is_mmacro below, we first
4199 * check whether the identifier is a single-line macro at
4200 * all, then think about checking for parameters if
4201 * necessary.
4202 */
H. Peter Anvin8b262472019-02-26 14:00:54 -08004203 list_for_each(m, head) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004204 if (!mstrcmp(m->name, mname, m->casesense))
4205 break;
H. Peter Anvin8b262472019-02-26 14:00:54 -08004206 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004207 if (m) {
4208 mstart = tline;
4209 params = NULL;
4210 paramsize = NULL;
H. Peter Anvin8b262472019-02-26 14:00:54 -08004211 eparams = NULL;
4212
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004213 if (m->nparam == 0) {
4214 /*
H. Peter Anvin8b262472019-02-26 14:00:54 -08004215 * Simple case: the macro is parameterless.
4216 * Nothing to parse; just drop the macro token itself.
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004217 */
H. Peter Anvin8b262472019-02-26 14:00:54 -08004218 tline = tline->next;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004219 } else {
4220 /*
4221 * Complicated case: at least one macro with this name
H. Peter Anvine2c80182005-01-15 22:15:51 +00004222 * exists and takes parameters. We must find the
4223 * parameters in the call, count them, find the SMacro
4224 * that corresponds to that form of the macro call, and
4225 * substitute for the parameters when we expand. What a
4226 * pain.
4227 */
4228 /*tline = tline->next;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004229 skip_white_(tline); */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004230 do {
4231 t = tline->next;
4232 while (tok_type_(t, TOK_SMAC_END)) {
Cyrill Gorcunov982186a2019-03-16 23:05:50 +03004233 if (t->a.mac)
4234 t->a.mac->in_progress = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004235 t->text = NULL;
4236 t = tline->next = delete_Token(t);
4237 }
4238 tline = t;
4239 } while (tok_type_(tline, TOK_WHITESPACE));
4240 if (!tok_is_(tline, "(")) {
4241 /*
4242 * This macro wasn't called with parameters: ignore
4243 * the call. (Behaviour borrowed from gnu cpp.)
4244 */
4245 tline = mstart;
4246 m = NULL;
4247 } else {
4248 int paren = 0;
4249 int white = 0;
4250 brackets = 0;
4251 nparam = 0;
4252 sparam = PARAM_DELTA;
4253 params = nasm_malloc(sparam * sizeof(Token *));
4254 params[0] = tline->next;
4255 paramsize = nasm_malloc(sparam * sizeof(int));
4256 paramsize[0] = 0;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07004257 while (true) { /* parameter loop */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004258 /*
4259 * For some unusual expansions
4260 * which concatenates function call
4261 */
4262 t = tline->next;
4263 while (tok_type_(t, TOK_SMAC_END)) {
Cyrill Gorcunov982186a2019-03-16 23:05:50 +03004264 if (t->a.mac)
4265 t->a.mac->in_progress = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004266 t->text = NULL;
4267 t = tline->next = delete_Token(t);
4268 }
4269 tline = t;
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00004270
H. Peter Anvine2c80182005-01-15 22:15:51 +00004271 if (!tline) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03004272 nasm_nonfatal("macro call expects terminating `)'");
H. Peter Anvine2c80182005-01-15 22:15:51 +00004273 break;
4274 }
4275 if (tline->type == TOK_WHITESPACE
4276 && brackets <= 0) {
4277 if (paramsize[nparam])
4278 white++;
4279 else
4280 params[nparam] = tline->next;
4281 continue; /* parameter loop */
4282 }
4283 if (tline->type == TOK_OTHER
4284 && tline->text[1] == 0) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004285 char ch = tline->text[0];
H. Peter Anvine2c80182005-01-15 22:15:51 +00004286 if (ch == ',' && !paren && brackets <= 0) {
4287 if (++nparam >= sparam) {
4288 sparam += PARAM_DELTA;
4289 params = nasm_realloc(params,
Cyrill Gorcunoved4a8052010-04-09 15:40:35 +04004290 sparam * sizeof(Token *));
4291 paramsize = nasm_realloc(paramsize,
4292 sparam * sizeof(int));
H. Peter Anvine2c80182005-01-15 22:15:51 +00004293 }
4294 params[nparam] = tline->next;
4295 paramsize[nparam] = 0;
4296 white = 0;
4297 continue; /* parameter loop */
4298 }
4299 if (ch == '{' &&
4300 (brackets > 0 || (brackets == 0 &&
4301 !paramsize[nparam])))
4302 {
4303 if (!(brackets++)) {
4304 params[nparam] = tline->next;
4305 continue; /* parameter loop */
4306 }
4307 }
4308 if (ch == '}' && brackets > 0)
4309 if (--brackets == 0) {
4310 brackets = -1;
4311 continue; /* parameter loop */
4312 }
4313 if (ch == '(' && !brackets)
4314 paren++;
4315 if (ch == ')' && brackets <= 0)
4316 if (--paren < 0)
4317 break;
4318 }
4319 if (brackets < 0) {
4320 brackets = 0;
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03004321 nasm_nonfatal("braces do not "
H. Peter Anvine2c80182005-01-15 22:15:51 +00004322 "enclose all of macro parameter");
4323 }
4324 paramsize[nparam] += white + 1;
4325 white = 0;
4326 } /* parameter loop */
4327 nparam++;
H. Peter Anvin8b262472019-02-26 14:00:54 -08004328
H. Peter Anvine2c80182005-01-15 22:15:51 +00004329 while (m && (m->nparam != nparam ||
H. Peter Anvin8b262472019-02-26 14:00:54 -08004330 mstrcmp(m->name, mname, m->casesense)))
H. Peter Anvine2c80182005-01-15 22:15:51 +00004331 m = m->next;
H. Peter Anvin (Intel)723ab482018-12-13 21:53:31 -08004332 if (!m) {
4333 /*!
4334 *!macro-params [on] macro calls with wrong parameter count
4335 *! covers warnings about \i{multi-line macros} being invoked
4336 *! with the wrong number of parameters. See \k{mlmacover} for an
4337 *! example of why you might want to disable this warning.
4338 */
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08004339 nasm_warn(WARN_MACRO_PARAMS,
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03004340 "macro `%s' exists, "
4341 "but not taking %d parameters",
4342 mstart->text, nparam);
H. Peter Anvin8b262472019-02-26 14:00:54 -08004343 } else if (m->eval_param) {
4344 struct ppscan pps;
4345 struct tokenval tokval;
4346 expr *evalresult;
4347
4348 /* Evaluate parameters if applicable */
4349 for (i = 0; i < nparam; i++) {
4350 if (!m->eval_param[i])
4351 continue;
4352
4353 pps.tptr = params[i];
4354 pps.ntokens = paramsize[i];
4355 tokval.t_type = TOKEN_INVALID;
4356 evalresult = evaluate(ppscan, &pps, &tokval,
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07004357 NULL, true, NULL);
H. Peter Anvin8b262472019-02-26 14:00:54 -08004358 if (!evalresult)
4359 continue;
4360
4361 if (tokval.t_type) {
4362 nasm_error(ERR_NONFATAL,
4363 "invalid expression in parameter %d of macro `%s'", i, m->name);
4364 continue;
4365 }
4366
4367 if (!is_simple(evalresult)) {
4368 nasm_error(ERR_NONFATAL,
4369 "non-constant expression in parameter %d of macro `%s'", i, m->name);
4370 continue;
4371 }
4372 params[i] = make_tok_num(reloc_value(evalresult));
4373 params[i]->next = eparams;
4374 eparams = params[i];
4375 paramsize[i] = 1;
4376 }
4377 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00004378 }
4379 }
4380 if (m && m->in_progress)
4381 m = NULL;
H. Peter Anvin8b262472019-02-26 14:00:54 -08004382 if (!m) {
4383 /* in progress or didn't find '(' or wrong nparam */
H. Peter Anvin70653092007-10-19 14:42:29 -07004384 /*
H. Peter Anvine2c80182005-01-15 22:15:51 +00004385 * Design question: should we handle !tline, which
4386 * indicates missing ')' here, or expand those
4387 * macros anyway, which requires the (t) test a few
H. Peter Anvin70653092007-10-19 14:42:29 -07004388 * lines down?
H. Peter Anvine2c80182005-01-15 22:15:51 +00004389 */
H. Peter Anvin8b262472019-02-26 14:00:54 -08004390 free_tlist(eparams);
H. Peter Anvine2c80182005-01-15 22:15:51 +00004391 nasm_free(params);
4392 nasm_free(paramsize);
4393 tline = mstart;
4394 } else {
4395 /*
4396 * Expand the macro: we are placed on the last token of the
4397 * call, so that we can easily split the call from the
4398 * following tokens. We also start by pushing an SMAC_END
4399 * token for the cycle removal.
4400 */
H. Peter Anvin8b262472019-02-26 14:00:54 -08004401 Token *expansion;
4402
H. Peter Anvine2c80182005-01-15 22:15:51 +00004403 t = tline;
4404 if (t) {
4405 tline = t->next;
4406 t->next = NULL;
4407 }
4408 tt = new_Token(tline, TOK_SMAC_END, NULL, 0);
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004409 tt->a.mac = m;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07004410 m->in_progress = true;
H. Peter Anvin8b262472019-02-26 14:00:54 -08004411 if (unlikely(m->magic))
4412 expansion = m->e.magic(m, params, paramsize);
4413 else
4414 expansion = m->e.expansion;
4415
H. Peter Anvine2c80182005-01-15 22:15:51 +00004416 tline = tt;
H. Peter Anvin8b262472019-02-26 14:00:54 -08004417 list_for_each(t, expansion) {
4418 if (is_smac_param(t->type)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00004419 Token *pcopy = tline, **ptail = &pcopy;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004420 Token *ttt, *pt;
4421 int i;
H. Peter Anvin734b1882002-04-30 21:01:08 +00004422
H. Peter Anvin8b262472019-02-26 14:00:54 -08004423 ttt = params[smac_nparam(t->type)];
4424 i = paramsize[smac_nparam(t->type)];
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004425 while (--i >= 0) {
H. Peter Anvin8b262472019-02-26 14:00:54 -08004426 nasm_assert(ttt);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004427 pt = *ptail = new_Token(tline, ttt->type,
4428 ttt->text, 0);
4429 ptail = &pt->next;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004430 ttt = ttt->next;
Cyrill Gorcunov59ce1c62017-10-22 18:42:07 +03004431 if (!ttt && i > 0) {
4432 /*
4433 * FIXME: Need to handle more gracefully,
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07004434 * exiting early on arguments analysis.
Cyrill Gorcunov59ce1c62017-10-22 18:42:07 +03004435 */
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03004436 nasm_fatal("macro `%s' expects %d args",
Cyrill Gorcunov59ce1c62017-10-22 18:42:07 +03004437 mstart->text,
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07004438 paramsize[t->type - TOK_SMAC_START_PARAMS]);
Cyrill Gorcunov59ce1c62017-10-22 18:42:07 +03004439 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00004440 }
4441 tline = pcopy;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004442 } else if (t->type == TOK_PREPROC_Q) {
4443 tt = new_Token(tline, TOK_ID, mname, 0);
4444 tline = tt;
4445 } else if (t->type == TOK_PREPROC_QQ) {
4446 tt = new_Token(tline, TOK_ID, m->name, 0);
4447 tline = tt;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004448 } else {
4449 tt = new_Token(tline, t->type, t->text, 0);
4450 tline = tt;
4451 }
4452 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00004453
H. Peter Anvine2c80182005-01-15 22:15:51 +00004454 /*
4455 * Having done that, get rid of the macro call, and clean
4456 * up the parameters.
4457 */
4458 nasm_free(params);
4459 nasm_free(paramsize);
4460 free_tlist(mstart);
H. Peter Anvin8b262472019-02-26 14:00:54 -08004461 free_tlist(eparams);
4462 if (m->magic)
4463 free_tlist(expansion);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004464 expanded = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004465 continue; /* main token loop */
4466 }
4467 }
4468 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004469
H. Peter Anvine2c80182005-01-15 22:15:51 +00004470 if (tline->type == TOK_SMAC_END) {
Cyrill Gorcunov980dd652018-10-14 19:25:32 +03004471 /* On error path it might already be dropped */
4472 if (tline->a.mac)
4473 tline->a.mac->in_progress = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004474 tline = delete_Token(tline);
4475 } else {
4476 t = *tail = tline;
4477 tline = tline->next;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004478 t->a.mac = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004479 t->next = NULL;
4480 tail = &t->next;
4481 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004482 }
4483
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004484 /*
4485 * Now scan the entire line and look for successive TOK_IDs that resulted
Keith Kaniosb7a89542007-04-12 02:40:54 +00004486 * after expansion (they can't be produced by tokenize()). The successive
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004487 * TOK_IDs should be concatenated.
4488 * Also we look for %+ tokens and concatenate the tokens before and after
4489 * them (without white spaces in between).
4490 */
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +04004491 if (expanded) {
Cyrill Gorcunovc6a742c2011-06-27 01:23:09 +04004492 const struct tokseq_match t[] = {
4493 {
4494 PP_CONCAT_MASK(TOK_ID) |
4495 PP_CONCAT_MASK(TOK_PREPROC_ID), /* head */
4496 PP_CONCAT_MASK(TOK_ID) |
4497 PP_CONCAT_MASK(TOK_PREPROC_ID) |
4498 PP_CONCAT_MASK(TOK_NUMBER) /* tail */
4499 }
4500 };
4501 if (paste_tokens(&thead, t, ARRAY_SIZE(t), true)) {
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +04004502 /*
4503 * If we concatenated something, *and* we had previously expanded
4504 * an actual macro, scan the lines again for macros...
4505 */
4506 tline = thead;
4507 expanded = false;
4508 goto again;
4509 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00004510 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004511
Cyrill Gorcunovbd38c8f2009-11-21 11:11:23 +03004512err:
H. Peter Anvine2c80182005-01-15 22:15:51 +00004513 if (org_tline) {
4514 if (thead) {
4515 *org_tline = *thead;
4516 /* since we just gave text to org_line, don't free it */
4517 thead->text = NULL;
4518 delete_Token(thead);
4519 } else {
4520 /* the expression expanded to empty line;
4521 we can't return NULL for some reasons
4522 we just set the line to a single WHITESPACE token. */
4523 memset(org_tline, 0, sizeof(*org_tline));
4524 org_tline->text = NULL;
4525 org_tline->type = TOK_WHITESPACE;
4526 }
4527 thead = org_tline;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004528 }
4529
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004530 return thead;
4531}
4532
4533/*
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004534 * Similar to expand_smacro but used exclusively with macro identifiers
4535 * right before they are fetched in. The reason is that there can be
4536 * identifiers consisting of several subparts. We consider that if there
4537 * are more than one element forming the name, user wants a expansion,
4538 * otherwise it will be left as-is. Example:
4539 *
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004540 * %define %$abc cde
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004541 *
4542 * the identifier %$abc will be left as-is so that the handler for %define
4543 * will suck it and define the corresponding value. Other case:
4544 *
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004545 * %define _%$abc cde
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004546 *
4547 * In this case user wants name to be expanded *before* %define starts
4548 * working, so we'll expand %$abc into something (if it has a value;
4549 * otherwise it will be left as-is) then concatenate all successive
4550 * PP_IDs into one.
4551 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004552static Token *expand_id(Token * tline)
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004553{
4554 Token *cur, *oldnext = NULL;
4555
H. Peter Anvin734b1882002-04-30 21:01:08 +00004556 if (!tline || !tline->next)
H. Peter Anvine2c80182005-01-15 22:15:51 +00004557 return tline;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004558
4559 cur = tline;
4560 while (cur->next &&
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004561 (cur->next->type == TOK_ID ||
4562 cur->next->type == TOK_PREPROC_ID
4563 || cur->next->type == TOK_NUMBER))
H. Peter Anvine2c80182005-01-15 22:15:51 +00004564 cur = cur->next;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004565
4566 /* If identifier consists of just one token, don't expand */
4567 if (cur == tline)
H. Peter Anvine2c80182005-01-15 22:15:51 +00004568 return tline;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004569
H. Peter Anvine2c80182005-01-15 22:15:51 +00004570 if (cur) {
4571 oldnext = cur->next; /* Detach the tail past identifier */
4572 cur->next = NULL; /* so that expand_smacro stops here */
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004573 }
4574
H. Peter Anvin734b1882002-04-30 21:01:08 +00004575 tline = expand_smacro(tline);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004576
H. Peter Anvine2c80182005-01-15 22:15:51 +00004577 if (cur) {
4578 /* expand_smacro possibly changhed tline; re-scan for EOL */
4579 cur = tline;
4580 while (cur && cur->next)
4581 cur = cur->next;
4582 if (cur)
4583 cur->next = oldnext;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004584 }
4585
4586 return tline;
4587}
4588
4589/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004590 * Determine whether the given line constitutes a multi-line macro
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004591 * call, and return the MMacro structure called if so. Doesn't have
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004592 * to check for an initial label - that's taken care of in
4593 * expand_mmacro - but must check numbers of parameters. Guaranteed
4594 * to be called with tline->type == TOK_ID, so the putative macro
4595 * name is easy to find.
4596 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004597static MMacro *is_mmacro(Token * tline, Token *** params_array)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004598{
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004599 MMacro *head, *m;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004600 Token **params;
4601 int nparam;
4602
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004603 head = (MMacro *) hash_findix(&mmacros, tline->text);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004604
4605 /*
4606 * Efficiency: first we see if any macro exists with the given
4607 * name. If not, we can return NULL immediately. _Then_ we
4608 * count the parameters, and then we look further along the
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004609 * list if necessary to find the proper MMacro.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004610 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004611 list_for_each(m, head)
4612 if (!mstrcmp(m->name, tline->text, m->casesense))
H. Peter Anvine2c80182005-01-15 22:15:51 +00004613 break;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004614 if (!m)
H. Peter Anvine2c80182005-01-15 22:15:51 +00004615 return NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004616
4617 /*
4618 * OK, we have a potential macro. Count and demarcate the
4619 * parameters.
4620 */
H. Peter Anvin734b1882002-04-30 21:01:08 +00004621 count_mmac_params(tline->next, &nparam, &params);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004622
4623 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004624 * So we know how many parameters we've got. Find the MMacro
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004625 * structure that handles this number.
4626 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004627 while (m) {
4628 if (m->nparam_min <= nparam
4629 && (m->plus || nparam <= m->nparam_max)) {
4630 /*
4631 * This one is right. Just check if cycle removal
4632 * prohibits us using it before we actually celebrate...
4633 */
4634 if (m->in_progress > m->max_depth) {
4635 if (m->max_depth > 0) {
H. Peter Anvin (Intel)80c4f232018-12-14 13:33:24 -08004636 nasm_warn(WARN_OTHER, "reached maximum recursion depth of %i",
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03004637 m->max_depth);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004638 }
4639 nasm_free(params);
4640 return NULL;
4641 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00004642 /*
4643 * It's right, and we can use it. Add its default
4644 * parameters to the end of our list if necessary.
4645 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004646 if (m->defaults && nparam < m->nparam_min + m->ndefs) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00004647 params =
4648 nasm_realloc(params,
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004649 ((m->nparam_min + m->ndefs +
H. Peter Anvine2c80182005-01-15 22:15:51 +00004650 1) * sizeof(*params)));
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004651 while (nparam < m->nparam_min + m->ndefs) {
4652 params[nparam] = m->defaults[nparam - m->nparam_min];
H. Peter Anvine2c80182005-01-15 22:15:51 +00004653 nparam++;
4654 }
4655 }
4656 /*
4657 * If we've gone over the maximum parameter count (and
4658 * we're in Plus mode), ignore parameters beyond
4659 * nparam_max.
4660 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004661 if (m->plus && nparam > m->nparam_max)
4662 nparam = m->nparam_max;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004663 /*
4664 * Then terminate the parameter list, and leave.
4665 */
4666 if (!params) { /* need this special case */
4667 params = nasm_malloc(sizeof(*params));
4668 nparam = 0;
4669 }
4670 params[nparam] = NULL;
4671 *params_array = params;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004672 return m;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004673 }
4674 /*
4675 * This one wasn't right: look for the next one with the
4676 * same name.
4677 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004678 list_for_each(m, m->next)
4679 if (!mstrcmp(m->name, tline->text, m->casesense))
H. Peter Anvine2c80182005-01-15 22:15:51 +00004680 break;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004681 }
4682
4683 /*
4684 * After all that, we didn't find one with the right number of
4685 * parameters. Issue a warning, and fail to expand the macro.
4686 */
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08004687 nasm_warn(WARN_MACRO_PARAMS,
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03004688 "macro `%s' exists, but not taking %d parameters",
4689 tline->text, nparam);
H. Peter Anvin734b1882002-04-30 21:01:08 +00004690 nasm_free(params);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004691 return NULL;
4692}
4693
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004694
4695/*
4696 * Save MMacro invocation specific fields in
4697 * preparation for a recursive macro expansion
4698 */
4699static void push_mmacro(MMacro *m)
4700{
4701 MMacroInvocation *i;
4702
4703 i = nasm_malloc(sizeof(MMacroInvocation));
4704 i->prev = m->prev;
4705 i->params = m->params;
4706 i->iline = m->iline;
4707 i->nparam = m->nparam;
4708 i->rotate = m->rotate;
4709 i->paramlen = m->paramlen;
4710 i->unique = m->unique;
4711 i->condcnt = m->condcnt;
4712 m->prev = i;
4713}
4714
4715
4716/*
4717 * Restore MMacro invocation specific fields that were
4718 * saved during a previous recursive macro expansion
4719 */
4720static void pop_mmacro(MMacro *m)
4721{
4722 MMacroInvocation *i;
4723
4724 if (m->prev) {
4725 i = m->prev;
4726 m->prev = i->prev;
4727 m->params = i->params;
4728 m->iline = i->iline;
4729 m->nparam = i->nparam;
4730 m->rotate = i->rotate;
4731 m->paramlen = i->paramlen;
4732 m->unique = i->unique;
4733 m->condcnt = i->condcnt;
4734 nasm_free(i);
4735 }
4736}
4737
4738
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004739/*
4740 * Expand the multi-line macro call made by the given line, if
4741 * there is one to be expanded. If there is, push the expansion on
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004742 * istk->expansion and return 1. Otherwise return 0.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004743 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004744static int expand_mmacro(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004745{
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004746 Token *startline = tline;
H. Peter Anvineba20a72002-04-30 20:53:55 +00004747 Token *label = NULL;
4748 int dont_prepend = 0;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004749 Token **params, *t, *tt;
4750 MMacro *m;
4751 Line *l, *ll;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004752 int i, nparam, *paramlen;
H. Peter Anvinc751e862008-06-09 10:18:45 -07004753 const char *mname;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004754
4755 t = tline;
H. Peter Anvineba20a72002-04-30 20:53:55 +00004756 skip_white_(t);
H. Peter Anvince2233b2008-05-25 21:57:00 -07004757 /* if (!tok_type_(t, TOK_ID)) Lino 02/25/02 */
H. Peter Anvindce1e2f2002-04-30 21:06:37 +00004758 if (!tok_type_(t, TOK_ID) && !tok_type_(t, TOK_PREPROC_ID))
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004759 return 0;
4760 m = is_mmacro(t, &params);
4761 if (m) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004762 mname = t->text;
H. Peter Anvinc751e862008-06-09 10:18:45 -07004763 } else {
H. Peter Anvine2c80182005-01-15 22:15:51 +00004764 Token *last;
4765 /*
4766 * We have an id which isn't a macro call. We'll assume
4767 * it might be a label; we'll also check to see if a
4768 * colon follows it. Then, if there's another id after
4769 * that lot, we'll check it again for macro-hood.
4770 */
4771 label = last = t;
4772 t = t->next;
4773 if (tok_type_(t, TOK_WHITESPACE))
4774 last = t, t = t->next;
4775 if (tok_is_(t, ":")) {
4776 dont_prepend = 1;
4777 last = t, t = t->next;
4778 if (tok_type_(t, TOK_WHITESPACE))
4779 last = t, t = t->next;
4780 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004781 if (!tok_type_(t, TOK_ID) || !(m = is_mmacro(t, &params)))
4782 return 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004783 last->next = NULL;
Keith Kanios891775e2009-07-11 06:08:54 -05004784 mname = t->text;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004785 tline = t;
H. Peter Anvineba20a72002-04-30 20:53:55 +00004786 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004787
4788 /*
4789 * Fix up the parameters: this involves stripping leading and
4790 * trailing whitespace, then stripping braces if they are
4791 * present.
4792 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004793 for (nparam = 0; params[nparam]; nparam++) ;
H. Peter Anvin734b1882002-04-30 21:01:08 +00004794 paramlen = nparam ? nasm_malloc(nparam * sizeof(*paramlen)) : NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004795
H. Peter Anvine2c80182005-01-15 22:15:51 +00004796 for (i = 0; params[i]; i++) {
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08004797 int brace = 0;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004798 int comma = (!m->plus || i < nparam - 1);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004799
H. Peter Anvine2c80182005-01-15 22:15:51 +00004800 t = params[i];
4801 skip_white_(t);
4802 if (tok_is_(t, "{"))
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08004803 t = t->next, brace++, comma = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004804 params[i] = t;
4805 paramlen[i] = 0;
4806 while (t) {
4807 if (comma && t->type == TOK_OTHER && !strcmp(t->text, ","))
4808 break; /* ... because we have hit a comma */
4809 if (comma && t->type == TOK_WHITESPACE
4810 && tok_is_(t->next, ","))
4811 break; /* ... or a space then a comma */
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08004812 if (brace && t->type == TOK_OTHER) {
4813 if (t->text[0] == '{')
4814 brace++; /* ... or a nested opening brace */
4815 else if (t->text[0] == '}')
4816 if (!--brace)
4817 break; /* ... or a brace */
4818 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00004819 t = t->next;
4820 paramlen[i]++;
4821 }
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08004822 if (brace)
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03004823 nasm_nonfatal("macro params should be enclosed in braces");
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004824 }
4825
4826 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004827 * OK, we have a MMacro structure together with a set of
4828 * parameters. We must now go through the expansion and push
4829 * copies of each Line on to istk->expansion. Substitution of
H. Peter Anvin76690a12002-04-30 20:52:49 +00004830 * parameter tokens and macro-local tokens doesn't get done
4831 * until the single-line macro substitution process; this is
4832 * because delaying them allows us to change the semantics
4833 * later through %rotate.
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004834 *
4835 * First, push an end marker on to istk->expansion, mark this
4836 * macro as in progress, and set up its invocation-specific
4837 * variables.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004838 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004839 ll = nasm_malloc(sizeof(Line));
4840 ll->next = istk->expansion;
4841 ll->finishes = m;
4842 ll->first = NULL;
4843 istk->expansion = ll;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004844
4845 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004846 * Save the previous MMacro expansion in the case of
4847 * macro recursion
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004848 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004849 if (m->max_depth && m->in_progress)
4850 push_mmacro(m);
4851
4852 m->in_progress ++;
4853 m->params = params;
4854 m->iline = tline;
4855 m->nparam = nparam;
4856 m->rotate = 0;
4857 m->paramlen = paramlen;
4858 m->unique = unique++;
4859 m->lineno = 0;
4860 m->condcnt = 0;
4861
4862 m->next_active = istk->mstk;
4863 istk->mstk = m;
4864
4865 list_for_each(l, m->expansion) {
4866 Token **tail;
4867
4868 ll = nasm_malloc(sizeof(Line));
4869 ll->finishes = NULL;
4870 ll->next = istk->expansion;
4871 istk->expansion = ll;
4872 tail = &ll->first;
4873
4874 list_for_each(t, l->first) {
4875 Token *x = t;
4876 switch (t->type) {
4877 case TOK_PREPROC_Q:
4878 tt = *tail = new_Token(NULL, TOK_ID, mname, 0);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004879 break;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004880 case TOK_PREPROC_QQ:
4881 tt = *tail = new_Token(NULL, TOK_ID, m->name, 0);
4882 break;
4883 case TOK_PREPROC_ID:
4884 if (t->text[1] == '0' && t->text[2] == '0') {
4885 dont_prepend = -1;
4886 x = label;
4887 if (!x)
4888 continue;
4889 }
4890 /* fall through */
4891 default:
4892 tt = *tail = new_Token(NULL, x->type, x->text, 0);
4893 break;
4894 }
4895 tail = &tt->next;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004896 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004897 *tail = NULL;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004898 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004899
4900 /*
H. Peter Anvineba20a72002-04-30 20:53:55 +00004901 * If we had a label, push it on as the first line of
4902 * the macro expansion.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004903 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004904 if (label) {
4905 if (dont_prepend < 0)
4906 free_tlist(startline);
4907 else {
4908 ll = nasm_malloc(sizeof(Line));
4909 ll->finishes = NULL;
4910 ll->next = istk->expansion;
4911 istk->expansion = ll;
4912 ll->first = startline;
4913 if (!dont_prepend) {
4914 while (label->next)
4915 label = label->next;
4916 label->next = tt = new_Token(NULL, TOK_OTHER, ":", 0);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004917 }
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004918 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004919 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004920
H. Peter Anvin8ac25aa2016-02-18 01:16:18 -08004921 lfmt->uplevel(m->nolist ? LIST_MACRO_NOLIST : LIST_MACRO);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004922
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004923 return 1;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004924}
4925
H. Peter Anvin130736c2016-02-17 20:27:41 -08004926/*
4927 * This function adds macro names to error messages, and suppresses
4928 * them if necessary.
4929 */
H. Peter Anvin (Intel)6bde2ed2018-12-13 19:39:41 -08004930static void pp_verror(errflags severity, const char *fmt, va_list arg)
Victor van den Elzen3b404c02008-09-18 13:51:36 +02004931{
H. Peter Anvin130736c2016-02-17 20:27:41 -08004932 char buff[BUFSIZ];
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004933 MMacro *mmac = NULL;
4934 int delta = 0;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02004935
H. Peter Anvin130736c2016-02-17 20:27:41 -08004936 /*
4937 * If we're in a dead branch of IF or something like it, ignore the error.
4938 * However, because %else etc are evaluated in the state context
4939 * of the previous branch, errors might get lost:
4940 * %if 0 ... %else trailing garbage ... %endif
4941 * So %else etc should set the ERR_PP_PRECOND flag.
4942 */
4943 if ((severity & ERR_MASK) < ERR_FATAL &&
4944 istk && istk->conds &&
4945 ((severity & ERR_PP_PRECOND) ?
4946 istk->conds->state == COND_NEVER :
H. Peter Anvineb6653f2016-04-05 13:03:10 -07004947 !emitting(istk->conds->state)))
H. Peter Anvin130736c2016-02-17 20:27:41 -08004948 return;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02004949
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004950 /* get %macro name */
H. Peter Anvin130736c2016-02-17 20:27:41 -08004951 if (!(severity & ERR_NOFILE) && istk && istk->mstk) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004952 mmac = istk->mstk;
4953 /* but %rep blocks should be skipped */
4954 while (mmac && !mmac->name)
4955 mmac = mmac->next_active, delta++;
Cyrill Gorcunov9900c6b2011-10-09 18:58:46 +04004956 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004957
H. Peter Anvin130736c2016-02-17 20:27:41 -08004958 if (mmac) {
4959 vsnprintf(buff, sizeof(buff), fmt, arg);
Victor van den Elzen3b404c02008-09-18 13:51:36 +02004960
H. Peter Anvin130736c2016-02-17 20:27:41 -08004961 nasm_set_verror(real_verror);
4962 nasm_error(severity, "(%s:%d) %s",
4963 mmac->name, mmac->lineno - delta, buff);
4964 nasm_set_verror(pp_verror);
4965 } else {
4966 real_verror(severity, fmt, arg);
4967 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004968}
4969
H. Peter Anvin8b262472019-02-26 14:00:54 -08004970static Token *stdmac_file(const SMacro *s, Token **params, int *paramsize)
4971{
4972 (void)s;
4973 (void)params;
4974 (void)paramsize;
4975
4976 return make_tok_qstr(src_get_fname());
4977}
4978
4979static Token *stdmac_line(const SMacro *s, Token **params, int *paramsize)
4980{
4981 (void)s;
4982 (void)params;
4983 (void)paramsize;
4984
4985 return make_tok_num(src_get_linnum());
4986}
4987
4988static Token *stdmac_bits(const SMacro *s, Token **params, int *paramsize)
4989{
4990 (void)s;
4991 (void)params;
4992 (void)paramsize;
4993
4994 return make_tok_num(globalbits);
4995}
4996
4997static Token *stdmac_ptr(const SMacro *s, Token **params, int *paramsize)
4998{
4999 const char *name;
5000
5001 (void)s;
5002 (void)params;
5003 (void)paramsize;
5004
5005 switch (globalbits) {
5006 case 16:
5007 name = "word";
5008 break;
5009 case 32:
5010 name = "dword";
5011 break;
5012 case 64:
5013 name = "qword";
5014 break;
5015 default:
5016 panic();
5017 }
5018 return new_Token(NULL, TOK_ID, name, 0);
5019}
5020
H. Peter Anvin8b262472019-02-26 14:00:54 -08005021/* Add magic standard macros */
5022struct magic_macros {
5023 const char *name;
5024 int nparams;
5025 Token *(*func)(const SMacro *s, Token **params, int *paramsize);
5026};
5027static const struct magic_macros magic_macros[] =
5028{
5029 { "__FILE__", 0, stdmac_file },
5030 { "__LINE__", 0, stdmac_line },
5031 { "__BITS__", 0, stdmac_bits },
5032 { "__PTR__", 0, stdmac_ptr },
H. Peter Anvin8b262472019-02-26 14:00:54 -08005033 { NULL, 0, NULL }
5034};
5035
5036static void pp_add_magic_stdmac(void)
5037{
5038 const struct magic_macros *m;
5039 SMacro *s;
5040
5041 for (m = magic_macros; m->name; m++) {
5042 s = define_smacro(NULL, m->name, true, m->nparams, NULL);
5043 s->magic = true;
5044 s->e.magic = m->func;
5045 }
5046}
5047
H. Peter Anvin734b1882002-04-30 21:01:08 +00005048static void
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08005049pp_reset(const char *file, enum preproc_mode mode, struct strlist *dep_list)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005050{
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08005051 int apass;
H. Peter Anvin7383b402008-09-24 10:20:40 -07005052
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005053 cstk = NULL;
H. Peter Anvin8b262472019-02-26 14:00:54 -08005054 nasm_new(istk);
H. Peter Anvin3e83cec2016-05-25 04:28:46 -07005055 istk->fp = nasm_open_read(file, NF_TEXT);
H. Peter Anvin274cda82016-05-10 02:56:29 -07005056 src_set(0, file);
H. Peter Anvineba20a72002-04-30 20:53:55 +00005057 istk->lineinc = 1;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005058 if (!istk->fp)
Cyrill Gorcunovc3527dd2018-11-24 22:17:47 +03005059 nasm_fatalf(ERR_NOFILE, "unable to open input file `%s'", file);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005060 defining = NULL;
Charles Crayned4200be2008-07-12 16:42:33 -07005061 nested_mac_count = 0;
5062 nested_rep_count = 0;
H. Peter Anvin97a23472007-09-16 17:57:25 -07005063 init_macros();
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005064 unique = 0;
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07005065 deplist = dep_list;
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08005066 pp_mode = mode;
H. Peter Anvinf7606612016-07-13 14:23:48 -07005067
H. Peter Anvin8b262472019-02-26 14:00:54 -08005068 pp_add_magic_stdmac();
5069
H. Peter Anvinf7606612016-07-13 14:23:48 -07005070 if (tasm_compatible_mode)
5071 pp_add_stdmac(nasm_stdmac_tasm);
5072
5073 pp_add_stdmac(nasm_stdmac_nasm);
5074 pp_add_stdmac(nasm_stdmac_version);
5075
Cyrill Gorcunov15ce78f2017-01-06 20:21:28 +03005076 if (extrastdmac)
5077 pp_add_stdmac(extrastdmac);
5078
H. Peter Anvinf7606612016-07-13 14:23:48 -07005079 stdmacpos = stdmacros[0];
5080 stdmacnext = &stdmacros[1];
5081
H. Peter Anvind2456592008-06-19 15:04:18 -07005082 do_predef = true;
H. Peter Anvin61f130f2008-09-25 15:45:06 -07005083
Cyrill Gorcunovb7bb5ac2018-11-11 21:33:52 +03005084 strlist_add(deplist, file);
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07005085
H. Peter Anvin61f130f2008-09-25 15:45:06 -07005086 /*
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07005087 * Define the __PASS__ macro. This is defined here unlike all the
5088 * other builtins, because it is special -- it varies between
5089 * passes -- but there is really no particular reason to make it
5090 * magic.
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08005091 *
5092 * 0 = dependencies only
5093 * 1 = preparatory passes
5094 * 2 = final pass
5095 * 3 = preproces only
H. Peter Anvin61f130f2008-09-25 15:45:06 -07005096 */
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08005097 switch (mode) {
5098 case PP_NORMAL:
5099 apass = pass_final() ? 2 : 1;
5100 break;
5101 case PP_DEPS:
5102 apass = 0;
5103 break;
5104 case PP_PREPROC:
5105 apass = 3;
5106 break;
5107 default:
5108 panic();
5109 }
5110
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07005111 define_smacro(NULL, "__PASS__", true, 0, make_tok_num(apass));
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005112}
5113
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07005114static void pp_init(void)
5115{
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07005116}
5117
Keith Kaniosa6dfa782007-04-13 16:47:53 +00005118static char *pp_getline(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005119{
Keith Kaniosa6dfa782007-04-13 16:47:53 +00005120 char *line;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005121 Token *tline;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005122
H. Peter Anvin130736c2016-02-17 20:27:41 -08005123 real_verror = nasm_set_verror(pp_verror);
H. Peter Anvin215186f2016-02-17 20:27:41 -08005124
H. Peter Anvine2c80182005-01-15 22:15:51 +00005125 while (1) {
5126 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005127 * Fetch a tokenized line, either from the macro-expansion
H. Peter Anvine2c80182005-01-15 22:15:51 +00005128 * buffer or from the input file.
5129 */
5130 tline = NULL;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005131 while (istk->expansion && istk->expansion->finishes) {
5132 Line *l = istk->expansion;
5133 if (!l->finishes->name && l->finishes->in_progress > 1) {
5134 Line *ll;
H. Peter Anvineba20a72002-04-30 20:53:55 +00005135
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005136 /*
5137 * This is a macro-end marker for a macro with no
5138 * name, which means it's not really a macro at all
5139 * but a %rep block, and the `in_progress' field is
5140 * more than 1, meaning that we still need to
5141 * repeat. (1 means the natural last repetition; 0
5142 * means termination by %exitrep.) We have
5143 * therefore expanded up to the %endrep, and must
5144 * push the whole block on to the expansion buffer
5145 * again. We don't bother to remove the macro-end
5146 * marker: we'd only have to generate another one
5147 * if we did.
5148 */
5149 l->finishes->in_progress--;
5150 list_for_each(l, l->finishes->expansion) {
5151 Token *t, *tt, **tail;
5152
5153 ll = nasm_malloc(sizeof(Line));
5154 ll->next = istk->expansion;
5155 ll->finishes = NULL;
5156 ll->first = NULL;
5157 tail = &ll->first;
5158
5159 list_for_each(t, l->first) {
5160 if (t->text || t->type == TOK_WHITESPACE) {
5161 tt = *tail = new_Token(NULL, t->type, t->text, 0);
5162 tail = &tt->next;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005163 }
5164 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005165
5166 istk->expansion = ll;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005167 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005168 } else {
5169 /*
5170 * Check whether a `%rep' was started and not ended
5171 * within this macro expansion. This can happen and
5172 * should be detected. It's a fatal error because
5173 * I'm too confused to work out how to recover
5174 * sensibly from it.
5175 */
5176 if (defining) {
5177 if (defining->name)
H. Peter Anvinc5136902018-06-15 18:20:17 -07005178 nasm_panic("defining with name in expansion");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005179 else if (istk->mstk->name)
H. Peter Anvinc5136902018-06-15 18:20:17 -07005180 nasm_fatal("`%%rep' without `%%endrep' within"
H. Peter Anvin130736c2016-02-17 20:27:41 -08005181 " expansion of macro `%s'",
5182 istk->mstk->name);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005183 }
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005184
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005185 /*
5186 * FIXME: investigate the relationship at this point between
5187 * istk->mstk and l->finishes
5188 */
5189 {
5190 MMacro *m = istk->mstk;
5191 istk->mstk = m->next_active;
5192 if (m->name) {
5193 /*
5194 * This was a real macro call, not a %rep, and
5195 * therefore the parameter information needs to
5196 * be freed.
5197 */
5198 if (m->prev) {
5199 pop_mmacro(m);
5200 l->finishes->in_progress --;
5201 } else {
5202 nasm_free(m->params);
5203 free_tlist(m->iline);
5204 nasm_free(m->paramlen);
5205 l->finishes->in_progress = 0;
5206 }
Adam Majer91e72402017-07-25 10:42:01 +02005207 }
5208
5209 /*
5210 * FIXME It is incorrect to always free_mmacro here.
5211 * It leads to usage-after-free.
5212 *
5213 * https://bugzilla.nasm.us/show_bug.cgi?id=3392414
5214 */
5215#if 0
5216 else
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005217 free_mmacro(m);
Adam Majer91e72402017-07-25 10:42:01 +02005218#endif
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005219 }
5220 istk->expansion = l->next;
5221 nasm_free(l);
H. Peter Anvin8ac25aa2016-02-18 01:16:18 -08005222 lfmt->downlevel(LIST_MACRO);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005223 }
5224 }
5225 while (1) { /* until we get a line we can use */
5226
5227 if (istk->expansion) { /* from a macro expansion */
5228 char *p;
5229 Line *l = istk->expansion;
5230 if (istk->mstk)
5231 istk->mstk->lineno++;
5232 tline = l->first;
5233 istk->expansion = l->next;
5234 nasm_free(l);
5235 p = detoken(tline, false);
H. Peter Anvin8ac25aa2016-02-18 01:16:18 -08005236 lfmt->line(LIST_MACRO, p);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005237 nasm_free(p);
5238 break;
5239 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00005240 line = read_line();
5241 if (line) { /* from the current input file */
5242 line = prepreproc(line);
Keith Kaniosb7a89542007-04-12 02:40:54 +00005243 tline = tokenize(line);
H. Peter Anvine2c80182005-01-15 22:15:51 +00005244 nasm_free(line);
5245 break;
5246 }
5247 /*
5248 * The current file has ended; work down the istk
5249 */
5250 {
5251 Include *i = istk;
5252 fclose(i->fp);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005253 if (i->conds) {
5254 /* nasm_error can't be conditionally suppressed */
H. Peter Anvinc5136902018-06-15 18:20:17 -07005255 nasm_fatal("expected `%%endif' before end of file");
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005256 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00005257 /* only set line and file name if there's a next node */
H. Peter Anvin274cda82016-05-10 02:56:29 -07005258 if (i->next)
5259 src_set(i->lineno, i->fname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00005260 istk = i->next;
H. Peter Anvin8ac25aa2016-02-18 01:16:18 -08005261 lfmt->downlevel(LIST_INCLUDE);
H. Peter Anvine2c80182005-01-15 22:15:51 +00005262 nasm_free(i);
H. Peter Anvin130736c2016-02-17 20:27:41 -08005263 if (!istk) {
5264 line = NULL;
5265 goto done;
5266 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005267 if (istk->expansion && istk->expansion->finishes)
5268 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005269 }
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005270 }
5271
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005272 /*
5273 * We must expand MMacro parameters and MMacro-local labels
5274 * _before_ we plunge into directive processing, to cope
5275 * with things like `%define something %1' such as STRUC
5276 * uses. Unless we're _defining_ a MMacro, in which case
5277 * those tokens should be left alone to go into the
5278 * definition; and unless we're in a non-emitting
5279 * condition, in which case we don't want to meddle with
5280 * anything.
5281 */
5282 if (!defining && !(istk->conds && !emitting(istk->conds->state))
5283 && !(istk->mstk && !istk->mstk->in_progress)) {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005284 tline = expand_mmac_params(tline);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005285 }
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005286
H. Peter Anvine2c80182005-01-15 22:15:51 +00005287 /*
5288 * Check the line to see if it's a preprocessor directive.
5289 */
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07005290 if (do_directive(tline, &line) == DIRECTIVE_FOUND) {
5291 if (line)
5292 break; /* Directive generated output */
5293 else
5294 continue;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005295 } else if (defining) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00005296 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005297 * We're defining a multi-line macro. We emit nothing
5298 * at all, and just
5299 * shove the tokenized line on to the macro definition.
H. Peter Anvine2c80182005-01-15 22:15:51 +00005300 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005301 Line *l = nasm_malloc(sizeof(Line));
5302 l->next = defining->expansion;
5303 l->first = tline;
5304 l->finishes = NULL;
5305 defining->expansion = l;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005306 continue;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005307 } else if (istk->conds && !emitting(istk->conds->state)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00005308 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005309 * We're in a non-emitting branch of a condition block.
H. Peter Anvine2c80182005-01-15 22:15:51 +00005310 * Emit nothing at all, not even a blank line: when we
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005311 * emerge from the condition we'll give a line-number
H. Peter Anvine2c80182005-01-15 22:15:51 +00005312 * directive so we keep our place correctly.
5313 */
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005314 free_tlist(tline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00005315 continue;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005316 } else if (istk->mstk && !istk->mstk->in_progress) {
5317 /*
5318 * We're in a %rep block which has been terminated, so
5319 * we're walking through to the %endrep without
5320 * emitting anything. Emit nothing at all, not even a
5321 * blank line: when we emerge from the %rep block we'll
5322 * give a line-number directive so we keep our place
5323 * correctly.
5324 */
5325 free_tlist(tline);
5326 continue;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005327 } else {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005328 tline = expand_smacro(tline);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005329 if (!expand_mmacro(tline)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00005330 /*
Keith Kaniosb7a89542007-04-12 02:40:54 +00005331 * De-tokenize the line again, and emit it.
H. Peter Anvine2c80182005-01-15 22:15:51 +00005332 */
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005333 line = detoken(tline, true);
5334 free_tlist(tline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00005335 break;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005336 } else {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005337 continue; /* expand_mmacro calls free_tlist */
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005338 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00005339 }
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005340 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005341
H. Peter Anvin130736c2016-02-17 20:27:41 -08005342done:
5343 nasm_set_verror(real_verror);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005344 return line;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005345}
5346
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08005347static void pp_cleanup_pass(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005348{
H. Peter Anvin130736c2016-02-17 20:27:41 -08005349 real_verror = nasm_set_verror(pp_verror);
H. Peter Anvin215186f2016-02-17 20:27:41 -08005350
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005351 if (defining) {
5352 if (defining->name) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03005353 nasm_nonfatal("end of file while still defining macro `%s'",
5354 defining->name);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005355 } else {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03005356 nasm_nonfatal("end of file while still in %%rep");
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005357 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005358
5359 free_mmacro(defining);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005360 defining = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005361 }
H. Peter Anvin130736c2016-02-17 20:27:41 -08005362
5363 nasm_set_verror(real_verror);
H. Peter Anvin215186f2016-02-17 20:27:41 -08005364
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005365 while (cstk)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005366 ctx_pop();
H. Peter Anvin97a23472007-09-16 17:57:25 -07005367 free_macros();
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005368 while (istk) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00005369 Include *i = istk;
5370 istk = istk->next;
5371 fclose(i->fp);
Cyrill Gorcunov8dcfd882011-03-03 09:18:56 +03005372 nasm_free(i);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005373 }
5374 while (cstk)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005375 ctx_pop();
H. Peter Anvin274cda82016-05-10 02:56:29 -07005376 src_set_fname(NULL);
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08005377}
5378
5379static void pp_cleanup_session(void)
5380{
5381 free_llist(predef);
5382 predef = NULL;
5383 delete_Blocks();
5384 freeTokens = NULL;
5385 ipath_list = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005386}
5387
Cyrill Gorcunov8c0666b2018-11-24 14:33:48 +03005388static void pp_include_path(struct strlist *list)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005389{
Cyrill Gorcunov8c0666b2018-11-24 14:33:48 +03005390 ipath_list = list;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005391}
Frank Kotlerd0ed6fd2003-08-27 11:33:56 +00005392
Cyrill Gorcunov0b78bff2012-05-07 01:57:55 +04005393static void pp_pre_include(char *fname)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005394{
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005395 Token *inc, *space, *name;
5396 Line *l;
5397
H. Peter Anvin734b1882002-04-30 21:01:08 +00005398 name = new_Token(NULL, TOK_INTERNAL_STRING, fname, 0);
5399 space = new_Token(name, TOK_WHITESPACE, NULL, 0);
5400 inc = new_Token(space, TOK_PREPROC_ID, "%include", 0);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005401
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005402 l = nasm_malloc(sizeof(Line));
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005403 l->next = predef;
5404 l->first = inc;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005405 l->finishes = NULL;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005406 predef = l;
5407}
5408
Cyrill Gorcunov0b78bff2012-05-07 01:57:55 +04005409static void pp_pre_define(char *definition)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005410{
5411 Token *def, *space;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005412 Line *l;
Keith Kaniosa6dfa782007-04-13 16:47:53 +00005413 char *equals;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005414
H. Peter Anvin130736c2016-02-17 20:27:41 -08005415 real_verror = nasm_set_verror(pp_verror);
H. Peter Anvin215186f2016-02-17 20:27:41 -08005416
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005417 equals = strchr(definition, '=');
H. Peter Anvin734b1882002-04-30 21:01:08 +00005418 space = new_Token(NULL, TOK_WHITESPACE, NULL, 0);
5419 def = new_Token(space, TOK_PREPROC_ID, "%define", 0);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005420 if (equals)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005421 *equals = ' ';
Keith Kaniosb7a89542007-04-12 02:40:54 +00005422 space->next = tokenize(definition);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005423 if (equals)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005424 *equals = '=';
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005425
Cyrill Gorcunov6d42e9b2015-02-08 11:07:17 +03005426 if (space->next->type != TOK_PREPROC_ID &&
5427 space->next->type != TOK_ID)
H. Peter Anvin (Intel)80c4f232018-12-14 13:33:24 -08005428 nasm_warn(WARN_OTHER, "pre-defining non ID `%s\'\n", definition);
Cyrill Gorcunov6d42e9b2015-02-08 11:07:17 +03005429
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005430 l = nasm_malloc(sizeof(Line));
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005431 l->next = predef;
5432 l->first = def;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005433 l->finishes = NULL;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005434 predef = l;
H. Peter Anvin130736c2016-02-17 20:27:41 -08005435
5436 nasm_set_verror(real_verror);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005437}
5438
Cyrill Gorcunov0b78bff2012-05-07 01:57:55 +04005439static void pp_pre_undefine(char *definition)
H. Peter Anvin620515a2002-04-30 20:57:38 +00005440{
5441 Token *def, *space;
5442 Line *l;
5443
H. Peter Anvin734b1882002-04-30 21:01:08 +00005444 space = new_Token(NULL, TOK_WHITESPACE, NULL, 0);
5445 def = new_Token(space, TOK_PREPROC_ID, "%undef", 0);
Keith Kaniosb7a89542007-04-12 02:40:54 +00005446 space->next = tokenize(definition);
H. Peter Anvin620515a2002-04-30 20:57:38 +00005447
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005448 l = nasm_malloc(sizeof(Line));
H. Peter Anvin620515a2002-04-30 20:57:38 +00005449 l->next = predef;
5450 l->first = def;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005451 l->finishes = NULL;
H. Peter Anvin620515a2002-04-30 20:57:38 +00005452 predef = l;
5453}
5454
H. Peter Anvin05990342018-06-11 13:32:42 -07005455/* Insert an early preprocessor command that doesn't need special handling */
5456static void pp_pre_command(const char *what, char *string)
5457{
5458 char *cmd;
5459 Token *def, *space;
5460 Line *l;
5461
5462 def = tokenize(string);
5463 if (what) {
5464 cmd = nasm_strcat(what[0] == '%' ? "" : "%", what);
5465 space = new_Token(def, TOK_WHITESPACE, NULL, 0);
5466 def = new_Token(space, TOK_PREPROC_ID, cmd, 0);
5467 }
5468
5469 l = nasm_malloc(sizeof(Line));
5470 l->next = predef;
5471 l->first = def;
5472 l->finishes = NULL;
5473 predef = l;
5474}
5475
H. Peter Anvinf7606612016-07-13 14:23:48 -07005476static void pp_add_stdmac(macros_t *macros)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005477{
H. Peter Anvinf7606612016-07-13 14:23:48 -07005478 macros_t **mp;
5479
5480 /* Find the end of the list and avoid duplicates */
5481 for (mp = stdmacros; *mp; mp++) {
5482 if (*mp == macros)
5483 return; /* Nothing to do */
5484 }
5485
5486 nasm_assert(mp < &stdmacros[ARRAY_SIZE(stdmacros)-1]);
5487
5488 *mp = macros;
H. Peter Anvin76690a12002-04-30 20:52:49 +00005489}
5490
Cyrill Gorcunov15ce78f2017-01-06 20:21:28 +03005491static void pp_extra_stdmac(macros_t *macros)
5492{
5493 extrastdmac = macros;
5494}
5495
H. Peter Anvin8b262472019-02-26 14:00:54 -08005496static Token *make_tok_num(int64_t val)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005497{
Cyrill Gorcunovce652742013-05-06 23:43:43 +04005498 char numbuf[32];
H. Peter Anvin8b262472019-02-26 14:00:54 -08005499 int len = snprintf(numbuf, sizeof(numbuf), "%"PRId64"", val);
5500 return new_Token(NULL, TOK_NUMBER, numbuf, len);
5501}
5502
5503static Token *make_tok_qstr(const char *str)
5504{
5505 Token *t = new_Token(NULL, TOK_STRING, NULL, 0);
5506 t->text = nasm_quote_cstr(str);
5507 return t;
H. Peter Anvineba20a72002-04-30 20:53:55 +00005508}
5509
H. Peter Anvin (Intel)6bde2ed2018-12-13 19:39:41 -08005510static void pp_list_one_macro(MMacro *m, errflags severity)
H. Peter Anvin37368952016-05-09 14:10:32 -07005511{
5512 if (!m)
5513 return;
5514
5515 /* We need to print the next_active list in reverse order */
5516 pp_list_one_macro(m->next_active, severity);
5517
5518 if (m->name && !m->nolist) {
H. Peter Anvin274cda82016-05-10 02:56:29 -07005519 src_set(m->xline + m->lineno, m->fname);
H. Peter Anvinddb29062018-12-11 00:06:29 -08005520 nasm_error(severity, "... from macro `%s' defined", m->name);
H. Peter Anvin37368952016-05-09 14:10:32 -07005521 }
5522}
5523
H. Peter Anvin (Intel)6bde2ed2018-12-13 19:39:41 -08005524static void pp_error_list_macros(errflags severity)
H. Peter Anvin4def1a82016-05-09 13:59:44 -07005525{
H. Peter Anvinddb29062018-12-11 00:06:29 -08005526 struct src_location saved;
H. Peter Anvin4def1a82016-05-09 13:59:44 -07005527
H. Peter Anvinddb29062018-12-11 00:06:29 -08005528 severity |= ERR_PP_LISTMACRO | ERR_NO_SEVERITY | ERR_HERE;
5529 saved = src_where();
H. Peter Anvin4def1a82016-05-09 13:59:44 -07005530
Cyrill Gorcunov771d04e2016-05-10 23:27:03 +03005531 if (istk)
5532 pp_list_one_macro(istk->mstk, severity);
H. Peter Anvin4def1a82016-05-09 13:59:44 -07005533
H. Peter Anvinddb29062018-12-11 00:06:29 -08005534 src_update(saved);
H. Peter Anvin4def1a82016-05-09 13:59:44 -07005535}
5536
H. Peter Anvine7469712016-02-18 02:20:59 -08005537const struct preproc_ops nasmpp = {
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07005538 pp_init,
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005539 pp_reset,
5540 pp_getline,
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08005541 pp_cleanup_pass,
5542 pp_cleanup_session,
Cyrill Gorcunov15ce78f2017-01-06 20:21:28 +03005543 pp_extra_stdmac,
Cyrill Gorcunov0b78bff2012-05-07 01:57:55 +04005544 pp_pre_define,
5545 pp_pre_undefine,
5546 pp_pre_include,
H. Peter Anvin05990342018-06-11 13:32:42 -07005547 pp_pre_command,
H. Peter Anvin4def1a82016-05-09 13:59:44 -07005548 pp_include_path,
5549 pp_error_list_macros,
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005550};