blob: e5ad05aaf406f3d7f9913c42dc4a9c172fe736a5 [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;
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -0700389static StrList *ipath;
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 */
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -0700392static 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 Anvin130736c2016-02-17 20:27:41 -0800454static void pp_verror(int severity, const char *fmt, va_list ap);
455static 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)
H. Peter Anvin130736c2016-02-17 20:27:41 -0800481 nasm_error(ERR_NONFATAL, "NUL character in `%s' directive",
H. Peter Anvin077fb932010-07-20 14:56:30 -0700482 pp_directives[directive]);
483
484 return clen;
485}
486
487/*
H. Peter Anvinb40992c2010-09-15 08:57:21 -0700488 * In-place reverse a list of tokens.
489 */
490static Token *reverse_tokens(Token *t)
491{
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800492 Token *prev = NULL;
493 Token *next;
H. Peter Anvinb40992c2010-09-15 08:57:21 -0700494
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800495 while (t) {
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +0400496 next = t->next;
497 t->next = prev;
498 prev = t;
499 t = next;
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800500 }
H. Peter Anvinb40992c2010-09-15 08:57:21 -0700501
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800502 return prev;
H. Peter Anvinb40992c2010-09-15 08:57:21 -0700503}
504
505/*
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300506 * Handle TASM specific directives, which do not contain a % in
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000507 * front of them. We do it here because I could not find any other
508 * place to do it for the moment, and it is a hack (ideally it would
509 * be nice to be able to use the NASM pre-processor to do it).
510 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000511static char *check_tasm_directive(char *line)
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000512{
Keith Kaniosb7a89542007-04-12 02:40:54 +0000513 int32_t i, j, k, m, len;
Cyrill Gorcunovf66ac7d2009-10-12 20:41:13 +0400514 char *p, *q, *oldline, oldchar;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000515
Cyrill Gorcunovf66ac7d2009-10-12 20:41:13 +0400516 p = nasm_skip_spaces(line);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000517
518 /* Binary search for the directive name */
519 i = -1;
Cyrill Gorcunova7319242010-06-03 22:04:36 +0400520 j = ARRAY_SIZE(tasm_directives);
Cyrill Gorcunovf66ac7d2009-10-12 20:41:13 +0400521 q = nasm_skip_word(p);
522 len = q - p;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000523 if (len) {
524 oldchar = p[len];
525 p[len] = 0;
526 while (j - i > 1) {
527 k = (j + i) / 2;
528 m = nasm_stricmp(p, tasm_directives[k]);
529 if (m == 0) {
530 /* We have found a directive, so jam a % in front of it
531 * so that NASM will then recognise it as one if it's own.
532 */
533 p[len] = oldchar;
534 len = strlen(p);
535 oldline = line;
536 line = nasm_malloc(len + 2);
537 line[0] = '%';
538 if (k == TM_IFDIFI) {
H. Peter Anvin18f48792009-06-27 15:56:27 -0700539 /*
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300540 * NASM does not recognise IFDIFI, so we convert
541 * it to %if 0. This is not used in NASM
542 * compatible code, but does need to parse for the
543 * TASM macro package.
H. Peter Anvine2c80182005-01-15 22:15:51 +0000544 */
H. Peter Anvin18f48792009-06-27 15:56:27 -0700545 strcpy(line + 1, "if 0");
H. Peter Anvine2c80182005-01-15 22:15:51 +0000546 } else {
547 memcpy(line + 1, p, len + 1);
548 }
549 nasm_free(oldline);
550 return line;
551 } else if (m < 0) {
552 j = k;
553 } else
554 i = k;
555 }
556 p[len] = oldchar;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000557 }
558 return line;
559}
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000560
H. Peter Anvin76690a12002-04-30 20:52:49 +0000561/*
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000562 * The pre-preprocessing stage... This function translates line
563 * number indications as they emerge from GNU cpp (`# lineno "file"
564 * flags') into NASM preprocessor line number indications (`%line
565 * lineno file').
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000566 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000567static char *prepreproc(char *line)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000568{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000569 int lineno, fnlen;
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000570 char *fname, *oldline;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000571
H. Peter Anvine2c80182005-01-15 22:15:51 +0000572 if (line[0] == '#' && line[1] == ' ') {
573 oldline = line;
574 fname = oldline + 2;
575 lineno = atoi(fname);
576 fname += strspn(fname, "0123456789 ");
577 if (*fname == '"')
578 fname++;
579 fnlen = strcspn(fname, "\"");
580 line = nasm_malloc(20 + fnlen);
581 snprintf(line, 20 + fnlen, "%%line %d %.*s", lineno, fnlen, fname);
582 nasm_free(oldline);
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000583 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000584 if (tasm_compatible_mode)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000585 return check_tasm_directive(line);
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000586 return line;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000587}
588
589/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000590 * Free a linked list of tokens.
591 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000592static void free_tlist(Token * list)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000593{
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +0400594 while (list)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000595 list = delete_Token(list);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000596}
597
598/*
599 * Free a linked list of lines.
600 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000601static void free_llist(Line * list)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000602{
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +0400603 Line *l, *tmp;
604 list_for_each_safe(l, tmp, list) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000605 free_tlist(l->first);
606 nasm_free(l);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000607 }
608}
609
610/*
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800611 * Free an MMacro
H. Peter Anvineba20a72002-04-30 20:53:55 +0000612 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800613static void free_mmacro(MMacro * m)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000614{
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800615 nasm_free(m->name);
616 free_tlist(m->dlist);
617 nasm_free(m->defaults);
618 free_llist(m->expansion);
619 nasm_free(m);
H. Peter Anvineba20a72002-04-30 20:53:55 +0000620}
621
622/*
H. Peter Anvin97a23472007-09-16 17:57:25 -0700623 * Free all currently defined macros, and free the hash tables
624 */
H. Peter Anvin072771e2008-05-22 13:17:51 -0700625static void free_smacro_table(struct hash_table *smt)
H. Peter Anvin97a23472007-09-16 17:57:25 -0700626{
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +0400627 SMacro *s, *tmp;
H. Peter Anvin072771e2008-05-22 13:17:51 -0700628 const char *key;
629 struct hash_tbl_node *it = NULL;
H. Peter Anvin97a23472007-09-16 17:57:25 -0700630
H. Peter Anvin072771e2008-05-22 13:17:51 -0700631 while ((s = hash_iterate(smt, &it, &key)) != NULL) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300632 nasm_free((void *)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 Anvin36206cd2012-03-03 16:14:51 -0800644 MMacro *m, *tmp;
H. Peter Anvin072771e2008-05-22 13:17:51 -0700645 const char *key;
646 struct hash_tbl_node *it = NULL;
H. Peter Anvin97a23472007-09-16 17:57:25 -0700647
648 it = NULL;
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800649 while ((m = hash_iterate(mmt, &it, &key)) != NULL) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300650 nasm_free((void *)key);
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800651 list_for_each_safe(m ,tmp, m)
652 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 Anvin166c2472008-05-28 12:28:58 -0700668 hash_init(&smacros, HASH_LARGE);
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800669 hash_init(&mmacros, HASH_LARGE);
H. Peter Anvin97a23472007-09-16 17:57:25 -0700670}
671
672/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000673 * Pop the context stack.
674 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000675static void ctx_pop(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000676{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000677 Context *c = cstk;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000678
679 cstk = cstk->next;
H. Peter Anvin166c2472008-05-28 12:28:58 -0700680 free_smacro_table(&c->localmac);
H. Peter Anvin734b1882002-04-30 21:01:08 +0000681 nasm_free(c->name);
682 nasm_free(c);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000683}
684
H. Peter Anvin072771e2008-05-22 13:17:51 -0700685/*
686 * Search for a key in the hash index; adding it if necessary
687 * (in which case we initialize the data pointer to NULL.)
688 */
689static void **
690hash_findi_add(struct hash_table *hash, const char *str)
691{
692 struct hash_insert hi;
693 void **r;
694 char *strx;
695
696 r = hash_findi(hash, str, &hi);
697 if (r)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300698 return r;
H. Peter Anvin072771e2008-05-22 13:17:51 -0700699
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300700 strx = nasm_strdup(str); /* Use a more efficient allocator here? */
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 != '}')
H. Peter Anvin130736c2016-02-17 20:27:41 -0800912 nasm_error(ERR_WARNING | ERR_PASS1,
913 "unterminated %%{ construct");
H. Peter Anvine2c80182005-01-15 22:15:51 +0000914 p[-1] = '\0';
915 if (*p)
916 p++;
917 type = TOK_PREPROC_ID;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300918 } else if (*p == '[') {
919 int lvl = 1;
920 line += 2; /* Skip the leading %[ */
921 p++;
922 while (lvl && (c = *p++)) {
923 switch (c) {
924 case ']':
925 lvl--;
926 break;
927 case '%':
928 if (*p == '[')
929 lvl++;
930 break;
931 case '\'':
932 case '\"':
933 case '`':
Cyrill Gorcunov3144e842017-10-22 10:50:55 +0300934 p = nasm_skip_string(p - 1);
935 if (*p)
936 p++;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300937 break;
938 default:
939 break;
940 }
941 }
942 p--;
943 if (*p)
944 *p++ = '\0';
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800945 if (lvl)
H. Peter Anvin130736c2016-02-17 20:27:41 -0800946 nasm_error(ERR_NONFATAL|ERR_PASS1,
947 "unterminated %%[ construct");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300948 type = TOK_INDIRECT;
949 } else if (*p == '?') {
950 type = TOK_PREPROC_Q; /* %? */
951 p++;
952 if (*p == '?') {
953 type = TOK_PREPROC_QQ; /* %?? */
954 p++;
955 }
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +0400956 } else if (*p == '!') {
957 type = TOK_PREPROC_ID;
958 p++;
959 if (isidchar(*p)) {
960 do {
961 p++;
962 }
963 while (isidchar(*p));
964 } else if (*p == '\'' || *p == '\"' || *p == '`') {
965 p = nasm_skip_string(p);
966 if (*p)
967 p++;
968 else
H. Peter Anvin130736c2016-02-17 20:27:41 -0800969 nasm_error(ERR_NONFATAL|ERR_PASS1,
970 "unterminated %%! string");
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +0400971 } else {
972 /* %! without string or identifier */
973 type = TOK_OTHER; /* Legacy behavior... */
974 }
H. Peter Anvine2c80182005-01-15 22:15:51 +0000975 } else if (isidchar(*p) ||
976 ((*p == '!' || *p == '%' || *p == '$') &&
977 isidchar(p[1]))) {
978 do {
979 p++;
980 }
981 while (isidchar(*p));
982 type = TOK_PREPROC_ID;
983 } else {
984 type = TOK_OTHER;
985 if (*p == '%')
986 p++;
987 }
988 } else if (isidstart(*p) || (*p == '$' && isidstart(p[1]))) {
989 type = TOK_ID;
990 p++;
991 while (*p && isidchar(*p))
992 p++;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -0700993 } else if (*p == '\'' || *p == '"' || *p == '`') {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000994 /*
995 * A string token.
996 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000997 type = TOK_STRING;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300998 p = nasm_skip_string(p);
Nickolay Yurchenkof3b3ce22003-09-21 20:38:43 +0000999
H. Peter Anvine2c80182005-01-15 22:15:51 +00001000 if (*p) {
1001 p++;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001002 } else {
H. Peter Anvin130736c2016-02-17 20:27:41 -08001003 nasm_error(ERR_WARNING|ERR_PASS1, "unterminated string");
H. Peter Anvine2c80182005-01-15 22:15:51 +00001004 /* Handling unterminated strings by UNV */
1005 /* type = -1; */
1006 }
Victor van den Elzenfb5f2512009-04-17 16:17:59 +02001007 } else if (p[0] == '$' && p[1] == '$') {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001008 type = TOK_OTHER; /* TOKEN_BASE */
Victor van den Elzenfb5f2512009-04-17 16:17:59 +02001009 p += 2;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001010 } else if (isnumstart(*p)) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001011 bool is_hex = false;
1012 bool is_float = false;
1013 bool has_e = false;
1014 char c, *r;
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001015
H. Peter Anvine2c80182005-01-15 22:15:51 +00001016 /*
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001017 * A numeric token.
H. Peter Anvine2c80182005-01-15 22:15:51 +00001018 */
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001019
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001020 if (*p == '$') {
1021 p++;
1022 is_hex = true;
1023 }
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001024
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001025 for (;;) {
1026 c = *p++;
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001027
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001028 if (!is_hex && (c == 'e' || c == 'E')) {
1029 has_e = true;
1030 if (*p == '+' || *p == '-') {
1031 /*
1032 * e can only be followed by +/- if it is either a
1033 * prefixed hex number or a floating-point number
1034 */
1035 p++;
1036 is_float = true;
1037 }
1038 } else if (c == 'H' || c == 'h' || c == 'X' || c == 'x') {
1039 is_hex = true;
1040 } else if (c == 'P' || c == 'p') {
1041 is_float = true;
1042 if (*p == '+' || *p == '-')
1043 p++;
Martin Lindheb150e382016-11-16 15:36:53 +01001044 } else if (isnumchar(c))
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001045 ; /* just advance */
1046 else if (c == '.') {
1047 /*
1048 * we need to deal with consequences of the legacy
1049 * parser, like "1.nolist" being two tokens
1050 * (TOK_NUMBER, TOK_ID) here; at least give it
1051 * a shot for now. In the future, we probably need
1052 * a flex-based scanner with proper pattern matching
1053 * to do it as well as it can be done. Nothing in
1054 * the world is going to help the person who wants
1055 * 0x123.p16 interpreted as two tokens, though.
1056 */
1057 r = p;
1058 while (*r == '_')
1059 r++;
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001060
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001061 if (nasm_isdigit(*r) || (is_hex && nasm_isxdigit(*r)) ||
1062 (!is_hex && (*r == 'e' || *r == 'E')) ||
1063 (*r == 'p' || *r == 'P')) {
1064 p = r;
1065 is_float = true;
1066 } else
1067 break; /* Terminate the token */
1068 } else
1069 break;
1070 }
1071 p--; /* Point to first character beyond number */
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001072
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001073 if (p == line+1 && *line == '$') {
1074 type = TOK_OTHER; /* TOKEN_HERE */
1075 } else {
1076 if (has_e && !is_hex) {
1077 /* 1e13 is floating-point, but 1e13h is not */
1078 is_float = true;
1079 }
H. Peter Anvind784a082009-04-20 14:01:18 -07001080
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001081 type = is_float ? TOK_FLOAT : TOK_NUMBER;
1082 }
H. Peter Anvinbda7a6e2008-06-21 10:23:17 -07001083 } else if (nasm_isspace(*p)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001084 type = TOK_WHITESPACE;
Cyrill Gorcunovf66ac7d2009-10-12 20:41:13 +04001085 p = nasm_skip_spaces(p);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001086 /*
1087 * Whitespace just before end-of-line is discarded by
1088 * pretending it's a comment; whitespace just before a
1089 * comment gets lumped into the comment.
1090 */
1091 if (!*p || *p == ';') {
1092 type = TOK_COMMENT;
1093 while (*p)
1094 p++;
1095 }
1096 } else if (*p == ';') {
1097 type = TOK_COMMENT;
1098 while (*p)
1099 p++;
1100 } else {
1101 /*
1102 * Anything else is an operator of some kind. We check
1103 * for all the double-character operators (>>, <<, //,
1104 * %%, <=, >=, ==, !=, <>, &&, ||, ^^), but anything
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001105 * else is a single-character operator.
H. Peter Anvine2c80182005-01-15 22:15:51 +00001106 */
1107 type = TOK_OTHER;
1108 if ((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[0] == '&' && p[1] == '&') ||
1117 (p[0] == '|' && p[1] == '|') ||
1118 (p[0] == '^' && p[1] == '^')) {
1119 p++;
1120 }
1121 p++;
1122 }
Nickolay Yurchenkof3b3ce22003-09-21 20:38:43 +00001123
H. Peter Anvine2c80182005-01-15 22:15:51 +00001124 /* Handling unterminated string by UNV */
1125 /*if (type == -1)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001126 {
1127 *tail = t = new_Token(NULL, TOK_STRING, line, p-line+1);
1128 t->text[p-line] = *line;
1129 tail = &t->next;
1130 }
1131 else */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001132 if (type != TOK_COMMENT) {
1133 *tail = t = new_Token(NULL, type, line, p - line);
1134 tail = &t->next;
1135 }
1136 line = p;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001137 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001138 return list;
1139}
1140
H. Peter Anvince616072002-04-30 21:02:23 +00001141/*
1142 * this function allocates a new managed block of memory and
H. Peter Anvin70653092007-10-19 14:42:29 -07001143 * returns a pointer to the block. The managed blocks are
H. Peter Anvince616072002-04-30 21:02:23 +00001144 * deleted only all at once by the delete_Blocks function.
1145 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001146static void *new_Block(size_t size)
H. Peter Anvince616072002-04-30 21:02:23 +00001147{
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001148 Blocks *b = &blocks;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001149
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001150 /* first, get to the end of the linked list */
1151 while (b->next)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001152 b = b->next;
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001153 /* now allocate the requested chunk */
1154 b->chunk = nasm_malloc(size);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001155
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001156 /* now allocate a new block for the next request */
Cyrill Gorcunovc31767c2014-06-28 02:22:17 +04001157 b->next = nasm_zalloc(sizeof(Blocks));
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001158 return b->chunk;
H. Peter Anvince616072002-04-30 21:02:23 +00001159}
1160
1161/*
1162 * this function deletes all managed blocks of memory
1163 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001164static void delete_Blocks(void)
H. Peter Anvince616072002-04-30 21:02:23 +00001165{
H. Peter Anvine2c80182005-01-15 22:15:51 +00001166 Blocks *a, *b = &blocks;
H. Peter Anvince616072002-04-30 21:02:23 +00001167
H. Peter Anvin70653092007-10-19 14:42:29 -07001168 /*
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001169 * keep in mind that the first block, pointed to by blocks
H. Peter Anvin70653092007-10-19 14:42:29 -07001170 * is a static and not dynamically allocated, so we don't
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001171 * free it.
1172 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001173 while (b) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001174 if (b->chunk)
1175 nasm_free(b->chunk);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001176 a = b;
1177 b = b->next;
1178 if (a != &blocks)
1179 nasm_free(a);
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001180 }
Cyrill Gorcunovdae24d72014-06-28 10:17:39 +04001181 memset(&blocks, 0, sizeof(blocks));
H. Peter Anvine2c80182005-01-15 22:15:51 +00001182}
H. Peter Anvin734b1882002-04-30 21:01:08 +00001183
1184/*
H. Peter Anvin70653092007-10-19 14:42:29 -07001185 * this function creates a new Token and passes a pointer to it
H. Peter Anvin734b1882002-04-30 21:01:08 +00001186 * back to the caller. It sets the type and text elements, and
H. Peter Anvinf26e0972008-07-01 21:26:27 -07001187 * also the a.mac and next elements to NULL.
H. Peter Anvin734b1882002-04-30 21:01:08 +00001188 */
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07001189static Token *new_Token(Token * next, enum pp_token_type type,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001190 const char *text, int txtlen)
H. Peter Anvin734b1882002-04-30 21:01:08 +00001191{
1192 Token *t;
1193 int i;
1194
H. Peter Anvin89cee572009-07-15 09:16:54 -04001195 if (!freeTokens) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001196 freeTokens = (Token *) new_Block(TOKEN_BLOCKSIZE * sizeof(Token));
1197 for (i = 0; i < TOKEN_BLOCKSIZE - 1; i++)
1198 freeTokens[i].next = &freeTokens[i + 1];
1199 freeTokens[i].next = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001200 }
1201 t = freeTokens;
1202 freeTokens = t->next;
1203 t->next = next;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07001204 t->a.mac = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001205 t->type = type;
H. Peter Anvin89cee572009-07-15 09:16:54 -04001206 if (type == TOK_WHITESPACE || !text) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001207 t->text = NULL;
1208 } else {
1209 if (txtlen == 0)
1210 txtlen = strlen(text);
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07001211 t->text = nasm_malloc(txtlen+1);
1212 memcpy(t->text, text, txtlen);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001213 t->text[txtlen] = '\0';
H. Peter Anvin734b1882002-04-30 21:01:08 +00001214 }
1215 return t;
1216}
1217
H. Peter Anvine2c80182005-01-15 22:15:51 +00001218static Token *delete_Token(Token * t)
H. Peter Anvin734b1882002-04-30 21:01:08 +00001219{
1220 Token *next = t->next;
1221 nasm_free(t->text);
H. Peter Anvin788e6c12002-04-30 21:02:01 +00001222 t->next = freeTokens;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001223 freeTokens = t;
1224 return next;
1225}
1226
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001227/*
1228 * Convert a line of tokens back into text.
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001229 * If expand_locals is not zero, identifiers of the form "%$*xxx"
1230 * will be transformed into ..@ctxnum.xxx
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001231 */
H. Peter Anvin9e200162008-06-04 17:23:14 -07001232static char *detoken(Token * tlist, bool expand_locals)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001233{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001234 Token *t;
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001235 char *line, *p;
H. Peter Anvinb4daadc2008-01-21 16:31:57 -08001236 const char *q;
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001237 int len = 0;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001238
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001239 list_for_each(t, tlist) {
Cyrill Gorcunov9b7ee092017-10-22 21:42:59 +03001240 if (t->type == TOK_PREPROC_ID && t->text &&
1241 t->text[0] && t->text[1] == '!') {
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001242 char *v;
1243 char *q = t->text;
H. Peter Anvin077fb932010-07-20 14:56:30 -07001244
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001245 v = t->text + 2;
1246 if (*v == '\'' || *v == '\"' || *v == '`') {
1247 size_t len = nasm_unquote(v, NULL);
1248 size_t clen = strlen(v);
H. Peter Anvin077fb932010-07-20 14:56:30 -07001249
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001250 if (len != clen) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08001251 nasm_error(ERR_NONFATAL | ERR_PASS1,
1252 "NUL character in %%! string");
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001253 v = NULL;
1254 }
1255 }
H. Peter Anvin077fb932010-07-20 14:56:30 -07001256
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001257 if (v) {
1258 char *p = getenv(v);
1259 if (!p) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08001260 nasm_error(ERR_NONFATAL | ERR_PASS1,
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001261 "nonexistent environment variable `%s'", v);
Cyrill Gorcunovbbb7a1a2016-06-19 12:15:24 +03001262 /*
1263 * FIXME We better should investigate if accessing
1264 * ->text[1] without ->text[0] is safe enough.
1265 */
1266 t->text = nasm_zalloc(2);
1267 } else
1268 t->text = nasm_strdup(p);
Cyrill Gorcunov75004872017-07-26 01:21:16 +03001269 nasm_free(q);
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001270 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001271 }
H. Peter Anvin077fb932010-07-20 14:56:30 -07001272
H. Peter Anvine2c80182005-01-15 22:15:51 +00001273 /* Expand local macros here and not during preprocessing */
1274 if (expand_locals &&
1275 t->type == TOK_PREPROC_ID && t->text &&
1276 t->text[0] == '%' && t->text[1] == '$') {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001277 const char *q;
1278 char *p;
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04001279 Context *ctx = get_ctx(t->text, &q);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001280 if (ctx) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001281 char buffer[40];
Keith Kanios93f2e9a2007-04-14 00:10:59 +00001282 snprintf(buffer, sizeof(buffer), "..@%"PRIu32".", ctx->number);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001283 p = nasm_strcat(buffer, q);
1284 nasm_free(t->text);
1285 t->text = p;
1286 }
1287 }
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001288 if (t->type == TOK_WHITESPACE)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001289 len++;
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001290 else if (t->text)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001291 len += strlen(t->text);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001292 }
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001293
H. Peter Anvin734b1882002-04-30 21:01:08 +00001294 p = line = nasm_malloc(len + 1);
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001295
1296 list_for_each(t, tlist) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001297 if (t->type == TOK_WHITESPACE) {
H. Peter Anvinb4daadc2008-01-21 16:31:57 -08001298 *p++ = ' ';
H. Peter Anvine2c80182005-01-15 22:15:51 +00001299 } else if (t->text) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001300 q = t->text;
1301 while (*q)
1302 *p++ = *q++;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001303 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001304 }
1305 *p = '\0';
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001306
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001307 return line;
1308}
1309
1310/*
H. Peter Anvin76690a12002-04-30 20:52:49 +00001311 * A scanner, suitable for use by the expression evaluator, which
1312 * operates on a line of Tokens. Expects a pointer to a pointer to
1313 * the first token in the line to be passed in as its private_data
1314 * field.
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001315 *
1316 * FIX: This really needs to be unified with stdscan.
H. Peter Anvin76690a12002-04-30 20:52:49 +00001317 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001318static int ppscan(void *private_data, struct tokenval *tokval)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001319{
H. Peter Anvin76690a12002-04-30 20:52:49 +00001320 Token **tlineptr = private_data;
1321 Token *tline;
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001322 char ourcopy[MAX_KEYWORD+1], *p, *r, *s;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001323
H. Peter Anvine2c80182005-01-15 22:15:51 +00001324 do {
1325 tline = *tlineptr;
1326 *tlineptr = tline ? tline->next : NULL;
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04001327 } while (tline && (tline->type == TOK_WHITESPACE ||
1328 tline->type == TOK_COMMENT));
H. Peter Anvin76690a12002-04-30 20:52:49 +00001329
1330 if (!tline)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001331 return tokval->t_type = TOKEN_EOS;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001332
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001333 tokval->t_charptr = tline->text;
1334
H. Peter Anvin76690a12002-04-30 20:52:49 +00001335 if (tline->text[0] == '$' && !tline->text[1])
H. Peter Anvine2c80182005-01-15 22:15:51 +00001336 return tokval->t_type = TOKEN_HERE;
H. Peter Anvin7cf897e2002-05-30 21:30:33 +00001337 if (tline->text[0] == '$' && tline->text[1] == '$' && !tline->text[2])
H. Peter Anvine2c80182005-01-15 22:15:51 +00001338 return tokval->t_type = TOKEN_BASE;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001339
H. Peter Anvine2c80182005-01-15 22:15:51 +00001340 if (tline->type == TOK_ID) {
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001341 p = tokval->t_charptr = tline->text;
1342 if (p[0] == '$') {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001343 tokval->t_charptr++;
1344 return tokval->t_type = TOKEN_ID;
1345 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00001346
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001347 for (r = p, s = ourcopy; *r; r++) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001348 if (r >= p+MAX_KEYWORD)
1349 return tokval->t_type = TOKEN_ID; /* Not a keyword */
H. Peter Anvinac8f8fc2008-06-11 15:49:41 -07001350 *s++ = nasm_tolower(*r);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001351 }
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001352 *s = '\0';
1353 /* right, so we have an identifier sitting in temp storage. now,
1354 * is it actually a register or instruction name, or what? */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001355 return nasm_token_hash(ourcopy, tokval);
H. Peter Anvin76690a12002-04-30 20:52:49 +00001356 }
1357
H. Peter Anvine2c80182005-01-15 22:15:51 +00001358 if (tline->type == TOK_NUMBER) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001359 bool rn_error;
1360 tokval->t_integer = readnum(tline->text, &rn_error);
1361 tokval->t_charptr = tline->text;
1362 if (rn_error)
1363 return tokval->t_type = TOKEN_ERRNUM;
1364 else
1365 return tokval->t_type = TOKEN_NUM;
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001366 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00001367
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001368 if (tline->type == TOK_FLOAT) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001369 return tokval->t_type = TOKEN_FLOAT;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001370 }
1371
H. Peter Anvine2c80182005-01-15 22:15:51 +00001372 if (tline->type == TOK_STRING) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001373 char bq, *ep;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001374
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001375 bq = tline->text[0];
H. Peter Anvin11627042008-06-09 20:45:19 -07001376 tokval->t_charptr = tline->text;
1377 tokval->t_inttwo = nasm_unquote(tline->text, &ep);
H. Peter Anvind2456592008-06-19 15:04:18 -07001378
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001379 if (ep[0] != bq || ep[1] != '\0')
1380 return tokval->t_type = TOKEN_ERRSTR;
1381 else
1382 return tokval->t_type = TOKEN_STR;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001383 }
1384
H. Peter Anvine2c80182005-01-15 22:15:51 +00001385 if (tline->type == TOK_OTHER) {
1386 if (!strcmp(tline->text, "<<"))
1387 return tokval->t_type = TOKEN_SHL;
1388 if (!strcmp(tline->text, ">>"))
1389 return tokval->t_type = TOKEN_SHR;
1390 if (!strcmp(tline->text, "//"))
1391 return tokval->t_type = TOKEN_SDIV;
1392 if (!strcmp(tline->text, "%%"))
1393 return tokval->t_type = TOKEN_SMOD;
1394 if (!strcmp(tline->text, "=="))
1395 return tokval->t_type = TOKEN_EQ;
1396 if (!strcmp(tline->text, "<>"))
1397 return tokval->t_type = TOKEN_NE;
1398 if (!strcmp(tline->text, "!="))
1399 return tokval->t_type = TOKEN_NE;
1400 if (!strcmp(tline->text, "<="))
1401 return tokval->t_type = TOKEN_LE;
1402 if (!strcmp(tline->text, ">="))
1403 return tokval->t_type = TOKEN_GE;
1404 if (!strcmp(tline->text, "&&"))
1405 return tokval->t_type = TOKEN_DBL_AND;
1406 if (!strcmp(tline->text, "^^"))
1407 return tokval->t_type = TOKEN_DBL_XOR;
1408 if (!strcmp(tline->text, "||"))
1409 return tokval->t_type = TOKEN_DBL_OR;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001410 }
1411
1412 /*
1413 * We have no other options: just return the first character of
1414 * the token text.
1415 */
1416 return tokval->t_type = tline->text[0];
1417}
1418
1419/*
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001420 * Compare a string to the name of an existing macro; this is a
1421 * simple wrapper which calls either strcmp or nasm_stricmp
1422 * depending on the value of the `casesense' parameter.
1423 */
H. Peter Anvin4db5a162007-10-11 13:42:09 -07001424static int mstrcmp(const char *p, const char *q, bool casesense)
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001425{
H. Peter Anvin734b1882002-04-30 21:01:08 +00001426 return casesense ? strcmp(p, q) : nasm_stricmp(p, q);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001427}
1428
1429/*
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07001430 * Compare a string to the name of an existing macro; this is a
1431 * simple wrapper which calls either strcmp or nasm_stricmp
1432 * depending on the value of the `casesense' parameter.
1433 */
1434static int mmemcmp(const char *p, const char *q, size_t l, bool casesense)
1435{
1436 return casesense ? memcmp(p, q, l) : nasm_memicmp(p, q, l);
1437}
1438
1439/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001440 * Return the Context structure associated with a %$ token. Return
1441 * NULL, having _already_ reported an error condition, if the
1442 * context stack isn't deep enough for the supplied number of $
1443 * signs.
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001444 *
1445 * If "namep" is non-NULL, set it to the pointer to the macro name
1446 * tail, i.e. the part beyond %$...
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001447 */
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04001448static Context *get_ctx(const char *name, const char **namep)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001449{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001450 Context *ctx;
1451 int i;
1452
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001453 if (namep)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001454 *namep = name;
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001455
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001456 if (!name || name[0] != '%' || name[1] != '$')
H. Peter Anvine2c80182005-01-15 22:15:51 +00001457 return NULL;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001458
H. Peter Anvine2c80182005-01-15 22:15:51 +00001459 if (!cstk) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08001460 nasm_error(ERR_NONFATAL, "`%s': context stack is empty", name);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001461 return NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001462 }
1463
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001464 name += 2;
1465 ctx = cstk;
1466 i = 0;
1467 while (ctx && *name == '$') {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001468 name++;
1469 i++;
1470 ctx = ctx->next;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001471 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001472 if (!ctx) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08001473 nasm_error(ERR_NONFATAL, "`%s': context stack is only"
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001474 " %d level%s deep", name, i, (i == 1 ? "" : "s"));
H. Peter Anvine2c80182005-01-15 22:15:51 +00001475 return NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001476 }
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001477
1478 if (namep)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001479 *namep = name;
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001480
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04001481 return ctx;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001482}
1483
1484/*
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001485 * Open an include file. This routine must always return a valid
1486 * file pointer if it returns - it's responsible for throwing an
1487 * ERR_FATAL and bombing out completely if not. It should also try
1488 * the include path one by one until it finds the file or reaches
1489 * the end of the path.
H. Peter Anvind81a2352016-09-21 14:03:18 -07001490 *
1491 * Note: for INC_PROBE the function returns NULL at all times;
1492 * instead look for the
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001493 */
H. Peter Anvind81a2352016-09-21 14:03:18 -07001494enum incopen_mode {
1495 INC_NEEDED, /* File must exist */
1496 INC_OPTIONAL, /* Missing is OK */
1497 INC_PROBE /* Only an existence probe */
1498};
1499
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001500/* This is conducts a full pathname search */
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07001501static FILE *inc_fopen_search(const char *file, char **slpath,
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001502 enum incopen_mode omode, enum file_flags fmode)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001503{
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001504 FILE *fp;
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07001505 const char *prefix = "";
1506 const struct strlist_entry *ip = ipath->head;
night199ukfdb1a1b2018-10-18 23:19:47 +02001507 char *sp;
H. Peter Anvind81a2352016-09-21 14:03:18 -07001508 bool found;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001509
H. Peter Anvine2c80182005-01-15 22:15:51 +00001510 while (1) {
night199ukfdb1a1b2018-10-18 23:19:47 +02001511 sp = nasm_catfile(prefix, file);
H. Peter Anvind81a2352016-09-21 14:03:18 -07001512 if (omode == INC_PROBE) {
1513 fp = NULL;
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07001514 found = nasm_file_exists(sp);
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001515 } else {
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07001516 fp = nasm_open_read(sp, fmode);
H. Peter Anvind81a2352016-09-21 14:03:18 -07001517 found = (fp != NULL);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001518 }
H. Peter Anvind81a2352016-09-21 14:03:18 -07001519 if (found) {
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07001520 *slpath = sp;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001521 return fp;
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001522 }
Jim Kukunas65a8afc2016-06-13 16:00:42 -04001523
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07001524 nasm_free(sp);
Jim Kukunas65a8afc2016-06-13 16:00:42 -04001525
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07001526 if (!ip) {
1527 *slpath = NULL;
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001528 return NULL;
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07001529 }
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001530
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07001531 prefix = ip->str;
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001532 ip = ip->next;
1533 }
1534}
1535
1536/*
1537 * Open a file, or test for the presence of one (depending on omode),
1538 * considering the include path.
1539 */
1540static FILE *inc_fopen(const char *file,
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07001541 StrList *dhead,
H. Peter Anvinccad6f92016-10-04 00:34:35 -07001542 const char **found_path,
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001543 enum incopen_mode omode,
1544 enum file_flags fmode)
1545{
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001546 struct hash_insert hi;
1547 void **hp;
1548 char *path;
1549 FILE *fp = NULL;
1550
1551 hp = hash_find(&FileHash, file, &hi);
1552 if (hp) {
1553 path = *hp;
Martin Storsjöf283c8f2017-08-13 17:28:46 +03001554 if (path || omode != INC_NEEDED) {
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07001555 strlist_add_string(dhead, path ? path : file);
Martin Storsjöf283c8f2017-08-13 17:28:46 +03001556 }
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001557 } else {
1558 /* Need to do the actual path search */
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07001559 fp = inc_fopen_search(file, &path, omode, fmode);
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001560
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07001561 /* Positive or negative result */
1562 hash_add(&hi, nasm_strdup(file), path);
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001563
H. Peter Anvin9924d1e2016-10-04 00:59:39 -07001564 /*
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07001565 * Add file to dependency path.
H. Peter Anvin9924d1e2016-10-04 00:59:39 -07001566 */
1567 if (path || omode != INC_NEEDED)
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07001568 strlist_add_string(dhead, file);
H. Peter Anvineba20a72002-04-30 20:53:55 +00001569 }
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001570
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001571 if (!path) {
1572 if (omode == INC_NEEDED)
H. Peter Anvinc5136902018-06-15 18:20:17 -07001573 nasm_fatal("unable to open include file `%s'", file);
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07001574 } else {
1575 if (!fp && omode != INC_PROBE)
1576 fp = nasm_open_read(path, fmode);
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001577 }
1578
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001579 if (found_path)
H. Peter Anvinccad6f92016-10-04 00:34:35 -07001580 *found_path = path;
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001581
1582 return fp;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001583}
1584
1585/*
Fabian Giesen0bbc38d2016-04-28 13:48:14 -07001586 * Opens an include or input file. Public version, for use by modules
1587 * that get a file:lineno pair and need to look at the file again
1588 * (e.g. the CodeView debug backend). Returns NULL on failure.
1589 */
H. Peter Anvin3e83cec2016-05-25 04:28:46 -07001590FILE *pp_input_fopen(const char *filename, enum file_flags mode)
Fabian Giesen0bbc38d2016-04-28 13:48:14 -07001591{
H. Peter Anvin9924d1e2016-10-04 00:59:39 -07001592 return inc_fopen(filename, NULL, NULL, INC_OPTIONAL, mode);
Fabian Giesen0bbc38d2016-04-28 13:48:14 -07001593}
1594
1595/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001596 * Determine if we should warn on defining a single-line macro of
H. Peter Anvinef7468f2002-04-30 20:57:59 +00001597 * name `name', with `nparam' parameters. If nparam is 0 or -1, will
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001598 * return true if _any_ single-line macro of that name is defined.
1599 * Otherwise, will return true if a single-line macro with either
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001600 * `nparam' or no parameters is defined.
1601 *
1602 * If a macro with precisely the right number of parameters is
H. Peter Anvinef7468f2002-04-30 20:57:59 +00001603 * defined, or nparam is -1, the address of the definition structure
1604 * will be returned in `defn'; otherwise NULL will be returned. If `defn'
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001605 * is NULL, no action will be taken regarding its contents, and no
1606 * error will occur.
1607 *
1608 * Note that this is also called with nparam zero to resolve
1609 * `ifdef'.
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001610 *
1611 * If you already know which context macro belongs to, you can pass
1612 * the context pointer as first parameter; if you won't but name begins
1613 * with %$ the context will be automatically computed. If all_contexts
1614 * is true, macro will be searched in outer contexts as well.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001615 */
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07001616static bool
H. Peter Anvinb2a5fda2008-06-19 21:42:42 -07001617smacro_defined(Context * ctx, const char *name, int nparam, SMacro ** defn,
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07001618 bool nocase)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001619{
H. Peter Anvin166c2472008-05-28 12:28:58 -07001620 struct hash_table *smtbl;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001621 SMacro *m;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001622
H. Peter Anvin97a23472007-09-16 17:57:25 -07001623 if (ctx) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001624 smtbl = &ctx->localmac;
H. Peter Anvin97a23472007-09-16 17:57:25 -07001625 } else if (name[0] == '%' && name[1] == '$') {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001626 if (cstk)
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04001627 ctx = get_ctx(name, &name);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001628 if (!ctx)
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001629 return false; /* got to return _something_ */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001630 smtbl = &ctx->localmac;
H. Peter Anvin97a23472007-09-16 17:57:25 -07001631 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001632 smtbl = &smacros;
H. Peter Anvin97a23472007-09-16 17:57:25 -07001633 }
H. Peter Anvin166c2472008-05-28 12:28:58 -07001634 m = (SMacro *) hash_findix(smtbl, name);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001635
H. Peter Anvine2c80182005-01-15 22:15:51 +00001636 while (m) {
1637 if (!mstrcmp(m->name, name, m->casesense && nocase) &&
Charles Crayne192d5b52007-10-18 19:02:42 -07001638 (nparam <= 0 || m->nparam == 0 || nparam == (int) m->nparam)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001639 if (defn) {
Charles Crayne192d5b52007-10-18 19:02:42 -07001640 if (nparam == (int) m->nparam || nparam == -1)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001641 *defn = m;
1642 else
1643 *defn = NULL;
1644 }
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001645 return true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001646 }
1647 m = m->next;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001648 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001649
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001650 return false;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001651}
1652
Cyrill Gorcunov3079f792018-11-14 10:03:42 +03001653/* param should be a natural number [0; INT_MAX] */
1654static int read_param_count(const char *str)
1655{
1656 int result;
1657 bool err;
1658
1659 result = readnum(str, &err);
1660 if (result < 0 || result > INT_MAX) {
1661 result = 0;
1662 nasm_error(ERR_NONFATAL, "parameter count `%s' is out of bounds [%d; %d]",
1663 str, 0, INT_MAX);
1664 } else if (err) {
1665 nasm_error(ERR_NONFATAL, "unable to parse parameter count `%s'", str);
1666 }
1667 return result;
1668}
1669
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001670/*
1671 * Count and mark off the parameters in a multi-line macro call.
1672 * This is called both from within the multi-line macro expansion
1673 * code, and also to mark off the default parameters when provided
1674 * in a %macro definition line.
1675 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001676static void count_mmac_params(Token * t, int *nparam, Token *** params)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001677{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001678 int paramsize, brace;
1679
1680 *nparam = paramsize = 0;
1681 *params = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001682 while (t) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001683 /* +1: we need space for the final NULL */
H. Peter Anvin91fb6f12008-09-01 10:56:33 -07001684 if (*nparam+1 >= paramsize) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001685 paramsize += PARAM_DELTA;
1686 *params = nasm_realloc(*params, sizeof(**params) * paramsize);
1687 }
1688 skip_white_(t);
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08001689 brace = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001690 if (tok_is_(t, "{"))
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08001691 brace++;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001692 (*params)[(*nparam)++] = t;
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08001693 if (brace) {
1694 while (brace && (t = t->next) != NULL) {
1695 if (tok_is_(t, "{"))
1696 brace++;
1697 else if (tok_is_(t, "}"))
1698 brace--;
1699 }
1700
1701 if (t) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001702 /*
1703 * Now we've found the closing brace, look further
1704 * for the comma.
1705 */
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08001706 t = t->next;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001707 skip_white_(t);
1708 if (tok_isnt_(t, ",")) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08001709 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00001710 "braces do not enclose all of macro parameter");
1711 while (tok_isnt_(t, ","))
1712 t = t->next;
1713 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001714 }
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08001715 } else {
1716 while (tok_isnt_(t, ","))
1717 t = t->next;
1718 }
1719 if (t) { /* got a comma/brace */
1720 t = t->next; /* eat the comma */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001721 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001722 }
1723}
1724
1725/*
H. Peter Anvin76690a12002-04-30 20:52:49 +00001726 * Determine whether one of the various `if' conditions is true or
1727 * not.
1728 *
1729 * We must free the tline we get passed.
1730 */
H. Peter Anvin70055962007-10-11 00:05:31 -07001731static bool if_condition(Token * tline, enum preproc_token ct)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001732{
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001733 enum pp_conditional i = PP_COND(ct);
H. Peter Anvin70055962007-10-11 00:05:31 -07001734 bool j;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001735 Token *t, *tt, **tptr, *origline;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001736 struct tokenval tokval;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001737 expr *evalresult;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001738 enum pp_token_type needtype;
H. Peter Anvin077fb932010-07-20 14:56:30 -07001739 char *p;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001740
1741 origline = tline;
1742
H. Peter Anvine2c80182005-01-15 22:15:51 +00001743 switch (i) {
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001744 case PPC_IFCTX:
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001745 j = false; /* have we matched yet? */
Victor van den Elzen0e857f12008-07-23 13:21:29 +02001746 while (true) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001747 skip_white_(tline);
Victor van den Elzen0e857f12008-07-23 13:21:29 +02001748 if (!tline)
1749 break;
1750 if (tline->type != TOK_ID) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08001751 nasm_error(ERR_NONFATAL,
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001752 "`%s' expects context identifiers", pp_directives[ct]);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001753 free_tlist(origline);
1754 return -1;
1755 }
Victor van den Elzen0e857f12008-07-23 13:21:29 +02001756 if (cstk && cstk->name && !nasm_stricmp(tline->text, cstk->name))
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001757 j = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001758 tline = tline->next;
1759 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001760 break;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001761
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001762 case PPC_IFDEF:
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001763 j = false; /* have we matched yet? */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001764 while (tline) {
1765 skip_white_(tline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001766 if (!tline || (tline->type != TOK_ID &&
1767 (tline->type != TOK_PREPROC_ID ||
1768 tline->text[1] != '$'))) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08001769 nasm_error(ERR_NONFATAL,
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001770 "`%s' expects macro identifiers", pp_directives[ct]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001771 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001772 }
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07001773 if (smacro_defined(NULL, tline->text, 0, NULL, true))
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001774 j = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001775 tline = tline->next;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001776 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001777 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001778
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001779 case PPC_IFENV:
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001780 tline = expand_smacro(tline);
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001781 j = false; /* have we matched yet? */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001782 while (tline) {
1783 skip_white_(tline);
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001784 if (!tline || (tline->type != TOK_ID &&
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001785 tline->type != TOK_STRING &&
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001786 (tline->type != TOK_PREPROC_ID ||
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001787 tline->text[1] != '!'))) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08001788 nasm_error(ERR_NONFATAL,
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001789 "`%s' expects environment variable names",
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001790 pp_directives[ct]);
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001791 goto fail;
1792 }
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001793 p = tline->text;
1794 if (tline->type == TOK_PREPROC_ID)
1795 p += 2; /* Skip leading %! */
1796 if (*p == '\'' || *p == '\"' || *p == '`')
1797 nasm_unquote_cstr(p, ct);
1798 if (getenv(p))
1799 j = true;
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001800 tline = tline->next;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001801 }
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001802 break;
1803
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001804 case PPC_IFIDN:
1805 case PPC_IFIDNI:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001806 tline = expand_smacro(tline);
1807 t = tt = tline;
1808 while (tok_isnt_(tt, ","))
1809 tt = tt->next;
1810 if (!tt) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08001811 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00001812 "`%s' expects two comma-separated arguments",
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001813 pp_directives[ct]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001814 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001815 }
1816 tt = tt->next;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001817 j = true; /* assume equality unless proved not */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001818 while ((t->type != TOK_OTHER || strcmp(t->text, ",")) && tt) {
1819 if (tt->type == TOK_OTHER && !strcmp(tt->text, ",")) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08001820 nasm_error(ERR_NONFATAL, "`%s': more than one comma on line",
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001821 pp_directives[ct]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001822 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001823 }
1824 if (t->type == TOK_WHITESPACE) {
1825 t = t->next;
1826 continue;
1827 }
1828 if (tt->type == TOK_WHITESPACE) {
1829 tt = tt->next;
1830 continue;
1831 }
1832 if (tt->type != t->type) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001833 j = false; /* found mismatching tokens */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001834 break;
1835 }
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07001836 /* When comparing strings, need to unquote them first */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001837 if (t->type == TOK_STRING) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001838 size_t l1 = nasm_unquote(t->text, NULL);
1839 size_t l2 = nasm_unquote(tt->text, NULL);
H. Peter Anvind2456592008-06-19 15:04:18 -07001840
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001841 if (l1 != l2) {
1842 j = false;
1843 break;
1844 }
1845 if (mmemcmp(t->text, tt->text, l1, i == PPC_IFIDN)) {
1846 j = false;
1847 break;
1848 }
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07001849 } else if (mstrcmp(tt->text, t->text, i == PPC_IFIDN) != 0) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001850 j = false; /* found mismatching tokens */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001851 break;
1852 }
Nickolay Yurchenkof3b3ce22003-09-21 20:38:43 +00001853
H. Peter Anvine2c80182005-01-15 22:15:51 +00001854 t = t->next;
1855 tt = tt->next;
1856 }
1857 if ((t->type != TOK_OTHER || strcmp(t->text, ",")) || tt)
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001858 j = false; /* trailing gunk on one end or other */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001859 break;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001860
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001861 case PPC_IFMACRO:
H. Peter Anvin89cee572009-07-15 09:16:54 -04001862 {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001863 bool found = false;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001864 MMacro searching, *mmac;
H. Peter Anvin65747262002-05-07 00:10:05 +00001865
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001866 skip_white_(tline);
1867 tline = expand_id(tline);
1868 if (!tok_type_(tline, TOK_ID)) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08001869 nasm_error(ERR_NONFATAL,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001870 "`%s' expects a macro name", pp_directives[ct]);
1871 goto fail;
1872 }
1873 searching.name = nasm_strdup(tline->text);
1874 searching.casesense = true;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001875 searching.plus = false;
1876 searching.nolist = false;
1877 searching.in_progress = 0;
1878 searching.max_depth = 0;
1879 searching.rep_nest = NULL;
1880 searching.nparam_min = 0;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001881 searching.nparam_max = INT_MAX;
1882 tline = expand_smacro(tline->next);
1883 skip_white_(tline);
1884 if (!tline) {
1885 } else if (!tok_type_(tline, TOK_NUMBER)) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08001886 nasm_error(ERR_NONFATAL,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001887 "`%s' expects a parameter count or nothing",
1888 pp_directives[ct]);
1889 } else {
1890 searching.nparam_min = searching.nparam_max =
Cyrill Gorcunov3079f792018-11-14 10:03:42 +03001891 read_param_count(tline->text);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001892 }
1893 if (tline && tok_is_(tline->next, "-")) {
1894 tline = tline->next->next;
1895 if (tok_is_(tline, "*"))
1896 searching.nparam_max = INT_MAX;
1897 else if (!tok_type_(tline, TOK_NUMBER))
H. Peter Anvin130736c2016-02-17 20:27:41 -08001898 nasm_error(ERR_NONFATAL,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001899 "`%s' expects a parameter count after `-'",
1900 pp_directives[ct]);
1901 else {
Cyrill Gorcunov3079f792018-11-14 10:03:42 +03001902 searching.nparam_max = read_param_count(tline->text);
Cyrill Gorcunovc9244ea2017-10-22 15:25:48 +03001903 if (searching.nparam_min > searching.nparam_max) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08001904 nasm_error(ERR_NONFATAL,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001905 "minimum parameter count exceeds maximum");
Cyrill Gorcunovc9244ea2017-10-22 15:25:48 +03001906 searching.nparam_max = searching.nparam_min;
1907 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001908 }
1909 }
1910 if (tline && tok_is_(tline->next, "+")) {
1911 tline = tline->next;
1912 searching.plus = true;
1913 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001914 mmac = (MMacro *) hash_findix(&mmacros, searching.name);
1915 while (mmac) {
1916 if (!strcmp(mmac->name, searching.name) &&
1917 (mmac->nparam_min <= searching.nparam_max
1918 || searching.plus)
1919 && (searching.nparam_min <= mmac->nparam_max
1920 || mmac->plus)) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001921 found = true;
1922 break;
1923 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001924 mmac = mmac->next;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001925 }
1926 if (tline && tline->next)
H. Peter Anvin130736c2016-02-17 20:27:41 -08001927 nasm_error(ERR_WARNING|ERR_PASS1,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001928 "trailing garbage after %%ifmacro ignored");
1929 nasm_free(searching.name);
1930 j = found;
1931 break;
H. Peter Anvin89cee572009-07-15 09:16:54 -04001932 }
H. Peter Anvin65747262002-05-07 00:10:05 +00001933
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001934 case PPC_IFID:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001935 needtype = TOK_ID;
1936 goto iftype;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001937 case PPC_IFNUM:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001938 needtype = TOK_NUMBER;
1939 goto iftype;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001940 case PPC_IFSTR:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001941 needtype = TOK_STRING;
1942 goto iftype;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001943
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001944iftype:
1945 t = tline = expand_smacro(tline);
H. Peter Anvind85d2502008-05-04 17:53:31 -07001946
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001947 while (tok_type_(t, TOK_WHITESPACE) ||
1948 (needtype == TOK_NUMBER &&
1949 tok_type_(t, TOK_OTHER) &&
1950 (t->text[0] == '-' || t->text[0] == '+') &&
1951 !t->text[1]))
1952 t = t->next;
H. Peter Anvind85d2502008-05-04 17:53:31 -07001953
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001954 j = tok_type_(t, needtype);
1955 break;
H. Peter Anvincbf768d2008-02-16 16:41:25 -08001956
1957 case PPC_IFTOKEN:
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 Anvincbf768d2008-02-16 16:41:25 -08001961
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001962 j = false;
1963 if (t) {
1964 t = t->next; /* Skip the actual token */
1965 while (tok_type_(t, TOK_WHITESPACE))
1966 t = t->next;
1967 j = !t; /* Should be nothing left */
1968 }
1969 break;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001970
H. Peter Anvin134b9462008-02-16 17:01:40 -08001971 case PPC_IFEMPTY:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001972 t = tline = expand_smacro(tline);
1973 while (tok_type_(t, TOK_WHITESPACE))
1974 t = t->next;
H. Peter Anvin134b9462008-02-16 17:01:40 -08001975
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001976 j = !t; /* Should be empty */
1977 break;
H. Peter Anvin134b9462008-02-16 17:01:40 -08001978
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001979 case PPC_IF:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001980 t = tline = expand_smacro(tline);
1981 tptr = &t;
1982 tokval.t_type = TOKEN_INVALID;
1983 evalresult = evaluate(ppscan, tptr, &tokval,
H. Peter Anvin130736c2016-02-17 20:27:41 -08001984 NULL, pass | CRITICAL, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001985 if (!evalresult)
1986 return -1;
1987 if (tokval.t_type)
H. Peter Anvin130736c2016-02-17 20:27:41 -08001988 nasm_error(ERR_WARNING|ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +00001989 "trailing garbage after expression ignored");
1990 if (!is_simple(evalresult)) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08001991 nasm_error(ERR_NONFATAL,
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001992 "non-constant value given to `%s'", pp_directives[ct]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001993 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001994 }
Chuck Crayne60ae75d2007-05-02 01:59:16 +00001995 j = reloc_value(evalresult) != 0;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001996 break;
H. Peter Anvin95e28822007-09-12 04:20:08 +00001997
H. Peter Anvine2c80182005-01-15 22:15:51 +00001998 default:
H. Peter Anvin130736c2016-02-17 20:27:41 -08001999 nasm_error(ERR_FATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00002000 "preprocessor directive `%s' not yet implemented",
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002001 pp_directives[ct]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002002 goto fail;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002003 }
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002004
2005 free_tlist(origline);
2006 return j ^ PP_NEGATIVE(ct);
H. Peter Anvin70653092007-10-19 14:42:29 -07002007
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002008fail:
2009 free_tlist(origline);
2010 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002011}
2012
2013/*
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002014 * Common code for defining an smacro
2015 */
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08002016static bool define_smacro(Context *ctx, const char *mname, bool casesense,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002017 int nparam, Token *expansion)
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002018{
2019 SMacro *smac, **smhead;
H. Peter Anvin166c2472008-05-28 12:28:58 -07002020 struct hash_table *smtbl;
H. Peter Anvin70653092007-10-19 14:42:29 -07002021
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002022 if (smacro_defined(ctx, mname, nparam, &smac, casesense)) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002023 if (!smac) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002024 nasm_error(ERR_WARNING|ERR_PASS1,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002025 "single-line macro `%s' defined both with and"
2026 " without parameters", mname);
2027 /*
2028 * Some instances of the old code considered this a failure,
2029 * some others didn't. What is the right thing to do here?
2030 */
2031 free_tlist(expansion);
2032 return false; /* Failure */
2033 } else {
2034 /*
2035 * We're redefining, so we have to take over an
2036 * existing SMacro structure. This means freeing
2037 * what was already in it.
2038 */
2039 nasm_free(smac->name);
2040 free_tlist(smac->expansion);
2041 }
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002042 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002043 smtbl = ctx ? &ctx->localmac : &smacros;
2044 smhead = (SMacro **) hash_findi_add(smtbl, mname);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002045 smac = nasm_malloc(sizeof(SMacro));
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002046 smac->next = *smhead;
2047 *smhead = smac;
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002048 }
2049 smac->name = nasm_strdup(mname);
2050 smac->casesense = casesense;
2051 smac->nparam = nparam;
2052 smac->expansion = expansion;
2053 smac->in_progress = false;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002054 return true; /* Success */
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002055}
2056
2057/*
2058 * Undefine an smacro
2059 */
2060static void undef_smacro(Context *ctx, const char *mname)
2061{
2062 SMacro **smhead, *s, **sp;
H. Peter Anvin166c2472008-05-28 12:28:58 -07002063 struct hash_table *smtbl;
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002064
H. Peter Anvin166c2472008-05-28 12:28:58 -07002065 smtbl = ctx ? &ctx->localmac : &smacros;
2066 smhead = (SMacro **)hash_findi(smtbl, mname, NULL);
H. Peter Anvin70653092007-10-19 14:42:29 -07002067
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002068 if (smhead) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002069 /*
2070 * We now have a macro name... go hunt for it.
2071 */
2072 sp = smhead;
2073 while ((s = *sp) != NULL) {
2074 if (!mstrcmp(s->name, mname, s->casesense)) {
2075 *sp = s->next;
2076 nasm_free(s->name);
2077 free_tlist(s->expansion);
2078 nasm_free(s);
2079 } else {
2080 sp = &s->next;
2081 }
2082 }
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002083 }
2084}
2085
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002086/*
H. Peter Anvina26433d2008-07-16 14:40:01 -07002087 * Parse a mmacro specification.
2088 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002089static bool parse_mmacro_spec(Token *tline, MMacro *def, const char *directive)
H. Peter Anvina26433d2008-07-16 14:40:01 -07002090{
H. Peter Anvina26433d2008-07-16 14:40:01 -07002091 tline = tline->next;
2092 skip_white_(tline);
2093 tline = expand_id(tline);
2094 if (!tok_type_(tline, TOK_ID)) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002095 nasm_error(ERR_NONFATAL, "`%s' expects a macro name", directive);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002096 return false;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002097 }
Victor van den Elzenb916ede2008-07-23 15:14:22 +02002098
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002099 def->prev = NULL;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002100 def->name = nasm_strdup(tline->text);
2101 def->plus = false;
2102 def->nolist = false;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002103 def->in_progress = 0;
2104 def->rep_nest = NULL;
Victor van den Elzenb916ede2008-07-23 15:14:22 +02002105 def->nparam_min = 0;
2106 def->nparam_max = 0;
2107
H. Peter Anvina26433d2008-07-16 14:40:01 -07002108 tline = expand_smacro(tline->next);
2109 skip_white_(tline);
2110 if (!tok_type_(tline, TOK_NUMBER)) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002111 nasm_error(ERR_NONFATAL, "`%s' expects a parameter count", directive);
H. Peter Anvina26433d2008-07-16 14:40:01 -07002112 } else {
Cyrill Gorcunov3079f792018-11-14 10:03:42 +03002113 def->nparam_min = def->nparam_max = read_param_count(tline->text);
H. Peter Anvina26433d2008-07-16 14:40:01 -07002114 }
2115 if (tline && tok_is_(tline->next, "-")) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002116 tline = tline->next->next;
2117 if (tok_is_(tline, "*")) {
2118 def->nparam_max = INT_MAX;
2119 } else if (!tok_type_(tline, TOK_NUMBER)) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002120 nasm_error(ERR_NONFATAL,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002121 "`%s' expects a parameter count after `-'", directive);
2122 } else {
Cyrill Gorcunov3079f792018-11-14 10:03:42 +03002123 def->nparam_max = read_param_count(tline->text);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002124 if (def->nparam_min > def->nparam_max) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002125 nasm_error(ERR_NONFATAL, "minimum parameter count exceeds maximum");
Cyrill Gorcunovc9244ea2017-10-22 15:25:48 +03002126 def->nparam_max = def->nparam_min;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002127 }
2128 }
H. Peter Anvina26433d2008-07-16 14:40:01 -07002129 }
2130 if (tline && tok_is_(tline->next, "+")) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002131 tline = tline->next;
2132 def->plus = true;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002133 }
2134 if (tline && tok_type_(tline->next, TOK_ID) &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002135 !nasm_stricmp(tline->next->text, ".nolist")) {
2136 tline = tline->next;
2137 def->nolist = true;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002138 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002139
H. Peter Anvina26433d2008-07-16 14:40:01 -07002140 /*
2141 * Handle default parameters.
2142 */
2143 if (tline && tline->next) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002144 def->dlist = tline->next;
2145 tline->next = NULL;
2146 count_mmac_params(def->dlist, &def->ndefs, &def->defaults);
H. Peter Anvina26433d2008-07-16 14:40:01 -07002147 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002148 def->dlist = NULL;
2149 def->defaults = NULL;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002150 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002151 def->expansion = NULL;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002152
H. Peter Anvin89cee572009-07-15 09:16:54 -04002153 if (def->defaults && def->ndefs > def->nparam_max - def->nparam_min &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002154 !def->plus)
H. Peter Anvin130736c2016-02-17 20:27:41 -08002155 nasm_error(ERR_WARNING|ERR_PASS1|ERR_WARN_MDP,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002156 "too many default macro parameters");
Victor van den Elzenb916ede2008-07-23 15:14:22 +02002157
H. Peter Anvina26433d2008-07-16 14:40:01 -07002158 return true;
2159}
2160
2161
2162/*
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002163 * Decode a size directive
2164 */
2165static int parse_size(const char *str) {
2166 static const char *size_names[] =
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002167 { "byte", "dword", "oword", "qword", "tword", "word", "yword" };
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002168 static const int sizes[] =
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002169 { 0, 1, 4, 16, 8, 10, 2, 32 };
Cyrill Gorcunovc713b5f2018-09-29 14:30:14 +03002170 return str ? sizes[bsii(str, size_names, ARRAY_SIZE(size_names))+1] : 0;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002171}
2172
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07002173/*
2174 * Process a preprocessor %pragma directive. Currently there are none.
2175 * Gets passed the token list starting with the "preproc" token from
2176 * "%pragma preproc".
2177 */
2178static void do_pragma_preproc(Token *tline)
2179{
2180 /* Skip to the real stuff */
2181 tline = tline->next;
2182 skip_white_(tline);
2183 if (!tline)
2184 return;
2185
2186 (void)tline; /* Nothing else to do at present */
2187}
2188
Ed Beroset3ab3f412002-06-11 03:31:49 +00002189/**
2190 * find and process preprocessor directive in passed line
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002191 * Find out if a line contains a preprocessor directive, and deal
2192 * with it if so.
H. Peter Anvin70653092007-10-19 14:42:29 -07002193 *
Ed Beroset3ab3f412002-06-11 03:31:49 +00002194 * If a directive _is_ found, it is the responsibility of this routine
2195 * (and not the caller) to free_tlist() the line.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002196 *
Ed Beroset3ab3f412002-06-11 03:31:49 +00002197 * @param tline a pointer to the current tokeninzed line linked list
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07002198 * @param output if this directive generated output
Ed Beroset3ab3f412002-06-11 03:31:49 +00002199 * @return DIRECTIVE_FOUND or NO_DIRECTIVE_FOUND
H. Peter Anvin70653092007-10-19 14:42:29 -07002200 *
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002201 */
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07002202static int do_directive(Token *tline, char **output)
H. Peter Anvineba20a72002-04-30 20:53:55 +00002203{
H. Peter Anvin4169a472007-09-12 01:29:43 +00002204 enum preproc_token i;
2205 int j;
H. Peter Anvin70055962007-10-11 00:05:31 -07002206 bool err;
2207 int nparam;
2208 bool nolist;
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07002209 bool casesense;
H. Peter Anvin8cfdb9d2007-09-14 18:36:01 -07002210 int k, m;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002211 int offset;
H. Peter Anvinccad6f92016-10-04 00:34:35 -07002212 char *p, *pp;
2213 const char *found_path;
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08002214 const char *mname;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002215 Include *inc;
2216 Context *ctx;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002217 Cond *cond;
2218 MMacro *mmac, **mmhead;
Jin Kyu Songc9486b92013-10-28 17:07:57 -07002219 Token *t = NULL, *tt, *param_start, *macro_start, *last, **tptr, *origline;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002220 Line *l;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002221 struct tokenval tokval;
2222 expr *evalresult;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002223 MMacro *tmp_defining; /* Used when manipulating rep_nest */
H. Peter Anvinf8ba53e2007-10-11 10:11:57 -07002224 int64_t count;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07002225 size_t len;
H. Peter Anvin8e3f75e2008-09-24 00:21:58 -07002226 int severity;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002227
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07002228 *output = NULL; /* No output generated */
H. Peter Anvin76690a12002-04-30 20:52:49 +00002229 origline = tline;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002230
H. Peter Anvineba20a72002-04-30 20:53:55 +00002231 skip_white_(tline);
H. Peter Anvinf2936d72008-06-04 15:11:23 -07002232 if (!tline || !tok_type_(tline, TOK_PREPROC_ID) ||
Cyrill Gorcunov4b5b7372018-10-29 22:54:08 +03002233 (tline->text[0] && (tline->text[1] == '%' ||
2234 tline->text[1] == '$' ||
2235 tline->text[1] == '!')))
H. Peter Anvine2c80182005-01-15 22:15:51 +00002236 return NO_DIRECTIVE_FOUND;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002237
H. Peter Anvin4169a472007-09-12 01:29:43 +00002238 i = pp_token_hash(tline->text);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002239
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002240 /*
2241 * FIXME: We zap execution of PP_RMACRO, PP_IRMACRO, PP_EXITMACRO
2242 * since they are known to be buggy at moment, we need to fix them
2243 * in future release (2.09-2.10)
2244 */
Philipp Klokeb432f572013-03-31 12:01:23 +02002245 if (i == PP_RMACRO || i == PP_IRMACRO || i == PP_EXITMACRO) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002246 nasm_error(ERR_NONFATAL, "unknown preprocessor directive `%s'",
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002247 tline->text);
2248 return NO_DIRECTIVE_FOUND;
2249 }
2250
2251 /*
2252 * If we're in a non-emitting branch of a condition construct,
2253 * or walking to the end of an already terminated %rep block,
2254 * we should ignore all directives except for condition
2255 * directives.
2256 */
2257 if (((istk->conds && !emitting(istk->conds->state)) ||
2258 (istk->mstk && !istk->mstk->in_progress)) && !is_condition(i)) {
2259 return NO_DIRECTIVE_FOUND;
2260 }
2261
2262 /*
2263 * If we're defining a macro or reading a %rep block, we should
2264 * ignore all directives except for %macro/%imacro (which nest),
2265 * %endm/%endmacro, and (only if we're in a %rep block) %endrep.
2266 * If we're in a %rep block, another %rep nests, so should be let through.
2267 */
2268 if (defining && i != PP_MACRO && i != PP_IMACRO &&
2269 i != PP_RMACRO && i != PP_IRMACRO &&
2270 i != PP_ENDMACRO && i != PP_ENDM &&
2271 (defining->name || (i != PP_ENDREP && i != PP_REP))) {
2272 return NO_DIRECTIVE_FOUND;
2273 }
2274
2275 if (defining) {
2276 if (i == PP_MACRO || i == PP_IMACRO ||
2277 i == PP_RMACRO || i == PP_IRMACRO) {
2278 nested_mac_count++;
2279 return NO_DIRECTIVE_FOUND;
2280 } else if (nested_mac_count > 0) {
2281 if (i == PP_ENDMACRO) {
2282 nested_mac_count--;
2283 return NO_DIRECTIVE_FOUND;
2284 }
2285 }
2286 if (!defining->name) {
2287 if (i == PP_REP) {
2288 nested_rep_count++;
2289 return NO_DIRECTIVE_FOUND;
2290 } else if (nested_rep_count > 0) {
2291 if (i == PP_ENDREP) {
2292 nested_rep_count--;
2293 return NO_DIRECTIVE_FOUND;
2294 }
2295 }
2296 }
2297 }
2298
H. Peter Anvin4169a472007-09-12 01:29:43 +00002299 switch (i) {
2300 case PP_INVALID:
H. Peter Anvin130736c2016-02-17 20:27:41 -08002301 nasm_error(ERR_NONFATAL, "unknown preprocessor directive `%s'",
H. Peter Anvine2c80182005-01-15 22:15:51 +00002302 tline->text);
2303 return NO_DIRECTIVE_FOUND; /* didn't get it */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002304
H. Peter Anvin3f87a2a2016-10-04 14:07:19 -07002305 case PP_PRAGMA:
2306 /*
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07002307 * %pragma namespace options...
2308 *
2309 * The namespace "preproc" is reserved for the preprocessor;
2310 * all other namespaces generate a [pragma] assembly directive.
2311 *
2312 * Invalid %pragmas are ignored and may have different
2313 * meaning in future versions of NASM.
H. Peter Anvin3f87a2a2016-10-04 14:07:19 -07002314 */
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07002315 tline = tline->next;
2316 skip_white_(tline);
2317 tline = expand_smacro(tline);
2318 if (tok_type_(tline, TOK_ID)) {
2319 if (!nasm_stricmp(tline->text, "preproc")) {
2320 /* Preprocessor pragma */
2321 do_pragma_preproc(tline);
2322 } else {
2323 /* Build the assembler directive */
2324 t = new_Token(NULL, TOK_OTHER, "[", 1);
2325 t->next = new_Token(NULL, TOK_ID, "pragma", 6);
2326 t->next->next = new_Token(tline, TOK_WHITESPACE, NULL, 0);
2327 tline = t;
2328 for (t = tline; t->next; t = t->next)
2329 ;
2330 t->next = new_Token(NULL, TOK_OTHER, "]", 1);
2331 /* true here can be revisited in the future */
2332 *output = detoken(tline, true);
2333 }
2334 }
H. Peter Anvin3f87a2a2016-10-04 14:07:19 -07002335 free_tlist(origline);
2336 return DIRECTIVE_FOUND;
2337
H. Peter Anvine2c80182005-01-15 22:15:51 +00002338 case PP_STACKSIZE:
2339 /* Directive to tell NASM what the default stack size is. The
2340 * default is for a 16-bit stack, and this can be overriden with
2341 * %stacksize large.
H. Peter Anvine2c80182005-01-15 22:15:51 +00002342 */
2343 tline = tline->next;
2344 if (tline && tline->type == TOK_WHITESPACE)
2345 tline = tline->next;
2346 if (!tline || tline->type != TOK_ID) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002347 nasm_error(ERR_NONFATAL, "`%%stacksize' missing size parameter");
H. Peter Anvine2c80182005-01-15 22:15:51 +00002348 free_tlist(origline);
2349 return DIRECTIVE_FOUND;
2350 }
2351 if (nasm_stricmp(tline->text, "flat") == 0) {
2352 /* All subsequent ARG directives are for a 32-bit stack */
2353 StackSize = 4;
2354 StackPointer = "ebp";
2355 ArgOffset = 8;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002356 LocalOffset = 0;
Charles Crayne7eaf9192007-11-08 22:11:14 -08002357 } else if (nasm_stricmp(tline->text, "flat64") == 0) {
2358 /* All subsequent ARG directives are for a 64-bit stack */
2359 StackSize = 8;
2360 StackPointer = "rbp";
Per Jessen53252e02010-02-11 00:16:59 +03002361 ArgOffset = 16;
Charles Crayne7eaf9192007-11-08 22:11:14 -08002362 LocalOffset = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002363 } else if (nasm_stricmp(tline->text, "large") == 0) {
2364 /* All subsequent ARG directives are for a 16-bit stack,
2365 * far function call.
2366 */
2367 StackSize = 2;
2368 StackPointer = "bp";
2369 ArgOffset = 4;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002370 LocalOffset = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002371 } else if (nasm_stricmp(tline->text, "small") == 0) {
2372 /* All subsequent ARG directives are for a 16-bit stack,
2373 * far function call. We don't support near functions.
2374 */
2375 StackSize = 2;
2376 StackPointer = "bp";
2377 ArgOffset = 6;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002378 LocalOffset = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002379 } else {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002380 nasm_error(ERR_NONFATAL, "`%%stacksize' invalid size type");
H. Peter Anvine2c80182005-01-15 22:15:51 +00002381 free_tlist(origline);
2382 return DIRECTIVE_FOUND;
2383 }
2384 free_tlist(origline);
2385 return DIRECTIVE_FOUND;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002386
H. Peter Anvine2c80182005-01-15 22:15:51 +00002387 case PP_ARG:
2388 /* TASM like ARG directive to define arguments to functions, in
2389 * the following form:
2390 *
2391 * ARG arg1:WORD, arg2:DWORD, arg4:QWORD
2392 */
2393 offset = ArgOffset;
2394 do {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00002395 char *arg, directive[256];
H. Peter Anvine2c80182005-01-15 22:15:51 +00002396 int size = StackSize;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002397
H. Peter Anvine2c80182005-01-15 22:15:51 +00002398 /* Find the argument name */
2399 tline = tline->next;
2400 if (tline && tline->type == TOK_WHITESPACE)
2401 tline = tline->next;
2402 if (!tline || tline->type != TOK_ID) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002403 nasm_error(ERR_NONFATAL, "`%%arg' missing argument parameter");
H. Peter Anvine2c80182005-01-15 22:15:51 +00002404 free_tlist(origline);
2405 return DIRECTIVE_FOUND;
2406 }
2407 arg = tline->text;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002408
H. Peter Anvine2c80182005-01-15 22:15:51 +00002409 /* Find the argument size type */
2410 tline = tline->next;
2411 if (!tline || tline->type != TOK_OTHER
2412 || tline->text[0] != ':') {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002413 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00002414 "Syntax error processing `%%arg' directive");
2415 free_tlist(origline);
2416 return DIRECTIVE_FOUND;
2417 }
2418 tline = tline->next;
2419 if (!tline || tline->type != TOK_ID) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002420 nasm_error(ERR_NONFATAL, "`%%arg' missing size type parameter");
H. Peter Anvine2c80182005-01-15 22:15:51 +00002421 free_tlist(origline);
2422 return DIRECTIVE_FOUND;
2423 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002424
H. Peter Anvine2c80182005-01-15 22:15:51 +00002425 /* Allow macro expansion of type parameter */
Keith Kaniosb7a89542007-04-12 02:40:54 +00002426 tt = tokenize(tline->text);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002427 tt = expand_smacro(tt);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002428 size = parse_size(tt->text);
2429 if (!size) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002430 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00002431 "Invalid size type for `%%arg' missing directive");
2432 free_tlist(tt);
2433 free_tlist(origline);
2434 return DIRECTIVE_FOUND;
2435 }
2436 free_tlist(tt);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002437
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002438 /* Round up to even stack slots */
2439 size = ALIGN(size, StackSize);
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002440
H. Peter Anvine2c80182005-01-15 22:15:51 +00002441 /* Now define the macro for the argument */
2442 snprintf(directive, sizeof(directive), "%%define %s (%s+%d)",
2443 arg, StackPointer, offset);
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07002444 do_directive(tokenize(directive), output);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002445 offset += size;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002446
H. Peter Anvine2c80182005-01-15 22:15:51 +00002447 /* Move to the next argument in the list */
2448 tline = tline->next;
2449 if (tline && tline->type == TOK_WHITESPACE)
2450 tline = tline->next;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002451 } while (tline && tline->type == TOK_OTHER && tline->text[0] == ',');
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002452 ArgOffset = offset;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002453 free_tlist(origline);
2454 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002455
H. Peter Anvine2c80182005-01-15 22:15:51 +00002456 case PP_LOCAL:
2457 /* TASM like LOCAL directive to define local variables for a
2458 * function, in the following form:
2459 *
2460 * LOCAL local1:WORD, local2:DWORD, local4:QWORD = LocalSize
2461 *
2462 * The '= LocalSize' at the end is ignored by NASM, but is
2463 * required by TASM to define the local parameter size (and used
2464 * by the TASM macro package).
2465 */
2466 offset = LocalOffset;
2467 do {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00002468 char *local, directive[256];
H. Peter Anvine2c80182005-01-15 22:15:51 +00002469 int size = StackSize;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002470
H. Peter Anvine2c80182005-01-15 22:15:51 +00002471 /* Find the argument name */
2472 tline = tline->next;
2473 if (tline && tline->type == TOK_WHITESPACE)
2474 tline = tline->next;
2475 if (!tline || tline->type != TOK_ID) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002476 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00002477 "`%%local' missing argument parameter");
2478 free_tlist(origline);
2479 return DIRECTIVE_FOUND;
2480 }
2481 local = tline->text;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002482
H. Peter Anvine2c80182005-01-15 22:15:51 +00002483 /* Find the argument size type */
2484 tline = tline->next;
2485 if (!tline || tline->type != TOK_OTHER
2486 || tline->text[0] != ':') {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002487 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00002488 "Syntax error processing `%%local' directive");
2489 free_tlist(origline);
2490 return DIRECTIVE_FOUND;
2491 }
2492 tline = tline->next;
2493 if (!tline || tline->type != TOK_ID) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002494 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00002495 "`%%local' missing size type parameter");
2496 free_tlist(origline);
2497 return DIRECTIVE_FOUND;
2498 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00002499
H. Peter Anvine2c80182005-01-15 22:15:51 +00002500 /* Allow macro expansion of type parameter */
Keith Kaniosb7a89542007-04-12 02:40:54 +00002501 tt = tokenize(tline->text);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002502 tt = expand_smacro(tt);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002503 size = parse_size(tt->text);
2504 if (!size) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002505 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00002506 "Invalid size type for `%%local' missing directive");
2507 free_tlist(tt);
2508 free_tlist(origline);
2509 return DIRECTIVE_FOUND;
2510 }
2511 free_tlist(tt);
H. Peter Anvin734b1882002-04-30 21:01:08 +00002512
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002513 /* Round up to even stack slots */
2514 size = ALIGN(size, StackSize);
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002515
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002516 offset += size; /* Negative offset, increment before */
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002517
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002518 /* Now define the macro for the argument */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002519 snprintf(directive, sizeof(directive), "%%define %s (%s-%d)",
2520 local, StackPointer, offset);
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07002521 do_directive(tokenize(directive), output);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002522
H. Peter Anvine2c80182005-01-15 22:15:51 +00002523 /* Now define the assign to setup the enter_c macro correctly */
2524 snprintf(directive, sizeof(directive),
2525 "%%assign %%$localsize %%$localsize+%d", size);
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07002526 do_directive(tokenize(directive), output);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002527
H. Peter Anvine2c80182005-01-15 22:15:51 +00002528 /* Move to the next argument in the list */
2529 tline = tline->next;
2530 if (tline && tline->type == TOK_WHITESPACE)
2531 tline = tline->next;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002532 } while (tline && tline->type == TOK_OTHER && tline->text[0] == ',');
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002533 LocalOffset = offset;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002534 free_tlist(origline);
2535 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002536
H. Peter Anvine2c80182005-01-15 22:15:51 +00002537 case PP_CLEAR:
2538 if (tline->next)
H. Peter Anvin130736c2016-02-17 20:27:41 -08002539 nasm_error(ERR_WARNING|ERR_PASS1,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002540 "trailing garbage after `%%clear' ignored");
2541 free_macros();
2542 init_macros();
H. Peter Anvine2c80182005-01-15 22:15:51 +00002543 free_tlist(origline);
2544 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002545
H. Peter Anvin418ca702008-05-30 10:42:30 -07002546 case PP_DEPEND:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002547 t = tline->next = expand_smacro(tline->next);
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002548 skip_white_(t);
2549 if (!t || (t->type != TOK_STRING &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002550 t->type != TOK_INTERNAL_STRING)) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002551 nasm_error(ERR_NONFATAL, "`%%depend' expects a file name");
H. Peter Anvin418ca702008-05-30 10:42:30 -07002552 free_tlist(origline);
2553 return DIRECTIVE_FOUND; /* but we did _something_ */
2554 }
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002555 if (t->next)
H. Peter Anvin130736c2016-02-17 20:27:41 -08002556 nasm_error(ERR_WARNING|ERR_PASS1,
H. Peter Anvin418ca702008-05-30 10:42:30 -07002557 "trailing garbage after `%%depend' ignored");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002558 p = t->text;
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002559 if (t->type != TOK_INTERNAL_STRING)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002560 nasm_unquote_cstr(p, i);
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07002561 strlist_add_string(deplist, p);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002562 free_tlist(origline);
H. Peter Anvin418ca702008-05-30 10:42:30 -07002563 return DIRECTIVE_FOUND;
2564
2565 case PP_INCLUDE:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002566 t = tline->next = expand_smacro(tline->next);
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002567 skip_white_(t);
H. Peter Anvind2456592008-06-19 15:04:18 -07002568
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002569 if (!t || (t->type != TOK_STRING &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002570 t->type != TOK_INTERNAL_STRING)) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002571 nasm_error(ERR_NONFATAL, "`%%include' expects a file name");
H. Peter Anvine2c80182005-01-15 22:15:51 +00002572 free_tlist(origline);
2573 return DIRECTIVE_FOUND; /* but we did _something_ */
2574 }
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002575 if (t->next)
H. Peter Anvin130736c2016-02-17 20:27:41 -08002576 nasm_error(ERR_WARNING|ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +00002577 "trailing garbage after `%%include' ignored");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002578 p = t->text;
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002579 if (t->type != TOK_INTERNAL_STRING)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002580 nasm_unquote_cstr(p, i);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002581 inc = nasm_malloc(sizeof(Include));
H. Peter Anvine2c80182005-01-15 22:15:51 +00002582 inc->next = istk;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002583 inc->conds = NULL;
Jim Kukunas65a8afc2016-06-13 16:00:42 -04002584 found_path = NULL;
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07002585 inc->fp = inc_fopen(p, deplist, &found_path,
H. Peter Anvind81a2352016-09-21 14:03:18 -07002586 pass == 0 ? INC_OPTIONAL : INC_NEEDED, NF_TEXT);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002587 if (!inc->fp) {
2588 /* -MG given but file not found */
2589 nasm_free(inc);
2590 } else {
Jim Kukunas65a8afc2016-06-13 16:00:42 -04002591 inc->fname = src_set_fname(found_path ? found_path : p);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002592 inc->lineno = src_set_linnum(0);
2593 inc->lineinc = 1;
2594 inc->expansion = NULL;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002595 inc->mstk = NULL;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002596 istk = inc;
H. Peter Anvin8ac25aa2016-02-18 01:16:18 -08002597 lfmt->uplevel(LIST_INCLUDE);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002598 }
2599 free_tlist(origline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002600 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002601
H. Peter Anvind2456592008-06-19 15:04:18 -07002602 case PP_USE:
H. Peter Anvinf4ae5ad2008-06-19 18:39:24 -07002603 {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002604 static macros_t *use_pkg;
2605 const char *pkg_macro = NULL;
H. Peter Anvinf4ae5ad2008-06-19 18:39:24 -07002606
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002607 tline = tline->next;
2608 skip_white_(tline);
2609 tline = expand_id(tline);
H. Peter Anvind2456592008-06-19 15:04:18 -07002610
H. Peter Anvin264b7b92008-10-24 16:38:17 -07002611 if (!tline || (tline->type != TOK_STRING &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002612 tline->type != TOK_INTERNAL_STRING &&
2613 tline->type != TOK_ID)) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002614 nasm_error(ERR_NONFATAL, "`%%use' expects a package name");
H. Peter Anvind2456592008-06-19 15:04:18 -07002615 free_tlist(origline);
2616 return DIRECTIVE_FOUND; /* but we did _something_ */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002617 }
H. Peter Anvin264b7b92008-10-24 16:38:17 -07002618 if (tline->next)
H. Peter Anvin130736c2016-02-17 20:27:41 -08002619 nasm_error(ERR_WARNING|ERR_PASS1,
H. Peter Anvind2456592008-06-19 15:04:18 -07002620 "trailing garbage after `%%use' ignored");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002621 if (tline->type == TOK_STRING)
2622 nasm_unquote_cstr(tline->text, i);
2623 use_pkg = nasm_stdmac_find_package(tline->text);
2624 if (!use_pkg)
H. Peter Anvin130736c2016-02-17 20:27:41 -08002625 nasm_error(ERR_NONFATAL, "unknown `%%use' package: %s", tline->text);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002626 else
2627 pkg_macro = (char *)use_pkg + 1; /* The first string will be <%define>__USE_*__ */
Victor van den Elzen35eb2ea2010-03-10 22:33:48 +01002628 if (use_pkg && ! smacro_defined(NULL, pkg_macro, 0, NULL, true)) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002629 /* Not already included, go ahead and include it */
2630 stdmacpos = use_pkg;
2631 }
2632 free_tlist(origline);
H. Peter Anvind2456592008-06-19 15:04:18 -07002633 return DIRECTIVE_FOUND;
H. Peter Anvinf4ae5ad2008-06-19 18:39:24 -07002634 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002635 case PP_PUSH:
H. Peter Anvine2c80182005-01-15 22:15:51 +00002636 case PP_REPL:
H. Peter Anvin42b56392008-10-24 16:24:21 -07002637 case PP_POP:
H. Peter Anvine2c80182005-01-15 22:15:51 +00002638 tline = tline->next;
2639 skip_white_(tline);
2640 tline = expand_id(tline);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002641 if (tline) {
2642 if (!tok_type_(tline, TOK_ID)) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002643 nasm_error(ERR_NONFATAL, "`%s' expects a context identifier",
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002644 pp_directives[i]);
2645 free_tlist(origline);
2646 return DIRECTIVE_FOUND; /* but we did _something_ */
2647 }
2648 if (tline->next)
H. Peter Anvin130736c2016-02-17 20:27:41 -08002649 nasm_error(ERR_WARNING|ERR_PASS1,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002650 "trailing garbage after `%s' ignored",
2651 pp_directives[i]);
2652 p = nasm_strdup(tline->text);
2653 } else {
2654 p = NULL; /* Anonymous */
2655 }
H. Peter Anvin42b56392008-10-24 16:24:21 -07002656
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002657 if (i == PP_PUSH) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002658 ctx = nasm_malloc(sizeof(Context));
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002659 ctx->next = cstk;
2660 hash_init(&ctx->localmac, HASH_SMALL);
2661 ctx->name = p;
2662 ctx->number = unique++;
2663 cstk = ctx;
2664 } else {
2665 /* %pop or %repl */
2666 if (!cstk) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002667 nasm_error(ERR_NONFATAL, "`%s': context stack is empty",
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002668 pp_directives[i]);
2669 } else if (i == PP_POP) {
2670 if (p && (!cstk->name || nasm_stricmp(p, cstk->name)))
H. Peter Anvin130736c2016-02-17 20:27:41 -08002671 nasm_error(ERR_NONFATAL, "`%%pop' in wrong context: %s, "
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002672 "expected %s",
2673 cstk->name ? cstk->name : "anonymous", p);
2674 else
2675 ctx_pop();
2676 } else {
2677 /* i == PP_REPL */
2678 nasm_free(cstk->name);
2679 cstk->name = p;
2680 p = NULL;
2681 }
2682 nasm_free(p);
2683 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002684 free_tlist(origline);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002685 return DIRECTIVE_FOUND;
H. Peter Anvin8e3f75e2008-09-24 00:21:58 -07002686 case PP_FATAL:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002687 severity = ERR_FATAL;
2688 goto issue_error;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002689 case PP_ERROR:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002690 severity = ERR_NONFATAL;
2691 goto issue_error;
H. Peter Anvin7df04172008-06-10 18:27:38 -07002692 case PP_WARNING:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002693 severity = ERR_WARNING|ERR_WARN_USER;
2694 goto issue_error;
H. Peter Anvin8e3f75e2008-09-24 00:21:58 -07002695
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002696issue_error:
H. Peter Anvin7df04172008-06-10 18:27:38 -07002697 {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002698 /* Only error out if this is the final pass */
2699 if (pass != 2 && i != PP_FATAL)
2700 return DIRECTIVE_FOUND;
2701
2702 tline->next = expand_smacro(tline->next);
2703 tline = tline->next;
2704 skip_white_(tline);
2705 t = tline ? tline->next : NULL;
2706 skip_white_(t);
2707 if (tok_type_(tline, TOK_STRING) && !t) {
2708 /* The line contains only a quoted string */
2709 p = tline->text;
2710 nasm_unquote(p, NULL); /* Ignore NUL character truncation */
H. Peter Anvin130736c2016-02-17 20:27:41 -08002711 nasm_error(severity, "%s", p);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002712 } else {
2713 /* Not a quoted string, or more than a quoted string */
2714 p = detoken(tline, false);
H. Peter Anvin130736c2016-02-17 20:27:41 -08002715 nasm_error(severity, "%s", p);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002716 nasm_free(p);
2717 }
2718 free_tlist(origline);
2719 return DIRECTIVE_FOUND;
H. Peter Anvin7df04172008-06-10 18:27:38 -07002720 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00002721
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002722 CASE_PP_IF:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002723 if (istk->conds && !emitting(istk->conds->state))
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002724 j = COND_NEVER;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002725 else {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002726 j = if_condition(tline->next, i);
2727 tline->next = NULL; /* it got freed */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002728 j = j < 0 ? COND_NEVER : j ? COND_IF_TRUE : COND_IF_FALSE;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002729 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002730 cond = nasm_malloc(sizeof(Cond));
2731 cond->next = istk->conds;
2732 cond->state = j;
2733 istk->conds = cond;
2734 if(istk->mstk)
2735 istk->mstk->condcnt ++;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002736 free_tlist(origline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002737 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002738
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002739 CASE_PP_ELIF:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002740 if (!istk->conds)
H. Peter Anvin130736c2016-02-17 20:27:41 -08002741 nasm_error(ERR_FATAL, "`%s': no matching `%%if'", pp_directives[i]);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002742 switch(istk->conds->state) {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002743 case COND_IF_TRUE:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002744 istk->conds->state = COND_DONE;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002745 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002746
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002747 case COND_DONE:
2748 case COND_NEVER:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002749 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002750
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002751 case COND_ELSE_TRUE:
2752 case COND_ELSE_FALSE:
H. Peter Anvin130736c2016-02-17 20:27:41 -08002753 nasm_error(ERR_WARNING|ERR_PASS1|ERR_PP_PRECOND,
2754 "`%%elif' after `%%else' ignored");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002755 istk->conds->state = COND_NEVER;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002756 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002757
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002758 case COND_IF_FALSE:
2759 /*
2760 * IMPORTANT: In the case of %if, we will already have
2761 * called expand_mmac_params(); however, if we're
2762 * processing an %elif we must have been in a
2763 * non-emitting mode, which would have inhibited
2764 * the normal invocation of expand_mmac_params().
2765 * Therefore, we have to do it explicitly here.
2766 */
2767 j = if_condition(expand_mmac_params(tline->next), i);
2768 tline->next = NULL; /* it got freed */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002769 istk->conds->state =
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002770 j < 0 ? COND_NEVER : j ? COND_IF_TRUE : COND_IF_FALSE;
2771 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002772 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002773 free_tlist(origline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002774 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002775
H. Peter Anvine2c80182005-01-15 22:15:51 +00002776 case PP_ELSE:
2777 if (tline->next)
H. Peter Anvin130736c2016-02-17 20:27:41 -08002778 nasm_error(ERR_WARNING|ERR_PASS1|ERR_PP_PRECOND,
2779 "trailing garbage after `%%else' ignored");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002780 if (!istk->conds)
H. Peter Anvinc5136902018-06-15 18:20:17 -07002781 nasm_fatal("`%%else: no matching `%%if'");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002782 switch(istk->conds->state) {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002783 case COND_IF_TRUE:
2784 case COND_DONE:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002785 istk->conds->state = COND_ELSE_FALSE;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002786 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002787
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002788 case COND_NEVER:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002789 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002790
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002791 case COND_IF_FALSE:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002792 istk->conds->state = COND_ELSE_TRUE;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002793 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002794
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002795 case COND_ELSE_TRUE:
2796 case COND_ELSE_FALSE:
H. Peter Anvin130736c2016-02-17 20:27:41 -08002797 nasm_error(ERR_WARNING|ERR_PASS1|ERR_PP_PRECOND,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002798 "`%%else' after `%%else' ignored.");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002799 istk->conds->state = COND_NEVER;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002800 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002801 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002802 free_tlist(origline);
2803 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002804
H. Peter Anvine2c80182005-01-15 22:15:51 +00002805 case PP_ENDIF:
2806 if (tline->next)
H. Peter Anvin130736c2016-02-17 20:27:41 -08002807 nasm_error(ERR_WARNING|ERR_PASS1|ERR_PP_PRECOND,
2808 "trailing garbage after `%%endif' ignored");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002809 if (!istk->conds)
H. Peter Anvin130736c2016-02-17 20:27:41 -08002810 nasm_error(ERR_FATAL, "`%%endif': no matching `%%if'");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002811 cond = istk->conds;
2812 istk->conds = cond->next;
2813 nasm_free(cond);
2814 if(istk->mstk)
2815 istk->mstk->condcnt --;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002816 free_tlist(origline);
2817 return DIRECTIVE_FOUND;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002818
H. Peter Anvindb8f96e2009-07-15 09:07:29 -04002819 case PP_RMACRO:
2820 case PP_IRMACRO:
H. Peter Anvine2c80182005-01-15 22:15:51 +00002821 case PP_MACRO:
2822 case PP_IMACRO:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002823 if (defining) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002824 nasm_error(ERR_FATAL, "`%s': already defining a macro",
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002825 pp_directives[i]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002826 return DIRECTIVE_FOUND;
2827 }
H. Peter Anvin4def1a82016-05-09 13:59:44 -07002828 defining = nasm_zalloc(sizeof(MMacro));
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07002829 defining->max_depth = ((i == PP_RMACRO) || (i == PP_IRMACRO))
2830 ? nasm_limit[LIMIT_MACROS] : 0;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002831 defining->casesense = (i == PP_MACRO) || (i == PP_RMACRO);
2832 if (!parse_mmacro_spec(tline, defining, pp_directives[i])) {
2833 nasm_free(defining);
2834 defining = NULL;
2835 return DIRECTIVE_FOUND;
2836 }
H. Peter Anvina26433d2008-07-16 14:40:01 -07002837
H. Peter Anvin4def1a82016-05-09 13:59:44 -07002838 src_get(&defining->xline, &defining->fname);
2839
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002840 mmac = (MMacro *) hash_findix(&mmacros, defining->name);
2841 while (mmac) {
2842 if (!strcmp(mmac->name, defining->name) &&
2843 (mmac->nparam_min <= defining->nparam_max
2844 || defining->plus)
2845 && (defining->nparam_min <= mmac->nparam_max
2846 || mmac->plus)) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002847 nasm_error(ERR_WARNING|ERR_PASS1,
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002848 "redefining multi-line macro `%s'", defining->name);
2849 return DIRECTIVE_FOUND;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002850 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002851 mmac = mmac->next;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002852 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002853 free_tlist(origline);
2854 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002855
H. Peter Anvine2c80182005-01-15 22:15:51 +00002856 case PP_ENDM:
2857 case PP_ENDMACRO:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002858 if (! (defining && defining->name)) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002859 nasm_error(ERR_NONFATAL, "`%s': not defining a macro", tline->text);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002860 return DIRECTIVE_FOUND;
2861 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002862 mmhead = (MMacro **) hash_findi_add(&mmacros, defining->name);
2863 defining->next = *mmhead;
2864 *mmhead = defining;
2865 defining = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002866 free_tlist(origline);
2867 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002868
H. Peter Anvin89cee572009-07-15 09:16:54 -04002869 case PP_EXITMACRO:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002870 /*
2871 * We must search along istk->expansion until we hit a
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002872 * macro-end marker for a macro with a name. Then we
2873 * bypass all lines between exitmacro and endmacro.
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002874 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002875 list_for_each(l, istk->expansion)
2876 if (l->finishes && l->finishes->name)
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002877 break;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002878
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002879 if (l) {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002880 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002881 * Remove all conditional entries relative to this
2882 * macro invocation. (safe to do in this context)
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002883 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002884 for ( ; l->finishes->condcnt > 0; l->finishes->condcnt --) {
2885 cond = istk->conds;
2886 istk->conds = cond->next;
2887 nasm_free(cond);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002888 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002889 istk->expansion = l;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002890 } else {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002891 nasm_error(ERR_NONFATAL, "`%%exitmacro' not within `%%macro' block");
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002892 }
Keith Kanios852f1ee2009-07-12 00:19:55 -05002893 free_tlist(origline);
2894 return DIRECTIVE_FOUND;
2895
H. Peter Anvina26433d2008-07-16 14:40:01 -07002896 case PP_UNMACRO:
2897 case PP_UNIMACRO:
2898 {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002899 MMacro **mmac_p;
2900 MMacro spec;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002901
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002902 spec.casesense = (i == PP_UNMACRO);
2903 if (!parse_mmacro_spec(tline, &spec, pp_directives[i])) {
2904 return DIRECTIVE_FOUND;
2905 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002906 mmac_p = (MMacro **) hash_findi(&mmacros, spec.name, NULL);
2907 while (mmac_p && *mmac_p) {
2908 mmac = *mmac_p;
2909 if (mmac->casesense == spec.casesense &&
2910 !mstrcmp(mmac->name, spec.name, spec.casesense) &&
2911 mmac->nparam_min == spec.nparam_min &&
2912 mmac->nparam_max == spec.nparam_max &&
2913 mmac->plus == spec.plus) {
2914 *mmac_p = mmac->next;
2915 free_mmacro(mmac);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002916 } else {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002917 mmac_p = &mmac->next;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002918 }
2919 }
2920 free_tlist(origline);
2921 free_tlist(spec.dlist);
2922 return DIRECTIVE_FOUND;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002923 }
2924
H. Peter Anvine2c80182005-01-15 22:15:51 +00002925 case PP_ROTATE:
2926 if (tline->next && tline->next->type == TOK_WHITESPACE)
2927 tline = tline->next;
H. Peter Anvin89cee572009-07-15 09:16:54 -04002928 if (!tline->next) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002929 free_tlist(origline);
H. Peter Anvin130736c2016-02-17 20:27:41 -08002930 nasm_error(ERR_NONFATAL, "`%%rotate' missing rotate count");
H. Peter Anvine2c80182005-01-15 22:15:51 +00002931 return DIRECTIVE_FOUND;
2932 }
2933 t = expand_smacro(tline->next);
2934 tline->next = NULL;
2935 free_tlist(origline);
2936 tline = t;
2937 tptr = &t;
2938 tokval.t_type = TOKEN_INVALID;
2939 evalresult =
H. Peter Anvin130736c2016-02-17 20:27:41 -08002940 evaluate(ppscan, tptr, &tokval, NULL, pass, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002941 free_tlist(tline);
2942 if (!evalresult)
2943 return DIRECTIVE_FOUND;
2944 if (tokval.t_type)
H. Peter Anvin130736c2016-02-17 20:27:41 -08002945 nasm_error(ERR_WARNING|ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +00002946 "trailing garbage after expression ignored");
2947 if (!is_simple(evalresult)) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002948 nasm_error(ERR_NONFATAL, "non-constant value given to `%%rotate'");
H. Peter Anvine2c80182005-01-15 22:15:51 +00002949 return DIRECTIVE_FOUND;
2950 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002951 mmac = istk->mstk;
2952 while (mmac && !mmac->name) /* avoid mistaking %reps for macros */
2953 mmac = mmac->next_active;
2954 if (!mmac) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002955 nasm_error(ERR_NONFATAL, "`%%rotate' invoked outside a macro call");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002956 } else if (mmac->nparam == 0) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002957 nasm_error(ERR_NONFATAL,
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002958 "`%%rotate' invoked within macro without parameters");
2959 } else {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002960 int rotate = mmac->rotate + reloc_value(evalresult);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002961
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002962 rotate %= (int)mmac->nparam;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002963 if (rotate < 0)
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002964 rotate += mmac->nparam;
2965
2966 mmac->rotate = rotate;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002967 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002968 return DIRECTIVE_FOUND;
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00002969
H. Peter Anvine2c80182005-01-15 22:15:51 +00002970 case PP_REP:
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002971 nolist = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002972 do {
2973 tline = tline->next;
2974 } while (tok_type_(tline, TOK_WHITESPACE));
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00002975
H. Peter Anvine2c80182005-01-15 22:15:51 +00002976 if (tok_type_(tline, TOK_ID) &&
2977 nasm_stricmp(tline->text, ".nolist") == 0) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002978 nolist = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002979 do {
2980 tline = tline->next;
2981 } while (tok_type_(tline, TOK_WHITESPACE));
2982 }
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00002983
H. Peter Anvine2c80182005-01-15 22:15:51 +00002984 if (tline) {
2985 t = expand_smacro(tline);
2986 tptr = &t;
2987 tokval.t_type = TOKEN_INVALID;
2988 evalresult =
H. Peter Anvin130736c2016-02-17 20:27:41 -08002989 evaluate(ppscan, tptr, &tokval, NULL, pass, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002990 if (!evalresult) {
2991 free_tlist(origline);
2992 return DIRECTIVE_FOUND;
2993 }
2994 if (tokval.t_type)
H. Peter Anvin130736c2016-02-17 20:27:41 -08002995 nasm_error(ERR_WARNING|ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +00002996 "trailing garbage after expression ignored");
2997 if (!is_simple(evalresult)) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002998 nasm_error(ERR_NONFATAL, "non-constant value given to `%%rep'");
H. Peter Anvine2c80182005-01-15 22:15:51 +00002999 return DIRECTIVE_FOUND;
3000 }
Cyrill Gorcunove091d6e2010-08-09 13:58:22 +04003001 count = reloc_value(evalresult);
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07003002 if (count > nasm_limit[LIMIT_REP]) {
3003 nasm_error(ERR_NONFATAL,
H. Peter Anvina3d96d02018-06-15 17:51:39 -07003004 "`%%rep' count %"PRId64" exceeds limit (currently %"PRId64")",
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07003005 count, nasm_limit[LIMIT_REP]);
Cyrill Gorcunove091d6e2010-08-09 13:58:22 +04003006 count = 0;
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07003007 } else if (count < 0) {
3008 nasm_error(ERR_WARNING|ERR_PASS2|ERR_WARN_NEG_REP,
3009 "negative `%%rep' count: %"PRId64, count);
3010 count = 0;
3011 } else {
Cyrill Gorcunove091d6e2010-08-09 13:58:22 +04003012 count++;
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07003013 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00003014 } else {
H. Peter Anvin130736c2016-02-17 20:27:41 -08003015 nasm_error(ERR_NONFATAL, "`%%rep' expects a repeat count");
H. Peter Anvinf8ba53e2007-10-11 10:11:57 -07003016 count = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003017 }
3018 free_tlist(origline);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003019
3020 tmp_defining = defining;
3021 defining = nasm_malloc(sizeof(MMacro));
3022 defining->prev = NULL;
3023 defining->name = NULL; /* flags this macro as a %rep block */
3024 defining->casesense = false;
3025 defining->plus = false;
3026 defining->nolist = nolist;
3027 defining->in_progress = count;
3028 defining->max_depth = 0;
3029 defining->nparam_min = defining->nparam_max = 0;
3030 defining->defaults = NULL;
3031 defining->dlist = NULL;
3032 defining->expansion = NULL;
3033 defining->next_active = istk->mstk;
3034 defining->rep_nest = tmp_defining;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003035 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003036
H. Peter Anvine2c80182005-01-15 22:15:51 +00003037 case PP_ENDREP:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003038 if (!defining || defining->name) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08003039 nasm_error(ERR_NONFATAL, "`%%endrep': no matching `%%rep'");
H. Peter Anvine2c80182005-01-15 22:15:51 +00003040 return DIRECTIVE_FOUND;
3041 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003042
H. Peter Anvine2c80182005-01-15 22:15:51 +00003043 /*
3044 * Now we have a "macro" defined - although it has no name
3045 * and we won't be entering it in the hash tables - we must
3046 * push a macro-end marker for it on to istk->expansion.
3047 * After that, it will take care of propagating itself (a
3048 * macro-end marker line for a macro which is really a %rep
3049 * block will cause the macro to be re-expanded, complete
3050 * with another macro-end marker to ensure the process
3051 * continues) until the whole expansion is forcibly removed
3052 * from istk->expansion by a %exitrep.
3053 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003054 l = nasm_malloc(sizeof(Line));
3055 l->next = istk->expansion;
3056 l->finishes = defining;
3057 l->first = NULL;
3058 istk->expansion = l;
3059
3060 istk->mstk = defining;
3061
H. Peter Anvin8ac25aa2016-02-18 01:16:18 -08003062 lfmt->uplevel(defining->nolist ? LIST_MACRO_NOLIST : LIST_MACRO);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003063 tmp_defining = defining;
3064 defining = defining->rep_nest;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003065 free_tlist(origline);
3066 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003067
H. Peter Anvine2c80182005-01-15 22:15:51 +00003068 case PP_EXITREP:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003069 /*
3070 * We must search along istk->expansion until we hit a
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003071 * macro-end marker for a macro with no name. Then we set
3072 * its `in_progress' flag to 0.
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003073 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003074 list_for_each(l, istk->expansion)
3075 if (l->finishes && !l->finishes->name)
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003076 break;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003077
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003078 if (l)
3079 l->finishes->in_progress = 1;
3080 else
H. Peter Anvin130736c2016-02-17 20:27:41 -08003081 nasm_error(ERR_NONFATAL, "`%%exitrep' not within `%%rep' block");
H. Peter Anvine2c80182005-01-15 22:15:51 +00003082 free_tlist(origline);
3083 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003084
H. Peter Anvine2c80182005-01-15 22:15:51 +00003085 case PP_XDEFINE:
3086 case PP_IXDEFINE:
3087 case PP_DEFINE:
3088 case PP_IDEFINE:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003089 casesense = (i == PP_DEFINE || i == PP_XDEFINE);
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07003090
H. Peter Anvine2c80182005-01-15 22:15:51 +00003091 tline = tline->next;
3092 skip_white_(tline);
3093 tline = expand_id(tline);
3094 if (!tline || (tline->type != TOK_ID &&
3095 (tline->type != TOK_PREPROC_ID ||
3096 tline->text[1] != '$'))) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08003097 nasm_error(ERR_NONFATAL, "`%s' expects a macro identifier",
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003098 pp_directives[i]);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003099 free_tlist(origline);
3100 return DIRECTIVE_FOUND;
3101 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003102
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04003103 ctx = get_ctx(tline->text, &mname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003104 last = tline;
3105 param_start = tline = tline->next;
3106 nparam = 0;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003107
H. Peter Anvine2c80182005-01-15 22:15:51 +00003108 /* Expand the macro definition now for %xdefine and %ixdefine */
3109 if ((i == PP_XDEFINE) || (i == PP_IXDEFINE))
3110 tline = expand_smacro(tline);
H. Peter Anvin734b1882002-04-30 21:01:08 +00003111
H. Peter Anvine2c80182005-01-15 22:15:51 +00003112 if (tok_is_(tline, "(")) {
3113 /*
3114 * This macro has parameters.
3115 */
H. Peter Anvin734b1882002-04-30 21:01:08 +00003116
H. Peter Anvine2c80182005-01-15 22:15:51 +00003117 tline = tline->next;
3118 while (1) {
3119 skip_white_(tline);
3120 if (!tline) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08003121 nasm_error(ERR_NONFATAL, "parameter identifier expected");
H. Peter Anvine2c80182005-01-15 22:15:51 +00003122 free_tlist(origline);
3123 return DIRECTIVE_FOUND;
3124 }
3125 if (tline->type != TOK_ID) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08003126 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00003127 "`%s': parameter identifier expected",
3128 tline->text);
3129 free_tlist(origline);
3130 return DIRECTIVE_FOUND;
3131 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003132 tline->type = TOK_SMAC_PARAM + nparam++;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003133 tline = tline->next;
3134 skip_white_(tline);
3135 if (tok_is_(tline, ",")) {
3136 tline = tline->next;
H. Peter Anvinca348b62008-07-23 10:49:26 -04003137 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003138 if (!tok_is_(tline, ")")) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08003139 nasm_error(ERR_NONFATAL,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003140 "`)' expected to terminate macro template");
3141 free_tlist(origline);
3142 return DIRECTIVE_FOUND;
3143 }
3144 break;
3145 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00003146 }
3147 last = tline;
3148 tline = tline->next;
3149 }
3150 if (tok_type_(tline, TOK_WHITESPACE))
3151 last = tline, tline = tline->next;
3152 macro_start = NULL;
3153 last->next = NULL;
3154 t = tline;
3155 while (t) {
3156 if (t->type == TOK_ID) {
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04003157 list_for_each(tt, param_start)
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003158 if (tt->type >= TOK_SMAC_PARAM &&
H. Peter Anvine2c80182005-01-15 22:15:51 +00003159 !strcmp(tt->text, t->text))
3160 t->type = tt->type;
3161 }
3162 tt = t->next;
3163 t->next = macro_start;
3164 macro_start = t;
3165 t = tt;
3166 }
3167 /*
3168 * Good. We now have a macro name, a parameter count, and a
3169 * token list (in reverse order) for an expansion. We ought
3170 * to be OK just to create an SMacro, store it, and let
3171 * free_tlist have the rest of the line (which we have
3172 * carefully re-terminated after chopping off the expansion
3173 * from the end).
3174 */
H. Peter Anvin4db5a162007-10-11 13:42:09 -07003175 define_smacro(ctx, mname, casesense, nparam, macro_start);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003176 free_tlist(origline);
3177 return DIRECTIVE_FOUND;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003178
H. Peter Anvine2c80182005-01-15 22:15:51 +00003179 case PP_UNDEF:
3180 tline = tline->next;
3181 skip_white_(tline);
3182 tline = expand_id(tline);
3183 if (!tline || (tline->type != TOK_ID &&
3184 (tline->type != TOK_PREPROC_ID ||
3185 tline->text[1] != '$'))) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08003186 nasm_error(ERR_NONFATAL, "`%%undef' expects a macro identifier");
H. Peter Anvine2c80182005-01-15 22:15:51 +00003187 free_tlist(origline);
3188 return DIRECTIVE_FOUND;
3189 }
3190 if (tline->next) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08003191 nasm_error(ERR_WARNING|ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +00003192 "trailing garbage after macro name ignored");
3193 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003194
H. Peter Anvine2c80182005-01-15 22:15:51 +00003195 /* Find the context that symbol belongs to */
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04003196 ctx = get_ctx(tline->text, &mname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003197 undef_smacro(ctx, mname);
3198 free_tlist(origline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003199 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003200
H. Peter Anvin9e200162008-06-04 17:23:14 -07003201 case PP_DEFSTR:
3202 case PP_IDEFSTR:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003203 casesense = (i == PP_DEFSTR);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003204
3205 tline = tline->next;
3206 skip_white_(tline);
3207 tline = expand_id(tline);
3208 if (!tline || (tline->type != TOK_ID &&
3209 (tline->type != TOK_PREPROC_ID ||
3210 tline->text[1] != '$'))) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08003211 nasm_error(ERR_NONFATAL, "`%s' expects a macro identifier",
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003212 pp_directives[i]);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003213 free_tlist(origline);
3214 return DIRECTIVE_FOUND;
3215 }
3216
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04003217 ctx = get_ctx(tline->text, &mname);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003218 last = tline;
3219 tline = expand_smacro(tline->next);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003220 last->next = NULL;
H. Peter Anvin9e200162008-06-04 17:23:14 -07003221
3222 while (tok_type_(tline, TOK_WHITESPACE))
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003223 tline = delete_Token(tline);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003224
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003225 p = detoken(tline, false);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003226 macro_start = nasm_malloc(sizeof(*macro_start));
3227 macro_start->next = NULL;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003228 macro_start->text = nasm_quote(p, strlen(p));
3229 macro_start->type = TOK_STRING;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003230 macro_start->a.mac = NULL;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003231 nasm_free(p);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003232
3233 /*
3234 * We now have a macro name, an implicit parameter count of
3235 * zero, and a string token to use as an expansion. Create
3236 * and store an SMacro.
3237 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003238 define_smacro(ctx, mname, casesense, 0, macro_start);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003239 free_tlist(origline);
3240 return DIRECTIVE_FOUND;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003241
H. Peter Anvin2f55bda2009-07-14 15:04:04 -04003242 case PP_DEFTOK:
3243 case PP_IDEFTOK:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003244 casesense = (i == PP_DEFTOK);
3245
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003246 tline = tline->next;
3247 skip_white_(tline);
3248 tline = expand_id(tline);
3249 if (!tline || (tline->type != TOK_ID &&
3250 (tline->type != TOK_PREPROC_ID ||
3251 tline->text[1] != '$'))) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08003252 nasm_error(ERR_NONFATAL,
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003253 "`%s' expects a macro identifier as first parameter",
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003254 pp_directives[i]);
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003255 free_tlist(origline);
3256 return DIRECTIVE_FOUND;
3257 }
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04003258 ctx = get_ctx(tline->text, &mname);
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003259 last = tline;
3260 tline = expand_smacro(tline->next);
3261 last->next = NULL;
3262
3263 t = tline;
3264 while (tok_type_(t, TOK_WHITESPACE))
3265 t = t->next;
3266 /* t should now point to the string */
Cyrill Gorcunov6908e582010-09-06 19:36:15 +04003267 if (!tok_type_(t, TOK_STRING)) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08003268 nasm_error(ERR_NONFATAL,
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003269 "`%s` requires string as second parameter",
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003270 pp_directives[i]);
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003271 free_tlist(tline);
3272 free_tlist(origline);
3273 return DIRECTIVE_FOUND;
3274 }
3275
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04003276 /*
3277 * Convert the string to a token stream. Note that smacros
3278 * are stored with the token stream reversed, so we have to
3279 * reverse the output of tokenize().
3280 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003281 nasm_unquote_cstr(t->text, i);
H. Peter Anvinb40992c2010-09-15 08:57:21 -07003282 macro_start = reverse_tokens(tokenize(t->text));
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003283
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003284 /*
3285 * We now have a macro name, an implicit parameter count of
3286 * zero, and a numeric token to use as an expansion. Create
3287 * and store an SMacro.
3288 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003289 define_smacro(ctx, mname, casesense, 0, macro_start);
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003290 free_tlist(tline);
3291 free_tlist(origline);
3292 return DIRECTIVE_FOUND;
H. Peter Anvin9e200162008-06-04 17:23:14 -07003293
H. Peter Anvin418ca702008-05-30 10:42:30 -07003294 case PP_PATHSEARCH:
3295 {
H. Peter Anvinccad6f92016-10-04 00:34:35 -07003296 const char *found_path;
H. Peter Anvin418ca702008-05-30 10:42:30 -07003297
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003298 casesense = true;
H. Peter Anvin418ca702008-05-30 10:42:30 -07003299
3300 tline = tline->next;
3301 skip_white_(tline);
3302 tline = expand_id(tline);
3303 if (!tline || (tline->type != TOK_ID &&
3304 (tline->type != TOK_PREPROC_ID ||
3305 tline->text[1] != '$'))) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08003306 nasm_error(ERR_NONFATAL,
H. Peter Anvin418ca702008-05-30 10:42:30 -07003307 "`%%pathsearch' expects a macro identifier as first parameter");
3308 free_tlist(origline);
3309 return DIRECTIVE_FOUND;
3310 }
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04003311 ctx = get_ctx(tline->text, &mname);
H. Peter Anvin418ca702008-05-30 10:42:30 -07003312 last = tline;
3313 tline = expand_smacro(tline->next);
3314 last->next = NULL;
3315
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003316 t = tline;
H. Peter Anvin418ca702008-05-30 10:42:30 -07003317 while (tok_type_(t, TOK_WHITESPACE))
3318 t = t->next;
3319
3320 if (!t || (t->type != TOK_STRING &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003321 t->type != TOK_INTERNAL_STRING)) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08003322 nasm_error(ERR_NONFATAL, "`%%pathsearch' expects a file name");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003323 free_tlist(tline);
H. Peter Anvin418ca702008-05-30 10:42:30 -07003324 free_tlist(origline);
3325 return DIRECTIVE_FOUND; /* but we did _something_ */
3326 }
3327 if (t->next)
H. Peter Anvin130736c2016-02-17 20:27:41 -08003328 nasm_error(ERR_WARNING|ERR_PASS1,
H. Peter Anvin418ca702008-05-30 10:42:30 -07003329 "trailing garbage after `%%pathsearch' ignored");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003330 p = t->text;
H. Peter Anvin427cc912008-06-01 21:43:03 -07003331 if (t->type != TOK_INTERNAL_STRING)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003332 nasm_unquote(p, NULL);
H. Peter Anvin418ca702008-05-30 10:42:30 -07003333
H. Peter Anvin9924d1e2016-10-04 00:59:39 -07003334 inc_fopen(p, NULL, &found_path, INC_PROBE, NF_BINARY);
H. Peter Anvinccad6f92016-10-04 00:34:35 -07003335 if (!found_path)
3336 found_path = p;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003337 macro_start = nasm_malloc(sizeof(*macro_start));
3338 macro_start->next = NULL;
H. Peter Anvinccad6f92016-10-04 00:34:35 -07003339 macro_start->text = nasm_quote(found_path, strlen(found_path));
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003340 macro_start->type = TOK_STRING;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003341 macro_start->a.mac = NULL;
H. Peter Anvin418ca702008-05-30 10:42:30 -07003342
3343 /*
3344 * We now have a macro name, an implicit parameter count of
3345 * zero, and a string token to use as an expansion. Create
3346 * and store an SMacro.
3347 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003348 define_smacro(ctx, mname, casesense, 0, macro_start);
3349 free_tlist(tline);
H. Peter Anvin418ca702008-05-30 10:42:30 -07003350 free_tlist(origline);
3351 return DIRECTIVE_FOUND;
3352 }
3353
H. Peter Anvine2c80182005-01-15 22:15:51 +00003354 case PP_STRLEN:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003355 casesense = true;
H. Peter Anvin70653092007-10-19 14:42:29 -07003356
H. Peter Anvine2c80182005-01-15 22:15:51 +00003357 tline = tline->next;
3358 skip_white_(tline);
3359 tline = expand_id(tline);
3360 if (!tline || (tline->type != TOK_ID &&
3361 (tline->type != TOK_PREPROC_ID ||
3362 tline->text[1] != '$'))) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08003363 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00003364 "`%%strlen' expects a macro identifier as first parameter");
3365 free_tlist(origline);
3366 return DIRECTIVE_FOUND;
3367 }
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04003368 ctx = get_ctx(tline->text, &mname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003369 last = tline;
3370 tline = expand_smacro(tline->next);
3371 last->next = NULL;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003372
H. Peter Anvine2c80182005-01-15 22:15:51 +00003373 t = tline;
3374 while (tok_type_(t, TOK_WHITESPACE))
3375 t = t->next;
3376 /* t should now point to the string */
Cyrill Gorcunov4e1d5ab2010-07-23 18:51:51 +04003377 if (!tok_type_(t, TOK_STRING)) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08003378 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00003379 "`%%strlen` requires string as second parameter");
3380 free_tlist(tline);
3381 free_tlist(origline);
3382 return DIRECTIVE_FOUND;
3383 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003384
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003385 macro_start = nasm_malloc(sizeof(*macro_start));
3386 macro_start->next = NULL;
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07003387 make_tok_num(macro_start, nasm_unquote(t->text, NULL));
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003388 macro_start->a.mac = NULL;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003389
H. Peter Anvine2c80182005-01-15 22:15:51 +00003390 /*
3391 * We now have a macro name, an implicit parameter count of
3392 * zero, and a numeric token to use as an expansion. Create
3393 * and store an SMacro.
3394 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003395 define_smacro(ctx, mname, casesense, 0, macro_start);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003396 free_tlist(tline);
3397 free_tlist(origline);
3398 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003399
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003400 case PP_STRCAT:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003401 casesense = true;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003402
3403 tline = tline->next;
3404 skip_white_(tline);
3405 tline = expand_id(tline);
3406 if (!tline || (tline->type != TOK_ID &&
3407 (tline->type != TOK_PREPROC_ID ||
3408 tline->text[1] != '$'))) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08003409 nasm_error(ERR_NONFATAL,
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003410 "`%%strcat' expects a macro identifier as first parameter");
3411 free_tlist(origline);
3412 return DIRECTIVE_FOUND;
3413 }
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04003414 ctx = get_ctx(tline->text, &mname);
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003415 last = tline;
3416 tline = expand_smacro(tline->next);
3417 last->next = NULL;
3418
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003419 len = 0;
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04003420 list_for_each(t, tline) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003421 switch (t->type) {
3422 case TOK_WHITESPACE:
3423 break;
3424 case TOK_STRING:
3425 len += t->a.len = nasm_unquote(t->text, NULL);
3426 break;
3427 case TOK_OTHER:
3428 if (!strcmp(t->text, ",")) /* permit comma separators */
3429 break;
3430 /* else fall through */
3431 default:
H. Peter Anvin130736c2016-02-17 20:27:41 -08003432 nasm_error(ERR_NONFATAL,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003433 "non-string passed to `%%strcat' (%d)", t->type);
3434 free_tlist(tline);
3435 free_tlist(origline);
3436 return DIRECTIVE_FOUND;
3437 }
3438 }
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003439
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003440 p = pp = nasm_malloc(len);
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04003441 list_for_each(t, tline) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003442 if (t->type == TOK_STRING) {
3443 memcpy(p, t->text, t->a.len);
3444 p += t->a.len;
3445 }
3446 }
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003447
3448 /*
3449 * We now have a macro name, an implicit parameter count of
3450 * zero, and a numeric token to use as an expansion. Create
3451 * and store an SMacro.
3452 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003453 macro_start = new_Token(NULL, TOK_STRING, NULL, 0);
3454 macro_start->text = nasm_quote(pp, len);
3455 nasm_free(pp);
3456 define_smacro(ctx, mname, casesense, 0, macro_start);
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003457 free_tlist(tline);
3458 free_tlist(origline);
3459 return DIRECTIVE_FOUND;
3460
H. Peter Anvine2c80182005-01-15 22:15:51 +00003461 case PP_SUBSTR:
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003462 {
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003463 int64_t start, count;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003464 size_t len;
H. Peter Anvind2456592008-06-19 15:04:18 -07003465
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003466 casesense = true;
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07003467
H. Peter Anvine2c80182005-01-15 22:15:51 +00003468 tline = tline->next;
3469 skip_white_(tline);
3470 tline = expand_id(tline);
3471 if (!tline || (tline->type != TOK_ID &&
3472 (tline->type != TOK_PREPROC_ID ||
3473 tline->text[1] != '$'))) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08003474 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00003475 "`%%substr' expects a macro identifier as first parameter");
3476 free_tlist(origline);
3477 return DIRECTIVE_FOUND;
3478 }
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04003479 ctx = get_ctx(tline->text, &mname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003480 last = tline;
3481 tline = expand_smacro(tline->next);
3482 last->next = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003483
Cyrill Gorcunov35519d62010-09-06 23:49:52 +04003484 if (tline) /* skip expanded id */
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003485 t = tline->next;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003486 while (tok_type_(t, TOK_WHITESPACE))
3487 t = t->next;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003488
H. Peter Anvine2c80182005-01-15 22:15:51 +00003489 /* t should now point to the string */
Cyrill Gorcunov35519d62010-09-06 23:49:52 +04003490 if (!tok_type_(t, TOK_STRING)) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08003491 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00003492 "`%%substr` requires string as second parameter");
3493 free_tlist(tline);
3494 free_tlist(origline);
3495 return DIRECTIVE_FOUND;
3496 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003497
H. Peter Anvine2c80182005-01-15 22:15:51 +00003498 tt = t->next;
3499 tptr = &tt;
3500 tokval.t_type = TOKEN_INVALID;
H. Peter Anvin130736c2016-02-17 20:27:41 -08003501 evalresult = evaluate(ppscan, tptr, &tokval, NULL, pass, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003502 if (!evalresult) {
3503 free_tlist(tline);
3504 free_tlist(origline);
3505 return DIRECTIVE_FOUND;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003506 } else if (!is_simple(evalresult)) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08003507 nasm_error(ERR_NONFATAL, "non-constant value given to `%%substr`");
H. Peter Anvine2c80182005-01-15 22:15:51 +00003508 free_tlist(tline);
3509 free_tlist(origline);
3510 return DIRECTIVE_FOUND;
3511 }
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003512 start = evalresult->value - 1;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003513
3514 while (tok_type_(tt, TOK_WHITESPACE))
3515 tt = tt->next;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003516 if (!tt) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003517 count = 1; /* Backwards compatibility: one character */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003518 } else {
3519 tokval.t_type = TOKEN_INVALID;
H. Peter Anvin130736c2016-02-17 20:27:41 -08003520 evalresult = evaluate(ppscan, tptr, &tokval, NULL, pass, NULL);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003521 if (!evalresult) {
3522 free_tlist(tline);
3523 free_tlist(origline);
3524 return DIRECTIVE_FOUND;
3525 } else if (!is_simple(evalresult)) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08003526 nasm_error(ERR_NONFATAL, "non-constant value given to `%%substr`");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003527 free_tlist(tline);
3528 free_tlist(origline);
3529 return DIRECTIVE_FOUND;
3530 }
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003531 count = evalresult->value;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003532 }
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003533
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003534 len = nasm_unquote(t->text, NULL);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003535
Cyrill Gorcunovcff031e2010-09-07 20:31:11 +04003536 /* make start and count being in range */
3537 if (start < 0)
3538 start = 0;
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003539 if (count < 0)
3540 count = len + count + 1 - start;
3541 if (start + count > (int64_t)len)
Cyrill Gorcunovcff031e2010-09-07 20:31:11 +04003542 count = len - start;
3543 if (!len || count < 0 || start >=(int64_t)len)
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003544 start = -1, count = 0; /* empty string */
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003545
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003546 macro_start = nasm_malloc(sizeof(*macro_start));
3547 macro_start->next = NULL;
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003548 macro_start->text = nasm_quote((start < 0) ? "" : t->text + start, count);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003549 macro_start->type = TOK_STRING;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003550 macro_start->a.mac = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003551
H. Peter Anvine2c80182005-01-15 22:15:51 +00003552 /*
3553 * We now have a macro name, an implicit parameter count of
3554 * zero, and a numeric token to use as an expansion. Create
3555 * and store an SMacro.
3556 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003557 define_smacro(ctx, mname, casesense, 0, macro_start);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003558 free_tlist(tline);
3559 free_tlist(origline);
3560 return DIRECTIVE_FOUND;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003561 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003562
H. Peter Anvine2c80182005-01-15 22:15:51 +00003563 case PP_ASSIGN:
3564 case PP_IASSIGN:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003565 casesense = (i == PP_ASSIGN);
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07003566
H. Peter Anvine2c80182005-01-15 22:15:51 +00003567 tline = tline->next;
3568 skip_white_(tline);
3569 tline = expand_id(tline);
3570 if (!tline || (tline->type != TOK_ID &&
3571 (tline->type != TOK_PREPROC_ID ||
3572 tline->text[1] != '$'))) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08003573 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00003574 "`%%%sassign' expects a macro identifier",
3575 (i == PP_IASSIGN ? "i" : ""));
3576 free_tlist(origline);
3577 return DIRECTIVE_FOUND;
3578 }
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04003579 ctx = get_ctx(tline->text, &mname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003580 last = tline;
3581 tline = expand_smacro(tline->next);
3582 last->next = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003583
H. Peter Anvine2c80182005-01-15 22:15:51 +00003584 t = tline;
3585 tptr = &t;
3586 tokval.t_type = TOKEN_INVALID;
H. Peter Anvin130736c2016-02-17 20:27:41 -08003587 evalresult = evaluate(ppscan, tptr, &tokval, NULL, pass, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003588 free_tlist(tline);
3589 if (!evalresult) {
3590 free_tlist(origline);
3591 return DIRECTIVE_FOUND;
3592 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003593
H. Peter Anvine2c80182005-01-15 22:15:51 +00003594 if (tokval.t_type)
H. Peter Anvin130736c2016-02-17 20:27:41 -08003595 nasm_error(ERR_WARNING|ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +00003596 "trailing garbage after expression ignored");
H. Peter Anvin734b1882002-04-30 21:01:08 +00003597
H. Peter Anvine2c80182005-01-15 22:15:51 +00003598 if (!is_simple(evalresult)) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08003599 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00003600 "non-constant value given to `%%%sassign'",
3601 (i == PP_IASSIGN ? "i" : ""));
3602 free_tlist(origline);
3603 return DIRECTIVE_FOUND;
3604 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003605
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003606 macro_start = nasm_malloc(sizeof(*macro_start));
3607 macro_start->next = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003608 make_tok_num(macro_start, reloc_value(evalresult));
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003609 macro_start->a.mac = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003610
H. Peter Anvine2c80182005-01-15 22:15:51 +00003611 /*
3612 * We now have a macro name, an implicit parameter count of
3613 * zero, and a numeric token to use as an expansion. Create
3614 * and store an SMacro.
3615 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003616 define_smacro(ctx, mname, casesense, 0, macro_start);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003617 free_tlist(origline);
3618 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003619
H. Peter Anvine2c80182005-01-15 22:15:51 +00003620 case PP_LINE:
3621 /*
3622 * Syntax is `%line nnn[+mmm] [filename]'
3623 */
3624 tline = tline->next;
3625 skip_white_(tline);
3626 if (!tok_type_(tline, TOK_NUMBER)) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08003627 nasm_error(ERR_NONFATAL, "`%%line' expects line number");
H. Peter Anvine2c80182005-01-15 22:15:51 +00003628 free_tlist(origline);
3629 return DIRECTIVE_FOUND;
3630 }
H. Peter Anvin70055962007-10-11 00:05:31 -07003631 k = readnum(tline->text, &err);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003632 m = 1;
3633 tline = tline->next;
3634 if (tok_is_(tline, "+")) {
3635 tline = tline->next;
3636 if (!tok_type_(tline, TOK_NUMBER)) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08003637 nasm_error(ERR_NONFATAL, "`%%line' expects line increment");
H. Peter Anvine2c80182005-01-15 22:15:51 +00003638 free_tlist(origline);
3639 return DIRECTIVE_FOUND;
3640 }
H. Peter Anvin70055962007-10-11 00:05:31 -07003641 m = readnum(tline->text, &err);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003642 tline = tline->next;
3643 }
3644 skip_white_(tline);
3645 src_set_linnum(k);
3646 istk->lineinc = m;
3647 if (tline) {
H. Peter Anvin274cda82016-05-10 02:56:29 -07003648 char *fname = detoken(tline, false);
3649 src_set_fname(fname);
3650 nasm_free(fname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003651 }
3652 free_tlist(origline);
3653 return DIRECTIVE_FOUND;
Keith Kaniosb307a4f2010-11-06 17:41:51 -05003654
H. Peter Anvine2c80182005-01-15 22:15:51 +00003655 default:
H. Peter Anvin130736c2016-02-17 20:27:41 -08003656 nasm_error(ERR_FATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00003657 "preprocessor directive `%s' not yet implemented",
H. Peter Anvin4169a472007-09-12 01:29:43 +00003658 pp_directives[i]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003659 return DIRECTIVE_FOUND;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003660 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003661}
3662
3663/*
H. Peter Anvin76690a12002-04-30 20:52:49 +00003664 * Ensure that a macro parameter contains a condition code and
3665 * nothing else. Return the condition code index if so, or -1
3666 * otherwise.
3667 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003668static int find_cc(Token * t)
H. Peter Anvineba20a72002-04-30 20:53:55 +00003669{
H. Peter Anvin76690a12002-04-30 20:52:49 +00003670 Token *tt;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003671
H. Peter Anvin25a99342007-09-22 17:45:45 -07003672 if (!t)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003673 return -1; /* Probably a %+ without a space */
H. Peter Anvin25a99342007-09-22 17:45:45 -07003674
H. Peter Anvineba20a72002-04-30 20:53:55 +00003675 skip_white_(t);
Cyrill Gorcunov7524cfd2017-10-22 19:01:16 +03003676 if (!t)
3677 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003678 if (t->type != TOK_ID)
H. Peter Anvine2c80182005-01-15 22:15:51 +00003679 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003680 tt = t->next;
H. Peter Anvineba20a72002-04-30 20:53:55 +00003681 skip_white_(tt);
H. Peter Anvin76690a12002-04-30 20:52:49 +00003682 if (tt && (tt->type != TOK_OTHER || strcmp(tt->text, ",")))
H. Peter Anvine2c80182005-01-15 22:15:51 +00003683 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003684
Cyrill Gorcunov19456392012-05-02 00:18:56 +04003685 return bsii(t->text, (const char **)conditions, ARRAY_SIZE(conditions));
H. Peter Anvin76690a12002-04-30 20:52:49 +00003686}
3687
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003688/*
3689 * This routines walks over tokens strem and hadnles tokens
3690 * pasting, if @handle_explicit passed then explicit pasting
3691 * term is handled, otherwise -- implicit pastings only.
3692 */
Cyrill Gorcunov575d4282010-10-06 00:25:55 +04003693static bool paste_tokens(Token **head, const struct tokseq_match *m,
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003694 size_t mnum, bool handle_explicit)
H. Peter Anvind784a082009-04-20 14:01:18 -07003695{
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003696 Token *tok, *next, **prev_next, **prev_nonspace;
3697 bool pasted = false;
3698 char *buf, *p;
3699 size_t len, i;
H. Peter Anvind784a082009-04-20 14:01:18 -07003700
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003701 /*
3702 * The last token before pasting. We need it
3703 * to be able to connect new handled tokens.
3704 * In other words if there were a tokens stream
3705 *
3706 * A -> B -> C -> D
3707 *
3708 * and we've joined tokens B and C, the resulting
3709 * stream should be
3710 *
3711 * A -> BC -> D
3712 */
3713 tok = *head;
3714 prev_next = NULL;
3715
3716 if (!tok_type_(tok, TOK_WHITESPACE) && !tok_type_(tok, TOK_PASTE))
3717 prev_nonspace = head;
3718 else
3719 prev_nonspace = NULL;
3720
3721 while (tok && (next = tok->next)) {
3722
3723 switch (tok->type) {
H. Peter Anvind784a082009-04-20 14:01:18 -07003724 case TOK_WHITESPACE:
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003725 /* Zap redundant whitespaces */
3726 while (tok_type_(next, TOK_WHITESPACE))
3727 next = delete_Token(next);
3728 tok->next = next;
H. Peter Anvind784a082009-04-20 14:01:18 -07003729 break;
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003730
3731 case TOK_PASTE:
3732 /* Explicit pasting */
3733 if (!handle_explicit)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003734 break;
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003735 next = delete_Token(tok);
3736
3737 while (tok_type_(next, TOK_WHITESPACE))
3738 next = delete_Token(next);
3739
3740 if (!pasted)
3741 pasted = true;
3742
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003743 /* Left pasting token is start of line */
3744 if (!prev_nonspace)
H. Peter Anvin130736c2016-02-17 20:27:41 -08003745 nasm_error(ERR_FATAL, "No lvalue found on pasting");
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003746
Cyrill Gorcunov8b5c9fb2013-02-04 01:24:54 +04003747 /*
3748 * No ending token, this might happen in two
3749 * cases
3750 *
3751 * 1) There indeed no right token at all
3752 * 2) There is a bare "%define ID" statement,
3753 * and @ID does expand to whitespace.
3754 *
3755 * So technically we need to do a grammar analysis
3756 * in another stage of parsing, but for now lets don't
3757 * change the behaviour people used to. Simply allow
3758 * whitespace after paste token.
3759 */
3760 if (!next) {
3761 /*
3762 * Zap ending space tokens and that's all.
3763 */
3764 tok = (*prev_nonspace)->next;
3765 while (tok_type_(tok, TOK_WHITESPACE))
3766 tok = delete_Token(tok);
3767 tok = *prev_nonspace;
3768 tok->next = NULL;
3769 break;
3770 }
3771
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003772 tok = *prev_nonspace;
3773 while (tok_type_(tok, TOK_WHITESPACE))
3774 tok = delete_Token(tok);
3775 len = strlen(tok->text);
3776 len += strlen(next->text);
3777
3778 p = buf = nasm_malloc(len + 1);
3779 strcpy(p, tok->text);
3780 p = strchr(p, '\0');
3781 strcpy(p, next->text);
3782
3783 delete_Token(tok);
3784
3785 tok = tokenize(buf);
3786 nasm_free(buf);
3787
3788 *prev_nonspace = tok;
3789 while (tok && tok->next)
3790 tok = tok->next;
3791
3792 tok->next = delete_Token(next);
3793
3794 /* Restart from pasted tokens head */
3795 tok = *prev_nonspace;
3796 break;
3797
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003798 default:
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003799 /* implicit pasting */
Cyrill Gorcunov575d4282010-10-06 00:25:55 +04003800 for (i = 0; i < mnum; i++) {
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003801 if (!(PP_CONCAT_MATCH(tok, m[i].mask_head)))
3802 continue;
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +04003803
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003804 len = 0;
3805 while (next && PP_CONCAT_MATCH(next, m[i].mask_tail)) {
3806 len += strlen(next->text);
3807 next = next->next;
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +04003808 }
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003809
Cyrill Gorcunov6f8109e2017-10-22 21:26:36 +03003810 /* No match or no text to process */
3811 if (tok == next || len == 0)
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003812 break;
3813
3814 len += strlen(tok->text);
3815 p = buf = nasm_malloc(len + 1);
3816
Adam Majer1a069432017-07-25 11:12:35 +02003817 strcpy(p, tok->text);
3818 p = strchr(p, '\0');
3819 tok = delete_Token(tok);
3820
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003821 while (tok != next) {
Adam Majer1a069432017-07-25 11:12:35 +02003822 if (PP_CONCAT_MATCH(tok, m[i].mask_tail)) {
3823 strcpy(p, tok->text);
3824 p = strchr(p, '\0');
3825 }
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003826 tok = delete_Token(tok);
3827 }
3828
3829 tok = tokenize(buf);
3830 nasm_free(buf);
3831
3832 if (prev_next)
3833 *prev_next = tok;
3834 else
3835 *head = tok;
3836
3837 /*
3838 * Connect pasted into original stream,
3839 * ie A -> new-tokens -> B
3840 */
3841 while (tok && tok->next)
3842 tok = tok->next;
3843 tok->next = next;
3844
3845 if (!pasted)
3846 pasted = true;
3847
3848 /* Restart from pasted tokens head */
3849 tok = prev_next ? *prev_next : *head;
Cyrill Gorcunov575d4282010-10-06 00:25:55 +04003850 }
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003851
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003852 break;
H. Peter Anvind784a082009-04-20 14:01:18 -07003853 }
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003854
3855 prev_next = &tok->next;
3856
3857 if (tok->next &&
3858 !tok_type_(tok->next, TOK_WHITESPACE) &&
3859 !tok_type_(tok->next, TOK_PASTE))
3860 prev_nonspace = prev_next;
3861
3862 tok = tok->next;
H. Peter Anvind784a082009-04-20 14:01:18 -07003863 }
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003864
3865 return pasted;
H. Peter Anvind784a082009-04-20 14:01:18 -07003866}
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003867
3868/*
3869 * expands to a list of tokens from %{x:y}
3870 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003871static Token *expand_mmac_params_range(MMacro *mac, Token *tline, Token ***last)
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003872{
3873 Token *t = tline, **tt, *tm, *head;
3874 char *pos;
3875 int fst, lst, j, i;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003876
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003877 pos = strchr(tline->text, ':');
3878 nasm_assert(pos);
3879
3880 lst = atoi(pos + 1);
3881 fst = atoi(tline->text + 1);
3882
3883 /*
3884 * only macros params are accounted so
3885 * if someone passes %0 -- we reject such
3886 * value(s)
3887 */
3888 if (lst == 0 || fst == 0)
3889 goto err;
3890
3891 /* the values should be sane */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003892 if ((fst > (int)mac->nparam || fst < (-(int)mac->nparam)) ||
3893 (lst > (int)mac->nparam || lst < (-(int)mac->nparam)))
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003894 goto err;
3895
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003896 fst = fst < 0 ? fst + (int)mac->nparam + 1: fst;
3897 lst = lst < 0 ? lst + (int)mac->nparam + 1: lst;
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003898
3899 /* counted from zero */
3900 fst--, lst--;
3901
3902 /*
Cyrill Gorcunove75331c2013-11-09 12:02:15 +04003903 * It will be at least one token. Note we
3904 * need to scan params until separator, otherwise
3905 * only first token will be passed.
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003906 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003907 tm = mac->params[(fst + mac->rotate) % mac->nparam];
Cyrill Gorcunov67f2ca22018-10-13 19:41:01 +03003908 if (!tm)
3909 goto err;
Cyrill Gorcunove75331c2013-11-09 12:02:15 +04003910 head = new_Token(NULL, tm->type, tm->text, 0);
3911 tt = &head->next, tm = tm->next;
3912 while (tok_isnt_(tm, ",")) {
3913 t = new_Token(NULL, tm->type, tm->text, 0);
3914 *tt = t, tt = &t->next, tm = tm->next;
3915 }
3916
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003917 if (fst < lst) {
3918 for (i = fst + 1; i <= lst; i++) {
3919 t = new_Token(NULL, TOK_OTHER, ",", 0);
3920 *tt = t, tt = &t->next;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003921 j = (i + mac->rotate) % mac->nparam;
3922 tm = mac->params[j];
Cyrill Gorcunove75331c2013-11-09 12:02:15 +04003923 while (tok_isnt_(tm, ",")) {
3924 t = new_Token(NULL, tm->type, tm->text, 0);
3925 *tt = t, tt = &t->next, tm = tm->next;
3926 }
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003927 }
3928 } else {
3929 for (i = fst - 1; i >= lst; i--) {
3930 t = new_Token(NULL, TOK_OTHER, ",", 0);
3931 *tt = t, tt = &t->next;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003932 j = (i + mac->rotate) % mac->nparam;
3933 tm = mac->params[j];
Cyrill Gorcunove75331c2013-11-09 12:02:15 +04003934 while (tok_isnt_(tm, ",")) {
3935 t = new_Token(NULL, tm->type, tm->text, 0);
3936 *tt = t, tt = &t->next, tm = tm->next;
3937 }
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003938 }
3939 }
3940
3941 *last = tt;
3942 return head;
3943
3944err:
H. Peter Anvin130736c2016-02-17 20:27:41 -08003945 nasm_error(ERR_NONFATAL, "`%%{%s}': macro parameters out of range",
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003946 &tline->text[1]);
3947 return tline;
3948}
3949
H. Peter Anvin76690a12002-04-30 20:52:49 +00003950/*
3951 * Expand MMacro-local things: parameter references (%0, %n, %+n,
H. Peter Anvin67c63722008-10-26 23:49:00 -07003952 * %-n) and MMacro-local identifiers (%%foo) as well as
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003953 * macro indirection (%[...]) and range (%{..:..}).
H. Peter Anvin76690a12002-04-30 20:52:49 +00003954 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003955static Token *expand_mmac_params(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00003956{
H. Peter Anvin734b1882002-04-30 21:01:08 +00003957 Token *t, *tt, **tail, *thead;
H. Peter Anvin6125b622009-04-08 14:02:25 -07003958 bool changed = false;
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003959 char *pos;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003960
3961 tail = &thead;
3962 thead = NULL;
3963
H. Peter Anvine2c80182005-01-15 22:15:51 +00003964 while (tline) {
Cyrill Gorcunov661f7232018-10-28 20:39:34 +03003965 if (tline->type == TOK_PREPROC_ID && tline->text && tline->text[0] &&
Cyrill Gorcunovca611192010-06-04 09:22:12 +04003966 (((tline->text[1] == '+' || tline->text[1] == '-') && tline->text[2]) ||
3967 (tline->text[1] >= '0' && tline->text[1] <= '9') ||
3968 tline->text[1] == '%')) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00003969 char *text = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003970 int type = 0, cc; /* type = 0 to placate optimisers */
Keith Kaniosa6dfa782007-04-13 16:47:53 +00003971 char tmpbuf[30];
H. Peter Anvin25a99342007-09-22 17:45:45 -07003972 unsigned int n;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003973 int i;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003974 MMacro *mac;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003975
H. Peter Anvine2c80182005-01-15 22:15:51 +00003976 t = tline;
3977 tline = tline->next;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003978
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003979 mac = istk->mstk;
3980 while (mac && !mac->name) /* avoid mistaking %reps for macros */
3981 mac = mac->next_active;
3982 if (!mac) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08003983 nasm_error(ERR_NONFATAL, "`%s': not in a macro call", t->text);
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003984 } else {
3985 pos = strchr(t->text, ':');
3986 if (!pos) {
3987 switch (t->text[1]) {
3988 /*
3989 * We have to make a substitution of one of the
3990 * forms %1, %-1, %+1, %%foo, %0.
3991 */
3992 case '0':
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003993 type = TOK_NUMBER;
3994 snprintf(tmpbuf, sizeof(tmpbuf), "%d", mac->nparam);
3995 text = nasm_strdup(tmpbuf);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003996 break;
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003997 case '%':
H. Peter Anvine2c80182005-01-15 22:15:51 +00003998 type = TOK_ID;
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003999 snprintf(tmpbuf, sizeof(tmpbuf), "..@%"PRIu64".",
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004000 mac->unique);
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004001 text = nasm_strcat(tmpbuf, t->text + 2);
4002 break;
4003 case '-':
4004 n = atoi(t->text + 2) - 1;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004005 if (n >= mac->nparam)
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004006 tt = NULL;
4007 else {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004008 if (mac->nparam > 1)
4009 n = (n + mac->rotate) % mac->nparam;
4010 tt = mac->params[n];
H. Peter Anvine2c80182005-01-15 22:15:51 +00004011 }
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004012 cc = find_cc(tt);
4013 if (cc == -1) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08004014 nasm_error(ERR_NONFATAL,
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004015 "macro parameter %d is not a condition code",
4016 n + 1);
4017 text = NULL;
4018 } else {
4019 type = TOK_ID;
4020 if (inverse_ccs[cc] == -1) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08004021 nasm_error(ERR_NONFATAL,
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004022 "condition code `%s' is not invertible",
4023 conditions[cc]);
4024 text = NULL;
4025 } else
4026 text = nasm_strdup(conditions[inverse_ccs[cc]]);
4027 }
4028 break;
4029 case '+':
4030 n = atoi(t->text + 2) - 1;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004031 if (n >= mac->nparam)
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004032 tt = NULL;
4033 else {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004034 if (mac->nparam > 1)
4035 n = (n + mac->rotate) % mac->nparam;
4036 tt = mac->params[n];
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004037 }
4038 cc = find_cc(tt);
4039 if (cc == -1) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08004040 nasm_error(ERR_NONFATAL,
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004041 "macro parameter %d is not a condition code",
4042 n + 1);
4043 text = NULL;
4044 } else {
4045 type = TOK_ID;
4046 text = nasm_strdup(conditions[cc]);
4047 }
4048 break;
4049 default:
4050 n = atoi(t->text + 1) - 1;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004051 if (n >= mac->nparam)
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004052 tt = NULL;
4053 else {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004054 if (mac->nparam > 1)
4055 n = (n + mac->rotate) % mac->nparam;
4056 tt = mac->params[n];
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004057 }
4058 if (tt) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004059 for (i = 0; i < mac->paramlen[n]; i++) {
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004060 *tail = new_Token(NULL, tt->type, tt->text, 0);
4061 tail = &(*tail)->next;
4062 tt = tt->next;
4063 }
4064 }
4065 text = NULL; /* we've done it here */
4066 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004067 }
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004068 } else {
4069 /*
4070 * seems we have a parameters range here
4071 */
4072 Token *head, **last;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004073 head = expand_mmac_params_range(mac, t, &last);
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004074 if (head != t) {
4075 *tail = head;
4076 *last = tline;
4077 tline = head;
4078 text = NULL;
4079 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00004080 }
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004081 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00004082 if (!text) {
4083 delete_Token(t);
4084 } else {
4085 *tail = t;
4086 tail = &t->next;
4087 t->type = type;
4088 nasm_free(t->text);
4089 t->text = text;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004090 t->a.mac = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004091 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004092 changed = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004093 continue;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004094 } else if (tline->type == TOK_INDIRECT) {
4095 t = tline;
4096 tline = tline->next;
4097 tt = tokenize(t->text);
4098 tt = expand_mmac_params(tt);
4099 tt = expand_smacro(tt);
4100 *tail = tt;
4101 while (tt) {
4102 tt->a.mac = NULL; /* Necessary? */
4103 tail = &tt->next;
4104 tt = tt->next;
4105 }
4106 delete_Token(t);
4107 changed = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004108 } else {
4109 t = *tail = tline;
4110 tline = tline->next;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004111 t->a.mac = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004112 tail = &t->next;
4113 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00004114 }
H. Peter Anvineba20a72002-04-30 20:53:55 +00004115 *tail = NULL;
H. Peter Anvin67c63722008-10-26 23:49:00 -07004116
Cyrill Gorcunovc6a742c2011-06-27 01:23:09 +04004117 if (changed) {
4118 const struct tokseq_match t[] = {
4119 {
4120 PP_CONCAT_MASK(TOK_ID) |
4121 PP_CONCAT_MASK(TOK_FLOAT), /* head */
4122 PP_CONCAT_MASK(TOK_ID) |
4123 PP_CONCAT_MASK(TOK_NUMBER) |
4124 PP_CONCAT_MASK(TOK_FLOAT) |
4125 PP_CONCAT_MASK(TOK_OTHER) /* tail */
4126 },
4127 {
4128 PP_CONCAT_MASK(TOK_NUMBER), /* head */
4129 PP_CONCAT_MASK(TOK_NUMBER) /* tail */
4130 }
4131 };
4132 paste_tokens(&thead, t, ARRAY_SIZE(t), false);
4133 }
H. Peter Anvin6125b622009-04-08 14:02:25 -07004134
H. Peter Anvin76690a12002-04-30 20:52:49 +00004135 return thead;
4136}
4137
4138/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004139 * Expand all single-line macro calls made in the given line.
4140 * Return the expanded version of the line. The original is deemed
4141 * to be destroyed in the process. (In reality we'll just move
4142 * Tokens from input to output a lot of the time, rather than
4143 * actually bothering to destroy and replicate.)
4144 */
H. Peter Anvincb1cf592007-11-19 12:26:50 -08004145
H. Peter Anvine2c80182005-01-15 22:15:51 +00004146static Token *expand_smacro(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004147{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004148 Token *t, *tt, *mstart, **tail, *thead;
H. Peter Anvineba20a72002-04-30 20:53:55 +00004149 SMacro *head = NULL, *m;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004150 Token **params;
4151 int *paramsize;
H. Peter Anvin25a99342007-09-22 17:45:45 -07004152 unsigned int nparam, sparam;
H. Peter Anvind784a082009-04-20 14:01:18 -07004153 int brackets;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004154 Token *org_tline = tline;
4155 Context *ctx;
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08004156 const char *mname;
H. Peter Anvina3d96d02018-06-15 17:51:39 -07004157 int64_t deadman = nasm_limit[LIMIT_MACROS];
H. Peter Anvin8287daf2009-07-07 16:00:58 -07004158 bool expanded;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004159
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004160 /*
4161 * Trick: we should avoid changing the start token pointer since it can
4162 * be contained in "next" field of other token. Because of this
4163 * we allocate a copy of first token and work with it; at the end of
4164 * routine we copy it back
4165 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004166 if (org_tline) {
Cyrill Gorcunoved4a8052010-04-09 15:40:35 +04004167 tline = new_Token(org_tline->next, org_tline->type,
4168 org_tline->text, 0);
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004169 tline->a.mac = org_tline->a.mac;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004170 nasm_free(org_tline->text);
4171 org_tline->text = NULL;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004172 }
4173
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004174 expanded = true; /* Always expand %+ at least once */
H. Peter Anvin8287daf2009-07-07 16:00:58 -07004175
H. Peter Anvincb1cf592007-11-19 12:26:50 -08004176again:
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004177 thead = NULL;
Cyrill Gorcunoved4a8052010-04-09 15:40:35 +04004178 tail = &thead;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004179
H. Peter Anvine2c80182005-01-15 22:15:51 +00004180 while (tline) { /* main token loop */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004181 if (!--deadman) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08004182 nasm_error(ERR_NONFATAL, "interminable macro recursion");
Cyrill Gorcunovbd38c8f2009-11-21 11:11:23 +03004183 goto err;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004184 }
H. Peter Anvincb1cf592007-11-19 12:26:50 -08004185
H. Peter Anvine2c80182005-01-15 22:15:51 +00004186 if ((mname = tline->text)) {
4187 /* if this token is a local macro, look in local context */
Cyrill Gorcunovc56d9ad2010-02-11 15:12:19 +03004188 if (tline->type == TOK_ID) {
4189 head = (SMacro *)hash_findix(&smacros, mname);
4190 } else if (tline->type == TOK_PREPROC_ID) {
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04004191 ctx = get_ctx(mname, &mname);
Cyrill Gorcunovc56d9ad2010-02-11 15:12:19 +03004192 head = ctx ? (SMacro *)hash_findix(&ctx->localmac, mname) : NULL;
4193 } else
4194 head = NULL;
H. Peter Anvin072771e2008-05-22 13:17:51 -07004195
H. Peter Anvine2c80182005-01-15 22:15:51 +00004196 /*
4197 * We've hit an identifier. As in is_mmacro below, we first
4198 * check whether the identifier is a single-line macro at
4199 * all, then think about checking for parameters if
4200 * necessary.
4201 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004202 list_for_each(m, head)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004203 if (!mstrcmp(m->name, mname, m->casesense))
4204 break;
4205 if (m) {
4206 mstart = tline;
4207 params = NULL;
4208 paramsize = NULL;
4209 if (m->nparam == 0) {
4210 /*
4211 * Simple case: the macro is parameterless. Discard the
4212 * one token that the macro call took, and push the
4213 * expansion back on the to-do stack.
4214 */
4215 if (!m->expansion) {
4216 if (!strcmp("__FILE__", m->name)) {
H. Peter Anvin274cda82016-05-10 02:56:29 -07004217 const char *file = src_get_fname();
4218 /* nasm_free(tline->text); here? */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004219 tline->text = nasm_quote(file, strlen(file));
4220 tline->type = TOK_STRING;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004221 continue;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004222 }
4223 if (!strcmp("__LINE__", m->name)) {
4224 nasm_free(tline->text);
4225 make_tok_num(tline, src_get_linnum());
4226 continue;
4227 }
4228 if (!strcmp("__BITS__", m->name)) {
4229 nasm_free(tline->text);
4230 make_tok_num(tline, globalbits);
4231 continue;
4232 }
4233 tline = delete_Token(tline);
4234 continue;
4235 }
4236 } else {
4237 /*
4238 * Complicated case: at least one macro with this name
H. Peter Anvine2c80182005-01-15 22:15:51 +00004239 * exists and takes parameters. We must find the
4240 * parameters in the call, count them, find the SMacro
4241 * that corresponds to that form of the macro call, and
4242 * substitute for the parameters when we expand. What a
4243 * pain.
4244 */
4245 /*tline = tline->next;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004246 skip_white_(tline); */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004247 do {
4248 t = tline->next;
4249 while (tok_type_(t, TOK_SMAC_END)) {
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004250 t->a.mac->in_progress = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004251 t->text = NULL;
4252 t = tline->next = delete_Token(t);
4253 }
4254 tline = t;
4255 } while (tok_type_(tline, TOK_WHITESPACE));
4256 if (!tok_is_(tline, "(")) {
4257 /*
4258 * This macro wasn't called with parameters: ignore
4259 * the call. (Behaviour borrowed from gnu cpp.)
4260 */
4261 tline = mstart;
4262 m = NULL;
4263 } else {
4264 int paren = 0;
4265 int white = 0;
4266 brackets = 0;
4267 nparam = 0;
4268 sparam = PARAM_DELTA;
4269 params = nasm_malloc(sparam * sizeof(Token *));
4270 params[0] = tline->next;
4271 paramsize = nasm_malloc(sparam * sizeof(int));
4272 paramsize[0] = 0;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07004273 while (true) { /* parameter loop */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004274 /*
4275 * For some unusual expansions
4276 * which concatenates function call
4277 */
4278 t = tline->next;
4279 while (tok_type_(t, TOK_SMAC_END)) {
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004280 t->a.mac->in_progress = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004281 t->text = NULL;
4282 t = tline->next = delete_Token(t);
4283 }
4284 tline = t;
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00004285
H. Peter Anvine2c80182005-01-15 22:15:51 +00004286 if (!tline) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08004287 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00004288 "macro call expects terminating `)'");
4289 break;
4290 }
4291 if (tline->type == TOK_WHITESPACE
4292 && brackets <= 0) {
4293 if (paramsize[nparam])
4294 white++;
4295 else
4296 params[nparam] = tline->next;
4297 continue; /* parameter loop */
4298 }
4299 if (tline->type == TOK_OTHER
4300 && tline->text[1] == 0) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004301 char ch = tline->text[0];
H. Peter Anvine2c80182005-01-15 22:15:51 +00004302 if (ch == ',' && !paren && brackets <= 0) {
4303 if (++nparam >= sparam) {
4304 sparam += PARAM_DELTA;
4305 params = nasm_realloc(params,
Cyrill Gorcunoved4a8052010-04-09 15:40:35 +04004306 sparam * sizeof(Token *));
4307 paramsize = nasm_realloc(paramsize,
4308 sparam * sizeof(int));
H. Peter Anvine2c80182005-01-15 22:15:51 +00004309 }
4310 params[nparam] = tline->next;
4311 paramsize[nparam] = 0;
4312 white = 0;
4313 continue; /* parameter loop */
4314 }
4315 if (ch == '{' &&
4316 (brackets > 0 || (brackets == 0 &&
4317 !paramsize[nparam])))
4318 {
4319 if (!(brackets++)) {
4320 params[nparam] = tline->next;
4321 continue; /* parameter loop */
4322 }
4323 }
4324 if (ch == '}' && brackets > 0)
4325 if (--brackets == 0) {
4326 brackets = -1;
4327 continue; /* parameter loop */
4328 }
4329 if (ch == '(' && !brackets)
4330 paren++;
4331 if (ch == ')' && brackets <= 0)
4332 if (--paren < 0)
4333 break;
4334 }
4335 if (brackets < 0) {
4336 brackets = 0;
H. Peter Anvin130736c2016-02-17 20:27:41 -08004337 nasm_error(ERR_NONFATAL, "braces do not "
H. Peter Anvine2c80182005-01-15 22:15:51 +00004338 "enclose all of macro parameter");
4339 }
4340 paramsize[nparam] += white + 1;
4341 white = 0;
4342 } /* parameter loop */
4343 nparam++;
4344 while (m && (m->nparam != nparam ||
4345 mstrcmp(m->name, mname,
4346 m->casesense)))
4347 m = m->next;
4348 if (!m)
H. Peter Anvin130736c2016-02-17 20:27:41 -08004349 nasm_error(ERR_WARNING|ERR_PASS1|ERR_WARN_MNP,
H. Peter Anvine2c80182005-01-15 22:15:51 +00004350 "macro `%s' exists, "
4351 "but not taking %d parameters",
4352 mstart->text, nparam);
4353 }
4354 }
4355 if (m && m->in_progress)
4356 m = NULL;
4357 if (!m) { /* in progess or didn't find '(' or wrong nparam */
H. Peter Anvin70653092007-10-19 14:42:29 -07004358 /*
H. Peter Anvine2c80182005-01-15 22:15:51 +00004359 * Design question: should we handle !tline, which
4360 * indicates missing ')' here, or expand those
4361 * macros anyway, which requires the (t) test a few
H. Peter Anvin70653092007-10-19 14:42:29 -07004362 * lines down?
H. Peter Anvine2c80182005-01-15 22:15:51 +00004363 */
4364 nasm_free(params);
4365 nasm_free(paramsize);
4366 tline = mstart;
4367 } else {
4368 /*
4369 * Expand the macro: we are placed on the last token of the
4370 * call, so that we can easily split the call from the
4371 * following tokens. We also start by pushing an SMAC_END
4372 * token for the cycle removal.
4373 */
4374 t = tline;
4375 if (t) {
4376 tline = t->next;
4377 t->next = NULL;
4378 }
4379 tt = new_Token(tline, TOK_SMAC_END, NULL, 0);
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004380 tt->a.mac = m;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07004381 m->in_progress = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004382 tline = tt;
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04004383 list_for_each(t, m->expansion) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004384 if (t->type >= TOK_SMAC_PARAM) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00004385 Token *pcopy = tline, **ptail = &pcopy;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004386 Token *ttt, *pt;
4387 int i;
H. Peter Anvin734b1882002-04-30 21:01:08 +00004388
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004389 ttt = params[t->type - TOK_SMAC_PARAM];
4390 i = paramsize[t->type - TOK_SMAC_PARAM];
4391 while (--i >= 0) {
4392 pt = *ptail = new_Token(tline, ttt->type,
4393 ttt->text, 0);
4394 ptail = &pt->next;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004395 ttt = ttt->next;
Cyrill Gorcunov59ce1c62017-10-22 18:42:07 +03004396 if (!ttt && i > 0) {
4397 /*
4398 * FIXME: Need to handle more gracefully,
4399 * exiting early on agruments analysis.
4400 */
4401 nasm_error(ERR_FATAL,
4402 "macro `%s' expects %d args",
4403 mstart->text,
4404 (int)paramsize[t->type - TOK_SMAC_PARAM]);
4405 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00004406 }
4407 tline = pcopy;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004408 } else if (t->type == TOK_PREPROC_Q) {
4409 tt = new_Token(tline, TOK_ID, mname, 0);
4410 tline = tt;
4411 } else if (t->type == TOK_PREPROC_QQ) {
4412 tt = new_Token(tline, TOK_ID, m->name, 0);
4413 tline = tt;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004414 } else {
4415 tt = new_Token(tline, t->type, t->text, 0);
4416 tline = tt;
4417 }
4418 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00004419
H. Peter Anvine2c80182005-01-15 22:15:51 +00004420 /*
4421 * Having done that, get rid of the macro call, and clean
4422 * up the parameters.
4423 */
4424 nasm_free(params);
4425 nasm_free(paramsize);
4426 free_tlist(mstart);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004427 expanded = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004428 continue; /* main token loop */
4429 }
4430 }
4431 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004432
H. Peter Anvine2c80182005-01-15 22:15:51 +00004433 if (tline->type == TOK_SMAC_END) {
Cyrill Gorcunov980dd652018-10-14 19:25:32 +03004434 /* On error path it might already be dropped */
4435 if (tline->a.mac)
4436 tline->a.mac->in_progress = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004437 tline = delete_Token(tline);
4438 } else {
4439 t = *tail = tline;
4440 tline = tline->next;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004441 t->a.mac = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004442 t->next = NULL;
4443 tail = &t->next;
4444 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004445 }
4446
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004447 /*
4448 * Now scan the entire line and look for successive TOK_IDs that resulted
Keith Kaniosb7a89542007-04-12 02:40:54 +00004449 * after expansion (they can't be produced by tokenize()). The successive
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004450 * TOK_IDs should be concatenated.
4451 * Also we look for %+ tokens and concatenate the tokens before and after
4452 * them (without white spaces in between).
4453 */
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +04004454 if (expanded) {
Cyrill Gorcunovc6a742c2011-06-27 01:23:09 +04004455 const struct tokseq_match t[] = {
4456 {
4457 PP_CONCAT_MASK(TOK_ID) |
4458 PP_CONCAT_MASK(TOK_PREPROC_ID), /* head */
4459 PP_CONCAT_MASK(TOK_ID) |
4460 PP_CONCAT_MASK(TOK_PREPROC_ID) |
4461 PP_CONCAT_MASK(TOK_NUMBER) /* tail */
4462 }
4463 };
4464 if (paste_tokens(&thead, t, ARRAY_SIZE(t), true)) {
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +04004465 /*
4466 * If we concatenated something, *and* we had previously expanded
4467 * an actual macro, scan the lines again for macros...
4468 */
4469 tline = thead;
4470 expanded = false;
4471 goto again;
4472 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00004473 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004474
Cyrill Gorcunovbd38c8f2009-11-21 11:11:23 +03004475err:
H. Peter Anvine2c80182005-01-15 22:15:51 +00004476 if (org_tline) {
4477 if (thead) {
4478 *org_tline = *thead;
4479 /* since we just gave text to org_line, don't free it */
4480 thead->text = NULL;
4481 delete_Token(thead);
4482 } else {
4483 /* the expression expanded to empty line;
4484 we can't return NULL for some reasons
4485 we just set the line to a single WHITESPACE token. */
4486 memset(org_tline, 0, sizeof(*org_tline));
4487 org_tline->text = NULL;
4488 org_tline->type = TOK_WHITESPACE;
4489 }
4490 thead = org_tline;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004491 }
4492
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004493 return thead;
4494}
4495
4496/*
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004497 * Similar to expand_smacro but used exclusively with macro identifiers
4498 * right before they are fetched in. The reason is that there can be
4499 * identifiers consisting of several subparts. We consider that if there
4500 * are more than one element forming the name, user wants a expansion,
4501 * otherwise it will be left as-is. Example:
4502 *
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004503 * %define %$abc cde
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004504 *
4505 * the identifier %$abc will be left as-is so that the handler for %define
4506 * will suck it and define the corresponding value. Other case:
4507 *
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004508 * %define _%$abc cde
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004509 *
4510 * In this case user wants name to be expanded *before* %define starts
4511 * working, so we'll expand %$abc into something (if it has a value;
4512 * otherwise it will be left as-is) then concatenate all successive
4513 * PP_IDs into one.
4514 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004515static Token *expand_id(Token * tline)
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004516{
4517 Token *cur, *oldnext = NULL;
4518
H. Peter Anvin734b1882002-04-30 21:01:08 +00004519 if (!tline || !tline->next)
H. Peter Anvine2c80182005-01-15 22:15:51 +00004520 return tline;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004521
4522 cur = tline;
4523 while (cur->next &&
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004524 (cur->next->type == TOK_ID ||
4525 cur->next->type == TOK_PREPROC_ID
4526 || cur->next->type == TOK_NUMBER))
H. Peter Anvine2c80182005-01-15 22:15:51 +00004527 cur = cur->next;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004528
4529 /* If identifier consists of just one token, don't expand */
4530 if (cur == tline)
H. Peter Anvine2c80182005-01-15 22:15:51 +00004531 return tline;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004532
H. Peter Anvine2c80182005-01-15 22:15:51 +00004533 if (cur) {
4534 oldnext = cur->next; /* Detach the tail past identifier */
4535 cur->next = NULL; /* so that expand_smacro stops here */
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004536 }
4537
H. Peter Anvin734b1882002-04-30 21:01:08 +00004538 tline = expand_smacro(tline);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004539
H. Peter Anvine2c80182005-01-15 22:15:51 +00004540 if (cur) {
4541 /* expand_smacro possibly changhed tline; re-scan for EOL */
4542 cur = tline;
4543 while (cur && cur->next)
4544 cur = cur->next;
4545 if (cur)
4546 cur->next = oldnext;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004547 }
4548
4549 return tline;
4550}
4551
4552/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004553 * Determine whether the given line constitutes a multi-line macro
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004554 * call, and return the MMacro structure called if so. Doesn't have
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004555 * to check for an initial label - that's taken care of in
4556 * expand_mmacro - but must check numbers of parameters. Guaranteed
4557 * to be called with tline->type == TOK_ID, so the putative macro
4558 * name is easy to find.
4559 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004560static MMacro *is_mmacro(Token * tline, Token *** params_array)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004561{
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004562 MMacro *head, *m;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004563 Token **params;
4564 int nparam;
4565
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004566 head = (MMacro *) hash_findix(&mmacros, tline->text);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004567
4568 /*
4569 * Efficiency: first we see if any macro exists with the given
4570 * name. If not, we can return NULL immediately. _Then_ we
4571 * count the parameters, and then we look further along the
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004572 * list if necessary to find the proper MMacro.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004573 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004574 list_for_each(m, head)
4575 if (!mstrcmp(m->name, tline->text, m->casesense))
H. Peter Anvine2c80182005-01-15 22:15:51 +00004576 break;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004577 if (!m)
H. Peter Anvine2c80182005-01-15 22:15:51 +00004578 return NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004579
4580 /*
4581 * OK, we have a potential macro. Count and demarcate the
4582 * parameters.
4583 */
H. Peter Anvin734b1882002-04-30 21:01:08 +00004584 count_mmac_params(tline->next, &nparam, &params);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004585
4586 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004587 * So we know how many parameters we've got. Find the MMacro
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004588 * structure that handles this number.
4589 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004590 while (m) {
4591 if (m->nparam_min <= nparam
4592 && (m->plus || nparam <= m->nparam_max)) {
4593 /*
4594 * This one is right. Just check if cycle removal
4595 * prohibits us using it before we actually celebrate...
4596 */
4597 if (m->in_progress > m->max_depth) {
4598 if (m->max_depth > 0) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08004599 nasm_error(ERR_WARNING,
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004600 "reached maximum recursion depth of %i",
4601 m->max_depth);
4602 }
4603 nasm_free(params);
4604 return NULL;
4605 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00004606 /*
4607 * It's right, and we can use it. Add its default
4608 * parameters to the end of our list if necessary.
4609 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004610 if (m->defaults && nparam < m->nparam_min + m->ndefs) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00004611 params =
4612 nasm_realloc(params,
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004613 ((m->nparam_min + m->ndefs +
H. Peter Anvine2c80182005-01-15 22:15:51 +00004614 1) * sizeof(*params)));
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004615 while (nparam < m->nparam_min + m->ndefs) {
4616 params[nparam] = m->defaults[nparam - m->nparam_min];
H. Peter Anvine2c80182005-01-15 22:15:51 +00004617 nparam++;
4618 }
4619 }
4620 /*
4621 * If we've gone over the maximum parameter count (and
4622 * we're in Plus mode), ignore parameters beyond
4623 * nparam_max.
4624 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004625 if (m->plus && nparam > m->nparam_max)
4626 nparam = m->nparam_max;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004627 /*
4628 * Then terminate the parameter list, and leave.
4629 */
4630 if (!params) { /* need this special case */
4631 params = nasm_malloc(sizeof(*params));
4632 nparam = 0;
4633 }
4634 params[nparam] = NULL;
4635 *params_array = params;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004636 return m;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004637 }
4638 /*
4639 * This one wasn't right: look for the next one with the
4640 * same name.
4641 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004642 list_for_each(m, m->next)
4643 if (!mstrcmp(m->name, tline->text, m->casesense))
H. Peter Anvine2c80182005-01-15 22:15:51 +00004644 break;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004645 }
4646
4647 /*
4648 * After all that, we didn't find one with the right number of
4649 * parameters. Issue a warning, and fail to expand the macro.
4650 */
H. Peter Anvin130736c2016-02-17 20:27:41 -08004651 nasm_error(ERR_WARNING|ERR_PASS1|ERR_WARN_MNP,
H. Peter Anvine2c80182005-01-15 22:15:51 +00004652 "macro `%s' exists, but not taking %d parameters",
4653 tline->text, nparam);
H. Peter Anvin734b1882002-04-30 21:01:08 +00004654 nasm_free(params);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004655 return NULL;
4656}
4657
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004658
4659/*
4660 * Save MMacro invocation specific fields in
4661 * preparation for a recursive macro expansion
4662 */
4663static void push_mmacro(MMacro *m)
4664{
4665 MMacroInvocation *i;
4666
4667 i = nasm_malloc(sizeof(MMacroInvocation));
4668 i->prev = m->prev;
4669 i->params = m->params;
4670 i->iline = m->iline;
4671 i->nparam = m->nparam;
4672 i->rotate = m->rotate;
4673 i->paramlen = m->paramlen;
4674 i->unique = m->unique;
4675 i->condcnt = m->condcnt;
4676 m->prev = i;
4677}
4678
4679
4680/*
4681 * Restore MMacro invocation specific fields that were
4682 * saved during a previous recursive macro expansion
4683 */
4684static void pop_mmacro(MMacro *m)
4685{
4686 MMacroInvocation *i;
4687
4688 if (m->prev) {
4689 i = m->prev;
4690 m->prev = i->prev;
4691 m->params = i->params;
4692 m->iline = i->iline;
4693 m->nparam = i->nparam;
4694 m->rotate = i->rotate;
4695 m->paramlen = i->paramlen;
4696 m->unique = i->unique;
4697 m->condcnt = i->condcnt;
4698 nasm_free(i);
4699 }
4700}
4701
4702
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004703/*
4704 * Expand the multi-line macro call made by the given line, if
4705 * there is one to be expanded. If there is, push the expansion on
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004706 * istk->expansion and return 1. Otherwise return 0.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004707 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004708static int expand_mmacro(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004709{
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004710 Token *startline = tline;
H. Peter Anvineba20a72002-04-30 20:53:55 +00004711 Token *label = NULL;
4712 int dont_prepend = 0;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004713 Token **params, *t, *tt;
4714 MMacro *m;
4715 Line *l, *ll;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004716 int i, nparam, *paramlen;
H. Peter Anvinc751e862008-06-09 10:18:45 -07004717 const char *mname;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004718
4719 t = tline;
H. Peter Anvineba20a72002-04-30 20:53:55 +00004720 skip_white_(t);
H. Peter Anvince2233b2008-05-25 21:57:00 -07004721 /* if (!tok_type_(t, TOK_ID)) Lino 02/25/02 */
H. Peter Anvindce1e2f2002-04-30 21:06:37 +00004722 if (!tok_type_(t, TOK_ID) && !tok_type_(t, TOK_PREPROC_ID))
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004723 return 0;
4724 m = is_mmacro(t, &params);
4725 if (m) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004726 mname = t->text;
H. Peter Anvinc751e862008-06-09 10:18:45 -07004727 } else {
H. Peter Anvine2c80182005-01-15 22:15:51 +00004728 Token *last;
4729 /*
4730 * We have an id which isn't a macro call. We'll assume
4731 * it might be a label; we'll also check to see if a
4732 * colon follows it. Then, if there's another id after
4733 * that lot, we'll check it again for macro-hood.
4734 */
4735 label = last = t;
4736 t = t->next;
4737 if (tok_type_(t, TOK_WHITESPACE))
4738 last = t, t = t->next;
4739 if (tok_is_(t, ":")) {
4740 dont_prepend = 1;
4741 last = t, t = t->next;
4742 if (tok_type_(t, TOK_WHITESPACE))
4743 last = t, t = t->next;
4744 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004745 if (!tok_type_(t, TOK_ID) || !(m = is_mmacro(t, &params)))
4746 return 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004747 last->next = NULL;
Keith Kanios891775e2009-07-11 06:08:54 -05004748 mname = t->text;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004749 tline = t;
H. Peter Anvineba20a72002-04-30 20:53:55 +00004750 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004751
4752 /*
4753 * Fix up the parameters: this involves stripping leading and
4754 * trailing whitespace, then stripping braces if they are
4755 * present.
4756 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004757 for (nparam = 0; params[nparam]; nparam++) ;
H. Peter Anvin734b1882002-04-30 21:01:08 +00004758 paramlen = nparam ? nasm_malloc(nparam * sizeof(*paramlen)) : NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004759
H. Peter Anvine2c80182005-01-15 22:15:51 +00004760 for (i = 0; params[i]; i++) {
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08004761 int brace = 0;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004762 int comma = (!m->plus || i < nparam - 1);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004763
H. Peter Anvine2c80182005-01-15 22:15:51 +00004764 t = params[i];
4765 skip_white_(t);
4766 if (tok_is_(t, "{"))
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08004767 t = t->next, brace++, comma = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004768 params[i] = t;
4769 paramlen[i] = 0;
4770 while (t) {
4771 if (comma && t->type == TOK_OTHER && !strcmp(t->text, ","))
4772 break; /* ... because we have hit a comma */
4773 if (comma && t->type == TOK_WHITESPACE
4774 && tok_is_(t->next, ","))
4775 break; /* ... or a space then a comma */
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08004776 if (brace && t->type == TOK_OTHER) {
4777 if (t->text[0] == '{')
4778 brace++; /* ... or a nested opening brace */
4779 else if (t->text[0] == '}')
4780 if (!--brace)
4781 break; /* ... or a brace */
4782 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00004783 t = t->next;
4784 paramlen[i]++;
4785 }
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08004786 if (brace)
H. Peter Anvin130736c2016-02-17 20:27:41 -08004787 nasm_error(ERR_NONFATAL, "macro params should be enclosed in braces");
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004788 }
4789
4790 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004791 * OK, we have a MMacro structure together with a set of
4792 * parameters. We must now go through the expansion and push
4793 * copies of each Line on to istk->expansion. Substitution of
H. Peter Anvin76690a12002-04-30 20:52:49 +00004794 * parameter tokens and macro-local tokens doesn't get done
4795 * until the single-line macro substitution process; this is
4796 * because delaying them allows us to change the semantics
4797 * later through %rotate.
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004798 *
4799 * First, push an end marker on to istk->expansion, mark this
4800 * macro as in progress, and set up its invocation-specific
4801 * variables.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004802 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004803 ll = nasm_malloc(sizeof(Line));
4804 ll->next = istk->expansion;
4805 ll->finishes = m;
4806 ll->first = NULL;
4807 istk->expansion = ll;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004808
4809 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004810 * Save the previous MMacro expansion in the case of
4811 * macro recursion
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004812 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004813 if (m->max_depth && m->in_progress)
4814 push_mmacro(m);
4815
4816 m->in_progress ++;
4817 m->params = params;
4818 m->iline = tline;
4819 m->nparam = nparam;
4820 m->rotate = 0;
4821 m->paramlen = paramlen;
4822 m->unique = unique++;
4823 m->lineno = 0;
4824 m->condcnt = 0;
4825
4826 m->next_active = istk->mstk;
4827 istk->mstk = m;
4828
4829 list_for_each(l, m->expansion) {
4830 Token **tail;
4831
4832 ll = nasm_malloc(sizeof(Line));
4833 ll->finishes = NULL;
4834 ll->next = istk->expansion;
4835 istk->expansion = ll;
4836 tail = &ll->first;
4837
4838 list_for_each(t, l->first) {
4839 Token *x = t;
4840 switch (t->type) {
4841 case TOK_PREPROC_Q:
4842 tt = *tail = new_Token(NULL, TOK_ID, mname, 0);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004843 break;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004844 case TOK_PREPROC_QQ:
4845 tt = *tail = new_Token(NULL, TOK_ID, m->name, 0);
4846 break;
4847 case TOK_PREPROC_ID:
4848 if (t->text[1] == '0' && t->text[2] == '0') {
4849 dont_prepend = -1;
4850 x = label;
4851 if (!x)
4852 continue;
4853 }
4854 /* fall through */
4855 default:
4856 tt = *tail = new_Token(NULL, x->type, x->text, 0);
4857 break;
4858 }
4859 tail = &tt->next;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004860 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004861 *tail = NULL;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004862 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004863
4864 /*
H. Peter Anvineba20a72002-04-30 20:53:55 +00004865 * If we had a label, push it on as the first line of
4866 * the macro expansion.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004867 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004868 if (label) {
4869 if (dont_prepend < 0)
4870 free_tlist(startline);
4871 else {
4872 ll = nasm_malloc(sizeof(Line));
4873 ll->finishes = NULL;
4874 ll->next = istk->expansion;
4875 istk->expansion = ll;
4876 ll->first = startline;
4877 if (!dont_prepend) {
4878 while (label->next)
4879 label = label->next;
4880 label->next = tt = new_Token(NULL, TOK_OTHER, ":", 0);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004881 }
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004882 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004883 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004884
H. Peter Anvin8ac25aa2016-02-18 01:16:18 -08004885 lfmt->uplevel(m->nolist ? LIST_MACRO_NOLIST : LIST_MACRO);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004886
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004887 return 1;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004888}
4889
H. Peter Anvin130736c2016-02-17 20:27:41 -08004890/*
4891 * This function adds macro names to error messages, and suppresses
4892 * them if necessary.
4893 */
4894static void pp_verror(int severity, const char *fmt, va_list arg)
Victor van den Elzen3b404c02008-09-18 13:51:36 +02004895{
H. Peter Anvin130736c2016-02-17 20:27:41 -08004896 char buff[BUFSIZ];
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004897 MMacro *mmac = NULL;
4898 int delta = 0;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02004899
H. Peter Anvin130736c2016-02-17 20:27:41 -08004900 /*
4901 * If we're in a dead branch of IF or something like it, ignore the error.
4902 * However, because %else etc are evaluated in the state context
4903 * of the previous branch, errors might get lost:
4904 * %if 0 ... %else trailing garbage ... %endif
4905 * So %else etc should set the ERR_PP_PRECOND flag.
4906 */
4907 if ((severity & ERR_MASK) < ERR_FATAL &&
4908 istk && istk->conds &&
4909 ((severity & ERR_PP_PRECOND) ?
4910 istk->conds->state == COND_NEVER :
H. Peter Anvineb6653f2016-04-05 13:03:10 -07004911 !emitting(istk->conds->state)))
H. Peter Anvin130736c2016-02-17 20:27:41 -08004912 return;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02004913
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004914 /* get %macro name */
H. Peter Anvin130736c2016-02-17 20:27:41 -08004915 if (!(severity & ERR_NOFILE) && istk && istk->mstk) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004916 mmac = istk->mstk;
4917 /* but %rep blocks should be skipped */
4918 while (mmac && !mmac->name)
4919 mmac = mmac->next_active, delta++;
Cyrill Gorcunov9900c6b2011-10-09 18:58:46 +04004920 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004921
H. Peter Anvin130736c2016-02-17 20:27:41 -08004922 if (mmac) {
4923 vsnprintf(buff, sizeof(buff), fmt, arg);
Victor van den Elzen3b404c02008-09-18 13:51:36 +02004924
H. Peter Anvin130736c2016-02-17 20:27:41 -08004925 nasm_set_verror(real_verror);
4926 nasm_error(severity, "(%s:%d) %s",
4927 mmac->name, mmac->lineno - delta, buff);
4928 nasm_set_verror(pp_verror);
4929 } else {
4930 real_verror(severity, fmt, arg);
4931 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004932}
4933
H. Peter Anvin734b1882002-04-30 21:01:08 +00004934static void
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07004935pp_reset(const char *file, int apass, StrList *dep_list)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004936{
H. Peter Anvin7383b402008-09-24 10:20:40 -07004937 Token *t;
4938
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004939 cstk = NULL;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004940 istk = nasm_malloc(sizeof(Include));
4941 istk->next = NULL;
4942 istk->conds = NULL;
4943 istk->expansion = NULL;
4944 istk->mstk = NULL;
H. Peter Anvin3e83cec2016-05-25 04:28:46 -07004945 istk->fp = nasm_open_read(file, NF_TEXT);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004946 istk->fname = NULL;
H. Peter Anvin274cda82016-05-10 02:56:29 -07004947 src_set(0, file);
H. Peter Anvineba20a72002-04-30 20:53:55 +00004948 istk->lineinc = 1;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004949 if (!istk->fp)
H. Peter Anvinc5136902018-06-15 18:20:17 -07004950 nasm_fatal_fl(ERR_NOFILE, "unable to open input file `%s'", file);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004951 defining = NULL;
Charles Crayned4200be2008-07-12 16:42:33 -07004952 nested_mac_count = 0;
4953 nested_rep_count = 0;
H. Peter Anvin97a23472007-09-16 17:57:25 -07004954 init_macros();
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004955 unique = 0;
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07004956 deplist = dep_list;
H. Peter Anvinf7606612016-07-13 14:23:48 -07004957
4958 if (tasm_compatible_mode)
4959 pp_add_stdmac(nasm_stdmac_tasm);
4960
4961 pp_add_stdmac(nasm_stdmac_nasm);
4962 pp_add_stdmac(nasm_stdmac_version);
4963
Cyrill Gorcunov15ce78f2017-01-06 20:21:28 +03004964 if (extrastdmac)
4965 pp_add_stdmac(extrastdmac);
4966
H. Peter Anvinf7606612016-07-13 14:23:48 -07004967 stdmacpos = stdmacros[0];
4968 stdmacnext = &stdmacros[1];
4969
H. Peter Anvind2456592008-06-19 15:04:18 -07004970 do_predef = true;
H. Peter Anvin61f130f2008-09-25 15:45:06 -07004971
4972 /*
4973 * 0 for dependencies, 1 for preparatory passes, 2 for final pass.
4974 * The caller, however, will also pass in 3 for preprocess-only so
4975 * we can set __PASS__ accordingly.
4976 */
4977 pass = apass > 2 ? 2 : apass;
4978
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07004979 strlist_add_string(deplist, file);
4980
H. Peter Anvin61f130f2008-09-25 15:45:06 -07004981 /*
4982 * Define the __PASS__ macro. This is defined here unlike
4983 * all the other builtins, because it is special -- it varies between
4984 * passes.
4985 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004986 t = nasm_malloc(sizeof(*t));
4987 t->next = NULL;
H. Peter Anvin61f130f2008-09-25 15:45:06 -07004988 make_tok_num(t, apass);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004989 t->a.mac = NULL;
H. Peter Anvin7383b402008-09-24 10:20:40 -07004990 define_smacro(NULL, "__PASS__", true, 0, t);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004991}
4992
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07004993static void pp_init(void)
4994{
4995 hash_init(&FileHash, HASH_MEDIUM);
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07004996 ipath = strlist_allocate();
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07004997}
4998
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004999static char *pp_getline(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005000{
Keith Kaniosa6dfa782007-04-13 16:47:53 +00005001 char *line;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005002 Token *tline;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005003
H. Peter Anvin130736c2016-02-17 20:27:41 -08005004 real_verror = nasm_set_verror(pp_verror);
H. Peter Anvin215186f2016-02-17 20:27:41 -08005005
H. Peter Anvine2c80182005-01-15 22:15:51 +00005006 while (1) {
5007 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005008 * Fetch a tokenized line, either from the macro-expansion
H. Peter Anvine2c80182005-01-15 22:15:51 +00005009 * buffer or from the input file.
5010 */
5011 tline = NULL;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005012 while (istk->expansion && istk->expansion->finishes) {
5013 Line *l = istk->expansion;
5014 if (!l->finishes->name && l->finishes->in_progress > 1) {
5015 Line *ll;
H. Peter Anvineba20a72002-04-30 20:53:55 +00005016
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005017 /*
5018 * This is a macro-end marker for a macro with no
5019 * name, which means it's not really a macro at all
5020 * but a %rep block, and the `in_progress' field is
5021 * more than 1, meaning that we still need to
5022 * repeat. (1 means the natural last repetition; 0
5023 * means termination by %exitrep.) We have
5024 * therefore expanded up to the %endrep, and must
5025 * push the whole block on to the expansion buffer
5026 * again. We don't bother to remove the macro-end
5027 * marker: we'd only have to generate another one
5028 * if we did.
5029 */
5030 l->finishes->in_progress--;
5031 list_for_each(l, l->finishes->expansion) {
5032 Token *t, *tt, **tail;
5033
5034 ll = nasm_malloc(sizeof(Line));
5035 ll->next = istk->expansion;
5036 ll->finishes = NULL;
5037 ll->first = NULL;
5038 tail = &ll->first;
5039
5040 list_for_each(t, l->first) {
5041 if (t->text || t->type == TOK_WHITESPACE) {
5042 tt = *tail = new_Token(NULL, t->type, t->text, 0);
5043 tail = &tt->next;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005044 }
5045 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005046
5047 istk->expansion = ll;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005048 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005049 } else {
5050 /*
5051 * Check whether a `%rep' was started and not ended
5052 * within this macro expansion. This can happen and
5053 * should be detected. It's a fatal error because
5054 * I'm too confused to work out how to recover
5055 * sensibly from it.
5056 */
5057 if (defining) {
5058 if (defining->name)
H. Peter Anvinc5136902018-06-15 18:20:17 -07005059 nasm_panic("defining with name in expansion");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005060 else if (istk->mstk->name)
H. Peter Anvinc5136902018-06-15 18:20:17 -07005061 nasm_fatal("`%%rep' without `%%endrep' within"
H. Peter Anvin130736c2016-02-17 20:27:41 -08005062 " expansion of macro `%s'",
5063 istk->mstk->name);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005064 }
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005065
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005066 /*
5067 * FIXME: investigate the relationship at this point between
5068 * istk->mstk and l->finishes
5069 */
5070 {
5071 MMacro *m = istk->mstk;
5072 istk->mstk = m->next_active;
5073 if (m->name) {
5074 /*
5075 * This was a real macro call, not a %rep, and
5076 * therefore the parameter information needs to
5077 * be freed.
5078 */
5079 if (m->prev) {
5080 pop_mmacro(m);
5081 l->finishes->in_progress --;
5082 } else {
5083 nasm_free(m->params);
5084 free_tlist(m->iline);
5085 nasm_free(m->paramlen);
5086 l->finishes->in_progress = 0;
5087 }
Adam Majer91e72402017-07-25 10:42:01 +02005088 }
5089
5090 /*
5091 * FIXME It is incorrect to always free_mmacro here.
5092 * It leads to usage-after-free.
5093 *
5094 * https://bugzilla.nasm.us/show_bug.cgi?id=3392414
5095 */
5096#if 0
5097 else
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005098 free_mmacro(m);
Adam Majer91e72402017-07-25 10:42:01 +02005099#endif
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005100 }
5101 istk->expansion = l->next;
5102 nasm_free(l);
H. Peter Anvin8ac25aa2016-02-18 01:16:18 -08005103 lfmt->downlevel(LIST_MACRO);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005104 }
5105 }
5106 while (1) { /* until we get a line we can use */
5107
5108 if (istk->expansion) { /* from a macro expansion */
5109 char *p;
5110 Line *l = istk->expansion;
5111 if (istk->mstk)
5112 istk->mstk->lineno++;
5113 tline = l->first;
5114 istk->expansion = l->next;
5115 nasm_free(l);
5116 p = detoken(tline, false);
H. Peter Anvin8ac25aa2016-02-18 01:16:18 -08005117 lfmt->line(LIST_MACRO, p);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005118 nasm_free(p);
5119 break;
5120 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00005121 line = read_line();
5122 if (line) { /* from the current input file */
5123 line = prepreproc(line);
Keith Kaniosb7a89542007-04-12 02:40:54 +00005124 tline = tokenize(line);
H. Peter Anvine2c80182005-01-15 22:15:51 +00005125 nasm_free(line);
5126 break;
5127 }
5128 /*
5129 * The current file has ended; work down the istk
5130 */
5131 {
5132 Include *i = istk;
5133 fclose(i->fp);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005134 if (i->conds) {
5135 /* nasm_error can't be conditionally suppressed */
H. Peter Anvinc5136902018-06-15 18:20:17 -07005136 nasm_fatal("expected `%%endif' before end of file");
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005137 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00005138 /* only set line and file name if there's a next node */
H. Peter Anvin274cda82016-05-10 02:56:29 -07005139 if (i->next)
5140 src_set(i->lineno, i->fname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00005141 istk = i->next;
H. Peter Anvin8ac25aa2016-02-18 01:16:18 -08005142 lfmt->downlevel(LIST_INCLUDE);
H. Peter Anvine2c80182005-01-15 22:15:51 +00005143 nasm_free(i);
H. Peter Anvin130736c2016-02-17 20:27:41 -08005144 if (!istk) {
5145 line = NULL;
5146 goto done;
5147 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005148 if (istk->expansion && istk->expansion->finishes)
5149 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005150 }
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005151 }
5152
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005153 /*
5154 * We must expand MMacro parameters and MMacro-local labels
5155 * _before_ we plunge into directive processing, to cope
5156 * with things like `%define something %1' such as STRUC
5157 * uses. Unless we're _defining_ a MMacro, in which case
5158 * those tokens should be left alone to go into the
5159 * definition; and unless we're in a non-emitting
5160 * condition, in which case we don't want to meddle with
5161 * anything.
5162 */
5163 if (!defining && !(istk->conds && !emitting(istk->conds->state))
5164 && !(istk->mstk && !istk->mstk->in_progress)) {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005165 tline = expand_mmac_params(tline);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005166 }
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005167
H. Peter Anvine2c80182005-01-15 22:15:51 +00005168 /*
5169 * Check the line to see if it's a preprocessor directive.
5170 */
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07005171 if (do_directive(tline, &line) == DIRECTIVE_FOUND) {
5172 if (line)
5173 break; /* Directive generated output */
5174 else
5175 continue;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005176 } else if (defining) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00005177 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005178 * We're defining a multi-line macro. We emit nothing
5179 * at all, and just
5180 * shove the tokenized line on to the macro definition.
H. Peter Anvine2c80182005-01-15 22:15:51 +00005181 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005182 Line *l = nasm_malloc(sizeof(Line));
5183 l->next = defining->expansion;
5184 l->first = tline;
5185 l->finishes = NULL;
5186 defining->expansion = l;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005187 continue;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005188 } else if (istk->conds && !emitting(istk->conds->state)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00005189 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005190 * We're in a non-emitting branch of a condition block.
H. Peter Anvine2c80182005-01-15 22:15:51 +00005191 * Emit nothing at all, not even a blank line: when we
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005192 * emerge from the condition we'll give a line-number
H. Peter Anvine2c80182005-01-15 22:15:51 +00005193 * directive so we keep our place correctly.
5194 */
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005195 free_tlist(tline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00005196 continue;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005197 } else if (istk->mstk && !istk->mstk->in_progress) {
5198 /*
5199 * We're in a %rep block which has been terminated, so
5200 * we're walking through to the %endrep without
5201 * emitting anything. Emit nothing at all, not even a
5202 * blank line: when we emerge from the %rep block we'll
5203 * give a line-number directive so we keep our place
5204 * correctly.
5205 */
5206 free_tlist(tline);
5207 continue;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005208 } else {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005209 tline = expand_smacro(tline);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005210 if (!expand_mmacro(tline)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00005211 /*
Keith Kaniosb7a89542007-04-12 02:40:54 +00005212 * De-tokenize the line again, and emit it.
H. Peter Anvine2c80182005-01-15 22:15:51 +00005213 */
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005214 line = detoken(tline, true);
5215 free_tlist(tline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00005216 break;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005217 } else {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005218 continue; /* expand_mmacro calls free_tlist */
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005219 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00005220 }
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005221 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005222
H. Peter Anvin130736c2016-02-17 20:27:41 -08005223done:
5224 nasm_set_verror(real_verror);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005225 return line;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005226}
5227
H. Peter Anvine2c80182005-01-15 22:15:51 +00005228static void pp_cleanup(int pass)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005229{
H. Peter Anvin130736c2016-02-17 20:27:41 -08005230 real_verror = nasm_set_verror(pp_verror);
H. Peter Anvin215186f2016-02-17 20:27:41 -08005231
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005232 if (defining) {
5233 if (defining->name) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08005234 nasm_error(ERR_NONFATAL,
5235 "end of file while still defining macro `%s'",
5236 defining->name);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005237 } else {
H. Peter Anvin130736c2016-02-17 20:27:41 -08005238 nasm_error(ERR_NONFATAL, "end of file while still in %%rep");
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005239 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005240
5241 free_mmacro(defining);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005242 defining = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005243 }
H. Peter Anvin130736c2016-02-17 20:27:41 -08005244
5245 nasm_set_verror(real_verror);
H. Peter Anvin215186f2016-02-17 20:27:41 -08005246
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005247 while (cstk)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005248 ctx_pop();
H. Peter Anvin97a23472007-09-16 17:57:25 -07005249 free_macros();
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005250 while (istk) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00005251 Include *i = istk;
5252 istk = istk->next;
5253 fclose(i->fp);
Cyrill Gorcunov8dcfd882011-03-03 09:18:56 +03005254 nasm_free(i);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005255 }
5256 while (cstk)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005257 ctx_pop();
H. Peter Anvin274cda82016-05-10 02:56:29 -07005258 src_set_fname(NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00005259 if (pass == 0) {
5260 free_llist(predef);
Cyrill Gorcunovdae24d72014-06-28 10:17:39 +04005261 predef = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005262 delete_Blocks();
Cyrill Gorcunovdae24d72014-06-28 10:17:39 +04005263 freeTokens = NULL;
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07005264 strlist_free(ipath);
H. Peter Anvin11dfa1a2008-07-02 18:11:04 -07005265 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005266}
5267
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07005268static void pp_include_path(const char *path)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005269{
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07005270 if (!path)
5271 path = "";
H. Peter Anvin37a321f2007-09-24 13:41:58 -07005272
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07005273 strlist_add_string(ipath, path);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005274}
Frank Kotlerd0ed6fd2003-08-27 11:33:56 +00005275
Cyrill Gorcunov0b78bff2012-05-07 01:57:55 +04005276static void pp_pre_include(char *fname)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005277{
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005278 Token *inc, *space, *name;
5279 Line *l;
5280
H. Peter Anvin734b1882002-04-30 21:01:08 +00005281 name = new_Token(NULL, TOK_INTERNAL_STRING, fname, 0);
5282 space = new_Token(name, TOK_WHITESPACE, NULL, 0);
5283 inc = new_Token(space, TOK_PREPROC_ID, "%include", 0);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005284
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005285 l = nasm_malloc(sizeof(Line));
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005286 l->next = predef;
5287 l->first = inc;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005288 l->finishes = NULL;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005289 predef = l;
5290}
5291
Cyrill Gorcunov0b78bff2012-05-07 01:57:55 +04005292static void pp_pre_define(char *definition)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005293{
5294 Token *def, *space;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005295 Line *l;
Keith Kaniosa6dfa782007-04-13 16:47:53 +00005296 char *equals;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005297
H. Peter Anvin130736c2016-02-17 20:27:41 -08005298 real_verror = nasm_set_verror(pp_verror);
H. Peter Anvin215186f2016-02-17 20:27:41 -08005299
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005300 equals = strchr(definition, '=');
H. Peter Anvin734b1882002-04-30 21:01:08 +00005301 space = new_Token(NULL, TOK_WHITESPACE, NULL, 0);
5302 def = new_Token(space, TOK_PREPROC_ID, "%define", 0);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005303 if (equals)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005304 *equals = ' ';
Keith Kaniosb7a89542007-04-12 02:40:54 +00005305 space->next = tokenize(definition);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005306 if (equals)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005307 *equals = '=';
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005308
Cyrill Gorcunov6d42e9b2015-02-08 11:07:17 +03005309 if (space->next->type != TOK_PREPROC_ID &&
5310 space->next->type != TOK_ID)
H. Peter Anvin130736c2016-02-17 20:27:41 -08005311 nasm_error(ERR_WARNING, "pre-defining non ID `%s\'\n", definition);
Cyrill Gorcunov6d42e9b2015-02-08 11:07:17 +03005312
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005313 l = nasm_malloc(sizeof(Line));
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005314 l->next = predef;
5315 l->first = def;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005316 l->finishes = NULL;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005317 predef = l;
H. Peter Anvin130736c2016-02-17 20:27:41 -08005318
5319 nasm_set_verror(real_verror);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005320}
5321
Cyrill Gorcunov0b78bff2012-05-07 01:57:55 +04005322static void pp_pre_undefine(char *definition)
H. Peter Anvin620515a2002-04-30 20:57:38 +00005323{
5324 Token *def, *space;
5325 Line *l;
5326
H. Peter Anvin734b1882002-04-30 21:01:08 +00005327 space = new_Token(NULL, TOK_WHITESPACE, NULL, 0);
5328 def = new_Token(space, TOK_PREPROC_ID, "%undef", 0);
Keith Kaniosb7a89542007-04-12 02:40:54 +00005329 space->next = tokenize(definition);
H. Peter Anvin620515a2002-04-30 20:57:38 +00005330
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005331 l = nasm_malloc(sizeof(Line));
H. Peter Anvin620515a2002-04-30 20:57:38 +00005332 l->next = predef;
5333 l->first = def;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005334 l->finishes = NULL;
H. Peter Anvin620515a2002-04-30 20:57:38 +00005335 predef = l;
5336}
5337
H. Peter Anvin05990342018-06-11 13:32:42 -07005338/* Insert an early preprocessor command that doesn't need special handling */
5339static void pp_pre_command(const char *what, char *string)
5340{
5341 char *cmd;
5342 Token *def, *space;
5343 Line *l;
5344
5345 def = tokenize(string);
5346 if (what) {
5347 cmd = nasm_strcat(what[0] == '%' ? "" : "%", what);
5348 space = new_Token(def, TOK_WHITESPACE, NULL, 0);
5349 def = new_Token(space, TOK_PREPROC_ID, cmd, 0);
5350 }
5351
5352 l = nasm_malloc(sizeof(Line));
5353 l->next = predef;
5354 l->first = def;
5355 l->finishes = NULL;
5356 predef = l;
5357}
5358
H. Peter Anvinf7606612016-07-13 14:23:48 -07005359static void pp_add_stdmac(macros_t *macros)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005360{
H. Peter Anvinf7606612016-07-13 14:23:48 -07005361 macros_t **mp;
5362
5363 /* Find the end of the list and avoid duplicates */
5364 for (mp = stdmacros; *mp; mp++) {
5365 if (*mp == macros)
5366 return; /* Nothing to do */
5367 }
5368
5369 nasm_assert(mp < &stdmacros[ARRAY_SIZE(stdmacros)-1]);
5370
5371 *mp = macros;
H. Peter Anvin76690a12002-04-30 20:52:49 +00005372}
5373
Cyrill Gorcunov15ce78f2017-01-06 20:21:28 +03005374static void pp_extra_stdmac(macros_t *macros)
5375{
5376 extrastdmac = macros;
5377}
5378
Keith Kaniosa5fc6462007-10-13 07:09:22 -07005379static void make_tok_num(Token * tok, int64_t val)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005380{
Cyrill Gorcunovce652742013-05-06 23:43:43 +04005381 char numbuf[32];
Keith Kaniosa5fc6462007-10-13 07:09:22 -07005382 snprintf(numbuf, sizeof(numbuf), "%"PRId64"", val);
H. Peter Anvineba20a72002-04-30 20:53:55 +00005383 tok->text = nasm_strdup(numbuf);
5384 tok->type = TOK_NUMBER;
5385}
5386
H. Peter Anvin37368952016-05-09 14:10:32 -07005387static void pp_list_one_macro(MMacro *m, int severity)
5388{
5389 if (!m)
5390 return;
5391
5392 /* We need to print the next_active list in reverse order */
5393 pp_list_one_macro(m->next_active, severity);
5394
5395 if (m->name && !m->nolist) {
H. Peter Anvin274cda82016-05-10 02:56:29 -07005396 src_set(m->xline + m->lineno, m->fname);
H. Peter Anvin37368952016-05-09 14:10:32 -07005397 nasm_error(severity, "... from macro `%s' defined here", m->name);
5398 }
5399}
5400
H. Peter Anvin4def1a82016-05-09 13:59:44 -07005401static void pp_error_list_macros(int severity)
5402{
H. Peter Anvin4def1a82016-05-09 13:59:44 -07005403 int32_t saved_line;
5404 const char *saved_fname = NULL;
5405
5406 severity |= ERR_PP_LISTMACRO | ERR_NO_SEVERITY;
H. Peter Anvin274cda82016-05-10 02:56:29 -07005407 src_get(&saved_line, &saved_fname);
H. Peter Anvin4def1a82016-05-09 13:59:44 -07005408
Cyrill Gorcunov771d04e2016-05-10 23:27:03 +03005409 if (istk)
5410 pp_list_one_macro(istk->mstk, severity);
H. Peter Anvin4def1a82016-05-09 13:59:44 -07005411
H. Peter Anvin274cda82016-05-10 02:56:29 -07005412 src_set(saved_line, saved_fname);
H. Peter Anvin4def1a82016-05-09 13:59:44 -07005413}
5414
H. Peter Anvine7469712016-02-18 02:20:59 -08005415const struct preproc_ops nasmpp = {
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07005416 pp_init,
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005417 pp_reset,
5418 pp_getline,
Cyrill Gorcunov0b78bff2012-05-07 01:57:55 +04005419 pp_cleanup,
Cyrill Gorcunov15ce78f2017-01-06 20:21:28 +03005420 pp_extra_stdmac,
Cyrill Gorcunov0b78bff2012-05-07 01:57:55 +04005421 pp_pre_define,
5422 pp_pre_undefine,
5423 pp_pre_include,
H. Peter Anvin05990342018-06-11 13:32:42 -07005424 pp_pre_command,
H. Peter Anvin4def1a82016-05-09 13:59:44 -07005425 pp_include_path,
5426 pp_error_list_macros,
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005427};