blob: 48840a32776fa5e83c48563997fcced580d4b444 [file] [log] [blame]
H. Peter Anvin9e6747c2009-06-28 17:13:04 -07001/* ----------------------------------------------------------------------- *
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002 *
H. Peter Anvin130736c2016-02-17 20:27:41 -08003 * Copyright 1996-2016 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 Anvin4169a472007-09-12 01:29:43 +000075#include "preproc.h"
H. Peter Anvin97a23472007-09-16 17:57:25 -070076#include "hashtbl.h"
H. Peter Anvin8cad14b2008-06-01 17:23:51 -070077#include "quote.h"
H. Peter Anvinc2df2822007-10-24 15:29:28 -070078#include "stdscan.h"
H. Peter Anvindbb640b2009-07-18 18:57:16 -070079#include "eval.h"
H. Peter Anvinc2df2822007-10-24 15:29:28 -070080#include "tokens.h"
H. Peter Anvina4835d42008-05-20 14:21:29 -070081#include "tables.h"
H. Peter Anvin8ac25aa2016-02-18 01:16:18 -080082#include "listing.h"
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000083
84typedef struct SMacro SMacro;
H. Peter Anvin36206cd2012-03-03 16:14:51 -080085typedef struct MMacro MMacro;
86typedef struct MMacroInvocation MMacroInvocation;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000087typedef struct Context Context;
88typedef struct Token Token;
H. Peter Anvince616072002-04-30 21:02:23 +000089typedef struct Blocks Blocks;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000090typedef struct Line Line;
91typedef struct Include Include;
H. Peter Anvin36206cd2012-03-03 16:14:51 -080092typedef struct Cond Cond;
H. Peter Anvin6768eb72002-04-30 20:52:26 +000093typedef struct IncPath IncPath;
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 Anvin6768eb72002-04-30 20:52:26 +0000279 * Include search path. This is simply a list of strings which get
280 * prepended, in turn, to the name of an include file, in an
281 * attempt to find the file if it's not in the current directory.
282 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000283struct IncPath {
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800284 IncPath *next;
285 char *path;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000286};
287
288/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000289 * Conditional assembly: we maintain a separate stack of these for
290 * each level of file inclusion. (The only reason we keep the
291 * stacks separate is to ensure that a stray `%endif' in a file
292 * included from within the true branch of a `%if' won't terminate
293 * it and cause confusion: instead, rightly, it'll cause an error.)
294 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800295struct Cond {
296 Cond *next;
297 int state;
298};
H. Peter Anvine2c80182005-01-15 22:15:51 +0000299enum {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000300 /*
301 * These states are for use just after %if or %elif: IF_TRUE
302 * means the condition has evaluated to truth so we are
303 * currently emitting, whereas IF_FALSE means we are not
304 * currently emitting but will start doing so if a %else comes
305 * up. In these states, all directives are admissible: %elif,
306 * %else and %endif. (And of course %if.)
307 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800308 COND_IF_TRUE, COND_IF_FALSE,
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000309 /*
310 * These states come up after a %else: ELSE_TRUE means we're
311 * emitting, and ELSE_FALSE means we're not. In ELSE_* states,
312 * any %elif or %else will cause an error.
313 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800314 COND_ELSE_TRUE, COND_ELSE_FALSE,
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000315 /*
Victor van den Elzen3b404c02008-09-18 13:51:36 +0200316 * These states mean that we're not emitting now, and also that
317 * nothing until %endif will be emitted at all. COND_DONE is
318 * used when we've had our moment of emission
319 * and have now started seeing %elifs. COND_NEVER is used when
320 * the condition construct in question is contained within a
321 * non-emitting branch of a larger condition construct,
322 * or if there is an error.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000323 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800324 COND_DONE, COND_NEVER
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000325};
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800326#define emitting(x) ( (x) == COND_IF_TRUE || (x) == COND_ELSE_TRUE )
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000327
H. Peter Anvin70653092007-10-19 14:42:29 -0700328/*
Ed Beroset3ab3f412002-06-11 03:31:49 +0000329 * These defines are used as the possible return values for do_directive
330 */
331#define NO_DIRECTIVE_FOUND 0
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300332#define DIRECTIVE_FOUND 1
Ed Beroset3ab3f412002-06-11 03:31:49 +0000333
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000334/*
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800335 * This define sets the upper limit for smacro and recursive mmacro
336 * expansions
Keith Kanios852f1ee2009-07-12 00:19:55 -0500337 */
338#define DEADMAN_LIMIT (1 << 20)
339
Cyrill Gorcunove091d6e2010-08-09 13:58:22 +0400340/* max reps */
341#define REP_LIMIT ((INT64_C(1) << 62))
342
Keith Kanios852f1ee2009-07-12 00:19:55 -0500343/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000344 * Condition codes. Note that we use c_ prefix not C_ because C_ is
345 * used in nasm.h for the "real" condition codes. At _this_ level,
346 * we treat CXZ and ECXZ as condition codes, albeit non-invertible
347 * ones, so we need a different enum...
348 */
H. Peter Anvin476d2862007-10-02 22:04:15 -0700349static const char * const conditions[] = {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000350 "a", "ae", "b", "be", "c", "cxz", "e", "ecxz", "g", "ge", "l", "le",
351 "na", "nae", "nb", "nbe", "nc", "ne", "ng", "nge", "nl", "nle", "no",
H. Peter Anvince9be342007-09-12 00:22:29 +0000352 "np", "ns", "nz", "o", "p", "pe", "po", "rcxz", "s", "z"
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000353};
H. Peter Anvin476d2862007-10-02 22:04:15 -0700354enum pp_conds {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000355 c_A, c_AE, c_B, c_BE, c_C, c_CXZ, c_E, c_ECXZ, c_G, c_GE, c_L, c_LE,
356 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 -0700357 c_NP, c_NS, c_NZ, c_O, c_P, c_PE, c_PO, c_RCXZ, c_S, c_Z,
358 c_none = -1
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000359};
H. Peter Anvin476d2862007-10-02 22:04:15 -0700360static const enum pp_conds inverse_ccs[] = {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000361 c_NA, c_NAE, c_NB, c_NBE, c_NC, -1, c_NE, -1, c_NG, c_NGE, c_NL, c_NLE,
362 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 +0000363 c_Z, c_NO, c_NP, c_PO, c_PE, -1, c_NS, c_NZ
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000364};
365
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800366/*
367 * Directive names.
368 */
369/* If this is a an IF, ELIF, ELSE or ENDIF keyword */
370static int is_condition(enum preproc_token arg)
371{
372 return PP_IS_COND(arg) || (arg == PP_ELSE) || (arg == PP_ENDIF);
373}
374
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000375/* For TASM compatibility we need to be able to recognise TASM compatible
376 * conditional compilation directives. Using the NASM pre-processor does
377 * not work, so we look for them specifically from the following list and
378 * then jam in the equivalent NASM directive into the input stream.
379 */
380
H. Peter Anvine2c80182005-01-15 22:15:51 +0000381enum {
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000382 TM_ARG, TM_ELIF, TM_ELSE, TM_ENDIF, TM_IF, TM_IFDEF, TM_IFDIFI,
383 TM_IFNDEF, TM_INCLUDE, TM_LOCAL
384};
385
H. Peter Anvin476d2862007-10-02 22:04:15 -0700386static const char * const tasm_directives[] = {
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000387 "arg", "elif", "else", "endif", "if", "ifdef", "ifdifi",
388 "ifndef", "include", "local"
389};
390
391static int StackSize = 4;
H. Peter Anvin6c8b2be2016-05-24 23:46:50 -0700392static const char *StackPointer = "ebp";
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000393static int ArgOffset = 8;
H. Peter Anvin8781cb02007-11-08 20:01:11 -0800394static int LocalOffset = 0;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000395
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000396static Context *cstk;
397static Include *istk;
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800398static IncPath *ipath = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000399
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300400static int pass; /* HACK: pass 0 = generate dependencies only */
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700401static StrList **dephead, **deptail; /* Dependency list */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000402
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300403static uint64_t unique; /* unique identifier numbers */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000404
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800405static Line *predef = NULL;
H. Peter Anvind2456592008-06-19 15:04:18 -0700406static bool do_predef;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000407
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000408/*
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800409 * The current set of multi-line macros we have defined.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000410 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800411static struct hash_table mmacros;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000412
413/*
414 * The current set of single-line macros we have defined.
415 */
H. Peter Anvin166c2472008-05-28 12:28:58 -0700416static struct hash_table smacros;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000417
418/*
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800419 * The multi-line macro we are currently defining, or the %rep
420 * block we are currently reading, if any.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000421 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800422static MMacro *defining;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000423
Charles Crayned4200be2008-07-12 16:42:33 -0700424static uint64_t nested_mac_count;
425static uint64_t nested_rep_count;
426
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000427/*
428 * The number of macro parameters to allocate space for at a time.
429 */
430#define PARAM_DELTA 16
431
432/*
H. Peter Anvinf7606612016-07-13 14:23:48 -0700433 * The standard macro set: defined in macros.c in a set of arrays.
434 * This gives our position in any macro set, while we are processing it.
435 * The stdmacset is an array of such macro sets.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000436 */
H. Peter Anvina70547f2008-07-19 21:44:26 -0700437static macros_t *stdmacpos;
H. Peter Anvinf7606612016-07-13 14:23:48 -0700438static macros_t **stdmacnext;
439static macros_t *stdmacros[8];
H. Peter Anvin76690a12002-04-30 20:52:49 +0000440
441/*
H. Peter Anvin734b1882002-04-30 21:01:08 +0000442 * Tokens are allocated in blocks to improve speed
443 */
444#define TOKEN_BLOCKSIZE 4096
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800445static Token *freeTokens = NULL;
H. Peter Anvince616072002-04-30 21:02:23 +0000446struct Blocks {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000447 Blocks *next;
448 void *chunk;
H. Peter Anvince616072002-04-30 21:02:23 +0000449};
450
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800451static Blocks blocks = { NULL, NULL };
H. Peter Anvin734b1882002-04-30 21:01:08 +0000452
453/*
H. Peter Anvin76690a12002-04-30 20:52:49 +0000454 * Forward declarations.
455 */
H. Peter Anvinf7606612016-07-13 14:23:48 -0700456static void pp_add_stdmac(macros_t *macros);
H. Peter Anvin734b1882002-04-30 21:01:08 +0000457static Token *expand_mmac_params(Token * tline);
458static Token *expand_smacro(Token * tline);
459static Token *expand_id(Token * tline);
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +0400460static Context *get_ctx(const char *name, const char **namep);
Keith Kaniosa5fc6462007-10-13 07:09:22 -0700461static void make_tok_num(Token * tok, int64_t val);
H. Peter Anvin130736c2016-02-17 20:27:41 -0800462static void pp_verror(int severity, const char *fmt, va_list ap);
463static vefunc real_verror;
H. Peter Anvince616072002-04-30 21:02:23 +0000464static void *new_Block(size_t size);
465static void delete_Blocks(void);
H. Peter Anvinc751e862008-06-09 10:18:45 -0700466static Token *new_Token(Token * next, enum pp_token_type type,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300467 const char *text, int txtlen);
H. Peter Anvin734b1882002-04-30 21:01:08 +0000468static Token *delete_Token(Token * t);
H. Peter Anvineba20a72002-04-30 20:53:55 +0000469
470/*
471 * Macros for safe checking of token pointers, avoid *(NULL)
472 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300473#define tok_type_(x,t) ((x) && (x)->type == (t))
474#define skip_white_(x) if (tok_type_((x), TOK_WHITESPACE)) (x)=(x)->next
475#define tok_is_(x,v) (tok_type_((x), TOK_OTHER) && !strcmp((x)->text,(v)))
476#define tok_isnt_(x,v) ((x) && ((x)->type!=TOK_OTHER || strcmp((x)->text,(v))))
H. Peter Anvin76690a12002-04-30 20:52:49 +0000477
Cyrill Gorcunov194ba892011-06-30 01:16:35 +0400478/*
H. Peter Anvin077fb932010-07-20 14:56:30 -0700479 * nasm_unquote with error if the string contains NUL characters.
480 * If the string contains NUL characters, issue an error and return
481 * the C len, i.e. truncate at the NUL.
482 */
483static size_t nasm_unquote_cstr(char *qstr, enum preproc_token directive)
484{
485 size_t len = nasm_unquote(qstr, NULL);
486 size_t clen = strlen(qstr);
487
488 if (len != clen)
H. Peter Anvin130736c2016-02-17 20:27:41 -0800489 nasm_error(ERR_NONFATAL, "NUL character in `%s' directive",
H. Peter Anvin077fb932010-07-20 14:56:30 -0700490 pp_directives[directive]);
491
492 return clen;
493}
494
495/*
H. Peter Anvinb40992c2010-09-15 08:57:21 -0700496 * In-place reverse a list of tokens.
497 */
498static Token *reverse_tokens(Token *t)
499{
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800500 Token *prev = NULL;
501 Token *next;
H. Peter Anvinb40992c2010-09-15 08:57:21 -0700502
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800503 while (t) {
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +0400504 next = t->next;
505 t->next = prev;
506 prev = t;
507 t = next;
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800508 }
H. Peter Anvinb40992c2010-09-15 08:57:21 -0700509
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800510 return prev;
H. Peter Anvinb40992c2010-09-15 08:57:21 -0700511}
512
513/*
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300514 * Handle TASM specific directives, which do not contain a % in
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000515 * front of them. We do it here because I could not find any other
516 * place to do it for the moment, and it is a hack (ideally it would
517 * be nice to be able to use the NASM pre-processor to do it).
518 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000519static char *check_tasm_directive(char *line)
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000520{
Keith Kaniosb7a89542007-04-12 02:40:54 +0000521 int32_t i, j, k, m, len;
Cyrill Gorcunovf66ac7d2009-10-12 20:41:13 +0400522 char *p, *q, *oldline, oldchar;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000523
Cyrill Gorcunovf66ac7d2009-10-12 20:41:13 +0400524 p = nasm_skip_spaces(line);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000525
526 /* Binary search for the directive name */
527 i = -1;
Cyrill Gorcunova7319242010-06-03 22:04:36 +0400528 j = ARRAY_SIZE(tasm_directives);
Cyrill Gorcunovf66ac7d2009-10-12 20:41:13 +0400529 q = nasm_skip_word(p);
530 len = q - p;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000531 if (len) {
532 oldchar = p[len];
533 p[len] = 0;
534 while (j - i > 1) {
535 k = (j + i) / 2;
536 m = nasm_stricmp(p, tasm_directives[k]);
537 if (m == 0) {
538 /* We have found a directive, so jam a % in front of it
539 * so that NASM will then recognise it as one if it's own.
540 */
541 p[len] = oldchar;
542 len = strlen(p);
543 oldline = line;
544 line = nasm_malloc(len + 2);
545 line[0] = '%';
546 if (k == TM_IFDIFI) {
H. Peter Anvin18f48792009-06-27 15:56:27 -0700547 /*
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300548 * NASM does not recognise IFDIFI, so we convert
549 * it to %if 0. This is not used in NASM
550 * compatible code, but does need to parse for the
551 * TASM macro package.
H. Peter Anvine2c80182005-01-15 22:15:51 +0000552 */
H. Peter Anvin18f48792009-06-27 15:56:27 -0700553 strcpy(line + 1, "if 0");
H. Peter Anvine2c80182005-01-15 22:15:51 +0000554 } else {
555 memcpy(line + 1, p, len + 1);
556 }
557 nasm_free(oldline);
558 return line;
559 } else if (m < 0) {
560 j = k;
561 } else
562 i = k;
563 }
564 p[len] = oldchar;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000565 }
566 return line;
567}
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000568
H. Peter Anvin76690a12002-04-30 20:52:49 +0000569/*
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000570 * The pre-preprocessing stage... This function translates line
571 * number indications as they emerge from GNU cpp (`# lineno "file"
572 * flags') into NASM preprocessor line number indications (`%line
573 * lineno file').
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000574 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000575static char *prepreproc(char *line)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000576{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000577 int lineno, fnlen;
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000578 char *fname, *oldline;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000579
H. Peter Anvine2c80182005-01-15 22:15:51 +0000580 if (line[0] == '#' && line[1] == ' ') {
581 oldline = line;
582 fname = oldline + 2;
583 lineno = atoi(fname);
584 fname += strspn(fname, "0123456789 ");
585 if (*fname == '"')
586 fname++;
587 fnlen = strcspn(fname, "\"");
588 line = nasm_malloc(20 + fnlen);
589 snprintf(line, 20 + fnlen, "%%line %d %.*s", lineno, fnlen, fname);
590 nasm_free(oldline);
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000591 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000592 if (tasm_compatible_mode)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000593 return check_tasm_directive(line);
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000594 return line;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000595}
596
597/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000598 * Free a linked list of tokens.
599 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000600static void free_tlist(Token * list)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000601{
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +0400602 while (list)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000603 list = delete_Token(list);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000604}
605
606/*
607 * Free a linked list of lines.
608 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000609static void free_llist(Line * list)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000610{
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +0400611 Line *l, *tmp;
612 list_for_each_safe(l, tmp, list) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000613 free_tlist(l->first);
614 nasm_free(l);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000615 }
616}
617
618/*
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800619 * Free an MMacro
H. Peter Anvineba20a72002-04-30 20:53:55 +0000620 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800621static void free_mmacro(MMacro * m)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000622{
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800623 nasm_free(m->name);
624 free_tlist(m->dlist);
625 nasm_free(m->defaults);
626 free_llist(m->expansion);
627 nasm_free(m);
H. Peter Anvineba20a72002-04-30 20:53:55 +0000628}
629
630/*
H. Peter Anvin97a23472007-09-16 17:57:25 -0700631 * Free all currently defined macros, and free the hash tables
632 */
H. Peter Anvin072771e2008-05-22 13:17:51 -0700633static void free_smacro_table(struct hash_table *smt)
H. Peter Anvin97a23472007-09-16 17:57:25 -0700634{
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +0400635 SMacro *s, *tmp;
H. Peter Anvin072771e2008-05-22 13:17:51 -0700636 const char *key;
637 struct hash_tbl_node *it = NULL;
H. Peter Anvin97a23472007-09-16 17:57:25 -0700638
H. Peter Anvin072771e2008-05-22 13:17:51 -0700639 while ((s = hash_iterate(smt, &it, &key)) != NULL) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300640 nasm_free((void *)key);
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +0400641 list_for_each_safe(s, tmp, s) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300642 nasm_free(s->name);
643 free_tlist(s->expansion);
644 nasm_free(s);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300645 }
H. Peter Anvin97a23472007-09-16 17:57:25 -0700646 }
H. Peter Anvin072771e2008-05-22 13:17:51 -0700647 hash_free(smt);
648}
649
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800650static void free_mmacro_table(struct hash_table *mmt)
H. Peter Anvin072771e2008-05-22 13:17:51 -0700651{
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800652 MMacro *m, *tmp;
H. Peter Anvin072771e2008-05-22 13:17:51 -0700653 const char *key;
654 struct hash_tbl_node *it = NULL;
H. Peter Anvin97a23472007-09-16 17:57:25 -0700655
656 it = NULL;
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800657 while ((m = hash_iterate(mmt, &it, &key)) != NULL) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300658 nasm_free((void *)key);
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800659 list_for_each_safe(m ,tmp, m)
660 free_mmacro(m);
H. Peter Anvin97a23472007-09-16 17:57:25 -0700661 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800662 hash_free(mmt);
H. Peter Anvin072771e2008-05-22 13:17:51 -0700663}
664
665static void free_macros(void)
666{
H. Peter Anvin166c2472008-05-28 12:28:58 -0700667 free_smacro_table(&smacros);
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800668 free_mmacro_table(&mmacros);
H. Peter Anvin97a23472007-09-16 17:57:25 -0700669}
670
671/*
672 * Initialize the hash tables
673 */
674static void init_macros(void)
675{
H. Peter Anvin166c2472008-05-28 12:28:58 -0700676 hash_init(&smacros, HASH_LARGE);
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800677 hash_init(&mmacros, HASH_LARGE);
H. Peter Anvin97a23472007-09-16 17:57:25 -0700678}
679
680/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000681 * Pop the context stack.
682 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000683static void ctx_pop(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000684{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000685 Context *c = cstk;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000686
687 cstk = cstk->next;
H. Peter Anvin166c2472008-05-28 12:28:58 -0700688 free_smacro_table(&c->localmac);
H. Peter Anvin734b1882002-04-30 21:01:08 +0000689 nasm_free(c->name);
690 nasm_free(c);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000691}
692
H. Peter Anvin072771e2008-05-22 13:17:51 -0700693/*
694 * Search for a key in the hash index; adding it if necessary
695 * (in which case we initialize the data pointer to NULL.)
696 */
697static void **
698hash_findi_add(struct hash_table *hash, const char *str)
699{
700 struct hash_insert hi;
701 void **r;
702 char *strx;
703
704 r = hash_findi(hash, str, &hi);
705 if (r)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300706 return r;
H. Peter Anvin072771e2008-05-22 13:17:51 -0700707
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300708 strx = nasm_strdup(str); /* Use a more efficient allocator here? */
H. Peter Anvin072771e2008-05-22 13:17:51 -0700709 return hash_add(&hi, strx, NULL);
710}
711
712/*
713 * Like hash_findi, but returns the data element rather than a pointer
714 * to it. Used only when not adding a new element, hence no third
715 * argument.
716 */
717static void *
718hash_findix(struct hash_table *hash, const char *str)
719{
720 void **p;
721
722 p = hash_findi(hash, str, NULL);
723 return p ? *p : NULL;
724}
725
Cyrill Gorcunov15bdc512010-07-13 11:27:41 +0400726/*
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800727 * read line from standart macros set,
Cyrill Gorcunov15bdc512010-07-13 11:27:41 +0400728 * if there no more left -- return NULL
729 */
730static char *line_from_stdmac(void)
731{
732 unsigned char c;
733 const unsigned char *p = stdmacpos;
734 char *line, *q;
735 size_t len = 0;
736
737 if (!stdmacpos)
738 return NULL;
739
740 while ((c = *p++)) {
741 if (c >= 0x80)
742 len += pp_directives_len[c - 0x80] + 1;
743 else
744 len++;
745 }
746
747 line = nasm_malloc(len + 1);
748 q = line;
749 while ((c = *stdmacpos++)) {
750 if (c >= 0x80) {
751 memcpy(q, pp_directives[c - 0x80], pp_directives_len[c - 0x80]);
752 q += pp_directives_len[c - 0x80];
753 *q++ = ' ';
754 } else {
755 *q++ = c;
756 }
757 }
758 stdmacpos = p;
759 *q = '\0';
760
761 if (!*stdmacpos) {
H. Peter Anvinf7606612016-07-13 14:23:48 -0700762 /* This was the last of this particular macro set */
Cyrill Gorcunov15bdc512010-07-13 11:27:41 +0400763 stdmacpos = NULL;
H. Peter Anvinf7606612016-07-13 14:23:48 -0700764 if (*stdmacnext) {
765 stdmacpos = *stdmacnext++;
Cyrill Gorcunov15bdc512010-07-13 11:27:41 +0400766 } else if (do_predef) {
767 Line *pd, *l;
768 Token *head, **tail, *t;
769
770 /*
771 * Nasty hack: here we push the contents of
772 * `predef' on to the top-level expansion stack,
773 * since this is the most convenient way to
774 * implement the pre-include and pre-define
775 * features.
776 */
777 list_for_each(pd, predef) {
778 head = NULL;
779 tail = &head;
780 list_for_each(t, pd->first) {
781 *tail = new_Token(NULL, t->type, t->text, 0);
782 tail = &(*tail)->next;
783 }
784
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800785 l = nasm_malloc(sizeof(Line));
786 l->next = istk->expansion;
787 l->first = head;
788 l->finishes = NULL;
789
790 istk->expansion = l;
Cyrill Gorcunov15bdc512010-07-13 11:27:41 +0400791 }
792 do_predef = false;
793 }
794 }
795
796 return line;
797}
798
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000799static char *read_line(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000800{
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +0400801 unsigned int size, c, next;
802 const unsigned int delta = 512;
803 const unsigned int pad = 8;
804 unsigned int nr_cont = 0;
805 bool cont = false;
806 char *buffer, *p;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000807
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +0400808 /* Standart macros set (predefined) goes first */
Cyrill Gorcunov15bdc512010-07-13 11:27:41 +0400809 p = line_from_stdmac();
810 if (p)
811 return p;
H. Peter Anvin72edbb82008-06-19 16:00:04 -0700812
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +0400813 size = delta;
814 p = buffer = nasm_malloc(size);
815
816 for (;;) {
817 c = fgetc(istk->fp);
818 if ((int)(c) == EOF) {
819 p[0] = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000820 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000821 }
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +0400822
823 switch (c) {
824 case '\r':
825 next = fgetc(istk->fp);
826 if (next != '\n')
827 ungetc(next, istk->fp);
828 if (cont) {
829 cont = false;
830 continue;
831 }
832 break;
833
834 case '\n':
835 if (cont) {
836 cont = false;
837 continue;
838 }
839 break;
840
841 case '\\':
842 next = fgetc(istk->fp);
843 ungetc(next, istk->fp);
Cyrill Gorcunov490f85e2012-12-27 20:02:17 +0400844 if (next == '\r' || next == '\n') {
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +0400845 cont = true;
846 nr_cont++;
847 continue;
848 }
849 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000850 }
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +0400851
852 if (c == '\r' || c == '\n') {
853 *p++ = 0;
854 break;
855 }
856
857 if (p >= (buffer + size - pad)) {
858 buffer = nasm_realloc(buffer, size + delta);
859 p = buffer + size - pad;
860 size += delta;
861 }
862
863 *p++ = (unsigned char)c;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000864 }
865
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +0400866 if (p == buffer) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000867 nasm_free(buffer);
868 return NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000869 }
870
Cyrill Gorcunova5aea572010-11-11 10:14:45 +0300871 src_set_linnum(src_get_linnum() + istk->lineinc +
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +0400872 (nr_cont * istk->lineinc));
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000873
874 /*
875 * Handle spurious ^Z, which may be inserted into source files
876 * by some file transfer utilities.
877 */
878 buffer[strcspn(buffer, "\032")] = '\0';
879
H. Peter Anvin8ac25aa2016-02-18 01:16:18 -0800880 lfmt->line(LIST_READ, buffer);
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000881
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000882 return buffer;
883}
884
885/*
Keith Kaniosb7a89542007-04-12 02:40:54 +0000886 * Tokenize a line of text. This is a very simple process since we
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000887 * don't need to parse the value out of e.g. numeric tokens: we
888 * simply split one string into many.
889 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000890static Token *tokenize(char *line)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000891{
H. Peter Anvinca544db2008-10-19 19:30:11 -0700892 char c, *p = line;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +0000893 enum pp_token_type type;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000894 Token *list = NULL;
895 Token *t, **tail = &list;
896
H. Peter Anvine2c80182005-01-15 22:15:51 +0000897 while (*line) {
898 p = line;
899 if (*p == '%') {
900 p++;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300901 if (*p == '+' && !nasm_isdigit(p[1])) {
902 p++;
903 type = TOK_PASTE;
904 } else if (nasm_isdigit(*p) ||
905 ((*p == '-' || *p == '+') && nasm_isdigit(p[1]))) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000906 do {
907 p++;
908 }
H. Peter Anvinbda7a6e2008-06-21 10:23:17 -0700909 while (nasm_isdigit(*p));
H. Peter Anvine2c80182005-01-15 22:15:51 +0000910 type = TOK_PREPROC_ID;
911 } else if (*p == '{') {
912 p++;
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800913 while (*p) {
914 if (*p == '}')
915 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000916 p[-1] = *p;
917 p++;
918 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800919 if (*p != '}')
H. Peter Anvin130736c2016-02-17 20:27:41 -0800920 nasm_error(ERR_WARNING | ERR_PASS1,
921 "unterminated %%{ construct");
H. Peter Anvine2c80182005-01-15 22:15:51 +0000922 p[-1] = '\0';
923 if (*p)
924 p++;
925 type = TOK_PREPROC_ID;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300926 } else if (*p == '[') {
927 int lvl = 1;
928 line += 2; /* Skip the leading %[ */
929 p++;
930 while (lvl && (c = *p++)) {
931 switch (c) {
932 case ']':
933 lvl--;
934 break;
935 case '%':
936 if (*p == '[')
937 lvl++;
938 break;
939 case '\'':
940 case '\"':
941 case '`':
Cyrill Gorcunovc6360a72010-07-13 13:32:19 +0400942 p = nasm_skip_string(p - 1) + 1;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300943 break;
944 default:
945 break;
946 }
947 }
948 p--;
949 if (*p)
950 *p++ = '\0';
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800951 if (lvl)
H. Peter Anvin130736c2016-02-17 20:27:41 -0800952 nasm_error(ERR_NONFATAL|ERR_PASS1,
953 "unterminated %%[ construct");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300954 type = TOK_INDIRECT;
955 } else if (*p == '?') {
956 type = TOK_PREPROC_Q; /* %? */
957 p++;
958 if (*p == '?') {
959 type = TOK_PREPROC_QQ; /* %?? */
960 p++;
961 }
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +0400962 } else if (*p == '!') {
963 type = TOK_PREPROC_ID;
964 p++;
965 if (isidchar(*p)) {
966 do {
967 p++;
968 }
969 while (isidchar(*p));
970 } else if (*p == '\'' || *p == '\"' || *p == '`') {
971 p = nasm_skip_string(p);
972 if (*p)
973 p++;
974 else
H. Peter Anvin130736c2016-02-17 20:27:41 -0800975 nasm_error(ERR_NONFATAL|ERR_PASS1,
976 "unterminated %%! string");
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +0400977 } else {
978 /* %! without string or identifier */
979 type = TOK_OTHER; /* Legacy behavior... */
980 }
H. Peter Anvine2c80182005-01-15 22:15:51 +0000981 } else if (isidchar(*p) ||
982 ((*p == '!' || *p == '%' || *p == '$') &&
983 isidchar(p[1]))) {
984 do {
985 p++;
986 }
987 while (isidchar(*p));
988 type = TOK_PREPROC_ID;
989 } else {
990 type = TOK_OTHER;
991 if (*p == '%')
992 p++;
993 }
994 } else if (isidstart(*p) || (*p == '$' && isidstart(p[1]))) {
995 type = TOK_ID;
996 p++;
997 while (*p && isidchar(*p))
998 p++;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -0700999 } else if (*p == '\'' || *p == '"' || *p == '`') {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001000 /*
1001 * A string token.
1002 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001003 type = TOK_STRING;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001004 p = nasm_skip_string(p);
Nickolay Yurchenkof3b3ce22003-09-21 20:38:43 +00001005
H. Peter Anvine2c80182005-01-15 22:15:51 +00001006 if (*p) {
1007 p++;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001008 } else {
H. Peter Anvin130736c2016-02-17 20:27:41 -08001009 nasm_error(ERR_WARNING|ERR_PASS1, "unterminated string");
H. Peter Anvine2c80182005-01-15 22:15:51 +00001010 /* Handling unterminated strings by UNV */
1011 /* type = -1; */
1012 }
Victor van den Elzenfb5f2512009-04-17 16:17:59 +02001013 } else if (p[0] == '$' && p[1] == '$') {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001014 type = TOK_OTHER; /* TOKEN_BASE */
Victor van den Elzenfb5f2512009-04-17 16:17:59 +02001015 p += 2;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001016 } else if (isnumstart(*p)) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001017 bool is_hex = false;
1018 bool is_float = false;
1019 bool has_e = false;
1020 char c, *r;
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001021
H. Peter Anvine2c80182005-01-15 22:15:51 +00001022 /*
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001023 * A numeric token.
H. Peter Anvine2c80182005-01-15 22:15:51 +00001024 */
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001025
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001026 if (*p == '$') {
1027 p++;
1028 is_hex = true;
1029 }
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001030
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001031 for (;;) {
1032 c = *p++;
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001033
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001034 if (!is_hex && (c == 'e' || c == 'E')) {
1035 has_e = true;
1036 if (*p == '+' || *p == '-') {
1037 /*
1038 * e can only be followed by +/- if it is either a
1039 * prefixed hex number or a floating-point number
1040 */
1041 p++;
1042 is_float = true;
1043 }
1044 } else if (c == 'H' || c == 'h' || c == 'X' || c == 'x') {
1045 is_hex = true;
1046 } else if (c == 'P' || c == 'p') {
1047 is_float = true;
1048 if (*p == '+' || *p == '-')
1049 p++;
1050 } else if (isnumchar(c) || c == '_')
1051 ; /* just advance */
1052 else if (c == '.') {
1053 /*
1054 * we need to deal with consequences of the legacy
1055 * parser, like "1.nolist" being two tokens
1056 * (TOK_NUMBER, TOK_ID) here; at least give it
1057 * a shot for now. In the future, we probably need
1058 * a flex-based scanner with proper pattern matching
1059 * to do it as well as it can be done. Nothing in
1060 * the world is going to help the person who wants
1061 * 0x123.p16 interpreted as two tokens, though.
1062 */
1063 r = p;
1064 while (*r == '_')
1065 r++;
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001066
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001067 if (nasm_isdigit(*r) || (is_hex && nasm_isxdigit(*r)) ||
1068 (!is_hex && (*r == 'e' || *r == 'E')) ||
1069 (*r == 'p' || *r == 'P')) {
1070 p = r;
1071 is_float = true;
1072 } else
1073 break; /* Terminate the token */
1074 } else
1075 break;
1076 }
1077 p--; /* Point to first character beyond number */
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001078
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001079 if (p == line+1 && *line == '$') {
1080 type = TOK_OTHER; /* TOKEN_HERE */
1081 } else {
1082 if (has_e && !is_hex) {
1083 /* 1e13 is floating-point, but 1e13h is not */
1084 is_float = true;
1085 }
H. Peter Anvind784a082009-04-20 14:01:18 -07001086
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001087 type = is_float ? TOK_FLOAT : TOK_NUMBER;
1088 }
H. Peter Anvinbda7a6e2008-06-21 10:23:17 -07001089 } else if (nasm_isspace(*p)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001090 type = TOK_WHITESPACE;
Cyrill Gorcunovf66ac7d2009-10-12 20:41:13 +04001091 p = nasm_skip_spaces(p);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001092 /*
1093 * Whitespace just before end-of-line is discarded by
1094 * pretending it's a comment; whitespace just before a
1095 * comment gets lumped into the comment.
1096 */
1097 if (!*p || *p == ';') {
1098 type = TOK_COMMENT;
1099 while (*p)
1100 p++;
1101 }
1102 } else if (*p == ';') {
1103 type = TOK_COMMENT;
1104 while (*p)
1105 p++;
1106 } else {
1107 /*
1108 * Anything else is an operator of some kind. We check
1109 * for all the double-character operators (>>, <<, //,
1110 * %%, <=, >=, ==, !=, <>, &&, ||, ^^), but anything
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001111 * else is a single-character operator.
H. Peter Anvine2c80182005-01-15 22:15:51 +00001112 */
1113 type = TOK_OTHER;
1114 if ((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[0] == '=' && p[1] == '=') ||
1120 (p[0] == '!' && p[1] == '=') ||
1121 (p[0] == '<' && p[1] == '>') ||
1122 (p[0] == '&' && p[1] == '&') ||
1123 (p[0] == '|' && p[1] == '|') ||
1124 (p[0] == '^' && p[1] == '^')) {
1125 p++;
1126 }
1127 p++;
1128 }
Nickolay Yurchenkof3b3ce22003-09-21 20:38:43 +00001129
H. Peter Anvine2c80182005-01-15 22:15:51 +00001130 /* Handling unterminated string by UNV */
1131 /*if (type == -1)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001132 {
1133 *tail = t = new_Token(NULL, TOK_STRING, line, p-line+1);
1134 t->text[p-line] = *line;
1135 tail = &t->next;
1136 }
1137 else */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001138 if (type != TOK_COMMENT) {
1139 *tail = t = new_Token(NULL, type, line, p - line);
1140 tail = &t->next;
1141 }
1142 line = p;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001143 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001144 return list;
1145}
1146
H. Peter Anvince616072002-04-30 21:02:23 +00001147/*
1148 * this function allocates a new managed block of memory and
H. Peter Anvin70653092007-10-19 14:42:29 -07001149 * returns a pointer to the block. The managed blocks are
H. Peter Anvince616072002-04-30 21:02:23 +00001150 * deleted only all at once by the delete_Blocks function.
1151 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001152static void *new_Block(size_t size)
H. Peter Anvince616072002-04-30 21:02:23 +00001153{
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001154 Blocks *b = &blocks;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001155
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001156 /* first, get to the end of the linked list */
1157 while (b->next)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001158 b = b->next;
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001159 /* now allocate the requested chunk */
1160 b->chunk = nasm_malloc(size);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001161
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001162 /* now allocate a new block for the next request */
Cyrill Gorcunovc31767c2014-06-28 02:22:17 +04001163 b->next = nasm_zalloc(sizeof(Blocks));
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001164 return b->chunk;
H. Peter Anvince616072002-04-30 21:02:23 +00001165}
1166
1167/*
1168 * this function deletes all managed blocks of memory
1169 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001170static void delete_Blocks(void)
H. Peter Anvince616072002-04-30 21:02:23 +00001171{
H. Peter Anvine2c80182005-01-15 22:15:51 +00001172 Blocks *a, *b = &blocks;
H. Peter Anvince616072002-04-30 21:02:23 +00001173
H. Peter Anvin70653092007-10-19 14:42:29 -07001174 /*
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001175 * keep in mind that the first block, pointed to by blocks
H. Peter Anvin70653092007-10-19 14:42:29 -07001176 * is a static and not dynamically allocated, so we don't
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001177 * free it.
1178 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001179 while (b) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001180 if (b->chunk)
1181 nasm_free(b->chunk);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001182 a = b;
1183 b = b->next;
1184 if (a != &blocks)
1185 nasm_free(a);
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001186 }
Cyrill Gorcunovdae24d72014-06-28 10:17:39 +04001187 memset(&blocks, 0, sizeof(blocks));
H. Peter Anvine2c80182005-01-15 22:15:51 +00001188}
H. Peter Anvin734b1882002-04-30 21:01:08 +00001189
1190/*
H. Peter Anvin70653092007-10-19 14:42:29 -07001191 * this function creates a new Token and passes a pointer to it
H. Peter Anvin734b1882002-04-30 21:01:08 +00001192 * back to the caller. It sets the type and text elements, and
H. Peter Anvinf26e0972008-07-01 21:26:27 -07001193 * also the a.mac and next elements to NULL.
H. Peter Anvin734b1882002-04-30 21:01:08 +00001194 */
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07001195static Token *new_Token(Token * next, enum pp_token_type type,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001196 const char *text, int txtlen)
H. Peter Anvin734b1882002-04-30 21:01:08 +00001197{
1198 Token *t;
1199 int i;
1200
H. Peter Anvin89cee572009-07-15 09:16:54 -04001201 if (!freeTokens) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001202 freeTokens = (Token *) new_Block(TOKEN_BLOCKSIZE * sizeof(Token));
1203 for (i = 0; i < TOKEN_BLOCKSIZE - 1; i++)
1204 freeTokens[i].next = &freeTokens[i + 1];
1205 freeTokens[i].next = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001206 }
1207 t = freeTokens;
1208 freeTokens = t->next;
1209 t->next = next;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07001210 t->a.mac = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001211 t->type = type;
H. Peter Anvin89cee572009-07-15 09:16:54 -04001212 if (type == TOK_WHITESPACE || !text) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001213 t->text = NULL;
1214 } else {
1215 if (txtlen == 0)
1216 txtlen = strlen(text);
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07001217 t->text = nasm_malloc(txtlen+1);
1218 memcpy(t->text, text, txtlen);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001219 t->text[txtlen] = '\0';
H. Peter Anvin734b1882002-04-30 21:01:08 +00001220 }
1221 return t;
1222}
1223
H. Peter Anvine2c80182005-01-15 22:15:51 +00001224static Token *delete_Token(Token * t)
H. Peter Anvin734b1882002-04-30 21:01:08 +00001225{
1226 Token *next = t->next;
1227 nasm_free(t->text);
H. Peter Anvin788e6c12002-04-30 21:02:01 +00001228 t->next = freeTokens;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001229 freeTokens = t;
1230 return next;
1231}
1232
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001233/*
1234 * Convert a line of tokens back into text.
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001235 * If expand_locals is not zero, identifiers of the form "%$*xxx"
1236 * will be transformed into ..@ctxnum.xxx
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001237 */
H. Peter Anvin9e200162008-06-04 17:23:14 -07001238static char *detoken(Token * tlist, bool expand_locals)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001239{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001240 Token *t;
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001241 char *line, *p;
H. Peter Anvinb4daadc2008-01-21 16:31:57 -08001242 const char *q;
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001243 int len = 0;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001244
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001245 list_for_each(t, tlist) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001246 if (t->type == TOK_PREPROC_ID && t->text[1] == '!') {
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001247 char *v;
1248 char *q = t->text;
H. Peter Anvin077fb932010-07-20 14:56:30 -07001249
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001250 v = t->text + 2;
1251 if (*v == '\'' || *v == '\"' || *v == '`') {
1252 size_t len = nasm_unquote(v, NULL);
1253 size_t clen = strlen(v);
H. Peter Anvin077fb932010-07-20 14:56:30 -07001254
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001255 if (len != clen) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08001256 nasm_error(ERR_NONFATAL | ERR_PASS1,
1257 "NUL character in %%! string");
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001258 v = NULL;
1259 }
1260 }
H. Peter Anvin077fb932010-07-20 14:56:30 -07001261
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001262 if (v) {
1263 char *p = getenv(v);
1264 if (!p) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08001265 nasm_error(ERR_NONFATAL | ERR_PASS1,
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001266 "nonexistent environment variable `%s'", v);
1267 p = "";
1268 }
1269 t->text = nasm_strdup(p);
1270 }
1271 nasm_free(q);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001272 }
H. Peter Anvin077fb932010-07-20 14:56:30 -07001273
H. Peter Anvine2c80182005-01-15 22:15:51 +00001274 /* Expand local macros here and not during preprocessing */
1275 if (expand_locals &&
1276 t->type == TOK_PREPROC_ID && t->text &&
1277 t->text[0] == '%' && t->text[1] == '$') {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001278 const char *q;
1279 char *p;
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04001280 Context *ctx = get_ctx(t->text, &q);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001281 if (ctx) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001282 char buffer[40];
Keith Kanios93f2e9a2007-04-14 00:10:59 +00001283 snprintf(buffer, sizeof(buffer), "..@%"PRIu32".", ctx->number);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001284 p = nasm_strcat(buffer, q);
1285 nasm_free(t->text);
1286 t->text = p;
1287 }
1288 }
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001289 if (t->type == TOK_WHITESPACE)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001290 len++;
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001291 else if (t->text)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001292 len += strlen(t->text);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001293 }
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001294
H. Peter Anvin734b1882002-04-30 21:01:08 +00001295 p = line = nasm_malloc(len + 1);
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001296
1297 list_for_each(t, tlist) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001298 if (t->type == TOK_WHITESPACE) {
H. Peter Anvinb4daadc2008-01-21 16:31:57 -08001299 *p++ = ' ';
H. Peter Anvine2c80182005-01-15 22:15:51 +00001300 } else if (t->text) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001301 q = t->text;
1302 while (*q)
1303 *p++ = *q++;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001304 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001305 }
1306 *p = '\0';
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001307
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001308 return line;
1309}
1310
1311/*
H. Peter Anvin76690a12002-04-30 20:52:49 +00001312 * A scanner, suitable for use by the expression evaluator, which
1313 * operates on a line of Tokens. Expects a pointer to a pointer to
1314 * the first token in the line to be passed in as its private_data
1315 * field.
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001316 *
1317 * FIX: This really needs to be unified with stdscan.
H. Peter Anvin76690a12002-04-30 20:52:49 +00001318 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001319static int ppscan(void *private_data, struct tokenval *tokval)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001320{
H. Peter Anvin76690a12002-04-30 20:52:49 +00001321 Token **tlineptr = private_data;
1322 Token *tline;
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001323 char ourcopy[MAX_KEYWORD+1], *p, *r, *s;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001324
H. Peter Anvine2c80182005-01-15 22:15:51 +00001325 do {
1326 tline = *tlineptr;
1327 *tlineptr = tline ? tline->next : NULL;
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04001328 } while (tline && (tline->type == TOK_WHITESPACE ||
1329 tline->type == TOK_COMMENT));
H. Peter Anvin76690a12002-04-30 20:52:49 +00001330
1331 if (!tline)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001332 return tokval->t_type = TOKEN_EOS;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001333
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001334 tokval->t_charptr = tline->text;
1335
H. Peter Anvin76690a12002-04-30 20:52:49 +00001336 if (tline->text[0] == '$' && !tline->text[1])
H. Peter Anvine2c80182005-01-15 22:15:51 +00001337 return tokval->t_type = TOKEN_HERE;
H. Peter Anvin7cf897e2002-05-30 21:30:33 +00001338 if (tline->text[0] == '$' && tline->text[1] == '$' && !tline->text[2])
H. Peter Anvine2c80182005-01-15 22:15:51 +00001339 return tokval->t_type = TOKEN_BASE;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001340
H. Peter Anvine2c80182005-01-15 22:15:51 +00001341 if (tline->type == TOK_ID) {
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001342 p = tokval->t_charptr = tline->text;
1343 if (p[0] == '$') {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001344 tokval->t_charptr++;
1345 return tokval->t_type = TOKEN_ID;
1346 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00001347
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001348 for (r = p, s = ourcopy; *r; r++) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001349 if (r >= p+MAX_KEYWORD)
1350 return tokval->t_type = TOKEN_ID; /* Not a keyword */
H. Peter Anvinac8f8fc2008-06-11 15:49:41 -07001351 *s++ = nasm_tolower(*r);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001352 }
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001353 *s = '\0';
1354 /* right, so we have an identifier sitting in temp storage. now,
1355 * is it actually a register or instruction name, or what? */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001356 return nasm_token_hash(ourcopy, tokval);
H. Peter Anvin76690a12002-04-30 20:52:49 +00001357 }
1358
H. Peter Anvine2c80182005-01-15 22:15:51 +00001359 if (tline->type == TOK_NUMBER) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001360 bool rn_error;
1361 tokval->t_integer = readnum(tline->text, &rn_error);
1362 tokval->t_charptr = tline->text;
1363 if (rn_error)
1364 return tokval->t_type = TOKEN_ERRNUM;
1365 else
1366 return tokval->t_type = TOKEN_NUM;
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001367 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00001368
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001369 if (tline->type == TOK_FLOAT) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001370 return tokval->t_type = TOKEN_FLOAT;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001371 }
1372
H. Peter Anvine2c80182005-01-15 22:15:51 +00001373 if (tline->type == TOK_STRING) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001374 char bq, *ep;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001375
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001376 bq = tline->text[0];
H. Peter Anvin11627042008-06-09 20:45:19 -07001377 tokval->t_charptr = tline->text;
1378 tokval->t_inttwo = nasm_unquote(tline->text, &ep);
H. Peter Anvind2456592008-06-19 15:04:18 -07001379
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001380 if (ep[0] != bq || ep[1] != '\0')
1381 return tokval->t_type = TOKEN_ERRSTR;
1382 else
1383 return tokval->t_type = TOKEN_STR;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001384 }
1385
H. Peter Anvine2c80182005-01-15 22:15:51 +00001386 if (tline->type == TOK_OTHER) {
1387 if (!strcmp(tline->text, "<<"))
1388 return tokval->t_type = TOKEN_SHL;
1389 if (!strcmp(tline->text, ">>"))
1390 return tokval->t_type = TOKEN_SHR;
1391 if (!strcmp(tline->text, "//"))
1392 return tokval->t_type = TOKEN_SDIV;
1393 if (!strcmp(tline->text, "%%"))
1394 return tokval->t_type = TOKEN_SMOD;
1395 if (!strcmp(tline->text, "=="))
1396 return tokval->t_type = TOKEN_EQ;
1397 if (!strcmp(tline->text, "<>"))
1398 return tokval->t_type = TOKEN_NE;
1399 if (!strcmp(tline->text, "!="))
1400 return tokval->t_type = TOKEN_NE;
1401 if (!strcmp(tline->text, "<="))
1402 return tokval->t_type = TOKEN_LE;
1403 if (!strcmp(tline->text, ">="))
1404 return tokval->t_type = TOKEN_GE;
1405 if (!strcmp(tline->text, "&&"))
1406 return tokval->t_type = TOKEN_DBL_AND;
1407 if (!strcmp(tline->text, "^^"))
1408 return tokval->t_type = TOKEN_DBL_XOR;
1409 if (!strcmp(tline->text, "||"))
1410 return tokval->t_type = TOKEN_DBL_OR;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001411 }
1412
1413 /*
1414 * We have no other options: just return the first character of
1415 * the token text.
1416 */
1417 return tokval->t_type = tline->text[0];
1418}
1419
1420/*
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001421 * Compare a string to the name of an existing macro; this is a
1422 * simple wrapper which calls either strcmp or nasm_stricmp
1423 * depending on the value of the `casesense' parameter.
1424 */
H. Peter Anvin4db5a162007-10-11 13:42:09 -07001425static int mstrcmp(const char *p, const char *q, bool casesense)
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001426{
H. Peter Anvin734b1882002-04-30 21:01:08 +00001427 return casesense ? strcmp(p, q) : nasm_stricmp(p, q);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001428}
1429
1430/*
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07001431 * Compare a string to the name of an existing macro; this is a
1432 * simple wrapper which calls either strcmp or nasm_stricmp
1433 * depending on the value of the `casesense' parameter.
1434 */
1435static int mmemcmp(const char *p, const char *q, size_t l, bool casesense)
1436{
1437 return casesense ? memcmp(p, q, l) : nasm_memicmp(p, q, l);
1438}
1439
1440/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001441 * Return the Context structure associated with a %$ token. Return
1442 * NULL, having _already_ reported an error condition, if the
1443 * context stack isn't deep enough for the supplied number of $
1444 * signs.
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001445 *
1446 * If "namep" is non-NULL, set it to the pointer to the macro name
1447 * tail, i.e. the part beyond %$...
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001448 */
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04001449static Context *get_ctx(const char *name, const char **namep)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001450{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001451 Context *ctx;
1452 int i;
1453
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001454 if (namep)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001455 *namep = name;
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001456
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001457 if (!name || name[0] != '%' || name[1] != '$')
H. Peter Anvine2c80182005-01-15 22:15:51 +00001458 return NULL;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001459
H. Peter Anvine2c80182005-01-15 22:15:51 +00001460 if (!cstk) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08001461 nasm_error(ERR_NONFATAL, "`%s': context stack is empty", name);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001462 return NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001463 }
1464
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001465 name += 2;
1466 ctx = cstk;
1467 i = 0;
1468 while (ctx && *name == '$') {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001469 name++;
1470 i++;
1471 ctx = ctx->next;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001472 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001473 if (!ctx) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08001474 nasm_error(ERR_NONFATAL, "`%s': context stack is only"
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001475 " %d level%s deep", name, i, (i == 1 ? "" : "s"));
H. Peter Anvine2c80182005-01-15 22:15:51 +00001476 return NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001477 }
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001478
1479 if (namep)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001480 *namep = name;
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001481
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04001482 return ctx;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001483}
1484
1485/*
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001486 * Check to see if a file is already in a string list
1487 */
1488static bool in_list(const StrList *list, const char *str)
1489{
1490 while (list) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001491 if (!strcmp(list->str, str))
1492 return true;
1493 list = list->next;
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001494 }
1495 return false;
1496}
1497
1498/*
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001499 * Open an include file. This routine must always return a valid
1500 * file pointer if it returns - it's responsible for throwing an
1501 * ERR_FATAL and bombing out completely if not. It should also try
1502 * the include path one by one until it finds the file or reaches
1503 * the end of the path.
1504 */
H. Peter Anvin2b1c3b92008-06-06 10:38:46 -07001505static FILE *inc_fopen(const char *file, StrList **dhead, StrList ***dtail,
H. Peter Anvin66d561f2016-06-16 15:47:46 -07001506 char **found_path, bool missing_ok, enum file_flags mode)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001507{
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001508 FILE *fp;
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001509 char *prefix = "";
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001510 IncPath *ip = ipath;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001511 int len = strlen(file);
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001512 size_t prefix_len = 0;
1513 StrList *sl;
Jim Kukunas65a8afc2016-06-13 16:00:42 -04001514 size_t path_len;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001515
H. Peter Anvine2c80182005-01-15 22:15:51 +00001516 while (1) {
Jim Kukunas65a8afc2016-06-13 16:00:42 -04001517 path_len = prefix_len + len + 1;
1518
1519 sl = nasm_malloc(path_len + sizeof sl->next);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001520 memcpy(sl->str, prefix, prefix_len);
1521 memcpy(sl->str+prefix_len, file, len+1);
Jim Kukunas65a8afc2016-06-13 16:00:42 -04001522
1523 if (found_path != NULL) {
1524 *found_path = nasm_malloc(path_len);
1525 memcpy(*found_path, sl->str, path_len);
1526 }
1527
H. Peter Anvin3e83cec2016-05-25 04:28:46 -07001528 fp = nasm_open_read(sl->str, mode);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001529 if (fp && dhead && !in_list(*dhead, sl->str)) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001530 sl->next = NULL;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001531 **dtail = sl;
1532 *dtail = &sl->next;
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001533 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001534 nasm_free(sl);
1535 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001536 if (fp)
1537 return fp;
Jim Kukunas65a8afc2016-06-13 16:00:42 -04001538
1539 if (found_path != NULL && *found_path != NULL) {
1540 nasm_free(*found_path);
H. Peter Anvinf500d832016-06-16 15:28:09 -07001541 *found_path = NULL;
Jim Kukunas65a8afc2016-06-13 16:00:42 -04001542 }
1543
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001544 if (!ip) {
1545 if (!missing_ok)
1546 break;
1547 prefix = NULL;
1548 } else {
1549 prefix = ip->path;
1550 ip = ip->next;
1551 }
1552 if (prefix) {
1553 prefix_len = strlen(prefix);
1554 } else {
1555 /* -MG given and file not found */
1556 if (dhead && !in_list(*dhead, file)) {
1557 sl = nasm_malloc(len+1+sizeof sl->next);
1558 sl->next = NULL;
1559 strcpy(sl->str, file);
1560 **dtail = sl;
1561 *dtail = &sl->next;
1562 }
1563 return NULL;
1564 }
H. Peter Anvineba20a72002-04-30 20:53:55 +00001565 }
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001566
H. Peter Anvin130736c2016-02-17 20:27:41 -08001567 nasm_error(ERR_FATAL, "unable to open include file `%s'", file);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001568 return NULL;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001569}
1570
1571/*
Fabian Giesen0bbc38d2016-04-28 13:48:14 -07001572 * Opens an include or input file. Public version, for use by modules
1573 * that get a file:lineno pair and need to look at the file again
1574 * (e.g. the CodeView debug backend). Returns NULL on failure.
1575 */
H. Peter Anvin3e83cec2016-05-25 04:28:46 -07001576FILE *pp_input_fopen(const char *filename, enum file_flags mode)
Fabian Giesen0bbc38d2016-04-28 13:48:14 -07001577{
1578 FILE *fp;
1579 StrList *xsl = NULL;
1580 StrList **xst = &xsl;
1581
Jim Kukunas65a8afc2016-06-13 16:00:42 -04001582 fp = inc_fopen(filename, &xsl, &xst, NULL, true, mode);
Fabian Giesen0bbc38d2016-04-28 13:48:14 -07001583 if (xsl)
1584 nasm_free(xsl);
1585 return fp;
1586}
1587
1588/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001589 * Determine if we should warn on defining a single-line macro of
H. Peter Anvinef7468f2002-04-30 20:57:59 +00001590 * name `name', with `nparam' parameters. If nparam is 0 or -1, will
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001591 * return true if _any_ single-line macro of that name is defined.
1592 * Otherwise, will return true if a single-line macro with either
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001593 * `nparam' or no parameters is defined.
1594 *
1595 * If a macro with precisely the right number of parameters is
H. Peter Anvinef7468f2002-04-30 20:57:59 +00001596 * defined, or nparam is -1, the address of the definition structure
1597 * will be returned in `defn'; otherwise NULL will be returned. If `defn'
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001598 * is NULL, no action will be taken regarding its contents, and no
1599 * error will occur.
1600 *
1601 * Note that this is also called with nparam zero to resolve
1602 * `ifdef'.
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001603 *
1604 * If you already know which context macro belongs to, you can pass
1605 * the context pointer as first parameter; if you won't but name begins
1606 * with %$ the context will be automatically computed. If all_contexts
1607 * is true, macro will be searched in outer contexts as well.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001608 */
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07001609static bool
H. Peter Anvinb2a5fda2008-06-19 21:42:42 -07001610smacro_defined(Context * ctx, const char *name, int nparam, SMacro ** defn,
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07001611 bool nocase)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001612{
H. Peter Anvin166c2472008-05-28 12:28:58 -07001613 struct hash_table *smtbl;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001614 SMacro *m;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001615
H. Peter Anvin97a23472007-09-16 17:57:25 -07001616 if (ctx) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001617 smtbl = &ctx->localmac;
H. Peter Anvin97a23472007-09-16 17:57:25 -07001618 } else if (name[0] == '%' && name[1] == '$') {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001619 if (cstk)
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04001620 ctx = get_ctx(name, &name);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001621 if (!ctx)
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001622 return false; /* got to return _something_ */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001623 smtbl = &ctx->localmac;
H. Peter Anvin97a23472007-09-16 17:57:25 -07001624 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001625 smtbl = &smacros;
H. Peter Anvin97a23472007-09-16 17:57:25 -07001626 }
H. Peter Anvin166c2472008-05-28 12:28:58 -07001627 m = (SMacro *) hash_findix(smtbl, name);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001628
H. Peter Anvine2c80182005-01-15 22:15:51 +00001629 while (m) {
1630 if (!mstrcmp(m->name, name, m->casesense && nocase) &&
Charles Crayne192d5b52007-10-18 19:02:42 -07001631 (nparam <= 0 || m->nparam == 0 || nparam == (int) m->nparam)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001632 if (defn) {
Charles Crayne192d5b52007-10-18 19:02:42 -07001633 if (nparam == (int) m->nparam || nparam == -1)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001634 *defn = m;
1635 else
1636 *defn = NULL;
1637 }
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001638 return true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001639 }
1640 m = m->next;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001641 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001642
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001643 return false;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001644}
1645
1646/*
1647 * Count and mark off the parameters in a multi-line macro call.
1648 * This is called both from within the multi-line macro expansion
1649 * code, and also to mark off the default parameters when provided
1650 * in a %macro definition line.
1651 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001652static void count_mmac_params(Token * t, int *nparam, Token *** params)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001653{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001654 int paramsize, brace;
1655
1656 *nparam = paramsize = 0;
1657 *params = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001658 while (t) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001659 /* +1: we need space for the final NULL */
H. Peter Anvin91fb6f12008-09-01 10:56:33 -07001660 if (*nparam+1 >= paramsize) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001661 paramsize += PARAM_DELTA;
1662 *params = nasm_realloc(*params, sizeof(**params) * paramsize);
1663 }
1664 skip_white_(t);
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08001665 brace = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001666 if (tok_is_(t, "{"))
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08001667 brace++;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001668 (*params)[(*nparam)++] = t;
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08001669 if (brace) {
1670 while (brace && (t = t->next) != NULL) {
1671 if (tok_is_(t, "{"))
1672 brace++;
1673 else if (tok_is_(t, "}"))
1674 brace--;
1675 }
1676
1677 if (t) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001678 /*
1679 * Now we've found the closing brace, look further
1680 * for the comma.
1681 */
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08001682 t = t->next;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001683 skip_white_(t);
1684 if (tok_isnt_(t, ",")) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08001685 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00001686 "braces do not enclose all of macro parameter");
1687 while (tok_isnt_(t, ","))
1688 t = t->next;
1689 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001690 }
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08001691 } else {
1692 while (tok_isnt_(t, ","))
1693 t = t->next;
1694 }
1695 if (t) { /* got a comma/brace */
1696 t = t->next; /* eat the comma */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001697 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001698 }
1699}
1700
1701/*
H. Peter Anvin76690a12002-04-30 20:52:49 +00001702 * Determine whether one of the various `if' conditions is true or
1703 * not.
1704 *
1705 * We must free the tline we get passed.
1706 */
H. Peter Anvin70055962007-10-11 00:05:31 -07001707static bool if_condition(Token * tline, enum preproc_token ct)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001708{
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001709 enum pp_conditional i = PP_COND(ct);
H. Peter Anvin70055962007-10-11 00:05:31 -07001710 bool j;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001711 Token *t, *tt, **tptr, *origline;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001712 struct tokenval tokval;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001713 expr *evalresult;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001714 enum pp_token_type needtype;
H. Peter Anvin077fb932010-07-20 14:56:30 -07001715 char *p;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001716
1717 origline = tline;
1718
H. Peter Anvine2c80182005-01-15 22:15:51 +00001719 switch (i) {
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001720 case PPC_IFCTX:
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001721 j = false; /* have we matched yet? */
Victor van den Elzen0e857f12008-07-23 13:21:29 +02001722 while (true) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001723 skip_white_(tline);
Victor van den Elzen0e857f12008-07-23 13:21:29 +02001724 if (!tline)
1725 break;
1726 if (tline->type != TOK_ID) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08001727 nasm_error(ERR_NONFATAL,
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001728 "`%s' expects context identifiers", pp_directives[ct]);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001729 free_tlist(origline);
1730 return -1;
1731 }
Victor van den Elzen0e857f12008-07-23 13:21:29 +02001732 if (cstk && cstk->name && !nasm_stricmp(tline->text, cstk->name))
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001733 j = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001734 tline = tline->next;
1735 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001736 break;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001737
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001738 case PPC_IFDEF:
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001739 j = false; /* have we matched yet? */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001740 while (tline) {
1741 skip_white_(tline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001742 if (!tline || (tline->type != TOK_ID &&
1743 (tline->type != TOK_PREPROC_ID ||
1744 tline->text[1] != '$'))) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08001745 nasm_error(ERR_NONFATAL,
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001746 "`%s' expects macro identifiers", pp_directives[ct]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001747 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001748 }
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07001749 if (smacro_defined(NULL, tline->text, 0, NULL, true))
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001750 j = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001751 tline = tline->next;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001752 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001753 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001754
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001755 case PPC_IFENV:
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001756 tline = expand_smacro(tline);
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001757 j = false; /* have we matched yet? */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001758 while (tline) {
1759 skip_white_(tline);
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001760 if (!tline || (tline->type != TOK_ID &&
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001761 tline->type != TOK_STRING &&
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001762 (tline->type != TOK_PREPROC_ID ||
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001763 tline->text[1] != '!'))) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08001764 nasm_error(ERR_NONFATAL,
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001765 "`%s' expects environment variable names",
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001766 pp_directives[ct]);
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001767 goto fail;
1768 }
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001769 p = tline->text;
1770 if (tline->type == TOK_PREPROC_ID)
1771 p += 2; /* Skip leading %! */
1772 if (*p == '\'' || *p == '\"' || *p == '`')
1773 nasm_unquote_cstr(p, ct);
1774 if (getenv(p))
1775 j = true;
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001776 tline = tline->next;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001777 }
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001778 break;
1779
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001780 case PPC_IFIDN:
1781 case PPC_IFIDNI:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001782 tline = expand_smacro(tline);
1783 t = tt = tline;
1784 while (tok_isnt_(tt, ","))
1785 tt = tt->next;
1786 if (!tt) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08001787 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00001788 "`%s' expects two comma-separated arguments",
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001789 pp_directives[ct]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001790 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001791 }
1792 tt = tt->next;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001793 j = true; /* assume equality unless proved not */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001794 while ((t->type != TOK_OTHER || strcmp(t->text, ",")) && tt) {
1795 if (tt->type == TOK_OTHER && !strcmp(tt->text, ",")) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08001796 nasm_error(ERR_NONFATAL, "`%s': more than one comma on line",
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001797 pp_directives[ct]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001798 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001799 }
1800 if (t->type == TOK_WHITESPACE) {
1801 t = t->next;
1802 continue;
1803 }
1804 if (tt->type == TOK_WHITESPACE) {
1805 tt = tt->next;
1806 continue;
1807 }
1808 if (tt->type != t->type) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001809 j = false; /* found mismatching tokens */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001810 break;
1811 }
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07001812 /* When comparing strings, need to unquote them first */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001813 if (t->type == TOK_STRING) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001814 size_t l1 = nasm_unquote(t->text, NULL);
1815 size_t l2 = nasm_unquote(tt->text, NULL);
H. Peter Anvind2456592008-06-19 15:04:18 -07001816
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001817 if (l1 != l2) {
1818 j = false;
1819 break;
1820 }
1821 if (mmemcmp(t->text, tt->text, l1, i == PPC_IFIDN)) {
1822 j = false;
1823 break;
1824 }
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07001825 } else if (mstrcmp(tt->text, t->text, i == PPC_IFIDN) != 0) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001826 j = false; /* found mismatching tokens */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001827 break;
1828 }
Nickolay Yurchenkof3b3ce22003-09-21 20:38:43 +00001829
H. Peter Anvine2c80182005-01-15 22:15:51 +00001830 t = t->next;
1831 tt = tt->next;
1832 }
1833 if ((t->type != TOK_OTHER || strcmp(t->text, ",")) || tt)
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001834 j = false; /* trailing gunk on one end or other */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001835 break;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001836
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001837 case PPC_IFMACRO:
H. Peter Anvin89cee572009-07-15 09:16:54 -04001838 {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001839 bool found = false;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001840 MMacro searching, *mmac;
H. Peter Anvin65747262002-05-07 00:10:05 +00001841
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001842 skip_white_(tline);
1843 tline = expand_id(tline);
1844 if (!tok_type_(tline, TOK_ID)) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08001845 nasm_error(ERR_NONFATAL,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001846 "`%s' expects a macro name", pp_directives[ct]);
1847 goto fail;
1848 }
1849 searching.name = nasm_strdup(tline->text);
1850 searching.casesense = true;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001851 searching.plus = false;
1852 searching.nolist = false;
1853 searching.in_progress = 0;
1854 searching.max_depth = 0;
1855 searching.rep_nest = NULL;
1856 searching.nparam_min = 0;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001857 searching.nparam_max = INT_MAX;
1858 tline = expand_smacro(tline->next);
1859 skip_white_(tline);
1860 if (!tline) {
1861 } else if (!tok_type_(tline, TOK_NUMBER)) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08001862 nasm_error(ERR_NONFATAL,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001863 "`%s' expects a parameter count or nothing",
1864 pp_directives[ct]);
1865 } else {
1866 searching.nparam_min = searching.nparam_max =
1867 readnum(tline->text, &j);
1868 if (j)
H. Peter Anvin130736c2016-02-17 20:27:41 -08001869 nasm_error(ERR_NONFATAL,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001870 "unable to parse parameter count `%s'",
1871 tline->text);
1872 }
1873 if (tline && tok_is_(tline->next, "-")) {
1874 tline = tline->next->next;
1875 if (tok_is_(tline, "*"))
1876 searching.nparam_max = INT_MAX;
1877 else if (!tok_type_(tline, TOK_NUMBER))
H. Peter Anvin130736c2016-02-17 20:27:41 -08001878 nasm_error(ERR_NONFATAL,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001879 "`%s' expects a parameter count after `-'",
1880 pp_directives[ct]);
1881 else {
1882 searching.nparam_max = readnum(tline->text, &j);
1883 if (j)
H. Peter Anvin130736c2016-02-17 20:27:41 -08001884 nasm_error(ERR_NONFATAL,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001885 "unable to parse parameter count `%s'",
1886 tline->text);
1887 if (searching.nparam_min > searching.nparam_max)
H. Peter Anvin130736c2016-02-17 20:27:41 -08001888 nasm_error(ERR_NONFATAL,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001889 "minimum parameter count exceeds maximum");
1890 }
1891 }
1892 if (tline && tok_is_(tline->next, "+")) {
1893 tline = tline->next;
1894 searching.plus = true;
1895 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001896 mmac = (MMacro *) hash_findix(&mmacros, searching.name);
1897 while (mmac) {
1898 if (!strcmp(mmac->name, searching.name) &&
1899 (mmac->nparam_min <= searching.nparam_max
1900 || searching.plus)
1901 && (searching.nparam_min <= mmac->nparam_max
1902 || mmac->plus)) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001903 found = true;
1904 break;
1905 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001906 mmac = mmac->next;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001907 }
1908 if (tline && tline->next)
H. Peter Anvin130736c2016-02-17 20:27:41 -08001909 nasm_error(ERR_WARNING|ERR_PASS1,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001910 "trailing garbage after %%ifmacro ignored");
1911 nasm_free(searching.name);
1912 j = found;
1913 break;
H. Peter Anvin89cee572009-07-15 09:16:54 -04001914 }
H. Peter Anvin65747262002-05-07 00:10:05 +00001915
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001916 case PPC_IFID:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001917 needtype = TOK_ID;
1918 goto iftype;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001919 case PPC_IFNUM:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001920 needtype = TOK_NUMBER;
1921 goto iftype;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001922 case PPC_IFSTR:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001923 needtype = TOK_STRING;
1924 goto iftype;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001925
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001926iftype:
1927 t = tline = expand_smacro(tline);
H. Peter Anvind85d2502008-05-04 17:53:31 -07001928
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001929 while (tok_type_(t, TOK_WHITESPACE) ||
1930 (needtype == TOK_NUMBER &&
1931 tok_type_(t, TOK_OTHER) &&
1932 (t->text[0] == '-' || t->text[0] == '+') &&
1933 !t->text[1]))
1934 t = t->next;
H. Peter Anvind85d2502008-05-04 17:53:31 -07001935
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001936 j = tok_type_(t, needtype);
1937 break;
H. Peter Anvincbf768d2008-02-16 16:41:25 -08001938
1939 case PPC_IFTOKEN:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001940 t = tline = expand_smacro(tline);
1941 while (tok_type_(t, TOK_WHITESPACE))
1942 t = t->next;
H. Peter Anvincbf768d2008-02-16 16:41:25 -08001943
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001944 j = false;
1945 if (t) {
1946 t = t->next; /* Skip the actual token */
1947 while (tok_type_(t, TOK_WHITESPACE))
1948 t = t->next;
1949 j = !t; /* Should be nothing left */
1950 }
1951 break;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001952
H. Peter Anvin134b9462008-02-16 17:01:40 -08001953 case PPC_IFEMPTY:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001954 t = tline = expand_smacro(tline);
1955 while (tok_type_(t, TOK_WHITESPACE))
1956 t = t->next;
H. Peter Anvin134b9462008-02-16 17:01:40 -08001957
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001958 j = !t; /* Should be empty */
1959 break;
H. Peter Anvin134b9462008-02-16 17:01:40 -08001960
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001961 case PPC_IF:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001962 t = tline = expand_smacro(tline);
1963 tptr = &t;
1964 tokval.t_type = TOKEN_INVALID;
1965 evalresult = evaluate(ppscan, tptr, &tokval,
H. Peter Anvin130736c2016-02-17 20:27:41 -08001966 NULL, pass | CRITICAL, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001967 if (!evalresult)
1968 return -1;
1969 if (tokval.t_type)
H. Peter Anvin130736c2016-02-17 20:27:41 -08001970 nasm_error(ERR_WARNING|ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +00001971 "trailing garbage after expression ignored");
1972 if (!is_simple(evalresult)) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08001973 nasm_error(ERR_NONFATAL,
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001974 "non-constant value given to `%s'", pp_directives[ct]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001975 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001976 }
Chuck Crayne60ae75d2007-05-02 01:59:16 +00001977 j = reloc_value(evalresult) != 0;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001978 break;
H. Peter Anvin95e28822007-09-12 04:20:08 +00001979
H. Peter Anvine2c80182005-01-15 22:15:51 +00001980 default:
H. Peter Anvin130736c2016-02-17 20:27:41 -08001981 nasm_error(ERR_FATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00001982 "preprocessor directive `%s' not yet implemented",
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001983 pp_directives[ct]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001984 goto fail;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001985 }
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001986
1987 free_tlist(origline);
1988 return j ^ PP_NEGATIVE(ct);
H. Peter Anvin70653092007-10-19 14:42:29 -07001989
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001990fail:
1991 free_tlist(origline);
1992 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001993}
1994
1995/*
H. Peter Anvin4db5a162007-10-11 13:42:09 -07001996 * Common code for defining an smacro
1997 */
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001998static bool define_smacro(Context *ctx, const char *mname, bool casesense,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001999 int nparam, Token *expansion)
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002000{
2001 SMacro *smac, **smhead;
H. Peter Anvin166c2472008-05-28 12:28:58 -07002002 struct hash_table *smtbl;
H. Peter Anvin70653092007-10-19 14:42:29 -07002003
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002004 if (smacro_defined(ctx, mname, nparam, &smac, casesense)) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002005 if (!smac) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002006 nasm_error(ERR_WARNING|ERR_PASS1,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002007 "single-line macro `%s' defined both with and"
2008 " without parameters", mname);
2009 /*
2010 * Some instances of the old code considered this a failure,
2011 * some others didn't. What is the right thing to do here?
2012 */
2013 free_tlist(expansion);
2014 return false; /* Failure */
2015 } else {
2016 /*
2017 * We're redefining, so we have to take over an
2018 * existing SMacro structure. This means freeing
2019 * what was already in it.
2020 */
2021 nasm_free(smac->name);
2022 free_tlist(smac->expansion);
2023 }
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002024 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002025 smtbl = ctx ? &ctx->localmac : &smacros;
2026 smhead = (SMacro **) hash_findi_add(smtbl, mname);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002027 smac = nasm_malloc(sizeof(SMacro));
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002028 smac->next = *smhead;
2029 *smhead = smac;
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002030 }
2031 smac->name = nasm_strdup(mname);
2032 smac->casesense = casesense;
2033 smac->nparam = nparam;
2034 smac->expansion = expansion;
2035 smac->in_progress = false;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002036 return true; /* Success */
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002037}
2038
2039/*
2040 * Undefine an smacro
2041 */
2042static void undef_smacro(Context *ctx, const char *mname)
2043{
2044 SMacro **smhead, *s, **sp;
H. Peter Anvin166c2472008-05-28 12:28:58 -07002045 struct hash_table *smtbl;
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002046
H. Peter Anvin166c2472008-05-28 12:28:58 -07002047 smtbl = ctx ? &ctx->localmac : &smacros;
2048 smhead = (SMacro **)hash_findi(smtbl, mname, NULL);
H. Peter Anvin70653092007-10-19 14:42:29 -07002049
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002050 if (smhead) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002051 /*
2052 * We now have a macro name... go hunt for it.
2053 */
2054 sp = smhead;
2055 while ((s = *sp) != NULL) {
2056 if (!mstrcmp(s->name, mname, s->casesense)) {
2057 *sp = s->next;
2058 nasm_free(s->name);
2059 free_tlist(s->expansion);
2060 nasm_free(s);
2061 } else {
2062 sp = &s->next;
2063 }
2064 }
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002065 }
2066}
2067
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002068/*
H. Peter Anvina26433d2008-07-16 14:40:01 -07002069 * Parse a mmacro specification.
2070 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002071static bool parse_mmacro_spec(Token *tline, MMacro *def, const char *directive)
H. Peter Anvina26433d2008-07-16 14:40:01 -07002072{
2073 bool err;
2074
2075 tline = tline->next;
2076 skip_white_(tline);
2077 tline = expand_id(tline);
2078 if (!tok_type_(tline, TOK_ID)) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002079 nasm_error(ERR_NONFATAL, "`%s' expects a macro name", directive);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002080 return false;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002081 }
Victor van den Elzenb916ede2008-07-23 15:14:22 +02002082
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002083 def->prev = NULL;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002084 def->name = nasm_strdup(tline->text);
2085 def->plus = false;
2086 def->nolist = false;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002087 def->in_progress = 0;
2088 def->rep_nest = NULL;
Victor van den Elzenb916ede2008-07-23 15:14:22 +02002089 def->nparam_min = 0;
2090 def->nparam_max = 0;
2091
H. Peter Anvina26433d2008-07-16 14:40:01 -07002092 tline = expand_smacro(tline->next);
2093 skip_white_(tline);
2094 if (!tok_type_(tline, TOK_NUMBER)) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002095 nasm_error(ERR_NONFATAL, "`%s' expects a parameter count", directive);
H. Peter Anvina26433d2008-07-16 14:40:01 -07002096 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002097 def->nparam_min = def->nparam_max =
2098 readnum(tline->text, &err);
2099 if (err)
H. Peter Anvin130736c2016-02-17 20:27:41 -08002100 nasm_error(ERR_NONFATAL,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002101 "unable to parse parameter count `%s'", tline->text);
H. Peter Anvina26433d2008-07-16 14:40:01 -07002102 }
2103 if (tline && tok_is_(tline->next, "-")) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002104 tline = tline->next->next;
2105 if (tok_is_(tline, "*")) {
2106 def->nparam_max = INT_MAX;
2107 } else if (!tok_type_(tline, TOK_NUMBER)) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002108 nasm_error(ERR_NONFATAL,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002109 "`%s' expects a parameter count after `-'", directive);
2110 } else {
2111 def->nparam_max = readnum(tline->text, &err);
2112 if (err) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002113 nasm_error(ERR_NONFATAL, "unable to parse parameter count `%s'",
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002114 tline->text);
2115 }
2116 if (def->nparam_min > def->nparam_max) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002117 nasm_error(ERR_NONFATAL, "minimum parameter count exceeds maximum");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002118 }
2119 }
H. Peter Anvina26433d2008-07-16 14:40:01 -07002120 }
2121 if (tline && tok_is_(tline->next, "+")) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002122 tline = tline->next;
2123 def->plus = true;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002124 }
2125 if (tline && tok_type_(tline->next, TOK_ID) &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002126 !nasm_stricmp(tline->next->text, ".nolist")) {
2127 tline = tline->next;
2128 def->nolist = true;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002129 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002130
H. Peter Anvina26433d2008-07-16 14:40:01 -07002131 /*
2132 * Handle default parameters.
2133 */
2134 if (tline && tline->next) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002135 def->dlist = tline->next;
2136 tline->next = NULL;
2137 count_mmac_params(def->dlist, &def->ndefs, &def->defaults);
H. Peter Anvina26433d2008-07-16 14:40:01 -07002138 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002139 def->dlist = NULL;
2140 def->defaults = NULL;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002141 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002142 def->expansion = NULL;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002143
H. Peter Anvin89cee572009-07-15 09:16:54 -04002144 if (def->defaults && def->ndefs > def->nparam_max - def->nparam_min &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002145 !def->plus)
H. Peter Anvin130736c2016-02-17 20:27:41 -08002146 nasm_error(ERR_WARNING|ERR_PASS1|ERR_WARN_MDP,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002147 "too many default macro parameters");
Victor van den Elzenb916ede2008-07-23 15:14:22 +02002148
H. Peter Anvina26433d2008-07-16 14:40:01 -07002149 return true;
2150}
2151
2152
2153/*
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002154 * Decode a size directive
2155 */
2156static int parse_size(const char *str) {
2157 static const char *size_names[] =
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002158 { "byte", "dword", "oword", "qword", "tword", "word", "yword" };
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002159 static const int sizes[] =
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002160 { 0, 1, 4, 16, 8, 10, 2, 32 };
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002161
Cyrill Gorcunova7319242010-06-03 22:04:36 +04002162 return sizes[bsii(str, size_names, ARRAY_SIZE(size_names))+1];
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002163}
2164
Ed Beroset3ab3f412002-06-11 03:31:49 +00002165/**
2166 * find and process preprocessor directive in passed line
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002167 * Find out if a line contains a preprocessor directive, and deal
2168 * with it if so.
H. Peter Anvin70653092007-10-19 14:42:29 -07002169 *
Ed Beroset3ab3f412002-06-11 03:31:49 +00002170 * If a directive _is_ found, it is the responsibility of this routine
2171 * (and not the caller) to free_tlist() the line.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002172 *
Ed Beroset3ab3f412002-06-11 03:31:49 +00002173 * @param tline a pointer to the current tokeninzed line linked list
2174 * @return DIRECTIVE_FOUND or NO_DIRECTIVE_FOUND
H. Peter Anvin70653092007-10-19 14:42:29 -07002175 *
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002176 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002177static int do_directive(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00002178{
H. Peter Anvin4169a472007-09-12 01:29:43 +00002179 enum preproc_token i;
2180 int j;
H. Peter Anvin70055962007-10-11 00:05:31 -07002181 bool err;
2182 int nparam;
2183 bool nolist;
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07002184 bool casesense;
H. Peter Anvin8cfdb9d2007-09-14 18:36:01 -07002185 int k, m;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002186 int offset;
Jim Kukunas65a8afc2016-06-13 16:00:42 -04002187 char *p, *pp, *found_path;
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08002188 const char *mname;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002189 Include *inc;
2190 Context *ctx;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002191 Cond *cond;
2192 MMacro *mmac, **mmhead;
Jin Kyu Songc9486b92013-10-28 17:07:57 -07002193 Token *t = NULL, *tt, *param_start, *macro_start, *last, **tptr, *origline;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002194 Line *l;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002195 struct tokenval tokval;
2196 expr *evalresult;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002197 MMacro *tmp_defining; /* Used when manipulating rep_nest */
H. Peter Anvinf8ba53e2007-10-11 10:11:57 -07002198 int64_t count;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07002199 size_t len;
H. Peter Anvin8e3f75e2008-09-24 00:21:58 -07002200 int severity;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002201
2202 origline = tline;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002203
H. Peter Anvineba20a72002-04-30 20:53:55 +00002204 skip_white_(tline);
H. Peter Anvinf2936d72008-06-04 15:11:23 -07002205 if (!tline || !tok_type_(tline, TOK_PREPROC_ID) ||
H. Peter Anvine2c80182005-01-15 22:15:51 +00002206 (tline->text[1] == '%' || tline->text[1] == '$'
2207 || tline->text[1] == '!'))
2208 return NO_DIRECTIVE_FOUND;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002209
H. Peter Anvin4169a472007-09-12 01:29:43 +00002210 i = pp_token_hash(tline->text);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002211
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002212 /*
2213 * FIXME: We zap execution of PP_RMACRO, PP_IRMACRO, PP_EXITMACRO
2214 * since they are known to be buggy at moment, we need to fix them
2215 * in future release (2.09-2.10)
2216 */
Philipp Klokeb432f572013-03-31 12:01:23 +02002217 if (i == PP_RMACRO || i == PP_IRMACRO || i == PP_EXITMACRO) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002218 nasm_error(ERR_NONFATAL, "unknown preprocessor directive `%s'",
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002219 tline->text);
2220 return NO_DIRECTIVE_FOUND;
2221 }
2222
2223 /*
2224 * If we're in a non-emitting branch of a condition construct,
2225 * or walking to the end of an already terminated %rep block,
2226 * we should ignore all directives except for condition
2227 * directives.
2228 */
2229 if (((istk->conds && !emitting(istk->conds->state)) ||
2230 (istk->mstk && !istk->mstk->in_progress)) && !is_condition(i)) {
2231 return NO_DIRECTIVE_FOUND;
2232 }
2233
2234 /*
2235 * If we're defining a macro or reading a %rep block, we should
2236 * ignore all directives except for %macro/%imacro (which nest),
2237 * %endm/%endmacro, and (only if we're in a %rep block) %endrep.
2238 * If we're in a %rep block, another %rep nests, so should be let through.
2239 */
2240 if (defining && i != PP_MACRO && i != PP_IMACRO &&
2241 i != PP_RMACRO && i != PP_IRMACRO &&
2242 i != PP_ENDMACRO && i != PP_ENDM &&
2243 (defining->name || (i != PP_ENDREP && i != PP_REP))) {
2244 return NO_DIRECTIVE_FOUND;
2245 }
2246
2247 if (defining) {
2248 if (i == PP_MACRO || i == PP_IMACRO ||
2249 i == PP_RMACRO || i == PP_IRMACRO) {
2250 nested_mac_count++;
2251 return NO_DIRECTIVE_FOUND;
2252 } else if (nested_mac_count > 0) {
2253 if (i == PP_ENDMACRO) {
2254 nested_mac_count--;
2255 return NO_DIRECTIVE_FOUND;
2256 }
2257 }
2258 if (!defining->name) {
2259 if (i == PP_REP) {
2260 nested_rep_count++;
2261 return NO_DIRECTIVE_FOUND;
2262 } else if (nested_rep_count > 0) {
2263 if (i == PP_ENDREP) {
2264 nested_rep_count--;
2265 return NO_DIRECTIVE_FOUND;
2266 }
2267 }
2268 }
2269 }
2270
H. Peter Anvin4169a472007-09-12 01:29:43 +00002271 switch (i) {
2272 case PP_INVALID:
H. Peter Anvin130736c2016-02-17 20:27:41 -08002273 nasm_error(ERR_NONFATAL, "unknown preprocessor directive `%s'",
H. Peter Anvine2c80182005-01-15 22:15:51 +00002274 tline->text);
2275 return NO_DIRECTIVE_FOUND; /* didn't get it */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002276
H. Peter Anvine2c80182005-01-15 22:15:51 +00002277 case PP_STACKSIZE:
2278 /* Directive to tell NASM what the default stack size is. The
2279 * default is for a 16-bit stack, and this can be overriden with
2280 * %stacksize large.
H. Peter Anvine2c80182005-01-15 22:15:51 +00002281 */
2282 tline = tline->next;
2283 if (tline && tline->type == TOK_WHITESPACE)
2284 tline = tline->next;
2285 if (!tline || tline->type != TOK_ID) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002286 nasm_error(ERR_NONFATAL, "`%%stacksize' missing size parameter");
H. Peter Anvine2c80182005-01-15 22:15:51 +00002287 free_tlist(origline);
2288 return DIRECTIVE_FOUND;
2289 }
2290 if (nasm_stricmp(tline->text, "flat") == 0) {
2291 /* All subsequent ARG directives are for a 32-bit stack */
2292 StackSize = 4;
2293 StackPointer = "ebp";
2294 ArgOffset = 8;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002295 LocalOffset = 0;
Charles Crayne7eaf9192007-11-08 22:11:14 -08002296 } else if (nasm_stricmp(tline->text, "flat64") == 0) {
2297 /* All subsequent ARG directives are for a 64-bit stack */
2298 StackSize = 8;
2299 StackPointer = "rbp";
Per Jessen53252e02010-02-11 00:16:59 +03002300 ArgOffset = 16;
Charles Crayne7eaf9192007-11-08 22:11:14 -08002301 LocalOffset = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002302 } else if (nasm_stricmp(tline->text, "large") == 0) {
2303 /* All subsequent ARG directives are for a 16-bit stack,
2304 * far function call.
2305 */
2306 StackSize = 2;
2307 StackPointer = "bp";
2308 ArgOffset = 4;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002309 LocalOffset = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002310 } else if (nasm_stricmp(tline->text, "small") == 0) {
2311 /* All subsequent ARG directives are for a 16-bit stack,
2312 * far function call. We don't support near functions.
2313 */
2314 StackSize = 2;
2315 StackPointer = "bp";
2316 ArgOffset = 6;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002317 LocalOffset = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002318 } else {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002319 nasm_error(ERR_NONFATAL, "`%%stacksize' invalid size type");
H. Peter Anvine2c80182005-01-15 22:15:51 +00002320 free_tlist(origline);
2321 return DIRECTIVE_FOUND;
2322 }
2323 free_tlist(origline);
2324 return DIRECTIVE_FOUND;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002325
H. Peter Anvine2c80182005-01-15 22:15:51 +00002326 case PP_ARG:
2327 /* TASM like ARG directive to define arguments to functions, in
2328 * the following form:
2329 *
2330 * ARG arg1:WORD, arg2:DWORD, arg4:QWORD
2331 */
2332 offset = ArgOffset;
2333 do {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00002334 char *arg, directive[256];
H. Peter Anvine2c80182005-01-15 22:15:51 +00002335 int size = StackSize;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002336
H. Peter Anvine2c80182005-01-15 22:15:51 +00002337 /* Find the argument name */
2338 tline = tline->next;
2339 if (tline && tline->type == TOK_WHITESPACE)
2340 tline = tline->next;
2341 if (!tline || tline->type != TOK_ID) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002342 nasm_error(ERR_NONFATAL, "`%%arg' missing argument parameter");
H. Peter Anvine2c80182005-01-15 22:15:51 +00002343 free_tlist(origline);
2344 return DIRECTIVE_FOUND;
2345 }
2346 arg = tline->text;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002347
H. Peter Anvine2c80182005-01-15 22:15:51 +00002348 /* Find the argument size type */
2349 tline = tline->next;
2350 if (!tline || tline->type != TOK_OTHER
2351 || tline->text[0] != ':') {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002352 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00002353 "Syntax error processing `%%arg' directive");
2354 free_tlist(origline);
2355 return DIRECTIVE_FOUND;
2356 }
2357 tline = tline->next;
2358 if (!tline || tline->type != TOK_ID) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002359 nasm_error(ERR_NONFATAL, "`%%arg' missing size type parameter");
H. Peter Anvine2c80182005-01-15 22:15:51 +00002360 free_tlist(origline);
2361 return DIRECTIVE_FOUND;
2362 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002363
H. Peter Anvine2c80182005-01-15 22:15:51 +00002364 /* Allow macro expansion of type parameter */
Keith Kaniosb7a89542007-04-12 02:40:54 +00002365 tt = tokenize(tline->text);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002366 tt = expand_smacro(tt);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002367 size = parse_size(tt->text);
2368 if (!size) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002369 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00002370 "Invalid size type for `%%arg' missing directive");
2371 free_tlist(tt);
2372 free_tlist(origline);
2373 return DIRECTIVE_FOUND;
2374 }
2375 free_tlist(tt);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002376
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002377 /* Round up to even stack slots */
2378 size = ALIGN(size, StackSize);
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002379
H. Peter Anvine2c80182005-01-15 22:15:51 +00002380 /* Now define the macro for the argument */
2381 snprintf(directive, sizeof(directive), "%%define %s (%s+%d)",
2382 arg, StackPointer, offset);
Keith Kaniosb7a89542007-04-12 02:40:54 +00002383 do_directive(tokenize(directive));
H. Peter Anvine2c80182005-01-15 22:15:51 +00002384 offset += size;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002385
H. Peter Anvine2c80182005-01-15 22:15:51 +00002386 /* Move to the next argument in the list */
2387 tline = tline->next;
2388 if (tline && tline->type == TOK_WHITESPACE)
2389 tline = tline->next;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002390 } while (tline && tline->type == TOK_OTHER && tline->text[0] == ',');
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002391 ArgOffset = offset;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002392 free_tlist(origline);
2393 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002394
H. Peter Anvine2c80182005-01-15 22:15:51 +00002395 case PP_LOCAL:
2396 /* TASM like LOCAL directive to define local variables for a
2397 * function, in the following form:
2398 *
2399 * LOCAL local1:WORD, local2:DWORD, local4:QWORD = LocalSize
2400 *
2401 * The '= LocalSize' at the end is ignored by NASM, but is
2402 * required by TASM to define the local parameter size (and used
2403 * by the TASM macro package).
2404 */
2405 offset = LocalOffset;
2406 do {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00002407 char *local, directive[256];
H. Peter Anvine2c80182005-01-15 22:15:51 +00002408 int size = StackSize;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002409
H. Peter Anvine2c80182005-01-15 22:15:51 +00002410 /* Find the argument name */
2411 tline = tline->next;
2412 if (tline && tline->type == TOK_WHITESPACE)
2413 tline = tline->next;
2414 if (!tline || tline->type != TOK_ID) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002415 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00002416 "`%%local' missing argument parameter");
2417 free_tlist(origline);
2418 return DIRECTIVE_FOUND;
2419 }
2420 local = tline->text;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002421
H. Peter Anvine2c80182005-01-15 22:15:51 +00002422 /* Find the argument size type */
2423 tline = tline->next;
2424 if (!tline || tline->type != TOK_OTHER
2425 || tline->text[0] != ':') {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002426 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00002427 "Syntax error processing `%%local' directive");
2428 free_tlist(origline);
2429 return DIRECTIVE_FOUND;
2430 }
2431 tline = tline->next;
2432 if (!tline || tline->type != TOK_ID) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002433 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00002434 "`%%local' missing size type parameter");
2435 free_tlist(origline);
2436 return DIRECTIVE_FOUND;
2437 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00002438
H. Peter Anvine2c80182005-01-15 22:15:51 +00002439 /* Allow macro expansion of type parameter */
Keith Kaniosb7a89542007-04-12 02:40:54 +00002440 tt = tokenize(tline->text);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002441 tt = expand_smacro(tt);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002442 size = parse_size(tt->text);
2443 if (!size) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002444 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00002445 "Invalid size type for `%%local' missing directive");
2446 free_tlist(tt);
2447 free_tlist(origline);
2448 return DIRECTIVE_FOUND;
2449 }
2450 free_tlist(tt);
H. Peter Anvin734b1882002-04-30 21:01:08 +00002451
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002452 /* Round up to even stack slots */
2453 size = ALIGN(size, StackSize);
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002454
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002455 offset += size; /* Negative offset, increment before */
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002456
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002457 /* Now define the macro for the argument */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002458 snprintf(directive, sizeof(directive), "%%define %s (%s-%d)",
2459 local, StackPointer, offset);
Keith Kaniosb7a89542007-04-12 02:40:54 +00002460 do_directive(tokenize(directive));
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002461
H. Peter Anvine2c80182005-01-15 22:15:51 +00002462 /* Now define the assign to setup the enter_c macro correctly */
2463 snprintf(directive, sizeof(directive),
2464 "%%assign %%$localsize %%$localsize+%d", size);
Keith Kaniosb7a89542007-04-12 02:40:54 +00002465 do_directive(tokenize(directive));
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002466
H. Peter Anvine2c80182005-01-15 22:15:51 +00002467 /* Move to the next argument in the list */
2468 tline = tline->next;
2469 if (tline && tline->type == TOK_WHITESPACE)
2470 tline = tline->next;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002471 } while (tline && tline->type == TOK_OTHER && tline->text[0] == ',');
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002472 LocalOffset = offset;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002473 free_tlist(origline);
2474 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002475
H. Peter Anvine2c80182005-01-15 22:15:51 +00002476 case PP_CLEAR:
2477 if (tline->next)
H. Peter Anvin130736c2016-02-17 20:27:41 -08002478 nasm_error(ERR_WARNING|ERR_PASS1,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002479 "trailing garbage after `%%clear' ignored");
2480 free_macros();
2481 init_macros();
H. Peter Anvine2c80182005-01-15 22:15:51 +00002482 free_tlist(origline);
2483 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002484
H. Peter Anvin418ca702008-05-30 10:42:30 -07002485 case PP_DEPEND:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002486 t = tline->next = expand_smacro(tline->next);
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002487 skip_white_(t);
2488 if (!t || (t->type != TOK_STRING &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002489 t->type != TOK_INTERNAL_STRING)) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002490 nasm_error(ERR_NONFATAL, "`%%depend' expects a file name");
H. Peter Anvin418ca702008-05-30 10:42:30 -07002491 free_tlist(origline);
2492 return DIRECTIVE_FOUND; /* but we did _something_ */
2493 }
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002494 if (t->next)
H. Peter Anvin130736c2016-02-17 20:27:41 -08002495 nasm_error(ERR_WARNING|ERR_PASS1,
H. Peter Anvin418ca702008-05-30 10:42:30 -07002496 "trailing garbage after `%%depend' ignored");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002497 p = t->text;
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002498 if (t->type != TOK_INTERNAL_STRING)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002499 nasm_unquote_cstr(p, i);
2500 if (dephead && !in_list(*dephead, p)) {
2501 StrList *sl = nasm_malloc(strlen(p)+1+sizeof sl->next);
2502 sl->next = NULL;
2503 strcpy(sl->str, p);
2504 *deptail = sl;
2505 deptail = &sl->next;
2506 }
2507 free_tlist(origline);
H. Peter Anvin418ca702008-05-30 10:42:30 -07002508 return DIRECTIVE_FOUND;
2509
2510 case PP_INCLUDE:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002511 t = tline->next = expand_smacro(tline->next);
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002512 skip_white_(t);
H. Peter Anvind2456592008-06-19 15:04:18 -07002513
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002514 if (!t || (t->type != TOK_STRING &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002515 t->type != TOK_INTERNAL_STRING)) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002516 nasm_error(ERR_NONFATAL, "`%%include' expects a file name");
H. Peter Anvine2c80182005-01-15 22:15:51 +00002517 free_tlist(origline);
2518 return DIRECTIVE_FOUND; /* but we did _something_ */
2519 }
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002520 if (t->next)
H. Peter Anvin130736c2016-02-17 20:27:41 -08002521 nasm_error(ERR_WARNING|ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +00002522 "trailing garbage after `%%include' ignored");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002523 p = t->text;
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002524 if (t->type != TOK_INTERNAL_STRING)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002525 nasm_unquote_cstr(p, i);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002526 inc = nasm_malloc(sizeof(Include));
H. Peter Anvine2c80182005-01-15 22:15:51 +00002527 inc->next = istk;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002528 inc->conds = NULL;
Jim Kukunas65a8afc2016-06-13 16:00:42 -04002529 found_path = NULL;
H. Peter Anvin66d561f2016-06-16 15:47:46 -07002530 inc->fp = inc_fopen(p, dephead, &deptail, &found_path, pass == 0, NF_TEXT);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002531 if (!inc->fp) {
2532 /* -MG given but file not found */
2533 nasm_free(inc);
2534 } else {
Jim Kukunas65a8afc2016-06-13 16:00:42 -04002535 inc->fname = src_set_fname(found_path ? found_path : p);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002536 inc->lineno = src_set_linnum(0);
2537 inc->lineinc = 1;
2538 inc->expansion = NULL;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002539 inc->mstk = NULL;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002540 istk = inc;
H. Peter Anvin8ac25aa2016-02-18 01:16:18 -08002541 lfmt->uplevel(LIST_INCLUDE);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002542 }
2543 free_tlist(origline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002544 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002545
H. Peter Anvind2456592008-06-19 15:04:18 -07002546 case PP_USE:
H. Peter Anvinf4ae5ad2008-06-19 18:39:24 -07002547 {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002548 static macros_t *use_pkg;
2549 const char *pkg_macro = NULL;
H. Peter Anvinf4ae5ad2008-06-19 18:39:24 -07002550
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002551 tline = tline->next;
2552 skip_white_(tline);
2553 tline = expand_id(tline);
H. Peter Anvind2456592008-06-19 15:04:18 -07002554
H. Peter Anvin264b7b92008-10-24 16:38:17 -07002555 if (!tline || (tline->type != TOK_STRING &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002556 tline->type != TOK_INTERNAL_STRING &&
2557 tline->type != TOK_ID)) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002558 nasm_error(ERR_NONFATAL, "`%%use' expects a package name");
H. Peter Anvind2456592008-06-19 15:04:18 -07002559 free_tlist(origline);
2560 return DIRECTIVE_FOUND; /* but we did _something_ */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002561 }
H. Peter Anvin264b7b92008-10-24 16:38:17 -07002562 if (tline->next)
H. Peter Anvin130736c2016-02-17 20:27:41 -08002563 nasm_error(ERR_WARNING|ERR_PASS1,
H. Peter Anvind2456592008-06-19 15:04:18 -07002564 "trailing garbage after `%%use' ignored");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002565 if (tline->type == TOK_STRING)
2566 nasm_unquote_cstr(tline->text, i);
2567 use_pkg = nasm_stdmac_find_package(tline->text);
2568 if (!use_pkg)
H. Peter Anvin130736c2016-02-17 20:27:41 -08002569 nasm_error(ERR_NONFATAL, "unknown `%%use' package: %s", tline->text);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002570 else
2571 pkg_macro = (char *)use_pkg + 1; /* The first string will be <%define>__USE_*__ */
Victor van den Elzen35eb2ea2010-03-10 22:33:48 +01002572 if (use_pkg && ! smacro_defined(NULL, pkg_macro, 0, NULL, true)) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002573 /* Not already included, go ahead and include it */
2574 stdmacpos = use_pkg;
2575 }
2576 free_tlist(origline);
H. Peter Anvind2456592008-06-19 15:04:18 -07002577 return DIRECTIVE_FOUND;
H. Peter Anvinf4ae5ad2008-06-19 18:39:24 -07002578 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002579 case PP_PUSH:
H. Peter Anvine2c80182005-01-15 22:15:51 +00002580 case PP_REPL:
H. Peter Anvin42b56392008-10-24 16:24:21 -07002581 case PP_POP:
H. Peter Anvine2c80182005-01-15 22:15:51 +00002582 tline = tline->next;
2583 skip_white_(tline);
2584 tline = expand_id(tline);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002585 if (tline) {
2586 if (!tok_type_(tline, TOK_ID)) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002587 nasm_error(ERR_NONFATAL, "`%s' expects a context identifier",
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002588 pp_directives[i]);
2589 free_tlist(origline);
2590 return DIRECTIVE_FOUND; /* but we did _something_ */
2591 }
2592 if (tline->next)
H. Peter Anvin130736c2016-02-17 20:27:41 -08002593 nasm_error(ERR_WARNING|ERR_PASS1,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002594 "trailing garbage after `%s' ignored",
2595 pp_directives[i]);
2596 p = nasm_strdup(tline->text);
2597 } else {
2598 p = NULL; /* Anonymous */
2599 }
H. Peter Anvin42b56392008-10-24 16:24:21 -07002600
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002601 if (i == PP_PUSH) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002602 ctx = nasm_malloc(sizeof(Context));
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002603 ctx->next = cstk;
2604 hash_init(&ctx->localmac, HASH_SMALL);
2605 ctx->name = p;
2606 ctx->number = unique++;
2607 cstk = ctx;
2608 } else {
2609 /* %pop or %repl */
2610 if (!cstk) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002611 nasm_error(ERR_NONFATAL, "`%s': context stack is empty",
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002612 pp_directives[i]);
2613 } else if (i == PP_POP) {
2614 if (p && (!cstk->name || nasm_stricmp(p, cstk->name)))
H. Peter Anvin130736c2016-02-17 20:27:41 -08002615 nasm_error(ERR_NONFATAL, "`%%pop' in wrong context: %s, "
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002616 "expected %s",
2617 cstk->name ? cstk->name : "anonymous", p);
2618 else
2619 ctx_pop();
2620 } else {
2621 /* i == PP_REPL */
2622 nasm_free(cstk->name);
2623 cstk->name = p;
2624 p = NULL;
2625 }
2626 nasm_free(p);
2627 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002628 free_tlist(origline);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002629 return DIRECTIVE_FOUND;
H. Peter Anvin8e3f75e2008-09-24 00:21:58 -07002630 case PP_FATAL:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002631 severity = ERR_FATAL;
2632 goto issue_error;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002633 case PP_ERROR:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002634 severity = ERR_NONFATAL;
2635 goto issue_error;
H. Peter Anvin7df04172008-06-10 18:27:38 -07002636 case PP_WARNING:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002637 severity = ERR_WARNING|ERR_WARN_USER;
2638 goto issue_error;
H. Peter Anvin8e3f75e2008-09-24 00:21:58 -07002639
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002640issue_error:
H. Peter Anvin7df04172008-06-10 18:27:38 -07002641 {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002642 /* Only error out if this is the final pass */
2643 if (pass != 2 && i != PP_FATAL)
2644 return DIRECTIVE_FOUND;
2645
2646 tline->next = expand_smacro(tline->next);
2647 tline = tline->next;
2648 skip_white_(tline);
2649 t = tline ? tline->next : NULL;
2650 skip_white_(t);
2651 if (tok_type_(tline, TOK_STRING) && !t) {
2652 /* The line contains only a quoted string */
2653 p = tline->text;
2654 nasm_unquote(p, NULL); /* Ignore NUL character truncation */
H. Peter Anvin130736c2016-02-17 20:27:41 -08002655 nasm_error(severity, "%s", p);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002656 } else {
2657 /* Not a quoted string, or more than a quoted string */
2658 p = detoken(tline, false);
H. Peter Anvin130736c2016-02-17 20:27:41 -08002659 nasm_error(severity, "%s", p);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002660 nasm_free(p);
2661 }
2662 free_tlist(origline);
2663 return DIRECTIVE_FOUND;
H. Peter Anvin7df04172008-06-10 18:27:38 -07002664 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00002665
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002666 CASE_PP_IF:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002667 if (istk->conds && !emitting(istk->conds->state))
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002668 j = COND_NEVER;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002669 else {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002670 j = if_condition(tline->next, i);
2671 tline->next = NULL; /* it got freed */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002672 j = j < 0 ? COND_NEVER : j ? COND_IF_TRUE : COND_IF_FALSE;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002673 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002674 cond = nasm_malloc(sizeof(Cond));
2675 cond->next = istk->conds;
2676 cond->state = j;
2677 istk->conds = cond;
2678 if(istk->mstk)
2679 istk->mstk->condcnt ++;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002680 free_tlist(origline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002681 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002682
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002683 CASE_PP_ELIF:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002684 if (!istk->conds)
H. Peter Anvin130736c2016-02-17 20:27:41 -08002685 nasm_error(ERR_FATAL, "`%s': no matching `%%if'", pp_directives[i]);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002686 switch(istk->conds->state) {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002687 case COND_IF_TRUE:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002688 istk->conds->state = COND_DONE;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002689 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002690
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002691 case COND_DONE:
2692 case COND_NEVER:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002693 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002694
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002695 case COND_ELSE_TRUE:
2696 case COND_ELSE_FALSE:
H. Peter Anvin130736c2016-02-17 20:27:41 -08002697 nasm_error(ERR_WARNING|ERR_PASS1|ERR_PP_PRECOND,
2698 "`%%elif' after `%%else' ignored");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002699 istk->conds->state = COND_NEVER;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002700 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002701
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002702 case COND_IF_FALSE:
2703 /*
2704 * IMPORTANT: In the case of %if, we will already have
2705 * called expand_mmac_params(); however, if we're
2706 * processing an %elif we must have been in a
2707 * non-emitting mode, which would have inhibited
2708 * the normal invocation of expand_mmac_params().
2709 * Therefore, we have to do it explicitly here.
2710 */
2711 j = if_condition(expand_mmac_params(tline->next), i);
2712 tline->next = NULL; /* it got freed */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002713 istk->conds->state =
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002714 j < 0 ? COND_NEVER : j ? COND_IF_TRUE : COND_IF_FALSE;
2715 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002716 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002717 free_tlist(origline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002718 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002719
H. Peter Anvine2c80182005-01-15 22:15:51 +00002720 case PP_ELSE:
2721 if (tline->next)
H. Peter Anvin130736c2016-02-17 20:27:41 -08002722 nasm_error(ERR_WARNING|ERR_PASS1|ERR_PP_PRECOND,
2723 "trailing garbage after `%%else' ignored");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002724 if (!istk->conds)
H. Peter Anvin130736c2016-02-17 20:27:41 -08002725 nasm_fatal(0, "`%%else: no matching `%%if'");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002726 switch(istk->conds->state) {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002727 case COND_IF_TRUE:
2728 case COND_DONE:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002729 istk->conds->state = COND_ELSE_FALSE;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002730 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002731
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002732 case COND_NEVER:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002733 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002734
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002735 case COND_IF_FALSE:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002736 istk->conds->state = COND_ELSE_TRUE;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002737 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002738
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002739 case COND_ELSE_TRUE:
2740 case COND_ELSE_FALSE:
H. Peter Anvin130736c2016-02-17 20:27:41 -08002741 nasm_error(ERR_WARNING|ERR_PASS1|ERR_PP_PRECOND,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002742 "`%%else' after `%%else' ignored.");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002743 istk->conds->state = COND_NEVER;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002744 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002745 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002746 free_tlist(origline);
2747 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002748
H. Peter Anvine2c80182005-01-15 22:15:51 +00002749 case PP_ENDIF:
2750 if (tline->next)
H. Peter Anvin130736c2016-02-17 20:27:41 -08002751 nasm_error(ERR_WARNING|ERR_PASS1|ERR_PP_PRECOND,
2752 "trailing garbage after `%%endif' ignored");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002753 if (!istk->conds)
H. Peter Anvin130736c2016-02-17 20:27:41 -08002754 nasm_error(ERR_FATAL, "`%%endif': no matching `%%if'");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002755 cond = istk->conds;
2756 istk->conds = cond->next;
2757 nasm_free(cond);
2758 if(istk->mstk)
2759 istk->mstk->condcnt --;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002760 free_tlist(origline);
2761 return DIRECTIVE_FOUND;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002762
H. Peter Anvindb8f96e2009-07-15 09:07:29 -04002763 case PP_RMACRO:
2764 case PP_IRMACRO:
H. Peter Anvine2c80182005-01-15 22:15:51 +00002765 case PP_MACRO:
2766 case PP_IMACRO:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002767 if (defining) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002768 nasm_error(ERR_FATAL, "`%s': already defining a macro",
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002769 pp_directives[i]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002770 return DIRECTIVE_FOUND;
2771 }
H. Peter Anvin4def1a82016-05-09 13:59:44 -07002772 defining = nasm_zalloc(sizeof(MMacro));
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002773 defining->max_depth =
2774 (i == PP_RMACRO) || (i == PP_IRMACRO) ? DEADMAN_LIMIT : 0;
2775 defining->casesense = (i == PP_MACRO) || (i == PP_RMACRO);
2776 if (!parse_mmacro_spec(tline, defining, pp_directives[i])) {
2777 nasm_free(defining);
2778 defining = NULL;
2779 return DIRECTIVE_FOUND;
2780 }
H. Peter Anvina26433d2008-07-16 14:40:01 -07002781
H. Peter Anvin4def1a82016-05-09 13:59:44 -07002782 src_get(&defining->xline, &defining->fname);
2783
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002784 mmac = (MMacro *) hash_findix(&mmacros, defining->name);
2785 while (mmac) {
2786 if (!strcmp(mmac->name, defining->name) &&
2787 (mmac->nparam_min <= defining->nparam_max
2788 || defining->plus)
2789 && (defining->nparam_min <= mmac->nparam_max
2790 || mmac->plus)) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002791 nasm_error(ERR_WARNING|ERR_PASS1,
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002792 "redefining multi-line macro `%s'", defining->name);
2793 return DIRECTIVE_FOUND;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002794 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002795 mmac = mmac->next;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002796 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002797 free_tlist(origline);
2798 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002799
H. Peter Anvine2c80182005-01-15 22:15:51 +00002800 case PP_ENDM:
2801 case PP_ENDMACRO:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002802 if (! (defining && defining->name)) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002803 nasm_error(ERR_NONFATAL, "`%s': not defining a macro", tline->text);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002804 return DIRECTIVE_FOUND;
2805 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002806 mmhead = (MMacro **) hash_findi_add(&mmacros, defining->name);
2807 defining->next = *mmhead;
2808 *mmhead = defining;
2809 defining = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002810 free_tlist(origline);
2811 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002812
H. Peter Anvin89cee572009-07-15 09:16:54 -04002813 case PP_EXITMACRO:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002814 /*
2815 * We must search along istk->expansion until we hit a
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002816 * macro-end marker for a macro with a name. Then we
2817 * bypass all lines between exitmacro and endmacro.
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002818 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002819 list_for_each(l, istk->expansion)
2820 if (l->finishes && l->finishes->name)
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002821 break;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002822
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002823 if (l) {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002824 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002825 * Remove all conditional entries relative to this
2826 * macro invocation. (safe to do in this context)
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002827 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002828 for ( ; l->finishes->condcnt > 0; l->finishes->condcnt --) {
2829 cond = istk->conds;
2830 istk->conds = cond->next;
2831 nasm_free(cond);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002832 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002833 istk->expansion = l;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002834 } else {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002835 nasm_error(ERR_NONFATAL, "`%%exitmacro' not within `%%macro' block");
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002836 }
Keith Kanios852f1ee2009-07-12 00:19:55 -05002837 free_tlist(origline);
2838 return DIRECTIVE_FOUND;
2839
H. Peter Anvina26433d2008-07-16 14:40:01 -07002840 case PP_UNMACRO:
2841 case PP_UNIMACRO:
2842 {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002843 MMacro **mmac_p;
2844 MMacro spec;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002845
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002846 spec.casesense = (i == PP_UNMACRO);
2847 if (!parse_mmacro_spec(tline, &spec, pp_directives[i])) {
2848 return DIRECTIVE_FOUND;
2849 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002850 mmac_p = (MMacro **) hash_findi(&mmacros, spec.name, NULL);
2851 while (mmac_p && *mmac_p) {
2852 mmac = *mmac_p;
2853 if (mmac->casesense == spec.casesense &&
2854 !mstrcmp(mmac->name, spec.name, spec.casesense) &&
2855 mmac->nparam_min == spec.nparam_min &&
2856 mmac->nparam_max == spec.nparam_max &&
2857 mmac->plus == spec.plus) {
2858 *mmac_p = mmac->next;
2859 free_mmacro(mmac);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002860 } else {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002861 mmac_p = &mmac->next;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002862 }
2863 }
2864 free_tlist(origline);
2865 free_tlist(spec.dlist);
2866 return DIRECTIVE_FOUND;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002867 }
2868
H. Peter Anvine2c80182005-01-15 22:15:51 +00002869 case PP_ROTATE:
2870 if (tline->next && tline->next->type == TOK_WHITESPACE)
2871 tline = tline->next;
H. Peter Anvin89cee572009-07-15 09:16:54 -04002872 if (!tline->next) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002873 free_tlist(origline);
H. Peter Anvin130736c2016-02-17 20:27:41 -08002874 nasm_error(ERR_NONFATAL, "`%%rotate' missing rotate count");
H. Peter Anvine2c80182005-01-15 22:15:51 +00002875 return DIRECTIVE_FOUND;
2876 }
2877 t = expand_smacro(tline->next);
2878 tline->next = NULL;
2879 free_tlist(origline);
2880 tline = t;
2881 tptr = &t;
2882 tokval.t_type = TOKEN_INVALID;
2883 evalresult =
H. Peter Anvin130736c2016-02-17 20:27:41 -08002884 evaluate(ppscan, tptr, &tokval, NULL, pass, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002885 free_tlist(tline);
2886 if (!evalresult)
2887 return DIRECTIVE_FOUND;
2888 if (tokval.t_type)
H. Peter Anvin130736c2016-02-17 20:27:41 -08002889 nasm_error(ERR_WARNING|ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +00002890 "trailing garbage after expression ignored");
2891 if (!is_simple(evalresult)) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002892 nasm_error(ERR_NONFATAL, "non-constant value given to `%%rotate'");
H. Peter Anvine2c80182005-01-15 22:15:51 +00002893 return DIRECTIVE_FOUND;
2894 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002895 mmac = istk->mstk;
2896 while (mmac && !mmac->name) /* avoid mistaking %reps for macros */
2897 mmac = mmac->next_active;
2898 if (!mmac) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002899 nasm_error(ERR_NONFATAL, "`%%rotate' invoked outside a macro call");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002900 } else if (mmac->nparam == 0) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002901 nasm_error(ERR_NONFATAL,
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002902 "`%%rotate' invoked within macro without parameters");
2903 } else {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002904 int rotate = mmac->rotate + reloc_value(evalresult);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002905
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002906 rotate %= (int)mmac->nparam;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002907 if (rotate < 0)
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002908 rotate += mmac->nparam;
2909
2910 mmac->rotate = rotate;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002911 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002912 return DIRECTIVE_FOUND;
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00002913
H. Peter Anvine2c80182005-01-15 22:15:51 +00002914 case PP_REP:
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002915 nolist = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002916 do {
2917 tline = tline->next;
2918 } while (tok_type_(tline, TOK_WHITESPACE));
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00002919
H. Peter Anvine2c80182005-01-15 22:15:51 +00002920 if (tok_type_(tline, TOK_ID) &&
2921 nasm_stricmp(tline->text, ".nolist") == 0) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002922 nolist = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002923 do {
2924 tline = tline->next;
2925 } while (tok_type_(tline, TOK_WHITESPACE));
2926 }
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00002927
H. Peter Anvine2c80182005-01-15 22:15:51 +00002928 if (tline) {
2929 t = expand_smacro(tline);
2930 tptr = &t;
2931 tokval.t_type = TOKEN_INVALID;
2932 evalresult =
H. Peter Anvin130736c2016-02-17 20:27:41 -08002933 evaluate(ppscan, tptr, &tokval, NULL, pass, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002934 if (!evalresult) {
2935 free_tlist(origline);
2936 return DIRECTIVE_FOUND;
2937 }
2938 if (tokval.t_type)
H. Peter Anvin130736c2016-02-17 20:27:41 -08002939 nasm_error(ERR_WARNING|ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +00002940 "trailing garbage after expression ignored");
2941 if (!is_simple(evalresult)) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002942 nasm_error(ERR_NONFATAL, "non-constant value given to `%%rep'");
H. Peter Anvine2c80182005-01-15 22:15:51 +00002943 return DIRECTIVE_FOUND;
2944 }
Cyrill Gorcunove091d6e2010-08-09 13:58:22 +04002945 count = reloc_value(evalresult);
2946 if (count >= REP_LIMIT) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002947 nasm_error(ERR_NONFATAL, "`%%rep' value exceeds limit");
Cyrill Gorcunove091d6e2010-08-09 13:58:22 +04002948 count = 0;
2949 } else
2950 count++;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002951 } else {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002952 nasm_error(ERR_NONFATAL, "`%%rep' expects a repeat count");
H. Peter Anvinf8ba53e2007-10-11 10:11:57 -07002953 count = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002954 }
2955 free_tlist(origline);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002956
2957 tmp_defining = defining;
2958 defining = nasm_malloc(sizeof(MMacro));
2959 defining->prev = NULL;
2960 defining->name = NULL; /* flags this macro as a %rep block */
2961 defining->casesense = false;
2962 defining->plus = false;
2963 defining->nolist = nolist;
2964 defining->in_progress = count;
2965 defining->max_depth = 0;
2966 defining->nparam_min = defining->nparam_max = 0;
2967 defining->defaults = NULL;
2968 defining->dlist = NULL;
2969 defining->expansion = NULL;
2970 defining->next_active = istk->mstk;
2971 defining->rep_nest = tmp_defining;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002972 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002973
H. Peter Anvine2c80182005-01-15 22:15:51 +00002974 case PP_ENDREP:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002975 if (!defining || defining->name) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002976 nasm_error(ERR_NONFATAL, "`%%endrep': no matching `%%rep'");
H. Peter Anvine2c80182005-01-15 22:15:51 +00002977 return DIRECTIVE_FOUND;
2978 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00002979
H. Peter Anvine2c80182005-01-15 22:15:51 +00002980 /*
2981 * Now we have a "macro" defined - although it has no name
2982 * and we won't be entering it in the hash tables - we must
2983 * push a macro-end marker for it on to istk->expansion.
2984 * After that, it will take care of propagating itself (a
2985 * macro-end marker line for a macro which is really a %rep
2986 * block will cause the macro to be re-expanded, complete
2987 * with another macro-end marker to ensure the process
2988 * continues) until the whole expansion is forcibly removed
2989 * from istk->expansion by a %exitrep.
2990 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002991 l = nasm_malloc(sizeof(Line));
2992 l->next = istk->expansion;
2993 l->finishes = defining;
2994 l->first = NULL;
2995 istk->expansion = l;
2996
2997 istk->mstk = defining;
2998
H. Peter Anvin8ac25aa2016-02-18 01:16:18 -08002999 lfmt->uplevel(defining->nolist ? LIST_MACRO_NOLIST : LIST_MACRO);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003000 tmp_defining = defining;
3001 defining = defining->rep_nest;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003002 free_tlist(origline);
3003 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003004
H. Peter Anvine2c80182005-01-15 22:15:51 +00003005 case PP_EXITREP:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003006 /*
3007 * We must search along istk->expansion until we hit a
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003008 * macro-end marker for a macro with no name. Then we set
3009 * its `in_progress' flag to 0.
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003010 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003011 list_for_each(l, istk->expansion)
3012 if (l->finishes && !l->finishes->name)
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003013 break;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003014
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003015 if (l)
3016 l->finishes->in_progress = 1;
3017 else
H. Peter Anvin130736c2016-02-17 20:27:41 -08003018 nasm_error(ERR_NONFATAL, "`%%exitrep' not within `%%rep' block");
H. Peter Anvine2c80182005-01-15 22:15:51 +00003019 free_tlist(origline);
3020 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003021
H. Peter Anvine2c80182005-01-15 22:15:51 +00003022 case PP_XDEFINE:
3023 case PP_IXDEFINE:
3024 case PP_DEFINE:
3025 case PP_IDEFINE:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003026 casesense = (i == PP_DEFINE || i == PP_XDEFINE);
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07003027
H. Peter Anvine2c80182005-01-15 22:15:51 +00003028 tline = tline->next;
3029 skip_white_(tline);
3030 tline = expand_id(tline);
3031 if (!tline || (tline->type != TOK_ID &&
3032 (tline->type != TOK_PREPROC_ID ||
3033 tline->text[1] != '$'))) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08003034 nasm_error(ERR_NONFATAL, "`%s' expects a macro identifier",
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003035 pp_directives[i]);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003036 free_tlist(origline);
3037 return DIRECTIVE_FOUND;
3038 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003039
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04003040 ctx = get_ctx(tline->text, &mname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003041 last = tline;
3042 param_start = tline = tline->next;
3043 nparam = 0;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003044
H. Peter Anvine2c80182005-01-15 22:15:51 +00003045 /* Expand the macro definition now for %xdefine and %ixdefine */
3046 if ((i == PP_XDEFINE) || (i == PP_IXDEFINE))
3047 tline = expand_smacro(tline);
H. Peter Anvin734b1882002-04-30 21:01:08 +00003048
H. Peter Anvine2c80182005-01-15 22:15:51 +00003049 if (tok_is_(tline, "(")) {
3050 /*
3051 * This macro has parameters.
3052 */
H. Peter Anvin734b1882002-04-30 21:01:08 +00003053
H. Peter Anvine2c80182005-01-15 22:15:51 +00003054 tline = tline->next;
3055 while (1) {
3056 skip_white_(tline);
3057 if (!tline) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08003058 nasm_error(ERR_NONFATAL, "parameter identifier expected");
H. Peter Anvine2c80182005-01-15 22:15:51 +00003059 free_tlist(origline);
3060 return DIRECTIVE_FOUND;
3061 }
3062 if (tline->type != TOK_ID) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08003063 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00003064 "`%s': parameter identifier expected",
3065 tline->text);
3066 free_tlist(origline);
3067 return DIRECTIVE_FOUND;
3068 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003069 tline->type = TOK_SMAC_PARAM + nparam++;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003070 tline = tline->next;
3071 skip_white_(tline);
3072 if (tok_is_(tline, ",")) {
3073 tline = tline->next;
H. Peter Anvinca348b62008-07-23 10:49:26 -04003074 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003075 if (!tok_is_(tline, ")")) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08003076 nasm_error(ERR_NONFATAL,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003077 "`)' expected to terminate macro template");
3078 free_tlist(origline);
3079 return DIRECTIVE_FOUND;
3080 }
3081 break;
3082 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00003083 }
3084 last = tline;
3085 tline = tline->next;
3086 }
3087 if (tok_type_(tline, TOK_WHITESPACE))
3088 last = tline, tline = tline->next;
3089 macro_start = NULL;
3090 last->next = NULL;
3091 t = tline;
3092 while (t) {
3093 if (t->type == TOK_ID) {
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04003094 list_for_each(tt, param_start)
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003095 if (tt->type >= TOK_SMAC_PARAM &&
H. Peter Anvine2c80182005-01-15 22:15:51 +00003096 !strcmp(tt->text, t->text))
3097 t->type = tt->type;
3098 }
3099 tt = t->next;
3100 t->next = macro_start;
3101 macro_start = t;
3102 t = tt;
3103 }
3104 /*
3105 * Good. We now have a macro name, a parameter count, and a
3106 * token list (in reverse order) for an expansion. We ought
3107 * to be OK just to create an SMacro, store it, and let
3108 * free_tlist have the rest of the line (which we have
3109 * carefully re-terminated after chopping off the expansion
3110 * from the end).
3111 */
H. Peter Anvin4db5a162007-10-11 13:42:09 -07003112 define_smacro(ctx, mname, casesense, nparam, macro_start);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003113 free_tlist(origline);
3114 return DIRECTIVE_FOUND;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003115
H. Peter Anvine2c80182005-01-15 22:15:51 +00003116 case PP_UNDEF:
3117 tline = tline->next;
3118 skip_white_(tline);
3119 tline = expand_id(tline);
3120 if (!tline || (tline->type != TOK_ID &&
3121 (tline->type != TOK_PREPROC_ID ||
3122 tline->text[1] != '$'))) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08003123 nasm_error(ERR_NONFATAL, "`%%undef' expects a macro identifier");
H. Peter Anvine2c80182005-01-15 22:15:51 +00003124 free_tlist(origline);
3125 return DIRECTIVE_FOUND;
3126 }
3127 if (tline->next) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08003128 nasm_error(ERR_WARNING|ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +00003129 "trailing garbage after macro name ignored");
3130 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003131
H. Peter Anvine2c80182005-01-15 22:15:51 +00003132 /* Find the context that symbol belongs to */
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04003133 ctx = get_ctx(tline->text, &mname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003134 undef_smacro(ctx, mname);
3135 free_tlist(origline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003136 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003137
H. Peter Anvin9e200162008-06-04 17:23:14 -07003138 case PP_DEFSTR:
3139 case PP_IDEFSTR:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003140 casesense = (i == PP_DEFSTR);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003141
3142 tline = tline->next;
3143 skip_white_(tline);
3144 tline = expand_id(tline);
3145 if (!tline || (tline->type != TOK_ID &&
3146 (tline->type != TOK_PREPROC_ID ||
3147 tline->text[1] != '$'))) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08003148 nasm_error(ERR_NONFATAL, "`%s' expects a macro identifier",
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003149 pp_directives[i]);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003150 free_tlist(origline);
3151 return DIRECTIVE_FOUND;
3152 }
3153
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04003154 ctx = get_ctx(tline->text, &mname);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003155 last = tline;
3156 tline = expand_smacro(tline->next);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003157 last->next = NULL;
H. Peter Anvin9e200162008-06-04 17:23:14 -07003158
3159 while (tok_type_(tline, TOK_WHITESPACE))
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003160 tline = delete_Token(tline);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003161
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003162 p = detoken(tline, false);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003163 macro_start = nasm_malloc(sizeof(*macro_start));
3164 macro_start->next = NULL;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003165 macro_start->text = nasm_quote(p, strlen(p));
3166 macro_start->type = TOK_STRING;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003167 macro_start->a.mac = NULL;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003168 nasm_free(p);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003169
3170 /*
3171 * We now have a macro name, an implicit parameter count of
3172 * zero, and a string token to use as an expansion. Create
3173 * and store an SMacro.
3174 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003175 define_smacro(ctx, mname, casesense, 0, macro_start);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003176 free_tlist(origline);
3177 return DIRECTIVE_FOUND;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003178
H. Peter Anvin2f55bda2009-07-14 15:04:04 -04003179 case PP_DEFTOK:
3180 case PP_IDEFTOK:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003181 casesense = (i == PP_DEFTOK);
3182
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003183 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,
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003190 "`%s' expects a macro identifier as first parameter",
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003191 pp_directives[i]);
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003192 free_tlist(origline);
3193 return DIRECTIVE_FOUND;
3194 }
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04003195 ctx = get_ctx(tline->text, &mname);
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003196 last = tline;
3197 tline = expand_smacro(tline->next);
3198 last->next = NULL;
3199
3200 t = tline;
3201 while (tok_type_(t, TOK_WHITESPACE))
3202 t = t->next;
3203 /* t should now point to the string */
Cyrill Gorcunov6908e582010-09-06 19:36:15 +04003204 if (!tok_type_(t, TOK_STRING)) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08003205 nasm_error(ERR_NONFATAL,
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003206 "`%s` requires string as second parameter",
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003207 pp_directives[i]);
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003208 free_tlist(tline);
3209 free_tlist(origline);
3210 return DIRECTIVE_FOUND;
3211 }
3212
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04003213 /*
3214 * Convert the string to a token stream. Note that smacros
3215 * are stored with the token stream reversed, so we have to
3216 * reverse the output of tokenize().
3217 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003218 nasm_unquote_cstr(t->text, i);
H. Peter Anvinb40992c2010-09-15 08:57:21 -07003219 macro_start = reverse_tokens(tokenize(t->text));
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003220
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003221 /*
3222 * We now have a macro name, an implicit parameter count of
3223 * zero, and a numeric token to use as an expansion. Create
3224 * and store an SMacro.
3225 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003226 define_smacro(ctx, mname, casesense, 0, macro_start);
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003227 free_tlist(tline);
3228 free_tlist(origline);
3229 return DIRECTIVE_FOUND;
H. Peter Anvin9e200162008-06-04 17:23:14 -07003230
H. Peter Anvin418ca702008-05-30 10:42:30 -07003231 case PP_PATHSEARCH:
3232 {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003233 FILE *fp;
3234 StrList *xsl = NULL;
3235 StrList **xst = &xsl;
H. Peter Anvin418ca702008-05-30 10:42:30 -07003236
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003237 casesense = true;
H. Peter Anvin418ca702008-05-30 10:42:30 -07003238
3239 tline = tline->next;
3240 skip_white_(tline);
3241 tline = expand_id(tline);
3242 if (!tline || (tline->type != TOK_ID &&
3243 (tline->type != TOK_PREPROC_ID ||
3244 tline->text[1] != '$'))) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08003245 nasm_error(ERR_NONFATAL,
H. Peter Anvin418ca702008-05-30 10:42:30 -07003246 "`%%pathsearch' expects a macro identifier as first parameter");
3247 free_tlist(origline);
3248 return DIRECTIVE_FOUND;
3249 }
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04003250 ctx = get_ctx(tline->text, &mname);
H. Peter Anvin418ca702008-05-30 10:42:30 -07003251 last = tline;
3252 tline = expand_smacro(tline->next);
3253 last->next = NULL;
3254
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003255 t = tline;
H. Peter Anvin418ca702008-05-30 10:42:30 -07003256 while (tok_type_(t, TOK_WHITESPACE))
3257 t = t->next;
3258
3259 if (!t || (t->type != TOK_STRING &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003260 t->type != TOK_INTERNAL_STRING)) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08003261 nasm_error(ERR_NONFATAL, "`%%pathsearch' expects a file name");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003262 free_tlist(tline);
H. Peter Anvin418ca702008-05-30 10:42:30 -07003263 free_tlist(origline);
3264 return DIRECTIVE_FOUND; /* but we did _something_ */
3265 }
3266 if (t->next)
H. Peter Anvin130736c2016-02-17 20:27:41 -08003267 nasm_error(ERR_WARNING|ERR_PASS1,
H. Peter Anvin418ca702008-05-30 10:42:30 -07003268 "trailing garbage after `%%pathsearch' ignored");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003269 p = t->text;
H. Peter Anvin427cc912008-06-01 21:43:03 -07003270 if (t->type != TOK_INTERNAL_STRING)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003271 nasm_unquote(p, NULL);
H. Peter Anvin418ca702008-05-30 10:42:30 -07003272
H. Peter Anvin66d561f2016-06-16 15:47:46 -07003273 fp = inc_fopen(p, &xsl, &xst, NULL, true, NF_TEXT);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003274 if (fp) {
3275 p = xsl->str;
3276 fclose(fp); /* Don't actually care about the file */
3277 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003278 macro_start = nasm_malloc(sizeof(*macro_start));
3279 macro_start->next = NULL;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003280 macro_start->text = nasm_quote(p, strlen(p));
3281 macro_start->type = TOK_STRING;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003282 macro_start->a.mac = NULL;
3283 if (xsl)
3284 nasm_free(xsl);
H. Peter Anvin418ca702008-05-30 10:42:30 -07003285
3286 /*
3287 * We now have a macro name, an implicit parameter count of
3288 * zero, and a string token to use as an expansion. Create
3289 * and store an SMacro.
3290 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003291 define_smacro(ctx, mname, casesense, 0, macro_start);
3292 free_tlist(tline);
H. Peter Anvin418ca702008-05-30 10:42:30 -07003293 free_tlist(origline);
3294 return DIRECTIVE_FOUND;
3295 }
3296
H. Peter Anvine2c80182005-01-15 22:15:51 +00003297 case PP_STRLEN:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003298 casesense = true;
H. Peter Anvin70653092007-10-19 14:42:29 -07003299
H. Peter Anvine2c80182005-01-15 22:15:51 +00003300 tline = tline->next;
3301 skip_white_(tline);
3302 tline = expand_id(tline);
3303 if (!tline || (tline->type != TOK_ID &&
3304 (tline->type != TOK_PREPROC_ID ||
3305 tline->text[1] != '$'))) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08003306 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00003307 "`%%strlen' expects a macro identifier as first parameter");
3308 free_tlist(origline);
3309 return DIRECTIVE_FOUND;
3310 }
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04003311 ctx = get_ctx(tline->text, &mname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003312 last = tline;
3313 tline = expand_smacro(tline->next);
3314 last->next = NULL;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003315
H. Peter Anvine2c80182005-01-15 22:15:51 +00003316 t = tline;
3317 while (tok_type_(t, TOK_WHITESPACE))
3318 t = t->next;
3319 /* t should now point to the string */
Cyrill Gorcunov4e1d5ab2010-07-23 18:51:51 +04003320 if (!tok_type_(t, TOK_STRING)) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08003321 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00003322 "`%%strlen` requires string as second parameter");
3323 free_tlist(tline);
3324 free_tlist(origline);
3325 return DIRECTIVE_FOUND;
3326 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003327
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003328 macro_start = nasm_malloc(sizeof(*macro_start));
3329 macro_start->next = NULL;
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07003330 make_tok_num(macro_start, nasm_unquote(t->text, NULL));
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003331 macro_start->a.mac = NULL;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003332
H. Peter Anvine2c80182005-01-15 22:15:51 +00003333 /*
3334 * We now have a macro name, an implicit parameter count of
3335 * zero, and a numeric token to use as an expansion. Create
3336 * and store an SMacro.
3337 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003338 define_smacro(ctx, mname, casesense, 0, macro_start);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003339 free_tlist(tline);
3340 free_tlist(origline);
3341 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003342
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003343 case PP_STRCAT:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003344 casesense = true;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003345
3346 tline = tline->next;
3347 skip_white_(tline);
3348 tline = expand_id(tline);
3349 if (!tline || (tline->type != TOK_ID &&
3350 (tline->type != TOK_PREPROC_ID ||
3351 tline->text[1] != '$'))) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08003352 nasm_error(ERR_NONFATAL,
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003353 "`%%strcat' expects a macro identifier as first parameter");
3354 free_tlist(origline);
3355 return DIRECTIVE_FOUND;
3356 }
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04003357 ctx = get_ctx(tline->text, &mname);
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003358 last = tline;
3359 tline = expand_smacro(tline->next);
3360 last->next = NULL;
3361
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003362 len = 0;
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04003363 list_for_each(t, tline) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003364 switch (t->type) {
3365 case TOK_WHITESPACE:
3366 break;
3367 case TOK_STRING:
3368 len += t->a.len = nasm_unquote(t->text, NULL);
3369 break;
3370 case TOK_OTHER:
3371 if (!strcmp(t->text, ",")) /* permit comma separators */
3372 break;
3373 /* else fall through */
3374 default:
H. Peter Anvin130736c2016-02-17 20:27:41 -08003375 nasm_error(ERR_NONFATAL,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003376 "non-string passed to `%%strcat' (%d)", t->type);
3377 free_tlist(tline);
3378 free_tlist(origline);
3379 return DIRECTIVE_FOUND;
3380 }
3381 }
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003382
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003383 p = pp = nasm_malloc(len);
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04003384 list_for_each(t, tline) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003385 if (t->type == TOK_STRING) {
3386 memcpy(p, t->text, t->a.len);
3387 p += t->a.len;
3388 }
3389 }
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003390
3391 /*
3392 * We now have a macro name, an implicit parameter count of
3393 * zero, and a numeric token to use as an expansion. Create
3394 * and store an SMacro.
3395 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003396 macro_start = new_Token(NULL, TOK_STRING, NULL, 0);
3397 macro_start->text = nasm_quote(pp, len);
3398 nasm_free(pp);
3399 define_smacro(ctx, mname, casesense, 0, macro_start);
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003400 free_tlist(tline);
3401 free_tlist(origline);
3402 return DIRECTIVE_FOUND;
3403
H. Peter Anvine2c80182005-01-15 22:15:51 +00003404 case PP_SUBSTR:
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003405 {
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003406 int64_t start, count;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003407 size_t len;
H. Peter Anvind2456592008-06-19 15:04:18 -07003408
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003409 casesense = true;
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07003410
H. Peter Anvine2c80182005-01-15 22:15:51 +00003411 tline = tline->next;
3412 skip_white_(tline);
3413 tline = expand_id(tline);
3414 if (!tline || (tline->type != TOK_ID &&
3415 (tline->type != TOK_PREPROC_ID ||
3416 tline->text[1] != '$'))) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08003417 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00003418 "`%%substr' expects a macro identifier as first parameter");
3419 free_tlist(origline);
3420 return DIRECTIVE_FOUND;
3421 }
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04003422 ctx = get_ctx(tline->text, &mname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003423 last = tline;
3424 tline = expand_smacro(tline->next);
3425 last->next = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003426
Cyrill Gorcunov35519d62010-09-06 23:49:52 +04003427 if (tline) /* skip expanded id */
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003428 t = tline->next;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003429 while (tok_type_(t, TOK_WHITESPACE))
3430 t = t->next;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003431
H. Peter Anvine2c80182005-01-15 22:15:51 +00003432 /* t should now point to the string */
Cyrill Gorcunov35519d62010-09-06 23:49:52 +04003433 if (!tok_type_(t, TOK_STRING)) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08003434 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00003435 "`%%substr` requires string as second parameter");
3436 free_tlist(tline);
3437 free_tlist(origline);
3438 return DIRECTIVE_FOUND;
3439 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003440
H. Peter Anvine2c80182005-01-15 22:15:51 +00003441 tt = t->next;
3442 tptr = &tt;
3443 tokval.t_type = TOKEN_INVALID;
H. Peter Anvin130736c2016-02-17 20:27:41 -08003444 evalresult = evaluate(ppscan, tptr, &tokval, NULL, pass, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003445 if (!evalresult) {
3446 free_tlist(tline);
3447 free_tlist(origline);
3448 return DIRECTIVE_FOUND;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003449 } else if (!is_simple(evalresult)) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08003450 nasm_error(ERR_NONFATAL, "non-constant value given to `%%substr`");
H. Peter Anvine2c80182005-01-15 22:15:51 +00003451 free_tlist(tline);
3452 free_tlist(origline);
3453 return DIRECTIVE_FOUND;
3454 }
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003455 start = evalresult->value - 1;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003456
3457 while (tok_type_(tt, TOK_WHITESPACE))
3458 tt = tt->next;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003459 if (!tt) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003460 count = 1; /* Backwards compatibility: one character */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003461 } else {
3462 tokval.t_type = TOKEN_INVALID;
H. Peter Anvin130736c2016-02-17 20:27:41 -08003463 evalresult = evaluate(ppscan, tptr, &tokval, NULL, pass, NULL);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003464 if (!evalresult) {
3465 free_tlist(tline);
3466 free_tlist(origline);
3467 return DIRECTIVE_FOUND;
3468 } else if (!is_simple(evalresult)) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08003469 nasm_error(ERR_NONFATAL, "non-constant value given to `%%substr`");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003470 free_tlist(tline);
3471 free_tlist(origline);
3472 return DIRECTIVE_FOUND;
3473 }
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003474 count = evalresult->value;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003475 }
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003476
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003477 len = nasm_unquote(t->text, NULL);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003478
Cyrill Gorcunovcff031e2010-09-07 20:31:11 +04003479 /* make start and count being in range */
3480 if (start < 0)
3481 start = 0;
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003482 if (count < 0)
3483 count = len + count + 1 - start;
3484 if (start + count > (int64_t)len)
Cyrill Gorcunovcff031e2010-09-07 20:31:11 +04003485 count = len - start;
3486 if (!len || count < 0 || start >=(int64_t)len)
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003487 start = -1, count = 0; /* empty string */
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003488
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003489 macro_start = nasm_malloc(sizeof(*macro_start));
3490 macro_start->next = NULL;
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003491 macro_start->text = nasm_quote((start < 0) ? "" : t->text + start, count);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003492 macro_start->type = TOK_STRING;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003493 macro_start->a.mac = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003494
H. Peter Anvine2c80182005-01-15 22:15:51 +00003495 /*
3496 * We now have a macro name, an implicit parameter count of
3497 * zero, and a numeric token to use as an expansion. Create
3498 * and store an SMacro.
3499 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003500 define_smacro(ctx, mname, casesense, 0, macro_start);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003501 free_tlist(tline);
3502 free_tlist(origline);
3503 return DIRECTIVE_FOUND;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003504 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003505
H. Peter Anvine2c80182005-01-15 22:15:51 +00003506 case PP_ASSIGN:
3507 case PP_IASSIGN:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003508 casesense = (i == PP_ASSIGN);
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07003509
H. Peter Anvine2c80182005-01-15 22:15:51 +00003510 tline = tline->next;
3511 skip_white_(tline);
3512 tline = expand_id(tline);
3513 if (!tline || (tline->type != TOK_ID &&
3514 (tline->type != TOK_PREPROC_ID ||
3515 tline->text[1] != '$'))) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08003516 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00003517 "`%%%sassign' expects a macro identifier",
3518 (i == PP_IASSIGN ? "i" : ""));
3519 free_tlist(origline);
3520 return DIRECTIVE_FOUND;
3521 }
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04003522 ctx = get_ctx(tline->text, &mname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003523 last = tline;
3524 tline = expand_smacro(tline->next);
3525 last->next = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003526
H. Peter Anvine2c80182005-01-15 22:15:51 +00003527 t = tline;
3528 tptr = &t;
3529 tokval.t_type = TOKEN_INVALID;
H. Peter Anvin130736c2016-02-17 20:27:41 -08003530 evalresult = evaluate(ppscan, tptr, &tokval, NULL, pass, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003531 free_tlist(tline);
3532 if (!evalresult) {
3533 free_tlist(origline);
3534 return DIRECTIVE_FOUND;
3535 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003536
H. Peter Anvine2c80182005-01-15 22:15:51 +00003537 if (tokval.t_type)
H. Peter Anvin130736c2016-02-17 20:27:41 -08003538 nasm_error(ERR_WARNING|ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +00003539 "trailing garbage after expression ignored");
H. Peter Anvin734b1882002-04-30 21:01:08 +00003540
H. Peter Anvine2c80182005-01-15 22:15:51 +00003541 if (!is_simple(evalresult)) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08003542 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00003543 "non-constant value given to `%%%sassign'",
3544 (i == PP_IASSIGN ? "i" : ""));
3545 free_tlist(origline);
3546 return DIRECTIVE_FOUND;
3547 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003548
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003549 macro_start = nasm_malloc(sizeof(*macro_start));
3550 macro_start->next = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003551 make_tok_num(macro_start, reloc_value(evalresult));
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003552 macro_start->a.mac = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003553
H. Peter Anvine2c80182005-01-15 22:15:51 +00003554 /*
3555 * We now have a macro name, an implicit parameter count of
3556 * zero, and a numeric token to use as an expansion. Create
3557 * and store an SMacro.
3558 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003559 define_smacro(ctx, mname, casesense, 0, macro_start);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003560 free_tlist(origline);
3561 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003562
H. Peter Anvine2c80182005-01-15 22:15:51 +00003563 case PP_LINE:
3564 /*
3565 * Syntax is `%line nnn[+mmm] [filename]'
3566 */
3567 tline = tline->next;
3568 skip_white_(tline);
3569 if (!tok_type_(tline, TOK_NUMBER)) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08003570 nasm_error(ERR_NONFATAL, "`%%line' expects line number");
H. Peter Anvine2c80182005-01-15 22:15:51 +00003571 free_tlist(origline);
3572 return DIRECTIVE_FOUND;
3573 }
H. Peter Anvin70055962007-10-11 00:05:31 -07003574 k = readnum(tline->text, &err);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003575 m = 1;
3576 tline = tline->next;
3577 if (tok_is_(tline, "+")) {
3578 tline = tline->next;
3579 if (!tok_type_(tline, TOK_NUMBER)) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08003580 nasm_error(ERR_NONFATAL, "`%%line' expects line increment");
H. Peter Anvine2c80182005-01-15 22:15:51 +00003581 free_tlist(origline);
3582 return DIRECTIVE_FOUND;
3583 }
H. Peter Anvin70055962007-10-11 00:05:31 -07003584 m = readnum(tline->text, &err);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003585 tline = tline->next;
3586 }
3587 skip_white_(tline);
3588 src_set_linnum(k);
3589 istk->lineinc = m;
3590 if (tline) {
H. Peter Anvin274cda82016-05-10 02:56:29 -07003591 char *fname = detoken(tline, false);
3592 src_set_fname(fname);
3593 nasm_free(fname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003594 }
3595 free_tlist(origline);
3596 return DIRECTIVE_FOUND;
Keith Kaniosb307a4f2010-11-06 17:41:51 -05003597
H. Peter Anvine2c80182005-01-15 22:15:51 +00003598 default:
H. Peter Anvin130736c2016-02-17 20:27:41 -08003599 nasm_error(ERR_FATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00003600 "preprocessor directive `%s' not yet implemented",
H. Peter Anvin4169a472007-09-12 01:29:43 +00003601 pp_directives[i]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003602 return DIRECTIVE_FOUND;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003603 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003604}
3605
3606/*
H. Peter Anvin76690a12002-04-30 20:52:49 +00003607 * Ensure that a macro parameter contains a condition code and
3608 * nothing else. Return the condition code index if so, or -1
3609 * otherwise.
3610 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003611static int find_cc(Token * t)
H. Peter Anvineba20a72002-04-30 20:53:55 +00003612{
H. Peter Anvin76690a12002-04-30 20:52:49 +00003613 Token *tt;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003614
H. Peter Anvin25a99342007-09-22 17:45:45 -07003615 if (!t)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003616 return -1; /* Probably a %+ without a space */
H. Peter Anvin25a99342007-09-22 17:45:45 -07003617
H. Peter Anvineba20a72002-04-30 20:53:55 +00003618 skip_white_(t);
H. Peter Anvin76690a12002-04-30 20:52:49 +00003619 if (t->type != TOK_ID)
H. Peter Anvine2c80182005-01-15 22:15:51 +00003620 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003621 tt = t->next;
H. Peter Anvineba20a72002-04-30 20:53:55 +00003622 skip_white_(tt);
H. Peter Anvin76690a12002-04-30 20:52:49 +00003623 if (tt && (tt->type != TOK_OTHER || strcmp(tt->text, ",")))
H. Peter Anvine2c80182005-01-15 22:15:51 +00003624 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003625
Cyrill Gorcunov19456392012-05-02 00:18:56 +04003626 return bsii(t->text, (const char **)conditions, ARRAY_SIZE(conditions));
H. Peter Anvin76690a12002-04-30 20:52:49 +00003627}
3628
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003629/*
3630 * This routines walks over tokens strem and hadnles tokens
3631 * pasting, if @handle_explicit passed then explicit pasting
3632 * term is handled, otherwise -- implicit pastings only.
3633 */
Cyrill Gorcunov575d4282010-10-06 00:25:55 +04003634static bool paste_tokens(Token **head, const struct tokseq_match *m,
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003635 size_t mnum, bool handle_explicit)
H. Peter Anvind784a082009-04-20 14:01:18 -07003636{
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003637 Token *tok, *next, **prev_next, **prev_nonspace;
3638 bool pasted = false;
3639 char *buf, *p;
3640 size_t len, i;
H. Peter Anvind784a082009-04-20 14:01:18 -07003641
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003642 /*
3643 * The last token before pasting. We need it
3644 * to be able to connect new handled tokens.
3645 * In other words if there were a tokens stream
3646 *
3647 * A -> B -> C -> D
3648 *
3649 * and we've joined tokens B and C, the resulting
3650 * stream should be
3651 *
3652 * A -> BC -> D
3653 */
3654 tok = *head;
3655 prev_next = NULL;
3656
3657 if (!tok_type_(tok, TOK_WHITESPACE) && !tok_type_(tok, TOK_PASTE))
3658 prev_nonspace = head;
3659 else
3660 prev_nonspace = NULL;
3661
3662 while (tok && (next = tok->next)) {
3663
3664 switch (tok->type) {
H. Peter Anvind784a082009-04-20 14:01:18 -07003665 case TOK_WHITESPACE:
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003666 /* Zap redundant whitespaces */
3667 while (tok_type_(next, TOK_WHITESPACE))
3668 next = delete_Token(next);
3669 tok->next = next;
H. Peter Anvind784a082009-04-20 14:01:18 -07003670 break;
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003671
3672 case TOK_PASTE:
3673 /* Explicit pasting */
3674 if (!handle_explicit)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003675 break;
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003676 next = delete_Token(tok);
3677
3678 while (tok_type_(next, TOK_WHITESPACE))
3679 next = delete_Token(next);
3680
3681 if (!pasted)
3682 pasted = true;
3683
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003684 /* Left pasting token is start of line */
3685 if (!prev_nonspace)
H. Peter Anvin130736c2016-02-17 20:27:41 -08003686 nasm_error(ERR_FATAL, "No lvalue found on pasting");
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003687
Cyrill Gorcunov8b5c9fb2013-02-04 01:24:54 +04003688 /*
3689 * No ending token, this might happen in two
3690 * cases
3691 *
3692 * 1) There indeed no right token at all
3693 * 2) There is a bare "%define ID" statement,
3694 * and @ID does expand to whitespace.
3695 *
3696 * So technically we need to do a grammar analysis
3697 * in another stage of parsing, but for now lets don't
3698 * change the behaviour people used to. Simply allow
3699 * whitespace after paste token.
3700 */
3701 if (!next) {
3702 /*
3703 * Zap ending space tokens and that's all.
3704 */
3705 tok = (*prev_nonspace)->next;
3706 while (tok_type_(tok, TOK_WHITESPACE))
3707 tok = delete_Token(tok);
3708 tok = *prev_nonspace;
3709 tok->next = NULL;
3710 break;
3711 }
3712
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003713 tok = *prev_nonspace;
3714 while (tok_type_(tok, TOK_WHITESPACE))
3715 tok = delete_Token(tok);
3716 len = strlen(tok->text);
3717 len += strlen(next->text);
3718
3719 p = buf = nasm_malloc(len + 1);
3720 strcpy(p, tok->text);
3721 p = strchr(p, '\0');
3722 strcpy(p, next->text);
3723
3724 delete_Token(tok);
3725
3726 tok = tokenize(buf);
3727 nasm_free(buf);
3728
3729 *prev_nonspace = tok;
3730 while (tok && tok->next)
3731 tok = tok->next;
3732
3733 tok->next = delete_Token(next);
3734
3735 /* Restart from pasted tokens head */
3736 tok = *prev_nonspace;
3737 break;
3738
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003739 default:
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003740 /* implicit pasting */
Cyrill Gorcunov575d4282010-10-06 00:25:55 +04003741 for (i = 0; i < mnum; i++) {
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003742 if (!(PP_CONCAT_MATCH(tok, m[i].mask_head)))
3743 continue;
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +04003744
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003745 len = 0;
3746 while (next && PP_CONCAT_MATCH(next, m[i].mask_tail)) {
3747 len += strlen(next->text);
3748 next = next->next;
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +04003749 }
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003750
3751 /* No match */
3752 if (tok == next)
3753 break;
3754
3755 len += strlen(tok->text);
3756 p = buf = nasm_malloc(len + 1);
3757
3758 while (tok != next) {
3759 strcpy(p, tok->text);
3760 p = strchr(p, '\0');
3761 tok = delete_Token(tok);
3762 }
3763
3764 tok = tokenize(buf);
3765 nasm_free(buf);
3766
3767 if (prev_next)
3768 *prev_next = tok;
3769 else
3770 *head = tok;
3771
3772 /*
3773 * Connect pasted into original stream,
3774 * ie A -> new-tokens -> B
3775 */
3776 while (tok && tok->next)
3777 tok = tok->next;
3778 tok->next = next;
3779
3780 if (!pasted)
3781 pasted = true;
3782
3783 /* Restart from pasted tokens head */
3784 tok = prev_next ? *prev_next : *head;
Cyrill Gorcunov575d4282010-10-06 00:25:55 +04003785 }
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003786
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003787 break;
H. Peter Anvind784a082009-04-20 14:01:18 -07003788 }
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003789
3790 prev_next = &tok->next;
3791
3792 if (tok->next &&
3793 !tok_type_(tok->next, TOK_WHITESPACE) &&
3794 !tok_type_(tok->next, TOK_PASTE))
3795 prev_nonspace = prev_next;
3796
3797 tok = tok->next;
H. Peter Anvind784a082009-04-20 14:01:18 -07003798 }
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003799
3800 return pasted;
H. Peter Anvind784a082009-04-20 14:01:18 -07003801}
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003802
3803/*
3804 * expands to a list of tokens from %{x:y}
3805 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003806static Token *expand_mmac_params_range(MMacro *mac, Token *tline, Token ***last)
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003807{
3808 Token *t = tline, **tt, *tm, *head;
3809 char *pos;
3810 int fst, lst, j, i;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003811
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003812 pos = strchr(tline->text, ':');
3813 nasm_assert(pos);
3814
3815 lst = atoi(pos + 1);
3816 fst = atoi(tline->text + 1);
3817
3818 /*
3819 * only macros params are accounted so
3820 * if someone passes %0 -- we reject such
3821 * value(s)
3822 */
3823 if (lst == 0 || fst == 0)
3824 goto err;
3825
3826 /* the values should be sane */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003827 if ((fst > (int)mac->nparam || fst < (-(int)mac->nparam)) ||
3828 (lst > (int)mac->nparam || lst < (-(int)mac->nparam)))
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003829 goto err;
3830
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003831 fst = fst < 0 ? fst + (int)mac->nparam + 1: fst;
3832 lst = lst < 0 ? lst + (int)mac->nparam + 1: lst;
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003833
3834 /* counted from zero */
3835 fst--, lst--;
3836
3837 /*
Cyrill Gorcunove75331c2013-11-09 12:02:15 +04003838 * It will be at least one token. Note we
3839 * need to scan params until separator, otherwise
3840 * only first token will be passed.
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003841 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003842 tm = mac->params[(fst + mac->rotate) % mac->nparam];
Cyrill Gorcunove75331c2013-11-09 12:02:15 +04003843 head = new_Token(NULL, tm->type, tm->text, 0);
3844 tt = &head->next, tm = tm->next;
3845 while (tok_isnt_(tm, ",")) {
3846 t = new_Token(NULL, tm->type, tm->text, 0);
3847 *tt = t, tt = &t->next, tm = tm->next;
3848 }
3849
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003850 if (fst < lst) {
3851 for (i = fst + 1; i <= lst; i++) {
3852 t = new_Token(NULL, TOK_OTHER, ",", 0);
3853 *tt = t, tt = &t->next;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003854 j = (i + mac->rotate) % mac->nparam;
3855 tm = mac->params[j];
Cyrill Gorcunove75331c2013-11-09 12:02:15 +04003856 while (tok_isnt_(tm, ",")) {
3857 t = new_Token(NULL, tm->type, tm->text, 0);
3858 *tt = t, tt = &t->next, tm = tm->next;
3859 }
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003860 }
3861 } else {
3862 for (i = fst - 1; i >= lst; i--) {
3863 t = new_Token(NULL, TOK_OTHER, ",", 0);
3864 *tt = t, tt = &t->next;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003865 j = (i + mac->rotate) % mac->nparam;
3866 tm = mac->params[j];
Cyrill Gorcunove75331c2013-11-09 12:02:15 +04003867 while (tok_isnt_(tm, ",")) {
3868 t = new_Token(NULL, tm->type, tm->text, 0);
3869 *tt = t, tt = &t->next, tm = tm->next;
3870 }
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003871 }
3872 }
3873
3874 *last = tt;
3875 return head;
3876
3877err:
H. Peter Anvin130736c2016-02-17 20:27:41 -08003878 nasm_error(ERR_NONFATAL, "`%%{%s}': macro parameters out of range",
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003879 &tline->text[1]);
3880 return tline;
3881}
3882
H. Peter Anvin76690a12002-04-30 20:52:49 +00003883/*
3884 * Expand MMacro-local things: parameter references (%0, %n, %+n,
H. Peter Anvin67c63722008-10-26 23:49:00 -07003885 * %-n) and MMacro-local identifiers (%%foo) as well as
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003886 * macro indirection (%[...]) and range (%{..:..}).
H. Peter Anvin76690a12002-04-30 20:52:49 +00003887 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003888static Token *expand_mmac_params(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00003889{
H. Peter Anvin734b1882002-04-30 21:01:08 +00003890 Token *t, *tt, **tail, *thead;
H. Peter Anvin6125b622009-04-08 14:02:25 -07003891 bool changed = false;
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003892 char *pos;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003893
3894 tail = &thead;
3895 thead = NULL;
3896
H. Peter Anvine2c80182005-01-15 22:15:51 +00003897 while (tline) {
3898 if (tline->type == TOK_PREPROC_ID &&
Cyrill Gorcunovca611192010-06-04 09:22:12 +04003899 (((tline->text[1] == '+' || tline->text[1] == '-') && tline->text[2]) ||
3900 (tline->text[1] >= '0' && tline->text[1] <= '9') ||
3901 tline->text[1] == '%')) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00003902 char *text = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003903 int type = 0, cc; /* type = 0 to placate optimisers */
Keith Kaniosa6dfa782007-04-13 16:47:53 +00003904 char tmpbuf[30];
H. Peter Anvin25a99342007-09-22 17:45:45 -07003905 unsigned int n;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003906 int i;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003907 MMacro *mac;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003908
H. Peter Anvine2c80182005-01-15 22:15:51 +00003909 t = tline;
3910 tline = tline->next;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003911
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003912 mac = istk->mstk;
3913 while (mac && !mac->name) /* avoid mistaking %reps for macros */
3914 mac = mac->next_active;
3915 if (!mac) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08003916 nasm_error(ERR_NONFATAL, "`%s': not in a macro call", t->text);
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003917 } else {
3918 pos = strchr(t->text, ':');
3919 if (!pos) {
3920 switch (t->text[1]) {
3921 /*
3922 * We have to make a substitution of one of the
3923 * forms %1, %-1, %+1, %%foo, %0.
3924 */
3925 case '0':
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003926 type = TOK_NUMBER;
3927 snprintf(tmpbuf, sizeof(tmpbuf), "%d", mac->nparam);
3928 text = nasm_strdup(tmpbuf);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003929 break;
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003930 case '%':
H. Peter Anvine2c80182005-01-15 22:15:51 +00003931 type = TOK_ID;
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003932 snprintf(tmpbuf, sizeof(tmpbuf), "..@%"PRIu64".",
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003933 mac->unique);
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003934 text = nasm_strcat(tmpbuf, t->text + 2);
3935 break;
3936 case '-':
3937 n = atoi(t->text + 2) - 1;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003938 if (n >= mac->nparam)
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003939 tt = NULL;
3940 else {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003941 if (mac->nparam > 1)
3942 n = (n + mac->rotate) % mac->nparam;
3943 tt = mac->params[n];
H. Peter Anvine2c80182005-01-15 22:15:51 +00003944 }
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003945 cc = find_cc(tt);
3946 if (cc == -1) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08003947 nasm_error(ERR_NONFATAL,
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003948 "macro parameter %d is not a condition code",
3949 n + 1);
3950 text = NULL;
3951 } else {
3952 type = TOK_ID;
3953 if (inverse_ccs[cc] == -1) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08003954 nasm_error(ERR_NONFATAL,
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003955 "condition code `%s' is not invertible",
3956 conditions[cc]);
3957 text = NULL;
3958 } else
3959 text = nasm_strdup(conditions[inverse_ccs[cc]]);
3960 }
3961 break;
3962 case '+':
3963 n = atoi(t->text + 2) - 1;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003964 if (n >= mac->nparam)
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003965 tt = NULL;
3966 else {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003967 if (mac->nparam > 1)
3968 n = (n + mac->rotate) % mac->nparam;
3969 tt = mac->params[n];
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003970 }
3971 cc = find_cc(tt);
3972 if (cc == -1) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08003973 nasm_error(ERR_NONFATAL,
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003974 "macro parameter %d is not a condition code",
3975 n + 1);
3976 text = NULL;
3977 } else {
3978 type = TOK_ID;
3979 text = nasm_strdup(conditions[cc]);
3980 }
3981 break;
3982 default:
3983 n = atoi(t->text + 1) - 1;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003984 if (n >= mac->nparam)
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003985 tt = NULL;
3986 else {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003987 if (mac->nparam > 1)
3988 n = (n + mac->rotate) % mac->nparam;
3989 tt = mac->params[n];
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003990 }
3991 if (tt) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003992 for (i = 0; i < mac->paramlen[n]; i++) {
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003993 *tail = new_Token(NULL, tt->type, tt->text, 0);
3994 tail = &(*tail)->next;
3995 tt = tt->next;
3996 }
3997 }
3998 text = NULL; /* we've done it here */
3999 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004000 }
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004001 } else {
4002 /*
4003 * seems we have a parameters range here
4004 */
4005 Token *head, **last;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004006 head = expand_mmac_params_range(mac, t, &last);
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004007 if (head != t) {
4008 *tail = head;
4009 *last = tline;
4010 tline = head;
4011 text = NULL;
4012 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00004013 }
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004014 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00004015 if (!text) {
4016 delete_Token(t);
4017 } else {
4018 *tail = t;
4019 tail = &t->next;
4020 t->type = type;
4021 nasm_free(t->text);
4022 t->text = text;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004023 t->a.mac = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004024 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004025 changed = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004026 continue;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004027 } else if (tline->type == TOK_INDIRECT) {
4028 t = tline;
4029 tline = tline->next;
4030 tt = tokenize(t->text);
4031 tt = expand_mmac_params(tt);
4032 tt = expand_smacro(tt);
4033 *tail = tt;
4034 while (tt) {
4035 tt->a.mac = NULL; /* Necessary? */
4036 tail = &tt->next;
4037 tt = tt->next;
4038 }
4039 delete_Token(t);
4040 changed = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004041 } else {
4042 t = *tail = tline;
4043 tline = tline->next;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004044 t->a.mac = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004045 tail = &t->next;
4046 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00004047 }
H. Peter Anvineba20a72002-04-30 20:53:55 +00004048 *tail = NULL;
H. Peter Anvin67c63722008-10-26 23:49:00 -07004049
Cyrill Gorcunovc6a742c2011-06-27 01:23:09 +04004050 if (changed) {
4051 const struct tokseq_match t[] = {
4052 {
4053 PP_CONCAT_MASK(TOK_ID) |
4054 PP_CONCAT_MASK(TOK_FLOAT), /* head */
4055 PP_CONCAT_MASK(TOK_ID) |
4056 PP_CONCAT_MASK(TOK_NUMBER) |
4057 PP_CONCAT_MASK(TOK_FLOAT) |
4058 PP_CONCAT_MASK(TOK_OTHER) /* tail */
4059 },
4060 {
4061 PP_CONCAT_MASK(TOK_NUMBER), /* head */
4062 PP_CONCAT_MASK(TOK_NUMBER) /* tail */
4063 }
4064 };
4065 paste_tokens(&thead, t, ARRAY_SIZE(t), false);
4066 }
H. Peter Anvin6125b622009-04-08 14:02:25 -07004067
H. Peter Anvin76690a12002-04-30 20:52:49 +00004068 return thead;
4069}
4070
4071/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004072 * Expand all single-line macro calls made in the given line.
4073 * Return the expanded version of the line. The original is deemed
4074 * to be destroyed in the process. (In reality we'll just move
4075 * Tokens from input to output a lot of the time, rather than
4076 * actually bothering to destroy and replicate.)
4077 */
H. Peter Anvincb1cf592007-11-19 12:26:50 -08004078
H. Peter Anvine2c80182005-01-15 22:15:51 +00004079static Token *expand_smacro(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004080{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004081 Token *t, *tt, *mstart, **tail, *thead;
H. Peter Anvineba20a72002-04-30 20:53:55 +00004082 SMacro *head = NULL, *m;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004083 Token **params;
4084 int *paramsize;
H. Peter Anvin25a99342007-09-22 17:45:45 -07004085 unsigned int nparam, sparam;
H. Peter Anvind784a082009-04-20 14:01:18 -07004086 int brackets;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004087 Token *org_tline = tline;
4088 Context *ctx;
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08004089 const char *mname;
H. Peter Anvin2a15e692007-11-19 13:14:59 -08004090 int deadman = DEADMAN_LIMIT;
H. Peter Anvin8287daf2009-07-07 16:00:58 -07004091 bool expanded;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004092
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004093 /*
4094 * Trick: we should avoid changing the start token pointer since it can
4095 * be contained in "next" field of other token. Because of this
4096 * we allocate a copy of first token and work with it; at the end of
4097 * routine we copy it back
4098 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004099 if (org_tline) {
Cyrill Gorcunoved4a8052010-04-09 15:40:35 +04004100 tline = new_Token(org_tline->next, org_tline->type,
4101 org_tline->text, 0);
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004102 tline->a.mac = org_tline->a.mac;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004103 nasm_free(org_tline->text);
4104 org_tline->text = NULL;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004105 }
4106
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004107 expanded = true; /* Always expand %+ at least once */
H. Peter Anvin8287daf2009-07-07 16:00:58 -07004108
H. Peter Anvincb1cf592007-11-19 12:26:50 -08004109again:
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004110 thead = NULL;
Cyrill Gorcunoved4a8052010-04-09 15:40:35 +04004111 tail = &thead;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004112
H. Peter Anvine2c80182005-01-15 22:15:51 +00004113 while (tline) { /* main token loop */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004114 if (!--deadman) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08004115 nasm_error(ERR_NONFATAL, "interminable macro recursion");
Cyrill Gorcunovbd38c8f2009-11-21 11:11:23 +03004116 goto err;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004117 }
H. Peter Anvincb1cf592007-11-19 12:26:50 -08004118
H. Peter Anvine2c80182005-01-15 22:15:51 +00004119 if ((mname = tline->text)) {
4120 /* if this token is a local macro, look in local context */
Cyrill Gorcunovc56d9ad2010-02-11 15:12:19 +03004121 if (tline->type == TOK_ID) {
4122 head = (SMacro *)hash_findix(&smacros, mname);
4123 } else if (tline->type == TOK_PREPROC_ID) {
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04004124 ctx = get_ctx(mname, &mname);
Cyrill Gorcunovc56d9ad2010-02-11 15:12:19 +03004125 head = ctx ? (SMacro *)hash_findix(&ctx->localmac, mname) : NULL;
4126 } else
4127 head = NULL;
H. Peter Anvin072771e2008-05-22 13:17:51 -07004128
H. Peter Anvine2c80182005-01-15 22:15:51 +00004129 /*
4130 * We've hit an identifier. As in is_mmacro below, we first
4131 * check whether the identifier is a single-line macro at
4132 * all, then think about checking for parameters if
4133 * necessary.
4134 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004135 list_for_each(m, head)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004136 if (!mstrcmp(m->name, mname, m->casesense))
4137 break;
4138 if (m) {
4139 mstart = tline;
4140 params = NULL;
4141 paramsize = NULL;
4142 if (m->nparam == 0) {
4143 /*
4144 * Simple case: the macro is parameterless. Discard the
4145 * one token that the macro call took, and push the
4146 * expansion back on the to-do stack.
4147 */
4148 if (!m->expansion) {
4149 if (!strcmp("__FILE__", m->name)) {
H. Peter Anvin274cda82016-05-10 02:56:29 -07004150 const char *file = src_get_fname();
4151 /* nasm_free(tline->text); here? */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004152 tline->text = nasm_quote(file, strlen(file));
4153 tline->type = TOK_STRING;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004154 continue;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004155 }
4156 if (!strcmp("__LINE__", m->name)) {
4157 nasm_free(tline->text);
4158 make_tok_num(tline, src_get_linnum());
4159 continue;
4160 }
4161 if (!strcmp("__BITS__", m->name)) {
4162 nasm_free(tline->text);
4163 make_tok_num(tline, globalbits);
4164 continue;
4165 }
4166 tline = delete_Token(tline);
4167 continue;
4168 }
4169 } else {
4170 /*
4171 * Complicated case: at least one macro with this name
H. Peter Anvine2c80182005-01-15 22:15:51 +00004172 * exists and takes parameters. We must find the
4173 * parameters in the call, count them, find the SMacro
4174 * that corresponds to that form of the macro call, and
4175 * substitute for the parameters when we expand. What a
4176 * pain.
4177 */
4178 /*tline = tline->next;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004179 skip_white_(tline); */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004180 do {
4181 t = tline->next;
4182 while (tok_type_(t, TOK_SMAC_END)) {
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004183 t->a.mac->in_progress = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004184 t->text = NULL;
4185 t = tline->next = delete_Token(t);
4186 }
4187 tline = t;
4188 } while (tok_type_(tline, TOK_WHITESPACE));
4189 if (!tok_is_(tline, "(")) {
4190 /*
4191 * This macro wasn't called with parameters: ignore
4192 * the call. (Behaviour borrowed from gnu cpp.)
4193 */
4194 tline = mstart;
4195 m = NULL;
4196 } else {
4197 int paren = 0;
4198 int white = 0;
4199 brackets = 0;
4200 nparam = 0;
4201 sparam = PARAM_DELTA;
4202 params = nasm_malloc(sparam * sizeof(Token *));
4203 params[0] = tline->next;
4204 paramsize = nasm_malloc(sparam * sizeof(int));
4205 paramsize[0] = 0;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07004206 while (true) { /* parameter loop */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004207 /*
4208 * For some unusual expansions
4209 * which concatenates function call
4210 */
4211 t = tline->next;
4212 while (tok_type_(t, TOK_SMAC_END)) {
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004213 t->a.mac->in_progress = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004214 t->text = NULL;
4215 t = tline->next = delete_Token(t);
4216 }
4217 tline = t;
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00004218
H. Peter Anvine2c80182005-01-15 22:15:51 +00004219 if (!tline) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08004220 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00004221 "macro call expects terminating `)'");
4222 break;
4223 }
4224 if (tline->type == TOK_WHITESPACE
4225 && brackets <= 0) {
4226 if (paramsize[nparam])
4227 white++;
4228 else
4229 params[nparam] = tline->next;
4230 continue; /* parameter loop */
4231 }
4232 if (tline->type == TOK_OTHER
4233 && tline->text[1] == 0) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004234 char ch = tline->text[0];
H. Peter Anvine2c80182005-01-15 22:15:51 +00004235 if (ch == ',' && !paren && brackets <= 0) {
4236 if (++nparam >= sparam) {
4237 sparam += PARAM_DELTA;
4238 params = nasm_realloc(params,
Cyrill Gorcunoved4a8052010-04-09 15:40:35 +04004239 sparam * sizeof(Token *));
4240 paramsize = nasm_realloc(paramsize,
4241 sparam * sizeof(int));
H. Peter Anvine2c80182005-01-15 22:15:51 +00004242 }
4243 params[nparam] = tline->next;
4244 paramsize[nparam] = 0;
4245 white = 0;
4246 continue; /* parameter loop */
4247 }
4248 if (ch == '{' &&
4249 (brackets > 0 || (brackets == 0 &&
4250 !paramsize[nparam])))
4251 {
4252 if (!(brackets++)) {
4253 params[nparam] = tline->next;
4254 continue; /* parameter loop */
4255 }
4256 }
4257 if (ch == '}' && brackets > 0)
4258 if (--brackets == 0) {
4259 brackets = -1;
4260 continue; /* parameter loop */
4261 }
4262 if (ch == '(' && !brackets)
4263 paren++;
4264 if (ch == ')' && brackets <= 0)
4265 if (--paren < 0)
4266 break;
4267 }
4268 if (brackets < 0) {
4269 brackets = 0;
H. Peter Anvin130736c2016-02-17 20:27:41 -08004270 nasm_error(ERR_NONFATAL, "braces do not "
H. Peter Anvine2c80182005-01-15 22:15:51 +00004271 "enclose all of macro parameter");
4272 }
4273 paramsize[nparam] += white + 1;
4274 white = 0;
4275 } /* parameter loop */
4276 nparam++;
4277 while (m && (m->nparam != nparam ||
4278 mstrcmp(m->name, mname,
4279 m->casesense)))
4280 m = m->next;
4281 if (!m)
H. Peter Anvin130736c2016-02-17 20:27:41 -08004282 nasm_error(ERR_WARNING|ERR_PASS1|ERR_WARN_MNP,
H. Peter Anvine2c80182005-01-15 22:15:51 +00004283 "macro `%s' exists, "
4284 "but not taking %d parameters",
4285 mstart->text, nparam);
4286 }
4287 }
4288 if (m && m->in_progress)
4289 m = NULL;
4290 if (!m) { /* in progess or didn't find '(' or wrong nparam */
H. Peter Anvin70653092007-10-19 14:42:29 -07004291 /*
H. Peter Anvine2c80182005-01-15 22:15:51 +00004292 * Design question: should we handle !tline, which
4293 * indicates missing ')' here, or expand those
4294 * macros anyway, which requires the (t) test a few
H. Peter Anvin70653092007-10-19 14:42:29 -07004295 * lines down?
H. Peter Anvine2c80182005-01-15 22:15:51 +00004296 */
4297 nasm_free(params);
4298 nasm_free(paramsize);
4299 tline = mstart;
4300 } else {
4301 /*
4302 * Expand the macro: we are placed on the last token of the
4303 * call, so that we can easily split the call from the
4304 * following tokens. We also start by pushing an SMAC_END
4305 * token for the cycle removal.
4306 */
4307 t = tline;
4308 if (t) {
4309 tline = t->next;
4310 t->next = NULL;
4311 }
4312 tt = new_Token(tline, TOK_SMAC_END, NULL, 0);
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004313 tt->a.mac = m;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07004314 m->in_progress = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004315 tline = tt;
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04004316 list_for_each(t, m->expansion) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004317 if (t->type >= TOK_SMAC_PARAM) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00004318 Token *pcopy = tline, **ptail = &pcopy;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004319 Token *ttt, *pt;
4320 int i;
H. Peter Anvin734b1882002-04-30 21:01:08 +00004321
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004322 ttt = params[t->type - TOK_SMAC_PARAM];
4323 i = paramsize[t->type - TOK_SMAC_PARAM];
4324 while (--i >= 0) {
4325 pt = *ptail = new_Token(tline, ttt->type,
4326 ttt->text, 0);
4327 ptail = &pt->next;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004328 ttt = ttt->next;
4329 }
4330 tline = pcopy;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004331 } else if (t->type == TOK_PREPROC_Q) {
4332 tt = new_Token(tline, TOK_ID, mname, 0);
4333 tline = tt;
4334 } else if (t->type == TOK_PREPROC_QQ) {
4335 tt = new_Token(tline, TOK_ID, m->name, 0);
4336 tline = tt;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004337 } else {
4338 tt = new_Token(tline, t->type, t->text, 0);
4339 tline = tt;
4340 }
4341 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00004342
H. Peter Anvine2c80182005-01-15 22:15:51 +00004343 /*
4344 * Having done that, get rid of the macro call, and clean
4345 * up the parameters.
4346 */
4347 nasm_free(params);
4348 nasm_free(paramsize);
4349 free_tlist(mstart);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004350 expanded = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004351 continue; /* main token loop */
4352 }
4353 }
4354 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004355
H. Peter Anvine2c80182005-01-15 22:15:51 +00004356 if (tline->type == TOK_SMAC_END) {
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004357 tline->a.mac->in_progress = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004358 tline = delete_Token(tline);
4359 } else {
4360 t = *tail = tline;
4361 tline = tline->next;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004362 t->a.mac = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004363 t->next = NULL;
4364 tail = &t->next;
4365 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004366 }
4367
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004368 /*
4369 * Now scan the entire line and look for successive TOK_IDs that resulted
Keith Kaniosb7a89542007-04-12 02:40:54 +00004370 * after expansion (they can't be produced by tokenize()). The successive
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004371 * TOK_IDs should be concatenated.
4372 * Also we look for %+ tokens and concatenate the tokens before and after
4373 * them (without white spaces in between).
4374 */
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +04004375 if (expanded) {
Cyrill Gorcunovc6a742c2011-06-27 01:23:09 +04004376 const struct tokseq_match t[] = {
4377 {
4378 PP_CONCAT_MASK(TOK_ID) |
4379 PP_CONCAT_MASK(TOK_PREPROC_ID), /* head */
4380 PP_CONCAT_MASK(TOK_ID) |
4381 PP_CONCAT_MASK(TOK_PREPROC_ID) |
4382 PP_CONCAT_MASK(TOK_NUMBER) /* tail */
4383 }
4384 };
4385 if (paste_tokens(&thead, t, ARRAY_SIZE(t), true)) {
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +04004386 /*
4387 * If we concatenated something, *and* we had previously expanded
4388 * an actual macro, scan the lines again for macros...
4389 */
4390 tline = thead;
4391 expanded = false;
4392 goto again;
4393 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00004394 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004395
Cyrill Gorcunovbd38c8f2009-11-21 11:11:23 +03004396err:
H. Peter Anvine2c80182005-01-15 22:15:51 +00004397 if (org_tline) {
4398 if (thead) {
4399 *org_tline = *thead;
4400 /* since we just gave text to org_line, don't free it */
4401 thead->text = NULL;
4402 delete_Token(thead);
4403 } else {
4404 /* the expression expanded to empty line;
4405 we can't return NULL for some reasons
4406 we just set the line to a single WHITESPACE token. */
4407 memset(org_tline, 0, sizeof(*org_tline));
4408 org_tline->text = NULL;
4409 org_tline->type = TOK_WHITESPACE;
4410 }
4411 thead = org_tline;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004412 }
4413
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004414 return thead;
4415}
4416
4417/*
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004418 * Similar to expand_smacro but used exclusively with macro identifiers
4419 * right before they are fetched in. The reason is that there can be
4420 * identifiers consisting of several subparts. We consider that if there
4421 * are more than one element forming the name, user wants a expansion,
4422 * otherwise it will be left as-is. Example:
4423 *
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004424 * %define %$abc cde
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004425 *
4426 * the identifier %$abc will be left as-is so that the handler for %define
4427 * will suck it and define the corresponding value. Other case:
4428 *
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004429 * %define _%$abc cde
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004430 *
4431 * In this case user wants name to be expanded *before* %define starts
4432 * working, so we'll expand %$abc into something (if it has a value;
4433 * otherwise it will be left as-is) then concatenate all successive
4434 * PP_IDs into one.
4435 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004436static Token *expand_id(Token * tline)
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004437{
4438 Token *cur, *oldnext = NULL;
4439
H. Peter Anvin734b1882002-04-30 21:01:08 +00004440 if (!tline || !tline->next)
H. Peter Anvine2c80182005-01-15 22:15:51 +00004441 return tline;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004442
4443 cur = tline;
4444 while (cur->next &&
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004445 (cur->next->type == TOK_ID ||
4446 cur->next->type == TOK_PREPROC_ID
4447 || cur->next->type == TOK_NUMBER))
H. Peter Anvine2c80182005-01-15 22:15:51 +00004448 cur = cur->next;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004449
4450 /* If identifier consists of just one token, don't expand */
4451 if (cur == tline)
H. Peter Anvine2c80182005-01-15 22:15:51 +00004452 return tline;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004453
H. Peter Anvine2c80182005-01-15 22:15:51 +00004454 if (cur) {
4455 oldnext = cur->next; /* Detach the tail past identifier */
4456 cur->next = NULL; /* so that expand_smacro stops here */
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004457 }
4458
H. Peter Anvin734b1882002-04-30 21:01:08 +00004459 tline = expand_smacro(tline);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004460
H. Peter Anvine2c80182005-01-15 22:15:51 +00004461 if (cur) {
4462 /* expand_smacro possibly changhed tline; re-scan for EOL */
4463 cur = tline;
4464 while (cur && cur->next)
4465 cur = cur->next;
4466 if (cur)
4467 cur->next = oldnext;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004468 }
4469
4470 return tline;
4471}
4472
4473/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004474 * Determine whether the given line constitutes a multi-line macro
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004475 * call, and return the MMacro structure called if so. Doesn't have
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004476 * to check for an initial label - that's taken care of in
4477 * expand_mmacro - but must check numbers of parameters. Guaranteed
4478 * to be called with tline->type == TOK_ID, so the putative macro
4479 * name is easy to find.
4480 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004481static MMacro *is_mmacro(Token * tline, Token *** params_array)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004482{
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004483 MMacro *head, *m;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004484 Token **params;
4485 int nparam;
4486
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004487 head = (MMacro *) hash_findix(&mmacros, tline->text);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004488
4489 /*
4490 * Efficiency: first we see if any macro exists with the given
4491 * name. If not, we can return NULL immediately. _Then_ we
4492 * count the parameters, and then we look further along the
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004493 * list if necessary to find the proper MMacro.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004494 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004495 list_for_each(m, head)
4496 if (!mstrcmp(m->name, tline->text, m->casesense))
H. Peter Anvine2c80182005-01-15 22:15:51 +00004497 break;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004498 if (!m)
H. Peter Anvine2c80182005-01-15 22:15:51 +00004499 return NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004500
4501 /*
4502 * OK, we have a potential macro. Count and demarcate the
4503 * parameters.
4504 */
H. Peter Anvin734b1882002-04-30 21:01:08 +00004505 count_mmac_params(tline->next, &nparam, &params);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004506
4507 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004508 * So we know how many parameters we've got. Find the MMacro
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004509 * structure that handles this number.
4510 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004511 while (m) {
4512 if (m->nparam_min <= nparam
4513 && (m->plus || nparam <= m->nparam_max)) {
4514 /*
4515 * This one is right. Just check if cycle removal
4516 * prohibits us using it before we actually celebrate...
4517 */
4518 if (m->in_progress > m->max_depth) {
4519 if (m->max_depth > 0) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08004520 nasm_error(ERR_WARNING,
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004521 "reached maximum recursion depth of %i",
4522 m->max_depth);
4523 }
4524 nasm_free(params);
4525 return NULL;
4526 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00004527 /*
4528 * It's right, and we can use it. Add its default
4529 * parameters to the end of our list if necessary.
4530 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004531 if (m->defaults && nparam < m->nparam_min + m->ndefs) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00004532 params =
4533 nasm_realloc(params,
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004534 ((m->nparam_min + m->ndefs +
H. Peter Anvine2c80182005-01-15 22:15:51 +00004535 1) * sizeof(*params)));
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004536 while (nparam < m->nparam_min + m->ndefs) {
4537 params[nparam] = m->defaults[nparam - m->nparam_min];
H. Peter Anvine2c80182005-01-15 22:15:51 +00004538 nparam++;
4539 }
4540 }
4541 /*
4542 * If we've gone over the maximum parameter count (and
4543 * we're in Plus mode), ignore parameters beyond
4544 * nparam_max.
4545 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004546 if (m->plus && nparam > m->nparam_max)
4547 nparam = m->nparam_max;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004548 /*
4549 * Then terminate the parameter list, and leave.
4550 */
4551 if (!params) { /* need this special case */
4552 params = nasm_malloc(sizeof(*params));
4553 nparam = 0;
4554 }
4555 params[nparam] = NULL;
4556 *params_array = params;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004557 return m;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004558 }
4559 /*
4560 * This one wasn't right: look for the next one with the
4561 * same name.
4562 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004563 list_for_each(m, m->next)
4564 if (!mstrcmp(m->name, tline->text, m->casesense))
H. Peter Anvine2c80182005-01-15 22:15:51 +00004565 break;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004566 }
4567
4568 /*
4569 * After all that, we didn't find one with the right number of
4570 * parameters. Issue a warning, and fail to expand the macro.
4571 */
H. Peter Anvin130736c2016-02-17 20:27:41 -08004572 nasm_error(ERR_WARNING|ERR_PASS1|ERR_WARN_MNP,
H. Peter Anvine2c80182005-01-15 22:15:51 +00004573 "macro `%s' exists, but not taking %d parameters",
4574 tline->text, nparam);
H. Peter Anvin734b1882002-04-30 21:01:08 +00004575 nasm_free(params);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004576 return NULL;
4577}
4578
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004579
4580/*
4581 * Save MMacro invocation specific fields in
4582 * preparation for a recursive macro expansion
4583 */
4584static void push_mmacro(MMacro *m)
4585{
4586 MMacroInvocation *i;
4587
4588 i = nasm_malloc(sizeof(MMacroInvocation));
4589 i->prev = m->prev;
4590 i->params = m->params;
4591 i->iline = m->iline;
4592 i->nparam = m->nparam;
4593 i->rotate = m->rotate;
4594 i->paramlen = m->paramlen;
4595 i->unique = m->unique;
4596 i->condcnt = m->condcnt;
4597 m->prev = i;
4598}
4599
4600
4601/*
4602 * Restore MMacro invocation specific fields that were
4603 * saved during a previous recursive macro expansion
4604 */
4605static void pop_mmacro(MMacro *m)
4606{
4607 MMacroInvocation *i;
4608
4609 if (m->prev) {
4610 i = m->prev;
4611 m->prev = i->prev;
4612 m->params = i->params;
4613 m->iline = i->iline;
4614 m->nparam = i->nparam;
4615 m->rotate = i->rotate;
4616 m->paramlen = i->paramlen;
4617 m->unique = i->unique;
4618 m->condcnt = i->condcnt;
4619 nasm_free(i);
4620 }
4621}
4622
4623
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004624/*
4625 * Expand the multi-line macro call made by the given line, if
4626 * there is one to be expanded. If there is, push the expansion on
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004627 * istk->expansion and return 1. Otherwise return 0.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004628 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004629static int expand_mmacro(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004630{
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004631 Token *startline = tline;
H. Peter Anvineba20a72002-04-30 20:53:55 +00004632 Token *label = NULL;
4633 int dont_prepend = 0;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004634 Token **params, *t, *tt;
4635 MMacro *m;
4636 Line *l, *ll;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004637 int i, nparam, *paramlen;
H. Peter Anvinc751e862008-06-09 10:18:45 -07004638 const char *mname;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004639
4640 t = tline;
H. Peter Anvineba20a72002-04-30 20:53:55 +00004641 skip_white_(t);
H. Peter Anvince2233b2008-05-25 21:57:00 -07004642 /* if (!tok_type_(t, TOK_ID)) Lino 02/25/02 */
H. Peter Anvindce1e2f2002-04-30 21:06:37 +00004643 if (!tok_type_(t, TOK_ID) && !tok_type_(t, TOK_PREPROC_ID))
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004644 return 0;
4645 m = is_mmacro(t, &params);
4646 if (m) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004647 mname = t->text;
H. Peter Anvinc751e862008-06-09 10:18:45 -07004648 } else {
H. Peter Anvine2c80182005-01-15 22:15:51 +00004649 Token *last;
4650 /*
4651 * We have an id which isn't a macro call. We'll assume
4652 * it might be a label; we'll also check to see if a
4653 * colon follows it. Then, if there's another id after
4654 * that lot, we'll check it again for macro-hood.
4655 */
4656 label = last = t;
4657 t = t->next;
4658 if (tok_type_(t, TOK_WHITESPACE))
4659 last = t, t = t->next;
4660 if (tok_is_(t, ":")) {
4661 dont_prepend = 1;
4662 last = t, t = t->next;
4663 if (tok_type_(t, TOK_WHITESPACE))
4664 last = t, t = t->next;
4665 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004666 if (!tok_type_(t, TOK_ID) || !(m = is_mmacro(t, &params)))
4667 return 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004668 last->next = NULL;
Keith Kanios891775e2009-07-11 06:08:54 -05004669 mname = t->text;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004670 tline = t;
H. Peter Anvineba20a72002-04-30 20:53:55 +00004671 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004672
4673 /*
4674 * Fix up the parameters: this involves stripping leading and
4675 * trailing whitespace, then stripping braces if they are
4676 * present.
4677 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004678 for (nparam = 0; params[nparam]; nparam++) ;
H. Peter Anvin734b1882002-04-30 21:01:08 +00004679 paramlen = nparam ? nasm_malloc(nparam * sizeof(*paramlen)) : NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004680
H. Peter Anvine2c80182005-01-15 22:15:51 +00004681 for (i = 0; params[i]; i++) {
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08004682 int brace = 0;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004683 int comma = (!m->plus || i < nparam - 1);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004684
H. Peter Anvine2c80182005-01-15 22:15:51 +00004685 t = params[i];
4686 skip_white_(t);
4687 if (tok_is_(t, "{"))
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08004688 t = t->next, brace++, comma = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004689 params[i] = t;
4690 paramlen[i] = 0;
4691 while (t) {
4692 if (comma && t->type == TOK_OTHER && !strcmp(t->text, ","))
4693 break; /* ... because we have hit a comma */
4694 if (comma && t->type == TOK_WHITESPACE
4695 && tok_is_(t->next, ","))
4696 break; /* ... or a space then a comma */
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08004697 if (brace && t->type == TOK_OTHER) {
4698 if (t->text[0] == '{')
4699 brace++; /* ... or a nested opening brace */
4700 else if (t->text[0] == '}')
4701 if (!--brace)
4702 break; /* ... or a brace */
4703 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00004704 t = t->next;
4705 paramlen[i]++;
4706 }
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08004707 if (brace)
H. Peter Anvin130736c2016-02-17 20:27:41 -08004708 nasm_error(ERR_NONFATAL, "macro params should be enclosed in braces");
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004709 }
4710
4711 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004712 * OK, we have a MMacro structure together with a set of
4713 * parameters. We must now go through the expansion and push
4714 * copies of each Line on to istk->expansion. Substitution of
H. Peter Anvin76690a12002-04-30 20:52:49 +00004715 * parameter tokens and macro-local tokens doesn't get done
4716 * until the single-line macro substitution process; this is
4717 * because delaying them allows us to change the semantics
4718 * later through %rotate.
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004719 *
4720 * First, push an end marker on to istk->expansion, mark this
4721 * macro as in progress, and set up its invocation-specific
4722 * variables.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004723 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004724 ll = nasm_malloc(sizeof(Line));
4725 ll->next = istk->expansion;
4726 ll->finishes = m;
4727 ll->first = NULL;
4728 istk->expansion = ll;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004729
4730 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004731 * Save the previous MMacro expansion in the case of
4732 * macro recursion
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004733 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004734 if (m->max_depth && m->in_progress)
4735 push_mmacro(m);
4736
4737 m->in_progress ++;
4738 m->params = params;
4739 m->iline = tline;
4740 m->nparam = nparam;
4741 m->rotate = 0;
4742 m->paramlen = paramlen;
4743 m->unique = unique++;
4744 m->lineno = 0;
4745 m->condcnt = 0;
4746
4747 m->next_active = istk->mstk;
4748 istk->mstk = m;
4749
4750 list_for_each(l, m->expansion) {
4751 Token **tail;
4752
4753 ll = nasm_malloc(sizeof(Line));
4754 ll->finishes = NULL;
4755 ll->next = istk->expansion;
4756 istk->expansion = ll;
4757 tail = &ll->first;
4758
4759 list_for_each(t, l->first) {
4760 Token *x = t;
4761 switch (t->type) {
4762 case TOK_PREPROC_Q:
4763 tt = *tail = new_Token(NULL, TOK_ID, mname, 0);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004764 break;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004765 case TOK_PREPROC_QQ:
4766 tt = *tail = new_Token(NULL, TOK_ID, m->name, 0);
4767 break;
4768 case TOK_PREPROC_ID:
4769 if (t->text[1] == '0' && t->text[2] == '0') {
4770 dont_prepend = -1;
4771 x = label;
4772 if (!x)
4773 continue;
4774 }
4775 /* fall through */
4776 default:
4777 tt = *tail = new_Token(NULL, x->type, x->text, 0);
4778 break;
4779 }
4780 tail = &tt->next;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004781 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004782 *tail = NULL;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004783 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004784
4785 /*
H. Peter Anvineba20a72002-04-30 20:53:55 +00004786 * If we had a label, push it on as the first line of
4787 * the macro expansion.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004788 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004789 if (label) {
4790 if (dont_prepend < 0)
4791 free_tlist(startline);
4792 else {
4793 ll = nasm_malloc(sizeof(Line));
4794 ll->finishes = NULL;
4795 ll->next = istk->expansion;
4796 istk->expansion = ll;
4797 ll->first = startline;
4798 if (!dont_prepend) {
4799 while (label->next)
4800 label = label->next;
4801 label->next = tt = new_Token(NULL, TOK_OTHER, ":", 0);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004802 }
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004803 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004804 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004805
H. Peter Anvin8ac25aa2016-02-18 01:16:18 -08004806 lfmt->uplevel(m->nolist ? LIST_MACRO_NOLIST : LIST_MACRO);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004807
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004808 return 1;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004809}
4810
H. Peter Anvin130736c2016-02-17 20:27:41 -08004811/*
4812 * This function adds macro names to error messages, and suppresses
4813 * them if necessary.
4814 */
4815static void pp_verror(int severity, const char *fmt, va_list arg)
Victor van den Elzen3b404c02008-09-18 13:51:36 +02004816{
H. Peter Anvin130736c2016-02-17 20:27:41 -08004817 char buff[BUFSIZ];
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004818 MMacro *mmac = NULL;
4819 int delta = 0;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02004820
H. Peter Anvin130736c2016-02-17 20:27:41 -08004821 /*
4822 * If we're in a dead branch of IF or something like it, ignore the error.
4823 * However, because %else etc are evaluated in the state context
4824 * of the previous branch, errors might get lost:
4825 * %if 0 ... %else trailing garbage ... %endif
4826 * So %else etc should set the ERR_PP_PRECOND flag.
4827 */
4828 if ((severity & ERR_MASK) < ERR_FATAL &&
4829 istk && istk->conds &&
4830 ((severity & ERR_PP_PRECOND) ?
4831 istk->conds->state == COND_NEVER :
H. Peter Anvineb6653f2016-04-05 13:03:10 -07004832 !emitting(istk->conds->state)))
H. Peter Anvin130736c2016-02-17 20:27:41 -08004833 return;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02004834
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004835 /* get %macro name */
H. Peter Anvin130736c2016-02-17 20:27:41 -08004836 if (!(severity & ERR_NOFILE) && istk && istk->mstk) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004837 mmac = istk->mstk;
4838 /* but %rep blocks should be skipped */
4839 while (mmac && !mmac->name)
4840 mmac = mmac->next_active, delta++;
Cyrill Gorcunov9900c6b2011-10-09 18:58:46 +04004841 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004842
H. Peter Anvin130736c2016-02-17 20:27:41 -08004843 if (mmac) {
4844 vsnprintf(buff, sizeof(buff), fmt, arg);
Victor van den Elzen3b404c02008-09-18 13:51:36 +02004845
H. Peter Anvin130736c2016-02-17 20:27:41 -08004846 nasm_set_verror(real_verror);
4847 nasm_error(severity, "(%s:%d) %s",
4848 mmac->name, mmac->lineno - delta, buff);
4849 nasm_set_verror(pp_verror);
4850 } else {
4851 real_verror(severity, fmt, arg);
4852 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004853}
4854
H. Peter Anvin734b1882002-04-30 21:01:08 +00004855static void
H. Peter Anvin130736c2016-02-17 20:27:41 -08004856pp_reset(char *file, int apass, StrList **deplist)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004857{
H. Peter Anvin7383b402008-09-24 10:20:40 -07004858 Token *t;
4859
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004860 cstk = NULL;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004861 istk = nasm_malloc(sizeof(Include));
4862 istk->next = NULL;
4863 istk->conds = NULL;
4864 istk->expansion = NULL;
4865 istk->mstk = NULL;
H. Peter Anvin3e83cec2016-05-25 04:28:46 -07004866 istk->fp = nasm_open_read(file, NF_TEXT);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004867 istk->fname = NULL;
H. Peter Anvin274cda82016-05-10 02:56:29 -07004868 src_set(0, file);
H. Peter Anvineba20a72002-04-30 20:53:55 +00004869 istk->lineinc = 1;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004870 if (!istk->fp)
H. Peter Anvin130736c2016-02-17 20:27:41 -08004871 nasm_fatal(ERR_NOFILE, "unable to open input file `%s'", file);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004872 defining = NULL;
Charles Crayned4200be2008-07-12 16:42:33 -07004873 nested_mac_count = 0;
4874 nested_rep_count = 0;
H. Peter Anvin97a23472007-09-16 17:57:25 -07004875 init_macros();
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004876 unique = 0;
H. Peter Anvinf7606612016-07-13 14:23:48 -07004877
4878 if (tasm_compatible_mode)
4879 pp_add_stdmac(nasm_stdmac_tasm);
4880
4881 pp_add_stdmac(nasm_stdmac_nasm);
4882 pp_add_stdmac(nasm_stdmac_version);
4883
4884 stdmacpos = stdmacros[0];
4885 stdmacnext = &stdmacros[1];
4886
H. Peter Anvind2456592008-06-19 15:04:18 -07004887 do_predef = true;
H. Peter Anvin61f130f2008-09-25 15:45:06 -07004888
4889 /*
4890 * 0 for dependencies, 1 for preparatory passes, 2 for final pass.
4891 * The caller, however, will also pass in 3 for preprocess-only so
4892 * we can set __PASS__ accordingly.
4893 */
4894 pass = apass > 2 ? 2 : apass;
4895
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07004896 dephead = deptail = deplist;
4897 if (deplist) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004898 StrList *sl = nasm_malloc(strlen(file)+1+sizeof sl->next);
4899 sl->next = NULL;
4900 strcpy(sl->str, file);
4901 *deptail = sl;
4902 deptail = &sl->next;
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07004903 }
H. Peter Anvin7383b402008-09-24 10:20:40 -07004904
H. Peter Anvin61f130f2008-09-25 15:45:06 -07004905 /*
4906 * Define the __PASS__ macro. This is defined here unlike
4907 * all the other builtins, because it is special -- it varies between
4908 * passes.
4909 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004910 t = nasm_malloc(sizeof(*t));
4911 t->next = NULL;
H. Peter Anvin61f130f2008-09-25 15:45:06 -07004912 make_tok_num(t, apass);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004913 t->a.mac = NULL;
H. Peter Anvin7383b402008-09-24 10:20:40 -07004914 define_smacro(NULL, "__PASS__", true, 0, t);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004915}
4916
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004917static char *pp_getline(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004918{
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004919 char *line;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004920 Token *tline;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004921
H. Peter Anvin130736c2016-02-17 20:27:41 -08004922 real_verror = nasm_set_verror(pp_verror);
H. Peter Anvin215186f2016-02-17 20:27:41 -08004923
H. Peter Anvine2c80182005-01-15 22:15:51 +00004924 while (1) {
4925 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004926 * Fetch a tokenized line, either from the macro-expansion
H. Peter Anvine2c80182005-01-15 22:15:51 +00004927 * buffer or from the input file.
4928 */
4929 tline = NULL;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004930 while (istk->expansion && istk->expansion->finishes) {
4931 Line *l = istk->expansion;
4932 if (!l->finishes->name && l->finishes->in_progress > 1) {
4933 Line *ll;
H. Peter Anvineba20a72002-04-30 20:53:55 +00004934
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004935 /*
4936 * This is a macro-end marker for a macro with no
4937 * name, which means it's not really a macro at all
4938 * but a %rep block, and the `in_progress' field is
4939 * more than 1, meaning that we still need to
4940 * repeat. (1 means the natural last repetition; 0
4941 * means termination by %exitrep.) We have
4942 * therefore expanded up to the %endrep, and must
4943 * push the whole block on to the expansion buffer
4944 * again. We don't bother to remove the macro-end
4945 * marker: we'd only have to generate another one
4946 * if we did.
4947 */
4948 l->finishes->in_progress--;
4949 list_for_each(l, l->finishes->expansion) {
4950 Token *t, *tt, **tail;
4951
4952 ll = nasm_malloc(sizeof(Line));
4953 ll->next = istk->expansion;
4954 ll->finishes = NULL;
4955 ll->first = NULL;
4956 tail = &ll->first;
4957
4958 list_for_each(t, l->first) {
4959 if (t->text || t->type == TOK_WHITESPACE) {
4960 tt = *tail = new_Token(NULL, t->type, t->text, 0);
4961 tail = &tt->next;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004962 }
4963 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004964
4965 istk->expansion = ll;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004966 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004967 } else {
4968 /*
4969 * Check whether a `%rep' was started and not ended
4970 * within this macro expansion. This can happen and
4971 * should be detected. It's a fatal error because
4972 * I'm too confused to work out how to recover
4973 * sensibly from it.
4974 */
4975 if (defining) {
4976 if (defining->name)
H. Peter Anvin130736c2016-02-17 20:27:41 -08004977 nasm_panic(0, "defining with name in expansion");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004978 else if (istk->mstk->name)
H. Peter Anvin130736c2016-02-17 20:27:41 -08004979 nasm_fatal(0, "`%%rep' without `%%endrep' within"
4980 " expansion of macro `%s'",
4981 istk->mstk->name);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004982 }
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004983
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004984 /*
4985 * FIXME: investigate the relationship at this point between
4986 * istk->mstk and l->finishes
4987 */
4988 {
4989 MMacro *m = istk->mstk;
4990 istk->mstk = m->next_active;
4991 if (m->name) {
4992 /*
4993 * This was a real macro call, not a %rep, and
4994 * therefore the parameter information needs to
4995 * be freed.
4996 */
4997 if (m->prev) {
4998 pop_mmacro(m);
4999 l->finishes->in_progress --;
5000 } else {
5001 nasm_free(m->params);
5002 free_tlist(m->iline);
5003 nasm_free(m->paramlen);
5004 l->finishes->in_progress = 0;
5005 }
5006 } else
5007 free_mmacro(m);
5008 }
5009 istk->expansion = l->next;
5010 nasm_free(l);
H. Peter Anvin8ac25aa2016-02-18 01:16:18 -08005011 lfmt->downlevel(LIST_MACRO);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005012 }
5013 }
5014 while (1) { /* until we get a line we can use */
5015
5016 if (istk->expansion) { /* from a macro expansion */
5017 char *p;
5018 Line *l = istk->expansion;
5019 if (istk->mstk)
5020 istk->mstk->lineno++;
5021 tline = l->first;
5022 istk->expansion = l->next;
5023 nasm_free(l);
5024 p = detoken(tline, false);
H. Peter Anvin8ac25aa2016-02-18 01:16:18 -08005025 lfmt->line(LIST_MACRO, p);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005026 nasm_free(p);
5027 break;
5028 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00005029 line = read_line();
5030 if (line) { /* from the current input file */
5031 line = prepreproc(line);
Keith Kaniosb7a89542007-04-12 02:40:54 +00005032 tline = tokenize(line);
H. Peter Anvine2c80182005-01-15 22:15:51 +00005033 nasm_free(line);
5034 break;
5035 }
5036 /*
5037 * The current file has ended; work down the istk
5038 */
5039 {
5040 Include *i = istk;
5041 fclose(i->fp);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005042 if (i->conds) {
5043 /* nasm_error can't be conditionally suppressed */
H. Peter Anvin41087062016-03-03 14:27:34 -08005044 nasm_fatal(0,
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005045 "expected `%%endif' before end of file");
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005046 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00005047 /* only set line and file name if there's a next node */
H. Peter Anvin274cda82016-05-10 02:56:29 -07005048 if (i->next)
5049 src_set(i->lineno, i->fname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00005050 istk = i->next;
H. Peter Anvin8ac25aa2016-02-18 01:16:18 -08005051 lfmt->downlevel(LIST_INCLUDE);
H. Peter Anvine2c80182005-01-15 22:15:51 +00005052 nasm_free(i);
H. Peter Anvin130736c2016-02-17 20:27:41 -08005053 if (!istk) {
5054 line = NULL;
5055 goto done;
5056 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005057 if (istk->expansion && istk->expansion->finishes)
5058 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005059 }
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005060 }
5061
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005062 /*
5063 * We must expand MMacro parameters and MMacro-local labels
5064 * _before_ we plunge into directive processing, to cope
5065 * with things like `%define something %1' such as STRUC
5066 * uses. Unless we're _defining_ a MMacro, in which case
5067 * those tokens should be left alone to go into the
5068 * definition; and unless we're in a non-emitting
5069 * condition, in which case we don't want to meddle with
5070 * anything.
5071 */
5072 if (!defining && !(istk->conds && !emitting(istk->conds->state))
5073 && !(istk->mstk && !istk->mstk->in_progress)) {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005074 tline = expand_mmac_params(tline);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005075 }
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005076
H. Peter Anvine2c80182005-01-15 22:15:51 +00005077 /*
5078 * Check the line to see if it's a preprocessor directive.
5079 */
5080 if (do_directive(tline) == DIRECTIVE_FOUND) {
5081 continue;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005082 } else if (defining) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00005083 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005084 * We're defining a multi-line macro. We emit nothing
5085 * at all, and just
5086 * shove the tokenized line on to the macro definition.
H. Peter Anvine2c80182005-01-15 22:15:51 +00005087 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005088 Line *l = nasm_malloc(sizeof(Line));
5089 l->next = defining->expansion;
5090 l->first = tline;
5091 l->finishes = NULL;
5092 defining->expansion = l;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005093 continue;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005094 } else if (istk->conds && !emitting(istk->conds->state)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00005095 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005096 * We're in a non-emitting branch of a condition block.
H. Peter Anvine2c80182005-01-15 22:15:51 +00005097 * Emit nothing at all, not even a blank line: when we
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005098 * emerge from the condition we'll give a line-number
H. Peter Anvine2c80182005-01-15 22:15:51 +00005099 * directive so we keep our place correctly.
5100 */
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005101 free_tlist(tline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00005102 continue;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005103 } else if (istk->mstk && !istk->mstk->in_progress) {
5104 /*
5105 * We're in a %rep block which has been terminated, so
5106 * we're walking through to the %endrep without
5107 * emitting anything. Emit nothing at all, not even a
5108 * blank line: when we emerge from the %rep block we'll
5109 * give a line-number directive so we keep our place
5110 * correctly.
5111 */
5112 free_tlist(tline);
5113 continue;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005114 } else {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005115 tline = expand_smacro(tline);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005116 if (!expand_mmacro(tline)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00005117 /*
Keith Kaniosb7a89542007-04-12 02:40:54 +00005118 * De-tokenize the line again, and emit it.
H. Peter Anvine2c80182005-01-15 22:15:51 +00005119 */
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005120 line = detoken(tline, true);
5121 free_tlist(tline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00005122 break;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005123 } else {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005124 continue; /* expand_mmacro calls free_tlist */
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005125 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00005126 }
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005127 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005128
H. Peter Anvin130736c2016-02-17 20:27:41 -08005129done:
5130 nasm_set_verror(real_verror);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005131 return line;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005132}
5133
H. Peter Anvine2c80182005-01-15 22:15:51 +00005134static void pp_cleanup(int pass)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005135{
H. Peter Anvin130736c2016-02-17 20:27:41 -08005136 real_verror = nasm_set_verror(pp_verror);
H. Peter Anvin215186f2016-02-17 20:27:41 -08005137
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005138 if (defining) {
5139 if (defining->name) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08005140 nasm_error(ERR_NONFATAL,
5141 "end of file while still defining macro `%s'",
5142 defining->name);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005143 } else {
H. Peter Anvin130736c2016-02-17 20:27:41 -08005144 nasm_error(ERR_NONFATAL, "end of file while still in %%rep");
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005145 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005146
5147 free_mmacro(defining);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005148 defining = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005149 }
H. Peter Anvin130736c2016-02-17 20:27:41 -08005150
5151 nasm_set_verror(real_verror);
H. Peter Anvin215186f2016-02-17 20:27:41 -08005152
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005153 while (cstk)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005154 ctx_pop();
H. Peter Anvin97a23472007-09-16 17:57:25 -07005155 free_macros();
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005156 while (istk) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00005157 Include *i = istk;
5158 istk = istk->next;
5159 fclose(i->fp);
Cyrill Gorcunov8dcfd882011-03-03 09:18:56 +03005160 nasm_free(i);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005161 }
5162 while (cstk)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005163 ctx_pop();
H. Peter Anvin274cda82016-05-10 02:56:29 -07005164 src_set_fname(NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00005165 if (pass == 0) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03005166 IncPath *i;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005167 free_llist(predef);
Cyrill Gorcunovdae24d72014-06-28 10:17:39 +04005168 predef = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005169 delete_Blocks();
Cyrill Gorcunovdae24d72014-06-28 10:17:39 +04005170 freeTokens = NULL;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03005171 while ((i = ipath)) {
5172 ipath = i->next;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005173 if (i->path)
5174 nasm_free(i->path);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03005175 nasm_free(i);
5176 }
H. Peter Anvin11dfa1a2008-07-02 18:11:04 -07005177 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005178}
5179
Cyrill Gorcunov0b78bff2012-05-07 01:57:55 +04005180static void pp_include_path(char *path)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005181{
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005182 IncPath *i;
H. Peter Anvin37a321f2007-09-24 13:41:58 -07005183
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005184 i = nasm_malloc(sizeof(IncPath));
5185 i->path = path ? nasm_strdup(path) : NULL;
5186 i->next = NULL;
Frank Kotlerd0ed6fd2003-08-27 11:33:56 +00005187
H. Peter Anvin89cee572009-07-15 09:16:54 -04005188 if (ipath) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00005189 IncPath *j = ipath;
H. Peter Anvin89cee572009-07-15 09:16:54 -04005190 while (j->next)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005191 j = j->next;
5192 j->next = i;
5193 } else {
5194 ipath = i;
Frank Kotlerd0ed6fd2003-08-27 11:33:56 +00005195 }
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005196}
Frank Kotlerd0ed6fd2003-08-27 11:33:56 +00005197
Cyrill Gorcunov0b78bff2012-05-07 01:57:55 +04005198static void pp_pre_include(char *fname)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005199{
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005200 Token *inc, *space, *name;
5201 Line *l;
5202
H. Peter Anvin734b1882002-04-30 21:01:08 +00005203 name = new_Token(NULL, TOK_INTERNAL_STRING, fname, 0);
5204 space = new_Token(name, TOK_WHITESPACE, NULL, 0);
5205 inc = new_Token(space, TOK_PREPROC_ID, "%include", 0);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005206
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005207 l = nasm_malloc(sizeof(Line));
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005208 l->next = predef;
5209 l->first = inc;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005210 l->finishes = NULL;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005211 predef = l;
5212}
5213
Cyrill Gorcunov0b78bff2012-05-07 01:57:55 +04005214static void pp_pre_define(char *definition)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005215{
5216 Token *def, *space;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005217 Line *l;
Keith Kaniosa6dfa782007-04-13 16:47:53 +00005218 char *equals;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005219
H. Peter Anvin130736c2016-02-17 20:27:41 -08005220 real_verror = nasm_set_verror(pp_verror);
H. Peter Anvin215186f2016-02-17 20:27:41 -08005221
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005222 equals = strchr(definition, '=');
H. Peter Anvin734b1882002-04-30 21:01:08 +00005223 space = new_Token(NULL, TOK_WHITESPACE, NULL, 0);
5224 def = new_Token(space, TOK_PREPROC_ID, "%define", 0);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005225 if (equals)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005226 *equals = ' ';
Keith Kaniosb7a89542007-04-12 02:40:54 +00005227 space->next = tokenize(definition);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005228 if (equals)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005229 *equals = '=';
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005230
Cyrill Gorcunov6d42e9b2015-02-08 11:07:17 +03005231 if (space->next->type != TOK_PREPROC_ID &&
5232 space->next->type != TOK_ID)
H. Peter Anvin130736c2016-02-17 20:27:41 -08005233 nasm_error(ERR_WARNING, "pre-defining non ID `%s\'\n", definition);
Cyrill Gorcunov6d42e9b2015-02-08 11:07:17 +03005234
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005235 l = nasm_malloc(sizeof(Line));
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005236 l->next = predef;
5237 l->first = def;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005238 l->finishes = NULL;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005239 predef = l;
H. Peter Anvin130736c2016-02-17 20:27:41 -08005240
5241 nasm_set_verror(real_verror);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005242}
5243
Cyrill Gorcunov0b78bff2012-05-07 01:57:55 +04005244static void pp_pre_undefine(char *definition)
H. Peter Anvin620515a2002-04-30 20:57:38 +00005245{
5246 Token *def, *space;
5247 Line *l;
5248
H. Peter Anvin734b1882002-04-30 21:01:08 +00005249 space = new_Token(NULL, TOK_WHITESPACE, NULL, 0);
5250 def = new_Token(space, TOK_PREPROC_ID, "%undef", 0);
Keith Kaniosb7a89542007-04-12 02:40:54 +00005251 space->next = tokenize(definition);
H. Peter Anvin620515a2002-04-30 20:57:38 +00005252
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005253 l = nasm_malloc(sizeof(Line));
H. Peter Anvin620515a2002-04-30 20:57:38 +00005254 l->next = predef;
5255 l->first = def;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005256 l->finishes = NULL;
H. Peter Anvin620515a2002-04-30 20:57:38 +00005257 predef = l;
5258}
5259
H. Peter Anvinf7606612016-07-13 14:23:48 -07005260static void pp_add_stdmac(macros_t *macros)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005261{
H. Peter Anvinf7606612016-07-13 14:23:48 -07005262 macros_t **mp;
5263
5264 /* Find the end of the list and avoid duplicates */
5265 for (mp = stdmacros; *mp; mp++) {
5266 if (*mp == macros)
5267 return; /* Nothing to do */
5268 }
5269
5270 nasm_assert(mp < &stdmacros[ARRAY_SIZE(stdmacros)-1]);
5271
5272 *mp = macros;
H. Peter Anvin76690a12002-04-30 20:52:49 +00005273}
5274
Keith Kaniosa5fc6462007-10-13 07:09:22 -07005275static void make_tok_num(Token * tok, int64_t val)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005276{
Cyrill Gorcunovce652742013-05-06 23:43:43 +04005277 char numbuf[32];
Keith Kaniosa5fc6462007-10-13 07:09:22 -07005278 snprintf(numbuf, sizeof(numbuf), "%"PRId64"", val);
H. Peter Anvineba20a72002-04-30 20:53:55 +00005279 tok->text = nasm_strdup(numbuf);
5280 tok->type = TOK_NUMBER;
5281}
5282
H. Peter Anvin37368952016-05-09 14:10:32 -07005283static void pp_list_one_macro(MMacro *m, int severity)
5284{
5285 if (!m)
5286 return;
5287
5288 /* We need to print the next_active list in reverse order */
5289 pp_list_one_macro(m->next_active, severity);
5290
5291 if (m->name && !m->nolist) {
H. Peter Anvin274cda82016-05-10 02:56:29 -07005292 src_set(m->xline + m->lineno, m->fname);
H. Peter Anvin37368952016-05-09 14:10:32 -07005293 nasm_error(severity, "... from macro `%s' defined here", m->name);
5294 }
5295}
5296
H. Peter Anvin4def1a82016-05-09 13:59:44 -07005297static void pp_error_list_macros(int severity)
5298{
H. Peter Anvin4def1a82016-05-09 13:59:44 -07005299 int32_t saved_line;
5300 const char *saved_fname = NULL;
5301
5302 severity |= ERR_PP_LISTMACRO | ERR_NO_SEVERITY;
H. Peter Anvin274cda82016-05-10 02:56:29 -07005303 src_get(&saved_line, &saved_fname);
H. Peter Anvin4def1a82016-05-09 13:59:44 -07005304
Cyrill Gorcunov771d04e2016-05-10 23:27:03 +03005305 if (istk)
5306 pp_list_one_macro(istk->mstk, severity);
H. Peter Anvin4def1a82016-05-09 13:59:44 -07005307
H. Peter Anvin274cda82016-05-10 02:56:29 -07005308 src_set(saved_line, saved_fname);
H. Peter Anvin4def1a82016-05-09 13:59:44 -07005309}
5310
H. Peter Anvine7469712016-02-18 02:20:59 -08005311const struct preproc_ops nasmpp = {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005312 pp_reset,
5313 pp_getline,
Cyrill Gorcunov0b78bff2012-05-07 01:57:55 +04005314 pp_cleanup,
H. Peter Anvinf7606612016-07-13 14:23:48 -07005315 pp_add_stdmac,
Cyrill Gorcunov0b78bff2012-05-07 01:57:55 +04005316 pp_pre_define,
5317 pp_pre_undefine,
5318 pp_pre_include,
H. Peter Anvin4def1a82016-05-09 13:59:44 -07005319 pp_include_path,
5320 pp_error_list_macros,
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005321};