blob: c18facdff491c2de19eed39040ea50618c348679 [file] [log] [blame]
H. Peter Anvin9e6747c2009-06-28 17:13:04 -07001/* ----------------------------------------------------------------------- *
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002 *
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003 * Copyright 1996-2012 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) {
506 next = t->next;
507 t->next = prev;
508 prev = t;
509 t = next;
510 }
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
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000802#define BUF_DELTA 512
803/*
804 * Read a line from the top file in istk, handling multiple CR/LFs
805 * at the end of the line read, and handling spurious ^Zs. Will
806 * return lines from the standard macro set if this has not already
807 * been done.
808 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000809static char *read_line(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000810{
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000811 char *buffer, *p, *q;
H. Peter Anvin9f394642002-04-30 21:07:51 +0000812 int bufsize, continued_count;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000813
Cyrill Gorcunov15bdc512010-07-13 11:27:41 +0400814 /*
815 * standart macros set (predefined) goes first
816 */
817 p = line_from_stdmac();
818 if (p)
819 return p;
H. Peter Anvin72edbb82008-06-19 16:00:04 -0700820
Cyrill Gorcunov15bdc512010-07-13 11:27:41 +0400821 /*
822 * regular read from a file
823 */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000824 bufsize = BUF_DELTA;
825 buffer = nasm_malloc(BUF_DELTA);
826 p = buffer;
H. Peter Anvin9f394642002-04-30 21:07:51 +0000827 continued_count = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000828 while (1) {
829 q = fgets(p, bufsize - (p - buffer), istk->fp);
830 if (!q)
831 break;
832 p += strlen(p);
833 if (p > buffer && p[-1] == '\n') {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300834 /*
835 * Convert backslash-CRLF line continuation sequences into
836 * nothing at all (for DOS and Windows)
837 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000838 if (((p - 2) > buffer) && (p[-3] == '\\') && (p[-2] == '\r')) {
839 p -= 3;
840 *p = 0;
841 continued_count++;
842 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300843 /*
844 * Also convert backslash-LF line continuation sequences into
845 * nothing at all (for Unix)
846 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000847 else if (((p - 1) > buffer) && (p[-2] == '\\')) {
848 p -= 2;
849 *p = 0;
850 continued_count++;
851 } else {
852 break;
853 }
854 }
855 if (p - buffer > bufsize - 10) {
Keith Kaniosb7a89542007-04-12 02:40:54 +0000856 int32_t offset = p - buffer;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000857 bufsize += BUF_DELTA;
858 buffer = nasm_realloc(buffer, bufsize);
859 p = buffer + offset; /* prevent stale-pointer problems */
860 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000861 }
862
H. Peter Anvine2c80182005-01-15 22:15:51 +0000863 if (!q && p == buffer) {
864 nasm_free(buffer);
865 return NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000866 }
867
Cyrill Gorcunova5aea572010-11-11 10:14:45 +0300868 src_set_linnum(src_get_linnum() + istk->lineinc +
869 (continued_count * istk->lineinc));
H. Peter Anvineba20a72002-04-30 20:53:55 +0000870
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000871 /*
872 * Play safe: remove CRs as well as LFs, if any of either are
873 * present at the end of the line.
874 */
H. Peter Anvineba20a72002-04-30 20:53:55 +0000875 while (--p >= buffer && (*p == '\n' || *p == '\r'))
H. Peter Anvine2c80182005-01-15 22:15:51 +0000876 *p = '\0';
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000877
878 /*
879 * Handle spurious ^Z, which may be inserted into source files
880 * by some file transfer utilities.
881 */
882 buffer[strcspn(buffer, "\032")] = '\0';
883
H. Peter Anvin734b1882002-04-30 21:01:08 +0000884 list->line(LIST_READ, buffer);
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000885
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000886 return buffer;
887}
888
889/*
Keith Kaniosb7a89542007-04-12 02:40:54 +0000890 * Tokenize a line of text. This is a very simple process since we
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000891 * don't need to parse the value out of e.g. numeric tokens: we
892 * simply split one string into many.
893 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000894static Token *tokenize(char *line)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000895{
H. Peter Anvinca544db2008-10-19 19:30:11 -0700896 char c, *p = line;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +0000897 enum pp_token_type type;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000898 Token *list = NULL;
899 Token *t, **tail = &list;
900
H. Peter Anvine2c80182005-01-15 22:15:51 +0000901 while (*line) {
902 p = line;
903 if (*p == '%') {
904 p++;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300905 if (*p == '+' && !nasm_isdigit(p[1])) {
906 p++;
907 type = TOK_PASTE;
908 } else if (nasm_isdigit(*p) ||
909 ((*p == '-' || *p == '+') && nasm_isdigit(p[1]))) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000910 do {
911 p++;
912 }
H. Peter Anvinbda7a6e2008-06-21 10:23:17 -0700913 while (nasm_isdigit(*p));
H. Peter Anvine2c80182005-01-15 22:15:51 +0000914 type = TOK_PREPROC_ID;
915 } else if (*p == '{') {
916 p++;
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800917 while (*p) {
918 if (*p == '}')
919 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000920 p[-1] = *p;
921 p++;
922 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800923 if (*p != '}')
924 error(ERR_WARNING | ERR_PASS1, "unterminated %{ construct");
H. Peter Anvine2c80182005-01-15 22:15:51 +0000925 p[-1] = '\0';
926 if (*p)
927 p++;
928 type = TOK_PREPROC_ID;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300929 } else if (*p == '[') {
930 int lvl = 1;
931 line += 2; /* Skip the leading %[ */
932 p++;
933 while (lvl && (c = *p++)) {
934 switch (c) {
935 case ']':
936 lvl--;
937 break;
938 case '%':
939 if (*p == '[')
940 lvl++;
941 break;
942 case '\'':
943 case '\"':
944 case '`':
Cyrill Gorcunovc6360a72010-07-13 13:32:19 +0400945 p = nasm_skip_string(p - 1) + 1;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300946 break;
947 default:
948 break;
949 }
950 }
951 p--;
952 if (*p)
953 *p++ = '\0';
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800954 if (lvl)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300955 error(ERR_NONFATAL, "unterminated %[ construct");
956 type = TOK_INDIRECT;
957 } else if (*p == '?') {
958 type = TOK_PREPROC_Q; /* %? */
959 p++;
960 if (*p == '?') {
961 type = TOK_PREPROC_QQ; /* %?? */
962 p++;
963 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800964 } else if (*p == '!') {
965 type = TOK_PREPROC_ID;
966 p++;
967 if (isidchar(*p)) {
968 do {
969 p++;
970 }
971 while (isidchar(*p));
972 } else if (*p == '\'' || *p == '\"' || *p == '`') {
973 p = nasm_skip_string(p);
974 if (*p)
975 p++;
976 else
977 error(ERR_NONFATAL|ERR_PASS1, "unterminated %! string");
978 } else {
979 /* %! without string or identifier */
980 type = TOK_OTHER; /* Legacy behavior... */
981 }
H. Peter Anvine2c80182005-01-15 22:15:51 +0000982 } else if (isidchar(*p) ||
983 ((*p == '!' || *p == '%' || *p == '$') &&
984 isidchar(p[1]))) {
985 do {
986 p++;
987 }
988 while (isidchar(*p));
989 type = TOK_PREPROC_ID;
990 } else {
991 type = TOK_OTHER;
992 if (*p == '%')
993 p++;
994 }
995 } else if (isidstart(*p) || (*p == '$' && isidstart(p[1]))) {
996 type = TOK_ID;
997 p++;
998 while (*p && isidchar(*p))
999 p++;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07001000 } else if (*p == '\'' || *p == '"' || *p == '`') {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001001 /*
1002 * A string token.
1003 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001004 type = TOK_STRING;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001005 p = nasm_skip_string(p);
Nickolay Yurchenkof3b3ce22003-09-21 20:38:43 +00001006
H. Peter Anvine2c80182005-01-15 22:15:51 +00001007 if (*p) {
1008 p++;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001009 } else {
H. Peter Anvin917a3492008-09-24 09:14:49 -07001010 error(ERR_WARNING|ERR_PASS1, "unterminated string");
H. Peter Anvine2c80182005-01-15 22:15:51 +00001011 /* Handling unterminated strings by UNV */
1012 /* type = -1; */
1013 }
Victor van den Elzenfb5f2512009-04-17 16:17:59 +02001014 } else if (p[0] == '$' && p[1] == '$') {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001015 type = TOK_OTHER; /* TOKEN_BASE */
Victor van den Elzenfb5f2512009-04-17 16:17:59 +02001016 p += 2;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001017 } else if (isnumstart(*p)) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001018 bool is_hex = false;
1019 bool is_float = false;
1020 bool has_e = false;
1021 char c, *r;
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001022
H. Peter Anvine2c80182005-01-15 22:15:51 +00001023 /*
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001024 * A numeric token.
H. Peter Anvine2c80182005-01-15 22:15:51 +00001025 */
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001026
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001027 if (*p == '$') {
1028 p++;
1029 is_hex = true;
1030 }
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001031
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001032 for (;;) {
1033 c = *p++;
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001034
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001035 if (!is_hex && (c == 'e' || c == 'E')) {
1036 has_e = true;
1037 if (*p == '+' || *p == '-') {
1038 /*
1039 * e can only be followed by +/- if it is either a
1040 * prefixed hex number or a floating-point number
1041 */
1042 p++;
1043 is_float = true;
1044 }
1045 } else if (c == 'H' || c == 'h' || c == 'X' || c == 'x') {
1046 is_hex = true;
1047 } else if (c == 'P' || c == 'p') {
1048 is_float = true;
1049 if (*p == '+' || *p == '-')
1050 p++;
1051 } else if (isnumchar(c) || c == '_')
1052 ; /* just advance */
1053 else if (c == '.') {
1054 /*
1055 * we need to deal with consequences of the legacy
1056 * parser, like "1.nolist" being two tokens
1057 * (TOK_NUMBER, TOK_ID) here; at least give it
1058 * a shot for now. In the future, we probably need
1059 * a flex-based scanner with proper pattern matching
1060 * to do it as well as it can be done. Nothing in
1061 * the world is going to help the person who wants
1062 * 0x123.p16 interpreted as two tokens, though.
1063 */
1064 r = p;
1065 while (*r == '_')
1066 r++;
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001067
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001068 if (nasm_isdigit(*r) || (is_hex && nasm_isxdigit(*r)) ||
1069 (!is_hex && (*r == 'e' || *r == 'E')) ||
1070 (*r == 'p' || *r == 'P')) {
1071 p = r;
1072 is_float = true;
1073 } else
1074 break; /* Terminate the token */
1075 } else
1076 break;
1077 }
1078 p--; /* Point to first character beyond number */
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001079
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001080 if (p == line+1 && *line == '$') {
1081 type = TOK_OTHER; /* TOKEN_HERE */
1082 } else {
1083 if (has_e && !is_hex) {
1084 /* 1e13 is floating-point, but 1e13h is not */
1085 is_float = true;
1086 }
H. Peter Anvind784a082009-04-20 14:01:18 -07001087
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001088 type = is_float ? TOK_FLOAT : TOK_NUMBER;
1089 }
H. Peter Anvinbda7a6e2008-06-21 10:23:17 -07001090 } else if (nasm_isspace(*p)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001091 type = TOK_WHITESPACE;
Cyrill Gorcunovf66ac7d2009-10-12 20:41:13 +04001092 p = nasm_skip_spaces(p);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001093 /*
1094 * Whitespace just before end-of-line is discarded by
1095 * pretending it's a comment; whitespace just before a
1096 * comment gets lumped into the comment.
1097 */
1098 if (!*p || *p == ';') {
1099 type = TOK_COMMENT;
1100 while (*p)
1101 p++;
1102 }
1103 } else if (*p == ';') {
1104 type = TOK_COMMENT;
1105 while (*p)
1106 p++;
1107 } else {
1108 /*
1109 * Anything else is an operator of some kind. We check
1110 * for all the double-character operators (>>, <<, //,
1111 * %%, <=, >=, ==, !=, <>, &&, ||, ^^), but anything
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001112 * else is a single-character operator.
H. Peter Anvine2c80182005-01-15 22:15:51 +00001113 */
1114 type = TOK_OTHER;
1115 if ((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[0] == '^' && p[1] == '^')) {
1126 p++;
1127 }
1128 p++;
1129 }
Nickolay Yurchenkof3b3ce22003-09-21 20:38:43 +00001130
H. Peter Anvine2c80182005-01-15 22:15:51 +00001131 /* Handling unterminated string by UNV */
1132 /*if (type == -1)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001133 {
1134 *tail = t = new_Token(NULL, TOK_STRING, line, p-line+1);
1135 t->text[p-line] = *line;
1136 tail = &t->next;
1137 }
1138 else */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001139 if (type != TOK_COMMENT) {
1140 *tail = t = new_Token(NULL, type, line, p - line);
1141 tail = &t->next;
1142 }
1143 line = p;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001144 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001145 return list;
1146}
1147
H. Peter Anvince616072002-04-30 21:02:23 +00001148/*
1149 * this function allocates a new managed block of memory and
H. Peter Anvin70653092007-10-19 14:42:29 -07001150 * returns a pointer to the block. The managed blocks are
H. Peter Anvince616072002-04-30 21:02:23 +00001151 * deleted only all at once by the delete_Blocks function.
1152 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001153static void *new_Block(size_t size)
H. Peter Anvince616072002-04-30 21:02:23 +00001154{
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001155 Blocks *b = &blocks;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001156
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001157 /* first, get to the end of the linked list */
1158 while (b->next)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001159 b = b->next;
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001160 /* now allocate the requested chunk */
1161 b->chunk = nasm_malloc(size);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001162
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001163 /* now allocate a new block for the next request */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001164 b->next = nasm_malloc(sizeof(Blocks));
1165 /* and initialize the contents of the new block */
1166 b->next->next = NULL;
1167 b->next->chunk = NULL;
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001168 return b->chunk;
H. Peter Anvince616072002-04-30 21:02:23 +00001169}
1170
1171/*
1172 * this function deletes all managed blocks of memory
1173 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001174static void delete_Blocks(void)
H. Peter Anvince616072002-04-30 21:02:23 +00001175{
H. Peter Anvine2c80182005-01-15 22:15:51 +00001176 Blocks *a, *b = &blocks;
H. Peter Anvince616072002-04-30 21:02:23 +00001177
H. Peter Anvin70653092007-10-19 14:42:29 -07001178 /*
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001179 * keep in mind that the first block, pointed to by blocks
H. Peter Anvin70653092007-10-19 14:42:29 -07001180 * is a static and not dynamically allocated, so we don't
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001181 * free it.
1182 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001183 while (b) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001184 if (b->chunk)
1185 nasm_free(b->chunk);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001186 a = b;
1187 b = b->next;
1188 if (a != &blocks)
1189 nasm_free(a);
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001190 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001191}
H. Peter Anvin734b1882002-04-30 21:01:08 +00001192
1193/*
H. Peter Anvin70653092007-10-19 14:42:29 -07001194 * this function creates a new Token and passes a pointer to it
H. Peter Anvin734b1882002-04-30 21:01:08 +00001195 * back to the caller. It sets the type and text elements, and
H. Peter Anvinf26e0972008-07-01 21:26:27 -07001196 * also the a.mac and next elements to NULL.
H. Peter Anvin734b1882002-04-30 21:01:08 +00001197 */
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07001198static Token *new_Token(Token * next, enum pp_token_type type,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001199 const char *text, int txtlen)
H. Peter Anvin734b1882002-04-30 21:01:08 +00001200{
1201 Token *t;
1202 int i;
1203
H. Peter Anvin89cee572009-07-15 09:16:54 -04001204 if (!freeTokens) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001205 freeTokens = (Token *) new_Block(TOKEN_BLOCKSIZE * sizeof(Token));
1206 for (i = 0; i < TOKEN_BLOCKSIZE - 1; i++)
1207 freeTokens[i].next = &freeTokens[i + 1];
1208 freeTokens[i].next = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001209 }
1210 t = freeTokens;
1211 freeTokens = t->next;
1212 t->next = next;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07001213 t->a.mac = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001214 t->type = type;
H. Peter Anvin89cee572009-07-15 09:16:54 -04001215 if (type == TOK_WHITESPACE || !text) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001216 t->text = NULL;
1217 } else {
1218 if (txtlen == 0)
1219 txtlen = strlen(text);
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07001220 t->text = nasm_malloc(txtlen+1);
1221 memcpy(t->text, text, txtlen);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001222 t->text[txtlen] = '\0';
H. Peter Anvin734b1882002-04-30 21:01:08 +00001223 }
1224 return t;
1225}
1226
H. Peter Anvine2c80182005-01-15 22:15:51 +00001227static Token *delete_Token(Token * t)
H. Peter Anvin734b1882002-04-30 21:01:08 +00001228{
1229 Token *next = t->next;
1230 nasm_free(t->text);
H. Peter Anvin788e6c12002-04-30 21:02:01 +00001231 t->next = freeTokens;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001232 freeTokens = t;
1233 return next;
1234}
1235
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001236/*
1237 * Convert a line of tokens back into text.
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001238 * If expand_locals is not zero, identifiers of the form "%$*xxx"
1239 * will be transformed into ..@ctxnum.xxx
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001240 */
H. Peter Anvin9e200162008-06-04 17:23:14 -07001241static char *detoken(Token * tlist, bool expand_locals)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001242{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001243 Token *t;
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001244 char *line, *p;
H. Peter Anvinb4daadc2008-01-21 16:31:57 -08001245 const char *q;
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001246 int len = 0;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001247
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001248 list_for_each(t, tlist) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001249 if (t->type == TOK_PREPROC_ID && t->text[1] == '!') {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001250 char *v;
1251 char *q = t->text;
H. Peter Anvin077fb932010-07-20 14:56:30 -07001252
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001253 v = t->text + 2;
1254 if (*v == '\'' || *v == '\"' || *v == '`') {
1255 size_t len = nasm_unquote(v, NULL);
1256 size_t clen = strlen(v);
H. Peter Anvin077fb932010-07-20 14:56:30 -07001257
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001258 if (len != clen) {
1259 error(ERR_NONFATAL | ERR_PASS1,
1260 "NUL character in %! string");
1261 v = NULL;
1262 }
1263 }
H. Peter Anvin077fb932010-07-20 14:56:30 -07001264
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001265 if (v) {
1266 char *p = getenv(v);
1267 if (!p) {
1268 error(ERR_NONFATAL | ERR_PASS1,
1269 "nonexistent environment variable `%s'", v);
1270 p = "";
1271 }
1272 t->text = nasm_strdup(p);
1273 }
1274 nasm_free(q);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001275 }
H. Peter Anvin077fb932010-07-20 14:56:30 -07001276
H. Peter Anvine2c80182005-01-15 22:15:51 +00001277 /* Expand local macros here and not during preprocessing */
1278 if (expand_locals &&
1279 t->type == TOK_PREPROC_ID && t->text &&
1280 t->text[0] == '%' && t->text[1] == '$') {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001281 const char *q;
1282 char *p;
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04001283 Context *ctx = get_ctx(t->text, &q);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001284 if (ctx) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001285 char buffer[40];
Keith Kanios93f2e9a2007-04-14 00:10:59 +00001286 snprintf(buffer, sizeof(buffer), "..@%"PRIu32".", ctx->number);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001287 p = nasm_strcat(buffer, q);
1288 nasm_free(t->text);
1289 t->text = p;
1290 }
1291 }
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001292 if (t->type == TOK_WHITESPACE)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001293 len++;
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001294 else if (t->text)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001295 len += strlen(t->text);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001296 }
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001297
H. Peter Anvin734b1882002-04-30 21:01:08 +00001298 p = line = nasm_malloc(len + 1);
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001299
1300 list_for_each(t, tlist) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001301 if (t->type == TOK_WHITESPACE) {
H. Peter Anvinb4daadc2008-01-21 16:31:57 -08001302 *p++ = ' ';
H. Peter Anvine2c80182005-01-15 22:15:51 +00001303 } else if (t->text) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001304 q = t->text;
1305 while (*q)
1306 *p++ = *q++;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001307 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001308 }
1309 *p = '\0';
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001310
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001311 return line;
1312}
1313
1314/*
H. Peter Anvin76690a12002-04-30 20:52:49 +00001315 * A scanner, suitable for use by the expression evaluator, which
1316 * operates on a line of Tokens. Expects a pointer to a pointer to
1317 * the first token in the line to be passed in as its private_data
1318 * field.
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001319 *
1320 * FIX: This really needs to be unified with stdscan.
H. Peter Anvin76690a12002-04-30 20:52:49 +00001321 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001322static int ppscan(void *private_data, struct tokenval *tokval)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001323{
H. Peter Anvin76690a12002-04-30 20:52:49 +00001324 Token **tlineptr = private_data;
1325 Token *tline;
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001326 char ourcopy[MAX_KEYWORD+1], *p, *r, *s;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001327
H. Peter Anvine2c80182005-01-15 22:15:51 +00001328 do {
1329 tline = *tlineptr;
1330 *tlineptr = tline ? tline->next : NULL;
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04001331 } while (tline && (tline->type == TOK_WHITESPACE ||
1332 tline->type == TOK_COMMENT));
H. Peter Anvin76690a12002-04-30 20:52:49 +00001333
1334 if (!tline)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001335 return tokval->t_type = TOKEN_EOS;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001336
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001337 tokval->t_charptr = tline->text;
1338
H. Peter Anvin76690a12002-04-30 20:52:49 +00001339 if (tline->text[0] == '$' && !tline->text[1])
H. Peter Anvine2c80182005-01-15 22:15:51 +00001340 return tokval->t_type = TOKEN_HERE;
H. Peter Anvin7cf897e2002-05-30 21:30:33 +00001341 if (tline->text[0] == '$' && tline->text[1] == '$' && !tline->text[2])
H. Peter Anvine2c80182005-01-15 22:15:51 +00001342 return tokval->t_type = TOKEN_BASE;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001343
H. Peter Anvine2c80182005-01-15 22:15:51 +00001344 if (tline->type == TOK_ID) {
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001345 p = tokval->t_charptr = tline->text;
1346 if (p[0] == '$') {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001347 tokval->t_charptr++;
1348 return tokval->t_type = TOKEN_ID;
1349 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00001350
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001351 for (r = p, s = ourcopy; *r; r++) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001352 if (r >= p+MAX_KEYWORD)
1353 return tokval->t_type = TOKEN_ID; /* Not a keyword */
H. Peter Anvinac8f8fc2008-06-11 15:49:41 -07001354 *s++ = nasm_tolower(*r);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001355 }
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001356 *s = '\0';
1357 /* right, so we have an identifier sitting in temp storage. now,
1358 * is it actually a register or instruction name, or what? */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001359 return nasm_token_hash(ourcopy, tokval);
H. Peter Anvin76690a12002-04-30 20:52:49 +00001360 }
1361
H. Peter Anvine2c80182005-01-15 22:15:51 +00001362 if (tline->type == TOK_NUMBER) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001363 bool rn_error;
1364 tokval->t_integer = readnum(tline->text, &rn_error);
1365 tokval->t_charptr = tline->text;
1366 if (rn_error)
1367 return tokval->t_type = TOKEN_ERRNUM;
1368 else
1369 return tokval->t_type = TOKEN_NUM;
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001370 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00001371
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001372 if (tline->type == TOK_FLOAT) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001373 return tokval->t_type = TOKEN_FLOAT;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001374 }
1375
H. Peter Anvine2c80182005-01-15 22:15:51 +00001376 if (tline->type == TOK_STRING) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001377 char bq, *ep;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001378
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001379 bq = tline->text[0];
H. Peter Anvin11627042008-06-09 20:45:19 -07001380 tokval->t_charptr = tline->text;
1381 tokval->t_inttwo = nasm_unquote(tline->text, &ep);
H. Peter Anvind2456592008-06-19 15:04:18 -07001382
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001383 if (ep[0] != bq || ep[1] != '\0')
1384 return tokval->t_type = TOKEN_ERRSTR;
1385 else
1386 return tokval->t_type = TOKEN_STR;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001387 }
1388
H. Peter Anvine2c80182005-01-15 22:15:51 +00001389 if (tline->type == TOK_OTHER) {
1390 if (!strcmp(tline->text, "<<"))
1391 return tokval->t_type = TOKEN_SHL;
1392 if (!strcmp(tline->text, ">>"))
1393 return tokval->t_type = TOKEN_SHR;
1394 if (!strcmp(tline->text, "//"))
1395 return tokval->t_type = TOKEN_SDIV;
1396 if (!strcmp(tline->text, "%%"))
1397 return tokval->t_type = TOKEN_SMOD;
1398 if (!strcmp(tline->text, "=="))
1399 return tokval->t_type = TOKEN_EQ;
1400 if (!strcmp(tline->text, "<>"))
1401 return tokval->t_type = TOKEN_NE;
1402 if (!strcmp(tline->text, "!="))
1403 return tokval->t_type = TOKEN_NE;
1404 if (!strcmp(tline->text, "<="))
1405 return tokval->t_type = TOKEN_LE;
1406 if (!strcmp(tline->text, ">="))
1407 return tokval->t_type = TOKEN_GE;
1408 if (!strcmp(tline->text, "&&"))
1409 return tokval->t_type = TOKEN_DBL_AND;
1410 if (!strcmp(tline->text, "^^"))
1411 return tokval->t_type = TOKEN_DBL_XOR;
1412 if (!strcmp(tline->text, "||"))
1413 return tokval->t_type = TOKEN_DBL_OR;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001414 }
1415
1416 /*
1417 * We have no other options: just return the first character of
1418 * the token text.
1419 */
1420 return tokval->t_type = tline->text[0];
1421}
1422
1423/*
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001424 * Compare a string to the name of an existing macro; this is a
1425 * simple wrapper which calls either strcmp or nasm_stricmp
1426 * depending on the value of the `casesense' parameter.
1427 */
H. Peter Anvin4db5a162007-10-11 13:42:09 -07001428static int mstrcmp(const char *p, const char *q, bool casesense)
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001429{
H. Peter Anvin734b1882002-04-30 21:01:08 +00001430 return casesense ? strcmp(p, q) : nasm_stricmp(p, q);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001431}
1432
1433/*
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07001434 * Compare a string to the name of an existing macro; this is a
1435 * simple wrapper which calls either strcmp or nasm_stricmp
1436 * depending on the value of the `casesense' parameter.
1437 */
1438static int mmemcmp(const char *p, const char *q, size_t l, bool casesense)
1439{
1440 return casesense ? memcmp(p, q, l) : nasm_memicmp(p, q, l);
1441}
1442
1443/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001444 * Return the Context structure associated with a %$ token. Return
1445 * NULL, having _already_ reported an error condition, if the
1446 * context stack isn't deep enough for the supplied number of $
1447 * signs.
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001448 *
1449 * If "namep" is non-NULL, set it to the pointer to the macro name
1450 * tail, i.e. the part beyond %$...
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001451 */
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04001452static Context *get_ctx(const char *name, const char **namep)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001453{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001454 Context *ctx;
1455 int i;
1456
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001457 if (namep)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001458 *namep = name;
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001459
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001460 if (!name || name[0] != '%' || name[1] != '$')
H. Peter Anvine2c80182005-01-15 22:15:51 +00001461 return NULL;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001462
H. Peter Anvine2c80182005-01-15 22:15:51 +00001463 if (!cstk) {
1464 error(ERR_NONFATAL, "`%s': context stack is empty", name);
1465 return NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001466 }
1467
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001468 name += 2;
1469 ctx = cstk;
1470 i = 0;
1471 while (ctx && *name == '$') {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001472 name++;
1473 i++;
1474 ctx = ctx->next;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001475 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001476 if (!ctx) {
1477 error(ERR_NONFATAL, "`%s': context stack is only"
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001478 " %d level%s deep", name, i, (i == 1 ? "" : "s"));
H. Peter Anvine2c80182005-01-15 22:15:51 +00001479 return NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001480 }
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001481
1482 if (namep)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001483 *namep = name;
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001484
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04001485 return ctx;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001486}
1487
1488/*
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001489 * Check to see if a file is already in a string list
1490 */
1491static bool in_list(const StrList *list, const char *str)
1492{
1493 while (list) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001494 if (!strcmp(list->str, str))
1495 return true;
1496 list = list->next;
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001497 }
1498 return false;
1499}
1500
1501/*
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001502 * Open an include file. This routine must always return a valid
1503 * file pointer if it returns - it's responsible for throwing an
1504 * ERR_FATAL and bombing out completely if not. It should also try
1505 * the include path one by one until it finds the file or reaches
1506 * the end of the path.
1507 */
H. Peter Anvin2b1c3b92008-06-06 10:38:46 -07001508static FILE *inc_fopen(const char *file, StrList **dhead, StrList ***dtail,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001509 bool missing_ok)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001510{
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001511 FILE *fp;
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001512 char *prefix = "";
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001513 IncPath *ip = ipath;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001514 int len = strlen(file);
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001515 size_t prefix_len = 0;
1516 StrList *sl;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001517
H. Peter Anvine2c80182005-01-15 22:15:51 +00001518 while (1) {
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001519 sl = nasm_malloc(prefix_len+len+1+sizeof sl->next);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001520 memcpy(sl->str, prefix, prefix_len);
1521 memcpy(sl->str+prefix_len, file, len+1);
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001522 fp = fopen(sl->str, "r");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001523 if (fp && dhead && !in_list(*dhead, sl->str)) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001524 sl->next = NULL;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001525 **dtail = sl;
1526 *dtail = &sl->next;
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001527 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001528 nasm_free(sl);
1529 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001530 if (fp)
1531 return fp;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001532 if (!ip) {
1533 if (!missing_ok)
1534 break;
1535 prefix = NULL;
1536 } else {
1537 prefix = ip->path;
1538 ip = ip->next;
1539 }
1540 if (prefix) {
1541 prefix_len = strlen(prefix);
1542 } else {
1543 /* -MG given and file not found */
1544 if (dhead && !in_list(*dhead, file)) {
1545 sl = nasm_malloc(len+1+sizeof sl->next);
1546 sl->next = NULL;
1547 strcpy(sl->str, file);
1548 **dtail = sl;
1549 *dtail = &sl->next;
1550 }
1551 return NULL;
1552 }
H. Peter Anvineba20a72002-04-30 20:53:55 +00001553 }
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001554
H. Peter Anvin734b1882002-04-30 21:01:08 +00001555 error(ERR_FATAL, "unable to open include file `%s'", file);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001556 return NULL;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001557}
1558
1559/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001560 * Determine if we should warn on defining a single-line macro of
H. Peter Anvinef7468f2002-04-30 20:57:59 +00001561 * name `name', with `nparam' parameters. If nparam is 0 or -1, will
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001562 * return true if _any_ single-line macro of that name is defined.
1563 * Otherwise, will return true if a single-line macro with either
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001564 * `nparam' or no parameters is defined.
1565 *
1566 * If a macro with precisely the right number of parameters is
H. Peter Anvinef7468f2002-04-30 20:57:59 +00001567 * defined, or nparam is -1, the address of the definition structure
1568 * will be returned in `defn'; otherwise NULL will be returned. If `defn'
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001569 * is NULL, no action will be taken regarding its contents, and no
1570 * error will occur.
1571 *
1572 * Note that this is also called with nparam zero to resolve
1573 * `ifdef'.
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001574 *
1575 * If you already know which context macro belongs to, you can pass
1576 * the context pointer as first parameter; if you won't but name begins
1577 * with %$ the context will be automatically computed. If all_contexts
1578 * is true, macro will be searched in outer contexts as well.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001579 */
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07001580static bool
H. Peter Anvinb2a5fda2008-06-19 21:42:42 -07001581smacro_defined(Context * ctx, const char *name, int nparam, SMacro ** defn,
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07001582 bool nocase)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001583{
H. Peter Anvin166c2472008-05-28 12:28:58 -07001584 struct hash_table *smtbl;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001585 SMacro *m;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001586
H. Peter Anvin97a23472007-09-16 17:57:25 -07001587 if (ctx) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001588 smtbl = &ctx->localmac;
H. Peter Anvin97a23472007-09-16 17:57:25 -07001589 } else if (name[0] == '%' && name[1] == '$') {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001590 if (cstk)
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04001591 ctx = get_ctx(name, &name);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001592 if (!ctx)
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001593 return false; /* got to return _something_ */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001594 smtbl = &ctx->localmac;
H. Peter Anvin97a23472007-09-16 17:57:25 -07001595 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001596 smtbl = &smacros;
H. Peter Anvin97a23472007-09-16 17:57:25 -07001597 }
H. Peter Anvin166c2472008-05-28 12:28:58 -07001598 m = (SMacro *) hash_findix(smtbl, name);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001599
H. Peter Anvine2c80182005-01-15 22:15:51 +00001600 while (m) {
1601 if (!mstrcmp(m->name, name, m->casesense && nocase) &&
Charles Crayne192d5b52007-10-18 19:02:42 -07001602 (nparam <= 0 || m->nparam == 0 || nparam == (int) m->nparam)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001603 if (defn) {
Charles Crayne192d5b52007-10-18 19:02:42 -07001604 if (nparam == (int) m->nparam || nparam == -1)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001605 *defn = m;
1606 else
1607 *defn = NULL;
1608 }
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001609 return true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001610 }
1611 m = m->next;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001612 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001613
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001614 return false;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001615}
1616
1617/*
1618 * Count and mark off the parameters in a multi-line macro call.
1619 * This is called both from within the multi-line macro expansion
1620 * code, and also to mark off the default parameters when provided
1621 * in a %macro definition line.
1622 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001623static void count_mmac_params(Token * t, int *nparam, Token *** params)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001624{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001625 int paramsize, brace;
1626
1627 *nparam = paramsize = 0;
1628 *params = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001629 while (t) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001630 /* +1: we need space for the final NULL */
H. Peter Anvin91fb6f12008-09-01 10:56:33 -07001631 if (*nparam+1 >= paramsize) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001632 paramsize += PARAM_DELTA;
1633 *params = nasm_realloc(*params, sizeof(**params) * paramsize);
1634 }
1635 skip_white_(t);
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001636 brace = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001637 if (tok_is_(t, "{"))
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001638 brace = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001639 (*params)[(*nparam)++] = t;
1640 while (tok_isnt_(t, brace ? "}" : ","))
1641 t = t->next;
1642 if (t) { /* got a comma/brace */
1643 t = t->next;
1644 if (brace) {
1645 /*
1646 * Now we've found the closing brace, look further
1647 * for the comma.
1648 */
1649 skip_white_(t);
1650 if (tok_isnt_(t, ",")) {
1651 error(ERR_NONFATAL,
1652 "braces do not enclose all of macro parameter");
1653 while (tok_isnt_(t, ","))
1654 t = t->next;
1655 }
1656 if (t)
1657 t = t->next; /* eat the comma */
1658 }
1659 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001660 }
1661}
1662
1663/*
H. Peter Anvin76690a12002-04-30 20:52:49 +00001664 * Determine whether one of the various `if' conditions is true or
1665 * not.
1666 *
1667 * We must free the tline we get passed.
1668 */
H. Peter Anvin70055962007-10-11 00:05:31 -07001669static bool if_condition(Token * tline, enum preproc_token ct)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001670{
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001671 enum pp_conditional i = PP_COND(ct);
H. Peter Anvin70055962007-10-11 00:05:31 -07001672 bool j;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001673 Token *t, *tt, **tptr, *origline;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001674 struct tokenval tokval;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001675 expr *evalresult;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001676 enum pp_token_type needtype;
H. Peter Anvin077fb932010-07-20 14:56:30 -07001677 char *p;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001678
1679 origline = tline;
1680
H. Peter Anvine2c80182005-01-15 22:15:51 +00001681 switch (i) {
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001682 case PPC_IFCTX:
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001683 j = false; /* have we matched yet? */
Victor van den Elzen0e857f12008-07-23 13:21:29 +02001684 while (true) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001685 skip_white_(tline);
Victor van den Elzen0e857f12008-07-23 13:21:29 +02001686 if (!tline)
1687 break;
1688 if (tline->type != TOK_ID) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001689 error(ERR_NONFATAL,
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001690 "`%s' expects context identifiers", pp_directives[ct]);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001691 free_tlist(origline);
1692 return -1;
1693 }
Victor van den Elzen0e857f12008-07-23 13:21:29 +02001694 if (cstk && cstk->name && !nasm_stricmp(tline->text, cstk->name))
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001695 j = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001696 tline = tline->next;
1697 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001698 break;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001699
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001700 case PPC_IFDEF:
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001701 j = false; /* have we matched yet? */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001702 while (tline) {
1703 skip_white_(tline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001704 if (!tline || (tline->type != TOK_ID &&
1705 (tline->type != TOK_PREPROC_ID ||
1706 tline->text[1] != '$'))) {
1707 error(ERR_NONFATAL,
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001708 "`%s' expects macro identifiers", pp_directives[ct]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001709 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001710 }
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07001711 if (smacro_defined(NULL, tline->text, 0, NULL, true))
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001712 j = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001713 tline = tline->next;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001714 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001715 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001716
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001717 case PPC_IFENV:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001718 tline = expand_smacro(tline);
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001719 j = false; /* have we matched yet? */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001720 while (tline) {
1721 skip_white_(tline);
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001722 if (!tline || (tline->type != TOK_ID &&
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001723 tline->type != TOK_STRING &&
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001724 (tline->type != TOK_PREPROC_ID ||
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001725 tline->text[1] != '!'))) {
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001726 error(ERR_NONFATAL,
1727 "`%s' expects environment variable names",
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001728 pp_directives[ct]);
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001729 goto fail;
1730 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001731 p = tline->text;
1732 if (tline->type == TOK_PREPROC_ID)
1733 p += 2; /* Skip leading %! */
1734 if (*p == '\'' || *p == '\"' || *p == '`')
1735 nasm_unquote_cstr(p, ct);
1736 if (getenv(p))
1737 j = true;
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001738 tline = tline->next;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001739 }
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001740 break;
1741
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001742 case PPC_IFIDN:
1743 case PPC_IFIDNI:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001744 tline = expand_smacro(tline);
1745 t = tt = tline;
1746 while (tok_isnt_(tt, ","))
1747 tt = tt->next;
1748 if (!tt) {
1749 error(ERR_NONFATAL,
1750 "`%s' expects two comma-separated arguments",
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001751 pp_directives[ct]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001752 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001753 }
1754 tt = tt->next;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001755 j = true; /* assume equality unless proved not */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001756 while ((t->type != TOK_OTHER || strcmp(t->text, ",")) && tt) {
1757 if (tt->type == TOK_OTHER && !strcmp(tt->text, ",")) {
1758 error(ERR_NONFATAL, "`%s': more than one comma on line",
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001759 pp_directives[ct]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001760 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001761 }
1762 if (t->type == TOK_WHITESPACE) {
1763 t = t->next;
1764 continue;
1765 }
1766 if (tt->type == TOK_WHITESPACE) {
1767 tt = tt->next;
1768 continue;
1769 }
1770 if (tt->type != t->type) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001771 j = false; /* found mismatching tokens */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001772 break;
1773 }
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07001774 /* When comparing strings, need to unquote them first */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001775 if (t->type == TOK_STRING) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001776 size_t l1 = nasm_unquote(t->text, NULL);
1777 size_t l2 = nasm_unquote(tt->text, NULL);
H. Peter Anvind2456592008-06-19 15:04:18 -07001778
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001779 if (l1 != l2) {
1780 j = false;
1781 break;
1782 }
1783 if (mmemcmp(t->text, tt->text, l1, i == PPC_IFIDN)) {
1784 j = false;
1785 break;
1786 }
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07001787 } else if (mstrcmp(tt->text, t->text, i == PPC_IFIDN) != 0) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001788 j = false; /* found mismatching tokens */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001789 break;
1790 }
Nickolay Yurchenkof3b3ce22003-09-21 20:38:43 +00001791
H. Peter Anvine2c80182005-01-15 22:15:51 +00001792 t = t->next;
1793 tt = tt->next;
1794 }
1795 if ((t->type != TOK_OTHER || strcmp(t->text, ",")) || tt)
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001796 j = false; /* trailing gunk on one end or other */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001797 break;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001798
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001799 case PPC_IFMACRO:
H. Peter Anvin89cee572009-07-15 09:16:54 -04001800 {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001801 bool found = false;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001802 MMacro searching, *mmac;
H. Peter Anvin65747262002-05-07 00:10:05 +00001803
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001804 skip_white_(tline);
1805 tline = expand_id(tline);
1806 if (!tok_type_(tline, TOK_ID)) {
1807 error(ERR_NONFATAL,
1808 "`%s' expects a macro name", pp_directives[ct]);
1809 goto fail;
1810 }
1811 searching.name = nasm_strdup(tline->text);
1812 searching.casesense = true;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001813 searching.plus = false;
1814 searching.nolist = false;
1815 searching.in_progress = 0;
1816 searching.max_depth = 0;
1817 searching.rep_nest = NULL;
1818 searching.nparam_min = 0;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001819 searching.nparam_max = INT_MAX;
1820 tline = expand_smacro(tline->next);
1821 skip_white_(tline);
1822 if (!tline) {
1823 } else if (!tok_type_(tline, TOK_NUMBER)) {
1824 error(ERR_NONFATAL,
1825 "`%s' expects a parameter count or nothing",
1826 pp_directives[ct]);
1827 } else {
1828 searching.nparam_min = searching.nparam_max =
1829 readnum(tline->text, &j);
1830 if (j)
1831 error(ERR_NONFATAL,
1832 "unable to parse parameter count `%s'",
1833 tline->text);
1834 }
1835 if (tline && tok_is_(tline->next, "-")) {
1836 tline = tline->next->next;
1837 if (tok_is_(tline, "*"))
1838 searching.nparam_max = INT_MAX;
1839 else if (!tok_type_(tline, TOK_NUMBER))
1840 error(ERR_NONFATAL,
1841 "`%s' expects a parameter count after `-'",
1842 pp_directives[ct]);
1843 else {
1844 searching.nparam_max = readnum(tline->text, &j);
1845 if (j)
1846 error(ERR_NONFATAL,
1847 "unable to parse parameter count `%s'",
1848 tline->text);
1849 if (searching.nparam_min > searching.nparam_max)
1850 error(ERR_NONFATAL,
1851 "minimum parameter count exceeds maximum");
1852 }
1853 }
1854 if (tline && tok_is_(tline->next, "+")) {
1855 tline = tline->next;
1856 searching.plus = true;
1857 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001858 mmac = (MMacro *) hash_findix(&mmacros, searching.name);
1859 while (mmac) {
1860 if (!strcmp(mmac->name, searching.name) &&
1861 (mmac->nparam_min <= searching.nparam_max
1862 || searching.plus)
1863 && (searching.nparam_min <= mmac->nparam_max
1864 || mmac->plus)) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001865 found = true;
1866 break;
1867 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001868 mmac = mmac->next;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001869 }
1870 if (tline && tline->next)
1871 error(ERR_WARNING|ERR_PASS1,
1872 "trailing garbage after %%ifmacro ignored");
1873 nasm_free(searching.name);
1874 j = found;
1875 break;
H. Peter Anvin89cee572009-07-15 09:16:54 -04001876 }
H. Peter Anvin65747262002-05-07 00:10:05 +00001877
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001878 case PPC_IFID:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001879 needtype = TOK_ID;
1880 goto iftype;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001881 case PPC_IFNUM:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001882 needtype = TOK_NUMBER;
1883 goto iftype;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001884 case PPC_IFSTR:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001885 needtype = TOK_STRING;
1886 goto iftype;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001887
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001888iftype:
1889 t = tline = expand_smacro(tline);
H. Peter Anvind85d2502008-05-04 17:53:31 -07001890
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001891 while (tok_type_(t, TOK_WHITESPACE) ||
1892 (needtype == TOK_NUMBER &&
1893 tok_type_(t, TOK_OTHER) &&
1894 (t->text[0] == '-' || t->text[0] == '+') &&
1895 !t->text[1]))
1896 t = t->next;
H. Peter Anvind85d2502008-05-04 17:53:31 -07001897
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001898 j = tok_type_(t, needtype);
1899 break;
H. Peter Anvincbf768d2008-02-16 16:41:25 -08001900
1901 case PPC_IFTOKEN:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001902 t = tline = expand_smacro(tline);
1903 while (tok_type_(t, TOK_WHITESPACE))
1904 t = t->next;
H. Peter Anvincbf768d2008-02-16 16:41:25 -08001905
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001906 j = false;
1907 if (t) {
1908 t = t->next; /* Skip the actual token */
1909 while (tok_type_(t, TOK_WHITESPACE))
1910 t = t->next;
1911 j = !t; /* Should be nothing left */
1912 }
1913 break;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001914
H. Peter Anvin134b9462008-02-16 17:01:40 -08001915 case PPC_IFEMPTY:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001916 t = tline = expand_smacro(tline);
1917 while (tok_type_(t, TOK_WHITESPACE))
1918 t = t->next;
H. Peter Anvin134b9462008-02-16 17:01:40 -08001919
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001920 j = !t; /* Should be empty */
1921 break;
H. Peter Anvin134b9462008-02-16 17:01:40 -08001922
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001923 case PPC_IF:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001924 t = tline = expand_smacro(tline);
1925 tptr = &t;
1926 tokval.t_type = TOKEN_INVALID;
1927 evalresult = evaluate(ppscan, tptr, &tokval,
1928 NULL, pass | CRITICAL, error, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001929 if (!evalresult)
1930 return -1;
1931 if (tokval.t_type)
H. Peter Anvin917a3492008-09-24 09:14:49 -07001932 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +00001933 "trailing garbage after expression ignored");
1934 if (!is_simple(evalresult)) {
1935 error(ERR_NONFATAL,
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001936 "non-constant value given to `%s'", pp_directives[ct]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001937 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001938 }
Chuck Crayne60ae75d2007-05-02 01:59:16 +00001939 j = reloc_value(evalresult) != 0;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001940 break;
H. Peter Anvin95e28822007-09-12 04:20:08 +00001941
H. Peter Anvine2c80182005-01-15 22:15:51 +00001942 default:
1943 error(ERR_FATAL,
1944 "preprocessor directive `%s' not yet implemented",
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001945 pp_directives[ct]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001946 goto fail;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001947 }
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001948
1949 free_tlist(origline);
1950 return j ^ PP_NEGATIVE(ct);
H. Peter Anvin70653092007-10-19 14:42:29 -07001951
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001952fail:
1953 free_tlist(origline);
1954 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001955}
1956
1957/*
H. Peter Anvin4db5a162007-10-11 13:42:09 -07001958 * Common code for defining an smacro
1959 */
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001960static bool define_smacro(Context *ctx, const char *mname, bool casesense,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001961 int nparam, Token *expansion)
H. Peter Anvin4db5a162007-10-11 13:42:09 -07001962{
1963 SMacro *smac, **smhead;
H. Peter Anvin166c2472008-05-28 12:28:58 -07001964 struct hash_table *smtbl;
H. Peter Anvin70653092007-10-19 14:42:29 -07001965
H. Peter Anvin4db5a162007-10-11 13:42:09 -07001966 if (smacro_defined(ctx, mname, nparam, &smac, casesense)) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001967 if (!smac) {
1968 error(ERR_WARNING|ERR_PASS1,
1969 "single-line macro `%s' defined both with and"
1970 " without parameters", mname);
1971 /*
1972 * Some instances of the old code considered this a failure,
1973 * some others didn't. What is the right thing to do here?
1974 */
1975 free_tlist(expansion);
1976 return false; /* Failure */
1977 } else {
1978 /*
1979 * We're redefining, so we have to take over an
1980 * existing SMacro structure. This means freeing
1981 * what was already in it.
1982 */
1983 nasm_free(smac->name);
1984 free_tlist(smac->expansion);
1985 }
H. Peter Anvin4db5a162007-10-11 13:42:09 -07001986 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001987 smtbl = ctx ? &ctx->localmac : &smacros;
1988 smhead = (SMacro **) hash_findi_add(smtbl, mname);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001989 smac = nasm_malloc(sizeof(SMacro));
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001990 smac->next = *smhead;
1991 *smhead = smac;
H. Peter Anvin4db5a162007-10-11 13:42:09 -07001992 }
1993 smac->name = nasm_strdup(mname);
1994 smac->casesense = casesense;
1995 smac->nparam = nparam;
1996 smac->expansion = expansion;
1997 smac->in_progress = false;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001998 return true; /* Success */
H. Peter Anvin4db5a162007-10-11 13:42:09 -07001999}
2000
2001/*
2002 * Undefine an smacro
2003 */
2004static void undef_smacro(Context *ctx, const char *mname)
2005{
2006 SMacro **smhead, *s, **sp;
H. Peter Anvin166c2472008-05-28 12:28:58 -07002007 struct hash_table *smtbl;
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002008
H. Peter Anvin166c2472008-05-28 12:28:58 -07002009 smtbl = ctx ? &ctx->localmac : &smacros;
2010 smhead = (SMacro **)hash_findi(smtbl, mname, NULL);
H. Peter Anvin70653092007-10-19 14:42:29 -07002011
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002012 if (smhead) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002013 /*
2014 * We now have a macro name... go hunt for it.
2015 */
2016 sp = smhead;
2017 while ((s = *sp) != NULL) {
2018 if (!mstrcmp(s->name, mname, s->casesense)) {
2019 *sp = s->next;
2020 nasm_free(s->name);
2021 free_tlist(s->expansion);
2022 nasm_free(s);
2023 } else {
2024 sp = &s->next;
2025 }
2026 }
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002027 }
2028}
2029
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002030/*
H. Peter Anvina26433d2008-07-16 14:40:01 -07002031 * Parse a mmacro specification.
2032 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002033static bool parse_mmacro_spec(Token *tline, MMacro *def, const char *directive)
H. Peter Anvina26433d2008-07-16 14:40:01 -07002034{
2035 bool err;
2036
2037 tline = tline->next;
2038 skip_white_(tline);
2039 tline = expand_id(tline);
2040 if (!tok_type_(tline, TOK_ID)) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002041 error(ERR_NONFATAL, "`%s' expects a macro name", directive);
2042 return false;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002043 }
Victor van den Elzenb916ede2008-07-23 15:14:22 +02002044
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002045 def->prev = NULL;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002046 def->name = nasm_strdup(tline->text);
2047 def->plus = false;
2048 def->nolist = false;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002049 def->in_progress = 0;
2050 def->rep_nest = NULL;
Victor van den Elzenb916ede2008-07-23 15:14:22 +02002051 def->nparam_min = 0;
2052 def->nparam_max = 0;
2053
H. Peter Anvina26433d2008-07-16 14:40:01 -07002054 tline = expand_smacro(tline->next);
2055 skip_white_(tline);
2056 if (!tok_type_(tline, TOK_NUMBER)) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002057 error(ERR_NONFATAL, "`%s' expects a parameter count", directive);
H. Peter Anvina26433d2008-07-16 14:40:01 -07002058 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002059 def->nparam_min = def->nparam_max =
2060 readnum(tline->text, &err);
2061 if (err)
2062 error(ERR_NONFATAL,
2063 "unable to parse parameter count `%s'", tline->text);
H. Peter Anvina26433d2008-07-16 14:40:01 -07002064 }
2065 if (tline && tok_is_(tline->next, "-")) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002066 tline = tline->next->next;
2067 if (tok_is_(tline, "*")) {
2068 def->nparam_max = INT_MAX;
2069 } else if (!tok_type_(tline, TOK_NUMBER)) {
2070 error(ERR_NONFATAL,
2071 "`%s' expects a parameter count after `-'", directive);
2072 } else {
2073 def->nparam_max = readnum(tline->text, &err);
2074 if (err) {
2075 error(ERR_NONFATAL, "unable to parse parameter count `%s'",
2076 tline->text);
2077 }
2078 if (def->nparam_min > def->nparam_max) {
2079 error(ERR_NONFATAL, "minimum parameter count exceeds maximum");
2080 }
2081 }
H. Peter Anvina26433d2008-07-16 14:40:01 -07002082 }
2083 if (tline && tok_is_(tline->next, "+")) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002084 tline = tline->next;
2085 def->plus = true;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002086 }
2087 if (tline && tok_type_(tline->next, TOK_ID) &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002088 !nasm_stricmp(tline->next->text, ".nolist")) {
2089 tline = tline->next;
2090 def->nolist = true;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002091 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002092
H. Peter Anvina26433d2008-07-16 14:40:01 -07002093 /*
2094 * Handle default parameters.
2095 */
2096 if (tline && tline->next) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002097 def->dlist = tline->next;
2098 tline->next = NULL;
2099 count_mmac_params(def->dlist, &def->ndefs, &def->defaults);
H. Peter Anvina26433d2008-07-16 14:40:01 -07002100 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002101 def->dlist = NULL;
2102 def->defaults = NULL;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002103 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002104 def->expansion = NULL;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002105
H. Peter Anvin89cee572009-07-15 09:16:54 -04002106 if (def->defaults && def->ndefs > def->nparam_max - def->nparam_min &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002107 !def->plus)
2108 error(ERR_WARNING|ERR_PASS1|ERR_WARN_MDP,
2109 "too many default macro parameters");
Victor van den Elzenb916ede2008-07-23 15:14:22 +02002110
H. Peter Anvina26433d2008-07-16 14:40:01 -07002111 return true;
2112}
2113
2114
2115/*
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002116 * Decode a size directive
2117 */
2118static int parse_size(const char *str) {
2119 static const char *size_names[] =
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002120 { "byte", "dword", "oword", "qword", "tword", "word", "yword" };
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002121 static const int sizes[] =
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002122 { 0, 1, 4, 16, 8, 10, 2, 32 };
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002123
Cyrill Gorcunova7319242010-06-03 22:04:36 +04002124 return sizes[bsii(str, size_names, ARRAY_SIZE(size_names))+1];
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002125}
2126
Ed Beroset3ab3f412002-06-11 03:31:49 +00002127/**
2128 * find and process preprocessor directive in passed line
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002129 * Find out if a line contains a preprocessor directive, and deal
2130 * with it if so.
H. Peter Anvin70653092007-10-19 14:42:29 -07002131 *
Ed Beroset3ab3f412002-06-11 03:31:49 +00002132 * If a directive _is_ found, it is the responsibility of this routine
2133 * (and not the caller) to free_tlist() the line.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002134 *
Ed Beroset3ab3f412002-06-11 03:31:49 +00002135 * @param tline a pointer to the current tokeninzed line linked list
2136 * @return DIRECTIVE_FOUND or NO_DIRECTIVE_FOUND
H. Peter Anvin70653092007-10-19 14:42:29 -07002137 *
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002138 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002139static int do_directive(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00002140{
H. Peter Anvin4169a472007-09-12 01:29:43 +00002141 enum preproc_token i;
2142 int j;
H. Peter Anvin70055962007-10-11 00:05:31 -07002143 bool err;
2144 int nparam;
2145 bool nolist;
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07002146 bool casesense;
H. Peter Anvin8cfdb9d2007-09-14 18:36:01 -07002147 int k, m;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002148 int offset;
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08002149 char *p, *pp;
2150 const char *mname;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002151 Include *inc;
2152 Context *ctx;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002153 Cond *cond;
2154 MMacro *mmac, **mmhead;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002155 Token *t, *tt, *param_start, *macro_start, *last, **tptr, *origline;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002156 Line *l;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002157 struct tokenval tokval;
2158 expr *evalresult;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002159 MMacro *tmp_defining; /* Used when manipulating rep_nest */
H. Peter Anvinf8ba53e2007-10-11 10:11:57 -07002160 int64_t count;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07002161 size_t len;
H. Peter Anvin8e3f75e2008-09-24 00:21:58 -07002162 int severity;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002163
2164 origline = tline;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002165
H. Peter Anvineba20a72002-04-30 20:53:55 +00002166 skip_white_(tline);
H. Peter Anvinf2936d72008-06-04 15:11:23 -07002167 if (!tline || !tok_type_(tline, TOK_PREPROC_ID) ||
H. Peter Anvine2c80182005-01-15 22:15:51 +00002168 (tline->text[1] == '%' || tline->text[1] == '$'
2169 || tline->text[1] == '!'))
2170 return NO_DIRECTIVE_FOUND;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002171
H. Peter Anvin4169a472007-09-12 01:29:43 +00002172 i = pp_token_hash(tline->text);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002173
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002174 /*
2175 * FIXME: We zap execution of PP_RMACRO, PP_IRMACRO, PP_EXITMACRO
2176 * since they are known to be buggy at moment, we need to fix them
2177 * in future release (2.09-2.10)
2178 */
2179 if (i == PP_RMACRO || i == PP_RMACRO || i == PP_EXITMACRO) {
2180 error(ERR_NONFATAL, "unknown preprocessor directive `%s'",
2181 tline->text);
2182 return NO_DIRECTIVE_FOUND;
2183 }
2184
2185 /*
2186 * If we're in a non-emitting branch of a condition construct,
2187 * or walking to the end of an already terminated %rep block,
2188 * we should ignore all directives except for condition
2189 * directives.
2190 */
2191 if (((istk->conds && !emitting(istk->conds->state)) ||
2192 (istk->mstk && !istk->mstk->in_progress)) && !is_condition(i)) {
2193 return NO_DIRECTIVE_FOUND;
2194 }
2195
2196 /*
2197 * If we're defining a macro or reading a %rep block, we should
2198 * ignore all directives except for %macro/%imacro (which nest),
2199 * %endm/%endmacro, and (only if we're in a %rep block) %endrep.
2200 * If we're in a %rep block, another %rep nests, so should be let through.
2201 */
2202 if (defining && i != PP_MACRO && i != PP_IMACRO &&
2203 i != PP_RMACRO && i != PP_IRMACRO &&
2204 i != PP_ENDMACRO && i != PP_ENDM &&
2205 (defining->name || (i != PP_ENDREP && i != PP_REP))) {
2206 return NO_DIRECTIVE_FOUND;
2207 }
2208
2209 if (defining) {
2210 if (i == PP_MACRO || i == PP_IMACRO ||
2211 i == PP_RMACRO || i == PP_IRMACRO) {
2212 nested_mac_count++;
2213 return NO_DIRECTIVE_FOUND;
2214 } else if (nested_mac_count > 0) {
2215 if (i == PP_ENDMACRO) {
2216 nested_mac_count--;
2217 return NO_DIRECTIVE_FOUND;
2218 }
2219 }
2220 if (!defining->name) {
2221 if (i == PP_REP) {
2222 nested_rep_count++;
2223 return NO_DIRECTIVE_FOUND;
2224 } else if (nested_rep_count > 0) {
2225 if (i == PP_ENDREP) {
2226 nested_rep_count--;
2227 return NO_DIRECTIVE_FOUND;
2228 }
2229 }
2230 }
2231 }
2232
H. Peter Anvin4169a472007-09-12 01:29:43 +00002233 switch (i) {
2234 case PP_INVALID:
H. Peter Anvine2c80182005-01-15 22:15:51 +00002235 error(ERR_NONFATAL, "unknown preprocessor directive `%s'",
2236 tline->text);
2237 return NO_DIRECTIVE_FOUND; /* didn't get it */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002238
H. Peter Anvine2c80182005-01-15 22:15:51 +00002239 case PP_STACKSIZE:
2240 /* Directive to tell NASM what the default stack size is. The
2241 * default is for a 16-bit stack, and this can be overriden with
2242 * %stacksize large.
H. Peter Anvine2c80182005-01-15 22:15:51 +00002243 */
2244 tline = tline->next;
2245 if (tline && tline->type == TOK_WHITESPACE)
2246 tline = tline->next;
2247 if (!tline || tline->type != TOK_ID) {
2248 error(ERR_NONFATAL, "`%%stacksize' missing size parameter");
2249 free_tlist(origline);
2250 return DIRECTIVE_FOUND;
2251 }
2252 if (nasm_stricmp(tline->text, "flat") == 0) {
2253 /* All subsequent ARG directives are for a 32-bit stack */
2254 StackSize = 4;
2255 StackPointer = "ebp";
2256 ArgOffset = 8;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002257 LocalOffset = 0;
Charles Crayne7eaf9192007-11-08 22:11:14 -08002258 } else if (nasm_stricmp(tline->text, "flat64") == 0) {
2259 /* All subsequent ARG directives are for a 64-bit stack */
2260 StackSize = 8;
2261 StackPointer = "rbp";
Per Jessen53252e02010-02-11 00:16:59 +03002262 ArgOffset = 16;
Charles Crayne7eaf9192007-11-08 22:11:14 -08002263 LocalOffset = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002264 } else if (nasm_stricmp(tline->text, "large") == 0) {
2265 /* All subsequent ARG directives are for a 16-bit stack,
2266 * far function call.
2267 */
2268 StackSize = 2;
2269 StackPointer = "bp";
2270 ArgOffset = 4;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002271 LocalOffset = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002272 } else if (nasm_stricmp(tline->text, "small") == 0) {
2273 /* All subsequent ARG directives are for a 16-bit stack,
2274 * far function call. We don't support near functions.
2275 */
2276 StackSize = 2;
2277 StackPointer = "bp";
2278 ArgOffset = 6;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002279 LocalOffset = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002280 } else {
2281 error(ERR_NONFATAL, "`%%stacksize' invalid size type");
2282 free_tlist(origline);
2283 return DIRECTIVE_FOUND;
2284 }
2285 free_tlist(origline);
2286 return DIRECTIVE_FOUND;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002287
H. Peter Anvine2c80182005-01-15 22:15:51 +00002288 case PP_ARG:
2289 /* TASM like ARG directive to define arguments to functions, in
2290 * the following form:
2291 *
2292 * ARG arg1:WORD, arg2:DWORD, arg4:QWORD
2293 */
2294 offset = ArgOffset;
2295 do {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00002296 char *arg, directive[256];
H. Peter Anvine2c80182005-01-15 22:15:51 +00002297 int size = StackSize;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002298
H. Peter Anvine2c80182005-01-15 22:15:51 +00002299 /* Find the argument name */
2300 tline = tline->next;
2301 if (tline && tline->type == TOK_WHITESPACE)
2302 tline = tline->next;
2303 if (!tline || tline->type != TOK_ID) {
2304 error(ERR_NONFATAL, "`%%arg' missing argument parameter");
2305 free_tlist(origline);
2306 return DIRECTIVE_FOUND;
2307 }
2308 arg = tline->text;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002309
H. Peter Anvine2c80182005-01-15 22:15:51 +00002310 /* Find the argument size type */
2311 tline = tline->next;
2312 if (!tline || tline->type != TOK_OTHER
2313 || tline->text[0] != ':') {
2314 error(ERR_NONFATAL,
2315 "Syntax error processing `%%arg' directive");
2316 free_tlist(origline);
2317 return DIRECTIVE_FOUND;
2318 }
2319 tline = tline->next;
2320 if (!tline || tline->type != TOK_ID) {
2321 error(ERR_NONFATAL, "`%%arg' missing size type parameter");
2322 free_tlist(origline);
2323 return DIRECTIVE_FOUND;
2324 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002325
H. Peter Anvine2c80182005-01-15 22:15:51 +00002326 /* Allow macro expansion of type parameter */
Keith Kaniosb7a89542007-04-12 02:40:54 +00002327 tt = tokenize(tline->text);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002328 tt = expand_smacro(tt);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002329 size = parse_size(tt->text);
2330 if (!size) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002331 error(ERR_NONFATAL,
2332 "Invalid size type for `%%arg' missing directive");
2333 free_tlist(tt);
2334 free_tlist(origline);
2335 return DIRECTIVE_FOUND;
2336 }
2337 free_tlist(tt);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002338
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002339 /* Round up to even stack slots */
2340 size = ALIGN(size, StackSize);
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002341
H. Peter Anvine2c80182005-01-15 22:15:51 +00002342 /* Now define the macro for the argument */
2343 snprintf(directive, sizeof(directive), "%%define %s (%s+%d)",
2344 arg, StackPointer, offset);
Keith Kaniosb7a89542007-04-12 02:40:54 +00002345 do_directive(tokenize(directive));
H. Peter Anvine2c80182005-01-15 22:15:51 +00002346 offset += size;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002347
H. Peter Anvine2c80182005-01-15 22:15:51 +00002348 /* Move to the next argument in the list */
2349 tline = tline->next;
2350 if (tline && tline->type == TOK_WHITESPACE)
2351 tline = tline->next;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002352 } while (tline && tline->type == TOK_OTHER && tline->text[0] == ',');
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002353 ArgOffset = offset;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002354 free_tlist(origline);
2355 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002356
H. Peter Anvine2c80182005-01-15 22:15:51 +00002357 case PP_LOCAL:
2358 /* TASM like LOCAL directive to define local variables for a
2359 * function, in the following form:
2360 *
2361 * LOCAL local1:WORD, local2:DWORD, local4:QWORD = LocalSize
2362 *
2363 * The '= LocalSize' at the end is ignored by NASM, but is
2364 * required by TASM to define the local parameter size (and used
2365 * by the TASM macro package).
2366 */
2367 offset = LocalOffset;
2368 do {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00002369 char *local, directive[256];
H. Peter Anvine2c80182005-01-15 22:15:51 +00002370 int size = StackSize;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002371
H. Peter Anvine2c80182005-01-15 22:15:51 +00002372 /* Find the argument name */
2373 tline = tline->next;
2374 if (tline && tline->type == TOK_WHITESPACE)
2375 tline = tline->next;
2376 if (!tline || tline->type != TOK_ID) {
2377 error(ERR_NONFATAL,
2378 "`%%local' missing argument parameter");
2379 free_tlist(origline);
2380 return DIRECTIVE_FOUND;
2381 }
2382 local = tline->text;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002383
H. Peter Anvine2c80182005-01-15 22:15:51 +00002384 /* Find the argument size type */
2385 tline = tline->next;
2386 if (!tline || tline->type != TOK_OTHER
2387 || tline->text[0] != ':') {
2388 error(ERR_NONFATAL,
2389 "Syntax error processing `%%local' directive");
2390 free_tlist(origline);
2391 return DIRECTIVE_FOUND;
2392 }
2393 tline = tline->next;
2394 if (!tline || tline->type != TOK_ID) {
2395 error(ERR_NONFATAL,
2396 "`%%local' missing size type parameter");
2397 free_tlist(origline);
2398 return DIRECTIVE_FOUND;
2399 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00002400
H. Peter Anvine2c80182005-01-15 22:15:51 +00002401 /* Allow macro expansion of type parameter */
Keith Kaniosb7a89542007-04-12 02:40:54 +00002402 tt = tokenize(tline->text);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002403 tt = expand_smacro(tt);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002404 size = parse_size(tt->text);
2405 if (!size) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002406 error(ERR_NONFATAL,
2407 "Invalid size type for `%%local' missing directive");
2408 free_tlist(tt);
2409 free_tlist(origline);
2410 return DIRECTIVE_FOUND;
2411 }
2412 free_tlist(tt);
H. Peter Anvin734b1882002-04-30 21:01:08 +00002413
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002414 /* Round up to even stack slots */
2415 size = ALIGN(size, StackSize);
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002416
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002417 offset += size; /* Negative offset, increment before */
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002418
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002419 /* Now define the macro for the argument */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002420 snprintf(directive, sizeof(directive), "%%define %s (%s-%d)",
2421 local, StackPointer, offset);
Keith Kaniosb7a89542007-04-12 02:40:54 +00002422 do_directive(tokenize(directive));
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002423
H. Peter Anvine2c80182005-01-15 22:15:51 +00002424 /* Now define the assign to setup the enter_c macro correctly */
2425 snprintf(directive, sizeof(directive),
2426 "%%assign %%$localsize %%$localsize+%d", size);
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 /* Move to the next argument in the list */
2430 tline = tline->next;
2431 if (tline && tline->type == TOK_WHITESPACE)
2432 tline = tline->next;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002433 } while (tline && tline->type == TOK_OTHER && tline->text[0] == ',');
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002434 LocalOffset = offset;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002435 free_tlist(origline);
2436 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002437
H. Peter Anvine2c80182005-01-15 22:15:51 +00002438 case PP_CLEAR:
2439 if (tline->next)
H. Peter Anvin917a3492008-09-24 09:14:49 -07002440 error(ERR_WARNING|ERR_PASS1,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002441 "trailing garbage after `%%clear' ignored");
2442 free_macros();
2443 init_macros();
H. Peter Anvine2c80182005-01-15 22:15:51 +00002444 free_tlist(origline);
2445 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002446
H. Peter Anvin418ca702008-05-30 10:42:30 -07002447 case PP_DEPEND:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002448 t = tline->next = expand_smacro(tline->next);
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002449 skip_white_(t);
2450 if (!t || (t->type != TOK_STRING &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002451 t->type != TOK_INTERNAL_STRING)) {
H. Peter Anvin418ca702008-05-30 10:42:30 -07002452 error(ERR_NONFATAL, "`%%depend' expects a file name");
2453 free_tlist(origline);
2454 return DIRECTIVE_FOUND; /* but we did _something_ */
2455 }
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002456 if (t->next)
H. Peter Anvin917a3492008-09-24 09:14:49 -07002457 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvin418ca702008-05-30 10:42:30 -07002458 "trailing garbage after `%%depend' ignored");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002459 p = t->text;
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002460 if (t->type != TOK_INTERNAL_STRING)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002461 nasm_unquote_cstr(p, i);
2462 if (dephead && !in_list(*dephead, p)) {
2463 StrList *sl = nasm_malloc(strlen(p)+1+sizeof sl->next);
2464 sl->next = NULL;
2465 strcpy(sl->str, p);
2466 *deptail = sl;
2467 deptail = &sl->next;
2468 }
2469 free_tlist(origline);
H. Peter Anvin418ca702008-05-30 10:42:30 -07002470 return DIRECTIVE_FOUND;
2471
2472 case PP_INCLUDE:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002473 t = tline->next = expand_smacro(tline->next);
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002474 skip_white_(t);
H. Peter Anvind2456592008-06-19 15:04:18 -07002475
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002476 if (!t || (t->type != TOK_STRING &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002477 t->type != TOK_INTERNAL_STRING)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002478 error(ERR_NONFATAL, "`%%include' expects a file name");
2479 free_tlist(origline);
2480 return DIRECTIVE_FOUND; /* but we did _something_ */
2481 }
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002482 if (t->next)
H. Peter Anvin917a3492008-09-24 09:14:49 -07002483 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +00002484 "trailing garbage after `%%include' ignored");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002485 p = t->text;
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002486 if (t->type != TOK_INTERNAL_STRING)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002487 nasm_unquote_cstr(p, i);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002488 inc = nasm_malloc(sizeof(Include));
H. Peter Anvine2c80182005-01-15 22:15:51 +00002489 inc->next = istk;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002490 inc->conds = NULL;
H. Peter Anvin2b1c3b92008-06-06 10:38:46 -07002491 inc->fp = inc_fopen(p, dephead, &deptail, pass == 0);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002492 if (!inc->fp) {
2493 /* -MG given but file not found */
2494 nasm_free(inc);
2495 } else {
2496 inc->fname = src_set_fname(nasm_strdup(p));
2497 inc->lineno = src_set_linnum(0);
2498 inc->lineinc = 1;
2499 inc->expansion = NULL;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002500 inc->mstk = NULL;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002501 istk = inc;
2502 list->uplevel(LIST_INCLUDE);
2503 }
2504 free_tlist(origline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002505 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002506
H. Peter Anvind2456592008-06-19 15:04:18 -07002507 case PP_USE:
H. Peter Anvinf4ae5ad2008-06-19 18:39:24 -07002508 {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002509 static macros_t *use_pkg;
2510 const char *pkg_macro = NULL;
H. Peter Anvinf4ae5ad2008-06-19 18:39:24 -07002511
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002512 tline = tline->next;
2513 skip_white_(tline);
2514 tline = expand_id(tline);
H. Peter Anvind2456592008-06-19 15:04:18 -07002515
H. Peter Anvin264b7b92008-10-24 16:38:17 -07002516 if (!tline || (tline->type != TOK_STRING &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002517 tline->type != TOK_INTERNAL_STRING &&
2518 tline->type != TOK_ID)) {
H. Peter Anvin926fc402008-06-19 16:26:12 -07002519 error(ERR_NONFATAL, "`%%use' expects a package name");
H. Peter Anvind2456592008-06-19 15:04:18 -07002520 free_tlist(origline);
2521 return DIRECTIVE_FOUND; /* but we did _something_ */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002522 }
H. Peter Anvin264b7b92008-10-24 16:38:17 -07002523 if (tline->next)
H. Peter Anvin917a3492008-09-24 09:14:49 -07002524 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvind2456592008-06-19 15:04:18 -07002525 "trailing garbage after `%%use' ignored");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002526 if (tline->type == TOK_STRING)
2527 nasm_unquote_cstr(tline->text, i);
2528 use_pkg = nasm_stdmac_find_package(tline->text);
2529 if (!use_pkg)
2530 error(ERR_NONFATAL, "unknown `%%use' package: %s", tline->text);
2531 else
2532 pkg_macro = (char *)use_pkg + 1; /* The first string will be <%define>__USE_*__ */
Victor van den Elzen35eb2ea2010-03-10 22:33:48 +01002533 if (use_pkg && ! smacro_defined(NULL, pkg_macro, 0, NULL, true)) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002534 /* Not already included, go ahead and include it */
2535 stdmacpos = use_pkg;
2536 }
2537 free_tlist(origline);
H. Peter Anvind2456592008-06-19 15:04:18 -07002538 return DIRECTIVE_FOUND;
H. Peter Anvinf4ae5ad2008-06-19 18:39:24 -07002539 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002540 case PP_PUSH:
H. Peter Anvine2c80182005-01-15 22:15:51 +00002541 case PP_REPL:
H. Peter Anvin42b56392008-10-24 16:24:21 -07002542 case PP_POP:
H. Peter Anvine2c80182005-01-15 22:15:51 +00002543 tline = tline->next;
2544 skip_white_(tline);
2545 tline = expand_id(tline);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002546 if (tline) {
2547 if (!tok_type_(tline, TOK_ID)) {
2548 error(ERR_NONFATAL, "`%s' expects a context identifier",
2549 pp_directives[i]);
2550 free_tlist(origline);
2551 return DIRECTIVE_FOUND; /* but we did _something_ */
2552 }
2553 if (tline->next)
2554 error(ERR_WARNING|ERR_PASS1,
2555 "trailing garbage after `%s' ignored",
2556 pp_directives[i]);
2557 p = nasm_strdup(tline->text);
2558 } else {
2559 p = NULL; /* Anonymous */
2560 }
H. Peter Anvin42b56392008-10-24 16:24:21 -07002561
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002562 if (i == PP_PUSH) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002563 ctx = nasm_malloc(sizeof(Context));
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002564 ctx->next = cstk;
2565 hash_init(&ctx->localmac, HASH_SMALL);
2566 ctx->name = p;
2567 ctx->number = unique++;
2568 cstk = ctx;
2569 } else {
2570 /* %pop or %repl */
2571 if (!cstk) {
2572 error(ERR_NONFATAL, "`%s': context stack is empty",
2573 pp_directives[i]);
2574 } else if (i == PP_POP) {
2575 if (p && (!cstk->name || nasm_stricmp(p, cstk->name)))
2576 error(ERR_NONFATAL, "`%%pop' in wrong context: %s, "
2577 "expected %s",
2578 cstk->name ? cstk->name : "anonymous", p);
2579 else
2580 ctx_pop();
2581 } else {
2582 /* i == PP_REPL */
2583 nasm_free(cstk->name);
2584 cstk->name = p;
2585 p = NULL;
2586 }
2587 nasm_free(p);
2588 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002589 free_tlist(origline);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002590 return DIRECTIVE_FOUND;
H. Peter Anvin8e3f75e2008-09-24 00:21:58 -07002591 case PP_FATAL:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002592 severity = ERR_FATAL;
2593 goto issue_error;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002594 case PP_ERROR:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002595 severity = ERR_NONFATAL;
2596 goto issue_error;
H. Peter Anvin7df04172008-06-10 18:27:38 -07002597 case PP_WARNING:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002598 severity = ERR_WARNING|ERR_WARN_USER;
2599 goto issue_error;
H. Peter Anvin8e3f75e2008-09-24 00:21:58 -07002600
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002601issue_error:
H. Peter Anvin7df04172008-06-10 18:27:38 -07002602 {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002603 /* Only error out if this is the final pass */
2604 if (pass != 2 && i != PP_FATAL)
2605 return DIRECTIVE_FOUND;
2606
2607 tline->next = expand_smacro(tline->next);
2608 tline = tline->next;
2609 skip_white_(tline);
2610 t = tline ? tline->next : NULL;
2611 skip_white_(t);
2612 if (tok_type_(tline, TOK_STRING) && !t) {
2613 /* The line contains only a quoted string */
2614 p = tline->text;
2615 nasm_unquote(p, NULL); /* Ignore NUL character truncation */
2616 error(severity, "%s", p);
2617 } else {
2618 /* Not a quoted string, or more than a quoted string */
2619 p = detoken(tline, false);
2620 error(severity, "%s", p);
2621 nasm_free(p);
2622 }
2623 free_tlist(origline);
2624 return DIRECTIVE_FOUND;
H. Peter Anvin7df04172008-06-10 18:27:38 -07002625 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00002626
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002627 CASE_PP_IF:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002628 if (istk->conds && !emitting(istk->conds->state))
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002629 j = COND_NEVER;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002630 else {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002631 j = if_condition(tline->next, i);
2632 tline->next = NULL; /* it got freed */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002633 j = j < 0 ? COND_NEVER : j ? COND_IF_TRUE : COND_IF_FALSE;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002634 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002635 cond = nasm_malloc(sizeof(Cond));
2636 cond->next = istk->conds;
2637 cond->state = j;
2638 istk->conds = cond;
2639 if(istk->mstk)
2640 istk->mstk->condcnt ++;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002641 free_tlist(origline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002642 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002643
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002644 CASE_PP_ELIF:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002645 if (!istk->conds)
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002646 error(ERR_FATAL, "`%s': no matching `%%if'", pp_directives[i]);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002647 switch(istk->conds->state) {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002648 case COND_IF_TRUE:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002649 istk->conds->state = COND_DONE;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002650 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002651
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002652 case COND_DONE:
2653 case COND_NEVER:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002654 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002655
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002656 case COND_ELSE_TRUE:
2657 case COND_ELSE_FALSE:
2658 error_precond(ERR_WARNING|ERR_PASS1,
2659 "`%%elif' after `%%else' ignored");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002660 istk->conds->state = COND_NEVER;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002661 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002662
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002663 case COND_IF_FALSE:
2664 /*
2665 * IMPORTANT: In the case of %if, we will already have
2666 * called expand_mmac_params(); however, if we're
2667 * processing an %elif we must have been in a
2668 * non-emitting mode, which would have inhibited
2669 * the normal invocation of expand_mmac_params().
2670 * Therefore, we have to do it explicitly here.
2671 */
2672 j = if_condition(expand_mmac_params(tline->next), i);
2673 tline->next = NULL; /* it got freed */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002674 istk->conds->state =
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002675 j < 0 ? COND_NEVER : j ? COND_IF_TRUE : COND_IF_FALSE;
2676 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002677 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002678 free_tlist(origline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002679 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002680
H. Peter Anvine2c80182005-01-15 22:15:51 +00002681 case PP_ELSE:
2682 if (tline->next)
H. Peter Anvin917a3492008-09-24 09:14:49 -07002683 error_precond(ERR_WARNING|ERR_PASS1,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002684 "trailing garbage after `%%else' ignored");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002685 if (!istk->conds)
2686 error(ERR_FATAL, "`%%else': no matching `%%if'");
2687 switch(istk->conds->state) {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002688 case COND_IF_TRUE:
2689 case COND_DONE:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002690 istk->conds->state = COND_ELSE_FALSE;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002691 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002692
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002693 case COND_NEVER:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002694 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002695
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002696 case COND_IF_FALSE:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002697 istk->conds->state = COND_ELSE_TRUE;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002698 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002699
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002700 case COND_ELSE_TRUE:
2701 case COND_ELSE_FALSE:
2702 error_precond(ERR_WARNING|ERR_PASS1,
2703 "`%%else' after `%%else' ignored.");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002704 istk->conds->state = COND_NEVER;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002705 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002706 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002707 free_tlist(origline);
2708 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002709
H. Peter Anvine2c80182005-01-15 22:15:51 +00002710 case PP_ENDIF:
2711 if (tline->next)
H. Peter Anvin917a3492008-09-24 09:14:49 -07002712 error_precond(ERR_WARNING|ERR_PASS1,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002713 "trailing garbage after `%%endif' ignored");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002714 if (!istk->conds)
2715 error(ERR_FATAL, "`%%endif': no matching `%%if'");
2716 cond = istk->conds;
2717 istk->conds = cond->next;
2718 nasm_free(cond);
2719 if(istk->mstk)
2720 istk->mstk->condcnt --;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002721 free_tlist(origline);
2722 return DIRECTIVE_FOUND;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002723
H. Peter Anvindb8f96e2009-07-15 09:07:29 -04002724 case PP_RMACRO:
2725 case PP_IRMACRO:
H. Peter Anvine2c80182005-01-15 22:15:51 +00002726 case PP_MACRO:
2727 case PP_IMACRO:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002728 if (defining) {
2729 error(ERR_FATAL, "`%s': already defining a macro",
2730 pp_directives[i]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002731 return DIRECTIVE_FOUND;
2732 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002733 defining = nasm_malloc(sizeof(MMacro));
2734 defining->max_depth =
2735 (i == PP_RMACRO) || (i == PP_IRMACRO) ? DEADMAN_LIMIT : 0;
2736 defining->casesense = (i == PP_MACRO) || (i == PP_RMACRO);
2737 if (!parse_mmacro_spec(tline, defining, pp_directives[i])) {
2738 nasm_free(defining);
2739 defining = NULL;
2740 return DIRECTIVE_FOUND;
2741 }
H. Peter Anvina26433d2008-07-16 14:40:01 -07002742
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002743 mmac = (MMacro *) hash_findix(&mmacros, defining->name);
2744 while (mmac) {
2745 if (!strcmp(mmac->name, defining->name) &&
2746 (mmac->nparam_min <= defining->nparam_max
2747 || defining->plus)
2748 && (defining->nparam_min <= mmac->nparam_max
2749 || mmac->plus)) {
2750 error(ERR_WARNING|ERR_PASS1,
2751 "redefining multi-line macro `%s'", defining->name);
2752 return DIRECTIVE_FOUND;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002753 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002754 mmac = mmac->next;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002755 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002756 free_tlist(origline);
2757 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002758
H. Peter Anvine2c80182005-01-15 22:15:51 +00002759 case PP_ENDM:
2760 case PP_ENDMACRO:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002761 if (! (defining && defining->name)) {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002762 error(ERR_NONFATAL, "`%s': not defining a macro", tline->text);
2763 return DIRECTIVE_FOUND;
2764 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002765 mmhead = (MMacro **) hash_findi_add(&mmacros, defining->name);
2766 defining->next = *mmhead;
2767 *mmhead = defining;
2768 defining = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002769 free_tlist(origline);
2770 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002771
H. Peter Anvin89cee572009-07-15 09:16:54 -04002772 case PP_EXITMACRO:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002773 /*
2774 * We must search along istk->expansion until we hit a
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002775 * macro-end marker for a macro with a name. Then we
2776 * bypass all lines between exitmacro and endmacro.
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002777 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002778 list_for_each(l, istk->expansion)
2779 if (l->finishes && l->finishes->name)
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002780 break;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002781
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002782 if (l) {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002783 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002784 * Remove all conditional entries relative to this
2785 * macro invocation. (safe to do in this context)
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002786 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002787 for ( ; l->finishes->condcnt > 0; l->finishes->condcnt --) {
2788 cond = istk->conds;
2789 istk->conds = cond->next;
2790 nasm_free(cond);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002791 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002792 istk->expansion = l;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002793 } else {
2794 error(ERR_NONFATAL, "`%%exitmacro' not within `%%macro' block");
2795 }
Keith Kanios852f1ee2009-07-12 00:19:55 -05002796 free_tlist(origline);
2797 return DIRECTIVE_FOUND;
2798
H. Peter Anvina26433d2008-07-16 14:40:01 -07002799 case PP_UNMACRO:
2800 case PP_UNIMACRO:
2801 {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002802 MMacro **mmac_p;
2803 MMacro spec;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002804
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002805 spec.casesense = (i == PP_UNMACRO);
2806 if (!parse_mmacro_spec(tline, &spec, pp_directives[i])) {
2807 return DIRECTIVE_FOUND;
2808 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002809 mmac_p = (MMacro **) hash_findi(&mmacros, spec.name, NULL);
2810 while (mmac_p && *mmac_p) {
2811 mmac = *mmac_p;
2812 if (mmac->casesense == spec.casesense &&
2813 !mstrcmp(mmac->name, spec.name, spec.casesense) &&
2814 mmac->nparam_min == spec.nparam_min &&
2815 mmac->nparam_max == spec.nparam_max &&
2816 mmac->plus == spec.plus) {
2817 *mmac_p = mmac->next;
2818 free_mmacro(mmac);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002819 } else {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002820 mmac_p = &mmac->next;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002821 }
2822 }
2823 free_tlist(origline);
2824 free_tlist(spec.dlist);
2825 return DIRECTIVE_FOUND;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002826 }
2827
H. Peter Anvine2c80182005-01-15 22:15:51 +00002828 case PP_ROTATE:
2829 if (tline->next && tline->next->type == TOK_WHITESPACE)
2830 tline = tline->next;
H. Peter Anvin89cee572009-07-15 09:16:54 -04002831 if (!tline->next) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002832 free_tlist(origline);
2833 error(ERR_NONFATAL, "`%%rotate' missing rotate count");
2834 return DIRECTIVE_FOUND;
2835 }
2836 t = expand_smacro(tline->next);
2837 tline->next = NULL;
2838 free_tlist(origline);
2839 tline = t;
2840 tptr = &t;
2841 tokval.t_type = TOKEN_INVALID;
2842 evalresult =
2843 evaluate(ppscan, tptr, &tokval, NULL, pass, error, NULL);
2844 free_tlist(tline);
2845 if (!evalresult)
2846 return DIRECTIVE_FOUND;
2847 if (tokval.t_type)
H. Peter Anvin917a3492008-09-24 09:14:49 -07002848 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +00002849 "trailing garbage after expression ignored");
2850 if (!is_simple(evalresult)) {
2851 error(ERR_NONFATAL, "non-constant value given to `%%rotate'");
2852 return DIRECTIVE_FOUND;
2853 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002854 mmac = istk->mstk;
2855 while (mmac && !mmac->name) /* avoid mistaking %reps for macros */
2856 mmac = mmac->next_active;
2857 if (!mmac) {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002858 error(ERR_NONFATAL, "`%%rotate' invoked outside a macro call");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002859 } else if (mmac->nparam == 0) {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002860 error(ERR_NONFATAL,
2861 "`%%rotate' invoked within macro without parameters");
2862 } else {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002863 int rotate = mmac->rotate + reloc_value(evalresult);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002864
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002865 rotate %= (int)mmac->nparam;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002866 if (rotate < 0)
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002867 rotate += mmac->nparam;
2868
2869 mmac->rotate = rotate;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002870 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002871 return DIRECTIVE_FOUND;
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00002872
H. Peter Anvine2c80182005-01-15 22:15:51 +00002873 case PP_REP:
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002874 nolist = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002875 do {
2876 tline = tline->next;
2877 } while (tok_type_(tline, TOK_WHITESPACE));
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00002878
H. Peter Anvine2c80182005-01-15 22:15:51 +00002879 if (tok_type_(tline, TOK_ID) &&
2880 nasm_stricmp(tline->text, ".nolist") == 0) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002881 nolist = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002882 do {
2883 tline = tline->next;
2884 } while (tok_type_(tline, TOK_WHITESPACE));
2885 }
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00002886
H. Peter Anvine2c80182005-01-15 22:15:51 +00002887 if (tline) {
2888 t = expand_smacro(tline);
2889 tptr = &t;
2890 tokval.t_type = TOKEN_INVALID;
2891 evalresult =
2892 evaluate(ppscan, tptr, &tokval, NULL, pass, error, NULL);
2893 if (!evalresult) {
2894 free_tlist(origline);
2895 return DIRECTIVE_FOUND;
2896 }
2897 if (tokval.t_type)
H. Peter Anvin917a3492008-09-24 09:14:49 -07002898 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +00002899 "trailing garbage after expression ignored");
2900 if (!is_simple(evalresult)) {
2901 error(ERR_NONFATAL, "non-constant value given to `%%rep'");
2902 return DIRECTIVE_FOUND;
2903 }
Cyrill Gorcunove091d6e2010-08-09 13:58:22 +04002904 count = reloc_value(evalresult);
2905 if (count >= REP_LIMIT) {
Cyrill Gorcunov71f4f842010-08-09 20:17:17 +04002906 error(ERR_NONFATAL, "`%%rep' value exceeds limit");
Cyrill Gorcunove091d6e2010-08-09 13:58:22 +04002907 count = 0;
2908 } else
2909 count++;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002910 } else {
2911 error(ERR_NONFATAL, "`%%rep' expects a repeat count");
H. Peter Anvinf8ba53e2007-10-11 10:11:57 -07002912 count = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002913 }
2914 free_tlist(origline);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002915
2916 tmp_defining = defining;
2917 defining = nasm_malloc(sizeof(MMacro));
2918 defining->prev = NULL;
2919 defining->name = NULL; /* flags this macro as a %rep block */
2920 defining->casesense = false;
2921 defining->plus = false;
2922 defining->nolist = nolist;
2923 defining->in_progress = count;
2924 defining->max_depth = 0;
2925 defining->nparam_min = defining->nparam_max = 0;
2926 defining->defaults = NULL;
2927 defining->dlist = NULL;
2928 defining->expansion = NULL;
2929 defining->next_active = istk->mstk;
2930 defining->rep_nest = tmp_defining;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002931 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002932
H. Peter Anvine2c80182005-01-15 22:15:51 +00002933 case PP_ENDREP:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002934 if (!defining || defining->name) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002935 error(ERR_NONFATAL, "`%%endrep': no matching `%%rep'");
2936 return DIRECTIVE_FOUND;
2937 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00002938
H. Peter Anvine2c80182005-01-15 22:15:51 +00002939 /*
2940 * Now we have a "macro" defined - although it has no name
2941 * and we won't be entering it in the hash tables - we must
2942 * push a macro-end marker for it on to istk->expansion.
2943 * After that, it will take care of propagating itself (a
2944 * macro-end marker line for a macro which is really a %rep
2945 * block will cause the macro to be re-expanded, complete
2946 * with another macro-end marker to ensure the process
2947 * continues) until the whole expansion is forcibly removed
2948 * from istk->expansion by a %exitrep.
2949 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002950 l = nasm_malloc(sizeof(Line));
2951 l->next = istk->expansion;
2952 l->finishes = defining;
2953 l->first = NULL;
2954 istk->expansion = l;
2955
2956 istk->mstk = defining;
2957
2958 list->uplevel(defining->nolist ? LIST_MACRO_NOLIST : LIST_MACRO);
2959 tmp_defining = defining;
2960 defining = defining->rep_nest;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002961 free_tlist(origline);
2962 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002963
H. Peter Anvine2c80182005-01-15 22:15:51 +00002964 case PP_EXITREP:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002965 /*
2966 * We must search along istk->expansion until we hit a
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002967 * macro-end marker for a macro with no name. Then we set
2968 * its `in_progress' flag to 0.
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002969 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002970 list_for_each(l, istk->expansion)
2971 if (l->finishes && !l->finishes->name)
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002972 break;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002973
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002974 if (l)
2975 l->finishes->in_progress = 1;
2976 else
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002977 error(ERR_NONFATAL, "`%%exitrep' not within `%%rep' block");
H. Peter Anvine2c80182005-01-15 22:15:51 +00002978 free_tlist(origline);
2979 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002980
H. Peter Anvine2c80182005-01-15 22:15:51 +00002981 case PP_XDEFINE:
2982 case PP_IXDEFINE:
2983 case PP_DEFINE:
2984 case PP_IDEFINE:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002985 casesense = (i == PP_DEFINE || i == PP_XDEFINE);
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07002986
H. Peter Anvine2c80182005-01-15 22:15:51 +00002987 tline = tline->next;
2988 skip_white_(tline);
2989 tline = expand_id(tline);
2990 if (!tline || (tline->type != TOK_ID &&
2991 (tline->type != TOK_PREPROC_ID ||
2992 tline->text[1] != '$'))) {
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07002993 error(ERR_NONFATAL, "`%s' expects a macro identifier",
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002994 pp_directives[i]);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002995 free_tlist(origline);
2996 return DIRECTIVE_FOUND;
2997 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00002998
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04002999 ctx = get_ctx(tline->text, &mname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003000 last = tline;
3001 param_start = tline = tline->next;
3002 nparam = 0;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003003
H. Peter Anvine2c80182005-01-15 22:15:51 +00003004 /* Expand the macro definition now for %xdefine and %ixdefine */
3005 if ((i == PP_XDEFINE) || (i == PP_IXDEFINE))
3006 tline = expand_smacro(tline);
H. Peter Anvin734b1882002-04-30 21:01:08 +00003007
H. Peter Anvine2c80182005-01-15 22:15:51 +00003008 if (tok_is_(tline, "(")) {
3009 /*
3010 * This macro has parameters.
3011 */
H. Peter Anvin734b1882002-04-30 21:01:08 +00003012
H. Peter Anvine2c80182005-01-15 22:15:51 +00003013 tline = tline->next;
3014 while (1) {
3015 skip_white_(tline);
3016 if (!tline) {
3017 error(ERR_NONFATAL, "parameter identifier expected");
3018 free_tlist(origline);
3019 return DIRECTIVE_FOUND;
3020 }
3021 if (tline->type != TOK_ID) {
3022 error(ERR_NONFATAL,
3023 "`%s': parameter identifier expected",
3024 tline->text);
3025 free_tlist(origline);
3026 return DIRECTIVE_FOUND;
3027 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003028 tline->type = TOK_SMAC_PARAM + nparam++;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003029 tline = tline->next;
3030 skip_white_(tline);
3031 if (tok_is_(tline, ",")) {
3032 tline = tline->next;
H. Peter Anvinca348b62008-07-23 10:49:26 -04003033 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003034 if (!tok_is_(tline, ")")) {
3035 error(ERR_NONFATAL,
3036 "`)' expected to terminate macro template");
3037 free_tlist(origline);
3038 return DIRECTIVE_FOUND;
3039 }
3040 break;
3041 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00003042 }
3043 last = tline;
3044 tline = tline->next;
3045 }
3046 if (tok_type_(tline, TOK_WHITESPACE))
3047 last = tline, tline = tline->next;
3048 macro_start = NULL;
3049 last->next = NULL;
3050 t = tline;
3051 while (t) {
3052 if (t->type == TOK_ID) {
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04003053 list_for_each(tt, param_start)
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003054 if (tt->type >= TOK_SMAC_PARAM &&
H. Peter Anvine2c80182005-01-15 22:15:51 +00003055 !strcmp(tt->text, t->text))
3056 t->type = tt->type;
3057 }
3058 tt = t->next;
3059 t->next = macro_start;
3060 macro_start = t;
3061 t = tt;
3062 }
3063 /*
3064 * Good. We now have a macro name, a parameter count, and a
3065 * token list (in reverse order) for an expansion. We ought
3066 * to be OK just to create an SMacro, store it, and let
3067 * free_tlist have the rest of the line (which we have
3068 * carefully re-terminated after chopping off the expansion
3069 * from the end).
3070 */
H. Peter Anvin4db5a162007-10-11 13:42:09 -07003071 define_smacro(ctx, mname, casesense, nparam, macro_start);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003072 free_tlist(origline);
3073 return DIRECTIVE_FOUND;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003074
H. Peter Anvine2c80182005-01-15 22:15:51 +00003075 case PP_UNDEF:
3076 tline = tline->next;
3077 skip_white_(tline);
3078 tline = expand_id(tline);
3079 if (!tline || (tline->type != TOK_ID &&
3080 (tline->type != TOK_PREPROC_ID ||
3081 tline->text[1] != '$'))) {
3082 error(ERR_NONFATAL, "`%%undef' expects a macro identifier");
3083 free_tlist(origline);
3084 return DIRECTIVE_FOUND;
3085 }
3086 if (tline->next) {
H. Peter Anvin917a3492008-09-24 09:14:49 -07003087 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +00003088 "trailing garbage after macro name ignored");
3089 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003090
H. Peter Anvine2c80182005-01-15 22:15:51 +00003091 /* Find the context that symbol belongs to */
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04003092 ctx = get_ctx(tline->text, &mname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003093 undef_smacro(ctx, mname);
3094 free_tlist(origline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003095 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003096
H. Peter Anvin9e200162008-06-04 17:23:14 -07003097 case PP_DEFSTR:
3098 case PP_IDEFSTR:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003099 casesense = (i == PP_DEFSTR);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003100
3101 tline = tline->next;
3102 skip_white_(tline);
3103 tline = expand_id(tline);
3104 if (!tline || (tline->type != TOK_ID &&
3105 (tline->type != TOK_PREPROC_ID ||
3106 tline->text[1] != '$'))) {
3107 error(ERR_NONFATAL, "`%s' expects a macro identifier",
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003108 pp_directives[i]);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003109 free_tlist(origline);
3110 return DIRECTIVE_FOUND;
3111 }
3112
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04003113 ctx = get_ctx(tline->text, &mname);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003114 last = tline;
3115 tline = expand_smacro(tline->next);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003116 last->next = NULL;
H. Peter Anvin9e200162008-06-04 17:23:14 -07003117
3118 while (tok_type_(tline, TOK_WHITESPACE))
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003119 tline = delete_Token(tline);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003120
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003121 p = detoken(tline, false);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003122 macro_start = nasm_malloc(sizeof(*macro_start));
3123 macro_start->next = NULL;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003124 macro_start->text = nasm_quote(p, strlen(p));
3125 macro_start->type = TOK_STRING;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003126 macro_start->a.mac = NULL;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003127 nasm_free(p);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003128
3129 /*
3130 * We now have a macro name, an implicit parameter count of
3131 * zero, and a string token to use as an expansion. Create
3132 * and store an SMacro.
3133 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003134 define_smacro(ctx, mname, casesense, 0, macro_start);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003135 free_tlist(origline);
3136 return DIRECTIVE_FOUND;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003137
H. Peter Anvin2f55bda2009-07-14 15:04:04 -04003138 case PP_DEFTOK:
3139 case PP_IDEFTOK:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003140 casesense = (i == PP_DEFTOK);
3141
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003142 tline = tline->next;
3143 skip_white_(tline);
3144 tline = expand_id(tline);
3145 if (!tline || (tline->type != TOK_ID &&
3146 (tline->type != TOK_PREPROC_ID ||
3147 tline->text[1] != '$'))) {
3148 error(ERR_NONFATAL,
3149 "`%s' expects a macro identifier as first parameter",
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003150 pp_directives[i]);
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003151 free_tlist(origline);
3152 return DIRECTIVE_FOUND;
3153 }
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04003154 ctx = get_ctx(tline->text, &mname);
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003155 last = tline;
3156 tline = expand_smacro(tline->next);
3157 last->next = NULL;
3158
3159 t = tline;
3160 while (tok_type_(t, TOK_WHITESPACE))
3161 t = t->next;
3162 /* t should now point to the string */
Cyrill Gorcunov6908e582010-09-06 19:36:15 +04003163 if (!tok_type_(t, TOK_STRING)) {
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003164 error(ERR_NONFATAL,
3165 "`%s` requires string as second parameter",
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003166 pp_directives[i]);
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003167 free_tlist(tline);
3168 free_tlist(origline);
3169 return DIRECTIVE_FOUND;
3170 }
3171
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003172 /*
3173 * Convert the string to a token stream. Note that smacros
3174 * are stored with the token stream reversed, so we have to
3175 * reverse the output of tokenize().
3176 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003177 nasm_unquote_cstr(t->text, i);
H. Peter Anvinb40992c2010-09-15 08:57:21 -07003178 macro_start = reverse_tokens(tokenize(t->text));
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003179
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003180 /*
3181 * We now have a macro name, an implicit parameter count of
3182 * zero, and a numeric token to use as an expansion. Create
3183 * and store an SMacro.
3184 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003185 define_smacro(ctx, mname, casesense, 0, macro_start);
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003186 free_tlist(tline);
3187 free_tlist(origline);
3188 return DIRECTIVE_FOUND;
H. Peter Anvin9e200162008-06-04 17:23:14 -07003189
H. Peter Anvin418ca702008-05-30 10:42:30 -07003190 case PP_PATHSEARCH:
3191 {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003192 FILE *fp;
3193 StrList *xsl = NULL;
3194 StrList **xst = &xsl;
H. Peter Anvin418ca702008-05-30 10:42:30 -07003195
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003196 casesense = true;
H. Peter Anvin418ca702008-05-30 10:42:30 -07003197
3198 tline = tline->next;
3199 skip_white_(tline);
3200 tline = expand_id(tline);
3201 if (!tline || (tline->type != TOK_ID &&
3202 (tline->type != TOK_PREPROC_ID ||
3203 tline->text[1] != '$'))) {
3204 error(ERR_NONFATAL,
3205 "`%%pathsearch' expects a macro identifier as first parameter");
3206 free_tlist(origline);
3207 return DIRECTIVE_FOUND;
3208 }
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04003209 ctx = get_ctx(tline->text, &mname);
H. Peter Anvin418ca702008-05-30 10:42:30 -07003210 last = tline;
3211 tline = expand_smacro(tline->next);
3212 last->next = NULL;
3213
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003214 t = tline;
H. Peter Anvin418ca702008-05-30 10:42:30 -07003215 while (tok_type_(t, TOK_WHITESPACE))
3216 t = t->next;
3217
3218 if (!t || (t->type != TOK_STRING &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003219 t->type != TOK_INTERNAL_STRING)) {
H. Peter Anvin418ca702008-05-30 10:42:30 -07003220 error(ERR_NONFATAL, "`%%pathsearch' expects a file name");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003221 free_tlist(tline);
H. Peter Anvin418ca702008-05-30 10:42:30 -07003222 free_tlist(origline);
3223 return DIRECTIVE_FOUND; /* but we did _something_ */
3224 }
3225 if (t->next)
H. Peter Anvin917a3492008-09-24 09:14:49 -07003226 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvin418ca702008-05-30 10:42:30 -07003227 "trailing garbage after `%%pathsearch' ignored");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003228 p = t->text;
H. Peter Anvin427cc912008-06-01 21:43:03 -07003229 if (t->type != TOK_INTERNAL_STRING)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003230 nasm_unquote(p, NULL);
H. Peter Anvin418ca702008-05-30 10:42:30 -07003231
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003232 fp = inc_fopen(p, &xsl, &xst, true);
3233 if (fp) {
3234 p = xsl->str;
3235 fclose(fp); /* Don't actually care about the file */
3236 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003237 macro_start = nasm_malloc(sizeof(*macro_start));
3238 macro_start->next = NULL;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003239 macro_start->text = nasm_quote(p, strlen(p));
3240 macro_start->type = TOK_STRING;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003241 macro_start->a.mac = NULL;
3242 if (xsl)
3243 nasm_free(xsl);
H. Peter Anvin418ca702008-05-30 10:42:30 -07003244
3245 /*
3246 * We now have a macro name, an implicit parameter count of
3247 * zero, and a string token to use as an expansion. Create
3248 * and store an SMacro.
3249 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003250 define_smacro(ctx, mname, casesense, 0, macro_start);
3251 free_tlist(tline);
H. Peter Anvin418ca702008-05-30 10:42:30 -07003252 free_tlist(origline);
3253 return DIRECTIVE_FOUND;
3254 }
3255
H. Peter Anvine2c80182005-01-15 22:15:51 +00003256 case PP_STRLEN:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003257 casesense = true;
H. Peter Anvin70653092007-10-19 14:42:29 -07003258
H. Peter Anvine2c80182005-01-15 22:15:51 +00003259 tline = tline->next;
3260 skip_white_(tline);
3261 tline = expand_id(tline);
3262 if (!tline || (tline->type != TOK_ID &&
3263 (tline->type != TOK_PREPROC_ID ||
3264 tline->text[1] != '$'))) {
3265 error(ERR_NONFATAL,
3266 "`%%strlen' expects a macro identifier as first parameter");
3267 free_tlist(origline);
3268 return DIRECTIVE_FOUND;
3269 }
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04003270 ctx = get_ctx(tline->text, &mname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003271 last = tline;
3272 tline = expand_smacro(tline->next);
3273 last->next = NULL;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003274
H. Peter Anvine2c80182005-01-15 22:15:51 +00003275 t = tline;
3276 while (tok_type_(t, TOK_WHITESPACE))
3277 t = t->next;
3278 /* t should now point to the string */
Cyrill Gorcunov4e1d5ab2010-07-23 18:51:51 +04003279 if (!tok_type_(t, TOK_STRING)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00003280 error(ERR_NONFATAL,
3281 "`%%strlen` requires string as second parameter");
3282 free_tlist(tline);
3283 free_tlist(origline);
3284 return DIRECTIVE_FOUND;
3285 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003286
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003287 macro_start = nasm_malloc(sizeof(*macro_start));
3288 macro_start->next = NULL;
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07003289 make_tok_num(macro_start, nasm_unquote(t->text, NULL));
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003290 macro_start->a.mac = NULL;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003291
H. Peter Anvine2c80182005-01-15 22:15:51 +00003292 /*
3293 * We now have a macro name, an implicit parameter count of
3294 * zero, and a numeric token to use as an expansion. Create
3295 * and store an SMacro.
3296 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003297 define_smacro(ctx, mname, casesense, 0, macro_start);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003298 free_tlist(tline);
3299 free_tlist(origline);
3300 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003301
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003302 case PP_STRCAT:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003303 casesense = true;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003304
3305 tline = tline->next;
3306 skip_white_(tline);
3307 tline = expand_id(tline);
3308 if (!tline || (tline->type != TOK_ID &&
3309 (tline->type != TOK_PREPROC_ID ||
3310 tline->text[1] != '$'))) {
3311 error(ERR_NONFATAL,
3312 "`%%strcat' expects a macro identifier as first parameter");
3313 free_tlist(origline);
3314 return DIRECTIVE_FOUND;
3315 }
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04003316 ctx = get_ctx(tline->text, &mname);
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003317 last = tline;
3318 tline = expand_smacro(tline->next);
3319 last->next = NULL;
3320
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003321 len = 0;
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04003322 list_for_each(t, tline) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003323 switch (t->type) {
3324 case TOK_WHITESPACE:
3325 break;
3326 case TOK_STRING:
3327 len += t->a.len = nasm_unquote(t->text, NULL);
3328 break;
3329 case TOK_OTHER:
3330 if (!strcmp(t->text, ",")) /* permit comma separators */
3331 break;
3332 /* else fall through */
3333 default:
3334 error(ERR_NONFATAL,
3335 "non-string passed to `%%strcat' (%d)", t->type);
3336 free_tlist(tline);
3337 free_tlist(origline);
3338 return DIRECTIVE_FOUND;
3339 }
3340 }
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003341
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003342 p = pp = nasm_malloc(len);
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04003343 list_for_each(t, tline) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003344 if (t->type == TOK_STRING) {
3345 memcpy(p, t->text, t->a.len);
3346 p += t->a.len;
3347 }
3348 }
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003349
3350 /*
3351 * We now have a macro name, an implicit parameter count of
3352 * zero, and a numeric token to use as an expansion. Create
3353 * and store an SMacro.
3354 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003355 macro_start = new_Token(NULL, TOK_STRING, NULL, 0);
3356 macro_start->text = nasm_quote(pp, len);
3357 nasm_free(pp);
3358 define_smacro(ctx, mname, casesense, 0, macro_start);
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003359 free_tlist(tline);
3360 free_tlist(origline);
3361 return DIRECTIVE_FOUND;
3362
H. Peter Anvine2c80182005-01-15 22:15:51 +00003363 case PP_SUBSTR:
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003364 {
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003365 int64_t start, count;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003366 size_t len;
H. Peter Anvind2456592008-06-19 15:04:18 -07003367
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003368 casesense = true;
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07003369
H. Peter Anvine2c80182005-01-15 22:15:51 +00003370 tline = tline->next;
3371 skip_white_(tline);
3372 tline = expand_id(tline);
3373 if (!tline || (tline->type != TOK_ID &&
3374 (tline->type != TOK_PREPROC_ID ||
3375 tline->text[1] != '$'))) {
3376 error(ERR_NONFATAL,
3377 "`%%substr' expects a macro identifier as first parameter");
3378 free_tlist(origline);
3379 return DIRECTIVE_FOUND;
3380 }
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04003381 ctx = get_ctx(tline->text, &mname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003382 last = tline;
3383 tline = expand_smacro(tline->next);
3384 last->next = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003385
Cyrill Gorcunov35519d62010-09-06 23:49:52 +04003386 if (tline) /* skip expanded id */
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003387 t = tline->next;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003388 while (tok_type_(t, TOK_WHITESPACE))
3389 t = t->next;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003390
H. Peter Anvine2c80182005-01-15 22:15:51 +00003391 /* t should now point to the string */
Cyrill Gorcunov35519d62010-09-06 23:49:52 +04003392 if (!tok_type_(t, TOK_STRING)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00003393 error(ERR_NONFATAL,
3394 "`%%substr` requires string as second parameter");
3395 free_tlist(tline);
3396 free_tlist(origline);
3397 return DIRECTIVE_FOUND;
3398 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003399
H. Peter Anvine2c80182005-01-15 22:15:51 +00003400 tt = t->next;
3401 tptr = &tt;
3402 tokval.t_type = TOKEN_INVALID;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003403 evalresult = evaluate(ppscan, tptr, &tokval, NULL,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003404 pass, error, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003405 if (!evalresult) {
3406 free_tlist(tline);
3407 free_tlist(origline);
3408 return DIRECTIVE_FOUND;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003409 } else if (!is_simple(evalresult)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00003410 error(ERR_NONFATAL, "non-constant value given to `%%substr`");
3411 free_tlist(tline);
3412 free_tlist(origline);
3413 return DIRECTIVE_FOUND;
3414 }
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003415 start = evalresult->value - 1;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003416
3417 while (tok_type_(tt, TOK_WHITESPACE))
3418 tt = tt->next;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003419 if (!tt) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003420 count = 1; /* Backwards compatibility: one character */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003421 } else {
3422 tokval.t_type = TOKEN_INVALID;
3423 evalresult = evaluate(ppscan, tptr, &tokval, NULL,
3424 pass, error, NULL);
3425 if (!evalresult) {
3426 free_tlist(tline);
3427 free_tlist(origline);
3428 return DIRECTIVE_FOUND;
3429 } else if (!is_simple(evalresult)) {
3430 error(ERR_NONFATAL, "non-constant value given to `%%substr`");
3431 free_tlist(tline);
3432 free_tlist(origline);
3433 return DIRECTIVE_FOUND;
3434 }
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003435 count = evalresult->value;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003436 }
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003437
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003438 len = nasm_unquote(t->text, NULL);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003439
Cyrill Gorcunovcff031e2010-09-07 20:31:11 +04003440 /* make start and count being in range */
3441 if (start < 0)
3442 start = 0;
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003443 if (count < 0)
3444 count = len + count + 1 - start;
3445 if (start + count > (int64_t)len)
Cyrill Gorcunovcff031e2010-09-07 20:31:11 +04003446 count = len - start;
3447 if (!len || count < 0 || start >=(int64_t)len)
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003448 start = -1, count = 0; /* empty string */
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003449
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003450 macro_start = nasm_malloc(sizeof(*macro_start));
3451 macro_start->next = NULL;
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003452 macro_start->text = nasm_quote((start < 0) ? "" : t->text + start, count);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003453 macro_start->type = TOK_STRING;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003454 macro_start->a.mac = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003455
H. Peter Anvine2c80182005-01-15 22:15:51 +00003456 /*
3457 * We now have a macro name, an implicit parameter count of
3458 * zero, and a numeric token to use as an expansion. Create
3459 * and store an SMacro.
3460 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003461 define_smacro(ctx, mname, casesense, 0, macro_start);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003462 free_tlist(tline);
3463 free_tlist(origline);
3464 return DIRECTIVE_FOUND;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003465 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003466
H. Peter Anvine2c80182005-01-15 22:15:51 +00003467 case PP_ASSIGN:
3468 case PP_IASSIGN:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003469 casesense = (i == PP_ASSIGN);
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07003470
H. Peter Anvine2c80182005-01-15 22:15:51 +00003471 tline = tline->next;
3472 skip_white_(tline);
3473 tline = expand_id(tline);
3474 if (!tline || (tline->type != TOK_ID &&
3475 (tline->type != TOK_PREPROC_ID ||
3476 tline->text[1] != '$'))) {
3477 error(ERR_NONFATAL,
3478 "`%%%sassign' expects a macro identifier",
3479 (i == PP_IASSIGN ? "i" : ""));
3480 free_tlist(origline);
3481 return DIRECTIVE_FOUND;
3482 }
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04003483 ctx = get_ctx(tline->text, &mname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003484 last = tline;
3485 tline = expand_smacro(tline->next);
3486 last->next = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003487
H. Peter Anvine2c80182005-01-15 22:15:51 +00003488 t = tline;
3489 tptr = &t;
3490 tokval.t_type = TOKEN_INVALID;
3491 evalresult =
3492 evaluate(ppscan, tptr, &tokval, NULL, pass, error, NULL);
3493 free_tlist(tline);
3494 if (!evalresult) {
3495 free_tlist(origline);
3496 return DIRECTIVE_FOUND;
3497 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003498
H. Peter Anvine2c80182005-01-15 22:15:51 +00003499 if (tokval.t_type)
H. Peter Anvin917a3492008-09-24 09:14:49 -07003500 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +00003501 "trailing garbage after expression ignored");
H. Peter Anvin734b1882002-04-30 21:01:08 +00003502
H. Peter Anvine2c80182005-01-15 22:15:51 +00003503 if (!is_simple(evalresult)) {
3504 error(ERR_NONFATAL,
3505 "non-constant value given to `%%%sassign'",
3506 (i == PP_IASSIGN ? "i" : ""));
3507 free_tlist(origline);
3508 return DIRECTIVE_FOUND;
3509 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003510
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003511 macro_start = nasm_malloc(sizeof(*macro_start));
3512 macro_start->next = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003513 make_tok_num(macro_start, reloc_value(evalresult));
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003514 macro_start->a.mac = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003515
H. Peter Anvine2c80182005-01-15 22:15:51 +00003516 /*
3517 * We now have a macro name, an implicit parameter count of
3518 * zero, and a numeric token to use as an expansion. Create
3519 * and store an SMacro.
3520 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003521 define_smacro(ctx, mname, casesense, 0, macro_start);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003522 free_tlist(origline);
3523 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003524
H. Peter Anvine2c80182005-01-15 22:15:51 +00003525 case PP_LINE:
3526 /*
3527 * Syntax is `%line nnn[+mmm] [filename]'
3528 */
3529 tline = tline->next;
3530 skip_white_(tline);
3531 if (!tok_type_(tline, TOK_NUMBER)) {
3532 error(ERR_NONFATAL, "`%%line' expects line number");
3533 free_tlist(origline);
3534 return DIRECTIVE_FOUND;
3535 }
H. Peter Anvin70055962007-10-11 00:05:31 -07003536 k = readnum(tline->text, &err);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003537 m = 1;
3538 tline = tline->next;
3539 if (tok_is_(tline, "+")) {
3540 tline = tline->next;
3541 if (!tok_type_(tline, TOK_NUMBER)) {
3542 error(ERR_NONFATAL, "`%%line' expects line increment");
3543 free_tlist(origline);
3544 return DIRECTIVE_FOUND;
3545 }
H. Peter Anvin70055962007-10-11 00:05:31 -07003546 m = readnum(tline->text, &err);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003547 tline = tline->next;
3548 }
3549 skip_white_(tline);
3550 src_set_linnum(k);
3551 istk->lineinc = m;
3552 if (tline) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07003553 nasm_free(src_set_fname(detoken(tline, false)));
H. Peter Anvine2c80182005-01-15 22:15:51 +00003554 }
3555 free_tlist(origline);
3556 return DIRECTIVE_FOUND;
Keith Kaniosb307a4f2010-11-06 17:41:51 -05003557
H. Peter Anvine2c80182005-01-15 22:15:51 +00003558 default:
3559 error(ERR_FATAL,
3560 "preprocessor directive `%s' not yet implemented",
H. Peter Anvin4169a472007-09-12 01:29:43 +00003561 pp_directives[i]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003562 return DIRECTIVE_FOUND;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003563 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003564}
3565
3566/*
H. Peter Anvin76690a12002-04-30 20:52:49 +00003567 * Ensure that a macro parameter contains a condition code and
3568 * nothing else. Return the condition code index if so, or -1
3569 * otherwise.
3570 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003571static int find_cc(Token * t)
H. Peter Anvineba20a72002-04-30 20:53:55 +00003572{
H. Peter Anvin76690a12002-04-30 20:52:49 +00003573 Token *tt;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003574
H. Peter Anvin25a99342007-09-22 17:45:45 -07003575 if (!t)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003576 return -1; /* Probably a %+ without a space */
H. Peter Anvin25a99342007-09-22 17:45:45 -07003577
H. Peter Anvineba20a72002-04-30 20:53:55 +00003578 skip_white_(t);
H. Peter Anvin76690a12002-04-30 20:52:49 +00003579 if (t->type != TOK_ID)
H. Peter Anvine2c80182005-01-15 22:15:51 +00003580 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003581 tt = t->next;
H. Peter Anvineba20a72002-04-30 20:53:55 +00003582 skip_white_(tt);
H. Peter Anvin76690a12002-04-30 20:52:49 +00003583 if (tt && (tt->type != TOK_OTHER || strcmp(tt->text, ",")))
H. Peter Anvine2c80182005-01-15 22:15:51 +00003584 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003585
Cyrill Gorcunov19456392012-05-02 00:18:56 +04003586 return bsii(t->text, (const char **)conditions, ARRAY_SIZE(conditions));
H. Peter Anvin76690a12002-04-30 20:52:49 +00003587}
3588
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003589/*
3590 * This routines walks over tokens strem and hadnles tokens
3591 * pasting, if @handle_explicit passed then explicit pasting
3592 * term is handled, otherwise -- implicit pastings only.
3593 */
Cyrill Gorcunov575d4282010-10-06 00:25:55 +04003594static bool paste_tokens(Token **head, const struct tokseq_match *m,
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003595 size_t mnum, bool handle_explicit)
H. Peter Anvind784a082009-04-20 14:01:18 -07003596{
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003597 Token *tok, *next, **prev_next, **prev_nonspace;
3598 bool pasted = false;
3599 char *buf, *p;
3600 size_t len, i;
H. Peter Anvind784a082009-04-20 14:01:18 -07003601
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003602 /*
3603 * The last token before pasting. We need it
3604 * to be able to connect new handled tokens.
3605 * In other words if there were a tokens stream
3606 *
3607 * A -> B -> C -> D
3608 *
3609 * and we've joined tokens B and C, the resulting
3610 * stream should be
3611 *
3612 * A -> BC -> D
3613 */
3614 tok = *head;
3615 prev_next = NULL;
3616
3617 if (!tok_type_(tok, TOK_WHITESPACE) && !tok_type_(tok, TOK_PASTE))
3618 prev_nonspace = head;
3619 else
3620 prev_nonspace = NULL;
3621
3622 while (tok && (next = tok->next)) {
3623
3624 switch (tok->type) {
H. Peter Anvind784a082009-04-20 14:01:18 -07003625 case TOK_WHITESPACE:
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003626 /* Zap redundant whitespaces */
3627 while (tok_type_(next, TOK_WHITESPACE))
3628 next = delete_Token(next);
3629 tok->next = next;
H. Peter Anvind784a082009-04-20 14:01:18 -07003630 break;
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003631
3632 case TOK_PASTE:
3633 /* Explicit pasting */
3634 if (!handle_explicit)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003635 break;
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003636 next = delete_Token(tok);
3637
3638 while (tok_type_(next, TOK_WHITESPACE))
3639 next = delete_Token(next);
3640
3641 if (!pasted)
3642 pasted = true;
3643
3644 /* No ending token */
3645 if (!next)
3646 error(ERR_FATAL, "No rvalue found on pasting");
3647
3648 /* Left pasting token is start of line */
3649 if (!prev_nonspace)
3650 error(ERR_FATAL, "No lvalue found on pasting");
3651
3652 tok = *prev_nonspace;
3653 while (tok_type_(tok, TOK_WHITESPACE))
3654 tok = delete_Token(tok);
3655 len = strlen(tok->text);
3656 len += strlen(next->text);
3657
3658 p = buf = nasm_malloc(len + 1);
3659 strcpy(p, tok->text);
3660 p = strchr(p, '\0');
3661 strcpy(p, next->text);
3662
3663 delete_Token(tok);
3664
3665 tok = tokenize(buf);
3666 nasm_free(buf);
3667
3668 *prev_nonspace = tok;
3669 while (tok && tok->next)
3670 tok = tok->next;
3671
3672 tok->next = delete_Token(next);
3673
3674 /* Restart from pasted tokens head */
3675 tok = *prev_nonspace;
3676 break;
3677
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003678 default:
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003679 /* implicit pasting */
Cyrill Gorcunov575d4282010-10-06 00:25:55 +04003680 for (i = 0; i < mnum; i++) {
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003681 if (!(PP_CONCAT_MATCH(tok, m[i].mask_head)))
3682 continue;
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +04003683
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003684 len = 0;
3685 while (next && PP_CONCAT_MATCH(next, m[i].mask_tail)) {
3686 len += strlen(next->text);
3687 next = next->next;
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +04003688 }
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003689
3690 /* No match */
3691 if (tok == next)
3692 break;
3693
3694 len += strlen(tok->text);
3695 p = buf = nasm_malloc(len + 1);
3696
3697 while (tok != next) {
3698 strcpy(p, tok->text);
3699 p = strchr(p, '\0');
3700 tok = delete_Token(tok);
3701 }
3702
3703 tok = tokenize(buf);
3704 nasm_free(buf);
3705
3706 if (prev_next)
3707 *prev_next = tok;
3708 else
3709 *head = tok;
3710
3711 /*
3712 * Connect pasted into original stream,
3713 * ie A -> new-tokens -> B
3714 */
3715 while (tok && tok->next)
3716 tok = tok->next;
3717 tok->next = next;
3718
3719 if (!pasted)
3720 pasted = true;
3721
3722 /* Restart from pasted tokens head */
3723 tok = prev_next ? *prev_next : *head;
Cyrill Gorcunov575d4282010-10-06 00:25:55 +04003724 }
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003725
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003726 break;
H. Peter Anvind784a082009-04-20 14:01:18 -07003727 }
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003728
3729 prev_next = &tok->next;
3730
3731 if (tok->next &&
3732 !tok_type_(tok->next, TOK_WHITESPACE) &&
3733 !tok_type_(tok->next, TOK_PASTE))
3734 prev_nonspace = prev_next;
3735
3736 tok = tok->next;
H. Peter Anvind784a082009-04-20 14:01:18 -07003737 }
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003738
3739 return pasted;
H. Peter Anvind784a082009-04-20 14:01:18 -07003740}
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003741
3742/*
3743 * expands to a list of tokens from %{x:y}
3744 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003745static Token *expand_mmac_params_range(MMacro *mac, Token *tline, Token ***last)
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003746{
3747 Token *t = tline, **tt, *tm, *head;
3748 char *pos;
3749 int fst, lst, j, i;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003750
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003751 pos = strchr(tline->text, ':');
3752 nasm_assert(pos);
3753
3754 lst = atoi(pos + 1);
3755 fst = atoi(tline->text + 1);
3756
3757 /*
3758 * only macros params are accounted so
3759 * if someone passes %0 -- we reject such
3760 * value(s)
3761 */
3762 if (lst == 0 || fst == 0)
3763 goto err;
3764
3765 /* the values should be sane */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003766 if ((fst > (int)mac->nparam || fst < (-(int)mac->nparam)) ||
3767 (lst > (int)mac->nparam || lst < (-(int)mac->nparam)))
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003768 goto err;
3769
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003770 fst = fst < 0 ? fst + (int)mac->nparam + 1: fst;
3771 lst = lst < 0 ? lst + (int)mac->nparam + 1: lst;
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003772
3773 /* counted from zero */
3774 fst--, lst--;
3775
3776 /*
3777 * it will be at least one token
3778 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003779 tm = mac->params[(fst + mac->rotate) % mac->nparam];
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003780 t = new_Token(NULL, tm->type, tm->text, 0);
3781 head = t, tt = &t->next;
3782 if (fst < lst) {
3783 for (i = fst + 1; i <= lst; i++) {
3784 t = new_Token(NULL, TOK_OTHER, ",", 0);
3785 *tt = t, tt = &t->next;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003786 j = (i + mac->rotate) % mac->nparam;
3787 tm = mac->params[j];
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003788 t = new_Token(NULL, tm->type, tm->text, 0);
3789 *tt = t, tt = &t->next;
3790 }
3791 } else {
3792 for (i = fst - 1; i >= lst; i--) {
3793 t = new_Token(NULL, TOK_OTHER, ",", 0);
3794 *tt = t, tt = &t->next;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003795 j = (i + mac->rotate) % mac->nparam;
3796 tm = mac->params[j];
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003797 t = new_Token(NULL, tm->type, tm->text, 0);
3798 *tt = t, tt = &t->next;
3799 }
3800 }
3801
3802 *last = tt;
3803 return head;
3804
3805err:
3806 error(ERR_NONFATAL, "`%%{%s}': macro parameters out of range",
3807 &tline->text[1]);
3808 return tline;
3809}
3810
H. Peter Anvin76690a12002-04-30 20:52:49 +00003811/*
3812 * Expand MMacro-local things: parameter references (%0, %n, %+n,
H. Peter Anvin67c63722008-10-26 23:49:00 -07003813 * %-n) and MMacro-local identifiers (%%foo) as well as
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003814 * macro indirection (%[...]) and range (%{..:..}).
H. Peter Anvin76690a12002-04-30 20:52:49 +00003815 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003816static Token *expand_mmac_params(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00003817{
H. Peter Anvin734b1882002-04-30 21:01:08 +00003818 Token *t, *tt, **tail, *thead;
H. Peter Anvin6125b622009-04-08 14:02:25 -07003819 bool changed = false;
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003820 char *pos;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003821
3822 tail = &thead;
3823 thead = NULL;
3824
H. Peter Anvine2c80182005-01-15 22:15:51 +00003825 while (tline) {
3826 if (tline->type == TOK_PREPROC_ID &&
Cyrill Gorcunovca611192010-06-04 09:22:12 +04003827 (((tline->text[1] == '+' || tline->text[1] == '-') && tline->text[2]) ||
3828 (tline->text[1] >= '0' && tline->text[1] <= '9') ||
3829 tline->text[1] == '%')) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00003830 char *text = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003831 int type = 0, cc; /* type = 0 to placate optimisers */
Keith Kaniosa6dfa782007-04-13 16:47:53 +00003832 char tmpbuf[30];
H. Peter Anvin25a99342007-09-22 17:45:45 -07003833 unsigned int n;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003834 int i;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003835 MMacro *mac;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003836
H. Peter Anvine2c80182005-01-15 22:15:51 +00003837 t = tline;
3838 tline = tline->next;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003839
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003840 mac = istk->mstk;
3841 while (mac && !mac->name) /* avoid mistaking %reps for macros */
3842 mac = mac->next_active;
3843 if (!mac) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00003844 error(ERR_NONFATAL, "`%s': not in a macro call", t->text);
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003845 } else {
3846 pos = strchr(t->text, ':');
3847 if (!pos) {
3848 switch (t->text[1]) {
3849 /*
3850 * We have to make a substitution of one of the
3851 * forms %1, %-1, %+1, %%foo, %0.
3852 */
3853 case '0':
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003854 type = TOK_NUMBER;
3855 snprintf(tmpbuf, sizeof(tmpbuf), "%d", mac->nparam);
3856 text = nasm_strdup(tmpbuf);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003857 break;
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003858 case '%':
H. Peter Anvine2c80182005-01-15 22:15:51 +00003859 type = TOK_ID;
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003860 snprintf(tmpbuf, sizeof(tmpbuf), "..@%"PRIu64".",
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003861 mac->unique);
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003862 text = nasm_strcat(tmpbuf, t->text + 2);
3863 break;
3864 case '-':
3865 n = atoi(t->text + 2) - 1;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003866 if (n >= mac->nparam)
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003867 tt = NULL;
3868 else {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003869 if (mac->nparam > 1)
3870 n = (n + mac->rotate) % mac->nparam;
3871 tt = mac->params[n];
H. Peter Anvine2c80182005-01-15 22:15:51 +00003872 }
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003873 cc = find_cc(tt);
3874 if (cc == -1) {
3875 error(ERR_NONFATAL,
3876 "macro parameter %d is not a condition code",
3877 n + 1);
3878 text = NULL;
3879 } else {
3880 type = TOK_ID;
3881 if (inverse_ccs[cc] == -1) {
3882 error(ERR_NONFATAL,
3883 "condition code `%s' is not invertible",
3884 conditions[cc]);
3885 text = NULL;
3886 } else
3887 text = nasm_strdup(conditions[inverse_ccs[cc]]);
3888 }
3889 break;
3890 case '+':
3891 n = atoi(t->text + 2) - 1;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003892 if (n >= mac->nparam)
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003893 tt = NULL;
3894 else {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003895 if (mac->nparam > 1)
3896 n = (n + mac->rotate) % mac->nparam;
3897 tt = mac->params[n];
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003898 }
3899 cc = find_cc(tt);
3900 if (cc == -1) {
3901 error(ERR_NONFATAL,
3902 "macro parameter %d is not a condition code",
3903 n + 1);
3904 text = NULL;
3905 } else {
3906 type = TOK_ID;
3907 text = nasm_strdup(conditions[cc]);
3908 }
3909 break;
3910 default:
3911 n = atoi(t->text + 1) - 1;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003912 if (n >= mac->nparam)
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003913 tt = NULL;
3914 else {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003915 if (mac->nparam > 1)
3916 n = (n + mac->rotate) % mac->nparam;
3917 tt = mac->params[n];
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003918 }
3919 if (tt) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003920 for (i = 0; i < mac->paramlen[n]; i++) {
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003921 *tail = new_Token(NULL, tt->type, tt->text, 0);
3922 tail = &(*tail)->next;
3923 tt = tt->next;
3924 }
3925 }
3926 text = NULL; /* we've done it here */
3927 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003928 }
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003929 } else {
3930 /*
3931 * seems we have a parameters range here
3932 */
3933 Token *head, **last;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003934 head = expand_mmac_params_range(mac, t, &last);
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003935 if (head != t) {
3936 *tail = head;
3937 *last = tline;
3938 tline = head;
3939 text = NULL;
3940 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00003941 }
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003942 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00003943 if (!text) {
3944 delete_Token(t);
3945 } else {
3946 *tail = t;
3947 tail = &t->next;
3948 t->type = type;
3949 nasm_free(t->text);
3950 t->text = text;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003951 t->a.mac = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003952 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003953 changed = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003954 continue;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003955 } else if (tline->type == TOK_INDIRECT) {
3956 t = tline;
3957 tline = tline->next;
3958 tt = tokenize(t->text);
3959 tt = expand_mmac_params(tt);
3960 tt = expand_smacro(tt);
3961 *tail = tt;
3962 while (tt) {
3963 tt->a.mac = NULL; /* Necessary? */
3964 tail = &tt->next;
3965 tt = tt->next;
3966 }
3967 delete_Token(t);
3968 changed = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003969 } else {
3970 t = *tail = tline;
3971 tline = tline->next;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003972 t->a.mac = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003973 tail = &t->next;
3974 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00003975 }
H. Peter Anvineba20a72002-04-30 20:53:55 +00003976 *tail = NULL;
H. Peter Anvin67c63722008-10-26 23:49:00 -07003977
Cyrill Gorcunovc6a742c2011-06-27 01:23:09 +04003978 if (changed) {
3979 const struct tokseq_match t[] = {
3980 {
3981 PP_CONCAT_MASK(TOK_ID) |
3982 PP_CONCAT_MASK(TOK_FLOAT), /* head */
3983 PP_CONCAT_MASK(TOK_ID) |
3984 PP_CONCAT_MASK(TOK_NUMBER) |
3985 PP_CONCAT_MASK(TOK_FLOAT) |
3986 PP_CONCAT_MASK(TOK_OTHER) /* tail */
3987 },
3988 {
3989 PP_CONCAT_MASK(TOK_NUMBER), /* head */
3990 PP_CONCAT_MASK(TOK_NUMBER) /* tail */
3991 }
3992 };
3993 paste_tokens(&thead, t, ARRAY_SIZE(t), false);
3994 }
H. Peter Anvin6125b622009-04-08 14:02:25 -07003995
H. Peter Anvin76690a12002-04-30 20:52:49 +00003996 return thead;
3997}
3998
3999/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004000 * Expand all single-line macro calls made in the given line.
4001 * Return the expanded version of the line. The original is deemed
4002 * to be destroyed in the process. (In reality we'll just move
4003 * Tokens from input to output a lot of the time, rather than
4004 * actually bothering to destroy and replicate.)
4005 */
H. Peter Anvincb1cf592007-11-19 12:26:50 -08004006
H. Peter Anvine2c80182005-01-15 22:15:51 +00004007static Token *expand_smacro(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004008{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004009 Token *t, *tt, *mstart, **tail, *thead;
H. Peter Anvineba20a72002-04-30 20:53:55 +00004010 SMacro *head = NULL, *m;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004011 Token **params;
4012 int *paramsize;
H. Peter Anvin25a99342007-09-22 17:45:45 -07004013 unsigned int nparam, sparam;
H. Peter Anvind784a082009-04-20 14:01:18 -07004014 int brackets;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004015 Token *org_tline = tline;
4016 Context *ctx;
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08004017 const char *mname;
H. Peter Anvin2a15e692007-11-19 13:14:59 -08004018 int deadman = DEADMAN_LIMIT;
H. Peter Anvin8287daf2009-07-07 16:00:58 -07004019 bool expanded;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004020
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004021 /*
4022 * Trick: we should avoid changing the start token pointer since it can
4023 * be contained in "next" field of other token. Because of this
4024 * we allocate a copy of first token and work with it; at the end of
4025 * routine we copy it back
4026 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004027 if (org_tline) {
Cyrill Gorcunoved4a8052010-04-09 15:40:35 +04004028 tline = new_Token(org_tline->next, org_tline->type,
4029 org_tline->text, 0);
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004030 tline->a.mac = org_tline->a.mac;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004031 nasm_free(org_tline->text);
4032 org_tline->text = NULL;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004033 }
4034
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004035 expanded = true; /* Always expand %+ at least once */
H. Peter Anvin8287daf2009-07-07 16:00:58 -07004036
H. Peter Anvincb1cf592007-11-19 12:26:50 -08004037again:
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004038 thead = NULL;
Cyrill Gorcunoved4a8052010-04-09 15:40:35 +04004039 tail = &thead;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004040
H. Peter Anvine2c80182005-01-15 22:15:51 +00004041 while (tline) { /* main token loop */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004042 if (!--deadman) {
4043 error(ERR_NONFATAL, "interminable macro recursion");
Cyrill Gorcunovbd38c8f2009-11-21 11:11:23 +03004044 goto err;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004045 }
H. Peter Anvincb1cf592007-11-19 12:26:50 -08004046
H. Peter Anvine2c80182005-01-15 22:15:51 +00004047 if ((mname = tline->text)) {
4048 /* if this token is a local macro, look in local context */
Cyrill Gorcunovc56d9ad2010-02-11 15:12:19 +03004049 if (tline->type == TOK_ID) {
4050 head = (SMacro *)hash_findix(&smacros, mname);
4051 } else if (tline->type == TOK_PREPROC_ID) {
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04004052 ctx = get_ctx(mname, &mname);
Cyrill Gorcunovc56d9ad2010-02-11 15:12:19 +03004053 head = ctx ? (SMacro *)hash_findix(&ctx->localmac, mname) : NULL;
4054 } else
4055 head = NULL;
H. Peter Anvin072771e2008-05-22 13:17:51 -07004056
H. Peter Anvine2c80182005-01-15 22:15:51 +00004057 /*
4058 * We've hit an identifier. As in is_mmacro below, we first
4059 * check whether the identifier is a single-line macro at
4060 * all, then think about checking for parameters if
4061 * necessary.
4062 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004063 list_for_each(m, head)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004064 if (!mstrcmp(m->name, mname, m->casesense))
4065 break;
4066 if (m) {
4067 mstart = tline;
4068 params = NULL;
4069 paramsize = NULL;
4070 if (m->nparam == 0) {
4071 /*
4072 * Simple case: the macro is parameterless. Discard the
4073 * one token that the macro call took, and push the
4074 * expansion back on the to-do stack.
4075 */
4076 if (!m->expansion) {
4077 if (!strcmp("__FILE__", m->name)) {
4078 int32_t num = 0;
4079 char *file = NULL;
4080 src_get(&num, &file);
4081 tline->text = nasm_quote(file, strlen(file));
4082 tline->type = TOK_STRING;
4083 nasm_free(file);
H. Peter Anvine2c80182005-01-15 22:15:51 +00004084 continue;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004085 }
4086 if (!strcmp("__LINE__", m->name)) {
4087 nasm_free(tline->text);
4088 make_tok_num(tline, src_get_linnum());
4089 continue;
4090 }
4091 if (!strcmp("__BITS__", m->name)) {
4092 nasm_free(tline->text);
4093 make_tok_num(tline, globalbits);
4094 continue;
4095 }
4096 tline = delete_Token(tline);
4097 continue;
4098 }
4099 } else {
4100 /*
4101 * Complicated case: at least one macro with this name
H. Peter Anvine2c80182005-01-15 22:15:51 +00004102 * exists and takes parameters. We must find the
4103 * parameters in the call, count them, find the SMacro
4104 * that corresponds to that form of the macro call, and
4105 * substitute for the parameters when we expand. What a
4106 * pain.
4107 */
4108 /*tline = tline->next;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004109 skip_white_(tline); */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004110 do {
4111 t = tline->next;
4112 while (tok_type_(t, TOK_SMAC_END)) {
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004113 t->a.mac->in_progress = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004114 t->text = NULL;
4115 t = tline->next = delete_Token(t);
4116 }
4117 tline = t;
4118 } while (tok_type_(tline, TOK_WHITESPACE));
4119 if (!tok_is_(tline, "(")) {
4120 /*
4121 * This macro wasn't called with parameters: ignore
4122 * the call. (Behaviour borrowed from gnu cpp.)
4123 */
4124 tline = mstart;
4125 m = NULL;
4126 } else {
4127 int paren = 0;
4128 int white = 0;
4129 brackets = 0;
4130 nparam = 0;
4131 sparam = PARAM_DELTA;
4132 params = nasm_malloc(sparam * sizeof(Token *));
4133 params[0] = tline->next;
4134 paramsize = nasm_malloc(sparam * sizeof(int));
4135 paramsize[0] = 0;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07004136 while (true) { /* parameter loop */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004137 /*
4138 * For some unusual expansions
4139 * which concatenates function call
4140 */
4141 t = tline->next;
4142 while (tok_type_(t, TOK_SMAC_END)) {
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004143 t->a.mac->in_progress = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004144 t->text = NULL;
4145 t = tline->next = delete_Token(t);
4146 }
4147 tline = t;
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00004148
H. Peter Anvine2c80182005-01-15 22:15:51 +00004149 if (!tline) {
4150 error(ERR_NONFATAL,
4151 "macro call expects terminating `)'");
4152 break;
4153 }
4154 if (tline->type == TOK_WHITESPACE
4155 && brackets <= 0) {
4156 if (paramsize[nparam])
4157 white++;
4158 else
4159 params[nparam] = tline->next;
4160 continue; /* parameter loop */
4161 }
4162 if (tline->type == TOK_OTHER
4163 && tline->text[1] == 0) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004164 char ch = tline->text[0];
H. Peter Anvine2c80182005-01-15 22:15:51 +00004165 if (ch == ',' && !paren && brackets <= 0) {
4166 if (++nparam >= sparam) {
4167 sparam += PARAM_DELTA;
4168 params = nasm_realloc(params,
Cyrill Gorcunoved4a8052010-04-09 15:40:35 +04004169 sparam * sizeof(Token *));
4170 paramsize = nasm_realloc(paramsize,
4171 sparam * sizeof(int));
H. Peter Anvine2c80182005-01-15 22:15:51 +00004172 }
4173 params[nparam] = tline->next;
4174 paramsize[nparam] = 0;
4175 white = 0;
4176 continue; /* parameter loop */
4177 }
4178 if (ch == '{' &&
4179 (brackets > 0 || (brackets == 0 &&
4180 !paramsize[nparam])))
4181 {
4182 if (!(brackets++)) {
4183 params[nparam] = tline->next;
4184 continue; /* parameter loop */
4185 }
4186 }
4187 if (ch == '}' && brackets > 0)
4188 if (--brackets == 0) {
4189 brackets = -1;
4190 continue; /* parameter loop */
4191 }
4192 if (ch == '(' && !brackets)
4193 paren++;
4194 if (ch == ')' && brackets <= 0)
4195 if (--paren < 0)
4196 break;
4197 }
4198 if (brackets < 0) {
4199 brackets = 0;
4200 error(ERR_NONFATAL, "braces do not "
4201 "enclose all of macro parameter");
4202 }
4203 paramsize[nparam] += white + 1;
4204 white = 0;
4205 } /* parameter loop */
4206 nparam++;
4207 while (m && (m->nparam != nparam ||
4208 mstrcmp(m->name, mname,
4209 m->casesense)))
4210 m = m->next;
4211 if (!m)
H. Peter Anvin917a3492008-09-24 09:14:49 -07004212 error(ERR_WARNING|ERR_PASS1|ERR_WARN_MNP,
H. Peter Anvine2c80182005-01-15 22:15:51 +00004213 "macro `%s' exists, "
4214 "but not taking %d parameters",
4215 mstart->text, nparam);
4216 }
4217 }
4218 if (m && m->in_progress)
4219 m = NULL;
4220 if (!m) { /* in progess or didn't find '(' or wrong nparam */
H. Peter Anvin70653092007-10-19 14:42:29 -07004221 /*
H. Peter Anvine2c80182005-01-15 22:15:51 +00004222 * Design question: should we handle !tline, which
4223 * indicates missing ')' here, or expand those
4224 * macros anyway, which requires the (t) test a few
H. Peter Anvin70653092007-10-19 14:42:29 -07004225 * lines down?
H. Peter Anvine2c80182005-01-15 22:15:51 +00004226 */
4227 nasm_free(params);
4228 nasm_free(paramsize);
4229 tline = mstart;
4230 } else {
4231 /*
4232 * Expand the macro: we are placed on the last token of the
4233 * call, so that we can easily split the call from the
4234 * following tokens. We also start by pushing an SMAC_END
4235 * token for the cycle removal.
4236 */
4237 t = tline;
4238 if (t) {
4239 tline = t->next;
4240 t->next = NULL;
4241 }
4242 tt = new_Token(tline, TOK_SMAC_END, NULL, 0);
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004243 tt->a.mac = m;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07004244 m->in_progress = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004245 tline = tt;
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04004246 list_for_each(t, m->expansion) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004247 if (t->type >= TOK_SMAC_PARAM) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00004248 Token *pcopy = tline, **ptail = &pcopy;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004249 Token *ttt, *pt;
4250 int i;
H. Peter Anvin734b1882002-04-30 21:01:08 +00004251
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004252 ttt = params[t->type - TOK_SMAC_PARAM];
4253 i = paramsize[t->type - TOK_SMAC_PARAM];
4254 while (--i >= 0) {
4255 pt = *ptail = new_Token(tline, ttt->type,
4256 ttt->text, 0);
4257 ptail = &pt->next;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004258 ttt = ttt->next;
4259 }
4260 tline = pcopy;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004261 } else if (t->type == TOK_PREPROC_Q) {
4262 tt = new_Token(tline, TOK_ID, mname, 0);
4263 tline = tt;
4264 } else if (t->type == TOK_PREPROC_QQ) {
4265 tt = new_Token(tline, TOK_ID, m->name, 0);
4266 tline = tt;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004267 } else {
4268 tt = new_Token(tline, t->type, t->text, 0);
4269 tline = tt;
4270 }
4271 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00004272
H. Peter Anvine2c80182005-01-15 22:15:51 +00004273 /*
4274 * Having done that, get rid of the macro call, and clean
4275 * up the parameters.
4276 */
4277 nasm_free(params);
4278 nasm_free(paramsize);
4279 free_tlist(mstart);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004280 expanded = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004281 continue; /* main token loop */
4282 }
4283 }
4284 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004285
H. Peter Anvine2c80182005-01-15 22:15:51 +00004286 if (tline->type == TOK_SMAC_END) {
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004287 tline->a.mac->in_progress = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004288 tline = delete_Token(tline);
4289 } else {
4290 t = *tail = tline;
4291 tline = tline->next;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004292 t->a.mac = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004293 t->next = NULL;
4294 tail = &t->next;
4295 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004296 }
4297
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004298 /*
4299 * Now scan the entire line and look for successive TOK_IDs that resulted
Keith Kaniosb7a89542007-04-12 02:40:54 +00004300 * after expansion (they can't be produced by tokenize()). The successive
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004301 * TOK_IDs should be concatenated.
4302 * Also we look for %+ tokens and concatenate the tokens before and after
4303 * them (without white spaces in between).
4304 */
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +04004305 if (expanded) {
Cyrill Gorcunovc6a742c2011-06-27 01:23:09 +04004306 const struct tokseq_match t[] = {
4307 {
4308 PP_CONCAT_MASK(TOK_ID) |
4309 PP_CONCAT_MASK(TOK_PREPROC_ID), /* head */
4310 PP_CONCAT_MASK(TOK_ID) |
4311 PP_CONCAT_MASK(TOK_PREPROC_ID) |
4312 PP_CONCAT_MASK(TOK_NUMBER) /* tail */
4313 }
4314 };
4315 if (paste_tokens(&thead, t, ARRAY_SIZE(t), true)) {
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +04004316 /*
4317 * If we concatenated something, *and* we had previously expanded
4318 * an actual macro, scan the lines again for macros...
4319 */
4320 tline = thead;
4321 expanded = false;
4322 goto again;
4323 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00004324 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004325
Cyrill Gorcunovbd38c8f2009-11-21 11:11:23 +03004326err:
H. Peter Anvine2c80182005-01-15 22:15:51 +00004327 if (org_tline) {
4328 if (thead) {
4329 *org_tline = *thead;
4330 /* since we just gave text to org_line, don't free it */
4331 thead->text = NULL;
4332 delete_Token(thead);
4333 } else {
4334 /* the expression expanded to empty line;
4335 we can't return NULL for some reasons
4336 we just set the line to a single WHITESPACE token. */
4337 memset(org_tline, 0, sizeof(*org_tline));
4338 org_tline->text = NULL;
4339 org_tline->type = TOK_WHITESPACE;
4340 }
4341 thead = org_tline;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004342 }
4343
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004344 return thead;
4345}
4346
4347/*
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004348 * Similar to expand_smacro but used exclusively with macro identifiers
4349 * right before they are fetched in. The reason is that there can be
4350 * identifiers consisting of several subparts. We consider that if there
4351 * are more than one element forming the name, user wants a expansion,
4352 * otherwise it will be left as-is. Example:
4353 *
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004354 * %define %$abc cde
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004355 *
4356 * the identifier %$abc will be left as-is so that the handler for %define
4357 * will suck it and define the corresponding value. Other case:
4358 *
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004359 * %define _%$abc cde
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004360 *
4361 * In this case user wants name to be expanded *before* %define starts
4362 * working, so we'll expand %$abc into something (if it has a value;
4363 * otherwise it will be left as-is) then concatenate all successive
4364 * PP_IDs into one.
4365 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004366static Token *expand_id(Token * tline)
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004367{
4368 Token *cur, *oldnext = NULL;
4369
H. Peter Anvin734b1882002-04-30 21:01:08 +00004370 if (!tline || !tline->next)
H. Peter Anvine2c80182005-01-15 22:15:51 +00004371 return tline;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004372
4373 cur = tline;
4374 while (cur->next &&
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004375 (cur->next->type == TOK_ID ||
4376 cur->next->type == TOK_PREPROC_ID
4377 || cur->next->type == TOK_NUMBER))
H. Peter Anvine2c80182005-01-15 22:15:51 +00004378 cur = cur->next;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004379
4380 /* If identifier consists of just one token, don't expand */
4381 if (cur == tline)
H. Peter Anvine2c80182005-01-15 22:15:51 +00004382 return tline;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004383
H. Peter Anvine2c80182005-01-15 22:15:51 +00004384 if (cur) {
4385 oldnext = cur->next; /* Detach the tail past identifier */
4386 cur->next = NULL; /* so that expand_smacro stops here */
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004387 }
4388
H. Peter Anvin734b1882002-04-30 21:01:08 +00004389 tline = expand_smacro(tline);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004390
H. Peter Anvine2c80182005-01-15 22:15:51 +00004391 if (cur) {
4392 /* expand_smacro possibly changhed tline; re-scan for EOL */
4393 cur = tline;
4394 while (cur && cur->next)
4395 cur = cur->next;
4396 if (cur)
4397 cur->next = oldnext;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004398 }
4399
4400 return tline;
4401}
4402
4403/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004404 * Determine whether the given line constitutes a multi-line macro
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004405 * call, and return the MMacro structure called if so. Doesn't have
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004406 * to check for an initial label - that's taken care of in
4407 * expand_mmacro - but must check numbers of parameters. Guaranteed
4408 * to be called with tline->type == TOK_ID, so the putative macro
4409 * name is easy to find.
4410 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004411static MMacro *is_mmacro(Token * tline, Token *** params_array)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004412{
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004413 MMacro *head, *m;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004414 Token **params;
4415 int nparam;
4416
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004417 head = (MMacro *) hash_findix(&mmacros, tline->text);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004418
4419 /*
4420 * Efficiency: first we see if any macro exists with the given
4421 * name. If not, we can return NULL immediately. _Then_ we
4422 * count the parameters, and then we look further along the
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004423 * list if necessary to find the proper MMacro.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004424 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004425 list_for_each(m, head)
4426 if (!mstrcmp(m->name, tline->text, m->casesense))
H. Peter Anvine2c80182005-01-15 22:15:51 +00004427 break;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004428 if (!m)
H. Peter Anvine2c80182005-01-15 22:15:51 +00004429 return NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004430
4431 /*
4432 * OK, we have a potential macro. Count and demarcate the
4433 * parameters.
4434 */
H. Peter Anvin734b1882002-04-30 21:01:08 +00004435 count_mmac_params(tline->next, &nparam, &params);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004436
4437 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004438 * So we know how many parameters we've got. Find the MMacro
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004439 * structure that handles this number.
4440 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004441 while (m) {
4442 if (m->nparam_min <= nparam
4443 && (m->plus || nparam <= m->nparam_max)) {
4444 /*
4445 * This one is right. Just check if cycle removal
4446 * prohibits us using it before we actually celebrate...
4447 */
4448 if (m->in_progress > m->max_depth) {
4449 if (m->max_depth > 0) {
4450 error(ERR_WARNING,
4451 "reached maximum recursion depth of %i",
4452 m->max_depth);
4453 }
4454 nasm_free(params);
4455 return NULL;
4456 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00004457 /*
4458 * It's right, and we can use it. Add its default
4459 * parameters to the end of our list if necessary.
4460 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004461 if (m->defaults && nparam < m->nparam_min + m->ndefs) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00004462 params =
4463 nasm_realloc(params,
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004464 ((m->nparam_min + m->ndefs +
H. Peter Anvine2c80182005-01-15 22:15:51 +00004465 1) * sizeof(*params)));
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004466 while (nparam < m->nparam_min + m->ndefs) {
4467 params[nparam] = m->defaults[nparam - m->nparam_min];
H. Peter Anvine2c80182005-01-15 22:15:51 +00004468 nparam++;
4469 }
4470 }
4471 /*
4472 * If we've gone over the maximum parameter count (and
4473 * we're in Plus mode), ignore parameters beyond
4474 * nparam_max.
4475 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004476 if (m->plus && nparam > m->nparam_max)
4477 nparam = m->nparam_max;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004478 /*
4479 * Then terminate the parameter list, and leave.
4480 */
4481 if (!params) { /* need this special case */
4482 params = nasm_malloc(sizeof(*params));
4483 nparam = 0;
4484 }
4485 params[nparam] = NULL;
4486 *params_array = params;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004487 return m;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004488 }
4489 /*
4490 * This one wasn't right: look for the next one with the
4491 * same name.
4492 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004493 list_for_each(m, m->next)
4494 if (!mstrcmp(m->name, tline->text, m->casesense))
H. Peter Anvine2c80182005-01-15 22:15:51 +00004495 break;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004496 }
4497
4498 /*
4499 * After all that, we didn't find one with the right number of
4500 * parameters. Issue a warning, and fail to expand the macro.
4501 */
H. Peter Anvin917a3492008-09-24 09:14:49 -07004502 error(ERR_WARNING|ERR_PASS1|ERR_WARN_MNP,
H. Peter Anvine2c80182005-01-15 22:15:51 +00004503 "macro `%s' exists, but not taking %d parameters",
4504 tline->text, nparam);
H. Peter Anvin734b1882002-04-30 21:01:08 +00004505 nasm_free(params);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004506 return NULL;
4507}
4508
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004509
4510/*
4511 * Save MMacro invocation specific fields in
4512 * preparation for a recursive macro expansion
4513 */
4514static void push_mmacro(MMacro *m)
4515{
4516 MMacroInvocation *i;
4517
4518 i = nasm_malloc(sizeof(MMacroInvocation));
4519 i->prev = m->prev;
4520 i->params = m->params;
4521 i->iline = m->iline;
4522 i->nparam = m->nparam;
4523 i->rotate = m->rotate;
4524 i->paramlen = m->paramlen;
4525 i->unique = m->unique;
4526 i->condcnt = m->condcnt;
4527 m->prev = i;
4528}
4529
4530
4531/*
4532 * Restore MMacro invocation specific fields that were
4533 * saved during a previous recursive macro expansion
4534 */
4535static void pop_mmacro(MMacro *m)
4536{
4537 MMacroInvocation *i;
4538
4539 if (m->prev) {
4540 i = m->prev;
4541 m->prev = i->prev;
4542 m->params = i->params;
4543 m->iline = i->iline;
4544 m->nparam = i->nparam;
4545 m->rotate = i->rotate;
4546 m->paramlen = i->paramlen;
4547 m->unique = i->unique;
4548 m->condcnt = i->condcnt;
4549 nasm_free(i);
4550 }
4551}
4552
4553
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004554/*
4555 * Expand the multi-line macro call made by the given line, if
4556 * there is one to be expanded. If there is, push the expansion on
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004557 * istk->expansion and return 1. Otherwise return 0.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004558 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004559static int expand_mmacro(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004560{
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004561 Token *startline = tline;
H. Peter Anvineba20a72002-04-30 20:53:55 +00004562 Token *label = NULL;
4563 int dont_prepend = 0;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004564 Token **params, *t, *tt;
4565 MMacro *m;
4566 Line *l, *ll;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004567 int i, nparam, *paramlen;
H. Peter Anvinc751e862008-06-09 10:18:45 -07004568 const char *mname;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004569
4570 t = tline;
H. Peter Anvineba20a72002-04-30 20:53:55 +00004571 skip_white_(t);
H. Peter Anvince2233b2008-05-25 21:57:00 -07004572 /* if (!tok_type_(t, TOK_ID)) Lino 02/25/02 */
H. Peter Anvindce1e2f2002-04-30 21:06:37 +00004573 if (!tok_type_(t, TOK_ID) && !tok_type_(t, TOK_PREPROC_ID))
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004574 return 0;
4575 m = is_mmacro(t, &params);
4576 if (m) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004577 mname = t->text;
H. Peter Anvinc751e862008-06-09 10:18:45 -07004578 } else {
H. Peter Anvine2c80182005-01-15 22:15:51 +00004579 Token *last;
4580 /*
4581 * We have an id which isn't a macro call. We'll assume
4582 * it might be a label; we'll also check to see if a
4583 * colon follows it. Then, if there's another id after
4584 * that lot, we'll check it again for macro-hood.
4585 */
4586 label = last = t;
4587 t = t->next;
4588 if (tok_type_(t, TOK_WHITESPACE))
4589 last = t, t = t->next;
4590 if (tok_is_(t, ":")) {
4591 dont_prepend = 1;
4592 last = t, t = t->next;
4593 if (tok_type_(t, TOK_WHITESPACE))
4594 last = t, t = t->next;
4595 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004596 if (!tok_type_(t, TOK_ID) || !(m = is_mmacro(t, &params)))
4597 return 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004598 last->next = NULL;
Keith Kanios891775e2009-07-11 06:08:54 -05004599 mname = t->text;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004600 tline = t;
H. Peter Anvineba20a72002-04-30 20:53:55 +00004601 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004602
4603 /*
4604 * Fix up the parameters: this involves stripping leading and
4605 * trailing whitespace, then stripping braces if they are
4606 * present.
4607 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004608 for (nparam = 0; params[nparam]; nparam++) ;
H. Peter Anvin734b1882002-04-30 21:01:08 +00004609 paramlen = nparam ? nasm_malloc(nparam * sizeof(*paramlen)) : NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004610
H. Peter Anvine2c80182005-01-15 22:15:51 +00004611 for (i = 0; params[i]; i++) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07004612 int brace = false;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004613 int comma = (!m->plus || i < nparam - 1);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004614
H. Peter Anvine2c80182005-01-15 22:15:51 +00004615 t = params[i];
4616 skip_white_(t);
4617 if (tok_is_(t, "{"))
H. Peter Anvin6867acc2007-10-10 14:58:45 -07004618 t = t->next, brace = true, comma = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004619 params[i] = t;
4620 paramlen[i] = 0;
4621 while (t) {
4622 if (comma && t->type == TOK_OTHER && !strcmp(t->text, ","))
4623 break; /* ... because we have hit a comma */
4624 if (comma && t->type == TOK_WHITESPACE
4625 && tok_is_(t->next, ","))
4626 break; /* ... or a space then a comma */
4627 if (brace && t->type == TOK_OTHER && !strcmp(t->text, "}"))
4628 break; /* ... or a brace */
4629 t = t->next;
4630 paramlen[i]++;
4631 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004632 }
4633
4634 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004635 * OK, we have a MMacro structure together with a set of
4636 * parameters. We must now go through the expansion and push
4637 * copies of each Line on to istk->expansion. Substitution of
H. Peter Anvin76690a12002-04-30 20:52:49 +00004638 * parameter tokens and macro-local tokens doesn't get done
4639 * until the single-line macro substitution process; this is
4640 * because delaying them allows us to change the semantics
4641 * later through %rotate.
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004642 *
4643 * First, push an end marker on to istk->expansion, mark this
4644 * macro as in progress, and set up its invocation-specific
4645 * variables.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004646 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004647 ll = nasm_malloc(sizeof(Line));
4648 ll->next = istk->expansion;
4649 ll->finishes = m;
4650 ll->first = NULL;
4651 istk->expansion = ll;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004652
4653 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004654 * Save the previous MMacro expansion in the case of
4655 * macro recursion
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004656 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004657 if (m->max_depth && m->in_progress)
4658 push_mmacro(m);
4659
4660 m->in_progress ++;
4661 m->params = params;
4662 m->iline = tline;
4663 m->nparam = nparam;
4664 m->rotate = 0;
4665 m->paramlen = paramlen;
4666 m->unique = unique++;
4667 m->lineno = 0;
4668 m->condcnt = 0;
4669
4670 m->next_active = istk->mstk;
4671 istk->mstk = m;
4672
4673 list_for_each(l, m->expansion) {
4674 Token **tail;
4675
4676 ll = nasm_malloc(sizeof(Line));
4677 ll->finishes = NULL;
4678 ll->next = istk->expansion;
4679 istk->expansion = ll;
4680 tail = &ll->first;
4681
4682 list_for_each(t, l->first) {
4683 Token *x = t;
4684 switch (t->type) {
4685 case TOK_PREPROC_Q:
4686 tt = *tail = new_Token(NULL, TOK_ID, mname, 0);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004687 break;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004688 case TOK_PREPROC_QQ:
4689 tt = *tail = new_Token(NULL, TOK_ID, m->name, 0);
4690 break;
4691 case TOK_PREPROC_ID:
4692 if (t->text[1] == '0' && t->text[2] == '0') {
4693 dont_prepend = -1;
4694 x = label;
4695 if (!x)
4696 continue;
4697 }
4698 /* fall through */
4699 default:
4700 tt = *tail = new_Token(NULL, x->type, x->text, 0);
4701 break;
4702 }
4703 tail = &tt->next;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004704 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004705 *tail = NULL;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004706 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004707
4708 /*
H. Peter Anvineba20a72002-04-30 20:53:55 +00004709 * If we had a label, push it on as the first line of
4710 * the macro expansion.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004711 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004712 if (label) {
4713 if (dont_prepend < 0)
4714 free_tlist(startline);
4715 else {
4716 ll = nasm_malloc(sizeof(Line));
4717 ll->finishes = NULL;
4718 ll->next = istk->expansion;
4719 istk->expansion = ll;
4720 ll->first = startline;
4721 if (!dont_prepend) {
4722 while (label->next)
4723 label = label->next;
4724 label->next = tt = new_Token(NULL, TOK_OTHER, ":", 0);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004725 }
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004726 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004727 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004728
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004729 list->uplevel(m->nolist ? LIST_MACRO_NOLIST : LIST_MACRO);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004730
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004731 return 1;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004732}
4733
Victor van den Elzen3b404c02008-09-18 13:51:36 +02004734/* The function that actually does the error reporting */
4735static void verror(int severity, const char *fmt, va_list arg)
4736{
4737 char buff[1024];
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004738 MMacro *mmac = NULL;
4739 int delta = 0;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02004740
4741 vsnprintf(buff, sizeof(buff), fmt, arg);
4742
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004743 /* get %macro name */
4744 if (istk && istk->mstk) {
4745 mmac = istk->mstk;
4746 /* but %rep blocks should be skipped */
4747 while (mmac && !mmac->name)
4748 mmac = mmac->next_active, delta++;
Cyrill Gorcunov9900c6b2011-10-09 18:58:46 +04004749 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004750
4751 if (mmac)
4752 nasm_error(severity, "(%s:%d) %s",
4753 mmac->name, mmac->lineno - delta, buff);
4754 else
4755 nasm_error(severity, "%s", buff);
Victor van den Elzen3b404c02008-09-18 13:51:36 +02004756}
4757
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004758/*
4759 * Since preprocessor always operate only on the line that didn't
H. Peter Anvin917a3492008-09-24 09:14:49 -07004760 * arrived yet, we should always use ERR_OFFBY1.
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004761 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004762static void error(int severity, const char *fmt, ...)
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004763{
4764 va_list arg;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004765
4766 /* If we're in a dead branch of IF or something like it, ignore the error */
4767 if (istk && istk->conds && !emitting(istk->conds->state))
4768 return;
4769
H. Peter Anvin734b1882002-04-30 21:01:08 +00004770 va_start(arg, fmt);
Victor van den Elzen3b404c02008-09-18 13:51:36 +02004771 verror(severity, fmt, arg);
H. Peter Anvin734b1882002-04-30 21:01:08 +00004772 va_end(arg);
Victor van den Elzen3b404c02008-09-18 13:51:36 +02004773}
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004774
Victor van den Elzen3b404c02008-09-18 13:51:36 +02004775/*
4776 * Because %else etc are evaluated in the state context
4777 * of the previous branch, errors might get lost with error():
4778 * %if 0 ... %else trailing garbage ... %endif
4779 * So %else etc should report errors with this function.
4780 */
4781static void error_precond(int severity, const char *fmt, ...)
4782{
4783 va_list arg;
4784
4785 /* Only ignore the error if it's really in a dead branch */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004786 if (istk && istk->conds && istk->conds->state == COND_NEVER)
Victor van den Elzen3b404c02008-09-18 13:51:36 +02004787 return;
4788
4789 va_start(arg, fmt);
4790 verror(severity, fmt, arg);
4791 va_end(arg);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004792}
4793
H. Peter Anvin734b1882002-04-30 21:01:08 +00004794static void
H. Peter Anvindbb640b2009-07-18 18:57:16 -07004795pp_reset(char *file, int apass, ListGen * listgen, StrList **deplist)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004796{
H. Peter Anvin7383b402008-09-24 10:20:40 -07004797 Token *t;
4798
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004799 cstk = NULL;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004800 istk = nasm_malloc(sizeof(Include));
4801 istk->next = NULL;
4802 istk->conds = NULL;
4803 istk->expansion = NULL;
4804 istk->mstk = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004805 istk->fp = fopen(file, "r");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004806 istk->fname = NULL;
H. Peter Anvineba20a72002-04-30 20:53:55 +00004807 src_set_fname(nasm_strdup(file));
4808 src_set_linnum(0);
4809 istk->lineinc = 1;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004810 if (!istk->fp)
H. Peter Anvin917a3492008-09-24 09:14:49 -07004811 error(ERR_FATAL|ERR_NOFILE, "unable to open input file `%s'",
H. Peter Anvine2c80182005-01-15 22:15:51 +00004812 file);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004813 defining = NULL;
Charles Crayned4200be2008-07-12 16:42:33 -07004814 nested_mac_count = 0;
4815 nested_rep_count = 0;
H. Peter Anvin97a23472007-09-16 17:57:25 -07004816 init_macros();
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004817 unique = 0;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004818 if (tasm_compatible_mode) {
H. Peter Anvina4835d42008-05-20 14:21:29 -07004819 stdmacpos = nasm_stdmac;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004820 } else {
H. Peter Anvina4835d42008-05-20 14:21:29 -07004821 stdmacpos = nasm_stdmac_after_tasm;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004822 }
H. Peter Anvind2456592008-06-19 15:04:18 -07004823 any_extrastdmac = extrastdmac && *extrastdmac;
4824 do_predef = true;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004825 list = listgen;
H. Peter Anvin61f130f2008-09-25 15:45:06 -07004826
4827 /*
4828 * 0 for dependencies, 1 for preparatory passes, 2 for final pass.
4829 * The caller, however, will also pass in 3 for preprocess-only so
4830 * we can set __PASS__ accordingly.
4831 */
4832 pass = apass > 2 ? 2 : apass;
4833
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07004834 dephead = deptail = deplist;
4835 if (deplist) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004836 StrList *sl = nasm_malloc(strlen(file)+1+sizeof sl->next);
4837 sl->next = NULL;
4838 strcpy(sl->str, file);
4839 *deptail = sl;
4840 deptail = &sl->next;
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07004841 }
H. Peter Anvin7383b402008-09-24 10:20:40 -07004842
H. Peter Anvin61f130f2008-09-25 15:45:06 -07004843 /*
4844 * Define the __PASS__ macro. This is defined here unlike
4845 * all the other builtins, because it is special -- it varies between
4846 * passes.
4847 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004848 t = nasm_malloc(sizeof(*t));
4849 t->next = NULL;
H. Peter Anvin61f130f2008-09-25 15:45:06 -07004850 make_tok_num(t, apass);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004851 t->a.mac = NULL;
H. Peter Anvin7383b402008-09-24 10:20:40 -07004852 define_smacro(NULL, "__PASS__", true, 0, t);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004853}
4854
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004855static char *pp_getline(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004856{
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004857 char *line;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004858 Token *tline;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004859
H. Peter Anvine2c80182005-01-15 22:15:51 +00004860 while (1) {
4861 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004862 * Fetch a tokenized line, either from the macro-expansion
H. Peter Anvine2c80182005-01-15 22:15:51 +00004863 * buffer or from the input file.
4864 */
4865 tline = NULL;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004866 while (istk->expansion && istk->expansion->finishes) {
4867 Line *l = istk->expansion;
4868 if (!l->finishes->name && l->finishes->in_progress > 1) {
4869 Line *ll;
H. Peter Anvineba20a72002-04-30 20:53:55 +00004870
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004871 /*
4872 * This is a macro-end marker for a macro with no
4873 * name, which means it's not really a macro at all
4874 * but a %rep block, and the `in_progress' field is
4875 * more than 1, meaning that we still need to
4876 * repeat. (1 means the natural last repetition; 0
4877 * means termination by %exitrep.) We have
4878 * therefore expanded up to the %endrep, and must
4879 * push the whole block on to the expansion buffer
4880 * again. We don't bother to remove the macro-end
4881 * marker: we'd only have to generate another one
4882 * if we did.
4883 */
4884 l->finishes->in_progress--;
4885 list_for_each(l, l->finishes->expansion) {
4886 Token *t, *tt, **tail;
4887
4888 ll = nasm_malloc(sizeof(Line));
4889 ll->next = istk->expansion;
4890 ll->finishes = NULL;
4891 ll->first = NULL;
4892 tail = &ll->first;
4893
4894 list_for_each(t, l->first) {
4895 if (t->text || t->type == TOK_WHITESPACE) {
4896 tt = *tail = new_Token(NULL, t->type, t->text, 0);
4897 tail = &tt->next;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004898 }
4899 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004900
4901 istk->expansion = ll;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004902 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004903 } else {
4904 /*
4905 * Check whether a `%rep' was started and not ended
4906 * within this macro expansion. This can happen and
4907 * should be detected. It's a fatal error because
4908 * I'm too confused to work out how to recover
4909 * sensibly from it.
4910 */
4911 if (defining) {
4912 if (defining->name)
4913 error(ERR_PANIC,
4914 "defining with name in expansion");
4915 else if (istk->mstk->name)
4916 error(ERR_FATAL,
4917 "`%%rep' without `%%endrep' within"
4918 " expansion of macro `%s'",
4919 istk->mstk->name);
4920 }
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004921
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004922 /*
4923 * FIXME: investigate the relationship at this point between
4924 * istk->mstk and l->finishes
4925 */
4926 {
4927 MMacro *m = istk->mstk;
4928 istk->mstk = m->next_active;
4929 if (m->name) {
4930 /*
4931 * This was a real macro call, not a %rep, and
4932 * therefore the parameter information needs to
4933 * be freed.
4934 */
4935 if (m->prev) {
4936 pop_mmacro(m);
4937 l->finishes->in_progress --;
4938 } else {
4939 nasm_free(m->params);
4940 free_tlist(m->iline);
4941 nasm_free(m->paramlen);
4942 l->finishes->in_progress = 0;
4943 }
4944 } else
4945 free_mmacro(m);
4946 }
4947 istk->expansion = l->next;
4948 nasm_free(l);
4949 list->downlevel(LIST_MACRO);
4950 }
4951 }
4952 while (1) { /* until we get a line we can use */
4953
4954 if (istk->expansion) { /* from a macro expansion */
4955 char *p;
4956 Line *l = istk->expansion;
4957 if (istk->mstk)
4958 istk->mstk->lineno++;
4959 tline = l->first;
4960 istk->expansion = l->next;
4961 nasm_free(l);
4962 p = detoken(tline, false);
4963 list->line(LIST_MACRO, p);
4964 nasm_free(p);
4965 break;
4966 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00004967 line = read_line();
4968 if (line) { /* from the current input file */
4969 line = prepreproc(line);
Keith Kaniosb7a89542007-04-12 02:40:54 +00004970 tline = tokenize(line);
H. Peter Anvine2c80182005-01-15 22:15:51 +00004971 nasm_free(line);
4972 break;
4973 }
4974 /*
4975 * The current file has ended; work down the istk
4976 */
4977 {
4978 Include *i = istk;
4979 fclose(i->fp);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004980 if (i->conds) {
4981 /* nasm_error can't be conditionally suppressed */
4982 nasm_error(ERR_FATAL,
4983 "expected `%%endif' before end of file");
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004984 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00004985 /* only set line and file name if there's a next node */
4986 if (i->next) {
4987 src_set_linnum(i->lineno);
Cyrill Gorcunovd34a1082011-03-07 11:23:08 +03004988 nasm_free(src_set_fname(nasm_strdup(i->fname)));
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004989 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00004990 istk = i->next;
4991 list->downlevel(LIST_INCLUDE);
4992 nasm_free(i);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004993 if (!istk)
4994 return NULL;
4995 if (istk->expansion && istk->expansion->finishes)
4996 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004997 }
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004998 }
4999
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005000 /*
5001 * We must expand MMacro parameters and MMacro-local labels
5002 * _before_ we plunge into directive processing, to cope
5003 * with things like `%define something %1' such as STRUC
5004 * uses. Unless we're _defining_ a MMacro, in which case
5005 * those tokens should be left alone to go into the
5006 * definition; and unless we're in a non-emitting
5007 * condition, in which case we don't want to meddle with
5008 * anything.
5009 */
5010 if (!defining && !(istk->conds && !emitting(istk->conds->state))
5011 && !(istk->mstk && !istk->mstk->in_progress)) {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005012 tline = expand_mmac_params(tline);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005013 }
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005014
H. Peter Anvine2c80182005-01-15 22:15:51 +00005015 /*
5016 * Check the line to see if it's a preprocessor directive.
5017 */
5018 if (do_directive(tline) == DIRECTIVE_FOUND) {
5019 continue;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005020 } else if (defining) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00005021 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005022 * We're defining a multi-line macro. We emit nothing
5023 * at all, and just
5024 * shove the tokenized line on to the macro definition.
H. Peter Anvine2c80182005-01-15 22:15:51 +00005025 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005026 Line *l = nasm_malloc(sizeof(Line));
5027 l->next = defining->expansion;
5028 l->first = tline;
5029 l->finishes = NULL;
5030 defining->expansion = l;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005031 continue;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005032 } else if (istk->conds && !emitting(istk->conds->state)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00005033 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005034 * We're in a non-emitting branch of a condition block.
H. Peter Anvine2c80182005-01-15 22:15:51 +00005035 * Emit nothing at all, not even a blank line: when we
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005036 * emerge from the condition we'll give a line-number
H. Peter Anvine2c80182005-01-15 22:15:51 +00005037 * directive so we keep our place correctly.
5038 */
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005039 free_tlist(tline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00005040 continue;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005041 } else if (istk->mstk && !istk->mstk->in_progress) {
5042 /*
5043 * We're in a %rep block which has been terminated, so
5044 * we're walking through to the %endrep without
5045 * emitting anything. Emit nothing at all, not even a
5046 * blank line: when we emerge from the %rep block we'll
5047 * give a line-number directive so we keep our place
5048 * correctly.
5049 */
5050 free_tlist(tline);
5051 continue;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005052 } else {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005053 tline = expand_smacro(tline);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005054 if (!expand_mmacro(tline)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00005055 /*
Keith Kaniosb7a89542007-04-12 02:40:54 +00005056 * De-tokenize the line again, and emit it.
H. Peter Anvine2c80182005-01-15 22:15:51 +00005057 */
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005058 line = detoken(tline, true);
5059 free_tlist(tline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00005060 break;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005061 } else {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005062 continue; /* expand_mmacro calls free_tlist */
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005063 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00005064 }
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005065 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005066
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005067 return line;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005068}
5069
H. Peter Anvine2c80182005-01-15 22:15:51 +00005070static void pp_cleanup(int pass)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005071{
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005072 if (defining) {
5073 if (defining->name) {
5074 error(ERR_NONFATAL,
5075 "end of file while still defining macro `%s'",
5076 defining->name);
5077 } else {
5078 error(ERR_NONFATAL, "end of file while still in %%rep");
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005079 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005080
5081 free_mmacro(defining);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005082 defining = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005083 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005084 while (cstk)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005085 ctx_pop();
H. Peter Anvin97a23472007-09-16 17:57:25 -07005086 free_macros();
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005087 while (istk) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00005088 Include *i = istk;
5089 istk = istk->next;
5090 fclose(i->fp);
5091 nasm_free(i->fname);
Cyrill Gorcunov8dcfd882011-03-03 09:18:56 +03005092 nasm_free(i);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005093 }
5094 while (cstk)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005095 ctx_pop();
H. Peter Anvin86877b22008-06-20 15:55:45 -07005096 nasm_free(src_set_fname(NULL));
H. Peter Anvine2c80182005-01-15 22:15:51 +00005097 if (pass == 0) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03005098 IncPath *i;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005099 free_llist(predef);
5100 delete_Blocks();
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03005101 while ((i = ipath)) {
5102 ipath = i->next;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005103 if (i->path)
5104 nasm_free(i->path);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03005105 nasm_free(i);
5106 }
H. Peter Anvin11dfa1a2008-07-02 18:11:04 -07005107 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005108}
5109
Cyrill Gorcunov0b78bff2012-05-07 01:57:55 +04005110static void pp_include_path(char *path)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005111{
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005112 IncPath *i;
H. Peter Anvin37a321f2007-09-24 13:41:58 -07005113
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005114 i = nasm_malloc(sizeof(IncPath));
5115 i->path = path ? nasm_strdup(path) : NULL;
5116 i->next = NULL;
Frank Kotlerd0ed6fd2003-08-27 11:33:56 +00005117
H. Peter Anvin89cee572009-07-15 09:16:54 -04005118 if (ipath) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00005119 IncPath *j = ipath;
H. Peter Anvin89cee572009-07-15 09:16:54 -04005120 while (j->next)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005121 j = j->next;
5122 j->next = i;
5123 } else {
5124 ipath = i;
Frank Kotlerd0ed6fd2003-08-27 11:33:56 +00005125 }
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005126}
Frank Kotlerd0ed6fd2003-08-27 11:33:56 +00005127
Cyrill Gorcunov0b78bff2012-05-07 01:57:55 +04005128static void pp_pre_include(char *fname)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005129{
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005130 Token *inc, *space, *name;
5131 Line *l;
5132
H. Peter Anvin734b1882002-04-30 21:01:08 +00005133 name = new_Token(NULL, TOK_INTERNAL_STRING, fname, 0);
5134 space = new_Token(name, TOK_WHITESPACE, NULL, 0);
5135 inc = new_Token(space, TOK_PREPROC_ID, "%include", 0);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005136
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005137 l = nasm_malloc(sizeof(Line));
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005138 l->next = predef;
5139 l->first = inc;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005140 l->finishes = NULL;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005141 predef = l;
5142}
5143
Cyrill Gorcunov0b78bff2012-05-07 01:57:55 +04005144static void pp_pre_define(char *definition)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005145{
5146 Token *def, *space;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005147 Line *l;
Keith Kaniosa6dfa782007-04-13 16:47:53 +00005148 char *equals;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005149
5150 equals = strchr(definition, '=');
H. Peter Anvin734b1882002-04-30 21:01:08 +00005151 space = new_Token(NULL, TOK_WHITESPACE, NULL, 0);
5152 def = new_Token(space, TOK_PREPROC_ID, "%define", 0);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005153 if (equals)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005154 *equals = ' ';
Keith Kaniosb7a89542007-04-12 02:40:54 +00005155 space->next = tokenize(definition);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005156 if (equals)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005157 *equals = '=';
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005158
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005159 l = nasm_malloc(sizeof(Line));
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005160 l->next = predef;
5161 l->first = def;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005162 l->finishes = NULL;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005163 predef = l;
5164}
5165
Cyrill Gorcunov0b78bff2012-05-07 01:57:55 +04005166static void pp_pre_undefine(char *definition)
H. Peter Anvin620515a2002-04-30 20:57:38 +00005167{
5168 Token *def, *space;
5169 Line *l;
5170
H. Peter Anvin734b1882002-04-30 21:01:08 +00005171 space = new_Token(NULL, TOK_WHITESPACE, NULL, 0);
5172 def = new_Token(space, TOK_PREPROC_ID, "%undef", 0);
Keith Kaniosb7a89542007-04-12 02:40:54 +00005173 space->next = tokenize(definition);
H. Peter Anvin620515a2002-04-30 20:57:38 +00005174
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005175 l = nasm_malloc(sizeof(Line));
H. Peter Anvin620515a2002-04-30 20:57:38 +00005176 l->next = predef;
5177 l->first = def;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005178 l->finishes = NULL;
H. Peter Anvin620515a2002-04-30 20:57:38 +00005179 predef = l;
5180}
5181
Cyrill Gorcunov0b78bff2012-05-07 01:57:55 +04005182static void pp_extra_stdmac(macros_t *macros)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005183{
H. Peter Anvin76690a12002-04-30 20:52:49 +00005184 extrastdmac = macros;
5185}
5186
Keith Kaniosa5fc6462007-10-13 07:09:22 -07005187static void make_tok_num(Token * tok, int64_t val)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005188{
Keith Kaniosa6dfa782007-04-13 16:47:53 +00005189 char numbuf[20];
Keith Kaniosa5fc6462007-10-13 07:09:22 -07005190 snprintf(numbuf, sizeof(numbuf), "%"PRId64"", val);
H. Peter Anvineba20a72002-04-30 20:53:55 +00005191 tok->text = nasm_strdup(numbuf);
5192 tok->type = TOK_NUMBER;
5193}
5194
Cyrill Gorcunov86b2ad02011-07-01 10:38:25 +04005195struct preproc_ops nasmpp = {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005196 pp_reset,
5197 pp_getline,
Cyrill Gorcunov0b78bff2012-05-07 01:57:55 +04005198 pp_cleanup,
5199 pp_extra_stdmac,
5200 pp_pre_define,
5201 pp_pre_undefine,
5202 pp_pre_include,
5203 pp_include_path
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005204};