blob: 808303ed96b8b1e87f903c21bffa03620dc3813f [file] [log] [blame]
H. Peter Anvin9e6747c2009-06-28 17:13:04 -07001/* ----------------------------------------------------------------------- *
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002 *
H. Peter Anvin05990342018-06-11 13:32:42 -07003 * Copyright 1996-2018 The NASM Authors - All Rights Reserved
H. Peter Anvin9e6747c2009-06-28 17:13:04 -07004 * See the file AUTHORS included with the NASM distribution for
5 * the specific copyright holders.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00006 *
H. Peter Anvin9e6747c2009-06-28 17:13:04 -07007 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following
9 * conditions are met:
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000010 *
H. Peter Anvin9e6747c2009-06-28 17:13:04 -070011 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above
14 * copyright notice, this list of conditions and the following
15 * disclaimer in the documentation and/or other materials provided
16 * with the distribution.
Cyrill Gorcunovaccda192010-02-16 10:27:56 +030017 *
H. Peter Anvin9e6747c2009-06-28 17:13:04 -070018 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
19 * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
20 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
21 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
23 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
26 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
29 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
30 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 *
32 * ----------------------------------------------------------------------- */
33
34/*
35 * preproc.c macro preprocessor for the Netwide Assembler
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000036 */
37
H. Peter Anvin4836e332002-04-30 20:56:43 +000038/* Typical flow of text through preproc
39 *
Keith Kaniosb7a89542007-04-12 02:40:54 +000040 * pp_getline gets tokenized lines, either
H. Peter Anvin4836e332002-04-30 20:56:43 +000041 *
42 * from a macro expansion
43 *
44 * or
45 * {
46 * read_line gets raw text from stdmacpos, or predef, or current input file
Keith Kaniosb7a89542007-04-12 02:40:54 +000047 * tokenize converts to tokens
H. Peter Anvin4836e332002-04-30 20:56:43 +000048 * }
49 *
50 * expand_mmac_params is used to expand %1 etc., unless a macro is being
51 * defined or a false conditional is being processed
52 * (%0, %1, %+1, %-1, %%foo
53 *
54 * do_directive checks for directives
55 *
56 * expand_smacro is used to expand single line macros
57 *
58 * expand_mmacro is used to expand multi-line macros
59 *
60 * detoken is used to convert the line back to text
61 */
H. Peter Anvineba20a72002-04-30 20:53:55 +000062
H. Peter Anvinfe501952007-10-02 21:53:51 -070063#include "compiler.h"
64
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000065#include <stdio.h>
H. Peter Anvinaf535c12002-04-30 20:59:21 +000066#include <stdarg.h>
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000067#include <stdlib.h>
68#include <stddef.h>
69#include <string.h>
70#include <ctype.h>
H. Peter Anvin76690a12002-04-30 20:52:49 +000071#include <limits.h>
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000072
73#include "nasm.h"
74#include "nasmlib.h"
H. Peter Anvinb20bc732017-03-07 19:23:03 -080075#include "error.h"
H. Peter Anvin4169a472007-09-12 01:29:43 +000076#include "preproc.h"
H. Peter Anvin97a23472007-09-16 17:57:25 -070077#include "hashtbl.h"
H. Peter Anvin8cad14b2008-06-01 17:23:51 -070078#include "quote.h"
H. Peter Anvinc2df2822007-10-24 15:29:28 -070079#include "stdscan.h"
H. Peter Anvindbb640b2009-07-18 18:57:16 -070080#include "eval.h"
H. Peter Anvinc2df2822007-10-24 15:29:28 -070081#include "tokens.h"
H. Peter Anvina4835d42008-05-20 14:21:29 -070082#include "tables.h"
H. Peter Anvin8ac25aa2016-02-18 01:16:18 -080083#include "listing.h"
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000084
85typedef struct SMacro SMacro;
H. Peter Anvin36206cd2012-03-03 16:14:51 -080086typedef struct MMacro MMacro;
87typedef struct MMacroInvocation MMacroInvocation;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000088typedef struct Context Context;
89typedef struct Token Token;
H. Peter Anvince616072002-04-30 21:02:23 +000090typedef struct Blocks Blocks;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000091typedef struct Line Line;
92typedef struct Include Include;
H. Peter Anvin36206cd2012-03-03 16:14:51 -080093typedef struct Cond Cond;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000094
95/*
H. Peter Anvin97a23472007-09-16 17:57:25 -070096 * Note on the storage of both SMacro and MMacros: the hash table
97 * indexes them case-insensitively, and we then have to go through a
98 * linked list of potential case aliases (and, for MMacros, parameter
99 * ranges); this is to preserve the matching semantics of the earlier
100 * code. If the number of case aliases for a specific macro is a
101 * performance issue, you may want to reconsider your coding style.
102 */
103
104/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000105 * Store the definition of a single-line macro.
106 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000107struct SMacro {
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800108 SMacro *next;
109 char *name;
110 bool casesense;
111 bool in_progress;
112 unsigned int nparam;
113 Token *expansion;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000114};
115
116/*
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800117 * Store the definition of a multi-line macro. This is also used to
118 * store the interiors of `%rep...%endrep' blocks, which are
119 * effectively self-re-invoking multi-line macros which simply
120 * don't have a name or bother to appear in the hash tables. %rep
121 * blocks are signified by having a NULL `name' field.
122 *
123 * In a MMacro describing a `%rep' block, the `in_progress' field
124 * isn't merely boolean, but gives the number of repeats left to
125 * run.
126 *
127 * The `next' field is used for storing MMacros in hash tables; the
128 * `next_active' field is for stacking them on istk entries.
129 *
130 * When a MMacro is being expanded, `params', `iline', `nparam',
131 * `paramlen', `rotate' and `unique' are local to the invocation.
132 */
133struct MMacro {
134 MMacro *next;
135 MMacroInvocation *prev; /* previous invocation */
136 char *name;
137 int nparam_min, nparam_max;
138 bool casesense;
139 bool plus; /* is the last parameter greedy? */
140 bool nolist; /* is this macro listing-inhibited? */
141 int64_t in_progress; /* is this macro currently being expanded? */
142 int32_t max_depth; /* maximum number of recursive expansions allowed */
143 Token *dlist; /* All defaults as one list */
144 Token **defaults; /* Parameter default pointers */
145 int ndefs; /* number of default parameters */
146 Line *expansion;
147
148 MMacro *next_active;
149 MMacro *rep_nest; /* used for nesting %rep */
150 Token **params; /* actual parameters */
151 Token *iline; /* invocation line */
152 unsigned int nparam, rotate;
153 int *paramlen;
154 uint64_t unique;
155 int lineno; /* Current line number on expansion */
156 uint64_t condcnt; /* number of if blocks... */
H. Peter Anvin4def1a82016-05-09 13:59:44 -0700157
H. Peter Anvin274cda82016-05-10 02:56:29 -0700158 const char *fname; /* File where defined */
H. Peter Anvin4def1a82016-05-09 13:59:44 -0700159 int32_t xline; /* First line in macro */
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800160};
161
162
163/* Store the definition of a multi-line macro, as defined in a
164 * previous recursive macro expansion.
165 */
166struct MMacroInvocation {
167 MMacroInvocation *prev; /* previous invocation */
168 Token **params; /* actual parameters */
169 Token *iline; /* invocation line */
170 unsigned int nparam, rotate;
171 int *paramlen;
172 uint64_t unique;
173 uint64_t condcnt;
174};
175
176
177/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000178 * The context stack is composed of a linked list of these.
179 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000180struct Context {
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800181 Context *next;
182 char *name;
183 struct hash_table localmac;
184 uint32_t number;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000185};
186
187/*
188 * This is the internal form which we break input lines up into.
189 * Typically stored in linked lists.
190 *
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000191 * Note that `type' serves a double meaning: TOK_SMAC_PARAM is not
192 * necessarily used as-is, but is intended to denote the number of
193 * the substituted parameter. So in the definition
194 *
195 * %define a(x,y) ( (x) & ~(y) )
H. Peter Anvin70653092007-10-19 14:42:29 -0700196 *
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000197 * the token representing `x' will have its type changed to
198 * TOK_SMAC_PARAM, but the one representing `y' will be
199 * TOK_SMAC_PARAM+1.
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000200 *
201 * TOK_INTERNAL_STRING is a dirty hack: it's a single string token
202 * which doesn't need quotes around it. Used in the pre-include
203 * mechanism as an alternative to trying to find a sensible type of
204 * quote to use on the filename we were passed.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000205 */
H. Peter Anvinda10e7b2007-09-12 04:18:37 +0000206enum pp_token_type {
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800207 TOK_NONE = 0, TOK_WHITESPACE, TOK_COMMENT, TOK_ID,
208 TOK_PREPROC_ID, TOK_STRING,
209 TOK_NUMBER, TOK_FLOAT, TOK_SMAC_END, TOK_OTHER,
H. Peter Anvin6c81f0a2008-05-25 21:46:17 -0700210 TOK_INTERNAL_STRING,
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800211 TOK_PREPROC_Q, TOK_PREPROC_QQ,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300212 TOK_PASTE, /* %+ */
213 TOK_INDIRECT, /* %[...] */
214 TOK_SMAC_PARAM, /* MUST BE LAST IN THE LIST!!! */
215 TOK_MAX = INT_MAX /* Keep compiler from reducing the range */
H. Peter Anvinda10e7b2007-09-12 04:18:37 +0000216};
217
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +0400218#define PP_CONCAT_MASK(x) (1 << (x))
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +0400219#define PP_CONCAT_MATCH(t, mask) (PP_CONCAT_MASK((t)->type) & mask)
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +0400220
Cyrill Gorcunov575d4282010-10-06 00:25:55 +0400221struct tokseq_match {
222 int mask_head;
223 int mask_tail;
224};
225
H. Peter Anvine2c80182005-01-15 22:15:51 +0000226struct Token {
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800227 Token *next;
228 char *text;
H. Peter Anvinf26e0972008-07-01 21:26:27 -0700229 union {
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800230 SMacro *mac; /* associated macro for TOK_SMAC_END */
231 size_t len; /* scratch length field */
232 } a; /* Auxiliary data */
233 enum pp_token_type type;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000234};
235
236/*
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800237 * Multi-line macro definitions are stored as a linked list of
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000238 * these, which is essentially a container to allow several linked
239 * lists of Tokens.
H. Peter Anvin70653092007-10-19 14:42:29 -0700240 *
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000241 * Note that in this module, linked lists are treated as stacks
242 * wherever possible. For this reason, Lines are _pushed_ on to the
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800243 * `expansion' field in MMacro structures, so that the linked list,
244 * if walked, would give the macro lines in reverse order; this
245 * means that we can walk the list when expanding a macro, and thus
246 * push the lines on to the `expansion' field in _istk_ in reverse
247 * order (so that when popped back off they are in the right
248 * order). It may seem cockeyed, and it relies on my design having
249 * an even number of steps in, but it works...
250 *
251 * Some of these structures, rather than being actual lines, are
252 * markers delimiting the end of the expansion of a given macro.
253 * This is for use in the cycle-tracking and %rep-handling code.
254 * Such structures have `finishes' non-NULL, and `first' NULL. All
255 * others have `finishes' NULL, but `first' may still be NULL if
256 * the line is blank.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000257 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000258struct Line {
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800259 Line *next;
260 MMacro *finishes;
261 Token *first;
Keith Kaniosb307a4f2010-11-06 17:41:51 -0500262};
263
264/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000265 * To handle an arbitrary level of file inclusion, we maintain a
266 * stack (ie linked list) of these things.
267 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000268struct Include {
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800269 Include *next;
270 FILE *fp;
271 Cond *conds;
272 Line *expansion;
H. Peter Anvin274cda82016-05-10 02:56:29 -0700273 const char *fname;
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800274 int lineno, lineinc;
275 MMacro *mstk; /* stack of active macros/reps */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000276};
277
278/*
H. Peter Anvin169ac7c2016-09-25 17:08:05 -0700279 * File real name hash, so we don't have to re-search the include
280 * path for every pass (and potentially more than that if a file
281 * is used more than once.)
282 */
283struct hash_table FileHash;
284
285/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000286 * Conditional assembly: we maintain a separate stack of these for
287 * each level of file inclusion. (The only reason we keep the
288 * stacks separate is to ensure that a stray `%endif' in a file
289 * included from within the true branch of a `%if' won't terminate
290 * it and cause confusion: instead, rightly, it'll cause an error.)
291 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800292struct Cond {
293 Cond *next;
294 int state;
295};
H. Peter Anvine2c80182005-01-15 22:15:51 +0000296enum {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000297 /*
298 * These states are for use just after %if or %elif: IF_TRUE
299 * means the condition has evaluated to truth so we are
300 * currently emitting, whereas IF_FALSE means we are not
301 * currently emitting but will start doing so if a %else comes
302 * up. In these states, all directives are admissible: %elif,
303 * %else and %endif. (And of course %if.)
304 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800305 COND_IF_TRUE, COND_IF_FALSE,
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000306 /*
307 * These states come up after a %else: ELSE_TRUE means we're
308 * emitting, and ELSE_FALSE means we're not. In ELSE_* states,
309 * any %elif or %else will cause an error.
310 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800311 COND_ELSE_TRUE, COND_ELSE_FALSE,
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000312 /*
Victor van den Elzen3b404c02008-09-18 13:51:36 +0200313 * These states mean that we're not emitting now, and also that
314 * nothing until %endif will be emitted at all. COND_DONE is
315 * used when we've had our moment of emission
316 * and have now started seeing %elifs. COND_NEVER is used when
317 * the condition construct in question is contained within a
318 * non-emitting branch of a larger condition construct,
319 * or if there is an error.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000320 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800321 COND_DONE, COND_NEVER
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000322};
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800323#define emitting(x) ( (x) == COND_IF_TRUE || (x) == COND_ELSE_TRUE )
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000324
H. Peter Anvin70653092007-10-19 14:42:29 -0700325/*
Ed Beroset3ab3f412002-06-11 03:31:49 +0000326 * These defines are used as the possible return values for do_directive
327 */
328#define NO_DIRECTIVE_FOUND 0
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300329#define DIRECTIVE_FOUND 1
Ed Beroset3ab3f412002-06-11 03:31:49 +0000330
Cyrill Gorcunove091d6e2010-08-09 13:58:22 +0400331/* max reps */
332#define REP_LIMIT ((INT64_C(1) << 62))
333
Keith Kanios852f1ee2009-07-12 00:19:55 -0500334/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000335 * Condition codes. Note that we use c_ prefix not C_ because C_ is
336 * used in nasm.h for the "real" condition codes. At _this_ level,
337 * we treat CXZ and ECXZ as condition codes, albeit non-invertible
338 * ones, so we need a different enum...
339 */
H. Peter Anvin476d2862007-10-02 22:04:15 -0700340static const char * const conditions[] = {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000341 "a", "ae", "b", "be", "c", "cxz", "e", "ecxz", "g", "ge", "l", "le",
342 "na", "nae", "nb", "nbe", "nc", "ne", "ng", "nge", "nl", "nle", "no",
H. Peter Anvince9be342007-09-12 00:22:29 +0000343 "np", "ns", "nz", "o", "p", "pe", "po", "rcxz", "s", "z"
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000344};
H. Peter Anvin476d2862007-10-02 22:04:15 -0700345enum pp_conds {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000346 c_A, c_AE, c_B, c_BE, c_C, c_CXZ, c_E, c_ECXZ, c_G, c_GE, c_L, c_LE,
347 c_NA, c_NAE, c_NB, c_NBE, c_NC, c_NE, c_NG, c_NGE, c_NL, c_NLE, c_NO,
H. Peter Anvin476d2862007-10-02 22:04:15 -0700348 c_NP, c_NS, c_NZ, c_O, c_P, c_PE, c_PO, c_RCXZ, c_S, c_Z,
349 c_none = -1
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000350};
H. Peter Anvin476d2862007-10-02 22:04:15 -0700351static const enum pp_conds inverse_ccs[] = {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000352 c_NA, c_NAE, c_NB, c_NBE, c_NC, -1, c_NE, -1, c_NG, c_NGE, c_NL, c_NLE,
353 c_A, c_AE, c_B, c_BE, c_C, c_E, c_G, c_GE, c_L, c_LE, c_O, c_P, c_S,
H. Peter Anvince9be342007-09-12 00:22:29 +0000354 c_Z, c_NO, c_NP, c_PO, c_PE, -1, c_NS, c_NZ
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000355};
356
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800357/*
358 * Directive names.
359 */
360/* If this is a an IF, ELIF, ELSE or ENDIF keyword */
361static int is_condition(enum preproc_token arg)
362{
363 return PP_IS_COND(arg) || (arg == PP_ELSE) || (arg == PP_ENDIF);
364}
365
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000366/* For TASM compatibility we need to be able to recognise TASM compatible
367 * conditional compilation directives. Using the NASM pre-processor does
368 * not work, so we look for them specifically from the following list and
369 * then jam in the equivalent NASM directive into the input stream.
370 */
371
H. Peter Anvine2c80182005-01-15 22:15:51 +0000372enum {
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000373 TM_ARG, TM_ELIF, TM_ELSE, TM_ENDIF, TM_IF, TM_IFDEF, TM_IFDIFI,
374 TM_IFNDEF, TM_INCLUDE, TM_LOCAL
375};
376
H. Peter Anvin476d2862007-10-02 22:04:15 -0700377static const char * const tasm_directives[] = {
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000378 "arg", "elif", "else", "endif", "if", "ifdef", "ifdifi",
379 "ifndef", "include", "local"
380};
381
382static int StackSize = 4;
H. Peter Anvin6c8b2be2016-05-24 23:46:50 -0700383static const char *StackPointer = "ebp";
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000384static int ArgOffset = 8;
H. Peter Anvin8781cb02007-11-08 20:01:11 -0800385static int LocalOffset = 0;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000386
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000387static Context *cstk;
388static Include *istk;
Cyrill Gorcunov8c0666b2018-11-24 14:33:48 +0300389static const struct strlist *ipath_list;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000390
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300391static int pass; /* HACK: pass 0 = generate dependencies only */
Cyrill Gorcunovb7bb5ac2018-11-11 21:33:52 +0300392static struct strlist *deplist;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000393
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300394static uint64_t unique; /* unique identifier numbers */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000395
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800396static Line *predef = NULL;
H. Peter Anvind2456592008-06-19 15:04:18 -0700397static bool do_predef;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000398
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000399/*
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800400 * The current set of multi-line macros we have defined.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000401 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800402static struct hash_table mmacros;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000403
404/*
405 * The current set of single-line macros we have defined.
406 */
H. Peter Anvin166c2472008-05-28 12:28:58 -0700407static struct hash_table smacros;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000408
409/*
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800410 * The multi-line macro we are currently defining, or the %rep
411 * block we are currently reading, if any.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000412 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800413static MMacro *defining;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000414
Charles Crayned4200be2008-07-12 16:42:33 -0700415static uint64_t nested_mac_count;
416static uint64_t nested_rep_count;
417
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000418/*
419 * The number of macro parameters to allocate space for at a time.
420 */
421#define PARAM_DELTA 16
422
423/*
H. Peter Anvinf7606612016-07-13 14:23:48 -0700424 * The standard macro set: defined in macros.c in a set of arrays.
425 * This gives our position in any macro set, while we are processing it.
426 * The stdmacset is an array of such macro sets.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000427 */
H. Peter Anvina70547f2008-07-19 21:44:26 -0700428static macros_t *stdmacpos;
H. Peter Anvinf7606612016-07-13 14:23:48 -0700429static macros_t **stdmacnext;
430static macros_t *stdmacros[8];
Cyrill Gorcunov15ce78f2017-01-06 20:21:28 +0300431static macros_t *extrastdmac;
H. Peter Anvin76690a12002-04-30 20:52:49 +0000432
433/*
H. Peter Anvin734b1882002-04-30 21:01:08 +0000434 * Tokens are allocated in blocks to improve speed
435 */
436#define TOKEN_BLOCKSIZE 4096
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800437static Token *freeTokens = NULL;
H. Peter Anvince616072002-04-30 21:02:23 +0000438struct Blocks {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000439 Blocks *next;
440 void *chunk;
H. Peter Anvince616072002-04-30 21:02:23 +0000441};
442
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800443static Blocks blocks = { NULL, NULL };
H. Peter Anvin734b1882002-04-30 21:01:08 +0000444
445/*
H. Peter Anvin76690a12002-04-30 20:52:49 +0000446 * Forward declarations.
447 */
H. Peter Anvinf7606612016-07-13 14:23:48 -0700448static void pp_add_stdmac(macros_t *macros);
H. Peter Anvin734b1882002-04-30 21:01:08 +0000449static Token *expand_mmac_params(Token * tline);
450static Token *expand_smacro(Token * tline);
451static Token *expand_id(Token * tline);
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +0400452static Context *get_ctx(const char *name, const char **namep);
Keith Kaniosa5fc6462007-10-13 07:09:22 -0700453static void make_tok_num(Token * tok, int64_t val);
H. Peter 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{
Cyrill Gorcunov8c0666b2018-11-24 14:33:48 +03001504 const struct strlist_entry *ip = NULL;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001505 FILE *fp;
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07001506 const char *prefix = "";
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
Cyrill Gorcunov8c0666b2018-11-24 14:33:48 +03001510 if (ipath_list)
1511 ip = ipath_list->head;
1512
H. Peter Anvine2c80182005-01-15 22:15:51 +00001513 while (1) {
night199ukfdb1a1b2018-10-18 23:19:47 +02001514 sp = nasm_catfile(prefix, file);
H. Peter Anvind81a2352016-09-21 14:03:18 -07001515 if (omode == INC_PROBE) {
1516 fp = NULL;
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07001517 found = nasm_file_exists(sp);
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001518 } else {
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07001519 fp = nasm_open_read(sp, fmode);
H. Peter Anvind81a2352016-09-21 14:03:18 -07001520 found = (fp != NULL);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001521 }
H. Peter Anvind81a2352016-09-21 14:03:18 -07001522 if (found) {
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07001523 *slpath = sp;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001524 return fp;
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001525 }
Jim Kukunas65a8afc2016-06-13 16:00:42 -04001526
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07001527 nasm_free(sp);
Jim Kukunas65a8afc2016-06-13 16:00:42 -04001528
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07001529 if (!ip) {
1530 *slpath = NULL;
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001531 return NULL;
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07001532 }
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001533
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07001534 prefix = ip->str;
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001535 ip = ip->next;
1536 }
1537}
1538
1539/*
1540 * Open a file, or test for the presence of one (depending on omode),
1541 * considering the include path.
1542 */
1543static FILE *inc_fopen(const char *file,
Cyrill Gorcunovb7bb5ac2018-11-11 21:33:52 +03001544 struct strlist *dhead,
H. Peter Anvinccad6f92016-10-04 00:34:35 -07001545 const char **found_path,
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001546 enum incopen_mode omode,
1547 enum file_flags fmode)
1548{
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001549 struct hash_insert hi;
1550 void **hp;
1551 char *path;
1552 FILE *fp = NULL;
1553
1554 hp = hash_find(&FileHash, file, &hi);
1555 if (hp) {
1556 path = *hp;
Martin Storsjöf283c8f2017-08-13 17:28:46 +03001557 if (path || omode != INC_NEEDED) {
Cyrill Gorcunovb7bb5ac2018-11-11 21:33:52 +03001558 strlist_add(dhead, path ? path : file);
Martin Storsjöf283c8f2017-08-13 17:28:46 +03001559 }
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001560 } else {
1561 /* Need to do the actual path search */
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07001562 fp = inc_fopen_search(file, &path, omode, fmode);
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001563
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07001564 /* Positive or negative result */
1565 hash_add(&hi, nasm_strdup(file), path);
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001566
H. Peter Anvin9924d1e2016-10-04 00:59:39 -07001567 /*
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07001568 * Add file to dependency path.
H. Peter Anvin9924d1e2016-10-04 00:59:39 -07001569 */
1570 if (path || omode != INC_NEEDED)
Cyrill Gorcunovb7bb5ac2018-11-11 21:33:52 +03001571 strlist_add(dhead, file);
H. Peter Anvineba20a72002-04-30 20:53:55 +00001572 }
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001573
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001574 if (!path) {
1575 if (omode == INC_NEEDED)
H. Peter Anvinc5136902018-06-15 18:20:17 -07001576 nasm_fatal("unable to open include file `%s'", file);
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07001577 } else {
1578 if (!fp && omode != INC_PROBE)
1579 fp = nasm_open_read(path, fmode);
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001580 }
1581
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001582 if (found_path)
H. Peter Anvinccad6f92016-10-04 00:34:35 -07001583 *found_path = path;
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001584
1585 return fp;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001586}
1587
1588/*
Fabian Giesen0bbc38d2016-04-28 13:48:14 -07001589 * Opens an include or input file. Public version, for use by modules
1590 * that get a file:lineno pair and need to look at the file again
1591 * (e.g. the CodeView debug backend). Returns NULL on failure.
1592 */
H. Peter Anvin3e83cec2016-05-25 04:28:46 -07001593FILE *pp_input_fopen(const char *filename, enum file_flags mode)
Fabian Giesen0bbc38d2016-04-28 13:48:14 -07001594{
H. Peter Anvin9924d1e2016-10-04 00:59:39 -07001595 return inc_fopen(filename, NULL, NULL, INC_OPTIONAL, mode);
Fabian Giesen0bbc38d2016-04-28 13:48:14 -07001596}
1597
1598/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001599 * Determine if we should warn on defining a single-line macro of
H. Peter Anvinef7468f2002-04-30 20:57:59 +00001600 * name `name', with `nparam' parameters. If nparam is 0 or -1, will
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001601 * return true if _any_ single-line macro of that name is defined.
1602 * Otherwise, will return true if a single-line macro with either
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001603 * `nparam' or no parameters is defined.
1604 *
1605 * If a macro with precisely the right number of parameters is
H. Peter Anvinef7468f2002-04-30 20:57:59 +00001606 * defined, or nparam is -1, the address of the definition structure
1607 * will be returned in `defn'; otherwise NULL will be returned. If `defn'
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001608 * is NULL, no action will be taken regarding its contents, and no
1609 * error will occur.
1610 *
1611 * Note that this is also called with nparam zero to resolve
1612 * `ifdef'.
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001613 *
1614 * If you already know which context macro belongs to, you can pass
1615 * the context pointer as first parameter; if you won't but name begins
1616 * with %$ the context will be automatically computed. If all_contexts
1617 * is true, macro will be searched in outer contexts as well.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001618 */
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07001619static bool
H. Peter Anvinb2a5fda2008-06-19 21:42:42 -07001620smacro_defined(Context * ctx, const char *name, int nparam, SMacro ** defn,
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07001621 bool nocase)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001622{
H. Peter Anvin166c2472008-05-28 12:28:58 -07001623 struct hash_table *smtbl;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001624 SMacro *m;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001625
H. Peter Anvin97a23472007-09-16 17:57:25 -07001626 if (ctx) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001627 smtbl = &ctx->localmac;
H. Peter Anvin97a23472007-09-16 17:57:25 -07001628 } else if (name[0] == '%' && name[1] == '$') {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001629 if (cstk)
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04001630 ctx = get_ctx(name, &name);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001631 if (!ctx)
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001632 return false; /* got to return _something_ */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001633 smtbl = &ctx->localmac;
H. Peter Anvin97a23472007-09-16 17:57:25 -07001634 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001635 smtbl = &smacros;
H. Peter Anvin97a23472007-09-16 17:57:25 -07001636 }
H. Peter Anvin166c2472008-05-28 12:28:58 -07001637 m = (SMacro *) hash_findix(smtbl, name);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001638
H. Peter Anvine2c80182005-01-15 22:15:51 +00001639 while (m) {
1640 if (!mstrcmp(m->name, name, m->casesense && nocase) &&
Charles Crayne192d5b52007-10-18 19:02:42 -07001641 (nparam <= 0 || m->nparam == 0 || nparam == (int) m->nparam)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001642 if (defn) {
Charles Crayne192d5b52007-10-18 19:02:42 -07001643 if (nparam == (int) m->nparam || nparam == -1)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001644 *defn = m;
1645 else
1646 *defn = NULL;
1647 }
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001648 return true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001649 }
1650 m = m->next;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001651 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001652
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001653 return false;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001654}
1655
Cyrill Gorcunov3079f792018-11-14 10:03:42 +03001656/* param should be a natural number [0; INT_MAX] */
1657static int read_param_count(const char *str)
1658{
1659 int result;
1660 bool err;
1661
1662 result = readnum(str, &err);
1663 if (result < 0 || result > INT_MAX) {
1664 result = 0;
1665 nasm_error(ERR_NONFATAL, "parameter count `%s' is out of bounds [%d; %d]",
1666 str, 0, INT_MAX);
1667 } else if (err) {
1668 nasm_error(ERR_NONFATAL, "unable to parse parameter count `%s'", str);
1669 }
1670 return result;
1671}
1672
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001673/*
1674 * Count and mark off the parameters in a multi-line macro call.
1675 * This is called both from within the multi-line macro expansion
1676 * code, and also to mark off the default parameters when provided
1677 * in a %macro definition line.
1678 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001679static void count_mmac_params(Token * t, int *nparam, Token *** params)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001680{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001681 int paramsize, brace;
1682
1683 *nparam = paramsize = 0;
1684 *params = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001685 while (t) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001686 /* +1: we need space for the final NULL */
H. Peter Anvin91fb6f12008-09-01 10:56:33 -07001687 if (*nparam+1 >= paramsize) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001688 paramsize += PARAM_DELTA;
1689 *params = nasm_realloc(*params, sizeof(**params) * paramsize);
1690 }
1691 skip_white_(t);
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08001692 brace = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001693 if (tok_is_(t, "{"))
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08001694 brace++;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001695 (*params)[(*nparam)++] = t;
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08001696 if (brace) {
1697 while (brace && (t = t->next) != NULL) {
1698 if (tok_is_(t, "{"))
1699 brace++;
1700 else if (tok_is_(t, "}"))
1701 brace--;
1702 }
1703
1704 if (t) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001705 /*
1706 * Now we've found the closing brace, look further
1707 * for the comma.
1708 */
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08001709 t = t->next;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001710 skip_white_(t);
1711 if (tok_isnt_(t, ",")) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08001712 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00001713 "braces do not enclose all of macro parameter");
1714 while (tok_isnt_(t, ","))
1715 t = t->next;
1716 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001717 }
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08001718 } else {
1719 while (tok_isnt_(t, ","))
1720 t = t->next;
1721 }
1722 if (t) { /* got a comma/brace */
1723 t = t->next; /* eat the comma */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001724 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001725 }
1726}
1727
1728/*
H. Peter Anvin76690a12002-04-30 20:52:49 +00001729 * Determine whether one of the various `if' conditions is true or
1730 * not.
1731 *
1732 * We must free the tline we get passed.
1733 */
H. Peter Anvin70055962007-10-11 00:05:31 -07001734static bool if_condition(Token * tline, enum preproc_token ct)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001735{
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001736 enum pp_conditional i = PP_COND(ct);
H. Peter Anvin70055962007-10-11 00:05:31 -07001737 bool j;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001738 Token *t, *tt, **tptr, *origline;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001739 struct tokenval tokval;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001740 expr *evalresult;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001741 enum pp_token_type needtype;
H. Peter Anvin077fb932010-07-20 14:56:30 -07001742 char *p;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001743
1744 origline = tline;
1745
H. Peter Anvine2c80182005-01-15 22:15:51 +00001746 switch (i) {
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001747 case PPC_IFCTX:
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001748 j = false; /* have we matched yet? */
Victor van den Elzen0e857f12008-07-23 13:21:29 +02001749 while (true) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001750 skip_white_(tline);
Victor van den Elzen0e857f12008-07-23 13:21:29 +02001751 if (!tline)
1752 break;
1753 if (tline->type != TOK_ID) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08001754 nasm_error(ERR_NONFATAL,
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001755 "`%s' expects context identifiers", pp_directives[ct]);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001756 free_tlist(origline);
1757 return -1;
1758 }
Victor van den Elzen0e857f12008-07-23 13:21:29 +02001759 if (cstk && cstk->name && !nasm_stricmp(tline->text, cstk->name))
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001760 j = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001761 tline = tline->next;
1762 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001763 break;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001764
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001765 case PPC_IFDEF:
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001766 j = false; /* have we matched yet? */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001767 while (tline) {
1768 skip_white_(tline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001769 if (!tline || (tline->type != TOK_ID &&
1770 (tline->type != TOK_PREPROC_ID ||
1771 tline->text[1] != '$'))) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08001772 nasm_error(ERR_NONFATAL,
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001773 "`%s' expects macro identifiers", pp_directives[ct]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001774 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001775 }
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07001776 if (smacro_defined(NULL, tline->text, 0, NULL, true))
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001777 j = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001778 tline = tline->next;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001779 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001780 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001781
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001782 case PPC_IFENV:
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001783 tline = expand_smacro(tline);
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001784 j = false; /* have we matched yet? */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001785 while (tline) {
1786 skip_white_(tline);
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001787 if (!tline || (tline->type != TOK_ID &&
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001788 tline->type != TOK_STRING &&
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001789 (tline->type != TOK_PREPROC_ID ||
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001790 tline->text[1] != '!'))) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08001791 nasm_error(ERR_NONFATAL,
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001792 "`%s' expects environment variable names",
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001793 pp_directives[ct]);
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001794 goto fail;
1795 }
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001796 p = tline->text;
1797 if (tline->type == TOK_PREPROC_ID)
1798 p += 2; /* Skip leading %! */
1799 if (*p == '\'' || *p == '\"' || *p == '`')
1800 nasm_unquote_cstr(p, ct);
1801 if (getenv(p))
1802 j = true;
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001803 tline = tline->next;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001804 }
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001805 break;
1806
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001807 case PPC_IFIDN:
1808 case PPC_IFIDNI:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001809 tline = expand_smacro(tline);
1810 t = tt = tline;
1811 while (tok_isnt_(tt, ","))
1812 tt = tt->next;
1813 if (!tt) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08001814 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00001815 "`%s' expects two comma-separated arguments",
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001816 pp_directives[ct]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001817 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001818 }
1819 tt = tt->next;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001820 j = true; /* assume equality unless proved not */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001821 while ((t->type != TOK_OTHER || strcmp(t->text, ",")) && tt) {
1822 if (tt->type == TOK_OTHER && !strcmp(tt->text, ",")) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08001823 nasm_error(ERR_NONFATAL, "`%s': more than one comma on line",
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001824 pp_directives[ct]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001825 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001826 }
1827 if (t->type == TOK_WHITESPACE) {
1828 t = t->next;
1829 continue;
1830 }
1831 if (tt->type == TOK_WHITESPACE) {
1832 tt = tt->next;
1833 continue;
1834 }
1835 if (tt->type != t->type) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001836 j = false; /* found mismatching tokens */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001837 break;
1838 }
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07001839 /* When comparing strings, need to unquote them first */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001840 if (t->type == TOK_STRING) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001841 size_t l1 = nasm_unquote(t->text, NULL);
1842 size_t l2 = nasm_unquote(tt->text, NULL);
H. Peter Anvind2456592008-06-19 15:04:18 -07001843
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001844 if (l1 != l2) {
1845 j = false;
1846 break;
1847 }
1848 if (mmemcmp(t->text, tt->text, l1, i == PPC_IFIDN)) {
1849 j = false;
1850 break;
1851 }
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07001852 } else if (mstrcmp(tt->text, t->text, i == PPC_IFIDN) != 0) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001853 j = false; /* found mismatching tokens */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001854 break;
1855 }
Nickolay Yurchenkof3b3ce22003-09-21 20:38:43 +00001856
H. Peter Anvine2c80182005-01-15 22:15:51 +00001857 t = t->next;
1858 tt = tt->next;
1859 }
1860 if ((t->type != TOK_OTHER || strcmp(t->text, ",")) || tt)
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001861 j = false; /* trailing gunk on one end or other */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001862 break;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001863
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001864 case PPC_IFMACRO:
H. Peter Anvin89cee572009-07-15 09:16:54 -04001865 {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001866 bool found = false;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001867 MMacro searching, *mmac;
H. Peter Anvin65747262002-05-07 00:10:05 +00001868
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001869 skip_white_(tline);
1870 tline = expand_id(tline);
1871 if (!tok_type_(tline, TOK_ID)) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08001872 nasm_error(ERR_NONFATAL,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001873 "`%s' expects a macro name", pp_directives[ct]);
1874 goto fail;
1875 }
1876 searching.name = nasm_strdup(tline->text);
1877 searching.casesense = true;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001878 searching.plus = false;
1879 searching.nolist = false;
1880 searching.in_progress = 0;
1881 searching.max_depth = 0;
1882 searching.rep_nest = NULL;
1883 searching.nparam_min = 0;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001884 searching.nparam_max = INT_MAX;
1885 tline = expand_smacro(tline->next);
1886 skip_white_(tline);
1887 if (!tline) {
1888 } else if (!tok_type_(tline, TOK_NUMBER)) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08001889 nasm_error(ERR_NONFATAL,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001890 "`%s' expects a parameter count or nothing",
1891 pp_directives[ct]);
1892 } else {
1893 searching.nparam_min = searching.nparam_max =
Cyrill Gorcunov3079f792018-11-14 10:03:42 +03001894 read_param_count(tline->text);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001895 }
1896 if (tline && tok_is_(tline->next, "-")) {
1897 tline = tline->next->next;
1898 if (tok_is_(tline, "*"))
1899 searching.nparam_max = INT_MAX;
1900 else if (!tok_type_(tline, TOK_NUMBER))
H. Peter Anvin130736c2016-02-17 20:27:41 -08001901 nasm_error(ERR_NONFATAL,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001902 "`%s' expects a parameter count after `-'",
1903 pp_directives[ct]);
1904 else {
Cyrill Gorcunov3079f792018-11-14 10:03:42 +03001905 searching.nparam_max = read_param_count(tline->text);
Cyrill Gorcunovc9244ea2017-10-22 15:25:48 +03001906 if (searching.nparam_min > searching.nparam_max) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08001907 nasm_error(ERR_NONFATAL,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001908 "minimum parameter count exceeds maximum");
Cyrill Gorcunovc9244ea2017-10-22 15:25:48 +03001909 searching.nparam_max = searching.nparam_min;
1910 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001911 }
1912 }
1913 if (tline && tok_is_(tline->next, "+")) {
1914 tline = tline->next;
1915 searching.plus = true;
1916 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001917 mmac = (MMacro *) hash_findix(&mmacros, searching.name);
1918 while (mmac) {
1919 if (!strcmp(mmac->name, searching.name) &&
1920 (mmac->nparam_min <= searching.nparam_max
1921 || searching.plus)
1922 && (searching.nparam_min <= mmac->nparam_max
1923 || mmac->plus)) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001924 found = true;
1925 break;
1926 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001927 mmac = mmac->next;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001928 }
1929 if (tline && tline->next)
H. Peter Anvin130736c2016-02-17 20:27:41 -08001930 nasm_error(ERR_WARNING|ERR_PASS1,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001931 "trailing garbage after %%ifmacro ignored");
1932 nasm_free(searching.name);
1933 j = found;
1934 break;
H. Peter Anvin89cee572009-07-15 09:16:54 -04001935 }
H. Peter Anvin65747262002-05-07 00:10:05 +00001936
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001937 case PPC_IFID:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001938 needtype = TOK_ID;
1939 goto iftype;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001940 case PPC_IFNUM:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001941 needtype = TOK_NUMBER;
1942 goto iftype;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001943 case PPC_IFSTR:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001944 needtype = TOK_STRING;
1945 goto iftype;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001946
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001947iftype:
1948 t = tline = expand_smacro(tline);
H. Peter Anvind85d2502008-05-04 17:53:31 -07001949
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001950 while (tok_type_(t, TOK_WHITESPACE) ||
1951 (needtype == TOK_NUMBER &&
1952 tok_type_(t, TOK_OTHER) &&
1953 (t->text[0] == '-' || t->text[0] == '+') &&
1954 !t->text[1]))
1955 t = t->next;
H. Peter Anvind85d2502008-05-04 17:53:31 -07001956
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001957 j = tok_type_(t, needtype);
1958 break;
H. Peter Anvincbf768d2008-02-16 16:41:25 -08001959
1960 case PPC_IFTOKEN:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001961 t = tline = expand_smacro(tline);
1962 while (tok_type_(t, TOK_WHITESPACE))
1963 t = t->next;
H. Peter Anvincbf768d2008-02-16 16:41:25 -08001964
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001965 j = false;
1966 if (t) {
1967 t = t->next; /* Skip the actual token */
1968 while (tok_type_(t, TOK_WHITESPACE))
1969 t = t->next;
1970 j = !t; /* Should be nothing left */
1971 }
1972 break;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001973
H. Peter Anvin134b9462008-02-16 17:01:40 -08001974 case PPC_IFEMPTY:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001975 t = tline = expand_smacro(tline);
1976 while (tok_type_(t, TOK_WHITESPACE))
1977 t = t->next;
H. Peter Anvin134b9462008-02-16 17:01:40 -08001978
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001979 j = !t; /* Should be empty */
1980 break;
H. Peter Anvin134b9462008-02-16 17:01:40 -08001981
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001982 case PPC_IF:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001983 t = tline = expand_smacro(tline);
1984 tptr = &t;
1985 tokval.t_type = TOKEN_INVALID;
1986 evalresult = evaluate(ppscan, tptr, &tokval,
H. Peter Anvin130736c2016-02-17 20:27:41 -08001987 NULL, pass | CRITICAL, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001988 if (!evalresult)
1989 return -1;
1990 if (tokval.t_type)
H. Peter Anvin130736c2016-02-17 20:27:41 -08001991 nasm_error(ERR_WARNING|ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +00001992 "trailing garbage after expression ignored");
1993 if (!is_simple(evalresult)) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08001994 nasm_error(ERR_NONFATAL,
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001995 "non-constant value given to `%s'", pp_directives[ct]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001996 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001997 }
Chuck Crayne60ae75d2007-05-02 01:59:16 +00001998 j = reloc_value(evalresult) != 0;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001999 break;
H. Peter Anvin95e28822007-09-12 04:20:08 +00002000
H. Peter Anvine2c80182005-01-15 22:15:51 +00002001 default:
H. Peter Anvin130736c2016-02-17 20:27:41 -08002002 nasm_error(ERR_FATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00002003 "preprocessor directive `%s' not yet implemented",
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002004 pp_directives[ct]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002005 goto fail;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002006 }
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002007
2008 free_tlist(origline);
2009 return j ^ PP_NEGATIVE(ct);
H. Peter Anvin70653092007-10-19 14:42:29 -07002010
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002011fail:
2012 free_tlist(origline);
2013 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002014}
2015
2016/*
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002017 * Common code for defining an smacro
2018 */
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08002019static bool define_smacro(Context *ctx, const char *mname, bool casesense,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002020 int nparam, Token *expansion)
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002021{
2022 SMacro *smac, **smhead;
H. Peter Anvin166c2472008-05-28 12:28:58 -07002023 struct hash_table *smtbl;
H. Peter Anvin70653092007-10-19 14:42:29 -07002024
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002025 if (smacro_defined(ctx, mname, nparam, &smac, casesense)) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002026 if (!smac) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002027 nasm_error(ERR_WARNING|ERR_PASS1,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002028 "single-line macro `%s' defined both with and"
2029 " without parameters", mname);
2030 /*
2031 * Some instances of the old code considered this a failure,
2032 * some others didn't. What is the right thing to do here?
2033 */
2034 free_tlist(expansion);
2035 return false; /* Failure */
2036 } else {
2037 /*
2038 * We're redefining, so we have to take over an
2039 * existing SMacro structure. This means freeing
2040 * what was already in it.
2041 */
2042 nasm_free(smac->name);
2043 free_tlist(smac->expansion);
2044 }
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002045 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002046 smtbl = ctx ? &ctx->localmac : &smacros;
2047 smhead = (SMacro **) hash_findi_add(smtbl, mname);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002048 smac = nasm_malloc(sizeof(SMacro));
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002049 smac->next = *smhead;
2050 *smhead = smac;
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002051 }
2052 smac->name = nasm_strdup(mname);
2053 smac->casesense = casesense;
2054 smac->nparam = nparam;
2055 smac->expansion = expansion;
2056 smac->in_progress = false;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002057 return true; /* Success */
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002058}
2059
2060/*
2061 * Undefine an smacro
2062 */
2063static void undef_smacro(Context *ctx, const char *mname)
2064{
2065 SMacro **smhead, *s, **sp;
H. Peter Anvin166c2472008-05-28 12:28:58 -07002066 struct hash_table *smtbl;
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002067
H. Peter Anvin166c2472008-05-28 12:28:58 -07002068 smtbl = ctx ? &ctx->localmac : &smacros;
2069 smhead = (SMacro **)hash_findi(smtbl, mname, NULL);
H. Peter Anvin70653092007-10-19 14:42:29 -07002070
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002071 if (smhead) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002072 /*
2073 * We now have a macro name... go hunt for it.
2074 */
2075 sp = smhead;
2076 while ((s = *sp) != NULL) {
2077 if (!mstrcmp(s->name, mname, s->casesense)) {
2078 *sp = s->next;
2079 nasm_free(s->name);
2080 free_tlist(s->expansion);
2081 nasm_free(s);
2082 } else {
2083 sp = &s->next;
2084 }
2085 }
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002086 }
2087}
2088
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002089/*
H. Peter Anvina26433d2008-07-16 14:40:01 -07002090 * Parse a mmacro specification.
2091 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002092static bool parse_mmacro_spec(Token *tline, MMacro *def, const char *directive)
H. Peter Anvina26433d2008-07-16 14:40:01 -07002093{
H. Peter Anvina26433d2008-07-16 14:40:01 -07002094 tline = tline->next;
2095 skip_white_(tline);
2096 tline = expand_id(tline);
2097 if (!tok_type_(tline, TOK_ID)) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002098 nasm_error(ERR_NONFATAL, "`%s' expects a macro name", directive);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002099 return false;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002100 }
Victor van den Elzenb916ede2008-07-23 15:14:22 +02002101
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002102 def->prev = NULL;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002103 def->name = nasm_strdup(tline->text);
2104 def->plus = false;
2105 def->nolist = false;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002106 def->in_progress = 0;
2107 def->rep_nest = NULL;
Victor van den Elzenb916ede2008-07-23 15:14:22 +02002108 def->nparam_min = 0;
2109 def->nparam_max = 0;
2110
H. Peter Anvina26433d2008-07-16 14:40:01 -07002111 tline = expand_smacro(tline->next);
2112 skip_white_(tline);
2113 if (!tok_type_(tline, TOK_NUMBER)) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002114 nasm_error(ERR_NONFATAL, "`%s' expects a parameter count", directive);
H. Peter Anvina26433d2008-07-16 14:40:01 -07002115 } else {
Cyrill Gorcunov3079f792018-11-14 10:03:42 +03002116 def->nparam_min = def->nparam_max = read_param_count(tline->text);
H. Peter Anvina26433d2008-07-16 14:40:01 -07002117 }
2118 if (tline && tok_is_(tline->next, "-")) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002119 tline = tline->next->next;
2120 if (tok_is_(tline, "*")) {
2121 def->nparam_max = INT_MAX;
2122 } else if (!tok_type_(tline, TOK_NUMBER)) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002123 nasm_error(ERR_NONFATAL,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002124 "`%s' expects a parameter count after `-'", directive);
2125 } else {
Cyrill Gorcunov3079f792018-11-14 10:03:42 +03002126 def->nparam_max = read_param_count(tline->text);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002127 if (def->nparam_min > def->nparam_max) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002128 nasm_error(ERR_NONFATAL, "minimum parameter count exceeds maximum");
Cyrill Gorcunovc9244ea2017-10-22 15:25:48 +03002129 def->nparam_max = def->nparam_min;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002130 }
2131 }
H. Peter Anvina26433d2008-07-16 14:40:01 -07002132 }
2133 if (tline && tok_is_(tline->next, "+")) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002134 tline = tline->next;
2135 def->plus = true;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002136 }
2137 if (tline && tok_type_(tline->next, TOK_ID) &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002138 !nasm_stricmp(tline->next->text, ".nolist")) {
2139 tline = tline->next;
2140 def->nolist = true;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002141 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002142
H. Peter Anvina26433d2008-07-16 14:40:01 -07002143 /*
2144 * Handle default parameters.
2145 */
2146 if (tline && tline->next) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002147 def->dlist = tline->next;
2148 tline->next = NULL;
2149 count_mmac_params(def->dlist, &def->ndefs, &def->defaults);
H. Peter Anvina26433d2008-07-16 14:40:01 -07002150 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002151 def->dlist = NULL;
2152 def->defaults = NULL;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002153 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002154 def->expansion = NULL;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002155
H. Peter Anvin89cee572009-07-15 09:16:54 -04002156 if (def->defaults && def->ndefs > def->nparam_max - def->nparam_min &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002157 !def->plus)
H. Peter Anvin130736c2016-02-17 20:27:41 -08002158 nasm_error(ERR_WARNING|ERR_PASS1|ERR_WARN_MDP,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002159 "too many default macro parameters");
Victor van den Elzenb916ede2008-07-23 15:14:22 +02002160
H. Peter Anvina26433d2008-07-16 14:40:01 -07002161 return true;
2162}
2163
2164
2165/*
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002166 * Decode a size directive
2167 */
2168static int parse_size(const char *str) {
2169 static const char *size_names[] =
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002170 { "byte", "dword", "oword", "qword", "tword", "word", "yword" };
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002171 static const int sizes[] =
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002172 { 0, 1, 4, 16, 8, 10, 2, 32 };
Cyrill Gorcunovc713b5f2018-09-29 14:30:14 +03002173 return str ? sizes[bsii(str, size_names, ARRAY_SIZE(size_names))+1] : 0;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002174}
2175
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07002176/*
2177 * Process a preprocessor %pragma directive. Currently there are none.
2178 * Gets passed the token list starting with the "preproc" token from
2179 * "%pragma preproc".
2180 */
2181static void do_pragma_preproc(Token *tline)
2182{
2183 /* Skip to the real stuff */
2184 tline = tline->next;
2185 skip_white_(tline);
2186 if (!tline)
2187 return;
2188
2189 (void)tline; /* Nothing else to do at present */
2190}
2191
Ed Beroset3ab3f412002-06-11 03:31:49 +00002192/**
2193 * find and process preprocessor directive in passed line
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002194 * Find out if a line contains a preprocessor directive, and deal
2195 * with it if so.
H. Peter Anvin70653092007-10-19 14:42:29 -07002196 *
Ed Beroset3ab3f412002-06-11 03:31:49 +00002197 * If a directive _is_ found, it is the responsibility of this routine
2198 * (and not the caller) to free_tlist() the line.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002199 *
Ed Beroset3ab3f412002-06-11 03:31:49 +00002200 * @param tline a pointer to the current tokeninzed line linked list
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07002201 * @param output if this directive generated output
Ed Beroset3ab3f412002-06-11 03:31:49 +00002202 * @return DIRECTIVE_FOUND or NO_DIRECTIVE_FOUND
H. Peter Anvin70653092007-10-19 14:42:29 -07002203 *
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002204 */
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07002205static int do_directive(Token *tline, char **output)
H. Peter Anvineba20a72002-04-30 20:53:55 +00002206{
H. Peter Anvin4169a472007-09-12 01:29:43 +00002207 enum preproc_token i;
2208 int j;
H. Peter Anvin70055962007-10-11 00:05:31 -07002209 bool err;
2210 int nparam;
2211 bool nolist;
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07002212 bool casesense;
H. Peter Anvin8cfdb9d2007-09-14 18:36:01 -07002213 int k, m;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002214 int offset;
H. Peter Anvinccad6f92016-10-04 00:34:35 -07002215 char *p, *pp;
2216 const char *found_path;
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08002217 const char *mname;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002218 Include *inc;
2219 Context *ctx;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002220 Cond *cond;
2221 MMacro *mmac, **mmhead;
Jin Kyu Songc9486b92013-10-28 17:07:57 -07002222 Token *t = NULL, *tt, *param_start, *macro_start, *last, **tptr, *origline;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002223 Line *l;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002224 struct tokenval tokval;
2225 expr *evalresult;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002226 MMacro *tmp_defining; /* Used when manipulating rep_nest */
H. Peter Anvinf8ba53e2007-10-11 10:11:57 -07002227 int64_t count;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07002228 size_t len;
H. Peter Anvin8e3f75e2008-09-24 00:21:58 -07002229 int severity;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002230
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07002231 *output = NULL; /* No output generated */
H. Peter Anvin76690a12002-04-30 20:52:49 +00002232 origline = tline;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002233
H. Peter Anvineba20a72002-04-30 20:53:55 +00002234 skip_white_(tline);
H. Peter Anvinf2936d72008-06-04 15:11:23 -07002235 if (!tline || !tok_type_(tline, TOK_PREPROC_ID) ||
Cyrill Gorcunov4b5b7372018-10-29 22:54:08 +03002236 (tline->text[0] && (tline->text[1] == '%' ||
2237 tline->text[1] == '$' ||
2238 tline->text[1] == '!')))
H. Peter Anvine2c80182005-01-15 22:15:51 +00002239 return NO_DIRECTIVE_FOUND;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002240
H. Peter Anvin4169a472007-09-12 01:29:43 +00002241 i = pp_token_hash(tline->text);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002242
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002243 /*
2244 * FIXME: We zap execution of PP_RMACRO, PP_IRMACRO, PP_EXITMACRO
2245 * since they are known to be buggy at moment, we need to fix them
2246 * in future release (2.09-2.10)
2247 */
Philipp Klokeb432f572013-03-31 12:01:23 +02002248 if (i == PP_RMACRO || i == PP_IRMACRO || i == PP_EXITMACRO) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002249 nasm_error(ERR_NONFATAL, "unknown preprocessor directive `%s'",
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002250 tline->text);
2251 return NO_DIRECTIVE_FOUND;
2252 }
2253
2254 /*
2255 * If we're in a non-emitting branch of a condition construct,
2256 * or walking to the end of an already terminated %rep block,
2257 * we should ignore all directives except for condition
2258 * directives.
2259 */
2260 if (((istk->conds && !emitting(istk->conds->state)) ||
2261 (istk->mstk && !istk->mstk->in_progress)) && !is_condition(i)) {
2262 return NO_DIRECTIVE_FOUND;
2263 }
2264
2265 /*
2266 * If we're defining a macro or reading a %rep block, we should
2267 * ignore all directives except for %macro/%imacro (which nest),
2268 * %endm/%endmacro, and (only if we're in a %rep block) %endrep.
2269 * If we're in a %rep block, another %rep nests, so should be let through.
2270 */
2271 if (defining && i != PP_MACRO && i != PP_IMACRO &&
2272 i != PP_RMACRO && i != PP_IRMACRO &&
2273 i != PP_ENDMACRO && i != PP_ENDM &&
2274 (defining->name || (i != PP_ENDREP && i != PP_REP))) {
2275 return NO_DIRECTIVE_FOUND;
2276 }
2277
2278 if (defining) {
2279 if (i == PP_MACRO || i == PP_IMACRO ||
2280 i == PP_RMACRO || i == PP_IRMACRO) {
2281 nested_mac_count++;
2282 return NO_DIRECTIVE_FOUND;
2283 } else if (nested_mac_count > 0) {
2284 if (i == PP_ENDMACRO) {
2285 nested_mac_count--;
2286 return NO_DIRECTIVE_FOUND;
2287 }
2288 }
2289 if (!defining->name) {
2290 if (i == PP_REP) {
2291 nested_rep_count++;
2292 return NO_DIRECTIVE_FOUND;
2293 } else if (nested_rep_count > 0) {
2294 if (i == PP_ENDREP) {
2295 nested_rep_count--;
2296 return NO_DIRECTIVE_FOUND;
2297 }
2298 }
2299 }
2300 }
2301
H. Peter Anvin4169a472007-09-12 01:29:43 +00002302 switch (i) {
2303 case PP_INVALID:
H. Peter Anvin130736c2016-02-17 20:27:41 -08002304 nasm_error(ERR_NONFATAL, "unknown preprocessor directive `%s'",
H. Peter Anvine2c80182005-01-15 22:15:51 +00002305 tline->text);
2306 return NO_DIRECTIVE_FOUND; /* didn't get it */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002307
H. Peter Anvin3f87a2a2016-10-04 14:07:19 -07002308 case PP_PRAGMA:
2309 /*
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07002310 * %pragma namespace options...
2311 *
2312 * The namespace "preproc" is reserved for the preprocessor;
2313 * all other namespaces generate a [pragma] assembly directive.
2314 *
2315 * Invalid %pragmas are ignored and may have different
2316 * meaning in future versions of NASM.
H. Peter Anvin3f87a2a2016-10-04 14:07:19 -07002317 */
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07002318 tline = tline->next;
2319 skip_white_(tline);
2320 tline = expand_smacro(tline);
2321 if (tok_type_(tline, TOK_ID)) {
2322 if (!nasm_stricmp(tline->text, "preproc")) {
2323 /* Preprocessor pragma */
2324 do_pragma_preproc(tline);
2325 } else {
2326 /* Build the assembler directive */
2327 t = new_Token(NULL, TOK_OTHER, "[", 1);
2328 t->next = new_Token(NULL, TOK_ID, "pragma", 6);
2329 t->next->next = new_Token(tline, TOK_WHITESPACE, NULL, 0);
2330 tline = t;
2331 for (t = tline; t->next; t = t->next)
2332 ;
2333 t->next = new_Token(NULL, TOK_OTHER, "]", 1);
2334 /* true here can be revisited in the future */
2335 *output = detoken(tline, true);
2336 }
2337 }
H. Peter Anvin3f87a2a2016-10-04 14:07:19 -07002338 free_tlist(origline);
2339 return DIRECTIVE_FOUND;
2340
H. Peter Anvine2c80182005-01-15 22:15:51 +00002341 case PP_STACKSIZE:
2342 /* Directive to tell NASM what the default stack size is. The
2343 * default is for a 16-bit stack, and this can be overriden with
2344 * %stacksize large.
H. Peter Anvine2c80182005-01-15 22:15:51 +00002345 */
2346 tline = tline->next;
2347 if (tline && tline->type == TOK_WHITESPACE)
2348 tline = tline->next;
2349 if (!tline || tline->type != TOK_ID) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002350 nasm_error(ERR_NONFATAL, "`%%stacksize' missing size parameter");
H. Peter Anvine2c80182005-01-15 22:15:51 +00002351 free_tlist(origline);
2352 return DIRECTIVE_FOUND;
2353 }
2354 if (nasm_stricmp(tline->text, "flat") == 0) {
2355 /* All subsequent ARG directives are for a 32-bit stack */
2356 StackSize = 4;
2357 StackPointer = "ebp";
2358 ArgOffset = 8;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002359 LocalOffset = 0;
Charles Crayne7eaf9192007-11-08 22:11:14 -08002360 } else if (nasm_stricmp(tline->text, "flat64") == 0) {
2361 /* All subsequent ARG directives are for a 64-bit stack */
2362 StackSize = 8;
2363 StackPointer = "rbp";
Per Jessen53252e02010-02-11 00:16:59 +03002364 ArgOffset = 16;
Charles Crayne7eaf9192007-11-08 22:11:14 -08002365 LocalOffset = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002366 } else if (nasm_stricmp(tline->text, "large") == 0) {
2367 /* All subsequent ARG directives are for a 16-bit stack,
2368 * far function call.
2369 */
2370 StackSize = 2;
2371 StackPointer = "bp";
2372 ArgOffset = 4;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002373 LocalOffset = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002374 } else if (nasm_stricmp(tline->text, "small") == 0) {
2375 /* All subsequent ARG directives are for a 16-bit stack,
2376 * far function call. We don't support near functions.
2377 */
2378 StackSize = 2;
2379 StackPointer = "bp";
2380 ArgOffset = 6;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002381 LocalOffset = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002382 } else {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002383 nasm_error(ERR_NONFATAL, "`%%stacksize' invalid size type");
H. Peter Anvine2c80182005-01-15 22:15:51 +00002384 free_tlist(origline);
2385 return DIRECTIVE_FOUND;
2386 }
2387 free_tlist(origline);
2388 return DIRECTIVE_FOUND;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002389
H. Peter Anvine2c80182005-01-15 22:15:51 +00002390 case PP_ARG:
2391 /* TASM like ARG directive to define arguments to functions, in
2392 * the following form:
2393 *
2394 * ARG arg1:WORD, arg2:DWORD, arg4:QWORD
2395 */
2396 offset = ArgOffset;
2397 do {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00002398 char *arg, directive[256];
H. Peter Anvine2c80182005-01-15 22:15:51 +00002399 int size = StackSize;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002400
H. Peter Anvine2c80182005-01-15 22:15:51 +00002401 /* Find the argument name */
2402 tline = tline->next;
2403 if (tline && tline->type == TOK_WHITESPACE)
2404 tline = tline->next;
2405 if (!tline || tline->type != TOK_ID) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002406 nasm_error(ERR_NONFATAL, "`%%arg' missing argument parameter");
H. Peter Anvine2c80182005-01-15 22:15:51 +00002407 free_tlist(origline);
2408 return DIRECTIVE_FOUND;
2409 }
2410 arg = tline->text;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002411
H. Peter Anvine2c80182005-01-15 22:15:51 +00002412 /* Find the argument size type */
2413 tline = tline->next;
2414 if (!tline || tline->type != TOK_OTHER
2415 || tline->text[0] != ':') {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002416 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00002417 "Syntax error processing `%%arg' directive");
2418 free_tlist(origline);
2419 return DIRECTIVE_FOUND;
2420 }
2421 tline = tline->next;
2422 if (!tline || tline->type != TOK_ID) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002423 nasm_error(ERR_NONFATAL, "`%%arg' missing size type parameter");
H. Peter Anvine2c80182005-01-15 22:15:51 +00002424 free_tlist(origline);
2425 return DIRECTIVE_FOUND;
2426 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002427
H. Peter Anvine2c80182005-01-15 22:15:51 +00002428 /* Allow macro expansion of type parameter */
Keith Kaniosb7a89542007-04-12 02:40:54 +00002429 tt = tokenize(tline->text);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002430 tt = expand_smacro(tt);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002431 size = parse_size(tt->text);
2432 if (!size) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002433 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00002434 "Invalid size type for `%%arg' missing directive");
2435 free_tlist(tt);
2436 free_tlist(origline);
2437 return DIRECTIVE_FOUND;
2438 }
2439 free_tlist(tt);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002440
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002441 /* Round up to even stack slots */
2442 size = ALIGN(size, StackSize);
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002443
H. Peter Anvine2c80182005-01-15 22:15:51 +00002444 /* Now define the macro for the argument */
2445 snprintf(directive, sizeof(directive), "%%define %s (%s+%d)",
2446 arg, StackPointer, offset);
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07002447 do_directive(tokenize(directive), output);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002448 offset += size;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002449
H. Peter Anvine2c80182005-01-15 22:15:51 +00002450 /* Move to the next argument in the list */
2451 tline = tline->next;
2452 if (tline && tline->type == TOK_WHITESPACE)
2453 tline = tline->next;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002454 } while (tline && tline->type == TOK_OTHER && tline->text[0] == ',');
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002455 ArgOffset = offset;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002456 free_tlist(origline);
2457 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002458
H. Peter Anvine2c80182005-01-15 22:15:51 +00002459 case PP_LOCAL:
2460 /* TASM like LOCAL directive to define local variables for a
2461 * function, in the following form:
2462 *
2463 * LOCAL local1:WORD, local2:DWORD, local4:QWORD = LocalSize
2464 *
2465 * The '= LocalSize' at the end is ignored by NASM, but is
2466 * required by TASM to define the local parameter size (and used
2467 * by the TASM macro package).
2468 */
2469 offset = LocalOffset;
2470 do {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00002471 char *local, directive[256];
H. Peter Anvine2c80182005-01-15 22:15:51 +00002472 int size = StackSize;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002473
H. Peter Anvine2c80182005-01-15 22:15:51 +00002474 /* Find the argument name */
2475 tline = tline->next;
2476 if (tline && tline->type == TOK_WHITESPACE)
2477 tline = tline->next;
2478 if (!tline || tline->type != TOK_ID) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002479 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00002480 "`%%local' missing argument parameter");
2481 free_tlist(origline);
2482 return DIRECTIVE_FOUND;
2483 }
2484 local = tline->text;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002485
H. Peter Anvine2c80182005-01-15 22:15:51 +00002486 /* Find the argument size type */
2487 tline = tline->next;
2488 if (!tline || tline->type != TOK_OTHER
2489 || tline->text[0] != ':') {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002490 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00002491 "Syntax error processing `%%local' directive");
2492 free_tlist(origline);
2493 return DIRECTIVE_FOUND;
2494 }
2495 tline = tline->next;
2496 if (!tline || tline->type != TOK_ID) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002497 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00002498 "`%%local' missing size type parameter");
2499 free_tlist(origline);
2500 return DIRECTIVE_FOUND;
2501 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00002502
H. Peter Anvine2c80182005-01-15 22:15:51 +00002503 /* Allow macro expansion of type parameter */
Keith Kaniosb7a89542007-04-12 02:40:54 +00002504 tt = tokenize(tline->text);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002505 tt = expand_smacro(tt);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002506 size = parse_size(tt->text);
2507 if (!size) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002508 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00002509 "Invalid size type for `%%local' missing directive");
2510 free_tlist(tt);
2511 free_tlist(origline);
2512 return DIRECTIVE_FOUND;
2513 }
2514 free_tlist(tt);
H. Peter Anvin734b1882002-04-30 21:01:08 +00002515
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002516 /* Round up to even stack slots */
2517 size = ALIGN(size, StackSize);
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002518
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002519 offset += size; /* Negative offset, increment before */
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002520
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002521 /* Now define the macro for the argument */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002522 snprintf(directive, sizeof(directive), "%%define %s (%s-%d)",
2523 local, StackPointer, offset);
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07002524 do_directive(tokenize(directive), output);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002525
H. Peter Anvine2c80182005-01-15 22:15:51 +00002526 /* Now define the assign to setup the enter_c macro correctly */
2527 snprintf(directive, sizeof(directive),
2528 "%%assign %%$localsize %%$localsize+%d", size);
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07002529 do_directive(tokenize(directive), output);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002530
H. Peter Anvine2c80182005-01-15 22:15:51 +00002531 /* Move to the next argument in the list */
2532 tline = tline->next;
2533 if (tline && tline->type == TOK_WHITESPACE)
2534 tline = tline->next;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002535 } while (tline && tline->type == TOK_OTHER && tline->text[0] == ',');
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002536 LocalOffset = offset;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002537 free_tlist(origline);
2538 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002539
H. Peter Anvine2c80182005-01-15 22:15:51 +00002540 case PP_CLEAR:
2541 if (tline->next)
H. Peter Anvin130736c2016-02-17 20:27:41 -08002542 nasm_error(ERR_WARNING|ERR_PASS1,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002543 "trailing garbage after `%%clear' ignored");
2544 free_macros();
2545 init_macros();
H. Peter Anvine2c80182005-01-15 22:15:51 +00002546 free_tlist(origline);
2547 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002548
H. Peter Anvin418ca702008-05-30 10:42:30 -07002549 case PP_DEPEND:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002550 t = tline->next = expand_smacro(tline->next);
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002551 skip_white_(t);
2552 if (!t || (t->type != TOK_STRING &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002553 t->type != TOK_INTERNAL_STRING)) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002554 nasm_error(ERR_NONFATAL, "`%%depend' expects a file name");
H. Peter Anvin418ca702008-05-30 10:42:30 -07002555 free_tlist(origline);
2556 return DIRECTIVE_FOUND; /* but we did _something_ */
2557 }
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002558 if (t->next)
H. Peter Anvin130736c2016-02-17 20:27:41 -08002559 nasm_error(ERR_WARNING|ERR_PASS1,
H. Peter Anvin418ca702008-05-30 10:42:30 -07002560 "trailing garbage after `%%depend' ignored");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002561 p = t->text;
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002562 if (t->type != TOK_INTERNAL_STRING)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002563 nasm_unquote_cstr(p, i);
Cyrill Gorcunovb7bb5ac2018-11-11 21:33:52 +03002564 strlist_add(deplist, p);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002565 free_tlist(origline);
H. Peter Anvin418ca702008-05-30 10:42:30 -07002566 return DIRECTIVE_FOUND;
2567
2568 case PP_INCLUDE:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002569 t = tline->next = expand_smacro(tline->next);
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002570 skip_white_(t);
H. Peter Anvind2456592008-06-19 15:04:18 -07002571
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002572 if (!t || (t->type != TOK_STRING &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002573 t->type != TOK_INTERNAL_STRING)) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002574 nasm_error(ERR_NONFATAL, "`%%include' expects a file name");
H. Peter Anvine2c80182005-01-15 22:15:51 +00002575 free_tlist(origline);
2576 return DIRECTIVE_FOUND; /* but we did _something_ */
2577 }
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002578 if (t->next)
H. Peter Anvin130736c2016-02-17 20:27:41 -08002579 nasm_error(ERR_WARNING|ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +00002580 "trailing garbage after `%%include' ignored");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002581 p = t->text;
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002582 if (t->type != TOK_INTERNAL_STRING)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002583 nasm_unquote_cstr(p, i);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002584 inc = nasm_malloc(sizeof(Include));
H. Peter Anvine2c80182005-01-15 22:15:51 +00002585 inc->next = istk;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002586 inc->conds = NULL;
Jim Kukunas65a8afc2016-06-13 16:00:42 -04002587 found_path = NULL;
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07002588 inc->fp = inc_fopen(p, deplist, &found_path,
H. Peter Anvind81a2352016-09-21 14:03:18 -07002589 pass == 0 ? INC_OPTIONAL : INC_NEEDED, NF_TEXT);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002590 if (!inc->fp) {
2591 /* -MG given but file not found */
2592 nasm_free(inc);
2593 } else {
Jim Kukunas65a8afc2016-06-13 16:00:42 -04002594 inc->fname = src_set_fname(found_path ? found_path : p);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002595 inc->lineno = src_set_linnum(0);
2596 inc->lineinc = 1;
2597 inc->expansion = NULL;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002598 inc->mstk = NULL;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002599 istk = inc;
H. Peter Anvin8ac25aa2016-02-18 01:16:18 -08002600 lfmt->uplevel(LIST_INCLUDE);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002601 }
2602 free_tlist(origline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002603 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002604
H. Peter Anvind2456592008-06-19 15:04:18 -07002605 case PP_USE:
H. Peter Anvinf4ae5ad2008-06-19 18:39:24 -07002606 {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002607 static macros_t *use_pkg;
2608 const char *pkg_macro = NULL;
H. Peter Anvinf4ae5ad2008-06-19 18:39:24 -07002609
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002610 tline = tline->next;
2611 skip_white_(tline);
2612 tline = expand_id(tline);
H. Peter Anvind2456592008-06-19 15:04:18 -07002613
H. Peter Anvin264b7b92008-10-24 16:38:17 -07002614 if (!tline || (tline->type != TOK_STRING &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002615 tline->type != TOK_INTERNAL_STRING &&
2616 tline->type != TOK_ID)) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002617 nasm_error(ERR_NONFATAL, "`%%use' expects a package name");
H. Peter Anvind2456592008-06-19 15:04:18 -07002618 free_tlist(origline);
2619 return DIRECTIVE_FOUND; /* but we did _something_ */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002620 }
H. Peter Anvin264b7b92008-10-24 16:38:17 -07002621 if (tline->next)
H. Peter Anvin130736c2016-02-17 20:27:41 -08002622 nasm_error(ERR_WARNING|ERR_PASS1,
H. Peter Anvind2456592008-06-19 15:04:18 -07002623 "trailing garbage after `%%use' ignored");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002624 if (tline->type == TOK_STRING)
2625 nasm_unquote_cstr(tline->text, i);
2626 use_pkg = nasm_stdmac_find_package(tline->text);
2627 if (!use_pkg)
H. Peter Anvin130736c2016-02-17 20:27:41 -08002628 nasm_error(ERR_NONFATAL, "unknown `%%use' package: %s", tline->text);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002629 else
2630 pkg_macro = (char *)use_pkg + 1; /* The first string will be <%define>__USE_*__ */
Victor van den Elzen35eb2ea2010-03-10 22:33:48 +01002631 if (use_pkg && ! smacro_defined(NULL, pkg_macro, 0, NULL, true)) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002632 /* Not already included, go ahead and include it */
2633 stdmacpos = use_pkg;
2634 }
2635 free_tlist(origline);
H. Peter Anvind2456592008-06-19 15:04:18 -07002636 return DIRECTIVE_FOUND;
H. Peter Anvinf4ae5ad2008-06-19 18:39:24 -07002637 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002638 case PP_PUSH:
H. Peter Anvine2c80182005-01-15 22:15:51 +00002639 case PP_REPL:
H. Peter Anvin42b56392008-10-24 16:24:21 -07002640 case PP_POP:
H. Peter Anvine2c80182005-01-15 22:15:51 +00002641 tline = tline->next;
2642 skip_white_(tline);
2643 tline = expand_id(tline);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002644 if (tline) {
2645 if (!tok_type_(tline, TOK_ID)) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002646 nasm_error(ERR_NONFATAL, "`%s' expects a context identifier",
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002647 pp_directives[i]);
2648 free_tlist(origline);
2649 return DIRECTIVE_FOUND; /* but we did _something_ */
2650 }
2651 if (tline->next)
H. Peter Anvin130736c2016-02-17 20:27:41 -08002652 nasm_error(ERR_WARNING|ERR_PASS1,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002653 "trailing garbage after `%s' ignored",
2654 pp_directives[i]);
2655 p = nasm_strdup(tline->text);
2656 } else {
2657 p = NULL; /* Anonymous */
2658 }
H. Peter Anvin42b56392008-10-24 16:24:21 -07002659
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002660 if (i == PP_PUSH) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002661 ctx = nasm_malloc(sizeof(Context));
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002662 ctx->next = cstk;
2663 hash_init(&ctx->localmac, HASH_SMALL);
2664 ctx->name = p;
2665 ctx->number = unique++;
2666 cstk = ctx;
2667 } else {
2668 /* %pop or %repl */
2669 if (!cstk) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002670 nasm_error(ERR_NONFATAL, "`%s': context stack is empty",
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002671 pp_directives[i]);
2672 } else if (i == PP_POP) {
2673 if (p && (!cstk->name || nasm_stricmp(p, cstk->name)))
H. Peter Anvin130736c2016-02-17 20:27:41 -08002674 nasm_error(ERR_NONFATAL, "`%%pop' in wrong context: %s, "
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002675 "expected %s",
2676 cstk->name ? cstk->name : "anonymous", p);
2677 else
2678 ctx_pop();
2679 } else {
2680 /* i == PP_REPL */
2681 nasm_free(cstk->name);
2682 cstk->name = p;
2683 p = NULL;
2684 }
2685 nasm_free(p);
2686 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002687 free_tlist(origline);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002688 return DIRECTIVE_FOUND;
H. Peter Anvin8e3f75e2008-09-24 00:21:58 -07002689 case PP_FATAL:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002690 severity = ERR_FATAL;
2691 goto issue_error;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002692 case PP_ERROR:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002693 severity = ERR_NONFATAL;
2694 goto issue_error;
H. Peter Anvin7df04172008-06-10 18:27:38 -07002695 case PP_WARNING:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002696 severity = ERR_WARNING|ERR_WARN_USER;
2697 goto issue_error;
H. Peter Anvin8e3f75e2008-09-24 00:21:58 -07002698
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002699issue_error:
H. Peter Anvin7df04172008-06-10 18:27:38 -07002700 {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002701 /* Only error out if this is the final pass */
2702 if (pass != 2 && i != PP_FATAL)
2703 return DIRECTIVE_FOUND;
2704
2705 tline->next = expand_smacro(tline->next);
2706 tline = tline->next;
2707 skip_white_(tline);
2708 t = tline ? tline->next : NULL;
2709 skip_white_(t);
2710 if (tok_type_(tline, TOK_STRING) && !t) {
2711 /* The line contains only a quoted string */
2712 p = tline->text;
2713 nasm_unquote(p, NULL); /* Ignore NUL character truncation */
H. Peter Anvin130736c2016-02-17 20:27:41 -08002714 nasm_error(severity, "%s", p);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002715 } else {
2716 /* Not a quoted string, or more than a quoted string */
2717 p = detoken(tline, false);
H. Peter Anvin130736c2016-02-17 20:27:41 -08002718 nasm_error(severity, "%s", p);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002719 nasm_free(p);
2720 }
2721 free_tlist(origline);
2722 return DIRECTIVE_FOUND;
H. Peter Anvin7df04172008-06-10 18:27:38 -07002723 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00002724
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002725 CASE_PP_IF:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002726 if (istk->conds && !emitting(istk->conds->state))
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002727 j = COND_NEVER;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002728 else {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002729 j = if_condition(tline->next, i);
2730 tline->next = NULL; /* it got freed */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002731 j = j < 0 ? COND_NEVER : j ? COND_IF_TRUE : COND_IF_FALSE;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002732 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002733 cond = nasm_malloc(sizeof(Cond));
2734 cond->next = istk->conds;
2735 cond->state = j;
2736 istk->conds = cond;
2737 if(istk->mstk)
2738 istk->mstk->condcnt ++;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002739 free_tlist(origline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002740 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002741
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002742 CASE_PP_ELIF:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002743 if (!istk->conds)
H. Peter Anvin130736c2016-02-17 20:27:41 -08002744 nasm_error(ERR_FATAL, "`%s': no matching `%%if'", pp_directives[i]);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002745 switch(istk->conds->state) {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002746 case COND_IF_TRUE:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002747 istk->conds->state = COND_DONE;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002748 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002749
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002750 case COND_DONE:
2751 case COND_NEVER:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002752 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002753
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002754 case COND_ELSE_TRUE:
2755 case COND_ELSE_FALSE:
H. Peter Anvin130736c2016-02-17 20:27:41 -08002756 nasm_error(ERR_WARNING|ERR_PASS1|ERR_PP_PRECOND,
2757 "`%%elif' after `%%else' ignored");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002758 istk->conds->state = COND_NEVER;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002759 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002760
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002761 case COND_IF_FALSE:
2762 /*
2763 * IMPORTANT: In the case of %if, we will already have
2764 * called expand_mmac_params(); however, if we're
2765 * processing an %elif we must have been in a
2766 * non-emitting mode, which would have inhibited
2767 * the normal invocation of expand_mmac_params().
2768 * Therefore, we have to do it explicitly here.
2769 */
2770 j = if_condition(expand_mmac_params(tline->next), i);
2771 tline->next = NULL; /* it got freed */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002772 istk->conds->state =
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002773 j < 0 ? COND_NEVER : j ? COND_IF_TRUE : COND_IF_FALSE;
2774 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002775 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002776 free_tlist(origline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002777 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002778
H. Peter Anvine2c80182005-01-15 22:15:51 +00002779 case PP_ELSE:
2780 if (tline->next)
H. Peter Anvin130736c2016-02-17 20:27:41 -08002781 nasm_error(ERR_WARNING|ERR_PASS1|ERR_PP_PRECOND,
2782 "trailing garbage after `%%else' ignored");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002783 if (!istk->conds)
H. Peter Anvinc5136902018-06-15 18:20:17 -07002784 nasm_fatal("`%%else: no matching `%%if'");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002785 switch(istk->conds->state) {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002786 case COND_IF_TRUE:
2787 case COND_DONE:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002788 istk->conds->state = COND_ELSE_FALSE;
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_NEVER:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002792 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002793
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002794 case COND_IF_FALSE:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002795 istk->conds->state = COND_ELSE_TRUE;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002796 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002797
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002798 case COND_ELSE_TRUE:
2799 case COND_ELSE_FALSE:
H. Peter Anvin130736c2016-02-17 20:27:41 -08002800 nasm_error(ERR_WARNING|ERR_PASS1|ERR_PP_PRECOND,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002801 "`%%else' after `%%else' ignored.");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002802 istk->conds->state = COND_NEVER;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002803 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002804 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002805 free_tlist(origline);
2806 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002807
H. Peter Anvine2c80182005-01-15 22:15:51 +00002808 case PP_ENDIF:
2809 if (tline->next)
H. Peter Anvin130736c2016-02-17 20:27:41 -08002810 nasm_error(ERR_WARNING|ERR_PASS1|ERR_PP_PRECOND,
2811 "trailing garbage after `%%endif' ignored");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002812 if (!istk->conds)
H. Peter Anvin130736c2016-02-17 20:27:41 -08002813 nasm_error(ERR_FATAL, "`%%endif': no matching `%%if'");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002814 cond = istk->conds;
2815 istk->conds = cond->next;
2816 nasm_free(cond);
2817 if(istk->mstk)
2818 istk->mstk->condcnt --;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002819 free_tlist(origline);
2820 return DIRECTIVE_FOUND;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002821
H. Peter Anvindb8f96e2009-07-15 09:07:29 -04002822 case PP_RMACRO:
2823 case PP_IRMACRO:
H. Peter Anvine2c80182005-01-15 22:15:51 +00002824 case PP_MACRO:
2825 case PP_IMACRO:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002826 if (defining) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002827 nasm_error(ERR_FATAL, "`%s': already defining a macro",
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002828 pp_directives[i]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002829 return DIRECTIVE_FOUND;
2830 }
H. Peter Anvin4def1a82016-05-09 13:59:44 -07002831 defining = nasm_zalloc(sizeof(MMacro));
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07002832 defining->max_depth = ((i == PP_RMACRO) || (i == PP_IRMACRO))
2833 ? nasm_limit[LIMIT_MACROS] : 0;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002834 defining->casesense = (i == PP_MACRO) || (i == PP_RMACRO);
2835 if (!parse_mmacro_spec(tline, defining, pp_directives[i])) {
2836 nasm_free(defining);
2837 defining = NULL;
2838 return DIRECTIVE_FOUND;
2839 }
H. Peter Anvina26433d2008-07-16 14:40:01 -07002840
H. Peter Anvin4def1a82016-05-09 13:59:44 -07002841 src_get(&defining->xline, &defining->fname);
2842
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002843 mmac = (MMacro *) hash_findix(&mmacros, defining->name);
2844 while (mmac) {
2845 if (!strcmp(mmac->name, defining->name) &&
2846 (mmac->nparam_min <= defining->nparam_max
2847 || defining->plus)
2848 && (defining->nparam_min <= mmac->nparam_max
2849 || mmac->plus)) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002850 nasm_error(ERR_WARNING|ERR_PASS1,
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002851 "redefining multi-line macro `%s'", defining->name);
2852 return DIRECTIVE_FOUND;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002853 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002854 mmac = mmac->next;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002855 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002856 free_tlist(origline);
2857 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002858
H. Peter Anvine2c80182005-01-15 22:15:51 +00002859 case PP_ENDM:
2860 case PP_ENDMACRO:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002861 if (! (defining && defining->name)) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002862 nasm_error(ERR_NONFATAL, "`%s': not defining a macro", tline->text);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002863 return DIRECTIVE_FOUND;
2864 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002865 mmhead = (MMacro **) hash_findi_add(&mmacros, defining->name);
2866 defining->next = *mmhead;
2867 *mmhead = defining;
2868 defining = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002869 free_tlist(origline);
2870 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002871
H. Peter Anvin89cee572009-07-15 09:16:54 -04002872 case PP_EXITMACRO:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002873 /*
2874 * We must search along istk->expansion until we hit a
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002875 * macro-end marker for a macro with a name. Then we
2876 * bypass all lines between exitmacro and endmacro.
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002877 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002878 list_for_each(l, istk->expansion)
2879 if (l->finishes && l->finishes->name)
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002880 break;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002881
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002882 if (l) {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002883 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002884 * Remove all conditional entries relative to this
2885 * macro invocation. (safe to do in this context)
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002886 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002887 for ( ; l->finishes->condcnt > 0; l->finishes->condcnt --) {
2888 cond = istk->conds;
2889 istk->conds = cond->next;
2890 nasm_free(cond);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002891 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002892 istk->expansion = l;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002893 } else {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002894 nasm_error(ERR_NONFATAL, "`%%exitmacro' not within `%%macro' block");
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002895 }
Keith Kanios852f1ee2009-07-12 00:19:55 -05002896 free_tlist(origline);
2897 return DIRECTIVE_FOUND;
2898
H. Peter Anvina26433d2008-07-16 14:40:01 -07002899 case PP_UNMACRO:
2900 case PP_UNIMACRO:
2901 {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002902 MMacro **mmac_p;
2903 MMacro spec;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002904
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002905 spec.casesense = (i == PP_UNMACRO);
2906 if (!parse_mmacro_spec(tline, &spec, pp_directives[i])) {
2907 return DIRECTIVE_FOUND;
2908 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002909 mmac_p = (MMacro **) hash_findi(&mmacros, spec.name, NULL);
2910 while (mmac_p && *mmac_p) {
2911 mmac = *mmac_p;
2912 if (mmac->casesense == spec.casesense &&
2913 !mstrcmp(mmac->name, spec.name, spec.casesense) &&
2914 mmac->nparam_min == spec.nparam_min &&
2915 mmac->nparam_max == spec.nparam_max &&
2916 mmac->plus == spec.plus) {
2917 *mmac_p = mmac->next;
2918 free_mmacro(mmac);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002919 } else {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002920 mmac_p = &mmac->next;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002921 }
2922 }
2923 free_tlist(origline);
2924 free_tlist(spec.dlist);
2925 return DIRECTIVE_FOUND;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002926 }
2927
H. Peter Anvine2c80182005-01-15 22:15:51 +00002928 case PP_ROTATE:
2929 if (tline->next && tline->next->type == TOK_WHITESPACE)
2930 tline = tline->next;
H. Peter Anvin89cee572009-07-15 09:16:54 -04002931 if (!tline->next) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002932 free_tlist(origline);
H. Peter Anvin130736c2016-02-17 20:27:41 -08002933 nasm_error(ERR_NONFATAL, "`%%rotate' missing rotate count");
H. Peter Anvine2c80182005-01-15 22:15:51 +00002934 return DIRECTIVE_FOUND;
2935 }
2936 t = expand_smacro(tline->next);
2937 tline->next = NULL;
2938 free_tlist(origline);
2939 tline = t;
2940 tptr = &t;
2941 tokval.t_type = TOKEN_INVALID;
2942 evalresult =
H. Peter Anvin130736c2016-02-17 20:27:41 -08002943 evaluate(ppscan, tptr, &tokval, NULL, pass, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002944 free_tlist(tline);
2945 if (!evalresult)
2946 return DIRECTIVE_FOUND;
2947 if (tokval.t_type)
H. Peter Anvin130736c2016-02-17 20:27:41 -08002948 nasm_error(ERR_WARNING|ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +00002949 "trailing garbage after expression ignored");
2950 if (!is_simple(evalresult)) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002951 nasm_error(ERR_NONFATAL, "non-constant value given to `%%rotate'");
H. Peter Anvine2c80182005-01-15 22:15:51 +00002952 return DIRECTIVE_FOUND;
2953 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002954 mmac = istk->mstk;
2955 while (mmac && !mmac->name) /* avoid mistaking %reps for macros */
2956 mmac = mmac->next_active;
2957 if (!mmac) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002958 nasm_error(ERR_NONFATAL, "`%%rotate' invoked outside a macro call");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002959 } else if (mmac->nparam == 0) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002960 nasm_error(ERR_NONFATAL,
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002961 "`%%rotate' invoked within macro without parameters");
2962 } else {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002963 int rotate = mmac->rotate + reloc_value(evalresult);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002964
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002965 rotate %= (int)mmac->nparam;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002966 if (rotate < 0)
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002967 rotate += mmac->nparam;
2968
2969 mmac->rotate = rotate;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002970 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002971 return DIRECTIVE_FOUND;
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00002972
H. Peter Anvine2c80182005-01-15 22:15:51 +00002973 case PP_REP:
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002974 nolist = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002975 do {
2976 tline = tline->next;
2977 } while (tok_type_(tline, TOK_WHITESPACE));
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00002978
H. Peter Anvine2c80182005-01-15 22:15:51 +00002979 if (tok_type_(tline, TOK_ID) &&
2980 nasm_stricmp(tline->text, ".nolist") == 0) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002981 nolist = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002982 do {
2983 tline = tline->next;
2984 } while (tok_type_(tline, TOK_WHITESPACE));
2985 }
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00002986
H. Peter Anvine2c80182005-01-15 22:15:51 +00002987 if (tline) {
2988 t = expand_smacro(tline);
2989 tptr = &t;
2990 tokval.t_type = TOKEN_INVALID;
2991 evalresult =
H. Peter Anvin130736c2016-02-17 20:27:41 -08002992 evaluate(ppscan, tptr, &tokval, NULL, pass, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002993 if (!evalresult) {
2994 free_tlist(origline);
2995 return DIRECTIVE_FOUND;
2996 }
2997 if (tokval.t_type)
H. Peter Anvin130736c2016-02-17 20:27:41 -08002998 nasm_error(ERR_WARNING|ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +00002999 "trailing garbage after expression ignored");
3000 if (!is_simple(evalresult)) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08003001 nasm_error(ERR_NONFATAL, "non-constant value given to `%%rep'");
H. Peter Anvine2c80182005-01-15 22:15:51 +00003002 return DIRECTIVE_FOUND;
3003 }
Cyrill Gorcunove091d6e2010-08-09 13:58:22 +04003004 count = reloc_value(evalresult);
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07003005 if (count > nasm_limit[LIMIT_REP]) {
3006 nasm_error(ERR_NONFATAL,
H. Peter Anvina3d96d02018-06-15 17:51:39 -07003007 "`%%rep' count %"PRId64" exceeds limit (currently %"PRId64")",
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07003008 count, nasm_limit[LIMIT_REP]);
Cyrill Gorcunove091d6e2010-08-09 13:58:22 +04003009 count = 0;
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07003010 } else if (count < 0) {
3011 nasm_error(ERR_WARNING|ERR_PASS2|ERR_WARN_NEG_REP,
3012 "negative `%%rep' count: %"PRId64, count);
3013 count = 0;
3014 } else {
Cyrill Gorcunove091d6e2010-08-09 13:58:22 +04003015 count++;
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07003016 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00003017 } else {
H. Peter Anvin130736c2016-02-17 20:27:41 -08003018 nasm_error(ERR_NONFATAL, "`%%rep' expects a repeat count");
H. Peter Anvinf8ba53e2007-10-11 10:11:57 -07003019 count = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003020 }
3021 free_tlist(origline);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003022
3023 tmp_defining = defining;
3024 defining = nasm_malloc(sizeof(MMacro));
3025 defining->prev = NULL;
3026 defining->name = NULL; /* flags this macro as a %rep block */
3027 defining->casesense = false;
3028 defining->plus = false;
3029 defining->nolist = nolist;
3030 defining->in_progress = count;
3031 defining->max_depth = 0;
3032 defining->nparam_min = defining->nparam_max = 0;
3033 defining->defaults = NULL;
3034 defining->dlist = NULL;
3035 defining->expansion = NULL;
3036 defining->next_active = istk->mstk;
3037 defining->rep_nest = tmp_defining;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003038 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003039
H. Peter Anvine2c80182005-01-15 22:15:51 +00003040 case PP_ENDREP:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003041 if (!defining || defining->name) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08003042 nasm_error(ERR_NONFATAL, "`%%endrep': no matching `%%rep'");
H. Peter Anvine2c80182005-01-15 22:15:51 +00003043 return DIRECTIVE_FOUND;
3044 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003045
H. Peter Anvine2c80182005-01-15 22:15:51 +00003046 /*
3047 * Now we have a "macro" defined - although it has no name
3048 * and we won't be entering it in the hash tables - we must
3049 * push a macro-end marker for it on to istk->expansion.
3050 * After that, it will take care of propagating itself (a
3051 * macro-end marker line for a macro which is really a %rep
3052 * block will cause the macro to be re-expanded, complete
3053 * with another macro-end marker to ensure the process
3054 * continues) until the whole expansion is forcibly removed
3055 * from istk->expansion by a %exitrep.
3056 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003057 l = nasm_malloc(sizeof(Line));
3058 l->next = istk->expansion;
3059 l->finishes = defining;
3060 l->first = NULL;
3061 istk->expansion = l;
3062
3063 istk->mstk = defining;
3064
H. Peter Anvin8ac25aa2016-02-18 01:16:18 -08003065 lfmt->uplevel(defining->nolist ? LIST_MACRO_NOLIST : LIST_MACRO);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003066 tmp_defining = defining;
3067 defining = defining->rep_nest;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003068 free_tlist(origline);
3069 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003070
H. Peter Anvine2c80182005-01-15 22:15:51 +00003071 case PP_EXITREP:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003072 /*
3073 * We must search along istk->expansion until we hit a
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003074 * macro-end marker for a macro with no name. Then we set
3075 * its `in_progress' flag to 0.
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003076 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003077 list_for_each(l, istk->expansion)
3078 if (l->finishes && !l->finishes->name)
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003079 break;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003080
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003081 if (l)
3082 l->finishes->in_progress = 1;
3083 else
H. Peter Anvin130736c2016-02-17 20:27:41 -08003084 nasm_error(ERR_NONFATAL, "`%%exitrep' not within `%%rep' block");
H. Peter Anvine2c80182005-01-15 22:15:51 +00003085 free_tlist(origline);
3086 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003087
H. Peter Anvine2c80182005-01-15 22:15:51 +00003088 case PP_XDEFINE:
3089 case PP_IXDEFINE:
3090 case PP_DEFINE:
3091 case PP_IDEFINE:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003092 casesense = (i == PP_DEFINE || i == PP_XDEFINE);
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07003093
H. Peter Anvine2c80182005-01-15 22:15:51 +00003094 tline = tline->next;
3095 skip_white_(tline);
3096 tline = expand_id(tline);
3097 if (!tline || (tline->type != TOK_ID &&
3098 (tline->type != TOK_PREPROC_ID ||
3099 tline->text[1] != '$'))) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08003100 nasm_error(ERR_NONFATAL, "`%s' expects a macro identifier",
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003101 pp_directives[i]);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003102 free_tlist(origline);
3103 return DIRECTIVE_FOUND;
3104 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003105
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04003106 ctx = get_ctx(tline->text, &mname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003107 last = tline;
3108 param_start = tline = tline->next;
3109 nparam = 0;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003110
H. Peter Anvine2c80182005-01-15 22:15:51 +00003111 /* Expand the macro definition now for %xdefine and %ixdefine */
3112 if ((i == PP_XDEFINE) || (i == PP_IXDEFINE))
3113 tline = expand_smacro(tline);
H. Peter Anvin734b1882002-04-30 21:01:08 +00003114
H. Peter Anvine2c80182005-01-15 22:15:51 +00003115 if (tok_is_(tline, "(")) {
3116 /*
3117 * This macro has parameters.
3118 */
H. Peter Anvin734b1882002-04-30 21:01:08 +00003119
H. Peter Anvine2c80182005-01-15 22:15:51 +00003120 tline = tline->next;
3121 while (1) {
3122 skip_white_(tline);
3123 if (!tline) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08003124 nasm_error(ERR_NONFATAL, "parameter identifier expected");
H. Peter Anvine2c80182005-01-15 22:15:51 +00003125 free_tlist(origline);
3126 return DIRECTIVE_FOUND;
3127 }
3128 if (tline->type != TOK_ID) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08003129 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00003130 "`%s': parameter identifier expected",
3131 tline->text);
3132 free_tlist(origline);
3133 return DIRECTIVE_FOUND;
3134 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003135 tline->type = TOK_SMAC_PARAM + nparam++;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003136 tline = tline->next;
3137 skip_white_(tline);
3138 if (tok_is_(tline, ",")) {
3139 tline = tline->next;
H. Peter Anvinca348b62008-07-23 10:49:26 -04003140 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003141 if (!tok_is_(tline, ")")) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08003142 nasm_error(ERR_NONFATAL,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003143 "`)' expected to terminate macro template");
3144 free_tlist(origline);
3145 return DIRECTIVE_FOUND;
3146 }
3147 break;
3148 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00003149 }
3150 last = tline;
3151 tline = tline->next;
3152 }
3153 if (tok_type_(tline, TOK_WHITESPACE))
3154 last = tline, tline = tline->next;
3155 macro_start = NULL;
3156 last->next = NULL;
3157 t = tline;
3158 while (t) {
3159 if (t->type == TOK_ID) {
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04003160 list_for_each(tt, param_start)
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003161 if (tt->type >= TOK_SMAC_PARAM &&
H. Peter Anvine2c80182005-01-15 22:15:51 +00003162 !strcmp(tt->text, t->text))
3163 t->type = tt->type;
3164 }
3165 tt = t->next;
3166 t->next = macro_start;
3167 macro_start = t;
3168 t = tt;
3169 }
3170 /*
3171 * Good. We now have a macro name, a parameter count, and a
3172 * token list (in reverse order) for an expansion. We ought
3173 * to be OK just to create an SMacro, store it, and let
3174 * free_tlist have the rest of the line (which we have
3175 * carefully re-terminated after chopping off the expansion
3176 * from the end).
3177 */
H. Peter Anvin4db5a162007-10-11 13:42:09 -07003178 define_smacro(ctx, mname, casesense, nparam, macro_start);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003179 free_tlist(origline);
3180 return DIRECTIVE_FOUND;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003181
H. Peter Anvine2c80182005-01-15 22:15:51 +00003182 case PP_UNDEF:
3183 tline = tline->next;
3184 skip_white_(tline);
3185 tline = expand_id(tline);
3186 if (!tline || (tline->type != TOK_ID &&
3187 (tline->type != TOK_PREPROC_ID ||
3188 tline->text[1] != '$'))) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08003189 nasm_error(ERR_NONFATAL, "`%%undef' expects a macro identifier");
H. Peter Anvine2c80182005-01-15 22:15:51 +00003190 free_tlist(origline);
3191 return DIRECTIVE_FOUND;
3192 }
3193 if (tline->next) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08003194 nasm_error(ERR_WARNING|ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +00003195 "trailing garbage after macro name ignored");
3196 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003197
H. Peter Anvine2c80182005-01-15 22:15:51 +00003198 /* Find the context that symbol belongs to */
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04003199 ctx = get_ctx(tline->text, &mname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003200 undef_smacro(ctx, mname);
3201 free_tlist(origline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003202 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003203
H. Peter Anvin9e200162008-06-04 17:23:14 -07003204 case PP_DEFSTR:
3205 case PP_IDEFSTR:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003206 casesense = (i == PP_DEFSTR);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003207
3208 tline = tline->next;
3209 skip_white_(tline);
3210 tline = expand_id(tline);
3211 if (!tline || (tline->type != TOK_ID &&
3212 (tline->type != TOK_PREPROC_ID ||
3213 tline->text[1] != '$'))) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08003214 nasm_error(ERR_NONFATAL, "`%s' expects a macro identifier",
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003215 pp_directives[i]);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003216 free_tlist(origline);
3217 return DIRECTIVE_FOUND;
3218 }
3219
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04003220 ctx = get_ctx(tline->text, &mname);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003221 last = tline;
3222 tline = expand_smacro(tline->next);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003223 last->next = NULL;
H. Peter Anvin9e200162008-06-04 17:23:14 -07003224
3225 while (tok_type_(tline, TOK_WHITESPACE))
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003226 tline = delete_Token(tline);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003227
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003228 p = detoken(tline, false);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003229 macro_start = nasm_malloc(sizeof(*macro_start));
3230 macro_start->next = NULL;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003231 macro_start->text = nasm_quote(p, strlen(p));
3232 macro_start->type = TOK_STRING;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003233 macro_start->a.mac = NULL;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003234 nasm_free(p);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003235
3236 /*
3237 * We now have a macro name, an implicit parameter count of
3238 * zero, and a string token to use as an expansion. Create
3239 * and store an SMacro.
3240 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003241 define_smacro(ctx, mname, casesense, 0, macro_start);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003242 free_tlist(origline);
3243 return DIRECTIVE_FOUND;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003244
H. Peter Anvin2f55bda2009-07-14 15:04:04 -04003245 case PP_DEFTOK:
3246 case PP_IDEFTOK:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003247 casesense = (i == PP_DEFTOK);
3248
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003249 tline = tline->next;
3250 skip_white_(tline);
3251 tline = expand_id(tline);
3252 if (!tline || (tline->type != TOK_ID &&
3253 (tline->type != TOK_PREPROC_ID ||
3254 tline->text[1] != '$'))) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08003255 nasm_error(ERR_NONFATAL,
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003256 "`%s' expects a macro identifier as first parameter",
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003257 pp_directives[i]);
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003258 free_tlist(origline);
3259 return DIRECTIVE_FOUND;
3260 }
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04003261 ctx = get_ctx(tline->text, &mname);
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003262 last = tline;
3263 tline = expand_smacro(tline->next);
3264 last->next = NULL;
3265
3266 t = tline;
3267 while (tok_type_(t, TOK_WHITESPACE))
3268 t = t->next;
3269 /* t should now point to the string */
Cyrill Gorcunov6908e582010-09-06 19:36:15 +04003270 if (!tok_type_(t, TOK_STRING)) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08003271 nasm_error(ERR_NONFATAL,
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003272 "`%s` requires string as second parameter",
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003273 pp_directives[i]);
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003274 free_tlist(tline);
3275 free_tlist(origline);
3276 return DIRECTIVE_FOUND;
3277 }
3278
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04003279 /*
3280 * Convert the string to a token stream. Note that smacros
3281 * are stored with the token stream reversed, so we have to
3282 * reverse the output of tokenize().
3283 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003284 nasm_unquote_cstr(t->text, i);
H. Peter Anvinb40992c2010-09-15 08:57:21 -07003285 macro_start = reverse_tokens(tokenize(t->text));
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003286
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003287 /*
3288 * We now have a macro name, an implicit parameter count of
3289 * zero, and a numeric token to use as an expansion. Create
3290 * and store an SMacro.
3291 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003292 define_smacro(ctx, mname, casesense, 0, macro_start);
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003293 free_tlist(tline);
3294 free_tlist(origline);
3295 return DIRECTIVE_FOUND;
H. Peter Anvin9e200162008-06-04 17:23:14 -07003296
H. Peter Anvin418ca702008-05-30 10:42:30 -07003297 case PP_PATHSEARCH:
3298 {
H. Peter Anvinccad6f92016-10-04 00:34:35 -07003299 const char *found_path;
H. Peter Anvin418ca702008-05-30 10:42:30 -07003300
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003301 casesense = true;
H. Peter Anvin418ca702008-05-30 10:42:30 -07003302
3303 tline = tline->next;
3304 skip_white_(tline);
3305 tline = expand_id(tline);
3306 if (!tline || (tline->type != TOK_ID &&
3307 (tline->type != TOK_PREPROC_ID ||
3308 tline->text[1] != '$'))) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08003309 nasm_error(ERR_NONFATAL,
H. Peter Anvin418ca702008-05-30 10:42:30 -07003310 "`%%pathsearch' expects a macro identifier as first parameter");
3311 free_tlist(origline);
3312 return DIRECTIVE_FOUND;
3313 }
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04003314 ctx = get_ctx(tline->text, &mname);
H. Peter Anvin418ca702008-05-30 10:42:30 -07003315 last = tline;
3316 tline = expand_smacro(tline->next);
3317 last->next = NULL;
3318
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003319 t = tline;
H. Peter Anvin418ca702008-05-30 10:42:30 -07003320 while (tok_type_(t, TOK_WHITESPACE))
3321 t = t->next;
3322
3323 if (!t || (t->type != TOK_STRING &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003324 t->type != TOK_INTERNAL_STRING)) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08003325 nasm_error(ERR_NONFATAL, "`%%pathsearch' expects a file name");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003326 free_tlist(tline);
H. Peter Anvin418ca702008-05-30 10:42:30 -07003327 free_tlist(origline);
3328 return DIRECTIVE_FOUND; /* but we did _something_ */
3329 }
3330 if (t->next)
H. Peter Anvin130736c2016-02-17 20:27:41 -08003331 nasm_error(ERR_WARNING|ERR_PASS1,
H. Peter Anvin418ca702008-05-30 10:42:30 -07003332 "trailing garbage after `%%pathsearch' ignored");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003333 p = t->text;
H. Peter Anvin427cc912008-06-01 21:43:03 -07003334 if (t->type != TOK_INTERNAL_STRING)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003335 nasm_unquote(p, NULL);
H. Peter Anvin418ca702008-05-30 10:42:30 -07003336
H. Peter Anvin9924d1e2016-10-04 00:59:39 -07003337 inc_fopen(p, NULL, &found_path, INC_PROBE, NF_BINARY);
H. Peter Anvinccad6f92016-10-04 00:34:35 -07003338 if (!found_path)
3339 found_path = p;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003340 macro_start = nasm_malloc(sizeof(*macro_start));
3341 macro_start->next = NULL;
H. Peter Anvinccad6f92016-10-04 00:34:35 -07003342 macro_start->text = nasm_quote(found_path, strlen(found_path));
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003343 macro_start->type = TOK_STRING;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003344 macro_start->a.mac = NULL;
H. Peter Anvin418ca702008-05-30 10:42:30 -07003345
3346 /*
3347 * We now have a macro name, an implicit parameter count of
3348 * zero, and a string token to use as an expansion. Create
3349 * and store an SMacro.
3350 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003351 define_smacro(ctx, mname, casesense, 0, macro_start);
3352 free_tlist(tline);
H. Peter Anvin418ca702008-05-30 10:42:30 -07003353 free_tlist(origline);
3354 return DIRECTIVE_FOUND;
3355 }
3356
H. Peter Anvine2c80182005-01-15 22:15:51 +00003357 case PP_STRLEN:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003358 casesense = true;
H. Peter Anvin70653092007-10-19 14:42:29 -07003359
H. Peter Anvine2c80182005-01-15 22:15:51 +00003360 tline = tline->next;
3361 skip_white_(tline);
3362 tline = expand_id(tline);
3363 if (!tline || (tline->type != TOK_ID &&
3364 (tline->type != TOK_PREPROC_ID ||
3365 tline->text[1] != '$'))) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08003366 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00003367 "`%%strlen' expects a macro identifier as first parameter");
3368 free_tlist(origline);
3369 return DIRECTIVE_FOUND;
3370 }
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04003371 ctx = get_ctx(tline->text, &mname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003372 last = tline;
3373 tline = expand_smacro(tline->next);
3374 last->next = NULL;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003375
H. Peter Anvine2c80182005-01-15 22:15:51 +00003376 t = tline;
3377 while (tok_type_(t, TOK_WHITESPACE))
3378 t = t->next;
3379 /* t should now point to the string */
Cyrill Gorcunov4e1d5ab2010-07-23 18:51:51 +04003380 if (!tok_type_(t, TOK_STRING)) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08003381 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00003382 "`%%strlen` requires string as second parameter");
3383 free_tlist(tline);
3384 free_tlist(origline);
3385 return DIRECTIVE_FOUND;
3386 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003387
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003388 macro_start = nasm_malloc(sizeof(*macro_start));
3389 macro_start->next = NULL;
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07003390 make_tok_num(macro_start, nasm_unquote(t->text, NULL));
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003391 macro_start->a.mac = NULL;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003392
H. Peter Anvine2c80182005-01-15 22:15:51 +00003393 /*
3394 * We now have a macro name, an implicit parameter count of
3395 * zero, and a numeric token to use as an expansion. Create
3396 * and store an SMacro.
3397 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003398 define_smacro(ctx, mname, casesense, 0, macro_start);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003399 free_tlist(tline);
3400 free_tlist(origline);
3401 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003402
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003403 case PP_STRCAT:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003404 casesense = true;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003405
3406 tline = tline->next;
3407 skip_white_(tline);
3408 tline = expand_id(tline);
3409 if (!tline || (tline->type != TOK_ID &&
3410 (tline->type != TOK_PREPROC_ID ||
3411 tline->text[1] != '$'))) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08003412 nasm_error(ERR_NONFATAL,
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003413 "`%%strcat' expects a macro identifier as first parameter");
3414 free_tlist(origline);
3415 return DIRECTIVE_FOUND;
3416 }
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04003417 ctx = get_ctx(tline->text, &mname);
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003418 last = tline;
3419 tline = expand_smacro(tline->next);
3420 last->next = NULL;
3421
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003422 len = 0;
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04003423 list_for_each(t, tline) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003424 switch (t->type) {
3425 case TOK_WHITESPACE:
3426 break;
3427 case TOK_STRING:
3428 len += t->a.len = nasm_unquote(t->text, NULL);
3429 break;
3430 case TOK_OTHER:
3431 if (!strcmp(t->text, ",")) /* permit comma separators */
3432 break;
3433 /* else fall through */
3434 default:
H. Peter Anvin130736c2016-02-17 20:27:41 -08003435 nasm_error(ERR_NONFATAL,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003436 "non-string passed to `%%strcat' (%d)", t->type);
3437 free_tlist(tline);
3438 free_tlist(origline);
3439 return DIRECTIVE_FOUND;
3440 }
3441 }
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003442
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003443 p = pp = nasm_malloc(len);
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04003444 list_for_each(t, tline) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003445 if (t->type == TOK_STRING) {
3446 memcpy(p, t->text, t->a.len);
3447 p += t->a.len;
3448 }
3449 }
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003450
3451 /*
3452 * We now have a macro name, an implicit parameter count of
3453 * zero, and a numeric token to use as an expansion. Create
3454 * and store an SMacro.
3455 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003456 macro_start = new_Token(NULL, TOK_STRING, NULL, 0);
3457 macro_start->text = nasm_quote(pp, len);
3458 nasm_free(pp);
3459 define_smacro(ctx, mname, casesense, 0, macro_start);
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003460 free_tlist(tline);
3461 free_tlist(origline);
3462 return DIRECTIVE_FOUND;
3463
H. Peter Anvine2c80182005-01-15 22:15:51 +00003464 case PP_SUBSTR:
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003465 {
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003466 int64_t start, count;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003467 size_t len;
H. Peter Anvind2456592008-06-19 15:04:18 -07003468
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003469 casesense = true;
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07003470
H. Peter Anvine2c80182005-01-15 22:15:51 +00003471 tline = tline->next;
3472 skip_white_(tline);
3473 tline = expand_id(tline);
3474 if (!tline || (tline->type != TOK_ID &&
3475 (tline->type != TOK_PREPROC_ID ||
3476 tline->text[1] != '$'))) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08003477 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00003478 "`%%substr' expects a macro identifier as first parameter");
3479 free_tlist(origline);
3480 return DIRECTIVE_FOUND;
3481 }
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04003482 ctx = get_ctx(tline->text, &mname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003483 last = tline;
3484 tline = expand_smacro(tline->next);
3485 last->next = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003486
Cyrill Gorcunov35519d62010-09-06 23:49:52 +04003487 if (tline) /* skip expanded id */
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003488 t = tline->next;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003489 while (tok_type_(t, TOK_WHITESPACE))
3490 t = t->next;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003491
H. Peter Anvine2c80182005-01-15 22:15:51 +00003492 /* t should now point to the string */
Cyrill Gorcunov35519d62010-09-06 23:49:52 +04003493 if (!tok_type_(t, TOK_STRING)) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08003494 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00003495 "`%%substr` requires string as second parameter");
3496 free_tlist(tline);
3497 free_tlist(origline);
3498 return DIRECTIVE_FOUND;
3499 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003500
H. Peter Anvine2c80182005-01-15 22:15:51 +00003501 tt = t->next;
3502 tptr = &tt;
3503 tokval.t_type = TOKEN_INVALID;
H. Peter Anvin130736c2016-02-17 20:27:41 -08003504 evalresult = evaluate(ppscan, tptr, &tokval, NULL, pass, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003505 if (!evalresult) {
3506 free_tlist(tline);
3507 free_tlist(origline);
3508 return DIRECTIVE_FOUND;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003509 } else if (!is_simple(evalresult)) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08003510 nasm_error(ERR_NONFATAL, "non-constant value given to `%%substr`");
H. Peter Anvine2c80182005-01-15 22:15:51 +00003511 free_tlist(tline);
3512 free_tlist(origline);
3513 return DIRECTIVE_FOUND;
3514 }
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003515 start = evalresult->value - 1;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003516
3517 while (tok_type_(tt, TOK_WHITESPACE))
3518 tt = tt->next;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003519 if (!tt) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003520 count = 1; /* Backwards compatibility: one character */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003521 } else {
3522 tokval.t_type = TOKEN_INVALID;
H. Peter Anvin130736c2016-02-17 20:27:41 -08003523 evalresult = evaluate(ppscan, tptr, &tokval, NULL, pass, NULL);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003524 if (!evalresult) {
3525 free_tlist(tline);
3526 free_tlist(origline);
3527 return DIRECTIVE_FOUND;
3528 } else if (!is_simple(evalresult)) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08003529 nasm_error(ERR_NONFATAL, "non-constant value given to `%%substr`");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003530 free_tlist(tline);
3531 free_tlist(origline);
3532 return DIRECTIVE_FOUND;
3533 }
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003534 count = evalresult->value;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003535 }
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003536
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003537 len = nasm_unquote(t->text, NULL);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003538
Cyrill Gorcunovcff031e2010-09-07 20:31:11 +04003539 /* make start and count being in range */
3540 if (start < 0)
3541 start = 0;
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003542 if (count < 0)
3543 count = len + count + 1 - start;
3544 if (start + count > (int64_t)len)
Cyrill Gorcunovcff031e2010-09-07 20:31:11 +04003545 count = len - start;
3546 if (!len || count < 0 || start >=(int64_t)len)
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003547 start = -1, count = 0; /* empty string */
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003548
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003549 macro_start = nasm_malloc(sizeof(*macro_start));
3550 macro_start->next = NULL;
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003551 macro_start->text = nasm_quote((start < 0) ? "" : t->text + start, count);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003552 macro_start->type = TOK_STRING;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003553 macro_start->a.mac = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003554
H. Peter Anvine2c80182005-01-15 22:15:51 +00003555 /*
3556 * We now have a macro name, an implicit parameter count of
3557 * zero, and a numeric token to use as an expansion. Create
3558 * and store an SMacro.
3559 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003560 define_smacro(ctx, mname, casesense, 0, macro_start);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003561 free_tlist(tline);
3562 free_tlist(origline);
3563 return DIRECTIVE_FOUND;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003564 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003565
H. Peter Anvine2c80182005-01-15 22:15:51 +00003566 case PP_ASSIGN:
3567 case PP_IASSIGN:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003568 casesense = (i == PP_ASSIGN);
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07003569
H. Peter Anvine2c80182005-01-15 22:15:51 +00003570 tline = tline->next;
3571 skip_white_(tline);
3572 tline = expand_id(tline);
3573 if (!tline || (tline->type != TOK_ID &&
3574 (tline->type != TOK_PREPROC_ID ||
3575 tline->text[1] != '$'))) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08003576 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00003577 "`%%%sassign' expects a macro identifier",
3578 (i == PP_IASSIGN ? "i" : ""));
3579 free_tlist(origline);
3580 return DIRECTIVE_FOUND;
3581 }
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04003582 ctx = get_ctx(tline->text, &mname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003583 last = tline;
3584 tline = expand_smacro(tline->next);
3585 last->next = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003586
H. Peter Anvine2c80182005-01-15 22:15:51 +00003587 t = tline;
3588 tptr = &t;
3589 tokval.t_type = TOKEN_INVALID;
H. Peter Anvin130736c2016-02-17 20:27:41 -08003590 evalresult = evaluate(ppscan, tptr, &tokval, NULL, pass, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003591 free_tlist(tline);
3592 if (!evalresult) {
3593 free_tlist(origline);
3594 return DIRECTIVE_FOUND;
3595 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003596
H. Peter Anvine2c80182005-01-15 22:15:51 +00003597 if (tokval.t_type)
H. Peter Anvin130736c2016-02-17 20:27:41 -08003598 nasm_error(ERR_WARNING|ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +00003599 "trailing garbage after expression ignored");
H. Peter Anvin734b1882002-04-30 21:01:08 +00003600
H. Peter Anvine2c80182005-01-15 22:15:51 +00003601 if (!is_simple(evalresult)) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08003602 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00003603 "non-constant value given to `%%%sassign'",
3604 (i == PP_IASSIGN ? "i" : ""));
3605 free_tlist(origline);
3606 return DIRECTIVE_FOUND;
3607 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003608
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003609 macro_start = nasm_malloc(sizeof(*macro_start));
3610 macro_start->next = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003611 make_tok_num(macro_start, reloc_value(evalresult));
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003612 macro_start->a.mac = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003613
H. Peter Anvine2c80182005-01-15 22:15:51 +00003614 /*
3615 * We now have a macro name, an implicit parameter count of
3616 * zero, and a numeric token to use as an expansion. Create
3617 * and store an SMacro.
3618 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003619 define_smacro(ctx, mname, casesense, 0, macro_start);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003620 free_tlist(origline);
3621 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003622
H. Peter Anvine2c80182005-01-15 22:15:51 +00003623 case PP_LINE:
3624 /*
3625 * Syntax is `%line nnn[+mmm] [filename]'
3626 */
3627 tline = tline->next;
3628 skip_white_(tline);
3629 if (!tok_type_(tline, TOK_NUMBER)) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08003630 nasm_error(ERR_NONFATAL, "`%%line' expects line number");
H. Peter Anvine2c80182005-01-15 22:15:51 +00003631 free_tlist(origline);
3632 return DIRECTIVE_FOUND;
3633 }
H. Peter Anvin70055962007-10-11 00:05:31 -07003634 k = readnum(tline->text, &err);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003635 m = 1;
3636 tline = tline->next;
3637 if (tok_is_(tline, "+")) {
3638 tline = tline->next;
3639 if (!tok_type_(tline, TOK_NUMBER)) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08003640 nasm_error(ERR_NONFATAL, "`%%line' expects line increment");
H. Peter Anvine2c80182005-01-15 22:15:51 +00003641 free_tlist(origline);
3642 return DIRECTIVE_FOUND;
3643 }
H. Peter Anvin70055962007-10-11 00:05:31 -07003644 m = readnum(tline->text, &err);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003645 tline = tline->next;
3646 }
3647 skip_white_(tline);
3648 src_set_linnum(k);
3649 istk->lineinc = m;
3650 if (tline) {
H. Peter Anvin274cda82016-05-10 02:56:29 -07003651 char *fname = detoken(tline, false);
3652 src_set_fname(fname);
3653 nasm_free(fname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003654 }
3655 free_tlist(origline);
3656 return DIRECTIVE_FOUND;
Keith Kaniosb307a4f2010-11-06 17:41:51 -05003657
H. Peter Anvine2c80182005-01-15 22:15:51 +00003658 default:
H. Peter Anvin130736c2016-02-17 20:27:41 -08003659 nasm_error(ERR_FATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00003660 "preprocessor directive `%s' not yet implemented",
H. Peter Anvin4169a472007-09-12 01:29:43 +00003661 pp_directives[i]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003662 return DIRECTIVE_FOUND;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003663 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003664}
3665
3666/*
H. Peter Anvin76690a12002-04-30 20:52:49 +00003667 * Ensure that a macro parameter contains a condition code and
3668 * nothing else. Return the condition code index if so, or -1
3669 * otherwise.
3670 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003671static int find_cc(Token * t)
H. Peter Anvineba20a72002-04-30 20:53:55 +00003672{
H. Peter Anvin76690a12002-04-30 20:52:49 +00003673 Token *tt;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003674
H. Peter Anvin25a99342007-09-22 17:45:45 -07003675 if (!t)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003676 return -1; /* Probably a %+ without a space */
H. Peter Anvin25a99342007-09-22 17:45:45 -07003677
H. Peter Anvineba20a72002-04-30 20:53:55 +00003678 skip_white_(t);
Cyrill Gorcunov7524cfd2017-10-22 19:01:16 +03003679 if (!t)
3680 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003681 if (t->type != TOK_ID)
H. Peter Anvine2c80182005-01-15 22:15:51 +00003682 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003683 tt = t->next;
H. Peter Anvineba20a72002-04-30 20:53:55 +00003684 skip_white_(tt);
H. Peter Anvin76690a12002-04-30 20:52:49 +00003685 if (tt && (tt->type != TOK_OTHER || strcmp(tt->text, ",")))
H. Peter Anvine2c80182005-01-15 22:15:51 +00003686 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003687
Cyrill Gorcunov19456392012-05-02 00:18:56 +04003688 return bsii(t->text, (const char **)conditions, ARRAY_SIZE(conditions));
H. Peter Anvin76690a12002-04-30 20:52:49 +00003689}
3690
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003691/*
3692 * This routines walks over tokens strem and hadnles tokens
3693 * pasting, if @handle_explicit passed then explicit pasting
3694 * term is handled, otherwise -- implicit pastings only.
3695 */
Cyrill Gorcunov575d4282010-10-06 00:25:55 +04003696static bool paste_tokens(Token **head, const struct tokseq_match *m,
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003697 size_t mnum, bool handle_explicit)
H. Peter Anvind784a082009-04-20 14:01:18 -07003698{
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003699 Token *tok, *next, **prev_next, **prev_nonspace;
3700 bool pasted = false;
3701 char *buf, *p;
3702 size_t len, i;
H. Peter Anvind784a082009-04-20 14:01:18 -07003703
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003704 /*
3705 * The last token before pasting. We need it
3706 * to be able to connect new handled tokens.
3707 * In other words if there were a tokens stream
3708 *
3709 * A -> B -> C -> D
3710 *
3711 * and we've joined tokens B and C, the resulting
3712 * stream should be
3713 *
3714 * A -> BC -> D
3715 */
3716 tok = *head;
3717 prev_next = NULL;
3718
3719 if (!tok_type_(tok, TOK_WHITESPACE) && !tok_type_(tok, TOK_PASTE))
3720 prev_nonspace = head;
3721 else
3722 prev_nonspace = NULL;
3723
3724 while (tok && (next = tok->next)) {
3725
3726 switch (tok->type) {
H. Peter Anvind784a082009-04-20 14:01:18 -07003727 case TOK_WHITESPACE:
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003728 /* Zap redundant whitespaces */
3729 while (tok_type_(next, TOK_WHITESPACE))
3730 next = delete_Token(next);
3731 tok->next = next;
H. Peter Anvind784a082009-04-20 14:01:18 -07003732 break;
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003733
3734 case TOK_PASTE:
3735 /* Explicit pasting */
3736 if (!handle_explicit)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003737 break;
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003738 next = delete_Token(tok);
3739
3740 while (tok_type_(next, TOK_WHITESPACE))
3741 next = delete_Token(next);
3742
3743 if (!pasted)
3744 pasted = true;
3745
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003746 /* Left pasting token is start of line */
3747 if (!prev_nonspace)
H. Peter Anvin130736c2016-02-17 20:27:41 -08003748 nasm_error(ERR_FATAL, "No lvalue found on pasting");
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003749
Cyrill Gorcunov8b5c9fb2013-02-04 01:24:54 +04003750 /*
3751 * No ending token, this might happen in two
3752 * cases
3753 *
3754 * 1) There indeed no right token at all
3755 * 2) There is a bare "%define ID" statement,
3756 * and @ID does expand to whitespace.
3757 *
3758 * So technically we need to do a grammar analysis
3759 * in another stage of parsing, but for now lets don't
3760 * change the behaviour people used to. Simply allow
3761 * whitespace after paste token.
3762 */
3763 if (!next) {
3764 /*
3765 * Zap ending space tokens and that's all.
3766 */
3767 tok = (*prev_nonspace)->next;
3768 while (tok_type_(tok, TOK_WHITESPACE))
3769 tok = delete_Token(tok);
3770 tok = *prev_nonspace;
3771 tok->next = NULL;
3772 break;
3773 }
3774
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003775 tok = *prev_nonspace;
3776 while (tok_type_(tok, TOK_WHITESPACE))
3777 tok = delete_Token(tok);
3778 len = strlen(tok->text);
3779 len += strlen(next->text);
3780
3781 p = buf = nasm_malloc(len + 1);
3782 strcpy(p, tok->text);
3783 p = strchr(p, '\0');
3784 strcpy(p, next->text);
3785
3786 delete_Token(tok);
3787
3788 tok = tokenize(buf);
3789 nasm_free(buf);
3790
3791 *prev_nonspace = tok;
3792 while (tok && tok->next)
3793 tok = tok->next;
3794
3795 tok->next = delete_Token(next);
3796
3797 /* Restart from pasted tokens head */
3798 tok = *prev_nonspace;
3799 break;
3800
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003801 default:
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003802 /* implicit pasting */
Cyrill Gorcunov575d4282010-10-06 00:25:55 +04003803 for (i = 0; i < mnum; i++) {
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003804 if (!(PP_CONCAT_MATCH(tok, m[i].mask_head)))
3805 continue;
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +04003806
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003807 len = 0;
3808 while (next && PP_CONCAT_MATCH(next, m[i].mask_tail)) {
3809 len += strlen(next->text);
3810 next = next->next;
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +04003811 }
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003812
Cyrill Gorcunov6f8109e2017-10-22 21:26:36 +03003813 /* No match or no text to process */
3814 if (tok == next || len == 0)
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003815 break;
3816
3817 len += strlen(tok->text);
3818 p = buf = nasm_malloc(len + 1);
3819
Adam Majer1a069432017-07-25 11:12:35 +02003820 strcpy(p, tok->text);
3821 p = strchr(p, '\0');
3822 tok = delete_Token(tok);
3823
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003824 while (tok != next) {
Adam Majer1a069432017-07-25 11:12:35 +02003825 if (PP_CONCAT_MATCH(tok, m[i].mask_tail)) {
3826 strcpy(p, tok->text);
3827 p = strchr(p, '\0');
3828 }
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003829 tok = delete_Token(tok);
3830 }
3831
3832 tok = tokenize(buf);
3833 nasm_free(buf);
3834
3835 if (prev_next)
3836 *prev_next = tok;
3837 else
3838 *head = tok;
3839
3840 /*
3841 * Connect pasted into original stream,
3842 * ie A -> new-tokens -> B
3843 */
3844 while (tok && tok->next)
3845 tok = tok->next;
3846 tok->next = next;
3847
3848 if (!pasted)
3849 pasted = true;
3850
3851 /* Restart from pasted tokens head */
3852 tok = prev_next ? *prev_next : *head;
Cyrill Gorcunov575d4282010-10-06 00:25:55 +04003853 }
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003854
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003855 break;
H. Peter Anvind784a082009-04-20 14:01:18 -07003856 }
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003857
3858 prev_next = &tok->next;
3859
3860 if (tok->next &&
3861 !tok_type_(tok->next, TOK_WHITESPACE) &&
3862 !tok_type_(tok->next, TOK_PASTE))
3863 prev_nonspace = prev_next;
3864
3865 tok = tok->next;
H. Peter Anvind784a082009-04-20 14:01:18 -07003866 }
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003867
3868 return pasted;
H. Peter Anvind784a082009-04-20 14:01:18 -07003869}
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003870
3871/*
3872 * expands to a list of tokens from %{x:y}
3873 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003874static Token *expand_mmac_params_range(MMacro *mac, Token *tline, Token ***last)
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003875{
3876 Token *t = tline, **tt, *tm, *head;
3877 char *pos;
3878 int fst, lst, j, i;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003879
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003880 pos = strchr(tline->text, ':');
3881 nasm_assert(pos);
3882
3883 lst = atoi(pos + 1);
3884 fst = atoi(tline->text + 1);
3885
3886 /*
3887 * only macros params are accounted so
3888 * if someone passes %0 -- we reject such
3889 * value(s)
3890 */
3891 if (lst == 0 || fst == 0)
3892 goto err;
3893
3894 /* the values should be sane */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003895 if ((fst > (int)mac->nparam || fst < (-(int)mac->nparam)) ||
3896 (lst > (int)mac->nparam || lst < (-(int)mac->nparam)))
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003897 goto err;
3898
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003899 fst = fst < 0 ? fst + (int)mac->nparam + 1: fst;
3900 lst = lst < 0 ? lst + (int)mac->nparam + 1: lst;
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003901
3902 /* counted from zero */
3903 fst--, lst--;
3904
3905 /*
Cyrill Gorcunove75331c2013-11-09 12:02:15 +04003906 * It will be at least one token. Note we
3907 * need to scan params until separator, otherwise
3908 * only first token will be passed.
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003909 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003910 tm = mac->params[(fst + mac->rotate) % mac->nparam];
Cyrill Gorcunov67f2ca22018-10-13 19:41:01 +03003911 if (!tm)
3912 goto err;
Cyrill Gorcunove75331c2013-11-09 12:02:15 +04003913 head = new_Token(NULL, tm->type, tm->text, 0);
3914 tt = &head->next, tm = tm->next;
3915 while (tok_isnt_(tm, ",")) {
3916 t = new_Token(NULL, tm->type, tm->text, 0);
3917 *tt = t, tt = &t->next, tm = tm->next;
3918 }
3919
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003920 if (fst < lst) {
3921 for (i = fst + 1; i <= lst; i++) {
3922 t = new_Token(NULL, TOK_OTHER, ",", 0);
3923 *tt = t, tt = &t->next;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003924 j = (i + mac->rotate) % mac->nparam;
3925 tm = mac->params[j];
Cyrill Gorcunove75331c2013-11-09 12:02:15 +04003926 while (tok_isnt_(tm, ",")) {
3927 t = new_Token(NULL, tm->type, tm->text, 0);
3928 *tt = t, tt = &t->next, tm = tm->next;
3929 }
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003930 }
3931 } else {
3932 for (i = fst - 1; i >= lst; i--) {
3933 t = new_Token(NULL, TOK_OTHER, ",", 0);
3934 *tt = t, tt = &t->next;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003935 j = (i + mac->rotate) % mac->nparam;
3936 tm = mac->params[j];
Cyrill Gorcunove75331c2013-11-09 12:02:15 +04003937 while (tok_isnt_(tm, ",")) {
3938 t = new_Token(NULL, tm->type, tm->text, 0);
3939 *tt = t, tt = &t->next, tm = tm->next;
3940 }
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003941 }
3942 }
3943
3944 *last = tt;
3945 return head;
3946
3947err:
H. Peter Anvin130736c2016-02-17 20:27:41 -08003948 nasm_error(ERR_NONFATAL, "`%%{%s}': macro parameters out of range",
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003949 &tline->text[1]);
3950 return tline;
3951}
3952
H. Peter Anvin76690a12002-04-30 20:52:49 +00003953/*
3954 * Expand MMacro-local things: parameter references (%0, %n, %+n,
H. Peter Anvin67c63722008-10-26 23:49:00 -07003955 * %-n) and MMacro-local identifiers (%%foo) as well as
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003956 * macro indirection (%[...]) and range (%{..:..}).
H. Peter Anvin76690a12002-04-30 20:52:49 +00003957 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003958static Token *expand_mmac_params(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00003959{
H. Peter Anvin734b1882002-04-30 21:01:08 +00003960 Token *t, *tt, **tail, *thead;
H. Peter Anvin6125b622009-04-08 14:02:25 -07003961 bool changed = false;
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003962 char *pos;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003963
3964 tail = &thead;
3965 thead = NULL;
3966
H. Peter Anvine2c80182005-01-15 22:15:51 +00003967 while (tline) {
Cyrill Gorcunov661f7232018-10-28 20:39:34 +03003968 if (tline->type == TOK_PREPROC_ID && tline->text && tline->text[0] &&
Cyrill Gorcunovca611192010-06-04 09:22:12 +04003969 (((tline->text[1] == '+' || tline->text[1] == '-') && tline->text[2]) ||
3970 (tline->text[1] >= '0' && tline->text[1] <= '9') ||
3971 tline->text[1] == '%')) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00003972 char *text = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003973 int type = 0, cc; /* type = 0 to placate optimisers */
Keith Kaniosa6dfa782007-04-13 16:47:53 +00003974 char tmpbuf[30];
H. Peter Anvin25a99342007-09-22 17:45:45 -07003975 unsigned int n;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003976 int i;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003977 MMacro *mac;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003978
H. Peter Anvine2c80182005-01-15 22:15:51 +00003979 t = tline;
3980 tline = tline->next;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003981
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003982 mac = istk->mstk;
3983 while (mac && !mac->name) /* avoid mistaking %reps for macros */
3984 mac = mac->next_active;
3985 if (!mac) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08003986 nasm_error(ERR_NONFATAL, "`%s': not in a macro call", t->text);
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003987 } else {
3988 pos = strchr(t->text, ':');
3989 if (!pos) {
3990 switch (t->text[1]) {
3991 /*
3992 * We have to make a substitution of one of the
3993 * forms %1, %-1, %+1, %%foo, %0.
3994 */
3995 case '0':
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003996 type = TOK_NUMBER;
3997 snprintf(tmpbuf, sizeof(tmpbuf), "%d", mac->nparam);
3998 text = nasm_strdup(tmpbuf);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003999 break;
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004000 case '%':
H. Peter Anvine2c80182005-01-15 22:15:51 +00004001 type = TOK_ID;
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004002 snprintf(tmpbuf, sizeof(tmpbuf), "..@%"PRIu64".",
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004003 mac->unique);
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004004 text = nasm_strcat(tmpbuf, t->text + 2);
4005 break;
4006 case '-':
4007 n = atoi(t->text + 2) - 1;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004008 if (n >= mac->nparam)
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004009 tt = NULL;
4010 else {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004011 if (mac->nparam > 1)
4012 n = (n + mac->rotate) % mac->nparam;
4013 tt = mac->params[n];
H. Peter Anvine2c80182005-01-15 22:15:51 +00004014 }
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004015 cc = find_cc(tt);
4016 if (cc == -1) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08004017 nasm_error(ERR_NONFATAL,
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004018 "macro parameter %d is not a condition code",
4019 n + 1);
4020 text = NULL;
4021 } else {
4022 type = TOK_ID;
4023 if (inverse_ccs[cc] == -1) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08004024 nasm_error(ERR_NONFATAL,
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004025 "condition code `%s' is not invertible",
4026 conditions[cc]);
4027 text = NULL;
4028 } else
4029 text = nasm_strdup(conditions[inverse_ccs[cc]]);
4030 }
4031 break;
4032 case '+':
4033 n = atoi(t->text + 2) - 1;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004034 if (n >= mac->nparam)
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004035 tt = NULL;
4036 else {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004037 if (mac->nparam > 1)
4038 n = (n + mac->rotate) % mac->nparam;
4039 tt = mac->params[n];
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004040 }
4041 cc = find_cc(tt);
4042 if (cc == -1) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08004043 nasm_error(ERR_NONFATAL,
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004044 "macro parameter %d is not a condition code",
4045 n + 1);
4046 text = NULL;
4047 } else {
4048 type = TOK_ID;
4049 text = nasm_strdup(conditions[cc]);
4050 }
4051 break;
4052 default:
4053 n = atoi(t->text + 1) - 1;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004054 if (n >= mac->nparam)
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004055 tt = NULL;
4056 else {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004057 if (mac->nparam > 1)
4058 n = (n + mac->rotate) % mac->nparam;
4059 tt = mac->params[n];
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004060 }
4061 if (tt) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004062 for (i = 0; i < mac->paramlen[n]; i++) {
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004063 *tail = new_Token(NULL, tt->type, tt->text, 0);
4064 tail = &(*tail)->next;
4065 tt = tt->next;
4066 }
4067 }
4068 text = NULL; /* we've done it here */
4069 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004070 }
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004071 } else {
4072 /*
4073 * seems we have a parameters range here
4074 */
4075 Token *head, **last;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004076 head = expand_mmac_params_range(mac, t, &last);
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004077 if (head != t) {
4078 *tail = head;
4079 *last = tline;
4080 tline = head;
4081 text = NULL;
4082 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00004083 }
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004084 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00004085 if (!text) {
4086 delete_Token(t);
4087 } else {
4088 *tail = t;
4089 tail = &t->next;
4090 t->type = type;
4091 nasm_free(t->text);
4092 t->text = text;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004093 t->a.mac = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004094 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004095 changed = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004096 continue;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004097 } else if (tline->type == TOK_INDIRECT) {
4098 t = tline;
4099 tline = tline->next;
4100 tt = tokenize(t->text);
4101 tt = expand_mmac_params(tt);
4102 tt = expand_smacro(tt);
4103 *tail = tt;
4104 while (tt) {
4105 tt->a.mac = NULL; /* Necessary? */
4106 tail = &tt->next;
4107 tt = tt->next;
4108 }
4109 delete_Token(t);
4110 changed = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004111 } else {
4112 t = *tail = tline;
4113 tline = tline->next;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004114 t->a.mac = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004115 tail = &t->next;
4116 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00004117 }
H. Peter Anvineba20a72002-04-30 20:53:55 +00004118 *tail = NULL;
H. Peter Anvin67c63722008-10-26 23:49:00 -07004119
Cyrill Gorcunovc6a742c2011-06-27 01:23:09 +04004120 if (changed) {
4121 const struct tokseq_match t[] = {
4122 {
4123 PP_CONCAT_MASK(TOK_ID) |
4124 PP_CONCAT_MASK(TOK_FLOAT), /* head */
4125 PP_CONCAT_MASK(TOK_ID) |
4126 PP_CONCAT_MASK(TOK_NUMBER) |
4127 PP_CONCAT_MASK(TOK_FLOAT) |
4128 PP_CONCAT_MASK(TOK_OTHER) /* tail */
4129 },
4130 {
4131 PP_CONCAT_MASK(TOK_NUMBER), /* head */
4132 PP_CONCAT_MASK(TOK_NUMBER) /* tail */
4133 }
4134 };
4135 paste_tokens(&thead, t, ARRAY_SIZE(t), false);
4136 }
H. Peter Anvin6125b622009-04-08 14:02:25 -07004137
H. Peter Anvin76690a12002-04-30 20:52:49 +00004138 return thead;
4139}
4140
4141/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004142 * Expand all single-line macro calls made in the given line.
4143 * Return the expanded version of the line. The original is deemed
4144 * to be destroyed in the process. (In reality we'll just move
4145 * Tokens from input to output a lot of the time, rather than
4146 * actually bothering to destroy and replicate.)
4147 */
H. Peter Anvincb1cf592007-11-19 12:26:50 -08004148
H. Peter Anvine2c80182005-01-15 22:15:51 +00004149static Token *expand_smacro(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004150{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004151 Token *t, *tt, *mstart, **tail, *thead;
H. Peter Anvineba20a72002-04-30 20:53:55 +00004152 SMacro *head = NULL, *m;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004153 Token **params;
4154 int *paramsize;
H. Peter Anvin25a99342007-09-22 17:45:45 -07004155 unsigned int nparam, sparam;
H. Peter Anvind784a082009-04-20 14:01:18 -07004156 int brackets;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004157 Token *org_tline = tline;
4158 Context *ctx;
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08004159 const char *mname;
H. Peter Anvina3d96d02018-06-15 17:51:39 -07004160 int64_t deadman = nasm_limit[LIMIT_MACROS];
H. Peter Anvin8287daf2009-07-07 16:00:58 -07004161 bool expanded;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004162
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004163 /*
4164 * Trick: we should avoid changing the start token pointer since it can
4165 * be contained in "next" field of other token. Because of this
4166 * we allocate a copy of first token and work with it; at the end of
4167 * routine we copy it back
4168 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004169 if (org_tline) {
Cyrill Gorcunoved4a8052010-04-09 15:40:35 +04004170 tline = new_Token(org_tline->next, org_tline->type,
4171 org_tline->text, 0);
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004172 tline->a.mac = org_tline->a.mac;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004173 nasm_free(org_tline->text);
4174 org_tline->text = NULL;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004175 }
4176
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004177 expanded = true; /* Always expand %+ at least once */
H. Peter Anvin8287daf2009-07-07 16:00:58 -07004178
H. Peter Anvincb1cf592007-11-19 12:26:50 -08004179again:
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004180 thead = NULL;
Cyrill Gorcunoved4a8052010-04-09 15:40:35 +04004181 tail = &thead;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004182
H. Peter Anvine2c80182005-01-15 22:15:51 +00004183 while (tline) { /* main token loop */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004184 if (!--deadman) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08004185 nasm_error(ERR_NONFATAL, "interminable macro recursion");
Cyrill Gorcunovbd38c8f2009-11-21 11:11:23 +03004186 goto err;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004187 }
H. Peter Anvincb1cf592007-11-19 12:26:50 -08004188
H. Peter Anvine2c80182005-01-15 22:15:51 +00004189 if ((mname = tline->text)) {
4190 /* if this token is a local macro, look in local context */
Cyrill Gorcunovc56d9ad2010-02-11 15:12:19 +03004191 if (tline->type == TOK_ID) {
4192 head = (SMacro *)hash_findix(&smacros, mname);
4193 } else if (tline->type == TOK_PREPROC_ID) {
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04004194 ctx = get_ctx(mname, &mname);
Cyrill Gorcunovc56d9ad2010-02-11 15:12:19 +03004195 head = ctx ? (SMacro *)hash_findix(&ctx->localmac, mname) : NULL;
4196 } else
4197 head = NULL;
H. Peter Anvin072771e2008-05-22 13:17:51 -07004198
H. Peter Anvine2c80182005-01-15 22:15:51 +00004199 /*
4200 * We've hit an identifier. As in is_mmacro below, we first
4201 * check whether the identifier is a single-line macro at
4202 * all, then think about checking for parameters if
4203 * necessary.
4204 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004205 list_for_each(m, head)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004206 if (!mstrcmp(m->name, mname, m->casesense))
4207 break;
4208 if (m) {
4209 mstart = tline;
4210 params = NULL;
4211 paramsize = NULL;
4212 if (m->nparam == 0) {
4213 /*
4214 * Simple case: the macro is parameterless. Discard the
4215 * one token that the macro call took, and push the
4216 * expansion back on the to-do stack.
4217 */
4218 if (!m->expansion) {
4219 if (!strcmp("__FILE__", m->name)) {
H. Peter Anvin274cda82016-05-10 02:56:29 -07004220 const char *file = src_get_fname();
4221 /* nasm_free(tline->text); here? */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004222 tline->text = nasm_quote(file, strlen(file));
4223 tline->type = TOK_STRING;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004224 continue;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004225 }
4226 if (!strcmp("__LINE__", m->name)) {
4227 nasm_free(tline->text);
4228 make_tok_num(tline, src_get_linnum());
4229 continue;
4230 }
4231 if (!strcmp("__BITS__", m->name)) {
4232 nasm_free(tline->text);
4233 make_tok_num(tline, globalbits);
4234 continue;
4235 }
4236 tline = delete_Token(tline);
4237 continue;
4238 }
4239 } else {
4240 /*
4241 * Complicated case: at least one macro with this name
H. Peter Anvine2c80182005-01-15 22:15:51 +00004242 * exists and takes parameters. We must find the
4243 * parameters in the call, count them, find the SMacro
4244 * that corresponds to that form of the macro call, and
4245 * substitute for the parameters when we expand. What a
4246 * pain.
4247 */
4248 /*tline = tline->next;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004249 skip_white_(tline); */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004250 do {
4251 t = tline->next;
4252 while (tok_type_(t, TOK_SMAC_END)) {
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004253 t->a.mac->in_progress = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004254 t->text = NULL;
4255 t = tline->next = delete_Token(t);
4256 }
4257 tline = t;
4258 } while (tok_type_(tline, TOK_WHITESPACE));
4259 if (!tok_is_(tline, "(")) {
4260 /*
4261 * This macro wasn't called with parameters: ignore
4262 * the call. (Behaviour borrowed from gnu cpp.)
4263 */
4264 tline = mstart;
4265 m = NULL;
4266 } else {
4267 int paren = 0;
4268 int white = 0;
4269 brackets = 0;
4270 nparam = 0;
4271 sparam = PARAM_DELTA;
4272 params = nasm_malloc(sparam * sizeof(Token *));
4273 params[0] = tline->next;
4274 paramsize = nasm_malloc(sparam * sizeof(int));
4275 paramsize[0] = 0;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07004276 while (true) { /* parameter loop */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004277 /*
4278 * For some unusual expansions
4279 * which concatenates function call
4280 */
4281 t = tline->next;
4282 while (tok_type_(t, TOK_SMAC_END)) {
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004283 t->a.mac->in_progress = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004284 t->text = NULL;
4285 t = tline->next = delete_Token(t);
4286 }
4287 tline = t;
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00004288
H. Peter Anvine2c80182005-01-15 22:15:51 +00004289 if (!tline) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08004290 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00004291 "macro call expects terminating `)'");
4292 break;
4293 }
4294 if (tline->type == TOK_WHITESPACE
4295 && brackets <= 0) {
4296 if (paramsize[nparam])
4297 white++;
4298 else
4299 params[nparam] = tline->next;
4300 continue; /* parameter loop */
4301 }
4302 if (tline->type == TOK_OTHER
4303 && tline->text[1] == 0) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004304 char ch = tline->text[0];
H. Peter Anvine2c80182005-01-15 22:15:51 +00004305 if (ch == ',' && !paren && brackets <= 0) {
4306 if (++nparam >= sparam) {
4307 sparam += PARAM_DELTA;
4308 params = nasm_realloc(params,
Cyrill Gorcunoved4a8052010-04-09 15:40:35 +04004309 sparam * sizeof(Token *));
4310 paramsize = nasm_realloc(paramsize,
4311 sparam * sizeof(int));
H. Peter Anvine2c80182005-01-15 22:15:51 +00004312 }
4313 params[nparam] = tline->next;
4314 paramsize[nparam] = 0;
4315 white = 0;
4316 continue; /* parameter loop */
4317 }
4318 if (ch == '{' &&
4319 (brackets > 0 || (brackets == 0 &&
4320 !paramsize[nparam])))
4321 {
4322 if (!(brackets++)) {
4323 params[nparam] = tline->next;
4324 continue; /* parameter loop */
4325 }
4326 }
4327 if (ch == '}' && brackets > 0)
4328 if (--brackets == 0) {
4329 brackets = -1;
4330 continue; /* parameter loop */
4331 }
4332 if (ch == '(' && !brackets)
4333 paren++;
4334 if (ch == ')' && brackets <= 0)
4335 if (--paren < 0)
4336 break;
4337 }
4338 if (brackets < 0) {
4339 brackets = 0;
H. Peter Anvin130736c2016-02-17 20:27:41 -08004340 nasm_error(ERR_NONFATAL, "braces do not "
H. Peter Anvine2c80182005-01-15 22:15:51 +00004341 "enclose all of macro parameter");
4342 }
4343 paramsize[nparam] += white + 1;
4344 white = 0;
4345 } /* parameter loop */
4346 nparam++;
4347 while (m && (m->nparam != nparam ||
4348 mstrcmp(m->name, mname,
4349 m->casesense)))
4350 m = m->next;
4351 if (!m)
H. Peter Anvin130736c2016-02-17 20:27:41 -08004352 nasm_error(ERR_WARNING|ERR_PASS1|ERR_WARN_MNP,
H. Peter Anvine2c80182005-01-15 22:15:51 +00004353 "macro `%s' exists, "
4354 "but not taking %d parameters",
4355 mstart->text, nparam);
4356 }
4357 }
4358 if (m && m->in_progress)
4359 m = NULL;
4360 if (!m) { /* in progess or didn't find '(' or wrong nparam */
H. Peter Anvin70653092007-10-19 14:42:29 -07004361 /*
H. Peter Anvine2c80182005-01-15 22:15:51 +00004362 * Design question: should we handle !tline, which
4363 * indicates missing ')' here, or expand those
4364 * macros anyway, which requires the (t) test a few
H. Peter Anvin70653092007-10-19 14:42:29 -07004365 * lines down?
H. Peter Anvine2c80182005-01-15 22:15:51 +00004366 */
4367 nasm_free(params);
4368 nasm_free(paramsize);
4369 tline = mstart;
4370 } else {
4371 /*
4372 * Expand the macro: we are placed on the last token of the
4373 * call, so that we can easily split the call from the
4374 * following tokens. We also start by pushing an SMAC_END
4375 * token for the cycle removal.
4376 */
4377 t = tline;
4378 if (t) {
4379 tline = t->next;
4380 t->next = NULL;
4381 }
4382 tt = new_Token(tline, TOK_SMAC_END, NULL, 0);
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004383 tt->a.mac = m;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07004384 m->in_progress = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004385 tline = tt;
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04004386 list_for_each(t, m->expansion) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004387 if (t->type >= TOK_SMAC_PARAM) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00004388 Token *pcopy = tline, **ptail = &pcopy;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004389 Token *ttt, *pt;
4390 int i;
H. Peter Anvin734b1882002-04-30 21:01:08 +00004391
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004392 ttt = params[t->type - TOK_SMAC_PARAM];
4393 i = paramsize[t->type - TOK_SMAC_PARAM];
4394 while (--i >= 0) {
4395 pt = *ptail = new_Token(tline, ttt->type,
4396 ttt->text, 0);
4397 ptail = &pt->next;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004398 ttt = ttt->next;
Cyrill Gorcunov59ce1c62017-10-22 18:42:07 +03004399 if (!ttt && i > 0) {
4400 /*
4401 * FIXME: Need to handle more gracefully,
4402 * exiting early on agruments analysis.
4403 */
4404 nasm_error(ERR_FATAL,
4405 "macro `%s' expects %d args",
4406 mstart->text,
4407 (int)paramsize[t->type - TOK_SMAC_PARAM]);
4408 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00004409 }
4410 tline = pcopy;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004411 } else if (t->type == TOK_PREPROC_Q) {
4412 tt = new_Token(tline, TOK_ID, mname, 0);
4413 tline = tt;
4414 } else if (t->type == TOK_PREPROC_QQ) {
4415 tt = new_Token(tline, TOK_ID, m->name, 0);
4416 tline = tt;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004417 } else {
4418 tt = new_Token(tline, t->type, t->text, 0);
4419 tline = tt;
4420 }
4421 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00004422
H. Peter Anvine2c80182005-01-15 22:15:51 +00004423 /*
4424 * Having done that, get rid of the macro call, and clean
4425 * up the parameters.
4426 */
4427 nasm_free(params);
4428 nasm_free(paramsize);
4429 free_tlist(mstart);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004430 expanded = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004431 continue; /* main token loop */
4432 }
4433 }
4434 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004435
H. Peter Anvine2c80182005-01-15 22:15:51 +00004436 if (tline->type == TOK_SMAC_END) {
Cyrill Gorcunov980dd652018-10-14 19:25:32 +03004437 /* On error path it might already be dropped */
4438 if (tline->a.mac)
4439 tline->a.mac->in_progress = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004440 tline = delete_Token(tline);
4441 } else {
4442 t = *tail = tline;
4443 tline = tline->next;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004444 t->a.mac = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004445 t->next = NULL;
4446 tail = &t->next;
4447 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004448 }
4449
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004450 /*
4451 * Now scan the entire line and look for successive TOK_IDs that resulted
Keith Kaniosb7a89542007-04-12 02:40:54 +00004452 * after expansion (they can't be produced by tokenize()). The successive
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004453 * TOK_IDs should be concatenated.
4454 * Also we look for %+ tokens and concatenate the tokens before and after
4455 * them (without white spaces in between).
4456 */
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +04004457 if (expanded) {
Cyrill Gorcunovc6a742c2011-06-27 01:23:09 +04004458 const struct tokseq_match t[] = {
4459 {
4460 PP_CONCAT_MASK(TOK_ID) |
4461 PP_CONCAT_MASK(TOK_PREPROC_ID), /* head */
4462 PP_CONCAT_MASK(TOK_ID) |
4463 PP_CONCAT_MASK(TOK_PREPROC_ID) |
4464 PP_CONCAT_MASK(TOK_NUMBER) /* tail */
4465 }
4466 };
4467 if (paste_tokens(&thead, t, ARRAY_SIZE(t), true)) {
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +04004468 /*
4469 * If we concatenated something, *and* we had previously expanded
4470 * an actual macro, scan the lines again for macros...
4471 */
4472 tline = thead;
4473 expanded = false;
4474 goto again;
4475 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00004476 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004477
Cyrill Gorcunovbd38c8f2009-11-21 11:11:23 +03004478err:
H. Peter Anvine2c80182005-01-15 22:15:51 +00004479 if (org_tline) {
4480 if (thead) {
4481 *org_tline = *thead;
4482 /* since we just gave text to org_line, don't free it */
4483 thead->text = NULL;
4484 delete_Token(thead);
4485 } else {
4486 /* the expression expanded to empty line;
4487 we can't return NULL for some reasons
4488 we just set the line to a single WHITESPACE token. */
4489 memset(org_tline, 0, sizeof(*org_tline));
4490 org_tline->text = NULL;
4491 org_tline->type = TOK_WHITESPACE;
4492 }
4493 thead = org_tline;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004494 }
4495
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004496 return thead;
4497}
4498
4499/*
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004500 * Similar to expand_smacro but used exclusively with macro identifiers
4501 * right before they are fetched in. The reason is that there can be
4502 * identifiers consisting of several subparts. We consider that if there
4503 * are more than one element forming the name, user wants a expansion,
4504 * otherwise it will be left as-is. Example:
4505 *
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004506 * %define %$abc cde
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004507 *
4508 * the identifier %$abc will be left as-is so that the handler for %define
4509 * will suck it and define the corresponding value. Other case:
4510 *
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004511 * %define _%$abc cde
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004512 *
4513 * In this case user wants name to be expanded *before* %define starts
4514 * working, so we'll expand %$abc into something (if it has a value;
4515 * otherwise it will be left as-is) then concatenate all successive
4516 * PP_IDs into one.
4517 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004518static Token *expand_id(Token * tline)
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004519{
4520 Token *cur, *oldnext = NULL;
4521
H. Peter Anvin734b1882002-04-30 21:01:08 +00004522 if (!tline || !tline->next)
H. Peter Anvine2c80182005-01-15 22:15:51 +00004523 return tline;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004524
4525 cur = tline;
4526 while (cur->next &&
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004527 (cur->next->type == TOK_ID ||
4528 cur->next->type == TOK_PREPROC_ID
4529 || cur->next->type == TOK_NUMBER))
H. Peter Anvine2c80182005-01-15 22:15:51 +00004530 cur = cur->next;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004531
4532 /* If identifier consists of just one token, don't expand */
4533 if (cur == tline)
H. Peter Anvine2c80182005-01-15 22:15:51 +00004534 return tline;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004535
H. Peter Anvine2c80182005-01-15 22:15:51 +00004536 if (cur) {
4537 oldnext = cur->next; /* Detach the tail past identifier */
4538 cur->next = NULL; /* so that expand_smacro stops here */
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004539 }
4540
H. Peter Anvin734b1882002-04-30 21:01:08 +00004541 tline = expand_smacro(tline);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004542
H. Peter Anvine2c80182005-01-15 22:15:51 +00004543 if (cur) {
4544 /* expand_smacro possibly changhed tline; re-scan for EOL */
4545 cur = tline;
4546 while (cur && cur->next)
4547 cur = cur->next;
4548 if (cur)
4549 cur->next = oldnext;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004550 }
4551
4552 return tline;
4553}
4554
4555/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004556 * Determine whether the given line constitutes a multi-line macro
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004557 * call, and return the MMacro structure called if so. Doesn't have
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004558 * to check for an initial label - that's taken care of in
4559 * expand_mmacro - but must check numbers of parameters. Guaranteed
4560 * to be called with tline->type == TOK_ID, so the putative macro
4561 * name is easy to find.
4562 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004563static MMacro *is_mmacro(Token * tline, Token *** params_array)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004564{
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004565 MMacro *head, *m;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004566 Token **params;
4567 int nparam;
4568
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004569 head = (MMacro *) hash_findix(&mmacros, tline->text);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004570
4571 /*
4572 * Efficiency: first we see if any macro exists with the given
4573 * name. If not, we can return NULL immediately. _Then_ we
4574 * count the parameters, and then we look further along the
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004575 * list if necessary to find the proper MMacro.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004576 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004577 list_for_each(m, head)
4578 if (!mstrcmp(m->name, tline->text, m->casesense))
H. Peter Anvine2c80182005-01-15 22:15:51 +00004579 break;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004580 if (!m)
H. Peter Anvine2c80182005-01-15 22:15:51 +00004581 return NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004582
4583 /*
4584 * OK, we have a potential macro. Count and demarcate the
4585 * parameters.
4586 */
H. Peter Anvin734b1882002-04-30 21:01:08 +00004587 count_mmac_params(tline->next, &nparam, &params);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004588
4589 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004590 * So we know how many parameters we've got. Find the MMacro
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004591 * structure that handles this number.
4592 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004593 while (m) {
4594 if (m->nparam_min <= nparam
4595 && (m->plus || nparam <= m->nparam_max)) {
4596 /*
4597 * This one is right. Just check if cycle removal
4598 * prohibits us using it before we actually celebrate...
4599 */
4600 if (m->in_progress > m->max_depth) {
4601 if (m->max_depth > 0) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08004602 nasm_error(ERR_WARNING,
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004603 "reached maximum recursion depth of %i",
4604 m->max_depth);
4605 }
4606 nasm_free(params);
4607 return NULL;
4608 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00004609 /*
4610 * It's right, and we can use it. Add its default
4611 * parameters to the end of our list if necessary.
4612 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004613 if (m->defaults && nparam < m->nparam_min + m->ndefs) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00004614 params =
4615 nasm_realloc(params,
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004616 ((m->nparam_min + m->ndefs +
H. Peter Anvine2c80182005-01-15 22:15:51 +00004617 1) * sizeof(*params)));
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004618 while (nparam < m->nparam_min + m->ndefs) {
4619 params[nparam] = m->defaults[nparam - m->nparam_min];
H. Peter Anvine2c80182005-01-15 22:15:51 +00004620 nparam++;
4621 }
4622 }
4623 /*
4624 * If we've gone over the maximum parameter count (and
4625 * we're in Plus mode), ignore parameters beyond
4626 * nparam_max.
4627 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004628 if (m->plus && nparam > m->nparam_max)
4629 nparam = m->nparam_max;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004630 /*
4631 * Then terminate the parameter list, and leave.
4632 */
4633 if (!params) { /* need this special case */
4634 params = nasm_malloc(sizeof(*params));
4635 nparam = 0;
4636 }
4637 params[nparam] = NULL;
4638 *params_array = params;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004639 return m;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004640 }
4641 /*
4642 * This one wasn't right: look for the next one with the
4643 * same name.
4644 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004645 list_for_each(m, m->next)
4646 if (!mstrcmp(m->name, tline->text, m->casesense))
H. Peter Anvine2c80182005-01-15 22:15:51 +00004647 break;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004648 }
4649
4650 /*
4651 * After all that, we didn't find one with the right number of
4652 * parameters. Issue a warning, and fail to expand the macro.
4653 */
H. Peter Anvin130736c2016-02-17 20:27:41 -08004654 nasm_error(ERR_WARNING|ERR_PASS1|ERR_WARN_MNP,
H. Peter Anvine2c80182005-01-15 22:15:51 +00004655 "macro `%s' exists, but not taking %d parameters",
4656 tline->text, nparam);
H. Peter Anvin734b1882002-04-30 21:01:08 +00004657 nasm_free(params);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004658 return NULL;
4659}
4660
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004661
4662/*
4663 * Save MMacro invocation specific fields in
4664 * preparation for a recursive macro expansion
4665 */
4666static void push_mmacro(MMacro *m)
4667{
4668 MMacroInvocation *i;
4669
4670 i = nasm_malloc(sizeof(MMacroInvocation));
4671 i->prev = m->prev;
4672 i->params = m->params;
4673 i->iline = m->iline;
4674 i->nparam = m->nparam;
4675 i->rotate = m->rotate;
4676 i->paramlen = m->paramlen;
4677 i->unique = m->unique;
4678 i->condcnt = m->condcnt;
4679 m->prev = i;
4680}
4681
4682
4683/*
4684 * Restore MMacro invocation specific fields that were
4685 * saved during a previous recursive macro expansion
4686 */
4687static void pop_mmacro(MMacro *m)
4688{
4689 MMacroInvocation *i;
4690
4691 if (m->prev) {
4692 i = m->prev;
4693 m->prev = i->prev;
4694 m->params = i->params;
4695 m->iline = i->iline;
4696 m->nparam = i->nparam;
4697 m->rotate = i->rotate;
4698 m->paramlen = i->paramlen;
4699 m->unique = i->unique;
4700 m->condcnt = i->condcnt;
4701 nasm_free(i);
4702 }
4703}
4704
4705
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004706/*
4707 * Expand the multi-line macro call made by the given line, if
4708 * there is one to be expanded. If there is, push the expansion on
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004709 * istk->expansion and return 1. Otherwise return 0.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004710 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004711static int expand_mmacro(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004712{
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004713 Token *startline = tline;
H. Peter Anvineba20a72002-04-30 20:53:55 +00004714 Token *label = NULL;
4715 int dont_prepend = 0;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004716 Token **params, *t, *tt;
4717 MMacro *m;
4718 Line *l, *ll;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004719 int i, nparam, *paramlen;
H. Peter Anvinc751e862008-06-09 10:18:45 -07004720 const char *mname;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004721
4722 t = tline;
H. Peter Anvineba20a72002-04-30 20:53:55 +00004723 skip_white_(t);
H. Peter Anvince2233b2008-05-25 21:57:00 -07004724 /* if (!tok_type_(t, TOK_ID)) Lino 02/25/02 */
H. Peter Anvindce1e2f2002-04-30 21:06:37 +00004725 if (!tok_type_(t, TOK_ID) && !tok_type_(t, TOK_PREPROC_ID))
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004726 return 0;
4727 m = is_mmacro(t, &params);
4728 if (m) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004729 mname = t->text;
H. Peter Anvinc751e862008-06-09 10:18:45 -07004730 } else {
H. Peter Anvine2c80182005-01-15 22:15:51 +00004731 Token *last;
4732 /*
4733 * We have an id which isn't a macro call. We'll assume
4734 * it might be a label; we'll also check to see if a
4735 * colon follows it. Then, if there's another id after
4736 * that lot, we'll check it again for macro-hood.
4737 */
4738 label = last = t;
4739 t = t->next;
4740 if (tok_type_(t, TOK_WHITESPACE))
4741 last = t, t = t->next;
4742 if (tok_is_(t, ":")) {
4743 dont_prepend = 1;
4744 last = t, t = t->next;
4745 if (tok_type_(t, TOK_WHITESPACE))
4746 last = t, t = t->next;
4747 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004748 if (!tok_type_(t, TOK_ID) || !(m = is_mmacro(t, &params)))
4749 return 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004750 last->next = NULL;
Keith Kanios891775e2009-07-11 06:08:54 -05004751 mname = t->text;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004752 tline = t;
H. Peter Anvineba20a72002-04-30 20:53:55 +00004753 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004754
4755 /*
4756 * Fix up the parameters: this involves stripping leading and
4757 * trailing whitespace, then stripping braces if they are
4758 * present.
4759 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004760 for (nparam = 0; params[nparam]; nparam++) ;
H. Peter Anvin734b1882002-04-30 21:01:08 +00004761 paramlen = nparam ? nasm_malloc(nparam * sizeof(*paramlen)) : NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004762
H. Peter Anvine2c80182005-01-15 22:15:51 +00004763 for (i = 0; params[i]; i++) {
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08004764 int brace = 0;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004765 int comma = (!m->plus || i < nparam - 1);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004766
H. Peter Anvine2c80182005-01-15 22:15:51 +00004767 t = params[i];
4768 skip_white_(t);
4769 if (tok_is_(t, "{"))
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08004770 t = t->next, brace++, comma = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004771 params[i] = t;
4772 paramlen[i] = 0;
4773 while (t) {
4774 if (comma && t->type == TOK_OTHER && !strcmp(t->text, ","))
4775 break; /* ... because we have hit a comma */
4776 if (comma && t->type == TOK_WHITESPACE
4777 && tok_is_(t->next, ","))
4778 break; /* ... or a space then a comma */
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08004779 if (brace && t->type == TOK_OTHER) {
4780 if (t->text[0] == '{')
4781 brace++; /* ... or a nested opening brace */
4782 else if (t->text[0] == '}')
4783 if (!--brace)
4784 break; /* ... or a brace */
4785 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00004786 t = t->next;
4787 paramlen[i]++;
4788 }
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08004789 if (brace)
H. Peter Anvin130736c2016-02-17 20:27:41 -08004790 nasm_error(ERR_NONFATAL, "macro params should be enclosed in braces");
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004791 }
4792
4793 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004794 * OK, we have a MMacro structure together with a set of
4795 * parameters. We must now go through the expansion and push
4796 * copies of each Line on to istk->expansion. Substitution of
H. Peter Anvin76690a12002-04-30 20:52:49 +00004797 * parameter tokens and macro-local tokens doesn't get done
4798 * until the single-line macro substitution process; this is
4799 * because delaying them allows us to change the semantics
4800 * later through %rotate.
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004801 *
4802 * First, push an end marker on to istk->expansion, mark this
4803 * macro as in progress, and set up its invocation-specific
4804 * variables.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004805 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004806 ll = nasm_malloc(sizeof(Line));
4807 ll->next = istk->expansion;
4808 ll->finishes = m;
4809 ll->first = NULL;
4810 istk->expansion = ll;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004811
4812 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004813 * Save the previous MMacro expansion in the case of
4814 * macro recursion
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004815 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004816 if (m->max_depth && m->in_progress)
4817 push_mmacro(m);
4818
4819 m->in_progress ++;
4820 m->params = params;
4821 m->iline = tline;
4822 m->nparam = nparam;
4823 m->rotate = 0;
4824 m->paramlen = paramlen;
4825 m->unique = unique++;
4826 m->lineno = 0;
4827 m->condcnt = 0;
4828
4829 m->next_active = istk->mstk;
4830 istk->mstk = m;
4831
4832 list_for_each(l, m->expansion) {
4833 Token **tail;
4834
4835 ll = nasm_malloc(sizeof(Line));
4836 ll->finishes = NULL;
4837 ll->next = istk->expansion;
4838 istk->expansion = ll;
4839 tail = &ll->first;
4840
4841 list_for_each(t, l->first) {
4842 Token *x = t;
4843 switch (t->type) {
4844 case TOK_PREPROC_Q:
4845 tt = *tail = new_Token(NULL, TOK_ID, mname, 0);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004846 break;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004847 case TOK_PREPROC_QQ:
4848 tt = *tail = new_Token(NULL, TOK_ID, m->name, 0);
4849 break;
4850 case TOK_PREPROC_ID:
4851 if (t->text[1] == '0' && t->text[2] == '0') {
4852 dont_prepend = -1;
4853 x = label;
4854 if (!x)
4855 continue;
4856 }
4857 /* fall through */
4858 default:
4859 tt = *tail = new_Token(NULL, x->type, x->text, 0);
4860 break;
4861 }
4862 tail = &tt->next;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004863 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004864 *tail = NULL;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004865 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004866
4867 /*
H. Peter Anvineba20a72002-04-30 20:53:55 +00004868 * If we had a label, push it on as the first line of
4869 * the macro expansion.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004870 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004871 if (label) {
4872 if (dont_prepend < 0)
4873 free_tlist(startline);
4874 else {
4875 ll = nasm_malloc(sizeof(Line));
4876 ll->finishes = NULL;
4877 ll->next = istk->expansion;
4878 istk->expansion = ll;
4879 ll->first = startline;
4880 if (!dont_prepend) {
4881 while (label->next)
4882 label = label->next;
4883 label->next = tt = new_Token(NULL, TOK_OTHER, ":", 0);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004884 }
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004885 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004886 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004887
H. Peter Anvin8ac25aa2016-02-18 01:16:18 -08004888 lfmt->uplevel(m->nolist ? LIST_MACRO_NOLIST : LIST_MACRO);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004889
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004890 return 1;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004891}
4892
H. Peter Anvin130736c2016-02-17 20:27:41 -08004893/*
4894 * This function adds macro names to error messages, and suppresses
4895 * them if necessary.
4896 */
4897static void pp_verror(int severity, const char *fmt, va_list arg)
Victor van den Elzen3b404c02008-09-18 13:51:36 +02004898{
H. Peter Anvin130736c2016-02-17 20:27:41 -08004899 char buff[BUFSIZ];
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004900 MMacro *mmac = NULL;
4901 int delta = 0;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02004902
H. Peter Anvin130736c2016-02-17 20:27:41 -08004903 /*
4904 * If we're in a dead branch of IF or something like it, ignore the error.
4905 * However, because %else etc are evaluated in the state context
4906 * of the previous branch, errors might get lost:
4907 * %if 0 ... %else trailing garbage ... %endif
4908 * So %else etc should set the ERR_PP_PRECOND flag.
4909 */
4910 if ((severity & ERR_MASK) < ERR_FATAL &&
4911 istk && istk->conds &&
4912 ((severity & ERR_PP_PRECOND) ?
4913 istk->conds->state == COND_NEVER :
H. Peter Anvineb6653f2016-04-05 13:03:10 -07004914 !emitting(istk->conds->state)))
H. Peter Anvin130736c2016-02-17 20:27:41 -08004915 return;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02004916
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004917 /* get %macro name */
H. Peter Anvin130736c2016-02-17 20:27:41 -08004918 if (!(severity & ERR_NOFILE) && istk && istk->mstk) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004919 mmac = istk->mstk;
4920 /* but %rep blocks should be skipped */
4921 while (mmac && !mmac->name)
4922 mmac = mmac->next_active, delta++;
Cyrill Gorcunov9900c6b2011-10-09 18:58:46 +04004923 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004924
H. Peter Anvin130736c2016-02-17 20:27:41 -08004925 if (mmac) {
4926 vsnprintf(buff, sizeof(buff), fmt, arg);
Victor van den Elzen3b404c02008-09-18 13:51:36 +02004927
H. Peter Anvin130736c2016-02-17 20:27:41 -08004928 nasm_set_verror(real_verror);
4929 nasm_error(severity, "(%s:%d) %s",
4930 mmac->name, mmac->lineno - delta, buff);
4931 nasm_set_verror(pp_verror);
4932 } else {
4933 real_verror(severity, fmt, arg);
4934 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004935}
4936
H. Peter Anvin734b1882002-04-30 21:01:08 +00004937static void
Cyrill Gorcunovb7bb5ac2018-11-11 21:33:52 +03004938pp_reset(const char *file, int apass, struct strlist *dep_list)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004939{
H. Peter Anvin7383b402008-09-24 10:20:40 -07004940 Token *t;
4941
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004942 cstk = NULL;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004943 istk = nasm_malloc(sizeof(Include));
4944 istk->next = NULL;
4945 istk->conds = NULL;
4946 istk->expansion = NULL;
4947 istk->mstk = NULL;
H. Peter Anvin3e83cec2016-05-25 04:28:46 -07004948 istk->fp = nasm_open_read(file, NF_TEXT);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004949 istk->fname = NULL;
H. Peter Anvin274cda82016-05-10 02:56:29 -07004950 src_set(0, file);
H. Peter Anvineba20a72002-04-30 20:53:55 +00004951 istk->lineinc = 1;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004952 if (!istk->fp)
Cyrill Gorcunovc3527dd2018-11-24 22:17:47 +03004953 nasm_fatalf(ERR_NOFILE, "unable to open input file `%s'", file);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004954 defining = NULL;
Charles Crayned4200be2008-07-12 16:42:33 -07004955 nested_mac_count = 0;
4956 nested_rep_count = 0;
H. Peter Anvin97a23472007-09-16 17:57:25 -07004957 init_macros();
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004958 unique = 0;
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07004959 deplist = dep_list;
H. Peter Anvinf7606612016-07-13 14:23:48 -07004960
4961 if (tasm_compatible_mode)
4962 pp_add_stdmac(nasm_stdmac_tasm);
4963
4964 pp_add_stdmac(nasm_stdmac_nasm);
4965 pp_add_stdmac(nasm_stdmac_version);
4966
Cyrill Gorcunov15ce78f2017-01-06 20:21:28 +03004967 if (extrastdmac)
4968 pp_add_stdmac(extrastdmac);
4969
H. Peter Anvinf7606612016-07-13 14:23:48 -07004970 stdmacpos = stdmacros[0];
4971 stdmacnext = &stdmacros[1];
4972
H. Peter Anvind2456592008-06-19 15:04:18 -07004973 do_predef = true;
H. Peter Anvin61f130f2008-09-25 15:45:06 -07004974
4975 /*
4976 * 0 for dependencies, 1 for preparatory passes, 2 for final pass.
4977 * The caller, however, will also pass in 3 for preprocess-only so
4978 * we can set __PASS__ accordingly.
4979 */
4980 pass = apass > 2 ? 2 : apass;
4981
Cyrill Gorcunovb7bb5ac2018-11-11 21:33:52 +03004982 strlist_add(deplist, file);
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07004983
H. Peter Anvin61f130f2008-09-25 15:45:06 -07004984 /*
4985 * Define the __PASS__ macro. This is defined here unlike
4986 * all the other builtins, because it is special -- it varies between
4987 * passes.
4988 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004989 t = nasm_malloc(sizeof(*t));
4990 t->next = NULL;
H. Peter Anvin61f130f2008-09-25 15:45:06 -07004991 make_tok_num(t, apass);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004992 t->a.mac = NULL;
H. Peter Anvin7383b402008-09-24 10:20:40 -07004993 define_smacro(NULL, "__PASS__", true, 0, t);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004994}
4995
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07004996static void pp_init(void)
4997{
4998 hash_init(&FileHash, HASH_MEDIUM);
4999}
5000
Keith Kaniosa6dfa782007-04-13 16:47:53 +00005001static char *pp_getline(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005002{
Keith Kaniosa6dfa782007-04-13 16:47:53 +00005003 char *line;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005004 Token *tline;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005005
H. Peter Anvin130736c2016-02-17 20:27:41 -08005006 real_verror = nasm_set_verror(pp_verror);
H. Peter Anvin215186f2016-02-17 20:27:41 -08005007
H. Peter Anvine2c80182005-01-15 22:15:51 +00005008 while (1) {
5009 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005010 * Fetch a tokenized line, either from the macro-expansion
H. Peter Anvine2c80182005-01-15 22:15:51 +00005011 * buffer or from the input file.
5012 */
5013 tline = NULL;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005014 while (istk->expansion && istk->expansion->finishes) {
5015 Line *l = istk->expansion;
5016 if (!l->finishes->name && l->finishes->in_progress > 1) {
5017 Line *ll;
H. Peter Anvineba20a72002-04-30 20:53:55 +00005018
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005019 /*
5020 * This is a macro-end marker for a macro with no
5021 * name, which means it's not really a macro at all
5022 * but a %rep block, and the `in_progress' field is
5023 * more than 1, meaning that we still need to
5024 * repeat. (1 means the natural last repetition; 0
5025 * means termination by %exitrep.) We have
5026 * therefore expanded up to the %endrep, and must
5027 * push the whole block on to the expansion buffer
5028 * again. We don't bother to remove the macro-end
5029 * marker: we'd only have to generate another one
5030 * if we did.
5031 */
5032 l->finishes->in_progress--;
5033 list_for_each(l, l->finishes->expansion) {
5034 Token *t, *tt, **tail;
5035
5036 ll = nasm_malloc(sizeof(Line));
5037 ll->next = istk->expansion;
5038 ll->finishes = NULL;
5039 ll->first = NULL;
5040 tail = &ll->first;
5041
5042 list_for_each(t, l->first) {
5043 if (t->text || t->type == TOK_WHITESPACE) {
5044 tt = *tail = new_Token(NULL, t->type, t->text, 0);
5045 tail = &tt->next;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005046 }
5047 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005048
5049 istk->expansion = ll;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005050 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005051 } else {
5052 /*
5053 * Check whether a `%rep' was started and not ended
5054 * within this macro expansion. This can happen and
5055 * should be detected. It's a fatal error because
5056 * I'm too confused to work out how to recover
5057 * sensibly from it.
5058 */
5059 if (defining) {
5060 if (defining->name)
H. Peter Anvinc5136902018-06-15 18:20:17 -07005061 nasm_panic("defining with name in expansion");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005062 else if (istk->mstk->name)
H. Peter Anvinc5136902018-06-15 18:20:17 -07005063 nasm_fatal("`%%rep' without `%%endrep' within"
H. Peter Anvin130736c2016-02-17 20:27:41 -08005064 " expansion of macro `%s'",
5065 istk->mstk->name);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005066 }
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005067
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005068 /*
5069 * FIXME: investigate the relationship at this point between
5070 * istk->mstk and l->finishes
5071 */
5072 {
5073 MMacro *m = istk->mstk;
5074 istk->mstk = m->next_active;
5075 if (m->name) {
5076 /*
5077 * This was a real macro call, not a %rep, and
5078 * therefore the parameter information needs to
5079 * be freed.
5080 */
5081 if (m->prev) {
5082 pop_mmacro(m);
5083 l->finishes->in_progress --;
5084 } else {
5085 nasm_free(m->params);
5086 free_tlist(m->iline);
5087 nasm_free(m->paramlen);
5088 l->finishes->in_progress = 0;
5089 }
Adam Majer91e72402017-07-25 10:42:01 +02005090 }
5091
5092 /*
5093 * FIXME It is incorrect to always free_mmacro here.
5094 * It leads to usage-after-free.
5095 *
5096 * https://bugzilla.nasm.us/show_bug.cgi?id=3392414
5097 */
5098#if 0
5099 else
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005100 free_mmacro(m);
Adam Majer91e72402017-07-25 10:42:01 +02005101#endif
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005102 }
5103 istk->expansion = l->next;
5104 nasm_free(l);
H. Peter Anvin8ac25aa2016-02-18 01:16:18 -08005105 lfmt->downlevel(LIST_MACRO);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005106 }
5107 }
5108 while (1) { /* until we get a line we can use */
5109
5110 if (istk->expansion) { /* from a macro expansion */
5111 char *p;
5112 Line *l = istk->expansion;
5113 if (istk->mstk)
5114 istk->mstk->lineno++;
5115 tline = l->first;
5116 istk->expansion = l->next;
5117 nasm_free(l);
5118 p = detoken(tline, false);
H. Peter Anvin8ac25aa2016-02-18 01:16:18 -08005119 lfmt->line(LIST_MACRO, p);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005120 nasm_free(p);
5121 break;
5122 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00005123 line = read_line();
5124 if (line) { /* from the current input file */
5125 line = prepreproc(line);
Keith Kaniosb7a89542007-04-12 02:40:54 +00005126 tline = tokenize(line);
H. Peter Anvine2c80182005-01-15 22:15:51 +00005127 nasm_free(line);
5128 break;
5129 }
5130 /*
5131 * The current file has ended; work down the istk
5132 */
5133 {
5134 Include *i = istk;
5135 fclose(i->fp);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005136 if (i->conds) {
5137 /* nasm_error can't be conditionally suppressed */
H. Peter Anvinc5136902018-06-15 18:20:17 -07005138 nasm_fatal("expected `%%endif' before end of file");
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005139 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00005140 /* only set line and file name if there's a next node */
H. Peter Anvin274cda82016-05-10 02:56:29 -07005141 if (i->next)
5142 src_set(i->lineno, i->fname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00005143 istk = i->next;
H. Peter Anvin8ac25aa2016-02-18 01:16:18 -08005144 lfmt->downlevel(LIST_INCLUDE);
H. Peter Anvine2c80182005-01-15 22:15:51 +00005145 nasm_free(i);
H. Peter Anvin130736c2016-02-17 20:27:41 -08005146 if (!istk) {
5147 line = NULL;
5148 goto done;
5149 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005150 if (istk->expansion && istk->expansion->finishes)
5151 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005152 }
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005153 }
5154
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005155 /*
5156 * We must expand MMacro parameters and MMacro-local labels
5157 * _before_ we plunge into directive processing, to cope
5158 * with things like `%define something %1' such as STRUC
5159 * uses. Unless we're _defining_ a MMacro, in which case
5160 * those tokens should be left alone to go into the
5161 * definition; and unless we're in a non-emitting
5162 * condition, in which case we don't want to meddle with
5163 * anything.
5164 */
5165 if (!defining && !(istk->conds && !emitting(istk->conds->state))
5166 && !(istk->mstk && !istk->mstk->in_progress)) {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005167 tline = expand_mmac_params(tline);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005168 }
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005169
H. Peter Anvine2c80182005-01-15 22:15:51 +00005170 /*
5171 * Check the line to see if it's a preprocessor directive.
5172 */
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07005173 if (do_directive(tline, &line) == DIRECTIVE_FOUND) {
5174 if (line)
5175 break; /* Directive generated output */
5176 else
5177 continue;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005178 } else if (defining) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00005179 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005180 * We're defining a multi-line macro. We emit nothing
5181 * at all, and just
5182 * shove the tokenized line on to the macro definition.
H. Peter Anvine2c80182005-01-15 22:15:51 +00005183 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005184 Line *l = nasm_malloc(sizeof(Line));
5185 l->next = defining->expansion;
5186 l->first = tline;
5187 l->finishes = NULL;
5188 defining->expansion = l;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005189 continue;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005190 } else if (istk->conds && !emitting(istk->conds->state)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00005191 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005192 * We're in a non-emitting branch of a condition block.
H. Peter Anvine2c80182005-01-15 22:15:51 +00005193 * Emit nothing at all, not even a blank line: when we
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005194 * emerge from the condition we'll give a line-number
H. Peter Anvine2c80182005-01-15 22:15:51 +00005195 * directive so we keep our place correctly.
5196 */
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005197 free_tlist(tline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00005198 continue;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005199 } else if (istk->mstk && !istk->mstk->in_progress) {
5200 /*
5201 * We're in a %rep block which has been terminated, so
5202 * we're walking through to the %endrep without
5203 * emitting anything. Emit nothing at all, not even a
5204 * blank line: when we emerge from the %rep block we'll
5205 * give a line-number directive so we keep our place
5206 * correctly.
5207 */
5208 free_tlist(tline);
5209 continue;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005210 } else {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005211 tline = expand_smacro(tline);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005212 if (!expand_mmacro(tline)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00005213 /*
Keith Kaniosb7a89542007-04-12 02:40:54 +00005214 * De-tokenize the line again, and emit it.
H. Peter Anvine2c80182005-01-15 22:15:51 +00005215 */
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005216 line = detoken(tline, true);
5217 free_tlist(tline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00005218 break;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005219 } else {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005220 continue; /* expand_mmacro calls free_tlist */
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005221 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00005222 }
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005223 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005224
H. Peter Anvin130736c2016-02-17 20:27:41 -08005225done:
5226 nasm_set_verror(real_verror);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005227 return line;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005228}
5229
H. Peter Anvine2c80182005-01-15 22:15:51 +00005230static void pp_cleanup(int pass)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005231{
H. Peter Anvin130736c2016-02-17 20:27:41 -08005232 real_verror = nasm_set_verror(pp_verror);
H. Peter Anvin215186f2016-02-17 20:27:41 -08005233
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005234 if (defining) {
5235 if (defining->name) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08005236 nasm_error(ERR_NONFATAL,
5237 "end of file while still defining macro `%s'",
5238 defining->name);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005239 } else {
H. Peter Anvin130736c2016-02-17 20:27:41 -08005240 nasm_error(ERR_NONFATAL, "end of file while still in %%rep");
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005241 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005242
5243 free_mmacro(defining);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005244 defining = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005245 }
H. Peter Anvin130736c2016-02-17 20:27:41 -08005246
5247 nasm_set_verror(real_verror);
H. Peter Anvin215186f2016-02-17 20:27:41 -08005248
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005249 while (cstk)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005250 ctx_pop();
H. Peter Anvin97a23472007-09-16 17:57:25 -07005251 free_macros();
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005252 while (istk) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00005253 Include *i = istk;
5254 istk = istk->next;
5255 fclose(i->fp);
Cyrill Gorcunov8dcfd882011-03-03 09:18:56 +03005256 nasm_free(i);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005257 }
5258 while (cstk)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005259 ctx_pop();
H. Peter Anvin274cda82016-05-10 02:56:29 -07005260 src_set_fname(NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00005261 if (pass == 0) {
5262 free_llist(predef);
Cyrill Gorcunovdae24d72014-06-28 10:17:39 +04005263 predef = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005264 delete_Blocks();
Cyrill Gorcunovdae24d72014-06-28 10:17:39 +04005265 freeTokens = NULL;
Cyrill Gorcunov8c0666b2018-11-24 14:33:48 +03005266 ipath_list = NULL;
H. Peter Anvin11dfa1a2008-07-02 18:11:04 -07005267 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005268}
5269
Cyrill Gorcunov8c0666b2018-11-24 14:33:48 +03005270static void pp_include_path(struct strlist *list)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005271{
Cyrill Gorcunov8c0666b2018-11-24 14:33:48 +03005272 ipath_list = list;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005273}
Frank Kotlerd0ed6fd2003-08-27 11:33:56 +00005274
Cyrill Gorcunov0b78bff2012-05-07 01:57:55 +04005275static void pp_pre_include(char *fname)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005276{
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005277 Token *inc, *space, *name;
5278 Line *l;
5279
H. Peter Anvin734b1882002-04-30 21:01:08 +00005280 name = new_Token(NULL, TOK_INTERNAL_STRING, fname, 0);
5281 space = new_Token(name, TOK_WHITESPACE, NULL, 0);
5282 inc = new_Token(space, TOK_PREPROC_ID, "%include", 0);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005283
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005284 l = nasm_malloc(sizeof(Line));
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005285 l->next = predef;
5286 l->first = inc;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005287 l->finishes = NULL;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005288 predef = l;
5289}
5290
Cyrill Gorcunov0b78bff2012-05-07 01:57:55 +04005291static void pp_pre_define(char *definition)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005292{
5293 Token *def, *space;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005294 Line *l;
Keith Kaniosa6dfa782007-04-13 16:47:53 +00005295 char *equals;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005296
H. Peter Anvin130736c2016-02-17 20:27:41 -08005297 real_verror = nasm_set_verror(pp_verror);
H. Peter Anvin215186f2016-02-17 20:27:41 -08005298
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005299 equals = strchr(definition, '=');
H. Peter Anvin734b1882002-04-30 21:01:08 +00005300 space = new_Token(NULL, TOK_WHITESPACE, NULL, 0);
5301 def = new_Token(space, TOK_PREPROC_ID, "%define", 0);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005302 if (equals)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005303 *equals = ' ';
Keith Kaniosb7a89542007-04-12 02:40:54 +00005304 space->next = tokenize(definition);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005305 if (equals)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005306 *equals = '=';
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005307
Cyrill Gorcunov6d42e9b2015-02-08 11:07:17 +03005308 if (space->next->type != TOK_PREPROC_ID &&
5309 space->next->type != TOK_ID)
H. Peter Anvin130736c2016-02-17 20:27:41 -08005310 nasm_error(ERR_WARNING, "pre-defining non ID `%s\'\n", definition);
Cyrill Gorcunov6d42e9b2015-02-08 11:07:17 +03005311
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005312 l = nasm_malloc(sizeof(Line));
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005313 l->next = predef;
5314 l->first = def;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005315 l->finishes = NULL;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005316 predef = l;
H. Peter Anvin130736c2016-02-17 20:27:41 -08005317
5318 nasm_set_verror(real_verror);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005319}
5320
Cyrill Gorcunov0b78bff2012-05-07 01:57:55 +04005321static void pp_pre_undefine(char *definition)
H. Peter Anvin620515a2002-04-30 20:57:38 +00005322{
5323 Token *def, *space;
5324 Line *l;
5325
H. Peter Anvin734b1882002-04-30 21:01:08 +00005326 space = new_Token(NULL, TOK_WHITESPACE, NULL, 0);
5327 def = new_Token(space, TOK_PREPROC_ID, "%undef", 0);
Keith Kaniosb7a89542007-04-12 02:40:54 +00005328 space->next = tokenize(definition);
H. Peter Anvin620515a2002-04-30 20:57:38 +00005329
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005330 l = nasm_malloc(sizeof(Line));
H. Peter Anvin620515a2002-04-30 20:57:38 +00005331 l->next = predef;
5332 l->first = def;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005333 l->finishes = NULL;
H. Peter Anvin620515a2002-04-30 20:57:38 +00005334 predef = l;
5335}
5336
H. Peter Anvin05990342018-06-11 13:32:42 -07005337/* Insert an early preprocessor command that doesn't need special handling */
5338static void pp_pre_command(const char *what, char *string)
5339{
5340 char *cmd;
5341 Token *def, *space;
5342 Line *l;
5343
5344 def = tokenize(string);
5345 if (what) {
5346 cmd = nasm_strcat(what[0] == '%' ? "" : "%", what);
5347 space = new_Token(def, TOK_WHITESPACE, NULL, 0);
5348 def = new_Token(space, TOK_PREPROC_ID, cmd, 0);
5349 }
5350
5351 l = nasm_malloc(sizeof(Line));
5352 l->next = predef;
5353 l->first = def;
5354 l->finishes = NULL;
5355 predef = l;
5356}
5357
H. Peter Anvinf7606612016-07-13 14:23:48 -07005358static void pp_add_stdmac(macros_t *macros)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005359{
H. Peter Anvinf7606612016-07-13 14:23:48 -07005360 macros_t **mp;
5361
5362 /* Find the end of the list and avoid duplicates */
5363 for (mp = stdmacros; *mp; mp++) {
5364 if (*mp == macros)
5365 return; /* Nothing to do */
5366 }
5367
5368 nasm_assert(mp < &stdmacros[ARRAY_SIZE(stdmacros)-1]);
5369
5370 *mp = macros;
H. Peter Anvin76690a12002-04-30 20:52:49 +00005371}
5372
Cyrill Gorcunov15ce78f2017-01-06 20:21:28 +03005373static void pp_extra_stdmac(macros_t *macros)
5374{
5375 extrastdmac = macros;
5376}
5377
Keith Kaniosa5fc6462007-10-13 07:09:22 -07005378static void make_tok_num(Token * tok, int64_t val)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005379{
Cyrill Gorcunovce652742013-05-06 23:43:43 +04005380 char numbuf[32];
Keith Kaniosa5fc6462007-10-13 07:09:22 -07005381 snprintf(numbuf, sizeof(numbuf), "%"PRId64"", val);
H. Peter Anvineba20a72002-04-30 20:53:55 +00005382 tok->text = nasm_strdup(numbuf);
5383 tok->type = TOK_NUMBER;
5384}
5385
H. Peter Anvin37368952016-05-09 14:10:32 -07005386static void pp_list_one_macro(MMacro *m, int severity)
5387{
5388 if (!m)
5389 return;
5390
5391 /* We need to print the next_active list in reverse order */
5392 pp_list_one_macro(m->next_active, severity);
5393
5394 if (m->name && !m->nolist) {
H. Peter Anvin274cda82016-05-10 02:56:29 -07005395 src_set(m->xline + m->lineno, m->fname);
H. Peter Anvin37368952016-05-09 14:10:32 -07005396 nasm_error(severity, "... from macro `%s' defined here", m->name);
5397 }
5398}
5399
H. Peter Anvin4def1a82016-05-09 13:59:44 -07005400static void pp_error_list_macros(int severity)
5401{
H. Peter Anvin4def1a82016-05-09 13:59:44 -07005402 int32_t saved_line;
5403 const char *saved_fname = NULL;
5404
5405 severity |= ERR_PP_LISTMACRO | ERR_NO_SEVERITY;
H. Peter Anvin274cda82016-05-10 02:56:29 -07005406 src_get(&saved_line, &saved_fname);
H. Peter Anvin4def1a82016-05-09 13:59:44 -07005407
Cyrill Gorcunov771d04e2016-05-10 23:27:03 +03005408 if (istk)
5409 pp_list_one_macro(istk->mstk, severity);
H. Peter Anvin4def1a82016-05-09 13:59:44 -07005410
H. Peter Anvin274cda82016-05-10 02:56:29 -07005411 src_set(saved_line, saved_fname);
H. Peter Anvin4def1a82016-05-09 13:59:44 -07005412}
5413
H. Peter Anvine7469712016-02-18 02:20:59 -08005414const struct preproc_ops nasmpp = {
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07005415 pp_init,
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005416 pp_reset,
5417 pp_getline,
Cyrill Gorcunov0b78bff2012-05-07 01:57:55 +04005418 pp_cleanup,
Cyrill Gorcunov15ce78f2017-01-06 20:21:28 +03005419 pp_extra_stdmac,
Cyrill Gorcunov0b78bff2012-05-07 01:57:55 +04005420 pp_pre_define,
5421 pp_pre_undefine,
5422 pp_pre_include,
H. Peter Anvin05990342018-06-11 13:32:42 -07005423 pp_pre_command,
H. Peter Anvin4def1a82016-05-09 13:59:44 -07005424 pp_include_path,
5425 pp_error_list_macros,
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005426};