blob: f21445b3d3afa97e1e4bc20288222c1b538015b4 [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
H. Peter Anvine2c80182005-01-15 22:15:51 +0000215struct Token {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000216 Token *next;
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000217 char *text;
H. Peter Anvinf26e0972008-07-01 21:26:27 -0700218 union {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300219 SMacro *mac; /* associated macro for TOK_SMAC_END */
220 size_t len; /* scratch length field */
221 } a; /* Auxiliary data */
H. Peter Anvinda10e7b2007-09-12 04:18:37 +0000222 enum pp_token_type type;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000223};
224
225/*
226 * Multi-line macro definitions are stored as a linked list of
227 * these, which is essentially a container to allow several linked
228 * lists of Tokens.
H. Peter Anvin70653092007-10-19 14:42:29 -0700229 *
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000230 * Note that in this module, linked lists are treated as stacks
231 * wherever possible. For this reason, Lines are _pushed_ on to the
232 * `expansion' field in MMacro structures, so that the linked list,
233 * if walked, would give the macro lines in reverse order; this
234 * means that we can walk the list when expanding a macro, and thus
235 * push the lines on to the `expansion' field in _istk_ in reverse
236 * order (so that when popped back off they are in the right
237 * order). It may seem cockeyed, and it relies on my design having
238 * an even number of steps in, but it works...
239 *
240 * Some of these structures, rather than being actual lines, are
241 * markers delimiting the end of the expansion of a given macro.
H. Peter Anvin76690a12002-04-30 20:52:49 +0000242 * This is for use in the cycle-tracking and %rep-handling code.
243 * Such structures have `finishes' non-NULL, and `first' NULL. All
244 * others have `finishes' NULL, but `first' may still be NULL if
245 * the line is blank.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000246 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000247struct Line {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000248 Line *next;
249 MMacro *finishes;
250 Token *first;
251};
252
253/*
254 * To handle an arbitrary level of file inclusion, we maintain a
255 * stack (ie linked list) of these things.
256 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000257struct Include {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000258 Include *next;
259 FILE *fp;
260 Cond *conds;
261 Line *expansion;
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000262 char *fname;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000263 int lineno, lineinc;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300264 MMacro *mstk; /* stack of active macros/reps */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000265};
266
267/*
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000268 * Include search path. This is simply a list of strings which get
269 * prepended, in turn, to the name of an include file, in an
270 * attempt to find the file if it's not in the current directory.
271 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000272struct IncPath {
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000273 IncPath *next;
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000274 char *path;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000275};
276
277/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000278 * Conditional assembly: we maintain a separate stack of these for
279 * each level of file inclusion. (The only reason we keep the
280 * stacks separate is to ensure that a stray `%endif' in a file
281 * included from within the true branch of a `%if' won't terminate
282 * it and cause confusion: instead, rightly, it'll cause an error.)
283 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000284struct Cond {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000285 Cond *next;
286 int state;
287};
H. Peter Anvine2c80182005-01-15 22:15:51 +0000288enum {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000289 /*
290 * These states are for use just after %if or %elif: IF_TRUE
291 * means the condition has evaluated to truth so we are
292 * currently emitting, whereas IF_FALSE means we are not
293 * currently emitting but will start doing so if a %else comes
294 * up. In these states, all directives are admissible: %elif,
295 * %else and %endif. (And of course %if.)
296 */
297 COND_IF_TRUE, COND_IF_FALSE,
298 /*
299 * These states come up after a %else: ELSE_TRUE means we're
300 * emitting, and ELSE_FALSE means we're not. In ELSE_* states,
301 * any %elif or %else will cause an error.
302 */
303 COND_ELSE_TRUE, COND_ELSE_FALSE,
304 /*
Victor van den Elzen3b404c02008-09-18 13:51:36 +0200305 * These states mean that we're not emitting now, and also that
306 * nothing until %endif will be emitted at all. COND_DONE is
307 * used when we've had our moment of emission
308 * and have now started seeing %elifs. COND_NEVER is used when
309 * the condition construct in question is contained within a
310 * non-emitting branch of a larger condition construct,
311 * or if there is an error.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000312 */
Victor van den Elzen3b404c02008-09-18 13:51:36 +0200313 COND_DONE, COND_NEVER
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000314};
315#define emitting(x) ( (x) == COND_IF_TRUE || (x) == COND_ELSE_TRUE )
316
H. Peter Anvin70653092007-10-19 14:42:29 -0700317/*
Ed Beroset3ab3f412002-06-11 03:31:49 +0000318 * These defines are used as the possible return values for do_directive
319 */
320#define NO_DIRECTIVE_FOUND 0
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300321#define DIRECTIVE_FOUND 1
Ed Beroset3ab3f412002-06-11 03:31:49 +0000322
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000323/*
Keith Kanios852f1ee2009-07-12 00:19:55 -0500324 * This define sets the upper limit for smacro and recursive mmacro
325 * expansions
326 */
327#define DEADMAN_LIMIT (1 << 20)
328
Cyrill Gorcunove091d6e2010-08-09 13:58:22 +0400329/* max reps */
330#define REP_LIMIT ((INT64_C(1) << 62))
331
Keith Kanios852f1ee2009-07-12 00:19:55 -0500332/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000333 * Condition codes. Note that we use c_ prefix not C_ because C_ is
334 * used in nasm.h for the "real" condition codes. At _this_ level,
335 * we treat CXZ and ECXZ as condition codes, albeit non-invertible
336 * ones, so we need a different enum...
337 */
H. Peter Anvin476d2862007-10-02 22:04:15 -0700338static const char * const conditions[] = {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000339 "a", "ae", "b", "be", "c", "cxz", "e", "ecxz", "g", "ge", "l", "le",
340 "na", "nae", "nb", "nbe", "nc", "ne", "ng", "nge", "nl", "nle", "no",
H. Peter Anvince9be342007-09-12 00:22:29 +0000341 "np", "ns", "nz", "o", "p", "pe", "po", "rcxz", "s", "z"
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000342};
H. Peter Anvin476d2862007-10-02 22:04:15 -0700343enum pp_conds {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000344 c_A, c_AE, c_B, c_BE, c_C, c_CXZ, c_E, c_ECXZ, c_G, c_GE, c_L, c_LE,
345 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 -0700346 c_NP, c_NS, c_NZ, c_O, c_P, c_PE, c_PO, c_RCXZ, c_S, c_Z,
347 c_none = -1
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000348};
H. Peter Anvin476d2862007-10-02 22:04:15 -0700349static const enum pp_conds inverse_ccs[] = {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000350 c_NA, c_NAE, c_NB, c_NBE, c_NC, -1, c_NE, -1, c_NG, c_NGE, c_NL, c_NLE,
351 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 +0000352 c_Z, c_NO, c_NP, c_PO, c_PE, -1, c_NS, c_NZ
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000353};
354
H. Peter Anvin76690a12002-04-30 20:52:49 +0000355/*
356 * Directive names.
357 */
H. Peter Anvin65747262002-05-07 00:10:05 +0000358/* If this is a an IF, ELIF, ELSE or ENDIF keyword */
H. Peter Anvin9bf0aa72007-09-12 02:12:07 +0000359static int is_condition(enum preproc_token arg)
H. Peter Anvin65747262002-05-07 00:10:05 +0000360{
H. Peter Anvinda10e7b2007-09-12 04:18:37 +0000361 return PP_IS_COND(arg) || (arg == PP_ELSE) || (arg == PP_ENDIF);
H. Peter Anvin65747262002-05-07 00:10:05 +0000362}
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000363
364/* For TASM compatibility we need to be able to recognise TASM compatible
365 * conditional compilation directives. Using the NASM pre-processor does
366 * not work, so we look for them specifically from the following list and
367 * then jam in the equivalent NASM directive into the input stream.
368 */
369
H. Peter Anvine2c80182005-01-15 22:15:51 +0000370enum {
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000371 TM_ARG, TM_ELIF, TM_ELSE, TM_ENDIF, TM_IF, TM_IFDEF, TM_IFDIFI,
372 TM_IFNDEF, TM_INCLUDE, TM_LOCAL
373};
374
H. Peter Anvin476d2862007-10-02 22:04:15 -0700375static const char * const tasm_directives[] = {
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000376 "arg", "elif", "else", "endif", "if", "ifdef", "ifdifi",
377 "ifndef", "include", "local"
378};
379
380static int StackSize = 4;
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000381static char *StackPointer = "ebp";
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000382static int ArgOffset = 8;
H. Peter Anvin8781cb02007-11-08 20:01:11 -0800383static int LocalOffset = 0;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000384
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000385static Context *cstk;
386static Include *istk;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000387static IncPath *ipath = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000388
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300389static int pass; /* HACK: pass 0 = generate dependencies only */
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700390static StrList **dephead, **deptail; /* Dependency list */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000391
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300392static uint64_t unique; /* unique identifier numbers */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000393
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000394static Line *predef = NULL;
H. Peter Anvind2456592008-06-19 15:04:18 -0700395static bool do_predef;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000396
397static ListGen *list;
398
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000399/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000400 * The current set of multi-line macros we have defined.
401 */
H. Peter Anvin166c2472008-05-28 12:28:58 -0700402static struct hash_table mmacros;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000403
404/*
405 * The current set of single-line macros we have defined.
406 */
H. Peter Anvin166c2472008-05-28 12:28:58 -0700407static struct hash_table smacros;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000408
409/*
H. Peter Anvin76690a12002-04-30 20:52:49 +0000410 * The multi-line macro we are currently defining, or the %rep
411 * block we are currently reading, if any.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000412 */
413static MMacro *defining;
414
Charles Crayned4200be2008-07-12 16:42:33 -0700415static uint64_t nested_mac_count;
416static uint64_t nested_rep_count;
417
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000418/*
419 * The number of macro parameters to allocate space for at a time.
420 */
421#define PARAM_DELTA 16
422
423/*
H. Peter Anvina4835d42008-05-20 14:21:29 -0700424 * The standard macro set: defined in macros.c in the array nasm_stdmac.
425 * This gives our position in the macro set, when we're processing it.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000426 */
H. Peter Anvina70547f2008-07-19 21:44:26 -0700427static macros_t *stdmacpos;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000428
429/*
H. Peter Anvin76690a12002-04-30 20:52:49 +0000430 * The extra standard macros that come from the object format, if
431 * any.
432 */
H. Peter Anvina70547f2008-07-19 21:44:26 -0700433static macros_t *extrastdmac = NULL;
H. Peter Anvincfb71762008-06-20 15:20:16 -0700434static bool any_extrastdmac;
H. Peter Anvin76690a12002-04-30 20:52:49 +0000435
436/*
H. Peter Anvin734b1882002-04-30 21:01:08 +0000437 * Tokens are allocated in blocks to improve speed
438 */
439#define TOKEN_BLOCKSIZE 4096
440static Token *freeTokens = NULL;
H. Peter Anvince616072002-04-30 21:02:23 +0000441struct Blocks {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000442 Blocks *next;
443 void *chunk;
H. Peter Anvince616072002-04-30 21:02:23 +0000444};
445
446static Blocks blocks = { NULL, NULL };
H. Peter Anvin734b1882002-04-30 21:01:08 +0000447
448/*
H. Peter Anvin76690a12002-04-30 20:52:49 +0000449 * Forward declarations.
450 */
H. Peter Anvin734b1882002-04-30 21:01:08 +0000451static Token *expand_mmac_params(Token * tline);
452static Token *expand_smacro(Token * tline);
453static Token *expand_id(Token * tline);
H. Peter Anvinf8ad5322009-02-21 17:55:08 -0800454static Context *get_ctx(const char *name, const char **namep,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300455 bool all_contexts);
Keith Kaniosa5fc6462007-10-13 07:09:22 -0700456static void make_tok_num(Token * tok, int64_t val);
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000457static void error(int severity, const char *fmt, ...);
Victor van den Elzen3b404c02008-09-18 13:51:36 +0200458static void error_precond(int severity, const char *fmt, ...);
H. Peter Anvince616072002-04-30 21:02:23 +0000459static void *new_Block(size_t size);
460static void delete_Blocks(void);
H. Peter Anvinc751e862008-06-09 10:18:45 -0700461static Token *new_Token(Token * next, enum pp_token_type type,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300462 const char *text, int txtlen);
H. Peter Anvin734b1882002-04-30 21:01:08 +0000463static Token *delete_Token(Token * t);
H. Peter Anvineba20a72002-04-30 20:53:55 +0000464
465/*
466 * Macros for safe checking of token pointers, avoid *(NULL)
467 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300468#define tok_type_(x,t) ((x) && (x)->type == (t))
469#define skip_white_(x) if (tok_type_((x), TOK_WHITESPACE)) (x)=(x)->next
470#define tok_is_(x,v) (tok_type_((x), TOK_OTHER) && !strcmp((x)->text,(v)))
471#define tok_isnt_(x,v) ((x) && ((x)->type!=TOK_OTHER || strcmp((x)->text,(v))))
H. Peter Anvin76690a12002-04-30 20:52:49 +0000472
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300473/*
H. Peter Anvin077fb932010-07-20 14:56:30 -0700474 * nasm_unquote with error if the string contains NUL characters.
475 * If the string contains NUL characters, issue an error and return
476 * the C len, i.e. truncate at the NUL.
477 */
478static size_t nasm_unquote_cstr(char *qstr, enum preproc_token directive)
479{
480 size_t len = nasm_unquote(qstr, NULL);
481 size_t clen = strlen(qstr);
482
483 if (len != clen)
484 error(ERR_NONFATAL, "NUL character in `%s' directive",
485 pp_directives[directive]);
486
487 return clen;
488}
489
490/*
H. Peter Anvinb40992c2010-09-15 08:57:21 -0700491 * In-place reverse a list of tokens.
492 */
493static Token *reverse_tokens(Token *t)
494{
495 Token *prev = NULL;
496 Token *next;
497
498 while (t) {
499 next = t->next;
500 t->next = prev;
501 prev = t;
502 t = next;
503 }
504
505 return prev;
506}
507
508/*
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300509 * Handle TASM specific directives, which do not contain a % in
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000510 * front of them. We do it here because I could not find any other
511 * place to do it for the moment, and it is a hack (ideally it would
512 * be nice to be able to use the NASM pre-processor to do it).
513 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000514static char *check_tasm_directive(char *line)
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000515{
Keith Kaniosb7a89542007-04-12 02:40:54 +0000516 int32_t i, j, k, m, len;
Cyrill Gorcunovf66ac7d2009-10-12 20:41:13 +0400517 char *p, *q, *oldline, oldchar;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000518
Cyrill Gorcunovf66ac7d2009-10-12 20:41:13 +0400519 p = nasm_skip_spaces(line);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000520
521 /* Binary search for the directive name */
522 i = -1;
Cyrill Gorcunova7319242010-06-03 22:04:36 +0400523 j = ARRAY_SIZE(tasm_directives);
Cyrill Gorcunovf66ac7d2009-10-12 20:41:13 +0400524 q = nasm_skip_word(p);
525 len = q - p;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000526 if (len) {
527 oldchar = p[len];
528 p[len] = 0;
529 while (j - i > 1) {
530 k = (j + i) / 2;
531 m = nasm_stricmp(p, tasm_directives[k]);
532 if (m == 0) {
533 /* We have found a directive, so jam a % in front of it
534 * so that NASM will then recognise it as one if it's own.
535 */
536 p[len] = oldchar;
537 len = strlen(p);
538 oldline = line;
539 line = nasm_malloc(len + 2);
540 line[0] = '%';
541 if (k == TM_IFDIFI) {
H. Peter Anvin18f48792009-06-27 15:56:27 -0700542 /*
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300543 * NASM does not recognise IFDIFI, so we convert
544 * it to %if 0. This is not used in NASM
545 * compatible code, but does need to parse for the
546 * TASM macro package.
H. Peter Anvine2c80182005-01-15 22:15:51 +0000547 */
H. Peter Anvin18f48792009-06-27 15:56:27 -0700548 strcpy(line + 1, "if 0");
H. Peter Anvine2c80182005-01-15 22:15:51 +0000549 } else {
550 memcpy(line + 1, p, len + 1);
551 }
552 nasm_free(oldline);
553 return line;
554 } else if (m < 0) {
555 j = k;
556 } else
557 i = k;
558 }
559 p[len] = oldchar;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000560 }
561 return line;
562}
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000563
H. Peter Anvin76690a12002-04-30 20:52:49 +0000564/*
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000565 * The pre-preprocessing stage... This function translates line
566 * number indications as they emerge from GNU cpp (`# lineno "file"
567 * flags') into NASM preprocessor line number indications (`%line
568 * lineno file').
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000569 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000570static char *prepreproc(char *line)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000571{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000572 int lineno, fnlen;
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000573 char *fname, *oldline;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000574
H. Peter Anvine2c80182005-01-15 22:15:51 +0000575 if (line[0] == '#' && line[1] == ' ') {
576 oldline = line;
577 fname = oldline + 2;
578 lineno = atoi(fname);
579 fname += strspn(fname, "0123456789 ");
580 if (*fname == '"')
581 fname++;
582 fnlen = strcspn(fname, "\"");
583 line = nasm_malloc(20 + fnlen);
584 snprintf(line, 20 + fnlen, "%%line %d %.*s", lineno, fnlen, fname);
585 nasm_free(oldline);
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000586 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000587 if (tasm_compatible_mode)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000588 return check_tasm_directive(line);
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000589 return line;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000590}
591
592/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000593 * Free a linked list of tokens.
594 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000595static void free_tlist(Token * list)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000596{
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +0400597 while (list)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000598 list = delete_Token(list);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000599}
600
601/*
602 * Free a linked list of lines.
603 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000604static void free_llist(Line * list)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000605{
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +0400606 Line *l, *tmp;
607 list_for_each_safe(l, tmp, list) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000608 free_tlist(l->first);
609 nasm_free(l);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000610 }
611}
612
613/*
H. Peter Anvineba20a72002-04-30 20:53:55 +0000614 * Free an MMacro
615 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000616static void free_mmacro(MMacro * m)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000617{
H. Peter Anvin734b1882002-04-30 21:01:08 +0000618 nasm_free(m->name);
619 free_tlist(m->dlist);
620 nasm_free(m->defaults);
621 free_llist(m->expansion);
622 nasm_free(m);
H. Peter Anvineba20a72002-04-30 20:53:55 +0000623}
624
625/*
H. Peter Anvin97a23472007-09-16 17:57:25 -0700626 * Free all currently defined macros, and free the hash tables
627 */
H. Peter Anvin072771e2008-05-22 13:17:51 -0700628static void free_smacro_table(struct hash_table *smt)
H. Peter Anvin97a23472007-09-16 17:57:25 -0700629{
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +0400630 SMacro *s, *tmp;
H. Peter Anvin072771e2008-05-22 13:17:51 -0700631 const char *key;
632 struct hash_tbl_node *it = NULL;
H. Peter Anvin97a23472007-09-16 17:57:25 -0700633
H. Peter Anvin072771e2008-05-22 13:17:51 -0700634 while ((s = hash_iterate(smt, &it, &key)) != NULL) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300635 nasm_free((void *)key);
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +0400636 list_for_each_safe(s, tmp, s) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300637 nasm_free(s->name);
638 free_tlist(s->expansion);
639 nasm_free(s);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300640 }
H. Peter Anvin97a23472007-09-16 17:57:25 -0700641 }
H. Peter Anvin072771e2008-05-22 13:17:51 -0700642 hash_free(smt);
643}
644
645static void free_mmacro_table(struct hash_table *mmt)
646{
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +0400647 MMacro *m, *tmp;
H. Peter Anvin072771e2008-05-22 13:17:51 -0700648 const char *key;
649 struct hash_tbl_node *it = NULL;
H. Peter Anvin97a23472007-09-16 17:57:25 -0700650
651 it = NULL;
H. Peter Anvin072771e2008-05-22 13:17:51 -0700652 while ((m = hash_iterate(mmt, &it, &key)) != NULL) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300653 nasm_free((void *)key);
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +0400654 list_for_each_safe(m ,tmp, m)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300655 free_mmacro(m);
H. Peter Anvin97a23472007-09-16 17:57:25 -0700656 }
H. Peter Anvin072771e2008-05-22 13:17:51 -0700657 hash_free(mmt);
658}
659
660static void free_macros(void)
661{
H. Peter Anvin166c2472008-05-28 12:28:58 -0700662 free_smacro_table(&smacros);
663 free_mmacro_table(&mmacros);
H. Peter Anvin97a23472007-09-16 17:57:25 -0700664}
665
666/*
667 * Initialize the hash tables
668 */
669static void init_macros(void)
670{
H. Peter Anvin166c2472008-05-28 12:28:58 -0700671 hash_init(&smacros, HASH_LARGE);
672 hash_init(&mmacros, HASH_LARGE);
H. Peter Anvin97a23472007-09-16 17:57:25 -0700673}
674
675/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000676 * Pop the context stack.
677 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000678static void ctx_pop(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000679{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000680 Context *c = cstk;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000681
682 cstk = cstk->next;
H. Peter Anvin166c2472008-05-28 12:28:58 -0700683 free_smacro_table(&c->localmac);
H. Peter Anvin734b1882002-04-30 21:01:08 +0000684 nasm_free(c->name);
685 nasm_free(c);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000686}
687
H. Peter Anvin072771e2008-05-22 13:17:51 -0700688/*
689 * Search for a key in the hash index; adding it if necessary
690 * (in which case we initialize the data pointer to NULL.)
691 */
692static void **
693hash_findi_add(struct hash_table *hash, const char *str)
694{
695 struct hash_insert hi;
696 void **r;
697 char *strx;
698
699 r = hash_findi(hash, str, &hi);
700 if (r)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300701 return r;
H. Peter Anvin072771e2008-05-22 13:17:51 -0700702
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300703 strx = nasm_strdup(str); /* Use a more efficient allocator here? */
H. Peter Anvin072771e2008-05-22 13:17:51 -0700704 return hash_add(&hi, strx, NULL);
705}
706
707/*
708 * Like hash_findi, but returns the data element rather than a pointer
709 * to it. Used only when not adding a new element, hence no third
710 * argument.
711 */
712static void *
713hash_findix(struct hash_table *hash, const char *str)
714{
715 void **p;
716
717 p = hash_findi(hash, str, NULL);
718 return p ? *p : NULL;
719}
720
Cyrill Gorcunov15bdc512010-07-13 11:27:41 +0400721/*
722 * read line from standart macros set,
723 * if there no more left -- return NULL
724 */
725static char *line_from_stdmac(void)
726{
727 unsigned char c;
728 const unsigned char *p = stdmacpos;
729 char *line, *q;
730 size_t len = 0;
731
732 if (!stdmacpos)
733 return NULL;
734
735 while ((c = *p++)) {
736 if (c >= 0x80)
737 len += pp_directives_len[c - 0x80] + 1;
738 else
739 len++;
740 }
741
742 line = nasm_malloc(len + 1);
743 q = line;
744 while ((c = *stdmacpos++)) {
745 if (c >= 0x80) {
746 memcpy(q, pp_directives[c - 0x80], pp_directives_len[c - 0x80]);
747 q += pp_directives_len[c - 0x80];
748 *q++ = ' ';
749 } else {
750 *q++ = c;
751 }
752 }
753 stdmacpos = p;
754 *q = '\0';
755
756 if (!*stdmacpos) {
757 /* This was the last of the standard macro chain... */
758 stdmacpos = NULL;
759 if (any_extrastdmac) {
760 stdmacpos = extrastdmac;
761 any_extrastdmac = false;
762 } else if (do_predef) {
763 Line *pd, *l;
764 Token *head, **tail, *t;
765
766 /*
767 * Nasty hack: here we push the contents of
768 * `predef' on to the top-level expansion stack,
769 * since this is the most convenient way to
770 * implement the pre-include and pre-define
771 * features.
772 */
773 list_for_each(pd, predef) {
774 head = NULL;
775 tail = &head;
776 list_for_each(t, pd->first) {
777 *tail = new_Token(NULL, t->type, t->text, 0);
778 tail = &(*tail)->next;
779 }
780
781 l = nasm_malloc(sizeof(Line));
782 l->next = istk->expansion;
783 l->first = head;
784 l->finishes = NULL;
785
786 istk->expansion = l;
787 }
788 do_predef = false;
789 }
790 }
791
792 return line;
793}
794
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000795#define BUF_DELTA 512
796/*
797 * Read a line from the top file in istk, handling multiple CR/LFs
798 * at the end of the line read, and handling spurious ^Zs. Will
799 * return lines from the standard macro set if this has not already
800 * been done.
801 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000802static char *read_line(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000803{
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000804 char *buffer, *p, *q;
H. Peter Anvin9f394642002-04-30 21:07:51 +0000805 int bufsize, continued_count;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000806
Cyrill Gorcunov15bdc512010-07-13 11:27:41 +0400807 /*
808 * standart macros set (predefined) goes first
809 */
810 p = line_from_stdmac();
811 if (p)
812 return p;
H. Peter Anvin72edbb82008-06-19 16:00:04 -0700813
Cyrill Gorcunov15bdc512010-07-13 11:27:41 +0400814 /*
815 * regular read from a file
816 */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000817 bufsize = BUF_DELTA;
818 buffer = nasm_malloc(BUF_DELTA);
819 p = buffer;
H. Peter Anvin9f394642002-04-30 21:07:51 +0000820 continued_count = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000821 while (1) {
822 q = fgets(p, bufsize - (p - buffer), istk->fp);
823 if (!q)
824 break;
825 p += strlen(p);
826 if (p > buffer && p[-1] == '\n') {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300827 /*
828 * Convert backslash-CRLF line continuation sequences into
829 * nothing at all (for DOS and Windows)
830 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000831 if (((p - 2) > buffer) && (p[-3] == '\\') && (p[-2] == '\r')) {
832 p -= 3;
833 *p = 0;
834 continued_count++;
835 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300836 /*
837 * Also convert backslash-LF line continuation sequences into
838 * nothing at all (for Unix)
839 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000840 else if (((p - 1) > buffer) && (p[-2] == '\\')) {
841 p -= 2;
842 *p = 0;
843 continued_count++;
844 } else {
845 break;
846 }
847 }
848 if (p - buffer > bufsize - 10) {
Keith Kaniosb7a89542007-04-12 02:40:54 +0000849 int32_t offset = p - buffer;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000850 bufsize += BUF_DELTA;
851 buffer = nasm_realloc(buffer, bufsize);
852 p = buffer + offset; /* prevent stale-pointer problems */
853 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000854 }
855
H. Peter Anvine2c80182005-01-15 22:15:51 +0000856 if (!q && p == buffer) {
857 nasm_free(buffer);
858 return NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000859 }
860
H. Peter Anvine2c80182005-01-15 22:15:51 +0000861 src_set_linnum(src_get_linnum() + istk->lineinc +
862 (continued_count * istk->lineinc));
H. Peter Anvineba20a72002-04-30 20:53:55 +0000863
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000864 /*
865 * Play safe: remove CRs as well as LFs, if any of either are
866 * present at the end of the line.
867 */
H. Peter Anvineba20a72002-04-30 20:53:55 +0000868 while (--p >= buffer && (*p == '\n' || *p == '\r'))
H. Peter Anvine2c80182005-01-15 22:15:51 +0000869 *p = '\0';
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000870
871 /*
872 * Handle spurious ^Z, which may be inserted into source files
873 * by some file transfer utilities.
874 */
875 buffer[strcspn(buffer, "\032")] = '\0';
876
H. Peter Anvin734b1882002-04-30 21:01:08 +0000877 list->line(LIST_READ, buffer);
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000878
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000879 return buffer;
880}
881
882/*
Keith Kaniosb7a89542007-04-12 02:40:54 +0000883 * Tokenize a line of text. This is a very simple process since we
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000884 * don't need to parse the value out of e.g. numeric tokens: we
885 * simply split one string into many.
886 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000887static Token *tokenize(char *line)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000888{
H. Peter Anvinca544db2008-10-19 19:30:11 -0700889 char c, *p = line;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +0000890 enum pp_token_type type;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000891 Token *list = NULL;
892 Token *t, **tail = &list;
893
H. Peter Anvine2c80182005-01-15 22:15:51 +0000894 while (*line) {
895 p = line;
896 if (*p == '%') {
897 p++;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300898 if (*p == '+' && !nasm_isdigit(p[1])) {
899 p++;
900 type = TOK_PASTE;
901 } else if (nasm_isdigit(*p) ||
902 ((*p == '-' || *p == '+') && nasm_isdigit(p[1]))) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000903 do {
904 p++;
905 }
H. Peter Anvinbda7a6e2008-06-21 10:23:17 -0700906 while (nasm_isdigit(*p));
H. Peter Anvine2c80182005-01-15 22:15:51 +0000907 type = TOK_PREPROC_ID;
908 } else if (*p == '{') {
909 p++;
910 while (*p && *p != '}') {
911 p[-1] = *p;
912 p++;
913 }
914 p[-1] = '\0';
915 if (*p)
916 p++;
917 type = TOK_PREPROC_ID;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300918 } else if (*p == '[') {
919 int lvl = 1;
920 line += 2; /* Skip the leading %[ */
921 p++;
922 while (lvl && (c = *p++)) {
923 switch (c) {
924 case ']':
925 lvl--;
926 break;
927 case '%':
928 if (*p == '[')
929 lvl++;
930 break;
931 case '\'':
932 case '\"':
933 case '`':
Cyrill Gorcunovc6360a72010-07-13 13:32:19 +0400934 p = nasm_skip_string(p - 1) + 1;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300935 break;
936 default:
937 break;
938 }
939 }
940 p--;
941 if (*p)
942 *p++ = '\0';
943 if (lvl)
944 error(ERR_NONFATAL, "unterminated %[ construct");
945 type = TOK_INDIRECT;
946 } else if (*p == '?') {
947 type = TOK_PREPROC_Q; /* %? */
948 p++;
949 if (*p == '?') {
950 type = TOK_PREPROC_QQ; /* %?? */
951 p++;
952 }
Cyrill Gorcunov84b4cba2010-09-12 21:39:40 +0400953 } else if (*p == '!') {
954 type = TOK_PREPROC_ID;
955 p++;
956 if (isidchar(*p)) {
957 do {
958 p++;
959 } while (isidchar(*p));
960 } else if (*p == '\'' || *p == '\"' || *p == '`') {
961 p = nasm_skip_string(p);
962 if (*p)
963 p++;
964 else
965 error(ERR_NONFATAL|ERR_PASS1, "unterminated %! string");
966 } else {
967 /* %! without string or identifier */
968 type = TOK_OTHER; /* Legacy behavior... */
969 }
H. Peter Anvine2c80182005-01-15 22:15:51 +0000970 } else if (isidchar(*p) ||
971 ((*p == '!' || *p == '%' || *p == '$') &&
972 isidchar(p[1]))) {
973 do {
974 p++;
975 }
976 while (isidchar(*p));
977 type = TOK_PREPROC_ID;
978 } else {
979 type = TOK_OTHER;
980 if (*p == '%')
981 p++;
982 }
983 } else if (isidstart(*p) || (*p == '$' && isidstart(p[1]))) {
984 type = TOK_ID;
985 p++;
986 while (*p && isidchar(*p))
987 p++;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -0700988 } else if (*p == '\'' || *p == '"' || *p == '`') {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000989 /*
990 * A string token.
991 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000992 type = TOK_STRING;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300993 p = nasm_skip_string(p);
Nickolay Yurchenkof3b3ce22003-09-21 20:38:43 +0000994
H. Peter Anvine2c80182005-01-15 22:15:51 +0000995 if (*p) {
996 p++;
997 } else {
H. Peter Anvin917a3492008-09-24 09:14:49 -0700998 error(ERR_WARNING|ERR_PASS1, "unterminated string");
H. Peter Anvine2c80182005-01-15 22:15:51 +0000999 /* Handling unterminated strings by UNV */
1000 /* type = -1; */
1001 }
Victor van den Elzenfb5f2512009-04-17 16:17:59 +02001002 } else if (p[0] == '$' && p[1] == '$') {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001003 type = TOK_OTHER; /* TOKEN_BASE */
Victor van den Elzenfb5f2512009-04-17 16:17:59 +02001004 p += 2;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001005 } else if (isnumstart(*p)) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001006 bool is_hex = false;
1007 bool is_float = false;
1008 bool has_e = false;
1009 char c, *r;
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001010
H. Peter Anvine2c80182005-01-15 22:15:51 +00001011 /*
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001012 * A numeric token.
H. Peter Anvine2c80182005-01-15 22:15:51 +00001013 */
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001014
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001015 if (*p == '$') {
1016 p++;
1017 is_hex = true;
1018 }
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001019
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001020 for (;;) {
1021 c = *p++;
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001022
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001023 if (!is_hex && (c == 'e' || c == 'E')) {
1024 has_e = true;
1025 if (*p == '+' || *p == '-') {
1026 /*
1027 * e can only be followed by +/- if it is either a
1028 * prefixed hex number or a floating-point number
1029 */
1030 p++;
1031 is_float = true;
1032 }
1033 } else if (c == 'H' || c == 'h' || c == 'X' || c == 'x') {
1034 is_hex = true;
1035 } else if (c == 'P' || c == 'p') {
1036 is_float = true;
1037 if (*p == '+' || *p == '-')
1038 p++;
1039 } else if (isnumchar(c) || c == '_')
1040 ; /* just advance */
1041 else if (c == '.') {
1042 /*
1043 * we need to deal with consequences of the legacy
1044 * parser, like "1.nolist" being two tokens
1045 * (TOK_NUMBER, TOK_ID) here; at least give it
1046 * a shot for now. In the future, we probably need
1047 * a flex-based scanner with proper pattern matching
1048 * to do it as well as it can be done. Nothing in
1049 * the world is going to help the person who wants
1050 * 0x123.p16 interpreted as two tokens, though.
1051 */
1052 r = p;
1053 while (*r == '_')
1054 r++;
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001055
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001056 if (nasm_isdigit(*r) || (is_hex && nasm_isxdigit(*r)) ||
1057 (!is_hex && (*r == 'e' || *r == 'E')) ||
1058 (*r == 'p' || *r == 'P')) {
1059 p = r;
1060 is_float = true;
1061 } else
1062 break; /* Terminate the token */
1063 } else
1064 break;
1065 }
1066 p--; /* Point to first character beyond number */
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001067
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001068 if (p == line+1 && *line == '$') {
1069 type = TOK_OTHER; /* TOKEN_HERE */
1070 } else {
1071 if (has_e && !is_hex) {
1072 /* 1e13 is floating-point, but 1e13h is not */
1073 is_float = true;
1074 }
H. Peter Anvind784a082009-04-20 14:01:18 -07001075
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001076 type = is_float ? TOK_FLOAT : TOK_NUMBER;
1077 }
H. Peter Anvinbda7a6e2008-06-21 10:23:17 -07001078 } else if (nasm_isspace(*p)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001079 type = TOK_WHITESPACE;
Cyrill Gorcunovf66ac7d2009-10-12 20:41:13 +04001080 p = nasm_skip_spaces(p);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001081 /*
1082 * Whitespace just before end-of-line is discarded by
1083 * pretending it's a comment; whitespace just before a
1084 * comment gets lumped into the comment.
1085 */
1086 if (!*p || *p == ';') {
1087 type = TOK_COMMENT;
1088 while (*p)
1089 p++;
1090 }
1091 } else if (*p == ';') {
1092 type = TOK_COMMENT;
1093 while (*p)
1094 p++;
1095 } else {
1096 /*
1097 * Anything else is an operator of some kind. We check
1098 * for all the double-character operators (>>, <<, //,
1099 * %%, <=, >=, ==, !=, <>, &&, ||, ^^), but anything
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001100 * else is a single-character operator.
H. Peter Anvine2c80182005-01-15 22:15:51 +00001101 */
1102 type = TOK_OTHER;
1103 if ((p[0] == '>' && p[1] == '>') ||
1104 (p[0] == '<' && p[1] == '<') ||
1105 (p[0] == '/' && p[1] == '/') ||
1106 (p[0] == '<' && p[1] == '=') ||
1107 (p[0] == '>' && p[1] == '=') ||
1108 (p[0] == '=' && p[1] == '=') ||
1109 (p[0] == '!' && p[1] == '=') ||
1110 (p[0] == '<' && p[1] == '>') ||
1111 (p[0] == '&' && p[1] == '&') ||
1112 (p[0] == '|' && p[1] == '|') ||
1113 (p[0] == '^' && p[1] == '^')) {
1114 p++;
1115 }
1116 p++;
1117 }
Nickolay Yurchenkof3b3ce22003-09-21 20:38:43 +00001118
H. Peter Anvine2c80182005-01-15 22:15:51 +00001119 /* Handling unterminated string by UNV */
1120 /*if (type == -1)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001121 {
1122 *tail = t = new_Token(NULL, TOK_STRING, line, p-line+1);
1123 t->text[p-line] = *line;
1124 tail = &t->next;
1125 }
1126 else */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001127 if (type != TOK_COMMENT) {
1128 *tail = t = new_Token(NULL, type, line, p - line);
1129 tail = &t->next;
1130 }
1131 line = p;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001132 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001133 return list;
1134}
1135
H. Peter Anvince616072002-04-30 21:02:23 +00001136/*
1137 * this function allocates a new managed block of memory and
H. Peter Anvin70653092007-10-19 14:42:29 -07001138 * returns a pointer to the block. The managed blocks are
H. Peter Anvince616072002-04-30 21:02:23 +00001139 * deleted only all at once by the delete_Blocks function.
1140 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001141static void *new_Block(size_t size)
H. Peter Anvince616072002-04-30 21:02:23 +00001142{
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001143 Blocks *b = &blocks;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001144
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001145 /* first, get to the end of the linked list */
1146 while (b->next)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001147 b = b->next;
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001148 /* now allocate the requested chunk */
1149 b->chunk = nasm_malloc(size);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001150
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001151 /* now allocate a new block for the next request */
1152 b->next = nasm_malloc(sizeof(Blocks));
1153 /* and initialize the contents of the new block */
1154 b->next->next = NULL;
1155 b->next->chunk = NULL;
1156 return b->chunk;
H. Peter Anvince616072002-04-30 21:02:23 +00001157}
1158
1159/*
1160 * this function deletes all managed blocks of memory
1161 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001162static void delete_Blocks(void)
H. Peter Anvince616072002-04-30 21:02:23 +00001163{
H. Peter Anvine2c80182005-01-15 22:15:51 +00001164 Blocks *a, *b = &blocks;
H. Peter Anvince616072002-04-30 21:02:23 +00001165
H. Peter Anvin70653092007-10-19 14:42:29 -07001166 /*
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001167 * keep in mind that the first block, pointed to by blocks
H. Peter Anvin70653092007-10-19 14:42:29 -07001168 * is a static and not dynamically allocated, so we don't
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001169 * free it.
1170 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001171 while (b) {
1172 if (b->chunk)
1173 nasm_free(b->chunk);
1174 a = b;
1175 b = b->next;
1176 if (a != &blocks)
1177 nasm_free(a);
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001178 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001179}
H. Peter Anvin734b1882002-04-30 21:01:08 +00001180
1181/*
H. Peter Anvin70653092007-10-19 14:42:29 -07001182 * this function creates a new Token and passes a pointer to it
H. Peter Anvin734b1882002-04-30 21:01:08 +00001183 * back to the caller. It sets the type and text elements, and
H. Peter Anvinf26e0972008-07-01 21:26:27 -07001184 * also the a.mac and next elements to NULL.
H. Peter Anvin734b1882002-04-30 21:01:08 +00001185 */
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07001186static Token *new_Token(Token * next, enum pp_token_type type,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001187 const char *text, int txtlen)
H. Peter Anvin734b1882002-04-30 21:01:08 +00001188{
1189 Token *t;
1190 int i;
1191
H. Peter Anvin89cee572009-07-15 09:16:54 -04001192 if (!freeTokens) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001193 freeTokens = (Token *) new_Block(TOKEN_BLOCKSIZE * sizeof(Token));
1194 for (i = 0; i < TOKEN_BLOCKSIZE - 1; i++)
1195 freeTokens[i].next = &freeTokens[i + 1];
1196 freeTokens[i].next = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001197 }
1198 t = freeTokens;
1199 freeTokens = t->next;
1200 t->next = next;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07001201 t->a.mac = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001202 t->type = type;
H. Peter Anvin89cee572009-07-15 09:16:54 -04001203 if (type == TOK_WHITESPACE || !text) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001204 t->text = NULL;
1205 } else {
1206 if (txtlen == 0)
1207 txtlen = strlen(text);
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07001208 t->text = nasm_malloc(txtlen+1);
1209 memcpy(t->text, text, txtlen);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001210 t->text[txtlen] = '\0';
H. Peter Anvin734b1882002-04-30 21:01:08 +00001211 }
1212 return t;
1213}
1214
H. Peter Anvine2c80182005-01-15 22:15:51 +00001215static Token *delete_Token(Token * t)
H. Peter Anvin734b1882002-04-30 21:01:08 +00001216{
1217 Token *next = t->next;
1218 nasm_free(t->text);
H. Peter Anvin788e6c12002-04-30 21:02:01 +00001219 t->next = freeTokens;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001220 freeTokens = t;
1221 return next;
1222}
1223
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001224/*
1225 * Convert a line of tokens back into text.
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001226 * If expand_locals is not zero, identifiers of the form "%$*xxx"
1227 * will be transformed into ..@ctxnum.xxx
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001228 */
H. Peter Anvin9e200162008-06-04 17:23:14 -07001229static char *detoken(Token * tlist, bool expand_locals)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001230{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001231 Token *t;
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001232 char *line, *p;
H. Peter Anvinb4daadc2008-01-21 16:31:57 -08001233 const char *q;
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001234 int len = 0;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001235
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001236 list_for_each(t, tlist) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001237 if (t->type == TOK_PREPROC_ID && t->text[1] == '!') {
Cyrill Gorcunov84b4cba2010-09-12 21:39:40 +04001238 char *v;
1239 char *q = t->text;
H. Peter Anvin077fb932010-07-20 14:56:30 -07001240
Cyrill Gorcunov84b4cba2010-09-12 21:39:40 +04001241 v = t->text + 2;
1242 if (*v == '\'' || *v == '\"' || *v == '`') {
1243 size_t len = nasm_unquote(v, NULL);
1244 size_t clen = strlen(v);
H. Peter Anvin077fb932010-07-20 14:56:30 -07001245
Cyrill Gorcunov84b4cba2010-09-12 21:39:40 +04001246 if (len != clen) {
1247 error(ERR_NONFATAL | ERR_PASS1,
1248 "NUL character in %! string");
1249 v = NULL;
1250 }
1251 }
H. Peter Anvin077fb932010-07-20 14:56:30 -07001252
Cyrill Gorcunov84b4cba2010-09-12 21:39:40 +04001253 if (v) {
1254 char *p = getenv(v);
1255 if (!p) {
1256 error(ERR_NONFATAL | ERR_PASS1,
1257 "nonexistent environment variable `%s'", v);
1258 p = "";
1259 }
1260 t->text = nasm_strdup(p);
1261 }
1262 nasm_free(q);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001263 }
H. Peter Anvin077fb932010-07-20 14:56:30 -07001264
H. Peter Anvine2c80182005-01-15 22:15:51 +00001265 /* Expand local macros here and not during preprocessing */
1266 if (expand_locals &&
1267 t->type == TOK_PREPROC_ID && t->text &&
1268 t->text[0] == '%' && t->text[1] == '$') {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001269 const char *q;
1270 char *p;
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001271 Context *ctx = get_ctx(t->text, &q, false);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001272 if (ctx) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001273 char buffer[40];
Keith Kanios93f2e9a2007-04-14 00:10:59 +00001274 snprintf(buffer, sizeof(buffer), "..@%"PRIu32".", ctx->number);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001275 p = nasm_strcat(buffer, q);
1276 nasm_free(t->text);
1277 t->text = p;
1278 }
1279 }
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001280 if (t->type == TOK_WHITESPACE)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001281 len++;
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001282 else if (t->text)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001283 len += strlen(t->text);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001284 }
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001285
H. Peter Anvin734b1882002-04-30 21:01:08 +00001286 p = line = nasm_malloc(len + 1);
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001287
1288 list_for_each(t, tlist) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001289 if (t->type == TOK_WHITESPACE) {
H. Peter Anvinb4daadc2008-01-21 16:31:57 -08001290 *p++ = ' ';
H. Peter Anvine2c80182005-01-15 22:15:51 +00001291 } else if (t->text) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001292 q = t->text;
1293 while (*q)
1294 *p++ = *q++;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001295 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001296 }
1297 *p = '\0';
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001298
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001299 return line;
1300}
1301
1302/*
H. Peter Anvin76690a12002-04-30 20:52:49 +00001303 * A scanner, suitable for use by the expression evaluator, which
1304 * operates on a line of Tokens. Expects a pointer to a pointer to
1305 * the first token in the line to be passed in as its private_data
1306 * field.
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001307 *
1308 * FIX: This really needs to be unified with stdscan.
H. Peter Anvin76690a12002-04-30 20:52:49 +00001309 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001310static int ppscan(void *private_data, struct tokenval *tokval)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001311{
H. Peter Anvin76690a12002-04-30 20:52:49 +00001312 Token **tlineptr = private_data;
1313 Token *tline;
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001314 char ourcopy[MAX_KEYWORD+1], *p, *r, *s;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001315
H. Peter Anvine2c80182005-01-15 22:15:51 +00001316 do {
1317 tline = *tlineptr;
1318 *tlineptr = tline ? tline->next : NULL;
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04001319 } while (tline && (tline->type == TOK_WHITESPACE ||
1320 tline->type == TOK_COMMENT));
H. Peter Anvin76690a12002-04-30 20:52:49 +00001321
1322 if (!tline)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001323 return tokval->t_type = TOKEN_EOS;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001324
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001325 tokval->t_charptr = tline->text;
1326
H. Peter Anvin76690a12002-04-30 20:52:49 +00001327 if (tline->text[0] == '$' && !tline->text[1])
H. Peter Anvine2c80182005-01-15 22:15:51 +00001328 return tokval->t_type = TOKEN_HERE;
H. Peter Anvin7cf897e2002-05-30 21:30:33 +00001329 if (tline->text[0] == '$' && tline->text[1] == '$' && !tline->text[2])
H. Peter Anvine2c80182005-01-15 22:15:51 +00001330 return tokval->t_type = TOKEN_BASE;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001331
H. Peter Anvine2c80182005-01-15 22:15:51 +00001332 if (tline->type == TOK_ID) {
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001333 p = tokval->t_charptr = tline->text;
1334 if (p[0] == '$') {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001335 tokval->t_charptr++;
1336 return tokval->t_type = TOKEN_ID;
1337 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00001338
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001339 for (r = p, s = ourcopy; *r; r++) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001340 if (r >= p+MAX_KEYWORD)
1341 return tokval->t_type = TOKEN_ID; /* Not a keyword */
H. Peter Anvinac8f8fc2008-06-11 15:49:41 -07001342 *s++ = nasm_tolower(*r);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001343 }
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001344 *s = '\0';
1345 /* right, so we have an identifier sitting in temp storage. now,
1346 * is it actually a register or instruction name, or what? */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001347 return nasm_token_hash(ourcopy, tokval);
H. Peter Anvin76690a12002-04-30 20:52:49 +00001348 }
1349
H. Peter Anvine2c80182005-01-15 22:15:51 +00001350 if (tline->type == TOK_NUMBER) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001351 bool rn_error;
1352 tokval->t_integer = readnum(tline->text, &rn_error);
1353 tokval->t_charptr = tline->text;
1354 if (rn_error)
1355 return tokval->t_type = TOKEN_ERRNUM;
1356 else
1357 return tokval->t_type = TOKEN_NUM;
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001358 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00001359
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001360 if (tline->type == TOK_FLOAT) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001361 return tokval->t_type = TOKEN_FLOAT;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001362 }
1363
H. Peter Anvine2c80182005-01-15 22:15:51 +00001364 if (tline->type == TOK_STRING) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001365 char bq, *ep;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001366
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001367 bq = tline->text[0];
H. Peter Anvin11627042008-06-09 20:45:19 -07001368 tokval->t_charptr = tline->text;
1369 tokval->t_inttwo = nasm_unquote(tline->text, &ep);
H. Peter Anvind2456592008-06-19 15:04:18 -07001370
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001371 if (ep[0] != bq || ep[1] != '\0')
1372 return tokval->t_type = TOKEN_ERRSTR;
1373 else
1374 return tokval->t_type = TOKEN_STR;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001375 }
1376
H. Peter Anvine2c80182005-01-15 22:15:51 +00001377 if (tline->type == TOK_OTHER) {
1378 if (!strcmp(tline->text, "<<"))
1379 return tokval->t_type = TOKEN_SHL;
1380 if (!strcmp(tline->text, ">>"))
1381 return tokval->t_type = TOKEN_SHR;
1382 if (!strcmp(tline->text, "//"))
1383 return tokval->t_type = TOKEN_SDIV;
1384 if (!strcmp(tline->text, "%%"))
1385 return tokval->t_type = TOKEN_SMOD;
1386 if (!strcmp(tline->text, "=="))
1387 return tokval->t_type = TOKEN_EQ;
1388 if (!strcmp(tline->text, "<>"))
1389 return tokval->t_type = TOKEN_NE;
1390 if (!strcmp(tline->text, "!="))
1391 return tokval->t_type = TOKEN_NE;
1392 if (!strcmp(tline->text, "<="))
1393 return tokval->t_type = TOKEN_LE;
1394 if (!strcmp(tline->text, ">="))
1395 return tokval->t_type = TOKEN_GE;
1396 if (!strcmp(tline->text, "&&"))
1397 return tokval->t_type = TOKEN_DBL_AND;
1398 if (!strcmp(tline->text, "^^"))
1399 return tokval->t_type = TOKEN_DBL_XOR;
1400 if (!strcmp(tline->text, "||"))
1401 return tokval->t_type = TOKEN_DBL_OR;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001402 }
1403
1404 /*
1405 * We have no other options: just return the first character of
1406 * the token text.
1407 */
1408 return tokval->t_type = tline->text[0];
1409}
1410
1411/*
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001412 * Compare a string to the name of an existing macro; this is a
1413 * simple wrapper which calls either strcmp or nasm_stricmp
1414 * depending on the value of the `casesense' parameter.
1415 */
H. Peter Anvin4db5a162007-10-11 13:42:09 -07001416static int mstrcmp(const char *p, const char *q, bool casesense)
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001417{
H. Peter Anvin734b1882002-04-30 21:01:08 +00001418 return casesense ? strcmp(p, q) : nasm_stricmp(p, q);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001419}
1420
1421/*
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07001422 * Compare a string to the name of an existing macro; this is a
1423 * simple wrapper which calls either strcmp or nasm_stricmp
1424 * depending on the value of the `casesense' parameter.
1425 */
1426static int mmemcmp(const char *p, const char *q, size_t l, bool casesense)
1427{
1428 return casesense ? memcmp(p, q, l) : nasm_memicmp(p, q, l);
1429}
1430
1431/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001432 * Return the Context structure associated with a %$ token. Return
1433 * NULL, having _already_ reported an error condition, if the
1434 * context stack isn't deep enough for the supplied number of $
1435 * signs.
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001436 * If all_contexts == true, contexts that enclose current are
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001437 * also scanned for such smacro, until it is found; if not -
1438 * only the context that directly results from the number of $'s
1439 * in variable's name.
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001440 *
1441 * If "namep" is non-NULL, set it to the pointer to the macro name
1442 * tail, i.e. the part beyond %$...
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001443 */
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001444static Context *get_ctx(const char *name, const char **namep,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001445 bool all_contexts)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001446{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001447 Context *ctx;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001448 SMacro *m;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001449 int i;
1450
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001451 if (namep)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001452 *namep = name;
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001453
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001454 if (!name || name[0] != '%' || name[1] != '$')
H. Peter Anvine2c80182005-01-15 22:15:51 +00001455 return NULL;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001456
H. Peter Anvine2c80182005-01-15 22:15:51 +00001457 if (!cstk) {
1458 error(ERR_NONFATAL, "`%s': context stack is empty", name);
1459 return NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001460 }
1461
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001462 name += 2;
1463 ctx = cstk;
1464 i = 0;
1465 while (ctx && *name == '$') {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001466 name++;
1467 i++;
1468 ctx = ctx->next;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001469 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001470 if (!ctx) {
1471 error(ERR_NONFATAL, "`%s': context stack is only"
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001472 " %d level%s deep", name, i, (i == 1 ? "" : "s"));
H. Peter Anvine2c80182005-01-15 22:15:51 +00001473 return NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001474 }
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001475
1476 if (namep)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001477 *namep = name;
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001478
Keith Kanios404589e2010-08-10 20:12:57 -05001479 if (!all_contexts)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001480 return ctx;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001481
Cyrill Gorcunovd90693c2010-08-11 20:31:46 +04001482 /*
1483 * NOTE: In 2.10 we will not need lookup in extarnal
1484 * contexts, so this is a gentle way to inform users
1485 * about their source code need to be updated
1486 */
1487
1488 /* first round -- check the current context */
1489 m = hash_findix(&ctx->localmac, name);
1490 while (m) {
1491 if (!mstrcmp(m->name, name, m->casesense))
1492 return ctx;
1493 m = m->next;
1494 }
1495
1496 /* second round - external contexts */
1497 while ((ctx = ctx->next)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001498 /* Search for this smacro in found context */
H. Peter Anvin166c2472008-05-28 12:28:58 -07001499 m = hash_findix(&ctx->localmac, name);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001500 while (m) {
Keith Kanios404589e2010-08-10 20:12:57 -05001501 if (!mstrcmp(m->name, name, m->casesense)) {
Keith Kanios88d947e2010-08-14 12:47:45 -05001502 /* NOTE: deprecated as of 2.10 */
Cyrill Gorcunovd90693c2010-08-11 20:31:46 +04001503 static int once = 0;
1504 if (!once) {
1505 error(ERR_WARNING, "context-local macro expansion"
Cyrill Gorcunov84b4cba2010-09-12 21:39:40 +04001506 " fall-through (automatic searching of outer"
1507 " contexts) will be deprecated starting in"
1508 " NASM 2.10, please see the NASM Manual for"
1509 " more information");
Cyrill Gorcunovd90693c2010-08-11 20:31:46 +04001510 once = 1;
1511 }
Keith Kanios88d947e2010-08-14 12:47:45 -05001512 error(ERR_WARNING, "`%s': context-local macro expansion fall-through", name);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001513 return ctx;
Cyrill Gorcunovd90693c2010-08-11 20:31:46 +04001514 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001515 m = m->next;
1516 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00001517 }
Cyrill Gorcunovd90693c2010-08-11 20:31:46 +04001518
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001519 return NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001520}
1521
1522/*
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001523 * Check to see if a file is already in a string list
1524 */
1525static bool in_list(const StrList *list, const char *str)
1526{
1527 while (list) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001528 if (!strcmp(list->str, str))
1529 return true;
1530 list = list->next;
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001531 }
1532 return false;
1533}
1534
1535/*
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001536 * Open an include file. This routine must always return a valid
1537 * file pointer if it returns - it's responsible for throwing an
1538 * ERR_FATAL and bombing out completely if not. It should also try
1539 * the include path one by one until it finds the file or reaches
1540 * the end of the path.
1541 */
H. Peter Anvin2b1c3b92008-06-06 10:38:46 -07001542static FILE *inc_fopen(const char *file, StrList **dhead, StrList ***dtail,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001543 bool missing_ok)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001544{
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001545 FILE *fp;
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001546 char *prefix = "";
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001547 IncPath *ip = ipath;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001548 int len = strlen(file);
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001549 size_t prefix_len = 0;
1550 StrList *sl;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001551
H. Peter Anvine2c80182005-01-15 22:15:51 +00001552 while (1) {
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001553 sl = nasm_malloc(prefix_len+len+1+sizeof sl->next);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001554 memcpy(sl->str, prefix, prefix_len);
1555 memcpy(sl->str+prefix_len, file, len+1);
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001556 fp = fopen(sl->str, "r");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001557 if (fp && dhead && !in_list(*dhead, sl->str)) {
1558 sl->next = NULL;
1559 **dtail = sl;
1560 *dtail = &sl->next;
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001561 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001562 nasm_free(sl);
1563 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001564 if (fp)
1565 return fp;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001566 if (!ip) {
1567 if (!missing_ok)
1568 break;
1569 prefix = NULL;
1570 } else {
1571 prefix = ip->path;
1572 ip = ip->next;
1573 }
1574 if (prefix) {
1575 prefix_len = strlen(prefix);
1576 } else {
1577 /* -MG given and file not found */
1578 if (dhead && !in_list(*dhead, file)) {
1579 sl = nasm_malloc(len+1+sizeof sl->next);
1580 sl->next = NULL;
1581 strcpy(sl->str, file);
1582 **dtail = sl;
1583 *dtail = &sl->next;
1584 }
1585 return NULL;
1586 }
H. Peter Anvineba20a72002-04-30 20:53:55 +00001587 }
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001588
H. Peter Anvin734b1882002-04-30 21:01:08 +00001589 error(ERR_FATAL, "unable to open include file `%s'", file);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001590 return NULL;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001591}
1592
1593/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001594 * Determine if we should warn on defining a single-line macro of
H. Peter Anvinef7468f2002-04-30 20:57:59 +00001595 * name `name', with `nparam' parameters. If nparam is 0 or -1, will
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001596 * return true if _any_ single-line macro of that name is defined.
1597 * Otherwise, will return true if a single-line macro with either
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001598 * `nparam' or no parameters is defined.
1599 *
1600 * If a macro with precisely the right number of parameters is
H. Peter Anvinef7468f2002-04-30 20:57:59 +00001601 * defined, or nparam is -1, the address of the definition structure
1602 * will be returned in `defn'; otherwise NULL will be returned. If `defn'
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001603 * is NULL, no action will be taken regarding its contents, and no
1604 * error will occur.
1605 *
1606 * Note that this is also called with nparam zero to resolve
1607 * `ifdef'.
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001608 *
1609 * If you already know which context macro belongs to, you can pass
1610 * the context pointer as first parameter; if you won't but name begins
1611 * with %$ the context will be automatically computed. If all_contexts
1612 * is true, macro will be searched in outer contexts as well.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001613 */
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07001614static bool
H. Peter Anvinb2a5fda2008-06-19 21:42:42 -07001615smacro_defined(Context * ctx, const char *name, int nparam, SMacro ** defn,
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07001616 bool nocase)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001617{
H. Peter Anvin166c2472008-05-28 12:28:58 -07001618 struct hash_table *smtbl;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001619 SMacro *m;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001620
H. Peter Anvin97a23472007-09-16 17:57:25 -07001621 if (ctx) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001622 smtbl = &ctx->localmac;
H. Peter Anvin97a23472007-09-16 17:57:25 -07001623 } else if (name[0] == '%' && name[1] == '$') {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001624 if (cstk)
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001625 ctx = get_ctx(name, &name, false);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001626 if (!ctx)
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001627 return false; /* got to return _something_ */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001628 smtbl = &ctx->localmac;
H. Peter Anvin97a23472007-09-16 17:57:25 -07001629 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001630 smtbl = &smacros;
H. Peter Anvin97a23472007-09-16 17:57:25 -07001631 }
H. Peter Anvin166c2472008-05-28 12:28:58 -07001632 m = (SMacro *) hash_findix(smtbl, name);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001633
H. Peter Anvine2c80182005-01-15 22:15:51 +00001634 while (m) {
1635 if (!mstrcmp(m->name, name, m->casesense && nocase) &&
Charles Crayne192d5b52007-10-18 19:02:42 -07001636 (nparam <= 0 || m->nparam == 0 || nparam == (int) m->nparam)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001637 if (defn) {
Charles Crayne192d5b52007-10-18 19:02:42 -07001638 if (nparam == (int) m->nparam || nparam == -1)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001639 *defn = m;
1640 else
1641 *defn = NULL;
1642 }
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001643 return true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001644 }
1645 m = m->next;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001646 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001647
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001648 return false;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001649}
1650
1651/*
1652 * Count and mark off the parameters in a multi-line macro call.
1653 * This is called both from within the multi-line macro expansion
1654 * code, and also to mark off the default parameters when provided
1655 * in a %macro definition line.
1656 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001657static void count_mmac_params(Token * t, int *nparam, Token *** params)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001658{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001659 int paramsize, brace;
1660
1661 *nparam = paramsize = 0;
1662 *params = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001663 while (t) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001664 /* +1: we need space for the final NULL */
H. Peter Anvin91fb6f12008-09-01 10:56:33 -07001665 if (*nparam+1 >= paramsize) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001666 paramsize += PARAM_DELTA;
1667 *params = nasm_realloc(*params, sizeof(**params) * paramsize);
1668 }
1669 skip_white_(t);
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001670 brace = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001671 if (tok_is_(t, "{"))
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001672 brace = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001673 (*params)[(*nparam)++] = t;
1674 while (tok_isnt_(t, brace ? "}" : ","))
1675 t = t->next;
1676 if (t) { /* got a comma/brace */
1677 t = t->next;
1678 if (brace) {
1679 /*
1680 * Now we've found the closing brace, look further
1681 * for the comma.
1682 */
1683 skip_white_(t);
1684 if (tok_isnt_(t, ",")) {
1685 error(ERR_NONFATAL,
1686 "braces do not enclose all of macro parameter");
1687 while (tok_isnt_(t, ","))
1688 t = t->next;
1689 }
1690 if (t)
1691 t = t->next; /* eat the comma */
1692 }
1693 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001694 }
1695}
1696
1697/*
H. Peter Anvin76690a12002-04-30 20:52:49 +00001698 * Determine whether one of the various `if' conditions is true or
1699 * not.
1700 *
1701 * We must free the tline we get passed.
1702 */
H. Peter Anvin70055962007-10-11 00:05:31 -07001703static bool if_condition(Token * tline, enum preproc_token ct)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001704{
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001705 enum pp_conditional i = PP_COND(ct);
H. Peter Anvin70055962007-10-11 00:05:31 -07001706 bool j;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001707 Token *t, *tt, **tptr, *origline;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001708 struct tokenval tokval;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001709 expr *evalresult;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001710 enum pp_token_type needtype;
H. Peter Anvin077fb932010-07-20 14:56:30 -07001711 char *p;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001712
1713 origline = tline;
1714
H. Peter Anvine2c80182005-01-15 22:15:51 +00001715 switch (i) {
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001716 case PPC_IFCTX:
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001717 j = false; /* have we matched yet? */
Victor van den Elzen0e857f12008-07-23 13:21:29 +02001718 while (true) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001719 skip_white_(tline);
Victor van den Elzen0e857f12008-07-23 13:21:29 +02001720 if (!tline)
1721 break;
1722 if (tline->type != TOK_ID) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001723 error(ERR_NONFATAL,
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001724 "`%s' expects context identifiers", pp_directives[ct]);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001725 free_tlist(origline);
1726 return -1;
1727 }
Victor van den Elzen0e857f12008-07-23 13:21:29 +02001728 if (cstk && cstk->name && !nasm_stricmp(tline->text, cstk->name))
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001729 j = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001730 tline = tline->next;
1731 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001732 break;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001733
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001734 case PPC_IFDEF:
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001735 j = false; /* have we matched yet? */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001736 while (tline) {
1737 skip_white_(tline);
1738 if (!tline || (tline->type != TOK_ID &&
1739 (tline->type != TOK_PREPROC_ID ||
1740 tline->text[1] != '$'))) {
1741 error(ERR_NONFATAL,
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001742 "`%s' expects macro identifiers", pp_directives[ct]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001743 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001744 }
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07001745 if (smacro_defined(NULL, tline->text, 0, NULL, true))
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001746 j = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001747 tline = tline->next;
1748 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001749 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001750
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001751 case PPC_IFENV:
Cyrill Gorcunov84b4cba2010-09-12 21:39:40 +04001752 tline = expand_smacro(tline);
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001753 j = false; /* have we matched yet? */
1754 while (tline) {
1755 skip_white_(tline);
1756 if (!tline || (tline->type != TOK_ID &&
Cyrill Gorcunov84b4cba2010-09-12 21:39:40 +04001757 tline->type != TOK_STRING &&
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001758 (tline->type != TOK_PREPROC_ID ||
Cyrill Gorcunov84b4cba2010-09-12 21:39:40 +04001759 tline->text[1] != '!'))) {
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001760 error(ERR_NONFATAL,
1761 "`%s' expects environment variable names",
Cyrill Gorcunov84b4cba2010-09-12 21:39:40 +04001762 pp_directives[ct]);
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001763 goto fail;
1764 }
Cyrill Gorcunov84b4cba2010-09-12 21:39:40 +04001765 p = tline->text;
1766 if (tline->type == TOK_PREPROC_ID)
1767 p += 2; /* Skip leading %! */
1768 if (*p == '\'' || *p == '\"' || *p == '`')
1769 nasm_unquote_cstr(p, ct);
1770 if (getenv(p))
1771 j = true;
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001772 tline = tline->next;
1773 }
1774 break;
1775
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001776 case PPC_IFIDN:
1777 case PPC_IFIDNI:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001778 tline = expand_smacro(tline);
1779 t = tt = tline;
1780 while (tok_isnt_(tt, ","))
1781 tt = tt->next;
1782 if (!tt) {
1783 error(ERR_NONFATAL,
1784 "`%s' expects two comma-separated arguments",
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001785 pp_directives[ct]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001786 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001787 }
1788 tt = tt->next;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001789 j = true; /* assume equality unless proved not */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001790 while ((t->type != TOK_OTHER || strcmp(t->text, ",")) && tt) {
1791 if (tt->type == TOK_OTHER && !strcmp(tt->text, ",")) {
1792 error(ERR_NONFATAL, "`%s': more than one comma on line",
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001793 pp_directives[ct]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001794 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001795 }
1796 if (t->type == TOK_WHITESPACE) {
1797 t = t->next;
1798 continue;
1799 }
1800 if (tt->type == TOK_WHITESPACE) {
1801 tt = tt->next;
1802 continue;
1803 }
1804 if (tt->type != t->type) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001805 j = false; /* found mismatching tokens */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001806 break;
1807 }
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07001808 /* When comparing strings, need to unquote them first */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001809 if (t->type == TOK_STRING) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001810 size_t l1 = nasm_unquote(t->text, NULL);
1811 size_t l2 = nasm_unquote(tt->text, NULL);
H. Peter Anvind2456592008-06-19 15:04:18 -07001812
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001813 if (l1 != l2) {
1814 j = false;
1815 break;
1816 }
1817 if (mmemcmp(t->text, tt->text, l1, i == PPC_IFIDN)) {
1818 j = false;
1819 break;
1820 }
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07001821 } else if (mstrcmp(tt->text, t->text, i == PPC_IFIDN) != 0) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001822 j = false; /* found mismatching tokens */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001823 break;
1824 }
Nickolay Yurchenkof3b3ce22003-09-21 20:38:43 +00001825
H. Peter Anvine2c80182005-01-15 22:15:51 +00001826 t = t->next;
1827 tt = tt->next;
1828 }
1829 if ((t->type != TOK_OTHER || strcmp(t->text, ",")) || tt)
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001830 j = false; /* trailing gunk on one end or other */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001831 break;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001832
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001833 case PPC_IFMACRO:
H. Peter Anvin89cee572009-07-15 09:16:54 -04001834 {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001835 bool found = false;
1836 MMacro searching, *mmac;
H. Peter Anvin65747262002-05-07 00:10:05 +00001837
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001838 skip_white_(tline);
1839 tline = expand_id(tline);
1840 if (!tok_type_(tline, TOK_ID)) {
1841 error(ERR_NONFATAL,
1842 "`%s' expects a macro name", pp_directives[ct]);
1843 goto fail;
1844 }
1845 searching.name = nasm_strdup(tline->text);
1846 searching.casesense = true;
1847 searching.plus = false;
1848 searching.nolist = false;
1849 searching.in_progress = 0;
1850 searching.max_depth = 0;
1851 searching.rep_nest = NULL;
1852 searching.nparam_min = 0;
1853 searching.nparam_max = INT_MAX;
1854 tline = expand_smacro(tline->next);
1855 skip_white_(tline);
1856 if (!tline) {
1857 } else if (!tok_type_(tline, TOK_NUMBER)) {
1858 error(ERR_NONFATAL,
1859 "`%s' expects a parameter count or nothing",
1860 pp_directives[ct]);
1861 } else {
1862 searching.nparam_min = searching.nparam_max =
1863 readnum(tline->text, &j);
1864 if (j)
1865 error(ERR_NONFATAL,
1866 "unable to parse parameter count `%s'",
1867 tline->text);
1868 }
1869 if (tline && tok_is_(tline->next, "-")) {
1870 tline = tline->next->next;
1871 if (tok_is_(tline, "*"))
1872 searching.nparam_max = INT_MAX;
1873 else if (!tok_type_(tline, TOK_NUMBER))
1874 error(ERR_NONFATAL,
1875 "`%s' expects a parameter count after `-'",
1876 pp_directives[ct]);
1877 else {
1878 searching.nparam_max = readnum(tline->text, &j);
1879 if (j)
1880 error(ERR_NONFATAL,
1881 "unable to parse parameter count `%s'",
1882 tline->text);
1883 if (searching.nparam_min > searching.nparam_max)
1884 error(ERR_NONFATAL,
1885 "minimum parameter count exceeds maximum");
1886 }
1887 }
1888 if (tline && tok_is_(tline->next, "+")) {
1889 tline = tline->next;
1890 searching.plus = true;
1891 }
1892 mmac = (MMacro *) hash_findix(&mmacros, searching.name);
1893 while (mmac) {
1894 if (!strcmp(mmac->name, searching.name) &&
1895 (mmac->nparam_min <= searching.nparam_max
1896 || searching.plus)
1897 && (searching.nparam_min <= mmac->nparam_max
1898 || mmac->plus)) {
1899 found = true;
1900 break;
1901 }
1902 mmac = mmac->next;
1903 }
1904 if (tline && tline->next)
1905 error(ERR_WARNING|ERR_PASS1,
1906 "trailing garbage after %%ifmacro ignored");
1907 nasm_free(searching.name);
1908 j = found;
1909 break;
H. Peter Anvin89cee572009-07-15 09:16:54 -04001910 }
H. Peter Anvin65747262002-05-07 00:10:05 +00001911
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001912 case PPC_IFID:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001913 needtype = TOK_ID;
1914 goto iftype;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001915 case PPC_IFNUM:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001916 needtype = TOK_NUMBER;
1917 goto iftype;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001918 case PPC_IFSTR:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001919 needtype = TOK_STRING;
1920 goto iftype;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001921
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001922iftype:
1923 t = tline = expand_smacro(tline);
H. Peter Anvind85d2502008-05-04 17:53:31 -07001924
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001925 while (tok_type_(t, TOK_WHITESPACE) ||
1926 (needtype == TOK_NUMBER &&
1927 tok_type_(t, TOK_OTHER) &&
1928 (t->text[0] == '-' || t->text[0] == '+') &&
1929 !t->text[1]))
1930 t = t->next;
H. Peter Anvind85d2502008-05-04 17:53:31 -07001931
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001932 j = tok_type_(t, needtype);
1933 break;
H. Peter Anvincbf768d2008-02-16 16:41:25 -08001934
1935 case PPC_IFTOKEN:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001936 t = tline = expand_smacro(tline);
1937 while (tok_type_(t, TOK_WHITESPACE))
1938 t = t->next;
H. Peter Anvincbf768d2008-02-16 16:41:25 -08001939
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001940 j = false;
1941 if (t) {
1942 t = t->next; /* Skip the actual token */
1943 while (tok_type_(t, TOK_WHITESPACE))
1944 t = t->next;
1945 j = !t; /* Should be nothing left */
1946 }
1947 break;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001948
H. Peter Anvin134b9462008-02-16 17:01:40 -08001949 case PPC_IFEMPTY:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001950 t = tline = expand_smacro(tline);
1951 while (tok_type_(t, TOK_WHITESPACE))
1952 t = t->next;
H. Peter Anvin134b9462008-02-16 17:01:40 -08001953
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001954 j = !t; /* Should be empty */
1955 break;
H. Peter Anvin134b9462008-02-16 17:01:40 -08001956
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001957 case PPC_IF:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001958 t = tline = expand_smacro(tline);
1959 tptr = &t;
1960 tokval.t_type = TOKEN_INVALID;
1961 evalresult = evaluate(ppscan, tptr, &tokval,
1962 NULL, pass | CRITICAL, error, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001963 if (!evalresult)
1964 return -1;
1965 if (tokval.t_type)
H. Peter Anvin917a3492008-09-24 09:14:49 -07001966 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +00001967 "trailing garbage after expression ignored");
1968 if (!is_simple(evalresult)) {
1969 error(ERR_NONFATAL,
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001970 "non-constant value given to `%s'", pp_directives[ct]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001971 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001972 }
Chuck Crayne60ae75d2007-05-02 01:59:16 +00001973 j = reloc_value(evalresult) != 0;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001974 break;
H. Peter Anvin95e28822007-09-12 04:20:08 +00001975
H. Peter Anvine2c80182005-01-15 22:15:51 +00001976 default:
1977 error(ERR_FATAL,
1978 "preprocessor directive `%s' not yet implemented",
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001979 pp_directives[ct]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001980 goto fail;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001981 }
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001982
1983 free_tlist(origline);
1984 return j ^ PP_NEGATIVE(ct);
H. Peter Anvin70653092007-10-19 14:42:29 -07001985
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001986fail:
1987 free_tlist(origline);
1988 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001989}
1990
1991/*
H. Peter Anvin4db5a162007-10-11 13:42:09 -07001992 * Common code for defining an smacro
1993 */
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001994static bool define_smacro(Context *ctx, const char *mname, bool casesense,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001995 int nparam, Token *expansion)
H. Peter Anvin4db5a162007-10-11 13:42:09 -07001996{
1997 SMacro *smac, **smhead;
H. Peter Anvin166c2472008-05-28 12:28:58 -07001998 struct hash_table *smtbl;
H. Peter Anvin70653092007-10-19 14:42:29 -07001999
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002000 if (smacro_defined(ctx, mname, nparam, &smac, casesense)) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002001 if (!smac) {
2002 error(ERR_WARNING|ERR_PASS1,
2003 "single-line macro `%s' defined both with and"
2004 " without parameters", mname);
2005 /*
2006 * Some instances of the old code considered this a failure,
2007 * some others didn't. What is the right thing to do here?
2008 */
2009 free_tlist(expansion);
2010 return false; /* Failure */
2011 } else {
2012 /*
2013 * We're redefining, so we have to take over an
2014 * existing SMacro structure. This means freeing
2015 * what was already in it.
2016 */
2017 nasm_free(smac->name);
2018 free_tlist(smac->expansion);
2019 }
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002020 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002021 smtbl = ctx ? &ctx->localmac : &smacros;
2022 smhead = (SMacro **) hash_findi_add(smtbl, mname);
2023 smac = nasm_malloc(sizeof(SMacro));
2024 smac->next = *smhead;
2025 *smhead = smac;
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002026 }
2027 smac->name = nasm_strdup(mname);
2028 smac->casesense = casesense;
2029 smac->nparam = nparam;
2030 smac->expansion = expansion;
2031 smac->in_progress = false;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002032 return true; /* Success */
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002033}
2034
2035/*
2036 * Undefine an smacro
2037 */
2038static void undef_smacro(Context *ctx, const char *mname)
2039{
2040 SMacro **smhead, *s, **sp;
H. Peter Anvin166c2472008-05-28 12:28:58 -07002041 struct hash_table *smtbl;
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002042
H. Peter Anvin166c2472008-05-28 12:28:58 -07002043 smtbl = ctx ? &ctx->localmac : &smacros;
2044 smhead = (SMacro **)hash_findi(smtbl, mname, NULL);
H. Peter Anvin70653092007-10-19 14:42:29 -07002045
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002046 if (smhead) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002047 /*
2048 * We now have a macro name... go hunt for it.
2049 */
2050 sp = smhead;
2051 while ((s = *sp) != NULL) {
2052 if (!mstrcmp(s->name, mname, s->casesense)) {
2053 *sp = s->next;
2054 nasm_free(s->name);
2055 free_tlist(s->expansion);
2056 nasm_free(s);
2057 } else {
2058 sp = &s->next;
2059 }
2060 }
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002061 }
2062}
2063
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002064/*
H. Peter Anvina26433d2008-07-16 14:40:01 -07002065 * Parse a mmacro specification.
2066 */
2067static bool parse_mmacro_spec(Token *tline, MMacro *def, const char *directive)
2068{
2069 bool err;
2070
2071 tline = tline->next;
2072 skip_white_(tline);
2073 tline = expand_id(tline);
2074 if (!tok_type_(tline, TOK_ID)) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002075 error(ERR_NONFATAL, "`%s' expects a macro name", directive);
2076 return false;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002077 }
Victor van den Elzenb916ede2008-07-23 15:14:22 +02002078
H. Peter Anvin89cee572009-07-15 09:16:54 -04002079 def->prev = NULL;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002080 def->name = nasm_strdup(tline->text);
2081 def->plus = false;
2082 def->nolist = false;
2083 def->in_progress = 0;
2084 def->rep_nest = NULL;
Victor van den Elzenb916ede2008-07-23 15:14:22 +02002085 def->nparam_min = 0;
2086 def->nparam_max = 0;
2087
H. Peter Anvina26433d2008-07-16 14:40:01 -07002088 tline = expand_smacro(tline->next);
2089 skip_white_(tline);
2090 if (!tok_type_(tline, TOK_NUMBER)) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002091 error(ERR_NONFATAL, "`%s' expects a parameter count", directive);
H. Peter Anvina26433d2008-07-16 14:40:01 -07002092 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002093 def->nparam_min = def->nparam_max =
2094 readnum(tline->text, &err);
2095 if (err)
2096 error(ERR_NONFATAL,
2097 "unable to parse parameter count `%s'", tline->text);
H. Peter Anvina26433d2008-07-16 14:40:01 -07002098 }
2099 if (tline && tok_is_(tline->next, "-")) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002100 tline = tline->next->next;
2101 if (tok_is_(tline, "*")) {
2102 def->nparam_max = INT_MAX;
2103 } else if (!tok_type_(tline, TOK_NUMBER)) {
2104 error(ERR_NONFATAL,
2105 "`%s' expects a parameter count after `-'", directive);
2106 } else {
2107 def->nparam_max = readnum(tline->text, &err);
2108 if (err) {
2109 error(ERR_NONFATAL, "unable to parse parameter count `%s'",
2110 tline->text);
2111 }
2112 if (def->nparam_min > def->nparam_max) {
2113 error(ERR_NONFATAL, "minimum parameter count exceeds maximum");
2114 }
2115 }
H. Peter Anvina26433d2008-07-16 14:40:01 -07002116 }
2117 if (tline && tok_is_(tline->next, "+")) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002118 tline = tline->next;
2119 def->plus = true;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002120 }
2121 if (tline && tok_type_(tline->next, TOK_ID) &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002122 !nasm_stricmp(tline->next->text, ".nolist")) {
2123 tline = tline->next;
2124 def->nolist = true;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002125 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002126
H. Peter Anvina26433d2008-07-16 14:40:01 -07002127 /*
2128 * Handle default parameters.
2129 */
2130 if (tline && tline->next) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002131 def->dlist = tline->next;
2132 tline->next = NULL;
2133 count_mmac_params(def->dlist, &def->ndefs, &def->defaults);
H. Peter Anvina26433d2008-07-16 14:40:01 -07002134 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002135 def->dlist = NULL;
2136 def->defaults = NULL;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002137 }
2138 def->expansion = NULL;
2139
H. Peter Anvin89cee572009-07-15 09:16:54 -04002140 if (def->defaults && def->ndefs > def->nparam_max - def->nparam_min &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002141 !def->plus)
2142 error(ERR_WARNING|ERR_PASS1|ERR_WARN_MDP,
2143 "too many default macro parameters");
Victor van den Elzenb916ede2008-07-23 15:14:22 +02002144
H. Peter Anvina26433d2008-07-16 14:40:01 -07002145 return true;
2146}
2147
2148
2149/*
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002150 * Decode a size directive
2151 */
2152static int parse_size(const char *str) {
2153 static const char *size_names[] =
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002154 { "byte", "dword", "oword", "qword", "tword", "word", "yword" };
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002155 static const int sizes[] =
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002156 { 0, 1, 4, 16, 8, 10, 2, 32 };
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002157
Cyrill Gorcunova7319242010-06-03 22:04:36 +04002158 return sizes[bsii(str, size_names, ARRAY_SIZE(size_names))+1];
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002159}
2160
Ed Beroset3ab3f412002-06-11 03:31:49 +00002161/**
2162 * find and process preprocessor directive in passed line
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002163 * Find out if a line contains a preprocessor directive, and deal
2164 * with it if so.
H. Peter Anvin70653092007-10-19 14:42:29 -07002165 *
Ed Beroset3ab3f412002-06-11 03:31:49 +00002166 * If a directive _is_ found, it is the responsibility of this routine
2167 * (and not the caller) to free_tlist() the line.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002168 *
Ed Beroset3ab3f412002-06-11 03:31:49 +00002169 * @param tline a pointer to the current tokeninzed line linked list
2170 * @return DIRECTIVE_FOUND or NO_DIRECTIVE_FOUND
H. Peter Anvin70653092007-10-19 14:42:29 -07002171 *
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002172 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002173static int do_directive(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00002174{
H. Peter Anvin4169a472007-09-12 01:29:43 +00002175 enum preproc_token i;
2176 int j;
H. Peter Anvin70055962007-10-11 00:05:31 -07002177 bool err;
2178 int nparam;
2179 bool nolist;
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07002180 bool casesense;
H. Peter Anvin8cfdb9d2007-09-14 18:36:01 -07002181 int k, m;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002182 int offset;
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08002183 char *p, *pp;
2184 const char *mname;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002185 Include *inc;
2186 Context *ctx;
2187 Cond *cond;
H. Peter Anvin97a23472007-09-16 17:57:25 -07002188 MMacro *mmac, **mmhead;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002189 Token *t, *tt, *param_start, *macro_start, *last, **tptr, *origline;
2190 Line *l;
2191 struct tokenval tokval;
2192 expr *evalresult;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002193 MMacro *tmp_defining; /* Used when manipulating rep_nest */
H. Peter Anvinf8ba53e2007-10-11 10:11:57 -07002194 int64_t count;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07002195 size_t len;
H. Peter Anvin8e3f75e2008-09-24 00:21:58 -07002196 int severity;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002197
2198 origline = tline;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002199
H. Peter Anvineba20a72002-04-30 20:53:55 +00002200 skip_white_(tline);
H. Peter Anvinf2936d72008-06-04 15:11:23 -07002201 if (!tline || !tok_type_(tline, TOK_PREPROC_ID) ||
H. Peter Anvine2c80182005-01-15 22:15:51 +00002202 (tline->text[1] == '%' || tline->text[1] == '$'
2203 || tline->text[1] == '!'))
2204 return NO_DIRECTIVE_FOUND;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002205
H. Peter Anvin4169a472007-09-12 01:29:43 +00002206 i = pp_token_hash(tline->text);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002207
2208 /*
Cyrill Gorcunovf09116f2010-02-28 11:52:53 +03002209 * FIXME: We zap execution of PP_RMACRO, PP_IRMACRO, PP_EXITMACRO
2210 * since they are known to be buggy at moment, we need to fix them
2211 * in future release (2.09-2.10)
2212 */
2213 if (i == PP_RMACRO || i == PP_RMACRO || i == PP_EXITMACRO) {
2214 error(ERR_NONFATAL, "unknown preprocessor directive `%s'",
2215 tline->text);
2216 return NO_DIRECTIVE_FOUND;
2217 }
2218
2219 /*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002220 * If we're in a non-emitting branch of a condition construct,
H. Peter Anvin76690a12002-04-30 20:52:49 +00002221 * or walking to the end of an already terminated %rep block,
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002222 * we should ignore all directives except for condition
2223 * directives.
2224 */
H. Peter Anvin76690a12002-04-30 20:52:49 +00002225 if (((istk->conds && !emitting(istk->conds->state)) ||
H. Peter Anvine2c80182005-01-15 22:15:51 +00002226 (istk->mstk && !istk->mstk->in_progress)) && !is_condition(i)) {
2227 return NO_DIRECTIVE_FOUND;
H. Peter Anvineba20a72002-04-30 20:53:55 +00002228 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002229
2230 /*
H. Peter Anvin76690a12002-04-30 20:52:49 +00002231 * If we're defining a macro or reading a %rep block, we should
Victor van den Elzen8f1120f2008-07-16 13:41:37 +02002232 * ignore all directives except for %macro/%imacro (which nest),
2233 * %endm/%endmacro, and (only if we're in a %rep block) %endrep.
2234 * If we're in a %rep block, another %rep nests, so should be let through.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002235 */
2236 if (defining && i != PP_MACRO && i != PP_IMACRO &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002237 i != PP_RMACRO && i != PP_IRMACRO &&
H. Peter Anvine2c80182005-01-15 22:15:51 +00002238 i != PP_ENDMACRO && i != PP_ENDM &&
2239 (defining->name || (i != PP_ENDREP && i != PP_REP))) {
2240 return NO_DIRECTIVE_FOUND;
H. Peter Anvineba20a72002-04-30 20:53:55 +00002241 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002242
Charles Crayned4200be2008-07-12 16:42:33 -07002243 if (defining) {
Keith Kanios852f1ee2009-07-12 00:19:55 -05002244 if (i == PP_MACRO || i == PP_IMACRO ||
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002245 i == PP_RMACRO || i == PP_IRMACRO) {
Charles Crayned4200be2008-07-12 16:42:33 -07002246 nested_mac_count++;
2247 return NO_DIRECTIVE_FOUND;
2248 } else if (nested_mac_count > 0) {
2249 if (i == PP_ENDMACRO) {
2250 nested_mac_count--;
2251 return NO_DIRECTIVE_FOUND;
2252 }
2253 }
2254 if (!defining->name) {
2255 if (i == PP_REP) {
2256 nested_rep_count++;
2257 return NO_DIRECTIVE_FOUND;
2258 } else if (nested_rep_count > 0) {
2259 if (i == PP_ENDREP) {
2260 nested_rep_count--;
2261 return NO_DIRECTIVE_FOUND;
2262 }
2263 }
2264 }
2265 }
2266
H. Peter Anvin4169a472007-09-12 01:29:43 +00002267 switch (i) {
2268 case PP_INVALID:
H. Peter Anvine2c80182005-01-15 22:15:51 +00002269 error(ERR_NONFATAL, "unknown preprocessor directive `%s'",
2270 tline->text);
2271 return NO_DIRECTIVE_FOUND; /* didn't get it */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002272
H. Peter Anvine2c80182005-01-15 22:15:51 +00002273 case PP_STACKSIZE:
2274 /* Directive to tell NASM what the default stack size is. The
2275 * default is for a 16-bit stack, and this can be overriden with
2276 * %stacksize large.
H. Peter Anvine2c80182005-01-15 22:15:51 +00002277 */
2278 tline = tline->next;
2279 if (tline && tline->type == TOK_WHITESPACE)
2280 tline = tline->next;
2281 if (!tline || tline->type != TOK_ID) {
2282 error(ERR_NONFATAL, "`%%stacksize' missing size parameter");
2283 free_tlist(origline);
2284 return DIRECTIVE_FOUND;
2285 }
2286 if (nasm_stricmp(tline->text, "flat") == 0) {
2287 /* All subsequent ARG directives are for a 32-bit stack */
2288 StackSize = 4;
2289 StackPointer = "ebp";
2290 ArgOffset = 8;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002291 LocalOffset = 0;
Charles Crayne7eaf9192007-11-08 22:11:14 -08002292 } else if (nasm_stricmp(tline->text, "flat64") == 0) {
2293 /* All subsequent ARG directives are for a 64-bit stack */
2294 StackSize = 8;
2295 StackPointer = "rbp";
Per Jessen53252e02010-02-11 00:16:59 +03002296 ArgOffset = 16;
Charles Crayne7eaf9192007-11-08 22:11:14 -08002297 LocalOffset = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002298 } else if (nasm_stricmp(tline->text, "large") == 0) {
2299 /* All subsequent ARG directives are for a 16-bit stack,
2300 * far function call.
2301 */
2302 StackSize = 2;
2303 StackPointer = "bp";
2304 ArgOffset = 4;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002305 LocalOffset = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002306 } else if (nasm_stricmp(tline->text, "small") == 0) {
2307 /* All subsequent ARG directives are for a 16-bit stack,
2308 * far function call. We don't support near functions.
2309 */
2310 StackSize = 2;
2311 StackPointer = "bp";
2312 ArgOffset = 6;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002313 LocalOffset = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002314 } else {
2315 error(ERR_NONFATAL, "`%%stacksize' invalid size type");
2316 free_tlist(origline);
2317 return DIRECTIVE_FOUND;
2318 }
2319 free_tlist(origline);
2320 return DIRECTIVE_FOUND;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002321
H. Peter Anvine2c80182005-01-15 22:15:51 +00002322 case PP_ARG:
2323 /* TASM like ARG directive to define arguments to functions, in
2324 * the following form:
2325 *
2326 * ARG arg1:WORD, arg2:DWORD, arg4:QWORD
2327 */
2328 offset = ArgOffset;
2329 do {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00002330 char *arg, directive[256];
H. Peter Anvine2c80182005-01-15 22:15:51 +00002331 int size = StackSize;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002332
H. Peter Anvine2c80182005-01-15 22:15:51 +00002333 /* Find the argument name */
2334 tline = tline->next;
2335 if (tline && tline->type == TOK_WHITESPACE)
2336 tline = tline->next;
2337 if (!tline || tline->type != TOK_ID) {
2338 error(ERR_NONFATAL, "`%%arg' missing argument parameter");
2339 free_tlist(origline);
2340 return DIRECTIVE_FOUND;
2341 }
2342 arg = tline->text;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002343
H. Peter Anvine2c80182005-01-15 22:15:51 +00002344 /* Find the argument size type */
2345 tline = tline->next;
2346 if (!tline || tline->type != TOK_OTHER
2347 || tline->text[0] != ':') {
2348 error(ERR_NONFATAL,
2349 "Syntax error processing `%%arg' directive");
2350 free_tlist(origline);
2351 return DIRECTIVE_FOUND;
2352 }
2353 tline = tline->next;
2354 if (!tline || tline->type != TOK_ID) {
2355 error(ERR_NONFATAL, "`%%arg' missing size type parameter");
2356 free_tlist(origline);
2357 return DIRECTIVE_FOUND;
2358 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002359
H. Peter Anvine2c80182005-01-15 22:15:51 +00002360 /* Allow macro expansion of type parameter */
Keith Kaniosb7a89542007-04-12 02:40:54 +00002361 tt = tokenize(tline->text);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002362 tt = expand_smacro(tt);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002363 size = parse_size(tt->text);
2364 if (!size) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002365 error(ERR_NONFATAL,
2366 "Invalid size type for `%%arg' missing directive");
2367 free_tlist(tt);
2368 free_tlist(origline);
2369 return DIRECTIVE_FOUND;
2370 }
2371 free_tlist(tt);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002372
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002373 /* Round up to even stack slots */
2374 size = ALIGN(size, StackSize);
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002375
H. Peter Anvine2c80182005-01-15 22:15:51 +00002376 /* Now define the macro for the argument */
2377 snprintf(directive, sizeof(directive), "%%define %s (%s+%d)",
2378 arg, StackPointer, offset);
Keith Kaniosb7a89542007-04-12 02:40:54 +00002379 do_directive(tokenize(directive));
H. Peter Anvine2c80182005-01-15 22:15:51 +00002380 offset += size;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002381
H. Peter Anvine2c80182005-01-15 22:15:51 +00002382 /* Move to the next argument in the list */
2383 tline = tline->next;
2384 if (tline && tline->type == TOK_WHITESPACE)
2385 tline = tline->next;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002386 } while (tline && tline->type == TOK_OTHER && tline->text[0] == ',');
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002387 ArgOffset = offset;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002388 free_tlist(origline);
2389 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002390
H. Peter Anvine2c80182005-01-15 22:15:51 +00002391 case PP_LOCAL:
2392 /* TASM like LOCAL directive to define local variables for a
2393 * function, in the following form:
2394 *
2395 * LOCAL local1:WORD, local2:DWORD, local4:QWORD = LocalSize
2396 *
2397 * The '= LocalSize' at the end is ignored by NASM, but is
2398 * required by TASM to define the local parameter size (and used
2399 * by the TASM macro package).
2400 */
2401 offset = LocalOffset;
2402 do {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00002403 char *local, directive[256];
H. Peter Anvine2c80182005-01-15 22:15:51 +00002404 int size = StackSize;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002405
H. Peter Anvine2c80182005-01-15 22:15:51 +00002406 /* Find the argument name */
2407 tline = tline->next;
2408 if (tline && tline->type == TOK_WHITESPACE)
2409 tline = tline->next;
2410 if (!tline || tline->type != TOK_ID) {
2411 error(ERR_NONFATAL,
2412 "`%%local' missing argument parameter");
2413 free_tlist(origline);
2414 return DIRECTIVE_FOUND;
2415 }
2416 local = tline->text;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002417
H. Peter Anvine2c80182005-01-15 22:15:51 +00002418 /* Find the argument size type */
2419 tline = tline->next;
2420 if (!tline || tline->type != TOK_OTHER
2421 || tline->text[0] != ':') {
2422 error(ERR_NONFATAL,
2423 "Syntax error processing `%%local' directive");
2424 free_tlist(origline);
2425 return DIRECTIVE_FOUND;
2426 }
2427 tline = tline->next;
2428 if (!tline || tline->type != TOK_ID) {
2429 error(ERR_NONFATAL,
2430 "`%%local' missing size type parameter");
2431 free_tlist(origline);
2432 return DIRECTIVE_FOUND;
2433 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00002434
H. Peter Anvine2c80182005-01-15 22:15:51 +00002435 /* Allow macro expansion of type parameter */
Keith Kaniosb7a89542007-04-12 02:40:54 +00002436 tt = tokenize(tline->text);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002437 tt = expand_smacro(tt);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002438 size = parse_size(tt->text);
2439 if (!size) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002440 error(ERR_NONFATAL,
2441 "Invalid size type for `%%local' missing directive");
2442 free_tlist(tt);
2443 free_tlist(origline);
2444 return DIRECTIVE_FOUND;
2445 }
2446 free_tlist(tt);
H. Peter Anvin734b1882002-04-30 21:01:08 +00002447
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002448 /* Round up to even stack slots */
2449 size = ALIGN(size, StackSize);
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002450
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002451 offset += size; /* Negative offset, increment before */
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002452
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002453 /* Now define the macro for the argument */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002454 snprintf(directive, sizeof(directive), "%%define %s (%s-%d)",
2455 local, StackPointer, offset);
Keith Kaniosb7a89542007-04-12 02:40:54 +00002456 do_directive(tokenize(directive));
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002457
H. Peter Anvine2c80182005-01-15 22:15:51 +00002458 /* Now define the assign to setup the enter_c macro correctly */
2459 snprintf(directive, sizeof(directive),
2460 "%%assign %%$localsize %%$localsize+%d", size);
Keith Kaniosb7a89542007-04-12 02:40:54 +00002461 do_directive(tokenize(directive));
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002462
H. Peter Anvine2c80182005-01-15 22:15:51 +00002463 /* Move to the next argument in the list */
2464 tline = tline->next;
2465 if (tline && tline->type == TOK_WHITESPACE)
2466 tline = tline->next;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002467 } while (tline && tline->type == TOK_OTHER && tline->text[0] == ',');
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002468 LocalOffset = offset;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002469 free_tlist(origline);
2470 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002471
H. Peter Anvine2c80182005-01-15 22:15:51 +00002472 case PP_CLEAR:
2473 if (tline->next)
H. Peter Anvin917a3492008-09-24 09:14:49 -07002474 error(ERR_WARNING|ERR_PASS1,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002475 "trailing garbage after `%%clear' ignored");
2476 free_macros();
2477 init_macros();
H. Peter Anvine2c80182005-01-15 22:15:51 +00002478 free_tlist(origline);
2479 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002480
H. Peter Anvin418ca702008-05-30 10:42:30 -07002481 case PP_DEPEND:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002482 t = tline->next = expand_smacro(tline->next);
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002483 skip_white_(t);
2484 if (!t || (t->type != TOK_STRING &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002485 t->type != TOK_INTERNAL_STRING)) {
H. Peter Anvin418ca702008-05-30 10:42:30 -07002486 error(ERR_NONFATAL, "`%%depend' expects a file name");
2487 free_tlist(origline);
2488 return DIRECTIVE_FOUND; /* but we did _something_ */
2489 }
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002490 if (t->next)
H. Peter Anvin917a3492008-09-24 09:14:49 -07002491 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvin418ca702008-05-30 10:42:30 -07002492 "trailing garbage after `%%depend' ignored");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002493 p = t->text;
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002494 if (t->type != TOK_INTERNAL_STRING)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002495 nasm_unquote_cstr(p, i);
2496 if (dephead && !in_list(*dephead, p)) {
2497 StrList *sl = nasm_malloc(strlen(p)+1+sizeof sl->next);
2498 sl->next = NULL;
2499 strcpy(sl->str, p);
2500 *deptail = sl;
2501 deptail = &sl->next;
2502 }
2503 free_tlist(origline);
H. Peter Anvin418ca702008-05-30 10:42:30 -07002504 return DIRECTIVE_FOUND;
2505
2506 case PP_INCLUDE:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002507 t = tline->next = expand_smacro(tline->next);
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002508 skip_white_(t);
H. Peter Anvind2456592008-06-19 15:04:18 -07002509
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002510 if (!t || (t->type != TOK_STRING &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002511 t->type != TOK_INTERNAL_STRING)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002512 error(ERR_NONFATAL, "`%%include' expects a file name");
2513 free_tlist(origline);
2514 return DIRECTIVE_FOUND; /* but we did _something_ */
2515 }
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002516 if (t->next)
H. Peter Anvin917a3492008-09-24 09:14:49 -07002517 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +00002518 "trailing garbage after `%%include' ignored");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002519 p = t->text;
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002520 if (t->type != TOK_INTERNAL_STRING)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002521 nasm_unquote_cstr(p, i);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002522 inc = nasm_malloc(sizeof(Include));
2523 inc->next = istk;
2524 inc->conds = NULL;
H. Peter Anvin2b1c3b92008-06-06 10:38:46 -07002525 inc->fp = inc_fopen(p, dephead, &deptail, pass == 0);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002526 if (!inc->fp) {
2527 /* -MG given but file not found */
2528 nasm_free(inc);
2529 } else {
2530 inc->fname = src_set_fname(nasm_strdup(p));
2531 inc->lineno = src_set_linnum(0);
2532 inc->lineinc = 1;
2533 inc->expansion = NULL;
2534 inc->mstk = NULL;
2535 istk = inc;
2536 list->uplevel(LIST_INCLUDE);
2537 }
2538 free_tlist(origline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002539 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002540
H. Peter Anvind2456592008-06-19 15:04:18 -07002541 case PP_USE:
H. Peter Anvinf4ae5ad2008-06-19 18:39:24 -07002542 {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002543 static macros_t *use_pkg;
2544 const char *pkg_macro = NULL;
H. Peter Anvinf4ae5ad2008-06-19 18:39:24 -07002545
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002546 tline = tline->next;
2547 skip_white_(tline);
2548 tline = expand_id(tline);
H. Peter Anvind2456592008-06-19 15:04:18 -07002549
H. Peter Anvin264b7b92008-10-24 16:38:17 -07002550 if (!tline || (tline->type != TOK_STRING &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002551 tline->type != TOK_INTERNAL_STRING &&
2552 tline->type != TOK_ID)) {
H. Peter Anvin926fc402008-06-19 16:26:12 -07002553 error(ERR_NONFATAL, "`%%use' expects a package name");
H. Peter Anvind2456592008-06-19 15:04:18 -07002554 free_tlist(origline);
2555 return DIRECTIVE_FOUND; /* but we did _something_ */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002556 }
H. Peter Anvin264b7b92008-10-24 16:38:17 -07002557 if (tline->next)
H. Peter Anvin917a3492008-09-24 09:14:49 -07002558 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvind2456592008-06-19 15:04:18 -07002559 "trailing garbage after `%%use' ignored");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002560 if (tline->type == TOK_STRING)
2561 nasm_unquote_cstr(tline->text, i);
2562 use_pkg = nasm_stdmac_find_package(tline->text);
2563 if (!use_pkg)
2564 error(ERR_NONFATAL, "unknown `%%use' package: %s", tline->text);
2565 else
2566 pkg_macro = (char *)use_pkg + 1; /* The first string will be <%define>__USE_*__ */
Victor van den Elzen35eb2ea2010-03-10 22:33:48 +01002567 if (use_pkg && ! smacro_defined(NULL, pkg_macro, 0, NULL, true)) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002568 /* Not already included, go ahead and include it */
2569 stdmacpos = use_pkg;
2570 }
2571 free_tlist(origline);
H. Peter Anvind2456592008-06-19 15:04:18 -07002572 return DIRECTIVE_FOUND;
H. Peter Anvinf4ae5ad2008-06-19 18:39:24 -07002573 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002574 case PP_PUSH:
H. Peter Anvine2c80182005-01-15 22:15:51 +00002575 case PP_REPL:
H. Peter Anvin42b56392008-10-24 16:24:21 -07002576 case PP_POP:
H. Peter Anvine2c80182005-01-15 22:15:51 +00002577 tline = tline->next;
2578 skip_white_(tline);
2579 tline = expand_id(tline);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002580 if (tline) {
2581 if (!tok_type_(tline, TOK_ID)) {
2582 error(ERR_NONFATAL, "`%s' expects a context identifier",
2583 pp_directives[i]);
2584 free_tlist(origline);
2585 return DIRECTIVE_FOUND; /* but we did _something_ */
2586 }
2587 if (tline->next)
2588 error(ERR_WARNING|ERR_PASS1,
2589 "trailing garbage after `%s' ignored",
2590 pp_directives[i]);
2591 p = nasm_strdup(tline->text);
2592 } else {
2593 p = NULL; /* Anonymous */
2594 }
H. Peter Anvin42b56392008-10-24 16:24:21 -07002595
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002596 if (i == PP_PUSH) {
2597 ctx = nasm_malloc(sizeof(Context));
2598 ctx->next = cstk;
2599 hash_init(&ctx->localmac, HASH_SMALL);
2600 ctx->name = p;
2601 ctx->number = unique++;
2602 cstk = ctx;
2603 } else {
2604 /* %pop or %repl */
2605 if (!cstk) {
2606 error(ERR_NONFATAL, "`%s': context stack is empty",
2607 pp_directives[i]);
2608 } else if (i == PP_POP) {
2609 if (p && (!cstk->name || nasm_stricmp(p, cstk->name)))
2610 error(ERR_NONFATAL, "`%%pop' in wrong context: %s, "
2611 "expected %s",
2612 cstk->name ? cstk->name : "anonymous", p);
2613 else
2614 ctx_pop();
2615 } else {
2616 /* i == PP_REPL */
2617 nasm_free(cstk->name);
2618 cstk->name = p;
2619 p = NULL;
2620 }
2621 nasm_free(p);
2622 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002623 free_tlist(origline);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002624 return DIRECTIVE_FOUND;
H. Peter Anvin8e3f75e2008-09-24 00:21:58 -07002625 case PP_FATAL:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002626 severity = ERR_FATAL;
2627 goto issue_error;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002628 case PP_ERROR:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002629 severity = ERR_NONFATAL;
2630 goto issue_error;
H. Peter Anvin7df04172008-06-10 18:27:38 -07002631 case PP_WARNING:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002632 severity = ERR_WARNING|ERR_WARN_USER;
2633 goto issue_error;
H. Peter Anvin8e3f75e2008-09-24 00:21:58 -07002634
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002635issue_error:
H. Peter Anvin7df04172008-06-10 18:27:38 -07002636 {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002637 /* Only error out if this is the final pass */
2638 if (pass != 2 && i != PP_FATAL)
2639 return DIRECTIVE_FOUND;
2640
2641 tline->next = expand_smacro(tline->next);
2642 tline = tline->next;
2643 skip_white_(tline);
2644 t = tline ? tline->next : NULL;
2645 skip_white_(t);
2646 if (tok_type_(tline, TOK_STRING) && !t) {
2647 /* The line contains only a quoted string */
2648 p = tline->text;
2649 nasm_unquote(p, NULL); /* Ignore NUL character truncation */
2650 error(severity, "%s", p);
2651 } else {
2652 /* Not a quoted string, or more than a quoted string */
2653 p = detoken(tline, false);
2654 error(severity, "%s", p);
2655 nasm_free(p);
2656 }
2657 free_tlist(origline);
2658 return DIRECTIVE_FOUND;
H. Peter Anvin7df04172008-06-10 18:27:38 -07002659 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00002660
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002661 CASE_PP_IF:
H. Peter Anvine2c80182005-01-15 22:15:51 +00002662 if (istk->conds && !emitting(istk->conds->state))
2663 j = COND_NEVER;
2664 else {
2665 j = if_condition(tline->next, i);
2666 tline->next = NULL; /* it got freed */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002667 j = j < 0 ? COND_NEVER : j ? COND_IF_TRUE : COND_IF_FALSE;
2668 }
2669 cond = nasm_malloc(sizeof(Cond));
2670 cond->next = istk->conds;
2671 cond->state = j;
2672 istk->conds = cond;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002673 if(istk->mstk)
Keith Kanios3c0d91f2009-10-25 13:28:03 -05002674 istk->mstk->condcnt ++;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002675 free_tlist(origline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002676 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002677
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002678 CASE_PP_ELIF:
H. Peter Anvine2c80182005-01-15 22:15:51 +00002679 if (!istk->conds)
H. Peter Anvin4169a472007-09-12 01:29:43 +00002680 error(ERR_FATAL, "`%s': no matching `%%if'", pp_directives[i]);
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002681 switch(istk->conds->state) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002682 case COND_IF_TRUE:
2683 istk->conds->state = COND_DONE;
2684 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002685
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002686 case COND_DONE:
2687 case COND_NEVER:
2688 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002689
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002690 case COND_ELSE_TRUE:
2691 case COND_ELSE_FALSE:
2692 error_precond(ERR_WARNING|ERR_PASS1,
2693 "`%%elif' after `%%else' ignored");
2694 istk->conds->state = COND_NEVER;
2695 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002696
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002697 case COND_IF_FALSE:
2698 /*
2699 * IMPORTANT: In the case of %if, we will already have
2700 * called expand_mmac_params(); however, if we're
2701 * processing an %elif we must have been in a
2702 * non-emitting mode, which would have inhibited
2703 * the normal invocation of expand_mmac_params().
2704 * Therefore, we have to do it explicitly here.
2705 */
2706 j = if_condition(expand_mmac_params(tline->next), i);
2707 tline->next = NULL; /* it got freed */
2708 istk->conds->state =
2709 j < 0 ? COND_NEVER : j ? COND_IF_TRUE : COND_IF_FALSE;
2710 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002711 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002712 free_tlist(origline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002713 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002714
H. Peter Anvine2c80182005-01-15 22:15:51 +00002715 case PP_ELSE:
2716 if (tline->next)
H. Peter Anvin917a3492008-09-24 09:14:49 -07002717 error_precond(ERR_WARNING|ERR_PASS1,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002718 "trailing garbage after `%%else' ignored");
H. Peter Anvine2c80182005-01-15 22:15:51 +00002719 if (!istk->conds)
2720 error(ERR_FATAL, "`%%else': no matching `%%if'");
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002721 switch(istk->conds->state) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002722 case COND_IF_TRUE:
2723 case COND_DONE:
2724 istk->conds->state = COND_ELSE_FALSE;
2725 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002726
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002727 case COND_NEVER:
2728 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002729
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002730 case COND_IF_FALSE:
2731 istk->conds->state = COND_ELSE_TRUE;
2732 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002733
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002734 case COND_ELSE_TRUE:
2735 case COND_ELSE_FALSE:
2736 error_precond(ERR_WARNING|ERR_PASS1,
2737 "`%%else' after `%%else' ignored.");
2738 istk->conds->state = COND_NEVER;
2739 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002740 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002741 free_tlist(origline);
2742 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002743
H. Peter Anvine2c80182005-01-15 22:15:51 +00002744 case PP_ENDIF:
2745 if (tline->next)
H. Peter Anvin917a3492008-09-24 09:14:49 -07002746 error_precond(ERR_WARNING|ERR_PASS1,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002747 "trailing garbage after `%%endif' ignored");
H. Peter Anvine2c80182005-01-15 22:15:51 +00002748 if (!istk->conds)
2749 error(ERR_FATAL, "`%%endif': no matching `%%if'");
2750 cond = istk->conds;
2751 istk->conds = cond->next;
2752 nasm_free(cond);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002753 if(istk->mstk)
Keith Kanios3c0d91f2009-10-25 13:28:03 -05002754 istk->mstk->condcnt --;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002755 free_tlist(origline);
2756 return DIRECTIVE_FOUND;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002757
H. Peter Anvindb8f96e2009-07-15 09:07:29 -04002758 case PP_RMACRO:
2759 case PP_IRMACRO:
H. Peter Anvine2c80182005-01-15 22:15:51 +00002760 case PP_MACRO:
2761 case PP_IMACRO:
H. Peter Anvina26433d2008-07-16 14:40:01 -07002762 if (defining) {
H. Peter Anvindb8f96e2009-07-15 09:07:29 -04002763 error(ERR_FATAL, "`%s': already defining a macro",
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002764 pp_directives[i]);
2765 return DIRECTIVE_FOUND;
2766 }
2767 defining = nasm_malloc(sizeof(MMacro));
2768 defining->max_depth =
2769 (i == PP_RMACRO) || (i == PP_IRMACRO) ? DEADMAN_LIMIT : 0;
2770 defining->casesense = (i == PP_MACRO) || (i == PP_RMACRO);
2771 if (!parse_mmacro_spec(tline, defining, pp_directives[i])) {
2772 nasm_free(defining);
2773 defining = NULL;
2774 return DIRECTIVE_FOUND;
2775 }
H. Peter Anvina26433d2008-07-16 14:40:01 -07002776
H. Peter Anvin166c2472008-05-28 12:28:58 -07002777 mmac = (MMacro *) hash_findix(&mmacros, defining->name);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002778 while (mmac) {
2779 if (!strcmp(mmac->name, defining->name) &&
2780 (mmac->nparam_min <= defining->nparam_max
2781 || defining->plus)
2782 && (defining->nparam_min <= mmac->nparam_max
2783 || mmac->plus)) {
H. Peter Anvin917a3492008-09-24 09:14:49 -07002784 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +00002785 "redefining multi-line macro `%s'", defining->name);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002786 return DIRECTIVE_FOUND;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002787 }
2788 mmac = mmac->next;
2789 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002790 free_tlist(origline);
2791 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002792
H. Peter Anvine2c80182005-01-15 22:15:51 +00002793 case PP_ENDM:
2794 case PP_ENDMACRO:
Victor van den Elzen8f1120f2008-07-16 13:41:37 +02002795 if (! (defining && defining->name)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002796 error(ERR_NONFATAL, "`%s': not defining a macro", tline->text);
2797 return DIRECTIVE_FOUND;
2798 }
Keith Kanios852f1ee2009-07-12 00:19:55 -05002799 mmhead = (MMacro **) hash_findi_add(&mmacros, defining->name);
H. Peter Anvin97a23472007-09-16 17:57:25 -07002800 defining->next = *mmhead;
Keith Kanios852f1ee2009-07-12 00:19:55 -05002801 *mmhead = defining;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002802 defining = NULL;
2803 free_tlist(origline);
2804 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002805
H. Peter Anvin89cee572009-07-15 09:16:54 -04002806 case PP_EXITMACRO:
Keith Kanios852f1ee2009-07-12 00:19:55 -05002807 /*
2808 * We must search along istk->expansion until we hit a
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002809 * macro-end marker for a macro with a name. Then we
Keith Kanios3c0d91f2009-10-25 13:28:03 -05002810 * bypass all lines between exitmacro and endmacro.
Keith Kanios852f1ee2009-07-12 00:19:55 -05002811 */
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04002812 list_for_each(l, istk->expansion)
Keith Kanios852f1ee2009-07-12 00:19:55 -05002813 if (l->finishes && l->finishes->name)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002814 break;
Keith Kanios852f1ee2009-07-12 00:19:55 -05002815
2816 if (l) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002817 /*
2818 * Remove all conditional entries relative to this
2819 * macro invocation. (safe to do in this context)
2820 */
Keith Kanios3c0d91f2009-10-25 13:28:03 -05002821 for ( ; l->finishes->condcnt > 0; l->finishes->condcnt --) {
2822 cond = istk->conds;
2823 istk->conds = cond->next;
2824 nasm_free(cond);
2825 }
2826 istk->expansion = l;
Keith Kanios852f1ee2009-07-12 00:19:55 -05002827 } else {
2828 error(ERR_NONFATAL, "`%%exitmacro' not within `%%macro' block");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002829 }
Keith Kanios852f1ee2009-07-12 00:19:55 -05002830 free_tlist(origline);
2831 return DIRECTIVE_FOUND;
2832
H. Peter Anvina26433d2008-07-16 14:40:01 -07002833 case PP_UNMACRO:
2834 case PP_UNIMACRO:
2835 {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002836 MMacro **mmac_p;
2837 MMacro spec;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002838
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002839 spec.casesense = (i == PP_UNMACRO);
2840 if (!parse_mmacro_spec(tline, &spec, pp_directives[i])) {
2841 return DIRECTIVE_FOUND;
2842 }
H. Peter Anvina26433d2008-07-16 14:40:01 -07002843 mmac_p = (MMacro **) hash_findi(&mmacros, spec.name, NULL);
2844 while (mmac_p && *mmac_p) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002845 mmac = *mmac_p;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002846 if (mmac->casesense == spec.casesense &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002847 !mstrcmp(mmac->name, spec.name, spec.casesense) &&
H. Peter Anvina26433d2008-07-16 14:40:01 -07002848 mmac->nparam_min == spec.nparam_min &&
2849 mmac->nparam_max == spec.nparam_max &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002850 mmac->plus == spec.plus) {
2851 *mmac_p = mmac->next;
2852 free_mmacro(mmac);
2853 } else {
2854 mmac_p = &mmac->next;
2855 }
2856 }
2857 free_tlist(origline);
2858 free_tlist(spec.dlist);
2859 return DIRECTIVE_FOUND;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002860 }
2861
H. Peter Anvine2c80182005-01-15 22:15:51 +00002862 case PP_ROTATE:
2863 if (tline->next && tline->next->type == TOK_WHITESPACE)
2864 tline = tline->next;
H. Peter Anvin89cee572009-07-15 09:16:54 -04002865 if (!tline->next) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002866 free_tlist(origline);
2867 error(ERR_NONFATAL, "`%%rotate' missing rotate count");
2868 return DIRECTIVE_FOUND;
2869 }
2870 t = expand_smacro(tline->next);
2871 tline->next = NULL;
2872 free_tlist(origline);
2873 tline = t;
2874 tptr = &t;
2875 tokval.t_type = TOKEN_INVALID;
2876 evalresult =
2877 evaluate(ppscan, tptr, &tokval, NULL, pass, error, NULL);
2878 free_tlist(tline);
2879 if (!evalresult)
2880 return DIRECTIVE_FOUND;
2881 if (tokval.t_type)
H. Peter Anvin917a3492008-09-24 09:14:49 -07002882 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +00002883 "trailing garbage after expression ignored");
2884 if (!is_simple(evalresult)) {
2885 error(ERR_NONFATAL, "non-constant value given to `%%rotate'");
2886 return DIRECTIVE_FOUND;
2887 }
2888 mmac = istk->mstk;
2889 while (mmac && !mmac->name) /* avoid mistaking %reps for macros */
2890 mmac = mmac->next_active;
2891 if (!mmac) {
2892 error(ERR_NONFATAL, "`%%rotate' invoked outside a macro call");
2893 } else if (mmac->nparam == 0) {
2894 error(ERR_NONFATAL,
2895 "`%%rotate' invoked within macro without parameters");
2896 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002897 int rotate = mmac->rotate + reloc_value(evalresult);
H. Peter Anvin734b1882002-04-30 21:01:08 +00002898
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002899 rotate %= (int)mmac->nparam;
2900 if (rotate < 0)
2901 rotate += mmac->nparam;
H. Peter Anvin70653092007-10-19 14:42:29 -07002902
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002903 mmac->rotate = rotate;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002904 }
2905 return DIRECTIVE_FOUND;
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00002906
H. Peter Anvine2c80182005-01-15 22:15:51 +00002907 case PP_REP:
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002908 nolist = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002909 do {
2910 tline = tline->next;
2911 } while (tok_type_(tline, TOK_WHITESPACE));
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00002912
H. Peter Anvine2c80182005-01-15 22:15:51 +00002913 if (tok_type_(tline, TOK_ID) &&
2914 nasm_stricmp(tline->text, ".nolist") == 0) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002915 nolist = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002916 do {
2917 tline = tline->next;
2918 } while (tok_type_(tline, TOK_WHITESPACE));
2919 }
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00002920
H. Peter Anvine2c80182005-01-15 22:15:51 +00002921 if (tline) {
2922 t = expand_smacro(tline);
2923 tptr = &t;
2924 tokval.t_type = TOKEN_INVALID;
2925 evalresult =
2926 evaluate(ppscan, tptr, &tokval, NULL, pass, error, NULL);
2927 if (!evalresult) {
2928 free_tlist(origline);
2929 return DIRECTIVE_FOUND;
2930 }
2931 if (tokval.t_type)
H. Peter Anvin917a3492008-09-24 09:14:49 -07002932 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +00002933 "trailing garbage after expression ignored");
2934 if (!is_simple(evalresult)) {
2935 error(ERR_NONFATAL, "non-constant value given to `%%rep'");
2936 return DIRECTIVE_FOUND;
2937 }
Cyrill Gorcunove091d6e2010-08-09 13:58:22 +04002938 count = reloc_value(evalresult);
2939 if (count >= REP_LIMIT) {
Cyrill Gorcunov71f4f842010-08-09 20:17:17 +04002940 error(ERR_NONFATAL, "`%%rep' value exceeds limit");
Cyrill Gorcunove091d6e2010-08-09 13:58:22 +04002941 count = 0;
2942 } else
2943 count++;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002944 } else {
2945 error(ERR_NONFATAL, "`%%rep' expects a repeat count");
H. Peter Anvinf8ba53e2007-10-11 10:11:57 -07002946 count = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002947 }
2948 free_tlist(origline);
H. Peter Anvin734b1882002-04-30 21:01:08 +00002949
H. Peter Anvine2c80182005-01-15 22:15:51 +00002950 tmp_defining = defining;
2951 defining = nasm_malloc(sizeof(MMacro));
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002952 defining->prev = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002953 defining->name = NULL; /* flags this macro as a %rep block */
H. Peter Anvin70055962007-10-11 00:05:31 -07002954 defining->casesense = false;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002955 defining->plus = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002956 defining->nolist = nolist;
H. Peter Anvinf8ba53e2007-10-11 10:11:57 -07002957 defining->in_progress = count;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002958 defining->max_depth = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002959 defining->nparam_min = defining->nparam_max = 0;
2960 defining->defaults = NULL;
2961 defining->dlist = NULL;
2962 defining->expansion = NULL;
2963 defining->next_active = istk->mstk;
2964 defining->rep_nest = tmp_defining;
2965 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002966
H. Peter Anvine2c80182005-01-15 22:15:51 +00002967 case PP_ENDREP:
2968 if (!defining || defining->name) {
2969 error(ERR_NONFATAL, "`%%endrep': no matching `%%rep'");
2970 return DIRECTIVE_FOUND;
2971 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00002972
H. Peter Anvine2c80182005-01-15 22:15:51 +00002973 /*
2974 * Now we have a "macro" defined - although it has no name
2975 * and we won't be entering it in the hash tables - we must
2976 * push a macro-end marker for it on to istk->expansion.
2977 * After that, it will take care of propagating itself (a
2978 * macro-end marker line for a macro which is really a %rep
2979 * block will cause the macro to be re-expanded, complete
2980 * with another macro-end marker to ensure the process
2981 * continues) until the whole expansion is forcibly removed
2982 * from istk->expansion by a %exitrep.
2983 */
2984 l = nasm_malloc(sizeof(Line));
2985 l->next = istk->expansion;
2986 l->finishes = defining;
2987 l->first = NULL;
2988 istk->expansion = l;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002989
H. Peter Anvine2c80182005-01-15 22:15:51 +00002990 istk->mstk = defining;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002991
H. Peter Anvine2c80182005-01-15 22:15:51 +00002992 list->uplevel(defining->nolist ? LIST_MACRO_NOLIST : LIST_MACRO);
2993 tmp_defining = defining;
2994 defining = defining->rep_nest;
2995 free_tlist(origline);
2996 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002997
H. Peter Anvine2c80182005-01-15 22:15:51 +00002998 case PP_EXITREP:
2999 /*
3000 * We must search along istk->expansion until we hit a
3001 * macro-end marker for a macro with no name. Then we set
3002 * its `in_progress' flag to 0.
3003 */
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04003004 list_for_each(l, istk->expansion)
H. Peter Anvine2c80182005-01-15 22:15:51 +00003005 if (l->finishes && !l->finishes->name)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003006 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003007
H. Peter Anvine2c80182005-01-15 22:15:51 +00003008 if (l)
Charles Crayned4200be2008-07-12 16:42:33 -07003009 l->finishes->in_progress = 1;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003010 else
3011 error(ERR_NONFATAL, "`%%exitrep' not within `%%rep' block");
3012 free_tlist(origline);
3013 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003014
H. Peter Anvine2c80182005-01-15 22:15:51 +00003015 case PP_XDEFINE:
3016 case PP_IXDEFINE:
3017 case PP_DEFINE:
3018 case PP_IDEFINE:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003019 casesense = (i == PP_DEFINE || i == PP_XDEFINE);
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07003020
H. Peter Anvine2c80182005-01-15 22:15:51 +00003021 tline = tline->next;
3022 skip_white_(tline);
3023 tline = expand_id(tline);
3024 if (!tline || (tline->type != TOK_ID &&
3025 (tline->type != TOK_PREPROC_ID ||
3026 tline->text[1] != '$'))) {
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07003027 error(ERR_NONFATAL, "`%s' expects a macro identifier",
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003028 pp_directives[i]);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003029 free_tlist(origline);
3030 return DIRECTIVE_FOUND;
3031 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003032
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08003033 ctx = get_ctx(tline->text, &mname, false);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003034 last = tline;
3035 param_start = tline = tline->next;
3036 nparam = 0;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003037
H. Peter Anvine2c80182005-01-15 22:15:51 +00003038 /* Expand the macro definition now for %xdefine and %ixdefine */
3039 if ((i == PP_XDEFINE) || (i == PP_IXDEFINE))
3040 tline = expand_smacro(tline);
H. Peter Anvin734b1882002-04-30 21:01:08 +00003041
H. Peter Anvine2c80182005-01-15 22:15:51 +00003042 if (tok_is_(tline, "(")) {
3043 /*
3044 * This macro has parameters.
3045 */
H. Peter Anvin734b1882002-04-30 21:01:08 +00003046
H. Peter Anvine2c80182005-01-15 22:15:51 +00003047 tline = tline->next;
3048 while (1) {
3049 skip_white_(tline);
3050 if (!tline) {
3051 error(ERR_NONFATAL, "parameter identifier expected");
3052 free_tlist(origline);
3053 return DIRECTIVE_FOUND;
3054 }
3055 if (tline->type != TOK_ID) {
3056 error(ERR_NONFATAL,
3057 "`%s': parameter identifier expected",
3058 tline->text);
3059 free_tlist(origline);
3060 return DIRECTIVE_FOUND;
3061 }
3062 tline->type = TOK_SMAC_PARAM + nparam++;
3063 tline = tline->next;
3064 skip_white_(tline);
3065 if (tok_is_(tline, ",")) {
3066 tline = tline->next;
H. Peter Anvinca348b62008-07-23 10:49:26 -04003067 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003068 if (!tok_is_(tline, ")")) {
3069 error(ERR_NONFATAL,
3070 "`)' expected to terminate macro template");
3071 free_tlist(origline);
3072 return DIRECTIVE_FOUND;
3073 }
3074 break;
3075 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00003076 }
3077 last = tline;
3078 tline = tline->next;
3079 }
3080 if (tok_type_(tline, TOK_WHITESPACE))
3081 last = tline, tline = tline->next;
3082 macro_start = NULL;
3083 last->next = NULL;
3084 t = tline;
3085 while (t) {
3086 if (t->type == TOK_ID) {
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04003087 list_for_each(tt, param_start)
H. Peter Anvine2c80182005-01-15 22:15:51 +00003088 if (tt->type >= TOK_SMAC_PARAM &&
3089 !strcmp(tt->text, t->text))
3090 t->type = tt->type;
3091 }
3092 tt = t->next;
3093 t->next = macro_start;
3094 macro_start = t;
3095 t = tt;
3096 }
3097 /*
3098 * Good. We now have a macro name, a parameter count, and a
3099 * token list (in reverse order) for an expansion. We ought
3100 * to be OK just to create an SMacro, store it, and let
3101 * free_tlist have the rest of the line (which we have
3102 * carefully re-terminated after chopping off the expansion
3103 * from the end).
3104 */
H. Peter Anvin4db5a162007-10-11 13:42:09 -07003105 define_smacro(ctx, mname, casesense, nparam, macro_start);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003106 free_tlist(origline);
3107 return DIRECTIVE_FOUND;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003108
H. Peter Anvine2c80182005-01-15 22:15:51 +00003109 case PP_UNDEF:
3110 tline = tline->next;
3111 skip_white_(tline);
3112 tline = expand_id(tline);
3113 if (!tline || (tline->type != TOK_ID &&
3114 (tline->type != TOK_PREPROC_ID ||
3115 tline->text[1] != '$'))) {
3116 error(ERR_NONFATAL, "`%%undef' expects a macro identifier");
3117 free_tlist(origline);
3118 return DIRECTIVE_FOUND;
3119 }
3120 if (tline->next) {
H. Peter Anvin917a3492008-09-24 09:14:49 -07003121 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +00003122 "trailing garbage after macro name ignored");
3123 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003124
H. Peter Anvine2c80182005-01-15 22:15:51 +00003125 /* Find the context that symbol belongs to */
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08003126 ctx = get_ctx(tline->text, &mname, false);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003127 undef_smacro(ctx, mname);
3128 free_tlist(origline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003129 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003130
H. Peter Anvin9e200162008-06-04 17:23:14 -07003131 case PP_DEFSTR:
3132 case PP_IDEFSTR:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003133 casesense = (i == PP_DEFSTR);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003134
3135 tline = tline->next;
3136 skip_white_(tline);
3137 tline = expand_id(tline);
3138 if (!tline || (tline->type != TOK_ID &&
3139 (tline->type != TOK_PREPROC_ID ||
3140 tline->text[1] != '$'))) {
3141 error(ERR_NONFATAL, "`%s' expects a macro identifier",
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003142 pp_directives[i]);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003143 free_tlist(origline);
3144 return DIRECTIVE_FOUND;
3145 }
3146
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08003147 ctx = get_ctx(tline->text, &mname, false);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003148 last = tline;
3149 tline = expand_smacro(tline->next);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003150 last->next = NULL;
H. Peter Anvin9e200162008-06-04 17:23:14 -07003151
3152 while (tok_type_(tline, TOK_WHITESPACE))
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003153 tline = delete_Token(tline);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003154
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003155 p = detoken(tline, false);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003156 macro_start = nasm_malloc(sizeof(*macro_start));
3157 macro_start->next = NULL;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003158 macro_start->text = nasm_quote(p, strlen(p));
3159 macro_start->type = TOK_STRING;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003160 macro_start->a.mac = NULL;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003161 nasm_free(p);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003162
3163 /*
3164 * We now have a macro name, an implicit parameter count of
3165 * zero, and a string token to use as an expansion. Create
3166 * and store an SMacro.
3167 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003168 define_smacro(ctx, mname, casesense, 0, macro_start);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003169 free_tlist(origline);
3170 return DIRECTIVE_FOUND;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003171
H. Peter Anvin2f55bda2009-07-14 15:04:04 -04003172 case PP_DEFTOK:
3173 case PP_IDEFTOK:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003174 casesense = (i == PP_DEFTOK);
3175
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003176 tline = tline->next;
3177 skip_white_(tline);
3178 tline = expand_id(tline);
3179 if (!tline || (tline->type != TOK_ID &&
3180 (tline->type != TOK_PREPROC_ID ||
3181 tline->text[1] != '$'))) {
3182 error(ERR_NONFATAL,
3183 "`%s' expects a macro identifier as first parameter",
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003184 pp_directives[i]);
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003185 free_tlist(origline);
3186 return DIRECTIVE_FOUND;
3187 }
3188 ctx = get_ctx(tline->text, &mname, false);
3189 last = tline;
3190 tline = expand_smacro(tline->next);
3191 last->next = NULL;
3192
3193 t = tline;
3194 while (tok_type_(t, TOK_WHITESPACE))
3195 t = t->next;
3196 /* t should now point to the string */
Cyrill Gorcunov6908e582010-09-06 19:36:15 +04003197 if (!tok_type_(t, TOK_STRING)) {
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003198 error(ERR_NONFATAL,
3199 "`%s` requires string as second parameter",
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003200 pp_directives[i]);
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003201 free_tlist(tline);
3202 free_tlist(origline);
3203 return DIRECTIVE_FOUND;
3204 }
3205
H. Peter Anvinb40992c2010-09-15 08:57:21 -07003206 /*
3207 * Convert the string to a token stream. Note that smacros
3208 * are stored with the token stream reversed, so we have to
3209 * reverse the output of tokenize().
3210 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003211 nasm_unquote_cstr(t->text, i);
H. Peter Anvinb40992c2010-09-15 08:57:21 -07003212 macro_start = reverse_tokens(tokenize(t->text));
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003213
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003214 /*
3215 * We now have a macro name, an implicit parameter count of
3216 * zero, and a numeric token to use as an expansion. Create
3217 * and store an SMacro.
3218 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003219 define_smacro(ctx, mname, casesense, 0, macro_start);
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003220 free_tlist(tline);
3221 free_tlist(origline);
3222 return DIRECTIVE_FOUND;
H. Peter Anvin9e200162008-06-04 17:23:14 -07003223
H. Peter Anvin418ca702008-05-30 10:42:30 -07003224 case PP_PATHSEARCH:
3225 {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003226 FILE *fp;
3227 StrList *xsl = NULL;
3228 StrList **xst = &xsl;
H. Peter Anvin418ca702008-05-30 10:42:30 -07003229
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003230 casesense = true;
H. Peter Anvin418ca702008-05-30 10:42:30 -07003231
3232 tline = tline->next;
3233 skip_white_(tline);
3234 tline = expand_id(tline);
3235 if (!tline || (tline->type != TOK_ID &&
3236 (tline->type != TOK_PREPROC_ID ||
3237 tline->text[1] != '$'))) {
3238 error(ERR_NONFATAL,
3239 "`%%pathsearch' expects a macro identifier as first parameter");
3240 free_tlist(origline);
3241 return DIRECTIVE_FOUND;
3242 }
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08003243 ctx = get_ctx(tline->text, &mname, false);
H. Peter Anvin418ca702008-05-30 10:42:30 -07003244 last = tline;
3245 tline = expand_smacro(tline->next);
3246 last->next = NULL;
3247
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003248 t = tline;
H. Peter Anvin418ca702008-05-30 10:42:30 -07003249 while (tok_type_(t, TOK_WHITESPACE))
3250 t = t->next;
3251
3252 if (!t || (t->type != TOK_STRING &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003253 t->type != TOK_INTERNAL_STRING)) {
H. Peter Anvin418ca702008-05-30 10:42:30 -07003254 error(ERR_NONFATAL, "`%%pathsearch' expects a file name");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003255 free_tlist(tline);
H. Peter Anvin418ca702008-05-30 10:42:30 -07003256 free_tlist(origline);
3257 return DIRECTIVE_FOUND; /* but we did _something_ */
3258 }
3259 if (t->next)
H. Peter Anvin917a3492008-09-24 09:14:49 -07003260 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvin418ca702008-05-30 10:42:30 -07003261 "trailing garbage after `%%pathsearch' ignored");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003262 p = t->text;
H. Peter Anvin427cc912008-06-01 21:43:03 -07003263 if (t->type != TOK_INTERNAL_STRING)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003264 nasm_unquote(p, NULL);
H. Peter Anvin418ca702008-05-30 10:42:30 -07003265
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003266 fp = inc_fopen(p, &xsl, &xst, true);
3267 if (fp) {
3268 p = xsl->str;
3269 fclose(fp); /* Don't actually care about the file */
3270 }
H. Peter Anvin418ca702008-05-30 10:42:30 -07003271 macro_start = nasm_malloc(sizeof(*macro_start));
3272 macro_start->next = NULL;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003273 macro_start->text = nasm_quote(p, strlen(p));
3274 macro_start->type = TOK_STRING;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003275 macro_start->a.mac = NULL;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003276 if (xsl)
3277 nasm_free(xsl);
H. Peter Anvin418ca702008-05-30 10:42:30 -07003278
3279 /*
3280 * We now have a macro name, an implicit parameter count of
3281 * zero, and a string token to use as an expansion. Create
3282 * and store an SMacro.
3283 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003284 define_smacro(ctx, mname, casesense, 0, macro_start);
3285 free_tlist(tline);
H. Peter Anvin418ca702008-05-30 10:42:30 -07003286 free_tlist(origline);
3287 return DIRECTIVE_FOUND;
3288 }
3289
H. Peter Anvine2c80182005-01-15 22:15:51 +00003290 case PP_STRLEN:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003291 casesense = true;
H. Peter Anvin70653092007-10-19 14:42:29 -07003292
H. Peter Anvine2c80182005-01-15 22:15:51 +00003293 tline = tline->next;
3294 skip_white_(tline);
3295 tline = expand_id(tline);
3296 if (!tline || (tline->type != TOK_ID &&
3297 (tline->type != TOK_PREPROC_ID ||
3298 tline->text[1] != '$'))) {
3299 error(ERR_NONFATAL,
3300 "`%%strlen' expects a macro identifier as first parameter");
3301 free_tlist(origline);
3302 return DIRECTIVE_FOUND;
3303 }
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08003304 ctx = get_ctx(tline->text, &mname, false);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003305 last = tline;
3306 tline = expand_smacro(tline->next);
3307 last->next = NULL;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003308
H. Peter Anvine2c80182005-01-15 22:15:51 +00003309 t = tline;
3310 while (tok_type_(t, TOK_WHITESPACE))
3311 t = t->next;
3312 /* t should now point to the string */
Cyrill Gorcunov4e1d5ab2010-07-23 18:51:51 +04003313 if (!tok_type_(t, TOK_STRING)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00003314 error(ERR_NONFATAL,
3315 "`%%strlen` requires string as second parameter");
3316 free_tlist(tline);
3317 free_tlist(origline);
3318 return DIRECTIVE_FOUND;
3319 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003320
H. Peter Anvine2c80182005-01-15 22:15:51 +00003321 macro_start = nasm_malloc(sizeof(*macro_start));
3322 macro_start->next = NULL;
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07003323 make_tok_num(macro_start, nasm_unquote(t->text, NULL));
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003324 macro_start->a.mac = NULL;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003325
H. Peter Anvine2c80182005-01-15 22:15:51 +00003326 /*
3327 * We now have a macro name, an implicit parameter count of
3328 * zero, and a numeric token to use as an expansion. Create
3329 * and store an SMacro.
3330 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003331 define_smacro(ctx, mname, casesense, 0, macro_start);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003332 free_tlist(tline);
3333 free_tlist(origline);
3334 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003335
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003336 case PP_STRCAT:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003337 casesense = true;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003338
3339 tline = tline->next;
3340 skip_white_(tline);
3341 tline = expand_id(tline);
3342 if (!tline || (tline->type != TOK_ID &&
3343 (tline->type != TOK_PREPROC_ID ||
3344 tline->text[1] != '$'))) {
3345 error(ERR_NONFATAL,
3346 "`%%strcat' expects a macro identifier as first parameter");
3347 free_tlist(origline);
3348 return DIRECTIVE_FOUND;
3349 }
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08003350 ctx = get_ctx(tline->text, &mname, false);
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003351 last = tline;
3352 tline = expand_smacro(tline->next);
3353 last->next = NULL;
3354
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003355 len = 0;
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04003356 list_for_each(t, tline) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003357 switch (t->type) {
3358 case TOK_WHITESPACE:
3359 break;
3360 case TOK_STRING:
3361 len += t->a.len = nasm_unquote(t->text, NULL);
3362 break;
3363 case TOK_OTHER:
3364 if (!strcmp(t->text, ",")) /* permit comma separators */
3365 break;
3366 /* else fall through */
3367 default:
3368 error(ERR_NONFATAL,
3369 "non-string passed to `%%strcat' (%d)", t->type);
3370 free_tlist(tline);
3371 free_tlist(origline);
3372 return DIRECTIVE_FOUND;
3373 }
3374 }
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003375
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003376 p = pp = nasm_malloc(len);
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04003377 list_for_each(t, tline) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003378 if (t->type == TOK_STRING) {
3379 memcpy(p, t->text, t->a.len);
3380 p += t->a.len;
3381 }
3382 }
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003383
3384 /*
3385 * We now have a macro name, an implicit parameter count of
3386 * zero, and a numeric token to use as an expansion. Create
3387 * and store an SMacro.
3388 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003389 macro_start = new_Token(NULL, TOK_STRING, NULL, 0);
3390 macro_start->text = nasm_quote(pp, len);
3391 nasm_free(pp);
3392 define_smacro(ctx, mname, casesense, 0, macro_start);
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003393 free_tlist(tline);
3394 free_tlist(origline);
3395 return DIRECTIVE_FOUND;
3396
H. Peter Anvine2c80182005-01-15 22:15:51 +00003397 case PP_SUBSTR:
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003398 {
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003399 int64_t start, count;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003400 size_t len;
H. Peter Anvind2456592008-06-19 15:04:18 -07003401
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003402 casesense = true;
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07003403
H. Peter Anvine2c80182005-01-15 22:15:51 +00003404 tline = tline->next;
3405 skip_white_(tline);
3406 tline = expand_id(tline);
3407 if (!tline || (tline->type != TOK_ID &&
3408 (tline->type != TOK_PREPROC_ID ||
3409 tline->text[1] != '$'))) {
3410 error(ERR_NONFATAL,
3411 "`%%substr' expects a macro identifier as first parameter");
3412 free_tlist(origline);
3413 return DIRECTIVE_FOUND;
3414 }
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08003415 ctx = get_ctx(tline->text, &mname, false);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003416 last = tline;
3417 tline = expand_smacro(tline->next);
3418 last->next = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003419
Cyrill Gorcunov35519d62010-09-06 23:49:52 +04003420 if (tline) /* skip expanded id */
3421 t = tline->next;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003422 while (tok_type_(t, TOK_WHITESPACE))
3423 t = t->next;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003424
H. Peter Anvine2c80182005-01-15 22:15:51 +00003425 /* t should now point to the string */
Cyrill Gorcunov35519d62010-09-06 23:49:52 +04003426 if (!tok_type_(t, TOK_STRING)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00003427 error(ERR_NONFATAL,
3428 "`%%substr` requires string as second parameter");
3429 free_tlist(tline);
3430 free_tlist(origline);
3431 return DIRECTIVE_FOUND;
3432 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003433
H. Peter Anvine2c80182005-01-15 22:15:51 +00003434 tt = t->next;
3435 tptr = &tt;
3436 tokval.t_type = TOKEN_INVALID;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003437 evalresult = evaluate(ppscan, tptr, &tokval, NULL,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003438 pass, error, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003439 if (!evalresult) {
3440 free_tlist(tline);
3441 free_tlist(origline);
3442 return DIRECTIVE_FOUND;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003443 } else if (!is_simple(evalresult)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00003444 error(ERR_NONFATAL, "non-constant value given to `%%substr`");
3445 free_tlist(tline);
3446 free_tlist(origline);
3447 return DIRECTIVE_FOUND;
3448 }
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003449 start = evalresult->value - 1;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003450
3451 while (tok_type_(tt, TOK_WHITESPACE))
3452 tt = tt->next;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003453 if (!tt) {
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003454 count = 1; /* Backwards compatibility: one character */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003455 } else {
3456 tokval.t_type = TOKEN_INVALID;
3457 evalresult = evaluate(ppscan, tptr, &tokval, NULL,
3458 pass, error, NULL);
3459 if (!evalresult) {
3460 free_tlist(tline);
3461 free_tlist(origline);
3462 return DIRECTIVE_FOUND;
3463 } else if (!is_simple(evalresult)) {
3464 error(ERR_NONFATAL, "non-constant value given to `%%substr`");
3465 free_tlist(tline);
3466 free_tlist(origline);
3467 return DIRECTIVE_FOUND;
3468 }
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003469 count = evalresult->value;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003470 }
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003471
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003472 len = nasm_unquote(t->text, NULL);
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003473
Cyrill Gorcunovcff031e2010-09-07 20:31:11 +04003474 /* make start and count being in range */
3475 if (start < 0)
3476 start = 0;
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003477 if (count < 0)
3478 count = len + count + 1 - start;
3479 if (start + count > (int64_t)len)
Cyrill Gorcunovcff031e2010-09-07 20:31:11 +04003480 count = len - start;
3481 if (!len || count < 0 || start >=(int64_t)len)
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003482 start = -1, count = 0; /* empty string */
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003483
H. Peter Anvine2c80182005-01-15 22:15:51 +00003484 macro_start = nasm_malloc(sizeof(*macro_start));
3485 macro_start->next = NULL;
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003486 macro_start->text = nasm_quote((start < 0) ? "" : t->text + start, count);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003487 macro_start->type = TOK_STRING;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003488 macro_start->a.mac = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003489
H. Peter Anvine2c80182005-01-15 22:15:51 +00003490 /*
3491 * We now have a macro name, an implicit parameter count of
3492 * zero, and a numeric token to use as an expansion. Create
3493 * and store an SMacro.
3494 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003495 define_smacro(ctx, mname, casesense, 0, macro_start);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003496 free_tlist(tline);
3497 free_tlist(origline);
3498 return DIRECTIVE_FOUND;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003499 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003500
H. Peter Anvine2c80182005-01-15 22:15:51 +00003501 case PP_ASSIGN:
3502 case PP_IASSIGN:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003503 casesense = (i == PP_ASSIGN);
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07003504
H. Peter Anvine2c80182005-01-15 22:15:51 +00003505 tline = tline->next;
3506 skip_white_(tline);
3507 tline = expand_id(tline);
3508 if (!tline || (tline->type != TOK_ID &&
3509 (tline->type != TOK_PREPROC_ID ||
3510 tline->text[1] != '$'))) {
3511 error(ERR_NONFATAL,
3512 "`%%%sassign' expects a macro identifier",
3513 (i == PP_IASSIGN ? "i" : ""));
3514 free_tlist(origline);
3515 return DIRECTIVE_FOUND;
3516 }
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08003517 ctx = get_ctx(tline->text, &mname, false);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003518 last = tline;
3519 tline = expand_smacro(tline->next);
3520 last->next = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003521
H. Peter Anvine2c80182005-01-15 22:15:51 +00003522 t = tline;
3523 tptr = &t;
3524 tokval.t_type = TOKEN_INVALID;
3525 evalresult =
3526 evaluate(ppscan, tptr, &tokval, NULL, pass, error, NULL);
3527 free_tlist(tline);
3528 if (!evalresult) {
3529 free_tlist(origline);
3530 return DIRECTIVE_FOUND;
3531 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003532
H. Peter Anvine2c80182005-01-15 22:15:51 +00003533 if (tokval.t_type)
H. Peter Anvin917a3492008-09-24 09:14:49 -07003534 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +00003535 "trailing garbage after expression ignored");
H. Peter Anvin734b1882002-04-30 21:01:08 +00003536
H. Peter Anvine2c80182005-01-15 22:15:51 +00003537 if (!is_simple(evalresult)) {
3538 error(ERR_NONFATAL,
3539 "non-constant value given to `%%%sassign'",
3540 (i == PP_IASSIGN ? "i" : ""));
3541 free_tlist(origline);
3542 return DIRECTIVE_FOUND;
3543 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003544
H. Peter Anvine2c80182005-01-15 22:15:51 +00003545 macro_start = nasm_malloc(sizeof(*macro_start));
3546 macro_start->next = NULL;
3547 make_tok_num(macro_start, reloc_value(evalresult));
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003548 macro_start->a.mac = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003549
H. Peter Anvine2c80182005-01-15 22:15:51 +00003550 /*
3551 * We now have a macro name, an implicit parameter count of
3552 * zero, and a numeric token to use as an expansion. Create
3553 * and store an SMacro.
3554 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003555 define_smacro(ctx, mname, casesense, 0, macro_start);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003556 free_tlist(origline);
3557 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003558
H. Peter Anvine2c80182005-01-15 22:15:51 +00003559 case PP_LINE:
3560 /*
3561 * Syntax is `%line nnn[+mmm] [filename]'
3562 */
3563 tline = tline->next;
3564 skip_white_(tline);
3565 if (!tok_type_(tline, TOK_NUMBER)) {
3566 error(ERR_NONFATAL, "`%%line' expects line number");
3567 free_tlist(origline);
3568 return DIRECTIVE_FOUND;
3569 }
H. Peter Anvin70055962007-10-11 00:05:31 -07003570 k = readnum(tline->text, &err);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003571 m = 1;
3572 tline = tline->next;
3573 if (tok_is_(tline, "+")) {
3574 tline = tline->next;
3575 if (!tok_type_(tline, TOK_NUMBER)) {
3576 error(ERR_NONFATAL, "`%%line' expects line increment");
3577 free_tlist(origline);
3578 return DIRECTIVE_FOUND;
3579 }
H. Peter Anvin70055962007-10-11 00:05:31 -07003580 m = readnum(tline->text, &err);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003581 tline = tline->next;
3582 }
3583 skip_white_(tline);
3584 src_set_linnum(k);
3585 istk->lineinc = m;
3586 if (tline) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07003587 nasm_free(src_set_fname(detoken(tline, false)));
H. Peter Anvine2c80182005-01-15 22:15:51 +00003588 }
3589 free_tlist(origline);
3590 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003591
H. Peter Anvine2c80182005-01-15 22:15:51 +00003592 default:
3593 error(ERR_FATAL,
3594 "preprocessor directive `%s' not yet implemented",
H. Peter Anvin4169a472007-09-12 01:29:43 +00003595 pp_directives[i]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003596 return DIRECTIVE_FOUND;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003597 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003598}
3599
3600/*
H. Peter Anvin76690a12002-04-30 20:52:49 +00003601 * Ensure that a macro parameter contains a condition code and
3602 * nothing else. Return the condition code index if so, or -1
3603 * otherwise.
3604 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003605static int find_cc(Token * t)
H. Peter Anvineba20a72002-04-30 20:53:55 +00003606{
H. Peter Anvin76690a12002-04-30 20:52:49 +00003607 Token *tt;
3608 int i, j, k, m;
3609
H. Peter Anvin25a99342007-09-22 17:45:45 -07003610 if (!t)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003611 return -1; /* Probably a %+ without a space */
H. Peter Anvin25a99342007-09-22 17:45:45 -07003612
H. Peter Anvineba20a72002-04-30 20:53:55 +00003613 skip_white_(t);
H. Peter Anvin76690a12002-04-30 20:52:49 +00003614 if (t->type != TOK_ID)
H. Peter Anvine2c80182005-01-15 22:15:51 +00003615 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003616 tt = t->next;
H. Peter Anvineba20a72002-04-30 20:53:55 +00003617 skip_white_(tt);
H. Peter Anvin76690a12002-04-30 20:52:49 +00003618 if (tt && (tt->type != TOK_OTHER || strcmp(tt->text, ",")))
H. Peter Anvine2c80182005-01-15 22:15:51 +00003619 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003620
3621 i = -1;
Cyrill Gorcunova7319242010-06-03 22:04:36 +04003622 j = ARRAY_SIZE(conditions);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003623 while (j - i > 1) {
3624 k = (j + i) / 2;
3625 m = nasm_stricmp(t->text, conditions[k]);
3626 if (m == 0) {
3627 i = k;
3628 j = -2;
3629 break;
3630 } else if (m < 0) {
3631 j = k;
3632 } else
3633 i = k;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003634 }
3635 if (j != -2)
H. Peter Anvine2c80182005-01-15 22:15:51 +00003636 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003637 return i;
3638}
3639
H. Peter Anvind784a082009-04-20 14:01:18 -07003640static bool paste_tokens(Token **head, bool handle_paste_tokens)
3641{
3642 Token **tail, *t, *tt;
3643 Token **paste_head;
3644 bool did_paste = false;
3645 char *tmp;
3646
3647 /* Now handle token pasting... */
3648 paste_head = NULL;
3649 tail = head;
3650 while ((t = *tail) && (tt = t->next)) {
3651 switch (t->type) {
3652 case TOK_WHITESPACE:
3653 if (tt->type == TOK_WHITESPACE) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003654 /* Zap adjacent whitespace tokens */
H. Peter Anvind784a082009-04-20 14:01:18 -07003655 t->next = delete_Token(tt);
3656 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003657 /* Do not advance paste_head here */
3658 tail = &t->next;
3659 }
H. Peter Anvind784a082009-04-20 14:01:18 -07003660 break;
3661 case TOK_ID:
Cyrill Gorcunov8bc80172010-09-16 00:16:19 +04003662 case TOK_PREPROC_ID:
H. Peter Anvind784a082009-04-20 14:01:18 -07003663 case TOK_NUMBER:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003664 case TOK_FLOAT:
3665 {
3666 size_t len = 0;
3667 char *tmp, *p;
H. Peter Anvind784a082009-04-20 14:01:18 -07003668
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003669 while (tt && (tt->type == TOK_ID || tt->type == TOK_PREPROC_ID ||
3670 tt->type == TOK_NUMBER || tt->type == TOK_FLOAT ||
3671 tt->type == TOK_OTHER)) {
3672 len += strlen(tt->text);
3673 tt = tt->next;
3674 }
H. Peter Anvind784a082009-04-20 14:01:18 -07003675
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003676 /*
3677 * Now tt points to the first token after
3678 * the potential paste area...
3679 */
3680 if (tt != t->next) {
3681 /* We have at least two tokens... */
3682 len += strlen(t->text);
3683 p = tmp = nasm_malloc(len+1);
H. Peter Anvind784a082009-04-20 14:01:18 -07003684
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003685 while (t != tt) {
3686 strcpy(p, t->text);
3687 p = strchr(p, '\0');
3688 t = delete_Token(t);
3689 }
H. Peter Anvind784a082009-04-20 14:01:18 -07003690
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003691 t = *tail = tokenize(tmp);
3692 nasm_free(tmp);
H. Peter Anvind784a082009-04-20 14:01:18 -07003693
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003694 while (t->next) {
3695 tail = &t->next;
3696 t = t->next;
3697 }
3698 t->next = tt; /* Attach the remaining token chain */
H. Peter Anvind784a082009-04-20 14:01:18 -07003699
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003700 did_paste = true;
3701 }
3702 paste_head = tail;
3703 tail = &t->next;
3704 break;
3705 }
3706 case TOK_PASTE: /* %+ */
3707 if (handle_paste_tokens) {
3708 /* Zap %+ and whitespace tokens to the right */
3709 while (t && (t->type == TOK_WHITESPACE ||
3710 t->type == TOK_PASTE))
3711 t = *tail = delete_Token(t);
3712 if (!paste_head || !t)
3713 break; /* Nothing to paste with */
3714 tail = paste_head;
3715 t = *tail;
3716 tt = t->next;
3717 while (tok_type_(tt, TOK_WHITESPACE))
3718 tt = t->next = delete_Token(tt);
H. Peter Anvind784a082009-04-20 14:01:18 -07003719
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003720 if (tt) {
3721 tmp = nasm_strcat(t->text, tt->text);
3722 delete_Token(t);
3723 tt = delete_Token(tt);
3724 t = *tail = tokenize(tmp);
3725 nasm_free(tmp);
3726 while (t->next) {
3727 tail = &t->next;
3728 t = t->next;
3729 }
3730 t->next = tt; /* Attach the remaining token chain */
3731 did_paste = true;
3732 }
3733 paste_head = tail;
3734 tail = &t->next;
3735 break;
3736 }
3737 /* else fall through */
3738 default:
Cyrill Gorcunovadabc152010-07-10 02:11:41 +04003739 tail = &t->next;
3740 if (!tok_type_(t->next, TOK_WHITESPACE))
3741 paste_head = tail;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003742 break;
H. Peter Anvind784a082009-04-20 14:01:18 -07003743 }
3744 }
3745 return did_paste;
3746}
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003747
3748/*
3749 * expands to a list of tokens from %{x:y}
3750 */
3751static Token *expand_mmac_params_range(MMacro *mac, Token *tline, Token ***last)
3752{
3753 Token *t = tline, **tt, *tm, *head;
3754 char *pos;
3755 int fst, lst, j, i;
3756
3757 pos = strchr(tline->text, ':');
3758 nasm_assert(pos);
3759
3760 lst = atoi(pos + 1);
3761 fst = atoi(tline->text + 1);
3762
3763 /*
3764 * only macros params are accounted so
3765 * if someone passes %0 -- we reject such
3766 * value(s)
3767 */
3768 if (lst == 0 || fst == 0)
3769 goto err;
3770
3771 /* the values should be sane */
Cyrill Gorcunov2f403752010-06-05 10:47:10 +04003772 if ((fst > (int)mac->nparam || fst < (-(int)mac->nparam)) ||
3773 (lst > (int)mac->nparam || lst < (-(int)mac->nparam)))
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003774 goto err;
3775
Cyrill Gorcunovefb35832010-06-20 01:52:19 +04003776 fst = fst < 0 ? fst + (int)mac->nparam + 1: fst;
3777 lst = lst < 0 ? lst + (int)mac->nparam + 1: lst;
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003778
3779 /* counted from zero */
3780 fst--, lst--;
3781
3782 /*
3783 * it will be at least one token
3784 */
3785 tm = mac->params[(fst + mac->rotate) % mac->nparam];
3786 t = new_Token(NULL, tm->type, tm->text, 0);
3787 head = t, tt = &t->next;
3788 if (fst < lst) {
3789 for (i = fst + 1; i <= lst; i++) {
3790 t = new_Token(NULL, TOK_OTHER, ",", 0);
3791 *tt = t, tt = &t->next;
3792 j = (i + mac->rotate) % mac->nparam;
3793 tm = mac->params[j];
3794 t = new_Token(NULL, tm->type, tm->text, 0);
3795 *tt = t, tt = &t->next;
3796 }
3797 } else {
3798 for (i = fst - 1; i >= lst; i--) {
3799 t = new_Token(NULL, TOK_OTHER, ",", 0);
3800 *tt = t, tt = &t->next;
3801 j = (i + mac->rotate) % mac->nparam;
3802 tm = mac->params[j];
3803 t = new_Token(NULL, tm->type, tm->text, 0);
3804 *tt = t, tt = &t->next;
3805 }
3806 }
3807
3808 *last = tt;
3809 return head;
3810
3811err:
3812 error(ERR_NONFATAL, "`%%{%s}': macro parameters out of range",
3813 &tline->text[1]);
3814 return tline;
3815}
3816
H. Peter Anvin76690a12002-04-30 20:52:49 +00003817/*
3818 * Expand MMacro-local things: parameter references (%0, %n, %+n,
H. Peter Anvin67c63722008-10-26 23:49:00 -07003819 * %-n) and MMacro-local identifiers (%%foo) as well as
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003820 * macro indirection (%[...]) and range (%{..:..}).
H. Peter Anvin76690a12002-04-30 20:52:49 +00003821 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003822static Token *expand_mmac_params(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00003823{
H. Peter Anvin734b1882002-04-30 21:01:08 +00003824 Token *t, *tt, **tail, *thead;
H. Peter Anvin6125b622009-04-08 14:02:25 -07003825 bool changed = false;
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003826 char *pos;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003827
3828 tail = &thead;
3829 thead = NULL;
3830
H. Peter Anvine2c80182005-01-15 22:15:51 +00003831 while (tline) {
3832 if (tline->type == TOK_PREPROC_ID &&
Cyrill Gorcunovca611192010-06-04 09:22:12 +04003833 (((tline->text[1] == '+' || tline->text[1] == '-') && tline->text[2]) ||
3834 (tline->text[1] >= '0' && tline->text[1] <= '9') ||
3835 tline->text[1] == '%')) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00003836 char *text = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003837 int type = 0, cc; /* type = 0 to placate optimisers */
Keith Kaniosa6dfa782007-04-13 16:47:53 +00003838 char tmpbuf[30];
H. Peter Anvin25a99342007-09-22 17:45:45 -07003839 unsigned int n;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003840 int i;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003841 MMacro *mac;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003842
H. Peter Anvine2c80182005-01-15 22:15:51 +00003843 t = tline;
3844 tline = tline->next;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003845
H. Peter Anvine2c80182005-01-15 22:15:51 +00003846 mac = istk->mstk;
3847 while (mac && !mac->name) /* avoid mistaking %reps for macros */
3848 mac = mac->next_active;
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003849 if (!mac) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00003850 error(ERR_NONFATAL, "`%s': not in a macro call", t->text);
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003851 } else {
3852 pos = strchr(t->text, ':');
3853 if (!pos) {
3854 switch (t->text[1]) {
3855 /*
3856 * We have to make a substitution of one of the
3857 * forms %1, %-1, %+1, %%foo, %0.
3858 */
3859 case '0':
3860 type = TOK_NUMBER;
3861 snprintf(tmpbuf, sizeof(tmpbuf), "%d", mac->nparam);
3862 text = nasm_strdup(tmpbuf);
3863 break;
3864 case '%':
H. Peter Anvine2c80182005-01-15 22:15:51 +00003865 type = TOK_ID;
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003866 snprintf(tmpbuf, sizeof(tmpbuf), "..@%"PRIu64".",
3867 mac->unique);
3868 text = nasm_strcat(tmpbuf, t->text + 2);
3869 break;
3870 case '-':
3871 n = atoi(t->text + 2) - 1;
3872 if (n >= mac->nparam)
3873 tt = NULL;
3874 else {
3875 if (mac->nparam > 1)
3876 n = (n + mac->rotate) % mac->nparam;
3877 tt = mac->params[n];
H. Peter Anvine2c80182005-01-15 22:15:51 +00003878 }
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003879 cc = find_cc(tt);
3880 if (cc == -1) {
3881 error(ERR_NONFATAL,
3882 "macro parameter %d is not a condition code",
3883 n + 1);
3884 text = NULL;
3885 } else {
3886 type = TOK_ID;
3887 if (inverse_ccs[cc] == -1) {
3888 error(ERR_NONFATAL,
3889 "condition code `%s' is not invertible",
3890 conditions[cc]);
3891 text = NULL;
3892 } else
3893 text = nasm_strdup(conditions[inverse_ccs[cc]]);
3894 }
3895 break;
3896 case '+':
3897 n = atoi(t->text + 2) - 1;
3898 if (n >= mac->nparam)
3899 tt = NULL;
3900 else {
3901 if (mac->nparam > 1)
3902 n = (n + mac->rotate) % mac->nparam;
3903 tt = mac->params[n];
3904 }
3905 cc = find_cc(tt);
3906 if (cc == -1) {
3907 error(ERR_NONFATAL,
3908 "macro parameter %d is not a condition code",
3909 n + 1);
3910 text = NULL;
3911 } else {
3912 type = TOK_ID;
3913 text = nasm_strdup(conditions[cc]);
3914 }
3915 break;
3916 default:
3917 n = atoi(t->text + 1) - 1;
3918 if (n >= mac->nparam)
3919 tt = NULL;
3920 else {
3921 if (mac->nparam > 1)
3922 n = (n + mac->rotate) % mac->nparam;
3923 tt = mac->params[n];
3924 }
3925 if (tt) {
3926 for (i = 0; i < mac->paramlen[n]; i++) {
3927 *tail = new_Token(NULL, tt->type, tt->text, 0);
3928 tail = &(*tail)->next;
3929 tt = tt->next;
3930 }
3931 }
3932 text = NULL; /* we've done it here */
3933 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003934 }
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003935 } else {
3936 /*
3937 * seems we have a parameters range here
3938 */
3939 Token *head, **last;
3940 head = expand_mmac_params_range(mac, t, &last);
3941 if (head != t) {
3942 *tail = head;
3943 *last = tline;
3944 tline = head;
3945 text = NULL;
3946 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00003947 }
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003948 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00003949 if (!text) {
3950 delete_Token(t);
3951 } else {
3952 *tail = t;
3953 tail = &t->next;
3954 t->type = type;
3955 nasm_free(t->text);
3956 t->text = text;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003957 t->a.mac = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003958 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003959 changed = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003960 continue;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003961 } else if (tline->type == TOK_INDIRECT) {
3962 t = tline;
3963 tline = tline->next;
3964 tt = tokenize(t->text);
3965 tt = expand_mmac_params(tt);
3966 tt = expand_smacro(tt);
3967 *tail = tt;
3968 while (tt) {
3969 tt->a.mac = NULL; /* Necessary? */
3970 tail = &tt->next;
3971 tt = tt->next;
3972 }
3973 delete_Token(t);
3974 changed = true;
Cyrill Gorcunov8bc80172010-09-16 00:16:19 +04003975 } else if (tline->type == TOK_PREPROC_ID &&
3976 tline->text[0] == '%' &&
3977 tline->text[1] == '$' &&
3978 !tok_type_(tline->next, TOK_WHITESPACE) &&
3979 (tok_type_(tline->next, TOK_ID) ||
3980 tok_type_(tline->next, TOK_PREPROC_ID) ||
3981 tok_type_(tline->next, TOK_NUMBER) ||
3982 tok_type_(tline->next, TOK_OTHER) ||
3983 tok_type_(tline->next, TOK_FLOAT))) {
3984 /*
3985 * In a sake of backward compatibility we allow
3986 * to expand local single macro that early before
3987 * pasting token code have place
3988 *
3989 * NOTE: that new code MUST use %+ macro to obtain
3990 * same result
3991 */
3992 t = tline;
3993 tline = tline->next;
3994 tt = tokenize(t->text);
3995 tt = expand_smacro(tt);
3996 *tail = tt;
3997 while (tt) {
3998 tt->a.mac = NULL;
3999 tail = &tt->next;
4000 tt = tt->next;
4001 }
4002 delete_Token(t);
4003 changed = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004004 } else {
4005 t = *tail = tline;
4006 tline = tline->next;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004007 t->a.mac = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004008 tail = &t->next;
4009 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00004010 }
H. Peter Anvineba20a72002-04-30 20:53:55 +00004011 *tail = NULL;
H. Peter Anvin67c63722008-10-26 23:49:00 -07004012
H. Peter Anvind784a082009-04-20 14:01:18 -07004013 if (changed)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004014 paste_tokens(&thead, false);
H. Peter Anvin6125b622009-04-08 14:02:25 -07004015
H. Peter Anvin76690a12002-04-30 20:52:49 +00004016 return thead;
4017}
4018
4019/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004020 * Expand all single-line macro calls made in the given line.
4021 * Return the expanded version of the line. The original is deemed
4022 * to be destroyed in the process. (In reality we'll just move
4023 * Tokens from input to output a lot of the time, rather than
4024 * actually bothering to destroy and replicate.)
4025 */
H. Peter Anvincb1cf592007-11-19 12:26:50 -08004026
H. Peter Anvine2c80182005-01-15 22:15:51 +00004027static Token *expand_smacro(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004028{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004029 Token *t, *tt, *mstart, **tail, *thead;
H. Peter Anvineba20a72002-04-30 20:53:55 +00004030 SMacro *head = NULL, *m;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004031 Token **params;
4032 int *paramsize;
H. Peter Anvin25a99342007-09-22 17:45:45 -07004033 unsigned int nparam, sparam;
H. Peter Anvind784a082009-04-20 14:01:18 -07004034 int brackets;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004035 Token *org_tline = tline;
4036 Context *ctx;
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08004037 const char *mname;
H. Peter Anvin2a15e692007-11-19 13:14:59 -08004038 int deadman = DEADMAN_LIMIT;
H. Peter Anvin8287daf2009-07-07 16:00:58 -07004039 bool expanded;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004040
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004041 /*
4042 * Trick: we should avoid changing the start token pointer since it can
4043 * be contained in "next" field of other token. Because of this
4044 * we allocate a copy of first token and work with it; at the end of
4045 * routine we copy it back
4046 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004047 if (org_tline) {
Cyrill Gorcunoved4a8052010-04-09 15:40:35 +04004048 tline = new_Token(org_tline->next, org_tline->type,
4049 org_tline->text, 0);
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004050 tline->a.mac = org_tline->a.mac;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004051 nasm_free(org_tline->text);
4052 org_tline->text = NULL;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004053 }
4054
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004055 expanded = true; /* Always expand %+ at least once */
H. Peter Anvin8287daf2009-07-07 16:00:58 -07004056
H. Peter Anvincb1cf592007-11-19 12:26:50 -08004057again:
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004058 thead = NULL;
Cyrill Gorcunoved4a8052010-04-09 15:40:35 +04004059 tail = &thead;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004060
H. Peter Anvine2c80182005-01-15 22:15:51 +00004061 while (tline) { /* main token loop */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004062 if (!--deadman) {
4063 error(ERR_NONFATAL, "interminable macro recursion");
Cyrill Gorcunovbd38c8f2009-11-21 11:11:23 +03004064 goto err;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004065 }
H. Peter Anvincb1cf592007-11-19 12:26:50 -08004066
H. Peter Anvine2c80182005-01-15 22:15:51 +00004067 if ((mname = tline->text)) {
4068 /* if this token is a local macro, look in local context */
Cyrill Gorcunovc56d9ad2010-02-11 15:12:19 +03004069 if (tline->type == TOK_ID) {
4070 head = (SMacro *)hash_findix(&smacros, mname);
4071 } else if (tline->type == TOK_PREPROC_ID) {
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08004072 ctx = get_ctx(mname, &mname, true);
Cyrill Gorcunovc56d9ad2010-02-11 15:12:19 +03004073 head = ctx ? (SMacro *)hash_findix(&ctx->localmac, mname) : NULL;
4074 } else
4075 head = NULL;
H. Peter Anvin072771e2008-05-22 13:17:51 -07004076
H. Peter Anvine2c80182005-01-15 22:15:51 +00004077 /*
4078 * We've hit an identifier. As in is_mmacro below, we first
4079 * check whether the identifier is a single-line macro at
4080 * all, then think about checking for parameters if
4081 * necessary.
4082 */
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04004083 list_for_each(m, head)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004084 if (!mstrcmp(m->name, mname, m->casesense))
4085 break;
4086 if (m) {
4087 mstart = tline;
4088 params = NULL;
4089 paramsize = NULL;
4090 if (m->nparam == 0) {
4091 /*
4092 * Simple case: the macro is parameterless. Discard the
4093 * one token that the macro call took, and push the
4094 * expansion back on the to-do stack.
4095 */
4096 if (!m->expansion) {
4097 if (!strcmp("__FILE__", m->name)) {
4098 int32_t num = 0;
4099 char *file = NULL;
4100 src_get(&num, &file);
4101 tline->text = nasm_quote(file, strlen(file));
4102 tline->type = TOK_STRING;
4103 nasm_free(file);
H. Peter Anvine2c80182005-01-15 22:15:51 +00004104 continue;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004105 }
4106 if (!strcmp("__LINE__", m->name)) {
4107 nasm_free(tline->text);
4108 make_tok_num(tline, src_get_linnum());
4109 continue;
4110 }
4111 if (!strcmp("__BITS__", m->name)) {
4112 nasm_free(tline->text);
4113 make_tok_num(tline, globalbits);
4114 continue;
4115 }
4116 tline = delete_Token(tline);
4117 continue;
4118 }
4119 } else {
4120 /*
4121 * Complicated case: at least one macro with this name
H. Peter Anvine2c80182005-01-15 22:15:51 +00004122 * exists and takes parameters. We must find the
4123 * parameters in the call, count them, find the SMacro
4124 * that corresponds to that form of the macro call, and
4125 * substitute for the parameters when we expand. What a
4126 * pain.
4127 */
4128 /*tline = tline->next;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004129 skip_white_(tline); */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004130 do {
4131 t = tline->next;
4132 while (tok_type_(t, TOK_SMAC_END)) {
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004133 t->a.mac->in_progress = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004134 t->text = NULL;
4135 t = tline->next = delete_Token(t);
4136 }
4137 tline = t;
4138 } while (tok_type_(tline, TOK_WHITESPACE));
4139 if (!tok_is_(tline, "(")) {
4140 /*
4141 * This macro wasn't called with parameters: ignore
4142 * the call. (Behaviour borrowed from gnu cpp.)
4143 */
4144 tline = mstart;
4145 m = NULL;
4146 } else {
4147 int paren = 0;
4148 int white = 0;
4149 brackets = 0;
4150 nparam = 0;
4151 sparam = PARAM_DELTA;
4152 params = nasm_malloc(sparam * sizeof(Token *));
4153 params[0] = tline->next;
4154 paramsize = nasm_malloc(sparam * sizeof(int));
4155 paramsize[0] = 0;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07004156 while (true) { /* parameter loop */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004157 /*
4158 * For some unusual expansions
4159 * which concatenates function call
4160 */
4161 t = tline->next;
4162 while (tok_type_(t, TOK_SMAC_END)) {
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004163 t->a.mac->in_progress = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004164 t->text = NULL;
4165 t = tline->next = delete_Token(t);
4166 }
4167 tline = t;
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00004168
H. Peter Anvine2c80182005-01-15 22:15:51 +00004169 if (!tline) {
4170 error(ERR_NONFATAL,
4171 "macro call expects terminating `)'");
4172 break;
4173 }
4174 if (tline->type == TOK_WHITESPACE
4175 && brackets <= 0) {
4176 if (paramsize[nparam])
4177 white++;
4178 else
4179 params[nparam] = tline->next;
4180 continue; /* parameter loop */
4181 }
4182 if (tline->type == TOK_OTHER
4183 && tline->text[1] == 0) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004184 char ch = tline->text[0];
H. Peter Anvine2c80182005-01-15 22:15:51 +00004185 if (ch == ',' && !paren && brackets <= 0) {
4186 if (++nparam >= sparam) {
4187 sparam += PARAM_DELTA;
4188 params = nasm_realloc(params,
Cyrill Gorcunoved4a8052010-04-09 15:40:35 +04004189 sparam * sizeof(Token *));
4190 paramsize = nasm_realloc(paramsize,
4191 sparam * sizeof(int));
H. Peter Anvine2c80182005-01-15 22:15:51 +00004192 }
4193 params[nparam] = tline->next;
4194 paramsize[nparam] = 0;
4195 white = 0;
4196 continue; /* parameter loop */
4197 }
4198 if (ch == '{' &&
4199 (brackets > 0 || (brackets == 0 &&
4200 !paramsize[nparam])))
4201 {
4202 if (!(brackets++)) {
4203 params[nparam] = tline->next;
4204 continue; /* parameter loop */
4205 }
4206 }
4207 if (ch == '}' && brackets > 0)
4208 if (--brackets == 0) {
4209 brackets = -1;
4210 continue; /* parameter loop */
4211 }
4212 if (ch == '(' && !brackets)
4213 paren++;
4214 if (ch == ')' && brackets <= 0)
4215 if (--paren < 0)
4216 break;
4217 }
4218 if (brackets < 0) {
4219 brackets = 0;
4220 error(ERR_NONFATAL, "braces do not "
4221 "enclose all of macro parameter");
4222 }
4223 paramsize[nparam] += white + 1;
4224 white = 0;
4225 } /* parameter loop */
4226 nparam++;
4227 while (m && (m->nparam != nparam ||
4228 mstrcmp(m->name, mname,
4229 m->casesense)))
4230 m = m->next;
4231 if (!m)
H. Peter Anvin917a3492008-09-24 09:14:49 -07004232 error(ERR_WARNING|ERR_PASS1|ERR_WARN_MNP,
H. Peter Anvine2c80182005-01-15 22:15:51 +00004233 "macro `%s' exists, "
4234 "but not taking %d parameters",
4235 mstart->text, nparam);
4236 }
4237 }
4238 if (m && m->in_progress)
4239 m = NULL;
4240 if (!m) { /* in progess or didn't find '(' or wrong nparam */
H. Peter Anvin70653092007-10-19 14:42:29 -07004241 /*
H. Peter Anvine2c80182005-01-15 22:15:51 +00004242 * Design question: should we handle !tline, which
4243 * indicates missing ')' here, or expand those
4244 * macros anyway, which requires the (t) test a few
H. Peter Anvin70653092007-10-19 14:42:29 -07004245 * lines down?
H. Peter Anvine2c80182005-01-15 22:15:51 +00004246 */
4247 nasm_free(params);
4248 nasm_free(paramsize);
4249 tline = mstart;
4250 } else {
4251 /*
4252 * Expand the macro: we are placed on the last token of the
4253 * call, so that we can easily split the call from the
4254 * following tokens. We also start by pushing an SMAC_END
4255 * token for the cycle removal.
4256 */
4257 t = tline;
4258 if (t) {
4259 tline = t->next;
4260 t->next = NULL;
4261 }
4262 tt = new_Token(tline, TOK_SMAC_END, NULL, 0);
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004263 tt->a.mac = m;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07004264 m->in_progress = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004265 tline = tt;
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04004266 list_for_each(t, m->expansion) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00004267 if (t->type >= TOK_SMAC_PARAM) {
4268 Token *pcopy = tline, **ptail = &pcopy;
4269 Token *ttt, *pt;
4270 int i;
H. Peter Anvin734b1882002-04-30 21:01:08 +00004271
H. Peter Anvine2c80182005-01-15 22:15:51 +00004272 ttt = params[t->type - TOK_SMAC_PARAM];
Cyrill Gorcunoved4a8052010-04-09 15:40:35 +04004273 i = paramsize[t->type - TOK_SMAC_PARAM];
4274 while (--i >= 0) {
4275 pt = *ptail = new_Token(tline, ttt->type,
4276 ttt->text, 0);
H. Peter Anvine2c80182005-01-15 22:15:51 +00004277 ptail = &pt->next;
4278 ttt = ttt->next;
4279 }
4280 tline = pcopy;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004281 } else if (t->type == TOK_PREPROC_Q) {
4282 tt = new_Token(tline, TOK_ID, mname, 0);
4283 tline = tt;
4284 } else if (t->type == TOK_PREPROC_QQ) {
4285 tt = new_Token(tline, TOK_ID, m->name, 0);
4286 tline = tt;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004287 } else {
4288 tt = new_Token(tline, t->type, t->text, 0);
4289 tline = tt;
4290 }
4291 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00004292
H. Peter Anvine2c80182005-01-15 22:15:51 +00004293 /*
4294 * Having done that, get rid of the macro call, and clean
4295 * up the parameters.
4296 */
4297 nasm_free(params);
4298 nasm_free(paramsize);
4299 free_tlist(mstart);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004300 expanded = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004301 continue; /* main token loop */
4302 }
4303 }
4304 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004305
H. Peter Anvine2c80182005-01-15 22:15:51 +00004306 if (tline->type == TOK_SMAC_END) {
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004307 tline->a.mac->in_progress = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004308 tline = delete_Token(tline);
4309 } else {
4310 t = *tail = tline;
4311 tline = tline->next;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004312 t->a.mac = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004313 t->next = NULL;
4314 tail = &t->next;
4315 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004316 }
4317
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004318 /*
4319 * Now scan the entire line and look for successive TOK_IDs that resulted
Keith Kaniosb7a89542007-04-12 02:40:54 +00004320 * after expansion (they can't be produced by tokenize()). The successive
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004321 * TOK_IDs should be concatenated.
4322 * Also we look for %+ tokens and concatenate the tokens before and after
4323 * them (without white spaces in between).
4324 */
H. Peter Anvin8287daf2009-07-07 16:00:58 -07004325 if (expanded && paste_tokens(&thead, true)) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004326 /*
4327 * If we concatenated something, *and* we had previously expanded
4328 * an actual macro, scan the lines again for macros...
4329 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004330 tline = thead;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004331 expanded = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004332 goto again;
H. Peter Anvin734b1882002-04-30 21:01:08 +00004333 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004334
Cyrill Gorcunovbd38c8f2009-11-21 11:11:23 +03004335err:
H. Peter Anvine2c80182005-01-15 22:15:51 +00004336 if (org_tline) {
4337 if (thead) {
4338 *org_tline = *thead;
4339 /* since we just gave text to org_line, don't free it */
4340 thead->text = NULL;
4341 delete_Token(thead);
4342 } else {
4343 /* the expression expanded to empty line;
4344 we can't return NULL for some reasons
4345 we just set the line to a single WHITESPACE token. */
4346 memset(org_tline, 0, sizeof(*org_tline));
4347 org_tline->text = NULL;
4348 org_tline->type = TOK_WHITESPACE;
4349 }
4350 thead = org_tline;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004351 }
4352
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004353 return thead;
4354}
4355
4356/*
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004357 * Similar to expand_smacro but used exclusively with macro identifiers
4358 * right before they are fetched in. The reason is that there can be
4359 * identifiers consisting of several subparts. We consider that if there
4360 * are more than one element forming the name, user wants a expansion,
4361 * otherwise it will be left as-is. Example:
4362 *
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004363 * %define %$abc cde
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004364 *
4365 * the identifier %$abc will be left as-is so that the handler for %define
4366 * will suck it and define the corresponding value. Other case:
4367 *
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004368 * %define _%$abc cde
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004369 *
4370 * In this case user wants name to be expanded *before* %define starts
4371 * working, so we'll expand %$abc into something (if it has a value;
4372 * otherwise it will be left as-is) then concatenate all successive
4373 * PP_IDs into one.
4374 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004375static Token *expand_id(Token * tline)
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004376{
4377 Token *cur, *oldnext = NULL;
4378
H. Peter Anvin734b1882002-04-30 21:01:08 +00004379 if (!tline || !tline->next)
H. Peter Anvine2c80182005-01-15 22:15:51 +00004380 return tline;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004381
4382 cur = tline;
4383 while (cur->next &&
H. Peter Anvine2c80182005-01-15 22:15:51 +00004384 (cur->next->type == TOK_ID ||
4385 cur->next->type == TOK_PREPROC_ID
4386 || cur->next->type == TOK_NUMBER))
4387 cur = cur->next;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004388
4389 /* If identifier consists of just one token, don't expand */
4390 if (cur == tline)
H. Peter Anvine2c80182005-01-15 22:15:51 +00004391 return tline;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004392
H. Peter Anvine2c80182005-01-15 22:15:51 +00004393 if (cur) {
4394 oldnext = cur->next; /* Detach the tail past identifier */
4395 cur->next = NULL; /* so that expand_smacro stops here */
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004396 }
4397
H. Peter Anvin734b1882002-04-30 21:01:08 +00004398 tline = expand_smacro(tline);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004399
H. Peter Anvine2c80182005-01-15 22:15:51 +00004400 if (cur) {
4401 /* expand_smacro possibly changhed tline; re-scan for EOL */
4402 cur = tline;
4403 while (cur && cur->next)
4404 cur = cur->next;
4405 if (cur)
4406 cur->next = oldnext;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004407 }
4408
4409 return tline;
4410}
4411
4412/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004413 * Determine whether the given line constitutes a multi-line macro
4414 * call, and return the MMacro structure called if so. Doesn't have
4415 * to check for an initial label - that's taken care of in
4416 * expand_mmacro - but must check numbers of parameters. Guaranteed
4417 * to be called with tline->type == TOK_ID, so the putative macro
4418 * name is easy to find.
4419 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004420static MMacro *is_mmacro(Token * tline, Token *** params_array)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004421{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004422 MMacro *head, *m;
4423 Token **params;
4424 int nparam;
4425
H. Peter Anvin166c2472008-05-28 12:28:58 -07004426 head = (MMacro *) hash_findix(&mmacros, tline->text);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004427
4428 /*
4429 * Efficiency: first we see if any macro exists with the given
4430 * name. If not, we can return NULL immediately. _Then_ we
4431 * count the parameters, and then we look further along the
4432 * list if necessary to find the proper MMacro.
4433 */
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04004434 list_for_each(m, head)
H. Peter Anvine2c80182005-01-15 22:15:51 +00004435 if (!mstrcmp(m->name, tline->text, m->casesense))
4436 break;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004437 if (!m)
H. Peter Anvine2c80182005-01-15 22:15:51 +00004438 return NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004439
4440 /*
4441 * OK, we have a potential macro. Count and demarcate the
4442 * parameters.
4443 */
H. Peter Anvin734b1882002-04-30 21:01:08 +00004444 count_mmac_params(tline->next, &nparam, &params);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004445
4446 /*
4447 * So we know how many parameters we've got. Find the MMacro
4448 * structure that handles this number.
4449 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004450 while (m) {
4451 if (m->nparam_min <= nparam
4452 && (m->plus || nparam <= m->nparam_max)) {
4453 /*
4454 * This one is right. Just check if cycle removal
4455 * prohibits us using it before we actually celebrate...
4456 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004457 if (m->in_progress > m->max_depth) {
4458 if (m->max_depth > 0) {
4459 error(ERR_WARNING,
4460 "reached maximum recursion depth of %i",
4461 m->max_depth);
4462 }
4463 nasm_free(params);
4464 return NULL;
4465 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00004466 /*
4467 * It's right, and we can use it. Add its default
4468 * parameters to the end of our list if necessary.
4469 */
4470 if (m->defaults && nparam < m->nparam_min + m->ndefs) {
4471 params =
4472 nasm_realloc(params,
4473 ((m->nparam_min + m->ndefs +
4474 1) * sizeof(*params)));
4475 while (nparam < m->nparam_min + m->ndefs) {
4476 params[nparam] = m->defaults[nparam - m->nparam_min];
4477 nparam++;
4478 }
4479 }
4480 /*
4481 * If we've gone over the maximum parameter count (and
4482 * we're in Plus mode), ignore parameters beyond
4483 * nparam_max.
4484 */
4485 if (m->plus && nparam > m->nparam_max)
4486 nparam = m->nparam_max;
4487 /*
4488 * Then terminate the parameter list, and leave.
4489 */
4490 if (!params) { /* need this special case */
4491 params = nasm_malloc(sizeof(*params));
4492 nparam = 0;
4493 }
4494 params[nparam] = NULL;
4495 *params_array = params;
4496 return m;
4497 }
4498 /*
4499 * This one wasn't right: look for the next one with the
4500 * same name.
4501 */
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04004502 list_for_each(m, m->next)
H. Peter Anvine2c80182005-01-15 22:15:51 +00004503 if (!mstrcmp(m->name, tline->text, m->casesense))
4504 break;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004505 }
4506
4507 /*
4508 * After all that, we didn't find one with the right number of
4509 * parameters. Issue a warning, and fail to expand the macro.
4510 */
H. Peter Anvin917a3492008-09-24 09:14:49 -07004511 error(ERR_WARNING|ERR_PASS1|ERR_WARN_MNP,
H. Peter Anvine2c80182005-01-15 22:15:51 +00004512 "macro `%s' exists, but not taking %d parameters",
4513 tline->text, nparam);
H. Peter Anvin734b1882002-04-30 21:01:08 +00004514 nasm_free(params);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004515 return NULL;
4516}
4517
Keith Kanios891775e2009-07-11 06:08:54 -05004518
4519/*
4520 * Save MMacro invocation specific fields in
4521 * preparation for a recursive macro expansion
4522 */
4523static void push_mmacro(MMacro *m)
4524{
4525 MMacroInvocation *i;
4526
H. Peter Anvin89cee572009-07-15 09:16:54 -04004527 i = nasm_malloc(sizeof(MMacroInvocation));
4528 i->prev = m->prev;
4529 i->params = m->params;
4530 i->iline = m->iline;
4531 i->nparam = m->nparam;
4532 i->rotate = m->rotate;
4533 i->paramlen = m->paramlen;
4534 i->unique = m->unique;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004535 i->condcnt = m->condcnt;
H. Peter Anvin89cee572009-07-15 09:16:54 -04004536 m->prev = i;
Keith Kanios891775e2009-07-11 06:08:54 -05004537}
4538
4539
4540/*
4541 * Restore MMacro invocation specific fields that were
4542 * saved during a previous recursive macro expansion
4543 */
4544static void pop_mmacro(MMacro *m)
4545{
4546 MMacroInvocation *i;
4547
H. Peter Anvin89cee572009-07-15 09:16:54 -04004548 if (m->prev) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004549 i = m->prev;
4550 m->prev = i->prev;
4551 m->params = i->params;
4552 m->iline = i->iline;
4553 m->nparam = i->nparam;
4554 m->rotate = i->rotate;
4555 m->paramlen = i->paramlen;
4556 m->unique = i->unique;
4557 m->condcnt = i->condcnt;
4558 nasm_free(i);
H. Peter Anvin89cee572009-07-15 09:16:54 -04004559 }
Keith Kanios891775e2009-07-11 06:08:54 -05004560}
4561
4562
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004563/*
4564 * Expand the multi-line macro call made by the given line, if
4565 * there is one to be expanded. If there is, push the expansion on
H. Peter Anvineba20a72002-04-30 20:53:55 +00004566 * istk->expansion and return 1. Otherwise return 0.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004567 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004568static int expand_mmacro(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004569{
4570 Token *startline = tline;
4571 Token *label = NULL;
4572 int dont_prepend = 0;
H. Peter Anvince2233b2008-05-25 21:57:00 -07004573 Token **params, *t, *mtok, *tt;
H. Peter Anvineba20a72002-04-30 20:53:55 +00004574 MMacro *m;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004575 Line *l, *ll;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004576 int i, nparam, *paramlen;
H. Peter Anvinc751e862008-06-09 10:18:45 -07004577 const char *mname;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004578
4579 t = tline;
H. Peter Anvineba20a72002-04-30 20:53:55 +00004580 skip_white_(t);
H. Peter Anvince2233b2008-05-25 21:57:00 -07004581 /* if (!tok_type_(t, TOK_ID)) Lino 02/25/02 */
H. Peter Anvindce1e2f2002-04-30 21:06:37 +00004582 if (!tok_type_(t, TOK_ID) && !tok_type_(t, TOK_PREPROC_ID))
H. Peter Anvine2c80182005-01-15 22:15:51 +00004583 return 0;
H. Peter Anvince2233b2008-05-25 21:57:00 -07004584 mtok = t;
H. Peter Anvin734b1882002-04-30 21:01:08 +00004585 m = is_mmacro(t, &params);
H. Peter Anvinc751e862008-06-09 10:18:45 -07004586 if (m) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004587 mname = t->text;
H. Peter Anvinc751e862008-06-09 10:18:45 -07004588 } else {
H. Peter Anvine2c80182005-01-15 22:15:51 +00004589 Token *last;
4590 /*
4591 * We have an id which isn't a macro call. We'll assume
4592 * it might be a label; we'll also check to see if a
4593 * colon follows it. Then, if there's another id after
4594 * that lot, we'll check it again for macro-hood.
4595 */
4596 label = last = t;
4597 t = t->next;
4598 if (tok_type_(t, TOK_WHITESPACE))
4599 last = t, t = t->next;
4600 if (tok_is_(t, ":")) {
4601 dont_prepend = 1;
4602 last = t, t = t->next;
4603 if (tok_type_(t, TOK_WHITESPACE))
4604 last = t, t = t->next;
4605 }
H. Peter Anvin89cee572009-07-15 09:16:54 -04004606 if (!tok_type_(t, TOK_ID) || !(m = is_mmacro(t, &params)))
H. Peter Anvine2c80182005-01-15 22:15:51 +00004607 return 0;
4608 last->next = NULL;
Keith Kanios891775e2009-07-11 06:08:54 -05004609 mname = t->text;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004610 tline = t;
H. Peter Anvineba20a72002-04-30 20:53:55 +00004611 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004612
4613 /*
4614 * Fix up the parameters: this involves stripping leading and
4615 * trailing whitespace, then stripping braces if they are
4616 * present.
4617 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004618 for (nparam = 0; params[nparam]; nparam++) ;
H. Peter Anvin734b1882002-04-30 21:01:08 +00004619 paramlen = nparam ? nasm_malloc(nparam * sizeof(*paramlen)) : NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004620
H. Peter Anvine2c80182005-01-15 22:15:51 +00004621 for (i = 0; params[i]; i++) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07004622 int brace = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004623 int comma = (!m->plus || i < nparam - 1);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004624
H. Peter Anvine2c80182005-01-15 22:15:51 +00004625 t = params[i];
4626 skip_white_(t);
4627 if (tok_is_(t, "{"))
H. Peter Anvin6867acc2007-10-10 14:58:45 -07004628 t = t->next, brace = true, comma = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004629 params[i] = t;
4630 paramlen[i] = 0;
4631 while (t) {
4632 if (comma && t->type == TOK_OTHER && !strcmp(t->text, ","))
4633 break; /* ... because we have hit a comma */
4634 if (comma && t->type == TOK_WHITESPACE
4635 && tok_is_(t->next, ","))
4636 break; /* ... or a space then a comma */
4637 if (brace && t->type == TOK_OTHER && !strcmp(t->text, "}"))
4638 break; /* ... or a brace */
4639 t = t->next;
4640 paramlen[i]++;
4641 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004642 }
4643
4644 /*
4645 * OK, we have a MMacro structure together with a set of
4646 * parameters. We must now go through the expansion and push
H. Peter Anvin76690a12002-04-30 20:52:49 +00004647 * copies of each Line on to istk->expansion. Substitution of
4648 * parameter tokens and macro-local tokens doesn't get done
4649 * until the single-line macro substitution process; this is
4650 * because delaying them allows us to change the semantics
4651 * later through %rotate.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004652 *
H. Peter Anvin76690a12002-04-30 20:52:49 +00004653 * First, push an end marker on to istk->expansion, mark this
4654 * macro as in progress, and set up its invocation-specific
4655 * variables.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004656 */
4657 ll = nasm_malloc(sizeof(Line));
4658 ll->next = istk->expansion;
4659 ll->finishes = m;
4660 ll->first = NULL;
4661 istk->expansion = ll;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004662
H. Peter Anvin89cee572009-07-15 09:16:54 -04004663 /*
4664 * Save the previous MMacro expansion in the case of
4665 * macro recursion
4666 */
4667 if (m->max_depth && m->in_progress)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004668 push_mmacro(m);
H. Peter Anvin76690a12002-04-30 20:52:49 +00004669
Keith Kanios891775e2009-07-11 06:08:54 -05004670 m->in_progress ++;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004671 m->params = params;
4672 m->iline = tline;
4673 m->nparam = nparam;
4674 m->rotate = 0;
4675 m->paramlen = paramlen;
4676 m->unique = unique++;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004677 m->lineno = 0;
Keith Kanios3c0d91f2009-10-25 13:28:03 -05004678 m->condcnt = 0;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004679
4680 m->next_active = istk->mstk;
4681 istk->mstk = m;
4682
Cyrill Gorcunov367d59e2010-04-09 15:43:03 +04004683 list_for_each(l, m->expansion) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00004684 Token **tail;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004685
H. Peter Anvine2c80182005-01-15 22:15:51 +00004686 ll = nasm_malloc(sizeof(Line));
4687 ll->finishes = NULL;
4688 ll->next = istk->expansion;
4689 istk->expansion = ll;
4690 tail = &ll->first;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004691
Cyrill Gorcunov367d59e2010-04-09 15:43:03 +04004692 list_for_each(t, l->first) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00004693 Token *x = t;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004694 switch (t->type) {
4695 case TOK_PREPROC_Q:
4696 tt = *tail = new_Token(NULL, TOK_ID, mname, 0);
4697 break;
4698 case TOK_PREPROC_QQ:
4699 tt = *tail = new_Token(NULL, TOK_ID, m->name, 0);
4700 break;
4701 case TOK_PREPROC_ID:
4702 if (t->text[1] == '0' && t->text[2] == '0') {
4703 dont_prepend = -1;
4704 x = label;
4705 if (!x)
4706 continue;
4707 }
4708 /* fall through */
4709 default:
4710 tt = *tail = new_Token(NULL, x->type, x->text, 0);
4711 break;
4712 }
4713 tail = &tt->next;
4714 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00004715 *tail = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004716 }
4717
4718 /*
H. Peter Anvineba20a72002-04-30 20:53:55 +00004719 * If we had a label, push it on as the first line of
4720 * the macro expansion.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004721 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004722 if (label) {
4723 if (dont_prepend < 0)
4724 free_tlist(startline);
4725 else {
4726 ll = nasm_malloc(sizeof(Line));
4727 ll->finishes = NULL;
4728 ll->next = istk->expansion;
4729 istk->expansion = ll;
4730 ll->first = startline;
4731 if (!dont_prepend) {
4732 while (label->next)
4733 label = label->next;
4734 label->next = tt = new_Token(NULL, TOK_OTHER, ":", 0);
4735 }
4736 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004737 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004738
H. Peter Anvin734b1882002-04-30 21:01:08 +00004739 list->uplevel(m->nolist ? LIST_MACRO_NOLIST : LIST_MACRO);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004740
H. Peter Anvineba20a72002-04-30 20:53:55 +00004741 return 1;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004742}
4743
Victor van den Elzen3b404c02008-09-18 13:51:36 +02004744/* The function that actually does the error reporting */
4745static void verror(int severity, const char *fmt, va_list arg)
4746{
4747 char buff[1024];
Cyrill Gorcunov4a350082010-09-24 15:24:42 +04004748 MMacro *mmac = NULL;
4749 int delta = 0;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02004750
4751 vsnprintf(buff, sizeof(buff), fmt, arg);
4752
Cyrill Gorcunov4a350082010-09-24 15:24:42 +04004753 /* get %macro name */
4754 if (istk && istk->mstk) {
4755 mmac = istk->mstk;
4756 /* but %rep blocks should be skipped */
4757 while (mmac && !mmac->name)
4758 mmac = mmac->next_active, delta++;
4759 }
4760
4761 if (mmac)
4762 nasm_error(severity, "(%s:%d) %s",
4763 mmac->name, mmac->lineno - delta, buff);
Victor van den Elzen3b404c02008-09-18 13:51:36 +02004764 else
H. Peter Anvindbb640b2009-07-18 18:57:16 -07004765 nasm_error(severity, "%s", buff);
Victor van den Elzen3b404c02008-09-18 13:51:36 +02004766}
4767
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004768/*
4769 * Since preprocessor always operate only on the line that didn't
H. Peter Anvin917a3492008-09-24 09:14:49 -07004770 * arrived yet, we should always use ERR_OFFBY1.
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004771 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004772static void error(int severity, const char *fmt, ...)
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004773{
4774 va_list arg;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004775
4776 /* If we're in a dead branch of IF or something like it, ignore the error */
H. Peter Anvin77ba0c62002-05-14 03:18:53 +00004777 if (istk && istk->conds && !emitting(istk->conds->state))
H. Peter Anvine2c80182005-01-15 22:15:51 +00004778 return;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004779
H. Peter Anvin734b1882002-04-30 21:01:08 +00004780 va_start(arg, fmt);
Victor van den Elzen3b404c02008-09-18 13:51:36 +02004781 verror(severity, fmt, arg);
H. Peter Anvin734b1882002-04-30 21:01:08 +00004782 va_end(arg);
Victor van den Elzen3b404c02008-09-18 13:51:36 +02004783}
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004784
Victor van den Elzen3b404c02008-09-18 13:51:36 +02004785/*
4786 * Because %else etc are evaluated in the state context
4787 * of the previous branch, errors might get lost with error():
4788 * %if 0 ... %else trailing garbage ... %endif
4789 * So %else etc should report errors with this function.
4790 */
4791static void error_precond(int severity, const char *fmt, ...)
4792{
4793 va_list arg;
4794
4795 /* Only ignore the error if it's really in a dead branch */
4796 if (istk && istk->conds && istk->conds->state == COND_NEVER)
4797 return;
4798
4799 va_start(arg, fmt);
4800 verror(severity, fmt, arg);
4801 va_end(arg);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004802}
4803
H. Peter Anvin734b1882002-04-30 21:01:08 +00004804static void
H. Peter Anvindbb640b2009-07-18 18:57:16 -07004805pp_reset(char *file, int apass, ListGen * listgen, StrList **deplist)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004806{
H. Peter Anvin7383b402008-09-24 10:20:40 -07004807 Token *t;
4808
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004809 cstk = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004810 istk = nasm_malloc(sizeof(Include));
4811 istk->next = NULL;
4812 istk->conds = NULL;
4813 istk->expansion = NULL;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004814 istk->mstk = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004815 istk->fp = fopen(file, "r");
H. Peter Anvineba20a72002-04-30 20:53:55 +00004816 istk->fname = NULL;
4817 src_set_fname(nasm_strdup(file));
4818 src_set_linnum(0);
4819 istk->lineinc = 1;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004820 if (!istk->fp)
H. Peter Anvin917a3492008-09-24 09:14:49 -07004821 error(ERR_FATAL|ERR_NOFILE, "unable to open input file `%s'",
H. Peter Anvine2c80182005-01-15 22:15:51 +00004822 file);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004823 defining = NULL;
Charles Crayned4200be2008-07-12 16:42:33 -07004824 nested_mac_count = 0;
4825 nested_rep_count = 0;
H. Peter Anvin97a23472007-09-16 17:57:25 -07004826 init_macros();
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004827 unique = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004828 if (tasm_compatible_mode) {
H. Peter Anvina4835d42008-05-20 14:21:29 -07004829 stdmacpos = nasm_stdmac;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004830 } else {
H. Peter Anvina4835d42008-05-20 14:21:29 -07004831 stdmacpos = nasm_stdmac_after_tasm;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004832 }
H. Peter Anvind2456592008-06-19 15:04:18 -07004833 any_extrastdmac = extrastdmac && *extrastdmac;
4834 do_predef = true;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004835 list = listgen;
H. Peter Anvin61f130f2008-09-25 15:45:06 -07004836
4837 /*
4838 * 0 for dependencies, 1 for preparatory passes, 2 for final pass.
4839 * The caller, however, will also pass in 3 for preprocess-only so
4840 * we can set __PASS__ accordingly.
4841 */
4842 pass = apass > 2 ? 2 : apass;
4843
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07004844 dephead = deptail = deplist;
4845 if (deplist) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004846 StrList *sl = nasm_malloc(strlen(file)+1+sizeof sl->next);
4847 sl->next = NULL;
4848 strcpy(sl->str, file);
4849 *deptail = sl;
4850 deptail = &sl->next;
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07004851 }
H. Peter Anvin7383b402008-09-24 10:20:40 -07004852
H. Peter Anvin61f130f2008-09-25 15:45:06 -07004853 /*
4854 * Define the __PASS__ macro. This is defined here unlike
4855 * all the other builtins, because it is special -- it varies between
4856 * passes.
4857 */
H. Peter Anvin7383b402008-09-24 10:20:40 -07004858 t = nasm_malloc(sizeof(*t));
4859 t->next = NULL;
H. Peter Anvin61f130f2008-09-25 15:45:06 -07004860 make_tok_num(t, apass);
H. Peter Anvin7383b402008-09-24 10:20:40 -07004861 t->a.mac = NULL;
4862 define_smacro(NULL, "__PASS__", true, 0, t);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004863}
4864
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004865static char *pp_getline(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004866{
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004867 char *line;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004868 Token *tline;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004869
H. Peter Anvine2c80182005-01-15 22:15:51 +00004870 while (1) {
4871 /*
Keith Kaniosb7a89542007-04-12 02:40:54 +00004872 * Fetch a tokenized line, either from the macro-expansion
H. Peter Anvine2c80182005-01-15 22:15:51 +00004873 * buffer or from the input file.
4874 */
4875 tline = NULL;
4876 while (istk->expansion && istk->expansion->finishes) {
4877 Line *l = istk->expansion;
4878 if (!l->finishes->name && l->finishes->in_progress > 1) {
4879 Line *ll;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004880
H. Peter Anvine2c80182005-01-15 22:15:51 +00004881 /*
4882 * This is a macro-end marker for a macro with no
4883 * name, which means it's not really a macro at all
4884 * but a %rep block, and the `in_progress' field is
4885 * more than 1, meaning that we still need to
4886 * repeat. (1 means the natural last repetition; 0
4887 * means termination by %exitrep.) We have
4888 * therefore expanded up to the %endrep, and must
4889 * push the whole block on to the expansion buffer
4890 * again. We don't bother to remove the macro-end
4891 * marker: we'd only have to generate another one
4892 * if we did.
4893 */
4894 l->finishes->in_progress--;
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04004895 list_for_each(l, l->finishes->expansion) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00004896 Token *t, *tt, **tail;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004897
H. Peter Anvine2c80182005-01-15 22:15:51 +00004898 ll = nasm_malloc(sizeof(Line));
4899 ll->next = istk->expansion;
4900 ll->finishes = NULL;
4901 ll->first = NULL;
4902 tail = &ll->first;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004903
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04004904 list_for_each(t, l->first) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00004905 if (t->text || t->type == TOK_WHITESPACE) {
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04004906 tt = *tail = new_Token(NULL, t->type, t->text, 0);
H. Peter Anvine2c80182005-01-15 22:15:51 +00004907 tail = &tt->next;
4908 }
4909 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00004910
H. Peter Anvine2c80182005-01-15 22:15:51 +00004911 istk->expansion = ll;
4912 }
4913 } else {
4914 /*
4915 * Check whether a `%rep' was started and not ended
4916 * within this macro expansion. This can happen and
4917 * should be detected. It's a fatal error because
4918 * I'm too confused to work out how to recover
4919 * sensibly from it.
4920 */
4921 if (defining) {
4922 if (defining->name)
4923 error(ERR_PANIC,
4924 "defining with name in expansion");
4925 else if (istk->mstk->name)
4926 error(ERR_FATAL,
4927 "`%%rep' without `%%endrep' within"
4928 " expansion of macro `%s'",
4929 istk->mstk->name);
4930 }
H. Peter Anvin87bc6192002-04-30 20:53:16 +00004931
H. Peter Anvine2c80182005-01-15 22:15:51 +00004932 /*
4933 * FIXME: investigate the relationship at this point between
4934 * istk->mstk and l->finishes
4935 */
4936 {
4937 MMacro *m = istk->mstk;
4938 istk->mstk = m->next_active;
4939 if (m->name) {
4940 /*
4941 * This was a real macro call, not a %rep, and
4942 * therefore the parameter information needs to
4943 * be freed.
4944 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004945 if (m->prev) {
4946 pop_mmacro(m);
4947 l->finishes->in_progress --;
4948 } else {
Keith Kanios891775e2009-07-11 06:08:54 -05004949 nasm_free(m->params);
4950 free_tlist(m->iline);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004951 nasm_free(m->paramlen);
4952 l->finishes->in_progress = 0;
4953 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00004954 } else
4955 free_mmacro(m);
4956 }
4957 istk->expansion = l->next;
4958 nasm_free(l);
4959 list->downlevel(LIST_MACRO);
4960 }
4961 }
4962 while (1) { /* until we get a line we can use */
H. Peter Anvineba20a72002-04-30 20:53:55 +00004963
H. Peter Anvine2c80182005-01-15 22:15:51 +00004964 if (istk->expansion) { /* from a macro expansion */
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004965 char *p;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004966 Line *l = istk->expansion;
4967 if (istk->mstk)
4968 istk->mstk->lineno++;
4969 tline = l->first;
4970 istk->expansion = l->next;
4971 nasm_free(l);
H. Peter Anvin6867acc2007-10-10 14:58:45 -07004972 p = detoken(tline, false);
H. Peter Anvine2c80182005-01-15 22:15:51 +00004973 list->line(LIST_MACRO, p);
4974 nasm_free(p);
4975 break;
4976 }
4977 line = read_line();
4978 if (line) { /* from the current input file */
4979 line = prepreproc(line);
Keith Kaniosb7a89542007-04-12 02:40:54 +00004980 tline = tokenize(line);
H. Peter Anvine2c80182005-01-15 22:15:51 +00004981 nasm_free(line);
4982 break;
4983 }
4984 /*
4985 * The current file has ended; work down the istk
4986 */
4987 {
4988 Include *i = istk;
4989 fclose(i->fp);
Cyrill Gorcunov5ace91d2010-09-12 02:00:05 +04004990 if (i->conds) {
4991 /* nasm_error can't be conditionally suppressed */
4992 nasm_error(ERR_FATAL,
4993 "expected `%%endif' before end of file");
4994 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00004995 /* only set line and file name if there's a next node */
4996 if (i->next) {
4997 src_set_linnum(i->lineno);
4998 nasm_free(src_set_fname(i->fname));
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004999 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00005000 istk = i->next;
5001 list->downlevel(LIST_INCLUDE);
5002 nasm_free(i);
5003 if (!istk)
5004 return NULL;
Victor van den Elzen4c9d6222008-10-01 13:08:50 +02005005 if (istk->expansion && istk->expansion->finishes)
5006 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005007 }
5008 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005009
H. Peter Anvine2c80182005-01-15 22:15:51 +00005010 /*
5011 * We must expand MMacro parameters and MMacro-local labels
5012 * _before_ we plunge into directive processing, to cope
5013 * with things like `%define something %1' such as STRUC
5014 * uses. Unless we're _defining_ a MMacro, in which case
5015 * those tokens should be left alone to go into the
5016 * definition; and unless we're in a non-emitting
5017 * condition, in which case we don't want to meddle with
5018 * anything.
5019 */
Charles Crayned4200be2008-07-12 16:42:33 -07005020 if (!defining && !(istk->conds && !emitting(istk->conds->state))
H. Peter Anvin992fe752008-10-19 15:45:05 -07005021 && !(istk->mstk && !istk->mstk->in_progress)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00005022 tline = expand_mmac_params(tline);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03005023 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00005024
H. Peter Anvine2c80182005-01-15 22:15:51 +00005025 /*
5026 * Check the line to see if it's a preprocessor directive.
5027 */
5028 if (do_directive(tline) == DIRECTIVE_FOUND) {
5029 continue;
5030 } else if (defining) {
5031 /*
5032 * We're defining a multi-line macro. We emit nothing
5033 * at all, and just
Keith Kaniosb7a89542007-04-12 02:40:54 +00005034 * shove the tokenized line on to the macro definition.
H. Peter Anvine2c80182005-01-15 22:15:51 +00005035 */
5036 Line *l = nasm_malloc(sizeof(Line));
5037 l->next = defining->expansion;
5038 l->first = tline;
H. Peter Anvin538002d2008-06-28 18:30:27 -07005039 l->finishes = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005040 defining->expansion = l;
5041 continue;
5042 } else if (istk->conds && !emitting(istk->conds->state)) {
5043 /*
5044 * We're in a non-emitting branch of a condition block.
5045 * Emit nothing at all, not even a blank line: when we
5046 * emerge from the condition we'll give a line-number
5047 * directive so we keep our place correctly.
5048 */
5049 free_tlist(tline);
5050 continue;
5051 } else if (istk->mstk && !istk->mstk->in_progress) {
5052 /*
5053 * We're in a %rep block which has been terminated, so
5054 * we're walking through to the %endrep without
5055 * emitting anything. Emit nothing at all, not even a
5056 * blank line: when we emerge from the %rep block we'll
5057 * give a line-number directive so we keep our place
5058 * correctly.
5059 */
5060 free_tlist(tline);
5061 continue;
5062 } else {
5063 tline = expand_smacro(tline);
5064 if (!expand_mmacro(tline)) {
5065 /*
Keith Kaniosb7a89542007-04-12 02:40:54 +00005066 * De-tokenize the line again, and emit it.
H. Peter Anvine2c80182005-01-15 22:15:51 +00005067 */
H. Peter Anvin6867acc2007-10-10 14:58:45 -07005068 line = detoken(tline, true);
H. Peter Anvine2c80182005-01-15 22:15:51 +00005069 free_tlist(tline);
5070 break;
5071 } else {
5072 continue; /* expand_mmacro calls free_tlist */
5073 }
5074 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005075 }
5076
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005077 return line;
5078}
5079
H. Peter Anvine2c80182005-01-15 22:15:51 +00005080static void pp_cleanup(int pass)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005081{
H. Peter Anvine2c80182005-01-15 22:15:51 +00005082 if (defining) {
H. Peter Anvin89cee572009-07-15 09:16:54 -04005083 if (defining->name) {
Victor van den Elzen8f1120f2008-07-16 13:41:37 +02005084 error(ERR_NONFATAL,
5085 "end of file while still defining macro `%s'",
5086 defining->name);
5087 } else {
5088 error(ERR_NONFATAL, "end of file while still in %%rep");
5089 }
5090
H. Peter Anvine2c80182005-01-15 22:15:51 +00005091 free_mmacro(defining);
Cyrill Gorcunova327c652010-02-14 17:19:38 +03005092 defining = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005093 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005094 while (cstk)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005095 ctx_pop();
H. Peter Anvin97a23472007-09-16 17:57:25 -07005096 free_macros();
H. Peter Anvine2c80182005-01-15 22:15:51 +00005097 while (istk) {
5098 Include *i = istk;
5099 istk = istk->next;
5100 fclose(i->fp);
5101 nasm_free(i->fname);
5102 nasm_free(i);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005103 }
5104 while (cstk)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005105 ctx_pop();
H. Peter Anvin86877b22008-06-20 15:55:45 -07005106 nasm_free(src_set_fname(NULL));
H. Peter Anvine2c80182005-01-15 22:15:51 +00005107 if (pass == 0) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03005108 IncPath *i;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005109 free_llist(predef);
5110 delete_Blocks();
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03005111 while ((i = ipath)) {
5112 ipath = i->next;
5113 if (i->path)
5114 nasm_free(i->path);
5115 nasm_free(i);
5116 }
H. Peter Anvin11dfa1a2008-07-02 18:11:04 -07005117 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005118}
5119
Keith Kaniosa6dfa782007-04-13 16:47:53 +00005120void pp_include_path(char *path)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005121{
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005122 IncPath *i;
H. Peter Anvin37a321f2007-09-24 13:41:58 -07005123
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005124 i = nasm_malloc(sizeof(IncPath));
H. Peter Anvin37a321f2007-09-24 13:41:58 -07005125 i->path = path ? nasm_strdup(path) : NULL;
Frank Kotlerd0ed6fd2003-08-27 11:33:56 +00005126 i->next = NULL;
5127
H. Peter Anvin89cee572009-07-15 09:16:54 -04005128 if (ipath) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00005129 IncPath *j = ipath;
H. Peter Anvin89cee572009-07-15 09:16:54 -04005130 while (j->next)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005131 j = j->next;
5132 j->next = i;
5133 } else {
5134 ipath = i;
Frank Kotlerd0ed6fd2003-08-27 11:33:56 +00005135 }
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005136}
Frank Kotlerd0ed6fd2003-08-27 11:33:56 +00005137
Keith Kaniosa6dfa782007-04-13 16:47:53 +00005138void pp_pre_include(char *fname)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005139{
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005140 Token *inc, *space, *name;
5141 Line *l;
5142
H. Peter Anvin734b1882002-04-30 21:01:08 +00005143 name = new_Token(NULL, TOK_INTERNAL_STRING, fname, 0);
5144 space = new_Token(name, TOK_WHITESPACE, NULL, 0);
5145 inc = new_Token(space, TOK_PREPROC_ID, "%include", 0);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005146
5147 l = nasm_malloc(sizeof(Line));
5148 l->next = predef;
5149 l->first = inc;
H. Peter Anvin538002d2008-06-28 18:30:27 -07005150 l->finishes = NULL;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005151 predef = l;
5152}
5153
Keith Kaniosa6dfa782007-04-13 16:47:53 +00005154void pp_pre_define(char *definition)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005155{
5156 Token *def, *space;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005157 Line *l;
Keith Kaniosa6dfa782007-04-13 16:47:53 +00005158 char *equals;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005159
5160 equals = strchr(definition, '=');
H. Peter Anvin734b1882002-04-30 21:01:08 +00005161 space = new_Token(NULL, TOK_WHITESPACE, NULL, 0);
5162 def = new_Token(space, TOK_PREPROC_ID, "%define", 0);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005163 if (equals)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005164 *equals = ' ';
Keith Kaniosb7a89542007-04-12 02:40:54 +00005165 space->next = tokenize(definition);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005166 if (equals)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005167 *equals = '=';
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005168
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005169 l = nasm_malloc(sizeof(Line));
5170 l->next = predef;
5171 l->first = def;
H. Peter Anvin538002d2008-06-28 18:30:27 -07005172 l->finishes = NULL;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005173 predef = l;
5174}
5175
Keith Kaniosa6dfa782007-04-13 16:47:53 +00005176void pp_pre_undefine(char *definition)
H. Peter Anvin620515a2002-04-30 20:57:38 +00005177{
5178 Token *def, *space;
5179 Line *l;
5180
H. Peter Anvin734b1882002-04-30 21:01:08 +00005181 space = new_Token(NULL, TOK_WHITESPACE, NULL, 0);
5182 def = new_Token(space, TOK_PREPROC_ID, "%undef", 0);
Keith Kaniosb7a89542007-04-12 02:40:54 +00005183 space->next = tokenize(definition);
H. Peter Anvin620515a2002-04-30 20:57:38 +00005184
5185 l = nasm_malloc(sizeof(Line));
5186 l->next = predef;
5187 l->first = def;
H. Peter Anvin538002d2008-06-28 18:30:27 -07005188 l->finishes = NULL;
H. Peter Anvin620515a2002-04-30 20:57:38 +00005189 predef = l;
5190}
5191
Keith Kaniosb7a89542007-04-12 02:40:54 +00005192/*
5193 * Added by Keith Kanios:
5194 *
5195 * This function is used to assist with "runtime" preprocessor
5196 * directives. (e.g. pp_runtime("%define __BITS__ 64");)
5197 *
5198 * ERRORS ARE IGNORED HERE, SO MAKE COMPLETELY SURE THAT YOU
5199 * PASS A VALID STRING TO THIS FUNCTION!!!!!
5200 */
5201
Keith Kaniosa6dfa782007-04-13 16:47:53 +00005202void pp_runtime(char *definition)
Keith Kaniosb7a89542007-04-12 02:40:54 +00005203{
5204 Token *def;
H. Peter Anvin70653092007-10-19 14:42:29 -07005205
Keith Kaniosb7a89542007-04-12 02:40:54 +00005206 def = tokenize(definition);
H. Peter Anvin89cee572009-07-15 09:16:54 -04005207 if (do_directive(def) == NO_DIRECTIVE_FOUND)
Keith Kaniosb7a89542007-04-12 02:40:54 +00005208 free_tlist(def);
H. Peter Anvin70653092007-10-19 14:42:29 -07005209
Keith Kaniosb7a89542007-04-12 02:40:54 +00005210}
5211
H. Peter Anvina70547f2008-07-19 21:44:26 -07005212void pp_extra_stdmac(macros_t *macros)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005213{
H. Peter Anvin76690a12002-04-30 20:52:49 +00005214 extrastdmac = macros;
5215}
5216
Keith Kaniosa5fc6462007-10-13 07:09:22 -07005217static void make_tok_num(Token * tok, int64_t val)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005218{
Keith Kaniosa6dfa782007-04-13 16:47:53 +00005219 char numbuf[20];
Keith Kaniosa5fc6462007-10-13 07:09:22 -07005220 snprintf(numbuf, sizeof(numbuf), "%"PRId64"", val);
H. Peter Anvineba20a72002-04-30 20:53:55 +00005221 tok->text = nasm_strdup(numbuf);
5222 tok->type = TOK_NUMBER;
5223}
5224
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005225Preproc nasmpp = {
5226 pp_reset,
5227 pp_getline,
5228 pp_cleanup
5229};