blob: 186e7244dbcca5191bc149b7aa8550520bb8cff7 [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/*
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300491 * Handle TASM specific directives, which do not contain a % in
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000492 * front of them. We do it here because I could not find any other
493 * place to do it for the moment, and it is a hack (ideally it would
494 * be nice to be able to use the NASM pre-processor to do it).
495 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000496static char *check_tasm_directive(char *line)
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000497{
Keith Kaniosb7a89542007-04-12 02:40:54 +0000498 int32_t i, j, k, m, len;
Cyrill Gorcunovf66ac7d2009-10-12 20:41:13 +0400499 char *p, *q, *oldline, oldchar;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000500
Cyrill Gorcunovf66ac7d2009-10-12 20:41:13 +0400501 p = nasm_skip_spaces(line);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000502
503 /* Binary search for the directive name */
504 i = -1;
Cyrill Gorcunova7319242010-06-03 22:04:36 +0400505 j = ARRAY_SIZE(tasm_directives);
Cyrill Gorcunovf66ac7d2009-10-12 20:41:13 +0400506 q = nasm_skip_word(p);
507 len = q - p;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000508 if (len) {
509 oldchar = p[len];
510 p[len] = 0;
511 while (j - i > 1) {
512 k = (j + i) / 2;
513 m = nasm_stricmp(p, tasm_directives[k]);
514 if (m == 0) {
515 /* We have found a directive, so jam a % in front of it
516 * so that NASM will then recognise it as one if it's own.
517 */
518 p[len] = oldchar;
519 len = strlen(p);
520 oldline = line;
521 line = nasm_malloc(len + 2);
522 line[0] = '%';
523 if (k == TM_IFDIFI) {
H. Peter Anvin18f48792009-06-27 15:56:27 -0700524 /*
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300525 * NASM does not recognise IFDIFI, so we convert
526 * it to %if 0. This is not used in NASM
527 * compatible code, but does need to parse for the
528 * TASM macro package.
H. Peter Anvine2c80182005-01-15 22:15:51 +0000529 */
H. Peter Anvin18f48792009-06-27 15:56:27 -0700530 strcpy(line + 1, "if 0");
H. Peter Anvine2c80182005-01-15 22:15:51 +0000531 } else {
532 memcpy(line + 1, p, len + 1);
533 }
534 nasm_free(oldline);
535 return line;
536 } else if (m < 0) {
537 j = k;
538 } else
539 i = k;
540 }
541 p[len] = oldchar;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000542 }
543 return line;
544}
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000545
H. Peter Anvin76690a12002-04-30 20:52:49 +0000546/*
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000547 * The pre-preprocessing stage... This function translates line
548 * number indications as they emerge from GNU cpp (`# lineno "file"
549 * flags') into NASM preprocessor line number indications (`%line
550 * lineno file').
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000551 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000552static char *prepreproc(char *line)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000553{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000554 int lineno, fnlen;
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000555 char *fname, *oldline;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000556
H. Peter Anvine2c80182005-01-15 22:15:51 +0000557 if (line[0] == '#' && line[1] == ' ') {
558 oldline = line;
559 fname = oldline + 2;
560 lineno = atoi(fname);
561 fname += strspn(fname, "0123456789 ");
562 if (*fname == '"')
563 fname++;
564 fnlen = strcspn(fname, "\"");
565 line = nasm_malloc(20 + fnlen);
566 snprintf(line, 20 + fnlen, "%%line %d %.*s", lineno, fnlen, fname);
567 nasm_free(oldline);
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000568 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000569 if (tasm_compatible_mode)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000570 return check_tasm_directive(line);
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000571 return line;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000572}
573
574/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000575 * Free a linked list of tokens.
576 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000577static void free_tlist(Token * list)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000578{
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +0400579 while (list)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000580 list = delete_Token(list);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000581}
582
583/*
584 * Free a linked list of lines.
585 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000586static void free_llist(Line * list)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000587{
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +0400588 Line *l, *tmp;
589 list_for_each_safe(l, tmp, list) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000590 free_tlist(l->first);
591 nasm_free(l);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000592 }
593}
594
595/*
H. Peter Anvineba20a72002-04-30 20:53:55 +0000596 * Free an MMacro
597 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000598static void free_mmacro(MMacro * m)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000599{
H. Peter Anvin734b1882002-04-30 21:01:08 +0000600 nasm_free(m->name);
601 free_tlist(m->dlist);
602 nasm_free(m->defaults);
603 free_llist(m->expansion);
604 nasm_free(m);
H. Peter Anvineba20a72002-04-30 20:53:55 +0000605}
606
607/*
H. Peter Anvin97a23472007-09-16 17:57:25 -0700608 * Free all currently defined macros, and free the hash tables
609 */
H. Peter Anvin072771e2008-05-22 13:17:51 -0700610static void free_smacro_table(struct hash_table *smt)
H. Peter Anvin97a23472007-09-16 17:57:25 -0700611{
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +0400612 SMacro *s, *tmp;
H. Peter Anvin072771e2008-05-22 13:17:51 -0700613 const char *key;
614 struct hash_tbl_node *it = NULL;
H. Peter Anvin97a23472007-09-16 17:57:25 -0700615
H. Peter Anvin072771e2008-05-22 13:17:51 -0700616 while ((s = hash_iterate(smt, &it, &key)) != NULL) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300617 nasm_free((void *)key);
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +0400618 list_for_each_safe(s, tmp, s) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300619 nasm_free(s->name);
620 free_tlist(s->expansion);
621 nasm_free(s);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300622 }
H. Peter Anvin97a23472007-09-16 17:57:25 -0700623 }
H. Peter Anvin072771e2008-05-22 13:17:51 -0700624 hash_free(smt);
625}
626
627static void free_mmacro_table(struct hash_table *mmt)
628{
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +0400629 MMacro *m, *tmp;
H. Peter Anvin072771e2008-05-22 13:17:51 -0700630 const char *key;
631 struct hash_tbl_node *it = NULL;
H. Peter Anvin97a23472007-09-16 17:57:25 -0700632
633 it = NULL;
H. Peter Anvin072771e2008-05-22 13:17:51 -0700634 while ((m = hash_iterate(mmt, &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(m ,tmp, m)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300637 free_mmacro(m);
H. Peter Anvin97a23472007-09-16 17:57:25 -0700638 }
H. Peter Anvin072771e2008-05-22 13:17:51 -0700639 hash_free(mmt);
640}
641
642static void free_macros(void)
643{
H. Peter Anvin166c2472008-05-28 12:28:58 -0700644 free_smacro_table(&smacros);
645 free_mmacro_table(&mmacros);
H. Peter Anvin97a23472007-09-16 17:57:25 -0700646}
647
648/*
649 * Initialize the hash tables
650 */
651static void init_macros(void)
652{
H. Peter Anvin166c2472008-05-28 12:28:58 -0700653 hash_init(&smacros, HASH_LARGE);
654 hash_init(&mmacros, HASH_LARGE);
H. Peter Anvin97a23472007-09-16 17:57:25 -0700655}
656
657/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000658 * Pop the context stack.
659 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000660static void ctx_pop(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000661{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000662 Context *c = cstk;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000663
664 cstk = cstk->next;
H. Peter Anvin166c2472008-05-28 12:28:58 -0700665 free_smacro_table(&c->localmac);
H. Peter Anvin734b1882002-04-30 21:01:08 +0000666 nasm_free(c->name);
667 nasm_free(c);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000668}
669
H. Peter Anvin072771e2008-05-22 13:17:51 -0700670/*
671 * Search for a key in the hash index; adding it if necessary
672 * (in which case we initialize the data pointer to NULL.)
673 */
674static void **
675hash_findi_add(struct hash_table *hash, const char *str)
676{
677 struct hash_insert hi;
678 void **r;
679 char *strx;
680
681 r = hash_findi(hash, str, &hi);
682 if (r)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300683 return r;
H. Peter Anvin072771e2008-05-22 13:17:51 -0700684
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300685 strx = nasm_strdup(str); /* Use a more efficient allocator here? */
H. Peter Anvin072771e2008-05-22 13:17:51 -0700686 return hash_add(&hi, strx, NULL);
687}
688
689/*
690 * Like hash_findi, but returns the data element rather than a pointer
691 * to it. Used only when not adding a new element, hence no third
692 * argument.
693 */
694static void *
695hash_findix(struct hash_table *hash, const char *str)
696{
697 void **p;
698
699 p = hash_findi(hash, str, NULL);
700 return p ? *p : NULL;
701}
702
Cyrill Gorcunov15bdc512010-07-13 11:27:41 +0400703/*
704 * read line from standart macros set,
705 * if there no more left -- return NULL
706 */
707static char *line_from_stdmac(void)
708{
709 unsigned char c;
710 const unsigned char *p = stdmacpos;
711 char *line, *q;
712 size_t len = 0;
713
714 if (!stdmacpos)
715 return NULL;
716
717 while ((c = *p++)) {
718 if (c >= 0x80)
719 len += pp_directives_len[c - 0x80] + 1;
720 else
721 len++;
722 }
723
724 line = nasm_malloc(len + 1);
725 q = line;
726 while ((c = *stdmacpos++)) {
727 if (c >= 0x80) {
728 memcpy(q, pp_directives[c - 0x80], pp_directives_len[c - 0x80]);
729 q += pp_directives_len[c - 0x80];
730 *q++ = ' ';
731 } else {
732 *q++ = c;
733 }
734 }
735 stdmacpos = p;
736 *q = '\0';
737
738 if (!*stdmacpos) {
739 /* This was the last of the standard macro chain... */
740 stdmacpos = NULL;
741 if (any_extrastdmac) {
742 stdmacpos = extrastdmac;
743 any_extrastdmac = false;
744 } else if (do_predef) {
745 Line *pd, *l;
746 Token *head, **tail, *t;
747
748 /*
749 * Nasty hack: here we push the contents of
750 * `predef' on to the top-level expansion stack,
751 * since this is the most convenient way to
752 * implement the pre-include and pre-define
753 * features.
754 */
755 list_for_each(pd, predef) {
756 head = NULL;
757 tail = &head;
758 list_for_each(t, pd->first) {
759 *tail = new_Token(NULL, t->type, t->text, 0);
760 tail = &(*tail)->next;
761 }
762
763 l = nasm_malloc(sizeof(Line));
764 l->next = istk->expansion;
765 l->first = head;
766 l->finishes = NULL;
767
768 istk->expansion = l;
769 }
770 do_predef = false;
771 }
772 }
773
774 return line;
775}
776
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000777#define BUF_DELTA 512
778/*
779 * Read a line from the top file in istk, handling multiple CR/LFs
780 * at the end of the line read, and handling spurious ^Zs. Will
781 * return lines from the standard macro set if this has not already
782 * been done.
783 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000784static char *read_line(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000785{
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000786 char *buffer, *p, *q;
H. Peter Anvin9f394642002-04-30 21:07:51 +0000787 int bufsize, continued_count;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000788
Cyrill Gorcunov15bdc512010-07-13 11:27:41 +0400789 /*
790 * standart macros set (predefined) goes first
791 */
792 p = line_from_stdmac();
793 if (p)
794 return p;
H. Peter Anvin72edbb82008-06-19 16:00:04 -0700795
Cyrill Gorcunov15bdc512010-07-13 11:27:41 +0400796 /*
797 * regular read from a file
798 */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000799 bufsize = BUF_DELTA;
800 buffer = nasm_malloc(BUF_DELTA);
801 p = buffer;
H. Peter Anvin9f394642002-04-30 21:07:51 +0000802 continued_count = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000803 while (1) {
804 q = fgets(p, bufsize - (p - buffer), istk->fp);
805 if (!q)
806 break;
807 p += strlen(p);
808 if (p > buffer && p[-1] == '\n') {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300809 /*
810 * Convert backslash-CRLF line continuation sequences into
811 * nothing at all (for DOS and Windows)
812 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000813 if (((p - 2) > buffer) && (p[-3] == '\\') && (p[-2] == '\r')) {
814 p -= 3;
815 *p = 0;
816 continued_count++;
817 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300818 /*
819 * Also convert backslash-LF line continuation sequences into
820 * nothing at all (for Unix)
821 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000822 else if (((p - 1) > buffer) && (p[-2] == '\\')) {
823 p -= 2;
824 *p = 0;
825 continued_count++;
826 } else {
827 break;
828 }
829 }
830 if (p - buffer > bufsize - 10) {
Keith Kaniosb7a89542007-04-12 02:40:54 +0000831 int32_t offset = p - buffer;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000832 bufsize += BUF_DELTA;
833 buffer = nasm_realloc(buffer, bufsize);
834 p = buffer + offset; /* prevent stale-pointer problems */
835 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000836 }
837
H. Peter Anvine2c80182005-01-15 22:15:51 +0000838 if (!q && p == buffer) {
839 nasm_free(buffer);
840 return NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000841 }
842
H. Peter Anvine2c80182005-01-15 22:15:51 +0000843 src_set_linnum(src_get_linnum() + istk->lineinc +
844 (continued_count * istk->lineinc));
H. Peter Anvineba20a72002-04-30 20:53:55 +0000845
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000846 /*
847 * Play safe: remove CRs as well as LFs, if any of either are
848 * present at the end of the line.
849 */
H. Peter Anvineba20a72002-04-30 20:53:55 +0000850 while (--p >= buffer && (*p == '\n' || *p == '\r'))
H. Peter Anvine2c80182005-01-15 22:15:51 +0000851 *p = '\0';
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000852
853 /*
854 * Handle spurious ^Z, which may be inserted into source files
855 * by some file transfer utilities.
856 */
857 buffer[strcspn(buffer, "\032")] = '\0';
858
H. Peter Anvin734b1882002-04-30 21:01:08 +0000859 list->line(LIST_READ, buffer);
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000860
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000861 return buffer;
862}
863
864/*
Keith Kaniosb7a89542007-04-12 02:40:54 +0000865 * Tokenize a line of text. This is a very simple process since we
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000866 * don't need to parse the value out of e.g. numeric tokens: we
867 * simply split one string into many.
868 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000869static Token *tokenize(char *line)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000870{
H. Peter Anvinca544db2008-10-19 19:30:11 -0700871 char c, *p = line;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +0000872 enum pp_token_type type;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000873 Token *list = NULL;
874 Token *t, **tail = &list;
875
H. Peter Anvine2c80182005-01-15 22:15:51 +0000876 while (*line) {
877 p = line;
878 if (*p == '%') {
879 p++;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300880 if (*p == '+' && !nasm_isdigit(p[1])) {
881 p++;
882 type = TOK_PASTE;
883 } else if (nasm_isdigit(*p) ||
884 ((*p == '-' || *p == '+') && nasm_isdigit(p[1]))) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000885 do {
886 p++;
887 }
H. Peter Anvinbda7a6e2008-06-21 10:23:17 -0700888 while (nasm_isdigit(*p));
H. Peter Anvine2c80182005-01-15 22:15:51 +0000889 type = TOK_PREPROC_ID;
890 } else if (*p == '{') {
891 p++;
892 while (*p && *p != '}') {
893 p[-1] = *p;
894 p++;
895 }
896 p[-1] = '\0';
897 if (*p)
898 p++;
899 type = TOK_PREPROC_ID;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300900 } else if (*p == '[') {
901 int lvl = 1;
902 line += 2; /* Skip the leading %[ */
903 p++;
904 while (lvl && (c = *p++)) {
905 switch (c) {
906 case ']':
907 lvl--;
908 break;
909 case '%':
910 if (*p == '[')
911 lvl++;
912 break;
913 case '\'':
914 case '\"':
915 case '`':
Cyrill Gorcunovc6360a72010-07-13 13:32:19 +0400916 p = nasm_skip_string(p - 1) + 1;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300917 break;
918 default:
919 break;
920 }
921 }
922 p--;
923 if (*p)
924 *p++ = '\0';
925 if (lvl)
926 error(ERR_NONFATAL, "unterminated %[ construct");
927 type = TOK_INDIRECT;
928 } else if (*p == '?') {
929 type = TOK_PREPROC_Q; /* %? */
930 p++;
931 if (*p == '?') {
932 type = TOK_PREPROC_QQ; /* %?? */
933 p++;
934 }
H. Peter Anvin077fb932010-07-20 14:56:30 -0700935 } else if (*p == '!') {
936 type = TOK_PREPROC_ID;
937 p++;
938 if (isidchar(*p)) {
939 do {
940 p++;
941 }
942 while (isidchar(*p));
943 } else if (*p == '\'' || *p == '\"' || *p == '`') {
944 p = nasm_skip_string(p);
945 if (*p)
946 p++;
947 else
948 error(ERR_NONFATAL|ERR_PASS1, "unterminated %! string");
949 } else {
950 /* %! without string or identifier */
951 type = TOK_OTHER; /* Legacy behavior... */
952 }
H. Peter Anvine2c80182005-01-15 22:15:51 +0000953 } else if (isidchar(*p) ||
954 ((*p == '!' || *p == '%' || *p == '$') &&
955 isidchar(p[1]))) {
956 do {
957 p++;
958 }
959 while (isidchar(*p));
960 type = TOK_PREPROC_ID;
961 } else {
962 type = TOK_OTHER;
963 if (*p == '%')
964 p++;
965 }
966 } else if (isidstart(*p) || (*p == '$' && isidstart(p[1]))) {
967 type = TOK_ID;
968 p++;
969 while (*p && isidchar(*p))
970 p++;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -0700971 } else if (*p == '\'' || *p == '"' || *p == '`') {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000972 /*
973 * A string token.
974 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000975 type = TOK_STRING;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300976 p = nasm_skip_string(p);
Nickolay Yurchenkof3b3ce22003-09-21 20:38:43 +0000977
H. Peter Anvine2c80182005-01-15 22:15:51 +0000978 if (*p) {
979 p++;
980 } else {
H. Peter Anvin917a3492008-09-24 09:14:49 -0700981 error(ERR_WARNING|ERR_PASS1, "unterminated string");
H. Peter Anvine2c80182005-01-15 22:15:51 +0000982 /* Handling unterminated strings by UNV */
983 /* type = -1; */
984 }
Victor van den Elzenfb5f2512009-04-17 16:17:59 +0200985 } else if (p[0] == '$' && p[1] == '$') {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300986 type = TOK_OTHER; /* TOKEN_BASE */
Victor van den Elzenfb5f2512009-04-17 16:17:59 +0200987 p += 2;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000988 } else if (isnumstart(*p)) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300989 bool is_hex = false;
990 bool is_float = false;
991 bool has_e = false;
992 char c, *r;
H. Peter Anvinc2df2822007-10-24 15:29:28 -0700993
H. Peter Anvine2c80182005-01-15 22:15:51 +0000994 /*
H. Peter Anvinc2df2822007-10-24 15:29:28 -0700995 * A numeric token.
H. Peter Anvine2c80182005-01-15 22:15:51 +0000996 */
H. Peter Anvinc2df2822007-10-24 15:29:28 -0700997
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300998 if (*p == '$') {
999 p++;
1000 is_hex = true;
1001 }
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001002
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001003 for (;;) {
1004 c = *p++;
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001005
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001006 if (!is_hex && (c == 'e' || c == 'E')) {
1007 has_e = true;
1008 if (*p == '+' || *p == '-') {
1009 /*
1010 * e can only be followed by +/- if it is either a
1011 * prefixed hex number or a floating-point number
1012 */
1013 p++;
1014 is_float = true;
1015 }
1016 } else if (c == 'H' || c == 'h' || c == 'X' || c == 'x') {
1017 is_hex = true;
1018 } else if (c == 'P' || c == 'p') {
1019 is_float = true;
1020 if (*p == '+' || *p == '-')
1021 p++;
1022 } else if (isnumchar(c) || c == '_')
1023 ; /* just advance */
1024 else if (c == '.') {
1025 /*
1026 * we need to deal with consequences of the legacy
1027 * parser, like "1.nolist" being two tokens
1028 * (TOK_NUMBER, TOK_ID) here; at least give it
1029 * a shot for now. In the future, we probably need
1030 * a flex-based scanner with proper pattern matching
1031 * to do it as well as it can be done. Nothing in
1032 * the world is going to help the person who wants
1033 * 0x123.p16 interpreted as two tokens, though.
1034 */
1035 r = p;
1036 while (*r == '_')
1037 r++;
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001038
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001039 if (nasm_isdigit(*r) || (is_hex && nasm_isxdigit(*r)) ||
1040 (!is_hex && (*r == 'e' || *r == 'E')) ||
1041 (*r == 'p' || *r == 'P')) {
1042 p = r;
1043 is_float = true;
1044 } else
1045 break; /* Terminate the token */
1046 } else
1047 break;
1048 }
1049 p--; /* Point to first character beyond number */
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001050
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001051 if (p == line+1 && *line == '$') {
1052 type = TOK_OTHER; /* TOKEN_HERE */
1053 } else {
1054 if (has_e && !is_hex) {
1055 /* 1e13 is floating-point, but 1e13h is not */
1056 is_float = true;
1057 }
H. Peter Anvind784a082009-04-20 14:01:18 -07001058
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001059 type = is_float ? TOK_FLOAT : TOK_NUMBER;
1060 }
H. Peter Anvinbda7a6e2008-06-21 10:23:17 -07001061 } else if (nasm_isspace(*p)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001062 type = TOK_WHITESPACE;
Cyrill Gorcunovf66ac7d2009-10-12 20:41:13 +04001063 p = nasm_skip_spaces(p);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001064 /*
1065 * Whitespace just before end-of-line is discarded by
1066 * pretending it's a comment; whitespace just before a
1067 * comment gets lumped into the comment.
1068 */
1069 if (!*p || *p == ';') {
1070 type = TOK_COMMENT;
1071 while (*p)
1072 p++;
1073 }
1074 } else if (*p == ';') {
1075 type = TOK_COMMENT;
1076 while (*p)
1077 p++;
1078 } else {
1079 /*
1080 * Anything else is an operator of some kind. We check
1081 * for all the double-character operators (>>, <<, //,
1082 * %%, <=, >=, ==, !=, <>, &&, ||, ^^), but anything
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001083 * else is a single-character operator.
H. Peter Anvine2c80182005-01-15 22:15:51 +00001084 */
1085 type = TOK_OTHER;
1086 if ((p[0] == '>' && p[1] == '>') ||
1087 (p[0] == '<' && p[1] == '<') ||
1088 (p[0] == '/' && p[1] == '/') ||
1089 (p[0] == '<' && p[1] == '=') ||
1090 (p[0] == '>' && p[1] == '=') ||
1091 (p[0] == '=' && p[1] == '=') ||
1092 (p[0] == '!' && p[1] == '=') ||
1093 (p[0] == '<' && p[1] == '>') ||
1094 (p[0] == '&' && p[1] == '&') ||
1095 (p[0] == '|' && p[1] == '|') ||
1096 (p[0] == '^' && p[1] == '^')) {
1097 p++;
1098 }
1099 p++;
1100 }
Nickolay Yurchenkof3b3ce22003-09-21 20:38:43 +00001101
H. Peter Anvine2c80182005-01-15 22:15:51 +00001102 /* Handling unterminated string by UNV */
1103 /*if (type == -1)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001104 {
1105 *tail = t = new_Token(NULL, TOK_STRING, line, p-line+1);
1106 t->text[p-line] = *line;
1107 tail = &t->next;
1108 }
1109 else */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001110 if (type != TOK_COMMENT) {
1111 *tail = t = new_Token(NULL, type, line, p - line);
1112 tail = &t->next;
1113 }
1114 line = p;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001115 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001116 return list;
1117}
1118
H. Peter Anvince616072002-04-30 21:02:23 +00001119/*
1120 * this function allocates a new managed block of memory and
H. Peter Anvin70653092007-10-19 14:42:29 -07001121 * returns a pointer to the block. The managed blocks are
H. Peter Anvince616072002-04-30 21:02:23 +00001122 * deleted only all at once by the delete_Blocks function.
1123 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001124static void *new_Block(size_t size)
H. Peter Anvince616072002-04-30 21:02:23 +00001125{
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001126 Blocks *b = &blocks;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001127
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001128 /* first, get to the end of the linked list */
1129 while (b->next)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001130 b = b->next;
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001131 /* now allocate the requested chunk */
1132 b->chunk = nasm_malloc(size);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001133
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001134 /* now allocate a new block for the next request */
1135 b->next = nasm_malloc(sizeof(Blocks));
1136 /* and initialize the contents of the new block */
1137 b->next->next = NULL;
1138 b->next->chunk = NULL;
1139 return b->chunk;
H. Peter Anvince616072002-04-30 21:02:23 +00001140}
1141
1142/*
1143 * this function deletes all managed blocks of memory
1144 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001145static void delete_Blocks(void)
H. Peter Anvince616072002-04-30 21:02:23 +00001146{
H. Peter Anvine2c80182005-01-15 22:15:51 +00001147 Blocks *a, *b = &blocks;
H. Peter Anvince616072002-04-30 21:02:23 +00001148
H. Peter Anvin70653092007-10-19 14:42:29 -07001149 /*
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001150 * keep in mind that the first block, pointed to by blocks
H. Peter Anvin70653092007-10-19 14:42:29 -07001151 * is a static and not dynamically allocated, so we don't
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001152 * free it.
1153 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001154 while (b) {
1155 if (b->chunk)
1156 nasm_free(b->chunk);
1157 a = b;
1158 b = b->next;
1159 if (a != &blocks)
1160 nasm_free(a);
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001161 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001162}
H. Peter Anvin734b1882002-04-30 21:01:08 +00001163
1164/*
H. Peter Anvin70653092007-10-19 14:42:29 -07001165 * this function creates a new Token and passes a pointer to it
H. Peter Anvin734b1882002-04-30 21:01:08 +00001166 * back to the caller. It sets the type and text elements, and
H. Peter Anvinf26e0972008-07-01 21:26:27 -07001167 * also the a.mac and next elements to NULL.
H. Peter Anvin734b1882002-04-30 21:01:08 +00001168 */
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07001169static Token *new_Token(Token * next, enum pp_token_type type,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001170 const char *text, int txtlen)
H. Peter Anvin734b1882002-04-30 21:01:08 +00001171{
1172 Token *t;
1173 int i;
1174
H. Peter Anvin89cee572009-07-15 09:16:54 -04001175 if (!freeTokens) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001176 freeTokens = (Token *) new_Block(TOKEN_BLOCKSIZE * sizeof(Token));
1177 for (i = 0; i < TOKEN_BLOCKSIZE - 1; i++)
1178 freeTokens[i].next = &freeTokens[i + 1];
1179 freeTokens[i].next = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001180 }
1181 t = freeTokens;
1182 freeTokens = t->next;
1183 t->next = next;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07001184 t->a.mac = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001185 t->type = type;
H. Peter Anvin89cee572009-07-15 09:16:54 -04001186 if (type == TOK_WHITESPACE || !text) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001187 t->text = NULL;
1188 } else {
1189 if (txtlen == 0)
1190 txtlen = strlen(text);
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07001191 t->text = nasm_malloc(txtlen+1);
1192 memcpy(t->text, text, txtlen);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001193 t->text[txtlen] = '\0';
H. Peter Anvin734b1882002-04-30 21:01:08 +00001194 }
1195 return t;
1196}
1197
H. Peter Anvine2c80182005-01-15 22:15:51 +00001198static Token *delete_Token(Token * t)
H. Peter Anvin734b1882002-04-30 21:01:08 +00001199{
1200 Token *next = t->next;
1201 nasm_free(t->text);
H. Peter Anvin788e6c12002-04-30 21:02:01 +00001202 t->next = freeTokens;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001203 freeTokens = t;
1204 return next;
1205}
1206
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001207/*
1208 * Convert a line of tokens back into text.
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001209 * If expand_locals is not zero, identifiers of the form "%$*xxx"
1210 * will be transformed into ..@ctxnum.xxx
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001211 */
H. Peter Anvin9e200162008-06-04 17:23:14 -07001212static char *detoken(Token * tlist, bool expand_locals)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001213{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001214 Token *t;
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001215 char *line, *p;
H. Peter Anvinb4daadc2008-01-21 16:31:57 -08001216 const char *q;
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001217 int len = 0;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001218
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001219 list_for_each(t, tlist) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001220 if (t->type == TOK_PREPROC_ID && t->text[1] == '!') {
H. Peter Anvin077fb932010-07-20 14:56:30 -07001221 char *v;
1222 char *q = t->text;
1223
1224 v = t->text + 2;
1225 if (*v == '\'' || *v == '\"' || *v == '`') {
1226 size_t len = nasm_unquote(v, NULL);
1227 size_t clen = strlen(v);
1228
1229 if (len != clen) {
1230 error(ERR_NONFATAL | ERR_PASS1,
1231 "NUL character in %! string");
1232 v = NULL;
1233 }
H. Peter Anvin5b00bf42010-07-13 11:43:06 -07001234 }
H. Peter Anvin077fb932010-07-20 14:56:30 -07001235
1236 if (v) {
1237 char *p = getenv(v);
1238 if (!p) {
1239 error(ERR_NONFATAL | ERR_PASS1,
1240 "nonexistent environment variable `%s'", v);
1241 p = "";
1242 }
1243 t->text = nasm_strdup(p);
1244 }
1245 nasm_free(q);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001246 }
H. Peter Anvin077fb932010-07-20 14:56:30 -07001247
H. Peter Anvine2c80182005-01-15 22:15:51 +00001248 /* Expand local macros here and not during preprocessing */
1249 if (expand_locals &&
1250 t->type == TOK_PREPROC_ID && t->text &&
1251 t->text[0] == '%' && t->text[1] == '$') {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001252 const char *q;
1253 char *p;
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001254 Context *ctx = get_ctx(t->text, &q, false);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001255 if (ctx) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001256 char buffer[40];
Keith Kanios93f2e9a2007-04-14 00:10:59 +00001257 snprintf(buffer, sizeof(buffer), "..@%"PRIu32".", ctx->number);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001258 p = nasm_strcat(buffer, q);
1259 nasm_free(t->text);
1260 t->text = p;
1261 }
1262 }
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001263 if (t->type == TOK_WHITESPACE)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001264 len++;
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001265 else if (t->text)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001266 len += strlen(t->text);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001267 }
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001268
H. Peter Anvin734b1882002-04-30 21:01:08 +00001269 p = line = nasm_malloc(len + 1);
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001270
1271 list_for_each(t, tlist) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001272 if (t->type == TOK_WHITESPACE) {
H. Peter Anvinb4daadc2008-01-21 16:31:57 -08001273 *p++ = ' ';
H. Peter Anvine2c80182005-01-15 22:15:51 +00001274 } else if (t->text) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001275 q = t->text;
1276 while (*q)
1277 *p++ = *q++;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001278 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001279 }
1280 *p = '\0';
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001281
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001282 return line;
1283}
1284
1285/*
H. Peter Anvin76690a12002-04-30 20:52:49 +00001286 * A scanner, suitable for use by the expression evaluator, which
1287 * operates on a line of Tokens. Expects a pointer to a pointer to
1288 * the first token in the line to be passed in as its private_data
1289 * field.
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001290 *
1291 * FIX: This really needs to be unified with stdscan.
H. Peter Anvin76690a12002-04-30 20:52:49 +00001292 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001293static int ppscan(void *private_data, struct tokenval *tokval)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001294{
H. Peter Anvin76690a12002-04-30 20:52:49 +00001295 Token **tlineptr = private_data;
1296 Token *tline;
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001297 char ourcopy[MAX_KEYWORD+1], *p, *r, *s;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001298
H. Peter Anvine2c80182005-01-15 22:15:51 +00001299 do {
1300 tline = *tlineptr;
1301 *tlineptr = tline ? tline->next : NULL;
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04001302 } while (tline && (tline->type == TOK_WHITESPACE ||
1303 tline->type == TOK_COMMENT));
H. Peter Anvin76690a12002-04-30 20:52:49 +00001304
1305 if (!tline)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001306 return tokval->t_type = TOKEN_EOS;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001307
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001308 tokval->t_charptr = tline->text;
1309
H. Peter Anvin76690a12002-04-30 20:52:49 +00001310 if (tline->text[0] == '$' && !tline->text[1])
H. Peter Anvine2c80182005-01-15 22:15:51 +00001311 return tokval->t_type = TOKEN_HERE;
H. Peter Anvin7cf897e2002-05-30 21:30:33 +00001312 if (tline->text[0] == '$' && tline->text[1] == '$' && !tline->text[2])
H. Peter Anvine2c80182005-01-15 22:15:51 +00001313 return tokval->t_type = TOKEN_BASE;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001314
H. Peter Anvine2c80182005-01-15 22:15:51 +00001315 if (tline->type == TOK_ID) {
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001316 p = tokval->t_charptr = tline->text;
1317 if (p[0] == '$') {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001318 tokval->t_charptr++;
1319 return tokval->t_type = TOKEN_ID;
1320 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00001321
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001322 for (r = p, s = ourcopy; *r; r++) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001323 if (r >= p+MAX_KEYWORD)
1324 return tokval->t_type = TOKEN_ID; /* Not a keyword */
H. Peter Anvinac8f8fc2008-06-11 15:49:41 -07001325 *s++ = nasm_tolower(*r);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001326 }
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001327 *s = '\0';
1328 /* right, so we have an identifier sitting in temp storage. now,
1329 * is it actually a register or instruction name, or what? */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001330 return nasm_token_hash(ourcopy, tokval);
H. Peter Anvin76690a12002-04-30 20:52:49 +00001331 }
1332
H. Peter Anvine2c80182005-01-15 22:15:51 +00001333 if (tline->type == TOK_NUMBER) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001334 bool rn_error;
1335 tokval->t_integer = readnum(tline->text, &rn_error);
1336 tokval->t_charptr = tline->text;
1337 if (rn_error)
1338 return tokval->t_type = TOKEN_ERRNUM;
1339 else
1340 return tokval->t_type = TOKEN_NUM;
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001341 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00001342
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001343 if (tline->type == TOK_FLOAT) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001344 return tokval->t_type = TOKEN_FLOAT;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001345 }
1346
H. Peter Anvine2c80182005-01-15 22:15:51 +00001347 if (tline->type == TOK_STRING) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001348 char bq, *ep;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001349
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001350 bq = tline->text[0];
H. Peter Anvin11627042008-06-09 20:45:19 -07001351 tokval->t_charptr = tline->text;
1352 tokval->t_inttwo = nasm_unquote(tline->text, &ep);
H. Peter Anvind2456592008-06-19 15:04:18 -07001353
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001354 if (ep[0] != bq || ep[1] != '\0')
1355 return tokval->t_type = TOKEN_ERRSTR;
1356 else
1357 return tokval->t_type = TOKEN_STR;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001358 }
1359
H. Peter Anvine2c80182005-01-15 22:15:51 +00001360 if (tline->type == TOK_OTHER) {
1361 if (!strcmp(tline->text, "<<"))
1362 return tokval->t_type = TOKEN_SHL;
1363 if (!strcmp(tline->text, ">>"))
1364 return tokval->t_type = TOKEN_SHR;
1365 if (!strcmp(tline->text, "//"))
1366 return tokval->t_type = TOKEN_SDIV;
1367 if (!strcmp(tline->text, "%%"))
1368 return tokval->t_type = TOKEN_SMOD;
1369 if (!strcmp(tline->text, "=="))
1370 return tokval->t_type = TOKEN_EQ;
1371 if (!strcmp(tline->text, "<>"))
1372 return tokval->t_type = TOKEN_NE;
1373 if (!strcmp(tline->text, "!="))
1374 return tokval->t_type = TOKEN_NE;
1375 if (!strcmp(tline->text, "<="))
1376 return tokval->t_type = TOKEN_LE;
1377 if (!strcmp(tline->text, ">="))
1378 return tokval->t_type = TOKEN_GE;
1379 if (!strcmp(tline->text, "&&"))
1380 return tokval->t_type = TOKEN_DBL_AND;
1381 if (!strcmp(tline->text, "^^"))
1382 return tokval->t_type = TOKEN_DBL_XOR;
1383 if (!strcmp(tline->text, "||"))
1384 return tokval->t_type = TOKEN_DBL_OR;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001385 }
1386
1387 /*
1388 * We have no other options: just return the first character of
1389 * the token text.
1390 */
1391 return tokval->t_type = tline->text[0];
1392}
1393
1394/*
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001395 * Compare a string to the name of an existing macro; this is a
1396 * simple wrapper which calls either strcmp or nasm_stricmp
1397 * depending on the value of the `casesense' parameter.
1398 */
H. Peter Anvin4db5a162007-10-11 13:42:09 -07001399static int mstrcmp(const char *p, const char *q, bool casesense)
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001400{
H. Peter Anvin734b1882002-04-30 21:01:08 +00001401 return casesense ? strcmp(p, q) : nasm_stricmp(p, q);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001402}
1403
1404/*
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07001405 * Compare a string to the name of an existing macro; this is a
1406 * simple wrapper which calls either strcmp or nasm_stricmp
1407 * depending on the value of the `casesense' parameter.
1408 */
1409static int mmemcmp(const char *p, const char *q, size_t l, bool casesense)
1410{
1411 return casesense ? memcmp(p, q, l) : nasm_memicmp(p, q, l);
1412}
1413
1414/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001415 * Return the Context structure associated with a %$ token. Return
1416 * NULL, having _already_ reported an error condition, if the
1417 * context stack isn't deep enough for the supplied number of $
1418 * signs.
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001419 * If all_contexts == true, contexts that enclose current are
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001420 * also scanned for such smacro, until it is found; if not -
1421 * only the context that directly results from the number of $'s
1422 * in variable's name.
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001423 *
1424 * If "namep" is non-NULL, set it to the pointer to the macro name
1425 * tail, i.e. the part beyond %$...
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001426 */
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001427static Context *get_ctx(const char *name, const char **namep,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001428 bool all_contexts)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001429{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001430 Context *ctx;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001431 SMacro *m;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001432 int i;
1433
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001434 if (namep)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001435 *namep = name;
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001436
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001437 if (!name || name[0] != '%' || name[1] != '$')
H. Peter Anvine2c80182005-01-15 22:15:51 +00001438 return NULL;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001439
H. Peter Anvine2c80182005-01-15 22:15:51 +00001440 if (!cstk) {
1441 error(ERR_NONFATAL, "`%s': context stack is empty", name);
1442 return NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001443 }
1444
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001445 name += 2;
1446 ctx = cstk;
1447 i = 0;
1448 while (ctx && *name == '$') {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001449 name++;
1450 i++;
1451 ctx = ctx->next;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001452 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001453 if (!ctx) {
1454 error(ERR_NONFATAL, "`%s': context stack is only"
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001455 " %d level%s deep", name, i, (i == 1 ? "" : "s"));
H. Peter Anvine2c80182005-01-15 22:15:51 +00001456 return NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001457 }
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001458
1459 if (namep)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001460 *namep = name;
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001461
Keith Kanios404589e2010-08-10 20:12:57 -05001462 if (!all_contexts)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001463 return ctx;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001464
H. Peter Anvine2c80182005-01-15 22:15:51 +00001465 do {
1466 /* Search for this smacro in found context */
H. Peter Anvin166c2472008-05-28 12:28:58 -07001467 m = hash_findix(&ctx->localmac, name);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001468 while (m) {
Keith Kanios404589e2010-08-10 20:12:57 -05001469 if (!mstrcmp(m->name, name, m->casesense)) {
1470 if ((i > 0) && (all_contexts == true)) {
1471 error(ERR_WARNING, "context-local label expansion"
1472 " to outer contexts will be deprecated"
1473 " starting in NASM 2.10, please update your"
1474 " code accordingly");
1475 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001476 return ctx;
Keith Kanios404589e2010-08-10 20:12:57 -05001477 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001478 m = m->next;
1479 }
1480 ctx = ctx->next;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001481 }
1482 while (ctx);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001483 return NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001484}
1485
1486/*
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001487 * Check to see if a file is already in a string list
1488 */
1489static bool in_list(const StrList *list, const char *str)
1490{
1491 while (list) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001492 if (!strcmp(list->str, str))
1493 return true;
1494 list = list->next;
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001495 }
1496 return false;
1497}
1498
1499/*
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001500 * Open an include file. This routine must always return a valid
1501 * file pointer if it returns - it's responsible for throwing an
1502 * ERR_FATAL and bombing out completely if not. It should also try
1503 * the include path one by one until it finds the file or reaches
1504 * the end of the path.
1505 */
H. Peter Anvin2b1c3b92008-06-06 10:38:46 -07001506static FILE *inc_fopen(const char *file, StrList **dhead, StrList ***dtail,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001507 bool missing_ok)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001508{
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001509 FILE *fp;
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001510 char *prefix = "";
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001511 IncPath *ip = ipath;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001512 int len = strlen(file);
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001513 size_t prefix_len = 0;
1514 StrList *sl;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001515
H. Peter Anvine2c80182005-01-15 22:15:51 +00001516 while (1) {
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001517 sl = nasm_malloc(prefix_len+len+1+sizeof sl->next);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001518 memcpy(sl->str, prefix, prefix_len);
1519 memcpy(sl->str+prefix_len, file, len+1);
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001520 fp = fopen(sl->str, "r");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001521 if (fp && dhead && !in_list(*dhead, sl->str)) {
1522 sl->next = NULL;
1523 **dtail = sl;
1524 *dtail = &sl->next;
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001525 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001526 nasm_free(sl);
1527 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001528 if (fp)
1529 return fp;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001530 if (!ip) {
1531 if (!missing_ok)
1532 break;
1533 prefix = NULL;
1534 } else {
1535 prefix = ip->path;
1536 ip = ip->next;
1537 }
1538 if (prefix) {
1539 prefix_len = strlen(prefix);
1540 } else {
1541 /* -MG given and file not found */
1542 if (dhead && !in_list(*dhead, file)) {
1543 sl = nasm_malloc(len+1+sizeof sl->next);
1544 sl->next = NULL;
1545 strcpy(sl->str, file);
1546 **dtail = sl;
1547 *dtail = &sl->next;
1548 }
1549 return NULL;
1550 }
H. Peter Anvineba20a72002-04-30 20:53:55 +00001551 }
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001552
H. Peter Anvin734b1882002-04-30 21:01:08 +00001553 error(ERR_FATAL, "unable to open include file `%s'", file);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001554 return NULL;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001555}
1556
1557/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001558 * Determine if we should warn on defining a single-line macro of
H. Peter Anvinef7468f2002-04-30 20:57:59 +00001559 * name `name', with `nparam' parameters. If nparam is 0 or -1, will
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001560 * return true if _any_ single-line macro of that name is defined.
1561 * Otherwise, will return true if a single-line macro with either
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001562 * `nparam' or no parameters is defined.
1563 *
1564 * If a macro with precisely the right number of parameters is
H. Peter Anvinef7468f2002-04-30 20:57:59 +00001565 * defined, or nparam is -1, the address of the definition structure
1566 * will be returned in `defn'; otherwise NULL will be returned. If `defn'
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001567 * is NULL, no action will be taken regarding its contents, and no
1568 * error will occur.
1569 *
1570 * Note that this is also called with nparam zero to resolve
1571 * `ifdef'.
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001572 *
1573 * If you already know which context macro belongs to, you can pass
1574 * the context pointer as first parameter; if you won't but name begins
1575 * with %$ the context will be automatically computed. If all_contexts
1576 * is true, macro will be searched in outer contexts as well.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001577 */
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07001578static bool
H. Peter Anvinb2a5fda2008-06-19 21:42:42 -07001579smacro_defined(Context * ctx, const char *name, int nparam, SMacro ** defn,
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07001580 bool nocase)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001581{
H. Peter Anvin166c2472008-05-28 12:28:58 -07001582 struct hash_table *smtbl;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001583 SMacro *m;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001584
H. Peter Anvin97a23472007-09-16 17:57:25 -07001585 if (ctx) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001586 smtbl = &ctx->localmac;
H. Peter Anvin97a23472007-09-16 17:57:25 -07001587 } else if (name[0] == '%' && name[1] == '$') {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001588 if (cstk)
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001589 ctx = get_ctx(name, &name, false);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001590 if (!ctx)
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001591 return false; /* got to return _something_ */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001592 smtbl = &ctx->localmac;
H. Peter Anvin97a23472007-09-16 17:57:25 -07001593 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001594 smtbl = &smacros;
H. Peter Anvin97a23472007-09-16 17:57:25 -07001595 }
H. Peter Anvin166c2472008-05-28 12:28:58 -07001596 m = (SMacro *) hash_findix(smtbl, name);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001597
H. Peter Anvine2c80182005-01-15 22:15:51 +00001598 while (m) {
1599 if (!mstrcmp(m->name, name, m->casesense && nocase) &&
Charles Crayne192d5b52007-10-18 19:02:42 -07001600 (nparam <= 0 || m->nparam == 0 || nparam == (int) m->nparam)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001601 if (defn) {
Charles Crayne192d5b52007-10-18 19:02:42 -07001602 if (nparam == (int) m->nparam || nparam == -1)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001603 *defn = m;
1604 else
1605 *defn = NULL;
1606 }
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001607 return true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001608 }
1609 m = m->next;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001610 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001611
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001612 return false;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001613}
1614
1615/*
1616 * Count and mark off the parameters in a multi-line macro call.
1617 * This is called both from within the multi-line macro expansion
1618 * code, and also to mark off the default parameters when provided
1619 * in a %macro definition line.
1620 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001621static void count_mmac_params(Token * t, int *nparam, Token *** params)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001622{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001623 int paramsize, brace;
1624
1625 *nparam = paramsize = 0;
1626 *params = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001627 while (t) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001628 /* +1: we need space for the final NULL */
H. Peter Anvin91fb6f12008-09-01 10:56:33 -07001629 if (*nparam+1 >= paramsize) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001630 paramsize += PARAM_DELTA;
1631 *params = nasm_realloc(*params, sizeof(**params) * paramsize);
1632 }
1633 skip_white_(t);
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001634 brace = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001635 if (tok_is_(t, "{"))
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001636 brace = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001637 (*params)[(*nparam)++] = t;
1638 while (tok_isnt_(t, brace ? "}" : ","))
1639 t = t->next;
1640 if (t) { /* got a comma/brace */
1641 t = t->next;
1642 if (brace) {
1643 /*
1644 * Now we've found the closing brace, look further
1645 * for the comma.
1646 */
1647 skip_white_(t);
1648 if (tok_isnt_(t, ",")) {
1649 error(ERR_NONFATAL,
1650 "braces do not enclose all of macro parameter");
1651 while (tok_isnt_(t, ","))
1652 t = t->next;
1653 }
1654 if (t)
1655 t = t->next; /* eat the comma */
1656 }
1657 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001658 }
1659}
1660
1661/*
H. Peter Anvin76690a12002-04-30 20:52:49 +00001662 * Determine whether one of the various `if' conditions is true or
1663 * not.
1664 *
1665 * We must free the tline we get passed.
1666 */
H. Peter Anvin70055962007-10-11 00:05:31 -07001667static bool if_condition(Token * tline, enum preproc_token ct)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001668{
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001669 enum pp_conditional i = PP_COND(ct);
H. Peter Anvin70055962007-10-11 00:05:31 -07001670 bool j;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001671 Token *t, *tt, **tptr, *origline;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001672 struct tokenval tokval;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001673 expr *evalresult;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001674 enum pp_token_type needtype;
H. Peter Anvin077fb932010-07-20 14:56:30 -07001675 char *p;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001676
1677 origline = tline;
1678
H. Peter Anvine2c80182005-01-15 22:15:51 +00001679 switch (i) {
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001680 case PPC_IFCTX:
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001681 j = false; /* have we matched yet? */
Victor van den Elzen0e857f12008-07-23 13:21:29 +02001682 while (true) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001683 skip_white_(tline);
Victor van den Elzen0e857f12008-07-23 13:21:29 +02001684 if (!tline)
1685 break;
1686 if (tline->type != TOK_ID) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001687 error(ERR_NONFATAL,
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001688 "`%s' expects context identifiers", pp_directives[ct]);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001689 free_tlist(origline);
1690 return -1;
1691 }
Victor van den Elzen0e857f12008-07-23 13:21:29 +02001692 if (cstk && cstk->name && !nasm_stricmp(tline->text, cstk->name))
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001693 j = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001694 tline = tline->next;
1695 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001696 break;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001697
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001698 case PPC_IFDEF:
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001699 j = false; /* have we matched yet? */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001700 while (tline) {
1701 skip_white_(tline);
1702 if (!tline || (tline->type != TOK_ID &&
1703 (tline->type != TOK_PREPROC_ID ||
1704 tline->text[1] != '$'))) {
1705 error(ERR_NONFATAL,
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001706 "`%s' expects macro identifiers", pp_directives[ct]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001707 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001708 }
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07001709 if (smacro_defined(NULL, tline->text, 0, NULL, true))
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001710 j = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001711 tline = tline->next;
1712 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001713 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001714
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001715 case PPC_IFENV:
1716 tline = expand_smacro(tline);
1717 j = false; /* have we matched yet? */
1718 while (tline) {
1719 skip_white_(tline);
1720 if (!tline || (tline->type != TOK_ID &&
H. Peter Anvin077fb932010-07-20 14:56:30 -07001721 tline->type != TOK_STRING &&
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001722 (tline->type != TOK_PREPROC_ID ||
H. Peter Anvin077fb932010-07-20 14:56:30 -07001723 tline->text[1] != '!'))) {
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001724 error(ERR_NONFATAL,
1725 "`%s' expects environment variable names",
1726 pp_directives[ct]);
1727 goto fail;
1728 }
H. Peter Anvin077fb932010-07-20 14:56:30 -07001729 p = tline->text;
1730 if (tline->type == TOK_PREPROC_ID)
1731 p += 2; /* Skip leading %! */
1732 if (*p == '\'' || *p == '\"' || *p == '`')
1733 nasm_unquote_cstr(p, ct);
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001734 if (getenv(p))
1735 j = true;
1736 tline = tline->next;
1737 }
1738 break;
1739
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001740 case PPC_IFIDN:
1741 case PPC_IFIDNI:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001742 tline = expand_smacro(tline);
1743 t = tt = tline;
1744 while (tok_isnt_(tt, ","))
1745 tt = tt->next;
1746 if (!tt) {
1747 error(ERR_NONFATAL,
1748 "`%s' expects two comma-separated arguments",
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001749 pp_directives[ct]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001750 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001751 }
1752 tt = tt->next;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001753 j = true; /* assume equality unless proved not */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001754 while ((t->type != TOK_OTHER || strcmp(t->text, ",")) && tt) {
1755 if (tt->type == TOK_OTHER && !strcmp(tt->text, ",")) {
1756 error(ERR_NONFATAL, "`%s': more than one comma on line",
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001757 pp_directives[ct]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001758 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001759 }
1760 if (t->type == TOK_WHITESPACE) {
1761 t = t->next;
1762 continue;
1763 }
1764 if (tt->type == TOK_WHITESPACE) {
1765 tt = tt->next;
1766 continue;
1767 }
1768 if (tt->type != t->type) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001769 j = false; /* found mismatching tokens */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001770 break;
1771 }
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07001772 /* When comparing strings, need to unquote them first */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001773 if (t->type == TOK_STRING) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001774 size_t l1 = nasm_unquote(t->text, NULL);
1775 size_t l2 = nasm_unquote(tt->text, NULL);
H. Peter Anvind2456592008-06-19 15:04:18 -07001776
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001777 if (l1 != l2) {
1778 j = false;
1779 break;
1780 }
1781 if (mmemcmp(t->text, tt->text, l1, i == PPC_IFIDN)) {
1782 j = false;
1783 break;
1784 }
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07001785 } else if (mstrcmp(tt->text, t->text, i == PPC_IFIDN) != 0) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001786 j = false; /* found mismatching tokens */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001787 break;
1788 }
Nickolay Yurchenkof3b3ce22003-09-21 20:38:43 +00001789
H. Peter Anvine2c80182005-01-15 22:15:51 +00001790 t = t->next;
1791 tt = tt->next;
1792 }
1793 if ((t->type != TOK_OTHER || strcmp(t->text, ",")) || tt)
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001794 j = false; /* trailing gunk on one end or other */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001795 break;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001796
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001797 case PPC_IFMACRO:
H. Peter Anvin89cee572009-07-15 09:16:54 -04001798 {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001799 bool found = false;
1800 MMacro searching, *mmac;
H. Peter Anvin65747262002-05-07 00:10:05 +00001801
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001802 skip_white_(tline);
1803 tline = expand_id(tline);
1804 if (!tok_type_(tline, TOK_ID)) {
1805 error(ERR_NONFATAL,
1806 "`%s' expects a macro name", pp_directives[ct]);
1807 goto fail;
1808 }
1809 searching.name = nasm_strdup(tline->text);
1810 searching.casesense = true;
1811 searching.plus = false;
1812 searching.nolist = false;
1813 searching.in_progress = 0;
1814 searching.max_depth = 0;
1815 searching.rep_nest = NULL;
1816 searching.nparam_min = 0;
1817 searching.nparam_max = INT_MAX;
1818 tline = expand_smacro(tline->next);
1819 skip_white_(tline);
1820 if (!tline) {
1821 } else if (!tok_type_(tline, TOK_NUMBER)) {
1822 error(ERR_NONFATAL,
1823 "`%s' expects a parameter count or nothing",
1824 pp_directives[ct]);
1825 } else {
1826 searching.nparam_min = searching.nparam_max =
1827 readnum(tline->text, &j);
1828 if (j)
1829 error(ERR_NONFATAL,
1830 "unable to parse parameter count `%s'",
1831 tline->text);
1832 }
1833 if (tline && tok_is_(tline->next, "-")) {
1834 tline = tline->next->next;
1835 if (tok_is_(tline, "*"))
1836 searching.nparam_max = INT_MAX;
1837 else if (!tok_type_(tline, TOK_NUMBER))
1838 error(ERR_NONFATAL,
1839 "`%s' expects a parameter count after `-'",
1840 pp_directives[ct]);
1841 else {
1842 searching.nparam_max = readnum(tline->text, &j);
1843 if (j)
1844 error(ERR_NONFATAL,
1845 "unable to parse parameter count `%s'",
1846 tline->text);
1847 if (searching.nparam_min > searching.nparam_max)
1848 error(ERR_NONFATAL,
1849 "minimum parameter count exceeds maximum");
1850 }
1851 }
1852 if (tline && tok_is_(tline->next, "+")) {
1853 tline = tline->next;
1854 searching.plus = true;
1855 }
1856 mmac = (MMacro *) hash_findix(&mmacros, searching.name);
1857 while (mmac) {
1858 if (!strcmp(mmac->name, searching.name) &&
1859 (mmac->nparam_min <= searching.nparam_max
1860 || searching.plus)
1861 && (searching.nparam_min <= mmac->nparam_max
1862 || mmac->plus)) {
1863 found = true;
1864 break;
1865 }
1866 mmac = mmac->next;
1867 }
1868 if (tline && tline->next)
1869 error(ERR_WARNING|ERR_PASS1,
1870 "trailing garbage after %%ifmacro ignored");
1871 nasm_free(searching.name);
1872 j = found;
1873 break;
H. Peter Anvin89cee572009-07-15 09:16:54 -04001874 }
H. Peter Anvin65747262002-05-07 00:10:05 +00001875
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001876 case PPC_IFID:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001877 needtype = TOK_ID;
1878 goto iftype;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001879 case PPC_IFNUM:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001880 needtype = TOK_NUMBER;
1881 goto iftype;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001882 case PPC_IFSTR:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001883 needtype = TOK_STRING;
1884 goto iftype;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001885
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001886iftype:
1887 t = tline = expand_smacro(tline);
H. Peter Anvind85d2502008-05-04 17:53:31 -07001888
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001889 while (tok_type_(t, TOK_WHITESPACE) ||
1890 (needtype == TOK_NUMBER &&
1891 tok_type_(t, TOK_OTHER) &&
1892 (t->text[0] == '-' || t->text[0] == '+') &&
1893 !t->text[1]))
1894 t = t->next;
H. Peter Anvind85d2502008-05-04 17:53:31 -07001895
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001896 j = tok_type_(t, needtype);
1897 break;
H. Peter Anvincbf768d2008-02-16 16:41:25 -08001898
1899 case PPC_IFTOKEN:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001900 t = tline = expand_smacro(tline);
1901 while (tok_type_(t, TOK_WHITESPACE))
1902 t = t->next;
H. Peter Anvincbf768d2008-02-16 16:41:25 -08001903
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001904 j = false;
1905 if (t) {
1906 t = t->next; /* Skip the actual token */
1907 while (tok_type_(t, TOK_WHITESPACE))
1908 t = t->next;
1909 j = !t; /* Should be nothing left */
1910 }
1911 break;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001912
H. Peter Anvin134b9462008-02-16 17:01:40 -08001913 case PPC_IFEMPTY:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001914 t = tline = expand_smacro(tline);
1915 while (tok_type_(t, TOK_WHITESPACE))
1916 t = t->next;
H. Peter Anvin134b9462008-02-16 17:01:40 -08001917
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001918 j = !t; /* Should be empty */
1919 break;
H. Peter Anvin134b9462008-02-16 17:01:40 -08001920
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001921 case PPC_IF:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001922 t = tline = expand_smacro(tline);
1923 tptr = &t;
1924 tokval.t_type = TOKEN_INVALID;
1925 evalresult = evaluate(ppscan, tptr, &tokval,
1926 NULL, pass | CRITICAL, error, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001927 if (!evalresult)
1928 return -1;
1929 if (tokval.t_type)
H. Peter Anvin917a3492008-09-24 09:14:49 -07001930 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +00001931 "trailing garbage after expression ignored");
1932 if (!is_simple(evalresult)) {
1933 error(ERR_NONFATAL,
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001934 "non-constant value given to `%s'", pp_directives[ct]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001935 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001936 }
Chuck Crayne60ae75d2007-05-02 01:59:16 +00001937 j = reloc_value(evalresult) != 0;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001938 break;
H. Peter Anvin95e28822007-09-12 04:20:08 +00001939
H. Peter Anvine2c80182005-01-15 22:15:51 +00001940 default:
1941 error(ERR_FATAL,
1942 "preprocessor directive `%s' not yet implemented",
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001943 pp_directives[ct]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001944 goto fail;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001945 }
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001946
1947 free_tlist(origline);
1948 return j ^ PP_NEGATIVE(ct);
H. Peter Anvin70653092007-10-19 14:42:29 -07001949
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001950fail:
1951 free_tlist(origline);
1952 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001953}
1954
1955/*
H. Peter Anvin4db5a162007-10-11 13:42:09 -07001956 * Common code for defining an smacro
1957 */
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001958static bool define_smacro(Context *ctx, const char *mname, bool casesense,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001959 int nparam, Token *expansion)
H. Peter Anvin4db5a162007-10-11 13:42:09 -07001960{
1961 SMacro *smac, **smhead;
H. Peter Anvin166c2472008-05-28 12:28:58 -07001962 struct hash_table *smtbl;
H. Peter Anvin70653092007-10-19 14:42:29 -07001963
H. Peter Anvin4db5a162007-10-11 13:42:09 -07001964 if (smacro_defined(ctx, mname, nparam, &smac, casesense)) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001965 if (!smac) {
1966 error(ERR_WARNING|ERR_PASS1,
1967 "single-line macro `%s' defined both with and"
1968 " without parameters", mname);
1969 /*
1970 * Some instances of the old code considered this a failure,
1971 * some others didn't. What is the right thing to do here?
1972 */
1973 free_tlist(expansion);
1974 return false; /* Failure */
1975 } else {
1976 /*
1977 * We're redefining, so we have to take over an
1978 * existing SMacro structure. This means freeing
1979 * what was already in it.
1980 */
1981 nasm_free(smac->name);
1982 free_tlist(smac->expansion);
1983 }
H. Peter Anvin4db5a162007-10-11 13:42:09 -07001984 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001985 smtbl = ctx ? &ctx->localmac : &smacros;
1986 smhead = (SMacro **) hash_findi_add(smtbl, mname);
1987 smac = nasm_malloc(sizeof(SMacro));
1988 smac->next = *smhead;
1989 *smhead = smac;
H. Peter Anvin4db5a162007-10-11 13:42:09 -07001990 }
1991 smac->name = nasm_strdup(mname);
1992 smac->casesense = casesense;
1993 smac->nparam = nparam;
1994 smac->expansion = expansion;
1995 smac->in_progress = false;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001996 return true; /* Success */
H. Peter Anvin4db5a162007-10-11 13:42:09 -07001997}
1998
1999/*
2000 * Undefine an smacro
2001 */
2002static void undef_smacro(Context *ctx, const char *mname)
2003{
2004 SMacro **smhead, *s, **sp;
H. Peter Anvin166c2472008-05-28 12:28:58 -07002005 struct hash_table *smtbl;
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002006
H. Peter Anvin166c2472008-05-28 12:28:58 -07002007 smtbl = ctx ? &ctx->localmac : &smacros;
2008 smhead = (SMacro **)hash_findi(smtbl, mname, NULL);
H. Peter Anvin70653092007-10-19 14:42:29 -07002009
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002010 if (smhead) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002011 /*
2012 * We now have a macro name... go hunt for it.
2013 */
2014 sp = smhead;
2015 while ((s = *sp) != NULL) {
2016 if (!mstrcmp(s->name, mname, s->casesense)) {
2017 *sp = s->next;
2018 nasm_free(s->name);
2019 free_tlist(s->expansion);
2020 nasm_free(s);
2021 } else {
2022 sp = &s->next;
2023 }
2024 }
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002025 }
2026}
2027
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002028/*
H. Peter Anvina26433d2008-07-16 14:40:01 -07002029 * Parse a mmacro specification.
2030 */
2031static bool parse_mmacro_spec(Token *tline, MMacro *def, const char *directive)
2032{
2033 bool err;
2034
2035 tline = tline->next;
2036 skip_white_(tline);
2037 tline = expand_id(tline);
2038 if (!tok_type_(tline, TOK_ID)) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002039 error(ERR_NONFATAL, "`%s' expects a macro name", directive);
2040 return false;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002041 }
Victor van den Elzenb916ede2008-07-23 15:14:22 +02002042
H. Peter Anvin89cee572009-07-15 09:16:54 -04002043 def->prev = NULL;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002044 def->name = nasm_strdup(tline->text);
2045 def->plus = false;
2046 def->nolist = false;
2047 def->in_progress = 0;
2048 def->rep_nest = NULL;
Victor van den Elzenb916ede2008-07-23 15:14:22 +02002049 def->nparam_min = 0;
2050 def->nparam_max = 0;
2051
H. Peter Anvina26433d2008-07-16 14:40:01 -07002052 tline = expand_smacro(tline->next);
2053 skip_white_(tline);
2054 if (!tok_type_(tline, TOK_NUMBER)) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002055 error(ERR_NONFATAL, "`%s' expects a parameter count", directive);
H. Peter Anvina26433d2008-07-16 14:40:01 -07002056 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002057 def->nparam_min = def->nparam_max =
2058 readnum(tline->text, &err);
2059 if (err)
2060 error(ERR_NONFATAL,
2061 "unable to parse parameter count `%s'", tline->text);
H. Peter Anvina26433d2008-07-16 14:40:01 -07002062 }
2063 if (tline && tok_is_(tline->next, "-")) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002064 tline = tline->next->next;
2065 if (tok_is_(tline, "*")) {
2066 def->nparam_max = INT_MAX;
2067 } else if (!tok_type_(tline, TOK_NUMBER)) {
2068 error(ERR_NONFATAL,
2069 "`%s' expects a parameter count after `-'", directive);
2070 } else {
2071 def->nparam_max = readnum(tline->text, &err);
2072 if (err) {
2073 error(ERR_NONFATAL, "unable to parse parameter count `%s'",
2074 tline->text);
2075 }
2076 if (def->nparam_min > def->nparam_max) {
2077 error(ERR_NONFATAL, "minimum parameter count exceeds maximum");
2078 }
2079 }
H. Peter Anvina26433d2008-07-16 14:40:01 -07002080 }
2081 if (tline && tok_is_(tline->next, "+")) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002082 tline = tline->next;
2083 def->plus = true;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002084 }
2085 if (tline && tok_type_(tline->next, TOK_ID) &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002086 !nasm_stricmp(tline->next->text, ".nolist")) {
2087 tline = tline->next;
2088 def->nolist = true;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002089 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002090
H. Peter Anvina26433d2008-07-16 14:40:01 -07002091 /*
2092 * Handle default parameters.
2093 */
2094 if (tline && tline->next) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002095 def->dlist = tline->next;
2096 tline->next = NULL;
2097 count_mmac_params(def->dlist, &def->ndefs, &def->defaults);
H. Peter Anvina26433d2008-07-16 14:40:01 -07002098 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002099 def->dlist = NULL;
2100 def->defaults = NULL;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002101 }
2102 def->expansion = NULL;
2103
H. Peter Anvin89cee572009-07-15 09:16:54 -04002104 if (def->defaults && def->ndefs > def->nparam_max - def->nparam_min &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002105 !def->plus)
2106 error(ERR_WARNING|ERR_PASS1|ERR_WARN_MDP,
2107 "too many default macro parameters");
Victor van den Elzenb916ede2008-07-23 15:14:22 +02002108
H. Peter Anvina26433d2008-07-16 14:40:01 -07002109 return true;
2110}
2111
2112
2113/*
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002114 * Decode a size directive
2115 */
2116static int parse_size(const char *str) {
2117 static const char *size_names[] =
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002118 { "byte", "dword", "oword", "qword", "tword", "word", "yword" };
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002119 static const int sizes[] =
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002120 { 0, 1, 4, 16, 8, 10, 2, 32 };
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002121
Cyrill Gorcunova7319242010-06-03 22:04:36 +04002122 return sizes[bsii(str, size_names, ARRAY_SIZE(size_names))+1];
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002123}
2124
Ed Beroset3ab3f412002-06-11 03:31:49 +00002125/**
2126 * find and process preprocessor directive in passed line
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002127 * Find out if a line contains a preprocessor directive, and deal
2128 * with it if so.
H. Peter Anvin70653092007-10-19 14:42:29 -07002129 *
Ed Beroset3ab3f412002-06-11 03:31:49 +00002130 * If a directive _is_ found, it is the responsibility of this routine
2131 * (and not the caller) to free_tlist() the line.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002132 *
Ed Beroset3ab3f412002-06-11 03:31:49 +00002133 * @param tline a pointer to the current tokeninzed line linked list
2134 * @return DIRECTIVE_FOUND or NO_DIRECTIVE_FOUND
H. Peter Anvin70653092007-10-19 14:42:29 -07002135 *
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002136 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002137static int do_directive(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00002138{
H. Peter Anvin4169a472007-09-12 01:29:43 +00002139 enum preproc_token i;
2140 int j;
H. Peter Anvin70055962007-10-11 00:05:31 -07002141 bool err;
2142 int nparam;
2143 bool nolist;
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07002144 bool casesense;
H. Peter Anvin8cfdb9d2007-09-14 18:36:01 -07002145 int k, m;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002146 int offset;
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08002147 char *p, *pp;
2148 const char *mname;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002149 Include *inc;
2150 Context *ctx;
2151 Cond *cond;
H. Peter Anvin97a23472007-09-16 17:57:25 -07002152 MMacro *mmac, **mmhead;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002153 Token *t, *tt, *param_start, *macro_start, *last, **tptr, *origline;
2154 Line *l;
2155 struct tokenval tokval;
2156 expr *evalresult;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002157 MMacro *tmp_defining; /* Used when manipulating rep_nest */
H. Peter Anvinf8ba53e2007-10-11 10:11:57 -07002158 int64_t count;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07002159 size_t len;
H. Peter Anvin8e3f75e2008-09-24 00:21:58 -07002160 int severity;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002161
2162 origline = tline;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002163
H. Peter Anvineba20a72002-04-30 20:53:55 +00002164 skip_white_(tline);
H. Peter Anvinf2936d72008-06-04 15:11:23 -07002165 if (!tline || !tok_type_(tline, TOK_PREPROC_ID) ||
H. Peter Anvine2c80182005-01-15 22:15:51 +00002166 (tline->text[1] == '%' || tline->text[1] == '$'
2167 || tline->text[1] == '!'))
2168 return NO_DIRECTIVE_FOUND;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002169
H. Peter Anvin4169a472007-09-12 01:29:43 +00002170 i = pp_token_hash(tline->text);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002171
2172 /*
Cyrill Gorcunovf09116f2010-02-28 11:52:53 +03002173 * FIXME: We zap execution of PP_RMACRO, PP_IRMACRO, PP_EXITMACRO
2174 * since they are known to be buggy at moment, we need to fix them
2175 * in future release (2.09-2.10)
2176 */
2177 if (i == PP_RMACRO || i == PP_RMACRO || i == PP_EXITMACRO) {
2178 error(ERR_NONFATAL, "unknown preprocessor directive `%s'",
2179 tline->text);
2180 return NO_DIRECTIVE_FOUND;
2181 }
2182
2183 /*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002184 * If we're in a non-emitting branch of a condition construct,
H. Peter Anvin76690a12002-04-30 20:52:49 +00002185 * or walking to the end of an already terminated %rep block,
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002186 * we should ignore all directives except for condition
2187 * directives.
2188 */
H. Peter Anvin76690a12002-04-30 20:52:49 +00002189 if (((istk->conds && !emitting(istk->conds->state)) ||
H. Peter Anvine2c80182005-01-15 22:15:51 +00002190 (istk->mstk && !istk->mstk->in_progress)) && !is_condition(i)) {
2191 return NO_DIRECTIVE_FOUND;
H. Peter Anvineba20a72002-04-30 20:53:55 +00002192 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002193
2194 /*
H. Peter Anvin76690a12002-04-30 20:52:49 +00002195 * If we're defining a macro or reading a %rep block, we should
Victor van den Elzen8f1120f2008-07-16 13:41:37 +02002196 * ignore all directives except for %macro/%imacro (which nest),
2197 * %endm/%endmacro, and (only if we're in a %rep block) %endrep.
2198 * If we're in a %rep block, another %rep nests, so should be let through.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002199 */
2200 if (defining && i != PP_MACRO && i != PP_IMACRO &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002201 i != PP_RMACRO && i != PP_IRMACRO &&
H. Peter Anvine2c80182005-01-15 22:15:51 +00002202 i != PP_ENDMACRO && i != PP_ENDM &&
2203 (defining->name || (i != PP_ENDREP && i != PP_REP))) {
2204 return NO_DIRECTIVE_FOUND;
H. Peter Anvineba20a72002-04-30 20:53:55 +00002205 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002206
Charles Crayned4200be2008-07-12 16:42:33 -07002207 if (defining) {
Keith Kanios852f1ee2009-07-12 00:19:55 -05002208 if (i == PP_MACRO || i == PP_IMACRO ||
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002209 i == PP_RMACRO || i == PP_IRMACRO) {
Charles Crayned4200be2008-07-12 16:42:33 -07002210 nested_mac_count++;
2211 return NO_DIRECTIVE_FOUND;
2212 } else if (nested_mac_count > 0) {
2213 if (i == PP_ENDMACRO) {
2214 nested_mac_count--;
2215 return NO_DIRECTIVE_FOUND;
2216 }
2217 }
2218 if (!defining->name) {
2219 if (i == PP_REP) {
2220 nested_rep_count++;
2221 return NO_DIRECTIVE_FOUND;
2222 } else if (nested_rep_count > 0) {
2223 if (i == PP_ENDREP) {
2224 nested_rep_count--;
2225 return NO_DIRECTIVE_FOUND;
2226 }
2227 }
2228 }
2229 }
2230
H. Peter Anvin4169a472007-09-12 01:29:43 +00002231 switch (i) {
2232 case PP_INVALID:
H. Peter Anvine2c80182005-01-15 22:15:51 +00002233 error(ERR_NONFATAL, "unknown preprocessor directive `%s'",
2234 tline->text);
2235 return NO_DIRECTIVE_FOUND; /* didn't get it */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002236
H. Peter Anvine2c80182005-01-15 22:15:51 +00002237 case PP_STACKSIZE:
2238 /* Directive to tell NASM what the default stack size is. The
2239 * default is for a 16-bit stack, and this can be overriden with
2240 * %stacksize large.
H. Peter Anvine2c80182005-01-15 22:15:51 +00002241 */
2242 tline = tline->next;
2243 if (tline && tline->type == TOK_WHITESPACE)
2244 tline = tline->next;
2245 if (!tline || tline->type != TOK_ID) {
2246 error(ERR_NONFATAL, "`%%stacksize' missing size parameter");
2247 free_tlist(origline);
2248 return DIRECTIVE_FOUND;
2249 }
2250 if (nasm_stricmp(tline->text, "flat") == 0) {
2251 /* All subsequent ARG directives are for a 32-bit stack */
2252 StackSize = 4;
2253 StackPointer = "ebp";
2254 ArgOffset = 8;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002255 LocalOffset = 0;
Charles Crayne7eaf9192007-11-08 22:11:14 -08002256 } else if (nasm_stricmp(tline->text, "flat64") == 0) {
2257 /* All subsequent ARG directives are for a 64-bit stack */
2258 StackSize = 8;
2259 StackPointer = "rbp";
Per Jessen53252e02010-02-11 00:16:59 +03002260 ArgOffset = 16;
Charles Crayne7eaf9192007-11-08 22:11:14 -08002261 LocalOffset = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002262 } else if (nasm_stricmp(tline->text, "large") == 0) {
2263 /* All subsequent ARG directives are for a 16-bit stack,
2264 * far function call.
2265 */
2266 StackSize = 2;
2267 StackPointer = "bp";
2268 ArgOffset = 4;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002269 LocalOffset = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002270 } else if (nasm_stricmp(tline->text, "small") == 0) {
2271 /* All subsequent ARG directives are for a 16-bit stack,
2272 * far function call. We don't support near functions.
2273 */
2274 StackSize = 2;
2275 StackPointer = "bp";
2276 ArgOffset = 6;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002277 LocalOffset = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002278 } else {
2279 error(ERR_NONFATAL, "`%%stacksize' invalid size type");
2280 free_tlist(origline);
2281 return DIRECTIVE_FOUND;
2282 }
2283 free_tlist(origline);
2284 return DIRECTIVE_FOUND;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002285
H. Peter Anvine2c80182005-01-15 22:15:51 +00002286 case PP_ARG:
2287 /* TASM like ARG directive to define arguments to functions, in
2288 * the following form:
2289 *
2290 * ARG arg1:WORD, arg2:DWORD, arg4:QWORD
2291 */
2292 offset = ArgOffset;
2293 do {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00002294 char *arg, directive[256];
H. Peter Anvine2c80182005-01-15 22:15:51 +00002295 int size = StackSize;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002296
H. Peter Anvine2c80182005-01-15 22:15:51 +00002297 /* Find the argument name */
2298 tline = tline->next;
2299 if (tline && tline->type == TOK_WHITESPACE)
2300 tline = tline->next;
2301 if (!tline || tline->type != TOK_ID) {
2302 error(ERR_NONFATAL, "`%%arg' missing argument parameter");
2303 free_tlist(origline);
2304 return DIRECTIVE_FOUND;
2305 }
2306 arg = tline->text;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002307
H. Peter Anvine2c80182005-01-15 22:15:51 +00002308 /* Find the argument size type */
2309 tline = tline->next;
2310 if (!tline || tline->type != TOK_OTHER
2311 || tline->text[0] != ':') {
2312 error(ERR_NONFATAL,
2313 "Syntax error processing `%%arg' directive");
2314 free_tlist(origline);
2315 return DIRECTIVE_FOUND;
2316 }
2317 tline = tline->next;
2318 if (!tline || tline->type != TOK_ID) {
2319 error(ERR_NONFATAL, "`%%arg' missing size type parameter");
2320 free_tlist(origline);
2321 return DIRECTIVE_FOUND;
2322 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002323
H. Peter Anvine2c80182005-01-15 22:15:51 +00002324 /* Allow macro expansion of type parameter */
Keith Kaniosb7a89542007-04-12 02:40:54 +00002325 tt = tokenize(tline->text);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002326 tt = expand_smacro(tt);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002327 size = parse_size(tt->text);
2328 if (!size) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002329 error(ERR_NONFATAL,
2330 "Invalid size type for `%%arg' missing directive");
2331 free_tlist(tt);
2332 free_tlist(origline);
2333 return DIRECTIVE_FOUND;
2334 }
2335 free_tlist(tt);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002336
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002337 /* Round up to even stack slots */
2338 size = ALIGN(size, StackSize);
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002339
H. Peter Anvine2c80182005-01-15 22:15:51 +00002340 /* Now define the macro for the argument */
2341 snprintf(directive, sizeof(directive), "%%define %s (%s+%d)",
2342 arg, StackPointer, offset);
Keith Kaniosb7a89542007-04-12 02:40:54 +00002343 do_directive(tokenize(directive));
H. Peter Anvine2c80182005-01-15 22:15:51 +00002344 offset += size;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002345
H. Peter Anvine2c80182005-01-15 22:15:51 +00002346 /* Move to the next argument in the list */
2347 tline = tline->next;
2348 if (tline && tline->type == TOK_WHITESPACE)
2349 tline = tline->next;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002350 } while (tline && tline->type == TOK_OTHER && tline->text[0] == ',');
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002351 ArgOffset = offset;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002352 free_tlist(origline);
2353 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002354
H. Peter Anvine2c80182005-01-15 22:15:51 +00002355 case PP_LOCAL:
2356 /* TASM like LOCAL directive to define local variables for a
2357 * function, in the following form:
2358 *
2359 * LOCAL local1:WORD, local2:DWORD, local4:QWORD = LocalSize
2360 *
2361 * The '= LocalSize' at the end is ignored by NASM, but is
2362 * required by TASM to define the local parameter size (and used
2363 * by the TASM macro package).
2364 */
2365 offset = LocalOffset;
2366 do {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00002367 char *local, directive[256];
H. Peter Anvine2c80182005-01-15 22:15:51 +00002368 int size = StackSize;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002369
H. Peter Anvine2c80182005-01-15 22:15:51 +00002370 /* Find the argument name */
2371 tline = tline->next;
2372 if (tline && tline->type == TOK_WHITESPACE)
2373 tline = tline->next;
2374 if (!tline || tline->type != TOK_ID) {
2375 error(ERR_NONFATAL,
2376 "`%%local' missing argument parameter");
2377 free_tlist(origline);
2378 return DIRECTIVE_FOUND;
2379 }
2380 local = tline->text;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002381
H. Peter Anvine2c80182005-01-15 22:15:51 +00002382 /* Find the argument size type */
2383 tline = tline->next;
2384 if (!tline || tline->type != TOK_OTHER
2385 || tline->text[0] != ':') {
2386 error(ERR_NONFATAL,
2387 "Syntax error processing `%%local' directive");
2388 free_tlist(origline);
2389 return DIRECTIVE_FOUND;
2390 }
2391 tline = tline->next;
2392 if (!tline || tline->type != TOK_ID) {
2393 error(ERR_NONFATAL,
2394 "`%%local' missing size type parameter");
2395 free_tlist(origline);
2396 return DIRECTIVE_FOUND;
2397 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00002398
H. Peter Anvine2c80182005-01-15 22:15:51 +00002399 /* Allow macro expansion of type parameter */
Keith Kaniosb7a89542007-04-12 02:40:54 +00002400 tt = tokenize(tline->text);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002401 tt = expand_smacro(tt);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002402 size = parse_size(tt->text);
2403 if (!size) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002404 error(ERR_NONFATAL,
2405 "Invalid size type for `%%local' missing directive");
2406 free_tlist(tt);
2407 free_tlist(origline);
2408 return DIRECTIVE_FOUND;
2409 }
2410 free_tlist(tt);
H. Peter Anvin734b1882002-04-30 21:01:08 +00002411
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002412 /* Round up to even stack slots */
2413 size = ALIGN(size, StackSize);
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002414
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002415 offset += size; /* Negative offset, increment before */
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002416
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002417 /* Now define the macro for the argument */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002418 snprintf(directive, sizeof(directive), "%%define %s (%s-%d)",
2419 local, StackPointer, offset);
Keith Kaniosb7a89542007-04-12 02:40:54 +00002420 do_directive(tokenize(directive));
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002421
H. Peter Anvine2c80182005-01-15 22:15:51 +00002422 /* Now define the assign to setup the enter_c macro correctly */
2423 snprintf(directive, sizeof(directive),
2424 "%%assign %%$localsize %%$localsize+%d", size);
Keith Kaniosb7a89542007-04-12 02:40:54 +00002425 do_directive(tokenize(directive));
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002426
H. Peter Anvine2c80182005-01-15 22:15:51 +00002427 /* Move to the next argument in the list */
2428 tline = tline->next;
2429 if (tline && tline->type == TOK_WHITESPACE)
2430 tline = tline->next;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002431 } while (tline && tline->type == TOK_OTHER && tline->text[0] == ',');
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002432 LocalOffset = offset;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002433 free_tlist(origline);
2434 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002435
H. Peter Anvine2c80182005-01-15 22:15:51 +00002436 case PP_CLEAR:
2437 if (tline->next)
H. Peter Anvin917a3492008-09-24 09:14:49 -07002438 error(ERR_WARNING|ERR_PASS1,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002439 "trailing garbage after `%%clear' ignored");
2440 free_macros();
2441 init_macros();
H. Peter Anvine2c80182005-01-15 22:15:51 +00002442 free_tlist(origline);
2443 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002444
H. Peter Anvin418ca702008-05-30 10:42:30 -07002445 case PP_DEPEND:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002446 t = tline->next = expand_smacro(tline->next);
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002447 skip_white_(t);
2448 if (!t || (t->type != TOK_STRING &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002449 t->type != TOK_INTERNAL_STRING)) {
H. Peter Anvin418ca702008-05-30 10:42:30 -07002450 error(ERR_NONFATAL, "`%%depend' expects a file name");
2451 free_tlist(origline);
2452 return DIRECTIVE_FOUND; /* but we did _something_ */
2453 }
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002454 if (t->next)
H. Peter Anvin917a3492008-09-24 09:14:49 -07002455 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvin418ca702008-05-30 10:42:30 -07002456 "trailing garbage after `%%depend' ignored");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002457 p = t->text;
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002458 if (t->type != TOK_INTERNAL_STRING)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002459 nasm_unquote_cstr(p, i);
2460 if (dephead && !in_list(*dephead, p)) {
2461 StrList *sl = nasm_malloc(strlen(p)+1+sizeof sl->next);
2462 sl->next = NULL;
2463 strcpy(sl->str, p);
2464 *deptail = sl;
2465 deptail = &sl->next;
2466 }
2467 free_tlist(origline);
H. Peter Anvin418ca702008-05-30 10:42:30 -07002468 return DIRECTIVE_FOUND;
2469
2470 case PP_INCLUDE:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002471 t = tline->next = expand_smacro(tline->next);
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002472 skip_white_(t);
H. Peter Anvind2456592008-06-19 15:04:18 -07002473
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002474 if (!t || (t->type != TOK_STRING &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002475 t->type != TOK_INTERNAL_STRING)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002476 error(ERR_NONFATAL, "`%%include' expects a file name");
2477 free_tlist(origline);
2478 return DIRECTIVE_FOUND; /* but we did _something_ */
2479 }
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002480 if (t->next)
H. Peter Anvin917a3492008-09-24 09:14:49 -07002481 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +00002482 "trailing garbage after `%%include' ignored");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002483 p = t->text;
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002484 if (t->type != TOK_INTERNAL_STRING)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002485 nasm_unquote_cstr(p, i);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002486 inc = nasm_malloc(sizeof(Include));
2487 inc->next = istk;
2488 inc->conds = NULL;
H. Peter Anvin2b1c3b92008-06-06 10:38:46 -07002489 inc->fp = inc_fopen(p, dephead, &deptail, pass == 0);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002490 if (!inc->fp) {
2491 /* -MG given but file not found */
2492 nasm_free(inc);
2493 } else {
2494 inc->fname = src_set_fname(nasm_strdup(p));
2495 inc->lineno = src_set_linnum(0);
2496 inc->lineinc = 1;
2497 inc->expansion = NULL;
2498 inc->mstk = NULL;
2499 istk = inc;
2500 list->uplevel(LIST_INCLUDE);
2501 }
2502 free_tlist(origline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002503 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002504
H. Peter Anvind2456592008-06-19 15:04:18 -07002505 case PP_USE:
H. Peter Anvinf4ae5ad2008-06-19 18:39:24 -07002506 {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002507 static macros_t *use_pkg;
2508 const char *pkg_macro = NULL;
H. Peter Anvinf4ae5ad2008-06-19 18:39:24 -07002509
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002510 tline = tline->next;
2511 skip_white_(tline);
2512 tline = expand_id(tline);
H. Peter Anvind2456592008-06-19 15:04:18 -07002513
H. Peter Anvin264b7b92008-10-24 16:38:17 -07002514 if (!tline || (tline->type != TOK_STRING &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002515 tline->type != TOK_INTERNAL_STRING &&
2516 tline->type != TOK_ID)) {
H. Peter Anvin926fc402008-06-19 16:26:12 -07002517 error(ERR_NONFATAL, "`%%use' expects a package name");
H. Peter Anvind2456592008-06-19 15:04:18 -07002518 free_tlist(origline);
2519 return DIRECTIVE_FOUND; /* but we did _something_ */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002520 }
H. Peter Anvin264b7b92008-10-24 16:38:17 -07002521 if (tline->next)
H. Peter Anvin917a3492008-09-24 09:14:49 -07002522 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvind2456592008-06-19 15:04:18 -07002523 "trailing garbage after `%%use' ignored");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002524 if (tline->type == TOK_STRING)
2525 nasm_unquote_cstr(tline->text, i);
2526 use_pkg = nasm_stdmac_find_package(tline->text);
2527 if (!use_pkg)
2528 error(ERR_NONFATAL, "unknown `%%use' package: %s", tline->text);
2529 else
2530 pkg_macro = (char *)use_pkg + 1; /* The first string will be <%define>__USE_*__ */
Victor van den Elzen35eb2ea2010-03-10 22:33:48 +01002531 if (use_pkg && ! smacro_defined(NULL, pkg_macro, 0, NULL, true)) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002532 /* Not already included, go ahead and include it */
2533 stdmacpos = use_pkg;
2534 }
2535 free_tlist(origline);
H. Peter Anvind2456592008-06-19 15:04:18 -07002536 return DIRECTIVE_FOUND;
H. Peter Anvinf4ae5ad2008-06-19 18:39:24 -07002537 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002538 case PP_PUSH:
H. Peter Anvine2c80182005-01-15 22:15:51 +00002539 case PP_REPL:
H. Peter Anvin42b56392008-10-24 16:24:21 -07002540 case PP_POP:
H. Peter Anvine2c80182005-01-15 22:15:51 +00002541 tline = tline->next;
2542 skip_white_(tline);
2543 tline = expand_id(tline);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002544 if (tline) {
2545 if (!tok_type_(tline, TOK_ID)) {
2546 error(ERR_NONFATAL, "`%s' expects a context identifier",
2547 pp_directives[i]);
2548 free_tlist(origline);
2549 return DIRECTIVE_FOUND; /* but we did _something_ */
2550 }
2551 if (tline->next)
2552 error(ERR_WARNING|ERR_PASS1,
2553 "trailing garbage after `%s' ignored",
2554 pp_directives[i]);
2555 p = nasm_strdup(tline->text);
2556 } else {
2557 p = NULL; /* Anonymous */
2558 }
H. Peter Anvin42b56392008-10-24 16:24:21 -07002559
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002560 if (i == PP_PUSH) {
2561 ctx = nasm_malloc(sizeof(Context));
2562 ctx->next = cstk;
2563 hash_init(&ctx->localmac, HASH_SMALL);
2564 ctx->name = p;
2565 ctx->number = unique++;
2566 cstk = ctx;
2567 } else {
2568 /* %pop or %repl */
2569 if (!cstk) {
2570 error(ERR_NONFATAL, "`%s': context stack is empty",
2571 pp_directives[i]);
2572 } else if (i == PP_POP) {
2573 if (p && (!cstk->name || nasm_stricmp(p, cstk->name)))
2574 error(ERR_NONFATAL, "`%%pop' in wrong context: %s, "
2575 "expected %s",
2576 cstk->name ? cstk->name : "anonymous", p);
2577 else
2578 ctx_pop();
2579 } else {
2580 /* i == PP_REPL */
2581 nasm_free(cstk->name);
2582 cstk->name = p;
2583 p = NULL;
2584 }
2585 nasm_free(p);
2586 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002587 free_tlist(origline);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002588 return DIRECTIVE_FOUND;
H. Peter Anvin8e3f75e2008-09-24 00:21:58 -07002589 case PP_FATAL:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002590 severity = ERR_FATAL;
2591 goto issue_error;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002592 case PP_ERROR:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002593 severity = ERR_NONFATAL;
2594 goto issue_error;
H. Peter Anvin7df04172008-06-10 18:27:38 -07002595 case PP_WARNING:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002596 severity = ERR_WARNING|ERR_WARN_USER;
2597 goto issue_error;
H. Peter Anvin8e3f75e2008-09-24 00:21:58 -07002598
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002599issue_error:
H. Peter Anvin7df04172008-06-10 18:27:38 -07002600 {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002601 /* Only error out if this is the final pass */
2602 if (pass != 2 && i != PP_FATAL)
2603 return DIRECTIVE_FOUND;
2604
2605 tline->next = expand_smacro(tline->next);
2606 tline = tline->next;
2607 skip_white_(tline);
2608 t = tline ? tline->next : NULL;
2609 skip_white_(t);
2610 if (tok_type_(tline, TOK_STRING) && !t) {
2611 /* The line contains only a quoted string */
2612 p = tline->text;
2613 nasm_unquote(p, NULL); /* Ignore NUL character truncation */
2614 error(severity, "%s", p);
2615 } else {
2616 /* Not a quoted string, or more than a quoted string */
2617 p = detoken(tline, false);
2618 error(severity, "%s", p);
2619 nasm_free(p);
2620 }
2621 free_tlist(origline);
2622 return DIRECTIVE_FOUND;
H. Peter Anvin7df04172008-06-10 18:27:38 -07002623 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00002624
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002625 CASE_PP_IF:
H. Peter Anvine2c80182005-01-15 22:15:51 +00002626 if (istk->conds && !emitting(istk->conds->state))
2627 j = COND_NEVER;
2628 else {
2629 j = if_condition(tline->next, i);
2630 tline->next = NULL; /* it got freed */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002631 j = j < 0 ? COND_NEVER : j ? COND_IF_TRUE : COND_IF_FALSE;
2632 }
2633 cond = nasm_malloc(sizeof(Cond));
2634 cond->next = istk->conds;
2635 cond->state = j;
2636 istk->conds = cond;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002637 if(istk->mstk)
Keith Kanios3c0d91f2009-10-25 13:28:03 -05002638 istk->mstk->condcnt ++;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002639 free_tlist(origline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002640 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002641
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002642 CASE_PP_ELIF:
H. Peter Anvine2c80182005-01-15 22:15:51 +00002643 if (!istk->conds)
H. Peter Anvin4169a472007-09-12 01:29:43 +00002644 error(ERR_FATAL, "`%s': no matching `%%if'", pp_directives[i]);
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002645 switch(istk->conds->state) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002646 case COND_IF_TRUE:
2647 istk->conds->state = COND_DONE;
2648 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002649
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002650 case COND_DONE:
2651 case COND_NEVER:
2652 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002653
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002654 case COND_ELSE_TRUE:
2655 case COND_ELSE_FALSE:
2656 error_precond(ERR_WARNING|ERR_PASS1,
2657 "`%%elif' after `%%else' ignored");
2658 istk->conds->state = COND_NEVER;
2659 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002660
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002661 case COND_IF_FALSE:
2662 /*
2663 * IMPORTANT: In the case of %if, we will already have
2664 * called expand_mmac_params(); however, if we're
2665 * processing an %elif we must have been in a
2666 * non-emitting mode, which would have inhibited
2667 * the normal invocation of expand_mmac_params().
2668 * Therefore, we have to do it explicitly here.
2669 */
2670 j = if_condition(expand_mmac_params(tline->next), i);
2671 tline->next = NULL; /* it got freed */
2672 istk->conds->state =
2673 j < 0 ? COND_NEVER : j ? COND_IF_TRUE : COND_IF_FALSE;
2674 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002675 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002676 free_tlist(origline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002677 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002678
H. Peter Anvine2c80182005-01-15 22:15:51 +00002679 case PP_ELSE:
2680 if (tline->next)
H. Peter Anvin917a3492008-09-24 09:14:49 -07002681 error_precond(ERR_WARNING|ERR_PASS1,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002682 "trailing garbage after `%%else' ignored");
H. Peter Anvine2c80182005-01-15 22:15:51 +00002683 if (!istk->conds)
2684 error(ERR_FATAL, "`%%else': no matching `%%if'");
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002685 switch(istk->conds->state) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002686 case COND_IF_TRUE:
2687 case COND_DONE:
2688 istk->conds->state = COND_ELSE_FALSE;
2689 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002690
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002691 case COND_NEVER:
2692 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002693
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002694 case COND_IF_FALSE:
2695 istk->conds->state = COND_ELSE_TRUE;
2696 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002697
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002698 case COND_ELSE_TRUE:
2699 case COND_ELSE_FALSE:
2700 error_precond(ERR_WARNING|ERR_PASS1,
2701 "`%%else' after `%%else' ignored.");
2702 istk->conds->state = COND_NEVER;
2703 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002704 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002705 free_tlist(origline);
2706 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002707
H. Peter Anvine2c80182005-01-15 22:15:51 +00002708 case PP_ENDIF:
2709 if (tline->next)
H. Peter Anvin917a3492008-09-24 09:14:49 -07002710 error_precond(ERR_WARNING|ERR_PASS1,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002711 "trailing garbage after `%%endif' ignored");
H. Peter Anvine2c80182005-01-15 22:15:51 +00002712 if (!istk->conds)
2713 error(ERR_FATAL, "`%%endif': no matching `%%if'");
2714 cond = istk->conds;
2715 istk->conds = cond->next;
2716 nasm_free(cond);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002717 if(istk->mstk)
Keith Kanios3c0d91f2009-10-25 13:28:03 -05002718 istk->mstk->condcnt --;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002719 free_tlist(origline);
2720 return DIRECTIVE_FOUND;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002721
H. Peter Anvindb8f96e2009-07-15 09:07:29 -04002722 case PP_RMACRO:
2723 case PP_IRMACRO:
H. Peter Anvine2c80182005-01-15 22:15:51 +00002724 case PP_MACRO:
2725 case PP_IMACRO:
H. Peter Anvina26433d2008-07-16 14:40:01 -07002726 if (defining) {
H. Peter Anvindb8f96e2009-07-15 09:07:29 -04002727 error(ERR_FATAL, "`%s': already defining a macro",
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002728 pp_directives[i]);
2729 return DIRECTIVE_FOUND;
2730 }
2731 defining = nasm_malloc(sizeof(MMacro));
2732 defining->max_depth =
2733 (i == PP_RMACRO) || (i == PP_IRMACRO) ? DEADMAN_LIMIT : 0;
2734 defining->casesense = (i == PP_MACRO) || (i == PP_RMACRO);
2735 if (!parse_mmacro_spec(tline, defining, pp_directives[i])) {
2736 nasm_free(defining);
2737 defining = NULL;
2738 return DIRECTIVE_FOUND;
2739 }
H. Peter Anvina26433d2008-07-16 14:40:01 -07002740
H. Peter Anvin166c2472008-05-28 12:28:58 -07002741 mmac = (MMacro *) hash_findix(&mmacros, defining->name);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002742 while (mmac) {
2743 if (!strcmp(mmac->name, defining->name) &&
2744 (mmac->nparam_min <= defining->nparam_max
2745 || defining->plus)
2746 && (defining->nparam_min <= mmac->nparam_max
2747 || mmac->plus)) {
H. Peter Anvin917a3492008-09-24 09:14:49 -07002748 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +00002749 "redefining multi-line macro `%s'", defining->name);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002750 return DIRECTIVE_FOUND;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002751 }
2752 mmac = mmac->next;
2753 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002754 free_tlist(origline);
2755 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002756
H. Peter Anvine2c80182005-01-15 22:15:51 +00002757 case PP_ENDM:
2758 case PP_ENDMACRO:
Victor van den Elzen8f1120f2008-07-16 13:41:37 +02002759 if (! (defining && defining->name)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002760 error(ERR_NONFATAL, "`%s': not defining a macro", tline->text);
2761 return DIRECTIVE_FOUND;
2762 }
Keith Kanios852f1ee2009-07-12 00:19:55 -05002763 mmhead = (MMacro **) hash_findi_add(&mmacros, defining->name);
H. Peter Anvin97a23472007-09-16 17:57:25 -07002764 defining->next = *mmhead;
Keith Kanios852f1ee2009-07-12 00:19:55 -05002765 *mmhead = defining;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002766 defining = NULL;
2767 free_tlist(origline);
2768 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002769
H. Peter Anvin89cee572009-07-15 09:16:54 -04002770 case PP_EXITMACRO:
Keith Kanios852f1ee2009-07-12 00:19:55 -05002771 /*
2772 * We must search along istk->expansion until we hit a
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002773 * macro-end marker for a macro with a name. Then we
Keith Kanios3c0d91f2009-10-25 13:28:03 -05002774 * bypass all lines between exitmacro and endmacro.
Keith Kanios852f1ee2009-07-12 00:19:55 -05002775 */
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04002776 list_for_each(l, istk->expansion)
Keith Kanios852f1ee2009-07-12 00:19:55 -05002777 if (l->finishes && l->finishes->name)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002778 break;
Keith Kanios852f1ee2009-07-12 00:19:55 -05002779
2780 if (l) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002781 /*
2782 * Remove all conditional entries relative to this
2783 * macro invocation. (safe to do in this context)
2784 */
Keith Kanios3c0d91f2009-10-25 13:28:03 -05002785 for ( ; l->finishes->condcnt > 0; l->finishes->condcnt --) {
2786 cond = istk->conds;
2787 istk->conds = cond->next;
2788 nasm_free(cond);
2789 }
2790 istk->expansion = l;
Keith Kanios852f1ee2009-07-12 00:19:55 -05002791 } else {
2792 error(ERR_NONFATAL, "`%%exitmacro' not within `%%macro' block");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002793 }
Keith Kanios852f1ee2009-07-12 00:19:55 -05002794 free_tlist(origline);
2795 return DIRECTIVE_FOUND;
2796
H. Peter Anvina26433d2008-07-16 14:40:01 -07002797 case PP_UNMACRO:
2798 case PP_UNIMACRO:
2799 {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002800 MMacro **mmac_p;
2801 MMacro spec;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002802
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002803 spec.casesense = (i == PP_UNMACRO);
2804 if (!parse_mmacro_spec(tline, &spec, pp_directives[i])) {
2805 return DIRECTIVE_FOUND;
2806 }
H. Peter Anvina26433d2008-07-16 14:40:01 -07002807 mmac_p = (MMacro **) hash_findi(&mmacros, spec.name, NULL);
2808 while (mmac_p && *mmac_p) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002809 mmac = *mmac_p;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002810 if (mmac->casesense == spec.casesense &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002811 !mstrcmp(mmac->name, spec.name, spec.casesense) &&
H. Peter Anvina26433d2008-07-16 14:40:01 -07002812 mmac->nparam_min == spec.nparam_min &&
2813 mmac->nparam_max == spec.nparam_max &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002814 mmac->plus == spec.plus) {
2815 *mmac_p = mmac->next;
2816 free_mmacro(mmac);
2817 } else {
2818 mmac_p = &mmac->next;
2819 }
2820 }
2821 free_tlist(origline);
2822 free_tlist(spec.dlist);
2823 return DIRECTIVE_FOUND;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002824 }
2825
H. Peter Anvine2c80182005-01-15 22:15:51 +00002826 case PP_ROTATE:
2827 if (tline->next && tline->next->type == TOK_WHITESPACE)
2828 tline = tline->next;
H. Peter Anvin89cee572009-07-15 09:16:54 -04002829 if (!tline->next) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002830 free_tlist(origline);
2831 error(ERR_NONFATAL, "`%%rotate' missing rotate count");
2832 return DIRECTIVE_FOUND;
2833 }
2834 t = expand_smacro(tline->next);
2835 tline->next = NULL;
2836 free_tlist(origline);
2837 tline = t;
2838 tptr = &t;
2839 tokval.t_type = TOKEN_INVALID;
2840 evalresult =
2841 evaluate(ppscan, tptr, &tokval, NULL, pass, error, NULL);
2842 free_tlist(tline);
2843 if (!evalresult)
2844 return DIRECTIVE_FOUND;
2845 if (tokval.t_type)
H. Peter Anvin917a3492008-09-24 09:14:49 -07002846 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +00002847 "trailing garbage after expression ignored");
2848 if (!is_simple(evalresult)) {
2849 error(ERR_NONFATAL, "non-constant value given to `%%rotate'");
2850 return DIRECTIVE_FOUND;
2851 }
2852 mmac = istk->mstk;
2853 while (mmac && !mmac->name) /* avoid mistaking %reps for macros */
2854 mmac = mmac->next_active;
2855 if (!mmac) {
2856 error(ERR_NONFATAL, "`%%rotate' invoked outside a macro call");
2857 } else if (mmac->nparam == 0) {
2858 error(ERR_NONFATAL,
2859 "`%%rotate' invoked within macro without parameters");
2860 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002861 int rotate = mmac->rotate + reloc_value(evalresult);
H. Peter Anvin734b1882002-04-30 21:01:08 +00002862
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002863 rotate %= (int)mmac->nparam;
2864 if (rotate < 0)
2865 rotate += mmac->nparam;
H. Peter Anvin70653092007-10-19 14:42:29 -07002866
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002867 mmac->rotate = rotate;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002868 }
2869 return DIRECTIVE_FOUND;
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00002870
H. Peter Anvine2c80182005-01-15 22:15:51 +00002871 case PP_REP:
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002872 nolist = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002873 do {
2874 tline = tline->next;
2875 } while (tok_type_(tline, TOK_WHITESPACE));
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00002876
H. Peter Anvine2c80182005-01-15 22:15:51 +00002877 if (tok_type_(tline, TOK_ID) &&
2878 nasm_stricmp(tline->text, ".nolist") == 0) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002879 nolist = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002880 do {
2881 tline = tline->next;
2882 } while (tok_type_(tline, TOK_WHITESPACE));
2883 }
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00002884
H. Peter Anvine2c80182005-01-15 22:15:51 +00002885 if (tline) {
2886 t = expand_smacro(tline);
2887 tptr = &t;
2888 tokval.t_type = TOKEN_INVALID;
2889 evalresult =
2890 evaluate(ppscan, tptr, &tokval, NULL, pass, error, NULL);
2891 if (!evalresult) {
2892 free_tlist(origline);
2893 return DIRECTIVE_FOUND;
2894 }
2895 if (tokval.t_type)
H. Peter Anvin917a3492008-09-24 09:14:49 -07002896 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +00002897 "trailing garbage after expression ignored");
2898 if (!is_simple(evalresult)) {
2899 error(ERR_NONFATAL, "non-constant value given to `%%rep'");
2900 return DIRECTIVE_FOUND;
2901 }
Cyrill Gorcunove091d6e2010-08-09 13:58:22 +04002902 count = reloc_value(evalresult);
2903 if (count >= REP_LIMIT) {
Cyrill Gorcunov71f4f842010-08-09 20:17:17 +04002904 error(ERR_NONFATAL, "`%%rep' value exceeds limit");
Cyrill Gorcunove091d6e2010-08-09 13:58:22 +04002905 count = 0;
2906 } else
2907 count++;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002908 } else {
2909 error(ERR_NONFATAL, "`%%rep' expects a repeat count");
H. Peter Anvinf8ba53e2007-10-11 10:11:57 -07002910 count = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002911 }
2912 free_tlist(origline);
H. Peter Anvin734b1882002-04-30 21:01:08 +00002913
H. Peter Anvine2c80182005-01-15 22:15:51 +00002914 tmp_defining = defining;
2915 defining = nasm_malloc(sizeof(MMacro));
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002916 defining->prev = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002917 defining->name = NULL; /* flags this macro as a %rep block */
H. Peter Anvin70055962007-10-11 00:05:31 -07002918 defining->casesense = false;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002919 defining->plus = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002920 defining->nolist = nolist;
H. Peter Anvinf8ba53e2007-10-11 10:11:57 -07002921 defining->in_progress = count;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002922 defining->max_depth = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002923 defining->nparam_min = defining->nparam_max = 0;
2924 defining->defaults = NULL;
2925 defining->dlist = NULL;
2926 defining->expansion = NULL;
2927 defining->next_active = istk->mstk;
2928 defining->rep_nest = tmp_defining;
2929 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002930
H. Peter Anvine2c80182005-01-15 22:15:51 +00002931 case PP_ENDREP:
2932 if (!defining || defining->name) {
2933 error(ERR_NONFATAL, "`%%endrep': no matching `%%rep'");
2934 return DIRECTIVE_FOUND;
2935 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00002936
H. Peter Anvine2c80182005-01-15 22:15:51 +00002937 /*
2938 * Now we have a "macro" defined - although it has no name
2939 * and we won't be entering it in the hash tables - we must
2940 * push a macro-end marker for it on to istk->expansion.
2941 * After that, it will take care of propagating itself (a
2942 * macro-end marker line for a macro which is really a %rep
2943 * block will cause the macro to be re-expanded, complete
2944 * with another macro-end marker to ensure the process
2945 * continues) until the whole expansion is forcibly removed
2946 * from istk->expansion by a %exitrep.
2947 */
2948 l = nasm_malloc(sizeof(Line));
2949 l->next = istk->expansion;
2950 l->finishes = defining;
2951 l->first = NULL;
2952 istk->expansion = l;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002953
H. Peter Anvine2c80182005-01-15 22:15:51 +00002954 istk->mstk = defining;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002955
H. Peter Anvine2c80182005-01-15 22:15:51 +00002956 list->uplevel(defining->nolist ? LIST_MACRO_NOLIST : LIST_MACRO);
2957 tmp_defining = defining;
2958 defining = defining->rep_nest;
2959 free_tlist(origline);
2960 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002961
H. Peter Anvine2c80182005-01-15 22:15:51 +00002962 case PP_EXITREP:
2963 /*
2964 * We must search along istk->expansion until we hit a
2965 * macro-end marker for a macro with no name. Then we set
2966 * its `in_progress' flag to 0.
2967 */
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04002968 list_for_each(l, istk->expansion)
H. Peter Anvine2c80182005-01-15 22:15:51 +00002969 if (l->finishes && !l->finishes->name)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002970 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002971
H. Peter Anvine2c80182005-01-15 22:15:51 +00002972 if (l)
Charles Crayned4200be2008-07-12 16:42:33 -07002973 l->finishes->in_progress = 1;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002974 else
2975 error(ERR_NONFATAL, "`%%exitrep' not within `%%rep' block");
2976 free_tlist(origline);
2977 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002978
H. Peter Anvine2c80182005-01-15 22:15:51 +00002979 case PP_XDEFINE:
2980 case PP_IXDEFINE:
2981 case PP_DEFINE:
2982 case PP_IDEFINE:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002983 casesense = (i == PP_DEFINE || i == PP_XDEFINE);
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07002984
H. Peter Anvine2c80182005-01-15 22:15:51 +00002985 tline = tline->next;
2986 skip_white_(tline);
2987 tline = expand_id(tline);
2988 if (!tline || (tline->type != TOK_ID &&
2989 (tline->type != TOK_PREPROC_ID ||
2990 tline->text[1] != '$'))) {
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07002991 error(ERR_NONFATAL, "`%s' expects a macro identifier",
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002992 pp_directives[i]);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002993 free_tlist(origline);
2994 return DIRECTIVE_FOUND;
2995 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00002996
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08002997 ctx = get_ctx(tline->text, &mname, false);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002998 last = tline;
2999 param_start = tline = tline->next;
3000 nparam = 0;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003001
H. Peter Anvine2c80182005-01-15 22:15:51 +00003002 /* Expand the macro definition now for %xdefine and %ixdefine */
3003 if ((i == PP_XDEFINE) || (i == PP_IXDEFINE))
3004 tline = expand_smacro(tline);
H. Peter Anvin734b1882002-04-30 21:01:08 +00003005
H. Peter Anvine2c80182005-01-15 22:15:51 +00003006 if (tok_is_(tline, "(")) {
3007 /*
3008 * This macro has parameters.
3009 */
H. Peter Anvin734b1882002-04-30 21:01:08 +00003010
H. Peter Anvine2c80182005-01-15 22:15:51 +00003011 tline = tline->next;
3012 while (1) {
3013 skip_white_(tline);
3014 if (!tline) {
3015 error(ERR_NONFATAL, "parameter identifier expected");
3016 free_tlist(origline);
3017 return DIRECTIVE_FOUND;
3018 }
3019 if (tline->type != TOK_ID) {
3020 error(ERR_NONFATAL,
3021 "`%s': parameter identifier expected",
3022 tline->text);
3023 free_tlist(origline);
3024 return DIRECTIVE_FOUND;
3025 }
3026 tline->type = TOK_SMAC_PARAM + nparam++;
3027 tline = tline->next;
3028 skip_white_(tline);
3029 if (tok_is_(tline, ",")) {
3030 tline = tline->next;
H. Peter Anvinca348b62008-07-23 10:49:26 -04003031 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003032 if (!tok_is_(tline, ")")) {
3033 error(ERR_NONFATAL,
3034 "`)' expected to terminate macro template");
3035 free_tlist(origline);
3036 return DIRECTIVE_FOUND;
3037 }
3038 break;
3039 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00003040 }
3041 last = tline;
3042 tline = tline->next;
3043 }
3044 if (tok_type_(tline, TOK_WHITESPACE))
3045 last = tline, tline = tline->next;
3046 macro_start = NULL;
3047 last->next = NULL;
3048 t = tline;
3049 while (t) {
3050 if (t->type == TOK_ID) {
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04003051 list_for_each(tt, param_start)
H. Peter Anvine2c80182005-01-15 22:15:51 +00003052 if (tt->type >= TOK_SMAC_PARAM &&
3053 !strcmp(tt->text, t->text))
3054 t->type = tt->type;
3055 }
3056 tt = t->next;
3057 t->next = macro_start;
3058 macro_start = t;
3059 t = tt;
3060 }
3061 /*
3062 * Good. We now have a macro name, a parameter count, and a
3063 * token list (in reverse order) for an expansion. We ought
3064 * to be OK just to create an SMacro, store it, and let
3065 * free_tlist have the rest of the line (which we have
3066 * carefully re-terminated after chopping off the expansion
3067 * from the end).
3068 */
H. Peter Anvin4db5a162007-10-11 13:42:09 -07003069 define_smacro(ctx, mname, casesense, nparam, macro_start);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003070 free_tlist(origline);
3071 return DIRECTIVE_FOUND;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003072
H. Peter Anvine2c80182005-01-15 22:15:51 +00003073 case PP_UNDEF:
3074 tline = tline->next;
3075 skip_white_(tline);
3076 tline = expand_id(tline);
3077 if (!tline || (tline->type != TOK_ID &&
3078 (tline->type != TOK_PREPROC_ID ||
3079 tline->text[1] != '$'))) {
3080 error(ERR_NONFATAL, "`%%undef' expects a macro identifier");
3081 free_tlist(origline);
3082 return DIRECTIVE_FOUND;
3083 }
3084 if (tline->next) {
H. Peter Anvin917a3492008-09-24 09:14:49 -07003085 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +00003086 "trailing garbage after macro name ignored");
3087 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003088
H. Peter Anvine2c80182005-01-15 22:15:51 +00003089 /* Find the context that symbol belongs to */
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08003090 ctx = get_ctx(tline->text, &mname, false);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003091 undef_smacro(ctx, mname);
3092 free_tlist(origline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003093 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003094
H. Peter Anvin9e200162008-06-04 17:23:14 -07003095 case PP_DEFSTR:
3096 case PP_IDEFSTR:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003097 casesense = (i == PP_DEFSTR);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003098
3099 tline = tline->next;
3100 skip_white_(tline);
3101 tline = expand_id(tline);
3102 if (!tline || (tline->type != TOK_ID &&
3103 (tline->type != TOK_PREPROC_ID ||
3104 tline->text[1] != '$'))) {
3105 error(ERR_NONFATAL, "`%s' expects a macro identifier",
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003106 pp_directives[i]);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003107 free_tlist(origline);
3108 return DIRECTIVE_FOUND;
3109 }
3110
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08003111 ctx = get_ctx(tline->text, &mname, false);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003112 last = tline;
3113 tline = expand_smacro(tline->next);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003114 last->next = NULL;
H. Peter Anvin9e200162008-06-04 17:23:14 -07003115
3116 while (tok_type_(tline, TOK_WHITESPACE))
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003117 tline = delete_Token(tline);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003118
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003119 p = detoken(tline, false);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003120 macro_start = nasm_malloc(sizeof(*macro_start));
3121 macro_start->next = NULL;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003122 macro_start->text = nasm_quote(p, strlen(p));
3123 macro_start->type = TOK_STRING;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003124 macro_start->a.mac = NULL;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003125 nasm_free(p);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003126
3127 /*
3128 * We now have a macro name, an implicit parameter count of
3129 * zero, and a string token to use as an expansion. Create
3130 * and store an SMacro.
3131 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003132 define_smacro(ctx, mname, casesense, 0, macro_start);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003133 free_tlist(origline);
3134 return DIRECTIVE_FOUND;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003135
H. Peter Anvin2f55bda2009-07-14 15:04:04 -04003136 case PP_DEFTOK:
3137 case PP_IDEFTOK:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003138 casesense = (i == PP_DEFTOK);
3139
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003140 tline = tline->next;
3141 skip_white_(tline);
3142 tline = expand_id(tline);
3143 if (!tline || (tline->type != TOK_ID &&
3144 (tline->type != TOK_PREPROC_ID ||
3145 tline->text[1] != '$'))) {
3146 error(ERR_NONFATAL,
3147 "`%s' expects a macro identifier as first parameter",
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003148 pp_directives[i]);
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003149 free_tlist(origline);
3150 return DIRECTIVE_FOUND;
3151 }
3152 ctx = get_ctx(tline->text, &mname, false);
3153 last = tline;
3154 tline = expand_smacro(tline->next);
3155 last->next = NULL;
3156
3157 t = tline;
3158 while (tok_type_(t, TOK_WHITESPACE))
3159 t = t->next;
3160 /* t should now point to the string */
3161 if (t->type != TOK_STRING) {
3162 error(ERR_NONFATAL,
3163 "`%s` requires string as second parameter",
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003164 pp_directives[i]);
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003165 free_tlist(tline);
3166 free_tlist(origline);
3167 return DIRECTIVE_FOUND;
3168 }
3169
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003170 nasm_unquote_cstr(t->text, i);
3171 macro_start = tokenize(t->text);
3172
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003173 /*
3174 * We now have a macro name, an implicit parameter count of
3175 * zero, and a numeric token to use as an expansion. Create
3176 * and store an SMacro.
3177 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003178 define_smacro(ctx, mname, casesense, 0, macro_start);
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003179 free_tlist(tline);
3180 free_tlist(origline);
3181 return DIRECTIVE_FOUND;
H. Peter Anvin9e200162008-06-04 17:23:14 -07003182
H. Peter Anvin418ca702008-05-30 10:42:30 -07003183 case PP_PATHSEARCH:
3184 {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003185 FILE *fp;
3186 StrList *xsl = NULL;
3187 StrList **xst = &xsl;
H. Peter Anvin418ca702008-05-30 10:42:30 -07003188
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003189 casesense = true;
H. Peter Anvin418ca702008-05-30 10:42:30 -07003190
3191 tline = tline->next;
3192 skip_white_(tline);
3193 tline = expand_id(tline);
3194 if (!tline || (tline->type != TOK_ID &&
3195 (tline->type != TOK_PREPROC_ID ||
3196 tline->text[1] != '$'))) {
3197 error(ERR_NONFATAL,
3198 "`%%pathsearch' expects a macro identifier as first parameter");
3199 free_tlist(origline);
3200 return DIRECTIVE_FOUND;
3201 }
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08003202 ctx = get_ctx(tline->text, &mname, false);
H. Peter Anvin418ca702008-05-30 10:42:30 -07003203 last = tline;
3204 tline = expand_smacro(tline->next);
3205 last->next = NULL;
3206
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003207 t = tline;
H. Peter Anvin418ca702008-05-30 10:42:30 -07003208 while (tok_type_(t, TOK_WHITESPACE))
3209 t = t->next;
3210
3211 if (!t || (t->type != TOK_STRING &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003212 t->type != TOK_INTERNAL_STRING)) {
H. Peter Anvin418ca702008-05-30 10:42:30 -07003213 error(ERR_NONFATAL, "`%%pathsearch' expects a file name");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003214 free_tlist(tline);
H. Peter Anvin418ca702008-05-30 10:42:30 -07003215 free_tlist(origline);
3216 return DIRECTIVE_FOUND; /* but we did _something_ */
3217 }
3218 if (t->next)
H. Peter Anvin917a3492008-09-24 09:14:49 -07003219 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvin418ca702008-05-30 10:42:30 -07003220 "trailing garbage after `%%pathsearch' ignored");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003221 p = t->text;
H. Peter Anvin427cc912008-06-01 21:43:03 -07003222 if (t->type != TOK_INTERNAL_STRING)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003223 nasm_unquote(p, NULL);
H. Peter Anvin418ca702008-05-30 10:42:30 -07003224
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003225 fp = inc_fopen(p, &xsl, &xst, true);
3226 if (fp) {
3227 p = xsl->str;
3228 fclose(fp); /* Don't actually care about the file */
3229 }
H. Peter Anvin418ca702008-05-30 10:42:30 -07003230 macro_start = nasm_malloc(sizeof(*macro_start));
3231 macro_start->next = NULL;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003232 macro_start->text = nasm_quote(p, strlen(p));
3233 macro_start->type = TOK_STRING;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003234 macro_start->a.mac = NULL;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003235 if (xsl)
3236 nasm_free(xsl);
H. Peter Anvin418ca702008-05-30 10:42:30 -07003237
3238 /*
3239 * We now have a macro name, an implicit parameter count of
3240 * zero, and a string token to use as an expansion. Create
3241 * and store an SMacro.
3242 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003243 define_smacro(ctx, mname, casesense, 0, macro_start);
3244 free_tlist(tline);
H. Peter Anvin418ca702008-05-30 10:42:30 -07003245 free_tlist(origline);
3246 return DIRECTIVE_FOUND;
3247 }
3248
H. Peter Anvine2c80182005-01-15 22:15:51 +00003249 case PP_STRLEN:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003250 casesense = true;
H. Peter Anvin70653092007-10-19 14:42:29 -07003251
H. Peter Anvine2c80182005-01-15 22:15:51 +00003252 tline = tline->next;
3253 skip_white_(tline);
3254 tline = expand_id(tline);
3255 if (!tline || (tline->type != TOK_ID &&
3256 (tline->type != TOK_PREPROC_ID ||
3257 tline->text[1] != '$'))) {
3258 error(ERR_NONFATAL,
3259 "`%%strlen' expects a macro identifier as first parameter");
3260 free_tlist(origline);
3261 return DIRECTIVE_FOUND;
3262 }
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08003263 ctx = get_ctx(tline->text, &mname, false);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003264 last = tline;
3265 tline = expand_smacro(tline->next);
3266 last->next = NULL;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003267
H. Peter Anvine2c80182005-01-15 22:15:51 +00003268 t = tline;
3269 while (tok_type_(t, TOK_WHITESPACE))
3270 t = t->next;
3271 /* t should now point to the string */
Cyrill Gorcunov4e1d5ab2010-07-23 18:51:51 +04003272 if (!tok_type_(t, TOK_STRING)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00003273 error(ERR_NONFATAL,
3274 "`%%strlen` requires string as second parameter");
3275 free_tlist(tline);
3276 free_tlist(origline);
3277 return DIRECTIVE_FOUND;
3278 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003279
H. Peter Anvine2c80182005-01-15 22:15:51 +00003280 macro_start = nasm_malloc(sizeof(*macro_start));
3281 macro_start->next = NULL;
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07003282 make_tok_num(macro_start, nasm_unquote(t->text, NULL));
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003283 macro_start->a.mac = NULL;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003284
H. Peter Anvine2c80182005-01-15 22:15:51 +00003285 /*
3286 * We now have a macro name, an implicit parameter count of
3287 * zero, and a numeric token to use as an expansion. Create
3288 * and store an SMacro.
3289 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003290 define_smacro(ctx, mname, casesense, 0, macro_start);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003291 free_tlist(tline);
3292 free_tlist(origline);
3293 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003294
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003295 case PP_STRCAT:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003296 casesense = true;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003297
3298 tline = tline->next;
3299 skip_white_(tline);
3300 tline = expand_id(tline);
3301 if (!tline || (tline->type != TOK_ID &&
3302 (tline->type != TOK_PREPROC_ID ||
3303 tline->text[1] != '$'))) {
3304 error(ERR_NONFATAL,
3305 "`%%strcat' expects a macro identifier as first parameter");
3306 free_tlist(origline);
3307 return DIRECTIVE_FOUND;
3308 }
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08003309 ctx = get_ctx(tline->text, &mname, false);
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003310 last = tline;
3311 tline = expand_smacro(tline->next);
3312 last->next = NULL;
3313
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003314 len = 0;
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04003315 list_for_each(t, tline) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003316 switch (t->type) {
3317 case TOK_WHITESPACE:
3318 break;
3319 case TOK_STRING:
3320 len += t->a.len = nasm_unquote(t->text, NULL);
3321 break;
3322 case TOK_OTHER:
3323 if (!strcmp(t->text, ",")) /* permit comma separators */
3324 break;
3325 /* else fall through */
3326 default:
3327 error(ERR_NONFATAL,
3328 "non-string passed to `%%strcat' (%d)", t->type);
3329 free_tlist(tline);
3330 free_tlist(origline);
3331 return DIRECTIVE_FOUND;
3332 }
3333 }
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003334
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003335 p = pp = nasm_malloc(len);
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04003336 list_for_each(t, tline) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003337 if (t->type == TOK_STRING) {
3338 memcpy(p, t->text, t->a.len);
3339 p += t->a.len;
3340 }
3341 }
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003342
3343 /*
3344 * We now have a macro name, an implicit parameter count of
3345 * zero, and a numeric token to use as an expansion. Create
3346 * and store an SMacro.
3347 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003348 macro_start = new_Token(NULL, TOK_STRING, NULL, 0);
3349 macro_start->text = nasm_quote(pp, len);
3350 nasm_free(pp);
3351 define_smacro(ctx, mname, casesense, 0, macro_start);
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003352 free_tlist(tline);
3353 free_tlist(origline);
3354 return DIRECTIVE_FOUND;
3355
H. Peter Anvine2c80182005-01-15 22:15:51 +00003356 case PP_SUBSTR:
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003357 {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003358 int64_t a1, a2;
3359 size_t len;
H. Peter Anvind2456592008-06-19 15:04:18 -07003360
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003361 casesense = true;
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07003362
H. Peter Anvine2c80182005-01-15 22:15:51 +00003363 tline = tline->next;
3364 skip_white_(tline);
3365 tline = expand_id(tline);
3366 if (!tline || (tline->type != TOK_ID &&
3367 (tline->type != TOK_PREPROC_ID ||
3368 tline->text[1] != '$'))) {
3369 error(ERR_NONFATAL,
3370 "`%%substr' expects a macro identifier as first parameter");
3371 free_tlist(origline);
3372 return DIRECTIVE_FOUND;
3373 }
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08003374 ctx = get_ctx(tline->text, &mname, false);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003375 last = tline;
3376 tline = expand_smacro(tline->next);
3377 last->next = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003378
H. Peter Anvine2c80182005-01-15 22:15:51 +00003379 t = tline->next;
3380 while (tok_type_(t, TOK_WHITESPACE))
3381 t = t->next;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003382
H. Peter Anvine2c80182005-01-15 22:15:51 +00003383 /* t should now point to the string */
3384 if (t->type != TOK_STRING) {
3385 error(ERR_NONFATAL,
3386 "`%%substr` requires string as second parameter");
3387 free_tlist(tline);
3388 free_tlist(origline);
3389 return DIRECTIVE_FOUND;
3390 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003391
H. Peter Anvine2c80182005-01-15 22:15:51 +00003392 tt = t->next;
3393 tptr = &tt;
3394 tokval.t_type = TOKEN_INVALID;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003395 evalresult = evaluate(ppscan, tptr, &tokval, NULL,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003396 pass, error, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003397 if (!evalresult) {
3398 free_tlist(tline);
3399 free_tlist(origline);
3400 return DIRECTIVE_FOUND;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003401 } else if (!is_simple(evalresult)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00003402 error(ERR_NONFATAL, "non-constant value given to `%%substr`");
3403 free_tlist(tline);
3404 free_tlist(origline);
3405 return DIRECTIVE_FOUND;
3406 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003407 a1 = evalresult->value-1;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003408
3409 while (tok_type_(tt, TOK_WHITESPACE))
3410 tt = tt->next;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003411 if (!tt) {
3412 a2 = 1; /* Backwards compatibility: one character */
3413 } else {
3414 tokval.t_type = TOKEN_INVALID;
3415 evalresult = evaluate(ppscan, tptr, &tokval, NULL,
3416 pass, error, NULL);
3417 if (!evalresult) {
3418 free_tlist(tline);
3419 free_tlist(origline);
3420 return DIRECTIVE_FOUND;
3421 } else if (!is_simple(evalresult)) {
3422 error(ERR_NONFATAL, "non-constant value given to `%%substr`");
3423 free_tlist(tline);
3424 free_tlist(origline);
3425 return DIRECTIVE_FOUND;
3426 }
3427 a2 = evalresult->value;
3428 }
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003429
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003430 len = nasm_unquote(t->text, NULL);
3431 if (a2 < 0)
3432 a2 = a2+1+len-a1;
3433 if (a1+a2 > (int64_t)len)
3434 a2 = len-a1;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003435
H. Peter Anvine2c80182005-01-15 22:15:51 +00003436 macro_start = nasm_malloc(sizeof(*macro_start));
3437 macro_start->next = NULL;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003438 macro_start->text = nasm_quote((a1 < 0) ? "" : t->text+a1, a2);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003439 macro_start->type = TOK_STRING;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003440 macro_start->a.mac = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003441
H. Peter Anvine2c80182005-01-15 22:15:51 +00003442 /*
3443 * We now have a macro name, an implicit parameter count of
3444 * zero, and a numeric token to use as an expansion. Create
3445 * and store an SMacro.
3446 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003447 define_smacro(ctx, mname, casesense, 0, macro_start);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003448 free_tlist(tline);
3449 free_tlist(origline);
3450 return DIRECTIVE_FOUND;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003451 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003452
H. Peter Anvine2c80182005-01-15 22:15:51 +00003453 case PP_ASSIGN:
3454 case PP_IASSIGN:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003455 casesense = (i == PP_ASSIGN);
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07003456
H. Peter Anvine2c80182005-01-15 22:15:51 +00003457 tline = tline->next;
3458 skip_white_(tline);
3459 tline = expand_id(tline);
3460 if (!tline || (tline->type != TOK_ID &&
3461 (tline->type != TOK_PREPROC_ID ||
3462 tline->text[1] != '$'))) {
3463 error(ERR_NONFATAL,
3464 "`%%%sassign' expects a macro identifier",
3465 (i == PP_IASSIGN ? "i" : ""));
3466 free_tlist(origline);
3467 return DIRECTIVE_FOUND;
3468 }
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08003469 ctx = get_ctx(tline->text, &mname, false);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003470 last = tline;
3471 tline = expand_smacro(tline->next);
3472 last->next = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003473
H. Peter Anvine2c80182005-01-15 22:15:51 +00003474 t = tline;
3475 tptr = &t;
3476 tokval.t_type = TOKEN_INVALID;
3477 evalresult =
3478 evaluate(ppscan, tptr, &tokval, NULL, pass, error, NULL);
3479 free_tlist(tline);
3480 if (!evalresult) {
3481 free_tlist(origline);
3482 return DIRECTIVE_FOUND;
3483 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003484
H. Peter Anvine2c80182005-01-15 22:15:51 +00003485 if (tokval.t_type)
H. Peter Anvin917a3492008-09-24 09:14:49 -07003486 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +00003487 "trailing garbage after expression ignored");
H. Peter Anvin734b1882002-04-30 21:01:08 +00003488
H. Peter Anvine2c80182005-01-15 22:15:51 +00003489 if (!is_simple(evalresult)) {
3490 error(ERR_NONFATAL,
3491 "non-constant value given to `%%%sassign'",
3492 (i == PP_IASSIGN ? "i" : ""));
3493 free_tlist(origline);
3494 return DIRECTIVE_FOUND;
3495 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003496
H. Peter Anvine2c80182005-01-15 22:15:51 +00003497 macro_start = nasm_malloc(sizeof(*macro_start));
3498 macro_start->next = NULL;
3499 make_tok_num(macro_start, reloc_value(evalresult));
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003500 macro_start->a.mac = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003501
H. Peter Anvine2c80182005-01-15 22:15:51 +00003502 /*
3503 * We now have a macro name, an implicit parameter count of
3504 * zero, and a numeric token to use as an expansion. Create
3505 * and store an SMacro.
3506 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003507 define_smacro(ctx, mname, casesense, 0, macro_start);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003508 free_tlist(origline);
3509 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003510
H. Peter Anvine2c80182005-01-15 22:15:51 +00003511 case PP_LINE:
3512 /*
3513 * Syntax is `%line nnn[+mmm] [filename]'
3514 */
3515 tline = tline->next;
3516 skip_white_(tline);
3517 if (!tok_type_(tline, TOK_NUMBER)) {
3518 error(ERR_NONFATAL, "`%%line' expects line number");
3519 free_tlist(origline);
3520 return DIRECTIVE_FOUND;
3521 }
H. Peter Anvin70055962007-10-11 00:05:31 -07003522 k = readnum(tline->text, &err);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003523 m = 1;
3524 tline = tline->next;
3525 if (tok_is_(tline, "+")) {
3526 tline = tline->next;
3527 if (!tok_type_(tline, TOK_NUMBER)) {
3528 error(ERR_NONFATAL, "`%%line' expects line increment");
3529 free_tlist(origline);
3530 return DIRECTIVE_FOUND;
3531 }
H. Peter Anvin70055962007-10-11 00:05:31 -07003532 m = readnum(tline->text, &err);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003533 tline = tline->next;
3534 }
3535 skip_white_(tline);
3536 src_set_linnum(k);
3537 istk->lineinc = m;
3538 if (tline) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07003539 nasm_free(src_set_fname(detoken(tline, false)));
H. Peter Anvine2c80182005-01-15 22:15:51 +00003540 }
3541 free_tlist(origline);
3542 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003543
H. Peter Anvine2c80182005-01-15 22:15:51 +00003544 default:
3545 error(ERR_FATAL,
3546 "preprocessor directive `%s' not yet implemented",
H. Peter Anvin4169a472007-09-12 01:29:43 +00003547 pp_directives[i]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003548 return DIRECTIVE_FOUND;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003549 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003550}
3551
3552/*
H. Peter Anvin76690a12002-04-30 20:52:49 +00003553 * Ensure that a macro parameter contains a condition code and
3554 * nothing else. Return the condition code index if so, or -1
3555 * otherwise.
3556 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003557static int find_cc(Token * t)
H. Peter Anvineba20a72002-04-30 20:53:55 +00003558{
H. Peter Anvin76690a12002-04-30 20:52:49 +00003559 Token *tt;
3560 int i, j, k, m;
3561
H. Peter Anvin25a99342007-09-22 17:45:45 -07003562 if (!t)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003563 return -1; /* Probably a %+ without a space */
H. Peter Anvin25a99342007-09-22 17:45:45 -07003564
H. Peter Anvineba20a72002-04-30 20:53:55 +00003565 skip_white_(t);
H. Peter Anvin76690a12002-04-30 20:52:49 +00003566 if (t->type != TOK_ID)
H. Peter Anvine2c80182005-01-15 22:15:51 +00003567 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003568 tt = t->next;
H. Peter Anvineba20a72002-04-30 20:53:55 +00003569 skip_white_(tt);
H. Peter Anvin76690a12002-04-30 20:52:49 +00003570 if (tt && (tt->type != TOK_OTHER || strcmp(tt->text, ",")))
H. Peter Anvine2c80182005-01-15 22:15:51 +00003571 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003572
3573 i = -1;
Cyrill Gorcunova7319242010-06-03 22:04:36 +04003574 j = ARRAY_SIZE(conditions);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003575 while (j - i > 1) {
3576 k = (j + i) / 2;
3577 m = nasm_stricmp(t->text, conditions[k]);
3578 if (m == 0) {
3579 i = k;
3580 j = -2;
3581 break;
3582 } else if (m < 0) {
3583 j = k;
3584 } else
3585 i = k;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003586 }
3587 if (j != -2)
H. Peter Anvine2c80182005-01-15 22:15:51 +00003588 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003589 return i;
3590}
3591
H. Peter Anvind784a082009-04-20 14:01:18 -07003592static bool paste_tokens(Token **head, bool handle_paste_tokens)
3593{
3594 Token **tail, *t, *tt;
3595 Token **paste_head;
3596 bool did_paste = false;
3597 char *tmp;
3598
3599 /* Now handle token pasting... */
3600 paste_head = NULL;
3601 tail = head;
3602 while ((t = *tail) && (tt = t->next)) {
3603 switch (t->type) {
3604 case TOK_WHITESPACE:
3605 if (tt->type == TOK_WHITESPACE) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003606 /* Zap adjacent whitespace tokens */
H. Peter Anvind784a082009-04-20 14:01:18 -07003607 t->next = delete_Token(tt);
3608 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003609 /* Do not advance paste_head here */
3610 tail = &t->next;
3611 }
H. Peter Anvind784a082009-04-20 14:01:18 -07003612 break;
3613 case TOK_ID:
H. Peter Anvind784a082009-04-20 14:01:18 -07003614 case TOK_NUMBER:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003615 case TOK_FLOAT:
3616 {
3617 size_t len = 0;
3618 char *tmp, *p;
H. Peter Anvind784a082009-04-20 14:01:18 -07003619
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003620 while (tt && (tt->type == TOK_ID || tt->type == TOK_PREPROC_ID ||
3621 tt->type == TOK_NUMBER || tt->type == TOK_FLOAT ||
3622 tt->type == TOK_OTHER)) {
3623 len += strlen(tt->text);
3624 tt = tt->next;
3625 }
H. Peter Anvind784a082009-04-20 14:01:18 -07003626
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003627 /*
3628 * Now tt points to the first token after
3629 * the potential paste area...
3630 */
3631 if (tt != t->next) {
3632 /* We have at least two tokens... */
3633 len += strlen(t->text);
3634 p = tmp = nasm_malloc(len+1);
H. Peter Anvind784a082009-04-20 14:01:18 -07003635
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003636 while (t != tt) {
3637 strcpy(p, t->text);
3638 p = strchr(p, '\0');
3639 t = delete_Token(t);
3640 }
H. Peter Anvind784a082009-04-20 14:01:18 -07003641
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003642 t = *tail = tokenize(tmp);
3643 nasm_free(tmp);
H. Peter Anvind784a082009-04-20 14:01:18 -07003644
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003645 while (t->next) {
3646 tail = &t->next;
3647 t = t->next;
3648 }
3649 t->next = tt; /* Attach the remaining token chain */
H. Peter Anvind784a082009-04-20 14:01:18 -07003650
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003651 did_paste = true;
3652 }
3653 paste_head = tail;
3654 tail = &t->next;
3655 break;
3656 }
3657 case TOK_PASTE: /* %+ */
3658 if (handle_paste_tokens) {
3659 /* Zap %+ and whitespace tokens to the right */
3660 while (t && (t->type == TOK_WHITESPACE ||
3661 t->type == TOK_PASTE))
3662 t = *tail = delete_Token(t);
3663 if (!paste_head || !t)
3664 break; /* Nothing to paste with */
3665 tail = paste_head;
3666 t = *tail;
3667 tt = t->next;
3668 while (tok_type_(tt, TOK_WHITESPACE))
3669 tt = t->next = delete_Token(tt);
H. Peter Anvind784a082009-04-20 14:01:18 -07003670
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003671 if (tt) {
3672 tmp = nasm_strcat(t->text, tt->text);
3673 delete_Token(t);
3674 tt = delete_Token(tt);
3675 t = *tail = tokenize(tmp);
3676 nasm_free(tmp);
3677 while (t->next) {
3678 tail = &t->next;
3679 t = t->next;
3680 }
3681 t->next = tt; /* Attach the remaining token chain */
3682 did_paste = true;
3683 }
3684 paste_head = tail;
3685 tail = &t->next;
3686 break;
3687 }
3688 /* else fall through */
3689 default:
Cyrill Gorcunovadabc152010-07-10 02:11:41 +04003690 tail = &t->next;
3691 if (!tok_type_(t->next, TOK_WHITESPACE))
3692 paste_head = tail;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003693 break;
H. Peter Anvind784a082009-04-20 14:01:18 -07003694 }
3695 }
3696 return did_paste;
3697}
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003698
3699/*
3700 * expands to a list of tokens from %{x:y}
3701 */
3702static Token *expand_mmac_params_range(MMacro *mac, Token *tline, Token ***last)
3703{
3704 Token *t = tline, **tt, *tm, *head;
3705 char *pos;
3706 int fst, lst, j, i;
3707
3708 pos = strchr(tline->text, ':');
3709 nasm_assert(pos);
3710
3711 lst = atoi(pos + 1);
3712 fst = atoi(tline->text + 1);
3713
3714 /*
3715 * only macros params are accounted so
3716 * if someone passes %0 -- we reject such
3717 * value(s)
3718 */
3719 if (lst == 0 || fst == 0)
3720 goto err;
3721
3722 /* the values should be sane */
Cyrill Gorcunov2f403752010-06-05 10:47:10 +04003723 if ((fst > (int)mac->nparam || fst < (-(int)mac->nparam)) ||
3724 (lst > (int)mac->nparam || lst < (-(int)mac->nparam)))
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003725 goto err;
3726
Cyrill Gorcunovefb35832010-06-20 01:52:19 +04003727 fst = fst < 0 ? fst + (int)mac->nparam + 1: fst;
3728 lst = lst < 0 ? lst + (int)mac->nparam + 1: lst;
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003729
3730 /* counted from zero */
3731 fst--, lst--;
3732
3733 /*
3734 * it will be at least one token
3735 */
3736 tm = mac->params[(fst + mac->rotate) % mac->nparam];
3737 t = new_Token(NULL, tm->type, tm->text, 0);
3738 head = t, tt = &t->next;
3739 if (fst < lst) {
3740 for (i = fst + 1; i <= lst; i++) {
3741 t = new_Token(NULL, TOK_OTHER, ",", 0);
3742 *tt = t, tt = &t->next;
3743 j = (i + mac->rotate) % mac->nparam;
3744 tm = mac->params[j];
3745 t = new_Token(NULL, tm->type, tm->text, 0);
3746 *tt = t, tt = &t->next;
3747 }
3748 } else {
3749 for (i = fst - 1; i >= lst; i--) {
3750 t = new_Token(NULL, TOK_OTHER, ",", 0);
3751 *tt = t, tt = &t->next;
3752 j = (i + mac->rotate) % mac->nparam;
3753 tm = mac->params[j];
3754 t = new_Token(NULL, tm->type, tm->text, 0);
3755 *tt = t, tt = &t->next;
3756 }
3757 }
3758
3759 *last = tt;
3760 return head;
3761
3762err:
3763 error(ERR_NONFATAL, "`%%{%s}': macro parameters out of range",
3764 &tline->text[1]);
3765 return tline;
3766}
3767
H. Peter Anvin76690a12002-04-30 20:52:49 +00003768/*
3769 * Expand MMacro-local things: parameter references (%0, %n, %+n,
H. Peter Anvin67c63722008-10-26 23:49:00 -07003770 * %-n) and MMacro-local identifiers (%%foo) as well as
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003771 * macro indirection (%[...]) and range (%{..:..}).
H. Peter Anvin76690a12002-04-30 20:52:49 +00003772 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003773static Token *expand_mmac_params(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00003774{
H. Peter Anvin734b1882002-04-30 21:01:08 +00003775 Token *t, *tt, **tail, *thead;
H. Peter Anvin6125b622009-04-08 14:02:25 -07003776 bool changed = false;
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003777 char *pos;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003778
3779 tail = &thead;
3780 thead = NULL;
3781
H. Peter Anvine2c80182005-01-15 22:15:51 +00003782 while (tline) {
3783 if (tline->type == TOK_PREPROC_ID &&
Cyrill Gorcunovca611192010-06-04 09:22:12 +04003784 (((tline->text[1] == '+' || tline->text[1] == '-') && tline->text[2]) ||
3785 (tline->text[1] >= '0' && tline->text[1] <= '9') ||
3786 tline->text[1] == '%')) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00003787 char *text = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003788 int type = 0, cc; /* type = 0 to placate optimisers */
Keith Kaniosa6dfa782007-04-13 16:47:53 +00003789 char tmpbuf[30];
H. Peter Anvin25a99342007-09-22 17:45:45 -07003790 unsigned int n;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003791 int i;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003792 MMacro *mac;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003793
H. Peter Anvine2c80182005-01-15 22:15:51 +00003794 t = tline;
3795 tline = tline->next;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003796
H. Peter Anvine2c80182005-01-15 22:15:51 +00003797 mac = istk->mstk;
3798 while (mac && !mac->name) /* avoid mistaking %reps for macros */
3799 mac = mac->next_active;
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003800 if (!mac) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00003801 error(ERR_NONFATAL, "`%s': not in a macro call", t->text);
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003802 } else {
3803 pos = strchr(t->text, ':');
3804 if (!pos) {
3805 switch (t->text[1]) {
3806 /*
3807 * We have to make a substitution of one of the
3808 * forms %1, %-1, %+1, %%foo, %0.
3809 */
3810 case '0':
3811 type = TOK_NUMBER;
3812 snprintf(tmpbuf, sizeof(tmpbuf), "%d", mac->nparam);
3813 text = nasm_strdup(tmpbuf);
3814 break;
3815 case '%':
H. Peter Anvine2c80182005-01-15 22:15:51 +00003816 type = TOK_ID;
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003817 snprintf(tmpbuf, sizeof(tmpbuf), "..@%"PRIu64".",
3818 mac->unique);
3819 text = nasm_strcat(tmpbuf, t->text + 2);
3820 break;
3821 case '-':
3822 n = atoi(t->text + 2) - 1;
3823 if (n >= mac->nparam)
3824 tt = NULL;
3825 else {
3826 if (mac->nparam > 1)
3827 n = (n + mac->rotate) % mac->nparam;
3828 tt = mac->params[n];
H. Peter Anvine2c80182005-01-15 22:15:51 +00003829 }
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003830 cc = find_cc(tt);
3831 if (cc == -1) {
3832 error(ERR_NONFATAL,
3833 "macro parameter %d is not a condition code",
3834 n + 1);
3835 text = NULL;
3836 } else {
3837 type = TOK_ID;
3838 if (inverse_ccs[cc] == -1) {
3839 error(ERR_NONFATAL,
3840 "condition code `%s' is not invertible",
3841 conditions[cc]);
3842 text = NULL;
3843 } else
3844 text = nasm_strdup(conditions[inverse_ccs[cc]]);
3845 }
3846 break;
3847 case '+':
3848 n = atoi(t->text + 2) - 1;
3849 if (n >= mac->nparam)
3850 tt = NULL;
3851 else {
3852 if (mac->nparam > 1)
3853 n = (n + mac->rotate) % mac->nparam;
3854 tt = mac->params[n];
3855 }
3856 cc = find_cc(tt);
3857 if (cc == -1) {
3858 error(ERR_NONFATAL,
3859 "macro parameter %d is not a condition code",
3860 n + 1);
3861 text = NULL;
3862 } else {
3863 type = TOK_ID;
3864 text = nasm_strdup(conditions[cc]);
3865 }
3866 break;
3867 default:
3868 n = atoi(t->text + 1) - 1;
3869 if (n >= mac->nparam)
3870 tt = NULL;
3871 else {
3872 if (mac->nparam > 1)
3873 n = (n + mac->rotate) % mac->nparam;
3874 tt = mac->params[n];
3875 }
3876 if (tt) {
3877 for (i = 0; i < mac->paramlen[n]; i++) {
3878 *tail = new_Token(NULL, tt->type, tt->text, 0);
3879 tail = &(*tail)->next;
3880 tt = tt->next;
3881 }
3882 }
3883 text = NULL; /* we've done it here */
3884 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003885 }
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003886 } else {
3887 /*
3888 * seems we have a parameters range here
3889 */
3890 Token *head, **last;
3891 head = expand_mmac_params_range(mac, t, &last);
3892 if (head != t) {
3893 *tail = head;
3894 *last = tline;
3895 tline = head;
3896 text = NULL;
3897 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00003898 }
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003899 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00003900 if (!text) {
3901 delete_Token(t);
3902 } else {
3903 *tail = t;
3904 tail = &t->next;
3905 t->type = type;
3906 nasm_free(t->text);
3907 t->text = text;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003908 t->a.mac = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003909 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003910 changed = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003911 continue;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003912 } else if (tline->type == TOK_INDIRECT) {
3913 t = tline;
3914 tline = tline->next;
3915 tt = tokenize(t->text);
3916 tt = expand_mmac_params(tt);
3917 tt = expand_smacro(tt);
3918 *tail = tt;
3919 while (tt) {
3920 tt->a.mac = NULL; /* Necessary? */
3921 tail = &tt->next;
3922 tt = tt->next;
3923 }
3924 delete_Token(t);
3925 changed = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003926 } else {
3927 t = *tail = tline;
3928 tline = tline->next;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003929 t->a.mac = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003930 tail = &t->next;
3931 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00003932 }
H. Peter Anvineba20a72002-04-30 20:53:55 +00003933 *tail = NULL;
H. Peter Anvin67c63722008-10-26 23:49:00 -07003934
H. Peter Anvind784a082009-04-20 14:01:18 -07003935 if (changed)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003936 paste_tokens(&thead, false);
H. Peter Anvin6125b622009-04-08 14:02:25 -07003937
H. Peter Anvin76690a12002-04-30 20:52:49 +00003938 return thead;
3939}
3940
3941/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003942 * Expand all single-line macro calls made in the given line.
3943 * Return the expanded version of the line. The original is deemed
3944 * to be destroyed in the process. (In reality we'll just move
3945 * Tokens from input to output a lot of the time, rather than
3946 * actually bothering to destroy and replicate.)
3947 */
H. Peter Anvincb1cf592007-11-19 12:26:50 -08003948
H. Peter Anvine2c80182005-01-15 22:15:51 +00003949static Token *expand_smacro(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00003950{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003951 Token *t, *tt, *mstart, **tail, *thead;
H. Peter Anvineba20a72002-04-30 20:53:55 +00003952 SMacro *head = NULL, *m;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003953 Token **params;
3954 int *paramsize;
H. Peter Anvin25a99342007-09-22 17:45:45 -07003955 unsigned int nparam, sparam;
H. Peter Anvind784a082009-04-20 14:01:18 -07003956 int brackets;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003957 Token *org_tline = tline;
3958 Context *ctx;
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08003959 const char *mname;
H. Peter Anvin2a15e692007-11-19 13:14:59 -08003960 int deadman = DEADMAN_LIMIT;
H. Peter Anvin8287daf2009-07-07 16:00:58 -07003961 bool expanded;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003962
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003963 /*
3964 * Trick: we should avoid changing the start token pointer since it can
3965 * be contained in "next" field of other token. Because of this
3966 * we allocate a copy of first token and work with it; at the end of
3967 * routine we copy it back
3968 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003969 if (org_tline) {
Cyrill Gorcunoved4a8052010-04-09 15:40:35 +04003970 tline = new_Token(org_tline->next, org_tline->type,
3971 org_tline->text, 0);
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003972 tline->a.mac = org_tline->a.mac;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003973 nasm_free(org_tline->text);
3974 org_tline->text = NULL;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003975 }
3976
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003977 expanded = true; /* Always expand %+ at least once */
H. Peter Anvin8287daf2009-07-07 16:00:58 -07003978
H. Peter Anvincb1cf592007-11-19 12:26:50 -08003979again:
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003980 thead = NULL;
Cyrill Gorcunoved4a8052010-04-09 15:40:35 +04003981 tail = &thead;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003982
H. Peter Anvine2c80182005-01-15 22:15:51 +00003983 while (tline) { /* main token loop */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003984 if (!--deadman) {
3985 error(ERR_NONFATAL, "interminable macro recursion");
Cyrill Gorcunovbd38c8f2009-11-21 11:11:23 +03003986 goto err;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003987 }
H. Peter Anvincb1cf592007-11-19 12:26:50 -08003988
H. Peter Anvine2c80182005-01-15 22:15:51 +00003989 if ((mname = tline->text)) {
3990 /* if this token is a local macro, look in local context */
Cyrill Gorcunovc56d9ad2010-02-11 15:12:19 +03003991 if (tline->type == TOK_ID) {
3992 head = (SMacro *)hash_findix(&smacros, mname);
3993 } else if (tline->type == TOK_PREPROC_ID) {
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08003994 ctx = get_ctx(mname, &mname, true);
Cyrill Gorcunovc56d9ad2010-02-11 15:12:19 +03003995 head = ctx ? (SMacro *)hash_findix(&ctx->localmac, mname) : NULL;
3996 } else
3997 head = NULL;
H. Peter Anvin072771e2008-05-22 13:17:51 -07003998
H. Peter Anvine2c80182005-01-15 22:15:51 +00003999 /*
4000 * We've hit an identifier. As in is_mmacro below, we first
4001 * check whether the identifier is a single-line macro at
4002 * all, then think about checking for parameters if
4003 * necessary.
4004 */
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04004005 list_for_each(m, head)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004006 if (!mstrcmp(m->name, mname, m->casesense))
4007 break;
4008 if (m) {
4009 mstart = tline;
4010 params = NULL;
4011 paramsize = NULL;
4012 if (m->nparam == 0) {
4013 /*
4014 * Simple case: the macro is parameterless. Discard the
4015 * one token that the macro call took, and push the
4016 * expansion back on the to-do stack.
4017 */
4018 if (!m->expansion) {
4019 if (!strcmp("__FILE__", m->name)) {
4020 int32_t num = 0;
4021 char *file = NULL;
4022 src_get(&num, &file);
4023 tline->text = nasm_quote(file, strlen(file));
4024 tline->type = TOK_STRING;
4025 nasm_free(file);
H. Peter Anvine2c80182005-01-15 22:15:51 +00004026 continue;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004027 }
4028 if (!strcmp("__LINE__", m->name)) {
4029 nasm_free(tline->text);
4030 make_tok_num(tline, src_get_linnum());
4031 continue;
4032 }
4033 if (!strcmp("__BITS__", m->name)) {
4034 nasm_free(tline->text);
4035 make_tok_num(tline, globalbits);
4036 continue;
4037 }
4038 tline = delete_Token(tline);
4039 continue;
4040 }
4041 } else {
4042 /*
4043 * Complicated case: at least one macro with this name
H. Peter Anvine2c80182005-01-15 22:15:51 +00004044 * exists and takes parameters. We must find the
4045 * parameters in the call, count them, find the SMacro
4046 * that corresponds to that form of the macro call, and
4047 * substitute for the parameters when we expand. What a
4048 * pain.
4049 */
4050 /*tline = tline->next;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004051 skip_white_(tline); */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004052 do {
4053 t = tline->next;
4054 while (tok_type_(t, TOK_SMAC_END)) {
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004055 t->a.mac->in_progress = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004056 t->text = NULL;
4057 t = tline->next = delete_Token(t);
4058 }
4059 tline = t;
4060 } while (tok_type_(tline, TOK_WHITESPACE));
4061 if (!tok_is_(tline, "(")) {
4062 /*
4063 * This macro wasn't called with parameters: ignore
4064 * the call. (Behaviour borrowed from gnu cpp.)
4065 */
4066 tline = mstart;
4067 m = NULL;
4068 } else {
4069 int paren = 0;
4070 int white = 0;
4071 brackets = 0;
4072 nparam = 0;
4073 sparam = PARAM_DELTA;
4074 params = nasm_malloc(sparam * sizeof(Token *));
4075 params[0] = tline->next;
4076 paramsize = nasm_malloc(sparam * sizeof(int));
4077 paramsize[0] = 0;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07004078 while (true) { /* parameter loop */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004079 /*
4080 * For some unusual expansions
4081 * which concatenates function call
4082 */
4083 t = tline->next;
4084 while (tok_type_(t, TOK_SMAC_END)) {
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004085 t->a.mac->in_progress = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004086 t->text = NULL;
4087 t = tline->next = delete_Token(t);
4088 }
4089 tline = t;
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00004090
H. Peter Anvine2c80182005-01-15 22:15:51 +00004091 if (!tline) {
4092 error(ERR_NONFATAL,
4093 "macro call expects terminating `)'");
4094 break;
4095 }
4096 if (tline->type == TOK_WHITESPACE
4097 && brackets <= 0) {
4098 if (paramsize[nparam])
4099 white++;
4100 else
4101 params[nparam] = tline->next;
4102 continue; /* parameter loop */
4103 }
4104 if (tline->type == TOK_OTHER
4105 && tline->text[1] == 0) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004106 char ch = tline->text[0];
H. Peter Anvine2c80182005-01-15 22:15:51 +00004107 if (ch == ',' && !paren && brackets <= 0) {
4108 if (++nparam >= sparam) {
4109 sparam += PARAM_DELTA;
4110 params = nasm_realloc(params,
Cyrill Gorcunoved4a8052010-04-09 15:40:35 +04004111 sparam * sizeof(Token *));
4112 paramsize = nasm_realloc(paramsize,
4113 sparam * sizeof(int));
H. Peter Anvine2c80182005-01-15 22:15:51 +00004114 }
4115 params[nparam] = tline->next;
4116 paramsize[nparam] = 0;
4117 white = 0;
4118 continue; /* parameter loop */
4119 }
4120 if (ch == '{' &&
4121 (brackets > 0 || (brackets == 0 &&
4122 !paramsize[nparam])))
4123 {
4124 if (!(brackets++)) {
4125 params[nparam] = tline->next;
4126 continue; /* parameter loop */
4127 }
4128 }
4129 if (ch == '}' && brackets > 0)
4130 if (--brackets == 0) {
4131 brackets = -1;
4132 continue; /* parameter loop */
4133 }
4134 if (ch == '(' && !brackets)
4135 paren++;
4136 if (ch == ')' && brackets <= 0)
4137 if (--paren < 0)
4138 break;
4139 }
4140 if (brackets < 0) {
4141 brackets = 0;
4142 error(ERR_NONFATAL, "braces do not "
4143 "enclose all of macro parameter");
4144 }
4145 paramsize[nparam] += white + 1;
4146 white = 0;
4147 } /* parameter loop */
4148 nparam++;
4149 while (m && (m->nparam != nparam ||
4150 mstrcmp(m->name, mname,
4151 m->casesense)))
4152 m = m->next;
4153 if (!m)
H. Peter Anvin917a3492008-09-24 09:14:49 -07004154 error(ERR_WARNING|ERR_PASS1|ERR_WARN_MNP,
H. Peter Anvine2c80182005-01-15 22:15:51 +00004155 "macro `%s' exists, "
4156 "but not taking %d parameters",
4157 mstart->text, nparam);
4158 }
4159 }
4160 if (m && m->in_progress)
4161 m = NULL;
4162 if (!m) { /* in progess or didn't find '(' or wrong nparam */
H. Peter Anvin70653092007-10-19 14:42:29 -07004163 /*
H. Peter Anvine2c80182005-01-15 22:15:51 +00004164 * Design question: should we handle !tline, which
4165 * indicates missing ')' here, or expand those
4166 * macros anyway, which requires the (t) test a few
H. Peter Anvin70653092007-10-19 14:42:29 -07004167 * lines down?
H. Peter Anvine2c80182005-01-15 22:15:51 +00004168 */
4169 nasm_free(params);
4170 nasm_free(paramsize);
4171 tline = mstart;
4172 } else {
4173 /*
4174 * Expand the macro: we are placed on the last token of the
4175 * call, so that we can easily split the call from the
4176 * following tokens. We also start by pushing an SMAC_END
4177 * token for the cycle removal.
4178 */
4179 t = tline;
4180 if (t) {
4181 tline = t->next;
4182 t->next = NULL;
4183 }
4184 tt = new_Token(tline, TOK_SMAC_END, NULL, 0);
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004185 tt->a.mac = m;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07004186 m->in_progress = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004187 tline = tt;
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04004188 list_for_each(t, m->expansion) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00004189 if (t->type >= TOK_SMAC_PARAM) {
4190 Token *pcopy = tline, **ptail = &pcopy;
4191 Token *ttt, *pt;
4192 int i;
H. Peter Anvin734b1882002-04-30 21:01:08 +00004193
H. Peter Anvine2c80182005-01-15 22:15:51 +00004194 ttt = params[t->type - TOK_SMAC_PARAM];
Cyrill Gorcunoved4a8052010-04-09 15:40:35 +04004195 i = paramsize[t->type - TOK_SMAC_PARAM];
4196 while (--i >= 0) {
4197 pt = *ptail = new_Token(tline, ttt->type,
4198 ttt->text, 0);
H. Peter Anvine2c80182005-01-15 22:15:51 +00004199 ptail = &pt->next;
4200 ttt = ttt->next;
4201 }
4202 tline = pcopy;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004203 } else if (t->type == TOK_PREPROC_Q) {
4204 tt = new_Token(tline, TOK_ID, mname, 0);
4205 tline = tt;
4206 } else if (t->type == TOK_PREPROC_QQ) {
4207 tt = new_Token(tline, TOK_ID, m->name, 0);
4208 tline = tt;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004209 } else {
4210 tt = new_Token(tline, t->type, t->text, 0);
4211 tline = tt;
4212 }
4213 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00004214
H. Peter Anvine2c80182005-01-15 22:15:51 +00004215 /*
4216 * Having done that, get rid of the macro call, and clean
4217 * up the parameters.
4218 */
4219 nasm_free(params);
4220 nasm_free(paramsize);
4221 free_tlist(mstart);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004222 expanded = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004223 continue; /* main token loop */
4224 }
4225 }
4226 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004227
H. Peter Anvine2c80182005-01-15 22:15:51 +00004228 if (tline->type == TOK_SMAC_END) {
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004229 tline->a.mac->in_progress = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004230 tline = delete_Token(tline);
4231 } else {
4232 t = *tail = tline;
4233 tline = tline->next;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004234 t->a.mac = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004235 t->next = NULL;
4236 tail = &t->next;
4237 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004238 }
4239
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004240 /*
4241 * Now scan the entire line and look for successive TOK_IDs that resulted
Keith Kaniosb7a89542007-04-12 02:40:54 +00004242 * after expansion (they can't be produced by tokenize()). The successive
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004243 * TOK_IDs should be concatenated.
4244 * Also we look for %+ tokens and concatenate the tokens before and after
4245 * them (without white spaces in between).
4246 */
H. Peter Anvin8287daf2009-07-07 16:00:58 -07004247 if (expanded && paste_tokens(&thead, true)) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004248 /*
4249 * If we concatenated something, *and* we had previously expanded
4250 * an actual macro, scan the lines again for macros...
4251 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004252 tline = thead;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004253 expanded = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004254 goto again;
H. Peter Anvin734b1882002-04-30 21:01:08 +00004255 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004256
Cyrill Gorcunovbd38c8f2009-11-21 11:11:23 +03004257err:
H. Peter Anvine2c80182005-01-15 22:15:51 +00004258 if (org_tline) {
4259 if (thead) {
4260 *org_tline = *thead;
4261 /* since we just gave text to org_line, don't free it */
4262 thead->text = NULL;
4263 delete_Token(thead);
4264 } else {
4265 /* the expression expanded to empty line;
4266 we can't return NULL for some reasons
4267 we just set the line to a single WHITESPACE token. */
4268 memset(org_tline, 0, sizeof(*org_tline));
4269 org_tline->text = NULL;
4270 org_tline->type = TOK_WHITESPACE;
4271 }
4272 thead = org_tline;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004273 }
4274
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004275 return thead;
4276}
4277
4278/*
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004279 * Similar to expand_smacro but used exclusively with macro identifiers
4280 * right before they are fetched in. The reason is that there can be
4281 * identifiers consisting of several subparts. We consider that if there
4282 * are more than one element forming the name, user wants a expansion,
4283 * otherwise it will be left as-is. Example:
4284 *
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004285 * %define %$abc cde
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004286 *
4287 * the identifier %$abc will be left as-is so that the handler for %define
4288 * will suck it and define the corresponding value. Other case:
4289 *
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004290 * %define _%$abc cde
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004291 *
4292 * In this case user wants name to be expanded *before* %define starts
4293 * working, so we'll expand %$abc into something (if it has a value;
4294 * otherwise it will be left as-is) then concatenate all successive
4295 * PP_IDs into one.
4296 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004297static Token *expand_id(Token * tline)
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004298{
4299 Token *cur, *oldnext = NULL;
4300
H. Peter Anvin734b1882002-04-30 21:01:08 +00004301 if (!tline || !tline->next)
H. Peter Anvine2c80182005-01-15 22:15:51 +00004302 return tline;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004303
4304 cur = tline;
4305 while (cur->next &&
H. Peter Anvine2c80182005-01-15 22:15:51 +00004306 (cur->next->type == TOK_ID ||
4307 cur->next->type == TOK_PREPROC_ID
4308 || cur->next->type == TOK_NUMBER))
4309 cur = cur->next;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004310
4311 /* If identifier consists of just one token, don't expand */
4312 if (cur == tline)
H. Peter Anvine2c80182005-01-15 22:15:51 +00004313 return tline;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004314
H. Peter Anvine2c80182005-01-15 22:15:51 +00004315 if (cur) {
4316 oldnext = cur->next; /* Detach the tail past identifier */
4317 cur->next = NULL; /* so that expand_smacro stops here */
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004318 }
4319
H. Peter Anvin734b1882002-04-30 21:01:08 +00004320 tline = expand_smacro(tline);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004321
H. Peter Anvine2c80182005-01-15 22:15:51 +00004322 if (cur) {
4323 /* expand_smacro possibly changhed tline; re-scan for EOL */
4324 cur = tline;
4325 while (cur && cur->next)
4326 cur = cur->next;
4327 if (cur)
4328 cur->next = oldnext;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004329 }
4330
4331 return tline;
4332}
4333
4334/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004335 * Determine whether the given line constitutes a multi-line macro
4336 * call, and return the MMacro structure called if so. Doesn't have
4337 * to check for an initial label - that's taken care of in
4338 * expand_mmacro - but must check numbers of parameters. Guaranteed
4339 * to be called with tline->type == TOK_ID, so the putative macro
4340 * name is easy to find.
4341 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004342static MMacro *is_mmacro(Token * tline, Token *** params_array)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004343{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004344 MMacro *head, *m;
4345 Token **params;
4346 int nparam;
4347
H. Peter Anvin166c2472008-05-28 12:28:58 -07004348 head = (MMacro *) hash_findix(&mmacros, tline->text);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004349
4350 /*
4351 * Efficiency: first we see if any macro exists with the given
4352 * name. If not, we can return NULL immediately. _Then_ we
4353 * count the parameters, and then we look further along the
4354 * list if necessary to find the proper MMacro.
4355 */
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04004356 list_for_each(m, head)
H. Peter Anvine2c80182005-01-15 22:15:51 +00004357 if (!mstrcmp(m->name, tline->text, m->casesense))
4358 break;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004359 if (!m)
H. Peter Anvine2c80182005-01-15 22:15:51 +00004360 return NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004361
4362 /*
4363 * OK, we have a potential macro. Count and demarcate the
4364 * parameters.
4365 */
H. Peter Anvin734b1882002-04-30 21:01:08 +00004366 count_mmac_params(tline->next, &nparam, &params);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004367
4368 /*
4369 * So we know how many parameters we've got. Find the MMacro
4370 * structure that handles this number.
4371 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004372 while (m) {
4373 if (m->nparam_min <= nparam
4374 && (m->plus || nparam <= m->nparam_max)) {
4375 /*
4376 * This one is right. Just check if cycle removal
4377 * prohibits us using it before we actually celebrate...
4378 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004379 if (m->in_progress > m->max_depth) {
4380 if (m->max_depth > 0) {
4381 error(ERR_WARNING,
4382 "reached maximum recursion depth of %i",
4383 m->max_depth);
4384 }
4385 nasm_free(params);
4386 return NULL;
4387 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00004388 /*
4389 * It's right, and we can use it. Add its default
4390 * parameters to the end of our list if necessary.
4391 */
4392 if (m->defaults && nparam < m->nparam_min + m->ndefs) {
4393 params =
4394 nasm_realloc(params,
4395 ((m->nparam_min + m->ndefs +
4396 1) * sizeof(*params)));
4397 while (nparam < m->nparam_min + m->ndefs) {
4398 params[nparam] = m->defaults[nparam - m->nparam_min];
4399 nparam++;
4400 }
4401 }
4402 /*
4403 * If we've gone over the maximum parameter count (and
4404 * we're in Plus mode), ignore parameters beyond
4405 * nparam_max.
4406 */
4407 if (m->plus && nparam > m->nparam_max)
4408 nparam = m->nparam_max;
4409 /*
4410 * Then terminate the parameter list, and leave.
4411 */
4412 if (!params) { /* need this special case */
4413 params = nasm_malloc(sizeof(*params));
4414 nparam = 0;
4415 }
4416 params[nparam] = NULL;
4417 *params_array = params;
4418 return m;
4419 }
4420 /*
4421 * This one wasn't right: look for the next one with the
4422 * same name.
4423 */
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04004424 list_for_each(m, m->next)
H. Peter Anvine2c80182005-01-15 22:15:51 +00004425 if (!mstrcmp(m->name, tline->text, m->casesense))
4426 break;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004427 }
4428
4429 /*
4430 * After all that, we didn't find one with the right number of
4431 * parameters. Issue a warning, and fail to expand the macro.
4432 */
H. Peter Anvin917a3492008-09-24 09:14:49 -07004433 error(ERR_WARNING|ERR_PASS1|ERR_WARN_MNP,
H. Peter Anvine2c80182005-01-15 22:15:51 +00004434 "macro `%s' exists, but not taking %d parameters",
4435 tline->text, nparam);
H. Peter Anvin734b1882002-04-30 21:01:08 +00004436 nasm_free(params);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004437 return NULL;
4438}
4439
Keith Kanios891775e2009-07-11 06:08:54 -05004440
4441/*
4442 * Save MMacro invocation specific fields in
4443 * preparation for a recursive macro expansion
4444 */
4445static void push_mmacro(MMacro *m)
4446{
4447 MMacroInvocation *i;
4448
H. Peter Anvin89cee572009-07-15 09:16:54 -04004449 i = nasm_malloc(sizeof(MMacroInvocation));
4450 i->prev = m->prev;
4451 i->params = m->params;
4452 i->iline = m->iline;
4453 i->nparam = m->nparam;
4454 i->rotate = m->rotate;
4455 i->paramlen = m->paramlen;
4456 i->unique = m->unique;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004457 i->condcnt = m->condcnt;
H. Peter Anvin89cee572009-07-15 09:16:54 -04004458 m->prev = i;
Keith Kanios891775e2009-07-11 06:08:54 -05004459}
4460
4461
4462/*
4463 * Restore MMacro invocation specific fields that were
4464 * saved during a previous recursive macro expansion
4465 */
4466static void pop_mmacro(MMacro *m)
4467{
4468 MMacroInvocation *i;
4469
H. Peter Anvin89cee572009-07-15 09:16:54 -04004470 if (m->prev) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004471 i = m->prev;
4472 m->prev = i->prev;
4473 m->params = i->params;
4474 m->iline = i->iline;
4475 m->nparam = i->nparam;
4476 m->rotate = i->rotate;
4477 m->paramlen = i->paramlen;
4478 m->unique = i->unique;
4479 m->condcnt = i->condcnt;
4480 nasm_free(i);
H. Peter Anvin89cee572009-07-15 09:16:54 -04004481 }
Keith Kanios891775e2009-07-11 06:08:54 -05004482}
4483
4484
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004485/*
4486 * Expand the multi-line macro call made by the given line, if
4487 * there is one to be expanded. If there is, push the expansion on
H. Peter Anvineba20a72002-04-30 20:53:55 +00004488 * istk->expansion and return 1. Otherwise return 0.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004489 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004490static int expand_mmacro(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004491{
4492 Token *startline = tline;
4493 Token *label = NULL;
4494 int dont_prepend = 0;
H. Peter Anvince2233b2008-05-25 21:57:00 -07004495 Token **params, *t, *mtok, *tt;
H. Peter Anvineba20a72002-04-30 20:53:55 +00004496 MMacro *m;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004497 Line *l, *ll;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004498 int i, nparam, *paramlen;
H. Peter Anvinc751e862008-06-09 10:18:45 -07004499 const char *mname;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004500
4501 t = tline;
H. Peter Anvineba20a72002-04-30 20:53:55 +00004502 skip_white_(t);
H. Peter Anvince2233b2008-05-25 21:57:00 -07004503 /* if (!tok_type_(t, TOK_ID)) Lino 02/25/02 */
H. Peter Anvindce1e2f2002-04-30 21:06:37 +00004504 if (!tok_type_(t, TOK_ID) && !tok_type_(t, TOK_PREPROC_ID))
H. Peter Anvine2c80182005-01-15 22:15:51 +00004505 return 0;
H. Peter Anvince2233b2008-05-25 21:57:00 -07004506 mtok = t;
H. Peter Anvin734b1882002-04-30 21:01:08 +00004507 m = is_mmacro(t, &params);
H. Peter Anvinc751e862008-06-09 10:18:45 -07004508 if (m) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004509 mname = t->text;
H. Peter Anvinc751e862008-06-09 10:18:45 -07004510 } else {
H. Peter Anvine2c80182005-01-15 22:15:51 +00004511 Token *last;
4512 /*
4513 * We have an id which isn't a macro call. We'll assume
4514 * it might be a label; we'll also check to see if a
4515 * colon follows it. Then, if there's another id after
4516 * that lot, we'll check it again for macro-hood.
4517 */
4518 label = last = t;
4519 t = t->next;
4520 if (tok_type_(t, TOK_WHITESPACE))
4521 last = t, t = t->next;
4522 if (tok_is_(t, ":")) {
4523 dont_prepend = 1;
4524 last = t, t = t->next;
4525 if (tok_type_(t, TOK_WHITESPACE))
4526 last = t, t = t->next;
4527 }
H. Peter Anvin89cee572009-07-15 09:16:54 -04004528 if (!tok_type_(t, TOK_ID) || !(m = is_mmacro(t, &params)))
H. Peter Anvine2c80182005-01-15 22:15:51 +00004529 return 0;
4530 last->next = NULL;
Keith Kanios891775e2009-07-11 06:08:54 -05004531 mname = t->text;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004532 tline = t;
H. Peter Anvineba20a72002-04-30 20:53:55 +00004533 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004534
4535 /*
4536 * Fix up the parameters: this involves stripping leading and
4537 * trailing whitespace, then stripping braces if they are
4538 * present.
4539 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004540 for (nparam = 0; params[nparam]; nparam++) ;
H. Peter Anvin734b1882002-04-30 21:01:08 +00004541 paramlen = nparam ? nasm_malloc(nparam * sizeof(*paramlen)) : NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004542
H. Peter Anvine2c80182005-01-15 22:15:51 +00004543 for (i = 0; params[i]; i++) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07004544 int brace = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004545 int comma = (!m->plus || i < nparam - 1);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004546
H. Peter Anvine2c80182005-01-15 22:15:51 +00004547 t = params[i];
4548 skip_white_(t);
4549 if (tok_is_(t, "{"))
H. Peter Anvin6867acc2007-10-10 14:58:45 -07004550 t = t->next, brace = true, comma = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004551 params[i] = t;
4552 paramlen[i] = 0;
4553 while (t) {
4554 if (comma && t->type == TOK_OTHER && !strcmp(t->text, ","))
4555 break; /* ... because we have hit a comma */
4556 if (comma && t->type == TOK_WHITESPACE
4557 && tok_is_(t->next, ","))
4558 break; /* ... or a space then a comma */
4559 if (brace && t->type == TOK_OTHER && !strcmp(t->text, "}"))
4560 break; /* ... or a brace */
4561 t = t->next;
4562 paramlen[i]++;
4563 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004564 }
4565
4566 /*
4567 * OK, we have a MMacro structure together with a set of
4568 * parameters. We must now go through the expansion and push
H. Peter Anvin76690a12002-04-30 20:52:49 +00004569 * copies of each Line on to istk->expansion. Substitution of
4570 * parameter tokens and macro-local tokens doesn't get done
4571 * until the single-line macro substitution process; this is
4572 * because delaying them allows us to change the semantics
4573 * later through %rotate.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004574 *
H. Peter Anvin76690a12002-04-30 20:52:49 +00004575 * First, push an end marker on to istk->expansion, mark this
4576 * macro as in progress, and set up its invocation-specific
4577 * variables.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004578 */
4579 ll = nasm_malloc(sizeof(Line));
4580 ll->next = istk->expansion;
4581 ll->finishes = m;
4582 ll->first = NULL;
4583 istk->expansion = ll;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004584
H. Peter Anvin89cee572009-07-15 09:16:54 -04004585 /*
4586 * Save the previous MMacro expansion in the case of
4587 * macro recursion
4588 */
4589 if (m->max_depth && m->in_progress)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004590 push_mmacro(m);
H. Peter Anvin76690a12002-04-30 20:52:49 +00004591
Keith Kanios891775e2009-07-11 06:08:54 -05004592 m->in_progress ++;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004593 m->params = params;
4594 m->iline = tline;
4595 m->nparam = nparam;
4596 m->rotate = 0;
4597 m->paramlen = paramlen;
4598 m->unique = unique++;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004599 m->lineno = 0;
Keith Kanios3c0d91f2009-10-25 13:28:03 -05004600 m->condcnt = 0;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004601
4602 m->next_active = istk->mstk;
4603 istk->mstk = m;
4604
Cyrill Gorcunov367d59e2010-04-09 15:43:03 +04004605 list_for_each(l, m->expansion) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00004606 Token **tail;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004607
H. Peter Anvine2c80182005-01-15 22:15:51 +00004608 ll = nasm_malloc(sizeof(Line));
4609 ll->finishes = NULL;
4610 ll->next = istk->expansion;
4611 istk->expansion = ll;
4612 tail = &ll->first;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004613
Cyrill Gorcunov367d59e2010-04-09 15:43:03 +04004614 list_for_each(t, l->first) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00004615 Token *x = t;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004616 switch (t->type) {
4617 case TOK_PREPROC_Q:
4618 tt = *tail = new_Token(NULL, TOK_ID, mname, 0);
4619 break;
4620 case TOK_PREPROC_QQ:
4621 tt = *tail = new_Token(NULL, TOK_ID, m->name, 0);
4622 break;
4623 case TOK_PREPROC_ID:
4624 if (t->text[1] == '0' && t->text[2] == '0') {
4625 dont_prepend = -1;
4626 x = label;
4627 if (!x)
4628 continue;
4629 }
4630 /* fall through */
4631 default:
4632 tt = *tail = new_Token(NULL, x->type, x->text, 0);
4633 break;
4634 }
4635 tail = &tt->next;
4636 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00004637 *tail = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004638 }
4639
4640 /*
H. Peter Anvineba20a72002-04-30 20:53:55 +00004641 * If we had a label, push it on as the first line of
4642 * the macro expansion.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004643 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004644 if (label) {
4645 if (dont_prepend < 0)
4646 free_tlist(startline);
4647 else {
4648 ll = nasm_malloc(sizeof(Line));
4649 ll->finishes = NULL;
4650 ll->next = istk->expansion;
4651 istk->expansion = ll;
4652 ll->first = startline;
4653 if (!dont_prepend) {
4654 while (label->next)
4655 label = label->next;
4656 label->next = tt = new_Token(NULL, TOK_OTHER, ":", 0);
4657 }
4658 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004659 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004660
H. Peter Anvin734b1882002-04-30 21:01:08 +00004661 list->uplevel(m->nolist ? LIST_MACRO_NOLIST : LIST_MACRO);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004662
H. Peter Anvineba20a72002-04-30 20:53:55 +00004663 return 1;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004664}
4665
Victor van den Elzen3b404c02008-09-18 13:51:36 +02004666/* The function that actually does the error reporting */
4667static void verror(int severity, const char *fmt, va_list arg)
4668{
4669 char buff[1024];
4670
4671 vsnprintf(buff, sizeof(buff), fmt, arg);
4672
4673 if (istk && istk->mstk && istk->mstk->name)
H. Peter Anvindbb640b2009-07-18 18:57:16 -07004674 nasm_error(severity, "(%s:%d) %s", istk->mstk->name,
Victor van den Elzen3b404c02008-09-18 13:51:36 +02004675 istk->mstk->lineno, buff);
4676 else
H. Peter Anvindbb640b2009-07-18 18:57:16 -07004677 nasm_error(severity, "%s", buff);
Victor van den Elzen3b404c02008-09-18 13:51:36 +02004678}
4679
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004680/*
4681 * Since preprocessor always operate only on the line that didn't
H. Peter Anvin917a3492008-09-24 09:14:49 -07004682 * arrived yet, we should always use ERR_OFFBY1.
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004683 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004684static void error(int severity, const char *fmt, ...)
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004685{
4686 va_list arg;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004687
4688 /* If we're in a dead branch of IF or something like it, ignore the error */
H. Peter Anvin77ba0c62002-05-14 03:18:53 +00004689 if (istk && istk->conds && !emitting(istk->conds->state))
H. Peter Anvine2c80182005-01-15 22:15:51 +00004690 return;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004691
H. Peter Anvin734b1882002-04-30 21:01:08 +00004692 va_start(arg, fmt);
Victor van den Elzen3b404c02008-09-18 13:51:36 +02004693 verror(severity, fmt, arg);
H. Peter Anvin734b1882002-04-30 21:01:08 +00004694 va_end(arg);
Victor van den Elzen3b404c02008-09-18 13:51:36 +02004695}
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004696
Victor van den Elzen3b404c02008-09-18 13:51:36 +02004697/*
4698 * Because %else etc are evaluated in the state context
4699 * of the previous branch, errors might get lost with error():
4700 * %if 0 ... %else trailing garbage ... %endif
4701 * So %else etc should report errors with this function.
4702 */
4703static void error_precond(int severity, const char *fmt, ...)
4704{
4705 va_list arg;
4706
4707 /* Only ignore the error if it's really in a dead branch */
4708 if (istk && istk->conds && istk->conds->state == COND_NEVER)
4709 return;
4710
4711 va_start(arg, fmt);
4712 verror(severity, fmt, arg);
4713 va_end(arg);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004714}
4715
H. Peter Anvin734b1882002-04-30 21:01:08 +00004716static void
H. Peter Anvindbb640b2009-07-18 18:57:16 -07004717pp_reset(char *file, int apass, ListGen * listgen, StrList **deplist)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004718{
H. Peter Anvin7383b402008-09-24 10:20:40 -07004719 Token *t;
4720
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004721 cstk = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004722 istk = nasm_malloc(sizeof(Include));
4723 istk->next = NULL;
4724 istk->conds = NULL;
4725 istk->expansion = NULL;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004726 istk->mstk = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004727 istk->fp = fopen(file, "r");
H. Peter Anvineba20a72002-04-30 20:53:55 +00004728 istk->fname = NULL;
4729 src_set_fname(nasm_strdup(file));
4730 src_set_linnum(0);
4731 istk->lineinc = 1;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004732 if (!istk->fp)
H. Peter Anvin917a3492008-09-24 09:14:49 -07004733 error(ERR_FATAL|ERR_NOFILE, "unable to open input file `%s'",
H. Peter Anvine2c80182005-01-15 22:15:51 +00004734 file);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004735 defining = NULL;
Charles Crayned4200be2008-07-12 16:42:33 -07004736 nested_mac_count = 0;
4737 nested_rep_count = 0;
H. Peter Anvin97a23472007-09-16 17:57:25 -07004738 init_macros();
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004739 unique = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004740 if (tasm_compatible_mode) {
H. Peter Anvina4835d42008-05-20 14:21:29 -07004741 stdmacpos = nasm_stdmac;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004742 } else {
H. Peter Anvina4835d42008-05-20 14:21:29 -07004743 stdmacpos = nasm_stdmac_after_tasm;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004744 }
H. Peter Anvind2456592008-06-19 15:04:18 -07004745 any_extrastdmac = extrastdmac && *extrastdmac;
4746 do_predef = true;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004747 list = listgen;
H. Peter Anvin61f130f2008-09-25 15:45:06 -07004748
4749 /*
4750 * 0 for dependencies, 1 for preparatory passes, 2 for final pass.
4751 * The caller, however, will also pass in 3 for preprocess-only so
4752 * we can set __PASS__ accordingly.
4753 */
4754 pass = apass > 2 ? 2 : apass;
4755
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07004756 dephead = deptail = deplist;
4757 if (deplist) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004758 StrList *sl = nasm_malloc(strlen(file)+1+sizeof sl->next);
4759 sl->next = NULL;
4760 strcpy(sl->str, file);
4761 *deptail = sl;
4762 deptail = &sl->next;
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07004763 }
H. Peter Anvin7383b402008-09-24 10:20:40 -07004764
H. Peter Anvin61f130f2008-09-25 15:45:06 -07004765 /*
4766 * Define the __PASS__ macro. This is defined here unlike
4767 * all the other builtins, because it is special -- it varies between
4768 * passes.
4769 */
H. Peter Anvin7383b402008-09-24 10:20:40 -07004770 t = nasm_malloc(sizeof(*t));
4771 t->next = NULL;
H. Peter Anvin61f130f2008-09-25 15:45:06 -07004772 make_tok_num(t, apass);
H. Peter Anvin7383b402008-09-24 10:20:40 -07004773 t->a.mac = NULL;
4774 define_smacro(NULL, "__PASS__", true, 0, t);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004775}
4776
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004777static char *pp_getline(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004778{
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004779 char *line;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004780 Token *tline;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004781
H. Peter Anvine2c80182005-01-15 22:15:51 +00004782 while (1) {
4783 /*
Keith Kaniosb7a89542007-04-12 02:40:54 +00004784 * Fetch a tokenized line, either from the macro-expansion
H. Peter Anvine2c80182005-01-15 22:15:51 +00004785 * buffer or from the input file.
4786 */
4787 tline = NULL;
4788 while (istk->expansion && istk->expansion->finishes) {
4789 Line *l = istk->expansion;
4790 if (!l->finishes->name && l->finishes->in_progress > 1) {
4791 Line *ll;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004792
H. Peter Anvine2c80182005-01-15 22:15:51 +00004793 /*
4794 * This is a macro-end marker for a macro with no
4795 * name, which means it's not really a macro at all
4796 * but a %rep block, and the `in_progress' field is
4797 * more than 1, meaning that we still need to
4798 * repeat. (1 means the natural last repetition; 0
4799 * means termination by %exitrep.) We have
4800 * therefore expanded up to the %endrep, and must
4801 * push the whole block on to the expansion buffer
4802 * again. We don't bother to remove the macro-end
4803 * marker: we'd only have to generate another one
4804 * if we did.
4805 */
4806 l->finishes->in_progress--;
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04004807 list_for_each(l, l->finishes->expansion) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00004808 Token *t, *tt, **tail;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004809
H. Peter Anvine2c80182005-01-15 22:15:51 +00004810 ll = nasm_malloc(sizeof(Line));
4811 ll->next = istk->expansion;
4812 ll->finishes = NULL;
4813 ll->first = NULL;
4814 tail = &ll->first;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004815
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04004816 list_for_each(t, l->first) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00004817 if (t->text || t->type == TOK_WHITESPACE) {
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04004818 tt = *tail = new_Token(NULL, t->type, t->text, 0);
H. Peter Anvine2c80182005-01-15 22:15:51 +00004819 tail = &tt->next;
4820 }
4821 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00004822
H. Peter Anvine2c80182005-01-15 22:15:51 +00004823 istk->expansion = ll;
4824 }
4825 } else {
4826 /*
4827 * Check whether a `%rep' was started and not ended
4828 * within this macro expansion. This can happen and
4829 * should be detected. It's a fatal error because
4830 * I'm too confused to work out how to recover
4831 * sensibly from it.
4832 */
4833 if (defining) {
4834 if (defining->name)
4835 error(ERR_PANIC,
4836 "defining with name in expansion");
4837 else if (istk->mstk->name)
4838 error(ERR_FATAL,
4839 "`%%rep' without `%%endrep' within"
4840 " expansion of macro `%s'",
4841 istk->mstk->name);
4842 }
H. Peter Anvin87bc6192002-04-30 20:53:16 +00004843
H. Peter Anvine2c80182005-01-15 22:15:51 +00004844 /*
4845 * FIXME: investigate the relationship at this point between
4846 * istk->mstk and l->finishes
4847 */
4848 {
4849 MMacro *m = istk->mstk;
4850 istk->mstk = m->next_active;
4851 if (m->name) {
4852 /*
4853 * This was a real macro call, not a %rep, and
4854 * therefore the parameter information needs to
4855 * be freed.
4856 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004857 if (m->prev) {
4858 pop_mmacro(m);
4859 l->finishes->in_progress --;
4860 } else {
Keith Kanios891775e2009-07-11 06:08:54 -05004861 nasm_free(m->params);
4862 free_tlist(m->iline);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004863 nasm_free(m->paramlen);
4864 l->finishes->in_progress = 0;
4865 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00004866 } else
4867 free_mmacro(m);
4868 }
4869 istk->expansion = l->next;
4870 nasm_free(l);
4871 list->downlevel(LIST_MACRO);
4872 }
4873 }
4874 while (1) { /* until we get a line we can use */
H. Peter Anvineba20a72002-04-30 20:53:55 +00004875
H. Peter Anvine2c80182005-01-15 22:15:51 +00004876 if (istk->expansion) { /* from a macro expansion */
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004877 char *p;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004878 Line *l = istk->expansion;
4879 if (istk->mstk)
4880 istk->mstk->lineno++;
4881 tline = l->first;
4882 istk->expansion = l->next;
4883 nasm_free(l);
H. Peter Anvin6867acc2007-10-10 14:58:45 -07004884 p = detoken(tline, false);
H. Peter Anvine2c80182005-01-15 22:15:51 +00004885 list->line(LIST_MACRO, p);
4886 nasm_free(p);
4887 break;
4888 }
4889 line = read_line();
4890 if (line) { /* from the current input file */
4891 line = prepreproc(line);
Keith Kaniosb7a89542007-04-12 02:40:54 +00004892 tline = tokenize(line);
H. Peter Anvine2c80182005-01-15 22:15:51 +00004893 nasm_free(line);
4894 break;
4895 }
4896 /*
4897 * The current file has ended; work down the istk
4898 */
4899 {
4900 Include *i = istk;
4901 fclose(i->fp);
4902 if (i->conds)
4903 error(ERR_FATAL,
4904 "expected `%%endif' before end of file");
4905 /* only set line and file name if there's a next node */
4906 if (i->next) {
4907 src_set_linnum(i->lineno);
4908 nasm_free(src_set_fname(i->fname));
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004909 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00004910 istk = i->next;
4911 list->downlevel(LIST_INCLUDE);
4912 nasm_free(i);
4913 if (!istk)
4914 return NULL;
Victor van den Elzen4c9d6222008-10-01 13:08:50 +02004915 if (istk->expansion && istk->expansion->finishes)
4916 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004917 }
4918 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004919
H. Peter Anvine2c80182005-01-15 22:15:51 +00004920 /*
4921 * We must expand MMacro parameters and MMacro-local labels
4922 * _before_ we plunge into directive processing, to cope
4923 * with things like `%define something %1' such as STRUC
4924 * uses. Unless we're _defining_ a MMacro, in which case
4925 * those tokens should be left alone to go into the
4926 * definition; and unless we're in a non-emitting
4927 * condition, in which case we don't want to meddle with
4928 * anything.
4929 */
Charles Crayned4200be2008-07-12 16:42:33 -07004930 if (!defining && !(istk->conds && !emitting(istk->conds->state))
H. Peter Anvin992fe752008-10-19 15:45:05 -07004931 && !(istk->mstk && !istk->mstk->in_progress)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00004932 tline = expand_mmac_params(tline);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004933 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00004934
H. Peter Anvine2c80182005-01-15 22:15:51 +00004935 /*
4936 * Check the line to see if it's a preprocessor directive.
4937 */
4938 if (do_directive(tline) == DIRECTIVE_FOUND) {
4939 continue;
4940 } else if (defining) {
4941 /*
4942 * We're defining a multi-line macro. We emit nothing
4943 * at all, and just
Keith Kaniosb7a89542007-04-12 02:40:54 +00004944 * shove the tokenized line on to the macro definition.
H. Peter Anvine2c80182005-01-15 22:15:51 +00004945 */
4946 Line *l = nasm_malloc(sizeof(Line));
4947 l->next = defining->expansion;
4948 l->first = tline;
H. Peter Anvin538002d2008-06-28 18:30:27 -07004949 l->finishes = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004950 defining->expansion = l;
4951 continue;
4952 } else if (istk->conds && !emitting(istk->conds->state)) {
4953 /*
4954 * We're in a non-emitting branch of a condition block.
4955 * Emit nothing at all, not even a blank line: when we
4956 * emerge from the condition we'll give a line-number
4957 * directive so we keep our place correctly.
4958 */
4959 free_tlist(tline);
4960 continue;
4961 } else if (istk->mstk && !istk->mstk->in_progress) {
4962 /*
4963 * We're in a %rep block which has been terminated, so
4964 * we're walking through to the %endrep without
4965 * emitting anything. Emit nothing at all, not even a
4966 * blank line: when we emerge from the %rep block we'll
4967 * give a line-number directive so we keep our place
4968 * correctly.
4969 */
4970 free_tlist(tline);
4971 continue;
4972 } else {
4973 tline = expand_smacro(tline);
4974 if (!expand_mmacro(tline)) {
4975 /*
Keith Kaniosb7a89542007-04-12 02:40:54 +00004976 * De-tokenize the line again, and emit it.
H. Peter Anvine2c80182005-01-15 22:15:51 +00004977 */
H. Peter Anvin6867acc2007-10-10 14:58:45 -07004978 line = detoken(tline, true);
H. Peter Anvine2c80182005-01-15 22:15:51 +00004979 free_tlist(tline);
4980 break;
4981 } else {
4982 continue; /* expand_mmacro calls free_tlist */
4983 }
4984 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004985 }
4986
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004987 return line;
4988}
4989
H. Peter Anvine2c80182005-01-15 22:15:51 +00004990static void pp_cleanup(int pass)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004991{
H. Peter Anvine2c80182005-01-15 22:15:51 +00004992 if (defining) {
H. Peter Anvin89cee572009-07-15 09:16:54 -04004993 if (defining->name) {
Victor van den Elzen8f1120f2008-07-16 13:41:37 +02004994 error(ERR_NONFATAL,
4995 "end of file while still defining macro `%s'",
4996 defining->name);
4997 } else {
4998 error(ERR_NONFATAL, "end of file while still in %%rep");
4999 }
5000
H. Peter Anvine2c80182005-01-15 22:15:51 +00005001 free_mmacro(defining);
Cyrill Gorcunova327c652010-02-14 17:19:38 +03005002 defining = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005003 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005004 while (cstk)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005005 ctx_pop();
H. Peter Anvin97a23472007-09-16 17:57:25 -07005006 free_macros();
H. Peter Anvine2c80182005-01-15 22:15:51 +00005007 while (istk) {
5008 Include *i = istk;
5009 istk = istk->next;
5010 fclose(i->fp);
5011 nasm_free(i->fname);
5012 nasm_free(i);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005013 }
5014 while (cstk)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005015 ctx_pop();
H. Peter Anvin86877b22008-06-20 15:55:45 -07005016 nasm_free(src_set_fname(NULL));
H. Peter Anvine2c80182005-01-15 22:15:51 +00005017 if (pass == 0) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03005018 IncPath *i;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005019 free_llist(predef);
5020 delete_Blocks();
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03005021 while ((i = ipath)) {
5022 ipath = i->next;
5023 if (i->path)
5024 nasm_free(i->path);
5025 nasm_free(i);
5026 }
H. Peter Anvin11dfa1a2008-07-02 18:11:04 -07005027 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005028}
5029
Keith Kaniosa6dfa782007-04-13 16:47:53 +00005030void pp_include_path(char *path)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005031{
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005032 IncPath *i;
H. Peter Anvin37a321f2007-09-24 13:41:58 -07005033
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005034 i = nasm_malloc(sizeof(IncPath));
H. Peter Anvin37a321f2007-09-24 13:41:58 -07005035 i->path = path ? nasm_strdup(path) : NULL;
Frank Kotlerd0ed6fd2003-08-27 11:33:56 +00005036 i->next = NULL;
5037
H. Peter Anvin89cee572009-07-15 09:16:54 -04005038 if (ipath) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00005039 IncPath *j = ipath;
H. Peter Anvin89cee572009-07-15 09:16:54 -04005040 while (j->next)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005041 j = j->next;
5042 j->next = i;
5043 } else {
5044 ipath = i;
Frank Kotlerd0ed6fd2003-08-27 11:33:56 +00005045 }
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005046}
Frank Kotlerd0ed6fd2003-08-27 11:33:56 +00005047
Keith Kaniosa6dfa782007-04-13 16:47:53 +00005048void pp_pre_include(char *fname)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005049{
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005050 Token *inc, *space, *name;
5051 Line *l;
5052
H. Peter Anvin734b1882002-04-30 21:01:08 +00005053 name = new_Token(NULL, TOK_INTERNAL_STRING, fname, 0);
5054 space = new_Token(name, TOK_WHITESPACE, NULL, 0);
5055 inc = new_Token(space, TOK_PREPROC_ID, "%include", 0);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005056
5057 l = nasm_malloc(sizeof(Line));
5058 l->next = predef;
5059 l->first = inc;
H. Peter Anvin538002d2008-06-28 18:30:27 -07005060 l->finishes = NULL;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005061 predef = l;
5062}
5063
Keith Kaniosa6dfa782007-04-13 16:47:53 +00005064void pp_pre_define(char *definition)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005065{
5066 Token *def, *space;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005067 Line *l;
Keith Kaniosa6dfa782007-04-13 16:47:53 +00005068 char *equals;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005069
5070 equals = strchr(definition, '=');
H. Peter Anvin734b1882002-04-30 21:01:08 +00005071 space = new_Token(NULL, TOK_WHITESPACE, NULL, 0);
5072 def = new_Token(space, TOK_PREPROC_ID, "%define", 0);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005073 if (equals)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005074 *equals = ' ';
Keith Kaniosb7a89542007-04-12 02:40:54 +00005075 space->next = tokenize(definition);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005076 if (equals)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005077 *equals = '=';
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005078
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005079 l = nasm_malloc(sizeof(Line));
5080 l->next = predef;
5081 l->first = def;
H. Peter Anvin538002d2008-06-28 18:30:27 -07005082 l->finishes = NULL;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005083 predef = l;
5084}
5085
Keith Kaniosa6dfa782007-04-13 16:47:53 +00005086void pp_pre_undefine(char *definition)
H. Peter Anvin620515a2002-04-30 20:57:38 +00005087{
5088 Token *def, *space;
5089 Line *l;
5090
H. Peter Anvin734b1882002-04-30 21:01:08 +00005091 space = new_Token(NULL, TOK_WHITESPACE, NULL, 0);
5092 def = new_Token(space, TOK_PREPROC_ID, "%undef", 0);
Keith Kaniosb7a89542007-04-12 02:40:54 +00005093 space->next = tokenize(definition);
H. Peter Anvin620515a2002-04-30 20:57:38 +00005094
5095 l = nasm_malloc(sizeof(Line));
5096 l->next = predef;
5097 l->first = def;
H. Peter Anvin538002d2008-06-28 18:30:27 -07005098 l->finishes = NULL;
H. Peter Anvin620515a2002-04-30 20:57:38 +00005099 predef = l;
5100}
5101
Keith Kaniosb7a89542007-04-12 02:40:54 +00005102/*
5103 * Added by Keith Kanios:
5104 *
5105 * This function is used to assist with "runtime" preprocessor
5106 * directives. (e.g. pp_runtime("%define __BITS__ 64");)
5107 *
5108 * ERRORS ARE IGNORED HERE, SO MAKE COMPLETELY SURE THAT YOU
5109 * PASS A VALID STRING TO THIS FUNCTION!!!!!
5110 */
5111
Keith Kaniosa6dfa782007-04-13 16:47:53 +00005112void pp_runtime(char *definition)
Keith Kaniosb7a89542007-04-12 02:40:54 +00005113{
5114 Token *def;
H. Peter Anvin70653092007-10-19 14:42:29 -07005115
Keith Kaniosb7a89542007-04-12 02:40:54 +00005116 def = tokenize(definition);
H. Peter Anvin89cee572009-07-15 09:16:54 -04005117 if (do_directive(def) == NO_DIRECTIVE_FOUND)
Keith Kaniosb7a89542007-04-12 02:40:54 +00005118 free_tlist(def);
H. Peter Anvin70653092007-10-19 14:42:29 -07005119
Keith Kaniosb7a89542007-04-12 02:40:54 +00005120}
5121
H. Peter Anvina70547f2008-07-19 21:44:26 -07005122void pp_extra_stdmac(macros_t *macros)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005123{
H. Peter Anvin76690a12002-04-30 20:52:49 +00005124 extrastdmac = macros;
5125}
5126
Keith Kaniosa5fc6462007-10-13 07:09:22 -07005127static void make_tok_num(Token * tok, int64_t val)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005128{
Keith Kaniosa6dfa782007-04-13 16:47:53 +00005129 char numbuf[20];
Keith Kaniosa5fc6462007-10-13 07:09:22 -07005130 snprintf(numbuf, sizeof(numbuf), "%"PRId64"", val);
H. Peter Anvineba20a72002-04-30 20:53:55 +00005131 tok->text = nasm_strdup(numbuf);
5132 tok->type = TOK_NUMBER;
5133}
5134
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005135Preproc nasmpp = {
5136 pp_reset,
5137 pp_getline,
5138 pp_cleanup
5139};