blob: 054cc209216b52623e1b128e8e62a590e7dd31c4 [file] [log] [blame]
H. Peter Anvin9e6747c2009-06-28 17:13:04 -07001/* ----------------------------------------------------------------------- *
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002 *
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04003 * Copyright 1996-2014 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>
Keith Kaniosb7a89542007-04-12 02:40:54 +000072#include <inttypes.h>
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000073
74#include "nasm.h"
75#include "nasmlib.h"
H. Peter Anvin4169a472007-09-12 01:29:43 +000076#include "preproc.h"
H. Peter Anvin97a23472007-09-16 17:57:25 -070077#include "hashtbl.h"
H. Peter Anvin8cad14b2008-06-01 17:23:51 -070078#include "quote.h"
H. Peter Anvinc2df2822007-10-24 15:29:28 -070079#include "stdscan.h"
H. Peter Anvindbb640b2009-07-18 18:57:16 -070080#include "eval.h"
H. Peter Anvinc2df2822007-10-24 15:29:28 -070081#include "tokens.h"
H. Peter Anvina4835d42008-05-20 14:21:29 -070082#include "tables.h"
H. Peter 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... */
157};
158
159
160/* Store the definition of a multi-line macro, as defined in a
161 * previous recursive macro expansion.
162 */
163struct MMacroInvocation {
164 MMacroInvocation *prev; /* previous invocation */
165 Token **params; /* actual parameters */
166 Token *iline; /* invocation line */
167 unsigned int nparam, rotate;
168 int *paramlen;
169 uint64_t unique;
170 uint64_t condcnt;
171};
172
173
174/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000175 * The context stack is composed of a linked list of these.
176 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000177struct Context {
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800178 Context *next;
179 char *name;
180 struct hash_table localmac;
181 uint32_t number;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000182};
183
184/*
185 * This is the internal form which we break input lines up into.
186 * Typically stored in linked lists.
187 *
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000188 * Note that `type' serves a double meaning: TOK_SMAC_PARAM is not
189 * necessarily used as-is, but is intended to denote the number of
190 * the substituted parameter. So in the definition
191 *
192 * %define a(x,y) ( (x) & ~(y) )
H. Peter Anvin70653092007-10-19 14:42:29 -0700193 *
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000194 * the token representing `x' will have its type changed to
195 * TOK_SMAC_PARAM, but the one representing `y' will be
196 * TOK_SMAC_PARAM+1.
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000197 *
198 * TOK_INTERNAL_STRING is a dirty hack: it's a single string token
199 * which doesn't need quotes around it. Used in the pre-include
200 * mechanism as an alternative to trying to find a sensible type of
201 * quote to use on the filename we were passed.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000202 */
H. Peter Anvinda10e7b2007-09-12 04:18:37 +0000203enum pp_token_type {
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800204 TOK_NONE = 0, TOK_WHITESPACE, TOK_COMMENT, TOK_ID,
205 TOK_PREPROC_ID, TOK_STRING,
206 TOK_NUMBER, TOK_FLOAT, TOK_SMAC_END, TOK_OTHER,
H. Peter Anvin6c81f0a2008-05-25 21:46:17 -0700207 TOK_INTERNAL_STRING,
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800208 TOK_PREPROC_Q, TOK_PREPROC_QQ,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300209 TOK_PASTE, /* %+ */
210 TOK_INDIRECT, /* %[...] */
211 TOK_SMAC_PARAM, /* MUST BE LAST IN THE LIST!!! */
212 TOK_MAX = INT_MAX /* Keep compiler from reducing the range */
H. Peter Anvinda10e7b2007-09-12 04:18:37 +0000213};
214
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +0400215#define PP_CONCAT_MASK(x) (1 << (x))
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +0400216#define PP_CONCAT_MATCH(t, mask) (PP_CONCAT_MASK((t)->type) & mask)
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +0400217
Cyrill Gorcunov575d4282010-10-06 00:25:55 +0400218struct tokseq_match {
219 int mask_head;
220 int mask_tail;
221};
222
H. Peter Anvine2c80182005-01-15 22:15:51 +0000223struct Token {
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800224 Token *next;
225 char *text;
H. Peter Anvinf26e0972008-07-01 21:26:27 -0700226 union {
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800227 SMacro *mac; /* associated macro for TOK_SMAC_END */
228 size_t len; /* scratch length field */
229 } a; /* Auxiliary data */
230 enum pp_token_type type;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000231};
232
233/*
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800234 * Multi-line macro definitions are stored as a linked list of
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000235 * these, which is essentially a container to allow several linked
236 * lists of Tokens.
H. Peter Anvin70653092007-10-19 14:42:29 -0700237 *
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000238 * Note that in this module, linked lists are treated as stacks
239 * wherever possible. For this reason, Lines are _pushed_ on to the
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800240 * `expansion' field in MMacro structures, so that the linked list,
241 * if walked, would give the macro lines in reverse order; this
242 * means that we can walk the list when expanding a macro, and thus
243 * push the lines on to the `expansion' field in _istk_ in reverse
244 * order (so that when popped back off they are in the right
245 * order). It may seem cockeyed, and it relies on my design having
246 * an even number of steps in, but it works...
247 *
248 * Some of these structures, rather than being actual lines, are
249 * markers delimiting the end of the expansion of a given macro.
250 * This is for use in the cycle-tracking and %rep-handling code.
251 * Such structures have `finishes' non-NULL, and `first' NULL. All
252 * others have `finishes' NULL, but `first' may still be NULL if
253 * the line is blank.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000254 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000255struct Line {
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800256 Line *next;
257 MMacro *finishes;
258 Token *first;
Keith Kaniosb307a4f2010-11-06 17:41:51 -0500259};
260
261/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000262 * To handle an arbitrary level of file inclusion, we maintain a
263 * stack (ie linked list) of these things.
264 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000265struct Include {
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800266 Include *next;
267 FILE *fp;
268 Cond *conds;
269 Line *expansion;
270 char *fname;
271 int lineno, lineinc;
272 MMacro *mstk; /* stack of active macros/reps */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000273};
274
275/*
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000276 * Include search path. This is simply a list of strings which get
277 * prepended, in turn, to the name of an include file, in an
278 * attempt to find the file if it's not in the current directory.
279 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000280struct IncPath {
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800281 IncPath *next;
282 char *path;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000283};
284
285/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000286 * Conditional assembly: we maintain a separate stack of these for
287 * each level of file inclusion. (The only reason we keep the
288 * stacks separate is to ensure that a stray `%endif' in a file
289 * included from within the true branch of a `%if' won't terminate
290 * it and cause confusion: instead, rightly, it'll cause an error.)
291 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800292struct Cond {
293 Cond *next;
294 int state;
295};
H. Peter Anvine2c80182005-01-15 22:15:51 +0000296enum {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000297 /*
298 * These states are for use just after %if or %elif: IF_TRUE
299 * means the condition has evaluated to truth so we are
300 * currently emitting, whereas IF_FALSE means we are not
301 * currently emitting but will start doing so if a %else comes
302 * up. In these states, all directives are admissible: %elif,
303 * %else and %endif. (And of course %if.)
304 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800305 COND_IF_TRUE, COND_IF_FALSE,
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000306 /*
307 * These states come up after a %else: ELSE_TRUE means we're
308 * emitting, and ELSE_FALSE means we're not. In ELSE_* states,
309 * any %elif or %else will cause an error.
310 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800311 COND_ELSE_TRUE, COND_ELSE_FALSE,
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000312 /*
Victor van den Elzen3b404c02008-09-18 13:51:36 +0200313 * These states mean that we're not emitting now, and also that
314 * nothing until %endif will be emitted at all. COND_DONE is
315 * used when we've had our moment of emission
316 * and have now started seeing %elifs. COND_NEVER is used when
317 * the condition construct in question is contained within a
318 * non-emitting branch of a larger condition construct,
319 * or if there is an error.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000320 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800321 COND_DONE, COND_NEVER
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000322};
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800323#define emitting(x) ( (x) == COND_IF_TRUE || (x) == COND_ELSE_TRUE )
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000324
H. Peter Anvin70653092007-10-19 14:42:29 -0700325/*
Ed Beroset3ab3f412002-06-11 03:31:49 +0000326 * These defines are used as the possible return values for do_directive
327 */
328#define NO_DIRECTIVE_FOUND 0
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300329#define DIRECTIVE_FOUND 1
Ed Beroset3ab3f412002-06-11 03:31:49 +0000330
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000331/*
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800332 * This define sets the upper limit for smacro and recursive mmacro
333 * expansions
Keith Kanios852f1ee2009-07-12 00:19:55 -0500334 */
335#define DEADMAN_LIMIT (1 << 20)
336
Cyrill Gorcunove091d6e2010-08-09 13:58:22 +0400337/* max reps */
338#define REP_LIMIT ((INT64_C(1) << 62))
339
Keith Kanios852f1ee2009-07-12 00:19:55 -0500340/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000341 * Condition codes. Note that we use c_ prefix not C_ because C_ is
342 * used in nasm.h for the "real" condition codes. At _this_ level,
343 * we treat CXZ and ECXZ as condition codes, albeit non-invertible
344 * ones, so we need a different enum...
345 */
H. Peter Anvin476d2862007-10-02 22:04:15 -0700346static const char * const conditions[] = {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000347 "a", "ae", "b", "be", "c", "cxz", "e", "ecxz", "g", "ge", "l", "le",
348 "na", "nae", "nb", "nbe", "nc", "ne", "ng", "nge", "nl", "nle", "no",
H. Peter Anvince9be342007-09-12 00:22:29 +0000349 "np", "ns", "nz", "o", "p", "pe", "po", "rcxz", "s", "z"
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000350};
H. Peter Anvin476d2862007-10-02 22:04:15 -0700351enum pp_conds {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000352 c_A, c_AE, c_B, c_BE, c_C, c_CXZ, c_E, c_ECXZ, c_G, c_GE, c_L, c_LE,
353 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 -0700354 c_NP, c_NS, c_NZ, c_O, c_P, c_PE, c_PO, c_RCXZ, c_S, c_Z,
355 c_none = -1
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000356};
H. Peter Anvin476d2862007-10-02 22:04:15 -0700357static const enum pp_conds inverse_ccs[] = {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000358 c_NA, c_NAE, c_NB, c_NBE, c_NC, -1, c_NE, -1, c_NG, c_NGE, c_NL, c_NLE,
359 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 +0000360 c_Z, c_NO, c_NP, c_PO, c_PE, -1, c_NS, c_NZ
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000361};
362
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800363/*
364 * Directive names.
365 */
366/* If this is a an IF, ELIF, ELSE or ENDIF keyword */
367static int is_condition(enum preproc_token arg)
368{
369 return PP_IS_COND(arg) || (arg == PP_ELSE) || (arg == PP_ENDIF);
370}
371
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000372/* For TASM compatibility we need to be able to recognise TASM compatible
373 * conditional compilation directives. Using the NASM pre-processor does
374 * not work, so we look for them specifically from the following list and
375 * then jam in the equivalent NASM directive into the input stream.
376 */
377
H. Peter Anvine2c80182005-01-15 22:15:51 +0000378enum {
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000379 TM_ARG, TM_ELIF, TM_ELSE, TM_ENDIF, TM_IF, TM_IFDEF, TM_IFDIFI,
380 TM_IFNDEF, TM_INCLUDE, TM_LOCAL
381};
382
H. Peter Anvin476d2862007-10-02 22:04:15 -0700383static const char * const tasm_directives[] = {
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000384 "arg", "elif", "else", "endif", "if", "ifdef", "ifdifi",
385 "ifndef", "include", "local"
386};
387
388static int StackSize = 4;
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000389static char *StackPointer = "ebp";
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000390static int ArgOffset = 8;
H. Peter Anvin8781cb02007-11-08 20:01:11 -0800391static int LocalOffset = 0;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000392
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000393static Context *cstk;
394static Include *istk;
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800395static IncPath *ipath = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000396
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300397static int pass; /* HACK: pass 0 = generate dependencies only */
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700398static StrList **dephead, **deptail; /* Dependency list */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000399
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300400static uint64_t unique; /* unique identifier numbers */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000401
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800402static Line *predef = NULL;
H. Peter Anvind2456592008-06-19 15:04:18 -0700403static bool do_predef;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000404
405static ListGen *list;
406
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000407/*
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800408 * The current set of multi-line macros we have defined.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000409 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800410static struct hash_table mmacros;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000411
412/*
413 * The current set of single-line macros we have defined.
414 */
H. Peter Anvin166c2472008-05-28 12:28:58 -0700415static struct hash_table smacros;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000416
417/*
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800418 * The multi-line macro we are currently defining, or the %rep
419 * block we are currently reading, if any.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000420 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800421static MMacro *defining;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000422
Charles Crayned4200be2008-07-12 16:42:33 -0700423static uint64_t nested_mac_count;
424static uint64_t nested_rep_count;
425
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000426/*
427 * The number of macro parameters to allocate space for at a time.
428 */
429#define PARAM_DELTA 16
430
431/*
H. Peter Anvina4835d42008-05-20 14:21:29 -0700432 * The standard macro set: defined in macros.c in the array nasm_stdmac.
433 * This gives our position in the macro set, when we're processing it.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000434 */
H. Peter Anvina70547f2008-07-19 21:44:26 -0700435static macros_t *stdmacpos;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000436
437/*
H. Peter Anvin76690a12002-04-30 20:52:49 +0000438 * The extra standard macros that come from the object format, if
439 * any.
440 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800441static macros_t *extrastdmac = NULL;
H. Peter Anvincfb71762008-06-20 15:20:16 -0700442static bool any_extrastdmac;
H. Peter Anvin76690a12002-04-30 20:52:49 +0000443
444/*
H. Peter Anvin734b1882002-04-30 21:01:08 +0000445 * Tokens are allocated in blocks to improve speed
446 */
447#define TOKEN_BLOCKSIZE 4096
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800448static Token *freeTokens = NULL;
H. Peter Anvince616072002-04-30 21:02:23 +0000449struct Blocks {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000450 Blocks *next;
451 void *chunk;
H. Peter Anvince616072002-04-30 21:02:23 +0000452};
453
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800454static Blocks blocks = { NULL, NULL };
H. Peter Anvin734b1882002-04-30 21:01:08 +0000455
456/*
H. Peter Anvin76690a12002-04-30 20:52:49 +0000457 * Forward declarations.
458 */
H. Peter Anvin734b1882002-04-30 21:01:08 +0000459static Token *expand_mmac_params(Token * tline);
460static Token *expand_smacro(Token * tline);
461static Token *expand_id(Token * tline);
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +0400462static Context *get_ctx(const char *name, const char **namep);
Keith Kaniosa5fc6462007-10-13 07:09:22 -0700463static void make_tok_num(Token * tok, int64_t val);
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000464static void error(int severity, const char *fmt, ...);
Victor van den Elzen3b404c02008-09-18 13:51:36 +0200465static void error_precond(int severity, const char *fmt, ...);
H. Peter Anvince616072002-04-30 21:02:23 +0000466static void *new_Block(size_t size);
467static void delete_Blocks(void);
H. Peter Anvinc751e862008-06-09 10:18:45 -0700468static Token *new_Token(Token * next, enum pp_token_type type,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300469 const char *text, int txtlen);
H. Peter Anvin734b1882002-04-30 21:01:08 +0000470static Token *delete_Token(Token * t);
H. Peter Anvineba20a72002-04-30 20:53:55 +0000471
472/*
473 * Macros for safe checking of token pointers, avoid *(NULL)
474 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300475#define tok_type_(x,t) ((x) && (x)->type == (t))
476#define skip_white_(x) if (tok_type_((x), TOK_WHITESPACE)) (x)=(x)->next
477#define tok_is_(x,v) (tok_type_((x), TOK_OTHER) && !strcmp((x)->text,(v)))
478#define tok_isnt_(x,v) ((x) && ((x)->type!=TOK_OTHER || strcmp((x)->text,(v))))
H. Peter Anvin76690a12002-04-30 20:52:49 +0000479
Cyrill Gorcunov194ba892011-06-30 01:16:35 +0400480/*
H. Peter Anvin077fb932010-07-20 14:56:30 -0700481 * nasm_unquote with error if the string contains NUL characters.
482 * If the string contains NUL characters, issue an error and return
483 * the C len, i.e. truncate at the NUL.
484 */
485static size_t nasm_unquote_cstr(char *qstr, enum preproc_token directive)
486{
487 size_t len = nasm_unquote(qstr, NULL);
488 size_t clen = strlen(qstr);
489
490 if (len != clen)
491 error(ERR_NONFATAL, "NUL character in `%s' directive",
492 pp_directives[directive]);
493
494 return clen;
495}
496
497/*
H. Peter Anvinb40992c2010-09-15 08:57:21 -0700498 * In-place reverse a list of tokens.
499 */
500static Token *reverse_tokens(Token *t)
501{
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800502 Token *prev = NULL;
503 Token *next;
H. Peter Anvinb40992c2010-09-15 08:57:21 -0700504
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800505 while (t) {
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +0400506 next = t->next;
507 t->next = prev;
508 prev = t;
509 t = next;
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800510 }
H. Peter Anvinb40992c2010-09-15 08:57:21 -0700511
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800512 return prev;
H. Peter Anvinb40992c2010-09-15 08:57:21 -0700513}
514
515/*
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300516 * Handle TASM specific directives, which do not contain a % in
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000517 * front of them. We do it here because I could not find any other
518 * place to do it for the moment, and it is a hack (ideally it would
519 * be nice to be able to use the NASM pre-processor to do it).
520 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000521static char *check_tasm_directive(char *line)
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000522{
Keith Kaniosb7a89542007-04-12 02:40:54 +0000523 int32_t i, j, k, m, len;
Cyrill Gorcunovf66ac7d2009-10-12 20:41:13 +0400524 char *p, *q, *oldline, oldchar;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000525
Cyrill Gorcunovf66ac7d2009-10-12 20:41:13 +0400526 p = nasm_skip_spaces(line);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000527
528 /* Binary search for the directive name */
529 i = -1;
Cyrill Gorcunova7319242010-06-03 22:04:36 +0400530 j = ARRAY_SIZE(tasm_directives);
Cyrill Gorcunovf66ac7d2009-10-12 20:41:13 +0400531 q = nasm_skip_word(p);
532 len = q - p;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000533 if (len) {
534 oldchar = p[len];
535 p[len] = 0;
536 while (j - i > 1) {
537 k = (j + i) / 2;
538 m = nasm_stricmp(p, tasm_directives[k]);
539 if (m == 0) {
540 /* We have found a directive, so jam a % in front of it
541 * so that NASM will then recognise it as one if it's own.
542 */
543 p[len] = oldchar;
544 len = strlen(p);
545 oldline = line;
546 line = nasm_malloc(len + 2);
547 line[0] = '%';
548 if (k == TM_IFDIFI) {
H. Peter Anvin18f48792009-06-27 15:56:27 -0700549 /*
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300550 * NASM does not recognise IFDIFI, so we convert
551 * it to %if 0. This is not used in NASM
552 * compatible code, but does need to parse for the
553 * TASM macro package.
H. Peter Anvine2c80182005-01-15 22:15:51 +0000554 */
H. Peter Anvin18f48792009-06-27 15:56:27 -0700555 strcpy(line + 1, "if 0");
H. Peter Anvine2c80182005-01-15 22:15:51 +0000556 } else {
557 memcpy(line + 1, p, len + 1);
558 }
559 nasm_free(oldline);
560 return line;
561 } else if (m < 0) {
562 j = k;
563 } else
564 i = k;
565 }
566 p[len] = oldchar;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000567 }
568 return line;
569}
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000570
H. Peter Anvin76690a12002-04-30 20:52:49 +0000571/*
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000572 * The pre-preprocessing stage... This function translates line
573 * number indications as they emerge from GNU cpp (`# lineno "file"
574 * flags') into NASM preprocessor line number indications (`%line
575 * lineno file').
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000576 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000577static char *prepreproc(char *line)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000578{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000579 int lineno, fnlen;
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000580 char *fname, *oldline;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000581
H. Peter Anvine2c80182005-01-15 22:15:51 +0000582 if (line[0] == '#' && line[1] == ' ') {
583 oldline = line;
584 fname = oldline + 2;
585 lineno = atoi(fname);
586 fname += strspn(fname, "0123456789 ");
587 if (*fname == '"')
588 fname++;
589 fnlen = strcspn(fname, "\"");
590 line = nasm_malloc(20 + fnlen);
591 snprintf(line, 20 + fnlen, "%%line %d %.*s", lineno, fnlen, fname);
592 nasm_free(oldline);
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000593 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000594 if (tasm_compatible_mode)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000595 return check_tasm_directive(line);
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000596 return line;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000597}
598
599/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000600 * Free a linked list of tokens.
601 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000602static void free_tlist(Token * list)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000603{
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +0400604 while (list)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000605 list = delete_Token(list);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000606}
607
608/*
609 * Free a linked list of lines.
610 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000611static void free_llist(Line * list)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000612{
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +0400613 Line *l, *tmp;
614 list_for_each_safe(l, tmp, list) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000615 free_tlist(l->first);
616 nasm_free(l);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000617 }
618}
619
620/*
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800621 * Free an MMacro
H. Peter Anvineba20a72002-04-30 20:53:55 +0000622 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800623static void free_mmacro(MMacro * m)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000624{
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800625 nasm_free(m->name);
626 free_tlist(m->dlist);
627 nasm_free(m->defaults);
628 free_llist(m->expansion);
629 nasm_free(m);
H. Peter Anvineba20a72002-04-30 20:53:55 +0000630}
631
632/*
H. Peter Anvin97a23472007-09-16 17:57:25 -0700633 * Free all currently defined macros, and free the hash tables
634 */
H. Peter Anvin072771e2008-05-22 13:17:51 -0700635static void free_smacro_table(struct hash_table *smt)
H. Peter Anvin97a23472007-09-16 17:57:25 -0700636{
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +0400637 SMacro *s, *tmp;
H. Peter Anvin072771e2008-05-22 13:17:51 -0700638 const char *key;
639 struct hash_tbl_node *it = NULL;
H. Peter Anvin97a23472007-09-16 17:57:25 -0700640
H. Peter Anvin072771e2008-05-22 13:17:51 -0700641 while ((s = hash_iterate(smt, &it, &key)) != NULL) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300642 nasm_free((void *)key);
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +0400643 list_for_each_safe(s, tmp, s) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300644 nasm_free(s->name);
645 free_tlist(s->expansion);
646 nasm_free(s);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300647 }
H. Peter Anvin97a23472007-09-16 17:57:25 -0700648 }
H. Peter Anvin072771e2008-05-22 13:17:51 -0700649 hash_free(smt);
650}
651
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800652static void free_mmacro_table(struct hash_table *mmt)
H. Peter Anvin072771e2008-05-22 13:17:51 -0700653{
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800654 MMacro *m, *tmp;
H. Peter Anvin072771e2008-05-22 13:17:51 -0700655 const char *key;
656 struct hash_tbl_node *it = NULL;
H. Peter Anvin97a23472007-09-16 17:57:25 -0700657
658 it = NULL;
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800659 while ((m = hash_iterate(mmt, &it, &key)) != NULL) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300660 nasm_free((void *)key);
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800661 list_for_each_safe(m ,tmp, m)
662 free_mmacro(m);
H. Peter Anvin97a23472007-09-16 17:57:25 -0700663 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800664 hash_free(mmt);
H. Peter Anvin072771e2008-05-22 13:17:51 -0700665}
666
667static void free_macros(void)
668{
H. Peter Anvin166c2472008-05-28 12:28:58 -0700669 free_smacro_table(&smacros);
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800670 free_mmacro_table(&mmacros);
H. Peter Anvin97a23472007-09-16 17:57:25 -0700671}
672
673/*
674 * Initialize the hash tables
675 */
676static void init_macros(void)
677{
H. Peter Anvin166c2472008-05-28 12:28:58 -0700678 hash_init(&smacros, HASH_LARGE);
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800679 hash_init(&mmacros, HASH_LARGE);
H. Peter Anvin97a23472007-09-16 17:57:25 -0700680}
681
682/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000683 * Pop the context stack.
684 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000685static void ctx_pop(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000686{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000687 Context *c = cstk;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000688
689 cstk = cstk->next;
H. Peter Anvin166c2472008-05-28 12:28:58 -0700690 free_smacro_table(&c->localmac);
H. Peter Anvin734b1882002-04-30 21:01:08 +0000691 nasm_free(c->name);
692 nasm_free(c);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000693}
694
H. Peter Anvin072771e2008-05-22 13:17:51 -0700695/*
696 * Search for a key in the hash index; adding it if necessary
697 * (in which case we initialize the data pointer to NULL.)
698 */
699static void **
700hash_findi_add(struct hash_table *hash, const char *str)
701{
702 struct hash_insert hi;
703 void **r;
704 char *strx;
705
706 r = hash_findi(hash, str, &hi);
707 if (r)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300708 return r;
H. Peter Anvin072771e2008-05-22 13:17:51 -0700709
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300710 strx = nasm_strdup(str); /* Use a more efficient allocator here? */
H. Peter Anvin072771e2008-05-22 13:17:51 -0700711 return hash_add(&hi, strx, NULL);
712}
713
714/*
715 * Like hash_findi, but returns the data element rather than a pointer
716 * to it. Used only when not adding a new element, hence no third
717 * argument.
718 */
719static void *
720hash_findix(struct hash_table *hash, const char *str)
721{
722 void **p;
723
724 p = hash_findi(hash, str, NULL);
725 return p ? *p : NULL;
726}
727
Cyrill Gorcunov15bdc512010-07-13 11:27:41 +0400728/*
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800729 * read line from standart macros set,
Cyrill Gorcunov15bdc512010-07-13 11:27:41 +0400730 * if there no more left -- return NULL
731 */
732static char *line_from_stdmac(void)
733{
734 unsigned char c;
735 const unsigned char *p = stdmacpos;
736 char *line, *q;
737 size_t len = 0;
738
739 if (!stdmacpos)
740 return NULL;
741
742 while ((c = *p++)) {
743 if (c >= 0x80)
744 len += pp_directives_len[c - 0x80] + 1;
745 else
746 len++;
747 }
748
749 line = nasm_malloc(len + 1);
750 q = line;
751 while ((c = *stdmacpos++)) {
752 if (c >= 0x80) {
753 memcpy(q, pp_directives[c - 0x80], pp_directives_len[c - 0x80]);
754 q += pp_directives_len[c - 0x80];
755 *q++ = ' ';
756 } else {
757 *q++ = c;
758 }
759 }
760 stdmacpos = p;
761 *q = '\0';
762
763 if (!*stdmacpos) {
764 /* This was the last of the standard macro chain... */
765 stdmacpos = NULL;
766 if (any_extrastdmac) {
767 stdmacpos = extrastdmac;
768 any_extrastdmac = false;
769 } else if (do_predef) {
770 Line *pd, *l;
771 Token *head, **tail, *t;
772
773 /*
774 * Nasty hack: here we push the contents of
775 * `predef' on to the top-level expansion stack,
776 * since this is the most convenient way to
777 * implement the pre-include and pre-define
778 * features.
779 */
780 list_for_each(pd, predef) {
781 head = NULL;
782 tail = &head;
783 list_for_each(t, pd->first) {
784 *tail = new_Token(NULL, t->type, t->text, 0);
785 tail = &(*tail)->next;
786 }
787
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800788 l = nasm_malloc(sizeof(Line));
789 l->next = istk->expansion;
790 l->first = head;
791 l->finishes = NULL;
792
793 istk->expansion = l;
Cyrill Gorcunov15bdc512010-07-13 11:27:41 +0400794 }
795 do_predef = false;
796 }
797 }
798
799 return line;
800}
801
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000802static char *read_line(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000803{
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +0400804 unsigned int size, c, next;
805 const unsigned int delta = 512;
806 const unsigned int pad = 8;
807 unsigned int nr_cont = 0;
808 bool cont = false;
809 char *buffer, *p;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000810
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +0400811 /* Standart macros set (predefined) goes first */
Cyrill Gorcunov15bdc512010-07-13 11:27:41 +0400812 p = line_from_stdmac();
813 if (p)
814 return p;
H. Peter Anvin72edbb82008-06-19 16:00:04 -0700815
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +0400816 size = delta;
817 p = buffer = nasm_malloc(size);
818
819 for (;;) {
820 c = fgetc(istk->fp);
821 if ((int)(c) == EOF) {
822 p[0] = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000823 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000824 }
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +0400825
826 switch (c) {
827 case '\r':
828 next = fgetc(istk->fp);
829 if (next != '\n')
830 ungetc(next, istk->fp);
831 if (cont) {
832 cont = false;
833 continue;
834 }
835 break;
836
837 case '\n':
838 if (cont) {
839 cont = false;
840 continue;
841 }
842 break;
843
844 case '\\':
845 next = fgetc(istk->fp);
846 ungetc(next, istk->fp);
Cyrill Gorcunov490f85e2012-12-27 20:02:17 +0400847 if (next == '\r' || next == '\n') {
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +0400848 cont = true;
849 nr_cont++;
850 continue;
851 }
852 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000853 }
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +0400854
855 if (c == '\r' || c == '\n') {
856 *p++ = 0;
857 break;
858 }
859
860 if (p >= (buffer + size - pad)) {
861 buffer = nasm_realloc(buffer, size + delta);
862 p = buffer + size - pad;
863 size += delta;
864 }
865
866 *p++ = (unsigned char)c;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000867 }
868
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +0400869 if (p == buffer) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000870 nasm_free(buffer);
871 return NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000872 }
873
Cyrill Gorcunova5aea572010-11-11 10:14:45 +0300874 src_set_linnum(src_get_linnum() + istk->lineinc +
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +0400875 (nr_cont * istk->lineinc));
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000876
877 /*
878 * Handle spurious ^Z, which may be inserted into source files
879 * by some file transfer utilities.
880 */
881 buffer[strcspn(buffer, "\032")] = '\0';
882
H. Peter Anvin734b1882002-04-30 21:01:08 +0000883 list->line(LIST_READ, buffer);
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000884
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000885 return buffer;
886}
887
888/*
Keith Kaniosb7a89542007-04-12 02:40:54 +0000889 * Tokenize a line of text. This is a very simple process since we
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000890 * don't need to parse the value out of e.g. numeric tokens: we
891 * simply split one string into many.
892 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000893static Token *tokenize(char *line)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000894{
H. Peter Anvinca544db2008-10-19 19:30:11 -0700895 char c, *p = line;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +0000896 enum pp_token_type type;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000897 Token *list = NULL;
898 Token *t, **tail = &list;
899
H. Peter Anvine2c80182005-01-15 22:15:51 +0000900 while (*line) {
901 p = line;
902 if (*p == '%') {
903 p++;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300904 if (*p == '+' && !nasm_isdigit(p[1])) {
905 p++;
906 type = TOK_PASTE;
907 } else if (nasm_isdigit(*p) ||
908 ((*p == '-' || *p == '+') && nasm_isdigit(p[1]))) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000909 do {
910 p++;
911 }
H. Peter Anvinbda7a6e2008-06-21 10:23:17 -0700912 while (nasm_isdigit(*p));
H. Peter Anvine2c80182005-01-15 22:15:51 +0000913 type = TOK_PREPROC_ID;
914 } else if (*p == '{') {
915 p++;
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800916 while (*p) {
917 if (*p == '}')
918 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000919 p[-1] = *p;
920 p++;
921 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800922 if (*p != '}')
923 error(ERR_WARNING | ERR_PASS1, "unterminated %{ construct");
H. Peter Anvine2c80182005-01-15 22:15:51 +0000924 p[-1] = '\0';
925 if (*p)
926 p++;
927 type = TOK_PREPROC_ID;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300928 } else if (*p == '[') {
929 int lvl = 1;
930 line += 2; /* Skip the leading %[ */
931 p++;
932 while (lvl && (c = *p++)) {
933 switch (c) {
934 case ']':
935 lvl--;
936 break;
937 case '%':
938 if (*p == '[')
939 lvl++;
940 break;
941 case '\'':
942 case '\"':
943 case '`':
Cyrill Gorcunovc6360a72010-07-13 13:32:19 +0400944 p = nasm_skip_string(p - 1) + 1;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300945 break;
946 default:
947 break;
948 }
949 }
950 p--;
951 if (*p)
952 *p++ = '\0';
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800953 if (lvl)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300954 error(ERR_NONFATAL, "unterminated %[ construct");
955 type = TOK_INDIRECT;
956 } else if (*p == '?') {
957 type = TOK_PREPROC_Q; /* %? */
958 p++;
959 if (*p == '?') {
960 type = TOK_PREPROC_QQ; /* %?? */
961 p++;
962 }
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +0400963 } else if (*p == '!') {
964 type = TOK_PREPROC_ID;
965 p++;
966 if (isidchar(*p)) {
967 do {
968 p++;
969 }
970 while (isidchar(*p));
971 } else if (*p == '\'' || *p == '\"' || *p == '`') {
972 p = nasm_skip_string(p);
973 if (*p)
974 p++;
975 else
976 error(ERR_NONFATAL|ERR_PASS1, "unterminated %! string");
977 } 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 Anvin917a3492008-09-24 09:14:49 -07001009 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) {
1256 error(ERR_NONFATAL | ERR_PASS1,
1257 "NUL character in %! string");
1258 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) {
1265 error(ERR_NONFATAL | ERR_PASS1,
1266 "nonexistent environment variable `%s'", v);
1267 p = "";
1268 }
1269 t->text = nasm_strdup(p);
1270 }
1271 nasm_free(q);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001272 }
H. Peter Anvin077fb932010-07-20 14:56:30 -07001273
H. Peter Anvine2c80182005-01-15 22:15:51 +00001274 /* Expand local macros here and not during preprocessing */
1275 if (expand_locals &&
1276 t->type == TOK_PREPROC_ID && t->text &&
1277 t->text[0] == '%' && t->text[1] == '$') {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001278 const char *q;
1279 char *p;
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04001280 Context *ctx = get_ctx(t->text, &q);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001281 if (ctx) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001282 char buffer[40];
Keith Kanios93f2e9a2007-04-14 00:10:59 +00001283 snprintf(buffer, sizeof(buffer), "..@%"PRIu32".", ctx->number);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001284 p = nasm_strcat(buffer, q);
1285 nasm_free(t->text);
1286 t->text = p;
1287 }
1288 }
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001289 if (t->type == TOK_WHITESPACE)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001290 len++;
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001291 else if (t->text)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001292 len += strlen(t->text);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001293 }
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001294
H. Peter Anvin734b1882002-04-30 21:01:08 +00001295 p = line = nasm_malloc(len + 1);
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001296
1297 list_for_each(t, tlist) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001298 if (t->type == TOK_WHITESPACE) {
H. Peter Anvinb4daadc2008-01-21 16:31:57 -08001299 *p++ = ' ';
H. Peter Anvine2c80182005-01-15 22:15:51 +00001300 } else if (t->text) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001301 q = t->text;
1302 while (*q)
1303 *p++ = *q++;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001304 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001305 }
1306 *p = '\0';
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001307
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001308 return line;
1309}
1310
1311/*
H. Peter Anvin76690a12002-04-30 20:52:49 +00001312 * A scanner, suitable for use by the expression evaluator, which
1313 * operates on a line of Tokens. Expects a pointer to a pointer to
1314 * the first token in the line to be passed in as its private_data
1315 * field.
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001316 *
1317 * FIX: This really needs to be unified with stdscan.
H. Peter Anvin76690a12002-04-30 20:52:49 +00001318 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001319static int ppscan(void *private_data, struct tokenval *tokval)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001320{
H. Peter Anvin76690a12002-04-30 20:52:49 +00001321 Token **tlineptr = private_data;
1322 Token *tline;
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001323 char ourcopy[MAX_KEYWORD+1], *p, *r, *s;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001324
H. Peter Anvine2c80182005-01-15 22:15:51 +00001325 do {
1326 tline = *tlineptr;
1327 *tlineptr = tline ? tline->next : NULL;
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04001328 } while (tline && (tline->type == TOK_WHITESPACE ||
1329 tline->type == TOK_COMMENT));
H. Peter Anvin76690a12002-04-30 20:52:49 +00001330
1331 if (!tline)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001332 return tokval->t_type = TOKEN_EOS;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001333
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001334 tokval->t_charptr = tline->text;
1335
H. Peter Anvin76690a12002-04-30 20:52:49 +00001336 if (tline->text[0] == '$' && !tline->text[1])
H. Peter Anvine2c80182005-01-15 22:15:51 +00001337 return tokval->t_type = TOKEN_HERE;
H. Peter Anvin7cf897e2002-05-30 21:30:33 +00001338 if (tline->text[0] == '$' && tline->text[1] == '$' && !tline->text[2])
H. Peter Anvine2c80182005-01-15 22:15:51 +00001339 return tokval->t_type = TOKEN_BASE;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001340
H. Peter Anvine2c80182005-01-15 22:15:51 +00001341 if (tline->type == TOK_ID) {
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001342 p = tokval->t_charptr = tline->text;
1343 if (p[0] == '$') {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001344 tokval->t_charptr++;
1345 return tokval->t_type = TOKEN_ID;
1346 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00001347
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001348 for (r = p, s = ourcopy; *r; r++) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001349 if (r >= p+MAX_KEYWORD)
1350 return tokval->t_type = TOKEN_ID; /* Not a keyword */
H. Peter Anvinac8f8fc2008-06-11 15:49:41 -07001351 *s++ = nasm_tolower(*r);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001352 }
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001353 *s = '\0';
1354 /* right, so we have an identifier sitting in temp storage. now,
1355 * is it actually a register or instruction name, or what? */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001356 return nasm_token_hash(ourcopy, tokval);
H. Peter Anvin76690a12002-04-30 20:52:49 +00001357 }
1358
H. Peter Anvine2c80182005-01-15 22:15:51 +00001359 if (tline->type == TOK_NUMBER) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001360 bool rn_error;
1361 tokval->t_integer = readnum(tline->text, &rn_error);
1362 tokval->t_charptr = tline->text;
1363 if (rn_error)
1364 return tokval->t_type = TOKEN_ERRNUM;
1365 else
1366 return tokval->t_type = TOKEN_NUM;
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001367 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00001368
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001369 if (tline->type == TOK_FLOAT) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001370 return tokval->t_type = TOKEN_FLOAT;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001371 }
1372
H. Peter Anvine2c80182005-01-15 22:15:51 +00001373 if (tline->type == TOK_STRING) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001374 char bq, *ep;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001375
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001376 bq = tline->text[0];
H. Peter Anvin11627042008-06-09 20:45:19 -07001377 tokval->t_charptr = tline->text;
1378 tokval->t_inttwo = nasm_unquote(tline->text, &ep);
H. Peter Anvind2456592008-06-19 15:04:18 -07001379
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001380 if (ep[0] != bq || ep[1] != '\0')
1381 return tokval->t_type = TOKEN_ERRSTR;
1382 else
1383 return tokval->t_type = TOKEN_STR;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001384 }
1385
H. Peter Anvine2c80182005-01-15 22:15:51 +00001386 if (tline->type == TOK_OTHER) {
1387 if (!strcmp(tline->text, "<<"))
1388 return tokval->t_type = TOKEN_SHL;
1389 if (!strcmp(tline->text, ">>"))
1390 return tokval->t_type = TOKEN_SHR;
1391 if (!strcmp(tline->text, "//"))
1392 return tokval->t_type = TOKEN_SDIV;
1393 if (!strcmp(tline->text, "%%"))
1394 return tokval->t_type = TOKEN_SMOD;
1395 if (!strcmp(tline->text, "=="))
1396 return tokval->t_type = TOKEN_EQ;
1397 if (!strcmp(tline->text, "<>"))
1398 return tokval->t_type = TOKEN_NE;
1399 if (!strcmp(tline->text, "!="))
1400 return tokval->t_type = TOKEN_NE;
1401 if (!strcmp(tline->text, "<="))
1402 return tokval->t_type = TOKEN_LE;
1403 if (!strcmp(tline->text, ">="))
1404 return tokval->t_type = TOKEN_GE;
1405 if (!strcmp(tline->text, "&&"))
1406 return tokval->t_type = TOKEN_DBL_AND;
1407 if (!strcmp(tline->text, "^^"))
1408 return tokval->t_type = TOKEN_DBL_XOR;
1409 if (!strcmp(tline->text, "||"))
1410 return tokval->t_type = TOKEN_DBL_OR;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001411 }
1412
1413 /*
1414 * We have no other options: just return the first character of
1415 * the token text.
1416 */
1417 return tokval->t_type = tline->text[0];
1418}
1419
1420/*
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001421 * Compare a string to the name of an existing macro; this is a
1422 * simple wrapper which calls either strcmp or nasm_stricmp
1423 * depending on the value of the `casesense' parameter.
1424 */
H. Peter Anvin4db5a162007-10-11 13:42:09 -07001425static int mstrcmp(const char *p, const char *q, bool casesense)
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001426{
H. Peter Anvin734b1882002-04-30 21:01:08 +00001427 return casesense ? strcmp(p, q) : nasm_stricmp(p, q);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001428}
1429
1430/*
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07001431 * Compare a string to the name of an existing macro; this is a
1432 * simple wrapper which calls either strcmp or nasm_stricmp
1433 * depending on the value of the `casesense' parameter.
1434 */
1435static int mmemcmp(const char *p, const char *q, size_t l, bool casesense)
1436{
1437 return casesense ? memcmp(p, q, l) : nasm_memicmp(p, q, l);
1438}
1439
1440/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001441 * Return the Context structure associated with a %$ token. Return
1442 * NULL, having _already_ reported an error condition, if the
1443 * context stack isn't deep enough for the supplied number of $
1444 * signs.
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001445 *
1446 * If "namep" is non-NULL, set it to the pointer to the macro name
1447 * tail, i.e. the part beyond %$...
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001448 */
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04001449static Context *get_ctx(const char *name, const char **namep)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001450{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001451 Context *ctx;
1452 int i;
1453
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001454 if (namep)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001455 *namep = name;
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001456
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001457 if (!name || name[0] != '%' || name[1] != '$')
H. Peter Anvine2c80182005-01-15 22:15:51 +00001458 return NULL;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001459
H. Peter Anvine2c80182005-01-15 22:15:51 +00001460 if (!cstk) {
1461 error(ERR_NONFATAL, "`%s': context stack is empty", name);
1462 return NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001463 }
1464
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001465 name += 2;
1466 ctx = cstk;
1467 i = 0;
1468 while (ctx && *name == '$') {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001469 name++;
1470 i++;
1471 ctx = ctx->next;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001472 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001473 if (!ctx) {
1474 error(ERR_NONFATAL, "`%s': context stack is only"
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001475 " %d level%s deep", name, i, (i == 1 ? "" : "s"));
H. Peter Anvine2c80182005-01-15 22:15:51 +00001476 return NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001477 }
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001478
1479 if (namep)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001480 *namep = name;
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001481
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04001482 return ctx;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001483}
1484
1485/*
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001486 * Check to see if a file is already in a string list
1487 */
1488static bool in_list(const StrList *list, const char *str)
1489{
1490 while (list) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001491 if (!strcmp(list->str, str))
1492 return true;
1493 list = list->next;
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001494 }
1495 return false;
1496}
1497
1498/*
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001499 * Open an include file. This routine must always return a valid
1500 * file pointer if it returns - it's responsible for throwing an
1501 * ERR_FATAL and bombing out completely if not. It should also try
1502 * the include path one by one until it finds the file or reaches
1503 * the end of the path.
1504 */
H. Peter Anvin2b1c3b92008-06-06 10:38:46 -07001505static FILE *inc_fopen(const char *file, StrList **dhead, StrList ***dtail,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001506 bool missing_ok)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001507{
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001508 FILE *fp;
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001509 char *prefix = "";
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001510 IncPath *ip = ipath;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001511 int len = strlen(file);
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001512 size_t prefix_len = 0;
1513 StrList *sl;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001514
H. Peter Anvine2c80182005-01-15 22:15:51 +00001515 while (1) {
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001516 sl = nasm_malloc(prefix_len+len+1+sizeof sl->next);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001517 memcpy(sl->str, prefix, prefix_len);
1518 memcpy(sl->str+prefix_len, file, len+1);
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001519 fp = fopen(sl->str, "r");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001520 if (fp && dhead && !in_list(*dhead, sl->str)) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001521 sl->next = NULL;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001522 **dtail = sl;
1523 *dtail = &sl->next;
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001524 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001525 nasm_free(sl);
1526 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001527 if (fp)
1528 return fp;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001529 if (!ip) {
1530 if (!missing_ok)
1531 break;
1532 prefix = NULL;
1533 } else {
1534 prefix = ip->path;
1535 ip = ip->next;
1536 }
1537 if (prefix) {
1538 prefix_len = strlen(prefix);
1539 } else {
1540 /* -MG given and file not found */
1541 if (dhead && !in_list(*dhead, file)) {
1542 sl = nasm_malloc(len+1+sizeof sl->next);
1543 sl->next = NULL;
1544 strcpy(sl->str, file);
1545 **dtail = sl;
1546 *dtail = &sl->next;
1547 }
1548 return NULL;
1549 }
H. Peter Anvineba20a72002-04-30 20:53:55 +00001550 }
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001551
H. Peter Anvin734b1882002-04-30 21:01:08 +00001552 error(ERR_FATAL, "unable to open include file `%s'", file);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001553 return NULL;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001554}
1555
1556/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001557 * Determine if we should warn on defining a single-line macro of
H. Peter Anvinef7468f2002-04-30 20:57:59 +00001558 * name `name', with `nparam' parameters. If nparam is 0 or -1, will
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001559 * return true if _any_ single-line macro of that name is defined.
1560 * Otherwise, will return true if a single-line macro with either
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001561 * `nparam' or no parameters is defined.
1562 *
1563 * If a macro with precisely the right number of parameters is
H. Peter Anvinef7468f2002-04-30 20:57:59 +00001564 * defined, or nparam is -1, the address of the definition structure
1565 * will be returned in `defn'; otherwise NULL will be returned. If `defn'
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001566 * is NULL, no action will be taken regarding its contents, and no
1567 * error will occur.
1568 *
1569 * Note that this is also called with nparam zero to resolve
1570 * `ifdef'.
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001571 *
1572 * If you already know which context macro belongs to, you can pass
1573 * the context pointer as first parameter; if you won't but name begins
1574 * with %$ the context will be automatically computed. If all_contexts
1575 * is true, macro will be searched in outer contexts as well.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001576 */
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07001577static bool
H. Peter Anvinb2a5fda2008-06-19 21:42:42 -07001578smacro_defined(Context * ctx, const char *name, int nparam, SMacro ** defn,
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07001579 bool nocase)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001580{
H. Peter Anvin166c2472008-05-28 12:28:58 -07001581 struct hash_table *smtbl;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001582 SMacro *m;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001583
H. Peter Anvin97a23472007-09-16 17:57:25 -07001584 if (ctx) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001585 smtbl = &ctx->localmac;
H. Peter Anvin97a23472007-09-16 17:57:25 -07001586 } else if (name[0] == '%' && name[1] == '$') {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001587 if (cstk)
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04001588 ctx = get_ctx(name, &name);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001589 if (!ctx)
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001590 return false; /* got to return _something_ */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001591 smtbl = &ctx->localmac;
H. Peter Anvin97a23472007-09-16 17:57:25 -07001592 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001593 smtbl = &smacros;
H. Peter Anvin97a23472007-09-16 17:57:25 -07001594 }
H. Peter Anvin166c2472008-05-28 12:28:58 -07001595 m = (SMacro *) hash_findix(smtbl, name);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001596
H. Peter Anvine2c80182005-01-15 22:15:51 +00001597 while (m) {
1598 if (!mstrcmp(m->name, name, m->casesense && nocase) &&
Charles Crayne192d5b52007-10-18 19:02:42 -07001599 (nparam <= 0 || m->nparam == 0 || nparam == (int) m->nparam)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001600 if (defn) {
Charles Crayne192d5b52007-10-18 19:02:42 -07001601 if (nparam == (int) m->nparam || nparam == -1)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001602 *defn = m;
1603 else
1604 *defn = NULL;
1605 }
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001606 return true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001607 }
1608 m = m->next;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001609 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001610
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001611 return false;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001612}
1613
1614/*
1615 * Count and mark off the parameters in a multi-line macro call.
1616 * This is called both from within the multi-line macro expansion
1617 * code, and also to mark off the default parameters when provided
1618 * in a %macro definition line.
1619 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001620static void count_mmac_params(Token * t, int *nparam, Token *** params)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001621{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001622 int paramsize, brace;
1623
1624 *nparam = paramsize = 0;
1625 *params = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001626 while (t) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001627 /* +1: we need space for the final NULL */
H. Peter Anvin91fb6f12008-09-01 10:56:33 -07001628 if (*nparam+1 >= paramsize) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001629 paramsize += PARAM_DELTA;
1630 *params = nasm_realloc(*params, sizeof(**params) * paramsize);
1631 }
1632 skip_white_(t);
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08001633 brace = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001634 if (tok_is_(t, "{"))
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08001635 brace++;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001636 (*params)[(*nparam)++] = t;
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08001637 if (brace) {
1638 while (brace && (t = t->next) != NULL) {
1639 if (tok_is_(t, "{"))
1640 brace++;
1641 else if (tok_is_(t, "}"))
1642 brace--;
1643 }
1644
1645 if (t) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001646 /*
1647 * Now we've found the closing brace, look further
1648 * for the comma.
1649 */
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08001650 t = t->next;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001651 skip_white_(t);
1652 if (tok_isnt_(t, ",")) {
1653 error(ERR_NONFATAL,
1654 "braces do not enclose all of macro parameter");
1655 while (tok_isnt_(t, ","))
1656 t = t->next;
1657 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001658 }
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08001659 } else {
1660 while (tok_isnt_(t, ","))
1661 t = t->next;
1662 }
1663 if (t) { /* got a comma/brace */
1664 t = t->next; /* eat the comma */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001665 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001666 }
1667}
1668
1669/*
H. Peter Anvin76690a12002-04-30 20:52:49 +00001670 * Determine whether one of the various `if' conditions is true or
1671 * not.
1672 *
1673 * We must free the tline we get passed.
1674 */
H. Peter Anvin70055962007-10-11 00:05:31 -07001675static bool if_condition(Token * tline, enum preproc_token ct)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001676{
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001677 enum pp_conditional i = PP_COND(ct);
H. Peter Anvin70055962007-10-11 00:05:31 -07001678 bool j;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001679 Token *t, *tt, **tptr, *origline;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001680 struct tokenval tokval;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001681 expr *evalresult;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001682 enum pp_token_type needtype;
H. Peter Anvin077fb932010-07-20 14:56:30 -07001683 char *p;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001684
1685 origline = tline;
1686
H. Peter Anvine2c80182005-01-15 22:15:51 +00001687 switch (i) {
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001688 case PPC_IFCTX:
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001689 j = false; /* have we matched yet? */
Victor van den Elzen0e857f12008-07-23 13:21:29 +02001690 while (true) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001691 skip_white_(tline);
Victor van den Elzen0e857f12008-07-23 13:21:29 +02001692 if (!tline)
1693 break;
1694 if (tline->type != TOK_ID) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001695 error(ERR_NONFATAL,
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001696 "`%s' expects context identifiers", pp_directives[ct]);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001697 free_tlist(origline);
1698 return -1;
1699 }
Victor van den Elzen0e857f12008-07-23 13:21:29 +02001700 if (cstk && cstk->name && !nasm_stricmp(tline->text, cstk->name))
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001701 j = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001702 tline = tline->next;
1703 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001704 break;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001705
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001706 case PPC_IFDEF:
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001707 j = false; /* have we matched yet? */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001708 while (tline) {
1709 skip_white_(tline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001710 if (!tline || (tline->type != TOK_ID &&
1711 (tline->type != TOK_PREPROC_ID ||
1712 tline->text[1] != '$'))) {
1713 error(ERR_NONFATAL,
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001714 "`%s' expects macro identifiers", pp_directives[ct]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001715 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001716 }
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07001717 if (smacro_defined(NULL, tline->text, 0, NULL, true))
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001718 j = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001719 tline = tline->next;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001720 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001721 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001722
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001723 case PPC_IFENV:
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001724 tline = expand_smacro(tline);
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001725 j = false; /* have we matched yet? */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001726 while (tline) {
1727 skip_white_(tline);
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001728 if (!tline || (tline->type != TOK_ID &&
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001729 tline->type != TOK_STRING &&
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001730 (tline->type != TOK_PREPROC_ID ||
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001731 tline->text[1] != '!'))) {
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001732 error(ERR_NONFATAL,
1733 "`%s' expects environment variable names",
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001734 pp_directives[ct]);
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001735 goto fail;
1736 }
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001737 p = tline->text;
1738 if (tline->type == TOK_PREPROC_ID)
1739 p += 2; /* Skip leading %! */
1740 if (*p == '\'' || *p == '\"' || *p == '`')
1741 nasm_unquote_cstr(p, ct);
1742 if (getenv(p))
1743 j = true;
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001744 tline = tline->next;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001745 }
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001746 break;
1747
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001748 case PPC_IFIDN:
1749 case PPC_IFIDNI:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001750 tline = expand_smacro(tline);
1751 t = tt = tline;
1752 while (tok_isnt_(tt, ","))
1753 tt = tt->next;
1754 if (!tt) {
1755 error(ERR_NONFATAL,
1756 "`%s' expects two comma-separated arguments",
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001757 pp_directives[ct]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001758 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001759 }
1760 tt = tt->next;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001761 j = true; /* assume equality unless proved not */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001762 while ((t->type != TOK_OTHER || strcmp(t->text, ",")) && tt) {
1763 if (tt->type == TOK_OTHER && !strcmp(tt->text, ",")) {
1764 error(ERR_NONFATAL, "`%s': more than one comma on line",
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001765 pp_directives[ct]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001766 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001767 }
1768 if (t->type == TOK_WHITESPACE) {
1769 t = t->next;
1770 continue;
1771 }
1772 if (tt->type == TOK_WHITESPACE) {
1773 tt = tt->next;
1774 continue;
1775 }
1776 if (tt->type != t->type) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001777 j = false; /* found mismatching tokens */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001778 break;
1779 }
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07001780 /* When comparing strings, need to unquote them first */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001781 if (t->type == TOK_STRING) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001782 size_t l1 = nasm_unquote(t->text, NULL);
1783 size_t l2 = nasm_unquote(tt->text, NULL);
H. Peter Anvind2456592008-06-19 15:04:18 -07001784
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001785 if (l1 != l2) {
1786 j = false;
1787 break;
1788 }
1789 if (mmemcmp(t->text, tt->text, l1, i == PPC_IFIDN)) {
1790 j = false;
1791 break;
1792 }
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07001793 } else if (mstrcmp(tt->text, t->text, i == PPC_IFIDN) != 0) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001794 j = false; /* found mismatching tokens */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001795 break;
1796 }
Nickolay Yurchenkof3b3ce22003-09-21 20:38:43 +00001797
H. Peter Anvine2c80182005-01-15 22:15:51 +00001798 t = t->next;
1799 tt = tt->next;
1800 }
1801 if ((t->type != TOK_OTHER || strcmp(t->text, ",")) || tt)
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001802 j = false; /* trailing gunk on one end or other */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001803 break;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001804
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001805 case PPC_IFMACRO:
H. Peter Anvin89cee572009-07-15 09:16:54 -04001806 {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001807 bool found = false;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001808 MMacro searching, *mmac;
H. Peter Anvin65747262002-05-07 00:10:05 +00001809
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001810 skip_white_(tline);
1811 tline = expand_id(tline);
1812 if (!tok_type_(tline, TOK_ID)) {
1813 error(ERR_NONFATAL,
1814 "`%s' expects a macro name", pp_directives[ct]);
1815 goto fail;
1816 }
1817 searching.name = nasm_strdup(tline->text);
1818 searching.casesense = true;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001819 searching.plus = false;
1820 searching.nolist = false;
1821 searching.in_progress = 0;
1822 searching.max_depth = 0;
1823 searching.rep_nest = NULL;
1824 searching.nparam_min = 0;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001825 searching.nparam_max = INT_MAX;
1826 tline = expand_smacro(tline->next);
1827 skip_white_(tline);
1828 if (!tline) {
1829 } else if (!tok_type_(tline, TOK_NUMBER)) {
1830 error(ERR_NONFATAL,
1831 "`%s' expects a parameter count or nothing",
1832 pp_directives[ct]);
1833 } else {
1834 searching.nparam_min = searching.nparam_max =
1835 readnum(tline->text, &j);
1836 if (j)
1837 error(ERR_NONFATAL,
1838 "unable to parse parameter count `%s'",
1839 tline->text);
1840 }
1841 if (tline && tok_is_(tline->next, "-")) {
1842 tline = tline->next->next;
1843 if (tok_is_(tline, "*"))
1844 searching.nparam_max = INT_MAX;
1845 else if (!tok_type_(tline, TOK_NUMBER))
1846 error(ERR_NONFATAL,
1847 "`%s' expects a parameter count after `-'",
1848 pp_directives[ct]);
1849 else {
1850 searching.nparam_max = readnum(tline->text, &j);
1851 if (j)
1852 error(ERR_NONFATAL,
1853 "unable to parse parameter count `%s'",
1854 tline->text);
1855 if (searching.nparam_min > searching.nparam_max)
1856 error(ERR_NONFATAL,
1857 "minimum parameter count exceeds maximum");
1858 }
1859 }
1860 if (tline && tok_is_(tline->next, "+")) {
1861 tline = tline->next;
1862 searching.plus = true;
1863 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001864 mmac = (MMacro *) hash_findix(&mmacros, searching.name);
1865 while (mmac) {
1866 if (!strcmp(mmac->name, searching.name) &&
1867 (mmac->nparam_min <= searching.nparam_max
1868 || searching.plus)
1869 && (searching.nparam_min <= mmac->nparam_max
1870 || mmac->plus)) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001871 found = true;
1872 break;
1873 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001874 mmac = mmac->next;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001875 }
1876 if (tline && tline->next)
1877 error(ERR_WARNING|ERR_PASS1,
1878 "trailing garbage after %%ifmacro ignored");
1879 nasm_free(searching.name);
1880 j = found;
1881 break;
H. Peter Anvin89cee572009-07-15 09:16:54 -04001882 }
H. Peter Anvin65747262002-05-07 00:10:05 +00001883
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001884 case PPC_IFID:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001885 needtype = TOK_ID;
1886 goto iftype;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001887 case PPC_IFNUM:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001888 needtype = TOK_NUMBER;
1889 goto iftype;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001890 case PPC_IFSTR:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001891 needtype = TOK_STRING;
1892 goto iftype;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001893
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001894iftype:
1895 t = tline = expand_smacro(tline);
H. Peter Anvind85d2502008-05-04 17:53:31 -07001896
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001897 while (tok_type_(t, TOK_WHITESPACE) ||
1898 (needtype == TOK_NUMBER &&
1899 tok_type_(t, TOK_OTHER) &&
1900 (t->text[0] == '-' || t->text[0] == '+') &&
1901 !t->text[1]))
1902 t = t->next;
H. Peter Anvind85d2502008-05-04 17:53:31 -07001903
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001904 j = tok_type_(t, needtype);
1905 break;
H. Peter Anvincbf768d2008-02-16 16:41:25 -08001906
1907 case PPC_IFTOKEN:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001908 t = tline = expand_smacro(tline);
1909 while (tok_type_(t, TOK_WHITESPACE))
1910 t = t->next;
H. Peter Anvincbf768d2008-02-16 16:41:25 -08001911
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001912 j = false;
1913 if (t) {
1914 t = t->next; /* Skip the actual token */
1915 while (tok_type_(t, TOK_WHITESPACE))
1916 t = t->next;
1917 j = !t; /* Should be nothing left */
1918 }
1919 break;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001920
H. Peter Anvin134b9462008-02-16 17:01:40 -08001921 case PPC_IFEMPTY:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001922 t = tline = expand_smacro(tline);
1923 while (tok_type_(t, TOK_WHITESPACE))
1924 t = t->next;
H. Peter Anvin134b9462008-02-16 17:01:40 -08001925
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001926 j = !t; /* Should be empty */
1927 break;
H. Peter Anvin134b9462008-02-16 17:01:40 -08001928
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001929 case PPC_IF:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001930 t = tline = expand_smacro(tline);
1931 tptr = &t;
1932 tokval.t_type = TOKEN_INVALID;
1933 evalresult = evaluate(ppscan, tptr, &tokval,
1934 NULL, pass | CRITICAL, error, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001935 if (!evalresult)
1936 return -1;
1937 if (tokval.t_type)
H. Peter Anvin917a3492008-09-24 09:14:49 -07001938 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +00001939 "trailing garbage after expression ignored");
1940 if (!is_simple(evalresult)) {
1941 error(ERR_NONFATAL,
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001942 "non-constant value given to `%s'", pp_directives[ct]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001943 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001944 }
Chuck Crayne60ae75d2007-05-02 01:59:16 +00001945 j = reloc_value(evalresult) != 0;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001946 break;
H. Peter Anvin95e28822007-09-12 04:20:08 +00001947
H. Peter Anvine2c80182005-01-15 22:15:51 +00001948 default:
1949 error(ERR_FATAL,
1950 "preprocessor directive `%s' not yet implemented",
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001951 pp_directives[ct]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001952 goto fail;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001953 }
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001954
1955 free_tlist(origline);
1956 return j ^ PP_NEGATIVE(ct);
H. Peter Anvin70653092007-10-19 14:42:29 -07001957
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001958fail:
1959 free_tlist(origline);
1960 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001961}
1962
1963/*
H. Peter Anvin4db5a162007-10-11 13:42:09 -07001964 * Common code for defining an smacro
1965 */
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001966static bool define_smacro(Context *ctx, const char *mname, bool casesense,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001967 int nparam, Token *expansion)
H. Peter Anvin4db5a162007-10-11 13:42:09 -07001968{
1969 SMacro *smac, **smhead;
H. Peter Anvin166c2472008-05-28 12:28:58 -07001970 struct hash_table *smtbl;
H. Peter Anvin70653092007-10-19 14:42:29 -07001971
H. Peter Anvin4db5a162007-10-11 13:42:09 -07001972 if (smacro_defined(ctx, mname, nparam, &smac, casesense)) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001973 if (!smac) {
1974 error(ERR_WARNING|ERR_PASS1,
1975 "single-line macro `%s' defined both with and"
1976 " without parameters", mname);
1977 /*
1978 * Some instances of the old code considered this a failure,
1979 * some others didn't. What is the right thing to do here?
1980 */
1981 free_tlist(expansion);
1982 return false; /* Failure */
1983 } else {
1984 /*
1985 * We're redefining, so we have to take over an
1986 * existing SMacro structure. This means freeing
1987 * what was already in it.
1988 */
1989 nasm_free(smac->name);
1990 free_tlist(smac->expansion);
1991 }
H. Peter Anvin4db5a162007-10-11 13:42:09 -07001992 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001993 smtbl = ctx ? &ctx->localmac : &smacros;
1994 smhead = (SMacro **) hash_findi_add(smtbl, mname);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001995 smac = nasm_malloc(sizeof(SMacro));
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001996 smac->next = *smhead;
1997 *smhead = smac;
H. Peter Anvin4db5a162007-10-11 13:42:09 -07001998 }
1999 smac->name = nasm_strdup(mname);
2000 smac->casesense = casesense;
2001 smac->nparam = nparam;
2002 smac->expansion = expansion;
2003 smac->in_progress = false;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002004 return true; /* Success */
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002005}
2006
2007/*
2008 * Undefine an smacro
2009 */
2010static void undef_smacro(Context *ctx, const char *mname)
2011{
2012 SMacro **smhead, *s, **sp;
H. Peter Anvin166c2472008-05-28 12:28:58 -07002013 struct hash_table *smtbl;
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002014
H. Peter Anvin166c2472008-05-28 12:28:58 -07002015 smtbl = ctx ? &ctx->localmac : &smacros;
2016 smhead = (SMacro **)hash_findi(smtbl, mname, NULL);
H. Peter Anvin70653092007-10-19 14:42:29 -07002017
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002018 if (smhead) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002019 /*
2020 * We now have a macro name... go hunt for it.
2021 */
2022 sp = smhead;
2023 while ((s = *sp) != NULL) {
2024 if (!mstrcmp(s->name, mname, s->casesense)) {
2025 *sp = s->next;
2026 nasm_free(s->name);
2027 free_tlist(s->expansion);
2028 nasm_free(s);
2029 } else {
2030 sp = &s->next;
2031 }
2032 }
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002033 }
2034}
2035
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002036/*
H. Peter Anvina26433d2008-07-16 14:40:01 -07002037 * Parse a mmacro specification.
2038 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002039static bool parse_mmacro_spec(Token *tline, MMacro *def, const char *directive)
H. Peter Anvina26433d2008-07-16 14:40:01 -07002040{
2041 bool err;
2042
2043 tline = tline->next;
2044 skip_white_(tline);
2045 tline = expand_id(tline);
2046 if (!tok_type_(tline, TOK_ID)) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002047 error(ERR_NONFATAL, "`%s' expects a macro name", directive);
2048 return false;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002049 }
Victor van den Elzenb916ede2008-07-23 15:14:22 +02002050
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002051 def->prev = NULL;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002052 def->name = nasm_strdup(tline->text);
2053 def->plus = false;
2054 def->nolist = false;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002055 def->in_progress = 0;
2056 def->rep_nest = NULL;
Victor van den Elzenb916ede2008-07-23 15:14:22 +02002057 def->nparam_min = 0;
2058 def->nparam_max = 0;
2059
H. Peter Anvina26433d2008-07-16 14:40:01 -07002060 tline = expand_smacro(tline->next);
2061 skip_white_(tline);
2062 if (!tok_type_(tline, TOK_NUMBER)) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002063 error(ERR_NONFATAL, "`%s' expects a parameter count", directive);
H. Peter Anvina26433d2008-07-16 14:40:01 -07002064 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002065 def->nparam_min = def->nparam_max =
2066 readnum(tline->text, &err);
2067 if (err)
2068 error(ERR_NONFATAL,
2069 "unable to parse parameter count `%s'", tline->text);
H. Peter Anvina26433d2008-07-16 14:40:01 -07002070 }
2071 if (tline && tok_is_(tline->next, "-")) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002072 tline = tline->next->next;
2073 if (tok_is_(tline, "*")) {
2074 def->nparam_max = INT_MAX;
2075 } else if (!tok_type_(tline, TOK_NUMBER)) {
2076 error(ERR_NONFATAL,
2077 "`%s' expects a parameter count after `-'", directive);
2078 } else {
2079 def->nparam_max = readnum(tline->text, &err);
2080 if (err) {
2081 error(ERR_NONFATAL, "unable to parse parameter count `%s'",
2082 tline->text);
2083 }
2084 if (def->nparam_min > def->nparam_max) {
2085 error(ERR_NONFATAL, "minimum parameter count exceeds maximum");
2086 }
2087 }
H. Peter Anvina26433d2008-07-16 14:40:01 -07002088 }
2089 if (tline && tok_is_(tline->next, "+")) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002090 tline = tline->next;
2091 def->plus = true;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002092 }
2093 if (tline && tok_type_(tline->next, TOK_ID) &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002094 !nasm_stricmp(tline->next->text, ".nolist")) {
2095 tline = tline->next;
2096 def->nolist = true;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002097 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002098
H. Peter Anvina26433d2008-07-16 14:40:01 -07002099 /*
2100 * Handle default parameters.
2101 */
2102 if (tline && tline->next) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002103 def->dlist = tline->next;
2104 tline->next = NULL;
2105 count_mmac_params(def->dlist, &def->ndefs, &def->defaults);
H. Peter Anvina26433d2008-07-16 14:40:01 -07002106 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002107 def->dlist = NULL;
2108 def->defaults = NULL;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002109 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002110 def->expansion = NULL;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002111
H. Peter Anvin89cee572009-07-15 09:16:54 -04002112 if (def->defaults && def->ndefs > def->nparam_max - def->nparam_min &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002113 !def->plus)
2114 error(ERR_WARNING|ERR_PASS1|ERR_WARN_MDP,
2115 "too many default macro parameters");
Victor van den Elzenb916ede2008-07-23 15:14:22 +02002116
H. Peter Anvina26433d2008-07-16 14:40:01 -07002117 return true;
2118}
2119
2120
2121/*
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002122 * Decode a size directive
2123 */
2124static int parse_size(const char *str) {
2125 static const char *size_names[] =
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002126 { "byte", "dword", "oword", "qword", "tword", "word", "yword" };
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002127 static const int sizes[] =
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002128 { 0, 1, 4, 16, 8, 10, 2, 32 };
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002129
Cyrill Gorcunova7319242010-06-03 22:04:36 +04002130 return sizes[bsii(str, size_names, ARRAY_SIZE(size_names))+1];
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002131}
2132
Ed Beroset3ab3f412002-06-11 03:31:49 +00002133/**
2134 * find and process preprocessor directive in passed line
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002135 * Find out if a line contains a preprocessor directive, and deal
2136 * with it if so.
H. Peter Anvin70653092007-10-19 14:42:29 -07002137 *
Ed Beroset3ab3f412002-06-11 03:31:49 +00002138 * If a directive _is_ found, it is the responsibility of this routine
2139 * (and not the caller) to free_tlist() the line.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002140 *
Ed Beroset3ab3f412002-06-11 03:31:49 +00002141 * @param tline a pointer to the current tokeninzed line linked list
2142 * @return DIRECTIVE_FOUND or NO_DIRECTIVE_FOUND
H. Peter Anvin70653092007-10-19 14:42:29 -07002143 *
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002144 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002145static int do_directive(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00002146{
H. Peter Anvin4169a472007-09-12 01:29:43 +00002147 enum preproc_token i;
2148 int j;
H. Peter Anvin70055962007-10-11 00:05:31 -07002149 bool err;
2150 int nparam;
2151 bool nolist;
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07002152 bool casesense;
H. Peter Anvin8cfdb9d2007-09-14 18:36:01 -07002153 int k, m;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002154 int offset;
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08002155 char *p, *pp;
2156 const char *mname;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002157 Include *inc;
2158 Context *ctx;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002159 Cond *cond;
2160 MMacro *mmac, **mmhead;
Jin Kyu Songc9486b92013-10-28 17:07:57 -07002161 Token *t = NULL, *tt, *param_start, *macro_start, *last, **tptr, *origline;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002162 Line *l;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002163 struct tokenval tokval;
2164 expr *evalresult;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002165 MMacro *tmp_defining; /* Used when manipulating rep_nest */
H. Peter Anvinf8ba53e2007-10-11 10:11:57 -07002166 int64_t count;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07002167 size_t len;
H. Peter Anvin8e3f75e2008-09-24 00:21:58 -07002168 int severity;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002169
2170 origline = tline;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002171
H. Peter Anvineba20a72002-04-30 20:53:55 +00002172 skip_white_(tline);
H. Peter Anvinf2936d72008-06-04 15:11:23 -07002173 if (!tline || !tok_type_(tline, TOK_PREPROC_ID) ||
H. Peter Anvine2c80182005-01-15 22:15:51 +00002174 (tline->text[1] == '%' || tline->text[1] == '$'
2175 || tline->text[1] == '!'))
2176 return NO_DIRECTIVE_FOUND;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002177
H. Peter Anvin4169a472007-09-12 01:29:43 +00002178 i = pp_token_hash(tline->text);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002179
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002180 /*
2181 * FIXME: We zap execution of PP_RMACRO, PP_IRMACRO, PP_EXITMACRO
2182 * since they are known to be buggy at moment, we need to fix them
2183 * in future release (2.09-2.10)
2184 */
Philipp Klokeb432f572013-03-31 12:01:23 +02002185 if (i == PP_RMACRO || i == PP_IRMACRO || i == PP_EXITMACRO) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002186 error(ERR_NONFATAL, "unknown preprocessor directive `%s'",
2187 tline->text);
2188 return NO_DIRECTIVE_FOUND;
2189 }
2190
2191 /*
2192 * If we're in a non-emitting branch of a condition construct,
2193 * or walking to the end of an already terminated %rep block,
2194 * we should ignore all directives except for condition
2195 * directives.
2196 */
2197 if (((istk->conds && !emitting(istk->conds->state)) ||
2198 (istk->mstk && !istk->mstk->in_progress)) && !is_condition(i)) {
2199 return NO_DIRECTIVE_FOUND;
2200 }
2201
2202 /*
2203 * If we're defining a macro or reading a %rep block, we should
2204 * ignore all directives except for %macro/%imacro (which nest),
2205 * %endm/%endmacro, and (only if we're in a %rep block) %endrep.
2206 * If we're in a %rep block, another %rep nests, so should be let through.
2207 */
2208 if (defining && i != PP_MACRO && i != PP_IMACRO &&
2209 i != PP_RMACRO && i != PP_IRMACRO &&
2210 i != PP_ENDMACRO && i != PP_ENDM &&
2211 (defining->name || (i != PP_ENDREP && i != PP_REP))) {
2212 return NO_DIRECTIVE_FOUND;
2213 }
2214
2215 if (defining) {
2216 if (i == PP_MACRO || i == PP_IMACRO ||
2217 i == PP_RMACRO || i == PP_IRMACRO) {
2218 nested_mac_count++;
2219 return NO_DIRECTIVE_FOUND;
2220 } else if (nested_mac_count > 0) {
2221 if (i == PP_ENDMACRO) {
2222 nested_mac_count--;
2223 return NO_DIRECTIVE_FOUND;
2224 }
2225 }
2226 if (!defining->name) {
2227 if (i == PP_REP) {
2228 nested_rep_count++;
2229 return NO_DIRECTIVE_FOUND;
2230 } else if (nested_rep_count > 0) {
2231 if (i == PP_ENDREP) {
2232 nested_rep_count--;
2233 return NO_DIRECTIVE_FOUND;
2234 }
2235 }
2236 }
2237 }
2238
H. Peter Anvin4169a472007-09-12 01:29:43 +00002239 switch (i) {
2240 case PP_INVALID:
H. Peter Anvine2c80182005-01-15 22:15:51 +00002241 error(ERR_NONFATAL, "unknown preprocessor directive `%s'",
2242 tline->text);
2243 return NO_DIRECTIVE_FOUND; /* didn't get it */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002244
H. Peter Anvine2c80182005-01-15 22:15:51 +00002245 case PP_STACKSIZE:
2246 /* Directive to tell NASM what the default stack size is. The
2247 * default is for a 16-bit stack, and this can be overriden with
2248 * %stacksize large.
H. Peter Anvine2c80182005-01-15 22:15:51 +00002249 */
2250 tline = tline->next;
2251 if (tline && tline->type == TOK_WHITESPACE)
2252 tline = tline->next;
2253 if (!tline || tline->type != TOK_ID) {
2254 error(ERR_NONFATAL, "`%%stacksize' missing size parameter");
2255 free_tlist(origline);
2256 return DIRECTIVE_FOUND;
2257 }
2258 if (nasm_stricmp(tline->text, "flat") == 0) {
2259 /* All subsequent ARG directives are for a 32-bit stack */
2260 StackSize = 4;
2261 StackPointer = "ebp";
2262 ArgOffset = 8;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002263 LocalOffset = 0;
Charles Crayne7eaf9192007-11-08 22:11:14 -08002264 } else if (nasm_stricmp(tline->text, "flat64") == 0) {
2265 /* All subsequent ARG directives are for a 64-bit stack */
2266 StackSize = 8;
2267 StackPointer = "rbp";
Per Jessen53252e02010-02-11 00:16:59 +03002268 ArgOffset = 16;
Charles Crayne7eaf9192007-11-08 22:11:14 -08002269 LocalOffset = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002270 } else if (nasm_stricmp(tline->text, "large") == 0) {
2271 /* All subsequent ARG directives are for a 16-bit stack,
2272 * far function call.
2273 */
2274 StackSize = 2;
2275 StackPointer = "bp";
2276 ArgOffset = 4;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002277 LocalOffset = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002278 } else if (nasm_stricmp(tline->text, "small") == 0) {
2279 /* All subsequent ARG directives are for a 16-bit stack,
2280 * far function call. We don't support near functions.
2281 */
2282 StackSize = 2;
2283 StackPointer = "bp";
2284 ArgOffset = 6;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002285 LocalOffset = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002286 } else {
2287 error(ERR_NONFATAL, "`%%stacksize' invalid size type");
2288 free_tlist(origline);
2289 return DIRECTIVE_FOUND;
2290 }
2291 free_tlist(origline);
2292 return DIRECTIVE_FOUND;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002293
H. Peter Anvine2c80182005-01-15 22:15:51 +00002294 case PP_ARG:
2295 /* TASM like ARG directive to define arguments to functions, in
2296 * the following form:
2297 *
2298 * ARG arg1:WORD, arg2:DWORD, arg4:QWORD
2299 */
2300 offset = ArgOffset;
2301 do {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00002302 char *arg, directive[256];
H. Peter Anvine2c80182005-01-15 22:15:51 +00002303 int size = StackSize;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002304
H. Peter Anvine2c80182005-01-15 22:15:51 +00002305 /* Find the argument name */
2306 tline = tline->next;
2307 if (tline && tline->type == TOK_WHITESPACE)
2308 tline = tline->next;
2309 if (!tline || tline->type != TOK_ID) {
2310 error(ERR_NONFATAL, "`%%arg' missing argument parameter");
2311 free_tlist(origline);
2312 return DIRECTIVE_FOUND;
2313 }
2314 arg = tline->text;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002315
H. Peter Anvine2c80182005-01-15 22:15:51 +00002316 /* Find the argument size type */
2317 tline = tline->next;
2318 if (!tline || tline->type != TOK_OTHER
2319 || tline->text[0] != ':') {
2320 error(ERR_NONFATAL,
2321 "Syntax error processing `%%arg' directive");
2322 free_tlist(origline);
2323 return DIRECTIVE_FOUND;
2324 }
2325 tline = tline->next;
2326 if (!tline || tline->type != TOK_ID) {
2327 error(ERR_NONFATAL, "`%%arg' missing size type parameter");
2328 free_tlist(origline);
2329 return DIRECTIVE_FOUND;
2330 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002331
H. Peter Anvine2c80182005-01-15 22:15:51 +00002332 /* Allow macro expansion of type parameter */
Keith Kaniosb7a89542007-04-12 02:40:54 +00002333 tt = tokenize(tline->text);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002334 tt = expand_smacro(tt);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002335 size = parse_size(tt->text);
2336 if (!size) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002337 error(ERR_NONFATAL,
2338 "Invalid size type for `%%arg' missing directive");
2339 free_tlist(tt);
2340 free_tlist(origline);
2341 return DIRECTIVE_FOUND;
2342 }
2343 free_tlist(tt);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002344
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002345 /* Round up to even stack slots */
2346 size = ALIGN(size, StackSize);
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002347
H. Peter Anvine2c80182005-01-15 22:15:51 +00002348 /* Now define the macro for the argument */
2349 snprintf(directive, sizeof(directive), "%%define %s (%s+%d)",
2350 arg, StackPointer, offset);
Keith Kaniosb7a89542007-04-12 02:40:54 +00002351 do_directive(tokenize(directive));
H. Peter Anvine2c80182005-01-15 22:15:51 +00002352 offset += size;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002353
H. Peter Anvine2c80182005-01-15 22:15:51 +00002354 /* Move to the next argument in the list */
2355 tline = tline->next;
2356 if (tline && tline->type == TOK_WHITESPACE)
2357 tline = tline->next;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002358 } while (tline && tline->type == TOK_OTHER && tline->text[0] == ',');
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002359 ArgOffset = offset;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002360 free_tlist(origline);
2361 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002362
H. Peter Anvine2c80182005-01-15 22:15:51 +00002363 case PP_LOCAL:
2364 /* TASM like LOCAL directive to define local variables for a
2365 * function, in the following form:
2366 *
2367 * LOCAL local1:WORD, local2:DWORD, local4:QWORD = LocalSize
2368 *
2369 * The '= LocalSize' at the end is ignored by NASM, but is
2370 * required by TASM to define the local parameter size (and used
2371 * by the TASM macro package).
2372 */
2373 offset = LocalOffset;
2374 do {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00002375 char *local, directive[256];
H. Peter Anvine2c80182005-01-15 22:15:51 +00002376 int size = StackSize;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002377
H. Peter Anvine2c80182005-01-15 22:15:51 +00002378 /* Find the argument name */
2379 tline = tline->next;
2380 if (tline && tline->type == TOK_WHITESPACE)
2381 tline = tline->next;
2382 if (!tline || tline->type != TOK_ID) {
2383 error(ERR_NONFATAL,
2384 "`%%local' missing argument parameter");
2385 free_tlist(origline);
2386 return DIRECTIVE_FOUND;
2387 }
2388 local = tline->text;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002389
H. Peter Anvine2c80182005-01-15 22:15:51 +00002390 /* Find the argument size type */
2391 tline = tline->next;
2392 if (!tline || tline->type != TOK_OTHER
2393 || tline->text[0] != ':') {
2394 error(ERR_NONFATAL,
2395 "Syntax error processing `%%local' directive");
2396 free_tlist(origline);
2397 return DIRECTIVE_FOUND;
2398 }
2399 tline = tline->next;
2400 if (!tline || tline->type != TOK_ID) {
2401 error(ERR_NONFATAL,
2402 "`%%local' missing size type parameter");
2403 free_tlist(origline);
2404 return DIRECTIVE_FOUND;
2405 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00002406
H. Peter Anvine2c80182005-01-15 22:15:51 +00002407 /* Allow macro expansion of type parameter */
Keith Kaniosb7a89542007-04-12 02:40:54 +00002408 tt = tokenize(tline->text);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002409 tt = expand_smacro(tt);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002410 size = parse_size(tt->text);
2411 if (!size) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002412 error(ERR_NONFATAL,
2413 "Invalid size type for `%%local' missing directive");
2414 free_tlist(tt);
2415 free_tlist(origline);
2416 return DIRECTIVE_FOUND;
2417 }
2418 free_tlist(tt);
H. Peter Anvin734b1882002-04-30 21:01:08 +00002419
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002420 /* Round up to even stack slots */
2421 size = ALIGN(size, StackSize);
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002422
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002423 offset += size; /* Negative offset, increment before */
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002424
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002425 /* Now define the macro for the argument */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002426 snprintf(directive, sizeof(directive), "%%define %s (%s-%d)",
2427 local, StackPointer, offset);
Keith Kaniosb7a89542007-04-12 02:40:54 +00002428 do_directive(tokenize(directive));
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002429
H. Peter Anvine2c80182005-01-15 22:15:51 +00002430 /* Now define the assign to setup the enter_c macro correctly */
2431 snprintf(directive, sizeof(directive),
2432 "%%assign %%$localsize %%$localsize+%d", size);
Keith Kaniosb7a89542007-04-12 02:40:54 +00002433 do_directive(tokenize(directive));
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002434
H. Peter Anvine2c80182005-01-15 22:15:51 +00002435 /* Move to the next argument in the list */
2436 tline = tline->next;
2437 if (tline && tline->type == TOK_WHITESPACE)
2438 tline = tline->next;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002439 } while (tline && tline->type == TOK_OTHER && tline->text[0] == ',');
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002440 LocalOffset = offset;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002441 free_tlist(origline);
2442 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002443
H. Peter Anvine2c80182005-01-15 22:15:51 +00002444 case PP_CLEAR:
2445 if (tline->next)
H. Peter Anvin917a3492008-09-24 09:14:49 -07002446 error(ERR_WARNING|ERR_PASS1,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002447 "trailing garbage after `%%clear' ignored");
2448 free_macros();
2449 init_macros();
H. Peter Anvine2c80182005-01-15 22:15:51 +00002450 free_tlist(origline);
2451 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002452
H. Peter Anvin418ca702008-05-30 10:42:30 -07002453 case PP_DEPEND:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002454 t = tline->next = expand_smacro(tline->next);
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002455 skip_white_(t);
2456 if (!t || (t->type != TOK_STRING &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002457 t->type != TOK_INTERNAL_STRING)) {
H. Peter Anvin418ca702008-05-30 10:42:30 -07002458 error(ERR_NONFATAL, "`%%depend' expects a file name");
2459 free_tlist(origline);
2460 return DIRECTIVE_FOUND; /* but we did _something_ */
2461 }
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002462 if (t->next)
H. Peter Anvin917a3492008-09-24 09:14:49 -07002463 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvin418ca702008-05-30 10:42:30 -07002464 "trailing garbage after `%%depend' ignored");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002465 p = t->text;
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002466 if (t->type != TOK_INTERNAL_STRING)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002467 nasm_unquote_cstr(p, i);
2468 if (dephead && !in_list(*dephead, p)) {
2469 StrList *sl = nasm_malloc(strlen(p)+1+sizeof sl->next);
2470 sl->next = NULL;
2471 strcpy(sl->str, p);
2472 *deptail = sl;
2473 deptail = &sl->next;
2474 }
2475 free_tlist(origline);
H. Peter Anvin418ca702008-05-30 10:42:30 -07002476 return DIRECTIVE_FOUND;
2477
2478 case PP_INCLUDE:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002479 t = tline->next = expand_smacro(tline->next);
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002480 skip_white_(t);
H. Peter Anvind2456592008-06-19 15:04:18 -07002481
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002482 if (!t || (t->type != TOK_STRING &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002483 t->type != TOK_INTERNAL_STRING)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002484 error(ERR_NONFATAL, "`%%include' expects a file name");
2485 free_tlist(origline);
2486 return DIRECTIVE_FOUND; /* but we did _something_ */
2487 }
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002488 if (t->next)
H. Peter Anvin917a3492008-09-24 09:14:49 -07002489 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +00002490 "trailing garbage after `%%include' ignored");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002491 p = t->text;
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002492 if (t->type != TOK_INTERNAL_STRING)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002493 nasm_unquote_cstr(p, i);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002494 inc = nasm_malloc(sizeof(Include));
H. Peter Anvine2c80182005-01-15 22:15:51 +00002495 inc->next = istk;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002496 inc->conds = NULL;
H. Peter Anvin2b1c3b92008-06-06 10:38:46 -07002497 inc->fp = inc_fopen(p, dephead, &deptail, pass == 0);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002498 if (!inc->fp) {
2499 /* -MG given but file not found */
2500 nasm_free(inc);
2501 } else {
2502 inc->fname = src_set_fname(nasm_strdup(p));
2503 inc->lineno = src_set_linnum(0);
2504 inc->lineinc = 1;
2505 inc->expansion = NULL;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002506 inc->mstk = NULL;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002507 istk = inc;
2508 list->uplevel(LIST_INCLUDE);
2509 }
2510 free_tlist(origline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002511 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002512
H. Peter Anvind2456592008-06-19 15:04:18 -07002513 case PP_USE:
H. Peter Anvinf4ae5ad2008-06-19 18:39:24 -07002514 {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002515 static macros_t *use_pkg;
2516 const char *pkg_macro = NULL;
H. Peter Anvinf4ae5ad2008-06-19 18:39:24 -07002517
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002518 tline = tline->next;
2519 skip_white_(tline);
2520 tline = expand_id(tline);
H. Peter Anvind2456592008-06-19 15:04:18 -07002521
H. Peter Anvin264b7b92008-10-24 16:38:17 -07002522 if (!tline || (tline->type != TOK_STRING &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002523 tline->type != TOK_INTERNAL_STRING &&
2524 tline->type != TOK_ID)) {
H. Peter Anvin926fc402008-06-19 16:26:12 -07002525 error(ERR_NONFATAL, "`%%use' expects a package name");
H. Peter Anvind2456592008-06-19 15:04:18 -07002526 free_tlist(origline);
2527 return DIRECTIVE_FOUND; /* but we did _something_ */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002528 }
H. Peter Anvin264b7b92008-10-24 16:38:17 -07002529 if (tline->next)
H. Peter Anvin917a3492008-09-24 09:14:49 -07002530 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvind2456592008-06-19 15:04:18 -07002531 "trailing garbage after `%%use' ignored");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002532 if (tline->type == TOK_STRING)
2533 nasm_unquote_cstr(tline->text, i);
2534 use_pkg = nasm_stdmac_find_package(tline->text);
2535 if (!use_pkg)
2536 error(ERR_NONFATAL, "unknown `%%use' package: %s", tline->text);
2537 else
2538 pkg_macro = (char *)use_pkg + 1; /* The first string will be <%define>__USE_*__ */
Victor van den Elzen35eb2ea2010-03-10 22:33:48 +01002539 if (use_pkg && ! smacro_defined(NULL, pkg_macro, 0, NULL, true)) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002540 /* Not already included, go ahead and include it */
2541 stdmacpos = use_pkg;
2542 }
2543 free_tlist(origline);
H. Peter Anvind2456592008-06-19 15:04:18 -07002544 return DIRECTIVE_FOUND;
H. Peter Anvinf4ae5ad2008-06-19 18:39:24 -07002545 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002546 case PP_PUSH:
H. Peter Anvine2c80182005-01-15 22:15:51 +00002547 case PP_REPL:
H. Peter Anvin42b56392008-10-24 16:24:21 -07002548 case PP_POP:
H. Peter Anvine2c80182005-01-15 22:15:51 +00002549 tline = tline->next;
2550 skip_white_(tline);
2551 tline = expand_id(tline);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002552 if (tline) {
2553 if (!tok_type_(tline, TOK_ID)) {
2554 error(ERR_NONFATAL, "`%s' expects a context identifier",
2555 pp_directives[i]);
2556 free_tlist(origline);
2557 return DIRECTIVE_FOUND; /* but we did _something_ */
2558 }
2559 if (tline->next)
2560 error(ERR_WARNING|ERR_PASS1,
2561 "trailing garbage after `%s' ignored",
2562 pp_directives[i]);
2563 p = nasm_strdup(tline->text);
2564 } else {
2565 p = NULL; /* Anonymous */
2566 }
H. Peter Anvin42b56392008-10-24 16:24:21 -07002567
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002568 if (i == PP_PUSH) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002569 ctx = nasm_malloc(sizeof(Context));
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002570 ctx->next = cstk;
2571 hash_init(&ctx->localmac, HASH_SMALL);
2572 ctx->name = p;
2573 ctx->number = unique++;
2574 cstk = ctx;
2575 } else {
2576 /* %pop or %repl */
2577 if (!cstk) {
2578 error(ERR_NONFATAL, "`%s': context stack is empty",
2579 pp_directives[i]);
2580 } else if (i == PP_POP) {
2581 if (p && (!cstk->name || nasm_stricmp(p, cstk->name)))
2582 error(ERR_NONFATAL, "`%%pop' in wrong context: %s, "
2583 "expected %s",
2584 cstk->name ? cstk->name : "anonymous", p);
2585 else
2586 ctx_pop();
2587 } else {
2588 /* i == PP_REPL */
2589 nasm_free(cstk->name);
2590 cstk->name = p;
2591 p = NULL;
2592 }
2593 nasm_free(p);
2594 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002595 free_tlist(origline);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002596 return DIRECTIVE_FOUND;
H. Peter Anvin8e3f75e2008-09-24 00:21:58 -07002597 case PP_FATAL:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002598 severity = ERR_FATAL;
2599 goto issue_error;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002600 case PP_ERROR:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002601 severity = ERR_NONFATAL;
2602 goto issue_error;
H. Peter Anvin7df04172008-06-10 18:27:38 -07002603 case PP_WARNING:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002604 severity = ERR_WARNING|ERR_WARN_USER;
2605 goto issue_error;
H. Peter Anvin8e3f75e2008-09-24 00:21:58 -07002606
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002607issue_error:
H. Peter Anvin7df04172008-06-10 18:27:38 -07002608 {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002609 /* Only error out if this is the final pass */
2610 if (pass != 2 && i != PP_FATAL)
2611 return DIRECTIVE_FOUND;
2612
2613 tline->next = expand_smacro(tline->next);
2614 tline = tline->next;
2615 skip_white_(tline);
2616 t = tline ? tline->next : NULL;
2617 skip_white_(t);
2618 if (tok_type_(tline, TOK_STRING) && !t) {
2619 /* The line contains only a quoted string */
2620 p = tline->text;
2621 nasm_unquote(p, NULL); /* Ignore NUL character truncation */
2622 error(severity, "%s", p);
2623 } else {
2624 /* Not a quoted string, or more than a quoted string */
2625 p = detoken(tline, false);
2626 error(severity, "%s", p);
2627 nasm_free(p);
2628 }
2629 free_tlist(origline);
2630 return DIRECTIVE_FOUND;
H. Peter Anvin7df04172008-06-10 18:27:38 -07002631 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00002632
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002633 CASE_PP_IF:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002634 if (istk->conds && !emitting(istk->conds->state))
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002635 j = COND_NEVER;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002636 else {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002637 j = if_condition(tline->next, i);
2638 tline->next = NULL; /* it got freed */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002639 j = j < 0 ? COND_NEVER : j ? COND_IF_TRUE : COND_IF_FALSE;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002640 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002641 cond = nasm_malloc(sizeof(Cond));
2642 cond->next = istk->conds;
2643 cond->state = j;
2644 istk->conds = cond;
2645 if(istk->mstk)
2646 istk->mstk->condcnt ++;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002647 free_tlist(origline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002648 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002649
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002650 CASE_PP_ELIF:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002651 if (!istk->conds)
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002652 error(ERR_FATAL, "`%s': no matching `%%if'", pp_directives[i]);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002653 switch(istk->conds->state) {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002654 case COND_IF_TRUE:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002655 istk->conds->state = COND_DONE;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002656 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002657
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002658 case COND_DONE:
2659 case COND_NEVER:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002660 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002661
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002662 case COND_ELSE_TRUE:
2663 case COND_ELSE_FALSE:
2664 error_precond(ERR_WARNING|ERR_PASS1,
2665 "`%%elif' after `%%else' ignored");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002666 istk->conds->state = COND_NEVER;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002667 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002668
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002669 case COND_IF_FALSE:
2670 /*
2671 * IMPORTANT: In the case of %if, we will already have
2672 * called expand_mmac_params(); however, if we're
2673 * processing an %elif we must have been in a
2674 * non-emitting mode, which would have inhibited
2675 * the normal invocation of expand_mmac_params().
2676 * Therefore, we have to do it explicitly here.
2677 */
2678 j = if_condition(expand_mmac_params(tline->next), i);
2679 tline->next = NULL; /* it got freed */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002680 istk->conds->state =
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002681 j < 0 ? COND_NEVER : j ? COND_IF_TRUE : COND_IF_FALSE;
2682 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002683 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002684 free_tlist(origline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002685 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002686
H. Peter Anvine2c80182005-01-15 22:15:51 +00002687 case PP_ELSE:
2688 if (tline->next)
H. Peter Anvin917a3492008-09-24 09:14:49 -07002689 error_precond(ERR_WARNING|ERR_PASS1,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002690 "trailing garbage after `%%else' ignored");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002691 if (!istk->conds)
2692 error(ERR_FATAL, "`%%else': no matching `%%if'");
2693 switch(istk->conds->state) {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002694 case COND_IF_TRUE:
2695 case COND_DONE:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002696 istk->conds->state = COND_ELSE_FALSE;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002697 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002698
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002699 case COND_NEVER:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002700 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002701
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002702 case COND_IF_FALSE:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002703 istk->conds->state = COND_ELSE_TRUE;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002704 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002705
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002706 case COND_ELSE_TRUE:
2707 case COND_ELSE_FALSE:
2708 error_precond(ERR_WARNING|ERR_PASS1,
2709 "`%%else' after `%%else' ignored.");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002710 istk->conds->state = COND_NEVER;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002711 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002712 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002713 free_tlist(origline);
2714 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002715
H. Peter Anvine2c80182005-01-15 22:15:51 +00002716 case PP_ENDIF:
2717 if (tline->next)
H. Peter Anvin917a3492008-09-24 09:14:49 -07002718 error_precond(ERR_WARNING|ERR_PASS1,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002719 "trailing garbage after `%%endif' ignored");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002720 if (!istk->conds)
2721 error(ERR_FATAL, "`%%endif': no matching `%%if'");
2722 cond = istk->conds;
2723 istk->conds = cond->next;
2724 nasm_free(cond);
2725 if(istk->mstk)
2726 istk->mstk->condcnt --;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002727 free_tlist(origline);
2728 return DIRECTIVE_FOUND;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002729
H. Peter Anvindb8f96e2009-07-15 09:07:29 -04002730 case PP_RMACRO:
2731 case PP_IRMACRO:
H. Peter Anvine2c80182005-01-15 22:15:51 +00002732 case PP_MACRO:
2733 case PP_IMACRO:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002734 if (defining) {
2735 error(ERR_FATAL, "`%s': already defining a macro",
2736 pp_directives[i]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002737 return DIRECTIVE_FOUND;
2738 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002739 defining = nasm_malloc(sizeof(MMacro));
2740 defining->max_depth =
2741 (i == PP_RMACRO) || (i == PP_IRMACRO) ? DEADMAN_LIMIT : 0;
2742 defining->casesense = (i == PP_MACRO) || (i == PP_RMACRO);
2743 if (!parse_mmacro_spec(tline, defining, pp_directives[i])) {
2744 nasm_free(defining);
2745 defining = NULL;
2746 return DIRECTIVE_FOUND;
2747 }
H. Peter Anvina26433d2008-07-16 14:40:01 -07002748
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002749 mmac = (MMacro *) hash_findix(&mmacros, defining->name);
2750 while (mmac) {
2751 if (!strcmp(mmac->name, defining->name) &&
2752 (mmac->nparam_min <= defining->nparam_max
2753 || defining->plus)
2754 && (defining->nparam_min <= mmac->nparam_max
2755 || mmac->plus)) {
2756 error(ERR_WARNING|ERR_PASS1,
2757 "redefining multi-line macro `%s'", defining->name);
2758 return DIRECTIVE_FOUND;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002759 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002760 mmac = mmac->next;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002761 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002762 free_tlist(origline);
2763 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002764
H. Peter Anvine2c80182005-01-15 22:15:51 +00002765 case PP_ENDM:
2766 case PP_ENDMACRO:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002767 if (! (defining && defining->name)) {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002768 error(ERR_NONFATAL, "`%s': not defining a macro", tline->text);
2769 return DIRECTIVE_FOUND;
2770 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002771 mmhead = (MMacro **) hash_findi_add(&mmacros, defining->name);
2772 defining->next = *mmhead;
2773 *mmhead = defining;
2774 defining = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002775 free_tlist(origline);
2776 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002777
H. Peter Anvin89cee572009-07-15 09:16:54 -04002778 case PP_EXITMACRO:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002779 /*
2780 * We must search along istk->expansion until we hit a
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002781 * macro-end marker for a macro with a name. Then we
2782 * bypass all lines between exitmacro and endmacro.
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002783 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002784 list_for_each(l, istk->expansion)
2785 if (l->finishes && l->finishes->name)
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002786 break;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002787
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002788 if (l) {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002789 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002790 * Remove all conditional entries relative to this
2791 * macro invocation. (safe to do in this context)
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002792 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002793 for ( ; l->finishes->condcnt > 0; l->finishes->condcnt --) {
2794 cond = istk->conds;
2795 istk->conds = cond->next;
2796 nasm_free(cond);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002797 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002798 istk->expansion = l;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002799 } else {
2800 error(ERR_NONFATAL, "`%%exitmacro' not within `%%macro' block");
2801 }
Keith Kanios852f1ee2009-07-12 00:19:55 -05002802 free_tlist(origline);
2803 return DIRECTIVE_FOUND;
2804
H. Peter Anvina26433d2008-07-16 14:40:01 -07002805 case PP_UNMACRO:
2806 case PP_UNIMACRO:
2807 {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002808 MMacro **mmac_p;
2809 MMacro spec;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002810
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002811 spec.casesense = (i == PP_UNMACRO);
2812 if (!parse_mmacro_spec(tline, &spec, pp_directives[i])) {
2813 return DIRECTIVE_FOUND;
2814 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002815 mmac_p = (MMacro **) hash_findi(&mmacros, spec.name, NULL);
2816 while (mmac_p && *mmac_p) {
2817 mmac = *mmac_p;
2818 if (mmac->casesense == spec.casesense &&
2819 !mstrcmp(mmac->name, spec.name, spec.casesense) &&
2820 mmac->nparam_min == spec.nparam_min &&
2821 mmac->nparam_max == spec.nparam_max &&
2822 mmac->plus == spec.plus) {
2823 *mmac_p = mmac->next;
2824 free_mmacro(mmac);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002825 } else {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002826 mmac_p = &mmac->next;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002827 }
2828 }
2829 free_tlist(origline);
2830 free_tlist(spec.dlist);
2831 return DIRECTIVE_FOUND;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002832 }
2833
H. Peter Anvine2c80182005-01-15 22:15:51 +00002834 case PP_ROTATE:
2835 if (tline->next && tline->next->type == TOK_WHITESPACE)
2836 tline = tline->next;
H. Peter Anvin89cee572009-07-15 09:16:54 -04002837 if (!tline->next) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002838 free_tlist(origline);
2839 error(ERR_NONFATAL, "`%%rotate' missing rotate count");
2840 return DIRECTIVE_FOUND;
2841 }
2842 t = expand_smacro(tline->next);
2843 tline->next = NULL;
2844 free_tlist(origline);
2845 tline = t;
2846 tptr = &t;
2847 tokval.t_type = TOKEN_INVALID;
2848 evalresult =
2849 evaluate(ppscan, tptr, &tokval, NULL, pass, error, NULL);
2850 free_tlist(tline);
2851 if (!evalresult)
2852 return DIRECTIVE_FOUND;
2853 if (tokval.t_type)
H. Peter Anvin917a3492008-09-24 09:14:49 -07002854 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +00002855 "trailing garbage after expression ignored");
2856 if (!is_simple(evalresult)) {
2857 error(ERR_NONFATAL, "non-constant value given to `%%rotate'");
2858 return DIRECTIVE_FOUND;
2859 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002860 mmac = istk->mstk;
2861 while (mmac && !mmac->name) /* avoid mistaking %reps for macros */
2862 mmac = mmac->next_active;
2863 if (!mmac) {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002864 error(ERR_NONFATAL, "`%%rotate' invoked outside a macro call");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002865 } else if (mmac->nparam == 0) {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002866 error(ERR_NONFATAL,
2867 "`%%rotate' invoked within macro without parameters");
2868 } else {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002869 int rotate = mmac->rotate + reloc_value(evalresult);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002870
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002871 rotate %= (int)mmac->nparam;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002872 if (rotate < 0)
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002873 rotate += mmac->nparam;
2874
2875 mmac->rotate = rotate;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002876 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002877 return DIRECTIVE_FOUND;
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00002878
H. Peter Anvine2c80182005-01-15 22:15:51 +00002879 case PP_REP:
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002880 nolist = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002881 do {
2882 tline = tline->next;
2883 } while (tok_type_(tline, TOK_WHITESPACE));
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00002884
H. Peter Anvine2c80182005-01-15 22:15:51 +00002885 if (tok_type_(tline, TOK_ID) &&
2886 nasm_stricmp(tline->text, ".nolist") == 0) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002887 nolist = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002888 do {
2889 tline = tline->next;
2890 } while (tok_type_(tline, TOK_WHITESPACE));
2891 }
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00002892
H. Peter Anvine2c80182005-01-15 22:15:51 +00002893 if (tline) {
2894 t = expand_smacro(tline);
2895 tptr = &t;
2896 tokval.t_type = TOKEN_INVALID;
2897 evalresult =
2898 evaluate(ppscan, tptr, &tokval, NULL, pass, error, NULL);
2899 if (!evalresult) {
2900 free_tlist(origline);
2901 return DIRECTIVE_FOUND;
2902 }
2903 if (tokval.t_type)
H. Peter Anvin917a3492008-09-24 09:14:49 -07002904 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +00002905 "trailing garbage after expression ignored");
2906 if (!is_simple(evalresult)) {
2907 error(ERR_NONFATAL, "non-constant value given to `%%rep'");
2908 return DIRECTIVE_FOUND;
2909 }
Cyrill Gorcunove091d6e2010-08-09 13:58:22 +04002910 count = reloc_value(evalresult);
2911 if (count >= REP_LIMIT) {
Cyrill Gorcunov71f4f842010-08-09 20:17:17 +04002912 error(ERR_NONFATAL, "`%%rep' value exceeds limit");
Cyrill Gorcunove091d6e2010-08-09 13:58:22 +04002913 count = 0;
2914 } else
2915 count++;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002916 } else {
2917 error(ERR_NONFATAL, "`%%rep' expects a repeat count");
H. Peter Anvinf8ba53e2007-10-11 10:11:57 -07002918 count = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002919 }
2920 free_tlist(origline);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002921
2922 tmp_defining = defining;
2923 defining = nasm_malloc(sizeof(MMacro));
2924 defining->prev = NULL;
2925 defining->name = NULL; /* flags this macro as a %rep block */
2926 defining->casesense = false;
2927 defining->plus = false;
2928 defining->nolist = nolist;
2929 defining->in_progress = count;
2930 defining->max_depth = 0;
2931 defining->nparam_min = defining->nparam_max = 0;
2932 defining->defaults = NULL;
2933 defining->dlist = NULL;
2934 defining->expansion = NULL;
2935 defining->next_active = istk->mstk;
2936 defining->rep_nest = tmp_defining;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002937 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002938
H. Peter Anvine2c80182005-01-15 22:15:51 +00002939 case PP_ENDREP:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002940 if (!defining || defining->name) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002941 error(ERR_NONFATAL, "`%%endrep': no matching `%%rep'");
2942 return DIRECTIVE_FOUND;
2943 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00002944
H. Peter Anvine2c80182005-01-15 22:15:51 +00002945 /*
2946 * Now we have a "macro" defined - although it has no name
2947 * and we won't be entering it in the hash tables - we must
2948 * push a macro-end marker for it on to istk->expansion.
2949 * After that, it will take care of propagating itself (a
2950 * macro-end marker line for a macro which is really a %rep
2951 * block will cause the macro to be re-expanded, complete
2952 * with another macro-end marker to ensure the process
2953 * continues) until the whole expansion is forcibly removed
2954 * from istk->expansion by a %exitrep.
2955 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002956 l = nasm_malloc(sizeof(Line));
2957 l->next = istk->expansion;
2958 l->finishes = defining;
2959 l->first = NULL;
2960 istk->expansion = l;
2961
2962 istk->mstk = defining;
2963
2964 list->uplevel(defining->nolist ? LIST_MACRO_NOLIST : LIST_MACRO);
2965 tmp_defining = defining;
2966 defining = defining->rep_nest;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002967 free_tlist(origline);
2968 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002969
H. Peter Anvine2c80182005-01-15 22:15:51 +00002970 case PP_EXITREP:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002971 /*
2972 * We must search along istk->expansion until we hit a
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002973 * macro-end marker for a macro with no name. Then we set
2974 * its `in_progress' flag to 0.
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002975 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002976 list_for_each(l, istk->expansion)
2977 if (l->finishes && !l->finishes->name)
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002978 break;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002979
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002980 if (l)
2981 l->finishes->in_progress = 1;
2982 else
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002983 error(ERR_NONFATAL, "`%%exitrep' not within `%%rep' block");
H. Peter Anvine2c80182005-01-15 22:15:51 +00002984 free_tlist(origline);
2985 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002986
H. Peter Anvine2c80182005-01-15 22:15:51 +00002987 case PP_XDEFINE:
2988 case PP_IXDEFINE:
2989 case PP_DEFINE:
2990 case PP_IDEFINE:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002991 casesense = (i == PP_DEFINE || i == PP_XDEFINE);
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07002992
H. Peter Anvine2c80182005-01-15 22:15:51 +00002993 tline = tline->next;
2994 skip_white_(tline);
2995 tline = expand_id(tline);
2996 if (!tline || (tline->type != TOK_ID &&
2997 (tline->type != TOK_PREPROC_ID ||
2998 tline->text[1] != '$'))) {
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07002999 error(ERR_NONFATAL, "`%s' expects a macro identifier",
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003000 pp_directives[i]);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003001 free_tlist(origline);
3002 return DIRECTIVE_FOUND;
3003 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003004
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04003005 ctx = get_ctx(tline->text, &mname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003006 last = tline;
3007 param_start = tline = tline->next;
3008 nparam = 0;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003009
H. Peter Anvine2c80182005-01-15 22:15:51 +00003010 /* Expand the macro definition now for %xdefine and %ixdefine */
3011 if ((i == PP_XDEFINE) || (i == PP_IXDEFINE))
3012 tline = expand_smacro(tline);
H. Peter Anvin734b1882002-04-30 21:01:08 +00003013
H. Peter Anvine2c80182005-01-15 22:15:51 +00003014 if (tok_is_(tline, "(")) {
3015 /*
3016 * This macro has parameters.
3017 */
H. Peter Anvin734b1882002-04-30 21:01:08 +00003018
H. Peter Anvine2c80182005-01-15 22:15:51 +00003019 tline = tline->next;
3020 while (1) {
3021 skip_white_(tline);
3022 if (!tline) {
3023 error(ERR_NONFATAL, "parameter identifier expected");
3024 free_tlist(origline);
3025 return DIRECTIVE_FOUND;
3026 }
3027 if (tline->type != TOK_ID) {
3028 error(ERR_NONFATAL,
3029 "`%s': parameter identifier expected",
3030 tline->text);
3031 free_tlist(origline);
3032 return DIRECTIVE_FOUND;
3033 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003034 tline->type = TOK_SMAC_PARAM + nparam++;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003035 tline = tline->next;
3036 skip_white_(tline);
3037 if (tok_is_(tline, ",")) {
3038 tline = tline->next;
H. Peter Anvinca348b62008-07-23 10:49:26 -04003039 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003040 if (!tok_is_(tline, ")")) {
3041 error(ERR_NONFATAL,
3042 "`)' expected to terminate macro template");
3043 free_tlist(origline);
3044 return DIRECTIVE_FOUND;
3045 }
3046 break;
3047 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00003048 }
3049 last = tline;
3050 tline = tline->next;
3051 }
3052 if (tok_type_(tline, TOK_WHITESPACE))
3053 last = tline, tline = tline->next;
3054 macro_start = NULL;
3055 last->next = NULL;
3056 t = tline;
3057 while (t) {
3058 if (t->type == TOK_ID) {
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04003059 list_for_each(tt, param_start)
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003060 if (tt->type >= TOK_SMAC_PARAM &&
H. Peter Anvine2c80182005-01-15 22:15:51 +00003061 !strcmp(tt->text, t->text))
3062 t->type = tt->type;
3063 }
3064 tt = t->next;
3065 t->next = macro_start;
3066 macro_start = t;
3067 t = tt;
3068 }
3069 /*
3070 * Good. We now have a macro name, a parameter count, and a
3071 * token list (in reverse order) for an expansion. We ought
3072 * to be OK just to create an SMacro, store it, and let
3073 * free_tlist have the rest of the line (which we have
3074 * carefully re-terminated after chopping off the expansion
3075 * from the end).
3076 */
H. Peter Anvin4db5a162007-10-11 13:42:09 -07003077 define_smacro(ctx, mname, casesense, nparam, macro_start);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003078 free_tlist(origline);
3079 return DIRECTIVE_FOUND;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003080
H. Peter Anvine2c80182005-01-15 22:15:51 +00003081 case PP_UNDEF:
3082 tline = tline->next;
3083 skip_white_(tline);
3084 tline = expand_id(tline);
3085 if (!tline || (tline->type != TOK_ID &&
3086 (tline->type != TOK_PREPROC_ID ||
3087 tline->text[1] != '$'))) {
3088 error(ERR_NONFATAL, "`%%undef' expects a macro identifier");
3089 free_tlist(origline);
3090 return DIRECTIVE_FOUND;
3091 }
3092 if (tline->next) {
H. Peter Anvin917a3492008-09-24 09:14:49 -07003093 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +00003094 "trailing garbage after macro name ignored");
3095 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003096
H. Peter Anvine2c80182005-01-15 22:15:51 +00003097 /* Find the context that symbol belongs to */
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04003098 ctx = get_ctx(tline->text, &mname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003099 undef_smacro(ctx, mname);
3100 free_tlist(origline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003101 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003102
H. Peter Anvin9e200162008-06-04 17:23:14 -07003103 case PP_DEFSTR:
3104 case PP_IDEFSTR:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003105 casesense = (i == PP_DEFSTR);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003106
3107 tline = tline->next;
3108 skip_white_(tline);
3109 tline = expand_id(tline);
3110 if (!tline || (tline->type != TOK_ID &&
3111 (tline->type != TOK_PREPROC_ID ||
3112 tline->text[1] != '$'))) {
3113 error(ERR_NONFATAL, "`%s' expects a macro identifier",
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003114 pp_directives[i]);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003115 free_tlist(origline);
3116 return DIRECTIVE_FOUND;
3117 }
3118
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04003119 ctx = get_ctx(tline->text, &mname);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003120 last = tline;
3121 tline = expand_smacro(tline->next);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003122 last->next = NULL;
H. Peter Anvin9e200162008-06-04 17:23:14 -07003123
3124 while (tok_type_(tline, TOK_WHITESPACE))
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003125 tline = delete_Token(tline);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003126
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003127 p = detoken(tline, false);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003128 macro_start = nasm_malloc(sizeof(*macro_start));
3129 macro_start->next = NULL;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003130 macro_start->text = nasm_quote(p, strlen(p));
3131 macro_start->type = TOK_STRING;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003132 macro_start->a.mac = NULL;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003133 nasm_free(p);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003134
3135 /*
3136 * We now have a macro name, an implicit parameter count of
3137 * zero, and a string token to use as an expansion. Create
3138 * and store an SMacro.
3139 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003140 define_smacro(ctx, mname, casesense, 0, macro_start);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003141 free_tlist(origline);
3142 return DIRECTIVE_FOUND;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003143
H. Peter Anvin2f55bda2009-07-14 15:04:04 -04003144 case PP_DEFTOK:
3145 case PP_IDEFTOK:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003146 casesense = (i == PP_DEFTOK);
3147
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003148 tline = tline->next;
3149 skip_white_(tline);
3150 tline = expand_id(tline);
3151 if (!tline || (tline->type != TOK_ID &&
3152 (tline->type != TOK_PREPROC_ID ||
3153 tline->text[1] != '$'))) {
3154 error(ERR_NONFATAL,
3155 "`%s' expects a macro identifier as first parameter",
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003156 pp_directives[i]);
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003157 free_tlist(origline);
3158 return DIRECTIVE_FOUND;
3159 }
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04003160 ctx = get_ctx(tline->text, &mname);
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003161 last = tline;
3162 tline = expand_smacro(tline->next);
3163 last->next = NULL;
3164
3165 t = tline;
3166 while (tok_type_(t, TOK_WHITESPACE))
3167 t = t->next;
3168 /* t should now point to the string */
Cyrill Gorcunov6908e582010-09-06 19:36:15 +04003169 if (!tok_type_(t, TOK_STRING)) {
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003170 error(ERR_NONFATAL,
3171 "`%s` requires string as second parameter",
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003172 pp_directives[i]);
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003173 free_tlist(tline);
3174 free_tlist(origline);
3175 return DIRECTIVE_FOUND;
3176 }
3177
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04003178 /*
3179 * Convert the string to a token stream. Note that smacros
3180 * are stored with the token stream reversed, so we have to
3181 * reverse the output of tokenize().
3182 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003183 nasm_unquote_cstr(t->text, i);
H. Peter Anvinb40992c2010-09-15 08:57:21 -07003184 macro_start = reverse_tokens(tokenize(t->text));
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003185
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003186 /*
3187 * We now have a macro name, an implicit parameter count of
3188 * zero, and a numeric token to use as an expansion. Create
3189 * and store an SMacro.
3190 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003191 define_smacro(ctx, mname, casesense, 0, macro_start);
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003192 free_tlist(tline);
3193 free_tlist(origline);
3194 return DIRECTIVE_FOUND;
H. Peter Anvin9e200162008-06-04 17:23:14 -07003195
H. Peter Anvin418ca702008-05-30 10:42:30 -07003196 case PP_PATHSEARCH:
3197 {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003198 FILE *fp;
3199 StrList *xsl = NULL;
3200 StrList **xst = &xsl;
H. Peter Anvin418ca702008-05-30 10:42:30 -07003201
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003202 casesense = true;
H. Peter Anvin418ca702008-05-30 10:42:30 -07003203
3204 tline = tline->next;
3205 skip_white_(tline);
3206 tline = expand_id(tline);
3207 if (!tline || (tline->type != TOK_ID &&
3208 (tline->type != TOK_PREPROC_ID ||
3209 tline->text[1] != '$'))) {
3210 error(ERR_NONFATAL,
3211 "`%%pathsearch' expects a macro identifier as first parameter");
3212 free_tlist(origline);
3213 return DIRECTIVE_FOUND;
3214 }
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04003215 ctx = get_ctx(tline->text, &mname);
H. Peter Anvin418ca702008-05-30 10:42:30 -07003216 last = tline;
3217 tline = expand_smacro(tline->next);
3218 last->next = NULL;
3219
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003220 t = tline;
H. Peter Anvin418ca702008-05-30 10:42:30 -07003221 while (tok_type_(t, TOK_WHITESPACE))
3222 t = t->next;
3223
3224 if (!t || (t->type != TOK_STRING &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003225 t->type != TOK_INTERNAL_STRING)) {
H. Peter Anvin418ca702008-05-30 10:42:30 -07003226 error(ERR_NONFATAL, "`%%pathsearch' expects a file name");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003227 free_tlist(tline);
H. Peter Anvin418ca702008-05-30 10:42:30 -07003228 free_tlist(origline);
3229 return DIRECTIVE_FOUND; /* but we did _something_ */
3230 }
3231 if (t->next)
H. Peter Anvin917a3492008-09-24 09:14:49 -07003232 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvin418ca702008-05-30 10:42:30 -07003233 "trailing garbage after `%%pathsearch' ignored");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003234 p = t->text;
H. Peter Anvin427cc912008-06-01 21:43:03 -07003235 if (t->type != TOK_INTERNAL_STRING)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003236 nasm_unquote(p, NULL);
H. Peter Anvin418ca702008-05-30 10:42:30 -07003237
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003238 fp = inc_fopen(p, &xsl, &xst, true);
3239 if (fp) {
3240 p = xsl->str;
3241 fclose(fp); /* Don't actually care about the file */
3242 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003243 macro_start = nasm_malloc(sizeof(*macro_start));
3244 macro_start->next = NULL;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003245 macro_start->text = nasm_quote(p, strlen(p));
3246 macro_start->type = TOK_STRING;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003247 macro_start->a.mac = NULL;
3248 if (xsl)
3249 nasm_free(xsl);
H. Peter Anvin418ca702008-05-30 10:42:30 -07003250
3251 /*
3252 * We now have a macro name, an implicit parameter count of
3253 * zero, and a string token to use as an expansion. Create
3254 * and store an SMacro.
3255 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003256 define_smacro(ctx, mname, casesense, 0, macro_start);
3257 free_tlist(tline);
H. Peter Anvin418ca702008-05-30 10:42:30 -07003258 free_tlist(origline);
3259 return DIRECTIVE_FOUND;
3260 }
3261
H. Peter Anvine2c80182005-01-15 22:15:51 +00003262 case PP_STRLEN:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003263 casesense = true;
H. Peter Anvin70653092007-10-19 14:42:29 -07003264
H. Peter Anvine2c80182005-01-15 22:15:51 +00003265 tline = tline->next;
3266 skip_white_(tline);
3267 tline = expand_id(tline);
3268 if (!tline || (tline->type != TOK_ID &&
3269 (tline->type != TOK_PREPROC_ID ||
3270 tline->text[1] != '$'))) {
3271 error(ERR_NONFATAL,
3272 "`%%strlen' expects a macro identifier as first parameter");
3273 free_tlist(origline);
3274 return DIRECTIVE_FOUND;
3275 }
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04003276 ctx = get_ctx(tline->text, &mname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003277 last = tline;
3278 tline = expand_smacro(tline->next);
3279 last->next = NULL;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003280
H. Peter Anvine2c80182005-01-15 22:15:51 +00003281 t = tline;
3282 while (tok_type_(t, TOK_WHITESPACE))
3283 t = t->next;
3284 /* t should now point to the string */
Cyrill Gorcunov4e1d5ab2010-07-23 18:51:51 +04003285 if (!tok_type_(t, TOK_STRING)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00003286 error(ERR_NONFATAL,
3287 "`%%strlen` requires string as second parameter");
3288 free_tlist(tline);
3289 free_tlist(origline);
3290 return DIRECTIVE_FOUND;
3291 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003292
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003293 macro_start = nasm_malloc(sizeof(*macro_start));
3294 macro_start->next = NULL;
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07003295 make_tok_num(macro_start, nasm_unquote(t->text, NULL));
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003296 macro_start->a.mac = NULL;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003297
H. Peter Anvine2c80182005-01-15 22:15:51 +00003298 /*
3299 * We now have a macro name, an implicit parameter count of
3300 * zero, and a numeric token to use as an expansion. Create
3301 * and store an SMacro.
3302 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003303 define_smacro(ctx, mname, casesense, 0, macro_start);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003304 free_tlist(tline);
3305 free_tlist(origline);
3306 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003307
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003308 case PP_STRCAT:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003309 casesense = true;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003310
3311 tline = tline->next;
3312 skip_white_(tline);
3313 tline = expand_id(tline);
3314 if (!tline || (tline->type != TOK_ID &&
3315 (tline->type != TOK_PREPROC_ID ||
3316 tline->text[1] != '$'))) {
3317 error(ERR_NONFATAL,
3318 "`%%strcat' expects a macro identifier as first parameter");
3319 free_tlist(origline);
3320 return DIRECTIVE_FOUND;
3321 }
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04003322 ctx = get_ctx(tline->text, &mname);
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003323 last = tline;
3324 tline = expand_smacro(tline->next);
3325 last->next = NULL;
3326
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003327 len = 0;
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04003328 list_for_each(t, tline) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003329 switch (t->type) {
3330 case TOK_WHITESPACE:
3331 break;
3332 case TOK_STRING:
3333 len += t->a.len = nasm_unquote(t->text, NULL);
3334 break;
3335 case TOK_OTHER:
3336 if (!strcmp(t->text, ",")) /* permit comma separators */
3337 break;
3338 /* else fall through */
3339 default:
3340 error(ERR_NONFATAL,
3341 "non-string passed to `%%strcat' (%d)", t->type);
3342 free_tlist(tline);
3343 free_tlist(origline);
3344 return DIRECTIVE_FOUND;
3345 }
3346 }
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003347
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003348 p = pp = nasm_malloc(len);
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04003349 list_for_each(t, tline) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003350 if (t->type == TOK_STRING) {
3351 memcpy(p, t->text, t->a.len);
3352 p += t->a.len;
3353 }
3354 }
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003355
3356 /*
3357 * We now have a macro name, an implicit parameter count of
3358 * zero, and a numeric token to use as an expansion. Create
3359 * and store an SMacro.
3360 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003361 macro_start = new_Token(NULL, TOK_STRING, NULL, 0);
3362 macro_start->text = nasm_quote(pp, len);
3363 nasm_free(pp);
3364 define_smacro(ctx, mname, casesense, 0, macro_start);
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003365 free_tlist(tline);
3366 free_tlist(origline);
3367 return DIRECTIVE_FOUND;
3368
H. Peter Anvine2c80182005-01-15 22:15:51 +00003369 case PP_SUBSTR:
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003370 {
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003371 int64_t start, count;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003372 size_t len;
H. Peter Anvind2456592008-06-19 15:04:18 -07003373
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003374 casesense = true;
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07003375
H. Peter Anvine2c80182005-01-15 22:15:51 +00003376 tline = tline->next;
3377 skip_white_(tline);
3378 tline = expand_id(tline);
3379 if (!tline || (tline->type != TOK_ID &&
3380 (tline->type != TOK_PREPROC_ID ||
3381 tline->text[1] != '$'))) {
3382 error(ERR_NONFATAL,
3383 "`%%substr' expects a macro identifier as first parameter");
3384 free_tlist(origline);
3385 return DIRECTIVE_FOUND;
3386 }
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04003387 ctx = get_ctx(tline->text, &mname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003388 last = tline;
3389 tline = expand_smacro(tline->next);
3390 last->next = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003391
Cyrill Gorcunov35519d62010-09-06 23:49:52 +04003392 if (tline) /* skip expanded id */
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003393 t = tline->next;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003394 while (tok_type_(t, TOK_WHITESPACE))
3395 t = t->next;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003396
H. Peter Anvine2c80182005-01-15 22:15:51 +00003397 /* t should now point to the string */
Cyrill Gorcunov35519d62010-09-06 23:49:52 +04003398 if (!tok_type_(t, TOK_STRING)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00003399 error(ERR_NONFATAL,
3400 "`%%substr` requires string as second parameter");
3401 free_tlist(tline);
3402 free_tlist(origline);
3403 return DIRECTIVE_FOUND;
3404 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003405
H. Peter Anvine2c80182005-01-15 22:15:51 +00003406 tt = t->next;
3407 tptr = &tt;
3408 tokval.t_type = TOKEN_INVALID;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003409 evalresult = evaluate(ppscan, tptr, &tokval, NULL,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003410 pass, error, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003411 if (!evalresult) {
3412 free_tlist(tline);
3413 free_tlist(origline);
3414 return DIRECTIVE_FOUND;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003415 } else if (!is_simple(evalresult)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00003416 error(ERR_NONFATAL, "non-constant value given to `%%substr`");
3417 free_tlist(tline);
3418 free_tlist(origline);
3419 return DIRECTIVE_FOUND;
3420 }
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003421 start = evalresult->value - 1;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003422
3423 while (tok_type_(tt, TOK_WHITESPACE))
3424 tt = tt->next;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003425 if (!tt) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003426 count = 1; /* Backwards compatibility: one character */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003427 } else {
3428 tokval.t_type = TOKEN_INVALID;
3429 evalresult = evaluate(ppscan, tptr, &tokval, NULL,
3430 pass, error, NULL);
3431 if (!evalresult) {
3432 free_tlist(tline);
3433 free_tlist(origline);
3434 return DIRECTIVE_FOUND;
3435 } else if (!is_simple(evalresult)) {
3436 error(ERR_NONFATAL, "non-constant value given to `%%substr`");
3437 free_tlist(tline);
3438 free_tlist(origline);
3439 return DIRECTIVE_FOUND;
3440 }
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003441 count = evalresult->value;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003442 }
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003443
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003444 len = nasm_unquote(t->text, NULL);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003445
Cyrill Gorcunovcff031e2010-09-07 20:31:11 +04003446 /* make start and count being in range */
3447 if (start < 0)
3448 start = 0;
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003449 if (count < 0)
3450 count = len + count + 1 - start;
3451 if (start + count > (int64_t)len)
Cyrill Gorcunovcff031e2010-09-07 20:31:11 +04003452 count = len - start;
3453 if (!len || count < 0 || start >=(int64_t)len)
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003454 start = -1, count = 0; /* empty string */
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003455
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003456 macro_start = nasm_malloc(sizeof(*macro_start));
3457 macro_start->next = NULL;
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003458 macro_start->text = nasm_quote((start < 0) ? "" : t->text + start, count);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003459 macro_start->type = TOK_STRING;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003460 macro_start->a.mac = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003461
H. Peter Anvine2c80182005-01-15 22:15:51 +00003462 /*
3463 * We now have a macro name, an implicit parameter count of
3464 * zero, and a numeric token to use as an expansion. Create
3465 * and store an SMacro.
3466 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003467 define_smacro(ctx, mname, casesense, 0, macro_start);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003468 free_tlist(tline);
3469 free_tlist(origline);
3470 return DIRECTIVE_FOUND;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003471 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003472
H. Peter Anvine2c80182005-01-15 22:15:51 +00003473 case PP_ASSIGN:
3474 case PP_IASSIGN:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003475 casesense = (i == PP_ASSIGN);
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07003476
H. Peter Anvine2c80182005-01-15 22:15:51 +00003477 tline = tline->next;
3478 skip_white_(tline);
3479 tline = expand_id(tline);
3480 if (!tline || (tline->type != TOK_ID &&
3481 (tline->type != TOK_PREPROC_ID ||
3482 tline->text[1] != '$'))) {
3483 error(ERR_NONFATAL,
3484 "`%%%sassign' expects a macro identifier",
3485 (i == PP_IASSIGN ? "i" : ""));
3486 free_tlist(origline);
3487 return DIRECTIVE_FOUND;
3488 }
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04003489 ctx = get_ctx(tline->text, &mname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003490 last = tline;
3491 tline = expand_smacro(tline->next);
3492 last->next = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003493
H. Peter Anvine2c80182005-01-15 22:15:51 +00003494 t = tline;
3495 tptr = &t;
3496 tokval.t_type = TOKEN_INVALID;
3497 evalresult =
3498 evaluate(ppscan, tptr, &tokval, NULL, pass, error, NULL);
3499 free_tlist(tline);
3500 if (!evalresult) {
3501 free_tlist(origline);
3502 return DIRECTIVE_FOUND;
3503 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003504
H. Peter Anvine2c80182005-01-15 22:15:51 +00003505 if (tokval.t_type)
H. Peter Anvin917a3492008-09-24 09:14:49 -07003506 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +00003507 "trailing garbage after expression ignored");
H. Peter Anvin734b1882002-04-30 21:01:08 +00003508
H. Peter Anvine2c80182005-01-15 22:15:51 +00003509 if (!is_simple(evalresult)) {
3510 error(ERR_NONFATAL,
3511 "non-constant value given to `%%%sassign'",
3512 (i == PP_IASSIGN ? "i" : ""));
3513 free_tlist(origline);
3514 return DIRECTIVE_FOUND;
3515 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003516
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003517 macro_start = nasm_malloc(sizeof(*macro_start));
3518 macro_start->next = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003519 make_tok_num(macro_start, reloc_value(evalresult));
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003520 macro_start->a.mac = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003521
H. Peter Anvine2c80182005-01-15 22:15:51 +00003522 /*
3523 * We now have a macro name, an implicit parameter count of
3524 * zero, and a numeric token to use as an expansion. Create
3525 * and store an SMacro.
3526 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003527 define_smacro(ctx, mname, casesense, 0, macro_start);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003528 free_tlist(origline);
3529 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003530
H. Peter Anvine2c80182005-01-15 22:15:51 +00003531 case PP_LINE:
3532 /*
3533 * Syntax is `%line nnn[+mmm] [filename]'
3534 */
3535 tline = tline->next;
3536 skip_white_(tline);
3537 if (!tok_type_(tline, TOK_NUMBER)) {
3538 error(ERR_NONFATAL, "`%%line' expects line number");
3539 free_tlist(origline);
3540 return DIRECTIVE_FOUND;
3541 }
H. Peter Anvin70055962007-10-11 00:05:31 -07003542 k = readnum(tline->text, &err);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003543 m = 1;
3544 tline = tline->next;
3545 if (tok_is_(tline, "+")) {
3546 tline = tline->next;
3547 if (!tok_type_(tline, TOK_NUMBER)) {
3548 error(ERR_NONFATAL, "`%%line' expects line increment");
3549 free_tlist(origline);
3550 return DIRECTIVE_FOUND;
3551 }
H. Peter Anvin70055962007-10-11 00:05:31 -07003552 m = readnum(tline->text, &err);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003553 tline = tline->next;
3554 }
3555 skip_white_(tline);
3556 src_set_linnum(k);
3557 istk->lineinc = m;
3558 if (tline) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07003559 nasm_free(src_set_fname(detoken(tline, false)));
H. Peter Anvine2c80182005-01-15 22:15:51 +00003560 }
3561 free_tlist(origline);
3562 return DIRECTIVE_FOUND;
Keith Kaniosb307a4f2010-11-06 17:41:51 -05003563
H. Peter Anvine2c80182005-01-15 22:15:51 +00003564 default:
3565 error(ERR_FATAL,
3566 "preprocessor directive `%s' not yet implemented",
H. Peter Anvin4169a472007-09-12 01:29:43 +00003567 pp_directives[i]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003568 return DIRECTIVE_FOUND;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003569 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003570}
3571
3572/*
H. Peter Anvin76690a12002-04-30 20:52:49 +00003573 * Ensure that a macro parameter contains a condition code and
3574 * nothing else. Return the condition code index if so, or -1
3575 * otherwise.
3576 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003577static int find_cc(Token * t)
H. Peter Anvineba20a72002-04-30 20:53:55 +00003578{
H. Peter Anvin76690a12002-04-30 20:52:49 +00003579 Token *tt;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003580
H. Peter Anvin25a99342007-09-22 17:45:45 -07003581 if (!t)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003582 return -1; /* Probably a %+ without a space */
H. Peter Anvin25a99342007-09-22 17:45:45 -07003583
H. Peter Anvineba20a72002-04-30 20:53:55 +00003584 skip_white_(t);
H. Peter Anvin76690a12002-04-30 20:52:49 +00003585 if (t->type != TOK_ID)
H. Peter Anvine2c80182005-01-15 22:15:51 +00003586 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003587 tt = t->next;
H. Peter Anvineba20a72002-04-30 20:53:55 +00003588 skip_white_(tt);
H. Peter Anvin76690a12002-04-30 20:52:49 +00003589 if (tt && (tt->type != TOK_OTHER || strcmp(tt->text, ",")))
H. Peter Anvine2c80182005-01-15 22:15:51 +00003590 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003591
Cyrill Gorcunov19456392012-05-02 00:18:56 +04003592 return bsii(t->text, (const char **)conditions, ARRAY_SIZE(conditions));
H. Peter Anvin76690a12002-04-30 20:52:49 +00003593}
3594
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003595/*
3596 * This routines walks over tokens strem and hadnles tokens
3597 * pasting, if @handle_explicit passed then explicit pasting
3598 * term is handled, otherwise -- implicit pastings only.
3599 */
Cyrill Gorcunov575d4282010-10-06 00:25:55 +04003600static bool paste_tokens(Token **head, const struct tokseq_match *m,
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003601 size_t mnum, bool handle_explicit)
H. Peter Anvind784a082009-04-20 14:01:18 -07003602{
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003603 Token *tok, *next, **prev_next, **prev_nonspace;
3604 bool pasted = false;
3605 char *buf, *p;
3606 size_t len, i;
H. Peter Anvind784a082009-04-20 14:01:18 -07003607
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003608 /*
3609 * The last token before pasting. We need it
3610 * to be able to connect new handled tokens.
3611 * In other words if there were a tokens stream
3612 *
3613 * A -> B -> C -> D
3614 *
3615 * and we've joined tokens B and C, the resulting
3616 * stream should be
3617 *
3618 * A -> BC -> D
3619 */
3620 tok = *head;
3621 prev_next = NULL;
3622
3623 if (!tok_type_(tok, TOK_WHITESPACE) && !tok_type_(tok, TOK_PASTE))
3624 prev_nonspace = head;
3625 else
3626 prev_nonspace = NULL;
3627
3628 while (tok && (next = tok->next)) {
3629
3630 switch (tok->type) {
H. Peter Anvind784a082009-04-20 14:01:18 -07003631 case TOK_WHITESPACE:
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003632 /* Zap redundant whitespaces */
3633 while (tok_type_(next, TOK_WHITESPACE))
3634 next = delete_Token(next);
3635 tok->next = next;
H. Peter Anvind784a082009-04-20 14:01:18 -07003636 break;
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003637
3638 case TOK_PASTE:
3639 /* Explicit pasting */
3640 if (!handle_explicit)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003641 break;
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003642 next = delete_Token(tok);
3643
3644 while (tok_type_(next, TOK_WHITESPACE))
3645 next = delete_Token(next);
3646
3647 if (!pasted)
3648 pasted = true;
3649
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003650 /* Left pasting token is start of line */
3651 if (!prev_nonspace)
3652 error(ERR_FATAL, "No lvalue found on pasting");
3653
Cyrill Gorcunov8b5c9fb2013-02-04 01:24:54 +04003654 /*
3655 * No ending token, this might happen in two
3656 * cases
3657 *
3658 * 1) There indeed no right token at all
3659 * 2) There is a bare "%define ID" statement,
3660 * and @ID does expand to whitespace.
3661 *
3662 * So technically we need to do a grammar analysis
3663 * in another stage of parsing, but for now lets don't
3664 * change the behaviour people used to. Simply allow
3665 * whitespace after paste token.
3666 */
3667 if (!next) {
3668 /*
3669 * Zap ending space tokens and that's all.
3670 */
3671 tok = (*prev_nonspace)->next;
3672 while (tok_type_(tok, TOK_WHITESPACE))
3673 tok = delete_Token(tok);
3674 tok = *prev_nonspace;
3675 tok->next = NULL;
3676 break;
3677 }
3678
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003679 tok = *prev_nonspace;
3680 while (tok_type_(tok, TOK_WHITESPACE))
3681 tok = delete_Token(tok);
3682 len = strlen(tok->text);
3683 len += strlen(next->text);
3684
3685 p = buf = nasm_malloc(len + 1);
3686 strcpy(p, tok->text);
3687 p = strchr(p, '\0');
3688 strcpy(p, next->text);
3689
3690 delete_Token(tok);
3691
3692 tok = tokenize(buf);
3693 nasm_free(buf);
3694
3695 *prev_nonspace = tok;
3696 while (tok && tok->next)
3697 tok = tok->next;
3698
3699 tok->next = delete_Token(next);
3700
3701 /* Restart from pasted tokens head */
3702 tok = *prev_nonspace;
3703 break;
3704
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003705 default:
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003706 /* implicit pasting */
Cyrill Gorcunov575d4282010-10-06 00:25:55 +04003707 for (i = 0; i < mnum; i++) {
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003708 if (!(PP_CONCAT_MATCH(tok, m[i].mask_head)))
3709 continue;
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +04003710
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003711 len = 0;
3712 while (next && PP_CONCAT_MATCH(next, m[i].mask_tail)) {
3713 len += strlen(next->text);
3714 next = next->next;
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +04003715 }
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003716
3717 /* No match */
3718 if (tok == next)
3719 break;
3720
3721 len += strlen(tok->text);
3722 p = buf = nasm_malloc(len + 1);
3723
3724 while (tok != next) {
3725 strcpy(p, tok->text);
3726 p = strchr(p, '\0');
3727 tok = delete_Token(tok);
3728 }
3729
3730 tok = tokenize(buf);
3731 nasm_free(buf);
3732
3733 if (prev_next)
3734 *prev_next = tok;
3735 else
3736 *head = tok;
3737
3738 /*
3739 * Connect pasted into original stream,
3740 * ie A -> new-tokens -> B
3741 */
3742 while (tok && tok->next)
3743 tok = tok->next;
3744 tok->next = next;
3745
3746 if (!pasted)
3747 pasted = true;
3748
3749 /* Restart from pasted tokens head */
3750 tok = prev_next ? *prev_next : *head;
Cyrill Gorcunov575d4282010-10-06 00:25:55 +04003751 }
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003752
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003753 break;
H. Peter Anvind784a082009-04-20 14:01:18 -07003754 }
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003755
3756 prev_next = &tok->next;
3757
3758 if (tok->next &&
3759 !tok_type_(tok->next, TOK_WHITESPACE) &&
3760 !tok_type_(tok->next, TOK_PASTE))
3761 prev_nonspace = prev_next;
3762
3763 tok = tok->next;
H. Peter Anvind784a082009-04-20 14:01:18 -07003764 }
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003765
3766 return pasted;
H. Peter Anvind784a082009-04-20 14:01:18 -07003767}
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003768
3769/*
3770 * expands to a list of tokens from %{x:y}
3771 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003772static Token *expand_mmac_params_range(MMacro *mac, Token *tline, Token ***last)
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003773{
3774 Token *t = tline, **tt, *tm, *head;
3775 char *pos;
3776 int fst, lst, j, i;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003777
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003778 pos = strchr(tline->text, ':');
3779 nasm_assert(pos);
3780
3781 lst = atoi(pos + 1);
3782 fst = atoi(tline->text + 1);
3783
3784 /*
3785 * only macros params are accounted so
3786 * if someone passes %0 -- we reject such
3787 * value(s)
3788 */
3789 if (lst == 0 || fst == 0)
3790 goto err;
3791
3792 /* the values should be sane */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003793 if ((fst > (int)mac->nparam || fst < (-(int)mac->nparam)) ||
3794 (lst > (int)mac->nparam || lst < (-(int)mac->nparam)))
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003795 goto err;
3796
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003797 fst = fst < 0 ? fst + (int)mac->nparam + 1: fst;
3798 lst = lst < 0 ? lst + (int)mac->nparam + 1: lst;
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003799
3800 /* counted from zero */
3801 fst--, lst--;
3802
3803 /*
Cyrill Gorcunove75331c2013-11-09 12:02:15 +04003804 * It will be at least one token. Note we
3805 * need to scan params until separator, otherwise
3806 * only first token will be passed.
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003807 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003808 tm = mac->params[(fst + mac->rotate) % mac->nparam];
Cyrill Gorcunove75331c2013-11-09 12:02:15 +04003809 head = new_Token(NULL, tm->type, tm->text, 0);
3810 tt = &head->next, tm = tm->next;
3811 while (tok_isnt_(tm, ",")) {
3812 t = new_Token(NULL, tm->type, tm->text, 0);
3813 *tt = t, tt = &t->next, tm = tm->next;
3814 }
3815
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003816 if (fst < lst) {
3817 for (i = fst + 1; i <= lst; i++) {
3818 t = new_Token(NULL, TOK_OTHER, ",", 0);
3819 *tt = t, tt = &t->next;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003820 j = (i + mac->rotate) % mac->nparam;
3821 tm = mac->params[j];
Cyrill Gorcunove75331c2013-11-09 12:02:15 +04003822 while (tok_isnt_(tm, ",")) {
3823 t = new_Token(NULL, tm->type, tm->text, 0);
3824 *tt = t, tt = &t->next, tm = tm->next;
3825 }
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003826 }
3827 } else {
3828 for (i = fst - 1; i >= lst; i--) {
3829 t = new_Token(NULL, TOK_OTHER, ",", 0);
3830 *tt = t, tt = &t->next;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003831 j = (i + mac->rotate) % mac->nparam;
3832 tm = mac->params[j];
Cyrill Gorcunove75331c2013-11-09 12:02:15 +04003833 while (tok_isnt_(tm, ",")) {
3834 t = new_Token(NULL, tm->type, tm->text, 0);
3835 *tt = t, tt = &t->next, tm = tm->next;
3836 }
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003837 }
3838 }
3839
3840 *last = tt;
3841 return head;
3842
3843err:
3844 error(ERR_NONFATAL, "`%%{%s}': macro parameters out of range",
3845 &tline->text[1]);
3846 return tline;
3847}
3848
H. Peter Anvin76690a12002-04-30 20:52:49 +00003849/*
3850 * Expand MMacro-local things: parameter references (%0, %n, %+n,
H. Peter Anvin67c63722008-10-26 23:49:00 -07003851 * %-n) and MMacro-local identifiers (%%foo) as well as
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003852 * macro indirection (%[...]) and range (%{..:..}).
H. Peter Anvin76690a12002-04-30 20:52:49 +00003853 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003854static Token *expand_mmac_params(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00003855{
H. Peter Anvin734b1882002-04-30 21:01:08 +00003856 Token *t, *tt, **tail, *thead;
H. Peter Anvin6125b622009-04-08 14:02:25 -07003857 bool changed = false;
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003858 char *pos;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003859
3860 tail = &thead;
3861 thead = NULL;
3862
H. Peter Anvine2c80182005-01-15 22:15:51 +00003863 while (tline) {
3864 if (tline->type == TOK_PREPROC_ID &&
Cyrill Gorcunovca611192010-06-04 09:22:12 +04003865 (((tline->text[1] == '+' || tline->text[1] == '-') && tline->text[2]) ||
3866 (tline->text[1] >= '0' && tline->text[1] <= '9') ||
3867 tline->text[1] == '%')) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00003868 char *text = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003869 int type = 0, cc; /* type = 0 to placate optimisers */
Keith Kaniosa6dfa782007-04-13 16:47:53 +00003870 char tmpbuf[30];
H. Peter Anvin25a99342007-09-22 17:45:45 -07003871 unsigned int n;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003872 int i;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003873 MMacro *mac;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003874
H. Peter Anvine2c80182005-01-15 22:15:51 +00003875 t = tline;
3876 tline = tline->next;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003877
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003878 mac = istk->mstk;
3879 while (mac && !mac->name) /* avoid mistaking %reps for macros */
3880 mac = mac->next_active;
3881 if (!mac) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00003882 error(ERR_NONFATAL, "`%s': not in a macro call", t->text);
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003883 } else {
3884 pos = strchr(t->text, ':');
3885 if (!pos) {
3886 switch (t->text[1]) {
3887 /*
3888 * We have to make a substitution of one of the
3889 * forms %1, %-1, %+1, %%foo, %0.
3890 */
3891 case '0':
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003892 type = TOK_NUMBER;
3893 snprintf(tmpbuf, sizeof(tmpbuf), "%d", mac->nparam);
3894 text = nasm_strdup(tmpbuf);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003895 break;
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003896 case '%':
H. Peter Anvine2c80182005-01-15 22:15:51 +00003897 type = TOK_ID;
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003898 snprintf(tmpbuf, sizeof(tmpbuf), "..@%"PRIu64".",
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003899 mac->unique);
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003900 text = nasm_strcat(tmpbuf, t->text + 2);
3901 break;
3902 case '-':
3903 n = atoi(t->text + 2) - 1;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003904 if (n >= mac->nparam)
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003905 tt = NULL;
3906 else {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003907 if (mac->nparam > 1)
3908 n = (n + mac->rotate) % mac->nparam;
3909 tt = mac->params[n];
H. Peter Anvine2c80182005-01-15 22:15:51 +00003910 }
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003911 cc = find_cc(tt);
3912 if (cc == -1) {
3913 error(ERR_NONFATAL,
3914 "macro parameter %d is not a condition code",
3915 n + 1);
3916 text = NULL;
3917 } else {
3918 type = TOK_ID;
3919 if (inverse_ccs[cc] == -1) {
3920 error(ERR_NONFATAL,
3921 "condition code `%s' is not invertible",
3922 conditions[cc]);
3923 text = NULL;
3924 } else
3925 text = nasm_strdup(conditions[inverse_ccs[cc]]);
3926 }
3927 break;
3928 case '+':
3929 n = atoi(t->text + 2) - 1;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003930 if (n >= mac->nparam)
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003931 tt = NULL;
3932 else {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003933 if (mac->nparam > 1)
3934 n = (n + mac->rotate) % mac->nparam;
3935 tt = mac->params[n];
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003936 }
3937 cc = find_cc(tt);
3938 if (cc == -1) {
3939 error(ERR_NONFATAL,
3940 "macro parameter %d is not a condition code",
3941 n + 1);
3942 text = NULL;
3943 } else {
3944 type = TOK_ID;
3945 text = nasm_strdup(conditions[cc]);
3946 }
3947 break;
3948 default:
3949 n = atoi(t->text + 1) - 1;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003950 if (n >= mac->nparam)
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003951 tt = NULL;
3952 else {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003953 if (mac->nparam > 1)
3954 n = (n + mac->rotate) % mac->nparam;
3955 tt = mac->params[n];
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003956 }
3957 if (tt) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003958 for (i = 0; i < mac->paramlen[n]; i++) {
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003959 *tail = new_Token(NULL, tt->type, tt->text, 0);
3960 tail = &(*tail)->next;
3961 tt = tt->next;
3962 }
3963 }
3964 text = NULL; /* we've done it here */
3965 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003966 }
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003967 } else {
3968 /*
3969 * seems we have a parameters range here
3970 */
3971 Token *head, **last;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003972 head = expand_mmac_params_range(mac, t, &last);
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003973 if (head != t) {
3974 *tail = head;
3975 *last = tline;
3976 tline = head;
3977 text = NULL;
3978 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00003979 }
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003980 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00003981 if (!text) {
3982 delete_Token(t);
3983 } else {
3984 *tail = t;
3985 tail = &t->next;
3986 t->type = type;
3987 nasm_free(t->text);
3988 t->text = text;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003989 t->a.mac = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003990 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003991 changed = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003992 continue;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003993 } else if (tline->type == TOK_INDIRECT) {
3994 t = tline;
3995 tline = tline->next;
3996 tt = tokenize(t->text);
3997 tt = expand_mmac_params(tt);
3998 tt = expand_smacro(tt);
3999 *tail = tt;
4000 while (tt) {
4001 tt->a.mac = NULL; /* Necessary? */
4002 tail = &tt->next;
4003 tt = tt->next;
4004 }
4005 delete_Token(t);
4006 changed = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004007 } else {
4008 t = *tail = tline;
4009 tline = tline->next;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004010 t->a.mac = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004011 tail = &t->next;
4012 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00004013 }
H. Peter Anvineba20a72002-04-30 20:53:55 +00004014 *tail = NULL;
H. Peter Anvin67c63722008-10-26 23:49:00 -07004015
Cyrill Gorcunovc6a742c2011-06-27 01:23:09 +04004016 if (changed) {
4017 const struct tokseq_match t[] = {
4018 {
4019 PP_CONCAT_MASK(TOK_ID) |
4020 PP_CONCAT_MASK(TOK_FLOAT), /* head */
4021 PP_CONCAT_MASK(TOK_ID) |
4022 PP_CONCAT_MASK(TOK_NUMBER) |
4023 PP_CONCAT_MASK(TOK_FLOAT) |
4024 PP_CONCAT_MASK(TOK_OTHER) /* tail */
4025 },
4026 {
4027 PP_CONCAT_MASK(TOK_NUMBER), /* head */
4028 PP_CONCAT_MASK(TOK_NUMBER) /* tail */
4029 }
4030 };
4031 paste_tokens(&thead, t, ARRAY_SIZE(t), false);
4032 }
H. Peter Anvin6125b622009-04-08 14:02:25 -07004033
H. Peter Anvin76690a12002-04-30 20:52:49 +00004034 return thead;
4035}
4036
4037/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004038 * Expand all single-line macro calls made in the given line.
4039 * Return the expanded version of the line. The original is deemed
4040 * to be destroyed in the process. (In reality we'll just move
4041 * Tokens from input to output a lot of the time, rather than
4042 * actually bothering to destroy and replicate.)
4043 */
H. Peter Anvincb1cf592007-11-19 12:26:50 -08004044
H. Peter Anvine2c80182005-01-15 22:15:51 +00004045static Token *expand_smacro(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004046{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004047 Token *t, *tt, *mstart, **tail, *thead;
H. Peter Anvineba20a72002-04-30 20:53:55 +00004048 SMacro *head = NULL, *m;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004049 Token **params;
4050 int *paramsize;
H. Peter Anvin25a99342007-09-22 17:45:45 -07004051 unsigned int nparam, sparam;
H. Peter Anvind784a082009-04-20 14:01:18 -07004052 int brackets;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004053 Token *org_tline = tline;
4054 Context *ctx;
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08004055 const char *mname;
H. Peter Anvin2a15e692007-11-19 13:14:59 -08004056 int deadman = DEADMAN_LIMIT;
H. Peter Anvin8287daf2009-07-07 16:00:58 -07004057 bool expanded;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004058
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004059 /*
4060 * Trick: we should avoid changing the start token pointer since it can
4061 * be contained in "next" field of other token. Because of this
4062 * we allocate a copy of first token and work with it; at the end of
4063 * routine we copy it back
4064 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004065 if (org_tline) {
Cyrill Gorcunoved4a8052010-04-09 15:40:35 +04004066 tline = new_Token(org_tline->next, org_tline->type,
4067 org_tline->text, 0);
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004068 tline->a.mac = org_tline->a.mac;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004069 nasm_free(org_tline->text);
4070 org_tline->text = NULL;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004071 }
4072
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004073 expanded = true; /* Always expand %+ at least once */
H. Peter Anvin8287daf2009-07-07 16:00:58 -07004074
H. Peter Anvincb1cf592007-11-19 12:26:50 -08004075again:
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004076 thead = NULL;
Cyrill Gorcunoved4a8052010-04-09 15:40:35 +04004077 tail = &thead;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004078
H. Peter Anvine2c80182005-01-15 22:15:51 +00004079 while (tline) { /* main token loop */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004080 if (!--deadman) {
4081 error(ERR_NONFATAL, "interminable macro recursion");
Cyrill Gorcunovbd38c8f2009-11-21 11:11:23 +03004082 goto err;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004083 }
H. Peter Anvincb1cf592007-11-19 12:26:50 -08004084
H. Peter Anvine2c80182005-01-15 22:15:51 +00004085 if ((mname = tline->text)) {
4086 /* if this token is a local macro, look in local context */
Cyrill Gorcunovc56d9ad2010-02-11 15:12:19 +03004087 if (tline->type == TOK_ID) {
4088 head = (SMacro *)hash_findix(&smacros, mname);
4089 } else if (tline->type == TOK_PREPROC_ID) {
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04004090 ctx = get_ctx(mname, &mname);
Cyrill Gorcunovc56d9ad2010-02-11 15:12:19 +03004091 head = ctx ? (SMacro *)hash_findix(&ctx->localmac, mname) : NULL;
4092 } else
4093 head = NULL;
H. Peter Anvin072771e2008-05-22 13:17:51 -07004094
H. Peter Anvine2c80182005-01-15 22:15:51 +00004095 /*
4096 * We've hit an identifier. As in is_mmacro below, we first
4097 * check whether the identifier is a single-line macro at
4098 * all, then think about checking for parameters if
4099 * necessary.
4100 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004101 list_for_each(m, head)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004102 if (!mstrcmp(m->name, mname, m->casesense))
4103 break;
4104 if (m) {
4105 mstart = tline;
4106 params = NULL;
4107 paramsize = NULL;
4108 if (m->nparam == 0) {
4109 /*
4110 * Simple case: the macro is parameterless. Discard the
4111 * one token that the macro call took, and push the
4112 * expansion back on the to-do stack.
4113 */
4114 if (!m->expansion) {
4115 if (!strcmp("__FILE__", m->name)) {
4116 int32_t num = 0;
4117 char *file = NULL;
4118 src_get(&num, &file);
4119 tline->text = nasm_quote(file, strlen(file));
4120 tline->type = TOK_STRING;
4121 nasm_free(file);
H. Peter Anvine2c80182005-01-15 22:15:51 +00004122 continue;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004123 }
4124 if (!strcmp("__LINE__", m->name)) {
4125 nasm_free(tline->text);
4126 make_tok_num(tline, src_get_linnum());
4127 continue;
4128 }
4129 if (!strcmp("__BITS__", m->name)) {
4130 nasm_free(tline->text);
4131 make_tok_num(tline, globalbits);
4132 continue;
4133 }
4134 tline = delete_Token(tline);
4135 continue;
4136 }
4137 } else {
4138 /*
4139 * Complicated case: at least one macro with this name
H. Peter Anvine2c80182005-01-15 22:15:51 +00004140 * exists and takes parameters. We must find the
4141 * parameters in the call, count them, find the SMacro
4142 * that corresponds to that form of the macro call, and
4143 * substitute for the parameters when we expand. What a
4144 * pain.
4145 */
4146 /*tline = tline->next;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004147 skip_white_(tline); */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004148 do {
4149 t = tline->next;
4150 while (tok_type_(t, TOK_SMAC_END)) {
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004151 t->a.mac->in_progress = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004152 t->text = NULL;
4153 t = tline->next = delete_Token(t);
4154 }
4155 tline = t;
4156 } while (tok_type_(tline, TOK_WHITESPACE));
4157 if (!tok_is_(tline, "(")) {
4158 /*
4159 * This macro wasn't called with parameters: ignore
4160 * the call. (Behaviour borrowed from gnu cpp.)
4161 */
4162 tline = mstart;
4163 m = NULL;
4164 } else {
4165 int paren = 0;
4166 int white = 0;
4167 brackets = 0;
4168 nparam = 0;
4169 sparam = PARAM_DELTA;
4170 params = nasm_malloc(sparam * sizeof(Token *));
4171 params[0] = tline->next;
4172 paramsize = nasm_malloc(sparam * sizeof(int));
4173 paramsize[0] = 0;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07004174 while (true) { /* parameter loop */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004175 /*
4176 * For some unusual expansions
4177 * which concatenates function call
4178 */
4179 t = tline->next;
4180 while (tok_type_(t, TOK_SMAC_END)) {
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004181 t->a.mac->in_progress = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004182 t->text = NULL;
4183 t = tline->next = delete_Token(t);
4184 }
4185 tline = t;
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00004186
H. Peter Anvine2c80182005-01-15 22:15:51 +00004187 if (!tline) {
4188 error(ERR_NONFATAL,
4189 "macro call expects terminating `)'");
4190 break;
4191 }
4192 if (tline->type == TOK_WHITESPACE
4193 && brackets <= 0) {
4194 if (paramsize[nparam])
4195 white++;
4196 else
4197 params[nparam] = tline->next;
4198 continue; /* parameter loop */
4199 }
4200 if (tline->type == TOK_OTHER
4201 && tline->text[1] == 0) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004202 char ch = tline->text[0];
H. Peter Anvine2c80182005-01-15 22:15:51 +00004203 if (ch == ',' && !paren && brackets <= 0) {
4204 if (++nparam >= sparam) {
4205 sparam += PARAM_DELTA;
4206 params = nasm_realloc(params,
Cyrill Gorcunoved4a8052010-04-09 15:40:35 +04004207 sparam * sizeof(Token *));
4208 paramsize = nasm_realloc(paramsize,
4209 sparam * sizeof(int));
H. Peter Anvine2c80182005-01-15 22:15:51 +00004210 }
4211 params[nparam] = tline->next;
4212 paramsize[nparam] = 0;
4213 white = 0;
4214 continue; /* parameter loop */
4215 }
4216 if (ch == '{' &&
4217 (brackets > 0 || (brackets == 0 &&
4218 !paramsize[nparam])))
4219 {
4220 if (!(brackets++)) {
4221 params[nparam] = tline->next;
4222 continue; /* parameter loop */
4223 }
4224 }
4225 if (ch == '}' && brackets > 0)
4226 if (--brackets == 0) {
4227 brackets = -1;
4228 continue; /* parameter loop */
4229 }
4230 if (ch == '(' && !brackets)
4231 paren++;
4232 if (ch == ')' && brackets <= 0)
4233 if (--paren < 0)
4234 break;
4235 }
4236 if (brackets < 0) {
4237 brackets = 0;
4238 error(ERR_NONFATAL, "braces do not "
4239 "enclose all of macro parameter");
4240 }
4241 paramsize[nparam] += white + 1;
4242 white = 0;
4243 } /* parameter loop */
4244 nparam++;
4245 while (m && (m->nparam != nparam ||
4246 mstrcmp(m->name, mname,
4247 m->casesense)))
4248 m = m->next;
4249 if (!m)
H. Peter Anvin917a3492008-09-24 09:14:49 -07004250 error(ERR_WARNING|ERR_PASS1|ERR_WARN_MNP,
H. Peter Anvine2c80182005-01-15 22:15:51 +00004251 "macro `%s' exists, "
4252 "but not taking %d parameters",
4253 mstart->text, nparam);
4254 }
4255 }
4256 if (m && m->in_progress)
4257 m = NULL;
4258 if (!m) { /* in progess or didn't find '(' or wrong nparam */
H. Peter Anvin70653092007-10-19 14:42:29 -07004259 /*
H. Peter Anvine2c80182005-01-15 22:15:51 +00004260 * Design question: should we handle !tline, which
4261 * indicates missing ')' here, or expand those
4262 * macros anyway, which requires the (t) test a few
H. Peter Anvin70653092007-10-19 14:42:29 -07004263 * lines down?
H. Peter Anvine2c80182005-01-15 22:15:51 +00004264 */
4265 nasm_free(params);
4266 nasm_free(paramsize);
4267 tline = mstart;
4268 } else {
4269 /*
4270 * Expand the macro: we are placed on the last token of the
4271 * call, so that we can easily split the call from the
4272 * following tokens. We also start by pushing an SMAC_END
4273 * token for the cycle removal.
4274 */
4275 t = tline;
4276 if (t) {
4277 tline = t->next;
4278 t->next = NULL;
4279 }
4280 tt = new_Token(tline, TOK_SMAC_END, NULL, 0);
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004281 tt->a.mac = m;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07004282 m->in_progress = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004283 tline = tt;
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04004284 list_for_each(t, m->expansion) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004285 if (t->type >= TOK_SMAC_PARAM) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00004286 Token *pcopy = tline, **ptail = &pcopy;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004287 Token *ttt, *pt;
4288 int i;
H. Peter Anvin734b1882002-04-30 21:01:08 +00004289
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004290 ttt = params[t->type - TOK_SMAC_PARAM];
4291 i = paramsize[t->type - TOK_SMAC_PARAM];
4292 while (--i >= 0) {
4293 pt = *ptail = new_Token(tline, ttt->type,
4294 ttt->text, 0);
4295 ptail = &pt->next;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004296 ttt = ttt->next;
4297 }
4298 tline = pcopy;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004299 } else if (t->type == TOK_PREPROC_Q) {
4300 tt = new_Token(tline, TOK_ID, mname, 0);
4301 tline = tt;
4302 } else if (t->type == TOK_PREPROC_QQ) {
4303 tt = new_Token(tline, TOK_ID, m->name, 0);
4304 tline = tt;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004305 } else {
4306 tt = new_Token(tline, t->type, t->text, 0);
4307 tline = tt;
4308 }
4309 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00004310
H. Peter Anvine2c80182005-01-15 22:15:51 +00004311 /*
4312 * Having done that, get rid of the macro call, and clean
4313 * up the parameters.
4314 */
4315 nasm_free(params);
4316 nasm_free(paramsize);
4317 free_tlist(mstart);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004318 expanded = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004319 continue; /* main token loop */
4320 }
4321 }
4322 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004323
H. Peter Anvine2c80182005-01-15 22:15:51 +00004324 if (tline->type == TOK_SMAC_END) {
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004325 tline->a.mac->in_progress = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004326 tline = delete_Token(tline);
4327 } else {
4328 t = *tail = tline;
4329 tline = tline->next;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004330 t->a.mac = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004331 t->next = NULL;
4332 tail = &t->next;
4333 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004334 }
4335
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004336 /*
4337 * Now scan the entire line and look for successive TOK_IDs that resulted
Keith Kaniosb7a89542007-04-12 02:40:54 +00004338 * after expansion (they can't be produced by tokenize()). The successive
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004339 * TOK_IDs should be concatenated.
4340 * Also we look for %+ tokens and concatenate the tokens before and after
4341 * them (without white spaces in between).
4342 */
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +04004343 if (expanded) {
Cyrill Gorcunovc6a742c2011-06-27 01:23:09 +04004344 const struct tokseq_match t[] = {
4345 {
4346 PP_CONCAT_MASK(TOK_ID) |
4347 PP_CONCAT_MASK(TOK_PREPROC_ID), /* head */
4348 PP_CONCAT_MASK(TOK_ID) |
4349 PP_CONCAT_MASK(TOK_PREPROC_ID) |
4350 PP_CONCAT_MASK(TOK_NUMBER) /* tail */
4351 }
4352 };
4353 if (paste_tokens(&thead, t, ARRAY_SIZE(t), true)) {
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +04004354 /*
4355 * If we concatenated something, *and* we had previously expanded
4356 * an actual macro, scan the lines again for macros...
4357 */
4358 tline = thead;
4359 expanded = false;
4360 goto again;
4361 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00004362 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004363
Cyrill Gorcunovbd38c8f2009-11-21 11:11:23 +03004364err:
H. Peter Anvine2c80182005-01-15 22:15:51 +00004365 if (org_tline) {
4366 if (thead) {
4367 *org_tline = *thead;
4368 /* since we just gave text to org_line, don't free it */
4369 thead->text = NULL;
4370 delete_Token(thead);
4371 } else {
4372 /* the expression expanded to empty line;
4373 we can't return NULL for some reasons
4374 we just set the line to a single WHITESPACE token. */
4375 memset(org_tline, 0, sizeof(*org_tline));
4376 org_tline->text = NULL;
4377 org_tline->type = TOK_WHITESPACE;
4378 }
4379 thead = org_tline;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004380 }
4381
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004382 return thead;
4383}
4384
4385/*
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004386 * Similar to expand_smacro but used exclusively with macro identifiers
4387 * right before they are fetched in. The reason is that there can be
4388 * identifiers consisting of several subparts. We consider that if there
4389 * are more than one element forming the name, user wants a expansion,
4390 * otherwise it will be left as-is. Example:
4391 *
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004392 * %define %$abc cde
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004393 *
4394 * the identifier %$abc will be left as-is so that the handler for %define
4395 * will suck it and define the corresponding value. Other case:
4396 *
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004397 * %define _%$abc cde
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004398 *
4399 * In this case user wants name to be expanded *before* %define starts
4400 * working, so we'll expand %$abc into something (if it has a value;
4401 * otherwise it will be left as-is) then concatenate all successive
4402 * PP_IDs into one.
4403 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004404static Token *expand_id(Token * tline)
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004405{
4406 Token *cur, *oldnext = NULL;
4407
H. Peter Anvin734b1882002-04-30 21:01:08 +00004408 if (!tline || !tline->next)
H. Peter Anvine2c80182005-01-15 22:15:51 +00004409 return tline;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004410
4411 cur = tline;
4412 while (cur->next &&
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004413 (cur->next->type == TOK_ID ||
4414 cur->next->type == TOK_PREPROC_ID
4415 || cur->next->type == TOK_NUMBER))
H. Peter Anvine2c80182005-01-15 22:15:51 +00004416 cur = cur->next;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004417
4418 /* If identifier consists of just one token, don't expand */
4419 if (cur == tline)
H. Peter Anvine2c80182005-01-15 22:15:51 +00004420 return tline;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004421
H. Peter Anvine2c80182005-01-15 22:15:51 +00004422 if (cur) {
4423 oldnext = cur->next; /* Detach the tail past identifier */
4424 cur->next = NULL; /* so that expand_smacro stops here */
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004425 }
4426
H. Peter Anvin734b1882002-04-30 21:01:08 +00004427 tline = expand_smacro(tline);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004428
H. Peter Anvine2c80182005-01-15 22:15:51 +00004429 if (cur) {
4430 /* expand_smacro possibly changhed tline; re-scan for EOL */
4431 cur = tline;
4432 while (cur && cur->next)
4433 cur = cur->next;
4434 if (cur)
4435 cur->next = oldnext;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004436 }
4437
4438 return tline;
4439}
4440
4441/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004442 * Determine whether the given line constitutes a multi-line macro
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004443 * call, and return the MMacro structure called if so. Doesn't have
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004444 * to check for an initial label - that's taken care of in
4445 * expand_mmacro - but must check numbers of parameters. Guaranteed
4446 * to be called with tline->type == TOK_ID, so the putative macro
4447 * name is easy to find.
4448 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004449static MMacro *is_mmacro(Token * tline, Token *** params_array)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004450{
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004451 MMacro *head, *m;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004452 Token **params;
4453 int nparam;
4454
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004455 head = (MMacro *) hash_findix(&mmacros, tline->text);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004456
4457 /*
4458 * Efficiency: first we see if any macro exists with the given
4459 * name. If not, we can return NULL immediately. _Then_ we
4460 * count the parameters, and then we look further along the
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004461 * list if necessary to find the proper MMacro.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004462 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004463 list_for_each(m, head)
4464 if (!mstrcmp(m->name, tline->text, m->casesense))
H. Peter Anvine2c80182005-01-15 22:15:51 +00004465 break;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004466 if (!m)
H. Peter Anvine2c80182005-01-15 22:15:51 +00004467 return NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004468
4469 /*
4470 * OK, we have a potential macro. Count and demarcate the
4471 * parameters.
4472 */
H. Peter Anvin734b1882002-04-30 21:01:08 +00004473 count_mmac_params(tline->next, &nparam, &params);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004474
4475 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004476 * So we know how many parameters we've got. Find the MMacro
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004477 * structure that handles this number.
4478 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004479 while (m) {
4480 if (m->nparam_min <= nparam
4481 && (m->plus || nparam <= m->nparam_max)) {
4482 /*
4483 * This one is right. Just check if cycle removal
4484 * prohibits us using it before we actually celebrate...
4485 */
4486 if (m->in_progress > m->max_depth) {
4487 if (m->max_depth > 0) {
4488 error(ERR_WARNING,
4489 "reached maximum recursion depth of %i",
4490 m->max_depth);
4491 }
4492 nasm_free(params);
4493 return NULL;
4494 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00004495 /*
4496 * It's right, and we can use it. Add its default
4497 * parameters to the end of our list if necessary.
4498 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004499 if (m->defaults && nparam < m->nparam_min + m->ndefs) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00004500 params =
4501 nasm_realloc(params,
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004502 ((m->nparam_min + m->ndefs +
H. Peter Anvine2c80182005-01-15 22:15:51 +00004503 1) * sizeof(*params)));
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004504 while (nparam < m->nparam_min + m->ndefs) {
4505 params[nparam] = m->defaults[nparam - m->nparam_min];
H. Peter Anvine2c80182005-01-15 22:15:51 +00004506 nparam++;
4507 }
4508 }
4509 /*
4510 * If we've gone over the maximum parameter count (and
4511 * we're in Plus mode), ignore parameters beyond
4512 * nparam_max.
4513 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004514 if (m->plus && nparam > m->nparam_max)
4515 nparam = m->nparam_max;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004516 /*
4517 * Then terminate the parameter list, and leave.
4518 */
4519 if (!params) { /* need this special case */
4520 params = nasm_malloc(sizeof(*params));
4521 nparam = 0;
4522 }
4523 params[nparam] = NULL;
4524 *params_array = params;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004525 return m;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004526 }
4527 /*
4528 * This one wasn't right: look for the next one with the
4529 * same name.
4530 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004531 list_for_each(m, m->next)
4532 if (!mstrcmp(m->name, tline->text, m->casesense))
H. Peter Anvine2c80182005-01-15 22:15:51 +00004533 break;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004534 }
4535
4536 /*
4537 * After all that, we didn't find one with the right number of
4538 * parameters. Issue a warning, and fail to expand the macro.
4539 */
H. Peter Anvin917a3492008-09-24 09:14:49 -07004540 error(ERR_WARNING|ERR_PASS1|ERR_WARN_MNP,
H. Peter Anvine2c80182005-01-15 22:15:51 +00004541 "macro `%s' exists, but not taking %d parameters",
4542 tline->text, nparam);
H. Peter Anvin734b1882002-04-30 21:01:08 +00004543 nasm_free(params);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004544 return NULL;
4545}
4546
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004547
4548/*
4549 * Save MMacro invocation specific fields in
4550 * preparation for a recursive macro expansion
4551 */
4552static void push_mmacro(MMacro *m)
4553{
4554 MMacroInvocation *i;
4555
4556 i = nasm_malloc(sizeof(MMacroInvocation));
4557 i->prev = m->prev;
4558 i->params = m->params;
4559 i->iline = m->iline;
4560 i->nparam = m->nparam;
4561 i->rotate = m->rotate;
4562 i->paramlen = m->paramlen;
4563 i->unique = m->unique;
4564 i->condcnt = m->condcnt;
4565 m->prev = i;
4566}
4567
4568
4569/*
4570 * Restore MMacro invocation specific fields that were
4571 * saved during a previous recursive macro expansion
4572 */
4573static void pop_mmacro(MMacro *m)
4574{
4575 MMacroInvocation *i;
4576
4577 if (m->prev) {
4578 i = m->prev;
4579 m->prev = i->prev;
4580 m->params = i->params;
4581 m->iline = i->iline;
4582 m->nparam = i->nparam;
4583 m->rotate = i->rotate;
4584 m->paramlen = i->paramlen;
4585 m->unique = i->unique;
4586 m->condcnt = i->condcnt;
4587 nasm_free(i);
4588 }
4589}
4590
4591
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004592/*
4593 * Expand the multi-line macro call made by the given line, if
4594 * there is one to be expanded. If there is, push the expansion on
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004595 * istk->expansion and return 1. Otherwise return 0.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004596 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004597static int expand_mmacro(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004598{
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004599 Token *startline = tline;
H. Peter Anvineba20a72002-04-30 20:53:55 +00004600 Token *label = NULL;
4601 int dont_prepend = 0;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004602 Token **params, *t, *tt;
4603 MMacro *m;
4604 Line *l, *ll;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004605 int i, nparam, *paramlen;
H. Peter Anvinc751e862008-06-09 10:18:45 -07004606 const char *mname;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004607
4608 t = tline;
H. Peter Anvineba20a72002-04-30 20:53:55 +00004609 skip_white_(t);
H. Peter Anvince2233b2008-05-25 21:57:00 -07004610 /* if (!tok_type_(t, TOK_ID)) Lino 02/25/02 */
H. Peter Anvindce1e2f2002-04-30 21:06:37 +00004611 if (!tok_type_(t, TOK_ID) && !tok_type_(t, TOK_PREPROC_ID))
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004612 return 0;
4613 m = is_mmacro(t, &params);
4614 if (m) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004615 mname = t->text;
H. Peter Anvinc751e862008-06-09 10:18:45 -07004616 } else {
H. Peter Anvine2c80182005-01-15 22:15:51 +00004617 Token *last;
4618 /*
4619 * We have an id which isn't a macro call. We'll assume
4620 * it might be a label; we'll also check to see if a
4621 * colon follows it. Then, if there's another id after
4622 * that lot, we'll check it again for macro-hood.
4623 */
4624 label = last = t;
4625 t = t->next;
4626 if (tok_type_(t, TOK_WHITESPACE))
4627 last = t, t = t->next;
4628 if (tok_is_(t, ":")) {
4629 dont_prepend = 1;
4630 last = t, t = t->next;
4631 if (tok_type_(t, TOK_WHITESPACE))
4632 last = t, t = t->next;
4633 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004634 if (!tok_type_(t, TOK_ID) || !(m = is_mmacro(t, &params)))
4635 return 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004636 last->next = NULL;
Keith Kanios891775e2009-07-11 06:08:54 -05004637 mname = t->text;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004638 tline = t;
H. Peter Anvineba20a72002-04-30 20:53:55 +00004639 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004640
4641 /*
4642 * Fix up the parameters: this involves stripping leading and
4643 * trailing whitespace, then stripping braces if they are
4644 * present.
4645 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004646 for (nparam = 0; params[nparam]; nparam++) ;
H. Peter Anvin734b1882002-04-30 21:01:08 +00004647 paramlen = nparam ? nasm_malloc(nparam * sizeof(*paramlen)) : NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004648
H. Peter Anvine2c80182005-01-15 22:15:51 +00004649 for (i = 0; params[i]; i++) {
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08004650 int brace = 0;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004651 int comma = (!m->plus || i < nparam - 1);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004652
H. Peter Anvine2c80182005-01-15 22:15:51 +00004653 t = params[i];
4654 skip_white_(t);
4655 if (tok_is_(t, "{"))
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08004656 t = t->next, brace++, comma = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004657 params[i] = t;
4658 paramlen[i] = 0;
4659 while (t) {
4660 if (comma && t->type == TOK_OTHER && !strcmp(t->text, ","))
4661 break; /* ... because we have hit a comma */
4662 if (comma && t->type == TOK_WHITESPACE
4663 && tok_is_(t->next, ","))
4664 break; /* ... or a space then a comma */
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08004665 if (brace && t->type == TOK_OTHER) {
4666 if (t->text[0] == '{')
4667 brace++; /* ... or a nested opening brace */
4668 else if (t->text[0] == '}')
4669 if (!--brace)
4670 break; /* ... or a brace */
4671 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00004672 t = t->next;
4673 paramlen[i]++;
4674 }
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08004675 if (brace)
4676 error(ERR_NONFATAL, "macro params should be enclosed in braces");
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004677 }
4678
4679 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004680 * OK, we have a MMacro structure together with a set of
4681 * parameters. We must now go through the expansion and push
4682 * copies of each Line on to istk->expansion. Substitution of
H. Peter Anvin76690a12002-04-30 20:52:49 +00004683 * parameter tokens and macro-local tokens doesn't get done
4684 * until the single-line macro substitution process; this is
4685 * because delaying them allows us to change the semantics
4686 * later through %rotate.
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004687 *
4688 * First, push an end marker on to istk->expansion, mark this
4689 * macro as in progress, and set up its invocation-specific
4690 * variables.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004691 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004692 ll = nasm_malloc(sizeof(Line));
4693 ll->next = istk->expansion;
4694 ll->finishes = m;
4695 ll->first = NULL;
4696 istk->expansion = ll;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004697
4698 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004699 * Save the previous MMacro expansion in the case of
4700 * macro recursion
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004701 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004702 if (m->max_depth && m->in_progress)
4703 push_mmacro(m);
4704
4705 m->in_progress ++;
4706 m->params = params;
4707 m->iline = tline;
4708 m->nparam = nparam;
4709 m->rotate = 0;
4710 m->paramlen = paramlen;
4711 m->unique = unique++;
4712 m->lineno = 0;
4713 m->condcnt = 0;
4714
4715 m->next_active = istk->mstk;
4716 istk->mstk = m;
4717
4718 list_for_each(l, m->expansion) {
4719 Token **tail;
4720
4721 ll = nasm_malloc(sizeof(Line));
4722 ll->finishes = NULL;
4723 ll->next = istk->expansion;
4724 istk->expansion = ll;
4725 tail = &ll->first;
4726
4727 list_for_each(t, l->first) {
4728 Token *x = t;
4729 switch (t->type) {
4730 case TOK_PREPROC_Q:
4731 tt = *tail = new_Token(NULL, TOK_ID, mname, 0);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004732 break;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004733 case TOK_PREPROC_QQ:
4734 tt = *tail = new_Token(NULL, TOK_ID, m->name, 0);
4735 break;
4736 case TOK_PREPROC_ID:
4737 if (t->text[1] == '0' && t->text[2] == '0') {
4738 dont_prepend = -1;
4739 x = label;
4740 if (!x)
4741 continue;
4742 }
4743 /* fall through */
4744 default:
4745 tt = *tail = new_Token(NULL, x->type, x->text, 0);
4746 break;
4747 }
4748 tail = &tt->next;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004749 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004750 *tail = NULL;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004751 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004752
4753 /*
H. Peter Anvineba20a72002-04-30 20:53:55 +00004754 * If we had a label, push it on as the first line of
4755 * the macro expansion.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004756 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004757 if (label) {
4758 if (dont_prepend < 0)
4759 free_tlist(startline);
4760 else {
4761 ll = nasm_malloc(sizeof(Line));
4762 ll->finishes = NULL;
4763 ll->next = istk->expansion;
4764 istk->expansion = ll;
4765 ll->first = startline;
4766 if (!dont_prepend) {
4767 while (label->next)
4768 label = label->next;
4769 label->next = tt = new_Token(NULL, TOK_OTHER, ":", 0);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004770 }
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004771 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004772 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004773
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004774 list->uplevel(m->nolist ? LIST_MACRO_NOLIST : LIST_MACRO);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004775
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004776 return 1;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004777}
4778
Victor van den Elzen3b404c02008-09-18 13:51:36 +02004779/* The function that actually does the error reporting */
4780static void verror(int severity, const char *fmt, va_list arg)
4781{
4782 char buff[1024];
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004783 MMacro *mmac = NULL;
4784 int delta = 0;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02004785
4786 vsnprintf(buff, sizeof(buff), fmt, arg);
4787
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004788 /* get %macro name */
4789 if (istk && istk->mstk) {
4790 mmac = istk->mstk;
4791 /* but %rep blocks should be skipped */
4792 while (mmac && !mmac->name)
4793 mmac = mmac->next_active, delta++;
Cyrill Gorcunov9900c6b2011-10-09 18:58:46 +04004794 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004795
4796 if (mmac)
4797 nasm_error(severity, "(%s:%d) %s",
4798 mmac->name, mmac->lineno - delta, buff);
4799 else
4800 nasm_error(severity, "%s", buff);
Victor van den Elzen3b404c02008-09-18 13:51:36 +02004801}
4802
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004803/*
4804 * Since preprocessor always operate only on the line that didn't
H. Peter Anvin917a3492008-09-24 09:14:49 -07004805 * arrived yet, we should always use ERR_OFFBY1.
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004806 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004807static void error(int severity, const char *fmt, ...)
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004808{
4809 va_list arg;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004810
4811 /* If we're in a dead branch of IF or something like it, ignore the error */
4812 if (istk && istk->conds && !emitting(istk->conds->state))
4813 return;
4814
H. Peter Anvin734b1882002-04-30 21:01:08 +00004815 va_start(arg, fmt);
Victor van den Elzen3b404c02008-09-18 13:51:36 +02004816 verror(severity, fmt, arg);
H. Peter Anvin734b1882002-04-30 21:01:08 +00004817 va_end(arg);
Victor van den Elzen3b404c02008-09-18 13:51:36 +02004818}
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004819
Victor van den Elzen3b404c02008-09-18 13:51:36 +02004820/*
4821 * Because %else etc are evaluated in the state context
4822 * of the previous branch, errors might get lost with error():
4823 * %if 0 ... %else trailing garbage ... %endif
4824 * So %else etc should report errors with this function.
4825 */
4826static void error_precond(int severity, const char *fmt, ...)
4827{
4828 va_list arg;
4829
4830 /* Only ignore the error if it's really in a dead branch */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004831 if (istk && istk->conds && istk->conds->state == COND_NEVER)
Victor van den Elzen3b404c02008-09-18 13:51:36 +02004832 return;
4833
4834 va_start(arg, fmt);
4835 verror(severity, fmt, arg);
4836 va_end(arg);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004837}
4838
H. Peter Anvin734b1882002-04-30 21:01:08 +00004839static void
H. Peter Anvindbb640b2009-07-18 18:57:16 -07004840pp_reset(char *file, int apass, ListGen * listgen, StrList **deplist)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004841{
H. Peter Anvin7383b402008-09-24 10:20:40 -07004842 Token *t;
4843
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004844 cstk = NULL;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004845 istk = nasm_malloc(sizeof(Include));
4846 istk->next = NULL;
4847 istk->conds = NULL;
4848 istk->expansion = NULL;
4849 istk->mstk = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004850 istk->fp = fopen(file, "r");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004851 istk->fname = NULL;
H. Peter Anvineba20a72002-04-30 20:53:55 +00004852 src_set_fname(nasm_strdup(file));
4853 src_set_linnum(0);
4854 istk->lineinc = 1;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004855 if (!istk->fp)
H. Peter Anvin917a3492008-09-24 09:14:49 -07004856 error(ERR_FATAL|ERR_NOFILE, "unable to open input file `%s'",
H. Peter Anvine2c80182005-01-15 22:15:51 +00004857 file);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004858 defining = NULL;
Charles Crayned4200be2008-07-12 16:42:33 -07004859 nested_mac_count = 0;
4860 nested_rep_count = 0;
H. Peter Anvin97a23472007-09-16 17:57:25 -07004861 init_macros();
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004862 unique = 0;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004863 if (tasm_compatible_mode) {
H. Peter Anvina4835d42008-05-20 14:21:29 -07004864 stdmacpos = nasm_stdmac;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004865 } else {
H. Peter Anvina4835d42008-05-20 14:21:29 -07004866 stdmacpos = nasm_stdmac_after_tasm;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004867 }
H. Peter Anvind2456592008-06-19 15:04:18 -07004868 any_extrastdmac = extrastdmac && *extrastdmac;
4869 do_predef = true;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004870 list = listgen;
H. Peter Anvin61f130f2008-09-25 15:45:06 -07004871
4872 /*
4873 * 0 for dependencies, 1 for preparatory passes, 2 for final pass.
4874 * The caller, however, will also pass in 3 for preprocess-only so
4875 * we can set __PASS__ accordingly.
4876 */
4877 pass = apass > 2 ? 2 : apass;
4878
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07004879 dephead = deptail = deplist;
4880 if (deplist) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004881 StrList *sl = nasm_malloc(strlen(file)+1+sizeof sl->next);
4882 sl->next = NULL;
4883 strcpy(sl->str, file);
4884 *deptail = sl;
4885 deptail = &sl->next;
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07004886 }
H. Peter Anvin7383b402008-09-24 10:20:40 -07004887
H. Peter Anvin61f130f2008-09-25 15:45:06 -07004888 /*
4889 * Define the __PASS__ macro. This is defined here unlike
4890 * all the other builtins, because it is special -- it varies between
4891 * passes.
4892 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004893 t = nasm_malloc(sizeof(*t));
4894 t->next = NULL;
H. Peter Anvin61f130f2008-09-25 15:45:06 -07004895 make_tok_num(t, apass);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004896 t->a.mac = NULL;
H. Peter Anvin7383b402008-09-24 10:20:40 -07004897 define_smacro(NULL, "__PASS__", true, 0, t);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004898}
4899
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004900static char *pp_getline(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004901{
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004902 char *line;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004903 Token *tline;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004904
H. Peter Anvine2c80182005-01-15 22:15:51 +00004905 while (1) {
4906 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004907 * Fetch a tokenized line, either from the macro-expansion
H. Peter Anvine2c80182005-01-15 22:15:51 +00004908 * buffer or from the input file.
4909 */
4910 tline = NULL;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004911 while (istk->expansion && istk->expansion->finishes) {
4912 Line *l = istk->expansion;
4913 if (!l->finishes->name && l->finishes->in_progress > 1) {
4914 Line *ll;
H. Peter Anvineba20a72002-04-30 20:53:55 +00004915
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004916 /*
4917 * This is a macro-end marker for a macro with no
4918 * name, which means it's not really a macro at all
4919 * but a %rep block, and the `in_progress' field is
4920 * more than 1, meaning that we still need to
4921 * repeat. (1 means the natural last repetition; 0
4922 * means termination by %exitrep.) We have
4923 * therefore expanded up to the %endrep, and must
4924 * push the whole block on to the expansion buffer
4925 * again. We don't bother to remove the macro-end
4926 * marker: we'd only have to generate another one
4927 * if we did.
4928 */
4929 l->finishes->in_progress--;
4930 list_for_each(l, l->finishes->expansion) {
4931 Token *t, *tt, **tail;
4932
4933 ll = nasm_malloc(sizeof(Line));
4934 ll->next = istk->expansion;
4935 ll->finishes = NULL;
4936 ll->first = NULL;
4937 tail = &ll->first;
4938
4939 list_for_each(t, l->first) {
4940 if (t->text || t->type == TOK_WHITESPACE) {
4941 tt = *tail = new_Token(NULL, t->type, t->text, 0);
4942 tail = &tt->next;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004943 }
4944 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004945
4946 istk->expansion = ll;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004947 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004948 } else {
4949 /*
4950 * Check whether a `%rep' was started and not ended
4951 * within this macro expansion. This can happen and
4952 * should be detected. It's a fatal error because
4953 * I'm too confused to work out how to recover
4954 * sensibly from it.
4955 */
4956 if (defining) {
4957 if (defining->name)
4958 error(ERR_PANIC,
4959 "defining with name in expansion");
4960 else if (istk->mstk->name)
4961 error(ERR_FATAL,
4962 "`%%rep' without `%%endrep' within"
4963 " expansion of macro `%s'",
4964 istk->mstk->name);
4965 }
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004966
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004967 /*
4968 * FIXME: investigate the relationship at this point between
4969 * istk->mstk and l->finishes
4970 */
4971 {
4972 MMacro *m = istk->mstk;
4973 istk->mstk = m->next_active;
4974 if (m->name) {
4975 /*
4976 * This was a real macro call, not a %rep, and
4977 * therefore the parameter information needs to
4978 * be freed.
4979 */
4980 if (m->prev) {
4981 pop_mmacro(m);
4982 l->finishes->in_progress --;
4983 } else {
4984 nasm_free(m->params);
4985 free_tlist(m->iline);
4986 nasm_free(m->paramlen);
4987 l->finishes->in_progress = 0;
4988 }
4989 } else
4990 free_mmacro(m);
4991 }
4992 istk->expansion = l->next;
4993 nasm_free(l);
4994 list->downlevel(LIST_MACRO);
4995 }
4996 }
4997 while (1) { /* until we get a line we can use */
4998
4999 if (istk->expansion) { /* from a macro expansion */
5000 char *p;
5001 Line *l = istk->expansion;
5002 if (istk->mstk)
5003 istk->mstk->lineno++;
5004 tline = l->first;
5005 istk->expansion = l->next;
5006 nasm_free(l);
5007 p = detoken(tline, false);
5008 list->line(LIST_MACRO, p);
5009 nasm_free(p);
5010 break;
5011 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00005012 line = read_line();
5013 if (line) { /* from the current input file */
5014 line = prepreproc(line);
Keith Kaniosb7a89542007-04-12 02:40:54 +00005015 tline = tokenize(line);
H. Peter Anvine2c80182005-01-15 22:15:51 +00005016 nasm_free(line);
5017 break;
5018 }
5019 /*
5020 * The current file has ended; work down the istk
5021 */
5022 {
5023 Include *i = istk;
5024 fclose(i->fp);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005025 if (i->conds) {
5026 /* nasm_error can't be conditionally suppressed */
H. Peter Anvin41087062016-03-03 14:27:34 -08005027 nasm_fatal(0,
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005028 "expected `%%endif' before end of file");
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005029 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00005030 /* only set line and file name if there's a next node */
5031 if (i->next) {
5032 src_set_linnum(i->lineno);
Cyrill Gorcunovd34a1082011-03-07 11:23:08 +03005033 nasm_free(src_set_fname(nasm_strdup(i->fname)));
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03005034 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00005035 istk = i->next;
5036 list->downlevel(LIST_INCLUDE);
5037 nasm_free(i);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005038 if (!istk)
5039 return NULL;
5040 if (istk->expansion && istk->expansion->finishes)
5041 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005042 }
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005043 }
5044
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005045 /*
5046 * We must expand MMacro parameters and MMacro-local labels
5047 * _before_ we plunge into directive processing, to cope
5048 * with things like `%define something %1' such as STRUC
5049 * uses. Unless we're _defining_ a MMacro, in which case
5050 * those tokens should be left alone to go into the
5051 * definition; and unless we're in a non-emitting
5052 * condition, in which case we don't want to meddle with
5053 * anything.
5054 */
5055 if (!defining && !(istk->conds && !emitting(istk->conds->state))
5056 && !(istk->mstk && !istk->mstk->in_progress)) {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005057 tline = expand_mmac_params(tline);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005058 }
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005059
H. Peter Anvine2c80182005-01-15 22:15:51 +00005060 /*
5061 * Check the line to see if it's a preprocessor directive.
5062 */
5063 if (do_directive(tline) == DIRECTIVE_FOUND) {
5064 continue;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005065 } else if (defining) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00005066 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005067 * We're defining a multi-line macro. We emit nothing
5068 * at all, and just
5069 * shove the tokenized line on to the macro definition.
H. Peter Anvine2c80182005-01-15 22:15:51 +00005070 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005071 Line *l = nasm_malloc(sizeof(Line));
5072 l->next = defining->expansion;
5073 l->first = tline;
5074 l->finishes = NULL;
5075 defining->expansion = l;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005076 continue;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005077 } else if (istk->conds && !emitting(istk->conds->state)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00005078 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005079 * We're in a non-emitting branch of a condition block.
H. Peter Anvine2c80182005-01-15 22:15:51 +00005080 * Emit nothing at all, not even a blank line: when we
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005081 * emerge from the condition we'll give a line-number
H. Peter Anvine2c80182005-01-15 22:15:51 +00005082 * directive so we keep our place correctly.
5083 */
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005084 free_tlist(tline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00005085 continue;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005086 } else if (istk->mstk && !istk->mstk->in_progress) {
5087 /*
5088 * We're in a %rep block which has been terminated, so
5089 * we're walking through to the %endrep without
5090 * emitting anything. Emit nothing at all, not even a
5091 * blank line: when we emerge from the %rep block we'll
5092 * give a line-number directive so we keep our place
5093 * correctly.
5094 */
5095 free_tlist(tline);
5096 continue;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005097 } else {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005098 tline = expand_smacro(tline);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005099 if (!expand_mmacro(tline)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00005100 /*
Keith Kaniosb7a89542007-04-12 02:40:54 +00005101 * De-tokenize the line again, and emit it.
H. Peter Anvine2c80182005-01-15 22:15:51 +00005102 */
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005103 line = detoken(tline, true);
5104 free_tlist(tline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00005105 break;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005106 } else {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005107 continue; /* expand_mmacro calls free_tlist */
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005108 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00005109 }
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005110 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005111
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005112 return line;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005113}
5114
H. Peter Anvine2c80182005-01-15 22:15:51 +00005115static void pp_cleanup(int pass)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005116{
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005117 if (defining) {
5118 if (defining->name) {
5119 error(ERR_NONFATAL,
5120 "end of file while still defining macro `%s'",
5121 defining->name);
5122 } else {
5123 error(ERR_NONFATAL, "end of file while still in %%rep");
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005124 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005125
5126 free_mmacro(defining);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005127 defining = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005128 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005129 while (cstk)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005130 ctx_pop();
H. Peter Anvin97a23472007-09-16 17:57:25 -07005131 free_macros();
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005132 while (istk) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00005133 Include *i = istk;
5134 istk = istk->next;
5135 fclose(i->fp);
5136 nasm_free(i->fname);
Cyrill Gorcunov8dcfd882011-03-03 09:18:56 +03005137 nasm_free(i);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005138 }
5139 while (cstk)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005140 ctx_pop();
H. Peter Anvin86877b22008-06-20 15:55:45 -07005141 nasm_free(src_set_fname(NULL));
H. Peter Anvine2c80182005-01-15 22:15:51 +00005142 if (pass == 0) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03005143 IncPath *i;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005144 free_llist(predef);
Cyrill Gorcunovdae24d72014-06-28 10:17:39 +04005145 predef = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005146 delete_Blocks();
Cyrill Gorcunovdae24d72014-06-28 10:17:39 +04005147 freeTokens = NULL;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03005148 while ((i = ipath)) {
5149 ipath = i->next;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005150 if (i->path)
5151 nasm_free(i->path);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03005152 nasm_free(i);
5153 }
H. Peter Anvin11dfa1a2008-07-02 18:11:04 -07005154 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005155}
5156
Cyrill Gorcunov0b78bff2012-05-07 01:57:55 +04005157static void pp_include_path(char *path)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005158{
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005159 IncPath *i;
H. Peter Anvin37a321f2007-09-24 13:41:58 -07005160
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005161 i = nasm_malloc(sizeof(IncPath));
5162 i->path = path ? nasm_strdup(path) : NULL;
5163 i->next = NULL;
Frank Kotlerd0ed6fd2003-08-27 11:33:56 +00005164
H. Peter Anvin89cee572009-07-15 09:16:54 -04005165 if (ipath) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00005166 IncPath *j = ipath;
H. Peter Anvin89cee572009-07-15 09:16:54 -04005167 while (j->next)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005168 j = j->next;
5169 j->next = i;
5170 } else {
5171 ipath = i;
Frank Kotlerd0ed6fd2003-08-27 11:33:56 +00005172 }
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005173}
Frank Kotlerd0ed6fd2003-08-27 11:33:56 +00005174
Cyrill Gorcunov0b78bff2012-05-07 01:57:55 +04005175static void pp_pre_include(char *fname)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005176{
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005177 Token *inc, *space, *name;
5178 Line *l;
5179
H. Peter Anvin734b1882002-04-30 21:01:08 +00005180 name = new_Token(NULL, TOK_INTERNAL_STRING, fname, 0);
5181 space = new_Token(name, TOK_WHITESPACE, NULL, 0);
5182 inc = new_Token(space, TOK_PREPROC_ID, "%include", 0);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005183
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005184 l = nasm_malloc(sizeof(Line));
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005185 l->next = predef;
5186 l->first = inc;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005187 l->finishes = NULL;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005188 predef = l;
5189}
5190
Cyrill Gorcunov0b78bff2012-05-07 01:57:55 +04005191static void pp_pre_define(char *definition)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005192{
5193 Token *def, *space;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005194 Line *l;
Keith Kaniosa6dfa782007-04-13 16:47:53 +00005195 char *equals;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005196
5197 equals = strchr(definition, '=');
H. Peter Anvin734b1882002-04-30 21:01:08 +00005198 space = new_Token(NULL, TOK_WHITESPACE, NULL, 0);
5199 def = new_Token(space, TOK_PREPROC_ID, "%define", 0);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005200 if (equals)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005201 *equals = ' ';
Keith Kaniosb7a89542007-04-12 02:40:54 +00005202 space->next = tokenize(definition);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005203 if (equals)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005204 *equals = '=';
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005205
Cyrill Gorcunov6d42e9b2015-02-08 11:07:17 +03005206 if (space->next->type != TOK_PREPROC_ID &&
5207 space->next->type != TOK_ID)
5208 error(ERR_WARNING, "pre-defining non ID `%s\'\n", definition);
5209
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005210 l = nasm_malloc(sizeof(Line));
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005211 l->next = predef;
5212 l->first = def;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005213 l->finishes = NULL;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005214 predef = l;
5215}
5216
Cyrill Gorcunov0b78bff2012-05-07 01:57:55 +04005217static void pp_pre_undefine(char *definition)
H. Peter Anvin620515a2002-04-30 20:57:38 +00005218{
5219 Token *def, *space;
5220 Line *l;
5221
H. Peter Anvin734b1882002-04-30 21:01:08 +00005222 space = new_Token(NULL, TOK_WHITESPACE, NULL, 0);
5223 def = new_Token(space, TOK_PREPROC_ID, "%undef", 0);
Keith Kaniosb7a89542007-04-12 02:40:54 +00005224 space->next = tokenize(definition);
H. Peter Anvin620515a2002-04-30 20:57:38 +00005225
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005226 l = nasm_malloc(sizeof(Line));
H. Peter Anvin620515a2002-04-30 20:57:38 +00005227 l->next = predef;
5228 l->first = def;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005229 l->finishes = NULL;
H. Peter Anvin620515a2002-04-30 20:57:38 +00005230 predef = l;
5231}
5232
Cyrill Gorcunov0b78bff2012-05-07 01:57:55 +04005233static void pp_extra_stdmac(macros_t *macros)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005234{
H. Peter Anvin76690a12002-04-30 20:52:49 +00005235 extrastdmac = macros;
5236}
5237
Keith Kaniosa5fc6462007-10-13 07:09:22 -07005238static void make_tok_num(Token * tok, int64_t val)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005239{
Cyrill Gorcunovce652742013-05-06 23:43:43 +04005240 char numbuf[32];
Keith Kaniosa5fc6462007-10-13 07:09:22 -07005241 snprintf(numbuf, sizeof(numbuf), "%"PRId64"", val);
H. Peter Anvineba20a72002-04-30 20:53:55 +00005242 tok->text = nasm_strdup(numbuf);
5243 tok->type = TOK_NUMBER;
5244}
5245
Cyrill Gorcunov86b2ad02011-07-01 10:38:25 +04005246struct preproc_ops nasmpp = {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005247 pp_reset,
5248 pp_getline,
Cyrill Gorcunov0b78bff2012-05-07 01:57:55 +04005249 pp_cleanup,
5250 pp_extra_stdmac,
5251 pp_pre_define,
5252 pp_pre_undefine,
5253 pp_pre_include,
5254 pp_include_path
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005255};