blob: 9b114c2e923bda3b1ad20cdc59af64d955ac9483 [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 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001187}
H. Peter Anvin734b1882002-04-30 21:01:08 +00001188
1189/*
H. Peter Anvin70653092007-10-19 14:42:29 -07001190 * this function creates a new Token and passes a pointer to it
H. Peter Anvin734b1882002-04-30 21:01:08 +00001191 * back to the caller. It sets the type and text elements, and
H. Peter Anvinf26e0972008-07-01 21:26:27 -07001192 * also the a.mac and next elements to NULL.
H. Peter Anvin734b1882002-04-30 21:01:08 +00001193 */
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07001194static Token *new_Token(Token * next, enum pp_token_type type,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001195 const char *text, int txtlen)
H. Peter Anvin734b1882002-04-30 21:01:08 +00001196{
1197 Token *t;
1198 int i;
1199
H. Peter Anvin89cee572009-07-15 09:16:54 -04001200 if (!freeTokens) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001201 freeTokens = (Token *) new_Block(TOKEN_BLOCKSIZE * sizeof(Token));
1202 for (i = 0; i < TOKEN_BLOCKSIZE - 1; i++)
1203 freeTokens[i].next = &freeTokens[i + 1];
1204 freeTokens[i].next = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001205 }
1206 t = freeTokens;
1207 freeTokens = t->next;
1208 t->next = next;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07001209 t->a.mac = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001210 t->type = type;
H. Peter Anvin89cee572009-07-15 09:16:54 -04001211 if (type == TOK_WHITESPACE || !text) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001212 t->text = NULL;
1213 } else {
1214 if (txtlen == 0)
1215 txtlen = strlen(text);
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07001216 t->text = nasm_malloc(txtlen+1);
1217 memcpy(t->text, text, txtlen);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001218 t->text[txtlen] = '\0';
H. Peter Anvin734b1882002-04-30 21:01:08 +00001219 }
1220 return t;
1221}
1222
H. Peter Anvine2c80182005-01-15 22:15:51 +00001223static Token *delete_Token(Token * t)
H. Peter Anvin734b1882002-04-30 21:01:08 +00001224{
1225 Token *next = t->next;
1226 nasm_free(t->text);
H. Peter Anvin788e6c12002-04-30 21:02:01 +00001227 t->next = freeTokens;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001228 freeTokens = t;
1229 return next;
1230}
1231
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001232/*
1233 * Convert a line of tokens back into text.
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001234 * If expand_locals is not zero, identifiers of the form "%$*xxx"
1235 * will be transformed into ..@ctxnum.xxx
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001236 */
H. Peter Anvin9e200162008-06-04 17:23:14 -07001237static char *detoken(Token * tlist, bool expand_locals)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001238{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001239 Token *t;
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001240 char *line, *p;
H. Peter Anvinb4daadc2008-01-21 16:31:57 -08001241 const char *q;
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001242 int len = 0;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001243
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001244 list_for_each(t, tlist) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001245 if (t->type == TOK_PREPROC_ID && t->text[1] == '!') {
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001246 char *v;
1247 char *q = t->text;
H. Peter Anvin077fb932010-07-20 14:56:30 -07001248
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001249 v = t->text + 2;
1250 if (*v == '\'' || *v == '\"' || *v == '`') {
1251 size_t len = nasm_unquote(v, NULL);
1252 size_t clen = strlen(v);
H. Peter Anvin077fb932010-07-20 14:56:30 -07001253
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001254 if (len != clen) {
1255 error(ERR_NONFATAL | ERR_PASS1,
1256 "NUL character in %! string");
1257 v = NULL;
1258 }
1259 }
H. Peter Anvin077fb932010-07-20 14:56:30 -07001260
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001261 if (v) {
1262 char *p = getenv(v);
1263 if (!p) {
1264 error(ERR_NONFATAL | ERR_PASS1,
1265 "nonexistent environment variable `%s'", v);
1266 p = "";
1267 }
1268 t->text = nasm_strdup(p);
1269 }
1270 nasm_free(q);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001271 }
H. Peter Anvin077fb932010-07-20 14:56:30 -07001272
H. Peter Anvine2c80182005-01-15 22:15:51 +00001273 /* Expand local macros here and not during preprocessing */
1274 if (expand_locals &&
1275 t->type == TOK_PREPROC_ID && t->text &&
1276 t->text[0] == '%' && t->text[1] == '$') {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001277 const char *q;
1278 char *p;
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04001279 Context *ctx = get_ctx(t->text, &q);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001280 if (ctx) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001281 char buffer[40];
Keith Kanios93f2e9a2007-04-14 00:10:59 +00001282 snprintf(buffer, sizeof(buffer), "..@%"PRIu32".", ctx->number);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001283 p = nasm_strcat(buffer, q);
1284 nasm_free(t->text);
1285 t->text = p;
1286 }
1287 }
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001288 if (t->type == TOK_WHITESPACE)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001289 len++;
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001290 else if (t->text)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001291 len += strlen(t->text);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001292 }
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001293
H. Peter Anvin734b1882002-04-30 21:01:08 +00001294 p = line = nasm_malloc(len + 1);
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001295
1296 list_for_each(t, tlist) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001297 if (t->type == TOK_WHITESPACE) {
H. Peter Anvinb4daadc2008-01-21 16:31:57 -08001298 *p++ = ' ';
H. Peter Anvine2c80182005-01-15 22:15:51 +00001299 } else if (t->text) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001300 q = t->text;
1301 while (*q)
1302 *p++ = *q++;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001303 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001304 }
1305 *p = '\0';
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001306
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001307 return line;
1308}
1309
1310/*
H. Peter Anvin76690a12002-04-30 20:52:49 +00001311 * A scanner, suitable for use by the expression evaluator, which
1312 * operates on a line of Tokens. Expects a pointer to a pointer to
1313 * the first token in the line to be passed in as its private_data
1314 * field.
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001315 *
1316 * FIX: This really needs to be unified with stdscan.
H. Peter Anvin76690a12002-04-30 20:52:49 +00001317 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001318static int ppscan(void *private_data, struct tokenval *tokval)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001319{
H. Peter Anvin76690a12002-04-30 20:52:49 +00001320 Token **tlineptr = private_data;
1321 Token *tline;
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001322 char ourcopy[MAX_KEYWORD+1], *p, *r, *s;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001323
H. Peter Anvine2c80182005-01-15 22:15:51 +00001324 do {
1325 tline = *tlineptr;
1326 *tlineptr = tline ? tline->next : NULL;
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04001327 } while (tline && (tline->type == TOK_WHITESPACE ||
1328 tline->type == TOK_COMMENT));
H. Peter Anvin76690a12002-04-30 20:52:49 +00001329
1330 if (!tline)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001331 return tokval->t_type = TOKEN_EOS;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001332
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001333 tokval->t_charptr = tline->text;
1334
H. Peter Anvin76690a12002-04-30 20:52:49 +00001335 if (tline->text[0] == '$' && !tline->text[1])
H. Peter Anvine2c80182005-01-15 22:15:51 +00001336 return tokval->t_type = TOKEN_HERE;
H. Peter Anvin7cf897e2002-05-30 21:30:33 +00001337 if (tline->text[0] == '$' && tline->text[1] == '$' && !tline->text[2])
H. Peter Anvine2c80182005-01-15 22:15:51 +00001338 return tokval->t_type = TOKEN_BASE;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001339
H. Peter Anvine2c80182005-01-15 22:15:51 +00001340 if (tline->type == TOK_ID) {
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001341 p = tokval->t_charptr = tline->text;
1342 if (p[0] == '$') {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001343 tokval->t_charptr++;
1344 return tokval->t_type = TOKEN_ID;
1345 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00001346
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001347 for (r = p, s = ourcopy; *r; r++) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001348 if (r >= p+MAX_KEYWORD)
1349 return tokval->t_type = TOKEN_ID; /* Not a keyword */
H. Peter Anvinac8f8fc2008-06-11 15:49:41 -07001350 *s++ = nasm_tolower(*r);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001351 }
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001352 *s = '\0';
1353 /* right, so we have an identifier sitting in temp storage. now,
1354 * is it actually a register or instruction name, or what? */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001355 return nasm_token_hash(ourcopy, tokval);
H. Peter Anvin76690a12002-04-30 20:52:49 +00001356 }
1357
H. Peter Anvine2c80182005-01-15 22:15:51 +00001358 if (tline->type == TOK_NUMBER) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001359 bool rn_error;
1360 tokval->t_integer = readnum(tline->text, &rn_error);
1361 tokval->t_charptr = tline->text;
1362 if (rn_error)
1363 return tokval->t_type = TOKEN_ERRNUM;
1364 else
1365 return tokval->t_type = TOKEN_NUM;
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001366 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00001367
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001368 if (tline->type == TOK_FLOAT) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001369 return tokval->t_type = TOKEN_FLOAT;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001370 }
1371
H. Peter Anvine2c80182005-01-15 22:15:51 +00001372 if (tline->type == TOK_STRING) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001373 char bq, *ep;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001374
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001375 bq = tline->text[0];
H. Peter Anvin11627042008-06-09 20:45:19 -07001376 tokval->t_charptr = tline->text;
1377 tokval->t_inttwo = nasm_unquote(tline->text, &ep);
H. Peter Anvind2456592008-06-19 15:04:18 -07001378
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001379 if (ep[0] != bq || ep[1] != '\0')
1380 return tokval->t_type = TOKEN_ERRSTR;
1381 else
1382 return tokval->t_type = TOKEN_STR;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001383 }
1384
H. Peter Anvine2c80182005-01-15 22:15:51 +00001385 if (tline->type == TOK_OTHER) {
1386 if (!strcmp(tline->text, "<<"))
1387 return tokval->t_type = TOKEN_SHL;
1388 if (!strcmp(tline->text, ">>"))
1389 return tokval->t_type = TOKEN_SHR;
1390 if (!strcmp(tline->text, "//"))
1391 return tokval->t_type = TOKEN_SDIV;
1392 if (!strcmp(tline->text, "%%"))
1393 return tokval->t_type = TOKEN_SMOD;
1394 if (!strcmp(tline->text, "=="))
1395 return tokval->t_type = TOKEN_EQ;
1396 if (!strcmp(tline->text, "<>"))
1397 return tokval->t_type = TOKEN_NE;
1398 if (!strcmp(tline->text, "!="))
1399 return tokval->t_type = TOKEN_NE;
1400 if (!strcmp(tline->text, "<="))
1401 return tokval->t_type = TOKEN_LE;
1402 if (!strcmp(tline->text, ">="))
1403 return tokval->t_type = TOKEN_GE;
1404 if (!strcmp(tline->text, "&&"))
1405 return tokval->t_type = TOKEN_DBL_AND;
1406 if (!strcmp(tline->text, "^^"))
1407 return tokval->t_type = TOKEN_DBL_XOR;
1408 if (!strcmp(tline->text, "||"))
1409 return tokval->t_type = TOKEN_DBL_OR;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001410 }
1411
1412 /*
1413 * We have no other options: just return the first character of
1414 * the token text.
1415 */
1416 return tokval->t_type = tline->text[0];
1417}
1418
1419/*
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001420 * Compare a string to the name of an existing macro; this is a
1421 * simple wrapper which calls either strcmp or nasm_stricmp
1422 * depending on the value of the `casesense' parameter.
1423 */
H. Peter Anvin4db5a162007-10-11 13:42:09 -07001424static int mstrcmp(const char *p, const char *q, bool casesense)
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001425{
H. Peter Anvin734b1882002-04-30 21:01:08 +00001426 return casesense ? strcmp(p, q) : nasm_stricmp(p, q);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001427}
1428
1429/*
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07001430 * Compare a string to the name of an existing macro; this is a
1431 * simple wrapper which calls either strcmp or nasm_stricmp
1432 * depending on the value of the `casesense' parameter.
1433 */
1434static int mmemcmp(const char *p, const char *q, size_t l, bool casesense)
1435{
1436 return casesense ? memcmp(p, q, l) : nasm_memicmp(p, q, l);
1437}
1438
1439/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001440 * Return the Context structure associated with a %$ token. Return
1441 * NULL, having _already_ reported an error condition, if the
1442 * context stack isn't deep enough for the supplied number of $
1443 * signs.
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001444 *
1445 * If "namep" is non-NULL, set it to the pointer to the macro name
1446 * tail, i.e. the part beyond %$...
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001447 */
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04001448static Context *get_ctx(const char *name, const char **namep)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001449{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001450 Context *ctx;
1451 int i;
1452
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001453 if (namep)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001454 *namep = name;
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001455
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001456 if (!name || name[0] != '%' || name[1] != '$')
H. Peter Anvine2c80182005-01-15 22:15:51 +00001457 return NULL;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001458
H. Peter Anvine2c80182005-01-15 22:15:51 +00001459 if (!cstk) {
1460 error(ERR_NONFATAL, "`%s': context stack is empty", name);
1461 return NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001462 }
1463
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001464 name += 2;
1465 ctx = cstk;
1466 i = 0;
1467 while (ctx && *name == '$') {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001468 name++;
1469 i++;
1470 ctx = ctx->next;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001471 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001472 if (!ctx) {
1473 error(ERR_NONFATAL, "`%s': context stack is only"
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001474 " %d level%s deep", name, i, (i == 1 ? "" : "s"));
H. Peter Anvine2c80182005-01-15 22:15:51 +00001475 return NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001476 }
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001477
1478 if (namep)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001479 *namep = name;
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001480
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04001481 return ctx;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001482}
1483
1484/*
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001485 * Check to see if a file is already in a string list
1486 */
1487static bool in_list(const StrList *list, const char *str)
1488{
1489 while (list) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001490 if (!strcmp(list->str, str))
1491 return true;
1492 list = list->next;
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001493 }
1494 return false;
1495}
1496
1497/*
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001498 * Open an include file. This routine must always return a valid
1499 * file pointer if it returns - it's responsible for throwing an
1500 * ERR_FATAL and bombing out completely if not. It should also try
1501 * the include path one by one until it finds the file or reaches
1502 * the end of the path.
1503 */
H. Peter Anvin2b1c3b92008-06-06 10:38:46 -07001504static FILE *inc_fopen(const char *file, StrList **dhead, StrList ***dtail,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001505 bool missing_ok)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001506{
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001507 FILE *fp;
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001508 char *prefix = "";
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001509 IncPath *ip = ipath;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001510 int len = strlen(file);
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001511 size_t prefix_len = 0;
1512 StrList *sl;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001513
H. Peter Anvine2c80182005-01-15 22:15:51 +00001514 while (1) {
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001515 sl = nasm_malloc(prefix_len+len+1+sizeof sl->next);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001516 memcpy(sl->str, prefix, prefix_len);
1517 memcpy(sl->str+prefix_len, file, len+1);
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001518 fp = fopen(sl->str, "r");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001519 if (fp && dhead && !in_list(*dhead, sl->str)) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001520 sl->next = NULL;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001521 **dtail = sl;
1522 *dtail = &sl->next;
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001523 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001524 nasm_free(sl);
1525 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001526 if (fp)
1527 return fp;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001528 if (!ip) {
1529 if (!missing_ok)
1530 break;
1531 prefix = NULL;
1532 } else {
1533 prefix = ip->path;
1534 ip = ip->next;
1535 }
1536 if (prefix) {
1537 prefix_len = strlen(prefix);
1538 } else {
1539 /* -MG given and file not found */
1540 if (dhead && !in_list(*dhead, file)) {
1541 sl = nasm_malloc(len+1+sizeof sl->next);
1542 sl->next = NULL;
1543 strcpy(sl->str, file);
1544 **dtail = sl;
1545 *dtail = &sl->next;
1546 }
1547 return NULL;
1548 }
H. Peter Anvineba20a72002-04-30 20:53:55 +00001549 }
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001550
H. Peter Anvin734b1882002-04-30 21:01:08 +00001551 error(ERR_FATAL, "unable to open include file `%s'", file);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001552 return NULL;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001553}
1554
1555/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001556 * Determine if we should warn on defining a single-line macro of
H. Peter Anvinef7468f2002-04-30 20:57:59 +00001557 * name `name', with `nparam' parameters. If nparam is 0 or -1, will
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001558 * return true if _any_ single-line macro of that name is defined.
1559 * Otherwise, will return true if a single-line macro with either
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001560 * `nparam' or no parameters is defined.
1561 *
1562 * If a macro with precisely the right number of parameters is
H. Peter Anvinef7468f2002-04-30 20:57:59 +00001563 * defined, or nparam is -1, the address of the definition structure
1564 * will be returned in `defn'; otherwise NULL will be returned. If `defn'
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001565 * is NULL, no action will be taken regarding its contents, and no
1566 * error will occur.
1567 *
1568 * Note that this is also called with nparam zero to resolve
1569 * `ifdef'.
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001570 *
1571 * If you already know which context macro belongs to, you can pass
1572 * the context pointer as first parameter; if you won't but name begins
1573 * with %$ the context will be automatically computed. If all_contexts
1574 * is true, macro will be searched in outer contexts as well.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001575 */
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07001576static bool
H. Peter Anvinb2a5fda2008-06-19 21:42:42 -07001577smacro_defined(Context * ctx, const char *name, int nparam, SMacro ** defn,
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07001578 bool nocase)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001579{
H. Peter Anvin166c2472008-05-28 12:28:58 -07001580 struct hash_table *smtbl;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001581 SMacro *m;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001582
H. Peter Anvin97a23472007-09-16 17:57:25 -07001583 if (ctx) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001584 smtbl = &ctx->localmac;
H. Peter Anvin97a23472007-09-16 17:57:25 -07001585 } else if (name[0] == '%' && name[1] == '$') {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001586 if (cstk)
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04001587 ctx = get_ctx(name, &name);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001588 if (!ctx)
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001589 return false; /* got to return _something_ */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001590 smtbl = &ctx->localmac;
H. Peter Anvin97a23472007-09-16 17:57:25 -07001591 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001592 smtbl = &smacros;
H. Peter Anvin97a23472007-09-16 17:57:25 -07001593 }
H. Peter Anvin166c2472008-05-28 12:28:58 -07001594 m = (SMacro *) hash_findix(smtbl, name);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001595
H. Peter Anvine2c80182005-01-15 22:15:51 +00001596 while (m) {
1597 if (!mstrcmp(m->name, name, m->casesense && nocase) &&
Charles Crayne192d5b52007-10-18 19:02:42 -07001598 (nparam <= 0 || m->nparam == 0 || nparam == (int) m->nparam)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001599 if (defn) {
Charles Crayne192d5b52007-10-18 19:02:42 -07001600 if (nparam == (int) m->nparam || nparam == -1)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001601 *defn = m;
1602 else
1603 *defn = NULL;
1604 }
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001605 return true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001606 }
1607 m = m->next;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001608 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001609
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001610 return false;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001611}
1612
1613/*
1614 * Count and mark off the parameters in a multi-line macro call.
1615 * This is called both from within the multi-line macro expansion
1616 * code, and also to mark off the default parameters when provided
1617 * in a %macro definition line.
1618 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001619static void count_mmac_params(Token * t, int *nparam, Token *** params)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001620{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001621 int paramsize, brace;
1622
1623 *nparam = paramsize = 0;
1624 *params = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001625 while (t) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001626 /* +1: we need space for the final NULL */
H. Peter Anvin91fb6f12008-09-01 10:56:33 -07001627 if (*nparam+1 >= paramsize) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001628 paramsize += PARAM_DELTA;
1629 *params = nasm_realloc(*params, sizeof(**params) * paramsize);
1630 }
1631 skip_white_(t);
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08001632 brace = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001633 if (tok_is_(t, "{"))
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08001634 brace++;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001635 (*params)[(*nparam)++] = t;
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08001636 if (brace) {
1637 while (brace && (t = t->next) != NULL) {
1638 if (tok_is_(t, "{"))
1639 brace++;
1640 else if (tok_is_(t, "}"))
1641 brace--;
1642 }
1643
1644 if (t) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001645 /*
1646 * Now we've found the closing brace, look further
1647 * for the comma.
1648 */
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08001649 t = t->next;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001650 skip_white_(t);
1651 if (tok_isnt_(t, ",")) {
1652 error(ERR_NONFATAL,
1653 "braces do not enclose all of macro parameter");
1654 while (tok_isnt_(t, ","))
1655 t = t->next;
1656 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001657 }
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08001658 } else {
1659 while (tok_isnt_(t, ","))
1660 t = t->next;
1661 }
1662 if (t) { /* got a comma/brace */
1663 t = t->next; /* eat the comma */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001664 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001665 }
1666}
1667
1668/*
H. Peter Anvin76690a12002-04-30 20:52:49 +00001669 * Determine whether one of the various `if' conditions is true or
1670 * not.
1671 *
1672 * We must free the tline we get passed.
1673 */
H. Peter Anvin70055962007-10-11 00:05:31 -07001674static bool if_condition(Token * tline, enum preproc_token ct)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001675{
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001676 enum pp_conditional i = PP_COND(ct);
H. Peter Anvin70055962007-10-11 00:05:31 -07001677 bool j;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001678 Token *t, *tt, **tptr, *origline;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001679 struct tokenval tokval;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001680 expr *evalresult;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001681 enum pp_token_type needtype;
H. Peter Anvin077fb932010-07-20 14:56:30 -07001682 char *p;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001683
1684 origline = tline;
1685
H. Peter Anvine2c80182005-01-15 22:15:51 +00001686 switch (i) {
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001687 case PPC_IFCTX:
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001688 j = false; /* have we matched yet? */
Victor van den Elzen0e857f12008-07-23 13:21:29 +02001689 while (true) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001690 skip_white_(tline);
Victor van den Elzen0e857f12008-07-23 13:21:29 +02001691 if (!tline)
1692 break;
1693 if (tline->type != TOK_ID) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001694 error(ERR_NONFATAL,
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001695 "`%s' expects context identifiers", pp_directives[ct]);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001696 free_tlist(origline);
1697 return -1;
1698 }
Victor van den Elzen0e857f12008-07-23 13:21:29 +02001699 if (cstk && cstk->name && !nasm_stricmp(tline->text, cstk->name))
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001700 j = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001701 tline = tline->next;
1702 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001703 break;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001704
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001705 case PPC_IFDEF:
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001706 j = false; /* have we matched yet? */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001707 while (tline) {
1708 skip_white_(tline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001709 if (!tline || (tline->type != TOK_ID &&
1710 (tline->type != TOK_PREPROC_ID ||
1711 tline->text[1] != '$'))) {
1712 error(ERR_NONFATAL,
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001713 "`%s' expects macro identifiers", pp_directives[ct]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001714 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001715 }
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07001716 if (smacro_defined(NULL, tline->text, 0, NULL, true))
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001717 j = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001718 tline = tline->next;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001719 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001720 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001721
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001722 case PPC_IFENV:
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001723 tline = expand_smacro(tline);
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001724 j = false; /* have we matched yet? */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001725 while (tline) {
1726 skip_white_(tline);
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001727 if (!tline || (tline->type != TOK_ID &&
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001728 tline->type != TOK_STRING &&
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001729 (tline->type != TOK_PREPROC_ID ||
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001730 tline->text[1] != '!'))) {
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001731 error(ERR_NONFATAL,
1732 "`%s' expects environment variable names",
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001733 pp_directives[ct]);
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001734 goto fail;
1735 }
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001736 p = tline->text;
1737 if (tline->type == TOK_PREPROC_ID)
1738 p += 2; /* Skip leading %! */
1739 if (*p == '\'' || *p == '\"' || *p == '`')
1740 nasm_unquote_cstr(p, ct);
1741 if (getenv(p))
1742 j = true;
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001743 tline = tline->next;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001744 }
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001745 break;
1746
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001747 case PPC_IFIDN:
1748 case PPC_IFIDNI:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001749 tline = expand_smacro(tline);
1750 t = tt = tline;
1751 while (tok_isnt_(tt, ","))
1752 tt = tt->next;
1753 if (!tt) {
1754 error(ERR_NONFATAL,
1755 "`%s' expects two comma-separated arguments",
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001756 pp_directives[ct]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001757 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001758 }
1759 tt = tt->next;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001760 j = true; /* assume equality unless proved not */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001761 while ((t->type != TOK_OTHER || strcmp(t->text, ",")) && tt) {
1762 if (tt->type == TOK_OTHER && !strcmp(tt->text, ",")) {
1763 error(ERR_NONFATAL, "`%s': more than one comma on line",
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001764 pp_directives[ct]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001765 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001766 }
1767 if (t->type == TOK_WHITESPACE) {
1768 t = t->next;
1769 continue;
1770 }
1771 if (tt->type == TOK_WHITESPACE) {
1772 tt = tt->next;
1773 continue;
1774 }
1775 if (tt->type != t->type) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001776 j = false; /* found mismatching tokens */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001777 break;
1778 }
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07001779 /* When comparing strings, need to unquote them first */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001780 if (t->type == TOK_STRING) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001781 size_t l1 = nasm_unquote(t->text, NULL);
1782 size_t l2 = nasm_unquote(tt->text, NULL);
H. Peter Anvind2456592008-06-19 15:04:18 -07001783
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001784 if (l1 != l2) {
1785 j = false;
1786 break;
1787 }
1788 if (mmemcmp(t->text, tt->text, l1, i == PPC_IFIDN)) {
1789 j = false;
1790 break;
1791 }
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07001792 } else if (mstrcmp(tt->text, t->text, i == PPC_IFIDN) != 0) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001793 j = false; /* found mismatching tokens */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001794 break;
1795 }
Nickolay Yurchenkof3b3ce22003-09-21 20:38:43 +00001796
H. Peter Anvine2c80182005-01-15 22:15:51 +00001797 t = t->next;
1798 tt = tt->next;
1799 }
1800 if ((t->type != TOK_OTHER || strcmp(t->text, ",")) || tt)
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001801 j = false; /* trailing gunk on one end or other */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001802 break;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001803
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001804 case PPC_IFMACRO:
H. Peter Anvin89cee572009-07-15 09:16:54 -04001805 {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001806 bool found = false;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001807 MMacro searching, *mmac;
H. Peter Anvin65747262002-05-07 00:10:05 +00001808
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001809 skip_white_(tline);
1810 tline = expand_id(tline);
1811 if (!tok_type_(tline, TOK_ID)) {
1812 error(ERR_NONFATAL,
1813 "`%s' expects a macro name", pp_directives[ct]);
1814 goto fail;
1815 }
1816 searching.name = nasm_strdup(tline->text);
1817 searching.casesense = true;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001818 searching.plus = false;
1819 searching.nolist = false;
1820 searching.in_progress = 0;
1821 searching.max_depth = 0;
1822 searching.rep_nest = NULL;
1823 searching.nparam_min = 0;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001824 searching.nparam_max = INT_MAX;
1825 tline = expand_smacro(tline->next);
1826 skip_white_(tline);
1827 if (!tline) {
1828 } else if (!tok_type_(tline, TOK_NUMBER)) {
1829 error(ERR_NONFATAL,
1830 "`%s' expects a parameter count or nothing",
1831 pp_directives[ct]);
1832 } else {
1833 searching.nparam_min = searching.nparam_max =
1834 readnum(tline->text, &j);
1835 if (j)
1836 error(ERR_NONFATAL,
1837 "unable to parse parameter count `%s'",
1838 tline->text);
1839 }
1840 if (tline && tok_is_(tline->next, "-")) {
1841 tline = tline->next->next;
1842 if (tok_is_(tline, "*"))
1843 searching.nparam_max = INT_MAX;
1844 else if (!tok_type_(tline, TOK_NUMBER))
1845 error(ERR_NONFATAL,
1846 "`%s' expects a parameter count after `-'",
1847 pp_directives[ct]);
1848 else {
1849 searching.nparam_max = readnum(tline->text, &j);
1850 if (j)
1851 error(ERR_NONFATAL,
1852 "unable to parse parameter count `%s'",
1853 tline->text);
1854 if (searching.nparam_min > searching.nparam_max)
1855 error(ERR_NONFATAL,
1856 "minimum parameter count exceeds maximum");
1857 }
1858 }
1859 if (tline && tok_is_(tline->next, "+")) {
1860 tline = tline->next;
1861 searching.plus = true;
1862 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001863 mmac = (MMacro *) hash_findix(&mmacros, searching.name);
1864 while (mmac) {
1865 if (!strcmp(mmac->name, searching.name) &&
1866 (mmac->nparam_min <= searching.nparam_max
1867 || searching.plus)
1868 && (searching.nparam_min <= mmac->nparam_max
1869 || mmac->plus)) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001870 found = true;
1871 break;
1872 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001873 mmac = mmac->next;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001874 }
1875 if (tline && tline->next)
1876 error(ERR_WARNING|ERR_PASS1,
1877 "trailing garbage after %%ifmacro ignored");
1878 nasm_free(searching.name);
1879 j = found;
1880 break;
H. Peter Anvin89cee572009-07-15 09:16:54 -04001881 }
H. Peter Anvin65747262002-05-07 00:10:05 +00001882
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001883 case PPC_IFID:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001884 needtype = TOK_ID;
1885 goto iftype;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001886 case PPC_IFNUM:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001887 needtype = TOK_NUMBER;
1888 goto iftype;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001889 case PPC_IFSTR:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001890 needtype = TOK_STRING;
1891 goto iftype;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001892
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001893iftype:
1894 t = tline = expand_smacro(tline);
H. Peter Anvind85d2502008-05-04 17:53:31 -07001895
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001896 while (tok_type_(t, TOK_WHITESPACE) ||
1897 (needtype == TOK_NUMBER &&
1898 tok_type_(t, TOK_OTHER) &&
1899 (t->text[0] == '-' || t->text[0] == '+') &&
1900 !t->text[1]))
1901 t = t->next;
H. Peter Anvind85d2502008-05-04 17:53:31 -07001902
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001903 j = tok_type_(t, needtype);
1904 break;
H. Peter Anvincbf768d2008-02-16 16:41:25 -08001905
1906 case PPC_IFTOKEN:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001907 t = tline = expand_smacro(tline);
1908 while (tok_type_(t, TOK_WHITESPACE))
1909 t = t->next;
H. Peter Anvincbf768d2008-02-16 16:41:25 -08001910
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001911 j = false;
1912 if (t) {
1913 t = t->next; /* Skip the actual token */
1914 while (tok_type_(t, TOK_WHITESPACE))
1915 t = t->next;
1916 j = !t; /* Should be nothing left */
1917 }
1918 break;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001919
H. Peter Anvin134b9462008-02-16 17:01:40 -08001920 case PPC_IFEMPTY:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001921 t = tline = expand_smacro(tline);
1922 while (tok_type_(t, TOK_WHITESPACE))
1923 t = t->next;
H. Peter Anvin134b9462008-02-16 17:01:40 -08001924
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001925 j = !t; /* Should be empty */
1926 break;
H. Peter Anvin134b9462008-02-16 17:01:40 -08001927
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001928 case PPC_IF:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001929 t = tline = expand_smacro(tline);
1930 tptr = &t;
1931 tokval.t_type = TOKEN_INVALID;
1932 evalresult = evaluate(ppscan, tptr, &tokval,
1933 NULL, pass | CRITICAL, error, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001934 if (!evalresult)
1935 return -1;
1936 if (tokval.t_type)
H. Peter Anvin917a3492008-09-24 09:14:49 -07001937 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +00001938 "trailing garbage after expression ignored");
1939 if (!is_simple(evalresult)) {
1940 error(ERR_NONFATAL,
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001941 "non-constant value given to `%s'", pp_directives[ct]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001942 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001943 }
Chuck Crayne60ae75d2007-05-02 01:59:16 +00001944 j = reloc_value(evalresult) != 0;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001945 break;
H. Peter Anvin95e28822007-09-12 04:20:08 +00001946
H. Peter Anvine2c80182005-01-15 22:15:51 +00001947 default:
1948 error(ERR_FATAL,
1949 "preprocessor directive `%s' not yet implemented",
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001950 pp_directives[ct]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001951 goto fail;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001952 }
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001953
1954 free_tlist(origline);
1955 return j ^ PP_NEGATIVE(ct);
H. Peter Anvin70653092007-10-19 14:42:29 -07001956
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001957fail:
1958 free_tlist(origline);
1959 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001960}
1961
1962/*
H. Peter Anvin4db5a162007-10-11 13:42:09 -07001963 * Common code for defining an smacro
1964 */
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001965static bool define_smacro(Context *ctx, const char *mname, bool casesense,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001966 int nparam, Token *expansion)
H. Peter Anvin4db5a162007-10-11 13:42:09 -07001967{
1968 SMacro *smac, **smhead;
H. Peter Anvin166c2472008-05-28 12:28:58 -07001969 struct hash_table *smtbl;
H. Peter Anvin70653092007-10-19 14:42:29 -07001970
H. Peter Anvin4db5a162007-10-11 13:42:09 -07001971 if (smacro_defined(ctx, mname, nparam, &smac, casesense)) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001972 if (!smac) {
1973 error(ERR_WARNING|ERR_PASS1,
1974 "single-line macro `%s' defined both with and"
1975 " without parameters", mname);
1976 /*
1977 * Some instances of the old code considered this a failure,
1978 * some others didn't. What is the right thing to do here?
1979 */
1980 free_tlist(expansion);
1981 return false; /* Failure */
1982 } else {
1983 /*
1984 * We're redefining, so we have to take over an
1985 * existing SMacro structure. This means freeing
1986 * what was already in it.
1987 */
1988 nasm_free(smac->name);
1989 free_tlist(smac->expansion);
1990 }
H. Peter Anvin4db5a162007-10-11 13:42:09 -07001991 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001992 smtbl = ctx ? &ctx->localmac : &smacros;
1993 smhead = (SMacro **) hash_findi_add(smtbl, mname);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001994 smac = nasm_malloc(sizeof(SMacro));
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001995 smac->next = *smhead;
1996 *smhead = smac;
H. Peter Anvin4db5a162007-10-11 13:42:09 -07001997 }
1998 smac->name = nasm_strdup(mname);
1999 smac->casesense = casesense;
2000 smac->nparam = nparam;
2001 smac->expansion = expansion;
2002 smac->in_progress = false;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002003 return true; /* Success */
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002004}
2005
2006/*
2007 * Undefine an smacro
2008 */
2009static void undef_smacro(Context *ctx, const char *mname)
2010{
2011 SMacro **smhead, *s, **sp;
H. Peter Anvin166c2472008-05-28 12:28:58 -07002012 struct hash_table *smtbl;
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002013
H. Peter Anvin166c2472008-05-28 12:28:58 -07002014 smtbl = ctx ? &ctx->localmac : &smacros;
2015 smhead = (SMacro **)hash_findi(smtbl, mname, NULL);
H. Peter Anvin70653092007-10-19 14:42:29 -07002016
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002017 if (smhead) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002018 /*
2019 * We now have a macro name... go hunt for it.
2020 */
2021 sp = smhead;
2022 while ((s = *sp) != NULL) {
2023 if (!mstrcmp(s->name, mname, s->casesense)) {
2024 *sp = s->next;
2025 nasm_free(s->name);
2026 free_tlist(s->expansion);
2027 nasm_free(s);
2028 } else {
2029 sp = &s->next;
2030 }
2031 }
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002032 }
2033}
2034
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002035/*
H. Peter Anvina26433d2008-07-16 14:40:01 -07002036 * Parse a mmacro specification.
2037 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002038static bool parse_mmacro_spec(Token *tline, MMacro *def, const char *directive)
H. Peter Anvina26433d2008-07-16 14:40:01 -07002039{
2040 bool err;
2041
2042 tline = tline->next;
2043 skip_white_(tline);
2044 tline = expand_id(tline);
2045 if (!tok_type_(tline, TOK_ID)) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002046 error(ERR_NONFATAL, "`%s' expects a macro name", directive);
2047 return false;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002048 }
Victor van den Elzenb916ede2008-07-23 15:14:22 +02002049
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002050 def->prev = NULL;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002051 def->name = nasm_strdup(tline->text);
2052 def->plus = false;
2053 def->nolist = false;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002054 def->in_progress = 0;
2055 def->rep_nest = NULL;
Victor van den Elzenb916ede2008-07-23 15:14:22 +02002056 def->nparam_min = 0;
2057 def->nparam_max = 0;
2058
H. Peter Anvina26433d2008-07-16 14:40:01 -07002059 tline = expand_smacro(tline->next);
2060 skip_white_(tline);
2061 if (!tok_type_(tline, TOK_NUMBER)) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002062 error(ERR_NONFATAL, "`%s' expects a parameter count", directive);
H. Peter Anvina26433d2008-07-16 14:40:01 -07002063 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002064 def->nparam_min = def->nparam_max =
2065 readnum(tline->text, &err);
2066 if (err)
2067 error(ERR_NONFATAL,
2068 "unable to parse parameter count `%s'", tline->text);
H. Peter Anvina26433d2008-07-16 14:40:01 -07002069 }
2070 if (tline && tok_is_(tline->next, "-")) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002071 tline = tline->next->next;
2072 if (tok_is_(tline, "*")) {
2073 def->nparam_max = INT_MAX;
2074 } else if (!tok_type_(tline, TOK_NUMBER)) {
2075 error(ERR_NONFATAL,
2076 "`%s' expects a parameter count after `-'", directive);
2077 } else {
2078 def->nparam_max = readnum(tline->text, &err);
2079 if (err) {
2080 error(ERR_NONFATAL, "unable to parse parameter count `%s'",
2081 tline->text);
2082 }
2083 if (def->nparam_min > def->nparam_max) {
2084 error(ERR_NONFATAL, "minimum parameter count exceeds maximum");
2085 }
2086 }
H. Peter Anvina26433d2008-07-16 14:40:01 -07002087 }
2088 if (tline && tok_is_(tline->next, "+")) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002089 tline = tline->next;
2090 def->plus = true;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002091 }
2092 if (tline && tok_type_(tline->next, TOK_ID) &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002093 !nasm_stricmp(tline->next->text, ".nolist")) {
2094 tline = tline->next;
2095 def->nolist = true;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002096 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002097
H. Peter Anvina26433d2008-07-16 14:40:01 -07002098 /*
2099 * Handle default parameters.
2100 */
2101 if (tline && tline->next) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002102 def->dlist = tline->next;
2103 tline->next = NULL;
2104 count_mmac_params(def->dlist, &def->ndefs, &def->defaults);
H. Peter Anvina26433d2008-07-16 14:40:01 -07002105 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002106 def->dlist = NULL;
2107 def->defaults = NULL;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002108 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002109 def->expansion = NULL;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002110
H. Peter Anvin89cee572009-07-15 09:16:54 -04002111 if (def->defaults && def->ndefs > def->nparam_max - def->nparam_min &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002112 !def->plus)
2113 error(ERR_WARNING|ERR_PASS1|ERR_WARN_MDP,
2114 "too many default macro parameters");
Victor van den Elzenb916ede2008-07-23 15:14:22 +02002115
H. Peter Anvina26433d2008-07-16 14:40:01 -07002116 return true;
2117}
2118
2119
2120/*
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002121 * Decode a size directive
2122 */
2123static int parse_size(const char *str) {
2124 static const char *size_names[] =
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002125 { "byte", "dword", "oword", "qword", "tword", "word", "yword" };
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002126 static const int sizes[] =
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002127 { 0, 1, 4, 16, 8, 10, 2, 32 };
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002128
Cyrill Gorcunova7319242010-06-03 22:04:36 +04002129 return sizes[bsii(str, size_names, ARRAY_SIZE(size_names))+1];
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002130}
2131
Ed Beroset3ab3f412002-06-11 03:31:49 +00002132/**
2133 * find and process preprocessor directive in passed line
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002134 * Find out if a line contains a preprocessor directive, and deal
2135 * with it if so.
H. Peter Anvin70653092007-10-19 14:42:29 -07002136 *
Ed Beroset3ab3f412002-06-11 03:31:49 +00002137 * If a directive _is_ found, it is the responsibility of this routine
2138 * (and not the caller) to free_tlist() the line.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002139 *
Ed Beroset3ab3f412002-06-11 03:31:49 +00002140 * @param tline a pointer to the current tokeninzed line linked list
2141 * @return DIRECTIVE_FOUND or NO_DIRECTIVE_FOUND
H. Peter Anvin70653092007-10-19 14:42:29 -07002142 *
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002143 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002144static int do_directive(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00002145{
H. Peter Anvin4169a472007-09-12 01:29:43 +00002146 enum preproc_token i;
2147 int j;
H. Peter Anvin70055962007-10-11 00:05:31 -07002148 bool err;
2149 int nparam;
2150 bool nolist;
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07002151 bool casesense;
H. Peter Anvin8cfdb9d2007-09-14 18:36:01 -07002152 int k, m;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002153 int offset;
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08002154 char *p, *pp;
2155 const char *mname;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002156 Include *inc;
2157 Context *ctx;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002158 Cond *cond;
2159 MMacro *mmac, **mmhead;
Jin Kyu Songc9486b92013-10-28 17:07:57 -07002160 Token *t = NULL, *tt, *param_start, *macro_start, *last, **tptr, *origline;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002161 Line *l;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002162 struct tokenval tokval;
2163 expr *evalresult;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002164 MMacro *tmp_defining; /* Used when manipulating rep_nest */
H. Peter Anvinf8ba53e2007-10-11 10:11:57 -07002165 int64_t count;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07002166 size_t len;
H. Peter Anvin8e3f75e2008-09-24 00:21:58 -07002167 int severity;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002168
2169 origline = tline;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002170
H. Peter Anvineba20a72002-04-30 20:53:55 +00002171 skip_white_(tline);
H. Peter Anvinf2936d72008-06-04 15:11:23 -07002172 if (!tline || !tok_type_(tline, TOK_PREPROC_ID) ||
H. Peter Anvine2c80182005-01-15 22:15:51 +00002173 (tline->text[1] == '%' || tline->text[1] == '$'
2174 || tline->text[1] == '!'))
2175 return NO_DIRECTIVE_FOUND;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002176
H. Peter Anvin4169a472007-09-12 01:29:43 +00002177 i = pp_token_hash(tline->text);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002178
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002179 /*
2180 * FIXME: We zap execution of PP_RMACRO, PP_IRMACRO, PP_EXITMACRO
2181 * since they are known to be buggy at moment, we need to fix them
2182 * in future release (2.09-2.10)
2183 */
Philipp Klokeb432f572013-03-31 12:01:23 +02002184 if (i == PP_RMACRO || i == PP_IRMACRO || i == PP_EXITMACRO) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002185 error(ERR_NONFATAL, "unknown preprocessor directive `%s'",
2186 tline->text);
2187 return NO_DIRECTIVE_FOUND;
2188 }
2189
2190 /*
2191 * If we're in a non-emitting branch of a condition construct,
2192 * or walking to the end of an already terminated %rep block,
2193 * we should ignore all directives except for condition
2194 * directives.
2195 */
2196 if (((istk->conds && !emitting(istk->conds->state)) ||
2197 (istk->mstk && !istk->mstk->in_progress)) && !is_condition(i)) {
2198 return NO_DIRECTIVE_FOUND;
2199 }
2200
2201 /*
2202 * If we're defining a macro or reading a %rep block, we should
2203 * ignore all directives except for %macro/%imacro (which nest),
2204 * %endm/%endmacro, and (only if we're in a %rep block) %endrep.
2205 * If we're in a %rep block, another %rep nests, so should be let through.
2206 */
2207 if (defining && i != PP_MACRO && i != PP_IMACRO &&
2208 i != PP_RMACRO && i != PP_IRMACRO &&
2209 i != PP_ENDMACRO && i != PP_ENDM &&
2210 (defining->name || (i != PP_ENDREP && i != PP_REP))) {
2211 return NO_DIRECTIVE_FOUND;
2212 }
2213
2214 if (defining) {
2215 if (i == PP_MACRO || i == PP_IMACRO ||
2216 i == PP_RMACRO || i == PP_IRMACRO) {
2217 nested_mac_count++;
2218 return NO_DIRECTIVE_FOUND;
2219 } else if (nested_mac_count > 0) {
2220 if (i == PP_ENDMACRO) {
2221 nested_mac_count--;
2222 return NO_DIRECTIVE_FOUND;
2223 }
2224 }
2225 if (!defining->name) {
2226 if (i == PP_REP) {
2227 nested_rep_count++;
2228 return NO_DIRECTIVE_FOUND;
2229 } else if (nested_rep_count > 0) {
2230 if (i == PP_ENDREP) {
2231 nested_rep_count--;
2232 return NO_DIRECTIVE_FOUND;
2233 }
2234 }
2235 }
2236 }
2237
H. Peter Anvin4169a472007-09-12 01:29:43 +00002238 switch (i) {
2239 case PP_INVALID:
H. Peter Anvine2c80182005-01-15 22:15:51 +00002240 error(ERR_NONFATAL, "unknown preprocessor directive `%s'",
2241 tline->text);
2242 return NO_DIRECTIVE_FOUND; /* didn't get it */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002243
H. Peter Anvine2c80182005-01-15 22:15:51 +00002244 case PP_STACKSIZE:
2245 /* Directive to tell NASM what the default stack size is. The
2246 * default is for a 16-bit stack, and this can be overriden with
2247 * %stacksize large.
H. Peter Anvine2c80182005-01-15 22:15:51 +00002248 */
2249 tline = tline->next;
2250 if (tline && tline->type == TOK_WHITESPACE)
2251 tline = tline->next;
2252 if (!tline || tline->type != TOK_ID) {
2253 error(ERR_NONFATAL, "`%%stacksize' missing size parameter");
2254 free_tlist(origline);
2255 return DIRECTIVE_FOUND;
2256 }
2257 if (nasm_stricmp(tline->text, "flat") == 0) {
2258 /* All subsequent ARG directives are for a 32-bit stack */
2259 StackSize = 4;
2260 StackPointer = "ebp";
2261 ArgOffset = 8;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002262 LocalOffset = 0;
Charles Crayne7eaf9192007-11-08 22:11:14 -08002263 } else if (nasm_stricmp(tline->text, "flat64") == 0) {
2264 /* All subsequent ARG directives are for a 64-bit stack */
2265 StackSize = 8;
2266 StackPointer = "rbp";
Per Jessen53252e02010-02-11 00:16:59 +03002267 ArgOffset = 16;
Charles Crayne7eaf9192007-11-08 22:11:14 -08002268 LocalOffset = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002269 } else if (nasm_stricmp(tline->text, "large") == 0) {
2270 /* All subsequent ARG directives are for a 16-bit stack,
2271 * far function call.
2272 */
2273 StackSize = 2;
2274 StackPointer = "bp";
2275 ArgOffset = 4;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002276 LocalOffset = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002277 } else if (nasm_stricmp(tline->text, "small") == 0) {
2278 /* All subsequent ARG directives are for a 16-bit stack,
2279 * far function call. We don't support near functions.
2280 */
2281 StackSize = 2;
2282 StackPointer = "bp";
2283 ArgOffset = 6;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002284 LocalOffset = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002285 } else {
2286 error(ERR_NONFATAL, "`%%stacksize' invalid size type");
2287 free_tlist(origline);
2288 return DIRECTIVE_FOUND;
2289 }
2290 free_tlist(origline);
2291 return DIRECTIVE_FOUND;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002292
H. Peter Anvine2c80182005-01-15 22:15:51 +00002293 case PP_ARG:
2294 /* TASM like ARG directive to define arguments to functions, in
2295 * the following form:
2296 *
2297 * ARG arg1:WORD, arg2:DWORD, arg4:QWORD
2298 */
2299 offset = ArgOffset;
2300 do {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00002301 char *arg, directive[256];
H. Peter Anvine2c80182005-01-15 22:15:51 +00002302 int size = StackSize;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002303
H. Peter Anvine2c80182005-01-15 22:15:51 +00002304 /* Find the argument name */
2305 tline = tline->next;
2306 if (tline && tline->type == TOK_WHITESPACE)
2307 tline = tline->next;
2308 if (!tline || tline->type != TOK_ID) {
2309 error(ERR_NONFATAL, "`%%arg' missing argument parameter");
2310 free_tlist(origline);
2311 return DIRECTIVE_FOUND;
2312 }
2313 arg = tline->text;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002314
H. Peter Anvine2c80182005-01-15 22:15:51 +00002315 /* Find the argument size type */
2316 tline = tline->next;
2317 if (!tline || tline->type != TOK_OTHER
2318 || tline->text[0] != ':') {
2319 error(ERR_NONFATAL,
2320 "Syntax error processing `%%arg' directive");
2321 free_tlist(origline);
2322 return DIRECTIVE_FOUND;
2323 }
2324 tline = tline->next;
2325 if (!tline || tline->type != TOK_ID) {
2326 error(ERR_NONFATAL, "`%%arg' missing size type parameter");
2327 free_tlist(origline);
2328 return DIRECTIVE_FOUND;
2329 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002330
H. Peter Anvine2c80182005-01-15 22:15:51 +00002331 /* Allow macro expansion of type parameter */
Keith Kaniosb7a89542007-04-12 02:40:54 +00002332 tt = tokenize(tline->text);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002333 tt = expand_smacro(tt);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002334 size = parse_size(tt->text);
2335 if (!size) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002336 error(ERR_NONFATAL,
2337 "Invalid size type for `%%arg' missing directive");
2338 free_tlist(tt);
2339 free_tlist(origline);
2340 return DIRECTIVE_FOUND;
2341 }
2342 free_tlist(tt);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002343
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002344 /* Round up to even stack slots */
2345 size = ALIGN(size, StackSize);
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002346
H. Peter Anvine2c80182005-01-15 22:15:51 +00002347 /* Now define the macro for the argument */
2348 snprintf(directive, sizeof(directive), "%%define %s (%s+%d)",
2349 arg, StackPointer, offset);
Keith Kaniosb7a89542007-04-12 02:40:54 +00002350 do_directive(tokenize(directive));
H. Peter Anvine2c80182005-01-15 22:15:51 +00002351 offset += size;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002352
H. Peter Anvine2c80182005-01-15 22:15:51 +00002353 /* Move to the next argument in the list */
2354 tline = tline->next;
2355 if (tline && tline->type == TOK_WHITESPACE)
2356 tline = tline->next;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002357 } while (tline && tline->type == TOK_OTHER && tline->text[0] == ',');
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002358 ArgOffset = offset;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002359 free_tlist(origline);
2360 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002361
H. Peter Anvine2c80182005-01-15 22:15:51 +00002362 case PP_LOCAL:
2363 /* TASM like LOCAL directive to define local variables for a
2364 * function, in the following form:
2365 *
2366 * LOCAL local1:WORD, local2:DWORD, local4:QWORD = LocalSize
2367 *
2368 * The '= LocalSize' at the end is ignored by NASM, but is
2369 * required by TASM to define the local parameter size (and used
2370 * by the TASM macro package).
2371 */
2372 offset = LocalOffset;
2373 do {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00002374 char *local, directive[256];
H. Peter Anvine2c80182005-01-15 22:15:51 +00002375 int size = StackSize;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002376
H. Peter Anvine2c80182005-01-15 22:15:51 +00002377 /* Find the argument name */
2378 tline = tline->next;
2379 if (tline && tline->type == TOK_WHITESPACE)
2380 tline = tline->next;
2381 if (!tline || tline->type != TOK_ID) {
2382 error(ERR_NONFATAL,
2383 "`%%local' missing argument parameter");
2384 free_tlist(origline);
2385 return DIRECTIVE_FOUND;
2386 }
2387 local = tline->text;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002388
H. Peter Anvine2c80182005-01-15 22:15:51 +00002389 /* Find the argument size type */
2390 tline = tline->next;
2391 if (!tline || tline->type != TOK_OTHER
2392 || tline->text[0] != ':') {
2393 error(ERR_NONFATAL,
2394 "Syntax error processing `%%local' directive");
2395 free_tlist(origline);
2396 return DIRECTIVE_FOUND;
2397 }
2398 tline = tline->next;
2399 if (!tline || tline->type != TOK_ID) {
2400 error(ERR_NONFATAL,
2401 "`%%local' missing size type parameter");
2402 free_tlist(origline);
2403 return DIRECTIVE_FOUND;
2404 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00002405
H. Peter Anvine2c80182005-01-15 22:15:51 +00002406 /* Allow macro expansion of type parameter */
Keith Kaniosb7a89542007-04-12 02:40:54 +00002407 tt = tokenize(tline->text);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002408 tt = expand_smacro(tt);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002409 size = parse_size(tt->text);
2410 if (!size) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002411 error(ERR_NONFATAL,
2412 "Invalid size type for `%%local' missing directive");
2413 free_tlist(tt);
2414 free_tlist(origline);
2415 return DIRECTIVE_FOUND;
2416 }
2417 free_tlist(tt);
H. Peter Anvin734b1882002-04-30 21:01:08 +00002418
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002419 /* Round up to even stack slots */
2420 size = ALIGN(size, StackSize);
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002421
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002422 offset += size; /* Negative offset, increment before */
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002423
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002424 /* Now define the macro for the argument */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002425 snprintf(directive, sizeof(directive), "%%define %s (%s-%d)",
2426 local, StackPointer, offset);
Keith Kaniosb7a89542007-04-12 02:40:54 +00002427 do_directive(tokenize(directive));
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002428
H. Peter Anvine2c80182005-01-15 22:15:51 +00002429 /* Now define the assign to setup the enter_c macro correctly */
2430 snprintf(directive, sizeof(directive),
2431 "%%assign %%$localsize %%$localsize+%d", size);
Keith Kaniosb7a89542007-04-12 02:40:54 +00002432 do_directive(tokenize(directive));
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002433
H. Peter Anvine2c80182005-01-15 22:15:51 +00002434 /* Move to the next argument in the list */
2435 tline = tline->next;
2436 if (tline && tline->type == TOK_WHITESPACE)
2437 tline = tline->next;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002438 } while (tline && tline->type == TOK_OTHER && tline->text[0] == ',');
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002439 LocalOffset = offset;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002440 free_tlist(origline);
2441 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002442
H. Peter Anvine2c80182005-01-15 22:15:51 +00002443 case PP_CLEAR:
2444 if (tline->next)
H. Peter Anvin917a3492008-09-24 09:14:49 -07002445 error(ERR_WARNING|ERR_PASS1,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002446 "trailing garbage after `%%clear' ignored");
2447 free_macros();
2448 init_macros();
H. Peter Anvine2c80182005-01-15 22:15:51 +00002449 free_tlist(origline);
2450 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002451
H. Peter Anvin418ca702008-05-30 10:42:30 -07002452 case PP_DEPEND:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002453 t = tline->next = expand_smacro(tline->next);
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002454 skip_white_(t);
2455 if (!t || (t->type != TOK_STRING &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002456 t->type != TOK_INTERNAL_STRING)) {
H. Peter Anvin418ca702008-05-30 10:42:30 -07002457 error(ERR_NONFATAL, "`%%depend' expects a file name");
2458 free_tlist(origline);
2459 return DIRECTIVE_FOUND; /* but we did _something_ */
2460 }
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002461 if (t->next)
H. Peter Anvin917a3492008-09-24 09:14:49 -07002462 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvin418ca702008-05-30 10:42:30 -07002463 "trailing garbage after `%%depend' ignored");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002464 p = t->text;
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002465 if (t->type != TOK_INTERNAL_STRING)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002466 nasm_unquote_cstr(p, i);
2467 if (dephead && !in_list(*dephead, p)) {
2468 StrList *sl = nasm_malloc(strlen(p)+1+sizeof sl->next);
2469 sl->next = NULL;
2470 strcpy(sl->str, p);
2471 *deptail = sl;
2472 deptail = &sl->next;
2473 }
2474 free_tlist(origline);
H. Peter Anvin418ca702008-05-30 10:42:30 -07002475 return DIRECTIVE_FOUND;
2476
2477 case PP_INCLUDE:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002478 t = tline->next = expand_smacro(tline->next);
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002479 skip_white_(t);
H. Peter Anvind2456592008-06-19 15:04:18 -07002480
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002481 if (!t || (t->type != TOK_STRING &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002482 t->type != TOK_INTERNAL_STRING)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002483 error(ERR_NONFATAL, "`%%include' expects a file name");
2484 free_tlist(origline);
2485 return DIRECTIVE_FOUND; /* but we did _something_ */
2486 }
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002487 if (t->next)
H. Peter Anvin917a3492008-09-24 09:14:49 -07002488 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +00002489 "trailing garbage after `%%include' ignored");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002490 p = t->text;
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002491 if (t->type != TOK_INTERNAL_STRING)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002492 nasm_unquote_cstr(p, i);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002493 inc = nasm_malloc(sizeof(Include));
H. Peter Anvine2c80182005-01-15 22:15:51 +00002494 inc->next = istk;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002495 inc->conds = NULL;
H. Peter Anvin2b1c3b92008-06-06 10:38:46 -07002496 inc->fp = inc_fopen(p, dephead, &deptail, pass == 0);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002497 if (!inc->fp) {
2498 /* -MG given but file not found */
2499 nasm_free(inc);
2500 } else {
2501 inc->fname = src_set_fname(nasm_strdup(p));
2502 inc->lineno = src_set_linnum(0);
2503 inc->lineinc = 1;
2504 inc->expansion = NULL;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002505 inc->mstk = NULL;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002506 istk = inc;
2507 list->uplevel(LIST_INCLUDE);
2508 }
2509 free_tlist(origline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002510 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002511
H. Peter Anvind2456592008-06-19 15:04:18 -07002512 case PP_USE:
H. Peter Anvinf4ae5ad2008-06-19 18:39:24 -07002513 {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002514 static macros_t *use_pkg;
2515 const char *pkg_macro = NULL;
H. Peter Anvinf4ae5ad2008-06-19 18:39:24 -07002516
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002517 tline = tline->next;
2518 skip_white_(tline);
2519 tline = expand_id(tline);
H. Peter Anvind2456592008-06-19 15:04:18 -07002520
H. Peter Anvin264b7b92008-10-24 16:38:17 -07002521 if (!tline || (tline->type != TOK_STRING &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002522 tline->type != TOK_INTERNAL_STRING &&
2523 tline->type != TOK_ID)) {
H. Peter Anvin926fc402008-06-19 16:26:12 -07002524 error(ERR_NONFATAL, "`%%use' expects a package name");
H. Peter Anvind2456592008-06-19 15:04:18 -07002525 free_tlist(origline);
2526 return DIRECTIVE_FOUND; /* but we did _something_ */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002527 }
H. Peter Anvin264b7b92008-10-24 16:38:17 -07002528 if (tline->next)
H. Peter Anvin917a3492008-09-24 09:14:49 -07002529 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvind2456592008-06-19 15:04:18 -07002530 "trailing garbage after `%%use' ignored");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002531 if (tline->type == TOK_STRING)
2532 nasm_unquote_cstr(tline->text, i);
2533 use_pkg = nasm_stdmac_find_package(tline->text);
2534 if (!use_pkg)
2535 error(ERR_NONFATAL, "unknown `%%use' package: %s", tline->text);
2536 else
2537 pkg_macro = (char *)use_pkg + 1; /* The first string will be <%define>__USE_*__ */
Victor van den Elzen35eb2ea2010-03-10 22:33:48 +01002538 if (use_pkg && ! smacro_defined(NULL, pkg_macro, 0, NULL, true)) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002539 /* Not already included, go ahead and include it */
2540 stdmacpos = use_pkg;
2541 }
2542 free_tlist(origline);
H. Peter Anvind2456592008-06-19 15:04:18 -07002543 return DIRECTIVE_FOUND;
H. Peter Anvinf4ae5ad2008-06-19 18:39:24 -07002544 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002545 case PP_PUSH:
H. Peter Anvine2c80182005-01-15 22:15:51 +00002546 case PP_REPL:
H. Peter Anvin42b56392008-10-24 16:24:21 -07002547 case PP_POP:
H. Peter Anvine2c80182005-01-15 22:15:51 +00002548 tline = tline->next;
2549 skip_white_(tline);
2550 tline = expand_id(tline);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002551 if (tline) {
2552 if (!tok_type_(tline, TOK_ID)) {
2553 error(ERR_NONFATAL, "`%s' expects a context identifier",
2554 pp_directives[i]);
2555 free_tlist(origline);
2556 return DIRECTIVE_FOUND; /* but we did _something_ */
2557 }
2558 if (tline->next)
2559 error(ERR_WARNING|ERR_PASS1,
2560 "trailing garbage after `%s' ignored",
2561 pp_directives[i]);
2562 p = nasm_strdup(tline->text);
2563 } else {
2564 p = NULL; /* Anonymous */
2565 }
H. Peter Anvin42b56392008-10-24 16:24:21 -07002566
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002567 if (i == PP_PUSH) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002568 ctx = nasm_malloc(sizeof(Context));
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002569 ctx->next = cstk;
2570 hash_init(&ctx->localmac, HASH_SMALL);
2571 ctx->name = p;
2572 ctx->number = unique++;
2573 cstk = ctx;
2574 } else {
2575 /* %pop or %repl */
2576 if (!cstk) {
2577 error(ERR_NONFATAL, "`%s': context stack is empty",
2578 pp_directives[i]);
2579 } else if (i == PP_POP) {
2580 if (p && (!cstk->name || nasm_stricmp(p, cstk->name)))
2581 error(ERR_NONFATAL, "`%%pop' in wrong context: %s, "
2582 "expected %s",
2583 cstk->name ? cstk->name : "anonymous", p);
2584 else
2585 ctx_pop();
2586 } else {
2587 /* i == PP_REPL */
2588 nasm_free(cstk->name);
2589 cstk->name = p;
2590 p = NULL;
2591 }
2592 nasm_free(p);
2593 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002594 free_tlist(origline);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002595 return DIRECTIVE_FOUND;
H. Peter Anvin8e3f75e2008-09-24 00:21:58 -07002596 case PP_FATAL:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002597 severity = ERR_FATAL;
2598 goto issue_error;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002599 case PP_ERROR:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002600 severity = ERR_NONFATAL;
2601 goto issue_error;
H. Peter Anvin7df04172008-06-10 18:27:38 -07002602 case PP_WARNING:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002603 severity = ERR_WARNING|ERR_WARN_USER;
2604 goto issue_error;
H. Peter Anvin8e3f75e2008-09-24 00:21:58 -07002605
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002606issue_error:
H. Peter Anvin7df04172008-06-10 18:27:38 -07002607 {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002608 /* Only error out if this is the final pass */
2609 if (pass != 2 && i != PP_FATAL)
2610 return DIRECTIVE_FOUND;
2611
2612 tline->next = expand_smacro(tline->next);
2613 tline = tline->next;
2614 skip_white_(tline);
2615 t = tline ? tline->next : NULL;
2616 skip_white_(t);
2617 if (tok_type_(tline, TOK_STRING) && !t) {
2618 /* The line contains only a quoted string */
2619 p = tline->text;
2620 nasm_unquote(p, NULL); /* Ignore NUL character truncation */
2621 error(severity, "%s", p);
2622 } else {
2623 /* Not a quoted string, or more than a quoted string */
2624 p = detoken(tline, false);
2625 error(severity, "%s", p);
2626 nasm_free(p);
2627 }
2628 free_tlist(origline);
2629 return DIRECTIVE_FOUND;
H. Peter Anvin7df04172008-06-10 18:27:38 -07002630 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00002631
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002632 CASE_PP_IF:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002633 if (istk->conds && !emitting(istk->conds->state))
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002634 j = COND_NEVER;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002635 else {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002636 j = if_condition(tline->next, i);
2637 tline->next = NULL; /* it got freed */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002638 j = j < 0 ? COND_NEVER : j ? COND_IF_TRUE : COND_IF_FALSE;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002639 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002640 cond = nasm_malloc(sizeof(Cond));
2641 cond->next = istk->conds;
2642 cond->state = j;
2643 istk->conds = cond;
2644 if(istk->mstk)
2645 istk->mstk->condcnt ++;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002646 free_tlist(origline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002647 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002648
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002649 CASE_PP_ELIF:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002650 if (!istk->conds)
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002651 error(ERR_FATAL, "`%s': no matching `%%if'", pp_directives[i]);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002652 switch(istk->conds->state) {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002653 case COND_IF_TRUE:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002654 istk->conds->state = COND_DONE;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002655 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002656
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002657 case COND_DONE:
2658 case COND_NEVER:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002659 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002660
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002661 case COND_ELSE_TRUE:
2662 case COND_ELSE_FALSE:
2663 error_precond(ERR_WARNING|ERR_PASS1,
2664 "`%%elif' after `%%else' ignored");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002665 istk->conds->state = COND_NEVER;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002666 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002667
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002668 case COND_IF_FALSE:
2669 /*
2670 * IMPORTANT: In the case of %if, we will already have
2671 * called expand_mmac_params(); however, if we're
2672 * processing an %elif we must have been in a
2673 * non-emitting mode, which would have inhibited
2674 * the normal invocation of expand_mmac_params().
2675 * Therefore, we have to do it explicitly here.
2676 */
2677 j = if_condition(expand_mmac_params(tline->next), i);
2678 tline->next = NULL; /* it got freed */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002679 istk->conds->state =
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002680 j < 0 ? COND_NEVER : j ? COND_IF_TRUE : COND_IF_FALSE;
2681 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002682 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002683 free_tlist(origline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002684 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002685
H. Peter Anvine2c80182005-01-15 22:15:51 +00002686 case PP_ELSE:
2687 if (tline->next)
H. Peter Anvin917a3492008-09-24 09:14:49 -07002688 error_precond(ERR_WARNING|ERR_PASS1,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002689 "trailing garbage after `%%else' ignored");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002690 if (!istk->conds)
2691 error(ERR_FATAL, "`%%else': no matching `%%if'");
2692 switch(istk->conds->state) {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002693 case COND_IF_TRUE:
2694 case COND_DONE:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002695 istk->conds->state = COND_ELSE_FALSE;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002696 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002697
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002698 case COND_NEVER:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002699 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002700
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002701 case COND_IF_FALSE:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002702 istk->conds->state = COND_ELSE_TRUE;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002703 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002704
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002705 case COND_ELSE_TRUE:
2706 case COND_ELSE_FALSE:
2707 error_precond(ERR_WARNING|ERR_PASS1,
2708 "`%%else' after `%%else' ignored.");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002709 istk->conds->state = COND_NEVER;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002710 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002711 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002712 free_tlist(origline);
2713 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002714
H. Peter Anvine2c80182005-01-15 22:15:51 +00002715 case PP_ENDIF:
2716 if (tline->next)
H. Peter Anvin917a3492008-09-24 09:14:49 -07002717 error_precond(ERR_WARNING|ERR_PASS1,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002718 "trailing garbage after `%%endif' ignored");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002719 if (!istk->conds)
2720 error(ERR_FATAL, "`%%endif': no matching `%%if'");
2721 cond = istk->conds;
2722 istk->conds = cond->next;
2723 nasm_free(cond);
2724 if(istk->mstk)
2725 istk->mstk->condcnt --;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002726 free_tlist(origline);
2727 return DIRECTIVE_FOUND;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002728
H. Peter Anvindb8f96e2009-07-15 09:07:29 -04002729 case PP_RMACRO:
2730 case PP_IRMACRO:
H. Peter Anvine2c80182005-01-15 22:15:51 +00002731 case PP_MACRO:
2732 case PP_IMACRO:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002733 if (defining) {
2734 error(ERR_FATAL, "`%s': already defining a macro",
2735 pp_directives[i]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002736 return DIRECTIVE_FOUND;
2737 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002738 defining = nasm_malloc(sizeof(MMacro));
2739 defining->max_depth =
2740 (i == PP_RMACRO) || (i == PP_IRMACRO) ? DEADMAN_LIMIT : 0;
2741 defining->casesense = (i == PP_MACRO) || (i == PP_RMACRO);
2742 if (!parse_mmacro_spec(tline, defining, pp_directives[i])) {
2743 nasm_free(defining);
2744 defining = NULL;
2745 return DIRECTIVE_FOUND;
2746 }
H. Peter Anvina26433d2008-07-16 14:40:01 -07002747
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002748 mmac = (MMacro *) hash_findix(&mmacros, defining->name);
2749 while (mmac) {
2750 if (!strcmp(mmac->name, defining->name) &&
2751 (mmac->nparam_min <= defining->nparam_max
2752 || defining->plus)
2753 && (defining->nparam_min <= mmac->nparam_max
2754 || mmac->plus)) {
2755 error(ERR_WARNING|ERR_PASS1,
2756 "redefining multi-line macro `%s'", defining->name);
2757 return DIRECTIVE_FOUND;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002758 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002759 mmac = mmac->next;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002760 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002761 free_tlist(origline);
2762 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002763
H. Peter Anvine2c80182005-01-15 22:15:51 +00002764 case PP_ENDM:
2765 case PP_ENDMACRO:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002766 if (! (defining && defining->name)) {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002767 error(ERR_NONFATAL, "`%s': not defining a macro", tline->text);
2768 return DIRECTIVE_FOUND;
2769 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002770 mmhead = (MMacro **) hash_findi_add(&mmacros, defining->name);
2771 defining->next = *mmhead;
2772 *mmhead = defining;
2773 defining = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002774 free_tlist(origline);
2775 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002776
H. Peter Anvin89cee572009-07-15 09:16:54 -04002777 case PP_EXITMACRO:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002778 /*
2779 * We must search along istk->expansion until we hit a
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002780 * macro-end marker for a macro with a name. Then we
2781 * bypass all lines between exitmacro and endmacro.
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002782 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002783 list_for_each(l, istk->expansion)
2784 if (l->finishes && l->finishes->name)
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002785 break;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002786
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002787 if (l) {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002788 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002789 * Remove all conditional entries relative to this
2790 * macro invocation. (safe to do in this context)
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002791 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002792 for ( ; l->finishes->condcnt > 0; l->finishes->condcnt --) {
2793 cond = istk->conds;
2794 istk->conds = cond->next;
2795 nasm_free(cond);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002796 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002797 istk->expansion = l;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002798 } else {
2799 error(ERR_NONFATAL, "`%%exitmacro' not within `%%macro' block");
2800 }
Keith Kanios852f1ee2009-07-12 00:19:55 -05002801 free_tlist(origline);
2802 return DIRECTIVE_FOUND;
2803
H. Peter Anvina26433d2008-07-16 14:40:01 -07002804 case PP_UNMACRO:
2805 case PP_UNIMACRO:
2806 {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002807 MMacro **mmac_p;
2808 MMacro spec;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002809
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002810 spec.casesense = (i == PP_UNMACRO);
2811 if (!parse_mmacro_spec(tline, &spec, pp_directives[i])) {
2812 return DIRECTIVE_FOUND;
2813 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002814 mmac_p = (MMacro **) hash_findi(&mmacros, spec.name, NULL);
2815 while (mmac_p && *mmac_p) {
2816 mmac = *mmac_p;
2817 if (mmac->casesense == spec.casesense &&
2818 !mstrcmp(mmac->name, spec.name, spec.casesense) &&
2819 mmac->nparam_min == spec.nparam_min &&
2820 mmac->nparam_max == spec.nparam_max &&
2821 mmac->plus == spec.plus) {
2822 *mmac_p = mmac->next;
2823 free_mmacro(mmac);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002824 } else {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002825 mmac_p = &mmac->next;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002826 }
2827 }
2828 free_tlist(origline);
2829 free_tlist(spec.dlist);
2830 return DIRECTIVE_FOUND;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002831 }
2832
H. Peter Anvine2c80182005-01-15 22:15:51 +00002833 case PP_ROTATE:
2834 if (tline->next && tline->next->type == TOK_WHITESPACE)
2835 tline = tline->next;
H. Peter Anvin89cee572009-07-15 09:16:54 -04002836 if (!tline->next) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002837 free_tlist(origline);
2838 error(ERR_NONFATAL, "`%%rotate' missing rotate count");
2839 return DIRECTIVE_FOUND;
2840 }
2841 t = expand_smacro(tline->next);
2842 tline->next = NULL;
2843 free_tlist(origline);
2844 tline = t;
2845 tptr = &t;
2846 tokval.t_type = TOKEN_INVALID;
2847 evalresult =
2848 evaluate(ppscan, tptr, &tokval, NULL, pass, error, NULL);
2849 free_tlist(tline);
2850 if (!evalresult)
2851 return DIRECTIVE_FOUND;
2852 if (tokval.t_type)
H. Peter Anvin917a3492008-09-24 09:14:49 -07002853 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +00002854 "trailing garbage after expression ignored");
2855 if (!is_simple(evalresult)) {
2856 error(ERR_NONFATAL, "non-constant value given to `%%rotate'");
2857 return DIRECTIVE_FOUND;
2858 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002859 mmac = istk->mstk;
2860 while (mmac && !mmac->name) /* avoid mistaking %reps for macros */
2861 mmac = mmac->next_active;
2862 if (!mmac) {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002863 error(ERR_NONFATAL, "`%%rotate' invoked outside a macro call");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002864 } else if (mmac->nparam == 0) {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002865 error(ERR_NONFATAL,
2866 "`%%rotate' invoked within macro without parameters");
2867 } else {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002868 int rotate = mmac->rotate + reloc_value(evalresult);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002869
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002870 rotate %= (int)mmac->nparam;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002871 if (rotate < 0)
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002872 rotate += mmac->nparam;
2873
2874 mmac->rotate = rotate;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002875 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002876 return DIRECTIVE_FOUND;
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00002877
H. Peter Anvine2c80182005-01-15 22:15:51 +00002878 case PP_REP:
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002879 nolist = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002880 do {
2881 tline = tline->next;
2882 } while (tok_type_(tline, TOK_WHITESPACE));
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00002883
H. Peter Anvine2c80182005-01-15 22:15:51 +00002884 if (tok_type_(tline, TOK_ID) &&
2885 nasm_stricmp(tline->text, ".nolist") == 0) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002886 nolist = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002887 do {
2888 tline = tline->next;
2889 } while (tok_type_(tline, TOK_WHITESPACE));
2890 }
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00002891
H. Peter Anvine2c80182005-01-15 22:15:51 +00002892 if (tline) {
2893 t = expand_smacro(tline);
2894 tptr = &t;
2895 tokval.t_type = TOKEN_INVALID;
2896 evalresult =
2897 evaluate(ppscan, tptr, &tokval, NULL, pass, error, NULL);
2898 if (!evalresult) {
2899 free_tlist(origline);
2900 return DIRECTIVE_FOUND;
2901 }
2902 if (tokval.t_type)
H. Peter Anvin917a3492008-09-24 09:14:49 -07002903 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +00002904 "trailing garbage after expression ignored");
2905 if (!is_simple(evalresult)) {
2906 error(ERR_NONFATAL, "non-constant value given to `%%rep'");
2907 return DIRECTIVE_FOUND;
2908 }
Cyrill Gorcunove091d6e2010-08-09 13:58:22 +04002909 count = reloc_value(evalresult);
2910 if (count >= REP_LIMIT) {
Cyrill Gorcunov71f4f842010-08-09 20:17:17 +04002911 error(ERR_NONFATAL, "`%%rep' value exceeds limit");
Cyrill Gorcunove091d6e2010-08-09 13:58:22 +04002912 count = 0;
2913 } else
2914 count++;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002915 } else {
2916 error(ERR_NONFATAL, "`%%rep' expects a repeat count");
H. Peter Anvinf8ba53e2007-10-11 10:11:57 -07002917 count = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002918 }
2919 free_tlist(origline);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002920
2921 tmp_defining = defining;
2922 defining = nasm_malloc(sizeof(MMacro));
2923 defining->prev = NULL;
2924 defining->name = NULL; /* flags this macro as a %rep block */
2925 defining->casesense = false;
2926 defining->plus = false;
2927 defining->nolist = nolist;
2928 defining->in_progress = count;
2929 defining->max_depth = 0;
2930 defining->nparam_min = defining->nparam_max = 0;
2931 defining->defaults = NULL;
2932 defining->dlist = NULL;
2933 defining->expansion = NULL;
2934 defining->next_active = istk->mstk;
2935 defining->rep_nest = tmp_defining;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002936 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002937
H. Peter Anvine2c80182005-01-15 22:15:51 +00002938 case PP_ENDREP:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002939 if (!defining || defining->name) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002940 error(ERR_NONFATAL, "`%%endrep': no matching `%%rep'");
2941 return DIRECTIVE_FOUND;
2942 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00002943
H. Peter Anvine2c80182005-01-15 22:15:51 +00002944 /*
2945 * Now we have a "macro" defined - although it has no name
2946 * and we won't be entering it in the hash tables - we must
2947 * push a macro-end marker for it on to istk->expansion.
2948 * After that, it will take care of propagating itself (a
2949 * macro-end marker line for a macro which is really a %rep
2950 * block will cause the macro to be re-expanded, complete
2951 * with another macro-end marker to ensure the process
2952 * continues) until the whole expansion is forcibly removed
2953 * from istk->expansion by a %exitrep.
2954 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002955 l = nasm_malloc(sizeof(Line));
2956 l->next = istk->expansion;
2957 l->finishes = defining;
2958 l->first = NULL;
2959 istk->expansion = l;
2960
2961 istk->mstk = defining;
2962
2963 list->uplevel(defining->nolist ? LIST_MACRO_NOLIST : LIST_MACRO);
2964 tmp_defining = defining;
2965 defining = defining->rep_nest;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002966 free_tlist(origline);
2967 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002968
H. Peter Anvine2c80182005-01-15 22:15:51 +00002969 case PP_EXITREP:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002970 /*
2971 * We must search along istk->expansion until we hit a
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002972 * macro-end marker for a macro with no name. Then we set
2973 * its `in_progress' flag to 0.
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002974 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002975 list_for_each(l, istk->expansion)
2976 if (l->finishes && !l->finishes->name)
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002977 break;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002978
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002979 if (l)
2980 l->finishes->in_progress = 1;
2981 else
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002982 error(ERR_NONFATAL, "`%%exitrep' not within `%%rep' block");
H. Peter Anvine2c80182005-01-15 22:15:51 +00002983 free_tlist(origline);
2984 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002985
H. Peter Anvine2c80182005-01-15 22:15:51 +00002986 case PP_XDEFINE:
2987 case PP_IXDEFINE:
2988 case PP_DEFINE:
2989 case PP_IDEFINE:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002990 casesense = (i == PP_DEFINE || i == PP_XDEFINE);
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07002991
H. Peter Anvine2c80182005-01-15 22:15:51 +00002992 tline = tline->next;
2993 skip_white_(tline);
2994 tline = expand_id(tline);
2995 if (!tline || (tline->type != TOK_ID &&
2996 (tline->type != TOK_PREPROC_ID ||
2997 tline->text[1] != '$'))) {
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07002998 error(ERR_NONFATAL, "`%s' expects a macro identifier",
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002999 pp_directives[i]);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003000 free_tlist(origline);
3001 return DIRECTIVE_FOUND;
3002 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003003
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04003004 ctx = get_ctx(tline->text, &mname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003005 last = tline;
3006 param_start = tline = tline->next;
3007 nparam = 0;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003008
H. Peter Anvine2c80182005-01-15 22:15:51 +00003009 /* Expand the macro definition now for %xdefine and %ixdefine */
3010 if ((i == PP_XDEFINE) || (i == PP_IXDEFINE))
3011 tline = expand_smacro(tline);
H. Peter Anvin734b1882002-04-30 21:01:08 +00003012
H. Peter Anvine2c80182005-01-15 22:15:51 +00003013 if (tok_is_(tline, "(")) {
3014 /*
3015 * This macro has parameters.
3016 */
H. Peter Anvin734b1882002-04-30 21:01:08 +00003017
H. Peter Anvine2c80182005-01-15 22:15:51 +00003018 tline = tline->next;
3019 while (1) {
3020 skip_white_(tline);
3021 if (!tline) {
3022 error(ERR_NONFATAL, "parameter identifier expected");
3023 free_tlist(origline);
3024 return DIRECTIVE_FOUND;
3025 }
3026 if (tline->type != TOK_ID) {
3027 error(ERR_NONFATAL,
3028 "`%s': parameter identifier expected",
3029 tline->text);
3030 free_tlist(origline);
3031 return DIRECTIVE_FOUND;
3032 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003033 tline->type = TOK_SMAC_PARAM + nparam++;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003034 tline = tline->next;
3035 skip_white_(tline);
3036 if (tok_is_(tline, ",")) {
3037 tline = tline->next;
H. Peter Anvinca348b62008-07-23 10:49:26 -04003038 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003039 if (!tok_is_(tline, ")")) {
3040 error(ERR_NONFATAL,
3041 "`)' expected to terminate macro template");
3042 free_tlist(origline);
3043 return DIRECTIVE_FOUND;
3044 }
3045 break;
3046 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00003047 }
3048 last = tline;
3049 tline = tline->next;
3050 }
3051 if (tok_type_(tline, TOK_WHITESPACE))
3052 last = tline, tline = tline->next;
3053 macro_start = NULL;
3054 last->next = NULL;
3055 t = tline;
3056 while (t) {
3057 if (t->type == TOK_ID) {
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04003058 list_for_each(tt, param_start)
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003059 if (tt->type >= TOK_SMAC_PARAM &&
H. Peter Anvine2c80182005-01-15 22:15:51 +00003060 !strcmp(tt->text, t->text))
3061 t->type = tt->type;
3062 }
3063 tt = t->next;
3064 t->next = macro_start;
3065 macro_start = t;
3066 t = tt;
3067 }
3068 /*
3069 * Good. We now have a macro name, a parameter count, and a
3070 * token list (in reverse order) for an expansion. We ought
3071 * to be OK just to create an SMacro, store it, and let
3072 * free_tlist have the rest of the line (which we have
3073 * carefully re-terminated after chopping off the expansion
3074 * from the end).
3075 */
H. Peter Anvin4db5a162007-10-11 13:42:09 -07003076 define_smacro(ctx, mname, casesense, nparam, macro_start);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003077 free_tlist(origline);
3078 return DIRECTIVE_FOUND;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003079
H. Peter Anvine2c80182005-01-15 22:15:51 +00003080 case PP_UNDEF:
3081 tline = tline->next;
3082 skip_white_(tline);
3083 tline = expand_id(tline);
3084 if (!tline || (tline->type != TOK_ID &&
3085 (tline->type != TOK_PREPROC_ID ||
3086 tline->text[1] != '$'))) {
3087 error(ERR_NONFATAL, "`%%undef' expects a macro identifier");
3088 free_tlist(origline);
3089 return DIRECTIVE_FOUND;
3090 }
3091 if (tline->next) {
H. Peter Anvin917a3492008-09-24 09:14:49 -07003092 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +00003093 "trailing garbage after macro name ignored");
3094 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003095
H. Peter Anvine2c80182005-01-15 22:15:51 +00003096 /* Find the context that symbol belongs to */
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04003097 ctx = get_ctx(tline->text, &mname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003098 undef_smacro(ctx, mname);
3099 free_tlist(origline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003100 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003101
H. Peter Anvin9e200162008-06-04 17:23:14 -07003102 case PP_DEFSTR:
3103 case PP_IDEFSTR:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003104 casesense = (i == PP_DEFSTR);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003105
3106 tline = tline->next;
3107 skip_white_(tline);
3108 tline = expand_id(tline);
3109 if (!tline || (tline->type != TOK_ID &&
3110 (tline->type != TOK_PREPROC_ID ||
3111 tline->text[1] != '$'))) {
3112 error(ERR_NONFATAL, "`%s' expects a macro identifier",
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003113 pp_directives[i]);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003114 free_tlist(origline);
3115 return DIRECTIVE_FOUND;
3116 }
3117
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04003118 ctx = get_ctx(tline->text, &mname);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003119 last = tline;
3120 tline = expand_smacro(tline->next);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003121 last->next = NULL;
H. Peter Anvin9e200162008-06-04 17:23:14 -07003122
3123 while (tok_type_(tline, TOK_WHITESPACE))
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003124 tline = delete_Token(tline);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003125
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003126 p = detoken(tline, false);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003127 macro_start = nasm_malloc(sizeof(*macro_start));
3128 macro_start->next = NULL;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003129 macro_start->text = nasm_quote(p, strlen(p));
3130 macro_start->type = TOK_STRING;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003131 macro_start->a.mac = NULL;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003132 nasm_free(p);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003133
3134 /*
3135 * We now have a macro name, an implicit parameter count of
3136 * zero, and a string token to use as an expansion. Create
3137 * and store an SMacro.
3138 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003139 define_smacro(ctx, mname, casesense, 0, macro_start);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003140 free_tlist(origline);
3141 return DIRECTIVE_FOUND;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003142
H. Peter Anvin2f55bda2009-07-14 15:04:04 -04003143 case PP_DEFTOK:
3144 case PP_IDEFTOK:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003145 casesense = (i == PP_DEFTOK);
3146
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003147 tline = tline->next;
3148 skip_white_(tline);
3149 tline = expand_id(tline);
3150 if (!tline || (tline->type != TOK_ID &&
3151 (tline->type != TOK_PREPROC_ID ||
3152 tline->text[1] != '$'))) {
3153 error(ERR_NONFATAL,
3154 "`%s' expects a macro identifier as first parameter",
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003155 pp_directives[i]);
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003156 free_tlist(origline);
3157 return DIRECTIVE_FOUND;
3158 }
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04003159 ctx = get_ctx(tline->text, &mname);
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003160 last = tline;
3161 tline = expand_smacro(tline->next);
3162 last->next = NULL;
3163
3164 t = tline;
3165 while (tok_type_(t, TOK_WHITESPACE))
3166 t = t->next;
3167 /* t should now point to the string */
Cyrill Gorcunov6908e582010-09-06 19:36:15 +04003168 if (!tok_type_(t, TOK_STRING)) {
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003169 error(ERR_NONFATAL,
3170 "`%s` requires string as second parameter",
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003171 pp_directives[i]);
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003172 free_tlist(tline);
3173 free_tlist(origline);
3174 return DIRECTIVE_FOUND;
3175 }
3176
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04003177 /*
3178 * Convert the string to a token stream. Note that smacros
3179 * are stored with the token stream reversed, so we have to
3180 * reverse the output of tokenize().
3181 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003182 nasm_unquote_cstr(t->text, i);
H. Peter Anvinb40992c2010-09-15 08:57:21 -07003183 macro_start = reverse_tokens(tokenize(t->text));
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003184
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003185 /*
3186 * We now have a macro name, an implicit parameter count of
3187 * zero, and a numeric token to use as an expansion. Create
3188 * and store an SMacro.
3189 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003190 define_smacro(ctx, mname, casesense, 0, macro_start);
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003191 free_tlist(tline);
3192 free_tlist(origline);
3193 return DIRECTIVE_FOUND;
H. Peter Anvin9e200162008-06-04 17:23:14 -07003194
H. Peter Anvin418ca702008-05-30 10:42:30 -07003195 case PP_PATHSEARCH:
3196 {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003197 FILE *fp;
3198 StrList *xsl = NULL;
3199 StrList **xst = &xsl;
H. Peter Anvin418ca702008-05-30 10:42:30 -07003200
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003201 casesense = true;
H. Peter Anvin418ca702008-05-30 10:42:30 -07003202
3203 tline = tline->next;
3204 skip_white_(tline);
3205 tline = expand_id(tline);
3206 if (!tline || (tline->type != TOK_ID &&
3207 (tline->type != TOK_PREPROC_ID ||
3208 tline->text[1] != '$'))) {
3209 error(ERR_NONFATAL,
3210 "`%%pathsearch' expects a macro identifier as first parameter");
3211 free_tlist(origline);
3212 return DIRECTIVE_FOUND;
3213 }
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04003214 ctx = get_ctx(tline->text, &mname);
H. Peter Anvin418ca702008-05-30 10:42:30 -07003215 last = tline;
3216 tline = expand_smacro(tline->next);
3217 last->next = NULL;
3218
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003219 t = tline;
H. Peter Anvin418ca702008-05-30 10:42:30 -07003220 while (tok_type_(t, TOK_WHITESPACE))
3221 t = t->next;
3222
3223 if (!t || (t->type != TOK_STRING &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003224 t->type != TOK_INTERNAL_STRING)) {
H. Peter Anvin418ca702008-05-30 10:42:30 -07003225 error(ERR_NONFATAL, "`%%pathsearch' expects a file name");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003226 free_tlist(tline);
H. Peter Anvin418ca702008-05-30 10:42:30 -07003227 free_tlist(origline);
3228 return DIRECTIVE_FOUND; /* but we did _something_ */
3229 }
3230 if (t->next)
H. Peter Anvin917a3492008-09-24 09:14:49 -07003231 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvin418ca702008-05-30 10:42:30 -07003232 "trailing garbage after `%%pathsearch' ignored");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003233 p = t->text;
H. Peter Anvin427cc912008-06-01 21:43:03 -07003234 if (t->type != TOK_INTERNAL_STRING)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003235 nasm_unquote(p, NULL);
H. Peter Anvin418ca702008-05-30 10:42:30 -07003236
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003237 fp = inc_fopen(p, &xsl, &xst, true);
3238 if (fp) {
3239 p = xsl->str;
3240 fclose(fp); /* Don't actually care about the file */
3241 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003242 macro_start = nasm_malloc(sizeof(*macro_start));
3243 macro_start->next = NULL;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003244 macro_start->text = nasm_quote(p, strlen(p));
3245 macro_start->type = TOK_STRING;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003246 macro_start->a.mac = NULL;
3247 if (xsl)
3248 nasm_free(xsl);
H. Peter Anvin418ca702008-05-30 10:42:30 -07003249
3250 /*
3251 * We now have a macro name, an implicit parameter count of
3252 * zero, and a string token to use as an expansion. Create
3253 * and store an SMacro.
3254 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003255 define_smacro(ctx, mname, casesense, 0, macro_start);
3256 free_tlist(tline);
H. Peter Anvin418ca702008-05-30 10:42:30 -07003257 free_tlist(origline);
3258 return DIRECTIVE_FOUND;
3259 }
3260
H. Peter Anvine2c80182005-01-15 22:15:51 +00003261 case PP_STRLEN:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003262 casesense = true;
H. Peter Anvin70653092007-10-19 14:42:29 -07003263
H. Peter Anvine2c80182005-01-15 22:15:51 +00003264 tline = tline->next;
3265 skip_white_(tline);
3266 tline = expand_id(tline);
3267 if (!tline || (tline->type != TOK_ID &&
3268 (tline->type != TOK_PREPROC_ID ||
3269 tline->text[1] != '$'))) {
3270 error(ERR_NONFATAL,
3271 "`%%strlen' expects a macro identifier as first parameter");
3272 free_tlist(origline);
3273 return DIRECTIVE_FOUND;
3274 }
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04003275 ctx = get_ctx(tline->text, &mname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003276 last = tline;
3277 tline = expand_smacro(tline->next);
3278 last->next = NULL;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003279
H. Peter Anvine2c80182005-01-15 22:15:51 +00003280 t = tline;
3281 while (tok_type_(t, TOK_WHITESPACE))
3282 t = t->next;
3283 /* t should now point to the string */
Cyrill Gorcunov4e1d5ab2010-07-23 18:51:51 +04003284 if (!tok_type_(t, TOK_STRING)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00003285 error(ERR_NONFATAL,
3286 "`%%strlen` requires string as second parameter");
3287 free_tlist(tline);
3288 free_tlist(origline);
3289 return DIRECTIVE_FOUND;
3290 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003291
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003292 macro_start = nasm_malloc(sizeof(*macro_start));
3293 macro_start->next = NULL;
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07003294 make_tok_num(macro_start, nasm_unquote(t->text, NULL));
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003295 macro_start->a.mac = NULL;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003296
H. Peter Anvine2c80182005-01-15 22:15:51 +00003297 /*
3298 * We now have a macro name, an implicit parameter count of
3299 * zero, and a numeric token to use as an expansion. Create
3300 * and store an SMacro.
3301 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003302 define_smacro(ctx, mname, casesense, 0, macro_start);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003303 free_tlist(tline);
3304 free_tlist(origline);
3305 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003306
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003307 case PP_STRCAT:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003308 casesense = true;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003309
3310 tline = tline->next;
3311 skip_white_(tline);
3312 tline = expand_id(tline);
3313 if (!tline || (tline->type != TOK_ID &&
3314 (tline->type != TOK_PREPROC_ID ||
3315 tline->text[1] != '$'))) {
3316 error(ERR_NONFATAL,
3317 "`%%strcat' expects a macro identifier as first parameter");
3318 free_tlist(origline);
3319 return DIRECTIVE_FOUND;
3320 }
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04003321 ctx = get_ctx(tline->text, &mname);
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003322 last = tline;
3323 tline = expand_smacro(tline->next);
3324 last->next = NULL;
3325
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003326 len = 0;
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04003327 list_for_each(t, tline) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003328 switch (t->type) {
3329 case TOK_WHITESPACE:
3330 break;
3331 case TOK_STRING:
3332 len += t->a.len = nasm_unquote(t->text, NULL);
3333 break;
3334 case TOK_OTHER:
3335 if (!strcmp(t->text, ",")) /* permit comma separators */
3336 break;
3337 /* else fall through */
3338 default:
3339 error(ERR_NONFATAL,
3340 "non-string passed to `%%strcat' (%d)", t->type);
3341 free_tlist(tline);
3342 free_tlist(origline);
3343 return DIRECTIVE_FOUND;
3344 }
3345 }
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003346
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003347 p = pp = nasm_malloc(len);
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04003348 list_for_each(t, tline) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003349 if (t->type == TOK_STRING) {
3350 memcpy(p, t->text, t->a.len);
3351 p += t->a.len;
3352 }
3353 }
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003354
3355 /*
3356 * We now have a macro name, an implicit parameter count of
3357 * zero, and a numeric token to use as an expansion. Create
3358 * and store an SMacro.
3359 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003360 macro_start = new_Token(NULL, TOK_STRING, NULL, 0);
3361 macro_start->text = nasm_quote(pp, len);
3362 nasm_free(pp);
3363 define_smacro(ctx, mname, casesense, 0, macro_start);
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003364 free_tlist(tline);
3365 free_tlist(origline);
3366 return DIRECTIVE_FOUND;
3367
H. Peter Anvine2c80182005-01-15 22:15:51 +00003368 case PP_SUBSTR:
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003369 {
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003370 int64_t start, count;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003371 size_t len;
H. Peter Anvind2456592008-06-19 15:04:18 -07003372
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003373 casesense = true;
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07003374
H. Peter Anvine2c80182005-01-15 22:15:51 +00003375 tline = tline->next;
3376 skip_white_(tline);
3377 tline = expand_id(tline);
3378 if (!tline || (tline->type != TOK_ID &&
3379 (tline->type != TOK_PREPROC_ID ||
3380 tline->text[1] != '$'))) {
3381 error(ERR_NONFATAL,
3382 "`%%substr' expects a macro identifier as first parameter");
3383 free_tlist(origline);
3384 return DIRECTIVE_FOUND;
3385 }
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04003386 ctx = get_ctx(tline->text, &mname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003387 last = tline;
3388 tline = expand_smacro(tline->next);
3389 last->next = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003390
Cyrill Gorcunov35519d62010-09-06 23:49:52 +04003391 if (tline) /* skip expanded id */
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003392 t = tline->next;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003393 while (tok_type_(t, TOK_WHITESPACE))
3394 t = t->next;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003395
H. Peter Anvine2c80182005-01-15 22:15:51 +00003396 /* t should now point to the string */
Cyrill Gorcunov35519d62010-09-06 23:49:52 +04003397 if (!tok_type_(t, TOK_STRING)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00003398 error(ERR_NONFATAL,
3399 "`%%substr` requires string as second parameter");
3400 free_tlist(tline);
3401 free_tlist(origline);
3402 return DIRECTIVE_FOUND;
3403 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003404
H. Peter Anvine2c80182005-01-15 22:15:51 +00003405 tt = t->next;
3406 tptr = &tt;
3407 tokval.t_type = TOKEN_INVALID;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003408 evalresult = evaluate(ppscan, tptr, &tokval, NULL,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003409 pass, error, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003410 if (!evalresult) {
3411 free_tlist(tline);
3412 free_tlist(origline);
3413 return DIRECTIVE_FOUND;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003414 } else if (!is_simple(evalresult)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00003415 error(ERR_NONFATAL, "non-constant value given to `%%substr`");
3416 free_tlist(tline);
3417 free_tlist(origline);
3418 return DIRECTIVE_FOUND;
3419 }
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003420 start = evalresult->value - 1;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003421
3422 while (tok_type_(tt, TOK_WHITESPACE))
3423 tt = tt->next;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003424 if (!tt) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003425 count = 1; /* Backwards compatibility: one character */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003426 } else {
3427 tokval.t_type = TOKEN_INVALID;
3428 evalresult = evaluate(ppscan, tptr, &tokval, NULL,
3429 pass, error, NULL);
3430 if (!evalresult) {
3431 free_tlist(tline);
3432 free_tlist(origline);
3433 return DIRECTIVE_FOUND;
3434 } else if (!is_simple(evalresult)) {
3435 error(ERR_NONFATAL, "non-constant value given to `%%substr`");
3436 free_tlist(tline);
3437 free_tlist(origline);
3438 return DIRECTIVE_FOUND;
3439 }
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003440 count = evalresult->value;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003441 }
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003442
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003443 len = nasm_unquote(t->text, NULL);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003444
Cyrill Gorcunovcff031e2010-09-07 20:31:11 +04003445 /* make start and count being in range */
3446 if (start < 0)
3447 start = 0;
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003448 if (count < 0)
3449 count = len + count + 1 - start;
3450 if (start + count > (int64_t)len)
Cyrill Gorcunovcff031e2010-09-07 20:31:11 +04003451 count = len - start;
3452 if (!len || count < 0 || start >=(int64_t)len)
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003453 start = -1, count = 0; /* empty string */
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003454
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003455 macro_start = nasm_malloc(sizeof(*macro_start));
3456 macro_start->next = NULL;
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003457 macro_start->text = nasm_quote((start < 0) ? "" : t->text + start, count);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003458 macro_start->type = TOK_STRING;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003459 macro_start->a.mac = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003460
H. Peter Anvine2c80182005-01-15 22:15:51 +00003461 /*
3462 * We now have a macro name, an implicit parameter count of
3463 * zero, and a numeric token to use as an expansion. Create
3464 * and store an SMacro.
3465 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003466 define_smacro(ctx, mname, casesense, 0, macro_start);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003467 free_tlist(tline);
3468 free_tlist(origline);
3469 return DIRECTIVE_FOUND;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003470 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003471
H. Peter Anvine2c80182005-01-15 22:15:51 +00003472 case PP_ASSIGN:
3473 case PP_IASSIGN:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003474 casesense = (i == PP_ASSIGN);
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07003475
H. Peter Anvine2c80182005-01-15 22:15:51 +00003476 tline = tline->next;
3477 skip_white_(tline);
3478 tline = expand_id(tline);
3479 if (!tline || (tline->type != TOK_ID &&
3480 (tline->type != TOK_PREPROC_ID ||
3481 tline->text[1] != '$'))) {
3482 error(ERR_NONFATAL,
3483 "`%%%sassign' expects a macro identifier",
3484 (i == PP_IASSIGN ? "i" : ""));
3485 free_tlist(origline);
3486 return DIRECTIVE_FOUND;
3487 }
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04003488 ctx = get_ctx(tline->text, &mname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003489 last = tline;
3490 tline = expand_smacro(tline->next);
3491 last->next = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003492
H. Peter Anvine2c80182005-01-15 22:15:51 +00003493 t = tline;
3494 tptr = &t;
3495 tokval.t_type = TOKEN_INVALID;
3496 evalresult =
3497 evaluate(ppscan, tptr, &tokval, NULL, pass, error, NULL);
3498 free_tlist(tline);
3499 if (!evalresult) {
3500 free_tlist(origline);
3501 return DIRECTIVE_FOUND;
3502 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003503
H. Peter Anvine2c80182005-01-15 22:15:51 +00003504 if (tokval.t_type)
H. Peter Anvin917a3492008-09-24 09:14:49 -07003505 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +00003506 "trailing garbage after expression ignored");
H. Peter Anvin734b1882002-04-30 21:01:08 +00003507
H. Peter Anvine2c80182005-01-15 22:15:51 +00003508 if (!is_simple(evalresult)) {
3509 error(ERR_NONFATAL,
3510 "non-constant value given to `%%%sassign'",
3511 (i == PP_IASSIGN ? "i" : ""));
3512 free_tlist(origline);
3513 return DIRECTIVE_FOUND;
3514 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003515
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003516 macro_start = nasm_malloc(sizeof(*macro_start));
3517 macro_start->next = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003518 make_tok_num(macro_start, reloc_value(evalresult));
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003519 macro_start->a.mac = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003520
H. Peter Anvine2c80182005-01-15 22:15:51 +00003521 /*
3522 * We now have a macro name, an implicit parameter count of
3523 * zero, and a numeric token to use as an expansion. Create
3524 * and store an SMacro.
3525 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003526 define_smacro(ctx, mname, casesense, 0, macro_start);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003527 free_tlist(origline);
3528 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003529
H. Peter Anvine2c80182005-01-15 22:15:51 +00003530 case PP_LINE:
3531 /*
3532 * Syntax is `%line nnn[+mmm] [filename]'
3533 */
3534 tline = tline->next;
3535 skip_white_(tline);
3536 if (!tok_type_(tline, TOK_NUMBER)) {
3537 error(ERR_NONFATAL, "`%%line' expects line number");
3538 free_tlist(origline);
3539 return DIRECTIVE_FOUND;
3540 }
H. Peter Anvin70055962007-10-11 00:05:31 -07003541 k = readnum(tline->text, &err);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003542 m = 1;
3543 tline = tline->next;
3544 if (tok_is_(tline, "+")) {
3545 tline = tline->next;
3546 if (!tok_type_(tline, TOK_NUMBER)) {
3547 error(ERR_NONFATAL, "`%%line' expects line increment");
3548 free_tlist(origline);
3549 return DIRECTIVE_FOUND;
3550 }
H. Peter Anvin70055962007-10-11 00:05:31 -07003551 m = readnum(tline->text, &err);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003552 tline = tline->next;
3553 }
3554 skip_white_(tline);
3555 src_set_linnum(k);
3556 istk->lineinc = m;
3557 if (tline) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07003558 nasm_free(src_set_fname(detoken(tline, false)));
H. Peter Anvine2c80182005-01-15 22:15:51 +00003559 }
3560 free_tlist(origline);
3561 return DIRECTIVE_FOUND;
Keith Kaniosb307a4f2010-11-06 17:41:51 -05003562
H. Peter Anvine2c80182005-01-15 22:15:51 +00003563 default:
3564 error(ERR_FATAL,
3565 "preprocessor directive `%s' not yet implemented",
H. Peter Anvin4169a472007-09-12 01:29:43 +00003566 pp_directives[i]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003567 return DIRECTIVE_FOUND;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003568 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003569}
3570
3571/*
H. Peter Anvin76690a12002-04-30 20:52:49 +00003572 * Ensure that a macro parameter contains a condition code and
3573 * nothing else. Return the condition code index if so, or -1
3574 * otherwise.
3575 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003576static int find_cc(Token * t)
H. Peter Anvineba20a72002-04-30 20:53:55 +00003577{
H. Peter Anvin76690a12002-04-30 20:52:49 +00003578 Token *tt;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003579
H. Peter Anvin25a99342007-09-22 17:45:45 -07003580 if (!t)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003581 return -1; /* Probably a %+ without a space */
H. Peter Anvin25a99342007-09-22 17:45:45 -07003582
H. Peter Anvineba20a72002-04-30 20:53:55 +00003583 skip_white_(t);
H. Peter Anvin76690a12002-04-30 20:52:49 +00003584 if (t->type != TOK_ID)
H. Peter Anvine2c80182005-01-15 22:15:51 +00003585 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003586 tt = t->next;
H. Peter Anvineba20a72002-04-30 20:53:55 +00003587 skip_white_(tt);
H. Peter Anvin76690a12002-04-30 20:52:49 +00003588 if (tt && (tt->type != TOK_OTHER || strcmp(tt->text, ",")))
H. Peter Anvine2c80182005-01-15 22:15:51 +00003589 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003590
Cyrill Gorcunov19456392012-05-02 00:18:56 +04003591 return bsii(t->text, (const char **)conditions, ARRAY_SIZE(conditions));
H. Peter Anvin76690a12002-04-30 20:52:49 +00003592}
3593
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003594/*
3595 * This routines walks over tokens strem and hadnles tokens
3596 * pasting, if @handle_explicit passed then explicit pasting
3597 * term is handled, otherwise -- implicit pastings only.
3598 */
Cyrill Gorcunov575d4282010-10-06 00:25:55 +04003599static bool paste_tokens(Token **head, const struct tokseq_match *m,
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003600 size_t mnum, bool handle_explicit)
H. Peter Anvind784a082009-04-20 14:01:18 -07003601{
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003602 Token *tok, *next, **prev_next, **prev_nonspace;
3603 bool pasted = false;
3604 char *buf, *p;
3605 size_t len, i;
H. Peter Anvind784a082009-04-20 14:01:18 -07003606
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003607 /*
3608 * The last token before pasting. We need it
3609 * to be able to connect new handled tokens.
3610 * In other words if there were a tokens stream
3611 *
3612 * A -> B -> C -> D
3613 *
3614 * and we've joined tokens B and C, the resulting
3615 * stream should be
3616 *
3617 * A -> BC -> D
3618 */
3619 tok = *head;
3620 prev_next = NULL;
3621
3622 if (!tok_type_(tok, TOK_WHITESPACE) && !tok_type_(tok, TOK_PASTE))
3623 prev_nonspace = head;
3624 else
3625 prev_nonspace = NULL;
3626
3627 while (tok && (next = tok->next)) {
3628
3629 switch (tok->type) {
H. Peter Anvind784a082009-04-20 14:01:18 -07003630 case TOK_WHITESPACE:
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003631 /* Zap redundant whitespaces */
3632 while (tok_type_(next, TOK_WHITESPACE))
3633 next = delete_Token(next);
3634 tok->next = next;
H. Peter Anvind784a082009-04-20 14:01:18 -07003635 break;
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003636
3637 case TOK_PASTE:
3638 /* Explicit pasting */
3639 if (!handle_explicit)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003640 break;
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003641 next = delete_Token(tok);
3642
3643 while (tok_type_(next, TOK_WHITESPACE))
3644 next = delete_Token(next);
3645
3646 if (!pasted)
3647 pasted = true;
3648
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003649 /* Left pasting token is start of line */
3650 if (!prev_nonspace)
3651 error(ERR_FATAL, "No lvalue found on pasting");
3652
Cyrill Gorcunov8b5c9fb2013-02-04 01:24:54 +04003653 /*
3654 * No ending token, this might happen in two
3655 * cases
3656 *
3657 * 1) There indeed no right token at all
3658 * 2) There is a bare "%define ID" statement,
3659 * and @ID does expand to whitespace.
3660 *
3661 * So technically we need to do a grammar analysis
3662 * in another stage of parsing, but for now lets don't
3663 * change the behaviour people used to. Simply allow
3664 * whitespace after paste token.
3665 */
3666 if (!next) {
3667 /*
3668 * Zap ending space tokens and that's all.
3669 */
3670 tok = (*prev_nonspace)->next;
3671 while (tok_type_(tok, TOK_WHITESPACE))
3672 tok = delete_Token(tok);
3673 tok = *prev_nonspace;
3674 tok->next = NULL;
3675 break;
3676 }
3677
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003678 tok = *prev_nonspace;
3679 while (tok_type_(tok, TOK_WHITESPACE))
3680 tok = delete_Token(tok);
3681 len = strlen(tok->text);
3682 len += strlen(next->text);
3683
3684 p = buf = nasm_malloc(len + 1);
3685 strcpy(p, tok->text);
3686 p = strchr(p, '\0');
3687 strcpy(p, next->text);
3688
3689 delete_Token(tok);
3690
3691 tok = tokenize(buf);
3692 nasm_free(buf);
3693
3694 *prev_nonspace = tok;
3695 while (tok && tok->next)
3696 tok = tok->next;
3697
3698 tok->next = delete_Token(next);
3699
3700 /* Restart from pasted tokens head */
3701 tok = *prev_nonspace;
3702 break;
3703
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003704 default:
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003705 /* implicit pasting */
Cyrill Gorcunov575d4282010-10-06 00:25:55 +04003706 for (i = 0; i < mnum; i++) {
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003707 if (!(PP_CONCAT_MATCH(tok, m[i].mask_head)))
3708 continue;
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +04003709
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003710 len = 0;
3711 while (next && PP_CONCAT_MATCH(next, m[i].mask_tail)) {
3712 len += strlen(next->text);
3713 next = next->next;
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +04003714 }
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003715
3716 /* No match */
3717 if (tok == next)
3718 break;
3719
3720 len += strlen(tok->text);
3721 p = buf = nasm_malloc(len + 1);
3722
3723 while (tok != next) {
3724 strcpy(p, tok->text);
3725 p = strchr(p, '\0');
3726 tok = delete_Token(tok);
3727 }
3728
3729 tok = tokenize(buf);
3730 nasm_free(buf);
3731
3732 if (prev_next)
3733 *prev_next = tok;
3734 else
3735 *head = tok;
3736
3737 /*
3738 * Connect pasted into original stream,
3739 * ie A -> new-tokens -> B
3740 */
3741 while (tok && tok->next)
3742 tok = tok->next;
3743 tok->next = next;
3744
3745 if (!pasted)
3746 pasted = true;
3747
3748 /* Restart from pasted tokens head */
3749 tok = prev_next ? *prev_next : *head;
Cyrill Gorcunov575d4282010-10-06 00:25:55 +04003750 }
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003751
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003752 break;
H. Peter Anvind784a082009-04-20 14:01:18 -07003753 }
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003754
3755 prev_next = &tok->next;
3756
3757 if (tok->next &&
3758 !tok_type_(tok->next, TOK_WHITESPACE) &&
3759 !tok_type_(tok->next, TOK_PASTE))
3760 prev_nonspace = prev_next;
3761
3762 tok = tok->next;
H. Peter Anvind784a082009-04-20 14:01:18 -07003763 }
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003764
3765 return pasted;
H. Peter Anvind784a082009-04-20 14:01:18 -07003766}
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003767
3768/*
3769 * expands to a list of tokens from %{x:y}
3770 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003771static Token *expand_mmac_params_range(MMacro *mac, Token *tline, Token ***last)
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003772{
3773 Token *t = tline, **tt, *tm, *head;
3774 char *pos;
3775 int fst, lst, j, i;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003776
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003777 pos = strchr(tline->text, ':');
3778 nasm_assert(pos);
3779
3780 lst = atoi(pos + 1);
3781 fst = atoi(tline->text + 1);
3782
3783 /*
3784 * only macros params are accounted so
3785 * if someone passes %0 -- we reject such
3786 * value(s)
3787 */
3788 if (lst == 0 || fst == 0)
3789 goto err;
3790
3791 /* the values should be sane */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003792 if ((fst > (int)mac->nparam || fst < (-(int)mac->nparam)) ||
3793 (lst > (int)mac->nparam || lst < (-(int)mac->nparam)))
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003794 goto err;
3795
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003796 fst = fst < 0 ? fst + (int)mac->nparam + 1: fst;
3797 lst = lst < 0 ? lst + (int)mac->nparam + 1: lst;
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003798
3799 /* counted from zero */
3800 fst--, lst--;
3801
3802 /*
Cyrill Gorcunove75331c2013-11-09 12:02:15 +04003803 * It will be at least one token. Note we
3804 * need to scan params until separator, otherwise
3805 * only first token will be passed.
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003806 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003807 tm = mac->params[(fst + mac->rotate) % mac->nparam];
Cyrill Gorcunove75331c2013-11-09 12:02:15 +04003808 head = new_Token(NULL, tm->type, tm->text, 0);
3809 tt = &head->next, tm = tm->next;
3810 while (tok_isnt_(tm, ",")) {
3811 t = new_Token(NULL, tm->type, tm->text, 0);
3812 *tt = t, tt = &t->next, tm = tm->next;
3813 }
3814
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003815 if (fst < lst) {
3816 for (i = fst + 1; i <= lst; i++) {
3817 t = new_Token(NULL, TOK_OTHER, ",", 0);
3818 *tt = t, tt = &t->next;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003819 j = (i + mac->rotate) % mac->nparam;
3820 tm = mac->params[j];
Cyrill Gorcunove75331c2013-11-09 12:02:15 +04003821 while (tok_isnt_(tm, ",")) {
3822 t = new_Token(NULL, tm->type, tm->text, 0);
3823 *tt = t, tt = &t->next, tm = tm->next;
3824 }
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003825 }
3826 } else {
3827 for (i = fst - 1; i >= lst; i--) {
3828 t = new_Token(NULL, TOK_OTHER, ",", 0);
3829 *tt = t, tt = &t->next;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003830 j = (i + mac->rotate) % mac->nparam;
3831 tm = mac->params[j];
Cyrill Gorcunove75331c2013-11-09 12:02:15 +04003832 while (tok_isnt_(tm, ",")) {
3833 t = new_Token(NULL, tm->type, tm->text, 0);
3834 *tt = t, tt = &t->next, tm = tm->next;
3835 }
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003836 }
3837 }
3838
3839 *last = tt;
3840 return head;
3841
3842err:
3843 error(ERR_NONFATAL, "`%%{%s}': macro parameters out of range",
3844 &tline->text[1]);
3845 return tline;
3846}
3847
H. Peter Anvin76690a12002-04-30 20:52:49 +00003848/*
3849 * Expand MMacro-local things: parameter references (%0, %n, %+n,
H. Peter Anvin67c63722008-10-26 23:49:00 -07003850 * %-n) and MMacro-local identifiers (%%foo) as well as
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003851 * macro indirection (%[...]) and range (%{..:..}).
H. Peter Anvin76690a12002-04-30 20:52:49 +00003852 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003853static Token *expand_mmac_params(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00003854{
H. Peter Anvin734b1882002-04-30 21:01:08 +00003855 Token *t, *tt, **tail, *thead;
H. Peter Anvin6125b622009-04-08 14:02:25 -07003856 bool changed = false;
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003857 char *pos;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003858
3859 tail = &thead;
3860 thead = NULL;
3861
H. Peter Anvine2c80182005-01-15 22:15:51 +00003862 while (tline) {
3863 if (tline->type == TOK_PREPROC_ID &&
Cyrill Gorcunovca611192010-06-04 09:22:12 +04003864 (((tline->text[1] == '+' || tline->text[1] == '-') && tline->text[2]) ||
3865 (tline->text[1] >= '0' && tline->text[1] <= '9') ||
3866 tline->text[1] == '%')) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00003867 char *text = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003868 int type = 0, cc; /* type = 0 to placate optimisers */
Keith Kaniosa6dfa782007-04-13 16:47:53 +00003869 char tmpbuf[30];
H. Peter Anvin25a99342007-09-22 17:45:45 -07003870 unsigned int n;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003871 int i;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003872 MMacro *mac;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003873
H. Peter Anvine2c80182005-01-15 22:15:51 +00003874 t = tline;
3875 tline = tline->next;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003876
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003877 mac = istk->mstk;
3878 while (mac && !mac->name) /* avoid mistaking %reps for macros */
3879 mac = mac->next_active;
3880 if (!mac) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00003881 error(ERR_NONFATAL, "`%s': not in a macro call", t->text);
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003882 } else {
3883 pos = strchr(t->text, ':');
3884 if (!pos) {
3885 switch (t->text[1]) {
3886 /*
3887 * We have to make a substitution of one of the
3888 * forms %1, %-1, %+1, %%foo, %0.
3889 */
3890 case '0':
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003891 type = TOK_NUMBER;
3892 snprintf(tmpbuf, sizeof(tmpbuf), "%d", mac->nparam);
3893 text = nasm_strdup(tmpbuf);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003894 break;
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003895 case '%':
H. Peter Anvine2c80182005-01-15 22:15:51 +00003896 type = TOK_ID;
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003897 snprintf(tmpbuf, sizeof(tmpbuf), "..@%"PRIu64".",
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003898 mac->unique);
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003899 text = nasm_strcat(tmpbuf, t->text + 2);
3900 break;
3901 case '-':
3902 n = atoi(t->text + 2) - 1;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003903 if (n >= mac->nparam)
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003904 tt = NULL;
3905 else {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003906 if (mac->nparam > 1)
3907 n = (n + mac->rotate) % mac->nparam;
3908 tt = mac->params[n];
H. Peter Anvine2c80182005-01-15 22:15:51 +00003909 }
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003910 cc = find_cc(tt);
3911 if (cc == -1) {
3912 error(ERR_NONFATAL,
3913 "macro parameter %d is not a condition code",
3914 n + 1);
3915 text = NULL;
3916 } else {
3917 type = TOK_ID;
3918 if (inverse_ccs[cc] == -1) {
3919 error(ERR_NONFATAL,
3920 "condition code `%s' is not invertible",
3921 conditions[cc]);
3922 text = NULL;
3923 } else
3924 text = nasm_strdup(conditions[inverse_ccs[cc]]);
3925 }
3926 break;
3927 case '+':
3928 n = atoi(t->text + 2) - 1;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003929 if (n >= mac->nparam)
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003930 tt = NULL;
3931 else {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003932 if (mac->nparam > 1)
3933 n = (n + mac->rotate) % mac->nparam;
3934 tt = mac->params[n];
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003935 }
3936 cc = find_cc(tt);
3937 if (cc == -1) {
3938 error(ERR_NONFATAL,
3939 "macro parameter %d is not a condition code",
3940 n + 1);
3941 text = NULL;
3942 } else {
3943 type = TOK_ID;
3944 text = nasm_strdup(conditions[cc]);
3945 }
3946 break;
3947 default:
3948 n = atoi(t->text + 1) - 1;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003949 if (n >= mac->nparam)
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003950 tt = NULL;
3951 else {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003952 if (mac->nparam > 1)
3953 n = (n + mac->rotate) % mac->nparam;
3954 tt = mac->params[n];
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003955 }
3956 if (tt) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003957 for (i = 0; i < mac->paramlen[n]; i++) {
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003958 *tail = new_Token(NULL, tt->type, tt->text, 0);
3959 tail = &(*tail)->next;
3960 tt = tt->next;
3961 }
3962 }
3963 text = NULL; /* we've done it here */
3964 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003965 }
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003966 } else {
3967 /*
3968 * seems we have a parameters range here
3969 */
3970 Token *head, **last;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003971 head = expand_mmac_params_range(mac, t, &last);
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003972 if (head != t) {
3973 *tail = head;
3974 *last = tline;
3975 tline = head;
3976 text = NULL;
3977 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00003978 }
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003979 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00003980 if (!text) {
3981 delete_Token(t);
3982 } else {
3983 *tail = t;
3984 tail = &t->next;
3985 t->type = type;
3986 nasm_free(t->text);
3987 t->text = text;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003988 t->a.mac = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003989 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003990 changed = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003991 continue;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003992 } else if (tline->type == TOK_INDIRECT) {
3993 t = tline;
3994 tline = tline->next;
3995 tt = tokenize(t->text);
3996 tt = expand_mmac_params(tt);
3997 tt = expand_smacro(tt);
3998 *tail = tt;
3999 while (tt) {
4000 tt->a.mac = NULL; /* Necessary? */
4001 tail = &tt->next;
4002 tt = tt->next;
4003 }
4004 delete_Token(t);
4005 changed = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004006 } else {
4007 t = *tail = tline;
4008 tline = tline->next;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004009 t->a.mac = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004010 tail = &t->next;
4011 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00004012 }
H. Peter Anvineba20a72002-04-30 20:53:55 +00004013 *tail = NULL;
H. Peter Anvin67c63722008-10-26 23:49:00 -07004014
Cyrill Gorcunovc6a742c2011-06-27 01:23:09 +04004015 if (changed) {
4016 const struct tokseq_match t[] = {
4017 {
4018 PP_CONCAT_MASK(TOK_ID) |
4019 PP_CONCAT_MASK(TOK_FLOAT), /* head */
4020 PP_CONCAT_MASK(TOK_ID) |
4021 PP_CONCAT_MASK(TOK_NUMBER) |
4022 PP_CONCAT_MASK(TOK_FLOAT) |
4023 PP_CONCAT_MASK(TOK_OTHER) /* tail */
4024 },
4025 {
4026 PP_CONCAT_MASK(TOK_NUMBER), /* head */
4027 PP_CONCAT_MASK(TOK_NUMBER) /* tail */
4028 }
4029 };
4030 paste_tokens(&thead, t, ARRAY_SIZE(t), false);
4031 }
H. Peter Anvin6125b622009-04-08 14:02:25 -07004032
H. Peter Anvin76690a12002-04-30 20:52:49 +00004033 return thead;
4034}
4035
4036/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004037 * Expand all single-line macro calls made in the given line.
4038 * Return the expanded version of the line. The original is deemed
4039 * to be destroyed in the process. (In reality we'll just move
4040 * Tokens from input to output a lot of the time, rather than
4041 * actually bothering to destroy and replicate.)
4042 */
H. Peter Anvincb1cf592007-11-19 12:26:50 -08004043
H. Peter Anvine2c80182005-01-15 22:15:51 +00004044static Token *expand_smacro(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004045{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004046 Token *t, *tt, *mstart, **tail, *thead;
H. Peter Anvineba20a72002-04-30 20:53:55 +00004047 SMacro *head = NULL, *m;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004048 Token **params;
4049 int *paramsize;
H. Peter Anvin25a99342007-09-22 17:45:45 -07004050 unsigned int nparam, sparam;
H. Peter Anvind784a082009-04-20 14:01:18 -07004051 int brackets;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004052 Token *org_tline = tline;
4053 Context *ctx;
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08004054 const char *mname;
H. Peter Anvin2a15e692007-11-19 13:14:59 -08004055 int deadman = DEADMAN_LIMIT;
H. Peter Anvin8287daf2009-07-07 16:00:58 -07004056 bool expanded;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004057
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004058 /*
4059 * Trick: we should avoid changing the start token pointer since it can
4060 * be contained in "next" field of other token. Because of this
4061 * we allocate a copy of first token and work with it; at the end of
4062 * routine we copy it back
4063 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004064 if (org_tline) {
Cyrill Gorcunoved4a8052010-04-09 15:40:35 +04004065 tline = new_Token(org_tline->next, org_tline->type,
4066 org_tline->text, 0);
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004067 tline->a.mac = org_tline->a.mac;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004068 nasm_free(org_tline->text);
4069 org_tline->text = NULL;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004070 }
4071
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004072 expanded = true; /* Always expand %+ at least once */
H. Peter Anvin8287daf2009-07-07 16:00:58 -07004073
H. Peter Anvincb1cf592007-11-19 12:26:50 -08004074again:
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004075 thead = NULL;
Cyrill Gorcunoved4a8052010-04-09 15:40:35 +04004076 tail = &thead;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004077
H. Peter Anvine2c80182005-01-15 22:15:51 +00004078 while (tline) { /* main token loop */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004079 if (!--deadman) {
4080 error(ERR_NONFATAL, "interminable macro recursion");
Cyrill Gorcunovbd38c8f2009-11-21 11:11:23 +03004081 goto err;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004082 }
H. Peter Anvincb1cf592007-11-19 12:26:50 -08004083
H. Peter Anvine2c80182005-01-15 22:15:51 +00004084 if ((mname = tline->text)) {
4085 /* if this token is a local macro, look in local context */
Cyrill Gorcunovc56d9ad2010-02-11 15:12:19 +03004086 if (tline->type == TOK_ID) {
4087 head = (SMacro *)hash_findix(&smacros, mname);
4088 } else if (tline->type == TOK_PREPROC_ID) {
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04004089 ctx = get_ctx(mname, &mname);
Cyrill Gorcunovc56d9ad2010-02-11 15:12:19 +03004090 head = ctx ? (SMacro *)hash_findix(&ctx->localmac, mname) : NULL;
4091 } else
4092 head = NULL;
H. Peter Anvin072771e2008-05-22 13:17:51 -07004093
H. Peter Anvine2c80182005-01-15 22:15:51 +00004094 /*
4095 * We've hit an identifier. As in is_mmacro below, we first
4096 * check whether the identifier is a single-line macro at
4097 * all, then think about checking for parameters if
4098 * necessary.
4099 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004100 list_for_each(m, head)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004101 if (!mstrcmp(m->name, mname, m->casesense))
4102 break;
4103 if (m) {
4104 mstart = tline;
4105 params = NULL;
4106 paramsize = NULL;
4107 if (m->nparam == 0) {
4108 /*
4109 * Simple case: the macro is parameterless. Discard the
4110 * one token that the macro call took, and push the
4111 * expansion back on the to-do stack.
4112 */
4113 if (!m->expansion) {
4114 if (!strcmp("__FILE__", m->name)) {
4115 int32_t num = 0;
4116 char *file = NULL;
4117 src_get(&num, &file);
4118 tline->text = nasm_quote(file, strlen(file));
4119 tline->type = TOK_STRING;
4120 nasm_free(file);
H. Peter Anvine2c80182005-01-15 22:15:51 +00004121 continue;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004122 }
4123 if (!strcmp("__LINE__", m->name)) {
4124 nasm_free(tline->text);
4125 make_tok_num(tline, src_get_linnum());
4126 continue;
4127 }
4128 if (!strcmp("__BITS__", m->name)) {
4129 nasm_free(tline->text);
4130 make_tok_num(tline, globalbits);
4131 continue;
4132 }
4133 tline = delete_Token(tline);
4134 continue;
4135 }
4136 } else {
4137 /*
4138 * Complicated case: at least one macro with this name
H. Peter Anvine2c80182005-01-15 22:15:51 +00004139 * exists and takes parameters. We must find the
4140 * parameters in the call, count them, find the SMacro
4141 * that corresponds to that form of the macro call, and
4142 * substitute for the parameters when we expand. What a
4143 * pain.
4144 */
4145 /*tline = tline->next;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004146 skip_white_(tline); */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004147 do {
4148 t = tline->next;
4149 while (tok_type_(t, TOK_SMAC_END)) {
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004150 t->a.mac->in_progress = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004151 t->text = NULL;
4152 t = tline->next = delete_Token(t);
4153 }
4154 tline = t;
4155 } while (tok_type_(tline, TOK_WHITESPACE));
4156 if (!tok_is_(tline, "(")) {
4157 /*
4158 * This macro wasn't called with parameters: ignore
4159 * the call. (Behaviour borrowed from gnu cpp.)
4160 */
4161 tline = mstart;
4162 m = NULL;
4163 } else {
4164 int paren = 0;
4165 int white = 0;
4166 brackets = 0;
4167 nparam = 0;
4168 sparam = PARAM_DELTA;
4169 params = nasm_malloc(sparam * sizeof(Token *));
4170 params[0] = tline->next;
4171 paramsize = nasm_malloc(sparam * sizeof(int));
4172 paramsize[0] = 0;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07004173 while (true) { /* parameter loop */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004174 /*
4175 * For some unusual expansions
4176 * which concatenates function call
4177 */
4178 t = tline->next;
4179 while (tok_type_(t, TOK_SMAC_END)) {
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004180 t->a.mac->in_progress = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004181 t->text = NULL;
4182 t = tline->next = delete_Token(t);
4183 }
4184 tline = t;
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00004185
H. Peter Anvine2c80182005-01-15 22:15:51 +00004186 if (!tline) {
4187 error(ERR_NONFATAL,
4188 "macro call expects terminating `)'");
4189 break;
4190 }
4191 if (tline->type == TOK_WHITESPACE
4192 && brackets <= 0) {
4193 if (paramsize[nparam])
4194 white++;
4195 else
4196 params[nparam] = tline->next;
4197 continue; /* parameter loop */
4198 }
4199 if (tline->type == TOK_OTHER
4200 && tline->text[1] == 0) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004201 char ch = tline->text[0];
H. Peter Anvine2c80182005-01-15 22:15:51 +00004202 if (ch == ',' && !paren && brackets <= 0) {
4203 if (++nparam >= sparam) {
4204 sparam += PARAM_DELTA;
4205 params = nasm_realloc(params,
Cyrill Gorcunoved4a8052010-04-09 15:40:35 +04004206 sparam * sizeof(Token *));
4207 paramsize = nasm_realloc(paramsize,
4208 sparam * sizeof(int));
H. Peter Anvine2c80182005-01-15 22:15:51 +00004209 }
4210 params[nparam] = tline->next;
4211 paramsize[nparam] = 0;
4212 white = 0;
4213 continue; /* parameter loop */
4214 }
4215 if (ch == '{' &&
4216 (brackets > 0 || (brackets == 0 &&
4217 !paramsize[nparam])))
4218 {
4219 if (!(brackets++)) {
4220 params[nparam] = tline->next;
4221 continue; /* parameter loop */
4222 }
4223 }
4224 if (ch == '}' && brackets > 0)
4225 if (--brackets == 0) {
4226 brackets = -1;
4227 continue; /* parameter loop */
4228 }
4229 if (ch == '(' && !brackets)
4230 paren++;
4231 if (ch == ')' && brackets <= 0)
4232 if (--paren < 0)
4233 break;
4234 }
4235 if (brackets < 0) {
4236 brackets = 0;
4237 error(ERR_NONFATAL, "braces do not "
4238 "enclose all of macro parameter");
4239 }
4240 paramsize[nparam] += white + 1;
4241 white = 0;
4242 } /* parameter loop */
4243 nparam++;
4244 while (m && (m->nparam != nparam ||
4245 mstrcmp(m->name, mname,
4246 m->casesense)))
4247 m = m->next;
4248 if (!m)
H. Peter Anvin917a3492008-09-24 09:14:49 -07004249 error(ERR_WARNING|ERR_PASS1|ERR_WARN_MNP,
H. Peter Anvine2c80182005-01-15 22:15:51 +00004250 "macro `%s' exists, "
4251 "but not taking %d parameters",
4252 mstart->text, nparam);
4253 }
4254 }
4255 if (m && m->in_progress)
4256 m = NULL;
4257 if (!m) { /* in progess or didn't find '(' or wrong nparam */
H. Peter Anvin70653092007-10-19 14:42:29 -07004258 /*
H. Peter Anvine2c80182005-01-15 22:15:51 +00004259 * Design question: should we handle !tline, which
4260 * indicates missing ')' here, or expand those
4261 * macros anyway, which requires the (t) test a few
H. Peter Anvin70653092007-10-19 14:42:29 -07004262 * lines down?
H. Peter Anvine2c80182005-01-15 22:15:51 +00004263 */
4264 nasm_free(params);
4265 nasm_free(paramsize);
4266 tline = mstart;
4267 } else {
4268 /*
4269 * Expand the macro: we are placed on the last token of the
4270 * call, so that we can easily split the call from the
4271 * following tokens. We also start by pushing an SMAC_END
4272 * token for the cycle removal.
4273 */
4274 t = tline;
4275 if (t) {
4276 tline = t->next;
4277 t->next = NULL;
4278 }
4279 tt = new_Token(tline, TOK_SMAC_END, NULL, 0);
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004280 tt->a.mac = m;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07004281 m->in_progress = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004282 tline = tt;
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04004283 list_for_each(t, m->expansion) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004284 if (t->type >= TOK_SMAC_PARAM) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00004285 Token *pcopy = tline, **ptail = &pcopy;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004286 Token *ttt, *pt;
4287 int i;
H. Peter Anvin734b1882002-04-30 21:01:08 +00004288
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004289 ttt = params[t->type - TOK_SMAC_PARAM];
4290 i = paramsize[t->type - TOK_SMAC_PARAM];
4291 while (--i >= 0) {
4292 pt = *ptail = new_Token(tline, ttt->type,
4293 ttt->text, 0);
4294 ptail = &pt->next;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004295 ttt = ttt->next;
4296 }
4297 tline = pcopy;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004298 } else if (t->type == TOK_PREPROC_Q) {
4299 tt = new_Token(tline, TOK_ID, mname, 0);
4300 tline = tt;
4301 } else if (t->type == TOK_PREPROC_QQ) {
4302 tt = new_Token(tline, TOK_ID, m->name, 0);
4303 tline = tt;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004304 } else {
4305 tt = new_Token(tline, t->type, t->text, 0);
4306 tline = tt;
4307 }
4308 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00004309
H. Peter Anvine2c80182005-01-15 22:15:51 +00004310 /*
4311 * Having done that, get rid of the macro call, and clean
4312 * up the parameters.
4313 */
4314 nasm_free(params);
4315 nasm_free(paramsize);
4316 free_tlist(mstart);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004317 expanded = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004318 continue; /* main token loop */
4319 }
4320 }
4321 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004322
H. Peter Anvine2c80182005-01-15 22:15:51 +00004323 if (tline->type == TOK_SMAC_END) {
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004324 tline->a.mac->in_progress = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004325 tline = delete_Token(tline);
4326 } else {
4327 t = *tail = tline;
4328 tline = tline->next;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004329 t->a.mac = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004330 t->next = NULL;
4331 tail = &t->next;
4332 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004333 }
4334
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004335 /*
4336 * Now scan the entire line and look for successive TOK_IDs that resulted
Keith Kaniosb7a89542007-04-12 02:40:54 +00004337 * after expansion (they can't be produced by tokenize()). The successive
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004338 * TOK_IDs should be concatenated.
4339 * Also we look for %+ tokens and concatenate the tokens before and after
4340 * them (without white spaces in between).
4341 */
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +04004342 if (expanded) {
Cyrill Gorcunovc6a742c2011-06-27 01:23:09 +04004343 const struct tokseq_match t[] = {
4344 {
4345 PP_CONCAT_MASK(TOK_ID) |
4346 PP_CONCAT_MASK(TOK_PREPROC_ID), /* head */
4347 PP_CONCAT_MASK(TOK_ID) |
4348 PP_CONCAT_MASK(TOK_PREPROC_ID) |
4349 PP_CONCAT_MASK(TOK_NUMBER) /* tail */
4350 }
4351 };
4352 if (paste_tokens(&thead, t, ARRAY_SIZE(t), true)) {
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +04004353 /*
4354 * If we concatenated something, *and* we had previously expanded
4355 * an actual macro, scan the lines again for macros...
4356 */
4357 tline = thead;
4358 expanded = false;
4359 goto again;
4360 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00004361 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004362
Cyrill Gorcunovbd38c8f2009-11-21 11:11:23 +03004363err:
H. Peter Anvine2c80182005-01-15 22:15:51 +00004364 if (org_tline) {
4365 if (thead) {
4366 *org_tline = *thead;
4367 /* since we just gave text to org_line, don't free it */
4368 thead->text = NULL;
4369 delete_Token(thead);
4370 } else {
4371 /* the expression expanded to empty line;
4372 we can't return NULL for some reasons
4373 we just set the line to a single WHITESPACE token. */
4374 memset(org_tline, 0, sizeof(*org_tline));
4375 org_tline->text = NULL;
4376 org_tline->type = TOK_WHITESPACE;
4377 }
4378 thead = org_tline;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004379 }
4380
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004381 return thead;
4382}
4383
4384/*
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004385 * Similar to expand_smacro but used exclusively with macro identifiers
4386 * right before they are fetched in. The reason is that there can be
4387 * identifiers consisting of several subparts. We consider that if there
4388 * are more than one element forming the name, user wants a expansion,
4389 * otherwise it will be left as-is. Example:
4390 *
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004391 * %define %$abc cde
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004392 *
4393 * the identifier %$abc will be left as-is so that the handler for %define
4394 * will suck it and define the corresponding value. Other case:
4395 *
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004396 * %define _%$abc cde
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004397 *
4398 * In this case user wants name to be expanded *before* %define starts
4399 * working, so we'll expand %$abc into something (if it has a value;
4400 * otherwise it will be left as-is) then concatenate all successive
4401 * PP_IDs into one.
4402 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004403static Token *expand_id(Token * tline)
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004404{
4405 Token *cur, *oldnext = NULL;
4406
H. Peter Anvin734b1882002-04-30 21:01:08 +00004407 if (!tline || !tline->next)
H. Peter Anvine2c80182005-01-15 22:15:51 +00004408 return tline;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004409
4410 cur = tline;
4411 while (cur->next &&
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004412 (cur->next->type == TOK_ID ||
4413 cur->next->type == TOK_PREPROC_ID
4414 || cur->next->type == TOK_NUMBER))
H. Peter Anvine2c80182005-01-15 22:15:51 +00004415 cur = cur->next;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004416
4417 /* If identifier consists of just one token, don't expand */
4418 if (cur == tline)
H. Peter Anvine2c80182005-01-15 22:15:51 +00004419 return tline;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004420
H. Peter Anvine2c80182005-01-15 22:15:51 +00004421 if (cur) {
4422 oldnext = cur->next; /* Detach the tail past identifier */
4423 cur->next = NULL; /* so that expand_smacro stops here */
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004424 }
4425
H. Peter Anvin734b1882002-04-30 21:01:08 +00004426 tline = expand_smacro(tline);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004427
H. Peter Anvine2c80182005-01-15 22:15:51 +00004428 if (cur) {
4429 /* expand_smacro possibly changhed tline; re-scan for EOL */
4430 cur = tline;
4431 while (cur && cur->next)
4432 cur = cur->next;
4433 if (cur)
4434 cur->next = oldnext;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004435 }
4436
4437 return tline;
4438}
4439
4440/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004441 * Determine whether the given line constitutes a multi-line macro
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004442 * call, and return the MMacro structure called if so. Doesn't have
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004443 * to check for an initial label - that's taken care of in
4444 * expand_mmacro - but must check numbers of parameters. Guaranteed
4445 * to be called with tline->type == TOK_ID, so the putative macro
4446 * name is easy to find.
4447 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004448static MMacro *is_mmacro(Token * tline, Token *** params_array)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004449{
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004450 MMacro *head, *m;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004451 Token **params;
4452 int nparam;
4453
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004454 head = (MMacro *) hash_findix(&mmacros, tline->text);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004455
4456 /*
4457 * Efficiency: first we see if any macro exists with the given
4458 * name. If not, we can return NULL immediately. _Then_ we
4459 * count the parameters, and then we look further along the
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004460 * list if necessary to find the proper MMacro.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004461 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004462 list_for_each(m, head)
4463 if (!mstrcmp(m->name, tline->text, m->casesense))
H. Peter Anvine2c80182005-01-15 22:15:51 +00004464 break;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004465 if (!m)
H. Peter Anvine2c80182005-01-15 22:15:51 +00004466 return NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004467
4468 /*
4469 * OK, we have a potential macro. Count and demarcate the
4470 * parameters.
4471 */
H. Peter Anvin734b1882002-04-30 21:01:08 +00004472 count_mmac_params(tline->next, &nparam, &params);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004473
4474 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004475 * So we know how many parameters we've got. Find the MMacro
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004476 * structure that handles this number.
4477 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004478 while (m) {
4479 if (m->nparam_min <= nparam
4480 && (m->plus || nparam <= m->nparam_max)) {
4481 /*
4482 * This one is right. Just check if cycle removal
4483 * prohibits us using it before we actually celebrate...
4484 */
4485 if (m->in_progress > m->max_depth) {
4486 if (m->max_depth > 0) {
4487 error(ERR_WARNING,
4488 "reached maximum recursion depth of %i",
4489 m->max_depth);
4490 }
4491 nasm_free(params);
4492 return NULL;
4493 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00004494 /*
4495 * It's right, and we can use it. Add its default
4496 * parameters to the end of our list if necessary.
4497 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004498 if (m->defaults && nparam < m->nparam_min + m->ndefs) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00004499 params =
4500 nasm_realloc(params,
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004501 ((m->nparam_min + m->ndefs +
H. Peter Anvine2c80182005-01-15 22:15:51 +00004502 1) * sizeof(*params)));
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004503 while (nparam < m->nparam_min + m->ndefs) {
4504 params[nparam] = m->defaults[nparam - m->nparam_min];
H. Peter Anvine2c80182005-01-15 22:15:51 +00004505 nparam++;
4506 }
4507 }
4508 /*
4509 * If we've gone over the maximum parameter count (and
4510 * we're in Plus mode), ignore parameters beyond
4511 * nparam_max.
4512 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004513 if (m->plus && nparam > m->nparam_max)
4514 nparam = m->nparam_max;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004515 /*
4516 * Then terminate the parameter list, and leave.
4517 */
4518 if (!params) { /* need this special case */
4519 params = nasm_malloc(sizeof(*params));
4520 nparam = 0;
4521 }
4522 params[nparam] = NULL;
4523 *params_array = params;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004524 return m;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004525 }
4526 /*
4527 * This one wasn't right: look for the next one with the
4528 * same name.
4529 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004530 list_for_each(m, m->next)
4531 if (!mstrcmp(m->name, tline->text, m->casesense))
H. Peter Anvine2c80182005-01-15 22:15:51 +00004532 break;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004533 }
4534
4535 /*
4536 * After all that, we didn't find one with the right number of
4537 * parameters. Issue a warning, and fail to expand the macro.
4538 */
H. Peter Anvin917a3492008-09-24 09:14:49 -07004539 error(ERR_WARNING|ERR_PASS1|ERR_WARN_MNP,
H. Peter Anvine2c80182005-01-15 22:15:51 +00004540 "macro `%s' exists, but not taking %d parameters",
4541 tline->text, nparam);
H. Peter Anvin734b1882002-04-30 21:01:08 +00004542 nasm_free(params);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004543 return NULL;
4544}
4545
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004546
4547/*
4548 * Save MMacro invocation specific fields in
4549 * preparation for a recursive macro expansion
4550 */
4551static void push_mmacro(MMacro *m)
4552{
4553 MMacroInvocation *i;
4554
4555 i = nasm_malloc(sizeof(MMacroInvocation));
4556 i->prev = m->prev;
4557 i->params = m->params;
4558 i->iline = m->iline;
4559 i->nparam = m->nparam;
4560 i->rotate = m->rotate;
4561 i->paramlen = m->paramlen;
4562 i->unique = m->unique;
4563 i->condcnt = m->condcnt;
4564 m->prev = i;
4565}
4566
4567
4568/*
4569 * Restore MMacro invocation specific fields that were
4570 * saved during a previous recursive macro expansion
4571 */
4572static void pop_mmacro(MMacro *m)
4573{
4574 MMacroInvocation *i;
4575
4576 if (m->prev) {
4577 i = m->prev;
4578 m->prev = i->prev;
4579 m->params = i->params;
4580 m->iline = i->iline;
4581 m->nparam = i->nparam;
4582 m->rotate = i->rotate;
4583 m->paramlen = i->paramlen;
4584 m->unique = i->unique;
4585 m->condcnt = i->condcnt;
4586 nasm_free(i);
4587 }
4588}
4589
4590
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004591/*
4592 * Expand the multi-line macro call made by the given line, if
4593 * there is one to be expanded. If there is, push the expansion on
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004594 * istk->expansion and return 1. Otherwise return 0.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004595 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004596static int expand_mmacro(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004597{
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004598 Token *startline = tline;
H. Peter Anvineba20a72002-04-30 20:53:55 +00004599 Token *label = NULL;
4600 int dont_prepend = 0;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004601 Token **params, *t, *tt;
4602 MMacro *m;
4603 Line *l, *ll;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004604 int i, nparam, *paramlen;
H. Peter Anvinc751e862008-06-09 10:18:45 -07004605 const char *mname;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004606
4607 t = tline;
H. Peter Anvineba20a72002-04-30 20:53:55 +00004608 skip_white_(t);
H. Peter Anvince2233b2008-05-25 21:57:00 -07004609 /* if (!tok_type_(t, TOK_ID)) Lino 02/25/02 */
H. Peter Anvindce1e2f2002-04-30 21:06:37 +00004610 if (!tok_type_(t, TOK_ID) && !tok_type_(t, TOK_PREPROC_ID))
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004611 return 0;
4612 m = is_mmacro(t, &params);
4613 if (m) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004614 mname = t->text;
H. Peter Anvinc751e862008-06-09 10:18:45 -07004615 } else {
H. Peter Anvine2c80182005-01-15 22:15:51 +00004616 Token *last;
4617 /*
4618 * We have an id which isn't a macro call. We'll assume
4619 * it might be a label; we'll also check to see if a
4620 * colon follows it. Then, if there's another id after
4621 * that lot, we'll check it again for macro-hood.
4622 */
4623 label = last = t;
4624 t = t->next;
4625 if (tok_type_(t, TOK_WHITESPACE))
4626 last = t, t = t->next;
4627 if (tok_is_(t, ":")) {
4628 dont_prepend = 1;
4629 last = t, t = t->next;
4630 if (tok_type_(t, TOK_WHITESPACE))
4631 last = t, t = t->next;
4632 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004633 if (!tok_type_(t, TOK_ID) || !(m = is_mmacro(t, &params)))
4634 return 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004635 last->next = NULL;
Keith Kanios891775e2009-07-11 06:08:54 -05004636 mname = t->text;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004637 tline = t;
H. Peter Anvineba20a72002-04-30 20:53:55 +00004638 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004639
4640 /*
4641 * Fix up the parameters: this involves stripping leading and
4642 * trailing whitespace, then stripping braces if they are
4643 * present.
4644 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004645 for (nparam = 0; params[nparam]; nparam++) ;
H. Peter Anvin734b1882002-04-30 21:01:08 +00004646 paramlen = nparam ? nasm_malloc(nparam * sizeof(*paramlen)) : NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004647
H. Peter Anvine2c80182005-01-15 22:15:51 +00004648 for (i = 0; params[i]; i++) {
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08004649 int brace = 0;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004650 int comma = (!m->plus || i < nparam - 1);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004651
H. Peter Anvine2c80182005-01-15 22:15:51 +00004652 t = params[i];
4653 skip_white_(t);
4654 if (tok_is_(t, "{"))
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08004655 t = t->next, brace++, comma = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004656 params[i] = t;
4657 paramlen[i] = 0;
4658 while (t) {
4659 if (comma && t->type == TOK_OTHER && !strcmp(t->text, ","))
4660 break; /* ... because we have hit a comma */
4661 if (comma && t->type == TOK_WHITESPACE
4662 && tok_is_(t->next, ","))
4663 break; /* ... or a space then a comma */
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08004664 if (brace && t->type == TOK_OTHER) {
4665 if (t->text[0] == '{')
4666 brace++; /* ... or a nested opening brace */
4667 else if (t->text[0] == '}')
4668 if (!--brace)
4669 break; /* ... or a brace */
4670 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00004671 t = t->next;
4672 paramlen[i]++;
4673 }
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08004674 if (brace)
4675 error(ERR_NONFATAL, "macro params should be enclosed in braces");
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004676 }
4677
4678 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004679 * OK, we have a MMacro structure together with a set of
4680 * parameters. We must now go through the expansion and push
4681 * copies of each Line on to istk->expansion. Substitution of
H. Peter Anvin76690a12002-04-30 20:52:49 +00004682 * parameter tokens and macro-local tokens doesn't get done
4683 * until the single-line macro substitution process; this is
4684 * because delaying them allows us to change the semantics
4685 * later through %rotate.
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004686 *
4687 * First, push an end marker on to istk->expansion, mark this
4688 * macro as in progress, and set up its invocation-specific
4689 * variables.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004690 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004691 ll = nasm_malloc(sizeof(Line));
4692 ll->next = istk->expansion;
4693 ll->finishes = m;
4694 ll->first = NULL;
4695 istk->expansion = ll;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004696
4697 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004698 * Save the previous MMacro expansion in the case of
4699 * macro recursion
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004700 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004701 if (m->max_depth && m->in_progress)
4702 push_mmacro(m);
4703
4704 m->in_progress ++;
4705 m->params = params;
4706 m->iline = tline;
4707 m->nparam = nparam;
4708 m->rotate = 0;
4709 m->paramlen = paramlen;
4710 m->unique = unique++;
4711 m->lineno = 0;
4712 m->condcnt = 0;
4713
4714 m->next_active = istk->mstk;
4715 istk->mstk = m;
4716
4717 list_for_each(l, m->expansion) {
4718 Token **tail;
4719
4720 ll = nasm_malloc(sizeof(Line));
4721 ll->finishes = NULL;
4722 ll->next = istk->expansion;
4723 istk->expansion = ll;
4724 tail = &ll->first;
4725
4726 list_for_each(t, l->first) {
4727 Token *x = t;
4728 switch (t->type) {
4729 case TOK_PREPROC_Q:
4730 tt = *tail = new_Token(NULL, TOK_ID, mname, 0);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004731 break;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004732 case TOK_PREPROC_QQ:
4733 tt = *tail = new_Token(NULL, TOK_ID, m->name, 0);
4734 break;
4735 case TOK_PREPROC_ID:
4736 if (t->text[1] == '0' && t->text[2] == '0') {
4737 dont_prepend = -1;
4738 x = label;
4739 if (!x)
4740 continue;
4741 }
4742 /* fall through */
4743 default:
4744 tt = *tail = new_Token(NULL, x->type, x->text, 0);
4745 break;
4746 }
4747 tail = &tt->next;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004748 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004749 *tail = NULL;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004750 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004751
4752 /*
H. Peter Anvineba20a72002-04-30 20:53:55 +00004753 * If we had a label, push it on as the first line of
4754 * the macro expansion.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004755 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004756 if (label) {
4757 if (dont_prepend < 0)
4758 free_tlist(startline);
4759 else {
4760 ll = nasm_malloc(sizeof(Line));
4761 ll->finishes = NULL;
4762 ll->next = istk->expansion;
4763 istk->expansion = ll;
4764 ll->first = startline;
4765 if (!dont_prepend) {
4766 while (label->next)
4767 label = label->next;
4768 label->next = tt = new_Token(NULL, TOK_OTHER, ":", 0);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004769 }
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004770 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004771 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004772
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004773 list->uplevel(m->nolist ? LIST_MACRO_NOLIST : LIST_MACRO);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004774
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004775 return 1;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004776}
4777
Victor van den Elzen3b404c02008-09-18 13:51:36 +02004778/* The function that actually does the error reporting */
4779static void verror(int severity, const char *fmt, va_list arg)
4780{
4781 char buff[1024];
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004782 MMacro *mmac = NULL;
4783 int delta = 0;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02004784
4785 vsnprintf(buff, sizeof(buff), fmt, arg);
4786
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004787 /* get %macro name */
4788 if (istk && istk->mstk) {
4789 mmac = istk->mstk;
4790 /* but %rep blocks should be skipped */
4791 while (mmac && !mmac->name)
4792 mmac = mmac->next_active, delta++;
Cyrill Gorcunov9900c6b2011-10-09 18:58:46 +04004793 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004794
4795 if (mmac)
4796 nasm_error(severity, "(%s:%d) %s",
4797 mmac->name, mmac->lineno - delta, buff);
4798 else
4799 nasm_error(severity, "%s", buff);
Victor van den Elzen3b404c02008-09-18 13:51:36 +02004800}
4801
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004802/*
4803 * Since preprocessor always operate only on the line that didn't
H. Peter Anvin917a3492008-09-24 09:14:49 -07004804 * arrived yet, we should always use ERR_OFFBY1.
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004805 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004806static void error(int severity, const char *fmt, ...)
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004807{
4808 va_list arg;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004809
4810 /* If we're in a dead branch of IF or something like it, ignore the error */
4811 if (istk && istk->conds && !emitting(istk->conds->state))
4812 return;
4813
H. Peter Anvin734b1882002-04-30 21:01:08 +00004814 va_start(arg, fmt);
Victor van den Elzen3b404c02008-09-18 13:51:36 +02004815 verror(severity, fmt, arg);
H. Peter Anvin734b1882002-04-30 21:01:08 +00004816 va_end(arg);
Victor van den Elzen3b404c02008-09-18 13:51:36 +02004817}
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004818
Victor van den Elzen3b404c02008-09-18 13:51:36 +02004819/*
4820 * Because %else etc are evaluated in the state context
4821 * of the previous branch, errors might get lost with error():
4822 * %if 0 ... %else trailing garbage ... %endif
4823 * So %else etc should report errors with this function.
4824 */
4825static void error_precond(int severity, const char *fmt, ...)
4826{
4827 va_list arg;
4828
4829 /* Only ignore the error if it's really in a dead branch */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004830 if (istk && istk->conds && istk->conds->state == COND_NEVER)
Victor van den Elzen3b404c02008-09-18 13:51:36 +02004831 return;
4832
4833 va_start(arg, fmt);
4834 verror(severity, fmt, arg);
4835 va_end(arg);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004836}
4837
H. Peter Anvin734b1882002-04-30 21:01:08 +00004838static void
H. Peter Anvindbb640b2009-07-18 18:57:16 -07004839pp_reset(char *file, int apass, ListGen * listgen, StrList **deplist)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004840{
H. Peter Anvin7383b402008-09-24 10:20:40 -07004841 Token *t;
4842
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004843 cstk = NULL;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004844 istk = nasm_malloc(sizeof(Include));
4845 istk->next = NULL;
4846 istk->conds = NULL;
4847 istk->expansion = NULL;
4848 istk->mstk = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004849 istk->fp = fopen(file, "r");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004850 istk->fname = NULL;
H. Peter Anvineba20a72002-04-30 20:53:55 +00004851 src_set_fname(nasm_strdup(file));
4852 src_set_linnum(0);
4853 istk->lineinc = 1;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004854 if (!istk->fp)
H. Peter Anvin917a3492008-09-24 09:14:49 -07004855 error(ERR_FATAL|ERR_NOFILE, "unable to open input file `%s'",
H. Peter Anvine2c80182005-01-15 22:15:51 +00004856 file);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004857 defining = NULL;
Charles Crayned4200be2008-07-12 16:42:33 -07004858 nested_mac_count = 0;
4859 nested_rep_count = 0;
H. Peter Anvin97a23472007-09-16 17:57:25 -07004860 init_macros();
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004861 unique = 0;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004862 if (tasm_compatible_mode) {
H. Peter Anvina4835d42008-05-20 14:21:29 -07004863 stdmacpos = nasm_stdmac;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004864 } else {
H. Peter Anvina4835d42008-05-20 14:21:29 -07004865 stdmacpos = nasm_stdmac_after_tasm;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004866 }
H. Peter Anvind2456592008-06-19 15:04:18 -07004867 any_extrastdmac = extrastdmac && *extrastdmac;
4868 do_predef = true;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004869 list = listgen;
H. Peter Anvin61f130f2008-09-25 15:45:06 -07004870
4871 /*
4872 * 0 for dependencies, 1 for preparatory passes, 2 for final pass.
4873 * The caller, however, will also pass in 3 for preprocess-only so
4874 * we can set __PASS__ accordingly.
4875 */
4876 pass = apass > 2 ? 2 : apass;
4877
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07004878 dephead = deptail = deplist;
4879 if (deplist) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004880 StrList *sl = nasm_malloc(strlen(file)+1+sizeof sl->next);
4881 sl->next = NULL;
4882 strcpy(sl->str, file);
4883 *deptail = sl;
4884 deptail = &sl->next;
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07004885 }
H. Peter Anvin7383b402008-09-24 10:20:40 -07004886
H. Peter Anvin61f130f2008-09-25 15:45:06 -07004887 /*
4888 * Define the __PASS__ macro. This is defined here unlike
4889 * all the other builtins, because it is special -- it varies between
4890 * passes.
4891 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004892 t = nasm_malloc(sizeof(*t));
4893 t->next = NULL;
H. Peter Anvin61f130f2008-09-25 15:45:06 -07004894 make_tok_num(t, apass);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004895 t->a.mac = NULL;
H. Peter Anvin7383b402008-09-24 10:20:40 -07004896 define_smacro(NULL, "__PASS__", true, 0, t);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004897}
4898
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004899static char *pp_getline(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004900{
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004901 char *line;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004902 Token *tline;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004903
H. Peter Anvine2c80182005-01-15 22:15:51 +00004904 while (1) {
4905 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004906 * Fetch a tokenized line, either from the macro-expansion
H. Peter Anvine2c80182005-01-15 22:15:51 +00004907 * buffer or from the input file.
4908 */
4909 tline = NULL;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004910 while (istk->expansion && istk->expansion->finishes) {
4911 Line *l = istk->expansion;
4912 if (!l->finishes->name && l->finishes->in_progress > 1) {
4913 Line *ll;
H. Peter Anvineba20a72002-04-30 20:53:55 +00004914
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004915 /*
4916 * This is a macro-end marker for a macro with no
4917 * name, which means it's not really a macro at all
4918 * but a %rep block, and the `in_progress' field is
4919 * more than 1, meaning that we still need to
4920 * repeat. (1 means the natural last repetition; 0
4921 * means termination by %exitrep.) We have
4922 * therefore expanded up to the %endrep, and must
4923 * push the whole block on to the expansion buffer
4924 * again. We don't bother to remove the macro-end
4925 * marker: we'd only have to generate another one
4926 * if we did.
4927 */
4928 l->finishes->in_progress--;
4929 list_for_each(l, l->finishes->expansion) {
4930 Token *t, *tt, **tail;
4931
4932 ll = nasm_malloc(sizeof(Line));
4933 ll->next = istk->expansion;
4934 ll->finishes = NULL;
4935 ll->first = NULL;
4936 tail = &ll->first;
4937
4938 list_for_each(t, l->first) {
4939 if (t->text || t->type == TOK_WHITESPACE) {
4940 tt = *tail = new_Token(NULL, t->type, t->text, 0);
4941 tail = &tt->next;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004942 }
4943 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004944
4945 istk->expansion = ll;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004946 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004947 } else {
4948 /*
4949 * Check whether a `%rep' was started and not ended
4950 * within this macro expansion. This can happen and
4951 * should be detected. It's a fatal error because
4952 * I'm too confused to work out how to recover
4953 * sensibly from it.
4954 */
4955 if (defining) {
4956 if (defining->name)
4957 error(ERR_PANIC,
4958 "defining with name in expansion");
4959 else if (istk->mstk->name)
4960 error(ERR_FATAL,
4961 "`%%rep' without `%%endrep' within"
4962 " expansion of macro `%s'",
4963 istk->mstk->name);
4964 }
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004965
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004966 /*
4967 * FIXME: investigate the relationship at this point between
4968 * istk->mstk and l->finishes
4969 */
4970 {
4971 MMacro *m = istk->mstk;
4972 istk->mstk = m->next_active;
4973 if (m->name) {
4974 /*
4975 * This was a real macro call, not a %rep, and
4976 * therefore the parameter information needs to
4977 * be freed.
4978 */
4979 if (m->prev) {
4980 pop_mmacro(m);
4981 l->finishes->in_progress --;
4982 } else {
4983 nasm_free(m->params);
4984 free_tlist(m->iline);
4985 nasm_free(m->paramlen);
4986 l->finishes->in_progress = 0;
4987 }
4988 } else
4989 free_mmacro(m);
4990 }
4991 istk->expansion = l->next;
4992 nasm_free(l);
4993 list->downlevel(LIST_MACRO);
4994 }
4995 }
4996 while (1) { /* until we get a line we can use */
4997
4998 if (istk->expansion) { /* from a macro expansion */
4999 char *p;
5000 Line *l = istk->expansion;
5001 if (istk->mstk)
5002 istk->mstk->lineno++;
5003 tline = l->first;
5004 istk->expansion = l->next;
5005 nasm_free(l);
5006 p = detoken(tline, false);
5007 list->line(LIST_MACRO, p);
5008 nasm_free(p);
5009 break;
5010 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00005011 line = read_line();
5012 if (line) { /* from the current input file */
5013 line = prepreproc(line);
Keith Kaniosb7a89542007-04-12 02:40:54 +00005014 tline = tokenize(line);
H. Peter Anvine2c80182005-01-15 22:15:51 +00005015 nasm_free(line);
5016 break;
5017 }
5018 /*
5019 * The current file has ended; work down the istk
5020 */
5021 {
5022 Include *i = istk;
5023 fclose(i->fp);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005024 if (i->conds) {
5025 /* nasm_error can't be conditionally suppressed */
5026 nasm_error(ERR_FATAL,
5027 "expected `%%endif' before end of file");
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005028 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00005029 /* only set line and file name if there's a next node */
5030 if (i->next) {
5031 src_set_linnum(i->lineno);
Cyrill Gorcunovd34a1082011-03-07 11:23:08 +03005032 nasm_free(src_set_fname(nasm_strdup(i->fname)));
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03005033 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00005034 istk = i->next;
5035 list->downlevel(LIST_INCLUDE);
5036 nasm_free(i);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005037 if (!istk)
5038 return NULL;
5039 if (istk->expansion && istk->expansion->finishes)
5040 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005041 }
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005042 }
5043
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005044 /*
5045 * We must expand MMacro parameters and MMacro-local labels
5046 * _before_ we plunge into directive processing, to cope
5047 * with things like `%define something %1' such as STRUC
5048 * uses. Unless we're _defining_ a MMacro, in which case
5049 * those tokens should be left alone to go into the
5050 * definition; and unless we're in a non-emitting
5051 * condition, in which case we don't want to meddle with
5052 * anything.
5053 */
5054 if (!defining && !(istk->conds && !emitting(istk->conds->state))
5055 && !(istk->mstk && !istk->mstk->in_progress)) {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005056 tline = expand_mmac_params(tline);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005057 }
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005058
H. Peter Anvine2c80182005-01-15 22:15:51 +00005059 /*
5060 * Check the line to see if it's a preprocessor directive.
5061 */
5062 if (do_directive(tline) == DIRECTIVE_FOUND) {
5063 continue;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005064 } else if (defining) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00005065 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005066 * We're defining a multi-line macro. We emit nothing
5067 * at all, and just
5068 * shove the tokenized line on to the macro definition.
H. Peter Anvine2c80182005-01-15 22:15:51 +00005069 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005070 Line *l = nasm_malloc(sizeof(Line));
5071 l->next = defining->expansion;
5072 l->first = tline;
5073 l->finishes = NULL;
5074 defining->expansion = l;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005075 continue;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005076 } else if (istk->conds && !emitting(istk->conds->state)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00005077 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005078 * We're in a non-emitting branch of a condition block.
H. Peter Anvine2c80182005-01-15 22:15:51 +00005079 * Emit nothing at all, not even a blank line: when we
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005080 * emerge from the condition we'll give a line-number
H. Peter Anvine2c80182005-01-15 22:15:51 +00005081 * directive so we keep our place correctly.
5082 */
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005083 free_tlist(tline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00005084 continue;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005085 } else if (istk->mstk && !istk->mstk->in_progress) {
5086 /*
5087 * We're in a %rep block which has been terminated, so
5088 * we're walking through to the %endrep without
5089 * emitting anything. Emit nothing at all, not even a
5090 * blank line: when we emerge from the %rep block we'll
5091 * give a line-number directive so we keep our place
5092 * correctly.
5093 */
5094 free_tlist(tline);
5095 continue;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005096 } else {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005097 tline = expand_smacro(tline);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005098 if (!expand_mmacro(tline)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00005099 /*
Keith Kaniosb7a89542007-04-12 02:40:54 +00005100 * De-tokenize the line again, and emit it.
H. Peter Anvine2c80182005-01-15 22:15:51 +00005101 */
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005102 line = detoken(tline, true);
5103 free_tlist(tline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00005104 break;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005105 } else {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005106 continue; /* expand_mmacro calls free_tlist */
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005107 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00005108 }
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005109 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005110
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005111 return line;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005112}
5113
H. Peter Anvine2c80182005-01-15 22:15:51 +00005114static void pp_cleanup(int pass)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005115{
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005116 if (defining) {
5117 if (defining->name) {
5118 error(ERR_NONFATAL,
5119 "end of file while still defining macro `%s'",
5120 defining->name);
5121 } else {
5122 error(ERR_NONFATAL, "end of file while still in %%rep");
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005123 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005124
5125 free_mmacro(defining);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005126 defining = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005127 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005128 while (cstk)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005129 ctx_pop();
H. Peter Anvin97a23472007-09-16 17:57:25 -07005130 free_macros();
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005131 while (istk) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00005132 Include *i = istk;
5133 istk = istk->next;
5134 fclose(i->fp);
5135 nasm_free(i->fname);
Cyrill Gorcunov8dcfd882011-03-03 09:18:56 +03005136 nasm_free(i);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005137 }
5138 while (cstk)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005139 ctx_pop();
H. Peter Anvin86877b22008-06-20 15:55:45 -07005140 nasm_free(src_set_fname(NULL));
H. Peter Anvine2c80182005-01-15 22:15:51 +00005141 if (pass == 0) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03005142 IncPath *i;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005143 free_llist(predef);
5144 delete_Blocks();
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03005145 while ((i = ipath)) {
5146 ipath = i->next;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005147 if (i->path)
5148 nasm_free(i->path);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03005149 nasm_free(i);
5150 }
H. Peter Anvin11dfa1a2008-07-02 18:11:04 -07005151 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005152}
5153
Cyrill Gorcunov0b78bff2012-05-07 01:57:55 +04005154static void pp_include_path(char *path)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005155{
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005156 IncPath *i;
H. Peter Anvin37a321f2007-09-24 13:41:58 -07005157
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005158 i = nasm_malloc(sizeof(IncPath));
5159 i->path = path ? nasm_strdup(path) : NULL;
5160 i->next = NULL;
Frank Kotlerd0ed6fd2003-08-27 11:33:56 +00005161
H. Peter Anvin89cee572009-07-15 09:16:54 -04005162 if (ipath) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00005163 IncPath *j = ipath;
H. Peter Anvin89cee572009-07-15 09:16:54 -04005164 while (j->next)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005165 j = j->next;
5166 j->next = i;
5167 } else {
5168 ipath = i;
Frank Kotlerd0ed6fd2003-08-27 11:33:56 +00005169 }
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005170}
Frank Kotlerd0ed6fd2003-08-27 11:33:56 +00005171
Cyrill Gorcunov0b78bff2012-05-07 01:57:55 +04005172static void pp_pre_include(char *fname)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005173{
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005174 Token *inc, *space, *name;
5175 Line *l;
5176
H. Peter Anvin734b1882002-04-30 21:01:08 +00005177 name = new_Token(NULL, TOK_INTERNAL_STRING, fname, 0);
5178 space = new_Token(name, TOK_WHITESPACE, NULL, 0);
5179 inc = new_Token(space, TOK_PREPROC_ID, "%include", 0);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005180
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005181 l = nasm_malloc(sizeof(Line));
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005182 l->next = predef;
5183 l->first = inc;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005184 l->finishes = NULL;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005185 predef = l;
5186}
5187
Cyrill Gorcunov0b78bff2012-05-07 01:57:55 +04005188static void pp_pre_define(char *definition)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005189{
5190 Token *def, *space;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005191 Line *l;
Keith Kaniosa6dfa782007-04-13 16:47:53 +00005192 char *equals;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005193
5194 equals = strchr(definition, '=');
H. Peter Anvin734b1882002-04-30 21:01:08 +00005195 space = new_Token(NULL, TOK_WHITESPACE, NULL, 0);
5196 def = new_Token(space, TOK_PREPROC_ID, "%define", 0);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005197 if (equals)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005198 *equals = ' ';
Keith Kaniosb7a89542007-04-12 02:40:54 +00005199 space->next = tokenize(definition);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005200 if (equals)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005201 *equals = '=';
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005202
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005203 l = nasm_malloc(sizeof(Line));
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005204 l->next = predef;
5205 l->first = def;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005206 l->finishes = NULL;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005207 predef = l;
5208}
5209
Cyrill Gorcunov0b78bff2012-05-07 01:57:55 +04005210static void pp_pre_undefine(char *definition)
H. Peter Anvin620515a2002-04-30 20:57:38 +00005211{
5212 Token *def, *space;
5213 Line *l;
5214
H. Peter Anvin734b1882002-04-30 21:01:08 +00005215 space = new_Token(NULL, TOK_WHITESPACE, NULL, 0);
5216 def = new_Token(space, TOK_PREPROC_ID, "%undef", 0);
Keith Kaniosb7a89542007-04-12 02:40:54 +00005217 space->next = tokenize(definition);
H. Peter Anvin620515a2002-04-30 20:57:38 +00005218
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005219 l = nasm_malloc(sizeof(Line));
H. Peter Anvin620515a2002-04-30 20:57:38 +00005220 l->next = predef;
5221 l->first = def;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005222 l->finishes = NULL;
H. Peter Anvin620515a2002-04-30 20:57:38 +00005223 predef = l;
5224}
5225
Cyrill Gorcunov0b78bff2012-05-07 01:57:55 +04005226static void pp_extra_stdmac(macros_t *macros)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005227{
H. Peter Anvin76690a12002-04-30 20:52:49 +00005228 extrastdmac = macros;
5229}
5230
Keith Kaniosa5fc6462007-10-13 07:09:22 -07005231static void make_tok_num(Token * tok, int64_t val)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005232{
Cyrill Gorcunovce652742013-05-06 23:43:43 +04005233 char numbuf[32];
Keith Kaniosa5fc6462007-10-13 07:09:22 -07005234 snprintf(numbuf, sizeof(numbuf), "%"PRId64"", val);
H. Peter Anvineba20a72002-04-30 20:53:55 +00005235 tok->text = nasm_strdup(numbuf);
5236 tok->type = TOK_NUMBER;
5237}
5238
Cyrill Gorcunov86b2ad02011-07-01 10:38:25 +04005239struct preproc_ops nasmpp = {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005240 pp_reset,
5241 pp_getline,
Cyrill Gorcunov0b78bff2012-05-07 01:57:55 +04005242 pp_cleanup,
5243 pp_extra_stdmac,
5244 pp_pre_define,
5245 pp_pre_undefine,
5246 pp_pre_include,
5247 pp_include_path
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005248};