blob: f3bf609c3ed807ae67308069d8d702721add3614 [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);
Cyrill Gorcunovbbb7a1a2016-06-19 12:15:24 +03001267 /*
1268 * FIXME We better should investigate if accessing
1269 * ->text[1] without ->text[0] is safe enough.
1270 */
1271 t->text = nasm_zalloc(2);
1272 } else
1273 t->text = nasm_strdup(p);
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001274 }
1275 nasm_free(q);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001276 }
H. Peter Anvin077fb932010-07-20 14:56:30 -07001277
H. Peter Anvine2c80182005-01-15 22:15:51 +00001278 /* Expand local macros here and not during preprocessing */
1279 if (expand_locals &&
1280 t->type == TOK_PREPROC_ID && t->text &&
1281 t->text[0] == '%' && t->text[1] == '$') {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001282 const char *q;
1283 char *p;
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04001284 Context *ctx = get_ctx(t->text, &q);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001285 if (ctx) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001286 char buffer[40];
Keith Kanios93f2e9a2007-04-14 00:10:59 +00001287 snprintf(buffer, sizeof(buffer), "..@%"PRIu32".", ctx->number);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001288 p = nasm_strcat(buffer, q);
1289 nasm_free(t->text);
1290 t->text = p;
1291 }
1292 }
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001293 if (t->type == TOK_WHITESPACE)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001294 len++;
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001295 else if (t->text)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001296 len += strlen(t->text);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001297 }
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001298
H. Peter Anvin734b1882002-04-30 21:01:08 +00001299 p = line = nasm_malloc(len + 1);
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001300
1301 list_for_each(t, tlist) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001302 if (t->type == TOK_WHITESPACE) {
H. Peter Anvinb4daadc2008-01-21 16:31:57 -08001303 *p++ = ' ';
H. Peter Anvine2c80182005-01-15 22:15:51 +00001304 } else if (t->text) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001305 q = t->text;
1306 while (*q)
1307 *p++ = *q++;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001308 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001309 }
1310 *p = '\0';
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001311
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001312 return line;
1313}
1314
1315/*
H. Peter Anvin76690a12002-04-30 20:52:49 +00001316 * A scanner, suitable for use by the expression evaluator, which
1317 * operates on a line of Tokens. Expects a pointer to a pointer to
1318 * the first token in the line to be passed in as its private_data
1319 * field.
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001320 *
1321 * FIX: This really needs to be unified with stdscan.
H. Peter Anvin76690a12002-04-30 20:52:49 +00001322 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001323static int ppscan(void *private_data, struct tokenval *tokval)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001324{
H. Peter Anvin76690a12002-04-30 20:52:49 +00001325 Token **tlineptr = private_data;
1326 Token *tline;
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001327 char ourcopy[MAX_KEYWORD+1], *p, *r, *s;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001328
H. Peter Anvine2c80182005-01-15 22:15:51 +00001329 do {
1330 tline = *tlineptr;
1331 *tlineptr = tline ? tline->next : NULL;
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04001332 } while (tline && (tline->type == TOK_WHITESPACE ||
1333 tline->type == TOK_COMMENT));
H. Peter Anvin76690a12002-04-30 20:52:49 +00001334
1335 if (!tline)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001336 return tokval->t_type = TOKEN_EOS;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001337
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001338 tokval->t_charptr = tline->text;
1339
H. Peter Anvin76690a12002-04-30 20:52:49 +00001340 if (tline->text[0] == '$' && !tline->text[1])
H. Peter Anvine2c80182005-01-15 22:15:51 +00001341 return tokval->t_type = TOKEN_HERE;
H. Peter Anvin7cf897e2002-05-30 21:30:33 +00001342 if (tline->text[0] == '$' && tline->text[1] == '$' && !tline->text[2])
H. Peter Anvine2c80182005-01-15 22:15:51 +00001343 return tokval->t_type = TOKEN_BASE;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001344
H. Peter Anvine2c80182005-01-15 22:15:51 +00001345 if (tline->type == TOK_ID) {
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001346 p = tokval->t_charptr = tline->text;
1347 if (p[0] == '$') {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001348 tokval->t_charptr++;
1349 return tokval->t_type = TOKEN_ID;
1350 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00001351
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001352 for (r = p, s = ourcopy; *r; r++) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001353 if (r >= p+MAX_KEYWORD)
1354 return tokval->t_type = TOKEN_ID; /* Not a keyword */
H. Peter Anvinac8f8fc2008-06-11 15:49:41 -07001355 *s++ = nasm_tolower(*r);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001356 }
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001357 *s = '\0';
1358 /* right, so we have an identifier sitting in temp storage. now,
1359 * is it actually a register or instruction name, or what? */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001360 return nasm_token_hash(ourcopy, tokval);
H. Peter Anvin76690a12002-04-30 20:52:49 +00001361 }
1362
H. Peter Anvine2c80182005-01-15 22:15:51 +00001363 if (tline->type == TOK_NUMBER) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001364 bool rn_error;
1365 tokval->t_integer = readnum(tline->text, &rn_error);
1366 tokval->t_charptr = tline->text;
1367 if (rn_error)
1368 return tokval->t_type = TOKEN_ERRNUM;
1369 else
1370 return tokval->t_type = TOKEN_NUM;
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001371 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00001372
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001373 if (tline->type == TOK_FLOAT) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001374 return tokval->t_type = TOKEN_FLOAT;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001375 }
1376
H. Peter Anvine2c80182005-01-15 22:15:51 +00001377 if (tline->type == TOK_STRING) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001378 char bq, *ep;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001379
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001380 bq = tline->text[0];
H. Peter Anvin11627042008-06-09 20:45:19 -07001381 tokval->t_charptr = tline->text;
1382 tokval->t_inttwo = nasm_unquote(tline->text, &ep);
H. Peter Anvind2456592008-06-19 15:04:18 -07001383
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001384 if (ep[0] != bq || ep[1] != '\0')
1385 return tokval->t_type = TOKEN_ERRSTR;
1386 else
1387 return tokval->t_type = TOKEN_STR;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001388 }
1389
H. Peter Anvine2c80182005-01-15 22:15:51 +00001390 if (tline->type == TOK_OTHER) {
1391 if (!strcmp(tline->text, "<<"))
1392 return tokval->t_type = TOKEN_SHL;
1393 if (!strcmp(tline->text, ">>"))
1394 return tokval->t_type = TOKEN_SHR;
1395 if (!strcmp(tline->text, "//"))
1396 return tokval->t_type = TOKEN_SDIV;
1397 if (!strcmp(tline->text, "%%"))
1398 return tokval->t_type = TOKEN_SMOD;
1399 if (!strcmp(tline->text, "=="))
1400 return tokval->t_type = TOKEN_EQ;
1401 if (!strcmp(tline->text, "<>"))
1402 return tokval->t_type = TOKEN_NE;
1403 if (!strcmp(tline->text, "!="))
1404 return tokval->t_type = TOKEN_NE;
1405 if (!strcmp(tline->text, "<="))
1406 return tokval->t_type = TOKEN_LE;
1407 if (!strcmp(tline->text, ">="))
1408 return tokval->t_type = TOKEN_GE;
1409 if (!strcmp(tline->text, "&&"))
1410 return tokval->t_type = TOKEN_DBL_AND;
1411 if (!strcmp(tline->text, "^^"))
1412 return tokval->t_type = TOKEN_DBL_XOR;
1413 if (!strcmp(tline->text, "||"))
1414 return tokval->t_type = TOKEN_DBL_OR;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001415 }
1416
1417 /*
1418 * We have no other options: just return the first character of
1419 * the token text.
1420 */
1421 return tokval->t_type = tline->text[0];
1422}
1423
1424/*
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001425 * Compare a string to the name of an existing macro; this is a
1426 * simple wrapper which calls either strcmp or nasm_stricmp
1427 * depending on the value of the `casesense' parameter.
1428 */
H. Peter Anvin4db5a162007-10-11 13:42:09 -07001429static int mstrcmp(const char *p, const char *q, bool casesense)
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001430{
H. Peter Anvin734b1882002-04-30 21:01:08 +00001431 return casesense ? strcmp(p, q) : nasm_stricmp(p, q);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001432}
1433
1434/*
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07001435 * Compare a string to the name of an existing macro; this is a
1436 * simple wrapper which calls either strcmp or nasm_stricmp
1437 * depending on the value of the `casesense' parameter.
1438 */
1439static int mmemcmp(const char *p, const char *q, size_t l, bool casesense)
1440{
1441 return casesense ? memcmp(p, q, l) : nasm_memicmp(p, q, l);
1442}
1443
1444/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001445 * Return the Context structure associated with a %$ token. Return
1446 * NULL, having _already_ reported an error condition, if the
1447 * context stack isn't deep enough for the supplied number of $
1448 * signs.
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001449 *
1450 * If "namep" is non-NULL, set it to the pointer to the macro name
1451 * tail, i.e. the part beyond %$...
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001452 */
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04001453static Context *get_ctx(const char *name, const char **namep)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001454{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001455 Context *ctx;
1456 int i;
1457
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001458 if (namep)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001459 *namep = name;
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001460
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001461 if (!name || name[0] != '%' || name[1] != '$')
H. Peter Anvine2c80182005-01-15 22:15:51 +00001462 return NULL;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001463
H. Peter Anvine2c80182005-01-15 22:15:51 +00001464 if (!cstk) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08001465 nasm_error(ERR_NONFATAL, "`%s': context stack is empty", name);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001466 return NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001467 }
1468
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001469 name += 2;
1470 ctx = cstk;
1471 i = 0;
1472 while (ctx && *name == '$') {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001473 name++;
1474 i++;
1475 ctx = ctx->next;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001476 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001477 if (!ctx) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08001478 nasm_error(ERR_NONFATAL, "`%s': context stack is only"
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001479 " %d level%s deep", name, i, (i == 1 ? "" : "s"));
H. Peter Anvine2c80182005-01-15 22:15:51 +00001480 return NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001481 }
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001482
1483 if (namep)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001484 *namep = name;
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001485
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04001486 return ctx;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001487}
1488
1489/*
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001490 * Check to see if a file is already in a string list
1491 */
1492static bool in_list(const StrList *list, const char *str)
1493{
1494 while (list) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001495 if (!strcmp(list->str, str))
1496 return true;
1497 list = list->next;
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001498 }
1499 return false;
1500}
1501
1502/*
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001503 * Open an include file. This routine must always return a valid
1504 * file pointer if it returns - it's responsible for throwing an
1505 * ERR_FATAL and bombing out completely if not. It should also try
1506 * the include path one by one until it finds the file or reaches
1507 * the end of the path.
H. Peter Anvind81a2352016-09-21 14:03:18 -07001508 *
1509 * Note: for INC_PROBE the function returns NULL at all times;
1510 * instead look for the
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001511 */
H. Peter Anvind81a2352016-09-21 14:03:18 -07001512enum incopen_mode {
1513 INC_NEEDED, /* File must exist */
1514 INC_OPTIONAL, /* Missing is OK */
1515 INC_PROBE /* Only an existence probe */
1516};
1517
H. Peter Anvin2b1c3b92008-06-06 10:38:46 -07001518static FILE *inc_fopen(const char *file, StrList **dhead, StrList ***dtail,
H. Peter Anvind81a2352016-09-21 14:03:18 -07001519 char **found_path, enum incopen_mode omode,
1520 enum file_flags fmode)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001521{
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001522 FILE *fp;
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001523 char *prefix = "";
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001524 IncPath *ip = ipath;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001525 int len = strlen(file);
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001526 size_t prefix_len = 0;
1527 StrList *sl;
Jim Kukunas65a8afc2016-06-13 16:00:42 -04001528 size_t path_len;
H. Peter Anvind81a2352016-09-21 14:03:18 -07001529 bool found;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001530
H. Peter Anvine2c80182005-01-15 22:15:51 +00001531 while (1) {
Jim Kukunas65a8afc2016-06-13 16:00:42 -04001532 path_len = prefix_len + len + 1;
1533
1534 sl = nasm_malloc(path_len + sizeof sl->next);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001535 memcpy(sl->str, prefix, prefix_len);
1536 memcpy(sl->str+prefix_len, file, len+1);
Jim Kukunas65a8afc2016-06-13 16:00:42 -04001537
H. Peter Anvind81a2352016-09-21 14:03:18 -07001538 if (omode == INC_PROBE) {
1539 fp = NULL;
1540 found = nasm_file_exists(sl->str);
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001541 } else {
H. Peter Anvind81a2352016-09-21 14:03:18 -07001542 fp = nasm_open_read(sl->str, fmode);
1543 found = (fp != NULL);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001544 }
H. Peter Anvind81a2352016-09-21 14:03:18 -07001545 if (found) {
1546 if (found_path)
1547 *found_path = nasm_strdup(sl->str);
1548
1549 if (dhead && !in_list(*dhead, sl->str)) {
1550 sl->next = NULL;
1551 **dtail = sl;
1552 *dtail = &sl->next;
1553 } else {
1554 nasm_free(sl);
1555 }
1556 }
1557 if (found)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001558 return fp;
Jim Kukunas65a8afc2016-06-13 16:00:42 -04001559
H. Peter Anvind81a2352016-09-21 14:03:18 -07001560 nasm_free(sl);
Jim Kukunas65a8afc2016-06-13 16:00:42 -04001561
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001562 if (!ip) {
H. Peter Anvind81a2352016-09-21 14:03:18 -07001563 if (omode == INC_NEEDED)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001564 break;
1565 prefix = NULL;
1566 } else {
1567 prefix = ip->path;
1568 ip = ip->next;
1569 }
1570 if (prefix) {
1571 prefix_len = strlen(prefix);
1572 } else {
1573 /* -MG given and file not found */
1574 if (dhead && !in_list(*dhead, file)) {
1575 sl = nasm_malloc(len+1+sizeof sl->next);
1576 sl->next = NULL;
1577 strcpy(sl->str, file);
1578 **dtail = sl;
1579 *dtail = &sl->next;
1580 }
1581 return NULL;
1582 }
H. Peter Anvineba20a72002-04-30 20:53:55 +00001583 }
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001584
H. Peter Anvin130736c2016-02-17 20:27:41 -08001585 nasm_error(ERR_FATAL, "unable to open include file `%s'", file);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001586 return NULL;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001587}
1588
1589/*
Fabian Giesen0bbc38d2016-04-28 13:48:14 -07001590 * Opens an include or input file. Public version, for use by modules
1591 * that get a file:lineno pair and need to look at the file again
1592 * (e.g. the CodeView debug backend). Returns NULL on failure.
1593 */
H. Peter Anvin3e83cec2016-05-25 04:28:46 -07001594FILE *pp_input_fopen(const char *filename, enum file_flags mode)
Fabian Giesen0bbc38d2016-04-28 13:48:14 -07001595{
1596 FILE *fp;
1597 StrList *xsl = NULL;
1598 StrList **xst = &xsl;
1599
H. Peter Anvind81a2352016-09-21 14:03:18 -07001600 fp = inc_fopen(filename, &xsl, &xst, NULL, INC_OPTIONAL, mode);
Fabian Giesen0bbc38d2016-04-28 13:48:14 -07001601 if (xsl)
1602 nasm_free(xsl);
1603 return fp;
1604}
1605
1606/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001607 * Determine if we should warn on defining a single-line macro of
H. Peter Anvinef7468f2002-04-30 20:57:59 +00001608 * name `name', with `nparam' parameters. If nparam is 0 or -1, will
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001609 * return true if _any_ single-line macro of that name is defined.
1610 * Otherwise, will return true if a single-line macro with either
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001611 * `nparam' or no parameters is defined.
1612 *
1613 * If a macro with precisely the right number of parameters is
H. Peter Anvinef7468f2002-04-30 20:57:59 +00001614 * defined, or nparam is -1, the address of the definition structure
1615 * will be returned in `defn'; otherwise NULL will be returned. If `defn'
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001616 * is NULL, no action will be taken regarding its contents, and no
1617 * error will occur.
1618 *
1619 * Note that this is also called with nparam zero to resolve
1620 * `ifdef'.
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001621 *
1622 * If you already know which context macro belongs to, you can pass
1623 * the context pointer as first parameter; if you won't but name begins
1624 * with %$ the context will be automatically computed. If all_contexts
1625 * is true, macro will be searched in outer contexts as well.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001626 */
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07001627static bool
H. Peter Anvinb2a5fda2008-06-19 21:42:42 -07001628smacro_defined(Context * ctx, const char *name, int nparam, SMacro ** defn,
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07001629 bool nocase)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001630{
H. Peter Anvin166c2472008-05-28 12:28:58 -07001631 struct hash_table *smtbl;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001632 SMacro *m;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001633
H. Peter Anvin97a23472007-09-16 17:57:25 -07001634 if (ctx) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001635 smtbl = &ctx->localmac;
H. Peter Anvin97a23472007-09-16 17:57:25 -07001636 } else if (name[0] == '%' && name[1] == '$') {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001637 if (cstk)
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04001638 ctx = get_ctx(name, &name);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001639 if (!ctx)
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001640 return false; /* got to return _something_ */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001641 smtbl = &ctx->localmac;
H. Peter Anvin97a23472007-09-16 17:57:25 -07001642 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001643 smtbl = &smacros;
H. Peter Anvin97a23472007-09-16 17:57:25 -07001644 }
H. Peter Anvin166c2472008-05-28 12:28:58 -07001645 m = (SMacro *) hash_findix(smtbl, name);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001646
H. Peter Anvine2c80182005-01-15 22:15:51 +00001647 while (m) {
1648 if (!mstrcmp(m->name, name, m->casesense && nocase) &&
Charles Crayne192d5b52007-10-18 19:02:42 -07001649 (nparam <= 0 || m->nparam == 0 || nparam == (int) m->nparam)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001650 if (defn) {
Charles Crayne192d5b52007-10-18 19:02:42 -07001651 if (nparam == (int) m->nparam || nparam == -1)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001652 *defn = m;
1653 else
1654 *defn = NULL;
1655 }
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001656 return true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001657 }
1658 m = m->next;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001659 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001660
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001661 return false;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001662}
1663
1664/*
1665 * Count and mark off the parameters in a multi-line macro call.
1666 * This is called both from within the multi-line macro expansion
1667 * code, and also to mark off the default parameters when provided
1668 * in a %macro definition line.
1669 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001670static void count_mmac_params(Token * t, int *nparam, Token *** params)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001671{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001672 int paramsize, brace;
1673
1674 *nparam = paramsize = 0;
1675 *params = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001676 while (t) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001677 /* +1: we need space for the final NULL */
H. Peter Anvin91fb6f12008-09-01 10:56:33 -07001678 if (*nparam+1 >= paramsize) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001679 paramsize += PARAM_DELTA;
1680 *params = nasm_realloc(*params, sizeof(**params) * paramsize);
1681 }
1682 skip_white_(t);
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08001683 brace = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001684 if (tok_is_(t, "{"))
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08001685 brace++;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001686 (*params)[(*nparam)++] = t;
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08001687 if (brace) {
1688 while (brace && (t = t->next) != NULL) {
1689 if (tok_is_(t, "{"))
1690 brace++;
1691 else if (tok_is_(t, "}"))
1692 brace--;
1693 }
1694
1695 if (t) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001696 /*
1697 * Now we've found the closing brace, look further
1698 * for the comma.
1699 */
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08001700 t = t->next;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001701 skip_white_(t);
1702 if (tok_isnt_(t, ",")) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08001703 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00001704 "braces do not enclose all of macro parameter");
1705 while (tok_isnt_(t, ","))
1706 t = t->next;
1707 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001708 }
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08001709 } else {
1710 while (tok_isnt_(t, ","))
1711 t = t->next;
1712 }
1713 if (t) { /* got a comma/brace */
1714 t = t->next; /* eat the comma */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001715 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001716 }
1717}
1718
1719/*
H. Peter Anvin76690a12002-04-30 20:52:49 +00001720 * Determine whether one of the various `if' conditions is true or
1721 * not.
1722 *
1723 * We must free the tline we get passed.
1724 */
H. Peter Anvin70055962007-10-11 00:05:31 -07001725static bool if_condition(Token * tline, enum preproc_token ct)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001726{
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001727 enum pp_conditional i = PP_COND(ct);
H. Peter Anvin70055962007-10-11 00:05:31 -07001728 bool j;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001729 Token *t, *tt, **tptr, *origline;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001730 struct tokenval tokval;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001731 expr *evalresult;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001732 enum pp_token_type needtype;
H. Peter Anvin077fb932010-07-20 14:56:30 -07001733 char *p;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001734
1735 origline = tline;
1736
H. Peter Anvine2c80182005-01-15 22:15:51 +00001737 switch (i) {
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001738 case PPC_IFCTX:
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001739 j = false; /* have we matched yet? */
Victor van den Elzen0e857f12008-07-23 13:21:29 +02001740 while (true) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001741 skip_white_(tline);
Victor van den Elzen0e857f12008-07-23 13:21:29 +02001742 if (!tline)
1743 break;
1744 if (tline->type != TOK_ID) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08001745 nasm_error(ERR_NONFATAL,
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001746 "`%s' expects context identifiers", pp_directives[ct]);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001747 free_tlist(origline);
1748 return -1;
1749 }
Victor van den Elzen0e857f12008-07-23 13:21:29 +02001750 if (cstk && cstk->name && !nasm_stricmp(tline->text, cstk->name))
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001751 j = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001752 tline = tline->next;
1753 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001754 break;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001755
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001756 case PPC_IFDEF:
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001757 j = false; /* have we matched yet? */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001758 while (tline) {
1759 skip_white_(tline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001760 if (!tline || (tline->type != TOK_ID &&
1761 (tline->type != TOK_PREPROC_ID ||
1762 tline->text[1] != '$'))) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08001763 nasm_error(ERR_NONFATAL,
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001764 "`%s' expects macro identifiers", pp_directives[ct]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001765 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001766 }
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07001767 if (smacro_defined(NULL, tline->text, 0, NULL, true))
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001768 j = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001769 tline = tline->next;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001770 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001771 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001772
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001773 case PPC_IFENV:
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001774 tline = expand_smacro(tline);
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001775 j = false; /* have we matched yet? */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001776 while (tline) {
1777 skip_white_(tline);
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001778 if (!tline || (tline->type != TOK_ID &&
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001779 tline->type != TOK_STRING &&
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001780 (tline->type != TOK_PREPROC_ID ||
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001781 tline->text[1] != '!'))) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08001782 nasm_error(ERR_NONFATAL,
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001783 "`%s' expects environment variable names",
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001784 pp_directives[ct]);
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001785 goto fail;
1786 }
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001787 p = tline->text;
1788 if (tline->type == TOK_PREPROC_ID)
1789 p += 2; /* Skip leading %! */
1790 if (*p == '\'' || *p == '\"' || *p == '`')
1791 nasm_unquote_cstr(p, ct);
1792 if (getenv(p))
1793 j = true;
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001794 tline = tline->next;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001795 }
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001796 break;
1797
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001798 case PPC_IFIDN:
1799 case PPC_IFIDNI:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001800 tline = expand_smacro(tline);
1801 t = tt = tline;
1802 while (tok_isnt_(tt, ","))
1803 tt = tt->next;
1804 if (!tt) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08001805 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00001806 "`%s' expects two comma-separated arguments",
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001807 pp_directives[ct]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001808 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001809 }
1810 tt = tt->next;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001811 j = true; /* assume equality unless proved not */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001812 while ((t->type != TOK_OTHER || strcmp(t->text, ",")) && tt) {
1813 if (tt->type == TOK_OTHER && !strcmp(tt->text, ",")) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08001814 nasm_error(ERR_NONFATAL, "`%s': more than one comma on line",
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001815 pp_directives[ct]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001816 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001817 }
1818 if (t->type == TOK_WHITESPACE) {
1819 t = t->next;
1820 continue;
1821 }
1822 if (tt->type == TOK_WHITESPACE) {
1823 tt = tt->next;
1824 continue;
1825 }
1826 if (tt->type != t->type) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001827 j = false; /* found mismatching tokens */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001828 break;
1829 }
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07001830 /* When comparing strings, need to unquote them first */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001831 if (t->type == TOK_STRING) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001832 size_t l1 = nasm_unquote(t->text, NULL);
1833 size_t l2 = nasm_unquote(tt->text, NULL);
H. Peter Anvind2456592008-06-19 15:04:18 -07001834
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001835 if (l1 != l2) {
1836 j = false;
1837 break;
1838 }
1839 if (mmemcmp(t->text, tt->text, l1, i == PPC_IFIDN)) {
1840 j = false;
1841 break;
1842 }
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07001843 } else if (mstrcmp(tt->text, t->text, i == PPC_IFIDN) != 0) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001844 j = false; /* found mismatching tokens */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001845 break;
1846 }
Nickolay Yurchenkof3b3ce22003-09-21 20:38:43 +00001847
H. Peter Anvine2c80182005-01-15 22:15:51 +00001848 t = t->next;
1849 tt = tt->next;
1850 }
1851 if ((t->type != TOK_OTHER || strcmp(t->text, ",")) || tt)
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001852 j = false; /* trailing gunk on one end or other */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001853 break;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001854
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001855 case PPC_IFMACRO:
H. Peter Anvin89cee572009-07-15 09:16:54 -04001856 {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001857 bool found = false;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001858 MMacro searching, *mmac;
H. Peter Anvin65747262002-05-07 00:10:05 +00001859
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001860 skip_white_(tline);
1861 tline = expand_id(tline);
1862 if (!tok_type_(tline, TOK_ID)) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08001863 nasm_error(ERR_NONFATAL,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001864 "`%s' expects a macro name", pp_directives[ct]);
1865 goto fail;
1866 }
1867 searching.name = nasm_strdup(tline->text);
1868 searching.casesense = true;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001869 searching.plus = false;
1870 searching.nolist = false;
1871 searching.in_progress = 0;
1872 searching.max_depth = 0;
1873 searching.rep_nest = NULL;
1874 searching.nparam_min = 0;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001875 searching.nparam_max = INT_MAX;
1876 tline = expand_smacro(tline->next);
1877 skip_white_(tline);
1878 if (!tline) {
1879 } else if (!tok_type_(tline, TOK_NUMBER)) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08001880 nasm_error(ERR_NONFATAL,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001881 "`%s' expects a parameter count or nothing",
1882 pp_directives[ct]);
1883 } else {
1884 searching.nparam_min = searching.nparam_max =
1885 readnum(tline->text, &j);
1886 if (j)
H. Peter Anvin130736c2016-02-17 20:27:41 -08001887 nasm_error(ERR_NONFATAL,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001888 "unable to parse parameter count `%s'",
1889 tline->text);
1890 }
1891 if (tline && tok_is_(tline->next, "-")) {
1892 tline = tline->next->next;
1893 if (tok_is_(tline, "*"))
1894 searching.nparam_max = INT_MAX;
1895 else if (!tok_type_(tline, TOK_NUMBER))
H. Peter Anvin130736c2016-02-17 20:27:41 -08001896 nasm_error(ERR_NONFATAL,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001897 "`%s' expects a parameter count after `-'",
1898 pp_directives[ct]);
1899 else {
1900 searching.nparam_max = readnum(tline->text, &j);
1901 if (j)
H. Peter Anvin130736c2016-02-17 20:27:41 -08001902 nasm_error(ERR_NONFATAL,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001903 "unable to parse parameter count `%s'",
1904 tline->text);
1905 if (searching.nparam_min > searching.nparam_max)
H. Peter Anvin130736c2016-02-17 20:27:41 -08001906 nasm_error(ERR_NONFATAL,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001907 "minimum parameter count exceeds maximum");
1908 }
1909 }
1910 if (tline && tok_is_(tline->next, "+")) {
1911 tline = tline->next;
1912 searching.plus = true;
1913 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001914 mmac = (MMacro *) hash_findix(&mmacros, searching.name);
1915 while (mmac) {
1916 if (!strcmp(mmac->name, searching.name) &&
1917 (mmac->nparam_min <= searching.nparam_max
1918 || searching.plus)
1919 && (searching.nparam_min <= mmac->nparam_max
1920 || mmac->plus)) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001921 found = true;
1922 break;
1923 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001924 mmac = mmac->next;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001925 }
1926 if (tline && tline->next)
H. Peter Anvin130736c2016-02-17 20:27:41 -08001927 nasm_error(ERR_WARNING|ERR_PASS1,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001928 "trailing garbage after %%ifmacro ignored");
1929 nasm_free(searching.name);
1930 j = found;
1931 break;
H. Peter Anvin89cee572009-07-15 09:16:54 -04001932 }
H. Peter Anvin65747262002-05-07 00:10:05 +00001933
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001934 case PPC_IFID:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001935 needtype = TOK_ID;
1936 goto iftype;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001937 case PPC_IFNUM:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001938 needtype = TOK_NUMBER;
1939 goto iftype;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001940 case PPC_IFSTR:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001941 needtype = TOK_STRING;
1942 goto iftype;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001943
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001944iftype:
1945 t = tline = expand_smacro(tline);
H. Peter Anvind85d2502008-05-04 17:53:31 -07001946
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001947 while (tok_type_(t, TOK_WHITESPACE) ||
1948 (needtype == TOK_NUMBER &&
1949 tok_type_(t, TOK_OTHER) &&
1950 (t->text[0] == '-' || t->text[0] == '+') &&
1951 !t->text[1]))
1952 t = t->next;
H. Peter Anvind85d2502008-05-04 17:53:31 -07001953
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001954 j = tok_type_(t, needtype);
1955 break;
H. Peter Anvincbf768d2008-02-16 16:41:25 -08001956
1957 case PPC_IFTOKEN:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001958 t = tline = expand_smacro(tline);
1959 while (tok_type_(t, TOK_WHITESPACE))
1960 t = t->next;
H. Peter Anvincbf768d2008-02-16 16:41:25 -08001961
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001962 j = false;
1963 if (t) {
1964 t = t->next; /* Skip the actual token */
1965 while (tok_type_(t, TOK_WHITESPACE))
1966 t = t->next;
1967 j = !t; /* Should be nothing left */
1968 }
1969 break;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001970
H. Peter Anvin134b9462008-02-16 17:01:40 -08001971 case PPC_IFEMPTY:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001972 t = tline = expand_smacro(tline);
1973 while (tok_type_(t, TOK_WHITESPACE))
1974 t = t->next;
H. Peter Anvin134b9462008-02-16 17:01:40 -08001975
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001976 j = !t; /* Should be empty */
1977 break;
H. Peter Anvin134b9462008-02-16 17:01:40 -08001978
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001979 case PPC_IF:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001980 t = tline = expand_smacro(tline);
1981 tptr = &t;
1982 tokval.t_type = TOKEN_INVALID;
1983 evalresult = evaluate(ppscan, tptr, &tokval,
H. Peter Anvin130736c2016-02-17 20:27:41 -08001984 NULL, pass | CRITICAL, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001985 if (!evalresult)
1986 return -1;
1987 if (tokval.t_type)
H. Peter Anvin130736c2016-02-17 20:27:41 -08001988 nasm_error(ERR_WARNING|ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +00001989 "trailing garbage after expression ignored");
1990 if (!is_simple(evalresult)) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08001991 nasm_error(ERR_NONFATAL,
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001992 "non-constant value given to `%s'", pp_directives[ct]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001993 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001994 }
Chuck Crayne60ae75d2007-05-02 01:59:16 +00001995 j = reloc_value(evalresult) != 0;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001996 break;
H. Peter Anvin95e28822007-09-12 04:20:08 +00001997
H. Peter Anvine2c80182005-01-15 22:15:51 +00001998 default:
H. Peter Anvin130736c2016-02-17 20:27:41 -08001999 nasm_error(ERR_FATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00002000 "preprocessor directive `%s' not yet implemented",
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002001 pp_directives[ct]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002002 goto fail;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002003 }
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002004
2005 free_tlist(origline);
2006 return j ^ PP_NEGATIVE(ct);
H. Peter Anvin70653092007-10-19 14:42:29 -07002007
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002008fail:
2009 free_tlist(origline);
2010 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002011}
2012
2013/*
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002014 * Common code for defining an smacro
2015 */
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08002016static bool define_smacro(Context *ctx, const char *mname, bool casesense,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002017 int nparam, Token *expansion)
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002018{
2019 SMacro *smac, **smhead;
H. Peter Anvin166c2472008-05-28 12:28:58 -07002020 struct hash_table *smtbl;
H. Peter Anvin70653092007-10-19 14:42:29 -07002021
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002022 if (smacro_defined(ctx, mname, nparam, &smac, casesense)) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002023 if (!smac) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002024 nasm_error(ERR_WARNING|ERR_PASS1,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002025 "single-line macro `%s' defined both with and"
2026 " without parameters", mname);
2027 /*
2028 * Some instances of the old code considered this a failure,
2029 * some others didn't. What is the right thing to do here?
2030 */
2031 free_tlist(expansion);
2032 return false; /* Failure */
2033 } else {
2034 /*
2035 * We're redefining, so we have to take over an
2036 * existing SMacro structure. This means freeing
2037 * what was already in it.
2038 */
2039 nasm_free(smac->name);
2040 free_tlist(smac->expansion);
2041 }
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002042 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002043 smtbl = ctx ? &ctx->localmac : &smacros;
2044 smhead = (SMacro **) hash_findi_add(smtbl, mname);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002045 smac = nasm_malloc(sizeof(SMacro));
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002046 smac->next = *smhead;
2047 *smhead = smac;
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002048 }
2049 smac->name = nasm_strdup(mname);
2050 smac->casesense = casesense;
2051 smac->nparam = nparam;
2052 smac->expansion = expansion;
2053 smac->in_progress = false;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002054 return true; /* Success */
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002055}
2056
2057/*
2058 * Undefine an smacro
2059 */
2060static void undef_smacro(Context *ctx, const char *mname)
2061{
2062 SMacro **smhead, *s, **sp;
H. Peter Anvin166c2472008-05-28 12:28:58 -07002063 struct hash_table *smtbl;
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002064
H. Peter Anvin166c2472008-05-28 12:28:58 -07002065 smtbl = ctx ? &ctx->localmac : &smacros;
2066 smhead = (SMacro **)hash_findi(smtbl, mname, NULL);
H. Peter Anvin70653092007-10-19 14:42:29 -07002067
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002068 if (smhead) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002069 /*
2070 * We now have a macro name... go hunt for it.
2071 */
2072 sp = smhead;
2073 while ((s = *sp) != NULL) {
2074 if (!mstrcmp(s->name, mname, s->casesense)) {
2075 *sp = s->next;
2076 nasm_free(s->name);
2077 free_tlist(s->expansion);
2078 nasm_free(s);
2079 } else {
2080 sp = &s->next;
2081 }
2082 }
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002083 }
2084}
2085
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002086/*
H. Peter Anvina26433d2008-07-16 14:40:01 -07002087 * Parse a mmacro specification.
2088 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002089static bool parse_mmacro_spec(Token *tline, MMacro *def, const char *directive)
H. Peter Anvina26433d2008-07-16 14:40:01 -07002090{
2091 bool err;
2092
2093 tline = tline->next;
2094 skip_white_(tline);
2095 tline = expand_id(tline);
2096 if (!tok_type_(tline, TOK_ID)) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002097 nasm_error(ERR_NONFATAL, "`%s' expects a macro name", directive);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002098 return false;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002099 }
Victor van den Elzenb916ede2008-07-23 15:14:22 +02002100
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002101 def->prev = NULL;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002102 def->name = nasm_strdup(tline->text);
2103 def->plus = false;
2104 def->nolist = false;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002105 def->in_progress = 0;
2106 def->rep_nest = NULL;
Victor van den Elzenb916ede2008-07-23 15:14:22 +02002107 def->nparam_min = 0;
2108 def->nparam_max = 0;
2109
H. Peter Anvina26433d2008-07-16 14:40:01 -07002110 tline = expand_smacro(tline->next);
2111 skip_white_(tline);
2112 if (!tok_type_(tline, TOK_NUMBER)) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002113 nasm_error(ERR_NONFATAL, "`%s' expects a parameter count", directive);
H. Peter Anvina26433d2008-07-16 14:40:01 -07002114 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002115 def->nparam_min = def->nparam_max =
2116 readnum(tline->text, &err);
2117 if (err)
H. Peter Anvin130736c2016-02-17 20:27:41 -08002118 nasm_error(ERR_NONFATAL,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002119 "unable to parse parameter count `%s'", tline->text);
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->next;
2123 if (tok_is_(tline, "*")) {
2124 def->nparam_max = INT_MAX;
2125 } else if (!tok_type_(tline, TOK_NUMBER)) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002126 nasm_error(ERR_NONFATAL,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002127 "`%s' expects a parameter count after `-'", directive);
2128 } else {
2129 def->nparam_max = readnum(tline->text, &err);
2130 if (err) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002131 nasm_error(ERR_NONFATAL, "unable to parse parameter count `%s'",
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002132 tline->text);
2133 }
2134 if (def->nparam_min > def->nparam_max) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002135 nasm_error(ERR_NONFATAL, "minimum parameter count exceeds maximum");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002136 }
2137 }
H. Peter Anvina26433d2008-07-16 14:40:01 -07002138 }
2139 if (tline && tok_is_(tline->next, "+")) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002140 tline = tline->next;
2141 def->plus = true;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002142 }
2143 if (tline && tok_type_(tline->next, TOK_ID) &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002144 !nasm_stricmp(tline->next->text, ".nolist")) {
2145 tline = tline->next;
2146 def->nolist = true;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002147 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002148
H. Peter Anvina26433d2008-07-16 14:40:01 -07002149 /*
2150 * Handle default parameters.
2151 */
2152 if (tline && tline->next) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002153 def->dlist = tline->next;
2154 tline->next = NULL;
2155 count_mmac_params(def->dlist, &def->ndefs, &def->defaults);
H. Peter Anvina26433d2008-07-16 14:40:01 -07002156 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002157 def->dlist = NULL;
2158 def->defaults = NULL;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002159 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002160 def->expansion = NULL;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002161
H. Peter Anvin89cee572009-07-15 09:16:54 -04002162 if (def->defaults && def->ndefs > def->nparam_max - def->nparam_min &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002163 !def->plus)
H. Peter Anvin130736c2016-02-17 20:27:41 -08002164 nasm_error(ERR_WARNING|ERR_PASS1|ERR_WARN_MDP,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002165 "too many default macro parameters");
Victor van den Elzenb916ede2008-07-23 15:14:22 +02002166
H. Peter Anvina26433d2008-07-16 14:40:01 -07002167 return true;
2168}
2169
2170
2171/*
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002172 * Decode a size directive
2173 */
2174static int parse_size(const char *str) {
2175 static const char *size_names[] =
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002176 { "byte", "dword", "oword", "qword", "tword", "word", "yword" };
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002177 static const int sizes[] =
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002178 { 0, 1, 4, 16, 8, 10, 2, 32 };
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002179
Cyrill Gorcunova7319242010-06-03 22:04:36 +04002180 return sizes[bsii(str, size_names, ARRAY_SIZE(size_names))+1];
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002181}
2182
Ed Beroset3ab3f412002-06-11 03:31:49 +00002183/**
2184 * find and process preprocessor directive in passed line
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002185 * Find out if a line contains a preprocessor directive, and deal
2186 * with it if so.
H. Peter Anvin70653092007-10-19 14:42:29 -07002187 *
Ed Beroset3ab3f412002-06-11 03:31:49 +00002188 * If a directive _is_ found, it is the responsibility of this routine
2189 * (and not the caller) to free_tlist() the line.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002190 *
Ed Beroset3ab3f412002-06-11 03:31:49 +00002191 * @param tline a pointer to the current tokeninzed line linked list
2192 * @return DIRECTIVE_FOUND or NO_DIRECTIVE_FOUND
H. Peter Anvin70653092007-10-19 14:42:29 -07002193 *
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002194 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002195static int do_directive(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00002196{
H. Peter Anvin4169a472007-09-12 01:29:43 +00002197 enum preproc_token i;
2198 int j;
H. Peter Anvin70055962007-10-11 00:05:31 -07002199 bool err;
2200 int nparam;
2201 bool nolist;
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07002202 bool casesense;
H. Peter Anvin8cfdb9d2007-09-14 18:36:01 -07002203 int k, m;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002204 int offset;
Jim Kukunas65a8afc2016-06-13 16:00:42 -04002205 char *p, *pp, *found_path;
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08002206 const char *mname;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002207 Include *inc;
2208 Context *ctx;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002209 Cond *cond;
2210 MMacro *mmac, **mmhead;
Jin Kyu Songc9486b92013-10-28 17:07:57 -07002211 Token *t = NULL, *tt, *param_start, *macro_start, *last, **tptr, *origline;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002212 Line *l;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002213 struct tokenval tokval;
2214 expr *evalresult;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002215 MMacro *tmp_defining; /* Used when manipulating rep_nest */
H. Peter Anvinf8ba53e2007-10-11 10:11:57 -07002216 int64_t count;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07002217 size_t len;
H. Peter Anvin8e3f75e2008-09-24 00:21:58 -07002218 int severity;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002219
2220 origline = tline;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002221
H. Peter Anvineba20a72002-04-30 20:53:55 +00002222 skip_white_(tline);
H. Peter Anvinf2936d72008-06-04 15:11:23 -07002223 if (!tline || !tok_type_(tline, TOK_PREPROC_ID) ||
H. Peter Anvine2c80182005-01-15 22:15:51 +00002224 (tline->text[1] == '%' || tline->text[1] == '$'
2225 || tline->text[1] == '!'))
2226 return NO_DIRECTIVE_FOUND;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002227
H. Peter Anvin4169a472007-09-12 01:29:43 +00002228 i = pp_token_hash(tline->text);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002229
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002230 /*
2231 * FIXME: We zap execution of PP_RMACRO, PP_IRMACRO, PP_EXITMACRO
2232 * since they are known to be buggy at moment, we need to fix them
2233 * in future release (2.09-2.10)
2234 */
Philipp Klokeb432f572013-03-31 12:01:23 +02002235 if (i == PP_RMACRO || i == PP_IRMACRO || i == PP_EXITMACRO) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002236 nasm_error(ERR_NONFATAL, "unknown preprocessor directive `%s'",
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002237 tline->text);
2238 return NO_DIRECTIVE_FOUND;
2239 }
2240
2241 /*
2242 * If we're in a non-emitting branch of a condition construct,
2243 * or walking to the end of an already terminated %rep block,
2244 * we should ignore all directives except for condition
2245 * directives.
2246 */
2247 if (((istk->conds && !emitting(istk->conds->state)) ||
2248 (istk->mstk && !istk->mstk->in_progress)) && !is_condition(i)) {
2249 return NO_DIRECTIVE_FOUND;
2250 }
2251
2252 /*
2253 * If we're defining a macro or reading a %rep block, we should
2254 * ignore all directives except for %macro/%imacro (which nest),
2255 * %endm/%endmacro, and (only if we're in a %rep block) %endrep.
2256 * If we're in a %rep block, another %rep nests, so should be let through.
2257 */
2258 if (defining && i != PP_MACRO && i != PP_IMACRO &&
2259 i != PP_RMACRO && i != PP_IRMACRO &&
2260 i != PP_ENDMACRO && i != PP_ENDM &&
2261 (defining->name || (i != PP_ENDREP && i != PP_REP))) {
2262 return NO_DIRECTIVE_FOUND;
2263 }
2264
2265 if (defining) {
2266 if (i == PP_MACRO || i == PP_IMACRO ||
2267 i == PP_RMACRO || i == PP_IRMACRO) {
2268 nested_mac_count++;
2269 return NO_DIRECTIVE_FOUND;
2270 } else if (nested_mac_count > 0) {
2271 if (i == PP_ENDMACRO) {
2272 nested_mac_count--;
2273 return NO_DIRECTIVE_FOUND;
2274 }
2275 }
2276 if (!defining->name) {
2277 if (i == PP_REP) {
2278 nested_rep_count++;
2279 return NO_DIRECTIVE_FOUND;
2280 } else if (nested_rep_count > 0) {
2281 if (i == PP_ENDREP) {
2282 nested_rep_count--;
2283 return NO_DIRECTIVE_FOUND;
2284 }
2285 }
2286 }
2287 }
2288
H. Peter Anvin4169a472007-09-12 01:29:43 +00002289 switch (i) {
2290 case PP_INVALID:
H. Peter Anvin130736c2016-02-17 20:27:41 -08002291 nasm_error(ERR_NONFATAL, "unknown preprocessor directive `%s'",
H. Peter Anvine2c80182005-01-15 22:15:51 +00002292 tline->text);
2293 return NO_DIRECTIVE_FOUND; /* didn't get it */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002294
H. Peter Anvine2c80182005-01-15 22:15:51 +00002295 case PP_STACKSIZE:
2296 /* Directive to tell NASM what the default stack size is. The
2297 * default is for a 16-bit stack, and this can be overriden with
2298 * %stacksize large.
H. Peter Anvine2c80182005-01-15 22:15:51 +00002299 */
2300 tline = tline->next;
2301 if (tline && tline->type == TOK_WHITESPACE)
2302 tline = tline->next;
2303 if (!tline || tline->type != TOK_ID) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002304 nasm_error(ERR_NONFATAL, "`%%stacksize' missing size parameter");
H. Peter Anvine2c80182005-01-15 22:15:51 +00002305 free_tlist(origline);
2306 return DIRECTIVE_FOUND;
2307 }
2308 if (nasm_stricmp(tline->text, "flat") == 0) {
2309 /* All subsequent ARG directives are for a 32-bit stack */
2310 StackSize = 4;
2311 StackPointer = "ebp";
2312 ArgOffset = 8;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002313 LocalOffset = 0;
Charles Crayne7eaf9192007-11-08 22:11:14 -08002314 } else if (nasm_stricmp(tline->text, "flat64") == 0) {
2315 /* All subsequent ARG directives are for a 64-bit stack */
2316 StackSize = 8;
2317 StackPointer = "rbp";
Per Jessen53252e02010-02-11 00:16:59 +03002318 ArgOffset = 16;
Charles Crayne7eaf9192007-11-08 22:11:14 -08002319 LocalOffset = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002320 } else if (nasm_stricmp(tline->text, "large") == 0) {
2321 /* All subsequent ARG directives are for a 16-bit stack,
2322 * far function call.
2323 */
2324 StackSize = 2;
2325 StackPointer = "bp";
2326 ArgOffset = 4;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002327 LocalOffset = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002328 } else if (nasm_stricmp(tline->text, "small") == 0) {
2329 /* All subsequent ARG directives are for a 16-bit stack,
2330 * far function call. We don't support near functions.
2331 */
2332 StackSize = 2;
2333 StackPointer = "bp";
2334 ArgOffset = 6;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002335 LocalOffset = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002336 } else {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002337 nasm_error(ERR_NONFATAL, "`%%stacksize' invalid size type");
H. Peter Anvine2c80182005-01-15 22:15:51 +00002338 free_tlist(origline);
2339 return DIRECTIVE_FOUND;
2340 }
2341 free_tlist(origline);
2342 return DIRECTIVE_FOUND;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002343
H. Peter Anvine2c80182005-01-15 22:15:51 +00002344 case PP_ARG:
2345 /* TASM like ARG directive to define arguments to functions, in
2346 * the following form:
2347 *
2348 * ARG arg1:WORD, arg2:DWORD, arg4:QWORD
2349 */
2350 offset = ArgOffset;
2351 do {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00002352 char *arg, directive[256];
H. Peter Anvine2c80182005-01-15 22:15:51 +00002353 int size = StackSize;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002354
H. Peter Anvine2c80182005-01-15 22:15:51 +00002355 /* Find the argument name */
2356 tline = tline->next;
2357 if (tline && tline->type == TOK_WHITESPACE)
2358 tline = tline->next;
2359 if (!tline || tline->type != TOK_ID) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002360 nasm_error(ERR_NONFATAL, "`%%arg' missing argument parameter");
H. Peter Anvine2c80182005-01-15 22:15:51 +00002361 free_tlist(origline);
2362 return DIRECTIVE_FOUND;
2363 }
2364 arg = tline->text;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002365
H. Peter Anvine2c80182005-01-15 22:15:51 +00002366 /* Find the argument size type */
2367 tline = tline->next;
2368 if (!tline || tline->type != TOK_OTHER
2369 || tline->text[0] != ':') {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002370 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00002371 "Syntax error processing `%%arg' directive");
2372 free_tlist(origline);
2373 return DIRECTIVE_FOUND;
2374 }
2375 tline = tline->next;
2376 if (!tline || tline->type != TOK_ID) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002377 nasm_error(ERR_NONFATAL, "`%%arg' missing size type parameter");
H. Peter Anvine2c80182005-01-15 22:15:51 +00002378 free_tlist(origline);
2379 return DIRECTIVE_FOUND;
2380 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002381
H. Peter Anvine2c80182005-01-15 22:15:51 +00002382 /* Allow macro expansion of type parameter */
Keith Kaniosb7a89542007-04-12 02:40:54 +00002383 tt = tokenize(tline->text);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002384 tt = expand_smacro(tt);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002385 size = parse_size(tt->text);
2386 if (!size) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002387 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00002388 "Invalid size type for `%%arg' missing directive");
2389 free_tlist(tt);
2390 free_tlist(origline);
2391 return DIRECTIVE_FOUND;
2392 }
2393 free_tlist(tt);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002394
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002395 /* Round up to even stack slots */
2396 size = ALIGN(size, StackSize);
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002397
H. Peter Anvine2c80182005-01-15 22:15:51 +00002398 /* Now define the macro for the argument */
2399 snprintf(directive, sizeof(directive), "%%define %s (%s+%d)",
2400 arg, StackPointer, offset);
Keith Kaniosb7a89542007-04-12 02:40:54 +00002401 do_directive(tokenize(directive));
H. Peter Anvine2c80182005-01-15 22:15:51 +00002402 offset += size;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002403
H. Peter Anvine2c80182005-01-15 22:15:51 +00002404 /* Move to the next argument in the list */
2405 tline = tline->next;
2406 if (tline && tline->type == TOK_WHITESPACE)
2407 tline = tline->next;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002408 } while (tline && tline->type == TOK_OTHER && tline->text[0] == ',');
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002409 ArgOffset = offset;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002410 free_tlist(origline);
2411 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002412
H. Peter Anvine2c80182005-01-15 22:15:51 +00002413 case PP_LOCAL:
2414 /* TASM like LOCAL directive to define local variables for a
2415 * function, in the following form:
2416 *
2417 * LOCAL local1:WORD, local2:DWORD, local4:QWORD = LocalSize
2418 *
2419 * The '= LocalSize' at the end is ignored by NASM, but is
2420 * required by TASM to define the local parameter size (and used
2421 * by the TASM macro package).
2422 */
2423 offset = LocalOffset;
2424 do {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00002425 char *local, directive[256];
H. Peter Anvine2c80182005-01-15 22:15:51 +00002426 int size = StackSize;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002427
H. Peter Anvine2c80182005-01-15 22:15:51 +00002428 /* Find the argument name */
2429 tline = tline->next;
2430 if (tline && tline->type == TOK_WHITESPACE)
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 argument parameter");
2435 free_tlist(origline);
2436 return DIRECTIVE_FOUND;
2437 }
2438 local = tline->text;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002439
H. Peter Anvine2c80182005-01-15 22:15:51 +00002440 /* Find the argument size type */
2441 tline = tline->next;
2442 if (!tline || tline->type != TOK_OTHER
2443 || tline->text[0] != ':') {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002444 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00002445 "Syntax error processing `%%local' directive");
2446 free_tlist(origline);
2447 return DIRECTIVE_FOUND;
2448 }
2449 tline = tline->next;
2450 if (!tline || tline->type != TOK_ID) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002451 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00002452 "`%%local' missing size type parameter");
2453 free_tlist(origline);
2454 return DIRECTIVE_FOUND;
2455 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00002456
H. Peter Anvine2c80182005-01-15 22:15:51 +00002457 /* Allow macro expansion of type parameter */
Keith Kaniosb7a89542007-04-12 02:40:54 +00002458 tt = tokenize(tline->text);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002459 tt = expand_smacro(tt);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002460 size = parse_size(tt->text);
2461 if (!size) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002462 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00002463 "Invalid size type for `%%local' missing directive");
2464 free_tlist(tt);
2465 free_tlist(origline);
2466 return DIRECTIVE_FOUND;
2467 }
2468 free_tlist(tt);
H. Peter Anvin734b1882002-04-30 21:01:08 +00002469
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002470 /* Round up to even stack slots */
2471 size = ALIGN(size, StackSize);
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002472
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002473 offset += size; /* Negative offset, increment before */
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002474
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002475 /* Now define the macro for the argument */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002476 snprintf(directive, sizeof(directive), "%%define %s (%s-%d)",
2477 local, StackPointer, offset);
Keith Kaniosb7a89542007-04-12 02:40:54 +00002478 do_directive(tokenize(directive));
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002479
H. Peter Anvine2c80182005-01-15 22:15:51 +00002480 /* Now define the assign to setup the enter_c macro correctly */
2481 snprintf(directive, sizeof(directive),
2482 "%%assign %%$localsize %%$localsize+%d", size);
Keith Kaniosb7a89542007-04-12 02:40:54 +00002483 do_directive(tokenize(directive));
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002484
H. Peter Anvine2c80182005-01-15 22:15:51 +00002485 /* Move to the next argument in the list */
2486 tline = tline->next;
2487 if (tline && tline->type == TOK_WHITESPACE)
2488 tline = tline->next;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002489 } while (tline && tline->type == TOK_OTHER && tline->text[0] == ',');
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002490 LocalOffset = offset;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002491 free_tlist(origline);
2492 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002493
H. Peter Anvine2c80182005-01-15 22:15:51 +00002494 case PP_CLEAR:
2495 if (tline->next)
H. Peter Anvin130736c2016-02-17 20:27:41 -08002496 nasm_error(ERR_WARNING|ERR_PASS1,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002497 "trailing garbage after `%%clear' ignored");
2498 free_macros();
2499 init_macros();
H. Peter Anvine2c80182005-01-15 22:15:51 +00002500 free_tlist(origline);
2501 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002502
H. Peter Anvin418ca702008-05-30 10:42:30 -07002503 case PP_DEPEND:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002504 t = tline->next = expand_smacro(tline->next);
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002505 skip_white_(t);
2506 if (!t || (t->type != TOK_STRING &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002507 t->type != TOK_INTERNAL_STRING)) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002508 nasm_error(ERR_NONFATAL, "`%%depend' expects a file name");
H. Peter Anvin418ca702008-05-30 10:42:30 -07002509 free_tlist(origline);
2510 return DIRECTIVE_FOUND; /* but we did _something_ */
2511 }
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002512 if (t->next)
H. Peter Anvin130736c2016-02-17 20:27:41 -08002513 nasm_error(ERR_WARNING|ERR_PASS1,
H. Peter Anvin418ca702008-05-30 10:42:30 -07002514 "trailing garbage after `%%depend' ignored");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002515 p = t->text;
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002516 if (t->type != TOK_INTERNAL_STRING)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002517 nasm_unquote_cstr(p, i);
2518 if (dephead && !in_list(*dephead, p)) {
2519 StrList *sl = nasm_malloc(strlen(p)+1+sizeof sl->next);
2520 sl->next = NULL;
2521 strcpy(sl->str, p);
2522 *deptail = sl;
2523 deptail = &sl->next;
2524 }
2525 free_tlist(origline);
H. Peter Anvin418ca702008-05-30 10:42:30 -07002526 return DIRECTIVE_FOUND;
2527
2528 case PP_INCLUDE:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002529 t = tline->next = expand_smacro(tline->next);
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002530 skip_white_(t);
H. Peter Anvind2456592008-06-19 15:04:18 -07002531
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002532 if (!t || (t->type != TOK_STRING &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002533 t->type != TOK_INTERNAL_STRING)) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002534 nasm_error(ERR_NONFATAL, "`%%include' expects a file name");
H. Peter Anvine2c80182005-01-15 22:15:51 +00002535 free_tlist(origline);
2536 return DIRECTIVE_FOUND; /* but we did _something_ */
2537 }
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002538 if (t->next)
H. Peter Anvin130736c2016-02-17 20:27:41 -08002539 nasm_error(ERR_WARNING|ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +00002540 "trailing garbage after `%%include' ignored");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002541 p = t->text;
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002542 if (t->type != TOK_INTERNAL_STRING)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002543 nasm_unquote_cstr(p, i);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002544 inc = nasm_malloc(sizeof(Include));
H. Peter Anvine2c80182005-01-15 22:15:51 +00002545 inc->next = istk;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002546 inc->conds = NULL;
Jim Kukunas65a8afc2016-06-13 16:00:42 -04002547 found_path = NULL;
H. Peter Anvind81a2352016-09-21 14:03:18 -07002548 inc->fp = inc_fopen(p, dephead, &deptail, &found_path,
2549 pass == 0 ? INC_OPTIONAL : INC_NEEDED, NF_TEXT);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002550 if (!inc->fp) {
2551 /* -MG given but file not found */
2552 nasm_free(inc);
2553 } else {
Jim Kukunas65a8afc2016-06-13 16:00:42 -04002554 inc->fname = src_set_fname(found_path ? found_path : p);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002555 inc->lineno = src_set_linnum(0);
2556 inc->lineinc = 1;
2557 inc->expansion = NULL;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002558 inc->mstk = NULL;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002559 istk = inc;
H. Peter Anvin8ac25aa2016-02-18 01:16:18 -08002560 lfmt->uplevel(LIST_INCLUDE);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002561 }
2562 free_tlist(origline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002563 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002564
H. Peter Anvind2456592008-06-19 15:04:18 -07002565 case PP_USE:
H. Peter Anvinf4ae5ad2008-06-19 18:39:24 -07002566 {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002567 static macros_t *use_pkg;
2568 const char *pkg_macro = NULL;
H. Peter Anvinf4ae5ad2008-06-19 18:39:24 -07002569
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002570 tline = tline->next;
2571 skip_white_(tline);
2572 tline = expand_id(tline);
H. Peter Anvind2456592008-06-19 15:04:18 -07002573
H. Peter Anvin264b7b92008-10-24 16:38:17 -07002574 if (!tline || (tline->type != TOK_STRING &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002575 tline->type != TOK_INTERNAL_STRING &&
2576 tline->type != TOK_ID)) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002577 nasm_error(ERR_NONFATAL, "`%%use' expects a package name");
H. Peter Anvind2456592008-06-19 15:04:18 -07002578 free_tlist(origline);
2579 return DIRECTIVE_FOUND; /* but we did _something_ */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002580 }
H. Peter Anvin264b7b92008-10-24 16:38:17 -07002581 if (tline->next)
H. Peter Anvin130736c2016-02-17 20:27:41 -08002582 nasm_error(ERR_WARNING|ERR_PASS1,
H. Peter Anvind2456592008-06-19 15:04:18 -07002583 "trailing garbage after `%%use' ignored");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002584 if (tline->type == TOK_STRING)
2585 nasm_unquote_cstr(tline->text, i);
2586 use_pkg = nasm_stdmac_find_package(tline->text);
2587 if (!use_pkg)
H. Peter Anvin130736c2016-02-17 20:27:41 -08002588 nasm_error(ERR_NONFATAL, "unknown `%%use' package: %s", tline->text);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002589 else
2590 pkg_macro = (char *)use_pkg + 1; /* The first string will be <%define>__USE_*__ */
Victor van den Elzen35eb2ea2010-03-10 22:33:48 +01002591 if (use_pkg && ! smacro_defined(NULL, pkg_macro, 0, NULL, true)) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002592 /* Not already included, go ahead and include it */
2593 stdmacpos = use_pkg;
2594 }
2595 free_tlist(origline);
H. Peter Anvind2456592008-06-19 15:04:18 -07002596 return DIRECTIVE_FOUND;
H. Peter Anvinf4ae5ad2008-06-19 18:39:24 -07002597 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002598 case PP_PUSH:
H. Peter Anvine2c80182005-01-15 22:15:51 +00002599 case PP_REPL:
H. Peter Anvin42b56392008-10-24 16:24:21 -07002600 case PP_POP:
H. Peter Anvine2c80182005-01-15 22:15:51 +00002601 tline = tline->next;
2602 skip_white_(tline);
2603 tline = expand_id(tline);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002604 if (tline) {
2605 if (!tok_type_(tline, TOK_ID)) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002606 nasm_error(ERR_NONFATAL, "`%s' expects a context identifier",
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002607 pp_directives[i]);
2608 free_tlist(origline);
2609 return DIRECTIVE_FOUND; /* but we did _something_ */
2610 }
2611 if (tline->next)
H. Peter Anvin130736c2016-02-17 20:27:41 -08002612 nasm_error(ERR_WARNING|ERR_PASS1,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002613 "trailing garbage after `%s' ignored",
2614 pp_directives[i]);
2615 p = nasm_strdup(tline->text);
2616 } else {
2617 p = NULL; /* Anonymous */
2618 }
H. Peter Anvin42b56392008-10-24 16:24:21 -07002619
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002620 if (i == PP_PUSH) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002621 ctx = nasm_malloc(sizeof(Context));
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002622 ctx->next = cstk;
2623 hash_init(&ctx->localmac, HASH_SMALL);
2624 ctx->name = p;
2625 ctx->number = unique++;
2626 cstk = ctx;
2627 } else {
2628 /* %pop or %repl */
2629 if (!cstk) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002630 nasm_error(ERR_NONFATAL, "`%s': context stack is empty",
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002631 pp_directives[i]);
2632 } else if (i == PP_POP) {
2633 if (p && (!cstk->name || nasm_stricmp(p, cstk->name)))
H. Peter Anvin130736c2016-02-17 20:27:41 -08002634 nasm_error(ERR_NONFATAL, "`%%pop' in wrong context: %s, "
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002635 "expected %s",
2636 cstk->name ? cstk->name : "anonymous", p);
2637 else
2638 ctx_pop();
2639 } else {
2640 /* i == PP_REPL */
2641 nasm_free(cstk->name);
2642 cstk->name = p;
2643 p = NULL;
2644 }
2645 nasm_free(p);
2646 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002647 free_tlist(origline);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002648 return DIRECTIVE_FOUND;
H. Peter Anvin8e3f75e2008-09-24 00:21:58 -07002649 case PP_FATAL:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002650 severity = ERR_FATAL;
2651 goto issue_error;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002652 case PP_ERROR:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002653 severity = ERR_NONFATAL;
2654 goto issue_error;
H. Peter Anvin7df04172008-06-10 18:27:38 -07002655 case PP_WARNING:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002656 severity = ERR_WARNING|ERR_WARN_USER;
2657 goto issue_error;
H. Peter Anvin8e3f75e2008-09-24 00:21:58 -07002658
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002659issue_error:
H. Peter Anvin7df04172008-06-10 18:27:38 -07002660 {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002661 /* Only error out if this is the final pass */
2662 if (pass != 2 && i != PP_FATAL)
2663 return DIRECTIVE_FOUND;
2664
2665 tline->next = expand_smacro(tline->next);
2666 tline = tline->next;
2667 skip_white_(tline);
2668 t = tline ? tline->next : NULL;
2669 skip_white_(t);
2670 if (tok_type_(tline, TOK_STRING) && !t) {
2671 /* The line contains only a quoted string */
2672 p = tline->text;
2673 nasm_unquote(p, NULL); /* Ignore NUL character truncation */
H. Peter Anvin130736c2016-02-17 20:27:41 -08002674 nasm_error(severity, "%s", p);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002675 } else {
2676 /* Not a quoted string, or more than a quoted string */
2677 p = detoken(tline, false);
H. Peter Anvin130736c2016-02-17 20:27:41 -08002678 nasm_error(severity, "%s", p);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002679 nasm_free(p);
2680 }
2681 free_tlist(origline);
2682 return DIRECTIVE_FOUND;
H. Peter Anvin7df04172008-06-10 18:27:38 -07002683 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00002684
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002685 CASE_PP_IF:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002686 if (istk->conds && !emitting(istk->conds->state))
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002687 j = COND_NEVER;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002688 else {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002689 j = if_condition(tline->next, i);
2690 tline->next = NULL; /* it got freed */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002691 j = j < 0 ? COND_NEVER : j ? COND_IF_TRUE : COND_IF_FALSE;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002692 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002693 cond = nasm_malloc(sizeof(Cond));
2694 cond->next = istk->conds;
2695 cond->state = j;
2696 istk->conds = cond;
2697 if(istk->mstk)
2698 istk->mstk->condcnt ++;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002699 free_tlist(origline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002700 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002701
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002702 CASE_PP_ELIF:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002703 if (!istk->conds)
H. Peter Anvin130736c2016-02-17 20:27:41 -08002704 nasm_error(ERR_FATAL, "`%s': no matching `%%if'", pp_directives[i]);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002705 switch(istk->conds->state) {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002706 case COND_IF_TRUE:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002707 istk->conds->state = COND_DONE;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002708 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002709
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002710 case COND_DONE:
2711 case COND_NEVER:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002712 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002713
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002714 case COND_ELSE_TRUE:
2715 case COND_ELSE_FALSE:
H. Peter Anvin130736c2016-02-17 20:27:41 -08002716 nasm_error(ERR_WARNING|ERR_PASS1|ERR_PP_PRECOND,
2717 "`%%elif' after `%%else' ignored");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002718 istk->conds->state = COND_NEVER;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002719 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002720
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002721 case COND_IF_FALSE:
2722 /*
2723 * IMPORTANT: In the case of %if, we will already have
2724 * called expand_mmac_params(); however, if we're
2725 * processing an %elif we must have been in a
2726 * non-emitting mode, which would have inhibited
2727 * the normal invocation of expand_mmac_params().
2728 * Therefore, we have to do it explicitly here.
2729 */
2730 j = if_condition(expand_mmac_params(tline->next), i);
2731 tline->next = NULL; /* it got freed */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002732 istk->conds->state =
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002733 j < 0 ? COND_NEVER : j ? COND_IF_TRUE : COND_IF_FALSE;
2734 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002735 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002736 free_tlist(origline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002737 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002738
H. Peter Anvine2c80182005-01-15 22:15:51 +00002739 case PP_ELSE:
2740 if (tline->next)
H. Peter Anvin130736c2016-02-17 20:27:41 -08002741 nasm_error(ERR_WARNING|ERR_PASS1|ERR_PP_PRECOND,
2742 "trailing garbage after `%%else' ignored");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002743 if (!istk->conds)
H. Peter Anvin130736c2016-02-17 20:27:41 -08002744 nasm_fatal(0, "`%%else: no matching `%%if'");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002745 switch(istk->conds->state) {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002746 case COND_IF_TRUE:
2747 case COND_DONE:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002748 istk->conds->state = COND_ELSE_FALSE;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002749 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002750
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002751 case COND_NEVER:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002752 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002753
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002754 case COND_IF_FALSE:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002755 istk->conds->state = COND_ELSE_TRUE;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002756 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002757
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002758 case COND_ELSE_TRUE:
2759 case COND_ELSE_FALSE:
H. Peter Anvin130736c2016-02-17 20:27:41 -08002760 nasm_error(ERR_WARNING|ERR_PASS1|ERR_PP_PRECOND,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002761 "`%%else' after `%%else' ignored.");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002762 istk->conds->state = COND_NEVER;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002763 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002764 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002765 free_tlist(origline);
2766 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002767
H. Peter Anvine2c80182005-01-15 22:15:51 +00002768 case PP_ENDIF:
2769 if (tline->next)
H. Peter Anvin130736c2016-02-17 20:27:41 -08002770 nasm_error(ERR_WARNING|ERR_PASS1|ERR_PP_PRECOND,
2771 "trailing garbage after `%%endif' ignored");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002772 if (!istk->conds)
H. Peter Anvin130736c2016-02-17 20:27:41 -08002773 nasm_error(ERR_FATAL, "`%%endif': no matching `%%if'");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002774 cond = istk->conds;
2775 istk->conds = cond->next;
2776 nasm_free(cond);
2777 if(istk->mstk)
2778 istk->mstk->condcnt --;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002779 free_tlist(origline);
2780 return DIRECTIVE_FOUND;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002781
H. Peter Anvindb8f96e2009-07-15 09:07:29 -04002782 case PP_RMACRO:
2783 case PP_IRMACRO:
H. Peter Anvine2c80182005-01-15 22:15:51 +00002784 case PP_MACRO:
2785 case PP_IMACRO:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002786 if (defining) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002787 nasm_error(ERR_FATAL, "`%s': already defining a macro",
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002788 pp_directives[i]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002789 return DIRECTIVE_FOUND;
2790 }
H. Peter Anvin4def1a82016-05-09 13:59:44 -07002791 defining = nasm_zalloc(sizeof(MMacro));
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002792 defining->max_depth =
2793 (i == PP_RMACRO) || (i == PP_IRMACRO) ? DEADMAN_LIMIT : 0;
2794 defining->casesense = (i == PP_MACRO) || (i == PP_RMACRO);
2795 if (!parse_mmacro_spec(tline, defining, pp_directives[i])) {
2796 nasm_free(defining);
2797 defining = NULL;
2798 return DIRECTIVE_FOUND;
2799 }
H. Peter Anvina26433d2008-07-16 14:40:01 -07002800
H. Peter Anvin4def1a82016-05-09 13:59:44 -07002801 src_get(&defining->xline, &defining->fname);
2802
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002803 mmac = (MMacro *) hash_findix(&mmacros, defining->name);
2804 while (mmac) {
2805 if (!strcmp(mmac->name, defining->name) &&
2806 (mmac->nparam_min <= defining->nparam_max
2807 || defining->plus)
2808 && (defining->nparam_min <= mmac->nparam_max
2809 || mmac->plus)) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002810 nasm_error(ERR_WARNING|ERR_PASS1,
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002811 "redefining multi-line macro `%s'", defining->name);
2812 return DIRECTIVE_FOUND;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002813 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002814 mmac = mmac->next;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002815 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002816 free_tlist(origline);
2817 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002818
H. Peter Anvine2c80182005-01-15 22:15:51 +00002819 case PP_ENDM:
2820 case PP_ENDMACRO:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002821 if (! (defining && defining->name)) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002822 nasm_error(ERR_NONFATAL, "`%s': not defining a macro", tline->text);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002823 return DIRECTIVE_FOUND;
2824 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002825 mmhead = (MMacro **) hash_findi_add(&mmacros, defining->name);
2826 defining->next = *mmhead;
2827 *mmhead = defining;
2828 defining = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002829 free_tlist(origline);
2830 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002831
H. Peter Anvin89cee572009-07-15 09:16:54 -04002832 case PP_EXITMACRO:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002833 /*
2834 * We must search along istk->expansion until we hit a
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002835 * macro-end marker for a macro with a name. Then we
2836 * bypass all lines between exitmacro and endmacro.
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002837 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002838 list_for_each(l, istk->expansion)
2839 if (l->finishes && l->finishes->name)
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002840 break;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002841
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002842 if (l) {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002843 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002844 * Remove all conditional entries relative to this
2845 * macro invocation. (safe to do in this context)
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002846 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002847 for ( ; l->finishes->condcnt > 0; l->finishes->condcnt --) {
2848 cond = istk->conds;
2849 istk->conds = cond->next;
2850 nasm_free(cond);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002851 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002852 istk->expansion = l;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002853 } else {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002854 nasm_error(ERR_NONFATAL, "`%%exitmacro' not within `%%macro' block");
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002855 }
Keith Kanios852f1ee2009-07-12 00:19:55 -05002856 free_tlist(origline);
2857 return DIRECTIVE_FOUND;
2858
H. Peter Anvina26433d2008-07-16 14:40:01 -07002859 case PP_UNMACRO:
2860 case PP_UNIMACRO:
2861 {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002862 MMacro **mmac_p;
2863 MMacro spec;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002864
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002865 spec.casesense = (i == PP_UNMACRO);
2866 if (!parse_mmacro_spec(tline, &spec, pp_directives[i])) {
2867 return DIRECTIVE_FOUND;
2868 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002869 mmac_p = (MMacro **) hash_findi(&mmacros, spec.name, NULL);
2870 while (mmac_p && *mmac_p) {
2871 mmac = *mmac_p;
2872 if (mmac->casesense == spec.casesense &&
2873 !mstrcmp(mmac->name, spec.name, spec.casesense) &&
2874 mmac->nparam_min == spec.nparam_min &&
2875 mmac->nparam_max == spec.nparam_max &&
2876 mmac->plus == spec.plus) {
2877 *mmac_p = mmac->next;
2878 free_mmacro(mmac);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002879 } else {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002880 mmac_p = &mmac->next;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002881 }
2882 }
2883 free_tlist(origline);
2884 free_tlist(spec.dlist);
2885 return DIRECTIVE_FOUND;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002886 }
2887
H. Peter Anvine2c80182005-01-15 22:15:51 +00002888 case PP_ROTATE:
2889 if (tline->next && tline->next->type == TOK_WHITESPACE)
2890 tline = tline->next;
H. Peter Anvin89cee572009-07-15 09:16:54 -04002891 if (!tline->next) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002892 free_tlist(origline);
H. Peter Anvin130736c2016-02-17 20:27:41 -08002893 nasm_error(ERR_NONFATAL, "`%%rotate' missing rotate count");
H. Peter Anvine2c80182005-01-15 22:15:51 +00002894 return DIRECTIVE_FOUND;
2895 }
2896 t = expand_smacro(tline->next);
2897 tline->next = NULL;
2898 free_tlist(origline);
2899 tline = t;
2900 tptr = &t;
2901 tokval.t_type = TOKEN_INVALID;
2902 evalresult =
H. Peter Anvin130736c2016-02-17 20:27:41 -08002903 evaluate(ppscan, tptr, &tokval, NULL, pass, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002904 free_tlist(tline);
2905 if (!evalresult)
2906 return DIRECTIVE_FOUND;
2907 if (tokval.t_type)
H. Peter Anvin130736c2016-02-17 20:27:41 -08002908 nasm_error(ERR_WARNING|ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +00002909 "trailing garbage after expression ignored");
2910 if (!is_simple(evalresult)) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002911 nasm_error(ERR_NONFATAL, "non-constant value given to `%%rotate'");
H. Peter Anvine2c80182005-01-15 22:15:51 +00002912 return DIRECTIVE_FOUND;
2913 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002914 mmac = istk->mstk;
2915 while (mmac && !mmac->name) /* avoid mistaking %reps for macros */
2916 mmac = mmac->next_active;
2917 if (!mmac) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002918 nasm_error(ERR_NONFATAL, "`%%rotate' invoked outside a macro call");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002919 } else if (mmac->nparam == 0) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002920 nasm_error(ERR_NONFATAL,
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002921 "`%%rotate' invoked within macro without parameters");
2922 } else {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002923 int rotate = mmac->rotate + reloc_value(evalresult);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002924
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002925 rotate %= (int)mmac->nparam;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002926 if (rotate < 0)
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002927 rotate += mmac->nparam;
2928
2929 mmac->rotate = rotate;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002930 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002931 return DIRECTIVE_FOUND;
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00002932
H. Peter Anvine2c80182005-01-15 22:15:51 +00002933 case PP_REP:
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002934 nolist = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002935 do {
2936 tline = tline->next;
2937 } while (tok_type_(tline, TOK_WHITESPACE));
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00002938
H. Peter Anvine2c80182005-01-15 22:15:51 +00002939 if (tok_type_(tline, TOK_ID) &&
2940 nasm_stricmp(tline->text, ".nolist") == 0) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002941 nolist = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002942 do {
2943 tline = tline->next;
2944 } while (tok_type_(tline, TOK_WHITESPACE));
2945 }
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00002946
H. Peter Anvine2c80182005-01-15 22:15:51 +00002947 if (tline) {
2948 t = expand_smacro(tline);
2949 tptr = &t;
2950 tokval.t_type = TOKEN_INVALID;
2951 evalresult =
H. Peter Anvin130736c2016-02-17 20:27:41 -08002952 evaluate(ppscan, tptr, &tokval, NULL, pass, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002953 if (!evalresult) {
2954 free_tlist(origline);
2955 return DIRECTIVE_FOUND;
2956 }
2957 if (tokval.t_type)
H. Peter Anvin130736c2016-02-17 20:27:41 -08002958 nasm_error(ERR_WARNING|ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +00002959 "trailing garbage after expression ignored");
2960 if (!is_simple(evalresult)) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002961 nasm_error(ERR_NONFATAL, "non-constant value given to `%%rep'");
H. Peter Anvine2c80182005-01-15 22:15:51 +00002962 return DIRECTIVE_FOUND;
2963 }
Cyrill Gorcunove091d6e2010-08-09 13:58:22 +04002964 count = reloc_value(evalresult);
2965 if (count >= REP_LIMIT) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002966 nasm_error(ERR_NONFATAL, "`%%rep' value exceeds limit");
Cyrill Gorcunove091d6e2010-08-09 13:58:22 +04002967 count = 0;
2968 } else
2969 count++;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002970 } else {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002971 nasm_error(ERR_NONFATAL, "`%%rep' expects a repeat count");
H. Peter Anvinf8ba53e2007-10-11 10:11:57 -07002972 count = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002973 }
2974 free_tlist(origline);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002975
2976 tmp_defining = defining;
2977 defining = nasm_malloc(sizeof(MMacro));
2978 defining->prev = NULL;
2979 defining->name = NULL; /* flags this macro as a %rep block */
2980 defining->casesense = false;
2981 defining->plus = false;
2982 defining->nolist = nolist;
2983 defining->in_progress = count;
2984 defining->max_depth = 0;
2985 defining->nparam_min = defining->nparam_max = 0;
2986 defining->defaults = NULL;
2987 defining->dlist = NULL;
2988 defining->expansion = NULL;
2989 defining->next_active = istk->mstk;
2990 defining->rep_nest = tmp_defining;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002991 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002992
H. Peter Anvine2c80182005-01-15 22:15:51 +00002993 case PP_ENDREP:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002994 if (!defining || defining->name) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002995 nasm_error(ERR_NONFATAL, "`%%endrep': no matching `%%rep'");
H. Peter Anvine2c80182005-01-15 22:15:51 +00002996 return DIRECTIVE_FOUND;
2997 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00002998
H. Peter Anvine2c80182005-01-15 22:15:51 +00002999 /*
3000 * Now we have a "macro" defined - although it has no name
3001 * and we won't be entering it in the hash tables - we must
3002 * push a macro-end marker for it on to istk->expansion.
3003 * After that, it will take care of propagating itself (a
3004 * macro-end marker line for a macro which is really a %rep
3005 * block will cause the macro to be re-expanded, complete
3006 * with another macro-end marker to ensure the process
3007 * continues) until the whole expansion is forcibly removed
3008 * from istk->expansion by a %exitrep.
3009 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003010 l = nasm_malloc(sizeof(Line));
3011 l->next = istk->expansion;
3012 l->finishes = defining;
3013 l->first = NULL;
3014 istk->expansion = l;
3015
3016 istk->mstk = defining;
3017
H. Peter Anvin8ac25aa2016-02-18 01:16:18 -08003018 lfmt->uplevel(defining->nolist ? LIST_MACRO_NOLIST : LIST_MACRO);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003019 tmp_defining = defining;
3020 defining = defining->rep_nest;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003021 free_tlist(origline);
3022 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003023
H. Peter Anvine2c80182005-01-15 22:15:51 +00003024 case PP_EXITREP:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003025 /*
3026 * We must search along istk->expansion until we hit a
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003027 * macro-end marker for a macro with no name. Then we set
3028 * its `in_progress' flag to 0.
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003029 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003030 list_for_each(l, istk->expansion)
3031 if (l->finishes && !l->finishes->name)
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003032 break;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003033
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003034 if (l)
3035 l->finishes->in_progress = 1;
3036 else
H. Peter Anvin130736c2016-02-17 20:27:41 -08003037 nasm_error(ERR_NONFATAL, "`%%exitrep' not within `%%rep' block");
H. Peter Anvine2c80182005-01-15 22:15:51 +00003038 free_tlist(origline);
3039 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003040
H. Peter Anvine2c80182005-01-15 22:15:51 +00003041 case PP_XDEFINE:
3042 case PP_IXDEFINE:
3043 case PP_DEFINE:
3044 case PP_IDEFINE:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003045 casesense = (i == PP_DEFINE || i == PP_XDEFINE);
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07003046
H. Peter Anvine2c80182005-01-15 22:15:51 +00003047 tline = tline->next;
3048 skip_white_(tline);
3049 tline = expand_id(tline);
3050 if (!tline || (tline->type != TOK_ID &&
3051 (tline->type != TOK_PREPROC_ID ||
3052 tline->text[1] != '$'))) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08003053 nasm_error(ERR_NONFATAL, "`%s' expects a macro identifier",
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003054 pp_directives[i]);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003055 free_tlist(origline);
3056 return DIRECTIVE_FOUND;
3057 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003058
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04003059 ctx = get_ctx(tline->text, &mname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003060 last = tline;
3061 param_start = tline = tline->next;
3062 nparam = 0;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003063
H. Peter Anvine2c80182005-01-15 22:15:51 +00003064 /* Expand the macro definition now for %xdefine and %ixdefine */
3065 if ((i == PP_XDEFINE) || (i == PP_IXDEFINE))
3066 tline = expand_smacro(tline);
H. Peter Anvin734b1882002-04-30 21:01:08 +00003067
H. Peter Anvine2c80182005-01-15 22:15:51 +00003068 if (tok_is_(tline, "(")) {
3069 /*
3070 * This macro has parameters.
3071 */
H. Peter Anvin734b1882002-04-30 21:01:08 +00003072
H. Peter Anvine2c80182005-01-15 22:15:51 +00003073 tline = tline->next;
3074 while (1) {
3075 skip_white_(tline);
3076 if (!tline) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08003077 nasm_error(ERR_NONFATAL, "parameter identifier expected");
H. Peter Anvine2c80182005-01-15 22:15:51 +00003078 free_tlist(origline);
3079 return DIRECTIVE_FOUND;
3080 }
3081 if (tline->type != TOK_ID) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08003082 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00003083 "`%s': parameter identifier expected",
3084 tline->text);
3085 free_tlist(origline);
3086 return DIRECTIVE_FOUND;
3087 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003088 tline->type = TOK_SMAC_PARAM + nparam++;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003089 tline = tline->next;
3090 skip_white_(tline);
3091 if (tok_is_(tline, ",")) {
3092 tline = tline->next;
H. Peter Anvinca348b62008-07-23 10:49:26 -04003093 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003094 if (!tok_is_(tline, ")")) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08003095 nasm_error(ERR_NONFATAL,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003096 "`)' expected to terminate macro template");
3097 free_tlist(origline);
3098 return DIRECTIVE_FOUND;
3099 }
3100 break;
3101 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00003102 }
3103 last = tline;
3104 tline = tline->next;
3105 }
3106 if (tok_type_(tline, TOK_WHITESPACE))
3107 last = tline, tline = tline->next;
3108 macro_start = NULL;
3109 last->next = NULL;
3110 t = tline;
3111 while (t) {
3112 if (t->type == TOK_ID) {
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04003113 list_for_each(tt, param_start)
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003114 if (tt->type >= TOK_SMAC_PARAM &&
H. Peter Anvine2c80182005-01-15 22:15:51 +00003115 !strcmp(tt->text, t->text))
3116 t->type = tt->type;
3117 }
3118 tt = t->next;
3119 t->next = macro_start;
3120 macro_start = t;
3121 t = tt;
3122 }
3123 /*
3124 * Good. We now have a macro name, a parameter count, and a
3125 * token list (in reverse order) for an expansion. We ought
3126 * to be OK just to create an SMacro, store it, and let
3127 * free_tlist have the rest of the line (which we have
3128 * carefully re-terminated after chopping off the expansion
3129 * from the end).
3130 */
H. Peter Anvin4db5a162007-10-11 13:42:09 -07003131 define_smacro(ctx, mname, casesense, nparam, macro_start);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003132 free_tlist(origline);
3133 return DIRECTIVE_FOUND;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003134
H. Peter Anvine2c80182005-01-15 22:15:51 +00003135 case PP_UNDEF:
3136 tline = tline->next;
3137 skip_white_(tline);
3138 tline = expand_id(tline);
3139 if (!tline || (tline->type != TOK_ID &&
3140 (tline->type != TOK_PREPROC_ID ||
3141 tline->text[1] != '$'))) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08003142 nasm_error(ERR_NONFATAL, "`%%undef' expects a macro identifier");
H. Peter Anvine2c80182005-01-15 22:15:51 +00003143 free_tlist(origline);
3144 return DIRECTIVE_FOUND;
3145 }
3146 if (tline->next) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08003147 nasm_error(ERR_WARNING|ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +00003148 "trailing garbage after macro name ignored");
3149 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003150
H. Peter Anvine2c80182005-01-15 22:15:51 +00003151 /* Find the context that symbol belongs to */
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04003152 ctx = get_ctx(tline->text, &mname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003153 undef_smacro(ctx, mname);
3154 free_tlist(origline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003155 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003156
H. Peter Anvin9e200162008-06-04 17:23:14 -07003157 case PP_DEFSTR:
3158 case PP_IDEFSTR:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003159 casesense = (i == PP_DEFSTR);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003160
3161 tline = tline->next;
3162 skip_white_(tline);
3163 tline = expand_id(tline);
3164 if (!tline || (tline->type != TOK_ID &&
3165 (tline->type != TOK_PREPROC_ID ||
3166 tline->text[1] != '$'))) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08003167 nasm_error(ERR_NONFATAL, "`%s' expects a macro identifier",
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003168 pp_directives[i]);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003169 free_tlist(origline);
3170 return DIRECTIVE_FOUND;
3171 }
3172
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04003173 ctx = get_ctx(tline->text, &mname);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003174 last = tline;
3175 tline = expand_smacro(tline->next);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003176 last->next = NULL;
H. Peter Anvin9e200162008-06-04 17:23:14 -07003177
3178 while (tok_type_(tline, TOK_WHITESPACE))
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003179 tline = delete_Token(tline);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003180
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003181 p = detoken(tline, false);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003182 macro_start = nasm_malloc(sizeof(*macro_start));
3183 macro_start->next = NULL;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003184 macro_start->text = nasm_quote(p, strlen(p));
3185 macro_start->type = TOK_STRING;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003186 macro_start->a.mac = NULL;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003187 nasm_free(p);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003188
3189 /*
3190 * We now have a macro name, an implicit parameter count of
3191 * zero, and a string token to use as an expansion. Create
3192 * and store an SMacro.
3193 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003194 define_smacro(ctx, mname, casesense, 0, macro_start);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003195 free_tlist(origline);
3196 return DIRECTIVE_FOUND;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003197
H. Peter Anvin2f55bda2009-07-14 15:04:04 -04003198 case PP_DEFTOK:
3199 case PP_IDEFTOK:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003200 casesense = (i == PP_DEFTOK);
3201
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003202 tline = tline->next;
3203 skip_white_(tline);
3204 tline = expand_id(tline);
3205 if (!tline || (tline->type != TOK_ID &&
3206 (tline->type != TOK_PREPROC_ID ||
3207 tline->text[1] != '$'))) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08003208 nasm_error(ERR_NONFATAL,
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003209 "`%s' expects a macro identifier as first parameter",
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003210 pp_directives[i]);
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003211 free_tlist(origline);
3212 return DIRECTIVE_FOUND;
3213 }
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04003214 ctx = get_ctx(tline->text, &mname);
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003215 last = tline;
3216 tline = expand_smacro(tline->next);
3217 last->next = NULL;
3218
3219 t = tline;
3220 while (tok_type_(t, TOK_WHITESPACE))
3221 t = t->next;
3222 /* t should now point to the string */
Cyrill Gorcunov6908e582010-09-06 19:36:15 +04003223 if (!tok_type_(t, TOK_STRING)) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08003224 nasm_error(ERR_NONFATAL,
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003225 "`%s` requires string as second parameter",
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003226 pp_directives[i]);
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003227 free_tlist(tline);
3228 free_tlist(origline);
3229 return DIRECTIVE_FOUND;
3230 }
3231
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04003232 /*
3233 * Convert the string to a token stream. Note that smacros
3234 * are stored with the token stream reversed, so we have to
3235 * reverse the output of tokenize().
3236 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003237 nasm_unquote_cstr(t->text, i);
H. Peter Anvinb40992c2010-09-15 08:57:21 -07003238 macro_start = reverse_tokens(tokenize(t->text));
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003239
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003240 /*
3241 * We now have a macro name, an implicit parameter count of
3242 * zero, and a numeric token to use as an expansion. Create
3243 * and store an SMacro.
3244 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003245 define_smacro(ctx, mname, casesense, 0, macro_start);
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003246 free_tlist(tline);
3247 free_tlist(origline);
3248 return DIRECTIVE_FOUND;
H. Peter Anvin9e200162008-06-04 17:23:14 -07003249
H. Peter Anvin418ca702008-05-30 10:42:30 -07003250 case PP_PATHSEARCH:
3251 {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003252 StrList *xsl = NULL;
3253 StrList **xst = &xsl;
H. Peter Anvin418ca702008-05-30 10:42:30 -07003254
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003255 casesense = true;
H. Peter Anvin418ca702008-05-30 10:42:30 -07003256
3257 tline = tline->next;
3258 skip_white_(tline);
3259 tline = expand_id(tline);
3260 if (!tline || (tline->type != TOK_ID &&
3261 (tline->type != TOK_PREPROC_ID ||
3262 tline->text[1] != '$'))) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08003263 nasm_error(ERR_NONFATAL,
H. Peter Anvin418ca702008-05-30 10:42:30 -07003264 "`%%pathsearch' expects a macro identifier as first parameter");
3265 free_tlist(origline);
3266 return DIRECTIVE_FOUND;
3267 }
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04003268 ctx = get_ctx(tline->text, &mname);
H. Peter Anvin418ca702008-05-30 10:42:30 -07003269 last = tline;
3270 tline = expand_smacro(tline->next);
3271 last->next = NULL;
3272
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003273 t = tline;
H. Peter Anvin418ca702008-05-30 10:42:30 -07003274 while (tok_type_(t, TOK_WHITESPACE))
3275 t = t->next;
3276
3277 if (!t || (t->type != TOK_STRING &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003278 t->type != TOK_INTERNAL_STRING)) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08003279 nasm_error(ERR_NONFATAL, "`%%pathsearch' expects a file name");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003280 free_tlist(tline);
H. Peter Anvin418ca702008-05-30 10:42:30 -07003281 free_tlist(origline);
3282 return DIRECTIVE_FOUND; /* but we did _something_ */
3283 }
3284 if (t->next)
H. Peter Anvin130736c2016-02-17 20:27:41 -08003285 nasm_error(ERR_WARNING|ERR_PASS1,
H. Peter Anvin418ca702008-05-30 10:42:30 -07003286 "trailing garbage after `%%pathsearch' ignored");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003287 p = t->text;
H. Peter Anvin427cc912008-06-01 21:43:03 -07003288 if (t->type != TOK_INTERNAL_STRING)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003289 nasm_unquote(p, NULL);
H. Peter Anvin418ca702008-05-30 10:42:30 -07003290
H. Peter Anvind81a2352016-09-21 14:03:18 -07003291 inc_fopen(p, &xsl, &xst, NULL, INC_PROBE, NF_BINARY);
3292 if (xsl)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003293 p = xsl->str;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003294 macro_start = nasm_malloc(sizeof(*macro_start));
3295 macro_start->next = NULL;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003296 macro_start->text = nasm_quote(p, strlen(p));
3297 macro_start->type = TOK_STRING;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003298 macro_start->a.mac = NULL;
3299 if (xsl)
3300 nasm_free(xsl);
H. Peter Anvin418ca702008-05-30 10:42:30 -07003301
3302 /*
3303 * We now have a macro name, an implicit parameter count of
3304 * zero, and a string token to use as an expansion. Create
3305 * and store an SMacro.
3306 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003307 define_smacro(ctx, mname, casesense, 0, macro_start);
3308 free_tlist(tline);
H. Peter Anvin418ca702008-05-30 10:42:30 -07003309 free_tlist(origline);
3310 return DIRECTIVE_FOUND;
3311 }
3312
H. Peter Anvine2c80182005-01-15 22:15:51 +00003313 case PP_STRLEN:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003314 casesense = true;
H. Peter Anvin70653092007-10-19 14:42:29 -07003315
H. Peter Anvine2c80182005-01-15 22:15:51 +00003316 tline = tline->next;
3317 skip_white_(tline);
3318 tline = expand_id(tline);
3319 if (!tline || (tline->type != TOK_ID &&
3320 (tline->type != TOK_PREPROC_ID ||
3321 tline->text[1] != '$'))) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08003322 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00003323 "`%%strlen' expects a macro identifier as first parameter");
3324 free_tlist(origline);
3325 return DIRECTIVE_FOUND;
3326 }
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04003327 ctx = get_ctx(tline->text, &mname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003328 last = tline;
3329 tline = expand_smacro(tline->next);
3330 last->next = NULL;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003331
H. Peter Anvine2c80182005-01-15 22:15:51 +00003332 t = tline;
3333 while (tok_type_(t, TOK_WHITESPACE))
3334 t = t->next;
3335 /* t should now point to the string */
Cyrill Gorcunov4e1d5ab2010-07-23 18:51:51 +04003336 if (!tok_type_(t, TOK_STRING)) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08003337 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00003338 "`%%strlen` requires string as second parameter");
3339 free_tlist(tline);
3340 free_tlist(origline);
3341 return DIRECTIVE_FOUND;
3342 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003343
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003344 macro_start = nasm_malloc(sizeof(*macro_start));
3345 macro_start->next = NULL;
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07003346 make_tok_num(macro_start, nasm_unquote(t->text, NULL));
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003347 macro_start->a.mac = NULL;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003348
H. Peter Anvine2c80182005-01-15 22:15:51 +00003349 /*
3350 * We now have a macro name, an implicit parameter count of
3351 * zero, and a numeric token to use as an expansion. Create
3352 * and store an SMacro.
3353 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003354 define_smacro(ctx, mname, casesense, 0, macro_start);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003355 free_tlist(tline);
3356 free_tlist(origline);
3357 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003358
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003359 case PP_STRCAT:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003360 casesense = true;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003361
3362 tline = tline->next;
3363 skip_white_(tline);
3364 tline = expand_id(tline);
3365 if (!tline || (tline->type != TOK_ID &&
3366 (tline->type != TOK_PREPROC_ID ||
3367 tline->text[1] != '$'))) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08003368 nasm_error(ERR_NONFATAL,
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003369 "`%%strcat' expects a macro identifier as first parameter");
3370 free_tlist(origline);
3371 return DIRECTIVE_FOUND;
3372 }
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04003373 ctx = get_ctx(tline->text, &mname);
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003374 last = tline;
3375 tline = expand_smacro(tline->next);
3376 last->next = NULL;
3377
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003378 len = 0;
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04003379 list_for_each(t, tline) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003380 switch (t->type) {
3381 case TOK_WHITESPACE:
3382 break;
3383 case TOK_STRING:
3384 len += t->a.len = nasm_unquote(t->text, NULL);
3385 break;
3386 case TOK_OTHER:
3387 if (!strcmp(t->text, ",")) /* permit comma separators */
3388 break;
3389 /* else fall through */
3390 default:
H. Peter Anvin130736c2016-02-17 20:27:41 -08003391 nasm_error(ERR_NONFATAL,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003392 "non-string passed to `%%strcat' (%d)", t->type);
3393 free_tlist(tline);
3394 free_tlist(origline);
3395 return DIRECTIVE_FOUND;
3396 }
3397 }
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003398
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003399 p = pp = nasm_malloc(len);
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04003400 list_for_each(t, tline) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003401 if (t->type == TOK_STRING) {
3402 memcpy(p, t->text, t->a.len);
3403 p += t->a.len;
3404 }
3405 }
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003406
3407 /*
3408 * We now have a macro name, an implicit parameter count of
3409 * zero, and a numeric token to use as an expansion. Create
3410 * and store an SMacro.
3411 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003412 macro_start = new_Token(NULL, TOK_STRING, NULL, 0);
3413 macro_start->text = nasm_quote(pp, len);
3414 nasm_free(pp);
3415 define_smacro(ctx, mname, casesense, 0, macro_start);
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003416 free_tlist(tline);
3417 free_tlist(origline);
3418 return DIRECTIVE_FOUND;
3419
H. Peter Anvine2c80182005-01-15 22:15:51 +00003420 case PP_SUBSTR:
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003421 {
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003422 int64_t start, count;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003423 size_t len;
H. Peter Anvind2456592008-06-19 15:04:18 -07003424
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003425 casesense = true;
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07003426
H. Peter Anvine2c80182005-01-15 22:15:51 +00003427 tline = tline->next;
3428 skip_white_(tline);
3429 tline = expand_id(tline);
3430 if (!tline || (tline->type != TOK_ID &&
3431 (tline->type != TOK_PREPROC_ID ||
3432 tline->text[1] != '$'))) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08003433 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00003434 "`%%substr' expects a macro identifier as first parameter");
3435 free_tlist(origline);
3436 return DIRECTIVE_FOUND;
3437 }
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04003438 ctx = get_ctx(tline->text, &mname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003439 last = tline;
3440 tline = expand_smacro(tline->next);
3441 last->next = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003442
Cyrill Gorcunov35519d62010-09-06 23:49:52 +04003443 if (tline) /* skip expanded id */
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003444 t = tline->next;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003445 while (tok_type_(t, TOK_WHITESPACE))
3446 t = t->next;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003447
H. Peter Anvine2c80182005-01-15 22:15:51 +00003448 /* t should now point to the string */
Cyrill Gorcunov35519d62010-09-06 23:49:52 +04003449 if (!tok_type_(t, TOK_STRING)) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08003450 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00003451 "`%%substr` requires string as second parameter");
3452 free_tlist(tline);
3453 free_tlist(origline);
3454 return DIRECTIVE_FOUND;
3455 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003456
H. Peter Anvine2c80182005-01-15 22:15:51 +00003457 tt = t->next;
3458 tptr = &tt;
3459 tokval.t_type = TOKEN_INVALID;
H. Peter Anvin130736c2016-02-17 20:27:41 -08003460 evalresult = evaluate(ppscan, tptr, &tokval, NULL, pass, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003461 if (!evalresult) {
3462 free_tlist(tline);
3463 free_tlist(origline);
3464 return DIRECTIVE_FOUND;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003465 } else if (!is_simple(evalresult)) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08003466 nasm_error(ERR_NONFATAL, "non-constant value given to `%%substr`");
H. Peter Anvine2c80182005-01-15 22:15:51 +00003467 free_tlist(tline);
3468 free_tlist(origline);
3469 return DIRECTIVE_FOUND;
3470 }
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003471 start = evalresult->value - 1;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003472
3473 while (tok_type_(tt, TOK_WHITESPACE))
3474 tt = tt->next;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003475 if (!tt) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003476 count = 1; /* Backwards compatibility: one character */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003477 } else {
3478 tokval.t_type = TOKEN_INVALID;
H. Peter Anvin130736c2016-02-17 20:27:41 -08003479 evalresult = evaluate(ppscan, tptr, &tokval, NULL, pass, NULL);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003480 if (!evalresult) {
3481 free_tlist(tline);
3482 free_tlist(origline);
3483 return DIRECTIVE_FOUND;
3484 } else if (!is_simple(evalresult)) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08003485 nasm_error(ERR_NONFATAL, "non-constant value given to `%%substr`");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003486 free_tlist(tline);
3487 free_tlist(origline);
3488 return DIRECTIVE_FOUND;
3489 }
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003490 count = evalresult->value;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003491 }
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003492
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003493 len = nasm_unquote(t->text, NULL);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003494
Cyrill Gorcunovcff031e2010-09-07 20:31:11 +04003495 /* make start and count being in range */
3496 if (start < 0)
3497 start = 0;
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003498 if (count < 0)
3499 count = len + count + 1 - start;
3500 if (start + count > (int64_t)len)
Cyrill Gorcunovcff031e2010-09-07 20:31:11 +04003501 count = len - start;
3502 if (!len || count < 0 || start >=(int64_t)len)
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003503 start = -1, count = 0; /* empty string */
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003504
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003505 macro_start = nasm_malloc(sizeof(*macro_start));
3506 macro_start->next = NULL;
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003507 macro_start->text = nasm_quote((start < 0) ? "" : t->text + start, count);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003508 macro_start->type = TOK_STRING;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003509 macro_start->a.mac = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003510
H. Peter Anvine2c80182005-01-15 22:15:51 +00003511 /*
3512 * We now have a macro name, an implicit parameter count of
3513 * zero, and a numeric token to use as an expansion. Create
3514 * and store an SMacro.
3515 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003516 define_smacro(ctx, mname, casesense, 0, macro_start);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003517 free_tlist(tline);
3518 free_tlist(origline);
3519 return DIRECTIVE_FOUND;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003520 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003521
H. Peter Anvine2c80182005-01-15 22:15:51 +00003522 case PP_ASSIGN:
3523 case PP_IASSIGN:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003524 casesense = (i == PP_ASSIGN);
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07003525
H. Peter Anvine2c80182005-01-15 22:15:51 +00003526 tline = tline->next;
3527 skip_white_(tline);
3528 tline = expand_id(tline);
3529 if (!tline || (tline->type != TOK_ID &&
3530 (tline->type != TOK_PREPROC_ID ||
3531 tline->text[1] != '$'))) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08003532 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00003533 "`%%%sassign' expects a macro identifier",
3534 (i == PP_IASSIGN ? "i" : ""));
3535 free_tlist(origline);
3536 return DIRECTIVE_FOUND;
3537 }
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04003538 ctx = get_ctx(tline->text, &mname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003539 last = tline;
3540 tline = expand_smacro(tline->next);
3541 last->next = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003542
H. Peter Anvine2c80182005-01-15 22:15:51 +00003543 t = tline;
3544 tptr = &t;
3545 tokval.t_type = TOKEN_INVALID;
H. Peter Anvin130736c2016-02-17 20:27:41 -08003546 evalresult = evaluate(ppscan, tptr, &tokval, NULL, pass, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003547 free_tlist(tline);
3548 if (!evalresult) {
3549 free_tlist(origline);
3550 return DIRECTIVE_FOUND;
3551 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003552
H. Peter Anvine2c80182005-01-15 22:15:51 +00003553 if (tokval.t_type)
H. Peter Anvin130736c2016-02-17 20:27:41 -08003554 nasm_error(ERR_WARNING|ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +00003555 "trailing garbage after expression ignored");
H. Peter Anvin734b1882002-04-30 21:01:08 +00003556
H. Peter Anvine2c80182005-01-15 22:15:51 +00003557 if (!is_simple(evalresult)) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08003558 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00003559 "non-constant value given to `%%%sassign'",
3560 (i == PP_IASSIGN ? "i" : ""));
3561 free_tlist(origline);
3562 return DIRECTIVE_FOUND;
3563 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003564
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003565 macro_start = nasm_malloc(sizeof(*macro_start));
3566 macro_start->next = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003567 make_tok_num(macro_start, reloc_value(evalresult));
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003568 macro_start->a.mac = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003569
H. Peter Anvine2c80182005-01-15 22:15:51 +00003570 /*
3571 * We now have a macro name, an implicit parameter count of
3572 * zero, and a numeric token to use as an expansion. Create
3573 * and store an SMacro.
3574 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003575 define_smacro(ctx, mname, casesense, 0, macro_start);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003576 free_tlist(origline);
3577 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003578
H. Peter Anvine2c80182005-01-15 22:15:51 +00003579 case PP_LINE:
3580 /*
3581 * Syntax is `%line nnn[+mmm] [filename]'
3582 */
3583 tline = tline->next;
3584 skip_white_(tline);
3585 if (!tok_type_(tline, TOK_NUMBER)) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08003586 nasm_error(ERR_NONFATAL, "`%%line' expects line number");
H. Peter Anvine2c80182005-01-15 22:15:51 +00003587 free_tlist(origline);
3588 return DIRECTIVE_FOUND;
3589 }
H. Peter Anvin70055962007-10-11 00:05:31 -07003590 k = readnum(tline->text, &err);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003591 m = 1;
3592 tline = tline->next;
3593 if (tok_is_(tline, "+")) {
3594 tline = tline->next;
3595 if (!tok_type_(tline, TOK_NUMBER)) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08003596 nasm_error(ERR_NONFATAL, "`%%line' expects line increment");
H. Peter Anvine2c80182005-01-15 22:15:51 +00003597 free_tlist(origline);
3598 return DIRECTIVE_FOUND;
3599 }
H. Peter Anvin70055962007-10-11 00:05:31 -07003600 m = readnum(tline->text, &err);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003601 tline = tline->next;
3602 }
3603 skip_white_(tline);
3604 src_set_linnum(k);
3605 istk->lineinc = m;
3606 if (tline) {
H. Peter Anvin274cda82016-05-10 02:56:29 -07003607 char *fname = detoken(tline, false);
3608 src_set_fname(fname);
3609 nasm_free(fname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003610 }
3611 free_tlist(origline);
3612 return DIRECTIVE_FOUND;
Keith Kaniosb307a4f2010-11-06 17:41:51 -05003613
H. Peter Anvine2c80182005-01-15 22:15:51 +00003614 default:
H. Peter Anvin130736c2016-02-17 20:27:41 -08003615 nasm_error(ERR_FATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00003616 "preprocessor directive `%s' not yet implemented",
H. Peter Anvin4169a472007-09-12 01:29:43 +00003617 pp_directives[i]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003618 return DIRECTIVE_FOUND;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003619 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003620}
3621
3622/*
H. Peter Anvin76690a12002-04-30 20:52:49 +00003623 * Ensure that a macro parameter contains a condition code and
3624 * nothing else. Return the condition code index if so, or -1
3625 * otherwise.
3626 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003627static int find_cc(Token * t)
H. Peter Anvineba20a72002-04-30 20:53:55 +00003628{
H. Peter Anvin76690a12002-04-30 20:52:49 +00003629 Token *tt;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003630
H. Peter Anvin25a99342007-09-22 17:45:45 -07003631 if (!t)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003632 return -1; /* Probably a %+ without a space */
H. Peter Anvin25a99342007-09-22 17:45:45 -07003633
H. Peter Anvineba20a72002-04-30 20:53:55 +00003634 skip_white_(t);
H. Peter Anvin76690a12002-04-30 20:52:49 +00003635 if (t->type != TOK_ID)
H. Peter Anvine2c80182005-01-15 22:15:51 +00003636 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003637 tt = t->next;
H. Peter Anvineba20a72002-04-30 20:53:55 +00003638 skip_white_(tt);
H. Peter Anvin76690a12002-04-30 20:52:49 +00003639 if (tt && (tt->type != TOK_OTHER || strcmp(tt->text, ",")))
H. Peter Anvine2c80182005-01-15 22:15:51 +00003640 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003641
Cyrill Gorcunov19456392012-05-02 00:18:56 +04003642 return bsii(t->text, (const char **)conditions, ARRAY_SIZE(conditions));
H. Peter Anvin76690a12002-04-30 20:52:49 +00003643}
3644
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003645/*
3646 * This routines walks over tokens strem and hadnles tokens
3647 * pasting, if @handle_explicit passed then explicit pasting
3648 * term is handled, otherwise -- implicit pastings only.
3649 */
Cyrill Gorcunov575d4282010-10-06 00:25:55 +04003650static bool paste_tokens(Token **head, const struct tokseq_match *m,
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003651 size_t mnum, bool handle_explicit)
H. Peter Anvind784a082009-04-20 14:01:18 -07003652{
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003653 Token *tok, *next, **prev_next, **prev_nonspace;
3654 bool pasted = false;
3655 char *buf, *p;
3656 size_t len, i;
H. Peter Anvind784a082009-04-20 14:01:18 -07003657
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003658 /*
3659 * The last token before pasting. We need it
3660 * to be able to connect new handled tokens.
3661 * In other words if there were a tokens stream
3662 *
3663 * A -> B -> C -> D
3664 *
3665 * and we've joined tokens B and C, the resulting
3666 * stream should be
3667 *
3668 * A -> BC -> D
3669 */
3670 tok = *head;
3671 prev_next = NULL;
3672
3673 if (!tok_type_(tok, TOK_WHITESPACE) && !tok_type_(tok, TOK_PASTE))
3674 prev_nonspace = head;
3675 else
3676 prev_nonspace = NULL;
3677
3678 while (tok && (next = tok->next)) {
3679
3680 switch (tok->type) {
H. Peter Anvind784a082009-04-20 14:01:18 -07003681 case TOK_WHITESPACE:
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003682 /* Zap redundant whitespaces */
3683 while (tok_type_(next, TOK_WHITESPACE))
3684 next = delete_Token(next);
3685 tok->next = next;
H. Peter Anvind784a082009-04-20 14:01:18 -07003686 break;
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003687
3688 case TOK_PASTE:
3689 /* Explicit pasting */
3690 if (!handle_explicit)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003691 break;
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003692 next = delete_Token(tok);
3693
3694 while (tok_type_(next, TOK_WHITESPACE))
3695 next = delete_Token(next);
3696
3697 if (!pasted)
3698 pasted = true;
3699
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003700 /* Left pasting token is start of line */
3701 if (!prev_nonspace)
H. Peter Anvin130736c2016-02-17 20:27:41 -08003702 nasm_error(ERR_FATAL, "No lvalue found on pasting");
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003703
Cyrill Gorcunov8b5c9fb2013-02-04 01:24:54 +04003704 /*
3705 * No ending token, this might happen in two
3706 * cases
3707 *
3708 * 1) There indeed no right token at all
3709 * 2) There is a bare "%define ID" statement,
3710 * and @ID does expand to whitespace.
3711 *
3712 * So technically we need to do a grammar analysis
3713 * in another stage of parsing, but for now lets don't
3714 * change the behaviour people used to. Simply allow
3715 * whitespace after paste token.
3716 */
3717 if (!next) {
3718 /*
3719 * Zap ending space tokens and that's all.
3720 */
3721 tok = (*prev_nonspace)->next;
3722 while (tok_type_(tok, TOK_WHITESPACE))
3723 tok = delete_Token(tok);
3724 tok = *prev_nonspace;
3725 tok->next = NULL;
3726 break;
3727 }
3728
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003729 tok = *prev_nonspace;
3730 while (tok_type_(tok, TOK_WHITESPACE))
3731 tok = delete_Token(tok);
3732 len = strlen(tok->text);
3733 len += strlen(next->text);
3734
3735 p = buf = nasm_malloc(len + 1);
3736 strcpy(p, tok->text);
3737 p = strchr(p, '\0');
3738 strcpy(p, next->text);
3739
3740 delete_Token(tok);
3741
3742 tok = tokenize(buf);
3743 nasm_free(buf);
3744
3745 *prev_nonspace = tok;
3746 while (tok && tok->next)
3747 tok = tok->next;
3748
3749 tok->next = delete_Token(next);
3750
3751 /* Restart from pasted tokens head */
3752 tok = *prev_nonspace;
3753 break;
3754
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003755 default:
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003756 /* implicit pasting */
Cyrill Gorcunov575d4282010-10-06 00:25:55 +04003757 for (i = 0; i < mnum; i++) {
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003758 if (!(PP_CONCAT_MATCH(tok, m[i].mask_head)))
3759 continue;
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +04003760
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003761 len = 0;
3762 while (next && PP_CONCAT_MATCH(next, m[i].mask_tail)) {
3763 len += strlen(next->text);
3764 next = next->next;
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +04003765 }
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003766
3767 /* No match */
3768 if (tok == next)
3769 break;
3770
3771 len += strlen(tok->text);
3772 p = buf = nasm_malloc(len + 1);
3773
3774 while (tok != next) {
3775 strcpy(p, tok->text);
3776 p = strchr(p, '\0');
3777 tok = delete_Token(tok);
3778 }
3779
3780 tok = tokenize(buf);
3781 nasm_free(buf);
3782
3783 if (prev_next)
3784 *prev_next = tok;
3785 else
3786 *head = tok;
3787
3788 /*
3789 * Connect pasted into original stream,
3790 * ie A -> new-tokens -> B
3791 */
3792 while (tok && tok->next)
3793 tok = tok->next;
3794 tok->next = next;
3795
3796 if (!pasted)
3797 pasted = true;
3798
3799 /* Restart from pasted tokens head */
3800 tok = prev_next ? *prev_next : *head;
Cyrill Gorcunov575d4282010-10-06 00:25:55 +04003801 }
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003802
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003803 break;
H. Peter Anvind784a082009-04-20 14:01:18 -07003804 }
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003805
3806 prev_next = &tok->next;
3807
3808 if (tok->next &&
3809 !tok_type_(tok->next, TOK_WHITESPACE) &&
3810 !tok_type_(tok->next, TOK_PASTE))
3811 prev_nonspace = prev_next;
3812
3813 tok = tok->next;
H. Peter Anvind784a082009-04-20 14:01:18 -07003814 }
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003815
3816 return pasted;
H. Peter Anvind784a082009-04-20 14:01:18 -07003817}
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003818
3819/*
3820 * expands to a list of tokens from %{x:y}
3821 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003822static Token *expand_mmac_params_range(MMacro *mac, Token *tline, Token ***last)
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003823{
3824 Token *t = tline, **tt, *tm, *head;
3825 char *pos;
3826 int fst, lst, j, i;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003827
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003828 pos = strchr(tline->text, ':');
3829 nasm_assert(pos);
3830
3831 lst = atoi(pos + 1);
3832 fst = atoi(tline->text + 1);
3833
3834 /*
3835 * only macros params are accounted so
3836 * if someone passes %0 -- we reject such
3837 * value(s)
3838 */
3839 if (lst == 0 || fst == 0)
3840 goto err;
3841
3842 /* the values should be sane */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003843 if ((fst > (int)mac->nparam || fst < (-(int)mac->nparam)) ||
3844 (lst > (int)mac->nparam || lst < (-(int)mac->nparam)))
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003845 goto err;
3846
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003847 fst = fst < 0 ? fst + (int)mac->nparam + 1: fst;
3848 lst = lst < 0 ? lst + (int)mac->nparam + 1: lst;
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003849
3850 /* counted from zero */
3851 fst--, lst--;
3852
3853 /*
Cyrill Gorcunove75331c2013-11-09 12:02:15 +04003854 * It will be at least one token. Note we
3855 * need to scan params until separator, otherwise
3856 * only first token will be passed.
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003857 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003858 tm = mac->params[(fst + mac->rotate) % mac->nparam];
Cyrill Gorcunove75331c2013-11-09 12:02:15 +04003859 head = new_Token(NULL, tm->type, tm->text, 0);
3860 tt = &head->next, tm = tm->next;
3861 while (tok_isnt_(tm, ",")) {
3862 t = new_Token(NULL, tm->type, tm->text, 0);
3863 *tt = t, tt = &t->next, tm = tm->next;
3864 }
3865
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003866 if (fst < lst) {
3867 for (i = fst + 1; i <= lst; i++) {
3868 t = new_Token(NULL, TOK_OTHER, ",", 0);
3869 *tt = t, tt = &t->next;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003870 j = (i + mac->rotate) % mac->nparam;
3871 tm = mac->params[j];
Cyrill Gorcunove75331c2013-11-09 12:02:15 +04003872 while (tok_isnt_(tm, ",")) {
3873 t = new_Token(NULL, tm->type, tm->text, 0);
3874 *tt = t, tt = &t->next, tm = tm->next;
3875 }
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003876 }
3877 } else {
3878 for (i = fst - 1; i >= lst; i--) {
3879 t = new_Token(NULL, TOK_OTHER, ",", 0);
3880 *tt = t, tt = &t->next;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003881 j = (i + mac->rotate) % mac->nparam;
3882 tm = mac->params[j];
Cyrill Gorcunove75331c2013-11-09 12:02:15 +04003883 while (tok_isnt_(tm, ",")) {
3884 t = new_Token(NULL, tm->type, tm->text, 0);
3885 *tt = t, tt = &t->next, tm = tm->next;
3886 }
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003887 }
3888 }
3889
3890 *last = tt;
3891 return head;
3892
3893err:
H. Peter Anvin130736c2016-02-17 20:27:41 -08003894 nasm_error(ERR_NONFATAL, "`%%{%s}': macro parameters out of range",
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003895 &tline->text[1]);
3896 return tline;
3897}
3898
H. Peter Anvin76690a12002-04-30 20:52:49 +00003899/*
3900 * Expand MMacro-local things: parameter references (%0, %n, %+n,
H. Peter Anvin67c63722008-10-26 23:49:00 -07003901 * %-n) and MMacro-local identifiers (%%foo) as well as
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003902 * macro indirection (%[...]) and range (%{..:..}).
H. Peter Anvin76690a12002-04-30 20:52:49 +00003903 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003904static Token *expand_mmac_params(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00003905{
H. Peter Anvin734b1882002-04-30 21:01:08 +00003906 Token *t, *tt, **tail, *thead;
H. Peter Anvin6125b622009-04-08 14:02:25 -07003907 bool changed = false;
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003908 char *pos;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003909
3910 tail = &thead;
3911 thead = NULL;
3912
H. Peter Anvine2c80182005-01-15 22:15:51 +00003913 while (tline) {
3914 if (tline->type == TOK_PREPROC_ID &&
Cyrill Gorcunovca611192010-06-04 09:22:12 +04003915 (((tline->text[1] == '+' || tline->text[1] == '-') && tline->text[2]) ||
3916 (tline->text[1] >= '0' && tline->text[1] <= '9') ||
3917 tline->text[1] == '%')) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00003918 char *text = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003919 int type = 0, cc; /* type = 0 to placate optimisers */
Keith Kaniosa6dfa782007-04-13 16:47:53 +00003920 char tmpbuf[30];
H. Peter Anvin25a99342007-09-22 17:45:45 -07003921 unsigned int n;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003922 int i;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003923 MMacro *mac;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003924
H. Peter Anvine2c80182005-01-15 22:15:51 +00003925 t = tline;
3926 tline = tline->next;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003927
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003928 mac = istk->mstk;
3929 while (mac && !mac->name) /* avoid mistaking %reps for macros */
3930 mac = mac->next_active;
3931 if (!mac) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08003932 nasm_error(ERR_NONFATAL, "`%s': not in a macro call", t->text);
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003933 } else {
3934 pos = strchr(t->text, ':');
3935 if (!pos) {
3936 switch (t->text[1]) {
3937 /*
3938 * We have to make a substitution of one of the
3939 * forms %1, %-1, %+1, %%foo, %0.
3940 */
3941 case '0':
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003942 type = TOK_NUMBER;
3943 snprintf(tmpbuf, sizeof(tmpbuf), "%d", mac->nparam);
3944 text = nasm_strdup(tmpbuf);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003945 break;
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003946 case '%':
H. Peter Anvine2c80182005-01-15 22:15:51 +00003947 type = TOK_ID;
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003948 snprintf(tmpbuf, sizeof(tmpbuf), "..@%"PRIu64".",
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003949 mac->unique);
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003950 text = nasm_strcat(tmpbuf, t->text + 2);
3951 break;
3952 case '-':
3953 n = atoi(t->text + 2) - 1;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003954 if (n >= mac->nparam)
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003955 tt = NULL;
3956 else {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003957 if (mac->nparam > 1)
3958 n = (n + mac->rotate) % mac->nparam;
3959 tt = mac->params[n];
H. Peter Anvine2c80182005-01-15 22:15:51 +00003960 }
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003961 cc = find_cc(tt);
3962 if (cc == -1) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08003963 nasm_error(ERR_NONFATAL,
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003964 "macro parameter %d is not a condition code",
3965 n + 1);
3966 text = NULL;
3967 } else {
3968 type = TOK_ID;
3969 if (inverse_ccs[cc] == -1) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08003970 nasm_error(ERR_NONFATAL,
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003971 "condition code `%s' is not invertible",
3972 conditions[cc]);
3973 text = NULL;
3974 } else
3975 text = nasm_strdup(conditions[inverse_ccs[cc]]);
3976 }
3977 break;
3978 case '+':
3979 n = atoi(t->text + 2) - 1;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003980 if (n >= mac->nparam)
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003981 tt = NULL;
3982 else {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003983 if (mac->nparam > 1)
3984 n = (n + mac->rotate) % mac->nparam;
3985 tt = mac->params[n];
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003986 }
3987 cc = find_cc(tt);
3988 if (cc == -1) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08003989 nasm_error(ERR_NONFATAL,
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003990 "macro parameter %d is not a condition code",
3991 n + 1);
3992 text = NULL;
3993 } else {
3994 type = TOK_ID;
3995 text = nasm_strdup(conditions[cc]);
3996 }
3997 break;
3998 default:
3999 n = atoi(t->text + 1) - 1;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004000 if (n >= mac->nparam)
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004001 tt = NULL;
4002 else {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004003 if (mac->nparam > 1)
4004 n = (n + mac->rotate) % mac->nparam;
4005 tt = mac->params[n];
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004006 }
4007 if (tt) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004008 for (i = 0; i < mac->paramlen[n]; i++) {
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004009 *tail = new_Token(NULL, tt->type, tt->text, 0);
4010 tail = &(*tail)->next;
4011 tt = tt->next;
4012 }
4013 }
4014 text = NULL; /* we've done it here */
4015 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004016 }
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004017 } else {
4018 /*
4019 * seems we have a parameters range here
4020 */
4021 Token *head, **last;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004022 head = expand_mmac_params_range(mac, t, &last);
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004023 if (head != t) {
4024 *tail = head;
4025 *last = tline;
4026 tline = head;
4027 text = NULL;
4028 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00004029 }
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004030 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00004031 if (!text) {
4032 delete_Token(t);
4033 } else {
4034 *tail = t;
4035 tail = &t->next;
4036 t->type = type;
4037 nasm_free(t->text);
4038 t->text = text;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004039 t->a.mac = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004040 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004041 changed = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004042 continue;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004043 } else if (tline->type == TOK_INDIRECT) {
4044 t = tline;
4045 tline = tline->next;
4046 tt = tokenize(t->text);
4047 tt = expand_mmac_params(tt);
4048 tt = expand_smacro(tt);
4049 *tail = tt;
4050 while (tt) {
4051 tt->a.mac = NULL; /* Necessary? */
4052 tail = &tt->next;
4053 tt = tt->next;
4054 }
4055 delete_Token(t);
4056 changed = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004057 } else {
4058 t = *tail = tline;
4059 tline = tline->next;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004060 t->a.mac = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004061 tail = &t->next;
4062 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00004063 }
H. Peter Anvineba20a72002-04-30 20:53:55 +00004064 *tail = NULL;
H. Peter Anvin67c63722008-10-26 23:49:00 -07004065
Cyrill Gorcunovc6a742c2011-06-27 01:23:09 +04004066 if (changed) {
4067 const struct tokseq_match t[] = {
4068 {
4069 PP_CONCAT_MASK(TOK_ID) |
4070 PP_CONCAT_MASK(TOK_FLOAT), /* head */
4071 PP_CONCAT_MASK(TOK_ID) |
4072 PP_CONCAT_MASK(TOK_NUMBER) |
4073 PP_CONCAT_MASK(TOK_FLOAT) |
4074 PP_CONCAT_MASK(TOK_OTHER) /* tail */
4075 },
4076 {
4077 PP_CONCAT_MASK(TOK_NUMBER), /* head */
4078 PP_CONCAT_MASK(TOK_NUMBER) /* tail */
4079 }
4080 };
4081 paste_tokens(&thead, t, ARRAY_SIZE(t), false);
4082 }
H. Peter Anvin6125b622009-04-08 14:02:25 -07004083
H. Peter Anvin76690a12002-04-30 20:52:49 +00004084 return thead;
4085}
4086
4087/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004088 * Expand all single-line macro calls made in the given line.
4089 * Return the expanded version of the line. The original is deemed
4090 * to be destroyed in the process. (In reality we'll just move
4091 * Tokens from input to output a lot of the time, rather than
4092 * actually bothering to destroy and replicate.)
4093 */
H. Peter Anvincb1cf592007-11-19 12:26:50 -08004094
H. Peter Anvine2c80182005-01-15 22:15:51 +00004095static Token *expand_smacro(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004096{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004097 Token *t, *tt, *mstart, **tail, *thead;
H. Peter Anvineba20a72002-04-30 20:53:55 +00004098 SMacro *head = NULL, *m;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004099 Token **params;
4100 int *paramsize;
H. Peter Anvin25a99342007-09-22 17:45:45 -07004101 unsigned int nparam, sparam;
H. Peter Anvind784a082009-04-20 14:01:18 -07004102 int brackets;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004103 Token *org_tline = tline;
4104 Context *ctx;
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08004105 const char *mname;
H. Peter Anvin2a15e692007-11-19 13:14:59 -08004106 int deadman = DEADMAN_LIMIT;
H. Peter Anvin8287daf2009-07-07 16:00:58 -07004107 bool expanded;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004108
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004109 /*
4110 * Trick: we should avoid changing the start token pointer since it can
4111 * be contained in "next" field of other token. Because of this
4112 * we allocate a copy of first token and work with it; at the end of
4113 * routine we copy it back
4114 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004115 if (org_tline) {
Cyrill Gorcunoved4a8052010-04-09 15:40:35 +04004116 tline = new_Token(org_tline->next, org_tline->type,
4117 org_tline->text, 0);
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004118 tline->a.mac = org_tline->a.mac;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004119 nasm_free(org_tline->text);
4120 org_tline->text = NULL;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004121 }
4122
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004123 expanded = true; /* Always expand %+ at least once */
H. Peter Anvin8287daf2009-07-07 16:00:58 -07004124
H. Peter Anvincb1cf592007-11-19 12:26:50 -08004125again:
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004126 thead = NULL;
Cyrill Gorcunoved4a8052010-04-09 15:40:35 +04004127 tail = &thead;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004128
H. Peter Anvine2c80182005-01-15 22:15:51 +00004129 while (tline) { /* main token loop */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004130 if (!--deadman) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08004131 nasm_error(ERR_NONFATAL, "interminable macro recursion");
Cyrill Gorcunovbd38c8f2009-11-21 11:11:23 +03004132 goto err;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004133 }
H. Peter Anvincb1cf592007-11-19 12:26:50 -08004134
H. Peter Anvine2c80182005-01-15 22:15:51 +00004135 if ((mname = tline->text)) {
4136 /* if this token is a local macro, look in local context */
Cyrill Gorcunovc56d9ad2010-02-11 15:12:19 +03004137 if (tline->type == TOK_ID) {
4138 head = (SMacro *)hash_findix(&smacros, mname);
4139 } else if (tline->type == TOK_PREPROC_ID) {
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04004140 ctx = get_ctx(mname, &mname);
Cyrill Gorcunovc56d9ad2010-02-11 15:12:19 +03004141 head = ctx ? (SMacro *)hash_findix(&ctx->localmac, mname) : NULL;
4142 } else
4143 head = NULL;
H. Peter Anvin072771e2008-05-22 13:17:51 -07004144
H. Peter Anvine2c80182005-01-15 22:15:51 +00004145 /*
4146 * We've hit an identifier. As in is_mmacro below, we first
4147 * check whether the identifier is a single-line macro at
4148 * all, then think about checking for parameters if
4149 * necessary.
4150 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004151 list_for_each(m, head)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004152 if (!mstrcmp(m->name, mname, m->casesense))
4153 break;
4154 if (m) {
4155 mstart = tline;
4156 params = NULL;
4157 paramsize = NULL;
4158 if (m->nparam == 0) {
4159 /*
4160 * Simple case: the macro is parameterless. Discard the
4161 * one token that the macro call took, and push the
4162 * expansion back on the to-do stack.
4163 */
4164 if (!m->expansion) {
4165 if (!strcmp("__FILE__", m->name)) {
H. Peter Anvin274cda82016-05-10 02:56:29 -07004166 const char *file = src_get_fname();
4167 /* nasm_free(tline->text); here? */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004168 tline->text = nasm_quote(file, strlen(file));
4169 tline->type = TOK_STRING;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004170 continue;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004171 }
4172 if (!strcmp("__LINE__", m->name)) {
4173 nasm_free(tline->text);
4174 make_tok_num(tline, src_get_linnum());
4175 continue;
4176 }
4177 if (!strcmp("__BITS__", m->name)) {
4178 nasm_free(tline->text);
4179 make_tok_num(tline, globalbits);
4180 continue;
4181 }
4182 tline = delete_Token(tline);
4183 continue;
4184 }
4185 } else {
4186 /*
4187 * Complicated case: at least one macro with this name
H. Peter Anvine2c80182005-01-15 22:15:51 +00004188 * exists and takes parameters. We must find the
4189 * parameters in the call, count them, find the SMacro
4190 * that corresponds to that form of the macro call, and
4191 * substitute for the parameters when we expand. What a
4192 * pain.
4193 */
4194 /*tline = tline->next;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004195 skip_white_(tline); */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004196 do {
4197 t = tline->next;
4198 while (tok_type_(t, TOK_SMAC_END)) {
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004199 t->a.mac->in_progress = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004200 t->text = NULL;
4201 t = tline->next = delete_Token(t);
4202 }
4203 tline = t;
4204 } while (tok_type_(tline, TOK_WHITESPACE));
4205 if (!tok_is_(tline, "(")) {
4206 /*
4207 * This macro wasn't called with parameters: ignore
4208 * the call. (Behaviour borrowed from gnu cpp.)
4209 */
4210 tline = mstart;
4211 m = NULL;
4212 } else {
4213 int paren = 0;
4214 int white = 0;
4215 brackets = 0;
4216 nparam = 0;
4217 sparam = PARAM_DELTA;
4218 params = nasm_malloc(sparam * sizeof(Token *));
4219 params[0] = tline->next;
4220 paramsize = nasm_malloc(sparam * sizeof(int));
4221 paramsize[0] = 0;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07004222 while (true) { /* parameter loop */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004223 /*
4224 * For some unusual expansions
4225 * which concatenates function call
4226 */
4227 t = tline->next;
4228 while (tok_type_(t, TOK_SMAC_END)) {
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004229 t->a.mac->in_progress = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004230 t->text = NULL;
4231 t = tline->next = delete_Token(t);
4232 }
4233 tline = t;
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00004234
H. Peter Anvine2c80182005-01-15 22:15:51 +00004235 if (!tline) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08004236 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00004237 "macro call expects terminating `)'");
4238 break;
4239 }
4240 if (tline->type == TOK_WHITESPACE
4241 && brackets <= 0) {
4242 if (paramsize[nparam])
4243 white++;
4244 else
4245 params[nparam] = tline->next;
4246 continue; /* parameter loop */
4247 }
4248 if (tline->type == TOK_OTHER
4249 && tline->text[1] == 0) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004250 char ch = tline->text[0];
H. Peter Anvine2c80182005-01-15 22:15:51 +00004251 if (ch == ',' && !paren && brackets <= 0) {
4252 if (++nparam >= sparam) {
4253 sparam += PARAM_DELTA;
4254 params = nasm_realloc(params,
Cyrill Gorcunoved4a8052010-04-09 15:40:35 +04004255 sparam * sizeof(Token *));
4256 paramsize = nasm_realloc(paramsize,
4257 sparam * sizeof(int));
H. Peter Anvine2c80182005-01-15 22:15:51 +00004258 }
4259 params[nparam] = tline->next;
4260 paramsize[nparam] = 0;
4261 white = 0;
4262 continue; /* parameter loop */
4263 }
4264 if (ch == '{' &&
4265 (brackets > 0 || (brackets == 0 &&
4266 !paramsize[nparam])))
4267 {
4268 if (!(brackets++)) {
4269 params[nparam] = tline->next;
4270 continue; /* parameter loop */
4271 }
4272 }
4273 if (ch == '}' && brackets > 0)
4274 if (--brackets == 0) {
4275 brackets = -1;
4276 continue; /* parameter loop */
4277 }
4278 if (ch == '(' && !brackets)
4279 paren++;
4280 if (ch == ')' && brackets <= 0)
4281 if (--paren < 0)
4282 break;
4283 }
4284 if (brackets < 0) {
4285 brackets = 0;
H. Peter Anvin130736c2016-02-17 20:27:41 -08004286 nasm_error(ERR_NONFATAL, "braces do not "
H. Peter Anvine2c80182005-01-15 22:15:51 +00004287 "enclose all of macro parameter");
4288 }
4289 paramsize[nparam] += white + 1;
4290 white = 0;
4291 } /* parameter loop */
4292 nparam++;
4293 while (m && (m->nparam != nparam ||
4294 mstrcmp(m->name, mname,
4295 m->casesense)))
4296 m = m->next;
4297 if (!m)
H. Peter Anvin130736c2016-02-17 20:27:41 -08004298 nasm_error(ERR_WARNING|ERR_PASS1|ERR_WARN_MNP,
H. Peter Anvine2c80182005-01-15 22:15:51 +00004299 "macro `%s' exists, "
4300 "but not taking %d parameters",
4301 mstart->text, nparam);
4302 }
4303 }
4304 if (m && m->in_progress)
4305 m = NULL;
4306 if (!m) { /* in progess or didn't find '(' or wrong nparam */
H. Peter Anvin70653092007-10-19 14:42:29 -07004307 /*
H. Peter Anvine2c80182005-01-15 22:15:51 +00004308 * Design question: should we handle !tline, which
4309 * indicates missing ')' here, or expand those
4310 * macros anyway, which requires the (t) test a few
H. Peter Anvin70653092007-10-19 14:42:29 -07004311 * lines down?
H. Peter Anvine2c80182005-01-15 22:15:51 +00004312 */
4313 nasm_free(params);
4314 nasm_free(paramsize);
4315 tline = mstart;
4316 } else {
4317 /*
4318 * Expand the macro: we are placed on the last token of the
4319 * call, so that we can easily split the call from the
4320 * following tokens. We also start by pushing an SMAC_END
4321 * token for the cycle removal.
4322 */
4323 t = tline;
4324 if (t) {
4325 tline = t->next;
4326 t->next = NULL;
4327 }
4328 tt = new_Token(tline, TOK_SMAC_END, NULL, 0);
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004329 tt->a.mac = m;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07004330 m->in_progress = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004331 tline = tt;
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04004332 list_for_each(t, m->expansion) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004333 if (t->type >= TOK_SMAC_PARAM) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00004334 Token *pcopy = tline, **ptail = &pcopy;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004335 Token *ttt, *pt;
4336 int i;
H. Peter Anvin734b1882002-04-30 21:01:08 +00004337
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004338 ttt = params[t->type - TOK_SMAC_PARAM];
4339 i = paramsize[t->type - TOK_SMAC_PARAM];
4340 while (--i >= 0) {
4341 pt = *ptail = new_Token(tline, ttt->type,
4342 ttt->text, 0);
4343 ptail = &pt->next;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004344 ttt = ttt->next;
4345 }
4346 tline = pcopy;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004347 } else if (t->type == TOK_PREPROC_Q) {
4348 tt = new_Token(tline, TOK_ID, mname, 0);
4349 tline = tt;
4350 } else if (t->type == TOK_PREPROC_QQ) {
4351 tt = new_Token(tline, TOK_ID, m->name, 0);
4352 tline = tt;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004353 } else {
4354 tt = new_Token(tline, t->type, t->text, 0);
4355 tline = tt;
4356 }
4357 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00004358
H. Peter Anvine2c80182005-01-15 22:15:51 +00004359 /*
4360 * Having done that, get rid of the macro call, and clean
4361 * up the parameters.
4362 */
4363 nasm_free(params);
4364 nasm_free(paramsize);
4365 free_tlist(mstart);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004366 expanded = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004367 continue; /* main token loop */
4368 }
4369 }
4370 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004371
H. Peter Anvine2c80182005-01-15 22:15:51 +00004372 if (tline->type == TOK_SMAC_END) {
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004373 tline->a.mac->in_progress = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004374 tline = delete_Token(tline);
4375 } else {
4376 t = *tail = tline;
4377 tline = tline->next;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004378 t->a.mac = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004379 t->next = NULL;
4380 tail = &t->next;
4381 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004382 }
4383
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004384 /*
4385 * Now scan the entire line and look for successive TOK_IDs that resulted
Keith Kaniosb7a89542007-04-12 02:40:54 +00004386 * after expansion (they can't be produced by tokenize()). The successive
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004387 * TOK_IDs should be concatenated.
4388 * Also we look for %+ tokens and concatenate the tokens before and after
4389 * them (without white spaces in between).
4390 */
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +04004391 if (expanded) {
Cyrill Gorcunovc6a742c2011-06-27 01:23:09 +04004392 const struct tokseq_match t[] = {
4393 {
4394 PP_CONCAT_MASK(TOK_ID) |
4395 PP_CONCAT_MASK(TOK_PREPROC_ID), /* head */
4396 PP_CONCAT_MASK(TOK_ID) |
4397 PP_CONCAT_MASK(TOK_PREPROC_ID) |
4398 PP_CONCAT_MASK(TOK_NUMBER) /* tail */
4399 }
4400 };
4401 if (paste_tokens(&thead, t, ARRAY_SIZE(t), true)) {
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +04004402 /*
4403 * If we concatenated something, *and* we had previously expanded
4404 * an actual macro, scan the lines again for macros...
4405 */
4406 tline = thead;
4407 expanded = false;
4408 goto again;
4409 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00004410 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004411
Cyrill Gorcunovbd38c8f2009-11-21 11:11:23 +03004412err:
H. Peter Anvine2c80182005-01-15 22:15:51 +00004413 if (org_tline) {
4414 if (thead) {
4415 *org_tline = *thead;
4416 /* since we just gave text to org_line, don't free it */
4417 thead->text = NULL;
4418 delete_Token(thead);
4419 } else {
4420 /* the expression expanded to empty line;
4421 we can't return NULL for some reasons
4422 we just set the line to a single WHITESPACE token. */
4423 memset(org_tline, 0, sizeof(*org_tline));
4424 org_tline->text = NULL;
4425 org_tline->type = TOK_WHITESPACE;
4426 }
4427 thead = org_tline;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004428 }
4429
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004430 return thead;
4431}
4432
4433/*
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004434 * Similar to expand_smacro but used exclusively with macro identifiers
4435 * right before they are fetched in. The reason is that there can be
4436 * identifiers consisting of several subparts. We consider that if there
4437 * are more than one element forming the name, user wants a expansion,
4438 * otherwise it will be left as-is. Example:
4439 *
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004440 * %define %$abc cde
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004441 *
4442 * the identifier %$abc will be left as-is so that the handler for %define
4443 * will suck it and define the corresponding value. Other case:
4444 *
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004445 * %define _%$abc cde
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004446 *
4447 * In this case user wants name to be expanded *before* %define starts
4448 * working, so we'll expand %$abc into something (if it has a value;
4449 * otherwise it will be left as-is) then concatenate all successive
4450 * PP_IDs into one.
4451 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004452static Token *expand_id(Token * tline)
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004453{
4454 Token *cur, *oldnext = NULL;
4455
H. Peter Anvin734b1882002-04-30 21:01:08 +00004456 if (!tline || !tline->next)
H. Peter Anvine2c80182005-01-15 22:15:51 +00004457 return tline;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004458
4459 cur = tline;
4460 while (cur->next &&
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004461 (cur->next->type == TOK_ID ||
4462 cur->next->type == TOK_PREPROC_ID
4463 || cur->next->type == TOK_NUMBER))
H. Peter Anvine2c80182005-01-15 22:15:51 +00004464 cur = cur->next;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004465
4466 /* If identifier consists of just one token, don't expand */
4467 if (cur == tline)
H. Peter Anvine2c80182005-01-15 22:15:51 +00004468 return tline;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004469
H. Peter Anvine2c80182005-01-15 22:15:51 +00004470 if (cur) {
4471 oldnext = cur->next; /* Detach the tail past identifier */
4472 cur->next = NULL; /* so that expand_smacro stops here */
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004473 }
4474
H. Peter Anvin734b1882002-04-30 21:01:08 +00004475 tline = expand_smacro(tline);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004476
H. Peter Anvine2c80182005-01-15 22:15:51 +00004477 if (cur) {
4478 /* expand_smacro possibly changhed tline; re-scan for EOL */
4479 cur = tline;
4480 while (cur && cur->next)
4481 cur = cur->next;
4482 if (cur)
4483 cur->next = oldnext;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004484 }
4485
4486 return tline;
4487}
4488
4489/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004490 * Determine whether the given line constitutes a multi-line macro
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004491 * call, and return the MMacro structure called if so. Doesn't have
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004492 * to check for an initial label - that's taken care of in
4493 * expand_mmacro - but must check numbers of parameters. Guaranteed
4494 * to be called with tline->type == TOK_ID, so the putative macro
4495 * name is easy to find.
4496 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004497static MMacro *is_mmacro(Token * tline, Token *** params_array)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004498{
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004499 MMacro *head, *m;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004500 Token **params;
4501 int nparam;
4502
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004503 head = (MMacro *) hash_findix(&mmacros, tline->text);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004504
4505 /*
4506 * Efficiency: first we see if any macro exists with the given
4507 * name. If not, we can return NULL immediately. _Then_ we
4508 * count the parameters, and then we look further along the
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004509 * list if necessary to find the proper MMacro.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004510 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004511 list_for_each(m, head)
4512 if (!mstrcmp(m->name, tline->text, m->casesense))
H. Peter Anvine2c80182005-01-15 22:15:51 +00004513 break;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004514 if (!m)
H. Peter Anvine2c80182005-01-15 22:15:51 +00004515 return NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004516
4517 /*
4518 * OK, we have a potential macro. Count and demarcate the
4519 * parameters.
4520 */
H. Peter Anvin734b1882002-04-30 21:01:08 +00004521 count_mmac_params(tline->next, &nparam, &params);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004522
4523 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004524 * So we know how many parameters we've got. Find the MMacro
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004525 * structure that handles this number.
4526 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004527 while (m) {
4528 if (m->nparam_min <= nparam
4529 && (m->plus || nparam <= m->nparam_max)) {
4530 /*
4531 * This one is right. Just check if cycle removal
4532 * prohibits us using it before we actually celebrate...
4533 */
4534 if (m->in_progress > m->max_depth) {
4535 if (m->max_depth > 0) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08004536 nasm_error(ERR_WARNING,
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004537 "reached maximum recursion depth of %i",
4538 m->max_depth);
4539 }
4540 nasm_free(params);
4541 return NULL;
4542 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00004543 /*
4544 * It's right, and we can use it. Add its default
4545 * parameters to the end of our list if necessary.
4546 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004547 if (m->defaults && nparam < m->nparam_min + m->ndefs) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00004548 params =
4549 nasm_realloc(params,
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004550 ((m->nparam_min + m->ndefs +
H. Peter Anvine2c80182005-01-15 22:15:51 +00004551 1) * sizeof(*params)));
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004552 while (nparam < m->nparam_min + m->ndefs) {
4553 params[nparam] = m->defaults[nparam - m->nparam_min];
H. Peter Anvine2c80182005-01-15 22:15:51 +00004554 nparam++;
4555 }
4556 }
4557 /*
4558 * If we've gone over the maximum parameter count (and
4559 * we're in Plus mode), ignore parameters beyond
4560 * nparam_max.
4561 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004562 if (m->plus && nparam > m->nparam_max)
4563 nparam = m->nparam_max;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004564 /*
4565 * Then terminate the parameter list, and leave.
4566 */
4567 if (!params) { /* need this special case */
4568 params = nasm_malloc(sizeof(*params));
4569 nparam = 0;
4570 }
4571 params[nparam] = NULL;
4572 *params_array = params;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004573 return m;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004574 }
4575 /*
4576 * This one wasn't right: look for the next one with the
4577 * same name.
4578 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004579 list_for_each(m, m->next)
4580 if (!mstrcmp(m->name, tline->text, m->casesense))
H. Peter Anvine2c80182005-01-15 22:15:51 +00004581 break;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004582 }
4583
4584 /*
4585 * After all that, we didn't find one with the right number of
4586 * parameters. Issue a warning, and fail to expand the macro.
4587 */
H. Peter Anvin130736c2016-02-17 20:27:41 -08004588 nasm_error(ERR_WARNING|ERR_PASS1|ERR_WARN_MNP,
H. Peter Anvine2c80182005-01-15 22:15:51 +00004589 "macro `%s' exists, but not taking %d parameters",
4590 tline->text, nparam);
H. Peter Anvin734b1882002-04-30 21:01:08 +00004591 nasm_free(params);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004592 return NULL;
4593}
4594
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004595
4596/*
4597 * Save MMacro invocation specific fields in
4598 * preparation for a recursive macro expansion
4599 */
4600static void push_mmacro(MMacro *m)
4601{
4602 MMacroInvocation *i;
4603
4604 i = nasm_malloc(sizeof(MMacroInvocation));
4605 i->prev = m->prev;
4606 i->params = m->params;
4607 i->iline = m->iline;
4608 i->nparam = m->nparam;
4609 i->rotate = m->rotate;
4610 i->paramlen = m->paramlen;
4611 i->unique = m->unique;
4612 i->condcnt = m->condcnt;
4613 m->prev = i;
4614}
4615
4616
4617/*
4618 * Restore MMacro invocation specific fields that were
4619 * saved during a previous recursive macro expansion
4620 */
4621static void pop_mmacro(MMacro *m)
4622{
4623 MMacroInvocation *i;
4624
4625 if (m->prev) {
4626 i = m->prev;
4627 m->prev = i->prev;
4628 m->params = i->params;
4629 m->iline = i->iline;
4630 m->nparam = i->nparam;
4631 m->rotate = i->rotate;
4632 m->paramlen = i->paramlen;
4633 m->unique = i->unique;
4634 m->condcnt = i->condcnt;
4635 nasm_free(i);
4636 }
4637}
4638
4639
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004640/*
4641 * Expand the multi-line macro call made by the given line, if
4642 * there is one to be expanded. If there is, push the expansion on
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004643 * istk->expansion and return 1. Otherwise return 0.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004644 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004645static int expand_mmacro(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004646{
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004647 Token *startline = tline;
H. Peter Anvineba20a72002-04-30 20:53:55 +00004648 Token *label = NULL;
4649 int dont_prepend = 0;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004650 Token **params, *t, *tt;
4651 MMacro *m;
4652 Line *l, *ll;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004653 int i, nparam, *paramlen;
H. Peter Anvinc751e862008-06-09 10:18:45 -07004654 const char *mname;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004655
4656 t = tline;
H. Peter Anvineba20a72002-04-30 20:53:55 +00004657 skip_white_(t);
H. Peter Anvince2233b2008-05-25 21:57:00 -07004658 /* if (!tok_type_(t, TOK_ID)) Lino 02/25/02 */
H. Peter Anvindce1e2f2002-04-30 21:06:37 +00004659 if (!tok_type_(t, TOK_ID) && !tok_type_(t, TOK_PREPROC_ID))
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004660 return 0;
4661 m = is_mmacro(t, &params);
4662 if (m) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004663 mname = t->text;
H. Peter Anvinc751e862008-06-09 10:18:45 -07004664 } else {
H. Peter Anvine2c80182005-01-15 22:15:51 +00004665 Token *last;
4666 /*
4667 * We have an id which isn't a macro call. We'll assume
4668 * it might be a label; we'll also check to see if a
4669 * colon follows it. Then, if there's another id after
4670 * that lot, we'll check it again for macro-hood.
4671 */
4672 label = last = t;
4673 t = t->next;
4674 if (tok_type_(t, TOK_WHITESPACE))
4675 last = t, t = t->next;
4676 if (tok_is_(t, ":")) {
4677 dont_prepend = 1;
4678 last = t, t = t->next;
4679 if (tok_type_(t, TOK_WHITESPACE))
4680 last = t, t = t->next;
4681 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004682 if (!tok_type_(t, TOK_ID) || !(m = is_mmacro(t, &params)))
4683 return 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004684 last->next = NULL;
Keith Kanios891775e2009-07-11 06:08:54 -05004685 mname = t->text;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004686 tline = t;
H. Peter Anvineba20a72002-04-30 20:53:55 +00004687 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004688
4689 /*
4690 * Fix up the parameters: this involves stripping leading and
4691 * trailing whitespace, then stripping braces if they are
4692 * present.
4693 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004694 for (nparam = 0; params[nparam]; nparam++) ;
H. Peter Anvin734b1882002-04-30 21:01:08 +00004695 paramlen = nparam ? nasm_malloc(nparam * sizeof(*paramlen)) : NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004696
H. Peter Anvine2c80182005-01-15 22:15:51 +00004697 for (i = 0; params[i]; i++) {
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08004698 int brace = 0;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004699 int comma = (!m->plus || i < nparam - 1);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004700
H. Peter Anvine2c80182005-01-15 22:15:51 +00004701 t = params[i];
4702 skip_white_(t);
4703 if (tok_is_(t, "{"))
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08004704 t = t->next, brace++, comma = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004705 params[i] = t;
4706 paramlen[i] = 0;
4707 while (t) {
4708 if (comma && t->type == TOK_OTHER && !strcmp(t->text, ","))
4709 break; /* ... because we have hit a comma */
4710 if (comma && t->type == TOK_WHITESPACE
4711 && tok_is_(t->next, ","))
4712 break; /* ... or a space then a comma */
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08004713 if (brace && t->type == TOK_OTHER) {
4714 if (t->text[0] == '{')
4715 brace++; /* ... or a nested opening brace */
4716 else if (t->text[0] == '}')
4717 if (!--brace)
4718 break; /* ... or a brace */
4719 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00004720 t = t->next;
4721 paramlen[i]++;
4722 }
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08004723 if (brace)
H. Peter Anvin130736c2016-02-17 20:27:41 -08004724 nasm_error(ERR_NONFATAL, "macro params should be enclosed in braces");
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004725 }
4726
4727 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004728 * OK, we have a MMacro structure together with a set of
4729 * parameters. We must now go through the expansion and push
4730 * copies of each Line on to istk->expansion. Substitution of
H. Peter Anvin76690a12002-04-30 20:52:49 +00004731 * parameter tokens and macro-local tokens doesn't get done
4732 * until the single-line macro substitution process; this is
4733 * because delaying them allows us to change the semantics
4734 * later through %rotate.
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004735 *
4736 * First, push an end marker on to istk->expansion, mark this
4737 * macro as in progress, and set up its invocation-specific
4738 * variables.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004739 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004740 ll = nasm_malloc(sizeof(Line));
4741 ll->next = istk->expansion;
4742 ll->finishes = m;
4743 ll->first = NULL;
4744 istk->expansion = ll;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004745
4746 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004747 * Save the previous MMacro expansion in the case of
4748 * macro recursion
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004749 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004750 if (m->max_depth && m->in_progress)
4751 push_mmacro(m);
4752
4753 m->in_progress ++;
4754 m->params = params;
4755 m->iline = tline;
4756 m->nparam = nparam;
4757 m->rotate = 0;
4758 m->paramlen = paramlen;
4759 m->unique = unique++;
4760 m->lineno = 0;
4761 m->condcnt = 0;
4762
4763 m->next_active = istk->mstk;
4764 istk->mstk = m;
4765
4766 list_for_each(l, m->expansion) {
4767 Token **tail;
4768
4769 ll = nasm_malloc(sizeof(Line));
4770 ll->finishes = NULL;
4771 ll->next = istk->expansion;
4772 istk->expansion = ll;
4773 tail = &ll->first;
4774
4775 list_for_each(t, l->first) {
4776 Token *x = t;
4777 switch (t->type) {
4778 case TOK_PREPROC_Q:
4779 tt = *tail = new_Token(NULL, TOK_ID, mname, 0);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004780 break;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004781 case TOK_PREPROC_QQ:
4782 tt = *tail = new_Token(NULL, TOK_ID, m->name, 0);
4783 break;
4784 case TOK_PREPROC_ID:
4785 if (t->text[1] == '0' && t->text[2] == '0') {
4786 dont_prepend = -1;
4787 x = label;
4788 if (!x)
4789 continue;
4790 }
4791 /* fall through */
4792 default:
4793 tt = *tail = new_Token(NULL, x->type, x->text, 0);
4794 break;
4795 }
4796 tail = &tt->next;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004797 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004798 *tail = NULL;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004799 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004800
4801 /*
H. Peter Anvineba20a72002-04-30 20:53:55 +00004802 * If we had a label, push it on as the first line of
4803 * the macro expansion.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004804 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004805 if (label) {
4806 if (dont_prepend < 0)
4807 free_tlist(startline);
4808 else {
4809 ll = nasm_malloc(sizeof(Line));
4810 ll->finishes = NULL;
4811 ll->next = istk->expansion;
4812 istk->expansion = ll;
4813 ll->first = startline;
4814 if (!dont_prepend) {
4815 while (label->next)
4816 label = label->next;
4817 label->next = tt = new_Token(NULL, TOK_OTHER, ":", 0);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004818 }
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004819 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004820 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004821
H. Peter Anvin8ac25aa2016-02-18 01:16:18 -08004822 lfmt->uplevel(m->nolist ? LIST_MACRO_NOLIST : LIST_MACRO);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004823
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004824 return 1;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004825}
4826
H. Peter Anvin130736c2016-02-17 20:27:41 -08004827/*
4828 * This function adds macro names to error messages, and suppresses
4829 * them if necessary.
4830 */
4831static void pp_verror(int severity, const char *fmt, va_list arg)
Victor van den Elzen3b404c02008-09-18 13:51:36 +02004832{
H. Peter Anvin130736c2016-02-17 20:27:41 -08004833 char buff[BUFSIZ];
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004834 MMacro *mmac = NULL;
4835 int delta = 0;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02004836
H. Peter Anvin130736c2016-02-17 20:27:41 -08004837 /*
4838 * If we're in a dead branch of IF or something like it, ignore the error.
4839 * However, because %else etc are evaluated in the state context
4840 * of the previous branch, errors might get lost:
4841 * %if 0 ... %else trailing garbage ... %endif
4842 * So %else etc should set the ERR_PP_PRECOND flag.
4843 */
4844 if ((severity & ERR_MASK) < ERR_FATAL &&
4845 istk && istk->conds &&
4846 ((severity & ERR_PP_PRECOND) ?
4847 istk->conds->state == COND_NEVER :
H. Peter Anvineb6653f2016-04-05 13:03:10 -07004848 !emitting(istk->conds->state)))
H. Peter Anvin130736c2016-02-17 20:27:41 -08004849 return;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02004850
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004851 /* get %macro name */
H. Peter Anvin130736c2016-02-17 20:27:41 -08004852 if (!(severity & ERR_NOFILE) && istk && istk->mstk) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004853 mmac = istk->mstk;
4854 /* but %rep blocks should be skipped */
4855 while (mmac && !mmac->name)
4856 mmac = mmac->next_active, delta++;
Cyrill Gorcunov9900c6b2011-10-09 18:58:46 +04004857 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004858
H. Peter Anvin130736c2016-02-17 20:27:41 -08004859 if (mmac) {
4860 vsnprintf(buff, sizeof(buff), fmt, arg);
Victor van den Elzen3b404c02008-09-18 13:51:36 +02004861
H. Peter Anvin130736c2016-02-17 20:27:41 -08004862 nasm_set_verror(real_verror);
4863 nasm_error(severity, "(%s:%d) %s",
4864 mmac->name, mmac->lineno - delta, buff);
4865 nasm_set_verror(pp_verror);
4866 } else {
4867 real_verror(severity, fmt, arg);
4868 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004869}
4870
H. Peter Anvin734b1882002-04-30 21:01:08 +00004871static void
H. Peter Anvin130736c2016-02-17 20:27:41 -08004872pp_reset(char *file, int apass, StrList **deplist)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004873{
H. Peter Anvin7383b402008-09-24 10:20:40 -07004874 Token *t;
4875
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004876 cstk = NULL;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004877 istk = nasm_malloc(sizeof(Include));
4878 istk->next = NULL;
4879 istk->conds = NULL;
4880 istk->expansion = NULL;
4881 istk->mstk = NULL;
H. Peter Anvin3e83cec2016-05-25 04:28:46 -07004882 istk->fp = nasm_open_read(file, NF_TEXT);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004883 istk->fname = NULL;
H. Peter Anvin274cda82016-05-10 02:56:29 -07004884 src_set(0, file);
H. Peter Anvineba20a72002-04-30 20:53:55 +00004885 istk->lineinc = 1;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004886 if (!istk->fp)
H. Peter Anvin130736c2016-02-17 20:27:41 -08004887 nasm_fatal(ERR_NOFILE, "unable to open input file `%s'", file);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004888 defining = NULL;
Charles Crayned4200be2008-07-12 16:42:33 -07004889 nested_mac_count = 0;
4890 nested_rep_count = 0;
H. Peter Anvin97a23472007-09-16 17:57:25 -07004891 init_macros();
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004892 unique = 0;
H. Peter Anvinf7606612016-07-13 14:23:48 -07004893
4894 if (tasm_compatible_mode)
4895 pp_add_stdmac(nasm_stdmac_tasm);
4896
4897 pp_add_stdmac(nasm_stdmac_nasm);
4898 pp_add_stdmac(nasm_stdmac_version);
4899
4900 stdmacpos = stdmacros[0];
4901 stdmacnext = &stdmacros[1];
4902
H. Peter Anvind2456592008-06-19 15:04:18 -07004903 do_predef = true;
H. Peter Anvin61f130f2008-09-25 15:45:06 -07004904
4905 /*
4906 * 0 for dependencies, 1 for preparatory passes, 2 for final pass.
4907 * The caller, however, will also pass in 3 for preprocess-only so
4908 * we can set __PASS__ accordingly.
4909 */
4910 pass = apass > 2 ? 2 : apass;
4911
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07004912 dephead = deptail = deplist;
4913 if (deplist) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004914 StrList *sl = nasm_malloc(strlen(file)+1+sizeof sl->next);
4915 sl->next = NULL;
4916 strcpy(sl->str, file);
4917 *deptail = sl;
4918 deptail = &sl->next;
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07004919 }
H. Peter Anvin7383b402008-09-24 10:20:40 -07004920
H. Peter Anvin61f130f2008-09-25 15:45:06 -07004921 /*
4922 * Define the __PASS__ macro. This is defined here unlike
4923 * all the other builtins, because it is special -- it varies between
4924 * passes.
4925 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004926 t = nasm_malloc(sizeof(*t));
4927 t->next = NULL;
H. Peter Anvin61f130f2008-09-25 15:45:06 -07004928 make_tok_num(t, apass);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004929 t->a.mac = NULL;
H. Peter Anvin7383b402008-09-24 10:20:40 -07004930 define_smacro(NULL, "__PASS__", true, 0, t);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004931}
4932
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004933static char *pp_getline(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004934{
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004935 char *line;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004936 Token *tline;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004937
H. Peter Anvin130736c2016-02-17 20:27:41 -08004938 real_verror = nasm_set_verror(pp_verror);
H. Peter Anvin215186f2016-02-17 20:27:41 -08004939
H. Peter Anvine2c80182005-01-15 22:15:51 +00004940 while (1) {
4941 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004942 * Fetch a tokenized line, either from the macro-expansion
H. Peter Anvine2c80182005-01-15 22:15:51 +00004943 * buffer or from the input file.
4944 */
4945 tline = NULL;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004946 while (istk->expansion && istk->expansion->finishes) {
4947 Line *l = istk->expansion;
4948 if (!l->finishes->name && l->finishes->in_progress > 1) {
4949 Line *ll;
H. Peter Anvineba20a72002-04-30 20:53:55 +00004950
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004951 /*
4952 * This is a macro-end marker for a macro with no
4953 * name, which means it's not really a macro at all
4954 * but a %rep block, and the `in_progress' field is
4955 * more than 1, meaning that we still need to
4956 * repeat. (1 means the natural last repetition; 0
4957 * means termination by %exitrep.) We have
4958 * therefore expanded up to the %endrep, and must
4959 * push the whole block on to the expansion buffer
4960 * again. We don't bother to remove the macro-end
4961 * marker: we'd only have to generate another one
4962 * if we did.
4963 */
4964 l->finishes->in_progress--;
4965 list_for_each(l, l->finishes->expansion) {
4966 Token *t, *tt, **tail;
4967
4968 ll = nasm_malloc(sizeof(Line));
4969 ll->next = istk->expansion;
4970 ll->finishes = NULL;
4971 ll->first = NULL;
4972 tail = &ll->first;
4973
4974 list_for_each(t, l->first) {
4975 if (t->text || t->type == TOK_WHITESPACE) {
4976 tt = *tail = new_Token(NULL, t->type, t->text, 0);
4977 tail = &tt->next;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004978 }
4979 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004980
4981 istk->expansion = ll;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004982 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004983 } else {
4984 /*
4985 * Check whether a `%rep' was started and not ended
4986 * within this macro expansion. This can happen and
4987 * should be detected. It's a fatal error because
4988 * I'm too confused to work out how to recover
4989 * sensibly from it.
4990 */
4991 if (defining) {
4992 if (defining->name)
H. Peter Anvin130736c2016-02-17 20:27:41 -08004993 nasm_panic(0, "defining with name in expansion");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004994 else if (istk->mstk->name)
H. Peter Anvin130736c2016-02-17 20:27:41 -08004995 nasm_fatal(0, "`%%rep' without `%%endrep' within"
4996 " expansion of macro `%s'",
4997 istk->mstk->name);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004998 }
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004999
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005000 /*
5001 * FIXME: investigate the relationship at this point between
5002 * istk->mstk and l->finishes
5003 */
5004 {
5005 MMacro *m = istk->mstk;
5006 istk->mstk = m->next_active;
5007 if (m->name) {
5008 /*
5009 * This was a real macro call, not a %rep, and
5010 * therefore the parameter information needs to
5011 * be freed.
5012 */
5013 if (m->prev) {
5014 pop_mmacro(m);
5015 l->finishes->in_progress --;
5016 } else {
5017 nasm_free(m->params);
5018 free_tlist(m->iline);
5019 nasm_free(m->paramlen);
5020 l->finishes->in_progress = 0;
5021 }
5022 } else
5023 free_mmacro(m);
5024 }
5025 istk->expansion = l->next;
5026 nasm_free(l);
H. Peter Anvin8ac25aa2016-02-18 01:16:18 -08005027 lfmt->downlevel(LIST_MACRO);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005028 }
5029 }
5030 while (1) { /* until we get a line we can use */
5031
5032 if (istk->expansion) { /* from a macro expansion */
5033 char *p;
5034 Line *l = istk->expansion;
5035 if (istk->mstk)
5036 istk->mstk->lineno++;
5037 tline = l->first;
5038 istk->expansion = l->next;
5039 nasm_free(l);
5040 p = detoken(tline, false);
H. Peter Anvin8ac25aa2016-02-18 01:16:18 -08005041 lfmt->line(LIST_MACRO, p);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005042 nasm_free(p);
5043 break;
5044 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00005045 line = read_line();
5046 if (line) { /* from the current input file */
5047 line = prepreproc(line);
Keith Kaniosb7a89542007-04-12 02:40:54 +00005048 tline = tokenize(line);
H. Peter Anvine2c80182005-01-15 22:15:51 +00005049 nasm_free(line);
5050 break;
5051 }
5052 /*
5053 * The current file has ended; work down the istk
5054 */
5055 {
5056 Include *i = istk;
5057 fclose(i->fp);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005058 if (i->conds) {
5059 /* nasm_error can't be conditionally suppressed */
H. Peter Anvin41087062016-03-03 14:27:34 -08005060 nasm_fatal(0,
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005061 "expected `%%endif' before end of file");
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005062 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00005063 /* only set line and file name if there's a next node */
H. Peter Anvin274cda82016-05-10 02:56:29 -07005064 if (i->next)
5065 src_set(i->lineno, i->fname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00005066 istk = i->next;
H. Peter Anvin8ac25aa2016-02-18 01:16:18 -08005067 lfmt->downlevel(LIST_INCLUDE);
H. Peter Anvine2c80182005-01-15 22:15:51 +00005068 nasm_free(i);
H. Peter Anvin130736c2016-02-17 20:27:41 -08005069 if (!istk) {
5070 line = NULL;
5071 goto done;
5072 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005073 if (istk->expansion && istk->expansion->finishes)
5074 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005075 }
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005076 }
5077
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005078 /*
5079 * We must expand MMacro parameters and MMacro-local labels
5080 * _before_ we plunge into directive processing, to cope
5081 * with things like `%define something %1' such as STRUC
5082 * uses. Unless we're _defining_ a MMacro, in which case
5083 * those tokens should be left alone to go into the
5084 * definition; and unless we're in a non-emitting
5085 * condition, in which case we don't want to meddle with
5086 * anything.
5087 */
5088 if (!defining && !(istk->conds && !emitting(istk->conds->state))
5089 && !(istk->mstk && !istk->mstk->in_progress)) {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005090 tline = expand_mmac_params(tline);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005091 }
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005092
H. Peter Anvine2c80182005-01-15 22:15:51 +00005093 /*
5094 * Check the line to see if it's a preprocessor directive.
5095 */
5096 if (do_directive(tline) == DIRECTIVE_FOUND) {
5097 continue;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005098 } else if (defining) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00005099 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005100 * We're defining a multi-line macro. We emit nothing
5101 * at all, and just
5102 * shove the tokenized line on to the macro definition.
H. Peter Anvine2c80182005-01-15 22:15:51 +00005103 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005104 Line *l = nasm_malloc(sizeof(Line));
5105 l->next = defining->expansion;
5106 l->first = tline;
5107 l->finishes = NULL;
5108 defining->expansion = l;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005109 continue;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005110 } else if (istk->conds && !emitting(istk->conds->state)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00005111 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005112 * We're in a non-emitting branch of a condition block.
H. Peter Anvine2c80182005-01-15 22:15:51 +00005113 * Emit nothing at all, not even a blank line: when we
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005114 * emerge from the condition we'll give a line-number
H. Peter Anvine2c80182005-01-15 22:15:51 +00005115 * directive so we keep our place correctly.
5116 */
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005117 free_tlist(tline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00005118 continue;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005119 } else if (istk->mstk && !istk->mstk->in_progress) {
5120 /*
5121 * We're in a %rep block which has been terminated, so
5122 * we're walking through to the %endrep without
5123 * emitting anything. Emit nothing at all, not even a
5124 * blank line: when we emerge from the %rep block we'll
5125 * give a line-number directive so we keep our place
5126 * correctly.
5127 */
5128 free_tlist(tline);
5129 continue;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005130 } else {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005131 tline = expand_smacro(tline);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005132 if (!expand_mmacro(tline)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00005133 /*
Keith Kaniosb7a89542007-04-12 02:40:54 +00005134 * De-tokenize the line again, and emit it.
H. Peter Anvine2c80182005-01-15 22:15:51 +00005135 */
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005136 line = detoken(tline, true);
5137 free_tlist(tline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00005138 break;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005139 } else {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005140 continue; /* expand_mmacro calls free_tlist */
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005141 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00005142 }
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005143 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005144
H. Peter Anvin130736c2016-02-17 20:27:41 -08005145done:
5146 nasm_set_verror(real_verror);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005147 return line;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005148}
5149
H. Peter Anvine2c80182005-01-15 22:15:51 +00005150static void pp_cleanup(int pass)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005151{
H. Peter Anvin130736c2016-02-17 20:27:41 -08005152 real_verror = nasm_set_verror(pp_verror);
H. Peter Anvin215186f2016-02-17 20:27:41 -08005153
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005154 if (defining) {
5155 if (defining->name) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08005156 nasm_error(ERR_NONFATAL,
5157 "end of file while still defining macro `%s'",
5158 defining->name);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005159 } else {
H. Peter Anvin130736c2016-02-17 20:27:41 -08005160 nasm_error(ERR_NONFATAL, "end of file while still in %%rep");
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005161 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005162
5163 free_mmacro(defining);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005164 defining = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005165 }
H. Peter Anvin130736c2016-02-17 20:27:41 -08005166
5167 nasm_set_verror(real_verror);
H. Peter Anvin215186f2016-02-17 20:27:41 -08005168
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005169 while (cstk)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005170 ctx_pop();
H. Peter Anvin97a23472007-09-16 17:57:25 -07005171 free_macros();
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005172 while (istk) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00005173 Include *i = istk;
5174 istk = istk->next;
5175 fclose(i->fp);
Cyrill Gorcunov8dcfd882011-03-03 09:18:56 +03005176 nasm_free(i);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005177 }
5178 while (cstk)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005179 ctx_pop();
H. Peter Anvin274cda82016-05-10 02:56:29 -07005180 src_set_fname(NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00005181 if (pass == 0) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03005182 IncPath *i;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005183 free_llist(predef);
Cyrill Gorcunovdae24d72014-06-28 10:17:39 +04005184 predef = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005185 delete_Blocks();
Cyrill Gorcunovdae24d72014-06-28 10:17:39 +04005186 freeTokens = NULL;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03005187 while ((i = ipath)) {
5188 ipath = i->next;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005189 if (i->path)
5190 nasm_free(i->path);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03005191 nasm_free(i);
5192 }
H. Peter Anvin11dfa1a2008-07-02 18:11:04 -07005193 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005194}
5195
Cyrill Gorcunov0b78bff2012-05-07 01:57:55 +04005196static void pp_include_path(char *path)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005197{
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005198 IncPath *i;
H. Peter Anvin37a321f2007-09-24 13:41:58 -07005199
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005200 i = nasm_malloc(sizeof(IncPath));
5201 i->path = path ? nasm_strdup(path) : NULL;
5202 i->next = NULL;
Frank Kotlerd0ed6fd2003-08-27 11:33:56 +00005203
H. Peter Anvin89cee572009-07-15 09:16:54 -04005204 if (ipath) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00005205 IncPath *j = ipath;
H. Peter Anvin89cee572009-07-15 09:16:54 -04005206 while (j->next)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005207 j = j->next;
5208 j->next = i;
5209 } else {
5210 ipath = i;
Frank Kotlerd0ed6fd2003-08-27 11:33:56 +00005211 }
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005212}
Frank Kotlerd0ed6fd2003-08-27 11:33:56 +00005213
Cyrill Gorcunov0b78bff2012-05-07 01:57:55 +04005214static void pp_pre_include(char *fname)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005215{
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005216 Token *inc, *space, *name;
5217 Line *l;
5218
H. Peter Anvin734b1882002-04-30 21:01:08 +00005219 name = new_Token(NULL, TOK_INTERNAL_STRING, fname, 0);
5220 space = new_Token(name, TOK_WHITESPACE, NULL, 0);
5221 inc = new_Token(space, TOK_PREPROC_ID, "%include", 0);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005222
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005223 l = nasm_malloc(sizeof(Line));
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005224 l->next = predef;
5225 l->first = inc;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005226 l->finishes = NULL;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005227 predef = l;
5228}
5229
Cyrill Gorcunov0b78bff2012-05-07 01:57:55 +04005230static void pp_pre_define(char *definition)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005231{
5232 Token *def, *space;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005233 Line *l;
Keith Kaniosa6dfa782007-04-13 16:47:53 +00005234 char *equals;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005235
H. Peter Anvin130736c2016-02-17 20:27:41 -08005236 real_verror = nasm_set_verror(pp_verror);
H. Peter Anvin215186f2016-02-17 20:27:41 -08005237
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005238 equals = strchr(definition, '=');
H. Peter Anvin734b1882002-04-30 21:01:08 +00005239 space = new_Token(NULL, TOK_WHITESPACE, NULL, 0);
5240 def = new_Token(space, TOK_PREPROC_ID, "%define", 0);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005241 if (equals)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005242 *equals = ' ';
Keith Kaniosb7a89542007-04-12 02:40:54 +00005243 space->next = tokenize(definition);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005244 if (equals)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005245 *equals = '=';
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005246
Cyrill Gorcunov6d42e9b2015-02-08 11:07:17 +03005247 if (space->next->type != TOK_PREPROC_ID &&
5248 space->next->type != TOK_ID)
H. Peter Anvin130736c2016-02-17 20:27:41 -08005249 nasm_error(ERR_WARNING, "pre-defining non ID `%s\'\n", definition);
Cyrill Gorcunov6d42e9b2015-02-08 11:07:17 +03005250
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005251 l = nasm_malloc(sizeof(Line));
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005252 l->next = predef;
5253 l->first = def;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005254 l->finishes = NULL;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005255 predef = l;
H. Peter Anvin130736c2016-02-17 20:27:41 -08005256
5257 nasm_set_verror(real_verror);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005258}
5259
Cyrill Gorcunov0b78bff2012-05-07 01:57:55 +04005260static void pp_pre_undefine(char *definition)
H. Peter Anvin620515a2002-04-30 20:57:38 +00005261{
5262 Token *def, *space;
5263 Line *l;
5264
H. Peter Anvin734b1882002-04-30 21:01:08 +00005265 space = new_Token(NULL, TOK_WHITESPACE, NULL, 0);
5266 def = new_Token(space, TOK_PREPROC_ID, "%undef", 0);
Keith Kaniosb7a89542007-04-12 02:40:54 +00005267 space->next = tokenize(definition);
H. Peter Anvin620515a2002-04-30 20:57:38 +00005268
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005269 l = nasm_malloc(sizeof(Line));
H. Peter Anvin620515a2002-04-30 20:57:38 +00005270 l->next = predef;
5271 l->first = def;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005272 l->finishes = NULL;
H. Peter Anvin620515a2002-04-30 20:57:38 +00005273 predef = l;
5274}
5275
H. Peter Anvinf7606612016-07-13 14:23:48 -07005276static void pp_add_stdmac(macros_t *macros)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005277{
H. Peter Anvinf7606612016-07-13 14:23:48 -07005278 macros_t **mp;
5279
5280 /* Find the end of the list and avoid duplicates */
5281 for (mp = stdmacros; *mp; mp++) {
5282 if (*mp == macros)
5283 return; /* Nothing to do */
5284 }
5285
5286 nasm_assert(mp < &stdmacros[ARRAY_SIZE(stdmacros)-1]);
5287
5288 *mp = macros;
H. Peter Anvin76690a12002-04-30 20:52:49 +00005289}
5290
Keith Kaniosa5fc6462007-10-13 07:09:22 -07005291static void make_tok_num(Token * tok, int64_t val)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005292{
Cyrill Gorcunovce652742013-05-06 23:43:43 +04005293 char numbuf[32];
Keith Kaniosa5fc6462007-10-13 07:09:22 -07005294 snprintf(numbuf, sizeof(numbuf), "%"PRId64"", val);
H. Peter Anvineba20a72002-04-30 20:53:55 +00005295 tok->text = nasm_strdup(numbuf);
5296 tok->type = TOK_NUMBER;
5297}
5298
H. Peter Anvin37368952016-05-09 14:10:32 -07005299static void pp_list_one_macro(MMacro *m, int severity)
5300{
5301 if (!m)
5302 return;
5303
5304 /* We need to print the next_active list in reverse order */
5305 pp_list_one_macro(m->next_active, severity);
5306
5307 if (m->name && !m->nolist) {
H. Peter Anvin274cda82016-05-10 02:56:29 -07005308 src_set(m->xline + m->lineno, m->fname);
H. Peter Anvin37368952016-05-09 14:10:32 -07005309 nasm_error(severity, "... from macro `%s' defined here", m->name);
5310 }
5311}
5312
H. Peter Anvin4def1a82016-05-09 13:59:44 -07005313static void pp_error_list_macros(int severity)
5314{
H. Peter Anvin4def1a82016-05-09 13:59:44 -07005315 int32_t saved_line;
5316 const char *saved_fname = NULL;
5317
5318 severity |= ERR_PP_LISTMACRO | ERR_NO_SEVERITY;
H. Peter Anvin274cda82016-05-10 02:56:29 -07005319 src_get(&saved_line, &saved_fname);
H. Peter Anvin4def1a82016-05-09 13:59:44 -07005320
Cyrill Gorcunov771d04e2016-05-10 23:27:03 +03005321 if (istk)
5322 pp_list_one_macro(istk->mstk, severity);
H. Peter Anvin4def1a82016-05-09 13:59:44 -07005323
H. Peter Anvin274cda82016-05-10 02:56:29 -07005324 src_set(saved_line, saved_fname);
H. Peter Anvin4def1a82016-05-09 13:59:44 -07005325}
5326
H. Peter Anvine7469712016-02-18 02:20:59 -08005327const struct preproc_ops nasmpp = {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005328 pp_reset,
5329 pp_getline,
Cyrill Gorcunov0b78bff2012-05-07 01:57:55 +04005330 pp_cleanup,
H. Peter Anvinf7606612016-07-13 14:23:48 -07005331 pp_add_stdmac,
Cyrill Gorcunov0b78bff2012-05-07 01:57:55 +04005332 pp_pre_define,
5333 pp_pre_undefine,
5334 pp_pre_include,
H. Peter Anvin4def1a82016-05-09 13:59:44 -07005335 pp_include_path,
5336 pp_error_list_macros,
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005337};