blob: 0dab8f285be2bdb0a151b0eda70f396a1df24338 [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))
216
Cyrill Gorcunov575d4282010-10-06 00:25:55 +0400217struct tokseq_match {
218 int mask_head;
219 int mask_tail;
220};
221
H. Peter Anvine2c80182005-01-15 22:15:51 +0000222struct Token {
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800223 Token *next;
224 char *text;
H. Peter Anvinf26e0972008-07-01 21:26:27 -0700225 union {
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800226 SMacro *mac; /* associated macro for TOK_SMAC_END */
227 size_t len; /* scratch length field */
228 } a; /* Auxiliary data */
229 enum pp_token_type type;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000230};
231
232/*
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800233 * Multi-line macro definitions are stored as a linked list of
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000234 * these, which is essentially a container to allow several linked
235 * lists of Tokens.
H. Peter Anvin70653092007-10-19 14:42:29 -0700236 *
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000237 * Note that in this module, linked lists are treated as stacks
238 * wherever possible. For this reason, Lines are _pushed_ on to the
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800239 * `expansion' field in MMacro structures, so that the linked list,
240 * if walked, would give the macro lines in reverse order; this
241 * means that we can walk the list when expanding a macro, and thus
242 * push the lines on to the `expansion' field in _istk_ in reverse
243 * order (so that when popped back off they are in the right
244 * order). It may seem cockeyed, and it relies on my design having
245 * an even number of steps in, but it works...
246 *
247 * Some of these structures, rather than being actual lines, are
248 * markers delimiting the end of the expansion of a given macro.
249 * This is for use in the cycle-tracking and %rep-handling code.
250 * Such structures have `finishes' non-NULL, and `first' NULL. All
251 * others have `finishes' NULL, but `first' may still be NULL if
252 * the line is blank.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000253 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000254struct Line {
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800255 Line *next;
256 MMacro *finishes;
257 Token *first;
Keith Kaniosb307a4f2010-11-06 17:41:51 -0500258};
259
260/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000261 * To handle an arbitrary level of file inclusion, we maintain a
262 * stack (ie linked list) of these things.
263 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000264struct Include {
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800265 Include *next;
266 FILE *fp;
267 Cond *conds;
268 Line *expansion;
269 char *fname;
270 int lineno, lineinc;
271 MMacro *mstk; /* stack of active macros/reps */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000272};
273
274/*
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000275 * Include search path. This is simply a list of strings which get
276 * prepended, in turn, to the name of an include file, in an
277 * attempt to find the file if it's not in the current directory.
278 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000279struct IncPath {
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800280 IncPath *next;
281 char *path;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000282};
283
284/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000285 * Conditional assembly: we maintain a separate stack of these for
286 * each level of file inclusion. (The only reason we keep the
287 * stacks separate is to ensure that a stray `%endif' in a file
288 * included from within the true branch of a `%if' won't terminate
289 * it and cause confusion: instead, rightly, it'll cause an error.)
290 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800291struct Cond {
292 Cond *next;
293 int state;
294};
H. Peter Anvine2c80182005-01-15 22:15:51 +0000295enum {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000296 /*
297 * These states are for use just after %if or %elif: IF_TRUE
298 * means the condition has evaluated to truth so we are
299 * currently emitting, whereas IF_FALSE means we are not
300 * currently emitting but will start doing so if a %else comes
301 * up. In these states, all directives are admissible: %elif,
302 * %else and %endif. (And of course %if.)
303 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800304 COND_IF_TRUE, COND_IF_FALSE,
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000305 /*
306 * These states come up after a %else: ELSE_TRUE means we're
307 * emitting, and ELSE_FALSE means we're not. In ELSE_* states,
308 * any %elif or %else will cause an error.
309 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800310 COND_ELSE_TRUE, COND_ELSE_FALSE,
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000311 /*
Victor van den Elzen3b404c02008-09-18 13:51:36 +0200312 * These states mean that we're not emitting now, and also that
313 * nothing until %endif will be emitted at all. COND_DONE is
314 * used when we've had our moment of emission
315 * and have now started seeing %elifs. COND_NEVER is used when
316 * the condition construct in question is contained within a
317 * non-emitting branch of a larger condition construct,
318 * or if there is an error.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000319 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800320 COND_DONE, COND_NEVER
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000321};
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800322#define emitting(x) ( (x) == COND_IF_TRUE || (x) == COND_ELSE_TRUE )
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000323
H. Peter Anvin70653092007-10-19 14:42:29 -0700324/*
Ed Beroset3ab3f412002-06-11 03:31:49 +0000325 * These defines are used as the possible return values for do_directive
326 */
327#define NO_DIRECTIVE_FOUND 0
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300328#define DIRECTIVE_FOUND 1
Ed Beroset3ab3f412002-06-11 03:31:49 +0000329
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000330/*
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800331 * This define sets the upper limit for smacro and recursive mmacro
332 * expansions
Keith Kanios852f1ee2009-07-12 00:19:55 -0500333 */
334#define DEADMAN_LIMIT (1 << 20)
335
Cyrill Gorcunove091d6e2010-08-09 13:58:22 +0400336/* max reps */
337#define REP_LIMIT ((INT64_C(1) << 62))
338
Keith Kanios852f1ee2009-07-12 00:19:55 -0500339/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000340 * Condition codes. Note that we use c_ prefix not C_ because C_ is
341 * used in nasm.h for the "real" condition codes. At _this_ level,
342 * we treat CXZ and ECXZ as condition codes, albeit non-invertible
343 * ones, so we need a different enum...
344 */
H. Peter Anvin476d2862007-10-02 22:04:15 -0700345static const char * const conditions[] = {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000346 "a", "ae", "b", "be", "c", "cxz", "e", "ecxz", "g", "ge", "l", "le",
347 "na", "nae", "nb", "nbe", "nc", "ne", "ng", "nge", "nl", "nle", "no",
H. Peter Anvince9be342007-09-12 00:22:29 +0000348 "np", "ns", "nz", "o", "p", "pe", "po", "rcxz", "s", "z"
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000349};
H. Peter Anvin476d2862007-10-02 22:04:15 -0700350enum pp_conds {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000351 c_A, c_AE, c_B, c_BE, c_C, c_CXZ, c_E, c_ECXZ, c_G, c_GE, c_L, c_LE,
352 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 -0700353 c_NP, c_NS, c_NZ, c_O, c_P, c_PE, c_PO, c_RCXZ, c_S, c_Z,
354 c_none = -1
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000355};
H. Peter Anvin476d2862007-10-02 22:04:15 -0700356static const enum pp_conds inverse_ccs[] = {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000357 c_NA, c_NAE, c_NB, c_NBE, c_NC, -1, c_NE, -1, c_NG, c_NGE, c_NL, c_NLE,
358 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 +0000359 c_Z, c_NO, c_NP, c_PO, c_PE, -1, c_NS, c_NZ
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000360};
361
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800362/*
363 * Directive names.
364 */
365/* If this is a an IF, ELIF, ELSE or ENDIF keyword */
366static int is_condition(enum preproc_token arg)
367{
368 return PP_IS_COND(arg) || (arg == PP_ELSE) || (arg == PP_ENDIF);
369}
370
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000371/* For TASM compatibility we need to be able to recognise TASM compatible
372 * conditional compilation directives. Using the NASM pre-processor does
373 * not work, so we look for them specifically from the following list and
374 * then jam in the equivalent NASM directive into the input stream.
375 */
376
H. Peter Anvine2c80182005-01-15 22:15:51 +0000377enum {
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000378 TM_ARG, TM_ELIF, TM_ELSE, TM_ENDIF, TM_IF, TM_IFDEF, TM_IFDIFI,
379 TM_IFNDEF, TM_INCLUDE, TM_LOCAL
380};
381
H. Peter Anvin476d2862007-10-02 22:04:15 -0700382static const char * const tasm_directives[] = {
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000383 "arg", "elif", "else", "endif", "if", "ifdef", "ifdifi",
384 "ifndef", "include", "local"
385};
386
387static int StackSize = 4;
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000388static char *StackPointer = "ebp";
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000389static int ArgOffset = 8;
H. Peter Anvin8781cb02007-11-08 20:01:11 -0800390static int LocalOffset = 0;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000391
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000392static Context *cstk;
393static Include *istk;
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800394static IncPath *ipath = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000395
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300396static int pass; /* HACK: pass 0 = generate dependencies only */
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700397static StrList **dephead, **deptail; /* Dependency list */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000398
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300399static uint64_t unique; /* unique identifier numbers */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000400
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800401static Line *predef = NULL;
H. Peter Anvind2456592008-06-19 15:04:18 -0700402static bool do_predef;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000403
404static ListGen *list;
405
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000406/*
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800407 * The current set of multi-line macros we have defined.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000408 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800409static struct hash_table mmacros;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000410
411/*
412 * The current set of single-line macros we have defined.
413 */
H. Peter Anvin166c2472008-05-28 12:28:58 -0700414static struct hash_table smacros;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000415
416/*
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800417 * The multi-line macro we are currently defining, or the %rep
418 * block we are currently reading, if any.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000419 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800420static MMacro *defining;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000421
Charles Crayned4200be2008-07-12 16:42:33 -0700422static uint64_t nested_mac_count;
423static uint64_t nested_rep_count;
424
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000425/*
426 * The number of macro parameters to allocate space for at a time.
427 */
428#define PARAM_DELTA 16
429
430/*
H. Peter Anvina4835d42008-05-20 14:21:29 -0700431 * The standard macro set: defined in macros.c in the array nasm_stdmac.
432 * This gives our position in the macro set, when we're processing it.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000433 */
H. Peter Anvina70547f2008-07-19 21:44:26 -0700434static macros_t *stdmacpos;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000435
436/*
H. Peter Anvin76690a12002-04-30 20:52:49 +0000437 * The extra standard macros that come from the object format, if
438 * any.
439 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800440static macros_t *extrastdmac = NULL;
H. Peter Anvincfb71762008-06-20 15:20:16 -0700441static bool any_extrastdmac;
H. Peter Anvin76690a12002-04-30 20:52:49 +0000442
443/*
H. Peter Anvin734b1882002-04-30 21:01:08 +0000444 * Tokens are allocated in blocks to improve speed
445 */
446#define TOKEN_BLOCKSIZE 4096
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800447static Token *freeTokens = NULL;
H. Peter Anvince616072002-04-30 21:02:23 +0000448struct Blocks {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000449 Blocks *next;
450 void *chunk;
H. Peter Anvince616072002-04-30 21:02:23 +0000451};
452
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800453static Blocks blocks = { NULL, NULL };
H. Peter Anvin734b1882002-04-30 21:01:08 +0000454
455/*
H. Peter Anvin76690a12002-04-30 20:52:49 +0000456 * Forward declarations.
457 */
H. Peter Anvin734b1882002-04-30 21:01:08 +0000458static Token *expand_mmac_params(Token * tline);
459static Token *expand_smacro(Token * tline);
460static Token *expand_id(Token * tline);
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800461static Context *get_ctx(const char *name, const char **namep,
462 bool all_contexts);
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;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001283 Context *ctx = get_ctx(t->text, &q, false);
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 Anvin36206cd2012-03-03 16:14:51 -08001448 * If all_contexts == true, contexts that enclose current are
1449 * also scanned for such smacro, until it is found; if not -
1450 * only the context that directly results from the number of $'s
1451 * in variable's name.
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001452 *
1453 * If "namep" is non-NULL, set it to the pointer to the macro name
1454 * tail, i.e. the part beyond %$...
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001455 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001456static Context *get_ctx(const char *name, const char **namep,
1457 bool all_contexts)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001458{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001459 Context *ctx;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001460 SMacro *m;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001461 int i;
1462
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001463 if (namep)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001464 *namep = name;
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001465
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001466 if (!name || name[0] != '%' || name[1] != '$')
H. Peter Anvine2c80182005-01-15 22:15:51 +00001467 return NULL;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001468
H. Peter Anvine2c80182005-01-15 22:15:51 +00001469 if (!cstk) {
1470 error(ERR_NONFATAL, "`%s': context stack is empty", name);
1471 return NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001472 }
1473
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001474 name += 2;
1475 ctx = cstk;
1476 i = 0;
1477 while (ctx && *name == '$') {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001478 name++;
1479 i++;
1480 ctx = ctx->next;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001481 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001482 if (!ctx) {
1483 error(ERR_NONFATAL, "`%s': context stack is only"
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001484 " %d level%s deep", name, i, (i == 1 ? "" : "s"));
H. Peter Anvine2c80182005-01-15 22:15:51 +00001485 return NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001486 }
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001487
1488 if (namep)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001489 *namep = name;
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001490
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001491 if (!all_contexts)
1492 return ctx;
1493
1494 /*
1495 * NOTE: In 2.10 we will not need lookup in extarnal
1496 * contexts, so this is a gentle way to inform users
1497 * about their source code need to be updated
1498 */
1499
1500 /* first round -- check the current context */
1501 m = hash_findix(&ctx->localmac, name);
1502 while (m) {
1503 if (!mstrcmp(m->name, name, m->casesense))
1504 return ctx;
1505 m = m->next;
1506 }
1507
1508 /* second round - external contexts */
1509 while ((ctx = ctx->next)) {
1510 /* Search for this smacro in found context */
1511 m = hash_findix(&ctx->localmac, name);
1512 while (m) {
1513 if (!mstrcmp(m->name, name, m->casesense)) {
1514 /* NOTE: deprecated as of 2.10 */
1515 static int once = 0;
1516 if (!once) {
1517 error(ERR_WARNING, "context-local macro expansion"
1518 " fall-through (automatic searching of outer"
1519 " contexts) will be deprecated starting in"
1520 " NASM 2.10, please see the NASM Manual for"
1521 " more information");
1522 once = 1;
1523 }
1524 error(ERR_WARNING, "`%s': context-local macro expansion fall-through", name);
1525 return ctx;
1526 }
1527 m = m->next;
1528 }
1529 }
1530
1531 return NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001532}
1533
1534/*
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001535 * Check to see if a file is already in a string list
1536 */
1537static bool in_list(const StrList *list, const char *str)
1538{
1539 while (list) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001540 if (!strcmp(list->str, str))
1541 return true;
1542 list = list->next;
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001543 }
1544 return false;
1545}
1546
1547/*
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001548 * Open an include file. This routine must always return a valid
1549 * file pointer if it returns - it's responsible for throwing an
1550 * ERR_FATAL and bombing out completely if not. It should also try
1551 * the include path one by one until it finds the file or reaches
1552 * the end of the path.
1553 */
H. Peter Anvin2b1c3b92008-06-06 10:38:46 -07001554static FILE *inc_fopen(const char *file, StrList **dhead, StrList ***dtail,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001555 bool missing_ok)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001556{
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001557 FILE *fp;
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001558 char *prefix = "";
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001559 IncPath *ip = ipath;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001560 int len = strlen(file);
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001561 size_t prefix_len = 0;
1562 StrList *sl;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001563
H. Peter Anvine2c80182005-01-15 22:15:51 +00001564 while (1) {
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001565 sl = nasm_malloc(prefix_len+len+1+sizeof sl->next);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001566 memcpy(sl->str, prefix, prefix_len);
1567 memcpy(sl->str+prefix_len, file, len+1);
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001568 fp = fopen(sl->str, "r");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001569 if (fp && dhead && !in_list(*dhead, sl->str)) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001570 sl->next = NULL;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001571 **dtail = sl;
1572 *dtail = &sl->next;
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001573 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001574 nasm_free(sl);
1575 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001576 if (fp)
1577 return fp;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001578 if (!ip) {
1579 if (!missing_ok)
1580 break;
1581 prefix = NULL;
1582 } else {
1583 prefix = ip->path;
1584 ip = ip->next;
1585 }
1586 if (prefix) {
1587 prefix_len = strlen(prefix);
1588 } else {
1589 /* -MG given and file not found */
1590 if (dhead && !in_list(*dhead, file)) {
1591 sl = nasm_malloc(len+1+sizeof sl->next);
1592 sl->next = NULL;
1593 strcpy(sl->str, file);
1594 **dtail = sl;
1595 *dtail = &sl->next;
1596 }
1597 return NULL;
1598 }
H. Peter Anvineba20a72002-04-30 20:53:55 +00001599 }
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001600
H. Peter Anvin734b1882002-04-30 21:01:08 +00001601 error(ERR_FATAL, "unable to open include file `%s'", file);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001602 return NULL;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001603}
1604
1605/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001606 * Determine if we should warn on defining a single-line macro of
H. Peter Anvinef7468f2002-04-30 20:57:59 +00001607 * name `name', with `nparam' parameters. If nparam is 0 or -1, will
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001608 * return true if _any_ single-line macro of that name is defined.
1609 * Otherwise, will return true if a single-line macro with either
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001610 * `nparam' or no parameters is defined.
1611 *
1612 * If a macro with precisely the right number of parameters is
H. Peter Anvinef7468f2002-04-30 20:57:59 +00001613 * defined, or nparam is -1, the address of the definition structure
1614 * will be returned in `defn'; otherwise NULL will be returned. If `defn'
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001615 * is NULL, no action will be taken regarding its contents, and no
1616 * error will occur.
1617 *
1618 * Note that this is also called with nparam zero to resolve
1619 * `ifdef'.
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001620 *
1621 * If you already know which context macro belongs to, you can pass
1622 * the context pointer as first parameter; if you won't but name begins
1623 * with %$ the context will be automatically computed. If all_contexts
1624 * is true, macro will be searched in outer contexts as well.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001625 */
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07001626static bool
H. Peter Anvinb2a5fda2008-06-19 21:42:42 -07001627smacro_defined(Context * ctx, const char *name, int nparam, SMacro ** defn,
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07001628 bool nocase)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001629{
H. Peter Anvin166c2472008-05-28 12:28:58 -07001630 struct hash_table *smtbl;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001631 SMacro *m;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001632
H. Peter Anvin97a23472007-09-16 17:57:25 -07001633 if (ctx) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001634 smtbl = &ctx->localmac;
H. Peter Anvin97a23472007-09-16 17:57:25 -07001635 } else if (name[0] == '%' && name[1] == '$') {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001636 if (cstk)
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001637 ctx = get_ctx(name, &name, false);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001638 if (!ctx)
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001639 return false; /* got to return _something_ */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001640 smtbl = &ctx->localmac;
H. Peter Anvin97a23472007-09-16 17:57:25 -07001641 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001642 smtbl = &smacros;
H. Peter Anvin97a23472007-09-16 17:57:25 -07001643 }
H. Peter Anvin166c2472008-05-28 12:28:58 -07001644 m = (SMacro *) hash_findix(smtbl, name);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001645
H. Peter Anvine2c80182005-01-15 22:15:51 +00001646 while (m) {
1647 if (!mstrcmp(m->name, name, m->casesense && nocase) &&
Charles Crayne192d5b52007-10-18 19:02:42 -07001648 (nparam <= 0 || m->nparam == 0 || nparam == (int) m->nparam)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001649 if (defn) {
Charles Crayne192d5b52007-10-18 19:02:42 -07001650 if (nparam == (int) m->nparam || nparam == -1)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001651 *defn = m;
1652 else
1653 *defn = NULL;
1654 }
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001655 return true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001656 }
1657 m = m->next;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001658 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001659
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001660 return false;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001661}
1662
1663/*
1664 * Count and mark off the parameters in a multi-line macro call.
1665 * This is called both from within the multi-line macro expansion
1666 * code, and also to mark off the default parameters when provided
1667 * in a %macro definition line.
1668 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001669static void count_mmac_params(Token * t, int *nparam, Token *** params)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001670{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001671 int paramsize, brace;
1672
1673 *nparam = paramsize = 0;
1674 *params = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001675 while (t) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001676 /* +1: we need space for the final NULL */
H. Peter Anvin91fb6f12008-09-01 10:56:33 -07001677 if (*nparam+1 >= paramsize) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001678 paramsize += PARAM_DELTA;
1679 *params = nasm_realloc(*params, sizeof(**params) * paramsize);
1680 }
1681 skip_white_(t);
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001682 brace = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001683 if (tok_is_(t, "{"))
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001684 brace = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001685 (*params)[(*nparam)++] = t;
1686 while (tok_isnt_(t, brace ? "}" : ","))
1687 t = t->next;
1688 if (t) { /* got a comma/brace */
1689 t = t->next;
1690 if (brace) {
1691 /*
1692 * Now we've found the closing brace, look further
1693 * for the comma.
1694 */
1695 skip_white_(t);
1696 if (tok_isnt_(t, ",")) {
1697 error(ERR_NONFATAL,
1698 "braces do not enclose all of macro parameter");
1699 while (tok_isnt_(t, ","))
1700 t = t->next;
1701 }
1702 if (t)
1703 t = t->next; /* eat the comma */
1704 }
1705 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001706 }
1707}
1708
1709/*
H. Peter Anvin76690a12002-04-30 20:52:49 +00001710 * Determine whether one of the various `if' conditions is true or
1711 * not.
1712 *
1713 * We must free the tline we get passed.
1714 */
H. Peter Anvin70055962007-10-11 00:05:31 -07001715static bool if_condition(Token * tline, enum preproc_token ct)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001716{
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001717 enum pp_conditional i = PP_COND(ct);
H. Peter Anvin70055962007-10-11 00:05:31 -07001718 bool j;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001719 Token *t, *tt, **tptr, *origline;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001720 struct tokenval tokval;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001721 expr *evalresult;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001722 enum pp_token_type needtype;
H. Peter Anvin077fb932010-07-20 14:56:30 -07001723 char *p;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001724
1725 origline = tline;
1726
H. Peter Anvine2c80182005-01-15 22:15:51 +00001727 switch (i) {
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001728 case PPC_IFCTX:
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001729 j = false; /* have we matched yet? */
Victor van den Elzen0e857f12008-07-23 13:21:29 +02001730 while (true) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001731 skip_white_(tline);
Victor van den Elzen0e857f12008-07-23 13:21:29 +02001732 if (!tline)
1733 break;
1734 if (tline->type != TOK_ID) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001735 error(ERR_NONFATAL,
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001736 "`%s' expects context identifiers", pp_directives[ct]);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001737 free_tlist(origline);
1738 return -1;
1739 }
Victor van den Elzen0e857f12008-07-23 13:21:29 +02001740 if (cstk && cstk->name && !nasm_stricmp(tline->text, cstk->name))
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001741 j = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001742 tline = tline->next;
1743 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001744 break;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001745
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001746 case PPC_IFDEF:
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001747 j = false; /* have we matched yet? */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001748 while (tline) {
1749 skip_white_(tline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001750 if (!tline || (tline->type != TOK_ID &&
1751 (tline->type != TOK_PREPROC_ID ||
1752 tline->text[1] != '$'))) {
1753 error(ERR_NONFATAL,
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001754 "`%s' expects macro identifiers", pp_directives[ct]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001755 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001756 }
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07001757 if (smacro_defined(NULL, tline->text, 0, NULL, true))
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001758 j = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001759 tline = tline->next;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001760 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001761 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001762
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001763 case PPC_IFENV:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001764 tline = expand_smacro(tline);
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001765 j = false; /* have we matched yet? */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001766 while (tline) {
1767 skip_white_(tline);
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001768 if (!tline || (tline->type != TOK_ID &&
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001769 tline->type != TOK_STRING &&
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001770 (tline->type != TOK_PREPROC_ID ||
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001771 tline->text[1] != '!'))) {
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001772 error(ERR_NONFATAL,
1773 "`%s' expects environment variable names",
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001774 pp_directives[ct]);
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001775 goto fail;
1776 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001777 p = tline->text;
1778 if (tline->type == TOK_PREPROC_ID)
1779 p += 2; /* Skip leading %! */
1780 if (*p == '\'' || *p == '\"' || *p == '`')
1781 nasm_unquote_cstr(p, ct);
1782 if (getenv(p))
1783 j = true;
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001784 tline = tline->next;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001785 }
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001786 break;
1787
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001788 case PPC_IFIDN:
1789 case PPC_IFIDNI:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001790 tline = expand_smacro(tline);
1791 t = tt = tline;
1792 while (tok_isnt_(tt, ","))
1793 tt = tt->next;
1794 if (!tt) {
1795 error(ERR_NONFATAL,
1796 "`%s' expects two comma-separated arguments",
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001797 pp_directives[ct]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001798 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001799 }
1800 tt = tt->next;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001801 j = true; /* assume equality unless proved not */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001802 while ((t->type != TOK_OTHER || strcmp(t->text, ",")) && tt) {
1803 if (tt->type == TOK_OTHER && !strcmp(tt->text, ",")) {
1804 error(ERR_NONFATAL, "`%s': more than one comma on line",
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001805 pp_directives[ct]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001806 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001807 }
1808 if (t->type == TOK_WHITESPACE) {
1809 t = t->next;
1810 continue;
1811 }
1812 if (tt->type == TOK_WHITESPACE) {
1813 tt = tt->next;
1814 continue;
1815 }
1816 if (tt->type != t->type) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001817 j = false; /* found mismatching tokens */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001818 break;
1819 }
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07001820 /* When comparing strings, need to unquote them first */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001821 if (t->type == TOK_STRING) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001822 size_t l1 = nasm_unquote(t->text, NULL);
1823 size_t l2 = nasm_unquote(tt->text, NULL);
H. Peter Anvind2456592008-06-19 15:04:18 -07001824
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001825 if (l1 != l2) {
1826 j = false;
1827 break;
1828 }
1829 if (mmemcmp(t->text, tt->text, l1, i == PPC_IFIDN)) {
1830 j = false;
1831 break;
1832 }
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07001833 } else if (mstrcmp(tt->text, t->text, i == PPC_IFIDN) != 0) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001834 j = false; /* found mismatching tokens */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001835 break;
1836 }
Nickolay Yurchenkof3b3ce22003-09-21 20:38:43 +00001837
H. Peter Anvine2c80182005-01-15 22:15:51 +00001838 t = t->next;
1839 tt = tt->next;
1840 }
1841 if ((t->type != TOK_OTHER || strcmp(t->text, ",")) || tt)
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001842 j = false; /* trailing gunk on one end or other */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001843 break;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001844
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001845 case PPC_IFMACRO:
H. Peter Anvin89cee572009-07-15 09:16:54 -04001846 {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001847 bool found = false;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001848 MMacro searching, *mmac;
H. Peter Anvin65747262002-05-07 00:10:05 +00001849
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001850 skip_white_(tline);
1851 tline = expand_id(tline);
1852 if (!tok_type_(tline, TOK_ID)) {
1853 error(ERR_NONFATAL,
1854 "`%s' expects a macro name", pp_directives[ct]);
1855 goto fail;
1856 }
1857 searching.name = nasm_strdup(tline->text);
1858 searching.casesense = true;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001859 searching.plus = false;
1860 searching.nolist = false;
1861 searching.in_progress = 0;
1862 searching.max_depth = 0;
1863 searching.rep_nest = NULL;
1864 searching.nparam_min = 0;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001865 searching.nparam_max = INT_MAX;
1866 tline = expand_smacro(tline->next);
1867 skip_white_(tline);
1868 if (!tline) {
1869 } else if (!tok_type_(tline, TOK_NUMBER)) {
1870 error(ERR_NONFATAL,
1871 "`%s' expects a parameter count or nothing",
1872 pp_directives[ct]);
1873 } else {
1874 searching.nparam_min = searching.nparam_max =
1875 readnum(tline->text, &j);
1876 if (j)
1877 error(ERR_NONFATAL,
1878 "unable to parse parameter count `%s'",
1879 tline->text);
1880 }
1881 if (tline && tok_is_(tline->next, "-")) {
1882 tline = tline->next->next;
1883 if (tok_is_(tline, "*"))
1884 searching.nparam_max = INT_MAX;
1885 else if (!tok_type_(tline, TOK_NUMBER))
1886 error(ERR_NONFATAL,
1887 "`%s' expects a parameter count after `-'",
1888 pp_directives[ct]);
1889 else {
1890 searching.nparam_max = readnum(tline->text, &j);
1891 if (j)
1892 error(ERR_NONFATAL,
1893 "unable to parse parameter count `%s'",
1894 tline->text);
1895 if (searching.nparam_min > searching.nparam_max)
1896 error(ERR_NONFATAL,
1897 "minimum parameter count exceeds maximum");
1898 }
1899 }
1900 if (tline && tok_is_(tline->next, "+")) {
1901 tline = tline->next;
1902 searching.plus = true;
1903 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001904 mmac = (MMacro *) hash_findix(&mmacros, searching.name);
1905 while (mmac) {
1906 if (!strcmp(mmac->name, searching.name) &&
1907 (mmac->nparam_min <= searching.nparam_max
1908 || searching.plus)
1909 && (searching.nparam_min <= mmac->nparam_max
1910 || mmac->plus)) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001911 found = true;
1912 break;
1913 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001914 mmac = mmac->next;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001915 }
1916 if (tline && tline->next)
1917 error(ERR_WARNING|ERR_PASS1,
1918 "trailing garbage after %%ifmacro ignored");
1919 nasm_free(searching.name);
1920 j = found;
1921 break;
H. Peter Anvin89cee572009-07-15 09:16:54 -04001922 }
H. Peter Anvin65747262002-05-07 00:10:05 +00001923
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001924 case PPC_IFID:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001925 needtype = TOK_ID;
1926 goto iftype;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001927 case PPC_IFNUM:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001928 needtype = TOK_NUMBER;
1929 goto iftype;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001930 case PPC_IFSTR:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001931 needtype = TOK_STRING;
1932 goto iftype;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001933
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001934iftype:
1935 t = tline = expand_smacro(tline);
H. Peter Anvind85d2502008-05-04 17:53:31 -07001936
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001937 while (tok_type_(t, TOK_WHITESPACE) ||
1938 (needtype == TOK_NUMBER &&
1939 tok_type_(t, TOK_OTHER) &&
1940 (t->text[0] == '-' || t->text[0] == '+') &&
1941 !t->text[1]))
1942 t = t->next;
H. Peter Anvind85d2502008-05-04 17:53:31 -07001943
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001944 j = tok_type_(t, needtype);
1945 break;
H. Peter Anvincbf768d2008-02-16 16:41:25 -08001946
1947 case PPC_IFTOKEN:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001948 t = tline = expand_smacro(tline);
1949 while (tok_type_(t, TOK_WHITESPACE))
1950 t = t->next;
H. Peter Anvincbf768d2008-02-16 16:41:25 -08001951
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001952 j = false;
1953 if (t) {
1954 t = t->next; /* Skip the actual token */
1955 while (tok_type_(t, TOK_WHITESPACE))
1956 t = t->next;
1957 j = !t; /* Should be nothing left */
1958 }
1959 break;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001960
H. Peter Anvin134b9462008-02-16 17:01:40 -08001961 case PPC_IFEMPTY:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001962 t = tline = expand_smacro(tline);
1963 while (tok_type_(t, TOK_WHITESPACE))
1964 t = t->next;
H. Peter Anvin134b9462008-02-16 17:01:40 -08001965
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001966 j = !t; /* Should be empty */
1967 break;
H. Peter Anvin134b9462008-02-16 17:01:40 -08001968
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001969 case PPC_IF:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001970 t = tline = expand_smacro(tline);
1971 tptr = &t;
1972 tokval.t_type = TOKEN_INVALID;
1973 evalresult = evaluate(ppscan, tptr, &tokval,
1974 NULL, pass | CRITICAL, error, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001975 if (!evalresult)
1976 return -1;
1977 if (tokval.t_type)
H. Peter Anvin917a3492008-09-24 09:14:49 -07001978 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +00001979 "trailing garbage after expression ignored");
1980 if (!is_simple(evalresult)) {
1981 error(ERR_NONFATAL,
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001982 "non-constant value given to `%s'", pp_directives[ct]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001983 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001984 }
Chuck Crayne60ae75d2007-05-02 01:59:16 +00001985 j = reloc_value(evalresult) != 0;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001986 break;
H. Peter Anvin95e28822007-09-12 04:20:08 +00001987
H. Peter Anvine2c80182005-01-15 22:15:51 +00001988 default:
1989 error(ERR_FATAL,
1990 "preprocessor directive `%s' not yet implemented",
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001991 pp_directives[ct]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001992 goto fail;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001993 }
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001994
1995 free_tlist(origline);
1996 return j ^ PP_NEGATIVE(ct);
H. Peter Anvin70653092007-10-19 14:42:29 -07001997
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001998fail:
1999 free_tlist(origline);
2000 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002001}
2002
2003/*
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002004 * Common code for defining an smacro
2005 */
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08002006static bool define_smacro(Context *ctx, const char *mname, bool casesense,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002007 int nparam, Token *expansion)
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002008{
2009 SMacro *smac, **smhead;
H. Peter Anvin166c2472008-05-28 12:28:58 -07002010 struct hash_table *smtbl;
H. Peter Anvin70653092007-10-19 14:42:29 -07002011
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002012 if (smacro_defined(ctx, mname, nparam, &smac, casesense)) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002013 if (!smac) {
2014 error(ERR_WARNING|ERR_PASS1,
2015 "single-line macro `%s' defined both with and"
2016 " without parameters", mname);
2017 /*
2018 * Some instances of the old code considered this a failure,
2019 * some others didn't. What is the right thing to do here?
2020 */
2021 free_tlist(expansion);
2022 return false; /* Failure */
2023 } else {
2024 /*
2025 * We're redefining, so we have to take over an
2026 * existing SMacro structure. This means freeing
2027 * what was already in it.
2028 */
2029 nasm_free(smac->name);
2030 free_tlist(smac->expansion);
2031 }
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002032 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002033 smtbl = ctx ? &ctx->localmac : &smacros;
2034 smhead = (SMacro **) hash_findi_add(smtbl, mname);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002035 smac = nasm_malloc(sizeof(SMacro));
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002036 smac->next = *smhead;
2037 *smhead = smac;
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002038 }
2039 smac->name = nasm_strdup(mname);
2040 smac->casesense = casesense;
2041 smac->nparam = nparam;
2042 smac->expansion = expansion;
2043 smac->in_progress = false;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002044 return true; /* Success */
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002045}
2046
2047/*
2048 * Undefine an smacro
2049 */
2050static void undef_smacro(Context *ctx, const char *mname)
2051{
2052 SMacro **smhead, *s, **sp;
H. Peter Anvin166c2472008-05-28 12:28:58 -07002053 struct hash_table *smtbl;
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002054
H. Peter Anvin166c2472008-05-28 12:28:58 -07002055 smtbl = ctx ? &ctx->localmac : &smacros;
2056 smhead = (SMacro **)hash_findi(smtbl, mname, NULL);
H. Peter Anvin70653092007-10-19 14:42:29 -07002057
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002058 if (smhead) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002059 /*
2060 * We now have a macro name... go hunt for it.
2061 */
2062 sp = smhead;
2063 while ((s = *sp) != NULL) {
2064 if (!mstrcmp(s->name, mname, s->casesense)) {
2065 *sp = s->next;
2066 nasm_free(s->name);
2067 free_tlist(s->expansion);
2068 nasm_free(s);
2069 } else {
2070 sp = &s->next;
2071 }
2072 }
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002073 }
2074}
2075
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002076/*
H. Peter Anvina26433d2008-07-16 14:40:01 -07002077 * Parse a mmacro specification.
2078 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002079static bool parse_mmacro_spec(Token *tline, MMacro *def, const char *directive)
H. Peter Anvina26433d2008-07-16 14:40:01 -07002080{
2081 bool err;
2082
2083 tline = tline->next;
2084 skip_white_(tline);
2085 tline = expand_id(tline);
2086 if (!tok_type_(tline, TOK_ID)) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002087 error(ERR_NONFATAL, "`%s' expects a macro name", directive);
2088 return false;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002089 }
Victor van den Elzenb916ede2008-07-23 15:14:22 +02002090
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002091 def->prev = NULL;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002092 def->name = nasm_strdup(tline->text);
2093 def->plus = false;
2094 def->nolist = false;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002095 def->in_progress = 0;
2096 def->rep_nest = NULL;
Victor van den Elzenb916ede2008-07-23 15:14:22 +02002097 def->nparam_min = 0;
2098 def->nparam_max = 0;
2099
H. Peter Anvina26433d2008-07-16 14:40:01 -07002100 tline = expand_smacro(tline->next);
2101 skip_white_(tline);
2102 if (!tok_type_(tline, TOK_NUMBER)) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002103 error(ERR_NONFATAL, "`%s' expects a parameter count", directive);
H. Peter Anvina26433d2008-07-16 14:40:01 -07002104 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002105 def->nparam_min = def->nparam_max =
2106 readnum(tline->text, &err);
2107 if (err)
2108 error(ERR_NONFATAL,
2109 "unable to parse parameter count `%s'", tline->text);
H. Peter Anvina26433d2008-07-16 14:40:01 -07002110 }
2111 if (tline && tok_is_(tline->next, "-")) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002112 tline = tline->next->next;
2113 if (tok_is_(tline, "*")) {
2114 def->nparam_max = INT_MAX;
2115 } else if (!tok_type_(tline, TOK_NUMBER)) {
2116 error(ERR_NONFATAL,
2117 "`%s' expects a parameter count after `-'", directive);
2118 } else {
2119 def->nparam_max = readnum(tline->text, &err);
2120 if (err) {
2121 error(ERR_NONFATAL, "unable to parse parameter count `%s'",
2122 tline->text);
2123 }
2124 if (def->nparam_min > def->nparam_max) {
2125 error(ERR_NONFATAL, "minimum parameter count exceeds maximum");
2126 }
2127 }
H. Peter Anvina26433d2008-07-16 14:40:01 -07002128 }
2129 if (tline && tok_is_(tline->next, "+")) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002130 tline = tline->next;
2131 def->plus = true;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002132 }
2133 if (tline && tok_type_(tline->next, TOK_ID) &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002134 !nasm_stricmp(tline->next->text, ".nolist")) {
2135 tline = tline->next;
2136 def->nolist = true;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002137 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002138
H. Peter Anvina26433d2008-07-16 14:40:01 -07002139 /*
2140 * Handle default parameters.
2141 */
2142 if (tline && tline->next) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002143 def->dlist = tline->next;
2144 tline->next = NULL;
2145 count_mmac_params(def->dlist, &def->ndefs, &def->defaults);
H. Peter Anvina26433d2008-07-16 14:40:01 -07002146 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002147 def->dlist = NULL;
2148 def->defaults = NULL;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002149 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002150 def->expansion = NULL;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002151
H. Peter Anvin89cee572009-07-15 09:16:54 -04002152 if (def->defaults && def->ndefs > def->nparam_max - def->nparam_min &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002153 !def->plus)
2154 error(ERR_WARNING|ERR_PASS1|ERR_WARN_MDP,
2155 "too many default macro parameters");
Victor van den Elzenb916ede2008-07-23 15:14:22 +02002156
H. Peter Anvina26433d2008-07-16 14:40:01 -07002157 return true;
2158}
2159
2160
2161/*
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002162 * Decode a size directive
2163 */
2164static int parse_size(const char *str) {
2165 static const char *size_names[] =
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002166 { "byte", "dword", "oword", "qword", "tword", "word", "yword" };
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002167 static const int sizes[] =
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002168 { 0, 1, 4, 16, 8, 10, 2, 32 };
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002169
Cyrill Gorcunova7319242010-06-03 22:04:36 +04002170 return sizes[bsii(str, size_names, ARRAY_SIZE(size_names))+1];
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002171}
2172
Ed Beroset3ab3f412002-06-11 03:31:49 +00002173/**
2174 * find and process preprocessor directive in passed line
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002175 * Find out if a line contains a preprocessor directive, and deal
2176 * with it if so.
H. Peter Anvin70653092007-10-19 14:42:29 -07002177 *
Ed Beroset3ab3f412002-06-11 03:31:49 +00002178 * If a directive _is_ found, it is the responsibility of this routine
2179 * (and not the caller) to free_tlist() the line.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002180 *
Ed Beroset3ab3f412002-06-11 03:31:49 +00002181 * @param tline a pointer to the current tokeninzed line linked list
2182 * @return DIRECTIVE_FOUND or NO_DIRECTIVE_FOUND
H. Peter Anvin70653092007-10-19 14:42:29 -07002183 *
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002184 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002185static int do_directive(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00002186{
H. Peter Anvin4169a472007-09-12 01:29:43 +00002187 enum preproc_token i;
2188 int j;
H. Peter Anvin70055962007-10-11 00:05:31 -07002189 bool err;
2190 int nparam;
2191 bool nolist;
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07002192 bool casesense;
H. Peter Anvin8cfdb9d2007-09-14 18:36:01 -07002193 int k, m;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002194 int offset;
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08002195 char *p, *pp;
2196 const char *mname;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002197 Include *inc;
2198 Context *ctx;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002199 Cond *cond;
2200 MMacro *mmac, **mmhead;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002201 Token *t, *tt, *param_start, *macro_start, *last, **tptr, *origline;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002202 Line *l;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002203 struct tokenval tokval;
2204 expr *evalresult;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002205 MMacro *tmp_defining; /* Used when manipulating rep_nest */
H. Peter Anvinf8ba53e2007-10-11 10:11:57 -07002206 int64_t count;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07002207 size_t len;
H. Peter Anvin8e3f75e2008-09-24 00:21:58 -07002208 int severity;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002209
2210 origline = tline;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002211
H. Peter Anvineba20a72002-04-30 20:53:55 +00002212 skip_white_(tline);
H. Peter Anvinf2936d72008-06-04 15:11:23 -07002213 if (!tline || !tok_type_(tline, TOK_PREPROC_ID) ||
H. Peter Anvine2c80182005-01-15 22:15:51 +00002214 (tline->text[1] == '%' || tline->text[1] == '$'
2215 || tline->text[1] == '!'))
2216 return NO_DIRECTIVE_FOUND;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002217
H. Peter Anvin4169a472007-09-12 01:29:43 +00002218 i = pp_token_hash(tline->text);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002219
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002220 /*
2221 * FIXME: We zap execution of PP_RMACRO, PP_IRMACRO, PP_EXITMACRO
2222 * since they are known to be buggy at moment, we need to fix them
2223 * in future release (2.09-2.10)
2224 */
2225 if (i == PP_RMACRO || i == PP_RMACRO || i == PP_EXITMACRO) {
2226 error(ERR_NONFATAL, "unknown preprocessor directive `%s'",
2227 tline->text);
2228 return NO_DIRECTIVE_FOUND;
2229 }
2230
2231 /*
2232 * If we're in a non-emitting branch of a condition construct,
2233 * or walking to the end of an already terminated %rep block,
2234 * we should ignore all directives except for condition
2235 * directives.
2236 */
2237 if (((istk->conds && !emitting(istk->conds->state)) ||
2238 (istk->mstk && !istk->mstk->in_progress)) && !is_condition(i)) {
2239 return NO_DIRECTIVE_FOUND;
2240 }
2241
2242 /*
2243 * If we're defining a macro or reading a %rep block, we should
2244 * ignore all directives except for %macro/%imacro (which nest),
2245 * %endm/%endmacro, and (only if we're in a %rep block) %endrep.
2246 * If we're in a %rep block, another %rep nests, so should be let through.
2247 */
2248 if (defining && i != PP_MACRO && i != PP_IMACRO &&
2249 i != PP_RMACRO && i != PP_IRMACRO &&
2250 i != PP_ENDMACRO && i != PP_ENDM &&
2251 (defining->name || (i != PP_ENDREP && i != PP_REP))) {
2252 return NO_DIRECTIVE_FOUND;
2253 }
2254
2255 if (defining) {
2256 if (i == PP_MACRO || i == PP_IMACRO ||
2257 i == PP_RMACRO || i == PP_IRMACRO) {
2258 nested_mac_count++;
2259 return NO_DIRECTIVE_FOUND;
2260 } else if (nested_mac_count > 0) {
2261 if (i == PP_ENDMACRO) {
2262 nested_mac_count--;
2263 return NO_DIRECTIVE_FOUND;
2264 }
2265 }
2266 if (!defining->name) {
2267 if (i == PP_REP) {
2268 nested_rep_count++;
2269 return NO_DIRECTIVE_FOUND;
2270 } else if (nested_rep_count > 0) {
2271 if (i == PP_ENDREP) {
2272 nested_rep_count--;
2273 return NO_DIRECTIVE_FOUND;
2274 }
2275 }
2276 }
2277 }
2278
H. Peter Anvin4169a472007-09-12 01:29:43 +00002279 switch (i) {
2280 case PP_INVALID:
H. Peter Anvine2c80182005-01-15 22:15:51 +00002281 error(ERR_NONFATAL, "unknown preprocessor directive `%s'",
2282 tline->text);
2283 return NO_DIRECTIVE_FOUND; /* didn't get it */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002284
H. Peter Anvine2c80182005-01-15 22:15:51 +00002285 case PP_STACKSIZE:
2286 /* Directive to tell NASM what the default stack size is. The
2287 * default is for a 16-bit stack, and this can be overriden with
2288 * %stacksize large.
H. Peter Anvine2c80182005-01-15 22:15:51 +00002289 */
2290 tline = tline->next;
2291 if (tline && tline->type == TOK_WHITESPACE)
2292 tline = tline->next;
2293 if (!tline || tline->type != TOK_ID) {
2294 error(ERR_NONFATAL, "`%%stacksize' missing size parameter");
2295 free_tlist(origline);
2296 return DIRECTIVE_FOUND;
2297 }
2298 if (nasm_stricmp(tline->text, "flat") == 0) {
2299 /* All subsequent ARG directives are for a 32-bit stack */
2300 StackSize = 4;
2301 StackPointer = "ebp";
2302 ArgOffset = 8;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002303 LocalOffset = 0;
Charles Crayne7eaf9192007-11-08 22:11:14 -08002304 } else if (nasm_stricmp(tline->text, "flat64") == 0) {
2305 /* All subsequent ARG directives are for a 64-bit stack */
2306 StackSize = 8;
2307 StackPointer = "rbp";
Per Jessen53252e02010-02-11 00:16:59 +03002308 ArgOffset = 16;
Charles Crayne7eaf9192007-11-08 22:11:14 -08002309 LocalOffset = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002310 } else if (nasm_stricmp(tline->text, "large") == 0) {
2311 /* All subsequent ARG directives are for a 16-bit stack,
2312 * far function call.
2313 */
2314 StackSize = 2;
2315 StackPointer = "bp";
2316 ArgOffset = 4;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002317 LocalOffset = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002318 } else if (nasm_stricmp(tline->text, "small") == 0) {
2319 /* All subsequent ARG directives are for a 16-bit stack,
2320 * far function call. We don't support near functions.
2321 */
2322 StackSize = 2;
2323 StackPointer = "bp";
2324 ArgOffset = 6;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002325 LocalOffset = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002326 } else {
2327 error(ERR_NONFATAL, "`%%stacksize' invalid size type");
2328 free_tlist(origline);
2329 return DIRECTIVE_FOUND;
2330 }
2331 free_tlist(origline);
2332 return DIRECTIVE_FOUND;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002333
H. Peter Anvine2c80182005-01-15 22:15:51 +00002334 case PP_ARG:
2335 /* TASM like ARG directive to define arguments to functions, in
2336 * the following form:
2337 *
2338 * ARG arg1:WORD, arg2:DWORD, arg4:QWORD
2339 */
2340 offset = ArgOffset;
2341 do {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00002342 char *arg, directive[256];
H. Peter Anvine2c80182005-01-15 22:15:51 +00002343 int size = StackSize;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002344
H. Peter Anvine2c80182005-01-15 22:15:51 +00002345 /* Find the argument name */
2346 tline = tline->next;
2347 if (tline && tline->type == TOK_WHITESPACE)
2348 tline = tline->next;
2349 if (!tline || tline->type != TOK_ID) {
2350 error(ERR_NONFATAL, "`%%arg' missing argument parameter");
2351 free_tlist(origline);
2352 return DIRECTIVE_FOUND;
2353 }
2354 arg = tline->text;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002355
H. Peter Anvine2c80182005-01-15 22:15:51 +00002356 /* Find the argument size type */
2357 tline = tline->next;
2358 if (!tline || tline->type != TOK_OTHER
2359 || tline->text[0] != ':') {
2360 error(ERR_NONFATAL,
2361 "Syntax error processing `%%arg' directive");
2362 free_tlist(origline);
2363 return DIRECTIVE_FOUND;
2364 }
2365 tline = tline->next;
2366 if (!tline || tline->type != TOK_ID) {
2367 error(ERR_NONFATAL, "`%%arg' missing size type parameter");
2368 free_tlist(origline);
2369 return DIRECTIVE_FOUND;
2370 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002371
H. Peter Anvine2c80182005-01-15 22:15:51 +00002372 /* Allow macro expansion of type parameter */
Keith Kaniosb7a89542007-04-12 02:40:54 +00002373 tt = tokenize(tline->text);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002374 tt = expand_smacro(tt);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002375 size = parse_size(tt->text);
2376 if (!size) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002377 error(ERR_NONFATAL,
2378 "Invalid size type for `%%arg' missing directive");
2379 free_tlist(tt);
2380 free_tlist(origline);
2381 return DIRECTIVE_FOUND;
2382 }
2383 free_tlist(tt);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002384
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002385 /* Round up to even stack slots */
2386 size = ALIGN(size, StackSize);
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002387
H. Peter Anvine2c80182005-01-15 22:15:51 +00002388 /* Now define the macro for the argument */
2389 snprintf(directive, sizeof(directive), "%%define %s (%s+%d)",
2390 arg, StackPointer, offset);
Keith Kaniosb7a89542007-04-12 02:40:54 +00002391 do_directive(tokenize(directive));
H. Peter Anvine2c80182005-01-15 22:15:51 +00002392 offset += size;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002393
H. Peter Anvine2c80182005-01-15 22:15:51 +00002394 /* Move to the next argument in the list */
2395 tline = tline->next;
2396 if (tline && tline->type == TOK_WHITESPACE)
2397 tline = tline->next;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002398 } while (tline && tline->type == TOK_OTHER && tline->text[0] == ',');
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002399 ArgOffset = offset;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002400 free_tlist(origline);
2401 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002402
H. Peter Anvine2c80182005-01-15 22:15:51 +00002403 case PP_LOCAL:
2404 /* TASM like LOCAL directive to define local variables for a
2405 * function, in the following form:
2406 *
2407 * LOCAL local1:WORD, local2:DWORD, local4:QWORD = LocalSize
2408 *
2409 * The '= LocalSize' at the end is ignored by NASM, but is
2410 * required by TASM to define the local parameter size (and used
2411 * by the TASM macro package).
2412 */
2413 offset = LocalOffset;
2414 do {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00002415 char *local, directive[256];
H. Peter Anvine2c80182005-01-15 22:15:51 +00002416 int size = StackSize;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002417
H. Peter Anvine2c80182005-01-15 22:15:51 +00002418 /* Find the argument name */
2419 tline = tline->next;
2420 if (tline && tline->type == TOK_WHITESPACE)
2421 tline = tline->next;
2422 if (!tline || tline->type != TOK_ID) {
2423 error(ERR_NONFATAL,
2424 "`%%local' missing argument parameter");
2425 free_tlist(origline);
2426 return DIRECTIVE_FOUND;
2427 }
2428 local = tline->text;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002429
H. Peter Anvine2c80182005-01-15 22:15:51 +00002430 /* Find the argument size type */
2431 tline = tline->next;
2432 if (!tline || tline->type != TOK_OTHER
2433 || tline->text[0] != ':') {
2434 error(ERR_NONFATAL,
2435 "Syntax error processing `%%local' directive");
2436 free_tlist(origline);
2437 return DIRECTIVE_FOUND;
2438 }
2439 tline = tline->next;
2440 if (!tline || tline->type != TOK_ID) {
2441 error(ERR_NONFATAL,
2442 "`%%local' missing size type parameter");
2443 free_tlist(origline);
2444 return DIRECTIVE_FOUND;
2445 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00002446
H. Peter Anvine2c80182005-01-15 22:15:51 +00002447 /* Allow macro expansion of type parameter */
Keith Kaniosb7a89542007-04-12 02:40:54 +00002448 tt = tokenize(tline->text);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002449 tt = expand_smacro(tt);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002450 size = parse_size(tt->text);
2451 if (!size) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002452 error(ERR_NONFATAL,
2453 "Invalid size type for `%%local' missing directive");
2454 free_tlist(tt);
2455 free_tlist(origline);
2456 return DIRECTIVE_FOUND;
2457 }
2458 free_tlist(tt);
H. Peter Anvin734b1882002-04-30 21:01:08 +00002459
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002460 /* Round up to even stack slots */
2461 size = ALIGN(size, StackSize);
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002462
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002463 offset += size; /* Negative offset, increment before */
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002464
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002465 /* Now define the macro for the argument */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002466 snprintf(directive, sizeof(directive), "%%define %s (%s-%d)",
2467 local, StackPointer, offset);
Keith Kaniosb7a89542007-04-12 02:40:54 +00002468 do_directive(tokenize(directive));
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002469
H. Peter Anvine2c80182005-01-15 22:15:51 +00002470 /* Now define the assign to setup the enter_c macro correctly */
2471 snprintf(directive, sizeof(directive),
2472 "%%assign %%$localsize %%$localsize+%d", size);
Keith Kaniosb7a89542007-04-12 02:40:54 +00002473 do_directive(tokenize(directive));
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002474
H. Peter Anvine2c80182005-01-15 22:15:51 +00002475 /* Move to the next argument in the list */
2476 tline = tline->next;
2477 if (tline && tline->type == TOK_WHITESPACE)
2478 tline = tline->next;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002479 } while (tline && tline->type == TOK_OTHER && tline->text[0] == ',');
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002480 LocalOffset = offset;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002481 free_tlist(origline);
2482 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002483
H. Peter Anvine2c80182005-01-15 22:15:51 +00002484 case PP_CLEAR:
2485 if (tline->next)
H. Peter Anvin917a3492008-09-24 09:14:49 -07002486 error(ERR_WARNING|ERR_PASS1,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002487 "trailing garbage after `%%clear' ignored");
2488 free_macros();
2489 init_macros();
H. Peter Anvine2c80182005-01-15 22:15:51 +00002490 free_tlist(origline);
2491 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002492
H. Peter Anvin418ca702008-05-30 10:42:30 -07002493 case PP_DEPEND:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002494 t = tline->next = expand_smacro(tline->next);
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002495 skip_white_(t);
2496 if (!t || (t->type != TOK_STRING &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002497 t->type != TOK_INTERNAL_STRING)) {
H. Peter Anvin418ca702008-05-30 10:42:30 -07002498 error(ERR_NONFATAL, "`%%depend' expects a file name");
2499 free_tlist(origline);
2500 return DIRECTIVE_FOUND; /* but we did _something_ */
2501 }
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002502 if (t->next)
H. Peter Anvin917a3492008-09-24 09:14:49 -07002503 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvin418ca702008-05-30 10:42:30 -07002504 "trailing garbage after `%%depend' ignored");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002505 p = t->text;
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002506 if (t->type != TOK_INTERNAL_STRING)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002507 nasm_unquote_cstr(p, i);
2508 if (dephead && !in_list(*dephead, p)) {
2509 StrList *sl = nasm_malloc(strlen(p)+1+sizeof sl->next);
2510 sl->next = NULL;
2511 strcpy(sl->str, p);
2512 *deptail = sl;
2513 deptail = &sl->next;
2514 }
2515 free_tlist(origline);
H. Peter Anvin418ca702008-05-30 10:42:30 -07002516 return DIRECTIVE_FOUND;
2517
2518 case PP_INCLUDE:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002519 t = tline->next = expand_smacro(tline->next);
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002520 skip_white_(t);
H. Peter Anvind2456592008-06-19 15:04:18 -07002521
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002522 if (!t || (t->type != TOK_STRING &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002523 t->type != TOK_INTERNAL_STRING)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002524 error(ERR_NONFATAL, "`%%include' expects a file name");
2525 free_tlist(origline);
2526 return DIRECTIVE_FOUND; /* but we did _something_ */
2527 }
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002528 if (t->next)
H. Peter Anvin917a3492008-09-24 09:14:49 -07002529 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +00002530 "trailing garbage after `%%include' ignored");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002531 p = t->text;
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002532 if (t->type != TOK_INTERNAL_STRING)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002533 nasm_unquote_cstr(p, i);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002534 inc = nasm_malloc(sizeof(Include));
H. Peter Anvine2c80182005-01-15 22:15:51 +00002535 inc->next = istk;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002536 inc->conds = NULL;
H. Peter Anvin2b1c3b92008-06-06 10:38:46 -07002537 inc->fp = inc_fopen(p, dephead, &deptail, pass == 0);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002538 if (!inc->fp) {
2539 /* -MG given but file not found */
2540 nasm_free(inc);
2541 } else {
2542 inc->fname = src_set_fname(nasm_strdup(p));
2543 inc->lineno = src_set_linnum(0);
2544 inc->lineinc = 1;
2545 inc->expansion = NULL;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002546 inc->mstk = NULL;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002547 istk = inc;
2548 list->uplevel(LIST_INCLUDE);
2549 }
2550 free_tlist(origline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002551 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002552
H. Peter Anvind2456592008-06-19 15:04:18 -07002553 case PP_USE:
H. Peter Anvinf4ae5ad2008-06-19 18:39:24 -07002554 {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002555 static macros_t *use_pkg;
2556 const char *pkg_macro = NULL;
H. Peter Anvinf4ae5ad2008-06-19 18:39:24 -07002557
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002558 tline = tline->next;
2559 skip_white_(tline);
2560 tline = expand_id(tline);
H. Peter Anvind2456592008-06-19 15:04:18 -07002561
H. Peter Anvin264b7b92008-10-24 16:38:17 -07002562 if (!tline || (tline->type != TOK_STRING &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002563 tline->type != TOK_INTERNAL_STRING &&
2564 tline->type != TOK_ID)) {
H. Peter Anvin926fc402008-06-19 16:26:12 -07002565 error(ERR_NONFATAL, "`%%use' expects a package name");
H. Peter Anvind2456592008-06-19 15:04:18 -07002566 free_tlist(origline);
2567 return DIRECTIVE_FOUND; /* but we did _something_ */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002568 }
H. Peter Anvin264b7b92008-10-24 16:38:17 -07002569 if (tline->next)
H. Peter Anvin917a3492008-09-24 09:14:49 -07002570 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvind2456592008-06-19 15:04:18 -07002571 "trailing garbage after `%%use' ignored");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002572 if (tline->type == TOK_STRING)
2573 nasm_unquote_cstr(tline->text, i);
2574 use_pkg = nasm_stdmac_find_package(tline->text);
2575 if (!use_pkg)
2576 error(ERR_NONFATAL, "unknown `%%use' package: %s", tline->text);
2577 else
2578 pkg_macro = (char *)use_pkg + 1; /* The first string will be <%define>__USE_*__ */
Victor van den Elzen35eb2ea2010-03-10 22:33:48 +01002579 if (use_pkg && ! smacro_defined(NULL, pkg_macro, 0, NULL, true)) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002580 /* Not already included, go ahead and include it */
2581 stdmacpos = use_pkg;
2582 }
2583 free_tlist(origline);
H. Peter Anvind2456592008-06-19 15:04:18 -07002584 return DIRECTIVE_FOUND;
H. Peter Anvinf4ae5ad2008-06-19 18:39:24 -07002585 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002586 case PP_PUSH:
H. Peter Anvine2c80182005-01-15 22:15:51 +00002587 case PP_REPL:
H. Peter Anvin42b56392008-10-24 16:24:21 -07002588 case PP_POP:
H. Peter Anvine2c80182005-01-15 22:15:51 +00002589 tline = tline->next;
2590 skip_white_(tline);
2591 tline = expand_id(tline);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002592 if (tline) {
2593 if (!tok_type_(tline, TOK_ID)) {
2594 error(ERR_NONFATAL, "`%s' expects a context identifier",
2595 pp_directives[i]);
2596 free_tlist(origline);
2597 return DIRECTIVE_FOUND; /* but we did _something_ */
2598 }
2599 if (tline->next)
2600 error(ERR_WARNING|ERR_PASS1,
2601 "trailing garbage after `%s' ignored",
2602 pp_directives[i]);
2603 p = nasm_strdup(tline->text);
2604 } else {
2605 p = NULL; /* Anonymous */
2606 }
H. Peter Anvin42b56392008-10-24 16:24:21 -07002607
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002608 if (i == PP_PUSH) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002609 ctx = nasm_malloc(sizeof(Context));
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002610 ctx->next = cstk;
2611 hash_init(&ctx->localmac, HASH_SMALL);
2612 ctx->name = p;
2613 ctx->number = unique++;
2614 cstk = ctx;
2615 } else {
2616 /* %pop or %repl */
2617 if (!cstk) {
2618 error(ERR_NONFATAL, "`%s': context stack is empty",
2619 pp_directives[i]);
2620 } else if (i == PP_POP) {
2621 if (p && (!cstk->name || nasm_stricmp(p, cstk->name)))
2622 error(ERR_NONFATAL, "`%%pop' in wrong context: %s, "
2623 "expected %s",
2624 cstk->name ? cstk->name : "anonymous", p);
2625 else
2626 ctx_pop();
2627 } else {
2628 /* i == PP_REPL */
2629 nasm_free(cstk->name);
2630 cstk->name = p;
2631 p = NULL;
2632 }
2633 nasm_free(p);
2634 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002635 free_tlist(origline);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002636 return DIRECTIVE_FOUND;
H. Peter Anvin8e3f75e2008-09-24 00:21:58 -07002637 case PP_FATAL:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002638 severity = ERR_FATAL;
2639 goto issue_error;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002640 case PP_ERROR:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002641 severity = ERR_NONFATAL;
2642 goto issue_error;
H. Peter Anvin7df04172008-06-10 18:27:38 -07002643 case PP_WARNING:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002644 severity = ERR_WARNING|ERR_WARN_USER;
2645 goto issue_error;
H. Peter Anvin8e3f75e2008-09-24 00:21:58 -07002646
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002647issue_error:
H. Peter Anvin7df04172008-06-10 18:27:38 -07002648 {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002649 /* Only error out if this is the final pass */
2650 if (pass != 2 && i != PP_FATAL)
2651 return DIRECTIVE_FOUND;
2652
2653 tline->next = expand_smacro(tline->next);
2654 tline = tline->next;
2655 skip_white_(tline);
2656 t = tline ? tline->next : NULL;
2657 skip_white_(t);
2658 if (tok_type_(tline, TOK_STRING) && !t) {
2659 /* The line contains only a quoted string */
2660 p = tline->text;
2661 nasm_unquote(p, NULL); /* Ignore NUL character truncation */
2662 error(severity, "%s", p);
2663 } else {
2664 /* Not a quoted string, or more than a quoted string */
2665 p = detoken(tline, false);
2666 error(severity, "%s", p);
2667 nasm_free(p);
2668 }
2669 free_tlist(origline);
2670 return DIRECTIVE_FOUND;
H. Peter Anvin7df04172008-06-10 18:27:38 -07002671 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00002672
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002673 CASE_PP_IF:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002674 if (istk->conds && !emitting(istk->conds->state))
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002675 j = COND_NEVER;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002676 else {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002677 j = if_condition(tline->next, i);
2678 tline->next = NULL; /* it got freed */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002679 j = j < 0 ? COND_NEVER : j ? COND_IF_TRUE : COND_IF_FALSE;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002680 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002681 cond = nasm_malloc(sizeof(Cond));
2682 cond->next = istk->conds;
2683 cond->state = j;
2684 istk->conds = cond;
2685 if(istk->mstk)
2686 istk->mstk->condcnt ++;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002687 free_tlist(origline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002688 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002689
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002690 CASE_PP_ELIF:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002691 if (!istk->conds)
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002692 error(ERR_FATAL, "`%s': no matching `%%if'", pp_directives[i]);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002693 switch(istk->conds->state) {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002694 case COND_IF_TRUE:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002695 istk->conds->state = COND_DONE;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002696 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002697
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002698 case COND_DONE:
2699 case COND_NEVER:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002700 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002701
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002702 case COND_ELSE_TRUE:
2703 case COND_ELSE_FALSE:
2704 error_precond(ERR_WARNING|ERR_PASS1,
2705 "`%%elif' after `%%else' ignored");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002706 istk->conds->state = COND_NEVER;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002707 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002708
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002709 case COND_IF_FALSE:
2710 /*
2711 * IMPORTANT: In the case of %if, we will already have
2712 * called expand_mmac_params(); however, if we're
2713 * processing an %elif we must have been in a
2714 * non-emitting mode, which would have inhibited
2715 * the normal invocation of expand_mmac_params().
2716 * Therefore, we have to do it explicitly here.
2717 */
2718 j = if_condition(expand_mmac_params(tline->next), i);
2719 tline->next = NULL; /* it got freed */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002720 istk->conds->state =
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002721 j < 0 ? COND_NEVER : j ? COND_IF_TRUE : COND_IF_FALSE;
2722 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002723 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002724 free_tlist(origline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002725 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002726
H. Peter Anvine2c80182005-01-15 22:15:51 +00002727 case PP_ELSE:
2728 if (tline->next)
H. Peter Anvin917a3492008-09-24 09:14:49 -07002729 error_precond(ERR_WARNING|ERR_PASS1,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002730 "trailing garbage after `%%else' ignored");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002731 if (!istk->conds)
2732 error(ERR_FATAL, "`%%else': no matching `%%if'");
2733 switch(istk->conds->state) {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002734 case COND_IF_TRUE:
2735 case COND_DONE:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002736 istk->conds->state = COND_ELSE_FALSE;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002737 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002738
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002739 case COND_NEVER:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002740 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002741
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002742 case COND_IF_FALSE:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002743 istk->conds->state = COND_ELSE_TRUE;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002744 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002745
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002746 case COND_ELSE_TRUE:
2747 case COND_ELSE_FALSE:
2748 error_precond(ERR_WARNING|ERR_PASS1,
2749 "`%%else' after `%%else' ignored.");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002750 istk->conds->state = COND_NEVER;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002751 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002752 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002753 free_tlist(origline);
2754 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002755
H. Peter Anvine2c80182005-01-15 22:15:51 +00002756 case PP_ENDIF:
2757 if (tline->next)
H. Peter Anvin917a3492008-09-24 09:14:49 -07002758 error_precond(ERR_WARNING|ERR_PASS1,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002759 "trailing garbage after `%%endif' ignored");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002760 if (!istk->conds)
2761 error(ERR_FATAL, "`%%endif': no matching `%%if'");
2762 cond = istk->conds;
2763 istk->conds = cond->next;
2764 nasm_free(cond);
2765 if(istk->mstk)
2766 istk->mstk->condcnt --;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002767 free_tlist(origline);
2768 return DIRECTIVE_FOUND;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002769
H. Peter Anvindb8f96e2009-07-15 09:07:29 -04002770 case PP_RMACRO:
2771 case PP_IRMACRO:
H. Peter Anvine2c80182005-01-15 22:15:51 +00002772 case PP_MACRO:
2773 case PP_IMACRO:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002774 if (defining) {
2775 error(ERR_FATAL, "`%s': already defining a macro",
2776 pp_directives[i]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002777 return DIRECTIVE_FOUND;
2778 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002779 defining = nasm_malloc(sizeof(MMacro));
2780 defining->max_depth =
2781 (i == PP_RMACRO) || (i == PP_IRMACRO) ? DEADMAN_LIMIT : 0;
2782 defining->casesense = (i == PP_MACRO) || (i == PP_RMACRO);
2783 if (!parse_mmacro_spec(tline, defining, pp_directives[i])) {
2784 nasm_free(defining);
2785 defining = NULL;
2786 return DIRECTIVE_FOUND;
2787 }
H. Peter Anvina26433d2008-07-16 14:40:01 -07002788
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002789 mmac = (MMacro *) hash_findix(&mmacros, defining->name);
2790 while (mmac) {
2791 if (!strcmp(mmac->name, defining->name) &&
2792 (mmac->nparam_min <= defining->nparam_max
2793 || defining->plus)
2794 && (defining->nparam_min <= mmac->nparam_max
2795 || mmac->plus)) {
2796 error(ERR_WARNING|ERR_PASS1,
2797 "redefining multi-line macro `%s'", defining->name);
2798 return DIRECTIVE_FOUND;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002799 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002800 mmac = mmac->next;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002801 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002802 free_tlist(origline);
2803 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002804
H. Peter Anvine2c80182005-01-15 22:15:51 +00002805 case PP_ENDM:
2806 case PP_ENDMACRO:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002807 if (! (defining && defining->name)) {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002808 error(ERR_NONFATAL, "`%s': not defining a macro", tline->text);
2809 return DIRECTIVE_FOUND;
2810 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002811 mmhead = (MMacro **) hash_findi_add(&mmacros, defining->name);
2812 defining->next = *mmhead;
2813 *mmhead = defining;
2814 defining = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002815 free_tlist(origline);
2816 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002817
H. Peter Anvin89cee572009-07-15 09:16:54 -04002818 case PP_EXITMACRO:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002819 /*
2820 * We must search along istk->expansion until we hit a
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002821 * macro-end marker for a macro with a name. Then we
2822 * bypass all lines between exitmacro and endmacro.
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002823 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002824 list_for_each(l, istk->expansion)
2825 if (l->finishes && l->finishes->name)
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002826 break;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002827
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002828 if (l) {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002829 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002830 * Remove all conditional entries relative to this
2831 * macro invocation. (safe to do in this context)
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002832 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002833 for ( ; l->finishes->condcnt > 0; l->finishes->condcnt --) {
2834 cond = istk->conds;
2835 istk->conds = cond->next;
2836 nasm_free(cond);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002837 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002838 istk->expansion = l;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002839 } else {
2840 error(ERR_NONFATAL, "`%%exitmacro' not within `%%macro' block");
2841 }
Keith Kanios852f1ee2009-07-12 00:19:55 -05002842 free_tlist(origline);
2843 return DIRECTIVE_FOUND;
2844
H. Peter Anvina26433d2008-07-16 14:40:01 -07002845 case PP_UNMACRO:
2846 case PP_UNIMACRO:
2847 {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002848 MMacro **mmac_p;
2849 MMacro spec;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002850
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002851 spec.casesense = (i == PP_UNMACRO);
2852 if (!parse_mmacro_spec(tline, &spec, pp_directives[i])) {
2853 return DIRECTIVE_FOUND;
2854 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002855 mmac_p = (MMacro **) hash_findi(&mmacros, spec.name, NULL);
2856 while (mmac_p && *mmac_p) {
2857 mmac = *mmac_p;
2858 if (mmac->casesense == spec.casesense &&
2859 !mstrcmp(mmac->name, spec.name, spec.casesense) &&
2860 mmac->nparam_min == spec.nparam_min &&
2861 mmac->nparam_max == spec.nparam_max &&
2862 mmac->plus == spec.plus) {
2863 *mmac_p = mmac->next;
2864 free_mmacro(mmac);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002865 } else {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002866 mmac_p = &mmac->next;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002867 }
2868 }
2869 free_tlist(origline);
2870 free_tlist(spec.dlist);
2871 return DIRECTIVE_FOUND;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002872 }
2873
H. Peter Anvine2c80182005-01-15 22:15:51 +00002874 case PP_ROTATE:
2875 if (tline->next && tline->next->type == TOK_WHITESPACE)
2876 tline = tline->next;
H. Peter Anvin89cee572009-07-15 09:16:54 -04002877 if (!tline->next) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002878 free_tlist(origline);
2879 error(ERR_NONFATAL, "`%%rotate' missing rotate count");
2880 return DIRECTIVE_FOUND;
2881 }
2882 t = expand_smacro(tline->next);
2883 tline->next = NULL;
2884 free_tlist(origline);
2885 tline = t;
2886 tptr = &t;
2887 tokval.t_type = TOKEN_INVALID;
2888 evalresult =
2889 evaluate(ppscan, tptr, &tokval, NULL, pass, error, NULL);
2890 free_tlist(tline);
2891 if (!evalresult)
2892 return DIRECTIVE_FOUND;
2893 if (tokval.t_type)
H. Peter Anvin917a3492008-09-24 09:14:49 -07002894 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +00002895 "trailing garbage after expression ignored");
2896 if (!is_simple(evalresult)) {
2897 error(ERR_NONFATAL, "non-constant value given to `%%rotate'");
2898 return DIRECTIVE_FOUND;
2899 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002900 mmac = istk->mstk;
2901 while (mmac && !mmac->name) /* avoid mistaking %reps for macros */
2902 mmac = mmac->next_active;
2903 if (!mmac) {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002904 error(ERR_NONFATAL, "`%%rotate' invoked outside a macro call");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002905 } else if (mmac->nparam == 0) {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002906 error(ERR_NONFATAL,
2907 "`%%rotate' invoked within macro without parameters");
2908 } else {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002909 int rotate = mmac->rotate + reloc_value(evalresult);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002910
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002911 rotate %= (int)mmac->nparam;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002912 if (rotate < 0)
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002913 rotate += mmac->nparam;
2914
2915 mmac->rotate = rotate;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002916 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002917 return DIRECTIVE_FOUND;
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00002918
H. Peter Anvine2c80182005-01-15 22:15:51 +00002919 case PP_REP:
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002920 nolist = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002921 do {
2922 tline = tline->next;
2923 } while (tok_type_(tline, TOK_WHITESPACE));
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00002924
H. Peter Anvine2c80182005-01-15 22:15:51 +00002925 if (tok_type_(tline, TOK_ID) &&
2926 nasm_stricmp(tline->text, ".nolist") == 0) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002927 nolist = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002928 do {
2929 tline = tline->next;
2930 } while (tok_type_(tline, TOK_WHITESPACE));
2931 }
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00002932
H. Peter Anvine2c80182005-01-15 22:15:51 +00002933 if (tline) {
2934 t = expand_smacro(tline);
2935 tptr = &t;
2936 tokval.t_type = TOKEN_INVALID;
2937 evalresult =
2938 evaluate(ppscan, tptr, &tokval, NULL, pass, error, NULL);
2939 if (!evalresult) {
2940 free_tlist(origline);
2941 return DIRECTIVE_FOUND;
2942 }
2943 if (tokval.t_type)
H. Peter Anvin917a3492008-09-24 09:14:49 -07002944 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +00002945 "trailing garbage after expression ignored");
2946 if (!is_simple(evalresult)) {
2947 error(ERR_NONFATAL, "non-constant value given to `%%rep'");
2948 return DIRECTIVE_FOUND;
2949 }
Cyrill Gorcunove091d6e2010-08-09 13:58:22 +04002950 count = reloc_value(evalresult);
2951 if (count >= REP_LIMIT) {
Cyrill Gorcunov71f4f842010-08-09 20:17:17 +04002952 error(ERR_NONFATAL, "`%%rep' value exceeds limit");
Cyrill Gorcunove091d6e2010-08-09 13:58:22 +04002953 count = 0;
2954 } else
2955 count++;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002956 } else {
2957 error(ERR_NONFATAL, "`%%rep' expects a repeat count");
H. Peter Anvinf8ba53e2007-10-11 10:11:57 -07002958 count = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002959 }
2960 free_tlist(origline);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002961
2962 tmp_defining = defining;
2963 defining = nasm_malloc(sizeof(MMacro));
2964 defining->prev = NULL;
2965 defining->name = NULL; /* flags this macro as a %rep block */
2966 defining->casesense = false;
2967 defining->plus = false;
2968 defining->nolist = nolist;
2969 defining->in_progress = count;
2970 defining->max_depth = 0;
2971 defining->nparam_min = defining->nparam_max = 0;
2972 defining->defaults = NULL;
2973 defining->dlist = NULL;
2974 defining->expansion = NULL;
2975 defining->next_active = istk->mstk;
2976 defining->rep_nest = tmp_defining;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002977 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002978
H. Peter Anvine2c80182005-01-15 22:15:51 +00002979 case PP_ENDREP:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002980 if (!defining || defining->name) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002981 error(ERR_NONFATAL, "`%%endrep': no matching `%%rep'");
2982 return DIRECTIVE_FOUND;
2983 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00002984
H. Peter Anvine2c80182005-01-15 22:15:51 +00002985 /*
2986 * Now we have a "macro" defined - although it has no name
2987 * and we won't be entering it in the hash tables - we must
2988 * push a macro-end marker for it on to istk->expansion.
2989 * After that, it will take care of propagating itself (a
2990 * macro-end marker line for a macro which is really a %rep
2991 * block will cause the macro to be re-expanded, complete
2992 * with another macro-end marker to ensure the process
2993 * continues) until the whole expansion is forcibly removed
2994 * from istk->expansion by a %exitrep.
2995 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002996 l = nasm_malloc(sizeof(Line));
2997 l->next = istk->expansion;
2998 l->finishes = defining;
2999 l->first = NULL;
3000 istk->expansion = l;
3001
3002 istk->mstk = defining;
3003
3004 list->uplevel(defining->nolist ? LIST_MACRO_NOLIST : LIST_MACRO);
3005 tmp_defining = defining;
3006 defining = defining->rep_nest;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003007 free_tlist(origline);
3008 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003009
H. Peter Anvine2c80182005-01-15 22:15:51 +00003010 case PP_EXITREP:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003011 /*
3012 * We must search along istk->expansion until we hit a
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003013 * macro-end marker for a macro with no name. Then we set
3014 * its `in_progress' flag to 0.
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003015 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003016 list_for_each(l, istk->expansion)
3017 if (l->finishes && !l->finishes->name)
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003018 break;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003019
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003020 if (l)
3021 l->finishes->in_progress = 1;
3022 else
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003023 error(ERR_NONFATAL, "`%%exitrep' not within `%%rep' block");
H. Peter Anvine2c80182005-01-15 22:15:51 +00003024 free_tlist(origline);
3025 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003026
H. Peter Anvine2c80182005-01-15 22:15:51 +00003027 case PP_XDEFINE:
3028 case PP_IXDEFINE:
3029 case PP_DEFINE:
3030 case PP_IDEFINE:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003031 casesense = (i == PP_DEFINE || i == PP_XDEFINE);
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07003032
H. Peter Anvine2c80182005-01-15 22:15:51 +00003033 tline = tline->next;
3034 skip_white_(tline);
3035 tline = expand_id(tline);
3036 if (!tline || (tline->type != TOK_ID &&
3037 (tline->type != TOK_PREPROC_ID ||
3038 tline->text[1] != '$'))) {
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07003039 error(ERR_NONFATAL, "`%s' expects a macro identifier",
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003040 pp_directives[i]);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003041 free_tlist(origline);
3042 return DIRECTIVE_FOUND;
3043 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003044
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003045 ctx = get_ctx(tline->text, &mname, false);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003046 last = tline;
3047 param_start = tline = tline->next;
3048 nparam = 0;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003049
H. Peter Anvine2c80182005-01-15 22:15:51 +00003050 /* Expand the macro definition now for %xdefine and %ixdefine */
3051 if ((i == PP_XDEFINE) || (i == PP_IXDEFINE))
3052 tline = expand_smacro(tline);
H. Peter Anvin734b1882002-04-30 21:01:08 +00003053
H. Peter Anvine2c80182005-01-15 22:15:51 +00003054 if (tok_is_(tline, "(")) {
3055 /*
3056 * This macro has parameters.
3057 */
H. Peter Anvin734b1882002-04-30 21:01:08 +00003058
H. Peter Anvine2c80182005-01-15 22:15:51 +00003059 tline = tline->next;
3060 while (1) {
3061 skip_white_(tline);
3062 if (!tline) {
3063 error(ERR_NONFATAL, "parameter identifier expected");
3064 free_tlist(origline);
3065 return DIRECTIVE_FOUND;
3066 }
3067 if (tline->type != TOK_ID) {
3068 error(ERR_NONFATAL,
3069 "`%s': parameter identifier expected",
3070 tline->text);
3071 free_tlist(origline);
3072 return DIRECTIVE_FOUND;
3073 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003074 tline->type = TOK_SMAC_PARAM + nparam++;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003075 tline = tline->next;
3076 skip_white_(tline);
3077 if (tok_is_(tline, ",")) {
3078 tline = tline->next;
H. Peter Anvinca348b62008-07-23 10:49:26 -04003079 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003080 if (!tok_is_(tline, ")")) {
3081 error(ERR_NONFATAL,
3082 "`)' expected to terminate macro template");
3083 free_tlist(origline);
3084 return DIRECTIVE_FOUND;
3085 }
3086 break;
3087 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00003088 }
3089 last = tline;
3090 tline = tline->next;
3091 }
3092 if (tok_type_(tline, TOK_WHITESPACE))
3093 last = tline, tline = tline->next;
3094 macro_start = NULL;
3095 last->next = NULL;
3096 t = tline;
3097 while (t) {
3098 if (t->type == TOK_ID) {
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04003099 list_for_each(tt, param_start)
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003100 if (tt->type >= TOK_SMAC_PARAM &&
H. Peter Anvine2c80182005-01-15 22:15:51 +00003101 !strcmp(tt->text, t->text))
3102 t->type = tt->type;
3103 }
3104 tt = t->next;
3105 t->next = macro_start;
3106 macro_start = t;
3107 t = tt;
3108 }
3109 /*
3110 * Good. We now have a macro name, a parameter count, and a
3111 * token list (in reverse order) for an expansion. We ought
3112 * to be OK just to create an SMacro, store it, and let
3113 * free_tlist have the rest of the line (which we have
3114 * carefully re-terminated after chopping off the expansion
3115 * from the end).
3116 */
H. Peter Anvin4db5a162007-10-11 13:42:09 -07003117 define_smacro(ctx, mname, casesense, nparam, macro_start);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003118 free_tlist(origline);
3119 return DIRECTIVE_FOUND;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003120
H. Peter Anvine2c80182005-01-15 22:15:51 +00003121 case PP_UNDEF:
3122 tline = tline->next;
3123 skip_white_(tline);
3124 tline = expand_id(tline);
3125 if (!tline || (tline->type != TOK_ID &&
3126 (tline->type != TOK_PREPROC_ID ||
3127 tline->text[1] != '$'))) {
3128 error(ERR_NONFATAL, "`%%undef' expects a macro identifier");
3129 free_tlist(origline);
3130 return DIRECTIVE_FOUND;
3131 }
3132 if (tline->next) {
H. Peter Anvin917a3492008-09-24 09:14:49 -07003133 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +00003134 "trailing garbage after macro name ignored");
3135 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003136
H. Peter Anvine2c80182005-01-15 22:15:51 +00003137 /* Find the context that symbol belongs to */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003138 ctx = get_ctx(tline->text, &mname, false);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003139 undef_smacro(ctx, mname);
3140 free_tlist(origline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003141 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003142
H. Peter Anvin9e200162008-06-04 17:23:14 -07003143 case PP_DEFSTR:
3144 case PP_IDEFSTR:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003145 casesense = (i == PP_DEFSTR);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003146
3147 tline = tline->next;
3148 skip_white_(tline);
3149 tline = expand_id(tline);
3150 if (!tline || (tline->type != TOK_ID &&
3151 (tline->type != TOK_PREPROC_ID ||
3152 tline->text[1] != '$'))) {
3153 error(ERR_NONFATAL, "`%s' expects a macro identifier",
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003154 pp_directives[i]);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003155 free_tlist(origline);
3156 return DIRECTIVE_FOUND;
3157 }
3158
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003159 ctx = get_ctx(tline->text, &mname, false);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003160 last = tline;
3161 tline = expand_smacro(tline->next);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003162 last->next = NULL;
H. Peter Anvin9e200162008-06-04 17:23:14 -07003163
3164 while (tok_type_(tline, TOK_WHITESPACE))
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003165 tline = delete_Token(tline);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003166
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003167 p = detoken(tline, false);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003168 macro_start = nasm_malloc(sizeof(*macro_start));
3169 macro_start->next = NULL;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003170 macro_start->text = nasm_quote(p, strlen(p));
3171 macro_start->type = TOK_STRING;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003172 macro_start->a.mac = NULL;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003173 nasm_free(p);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003174
3175 /*
3176 * We now have a macro name, an implicit parameter count of
3177 * zero, and a string token to use as an expansion. Create
3178 * and store an SMacro.
3179 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003180 define_smacro(ctx, mname, casesense, 0, macro_start);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003181 free_tlist(origline);
3182 return DIRECTIVE_FOUND;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003183
H. Peter Anvin2f55bda2009-07-14 15:04:04 -04003184 case PP_DEFTOK:
3185 case PP_IDEFTOK:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003186 casesense = (i == PP_DEFTOK);
3187
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003188 tline = tline->next;
3189 skip_white_(tline);
3190 tline = expand_id(tline);
3191 if (!tline || (tline->type != TOK_ID &&
3192 (tline->type != TOK_PREPROC_ID ||
3193 tline->text[1] != '$'))) {
3194 error(ERR_NONFATAL,
3195 "`%s' expects a macro identifier as first parameter",
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003196 pp_directives[i]);
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003197 free_tlist(origline);
3198 return DIRECTIVE_FOUND;
3199 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003200 ctx = get_ctx(tline->text, &mname, false);
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003201 last = tline;
3202 tline = expand_smacro(tline->next);
3203 last->next = NULL;
3204
3205 t = tline;
3206 while (tok_type_(t, TOK_WHITESPACE))
3207 t = t->next;
3208 /* t should now point to the string */
Cyrill Gorcunov6908e582010-09-06 19:36:15 +04003209 if (!tok_type_(t, TOK_STRING)) {
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003210 error(ERR_NONFATAL,
3211 "`%s` requires string as second parameter",
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003212 pp_directives[i]);
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003213 free_tlist(tline);
3214 free_tlist(origline);
3215 return DIRECTIVE_FOUND;
3216 }
3217
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003218 /*
3219 * Convert the string to a token stream. Note that smacros
3220 * are stored with the token stream reversed, so we have to
3221 * reverse the output of tokenize().
3222 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003223 nasm_unquote_cstr(t->text, i);
H. Peter Anvinb40992c2010-09-15 08:57:21 -07003224 macro_start = reverse_tokens(tokenize(t->text));
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003225
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003226 /*
3227 * We now have a macro name, an implicit parameter count of
3228 * zero, and a numeric token to use as an expansion. Create
3229 * and store an SMacro.
3230 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003231 define_smacro(ctx, mname, casesense, 0, macro_start);
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003232 free_tlist(tline);
3233 free_tlist(origline);
3234 return DIRECTIVE_FOUND;
H. Peter Anvin9e200162008-06-04 17:23:14 -07003235
H. Peter Anvin418ca702008-05-30 10:42:30 -07003236 case PP_PATHSEARCH:
3237 {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003238 FILE *fp;
3239 StrList *xsl = NULL;
3240 StrList **xst = &xsl;
H. Peter Anvin418ca702008-05-30 10:42:30 -07003241
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003242 casesense = true;
H. Peter Anvin418ca702008-05-30 10:42:30 -07003243
3244 tline = tline->next;
3245 skip_white_(tline);
3246 tline = expand_id(tline);
3247 if (!tline || (tline->type != TOK_ID &&
3248 (tline->type != TOK_PREPROC_ID ||
3249 tline->text[1] != '$'))) {
3250 error(ERR_NONFATAL,
3251 "`%%pathsearch' expects a macro identifier as first parameter");
3252 free_tlist(origline);
3253 return DIRECTIVE_FOUND;
3254 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003255 ctx = get_ctx(tline->text, &mname, false);
H. Peter Anvin418ca702008-05-30 10:42:30 -07003256 last = tline;
3257 tline = expand_smacro(tline->next);
3258 last->next = NULL;
3259
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003260 t = tline;
H. Peter Anvin418ca702008-05-30 10:42:30 -07003261 while (tok_type_(t, TOK_WHITESPACE))
3262 t = t->next;
3263
3264 if (!t || (t->type != TOK_STRING &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003265 t->type != TOK_INTERNAL_STRING)) {
H. Peter Anvin418ca702008-05-30 10:42:30 -07003266 error(ERR_NONFATAL, "`%%pathsearch' expects a file name");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003267 free_tlist(tline);
H. Peter Anvin418ca702008-05-30 10:42:30 -07003268 free_tlist(origline);
3269 return DIRECTIVE_FOUND; /* but we did _something_ */
3270 }
3271 if (t->next)
H. Peter Anvin917a3492008-09-24 09:14:49 -07003272 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvin418ca702008-05-30 10:42:30 -07003273 "trailing garbage after `%%pathsearch' ignored");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003274 p = t->text;
H. Peter Anvin427cc912008-06-01 21:43:03 -07003275 if (t->type != TOK_INTERNAL_STRING)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003276 nasm_unquote(p, NULL);
H. Peter Anvin418ca702008-05-30 10:42:30 -07003277
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003278 fp = inc_fopen(p, &xsl, &xst, true);
3279 if (fp) {
3280 p = xsl->str;
3281 fclose(fp); /* Don't actually care about the file */
3282 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003283 macro_start = nasm_malloc(sizeof(*macro_start));
3284 macro_start->next = NULL;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003285 macro_start->text = nasm_quote(p, strlen(p));
3286 macro_start->type = TOK_STRING;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003287 macro_start->a.mac = NULL;
3288 if (xsl)
3289 nasm_free(xsl);
H. Peter Anvin418ca702008-05-30 10:42:30 -07003290
3291 /*
3292 * We now have a macro name, an implicit parameter count of
3293 * zero, and a string token to use as an expansion. Create
3294 * and store an SMacro.
3295 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003296 define_smacro(ctx, mname, casesense, 0, macro_start);
3297 free_tlist(tline);
H. Peter Anvin418ca702008-05-30 10:42:30 -07003298 free_tlist(origline);
3299 return DIRECTIVE_FOUND;
3300 }
3301
H. Peter Anvine2c80182005-01-15 22:15:51 +00003302 case PP_STRLEN:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003303 casesense = true;
H. Peter Anvin70653092007-10-19 14:42:29 -07003304
H. Peter Anvine2c80182005-01-15 22:15:51 +00003305 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 "`%%strlen' expects a macro identifier as first parameter");
3313 free_tlist(origline);
3314 return DIRECTIVE_FOUND;
3315 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003316 ctx = get_ctx(tline->text, &mname, false);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003317 last = tline;
3318 tline = expand_smacro(tline->next);
3319 last->next = NULL;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003320
H. Peter Anvine2c80182005-01-15 22:15:51 +00003321 t = tline;
3322 while (tok_type_(t, TOK_WHITESPACE))
3323 t = t->next;
3324 /* t should now point to the string */
Cyrill Gorcunov4e1d5ab2010-07-23 18:51:51 +04003325 if (!tok_type_(t, TOK_STRING)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00003326 error(ERR_NONFATAL,
3327 "`%%strlen` requires string as second parameter");
3328 free_tlist(tline);
3329 free_tlist(origline);
3330 return DIRECTIVE_FOUND;
3331 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003332
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003333 macro_start = nasm_malloc(sizeof(*macro_start));
3334 macro_start->next = NULL;
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07003335 make_tok_num(macro_start, nasm_unquote(t->text, NULL));
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003336 macro_start->a.mac = NULL;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003337
H. Peter Anvine2c80182005-01-15 22:15:51 +00003338 /*
3339 * We now have a macro name, an implicit parameter count of
3340 * zero, and a numeric token to use as an expansion. Create
3341 * and store an SMacro.
3342 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003343 define_smacro(ctx, mname, casesense, 0, macro_start);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003344 free_tlist(tline);
3345 free_tlist(origline);
3346 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003347
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003348 case PP_STRCAT:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003349 casesense = true;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003350
3351 tline = tline->next;
3352 skip_white_(tline);
3353 tline = expand_id(tline);
3354 if (!tline || (tline->type != TOK_ID &&
3355 (tline->type != TOK_PREPROC_ID ||
3356 tline->text[1] != '$'))) {
3357 error(ERR_NONFATAL,
3358 "`%%strcat' expects a macro identifier as first parameter");
3359 free_tlist(origline);
3360 return DIRECTIVE_FOUND;
3361 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003362 ctx = get_ctx(tline->text, &mname, false);
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003363 last = tline;
3364 tline = expand_smacro(tline->next);
3365 last->next = NULL;
3366
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003367 len = 0;
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04003368 list_for_each(t, tline) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003369 switch (t->type) {
3370 case TOK_WHITESPACE:
3371 break;
3372 case TOK_STRING:
3373 len += t->a.len = nasm_unquote(t->text, NULL);
3374 break;
3375 case TOK_OTHER:
3376 if (!strcmp(t->text, ",")) /* permit comma separators */
3377 break;
3378 /* else fall through */
3379 default:
3380 error(ERR_NONFATAL,
3381 "non-string passed to `%%strcat' (%d)", t->type);
3382 free_tlist(tline);
3383 free_tlist(origline);
3384 return DIRECTIVE_FOUND;
3385 }
3386 }
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003387
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003388 p = pp = nasm_malloc(len);
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04003389 list_for_each(t, tline) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003390 if (t->type == TOK_STRING) {
3391 memcpy(p, t->text, t->a.len);
3392 p += t->a.len;
3393 }
3394 }
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003395
3396 /*
3397 * We now have a macro name, an implicit parameter count of
3398 * zero, and a numeric token to use as an expansion. Create
3399 * and store an SMacro.
3400 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003401 macro_start = new_Token(NULL, TOK_STRING, NULL, 0);
3402 macro_start->text = nasm_quote(pp, len);
3403 nasm_free(pp);
3404 define_smacro(ctx, mname, casesense, 0, macro_start);
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003405 free_tlist(tline);
3406 free_tlist(origline);
3407 return DIRECTIVE_FOUND;
3408
H. Peter Anvine2c80182005-01-15 22:15:51 +00003409 case PP_SUBSTR:
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003410 {
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003411 int64_t start, count;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003412 size_t len;
H. Peter Anvind2456592008-06-19 15:04:18 -07003413
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003414 casesense = true;
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07003415
H. Peter Anvine2c80182005-01-15 22:15:51 +00003416 tline = tline->next;
3417 skip_white_(tline);
3418 tline = expand_id(tline);
3419 if (!tline || (tline->type != TOK_ID &&
3420 (tline->type != TOK_PREPROC_ID ||
3421 tline->text[1] != '$'))) {
3422 error(ERR_NONFATAL,
3423 "`%%substr' expects a macro identifier as first parameter");
3424 free_tlist(origline);
3425 return DIRECTIVE_FOUND;
3426 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003427 ctx = get_ctx(tline->text, &mname, false);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003428 last = tline;
3429 tline = expand_smacro(tline->next);
3430 last->next = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003431
Cyrill Gorcunov35519d62010-09-06 23:49:52 +04003432 if (tline) /* skip expanded id */
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003433 t = tline->next;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003434 while (tok_type_(t, TOK_WHITESPACE))
3435 t = t->next;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003436
H. Peter Anvine2c80182005-01-15 22:15:51 +00003437 /* t should now point to the string */
Cyrill Gorcunov35519d62010-09-06 23:49:52 +04003438 if (!tok_type_(t, TOK_STRING)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00003439 error(ERR_NONFATAL,
3440 "`%%substr` requires string as second parameter");
3441 free_tlist(tline);
3442 free_tlist(origline);
3443 return DIRECTIVE_FOUND;
3444 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003445
H. Peter Anvine2c80182005-01-15 22:15:51 +00003446 tt = t->next;
3447 tptr = &tt;
3448 tokval.t_type = TOKEN_INVALID;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003449 evalresult = evaluate(ppscan, tptr, &tokval, NULL,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003450 pass, error, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003451 if (!evalresult) {
3452 free_tlist(tline);
3453 free_tlist(origline);
3454 return DIRECTIVE_FOUND;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003455 } else if (!is_simple(evalresult)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00003456 error(ERR_NONFATAL, "non-constant value given to `%%substr`");
3457 free_tlist(tline);
3458 free_tlist(origline);
3459 return DIRECTIVE_FOUND;
3460 }
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003461 start = evalresult->value - 1;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003462
3463 while (tok_type_(tt, TOK_WHITESPACE))
3464 tt = tt->next;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003465 if (!tt) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003466 count = 1; /* Backwards compatibility: one character */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003467 } else {
3468 tokval.t_type = TOKEN_INVALID;
3469 evalresult = evaluate(ppscan, tptr, &tokval, NULL,
3470 pass, error, NULL);
3471 if (!evalresult) {
3472 free_tlist(tline);
3473 free_tlist(origline);
3474 return DIRECTIVE_FOUND;
3475 } else if (!is_simple(evalresult)) {
3476 error(ERR_NONFATAL, "non-constant value given to `%%substr`");
3477 free_tlist(tline);
3478 free_tlist(origline);
3479 return DIRECTIVE_FOUND;
3480 }
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003481 count = evalresult->value;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003482 }
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003483
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003484 len = nasm_unquote(t->text, NULL);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003485
Cyrill Gorcunovcff031e2010-09-07 20:31:11 +04003486 /* make start and count being in range */
3487 if (start < 0)
3488 start = 0;
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003489 if (count < 0)
3490 count = len + count + 1 - start;
3491 if (start + count > (int64_t)len)
Cyrill Gorcunovcff031e2010-09-07 20:31:11 +04003492 count = len - start;
3493 if (!len || count < 0 || start >=(int64_t)len)
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003494 start = -1, count = 0; /* empty string */
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003495
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003496 macro_start = nasm_malloc(sizeof(*macro_start));
3497 macro_start->next = NULL;
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003498 macro_start->text = nasm_quote((start < 0) ? "" : t->text + start, count);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003499 macro_start->type = TOK_STRING;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003500 macro_start->a.mac = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003501
H. Peter Anvine2c80182005-01-15 22:15:51 +00003502 /*
3503 * We now have a macro name, an implicit parameter count of
3504 * zero, and a numeric token to use as an expansion. Create
3505 * and store an SMacro.
3506 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003507 define_smacro(ctx, mname, casesense, 0, macro_start);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003508 free_tlist(tline);
3509 free_tlist(origline);
3510 return DIRECTIVE_FOUND;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003511 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003512
H. Peter Anvine2c80182005-01-15 22:15:51 +00003513 case PP_ASSIGN:
3514 case PP_IASSIGN:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003515 casesense = (i == PP_ASSIGN);
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07003516
H. Peter Anvine2c80182005-01-15 22:15:51 +00003517 tline = tline->next;
3518 skip_white_(tline);
3519 tline = expand_id(tline);
3520 if (!tline || (tline->type != TOK_ID &&
3521 (tline->type != TOK_PREPROC_ID ||
3522 tline->text[1] != '$'))) {
3523 error(ERR_NONFATAL,
3524 "`%%%sassign' expects a macro identifier",
3525 (i == PP_IASSIGN ? "i" : ""));
3526 free_tlist(origline);
3527 return DIRECTIVE_FOUND;
3528 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003529 ctx = get_ctx(tline->text, &mname, false);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003530 last = tline;
3531 tline = expand_smacro(tline->next);
3532 last->next = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003533
H. Peter Anvine2c80182005-01-15 22:15:51 +00003534 t = tline;
3535 tptr = &t;
3536 tokval.t_type = TOKEN_INVALID;
3537 evalresult =
3538 evaluate(ppscan, tptr, &tokval, NULL, pass, error, NULL);
3539 free_tlist(tline);
3540 if (!evalresult) {
3541 free_tlist(origline);
3542 return DIRECTIVE_FOUND;
3543 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003544
H. Peter Anvine2c80182005-01-15 22:15:51 +00003545 if (tokval.t_type)
H. Peter Anvin917a3492008-09-24 09:14:49 -07003546 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +00003547 "trailing garbage after expression ignored");
H. Peter Anvin734b1882002-04-30 21:01:08 +00003548
H. Peter Anvine2c80182005-01-15 22:15:51 +00003549 if (!is_simple(evalresult)) {
3550 error(ERR_NONFATAL,
3551 "non-constant value given to `%%%sassign'",
3552 (i == PP_IASSIGN ? "i" : ""));
3553 free_tlist(origline);
3554 return DIRECTIVE_FOUND;
3555 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003556
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003557 macro_start = nasm_malloc(sizeof(*macro_start));
3558 macro_start->next = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003559 make_tok_num(macro_start, reloc_value(evalresult));
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003560 macro_start->a.mac = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003561
H. Peter Anvine2c80182005-01-15 22:15:51 +00003562 /*
3563 * We now have a macro name, an implicit parameter count of
3564 * zero, and a numeric token to use as an expansion. Create
3565 * and store an SMacro.
3566 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003567 define_smacro(ctx, mname, casesense, 0, macro_start);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003568 free_tlist(origline);
3569 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003570
H. Peter Anvine2c80182005-01-15 22:15:51 +00003571 case PP_LINE:
3572 /*
3573 * Syntax is `%line nnn[+mmm] [filename]'
3574 */
3575 tline = tline->next;
3576 skip_white_(tline);
3577 if (!tok_type_(tline, TOK_NUMBER)) {
3578 error(ERR_NONFATAL, "`%%line' expects line number");
3579 free_tlist(origline);
3580 return DIRECTIVE_FOUND;
3581 }
H. Peter Anvin70055962007-10-11 00:05:31 -07003582 k = readnum(tline->text, &err);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003583 m = 1;
3584 tline = tline->next;
3585 if (tok_is_(tline, "+")) {
3586 tline = tline->next;
3587 if (!tok_type_(tline, TOK_NUMBER)) {
3588 error(ERR_NONFATAL, "`%%line' expects line increment");
3589 free_tlist(origline);
3590 return DIRECTIVE_FOUND;
3591 }
H. Peter Anvin70055962007-10-11 00:05:31 -07003592 m = readnum(tline->text, &err);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003593 tline = tline->next;
3594 }
3595 skip_white_(tline);
3596 src_set_linnum(k);
3597 istk->lineinc = m;
3598 if (tline) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07003599 nasm_free(src_set_fname(detoken(tline, false)));
H. Peter Anvine2c80182005-01-15 22:15:51 +00003600 }
3601 free_tlist(origline);
3602 return DIRECTIVE_FOUND;
Keith Kaniosb307a4f2010-11-06 17:41:51 -05003603
H. Peter Anvine2c80182005-01-15 22:15:51 +00003604 default:
3605 error(ERR_FATAL,
3606 "preprocessor directive `%s' not yet implemented",
H. Peter Anvin4169a472007-09-12 01:29:43 +00003607 pp_directives[i]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003608 return DIRECTIVE_FOUND;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003609 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003610}
3611
3612/*
H. Peter Anvin76690a12002-04-30 20:52:49 +00003613 * Ensure that a macro parameter contains a condition code and
3614 * nothing else. Return the condition code index if so, or -1
3615 * otherwise.
3616 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003617static int find_cc(Token * t)
H. Peter Anvineba20a72002-04-30 20:53:55 +00003618{
H. Peter Anvin76690a12002-04-30 20:52:49 +00003619 Token *tt;
3620 int i, j, k, m;
3621
H. Peter Anvin25a99342007-09-22 17:45:45 -07003622 if (!t)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003623 return -1; /* Probably a %+ without a space */
H. Peter Anvin25a99342007-09-22 17:45:45 -07003624
H. Peter Anvineba20a72002-04-30 20:53:55 +00003625 skip_white_(t);
H. Peter Anvin76690a12002-04-30 20:52:49 +00003626 if (t->type != TOK_ID)
H. Peter Anvine2c80182005-01-15 22:15:51 +00003627 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003628 tt = t->next;
H. Peter Anvineba20a72002-04-30 20:53:55 +00003629 skip_white_(tt);
H. Peter Anvin76690a12002-04-30 20:52:49 +00003630 if (tt && (tt->type != TOK_OTHER || strcmp(tt->text, ",")))
H. Peter Anvine2c80182005-01-15 22:15:51 +00003631 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003632
3633 i = -1;
Cyrill Gorcunova7319242010-06-03 22:04:36 +04003634 j = ARRAY_SIZE(conditions);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003635 while (j - i > 1) {
3636 k = (j + i) / 2;
3637 m = nasm_stricmp(t->text, conditions[k]);
3638 if (m == 0) {
3639 i = k;
3640 j = -2;
3641 break;
3642 } else if (m < 0) {
3643 j = k;
3644 } else
3645 i = k;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003646 }
3647 if (j != -2)
H. Peter Anvine2c80182005-01-15 22:15:51 +00003648 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003649 return i;
3650}
3651
Cyrill Gorcunov575d4282010-10-06 00:25:55 +04003652static bool paste_tokens(Token **head, const struct tokseq_match *m,
3653 int mnum, bool handle_paste_tokens)
H. Peter Anvind784a082009-04-20 14:01:18 -07003654{
3655 Token **tail, *t, *tt;
3656 Token **paste_head;
3657 bool did_paste = false;
3658 char *tmp;
Cyrill Gorcunov575d4282010-10-06 00:25:55 +04003659 int i;
H. Peter Anvind784a082009-04-20 14:01:18 -07003660
3661 /* Now handle token pasting... */
3662 paste_head = NULL;
3663 tail = head;
3664 while ((t = *tail) && (tt = t->next)) {
3665 switch (t->type) {
3666 case TOK_WHITESPACE:
3667 if (tt->type == TOK_WHITESPACE) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003668 /* Zap adjacent whitespace tokens */
H. Peter Anvind784a082009-04-20 14:01:18 -07003669 t->next = delete_Token(tt);
3670 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003671 /* Do not advance paste_head here */
3672 tail = &t->next;
3673 }
H. Peter Anvind784a082009-04-20 14:01:18 -07003674 break;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003675 case TOK_PASTE: /* %+ */
3676 if (handle_paste_tokens) {
3677 /* Zap %+ and whitespace tokens to the right */
3678 while (t && (t->type == TOK_WHITESPACE ||
3679 t->type == TOK_PASTE))
3680 t = *tail = delete_Token(t);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003681 if (!paste_head || !t)
3682 break; /* Nothing to paste with */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003683 tail = paste_head;
3684 t = *tail;
3685 tt = t->next;
3686 while (tok_type_(tt, TOK_WHITESPACE))
3687 tt = t->next = delete_Token(tt);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003688 if (tt) {
3689 tmp = nasm_strcat(t->text, tt->text);
3690 delete_Token(t);
3691 tt = delete_Token(tt);
3692 t = *tail = tokenize(tmp);
3693 nasm_free(tmp);
3694 while (t->next) {
3695 tail = &t->next;
3696 t = t->next;
3697 }
3698 t->next = tt; /* Attach the remaining token chain */
3699 did_paste = true;
3700 }
3701 paste_head = tail;
3702 tail = &t->next;
3703 break;
3704 }
3705 /* else fall through */
3706 default:
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +04003707 /*
3708 * Concatenation of tokens might look nontrivial
3709 * but in real it's pretty simple -- the caller
Cyrill Gorcunov575d4282010-10-06 00:25:55 +04003710 * prepares the masks of token types to be concatenated
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +04003711 * and we simply find matched sequences and slip
3712 * them together
3713 */
Cyrill Gorcunov575d4282010-10-06 00:25:55 +04003714 for (i = 0; i < mnum; i++) {
3715 if (PP_CONCAT_MASK(t->type) & m[i].mask_head) {
3716 size_t len = 0;
3717 char *tmp, *p;
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +04003718
Cyrill Gorcunov575d4282010-10-06 00:25:55 +04003719 while (tt && (PP_CONCAT_MASK(tt->type) & m[i].mask_tail)) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003720 len += strlen(tt->text);
3721 tt = tt->next;
Cyrill Gorcunov575d4282010-10-06 00:25:55 +04003722 }
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +04003723
Cyrill Gorcunov575d4282010-10-06 00:25:55 +04003724 /*
3725 * Now tt points to the first token after
3726 * the potential paste area...
3727 */
3728 if (tt != t->next) {
3729 /* We have at least two tokens... */
3730 len += strlen(t->text);
3731 p = tmp = nasm_malloc(len+1);
3732 while (t != tt) {
3733 strcpy(p, t->text);
3734 p = strchr(p, '\0');
3735 t = delete_Token(t);
3736 }
3737 t = *tail = tokenize(tmp);
3738 nasm_free(tmp);
3739 while (t->next) {
3740 tail = &t->next;
3741 t = t->next;
3742 }
3743 t->next = tt; /* Attach the remaining token chain */
3744 did_paste = true;
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +04003745 }
Cyrill Gorcunov575d4282010-10-06 00:25:55 +04003746 paste_head = tail;
3747 tail = &t->next;
3748 break;
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +04003749 }
Cyrill Gorcunov575d4282010-10-06 00:25:55 +04003750 }
3751 if (i >= mnum) { /* no match */
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +04003752 tail = &t->next;
3753 if (!tok_type_(t->next, TOK_WHITESPACE))
3754 paste_head = tail;
3755 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003756 break;
H. Peter Anvind784a082009-04-20 14:01:18 -07003757 }
3758 }
3759 return did_paste;
3760}
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003761
3762/*
3763 * expands to a list of tokens from %{x:y}
3764 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003765static Token *expand_mmac_params_range(MMacro *mac, Token *tline, Token ***last)
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003766{
3767 Token *t = tline, **tt, *tm, *head;
3768 char *pos;
3769 int fst, lst, j, i;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003770
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003771 pos = strchr(tline->text, ':');
3772 nasm_assert(pos);
3773
3774 lst = atoi(pos + 1);
3775 fst = atoi(tline->text + 1);
3776
3777 /*
3778 * only macros params are accounted so
3779 * if someone passes %0 -- we reject such
3780 * value(s)
3781 */
3782 if (lst == 0 || fst == 0)
3783 goto err;
3784
3785 /* the values should be sane */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003786 if ((fst > (int)mac->nparam || fst < (-(int)mac->nparam)) ||
3787 (lst > (int)mac->nparam || lst < (-(int)mac->nparam)))
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003788 goto err;
3789
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003790 fst = fst < 0 ? fst + (int)mac->nparam + 1: fst;
3791 lst = lst < 0 ? lst + (int)mac->nparam + 1: lst;
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003792
3793 /* counted from zero */
3794 fst--, lst--;
3795
3796 /*
3797 * it will be at least one token
3798 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003799 tm = mac->params[(fst + mac->rotate) % mac->nparam];
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003800 t = new_Token(NULL, tm->type, tm->text, 0);
3801 head = t, tt = &t->next;
3802 if (fst < lst) {
3803 for (i = fst + 1; i <= lst; i++) {
3804 t = new_Token(NULL, TOK_OTHER, ",", 0);
3805 *tt = t, tt = &t->next;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003806 j = (i + mac->rotate) % mac->nparam;
3807 tm = mac->params[j];
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003808 t = new_Token(NULL, tm->type, tm->text, 0);
3809 *tt = t, tt = &t->next;
3810 }
3811 } else {
3812 for (i = fst - 1; i >= lst; i--) {
3813 t = new_Token(NULL, TOK_OTHER, ",", 0);
3814 *tt = t, tt = &t->next;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003815 j = (i + mac->rotate) % mac->nparam;
3816 tm = mac->params[j];
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003817 t = new_Token(NULL, tm->type, tm->text, 0);
3818 *tt = t, tt = &t->next;
3819 }
3820 }
3821
3822 *last = tt;
3823 return head;
3824
3825err:
3826 error(ERR_NONFATAL, "`%%{%s}': macro parameters out of range",
3827 &tline->text[1]);
3828 return tline;
3829}
3830
H. Peter Anvin76690a12002-04-30 20:52:49 +00003831/*
3832 * Expand MMacro-local things: parameter references (%0, %n, %+n,
H. Peter Anvin67c63722008-10-26 23:49:00 -07003833 * %-n) and MMacro-local identifiers (%%foo) as well as
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003834 * macro indirection (%[...]) and range (%{..:..}).
H. Peter Anvin76690a12002-04-30 20:52:49 +00003835 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003836static Token *expand_mmac_params(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00003837{
H. Peter Anvin734b1882002-04-30 21:01:08 +00003838 Token *t, *tt, **tail, *thead;
H. Peter Anvin6125b622009-04-08 14:02:25 -07003839 bool changed = false;
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003840 char *pos;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003841
3842 tail = &thead;
3843 thead = NULL;
3844
H. Peter Anvine2c80182005-01-15 22:15:51 +00003845 while (tline) {
3846 if (tline->type == TOK_PREPROC_ID &&
Cyrill Gorcunovca611192010-06-04 09:22:12 +04003847 (((tline->text[1] == '+' || tline->text[1] == '-') && tline->text[2]) ||
3848 (tline->text[1] >= '0' && tline->text[1] <= '9') ||
3849 tline->text[1] == '%')) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00003850 char *text = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003851 int type = 0, cc; /* type = 0 to placate optimisers */
Keith Kaniosa6dfa782007-04-13 16:47:53 +00003852 char tmpbuf[30];
H. Peter Anvin25a99342007-09-22 17:45:45 -07003853 unsigned int n;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003854 int i;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003855 MMacro *mac;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003856
H. Peter Anvine2c80182005-01-15 22:15:51 +00003857 t = tline;
3858 tline = tline->next;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003859
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003860 mac = istk->mstk;
3861 while (mac && !mac->name) /* avoid mistaking %reps for macros */
3862 mac = mac->next_active;
3863 if (!mac) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00003864 error(ERR_NONFATAL, "`%s': not in a macro call", t->text);
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003865 } else {
3866 pos = strchr(t->text, ':');
3867 if (!pos) {
3868 switch (t->text[1]) {
3869 /*
3870 * We have to make a substitution of one of the
3871 * forms %1, %-1, %+1, %%foo, %0.
3872 */
3873 case '0':
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003874 type = TOK_NUMBER;
3875 snprintf(tmpbuf, sizeof(tmpbuf), "%d", mac->nparam);
3876 text = nasm_strdup(tmpbuf);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003877 break;
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003878 case '%':
H. Peter Anvine2c80182005-01-15 22:15:51 +00003879 type = TOK_ID;
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003880 snprintf(tmpbuf, sizeof(tmpbuf), "..@%"PRIu64".",
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003881 mac->unique);
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003882 text = nasm_strcat(tmpbuf, t->text + 2);
3883 break;
3884 case '-':
3885 n = atoi(t->text + 2) - 1;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003886 if (n >= mac->nparam)
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003887 tt = NULL;
3888 else {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003889 if (mac->nparam > 1)
3890 n = (n + mac->rotate) % mac->nparam;
3891 tt = mac->params[n];
H. Peter Anvine2c80182005-01-15 22:15:51 +00003892 }
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003893 cc = find_cc(tt);
3894 if (cc == -1) {
3895 error(ERR_NONFATAL,
3896 "macro parameter %d is not a condition code",
3897 n + 1);
3898 text = NULL;
3899 } else {
3900 type = TOK_ID;
3901 if (inverse_ccs[cc] == -1) {
3902 error(ERR_NONFATAL,
3903 "condition code `%s' is not invertible",
3904 conditions[cc]);
3905 text = NULL;
3906 } else
3907 text = nasm_strdup(conditions[inverse_ccs[cc]]);
3908 }
3909 break;
3910 case '+':
3911 n = atoi(t->text + 2) - 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 cc = find_cc(tt);
3920 if (cc == -1) {
3921 error(ERR_NONFATAL,
3922 "macro parameter %d is not a condition code",
3923 n + 1);
3924 text = NULL;
3925 } else {
3926 type = TOK_ID;
3927 text = nasm_strdup(conditions[cc]);
3928 }
3929 break;
3930 default:
3931 n = atoi(t->text + 1) - 1;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003932 if (n >= mac->nparam)
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003933 tt = NULL;
3934 else {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003935 if (mac->nparam > 1)
3936 n = (n + mac->rotate) % mac->nparam;
3937 tt = mac->params[n];
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003938 }
3939 if (tt) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003940 for (i = 0; i < mac->paramlen[n]; i++) {
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003941 *tail = new_Token(NULL, tt->type, tt->text, 0);
3942 tail = &(*tail)->next;
3943 tt = tt->next;
3944 }
3945 }
3946 text = NULL; /* we've done it here */
3947 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003948 }
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003949 } else {
3950 /*
3951 * seems we have a parameters range here
3952 */
3953 Token *head, **last;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003954 head = expand_mmac_params_range(mac, t, &last);
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003955 if (head != t) {
3956 *tail = head;
3957 *last = tline;
3958 tline = head;
3959 text = NULL;
3960 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00003961 }
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003962 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00003963 if (!text) {
3964 delete_Token(t);
3965 } else {
3966 *tail = t;
3967 tail = &t->next;
3968 t->type = type;
3969 nasm_free(t->text);
3970 t->text = text;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003971 t->a.mac = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003972 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003973 changed = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003974 continue;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003975 } else if (tline->type == TOK_INDIRECT) {
3976 t = tline;
3977 tline = tline->next;
3978 tt = tokenize(t->text);
3979 tt = expand_mmac_params(tt);
3980 tt = expand_smacro(tt);
3981 *tail = tt;
3982 while (tt) {
3983 tt->a.mac = NULL; /* Necessary? */
3984 tail = &tt->next;
3985 tt = tt->next;
3986 }
3987 delete_Token(t);
3988 changed = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003989 } else {
3990 t = *tail = tline;
3991 tline = tline->next;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003992 t->a.mac = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003993 tail = &t->next;
3994 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00003995 }
H. Peter Anvineba20a72002-04-30 20:53:55 +00003996 *tail = NULL;
H. Peter Anvin67c63722008-10-26 23:49:00 -07003997
Cyrill Gorcunovc6a742c2011-06-27 01:23:09 +04003998 if (changed) {
3999 const struct tokseq_match t[] = {
4000 {
4001 PP_CONCAT_MASK(TOK_ID) |
4002 PP_CONCAT_MASK(TOK_FLOAT), /* head */
4003 PP_CONCAT_MASK(TOK_ID) |
4004 PP_CONCAT_MASK(TOK_NUMBER) |
4005 PP_CONCAT_MASK(TOK_FLOAT) |
4006 PP_CONCAT_MASK(TOK_OTHER) /* tail */
4007 },
4008 {
4009 PP_CONCAT_MASK(TOK_NUMBER), /* head */
4010 PP_CONCAT_MASK(TOK_NUMBER) /* tail */
4011 }
4012 };
4013 paste_tokens(&thead, t, ARRAY_SIZE(t), false);
4014 }
H. Peter Anvin6125b622009-04-08 14:02:25 -07004015
H. Peter Anvin76690a12002-04-30 20:52:49 +00004016 return thead;
4017}
4018
4019/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004020 * Expand all single-line macro calls made in the given line.
4021 * Return the expanded version of the line. The original is deemed
4022 * to be destroyed in the process. (In reality we'll just move
4023 * Tokens from input to output a lot of the time, rather than
4024 * actually bothering to destroy and replicate.)
4025 */
H. Peter Anvincb1cf592007-11-19 12:26:50 -08004026
H. Peter Anvine2c80182005-01-15 22:15:51 +00004027static Token *expand_smacro(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004028{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004029 Token *t, *tt, *mstart, **tail, *thead;
H. Peter Anvineba20a72002-04-30 20:53:55 +00004030 SMacro *head = NULL, *m;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004031 Token **params;
4032 int *paramsize;
H. Peter Anvin25a99342007-09-22 17:45:45 -07004033 unsigned int nparam, sparam;
H. Peter Anvind784a082009-04-20 14:01:18 -07004034 int brackets;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004035 Token *org_tline = tline;
4036 Context *ctx;
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08004037 const char *mname;
H. Peter Anvin2a15e692007-11-19 13:14:59 -08004038 int deadman = DEADMAN_LIMIT;
H. Peter Anvin8287daf2009-07-07 16:00:58 -07004039 bool expanded;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004040
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004041 /*
4042 * Trick: we should avoid changing the start token pointer since it can
4043 * be contained in "next" field of other token. Because of this
4044 * we allocate a copy of first token and work with it; at the end of
4045 * routine we copy it back
4046 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004047 if (org_tline) {
Cyrill Gorcunoved4a8052010-04-09 15:40:35 +04004048 tline = new_Token(org_tline->next, org_tline->type,
4049 org_tline->text, 0);
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004050 tline->a.mac = org_tline->a.mac;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004051 nasm_free(org_tline->text);
4052 org_tline->text = NULL;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004053 }
4054
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004055 expanded = true; /* Always expand %+ at least once */
H. Peter Anvin8287daf2009-07-07 16:00:58 -07004056
H. Peter Anvincb1cf592007-11-19 12:26:50 -08004057again:
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004058 thead = NULL;
Cyrill Gorcunoved4a8052010-04-09 15:40:35 +04004059 tail = &thead;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004060
H. Peter Anvine2c80182005-01-15 22:15:51 +00004061 while (tline) { /* main token loop */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004062 if (!--deadman) {
4063 error(ERR_NONFATAL, "interminable macro recursion");
Cyrill Gorcunovbd38c8f2009-11-21 11:11:23 +03004064 goto err;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004065 }
H. Peter Anvincb1cf592007-11-19 12:26:50 -08004066
H. Peter Anvine2c80182005-01-15 22:15:51 +00004067 if ((mname = tline->text)) {
4068 /* if this token is a local macro, look in local context */
Cyrill Gorcunovc56d9ad2010-02-11 15:12:19 +03004069 if (tline->type == TOK_ID) {
4070 head = (SMacro *)hash_findix(&smacros, mname);
4071 } else if (tline->type == TOK_PREPROC_ID) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004072 ctx = get_ctx(mname, &mname, true);
Cyrill Gorcunovc56d9ad2010-02-11 15:12:19 +03004073 head = ctx ? (SMacro *)hash_findix(&ctx->localmac, mname) : NULL;
4074 } else
4075 head = NULL;
H. Peter Anvin072771e2008-05-22 13:17:51 -07004076
H. Peter Anvine2c80182005-01-15 22:15:51 +00004077 /*
4078 * We've hit an identifier. As in is_mmacro below, we first
4079 * check whether the identifier is a single-line macro at
4080 * all, then think about checking for parameters if
4081 * necessary.
4082 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004083 list_for_each(m, head)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004084 if (!mstrcmp(m->name, mname, m->casesense))
4085 break;
4086 if (m) {
4087 mstart = tline;
4088 params = NULL;
4089 paramsize = NULL;
4090 if (m->nparam == 0) {
4091 /*
4092 * Simple case: the macro is parameterless. Discard the
4093 * one token that the macro call took, and push the
4094 * expansion back on the to-do stack.
4095 */
4096 if (!m->expansion) {
4097 if (!strcmp("__FILE__", m->name)) {
4098 int32_t num = 0;
4099 char *file = NULL;
4100 src_get(&num, &file);
4101 tline->text = nasm_quote(file, strlen(file));
4102 tline->type = TOK_STRING;
4103 nasm_free(file);
H. Peter Anvine2c80182005-01-15 22:15:51 +00004104 continue;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004105 }
4106 if (!strcmp("__LINE__", m->name)) {
4107 nasm_free(tline->text);
4108 make_tok_num(tline, src_get_linnum());
4109 continue;
4110 }
4111 if (!strcmp("__BITS__", m->name)) {
4112 nasm_free(tline->text);
4113 make_tok_num(tline, globalbits);
4114 continue;
4115 }
4116 tline = delete_Token(tline);
4117 continue;
4118 }
4119 } else {
4120 /*
4121 * Complicated case: at least one macro with this name
H. Peter Anvine2c80182005-01-15 22:15:51 +00004122 * exists and takes parameters. We must find the
4123 * parameters in the call, count them, find the SMacro
4124 * that corresponds to that form of the macro call, and
4125 * substitute for the parameters when we expand. What a
4126 * pain.
4127 */
4128 /*tline = tline->next;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004129 skip_white_(tline); */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004130 do {
4131 t = tline->next;
4132 while (tok_type_(t, TOK_SMAC_END)) {
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004133 t->a.mac->in_progress = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004134 t->text = NULL;
4135 t = tline->next = delete_Token(t);
4136 }
4137 tline = t;
4138 } while (tok_type_(tline, TOK_WHITESPACE));
4139 if (!tok_is_(tline, "(")) {
4140 /*
4141 * This macro wasn't called with parameters: ignore
4142 * the call. (Behaviour borrowed from gnu cpp.)
4143 */
4144 tline = mstart;
4145 m = NULL;
4146 } else {
4147 int paren = 0;
4148 int white = 0;
4149 brackets = 0;
4150 nparam = 0;
4151 sparam = PARAM_DELTA;
4152 params = nasm_malloc(sparam * sizeof(Token *));
4153 params[0] = tline->next;
4154 paramsize = nasm_malloc(sparam * sizeof(int));
4155 paramsize[0] = 0;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07004156 while (true) { /* parameter loop */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004157 /*
4158 * For some unusual expansions
4159 * which concatenates function call
4160 */
4161 t = tline->next;
4162 while (tok_type_(t, TOK_SMAC_END)) {
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004163 t->a.mac->in_progress = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004164 t->text = NULL;
4165 t = tline->next = delete_Token(t);
4166 }
4167 tline = t;
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00004168
H. Peter Anvine2c80182005-01-15 22:15:51 +00004169 if (!tline) {
4170 error(ERR_NONFATAL,
4171 "macro call expects terminating `)'");
4172 break;
4173 }
4174 if (tline->type == TOK_WHITESPACE
4175 && brackets <= 0) {
4176 if (paramsize[nparam])
4177 white++;
4178 else
4179 params[nparam] = tline->next;
4180 continue; /* parameter loop */
4181 }
4182 if (tline->type == TOK_OTHER
4183 && tline->text[1] == 0) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004184 char ch = tline->text[0];
H. Peter Anvine2c80182005-01-15 22:15:51 +00004185 if (ch == ',' && !paren && brackets <= 0) {
4186 if (++nparam >= sparam) {
4187 sparam += PARAM_DELTA;
4188 params = nasm_realloc(params,
Cyrill Gorcunoved4a8052010-04-09 15:40:35 +04004189 sparam * sizeof(Token *));
4190 paramsize = nasm_realloc(paramsize,
4191 sparam * sizeof(int));
H. Peter Anvine2c80182005-01-15 22:15:51 +00004192 }
4193 params[nparam] = tline->next;
4194 paramsize[nparam] = 0;
4195 white = 0;
4196 continue; /* parameter loop */
4197 }
4198 if (ch == '{' &&
4199 (brackets > 0 || (brackets == 0 &&
4200 !paramsize[nparam])))
4201 {
4202 if (!(brackets++)) {
4203 params[nparam] = tline->next;
4204 continue; /* parameter loop */
4205 }
4206 }
4207 if (ch == '}' && brackets > 0)
4208 if (--brackets == 0) {
4209 brackets = -1;
4210 continue; /* parameter loop */
4211 }
4212 if (ch == '(' && !brackets)
4213 paren++;
4214 if (ch == ')' && brackets <= 0)
4215 if (--paren < 0)
4216 break;
4217 }
4218 if (brackets < 0) {
4219 brackets = 0;
4220 error(ERR_NONFATAL, "braces do not "
4221 "enclose all of macro parameter");
4222 }
4223 paramsize[nparam] += white + 1;
4224 white = 0;
4225 } /* parameter loop */
4226 nparam++;
4227 while (m && (m->nparam != nparam ||
4228 mstrcmp(m->name, mname,
4229 m->casesense)))
4230 m = m->next;
4231 if (!m)
H. Peter Anvin917a3492008-09-24 09:14:49 -07004232 error(ERR_WARNING|ERR_PASS1|ERR_WARN_MNP,
H. Peter Anvine2c80182005-01-15 22:15:51 +00004233 "macro `%s' exists, "
4234 "but not taking %d parameters",
4235 mstart->text, nparam);
4236 }
4237 }
4238 if (m && m->in_progress)
4239 m = NULL;
4240 if (!m) { /* in progess or didn't find '(' or wrong nparam */
H. Peter Anvin70653092007-10-19 14:42:29 -07004241 /*
H. Peter Anvine2c80182005-01-15 22:15:51 +00004242 * Design question: should we handle !tline, which
4243 * indicates missing ')' here, or expand those
4244 * macros anyway, which requires the (t) test a few
H. Peter Anvin70653092007-10-19 14:42:29 -07004245 * lines down?
H. Peter Anvine2c80182005-01-15 22:15:51 +00004246 */
4247 nasm_free(params);
4248 nasm_free(paramsize);
4249 tline = mstart;
4250 } else {
4251 /*
4252 * Expand the macro: we are placed on the last token of the
4253 * call, so that we can easily split the call from the
4254 * following tokens. We also start by pushing an SMAC_END
4255 * token for the cycle removal.
4256 */
4257 t = tline;
4258 if (t) {
4259 tline = t->next;
4260 t->next = NULL;
4261 }
4262 tt = new_Token(tline, TOK_SMAC_END, NULL, 0);
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004263 tt->a.mac = m;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07004264 m->in_progress = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004265 tline = tt;
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04004266 list_for_each(t, m->expansion) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004267 if (t->type >= TOK_SMAC_PARAM) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00004268 Token *pcopy = tline, **ptail = &pcopy;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004269 Token *ttt, *pt;
4270 int i;
H. Peter Anvin734b1882002-04-30 21:01:08 +00004271
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004272 ttt = params[t->type - TOK_SMAC_PARAM];
4273 i = paramsize[t->type - TOK_SMAC_PARAM];
4274 while (--i >= 0) {
4275 pt = *ptail = new_Token(tline, ttt->type,
4276 ttt->text, 0);
4277 ptail = &pt->next;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004278 ttt = ttt->next;
4279 }
4280 tline = pcopy;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004281 } else if (t->type == TOK_PREPROC_Q) {
4282 tt = new_Token(tline, TOK_ID, mname, 0);
4283 tline = tt;
4284 } else if (t->type == TOK_PREPROC_QQ) {
4285 tt = new_Token(tline, TOK_ID, m->name, 0);
4286 tline = tt;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004287 } else {
4288 tt = new_Token(tline, t->type, t->text, 0);
4289 tline = tt;
4290 }
4291 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00004292
H. Peter Anvine2c80182005-01-15 22:15:51 +00004293 /*
4294 * Having done that, get rid of the macro call, and clean
4295 * up the parameters.
4296 */
4297 nasm_free(params);
4298 nasm_free(paramsize);
4299 free_tlist(mstart);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004300 expanded = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004301 continue; /* main token loop */
4302 }
4303 }
4304 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004305
H. Peter Anvine2c80182005-01-15 22:15:51 +00004306 if (tline->type == TOK_SMAC_END) {
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004307 tline->a.mac->in_progress = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004308 tline = delete_Token(tline);
4309 } else {
4310 t = *tail = tline;
4311 tline = tline->next;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004312 t->a.mac = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004313 t->next = NULL;
4314 tail = &t->next;
4315 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004316 }
4317
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004318 /*
4319 * Now scan the entire line and look for successive TOK_IDs that resulted
Keith Kaniosb7a89542007-04-12 02:40:54 +00004320 * after expansion (they can't be produced by tokenize()). The successive
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004321 * TOK_IDs should be concatenated.
4322 * Also we look for %+ tokens and concatenate the tokens before and after
4323 * them (without white spaces in between).
4324 */
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +04004325 if (expanded) {
Cyrill Gorcunovc6a742c2011-06-27 01:23:09 +04004326 const struct tokseq_match t[] = {
4327 {
4328 PP_CONCAT_MASK(TOK_ID) |
4329 PP_CONCAT_MASK(TOK_PREPROC_ID), /* head */
4330 PP_CONCAT_MASK(TOK_ID) |
4331 PP_CONCAT_MASK(TOK_PREPROC_ID) |
4332 PP_CONCAT_MASK(TOK_NUMBER) /* tail */
4333 }
4334 };
4335 if (paste_tokens(&thead, t, ARRAY_SIZE(t), true)) {
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +04004336 /*
4337 * If we concatenated something, *and* we had previously expanded
4338 * an actual macro, scan the lines again for macros...
4339 */
4340 tline = thead;
4341 expanded = false;
4342 goto again;
4343 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00004344 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004345
Cyrill Gorcunovbd38c8f2009-11-21 11:11:23 +03004346err:
H. Peter Anvine2c80182005-01-15 22:15:51 +00004347 if (org_tline) {
4348 if (thead) {
4349 *org_tline = *thead;
4350 /* since we just gave text to org_line, don't free it */
4351 thead->text = NULL;
4352 delete_Token(thead);
4353 } else {
4354 /* the expression expanded to empty line;
4355 we can't return NULL for some reasons
4356 we just set the line to a single WHITESPACE token. */
4357 memset(org_tline, 0, sizeof(*org_tline));
4358 org_tline->text = NULL;
4359 org_tline->type = TOK_WHITESPACE;
4360 }
4361 thead = org_tline;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004362 }
4363
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004364 return thead;
4365}
4366
4367/*
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004368 * Similar to expand_smacro but used exclusively with macro identifiers
4369 * right before they are fetched in. The reason is that there can be
4370 * identifiers consisting of several subparts. We consider that if there
4371 * are more than one element forming the name, user wants a expansion,
4372 * otherwise it will be left as-is. Example:
4373 *
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004374 * %define %$abc cde
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004375 *
4376 * the identifier %$abc will be left as-is so that the handler for %define
4377 * will suck it and define the corresponding value. Other case:
4378 *
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004379 * %define _%$abc cde
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004380 *
4381 * In this case user wants name to be expanded *before* %define starts
4382 * working, so we'll expand %$abc into something (if it has a value;
4383 * otherwise it will be left as-is) then concatenate all successive
4384 * PP_IDs into one.
4385 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004386static Token *expand_id(Token * tline)
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004387{
4388 Token *cur, *oldnext = NULL;
4389
H. Peter Anvin734b1882002-04-30 21:01:08 +00004390 if (!tline || !tline->next)
H. Peter Anvine2c80182005-01-15 22:15:51 +00004391 return tline;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004392
4393 cur = tline;
4394 while (cur->next &&
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004395 (cur->next->type == TOK_ID ||
4396 cur->next->type == TOK_PREPROC_ID
4397 || cur->next->type == TOK_NUMBER))
H. Peter Anvine2c80182005-01-15 22:15:51 +00004398 cur = cur->next;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004399
4400 /* If identifier consists of just one token, don't expand */
4401 if (cur == tline)
H. Peter Anvine2c80182005-01-15 22:15:51 +00004402 return tline;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004403
H. Peter Anvine2c80182005-01-15 22:15:51 +00004404 if (cur) {
4405 oldnext = cur->next; /* Detach the tail past identifier */
4406 cur->next = NULL; /* so that expand_smacro stops here */
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004407 }
4408
H. Peter Anvin734b1882002-04-30 21:01:08 +00004409 tline = expand_smacro(tline);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004410
H. Peter Anvine2c80182005-01-15 22:15:51 +00004411 if (cur) {
4412 /* expand_smacro possibly changhed tline; re-scan for EOL */
4413 cur = tline;
4414 while (cur && cur->next)
4415 cur = cur->next;
4416 if (cur)
4417 cur->next = oldnext;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004418 }
4419
4420 return tline;
4421}
4422
4423/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004424 * Determine whether the given line constitutes a multi-line macro
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004425 * call, and return the MMacro structure called if so. Doesn't have
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004426 * to check for an initial label - that's taken care of in
4427 * expand_mmacro - but must check numbers of parameters. Guaranteed
4428 * to be called with tline->type == TOK_ID, so the putative macro
4429 * name is easy to find.
4430 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004431static MMacro *is_mmacro(Token * tline, Token *** params_array)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004432{
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004433 MMacro *head, *m;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004434 Token **params;
4435 int nparam;
4436
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004437 head = (MMacro *) hash_findix(&mmacros, tline->text);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004438
4439 /*
4440 * Efficiency: first we see if any macro exists with the given
4441 * name. If not, we can return NULL immediately. _Then_ we
4442 * count the parameters, and then we look further along the
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004443 * list if necessary to find the proper MMacro.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004444 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004445 list_for_each(m, head)
4446 if (!mstrcmp(m->name, tline->text, m->casesense))
H. Peter Anvine2c80182005-01-15 22:15:51 +00004447 break;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004448 if (!m)
H. Peter Anvine2c80182005-01-15 22:15:51 +00004449 return NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004450
4451 /*
4452 * OK, we have a potential macro. Count and demarcate the
4453 * parameters.
4454 */
H. Peter Anvin734b1882002-04-30 21:01:08 +00004455 count_mmac_params(tline->next, &nparam, &params);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004456
4457 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004458 * So we know how many parameters we've got. Find the MMacro
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004459 * structure that handles this number.
4460 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004461 while (m) {
4462 if (m->nparam_min <= nparam
4463 && (m->plus || nparam <= m->nparam_max)) {
4464 /*
4465 * This one is right. Just check if cycle removal
4466 * prohibits us using it before we actually celebrate...
4467 */
4468 if (m->in_progress > m->max_depth) {
4469 if (m->max_depth > 0) {
4470 error(ERR_WARNING,
4471 "reached maximum recursion depth of %i",
4472 m->max_depth);
4473 }
4474 nasm_free(params);
4475 return NULL;
4476 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00004477 /*
4478 * It's right, and we can use it. Add its default
4479 * parameters to the end of our list if necessary.
4480 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004481 if (m->defaults && nparam < m->nparam_min + m->ndefs) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00004482 params =
4483 nasm_realloc(params,
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004484 ((m->nparam_min + m->ndefs +
H. Peter Anvine2c80182005-01-15 22:15:51 +00004485 1) * sizeof(*params)));
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004486 while (nparam < m->nparam_min + m->ndefs) {
4487 params[nparam] = m->defaults[nparam - m->nparam_min];
H. Peter Anvine2c80182005-01-15 22:15:51 +00004488 nparam++;
4489 }
4490 }
4491 /*
4492 * If we've gone over the maximum parameter count (and
4493 * we're in Plus mode), ignore parameters beyond
4494 * nparam_max.
4495 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004496 if (m->plus && nparam > m->nparam_max)
4497 nparam = m->nparam_max;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004498 /*
4499 * Then terminate the parameter list, and leave.
4500 */
4501 if (!params) { /* need this special case */
4502 params = nasm_malloc(sizeof(*params));
4503 nparam = 0;
4504 }
4505 params[nparam] = NULL;
4506 *params_array = params;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004507 return m;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004508 }
4509 /*
4510 * This one wasn't right: look for the next one with the
4511 * same name.
4512 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004513 list_for_each(m, m->next)
4514 if (!mstrcmp(m->name, tline->text, m->casesense))
H. Peter Anvine2c80182005-01-15 22:15:51 +00004515 break;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004516 }
4517
4518 /*
4519 * After all that, we didn't find one with the right number of
4520 * parameters. Issue a warning, and fail to expand the macro.
4521 */
H. Peter Anvin917a3492008-09-24 09:14:49 -07004522 error(ERR_WARNING|ERR_PASS1|ERR_WARN_MNP,
H. Peter Anvine2c80182005-01-15 22:15:51 +00004523 "macro `%s' exists, but not taking %d parameters",
4524 tline->text, nparam);
H. Peter Anvin734b1882002-04-30 21:01:08 +00004525 nasm_free(params);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004526 return NULL;
4527}
4528
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004529
4530/*
4531 * Save MMacro invocation specific fields in
4532 * preparation for a recursive macro expansion
4533 */
4534static void push_mmacro(MMacro *m)
4535{
4536 MMacroInvocation *i;
4537
4538 i = nasm_malloc(sizeof(MMacroInvocation));
4539 i->prev = m->prev;
4540 i->params = m->params;
4541 i->iline = m->iline;
4542 i->nparam = m->nparam;
4543 i->rotate = m->rotate;
4544 i->paramlen = m->paramlen;
4545 i->unique = m->unique;
4546 i->condcnt = m->condcnt;
4547 m->prev = i;
4548}
4549
4550
4551/*
4552 * Restore MMacro invocation specific fields that were
4553 * saved during a previous recursive macro expansion
4554 */
4555static void pop_mmacro(MMacro *m)
4556{
4557 MMacroInvocation *i;
4558
4559 if (m->prev) {
4560 i = m->prev;
4561 m->prev = i->prev;
4562 m->params = i->params;
4563 m->iline = i->iline;
4564 m->nparam = i->nparam;
4565 m->rotate = i->rotate;
4566 m->paramlen = i->paramlen;
4567 m->unique = i->unique;
4568 m->condcnt = i->condcnt;
4569 nasm_free(i);
4570 }
4571}
4572
4573
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004574/*
4575 * Expand the multi-line macro call made by the given line, if
4576 * there is one to be expanded. If there is, push the expansion on
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004577 * istk->expansion and return 1. Otherwise return 0.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004578 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004579static int expand_mmacro(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004580{
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004581 Token *startline = tline;
H. Peter Anvineba20a72002-04-30 20:53:55 +00004582 Token *label = NULL;
4583 int dont_prepend = 0;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004584 Token **params, *t, *tt;
4585 MMacro *m;
4586 Line *l, *ll;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004587 int i, nparam, *paramlen;
H. Peter Anvinc751e862008-06-09 10:18:45 -07004588 const char *mname;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004589
4590 t = tline;
H. Peter Anvineba20a72002-04-30 20:53:55 +00004591 skip_white_(t);
H. Peter Anvince2233b2008-05-25 21:57:00 -07004592 /* if (!tok_type_(t, TOK_ID)) Lino 02/25/02 */
H. Peter Anvindce1e2f2002-04-30 21:06:37 +00004593 if (!tok_type_(t, TOK_ID) && !tok_type_(t, TOK_PREPROC_ID))
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004594 return 0;
4595 m = is_mmacro(t, &params);
4596 if (m) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004597 mname = t->text;
H. Peter Anvinc751e862008-06-09 10:18:45 -07004598 } else {
H. Peter Anvine2c80182005-01-15 22:15:51 +00004599 Token *last;
4600 /*
4601 * We have an id which isn't a macro call. We'll assume
4602 * it might be a label; we'll also check to see if a
4603 * colon follows it. Then, if there's another id after
4604 * that lot, we'll check it again for macro-hood.
4605 */
4606 label = last = t;
4607 t = t->next;
4608 if (tok_type_(t, TOK_WHITESPACE))
4609 last = t, t = t->next;
4610 if (tok_is_(t, ":")) {
4611 dont_prepend = 1;
4612 last = t, t = t->next;
4613 if (tok_type_(t, TOK_WHITESPACE))
4614 last = t, t = t->next;
4615 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004616 if (!tok_type_(t, TOK_ID) || !(m = is_mmacro(t, &params)))
4617 return 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004618 last->next = NULL;
Keith Kanios891775e2009-07-11 06:08:54 -05004619 mname = t->text;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004620 tline = t;
H. Peter Anvineba20a72002-04-30 20:53:55 +00004621 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004622
4623 /*
4624 * Fix up the parameters: this involves stripping leading and
4625 * trailing whitespace, then stripping braces if they are
4626 * present.
4627 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004628 for (nparam = 0; params[nparam]; nparam++) ;
H. Peter Anvin734b1882002-04-30 21:01:08 +00004629 paramlen = nparam ? nasm_malloc(nparam * sizeof(*paramlen)) : NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004630
H. Peter Anvine2c80182005-01-15 22:15:51 +00004631 for (i = 0; params[i]; i++) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07004632 int brace = false;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004633 int comma = (!m->plus || i < nparam - 1);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004634
H. Peter Anvine2c80182005-01-15 22:15:51 +00004635 t = params[i];
4636 skip_white_(t);
4637 if (tok_is_(t, "{"))
H. Peter Anvin6867acc2007-10-10 14:58:45 -07004638 t = t->next, brace = true, comma = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004639 params[i] = t;
4640 paramlen[i] = 0;
4641 while (t) {
4642 if (comma && t->type == TOK_OTHER && !strcmp(t->text, ","))
4643 break; /* ... because we have hit a comma */
4644 if (comma && t->type == TOK_WHITESPACE
4645 && tok_is_(t->next, ","))
4646 break; /* ... or a space then a comma */
4647 if (brace && t->type == TOK_OTHER && !strcmp(t->text, "}"))
4648 break; /* ... or a brace */
4649 t = t->next;
4650 paramlen[i]++;
4651 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004652 }
4653
4654 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004655 * OK, we have a MMacro structure together with a set of
4656 * parameters. We must now go through the expansion and push
4657 * copies of each Line on to istk->expansion. Substitution of
H. Peter Anvin76690a12002-04-30 20:52:49 +00004658 * parameter tokens and macro-local tokens doesn't get done
4659 * until the single-line macro substitution process; this is
4660 * because delaying them allows us to change the semantics
4661 * later through %rotate.
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004662 *
4663 * First, push an end marker on to istk->expansion, mark this
4664 * macro as in progress, and set up its invocation-specific
4665 * variables.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004666 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004667 ll = nasm_malloc(sizeof(Line));
4668 ll->next = istk->expansion;
4669 ll->finishes = m;
4670 ll->first = NULL;
4671 istk->expansion = ll;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004672
4673 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004674 * Save the previous MMacro expansion in the case of
4675 * macro recursion
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004676 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004677 if (m->max_depth && m->in_progress)
4678 push_mmacro(m);
4679
4680 m->in_progress ++;
4681 m->params = params;
4682 m->iline = tline;
4683 m->nparam = nparam;
4684 m->rotate = 0;
4685 m->paramlen = paramlen;
4686 m->unique = unique++;
4687 m->lineno = 0;
4688 m->condcnt = 0;
4689
4690 m->next_active = istk->mstk;
4691 istk->mstk = m;
4692
4693 list_for_each(l, m->expansion) {
4694 Token **tail;
4695
4696 ll = nasm_malloc(sizeof(Line));
4697 ll->finishes = NULL;
4698 ll->next = istk->expansion;
4699 istk->expansion = ll;
4700 tail = &ll->first;
4701
4702 list_for_each(t, l->first) {
4703 Token *x = t;
4704 switch (t->type) {
4705 case TOK_PREPROC_Q:
4706 tt = *tail = new_Token(NULL, TOK_ID, mname, 0);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004707 break;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004708 case TOK_PREPROC_QQ:
4709 tt = *tail = new_Token(NULL, TOK_ID, m->name, 0);
4710 break;
4711 case TOK_PREPROC_ID:
4712 if (t->text[1] == '0' && t->text[2] == '0') {
4713 dont_prepend = -1;
4714 x = label;
4715 if (!x)
4716 continue;
4717 }
4718 /* fall through */
4719 default:
4720 tt = *tail = new_Token(NULL, x->type, x->text, 0);
4721 break;
4722 }
4723 tail = &tt->next;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004724 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004725 *tail = NULL;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004726 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004727
4728 /*
H. Peter Anvineba20a72002-04-30 20:53:55 +00004729 * If we had a label, push it on as the first line of
4730 * the macro expansion.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004731 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004732 if (label) {
4733 if (dont_prepend < 0)
4734 free_tlist(startline);
4735 else {
4736 ll = nasm_malloc(sizeof(Line));
4737 ll->finishes = NULL;
4738 ll->next = istk->expansion;
4739 istk->expansion = ll;
4740 ll->first = startline;
4741 if (!dont_prepend) {
4742 while (label->next)
4743 label = label->next;
4744 label->next = tt = new_Token(NULL, TOK_OTHER, ":", 0);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004745 }
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004746 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004747 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004748
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004749 list->uplevel(m->nolist ? LIST_MACRO_NOLIST : LIST_MACRO);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004750
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004751 return 1;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004752}
4753
Victor van den Elzen3b404c02008-09-18 13:51:36 +02004754/* The function that actually does the error reporting */
4755static void verror(int severity, const char *fmt, va_list arg)
4756{
4757 char buff[1024];
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004758 MMacro *mmac = NULL;
4759 int delta = 0;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02004760
4761 vsnprintf(buff, sizeof(buff), fmt, arg);
4762
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004763 /* get %macro name */
4764 if (istk && istk->mstk) {
4765 mmac = istk->mstk;
4766 /* but %rep blocks should be skipped */
4767 while (mmac && !mmac->name)
4768 mmac = mmac->next_active, delta++;
Cyrill Gorcunov9900c6b2011-10-09 18:58:46 +04004769 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004770
4771 if (mmac)
4772 nasm_error(severity, "(%s:%d) %s",
4773 mmac->name, mmac->lineno - delta, buff);
4774 else
4775 nasm_error(severity, "%s", buff);
Victor van den Elzen3b404c02008-09-18 13:51:36 +02004776}
4777
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004778/*
4779 * Since preprocessor always operate only on the line that didn't
H. Peter Anvin917a3492008-09-24 09:14:49 -07004780 * arrived yet, we should always use ERR_OFFBY1.
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004781 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004782static void error(int severity, const char *fmt, ...)
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004783{
4784 va_list arg;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004785
4786 /* If we're in a dead branch of IF or something like it, ignore the error */
4787 if (istk && istk->conds && !emitting(istk->conds->state))
4788 return;
4789
H. Peter Anvin734b1882002-04-30 21:01:08 +00004790 va_start(arg, fmt);
Victor van den Elzen3b404c02008-09-18 13:51:36 +02004791 verror(severity, fmt, arg);
H. Peter Anvin734b1882002-04-30 21:01:08 +00004792 va_end(arg);
Victor van den Elzen3b404c02008-09-18 13:51:36 +02004793}
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004794
Victor van den Elzen3b404c02008-09-18 13:51:36 +02004795/*
4796 * Because %else etc are evaluated in the state context
4797 * of the previous branch, errors might get lost with error():
4798 * %if 0 ... %else trailing garbage ... %endif
4799 * So %else etc should report errors with this function.
4800 */
4801static void error_precond(int severity, const char *fmt, ...)
4802{
4803 va_list arg;
4804
4805 /* Only ignore the error if it's really in a dead branch */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004806 if (istk && istk->conds && istk->conds->state == COND_NEVER)
Victor van den Elzen3b404c02008-09-18 13:51:36 +02004807 return;
4808
4809 va_start(arg, fmt);
4810 verror(severity, fmt, arg);
4811 va_end(arg);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004812}
4813
H. Peter Anvin734b1882002-04-30 21:01:08 +00004814static void
H. Peter Anvindbb640b2009-07-18 18:57:16 -07004815pp_reset(char *file, int apass, ListGen * listgen, StrList **deplist)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004816{
H. Peter Anvin7383b402008-09-24 10:20:40 -07004817 Token *t;
4818
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004819 cstk = NULL;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004820 istk = nasm_malloc(sizeof(Include));
4821 istk->next = NULL;
4822 istk->conds = NULL;
4823 istk->expansion = NULL;
4824 istk->mstk = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004825 istk->fp = fopen(file, "r");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004826 istk->fname = NULL;
H. Peter Anvineba20a72002-04-30 20:53:55 +00004827 src_set_fname(nasm_strdup(file));
4828 src_set_linnum(0);
4829 istk->lineinc = 1;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004830 if (!istk->fp)
H. Peter Anvin917a3492008-09-24 09:14:49 -07004831 error(ERR_FATAL|ERR_NOFILE, "unable to open input file `%s'",
H. Peter Anvine2c80182005-01-15 22:15:51 +00004832 file);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004833 defining = NULL;
Charles Crayned4200be2008-07-12 16:42:33 -07004834 nested_mac_count = 0;
4835 nested_rep_count = 0;
H. Peter Anvin97a23472007-09-16 17:57:25 -07004836 init_macros();
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004837 unique = 0;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004838 if (tasm_compatible_mode) {
H. Peter Anvina4835d42008-05-20 14:21:29 -07004839 stdmacpos = nasm_stdmac;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004840 } else {
H. Peter Anvina4835d42008-05-20 14:21:29 -07004841 stdmacpos = nasm_stdmac_after_tasm;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004842 }
H. Peter Anvind2456592008-06-19 15:04:18 -07004843 any_extrastdmac = extrastdmac && *extrastdmac;
4844 do_predef = true;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004845 list = listgen;
H. Peter Anvin61f130f2008-09-25 15:45:06 -07004846
4847 /*
4848 * 0 for dependencies, 1 for preparatory passes, 2 for final pass.
4849 * The caller, however, will also pass in 3 for preprocess-only so
4850 * we can set __PASS__ accordingly.
4851 */
4852 pass = apass > 2 ? 2 : apass;
4853
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07004854 dephead = deptail = deplist;
4855 if (deplist) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004856 StrList *sl = nasm_malloc(strlen(file)+1+sizeof sl->next);
4857 sl->next = NULL;
4858 strcpy(sl->str, file);
4859 *deptail = sl;
4860 deptail = &sl->next;
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07004861 }
H. Peter Anvin7383b402008-09-24 10:20:40 -07004862
H. Peter Anvin61f130f2008-09-25 15:45:06 -07004863 /*
4864 * Define the __PASS__ macro. This is defined here unlike
4865 * all the other builtins, because it is special -- it varies between
4866 * passes.
4867 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004868 t = nasm_malloc(sizeof(*t));
4869 t->next = NULL;
H. Peter Anvin61f130f2008-09-25 15:45:06 -07004870 make_tok_num(t, apass);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004871 t->a.mac = NULL;
H. Peter Anvin7383b402008-09-24 10:20:40 -07004872 define_smacro(NULL, "__PASS__", true, 0, t);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004873}
4874
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004875static char *pp_getline(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004876{
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004877 char *line;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004878 Token *tline;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004879
H. Peter Anvine2c80182005-01-15 22:15:51 +00004880 while (1) {
4881 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004882 * Fetch a tokenized line, either from the macro-expansion
H. Peter Anvine2c80182005-01-15 22:15:51 +00004883 * buffer or from the input file.
4884 */
4885 tline = NULL;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004886 while (istk->expansion && istk->expansion->finishes) {
4887 Line *l = istk->expansion;
4888 if (!l->finishes->name && l->finishes->in_progress > 1) {
4889 Line *ll;
H. Peter Anvineba20a72002-04-30 20:53:55 +00004890
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004891 /*
4892 * This is a macro-end marker for a macro with no
4893 * name, which means it's not really a macro at all
4894 * but a %rep block, and the `in_progress' field is
4895 * more than 1, meaning that we still need to
4896 * repeat. (1 means the natural last repetition; 0
4897 * means termination by %exitrep.) We have
4898 * therefore expanded up to the %endrep, and must
4899 * push the whole block on to the expansion buffer
4900 * again. We don't bother to remove the macro-end
4901 * marker: we'd only have to generate another one
4902 * if we did.
4903 */
4904 l->finishes->in_progress--;
4905 list_for_each(l, l->finishes->expansion) {
4906 Token *t, *tt, **tail;
4907
4908 ll = nasm_malloc(sizeof(Line));
4909 ll->next = istk->expansion;
4910 ll->finishes = NULL;
4911 ll->first = NULL;
4912 tail = &ll->first;
4913
4914 list_for_each(t, l->first) {
4915 if (t->text || t->type == TOK_WHITESPACE) {
4916 tt = *tail = new_Token(NULL, t->type, t->text, 0);
4917 tail = &tt->next;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004918 }
4919 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004920
4921 istk->expansion = ll;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004922 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004923 } else {
4924 /*
4925 * Check whether a `%rep' was started and not ended
4926 * within this macro expansion. This can happen and
4927 * should be detected. It's a fatal error because
4928 * I'm too confused to work out how to recover
4929 * sensibly from it.
4930 */
4931 if (defining) {
4932 if (defining->name)
4933 error(ERR_PANIC,
4934 "defining with name in expansion");
4935 else if (istk->mstk->name)
4936 error(ERR_FATAL,
4937 "`%%rep' without `%%endrep' within"
4938 " expansion of macro `%s'",
4939 istk->mstk->name);
4940 }
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004941
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004942 /*
4943 * FIXME: investigate the relationship at this point between
4944 * istk->mstk and l->finishes
4945 */
4946 {
4947 MMacro *m = istk->mstk;
4948 istk->mstk = m->next_active;
4949 if (m->name) {
4950 /*
4951 * This was a real macro call, not a %rep, and
4952 * therefore the parameter information needs to
4953 * be freed.
4954 */
4955 if (m->prev) {
4956 pop_mmacro(m);
4957 l->finishes->in_progress --;
4958 } else {
4959 nasm_free(m->params);
4960 free_tlist(m->iline);
4961 nasm_free(m->paramlen);
4962 l->finishes->in_progress = 0;
4963 }
4964 } else
4965 free_mmacro(m);
4966 }
4967 istk->expansion = l->next;
4968 nasm_free(l);
4969 list->downlevel(LIST_MACRO);
4970 }
4971 }
4972 while (1) { /* until we get a line we can use */
4973
4974 if (istk->expansion) { /* from a macro expansion */
4975 char *p;
4976 Line *l = istk->expansion;
4977 if (istk->mstk)
4978 istk->mstk->lineno++;
4979 tline = l->first;
4980 istk->expansion = l->next;
4981 nasm_free(l);
4982 p = detoken(tline, false);
4983 list->line(LIST_MACRO, p);
4984 nasm_free(p);
4985 break;
4986 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00004987 line = read_line();
4988 if (line) { /* from the current input file */
4989 line = prepreproc(line);
Keith Kaniosb7a89542007-04-12 02:40:54 +00004990 tline = tokenize(line);
H. Peter Anvine2c80182005-01-15 22:15:51 +00004991 nasm_free(line);
4992 break;
4993 }
4994 /*
4995 * The current file has ended; work down the istk
4996 */
4997 {
4998 Include *i = istk;
4999 fclose(i->fp);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005000 if (i->conds) {
5001 /* nasm_error can't be conditionally suppressed */
5002 nasm_error(ERR_FATAL,
5003 "expected `%%endif' before end of file");
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005004 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00005005 /* only set line and file name if there's a next node */
5006 if (i->next) {
5007 src_set_linnum(i->lineno);
Cyrill Gorcunovd34a1082011-03-07 11:23:08 +03005008 nasm_free(src_set_fname(nasm_strdup(i->fname)));
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03005009 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00005010 istk = i->next;
5011 list->downlevel(LIST_INCLUDE);
5012 nasm_free(i);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005013 if (!istk)
5014 return NULL;
5015 if (istk->expansion && istk->expansion->finishes)
5016 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005017 }
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005018 }
5019
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005020 /*
5021 * We must expand MMacro parameters and MMacro-local labels
5022 * _before_ we plunge into directive processing, to cope
5023 * with things like `%define something %1' such as STRUC
5024 * uses. Unless we're _defining_ a MMacro, in which case
5025 * those tokens should be left alone to go into the
5026 * definition; and unless we're in a non-emitting
5027 * condition, in which case we don't want to meddle with
5028 * anything.
5029 */
5030 if (!defining && !(istk->conds && !emitting(istk->conds->state))
5031 && !(istk->mstk && !istk->mstk->in_progress)) {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005032 tline = expand_mmac_params(tline);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005033 }
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005034
H. Peter Anvine2c80182005-01-15 22:15:51 +00005035 /*
5036 * Check the line to see if it's a preprocessor directive.
5037 */
5038 if (do_directive(tline) == DIRECTIVE_FOUND) {
5039 continue;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005040 } else if (defining) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00005041 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005042 * We're defining a multi-line macro. We emit nothing
5043 * at all, and just
5044 * shove the tokenized line on to the macro definition.
H. Peter Anvine2c80182005-01-15 22:15:51 +00005045 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005046 Line *l = nasm_malloc(sizeof(Line));
5047 l->next = defining->expansion;
5048 l->first = tline;
5049 l->finishes = NULL;
5050 defining->expansion = l;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005051 continue;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005052 } else if (istk->conds && !emitting(istk->conds->state)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00005053 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005054 * We're in a non-emitting branch of a condition block.
H. Peter Anvine2c80182005-01-15 22:15:51 +00005055 * Emit nothing at all, not even a blank line: when we
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005056 * emerge from the condition we'll give a line-number
H. Peter Anvine2c80182005-01-15 22:15:51 +00005057 * directive so we keep our place correctly.
5058 */
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005059 free_tlist(tline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00005060 continue;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005061 } else if (istk->mstk && !istk->mstk->in_progress) {
5062 /*
5063 * We're in a %rep block which has been terminated, so
5064 * we're walking through to the %endrep without
5065 * emitting anything. Emit nothing at all, not even a
5066 * blank line: when we emerge from the %rep block we'll
5067 * give a line-number directive so we keep our place
5068 * correctly.
5069 */
5070 free_tlist(tline);
5071 continue;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005072 } else {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005073 tline = expand_smacro(tline);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005074 if (!expand_mmacro(tline)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00005075 /*
Keith Kaniosb7a89542007-04-12 02:40:54 +00005076 * De-tokenize the line again, and emit it.
H. Peter Anvine2c80182005-01-15 22:15:51 +00005077 */
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005078 line = detoken(tline, true);
5079 free_tlist(tline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00005080 break;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005081 } else {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005082 continue; /* expand_mmacro calls free_tlist */
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005083 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00005084 }
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005085 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005086
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005087 return line;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005088}
5089
H. Peter Anvine2c80182005-01-15 22:15:51 +00005090static void pp_cleanup(int pass)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005091{
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005092 if (defining) {
5093 if (defining->name) {
5094 error(ERR_NONFATAL,
5095 "end of file while still defining macro `%s'",
5096 defining->name);
5097 } else {
5098 error(ERR_NONFATAL, "end of file while still in %%rep");
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005099 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005100
5101 free_mmacro(defining);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005102 defining = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005103 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005104 while (cstk)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005105 ctx_pop();
H. Peter Anvin97a23472007-09-16 17:57:25 -07005106 free_macros();
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005107 while (istk) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00005108 Include *i = istk;
5109 istk = istk->next;
5110 fclose(i->fp);
5111 nasm_free(i->fname);
Cyrill Gorcunov8dcfd882011-03-03 09:18:56 +03005112 nasm_free(i);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005113 }
5114 while (cstk)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005115 ctx_pop();
H. Peter Anvin86877b22008-06-20 15:55:45 -07005116 nasm_free(src_set_fname(NULL));
H. Peter Anvine2c80182005-01-15 22:15:51 +00005117 if (pass == 0) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03005118 IncPath *i;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005119 free_llist(predef);
5120 delete_Blocks();
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03005121 while ((i = ipath)) {
5122 ipath = i->next;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005123 if (i->path)
5124 nasm_free(i->path);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03005125 nasm_free(i);
5126 }
H. Peter Anvin11dfa1a2008-07-02 18:11:04 -07005127 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005128}
5129
Keith Kaniosa6dfa782007-04-13 16:47:53 +00005130void pp_include_path(char *path)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005131{
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005132 IncPath *i;
H. Peter Anvin37a321f2007-09-24 13:41:58 -07005133
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005134 i = nasm_malloc(sizeof(IncPath));
5135 i->path = path ? nasm_strdup(path) : NULL;
5136 i->next = NULL;
Frank Kotlerd0ed6fd2003-08-27 11:33:56 +00005137
H. Peter Anvin89cee572009-07-15 09:16:54 -04005138 if (ipath) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00005139 IncPath *j = ipath;
H. Peter Anvin89cee572009-07-15 09:16:54 -04005140 while (j->next)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005141 j = j->next;
5142 j->next = i;
5143 } else {
5144 ipath = i;
Frank Kotlerd0ed6fd2003-08-27 11:33:56 +00005145 }
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005146}
Frank Kotlerd0ed6fd2003-08-27 11:33:56 +00005147
Keith Kaniosa6dfa782007-04-13 16:47:53 +00005148void pp_pre_include(char *fname)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005149{
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005150 Token *inc, *space, *name;
5151 Line *l;
5152
H. Peter Anvin734b1882002-04-30 21:01:08 +00005153 name = new_Token(NULL, TOK_INTERNAL_STRING, fname, 0);
5154 space = new_Token(name, TOK_WHITESPACE, NULL, 0);
5155 inc = new_Token(space, TOK_PREPROC_ID, "%include", 0);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005156
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005157 l = nasm_malloc(sizeof(Line));
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005158 l->next = predef;
5159 l->first = inc;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005160 l->finishes = NULL;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005161 predef = l;
5162}
5163
Keith Kaniosa6dfa782007-04-13 16:47:53 +00005164void pp_pre_define(char *definition)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005165{
5166 Token *def, *space;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005167 Line *l;
Keith Kaniosa6dfa782007-04-13 16:47:53 +00005168 char *equals;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005169
5170 equals = strchr(definition, '=');
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, "%define", 0);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005173 if (equals)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005174 *equals = ' ';
Keith Kaniosb7a89542007-04-12 02:40:54 +00005175 space->next = tokenize(definition);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005176 if (equals)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005177 *equals = '=';
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005178
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005179 l = nasm_malloc(sizeof(Line));
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005180 l->next = predef;
5181 l->first = def;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005182 l->finishes = NULL;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005183 predef = l;
5184}
5185
Keith Kaniosa6dfa782007-04-13 16:47:53 +00005186void pp_pre_undefine(char *definition)
H. Peter Anvin620515a2002-04-30 20:57:38 +00005187{
5188 Token *def, *space;
5189 Line *l;
5190
H. Peter Anvin734b1882002-04-30 21:01:08 +00005191 space = new_Token(NULL, TOK_WHITESPACE, NULL, 0);
5192 def = new_Token(space, TOK_PREPROC_ID, "%undef", 0);
Keith Kaniosb7a89542007-04-12 02:40:54 +00005193 space->next = tokenize(definition);
H. Peter Anvin620515a2002-04-30 20:57:38 +00005194
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005195 l = nasm_malloc(sizeof(Line));
H. Peter Anvin620515a2002-04-30 20:57:38 +00005196 l->next = predef;
5197 l->first = def;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005198 l->finishes = NULL;
H. Peter Anvin620515a2002-04-30 20:57:38 +00005199 predef = l;
5200}
5201
Keith Kaniosb7a89542007-04-12 02:40:54 +00005202/*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005203 * Added by Keith Kanios:
5204 *
Keith Kaniosb7a89542007-04-12 02:40:54 +00005205 * This function is used to assist with "runtime" preprocessor
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005206 * directives. (e.g. pp_runtime("%define __BITS__ 64");)
Keith Kaniosb7a89542007-04-12 02:40:54 +00005207 *
5208 * ERRORS ARE IGNORED HERE, SO MAKE COMPLETELY SURE THAT YOU
5209 * PASS A VALID STRING TO THIS FUNCTION!!!!!
5210 */
5211
Keith Kaniosa6dfa782007-04-13 16:47:53 +00005212void pp_runtime(char *definition)
Keith Kaniosb7a89542007-04-12 02:40:54 +00005213{
5214 Token *def;
H. Peter Anvin70653092007-10-19 14:42:29 -07005215
Keith Kaniosb7a89542007-04-12 02:40:54 +00005216 def = tokenize(definition);
H. Peter Anvin89cee572009-07-15 09:16:54 -04005217 if (do_directive(def) == NO_DIRECTIVE_FOUND)
Keith Kaniosb7a89542007-04-12 02:40:54 +00005218 free_tlist(def);
H. Peter Anvin70653092007-10-19 14:42:29 -07005219
Keith Kaniosb7a89542007-04-12 02:40:54 +00005220}
5221
H. Peter Anvina70547f2008-07-19 21:44:26 -07005222void pp_extra_stdmac(macros_t *macros)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005223{
H. Peter Anvin76690a12002-04-30 20:52:49 +00005224 extrastdmac = macros;
5225}
5226
Keith Kaniosa5fc6462007-10-13 07:09:22 -07005227static void make_tok_num(Token * tok, int64_t val)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005228{
Keith Kaniosa6dfa782007-04-13 16:47:53 +00005229 char numbuf[20];
Keith Kaniosa5fc6462007-10-13 07:09:22 -07005230 snprintf(numbuf, sizeof(numbuf), "%"PRId64"", val);
H. Peter Anvineba20a72002-04-30 20:53:55 +00005231 tok->text = nasm_strdup(numbuf);
5232 tok->type = TOK_NUMBER;
5233}
5234
Cyrill Gorcunov86b2ad02011-07-01 10:38:25 +04005235struct preproc_ops nasmpp = {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005236 pp_reset,
5237 pp_getline,
5238 pp_cleanup
5239};