blob: 741430f2b7a6b17071315c0c59bee60753c16005 [file] [log] [blame]
H. Peter Anvin9e6747c2009-06-28 17:13:04 -07001/* ----------------------------------------------------------------------- *
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002 *
H. Peter Anvin05990342018-06-11 13:32:42 -07003 * Copyright 1996-2018 The NASM Authors - All Rights Reserved
H. Peter Anvin9e6747c2009-06-28 17:13:04 -07004 * See the file AUTHORS included with the NASM distribution for
5 * the specific copyright holders.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00006 *
H. Peter Anvin9e6747c2009-06-28 17:13:04 -07007 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following
9 * conditions are met:
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000010 *
H. Peter Anvin9e6747c2009-06-28 17:13:04 -070011 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above
14 * copyright notice, this list of conditions and the following
15 * disclaimer in the documentation and/or other materials provided
16 * with the distribution.
Cyrill Gorcunovaccda192010-02-16 10:27:56 +030017 *
H. Peter Anvin9e6747c2009-06-28 17:13:04 -070018 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
19 * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
20 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
21 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
23 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
26 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
29 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
30 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 *
32 * ----------------------------------------------------------------------- */
33
34/*
35 * preproc.c macro preprocessor for the Netwide Assembler
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000036 */
37
H. Peter Anvin4836e332002-04-30 20:56:43 +000038/* Typical flow of text through preproc
39 *
Keith Kaniosb7a89542007-04-12 02:40:54 +000040 * pp_getline gets tokenized lines, either
H. Peter Anvin4836e332002-04-30 20:56:43 +000041 *
42 * from a macro expansion
43 *
44 * or
45 * {
46 * read_line gets raw text from stdmacpos, or predef, or current input file
Keith Kaniosb7a89542007-04-12 02:40:54 +000047 * tokenize converts to tokens
H. Peter Anvin4836e332002-04-30 20:56:43 +000048 * }
49 *
50 * expand_mmac_params is used to expand %1 etc., unless a macro is being
51 * defined or a false conditional is being processed
52 * (%0, %1, %+1, %-1, %%foo
53 *
54 * do_directive checks for directives
55 *
56 * expand_smacro is used to expand single line macros
57 *
58 * expand_mmacro is used to expand multi-line macros
59 *
60 * detoken is used to convert the line back to text
61 */
H. Peter Anvineba20a72002-04-30 20:53:55 +000062
H. Peter Anvinfe501952007-10-02 21:53:51 -070063#include "compiler.h"
64
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000065#include <stdio.h>
H. Peter Anvinaf535c12002-04-30 20:59:21 +000066#include <stdarg.h>
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000067#include <stdlib.h>
68#include <stddef.h>
69#include <string.h>
70#include <ctype.h>
H. Peter Anvin76690a12002-04-30 20:52:49 +000071#include <limits.h>
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000072
73#include "nasm.h"
74#include "nasmlib.h"
H. Peter Anvinb20bc732017-03-07 19:23:03 -080075#include "error.h"
H. Peter Anvin4169a472007-09-12 01:29:43 +000076#include "preproc.h"
H. Peter Anvin97a23472007-09-16 17:57:25 -070077#include "hashtbl.h"
H. Peter Anvin8cad14b2008-06-01 17:23:51 -070078#include "quote.h"
H. Peter Anvinc2df2822007-10-24 15:29:28 -070079#include "stdscan.h"
H. Peter Anvindbb640b2009-07-18 18:57:16 -070080#include "eval.h"
H. Peter Anvinc2df2822007-10-24 15:29:28 -070081#include "tokens.h"
H. Peter Anvina4835d42008-05-20 14:21:29 -070082#include "tables.h"
H. Peter Anvin8ac25aa2016-02-18 01:16:18 -080083#include "listing.h"
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000084
85typedef struct SMacro SMacro;
H. Peter Anvin36206cd2012-03-03 16:14:51 -080086typedef struct MMacro MMacro;
87typedef struct MMacroInvocation MMacroInvocation;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000088typedef struct Context Context;
89typedef struct Token Token;
H. Peter Anvince616072002-04-30 21:02:23 +000090typedef struct Blocks Blocks;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000091typedef struct Line Line;
92typedef struct Include Include;
H. Peter Anvin36206cd2012-03-03 16:14:51 -080093typedef struct Cond Cond;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000094
95/*
H. Peter Anvin97a23472007-09-16 17:57:25 -070096 * Note on the storage of both SMacro and MMacros: the hash table
97 * indexes them case-insensitively, and we then have to go through a
98 * linked list of potential case aliases (and, for MMacros, parameter
99 * ranges); this is to preserve the matching semantics of the earlier
100 * code. If the number of case aliases for a specific macro is a
101 * performance issue, you may want to reconsider your coding style.
102 */
103
104/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000105 * Store the definition of a single-line macro.
106 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000107struct SMacro {
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800108 SMacro *next;
109 char *name;
110 bool casesense;
111 bool in_progress;
112 unsigned int nparam;
113 Token *expansion;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000114};
115
116/*
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800117 * Store the definition of a multi-line macro. This is also used to
118 * store the interiors of `%rep...%endrep' blocks, which are
119 * effectively self-re-invoking multi-line macros which simply
120 * don't have a name or bother to appear in the hash tables. %rep
121 * blocks are signified by having a NULL `name' field.
122 *
123 * In a MMacro describing a `%rep' block, the `in_progress' field
124 * isn't merely boolean, but gives the number of repeats left to
125 * run.
126 *
127 * The `next' field is used for storing MMacros in hash tables; the
128 * `next_active' field is for stacking them on istk entries.
129 *
130 * When a MMacro is being expanded, `params', `iline', `nparam',
131 * `paramlen', `rotate' and `unique' are local to the invocation.
132 */
133struct MMacro {
134 MMacro *next;
135 MMacroInvocation *prev; /* previous invocation */
136 char *name;
137 int nparam_min, nparam_max;
138 bool casesense;
139 bool plus; /* is the last parameter greedy? */
140 bool nolist; /* is this macro listing-inhibited? */
141 int64_t in_progress; /* is this macro currently being expanded? */
142 int32_t max_depth; /* maximum number of recursive expansions allowed */
143 Token *dlist; /* All defaults as one list */
144 Token **defaults; /* Parameter default pointers */
145 int ndefs; /* number of default parameters */
146 Line *expansion;
147
148 MMacro *next_active;
149 MMacro *rep_nest; /* used for nesting %rep */
150 Token **params; /* actual parameters */
151 Token *iline; /* invocation line */
152 unsigned int nparam, rotate;
153 int *paramlen;
154 uint64_t unique;
155 int lineno; /* Current line number on expansion */
156 uint64_t condcnt; /* number of if blocks... */
H. Peter Anvin4def1a82016-05-09 13:59:44 -0700157
H. Peter Anvin274cda82016-05-10 02:56:29 -0700158 const char *fname; /* File where defined */
H. Peter Anvin4def1a82016-05-09 13:59:44 -0700159 int32_t xline; /* First line in macro */
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800160};
161
162
163/* Store the definition of a multi-line macro, as defined in a
164 * previous recursive macro expansion.
165 */
166struct MMacroInvocation {
167 MMacroInvocation *prev; /* previous invocation */
168 Token **params; /* actual parameters */
169 Token *iline; /* invocation line */
170 unsigned int nparam, rotate;
171 int *paramlen;
172 uint64_t unique;
173 uint64_t condcnt;
174};
175
176
177/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000178 * The context stack is composed of a linked list of these.
179 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000180struct Context {
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800181 Context *next;
182 char *name;
183 struct hash_table localmac;
184 uint32_t number;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000185};
186
187/*
188 * This is the internal form which we break input lines up into.
189 * Typically stored in linked lists.
190 *
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000191 * Note that `type' serves a double meaning: TOK_SMAC_PARAM is not
192 * necessarily used as-is, but is intended to denote the number of
193 * the substituted parameter. So in the definition
194 *
195 * %define a(x,y) ( (x) & ~(y) )
H. Peter Anvin70653092007-10-19 14:42:29 -0700196 *
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000197 * the token representing `x' will have its type changed to
198 * TOK_SMAC_PARAM, but the one representing `y' will be
199 * TOK_SMAC_PARAM+1.
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000200 *
201 * TOK_INTERNAL_STRING is a dirty hack: it's a single string token
202 * which doesn't need quotes around it. Used in the pre-include
203 * mechanism as an alternative to trying to find a sensible type of
204 * quote to use on the filename we were passed.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000205 */
H. Peter Anvinda10e7b2007-09-12 04:18:37 +0000206enum pp_token_type {
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800207 TOK_NONE = 0, TOK_WHITESPACE, TOK_COMMENT, TOK_ID,
208 TOK_PREPROC_ID, TOK_STRING,
209 TOK_NUMBER, TOK_FLOAT, TOK_SMAC_END, TOK_OTHER,
H. Peter Anvin6c81f0a2008-05-25 21:46:17 -0700210 TOK_INTERNAL_STRING,
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800211 TOK_PREPROC_Q, TOK_PREPROC_QQ,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300212 TOK_PASTE, /* %+ */
213 TOK_INDIRECT, /* %[...] */
214 TOK_SMAC_PARAM, /* MUST BE LAST IN THE LIST!!! */
215 TOK_MAX = INT_MAX /* Keep compiler from reducing the range */
H. Peter Anvinda10e7b2007-09-12 04:18:37 +0000216};
217
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +0400218#define PP_CONCAT_MASK(x) (1 << (x))
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +0400219#define PP_CONCAT_MATCH(t, mask) (PP_CONCAT_MASK((t)->type) & mask)
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +0400220
Cyrill Gorcunov575d4282010-10-06 00:25:55 +0400221struct tokseq_match {
222 int mask_head;
223 int mask_tail;
224};
225
H. Peter Anvine2c80182005-01-15 22:15:51 +0000226struct Token {
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800227 Token *next;
228 char *text;
H. Peter Anvinf26e0972008-07-01 21:26:27 -0700229 union {
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800230 SMacro *mac; /* associated macro for TOK_SMAC_END */
231 size_t len; /* scratch length field */
232 } a; /* Auxiliary data */
233 enum pp_token_type type;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000234};
235
236/*
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800237 * Multi-line macro definitions are stored as a linked list of
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000238 * these, which is essentially a container to allow several linked
239 * lists of Tokens.
H. Peter Anvin70653092007-10-19 14:42:29 -0700240 *
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000241 * Note that in this module, linked lists are treated as stacks
242 * wherever possible. For this reason, Lines are _pushed_ on to the
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800243 * `expansion' field in MMacro structures, so that the linked list,
244 * if walked, would give the macro lines in reverse order; this
245 * means that we can walk the list when expanding a macro, and thus
246 * push the lines on to the `expansion' field in _istk_ in reverse
247 * order (so that when popped back off they are in the right
248 * order). It may seem cockeyed, and it relies on my design having
249 * an even number of steps in, but it works...
250 *
251 * Some of these structures, rather than being actual lines, are
252 * markers delimiting the end of the expansion of a given macro.
253 * This is for use in the cycle-tracking and %rep-handling code.
254 * Such structures have `finishes' non-NULL, and `first' NULL. All
255 * others have `finishes' NULL, but `first' may still be NULL if
256 * the line is blank.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000257 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000258struct Line {
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800259 Line *next;
260 MMacro *finishes;
261 Token *first;
Keith Kaniosb307a4f2010-11-06 17:41:51 -0500262};
263
264/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000265 * To handle an arbitrary level of file inclusion, we maintain a
266 * stack (ie linked list) of these things.
267 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000268struct Include {
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800269 Include *next;
270 FILE *fp;
271 Cond *conds;
272 Line *expansion;
H. Peter Anvin274cda82016-05-10 02:56:29 -0700273 const char *fname;
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800274 int lineno, lineinc;
275 MMacro *mstk; /* stack of active macros/reps */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000276};
277
278/*
H. Peter Anvin169ac7c2016-09-25 17:08:05 -0700279 * File real name hash, so we don't have to re-search the include
280 * path for every pass (and potentially more than that if a file
281 * is used more than once.)
282 */
283struct hash_table FileHash;
284
285/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000286 * Conditional assembly: we maintain a separate stack of these for
287 * each level of file inclusion. (The only reason we keep the
288 * stacks separate is to ensure that a stray `%endif' in a file
289 * included from within the true branch of a `%if' won't terminate
290 * it and cause confusion: instead, rightly, it'll cause an error.)
291 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800292struct Cond {
293 Cond *next;
294 int state;
295};
H. Peter Anvine2c80182005-01-15 22:15:51 +0000296enum {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000297 /*
298 * These states are for use just after %if or %elif: IF_TRUE
299 * means the condition has evaluated to truth so we are
300 * currently emitting, whereas IF_FALSE means we are not
301 * currently emitting but will start doing so if a %else comes
302 * up. In these states, all directives are admissible: %elif,
303 * %else and %endif. (And of course %if.)
304 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800305 COND_IF_TRUE, COND_IF_FALSE,
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000306 /*
307 * These states come up after a %else: ELSE_TRUE means we're
308 * emitting, and ELSE_FALSE means we're not. In ELSE_* states,
309 * any %elif or %else will cause an error.
310 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800311 COND_ELSE_TRUE, COND_ELSE_FALSE,
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000312 /*
Victor van den Elzen3b404c02008-09-18 13:51:36 +0200313 * These states mean that we're not emitting now, and also that
314 * nothing until %endif will be emitted at all. COND_DONE is
315 * used when we've had our moment of emission
316 * and have now started seeing %elifs. COND_NEVER is used when
317 * the condition construct in question is contained within a
318 * non-emitting branch of a larger condition construct,
319 * or if there is an error.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000320 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800321 COND_DONE, COND_NEVER
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000322};
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800323#define emitting(x) ( (x) == COND_IF_TRUE || (x) == COND_ELSE_TRUE )
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000324
H. Peter Anvin70653092007-10-19 14:42:29 -0700325/*
Ed Beroset3ab3f412002-06-11 03:31:49 +0000326 * These defines are used as the possible return values for do_directive
327 */
328#define NO_DIRECTIVE_FOUND 0
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300329#define DIRECTIVE_FOUND 1
Ed Beroset3ab3f412002-06-11 03:31:49 +0000330
Cyrill Gorcunove091d6e2010-08-09 13:58:22 +0400331/* max reps */
332#define REP_LIMIT ((INT64_C(1) << 62))
333
Keith Kanios852f1ee2009-07-12 00:19:55 -0500334/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000335 * Condition codes. Note that we use c_ prefix not C_ because C_ is
336 * used in nasm.h for the "real" condition codes. At _this_ level,
337 * we treat CXZ and ECXZ as condition codes, albeit non-invertible
338 * ones, so we need a different enum...
339 */
H. Peter Anvin476d2862007-10-02 22:04:15 -0700340static const char * const conditions[] = {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000341 "a", "ae", "b", "be", "c", "cxz", "e", "ecxz", "g", "ge", "l", "le",
342 "na", "nae", "nb", "nbe", "nc", "ne", "ng", "nge", "nl", "nle", "no",
H. Peter Anvince9be342007-09-12 00:22:29 +0000343 "np", "ns", "nz", "o", "p", "pe", "po", "rcxz", "s", "z"
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000344};
H. Peter Anvin476d2862007-10-02 22:04:15 -0700345enum pp_conds {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000346 c_A, c_AE, c_B, c_BE, c_C, c_CXZ, c_E, c_ECXZ, c_G, c_GE, c_L, c_LE,
347 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 -0700348 c_NP, c_NS, c_NZ, c_O, c_P, c_PE, c_PO, c_RCXZ, c_S, c_Z,
349 c_none = -1
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000350};
H. Peter Anvin476d2862007-10-02 22:04:15 -0700351static const enum pp_conds inverse_ccs[] = {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000352 c_NA, c_NAE, c_NB, c_NBE, c_NC, -1, c_NE, -1, c_NG, c_NGE, c_NL, c_NLE,
353 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 +0000354 c_Z, c_NO, c_NP, c_PO, c_PE, -1, c_NS, c_NZ
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000355};
356
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800357/*
358 * Directive names.
359 */
360/* If this is a an IF, ELIF, ELSE or ENDIF keyword */
361static int is_condition(enum preproc_token arg)
362{
363 return PP_IS_COND(arg) || (arg == PP_ELSE) || (arg == PP_ENDIF);
364}
365
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000366/* For TASM compatibility we need to be able to recognise TASM compatible
367 * conditional compilation directives. Using the NASM pre-processor does
368 * not work, so we look for them specifically from the following list and
369 * then jam in the equivalent NASM directive into the input stream.
370 */
371
H. Peter Anvine2c80182005-01-15 22:15:51 +0000372enum {
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000373 TM_ARG, TM_ELIF, TM_ELSE, TM_ENDIF, TM_IF, TM_IFDEF, TM_IFDIFI,
374 TM_IFNDEF, TM_INCLUDE, TM_LOCAL
375};
376
H. Peter Anvin476d2862007-10-02 22:04:15 -0700377static const char * const tasm_directives[] = {
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000378 "arg", "elif", "else", "endif", "if", "ifdef", "ifdifi",
379 "ifndef", "include", "local"
380};
381
382static int StackSize = 4;
H. Peter Anvin6c8b2be2016-05-24 23:46:50 -0700383static const char *StackPointer = "ebp";
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000384static int ArgOffset = 8;
H. Peter Anvin8781cb02007-11-08 20:01:11 -0800385static int LocalOffset = 0;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000386
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000387static Context *cstk;
388static Include *istk;
Cyrill Gorcunov8c0666b2018-11-24 14:33:48 +0300389static const struct strlist *ipath_list;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000390
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300391static int pass; /* HACK: pass 0 = generate dependencies only */
Cyrill Gorcunovb7bb5ac2018-11-11 21:33:52 +0300392static struct strlist *deplist;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000393
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300394static uint64_t unique; /* unique identifier numbers */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000395
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800396static Line *predef = NULL;
H. Peter Anvind2456592008-06-19 15:04:18 -0700397static bool do_predef;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000398
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000399/*
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800400 * The current set of multi-line macros we have defined.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000401 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800402static struct hash_table mmacros;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000403
404/*
405 * The current set of single-line macros we have defined.
406 */
H. Peter Anvin166c2472008-05-28 12:28:58 -0700407static struct hash_table smacros;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000408
409/*
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800410 * The multi-line macro we are currently defining, or the %rep
411 * block we are currently reading, if any.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000412 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800413static MMacro *defining;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000414
Charles Crayned4200be2008-07-12 16:42:33 -0700415static uint64_t nested_mac_count;
416static uint64_t nested_rep_count;
417
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000418/*
419 * The number of macro parameters to allocate space for at a time.
420 */
421#define PARAM_DELTA 16
422
423/*
H. Peter Anvinf7606612016-07-13 14:23:48 -0700424 * The standard macro set: defined in macros.c in a set of arrays.
425 * This gives our position in any macro set, while we are processing it.
426 * The stdmacset is an array of such macro sets.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000427 */
H. Peter Anvina70547f2008-07-19 21:44:26 -0700428static macros_t *stdmacpos;
H. Peter Anvinf7606612016-07-13 14:23:48 -0700429static macros_t **stdmacnext;
430static macros_t *stdmacros[8];
Cyrill Gorcunov15ce78f2017-01-06 20:21:28 +0300431static macros_t *extrastdmac;
H. Peter Anvin76690a12002-04-30 20:52:49 +0000432
433/*
H. Peter Anvin734b1882002-04-30 21:01:08 +0000434 * Tokens are allocated in blocks to improve speed
435 */
436#define TOKEN_BLOCKSIZE 4096
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800437static Token *freeTokens = NULL;
H. Peter Anvince616072002-04-30 21:02:23 +0000438struct Blocks {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000439 Blocks *next;
440 void *chunk;
H. Peter Anvince616072002-04-30 21:02:23 +0000441};
442
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800443static Blocks blocks = { NULL, NULL };
H. Peter Anvin734b1882002-04-30 21:01:08 +0000444
445/*
H. Peter Anvin76690a12002-04-30 20:52:49 +0000446 * Forward declarations.
447 */
H. Peter Anvinf7606612016-07-13 14:23:48 -0700448static void pp_add_stdmac(macros_t *macros);
H. Peter Anvin734b1882002-04-30 21:01:08 +0000449static Token *expand_mmac_params(Token * tline);
450static Token *expand_smacro(Token * tline);
451static Token *expand_id(Token * tline);
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +0400452static Context *get_ctx(const char *name, const char **namep);
Keith Kaniosa5fc6462007-10-13 07:09:22 -0700453static void make_tok_num(Token * tok, int64_t val);
H. Peter Anvin (Intel)6bde2ed2018-12-13 19:39:41 -0800454static void pp_verror(errflags severity, const char *fmt, va_list ap);
H. Peter Anvin130736c2016-02-17 20:27:41 -0800455static vefunc real_verror;
H. Peter Anvince616072002-04-30 21:02:23 +0000456static void *new_Block(size_t size);
457static void delete_Blocks(void);
H. Peter Anvinc751e862008-06-09 10:18:45 -0700458static Token *new_Token(Token * next, enum pp_token_type type,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300459 const char *text, int txtlen);
H. Peter Anvin734b1882002-04-30 21:01:08 +0000460static Token *delete_Token(Token * t);
H. Peter Anvineba20a72002-04-30 20:53:55 +0000461
462/*
463 * Macros for safe checking of token pointers, avoid *(NULL)
464 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300465#define tok_type_(x,t) ((x) && (x)->type == (t))
466#define skip_white_(x) if (tok_type_((x), TOK_WHITESPACE)) (x)=(x)->next
467#define tok_is_(x,v) (tok_type_((x), TOK_OTHER) && !strcmp((x)->text,(v)))
468#define tok_isnt_(x,v) ((x) && ((x)->type!=TOK_OTHER || strcmp((x)->text,(v))))
H. Peter Anvin76690a12002-04-30 20:52:49 +0000469
Cyrill Gorcunov194ba892011-06-30 01:16:35 +0400470/*
H. Peter Anvin077fb932010-07-20 14:56:30 -0700471 * nasm_unquote with error if the string contains NUL characters.
472 * If the string contains NUL characters, issue an error and return
473 * the C len, i.e. truncate at the NUL.
474 */
475static size_t nasm_unquote_cstr(char *qstr, enum preproc_token directive)
476{
477 size_t len = nasm_unquote(qstr, NULL);
478 size_t clen = strlen(qstr);
479
480 if (len != clen)
Cyrill Gorcunov295b7952018-11-25 12:55:48 +0300481 nasm_nonfatal("NUL character in `%s' directive",
482 pp_directives[directive]);
H. Peter Anvin077fb932010-07-20 14:56:30 -0700483 return clen;
484}
485
486/*
H. Peter Anvinb40992c2010-09-15 08:57:21 -0700487 * In-place reverse a list of tokens.
488 */
489static Token *reverse_tokens(Token *t)
490{
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800491 Token *prev = NULL;
492 Token *next;
H. Peter Anvinb40992c2010-09-15 08:57:21 -0700493
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800494 while (t) {
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +0400495 next = t->next;
496 t->next = prev;
497 prev = t;
498 t = next;
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800499 }
H. Peter Anvinb40992c2010-09-15 08:57:21 -0700500
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800501 return prev;
H. Peter Anvinb40992c2010-09-15 08:57:21 -0700502}
503
504/*
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300505 * Handle TASM specific directives, which do not contain a % in
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000506 * front of them. We do it here because I could not find any other
507 * place to do it for the moment, and it is a hack (ideally it would
508 * be nice to be able to use the NASM pre-processor to do it).
509 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000510static char *check_tasm_directive(char *line)
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000511{
Keith Kaniosb7a89542007-04-12 02:40:54 +0000512 int32_t i, j, k, m, len;
Cyrill Gorcunovf66ac7d2009-10-12 20:41:13 +0400513 char *p, *q, *oldline, oldchar;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000514
Cyrill Gorcunovf66ac7d2009-10-12 20:41:13 +0400515 p = nasm_skip_spaces(line);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000516
517 /* Binary search for the directive name */
518 i = -1;
Cyrill Gorcunova7319242010-06-03 22:04:36 +0400519 j = ARRAY_SIZE(tasm_directives);
Cyrill Gorcunovf66ac7d2009-10-12 20:41:13 +0400520 q = nasm_skip_word(p);
521 len = q - p;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000522 if (len) {
523 oldchar = p[len];
524 p[len] = 0;
525 while (j - i > 1) {
526 k = (j + i) / 2;
527 m = nasm_stricmp(p, tasm_directives[k]);
528 if (m == 0) {
529 /* We have found a directive, so jam a % in front of it
530 * so that NASM will then recognise it as one if it's own.
531 */
532 p[len] = oldchar;
533 len = strlen(p);
534 oldline = line;
535 line = nasm_malloc(len + 2);
536 line[0] = '%';
537 if (k == TM_IFDIFI) {
H. Peter Anvin18f48792009-06-27 15:56:27 -0700538 /*
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300539 * NASM does not recognise IFDIFI, so we convert
540 * it to %if 0. This is not used in NASM
541 * compatible code, but does need to parse for the
542 * TASM macro package.
H. Peter Anvine2c80182005-01-15 22:15:51 +0000543 */
H. Peter Anvin18f48792009-06-27 15:56:27 -0700544 strcpy(line + 1, "if 0");
H. Peter Anvine2c80182005-01-15 22:15:51 +0000545 } else {
546 memcpy(line + 1, p, len + 1);
547 }
548 nasm_free(oldline);
549 return line;
550 } else if (m < 0) {
551 j = k;
552 } else
553 i = k;
554 }
555 p[len] = oldchar;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000556 }
557 return line;
558}
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000559
H. Peter Anvin76690a12002-04-30 20:52:49 +0000560/*
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000561 * The pre-preprocessing stage... This function translates line
562 * number indications as they emerge from GNU cpp (`# lineno "file"
563 * flags') into NASM preprocessor line number indications (`%line
564 * lineno file').
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000565 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000566static char *prepreproc(char *line)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000567{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000568 int lineno, fnlen;
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000569 char *fname, *oldline;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000570
H. Peter Anvine2c80182005-01-15 22:15:51 +0000571 if (line[0] == '#' && line[1] == ' ') {
572 oldline = line;
573 fname = oldline + 2;
574 lineno = atoi(fname);
575 fname += strspn(fname, "0123456789 ");
576 if (*fname == '"')
577 fname++;
578 fnlen = strcspn(fname, "\"");
579 line = nasm_malloc(20 + fnlen);
580 snprintf(line, 20 + fnlen, "%%line %d %.*s", lineno, fnlen, fname);
581 nasm_free(oldline);
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000582 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000583 if (tasm_compatible_mode)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000584 return check_tasm_directive(line);
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000585 return line;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000586}
587
588/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000589 * Free a linked list of tokens.
590 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000591static void free_tlist(Token * list)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000592{
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +0400593 while (list)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000594 list = delete_Token(list);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000595}
596
597/*
598 * Free a linked list of lines.
599 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000600static void free_llist(Line * list)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000601{
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +0400602 Line *l, *tmp;
603 list_for_each_safe(l, tmp, list) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000604 free_tlist(l->first);
605 nasm_free(l);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000606 }
607}
608
609/*
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800610 * Free an MMacro
H. Peter Anvineba20a72002-04-30 20:53:55 +0000611 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800612static void free_mmacro(MMacro * m)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000613{
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800614 nasm_free(m->name);
615 free_tlist(m->dlist);
616 nasm_free(m->defaults);
617 free_llist(m->expansion);
618 nasm_free(m);
H. Peter Anvineba20a72002-04-30 20:53:55 +0000619}
620
621/*
H. Peter Anvin97a23472007-09-16 17:57:25 -0700622 * Free all currently defined macros, and free the hash tables
623 */
H. Peter Anvin072771e2008-05-22 13:17:51 -0700624static void free_smacro_table(struct hash_table *smt)
H. Peter Anvin97a23472007-09-16 17:57:25 -0700625{
H. Peter Anvin (Intel)ebb05a02018-12-11 12:30:25 -0800626 struct hash_iterator it;
627 const struct hash_node *np;
H. Peter Anvin97a23472007-09-16 17:57:25 -0700628
H. Peter Anvin (Intel)ebb05a02018-12-11 12:30:25 -0800629 hash_for_each(smt, it, np) {
630 SMacro *tmp;
631 SMacro *s = np->data;
632 nasm_free((void *)np->key);
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +0400633 list_for_each_safe(s, tmp, s) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300634 nasm_free(s->name);
635 free_tlist(s->expansion);
636 nasm_free(s);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300637 }
H. Peter Anvin97a23472007-09-16 17:57:25 -0700638 }
H. Peter Anvin072771e2008-05-22 13:17:51 -0700639 hash_free(smt);
640}
641
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800642static void free_mmacro_table(struct hash_table *mmt)
H. Peter Anvin072771e2008-05-22 13:17:51 -0700643{
H. Peter Anvin (Intel)ebb05a02018-12-11 12:30:25 -0800644 struct hash_iterator it;
645 const struct hash_node *np;
H. Peter Anvin97a23472007-09-16 17:57:25 -0700646
H. Peter Anvin (Intel)ebb05a02018-12-11 12:30:25 -0800647 hash_for_each(mmt, it, np) {
648 MMacro *tmp;
649 MMacro *m = np->data;
650 nasm_free((void *)np->key);
651 list_for_each_safe(m, tmp, m)
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800652 free_mmacro(m);
H. Peter Anvin97a23472007-09-16 17:57:25 -0700653 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800654 hash_free(mmt);
H. Peter Anvin072771e2008-05-22 13:17:51 -0700655}
656
657static void free_macros(void)
658{
H. Peter Anvin166c2472008-05-28 12:28:58 -0700659 free_smacro_table(&smacros);
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800660 free_mmacro_table(&mmacros);
H. Peter Anvin97a23472007-09-16 17:57:25 -0700661}
662
663/*
664 * Initialize the hash tables
665 */
666static void init_macros(void)
667{
H. Peter Anvin97a23472007-09-16 17:57:25 -0700668}
669
670/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000671 * Pop the context stack.
672 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000673static void ctx_pop(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000674{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000675 Context *c = cstk;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000676
677 cstk = cstk->next;
H. Peter Anvin166c2472008-05-28 12:28:58 -0700678 free_smacro_table(&c->localmac);
H. Peter Anvin734b1882002-04-30 21:01:08 +0000679 nasm_free(c->name);
680 nasm_free(c);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000681}
682
H. Peter Anvin072771e2008-05-22 13:17:51 -0700683/*
684 * Search for a key in the hash index; adding it if necessary
685 * (in which case we initialize the data pointer to NULL.)
686 */
687static void **
688hash_findi_add(struct hash_table *hash, const char *str)
689{
690 struct hash_insert hi;
691 void **r;
692 char *strx;
H. Peter Anvin (Intel)ebb05a02018-12-11 12:30:25 -0800693 size_t l = strlen(str) + 1;
H. Peter Anvin072771e2008-05-22 13:17:51 -0700694
H. Peter Anvin (Intel)ebb05a02018-12-11 12:30:25 -0800695 r = hash_findib(hash, str, l, &hi);
H. Peter Anvin072771e2008-05-22 13:17:51 -0700696 if (r)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300697 return r;
H. Peter Anvin072771e2008-05-22 13:17:51 -0700698
H. Peter Anvin (Intel)ebb05a02018-12-11 12:30:25 -0800699 strx = nasm_malloc(l); /* Use a more efficient allocator here? */
700 memcpy(strx, str, l);
H. Peter Anvin072771e2008-05-22 13:17:51 -0700701 return hash_add(&hi, strx, NULL);
702}
703
704/*
705 * Like hash_findi, but returns the data element rather than a pointer
706 * to it. Used only when not adding a new element, hence no third
707 * argument.
708 */
709static void *
710hash_findix(struct hash_table *hash, const char *str)
711{
712 void **p;
713
714 p = hash_findi(hash, str, NULL);
715 return p ? *p : NULL;
716}
717
Cyrill Gorcunov15bdc512010-07-13 11:27:41 +0400718/*
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800719 * read line from standart macros set,
Cyrill Gorcunov15bdc512010-07-13 11:27:41 +0400720 * if there no more left -- return NULL
721 */
722static char *line_from_stdmac(void)
723{
724 unsigned char c;
725 const unsigned char *p = stdmacpos;
726 char *line, *q;
727 size_t len = 0;
728
729 if (!stdmacpos)
730 return NULL;
731
732 while ((c = *p++)) {
733 if (c >= 0x80)
734 len += pp_directives_len[c - 0x80] + 1;
735 else
736 len++;
737 }
738
739 line = nasm_malloc(len + 1);
740 q = line;
741 while ((c = *stdmacpos++)) {
742 if (c >= 0x80) {
743 memcpy(q, pp_directives[c - 0x80], pp_directives_len[c - 0x80]);
744 q += pp_directives_len[c - 0x80];
745 *q++ = ' ';
746 } else {
747 *q++ = c;
748 }
749 }
750 stdmacpos = p;
751 *q = '\0';
752
753 if (!*stdmacpos) {
H. Peter Anvinf7606612016-07-13 14:23:48 -0700754 /* This was the last of this particular macro set */
Cyrill Gorcunov15bdc512010-07-13 11:27:41 +0400755 stdmacpos = NULL;
H. Peter Anvinf7606612016-07-13 14:23:48 -0700756 if (*stdmacnext) {
757 stdmacpos = *stdmacnext++;
Cyrill Gorcunov15bdc512010-07-13 11:27:41 +0400758 } else if (do_predef) {
759 Line *pd, *l;
760 Token *head, **tail, *t;
761
762 /*
763 * Nasty hack: here we push the contents of
764 * `predef' on to the top-level expansion stack,
765 * since this is the most convenient way to
766 * implement the pre-include and pre-define
767 * features.
768 */
769 list_for_each(pd, predef) {
770 head = NULL;
771 tail = &head;
772 list_for_each(t, pd->first) {
773 *tail = new_Token(NULL, t->type, t->text, 0);
774 tail = &(*tail)->next;
775 }
776
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800777 l = nasm_malloc(sizeof(Line));
778 l->next = istk->expansion;
779 l->first = head;
780 l->finishes = NULL;
781
782 istk->expansion = l;
Cyrill Gorcunov15bdc512010-07-13 11:27:41 +0400783 }
784 do_predef = false;
785 }
786 }
787
788 return line;
789}
790
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000791static char *read_line(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000792{
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +0400793 unsigned int size, c, next;
794 const unsigned int delta = 512;
795 const unsigned int pad = 8;
796 unsigned int nr_cont = 0;
797 bool cont = false;
798 char *buffer, *p;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000799
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +0400800 /* Standart macros set (predefined) goes first */
Cyrill Gorcunov15bdc512010-07-13 11:27:41 +0400801 p = line_from_stdmac();
802 if (p)
803 return p;
H. Peter Anvin72edbb82008-06-19 16:00:04 -0700804
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +0400805 size = delta;
806 p = buffer = nasm_malloc(size);
807
808 for (;;) {
809 c = fgetc(istk->fp);
810 if ((int)(c) == EOF) {
811 p[0] = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000812 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000813 }
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +0400814
815 switch (c) {
816 case '\r':
817 next = fgetc(istk->fp);
818 if (next != '\n')
819 ungetc(next, istk->fp);
820 if (cont) {
821 cont = false;
822 continue;
823 }
824 break;
825
826 case '\n':
827 if (cont) {
828 cont = false;
829 continue;
830 }
831 break;
832
833 case '\\':
834 next = fgetc(istk->fp);
835 ungetc(next, istk->fp);
Cyrill Gorcunov490f85e2012-12-27 20:02:17 +0400836 if (next == '\r' || next == '\n') {
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +0400837 cont = true;
838 nr_cont++;
839 continue;
840 }
841 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000842 }
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +0400843
844 if (c == '\r' || c == '\n') {
845 *p++ = 0;
846 break;
847 }
848
849 if (p >= (buffer + size - pad)) {
850 buffer = nasm_realloc(buffer, size + delta);
851 p = buffer + size - pad;
852 size += delta;
853 }
854
855 *p++ = (unsigned char)c;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000856 }
857
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +0400858 if (p == buffer) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000859 nasm_free(buffer);
860 return NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000861 }
862
Cyrill Gorcunova5aea572010-11-11 10:14:45 +0300863 src_set_linnum(src_get_linnum() + istk->lineinc +
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +0400864 (nr_cont * istk->lineinc));
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000865
866 /*
867 * Handle spurious ^Z, which may be inserted into source files
868 * by some file transfer utilities.
869 */
870 buffer[strcspn(buffer, "\032")] = '\0';
871
H. Peter Anvin8ac25aa2016-02-18 01:16:18 -0800872 lfmt->line(LIST_READ, buffer);
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000873
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000874 return buffer;
875}
876
877/*
Keith Kaniosb7a89542007-04-12 02:40:54 +0000878 * Tokenize a line of text. This is a very simple process since we
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000879 * don't need to parse the value out of e.g. numeric tokens: we
880 * simply split one string into many.
881 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000882static Token *tokenize(char *line)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000883{
H. Peter Anvinca544db2008-10-19 19:30:11 -0700884 char c, *p = line;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +0000885 enum pp_token_type type;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000886 Token *list = NULL;
887 Token *t, **tail = &list;
888
H. Peter Anvine2c80182005-01-15 22:15:51 +0000889 while (*line) {
890 p = line;
891 if (*p == '%') {
892 p++;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300893 if (*p == '+' && !nasm_isdigit(p[1])) {
894 p++;
895 type = TOK_PASTE;
896 } else if (nasm_isdigit(*p) ||
897 ((*p == '-' || *p == '+') && nasm_isdigit(p[1]))) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000898 do {
899 p++;
900 }
H. Peter Anvinbda7a6e2008-06-21 10:23:17 -0700901 while (nasm_isdigit(*p));
H. Peter Anvine2c80182005-01-15 22:15:51 +0000902 type = TOK_PREPROC_ID;
903 } else if (*p == '{') {
904 p++;
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800905 while (*p) {
906 if (*p == '}')
907 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000908 p[-1] = *p;
909 p++;
910 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800911 if (*p != '}')
Cyrill Gorcunov295b7952018-11-25 12:55:48 +0300912 nasm_warnf(ERR_PASS1, "unterminated %%{ construct");
H. Peter Anvine2c80182005-01-15 22:15:51 +0000913 p[-1] = '\0';
914 if (*p)
915 p++;
916 type = TOK_PREPROC_ID;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300917 } else if (*p == '[') {
918 int lvl = 1;
919 line += 2; /* Skip the leading %[ */
920 p++;
921 while (lvl && (c = *p++)) {
922 switch (c) {
923 case ']':
924 lvl--;
925 break;
926 case '%':
927 if (*p == '[')
928 lvl++;
929 break;
930 case '\'':
931 case '\"':
932 case '`':
Cyrill Gorcunov3144e842017-10-22 10:50:55 +0300933 p = nasm_skip_string(p - 1);
934 if (*p)
935 p++;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300936 break;
937 default:
938 break;
939 }
940 }
941 p--;
942 if (*p)
943 *p++ = '\0';
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800944 if (lvl)
Cyrill Gorcunov295b7952018-11-25 12:55:48 +0300945 nasm_nonfatalf(ERR_PASS1, "unterminated %%[ construct");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300946 type = TOK_INDIRECT;
947 } else if (*p == '?') {
948 type = TOK_PREPROC_Q; /* %? */
949 p++;
950 if (*p == '?') {
951 type = TOK_PREPROC_QQ; /* %?? */
952 p++;
953 }
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +0400954 } else if (*p == '!') {
955 type = TOK_PREPROC_ID;
956 p++;
H. Peter Anvin13506202018-11-28 14:55:58 -0800957 if (nasm_isidchar(*p)) {
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +0400958 do {
959 p++;
960 }
H. Peter Anvin13506202018-11-28 14:55:58 -0800961 while (nasm_isidchar(*p));
H. Peter Anvin53e2e4c2018-11-28 15:01:40 -0800962 } else if (nasm_isquote(*p)) {
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +0400963 p = nasm_skip_string(p);
964 if (*p)
965 p++;
966 else
Cyrill Gorcunov295b7952018-11-25 12:55:48 +0300967 nasm_nonfatalf(ERR_PASS1, "unterminated %%! string");
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +0400968 } else {
969 /* %! without string or identifier */
970 type = TOK_OTHER; /* Legacy behavior... */
971 }
H. Peter Anvin13506202018-11-28 14:55:58 -0800972 } else if (nasm_isidchar(*p) ||
H. Peter Anvine2c80182005-01-15 22:15:51 +0000973 ((*p == '!' || *p == '%' || *p == '$') &&
H. Peter Anvin13506202018-11-28 14:55:58 -0800974 nasm_isidchar(p[1]))) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000975 do {
976 p++;
977 }
H. Peter Anvin13506202018-11-28 14:55:58 -0800978 while (nasm_isidchar(*p));
H. Peter Anvine2c80182005-01-15 22:15:51 +0000979 type = TOK_PREPROC_ID;
980 } else {
981 type = TOK_OTHER;
982 if (*p == '%')
983 p++;
984 }
H. Peter Anvin13506202018-11-28 14:55:58 -0800985 } else if (nasm_isidstart(*p) || (*p == '$' && nasm_isidstart(p[1]))) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000986 type = TOK_ID;
987 p++;
H. Peter Anvin13506202018-11-28 14:55:58 -0800988 while (*p && nasm_isidchar(*p))
H. Peter Anvine2c80182005-01-15 22:15:51 +0000989 p++;
H. Peter Anvin53e2e4c2018-11-28 15:01:40 -0800990 } else if (nasm_isquote(*p)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000991 /*
992 * A string token.
993 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000994 type = TOK_STRING;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300995 p = nasm_skip_string(p);
Nickolay Yurchenkof3b3ce22003-09-21 20:38:43 +0000996
H. Peter Anvine2c80182005-01-15 22:15:51 +0000997 if (*p) {
998 p++;
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800999 } else {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03001000 nasm_warnf(ERR_PASS1, "unterminated string");
H. Peter Anvine2c80182005-01-15 22:15:51 +00001001 /* Handling unterminated strings by UNV */
1002 /* type = -1; */
1003 }
Victor van den Elzenfb5f2512009-04-17 16:17:59 +02001004 } else if (p[0] == '$' && p[1] == '$') {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001005 type = TOK_OTHER; /* TOKEN_BASE */
Victor van den Elzenfb5f2512009-04-17 16:17:59 +02001006 p += 2;
H. Peter Anvin13506202018-11-28 14:55:58 -08001007 } else if (nasm_isnumstart(*p)) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001008 bool is_hex = false;
1009 bool is_float = false;
1010 bool has_e = false;
1011 char c, *r;
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001012
H. Peter Anvine2c80182005-01-15 22:15:51 +00001013 /*
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001014 * A numeric token.
H. Peter Anvine2c80182005-01-15 22:15:51 +00001015 */
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001016
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001017 if (*p == '$') {
1018 p++;
1019 is_hex = true;
1020 }
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001021
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001022 for (;;) {
1023 c = *p++;
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001024
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001025 if (!is_hex && (c == 'e' || c == 'E')) {
1026 has_e = true;
1027 if (*p == '+' || *p == '-') {
1028 /*
1029 * e can only be followed by +/- if it is either a
1030 * prefixed hex number or a floating-point number
1031 */
1032 p++;
1033 is_float = true;
1034 }
1035 } else if (c == 'H' || c == 'h' || c == 'X' || c == 'x') {
1036 is_hex = true;
1037 } else if (c == 'P' || c == 'p') {
1038 is_float = true;
1039 if (*p == '+' || *p == '-')
1040 p++;
H. Peter Anvin13506202018-11-28 14:55:58 -08001041 } else if (nasm_isnumchar(c))
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001042 ; /* just advance */
1043 else if (c == '.') {
1044 /*
1045 * we need to deal with consequences of the legacy
1046 * parser, like "1.nolist" being two tokens
1047 * (TOK_NUMBER, TOK_ID) here; at least give it
1048 * a shot for now. In the future, we probably need
1049 * a flex-based scanner with proper pattern matching
1050 * to do it as well as it can be done. Nothing in
1051 * the world is going to help the person who wants
1052 * 0x123.p16 interpreted as two tokens, though.
1053 */
1054 r = p;
1055 while (*r == '_')
1056 r++;
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001057
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001058 if (nasm_isdigit(*r) || (is_hex && nasm_isxdigit(*r)) ||
1059 (!is_hex && (*r == 'e' || *r == 'E')) ||
1060 (*r == 'p' || *r == 'P')) {
1061 p = r;
1062 is_float = true;
1063 } else
1064 break; /* Terminate the token */
1065 } else
1066 break;
1067 }
1068 p--; /* Point to first character beyond number */
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001069
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001070 if (p == line+1 && *line == '$') {
1071 type = TOK_OTHER; /* TOKEN_HERE */
1072 } else {
1073 if (has_e && !is_hex) {
1074 /* 1e13 is floating-point, but 1e13h is not */
1075 is_float = true;
1076 }
H. Peter Anvind784a082009-04-20 14:01:18 -07001077
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001078 type = is_float ? TOK_FLOAT : TOK_NUMBER;
1079 }
H. Peter Anvinbda7a6e2008-06-21 10:23:17 -07001080 } else if (nasm_isspace(*p)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001081 type = TOK_WHITESPACE;
Cyrill Gorcunovf66ac7d2009-10-12 20:41:13 +04001082 p = nasm_skip_spaces(p);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001083 /*
1084 * Whitespace just before end-of-line is discarded by
1085 * pretending it's a comment; whitespace just before a
1086 * comment gets lumped into the comment.
1087 */
1088 if (!*p || *p == ';') {
1089 type = TOK_COMMENT;
1090 while (*p)
1091 p++;
1092 }
1093 } else if (*p == ';') {
1094 type = TOK_COMMENT;
1095 while (*p)
1096 p++;
1097 } else {
1098 /*
1099 * Anything else is an operator of some kind. We check
1100 * for all the double-character operators (>>, <<, //,
1101 * %%, <=, >=, ==, !=, <>, &&, ||, ^^), but anything
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001102 * else is a single-character operator.
H. Peter Anvine2c80182005-01-15 22:15:51 +00001103 */
1104 type = TOK_OTHER;
1105 if ((p[0] == '>' && p[1] == '>') ||
1106 (p[0] == '<' && p[1] == '<') ||
1107 (p[0] == '/' && p[1] == '/') ||
1108 (p[0] == '<' && p[1] == '=') ||
1109 (p[0] == '>' && p[1] == '=') ||
1110 (p[0] == '=' && p[1] == '=') ||
1111 (p[0] == '!' && p[1] == '=') ||
1112 (p[0] == '<' && p[1] == '>') ||
1113 (p[0] == '&' && p[1] == '&') ||
1114 (p[0] == '|' && p[1] == '|') ||
1115 (p[0] == '^' && p[1] == '^')) {
1116 p++;
1117 }
1118 p++;
1119 }
Nickolay Yurchenkof3b3ce22003-09-21 20:38:43 +00001120
H. Peter Anvine2c80182005-01-15 22:15:51 +00001121 /* Handling unterminated string by UNV */
1122 /*if (type == -1)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001123 {
1124 *tail = t = new_Token(NULL, TOK_STRING, line, p-line+1);
1125 t->text[p-line] = *line;
1126 tail = &t->next;
1127 }
1128 else */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001129 if (type != TOK_COMMENT) {
1130 *tail = t = new_Token(NULL, type, line, p - line);
1131 tail = &t->next;
1132 }
1133 line = p;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001134 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001135 return list;
1136}
1137
H. Peter Anvince616072002-04-30 21:02:23 +00001138/*
1139 * this function allocates a new managed block of memory and
H. Peter Anvin70653092007-10-19 14:42:29 -07001140 * returns a pointer to the block. The managed blocks are
H. Peter Anvince616072002-04-30 21:02:23 +00001141 * deleted only all at once by the delete_Blocks function.
1142 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001143static void *new_Block(size_t size)
H. Peter Anvince616072002-04-30 21:02:23 +00001144{
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001145 Blocks *b = &blocks;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001146
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001147 /* first, get to the end of the linked list */
1148 while (b->next)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001149 b = b->next;
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001150 /* now allocate the requested chunk */
1151 b->chunk = nasm_malloc(size);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001152
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001153 /* now allocate a new block for the next request */
Cyrill Gorcunovc31767c2014-06-28 02:22:17 +04001154 b->next = nasm_zalloc(sizeof(Blocks));
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001155 return b->chunk;
H. Peter Anvince616072002-04-30 21:02:23 +00001156}
1157
1158/*
1159 * this function deletes all managed blocks of memory
1160 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001161static void delete_Blocks(void)
H. Peter Anvince616072002-04-30 21:02:23 +00001162{
H. Peter Anvine2c80182005-01-15 22:15:51 +00001163 Blocks *a, *b = &blocks;
H. Peter Anvince616072002-04-30 21:02:23 +00001164
H. Peter Anvin70653092007-10-19 14:42:29 -07001165 /*
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001166 * keep in mind that the first block, pointed to by blocks
H. Peter Anvin70653092007-10-19 14:42:29 -07001167 * is a static and not dynamically allocated, so we don't
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001168 * free it.
1169 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001170 while (b) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001171 if (b->chunk)
1172 nasm_free(b->chunk);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001173 a = b;
1174 b = b->next;
1175 if (a != &blocks)
1176 nasm_free(a);
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001177 }
Cyrill Gorcunovdae24d72014-06-28 10:17:39 +04001178 memset(&blocks, 0, sizeof(blocks));
H. Peter Anvine2c80182005-01-15 22:15:51 +00001179}
H. Peter Anvin734b1882002-04-30 21:01:08 +00001180
1181/*
H. Peter Anvin70653092007-10-19 14:42:29 -07001182 * this function creates a new Token and passes a pointer to it
H. Peter Anvin734b1882002-04-30 21:01:08 +00001183 * back to the caller. It sets the type and text elements, and
H. Peter Anvinf26e0972008-07-01 21:26:27 -07001184 * also the a.mac and next elements to NULL.
H. Peter Anvin734b1882002-04-30 21:01:08 +00001185 */
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07001186static Token *new_Token(Token * next, enum pp_token_type type,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001187 const char *text, int txtlen)
H. Peter Anvin734b1882002-04-30 21:01:08 +00001188{
1189 Token *t;
1190 int i;
1191
H. Peter Anvin89cee572009-07-15 09:16:54 -04001192 if (!freeTokens) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001193 freeTokens = (Token *) new_Block(TOKEN_BLOCKSIZE * sizeof(Token));
1194 for (i = 0; i < TOKEN_BLOCKSIZE - 1; i++)
1195 freeTokens[i].next = &freeTokens[i + 1];
1196 freeTokens[i].next = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001197 }
1198 t = freeTokens;
1199 freeTokens = t->next;
1200 t->next = next;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07001201 t->a.mac = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001202 t->type = type;
H. Peter Anvin89cee572009-07-15 09:16:54 -04001203 if (type == TOK_WHITESPACE || !text) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001204 t->text = NULL;
1205 } else {
1206 if (txtlen == 0)
1207 txtlen = strlen(text);
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07001208 t->text = nasm_malloc(txtlen+1);
1209 memcpy(t->text, text, txtlen);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001210 t->text[txtlen] = '\0';
H. Peter Anvin734b1882002-04-30 21:01:08 +00001211 }
1212 return t;
1213}
1214
H. Peter Anvine2c80182005-01-15 22:15:51 +00001215static Token *delete_Token(Token * t)
H. Peter Anvin734b1882002-04-30 21:01:08 +00001216{
1217 Token *next = t->next;
1218 nasm_free(t->text);
H. Peter Anvin788e6c12002-04-30 21:02:01 +00001219 t->next = freeTokens;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001220 freeTokens = t;
1221 return next;
1222}
1223
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001224/*
1225 * Convert a line of tokens back into text.
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001226 * If expand_locals is not zero, identifiers of the form "%$*xxx"
1227 * will be transformed into ..@ctxnum.xxx
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001228 */
H. Peter Anvin9e200162008-06-04 17:23:14 -07001229static char *detoken(Token * tlist, bool expand_locals)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001230{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001231 Token *t;
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001232 char *line, *p;
H. Peter Anvinb4daadc2008-01-21 16:31:57 -08001233 const char *q;
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001234 int len = 0;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001235
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001236 list_for_each(t, tlist) {
Cyrill Gorcunov9b7ee092017-10-22 21:42:59 +03001237 if (t->type == TOK_PREPROC_ID && t->text &&
1238 t->text[0] && t->text[1] == '!') {
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001239 char *v;
1240 char *q = t->text;
H. Peter Anvin077fb932010-07-20 14:56:30 -07001241
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001242 v = t->text + 2;
H. Peter Anvin53e2e4c2018-11-28 15:01:40 -08001243 if (nasm_isquote(*v)) {
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001244 size_t len = nasm_unquote(v, NULL);
1245 size_t clen = strlen(v);
H. Peter Anvin077fb932010-07-20 14:56:30 -07001246
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001247 if (len != clen) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03001248 nasm_nonfatalf(ERR_PASS1, "NUL character in %%! string");
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001249 v = NULL;
1250 }
1251 }
H. Peter Anvin077fb932010-07-20 14:56:30 -07001252
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001253 if (v) {
1254 char *p = getenv(v);
1255 if (!p) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03001256 nasm_nonfatalf(ERR_PASS1, "nonexistent environment variable `%s'", v);
Cyrill Gorcunovbbb7a1a2016-06-19 12:15:24 +03001257 /*
1258 * FIXME We better should investigate if accessing
1259 * ->text[1] without ->text[0] is safe enough.
1260 */
1261 t->text = nasm_zalloc(2);
1262 } else
1263 t->text = nasm_strdup(p);
Cyrill Gorcunov75004872017-07-26 01:21:16 +03001264 nasm_free(q);
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001265 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001266 }
H. Peter Anvin077fb932010-07-20 14:56:30 -07001267
H. Peter Anvine2c80182005-01-15 22:15:51 +00001268 /* Expand local macros here and not during preprocessing */
1269 if (expand_locals &&
1270 t->type == TOK_PREPROC_ID && t->text &&
1271 t->text[0] == '%' && t->text[1] == '$') {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001272 const char *q;
1273 char *p;
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04001274 Context *ctx = get_ctx(t->text, &q);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001275 if (ctx) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001276 char buffer[40];
Keith Kanios93f2e9a2007-04-14 00:10:59 +00001277 snprintf(buffer, sizeof(buffer), "..@%"PRIu32".", ctx->number);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001278 p = nasm_strcat(buffer, q);
1279 nasm_free(t->text);
1280 t->text = p;
1281 }
1282 }
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001283 if (t->type == TOK_WHITESPACE)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001284 len++;
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001285 else if (t->text)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001286 len += strlen(t->text);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001287 }
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001288
H. Peter Anvin734b1882002-04-30 21:01:08 +00001289 p = line = nasm_malloc(len + 1);
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001290
1291 list_for_each(t, tlist) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001292 if (t->type == TOK_WHITESPACE) {
H. Peter Anvinb4daadc2008-01-21 16:31:57 -08001293 *p++ = ' ';
H. Peter Anvine2c80182005-01-15 22:15:51 +00001294 } else if (t->text) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001295 q = t->text;
1296 while (*q)
1297 *p++ = *q++;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001298 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001299 }
1300 *p = '\0';
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001301
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001302 return line;
1303}
1304
1305/*
H. Peter Anvin76690a12002-04-30 20:52:49 +00001306 * A scanner, suitable for use by the expression evaluator, which
1307 * operates on a line of Tokens. Expects a pointer to a pointer to
1308 * the first token in the line to be passed in as its private_data
1309 * field.
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001310 *
1311 * FIX: This really needs to be unified with stdscan.
H. Peter Anvin76690a12002-04-30 20:52:49 +00001312 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001313static int ppscan(void *private_data, struct tokenval *tokval)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001314{
H. Peter Anvin76690a12002-04-30 20:52:49 +00001315 Token **tlineptr = private_data;
1316 Token *tline;
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001317 char ourcopy[MAX_KEYWORD+1], *p, *r, *s;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001318
H. Peter Anvine2c80182005-01-15 22:15:51 +00001319 do {
1320 tline = *tlineptr;
1321 *tlineptr = tline ? tline->next : NULL;
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04001322 } while (tline && (tline->type == TOK_WHITESPACE ||
1323 tline->type == TOK_COMMENT));
H. Peter Anvin76690a12002-04-30 20:52:49 +00001324
1325 if (!tline)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001326 return tokval->t_type = TOKEN_EOS;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001327
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001328 tokval->t_charptr = tline->text;
1329
H. Peter Anvin76690a12002-04-30 20:52:49 +00001330 if (tline->text[0] == '$' && !tline->text[1])
H. Peter Anvine2c80182005-01-15 22:15:51 +00001331 return tokval->t_type = TOKEN_HERE;
H. Peter Anvin7cf897e2002-05-30 21:30:33 +00001332 if (tline->text[0] == '$' && tline->text[1] == '$' && !tline->text[2])
H. Peter Anvine2c80182005-01-15 22:15:51 +00001333 return tokval->t_type = TOKEN_BASE;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001334
H. Peter Anvine2c80182005-01-15 22:15:51 +00001335 if (tline->type == TOK_ID) {
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001336 p = tokval->t_charptr = tline->text;
1337 if (p[0] == '$') {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001338 tokval->t_charptr++;
1339 return tokval->t_type = TOKEN_ID;
1340 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00001341
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001342 for (r = p, s = ourcopy; *r; r++) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001343 if (r >= p+MAX_KEYWORD)
1344 return tokval->t_type = TOKEN_ID; /* Not a keyword */
H. Peter Anvinac8f8fc2008-06-11 15:49:41 -07001345 *s++ = nasm_tolower(*r);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001346 }
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001347 *s = '\0';
1348 /* right, so we have an identifier sitting in temp storage. now,
1349 * is it actually a register or instruction name, or what? */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001350 return nasm_token_hash(ourcopy, tokval);
H. Peter Anvin76690a12002-04-30 20:52:49 +00001351 }
1352
H. Peter Anvine2c80182005-01-15 22:15:51 +00001353 if (tline->type == TOK_NUMBER) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001354 bool rn_error;
1355 tokval->t_integer = readnum(tline->text, &rn_error);
1356 tokval->t_charptr = tline->text;
1357 if (rn_error)
1358 return tokval->t_type = TOKEN_ERRNUM;
1359 else
1360 return tokval->t_type = TOKEN_NUM;
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001361 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00001362
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001363 if (tline->type == TOK_FLOAT) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001364 return tokval->t_type = TOKEN_FLOAT;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001365 }
1366
H. Peter Anvine2c80182005-01-15 22:15:51 +00001367 if (tline->type == TOK_STRING) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001368 char bq, *ep;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001369
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001370 bq = tline->text[0];
H. Peter Anvin11627042008-06-09 20:45:19 -07001371 tokval->t_charptr = tline->text;
1372 tokval->t_inttwo = nasm_unquote(tline->text, &ep);
H. Peter Anvind2456592008-06-19 15:04:18 -07001373
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001374 if (ep[0] != bq || ep[1] != '\0')
1375 return tokval->t_type = TOKEN_ERRSTR;
1376 else
1377 return tokval->t_type = TOKEN_STR;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001378 }
1379
H. Peter Anvine2c80182005-01-15 22:15:51 +00001380 if (tline->type == TOK_OTHER) {
1381 if (!strcmp(tline->text, "<<"))
1382 return tokval->t_type = TOKEN_SHL;
1383 if (!strcmp(tline->text, ">>"))
1384 return tokval->t_type = TOKEN_SHR;
1385 if (!strcmp(tline->text, "//"))
1386 return tokval->t_type = TOKEN_SDIV;
1387 if (!strcmp(tline->text, "%%"))
1388 return tokval->t_type = TOKEN_SMOD;
1389 if (!strcmp(tline->text, "=="))
1390 return tokval->t_type = TOKEN_EQ;
1391 if (!strcmp(tline->text, "<>"))
1392 return tokval->t_type = TOKEN_NE;
1393 if (!strcmp(tline->text, "!="))
1394 return tokval->t_type = TOKEN_NE;
1395 if (!strcmp(tline->text, "<="))
1396 return tokval->t_type = TOKEN_LE;
1397 if (!strcmp(tline->text, ">="))
1398 return tokval->t_type = TOKEN_GE;
1399 if (!strcmp(tline->text, "&&"))
1400 return tokval->t_type = TOKEN_DBL_AND;
1401 if (!strcmp(tline->text, "^^"))
1402 return tokval->t_type = TOKEN_DBL_XOR;
1403 if (!strcmp(tline->text, "||"))
1404 return tokval->t_type = TOKEN_DBL_OR;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001405 }
1406
1407 /*
1408 * We have no other options: just return the first character of
1409 * the token text.
1410 */
1411 return tokval->t_type = tline->text[0];
1412}
1413
1414/*
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001415 * Compare a string to the name of an existing macro; this is a
1416 * simple wrapper which calls either strcmp or nasm_stricmp
1417 * depending on the value of the `casesense' parameter.
1418 */
H. Peter Anvin4db5a162007-10-11 13:42:09 -07001419static int mstrcmp(const char *p, const char *q, bool casesense)
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001420{
H. Peter Anvin734b1882002-04-30 21:01:08 +00001421 return casesense ? strcmp(p, q) : nasm_stricmp(p, q);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001422}
1423
1424/*
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07001425 * Compare a string to the name of an existing macro; this is a
1426 * simple wrapper which calls either strcmp or nasm_stricmp
1427 * depending on the value of the `casesense' parameter.
1428 */
1429static int mmemcmp(const char *p, const char *q, size_t l, bool casesense)
1430{
1431 return casesense ? memcmp(p, q, l) : nasm_memicmp(p, q, l);
1432}
1433
1434/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001435 * Return the Context structure associated with a %$ token. Return
1436 * NULL, having _already_ reported an error condition, if the
1437 * context stack isn't deep enough for the supplied number of $
1438 * signs.
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001439 *
1440 * If "namep" is non-NULL, set it to the pointer to the macro name
1441 * tail, i.e. the part beyond %$...
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001442 */
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04001443static Context *get_ctx(const char *name, const char **namep)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001444{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001445 Context *ctx;
1446 int i;
1447
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001448 if (namep)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001449 *namep = name;
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001450
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001451 if (!name || name[0] != '%' || name[1] != '$')
H. Peter Anvine2c80182005-01-15 22:15:51 +00001452 return NULL;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001453
H. Peter Anvine2c80182005-01-15 22:15:51 +00001454 if (!cstk) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03001455 nasm_nonfatal("`%s': context stack is empty", name);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001456 return NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001457 }
1458
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001459 name += 2;
1460 ctx = cstk;
1461 i = 0;
1462 while (ctx && *name == '$') {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001463 name++;
1464 i++;
1465 ctx = ctx->next;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001466 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001467 if (!ctx) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03001468 nasm_nonfatal("`%s': context stack is only"
1469 " %d level%s deep", name, i, (i == 1 ? "" : "s"));
H. Peter Anvine2c80182005-01-15 22:15:51 +00001470 return NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001471 }
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001472
1473 if (namep)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001474 *namep = name;
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001475
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04001476 return ctx;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001477}
1478
1479/*
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001480 * Open an include file. This routine must always return a valid
1481 * file pointer if it returns - it's responsible for throwing an
1482 * ERR_FATAL and bombing out completely if not. It should also try
1483 * the include path one by one until it finds the file or reaches
1484 * the end of the path.
H. Peter Anvind81a2352016-09-21 14:03:18 -07001485 *
1486 * Note: for INC_PROBE the function returns NULL at all times;
1487 * instead look for the
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001488 */
H. Peter Anvind81a2352016-09-21 14:03:18 -07001489enum incopen_mode {
1490 INC_NEEDED, /* File must exist */
1491 INC_OPTIONAL, /* Missing is OK */
1492 INC_PROBE /* Only an existence probe */
1493};
1494
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001495/* This is conducts a full pathname search */
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07001496static FILE *inc_fopen_search(const char *file, char **slpath,
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001497 enum incopen_mode omode, enum file_flags fmode)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001498{
H. Peter Anvin (Intel)64471092018-12-11 13:06:14 -08001499 const struct strlist_entry *ip = strlist_head(ipath_list);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001500 FILE *fp;
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07001501 const char *prefix = "";
night199ukfdb1a1b2018-10-18 23:19:47 +02001502 char *sp;
H. Peter Anvind81a2352016-09-21 14:03:18 -07001503 bool found;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001504
H. Peter Anvine2c80182005-01-15 22:15:51 +00001505 while (1) {
night199ukfdb1a1b2018-10-18 23:19:47 +02001506 sp = nasm_catfile(prefix, file);
H. Peter Anvind81a2352016-09-21 14:03:18 -07001507 if (omode == INC_PROBE) {
1508 fp = NULL;
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07001509 found = nasm_file_exists(sp);
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001510 } else {
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07001511 fp = nasm_open_read(sp, fmode);
H. Peter Anvind81a2352016-09-21 14:03:18 -07001512 found = (fp != NULL);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001513 }
H. Peter Anvind81a2352016-09-21 14:03:18 -07001514 if (found) {
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07001515 *slpath = sp;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001516 return fp;
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001517 }
Jim Kukunas65a8afc2016-06-13 16:00:42 -04001518
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07001519 nasm_free(sp);
Jim Kukunas65a8afc2016-06-13 16:00:42 -04001520
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07001521 if (!ip) {
1522 *slpath = NULL;
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001523 return NULL;
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07001524 }
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001525
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07001526 prefix = ip->str;
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001527 ip = ip->next;
1528 }
1529}
1530
1531/*
1532 * Open a file, or test for the presence of one (depending on omode),
1533 * considering the include path.
1534 */
1535static FILE *inc_fopen(const char *file,
Cyrill Gorcunovb7bb5ac2018-11-11 21:33:52 +03001536 struct strlist *dhead,
H. Peter Anvinccad6f92016-10-04 00:34:35 -07001537 const char **found_path,
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001538 enum incopen_mode omode,
1539 enum file_flags fmode)
1540{
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001541 struct hash_insert hi;
1542 void **hp;
1543 char *path;
1544 FILE *fp = NULL;
1545
1546 hp = hash_find(&FileHash, file, &hi);
1547 if (hp) {
1548 path = *hp;
Martin Storsjöf283c8f2017-08-13 17:28:46 +03001549 if (path || omode != INC_NEEDED) {
Cyrill Gorcunovb7bb5ac2018-11-11 21:33:52 +03001550 strlist_add(dhead, path ? path : file);
Martin Storsjöf283c8f2017-08-13 17:28:46 +03001551 }
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001552 } else {
1553 /* Need to do the actual path search */
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07001554 fp = inc_fopen_search(file, &path, omode, fmode);
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001555
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07001556 /* Positive or negative result */
1557 hash_add(&hi, nasm_strdup(file), path);
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001558
H. Peter Anvin9924d1e2016-10-04 00:59:39 -07001559 /*
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07001560 * Add file to dependency path.
H. Peter Anvin9924d1e2016-10-04 00:59:39 -07001561 */
1562 if (path || omode != INC_NEEDED)
Cyrill Gorcunovb7bb5ac2018-11-11 21:33:52 +03001563 strlist_add(dhead, file);
H. Peter Anvineba20a72002-04-30 20:53:55 +00001564 }
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001565
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001566 if (!path) {
1567 if (omode == INC_NEEDED)
H. Peter Anvinc5136902018-06-15 18:20:17 -07001568 nasm_fatal("unable to open include file `%s'", file);
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07001569 } else {
1570 if (!fp && omode != INC_PROBE)
1571 fp = nasm_open_read(path, fmode);
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001572 }
1573
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001574 if (found_path)
H. Peter Anvinccad6f92016-10-04 00:34:35 -07001575 *found_path = path;
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001576
1577 return fp;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001578}
1579
1580/*
Fabian Giesen0bbc38d2016-04-28 13:48:14 -07001581 * Opens an include or input file. Public version, for use by modules
1582 * that get a file:lineno pair and need to look at the file again
1583 * (e.g. the CodeView debug backend). Returns NULL on failure.
1584 */
H. Peter Anvin3e83cec2016-05-25 04:28:46 -07001585FILE *pp_input_fopen(const char *filename, enum file_flags mode)
Fabian Giesen0bbc38d2016-04-28 13:48:14 -07001586{
H. Peter Anvin9924d1e2016-10-04 00:59:39 -07001587 return inc_fopen(filename, NULL, NULL, INC_OPTIONAL, mode);
Fabian Giesen0bbc38d2016-04-28 13:48:14 -07001588}
1589
1590/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001591 * Determine if we should warn on defining a single-line macro of
H. Peter Anvinef7468f2002-04-30 20:57:59 +00001592 * name `name', with `nparam' parameters. If nparam is 0 or -1, will
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001593 * return true if _any_ single-line macro of that name is defined.
1594 * Otherwise, will return true if a single-line macro with either
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001595 * `nparam' or no parameters is defined.
1596 *
1597 * If a macro with precisely the right number of parameters is
H. Peter Anvinef7468f2002-04-30 20:57:59 +00001598 * defined, or nparam is -1, the address of the definition structure
1599 * will be returned in `defn'; otherwise NULL will be returned. If `defn'
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001600 * is NULL, no action will be taken regarding its contents, and no
1601 * error will occur.
1602 *
1603 * Note that this is also called with nparam zero to resolve
1604 * `ifdef'.
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001605 *
1606 * If you already know which context macro belongs to, you can pass
1607 * the context pointer as first parameter; if you won't but name begins
1608 * with %$ the context will be automatically computed. If all_contexts
1609 * is true, macro will be searched in outer contexts as well.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001610 */
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07001611static bool
H. Peter Anvinb2a5fda2008-06-19 21:42:42 -07001612smacro_defined(Context * ctx, const char *name, int nparam, SMacro ** defn,
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07001613 bool nocase)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001614{
H. Peter Anvin166c2472008-05-28 12:28:58 -07001615 struct hash_table *smtbl;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001616 SMacro *m;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001617
H. Peter Anvin97a23472007-09-16 17:57:25 -07001618 if (ctx) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001619 smtbl = &ctx->localmac;
H. Peter Anvin97a23472007-09-16 17:57:25 -07001620 } else if (name[0] == '%' && name[1] == '$') {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001621 if (cstk)
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04001622 ctx = get_ctx(name, &name);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001623 if (!ctx)
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001624 return false; /* got to return _something_ */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001625 smtbl = &ctx->localmac;
H. Peter Anvin97a23472007-09-16 17:57:25 -07001626 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001627 smtbl = &smacros;
H. Peter Anvin97a23472007-09-16 17:57:25 -07001628 }
H. Peter Anvin166c2472008-05-28 12:28:58 -07001629 m = (SMacro *) hash_findix(smtbl, name);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001630
H. Peter Anvine2c80182005-01-15 22:15:51 +00001631 while (m) {
1632 if (!mstrcmp(m->name, name, m->casesense && nocase) &&
Charles Crayne192d5b52007-10-18 19:02:42 -07001633 (nparam <= 0 || m->nparam == 0 || nparam == (int) m->nparam)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001634 if (defn) {
Charles Crayne192d5b52007-10-18 19:02:42 -07001635 if (nparam == (int) m->nparam || nparam == -1)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001636 *defn = m;
1637 else
1638 *defn = NULL;
1639 }
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001640 return true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001641 }
1642 m = m->next;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001643 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001644
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001645 return false;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001646}
1647
Cyrill Gorcunov3079f792018-11-14 10:03:42 +03001648/* param should be a natural number [0; INT_MAX] */
1649static int read_param_count(const char *str)
1650{
1651 int result;
1652 bool err;
1653
1654 result = readnum(str, &err);
1655 if (result < 0 || result > INT_MAX) {
1656 result = 0;
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03001657 nasm_nonfatal("parameter count `%s' is out of bounds [%d; %d]",
1658 str, 0, INT_MAX);
1659 } else if (err)
1660 nasm_nonfatal("unable to parse parameter count `%s'", str);
Cyrill Gorcunov3079f792018-11-14 10:03:42 +03001661 return result;
1662}
1663
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001664/*
1665 * Count and mark off the parameters in a multi-line macro call.
1666 * This is called both from within the multi-line macro expansion
1667 * code, and also to mark off the default parameters when provided
1668 * in a %macro definition line.
1669 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001670static void count_mmac_params(Token * t, int *nparam, Token *** params)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001671{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001672 int paramsize, brace;
1673
1674 *nparam = paramsize = 0;
1675 *params = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001676 while (t) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001677 /* +1: we need space for the final NULL */
H. Peter Anvin91fb6f12008-09-01 10:56:33 -07001678 if (*nparam+1 >= paramsize) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001679 paramsize += PARAM_DELTA;
1680 *params = nasm_realloc(*params, sizeof(**params) * paramsize);
1681 }
1682 skip_white_(t);
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08001683 brace = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001684 if (tok_is_(t, "{"))
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08001685 brace++;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001686 (*params)[(*nparam)++] = t;
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08001687 if (brace) {
1688 while (brace && (t = t->next) != NULL) {
1689 if (tok_is_(t, "{"))
1690 brace++;
1691 else if (tok_is_(t, "}"))
1692 brace--;
1693 }
1694
1695 if (t) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001696 /*
1697 * Now we've found the closing brace, look further
1698 * for the comma.
1699 */
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08001700 t = t->next;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001701 skip_white_(t);
1702 if (tok_isnt_(t, ",")) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03001703 nasm_nonfatal("braces do not enclose all of macro parameter");
H. Peter Anvine2c80182005-01-15 22:15:51 +00001704 while (tok_isnt_(t, ","))
1705 t = t->next;
1706 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001707 }
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08001708 } else {
1709 while (tok_isnt_(t, ","))
1710 t = t->next;
1711 }
1712 if (t) { /* got a comma/brace */
1713 t = t->next; /* eat the comma */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001714 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001715 }
1716}
1717
1718/*
H. Peter Anvin76690a12002-04-30 20:52:49 +00001719 * Determine whether one of the various `if' conditions is true or
1720 * not.
1721 *
1722 * We must free the tline we get passed.
1723 */
H. Peter Anvin70055962007-10-11 00:05:31 -07001724static bool if_condition(Token * tline, enum preproc_token ct)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001725{
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001726 enum pp_conditional i = PP_COND(ct);
H. Peter Anvin70055962007-10-11 00:05:31 -07001727 bool j;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001728 Token *t, *tt, **tptr, *origline;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001729 struct tokenval tokval;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001730 expr *evalresult;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001731 enum pp_token_type needtype;
H. Peter Anvin077fb932010-07-20 14:56:30 -07001732 char *p;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001733
1734 origline = tline;
1735
H. Peter Anvine2c80182005-01-15 22:15:51 +00001736 switch (i) {
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001737 case PPC_IFCTX:
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001738 j = false; /* have we matched yet? */
Victor van den Elzen0e857f12008-07-23 13:21:29 +02001739 while (true) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001740 skip_white_(tline);
Victor van den Elzen0e857f12008-07-23 13:21:29 +02001741 if (!tline)
1742 break;
1743 if (tline->type != TOK_ID) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03001744 nasm_nonfatal("`%s' expects context identifiers",
1745 pp_directives[ct]);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001746 free_tlist(origline);
1747 return -1;
1748 }
Victor van den Elzen0e857f12008-07-23 13:21:29 +02001749 if (cstk && cstk->name && !nasm_stricmp(tline->text, cstk->name))
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001750 j = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001751 tline = tline->next;
1752 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001753 break;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001754
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001755 case PPC_IFDEF:
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001756 j = false; /* have we matched yet? */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001757 while (tline) {
1758 skip_white_(tline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001759 if (!tline || (tline->type != TOK_ID &&
1760 (tline->type != TOK_PREPROC_ID ||
1761 tline->text[1] != '$'))) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03001762 nasm_nonfatal("`%s' expects macro identifiers",
1763 pp_directives[ct]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001764 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001765 }
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07001766 if (smacro_defined(NULL, tline->text, 0, NULL, true))
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001767 j = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001768 tline = tline->next;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001769 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001770 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001771
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001772 case PPC_IFENV:
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001773 tline = expand_smacro(tline);
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001774 j = false; /* have we matched yet? */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001775 while (tline) {
1776 skip_white_(tline);
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001777 if (!tline || (tline->type != TOK_ID &&
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001778 tline->type != TOK_STRING &&
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001779 (tline->type != TOK_PREPROC_ID ||
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001780 tline->text[1] != '!'))) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03001781 nasm_nonfatal("`%s' expects environment variable names",
1782 pp_directives[ct]);
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001783 goto fail;
1784 }
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001785 p = tline->text;
1786 if (tline->type == TOK_PREPROC_ID)
1787 p += 2; /* Skip leading %! */
H. Peter Anvin53e2e4c2018-11-28 15:01:40 -08001788 if (nasm_isquote(*p))
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001789 nasm_unquote_cstr(p, ct);
1790 if (getenv(p))
1791 j = true;
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001792 tline = tline->next;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001793 }
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001794 break;
1795
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001796 case PPC_IFIDN:
1797 case PPC_IFIDNI:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001798 tline = expand_smacro(tline);
1799 t = tt = tline;
1800 while (tok_isnt_(tt, ","))
1801 tt = tt->next;
1802 if (!tt) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03001803 nasm_nonfatal("`%s' expects two comma-separated arguments",
1804 pp_directives[ct]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001805 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001806 }
1807 tt = tt->next;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001808 j = true; /* assume equality unless proved not */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001809 while ((t->type != TOK_OTHER || strcmp(t->text, ",")) && tt) {
1810 if (tt->type == TOK_OTHER && !strcmp(tt->text, ",")) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03001811 nasm_nonfatal("`%s': more than one comma on line",
1812 pp_directives[ct]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001813 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001814 }
1815 if (t->type == TOK_WHITESPACE) {
1816 t = t->next;
1817 continue;
1818 }
1819 if (tt->type == TOK_WHITESPACE) {
1820 tt = tt->next;
1821 continue;
1822 }
1823 if (tt->type != t->type) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001824 j = false; /* found mismatching tokens */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001825 break;
1826 }
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07001827 /* When comparing strings, need to unquote them first */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001828 if (t->type == TOK_STRING) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001829 size_t l1 = nasm_unquote(t->text, NULL);
1830 size_t l2 = nasm_unquote(tt->text, NULL);
H. Peter Anvind2456592008-06-19 15:04:18 -07001831
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001832 if (l1 != l2) {
1833 j = false;
1834 break;
1835 }
1836 if (mmemcmp(t->text, tt->text, l1, i == PPC_IFIDN)) {
1837 j = false;
1838 break;
1839 }
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07001840 } else if (mstrcmp(tt->text, t->text, i == PPC_IFIDN) != 0) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001841 j = false; /* found mismatching tokens */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001842 break;
1843 }
Nickolay Yurchenkof3b3ce22003-09-21 20:38:43 +00001844
H. Peter Anvine2c80182005-01-15 22:15:51 +00001845 t = t->next;
1846 tt = tt->next;
1847 }
1848 if ((t->type != TOK_OTHER || strcmp(t->text, ",")) || tt)
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001849 j = false; /* trailing gunk on one end or other */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001850 break;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001851
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001852 case PPC_IFMACRO:
H. Peter Anvin89cee572009-07-15 09:16:54 -04001853 {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001854 bool found = false;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001855 MMacro searching, *mmac;
H. Peter Anvin65747262002-05-07 00:10:05 +00001856
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001857 skip_white_(tline);
1858 tline = expand_id(tline);
1859 if (!tok_type_(tline, TOK_ID)) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03001860 nasm_nonfatal("`%s' expects a macro name", pp_directives[ct]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001861 goto fail;
1862 }
1863 searching.name = nasm_strdup(tline->text);
1864 searching.casesense = true;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001865 searching.plus = false;
1866 searching.nolist = false;
1867 searching.in_progress = 0;
1868 searching.max_depth = 0;
1869 searching.rep_nest = NULL;
1870 searching.nparam_min = 0;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001871 searching.nparam_max = INT_MAX;
1872 tline = expand_smacro(tline->next);
1873 skip_white_(tline);
1874 if (!tline) {
1875 } else if (!tok_type_(tline, TOK_NUMBER)) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03001876 nasm_nonfatal("`%s' expects a parameter count or nothing",
1877 pp_directives[ct]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001878 } else {
1879 searching.nparam_min = searching.nparam_max =
Cyrill Gorcunov3079f792018-11-14 10:03:42 +03001880 read_param_count(tline->text);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001881 }
1882 if (tline && tok_is_(tline->next, "-")) {
1883 tline = tline->next->next;
1884 if (tok_is_(tline, "*"))
1885 searching.nparam_max = INT_MAX;
1886 else if (!tok_type_(tline, TOK_NUMBER))
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03001887 nasm_nonfatal("`%s' expects a parameter count after `-'",
1888 pp_directives[ct]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001889 else {
Cyrill Gorcunov3079f792018-11-14 10:03:42 +03001890 searching.nparam_max = read_param_count(tline->text);
Cyrill Gorcunovc9244ea2017-10-22 15:25:48 +03001891 if (searching.nparam_min > searching.nparam_max) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03001892 nasm_nonfatal("minimum parameter count exceeds maximum");
Cyrill Gorcunovc9244ea2017-10-22 15:25:48 +03001893 searching.nparam_max = searching.nparam_min;
1894 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001895 }
1896 }
1897 if (tline && tok_is_(tline->next, "+")) {
1898 tline = tline->next;
1899 searching.plus = true;
1900 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001901 mmac = (MMacro *) hash_findix(&mmacros, searching.name);
1902 while (mmac) {
1903 if (!strcmp(mmac->name, searching.name) &&
1904 (mmac->nparam_min <= searching.nparam_max
1905 || searching.plus)
1906 && (searching.nparam_min <= mmac->nparam_max
1907 || mmac->plus)) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001908 found = true;
1909 break;
1910 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001911 mmac = mmac->next;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001912 }
1913 if (tline && tline->next)
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03001914 nasm_warnf(ERR_PASS1, "trailing garbage after %%ifmacro ignored");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001915 nasm_free(searching.name);
1916 j = found;
1917 break;
H. Peter Anvin89cee572009-07-15 09:16:54 -04001918 }
H. Peter Anvin65747262002-05-07 00:10:05 +00001919
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001920 case PPC_IFID:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001921 needtype = TOK_ID;
1922 goto iftype;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001923 case PPC_IFNUM:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001924 needtype = TOK_NUMBER;
1925 goto iftype;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001926 case PPC_IFSTR:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001927 needtype = TOK_STRING;
1928 goto iftype;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001929
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001930iftype:
1931 t = tline = expand_smacro(tline);
H. Peter Anvind85d2502008-05-04 17:53:31 -07001932
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001933 while (tok_type_(t, TOK_WHITESPACE) ||
1934 (needtype == TOK_NUMBER &&
1935 tok_type_(t, TOK_OTHER) &&
1936 (t->text[0] == '-' || t->text[0] == '+') &&
1937 !t->text[1]))
1938 t = t->next;
H. Peter Anvind85d2502008-05-04 17:53:31 -07001939
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001940 j = tok_type_(t, needtype);
1941 break;
H. Peter Anvincbf768d2008-02-16 16:41:25 -08001942
1943 case PPC_IFTOKEN:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001944 t = tline = expand_smacro(tline);
1945 while (tok_type_(t, TOK_WHITESPACE))
1946 t = t->next;
H. Peter Anvincbf768d2008-02-16 16:41:25 -08001947
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001948 j = false;
1949 if (t) {
1950 t = t->next; /* Skip the actual token */
1951 while (tok_type_(t, TOK_WHITESPACE))
1952 t = t->next;
1953 j = !t; /* Should be nothing left */
1954 }
1955 break;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001956
H. Peter Anvin134b9462008-02-16 17:01:40 -08001957 case PPC_IFEMPTY:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001958 t = tline = expand_smacro(tline);
1959 while (tok_type_(t, TOK_WHITESPACE))
1960 t = t->next;
H. Peter Anvin134b9462008-02-16 17:01:40 -08001961
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001962 j = !t; /* Should be empty */
1963 break;
H. Peter Anvin134b9462008-02-16 17:01:40 -08001964
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001965 case PPC_IF:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001966 t = tline = expand_smacro(tline);
1967 tptr = &t;
1968 tokval.t_type = TOKEN_INVALID;
1969 evalresult = evaluate(ppscan, tptr, &tokval,
H. Peter Anvin130736c2016-02-17 20:27:41 -08001970 NULL, pass | CRITICAL, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001971 if (!evalresult)
1972 return -1;
1973 if (tokval.t_type)
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03001974 nasm_warnf(ERR_PASS1, "trailing garbage after expression ignored");
H. Peter Anvine2c80182005-01-15 22:15:51 +00001975 if (!is_simple(evalresult)) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03001976 nasm_nonfatal("non-constant value given to `%s'",
1977 pp_directives[ct]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001978 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001979 }
Chuck Crayne60ae75d2007-05-02 01:59:16 +00001980 j = reloc_value(evalresult) != 0;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001981 break;
H. Peter Anvin95e28822007-09-12 04:20:08 +00001982
H. Peter Anvine2c80182005-01-15 22:15:51 +00001983 default:
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03001984 nasm_fatal("preprocessor directive `%s' not yet implemented",
1985 pp_directives[ct]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001986 goto fail;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001987 }
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001988
1989 free_tlist(origline);
1990 return j ^ PP_NEGATIVE(ct);
H. Peter Anvin70653092007-10-19 14:42:29 -07001991
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001992fail:
1993 free_tlist(origline);
1994 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001995}
1996
1997/*
H. Peter Anvin4db5a162007-10-11 13:42:09 -07001998 * Common code for defining an smacro
1999 */
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08002000static bool define_smacro(Context *ctx, const char *mname, bool casesense,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002001 int nparam, Token *expansion)
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002002{
2003 SMacro *smac, **smhead;
H. Peter Anvin166c2472008-05-28 12:28:58 -07002004 struct hash_table *smtbl;
H. Peter Anvin70653092007-10-19 14:42:29 -07002005
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002006 if (smacro_defined(ctx, mname, nparam, &smac, casesense)) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002007 if (!smac) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002008 nasm_warnf(ERR_PASS1, "single-line macro `%s' defined both with and"
2009 " without parameters", mname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002010 /*
2011 * Some instances of the old code considered this a failure,
2012 * some others didn't. What is the right thing to do here?
2013 */
2014 free_tlist(expansion);
2015 return false; /* Failure */
2016 } else {
2017 /*
2018 * We're redefining, so we have to take over an
2019 * existing SMacro structure. This means freeing
2020 * what was already in it.
2021 */
2022 nasm_free(smac->name);
2023 free_tlist(smac->expansion);
2024 }
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002025 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002026 smtbl = ctx ? &ctx->localmac : &smacros;
2027 smhead = (SMacro **) hash_findi_add(smtbl, mname);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002028 smac = nasm_malloc(sizeof(SMacro));
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002029 smac->next = *smhead;
2030 *smhead = smac;
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002031 }
2032 smac->name = nasm_strdup(mname);
2033 smac->casesense = casesense;
2034 smac->nparam = nparam;
2035 smac->expansion = expansion;
2036 smac->in_progress = false;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002037 return true; /* Success */
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002038}
2039
2040/*
2041 * Undefine an smacro
2042 */
2043static void undef_smacro(Context *ctx, const char *mname)
2044{
2045 SMacro **smhead, *s, **sp;
H. Peter Anvin166c2472008-05-28 12:28:58 -07002046 struct hash_table *smtbl;
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002047
H. Peter Anvin166c2472008-05-28 12:28:58 -07002048 smtbl = ctx ? &ctx->localmac : &smacros;
2049 smhead = (SMacro **)hash_findi(smtbl, mname, NULL);
H. Peter Anvin70653092007-10-19 14:42:29 -07002050
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002051 if (smhead) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002052 /*
2053 * We now have a macro name... go hunt for it.
2054 */
2055 sp = smhead;
2056 while ((s = *sp) != NULL) {
2057 if (!mstrcmp(s->name, mname, s->casesense)) {
2058 *sp = s->next;
2059 nasm_free(s->name);
2060 free_tlist(s->expansion);
2061 nasm_free(s);
2062 } else {
2063 sp = &s->next;
2064 }
2065 }
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002066 }
2067}
2068
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002069/*
H. Peter Anvina26433d2008-07-16 14:40:01 -07002070 * Parse a mmacro specification.
2071 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002072static bool parse_mmacro_spec(Token *tline, MMacro *def, const char *directive)
H. Peter Anvina26433d2008-07-16 14:40:01 -07002073{
H. Peter Anvina26433d2008-07-16 14:40:01 -07002074 tline = tline->next;
2075 skip_white_(tline);
2076 tline = expand_id(tline);
2077 if (!tok_type_(tline, TOK_ID)) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002078 nasm_nonfatal("`%s' expects a macro name", directive);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002079 return false;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002080 }
Victor van den Elzenb916ede2008-07-23 15:14:22 +02002081
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002082 def->prev = NULL;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002083 def->name = nasm_strdup(tline->text);
2084 def->plus = false;
2085 def->nolist = false;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002086 def->in_progress = 0;
2087 def->rep_nest = NULL;
Victor van den Elzenb916ede2008-07-23 15:14:22 +02002088 def->nparam_min = 0;
2089 def->nparam_max = 0;
2090
H. Peter Anvina26433d2008-07-16 14:40:01 -07002091 tline = expand_smacro(tline->next);
2092 skip_white_(tline);
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002093 if (!tok_type_(tline, TOK_NUMBER))
2094 nasm_nonfatal("`%s' expects a parameter count", directive);
2095 else
Cyrill Gorcunov3079f792018-11-14 10:03:42 +03002096 def->nparam_min = def->nparam_max = read_param_count(tline->text);
H. Peter Anvina26433d2008-07-16 14:40:01 -07002097 if (tline && tok_is_(tline->next, "-")) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002098 tline = tline->next->next;
2099 if (tok_is_(tline, "*")) {
2100 def->nparam_max = INT_MAX;
2101 } else if (!tok_type_(tline, TOK_NUMBER)) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002102 nasm_nonfatal("`%s' expects a parameter count after `-'", directive);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002103 } else {
Cyrill Gorcunov3079f792018-11-14 10:03:42 +03002104 def->nparam_max = read_param_count(tline->text);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002105 if (def->nparam_min > def->nparam_max) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002106 nasm_nonfatal("minimum parameter count exceeds maximum");
Cyrill Gorcunovc9244ea2017-10-22 15:25:48 +03002107 def->nparam_max = def->nparam_min;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002108 }
2109 }
H. Peter Anvina26433d2008-07-16 14:40:01 -07002110 }
2111 if (tline && tok_is_(tline->next, "+")) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002112 tline = tline->next;
2113 def->plus = true;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002114 }
2115 if (tline && tok_type_(tline->next, TOK_ID) &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002116 !nasm_stricmp(tline->next->text, ".nolist")) {
2117 tline = tline->next;
2118 def->nolist = true;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002119 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002120
H. Peter Anvina26433d2008-07-16 14:40:01 -07002121 /*
2122 * Handle default parameters.
2123 */
2124 if (tline && tline->next) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002125 def->dlist = tline->next;
2126 tline->next = NULL;
2127 count_mmac_params(def->dlist, &def->ndefs, &def->defaults);
H. Peter Anvina26433d2008-07-16 14:40:01 -07002128 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002129 def->dlist = NULL;
2130 def->defaults = NULL;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002131 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002132 def->expansion = NULL;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002133
H. Peter Anvin89cee572009-07-15 09:16:54 -04002134 if (def->defaults && def->ndefs > def->nparam_max - def->nparam_min &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002135 !def->plus)
H. Peter Anvin (Intel)fef75c22018-12-12 18:13:47 -08002136 nasm_warnf(ERR_PASS1|WARN_MACRO_DEFAULTS, "too many default macro parameters in macro `%s'", def->name);
Victor van den Elzenb916ede2008-07-23 15:14:22 +02002137
H. Peter Anvina26433d2008-07-16 14:40:01 -07002138 return true;
2139}
2140
2141
2142/*
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002143 * Decode a size directive
2144 */
2145static int parse_size(const char *str) {
2146 static const char *size_names[] =
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002147 { "byte", "dword", "oword", "qword", "tword", "word", "yword" };
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002148 static const int sizes[] =
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002149 { 0, 1, 4, 16, 8, 10, 2, 32 };
Cyrill Gorcunovc713b5f2018-09-29 14:30:14 +03002150 return str ? sizes[bsii(str, size_names, ARRAY_SIZE(size_names))+1] : 0;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002151}
2152
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07002153/*
2154 * Process a preprocessor %pragma directive. Currently there are none.
2155 * Gets passed the token list starting with the "preproc" token from
2156 * "%pragma preproc".
2157 */
2158static void do_pragma_preproc(Token *tline)
2159{
2160 /* Skip to the real stuff */
2161 tline = tline->next;
2162 skip_white_(tline);
2163 if (!tline)
2164 return;
2165
2166 (void)tline; /* Nothing else to do at present */
2167}
2168
Ed Beroset3ab3f412002-06-11 03:31:49 +00002169/**
2170 * find and process preprocessor directive in passed line
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002171 * Find out if a line contains a preprocessor directive, and deal
2172 * with it if so.
H. Peter Anvin70653092007-10-19 14:42:29 -07002173 *
Ed Beroset3ab3f412002-06-11 03:31:49 +00002174 * If a directive _is_ found, it is the responsibility of this routine
2175 * (and not the caller) to free_tlist() the line.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002176 *
Ed Beroset3ab3f412002-06-11 03:31:49 +00002177 * @param tline a pointer to the current tokeninzed line linked list
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07002178 * @param output if this directive generated output
Ed Beroset3ab3f412002-06-11 03:31:49 +00002179 * @return DIRECTIVE_FOUND or NO_DIRECTIVE_FOUND
H. Peter Anvin70653092007-10-19 14:42:29 -07002180 *
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002181 */
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07002182static int do_directive(Token *tline, char **output)
H. Peter Anvineba20a72002-04-30 20:53:55 +00002183{
H. Peter Anvin4169a472007-09-12 01:29:43 +00002184 enum preproc_token i;
2185 int j;
H. Peter Anvin70055962007-10-11 00:05:31 -07002186 bool err;
2187 int nparam;
2188 bool nolist;
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07002189 bool casesense;
H. Peter Anvin8cfdb9d2007-09-14 18:36:01 -07002190 int k, m;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002191 int offset;
H. Peter Anvinccad6f92016-10-04 00:34:35 -07002192 char *p, *pp;
2193 const char *found_path;
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08002194 const char *mname;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002195 Include *inc;
2196 Context *ctx;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002197 Cond *cond;
2198 MMacro *mmac, **mmhead;
Jin Kyu Songc9486b92013-10-28 17:07:57 -07002199 Token *t = NULL, *tt, *param_start, *macro_start, *last, **tptr, *origline;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002200 Line *l;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002201 struct tokenval tokval;
2202 expr *evalresult;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002203 MMacro *tmp_defining; /* Used when manipulating rep_nest */
H. Peter Anvinf8ba53e2007-10-11 10:11:57 -07002204 int64_t count;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07002205 size_t len;
H. Peter Anvin (Intel)6bde2ed2018-12-13 19:39:41 -08002206 errflags severity;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002207
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07002208 *output = NULL; /* No output generated */
H. Peter Anvin76690a12002-04-30 20:52:49 +00002209 origline = tline;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002210
H. Peter Anvineba20a72002-04-30 20:53:55 +00002211 skip_white_(tline);
H. Peter Anvinf2936d72008-06-04 15:11:23 -07002212 if (!tline || !tok_type_(tline, TOK_PREPROC_ID) ||
Cyrill Gorcunov4b5b7372018-10-29 22:54:08 +03002213 (tline->text[0] && (tline->text[1] == '%' ||
2214 tline->text[1] == '$' ||
2215 tline->text[1] == '!')))
H. Peter Anvine2c80182005-01-15 22:15:51 +00002216 return NO_DIRECTIVE_FOUND;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002217
H. Peter Anvin4169a472007-09-12 01:29:43 +00002218 i = pp_token_hash(tline->text);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002219
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002220 /*
2221 * FIXME: We zap execution of PP_RMACRO, PP_IRMACRO, PP_EXITMACRO
2222 * since they are known to be buggy at moment, we need to fix them
2223 * in future release (2.09-2.10)
2224 */
Philipp Klokeb432f572013-03-31 12:01:23 +02002225 if (i == PP_RMACRO || i == PP_IRMACRO || i == PP_EXITMACRO) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002226 nasm_nonfatal("unknown preprocessor directive `%s'", tline->text);
2227 return NO_DIRECTIVE_FOUND;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002228 }
2229
2230 /*
2231 * If we're in a non-emitting branch of a condition construct,
2232 * or walking to the end of an already terminated %rep block,
2233 * we should ignore all directives except for condition
2234 * directives.
2235 */
2236 if (((istk->conds && !emitting(istk->conds->state)) ||
2237 (istk->mstk && !istk->mstk->in_progress)) && !is_condition(i)) {
2238 return NO_DIRECTIVE_FOUND;
2239 }
2240
2241 /*
2242 * If we're defining a macro or reading a %rep block, we should
2243 * ignore all directives except for %macro/%imacro (which nest),
2244 * %endm/%endmacro, and (only if we're in a %rep block) %endrep.
2245 * If we're in a %rep block, another %rep nests, so should be let through.
2246 */
2247 if (defining && i != PP_MACRO && i != PP_IMACRO &&
2248 i != PP_RMACRO && i != PP_IRMACRO &&
2249 i != PP_ENDMACRO && i != PP_ENDM &&
2250 (defining->name || (i != PP_ENDREP && i != PP_REP))) {
2251 return NO_DIRECTIVE_FOUND;
2252 }
2253
2254 if (defining) {
2255 if (i == PP_MACRO || i == PP_IMACRO ||
2256 i == PP_RMACRO || i == PP_IRMACRO) {
2257 nested_mac_count++;
2258 return NO_DIRECTIVE_FOUND;
2259 } else if (nested_mac_count > 0) {
2260 if (i == PP_ENDMACRO) {
2261 nested_mac_count--;
2262 return NO_DIRECTIVE_FOUND;
2263 }
2264 }
2265 if (!defining->name) {
2266 if (i == PP_REP) {
2267 nested_rep_count++;
2268 return NO_DIRECTIVE_FOUND;
2269 } else if (nested_rep_count > 0) {
2270 if (i == PP_ENDREP) {
2271 nested_rep_count--;
2272 return NO_DIRECTIVE_FOUND;
2273 }
2274 }
2275 }
2276 }
2277
H. Peter Anvin4169a472007-09-12 01:29:43 +00002278 switch (i) {
2279 case PP_INVALID:
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002280 nasm_nonfatal("unknown preprocessor directive `%s'", tline->text);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002281 return NO_DIRECTIVE_FOUND; /* didn't get it */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002282
H. Peter Anvin3f87a2a2016-10-04 14:07:19 -07002283 case PP_PRAGMA:
2284 /*
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07002285 * %pragma namespace options...
2286 *
2287 * The namespace "preproc" is reserved for the preprocessor;
2288 * all other namespaces generate a [pragma] assembly directive.
2289 *
2290 * Invalid %pragmas are ignored and may have different
2291 * meaning in future versions of NASM.
H. Peter Anvin3f87a2a2016-10-04 14:07:19 -07002292 */
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07002293 tline = tline->next;
2294 skip_white_(tline);
2295 tline = expand_smacro(tline);
2296 if (tok_type_(tline, TOK_ID)) {
2297 if (!nasm_stricmp(tline->text, "preproc")) {
2298 /* Preprocessor pragma */
2299 do_pragma_preproc(tline);
2300 } else {
2301 /* Build the assembler directive */
2302 t = new_Token(NULL, TOK_OTHER, "[", 1);
2303 t->next = new_Token(NULL, TOK_ID, "pragma", 6);
2304 t->next->next = new_Token(tline, TOK_WHITESPACE, NULL, 0);
2305 tline = t;
2306 for (t = tline; t->next; t = t->next)
2307 ;
2308 t->next = new_Token(NULL, TOK_OTHER, "]", 1);
2309 /* true here can be revisited in the future */
2310 *output = detoken(tline, true);
2311 }
2312 }
H. Peter Anvin3f87a2a2016-10-04 14:07:19 -07002313 free_tlist(origline);
2314 return DIRECTIVE_FOUND;
2315
H. Peter Anvine2c80182005-01-15 22:15:51 +00002316 case PP_STACKSIZE:
2317 /* Directive to tell NASM what the default stack size is. The
2318 * default is for a 16-bit stack, and this can be overriden with
2319 * %stacksize large.
H. Peter Anvine2c80182005-01-15 22:15:51 +00002320 */
2321 tline = tline->next;
2322 if (tline && tline->type == TOK_WHITESPACE)
2323 tline = tline->next;
2324 if (!tline || tline->type != TOK_ID) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002325 nasm_nonfatal("`%%stacksize' missing size parameter");
H. Peter Anvine2c80182005-01-15 22:15:51 +00002326 free_tlist(origline);
2327 return DIRECTIVE_FOUND;
2328 }
2329 if (nasm_stricmp(tline->text, "flat") == 0) {
2330 /* All subsequent ARG directives are for a 32-bit stack */
2331 StackSize = 4;
2332 StackPointer = "ebp";
2333 ArgOffset = 8;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002334 LocalOffset = 0;
Charles Crayne7eaf9192007-11-08 22:11:14 -08002335 } else if (nasm_stricmp(tline->text, "flat64") == 0) {
2336 /* All subsequent ARG directives are for a 64-bit stack */
2337 StackSize = 8;
2338 StackPointer = "rbp";
Per Jessen53252e02010-02-11 00:16:59 +03002339 ArgOffset = 16;
Charles Crayne7eaf9192007-11-08 22:11:14 -08002340 LocalOffset = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002341 } else if (nasm_stricmp(tline->text, "large") == 0) {
2342 /* All subsequent ARG directives are for a 16-bit stack,
2343 * far function call.
2344 */
2345 StackSize = 2;
2346 StackPointer = "bp";
2347 ArgOffset = 4;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002348 LocalOffset = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002349 } else if (nasm_stricmp(tline->text, "small") == 0) {
2350 /* All subsequent ARG directives are for a 16-bit stack,
2351 * far function call. We don't support near functions.
2352 */
2353 StackSize = 2;
2354 StackPointer = "bp";
2355 ArgOffset = 6;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002356 LocalOffset = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002357 } else {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002358 nasm_nonfatal("`%%stacksize' invalid size type");
H. Peter Anvine2c80182005-01-15 22:15:51 +00002359 free_tlist(origline);
2360 return DIRECTIVE_FOUND;
2361 }
2362 free_tlist(origline);
2363 return DIRECTIVE_FOUND;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002364
H. Peter Anvine2c80182005-01-15 22:15:51 +00002365 case PP_ARG:
2366 /* TASM like ARG directive to define arguments to functions, in
2367 * the following form:
2368 *
2369 * ARG arg1:WORD, arg2:DWORD, arg4:QWORD
2370 */
2371 offset = ArgOffset;
2372 do {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00002373 char *arg, directive[256];
H. Peter Anvine2c80182005-01-15 22:15:51 +00002374 int size = StackSize;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002375
H. Peter Anvine2c80182005-01-15 22:15:51 +00002376 /* Find the argument name */
2377 tline = tline->next;
2378 if (tline && tline->type == TOK_WHITESPACE)
2379 tline = tline->next;
2380 if (!tline || tline->type != TOK_ID) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002381 nasm_nonfatal("`%%arg' missing argument parameter");
H. Peter Anvine2c80182005-01-15 22:15:51 +00002382 free_tlist(origline);
2383 return DIRECTIVE_FOUND;
2384 }
2385 arg = tline->text;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002386
H. Peter Anvine2c80182005-01-15 22:15:51 +00002387 /* Find the argument size type */
2388 tline = tline->next;
2389 if (!tline || tline->type != TOK_OTHER
2390 || tline->text[0] != ':') {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002391 nasm_nonfatal("Syntax error processing `%%arg' directive");
H. Peter Anvine2c80182005-01-15 22:15:51 +00002392 free_tlist(origline);
2393 return DIRECTIVE_FOUND;
2394 }
2395 tline = tline->next;
2396 if (!tline || tline->type != TOK_ID) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002397 nasm_nonfatal("`%%arg' missing size type parameter");
H. Peter Anvine2c80182005-01-15 22:15:51 +00002398 free_tlist(origline);
2399 return DIRECTIVE_FOUND;
2400 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002401
H. Peter Anvine2c80182005-01-15 22:15:51 +00002402 /* Allow macro expansion of type parameter */
Keith Kaniosb7a89542007-04-12 02:40:54 +00002403 tt = tokenize(tline->text);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002404 tt = expand_smacro(tt);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002405 size = parse_size(tt->text);
2406 if (!size) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002407 nasm_nonfatal("Invalid size type for `%%arg' missing directive");
H. Peter Anvine2c80182005-01-15 22:15:51 +00002408 free_tlist(tt);
2409 free_tlist(origline);
2410 return DIRECTIVE_FOUND;
2411 }
2412 free_tlist(tt);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002413
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002414 /* Round up to even stack slots */
2415 size = ALIGN(size, StackSize);
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002416
H. Peter Anvine2c80182005-01-15 22:15:51 +00002417 /* Now define the macro for the argument */
2418 snprintf(directive, sizeof(directive), "%%define %s (%s+%d)",
2419 arg, StackPointer, offset);
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07002420 do_directive(tokenize(directive), output);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002421 offset += size;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002422
H. Peter Anvine2c80182005-01-15 22:15:51 +00002423 /* Move to the next argument in the list */
2424 tline = tline->next;
2425 if (tline && tline->type == TOK_WHITESPACE)
2426 tline = tline->next;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002427 } while (tline && tline->type == TOK_OTHER && tline->text[0] == ',');
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002428 ArgOffset = offset;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002429 free_tlist(origline);
2430 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002431
H. Peter Anvine2c80182005-01-15 22:15:51 +00002432 case PP_LOCAL:
2433 /* TASM like LOCAL directive to define local variables for a
2434 * function, in the following form:
2435 *
2436 * LOCAL local1:WORD, local2:DWORD, local4:QWORD = LocalSize
2437 *
2438 * The '= LocalSize' at the end is ignored by NASM, but is
2439 * required by TASM to define the local parameter size (and used
2440 * by the TASM macro package).
2441 */
2442 offset = LocalOffset;
2443 do {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00002444 char *local, directive[256];
H. Peter Anvine2c80182005-01-15 22:15:51 +00002445 int size = StackSize;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002446
H. Peter Anvine2c80182005-01-15 22:15:51 +00002447 /* Find the argument name */
2448 tline = tline->next;
2449 if (tline && tline->type == TOK_WHITESPACE)
2450 tline = tline->next;
2451 if (!tline || tline->type != TOK_ID) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002452 nasm_nonfatal("`%%local' missing argument parameter");
H. Peter Anvine2c80182005-01-15 22:15:51 +00002453 free_tlist(origline);
2454 return DIRECTIVE_FOUND;
2455 }
2456 local = tline->text;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002457
H. Peter Anvine2c80182005-01-15 22:15:51 +00002458 /* Find the argument size type */
2459 tline = tline->next;
2460 if (!tline || tline->type != TOK_OTHER
2461 || tline->text[0] != ':') {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002462 nasm_nonfatal("Syntax error processing `%%local' directive");
H. Peter Anvine2c80182005-01-15 22:15:51 +00002463 free_tlist(origline);
2464 return DIRECTIVE_FOUND;
2465 }
2466 tline = tline->next;
2467 if (!tline || tline->type != TOK_ID) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002468 nasm_nonfatal("`%%local' missing size type parameter");
H. Peter Anvine2c80182005-01-15 22:15:51 +00002469 free_tlist(origline);
2470 return DIRECTIVE_FOUND;
2471 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00002472
H. Peter Anvine2c80182005-01-15 22:15:51 +00002473 /* Allow macro expansion of type parameter */
Keith Kaniosb7a89542007-04-12 02:40:54 +00002474 tt = tokenize(tline->text);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002475 tt = expand_smacro(tt);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002476 size = parse_size(tt->text);
2477 if (!size) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002478 nasm_nonfatal("Invalid size type for `%%local' missing directive");
H. Peter Anvine2c80182005-01-15 22:15:51 +00002479 free_tlist(tt);
2480 free_tlist(origline);
2481 return DIRECTIVE_FOUND;
2482 }
2483 free_tlist(tt);
H. Peter Anvin734b1882002-04-30 21:01:08 +00002484
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002485 /* Round up to even stack slots */
2486 size = ALIGN(size, StackSize);
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002487
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002488 offset += size; /* Negative offset, increment before */
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002489
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002490 /* Now define the macro for the argument */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002491 snprintf(directive, sizeof(directive), "%%define %s (%s-%d)",
2492 local, StackPointer, offset);
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07002493 do_directive(tokenize(directive), output);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002494
H. Peter Anvine2c80182005-01-15 22:15:51 +00002495 /* Now define the assign to setup the enter_c macro correctly */
2496 snprintf(directive, sizeof(directive),
2497 "%%assign %%$localsize %%$localsize+%d", size);
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07002498 do_directive(tokenize(directive), output);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002499
H. Peter Anvine2c80182005-01-15 22:15:51 +00002500 /* Move to the next argument in the list */
2501 tline = tline->next;
2502 if (tline && tline->type == TOK_WHITESPACE)
2503 tline = tline->next;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002504 } while (tline && tline->type == TOK_OTHER && tline->text[0] == ',');
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002505 LocalOffset = offset;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002506 free_tlist(origline);
2507 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002508
H. Peter Anvine2c80182005-01-15 22:15:51 +00002509 case PP_CLEAR:
2510 if (tline->next)
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002511 nasm_warnf(ERR_PASS1, "trailing garbage after `%%clear' ignored");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002512 free_macros();
2513 init_macros();
H. Peter Anvine2c80182005-01-15 22:15:51 +00002514 free_tlist(origline);
2515 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002516
H. Peter Anvin418ca702008-05-30 10:42:30 -07002517 case PP_DEPEND:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002518 t = tline->next = expand_smacro(tline->next);
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002519 skip_white_(t);
2520 if (!t || (t->type != TOK_STRING &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002521 t->type != TOK_INTERNAL_STRING)) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002522 nasm_nonfatal("`%%depend' expects a file name");
H. Peter Anvin418ca702008-05-30 10:42:30 -07002523 free_tlist(origline);
2524 return DIRECTIVE_FOUND; /* but we did _something_ */
2525 }
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002526 if (t->next)
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002527 nasm_warnf(ERR_PASS1, "trailing garbage after `%%depend' ignored");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002528 p = t->text;
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002529 if (t->type != TOK_INTERNAL_STRING)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002530 nasm_unquote_cstr(p, i);
Cyrill Gorcunovb7bb5ac2018-11-11 21:33:52 +03002531 strlist_add(deplist, p);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002532 free_tlist(origline);
H. Peter Anvin418ca702008-05-30 10:42:30 -07002533 return DIRECTIVE_FOUND;
2534
2535 case PP_INCLUDE:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002536 t = tline->next = expand_smacro(tline->next);
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002537 skip_white_(t);
H. Peter Anvind2456592008-06-19 15:04:18 -07002538
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002539 if (!t || (t->type != TOK_STRING &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002540 t->type != TOK_INTERNAL_STRING)) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002541 nasm_nonfatal("`%%include' expects a file name");
H. Peter Anvine2c80182005-01-15 22:15:51 +00002542 free_tlist(origline);
2543 return DIRECTIVE_FOUND; /* but we did _something_ */
2544 }
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002545 if (t->next)
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002546 nasm_warnf(ERR_PASS1, "trailing garbage after `%%include' ignored");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002547 p = t->text;
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002548 if (t->type != TOK_INTERNAL_STRING)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002549 nasm_unquote_cstr(p, i);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002550 inc = nasm_malloc(sizeof(Include));
H. Peter Anvine2c80182005-01-15 22:15:51 +00002551 inc->next = istk;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002552 inc->conds = NULL;
Jim Kukunas65a8afc2016-06-13 16:00:42 -04002553 found_path = NULL;
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07002554 inc->fp = inc_fopen(p, deplist, &found_path,
H. Peter Anvind81a2352016-09-21 14:03:18 -07002555 pass == 0 ? INC_OPTIONAL : INC_NEEDED, NF_TEXT);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002556 if (!inc->fp) {
2557 /* -MG given but file not found */
2558 nasm_free(inc);
2559 } else {
Jim Kukunas65a8afc2016-06-13 16:00:42 -04002560 inc->fname = src_set_fname(found_path ? found_path : p);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002561 inc->lineno = src_set_linnum(0);
2562 inc->lineinc = 1;
2563 inc->expansion = NULL;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002564 inc->mstk = NULL;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002565 istk = inc;
H. Peter Anvin8ac25aa2016-02-18 01:16:18 -08002566 lfmt->uplevel(LIST_INCLUDE);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002567 }
2568 free_tlist(origline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002569 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002570
H. Peter Anvind2456592008-06-19 15:04:18 -07002571 case PP_USE:
H. Peter Anvinf4ae5ad2008-06-19 18:39:24 -07002572 {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002573 static macros_t *use_pkg;
2574 const char *pkg_macro = NULL;
H. Peter Anvinf4ae5ad2008-06-19 18:39:24 -07002575
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002576 tline = tline->next;
2577 skip_white_(tline);
2578 tline = expand_id(tline);
H. Peter Anvind2456592008-06-19 15:04:18 -07002579
H. Peter Anvin264b7b92008-10-24 16:38:17 -07002580 if (!tline || (tline->type != TOK_STRING &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002581 tline->type != TOK_INTERNAL_STRING &&
2582 tline->type != TOK_ID)) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002583 nasm_nonfatal("`%%use' expects a package name");
H. Peter Anvind2456592008-06-19 15:04:18 -07002584 free_tlist(origline);
2585 return DIRECTIVE_FOUND; /* but we did _something_ */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002586 }
H. Peter Anvin264b7b92008-10-24 16:38:17 -07002587 if (tline->next)
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002588 nasm_warnf(ERR_PASS1, "trailing garbage after `%%use' ignored");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002589 if (tline->type == TOK_STRING)
2590 nasm_unquote_cstr(tline->text, i);
2591 use_pkg = nasm_stdmac_find_package(tline->text);
2592 if (!use_pkg)
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002593 nasm_nonfatal("unknown `%%use' package: %s", tline->text);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002594 else
2595 pkg_macro = (char *)use_pkg + 1; /* The first string will be <%define>__USE_*__ */
Victor van den Elzen35eb2ea2010-03-10 22:33:48 +01002596 if (use_pkg && ! smacro_defined(NULL, pkg_macro, 0, NULL, true)) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002597 /* Not already included, go ahead and include it */
2598 stdmacpos = use_pkg;
2599 }
2600 free_tlist(origline);
H. Peter Anvind2456592008-06-19 15:04:18 -07002601 return DIRECTIVE_FOUND;
H. Peter Anvinf4ae5ad2008-06-19 18:39:24 -07002602 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002603 case PP_PUSH:
H. Peter Anvine2c80182005-01-15 22:15:51 +00002604 case PP_REPL:
H. Peter Anvin42b56392008-10-24 16:24:21 -07002605 case PP_POP:
H. Peter Anvine2c80182005-01-15 22:15:51 +00002606 tline = tline->next;
2607 skip_white_(tline);
2608 tline = expand_id(tline);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002609 if (tline) {
2610 if (!tok_type_(tline, TOK_ID)) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002611 nasm_nonfatal("`%s' expects a context identifier",
2612 pp_directives[i]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002613 free_tlist(origline);
2614 return DIRECTIVE_FOUND; /* but we did _something_ */
2615 }
2616 if (tline->next)
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002617 nasm_warnf(ERR_PASS1, "trailing garbage after `%s' ignored",
2618 pp_directives[i]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002619 p = nasm_strdup(tline->text);
2620 } else {
2621 p = NULL; /* Anonymous */
2622 }
H. Peter Anvin42b56392008-10-24 16:24:21 -07002623
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002624 if (i == PP_PUSH) {
H. Peter Anvin (Intel)ebb05a02018-12-11 12:30:25 -08002625 nasm_new(ctx);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002626 ctx->next = cstk;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002627 ctx->name = p;
2628 ctx->number = unique++;
2629 cstk = ctx;
2630 } else {
2631 /* %pop or %repl */
2632 if (!cstk) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002633 nasm_nonfatal("`%s': context stack is empty",
2634 pp_directives[i]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002635 } else if (i == PP_POP) {
2636 if (p && (!cstk->name || nasm_stricmp(p, cstk->name)))
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002637 nasm_nonfatal("`%%pop' in wrong context: %s, expected %s",
2638 cstk->name ? cstk->name : "anonymous", p);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002639 else
2640 ctx_pop();
2641 } else {
2642 /* i == PP_REPL */
2643 nasm_free(cstk->name);
2644 cstk->name = p;
2645 p = NULL;
2646 }
2647 nasm_free(p);
2648 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002649 free_tlist(origline);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002650 return DIRECTIVE_FOUND;
H. Peter Anvin8e3f75e2008-09-24 00:21:58 -07002651 case PP_FATAL:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002652 severity = ERR_FATAL;
2653 goto issue_error;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002654 case PP_ERROR:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002655 severity = ERR_NONFATAL;
2656 goto issue_error;
H. Peter Anvin7df04172008-06-10 18:27:38 -07002657 case PP_WARNING:
H. Peter Anvin (Intel)df4d3422018-12-12 17:48:38 -08002658 severity = ERR_WARNING|WARN_USER;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002659 goto issue_error;
H. Peter Anvin8e3f75e2008-09-24 00:21:58 -07002660
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002661issue_error:
H. Peter Anvin7df04172008-06-10 18:27:38 -07002662 {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002663 /* Only error out if this is the final pass */
2664 if (pass != 2 && i != PP_FATAL)
2665 return DIRECTIVE_FOUND;
2666
2667 tline->next = expand_smacro(tline->next);
2668 tline = tline->next;
2669 skip_white_(tline);
2670 t = tline ? tline->next : NULL;
2671 skip_white_(t);
2672 if (tok_type_(tline, TOK_STRING) && !t) {
2673 /* The line contains only a quoted string */
2674 p = tline->text;
2675 nasm_unquote(p, NULL); /* Ignore NUL character truncation */
H. Peter Anvin130736c2016-02-17 20:27:41 -08002676 nasm_error(severity, "%s", p);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002677 } else {
2678 /* Not a quoted string, or more than a quoted string */
2679 p = detoken(tline, false);
H. Peter Anvin130736c2016-02-17 20:27:41 -08002680 nasm_error(severity, "%s", p);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002681 nasm_free(p);
2682 }
2683 free_tlist(origline);
2684 return DIRECTIVE_FOUND;
H. Peter Anvin7df04172008-06-10 18:27:38 -07002685 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00002686
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002687 CASE_PP_IF:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002688 if (istk->conds && !emitting(istk->conds->state))
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002689 j = COND_NEVER;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002690 else {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002691 j = if_condition(tline->next, i);
2692 tline->next = NULL; /* it got freed */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002693 j = j < 0 ? COND_NEVER : j ? COND_IF_TRUE : COND_IF_FALSE;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002694 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002695 cond = nasm_malloc(sizeof(Cond));
2696 cond->next = istk->conds;
2697 cond->state = j;
2698 istk->conds = cond;
2699 if(istk->mstk)
2700 istk->mstk->condcnt ++;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002701 free_tlist(origline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002702 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002703
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002704 CASE_PP_ELIF:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002705 if (!istk->conds)
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002706 nasm_fatal("`%s': no matching `%%if'", pp_directives[i]);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002707 switch(istk->conds->state) {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002708 case COND_IF_TRUE:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002709 istk->conds->state = COND_DONE;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002710 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002711
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002712 case COND_DONE:
2713 case COND_NEVER:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002714 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002715
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002716 case COND_ELSE_TRUE:
2717 case COND_ELSE_FALSE:
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002718 nasm_warnf(ERR_PASS1|ERR_PP_PRECOND,
2719 "`%%elif' after `%%else' ignored");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002720 istk->conds->state = COND_NEVER;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002721 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002722
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002723 case COND_IF_FALSE:
2724 /*
2725 * IMPORTANT: In the case of %if, we will already have
2726 * called expand_mmac_params(); however, if we're
2727 * processing an %elif we must have been in a
2728 * non-emitting mode, which would have inhibited
2729 * the normal invocation of expand_mmac_params().
2730 * Therefore, we have to do it explicitly here.
2731 */
2732 j = if_condition(expand_mmac_params(tline->next), i);
2733 tline->next = NULL; /* it got freed */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002734 istk->conds->state =
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002735 j < 0 ? COND_NEVER : j ? COND_IF_TRUE : COND_IF_FALSE;
2736 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002737 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002738 free_tlist(origline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002739 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002740
H. Peter Anvine2c80182005-01-15 22:15:51 +00002741 case PP_ELSE:
2742 if (tline->next)
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002743 nasm_warnf(ERR_PASS1|ERR_PP_PRECOND,
2744 "trailing garbage after `%%else' ignored");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002745 if (!istk->conds)
H. Peter Anvinc5136902018-06-15 18:20:17 -07002746 nasm_fatal("`%%else: no matching `%%if'");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002747 switch(istk->conds->state) {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002748 case COND_IF_TRUE:
2749 case COND_DONE:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002750 istk->conds->state = COND_ELSE_FALSE;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002751 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002752
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002753 case COND_NEVER:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002754 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002755
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002756 case COND_IF_FALSE:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002757 istk->conds->state = COND_ELSE_TRUE;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002758 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002759
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002760 case COND_ELSE_TRUE:
2761 case COND_ELSE_FALSE:
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002762 nasm_warnf(ERR_PASS1|ERR_PP_PRECOND,
2763 "`%%else' after `%%else' ignored.");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002764 istk->conds->state = COND_NEVER;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002765 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002766 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002767 free_tlist(origline);
2768 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002769
H. Peter Anvine2c80182005-01-15 22:15:51 +00002770 case PP_ENDIF:
2771 if (tline->next)
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002772 nasm_warnf(ERR_PASS1|ERR_PP_PRECOND,
2773 "trailing garbage after `%%endif' ignored");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002774 if (!istk->conds)
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002775 nasm_fatal("`%%endif': no matching `%%if'");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002776 cond = istk->conds;
2777 istk->conds = cond->next;
2778 nasm_free(cond);
2779 if(istk->mstk)
2780 istk->mstk->condcnt --;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002781 free_tlist(origline);
2782 return DIRECTIVE_FOUND;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002783
H. Peter Anvindb8f96e2009-07-15 09:07:29 -04002784 case PP_RMACRO:
2785 case PP_IRMACRO:
H. Peter Anvine2c80182005-01-15 22:15:51 +00002786 case PP_MACRO:
2787 case PP_IMACRO:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002788 if (defining) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002789 nasm_fatal("`%s': already defining a macro",
2790 pp_directives[i]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002791 return DIRECTIVE_FOUND;
2792 }
H. Peter Anvin4def1a82016-05-09 13:59:44 -07002793 defining = nasm_zalloc(sizeof(MMacro));
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07002794 defining->max_depth = ((i == PP_RMACRO) || (i == PP_IRMACRO))
2795 ? nasm_limit[LIMIT_MACROS] : 0;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002796 defining->casesense = (i == PP_MACRO) || (i == PP_RMACRO);
2797 if (!parse_mmacro_spec(tline, defining, pp_directives[i])) {
2798 nasm_free(defining);
2799 defining = NULL;
2800 return DIRECTIVE_FOUND;
2801 }
H. Peter Anvina26433d2008-07-16 14:40:01 -07002802
H. Peter Anvin4def1a82016-05-09 13:59:44 -07002803 src_get(&defining->xline, &defining->fname);
2804
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002805 mmac = (MMacro *) hash_findix(&mmacros, defining->name);
2806 while (mmac) {
2807 if (!strcmp(mmac->name, defining->name) &&
2808 (mmac->nparam_min <= defining->nparam_max
2809 || defining->plus)
2810 && (defining->nparam_min <= mmac->nparam_max
2811 || mmac->plus)) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002812 nasm_warnf(ERR_PASS1, "redefining multi-line macro `%s'",
2813 defining->name);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002814 return DIRECTIVE_FOUND;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002815 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002816 mmac = mmac->next;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002817 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002818 free_tlist(origline);
2819 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002820
H. Peter Anvine2c80182005-01-15 22:15:51 +00002821 case PP_ENDM:
2822 case PP_ENDMACRO:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002823 if (! (defining && defining->name)) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002824 nasm_nonfatal("`%s': not defining a macro", tline->text);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002825 return DIRECTIVE_FOUND;
2826 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002827 mmhead = (MMacro **) hash_findi_add(&mmacros, defining->name);
2828 defining->next = *mmhead;
2829 *mmhead = defining;
2830 defining = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002831 free_tlist(origline);
2832 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002833
H. Peter Anvin89cee572009-07-15 09:16:54 -04002834 case PP_EXITMACRO:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002835 /*
2836 * We must search along istk->expansion until we hit a
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002837 * macro-end marker for a macro with a name. Then we
2838 * bypass all lines between exitmacro and endmacro.
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002839 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002840 list_for_each(l, istk->expansion)
2841 if (l->finishes && l->finishes->name)
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002842 break;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002843
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002844 if (l) {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002845 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002846 * Remove all conditional entries relative to this
2847 * macro invocation. (safe to do in this context)
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002848 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002849 for ( ; l->finishes->condcnt > 0; l->finishes->condcnt --) {
2850 cond = istk->conds;
2851 istk->conds = cond->next;
2852 nasm_free(cond);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002853 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002854 istk->expansion = l;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002855 } else {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002856 nasm_nonfatal("`%%exitmacro' not within `%%macro' block");
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002857 }
Keith Kanios852f1ee2009-07-12 00:19:55 -05002858 free_tlist(origline);
2859 return DIRECTIVE_FOUND;
2860
H. Peter Anvina26433d2008-07-16 14:40:01 -07002861 case PP_UNMACRO:
2862 case PP_UNIMACRO:
2863 {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002864 MMacro **mmac_p;
2865 MMacro spec;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002866
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002867 spec.casesense = (i == PP_UNMACRO);
2868 if (!parse_mmacro_spec(tline, &spec, pp_directives[i])) {
2869 return DIRECTIVE_FOUND;
2870 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002871 mmac_p = (MMacro **) hash_findi(&mmacros, spec.name, NULL);
2872 while (mmac_p && *mmac_p) {
2873 mmac = *mmac_p;
2874 if (mmac->casesense == spec.casesense &&
2875 !mstrcmp(mmac->name, spec.name, spec.casesense) &&
2876 mmac->nparam_min == spec.nparam_min &&
2877 mmac->nparam_max == spec.nparam_max &&
2878 mmac->plus == spec.plus) {
2879 *mmac_p = mmac->next;
2880 free_mmacro(mmac);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002881 } else {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002882 mmac_p = &mmac->next;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002883 }
2884 }
2885 free_tlist(origline);
2886 free_tlist(spec.dlist);
2887 return DIRECTIVE_FOUND;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002888 }
2889
H. Peter Anvine2c80182005-01-15 22:15:51 +00002890 case PP_ROTATE:
2891 if (tline->next && tline->next->type == TOK_WHITESPACE)
2892 tline = tline->next;
H. Peter Anvin89cee572009-07-15 09:16:54 -04002893 if (!tline->next) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002894 free_tlist(origline);
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002895 nasm_nonfatal("`%%rotate' missing rotate count");
H. Peter Anvine2c80182005-01-15 22:15:51 +00002896 return DIRECTIVE_FOUND;
2897 }
2898 t = expand_smacro(tline->next);
2899 tline->next = NULL;
2900 free_tlist(origline);
2901 tline = t;
2902 tptr = &t;
2903 tokval.t_type = TOKEN_INVALID;
2904 evalresult =
H. Peter Anvin130736c2016-02-17 20:27:41 -08002905 evaluate(ppscan, tptr, &tokval, NULL, pass, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002906 free_tlist(tline);
2907 if (!evalresult)
2908 return DIRECTIVE_FOUND;
2909 if (tokval.t_type)
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002910 nasm_warnf(ERR_PASS1, "trailing garbage after expression ignored");
H. Peter Anvine2c80182005-01-15 22:15:51 +00002911 if (!is_simple(evalresult)) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002912 nasm_nonfatal("non-constant value given to `%%rotate'");
H. Peter Anvine2c80182005-01-15 22:15:51 +00002913 return DIRECTIVE_FOUND;
2914 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002915 mmac = istk->mstk;
2916 while (mmac && !mmac->name) /* avoid mistaking %reps for macros */
2917 mmac = mmac->next_active;
2918 if (!mmac) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002919 nasm_nonfatal("`%%rotate' invoked outside a macro call");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002920 } else if (mmac->nparam == 0) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002921 nasm_nonfatal("`%%rotate' invoked within macro without parameters");
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002922 } else {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002923 int rotate = mmac->rotate + reloc_value(evalresult);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002924
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002925 rotate %= (int)mmac->nparam;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002926 if (rotate < 0)
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002927 rotate += mmac->nparam;
2928
2929 mmac->rotate = rotate;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002930 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002931 return DIRECTIVE_FOUND;
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00002932
H. Peter Anvine2c80182005-01-15 22:15:51 +00002933 case PP_REP:
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002934 nolist = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002935 do {
2936 tline = tline->next;
2937 } while (tok_type_(tline, TOK_WHITESPACE));
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00002938
H. Peter Anvine2c80182005-01-15 22:15:51 +00002939 if (tok_type_(tline, TOK_ID) &&
2940 nasm_stricmp(tline->text, ".nolist") == 0) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002941 nolist = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002942 do {
2943 tline = tline->next;
2944 } while (tok_type_(tline, TOK_WHITESPACE));
2945 }
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00002946
H. Peter Anvine2c80182005-01-15 22:15:51 +00002947 if (tline) {
2948 t = expand_smacro(tline);
2949 tptr = &t;
2950 tokval.t_type = TOKEN_INVALID;
2951 evalresult =
H. Peter Anvin130736c2016-02-17 20:27:41 -08002952 evaluate(ppscan, tptr, &tokval, NULL, pass, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002953 if (!evalresult) {
2954 free_tlist(origline);
2955 return DIRECTIVE_FOUND;
2956 }
2957 if (tokval.t_type)
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002958 nasm_warnf(ERR_PASS1, "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 `%%rep'");
H. Peter Anvine2c80182005-01-15 22:15:51 +00002961 return DIRECTIVE_FOUND;
2962 }
Cyrill Gorcunove091d6e2010-08-09 13:58:22 +04002963 count = reloc_value(evalresult);
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07002964 if (count > nasm_limit[LIMIT_REP]) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002965 nasm_nonfatal("`%%rep' count %"PRId64" exceeds limit (currently %"PRId64")",
2966 count, nasm_limit[LIMIT_REP]);
Cyrill Gorcunove091d6e2010-08-09 13:58:22 +04002967 count = 0;
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07002968 } else if (count < 0) {
H. Peter Anvin (Intel)fef75c22018-12-12 18:13:47 -08002969 nasm_warnf(ERR_PASS2|WARN_NEGATIVE_REP,
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07002970 "negative `%%rep' count: %"PRId64, count);
2971 count = 0;
2972 } else {
Cyrill Gorcunove091d6e2010-08-09 13:58:22 +04002973 count++;
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07002974 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002975 } else {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002976 nasm_nonfatal("`%%rep' expects a repeat count");
H. Peter Anvinf8ba53e2007-10-11 10:11:57 -07002977 count = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002978 }
2979 free_tlist(origline);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002980
2981 tmp_defining = defining;
2982 defining = nasm_malloc(sizeof(MMacro));
2983 defining->prev = NULL;
2984 defining->name = NULL; /* flags this macro as a %rep block */
2985 defining->casesense = false;
2986 defining->plus = false;
2987 defining->nolist = nolist;
2988 defining->in_progress = count;
2989 defining->max_depth = 0;
2990 defining->nparam_min = defining->nparam_max = 0;
2991 defining->defaults = NULL;
2992 defining->dlist = NULL;
2993 defining->expansion = NULL;
2994 defining->next_active = istk->mstk;
2995 defining->rep_nest = tmp_defining;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002996 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002997
H. Peter Anvine2c80182005-01-15 22:15:51 +00002998 case PP_ENDREP:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002999 if (!defining || defining->name) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003000 nasm_nonfatal("`%%endrep': no matching `%%rep'");
H. Peter Anvine2c80182005-01-15 22:15:51 +00003001 return DIRECTIVE_FOUND;
3002 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003003
H. Peter Anvine2c80182005-01-15 22:15:51 +00003004 /*
3005 * Now we have a "macro" defined - although it has no name
3006 * and we won't be entering it in the hash tables - we must
3007 * push a macro-end marker for it on to istk->expansion.
3008 * After that, it will take care of propagating itself (a
3009 * macro-end marker line for a macro which is really a %rep
3010 * block will cause the macro to be re-expanded, complete
3011 * with another macro-end marker to ensure the process
3012 * continues) until the whole expansion is forcibly removed
3013 * from istk->expansion by a %exitrep.
3014 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003015 l = nasm_malloc(sizeof(Line));
3016 l->next = istk->expansion;
3017 l->finishes = defining;
3018 l->first = NULL;
3019 istk->expansion = l;
3020
3021 istk->mstk = defining;
3022
H. Peter Anvin8ac25aa2016-02-18 01:16:18 -08003023 lfmt->uplevel(defining->nolist ? LIST_MACRO_NOLIST : LIST_MACRO);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003024 tmp_defining = defining;
3025 defining = defining->rep_nest;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003026 free_tlist(origline);
3027 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003028
H. Peter Anvine2c80182005-01-15 22:15:51 +00003029 case PP_EXITREP:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003030 /*
3031 * We must search along istk->expansion until we hit a
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003032 * macro-end marker for a macro with no name. Then we set
3033 * its `in_progress' flag to 0.
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003034 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003035 list_for_each(l, istk->expansion)
3036 if (l->finishes && !l->finishes->name)
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003037 break;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003038
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003039 if (l)
3040 l->finishes->in_progress = 1;
3041 else
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003042 nasm_nonfatal("`%%exitrep' not within `%%rep' block");
H. Peter Anvine2c80182005-01-15 22:15:51 +00003043 free_tlist(origline);
3044 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003045
H. Peter Anvine2c80182005-01-15 22:15:51 +00003046 case PP_XDEFINE:
3047 case PP_IXDEFINE:
3048 case PP_DEFINE:
3049 case PP_IDEFINE:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003050 casesense = (i == PP_DEFINE || i == PP_XDEFINE);
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07003051
H. Peter Anvine2c80182005-01-15 22:15:51 +00003052 tline = tline->next;
3053 skip_white_(tline);
3054 tline = expand_id(tline);
3055 if (!tline || (tline->type != TOK_ID &&
3056 (tline->type != TOK_PREPROC_ID ||
3057 tline->text[1] != '$'))) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003058 nasm_nonfatal("`%s' expects a macro identifier",
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003059 pp_directives[i]);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003060 free_tlist(origline);
3061 return DIRECTIVE_FOUND;
3062 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003063
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04003064 ctx = get_ctx(tline->text, &mname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003065 last = tline;
3066 param_start = tline = tline->next;
3067 nparam = 0;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003068
H. Peter Anvine2c80182005-01-15 22:15:51 +00003069 /* Expand the macro definition now for %xdefine and %ixdefine */
3070 if ((i == PP_XDEFINE) || (i == PP_IXDEFINE))
3071 tline = expand_smacro(tline);
H. Peter Anvin734b1882002-04-30 21:01:08 +00003072
H. Peter Anvine2c80182005-01-15 22:15:51 +00003073 if (tok_is_(tline, "(")) {
3074 /*
3075 * This macro has parameters.
3076 */
H. Peter Anvin734b1882002-04-30 21:01:08 +00003077
H. Peter Anvine2c80182005-01-15 22:15:51 +00003078 tline = tline->next;
3079 while (1) {
3080 skip_white_(tline);
3081 if (!tline) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003082 nasm_nonfatal("parameter identifier expected");
H. Peter Anvine2c80182005-01-15 22:15:51 +00003083 free_tlist(origline);
3084 return DIRECTIVE_FOUND;
3085 }
3086 if (tline->type != TOK_ID) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003087 nasm_nonfatal("`%s': parameter identifier expected",
3088 tline->text);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003089 free_tlist(origline);
3090 return DIRECTIVE_FOUND;
3091 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003092 tline->type = TOK_SMAC_PARAM + nparam++;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003093 tline = tline->next;
3094 skip_white_(tline);
3095 if (tok_is_(tline, ",")) {
3096 tline = tline->next;
H. Peter Anvinca348b62008-07-23 10:49:26 -04003097 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003098 if (!tok_is_(tline, ")")) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003099 nasm_nonfatal("`)' expected to terminate macro template");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003100 free_tlist(origline);
3101 return DIRECTIVE_FOUND;
3102 }
3103 break;
3104 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00003105 }
3106 last = tline;
3107 tline = tline->next;
3108 }
3109 if (tok_type_(tline, TOK_WHITESPACE))
3110 last = tline, tline = tline->next;
3111 macro_start = NULL;
3112 last->next = NULL;
3113 t = tline;
3114 while (t) {
3115 if (t->type == TOK_ID) {
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04003116 list_for_each(tt, param_start)
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003117 if (tt->type >= TOK_SMAC_PARAM &&
H. Peter Anvine2c80182005-01-15 22:15:51 +00003118 !strcmp(tt->text, t->text))
3119 t->type = tt->type;
3120 }
3121 tt = t->next;
3122 t->next = macro_start;
3123 macro_start = t;
3124 t = tt;
3125 }
3126 /*
3127 * Good. We now have a macro name, a parameter count, and a
3128 * token list (in reverse order) for an expansion. We ought
3129 * to be OK just to create an SMacro, store it, and let
3130 * free_tlist have the rest of the line (which we have
3131 * carefully re-terminated after chopping off the expansion
3132 * from the end).
3133 */
H. Peter Anvin4db5a162007-10-11 13:42:09 -07003134 define_smacro(ctx, mname, casesense, nparam, macro_start);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003135 free_tlist(origline);
3136 return DIRECTIVE_FOUND;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003137
H. Peter Anvine2c80182005-01-15 22:15:51 +00003138 case PP_UNDEF:
3139 tline = tline->next;
3140 skip_white_(tline);
3141 tline = expand_id(tline);
3142 if (!tline || (tline->type != TOK_ID &&
3143 (tline->type != TOK_PREPROC_ID ||
3144 tline->text[1] != '$'))) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003145 nasm_nonfatal("`%%undef' expects a macro identifier");
H. Peter Anvine2c80182005-01-15 22:15:51 +00003146 free_tlist(origline);
3147 return DIRECTIVE_FOUND;
3148 }
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003149 if (tline->next)
3150 nasm_warnf(ERR_PASS1, "trailing garbage after macro name ignored");
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003151
H. Peter Anvine2c80182005-01-15 22:15:51 +00003152 /* Find the context that symbol belongs to */
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04003153 ctx = get_ctx(tline->text, &mname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003154 undef_smacro(ctx, mname);
3155 free_tlist(origline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003156 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003157
H. Peter Anvin9e200162008-06-04 17:23:14 -07003158 case PP_DEFSTR:
3159 case PP_IDEFSTR:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003160 casesense = (i == PP_DEFSTR);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003161
3162 tline = tline->next;
3163 skip_white_(tline);
3164 tline = expand_id(tline);
3165 if (!tline || (tline->type != TOK_ID &&
3166 (tline->type != TOK_PREPROC_ID ||
3167 tline->text[1] != '$'))) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003168 nasm_nonfatal("`%s' expects a macro identifier",
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003169 pp_directives[i]);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003170 free_tlist(origline);
3171 return DIRECTIVE_FOUND;
3172 }
3173
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04003174 ctx = get_ctx(tline->text, &mname);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003175 last = tline;
3176 tline = expand_smacro(tline->next);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003177 last->next = NULL;
H. Peter Anvin9e200162008-06-04 17:23:14 -07003178
3179 while (tok_type_(tline, TOK_WHITESPACE))
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003180 tline = delete_Token(tline);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003181
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003182 p = detoken(tline, false);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003183 macro_start = nasm_malloc(sizeof(*macro_start));
3184 macro_start->next = NULL;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003185 macro_start->text = nasm_quote(p, strlen(p));
3186 macro_start->type = TOK_STRING;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003187 macro_start->a.mac = NULL;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003188 nasm_free(p);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003189
3190 /*
3191 * We now have a macro name, an implicit parameter count of
3192 * zero, and a string token to use as an expansion. Create
3193 * and store an SMacro.
3194 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003195 define_smacro(ctx, mname, casesense, 0, macro_start);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003196 free_tlist(origline);
3197 return DIRECTIVE_FOUND;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003198
H. Peter Anvin2f55bda2009-07-14 15:04:04 -04003199 case PP_DEFTOK:
3200 case PP_IDEFTOK:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003201 casesense = (i == PP_DEFTOK);
3202
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003203 tline = tline->next;
3204 skip_white_(tline);
3205 tline = expand_id(tline);
3206 if (!tline || (tline->type != TOK_ID &&
3207 (tline->type != TOK_PREPROC_ID ||
3208 tline->text[1] != '$'))) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003209 nasm_nonfatal("`%s' expects a macro identifier as first parameter",
3210 pp_directives[i]);
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003211 free_tlist(origline);
3212 return DIRECTIVE_FOUND;
3213 }
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04003214 ctx = get_ctx(tline->text, &mname);
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003215 last = tline;
3216 tline = expand_smacro(tline->next);
3217 last->next = NULL;
3218
3219 t = tline;
3220 while (tok_type_(t, TOK_WHITESPACE))
3221 t = t->next;
3222 /* t should now point to the string */
Cyrill Gorcunov6908e582010-09-06 19:36:15 +04003223 if (!tok_type_(t, TOK_STRING)) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003224 nasm_nonfatal("`%s` requires string as second parameter",
3225 pp_directives[i]);
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003226 free_tlist(tline);
3227 free_tlist(origline);
3228 return DIRECTIVE_FOUND;
3229 }
3230
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04003231 /*
3232 * Convert the string to a token stream. Note that smacros
3233 * are stored with the token stream reversed, so we have to
3234 * reverse the output of tokenize().
3235 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003236 nasm_unquote_cstr(t->text, i);
H. Peter Anvinb40992c2010-09-15 08:57:21 -07003237 macro_start = reverse_tokens(tokenize(t->text));
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003238
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003239 /*
3240 * We now have a macro name, an implicit parameter count of
3241 * zero, and a numeric token to use as an expansion. Create
3242 * and store an SMacro.
3243 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003244 define_smacro(ctx, mname, casesense, 0, macro_start);
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003245 free_tlist(tline);
3246 free_tlist(origline);
3247 return DIRECTIVE_FOUND;
H. Peter Anvin9e200162008-06-04 17:23:14 -07003248
H. Peter Anvin418ca702008-05-30 10:42:30 -07003249 case PP_PATHSEARCH:
3250 {
H. Peter Anvinccad6f92016-10-04 00:34:35 -07003251 const char *found_path;
H. Peter Anvin418ca702008-05-30 10:42:30 -07003252
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003253 casesense = true;
H. Peter Anvin418ca702008-05-30 10:42:30 -07003254
3255 tline = tline->next;
3256 skip_white_(tline);
3257 tline = expand_id(tline);
3258 if (!tline || (tline->type != TOK_ID &&
3259 (tline->type != TOK_PREPROC_ID ||
3260 tline->text[1] != '$'))) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003261 nasm_nonfatal("`%%pathsearch' expects a macro identifier as first parameter");
H. Peter Anvin418ca702008-05-30 10:42:30 -07003262 free_tlist(origline);
3263 return DIRECTIVE_FOUND;
3264 }
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04003265 ctx = get_ctx(tline->text, &mname);
H. Peter Anvin418ca702008-05-30 10:42:30 -07003266 last = tline;
3267 tline = expand_smacro(tline->next);
3268 last->next = NULL;
3269
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003270 t = tline;
H. Peter Anvin418ca702008-05-30 10:42:30 -07003271 while (tok_type_(t, TOK_WHITESPACE))
3272 t = t->next;
3273
3274 if (!t || (t->type != TOK_STRING &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003275 t->type != TOK_INTERNAL_STRING)) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003276 nasm_nonfatal("`%%pathsearch' expects a file name");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003277 free_tlist(tline);
H. Peter Anvin418ca702008-05-30 10:42:30 -07003278 free_tlist(origline);
3279 return DIRECTIVE_FOUND; /* but we did _something_ */
3280 }
3281 if (t->next)
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003282 nasm_warnf(ERR_PASS1, "trailing garbage after `%%pathsearch' ignored");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003283 p = t->text;
H. Peter Anvin427cc912008-06-01 21:43:03 -07003284 if (t->type != TOK_INTERNAL_STRING)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003285 nasm_unquote(p, NULL);
H. Peter Anvin418ca702008-05-30 10:42:30 -07003286
H. Peter Anvin9924d1e2016-10-04 00:59:39 -07003287 inc_fopen(p, NULL, &found_path, INC_PROBE, NF_BINARY);
H. Peter Anvinccad6f92016-10-04 00:34:35 -07003288 if (!found_path)
3289 found_path = p;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003290 macro_start = nasm_malloc(sizeof(*macro_start));
3291 macro_start->next = NULL;
H. Peter Anvinccad6f92016-10-04 00:34:35 -07003292 macro_start->text = nasm_quote(found_path, strlen(found_path));
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003293 macro_start->type = TOK_STRING;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003294 macro_start->a.mac = NULL;
H. Peter Anvin418ca702008-05-30 10:42:30 -07003295
3296 /*
3297 * We now have a macro name, an implicit parameter count of
3298 * zero, and a string token to use as an expansion. Create
3299 * and store an SMacro.
3300 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003301 define_smacro(ctx, mname, casesense, 0, macro_start);
3302 free_tlist(tline);
H. Peter Anvin418ca702008-05-30 10:42:30 -07003303 free_tlist(origline);
3304 return DIRECTIVE_FOUND;
3305 }
3306
H. Peter Anvine2c80182005-01-15 22:15:51 +00003307 case PP_STRLEN:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003308 casesense = true;
H. Peter Anvin70653092007-10-19 14:42:29 -07003309
H. Peter Anvine2c80182005-01-15 22:15:51 +00003310 tline = tline->next;
3311 skip_white_(tline);
3312 tline = expand_id(tline);
3313 if (!tline || (tline->type != TOK_ID &&
3314 (tline->type != TOK_PREPROC_ID ||
3315 tline->text[1] != '$'))) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003316 nasm_nonfatal("`%%strlen' expects a macro identifier as first parameter");
H. Peter Anvine2c80182005-01-15 22:15:51 +00003317 free_tlist(origline);
3318 return DIRECTIVE_FOUND;
3319 }
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04003320 ctx = get_ctx(tline->text, &mname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003321 last = tline;
3322 tline = expand_smacro(tline->next);
3323 last->next = NULL;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003324
H. Peter Anvine2c80182005-01-15 22:15:51 +00003325 t = tline;
3326 while (tok_type_(t, TOK_WHITESPACE))
3327 t = t->next;
3328 /* t should now point to the string */
Cyrill Gorcunov4e1d5ab2010-07-23 18:51:51 +04003329 if (!tok_type_(t, TOK_STRING)) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003330 nasm_nonfatal("`%%strlen` requires string as second parameter");
H. Peter Anvine2c80182005-01-15 22:15:51 +00003331 free_tlist(tline);
3332 free_tlist(origline);
3333 return DIRECTIVE_FOUND;
3334 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003335
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003336 macro_start = nasm_malloc(sizeof(*macro_start));
3337 macro_start->next = NULL;
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07003338 make_tok_num(macro_start, nasm_unquote(t->text, NULL));
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003339 macro_start->a.mac = NULL;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003340
H. Peter Anvine2c80182005-01-15 22:15:51 +00003341 /*
3342 * We now have a macro name, an implicit parameter count of
3343 * zero, and a numeric token to use as an expansion. Create
3344 * and store an SMacro.
3345 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003346 define_smacro(ctx, mname, casesense, 0, macro_start);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003347 free_tlist(tline);
3348 free_tlist(origline);
3349 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003350
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003351 case PP_STRCAT:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003352 casesense = true;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003353
3354 tline = tline->next;
3355 skip_white_(tline);
3356 tline = expand_id(tline);
3357 if (!tline || (tline->type != TOK_ID &&
3358 (tline->type != TOK_PREPROC_ID ||
3359 tline->text[1] != '$'))) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003360 nasm_nonfatal("`%%strcat' expects a macro identifier as first parameter");
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003361 free_tlist(origline);
3362 return DIRECTIVE_FOUND;
3363 }
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04003364 ctx = get_ctx(tline->text, &mname);
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003365 last = tline;
3366 tline = expand_smacro(tline->next);
3367 last->next = NULL;
3368
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003369 len = 0;
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04003370 list_for_each(t, tline) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003371 switch (t->type) {
3372 case TOK_WHITESPACE:
3373 break;
3374 case TOK_STRING:
3375 len += t->a.len = nasm_unquote(t->text, NULL);
3376 break;
3377 case TOK_OTHER:
3378 if (!strcmp(t->text, ",")) /* permit comma separators */
3379 break;
3380 /* else fall through */
3381 default:
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003382 nasm_nonfatal("non-string passed to `%%strcat' (%d)", t->type);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003383 free_tlist(tline);
3384 free_tlist(origline);
3385 return DIRECTIVE_FOUND;
3386 }
3387 }
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003388
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003389 p = pp = nasm_malloc(len);
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04003390 list_for_each(t, tline) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003391 if (t->type == TOK_STRING) {
3392 memcpy(p, t->text, t->a.len);
3393 p += t->a.len;
3394 }
3395 }
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003396
3397 /*
3398 * We now have a macro name, an implicit parameter count of
3399 * zero, and a numeric token to use as an expansion. Create
3400 * and store an SMacro.
3401 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003402 macro_start = new_Token(NULL, TOK_STRING, NULL, 0);
3403 macro_start->text = nasm_quote(pp, len);
3404 nasm_free(pp);
3405 define_smacro(ctx, mname, casesense, 0, macro_start);
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003406 free_tlist(tline);
3407 free_tlist(origline);
3408 return DIRECTIVE_FOUND;
3409
H. Peter Anvine2c80182005-01-15 22:15:51 +00003410 case PP_SUBSTR:
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003411 {
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003412 int64_t start, count;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003413 size_t len;
H. Peter Anvind2456592008-06-19 15:04:18 -07003414
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003415 casesense = true;
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07003416
H. Peter Anvine2c80182005-01-15 22:15:51 +00003417 tline = tline->next;
3418 skip_white_(tline);
3419 tline = expand_id(tline);
3420 if (!tline || (tline->type != TOK_ID &&
3421 (tline->type != TOK_PREPROC_ID ||
3422 tline->text[1] != '$'))) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003423 nasm_nonfatal("`%%substr' expects a macro identifier as first parameter");
H. Peter Anvine2c80182005-01-15 22:15:51 +00003424 free_tlist(origline);
3425 return DIRECTIVE_FOUND;
3426 }
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04003427 ctx = get_ctx(tline->text, &mname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003428 last = tline;
3429 tline = expand_smacro(tline->next);
3430 last->next = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003431
Cyrill Gorcunov35519d62010-09-06 23:49:52 +04003432 if (tline) /* skip expanded id */
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003433 t = tline->next;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003434 while (tok_type_(t, TOK_WHITESPACE))
3435 t = t->next;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003436
H. Peter Anvine2c80182005-01-15 22:15:51 +00003437 /* t should now point to the string */
Cyrill Gorcunov35519d62010-09-06 23:49:52 +04003438 if (!tok_type_(t, TOK_STRING)) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003439 nasm_nonfatal("`%%substr` requires string as second parameter");
H. Peter Anvine2c80182005-01-15 22:15:51 +00003440 free_tlist(tline);
3441 free_tlist(origline);
3442 return DIRECTIVE_FOUND;
3443 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003444
H. Peter Anvine2c80182005-01-15 22:15:51 +00003445 tt = t->next;
3446 tptr = &tt;
3447 tokval.t_type = TOKEN_INVALID;
H. Peter Anvin130736c2016-02-17 20:27:41 -08003448 evalresult = evaluate(ppscan, tptr, &tokval, NULL, pass, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003449 if (!evalresult) {
3450 free_tlist(tline);
3451 free_tlist(origline);
3452 return DIRECTIVE_FOUND;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003453 } else if (!is_simple(evalresult)) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003454 nasm_nonfatal("non-constant value given to `%%substr`");
H. Peter Anvine2c80182005-01-15 22:15:51 +00003455 free_tlist(tline);
3456 free_tlist(origline);
3457 return DIRECTIVE_FOUND;
3458 }
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003459 start = evalresult->value - 1;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003460
3461 while (tok_type_(tt, TOK_WHITESPACE))
3462 tt = tt->next;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003463 if (!tt) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003464 count = 1; /* Backwards compatibility: one character */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003465 } else {
3466 tokval.t_type = TOKEN_INVALID;
H. Peter Anvin130736c2016-02-17 20:27:41 -08003467 evalresult = evaluate(ppscan, tptr, &tokval, NULL, pass, NULL);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003468 if (!evalresult) {
3469 free_tlist(tline);
3470 free_tlist(origline);
3471 return DIRECTIVE_FOUND;
3472 } else if (!is_simple(evalresult)) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003473 nasm_nonfatal("non-constant value given to `%%substr`");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003474 free_tlist(tline);
3475 free_tlist(origline);
3476 return DIRECTIVE_FOUND;
3477 }
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003478 count = evalresult->value;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003479 }
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003480
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003481 len = nasm_unquote(t->text, NULL);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003482
Cyrill Gorcunovcff031e2010-09-07 20:31:11 +04003483 /* make start and count being in range */
3484 if (start < 0)
3485 start = 0;
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003486 if (count < 0)
3487 count = len + count + 1 - start;
3488 if (start + count > (int64_t)len)
Cyrill Gorcunovcff031e2010-09-07 20:31:11 +04003489 count = len - start;
3490 if (!len || count < 0 || start >=(int64_t)len)
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003491 start = -1, count = 0; /* empty string */
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003492
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003493 macro_start = nasm_malloc(sizeof(*macro_start));
3494 macro_start->next = NULL;
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003495 macro_start->text = nasm_quote((start < 0) ? "" : t->text + start, count);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003496 macro_start->type = TOK_STRING;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003497 macro_start->a.mac = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003498
H. Peter Anvine2c80182005-01-15 22:15:51 +00003499 /*
3500 * We now have a macro name, an implicit parameter count of
3501 * zero, and a numeric token to use as an expansion. Create
3502 * and store an SMacro.
3503 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003504 define_smacro(ctx, mname, casesense, 0, macro_start);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003505 free_tlist(tline);
3506 free_tlist(origline);
3507 return DIRECTIVE_FOUND;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003508 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003509
H. Peter Anvine2c80182005-01-15 22:15:51 +00003510 case PP_ASSIGN:
3511 case PP_IASSIGN:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003512 casesense = (i == PP_ASSIGN);
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07003513
H. Peter Anvine2c80182005-01-15 22:15:51 +00003514 tline = tline->next;
3515 skip_white_(tline);
3516 tline = expand_id(tline);
3517 if (!tline || (tline->type != TOK_ID &&
3518 (tline->type != TOK_PREPROC_ID ||
3519 tline->text[1] != '$'))) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003520 nasm_nonfatal("`%%%sassign' expects a macro identifier",
3521 (i == PP_IASSIGN ? "i" : ""));
H. Peter Anvine2c80182005-01-15 22:15:51 +00003522 free_tlist(origline);
3523 return DIRECTIVE_FOUND;
3524 }
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04003525 ctx = get_ctx(tline->text, &mname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003526 last = tline;
3527 tline = expand_smacro(tline->next);
3528 last->next = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003529
H. Peter Anvine2c80182005-01-15 22:15:51 +00003530 t = tline;
3531 tptr = &t;
3532 tokval.t_type = TOKEN_INVALID;
H. Peter Anvin130736c2016-02-17 20:27:41 -08003533 evalresult = evaluate(ppscan, tptr, &tokval, NULL, pass, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003534 free_tlist(tline);
3535 if (!evalresult) {
3536 free_tlist(origline);
3537 return DIRECTIVE_FOUND;
3538 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003539
H. Peter Anvine2c80182005-01-15 22:15:51 +00003540 if (tokval.t_type)
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003541 nasm_warnf(ERR_PASS1, "trailing garbage after expression ignored");
H. Peter Anvin734b1882002-04-30 21:01:08 +00003542
H. Peter Anvine2c80182005-01-15 22:15:51 +00003543 if (!is_simple(evalresult)) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003544 nasm_nonfatal("non-constant value given to `%%%sassign'",
3545 (i == PP_IASSIGN ? "i" : ""));
H. Peter Anvine2c80182005-01-15 22:15:51 +00003546 free_tlist(origline);
3547 return DIRECTIVE_FOUND;
3548 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003549
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003550 macro_start = nasm_malloc(sizeof(*macro_start));
3551 macro_start->next = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003552 make_tok_num(macro_start, reloc_value(evalresult));
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003553 macro_start->a.mac = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003554
H. Peter Anvine2c80182005-01-15 22:15:51 +00003555 /*
3556 * We now have a macro name, an implicit parameter count of
3557 * zero, and a numeric token to use as an expansion. Create
3558 * and store an SMacro.
3559 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003560 define_smacro(ctx, mname, casesense, 0, macro_start);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003561 free_tlist(origline);
3562 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003563
H. Peter Anvine2c80182005-01-15 22:15:51 +00003564 case PP_LINE:
3565 /*
3566 * Syntax is `%line nnn[+mmm] [filename]'
3567 */
3568 tline = tline->next;
3569 skip_white_(tline);
3570 if (!tok_type_(tline, TOK_NUMBER)) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003571 nasm_nonfatal("`%%line' expects line number");
H. Peter Anvine2c80182005-01-15 22:15:51 +00003572 free_tlist(origline);
3573 return DIRECTIVE_FOUND;
3574 }
H. Peter Anvin70055962007-10-11 00:05:31 -07003575 k = readnum(tline->text, &err);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003576 m = 1;
3577 tline = tline->next;
3578 if (tok_is_(tline, "+")) {
3579 tline = tline->next;
3580 if (!tok_type_(tline, TOK_NUMBER)) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003581 nasm_nonfatal("`%%line' expects line increment");
H. Peter Anvine2c80182005-01-15 22:15:51 +00003582 free_tlist(origline);
3583 return DIRECTIVE_FOUND;
3584 }
H. Peter Anvin70055962007-10-11 00:05:31 -07003585 m = readnum(tline->text, &err);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003586 tline = tline->next;
3587 }
3588 skip_white_(tline);
3589 src_set_linnum(k);
3590 istk->lineinc = m;
3591 if (tline) {
H. Peter Anvin274cda82016-05-10 02:56:29 -07003592 char *fname = detoken(tline, false);
3593 src_set_fname(fname);
3594 nasm_free(fname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003595 }
3596 free_tlist(origline);
3597 return DIRECTIVE_FOUND;
Keith Kaniosb307a4f2010-11-06 17:41:51 -05003598
H. Peter Anvine2c80182005-01-15 22:15:51 +00003599 default:
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003600 nasm_fatal("preprocessor directive `%s' not yet implemented",
3601 pp_directives[i]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003602 return DIRECTIVE_FOUND;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003603 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003604}
3605
3606/*
H. Peter Anvin76690a12002-04-30 20:52:49 +00003607 * Ensure that a macro parameter contains a condition code and
3608 * nothing else. Return the condition code index if so, or -1
3609 * otherwise.
3610 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003611static int find_cc(Token * t)
H. Peter Anvineba20a72002-04-30 20:53:55 +00003612{
H. Peter Anvin76690a12002-04-30 20:52:49 +00003613 Token *tt;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003614
H. Peter Anvin25a99342007-09-22 17:45:45 -07003615 if (!t)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003616 return -1; /* Probably a %+ without a space */
H. Peter Anvin25a99342007-09-22 17:45:45 -07003617
H. Peter Anvineba20a72002-04-30 20:53:55 +00003618 skip_white_(t);
Cyrill Gorcunov7524cfd2017-10-22 19:01:16 +03003619 if (!t)
3620 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003621 if (t->type != TOK_ID)
H. Peter Anvine2c80182005-01-15 22:15:51 +00003622 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003623 tt = t->next;
H. Peter Anvineba20a72002-04-30 20:53:55 +00003624 skip_white_(tt);
H. Peter Anvin76690a12002-04-30 20:52:49 +00003625 if (tt && (tt->type != TOK_OTHER || strcmp(tt->text, ",")))
H. Peter Anvine2c80182005-01-15 22:15:51 +00003626 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003627
Cyrill Gorcunov19456392012-05-02 00:18:56 +04003628 return bsii(t->text, (const char **)conditions, ARRAY_SIZE(conditions));
H. Peter Anvin76690a12002-04-30 20:52:49 +00003629}
3630
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003631/*
3632 * This routines walks over tokens strem and hadnles tokens
3633 * pasting, if @handle_explicit passed then explicit pasting
3634 * term is handled, otherwise -- implicit pastings only.
3635 */
Cyrill Gorcunov575d4282010-10-06 00:25:55 +04003636static bool paste_tokens(Token **head, const struct tokseq_match *m,
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003637 size_t mnum, bool handle_explicit)
H. Peter Anvind784a082009-04-20 14:01:18 -07003638{
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003639 Token *tok, *next, **prev_next, **prev_nonspace;
3640 bool pasted = false;
3641 char *buf, *p;
3642 size_t len, i;
H. Peter Anvind784a082009-04-20 14:01:18 -07003643
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003644 /*
3645 * The last token before pasting. We need it
3646 * to be able to connect new handled tokens.
3647 * In other words if there were a tokens stream
3648 *
3649 * A -> B -> C -> D
3650 *
3651 * and we've joined tokens B and C, the resulting
3652 * stream should be
3653 *
3654 * A -> BC -> D
3655 */
3656 tok = *head;
3657 prev_next = NULL;
3658
3659 if (!tok_type_(tok, TOK_WHITESPACE) && !tok_type_(tok, TOK_PASTE))
3660 prev_nonspace = head;
3661 else
3662 prev_nonspace = NULL;
3663
3664 while (tok && (next = tok->next)) {
3665
3666 switch (tok->type) {
H. Peter Anvind784a082009-04-20 14:01:18 -07003667 case TOK_WHITESPACE:
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003668 /* Zap redundant whitespaces */
3669 while (tok_type_(next, TOK_WHITESPACE))
3670 next = delete_Token(next);
3671 tok->next = next;
H. Peter Anvind784a082009-04-20 14:01:18 -07003672 break;
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003673
3674 case TOK_PASTE:
3675 /* Explicit pasting */
3676 if (!handle_explicit)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003677 break;
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003678 next = delete_Token(tok);
3679
3680 while (tok_type_(next, TOK_WHITESPACE))
3681 next = delete_Token(next);
3682
3683 if (!pasted)
3684 pasted = true;
3685
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003686 /* Left pasting token is start of line */
3687 if (!prev_nonspace)
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003688 nasm_fatal("No lvalue found on pasting");
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003689
Cyrill Gorcunov8b5c9fb2013-02-04 01:24:54 +04003690 /*
3691 * No ending token, this might happen in two
3692 * cases
3693 *
3694 * 1) There indeed no right token at all
3695 * 2) There is a bare "%define ID" statement,
3696 * and @ID does expand to whitespace.
3697 *
3698 * So technically we need to do a grammar analysis
3699 * in another stage of parsing, but for now lets don't
3700 * change the behaviour people used to. Simply allow
3701 * whitespace after paste token.
3702 */
3703 if (!next) {
3704 /*
3705 * Zap ending space tokens and that's all.
3706 */
3707 tok = (*prev_nonspace)->next;
3708 while (tok_type_(tok, TOK_WHITESPACE))
3709 tok = delete_Token(tok);
3710 tok = *prev_nonspace;
3711 tok->next = NULL;
3712 break;
3713 }
3714
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003715 tok = *prev_nonspace;
3716 while (tok_type_(tok, TOK_WHITESPACE))
3717 tok = delete_Token(tok);
3718 len = strlen(tok->text);
3719 len += strlen(next->text);
3720
3721 p = buf = nasm_malloc(len + 1);
3722 strcpy(p, tok->text);
3723 p = strchr(p, '\0');
3724 strcpy(p, next->text);
3725
3726 delete_Token(tok);
3727
3728 tok = tokenize(buf);
3729 nasm_free(buf);
3730
3731 *prev_nonspace = tok;
3732 while (tok && tok->next)
3733 tok = tok->next;
3734
3735 tok->next = delete_Token(next);
3736
3737 /* Restart from pasted tokens head */
3738 tok = *prev_nonspace;
3739 break;
3740
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003741 default:
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003742 /* implicit pasting */
Cyrill Gorcunov575d4282010-10-06 00:25:55 +04003743 for (i = 0; i < mnum; i++) {
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003744 if (!(PP_CONCAT_MATCH(tok, m[i].mask_head)))
3745 continue;
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +04003746
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003747 len = 0;
3748 while (next && PP_CONCAT_MATCH(next, m[i].mask_tail)) {
3749 len += strlen(next->text);
3750 next = next->next;
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +04003751 }
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003752
Cyrill Gorcunov6f8109e2017-10-22 21:26:36 +03003753 /* No match or no text to process */
3754 if (tok == next || len == 0)
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003755 break;
3756
3757 len += strlen(tok->text);
3758 p = buf = nasm_malloc(len + 1);
3759
Adam Majer1a069432017-07-25 11:12:35 +02003760 strcpy(p, tok->text);
3761 p = strchr(p, '\0');
3762 tok = delete_Token(tok);
3763
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003764 while (tok != next) {
Adam Majer1a069432017-07-25 11:12:35 +02003765 if (PP_CONCAT_MATCH(tok, m[i].mask_tail)) {
3766 strcpy(p, tok->text);
3767 p = strchr(p, '\0');
3768 }
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003769 tok = delete_Token(tok);
3770 }
3771
3772 tok = tokenize(buf);
3773 nasm_free(buf);
3774
3775 if (prev_next)
3776 *prev_next = tok;
3777 else
3778 *head = tok;
3779
3780 /*
3781 * Connect pasted into original stream,
3782 * ie A -> new-tokens -> B
3783 */
3784 while (tok && tok->next)
3785 tok = tok->next;
3786 tok->next = next;
3787
3788 if (!pasted)
3789 pasted = true;
3790
3791 /* Restart from pasted tokens head */
3792 tok = prev_next ? *prev_next : *head;
Cyrill Gorcunov575d4282010-10-06 00:25:55 +04003793 }
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003794
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003795 break;
H. Peter Anvind784a082009-04-20 14:01:18 -07003796 }
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003797
3798 prev_next = &tok->next;
3799
3800 if (tok->next &&
3801 !tok_type_(tok->next, TOK_WHITESPACE) &&
3802 !tok_type_(tok->next, TOK_PASTE))
3803 prev_nonspace = prev_next;
3804
3805 tok = tok->next;
H. Peter Anvind784a082009-04-20 14:01:18 -07003806 }
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003807
3808 return pasted;
H. Peter Anvind784a082009-04-20 14:01:18 -07003809}
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003810
3811/*
3812 * expands to a list of tokens from %{x:y}
3813 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003814static Token *expand_mmac_params_range(MMacro *mac, Token *tline, Token ***last)
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003815{
3816 Token *t = tline, **tt, *tm, *head;
3817 char *pos;
3818 int fst, lst, j, i;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003819
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003820 pos = strchr(tline->text, ':');
3821 nasm_assert(pos);
3822
3823 lst = atoi(pos + 1);
3824 fst = atoi(tline->text + 1);
3825
3826 /*
3827 * only macros params are accounted so
3828 * if someone passes %0 -- we reject such
3829 * value(s)
3830 */
3831 if (lst == 0 || fst == 0)
3832 goto err;
3833
3834 /* the values should be sane */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003835 if ((fst > (int)mac->nparam || fst < (-(int)mac->nparam)) ||
3836 (lst > (int)mac->nparam || lst < (-(int)mac->nparam)))
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003837 goto err;
3838
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003839 fst = fst < 0 ? fst + (int)mac->nparam + 1: fst;
3840 lst = lst < 0 ? lst + (int)mac->nparam + 1: lst;
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003841
3842 /* counted from zero */
3843 fst--, lst--;
3844
3845 /*
Cyrill Gorcunove75331c2013-11-09 12:02:15 +04003846 * It will be at least one token. Note we
3847 * need to scan params until separator, otherwise
3848 * only first token will be passed.
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003849 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003850 tm = mac->params[(fst + mac->rotate) % mac->nparam];
Cyrill Gorcunov67f2ca22018-10-13 19:41:01 +03003851 if (!tm)
3852 goto err;
Cyrill Gorcunove75331c2013-11-09 12:02:15 +04003853 head = new_Token(NULL, tm->type, tm->text, 0);
3854 tt = &head->next, tm = tm->next;
3855 while (tok_isnt_(tm, ",")) {
3856 t = new_Token(NULL, tm->type, tm->text, 0);
3857 *tt = t, tt = &t->next, tm = tm->next;
3858 }
3859
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003860 if (fst < lst) {
3861 for (i = fst + 1; i <= lst; i++) {
3862 t = new_Token(NULL, TOK_OTHER, ",", 0);
3863 *tt = t, tt = &t->next;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003864 j = (i + mac->rotate) % mac->nparam;
3865 tm = mac->params[j];
Cyrill Gorcunove75331c2013-11-09 12:02:15 +04003866 while (tok_isnt_(tm, ",")) {
3867 t = new_Token(NULL, tm->type, tm->text, 0);
3868 *tt = t, tt = &t->next, tm = tm->next;
3869 }
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003870 }
3871 } else {
3872 for (i = fst - 1; i >= lst; i--) {
3873 t = new_Token(NULL, TOK_OTHER, ",", 0);
3874 *tt = t, tt = &t->next;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003875 j = (i + mac->rotate) % mac->nparam;
3876 tm = mac->params[j];
Cyrill Gorcunove75331c2013-11-09 12:02:15 +04003877 while (tok_isnt_(tm, ",")) {
3878 t = new_Token(NULL, tm->type, tm->text, 0);
3879 *tt = t, tt = &t->next, tm = tm->next;
3880 }
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003881 }
3882 }
3883
3884 *last = tt;
3885 return head;
3886
3887err:
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003888 nasm_nonfatal("`%%{%s}': macro parameters out of range",
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003889 &tline->text[1]);
3890 return tline;
3891}
3892
H. Peter Anvin76690a12002-04-30 20:52:49 +00003893/*
3894 * Expand MMacro-local things: parameter references (%0, %n, %+n,
H. Peter Anvin67c63722008-10-26 23:49:00 -07003895 * %-n) and MMacro-local identifiers (%%foo) as well as
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003896 * macro indirection (%[...]) and range (%{..:..}).
H. Peter Anvin76690a12002-04-30 20:52:49 +00003897 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003898static Token *expand_mmac_params(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00003899{
H. Peter Anvin734b1882002-04-30 21:01:08 +00003900 Token *t, *tt, **tail, *thead;
H. Peter Anvin6125b622009-04-08 14:02:25 -07003901 bool changed = false;
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003902 char *pos;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003903
3904 tail = &thead;
3905 thead = NULL;
3906
H. Peter Anvine2c80182005-01-15 22:15:51 +00003907 while (tline) {
Cyrill Gorcunov661f7232018-10-28 20:39:34 +03003908 if (tline->type == TOK_PREPROC_ID && tline->text && tline->text[0] &&
Cyrill Gorcunovca611192010-06-04 09:22:12 +04003909 (((tline->text[1] == '+' || tline->text[1] == '-') && tline->text[2]) ||
3910 (tline->text[1] >= '0' && tline->text[1] <= '9') ||
3911 tline->text[1] == '%')) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00003912 char *text = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003913 int type = 0, cc; /* type = 0 to placate optimisers */
Keith Kaniosa6dfa782007-04-13 16:47:53 +00003914 char tmpbuf[30];
H. Peter Anvin25a99342007-09-22 17:45:45 -07003915 unsigned int n;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003916 int i;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003917 MMacro *mac;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003918
H. Peter Anvine2c80182005-01-15 22:15:51 +00003919 t = tline;
3920 tline = tline->next;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003921
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003922 mac = istk->mstk;
3923 while (mac && !mac->name) /* avoid mistaking %reps for macros */
3924 mac = mac->next_active;
3925 if (!mac) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003926 nasm_nonfatal("`%s': not in a macro call", t->text);
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003927 } else {
3928 pos = strchr(t->text, ':');
3929 if (!pos) {
3930 switch (t->text[1]) {
3931 /*
3932 * We have to make a substitution of one of the
3933 * forms %1, %-1, %+1, %%foo, %0.
3934 */
3935 case '0':
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003936 type = TOK_NUMBER;
3937 snprintf(tmpbuf, sizeof(tmpbuf), "%d", mac->nparam);
3938 text = nasm_strdup(tmpbuf);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003939 break;
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003940 case '%':
H. Peter Anvine2c80182005-01-15 22:15:51 +00003941 type = TOK_ID;
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003942 snprintf(tmpbuf, sizeof(tmpbuf), "..@%"PRIu64".",
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003943 mac->unique);
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003944 text = nasm_strcat(tmpbuf, t->text + 2);
3945 break;
3946 case '-':
3947 n = atoi(t->text + 2) - 1;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003948 if (n >= mac->nparam)
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003949 tt = NULL;
3950 else {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003951 if (mac->nparam > 1)
3952 n = (n + mac->rotate) % mac->nparam;
3953 tt = mac->params[n];
H. Peter Anvine2c80182005-01-15 22:15:51 +00003954 }
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003955 cc = find_cc(tt);
3956 if (cc == -1) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003957 nasm_nonfatal("macro parameter %d is not a condition code",
3958 n + 1);
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003959 text = NULL;
3960 } else {
3961 type = TOK_ID;
3962 if (inverse_ccs[cc] == -1) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003963 nasm_nonfatal("condition code `%s' is not invertible",
3964 conditions[cc]);
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003965 text = NULL;
3966 } else
3967 text = nasm_strdup(conditions[inverse_ccs[cc]]);
3968 }
3969 break;
3970 case '+':
3971 n = atoi(t->text + 2) - 1;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003972 if (n >= mac->nparam)
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003973 tt = NULL;
3974 else {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003975 if (mac->nparam > 1)
3976 n = (n + mac->rotate) % mac->nparam;
3977 tt = mac->params[n];
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003978 }
3979 cc = find_cc(tt);
3980 if (cc == -1) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003981 nasm_nonfatal("macro parameter %d is not a condition code",
3982 n + 1);
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003983 text = NULL;
3984 } else {
3985 type = TOK_ID;
3986 text = nasm_strdup(conditions[cc]);
3987 }
3988 break;
3989 default:
3990 n = atoi(t->text + 1) - 1;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003991 if (n >= mac->nparam)
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003992 tt = NULL;
3993 else {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003994 if (mac->nparam > 1)
3995 n = (n + mac->rotate) % mac->nparam;
3996 tt = mac->params[n];
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003997 }
3998 if (tt) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003999 for (i = 0; i < mac->paramlen[n]; i++) {
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004000 *tail = new_Token(NULL, tt->type, tt->text, 0);
4001 tail = &(*tail)->next;
4002 tt = tt->next;
4003 }
4004 }
4005 text = NULL; /* we've done it here */
4006 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004007 }
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004008 } else {
4009 /*
4010 * seems we have a parameters range here
4011 */
4012 Token *head, **last;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004013 head = expand_mmac_params_range(mac, t, &last);
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004014 if (head != t) {
4015 *tail = head;
4016 *last = tline;
4017 tline = head;
4018 text = NULL;
4019 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00004020 }
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004021 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00004022 if (!text) {
4023 delete_Token(t);
4024 } else {
4025 *tail = t;
4026 tail = &t->next;
4027 t->type = type;
4028 nasm_free(t->text);
4029 t->text = text;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004030 t->a.mac = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004031 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004032 changed = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004033 continue;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004034 } else if (tline->type == TOK_INDIRECT) {
4035 t = tline;
4036 tline = tline->next;
4037 tt = tokenize(t->text);
4038 tt = expand_mmac_params(tt);
4039 tt = expand_smacro(tt);
4040 *tail = tt;
4041 while (tt) {
4042 tt->a.mac = NULL; /* Necessary? */
4043 tail = &tt->next;
4044 tt = tt->next;
4045 }
4046 delete_Token(t);
4047 changed = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004048 } else {
4049 t = *tail = tline;
4050 tline = tline->next;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004051 t->a.mac = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004052 tail = &t->next;
4053 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00004054 }
H. Peter Anvineba20a72002-04-30 20:53:55 +00004055 *tail = NULL;
H. Peter Anvin67c63722008-10-26 23:49:00 -07004056
Cyrill Gorcunovc6a742c2011-06-27 01:23:09 +04004057 if (changed) {
4058 const struct tokseq_match t[] = {
4059 {
4060 PP_CONCAT_MASK(TOK_ID) |
4061 PP_CONCAT_MASK(TOK_FLOAT), /* head */
4062 PP_CONCAT_MASK(TOK_ID) |
4063 PP_CONCAT_MASK(TOK_NUMBER) |
4064 PP_CONCAT_MASK(TOK_FLOAT) |
4065 PP_CONCAT_MASK(TOK_OTHER) /* tail */
4066 },
4067 {
4068 PP_CONCAT_MASK(TOK_NUMBER), /* head */
4069 PP_CONCAT_MASK(TOK_NUMBER) /* tail */
4070 }
4071 };
4072 paste_tokens(&thead, t, ARRAY_SIZE(t), false);
4073 }
H. Peter Anvin6125b622009-04-08 14:02:25 -07004074
H. Peter Anvin76690a12002-04-30 20:52:49 +00004075 return thead;
4076}
4077
4078/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004079 * Expand all single-line macro calls made in the given line.
4080 * Return the expanded version of the line. The original is deemed
4081 * to be destroyed in the process. (In reality we'll just move
4082 * Tokens from input to output a lot of the time, rather than
4083 * actually bothering to destroy and replicate.)
4084 */
H. Peter Anvincb1cf592007-11-19 12:26:50 -08004085
H. Peter Anvine2c80182005-01-15 22:15:51 +00004086static Token *expand_smacro(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004087{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004088 Token *t, *tt, *mstart, **tail, *thead;
H. Peter Anvineba20a72002-04-30 20:53:55 +00004089 SMacro *head = NULL, *m;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004090 Token **params;
4091 int *paramsize;
H. Peter Anvin25a99342007-09-22 17:45:45 -07004092 unsigned int nparam, sparam;
H. Peter Anvind784a082009-04-20 14:01:18 -07004093 int brackets;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004094 Token *org_tline = tline;
4095 Context *ctx;
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08004096 const char *mname;
H. Peter Anvina3d96d02018-06-15 17:51:39 -07004097 int64_t deadman = nasm_limit[LIMIT_MACROS];
H. Peter Anvin8287daf2009-07-07 16:00:58 -07004098 bool expanded;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004099
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004100 /*
4101 * Trick: we should avoid changing the start token pointer since it can
4102 * be contained in "next" field of other token. Because of this
4103 * we allocate a copy of first token and work with it; at the end of
4104 * routine we copy it back
4105 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004106 if (org_tline) {
Cyrill Gorcunoved4a8052010-04-09 15:40:35 +04004107 tline = new_Token(org_tline->next, org_tline->type,
4108 org_tline->text, 0);
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004109 tline->a.mac = org_tline->a.mac;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004110 nasm_free(org_tline->text);
4111 org_tline->text = NULL;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004112 }
4113
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004114 expanded = true; /* Always expand %+ at least once */
H. Peter Anvin8287daf2009-07-07 16:00:58 -07004115
H. Peter Anvincb1cf592007-11-19 12:26:50 -08004116again:
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004117 thead = NULL;
Cyrill Gorcunoved4a8052010-04-09 15:40:35 +04004118 tail = &thead;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004119
H. Peter Anvine2c80182005-01-15 22:15:51 +00004120 while (tline) { /* main token loop */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004121 if (!--deadman) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03004122 nasm_nonfatal("interminable macro recursion");
Cyrill Gorcunovbd38c8f2009-11-21 11:11:23 +03004123 goto err;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004124 }
H. Peter Anvincb1cf592007-11-19 12:26:50 -08004125
H. Peter Anvine2c80182005-01-15 22:15:51 +00004126 if ((mname = tline->text)) {
4127 /* if this token is a local macro, look in local context */
Cyrill Gorcunovc56d9ad2010-02-11 15:12:19 +03004128 if (tline->type == TOK_ID) {
4129 head = (SMacro *)hash_findix(&smacros, mname);
4130 } else if (tline->type == TOK_PREPROC_ID) {
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04004131 ctx = get_ctx(mname, &mname);
Cyrill Gorcunovc56d9ad2010-02-11 15:12:19 +03004132 head = ctx ? (SMacro *)hash_findix(&ctx->localmac, mname) : NULL;
4133 } else
4134 head = NULL;
H. Peter Anvin072771e2008-05-22 13:17:51 -07004135
H. Peter Anvine2c80182005-01-15 22:15:51 +00004136 /*
4137 * We've hit an identifier. As in is_mmacro below, we first
4138 * check whether the identifier is a single-line macro at
4139 * all, then think about checking for parameters if
4140 * necessary.
4141 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004142 list_for_each(m, head)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004143 if (!mstrcmp(m->name, mname, m->casesense))
4144 break;
4145 if (m) {
4146 mstart = tline;
4147 params = NULL;
4148 paramsize = NULL;
4149 if (m->nparam == 0) {
4150 /*
4151 * Simple case: the macro is parameterless. Discard the
4152 * one token that the macro call took, and push the
4153 * expansion back on the to-do stack.
4154 */
4155 if (!m->expansion) {
4156 if (!strcmp("__FILE__", m->name)) {
H. Peter Anvin274cda82016-05-10 02:56:29 -07004157 const char *file = src_get_fname();
4158 /* nasm_free(tline->text); here? */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004159 tline->text = nasm_quote(file, strlen(file));
4160 tline->type = TOK_STRING;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004161 continue;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004162 }
4163 if (!strcmp("__LINE__", m->name)) {
4164 nasm_free(tline->text);
4165 make_tok_num(tline, src_get_linnum());
4166 continue;
4167 }
4168 if (!strcmp("__BITS__", m->name)) {
4169 nasm_free(tline->text);
4170 make_tok_num(tline, globalbits);
4171 continue;
4172 }
4173 tline = delete_Token(tline);
4174 continue;
4175 }
4176 } else {
4177 /*
4178 * Complicated case: at least one macro with this name
H. Peter Anvine2c80182005-01-15 22:15:51 +00004179 * exists and takes parameters. We must find the
4180 * parameters in the call, count them, find the SMacro
4181 * that corresponds to that form of the macro call, and
4182 * substitute for the parameters when we expand. What a
4183 * pain.
4184 */
4185 /*tline = tline->next;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004186 skip_white_(tline); */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004187 do {
4188 t = tline->next;
4189 while (tok_type_(t, TOK_SMAC_END)) {
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004190 t->a.mac->in_progress = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004191 t->text = NULL;
4192 t = tline->next = delete_Token(t);
4193 }
4194 tline = t;
4195 } while (tok_type_(tline, TOK_WHITESPACE));
4196 if (!tok_is_(tline, "(")) {
4197 /*
4198 * This macro wasn't called with parameters: ignore
4199 * the call. (Behaviour borrowed from gnu cpp.)
4200 */
4201 tline = mstart;
4202 m = NULL;
4203 } else {
4204 int paren = 0;
4205 int white = 0;
4206 brackets = 0;
4207 nparam = 0;
4208 sparam = PARAM_DELTA;
4209 params = nasm_malloc(sparam * sizeof(Token *));
4210 params[0] = tline->next;
4211 paramsize = nasm_malloc(sparam * sizeof(int));
4212 paramsize[0] = 0;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07004213 while (true) { /* parameter loop */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004214 /*
4215 * For some unusual expansions
4216 * which concatenates function call
4217 */
4218 t = tline->next;
4219 while (tok_type_(t, TOK_SMAC_END)) {
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004220 t->a.mac->in_progress = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004221 t->text = NULL;
4222 t = tline->next = delete_Token(t);
4223 }
4224 tline = t;
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00004225
H. Peter Anvine2c80182005-01-15 22:15:51 +00004226 if (!tline) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03004227 nasm_nonfatal("macro call expects terminating `)'");
H. Peter Anvine2c80182005-01-15 22:15:51 +00004228 break;
4229 }
4230 if (tline->type == TOK_WHITESPACE
4231 && brackets <= 0) {
4232 if (paramsize[nparam])
4233 white++;
4234 else
4235 params[nparam] = tline->next;
4236 continue; /* parameter loop */
4237 }
4238 if (tline->type == TOK_OTHER
4239 && tline->text[1] == 0) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004240 char ch = tline->text[0];
H. Peter Anvine2c80182005-01-15 22:15:51 +00004241 if (ch == ',' && !paren && brackets <= 0) {
4242 if (++nparam >= sparam) {
4243 sparam += PARAM_DELTA;
4244 params = nasm_realloc(params,
Cyrill Gorcunoved4a8052010-04-09 15:40:35 +04004245 sparam * sizeof(Token *));
4246 paramsize = nasm_realloc(paramsize,
4247 sparam * sizeof(int));
H. Peter Anvine2c80182005-01-15 22:15:51 +00004248 }
4249 params[nparam] = tline->next;
4250 paramsize[nparam] = 0;
4251 white = 0;
4252 continue; /* parameter loop */
4253 }
4254 if (ch == '{' &&
4255 (brackets > 0 || (brackets == 0 &&
4256 !paramsize[nparam])))
4257 {
4258 if (!(brackets++)) {
4259 params[nparam] = tline->next;
4260 continue; /* parameter loop */
4261 }
4262 }
4263 if (ch == '}' && brackets > 0)
4264 if (--brackets == 0) {
4265 brackets = -1;
4266 continue; /* parameter loop */
4267 }
4268 if (ch == '(' && !brackets)
4269 paren++;
4270 if (ch == ')' && brackets <= 0)
4271 if (--paren < 0)
4272 break;
4273 }
4274 if (brackets < 0) {
4275 brackets = 0;
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03004276 nasm_nonfatal("braces do not "
H. Peter Anvine2c80182005-01-15 22:15:51 +00004277 "enclose all of macro parameter");
4278 }
4279 paramsize[nparam] += white + 1;
4280 white = 0;
4281 } /* parameter loop */
4282 nparam++;
4283 while (m && (m->nparam != nparam ||
4284 mstrcmp(m->name, mname,
4285 m->casesense)))
4286 m = m->next;
4287 if (!m)
H. Peter Anvin (Intel)26572c62018-12-13 16:51:45 -08004288 nasm_warnf(ERR_PASS1|WARN_MACRO_PARAMS,
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03004289 "macro `%s' exists, "
4290 "but not taking %d parameters",
4291 mstart->text, nparam);
H. Peter Anvine2c80182005-01-15 22:15:51 +00004292 }
4293 }
4294 if (m && m->in_progress)
4295 m = NULL;
4296 if (!m) { /* in progess or didn't find '(' or wrong nparam */
H. Peter Anvin70653092007-10-19 14:42:29 -07004297 /*
H. Peter Anvine2c80182005-01-15 22:15:51 +00004298 * Design question: should we handle !tline, which
4299 * indicates missing ')' here, or expand those
4300 * macros anyway, which requires the (t) test a few
H. Peter Anvin70653092007-10-19 14:42:29 -07004301 * lines down?
H. Peter Anvine2c80182005-01-15 22:15:51 +00004302 */
4303 nasm_free(params);
4304 nasm_free(paramsize);
4305 tline = mstart;
4306 } else {
4307 /*
4308 * Expand the macro: we are placed on the last token of the
4309 * call, so that we can easily split the call from the
4310 * following tokens. We also start by pushing an SMAC_END
4311 * token for the cycle removal.
4312 */
4313 t = tline;
4314 if (t) {
4315 tline = t->next;
4316 t->next = NULL;
4317 }
4318 tt = new_Token(tline, TOK_SMAC_END, NULL, 0);
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004319 tt->a.mac = m;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07004320 m->in_progress = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004321 tline = tt;
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04004322 list_for_each(t, m->expansion) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004323 if (t->type >= TOK_SMAC_PARAM) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00004324 Token *pcopy = tline, **ptail = &pcopy;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004325 Token *ttt, *pt;
4326 int i;
H. Peter Anvin734b1882002-04-30 21:01:08 +00004327
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004328 ttt = params[t->type - TOK_SMAC_PARAM];
4329 i = paramsize[t->type - TOK_SMAC_PARAM];
4330 while (--i >= 0) {
4331 pt = *ptail = new_Token(tline, ttt->type,
4332 ttt->text, 0);
4333 ptail = &pt->next;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004334 ttt = ttt->next;
Cyrill Gorcunov59ce1c62017-10-22 18:42:07 +03004335 if (!ttt && i > 0) {
4336 /*
4337 * FIXME: Need to handle more gracefully,
4338 * exiting early on agruments analysis.
4339 */
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03004340 nasm_fatal("macro `%s' expects %d args",
Cyrill Gorcunov59ce1c62017-10-22 18:42:07 +03004341 mstart->text,
4342 (int)paramsize[t->type - TOK_SMAC_PARAM]);
4343 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00004344 }
4345 tline = pcopy;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004346 } else if (t->type == TOK_PREPROC_Q) {
4347 tt = new_Token(tline, TOK_ID, mname, 0);
4348 tline = tt;
4349 } else if (t->type == TOK_PREPROC_QQ) {
4350 tt = new_Token(tline, TOK_ID, m->name, 0);
4351 tline = tt;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004352 } else {
4353 tt = new_Token(tline, t->type, t->text, 0);
4354 tline = tt;
4355 }
4356 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00004357
H. Peter Anvine2c80182005-01-15 22:15:51 +00004358 /*
4359 * Having done that, get rid of the macro call, and clean
4360 * up the parameters.
4361 */
4362 nasm_free(params);
4363 nasm_free(paramsize);
4364 free_tlist(mstart);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004365 expanded = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004366 continue; /* main token loop */
4367 }
4368 }
4369 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004370
H. Peter Anvine2c80182005-01-15 22:15:51 +00004371 if (tline->type == TOK_SMAC_END) {
Cyrill Gorcunov980dd652018-10-14 19:25:32 +03004372 /* On error path it might already be dropped */
4373 if (tline->a.mac)
4374 tline->a.mac->in_progress = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004375 tline = delete_Token(tline);
4376 } else {
4377 t = *tail = tline;
4378 tline = tline->next;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004379 t->a.mac = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004380 t->next = NULL;
4381 tail = &t->next;
4382 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004383 }
4384
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004385 /*
4386 * Now scan the entire line and look for successive TOK_IDs that resulted
Keith Kaniosb7a89542007-04-12 02:40:54 +00004387 * after expansion (they can't be produced by tokenize()). The successive
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004388 * TOK_IDs should be concatenated.
4389 * Also we look for %+ tokens and concatenate the tokens before and after
4390 * them (without white spaces in between).
4391 */
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +04004392 if (expanded) {
Cyrill Gorcunovc6a742c2011-06-27 01:23:09 +04004393 const struct tokseq_match t[] = {
4394 {
4395 PP_CONCAT_MASK(TOK_ID) |
4396 PP_CONCAT_MASK(TOK_PREPROC_ID), /* head */
4397 PP_CONCAT_MASK(TOK_ID) |
4398 PP_CONCAT_MASK(TOK_PREPROC_ID) |
4399 PP_CONCAT_MASK(TOK_NUMBER) /* tail */
4400 }
4401 };
4402 if (paste_tokens(&thead, t, ARRAY_SIZE(t), true)) {
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +04004403 /*
4404 * If we concatenated something, *and* we had previously expanded
4405 * an actual macro, scan the lines again for macros...
4406 */
4407 tline = thead;
4408 expanded = false;
4409 goto again;
4410 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00004411 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004412
Cyrill Gorcunovbd38c8f2009-11-21 11:11:23 +03004413err:
H. Peter Anvine2c80182005-01-15 22:15:51 +00004414 if (org_tline) {
4415 if (thead) {
4416 *org_tline = *thead;
4417 /* since we just gave text to org_line, don't free it */
4418 thead->text = NULL;
4419 delete_Token(thead);
4420 } else {
4421 /* the expression expanded to empty line;
4422 we can't return NULL for some reasons
4423 we just set the line to a single WHITESPACE token. */
4424 memset(org_tline, 0, sizeof(*org_tline));
4425 org_tline->text = NULL;
4426 org_tline->type = TOK_WHITESPACE;
4427 }
4428 thead = org_tline;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004429 }
4430
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004431 return thead;
4432}
4433
4434/*
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004435 * Similar to expand_smacro but used exclusively with macro identifiers
4436 * right before they are fetched in. The reason is that there can be
4437 * identifiers consisting of several subparts. We consider that if there
4438 * are more than one element forming the name, user wants a expansion,
4439 * otherwise it will be left as-is. Example:
4440 *
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004441 * %define %$abc cde
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004442 *
4443 * the identifier %$abc will be left as-is so that the handler for %define
4444 * will suck it and define the corresponding value. Other case:
4445 *
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004446 * %define _%$abc cde
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004447 *
4448 * In this case user wants name to be expanded *before* %define starts
4449 * working, so we'll expand %$abc into something (if it has a value;
4450 * otherwise it will be left as-is) then concatenate all successive
4451 * PP_IDs into one.
4452 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004453static Token *expand_id(Token * tline)
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004454{
4455 Token *cur, *oldnext = NULL;
4456
H. Peter Anvin734b1882002-04-30 21:01:08 +00004457 if (!tline || !tline->next)
H. Peter Anvine2c80182005-01-15 22:15:51 +00004458 return tline;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004459
4460 cur = tline;
4461 while (cur->next &&
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004462 (cur->next->type == TOK_ID ||
4463 cur->next->type == TOK_PREPROC_ID
4464 || cur->next->type == TOK_NUMBER))
H. Peter Anvine2c80182005-01-15 22:15:51 +00004465 cur = cur->next;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004466
4467 /* If identifier consists of just one token, don't expand */
4468 if (cur == tline)
H. Peter Anvine2c80182005-01-15 22:15:51 +00004469 return tline;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004470
H. Peter Anvine2c80182005-01-15 22:15:51 +00004471 if (cur) {
4472 oldnext = cur->next; /* Detach the tail past identifier */
4473 cur->next = NULL; /* so that expand_smacro stops here */
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004474 }
4475
H. Peter Anvin734b1882002-04-30 21:01:08 +00004476 tline = expand_smacro(tline);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004477
H. Peter Anvine2c80182005-01-15 22:15:51 +00004478 if (cur) {
4479 /* expand_smacro possibly changhed tline; re-scan for EOL */
4480 cur = tline;
4481 while (cur && cur->next)
4482 cur = cur->next;
4483 if (cur)
4484 cur->next = oldnext;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004485 }
4486
4487 return tline;
4488}
4489
4490/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004491 * Determine whether the given line constitutes a multi-line macro
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004492 * call, and return the MMacro structure called if so. Doesn't have
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004493 * to check for an initial label - that's taken care of in
4494 * expand_mmacro - but must check numbers of parameters. Guaranteed
4495 * to be called with tline->type == TOK_ID, so the putative macro
4496 * name is easy to find.
4497 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004498static MMacro *is_mmacro(Token * tline, Token *** params_array)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004499{
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004500 MMacro *head, *m;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004501 Token **params;
4502 int nparam;
4503
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004504 head = (MMacro *) hash_findix(&mmacros, tline->text);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004505
4506 /*
4507 * Efficiency: first we see if any macro exists with the given
4508 * name. If not, we can return NULL immediately. _Then_ we
4509 * count the parameters, and then we look further along the
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004510 * list if necessary to find the proper MMacro.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004511 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004512 list_for_each(m, head)
4513 if (!mstrcmp(m->name, tline->text, m->casesense))
H. Peter Anvine2c80182005-01-15 22:15:51 +00004514 break;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004515 if (!m)
H. Peter Anvine2c80182005-01-15 22:15:51 +00004516 return NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004517
4518 /*
4519 * OK, we have a potential macro. Count and demarcate the
4520 * parameters.
4521 */
H. Peter Anvin734b1882002-04-30 21:01:08 +00004522 count_mmac_params(tline->next, &nparam, &params);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004523
4524 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004525 * So we know how many parameters we've got. Find the MMacro
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004526 * structure that handles this number.
4527 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004528 while (m) {
4529 if (m->nparam_min <= nparam
4530 && (m->plus || nparam <= m->nparam_max)) {
4531 /*
4532 * This one is right. Just check if cycle removal
4533 * prohibits us using it before we actually celebrate...
4534 */
4535 if (m->in_progress > m->max_depth) {
4536 if (m->max_depth > 0) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03004537 nasm_warn("reached maximum recursion depth of %i",
4538 m->max_depth);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004539 }
4540 nasm_free(params);
4541 return NULL;
4542 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00004543 /*
4544 * It's right, and we can use it. Add its default
4545 * parameters to the end of our list if necessary.
4546 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004547 if (m->defaults && nparam < m->nparam_min + m->ndefs) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00004548 params =
4549 nasm_realloc(params,
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004550 ((m->nparam_min + m->ndefs +
H. Peter Anvine2c80182005-01-15 22:15:51 +00004551 1) * sizeof(*params)));
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004552 while (nparam < m->nparam_min + m->ndefs) {
4553 params[nparam] = m->defaults[nparam - m->nparam_min];
H. Peter Anvine2c80182005-01-15 22:15:51 +00004554 nparam++;
4555 }
4556 }
4557 /*
4558 * If we've gone over the maximum parameter count (and
4559 * we're in Plus mode), ignore parameters beyond
4560 * nparam_max.
4561 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004562 if (m->plus && nparam > m->nparam_max)
4563 nparam = m->nparam_max;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004564 /*
4565 * Then terminate the parameter list, and leave.
4566 */
4567 if (!params) { /* need this special case */
4568 params = nasm_malloc(sizeof(*params));
4569 nparam = 0;
4570 }
4571 params[nparam] = NULL;
4572 *params_array = params;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004573 return m;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004574 }
4575 /*
4576 * This one wasn't right: look for the next one with the
4577 * same name.
4578 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004579 list_for_each(m, m->next)
4580 if (!mstrcmp(m->name, tline->text, m->casesense))
H. Peter Anvine2c80182005-01-15 22:15:51 +00004581 break;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004582 }
4583
4584 /*
4585 * After all that, we didn't find one with the right number of
4586 * parameters. Issue a warning, and fail to expand the macro.
4587 */
H. Peter Anvin (Intel)26572c62018-12-13 16:51:45 -08004588 nasm_warnf(ERR_PASS1|WARN_MACRO_PARAMS,
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03004589 "macro `%s' exists, but not taking %d parameters",
4590 tline->text, nparam);
H. Peter Anvin734b1882002-04-30 21:01:08 +00004591 nasm_free(params);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004592 return NULL;
4593}
4594
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004595
4596/*
4597 * Save MMacro invocation specific fields in
4598 * preparation for a recursive macro expansion
4599 */
4600static void push_mmacro(MMacro *m)
4601{
4602 MMacroInvocation *i;
4603
4604 i = nasm_malloc(sizeof(MMacroInvocation));
4605 i->prev = m->prev;
4606 i->params = m->params;
4607 i->iline = m->iline;
4608 i->nparam = m->nparam;
4609 i->rotate = m->rotate;
4610 i->paramlen = m->paramlen;
4611 i->unique = m->unique;
4612 i->condcnt = m->condcnt;
4613 m->prev = i;
4614}
4615
4616
4617/*
4618 * Restore MMacro invocation specific fields that were
4619 * saved during a previous recursive macro expansion
4620 */
4621static void pop_mmacro(MMacro *m)
4622{
4623 MMacroInvocation *i;
4624
4625 if (m->prev) {
4626 i = m->prev;
4627 m->prev = i->prev;
4628 m->params = i->params;
4629 m->iline = i->iline;
4630 m->nparam = i->nparam;
4631 m->rotate = i->rotate;
4632 m->paramlen = i->paramlen;
4633 m->unique = i->unique;
4634 m->condcnt = i->condcnt;
4635 nasm_free(i);
4636 }
4637}
4638
4639
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004640/*
4641 * Expand the multi-line macro call made by the given line, if
4642 * there is one to be expanded. If there is, push the expansion on
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004643 * istk->expansion and return 1. Otherwise return 0.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004644 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004645static int expand_mmacro(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004646{
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004647 Token *startline = tline;
H. Peter Anvineba20a72002-04-30 20:53:55 +00004648 Token *label = NULL;
4649 int dont_prepend = 0;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004650 Token **params, *t, *tt;
4651 MMacro *m;
4652 Line *l, *ll;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004653 int i, nparam, *paramlen;
H. Peter Anvinc751e862008-06-09 10:18:45 -07004654 const char *mname;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004655
4656 t = tline;
H. Peter Anvineba20a72002-04-30 20:53:55 +00004657 skip_white_(t);
H. Peter Anvince2233b2008-05-25 21:57:00 -07004658 /* if (!tok_type_(t, TOK_ID)) Lino 02/25/02 */
H. Peter Anvindce1e2f2002-04-30 21:06:37 +00004659 if (!tok_type_(t, TOK_ID) && !tok_type_(t, TOK_PREPROC_ID))
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004660 return 0;
4661 m = is_mmacro(t, &params);
4662 if (m) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004663 mname = t->text;
H. Peter Anvinc751e862008-06-09 10:18:45 -07004664 } else {
H. Peter Anvine2c80182005-01-15 22:15:51 +00004665 Token *last;
4666 /*
4667 * We have an id which isn't a macro call. We'll assume
4668 * it might be a label; we'll also check to see if a
4669 * colon follows it. Then, if there's another id after
4670 * that lot, we'll check it again for macro-hood.
4671 */
4672 label = last = t;
4673 t = t->next;
4674 if (tok_type_(t, TOK_WHITESPACE))
4675 last = t, t = t->next;
4676 if (tok_is_(t, ":")) {
4677 dont_prepend = 1;
4678 last = t, t = t->next;
4679 if (tok_type_(t, TOK_WHITESPACE))
4680 last = t, t = t->next;
4681 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004682 if (!tok_type_(t, TOK_ID) || !(m = is_mmacro(t, &params)))
4683 return 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004684 last->next = NULL;
Keith Kanios891775e2009-07-11 06:08:54 -05004685 mname = t->text;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004686 tline = t;
H. Peter Anvineba20a72002-04-30 20:53:55 +00004687 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004688
4689 /*
4690 * Fix up the parameters: this involves stripping leading and
4691 * trailing whitespace, then stripping braces if they are
4692 * present.
4693 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004694 for (nparam = 0; params[nparam]; nparam++) ;
H. Peter Anvin734b1882002-04-30 21:01:08 +00004695 paramlen = nparam ? nasm_malloc(nparam * sizeof(*paramlen)) : NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004696
H. Peter Anvine2c80182005-01-15 22:15:51 +00004697 for (i = 0; params[i]; i++) {
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08004698 int brace = 0;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004699 int comma = (!m->plus || i < nparam - 1);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004700
H. Peter Anvine2c80182005-01-15 22:15:51 +00004701 t = params[i];
4702 skip_white_(t);
4703 if (tok_is_(t, "{"))
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08004704 t = t->next, brace++, comma = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004705 params[i] = t;
4706 paramlen[i] = 0;
4707 while (t) {
4708 if (comma && t->type == TOK_OTHER && !strcmp(t->text, ","))
4709 break; /* ... because we have hit a comma */
4710 if (comma && t->type == TOK_WHITESPACE
4711 && tok_is_(t->next, ","))
4712 break; /* ... or a space then a comma */
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08004713 if (brace && t->type == TOK_OTHER) {
4714 if (t->text[0] == '{')
4715 brace++; /* ... or a nested opening brace */
4716 else if (t->text[0] == '}')
4717 if (!--brace)
4718 break; /* ... or a brace */
4719 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00004720 t = t->next;
4721 paramlen[i]++;
4722 }
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08004723 if (brace)
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03004724 nasm_nonfatal("macro params should be enclosed in braces");
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004725 }
4726
4727 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004728 * OK, we have a MMacro structure together with a set of
4729 * parameters. We must now go through the expansion and push
4730 * copies of each Line on to istk->expansion. Substitution of
H. Peter Anvin76690a12002-04-30 20:52:49 +00004731 * parameter tokens and macro-local tokens doesn't get done
4732 * until the single-line macro substitution process; this is
4733 * because delaying them allows us to change the semantics
4734 * later through %rotate.
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004735 *
4736 * First, push an end marker on to istk->expansion, mark this
4737 * macro as in progress, and set up its invocation-specific
4738 * variables.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004739 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004740 ll = nasm_malloc(sizeof(Line));
4741 ll->next = istk->expansion;
4742 ll->finishes = m;
4743 ll->first = NULL;
4744 istk->expansion = ll;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004745
4746 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004747 * Save the previous MMacro expansion in the case of
4748 * macro recursion
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004749 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004750 if (m->max_depth && m->in_progress)
4751 push_mmacro(m);
4752
4753 m->in_progress ++;
4754 m->params = params;
4755 m->iline = tline;
4756 m->nparam = nparam;
4757 m->rotate = 0;
4758 m->paramlen = paramlen;
4759 m->unique = unique++;
4760 m->lineno = 0;
4761 m->condcnt = 0;
4762
4763 m->next_active = istk->mstk;
4764 istk->mstk = m;
4765
4766 list_for_each(l, m->expansion) {
4767 Token **tail;
4768
4769 ll = nasm_malloc(sizeof(Line));
4770 ll->finishes = NULL;
4771 ll->next = istk->expansion;
4772 istk->expansion = ll;
4773 tail = &ll->first;
4774
4775 list_for_each(t, l->first) {
4776 Token *x = t;
4777 switch (t->type) {
4778 case TOK_PREPROC_Q:
4779 tt = *tail = new_Token(NULL, TOK_ID, mname, 0);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004780 break;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004781 case TOK_PREPROC_QQ:
4782 tt = *tail = new_Token(NULL, TOK_ID, m->name, 0);
4783 break;
4784 case TOK_PREPROC_ID:
4785 if (t->text[1] == '0' && t->text[2] == '0') {
4786 dont_prepend = -1;
4787 x = label;
4788 if (!x)
4789 continue;
4790 }
4791 /* fall through */
4792 default:
4793 tt = *tail = new_Token(NULL, x->type, x->text, 0);
4794 break;
4795 }
4796 tail = &tt->next;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004797 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004798 *tail = NULL;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004799 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004800
4801 /*
H. Peter Anvineba20a72002-04-30 20:53:55 +00004802 * If we had a label, push it on as the first line of
4803 * the macro expansion.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004804 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004805 if (label) {
4806 if (dont_prepend < 0)
4807 free_tlist(startline);
4808 else {
4809 ll = nasm_malloc(sizeof(Line));
4810 ll->finishes = NULL;
4811 ll->next = istk->expansion;
4812 istk->expansion = ll;
4813 ll->first = startline;
4814 if (!dont_prepend) {
4815 while (label->next)
4816 label = label->next;
4817 label->next = tt = new_Token(NULL, TOK_OTHER, ":", 0);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004818 }
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004819 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004820 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004821
H. Peter Anvin8ac25aa2016-02-18 01:16:18 -08004822 lfmt->uplevel(m->nolist ? LIST_MACRO_NOLIST : LIST_MACRO);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004823
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004824 return 1;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004825}
4826
H. Peter Anvin130736c2016-02-17 20:27:41 -08004827/*
4828 * This function adds macro names to error messages, and suppresses
4829 * them if necessary.
4830 */
H. Peter Anvin (Intel)6bde2ed2018-12-13 19:39:41 -08004831static void pp_verror(errflags severity, const char *fmt, va_list arg)
Victor van den Elzen3b404c02008-09-18 13:51:36 +02004832{
H. Peter Anvin130736c2016-02-17 20:27:41 -08004833 char buff[BUFSIZ];
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004834 MMacro *mmac = NULL;
4835 int delta = 0;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02004836
H. Peter Anvin130736c2016-02-17 20:27:41 -08004837 /*
4838 * If we're in a dead branch of IF or something like it, ignore the error.
4839 * However, because %else etc are evaluated in the state context
4840 * of the previous branch, errors might get lost:
4841 * %if 0 ... %else trailing garbage ... %endif
4842 * So %else etc should set the ERR_PP_PRECOND flag.
4843 */
4844 if ((severity & ERR_MASK) < ERR_FATAL &&
4845 istk && istk->conds &&
4846 ((severity & ERR_PP_PRECOND) ?
4847 istk->conds->state == COND_NEVER :
H. Peter Anvineb6653f2016-04-05 13:03:10 -07004848 !emitting(istk->conds->state)))
H. Peter Anvin130736c2016-02-17 20:27:41 -08004849 return;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02004850
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004851 /* get %macro name */
H. Peter Anvin130736c2016-02-17 20:27:41 -08004852 if (!(severity & ERR_NOFILE) && istk && istk->mstk) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004853 mmac = istk->mstk;
4854 /* but %rep blocks should be skipped */
4855 while (mmac && !mmac->name)
4856 mmac = mmac->next_active, delta++;
Cyrill Gorcunov9900c6b2011-10-09 18:58:46 +04004857 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004858
H. Peter Anvin130736c2016-02-17 20:27:41 -08004859 if (mmac) {
4860 vsnprintf(buff, sizeof(buff), fmt, arg);
Victor van den Elzen3b404c02008-09-18 13:51:36 +02004861
H. Peter Anvin130736c2016-02-17 20:27:41 -08004862 nasm_set_verror(real_verror);
4863 nasm_error(severity, "(%s:%d) %s",
4864 mmac->name, mmac->lineno - delta, buff);
4865 nasm_set_verror(pp_verror);
4866 } else {
4867 real_verror(severity, fmt, arg);
4868 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004869}
4870
H. Peter Anvin734b1882002-04-30 21:01:08 +00004871static void
Cyrill Gorcunovb7bb5ac2018-11-11 21:33:52 +03004872pp_reset(const char *file, int apass, struct strlist *dep_list)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004873{
H. Peter Anvin7383b402008-09-24 10:20:40 -07004874 Token *t;
4875
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004876 cstk = NULL;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004877 istk = nasm_malloc(sizeof(Include));
4878 istk->next = NULL;
4879 istk->conds = NULL;
4880 istk->expansion = NULL;
4881 istk->mstk = NULL;
H. Peter Anvin3e83cec2016-05-25 04:28:46 -07004882 istk->fp = nasm_open_read(file, NF_TEXT);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004883 istk->fname = NULL;
H. Peter Anvin274cda82016-05-10 02:56:29 -07004884 src_set(0, file);
H. Peter Anvineba20a72002-04-30 20:53:55 +00004885 istk->lineinc = 1;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004886 if (!istk->fp)
Cyrill Gorcunovc3527dd2018-11-24 22:17:47 +03004887 nasm_fatalf(ERR_NOFILE, "unable to open input file `%s'", file);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004888 defining = NULL;
Charles Crayned4200be2008-07-12 16:42:33 -07004889 nested_mac_count = 0;
4890 nested_rep_count = 0;
H. Peter Anvin97a23472007-09-16 17:57:25 -07004891 init_macros();
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004892 unique = 0;
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07004893 deplist = dep_list;
H. Peter Anvinf7606612016-07-13 14:23:48 -07004894
4895 if (tasm_compatible_mode)
4896 pp_add_stdmac(nasm_stdmac_tasm);
4897
4898 pp_add_stdmac(nasm_stdmac_nasm);
4899 pp_add_stdmac(nasm_stdmac_version);
4900
Cyrill Gorcunov15ce78f2017-01-06 20:21:28 +03004901 if (extrastdmac)
4902 pp_add_stdmac(extrastdmac);
4903
H. Peter Anvinf7606612016-07-13 14:23:48 -07004904 stdmacpos = stdmacros[0];
4905 stdmacnext = &stdmacros[1];
4906
H. Peter Anvind2456592008-06-19 15:04:18 -07004907 do_predef = true;
H. Peter Anvin61f130f2008-09-25 15:45:06 -07004908
4909 /*
4910 * 0 for dependencies, 1 for preparatory passes, 2 for final pass.
4911 * The caller, however, will also pass in 3 for preprocess-only so
4912 * we can set __PASS__ accordingly.
4913 */
4914 pass = apass > 2 ? 2 : apass;
4915
Cyrill Gorcunovb7bb5ac2018-11-11 21:33:52 +03004916 strlist_add(deplist, file);
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07004917
H. Peter Anvin61f130f2008-09-25 15:45:06 -07004918 /*
4919 * Define the __PASS__ macro. This is defined here unlike
4920 * all the other builtins, because it is special -- it varies between
4921 * passes.
4922 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004923 t = nasm_malloc(sizeof(*t));
4924 t->next = NULL;
H. Peter Anvin61f130f2008-09-25 15:45:06 -07004925 make_tok_num(t, apass);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004926 t->a.mac = NULL;
H. Peter Anvin7383b402008-09-24 10:20:40 -07004927 define_smacro(NULL, "__PASS__", true, 0, t);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004928}
4929
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07004930static void pp_init(void)
4931{
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07004932}
4933
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004934static char *pp_getline(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004935{
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004936 char *line;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004937 Token *tline;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004938
H. Peter Anvin130736c2016-02-17 20:27:41 -08004939 real_verror = nasm_set_verror(pp_verror);
H. Peter Anvin215186f2016-02-17 20:27:41 -08004940
H. Peter Anvine2c80182005-01-15 22:15:51 +00004941 while (1) {
4942 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004943 * Fetch a tokenized line, either from the macro-expansion
H. Peter Anvine2c80182005-01-15 22:15:51 +00004944 * buffer or from the input file.
4945 */
4946 tline = NULL;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004947 while (istk->expansion && istk->expansion->finishes) {
4948 Line *l = istk->expansion;
4949 if (!l->finishes->name && l->finishes->in_progress > 1) {
4950 Line *ll;
H. Peter Anvineba20a72002-04-30 20:53:55 +00004951
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004952 /*
4953 * This is a macro-end marker for a macro with no
4954 * name, which means it's not really a macro at all
4955 * but a %rep block, and the `in_progress' field is
4956 * more than 1, meaning that we still need to
4957 * repeat. (1 means the natural last repetition; 0
4958 * means termination by %exitrep.) We have
4959 * therefore expanded up to the %endrep, and must
4960 * push the whole block on to the expansion buffer
4961 * again. We don't bother to remove the macro-end
4962 * marker: we'd only have to generate another one
4963 * if we did.
4964 */
4965 l->finishes->in_progress--;
4966 list_for_each(l, l->finishes->expansion) {
4967 Token *t, *tt, **tail;
4968
4969 ll = nasm_malloc(sizeof(Line));
4970 ll->next = istk->expansion;
4971 ll->finishes = NULL;
4972 ll->first = NULL;
4973 tail = &ll->first;
4974
4975 list_for_each(t, l->first) {
4976 if (t->text || t->type == TOK_WHITESPACE) {
4977 tt = *tail = new_Token(NULL, t->type, t->text, 0);
4978 tail = &tt->next;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004979 }
4980 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004981
4982 istk->expansion = ll;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004983 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004984 } else {
4985 /*
4986 * Check whether a `%rep' was started and not ended
4987 * within this macro expansion. This can happen and
4988 * should be detected. It's a fatal error because
4989 * I'm too confused to work out how to recover
4990 * sensibly from it.
4991 */
4992 if (defining) {
4993 if (defining->name)
H. Peter Anvinc5136902018-06-15 18:20:17 -07004994 nasm_panic("defining with name in expansion");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004995 else if (istk->mstk->name)
H. Peter Anvinc5136902018-06-15 18:20:17 -07004996 nasm_fatal("`%%rep' without `%%endrep' within"
H. Peter Anvin130736c2016-02-17 20:27:41 -08004997 " expansion of macro `%s'",
4998 istk->mstk->name);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004999 }
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005000
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005001 /*
5002 * FIXME: investigate the relationship at this point between
5003 * istk->mstk and l->finishes
5004 */
5005 {
5006 MMacro *m = istk->mstk;
5007 istk->mstk = m->next_active;
5008 if (m->name) {
5009 /*
5010 * This was a real macro call, not a %rep, and
5011 * therefore the parameter information needs to
5012 * be freed.
5013 */
5014 if (m->prev) {
5015 pop_mmacro(m);
5016 l->finishes->in_progress --;
5017 } else {
5018 nasm_free(m->params);
5019 free_tlist(m->iline);
5020 nasm_free(m->paramlen);
5021 l->finishes->in_progress = 0;
5022 }
Adam Majer91e72402017-07-25 10:42:01 +02005023 }
5024
5025 /*
5026 * FIXME It is incorrect to always free_mmacro here.
5027 * It leads to usage-after-free.
5028 *
5029 * https://bugzilla.nasm.us/show_bug.cgi?id=3392414
5030 */
5031#if 0
5032 else
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005033 free_mmacro(m);
Adam Majer91e72402017-07-25 10:42:01 +02005034#endif
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005035 }
5036 istk->expansion = l->next;
5037 nasm_free(l);
H. Peter Anvin8ac25aa2016-02-18 01:16:18 -08005038 lfmt->downlevel(LIST_MACRO);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005039 }
5040 }
5041 while (1) { /* until we get a line we can use */
5042
5043 if (istk->expansion) { /* from a macro expansion */
5044 char *p;
5045 Line *l = istk->expansion;
5046 if (istk->mstk)
5047 istk->mstk->lineno++;
5048 tline = l->first;
5049 istk->expansion = l->next;
5050 nasm_free(l);
5051 p = detoken(tline, false);
H. Peter Anvin8ac25aa2016-02-18 01:16:18 -08005052 lfmt->line(LIST_MACRO, p);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005053 nasm_free(p);
5054 break;
5055 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00005056 line = read_line();
5057 if (line) { /* from the current input file */
5058 line = prepreproc(line);
Keith Kaniosb7a89542007-04-12 02:40:54 +00005059 tline = tokenize(line);
H. Peter Anvine2c80182005-01-15 22:15:51 +00005060 nasm_free(line);
5061 break;
5062 }
5063 /*
5064 * The current file has ended; work down the istk
5065 */
5066 {
5067 Include *i = istk;
5068 fclose(i->fp);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005069 if (i->conds) {
5070 /* nasm_error can't be conditionally suppressed */
H. Peter Anvinc5136902018-06-15 18:20:17 -07005071 nasm_fatal("expected `%%endif' before end of file");
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005072 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00005073 /* only set line and file name if there's a next node */
H. Peter Anvin274cda82016-05-10 02:56:29 -07005074 if (i->next)
5075 src_set(i->lineno, i->fname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00005076 istk = i->next;
H. Peter Anvin8ac25aa2016-02-18 01:16:18 -08005077 lfmt->downlevel(LIST_INCLUDE);
H. Peter Anvine2c80182005-01-15 22:15:51 +00005078 nasm_free(i);
H. Peter Anvin130736c2016-02-17 20:27:41 -08005079 if (!istk) {
5080 line = NULL;
5081 goto done;
5082 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005083 if (istk->expansion && istk->expansion->finishes)
5084 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005085 }
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005086 }
5087
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005088 /*
5089 * We must expand MMacro parameters and MMacro-local labels
5090 * _before_ we plunge into directive processing, to cope
5091 * with things like `%define something %1' such as STRUC
5092 * uses. Unless we're _defining_ a MMacro, in which case
5093 * those tokens should be left alone to go into the
5094 * definition; and unless we're in a non-emitting
5095 * condition, in which case we don't want to meddle with
5096 * anything.
5097 */
5098 if (!defining && !(istk->conds && !emitting(istk->conds->state))
5099 && !(istk->mstk && !istk->mstk->in_progress)) {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005100 tline = expand_mmac_params(tline);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005101 }
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005102
H. Peter Anvine2c80182005-01-15 22:15:51 +00005103 /*
5104 * Check the line to see if it's a preprocessor directive.
5105 */
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07005106 if (do_directive(tline, &line) == DIRECTIVE_FOUND) {
5107 if (line)
5108 break; /* Directive generated output */
5109 else
5110 continue;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005111 } else if (defining) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00005112 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005113 * We're defining a multi-line macro. We emit nothing
5114 * at all, and just
5115 * shove the tokenized line on to the macro definition.
H. Peter Anvine2c80182005-01-15 22:15:51 +00005116 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005117 Line *l = nasm_malloc(sizeof(Line));
5118 l->next = defining->expansion;
5119 l->first = tline;
5120 l->finishes = NULL;
5121 defining->expansion = l;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005122 continue;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005123 } else if (istk->conds && !emitting(istk->conds->state)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00005124 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005125 * We're in a non-emitting branch of a condition block.
H. Peter Anvine2c80182005-01-15 22:15:51 +00005126 * Emit nothing at all, not even a blank line: when we
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005127 * emerge from the condition we'll give a line-number
H. Peter Anvine2c80182005-01-15 22:15:51 +00005128 * directive so we keep our place correctly.
5129 */
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005130 free_tlist(tline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00005131 continue;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005132 } else if (istk->mstk && !istk->mstk->in_progress) {
5133 /*
5134 * We're in a %rep block which has been terminated, so
5135 * we're walking through to the %endrep without
5136 * emitting anything. Emit nothing at all, not even a
5137 * blank line: when we emerge from the %rep block we'll
5138 * give a line-number directive so we keep our place
5139 * correctly.
5140 */
5141 free_tlist(tline);
5142 continue;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005143 } else {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005144 tline = expand_smacro(tline);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005145 if (!expand_mmacro(tline)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00005146 /*
Keith Kaniosb7a89542007-04-12 02:40:54 +00005147 * De-tokenize the line again, and emit it.
H. Peter Anvine2c80182005-01-15 22:15:51 +00005148 */
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005149 line = detoken(tline, true);
5150 free_tlist(tline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00005151 break;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005152 } else {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005153 continue; /* expand_mmacro calls free_tlist */
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005154 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00005155 }
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005156 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005157
H. Peter Anvin130736c2016-02-17 20:27:41 -08005158done:
5159 nasm_set_verror(real_verror);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005160 return line;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005161}
5162
H. Peter Anvine2c80182005-01-15 22:15:51 +00005163static void pp_cleanup(int pass)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005164{
H. Peter Anvin130736c2016-02-17 20:27:41 -08005165 real_verror = nasm_set_verror(pp_verror);
H. Peter Anvin215186f2016-02-17 20:27:41 -08005166
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005167 if (defining) {
5168 if (defining->name) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03005169 nasm_nonfatal("end of file while still defining macro `%s'",
5170 defining->name);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005171 } else {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03005172 nasm_nonfatal("end of file while still in %%rep");
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005173 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005174
5175 free_mmacro(defining);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005176 defining = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005177 }
H. Peter Anvin130736c2016-02-17 20:27:41 -08005178
5179 nasm_set_verror(real_verror);
H. Peter Anvin215186f2016-02-17 20:27:41 -08005180
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005181 while (cstk)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005182 ctx_pop();
H. Peter Anvin97a23472007-09-16 17:57:25 -07005183 free_macros();
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005184 while (istk) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00005185 Include *i = istk;
5186 istk = istk->next;
5187 fclose(i->fp);
Cyrill Gorcunov8dcfd882011-03-03 09:18:56 +03005188 nasm_free(i);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005189 }
5190 while (cstk)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005191 ctx_pop();
H. Peter Anvin274cda82016-05-10 02:56:29 -07005192 src_set_fname(NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00005193 if (pass == 0) {
5194 free_llist(predef);
Cyrill Gorcunovdae24d72014-06-28 10:17:39 +04005195 predef = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005196 delete_Blocks();
Cyrill Gorcunovdae24d72014-06-28 10:17:39 +04005197 freeTokens = NULL;
Cyrill Gorcunov8c0666b2018-11-24 14:33:48 +03005198 ipath_list = NULL;
H. Peter Anvin11dfa1a2008-07-02 18:11:04 -07005199 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005200}
5201
Cyrill Gorcunov8c0666b2018-11-24 14:33:48 +03005202static void pp_include_path(struct strlist *list)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005203{
Cyrill Gorcunov8c0666b2018-11-24 14:33:48 +03005204 ipath_list = list;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005205}
Frank Kotlerd0ed6fd2003-08-27 11:33:56 +00005206
Cyrill Gorcunov0b78bff2012-05-07 01:57:55 +04005207static void pp_pre_include(char *fname)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005208{
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005209 Token *inc, *space, *name;
5210 Line *l;
5211
H. Peter Anvin734b1882002-04-30 21:01:08 +00005212 name = new_Token(NULL, TOK_INTERNAL_STRING, fname, 0);
5213 space = new_Token(name, TOK_WHITESPACE, NULL, 0);
5214 inc = new_Token(space, TOK_PREPROC_ID, "%include", 0);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005215
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005216 l = nasm_malloc(sizeof(Line));
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005217 l->next = predef;
5218 l->first = inc;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005219 l->finishes = NULL;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005220 predef = l;
5221}
5222
Cyrill Gorcunov0b78bff2012-05-07 01:57:55 +04005223static void pp_pre_define(char *definition)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005224{
5225 Token *def, *space;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005226 Line *l;
Keith Kaniosa6dfa782007-04-13 16:47:53 +00005227 char *equals;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005228
H. Peter Anvin130736c2016-02-17 20:27:41 -08005229 real_verror = nasm_set_verror(pp_verror);
H. Peter Anvin215186f2016-02-17 20:27:41 -08005230
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005231 equals = strchr(definition, '=');
H. Peter Anvin734b1882002-04-30 21:01:08 +00005232 space = new_Token(NULL, TOK_WHITESPACE, NULL, 0);
5233 def = new_Token(space, TOK_PREPROC_ID, "%define", 0);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005234 if (equals)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005235 *equals = ' ';
Keith Kaniosb7a89542007-04-12 02:40:54 +00005236 space->next = tokenize(definition);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005237 if (equals)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005238 *equals = '=';
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005239
Cyrill Gorcunov6d42e9b2015-02-08 11:07:17 +03005240 if (space->next->type != TOK_PREPROC_ID &&
5241 space->next->type != TOK_ID)
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03005242 nasm_warn("pre-defining non ID `%s\'\n", definition);
Cyrill Gorcunov6d42e9b2015-02-08 11:07:17 +03005243
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005244 l = nasm_malloc(sizeof(Line));
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005245 l->next = predef;
5246 l->first = def;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005247 l->finishes = NULL;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005248 predef = l;
H. Peter Anvin130736c2016-02-17 20:27:41 -08005249
5250 nasm_set_verror(real_verror);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005251}
5252
Cyrill Gorcunov0b78bff2012-05-07 01:57:55 +04005253static void pp_pre_undefine(char *definition)
H. Peter Anvin620515a2002-04-30 20:57:38 +00005254{
5255 Token *def, *space;
5256 Line *l;
5257
H. Peter Anvin734b1882002-04-30 21:01:08 +00005258 space = new_Token(NULL, TOK_WHITESPACE, NULL, 0);
5259 def = new_Token(space, TOK_PREPROC_ID, "%undef", 0);
Keith Kaniosb7a89542007-04-12 02:40:54 +00005260 space->next = tokenize(definition);
H. Peter Anvin620515a2002-04-30 20:57:38 +00005261
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005262 l = nasm_malloc(sizeof(Line));
H. Peter Anvin620515a2002-04-30 20:57:38 +00005263 l->next = predef;
5264 l->first = def;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005265 l->finishes = NULL;
H. Peter Anvin620515a2002-04-30 20:57:38 +00005266 predef = l;
5267}
5268
H. Peter Anvin05990342018-06-11 13:32:42 -07005269/* Insert an early preprocessor command that doesn't need special handling */
5270static void pp_pre_command(const char *what, char *string)
5271{
5272 char *cmd;
5273 Token *def, *space;
5274 Line *l;
5275
5276 def = tokenize(string);
5277 if (what) {
5278 cmd = nasm_strcat(what[0] == '%' ? "" : "%", what);
5279 space = new_Token(def, TOK_WHITESPACE, NULL, 0);
5280 def = new_Token(space, TOK_PREPROC_ID, cmd, 0);
5281 }
5282
5283 l = nasm_malloc(sizeof(Line));
5284 l->next = predef;
5285 l->first = def;
5286 l->finishes = NULL;
5287 predef = l;
5288}
5289
H. Peter Anvinf7606612016-07-13 14:23:48 -07005290static void pp_add_stdmac(macros_t *macros)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005291{
H. Peter Anvinf7606612016-07-13 14:23:48 -07005292 macros_t **mp;
5293
5294 /* Find the end of the list and avoid duplicates */
5295 for (mp = stdmacros; *mp; mp++) {
5296 if (*mp == macros)
5297 return; /* Nothing to do */
5298 }
5299
5300 nasm_assert(mp < &stdmacros[ARRAY_SIZE(stdmacros)-1]);
5301
5302 *mp = macros;
H. Peter Anvin76690a12002-04-30 20:52:49 +00005303}
5304
Cyrill Gorcunov15ce78f2017-01-06 20:21:28 +03005305static void pp_extra_stdmac(macros_t *macros)
5306{
5307 extrastdmac = macros;
5308}
5309
Keith Kaniosa5fc6462007-10-13 07:09:22 -07005310static void make_tok_num(Token * tok, int64_t val)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005311{
Cyrill Gorcunovce652742013-05-06 23:43:43 +04005312 char numbuf[32];
Keith Kaniosa5fc6462007-10-13 07:09:22 -07005313 snprintf(numbuf, sizeof(numbuf), "%"PRId64"", val);
H. Peter Anvineba20a72002-04-30 20:53:55 +00005314 tok->text = nasm_strdup(numbuf);
5315 tok->type = TOK_NUMBER;
5316}
5317
H. Peter Anvin (Intel)6bde2ed2018-12-13 19:39:41 -08005318static void pp_list_one_macro(MMacro *m, errflags severity)
H. Peter Anvin37368952016-05-09 14:10:32 -07005319{
5320 if (!m)
5321 return;
5322
5323 /* We need to print the next_active list in reverse order */
5324 pp_list_one_macro(m->next_active, severity);
5325
5326 if (m->name && !m->nolist) {
H. Peter Anvin274cda82016-05-10 02:56:29 -07005327 src_set(m->xline + m->lineno, m->fname);
H. Peter Anvinddb29062018-12-11 00:06:29 -08005328 nasm_error(severity, "... from macro `%s' defined", m->name);
H. Peter Anvin37368952016-05-09 14:10:32 -07005329 }
5330}
5331
H. Peter Anvin (Intel)6bde2ed2018-12-13 19:39:41 -08005332static void pp_error_list_macros(errflags severity)
H. Peter Anvin4def1a82016-05-09 13:59:44 -07005333{
H. Peter Anvinddb29062018-12-11 00:06:29 -08005334 struct src_location saved;
H. Peter Anvin4def1a82016-05-09 13:59:44 -07005335
H. Peter Anvinddb29062018-12-11 00:06:29 -08005336 severity |= ERR_PP_LISTMACRO | ERR_NO_SEVERITY | ERR_HERE;
5337 saved = src_where();
H. Peter Anvin4def1a82016-05-09 13:59:44 -07005338
Cyrill Gorcunov771d04e2016-05-10 23:27:03 +03005339 if (istk)
5340 pp_list_one_macro(istk->mstk, severity);
H. Peter Anvin4def1a82016-05-09 13:59:44 -07005341
H. Peter Anvinddb29062018-12-11 00:06:29 -08005342 src_update(saved);
H. Peter Anvin4def1a82016-05-09 13:59:44 -07005343}
5344
H. Peter Anvine7469712016-02-18 02:20:59 -08005345const struct preproc_ops nasmpp = {
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07005346 pp_init,
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005347 pp_reset,
5348 pp_getline,
Cyrill Gorcunov0b78bff2012-05-07 01:57:55 +04005349 pp_cleanup,
Cyrill Gorcunov15ce78f2017-01-06 20:21:28 +03005350 pp_extra_stdmac,
Cyrill Gorcunov0b78bff2012-05-07 01:57:55 +04005351 pp_pre_define,
5352 pp_pre_undefine,
5353 pp_pre_include,
H. Peter Anvin05990342018-06-11 13:32:42 -07005354 pp_pre_command,
H. Peter Anvin4def1a82016-05-09 13:59:44 -07005355 pp_include_path,
5356 pp_error_list_macros,
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005357};