blob: dd52e1e13a4b7beb4dd706de36e14a50e7bb4bca [file] [log] [blame]
H. Peter Anvin9e6747c2009-06-28 17:13:04 -07001/* ----------------------------------------------------------------------- *
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002 *
3 * Copyright 1996-2010 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;
85typedef struct MMacro MMacro;
Keith Kanios891775e2009-07-11 06:08:54 -050086typedef 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;
92typedef 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 Anvind7ed89e2002-04-30 20:52:08 +0000108 SMacro *next;
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000109 char *name;
H. Peter Anvin70055962007-10-11 00:05:31 -0700110 bool casesense;
H. Peter Anvin16ed4382007-10-11 10:06:19 -0700111 bool in_progress;
H. Peter Anvin70055962007-10-11 00:05:31 -0700112 unsigned int nparam;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000113 Token *expansion;
114};
115
116/*
H. Peter Anvin76690a12002-04-30 20:52:49 +0000117 * 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.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000132 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000133struct MMacro {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000134 MMacro *next;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300135 MMacroInvocation *prev; /* previous invocation */
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000136 char *name;
H. Peter Anvin8cfdb9d2007-09-14 18:36:01 -0700137 int nparam_min, nparam_max;
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700138 bool casesense;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300139 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 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000143 Token *dlist; /* All defaults as one list */
144 Token **defaults; /* Parameter default pointers */
145 int ndefs; /* number of default parameters */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000146 Line *expansion;
H. Peter Anvin76690a12002-04-30 20:52:49 +0000147
148 MMacro *next_active;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000149 MMacro *rep_nest; /* used for nesting %rep */
150 Token **params; /* actual parameters */
151 Token *iline; /* invocation line */
H. Peter Anvin25a99342007-09-22 17:45:45 -0700152 unsigned int nparam, rotate;
153 int *paramlen;
H. Peter Anvinf8ba53e2007-10-11 10:11:57 -0700154 uint64_t unique;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000155 int lineno; /* Current line number on expansion */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300156 uint64_t condcnt; /* number of if blocks... */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000157};
158
Keith Kanios891775e2009-07-11 06:08:54 -0500159
160/* Store the definition of a multi-line macro, as defined in a
161 * previous recursive macro expansion.
162 */
163struct MMacroInvocation {
H. Peter Anvin89cee572009-07-15 09:16:54 -0400164 MMacroInvocation *prev; /* previous invocation */
165 Token **params; /* actual parameters */
166 Token *iline; /* invocation line */
Keith Kanios891775e2009-07-11 06:08:54 -0500167 unsigned int nparam, rotate;
168 int *paramlen;
169 uint64_t unique;
Keith Kanios3c0d91f2009-10-25 13:28:03 -0500170 uint64_t condcnt;
Keith Kanios891775e2009-07-11 06:08:54 -0500171};
172
173
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000174/*
175 * The context stack is composed of a linked list of these.
176 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000177struct Context {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000178 Context *next;
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000179 char *name;
H. Peter Anvin166c2472008-05-28 12:28:58 -0700180 struct hash_table localmac;
Keith Kaniosb7a89542007-04-12 02:40:54 +0000181 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 {
204 TOK_NONE = 0, TOK_WHITESPACE, TOK_COMMENT, TOK_ID,
205 TOK_PREPROC_ID, TOK_STRING,
H. Peter Anvin6c81f0a2008-05-25 21:46:17 -0700206 TOK_NUMBER, TOK_FLOAT, TOK_SMAC_END, TOK_OTHER,
207 TOK_INTERNAL_STRING,
208 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 Anvind7ed89e2002-04-30 20:52:08 +0000223 Token *next;
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000224 char *text;
H. Peter Anvinf26e0972008-07-01 21:26:27 -0700225 union {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300226 SMacro *mac; /* associated macro for TOK_SMAC_END */
227 size_t len; /* scratch length field */
228 } a; /* Auxiliary data */
H. Peter Anvinda10e7b2007-09-12 04:18:37 +0000229 enum pp_token_type type;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000230};
231
232/*
233 * Multi-line macro definitions are stored as a linked list of
234 * 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
239 * `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.
H. Peter Anvin76690a12002-04-30 20:52:49 +0000249 * 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 Anvind7ed89e2002-04-30 20:52:08 +0000255 Line *next;
256 MMacro *finishes;
257 Token *first;
258};
259
260/*
261 * 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 Anvind7ed89e2002-04-30 20:52:08 +0000265 Include *next;
266 FILE *fp;
267 Cond *conds;
268 Line *expansion;
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000269 char *fname;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000270 int lineno, lineinc;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300271 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 Anvin6768eb72002-04-30 20:52:26 +0000280 IncPath *next;
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000281 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 Anvine2c80182005-01-15 22:15:51 +0000291struct Cond {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000292 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 */
304 COND_IF_TRUE, COND_IF_FALSE,
305 /*
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 */
310 COND_ELSE_TRUE, COND_ELSE_FALSE,
311 /*
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 */
Victor van den Elzen3b404c02008-09-18 13:51:36 +0200320 COND_DONE, COND_NEVER
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000321};
322#define emitting(x) ( (x) == COND_IF_TRUE || (x) == COND_ELSE_TRUE )
323
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/*
Keith Kanios852f1ee2009-07-12 00:19:55 -0500331 * This define sets the upper limit for smacro and recursive mmacro
332 * expansions
333 */
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 Anvin76690a12002-04-30 20:52:49 +0000362/*
363 * Directive names.
364 */
H. Peter Anvin65747262002-05-07 00:10:05 +0000365/* If this is a an IF, ELIF, ELSE or ENDIF keyword */
H. Peter Anvin9bf0aa72007-09-12 02:12:07 +0000366static int is_condition(enum preproc_token arg)
H. Peter Anvin65747262002-05-07 00:10:05 +0000367{
H. Peter Anvinda10e7b2007-09-12 04:18:37 +0000368 return PP_IS_COND(arg) || (arg == PP_ELSE) || (arg == PP_ENDIF);
H. Peter Anvin65747262002-05-07 00:10:05 +0000369}
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000370
371/* 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 Anvin6768eb72002-04-30 20:52:26 +0000394static 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 Anvin6768eb72002-04-30 20:52:26 +0000401static 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 Anvind7ed89e2002-04-30 20:52:08 +0000407 * The current set of multi-line macros we have defined.
408 */
H. Peter Anvin166c2472008-05-28 12:28:58 -0700409static 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 Anvin76690a12002-04-30 20:52:49 +0000417 * 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 */
420static MMacro *defining;
421
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 Anvina70547f2008-07-19 21:44:26 -0700440static 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
447static 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
453static Blocks blocks = { NULL, NULL };
H. Peter Anvin734b1882002-04-30 21:01:08 +0000454
455/*
H. Peter Anvin76690a12002-04-30 20:52:49 +0000456 * Forward declarations.
457 */
H. Peter Anvin734b1882002-04-30 21:01:08 +0000458static Token *expand_mmac_params(Token * tline);
459static Token *expand_smacro(Token * tline);
460static Token *expand_id(Token * tline);
H. Peter Anvinf8ad5322009-02-21 17:55:08 -0800461static Context *get_ctx(const char *name, const char **namep,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300462 bool all_contexts);
Keith Kaniosa5fc6462007-10-13 07:09:22 -0700463static void make_tok_num(Token * tok, int64_t val);
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000464static void error(int severity, const char *fmt, ...);
Victor van den Elzen3b404c02008-09-18 13:51:36 +0200465static void error_precond(int severity, const char *fmt, ...);
H. Peter Anvince616072002-04-30 21:02:23 +0000466static void *new_Block(size_t size);
467static void delete_Blocks(void);
H. Peter Anvinc751e862008-06-09 10:18:45 -0700468static Token *new_Token(Token * next, enum pp_token_type type,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300469 const char *text, int txtlen);
H. Peter Anvin734b1882002-04-30 21:01:08 +0000470static Token *delete_Token(Token * t);
H. Peter Anvineba20a72002-04-30 20:53:55 +0000471
472/*
473 * Macros for safe checking of token pointers, avoid *(NULL)
474 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300475#define tok_type_(x,t) ((x) && (x)->type == (t))
476#define skip_white_(x) if (tok_type_((x), TOK_WHITESPACE)) (x)=(x)->next
477#define tok_is_(x,v) (tok_type_((x), TOK_OTHER) && !strcmp((x)->text,(v)))
478#define tok_isnt_(x,v) ((x) && ((x)->type!=TOK_OTHER || strcmp((x)->text,(v))))
H. Peter Anvin76690a12002-04-30 20:52:49 +0000479
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300480/*
H. Peter Anvin077fb932010-07-20 14:56:30 -0700481 * nasm_unquote with error if the string contains NUL characters.
482 * If the string contains NUL characters, issue an error and return
483 * the C len, i.e. truncate at the NUL.
484 */
485static size_t nasm_unquote_cstr(char *qstr, enum preproc_token directive)
486{
487 size_t len = nasm_unquote(qstr, NULL);
488 size_t clen = strlen(qstr);
489
490 if (len != clen)
491 error(ERR_NONFATAL, "NUL character in `%s' directive",
492 pp_directives[directive]);
493
494 return clen;
495}
496
497/*
H. Peter Anvinb40992c2010-09-15 08:57:21 -0700498 * In-place reverse a list of tokens.
499 */
500static Token *reverse_tokens(Token *t)
501{
502 Token *prev = NULL;
503 Token *next;
504
505 while (t) {
506 next = t->next;
507 t->next = prev;
508 prev = t;
509 t = next;
510 }
511
512 return prev;
513}
514
515/*
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300516 * Handle TASM specific directives, which do not contain a % in
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000517 * front of them. We do it here because I could not find any other
518 * place to do it for the moment, and it is a hack (ideally it would
519 * be nice to be able to use the NASM pre-processor to do it).
520 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000521static char *check_tasm_directive(char *line)
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000522{
Keith Kaniosb7a89542007-04-12 02:40:54 +0000523 int32_t i, j, k, m, len;
Cyrill Gorcunovf66ac7d2009-10-12 20:41:13 +0400524 char *p, *q, *oldline, oldchar;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000525
Cyrill Gorcunovf66ac7d2009-10-12 20:41:13 +0400526 p = nasm_skip_spaces(line);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000527
528 /* Binary search for the directive name */
529 i = -1;
Cyrill Gorcunova7319242010-06-03 22:04:36 +0400530 j = ARRAY_SIZE(tasm_directives);
Cyrill Gorcunovf66ac7d2009-10-12 20:41:13 +0400531 q = nasm_skip_word(p);
532 len = q - p;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000533 if (len) {
534 oldchar = p[len];
535 p[len] = 0;
536 while (j - i > 1) {
537 k = (j + i) / 2;
538 m = nasm_stricmp(p, tasm_directives[k]);
539 if (m == 0) {
540 /* We have found a directive, so jam a % in front of it
541 * so that NASM will then recognise it as one if it's own.
542 */
543 p[len] = oldchar;
544 len = strlen(p);
545 oldline = line;
546 line = nasm_malloc(len + 2);
547 line[0] = '%';
548 if (k == TM_IFDIFI) {
H. Peter Anvin18f48792009-06-27 15:56:27 -0700549 /*
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300550 * NASM does not recognise IFDIFI, so we convert
551 * it to %if 0. This is not used in NASM
552 * compatible code, but does need to parse for the
553 * TASM macro package.
H. Peter Anvine2c80182005-01-15 22:15:51 +0000554 */
H. Peter Anvin18f48792009-06-27 15:56:27 -0700555 strcpy(line + 1, "if 0");
H. Peter Anvine2c80182005-01-15 22:15:51 +0000556 } else {
557 memcpy(line + 1, p, len + 1);
558 }
559 nasm_free(oldline);
560 return line;
561 } else if (m < 0) {
562 j = k;
563 } else
564 i = k;
565 }
566 p[len] = oldchar;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000567 }
568 return line;
569}
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000570
H. Peter Anvin76690a12002-04-30 20:52:49 +0000571/*
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000572 * The pre-preprocessing stage... This function translates line
573 * number indications as they emerge from GNU cpp (`# lineno "file"
574 * flags') into NASM preprocessor line number indications (`%line
575 * lineno file').
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000576 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000577static char *prepreproc(char *line)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000578{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000579 int lineno, fnlen;
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000580 char *fname, *oldline;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000581
H. Peter Anvine2c80182005-01-15 22:15:51 +0000582 if (line[0] == '#' && line[1] == ' ') {
583 oldline = line;
584 fname = oldline + 2;
585 lineno = atoi(fname);
586 fname += strspn(fname, "0123456789 ");
587 if (*fname == '"')
588 fname++;
589 fnlen = strcspn(fname, "\"");
590 line = nasm_malloc(20 + fnlen);
591 snprintf(line, 20 + fnlen, "%%line %d %.*s", lineno, fnlen, fname);
592 nasm_free(oldline);
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000593 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000594 if (tasm_compatible_mode)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000595 return check_tasm_directive(line);
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000596 return line;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000597}
598
599/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000600 * Free a linked list of tokens.
601 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000602static void free_tlist(Token * list)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000603{
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +0400604 while (list)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000605 list = delete_Token(list);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000606}
607
608/*
609 * Free a linked list of lines.
610 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000611static void free_llist(Line * list)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000612{
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +0400613 Line *l, *tmp;
614 list_for_each_safe(l, tmp, list) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000615 free_tlist(l->first);
616 nasm_free(l);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000617 }
618}
619
620/*
H. Peter Anvineba20a72002-04-30 20:53:55 +0000621 * Free an MMacro
622 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000623static void free_mmacro(MMacro * m)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000624{
H. Peter Anvin734b1882002-04-30 21:01:08 +0000625 nasm_free(m->name);
626 free_tlist(m->dlist);
627 nasm_free(m->defaults);
628 free_llist(m->expansion);
629 nasm_free(m);
H. Peter Anvineba20a72002-04-30 20:53:55 +0000630}
631
632/*
H. Peter Anvin97a23472007-09-16 17:57:25 -0700633 * Free all currently defined macros, and free the hash tables
634 */
H. Peter Anvin072771e2008-05-22 13:17:51 -0700635static void free_smacro_table(struct hash_table *smt)
H. Peter Anvin97a23472007-09-16 17:57:25 -0700636{
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +0400637 SMacro *s, *tmp;
H. Peter Anvin072771e2008-05-22 13:17:51 -0700638 const char *key;
639 struct hash_tbl_node *it = NULL;
H. Peter Anvin97a23472007-09-16 17:57:25 -0700640
H. Peter Anvin072771e2008-05-22 13:17:51 -0700641 while ((s = hash_iterate(smt, &it, &key)) != NULL) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300642 nasm_free((void *)key);
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +0400643 list_for_each_safe(s, tmp, s) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300644 nasm_free(s->name);
645 free_tlist(s->expansion);
646 nasm_free(s);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300647 }
H. Peter Anvin97a23472007-09-16 17:57:25 -0700648 }
H. Peter Anvin072771e2008-05-22 13:17:51 -0700649 hash_free(smt);
650}
651
652static void free_mmacro_table(struct hash_table *mmt)
653{
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +0400654 MMacro *m, *tmp;
H. Peter Anvin072771e2008-05-22 13:17:51 -0700655 const char *key;
656 struct hash_tbl_node *it = NULL;
H. Peter Anvin97a23472007-09-16 17:57:25 -0700657
658 it = NULL;
H. Peter Anvin072771e2008-05-22 13:17:51 -0700659 while ((m = hash_iterate(mmt, &it, &key)) != NULL) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300660 nasm_free((void *)key);
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +0400661 list_for_each_safe(m ,tmp, m)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300662 free_mmacro(m);
H. Peter Anvin97a23472007-09-16 17:57:25 -0700663 }
H. Peter Anvin072771e2008-05-22 13:17:51 -0700664 hash_free(mmt);
665}
666
667static void free_macros(void)
668{
H. Peter Anvin166c2472008-05-28 12:28:58 -0700669 free_smacro_table(&smacros);
670 free_mmacro_table(&mmacros);
H. Peter Anvin97a23472007-09-16 17:57:25 -0700671}
672
673/*
674 * Initialize the hash tables
675 */
676static void init_macros(void)
677{
H. Peter Anvin166c2472008-05-28 12:28:58 -0700678 hash_init(&smacros, HASH_LARGE);
679 hash_init(&mmacros, HASH_LARGE);
H. Peter Anvin97a23472007-09-16 17:57:25 -0700680}
681
682/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000683 * Pop the context stack.
684 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000685static void ctx_pop(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000686{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000687 Context *c = cstk;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000688
689 cstk = cstk->next;
H. Peter Anvin166c2472008-05-28 12:28:58 -0700690 free_smacro_table(&c->localmac);
H. Peter Anvin734b1882002-04-30 21:01:08 +0000691 nasm_free(c->name);
692 nasm_free(c);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000693}
694
H. Peter Anvin072771e2008-05-22 13:17:51 -0700695/*
696 * Search for a key in the hash index; adding it if necessary
697 * (in which case we initialize the data pointer to NULL.)
698 */
699static void **
700hash_findi_add(struct hash_table *hash, const char *str)
701{
702 struct hash_insert hi;
703 void **r;
704 char *strx;
705
706 r = hash_findi(hash, str, &hi);
707 if (r)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300708 return r;
H. Peter Anvin072771e2008-05-22 13:17:51 -0700709
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300710 strx = nasm_strdup(str); /* Use a more efficient allocator here? */
H. Peter Anvin072771e2008-05-22 13:17:51 -0700711 return hash_add(&hi, strx, NULL);
712}
713
714/*
715 * Like hash_findi, but returns the data element rather than a pointer
716 * to it. Used only when not adding a new element, hence no third
717 * argument.
718 */
719static void *
720hash_findix(struct hash_table *hash, const char *str)
721{
722 void **p;
723
724 p = hash_findi(hash, str, NULL);
725 return p ? *p : NULL;
726}
727
Cyrill Gorcunov15bdc512010-07-13 11:27:41 +0400728/*
729 * read line from standart macros set,
730 * if there no more left -- return NULL
731 */
732static char *line_from_stdmac(void)
733{
734 unsigned char c;
735 const unsigned char *p = stdmacpos;
736 char *line, *q;
737 size_t len = 0;
738
739 if (!stdmacpos)
740 return NULL;
741
742 while ((c = *p++)) {
743 if (c >= 0x80)
744 len += pp_directives_len[c - 0x80] + 1;
745 else
746 len++;
747 }
748
749 line = nasm_malloc(len + 1);
750 q = line;
751 while ((c = *stdmacpos++)) {
752 if (c >= 0x80) {
753 memcpy(q, pp_directives[c - 0x80], pp_directives_len[c - 0x80]);
754 q += pp_directives_len[c - 0x80];
755 *q++ = ' ';
756 } else {
757 *q++ = c;
758 }
759 }
760 stdmacpos = p;
761 *q = '\0';
762
763 if (!*stdmacpos) {
764 /* This was the last of the standard macro chain... */
765 stdmacpos = NULL;
766 if (any_extrastdmac) {
767 stdmacpos = extrastdmac;
768 any_extrastdmac = false;
769 } else if (do_predef) {
770 Line *pd, *l;
771 Token *head, **tail, *t;
772
773 /*
774 * Nasty hack: here we push the contents of
775 * `predef' on to the top-level expansion stack,
776 * since this is the most convenient way to
777 * implement the pre-include and pre-define
778 * features.
779 */
780 list_for_each(pd, predef) {
781 head = NULL;
782 tail = &head;
783 list_for_each(t, pd->first) {
784 *tail = new_Token(NULL, t->type, t->text, 0);
785 tail = &(*tail)->next;
786 }
787
788 l = nasm_malloc(sizeof(Line));
789 l->next = istk->expansion;
790 l->first = head;
791 l->finishes = NULL;
792
793 istk->expansion = l;
794 }
795 do_predef = false;
796 }
797 }
798
799 return line;
800}
801
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000802#define BUF_DELTA 512
803/*
804 * Read a line from the top file in istk, handling multiple CR/LFs
805 * at the end of the line read, and handling spurious ^Zs. Will
806 * return lines from the standard macro set if this has not already
807 * been done.
808 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000809static char *read_line(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000810{
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000811 char *buffer, *p, *q;
H. Peter Anvin9f394642002-04-30 21:07:51 +0000812 int bufsize, continued_count;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000813
Cyrill Gorcunov15bdc512010-07-13 11:27:41 +0400814 /*
815 * standart macros set (predefined) goes first
816 */
817 p = line_from_stdmac();
818 if (p)
819 return p;
H. Peter Anvin72edbb82008-06-19 16:00:04 -0700820
Cyrill Gorcunov15bdc512010-07-13 11:27:41 +0400821 /*
822 * regular read from a file
823 */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000824 bufsize = BUF_DELTA;
825 buffer = nasm_malloc(BUF_DELTA);
826 p = buffer;
H. Peter Anvin9f394642002-04-30 21:07:51 +0000827 continued_count = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000828 while (1) {
829 q = fgets(p, bufsize - (p - buffer), istk->fp);
830 if (!q)
831 break;
832 p += strlen(p);
833 if (p > buffer && p[-1] == '\n') {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300834 /*
835 * Convert backslash-CRLF line continuation sequences into
836 * nothing at all (for DOS and Windows)
837 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000838 if (((p - 2) > buffer) && (p[-3] == '\\') && (p[-2] == '\r')) {
839 p -= 3;
840 *p = 0;
841 continued_count++;
842 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300843 /*
844 * Also convert backslash-LF line continuation sequences into
845 * nothing at all (for Unix)
846 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000847 else if (((p - 1) > buffer) && (p[-2] == '\\')) {
848 p -= 2;
849 *p = 0;
850 continued_count++;
851 } else {
852 break;
853 }
854 }
855 if (p - buffer > bufsize - 10) {
Keith Kaniosb7a89542007-04-12 02:40:54 +0000856 int32_t offset = p - buffer;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000857 bufsize += BUF_DELTA;
858 buffer = nasm_realloc(buffer, bufsize);
859 p = buffer + offset; /* prevent stale-pointer problems */
860 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000861 }
862
H. Peter Anvine2c80182005-01-15 22:15:51 +0000863 if (!q && p == buffer) {
864 nasm_free(buffer);
865 return NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000866 }
867
H. Peter Anvine2c80182005-01-15 22:15:51 +0000868 src_set_linnum(src_get_linnum() + istk->lineinc +
869 (continued_count * istk->lineinc));
H. Peter Anvineba20a72002-04-30 20:53:55 +0000870
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000871 /*
872 * Play safe: remove CRs as well as LFs, if any of either are
873 * present at the end of the line.
874 */
H. Peter Anvineba20a72002-04-30 20:53:55 +0000875 while (--p >= buffer && (*p == '\n' || *p == '\r'))
H. Peter Anvine2c80182005-01-15 22:15:51 +0000876 *p = '\0';
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000877
878 /*
879 * Handle spurious ^Z, which may be inserted into source files
880 * by some file transfer utilities.
881 */
882 buffer[strcspn(buffer, "\032")] = '\0';
883
H. Peter Anvin734b1882002-04-30 21:01:08 +0000884 list->line(LIST_READ, buffer);
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000885
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000886 return buffer;
887}
888
889/*
Keith Kaniosb7a89542007-04-12 02:40:54 +0000890 * Tokenize a line of text. This is a very simple process since we
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000891 * don't need to parse the value out of e.g. numeric tokens: we
892 * simply split one string into many.
893 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000894static Token *tokenize(char *line)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000895{
H. Peter Anvinca544db2008-10-19 19:30:11 -0700896 char c, *p = line;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +0000897 enum pp_token_type type;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000898 Token *list = NULL;
899 Token *t, **tail = &list;
900
H. Peter Anvine2c80182005-01-15 22:15:51 +0000901 while (*line) {
902 p = line;
903 if (*p == '%') {
904 p++;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300905 if (*p == '+' && !nasm_isdigit(p[1])) {
906 p++;
907 type = TOK_PASTE;
908 } else if (nasm_isdigit(*p) ||
909 ((*p == '-' || *p == '+') && nasm_isdigit(p[1]))) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000910 do {
911 p++;
912 }
H. Peter Anvinbda7a6e2008-06-21 10:23:17 -0700913 while (nasm_isdigit(*p));
H. Peter Anvine2c80182005-01-15 22:15:51 +0000914 type = TOK_PREPROC_ID;
915 } else if (*p == '{') {
916 p++;
Cyrill Gorcunove0fdd772010-10-07 19:30:54 +0400917 while (*p) {
918 if (*p == '}')
919 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000920 p[-1] = *p;
921 p++;
922 }
Cyrill Gorcunove0fdd772010-10-07 19:30:54 +0400923 if (*p != '}')
924 error(ERR_WARNING | ERR_PASS1, "unterminated %{ construct");
H. Peter Anvine2c80182005-01-15 22:15:51 +0000925 p[-1] = '\0';
926 if (*p)
927 p++;
928 type = TOK_PREPROC_ID;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300929 } else if (*p == '[') {
930 int lvl = 1;
931 line += 2; /* Skip the leading %[ */
932 p++;
933 while (lvl && (c = *p++)) {
934 switch (c) {
935 case ']':
936 lvl--;
937 break;
938 case '%':
939 if (*p == '[')
940 lvl++;
941 break;
942 case '\'':
943 case '\"':
944 case '`':
Cyrill Gorcunovc6360a72010-07-13 13:32:19 +0400945 p = nasm_skip_string(p - 1) + 1;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300946 break;
947 default:
948 break;
949 }
950 }
951 p--;
952 if (*p)
953 *p++ = '\0';
954 if (lvl)
955 error(ERR_NONFATAL, "unterminated %[ construct");
956 type = TOK_INDIRECT;
957 } else if (*p == '?') {
958 type = TOK_PREPROC_Q; /* %? */
959 p++;
960 if (*p == '?') {
961 type = TOK_PREPROC_QQ; /* %?? */
962 p++;
963 }
Cyrill Gorcunov84b4cba2010-09-12 21:39:40 +0400964 } else if (*p == '!') {
965 type = TOK_PREPROC_ID;
966 p++;
967 if (isidchar(*p)) {
968 do {
969 p++;
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++;
1008 } 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 */
1163 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;
1167 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) {
1183 if (b->chunk)
1184 nasm_free(b->chunk);
1185 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] == '!') {
Cyrill Gorcunov84b4cba2010-09-12 21:39:40 +04001249 char *v;
1250 char *q = t->text;
H. Peter Anvin077fb932010-07-20 14:56:30 -07001251
Cyrill Gorcunov84b4cba2010-09-12 21:39:40 +04001252 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
Cyrill Gorcunov84b4cba2010-09-12 21:39:40 +04001257 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
Cyrill Gorcunov84b4cba2010-09-12 21:39:40 +04001264 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;
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001282 Context *ctx = get_ctx(t->text, &q, false);
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 Anvin6867acc2007-10-10 14:58:45 -07001447 * If all_contexts == true, contexts that enclose current are
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001448 * also scanned for such smacro, until it is found; if not -
1449 * only the context that directly results from the number of $'s
1450 * in variable's name.
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001451 *
1452 * If "namep" is non-NULL, set it to the pointer to the macro name
1453 * tail, i.e. the part beyond %$...
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001454 */
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001455static Context *get_ctx(const char *name, const char **namep,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001456 bool all_contexts)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001457{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001458 Context *ctx;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001459 SMacro *m;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001460 int i;
1461
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001462 if (namep)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001463 *namep = name;
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001464
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001465 if (!name || name[0] != '%' || name[1] != '$')
H. Peter Anvine2c80182005-01-15 22:15:51 +00001466 return NULL;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001467
H. Peter Anvine2c80182005-01-15 22:15:51 +00001468 if (!cstk) {
1469 error(ERR_NONFATAL, "`%s': context stack is empty", name);
1470 return NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001471 }
1472
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001473 name += 2;
1474 ctx = cstk;
1475 i = 0;
1476 while (ctx && *name == '$') {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001477 name++;
1478 i++;
1479 ctx = ctx->next;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001480 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001481 if (!ctx) {
1482 error(ERR_NONFATAL, "`%s': context stack is only"
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001483 " %d level%s deep", name, i, (i == 1 ? "" : "s"));
H. Peter Anvine2c80182005-01-15 22:15:51 +00001484 return NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001485 }
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001486
1487 if (namep)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001488 *namep = name;
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001489
Keith Kanios404589e2010-08-10 20:12:57 -05001490 if (!all_contexts)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001491 return ctx;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001492
Cyrill Gorcunovd90693c2010-08-11 20:31:46 +04001493 /*
1494 * NOTE: In 2.10 we will not need lookup in extarnal
1495 * contexts, so this is a gentle way to inform users
1496 * about their source code need to be updated
1497 */
1498
1499 /* first round -- check the current context */
1500 m = hash_findix(&ctx->localmac, name);
1501 while (m) {
1502 if (!mstrcmp(m->name, name, m->casesense))
1503 return ctx;
1504 m = m->next;
1505 }
1506
1507 /* second round - external contexts */
1508 while ((ctx = ctx->next)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001509 /* Search for this smacro in found context */
H. Peter Anvin166c2472008-05-28 12:28:58 -07001510 m = hash_findix(&ctx->localmac, name);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001511 while (m) {
Keith Kanios404589e2010-08-10 20:12:57 -05001512 if (!mstrcmp(m->name, name, m->casesense)) {
Keith Kanios88d947e2010-08-14 12:47:45 -05001513 /* NOTE: deprecated as of 2.10 */
Cyrill Gorcunovd90693c2010-08-11 20:31:46 +04001514 static int once = 0;
1515 if (!once) {
1516 error(ERR_WARNING, "context-local macro expansion"
Cyrill Gorcunov84b4cba2010-09-12 21:39:40 +04001517 " fall-through (automatic searching of outer"
1518 " contexts) will be deprecated starting in"
1519 " NASM 2.10, please see the NASM Manual for"
1520 " more information");
Cyrill Gorcunovd90693c2010-08-11 20:31:46 +04001521 once = 1;
1522 }
Keith Kanios88d947e2010-08-14 12:47:45 -05001523 error(ERR_WARNING, "`%s': context-local macro expansion fall-through", name);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001524 return ctx;
Cyrill Gorcunovd90693c2010-08-11 20:31:46 +04001525 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001526 m = m->next;
1527 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00001528 }
Cyrill Gorcunovd90693c2010-08-11 20:31:46 +04001529
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001530 return NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001531}
1532
1533/*
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001534 * Check to see if a file is already in a string list
1535 */
1536static bool in_list(const StrList *list, const char *str)
1537{
1538 while (list) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001539 if (!strcmp(list->str, str))
1540 return true;
1541 list = list->next;
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001542 }
1543 return false;
1544}
1545
1546/*
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001547 * Open an include file. This routine must always return a valid
1548 * file pointer if it returns - it's responsible for throwing an
1549 * ERR_FATAL and bombing out completely if not. It should also try
1550 * the include path one by one until it finds the file or reaches
1551 * the end of the path.
1552 */
H. Peter Anvin2b1c3b92008-06-06 10:38:46 -07001553static FILE *inc_fopen(const char *file, StrList **dhead, StrList ***dtail,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001554 bool missing_ok)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001555{
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001556 FILE *fp;
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001557 char *prefix = "";
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001558 IncPath *ip = ipath;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001559 int len = strlen(file);
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001560 size_t prefix_len = 0;
1561 StrList *sl;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001562
H. Peter Anvine2c80182005-01-15 22:15:51 +00001563 while (1) {
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001564 sl = nasm_malloc(prefix_len+len+1+sizeof sl->next);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001565 memcpy(sl->str, prefix, prefix_len);
1566 memcpy(sl->str+prefix_len, file, len+1);
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001567 fp = fopen(sl->str, "r");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001568 if (fp && dhead && !in_list(*dhead, sl->str)) {
1569 sl->next = NULL;
1570 **dtail = sl;
1571 *dtail = &sl->next;
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001572 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001573 nasm_free(sl);
1574 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001575 if (fp)
1576 return fp;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001577 if (!ip) {
1578 if (!missing_ok)
1579 break;
1580 prefix = NULL;
1581 } else {
1582 prefix = ip->path;
1583 ip = ip->next;
1584 }
1585 if (prefix) {
1586 prefix_len = strlen(prefix);
1587 } else {
1588 /* -MG given and file not found */
1589 if (dhead && !in_list(*dhead, file)) {
1590 sl = nasm_malloc(len+1+sizeof sl->next);
1591 sl->next = NULL;
1592 strcpy(sl->str, file);
1593 **dtail = sl;
1594 *dtail = &sl->next;
1595 }
1596 return NULL;
1597 }
H. Peter Anvineba20a72002-04-30 20:53:55 +00001598 }
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001599
H. Peter Anvin734b1882002-04-30 21:01:08 +00001600 error(ERR_FATAL, "unable to open include file `%s'", file);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001601 return NULL;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001602}
1603
1604/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001605 * Determine if we should warn on defining a single-line macro of
H. Peter Anvinef7468f2002-04-30 20:57:59 +00001606 * name `name', with `nparam' parameters. If nparam is 0 or -1, will
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001607 * return true if _any_ single-line macro of that name is defined.
1608 * Otherwise, will return true if a single-line macro with either
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001609 * `nparam' or no parameters is defined.
1610 *
1611 * If a macro with precisely the right number of parameters is
H. Peter Anvinef7468f2002-04-30 20:57:59 +00001612 * defined, or nparam is -1, the address of the definition structure
1613 * will be returned in `defn'; otherwise NULL will be returned. If `defn'
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001614 * is NULL, no action will be taken regarding its contents, and no
1615 * error will occur.
1616 *
1617 * Note that this is also called with nparam zero to resolve
1618 * `ifdef'.
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001619 *
1620 * If you already know which context macro belongs to, you can pass
1621 * the context pointer as first parameter; if you won't but name begins
1622 * with %$ the context will be automatically computed. If all_contexts
1623 * is true, macro will be searched in outer contexts as well.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001624 */
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07001625static bool
H. Peter Anvinb2a5fda2008-06-19 21:42:42 -07001626smacro_defined(Context * ctx, const char *name, int nparam, SMacro ** defn,
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07001627 bool nocase)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001628{
H. Peter Anvin166c2472008-05-28 12:28:58 -07001629 struct hash_table *smtbl;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001630 SMacro *m;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001631
H. Peter Anvin97a23472007-09-16 17:57:25 -07001632 if (ctx) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001633 smtbl = &ctx->localmac;
H. Peter Anvin97a23472007-09-16 17:57:25 -07001634 } else if (name[0] == '%' && name[1] == '$') {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001635 if (cstk)
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001636 ctx = get_ctx(name, &name, false);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001637 if (!ctx)
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001638 return false; /* got to return _something_ */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001639 smtbl = &ctx->localmac;
H. Peter Anvin97a23472007-09-16 17:57:25 -07001640 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001641 smtbl = &smacros;
H. Peter Anvin97a23472007-09-16 17:57:25 -07001642 }
H. Peter Anvin166c2472008-05-28 12:28:58 -07001643 m = (SMacro *) hash_findix(smtbl, name);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001644
H. Peter Anvine2c80182005-01-15 22:15:51 +00001645 while (m) {
1646 if (!mstrcmp(m->name, name, m->casesense && nocase) &&
Charles Crayne192d5b52007-10-18 19:02:42 -07001647 (nparam <= 0 || m->nparam == 0 || nparam == (int) m->nparam)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001648 if (defn) {
Charles Crayne192d5b52007-10-18 19:02:42 -07001649 if (nparam == (int) m->nparam || nparam == -1)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001650 *defn = m;
1651 else
1652 *defn = NULL;
1653 }
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001654 return true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001655 }
1656 m = m->next;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001657 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001658
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001659 return false;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001660}
1661
1662/*
1663 * Count and mark off the parameters in a multi-line macro call.
1664 * This is called both from within the multi-line macro expansion
1665 * code, and also to mark off the default parameters when provided
1666 * in a %macro definition line.
1667 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001668static void count_mmac_params(Token * t, int *nparam, Token *** params)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001669{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001670 int paramsize, brace;
1671
1672 *nparam = paramsize = 0;
1673 *params = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001674 while (t) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001675 /* +1: we need space for the final NULL */
H. Peter Anvin91fb6f12008-09-01 10:56:33 -07001676 if (*nparam+1 >= paramsize) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001677 paramsize += PARAM_DELTA;
1678 *params = nasm_realloc(*params, sizeof(**params) * paramsize);
1679 }
1680 skip_white_(t);
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001681 brace = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001682 if (tok_is_(t, "{"))
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001683 brace = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001684 (*params)[(*nparam)++] = t;
1685 while (tok_isnt_(t, brace ? "}" : ","))
1686 t = t->next;
1687 if (t) { /* got a comma/brace */
1688 t = t->next;
1689 if (brace) {
1690 /*
1691 * Now we've found the closing brace, look further
1692 * for the comma.
1693 */
1694 skip_white_(t);
1695 if (tok_isnt_(t, ",")) {
1696 error(ERR_NONFATAL,
1697 "braces do not enclose all of macro parameter");
1698 while (tok_isnt_(t, ","))
1699 t = t->next;
1700 }
1701 if (t)
1702 t = t->next; /* eat the comma */
1703 }
1704 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001705 }
1706}
1707
1708/*
H. Peter Anvin76690a12002-04-30 20:52:49 +00001709 * Determine whether one of the various `if' conditions is true or
1710 * not.
1711 *
1712 * We must free the tline we get passed.
1713 */
H. Peter Anvin70055962007-10-11 00:05:31 -07001714static bool if_condition(Token * tline, enum preproc_token ct)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001715{
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001716 enum pp_conditional i = PP_COND(ct);
H. Peter Anvin70055962007-10-11 00:05:31 -07001717 bool j;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001718 Token *t, *tt, **tptr, *origline;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001719 struct tokenval tokval;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001720 expr *evalresult;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001721 enum pp_token_type needtype;
H. Peter Anvin077fb932010-07-20 14:56:30 -07001722 char *p;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001723
1724 origline = tline;
1725
H. Peter Anvine2c80182005-01-15 22:15:51 +00001726 switch (i) {
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001727 case PPC_IFCTX:
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001728 j = false; /* have we matched yet? */
Victor van den Elzen0e857f12008-07-23 13:21:29 +02001729 while (true) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001730 skip_white_(tline);
Victor van den Elzen0e857f12008-07-23 13:21:29 +02001731 if (!tline)
1732 break;
1733 if (tline->type != TOK_ID) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001734 error(ERR_NONFATAL,
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001735 "`%s' expects context identifiers", pp_directives[ct]);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001736 free_tlist(origline);
1737 return -1;
1738 }
Victor van den Elzen0e857f12008-07-23 13:21:29 +02001739 if (cstk && cstk->name && !nasm_stricmp(tline->text, cstk->name))
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001740 j = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001741 tline = tline->next;
1742 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001743 break;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001744
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001745 case PPC_IFDEF:
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001746 j = false; /* have we matched yet? */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001747 while (tline) {
1748 skip_white_(tline);
1749 if (!tline || (tline->type != TOK_ID &&
1750 (tline->type != TOK_PREPROC_ID ||
1751 tline->text[1] != '$'))) {
1752 error(ERR_NONFATAL,
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001753 "`%s' expects macro identifiers", pp_directives[ct]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001754 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001755 }
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07001756 if (smacro_defined(NULL, tline->text, 0, NULL, true))
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001757 j = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001758 tline = tline->next;
1759 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001760 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001761
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001762 case PPC_IFENV:
Cyrill Gorcunov84b4cba2010-09-12 21:39:40 +04001763 tline = expand_smacro(tline);
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001764 j = false; /* have we matched yet? */
1765 while (tline) {
1766 skip_white_(tline);
1767 if (!tline || (tline->type != TOK_ID &&
Cyrill Gorcunov84b4cba2010-09-12 21:39:40 +04001768 tline->type != TOK_STRING &&
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001769 (tline->type != TOK_PREPROC_ID ||
Cyrill Gorcunov84b4cba2010-09-12 21:39:40 +04001770 tline->text[1] != '!'))) {
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001771 error(ERR_NONFATAL,
1772 "`%s' expects environment variable names",
Cyrill Gorcunov84b4cba2010-09-12 21:39:40 +04001773 pp_directives[ct]);
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001774 goto fail;
1775 }
Cyrill Gorcunov84b4cba2010-09-12 21:39:40 +04001776 p = tline->text;
1777 if (tline->type == TOK_PREPROC_ID)
1778 p += 2; /* Skip leading %! */
1779 if (*p == '\'' || *p == '\"' || *p == '`')
1780 nasm_unquote_cstr(p, ct);
1781 if (getenv(p))
1782 j = true;
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001783 tline = tline->next;
1784 }
1785 break;
1786
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001787 case PPC_IFIDN:
1788 case PPC_IFIDNI:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001789 tline = expand_smacro(tline);
1790 t = tt = tline;
1791 while (tok_isnt_(tt, ","))
1792 tt = tt->next;
1793 if (!tt) {
1794 error(ERR_NONFATAL,
1795 "`%s' expects two comma-separated arguments",
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001796 pp_directives[ct]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001797 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001798 }
1799 tt = tt->next;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001800 j = true; /* assume equality unless proved not */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001801 while ((t->type != TOK_OTHER || strcmp(t->text, ",")) && tt) {
1802 if (tt->type == TOK_OTHER && !strcmp(tt->text, ",")) {
1803 error(ERR_NONFATAL, "`%s': more than one comma on line",
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001804 pp_directives[ct]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001805 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001806 }
1807 if (t->type == TOK_WHITESPACE) {
1808 t = t->next;
1809 continue;
1810 }
1811 if (tt->type == TOK_WHITESPACE) {
1812 tt = tt->next;
1813 continue;
1814 }
1815 if (tt->type != t->type) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001816 j = false; /* found mismatching tokens */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001817 break;
1818 }
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07001819 /* When comparing strings, need to unquote them first */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001820 if (t->type == TOK_STRING) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001821 size_t l1 = nasm_unquote(t->text, NULL);
1822 size_t l2 = nasm_unquote(tt->text, NULL);
H. Peter Anvind2456592008-06-19 15:04:18 -07001823
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001824 if (l1 != l2) {
1825 j = false;
1826 break;
1827 }
1828 if (mmemcmp(t->text, tt->text, l1, i == PPC_IFIDN)) {
1829 j = false;
1830 break;
1831 }
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07001832 } else if (mstrcmp(tt->text, t->text, i == PPC_IFIDN) != 0) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001833 j = false; /* found mismatching tokens */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001834 break;
1835 }
Nickolay Yurchenkof3b3ce22003-09-21 20:38:43 +00001836
H. Peter Anvine2c80182005-01-15 22:15:51 +00001837 t = t->next;
1838 tt = tt->next;
1839 }
1840 if ((t->type != TOK_OTHER || strcmp(t->text, ",")) || tt)
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001841 j = false; /* trailing gunk on one end or other */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001842 break;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001843
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001844 case PPC_IFMACRO:
H. Peter Anvin89cee572009-07-15 09:16:54 -04001845 {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001846 bool found = false;
1847 MMacro searching, *mmac;
H. Peter Anvin65747262002-05-07 00:10:05 +00001848
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001849 skip_white_(tline);
1850 tline = expand_id(tline);
1851 if (!tok_type_(tline, TOK_ID)) {
1852 error(ERR_NONFATAL,
1853 "`%s' expects a macro name", pp_directives[ct]);
1854 goto fail;
1855 }
1856 searching.name = nasm_strdup(tline->text);
1857 searching.casesense = true;
1858 searching.plus = false;
1859 searching.nolist = false;
1860 searching.in_progress = 0;
1861 searching.max_depth = 0;
1862 searching.rep_nest = NULL;
1863 searching.nparam_min = 0;
1864 searching.nparam_max = INT_MAX;
1865 tline = expand_smacro(tline->next);
1866 skip_white_(tline);
1867 if (!tline) {
1868 } else if (!tok_type_(tline, TOK_NUMBER)) {
1869 error(ERR_NONFATAL,
1870 "`%s' expects a parameter count or nothing",
1871 pp_directives[ct]);
1872 } else {
1873 searching.nparam_min = searching.nparam_max =
1874 readnum(tline->text, &j);
1875 if (j)
1876 error(ERR_NONFATAL,
1877 "unable to parse parameter count `%s'",
1878 tline->text);
1879 }
1880 if (tline && tok_is_(tline->next, "-")) {
1881 tline = tline->next->next;
1882 if (tok_is_(tline, "*"))
1883 searching.nparam_max = INT_MAX;
1884 else if (!tok_type_(tline, TOK_NUMBER))
1885 error(ERR_NONFATAL,
1886 "`%s' expects a parameter count after `-'",
1887 pp_directives[ct]);
1888 else {
1889 searching.nparam_max = readnum(tline->text, &j);
1890 if (j)
1891 error(ERR_NONFATAL,
1892 "unable to parse parameter count `%s'",
1893 tline->text);
1894 if (searching.nparam_min > searching.nparam_max)
1895 error(ERR_NONFATAL,
1896 "minimum parameter count exceeds maximum");
1897 }
1898 }
1899 if (tline && tok_is_(tline->next, "+")) {
1900 tline = tline->next;
1901 searching.plus = true;
1902 }
1903 mmac = (MMacro *) hash_findix(&mmacros, searching.name);
1904 while (mmac) {
1905 if (!strcmp(mmac->name, searching.name) &&
1906 (mmac->nparam_min <= searching.nparam_max
1907 || searching.plus)
1908 && (searching.nparam_min <= mmac->nparam_max
1909 || mmac->plus)) {
1910 found = true;
1911 break;
1912 }
1913 mmac = mmac->next;
1914 }
1915 if (tline && tline->next)
1916 error(ERR_WARNING|ERR_PASS1,
1917 "trailing garbage after %%ifmacro ignored");
1918 nasm_free(searching.name);
1919 j = found;
1920 break;
H. Peter Anvin89cee572009-07-15 09:16:54 -04001921 }
H. Peter Anvin65747262002-05-07 00:10:05 +00001922
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001923 case PPC_IFID:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001924 needtype = TOK_ID;
1925 goto iftype;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001926 case PPC_IFNUM:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001927 needtype = TOK_NUMBER;
1928 goto iftype;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001929 case PPC_IFSTR:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001930 needtype = TOK_STRING;
1931 goto iftype;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001932
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001933iftype:
1934 t = tline = expand_smacro(tline);
H. Peter Anvind85d2502008-05-04 17:53:31 -07001935
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001936 while (tok_type_(t, TOK_WHITESPACE) ||
1937 (needtype == TOK_NUMBER &&
1938 tok_type_(t, TOK_OTHER) &&
1939 (t->text[0] == '-' || t->text[0] == '+') &&
1940 !t->text[1]))
1941 t = t->next;
H. Peter Anvind85d2502008-05-04 17:53:31 -07001942
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001943 j = tok_type_(t, needtype);
1944 break;
H. Peter Anvincbf768d2008-02-16 16:41:25 -08001945
1946 case PPC_IFTOKEN:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001947 t = tline = expand_smacro(tline);
1948 while (tok_type_(t, TOK_WHITESPACE))
1949 t = t->next;
H. Peter Anvincbf768d2008-02-16 16:41:25 -08001950
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001951 j = false;
1952 if (t) {
1953 t = t->next; /* Skip the actual token */
1954 while (tok_type_(t, TOK_WHITESPACE))
1955 t = t->next;
1956 j = !t; /* Should be nothing left */
1957 }
1958 break;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001959
H. Peter Anvin134b9462008-02-16 17:01:40 -08001960 case PPC_IFEMPTY:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001961 t = tline = expand_smacro(tline);
1962 while (tok_type_(t, TOK_WHITESPACE))
1963 t = t->next;
H. Peter Anvin134b9462008-02-16 17:01:40 -08001964
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001965 j = !t; /* Should be empty */
1966 break;
H. Peter Anvin134b9462008-02-16 17:01:40 -08001967
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001968 case PPC_IF:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001969 t = tline = expand_smacro(tline);
1970 tptr = &t;
1971 tokval.t_type = TOKEN_INVALID;
1972 evalresult = evaluate(ppscan, tptr, &tokval,
1973 NULL, pass | CRITICAL, error, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001974 if (!evalresult)
1975 return -1;
1976 if (tokval.t_type)
H. Peter Anvin917a3492008-09-24 09:14:49 -07001977 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +00001978 "trailing garbage after expression ignored");
1979 if (!is_simple(evalresult)) {
1980 error(ERR_NONFATAL,
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001981 "non-constant value given to `%s'", pp_directives[ct]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001982 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001983 }
Chuck Crayne60ae75d2007-05-02 01:59:16 +00001984 j = reloc_value(evalresult) != 0;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001985 break;
H. Peter Anvin95e28822007-09-12 04:20:08 +00001986
H. Peter Anvine2c80182005-01-15 22:15:51 +00001987 default:
1988 error(ERR_FATAL,
1989 "preprocessor directive `%s' not yet implemented",
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001990 pp_directives[ct]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001991 goto fail;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001992 }
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001993
1994 free_tlist(origline);
1995 return j ^ PP_NEGATIVE(ct);
H. Peter Anvin70653092007-10-19 14:42:29 -07001996
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001997fail:
1998 free_tlist(origline);
1999 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002000}
2001
2002/*
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002003 * Common code for defining an smacro
2004 */
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08002005static bool define_smacro(Context *ctx, const char *mname, bool casesense,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002006 int nparam, Token *expansion)
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002007{
2008 SMacro *smac, **smhead;
H. Peter Anvin166c2472008-05-28 12:28:58 -07002009 struct hash_table *smtbl;
H. Peter Anvin70653092007-10-19 14:42:29 -07002010
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002011 if (smacro_defined(ctx, mname, nparam, &smac, casesense)) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002012 if (!smac) {
2013 error(ERR_WARNING|ERR_PASS1,
2014 "single-line macro `%s' defined both with and"
2015 " without parameters", mname);
2016 /*
2017 * Some instances of the old code considered this a failure,
2018 * some others didn't. What is the right thing to do here?
2019 */
2020 free_tlist(expansion);
2021 return false; /* Failure */
2022 } else {
2023 /*
2024 * We're redefining, so we have to take over an
2025 * existing SMacro structure. This means freeing
2026 * what was already in it.
2027 */
2028 nasm_free(smac->name);
2029 free_tlist(smac->expansion);
2030 }
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002031 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002032 smtbl = ctx ? &ctx->localmac : &smacros;
2033 smhead = (SMacro **) hash_findi_add(smtbl, mname);
2034 smac = nasm_malloc(sizeof(SMacro));
2035 smac->next = *smhead;
2036 *smhead = smac;
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002037 }
2038 smac->name = nasm_strdup(mname);
2039 smac->casesense = casesense;
2040 smac->nparam = nparam;
2041 smac->expansion = expansion;
2042 smac->in_progress = false;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002043 return true; /* Success */
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002044}
2045
2046/*
2047 * Undefine an smacro
2048 */
2049static void undef_smacro(Context *ctx, const char *mname)
2050{
2051 SMacro **smhead, *s, **sp;
H. Peter Anvin166c2472008-05-28 12:28:58 -07002052 struct hash_table *smtbl;
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002053
H. Peter Anvin166c2472008-05-28 12:28:58 -07002054 smtbl = ctx ? &ctx->localmac : &smacros;
2055 smhead = (SMacro **)hash_findi(smtbl, mname, NULL);
H. Peter Anvin70653092007-10-19 14:42:29 -07002056
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002057 if (smhead) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002058 /*
2059 * We now have a macro name... go hunt for it.
2060 */
2061 sp = smhead;
2062 while ((s = *sp) != NULL) {
2063 if (!mstrcmp(s->name, mname, s->casesense)) {
2064 *sp = s->next;
2065 nasm_free(s->name);
2066 free_tlist(s->expansion);
2067 nasm_free(s);
2068 } else {
2069 sp = &s->next;
2070 }
2071 }
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002072 }
2073}
2074
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002075/*
H. Peter Anvina26433d2008-07-16 14:40:01 -07002076 * Parse a mmacro specification.
2077 */
2078static bool parse_mmacro_spec(Token *tline, MMacro *def, const char *directive)
2079{
2080 bool err;
2081
2082 tline = tline->next;
2083 skip_white_(tline);
2084 tline = expand_id(tline);
2085 if (!tok_type_(tline, TOK_ID)) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002086 error(ERR_NONFATAL, "`%s' expects a macro name", directive);
2087 return false;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002088 }
Victor van den Elzenb916ede2008-07-23 15:14:22 +02002089
H. Peter Anvin89cee572009-07-15 09:16:54 -04002090 def->prev = NULL;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002091 def->name = nasm_strdup(tline->text);
2092 def->plus = false;
2093 def->nolist = false;
2094 def->in_progress = 0;
2095 def->rep_nest = NULL;
Victor van den Elzenb916ede2008-07-23 15:14:22 +02002096 def->nparam_min = 0;
2097 def->nparam_max = 0;
2098
H. Peter Anvina26433d2008-07-16 14:40:01 -07002099 tline = expand_smacro(tline->next);
2100 skip_white_(tline);
2101 if (!tok_type_(tline, TOK_NUMBER)) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002102 error(ERR_NONFATAL, "`%s' expects a parameter count", directive);
H. Peter Anvina26433d2008-07-16 14:40:01 -07002103 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002104 def->nparam_min = def->nparam_max =
2105 readnum(tline->text, &err);
2106 if (err)
2107 error(ERR_NONFATAL,
2108 "unable to parse parameter count `%s'", tline->text);
H. Peter Anvina26433d2008-07-16 14:40:01 -07002109 }
2110 if (tline && tok_is_(tline->next, "-")) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002111 tline = tline->next->next;
2112 if (tok_is_(tline, "*")) {
2113 def->nparam_max = INT_MAX;
2114 } else if (!tok_type_(tline, TOK_NUMBER)) {
2115 error(ERR_NONFATAL,
2116 "`%s' expects a parameter count after `-'", directive);
2117 } else {
2118 def->nparam_max = readnum(tline->text, &err);
2119 if (err) {
2120 error(ERR_NONFATAL, "unable to parse parameter count `%s'",
2121 tline->text);
2122 }
2123 if (def->nparam_min > def->nparam_max) {
2124 error(ERR_NONFATAL, "minimum parameter count exceeds maximum");
2125 }
2126 }
H. Peter Anvina26433d2008-07-16 14:40:01 -07002127 }
2128 if (tline && tok_is_(tline->next, "+")) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002129 tline = tline->next;
2130 def->plus = true;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002131 }
2132 if (tline && tok_type_(tline->next, TOK_ID) &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002133 !nasm_stricmp(tline->next->text, ".nolist")) {
2134 tline = tline->next;
2135 def->nolist = true;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002136 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002137
H. Peter Anvina26433d2008-07-16 14:40:01 -07002138 /*
2139 * Handle default parameters.
2140 */
2141 if (tline && tline->next) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002142 def->dlist = tline->next;
2143 tline->next = NULL;
2144 count_mmac_params(def->dlist, &def->ndefs, &def->defaults);
H. Peter Anvina26433d2008-07-16 14:40:01 -07002145 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002146 def->dlist = NULL;
2147 def->defaults = NULL;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002148 }
2149 def->expansion = NULL;
2150
H. Peter Anvin89cee572009-07-15 09:16:54 -04002151 if (def->defaults && def->ndefs > def->nparam_max - def->nparam_min &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002152 !def->plus)
2153 error(ERR_WARNING|ERR_PASS1|ERR_WARN_MDP,
2154 "too many default macro parameters");
Victor van den Elzenb916ede2008-07-23 15:14:22 +02002155
H. Peter Anvina26433d2008-07-16 14:40:01 -07002156 return true;
2157}
2158
2159
2160/*
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002161 * Decode a size directive
2162 */
2163static int parse_size(const char *str) {
2164 static const char *size_names[] =
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002165 { "byte", "dword", "oword", "qword", "tword", "word", "yword" };
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002166 static const int sizes[] =
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002167 { 0, 1, 4, 16, 8, 10, 2, 32 };
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002168
Cyrill Gorcunova7319242010-06-03 22:04:36 +04002169 return sizes[bsii(str, size_names, ARRAY_SIZE(size_names))+1];
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002170}
2171
Ed Beroset3ab3f412002-06-11 03:31:49 +00002172/**
2173 * find and process preprocessor directive in passed line
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002174 * Find out if a line contains a preprocessor directive, and deal
2175 * with it if so.
H. Peter Anvin70653092007-10-19 14:42:29 -07002176 *
Ed Beroset3ab3f412002-06-11 03:31:49 +00002177 * If a directive _is_ found, it is the responsibility of this routine
2178 * (and not the caller) to free_tlist() the line.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002179 *
Ed Beroset3ab3f412002-06-11 03:31:49 +00002180 * @param tline a pointer to the current tokeninzed line linked list
2181 * @return DIRECTIVE_FOUND or NO_DIRECTIVE_FOUND
H. Peter Anvin70653092007-10-19 14:42:29 -07002182 *
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002183 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002184static int do_directive(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00002185{
H. Peter Anvin4169a472007-09-12 01:29:43 +00002186 enum preproc_token i;
2187 int j;
H. Peter Anvin70055962007-10-11 00:05:31 -07002188 bool err;
2189 int nparam;
2190 bool nolist;
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07002191 bool casesense;
H. Peter Anvin8cfdb9d2007-09-14 18:36:01 -07002192 int k, m;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002193 int offset;
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08002194 char *p, *pp;
2195 const char *mname;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002196 Include *inc;
2197 Context *ctx;
2198 Cond *cond;
H. Peter Anvin97a23472007-09-16 17:57:25 -07002199 MMacro *mmac, **mmhead;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002200 Token *t, *tt, *param_start, *macro_start, *last, **tptr, *origline;
2201 Line *l;
2202 struct tokenval tokval;
2203 expr *evalresult;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002204 MMacro *tmp_defining; /* Used when manipulating rep_nest */
H. Peter Anvinf8ba53e2007-10-11 10:11:57 -07002205 int64_t count;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07002206 size_t len;
H. Peter Anvin8e3f75e2008-09-24 00:21:58 -07002207 int severity;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002208
2209 origline = tline;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002210
H. Peter Anvineba20a72002-04-30 20:53:55 +00002211 skip_white_(tline);
H. Peter Anvinf2936d72008-06-04 15:11:23 -07002212 if (!tline || !tok_type_(tline, TOK_PREPROC_ID) ||
H. Peter Anvine2c80182005-01-15 22:15:51 +00002213 (tline->text[1] == '%' || tline->text[1] == '$'
2214 || tline->text[1] == '!'))
2215 return NO_DIRECTIVE_FOUND;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002216
H. Peter Anvin4169a472007-09-12 01:29:43 +00002217 i = pp_token_hash(tline->text);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002218
2219 /*
Cyrill Gorcunovf09116f2010-02-28 11:52:53 +03002220 * FIXME: We zap execution of PP_RMACRO, PP_IRMACRO, PP_EXITMACRO
2221 * since they are known to be buggy at moment, we need to fix them
2222 * in future release (2.09-2.10)
2223 */
2224 if (i == PP_RMACRO || i == PP_RMACRO || i == PP_EXITMACRO) {
2225 error(ERR_NONFATAL, "unknown preprocessor directive `%s'",
2226 tline->text);
2227 return NO_DIRECTIVE_FOUND;
2228 }
2229
2230 /*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002231 * If we're in a non-emitting branch of a condition construct,
H. Peter Anvin76690a12002-04-30 20:52:49 +00002232 * or walking to the end of an already terminated %rep block,
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002233 * we should ignore all directives except for condition
2234 * directives.
2235 */
H. Peter Anvin76690a12002-04-30 20:52:49 +00002236 if (((istk->conds && !emitting(istk->conds->state)) ||
H. Peter Anvine2c80182005-01-15 22:15:51 +00002237 (istk->mstk && !istk->mstk->in_progress)) && !is_condition(i)) {
2238 return NO_DIRECTIVE_FOUND;
H. Peter Anvineba20a72002-04-30 20:53:55 +00002239 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002240
2241 /*
H. Peter Anvin76690a12002-04-30 20:52:49 +00002242 * If we're defining a macro or reading a %rep block, we should
Victor van den Elzen8f1120f2008-07-16 13:41:37 +02002243 * ignore all directives except for %macro/%imacro (which nest),
2244 * %endm/%endmacro, and (only if we're in a %rep block) %endrep.
2245 * If we're in a %rep block, another %rep nests, so should be let through.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002246 */
2247 if (defining && i != PP_MACRO && i != PP_IMACRO &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002248 i != PP_RMACRO && i != PP_IRMACRO &&
H. Peter Anvine2c80182005-01-15 22:15:51 +00002249 i != PP_ENDMACRO && i != PP_ENDM &&
2250 (defining->name || (i != PP_ENDREP && i != PP_REP))) {
2251 return NO_DIRECTIVE_FOUND;
H. Peter Anvineba20a72002-04-30 20:53:55 +00002252 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002253
Charles Crayned4200be2008-07-12 16:42:33 -07002254 if (defining) {
Keith Kanios852f1ee2009-07-12 00:19:55 -05002255 if (i == PP_MACRO || i == PP_IMACRO ||
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002256 i == PP_RMACRO || i == PP_IRMACRO) {
Charles Crayned4200be2008-07-12 16:42:33 -07002257 nested_mac_count++;
2258 return NO_DIRECTIVE_FOUND;
2259 } else if (nested_mac_count > 0) {
2260 if (i == PP_ENDMACRO) {
2261 nested_mac_count--;
2262 return NO_DIRECTIVE_FOUND;
2263 }
2264 }
2265 if (!defining->name) {
2266 if (i == PP_REP) {
2267 nested_rep_count++;
2268 return NO_DIRECTIVE_FOUND;
2269 } else if (nested_rep_count > 0) {
2270 if (i == PP_ENDREP) {
2271 nested_rep_count--;
2272 return NO_DIRECTIVE_FOUND;
2273 }
2274 }
2275 }
2276 }
2277
H. Peter Anvin4169a472007-09-12 01:29:43 +00002278 switch (i) {
2279 case PP_INVALID:
H. Peter Anvine2c80182005-01-15 22:15:51 +00002280 error(ERR_NONFATAL, "unknown preprocessor directive `%s'",
2281 tline->text);
2282 return NO_DIRECTIVE_FOUND; /* didn't get it */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002283
H. Peter Anvine2c80182005-01-15 22:15:51 +00002284 case PP_STACKSIZE:
2285 /* Directive to tell NASM what the default stack size is. The
2286 * default is for a 16-bit stack, and this can be overriden with
2287 * %stacksize large.
H. Peter Anvine2c80182005-01-15 22:15:51 +00002288 */
2289 tline = tline->next;
2290 if (tline && tline->type == TOK_WHITESPACE)
2291 tline = tline->next;
2292 if (!tline || tline->type != TOK_ID) {
2293 error(ERR_NONFATAL, "`%%stacksize' missing size parameter");
2294 free_tlist(origline);
2295 return DIRECTIVE_FOUND;
2296 }
2297 if (nasm_stricmp(tline->text, "flat") == 0) {
2298 /* All subsequent ARG directives are for a 32-bit stack */
2299 StackSize = 4;
2300 StackPointer = "ebp";
2301 ArgOffset = 8;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002302 LocalOffset = 0;
Charles Crayne7eaf9192007-11-08 22:11:14 -08002303 } else if (nasm_stricmp(tline->text, "flat64") == 0) {
2304 /* All subsequent ARG directives are for a 64-bit stack */
2305 StackSize = 8;
2306 StackPointer = "rbp";
Per Jessen53252e02010-02-11 00:16:59 +03002307 ArgOffset = 16;
Charles Crayne7eaf9192007-11-08 22:11:14 -08002308 LocalOffset = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002309 } else if (nasm_stricmp(tline->text, "large") == 0) {
2310 /* All subsequent ARG directives are for a 16-bit stack,
2311 * far function call.
2312 */
2313 StackSize = 2;
2314 StackPointer = "bp";
2315 ArgOffset = 4;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002316 LocalOffset = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002317 } else if (nasm_stricmp(tline->text, "small") == 0) {
2318 /* All subsequent ARG directives are for a 16-bit stack,
2319 * far function call. We don't support near functions.
2320 */
2321 StackSize = 2;
2322 StackPointer = "bp";
2323 ArgOffset = 6;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002324 LocalOffset = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002325 } else {
2326 error(ERR_NONFATAL, "`%%stacksize' invalid size type");
2327 free_tlist(origline);
2328 return DIRECTIVE_FOUND;
2329 }
2330 free_tlist(origline);
2331 return DIRECTIVE_FOUND;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002332
H. Peter Anvine2c80182005-01-15 22:15:51 +00002333 case PP_ARG:
2334 /* TASM like ARG directive to define arguments to functions, in
2335 * the following form:
2336 *
2337 * ARG arg1:WORD, arg2:DWORD, arg4:QWORD
2338 */
2339 offset = ArgOffset;
2340 do {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00002341 char *arg, directive[256];
H. Peter Anvine2c80182005-01-15 22:15:51 +00002342 int size = StackSize;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002343
H. Peter Anvine2c80182005-01-15 22:15:51 +00002344 /* Find the argument name */
2345 tline = tline->next;
2346 if (tline && tline->type == TOK_WHITESPACE)
2347 tline = tline->next;
2348 if (!tline || tline->type != TOK_ID) {
2349 error(ERR_NONFATAL, "`%%arg' missing argument parameter");
2350 free_tlist(origline);
2351 return DIRECTIVE_FOUND;
2352 }
2353 arg = tline->text;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002354
H. Peter Anvine2c80182005-01-15 22:15:51 +00002355 /* Find the argument size type */
2356 tline = tline->next;
2357 if (!tline || tline->type != TOK_OTHER
2358 || tline->text[0] != ':') {
2359 error(ERR_NONFATAL,
2360 "Syntax error processing `%%arg' directive");
2361 free_tlist(origline);
2362 return DIRECTIVE_FOUND;
2363 }
2364 tline = tline->next;
2365 if (!tline || tline->type != TOK_ID) {
2366 error(ERR_NONFATAL, "`%%arg' missing size type parameter");
2367 free_tlist(origline);
2368 return DIRECTIVE_FOUND;
2369 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002370
H. Peter Anvine2c80182005-01-15 22:15:51 +00002371 /* Allow macro expansion of type parameter */
Keith Kaniosb7a89542007-04-12 02:40:54 +00002372 tt = tokenize(tline->text);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002373 tt = expand_smacro(tt);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002374 size = parse_size(tt->text);
2375 if (!size) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002376 error(ERR_NONFATAL,
2377 "Invalid size type for `%%arg' missing directive");
2378 free_tlist(tt);
2379 free_tlist(origline);
2380 return DIRECTIVE_FOUND;
2381 }
2382 free_tlist(tt);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002383
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002384 /* Round up to even stack slots */
2385 size = ALIGN(size, StackSize);
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002386
H. Peter Anvine2c80182005-01-15 22:15:51 +00002387 /* Now define the macro for the argument */
2388 snprintf(directive, sizeof(directive), "%%define %s (%s+%d)",
2389 arg, StackPointer, offset);
Keith Kaniosb7a89542007-04-12 02:40:54 +00002390 do_directive(tokenize(directive));
H. Peter Anvine2c80182005-01-15 22:15:51 +00002391 offset += size;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002392
H. Peter Anvine2c80182005-01-15 22:15:51 +00002393 /* Move to the next argument in the list */
2394 tline = tline->next;
2395 if (tline && tline->type == TOK_WHITESPACE)
2396 tline = tline->next;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002397 } while (tline && tline->type == TOK_OTHER && tline->text[0] == ',');
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002398 ArgOffset = offset;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002399 free_tlist(origline);
2400 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002401
H. Peter Anvine2c80182005-01-15 22:15:51 +00002402 case PP_LOCAL:
2403 /* TASM like LOCAL directive to define local variables for a
2404 * function, in the following form:
2405 *
2406 * LOCAL local1:WORD, local2:DWORD, local4:QWORD = LocalSize
2407 *
2408 * The '= LocalSize' at the end is ignored by NASM, but is
2409 * required by TASM to define the local parameter size (and used
2410 * by the TASM macro package).
2411 */
2412 offset = LocalOffset;
2413 do {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00002414 char *local, directive[256];
H. Peter Anvine2c80182005-01-15 22:15:51 +00002415 int size = StackSize;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002416
H. Peter Anvine2c80182005-01-15 22:15:51 +00002417 /* Find the argument name */
2418 tline = tline->next;
2419 if (tline && tline->type == TOK_WHITESPACE)
2420 tline = tline->next;
2421 if (!tline || tline->type != TOK_ID) {
2422 error(ERR_NONFATAL,
2423 "`%%local' missing argument parameter");
2424 free_tlist(origline);
2425 return DIRECTIVE_FOUND;
2426 }
2427 local = tline->text;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002428
H. Peter Anvine2c80182005-01-15 22:15:51 +00002429 /* Find the argument size type */
2430 tline = tline->next;
2431 if (!tline || tline->type != TOK_OTHER
2432 || tline->text[0] != ':') {
2433 error(ERR_NONFATAL,
2434 "Syntax error processing `%%local' directive");
2435 free_tlist(origline);
2436 return DIRECTIVE_FOUND;
2437 }
2438 tline = tline->next;
2439 if (!tline || tline->type != TOK_ID) {
2440 error(ERR_NONFATAL,
2441 "`%%local' missing size type parameter");
2442 free_tlist(origline);
2443 return DIRECTIVE_FOUND;
2444 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00002445
H. Peter Anvine2c80182005-01-15 22:15:51 +00002446 /* Allow macro expansion of type parameter */
Keith Kaniosb7a89542007-04-12 02:40:54 +00002447 tt = tokenize(tline->text);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002448 tt = expand_smacro(tt);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002449 size = parse_size(tt->text);
2450 if (!size) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002451 error(ERR_NONFATAL,
2452 "Invalid size type for `%%local' missing directive");
2453 free_tlist(tt);
2454 free_tlist(origline);
2455 return DIRECTIVE_FOUND;
2456 }
2457 free_tlist(tt);
H. Peter Anvin734b1882002-04-30 21:01:08 +00002458
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002459 /* Round up to even stack slots */
2460 size = ALIGN(size, StackSize);
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002461
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002462 offset += size; /* Negative offset, increment before */
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002463
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002464 /* Now define the macro for the argument */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002465 snprintf(directive, sizeof(directive), "%%define %s (%s-%d)",
2466 local, StackPointer, offset);
Keith Kaniosb7a89542007-04-12 02:40:54 +00002467 do_directive(tokenize(directive));
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002468
H. Peter Anvine2c80182005-01-15 22:15:51 +00002469 /* Now define the assign to setup the enter_c macro correctly */
2470 snprintf(directive, sizeof(directive),
2471 "%%assign %%$localsize %%$localsize+%d", size);
Keith Kaniosb7a89542007-04-12 02:40:54 +00002472 do_directive(tokenize(directive));
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002473
H. Peter Anvine2c80182005-01-15 22:15:51 +00002474 /* Move to the next argument in the list */
2475 tline = tline->next;
2476 if (tline && tline->type == TOK_WHITESPACE)
2477 tline = tline->next;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002478 } while (tline && tline->type == TOK_OTHER && tline->text[0] == ',');
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002479 LocalOffset = offset;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002480 free_tlist(origline);
2481 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002482
H. Peter Anvine2c80182005-01-15 22:15:51 +00002483 case PP_CLEAR:
2484 if (tline->next)
H. Peter Anvin917a3492008-09-24 09:14:49 -07002485 error(ERR_WARNING|ERR_PASS1,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002486 "trailing garbage after `%%clear' ignored");
2487 free_macros();
2488 init_macros();
H. Peter Anvine2c80182005-01-15 22:15:51 +00002489 free_tlist(origline);
2490 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002491
H. Peter Anvin418ca702008-05-30 10:42:30 -07002492 case PP_DEPEND:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002493 t = tline->next = expand_smacro(tline->next);
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002494 skip_white_(t);
2495 if (!t || (t->type != TOK_STRING &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002496 t->type != TOK_INTERNAL_STRING)) {
H. Peter Anvin418ca702008-05-30 10:42:30 -07002497 error(ERR_NONFATAL, "`%%depend' expects a file name");
2498 free_tlist(origline);
2499 return DIRECTIVE_FOUND; /* but we did _something_ */
2500 }
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002501 if (t->next)
H. Peter Anvin917a3492008-09-24 09:14:49 -07002502 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvin418ca702008-05-30 10:42:30 -07002503 "trailing garbage after `%%depend' ignored");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002504 p = t->text;
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002505 if (t->type != TOK_INTERNAL_STRING)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002506 nasm_unquote_cstr(p, i);
2507 if (dephead && !in_list(*dephead, p)) {
2508 StrList *sl = nasm_malloc(strlen(p)+1+sizeof sl->next);
2509 sl->next = NULL;
2510 strcpy(sl->str, p);
2511 *deptail = sl;
2512 deptail = &sl->next;
2513 }
2514 free_tlist(origline);
H. Peter Anvin418ca702008-05-30 10:42:30 -07002515 return DIRECTIVE_FOUND;
2516
2517 case PP_INCLUDE:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002518 t = tline->next = expand_smacro(tline->next);
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002519 skip_white_(t);
H. Peter Anvind2456592008-06-19 15:04:18 -07002520
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002521 if (!t || (t->type != TOK_STRING &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002522 t->type != TOK_INTERNAL_STRING)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002523 error(ERR_NONFATAL, "`%%include' expects a file name");
2524 free_tlist(origline);
2525 return DIRECTIVE_FOUND; /* but we did _something_ */
2526 }
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002527 if (t->next)
H. Peter Anvin917a3492008-09-24 09:14:49 -07002528 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +00002529 "trailing garbage after `%%include' ignored");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002530 p = t->text;
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002531 if (t->type != TOK_INTERNAL_STRING)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002532 nasm_unquote_cstr(p, i);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002533 inc = nasm_malloc(sizeof(Include));
2534 inc->next = istk;
2535 inc->conds = NULL;
H. Peter Anvin2b1c3b92008-06-06 10:38:46 -07002536 inc->fp = inc_fopen(p, dephead, &deptail, pass == 0);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002537 if (!inc->fp) {
2538 /* -MG given but file not found */
2539 nasm_free(inc);
2540 } else {
2541 inc->fname = src_set_fname(nasm_strdup(p));
2542 inc->lineno = src_set_linnum(0);
2543 inc->lineinc = 1;
2544 inc->expansion = NULL;
2545 inc->mstk = NULL;
2546 istk = inc;
2547 list->uplevel(LIST_INCLUDE);
2548 }
2549 free_tlist(origline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002550 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002551
H. Peter Anvind2456592008-06-19 15:04:18 -07002552 case PP_USE:
H. Peter Anvinf4ae5ad2008-06-19 18:39:24 -07002553 {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002554 static macros_t *use_pkg;
2555 const char *pkg_macro = NULL;
H. Peter Anvinf4ae5ad2008-06-19 18:39:24 -07002556
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002557 tline = tline->next;
2558 skip_white_(tline);
2559 tline = expand_id(tline);
H. Peter Anvind2456592008-06-19 15:04:18 -07002560
H. Peter Anvin264b7b92008-10-24 16:38:17 -07002561 if (!tline || (tline->type != TOK_STRING &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002562 tline->type != TOK_INTERNAL_STRING &&
2563 tline->type != TOK_ID)) {
H. Peter Anvin926fc402008-06-19 16:26:12 -07002564 error(ERR_NONFATAL, "`%%use' expects a package name");
H. Peter Anvind2456592008-06-19 15:04:18 -07002565 free_tlist(origline);
2566 return DIRECTIVE_FOUND; /* but we did _something_ */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002567 }
H. Peter Anvin264b7b92008-10-24 16:38:17 -07002568 if (tline->next)
H. Peter Anvin917a3492008-09-24 09:14:49 -07002569 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvind2456592008-06-19 15:04:18 -07002570 "trailing garbage after `%%use' ignored");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002571 if (tline->type == TOK_STRING)
2572 nasm_unquote_cstr(tline->text, i);
2573 use_pkg = nasm_stdmac_find_package(tline->text);
2574 if (!use_pkg)
2575 error(ERR_NONFATAL, "unknown `%%use' package: %s", tline->text);
2576 else
2577 pkg_macro = (char *)use_pkg + 1; /* The first string will be <%define>__USE_*__ */
Victor van den Elzen35eb2ea2010-03-10 22:33:48 +01002578 if (use_pkg && ! smacro_defined(NULL, pkg_macro, 0, NULL, true)) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002579 /* Not already included, go ahead and include it */
2580 stdmacpos = use_pkg;
2581 }
2582 free_tlist(origline);
H. Peter Anvind2456592008-06-19 15:04:18 -07002583 return DIRECTIVE_FOUND;
H. Peter Anvinf4ae5ad2008-06-19 18:39:24 -07002584 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002585 case PP_PUSH:
H. Peter Anvine2c80182005-01-15 22:15:51 +00002586 case PP_REPL:
H. Peter Anvin42b56392008-10-24 16:24:21 -07002587 case PP_POP:
H. Peter Anvine2c80182005-01-15 22:15:51 +00002588 tline = tline->next;
2589 skip_white_(tline);
2590 tline = expand_id(tline);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002591 if (tline) {
2592 if (!tok_type_(tline, TOK_ID)) {
2593 error(ERR_NONFATAL, "`%s' expects a context identifier",
2594 pp_directives[i]);
2595 free_tlist(origline);
2596 return DIRECTIVE_FOUND; /* but we did _something_ */
2597 }
2598 if (tline->next)
2599 error(ERR_WARNING|ERR_PASS1,
2600 "trailing garbage after `%s' ignored",
2601 pp_directives[i]);
2602 p = nasm_strdup(tline->text);
2603 } else {
2604 p = NULL; /* Anonymous */
2605 }
H. Peter Anvin42b56392008-10-24 16:24:21 -07002606
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002607 if (i == PP_PUSH) {
2608 ctx = nasm_malloc(sizeof(Context));
2609 ctx->next = cstk;
2610 hash_init(&ctx->localmac, HASH_SMALL);
2611 ctx->name = p;
2612 ctx->number = unique++;
2613 cstk = ctx;
2614 } else {
2615 /* %pop or %repl */
2616 if (!cstk) {
2617 error(ERR_NONFATAL, "`%s': context stack is empty",
2618 pp_directives[i]);
2619 } else if (i == PP_POP) {
2620 if (p && (!cstk->name || nasm_stricmp(p, cstk->name)))
2621 error(ERR_NONFATAL, "`%%pop' in wrong context: %s, "
2622 "expected %s",
2623 cstk->name ? cstk->name : "anonymous", p);
2624 else
2625 ctx_pop();
2626 } else {
2627 /* i == PP_REPL */
2628 nasm_free(cstk->name);
2629 cstk->name = p;
2630 p = NULL;
2631 }
2632 nasm_free(p);
2633 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002634 free_tlist(origline);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002635 return DIRECTIVE_FOUND;
H. Peter Anvin8e3f75e2008-09-24 00:21:58 -07002636 case PP_FATAL:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002637 severity = ERR_FATAL;
2638 goto issue_error;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002639 case PP_ERROR:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002640 severity = ERR_NONFATAL;
2641 goto issue_error;
H. Peter Anvin7df04172008-06-10 18:27:38 -07002642 case PP_WARNING:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002643 severity = ERR_WARNING|ERR_WARN_USER;
2644 goto issue_error;
H. Peter Anvin8e3f75e2008-09-24 00:21:58 -07002645
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002646issue_error:
H. Peter Anvin7df04172008-06-10 18:27:38 -07002647 {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002648 /* Only error out if this is the final pass */
2649 if (pass != 2 && i != PP_FATAL)
2650 return DIRECTIVE_FOUND;
2651
2652 tline->next = expand_smacro(tline->next);
2653 tline = tline->next;
2654 skip_white_(tline);
2655 t = tline ? tline->next : NULL;
2656 skip_white_(t);
2657 if (tok_type_(tline, TOK_STRING) && !t) {
2658 /* The line contains only a quoted string */
2659 p = tline->text;
2660 nasm_unquote(p, NULL); /* Ignore NUL character truncation */
2661 error(severity, "%s", p);
2662 } else {
2663 /* Not a quoted string, or more than a quoted string */
2664 p = detoken(tline, false);
2665 error(severity, "%s", p);
2666 nasm_free(p);
2667 }
2668 free_tlist(origline);
2669 return DIRECTIVE_FOUND;
H. Peter Anvin7df04172008-06-10 18:27:38 -07002670 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00002671
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002672 CASE_PP_IF:
H. Peter Anvine2c80182005-01-15 22:15:51 +00002673 if (istk->conds && !emitting(istk->conds->state))
2674 j = COND_NEVER;
2675 else {
2676 j = if_condition(tline->next, i);
2677 tline->next = NULL; /* it got freed */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002678 j = j < 0 ? COND_NEVER : j ? COND_IF_TRUE : COND_IF_FALSE;
2679 }
2680 cond = nasm_malloc(sizeof(Cond));
2681 cond->next = istk->conds;
2682 cond->state = j;
2683 istk->conds = cond;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002684 if(istk->mstk)
Keith Kanios3c0d91f2009-10-25 13:28:03 -05002685 istk->mstk->condcnt ++;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002686 free_tlist(origline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002687 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002688
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002689 CASE_PP_ELIF:
H. Peter Anvine2c80182005-01-15 22:15:51 +00002690 if (!istk->conds)
H. Peter Anvin4169a472007-09-12 01:29:43 +00002691 error(ERR_FATAL, "`%s': no matching `%%if'", pp_directives[i]);
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002692 switch(istk->conds->state) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002693 case COND_IF_TRUE:
2694 istk->conds->state = COND_DONE;
2695 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002696
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002697 case COND_DONE:
2698 case COND_NEVER:
2699 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002700
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002701 case COND_ELSE_TRUE:
2702 case COND_ELSE_FALSE:
2703 error_precond(ERR_WARNING|ERR_PASS1,
2704 "`%%elif' after `%%else' ignored");
2705 istk->conds->state = COND_NEVER;
2706 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002707
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002708 case COND_IF_FALSE:
2709 /*
2710 * IMPORTANT: In the case of %if, we will already have
2711 * called expand_mmac_params(); however, if we're
2712 * processing an %elif we must have been in a
2713 * non-emitting mode, which would have inhibited
2714 * the normal invocation of expand_mmac_params().
2715 * Therefore, we have to do it explicitly here.
2716 */
2717 j = if_condition(expand_mmac_params(tline->next), i);
2718 tline->next = NULL; /* it got freed */
2719 istk->conds->state =
2720 j < 0 ? COND_NEVER : j ? COND_IF_TRUE : COND_IF_FALSE;
2721 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002722 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002723 free_tlist(origline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002724 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002725
H. Peter Anvine2c80182005-01-15 22:15:51 +00002726 case PP_ELSE:
2727 if (tline->next)
H. Peter Anvin917a3492008-09-24 09:14:49 -07002728 error_precond(ERR_WARNING|ERR_PASS1,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002729 "trailing garbage after `%%else' ignored");
H. Peter Anvine2c80182005-01-15 22:15:51 +00002730 if (!istk->conds)
2731 error(ERR_FATAL, "`%%else': no matching `%%if'");
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002732 switch(istk->conds->state) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002733 case COND_IF_TRUE:
2734 case COND_DONE:
2735 istk->conds->state = COND_ELSE_FALSE;
2736 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002737
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002738 case COND_NEVER:
2739 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002740
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002741 case COND_IF_FALSE:
2742 istk->conds->state = COND_ELSE_TRUE;
2743 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002744
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002745 case COND_ELSE_TRUE:
2746 case COND_ELSE_FALSE:
2747 error_precond(ERR_WARNING|ERR_PASS1,
2748 "`%%else' after `%%else' ignored.");
2749 istk->conds->state = COND_NEVER;
2750 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002751 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002752 free_tlist(origline);
2753 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002754
H. Peter Anvine2c80182005-01-15 22:15:51 +00002755 case PP_ENDIF:
2756 if (tline->next)
H. Peter Anvin917a3492008-09-24 09:14:49 -07002757 error_precond(ERR_WARNING|ERR_PASS1,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002758 "trailing garbage after `%%endif' ignored");
H. Peter Anvine2c80182005-01-15 22:15:51 +00002759 if (!istk->conds)
2760 error(ERR_FATAL, "`%%endif': no matching `%%if'");
2761 cond = istk->conds;
2762 istk->conds = cond->next;
2763 nasm_free(cond);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002764 if(istk->mstk)
Keith Kanios3c0d91f2009-10-25 13:28:03 -05002765 istk->mstk->condcnt --;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002766 free_tlist(origline);
2767 return DIRECTIVE_FOUND;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002768
H. Peter Anvindb8f96e2009-07-15 09:07:29 -04002769 case PP_RMACRO:
2770 case PP_IRMACRO:
H. Peter Anvine2c80182005-01-15 22:15:51 +00002771 case PP_MACRO:
2772 case PP_IMACRO:
H. Peter Anvina26433d2008-07-16 14:40:01 -07002773 if (defining) {
H. Peter Anvindb8f96e2009-07-15 09:07:29 -04002774 error(ERR_FATAL, "`%s': already defining a macro",
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002775 pp_directives[i]);
2776 return DIRECTIVE_FOUND;
2777 }
2778 defining = nasm_malloc(sizeof(MMacro));
2779 defining->max_depth =
2780 (i == PP_RMACRO) || (i == PP_IRMACRO) ? DEADMAN_LIMIT : 0;
2781 defining->casesense = (i == PP_MACRO) || (i == PP_RMACRO);
2782 if (!parse_mmacro_spec(tline, defining, pp_directives[i])) {
2783 nasm_free(defining);
2784 defining = NULL;
2785 return DIRECTIVE_FOUND;
2786 }
H. Peter Anvina26433d2008-07-16 14:40:01 -07002787
H. Peter Anvin166c2472008-05-28 12:28:58 -07002788 mmac = (MMacro *) hash_findix(&mmacros, defining->name);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002789 while (mmac) {
2790 if (!strcmp(mmac->name, defining->name) &&
2791 (mmac->nparam_min <= defining->nparam_max
2792 || defining->plus)
2793 && (defining->nparam_min <= mmac->nparam_max
2794 || mmac->plus)) {
H. Peter Anvin917a3492008-09-24 09:14:49 -07002795 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +00002796 "redefining multi-line macro `%s'", defining->name);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002797 return DIRECTIVE_FOUND;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002798 }
2799 mmac = mmac->next;
2800 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002801 free_tlist(origline);
2802 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002803
H. Peter Anvine2c80182005-01-15 22:15:51 +00002804 case PP_ENDM:
2805 case PP_ENDMACRO:
Victor van den Elzen8f1120f2008-07-16 13:41:37 +02002806 if (! (defining && defining->name)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002807 error(ERR_NONFATAL, "`%s': not defining a macro", tline->text);
2808 return DIRECTIVE_FOUND;
2809 }
Keith Kanios852f1ee2009-07-12 00:19:55 -05002810 mmhead = (MMacro **) hash_findi_add(&mmacros, defining->name);
H. Peter Anvin97a23472007-09-16 17:57:25 -07002811 defining->next = *mmhead;
Keith Kanios852f1ee2009-07-12 00:19:55 -05002812 *mmhead = defining;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002813 defining = NULL;
2814 free_tlist(origline);
2815 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002816
H. Peter Anvin89cee572009-07-15 09:16:54 -04002817 case PP_EXITMACRO:
Keith Kanios852f1ee2009-07-12 00:19:55 -05002818 /*
2819 * We must search along istk->expansion until we hit a
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002820 * macro-end marker for a macro with a name. Then we
Keith Kanios3c0d91f2009-10-25 13:28:03 -05002821 * bypass all lines between exitmacro and endmacro.
Keith Kanios852f1ee2009-07-12 00:19:55 -05002822 */
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04002823 list_for_each(l, istk->expansion)
Keith Kanios852f1ee2009-07-12 00:19:55 -05002824 if (l->finishes && l->finishes->name)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002825 break;
Keith Kanios852f1ee2009-07-12 00:19:55 -05002826
2827 if (l) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002828 /*
2829 * Remove all conditional entries relative to this
2830 * macro invocation. (safe to do in this context)
2831 */
Keith Kanios3c0d91f2009-10-25 13:28:03 -05002832 for ( ; l->finishes->condcnt > 0; l->finishes->condcnt --) {
2833 cond = istk->conds;
2834 istk->conds = cond->next;
2835 nasm_free(cond);
2836 }
2837 istk->expansion = l;
Keith Kanios852f1ee2009-07-12 00:19:55 -05002838 } else {
2839 error(ERR_NONFATAL, "`%%exitmacro' not within `%%macro' block");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002840 }
Keith Kanios852f1ee2009-07-12 00:19:55 -05002841 free_tlist(origline);
2842 return DIRECTIVE_FOUND;
2843
H. Peter Anvina26433d2008-07-16 14:40:01 -07002844 case PP_UNMACRO:
2845 case PP_UNIMACRO:
2846 {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002847 MMacro **mmac_p;
2848 MMacro spec;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002849
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002850 spec.casesense = (i == PP_UNMACRO);
2851 if (!parse_mmacro_spec(tline, &spec, pp_directives[i])) {
2852 return DIRECTIVE_FOUND;
2853 }
H. Peter Anvina26433d2008-07-16 14:40:01 -07002854 mmac_p = (MMacro **) hash_findi(&mmacros, spec.name, NULL);
2855 while (mmac_p && *mmac_p) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002856 mmac = *mmac_p;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002857 if (mmac->casesense == spec.casesense &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002858 !mstrcmp(mmac->name, spec.name, spec.casesense) &&
H. Peter Anvina26433d2008-07-16 14:40:01 -07002859 mmac->nparam_min == spec.nparam_min &&
2860 mmac->nparam_max == spec.nparam_max &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002861 mmac->plus == spec.plus) {
2862 *mmac_p = mmac->next;
2863 free_mmacro(mmac);
2864 } else {
2865 mmac_p = &mmac->next;
2866 }
2867 }
2868 free_tlist(origline);
2869 free_tlist(spec.dlist);
2870 return DIRECTIVE_FOUND;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002871 }
2872
H. Peter Anvine2c80182005-01-15 22:15:51 +00002873 case PP_ROTATE:
2874 if (tline->next && tline->next->type == TOK_WHITESPACE)
2875 tline = tline->next;
H. Peter Anvin89cee572009-07-15 09:16:54 -04002876 if (!tline->next) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002877 free_tlist(origline);
2878 error(ERR_NONFATAL, "`%%rotate' missing rotate count");
2879 return DIRECTIVE_FOUND;
2880 }
2881 t = expand_smacro(tline->next);
2882 tline->next = NULL;
2883 free_tlist(origline);
2884 tline = t;
2885 tptr = &t;
2886 tokval.t_type = TOKEN_INVALID;
2887 evalresult =
2888 evaluate(ppscan, tptr, &tokval, NULL, pass, error, NULL);
2889 free_tlist(tline);
2890 if (!evalresult)
2891 return DIRECTIVE_FOUND;
2892 if (tokval.t_type)
H. Peter Anvin917a3492008-09-24 09:14:49 -07002893 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +00002894 "trailing garbage after expression ignored");
2895 if (!is_simple(evalresult)) {
2896 error(ERR_NONFATAL, "non-constant value given to `%%rotate'");
2897 return DIRECTIVE_FOUND;
2898 }
2899 mmac = istk->mstk;
2900 while (mmac && !mmac->name) /* avoid mistaking %reps for macros */
2901 mmac = mmac->next_active;
2902 if (!mmac) {
2903 error(ERR_NONFATAL, "`%%rotate' invoked outside a macro call");
2904 } else if (mmac->nparam == 0) {
2905 error(ERR_NONFATAL,
2906 "`%%rotate' invoked within macro without parameters");
2907 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002908 int rotate = mmac->rotate + reloc_value(evalresult);
H. Peter Anvin734b1882002-04-30 21:01:08 +00002909
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002910 rotate %= (int)mmac->nparam;
2911 if (rotate < 0)
2912 rotate += mmac->nparam;
H. Peter Anvin70653092007-10-19 14:42:29 -07002913
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002914 mmac->rotate = rotate;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002915 }
2916 return DIRECTIVE_FOUND;
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00002917
H. Peter Anvine2c80182005-01-15 22:15:51 +00002918 case PP_REP:
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002919 nolist = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002920 do {
2921 tline = tline->next;
2922 } while (tok_type_(tline, TOK_WHITESPACE));
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00002923
H. Peter Anvine2c80182005-01-15 22:15:51 +00002924 if (tok_type_(tline, TOK_ID) &&
2925 nasm_stricmp(tline->text, ".nolist") == 0) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002926 nolist = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002927 do {
2928 tline = tline->next;
2929 } while (tok_type_(tline, TOK_WHITESPACE));
2930 }
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00002931
H. Peter Anvine2c80182005-01-15 22:15:51 +00002932 if (tline) {
2933 t = expand_smacro(tline);
2934 tptr = &t;
2935 tokval.t_type = TOKEN_INVALID;
2936 evalresult =
2937 evaluate(ppscan, tptr, &tokval, NULL, pass, error, NULL);
2938 if (!evalresult) {
2939 free_tlist(origline);
2940 return DIRECTIVE_FOUND;
2941 }
2942 if (tokval.t_type)
H. Peter Anvin917a3492008-09-24 09:14:49 -07002943 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +00002944 "trailing garbage after expression ignored");
2945 if (!is_simple(evalresult)) {
2946 error(ERR_NONFATAL, "non-constant value given to `%%rep'");
2947 return DIRECTIVE_FOUND;
2948 }
Cyrill Gorcunove091d6e2010-08-09 13:58:22 +04002949 count = reloc_value(evalresult);
2950 if (count >= REP_LIMIT) {
Cyrill Gorcunov71f4f842010-08-09 20:17:17 +04002951 error(ERR_NONFATAL, "`%%rep' value exceeds limit");
Cyrill Gorcunove091d6e2010-08-09 13:58:22 +04002952 count = 0;
2953 } else
2954 count++;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002955 } else {
2956 error(ERR_NONFATAL, "`%%rep' expects a repeat count");
H. Peter Anvinf8ba53e2007-10-11 10:11:57 -07002957 count = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002958 }
2959 free_tlist(origline);
H. Peter Anvin734b1882002-04-30 21:01:08 +00002960
H. Peter Anvine2c80182005-01-15 22:15:51 +00002961 tmp_defining = defining;
2962 defining = nasm_malloc(sizeof(MMacro));
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002963 defining->prev = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002964 defining->name = NULL; /* flags this macro as a %rep block */
H. Peter Anvin70055962007-10-11 00:05:31 -07002965 defining->casesense = false;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002966 defining->plus = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002967 defining->nolist = nolist;
H. Peter Anvinf8ba53e2007-10-11 10:11:57 -07002968 defining->in_progress = count;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002969 defining->max_depth = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002970 defining->nparam_min = defining->nparam_max = 0;
2971 defining->defaults = NULL;
2972 defining->dlist = NULL;
2973 defining->expansion = NULL;
2974 defining->next_active = istk->mstk;
2975 defining->rep_nest = tmp_defining;
2976 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002977
H. Peter Anvine2c80182005-01-15 22:15:51 +00002978 case PP_ENDREP:
2979 if (!defining || defining->name) {
2980 error(ERR_NONFATAL, "`%%endrep': no matching `%%rep'");
2981 return DIRECTIVE_FOUND;
2982 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00002983
H. Peter Anvine2c80182005-01-15 22:15:51 +00002984 /*
2985 * Now we have a "macro" defined - although it has no name
2986 * and we won't be entering it in the hash tables - we must
2987 * push a macro-end marker for it on to istk->expansion.
2988 * After that, it will take care of propagating itself (a
2989 * macro-end marker line for a macro which is really a %rep
2990 * block will cause the macro to be re-expanded, complete
2991 * with another macro-end marker to ensure the process
2992 * continues) until the whole expansion is forcibly removed
2993 * from istk->expansion by a %exitrep.
2994 */
2995 l = nasm_malloc(sizeof(Line));
2996 l->next = istk->expansion;
2997 l->finishes = defining;
2998 l->first = NULL;
2999 istk->expansion = l;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003000
H. Peter Anvine2c80182005-01-15 22:15:51 +00003001 istk->mstk = defining;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003002
H. Peter Anvine2c80182005-01-15 22:15:51 +00003003 list->uplevel(defining->nolist ? LIST_MACRO_NOLIST : LIST_MACRO);
3004 tmp_defining = defining;
3005 defining = defining->rep_nest;
3006 free_tlist(origline);
3007 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003008
H. Peter Anvine2c80182005-01-15 22:15:51 +00003009 case PP_EXITREP:
3010 /*
3011 * We must search along istk->expansion until we hit a
3012 * macro-end marker for a macro with no name. Then we set
3013 * its `in_progress' flag to 0.
3014 */
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04003015 list_for_each(l, istk->expansion)
H. Peter Anvine2c80182005-01-15 22:15:51 +00003016 if (l->finishes && !l->finishes->name)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003017 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003018
H. Peter Anvine2c80182005-01-15 22:15:51 +00003019 if (l)
Charles Crayned4200be2008-07-12 16:42:33 -07003020 l->finishes->in_progress = 1;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003021 else
3022 error(ERR_NONFATAL, "`%%exitrep' not within `%%rep' block");
3023 free_tlist(origline);
3024 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003025
H. Peter Anvine2c80182005-01-15 22:15:51 +00003026 case PP_XDEFINE:
3027 case PP_IXDEFINE:
3028 case PP_DEFINE:
3029 case PP_IDEFINE:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003030 casesense = (i == PP_DEFINE || i == PP_XDEFINE);
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07003031
H. Peter Anvine2c80182005-01-15 22:15:51 +00003032 tline = tline->next;
3033 skip_white_(tline);
3034 tline = expand_id(tline);
3035 if (!tline || (tline->type != TOK_ID &&
3036 (tline->type != TOK_PREPROC_ID ||
3037 tline->text[1] != '$'))) {
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07003038 error(ERR_NONFATAL, "`%s' expects a macro identifier",
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003039 pp_directives[i]);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003040 free_tlist(origline);
3041 return DIRECTIVE_FOUND;
3042 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003043
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08003044 ctx = get_ctx(tline->text, &mname, false);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003045 last = tline;
3046 param_start = tline = tline->next;
3047 nparam = 0;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003048
H. Peter Anvine2c80182005-01-15 22:15:51 +00003049 /* Expand the macro definition now for %xdefine and %ixdefine */
3050 if ((i == PP_XDEFINE) || (i == PP_IXDEFINE))
3051 tline = expand_smacro(tline);
H. Peter Anvin734b1882002-04-30 21:01:08 +00003052
H. Peter Anvine2c80182005-01-15 22:15:51 +00003053 if (tok_is_(tline, "(")) {
3054 /*
3055 * This macro has parameters.
3056 */
H. Peter Anvin734b1882002-04-30 21:01:08 +00003057
H. Peter Anvine2c80182005-01-15 22:15:51 +00003058 tline = tline->next;
3059 while (1) {
3060 skip_white_(tline);
3061 if (!tline) {
3062 error(ERR_NONFATAL, "parameter identifier expected");
3063 free_tlist(origline);
3064 return DIRECTIVE_FOUND;
3065 }
3066 if (tline->type != TOK_ID) {
3067 error(ERR_NONFATAL,
3068 "`%s': parameter identifier expected",
3069 tline->text);
3070 free_tlist(origline);
3071 return DIRECTIVE_FOUND;
3072 }
3073 tline->type = TOK_SMAC_PARAM + nparam++;
3074 tline = tline->next;
3075 skip_white_(tline);
3076 if (tok_is_(tline, ",")) {
3077 tline = tline->next;
H. Peter Anvinca348b62008-07-23 10:49:26 -04003078 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003079 if (!tok_is_(tline, ")")) {
3080 error(ERR_NONFATAL,
3081 "`)' expected to terminate macro template");
3082 free_tlist(origline);
3083 return DIRECTIVE_FOUND;
3084 }
3085 break;
3086 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00003087 }
3088 last = tline;
3089 tline = tline->next;
3090 }
3091 if (tok_type_(tline, TOK_WHITESPACE))
3092 last = tline, tline = tline->next;
3093 macro_start = NULL;
3094 last->next = NULL;
3095 t = tline;
3096 while (t) {
3097 if (t->type == TOK_ID) {
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04003098 list_for_each(tt, param_start)
H. Peter Anvine2c80182005-01-15 22:15:51 +00003099 if (tt->type >= TOK_SMAC_PARAM &&
3100 !strcmp(tt->text, t->text))
3101 t->type = tt->type;
3102 }
3103 tt = t->next;
3104 t->next = macro_start;
3105 macro_start = t;
3106 t = tt;
3107 }
3108 /*
3109 * Good. We now have a macro name, a parameter count, and a
3110 * token list (in reverse order) for an expansion. We ought
3111 * to be OK just to create an SMacro, store it, and let
3112 * free_tlist have the rest of the line (which we have
3113 * carefully re-terminated after chopping off the expansion
3114 * from the end).
3115 */
H. Peter Anvin4db5a162007-10-11 13:42:09 -07003116 define_smacro(ctx, mname, casesense, nparam, macro_start);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003117 free_tlist(origline);
3118 return DIRECTIVE_FOUND;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003119
H. Peter Anvine2c80182005-01-15 22:15:51 +00003120 case PP_UNDEF:
3121 tline = tline->next;
3122 skip_white_(tline);
3123 tline = expand_id(tline);
3124 if (!tline || (tline->type != TOK_ID &&
3125 (tline->type != TOK_PREPROC_ID ||
3126 tline->text[1] != '$'))) {
3127 error(ERR_NONFATAL, "`%%undef' expects a macro identifier");
3128 free_tlist(origline);
3129 return DIRECTIVE_FOUND;
3130 }
3131 if (tline->next) {
H. Peter Anvin917a3492008-09-24 09:14:49 -07003132 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +00003133 "trailing garbage after macro name ignored");
3134 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003135
H. Peter Anvine2c80182005-01-15 22:15:51 +00003136 /* Find the context that symbol belongs to */
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08003137 ctx = get_ctx(tline->text, &mname, false);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003138 undef_smacro(ctx, mname);
3139 free_tlist(origline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003140 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003141
H. Peter Anvin9e200162008-06-04 17:23:14 -07003142 case PP_DEFSTR:
3143 case PP_IDEFSTR:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003144 casesense = (i == PP_DEFSTR);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003145
3146 tline = tline->next;
3147 skip_white_(tline);
3148 tline = expand_id(tline);
3149 if (!tline || (tline->type != TOK_ID &&
3150 (tline->type != TOK_PREPROC_ID ||
3151 tline->text[1] != '$'))) {
3152 error(ERR_NONFATAL, "`%s' expects a macro identifier",
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003153 pp_directives[i]);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003154 free_tlist(origline);
3155 return DIRECTIVE_FOUND;
3156 }
3157
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08003158 ctx = get_ctx(tline->text, &mname, false);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003159 last = tline;
3160 tline = expand_smacro(tline->next);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003161 last->next = NULL;
H. Peter Anvin9e200162008-06-04 17:23:14 -07003162
3163 while (tok_type_(tline, TOK_WHITESPACE))
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003164 tline = delete_Token(tline);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003165
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003166 p = detoken(tline, false);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003167 macro_start = nasm_malloc(sizeof(*macro_start));
3168 macro_start->next = NULL;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003169 macro_start->text = nasm_quote(p, strlen(p));
3170 macro_start->type = TOK_STRING;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003171 macro_start->a.mac = NULL;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003172 nasm_free(p);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003173
3174 /*
3175 * We now have a macro name, an implicit parameter count of
3176 * zero, and a string token to use as an expansion. Create
3177 * and store an SMacro.
3178 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003179 define_smacro(ctx, mname, casesense, 0, macro_start);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003180 free_tlist(origline);
3181 return DIRECTIVE_FOUND;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003182
H. Peter Anvin2f55bda2009-07-14 15:04:04 -04003183 case PP_DEFTOK:
3184 case PP_IDEFTOK:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003185 casesense = (i == PP_DEFTOK);
3186
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003187 tline = tline->next;
3188 skip_white_(tline);
3189 tline = expand_id(tline);
3190 if (!tline || (tline->type != TOK_ID &&
3191 (tline->type != TOK_PREPROC_ID ||
3192 tline->text[1] != '$'))) {
3193 error(ERR_NONFATAL,
3194 "`%s' expects a macro identifier as first parameter",
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003195 pp_directives[i]);
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003196 free_tlist(origline);
3197 return DIRECTIVE_FOUND;
3198 }
3199 ctx = get_ctx(tline->text, &mname, false);
3200 last = tline;
3201 tline = expand_smacro(tline->next);
3202 last->next = NULL;
3203
3204 t = tline;
3205 while (tok_type_(t, TOK_WHITESPACE))
3206 t = t->next;
3207 /* t should now point to the string */
Cyrill Gorcunov6908e582010-09-06 19:36:15 +04003208 if (!tok_type_(t, TOK_STRING)) {
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003209 error(ERR_NONFATAL,
3210 "`%s` requires string as second parameter",
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003211 pp_directives[i]);
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003212 free_tlist(tline);
3213 free_tlist(origline);
3214 return DIRECTIVE_FOUND;
3215 }
3216
H. Peter Anvinb40992c2010-09-15 08:57:21 -07003217 /*
3218 * Convert the string to a token stream. Note that smacros
3219 * are stored with the token stream reversed, so we have to
3220 * reverse the output of tokenize().
3221 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003222 nasm_unquote_cstr(t->text, i);
H. Peter Anvinb40992c2010-09-15 08:57:21 -07003223 macro_start = reverse_tokens(tokenize(t->text));
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003224
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003225 /*
3226 * We now have a macro name, an implicit parameter count of
3227 * zero, and a numeric token to use as an expansion. Create
3228 * and store an SMacro.
3229 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003230 define_smacro(ctx, mname, casesense, 0, macro_start);
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003231 free_tlist(tline);
3232 free_tlist(origline);
3233 return DIRECTIVE_FOUND;
H. Peter Anvin9e200162008-06-04 17:23:14 -07003234
H. Peter Anvin418ca702008-05-30 10:42:30 -07003235 case PP_PATHSEARCH:
3236 {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003237 FILE *fp;
3238 StrList *xsl = NULL;
3239 StrList **xst = &xsl;
H. Peter Anvin418ca702008-05-30 10:42:30 -07003240
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003241 casesense = true;
H. Peter Anvin418ca702008-05-30 10:42:30 -07003242
3243 tline = tline->next;
3244 skip_white_(tline);
3245 tline = expand_id(tline);
3246 if (!tline || (tline->type != TOK_ID &&
3247 (tline->type != TOK_PREPROC_ID ||
3248 tline->text[1] != '$'))) {
3249 error(ERR_NONFATAL,
3250 "`%%pathsearch' expects a macro identifier as first parameter");
3251 free_tlist(origline);
3252 return DIRECTIVE_FOUND;
3253 }
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08003254 ctx = get_ctx(tline->text, &mname, false);
H. Peter Anvin418ca702008-05-30 10:42:30 -07003255 last = tline;
3256 tline = expand_smacro(tline->next);
3257 last->next = NULL;
3258
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003259 t = tline;
H. Peter Anvin418ca702008-05-30 10:42:30 -07003260 while (tok_type_(t, TOK_WHITESPACE))
3261 t = t->next;
3262
3263 if (!t || (t->type != TOK_STRING &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003264 t->type != TOK_INTERNAL_STRING)) {
H. Peter Anvin418ca702008-05-30 10:42:30 -07003265 error(ERR_NONFATAL, "`%%pathsearch' expects a file name");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003266 free_tlist(tline);
H. Peter Anvin418ca702008-05-30 10:42:30 -07003267 free_tlist(origline);
3268 return DIRECTIVE_FOUND; /* but we did _something_ */
3269 }
3270 if (t->next)
H. Peter Anvin917a3492008-09-24 09:14:49 -07003271 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvin418ca702008-05-30 10:42:30 -07003272 "trailing garbage after `%%pathsearch' ignored");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003273 p = t->text;
H. Peter Anvin427cc912008-06-01 21:43:03 -07003274 if (t->type != TOK_INTERNAL_STRING)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003275 nasm_unquote(p, NULL);
H. Peter Anvin418ca702008-05-30 10:42:30 -07003276
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003277 fp = inc_fopen(p, &xsl, &xst, true);
3278 if (fp) {
3279 p = xsl->str;
3280 fclose(fp); /* Don't actually care about the file */
3281 }
H. Peter Anvin418ca702008-05-30 10:42:30 -07003282 macro_start = nasm_malloc(sizeof(*macro_start));
3283 macro_start->next = NULL;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003284 macro_start->text = nasm_quote(p, strlen(p));
3285 macro_start->type = TOK_STRING;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003286 macro_start->a.mac = NULL;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003287 if (xsl)
3288 nasm_free(xsl);
H. Peter Anvin418ca702008-05-30 10:42:30 -07003289
3290 /*
3291 * We now have a macro name, an implicit parameter count of
3292 * zero, and a string token to use as an expansion. Create
3293 * and store an SMacro.
3294 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003295 define_smacro(ctx, mname, casesense, 0, macro_start);
3296 free_tlist(tline);
H. Peter Anvin418ca702008-05-30 10:42:30 -07003297 free_tlist(origline);
3298 return DIRECTIVE_FOUND;
3299 }
3300
H. Peter Anvine2c80182005-01-15 22:15:51 +00003301 case PP_STRLEN:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003302 casesense = true;
H. Peter Anvin70653092007-10-19 14:42:29 -07003303
H. Peter Anvine2c80182005-01-15 22:15:51 +00003304 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 "`%%strlen' expects a macro identifier as first parameter");
3312 free_tlist(origline);
3313 return DIRECTIVE_FOUND;
3314 }
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08003315 ctx = get_ctx(tline->text, &mname, false);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003316 last = tline;
3317 tline = expand_smacro(tline->next);
3318 last->next = NULL;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003319
H. Peter Anvine2c80182005-01-15 22:15:51 +00003320 t = tline;
3321 while (tok_type_(t, TOK_WHITESPACE))
3322 t = t->next;
3323 /* t should now point to the string */
Cyrill Gorcunov4e1d5ab2010-07-23 18:51:51 +04003324 if (!tok_type_(t, TOK_STRING)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00003325 error(ERR_NONFATAL,
3326 "`%%strlen` requires string as second parameter");
3327 free_tlist(tline);
3328 free_tlist(origline);
3329 return DIRECTIVE_FOUND;
3330 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003331
H. Peter Anvine2c80182005-01-15 22:15:51 +00003332 macro_start = nasm_malloc(sizeof(*macro_start));
3333 macro_start->next = NULL;
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07003334 make_tok_num(macro_start, nasm_unquote(t->text, NULL));
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003335 macro_start->a.mac = NULL;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003336
H. Peter Anvine2c80182005-01-15 22:15:51 +00003337 /*
3338 * We now have a macro name, an implicit parameter count of
3339 * zero, and a numeric token to use as an expansion. Create
3340 * and store an SMacro.
3341 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003342 define_smacro(ctx, mname, casesense, 0, macro_start);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003343 free_tlist(tline);
3344 free_tlist(origline);
3345 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003346
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003347 case PP_STRCAT:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003348 casesense = true;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003349
3350 tline = tline->next;
3351 skip_white_(tline);
3352 tline = expand_id(tline);
3353 if (!tline || (tline->type != TOK_ID &&
3354 (tline->type != TOK_PREPROC_ID ||
3355 tline->text[1] != '$'))) {
3356 error(ERR_NONFATAL,
3357 "`%%strcat' expects a macro identifier as first parameter");
3358 free_tlist(origline);
3359 return DIRECTIVE_FOUND;
3360 }
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08003361 ctx = get_ctx(tline->text, &mname, false);
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003362 last = tline;
3363 tline = expand_smacro(tline->next);
3364 last->next = NULL;
3365
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003366 len = 0;
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04003367 list_for_each(t, tline) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003368 switch (t->type) {
3369 case TOK_WHITESPACE:
3370 break;
3371 case TOK_STRING:
3372 len += t->a.len = nasm_unquote(t->text, NULL);
3373 break;
3374 case TOK_OTHER:
3375 if (!strcmp(t->text, ",")) /* permit comma separators */
3376 break;
3377 /* else fall through */
3378 default:
3379 error(ERR_NONFATAL,
3380 "non-string passed to `%%strcat' (%d)", t->type);
3381 free_tlist(tline);
3382 free_tlist(origline);
3383 return DIRECTIVE_FOUND;
3384 }
3385 }
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003386
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003387 p = pp = nasm_malloc(len);
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04003388 list_for_each(t, tline) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003389 if (t->type == TOK_STRING) {
3390 memcpy(p, t->text, t->a.len);
3391 p += t->a.len;
3392 }
3393 }
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003394
3395 /*
3396 * We now have a macro name, an implicit parameter count of
3397 * zero, and a numeric token to use as an expansion. Create
3398 * and store an SMacro.
3399 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003400 macro_start = new_Token(NULL, TOK_STRING, NULL, 0);
3401 macro_start->text = nasm_quote(pp, len);
3402 nasm_free(pp);
3403 define_smacro(ctx, mname, casesense, 0, macro_start);
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003404 free_tlist(tline);
3405 free_tlist(origline);
3406 return DIRECTIVE_FOUND;
3407
H. Peter Anvine2c80182005-01-15 22:15:51 +00003408 case PP_SUBSTR:
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003409 {
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003410 int64_t start, count;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003411 size_t len;
H. Peter Anvind2456592008-06-19 15:04:18 -07003412
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003413 casesense = true;
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07003414
H. Peter Anvine2c80182005-01-15 22:15:51 +00003415 tline = tline->next;
3416 skip_white_(tline);
3417 tline = expand_id(tline);
3418 if (!tline || (tline->type != TOK_ID &&
3419 (tline->type != TOK_PREPROC_ID ||
3420 tline->text[1] != '$'))) {
3421 error(ERR_NONFATAL,
3422 "`%%substr' expects a macro identifier as first parameter");
3423 free_tlist(origline);
3424 return DIRECTIVE_FOUND;
3425 }
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08003426 ctx = get_ctx(tline->text, &mname, false);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003427 last = tline;
3428 tline = expand_smacro(tline->next);
3429 last->next = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003430
Cyrill Gorcunov35519d62010-09-06 23:49:52 +04003431 if (tline) /* skip expanded id */
3432 t = tline->next;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003433 while (tok_type_(t, TOK_WHITESPACE))
3434 t = t->next;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003435
H. Peter Anvine2c80182005-01-15 22:15:51 +00003436 /* t should now point to the string */
Cyrill Gorcunov35519d62010-09-06 23:49:52 +04003437 if (!tok_type_(t, TOK_STRING)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00003438 error(ERR_NONFATAL,
3439 "`%%substr` requires string as second parameter");
3440 free_tlist(tline);
3441 free_tlist(origline);
3442 return DIRECTIVE_FOUND;
3443 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003444
H. Peter Anvine2c80182005-01-15 22:15:51 +00003445 tt = t->next;
3446 tptr = &tt;
3447 tokval.t_type = TOKEN_INVALID;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003448 evalresult = evaluate(ppscan, tptr, &tokval, NULL,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003449 pass, error, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003450 if (!evalresult) {
3451 free_tlist(tline);
3452 free_tlist(origline);
3453 return DIRECTIVE_FOUND;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003454 } else if (!is_simple(evalresult)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00003455 error(ERR_NONFATAL, "non-constant value given to `%%substr`");
3456 free_tlist(tline);
3457 free_tlist(origline);
3458 return DIRECTIVE_FOUND;
3459 }
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003460 start = evalresult->value - 1;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003461
3462 while (tok_type_(tt, TOK_WHITESPACE))
3463 tt = tt->next;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003464 if (!tt) {
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003465 count = 1; /* Backwards compatibility: one character */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003466 } else {
3467 tokval.t_type = TOKEN_INVALID;
3468 evalresult = evaluate(ppscan, tptr, &tokval, NULL,
3469 pass, error, NULL);
3470 if (!evalresult) {
3471 free_tlist(tline);
3472 free_tlist(origline);
3473 return DIRECTIVE_FOUND;
3474 } else if (!is_simple(evalresult)) {
3475 error(ERR_NONFATAL, "non-constant value given to `%%substr`");
3476 free_tlist(tline);
3477 free_tlist(origline);
3478 return DIRECTIVE_FOUND;
3479 }
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003480 count = evalresult->value;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003481 }
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003482
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003483 len = nasm_unquote(t->text, NULL);
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003484
Cyrill Gorcunovcff031e2010-09-07 20:31:11 +04003485 /* make start and count being in range */
3486 if (start < 0)
3487 start = 0;
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003488 if (count < 0)
3489 count = len + count + 1 - start;
3490 if (start + count > (int64_t)len)
Cyrill Gorcunovcff031e2010-09-07 20:31:11 +04003491 count = len - start;
3492 if (!len || count < 0 || start >=(int64_t)len)
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003493 start = -1, count = 0; /* empty string */
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003494
H. Peter Anvine2c80182005-01-15 22:15:51 +00003495 macro_start = nasm_malloc(sizeof(*macro_start));
3496 macro_start->next = NULL;
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003497 macro_start->text = nasm_quote((start < 0) ? "" : t->text + start, count);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003498 macro_start->type = TOK_STRING;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003499 macro_start->a.mac = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003500
H. Peter Anvine2c80182005-01-15 22:15:51 +00003501 /*
3502 * We now have a macro name, an implicit parameter count of
3503 * zero, and a numeric token to use as an expansion. Create
3504 * and store an SMacro.
3505 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003506 define_smacro(ctx, mname, casesense, 0, macro_start);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003507 free_tlist(tline);
3508 free_tlist(origline);
3509 return DIRECTIVE_FOUND;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003510 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003511
H. Peter Anvine2c80182005-01-15 22:15:51 +00003512 case PP_ASSIGN:
3513 case PP_IASSIGN:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003514 casesense = (i == PP_ASSIGN);
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07003515
H. Peter Anvine2c80182005-01-15 22:15:51 +00003516 tline = tline->next;
3517 skip_white_(tline);
3518 tline = expand_id(tline);
3519 if (!tline || (tline->type != TOK_ID &&
3520 (tline->type != TOK_PREPROC_ID ||
3521 tline->text[1] != '$'))) {
3522 error(ERR_NONFATAL,
3523 "`%%%sassign' expects a macro identifier",
3524 (i == PP_IASSIGN ? "i" : ""));
3525 free_tlist(origline);
3526 return DIRECTIVE_FOUND;
3527 }
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08003528 ctx = get_ctx(tline->text, &mname, false);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003529 last = tline;
3530 tline = expand_smacro(tline->next);
3531 last->next = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003532
H. Peter Anvine2c80182005-01-15 22:15:51 +00003533 t = tline;
3534 tptr = &t;
3535 tokval.t_type = TOKEN_INVALID;
3536 evalresult =
3537 evaluate(ppscan, tptr, &tokval, NULL, pass, error, NULL);
3538 free_tlist(tline);
3539 if (!evalresult) {
3540 free_tlist(origline);
3541 return DIRECTIVE_FOUND;
3542 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003543
H. Peter Anvine2c80182005-01-15 22:15:51 +00003544 if (tokval.t_type)
H. Peter Anvin917a3492008-09-24 09:14:49 -07003545 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +00003546 "trailing garbage after expression ignored");
H. Peter Anvin734b1882002-04-30 21:01:08 +00003547
H. Peter Anvine2c80182005-01-15 22:15:51 +00003548 if (!is_simple(evalresult)) {
3549 error(ERR_NONFATAL,
3550 "non-constant value given to `%%%sassign'",
3551 (i == PP_IASSIGN ? "i" : ""));
3552 free_tlist(origline);
3553 return DIRECTIVE_FOUND;
3554 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003555
H. Peter Anvine2c80182005-01-15 22:15:51 +00003556 macro_start = nasm_malloc(sizeof(*macro_start));
3557 macro_start->next = NULL;
3558 make_tok_num(macro_start, reloc_value(evalresult));
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003559 macro_start->a.mac = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003560
H. Peter Anvine2c80182005-01-15 22:15:51 +00003561 /*
3562 * We now have a macro name, an implicit parameter count of
3563 * zero, and a numeric token to use as an expansion. Create
3564 * and store an SMacro.
3565 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003566 define_smacro(ctx, mname, casesense, 0, macro_start);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003567 free_tlist(origline);
3568 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003569
H. Peter Anvine2c80182005-01-15 22:15:51 +00003570 case PP_LINE:
3571 /*
3572 * Syntax is `%line nnn[+mmm] [filename]'
3573 */
3574 tline = tline->next;
3575 skip_white_(tline);
3576 if (!tok_type_(tline, TOK_NUMBER)) {
3577 error(ERR_NONFATAL, "`%%line' expects line number");
3578 free_tlist(origline);
3579 return DIRECTIVE_FOUND;
3580 }
H. Peter Anvin70055962007-10-11 00:05:31 -07003581 k = readnum(tline->text, &err);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003582 m = 1;
3583 tline = tline->next;
3584 if (tok_is_(tline, "+")) {
3585 tline = tline->next;
3586 if (!tok_type_(tline, TOK_NUMBER)) {
3587 error(ERR_NONFATAL, "`%%line' expects line increment");
3588 free_tlist(origline);
3589 return DIRECTIVE_FOUND;
3590 }
H. Peter Anvin70055962007-10-11 00:05:31 -07003591 m = readnum(tline->text, &err);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003592 tline = tline->next;
3593 }
3594 skip_white_(tline);
3595 src_set_linnum(k);
3596 istk->lineinc = m;
3597 if (tline) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07003598 nasm_free(src_set_fname(detoken(tline, false)));
H. Peter Anvine2c80182005-01-15 22:15:51 +00003599 }
3600 free_tlist(origline);
3601 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003602
H. Peter Anvine2c80182005-01-15 22:15:51 +00003603 default:
3604 error(ERR_FATAL,
3605 "preprocessor directive `%s' not yet implemented",
H. Peter Anvin4169a472007-09-12 01:29:43 +00003606 pp_directives[i]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003607 return DIRECTIVE_FOUND;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003608 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003609}
3610
3611/*
H. Peter Anvin76690a12002-04-30 20:52:49 +00003612 * Ensure that a macro parameter contains a condition code and
3613 * nothing else. Return the condition code index if so, or -1
3614 * otherwise.
3615 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003616static int find_cc(Token * t)
H. Peter Anvineba20a72002-04-30 20:53:55 +00003617{
H. Peter Anvin76690a12002-04-30 20:52:49 +00003618 Token *tt;
3619 int i, j, k, m;
3620
H. Peter Anvin25a99342007-09-22 17:45:45 -07003621 if (!t)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003622 return -1; /* Probably a %+ without a space */
H. Peter Anvin25a99342007-09-22 17:45:45 -07003623
H. Peter Anvineba20a72002-04-30 20:53:55 +00003624 skip_white_(t);
H. Peter Anvin76690a12002-04-30 20:52:49 +00003625 if (t->type != TOK_ID)
H. Peter Anvine2c80182005-01-15 22:15:51 +00003626 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003627 tt = t->next;
H. Peter Anvineba20a72002-04-30 20:53:55 +00003628 skip_white_(tt);
H. Peter Anvin76690a12002-04-30 20:52:49 +00003629 if (tt && (tt->type != TOK_OTHER || strcmp(tt->text, ",")))
H. Peter Anvine2c80182005-01-15 22:15:51 +00003630 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003631
3632 i = -1;
Cyrill Gorcunova7319242010-06-03 22:04:36 +04003633 j = ARRAY_SIZE(conditions);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003634 while (j - i > 1) {
3635 k = (j + i) / 2;
3636 m = nasm_stricmp(t->text, conditions[k]);
3637 if (m == 0) {
3638 i = k;
3639 j = -2;
3640 break;
3641 } else if (m < 0) {
3642 j = k;
3643 } else
3644 i = k;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003645 }
3646 if (j != -2)
H. Peter Anvine2c80182005-01-15 22:15:51 +00003647 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003648 return i;
3649}
3650
Cyrill Gorcunov575d4282010-10-06 00:25:55 +04003651static bool paste_tokens(Token **head, const struct tokseq_match *m,
3652 int mnum, bool handle_paste_tokens)
H. Peter Anvind784a082009-04-20 14:01:18 -07003653{
3654 Token **tail, *t, *tt;
3655 Token **paste_head;
3656 bool did_paste = false;
3657 char *tmp;
Cyrill Gorcunov575d4282010-10-06 00:25:55 +04003658 int i;
H. Peter Anvind784a082009-04-20 14:01:18 -07003659
3660 /* Now handle token pasting... */
3661 paste_head = NULL;
3662 tail = head;
3663 while ((t = *tail) && (tt = t->next)) {
3664 switch (t->type) {
3665 case TOK_WHITESPACE:
3666 if (tt->type == TOK_WHITESPACE) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003667 /* Zap adjacent whitespace tokens */
H. Peter Anvind784a082009-04-20 14:01:18 -07003668 t->next = delete_Token(tt);
3669 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003670 /* Do not advance paste_head here */
3671 tail = &t->next;
3672 }
H. Peter Anvind784a082009-04-20 14:01:18 -07003673 break;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003674 case TOK_PASTE: /* %+ */
3675 if (handle_paste_tokens) {
3676 /* Zap %+ and whitespace tokens to the right */
3677 while (t && (t->type == TOK_WHITESPACE ||
3678 t->type == TOK_PASTE))
3679 t = *tail = delete_Token(t);
3680 if (!paste_head || !t)
3681 break; /* Nothing to paste with */
3682 tail = paste_head;
3683 t = *tail;
3684 tt = t->next;
3685 while (tok_type_(tt, TOK_WHITESPACE))
3686 tt = t->next = delete_Token(tt);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003687 if (tt) {
3688 tmp = nasm_strcat(t->text, tt->text);
3689 delete_Token(t);
3690 tt = delete_Token(tt);
3691 t = *tail = tokenize(tmp);
3692 nasm_free(tmp);
3693 while (t->next) {
3694 tail = &t->next;
3695 t = t->next;
3696 }
3697 t->next = tt; /* Attach the remaining token chain */
3698 did_paste = true;
3699 }
3700 paste_head = tail;
3701 tail = &t->next;
3702 break;
3703 }
3704 /* else fall through */
3705 default:
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +04003706 /*
3707 * Concatenation of tokens might look nontrivial
3708 * but in real it's pretty simple -- the caller
Cyrill Gorcunov575d4282010-10-06 00:25:55 +04003709 * prepares the masks of token types to be concatenated
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +04003710 * and we simply find matched sequences and slip
3711 * them together
3712 */
Cyrill Gorcunov575d4282010-10-06 00:25:55 +04003713 for (i = 0; i < mnum; i++) {
3714 if (PP_CONCAT_MASK(t->type) & m[i].mask_head) {
3715 size_t len = 0;
3716 char *tmp, *p;
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +04003717
Cyrill Gorcunov575d4282010-10-06 00:25:55 +04003718 while (tt && (PP_CONCAT_MASK(tt->type) & m[i].mask_tail)) {
3719 len += strlen(tt->text);
3720 tt = tt->next;
3721 }
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +04003722
Cyrill Gorcunov575d4282010-10-06 00:25:55 +04003723 /*
3724 * Now tt points to the first token after
3725 * the potential paste area...
3726 */
3727 if (tt != t->next) {
3728 /* We have at least two tokens... */
3729 len += strlen(t->text);
3730 p = tmp = nasm_malloc(len+1);
3731 while (t != tt) {
3732 strcpy(p, t->text);
3733 p = strchr(p, '\0');
3734 t = delete_Token(t);
3735 }
3736 t = *tail = tokenize(tmp);
3737 nasm_free(tmp);
3738 while (t->next) {
3739 tail = &t->next;
3740 t = t->next;
3741 }
3742 t->next = tt; /* Attach the remaining token chain */
3743 did_paste = true;
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +04003744 }
Cyrill Gorcunov575d4282010-10-06 00:25:55 +04003745 paste_head = tail;
3746 tail = &t->next;
3747 break;
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +04003748 }
Cyrill Gorcunov575d4282010-10-06 00:25:55 +04003749 }
3750 if (i >= mnum) { /* no match */
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +04003751 tail = &t->next;
3752 if (!tok_type_(t->next, TOK_WHITESPACE))
3753 paste_head = tail;
3754 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003755 break;
H. Peter Anvind784a082009-04-20 14:01:18 -07003756 }
3757 }
3758 return did_paste;
3759}
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003760
3761/*
3762 * expands to a list of tokens from %{x:y}
3763 */
3764static Token *expand_mmac_params_range(MMacro *mac, Token *tline, Token ***last)
3765{
3766 Token *t = tline, **tt, *tm, *head;
3767 char *pos;
3768 int fst, lst, j, i;
3769
3770 pos = strchr(tline->text, ':');
3771 nasm_assert(pos);
3772
3773 lst = atoi(pos + 1);
3774 fst = atoi(tline->text + 1);
3775
3776 /*
3777 * only macros params are accounted so
3778 * if someone passes %0 -- we reject such
3779 * value(s)
3780 */
3781 if (lst == 0 || fst == 0)
3782 goto err;
3783
3784 /* the values should be sane */
Cyrill Gorcunov2f403752010-06-05 10:47:10 +04003785 if ((fst > (int)mac->nparam || fst < (-(int)mac->nparam)) ||
3786 (lst > (int)mac->nparam || lst < (-(int)mac->nparam)))
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003787 goto err;
3788
Cyrill Gorcunovefb35832010-06-20 01:52:19 +04003789 fst = fst < 0 ? fst + (int)mac->nparam + 1: fst;
3790 lst = lst < 0 ? lst + (int)mac->nparam + 1: lst;
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003791
3792 /* counted from zero */
3793 fst--, lst--;
3794
3795 /*
3796 * it will be at least one token
3797 */
3798 tm = mac->params[(fst + mac->rotate) % mac->nparam];
3799 t = new_Token(NULL, tm->type, tm->text, 0);
3800 head = t, tt = &t->next;
3801 if (fst < lst) {
3802 for (i = fst + 1; i <= lst; i++) {
3803 t = new_Token(NULL, TOK_OTHER, ",", 0);
3804 *tt = t, tt = &t->next;
3805 j = (i + mac->rotate) % mac->nparam;
3806 tm = mac->params[j];
3807 t = new_Token(NULL, tm->type, tm->text, 0);
3808 *tt = t, tt = &t->next;
3809 }
3810 } else {
3811 for (i = fst - 1; i >= lst; i--) {
3812 t = new_Token(NULL, TOK_OTHER, ",", 0);
3813 *tt = t, tt = &t->next;
3814 j = (i + mac->rotate) % mac->nparam;
3815 tm = mac->params[j];
3816 t = new_Token(NULL, tm->type, tm->text, 0);
3817 *tt = t, tt = &t->next;
3818 }
3819 }
3820
3821 *last = tt;
3822 return head;
3823
3824err:
3825 error(ERR_NONFATAL, "`%%{%s}': macro parameters out of range",
3826 &tline->text[1]);
3827 return tline;
3828}
3829
H. Peter Anvin76690a12002-04-30 20:52:49 +00003830/*
3831 * Expand MMacro-local things: parameter references (%0, %n, %+n,
H. Peter Anvin67c63722008-10-26 23:49:00 -07003832 * %-n) and MMacro-local identifiers (%%foo) as well as
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003833 * macro indirection (%[...]) and range (%{..:..}).
H. Peter Anvin76690a12002-04-30 20:52:49 +00003834 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003835static Token *expand_mmac_params(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00003836{
H. Peter Anvin734b1882002-04-30 21:01:08 +00003837 Token *t, *tt, **tail, *thead;
H. Peter Anvin6125b622009-04-08 14:02:25 -07003838 bool changed = false;
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003839 char *pos;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003840
3841 tail = &thead;
3842 thead = NULL;
3843
H. Peter Anvine2c80182005-01-15 22:15:51 +00003844 while (tline) {
3845 if (tline->type == TOK_PREPROC_ID &&
Cyrill Gorcunovca611192010-06-04 09:22:12 +04003846 (((tline->text[1] == '+' || tline->text[1] == '-') && tline->text[2]) ||
3847 (tline->text[1] >= '0' && tline->text[1] <= '9') ||
3848 tline->text[1] == '%')) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00003849 char *text = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003850 int type = 0, cc; /* type = 0 to placate optimisers */
Keith Kaniosa6dfa782007-04-13 16:47:53 +00003851 char tmpbuf[30];
H. Peter Anvin25a99342007-09-22 17:45:45 -07003852 unsigned int n;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003853 int i;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003854 MMacro *mac;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003855
H. Peter Anvine2c80182005-01-15 22:15:51 +00003856 t = tline;
3857 tline = tline->next;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003858
H. Peter Anvine2c80182005-01-15 22:15:51 +00003859 mac = istk->mstk;
3860 while (mac && !mac->name) /* avoid mistaking %reps for macros */
3861 mac = mac->next_active;
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003862 if (!mac) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00003863 error(ERR_NONFATAL, "`%s': not in a macro call", t->text);
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003864 } else {
3865 pos = strchr(t->text, ':');
3866 if (!pos) {
3867 switch (t->text[1]) {
3868 /*
3869 * We have to make a substitution of one of the
3870 * forms %1, %-1, %+1, %%foo, %0.
3871 */
3872 case '0':
3873 type = TOK_NUMBER;
3874 snprintf(tmpbuf, sizeof(tmpbuf), "%d", mac->nparam);
3875 text = nasm_strdup(tmpbuf);
3876 break;
3877 case '%':
H. Peter Anvine2c80182005-01-15 22:15:51 +00003878 type = TOK_ID;
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003879 snprintf(tmpbuf, sizeof(tmpbuf), "..@%"PRIu64".",
3880 mac->unique);
3881 text = nasm_strcat(tmpbuf, t->text + 2);
3882 break;
3883 case '-':
3884 n = atoi(t->text + 2) - 1;
3885 if (n >= mac->nparam)
3886 tt = NULL;
3887 else {
3888 if (mac->nparam > 1)
3889 n = (n + mac->rotate) % mac->nparam;
3890 tt = mac->params[n];
H. Peter Anvine2c80182005-01-15 22:15:51 +00003891 }
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003892 cc = find_cc(tt);
3893 if (cc == -1) {
3894 error(ERR_NONFATAL,
3895 "macro parameter %d is not a condition code",
3896 n + 1);
3897 text = NULL;
3898 } else {
3899 type = TOK_ID;
3900 if (inverse_ccs[cc] == -1) {
3901 error(ERR_NONFATAL,
3902 "condition code `%s' is not invertible",
3903 conditions[cc]);
3904 text = NULL;
3905 } else
3906 text = nasm_strdup(conditions[inverse_ccs[cc]]);
3907 }
3908 break;
3909 case '+':
3910 n = atoi(t->text + 2) - 1;
3911 if (n >= mac->nparam)
3912 tt = NULL;
3913 else {
3914 if (mac->nparam > 1)
3915 n = (n + mac->rotate) % mac->nparam;
3916 tt = mac->params[n];
3917 }
3918 cc = find_cc(tt);
3919 if (cc == -1) {
3920 error(ERR_NONFATAL,
3921 "macro parameter %d is not a condition code",
3922 n + 1);
3923 text = NULL;
3924 } else {
3925 type = TOK_ID;
3926 text = nasm_strdup(conditions[cc]);
3927 }
3928 break;
3929 default:
3930 n = atoi(t->text + 1) - 1;
3931 if (n >= mac->nparam)
3932 tt = NULL;
3933 else {
3934 if (mac->nparam > 1)
3935 n = (n + mac->rotate) % mac->nparam;
3936 tt = mac->params[n];
3937 }
3938 if (tt) {
3939 for (i = 0; i < mac->paramlen[n]; i++) {
3940 *tail = new_Token(NULL, tt->type, tt->text, 0);
3941 tail = &(*tail)->next;
3942 tt = tt->next;
3943 }
3944 }
3945 text = NULL; /* we've done it here */
3946 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003947 }
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003948 } else {
3949 /*
3950 * seems we have a parameters range here
3951 */
3952 Token *head, **last;
3953 head = expand_mmac_params_range(mac, t, &last);
3954 if (head != t) {
3955 *tail = head;
3956 *last = tline;
3957 tline = head;
3958 text = NULL;
3959 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00003960 }
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003961 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00003962 if (!text) {
3963 delete_Token(t);
3964 } else {
3965 *tail = t;
3966 tail = &t->next;
3967 t->type = type;
3968 nasm_free(t->text);
3969 t->text = text;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003970 t->a.mac = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003971 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003972 changed = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003973 continue;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003974 } else if (tline->type == TOK_INDIRECT) {
3975 t = tline;
3976 tline = tline->next;
3977 tt = tokenize(t->text);
3978 tt = expand_mmac_params(tt);
3979 tt = expand_smacro(tt);
3980 *tail = tt;
3981 while (tt) {
3982 tt->a.mac = NULL; /* Necessary? */
3983 tail = &tt->next;
3984 tt = tt->next;
3985 }
3986 delete_Token(t);
3987 changed = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003988 } else {
3989 t = *tail = tline;
3990 tline = tline->next;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003991 t->a.mac = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003992 tail = &t->next;
3993 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00003994 }
H. Peter Anvineba20a72002-04-30 20:53:55 +00003995 *tail = NULL;
H. Peter Anvin67c63722008-10-26 23:49:00 -07003996
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +04003997 if (changed) {
Cyrill Gorcunov575d4282010-10-06 00:25:55 +04003998 const struct tokseq_match t[] = {
3999 {
4000 PP_CONCAT_MASK(TOK_ID) |
4001 PP_CONCAT_MASK(TOK_FLOAT), /* head */
4002 PP_CONCAT_MASK(TOK_ID) |
4003 PP_CONCAT_MASK(TOK_NUMBER) |
4004 PP_CONCAT_MASK(TOK_FLOAT) |
4005 PP_CONCAT_MASK(TOK_OTHER) /* tail */
4006 },
4007 {
4008 PP_CONCAT_MASK(TOK_NUMBER), /* head */
4009 PP_CONCAT_MASK(TOK_NUMBER) /* tail */
4010 }
4011 };
4012 paste_tokens(&thead, t, ARRAY_SIZE(t), false);
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +04004013 }
H. Peter Anvin6125b622009-04-08 14:02:25 -07004014
H. Peter Anvin76690a12002-04-30 20:52:49 +00004015 return thead;
4016}
4017
4018/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004019 * Expand all single-line macro calls made in the given line.
4020 * Return the expanded version of the line. The original is deemed
4021 * to be destroyed in the process. (In reality we'll just move
4022 * Tokens from input to output a lot of the time, rather than
4023 * actually bothering to destroy and replicate.)
4024 */
H. Peter Anvincb1cf592007-11-19 12:26:50 -08004025
H. Peter Anvine2c80182005-01-15 22:15:51 +00004026static Token *expand_smacro(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004027{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004028 Token *t, *tt, *mstart, **tail, *thead;
H. Peter Anvineba20a72002-04-30 20:53:55 +00004029 SMacro *head = NULL, *m;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004030 Token **params;
4031 int *paramsize;
H. Peter Anvin25a99342007-09-22 17:45:45 -07004032 unsigned int nparam, sparam;
H. Peter Anvind784a082009-04-20 14:01:18 -07004033 int brackets;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004034 Token *org_tline = tline;
4035 Context *ctx;
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08004036 const char *mname;
H. Peter Anvin2a15e692007-11-19 13:14:59 -08004037 int deadman = DEADMAN_LIMIT;
H. Peter Anvin8287daf2009-07-07 16:00:58 -07004038 bool expanded;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004039
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004040 /*
4041 * Trick: we should avoid changing the start token pointer since it can
4042 * be contained in "next" field of other token. Because of this
4043 * we allocate a copy of first token and work with it; at the end of
4044 * routine we copy it back
4045 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004046 if (org_tline) {
Cyrill Gorcunoved4a8052010-04-09 15:40:35 +04004047 tline = new_Token(org_tline->next, org_tline->type,
4048 org_tline->text, 0);
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004049 tline->a.mac = org_tline->a.mac;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004050 nasm_free(org_tline->text);
4051 org_tline->text = NULL;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004052 }
4053
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004054 expanded = true; /* Always expand %+ at least once */
H. Peter Anvin8287daf2009-07-07 16:00:58 -07004055
H. Peter Anvincb1cf592007-11-19 12:26:50 -08004056again:
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004057 thead = NULL;
Cyrill Gorcunoved4a8052010-04-09 15:40:35 +04004058 tail = &thead;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004059
H. Peter Anvine2c80182005-01-15 22:15:51 +00004060 while (tline) { /* main token loop */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004061 if (!--deadman) {
4062 error(ERR_NONFATAL, "interminable macro recursion");
Cyrill Gorcunovbd38c8f2009-11-21 11:11:23 +03004063 goto err;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004064 }
H. Peter Anvincb1cf592007-11-19 12:26:50 -08004065
H. Peter Anvine2c80182005-01-15 22:15:51 +00004066 if ((mname = tline->text)) {
4067 /* if this token is a local macro, look in local context */
Cyrill Gorcunovc56d9ad2010-02-11 15:12:19 +03004068 if (tline->type == TOK_ID) {
4069 head = (SMacro *)hash_findix(&smacros, mname);
4070 } else if (tline->type == TOK_PREPROC_ID) {
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08004071 ctx = get_ctx(mname, &mname, true);
Cyrill Gorcunovc56d9ad2010-02-11 15:12:19 +03004072 head = ctx ? (SMacro *)hash_findix(&ctx->localmac, mname) : NULL;
4073 } else
4074 head = NULL;
H. Peter Anvin072771e2008-05-22 13:17:51 -07004075
H. Peter Anvine2c80182005-01-15 22:15:51 +00004076 /*
4077 * We've hit an identifier. As in is_mmacro below, we first
4078 * check whether the identifier is a single-line macro at
4079 * all, then think about checking for parameters if
4080 * necessary.
4081 */
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04004082 list_for_each(m, head)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004083 if (!mstrcmp(m->name, mname, m->casesense))
4084 break;
4085 if (m) {
4086 mstart = tline;
4087 params = NULL;
4088 paramsize = NULL;
4089 if (m->nparam == 0) {
4090 /*
4091 * Simple case: the macro is parameterless. Discard the
4092 * one token that the macro call took, and push the
4093 * expansion back on the to-do stack.
4094 */
4095 if (!m->expansion) {
4096 if (!strcmp("__FILE__", m->name)) {
4097 int32_t num = 0;
4098 char *file = NULL;
4099 src_get(&num, &file);
4100 tline->text = nasm_quote(file, strlen(file));
4101 tline->type = TOK_STRING;
4102 nasm_free(file);
H. Peter Anvine2c80182005-01-15 22:15:51 +00004103 continue;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004104 }
4105 if (!strcmp("__LINE__", m->name)) {
4106 nasm_free(tline->text);
4107 make_tok_num(tline, src_get_linnum());
4108 continue;
4109 }
4110 if (!strcmp("__BITS__", m->name)) {
4111 nasm_free(tline->text);
4112 make_tok_num(tline, globalbits);
4113 continue;
4114 }
4115 tline = delete_Token(tline);
4116 continue;
4117 }
4118 } else {
4119 /*
4120 * Complicated case: at least one macro with this name
H. Peter Anvine2c80182005-01-15 22:15:51 +00004121 * exists and takes parameters. We must find the
4122 * parameters in the call, count them, find the SMacro
4123 * that corresponds to that form of the macro call, and
4124 * substitute for the parameters when we expand. What a
4125 * pain.
4126 */
4127 /*tline = tline->next;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004128 skip_white_(tline); */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004129 do {
4130 t = tline->next;
4131 while (tok_type_(t, TOK_SMAC_END)) {
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004132 t->a.mac->in_progress = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004133 t->text = NULL;
4134 t = tline->next = delete_Token(t);
4135 }
4136 tline = t;
4137 } while (tok_type_(tline, TOK_WHITESPACE));
4138 if (!tok_is_(tline, "(")) {
4139 /*
4140 * This macro wasn't called with parameters: ignore
4141 * the call. (Behaviour borrowed from gnu cpp.)
4142 */
4143 tline = mstart;
4144 m = NULL;
4145 } else {
4146 int paren = 0;
4147 int white = 0;
4148 brackets = 0;
4149 nparam = 0;
4150 sparam = PARAM_DELTA;
4151 params = nasm_malloc(sparam * sizeof(Token *));
4152 params[0] = tline->next;
4153 paramsize = nasm_malloc(sparam * sizeof(int));
4154 paramsize[0] = 0;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07004155 while (true) { /* parameter loop */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004156 /*
4157 * For some unusual expansions
4158 * which concatenates function call
4159 */
4160 t = tline->next;
4161 while (tok_type_(t, TOK_SMAC_END)) {
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004162 t->a.mac->in_progress = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004163 t->text = NULL;
4164 t = tline->next = delete_Token(t);
4165 }
4166 tline = t;
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00004167
H. Peter Anvine2c80182005-01-15 22:15:51 +00004168 if (!tline) {
4169 error(ERR_NONFATAL,
4170 "macro call expects terminating `)'");
4171 break;
4172 }
4173 if (tline->type == TOK_WHITESPACE
4174 && brackets <= 0) {
4175 if (paramsize[nparam])
4176 white++;
4177 else
4178 params[nparam] = tline->next;
4179 continue; /* parameter loop */
4180 }
4181 if (tline->type == TOK_OTHER
4182 && tline->text[1] == 0) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004183 char ch = tline->text[0];
H. Peter Anvine2c80182005-01-15 22:15:51 +00004184 if (ch == ',' && !paren && brackets <= 0) {
4185 if (++nparam >= sparam) {
4186 sparam += PARAM_DELTA;
4187 params = nasm_realloc(params,
Cyrill Gorcunoved4a8052010-04-09 15:40:35 +04004188 sparam * sizeof(Token *));
4189 paramsize = nasm_realloc(paramsize,
4190 sparam * sizeof(int));
H. Peter Anvine2c80182005-01-15 22:15:51 +00004191 }
4192 params[nparam] = tline->next;
4193 paramsize[nparam] = 0;
4194 white = 0;
4195 continue; /* parameter loop */
4196 }
4197 if (ch == '{' &&
4198 (brackets > 0 || (brackets == 0 &&
4199 !paramsize[nparam])))
4200 {
4201 if (!(brackets++)) {
4202 params[nparam] = tline->next;
4203 continue; /* parameter loop */
4204 }
4205 }
4206 if (ch == '}' && brackets > 0)
4207 if (--brackets == 0) {
4208 brackets = -1;
4209 continue; /* parameter loop */
4210 }
4211 if (ch == '(' && !brackets)
4212 paren++;
4213 if (ch == ')' && brackets <= 0)
4214 if (--paren < 0)
4215 break;
4216 }
4217 if (brackets < 0) {
4218 brackets = 0;
4219 error(ERR_NONFATAL, "braces do not "
4220 "enclose all of macro parameter");
4221 }
4222 paramsize[nparam] += white + 1;
4223 white = 0;
4224 } /* parameter loop */
4225 nparam++;
4226 while (m && (m->nparam != nparam ||
4227 mstrcmp(m->name, mname,
4228 m->casesense)))
4229 m = m->next;
4230 if (!m)
H. Peter Anvin917a3492008-09-24 09:14:49 -07004231 error(ERR_WARNING|ERR_PASS1|ERR_WARN_MNP,
H. Peter Anvine2c80182005-01-15 22:15:51 +00004232 "macro `%s' exists, "
4233 "but not taking %d parameters",
4234 mstart->text, nparam);
4235 }
4236 }
4237 if (m && m->in_progress)
4238 m = NULL;
4239 if (!m) { /* in progess or didn't find '(' or wrong nparam */
H. Peter Anvin70653092007-10-19 14:42:29 -07004240 /*
H. Peter Anvine2c80182005-01-15 22:15:51 +00004241 * Design question: should we handle !tline, which
4242 * indicates missing ')' here, or expand those
4243 * macros anyway, which requires the (t) test a few
H. Peter Anvin70653092007-10-19 14:42:29 -07004244 * lines down?
H. Peter Anvine2c80182005-01-15 22:15:51 +00004245 */
4246 nasm_free(params);
4247 nasm_free(paramsize);
4248 tline = mstart;
4249 } else {
4250 /*
4251 * Expand the macro: we are placed on the last token of the
4252 * call, so that we can easily split the call from the
4253 * following tokens. We also start by pushing an SMAC_END
4254 * token for the cycle removal.
4255 */
4256 t = tline;
4257 if (t) {
4258 tline = t->next;
4259 t->next = NULL;
4260 }
4261 tt = new_Token(tline, TOK_SMAC_END, NULL, 0);
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004262 tt->a.mac = m;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07004263 m->in_progress = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004264 tline = tt;
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04004265 list_for_each(t, m->expansion) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00004266 if (t->type >= TOK_SMAC_PARAM) {
4267 Token *pcopy = tline, **ptail = &pcopy;
4268 Token *ttt, *pt;
4269 int i;
H. Peter Anvin734b1882002-04-30 21:01:08 +00004270
H. Peter Anvine2c80182005-01-15 22:15:51 +00004271 ttt = params[t->type - TOK_SMAC_PARAM];
Cyrill Gorcunoved4a8052010-04-09 15:40:35 +04004272 i = paramsize[t->type - TOK_SMAC_PARAM];
4273 while (--i >= 0) {
4274 pt = *ptail = new_Token(tline, ttt->type,
4275 ttt->text, 0);
H. Peter Anvine2c80182005-01-15 22:15:51 +00004276 ptail = &pt->next;
4277 ttt = ttt->next;
4278 }
4279 tline = pcopy;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004280 } else if (t->type == TOK_PREPROC_Q) {
4281 tt = new_Token(tline, TOK_ID, mname, 0);
4282 tline = tt;
4283 } else if (t->type == TOK_PREPROC_QQ) {
4284 tt = new_Token(tline, TOK_ID, m->name, 0);
4285 tline = tt;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004286 } else {
4287 tt = new_Token(tline, t->type, t->text, 0);
4288 tline = tt;
4289 }
4290 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00004291
H. Peter Anvine2c80182005-01-15 22:15:51 +00004292 /*
4293 * Having done that, get rid of the macro call, and clean
4294 * up the parameters.
4295 */
4296 nasm_free(params);
4297 nasm_free(paramsize);
4298 free_tlist(mstart);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004299 expanded = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004300 continue; /* main token loop */
4301 }
4302 }
4303 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004304
H. Peter Anvine2c80182005-01-15 22:15:51 +00004305 if (tline->type == TOK_SMAC_END) {
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004306 tline->a.mac->in_progress = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004307 tline = delete_Token(tline);
4308 } else {
4309 t = *tail = tline;
4310 tline = tline->next;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004311 t->a.mac = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004312 t->next = NULL;
4313 tail = &t->next;
4314 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004315 }
4316
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004317 /*
4318 * Now scan the entire line and look for successive TOK_IDs that resulted
Keith Kaniosb7a89542007-04-12 02:40:54 +00004319 * after expansion (they can't be produced by tokenize()). The successive
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004320 * TOK_IDs should be concatenated.
4321 * Also we look for %+ tokens and concatenate the tokens before and after
4322 * them (without white spaces in between).
4323 */
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +04004324 if (expanded) {
Cyrill Gorcunov575d4282010-10-06 00:25:55 +04004325 const struct tokseq_match t[] = {
4326 {
4327 PP_CONCAT_MASK(TOK_ID) |
4328 PP_CONCAT_MASK(TOK_PREPROC_ID), /* head */
4329 PP_CONCAT_MASK(TOK_ID) |
4330 PP_CONCAT_MASK(TOK_PREPROC_ID) |
4331 PP_CONCAT_MASK(TOK_NUMBER) /* tail */
4332 }
4333 };
4334 if (paste_tokens(&thead, t, ARRAY_SIZE(t), true)) {
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +04004335 /*
4336 * If we concatenated something, *and* we had previously expanded
4337 * an actual macro, scan the lines again for macros...
4338 */
4339 tline = thead;
4340 expanded = false;
4341 goto again;
4342 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00004343 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004344
Cyrill Gorcunovbd38c8f2009-11-21 11:11:23 +03004345err:
H. Peter Anvine2c80182005-01-15 22:15:51 +00004346 if (org_tline) {
4347 if (thead) {
4348 *org_tline = *thead;
4349 /* since we just gave text to org_line, don't free it */
4350 thead->text = NULL;
4351 delete_Token(thead);
4352 } else {
4353 /* the expression expanded to empty line;
4354 we can't return NULL for some reasons
4355 we just set the line to a single WHITESPACE token. */
4356 memset(org_tline, 0, sizeof(*org_tline));
4357 org_tline->text = NULL;
4358 org_tline->type = TOK_WHITESPACE;
4359 }
4360 thead = org_tline;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004361 }
4362
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004363 return thead;
4364}
4365
4366/*
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004367 * Similar to expand_smacro but used exclusively with macro identifiers
4368 * right before they are fetched in. The reason is that there can be
4369 * identifiers consisting of several subparts. We consider that if there
4370 * are more than one element forming the name, user wants a expansion,
4371 * otherwise it will be left as-is. Example:
4372 *
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004373 * %define %$abc cde
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004374 *
4375 * the identifier %$abc will be left as-is so that the handler for %define
4376 * will suck it and define the corresponding value. Other case:
4377 *
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004378 * %define _%$abc cde
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004379 *
4380 * In this case user wants name to be expanded *before* %define starts
4381 * working, so we'll expand %$abc into something (if it has a value;
4382 * otherwise it will be left as-is) then concatenate all successive
4383 * PP_IDs into one.
4384 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004385static Token *expand_id(Token * tline)
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004386{
4387 Token *cur, *oldnext = NULL;
4388
H. Peter Anvin734b1882002-04-30 21:01:08 +00004389 if (!tline || !tline->next)
H. Peter Anvine2c80182005-01-15 22:15:51 +00004390 return tline;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004391
4392 cur = tline;
4393 while (cur->next &&
H. Peter Anvine2c80182005-01-15 22:15:51 +00004394 (cur->next->type == TOK_ID ||
4395 cur->next->type == TOK_PREPROC_ID
4396 || cur->next->type == TOK_NUMBER))
4397 cur = cur->next;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004398
4399 /* If identifier consists of just one token, don't expand */
4400 if (cur == tline)
H. Peter Anvine2c80182005-01-15 22:15:51 +00004401 return tline;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004402
H. Peter Anvine2c80182005-01-15 22:15:51 +00004403 if (cur) {
4404 oldnext = cur->next; /* Detach the tail past identifier */
4405 cur->next = NULL; /* so that expand_smacro stops here */
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004406 }
4407
H. Peter Anvin734b1882002-04-30 21:01:08 +00004408 tline = expand_smacro(tline);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004409
H. Peter Anvine2c80182005-01-15 22:15:51 +00004410 if (cur) {
4411 /* expand_smacro possibly changhed tline; re-scan for EOL */
4412 cur = tline;
4413 while (cur && cur->next)
4414 cur = cur->next;
4415 if (cur)
4416 cur->next = oldnext;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004417 }
4418
4419 return tline;
4420}
4421
4422/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004423 * Determine whether the given line constitutes a multi-line macro
4424 * call, and return the MMacro structure called if so. Doesn't have
4425 * to check for an initial label - that's taken care of in
4426 * expand_mmacro - but must check numbers of parameters. Guaranteed
4427 * to be called with tline->type == TOK_ID, so the putative macro
4428 * name is easy to find.
4429 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004430static MMacro *is_mmacro(Token * tline, Token *** params_array)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004431{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004432 MMacro *head, *m;
4433 Token **params;
4434 int nparam;
4435
H. Peter Anvin166c2472008-05-28 12:28:58 -07004436 head = (MMacro *) hash_findix(&mmacros, tline->text);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004437
4438 /*
4439 * Efficiency: first we see if any macro exists with the given
4440 * name. If not, we can return NULL immediately. _Then_ we
4441 * count the parameters, and then we look further along the
4442 * list if necessary to find the proper MMacro.
4443 */
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04004444 list_for_each(m, head)
H. Peter Anvine2c80182005-01-15 22:15:51 +00004445 if (!mstrcmp(m->name, tline->text, m->casesense))
4446 break;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004447 if (!m)
H. Peter Anvine2c80182005-01-15 22:15:51 +00004448 return NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004449
4450 /*
4451 * OK, we have a potential macro. Count and demarcate the
4452 * parameters.
4453 */
H. Peter Anvin734b1882002-04-30 21:01:08 +00004454 count_mmac_params(tline->next, &nparam, &params);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004455
4456 /*
4457 * So we know how many parameters we've got. Find the MMacro
4458 * structure that handles this number.
4459 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004460 while (m) {
4461 if (m->nparam_min <= nparam
4462 && (m->plus || nparam <= m->nparam_max)) {
4463 /*
4464 * This one is right. Just check if cycle removal
4465 * prohibits us using it before we actually celebrate...
4466 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004467 if (m->in_progress > m->max_depth) {
4468 if (m->max_depth > 0) {
4469 error(ERR_WARNING,
4470 "reached maximum recursion depth of %i",
4471 m->max_depth);
4472 }
4473 nasm_free(params);
4474 return NULL;
4475 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00004476 /*
4477 * It's right, and we can use it. Add its default
4478 * parameters to the end of our list if necessary.
4479 */
4480 if (m->defaults && nparam < m->nparam_min + m->ndefs) {
4481 params =
4482 nasm_realloc(params,
4483 ((m->nparam_min + m->ndefs +
4484 1) * sizeof(*params)));
4485 while (nparam < m->nparam_min + m->ndefs) {
4486 params[nparam] = m->defaults[nparam - m->nparam_min];
4487 nparam++;
4488 }
4489 }
4490 /*
4491 * If we've gone over the maximum parameter count (and
4492 * we're in Plus mode), ignore parameters beyond
4493 * nparam_max.
4494 */
4495 if (m->plus && nparam > m->nparam_max)
4496 nparam = m->nparam_max;
4497 /*
4498 * Then terminate the parameter list, and leave.
4499 */
4500 if (!params) { /* need this special case */
4501 params = nasm_malloc(sizeof(*params));
4502 nparam = 0;
4503 }
4504 params[nparam] = NULL;
4505 *params_array = params;
4506 return m;
4507 }
4508 /*
4509 * This one wasn't right: look for the next one with the
4510 * same name.
4511 */
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04004512 list_for_each(m, m->next)
H. Peter Anvine2c80182005-01-15 22:15:51 +00004513 if (!mstrcmp(m->name, tline->text, m->casesense))
4514 break;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004515 }
4516
4517 /*
4518 * After all that, we didn't find one with the right number of
4519 * parameters. Issue a warning, and fail to expand the macro.
4520 */
H. Peter Anvin917a3492008-09-24 09:14:49 -07004521 error(ERR_WARNING|ERR_PASS1|ERR_WARN_MNP,
H. Peter Anvine2c80182005-01-15 22:15:51 +00004522 "macro `%s' exists, but not taking %d parameters",
4523 tline->text, nparam);
H. Peter Anvin734b1882002-04-30 21:01:08 +00004524 nasm_free(params);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004525 return NULL;
4526}
4527
Keith Kanios891775e2009-07-11 06:08:54 -05004528
4529/*
4530 * Save MMacro invocation specific fields in
4531 * preparation for a recursive macro expansion
4532 */
4533static void push_mmacro(MMacro *m)
4534{
4535 MMacroInvocation *i;
4536
H. Peter Anvin89cee572009-07-15 09:16:54 -04004537 i = nasm_malloc(sizeof(MMacroInvocation));
4538 i->prev = m->prev;
4539 i->params = m->params;
4540 i->iline = m->iline;
4541 i->nparam = m->nparam;
4542 i->rotate = m->rotate;
4543 i->paramlen = m->paramlen;
4544 i->unique = m->unique;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004545 i->condcnt = m->condcnt;
H. Peter Anvin89cee572009-07-15 09:16:54 -04004546 m->prev = i;
Keith Kanios891775e2009-07-11 06:08:54 -05004547}
4548
4549
4550/*
4551 * Restore MMacro invocation specific fields that were
4552 * saved during a previous recursive macro expansion
4553 */
4554static void pop_mmacro(MMacro *m)
4555{
4556 MMacroInvocation *i;
4557
H. Peter Anvin89cee572009-07-15 09:16:54 -04004558 if (m->prev) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004559 i = m->prev;
4560 m->prev = i->prev;
4561 m->params = i->params;
4562 m->iline = i->iline;
4563 m->nparam = i->nparam;
4564 m->rotate = i->rotate;
4565 m->paramlen = i->paramlen;
4566 m->unique = i->unique;
4567 m->condcnt = i->condcnt;
4568 nasm_free(i);
H. Peter Anvin89cee572009-07-15 09:16:54 -04004569 }
Keith Kanios891775e2009-07-11 06:08:54 -05004570}
4571
4572
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004573/*
4574 * Expand the multi-line macro call made by the given line, if
4575 * there is one to be expanded. If there is, push the expansion on
H. Peter Anvineba20a72002-04-30 20:53:55 +00004576 * istk->expansion and return 1. Otherwise return 0.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004577 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004578static int expand_mmacro(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004579{
4580 Token *startline = tline;
4581 Token *label = NULL;
4582 int dont_prepend = 0;
H. Peter Anvince2233b2008-05-25 21:57:00 -07004583 Token **params, *t, *mtok, *tt;
H. Peter Anvineba20a72002-04-30 20:53:55 +00004584 MMacro *m;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004585 Line *l, *ll;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004586 int i, nparam, *paramlen;
H. Peter Anvinc751e862008-06-09 10:18:45 -07004587 const char *mname;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004588
4589 t = tline;
H. Peter Anvineba20a72002-04-30 20:53:55 +00004590 skip_white_(t);
H. Peter Anvince2233b2008-05-25 21:57:00 -07004591 /* if (!tok_type_(t, TOK_ID)) Lino 02/25/02 */
H. Peter Anvindce1e2f2002-04-30 21:06:37 +00004592 if (!tok_type_(t, TOK_ID) && !tok_type_(t, TOK_PREPROC_ID))
H. Peter Anvine2c80182005-01-15 22:15:51 +00004593 return 0;
H. Peter Anvince2233b2008-05-25 21:57:00 -07004594 mtok = t;
H. Peter Anvin734b1882002-04-30 21:01:08 +00004595 m = is_mmacro(t, &params);
H. Peter Anvinc751e862008-06-09 10:18:45 -07004596 if (m) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004597 mname = t->text;
H. Peter Anvinc751e862008-06-09 10:18:45 -07004598 } else {
H. Peter Anvine2c80182005-01-15 22:15:51 +00004599 Token *last;
4600 /*
4601 * We have an id which isn't a macro call. We'll assume
4602 * it might be a label; we'll also check to see if a
4603 * colon follows it. Then, if there's another id after
4604 * that lot, we'll check it again for macro-hood.
4605 */
4606 label = last = t;
4607 t = t->next;
4608 if (tok_type_(t, TOK_WHITESPACE))
4609 last = t, t = t->next;
4610 if (tok_is_(t, ":")) {
4611 dont_prepend = 1;
4612 last = t, t = t->next;
4613 if (tok_type_(t, TOK_WHITESPACE))
4614 last = t, t = t->next;
4615 }
H. Peter Anvin89cee572009-07-15 09:16:54 -04004616 if (!tok_type_(t, TOK_ID) || !(m = is_mmacro(t, &params)))
H. Peter Anvine2c80182005-01-15 22:15:51 +00004617 return 0;
4618 last->next = NULL;
Keith Kanios891775e2009-07-11 06:08:54 -05004619 mname = t->text;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004620 tline = t;
H. Peter Anvineba20a72002-04-30 20:53:55 +00004621 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004622
4623 /*
4624 * Fix up the parameters: this involves stripping leading and
4625 * trailing whitespace, then stripping braces if they are
4626 * present.
4627 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004628 for (nparam = 0; params[nparam]; nparam++) ;
H. Peter Anvin734b1882002-04-30 21:01:08 +00004629 paramlen = nparam ? nasm_malloc(nparam * sizeof(*paramlen)) : NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004630
H. Peter Anvine2c80182005-01-15 22:15:51 +00004631 for (i = 0; params[i]; i++) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07004632 int brace = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004633 int comma = (!m->plus || i < nparam - 1);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004634
H. Peter Anvine2c80182005-01-15 22:15:51 +00004635 t = params[i];
4636 skip_white_(t);
4637 if (tok_is_(t, "{"))
H. Peter Anvin6867acc2007-10-10 14:58:45 -07004638 t = t->next, brace = true, comma = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004639 params[i] = t;
4640 paramlen[i] = 0;
4641 while (t) {
4642 if (comma && t->type == TOK_OTHER && !strcmp(t->text, ","))
4643 break; /* ... because we have hit a comma */
4644 if (comma && t->type == TOK_WHITESPACE
4645 && tok_is_(t->next, ","))
4646 break; /* ... or a space then a comma */
4647 if (brace && t->type == TOK_OTHER && !strcmp(t->text, "}"))
4648 break; /* ... or a brace */
4649 t = t->next;
4650 paramlen[i]++;
4651 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004652 }
4653
4654 /*
4655 * OK, we have a MMacro structure together with a set of
4656 * parameters. We must now go through the expansion and push
H. Peter Anvin76690a12002-04-30 20:52:49 +00004657 * copies of each Line on to istk->expansion. Substitution of
4658 * parameter tokens and macro-local tokens doesn't get done
4659 * until the single-line macro substitution process; this is
4660 * because delaying them allows us to change the semantics
4661 * later through %rotate.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004662 *
H. Peter Anvin76690a12002-04-30 20:52:49 +00004663 * First, push an end marker on to istk->expansion, mark this
4664 * macro as in progress, and set up its invocation-specific
4665 * variables.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004666 */
4667 ll = nasm_malloc(sizeof(Line));
4668 ll->next = istk->expansion;
4669 ll->finishes = m;
4670 ll->first = NULL;
4671 istk->expansion = ll;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004672
H. Peter Anvin89cee572009-07-15 09:16:54 -04004673 /*
4674 * Save the previous MMacro expansion in the case of
4675 * macro recursion
4676 */
4677 if (m->max_depth && m->in_progress)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004678 push_mmacro(m);
H. Peter Anvin76690a12002-04-30 20:52:49 +00004679
Keith Kanios891775e2009-07-11 06:08:54 -05004680 m->in_progress ++;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004681 m->params = params;
4682 m->iline = tline;
4683 m->nparam = nparam;
4684 m->rotate = 0;
4685 m->paramlen = paramlen;
4686 m->unique = unique++;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004687 m->lineno = 0;
Keith Kanios3c0d91f2009-10-25 13:28:03 -05004688 m->condcnt = 0;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004689
4690 m->next_active = istk->mstk;
4691 istk->mstk = m;
4692
Cyrill Gorcunov367d59e2010-04-09 15:43:03 +04004693 list_for_each(l, m->expansion) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00004694 Token **tail;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004695
H. Peter Anvine2c80182005-01-15 22:15:51 +00004696 ll = nasm_malloc(sizeof(Line));
4697 ll->finishes = NULL;
4698 ll->next = istk->expansion;
4699 istk->expansion = ll;
4700 tail = &ll->first;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004701
Cyrill Gorcunov367d59e2010-04-09 15:43:03 +04004702 list_for_each(t, l->first) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00004703 Token *x = t;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004704 switch (t->type) {
4705 case TOK_PREPROC_Q:
4706 tt = *tail = new_Token(NULL, TOK_ID, mname, 0);
4707 break;
4708 case TOK_PREPROC_QQ:
4709 tt = *tail = new_Token(NULL, TOK_ID, m->name, 0);
4710 break;
4711 case TOK_PREPROC_ID:
4712 if (t->text[1] == '0' && t->text[2] == '0') {
4713 dont_prepend = -1;
4714 x = label;
4715 if (!x)
4716 continue;
4717 }
4718 /* fall through */
4719 default:
4720 tt = *tail = new_Token(NULL, x->type, x->text, 0);
4721 break;
4722 }
4723 tail = &tt->next;
4724 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00004725 *tail = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004726 }
4727
4728 /*
H. Peter Anvineba20a72002-04-30 20:53:55 +00004729 * If we had a label, push it on as the first line of
4730 * the macro expansion.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004731 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004732 if (label) {
4733 if (dont_prepend < 0)
4734 free_tlist(startline);
4735 else {
4736 ll = nasm_malloc(sizeof(Line));
4737 ll->finishes = NULL;
4738 ll->next = istk->expansion;
4739 istk->expansion = ll;
4740 ll->first = startline;
4741 if (!dont_prepend) {
4742 while (label->next)
4743 label = label->next;
4744 label->next = tt = new_Token(NULL, TOK_OTHER, ":", 0);
4745 }
4746 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004747 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004748
H. Peter Anvin734b1882002-04-30 21:01:08 +00004749 list->uplevel(m->nolist ? LIST_MACRO_NOLIST : LIST_MACRO);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004750
H. Peter Anvineba20a72002-04-30 20:53:55 +00004751 return 1;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004752}
4753
Victor van den Elzen3b404c02008-09-18 13:51:36 +02004754/* The function that actually does the error reporting */
4755static void verror(int severity, const char *fmt, va_list arg)
4756{
4757 char buff[1024];
Cyrill Gorcunov4a350082010-09-24 15:24:42 +04004758 MMacro *mmac = NULL;
4759 int delta = 0;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02004760
4761 vsnprintf(buff, sizeof(buff), fmt, arg);
4762
Cyrill Gorcunov4a350082010-09-24 15:24:42 +04004763 /* get %macro name */
4764 if (istk && istk->mstk) {
4765 mmac = istk->mstk;
4766 /* but %rep blocks should be skipped */
4767 while (mmac && !mmac->name)
4768 mmac = mmac->next_active, delta++;
4769 }
4770
4771 if (mmac)
4772 nasm_error(severity, "(%s:%d) %s",
4773 mmac->name, mmac->lineno - delta, buff);
Victor van den Elzen3b404c02008-09-18 13:51:36 +02004774 else
H. Peter Anvindbb640b2009-07-18 18:57:16 -07004775 nasm_error(severity, "%s", buff);
Victor van den Elzen3b404c02008-09-18 13:51:36 +02004776}
4777
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004778/*
4779 * Since preprocessor always operate only on the line that didn't
H. Peter Anvin917a3492008-09-24 09:14:49 -07004780 * arrived yet, we should always use ERR_OFFBY1.
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004781 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004782static void error(int severity, const char *fmt, ...)
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004783{
4784 va_list arg;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004785
4786 /* If we're in a dead branch of IF or something like it, ignore the error */
H. Peter Anvin77ba0c62002-05-14 03:18:53 +00004787 if (istk && istk->conds && !emitting(istk->conds->state))
H. Peter Anvine2c80182005-01-15 22:15:51 +00004788 return;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004789
H. Peter Anvin734b1882002-04-30 21:01:08 +00004790 va_start(arg, fmt);
Victor van den Elzen3b404c02008-09-18 13:51:36 +02004791 verror(severity, fmt, arg);
H. Peter Anvin734b1882002-04-30 21:01:08 +00004792 va_end(arg);
Victor van den Elzen3b404c02008-09-18 13:51:36 +02004793}
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004794
Victor van den Elzen3b404c02008-09-18 13:51:36 +02004795/*
4796 * Because %else etc are evaluated in the state context
4797 * of the previous branch, errors might get lost with error():
4798 * %if 0 ... %else trailing garbage ... %endif
4799 * So %else etc should report errors with this function.
4800 */
4801static void error_precond(int severity, const char *fmt, ...)
4802{
4803 va_list arg;
4804
4805 /* Only ignore the error if it's really in a dead branch */
4806 if (istk && istk->conds && istk->conds->state == COND_NEVER)
4807 return;
4808
4809 va_start(arg, fmt);
4810 verror(severity, fmt, arg);
4811 va_end(arg);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004812}
4813
H. Peter Anvin734b1882002-04-30 21:01:08 +00004814static void
H. Peter Anvindbb640b2009-07-18 18:57:16 -07004815pp_reset(char *file, int apass, ListGen * listgen, StrList **deplist)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004816{
H. Peter Anvin7383b402008-09-24 10:20:40 -07004817 Token *t;
4818
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004819 cstk = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004820 istk = nasm_malloc(sizeof(Include));
4821 istk->next = NULL;
4822 istk->conds = NULL;
4823 istk->expansion = NULL;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004824 istk->mstk = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004825 istk->fp = fopen(file, "r");
H. Peter Anvineba20a72002-04-30 20:53:55 +00004826 istk->fname = NULL;
4827 src_set_fname(nasm_strdup(file));
4828 src_set_linnum(0);
4829 istk->lineinc = 1;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004830 if (!istk->fp)
H. Peter Anvin917a3492008-09-24 09:14:49 -07004831 error(ERR_FATAL|ERR_NOFILE, "unable to open input file `%s'",
H. Peter Anvine2c80182005-01-15 22:15:51 +00004832 file);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004833 defining = NULL;
Charles Crayned4200be2008-07-12 16:42:33 -07004834 nested_mac_count = 0;
4835 nested_rep_count = 0;
H. Peter Anvin97a23472007-09-16 17:57:25 -07004836 init_macros();
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004837 unique = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004838 if (tasm_compatible_mode) {
H. Peter Anvina4835d42008-05-20 14:21:29 -07004839 stdmacpos = nasm_stdmac;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004840 } else {
H. Peter Anvina4835d42008-05-20 14:21:29 -07004841 stdmacpos = nasm_stdmac_after_tasm;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004842 }
H. Peter Anvind2456592008-06-19 15:04:18 -07004843 any_extrastdmac = extrastdmac && *extrastdmac;
4844 do_predef = true;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004845 list = listgen;
H. Peter Anvin61f130f2008-09-25 15:45:06 -07004846
4847 /*
4848 * 0 for dependencies, 1 for preparatory passes, 2 for final pass.
4849 * The caller, however, will also pass in 3 for preprocess-only so
4850 * we can set __PASS__ accordingly.
4851 */
4852 pass = apass > 2 ? 2 : apass;
4853
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07004854 dephead = deptail = deplist;
4855 if (deplist) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004856 StrList *sl = nasm_malloc(strlen(file)+1+sizeof sl->next);
4857 sl->next = NULL;
4858 strcpy(sl->str, file);
4859 *deptail = sl;
4860 deptail = &sl->next;
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07004861 }
H. Peter Anvin7383b402008-09-24 10:20:40 -07004862
H. Peter Anvin61f130f2008-09-25 15:45:06 -07004863 /*
4864 * Define the __PASS__ macro. This is defined here unlike
4865 * all the other builtins, because it is special -- it varies between
4866 * passes.
4867 */
H. Peter Anvin7383b402008-09-24 10:20:40 -07004868 t = nasm_malloc(sizeof(*t));
4869 t->next = NULL;
H. Peter Anvin61f130f2008-09-25 15:45:06 -07004870 make_tok_num(t, apass);
H. Peter Anvin7383b402008-09-24 10:20:40 -07004871 t->a.mac = NULL;
4872 define_smacro(NULL, "__PASS__", true, 0, t);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004873}
4874
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004875static char *pp_getline(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004876{
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004877 char *line;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004878 Token *tline;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004879
H. Peter Anvine2c80182005-01-15 22:15:51 +00004880 while (1) {
4881 /*
Keith Kaniosb7a89542007-04-12 02:40:54 +00004882 * Fetch a tokenized line, either from the macro-expansion
H. Peter Anvine2c80182005-01-15 22:15:51 +00004883 * buffer or from the input file.
4884 */
4885 tline = NULL;
4886 while (istk->expansion && istk->expansion->finishes) {
4887 Line *l = istk->expansion;
4888 if (!l->finishes->name && l->finishes->in_progress > 1) {
4889 Line *ll;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004890
H. Peter Anvine2c80182005-01-15 22:15:51 +00004891 /*
4892 * This is a macro-end marker for a macro with no
4893 * name, which means it's not really a macro at all
4894 * but a %rep block, and the `in_progress' field is
4895 * more than 1, meaning that we still need to
4896 * repeat. (1 means the natural last repetition; 0
4897 * means termination by %exitrep.) We have
4898 * therefore expanded up to the %endrep, and must
4899 * push the whole block on to the expansion buffer
4900 * again. We don't bother to remove the macro-end
4901 * marker: we'd only have to generate another one
4902 * if we did.
4903 */
4904 l->finishes->in_progress--;
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04004905 list_for_each(l, l->finishes->expansion) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00004906 Token *t, *tt, **tail;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004907
H. Peter Anvine2c80182005-01-15 22:15:51 +00004908 ll = nasm_malloc(sizeof(Line));
4909 ll->next = istk->expansion;
4910 ll->finishes = NULL;
4911 ll->first = NULL;
4912 tail = &ll->first;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004913
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04004914 list_for_each(t, l->first) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00004915 if (t->text || t->type == TOK_WHITESPACE) {
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04004916 tt = *tail = new_Token(NULL, t->type, t->text, 0);
H. Peter Anvine2c80182005-01-15 22:15:51 +00004917 tail = &tt->next;
4918 }
4919 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00004920
H. Peter Anvine2c80182005-01-15 22:15:51 +00004921 istk->expansion = ll;
4922 }
4923 } else {
4924 /*
4925 * Check whether a `%rep' was started and not ended
4926 * within this macro expansion. This can happen and
4927 * should be detected. It's a fatal error because
4928 * I'm too confused to work out how to recover
4929 * sensibly from it.
4930 */
4931 if (defining) {
4932 if (defining->name)
4933 error(ERR_PANIC,
4934 "defining with name in expansion");
4935 else if (istk->mstk->name)
4936 error(ERR_FATAL,
4937 "`%%rep' without `%%endrep' within"
4938 " expansion of macro `%s'",
4939 istk->mstk->name);
4940 }
H. Peter Anvin87bc6192002-04-30 20:53:16 +00004941
H. Peter Anvine2c80182005-01-15 22:15:51 +00004942 /*
4943 * FIXME: investigate the relationship at this point between
4944 * istk->mstk and l->finishes
4945 */
4946 {
4947 MMacro *m = istk->mstk;
4948 istk->mstk = m->next_active;
4949 if (m->name) {
4950 /*
4951 * This was a real macro call, not a %rep, and
4952 * therefore the parameter information needs to
4953 * be freed.
4954 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004955 if (m->prev) {
4956 pop_mmacro(m);
4957 l->finishes->in_progress --;
4958 } else {
Keith Kanios891775e2009-07-11 06:08:54 -05004959 nasm_free(m->params);
4960 free_tlist(m->iline);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004961 nasm_free(m->paramlen);
4962 l->finishes->in_progress = 0;
4963 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00004964 } else
4965 free_mmacro(m);
4966 }
4967 istk->expansion = l->next;
4968 nasm_free(l);
4969 list->downlevel(LIST_MACRO);
4970 }
4971 }
4972 while (1) { /* until we get a line we can use */
H. Peter Anvineba20a72002-04-30 20:53:55 +00004973
H. Peter Anvine2c80182005-01-15 22:15:51 +00004974 if (istk->expansion) { /* from a macro expansion */
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004975 char *p;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004976 Line *l = istk->expansion;
4977 if (istk->mstk)
4978 istk->mstk->lineno++;
4979 tline = l->first;
4980 istk->expansion = l->next;
4981 nasm_free(l);
H. Peter Anvin6867acc2007-10-10 14:58:45 -07004982 p = detoken(tline, false);
H. Peter Anvine2c80182005-01-15 22:15:51 +00004983 list->line(LIST_MACRO, p);
4984 nasm_free(p);
4985 break;
4986 }
4987 line = read_line();
4988 if (line) { /* from the current input file */
4989 line = prepreproc(line);
Keith Kaniosb7a89542007-04-12 02:40:54 +00004990 tline = tokenize(line);
H. Peter Anvine2c80182005-01-15 22:15:51 +00004991 nasm_free(line);
4992 break;
4993 }
4994 /*
4995 * The current file has ended; work down the istk
4996 */
4997 {
4998 Include *i = istk;
4999 fclose(i->fp);
Cyrill Gorcunov5ace91d2010-09-12 02:00:05 +04005000 if (i->conds) {
5001 /* nasm_error can't be conditionally suppressed */
5002 nasm_error(ERR_FATAL,
5003 "expected `%%endif' before end of file");
5004 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00005005 /* only set line and file name if there's a next node */
5006 if (i->next) {
5007 src_set_linnum(i->lineno);
5008 nasm_free(src_set_fname(i->fname));
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03005009 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00005010 istk = i->next;
5011 list->downlevel(LIST_INCLUDE);
5012 nasm_free(i);
5013 if (!istk)
5014 return NULL;
Victor van den Elzen4c9d6222008-10-01 13:08:50 +02005015 if (istk->expansion && istk->expansion->finishes)
5016 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005017 }
5018 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005019
H. Peter Anvine2c80182005-01-15 22:15:51 +00005020 /*
5021 * We must expand MMacro parameters and MMacro-local labels
5022 * _before_ we plunge into directive processing, to cope
5023 * with things like `%define something %1' such as STRUC
5024 * uses. Unless we're _defining_ a MMacro, in which case
5025 * those tokens should be left alone to go into the
5026 * definition; and unless we're in a non-emitting
5027 * condition, in which case we don't want to meddle with
5028 * anything.
5029 */
Charles Crayned4200be2008-07-12 16:42:33 -07005030 if (!defining && !(istk->conds && !emitting(istk->conds->state))
H. Peter Anvin992fe752008-10-19 15:45:05 -07005031 && !(istk->mstk && !istk->mstk->in_progress)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00005032 tline = expand_mmac_params(tline);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03005033 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00005034
H. Peter Anvine2c80182005-01-15 22:15:51 +00005035 /*
5036 * Check the line to see if it's a preprocessor directive.
5037 */
5038 if (do_directive(tline) == DIRECTIVE_FOUND) {
5039 continue;
5040 } else if (defining) {
5041 /*
5042 * We're defining a multi-line macro. We emit nothing
5043 * at all, and just
Keith Kaniosb7a89542007-04-12 02:40:54 +00005044 * shove the tokenized line on to the macro definition.
H. Peter Anvine2c80182005-01-15 22:15:51 +00005045 */
5046 Line *l = nasm_malloc(sizeof(Line));
5047 l->next = defining->expansion;
5048 l->first = tline;
H. Peter Anvin538002d2008-06-28 18:30:27 -07005049 l->finishes = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005050 defining->expansion = l;
5051 continue;
5052 } else if (istk->conds && !emitting(istk->conds->state)) {
5053 /*
5054 * We're in a non-emitting branch of a condition block.
5055 * Emit nothing at all, not even a blank line: when we
5056 * emerge from the condition we'll give a line-number
5057 * directive so we keep our place correctly.
5058 */
5059 free_tlist(tline);
5060 continue;
5061 } else if (istk->mstk && !istk->mstk->in_progress) {
5062 /*
5063 * We're in a %rep block which has been terminated, so
5064 * we're walking through to the %endrep without
5065 * emitting anything. Emit nothing at all, not even a
5066 * blank line: when we emerge from the %rep block we'll
5067 * give a line-number directive so we keep our place
5068 * correctly.
5069 */
5070 free_tlist(tline);
5071 continue;
5072 } else {
5073 tline = expand_smacro(tline);
5074 if (!expand_mmacro(tline)) {
5075 /*
Keith Kaniosb7a89542007-04-12 02:40:54 +00005076 * De-tokenize the line again, and emit it.
H. Peter Anvine2c80182005-01-15 22:15:51 +00005077 */
H. Peter Anvin6867acc2007-10-10 14:58:45 -07005078 line = detoken(tline, true);
H. Peter Anvine2c80182005-01-15 22:15:51 +00005079 free_tlist(tline);
5080 break;
5081 } else {
5082 continue; /* expand_mmacro calls free_tlist */
5083 }
5084 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005085 }
5086
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005087 return line;
5088}
5089
H. Peter Anvine2c80182005-01-15 22:15:51 +00005090static void pp_cleanup(int pass)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005091{
H. Peter Anvine2c80182005-01-15 22:15:51 +00005092 if (defining) {
H. Peter Anvin89cee572009-07-15 09:16:54 -04005093 if (defining->name) {
Victor van den Elzen8f1120f2008-07-16 13:41:37 +02005094 error(ERR_NONFATAL,
5095 "end of file while still defining macro `%s'",
5096 defining->name);
5097 } else {
5098 error(ERR_NONFATAL, "end of file while still in %%rep");
5099 }
5100
H. Peter Anvine2c80182005-01-15 22:15:51 +00005101 free_mmacro(defining);
Cyrill Gorcunova327c652010-02-14 17:19:38 +03005102 defining = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005103 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005104 while (cstk)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005105 ctx_pop();
H. Peter Anvin97a23472007-09-16 17:57:25 -07005106 free_macros();
H. Peter Anvine2c80182005-01-15 22:15:51 +00005107 while (istk) {
5108 Include *i = istk;
5109 istk = istk->next;
5110 fclose(i->fp);
5111 nasm_free(i->fname);
5112 nasm_free(i);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005113 }
5114 while (cstk)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005115 ctx_pop();
H. Peter Anvin86877b22008-06-20 15:55:45 -07005116 nasm_free(src_set_fname(NULL));
H. Peter Anvine2c80182005-01-15 22:15:51 +00005117 if (pass == 0) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03005118 IncPath *i;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005119 free_llist(predef);
5120 delete_Blocks();
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03005121 while ((i = ipath)) {
5122 ipath = i->next;
5123 if (i->path)
5124 nasm_free(i->path);
5125 nasm_free(i);
5126 }
H. Peter Anvin11dfa1a2008-07-02 18:11:04 -07005127 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005128}
5129
Keith Kaniosa6dfa782007-04-13 16:47:53 +00005130void pp_include_path(char *path)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005131{
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005132 IncPath *i;
H. Peter Anvin37a321f2007-09-24 13:41:58 -07005133
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005134 i = nasm_malloc(sizeof(IncPath));
H. Peter Anvin37a321f2007-09-24 13:41:58 -07005135 i->path = path ? nasm_strdup(path) : NULL;
Frank Kotlerd0ed6fd2003-08-27 11:33:56 +00005136 i->next = NULL;
5137
H. Peter Anvin89cee572009-07-15 09:16:54 -04005138 if (ipath) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00005139 IncPath *j = ipath;
H. Peter Anvin89cee572009-07-15 09:16:54 -04005140 while (j->next)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005141 j = j->next;
5142 j->next = i;
5143 } else {
5144 ipath = i;
Frank Kotlerd0ed6fd2003-08-27 11:33:56 +00005145 }
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005146}
Frank Kotlerd0ed6fd2003-08-27 11:33:56 +00005147
Keith Kaniosa6dfa782007-04-13 16:47:53 +00005148void pp_pre_include(char *fname)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005149{
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005150 Token *inc, *space, *name;
5151 Line *l;
5152
H. Peter Anvin734b1882002-04-30 21:01:08 +00005153 name = new_Token(NULL, TOK_INTERNAL_STRING, fname, 0);
5154 space = new_Token(name, TOK_WHITESPACE, NULL, 0);
5155 inc = new_Token(space, TOK_PREPROC_ID, "%include", 0);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005156
5157 l = nasm_malloc(sizeof(Line));
5158 l->next = predef;
5159 l->first = inc;
H. Peter Anvin538002d2008-06-28 18:30:27 -07005160 l->finishes = NULL;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005161 predef = l;
5162}
5163
Keith Kaniosa6dfa782007-04-13 16:47:53 +00005164void pp_pre_define(char *definition)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005165{
5166 Token *def, *space;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005167 Line *l;
Keith Kaniosa6dfa782007-04-13 16:47:53 +00005168 char *equals;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005169
5170 equals = strchr(definition, '=');
H. Peter Anvin734b1882002-04-30 21:01:08 +00005171 space = new_Token(NULL, TOK_WHITESPACE, NULL, 0);
5172 def = new_Token(space, TOK_PREPROC_ID, "%define", 0);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005173 if (equals)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005174 *equals = ' ';
Keith Kaniosb7a89542007-04-12 02:40:54 +00005175 space->next = tokenize(definition);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005176 if (equals)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005177 *equals = '=';
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005178
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005179 l = nasm_malloc(sizeof(Line));
5180 l->next = predef;
5181 l->first = def;
H. Peter Anvin538002d2008-06-28 18:30:27 -07005182 l->finishes = NULL;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005183 predef = l;
5184}
5185
Keith Kaniosa6dfa782007-04-13 16:47:53 +00005186void pp_pre_undefine(char *definition)
H. Peter Anvin620515a2002-04-30 20:57:38 +00005187{
5188 Token *def, *space;
5189 Line *l;
5190
H. Peter Anvin734b1882002-04-30 21:01:08 +00005191 space = new_Token(NULL, TOK_WHITESPACE, NULL, 0);
5192 def = new_Token(space, TOK_PREPROC_ID, "%undef", 0);
Keith Kaniosb7a89542007-04-12 02:40:54 +00005193 space->next = tokenize(definition);
H. Peter Anvin620515a2002-04-30 20:57:38 +00005194
5195 l = nasm_malloc(sizeof(Line));
5196 l->next = predef;
5197 l->first = def;
H. Peter Anvin538002d2008-06-28 18:30:27 -07005198 l->finishes = NULL;
H. Peter Anvin620515a2002-04-30 20:57:38 +00005199 predef = l;
5200}
5201
Keith Kaniosb7a89542007-04-12 02:40:54 +00005202/*
5203 * Added by Keith Kanios:
5204 *
5205 * This function is used to assist with "runtime" preprocessor
5206 * directives. (e.g. pp_runtime("%define __BITS__ 64");)
5207 *
5208 * ERRORS ARE IGNORED HERE, SO MAKE COMPLETELY SURE THAT YOU
5209 * PASS A VALID STRING TO THIS FUNCTION!!!!!
5210 */
5211
Keith Kaniosa6dfa782007-04-13 16:47:53 +00005212void pp_runtime(char *definition)
Keith Kaniosb7a89542007-04-12 02:40:54 +00005213{
5214 Token *def;
H. Peter Anvin70653092007-10-19 14:42:29 -07005215
Keith Kaniosb7a89542007-04-12 02:40:54 +00005216 def = tokenize(definition);
H. Peter Anvin89cee572009-07-15 09:16:54 -04005217 if (do_directive(def) == NO_DIRECTIVE_FOUND)
Keith Kaniosb7a89542007-04-12 02:40:54 +00005218 free_tlist(def);
H. Peter Anvin70653092007-10-19 14:42:29 -07005219
Keith Kaniosb7a89542007-04-12 02:40:54 +00005220}
5221
H. Peter Anvina70547f2008-07-19 21:44:26 -07005222void pp_extra_stdmac(macros_t *macros)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005223{
H. Peter Anvin76690a12002-04-30 20:52:49 +00005224 extrastdmac = macros;
5225}
5226
Keith Kaniosa5fc6462007-10-13 07:09:22 -07005227static void make_tok_num(Token * tok, int64_t val)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005228{
Keith Kaniosa6dfa782007-04-13 16:47:53 +00005229 char numbuf[20];
Keith Kaniosa5fc6462007-10-13 07:09:22 -07005230 snprintf(numbuf, sizeof(numbuf), "%"PRId64"", val);
H. Peter Anvineba20a72002-04-30 20:53:55 +00005231 tok->text = nasm_strdup(numbuf);
5232 tok->type = TOK_NUMBER;
5233}
5234
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005235Preproc nasmpp = {
5236 pp_reset,
5237 pp_getline,
5238 pp_cleanup
5239};