blob: 0a02f54eb16a126ab10597ae997c49ef3d3c05c3 [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);
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +0400461static Context *get_ctx(const char *name, const char **namep);
Keith Kaniosa5fc6462007-10-13 07:09:22 -0700462static void make_tok_num(Token * tok, int64_t val);
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000463static void error(int severity, const char *fmt, ...);
Victor van den Elzen3b404c02008-09-18 13:51:36 +0200464static void error_precond(int severity, const char *fmt, ...);
H. Peter Anvince616072002-04-30 21:02:23 +0000465static void *new_Block(size_t size);
466static void delete_Blocks(void);
H. Peter Anvinc751e862008-06-09 10:18:45 -0700467static Token *new_Token(Token * next, enum pp_token_type type,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300468 const char *text, int txtlen);
H. Peter Anvin734b1882002-04-30 21:01:08 +0000469static Token *delete_Token(Token * t);
H. Peter Anvineba20a72002-04-30 20:53:55 +0000470
471/*
472 * Macros for safe checking of token pointers, avoid *(NULL)
473 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300474#define tok_type_(x,t) ((x) && (x)->type == (t))
475#define skip_white_(x) if (tok_type_((x), TOK_WHITESPACE)) (x)=(x)->next
476#define tok_is_(x,v) (tok_type_((x), TOK_OTHER) && !strcmp((x)->text,(v)))
477#define tok_isnt_(x,v) ((x) && ((x)->type!=TOK_OTHER || strcmp((x)->text,(v))))
H. Peter Anvin76690a12002-04-30 20:52:49 +0000478
Cyrill Gorcunov194ba892011-06-30 01:16:35 +0400479/*
H. Peter Anvin077fb932010-07-20 14:56:30 -0700480 * nasm_unquote with error if the string contains NUL characters.
481 * If the string contains NUL characters, issue an error and return
482 * the C len, i.e. truncate at the NUL.
483 */
484static size_t nasm_unquote_cstr(char *qstr, enum preproc_token directive)
485{
486 size_t len = nasm_unquote(qstr, NULL);
487 size_t clen = strlen(qstr);
488
489 if (len != clen)
490 error(ERR_NONFATAL, "NUL character in `%s' directive",
491 pp_directives[directive]);
492
493 return clen;
494}
495
496/*
H. Peter Anvinb40992c2010-09-15 08:57:21 -0700497 * In-place reverse a list of tokens.
498 */
499static Token *reverse_tokens(Token *t)
500{
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800501 Token *prev = NULL;
502 Token *next;
H. Peter Anvinb40992c2010-09-15 08:57:21 -0700503
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800504 while (t) {
505 next = t->next;
506 t->next = prev;
507 prev = t;
508 t = next;
509 }
H. Peter Anvinb40992c2010-09-15 08:57:21 -0700510
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800511 return prev;
H. Peter Anvinb40992c2010-09-15 08:57:21 -0700512}
513
514/*
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300515 * Handle TASM specific directives, which do not contain a % in
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000516 * front of them. We do it here because I could not find any other
517 * place to do it for the moment, and it is a hack (ideally it would
518 * be nice to be able to use the NASM pre-processor to do it).
519 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000520static char *check_tasm_directive(char *line)
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000521{
Keith Kaniosb7a89542007-04-12 02:40:54 +0000522 int32_t i, j, k, m, len;
Cyrill Gorcunovf66ac7d2009-10-12 20:41:13 +0400523 char *p, *q, *oldline, oldchar;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000524
Cyrill Gorcunovf66ac7d2009-10-12 20:41:13 +0400525 p = nasm_skip_spaces(line);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000526
527 /* Binary search for the directive name */
528 i = -1;
Cyrill Gorcunova7319242010-06-03 22:04:36 +0400529 j = ARRAY_SIZE(tasm_directives);
Cyrill Gorcunovf66ac7d2009-10-12 20:41:13 +0400530 q = nasm_skip_word(p);
531 len = q - p;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000532 if (len) {
533 oldchar = p[len];
534 p[len] = 0;
535 while (j - i > 1) {
536 k = (j + i) / 2;
537 m = nasm_stricmp(p, tasm_directives[k]);
538 if (m == 0) {
539 /* We have found a directive, so jam a % in front of it
540 * so that NASM will then recognise it as one if it's own.
541 */
542 p[len] = oldchar;
543 len = strlen(p);
544 oldline = line;
545 line = nasm_malloc(len + 2);
546 line[0] = '%';
547 if (k == TM_IFDIFI) {
H. Peter Anvin18f48792009-06-27 15:56:27 -0700548 /*
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300549 * NASM does not recognise IFDIFI, so we convert
550 * it to %if 0. This is not used in NASM
551 * compatible code, but does need to parse for the
552 * TASM macro package.
H. Peter Anvine2c80182005-01-15 22:15:51 +0000553 */
H. Peter Anvin18f48792009-06-27 15:56:27 -0700554 strcpy(line + 1, "if 0");
H. Peter Anvine2c80182005-01-15 22:15:51 +0000555 } else {
556 memcpy(line + 1, p, len + 1);
557 }
558 nasm_free(oldline);
559 return line;
560 } else if (m < 0) {
561 j = k;
562 } else
563 i = k;
564 }
565 p[len] = oldchar;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000566 }
567 return line;
568}
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000569
H. Peter Anvin76690a12002-04-30 20:52:49 +0000570/*
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000571 * The pre-preprocessing stage... This function translates line
572 * number indications as they emerge from GNU cpp (`# lineno "file"
573 * flags') into NASM preprocessor line number indications (`%line
574 * lineno file').
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000575 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000576static char *prepreproc(char *line)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000577{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000578 int lineno, fnlen;
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000579 char *fname, *oldline;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000580
H. Peter Anvine2c80182005-01-15 22:15:51 +0000581 if (line[0] == '#' && line[1] == ' ') {
582 oldline = line;
583 fname = oldline + 2;
584 lineno = atoi(fname);
585 fname += strspn(fname, "0123456789 ");
586 if (*fname == '"')
587 fname++;
588 fnlen = strcspn(fname, "\"");
589 line = nasm_malloc(20 + fnlen);
590 snprintf(line, 20 + fnlen, "%%line %d %.*s", lineno, fnlen, fname);
591 nasm_free(oldline);
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000592 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000593 if (tasm_compatible_mode)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000594 return check_tasm_directive(line);
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000595 return line;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000596}
597
598/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000599 * Free a linked list of tokens.
600 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000601static void free_tlist(Token * list)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000602{
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +0400603 while (list)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000604 list = delete_Token(list);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000605}
606
607/*
608 * Free a linked list of lines.
609 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000610static void free_llist(Line * list)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000611{
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +0400612 Line *l, *tmp;
613 list_for_each_safe(l, tmp, list) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000614 free_tlist(l->first);
615 nasm_free(l);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000616 }
617}
618
619/*
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800620 * Free an MMacro
H. Peter Anvineba20a72002-04-30 20:53:55 +0000621 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800622static void free_mmacro(MMacro * m)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000623{
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800624 nasm_free(m->name);
625 free_tlist(m->dlist);
626 nasm_free(m->defaults);
627 free_llist(m->expansion);
628 nasm_free(m);
H. Peter Anvineba20a72002-04-30 20:53:55 +0000629}
630
631/*
H. Peter Anvin97a23472007-09-16 17:57:25 -0700632 * Free all currently defined macros, and free the hash tables
633 */
H. Peter Anvin072771e2008-05-22 13:17:51 -0700634static void free_smacro_table(struct hash_table *smt)
H. Peter Anvin97a23472007-09-16 17:57:25 -0700635{
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +0400636 SMacro *s, *tmp;
H. Peter Anvin072771e2008-05-22 13:17:51 -0700637 const char *key;
638 struct hash_tbl_node *it = NULL;
H. Peter Anvin97a23472007-09-16 17:57:25 -0700639
H. Peter Anvin072771e2008-05-22 13:17:51 -0700640 while ((s = hash_iterate(smt, &it, &key)) != NULL) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300641 nasm_free((void *)key);
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +0400642 list_for_each_safe(s, tmp, s) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300643 nasm_free(s->name);
644 free_tlist(s->expansion);
645 nasm_free(s);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300646 }
H. Peter Anvin97a23472007-09-16 17:57:25 -0700647 }
H. Peter Anvin072771e2008-05-22 13:17:51 -0700648 hash_free(smt);
649}
650
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800651static void free_mmacro_table(struct hash_table *mmt)
H. Peter Anvin072771e2008-05-22 13:17:51 -0700652{
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800653 MMacro *m, *tmp;
H. Peter Anvin072771e2008-05-22 13:17:51 -0700654 const char *key;
655 struct hash_tbl_node *it = NULL;
H. Peter Anvin97a23472007-09-16 17:57:25 -0700656
657 it = NULL;
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800658 while ((m = hash_iterate(mmt, &it, &key)) != NULL) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300659 nasm_free((void *)key);
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800660 list_for_each_safe(m ,tmp, m)
661 free_mmacro(m);
H. Peter Anvin97a23472007-09-16 17:57:25 -0700662 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800663 hash_free(mmt);
H. Peter Anvin072771e2008-05-22 13:17:51 -0700664}
665
666static void free_macros(void)
667{
H. Peter Anvin166c2472008-05-28 12:28:58 -0700668 free_smacro_table(&smacros);
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800669 free_mmacro_table(&mmacros);
H. Peter Anvin97a23472007-09-16 17:57:25 -0700670}
671
672/*
673 * Initialize the hash tables
674 */
675static void init_macros(void)
676{
H. Peter Anvin166c2472008-05-28 12:28:58 -0700677 hash_init(&smacros, HASH_LARGE);
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800678 hash_init(&mmacros, HASH_LARGE);
H. Peter Anvin97a23472007-09-16 17:57:25 -0700679}
680
681/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000682 * Pop the context stack.
683 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000684static void ctx_pop(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000685{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000686 Context *c = cstk;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000687
688 cstk = cstk->next;
H. Peter Anvin166c2472008-05-28 12:28:58 -0700689 free_smacro_table(&c->localmac);
H. Peter Anvin734b1882002-04-30 21:01:08 +0000690 nasm_free(c->name);
691 nasm_free(c);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000692}
693
H. Peter Anvin072771e2008-05-22 13:17:51 -0700694/*
695 * Search for a key in the hash index; adding it if necessary
696 * (in which case we initialize the data pointer to NULL.)
697 */
698static void **
699hash_findi_add(struct hash_table *hash, const char *str)
700{
701 struct hash_insert hi;
702 void **r;
703 char *strx;
704
705 r = hash_findi(hash, str, &hi);
706 if (r)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300707 return r;
H. Peter Anvin072771e2008-05-22 13:17:51 -0700708
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300709 strx = nasm_strdup(str); /* Use a more efficient allocator here? */
H. Peter Anvin072771e2008-05-22 13:17:51 -0700710 return hash_add(&hi, strx, NULL);
711}
712
713/*
714 * Like hash_findi, but returns the data element rather than a pointer
715 * to it. Used only when not adding a new element, hence no third
716 * argument.
717 */
718static void *
719hash_findix(struct hash_table *hash, const char *str)
720{
721 void **p;
722
723 p = hash_findi(hash, str, NULL);
724 return p ? *p : NULL;
725}
726
Cyrill Gorcunov15bdc512010-07-13 11:27:41 +0400727/*
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800728 * read line from standart macros set,
Cyrill Gorcunov15bdc512010-07-13 11:27:41 +0400729 * if there no more left -- return NULL
730 */
731static char *line_from_stdmac(void)
732{
733 unsigned char c;
734 const unsigned char *p = stdmacpos;
735 char *line, *q;
736 size_t len = 0;
737
738 if (!stdmacpos)
739 return NULL;
740
741 while ((c = *p++)) {
742 if (c >= 0x80)
743 len += pp_directives_len[c - 0x80] + 1;
744 else
745 len++;
746 }
747
748 line = nasm_malloc(len + 1);
749 q = line;
750 while ((c = *stdmacpos++)) {
751 if (c >= 0x80) {
752 memcpy(q, pp_directives[c - 0x80], pp_directives_len[c - 0x80]);
753 q += pp_directives_len[c - 0x80];
754 *q++ = ' ';
755 } else {
756 *q++ = c;
757 }
758 }
759 stdmacpos = p;
760 *q = '\0';
761
762 if (!*stdmacpos) {
763 /* This was the last of the standard macro chain... */
764 stdmacpos = NULL;
765 if (any_extrastdmac) {
766 stdmacpos = extrastdmac;
767 any_extrastdmac = false;
768 } else if (do_predef) {
769 Line *pd, *l;
770 Token *head, **tail, *t;
771
772 /*
773 * Nasty hack: here we push the contents of
774 * `predef' on to the top-level expansion stack,
775 * since this is the most convenient way to
776 * implement the pre-include and pre-define
777 * features.
778 */
779 list_for_each(pd, predef) {
780 head = NULL;
781 tail = &head;
782 list_for_each(t, pd->first) {
783 *tail = new_Token(NULL, t->type, t->text, 0);
784 tail = &(*tail)->next;
785 }
786
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800787 l = nasm_malloc(sizeof(Line));
788 l->next = istk->expansion;
789 l->first = head;
790 l->finishes = NULL;
791
792 istk->expansion = l;
Cyrill Gorcunov15bdc512010-07-13 11:27:41 +0400793 }
794 do_predef = false;
795 }
796 }
797
798 return line;
799}
800
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000801#define BUF_DELTA 512
802/*
803 * Read a line from the top file in istk, handling multiple CR/LFs
804 * at the end of the line read, and handling spurious ^Zs. Will
805 * return lines from the standard macro set if this has not already
806 * been done.
807 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000808static char *read_line(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000809{
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000810 char *buffer, *p, *q;
H. Peter Anvin9f394642002-04-30 21:07:51 +0000811 int bufsize, continued_count;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000812
Cyrill Gorcunov15bdc512010-07-13 11:27:41 +0400813 /*
814 * standart macros set (predefined) goes first
815 */
816 p = line_from_stdmac();
817 if (p)
818 return p;
H. Peter Anvin72edbb82008-06-19 16:00:04 -0700819
Cyrill Gorcunov15bdc512010-07-13 11:27:41 +0400820 /*
821 * regular read from a file
822 */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000823 bufsize = BUF_DELTA;
824 buffer = nasm_malloc(BUF_DELTA);
825 p = buffer;
H. Peter Anvin9f394642002-04-30 21:07:51 +0000826 continued_count = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000827 while (1) {
828 q = fgets(p, bufsize - (p - buffer), istk->fp);
829 if (!q)
830 break;
831 p += strlen(p);
832 if (p > buffer && p[-1] == '\n') {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300833 /*
834 * Convert backslash-CRLF line continuation sequences into
835 * nothing at all (for DOS and Windows)
836 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000837 if (((p - 2) > buffer) && (p[-3] == '\\') && (p[-2] == '\r')) {
838 p -= 3;
839 *p = 0;
840 continued_count++;
841 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300842 /*
843 * Also convert backslash-LF line continuation sequences into
844 * nothing at all (for Unix)
845 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000846 else if (((p - 1) > buffer) && (p[-2] == '\\')) {
847 p -= 2;
848 *p = 0;
849 continued_count++;
850 } else {
851 break;
852 }
853 }
854 if (p - buffer > bufsize - 10) {
Keith Kaniosb7a89542007-04-12 02:40:54 +0000855 int32_t offset = p - buffer;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000856 bufsize += BUF_DELTA;
857 buffer = nasm_realloc(buffer, bufsize);
858 p = buffer + offset; /* prevent stale-pointer problems */
859 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000860 }
861
H. Peter Anvine2c80182005-01-15 22:15:51 +0000862 if (!q && p == buffer) {
863 nasm_free(buffer);
864 return NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000865 }
866
Cyrill Gorcunova5aea572010-11-11 10:14:45 +0300867 src_set_linnum(src_get_linnum() + istk->lineinc +
868 (continued_count * istk->lineinc));
H. Peter Anvineba20a72002-04-30 20:53:55 +0000869
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000870 /*
871 * Play safe: remove CRs as well as LFs, if any of either are
872 * present at the end of the line.
873 */
H. Peter Anvineba20a72002-04-30 20:53:55 +0000874 while (--p >= buffer && (*p == '\n' || *p == '\r'))
H. Peter Anvine2c80182005-01-15 22:15:51 +0000875 *p = '\0';
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000876
877 /*
878 * Handle spurious ^Z, which may be inserted into source files
879 * by some file transfer utilities.
880 */
881 buffer[strcspn(buffer, "\032")] = '\0';
882
H. Peter Anvin734b1882002-04-30 21:01:08 +0000883 list->line(LIST_READ, buffer);
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000884
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000885 return buffer;
886}
887
888/*
Keith Kaniosb7a89542007-04-12 02:40:54 +0000889 * Tokenize a line of text. This is a very simple process since we
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000890 * don't need to parse the value out of e.g. numeric tokens: we
891 * simply split one string into many.
892 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000893static Token *tokenize(char *line)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000894{
H. Peter Anvinca544db2008-10-19 19:30:11 -0700895 char c, *p = line;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +0000896 enum pp_token_type type;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000897 Token *list = NULL;
898 Token *t, **tail = &list;
899
H. Peter Anvine2c80182005-01-15 22:15:51 +0000900 while (*line) {
901 p = line;
902 if (*p == '%') {
903 p++;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300904 if (*p == '+' && !nasm_isdigit(p[1])) {
905 p++;
906 type = TOK_PASTE;
907 } else if (nasm_isdigit(*p) ||
908 ((*p == '-' || *p == '+') && nasm_isdigit(p[1]))) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000909 do {
910 p++;
911 }
H. Peter Anvinbda7a6e2008-06-21 10:23:17 -0700912 while (nasm_isdigit(*p));
H. Peter Anvine2c80182005-01-15 22:15:51 +0000913 type = TOK_PREPROC_ID;
914 } else if (*p == '{') {
915 p++;
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800916 while (*p) {
917 if (*p == '}')
918 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000919 p[-1] = *p;
920 p++;
921 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800922 if (*p != '}')
923 error(ERR_WARNING | ERR_PASS1, "unterminated %{ construct");
H. Peter Anvine2c80182005-01-15 22:15:51 +0000924 p[-1] = '\0';
925 if (*p)
926 p++;
927 type = TOK_PREPROC_ID;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300928 } else if (*p == '[') {
929 int lvl = 1;
930 line += 2; /* Skip the leading %[ */
931 p++;
932 while (lvl && (c = *p++)) {
933 switch (c) {
934 case ']':
935 lvl--;
936 break;
937 case '%':
938 if (*p == '[')
939 lvl++;
940 break;
941 case '\'':
942 case '\"':
943 case '`':
Cyrill Gorcunovc6360a72010-07-13 13:32:19 +0400944 p = nasm_skip_string(p - 1) + 1;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300945 break;
946 default:
947 break;
948 }
949 }
950 p--;
951 if (*p)
952 *p++ = '\0';
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800953 if (lvl)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300954 error(ERR_NONFATAL, "unterminated %[ construct");
955 type = TOK_INDIRECT;
956 } else if (*p == '?') {
957 type = TOK_PREPROC_Q; /* %? */
958 p++;
959 if (*p == '?') {
960 type = TOK_PREPROC_QQ; /* %?? */
961 p++;
962 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800963 } else if (*p == '!') {
964 type = TOK_PREPROC_ID;
965 p++;
966 if (isidchar(*p)) {
967 do {
968 p++;
969 }
970 while (isidchar(*p));
971 } else if (*p == '\'' || *p == '\"' || *p == '`') {
972 p = nasm_skip_string(p);
973 if (*p)
974 p++;
975 else
976 error(ERR_NONFATAL|ERR_PASS1, "unterminated %! string");
977 } else {
978 /* %! without string or identifier */
979 type = TOK_OTHER; /* Legacy behavior... */
980 }
H. Peter Anvine2c80182005-01-15 22:15:51 +0000981 } else if (isidchar(*p) ||
982 ((*p == '!' || *p == '%' || *p == '$') &&
983 isidchar(p[1]))) {
984 do {
985 p++;
986 }
987 while (isidchar(*p));
988 type = TOK_PREPROC_ID;
989 } else {
990 type = TOK_OTHER;
991 if (*p == '%')
992 p++;
993 }
994 } else if (isidstart(*p) || (*p == '$' && isidstart(p[1]))) {
995 type = TOK_ID;
996 p++;
997 while (*p && isidchar(*p))
998 p++;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -0700999 } else if (*p == '\'' || *p == '"' || *p == '`') {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001000 /*
1001 * A string token.
1002 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001003 type = TOK_STRING;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001004 p = nasm_skip_string(p);
Nickolay Yurchenkof3b3ce22003-09-21 20:38:43 +00001005
H. Peter Anvine2c80182005-01-15 22:15:51 +00001006 if (*p) {
1007 p++;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001008 } else {
H. Peter Anvin917a3492008-09-24 09:14:49 -07001009 error(ERR_WARNING|ERR_PASS1, "unterminated string");
H. Peter Anvine2c80182005-01-15 22:15:51 +00001010 /* Handling unterminated strings by UNV */
1011 /* type = -1; */
1012 }
Victor van den Elzenfb5f2512009-04-17 16:17:59 +02001013 } else if (p[0] == '$' && p[1] == '$') {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001014 type = TOK_OTHER; /* TOKEN_BASE */
Victor van den Elzenfb5f2512009-04-17 16:17:59 +02001015 p += 2;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001016 } else if (isnumstart(*p)) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001017 bool is_hex = false;
1018 bool is_float = false;
1019 bool has_e = false;
1020 char c, *r;
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001021
H. Peter Anvine2c80182005-01-15 22:15:51 +00001022 /*
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001023 * A numeric token.
H. Peter Anvine2c80182005-01-15 22:15:51 +00001024 */
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001025
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001026 if (*p == '$') {
1027 p++;
1028 is_hex = true;
1029 }
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001030
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001031 for (;;) {
1032 c = *p++;
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001033
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001034 if (!is_hex && (c == 'e' || c == 'E')) {
1035 has_e = true;
1036 if (*p == '+' || *p == '-') {
1037 /*
1038 * e can only be followed by +/- if it is either a
1039 * prefixed hex number or a floating-point number
1040 */
1041 p++;
1042 is_float = true;
1043 }
1044 } else if (c == 'H' || c == 'h' || c == 'X' || c == 'x') {
1045 is_hex = true;
1046 } else if (c == 'P' || c == 'p') {
1047 is_float = true;
1048 if (*p == '+' || *p == '-')
1049 p++;
1050 } else if (isnumchar(c) || c == '_')
1051 ; /* just advance */
1052 else if (c == '.') {
1053 /*
1054 * we need to deal with consequences of the legacy
1055 * parser, like "1.nolist" being two tokens
1056 * (TOK_NUMBER, TOK_ID) here; at least give it
1057 * a shot for now. In the future, we probably need
1058 * a flex-based scanner with proper pattern matching
1059 * to do it as well as it can be done. Nothing in
1060 * the world is going to help the person who wants
1061 * 0x123.p16 interpreted as two tokens, though.
1062 */
1063 r = p;
1064 while (*r == '_')
1065 r++;
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001066
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001067 if (nasm_isdigit(*r) || (is_hex && nasm_isxdigit(*r)) ||
1068 (!is_hex && (*r == 'e' || *r == 'E')) ||
1069 (*r == 'p' || *r == 'P')) {
1070 p = r;
1071 is_float = true;
1072 } else
1073 break; /* Terminate the token */
1074 } else
1075 break;
1076 }
1077 p--; /* Point to first character beyond number */
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001078
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001079 if (p == line+1 && *line == '$') {
1080 type = TOK_OTHER; /* TOKEN_HERE */
1081 } else {
1082 if (has_e && !is_hex) {
1083 /* 1e13 is floating-point, but 1e13h is not */
1084 is_float = true;
1085 }
H. Peter Anvind784a082009-04-20 14:01:18 -07001086
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001087 type = is_float ? TOK_FLOAT : TOK_NUMBER;
1088 }
H. Peter Anvinbda7a6e2008-06-21 10:23:17 -07001089 } else if (nasm_isspace(*p)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001090 type = TOK_WHITESPACE;
Cyrill Gorcunovf66ac7d2009-10-12 20:41:13 +04001091 p = nasm_skip_spaces(p);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001092 /*
1093 * Whitespace just before end-of-line is discarded by
1094 * pretending it's a comment; whitespace just before a
1095 * comment gets lumped into the comment.
1096 */
1097 if (!*p || *p == ';') {
1098 type = TOK_COMMENT;
1099 while (*p)
1100 p++;
1101 }
1102 } else if (*p == ';') {
1103 type = TOK_COMMENT;
1104 while (*p)
1105 p++;
1106 } else {
1107 /*
1108 * Anything else is an operator of some kind. We check
1109 * for all the double-character operators (>>, <<, //,
1110 * %%, <=, >=, ==, !=, <>, &&, ||, ^^), but anything
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001111 * else is a single-character operator.
H. Peter Anvine2c80182005-01-15 22:15:51 +00001112 */
1113 type = TOK_OTHER;
1114 if ((p[0] == '>' && p[1] == '>') ||
1115 (p[0] == '<' && p[1] == '<') ||
1116 (p[0] == '/' && p[1] == '/') ||
1117 (p[0] == '<' && p[1] == '=') ||
1118 (p[0] == '>' && p[1] == '=') ||
1119 (p[0] == '=' && p[1] == '=') ||
1120 (p[0] == '!' && p[1] == '=') ||
1121 (p[0] == '<' && p[1] == '>') ||
1122 (p[0] == '&' && p[1] == '&') ||
1123 (p[0] == '|' && p[1] == '|') ||
1124 (p[0] == '^' && p[1] == '^')) {
1125 p++;
1126 }
1127 p++;
1128 }
Nickolay Yurchenkof3b3ce22003-09-21 20:38:43 +00001129
H. Peter Anvine2c80182005-01-15 22:15:51 +00001130 /* Handling unterminated string by UNV */
1131 /*if (type == -1)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001132 {
1133 *tail = t = new_Token(NULL, TOK_STRING, line, p-line+1);
1134 t->text[p-line] = *line;
1135 tail = &t->next;
1136 }
1137 else */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001138 if (type != TOK_COMMENT) {
1139 *tail = t = new_Token(NULL, type, line, p - line);
1140 tail = &t->next;
1141 }
1142 line = p;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001143 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001144 return list;
1145}
1146
H. Peter Anvince616072002-04-30 21:02:23 +00001147/*
1148 * this function allocates a new managed block of memory and
H. Peter Anvin70653092007-10-19 14:42:29 -07001149 * returns a pointer to the block. The managed blocks are
H. Peter Anvince616072002-04-30 21:02:23 +00001150 * deleted only all at once by the delete_Blocks function.
1151 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001152static void *new_Block(size_t size)
H. Peter Anvince616072002-04-30 21:02:23 +00001153{
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001154 Blocks *b = &blocks;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001155
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001156 /* first, get to the end of the linked list */
1157 while (b->next)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001158 b = b->next;
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001159 /* now allocate the requested chunk */
1160 b->chunk = nasm_malloc(size);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001161
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001162 /* now allocate a new block for the next request */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001163 b->next = nasm_malloc(sizeof(Blocks));
1164 /* and initialize the contents of the new block */
1165 b->next->next = NULL;
1166 b->next->chunk = NULL;
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001167 return b->chunk;
H. Peter Anvince616072002-04-30 21:02:23 +00001168}
1169
1170/*
1171 * this function deletes all managed blocks of memory
1172 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001173static void delete_Blocks(void)
H. Peter Anvince616072002-04-30 21:02:23 +00001174{
H. Peter Anvine2c80182005-01-15 22:15:51 +00001175 Blocks *a, *b = &blocks;
H. Peter Anvince616072002-04-30 21:02:23 +00001176
H. Peter Anvin70653092007-10-19 14:42:29 -07001177 /*
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001178 * keep in mind that the first block, pointed to by blocks
H. Peter Anvin70653092007-10-19 14:42:29 -07001179 * is a static and not dynamically allocated, so we don't
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001180 * free it.
1181 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001182 while (b) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001183 if (b->chunk)
1184 nasm_free(b->chunk);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001185 a = b;
1186 b = b->next;
1187 if (a != &blocks)
1188 nasm_free(a);
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001189 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001190}
H. Peter Anvin734b1882002-04-30 21:01:08 +00001191
1192/*
H. Peter Anvin70653092007-10-19 14:42:29 -07001193 * this function creates a new Token and passes a pointer to it
H. Peter Anvin734b1882002-04-30 21:01:08 +00001194 * back to the caller. It sets the type and text elements, and
H. Peter Anvinf26e0972008-07-01 21:26:27 -07001195 * also the a.mac and next elements to NULL.
H. Peter Anvin734b1882002-04-30 21:01:08 +00001196 */
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07001197static Token *new_Token(Token * next, enum pp_token_type type,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001198 const char *text, int txtlen)
H. Peter Anvin734b1882002-04-30 21:01:08 +00001199{
1200 Token *t;
1201 int i;
1202
H. Peter Anvin89cee572009-07-15 09:16:54 -04001203 if (!freeTokens) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001204 freeTokens = (Token *) new_Block(TOKEN_BLOCKSIZE * sizeof(Token));
1205 for (i = 0; i < TOKEN_BLOCKSIZE - 1; i++)
1206 freeTokens[i].next = &freeTokens[i + 1];
1207 freeTokens[i].next = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001208 }
1209 t = freeTokens;
1210 freeTokens = t->next;
1211 t->next = next;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07001212 t->a.mac = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001213 t->type = type;
H. Peter Anvin89cee572009-07-15 09:16:54 -04001214 if (type == TOK_WHITESPACE || !text) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001215 t->text = NULL;
1216 } else {
1217 if (txtlen == 0)
1218 txtlen = strlen(text);
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07001219 t->text = nasm_malloc(txtlen+1);
1220 memcpy(t->text, text, txtlen);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001221 t->text[txtlen] = '\0';
H. Peter Anvin734b1882002-04-30 21:01:08 +00001222 }
1223 return t;
1224}
1225
H. Peter Anvine2c80182005-01-15 22:15:51 +00001226static Token *delete_Token(Token * t)
H. Peter Anvin734b1882002-04-30 21:01:08 +00001227{
1228 Token *next = t->next;
1229 nasm_free(t->text);
H. Peter Anvin788e6c12002-04-30 21:02:01 +00001230 t->next = freeTokens;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001231 freeTokens = t;
1232 return next;
1233}
1234
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001235/*
1236 * Convert a line of tokens back into text.
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001237 * If expand_locals is not zero, identifiers of the form "%$*xxx"
1238 * will be transformed into ..@ctxnum.xxx
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001239 */
H. Peter Anvin9e200162008-06-04 17:23:14 -07001240static char *detoken(Token * tlist, bool expand_locals)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001241{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001242 Token *t;
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001243 char *line, *p;
H. Peter Anvinb4daadc2008-01-21 16:31:57 -08001244 const char *q;
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001245 int len = 0;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001246
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001247 list_for_each(t, tlist) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001248 if (t->type == TOK_PREPROC_ID && t->text[1] == '!') {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001249 char *v;
1250 char *q = t->text;
H. Peter Anvin077fb932010-07-20 14:56:30 -07001251
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001252 v = t->text + 2;
1253 if (*v == '\'' || *v == '\"' || *v == '`') {
1254 size_t len = nasm_unquote(v, NULL);
1255 size_t clen = strlen(v);
H. Peter Anvin077fb932010-07-20 14:56:30 -07001256
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001257 if (len != clen) {
1258 error(ERR_NONFATAL | ERR_PASS1,
1259 "NUL character in %! string");
1260 v = NULL;
1261 }
1262 }
H. Peter Anvin077fb932010-07-20 14:56:30 -07001263
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001264 if (v) {
1265 char *p = getenv(v);
1266 if (!p) {
1267 error(ERR_NONFATAL | ERR_PASS1,
1268 "nonexistent environment variable `%s'", v);
1269 p = "";
1270 }
1271 t->text = nasm_strdup(p);
1272 }
1273 nasm_free(q);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001274 }
H. Peter Anvin077fb932010-07-20 14:56:30 -07001275
H. Peter Anvine2c80182005-01-15 22:15:51 +00001276 /* Expand local macros here and not during preprocessing */
1277 if (expand_locals &&
1278 t->type == TOK_PREPROC_ID && t->text &&
1279 t->text[0] == '%' && t->text[1] == '$') {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001280 const char *q;
1281 char *p;
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04001282 Context *ctx = get_ctx(t->text, &q);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001283 if (ctx) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001284 char buffer[40];
Keith Kanios93f2e9a2007-04-14 00:10:59 +00001285 snprintf(buffer, sizeof(buffer), "..@%"PRIu32".", ctx->number);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001286 p = nasm_strcat(buffer, q);
1287 nasm_free(t->text);
1288 t->text = p;
1289 }
1290 }
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001291 if (t->type == TOK_WHITESPACE)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001292 len++;
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001293 else if (t->text)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001294 len += strlen(t->text);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001295 }
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001296
H. Peter Anvin734b1882002-04-30 21:01:08 +00001297 p = line = nasm_malloc(len + 1);
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001298
1299 list_for_each(t, tlist) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001300 if (t->type == TOK_WHITESPACE) {
H. Peter Anvinb4daadc2008-01-21 16:31:57 -08001301 *p++ = ' ';
H. Peter Anvine2c80182005-01-15 22:15:51 +00001302 } else if (t->text) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001303 q = t->text;
1304 while (*q)
1305 *p++ = *q++;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001306 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001307 }
1308 *p = '\0';
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001309
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001310 return line;
1311}
1312
1313/*
H. Peter Anvin76690a12002-04-30 20:52:49 +00001314 * A scanner, suitable for use by the expression evaluator, which
1315 * operates on a line of Tokens. Expects a pointer to a pointer to
1316 * the first token in the line to be passed in as its private_data
1317 * field.
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001318 *
1319 * FIX: This really needs to be unified with stdscan.
H. Peter Anvin76690a12002-04-30 20:52:49 +00001320 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001321static int ppscan(void *private_data, struct tokenval *tokval)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001322{
H. Peter Anvin76690a12002-04-30 20:52:49 +00001323 Token **tlineptr = private_data;
1324 Token *tline;
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001325 char ourcopy[MAX_KEYWORD+1], *p, *r, *s;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001326
H. Peter Anvine2c80182005-01-15 22:15:51 +00001327 do {
1328 tline = *tlineptr;
1329 *tlineptr = tline ? tline->next : NULL;
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04001330 } while (tline && (tline->type == TOK_WHITESPACE ||
1331 tline->type == TOK_COMMENT));
H. Peter Anvin76690a12002-04-30 20:52:49 +00001332
1333 if (!tline)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001334 return tokval->t_type = TOKEN_EOS;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001335
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001336 tokval->t_charptr = tline->text;
1337
H. Peter Anvin76690a12002-04-30 20:52:49 +00001338 if (tline->text[0] == '$' && !tline->text[1])
H. Peter Anvine2c80182005-01-15 22:15:51 +00001339 return tokval->t_type = TOKEN_HERE;
H. Peter Anvin7cf897e2002-05-30 21:30:33 +00001340 if (tline->text[0] == '$' && tline->text[1] == '$' && !tline->text[2])
H. Peter Anvine2c80182005-01-15 22:15:51 +00001341 return tokval->t_type = TOKEN_BASE;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001342
H. Peter Anvine2c80182005-01-15 22:15:51 +00001343 if (tline->type == TOK_ID) {
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001344 p = tokval->t_charptr = tline->text;
1345 if (p[0] == '$') {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001346 tokval->t_charptr++;
1347 return tokval->t_type = TOKEN_ID;
1348 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00001349
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001350 for (r = p, s = ourcopy; *r; r++) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001351 if (r >= p+MAX_KEYWORD)
1352 return tokval->t_type = TOKEN_ID; /* Not a keyword */
H. Peter Anvinac8f8fc2008-06-11 15:49:41 -07001353 *s++ = nasm_tolower(*r);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001354 }
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001355 *s = '\0';
1356 /* right, so we have an identifier sitting in temp storage. now,
1357 * is it actually a register or instruction name, or what? */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001358 return nasm_token_hash(ourcopy, tokval);
H. Peter Anvin76690a12002-04-30 20:52:49 +00001359 }
1360
H. Peter Anvine2c80182005-01-15 22:15:51 +00001361 if (tline->type == TOK_NUMBER) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001362 bool rn_error;
1363 tokval->t_integer = readnum(tline->text, &rn_error);
1364 tokval->t_charptr = tline->text;
1365 if (rn_error)
1366 return tokval->t_type = TOKEN_ERRNUM;
1367 else
1368 return tokval->t_type = TOKEN_NUM;
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001369 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00001370
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001371 if (tline->type == TOK_FLOAT) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001372 return tokval->t_type = TOKEN_FLOAT;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001373 }
1374
H. Peter Anvine2c80182005-01-15 22:15:51 +00001375 if (tline->type == TOK_STRING) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001376 char bq, *ep;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001377
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001378 bq = tline->text[0];
H. Peter Anvin11627042008-06-09 20:45:19 -07001379 tokval->t_charptr = tline->text;
1380 tokval->t_inttwo = nasm_unquote(tline->text, &ep);
H. Peter Anvind2456592008-06-19 15:04:18 -07001381
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001382 if (ep[0] != bq || ep[1] != '\0')
1383 return tokval->t_type = TOKEN_ERRSTR;
1384 else
1385 return tokval->t_type = TOKEN_STR;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001386 }
1387
H. Peter Anvine2c80182005-01-15 22:15:51 +00001388 if (tline->type == TOK_OTHER) {
1389 if (!strcmp(tline->text, "<<"))
1390 return tokval->t_type = TOKEN_SHL;
1391 if (!strcmp(tline->text, ">>"))
1392 return tokval->t_type = TOKEN_SHR;
1393 if (!strcmp(tline->text, "//"))
1394 return tokval->t_type = TOKEN_SDIV;
1395 if (!strcmp(tline->text, "%%"))
1396 return tokval->t_type = TOKEN_SMOD;
1397 if (!strcmp(tline->text, "=="))
1398 return tokval->t_type = TOKEN_EQ;
1399 if (!strcmp(tline->text, "<>"))
1400 return tokval->t_type = TOKEN_NE;
1401 if (!strcmp(tline->text, "!="))
1402 return tokval->t_type = TOKEN_NE;
1403 if (!strcmp(tline->text, "<="))
1404 return tokval->t_type = TOKEN_LE;
1405 if (!strcmp(tline->text, ">="))
1406 return tokval->t_type = TOKEN_GE;
1407 if (!strcmp(tline->text, "&&"))
1408 return tokval->t_type = TOKEN_DBL_AND;
1409 if (!strcmp(tline->text, "^^"))
1410 return tokval->t_type = TOKEN_DBL_XOR;
1411 if (!strcmp(tline->text, "||"))
1412 return tokval->t_type = TOKEN_DBL_OR;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001413 }
1414
1415 /*
1416 * We have no other options: just return the first character of
1417 * the token text.
1418 */
1419 return tokval->t_type = tline->text[0];
1420}
1421
1422/*
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001423 * Compare a string to the name of an existing macro; this is a
1424 * simple wrapper which calls either strcmp or nasm_stricmp
1425 * depending on the value of the `casesense' parameter.
1426 */
H. Peter Anvin4db5a162007-10-11 13:42:09 -07001427static int mstrcmp(const char *p, const char *q, bool casesense)
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001428{
H. Peter Anvin734b1882002-04-30 21:01:08 +00001429 return casesense ? strcmp(p, q) : nasm_stricmp(p, q);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001430}
1431
1432/*
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07001433 * Compare a string to the name of an existing macro; this is a
1434 * simple wrapper which calls either strcmp or nasm_stricmp
1435 * depending on the value of the `casesense' parameter.
1436 */
1437static int mmemcmp(const char *p, const char *q, size_t l, bool casesense)
1438{
1439 return casesense ? memcmp(p, q, l) : nasm_memicmp(p, q, l);
1440}
1441
1442/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001443 * Return the Context structure associated with a %$ token. Return
1444 * NULL, having _already_ reported an error condition, if the
1445 * context stack isn't deep enough for the supplied number of $
1446 * signs.
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001447 *
1448 * If "namep" is non-NULL, set it to the pointer to the macro name
1449 * tail, i.e. the part beyond %$...
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001450 */
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04001451static Context *get_ctx(const char *name, const char **namep)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001452{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001453 Context *ctx;
1454 int i;
1455
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001456 if (namep)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001457 *namep = name;
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001458
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001459 if (!name || name[0] != '%' || name[1] != '$')
H. Peter Anvine2c80182005-01-15 22:15:51 +00001460 return NULL;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001461
H. Peter Anvine2c80182005-01-15 22:15:51 +00001462 if (!cstk) {
1463 error(ERR_NONFATAL, "`%s': context stack is empty", name);
1464 return NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001465 }
1466
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001467 name += 2;
1468 ctx = cstk;
1469 i = 0;
1470 while (ctx && *name == '$') {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001471 name++;
1472 i++;
1473 ctx = ctx->next;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001474 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001475 if (!ctx) {
1476 error(ERR_NONFATAL, "`%s': context stack is only"
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001477 " %d level%s deep", name, i, (i == 1 ? "" : "s"));
H. Peter Anvine2c80182005-01-15 22:15:51 +00001478 return NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001479 }
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001480
1481 if (namep)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001482 *namep = name;
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001483
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04001484 return ctx;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001485}
1486
1487/*
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001488 * Check to see if a file is already in a string list
1489 */
1490static bool in_list(const StrList *list, const char *str)
1491{
1492 while (list) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001493 if (!strcmp(list->str, str))
1494 return true;
1495 list = list->next;
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001496 }
1497 return false;
1498}
1499
1500/*
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001501 * Open an include file. This routine must always return a valid
1502 * file pointer if it returns - it's responsible for throwing an
1503 * ERR_FATAL and bombing out completely if not. It should also try
1504 * the include path one by one until it finds the file or reaches
1505 * the end of the path.
1506 */
H. Peter Anvin2b1c3b92008-06-06 10:38:46 -07001507static FILE *inc_fopen(const char *file, StrList **dhead, StrList ***dtail,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001508 bool missing_ok)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001509{
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001510 FILE *fp;
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001511 char *prefix = "";
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001512 IncPath *ip = ipath;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001513 int len = strlen(file);
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001514 size_t prefix_len = 0;
1515 StrList *sl;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001516
H. Peter Anvine2c80182005-01-15 22:15:51 +00001517 while (1) {
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001518 sl = nasm_malloc(prefix_len+len+1+sizeof sl->next);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001519 memcpy(sl->str, prefix, prefix_len);
1520 memcpy(sl->str+prefix_len, file, len+1);
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001521 fp = fopen(sl->str, "r");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001522 if (fp && dhead && !in_list(*dhead, sl->str)) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001523 sl->next = NULL;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001524 **dtail = sl;
1525 *dtail = &sl->next;
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001526 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001527 nasm_free(sl);
1528 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001529 if (fp)
1530 return fp;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001531 if (!ip) {
1532 if (!missing_ok)
1533 break;
1534 prefix = NULL;
1535 } else {
1536 prefix = ip->path;
1537 ip = ip->next;
1538 }
1539 if (prefix) {
1540 prefix_len = strlen(prefix);
1541 } else {
1542 /* -MG given and file not found */
1543 if (dhead && !in_list(*dhead, file)) {
1544 sl = nasm_malloc(len+1+sizeof sl->next);
1545 sl->next = NULL;
1546 strcpy(sl->str, file);
1547 **dtail = sl;
1548 *dtail = &sl->next;
1549 }
1550 return NULL;
1551 }
H. Peter Anvineba20a72002-04-30 20:53:55 +00001552 }
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001553
H. Peter Anvin734b1882002-04-30 21:01:08 +00001554 error(ERR_FATAL, "unable to open include file `%s'", file);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001555 return NULL;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001556}
1557
1558/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001559 * Determine if we should warn on defining a single-line macro of
H. Peter Anvinef7468f2002-04-30 20:57:59 +00001560 * name `name', with `nparam' parameters. If nparam is 0 or -1, will
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001561 * return true if _any_ single-line macro of that name is defined.
1562 * Otherwise, will return true if a single-line macro with either
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001563 * `nparam' or no parameters is defined.
1564 *
1565 * If a macro with precisely the right number of parameters is
H. Peter Anvinef7468f2002-04-30 20:57:59 +00001566 * defined, or nparam is -1, the address of the definition structure
1567 * will be returned in `defn'; otherwise NULL will be returned. If `defn'
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001568 * is NULL, no action will be taken regarding its contents, and no
1569 * error will occur.
1570 *
1571 * Note that this is also called with nparam zero to resolve
1572 * `ifdef'.
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001573 *
1574 * If you already know which context macro belongs to, you can pass
1575 * the context pointer as first parameter; if you won't but name begins
1576 * with %$ the context will be automatically computed. If all_contexts
1577 * is true, macro will be searched in outer contexts as well.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001578 */
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07001579static bool
H. Peter Anvinb2a5fda2008-06-19 21:42:42 -07001580smacro_defined(Context * ctx, const char *name, int nparam, SMacro ** defn,
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07001581 bool nocase)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001582{
H. Peter Anvin166c2472008-05-28 12:28:58 -07001583 struct hash_table *smtbl;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001584 SMacro *m;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001585
H. Peter Anvin97a23472007-09-16 17:57:25 -07001586 if (ctx) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001587 smtbl = &ctx->localmac;
H. Peter Anvin97a23472007-09-16 17:57:25 -07001588 } else if (name[0] == '%' && name[1] == '$') {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001589 if (cstk)
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04001590 ctx = get_ctx(name, &name);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001591 if (!ctx)
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001592 return false; /* got to return _something_ */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001593 smtbl = &ctx->localmac;
H. Peter Anvin97a23472007-09-16 17:57:25 -07001594 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001595 smtbl = &smacros;
H. Peter Anvin97a23472007-09-16 17:57:25 -07001596 }
H. Peter Anvin166c2472008-05-28 12:28:58 -07001597 m = (SMacro *) hash_findix(smtbl, name);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001598
H. Peter Anvine2c80182005-01-15 22:15:51 +00001599 while (m) {
1600 if (!mstrcmp(m->name, name, m->casesense && nocase) &&
Charles Crayne192d5b52007-10-18 19:02:42 -07001601 (nparam <= 0 || m->nparam == 0 || nparam == (int) m->nparam)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001602 if (defn) {
Charles Crayne192d5b52007-10-18 19:02:42 -07001603 if (nparam == (int) m->nparam || nparam == -1)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001604 *defn = m;
1605 else
1606 *defn = NULL;
1607 }
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001608 return true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001609 }
1610 m = m->next;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001611 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001612
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001613 return false;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001614}
1615
1616/*
1617 * Count and mark off the parameters in a multi-line macro call.
1618 * This is called both from within the multi-line macro expansion
1619 * code, and also to mark off the default parameters when provided
1620 * in a %macro definition line.
1621 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001622static void count_mmac_params(Token * t, int *nparam, Token *** params)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001623{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001624 int paramsize, brace;
1625
1626 *nparam = paramsize = 0;
1627 *params = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001628 while (t) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001629 /* +1: we need space for the final NULL */
H. Peter Anvin91fb6f12008-09-01 10:56:33 -07001630 if (*nparam+1 >= paramsize) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001631 paramsize += PARAM_DELTA;
1632 *params = nasm_realloc(*params, sizeof(**params) * paramsize);
1633 }
1634 skip_white_(t);
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001635 brace = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001636 if (tok_is_(t, "{"))
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001637 brace = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001638 (*params)[(*nparam)++] = t;
1639 while (tok_isnt_(t, brace ? "}" : ","))
1640 t = t->next;
1641 if (t) { /* got a comma/brace */
1642 t = t->next;
1643 if (brace) {
1644 /*
1645 * Now we've found the closing brace, look further
1646 * for the comma.
1647 */
1648 skip_white_(t);
1649 if (tok_isnt_(t, ",")) {
1650 error(ERR_NONFATAL,
1651 "braces do not enclose all of macro parameter");
1652 while (tok_isnt_(t, ","))
1653 t = t->next;
1654 }
1655 if (t)
1656 t = t->next; /* eat the comma */
1657 }
1658 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001659 }
1660}
1661
1662/*
H. Peter Anvin76690a12002-04-30 20:52:49 +00001663 * Determine whether one of the various `if' conditions is true or
1664 * not.
1665 *
1666 * We must free the tline we get passed.
1667 */
H. Peter Anvin70055962007-10-11 00:05:31 -07001668static bool if_condition(Token * tline, enum preproc_token ct)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001669{
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001670 enum pp_conditional i = PP_COND(ct);
H. Peter Anvin70055962007-10-11 00:05:31 -07001671 bool j;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001672 Token *t, *tt, **tptr, *origline;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001673 struct tokenval tokval;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001674 expr *evalresult;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001675 enum pp_token_type needtype;
H. Peter Anvin077fb932010-07-20 14:56:30 -07001676 char *p;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001677
1678 origline = tline;
1679
H. Peter Anvine2c80182005-01-15 22:15:51 +00001680 switch (i) {
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001681 case PPC_IFCTX:
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001682 j = false; /* have we matched yet? */
Victor van den Elzen0e857f12008-07-23 13:21:29 +02001683 while (true) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001684 skip_white_(tline);
Victor van den Elzen0e857f12008-07-23 13:21:29 +02001685 if (!tline)
1686 break;
1687 if (tline->type != TOK_ID) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001688 error(ERR_NONFATAL,
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001689 "`%s' expects context identifiers", pp_directives[ct]);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001690 free_tlist(origline);
1691 return -1;
1692 }
Victor van den Elzen0e857f12008-07-23 13:21:29 +02001693 if (cstk && cstk->name && !nasm_stricmp(tline->text, cstk->name))
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001694 j = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001695 tline = tline->next;
1696 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001697 break;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001698
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001699 case PPC_IFDEF:
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001700 j = false; /* have we matched yet? */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001701 while (tline) {
1702 skip_white_(tline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001703 if (!tline || (tline->type != TOK_ID &&
1704 (tline->type != TOK_PREPROC_ID ||
1705 tline->text[1] != '$'))) {
1706 error(ERR_NONFATAL,
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001707 "`%s' expects macro identifiers", pp_directives[ct]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001708 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001709 }
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07001710 if (smacro_defined(NULL, tline->text, 0, NULL, true))
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001711 j = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001712 tline = tline->next;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001713 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001714 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001715
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001716 case PPC_IFENV:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001717 tline = expand_smacro(tline);
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001718 j = false; /* have we matched yet? */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001719 while (tline) {
1720 skip_white_(tline);
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001721 if (!tline || (tline->type != TOK_ID &&
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001722 tline->type != TOK_STRING &&
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001723 (tline->type != TOK_PREPROC_ID ||
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001724 tline->text[1] != '!'))) {
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001725 error(ERR_NONFATAL,
1726 "`%s' expects environment variable names",
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001727 pp_directives[ct]);
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001728 goto fail;
1729 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001730 p = tline->text;
1731 if (tline->type == TOK_PREPROC_ID)
1732 p += 2; /* Skip leading %! */
1733 if (*p == '\'' || *p == '\"' || *p == '`')
1734 nasm_unquote_cstr(p, ct);
1735 if (getenv(p))
1736 j = true;
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001737 tline = tline->next;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001738 }
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001739 break;
1740
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001741 case PPC_IFIDN:
1742 case PPC_IFIDNI:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001743 tline = expand_smacro(tline);
1744 t = tt = tline;
1745 while (tok_isnt_(tt, ","))
1746 tt = tt->next;
1747 if (!tt) {
1748 error(ERR_NONFATAL,
1749 "`%s' expects two comma-separated arguments",
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001750 pp_directives[ct]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001751 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001752 }
1753 tt = tt->next;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001754 j = true; /* assume equality unless proved not */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001755 while ((t->type != TOK_OTHER || strcmp(t->text, ",")) && tt) {
1756 if (tt->type == TOK_OTHER && !strcmp(tt->text, ",")) {
1757 error(ERR_NONFATAL, "`%s': more than one comma on line",
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001758 pp_directives[ct]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001759 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001760 }
1761 if (t->type == TOK_WHITESPACE) {
1762 t = t->next;
1763 continue;
1764 }
1765 if (tt->type == TOK_WHITESPACE) {
1766 tt = tt->next;
1767 continue;
1768 }
1769 if (tt->type != t->type) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001770 j = false; /* found mismatching tokens */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001771 break;
1772 }
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07001773 /* When comparing strings, need to unquote them first */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001774 if (t->type == TOK_STRING) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001775 size_t l1 = nasm_unquote(t->text, NULL);
1776 size_t l2 = nasm_unquote(tt->text, NULL);
H. Peter Anvind2456592008-06-19 15:04:18 -07001777
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001778 if (l1 != l2) {
1779 j = false;
1780 break;
1781 }
1782 if (mmemcmp(t->text, tt->text, l1, i == PPC_IFIDN)) {
1783 j = false;
1784 break;
1785 }
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07001786 } else if (mstrcmp(tt->text, t->text, i == PPC_IFIDN) != 0) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001787 j = false; /* found mismatching tokens */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001788 break;
1789 }
Nickolay Yurchenkof3b3ce22003-09-21 20:38:43 +00001790
H. Peter Anvine2c80182005-01-15 22:15:51 +00001791 t = t->next;
1792 tt = tt->next;
1793 }
1794 if ((t->type != TOK_OTHER || strcmp(t->text, ",")) || tt)
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001795 j = false; /* trailing gunk on one end or other */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001796 break;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001797
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001798 case PPC_IFMACRO:
H. Peter Anvin89cee572009-07-15 09:16:54 -04001799 {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001800 bool found = false;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001801 MMacro searching, *mmac;
H. Peter Anvin65747262002-05-07 00:10:05 +00001802
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001803 skip_white_(tline);
1804 tline = expand_id(tline);
1805 if (!tok_type_(tline, TOK_ID)) {
1806 error(ERR_NONFATAL,
1807 "`%s' expects a macro name", pp_directives[ct]);
1808 goto fail;
1809 }
1810 searching.name = nasm_strdup(tline->text);
1811 searching.casesense = true;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001812 searching.plus = false;
1813 searching.nolist = false;
1814 searching.in_progress = 0;
1815 searching.max_depth = 0;
1816 searching.rep_nest = NULL;
1817 searching.nparam_min = 0;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001818 searching.nparam_max = INT_MAX;
1819 tline = expand_smacro(tline->next);
1820 skip_white_(tline);
1821 if (!tline) {
1822 } else if (!tok_type_(tline, TOK_NUMBER)) {
1823 error(ERR_NONFATAL,
1824 "`%s' expects a parameter count or nothing",
1825 pp_directives[ct]);
1826 } else {
1827 searching.nparam_min = searching.nparam_max =
1828 readnum(tline->text, &j);
1829 if (j)
1830 error(ERR_NONFATAL,
1831 "unable to parse parameter count `%s'",
1832 tline->text);
1833 }
1834 if (tline && tok_is_(tline->next, "-")) {
1835 tline = tline->next->next;
1836 if (tok_is_(tline, "*"))
1837 searching.nparam_max = INT_MAX;
1838 else if (!tok_type_(tline, TOK_NUMBER))
1839 error(ERR_NONFATAL,
1840 "`%s' expects a parameter count after `-'",
1841 pp_directives[ct]);
1842 else {
1843 searching.nparam_max = readnum(tline->text, &j);
1844 if (j)
1845 error(ERR_NONFATAL,
1846 "unable to parse parameter count `%s'",
1847 tline->text);
1848 if (searching.nparam_min > searching.nparam_max)
1849 error(ERR_NONFATAL,
1850 "minimum parameter count exceeds maximum");
1851 }
1852 }
1853 if (tline && tok_is_(tline->next, "+")) {
1854 tline = tline->next;
1855 searching.plus = true;
1856 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001857 mmac = (MMacro *) hash_findix(&mmacros, searching.name);
1858 while (mmac) {
1859 if (!strcmp(mmac->name, searching.name) &&
1860 (mmac->nparam_min <= searching.nparam_max
1861 || searching.plus)
1862 && (searching.nparam_min <= mmac->nparam_max
1863 || mmac->plus)) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001864 found = true;
1865 break;
1866 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001867 mmac = mmac->next;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001868 }
1869 if (tline && tline->next)
1870 error(ERR_WARNING|ERR_PASS1,
1871 "trailing garbage after %%ifmacro ignored");
1872 nasm_free(searching.name);
1873 j = found;
1874 break;
H. Peter Anvin89cee572009-07-15 09:16:54 -04001875 }
H. Peter Anvin65747262002-05-07 00:10:05 +00001876
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001877 case PPC_IFID:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001878 needtype = TOK_ID;
1879 goto iftype;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001880 case PPC_IFNUM:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001881 needtype = TOK_NUMBER;
1882 goto iftype;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001883 case PPC_IFSTR:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001884 needtype = TOK_STRING;
1885 goto iftype;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001886
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001887iftype:
1888 t = tline = expand_smacro(tline);
H. Peter Anvind85d2502008-05-04 17:53:31 -07001889
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001890 while (tok_type_(t, TOK_WHITESPACE) ||
1891 (needtype == TOK_NUMBER &&
1892 tok_type_(t, TOK_OTHER) &&
1893 (t->text[0] == '-' || t->text[0] == '+') &&
1894 !t->text[1]))
1895 t = t->next;
H. Peter Anvind85d2502008-05-04 17:53:31 -07001896
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001897 j = tok_type_(t, needtype);
1898 break;
H. Peter Anvincbf768d2008-02-16 16:41:25 -08001899
1900 case PPC_IFTOKEN:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001901 t = tline = expand_smacro(tline);
1902 while (tok_type_(t, TOK_WHITESPACE))
1903 t = t->next;
H. Peter Anvincbf768d2008-02-16 16:41:25 -08001904
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001905 j = false;
1906 if (t) {
1907 t = t->next; /* Skip the actual token */
1908 while (tok_type_(t, TOK_WHITESPACE))
1909 t = t->next;
1910 j = !t; /* Should be nothing left */
1911 }
1912 break;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001913
H. Peter Anvin134b9462008-02-16 17:01:40 -08001914 case PPC_IFEMPTY:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001915 t = tline = expand_smacro(tline);
1916 while (tok_type_(t, TOK_WHITESPACE))
1917 t = t->next;
H. Peter Anvin134b9462008-02-16 17:01:40 -08001918
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001919 j = !t; /* Should be empty */
1920 break;
H. Peter Anvin134b9462008-02-16 17:01:40 -08001921
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001922 case PPC_IF:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001923 t = tline = expand_smacro(tline);
1924 tptr = &t;
1925 tokval.t_type = TOKEN_INVALID;
1926 evalresult = evaluate(ppscan, tptr, &tokval,
1927 NULL, pass | CRITICAL, error, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001928 if (!evalresult)
1929 return -1;
1930 if (tokval.t_type)
H. Peter Anvin917a3492008-09-24 09:14:49 -07001931 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +00001932 "trailing garbage after expression ignored");
1933 if (!is_simple(evalresult)) {
1934 error(ERR_NONFATAL,
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001935 "non-constant value given to `%s'", pp_directives[ct]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001936 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001937 }
Chuck Crayne60ae75d2007-05-02 01:59:16 +00001938 j = reloc_value(evalresult) != 0;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001939 break;
H. Peter Anvin95e28822007-09-12 04:20:08 +00001940
H. Peter Anvine2c80182005-01-15 22:15:51 +00001941 default:
1942 error(ERR_FATAL,
1943 "preprocessor directive `%s' not yet implemented",
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001944 pp_directives[ct]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001945 goto fail;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001946 }
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001947
1948 free_tlist(origline);
1949 return j ^ PP_NEGATIVE(ct);
H. Peter Anvin70653092007-10-19 14:42:29 -07001950
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001951fail:
1952 free_tlist(origline);
1953 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001954}
1955
1956/*
H. Peter Anvin4db5a162007-10-11 13:42:09 -07001957 * Common code for defining an smacro
1958 */
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001959static bool define_smacro(Context *ctx, const char *mname, bool casesense,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001960 int nparam, Token *expansion)
H. Peter Anvin4db5a162007-10-11 13:42:09 -07001961{
1962 SMacro *smac, **smhead;
H. Peter Anvin166c2472008-05-28 12:28:58 -07001963 struct hash_table *smtbl;
H. Peter Anvin70653092007-10-19 14:42:29 -07001964
H. Peter Anvin4db5a162007-10-11 13:42:09 -07001965 if (smacro_defined(ctx, mname, nparam, &smac, casesense)) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001966 if (!smac) {
1967 error(ERR_WARNING|ERR_PASS1,
1968 "single-line macro `%s' defined both with and"
1969 " without parameters", mname);
1970 /*
1971 * Some instances of the old code considered this a failure,
1972 * some others didn't. What is the right thing to do here?
1973 */
1974 free_tlist(expansion);
1975 return false; /* Failure */
1976 } else {
1977 /*
1978 * We're redefining, so we have to take over an
1979 * existing SMacro structure. This means freeing
1980 * what was already in it.
1981 */
1982 nasm_free(smac->name);
1983 free_tlist(smac->expansion);
1984 }
H. Peter Anvin4db5a162007-10-11 13:42:09 -07001985 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001986 smtbl = ctx ? &ctx->localmac : &smacros;
1987 smhead = (SMacro **) hash_findi_add(smtbl, mname);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001988 smac = nasm_malloc(sizeof(SMacro));
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001989 smac->next = *smhead;
1990 *smhead = smac;
H. Peter Anvin4db5a162007-10-11 13:42:09 -07001991 }
1992 smac->name = nasm_strdup(mname);
1993 smac->casesense = casesense;
1994 smac->nparam = nparam;
1995 smac->expansion = expansion;
1996 smac->in_progress = false;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001997 return true; /* Success */
H. Peter Anvin4db5a162007-10-11 13:42:09 -07001998}
1999
2000/*
2001 * Undefine an smacro
2002 */
2003static void undef_smacro(Context *ctx, const char *mname)
2004{
2005 SMacro **smhead, *s, **sp;
H. Peter Anvin166c2472008-05-28 12:28:58 -07002006 struct hash_table *smtbl;
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002007
H. Peter Anvin166c2472008-05-28 12:28:58 -07002008 smtbl = ctx ? &ctx->localmac : &smacros;
2009 smhead = (SMacro **)hash_findi(smtbl, mname, NULL);
H. Peter Anvin70653092007-10-19 14:42:29 -07002010
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002011 if (smhead) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002012 /*
2013 * We now have a macro name... go hunt for it.
2014 */
2015 sp = smhead;
2016 while ((s = *sp) != NULL) {
2017 if (!mstrcmp(s->name, mname, s->casesense)) {
2018 *sp = s->next;
2019 nasm_free(s->name);
2020 free_tlist(s->expansion);
2021 nasm_free(s);
2022 } else {
2023 sp = &s->next;
2024 }
2025 }
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002026 }
2027}
2028
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002029/*
H. Peter Anvina26433d2008-07-16 14:40:01 -07002030 * Parse a mmacro specification.
2031 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002032static bool parse_mmacro_spec(Token *tline, MMacro *def, const char *directive)
H. Peter Anvina26433d2008-07-16 14:40:01 -07002033{
2034 bool err;
2035
2036 tline = tline->next;
2037 skip_white_(tline);
2038 tline = expand_id(tline);
2039 if (!tok_type_(tline, TOK_ID)) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002040 error(ERR_NONFATAL, "`%s' expects a macro name", directive);
2041 return false;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002042 }
Victor van den Elzenb916ede2008-07-23 15:14:22 +02002043
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002044 def->prev = NULL;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002045 def->name = nasm_strdup(tline->text);
2046 def->plus = false;
2047 def->nolist = false;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002048 def->in_progress = 0;
2049 def->rep_nest = NULL;
Victor van den Elzenb916ede2008-07-23 15:14:22 +02002050 def->nparam_min = 0;
2051 def->nparam_max = 0;
2052
H. Peter Anvina26433d2008-07-16 14:40:01 -07002053 tline = expand_smacro(tline->next);
2054 skip_white_(tline);
2055 if (!tok_type_(tline, TOK_NUMBER)) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002056 error(ERR_NONFATAL, "`%s' expects a parameter count", directive);
H. Peter Anvina26433d2008-07-16 14:40:01 -07002057 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002058 def->nparam_min = def->nparam_max =
2059 readnum(tline->text, &err);
2060 if (err)
2061 error(ERR_NONFATAL,
2062 "unable to parse parameter count `%s'", tline->text);
H. Peter Anvina26433d2008-07-16 14:40:01 -07002063 }
2064 if (tline && tok_is_(tline->next, "-")) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002065 tline = tline->next->next;
2066 if (tok_is_(tline, "*")) {
2067 def->nparam_max = INT_MAX;
2068 } else if (!tok_type_(tline, TOK_NUMBER)) {
2069 error(ERR_NONFATAL,
2070 "`%s' expects a parameter count after `-'", directive);
2071 } else {
2072 def->nparam_max = readnum(tline->text, &err);
2073 if (err) {
2074 error(ERR_NONFATAL, "unable to parse parameter count `%s'",
2075 tline->text);
2076 }
2077 if (def->nparam_min > def->nparam_max) {
2078 error(ERR_NONFATAL, "minimum parameter count exceeds maximum");
2079 }
2080 }
H. Peter Anvina26433d2008-07-16 14:40:01 -07002081 }
2082 if (tline && tok_is_(tline->next, "+")) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002083 tline = tline->next;
2084 def->plus = true;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002085 }
2086 if (tline && tok_type_(tline->next, TOK_ID) &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002087 !nasm_stricmp(tline->next->text, ".nolist")) {
2088 tline = tline->next;
2089 def->nolist = true;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002090 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002091
H. Peter Anvina26433d2008-07-16 14:40:01 -07002092 /*
2093 * Handle default parameters.
2094 */
2095 if (tline && tline->next) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002096 def->dlist = tline->next;
2097 tline->next = NULL;
2098 count_mmac_params(def->dlist, &def->ndefs, &def->defaults);
H. Peter Anvina26433d2008-07-16 14:40:01 -07002099 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002100 def->dlist = NULL;
2101 def->defaults = NULL;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002102 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002103 def->expansion = NULL;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002104
H. Peter Anvin89cee572009-07-15 09:16:54 -04002105 if (def->defaults && def->ndefs > def->nparam_max - def->nparam_min &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002106 !def->plus)
2107 error(ERR_WARNING|ERR_PASS1|ERR_WARN_MDP,
2108 "too many default macro parameters");
Victor van den Elzenb916ede2008-07-23 15:14:22 +02002109
H. Peter Anvina26433d2008-07-16 14:40:01 -07002110 return true;
2111}
2112
2113
2114/*
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002115 * Decode a size directive
2116 */
2117static int parse_size(const char *str) {
2118 static const char *size_names[] =
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002119 { "byte", "dword", "oword", "qword", "tword", "word", "yword" };
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002120 static const int sizes[] =
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002121 { 0, 1, 4, 16, 8, 10, 2, 32 };
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002122
Cyrill Gorcunova7319242010-06-03 22:04:36 +04002123 return sizes[bsii(str, size_names, ARRAY_SIZE(size_names))+1];
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002124}
2125
Ed Beroset3ab3f412002-06-11 03:31:49 +00002126/**
2127 * find and process preprocessor directive in passed line
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002128 * Find out if a line contains a preprocessor directive, and deal
2129 * with it if so.
H. Peter Anvin70653092007-10-19 14:42:29 -07002130 *
Ed Beroset3ab3f412002-06-11 03:31:49 +00002131 * If a directive _is_ found, it is the responsibility of this routine
2132 * (and not the caller) to free_tlist() the line.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002133 *
Ed Beroset3ab3f412002-06-11 03:31:49 +00002134 * @param tline a pointer to the current tokeninzed line linked list
2135 * @return DIRECTIVE_FOUND or NO_DIRECTIVE_FOUND
H. Peter Anvin70653092007-10-19 14:42:29 -07002136 *
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002137 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002138static int do_directive(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00002139{
H. Peter Anvin4169a472007-09-12 01:29:43 +00002140 enum preproc_token i;
2141 int j;
H. Peter Anvin70055962007-10-11 00:05:31 -07002142 bool err;
2143 int nparam;
2144 bool nolist;
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07002145 bool casesense;
H. Peter Anvin8cfdb9d2007-09-14 18:36:01 -07002146 int k, m;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002147 int offset;
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08002148 char *p, *pp;
2149 const char *mname;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002150 Include *inc;
2151 Context *ctx;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002152 Cond *cond;
2153 MMacro *mmac, **mmhead;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002154 Token *t, *tt, *param_start, *macro_start, *last, **tptr, *origline;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002155 Line *l;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002156 struct tokenval tokval;
2157 expr *evalresult;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002158 MMacro *tmp_defining; /* Used when manipulating rep_nest */
H. Peter Anvinf8ba53e2007-10-11 10:11:57 -07002159 int64_t count;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07002160 size_t len;
H. Peter Anvin8e3f75e2008-09-24 00:21:58 -07002161 int severity;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002162
2163 origline = tline;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002164
H. Peter Anvineba20a72002-04-30 20:53:55 +00002165 skip_white_(tline);
H. Peter Anvinf2936d72008-06-04 15:11:23 -07002166 if (!tline || !tok_type_(tline, TOK_PREPROC_ID) ||
H. Peter Anvine2c80182005-01-15 22:15:51 +00002167 (tline->text[1] == '%' || tline->text[1] == '$'
2168 || tline->text[1] == '!'))
2169 return NO_DIRECTIVE_FOUND;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002170
H. Peter Anvin4169a472007-09-12 01:29:43 +00002171 i = pp_token_hash(tline->text);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002172
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002173 /*
2174 * FIXME: We zap execution of PP_RMACRO, PP_IRMACRO, PP_EXITMACRO
2175 * since they are known to be buggy at moment, we need to fix them
2176 * in future release (2.09-2.10)
2177 */
2178 if (i == PP_RMACRO || i == PP_RMACRO || i == PP_EXITMACRO) {
2179 error(ERR_NONFATAL, "unknown preprocessor directive `%s'",
2180 tline->text);
2181 return NO_DIRECTIVE_FOUND;
2182 }
2183
2184 /*
2185 * If we're in a non-emitting branch of a condition construct,
2186 * or walking to the end of an already terminated %rep block,
2187 * we should ignore all directives except for condition
2188 * directives.
2189 */
2190 if (((istk->conds && !emitting(istk->conds->state)) ||
2191 (istk->mstk && !istk->mstk->in_progress)) && !is_condition(i)) {
2192 return NO_DIRECTIVE_FOUND;
2193 }
2194
2195 /*
2196 * If we're defining a macro or reading a %rep block, we should
2197 * ignore all directives except for %macro/%imacro (which nest),
2198 * %endm/%endmacro, and (only if we're in a %rep block) %endrep.
2199 * If we're in a %rep block, another %rep nests, so should be let through.
2200 */
2201 if (defining && i != PP_MACRO && i != PP_IMACRO &&
2202 i != PP_RMACRO && i != PP_IRMACRO &&
2203 i != PP_ENDMACRO && i != PP_ENDM &&
2204 (defining->name || (i != PP_ENDREP && i != PP_REP))) {
2205 return NO_DIRECTIVE_FOUND;
2206 }
2207
2208 if (defining) {
2209 if (i == PP_MACRO || i == PP_IMACRO ||
2210 i == PP_RMACRO || i == PP_IRMACRO) {
2211 nested_mac_count++;
2212 return NO_DIRECTIVE_FOUND;
2213 } else if (nested_mac_count > 0) {
2214 if (i == PP_ENDMACRO) {
2215 nested_mac_count--;
2216 return NO_DIRECTIVE_FOUND;
2217 }
2218 }
2219 if (!defining->name) {
2220 if (i == PP_REP) {
2221 nested_rep_count++;
2222 return NO_DIRECTIVE_FOUND;
2223 } else if (nested_rep_count > 0) {
2224 if (i == PP_ENDREP) {
2225 nested_rep_count--;
2226 return NO_DIRECTIVE_FOUND;
2227 }
2228 }
2229 }
2230 }
2231
H. Peter Anvin4169a472007-09-12 01:29:43 +00002232 switch (i) {
2233 case PP_INVALID:
H. Peter Anvine2c80182005-01-15 22:15:51 +00002234 error(ERR_NONFATAL, "unknown preprocessor directive `%s'",
2235 tline->text);
2236 return NO_DIRECTIVE_FOUND; /* didn't get it */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002237
H. Peter Anvine2c80182005-01-15 22:15:51 +00002238 case PP_STACKSIZE:
2239 /* Directive to tell NASM what the default stack size is. The
2240 * default is for a 16-bit stack, and this can be overriden with
2241 * %stacksize large.
H. Peter Anvine2c80182005-01-15 22:15:51 +00002242 */
2243 tline = tline->next;
2244 if (tline && tline->type == TOK_WHITESPACE)
2245 tline = tline->next;
2246 if (!tline || tline->type != TOK_ID) {
2247 error(ERR_NONFATAL, "`%%stacksize' missing size parameter");
2248 free_tlist(origline);
2249 return DIRECTIVE_FOUND;
2250 }
2251 if (nasm_stricmp(tline->text, "flat") == 0) {
2252 /* All subsequent ARG directives are for a 32-bit stack */
2253 StackSize = 4;
2254 StackPointer = "ebp";
2255 ArgOffset = 8;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002256 LocalOffset = 0;
Charles Crayne7eaf9192007-11-08 22:11:14 -08002257 } else if (nasm_stricmp(tline->text, "flat64") == 0) {
2258 /* All subsequent ARG directives are for a 64-bit stack */
2259 StackSize = 8;
2260 StackPointer = "rbp";
Per Jessen53252e02010-02-11 00:16:59 +03002261 ArgOffset = 16;
Charles Crayne7eaf9192007-11-08 22:11:14 -08002262 LocalOffset = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002263 } else if (nasm_stricmp(tline->text, "large") == 0) {
2264 /* All subsequent ARG directives are for a 16-bit stack,
2265 * far function call.
2266 */
2267 StackSize = 2;
2268 StackPointer = "bp";
2269 ArgOffset = 4;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002270 LocalOffset = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002271 } else if (nasm_stricmp(tline->text, "small") == 0) {
2272 /* All subsequent ARG directives are for a 16-bit stack,
2273 * far function call. We don't support near functions.
2274 */
2275 StackSize = 2;
2276 StackPointer = "bp";
2277 ArgOffset = 6;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002278 LocalOffset = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002279 } else {
2280 error(ERR_NONFATAL, "`%%stacksize' invalid size type");
2281 free_tlist(origline);
2282 return DIRECTIVE_FOUND;
2283 }
2284 free_tlist(origline);
2285 return DIRECTIVE_FOUND;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002286
H. Peter Anvine2c80182005-01-15 22:15:51 +00002287 case PP_ARG:
2288 /* TASM like ARG directive to define arguments to functions, in
2289 * the following form:
2290 *
2291 * ARG arg1:WORD, arg2:DWORD, arg4:QWORD
2292 */
2293 offset = ArgOffset;
2294 do {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00002295 char *arg, directive[256];
H. Peter Anvine2c80182005-01-15 22:15:51 +00002296 int size = StackSize;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002297
H. Peter Anvine2c80182005-01-15 22:15:51 +00002298 /* Find the argument name */
2299 tline = tline->next;
2300 if (tline && tline->type == TOK_WHITESPACE)
2301 tline = tline->next;
2302 if (!tline || tline->type != TOK_ID) {
2303 error(ERR_NONFATAL, "`%%arg' missing argument parameter");
2304 free_tlist(origline);
2305 return DIRECTIVE_FOUND;
2306 }
2307 arg = tline->text;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002308
H. Peter Anvine2c80182005-01-15 22:15:51 +00002309 /* Find the argument size type */
2310 tline = tline->next;
2311 if (!tline || tline->type != TOK_OTHER
2312 || tline->text[0] != ':') {
2313 error(ERR_NONFATAL,
2314 "Syntax error processing `%%arg' directive");
2315 free_tlist(origline);
2316 return DIRECTIVE_FOUND;
2317 }
2318 tline = tline->next;
2319 if (!tline || tline->type != TOK_ID) {
2320 error(ERR_NONFATAL, "`%%arg' missing size type parameter");
2321 free_tlist(origline);
2322 return DIRECTIVE_FOUND;
2323 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002324
H. Peter Anvine2c80182005-01-15 22:15:51 +00002325 /* Allow macro expansion of type parameter */
Keith Kaniosb7a89542007-04-12 02:40:54 +00002326 tt = tokenize(tline->text);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002327 tt = expand_smacro(tt);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002328 size = parse_size(tt->text);
2329 if (!size) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002330 error(ERR_NONFATAL,
2331 "Invalid size type for `%%arg' missing directive");
2332 free_tlist(tt);
2333 free_tlist(origline);
2334 return DIRECTIVE_FOUND;
2335 }
2336 free_tlist(tt);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002337
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002338 /* Round up to even stack slots */
2339 size = ALIGN(size, StackSize);
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002340
H. Peter Anvine2c80182005-01-15 22:15:51 +00002341 /* Now define the macro for the argument */
2342 snprintf(directive, sizeof(directive), "%%define %s (%s+%d)",
2343 arg, StackPointer, offset);
Keith Kaniosb7a89542007-04-12 02:40:54 +00002344 do_directive(tokenize(directive));
H. Peter Anvine2c80182005-01-15 22:15:51 +00002345 offset += size;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002346
H. Peter Anvine2c80182005-01-15 22:15:51 +00002347 /* Move to the next argument in the list */
2348 tline = tline->next;
2349 if (tline && tline->type == TOK_WHITESPACE)
2350 tline = tline->next;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002351 } while (tline && tline->type == TOK_OTHER && tline->text[0] == ',');
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002352 ArgOffset = offset;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002353 free_tlist(origline);
2354 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002355
H. Peter Anvine2c80182005-01-15 22:15:51 +00002356 case PP_LOCAL:
2357 /* TASM like LOCAL directive to define local variables for a
2358 * function, in the following form:
2359 *
2360 * LOCAL local1:WORD, local2:DWORD, local4:QWORD = LocalSize
2361 *
2362 * The '= LocalSize' at the end is ignored by NASM, but is
2363 * required by TASM to define the local parameter size (and used
2364 * by the TASM macro package).
2365 */
2366 offset = LocalOffset;
2367 do {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00002368 char *local, directive[256];
H. Peter Anvine2c80182005-01-15 22:15:51 +00002369 int size = StackSize;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002370
H. Peter Anvine2c80182005-01-15 22:15:51 +00002371 /* Find the argument name */
2372 tline = tline->next;
2373 if (tline && tline->type == TOK_WHITESPACE)
2374 tline = tline->next;
2375 if (!tline || tline->type != TOK_ID) {
2376 error(ERR_NONFATAL,
2377 "`%%local' missing argument parameter");
2378 free_tlist(origline);
2379 return DIRECTIVE_FOUND;
2380 }
2381 local = tline->text;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002382
H. Peter Anvine2c80182005-01-15 22:15:51 +00002383 /* Find the argument size type */
2384 tline = tline->next;
2385 if (!tline || tline->type != TOK_OTHER
2386 || tline->text[0] != ':') {
2387 error(ERR_NONFATAL,
2388 "Syntax error processing `%%local' directive");
2389 free_tlist(origline);
2390 return DIRECTIVE_FOUND;
2391 }
2392 tline = tline->next;
2393 if (!tline || tline->type != TOK_ID) {
2394 error(ERR_NONFATAL,
2395 "`%%local' missing size type parameter");
2396 free_tlist(origline);
2397 return DIRECTIVE_FOUND;
2398 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00002399
H. Peter Anvine2c80182005-01-15 22:15:51 +00002400 /* Allow macro expansion of type parameter */
Keith Kaniosb7a89542007-04-12 02:40:54 +00002401 tt = tokenize(tline->text);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002402 tt = expand_smacro(tt);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002403 size = parse_size(tt->text);
2404 if (!size) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002405 error(ERR_NONFATAL,
2406 "Invalid size type for `%%local' missing directive");
2407 free_tlist(tt);
2408 free_tlist(origline);
2409 return DIRECTIVE_FOUND;
2410 }
2411 free_tlist(tt);
H. Peter Anvin734b1882002-04-30 21:01:08 +00002412
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002413 /* Round up to even stack slots */
2414 size = ALIGN(size, StackSize);
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002415
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002416 offset += size; /* Negative offset, increment before */
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002417
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002418 /* Now define the macro for the argument */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002419 snprintf(directive, sizeof(directive), "%%define %s (%s-%d)",
2420 local, StackPointer, offset);
Keith Kaniosb7a89542007-04-12 02:40:54 +00002421 do_directive(tokenize(directive));
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002422
H. Peter Anvine2c80182005-01-15 22:15:51 +00002423 /* Now define the assign to setup the enter_c macro correctly */
2424 snprintf(directive, sizeof(directive),
2425 "%%assign %%$localsize %%$localsize+%d", size);
Keith Kaniosb7a89542007-04-12 02:40:54 +00002426 do_directive(tokenize(directive));
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002427
H. Peter Anvine2c80182005-01-15 22:15:51 +00002428 /* Move to the next argument in the list */
2429 tline = tline->next;
2430 if (tline && tline->type == TOK_WHITESPACE)
2431 tline = tline->next;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002432 } while (tline && tline->type == TOK_OTHER && tline->text[0] == ',');
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002433 LocalOffset = offset;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002434 free_tlist(origline);
2435 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002436
H. Peter Anvine2c80182005-01-15 22:15:51 +00002437 case PP_CLEAR:
2438 if (tline->next)
H. Peter Anvin917a3492008-09-24 09:14:49 -07002439 error(ERR_WARNING|ERR_PASS1,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002440 "trailing garbage after `%%clear' ignored");
2441 free_macros();
2442 init_macros();
H. Peter Anvine2c80182005-01-15 22:15:51 +00002443 free_tlist(origline);
2444 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002445
H. Peter Anvin418ca702008-05-30 10:42:30 -07002446 case PP_DEPEND:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002447 t = tline->next = expand_smacro(tline->next);
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002448 skip_white_(t);
2449 if (!t || (t->type != TOK_STRING &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002450 t->type != TOK_INTERNAL_STRING)) {
H. Peter Anvin418ca702008-05-30 10:42:30 -07002451 error(ERR_NONFATAL, "`%%depend' expects a file name");
2452 free_tlist(origline);
2453 return DIRECTIVE_FOUND; /* but we did _something_ */
2454 }
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002455 if (t->next)
H. Peter Anvin917a3492008-09-24 09:14:49 -07002456 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvin418ca702008-05-30 10:42:30 -07002457 "trailing garbage after `%%depend' ignored");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002458 p = t->text;
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002459 if (t->type != TOK_INTERNAL_STRING)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002460 nasm_unquote_cstr(p, i);
2461 if (dephead && !in_list(*dephead, p)) {
2462 StrList *sl = nasm_malloc(strlen(p)+1+sizeof sl->next);
2463 sl->next = NULL;
2464 strcpy(sl->str, p);
2465 *deptail = sl;
2466 deptail = &sl->next;
2467 }
2468 free_tlist(origline);
H. Peter Anvin418ca702008-05-30 10:42:30 -07002469 return DIRECTIVE_FOUND;
2470
2471 case PP_INCLUDE:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002472 t = tline->next = expand_smacro(tline->next);
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002473 skip_white_(t);
H. Peter Anvind2456592008-06-19 15:04:18 -07002474
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002475 if (!t || (t->type != TOK_STRING &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002476 t->type != TOK_INTERNAL_STRING)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002477 error(ERR_NONFATAL, "`%%include' expects a file name");
2478 free_tlist(origline);
2479 return DIRECTIVE_FOUND; /* but we did _something_ */
2480 }
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002481 if (t->next)
H. Peter Anvin917a3492008-09-24 09:14:49 -07002482 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +00002483 "trailing garbage after `%%include' ignored");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002484 p = t->text;
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002485 if (t->type != TOK_INTERNAL_STRING)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002486 nasm_unquote_cstr(p, i);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002487 inc = nasm_malloc(sizeof(Include));
H. Peter Anvine2c80182005-01-15 22:15:51 +00002488 inc->next = istk;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002489 inc->conds = NULL;
H. Peter Anvin2b1c3b92008-06-06 10:38:46 -07002490 inc->fp = inc_fopen(p, dephead, &deptail, pass == 0);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002491 if (!inc->fp) {
2492 /* -MG given but file not found */
2493 nasm_free(inc);
2494 } else {
2495 inc->fname = src_set_fname(nasm_strdup(p));
2496 inc->lineno = src_set_linnum(0);
2497 inc->lineinc = 1;
2498 inc->expansion = NULL;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002499 inc->mstk = NULL;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002500 istk = inc;
2501 list->uplevel(LIST_INCLUDE);
2502 }
2503 free_tlist(origline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002504 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002505
H. Peter Anvind2456592008-06-19 15:04:18 -07002506 case PP_USE:
H. Peter Anvinf4ae5ad2008-06-19 18:39:24 -07002507 {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002508 static macros_t *use_pkg;
2509 const char *pkg_macro = NULL;
H. Peter Anvinf4ae5ad2008-06-19 18:39:24 -07002510
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002511 tline = tline->next;
2512 skip_white_(tline);
2513 tline = expand_id(tline);
H. Peter Anvind2456592008-06-19 15:04:18 -07002514
H. Peter Anvin264b7b92008-10-24 16:38:17 -07002515 if (!tline || (tline->type != TOK_STRING &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002516 tline->type != TOK_INTERNAL_STRING &&
2517 tline->type != TOK_ID)) {
H. Peter Anvin926fc402008-06-19 16:26:12 -07002518 error(ERR_NONFATAL, "`%%use' expects a package name");
H. Peter Anvind2456592008-06-19 15:04:18 -07002519 free_tlist(origline);
2520 return DIRECTIVE_FOUND; /* but we did _something_ */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002521 }
H. Peter Anvin264b7b92008-10-24 16:38:17 -07002522 if (tline->next)
H. Peter Anvin917a3492008-09-24 09:14:49 -07002523 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvind2456592008-06-19 15:04:18 -07002524 "trailing garbage after `%%use' ignored");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002525 if (tline->type == TOK_STRING)
2526 nasm_unquote_cstr(tline->text, i);
2527 use_pkg = nasm_stdmac_find_package(tline->text);
2528 if (!use_pkg)
2529 error(ERR_NONFATAL, "unknown `%%use' package: %s", tline->text);
2530 else
2531 pkg_macro = (char *)use_pkg + 1; /* The first string will be <%define>__USE_*__ */
Victor van den Elzen35eb2ea2010-03-10 22:33:48 +01002532 if (use_pkg && ! smacro_defined(NULL, pkg_macro, 0, NULL, true)) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002533 /* Not already included, go ahead and include it */
2534 stdmacpos = use_pkg;
2535 }
2536 free_tlist(origline);
H. Peter Anvind2456592008-06-19 15:04:18 -07002537 return DIRECTIVE_FOUND;
H. Peter Anvinf4ae5ad2008-06-19 18:39:24 -07002538 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002539 case PP_PUSH:
H. Peter Anvine2c80182005-01-15 22:15:51 +00002540 case PP_REPL:
H. Peter Anvin42b56392008-10-24 16:24:21 -07002541 case PP_POP:
H. Peter Anvine2c80182005-01-15 22:15:51 +00002542 tline = tline->next;
2543 skip_white_(tline);
2544 tline = expand_id(tline);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002545 if (tline) {
2546 if (!tok_type_(tline, TOK_ID)) {
2547 error(ERR_NONFATAL, "`%s' expects a context identifier",
2548 pp_directives[i]);
2549 free_tlist(origline);
2550 return DIRECTIVE_FOUND; /* but we did _something_ */
2551 }
2552 if (tline->next)
2553 error(ERR_WARNING|ERR_PASS1,
2554 "trailing garbage after `%s' ignored",
2555 pp_directives[i]);
2556 p = nasm_strdup(tline->text);
2557 } else {
2558 p = NULL; /* Anonymous */
2559 }
H. Peter Anvin42b56392008-10-24 16:24:21 -07002560
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002561 if (i == PP_PUSH) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002562 ctx = nasm_malloc(sizeof(Context));
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002563 ctx->next = cstk;
2564 hash_init(&ctx->localmac, HASH_SMALL);
2565 ctx->name = p;
2566 ctx->number = unique++;
2567 cstk = ctx;
2568 } else {
2569 /* %pop or %repl */
2570 if (!cstk) {
2571 error(ERR_NONFATAL, "`%s': context stack is empty",
2572 pp_directives[i]);
2573 } else if (i == PP_POP) {
2574 if (p && (!cstk->name || nasm_stricmp(p, cstk->name)))
2575 error(ERR_NONFATAL, "`%%pop' in wrong context: %s, "
2576 "expected %s",
2577 cstk->name ? cstk->name : "anonymous", p);
2578 else
2579 ctx_pop();
2580 } else {
2581 /* i == PP_REPL */
2582 nasm_free(cstk->name);
2583 cstk->name = p;
2584 p = NULL;
2585 }
2586 nasm_free(p);
2587 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002588 free_tlist(origline);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002589 return DIRECTIVE_FOUND;
H. Peter Anvin8e3f75e2008-09-24 00:21:58 -07002590 case PP_FATAL:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002591 severity = ERR_FATAL;
2592 goto issue_error;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002593 case PP_ERROR:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002594 severity = ERR_NONFATAL;
2595 goto issue_error;
H. Peter Anvin7df04172008-06-10 18:27:38 -07002596 case PP_WARNING:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002597 severity = ERR_WARNING|ERR_WARN_USER;
2598 goto issue_error;
H. Peter Anvin8e3f75e2008-09-24 00:21:58 -07002599
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002600issue_error:
H. Peter Anvin7df04172008-06-10 18:27:38 -07002601 {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002602 /* Only error out if this is the final pass */
2603 if (pass != 2 && i != PP_FATAL)
2604 return DIRECTIVE_FOUND;
2605
2606 tline->next = expand_smacro(tline->next);
2607 tline = tline->next;
2608 skip_white_(tline);
2609 t = tline ? tline->next : NULL;
2610 skip_white_(t);
2611 if (tok_type_(tline, TOK_STRING) && !t) {
2612 /* The line contains only a quoted string */
2613 p = tline->text;
2614 nasm_unquote(p, NULL); /* Ignore NUL character truncation */
2615 error(severity, "%s", p);
2616 } else {
2617 /* Not a quoted string, or more than a quoted string */
2618 p = detoken(tline, false);
2619 error(severity, "%s", p);
2620 nasm_free(p);
2621 }
2622 free_tlist(origline);
2623 return DIRECTIVE_FOUND;
H. Peter Anvin7df04172008-06-10 18:27:38 -07002624 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00002625
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002626 CASE_PP_IF:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002627 if (istk->conds && !emitting(istk->conds->state))
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002628 j = COND_NEVER;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002629 else {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002630 j = if_condition(tline->next, i);
2631 tline->next = NULL; /* it got freed */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002632 j = j < 0 ? COND_NEVER : j ? COND_IF_TRUE : COND_IF_FALSE;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002633 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002634 cond = nasm_malloc(sizeof(Cond));
2635 cond->next = istk->conds;
2636 cond->state = j;
2637 istk->conds = cond;
2638 if(istk->mstk)
2639 istk->mstk->condcnt ++;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002640 free_tlist(origline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002641 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002642
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002643 CASE_PP_ELIF:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002644 if (!istk->conds)
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002645 error(ERR_FATAL, "`%s': no matching `%%if'", pp_directives[i]);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002646 switch(istk->conds->state) {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002647 case COND_IF_TRUE:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002648 istk->conds->state = COND_DONE;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002649 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002650
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002651 case COND_DONE:
2652 case COND_NEVER:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002653 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002654
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002655 case COND_ELSE_TRUE:
2656 case COND_ELSE_FALSE:
2657 error_precond(ERR_WARNING|ERR_PASS1,
2658 "`%%elif' after `%%else' ignored");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002659 istk->conds->state = COND_NEVER;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002660 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002661
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002662 case COND_IF_FALSE:
2663 /*
2664 * IMPORTANT: In the case of %if, we will already have
2665 * called expand_mmac_params(); however, if we're
2666 * processing an %elif we must have been in a
2667 * non-emitting mode, which would have inhibited
2668 * the normal invocation of expand_mmac_params().
2669 * Therefore, we have to do it explicitly here.
2670 */
2671 j = if_condition(expand_mmac_params(tline->next), i);
2672 tline->next = NULL; /* it got freed */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002673 istk->conds->state =
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002674 j < 0 ? COND_NEVER : j ? COND_IF_TRUE : COND_IF_FALSE;
2675 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002676 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002677 free_tlist(origline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002678 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002679
H. Peter Anvine2c80182005-01-15 22:15:51 +00002680 case PP_ELSE:
2681 if (tline->next)
H. Peter Anvin917a3492008-09-24 09:14:49 -07002682 error_precond(ERR_WARNING|ERR_PASS1,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002683 "trailing garbage after `%%else' ignored");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002684 if (!istk->conds)
2685 error(ERR_FATAL, "`%%else': no matching `%%if'");
2686 switch(istk->conds->state) {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002687 case COND_IF_TRUE:
2688 case COND_DONE:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002689 istk->conds->state = COND_ELSE_FALSE;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002690 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002691
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002692 case COND_NEVER:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002693 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002694
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002695 case COND_IF_FALSE:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002696 istk->conds->state = COND_ELSE_TRUE;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002697 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002698
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002699 case COND_ELSE_TRUE:
2700 case COND_ELSE_FALSE:
2701 error_precond(ERR_WARNING|ERR_PASS1,
2702 "`%%else' after `%%else' ignored.");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002703 istk->conds->state = COND_NEVER;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002704 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002705 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002706 free_tlist(origline);
2707 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002708
H. Peter Anvine2c80182005-01-15 22:15:51 +00002709 case PP_ENDIF:
2710 if (tline->next)
H. Peter Anvin917a3492008-09-24 09:14:49 -07002711 error_precond(ERR_WARNING|ERR_PASS1,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002712 "trailing garbage after `%%endif' ignored");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002713 if (!istk->conds)
2714 error(ERR_FATAL, "`%%endif': no matching `%%if'");
2715 cond = istk->conds;
2716 istk->conds = cond->next;
2717 nasm_free(cond);
2718 if(istk->mstk)
2719 istk->mstk->condcnt --;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002720 free_tlist(origline);
2721 return DIRECTIVE_FOUND;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002722
H. Peter Anvindb8f96e2009-07-15 09:07:29 -04002723 case PP_RMACRO:
2724 case PP_IRMACRO:
H. Peter Anvine2c80182005-01-15 22:15:51 +00002725 case PP_MACRO:
2726 case PP_IMACRO:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002727 if (defining) {
2728 error(ERR_FATAL, "`%s': already defining a macro",
2729 pp_directives[i]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002730 return DIRECTIVE_FOUND;
2731 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002732 defining = nasm_malloc(sizeof(MMacro));
2733 defining->max_depth =
2734 (i == PP_RMACRO) || (i == PP_IRMACRO) ? DEADMAN_LIMIT : 0;
2735 defining->casesense = (i == PP_MACRO) || (i == PP_RMACRO);
2736 if (!parse_mmacro_spec(tline, defining, pp_directives[i])) {
2737 nasm_free(defining);
2738 defining = NULL;
2739 return DIRECTIVE_FOUND;
2740 }
H. Peter Anvina26433d2008-07-16 14:40:01 -07002741
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002742 mmac = (MMacro *) hash_findix(&mmacros, defining->name);
2743 while (mmac) {
2744 if (!strcmp(mmac->name, defining->name) &&
2745 (mmac->nparam_min <= defining->nparam_max
2746 || defining->plus)
2747 && (defining->nparam_min <= mmac->nparam_max
2748 || mmac->plus)) {
2749 error(ERR_WARNING|ERR_PASS1,
2750 "redefining multi-line macro `%s'", defining->name);
2751 return DIRECTIVE_FOUND;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002752 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002753 mmac = mmac->next;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002754 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002755 free_tlist(origline);
2756 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002757
H. Peter Anvine2c80182005-01-15 22:15:51 +00002758 case PP_ENDM:
2759 case PP_ENDMACRO:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002760 if (! (defining && defining->name)) {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002761 error(ERR_NONFATAL, "`%s': not defining a macro", tline->text);
2762 return DIRECTIVE_FOUND;
2763 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002764 mmhead = (MMacro **) hash_findi_add(&mmacros, defining->name);
2765 defining->next = *mmhead;
2766 *mmhead = defining;
2767 defining = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002768 free_tlist(origline);
2769 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002770
H. Peter Anvin89cee572009-07-15 09:16:54 -04002771 case PP_EXITMACRO:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002772 /*
2773 * We must search along istk->expansion until we hit a
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002774 * macro-end marker for a macro with a name. Then we
2775 * bypass all lines between exitmacro and endmacro.
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002776 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002777 list_for_each(l, istk->expansion)
2778 if (l->finishes && l->finishes->name)
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002779 break;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002780
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002781 if (l) {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002782 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002783 * Remove all conditional entries relative to this
2784 * macro invocation. (safe to do in this context)
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002785 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002786 for ( ; l->finishes->condcnt > 0; l->finishes->condcnt --) {
2787 cond = istk->conds;
2788 istk->conds = cond->next;
2789 nasm_free(cond);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002790 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002791 istk->expansion = l;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002792 } else {
2793 error(ERR_NONFATAL, "`%%exitmacro' not within `%%macro' block");
2794 }
Keith Kanios852f1ee2009-07-12 00:19:55 -05002795 free_tlist(origline);
2796 return DIRECTIVE_FOUND;
2797
H. Peter Anvina26433d2008-07-16 14:40:01 -07002798 case PP_UNMACRO:
2799 case PP_UNIMACRO:
2800 {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002801 MMacro **mmac_p;
2802 MMacro spec;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002803
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002804 spec.casesense = (i == PP_UNMACRO);
2805 if (!parse_mmacro_spec(tline, &spec, pp_directives[i])) {
2806 return DIRECTIVE_FOUND;
2807 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002808 mmac_p = (MMacro **) hash_findi(&mmacros, spec.name, NULL);
2809 while (mmac_p && *mmac_p) {
2810 mmac = *mmac_p;
2811 if (mmac->casesense == spec.casesense &&
2812 !mstrcmp(mmac->name, spec.name, spec.casesense) &&
2813 mmac->nparam_min == spec.nparam_min &&
2814 mmac->nparam_max == spec.nparam_max &&
2815 mmac->plus == spec.plus) {
2816 *mmac_p = mmac->next;
2817 free_mmacro(mmac);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002818 } else {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002819 mmac_p = &mmac->next;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002820 }
2821 }
2822 free_tlist(origline);
2823 free_tlist(spec.dlist);
2824 return DIRECTIVE_FOUND;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002825 }
2826
H. Peter Anvine2c80182005-01-15 22:15:51 +00002827 case PP_ROTATE:
2828 if (tline->next && tline->next->type == TOK_WHITESPACE)
2829 tline = tline->next;
H. Peter Anvin89cee572009-07-15 09:16:54 -04002830 if (!tline->next) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002831 free_tlist(origline);
2832 error(ERR_NONFATAL, "`%%rotate' missing rotate count");
2833 return DIRECTIVE_FOUND;
2834 }
2835 t = expand_smacro(tline->next);
2836 tline->next = NULL;
2837 free_tlist(origline);
2838 tline = t;
2839 tptr = &t;
2840 tokval.t_type = TOKEN_INVALID;
2841 evalresult =
2842 evaluate(ppscan, tptr, &tokval, NULL, pass, error, NULL);
2843 free_tlist(tline);
2844 if (!evalresult)
2845 return DIRECTIVE_FOUND;
2846 if (tokval.t_type)
H. Peter Anvin917a3492008-09-24 09:14:49 -07002847 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +00002848 "trailing garbage after expression ignored");
2849 if (!is_simple(evalresult)) {
2850 error(ERR_NONFATAL, "non-constant value given to `%%rotate'");
2851 return DIRECTIVE_FOUND;
2852 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002853 mmac = istk->mstk;
2854 while (mmac && !mmac->name) /* avoid mistaking %reps for macros */
2855 mmac = mmac->next_active;
2856 if (!mmac) {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002857 error(ERR_NONFATAL, "`%%rotate' invoked outside a macro call");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002858 } else if (mmac->nparam == 0) {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002859 error(ERR_NONFATAL,
2860 "`%%rotate' invoked within macro without parameters");
2861 } else {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002862 int rotate = mmac->rotate + reloc_value(evalresult);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002863
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002864 rotate %= (int)mmac->nparam;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002865 if (rotate < 0)
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002866 rotate += mmac->nparam;
2867
2868 mmac->rotate = rotate;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002869 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002870 return DIRECTIVE_FOUND;
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00002871
H. Peter Anvine2c80182005-01-15 22:15:51 +00002872 case PP_REP:
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002873 nolist = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002874 do {
2875 tline = tline->next;
2876 } while (tok_type_(tline, TOK_WHITESPACE));
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00002877
H. Peter Anvine2c80182005-01-15 22:15:51 +00002878 if (tok_type_(tline, TOK_ID) &&
2879 nasm_stricmp(tline->text, ".nolist") == 0) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002880 nolist = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002881 do {
2882 tline = tline->next;
2883 } while (tok_type_(tline, TOK_WHITESPACE));
2884 }
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00002885
H. Peter Anvine2c80182005-01-15 22:15:51 +00002886 if (tline) {
2887 t = expand_smacro(tline);
2888 tptr = &t;
2889 tokval.t_type = TOKEN_INVALID;
2890 evalresult =
2891 evaluate(ppscan, tptr, &tokval, NULL, pass, error, NULL);
2892 if (!evalresult) {
2893 free_tlist(origline);
2894 return DIRECTIVE_FOUND;
2895 }
2896 if (tokval.t_type)
H. Peter Anvin917a3492008-09-24 09:14:49 -07002897 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +00002898 "trailing garbage after expression ignored");
2899 if (!is_simple(evalresult)) {
2900 error(ERR_NONFATAL, "non-constant value given to `%%rep'");
2901 return DIRECTIVE_FOUND;
2902 }
Cyrill Gorcunove091d6e2010-08-09 13:58:22 +04002903 count = reloc_value(evalresult);
2904 if (count >= REP_LIMIT) {
Cyrill Gorcunov71f4f842010-08-09 20:17:17 +04002905 error(ERR_NONFATAL, "`%%rep' value exceeds limit");
Cyrill Gorcunove091d6e2010-08-09 13:58:22 +04002906 count = 0;
2907 } else
2908 count++;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002909 } else {
2910 error(ERR_NONFATAL, "`%%rep' expects a repeat count");
H. Peter Anvinf8ba53e2007-10-11 10:11:57 -07002911 count = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002912 }
2913 free_tlist(origline);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002914
2915 tmp_defining = defining;
2916 defining = nasm_malloc(sizeof(MMacro));
2917 defining->prev = NULL;
2918 defining->name = NULL; /* flags this macro as a %rep block */
2919 defining->casesense = false;
2920 defining->plus = false;
2921 defining->nolist = nolist;
2922 defining->in_progress = count;
2923 defining->max_depth = 0;
2924 defining->nparam_min = defining->nparam_max = 0;
2925 defining->defaults = NULL;
2926 defining->dlist = NULL;
2927 defining->expansion = NULL;
2928 defining->next_active = istk->mstk;
2929 defining->rep_nest = tmp_defining;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002930 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002931
H. Peter Anvine2c80182005-01-15 22:15:51 +00002932 case PP_ENDREP:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002933 if (!defining || defining->name) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002934 error(ERR_NONFATAL, "`%%endrep': no matching `%%rep'");
2935 return DIRECTIVE_FOUND;
2936 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00002937
H. Peter Anvine2c80182005-01-15 22:15:51 +00002938 /*
2939 * Now we have a "macro" defined - although it has no name
2940 * and we won't be entering it in the hash tables - we must
2941 * push a macro-end marker for it on to istk->expansion.
2942 * After that, it will take care of propagating itself (a
2943 * macro-end marker line for a macro which is really a %rep
2944 * block will cause the macro to be re-expanded, complete
2945 * with another macro-end marker to ensure the process
2946 * continues) until the whole expansion is forcibly removed
2947 * from istk->expansion by a %exitrep.
2948 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002949 l = nasm_malloc(sizeof(Line));
2950 l->next = istk->expansion;
2951 l->finishes = defining;
2952 l->first = NULL;
2953 istk->expansion = l;
2954
2955 istk->mstk = defining;
2956
2957 list->uplevel(defining->nolist ? LIST_MACRO_NOLIST : LIST_MACRO);
2958 tmp_defining = defining;
2959 defining = defining->rep_nest;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002960 free_tlist(origline);
2961 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002962
H. Peter Anvine2c80182005-01-15 22:15:51 +00002963 case PP_EXITREP:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002964 /*
2965 * We must search along istk->expansion until we hit a
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002966 * macro-end marker for a macro with no name. Then we set
2967 * its `in_progress' flag to 0.
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002968 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002969 list_for_each(l, istk->expansion)
2970 if (l->finishes && !l->finishes->name)
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002971 break;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002972
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002973 if (l)
2974 l->finishes->in_progress = 1;
2975 else
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002976 error(ERR_NONFATAL, "`%%exitrep' not within `%%rep' block");
H. Peter Anvine2c80182005-01-15 22:15:51 +00002977 free_tlist(origline);
2978 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002979
H. Peter Anvine2c80182005-01-15 22:15:51 +00002980 case PP_XDEFINE:
2981 case PP_IXDEFINE:
2982 case PP_DEFINE:
2983 case PP_IDEFINE:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002984 casesense = (i == PP_DEFINE || i == PP_XDEFINE);
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07002985
H. Peter Anvine2c80182005-01-15 22:15:51 +00002986 tline = tline->next;
2987 skip_white_(tline);
2988 tline = expand_id(tline);
2989 if (!tline || (tline->type != TOK_ID &&
2990 (tline->type != TOK_PREPROC_ID ||
2991 tline->text[1] != '$'))) {
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07002992 error(ERR_NONFATAL, "`%s' expects a macro identifier",
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002993 pp_directives[i]);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002994 free_tlist(origline);
2995 return DIRECTIVE_FOUND;
2996 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00002997
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04002998 ctx = get_ctx(tline->text, &mname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002999 last = tline;
3000 param_start = tline = tline->next;
3001 nparam = 0;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003002
H. Peter Anvine2c80182005-01-15 22:15:51 +00003003 /* Expand the macro definition now for %xdefine and %ixdefine */
3004 if ((i == PP_XDEFINE) || (i == PP_IXDEFINE))
3005 tline = expand_smacro(tline);
H. Peter Anvin734b1882002-04-30 21:01:08 +00003006
H. Peter Anvine2c80182005-01-15 22:15:51 +00003007 if (tok_is_(tline, "(")) {
3008 /*
3009 * This macro has parameters.
3010 */
H. Peter Anvin734b1882002-04-30 21:01:08 +00003011
H. Peter Anvine2c80182005-01-15 22:15:51 +00003012 tline = tline->next;
3013 while (1) {
3014 skip_white_(tline);
3015 if (!tline) {
3016 error(ERR_NONFATAL, "parameter identifier expected");
3017 free_tlist(origline);
3018 return DIRECTIVE_FOUND;
3019 }
3020 if (tline->type != TOK_ID) {
3021 error(ERR_NONFATAL,
3022 "`%s': parameter identifier expected",
3023 tline->text);
3024 free_tlist(origline);
3025 return DIRECTIVE_FOUND;
3026 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003027 tline->type = TOK_SMAC_PARAM + nparam++;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003028 tline = tline->next;
3029 skip_white_(tline);
3030 if (tok_is_(tline, ",")) {
3031 tline = tline->next;
H. Peter Anvinca348b62008-07-23 10:49:26 -04003032 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003033 if (!tok_is_(tline, ")")) {
3034 error(ERR_NONFATAL,
3035 "`)' expected to terminate macro template");
3036 free_tlist(origline);
3037 return DIRECTIVE_FOUND;
3038 }
3039 break;
3040 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00003041 }
3042 last = tline;
3043 tline = tline->next;
3044 }
3045 if (tok_type_(tline, TOK_WHITESPACE))
3046 last = tline, tline = tline->next;
3047 macro_start = NULL;
3048 last->next = NULL;
3049 t = tline;
3050 while (t) {
3051 if (t->type == TOK_ID) {
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04003052 list_for_each(tt, param_start)
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003053 if (tt->type >= TOK_SMAC_PARAM &&
H. Peter Anvine2c80182005-01-15 22:15:51 +00003054 !strcmp(tt->text, t->text))
3055 t->type = tt->type;
3056 }
3057 tt = t->next;
3058 t->next = macro_start;
3059 macro_start = t;
3060 t = tt;
3061 }
3062 /*
3063 * Good. We now have a macro name, a parameter count, and a
3064 * token list (in reverse order) for an expansion. We ought
3065 * to be OK just to create an SMacro, store it, and let
3066 * free_tlist have the rest of the line (which we have
3067 * carefully re-terminated after chopping off the expansion
3068 * from the end).
3069 */
H. Peter Anvin4db5a162007-10-11 13:42:09 -07003070 define_smacro(ctx, mname, casesense, nparam, macro_start);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003071 free_tlist(origline);
3072 return DIRECTIVE_FOUND;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003073
H. Peter Anvine2c80182005-01-15 22:15:51 +00003074 case PP_UNDEF:
3075 tline = tline->next;
3076 skip_white_(tline);
3077 tline = expand_id(tline);
3078 if (!tline || (tline->type != TOK_ID &&
3079 (tline->type != TOK_PREPROC_ID ||
3080 tline->text[1] != '$'))) {
3081 error(ERR_NONFATAL, "`%%undef' expects a macro identifier");
3082 free_tlist(origline);
3083 return DIRECTIVE_FOUND;
3084 }
3085 if (tline->next) {
H. Peter Anvin917a3492008-09-24 09:14:49 -07003086 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +00003087 "trailing garbage after macro name ignored");
3088 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003089
H. Peter Anvine2c80182005-01-15 22:15:51 +00003090 /* Find the context that symbol belongs to */
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04003091 ctx = get_ctx(tline->text, &mname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003092 undef_smacro(ctx, mname);
3093 free_tlist(origline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003094 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003095
H. Peter Anvin9e200162008-06-04 17:23:14 -07003096 case PP_DEFSTR:
3097 case PP_IDEFSTR:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003098 casesense = (i == PP_DEFSTR);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003099
3100 tline = tline->next;
3101 skip_white_(tline);
3102 tline = expand_id(tline);
3103 if (!tline || (tline->type != TOK_ID &&
3104 (tline->type != TOK_PREPROC_ID ||
3105 tline->text[1] != '$'))) {
3106 error(ERR_NONFATAL, "`%s' expects a macro identifier",
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003107 pp_directives[i]);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003108 free_tlist(origline);
3109 return DIRECTIVE_FOUND;
3110 }
3111
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04003112 ctx = get_ctx(tline->text, &mname);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003113 last = tline;
3114 tline = expand_smacro(tline->next);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003115 last->next = NULL;
H. Peter Anvin9e200162008-06-04 17:23:14 -07003116
3117 while (tok_type_(tline, TOK_WHITESPACE))
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003118 tline = delete_Token(tline);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003119
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003120 p = detoken(tline, false);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003121 macro_start = nasm_malloc(sizeof(*macro_start));
3122 macro_start->next = NULL;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003123 macro_start->text = nasm_quote(p, strlen(p));
3124 macro_start->type = TOK_STRING;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003125 macro_start->a.mac = NULL;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003126 nasm_free(p);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003127
3128 /*
3129 * We now have a macro name, an implicit parameter count of
3130 * zero, and a string token to use as an expansion. Create
3131 * and store an SMacro.
3132 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003133 define_smacro(ctx, mname, casesense, 0, macro_start);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003134 free_tlist(origline);
3135 return DIRECTIVE_FOUND;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003136
H. Peter Anvin2f55bda2009-07-14 15:04:04 -04003137 case PP_DEFTOK:
3138 case PP_IDEFTOK:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003139 casesense = (i == PP_DEFTOK);
3140
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003141 tline = tline->next;
3142 skip_white_(tline);
3143 tline = expand_id(tline);
3144 if (!tline || (tline->type != TOK_ID &&
3145 (tline->type != TOK_PREPROC_ID ||
3146 tline->text[1] != '$'))) {
3147 error(ERR_NONFATAL,
3148 "`%s' expects a macro identifier as first parameter",
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003149 pp_directives[i]);
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003150 free_tlist(origline);
3151 return DIRECTIVE_FOUND;
3152 }
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04003153 ctx = get_ctx(tline->text, &mname);
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003154 last = tline;
3155 tline = expand_smacro(tline->next);
3156 last->next = NULL;
3157
3158 t = tline;
3159 while (tok_type_(t, TOK_WHITESPACE))
3160 t = t->next;
3161 /* t should now point to the string */
Cyrill Gorcunov6908e582010-09-06 19:36:15 +04003162 if (!tok_type_(t, TOK_STRING)) {
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003163 error(ERR_NONFATAL,
3164 "`%s` requires string as second parameter",
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003165 pp_directives[i]);
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003166 free_tlist(tline);
3167 free_tlist(origline);
3168 return DIRECTIVE_FOUND;
3169 }
3170
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003171 /*
3172 * Convert the string to a token stream. Note that smacros
3173 * are stored with the token stream reversed, so we have to
3174 * reverse the output of tokenize().
3175 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003176 nasm_unquote_cstr(t->text, i);
H. Peter Anvinb40992c2010-09-15 08:57:21 -07003177 macro_start = reverse_tokens(tokenize(t->text));
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003178
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003179 /*
3180 * We now have a macro name, an implicit parameter count of
3181 * zero, and a numeric token to use as an expansion. Create
3182 * and store an SMacro.
3183 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003184 define_smacro(ctx, mname, casesense, 0, macro_start);
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003185 free_tlist(tline);
3186 free_tlist(origline);
3187 return DIRECTIVE_FOUND;
H. Peter Anvin9e200162008-06-04 17:23:14 -07003188
H. Peter Anvin418ca702008-05-30 10:42:30 -07003189 case PP_PATHSEARCH:
3190 {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003191 FILE *fp;
3192 StrList *xsl = NULL;
3193 StrList **xst = &xsl;
H. Peter Anvin418ca702008-05-30 10:42:30 -07003194
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003195 casesense = true;
H. Peter Anvin418ca702008-05-30 10:42:30 -07003196
3197 tline = tline->next;
3198 skip_white_(tline);
3199 tline = expand_id(tline);
3200 if (!tline || (tline->type != TOK_ID &&
3201 (tline->type != TOK_PREPROC_ID ||
3202 tline->text[1] != '$'))) {
3203 error(ERR_NONFATAL,
3204 "`%%pathsearch' expects a macro identifier as first parameter");
3205 free_tlist(origline);
3206 return DIRECTIVE_FOUND;
3207 }
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04003208 ctx = get_ctx(tline->text, &mname);
H. Peter Anvin418ca702008-05-30 10:42:30 -07003209 last = tline;
3210 tline = expand_smacro(tline->next);
3211 last->next = NULL;
3212
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003213 t = tline;
H. Peter Anvin418ca702008-05-30 10:42:30 -07003214 while (tok_type_(t, TOK_WHITESPACE))
3215 t = t->next;
3216
3217 if (!t || (t->type != TOK_STRING &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003218 t->type != TOK_INTERNAL_STRING)) {
H. Peter Anvin418ca702008-05-30 10:42:30 -07003219 error(ERR_NONFATAL, "`%%pathsearch' expects a file name");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003220 free_tlist(tline);
H. Peter Anvin418ca702008-05-30 10:42:30 -07003221 free_tlist(origline);
3222 return DIRECTIVE_FOUND; /* but we did _something_ */
3223 }
3224 if (t->next)
H. Peter Anvin917a3492008-09-24 09:14:49 -07003225 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvin418ca702008-05-30 10:42:30 -07003226 "trailing garbage after `%%pathsearch' ignored");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003227 p = t->text;
H. Peter Anvin427cc912008-06-01 21:43:03 -07003228 if (t->type != TOK_INTERNAL_STRING)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003229 nasm_unquote(p, NULL);
H. Peter Anvin418ca702008-05-30 10:42:30 -07003230
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003231 fp = inc_fopen(p, &xsl, &xst, true);
3232 if (fp) {
3233 p = xsl->str;
3234 fclose(fp); /* Don't actually care about the file */
3235 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003236 macro_start = nasm_malloc(sizeof(*macro_start));
3237 macro_start->next = NULL;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003238 macro_start->text = nasm_quote(p, strlen(p));
3239 macro_start->type = TOK_STRING;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003240 macro_start->a.mac = NULL;
3241 if (xsl)
3242 nasm_free(xsl);
H. Peter Anvin418ca702008-05-30 10:42:30 -07003243
3244 /*
3245 * We now have a macro name, an implicit parameter count of
3246 * zero, and a string token to use as an expansion. Create
3247 * and store an SMacro.
3248 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003249 define_smacro(ctx, mname, casesense, 0, macro_start);
3250 free_tlist(tline);
H. Peter Anvin418ca702008-05-30 10:42:30 -07003251 free_tlist(origline);
3252 return DIRECTIVE_FOUND;
3253 }
3254
H. Peter Anvine2c80182005-01-15 22:15:51 +00003255 case PP_STRLEN:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003256 casesense = true;
H. Peter Anvin70653092007-10-19 14:42:29 -07003257
H. Peter Anvine2c80182005-01-15 22:15:51 +00003258 tline = tline->next;
3259 skip_white_(tline);
3260 tline = expand_id(tline);
3261 if (!tline || (tline->type != TOK_ID &&
3262 (tline->type != TOK_PREPROC_ID ||
3263 tline->text[1] != '$'))) {
3264 error(ERR_NONFATAL,
3265 "`%%strlen' expects a macro identifier as first parameter");
3266 free_tlist(origline);
3267 return DIRECTIVE_FOUND;
3268 }
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04003269 ctx = get_ctx(tline->text, &mname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003270 last = tline;
3271 tline = expand_smacro(tline->next);
3272 last->next = NULL;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003273
H. Peter Anvine2c80182005-01-15 22:15:51 +00003274 t = tline;
3275 while (tok_type_(t, TOK_WHITESPACE))
3276 t = t->next;
3277 /* t should now point to the string */
Cyrill Gorcunov4e1d5ab2010-07-23 18:51:51 +04003278 if (!tok_type_(t, TOK_STRING)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00003279 error(ERR_NONFATAL,
3280 "`%%strlen` requires string as second parameter");
3281 free_tlist(tline);
3282 free_tlist(origline);
3283 return DIRECTIVE_FOUND;
3284 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003285
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003286 macro_start = nasm_malloc(sizeof(*macro_start));
3287 macro_start->next = NULL;
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07003288 make_tok_num(macro_start, nasm_unquote(t->text, NULL));
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003289 macro_start->a.mac = NULL;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003290
H. Peter Anvine2c80182005-01-15 22:15:51 +00003291 /*
3292 * We now have a macro name, an implicit parameter count of
3293 * zero, and a numeric 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);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003297 free_tlist(tline);
3298 free_tlist(origline);
3299 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003300
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003301 case PP_STRCAT:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003302 casesense = true;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003303
3304 tline = tline->next;
3305 skip_white_(tline);
3306 tline = expand_id(tline);
3307 if (!tline || (tline->type != TOK_ID &&
3308 (tline->type != TOK_PREPROC_ID ||
3309 tline->text[1] != '$'))) {
3310 error(ERR_NONFATAL,
3311 "`%%strcat' expects a macro identifier as first parameter");
3312 free_tlist(origline);
3313 return DIRECTIVE_FOUND;
3314 }
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04003315 ctx = get_ctx(tline->text, &mname);
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003316 last = tline;
3317 tline = expand_smacro(tline->next);
3318 last->next = NULL;
3319
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003320 len = 0;
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04003321 list_for_each(t, tline) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003322 switch (t->type) {
3323 case TOK_WHITESPACE:
3324 break;
3325 case TOK_STRING:
3326 len += t->a.len = nasm_unquote(t->text, NULL);
3327 break;
3328 case TOK_OTHER:
3329 if (!strcmp(t->text, ",")) /* permit comma separators */
3330 break;
3331 /* else fall through */
3332 default:
3333 error(ERR_NONFATAL,
3334 "non-string passed to `%%strcat' (%d)", t->type);
3335 free_tlist(tline);
3336 free_tlist(origline);
3337 return DIRECTIVE_FOUND;
3338 }
3339 }
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003340
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003341 p = pp = nasm_malloc(len);
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04003342 list_for_each(t, tline) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003343 if (t->type == TOK_STRING) {
3344 memcpy(p, t->text, t->a.len);
3345 p += t->a.len;
3346 }
3347 }
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003348
3349 /*
3350 * We now have a macro name, an implicit parameter count of
3351 * zero, and a numeric token to use as an expansion. Create
3352 * and store an SMacro.
3353 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003354 macro_start = new_Token(NULL, TOK_STRING, NULL, 0);
3355 macro_start->text = nasm_quote(pp, len);
3356 nasm_free(pp);
3357 define_smacro(ctx, mname, casesense, 0, macro_start);
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003358 free_tlist(tline);
3359 free_tlist(origline);
3360 return DIRECTIVE_FOUND;
3361
H. Peter Anvine2c80182005-01-15 22:15:51 +00003362 case PP_SUBSTR:
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003363 {
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003364 int64_t start, count;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003365 size_t len;
H. Peter Anvind2456592008-06-19 15:04:18 -07003366
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003367 casesense = true;
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07003368
H. Peter Anvine2c80182005-01-15 22:15:51 +00003369 tline = tline->next;
3370 skip_white_(tline);
3371 tline = expand_id(tline);
3372 if (!tline || (tline->type != TOK_ID &&
3373 (tline->type != TOK_PREPROC_ID ||
3374 tline->text[1] != '$'))) {
3375 error(ERR_NONFATAL,
3376 "`%%substr' expects a macro identifier as first parameter");
3377 free_tlist(origline);
3378 return DIRECTIVE_FOUND;
3379 }
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04003380 ctx = get_ctx(tline->text, &mname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003381 last = tline;
3382 tline = expand_smacro(tline->next);
3383 last->next = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003384
Cyrill Gorcunov35519d62010-09-06 23:49:52 +04003385 if (tline) /* skip expanded id */
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003386 t = tline->next;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003387 while (tok_type_(t, TOK_WHITESPACE))
3388 t = t->next;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003389
H. Peter Anvine2c80182005-01-15 22:15:51 +00003390 /* t should now point to the string */
Cyrill Gorcunov35519d62010-09-06 23:49:52 +04003391 if (!tok_type_(t, TOK_STRING)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00003392 error(ERR_NONFATAL,
3393 "`%%substr` requires string as second parameter");
3394 free_tlist(tline);
3395 free_tlist(origline);
3396 return DIRECTIVE_FOUND;
3397 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003398
H. Peter Anvine2c80182005-01-15 22:15:51 +00003399 tt = t->next;
3400 tptr = &tt;
3401 tokval.t_type = TOKEN_INVALID;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003402 evalresult = evaluate(ppscan, tptr, &tokval, NULL,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003403 pass, error, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003404 if (!evalresult) {
3405 free_tlist(tline);
3406 free_tlist(origline);
3407 return DIRECTIVE_FOUND;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003408 } else if (!is_simple(evalresult)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00003409 error(ERR_NONFATAL, "non-constant value given to `%%substr`");
3410 free_tlist(tline);
3411 free_tlist(origline);
3412 return DIRECTIVE_FOUND;
3413 }
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003414 start = evalresult->value - 1;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003415
3416 while (tok_type_(tt, TOK_WHITESPACE))
3417 tt = tt->next;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003418 if (!tt) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003419 count = 1; /* Backwards compatibility: one character */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003420 } else {
3421 tokval.t_type = TOKEN_INVALID;
3422 evalresult = evaluate(ppscan, tptr, &tokval, NULL,
3423 pass, error, NULL);
3424 if (!evalresult) {
3425 free_tlist(tline);
3426 free_tlist(origline);
3427 return DIRECTIVE_FOUND;
3428 } else if (!is_simple(evalresult)) {
3429 error(ERR_NONFATAL, "non-constant value given to `%%substr`");
3430 free_tlist(tline);
3431 free_tlist(origline);
3432 return DIRECTIVE_FOUND;
3433 }
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003434 count = evalresult->value;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003435 }
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003436
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003437 len = nasm_unquote(t->text, NULL);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003438
Cyrill Gorcunovcff031e2010-09-07 20:31:11 +04003439 /* make start and count being in range */
3440 if (start < 0)
3441 start = 0;
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003442 if (count < 0)
3443 count = len + count + 1 - start;
3444 if (start + count > (int64_t)len)
Cyrill Gorcunovcff031e2010-09-07 20:31:11 +04003445 count = len - start;
3446 if (!len || count < 0 || start >=(int64_t)len)
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003447 start = -1, count = 0; /* empty string */
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003448
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003449 macro_start = nasm_malloc(sizeof(*macro_start));
3450 macro_start->next = NULL;
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003451 macro_start->text = nasm_quote((start < 0) ? "" : t->text + start, count);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003452 macro_start->type = TOK_STRING;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003453 macro_start->a.mac = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003454
H. Peter Anvine2c80182005-01-15 22:15:51 +00003455 /*
3456 * We now have a macro name, an implicit parameter count of
3457 * zero, and a numeric token to use as an expansion. Create
3458 * and store an SMacro.
3459 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003460 define_smacro(ctx, mname, casesense, 0, macro_start);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003461 free_tlist(tline);
3462 free_tlist(origline);
3463 return DIRECTIVE_FOUND;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003464 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003465
H. Peter Anvine2c80182005-01-15 22:15:51 +00003466 case PP_ASSIGN:
3467 case PP_IASSIGN:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003468 casesense = (i == PP_ASSIGN);
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07003469
H. Peter Anvine2c80182005-01-15 22:15:51 +00003470 tline = tline->next;
3471 skip_white_(tline);
3472 tline = expand_id(tline);
3473 if (!tline || (tline->type != TOK_ID &&
3474 (tline->type != TOK_PREPROC_ID ||
3475 tline->text[1] != '$'))) {
3476 error(ERR_NONFATAL,
3477 "`%%%sassign' expects a macro identifier",
3478 (i == PP_IASSIGN ? "i" : ""));
3479 free_tlist(origline);
3480 return DIRECTIVE_FOUND;
3481 }
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04003482 ctx = get_ctx(tline->text, &mname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003483 last = tline;
3484 tline = expand_smacro(tline->next);
3485 last->next = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003486
H. Peter Anvine2c80182005-01-15 22:15:51 +00003487 t = tline;
3488 tptr = &t;
3489 tokval.t_type = TOKEN_INVALID;
3490 evalresult =
3491 evaluate(ppscan, tptr, &tokval, NULL, pass, error, NULL);
3492 free_tlist(tline);
3493 if (!evalresult) {
3494 free_tlist(origline);
3495 return DIRECTIVE_FOUND;
3496 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003497
H. Peter Anvine2c80182005-01-15 22:15:51 +00003498 if (tokval.t_type)
H. Peter Anvin917a3492008-09-24 09:14:49 -07003499 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +00003500 "trailing garbage after expression ignored");
H. Peter Anvin734b1882002-04-30 21:01:08 +00003501
H. Peter Anvine2c80182005-01-15 22:15:51 +00003502 if (!is_simple(evalresult)) {
3503 error(ERR_NONFATAL,
3504 "non-constant value given to `%%%sassign'",
3505 (i == PP_IASSIGN ? "i" : ""));
3506 free_tlist(origline);
3507 return DIRECTIVE_FOUND;
3508 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003509
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003510 macro_start = nasm_malloc(sizeof(*macro_start));
3511 macro_start->next = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003512 make_tok_num(macro_start, reloc_value(evalresult));
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003513 macro_start->a.mac = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003514
H. Peter Anvine2c80182005-01-15 22:15:51 +00003515 /*
3516 * We now have a macro name, an implicit parameter count of
3517 * zero, and a numeric token to use as an expansion. Create
3518 * and store an SMacro.
3519 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003520 define_smacro(ctx, mname, casesense, 0, macro_start);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003521 free_tlist(origline);
3522 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003523
H. Peter Anvine2c80182005-01-15 22:15:51 +00003524 case PP_LINE:
3525 /*
3526 * Syntax is `%line nnn[+mmm] [filename]'
3527 */
3528 tline = tline->next;
3529 skip_white_(tline);
3530 if (!tok_type_(tline, TOK_NUMBER)) {
3531 error(ERR_NONFATAL, "`%%line' expects line number");
3532 free_tlist(origline);
3533 return DIRECTIVE_FOUND;
3534 }
H. Peter Anvin70055962007-10-11 00:05:31 -07003535 k = readnum(tline->text, &err);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003536 m = 1;
3537 tline = tline->next;
3538 if (tok_is_(tline, "+")) {
3539 tline = tline->next;
3540 if (!tok_type_(tline, TOK_NUMBER)) {
3541 error(ERR_NONFATAL, "`%%line' expects line increment");
3542 free_tlist(origline);
3543 return DIRECTIVE_FOUND;
3544 }
H. Peter Anvin70055962007-10-11 00:05:31 -07003545 m = readnum(tline->text, &err);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003546 tline = tline->next;
3547 }
3548 skip_white_(tline);
3549 src_set_linnum(k);
3550 istk->lineinc = m;
3551 if (tline) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07003552 nasm_free(src_set_fname(detoken(tline, false)));
H. Peter Anvine2c80182005-01-15 22:15:51 +00003553 }
3554 free_tlist(origline);
3555 return DIRECTIVE_FOUND;
Keith Kaniosb307a4f2010-11-06 17:41:51 -05003556
H. Peter Anvine2c80182005-01-15 22:15:51 +00003557 default:
3558 error(ERR_FATAL,
3559 "preprocessor directive `%s' not yet implemented",
H. Peter Anvin4169a472007-09-12 01:29:43 +00003560 pp_directives[i]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003561 return DIRECTIVE_FOUND;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003562 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003563}
3564
3565/*
H. Peter Anvin76690a12002-04-30 20:52:49 +00003566 * Ensure that a macro parameter contains a condition code and
3567 * nothing else. Return the condition code index if so, or -1
3568 * otherwise.
3569 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003570static int find_cc(Token * t)
H. Peter Anvineba20a72002-04-30 20:53:55 +00003571{
H. Peter Anvin76690a12002-04-30 20:52:49 +00003572 Token *tt;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003573
H. Peter Anvin25a99342007-09-22 17:45:45 -07003574 if (!t)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003575 return -1; /* Probably a %+ without a space */
H. Peter Anvin25a99342007-09-22 17:45:45 -07003576
H. Peter Anvineba20a72002-04-30 20:53:55 +00003577 skip_white_(t);
H. Peter Anvin76690a12002-04-30 20:52:49 +00003578 if (t->type != TOK_ID)
H. Peter Anvine2c80182005-01-15 22:15:51 +00003579 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003580 tt = t->next;
H. Peter Anvineba20a72002-04-30 20:53:55 +00003581 skip_white_(tt);
H. Peter Anvin76690a12002-04-30 20:52:49 +00003582 if (tt && (tt->type != TOK_OTHER || strcmp(tt->text, ",")))
H. Peter Anvine2c80182005-01-15 22:15:51 +00003583 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003584
Cyrill Gorcunov19456392012-05-02 00:18:56 +04003585 return bsii(t->text, (const char **)conditions, ARRAY_SIZE(conditions));
H. Peter Anvin76690a12002-04-30 20:52:49 +00003586}
3587
Cyrill Gorcunov575d4282010-10-06 00:25:55 +04003588static bool paste_tokens(Token **head, const struct tokseq_match *m,
3589 int mnum, bool handle_paste_tokens)
H. Peter Anvind784a082009-04-20 14:01:18 -07003590{
3591 Token **tail, *t, *tt;
3592 Token **paste_head;
3593 bool did_paste = false;
3594 char *tmp;
Cyrill Gorcunov575d4282010-10-06 00:25:55 +04003595 int i;
H. Peter Anvind784a082009-04-20 14:01:18 -07003596
3597 /* Now handle token pasting... */
3598 paste_head = NULL;
3599 tail = head;
3600 while ((t = *tail) && (tt = t->next)) {
3601 switch (t->type) {
3602 case TOK_WHITESPACE:
3603 if (tt->type == TOK_WHITESPACE) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003604 /* Zap adjacent whitespace tokens */
H. Peter Anvind784a082009-04-20 14:01:18 -07003605 t->next = delete_Token(tt);
3606 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003607 /* Do not advance paste_head here */
3608 tail = &t->next;
3609 }
H. Peter Anvind784a082009-04-20 14:01:18 -07003610 break;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003611 case TOK_PASTE: /* %+ */
3612 if (handle_paste_tokens) {
3613 /* Zap %+ and whitespace tokens to the right */
3614 while (t && (t->type == TOK_WHITESPACE ||
3615 t->type == TOK_PASTE))
3616 t = *tail = delete_Token(t);
Cyrill Gorcunov65ff09a2012-03-04 13:05:55 +04003617 if (!t) { /* Dangling %+ term */
3618 if (paste_head)
3619 (*paste_head)->next = NULL;
3620 else
3621 *head = NULL;
3622 return did_paste;
3623 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003624 tail = paste_head;
3625 t = *tail;
3626 tt = t->next;
3627 while (tok_type_(tt, TOK_WHITESPACE))
3628 tt = t->next = delete_Token(tt);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003629 if (tt) {
3630 tmp = nasm_strcat(t->text, tt->text);
3631 delete_Token(t);
3632 tt = delete_Token(tt);
3633 t = *tail = tokenize(tmp);
3634 nasm_free(tmp);
3635 while (t->next) {
3636 tail = &t->next;
3637 t = t->next;
3638 }
3639 t->next = tt; /* Attach the remaining token chain */
3640 did_paste = true;
3641 }
3642 paste_head = tail;
3643 tail = &t->next;
3644 break;
3645 }
3646 /* else fall through */
3647 default:
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +04003648 /*
3649 * Concatenation of tokens might look nontrivial
3650 * but in real it's pretty simple -- the caller
Cyrill Gorcunov575d4282010-10-06 00:25:55 +04003651 * prepares the masks of token types to be concatenated
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +04003652 * and we simply find matched sequences and slip
3653 * them together
3654 */
Cyrill Gorcunov575d4282010-10-06 00:25:55 +04003655 for (i = 0; i < mnum; i++) {
3656 if (PP_CONCAT_MASK(t->type) & m[i].mask_head) {
3657 size_t len = 0;
3658 char *tmp, *p;
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +04003659
Cyrill Gorcunov575d4282010-10-06 00:25:55 +04003660 while (tt && (PP_CONCAT_MASK(tt->type) & m[i].mask_tail)) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003661 len += strlen(tt->text);
3662 tt = tt->next;
Cyrill Gorcunov575d4282010-10-06 00:25:55 +04003663 }
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +04003664
Cyrill Gorcunov575d4282010-10-06 00:25:55 +04003665 /*
3666 * Now tt points to the first token after
3667 * the potential paste area...
3668 */
3669 if (tt != t->next) {
3670 /* We have at least two tokens... */
3671 len += strlen(t->text);
3672 p = tmp = nasm_malloc(len+1);
3673 while (t != tt) {
3674 strcpy(p, t->text);
3675 p = strchr(p, '\0');
3676 t = delete_Token(t);
3677 }
3678 t = *tail = tokenize(tmp);
3679 nasm_free(tmp);
3680 while (t->next) {
3681 tail = &t->next;
3682 t = t->next;
3683 }
3684 t->next = tt; /* Attach the remaining token chain */
3685 did_paste = true;
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +04003686 }
Cyrill Gorcunov575d4282010-10-06 00:25:55 +04003687 paste_head = tail;
3688 tail = &t->next;
3689 break;
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +04003690 }
Cyrill Gorcunov575d4282010-10-06 00:25:55 +04003691 }
3692 if (i >= mnum) { /* no match */
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +04003693 tail = &t->next;
3694 if (!tok_type_(t->next, TOK_WHITESPACE))
3695 paste_head = tail;
3696 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003697 break;
H. Peter Anvind784a082009-04-20 14:01:18 -07003698 }
3699 }
3700 return did_paste;
3701}
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003702
3703/*
3704 * expands to a list of tokens from %{x:y}
3705 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003706static Token *expand_mmac_params_range(MMacro *mac, Token *tline, Token ***last)
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003707{
3708 Token *t = tline, **tt, *tm, *head;
3709 char *pos;
3710 int fst, lst, j, i;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003711
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003712 pos = strchr(tline->text, ':');
3713 nasm_assert(pos);
3714
3715 lst = atoi(pos + 1);
3716 fst = atoi(tline->text + 1);
3717
3718 /*
3719 * only macros params are accounted so
3720 * if someone passes %0 -- we reject such
3721 * value(s)
3722 */
3723 if (lst == 0 || fst == 0)
3724 goto err;
3725
3726 /* the values should be sane */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003727 if ((fst > (int)mac->nparam || fst < (-(int)mac->nparam)) ||
3728 (lst > (int)mac->nparam || lst < (-(int)mac->nparam)))
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003729 goto err;
3730
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003731 fst = fst < 0 ? fst + (int)mac->nparam + 1: fst;
3732 lst = lst < 0 ? lst + (int)mac->nparam + 1: lst;
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003733
3734 /* counted from zero */
3735 fst--, lst--;
3736
3737 /*
3738 * it will be at least one token
3739 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003740 tm = mac->params[(fst + mac->rotate) % mac->nparam];
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003741 t = new_Token(NULL, tm->type, tm->text, 0);
3742 head = t, tt = &t->next;
3743 if (fst < lst) {
3744 for (i = fst + 1; i <= lst; i++) {
3745 t = new_Token(NULL, TOK_OTHER, ",", 0);
3746 *tt = t, tt = &t->next;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003747 j = (i + mac->rotate) % mac->nparam;
3748 tm = mac->params[j];
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003749 t = new_Token(NULL, tm->type, tm->text, 0);
3750 *tt = t, tt = &t->next;
3751 }
3752 } else {
3753 for (i = fst - 1; i >= lst; i--) {
3754 t = new_Token(NULL, TOK_OTHER, ",", 0);
3755 *tt = t, tt = &t->next;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003756 j = (i + mac->rotate) % mac->nparam;
3757 tm = mac->params[j];
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003758 t = new_Token(NULL, tm->type, tm->text, 0);
3759 *tt = t, tt = &t->next;
3760 }
3761 }
3762
3763 *last = tt;
3764 return head;
3765
3766err:
3767 error(ERR_NONFATAL, "`%%{%s}': macro parameters out of range",
3768 &tline->text[1]);
3769 return tline;
3770}
3771
H. Peter Anvin76690a12002-04-30 20:52:49 +00003772/*
3773 * Expand MMacro-local things: parameter references (%0, %n, %+n,
H. Peter Anvin67c63722008-10-26 23:49:00 -07003774 * %-n) and MMacro-local identifiers (%%foo) as well as
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003775 * macro indirection (%[...]) and range (%{..:..}).
H. Peter Anvin76690a12002-04-30 20:52:49 +00003776 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003777static Token *expand_mmac_params(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00003778{
H. Peter Anvin734b1882002-04-30 21:01:08 +00003779 Token *t, *tt, **tail, *thead;
H. Peter Anvin6125b622009-04-08 14:02:25 -07003780 bool changed = false;
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003781 char *pos;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003782
3783 tail = &thead;
3784 thead = NULL;
3785
H. Peter Anvine2c80182005-01-15 22:15:51 +00003786 while (tline) {
3787 if (tline->type == TOK_PREPROC_ID &&
Cyrill Gorcunovca611192010-06-04 09:22:12 +04003788 (((tline->text[1] == '+' || tline->text[1] == '-') && tline->text[2]) ||
3789 (tline->text[1] >= '0' && tline->text[1] <= '9') ||
3790 tline->text[1] == '%')) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00003791 char *text = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003792 int type = 0, cc; /* type = 0 to placate optimisers */
Keith Kaniosa6dfa782007-04-13 16:47:53 +00003793 char tmpbuf[30];
H. Peter Anvin25a99342007-09-22 17:45:45 -07003794 unsigned int n;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003795 int i;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003796 MMacro *mac;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003797
H. Peter Anvine2c80182005-01-15 22:15:51 +00003798 t = tline;
3799 tline = tline->next;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003800
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003801 mac = istk->mstk;
3802 while (mac && !mac->name) /* avoid mistaking %reps for macros */
3803 mac = mac->next_active;
3804 if (!mac) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00003805 error(ERR_NONFATAL, "`%s': not in a macro call", t->text);
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003806 } else {
3807 pos = strchr(t->text, ':');
3808 if (!pos) {
3809 switch (t->text[1]) {
3810 /*
3811 * We have to make a substitution of one of the
3812 * forms %1, %-1, %+1, %%foo, %0.
3813 */
3814 case '0':
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003815 type = TOK_NUMBER;
3816 snprintf(tmpbuf, sizeof(tmpbuf), "%d", mac->nparam);
3817 text = nasm_strdup(tmpbuf);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003818 break;
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003819 case '%':
H. Peter Anvine2c80182005-01-15 22:15:51 +00003820 type = TOK_ID;
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003821 snprintf(tmpbuf, sizeof(tmpbuf), "..@%"PRIu64".",
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003822 mac->unique);
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003823 text = nasm_strcat(tmpbuf, t->text + 2);
3824 break;
3825 case '-':
3826 n = atoi(t->text + 2) - 1;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003827 if (n >= mac->nparam)
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003828 tt = NULL;
3829 else {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003830 if (mac->nparam > 1)
3831 n = (n + mac->rotate) % mac->nparam;
3832 tt = mac->params[n];
H. Peter Anvine2c80182005-01-15 22:15:51 +00003833 }
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003834 cc = find_cc(tt);
3835 if (cc == -1) {
3836 error(ERR_NONFATAL,
3837 "macro parameter %d is not a condition code",
3838 n + 1);
3839 text = NULL;
3840 } else {
3841 type = TOK_ID;
3842 if (inverse_ccs[cc] == -1) {
3843 error(ERR_NONFATAL,
3844 "condition code `%s' is not invertible",
3845 conditions[cc]);
3846 text = NULL;
3847 } else
3848 text = nasm_strdup(conditions[inverse_ccs[cc]]);
3849 }
3850 break;
3851 case '+':
3852 n = atoi(t->text + 2) - 1;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003853 if (n >= mac->nparam)
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003854 tt = NULL;
3855 else {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003856 if (mac->nparam > 1)
3857 n = (n + mac->rotate) % mac->nparam;
3858 tt = mac->params[n];
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003859 }
3860 cc = find_cc(tt);
3861 if (cc == -1) {
3862 error(ERR_NONFATAL,
3863 "macro parameter %d is not a condition code",
3864 n + 1);
3865 text = NULL;
3866 } else {
3867 type = TOK_ID;
3868 text = nasm_strdup(conditions[cc]);
3869 }
3870 break;
3871 default:
3872 n = atoi(t->text + 1) - 1;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003873 if (n >= mac->nparam)
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003874 tt = NULL;
3875 else {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003876 if (mac->nparam > 1)
3877 n = (n + mac->rotate) % mac->nparam;
3878 tt = mac->params[n];
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003879 }
3880 if (tt) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003881 for (i = 0; i < mac->paramlen[n]; i++) {
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003882 *tail = new_Token(NULL, tt->type, tt->text, 0);
3883 tail = &(*tail)->next;
3884 tt = tt->next;
3885 }
3886 }
3887 text = NULL; /* we've done it here */
3888 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003889 }
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003890 } else {
3891 /*
3892 * seems we have a parameters range here
3893 */
3894 Token *head, **last;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003895 head = expand_mmac_params_range(mac, t, &last);
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003896 if (head != t) {
3897 *tail = head;
3898 *last = tline;
3899 tline = head;
3900 text = NULL;
3901 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00003902 }
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003903 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00003904 if (!text) {
3905 delete_Token(t);
3906 } else {
3907 *tail = t;
3908 tail = &t->next;
3909 t->type = type;
3910 nasm_free(t->text);
3911 t->text = text;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003912 t->a.mac = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003913 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003914 changed = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003915 continue;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003916 } else if (tline->type == TOK_INDIRECT) {
3917 t = tline;
3918 tline = tline->next;
3919 tt = tokenize(t->text);
3920 tt = expand_mmac_params(tt);
3921 tt = expand_smacro(tt);
3922 *tail = tt;
3923 while (tt) {
3924 tt->a.mac = NULL; /* Necessary? */
3925 tail = &tt->next;
3926 tt = tt->next;
3927 }
3928 delete_Token(t);
3929 changed = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003930 } else {
3931 t = *tail = tline;
3932 tline = tline->next;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003933 t->a.mac = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003934 tail = &t->next;
3935 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00003936 }
H. Peter Anvineba20a72002-04-30 20:53:55 +00003937 *tail = NULL;
H. Peter Anvin67c63722008-10-26 23:49:00 -07003938
Cyrill Gorcunovc6a742c2011-06-27 01:23:09 +04003939 if (changed) {
3940 const struct tokseq_match t[] = {
3941 {
3942 PP_CONCAT_MASK(TOK_ID) |
3943 PP_CONCAT_MASK(TOK_FLOAT), /* head */
3944 PP_CONCAT_MASK(TOK_ID) |
3945 PP_CONCAT_MASK(TOK_NUMBER) |
3946 PP_CONCAT_MASK(TOK_FLOAT) |
3947 PP_CONCAT_MASK(TOK_OTHER) /* tail */
3948 },
3949 {
3950 PP_CONCAT_MASK(TOK_NUMBER), /* head */
3951 PP_CONCAT_MASK(TOK_NUMBER) /* tail */
3952 }
3953 };
3954 paste_tokens(&thead, t, ARRAY_SIZE(t), false);
3955 }
H. Peter Anvin6125b622009-04-08 14:02:25 -07003956
H. Peter Anvin76690a12002-04-30 20:52:49 +00003957 return thead;
3958}
3959
3960/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003961 * Expand all single-line macro calls made in the given line.
3962 * Return the expanded version of the line. The original is deemed
3963 * to be destroyed in the process. (In reality we'll just move
3964 * Tokens from input to output a lot of the time, rather than
3965 * actually bothering to destroy and replicate.)
3966 */
H. Peter Anvincb1cf592007-11-19 12:26:50 -08003967
H. Peter Anvine2c80182005-01-15 22:15:51 +00003968static Token *expand_smacro(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00003969{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003970 Token *t, *tt, *mstart, **tail, *thead;
H. Peter Anvineba20a72002-04-30 20:53:55 +00003971 SMacro *head = NULL, *m;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003972 Token **params;
3973 int *paramsize;
H. Peter Anvin25a99342007-09-22 17:45:45 -07003974 unsigned int nparam, sparam;
H. Peter Anvind784a082009-04-20 14:01:18 -07003975 int brackets;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003976 Token *org_tline = tline;
3977 Context *ctx;
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08003978 const char *mname;
H. Peter Anvin2a15e692007-11-19 13:14:59 -08003979 int deadman = DEADMAN_LIMIT;
H. Peter Anvin8287daf2009-07-07 16:00:58 -07003980 bool expanded;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003981
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003982 /*
3983 * Trick: we should avoid changing the start token pointer since it can
3984 * be contained in "next" field of other token. Because of this
3985 * we allocate a copy of first token and work with it; at the end of
3986 * routine we copy it back
3987 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003988 if (org_tline) {
Cyrill Gorcunoved4a8052010-04-09 15:40:35 +04003989 tline = new_Token(org_tline->next, org_tline->type,
3990 org_tline->text, 0);
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003991 tline->a.mac = org_tline->a.mac;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003992 nasm_free(org_tline->text);
3993 org_tline->text = NULL;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003994 }
3995
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003996 expanded = true; /* Always expand %+ at least once */
H. Peter Anvin8287daf2009-07-07 16:00:58 -07003997
H. Peter Anvincb1cf592007-11-19 12:26:50 -08003998again:
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003999 thead = NULL;
Cyrill Gorcunoved4a8052010-04-09 15:40:35 +04004000 tail = &thead;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004001
H. Peter Anvine2c80182005-01-15 22:15:51 +00004002 while (tline) { /* main token loop */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004003 if (!--deadman) {
4004 error(ERR_NONFATAL, "interminable macro recursion");
Cyrill Gorcunovbd38c8f2009-11-21 11:11:23 +03004005 goto err;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004006 }
H. Peter Anvincb1cf592007-11-19 12:26:50 -08004007
H. Peter Anvine2c80182005-01-15 22:15:51 +00004008 if ((mname = tline->text)) {
4009 /* if this token is a local macro, look in local context */
Cyrill Gorcunovc56d9ad2010-02-11 15:12:19 +03004010 if (tline->type == TOK_ID) {
4011 head = (SMacro *)hash_findix(&smacros, mname);
4012 } else if (tline->type == TOK_PREPROC_ID) {
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04004013 ctx = get_ctx(mname, &mname);
Cyrill Gorcunovc56d9ad2010-02-11 15:12:19 +03004014 head = ctx ? (SMacro *)hash_findix(&ctx->localmac, mname) : NULL;
4015 } else
4016 head = NULL;
H. Peter Anvin072771e2008-05-22 13:17:51 -07004017
H. Peter Anvine2c80182005-01-15 22:15:51 +00004018 /*
4019 * We've hit an identifier. As in is_mmacro below, we first
4020 * check whether the identifier is a single-line macro at
4021 * all, then think about checking for parameters if
4022 * necessary.
4023 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004024 list_for_each(m, head)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004025 if (!mstrcmp(m->name, mname, m->casesense))
4026 break;
4027 if (m) {
4028 mstart = tline;
4029 params = NULL;
4030 paramsize = NULL;
4031 if (m->nparam == 0) {
4032 /*
4033 * Simple case: the macro is parameterless. Discard the
4034 * one token that the macro call took, and push the
4035 * expansion back on the to-do stack.
4036 */
4037 if (!m->expansion) {
4038 if (!strcmp("__FILE__", m->name)) {
4039 int32_t num = 0;
4040 char *file = NULL;
4041 src_get(&num, &file);
4042 tline->text = nasm_quote(file, strlen(file));
4043 tline->type = TOK_STRING;
4044 nasm_free(file);
H. Peter Anvine2c80182005-01-15 22:15:51 +00004045 continue;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004046 }
4047 if (!strcmp("__LINE__", m->name)) {
4048 nasm_free(tline->text);
4049 make_tok_num(tline, src_get_linnum());
4050 continue;
4051 }
4052 if (!strcmp("__BITS__", m->name)) {
4053 nasm_free(tline->text);
4054 make_tok_num(tline, globalbits);
4055 continue;
4056 }
4057 tline = delete_Token(tline);
4058 continue;
4059 }
4060 } else {
4061 /*
4062 * Complicated case: at least one macro with this name
H. Peter Anvine2c80182005-01-15 22:15:51 +00004063 * exists and takes parameters. We must find the
4064 * parameters in the call, count them, find the SMacro
4065 * that corresponds to that form of the macro call, and
4066 * substitute for the parameters when we expand. What a
4067 * pain.
4068 */
4069 /*tline = tline->next;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004070 skip_white_(tline); */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004071 do {
4072 t = tline->next;
4073 while (tok_type_(t, TOK_SMAC_END)) {
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004074 t->a.mac->in_progress = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004075 t->text = NULL;
4076 t = tline->next = delete_Token(t);
4077 }
4078 tline = t;
4079 } while (tok_type_(tline, TOK_WHITESPACE));
4080 if (!tok_is_(tline, "(")) {
4081 /*
4082 * This macro wasn't called with parameters: ignore
4083 * the call. (Behaviour borrowed from gnu cpp.)
4084 */
4085 tline = mstart;
4086 m = NULL;
4087 } else {
4088 int paren = 0;
4089 int white = 0;
4090 brackets = 0;
4091 nparam = 0;
4092 sparam = PARAM_DELTA;
4093 params = nasm_malloc(sparam * sizeof(Token *));
4094 params[0] = tline->next;
4095 paramsize = nasm_malloc(sparam * sizeof(int));
4096 paramsize[0] = 0;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07004097 while (true) { /* parameter loop */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004098 /*
4099 * For some unusual expansions
4100 * which concatenates function call
4101 */
4102 t = tline->next;
4103 while (tok_type_(t, TOK_SMAC_END)) {
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004104 t->a.mac->in_progress = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004105 t->text = NULL;
4106 t = tline->next = delete_Token(t);
4107 }
4108 tline = t;
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00004109
H. Peter Anvine2c80182005-01-15 22:15:51 +00004110 if (!tline) {
4111 error(ERR_NONFATAL,
4112 "macro call expects terminating `)'");
4113 break;
4114 }
4115 if (tline->type == TOK_WHITESPACE
4116 && brackets <= 0) {
4117 if (paramsize[nparam])
4118 white++;
4119 else
4120 params[nparam] = tline->next;
4121 continue; /* parameter loop */
4122 }
4123 if (tline->type == TOK_OTHER
4124 && tline->text[1] == 0) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004125 char ch = tline->text[0];
H. Peter Anvine2c80182005-01-15 22:15:51 +00004126 if (ch == ',' && !paren && brackets <= 0) {
4127 if (++nparam >= sparam) {
4128 sparam += PARAM_DELTA;
4129 params = nasm_realloc(params,
Cyrill Gorcunoved4a8052010-04-09 15:40:35 +04004130 sparam * sizeof(Token *));
4131 paramsize = nasm_realloc(paramsize,
4132 sparam * sizeof(int));
H. Peter Anvine2c80182005-01-15 22:15:51 +00004133 }
4134 params[nparam] = tline->next;
4135 paramsize[nparam] = 0;
4136 white = 0;
4137 continue; /* parameter loop */
4138 }
4139 if (ch == '{' &&
4140 (brackets > 0 || (brackets == 0 &&
4141 !paramsize[nparam])))
4142 {
4143 if (!(brackets++)) {
4144 params[nparam] = tline->next;
4145 continue; /* parameter loop */
4146 }
4147 }
4148 if (ch == '}' && brackets > 0)
4149 if (--brackets == 0) {
4150 brackets = -1;
4151 continue; /* parameter loop */
4152 }
4153 if (ch == '(' && !brackets)
4154 paren++;
4155 if (ch == ')' && brackets <= 0)
4156 if (--paren < 0)
4157 break;
4158 }
4159 if (brackets < 0) {
4160 brackets = 0;
4161 error(ERR_NONFATAL, "braces do not "
4162 "enclose all of macro parameter");
4163 }
4164 paramsize[nparam] += white + 1;
4165 white = 0;
4166 } /* parameter loop */
4167 nparam++;
4168 while (m && (m->nparam != nparam ||
4169 mstrcmp(m->name, mname,
4170 m->casesense)))
4171 m = m->next;
4172 if (!m)
H. Peter Anvin917a3492008-09-24 09:14:49 -07004173 error(ERR_WARNING|ERR_PASS1|ERR_WARN_MNP,
H. Peter Anvine2c80182005-01-15 22:15:51 +00004174 "macro `%s' exists, "
4175 "but not taking %d parameters",
4176 mstart->text, nparam);
4177 }
4178 }
4179 if (m && m->in_progress)
4180 m = NULL;
4181 if (!m) { /* in progess or didn't find '(' or wrong nparam */
H. Peter Anvin70653092007-10-19 14:42:29 -07004182 /*
H. Peter Anvine2c80182005-01-15 22:15:51 +00004183 * Design question: should we handle !tline, which
4184 * indicates missing ')' here, or expand those
4185 * macros anyway, which requires the (t) test a few
H. Peter Anvin70653092007-10-19 14:42:29 -07004186 * lines down?
H. Peter Anvine2c80182005-01-15 22:15:51 +00004187 */
4188 nasm_free(params);
4189 nasm_free(paramsize);
4190 tline = mstart;
4191 } else {
4192 /*
4193 * Expand the macro: we are placed on the last token of the
4194 * call, so that we can easily split the call from the
4195 * following tokens. We also start by pushing an SMAC_END
4196 * token for the cycle removal.
4197 */
4198 t = tline;
4199 if (t) {
4200 tline = t->next;
4201 t->next = NULL;
4202 }
4203 tt = new_Token(tline, TOK_SMAC_END, NULL, 0);
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004204 tt->a.mac = m;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07004205 m->in_progress = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004206 tline = tt;
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04004207 list_for_each(t, m->expansion) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004208 if (t->type >= TOK_SMAC_PARAM) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00004209 Token *pcopy = tline, **ptail = &pcopy;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004210 Token *ttt, *pt;
4211 int i;
H. Peter Anvin734b1882002-04-30 21:01:08 +00004212
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004213 ttt = params[t->type - TOK_SMAC_PARAM];
4214 i = paramsize[t->type - TOK_SMAC_PARAM];
4215 while (--i >= 0) {
4216 pt = *ptail = new_Token(tline, ttt->type,
4217 ttt->text, 0);
4218 ptail = &pt->next;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004219 ttt = ttt->next;
4220 }
4221 tline = pcopy;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004222 } else if (t->type == TOK_PREPROC_Q) {
4223 tt = new_Token(tline, TOK_ID, mname, 0);
4224 tline = tt;
4225 } else if (t->type == TOK_PREPROC_QQ) {
4226 tt = new_Token(tline, TOK_ID, m->name, 0);
4227 tline = tt;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004228 } else {
4229 tt = new_Token(tline, t->type, t->text, 0);
4230 tline = tt;
4231 }
4232 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00004233
H. Peter Anvine2c80182005-01-15 22:15:51 +00004234 /*
4235 * Having done that, get rid of the macro call, and clean
4236 * up the parameters.
4237 */
4238 nasm_free(params);
4239 nasm_free(paramsize);
4240 free_tlist(mstart);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004241 expanded = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004242 continue; /* main token loop */
4243 }
4244 }
4245 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004246
H. Peter Anvine2c80182005-01-15 22:15:51 +00004247 if (tline->type == TOK_SMAC_END) {
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004248 tline->a.mac->in_progress = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004249 tline = delete_Token(tline);
4250 } else {
4251 t = *tail = tline;
4252 tline = tline->next;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004253 t->a.mac = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004254 t->next = NULL;
4255 tail = &t->next;
4256 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004257 }
4258
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004259 /*
4260 * Now scan the entire line and look for successive TOK_IDs that resulted
Keith Kaniosb7a89542007-04-12 02:40:54 +00004261 * after expansion (they can't be produced by tokenize()). The successive
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004262 * TOK_IDs should be concatenated.
4263 * Also we look for %+ tokens and concatenate the tokens before and after
4264 * them (without white spaces in between).
4265 */
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +04004266 if (expanded) {
Cyrill Gorcunovc6a742c2011-06-27 01:23:09 +04004267 const struct tokseq_match t[] = {
4268 {
4269 PP_CONCAT_MASK(TOK_ID) |
4270 PP_CONCAT_MASK(TOK_PREPROC_ID), /* head */
4271 PP_CONCAT_MASK(TOK_ID) |
4272 PP_CONCAT_MASK(TOK_PREPROC_ID) |
4273 PP_CONCAT_MASK(TOK_NUMBER) /* tail */
4274 }
4275 };
4276 if (paste_tokens(&thead, t, ARRAY_SIZE(t), true)) {
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +04004277 /*
4278 * If we concatenated something, *and* we had previously expanded
4279 * an actual macro, scan the lines again for macros...
4280 */
4281 tline = thead;
4282 expanded = false;
4283 goto again;
4284 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00004285 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004286
Cyrill Gorcunovbd38c8f2009-11-21 11:11:23 +03004287err:
H. Peter Anvine2c80182005-01-15 22:15:51 +00004288 if (org_tline) {
4289 if (thead) {
4290 *org_tline = *thead;
4291 /* since we just gave text to org_line, don't free it */
4292 thead->text = NULL;
4293 delete_Token(thead);
4294 } else {
4295 /* the expression expanded to empty line;
4296 we can't return NULL for some reasons
4297 we just set the line to a single WHITESPACE token. */
4298 memset(org_tline, 0, sizeof(*org_tline));
4299 org_tline->text = NULL;
4300 org_tline->type = TOK_WHITESPACE;
4301 }
4302 thead = org_tline;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004303 }
4304
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004305 return thead;
4306}
4307
4308/*
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004309 * Similar to expand_smacro but used exclusively with macro identifiers
4310 * right before they are fetched in. The reason is that there can be
4311 * identifiers consisting of several subparts. We consider that if there
4312 * are more than one element forming the name, user wants a expansion,
4313 * otherwise it will be left as-is. Example:
4314 *
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004315 * %define %$abc cde
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004316 *
4317 * the identifier %$abc will be left as-is so that the handler for %define
4318 * will suck it and define the corresponding value. Other case:
4319 *
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004320 * %define _%$abc cde
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004321 *
4322 * In this case user wants name to be expanded *before* %define starts
4323 * working, so we'll expand %$abc into something (if it has a value;
4324 * otherwise it will be left as-is) then concatenate all successive
4325 * PP_IDs into one.
4326 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004327static Token *expand_id(Token * tline)
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004328{
4329 Token *cur, *oldnext = NULL;
4330
H. Peter Anvin734b1882002-04-30 21:01:08 +00004331 if (!tline || !tline->next)
H. Peter Anvine2c80182005-01-15 22:15:51 +00004332 return tline;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004333
4334 cur = tline;
4335 while (cur->next &&
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004336 (cur->next->type == TOK_ID ||
4337 cur->next->type == TOK_PREPROC_ID
4338 || cur->next->type == TOK_NUMBER))
H. Peter Anvine2c80182005-01-15 22:15:51 +00004339 cur = cur->next;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004340
4341 /* If identifier consists of just one token, don't expand */
4342 if (cur == tline)
H. Peter Anvine2c80182005-01-15 22:15:51 +00004343 return tline;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004344
H. Peter Anvine2c80182005-01-15 22:15:51 +00004345 if (cur) {
4346 oldnext = cur->next; /* Detach the tail past identifier */
4347 cur->next = NULL; /* so that expand_smacro stops here */
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004348 }
4349
H. Peter Anvin734b1882002-04-30 21:01:08 +00004350 tline = expand_smacro(tline);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004351
H. Peter Anvine2c80182005-01-15 22:15:51 +00004352 if (cur) {
4353 /* expand_smacro possibly changhed tline; re-scan for EOL */
4354 cur = tline;
4355 while (cur && cur->next)
4356 cur = cur->next;
4357 if (cur)
4358 cur->next = oldnext;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004359 }
4360
4361 return tline;
4362}
4363
4364/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004365 * Determine whether the given line constitutes a multi-line macro
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004366 * call, and return the MMacro structure called if so. Doesn't have
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004367 * to check for an initial label - that's taken care of in
4368 * expand_mmacro - but must check numbers of parameters. Guaranteed
4369 * to be called with tline->type == TOK_ID, so the putative macro
4370 * name is easy to find.
4371 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004372static MMacro *is_mmacro(Token * tline, Token *** params_array)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004373{
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004374 MMacro *head, *m;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004375 Token **params;
4376 int nparam;
4377
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004378 head = (MMacro *) hash_findix(&mmacros, tline->text);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004379
4380 /*
4381 * Efficiency: first we see if any macro exists with the given
4382 * name. If not, we can return NULL immediately. _Then_ we
4383 * count the parameters, and then we look further along the
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004384 * list if necessary to find the proper MMacro.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004385 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004386 list_for_each(m, head)
4387 if (!mstrcmp(m->name, tline->text, m->casesense))
H. Peter Anvine2c80182005-01-15 22:15:51 +00004388 break;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004389 if (!m)
H. Peter Anvine2c80182005-01-15 22:15:51 +00004390 return NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004391
4392 /*
4393 * OK, we have a potential macro. Count and demarcate the
4394 * parameters.
4395 */
H. Peter Anvin734b1882002-04-30 21:01:08 +00004396 count_mmac_params(tline->next, &nparam, &params);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004397
4398 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004399 * So we know how many parameters we've got. Find the MMacro
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004400 * structure that handles this number.
4401 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004402 while (m) {
4403 if (m->nparam_min <= nparam
4404 && (m->plus || nparam <= m->nparam_max)) {
4405 /*
4406 * This one is right. Just check if cycle removal
4407 * prohibits us using it before we actually celebrate...
4408 */
4409 if (m->in_progress > m->max_depth) {
4410 if (m->max_depth > 0) {
4411 error(ERR_WARNING,
4412 "reached maximum recursion depth of %i",
4413 m->max_depth);
4414 }
4415 nasm_free(params);
4416 return NULL;
4417 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00004418 /*
4419 * It's right, and we can use it. Add its default
4420 * parameters to the end of our list if necessary.
4421 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004422 if (m->defaults && nparam < m->nparam_min + m->ndefs) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00004423 params =
4424 nasm_realloc(params,
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004425 ((m->nparam_min + m->ndefs +
H. Peter Anvine2c80182005-01-15 22:15:51 +00004426 1) * sizeof(*params)));
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004427 while (nparam < m->nparam_min + m->ndefs) {
4428 params[nparam] = m->defaults[nparam - m->nparam_min];
H. Peter Anvine2c80182005-01-15 22:15:51 +00004429 nparam++;
4430 }
4431 }
4432 /*
4433 * If we've gone over the maximum parameter count (and
4434 * we're in Plus mode), ignore parameters beyond
4435 * nparam_max.
4436 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004437 if (m->plus && nparam > m->nparam_max)
4438 nparam = m->nparam_max;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004439 /*
4440 * Then terminate the parameter list, and leave.
4441 */
4442 if (!params) { /* need this special case */
4443 params = nasm_malloc(sizeof(*params));
4444 nparam = 0;
4445 }
4446 params[nparam] = NULL;
4447 *params_array = params;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004448 return m;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004449 }
4450 /*
4451 * This one wasn't right: look for the next one with the
4452 * same name.
4453 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004454 list_for_each(m, m->next)
4455 if (!mstrcmp(m->name, tline->text, m->casesense))
H. Peter Anvine2c80182005-01-15 22:15:51 +00004456 break;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004457 }
4458
4459 /*
4460 * After all that, we didn't find one with the right number of
4461 * parameters. Issue a warning, and fail to expand the macro.
4462 */
H. Peter Anvin917a3492008-09-24 09:14:49 -07004463 error(ERR_WARNING|ERR_PASS1|ERR_WARN_MNP,
H. Peter Anvine2c80182005-01-15 22:15:51 +00004464 "macro `%s' exists, but not taking %d parameters",
4465 tline->text, nparam);
H. Peter Anvin734b1882002-04-30 21:01:08 +00004466 nasm_free(params);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004467 return NULL;
4468}
4469
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004470
4471/*
4472 * Save MMacro invocation specific fields in
4473 * preparation for a recursive macro expansion
4474 */
4475static void push_mmacro(MMacro *m)
4476{
4477 MMacroInvocation *i;
4478
4479 i = nasm_malloc(sizeof(MMacroInvocation));
4480 i->prev = m->prev;
4481 i->params = m->params;
4482 i->iline = m->iline;
4483 i->nparam = m->nparam;
4484 i->rotate = m->rotate;
4485 i->paramlen = m->paramlen;
4486 i->unique = m->unique;
4487 i->condcnt = m->condcnt;
4488 m->prev = i;
4489}
4490
4491
4492/*
4493 * Restore MMacro invocation specific fields that were
4494 * saved during a previous recursive macro expansion
4495 */
4496static void pop_mmacro(MMacro *m)
4497{
4498 MMacroInvocation *i;
4499
4500 if (m->prev) {
4501 i = m->prev;
4502 m->prev = i->prev;
4503 m->params = i->params;
4504 m->iline = i->iline;
4505 m->nparam = i->nparam;
4506 m->rotate = i->rotate;
4507 m->paramlen = i->paramlen;
4508 m->unique = i->unique;
4509 m->condcnt = i->condcnt;
4510 nasm_free(i);
4511 }
4512}
4513
4514
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004515/*
4516 * Expand the multi-line macro call made by the given line, if
4517 * there is one to be expanded. If there is, push the expansion on
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004518 * istk->expansion and return 1. Otherwise return 0.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004519 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004520static int expand_mmacro(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004521{
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004522 Token *startline = tline;
H. Peter Anvineba20a72002-04-30 20:53:55 +00004523 Token *label = NULL;
4524 int dont_prepend = 0;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004525 Token **params, *t, *tt;
4526 MMacro *m;
4527 Line *l, *ll;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004528 int i, nparam, *paramlen;
H. Peter Anvinc751e862008-06-09 10:18:45 -07004529 const char *mname;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004530
4531 t = tline;
H. Peter Anvineba20a72002-04-30 20:53:55 +00004532 skip_white_(t);
H. Peter Anvince2233b2008-05-25 21:57:00 -07004533 /* if (!tok_type_(t, TOK_ID)) Lino 02/25/02 */
H. Peter Anvindce1e2f2002-04-30 21:06:37 +00004534 if (!tok_type_(t, TOK_ID) && !tok_type_(t, TOK_PREPROC_ID))
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004535 return 0;
4536 m = is_mmacro(t, &params);
4537 if (m) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004538 mname = t->text;
H. Peter Anvinc751e862008-06-09 10:18:45 -07004539 } else {
H. Peter Anvine2c80182005-01-15 22:15:51 +00004540 Token *last;
4541 /*
4542 * We have an id which isn't a macro call. We'll assume
4543 * it might be a label; we'll also check to see if a
4544 * colon follows it. Then, if there's another id after
4545 * that lot, we'll check it again for macro-hood.
4546 */
4547 label = last = t;
4548 t = t->next;
4549 if (tok_type_(t, TOK_WHITESPACE))
4550 last = t, t = t->next;
4551 if (tok_is_(t, ":")) {
4552 dont_prepend = 1;
4553 last = t, t = t->next;
4554 if (tok_type_(t, TOK_WHITESPACE))
4555 last = t, t = t->next;
4556 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004557 if (!tok_type_(t, TOK_ID) || !(m = is_mmacro(t, &params)))
4558 return 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004559 last->next = NULL;
Keith Kanios891775e2009-07-11 06:08:54 -05004560 mname = t->text;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004561 tline = t;
H. Peter Anvineba20a72002-04-30 20:53:55 +00004562 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004563
4564 /*
4565 * Fix up the parameters: this involves stripping leading and
4566 * trailing whitespace, then stripping braces if they are
4567 * present.
4568 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004569 for (nparam = 0; params[nparam]; nparam++) ;
H. Peter Anvin734b1882002-04-30 21:01:08 +00004570 paramlen = nparam ? nasm_malloc(nparam * sizeof(*paramlen)) : NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004571
H. Peter Anvine2c80182005-01-15 22:15:51 +00004572 for (i = 0; params[i]; i++) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07004573 int brace = false;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004574 int comma = (!m->plus || i < nparam - 1);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004575
H. Peter Anvine2c80182005-01-15 22:15:51 +00004576 t = params[i];
4577 skip_white_(t);
4578 if (tok_is_(t, "{"))
H. Peter Anvin6867acc2007-10-10 14:58:45 -07004579 t = t->next, brace = true, comma = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004580 params[i] = t;
4581 paramlen[i] = 0;
4582 while (t) {
4583 if (comma && t->type == TOK_OTHER && !strcmp(t->text, ","))
4584 break; /* ... because we have hit a comma */
4585 if (comma && t->type == TOK_WHITESPACE
4586 && tok_is_(t->next, ","))
4587 break; /* ... or a space then a comma */
4588 if (brace && t->type == TOK_OTHER && !strcmp(t->text, "}"))
4589 break; /* ... or a brace */
4590 t = t->next;
4591 paramlen[i]++;
4592 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004593 }
4594
4595 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004596 * OK, we have a MMacro structure together with a set of
4597 * parameters. We must now go through the expansion and push
4598 * copies of each Line on to istk->expansion. Substitution of
H. Peter Anvin76690a12002-04-30 20:52:49 +00004599 * parameter tokens and macro-local tokens doesn't get done
4600 * until the single-line macro substitution process; this is
4601 * because delaying them allows us to change the semantics
4602 * later through %rotate.
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004603 *
4604 * First, push an end marker on to istk->expansion, mark this
4605 * macro as in progress, and set up its invocation-specific
4606 * variables.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004607 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004608 ll = nasm_malloc(sizeof(Line));
4609 ll->next = istk->expansion;
4610 ll->finishes = m;
4611 ll->first = NULL;
4612 istk->expansion = ll;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004613
4614 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004615 * Save the previous MMacro expansion in the case of
4616 * macro recursion
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004617 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004618 if (m->max_depth && m->in_progress)
4619 push_mmacro(m);
4620
4621 m->in_progress ++;
4622 m->params = params;
4623 m->iline = tline;
4624 m->nparam = nparam;
4625 m->rotate = 0;
4626 m->paramlen = paramlen;
4627 m->unique = unique++;
4628 m->lineno = 0;
4629 m->condcnt = 0;
4630
4631 m->next_active = istk->mstk;
4632 istk->mstk = m;
4633
4634 list_for_each(l, m->expansion) {
4635 Token **tail;
4636
4637 ll = nasm_malloc(sizeof(Line));
4638 ll->finishes = NULL;
4639 ll->next = istk->expansion;
4640 istk->expansion = ll;
4641 tail = &ll->first;
4642
4643 list_for_each(t, l->first) {
4644 Token *x = t;
4645 switch (t->type) {
4646 case TOK_PREPROC_Q:
4647 tt = *tail = new_Token(NULL, TOK_ID, mname, 0);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004648 break;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004649 case TOK_PREPROC_QQ:
4650 tt = *tail = new_Token(NULL, TOK_ID, m->name, 0);
4651 break;
4652 case TOK_PREPROC_ID:
4653 if (t->text[1] == '0' && t->text[2] == '0') {
4654 dont_prepend = -1;
4655 x = label;
4656 if (!x)
4657 continue;
4658 }
4659 /* fall through */
4660 default:
4661 tt = *tail = new_Token(NULL, x->type, x->text, 0);
4662 break;
4663 }
4664 tail = &tt->next;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004665 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004666 *tail = NULL;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004667 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004668
4669 /*
H. Peter Anvineba20a72002-04-30 20:53:55 +00004670 * If we had a label, push it on as the first line of
4671 * the macro expansion.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004672 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004673 if (label) {
4674 if (dont_prepend < 0)
4675 free_tlist(startline);
4676 else {
4677 ll = nasm_malloc(sizeof(Line));
4678 ll->finishes = NULL;
4679 ll->next = istk->expansion;
4680 istk->expansion = ll;
4681 ll->first = startline;
4682 if (!dont_prepend) {
4683 while (label->next)
4684 label = label->next;
4685 label->next = tt = new_Token(NULL, TOK_OTHER, ":", 0);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004686 }
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004687 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004688 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004689
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004690 list->uplevel(m->nolist ? LIST_MACRO_NOLIST : LIST_MACRO);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004691
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004692 return 1;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004693}
4694
Victor van den Elzen3b404c02008-09-18 13:51:36 +02004695/* The function that actually does the error reporting */
4696static void verror(int severity, const char *fmt, va_list arg)
4697{
4698 char buff[1024];
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004699 MMacro *mmac = NULL;
4700 int delta = 0;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02004701
4702 vsnprintf(buff, sizeof(buff), fmt, arg);
4703
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004704 /* get %macro name */
4705 if (istk && istk->mstk) {
4706 mmac = istk->mstk;
4707 /* but %rep blocks should be skipped */
4708 while (mmac && !mmac->name)
4709 mmac = mmac->next_active, delta++;
Cyrill Gorcunov9900c6b2011-10-09 18:58:46 +04004710 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004711
4712 if (mmac)
4713 nasm_error(severity, "(%s:%d) %s",
4714 mmac->name, mmac->lineno - delta, buff);
4715 else
4716 nasm_error(severity, "%s", buff);
Victor van den Elzen3b404c02008-09-18 13:51:36 +02004717}
4718
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004719/*
4720 * Since preprocessor always operate only on the line that didn't
H. Peter Anvin917a3492008-09-24 09:14:49 -07004721 * arrived yet, we should always use ERR_OFFBY1.
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004722 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004723static void error(int severity, const char *fmt, ...)
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004724{
4725 va_list arg;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004726
4727 /* If we're in a dead branch of IF or something like it, ignore the error */
4728 if (istk && istk->conds && !emitting(istk->conds->state))
4729 return;
4730
H. Peter Anvin734b1882002-04-30 21:01:08 +00004731 va_start(arg, fmt);
Victor van den Elzen3b404c02008-09-18 13:51:36 +02004732 verror(severity, fmt, arg);
H. Peter Anvin734b1882002-04-30 21:01:08 +00004733 va_end(arg);
Victor van den Elzen3b404c02008-09-18 13:51:36 +02004734}
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004735
Victor van den Elzen3b404c02008-09-18 13:51:36 +02004736/*
4737 * Because %else etc are evaluated in the state context
4738 * of the previous branch, errors might get lost with error():
4739 * %if 0 ... %else trailing garbage ... %endif
4740 * So %else etc should report errors with this function.
4741 */
4742static void error_precond(int severity, const char *fmt, ...)
4743{
4744 va_list arg;
4745
4746 /* Only ignore the error if it's really in a dead branch */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004747 if (istk && istk->conds && istk->conds->state == COND_NEVER)
Victor van den Elzen3b404c02008-09-18 13:51:36 +02004748 return;
4749
4750 va_start(arg, fmt);
4751 verror(severity, fmt, arg);
4752 va_end(arg);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004753}
4754
H. Peter Anvin734b1882002-04-30 21:01:08 +00004755static void
H. Peter Anvindbb640b2009-07-18 18:57:16 -07004756pp_reset(char *file, int apass, ListGen * listgen, StrList **deplist)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004757{
H. Peter Anvin7383b402008-09-24 10:20:40 -07004758 Token *t;
4759
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004760 cstk = NULL;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004761 istk = nasm_malloc(sizeof(Include));
4762 istk->next = NULL;
4763 istk->conds = NULL;
4764 istk->expansion = NULL;
4765 istk->mstk = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004766 istk->fp = fopen(file, "r");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004767 istk->fname = NULL;
H. Peter Anvineba20a72002-04-30 20:53:55 +00004768 src_set_fname(nasm_strdup(file));
4769 src_set_linnum(0);
4770 istk->lineinc = 1;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004771 if (!istk->fp)
H. Peter Anvin917a3492008-09-24 09:14:49 -07004772 error(ERR_FATAL|ERR_NOFILE, "unable to open input file `%s'",
H. Peter Anvine2c80182005-01-15 22:15:51 +00004773 file);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004774 defining = NULL;
Charles Crayned4200be2008-07-12 16:42:33 -07004775 nested_mac_count = 0;
4776 nested_rep_count = 0;
H. Peter Anvin97a23472007-09-16 17:57:25 -07004777 init_macros();
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004778 unique = 0;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004779 if (tasm_compatible_mode) {
H. Peter Anvina4835d42008-05-20 14:21:29 -07004780 stdmacpos = nasm_stdmac;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004781 } else {
H. Peter Anvina4835d42008-05-20 14:21:29 -07004782 stdmacpos = nasm_stdmac_after_tasm;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004783 }
H. Peter Anvind2456592008-06-19 15:04:18 -07004784 any_extrastdmac = extrastdmac && *extrastdmac;
4785 do_predef = true;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004786 list = listgen;
H. Peter Anvin61f130f2008-09-25 15:45:06 -07004787
4788 /*
4789 * 0 for dependencies, 1 for preparatory passes, 2 for final pass.
4790 * The caller, however, will also pass in 3 for preprocess-only so
4791 * we can set __PASS__ accordingly.
4792 */
4793 pass = apass > 2 ? 2 : apass;
4794
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07004795 dephead = deptail = deplist;
4796 if (deplist) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004797 StrList *sl = nasm_malloc(strlen(file)+1+sizeof sl->next);
4798 sl->next = NULL;
4799 strcpy(sl->str, file);
4800 *deptail = sl;
4801 deptail = &sl->next;
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07004802 }
H. Peter Anvin7383b402008-09-24 10:20:40 -07004803
H. Peter Anvin61f130f2008-09-25 15:45:06 -07004804 /*
4805 * Define the __PASS__ macro. This is defined here unlike
4806 * all the other builtins, because it is special -- it varies between
4807 * passes.
4808 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004809 t = nasm_malloc(sizeof(*t));
4810 t->next = NULL;
H. Peter Anvin61f130f2008-09-25 15:45:06 -07004811 make_tok_num(t, apass);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004812 t->a.mac = NULL;
H. Peter Anvin7383b402008-09-24 10:20:40 -07004813 define_smacro(NULL, "__PASS__", true, 0, t);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004814}
4815
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004816static char *pp_getline(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004817{
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004818 char *line;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004819 Token *tline;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004820
H. Peter Anvine2c80182005-01-15 22:15:51 +00004821 while (1) {
4822 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004823 * Fetch a tokenized line, either from the macro-expansion
H. Peter Anvine2c80182005-01-15 22:15:51 +00004824 * buffer or from the input file.
4825 */
4826 tline = NULL;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004827 while (istk->expansion && istk->expansion->finishes) {
4828 Line *l = istk->expansion;
4829 if (!l->finishes->name && l->finishes->in_progress > 1) {
4830 Line *ll;
H. Peter Anvineba20a72002-04-30 20:53:55 +00004831
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004832 /*
4833 * This is a macro-end marker for a macro with no
4834 * name, which means it's not really a macro at all
4835 * but a %rep block, and the `in_progress' field is
4836 * more than 1, meaning that we still need to
4837 * repeat. (1 means the natural last repetition; 0
4838 * means termination by %exitrep.) We have
4839 * therefore expanded up to the %endrep, and must
4840 * push the whole block on to the expansion buffer
4841 * again. We don't bother to remove the macro-end
4842 * marker: we'd only have to generate another one
4843 * if we did.
4844 */
4845 l->finishes->in_progress--;
4846 list_for_each(l, l->finishes->expansion) {
4847 Token *t, *tt, **tail;
4848
4849 ll = nasm_malloc(sizeof(Line));
4850 ll->next = istk->expansion;
4851 ll->finishes = NULL;
4852 ll->first = NULL;
4853 tail = &ll->first;
4854
4855 list_for_each(t, l->first) {
4856 if (t->text || t->type == TOK_WHITESPACE) {
4857 tt = *tail = new_Token(NULL, t->type, t->text, 0);
4858 tail = &tt->next;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004859 }
4860 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004861
4862 istk->expansion = ll;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004863 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004864 } else {
4865 /*
4866 * Check whether a `%rep' was started and not ended
4867 * within this macro expansion. This can happen and
4868 * should be detected. It's a fatal error because
4869 * I'm too confused to work out how to recover
4870 * sensibly from it.
4871 */
4872 if (defining) {
4873 if (defining->name)
4874 error(ERR_PANIC,
4875 "defining with name in expansion");
4876 else if (istk->mstk->name)
4877 error(ERR_FATAL,
4878 "`%%rep' without `%%endrep' within"
4879 " expansion of macro `%s'",
4880 istk->mstk->name);
4881 }
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004882
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004883 /*
4884 * FIXME: investigate the relationship at this point between
4885 * istk->mstk and l->finishes
4886 */
4887 {
4888 MMacro *m = istk->mstk;
4889 istk->mstk = m->next_active;
4890 if (m->name) {
4891 /*
4892 * This was a real macro call, not a %rep, and
4893 * therefore the parameter information needs to
4894 * be freed.
4895 */
4896 if (m->prev) {
4897 pop_mmacro(m);
4898 l->finishes->in_progress --;
4899 } else {
4900 nasm_free(m->params);
4901 free_tlist(m->iline);
4902 nasm_free(m->paramlen);
4903 l->finishes->in_progress = 0;
4904 }
4905 } else
4906 free_mmacro(m);
4907 }
4908 istk->expansion = l->next;
4909 nasm_free(l);
4910 list->downlevel(LIST_MACRO);
4911 }
4912 }
4913 while (1) { /* until we get a line we can use */
4914
4915 if (istk->expansion) { /* from a macro expansion */
4916 char *p;
4917 Line *l = istk->expansion;
4918 if (istk->mstk)
4919 istk->mstk->lineno++;
4920 tline = l->first;
4921 istk->expansion = l->next;
4922 nasm_free(l);
4923 p = detoken(tline, false);
4924 list->line(LIST_MACRO, p);
4925 nasm_free(p);
4926 break;
4927 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00004928 line = read_line();
4929 if (line) { /* from the current input file */
4930 line = prepreproc(line);
Keith Kaniosb7a89542007-04-12 02:40:54 +00004931 tline = tokenize(line);
H. Peter Anvine2c80182005-01-15 22:15:51 +00004932 nasm_free(line);
4933 break;
4934 }
4935 /*
4936 * The current file has ended; work down the istk
4937 */
4938 {
4939 Include *i = istk;
4940 fclose(i->fp);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004941 if (i->conds) {
4942 /* nasm_error can't be conditionally suppressed */
4943 nasm_error(ERR_FATAL,
4944 "expected `%%endif' before end of file");
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004945 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00004946 /* only set line and file name if there's a next node */
4947 if (i->next) {
4948 src_set_linnum(i->lineno);
Cyrill Gorcunovd34a1082011-03-07 11:23:08 +03004949 nasm_free(src_set_fname(nasm_strdup(i->fname)));
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004950 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00004951 istk = i->next;
4952 list->downlevel(LIST_INCLUDE);
4953 nasm_free(i);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004954 if (!istk)
4955 return NULL;
4956 if (istk->expansion && istk->expansion->finishes)
4957 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004958 }
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004959 }
4960
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004961 /*
4962 * We must expand MMacro parameters and MMacro-local labels
4963 * _before_ we plunge into directive processing, to cope
4964 * with things like `%define something %1' such as STRUC
4965 * uses. Unless we're _defining_ a MMacro, in which case
4966 * those tokens should be left alone to go into the
4967 * definition; and unless we're in a non-emitting
4968 * condition, in which case we don't want to meddle with
4969 * anything.
4970 */
4971 if (!defining && !(istk->conds && !emitting(istk->conds->state))
4972 && !(istk->mstk && !istk->mstk->in_progress)) {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004973 tline = expand_mmac_params(tline);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004974 }
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004975
H. Peter Anvine2c80182005-01-15 22:15:51 +00004976 /*
4977 * Check the line to see if it's a preprocessor directive.
4978 */
4979 if (do_directive(tline) == DIRECTIVE_FOUND) {
4980 continue;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004981 } else if (defining) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00004982 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004983 * We're defining a multi-line macro. We emit nothing
4984 * at all, and just
4985 * shove the tokenized line on to the macro definition.
H. Peter Anvine2c80182005-01-15 22:15:51 +00004986 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004987 Line *l = nasm_malloc(sizeof(Line));
4988 l->next = defining->expansion;
4989 l->first = tline;
4990 l->finishes = NULL;
4991 defining->expansion = l;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004992 continue;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004993 } else if (istk->conds && !emitting(istk->conds->state)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00004994 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004995 * We're in a non-emitting branch of a condition block.
H. Peter Anvine2c80182005-01-15 22:15:51 +00004996 * Emit nothing at all, not even a blank line: when we
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004997 * emerge from the condition we'll give a line-number
H. Peter Anvine2c80182005-01-15 22:15:51 +00004998 * directive so we keep our place correctly.
4999 */
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005000 free_tlist(tline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00005001 continue;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005002 } else if (istk->mstk && !istk->mstk->in_progress) {
5003 /*
5004 * We're in a %rep block which has been terminated, so
5005 * we're walking through to the %endrep without
5006 * emitting anything. Emit nothing at all, not even a
5007 * blank line: when we emerge from the %rep block we'll
5008 * give a line-number directive so we keep our place
5009 * correctly.
5010 */
5011 free_tlist(tline);
5012 continue;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005013 } else {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005014 tline = expand_smacro(tline);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005015 if (!expand_mmacro(tline)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00005016 /*
Keith Kaniosb7a89542007-04-12 02:40:54 +00005017 * De-tokenize the line again, and emit it.
H. Peter Anvine2c80182005-01-15 22:15:51 +00005018 */
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005019 line = detoken(tline, true);
5020 free_tlist(tline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00005021 break;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005022 } else {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005023 continue; /* expand_mmacro calls free_tlist */
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005024 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00005025 }
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005026 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005027
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005028 return line;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005029}
5030
H. Peter Anvine2c80182005-01-15 22:15:51 +00005031static void pp_cleanup(int pass)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005032{
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005033 if (defining) {
5034 if (defining->name) {
5035 error(ERR_NONFATAL,
5036 "end of file while still defining macro `%s'",
5037 defining->name);
5038 } else {
5039 error(ERR_NONFATAL, "end of file while still in %%rep");
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005040 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005041
5042 free_mmacro(defining);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005043 defining = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005044 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005045 while (cstk)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005046 ctx_pop();
H. Peter Anvin97a23472007-09-16 17:57:25 -07005047 free_macros();
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005048 while (istk) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00005049 Include *i = istk;
5050 istk = istk->next;
5051 fclose(i->fp);
5052 nasm_free(i->fname);
Cyrill Gorcunov8dcfd882011-03-03 09:18:56 +03005053 nasm_free(i);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005054 }
5055 while (cstk)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005056 ctx_pop();
H. Peter Anvin86877b22008-06-20 15:55:45 -07005057 nasm_free(src_set_fname(NULL));
H. Peter Anvine2c80182005-01-15 22:15:51 +00005058 if (pass == 0) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03005059 IncPath *i;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005060 free_llist(predef);
5061 delete_Blocks();
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03005062 while ((i = ipath)) {
5063 ipath = i->next;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005064 if (i->path)
5065 nasm_free(i->path);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03005066 nasm_free(i);
5067 }
H. Peter Anvin11dfa1a2008-07-02 18:11:04 -07005068 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005069}
5070
Keith Kaniosa6dfa782007-04-13 16:47:53 +00005071void pp_include_path(char *path)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005072{
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005073 IncPath *i;
H. Peter Anvin37a321f2007-09-24 13:41:58 -07005074
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005075 i = nasm_malloc(sizeof(IncPath));
5076 i->path = path ? nasm_strdup(path) : NULL;
5077 i->next = NULL;
Frank Kotlerd0ed6fd2003-08-27 11:33:56 +00005078
H. Peter Anvin89cee572009-07-15 09:16:54 -04005079 if (ipath) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00005080 IncPath *j = ipath;
H. Peter Anvin89cee572009-07-15 09:16:54 -04005081 while (j->next)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005082 j = j->next;
5083 j->next = i;
5084 } else {
5085 ipath = i;
Frank Kotlerd0ed6fd2003-08-27 11:33:56 +00005086 }
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005087}
Frank Kotlerd0ed6fd2003-08-27 11:33:56 +00005088
Keith Kaniosa6dfa782007-04-13 16:47:53 +00005089void pp_pre_include(char *fname)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005090{
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005091 Token *inc, *space, *name;
5092 Line *l;
5093
H. Peter Anvin734b1882002-04-30 21:01:08 +00005094 name = new_Token(NULL, TOK_INTERNAL_STRING, fname, 0);
5095 space = new_Token(name, TOK_WHITESPACE, NULL, 0);
5096 inc = new_Token(space, TOK_PREPROC_ID, "%include", 0);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005097
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005098 l = nasm_malloc(sizeof(Line));
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005099 l->next = predef;
5100 l->first = inc;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005101 l->finishes = NULL;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005102 predef = l;
5103}
5104
Keith Kaniosa6dfa782007-04-13 16:47:53 +00005105void pp_pre_define(char *definition)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005106{
5107 Token *def, *space;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005108 Line *l;
Keith Kaniosa6dfa782007-04-13 16:47:53 +00005109 char *equals;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005110
5111 equals = strchr(definition, '=');
H. Peter Anvin734b1882002-04-30 21:01:08 +00005112 space = new_Token(NULL, TOK_WHITESPACE, NULL, 0);
5113 def = new_Token(space, TOK_PREPROC_ID, "%define", 0);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005114 if (equals)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005115 *equals = ' ';
Keith Kaniosb7a89542007-04-12 02:40:54 +00005116 space->next = tokenize(definition);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005117 if (equals)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005118 *equals = '=';
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005119
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005120 l = nasm_malloc(sizeof(Line));
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005121 l->next = predef;
5122 l->first = def;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005123 l->finishes = NULL;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005124 predef = l;
5125}
5126
Keith Kaniosa6dfa782007-04-13 16:47:53 +00005127void pp_pre_undefine(char *definition)
H. Peter Anvin620515a2002-04-30 20:57:38 +00005128{
5129 Token *def, *space;
5130 Line *l;
5131
H. Peter Anvin734b1882002-04-30 21:01:08 +00005132 space = new_Token(NULL, TOK_WHITESPACE, NULL, 0);
5133 def = new_Token(space, TOK_PREPROC_ID, "%undef", 0);
Keith Kaniosb7a89542007-04-12 02:40:54 +00005134 space->next = tokenize(definition);
H. Peter Anvin620515a2002-04-30 20:57:38 +00005135
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005136 l = nasm_malloc(sizeof(Line));
H. Peter Anvin620515a2002-04-30 20:57:38 +00005137 l->next = predef;
5138 l->first = def;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005139 l->finishes = NULL;
H. Peter Anvin620515a2002-04-30 20:57:38 +00005140 predef = l;
5141}
5142
H. Peter Anvina70547f2008-07-19 21:44:26 -07005143void pp_extra_stdmac(macros_t *macros)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005144{
H. Peter Anvin76690a12002-04-30 20:52:49 +00005145 extrastdmac = macros;
5146}
5147
Keith Kaniosa5fc6462007-10-13 07:09:22 -07005148static void make_tok_num(Token * tok, int64_t val)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005149{
Keith Kaniosa6dfa782007-04-13 16:47:53 +00005150 char numbuf[20];
Keith Kaniosa5fc6462007-10-13 07:09:22 -07005151 snprintf(numbuf, sizeof(numbuf), "%"PRId64"", val);
H. Peter Anvineba20a72002-04-30 20:53:55 +00005152 tok->text = nasm_strdup(numbuf);
5153 tok->type = TOK_NUMBER;
5154}
5155
Cyrill Gorcunov86b2ad02011-07-01 10:38:25 +04005156struct preproc_ops nasmpp = {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005157 pp_reset,
5158 pp_getline,
5159 pp_cleanup
5160};