blob: 4c81963cc86ed30af3997c103b6e9db089fcf5ae [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 }
Cyrill Gorcunov84b4cba2010-09-12 21:39:40 +0400935 } else if (*p == '!') {
936 type = TOK_PREPROC_ID;
937 p++;
938 if (isidchar(*p)) {
939 do {
940 p++;
941 } while (isidchar(*p));
942 } else if (*p == '\'' || *p == '\"' || *p == '`') {
943 p = nasm_skip_string(p);
944 if (*p)
945 p++;
946 else
947 error(ERR_NONFATAL|ERR_PASS1, "unterminated %! string");
948 } else {
949 /* %! without string or identifier */
950 type = TOK_OTHER; /* Legacy behavior... */
951 }
H. Peter Anvine2c80182005-01-15 22:15:51 +0000952 } else if (isidchar(*p) ||
953 ((*p == '!' || *p == '%' || *p == '$') &&
954 isidchar(p[1]))) {
955 do {
956 p++;
957 }
958 while (isidchar(*p));
959 type = TOK_PREPROC_ID;
960 } else {
961 type = TOK_OTHER;
962 if (*p == '%')
963 p++;
964 }
965 } else if (isidstart(*p) || (*p == '$' && isidstart(p[1]))) {
966 type = TOK_ID;
967 p++;
968 while (*p && isidchar(*p))
969 p++;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -0700970 } else if (*p == '\'' || *p == '"' || *p == '`') {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000971 /*
972 * A string token.
973 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000974 type = TOK_STRING;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300975 p = nasm_skip_string(p);
Nickolay Yurchenkof3b3ce22003-09-21 20:38:43 +0000976
H. Peter Anvine2c80182005-01-15 22:15:51 +0000977 if (*p) {
978 p++;
979 } else {
H. Peter Anvin917a3492008-09-24 09:14:49 -0700980 error(ERR_WARNING|ERR_PASS1, "unterminated string");
H. Peter Anvine2c80182005-01-15 22:15:51 +0000981 /* Handling unterminated strings by UNV */
982 /* type = -1; */
983 }
Victor van den Elzenfb5f2512009-04-17 16:17:59 +0200984 } else if (p[0] == '$' && p[1] == '$') {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300985 type = TOK_OTHER; /* TOKEN_BASE */
Victor van den Elzenfb5f2512009-04-17 16:17:59 +0200986 p += 2;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000987 } else if (isnumstart(*p)) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300988 bool is_hex = false;
989 bool is_float = false;
990 bool has_e = false;
991 char c, *r;
H. Peter Anvinc2df2822007-10-24 15:29:28 -0700992
H. Peter Anvine2c80182005-01-15 22:15:51 +0000993 /*
H. Peter Anvinc2df2822007-10-24 15:29:28 -0700994 * A numeric token.
H. Peter Anvine2c80182005-01-15 22:15:51 +0000995 */
H. Peter Anvinc2df2822007-10-24 15:29:28 -0700996
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300997 if (*p == '$') {
998 p++;
999 is_hex = true;
1000 }
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001001
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001002 for (;;) {
1003 c = *p++;
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001004
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001005 if (!is_hex && (c == 'e' || c == 'E')) {
1006 has_e = true;
1007 if (*p == '+' || *p == '-') {
1008 /*
1009 * e can only be followed by +/- if it is either a
1010 * prefixed hex number or a floating-point number
1011 */
1012 p++;
1013 is_float = true;
1014 }
1015 } else if (c == 'H' || c == 'h' || c == 'X' || c == 'x') {
1016 is_hex = true;
1017 } else if (c == 'P' || c == 'p') {
1018 is_float = true;
1019 if (*p == '+' || *p == '-')
1020 p++;
1021 } else if (isnumchar(c) || c == '_')
1022 ; /* just advance */
1023 else if (c == '.') {
1024 /*
1025 * we need to deal with consequences of the legacy
1026 * parser, like "1.nolist" being two tokens
1027 * (TOK_NUMBER, TOK_ID) here; at least give it
1028 * a shot for now. In the future, we probably need
1029 * a flex-based scanner with proper pattern matching
1030 * to do it as well as it can be done. Nothing in
1031 * the world is going to help the person who wants
1032 * 0x123.p16 interpreted as two tokens, though.
1033 */
1034 r = p;
1035 while (*r == '_')
1036 r++;
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001037
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001038 if (nasm_isdigit(*r) || (is_hex && nasm_isxdigit(*r)) ||
1039 (!is_hex && (*r == 'e' || *r == 'E')) ||
1040 (*r == 'p' || *r == 'P')) {
1041 p = r;
1042 is_float = true;
1043 } else
1044 break; /* Terminate the token */
1045 } else
1046 break;
1047 }
1048 p--; /* Point to first character beyond number */
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001049
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001050 if (p == line+1 && *line == '$') {
1051 type = TOK_OTHER; /* TOKEN_HERE */
1052 } else {
1053 if (has_e && !is_hex) {
1054 /* 1e13 is floating-point, but 1e13h is not */
1055 is_float = true;
1056 }
H. Peter Anvind784a082009-04-20 14:01:18 -07001057
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001058 type = is_float ? TOK_FLOAT : TOK_NUMBER;
1059 }
H. Peter Anvinbda7a6e2008-06-21 10:23:17 -07001060 } else if (nasm_isspace(*p)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001061 type = TOK_WHITESPACE;
Cyrill Gorcunovf66ac7d2009-10-12 20:41:13 +04001062 p = nasm_skip_spaces(p);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001063 /*
1064 * Whitespace just before end-of-line is discarded by
1065 * pretending it's a comment; whitespace just before a
1066 * comment gets lumped into the comment.
1067 */
1068 if (!*p || *p == ';') {
1069 type = TOK_COMMENT;
1070 while (*p)
1071 p++;
1072 }
1073 } else if (*p == ';') {
1074 type = TOK_COMMENT;
1075 while (*p)
1076 p++;
1077 } else {
1078 /*
1079 * Anything else is an operator of some kind. We check
1080 * for all the double-character operators (>>, <<, //,
1081 * %%, <=, >=, ==, !=, <>, &&, ||, ^^), but anything
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001082 * else is a single-character operator.
H. Peter Anvine2c80182005-01-15 22:15:51 +00001083 */
1084 type = TOK_OTHER;
1085 if ((p[0] == '>' && p[1] == '>') ||
1086 (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++;
1097 }
1098 p++;
1099 }
Nickolay Yurchenkof3b3ce22003-09-21 20:38:43 +00001100
H. Peter Anvine2c80182005-01-15 22:15:51 +00001101 /* Handling unterminated string by UNV */
1102 /*if (type == -1)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001103 {
1104 *tail = t = new_Token(NULL, TOK_STRING, line, p-line+1);
1105 t->text[p-line] = *line;
1106 tail = &t->next;
1107 }
1108 else */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001109 if (type != TOK_COMMENT) {
1110 *tail = t = new_Token(NULL, type, line, p - line);
1111 tail = &t->next;
1112 }
1113 line = p;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001114 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001115 return list;
1116}
1117
H. Peter Anvince616072002-04-30 21:02:23 +00001118/*
1119 * this function allocates a new managed block of memory and
H. Peter Anvin70653092007-10-19 14:42:29 -07001120 * returns a pointer to the block. The managed blocks are
H. Peter Anvince616072002-04-30 21:02:23 +00001121 * deleted only all at once by the delete_Blocks function.
1122 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001123static void *new_Block(size_t size)
H. Peter Anvince616072002-04-30 21:02:23 +00001124{
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001125 Blocks *b = &blocks;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001126
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001127 /* first, get to the end of the linked list */
1128 while (b->next)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001129 b = b->next;
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001130 /* now allocate the requested chunk */
1131 b->chunk = nasm_malloc(size);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001132
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001133 /* now allocate a new block for the next request */
1134 b->next = nasm_malloc(sizeof(Blocks));
1135 /* and initialize the contents of the new block */
1136 b->next->next = NULL;
1137 b->next->chunk = NULL;
1138 return b->chunk;
H. Peter Anvince616072002-04-30 21:02:23 +00001139}
1140
1141/*
1142 * this function deletes all managed blocks of memory
1143 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001144static void delete_Blocks(void)
H. Peter Anvince616072002-04-30 21:02:23 +00001145{
H. Peter Anvine2c80182005-01-15 22:15:51 +00001146 Blocks *a, *b = &blocks;
H. Peter Anvince616072002-04-30 21:02:23 +00001147
H. Peter Anvin70653092007-10-19 14:42:29 -07001148 /*
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001149 * keep in mind that the first block, pointed to by blocks
H. Peter Anvin70653092007-10-19 14:42:29 -07001150 * is a static and not dynamically allocated, so we don't
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001151 * free it.
1152 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001153 while (b) {
1154 if (b->chunk)
1155 nasm_free(b->chunk);
1156 a = b;
1157 b = b->next;
1158 if (a != &blocks)
1159 nasm_free(a);
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001160 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001161}
H. Peter Anvin734b1882002-04-30 21:01:08 +00001162
1163/*
H. Peter Anvin70653092007-10-19 14:42:29 -07001164 * this function creates a new Token and passes a pointer to it
H. Peter Anvin734b1882002-04-30 21:01:08 +00001165 * back to the caller. It sets the type and text elements, and
H. Peter Anvinf26e0972008-07-01 21:26:27 -07001166 * also the a.mac and next elements to NULL.
H. Peter Anvin734b1882002-04-30 21:01:08 +00001167 */
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07001168static Token *new_Token(Token * next, enum pp_token_type type,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001169 const char *text, int txtlen)
H. Peter Anvin734b1882002-04-30 21:01:08 +00001170{
1171 Token *t;
1172 int i;
1173
H. Peter Anvin89cee572009-07-15 09:16:54 -04001174 if (!freeTokens) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001175 freeTokens = (Token *) new_Block(TOKEN_BLOCKSIZE * sizeof(Token));
1176 for (i = 0; i < TOKEN_BLOCKSIZE - 1; i++)
1177 freeTokens[i].next = &freeTokens[i + 1];
1178 freeTokens[i].next = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001179 }
1180 t = freeTokens;
1181 freeTokens = t->next;
1182 t->next = next;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07001183 t->a.mac = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001184 t->type = type;
H. Peter Anvin89cee572009-07-15 09:16:54 -04001185 if (type == TOK_WHITESPACE || !text) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001186 t->text = NULL;
1187 } else {
1188 if (txtlen == 0)
1189 txtlen = strlen(text);
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07001190 t->text = nasm_malloc(txtlen+1);
1191 memcpy(t->text, text, txtlen);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001192 t->text[txtlen] = '\0';
H. Peter Anvin734b1882002-04-30 21:01:08 +00001193 }
1194 return t;
1195}
1196
H. Peter Anvine2c80182005-01-15 22:15:51 +00001197static Token *delete_Token(Token * t)
H. Peter Anvin734b1882002-04-30 21:01:08 +00001198{
1199 Token *next = t->next;
1200 nasm_free(t->text);
H. Peter Anvin788e6c12002-04-30 21:02:01 +00001201 t->next = freeTokens;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001202 freeTokens = t;
1203 return next;
1204}
1205
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001206/*
1207 * Convert a line of tokens back into text.
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001208 * If expand_locals is not zero, identifiers of the form "%$*xxx"
1209 * will be transformed into ..@ctxnum.xxx
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001210 */
H. Peter Anvin9e200162008-06-04 17:23:14 -07001211static char *detoken(Token * tlist, bool expand_locals)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001212{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001213 Token *t;
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001214 char *line, *p;
H. Peter Anvinb4daadc2008-01-21 16:31:57 -08001215 const char *q;
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001216 int len = 0;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001217
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001218 list_for_each(t, tlist) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001219 if (t->type == TOK_PREPROC_ID && t->text[1] == '!') {
Cyrill Gorcunov84b4cba2010-09-12 21:39:40 +04001220 char *v;
1221 char *q = t->text;
H. Peter Anvin077fb932010-07-20 14:56:30 -07001222
Cyrill Gorcunov84b4cba2010-09-12 21:39:40 +04001223 v = t->text + 2;
1224 if (*v == '\'' || *v == '\"' || *v == '`') {
1225 size_t len = nasm_unquote(v, NULL);
1226 size_t clen = strlen(v);
H. Peter Anvin077fb932010-07-20 14:56:30 -07001227
Cyrill Gorcunov84b4cba2010-09-12 21:39:40 +04001228 if (len != clen) {
1229 error(ERR_NONFATAL | ERR_PASS1,
1230 "NUL character in %! string");
1231 v = NULL;
1232 }
1233 }
H. Peter Anvin077fb932010-07-20 14:56:30 -07001234
Cyrill Gorcunov84b4cba2010-09-12 21:39:40 +04001235 if (v) {
1236 char *p = getenv(v);
1237 if (!p) {
1238 error(ERR_NONFATAL | ERR_PASS1,
1239 "nonexistent environment variable `%s'", v);
1240 p = "";
1241 }
1242 t->text = nasm_strdup(p);
1243 }
1244 nasm_free(q);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001245 }
H. Peter Anvin077fb932010-07-20 14:56:30 -07001246
H. Peter Anvine2c80182005-01-15 22:15:51 +00001247 /* Expand local macros here and not during preprocessing */
1248 if (expand_locals &&
1249 t->type == TOK_PREPROC_ID && t->text &&
1250 t->text[0] == '%' && t->text[1] == '$') {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001251 const char *q;
1252 char *p;
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001253 Context *ctx = get_ctx(t->text, &q, false);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001254 if (ctx) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001255 char buffer[40];
Keith Kanios93f2e9a2007-04-14 00:10:59 +00001256 snprintf(buffer, sizeof(buffer), "..@%"PRIu32".", ctx->number);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001257 p = nasm_strcat(buffer, q);
1258 nasm_free(t->text);
1259 t->text = p;
1260 }
1261 }
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001262 if (t->type == TOK_WHITESPACE)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001263 len++;
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001264 else if (t->text)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001265 len += strlen(t->text);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001266 }
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001267
H. Peter Anvin734b1882002-04-30 21:01:08 +00001268 p = line = nasm_malloc(len + 1);
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001269
1270 list_for_each(t, tlist) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001271 if (t->type == TOK_WHITESPACE) {
H. Peter Anvinb4daadc2008-01-21 16:31:57 -08001272 *p++ = ' ';
H. Peter Anvine2c80182005-01-15 22:15:51 +00001273 } else if (t->text) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001274 q = t->text;
1275 while (*q)
1276 *p++ = *q++;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001277 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001278 }
1279 *p = '\0';
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001280
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001281 return line;
1282}
1283
1284/*
H. Peter Anvin76690a12002-04-30 20:52:49 +00001285 * A scanner, suitable for use by the expression evaluator, which
1286 * operates on a line of Tokens. Expects a pointer to a pointer to
1287 * the first token in the line to be passed in as its private_data
1288 * field.
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001289 *
1290 * FIX: This really needs to be unified with stdscan.
H. Peter Anvin76690a12002-04-30 20:52:49 +00001291 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001292static int ppscan(void *private_data, struct tokenval *tokval)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001293{
H. Peter Anvin76690a12002-04-30 20:52:49 +00001294 Token **tlineptr = private_data;
1295 Token *tline;
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001296 char ourcopy[MAX_KEYWORD+1], *p, *r, *s;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001297
H. Peter Anvine2c80182005-01-15 22:15:51 +00001298 do {
1299 tline = *tlineptr;
1300 *tlineptr = tline ? tline->next : NULL;
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04001301 } while (tline && (tline->type == TOK_WHITESPACE ||
1302 tline->type == TOK_COMMENT));
H. Peter Anvin76690a12002-04-30 20:52:49 +00001303
1304 if (!tline)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001305 return tokval->t_type = TOKEN_EOS;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001306
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001307 tokval->t_charptr = tline->text;
1308
H. Peter Anvin76690a12002-04-30 20:52:49 +00001309 if (tline->text[0] == '$' && !tline->text[1])
H. Peter Anvine2c80182005-01-15 22:15:51 +00001310 return tokval->t_type = TOKEN_HERE;
H. Peter Anvin7cf897e2002-05-30 21:30:33 +00001311 if (tline->text[0] == '$' && tline->text[1] == '$' && !tline->text[2])
H. Peter Anvine2c80182005-01-15 22:15:51 +00001312 return tokval->t_type = TOKEN_BASE;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001313
H. Peter Anvine2c80182005-01-15 22:15:51 +00001314 if (tline->type == TOK_ID) {
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001315 p = tokval->t_charptr = tline->text;
1316 if (p[0] == '$') {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001317 tokval->t_charptr++;
1318 return tokval->t_type = TOKEN_ID;
1319 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00001320
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001321 for (r = p, s = ourcopy; *r; r++) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001322 if (r >= p+MAX_KEYWORD)
1323 return tokval->t_type = TOKEN_ID; /* Not a keyword */
H. Peter Anvinac8f8fc2008-06-11 15:49:41 -07001324 *s++ = nasm_tolower(*r);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001325 }
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001326 *s = '\0';
1327 /* right, so we have an identifier sitting in temp storage. now,
1328 * is it actually a register or instruction name, or what? */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001329 return nasm_token_hash(ourcopy, tokval);
H. Peter Anvin76690a12002-04-30 20:52:49 +00001330 }
1331
H. Peter Anvine2c80182005-01-15 22:15:51 +00001332 if (tline->type == TOK_NUMBER) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001333 bool rn_error;
1334 tokval->t_integer = readnum(tline->text, &rn_error);
1335 tokval->t_charptr = tline->text;
1336 if (rn_error)
1337 return tokval->t_type = TOKEN_ERRNUM;
1338 else
1339 return tokval->t_type = TOKEN_NUM;
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001340 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00001341
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001342 if (tline->type == TOK_FLOAT) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001343 return tokval->t_type = TOKEN_FLOAT;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001344 }
1345
H. Peter Anvine2c80182005-01-15 22:15:51 +00001346 if (tline->type == TOK_STRING) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001347 char bq, *ep;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001348
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001349 bq = tline->text[0];
H. Peter Anvin11627042008-06-09 20:45:19 -07001350 tokval->t_charptr = tline->text;
1351 tokval->t_inttwo = nasm_unquote(tline->text, &ep);
H. Peter Anvind2456592008-06-19 15:04:18 -07001352
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001353 if (ep[0] != bq || ep[1] != '\0')
1354 return tokval->t_type = TOKEN_ERRSTR;
1355 else
1356 return tokval->t_type = TOKEN_STR;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001357 }
1358
H. Peter Anvine2c80182005-01-15 22:15:51 +00001359 if (tline->type == TOK_OTHER) {
1360 if (!strcmp(tline->text, "<<"))
1361 return tokval->t_type = TOKEN_SHL;
1362 if (!strcmp(tline->text, ">>"))
1363 return tokval->t_type = TOKEN_SHR;
1364 if (!strcmp(tline->text, "//"))
1365 return tokval->t_type = TOKEN_SDIV;
1366 if (!strcmp(tline->text, "%%"))
1367 return tokval->t_type = TOKEN_SMOD;
1368 if (!strcmp(tline->text, "=="))
1369 return tokval->t_type = TOKEN_EQ;
1370 if (!strcmp(tline->text, "<>"))
1371 return tokval->t_type = TOKEN_NE;
1372 if (!strcmp(tline->text, "!="))
1373 return tokval->t_type = TOKEN_NE;
1374 if (!strcmp(tline->text, "<="))
1375 return tokval->t_type = TOKEN_LE;
1376 if (!strcmp(tline->text, ">="))
1377 return tokval->t_type = TOKEN_GE;
1378 if (!strcmp(tline->text, "&&"))
1379 return tokval->t_type = TOKEN_DBL_AND;
1380 if (!strcmp(tline->text, "^^"))
1381 return tokval->t_type = TOKEN_DBL_XOR;
1382 if (!strcmp(tline->text, "||"))
1383 return tokval->t_type = TOKEN_DBL_OR;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001384 }
1385
1386 /*
1387 * We have no other options: just return the first character of
1388 * the token text.
1389 */
1390 return tokval->t_type = tline->text[0];
1391}
1392
1393/*
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001394 * Compare a string to the name of an existing macro; this is a
1395 * simple wrapper which calls either strcmp or nasm_stricmp
1396 * depending on the value of the `casesense' parameter.
1397 */
H. Peter Anvin4db5a162007-10-11 13:42:09 -07001398static int mstrcmp(const char *p, const char *q, bool casesense)
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001399{
H. Peter Anvin734b1882002-04-30 21:01:08 +00001400 return casesense ? strcmp(p, q) : nasm_stricmp(p, q);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001401}
1402
1403/*
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07001404 * Compare a string to the name of an existing macro; this is a
1405 * simple wrapper which calls either strcmp or nasm_stricmp
1406 * depending on the value of the `casesense' parameter.
1407 */
1408static int mmemcmp(const char *p, const char *q, size_t l, bool casesense)
1409{
1410 return casesense ? memcmp(p, q, l) : nasm_memicmp(p, q, l);
1411}
1412
1413/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001414 * Return the Context structure associated with a %$ token. Return
1415 * NULL, having _already_ reported an error condition, if the
1416 * context stack isn't deep enough for the supplied number of $
1417 * signs.
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001418 * If all_contexts == true, contexts that enclose current are
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001419 * also scanned for such smacro, until it is found; if not -
1420 * only the context that directly results from the number of $'s
1421 * in variable's name.
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001422 *
1423 * If "namep" is non-NULL, set it to the pointer to the macro name
1424 * tail, i.e. the part beyond %$...
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001425 */
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001426static Context *get_ctx(const char *name, const char **namep,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001427 bool all_contexts)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001428{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001429 Context *ctx;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001430 SMacro *m;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001431 int i;
1432
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001433 if (namep)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001434 *namep = name;
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001435
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001436 if (!name || name[0] != '%' || name[1] != '$')
H. Peter Anvine2c80182005-01-15 22:15:51 +00001437 return NULL;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001438
H. Peter Anvine2c80182005-01-15 22:15:51 +00001439 if (!cstk) {
1440 error(ERR_NONFATAL, "`%s': context stack is empty", name);
1441 return NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001442 }
1443
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001444 name += 2;
1445 ctx = cstk;
1446 i = 0;
1447 while (ctx && *name == '$') {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001448 name++;
1449 i++;
1450 ctx = ctx->next;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001451 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001452 if (!ctx) {
1453 error(ERR_NONFATAL, "`%s': context stack is only"
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001454 " %d level%s deep", name, i, (i == 1 ? "" : "s"));
H. Peter Anvine2c80182005-01-15 22:15:51 +00001455 return NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001456 }
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001457
1458 if (namep)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001459 *namep = name;
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001460
Keith Kanios404589e2010-08-10 20:12:57 -05001461 if (!all_contexts)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001462 return ctx;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001463
Cyrill Gorcunovd90693c2010-08-11 20:31:46 +04001464 /*
1465 * NOTE: In 2.10 we will not need lookup in extarnal
1466 * contexts, so this is a gentle way to inform users
1467 * about their source code need to be updated
1468 */
1469
1470 /* first round -- check the current context */
1471 m = hash_findix(&ctx->localmac, name);
1472 while (m) {
1473 if (!mstrcmp(m->name, name, m->casesense))
1474 return ctx;
1475 m = m->next;
1476 }
1477
1478 /* second round - external contexts */
1479 while ((ctx = ctx->next)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001480 /* Search for this smacro in found context */
H. Peter Anvin166c2472008-05-28 12:28:58 -07001481 m = hash_findix(&ctx->localmac, name);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001482 while (m) {
Keith Kanios404589e2010-08-10 20:12:57 -05001483 if (!mstrcmp(m->name, name, m->casesense)) {
Keith Kanios88d947e2010-08-14 12:47:45 -05001484 /* NOTE: deprecated as of 2.10 */
Cyrill Gorcunovd90693c2010-08-11 20:31:46 +04001485 static int once = 0;
1486 if (!once) {
1487 error(ERR_WARNING, "context-local macro expansion"
Cyrill Gorcunov84b4cba2010-09-12 21:39:40 +04001488 " fall-through (automatic searching of outer"
1489 " contexts) will be deprecated starting in"
1490 " NASM 2.10, please see the NASM Manual for"
1491 " more information");
Cyrill Gorcunovd90693c2010-08-11 20:31:46 +04001492 once = 1;
1493 }
Keith Kanios88d947e2010-08-14 12:47:45 -05001494 error(ERR_WARNING, "`%s': context-local macro expansion fall-through", name);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001495 return ctx;
Cyrill Gorcunovd90693c2010-08-11 20:31:46 +04001496 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001497 m = m->next;
1498 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00001499 }
Cyrill Gorcunovd90693c2010-08-11 20:31:46 +04001500
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001501 return NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001502}
1503
1504/*
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001505 * Check to see if a file is already in a string list
1506 */
1507static bool in_list(const StrList *list, const char *str)
1508{
1509 while (list) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001510 if (!strcmp(list->str, str))
1511 return true;
1512 list = list->next;
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001513 }
1514 return false;
1515}
1516
1517/*
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001518 * Open an include file. This routine must always return a valid
1519 * file pointer if it returns - it's responsible for throwing an
1520 * ERR_FATAL and bombing out completely if not. It should also try
1521 * the include path one by one until it finds the file or reaches
1522 * the end of the path.
1523 */
H. Peter Anvin2b1c3b92008-06-06 10:38:46 -07001524static FILE *inc_fopen(const char *file, StrList **dhead, StrList ***dtail,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001525 bool missing_ok)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001526{
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001527 FILE *fp;
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001528 char *prefix = "";
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001529 IncPath *ip = ipath;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001530 int len = strlen(file);
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001531 size_t prefix_len = 0;
1532 StrList *sl;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001533
H. Peter Anvine2c80182005-01-15 22:15:51 +00001534 while (1) {
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001535 sl = nasm_malloc(prefix_len+len+1+sizeof sl->next);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001536 memcpy(sl->str, prefix, prefix_len);
1537 memcpy(sl->str+prefix_len, file, len+1);
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001538 fp = fopen(sl->str, "r");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001539 if (fp && dhead && !in_list(*dhead, sl->str)) {
1540 sl->next = NULL;
1541 **dtail = sl;
1542 *dtail = &sl->next;
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001543 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001544 nasm_free(sl);
1545 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001546 if (fp)
1547 return fp;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001548 if (!ip) {
1549 if (!missing_ok)
1550 break;
1551 prefix = NULL;
1552 } else {
1553 prefix = ip->path;
1554 ip = ip->next;
1555 }
1556 if (prefix) {
1557 prefix_len = strlen(prefix);
1558 } else {
1559 /* -MG given and file not found */
1560 if (dhead && !in_list(*dhead, file)) {
1561 sl = nasm_malloc(len+1+sizeof sl->next);
1562 sl->next = NULL;
1563 strcpy(sl->str, file);
1564 **dtail = sl;
1565 *dtail = &sl->next;
1566 }
1567 return NULL;
1568 }
H. Peter Anvineba20a72002-04-30 20:53:55 +00001569 }
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001570
H. Peter Anvin734b1882002-04-30 21:01:08 +00001571 error(ERR_FATAL, "unable to open include file `%s'", file);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001572 return NULL;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001573}
1574
1575/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001576 * Determine if we should warn on defining a single-line macro of
H. Peter Anvinef7468f2002-04-30 20:57:59 +00001577 * name `name', with `nparam' parameters. If nparam is 0 or -1, will
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001578 * return true if _any_ single-line macro of that name is defined.
1579 * Otherwise, will return true if a single-line macro with either
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001580 * `nparam' or no parameters is defined.
1581 *
1582 * If a macro with precisely the right number of parameters is
H. Peter Anvinef7468f2002-04-30 20:57:59 +00001583 * defined, or nparam is -1, the address of the definition structure
1584 * will be returned in `defn'; otherwise NULL will be returned. If `defn'
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001585 * is NULL, no action will be taken regarding its contents, and no
1586 * error will occur.
1587 *
1588 * Note that this is also called with nparam zero to resolve
1589 * `ifdef'.
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001590 *
1591 * If you already know which context macro belongs to, you can pass
1592 * the context pointer as first parameter; if you won't but name begins
1593 * with %$ the context will be automatically computed. If all_contexts
1594 * is true, macro will be searched in outer contexts as well.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001595 */
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07001596static bool
H. Peter Anvinb2a5fda2008-06-19 21:42:42 -07001597smacro_defined(Context * ctx, const char *name, int nparam, SMacro ** defn,
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07001598 bool nocase)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001599{
H. Peter Anvin166c2472008-05-28 12:28:58 -07001600 struct hash_table *smtbl;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001601 SMacro *m;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001602
H. Peter Anvin97a23472007-09-16 17:57:25 -07001603 if (ctx) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001604 smtbl = &ctx->localmac;
H. Peter Anvin97a23472007-09-16 17:57:25 -07001605 } else if (name[0] == '%' && name[1] == '$') {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001606 if (cstk)
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001607 ctx = get_ctx(name, &name, false);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001608 if (!ctx)
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001609 return false; /* got to return _something_ */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001610 smtbl = &ctx->localmac;
H. Peter Anvin97a23472007-09-16 17:57:25 -07001611 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001612 smtbl = &smacros;
H. Peter Anvin97a23472007-09-16 17:57:25 -07001613 }
H. Peter Anvin166c2472008-05-28 12:28:58 -07001614 m = (SMacro *) hash_findix(smtbl, name);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001615
H. Peter Anvine2c80182005-01-15 22:15:51 +00001616 while (m) {
1617 if (!mstrcmp(m->name, name, m->casesense && nocase) &&
Charles Crayne192d5b52007-10-18 19:02:42 -07001618 (nparam <= 0 || m->nparam == 0 || nparam == (int) m->nparam)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001619 if (defn) {
Charles Crayne192d5b52007-10-18 19:02:42 -07001620 if (nparam == (int) m->nparam || nparam == -1)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001621 *defn = m;
1622 else
1623 *defn = NULL;
1624 }
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001625 return true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001626 }
1627 m = m->next;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001628 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001629
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001630 return false;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001631}
1632
1633/*
1634 * Count and mark off the parameters in a multi-line macro call.
1635 * This is called both from within the multi-line macro expansion
1636 * code, and also to mark off the default parameters when provided
1637 * in a %macro definition line.
1638 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001639static void count_mmac_params(Token * t, int *nparam, Token *** params)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001640{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001641 int paramsize, brace;
1642
1643 *nparam = paramsize = 0;
1644 *params = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001645 while (t) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001646 /* +1: we need space for the final NULL */
H. Peter Anvin91fb6f12008-09-01 10:56:33 -07001647 if (*nparam+1 >= paramsize) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001648 paramsize += PARAM_DELTA;
1649 *params = nasm_realloc(*params, sizeof(**params) * paramsize);
1650 }
1651 skip_white_(t);
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001652 brace = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001653 if (tok_is_(t, "{"))
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001654 brace = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001655 (*params)[(*nparam)++] = t;
1656 while (tok_isnt_(t, brace ? "}" : ","))
1657 t = t->next;
1658 if (t) { /* got a comma/brace */
1659 t = t->next;
1660 if (brace) {
1661 /*
1662 * Now we've found the closing brace, look further
1663 * for the comma.
1664 */
1665 skip_white_(t);
1666 if (tok_isnt_(t, ",")) {
1667 error(ERR_NONFATAL,
1668 "braces do not enclose all of macro parameter");
1669 while (tok_isnt_(t, ","))
1670 t = t->next;
1671 }
1672 if (t)
1673 t = t->next; /* eat the comma */
1674 }
1675 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001676 }
1677}
1678
1679/*
H. Peter Anvin76690a12002-04-30 20:52:49 +00001680 * Determine whether one of the various `if' conditions is true or
1681 * not.
1682 *
1683 * We must free the tline we get passed.
1684 */
H. Peter Anvin70055962007-10-11 00:05:31 -07001685static bool if_condition(Token * tline, enum preproc_token ct)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001686{
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001687 enum pp_conditional i = PP_COND(ct);
H. Peter Anvin70055962007-10-11 00:05:31 -07001688 bool j;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001689 Token *t, *tt, **tptr, *origline;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001690 struct tokenval tokval;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001691 expr *evalresult;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001692 enum pp_token_type needtype;
H. Peter Anvin077fb932010-07-20 14:56:30 -07001693 char *p;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001694
1695 origline = tline;
1696
H. Peter Anvine2c80182005-01-15 22:15:51 +00001697 switch (i) {
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001698 case PPC_IFCTX:
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001699 j = false; /* have we matched yet? */
Victor van den Elzen0e857f12008-07-23 13:21:29 +02001700 while (true) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001701 skip_white_(tline);
Victor van den Elzen0e857f12008-07-23 13:21:29 +02001702 if (!tline)
1703 break;
1704 if (tline->type != TOK_ID) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001705 error(ERR_NONFATAL,
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001706 "`%s' expects context identifiers", pp_directives[ct]);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001707 free_tlist(origline);
1708 return -1;
1709 }
Victor van den Elzen0e857f12008-07-23 13:21:29 +02001710 if (cstk && cstk->name && !nasm_stricmp(tline->text, cstk->name))
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001711 j = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001712 tline = tline->next;
1713 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001714 break;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001715
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001716 case PPC_IFDEF:
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001717 j = false; /* have we matched yet? */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001718 while (tline) {
1719 skip_white_(tline);
1720 if (!tline || (tline->type != TOK_ID &&
1721 (tline->type != TOK_PREPROC_ID ||
1722 tline->text[1] != '$'))) {
1723 error(ERR_NONFATAL,
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001724 "`%s' expects macro identifiers", pp_directives[ct]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001725 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001726 }
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07001727 if (smacro_defined(NULL, tline->text, 0, NULL, true))
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001728 j = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001729 tline = tline->next;
1730 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001731 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001732
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001733 case PPC_IFENV:
Cyrill Gorcunov84b4cba2010-09-12 21:39:40 +04001734 tline = expand_smacro(tline);
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001735 j = false; /* have we matched yet? */
1736 while (tline) {
1737 skip_white_(tline);
1738 if (!tline || (tline->type != TOK_ID &&
Cyrill Gorcunov84b4cba2010-09-12 21:39:40 +04001739 tline->type != TOK_STRING &&
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001740 (tline->type != TOK_PREPROC_ID ||
Cyrill Gorcunov84b4cba2010-09-12 21:39:40 +04001741 tline->text[1] != '!'))) {
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001742 error(ERR_NONFATAL,
1743 "`%s' expects environment variable names",
Cyrill Gorcunov84b4cba2010-09-12 21:39:40 +04001744 pp_directives[ct]);
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001745 goto fail;
1746 }
Cyrill Gorcunov84b4cba2010-09-12 21:39:40 +04001747 p = tline->text;
1748 if (tline->type == TOK_PREPROC_ID)
1749 p += 2; /* Skip leading %! */
1750 if (*p == '\'' || *p == '\"' || *p == '`')
1751 nasm_unquote_cstr(p, ct);
1752 if (getenv(p))
1753 j = true;
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001754 tline = tline->next;
1755 }
1756 break;
1757
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001758 case PPC_IFIDN:
1759 case PPC_IFIDNI:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001760 tline = expand_smacro(tline);
1761 t = tt = tline;
1762 while (tok_isnt_(tt, ","))
1763 tt = tt->next;
1764 if (!tt) {
1765 error(ERR_NONFATAL,
1766 "`%s' expects two comma-separated arguments",
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001767 pp_directives[ct]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001768 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001769 }
1770 tt = tt->next;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001771 j = true; /* assume equality unless proved not */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001772 while ((t->type != TOK_OTHER || strcmp(t->text, ",")) && tt) {
1773 if (tt->type == TOK_OTHER && !strcmp(tt->text, ",")) {
1774 error(ERR_NONFATAL, "`%s': more than one comma on line",
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001775 pp_directives[ct]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001776 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001777 }
1778 if (t->type == TOK_WHITESPACE) {
1779 t = t->next;
1780 continue;
1781 }
1782 if (tt->type == TOK_WHITESPACE) {
1783 tt = tt->next;
1784 continue;
1785 }
1786 if (tt->type != t->type) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001787 j = false; /* found mismatching tokens */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001788 break;
1789 }
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07001790 /* When comparing strings, need to unquote them first */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001791 if (t->type == TOK_STRING) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001792 size_t l1 = nasm_unquote(t->text, NULL);
1793 size_t l2 = nasm_unquote(tt->text, NULL);
H. Peter Anvind2456592008-06-19 15:04:18 -07001794
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001795 if (l1 != l2) {
1796 j = false;
1797 break;
1798 }
1799 if (mmemcmp(t->text, tt->text, l1, i == PPC_IFIDN)) {
1800 j = false;
1801 break;
1802 }
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07001803 } else if (mstrcmp(tt->text, t->text, i == PPC_IFIDN) != 0) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001804 j = false; /* found mismatching tokens */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001805 break;
1806 }
Nickolay Yurchenkof3b3ce22003-09-21 20:38:43 +00001807
H. Peter Anvine2c80182005-01-15 22:15:51 +00001808 t = t->next;
1809 tt = tt->next;
1810 }
1811 if ((t->type != TOK_OTHER || strcmp(t->text, ",")) || tt)
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001812 j = false; /* trailing gunk on one end or other */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001813 break;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001814
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001815 case PPC_IFMACRO:
H. Peter Anvin89cee572009-07-15 09:16:54 -04001816 {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001817 bool found = false;
1818 MMacro searching, *mmac;
H. Peter Anvin65747262002-05-07 00:10:05 +00001819
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001820 skip_white_(tline);
1821 tline = expand_id(tline);
1822 if (!tok_type_(tline, TOK_ID)) {
1823 error(ERR_NONFATAL,
1824 "`%s' expects a macro name", pp_directives[ct]);
1825 goto fail;
1826 }
1827 searching.name = nasm_strdup(tline->text);
1828 searching.casesense = true;
1829 searching.plus = false;
1830 searching.nolist = false;
1831 searching.in_progress = 0;
1832 searching.max_depth = 0;
1833 searching.rep_nest = NULL;
1834 searching.nparam_min = 0;
1835 searching.nparam_max = INT_MAX;
1836 tline = expand_smacro(tline->next);
1837 skip_white_(tline);
1838 if (!tline) {
1839 } else if (!tok_type_(tline, TOK_NUMBER)) {
1840 error(ERR_NONFATAL,
1841 "`%s' expects a parameter count or nothing",
1842 pp_directives[ct]);
1843 } else {
1844 searching.nparam_min = searching.nparam_max =
1845 readnum(tline->text, &j);
1846 if (j)
1847 error(ERR_NONFATAL,
1848 "unable to parse parameter count `%s'",
1849 tline->text);
1850 }
1851 if (tline && tok_is_(tline->next, "-")) {
1852 tline = tline->next->next;
1853 if (tok_is_(tline, "*"))
1854 searching.nparam_max = INT_MAX;
1855 else if (!tok_type_(tline, TOK_NUMBER))
1856 error(ERR_NONFATAL,
1857 "`%s' expects a parameter count after `-'",
1858 pp_directives[ct]);
1859 else {
1860 searching.nparam_max = readnum(tline->text, &j);
1861 if (j)
1862 error(ERR_NONFATAL,
1863 "unable to parse parameter count `%s'",
1864 tline->text);
1865 if (searching.nparam_min > searching.nparam_max)
1866 error(ERR_NONFATAL,
1867 "minimum parameter count exceeds maximum");
1868 }
1869 }
1870 if (tline && tok_is_(tline->next, "+")) {
1871 tline = tline->next;
1872 searching.plus = true;
1873 }
1874 mmac = (MMacro *) hash_findix(&mmacros, searching.name);
1875 while (mmac) {
1876 if (!strcmp(mmac->name, searching.name) &&
1877 (mmac->nparam_min <= searching.nparam_max
1878 || searching.plus)
1879 && (searching.nparam_min <= mmac->nparam_max
1880 || mmac->plus)) {
1881 found = true;
1882 break;
1883 }
1884 mmac = mmac->next;
1885 }
1886 if (tline && tline->next)
1887 error(ERR_WARNING|ERR_PASS1,
1888 "trailing garbage after %%ifmacro ignored");
1889 nasm_free(searching.name);
1890 j = found;
1891 break;
H. Peter Anvin89cee572009-07-15 09:16:54 -04001892 }
H. Peter Anvin65747262002-05-07 00:10:05 +00001893
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001894 case PPC_IFID:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001895 needtype = TOK_ID;
1896 goto iftype;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001897 case PPC_IFNUM:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001898 needtype = TOK_NUMBER;
1899 goto iftype;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001900 case PPC_IFSTR:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001901 needtype = TOK_STRING;
1902 goto iftype;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001903
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001904iftype:
1905 t = tline = expand_smacro(tline);
H. Peter Anvind85d2502008-05-04 17:53:31 -07001906
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001907 while (tok_type_(t, TOK_WHITESPACE) ||
1908 (needtype == TOK_NUMBER &&
1909 tok_type_(t, TOK_OTHER) &&
1910 (t->text[0] == '-' || t->text[0] == '+') &&
1911 !t->text[1]))
1912 t = t->next;
H. Peter Anvind85d2502008-05-04 17:53:31 -07001913
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001914 j = tok_type_(t, needtype);
1915 break;
H. Peter Anvincbf768d2008-02-16 16:41:25 -08001916
1917 case PPC_IFTOKEN:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001918 t = tline = expand_smacro(tline);
1919 while (tok_type_(t, TOK_WHITESPACE))
1920 t = t->next;
H. Peter Anvincbf768d2008-02-16 16:41:25 -08001921
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001922 j = false;
1923 if (t) {
1924 t = t->next; /* Skip the actual token */
1925 while (tok_type_(t, TOK_WHITESPACE))
1926 t = t->next;
1927 j = !t; /* Should be nothing left */
1928 }
1929 break;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001930
H. Peter Anvin134b9462008-02-16 17:01:40 -08001931 case PPC_IFEMPTY:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001932 t = tline = expand_smacro(tline);
1933 while (tok_type_(t, TOK_WHITESPACE))
1934 t = t->next;
H. Peter Anvin134b9462008-02-16 17:01:40 -08001935
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001936 j = !t; /* Should be empty */
1937 break;
H. Peter Anvin134b9462008-02-16 17:01:40 -08001938
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001939 case PPC_IF:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001940 t = tline = expand_smacro(tline);
1941 tptr = &t;
1942 tokval.t_type = TOKEN_INVALID;
1943 evalresult = evaluate(ppscan, tptr, &tokval,
1944 NULL, pass | CRITICAL, error, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001945 if (!evalresult)
1946 return -1;
1947 if (tokval.t_type)
H. Peter Anvin917a3492008-09-24 09:14:49 -07001948 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +00001949 "trailing garbage after expression ignored");
1950 if (!is_simple(evalresult)) {
1951 error(ERR_NONFATAL,
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001952 "non-constant value given to `%s'", pp_directives[ct]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001953 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001954 }
Chuck Crayne60ae75d2007-05-02 01:59:16 +00001955 j = reloc_value(evalresult) != 0;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001956 break;
H. Peter Anvin95e28822007-09-12 04:20:08 +00001957
H. Peter Anvine2c80182005-01-15 22:15:51 +00001958 default:
1959 error(ERR_FATAL,
1960 "preprocessor directive `%s' not yet implemented",
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001961 pp_directives[ct]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001962 goto fail;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001963 }
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001964
1965 free_tlist(origline);
1966 return j ^ PP_NEGATIVE(ct);
H. Peter Anvin70653092007-10-19 14:42:29 -07001967
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001968fail:
1969 free_tlist(origline);
1970 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001971}
1972
1973/*
H. Peter Anvin4db5a162007-10-11 13:42:09 -07001974 * Common code for defining an smacro
1975 */
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001976static bool define_smacro(Context *ctx, const char *mname, bool casesense,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001977 int nparam, Token *expansion)
H. Peter Anvin4db5a162007-10-11 13:42:09 -07001978{
1979 SMacro *smac, **smhead;
H. Peter Anvin166c2472008-05-28 12:28:58 -07001980 struct hash_table *smtbl;
H. Peter Anvin70653092007-10-19 14:42:29 -07001981
H. Peter Anvin4db5a162007-10-11 13:42:09 -07001982 if (smacro_defined(ctx, mname, nparam, &smac, casesense)) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001983 if (!smac) {
1984 error(ERR_WARNING|ERR_PASS1,
1985 "single-line macro `%s' defined both with and"
1986 " without parameters", mname);
1987 /*
1988 * Some instances of the old code considered this a failure,
1989 * some others didn't. What is the right thing to do here?
1990 */
1991 free_tlist(expansion);
1992 return false; /* Failure */
1993 } else {
1994 /*
1995 * We're redefining, so we have to take over an
1996 * existing SMacro structure. This means freeing
1997 * what was already in it.
1998 */
1999 nasm_free(smac->name);
2000 free_tlist(smac->expansion);
2001 }
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002002 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002003 smtbl = ctx ? &ctx->localmac : &smacros;
2004 smhead = (SMacro **) hash_findi_add(smtbl, mname);
2005 smac = nasm_malloc(sizeof(SMacro));
2006 smac->next = *smhead;
2007 *smhead = smac;
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002008 }
2009 smac->name = nasm_strdup(mname);
2010 smac->casesense = casesense;
2011 smac->nparam = nparam;
2012 smac->expansion = expansion;
2013 smac->in_progress = false;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002014 return true; /* Success */
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002015}
2016
2017/*
2018 * Undefine an smacro
2019 */
2020static void undef_smacro(Context *ctx, const char *mname)
2021{
2022 SMacro **smhead, *s, **sp;
H. Peter Anvin166c2472008-05-28 12:28:58 -07002023 struct hash_table *smtbl;
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002024
H. Peter Anvin166c2472008-05-28 12:28:58 -07002025 smtbl = ctx ? &ctx->localmac : &smacros;
2026 smhead = (SMacro **)hash_findi(smtbl, mname, NULL);
H. Peter Anvin70653092007-10-19 14:42:29 -07002027
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002028 if (smhead) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002029 /*
2030 * We now have a macro name... go hunt for it.
2031 */
2032 sp = smhead;
2033 while ((s = *sp) != NULL) {
2034 if (!mstrcmp(s->name, mname, s->casesense)) {
2035 *sp = s->next;
2036 nasm_free(s->name);
2037 free_tlist(s->expansion);
2038 nasm_free(s);
2039 } else {
2040 sp = &s->next;
2041 }
2042 }
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002043 }
2044}
2045
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002046/*
H. Peter Anvina26433d2008-07-16 14:40:01 -07002047 * Parse a mmacro specification.
2048 */
2049static bool parse_mmacro_spec(Token *tline, MMacro *def, const char *directive)
2050{
2051 bool err;
2052
2053 tline = tline->next;
2054 skip_white_(tline);
2055 tline = expand_id(tline);
2056 if (!tok_type_(tline, TOK_ID)) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002057 error(ERR_NONFATAL, "`%s' expects a macro name", directive);
2058 return false;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002059 }
Victor van den Elzenb916ede2008-07-23 15:14:22 +02002060
H. Peter Anvin89cee572009-07-15 09:16:54 -04002061 def->prev = NULL;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002062 def->name = nasm_strdup(tline->text);
2063 def->plus = false;
2064 def->nolist = false;
2065 def->in_progress = 0;
2066 def->rep_nest = NULL;
Victor van den Elzenb916ede2008-07-23 15:14:22 +02002067 def->nparam_min = 0;
2068 def->nparam_max = 0;
2069
H. Peter Anvina26433d2008-07-16 14:40:01 -07002070 tline = expand_smacro(tline->next);
2071 skip_white_(tline);
2072 if (!tok_type_(tline, TOK_NUMBER)) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002073 error(ERR_NONFATAL, "`%s' expects a parameter count", directive);
H. Peter Anvina26433d2008-07-16 14:40:01 -07002074 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002075 def->nparam_min = def->nparam_max =
2076 readnum(tline->text, &err);
2077 if (err)
2078 error(ERR_NONFATAL,
2079 "unable to parse parameter count `%s'", tline->text);
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->next;
2083 if (tok_is_(tline, "*")) {
2084 def->nparam_max = INT_MAX;
2085 } else if (!tok_type_(tline, TOK_NUMBER)) {
2086 error(ERR_NONFATAL,
2087 "`%s' expects a parameter count after `-'", directive);
2088 } else {
2089 def->nparam_max = readnum(tline->text, &err);
2090 if (err) {
2091 error(ERR_NONFATAL, "unable to parse parameter count `%s'",
2092 tline->text);
2093 }
2094 if (def->nparam_min > def->nparam_max) {
2095 error(ERR_NONFATAL, "minimum parameter count exceeds maximum");
2096 }
2097 }
H. Peter Anvina26433d2008-07-16 14:40:01 -07002098 }
2099 if (tline && tok_is_(tline->next, "+")) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002100 tline = tline->next;
2101 def->plus = true;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002102 }
2103 if (tline && tok_type_(tline->next, TOK_ID) &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002104 !nasm_stricmp(tline->next->text, ".nolist")) {
2105 tline = tline->next;
2106 def->nolist = true;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002107 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002108
H. Peter Anvina26433d2008-07-16 14:40:01 -07002109 /*
2110 * Handle default parameters.
2111 */
2112 if (tline && tline->next) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002113 def->dlist = tline->next;
2114 tline->next = NULL;
2115 count_mmac_params(def->dlist, &def->ndefs, &def->defaults);
H. Peter Anvina26433d2008-07-16 14:40:01 -07002116 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002117 def->dlist = NULL;
2118 def->defaults = NULL;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002119 }
2120 def->expansion = NULL;
2121
H. Peter Anvin89cee572009-07-15 09:16:54 -04002122 if (def->defaults && def->ndefs > def->nparam_max - def->nparam_min &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002123 !def->plus)
2124 error(ERR_WARNING|ERR_PASS1|ERR_WARN_MDP,
2125 "too many default macro parameters");
Victor van den Elzenb916ede2008-07-23 15:14:22 +02002126
H. Peter Anvina26433d2008-07-16 14:40:01 -07002127 return true;
2128}
2129
2130
2131/*
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002132 * Decode a size directive
2133 */
2134static int parse_size(const char *str) {
2135 static const char *size_names[] =
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002136 { "byte", "dword", "oword", "qword", "tword", "word", "yword" };
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002137 static const int sizes[] =
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002138 { 0, 1, 4, 16, 8, 10, 2, 32 };
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002139
Cyrill Gorcunova7319242010-06-03 22:04:36 +04002140 return sizes[bsii(str, size_names, ARRAY_SIZE(size_names))+1];
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002141}
2142
Ed Beroset3ab3f412002-06-11 03:31:49 +00002143/**
2144 * find and process preprocessor directive in passed line
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002145 * Find out if a line contains a preprocessor directive, and deal
2146 * with it if so.
H. Peter Anvin70653092007-10-19 14:42:29 -07002147 *
Ed Beroset3ab3f412002-06-11 03:31:49 +00002148 * If a directive _is_ found, it is the responsibility of this routine
2149 * (and not the caller) to free_tlist() the line.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002150 *
Ed Beroset3ab3f412002-06-11 03:31:49 +00002151 * @param tline a pointer to the current tokeninzed line linked list
2152 * @return DIRECTIVE_FOUND or NO_DIRECTIVE_FOUND
H. Peter Anvin70653092007-10-19 14:42:29 -07002153 *
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002154 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002155static int do_directive(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00002156{
H. Peter Anvin4169a472007-09-12 01:29:43 +00002157 enum preproc_token i;
2158 int j;
H. Peter Anvin70055962007-10-11 00:05:31 -07002159 bool err;
2160 int nparam;
2161 bool nolist;
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07002162 bool casesense;
H. Peter Anvin8cfdb9d2007-09-14 18:36:01 -07002163 int k, m;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002164 int offset;
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08002165 char *p, *pp;
2166 const char *mname;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002167 Include *inc;
2168 Context *ctx;
2169 Cond *cond;
H. Peter Anvin97a23472007-09-16 17:57:25 -07002170 MMacro *mmac, **mmhead;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002171 Token *t, *tt, *param_start, *macro_start, *last, **tptr, *origline;
2172 Line *l;
2173 struct tokenval tokval;
2174 expr *evalresult;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002175 MMacro *tmp_defining; /* Used when manipulating rep_nest */
H. Peter Anvinf8ba53e2007-10-11 10:11:57 -07002176 int64_t count;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07002177 size_t len;
H. Peter Anvin8e3f75e2008-09-24 00:21:58 -07002178 int severity;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002179
2180 origline = tline;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002181
H. Peter Anvineba20a72002-04-30 20:53:55 +00002182 skip_white_(tline);
H. Peter Anvinf2936d72008-06-04 15:11:23 -07002183 if (!tline || !tok_type_(tline, TOK_PREPROC_ID) ||
H. Peter Anvine2c80182005-01-15 22:15:51 +00002184 (tline->text[1] == '%' || tline->text[1] == '$'
2185 || tline->text[1] == '!'))
2186 return NO_DIRECTIVE_FOUND;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002187
H. Peter Anvin4169a472007-09-12 01:29:43 +00002188 i = pp_token_hash(tline->text);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002189
2190 /*
Cyrill Gorcunovf09116f2010-02-28 11:52:53 +03002191 * FIXME: We zap execution of PP_RMACRO, PP_IRMACRO, PP_EXITMACRO
2192 * since they are known to be buggy at moment, we need to fix them
2193 * in future release (2.09-2.10)
2194 */
2195 if (i == PP_RMACRO || i == PP_RMACRO || i == PP_EXITMACRO) {
2196 error(ERR_NONFATAL, "unknown preprocessor directive `%s'",
2197 tline->text);
2198 return NO_DIRECTIVE_FOUND;
2199 }
2200
2201 /*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002202 * If we're in a non-emitting branch of a condition construct,
H. Peter Anvin76690a12002-04-30 20:52:49 +00002203 * or walking to the end of an already terminated %rep block,
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002204 * we should ignore all directives except for condition
2205 * directives.
2206 */
H. Peter Anvin76690a12002-04-30 20:52:49 +00002207 if (((istk->conds && !emitting(istk->conds->state)) ||
H. Peter Anvine2c80182005-01-15 22:15:51 +00002208 (istk->mstk && !istk->mstk->in_progress)) && !is_condition(i)) {
2209 return NO_DIRECTIVE_FOUND;
H. Peter Anvineba20a72002-04-30 20:53:55 +00002210 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002211
2212 /*
H. Peter Anvin76690a12002-04-30 20:52:49 +00002213 * If we're defining a macro or reading a %rep block, we should
Victor van den Elzen8f1120f2008-07-16 13:41:37 +02002214 * ignore all directives except for %macro/%imacro (which nest),
2215 * %endm/%endmacro, and (only if we're in a %rep block) %endrep.
2216 * If we're in a %rep block, another %rep nests, so should be let through.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002217 */
2218 if (defining && i != PP_MACRO && i != PP_IMACRO &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002219 i != PP_RMACRO && i != PP_IRMACRO &&
H. Peter Anvine2c80182005-01-15 22:15:51 +00002220 i != PP_ENDMACRO && i != PP_ENDM &&
2221 (defining->name || (i != PP_ENDREP && i != PP_REP))) {
2222 return NO_DIRECTIVE_FOUND;
H. Peter Anvineba20a72002-04-30 20:53:55 +00002223 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002224
Charles Crayned4200be2008-07-12 16:42:33 -07002225 if (defining) {
Keith Kanios852f1ee2009-07-12 00:19:55 -05002226 if (i == PP_MACRO || i == PP_IMACRO ||
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002227 i == PP_RMACRO || i == PP_IRMACRO) {
Charles Crayned4200be2008-07-12 16:42:33 -07002228 nested_mac_count++;
2229 return NO_DIRECTIVE_FOUND;
2230 } else if (nested_mac_count > 0) {
2231 if (i == PP_ENDMACRO) {
2232 nested_mac_count--;
2233 return NO_DIRECTIVE_FOUND;
2234 }
2235 }
2236 if (!defining->name) {
2237 if (i == PP_REP) {
2238 nested_rep_count++;
2239 return NO_DIRECTIVE_FOUND;
2240 } else if (nested_rep_count > 0) {
2241 if (i == PP_ENDREP) {
2242 nested_rep_count--;
2243 return NO_DIRECTIVE_FOUND;
2244 }
2245 }
2246 }
2247 }
2248
H. Peter Anvin4169a472007-09-12 01:29:43 +00002249 switch (i) {
2250 case PP_INVALID:
H. Peter Anvine2c80182005-01-15 22:15:51 +00002251 error(ERR_NONFATAL, "unknown preprocessor directive `%s'",
2252 tline->text);
2253 return NO_DIRECTIVE_FOUND; /* didn't get it */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002254
H. Peter Anvine2c80182005-01-15 22:15:51 +00002255 case PP_STACKSIZE:
2256 /* Directive to tell NASM what the default stack size is. The
2257 * default is for a 16-bit stack, and this can be overriden with
2258 * %stacksize large.
H. Peter Anvine2c80182005-01-15 22:15:51 +00002259 */
2260 tline = tline->next;
2261 if (tline && tline->type == TOK_WHITESPACE)
2262 tline = tline->next;
2263 if (!tline || tline->type != TOK_ID) {
2264 error(ERR_NONFATAL, "`%%stacksize' missing size parameter");
2265 free_tlist(origline);
2266 return DIRECTIVE_FOUND;
2267 }
2268 if (nasm_stricmp(tline->text, "flat") == 0) {
2269 /* All subsequent ARG directives are for a 32-bit stack */
2270 StackSize = 4;
2271 StackPointer = "ebp";
2272 ArgOffset = 8;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002273 LocalOffset = 0;
Charles Crayne7eaf9192007-11-08 22:11:14 -08002274 } else if (nasm_stricmp(tline->text, "flat64") == 0) {
2275 /* All subsequent ARG directives are for a 64-bit stack */
2276 StackSize = 8;
2277 StackPointer = "rbp";
Per Jessen53252e02010-02-11 00:16:59 +03002278 ArgOffset = 16;
Charles Crayne7eaf9192007-11-08 22:11:14 -08002279 LocalOffset = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002280 } else if (nasm_stricmp(tline->text, "large") == 0) {
2281 /* All subsequent ARG directives are for a 16-bit stack,
2282 * far function call.
2283 */
2284 StackSize = 2;
2285 StackPointer = "bp";
2286 ArgOffset = 4;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002287 LocalOffset = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002288 } else if (nasm_stricmp(tline->text, "small") == 0) {
2289 /* All subsequent ARG directives are for a 16-bit stack,
2290 * far function call. We don't support near functions.
2291 */
2292 StackSize = 2;
2293 StackPointer = "bp";
2294 ArgOffset = 6;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002295 LocalOffset = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002296 } else {
2297 error(ERR_NONFATAL, "`%%stacksize' invalid size type");
2298 free_tlist(origline);
2299 return DIRECTIVE_FOUND;
2300 }
2301 free_tlist(origline);
2302 return DIRECTIVE_FOUND;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002303
H. Peter Anvine2c80182005-01-15 22:15:51 +00002304 case PP_ARG:
2305 /* TASM like ARG directive to define arguments to functions, in
2306 * the following form:
2307 *
2308 * ARG arg1:WORD, arg2:DWORD, arg4:QWORD
2309 */
2310 offset = ArgOffset;
2311 do {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00002312 char *arg, directive[256];
H. Peter Anvine2c80182005-01-15 22:15:51 +00002313 int size = StackSize;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002314
H. Peter Anvine2c80182005-01-15 22:15:51 +00002315 /* Find the argument name */
2316 tline = tline->next;
2317 if (tline && tline->type == TOK_WHITESPACE)
2318 tline = tline->next;
2319 if (!tline || tline->type != TOK_ID) {
2320 error(ERR_NONFATAL, "`%%arg' missing argument parameter");
2321 free_tlist(origline);
2322 return DIRECTIVE_FOUND;
2323 }
2324 arg = tline->text;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002325
H. Peter Anvine2c80182005-01-15 22:15:51 +00002326 /* Find the argument size type */
2327 tline = tline->next;
2328 if (!tline || tline->type != TOK_OTHER
2329 || tline->text[0] != ':') {
2330 error(ERR_NONFATAL,
2331 "Syntax error processing `%%arg' directive");
2332 free_tlist(origline);
2333 return DIRECTIVE_FOUND;
2334 }
2335 tline = tline->next;
2336 if (!tline || tline->type != TOK_ID) {
2337 error(ERR_NONFATAL, "`%%arg' missing size type parameter");
2338 free_tlist(origline);
2339 return DIRECTIVE_FOUND;
2340 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002341
H. Peter Anvine2c80182005-01-15 22:15:51 +00002342 /* Allow macro expansion of type parameter */
Keith Kaniosb7a89542007-04-12 02:40:54 +00002343 tt = tokenize(tline->text);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002344 tt = expand_smacro(tt);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002345 size = parse_size(tt->text);
2346 if (!size) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002347 error(ERR_NONFATAL,
2348 "Invalid size type for `%%arg' missing directive");
2349 free_tlist(tt);
2350 free_tlist(origline);
2351 return DIRECTIVE_FOUND;
2352 }
2353 free_tlist(tt);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002354
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002355 /* Round up to even stack slots */
2356 size = ALIGN(size, StackSize);
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002357
H. Peter Anvine2c80182005-01-15 22:15:51 +00002358 /* Now define the macro for the argument */
2359 snprintf(directive, sizeof(directive), "%%define %s (%s+%d)",
2360 arg, StackPointer, offset);
Keith Kaniosb7a89542007-04-12 02:40:54 +00002361 do_directive(tokenize(directive));
H. Peter Anvine2c80182005-01-15 22:15:51 +00002362 offset += size;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002363
H. Peter Anvine2c80182005-01-15 22:15:51 +00002364 /* Move to the next argument in the list */
2365 tline = tline->next;
2366 if (tline && tline->type == TOK_WHITESPACE)
2367 tline = tline->next;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002368 } while (tline && tline->type == TOK_OTHER && tline->text[0] == ',');
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002369 ArgOffset = offset;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002370 free_tlist(origline);
2371 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002372
H. Peter Anvine2c80182005-01-15 22:15:51 +00002373 case PP_LOCAL:
2374 /* TASM like LOCAL directive to define local variables for a
2375 * function, in the following form:
2376 *
2377 * LOCAL local1:WORD, local2:DWORD, local4:QWORD = LocalSize
2378 *
2379 * The '= LocalSize' at the end is ignored by NASM, but is
2380 * required by TASM to define the local parameter size (and used
2381 * by the TASM macro package).
2382 */
2383 offset = LocalOffset;
2384 do {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00002385 char *local, directive[256];
H. Peter Anvine2c80182005-01-15 22:15:51 +00002386 int size = StackSize;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002387
H. Peter Anvine2c80182005-01-15 22:15:51 +00002388 /* Find the argument name */
2389 tline = tline->next;
2390 if (tline && tline->type == TOK_WHITESPACE)
2391 tline = tline->next;
2392 if (!tline || tline->type != TOK_ID) {
2393 error(ERR_NONFATAL,
2394 "`%%local' missing argument parameter");
2395 free_tlist(origline);
2396 return DIRECTIVE_FOUND;
2397 }
2398 local = tline->text;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002399
H. Peter Anvine2c80182005-01-15 22:15:51 +00002400 /* Find the argument size type */
2401 tline = tline->next;
2402 if (!tline || tline->type != TOK_OTHER
2403 || tline->text[0] != ':') {
2404 error(ERR_NONFATAL,
2405 "Syntax error processing `%%local' directive");
2406 free_tlist(origline);
2407 return DIRECTIVE_FOUND;
2408 }
2409 tline = tline->next;
2410 if (!tline || tline->type != TOK_ID) {
2411 error(ERR_NONFATAL,
2412 "`%%local' missing size type parameter");
2413 free_tlist(origline);
2414 return DIRECTIVE_FOUND;
2415 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00002416
H. Peter Anvine2c80182005-01-15 22:15:51 +00002417 /* Allow macro expansion of type parameter */
Keith Kaniosb7a89542007-04-12 02:40:54 +00002418 tt = tokenize(tline->text);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002419 tt = expand_smacro(tt);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002420 size = parse_size(tt->text);
2421 if (!size) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002422 error(ERR_NONFATAL,
2423 "Invalid size type for `%%local' missing directive");
2424 free_tlist(tt);
2425 free_tlist(origline);
2426 return DIRECTIVE_FOUND;
2427 }
2428 free_tlist(tt);
H. Peter Anvin734b1882002-04-30 21:01:08 +00002429
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002430 /* Round up to even stack slots */
2431 size = ALIGN(size, StackSize);
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002432
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002433 offset += size; /* Negative offset, increment before */
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002434
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002435 /* Now define the macro for the argument */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002436 snprintf(directive, sizeof(directive), "%%define %s (%s-%d)",
2437 local, StackPointer, offset);
Keith Kaniosb7a89542007-04-12 02:40:54 +00002438 do_directive(tokenize(directive));
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002439
H. Peter Anvine2c80182005-01-15 22:15:51 +00002440 /* Now define the assign to setup the enter_c macro correctly */
2441 snprintf(directive, sizeof(directive),
2442 "%%assign %%$localsize %%$localsize+%d", size);
Keith Kaniosb7a89542007-04-12 02:40:54 +00002443 do_directive(tokenize(directive));
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002444
H. Peter Anvine2c80182005-01-15 22:15:51 +00002445 /* Move to the next argument in the list */
2446 tline = tline->next;
2447 if (tline && tline->type == TOK_WHITESPACE)
2448 tline = tline->next;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002449 } while (tline && tline->type == TOK_OTHER && tline->text[0] == ',');
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002450 LocalOffset = offset;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002451 free_tlist(origline);
2452 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002453
H. Peter Anvine2c80182005-01-15 22:15:51 +00002454 case PP_CLEAR:
2455 if (tline->next)
H. Peter Anvin917a3492008-09-24 09:14:49 -07002456 error(ERR_WARNING|ERR_PASS1,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002457 "trailing garbage after `%%clear' ignored");
2458 free_macros();
2459 init_macros();
H. Peter Anvine2c80182005-01-15 22:15:51 +00002460 free_tlist(origline);
2461 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002462
H. Peter Anvin418ca702008-05-30 10:42:30 -07002463 case PP_DEPEND:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002464 t = tline->next = expand_smacro(tline->next);
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002465 skip_white_(t);
2466 if (!t || (t->type != TOK_STRING &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002467 t->type != TOK_INTERNAL_STRING)) {
H. Peter Anvin418ca702008-05-30 10:42:30 -07002468 error(ERR_NONFATAL, "`%%depend' expects a file name");
2469 free_tlist(origline);
2470 return DIRECTIVE_FOUND; /* but we did _something_ */
2471 }
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002472 if (t->next)
H. Peter Anvin917a3492008-09-24 09:14:49 -07002473 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvin418ca702008-05-30 10:42:30 -07002474 "trailing garbage after `%%depend' ignored");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002475 p = t->text;
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002476 if (t->type != TOK_INTERNAL_STRING)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002477 nasm_unquote_cstr(p, i);
2478 if (dephead && !in_list(*dephead, p)) {
2479 StrList *sl = nasm_malloc(strlen(p)+1+sizeof sl->next);
2480 sl->next = NULL;
2481 strcpy(sl->str, p);
2482 *deptail = sl;
2483 deptail = &sl->next;
2484 }
2485 free_tlist(origline);
H. Peter Anvin418ca702008-05-30 10:42:30 -07002486 return DIRECTIVE_FOUND;
2487
2488 case PP_INCLUDE:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002489 t = tline->next = expand_smacro(tline->next);
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002490 skip_white_(t);
H. Peter Anvind2456592008-06-19 15:04:18 -07002491
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002492 if (!t || (t->type != TOK_STRING &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002493 t->type != TOK_INTERNAL_STRING)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002494 error(ERR_NONFATAL, "`%%include' expects a file name");
2495 free_tlist(origline);
2496 return DIRECTIVE_FOUND; /* but we did _something_ */
2497 }
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002498 if (t->next)
H. Peter Anvin917a3492008-09-24 09:14:49 -07002499 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +00002500 "trailing garbage after `%%include' ignored");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002501 p = t->text;
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002502 if (t->type != TOK_INTERNAL_STRING)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002503 nasm_unquote_cstr(p, i);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002504 inc = nasm_malloc(sizeof(Include));
2505 inc->next = istk;
2506 inc->conds = NULL;
H. Peter Anvin2b1c3b92008-06-06 10:38:46 -07002507 inc->fp = inc_fopen(p, dephead, &deptail, pass == 0);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002508 if (!inc->fp) {
2509 /* -MG given but file not found */
2510 nasm_free(inc);
2511 } else {
2512 inc->fname = src_set_fname(nasm_strdup(p));
2513 inc->lineno = src_set_linnum(0);
2514 inc->lineinc = 1;
2515 inc->expansion = NULL;
2516 inc->mstk = NULL;
2517 istk = inc;
2518 list->uplevel(LIST_INCLUDE);
2519 }
2520 free_tlist(origline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002521 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002522
H. Peter Anvind2456592008-06-19 15:04:18 -07002523 case PP_USE:
H. Peter Anvinf4ae5ad2008-06-19 18:39:24 -07002524 {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002525 static macros_t *use_pkg;
2526 const char *pkg_macro = NULL;
H. Peter Anvinf4ae5ad2008-06-19 18:39:24 -07002527
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002528 tline = tline->next;
2529 skip_white_(tline);
2530 tline = expand_id(tline);
H. Peter Anvind2456592008-06-19 15:04:18 -07002531
H. Peter Anvin264b7b92008-10-24 16:38:17 -07002532 if (!tline || (tline->type != TOK_STRING &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002533 tline->type != TOK_INTERNAL_STRING &&
2534 tline->type != TOK_ID)) {
H. Peter Anvin926fc402008-06-19 16:26:12 -07002535 error(ERR_NONFATAL, "`%%use' expects a package name");
H. Peter Anvind2456592008-06-19 15:04:18 -07002536 free_tlist(origline);
2537 return DIRECTIVE_FOUND; /* but we did _something_ */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002538 }
H. Peter Anvin264b7b92008-10-24 16:38:17 -07002539 if (tline->next)
H. Peter Anvin917a3492008-09-24 09:14:49 -07002540 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvind2456592008-06-19 15:04:18 -07002541 "trailing garbage after `%%use' ignored");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002542 if (tline->type == TOK_STRING)
2543 nasm_unquote_cstr(tline->text, i);
2544 use_pkg = nasm_stdmac_find_package(tline->text);
2545 if (!use_pkg)
2546 error(ERR_NONFATAL, "unknown `%%use' package: %s", tline->text);
2547 else
2548 pkg_macro = (char *)use_pkg + 1; /* The first string will be <%define>__USE_*__ */
Victor van den Elzen35eb2ea2010-03-10 22:33:48 +01002549 if (use_pkg && ! smacro_defined(NULL, pkg_macro, 0, NULL, true)) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002550 /* Not already included, go ahead and include it */
2551 stdmacpos = use_pkg;
2552 }
2553 free_tlist(origline);
H. Peter Anvind2456592008-06-19 15:04:18 -07002554 return DIRECTIVE_FOUND;
H. Peter Anvinf4ae5ad2008-06-19 18:39:24 -07002555 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002556 case PP_PUSH:
H. Peter Anvine2c80182005-01-15 22:15:51 +00002557 case PP_REPL:
H. Peter Anvin42b56392008-10-24 16:24:21 -07002558 case PP_POP:
H. Peter Anvine2c80182005-01-15 22:15:51 +00002559 tline = tline->next;
2560 skip_white_(tline);
2561 tline = expand_id(tline);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002562 if (tline) {
2563 if (!tok_type_(tline, TOK_ID)) {
2564 error(ERR_NONFATAL, "`%s' expects a context identifier",
2565 pp_directives[i]);
2566 free_tlist(origline);
2567 return DIRECTIVE_FOUND; /* but we did _something_ */
2568 }
2569 if (tline->next)
2570 error(ERR_WARNING|ERR_PASS1,
2571 "trailing garbage after `%s' ignored",
2572 pp_directives[i]);
2573 p = nasm_strdup(tline->text);
2574 } else {
2575 p = NULL; /* Anonymous */
2576 }
H. Peter Anvin42b56392008-10-24 16:24:21 -07002577
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002578 if (i == PP_PUSH) {
2579 ctx = nasm_malloc(sizeof(Context));
2580 ctx->next = cstk;
2581 hash_init(&ctx->localmac, HASH_SMALL);
2582 ctx->name = p;
2583 ctx->number = unique++;
2584 cstk = ctx;
2585 } else {
2586 /* %pop or %repl */
2587 if (!cstk) {
2588 error(ERR_NONFATAL, "`%s': context stack is empty",
2589 pp_directives[i]);
2590 } else if (i == PP_POP) {
2591 if (p && (!cstk->name || nasm_stricmp(p, cstk->name)))
2592 error(ERR_NONFATAL, "`%%pop' in wrong context: %s, "
2593 "expected %s",
2594 cstk->name ? cstk->name : "anonymous", p);
2595 else
2596 ctx_pop();
2597 } else {
2598 /* i == PP_REPL */
2599 nasm_free(cstk->name);
2600 cstk->name = p;
2601 p = NULL;
2602 }
2603 nasm_free(p);
2604 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002605 free_tlist(origline);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002606 return DIRECTIVE_FOUND;
H. Peter Anvin8e3f75e2008-09-24 00:21:58 -07002607 case PP_FATAL:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002608 severity = ERR_FATAL;
2609 goto issue_error;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002610 case PP_ERROR:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002611 severity = ERR_NONFATAL;
2612 goto issue_error;
H. Peter Anvin7df04172008-06-10 18:27:38 -07002613 case PP_WARNING:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002614 severity = ERR_WARNING|ERR_WARN_USER;
2615 goto issue_error;
H. Peter Anvin8e3f75e2008-09-24 00:21:58 -07002616
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002617issue_error:
H. Peter Anvin7df04172008-06-10 18:27:38 -07002618 {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002619 /* Only error out if this is the final pass */
2620 if (pass != 2 && i != PP_FATAL)
2621 return DIRECTIVE_FOUND;
2622
2623 tline->next = expand_smacro(tline->next);
2624 tline = tline->next;
2625 skip_white_(tline);
2626 t = tline ? tline->next : NULL;
2627 skip_white_(t);
2628 if (tok_type_(tline, TOK_STRING) && !t) {
2629 /* The line contains only a quoted string */
2630 p = tline->text;
2631 nasm_unquote(p, NULL); /* Ignore NUL character truncation */
2632 error(severity, "%s", p);
2633 } else {
2634 /* Not a quoted string, or more than a quoted string */
2635 p = detoken(tline, false);
2636 error(severity, "%s", p);
2637 nasm_free(p);
2638 }
2639 free_tlist(origline);
2640 return DIRECTIVE_FOUND;
H. Peter Anvin7df04172008-06-10 18:27:38 -07002641 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00002642
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002643 CASE_PP_IF:
H. Peter Anvine2c80182005-01-15 22:15:51 +00002644 if (istk->conds && !emitting(istk->conds->state))
2645 j = COND_NEVER;
2646 else {
2647 j = if_condition(tline->next, i);
2648 tline->next = NULL; /* it got freed */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002649 j = j < 0 ? COND_NEVER : j ? COND_IF_TRUE : COND_IF_FALSE;
2650 }
2651 cond = nasm_malloc(sizeof(Cond));
2652 cond->next = istk->conds;
2653 cond->state = j;
2654 istk->conds = cond;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002655 if(istk->mstk)
Keith Kanios3c0d91f2009-10-25 13:28:03 -05002656 istk->mstk->condcnt ++;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002657 free_tlist(origline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002658 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002659
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002660 CASE_PP_ELIF:
H. Peter Anvine2c80182005-01-15 22:15:51 +00002661 if (!istk->conds)
H. Peter Anvin4169a472007-09-12 01:29:43 +00002662 error(ERR_FATAL, "`%s': no matching `%%if'", pp_directives[i]);
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002663 switch(istk->conds->state) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002664 case COND_IF_TRUE:
2665 istk->conds->state = COND_DONE;
2666 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002667
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002668 case COND_DONE:
2669 case COND_NEVER:
2670 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002671
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002672 case COND_ELSE_TRUE:
2673 case COND_ELSE_FALSE:
2674 error_precond(ERR_WARNING|ERR_PASS1,
2675 "`%%elif' after `%%else' ignored");
2676 istk->conds->state = COND_NEVER;
2677 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002678
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002679 case COND_IF_FALSE:
2680 /*
2681 * IMPORTANT: In the case of %if, we will already have
2682 * called expand_mmac_params(); however, if we're
2683 * processing an %elif we must have been in a
2684 * non-emitting mode, which would have inhibited
2685 * the normal invocation of expand_mmac_params().
2686 * Therefore, we have to do it explicitly here.
2687 */
2688 j = if_condition(expand_mmac_params(tline->next), i);
2689 tline->next = NULL; /* it got freed */
2690 istk->conds->state =
2691 j < 0 ? COND_NEVER : j ? COND_IF_TRUE : COND_IF_FALSE;
2692 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002693 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002694 free_tlist(origline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002695 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002696
H. Peter Anvine2c80182005-01-15 22:15:51 +00002697 case PP_ELSE:
2698 if (tline->next)
H. Peter Anvin917a3492008-09-24 09:14:49 -07002699 error_precond(ERR_WARNING|ERR_PASS1,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002700 "trailing garbage after `%%else' ignored");
H. Peter Anvine2c80182005-01-15 22:15:51 +00002701 if (!istk->conds)
2702 error(ERR_FATAL, "`%%else': no matching `%%if'");
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002703 switch(istk->conds->state) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002704 case COND_IF_TRUE:
2705 case COND_DONE:
2706 istk->conds->state = COND_ELSE_FALSE;
2707 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002708
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002709 case COND_NEVER:
2710 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002711
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002712 case COND_IF_FALSE:
2713 istk->conds->state = COND_ELSE_TRUE;
2714 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002715
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002716 case COND_ELSE_TRUE:
2717 case COND_ELSE_FALSE:
2718 error_precond(ERR_WARNING|ERR_PASS1,
2719 "`%%else' after `%%else' ignored.");
2720 istk->conds->state = COND_NEVER;
2721 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002722 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002723 free_tlist(origline);
2724 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002725
H. Peter Anvine2c80182005-01-15 22:15:51 +00002726 case PP_ENDIF:
2727 if (tline->next)
H. Peter Anvin917a3492008-09-24 09:14:49 -07002728 error_precond(ERR_WARNING|ERR_PASS1,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002729 "trailing garbage after `%%endif' ignored");
H. Peter Anvine2c80182005-01-15 22:15:51 +00002730 if (!istk->conds)
2731 error(ERR_FATAL, "`%%endif': no matching `%%if'");
2732 cond = istk->conds;
2733 istk->conds = cond->next;
2734 nasm_free(cond);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002735 if(istk->mstk)
Keith Kanios3c0d91f2009-10-25 13:28:03 -05002736 istk->mstk->condcnt --;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002737 free_tlist(origline);
2738 return DIRECTIVE_FOUND;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002739
H. Peter Anvindb8f96e2009-07-15 09:07:29 -04002740 case PP_RMACRO:
2741 case PP_IRMACRO:
H. Peter Anvine2c80182005-01-15 22:15:51 +00002742 case PP_MACRO:
2743 case PP_IMACRO:
H. Peter Anvina26433d2008-07-16 14:40:01 -07002744 if (defining) {
H. Peter Anvindb8f96e2009-07-15 09:07:29 -04002745 error(ERR_FATAL, "`%s': already defining a macro",
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002746 pp_directives[i]);
2747 return DIRECTIVE_FOUND;
2748 }
2749 defining = nasm_malloc(sizeof(MMacro));
2750 defining->max_depth =
2751 (i == PP_RMACRO) || (i == PP_IRMACRO) ? DEADMAN_LIMIT : 0;
2752 defining->casesense = (i == PP_MACRO) || (i == PP_RMACRO);
2753 if (!parse_mmacro_spec(tline, defining, pp_directives[i])) {
2754 nasm_free(defining);
2755 defining = NULL;
2756 return DIRECTIVE_FOUND;
2757 }
H. Peter Anvina26433d2008-07-16 14:40:01 -07002758
H. Peter Anvin166c2472008-05-28 12:28:58 -07002759 mmac = (MMacro *) hash_findix(&mmacros, defining->name);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002760 while (mmac) {
2761 if (!strcmp(mmac->name, defining->name) &&
2762 (mmac->nparam_min <= defining->nparam_max
2763 || defining->plus)
2764 && (defining->nparam_min <= mmac->nparam_max
2765 || mmac->plus)) {
H. Peter Anvin917a3492008-09-24 09:14:49 -07002766 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +00002767 "redefining multi-line macro `%s'", defining->name);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002768 return DIRECTIVE_FOUND;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002769 }
2770 mmac = mmac->next;
2771 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002772 free_tlist(origline);
2773 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002774
H. Peter Anvine2c80182005-01-15 22:15:51 +00002775 case PP_ENDM:
2776 case PP_ENDMACRO:
Victor van den Elzen8f1120f2008-07-16 13:41:37 +02002777 if (! (defining && defining->name)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002778 error(ERR_NONFATAL, "`%s': not defining a macro", tline->text);
2779 return DIRECTIVE_FOUND;
2780 }
Keith Kanios852f1ee2009-07-12 00:19:55 -05002781 mmhead = (MMacro **) hash_findi_add(&mmacros, defining->name);
H. Peter Anvin97a23472007-09-16 17:57:25 -07002782 defining->next = *mmhead;
Keith Kanios852f1ee2009-07-12 00:19:55 -05002783 *mmhead = defining;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002784 defining = NULL;
2785 free_tlist(origline);
2786 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002787
H. Peter Anvin89cee572009-07-15 09:16:54 -04002788 case PP_EXITMACRO:
Keith Kanios852f1ee2009-07-12 00:19:55 -05002789 /*
2790 * We must search along istk->expansion until we hit a
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002791 * macro-end marker for a macro with a name. Then we
Keith Kanios3c0d91f2009-10-25 13:28:03 -05002792 * bypass all lines between exitmacro and endmacro.
Keith Kanios852f1ee2009-07-12 00:19:55 -05002793 */
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04002794 list_for_each(l, istk->expansion)
Keith Kanios852f1ee2009-07-12 00:19:55 -05002795 if (l->finishes && l->finishes->name)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002796 break;
Keith Kanios852f1ee2009-07-12 00:19:55 -05002797
2798 if (l) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002799 /*
2800 * Remove all conditional entries relative to this
2801 * macro invocation. (safe to do in this context)
2802 */
Keith Kanios3c0d91f2009-10-25 13:28:03 -05002803 for ( ; l->finishes->condcnt > 0; l->finishes->condcnt --) {
2804 cond = istk->conds;
2805 istk->conds = cond->next;
2806 nasm_free(cond);
2807 }
2808 istk->expansion = l;
Keith Kanios852f1ee2009-07-12 00:19:55 -05002809 } else {
2810 error(ERR_NONFATAL, "`%%exitmacro' not within `%%macro' block");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002811 }
Keith Kanios852f1ee2009-07-12 00:19:55 -05002812 free_tlist(origline);
2813 return DIRECTIVE_FOUND;
2814
H. Peter Anvina26433d2008-07-16 14:40:01 -07002815 case PP_UNMACRO:
2816 case PP_UNIMACRO:
2817 {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002818 MMacro **mmac_p;
2819 MMacro spec;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002820
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002821 spec.casesense = (i == PP_UNMACRO);
2822 if (!parse_mmacro_spec(tline, &spec, pp_directives[i])) {
2823 return DIRECTIVE_FOUND;
2824 }
H. Peter Anvina26433d2008-07-16 14:40:01 -07002825 mmac_p = (MMacro **) hash_findi(&mmacros, spec.name, NULL);
2826 while (mmac_p && *mmac_p) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002827 mmac = *mmac_p;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002828 if (mmac->casesense == spec.casesense &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002829 !mstrcmp(mmac->name, spec.name, spec.casesense) &&
H. Peter Anvina26433d2008-07-16 14:40:01 -07002830 mmac->nparam_min == spec.nparam_min &&
2831 mmac->nparam_max == spec.nparam_max &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002832 mmac->plus == spec.plus) {
2833 *mmac_p = mmac->next;
2834 free_mmacro(mmac);
2835 } else {
2836 mmac_p = &mmac->next;
2837 }
2838 }
2839 free_tlist(origline);
2840 free_tlist(spec.dlist);
2841 return DIRECTIVE_FOUND;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002842 }
2843
H. Peter Anvine2c80182005-01-15 22:15:51 +00002844 case PP_ROTATE:
2845 if (tline->next && tline->next->type == TOK_WHITESPACE)
2846 tline = tline->next;
H. Peter Anvin89cee572009-07-15 09:16:54 -04002847 if (!tline->next) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002848 free_tlist(origline);
2849 error(ERR_NONFATAL, "`%%rotate' missing rotate count");
2850 return DIRECTIVE_FOUND;
2851 }
2852 t = expand_smacro(tline->next);
2853 tline->next = NULL;
2854 free_tlist(origline);
2855 tline = t;
2856 tptr = &t;
2857 tokval.t_type = TOKEN_INVALID;
2858 evalresult =
2859 evaluate(ppscan, tptr, &tokval, NULL, pass, error, NULL);
2860 free_tlist(tline);
2861 if (!evalresult)
2862 return DIRECTIVE_FOUND;
2863 if (tokval.t_type)
H. Peter Anvin917a3492008-09-24 09:14:49 -07002864 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +00002865 "trailing garbage after expression ignored");
2866 if (!is_simple(evalresult)) {
2867 error(ERR_NONFATAL, "non-constant value given to `%%rotate'");
2868 return DIRECTIVE_FOUND;
2869 }
2870 mmac = istk->mstk;
2871 while (mmac && !mmac->name) /* avoid mistaking %reps for macros */
2872 mmac = mmac->next_active;
2873 if (!mmac) {
2874 error(ERR_NONFATAL, "`%%rotate' invoked outside a macro call");
2875 } else if (mmac->nparam == 0) {
2876 error(ERR_NONFATAL,
2877 "`%%rotate' invoked within macro without parameters");
2878 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002879 int rotate = mmac->rotate + reloc_value(evalresult);
H. Peter Anvin734b1882002-04-30 21:01:08 +00002880
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002881 rotate %= (int)mmac->nparam;
2882 if (rotate < 0)
2883 rotate += mmac->nparam;
H. Peter Anvin70653092007-10-19 14:42:29 -07002884
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002885 mmac->rotate = rotate;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002886 }
2887 return DIRECTIVE_FOUND;
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00002888
H. Peter Anvine2c80182005-01-15 22:15:51 +00002889 case PP_REP:
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002890 nolist = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002891 do {
2892 tline = tline->next;
2893 } while (tok_type_(tline, TOK_WHITESPACE));
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00002894
H. Peter Anvine2c80182005-01-15 22:15:51 +00002895 if (tok_type_(tline, TOK_ID) &&
2896 nasm_stricmp(tline->text, ".nolist") == 0) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002897 nolist = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002898 do {
2899 tline = tline->next;
2900 } while (tok_type_(tline, TOK_WHITESPACE));
2901 }
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00002902
H. Peter Anvine2c80182005-01-15 22:15:51 +00002903 if (tline) {
2904 t = expand_smacro(tline);
2905 tptr = &t;
2906 tokval.t_type = TOKEN_INVALID;
2907 evalresult =
2908 evaluate(ppscan, tptr, &tokval, NULL, pass, error, NULL);
2909 if (!evalresult) {
2910 free_tlist(origline);
2911 return DIRECTIVE_FOUND;
2912 }
2913 if (tokval.t_type)
H. Peter Anvin917a3492008-09-24 09:14:49 -07002914 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +00002915 "trailing garbage after expression ignored");
2916 if (!is_simple(evalresult)) {
2917 error(ERR_NONFATAL, "non-constant value given to `%%rep'");
2918 return DIRECTIVE_FOUND;
2919 }
Cyrill Gorcunove091d6e2010-08-09 13:58:22 +04002920 count = reloc_value(evalresult);
2921 if (count >= REP_LIMIT) {
Cyrill Gorcunov71f4f842010-08-09 20:17:17 +04002922 error(ERR_NONFATAL, "`%%rep' value exceeds limit");
Cyrill Gorcunove091d6e2010-08-09 13:58:22 +04002923 count = 0;
2924 } else
2925 count++;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002926 } else {
2927 error(ERR_NONFATAL, "`%%rep' expects a repeat count");
H. Peter Anvinf8ba53e2007-10-11 10:11:57 -07002928 count = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002929 }
2930 free_tlist(origline);
H. Peter Anvin734b1882002-04-30 21:01:08 +00002931
H. Peter Anvine2c80182005-01-15 22:15:51 +00002932 tmp_defining = defining;
2933 defining = nasm_malloc(sizeof(MMacro));
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002934 defining->prev = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002935 defining->name = NULL; /* flags this macro as a %rep block */
H. Peter Anvin70055962007-10-11 00:05:31 -07002936 defining->casesense = false;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002937 defining->plus = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002938 defining->nolist = nolist;
H. Peter Anvinf8ba53e2007-10-11 10:11:57 -07002939 defining->in_progress = count;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002940 defining->max_depth = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002941 defining->nparam_min = defining->nparam_max = 0;
2942 defining->defaults = NULL;
2943 defining->dlist = NULL;
2944 defining->expansion = NULL;
2945 defining->next_active = istk->mstk;
2946 defining->rep_nest = tmp_defining;
2947 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002948
H. Peter Anvine2c80182005-01-15 22:15:51 +00002949 case PP_ENDREP:
2950 if (!defining || defining->name) {
2951 error(ERR_NONFATAL, "`%%endrep': no matching `%%rep'");
2952 return DIRECTIVE_FOUND;
2953 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00002954
H. Peter Anvine2c80182005-01-15 22:15:51 +00002955 /*
2956 * Now we have a "macro" defined - although it has no name
2957 * and we won't be entering it in the hash tables - we must
2958 * push a macro-end marker for it on to istk->expansion.
2959 * After that, it will take care of propagating itself (a
2960 * macro-end marker line for a macro which is really a %rep
2961 * block will cause the macro to be re-expanded, complete
2962 * with another macro-end marker to ensure the process
2963 * continues) until the whole expansion is forcibly removed
2964 * from istk->expansion by a %exitrep.
2965 */
2966 l = nasm_malloc(sizeof(Line));
2967 l->next = istk->expansion;
2968 l->finishes = defining;
2969 l->first = NULL;
2970 istk->expansion = l;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002971
H. Peter Anvine2c80182005-01-15 22:15:51 +00002972 istk->mstk = defining;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002973
H. Peter Anvine2c80182005-01-15 22:15:51 +00002974 list->uplevel(defining->nolist ? LIST_MACRO_NOLIST : LIST_MACRO);
2975 tmp_defining = defining;
2976 defining = defining->rep_nest;
2977 free_tlist(origline);
2978 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002979
H. Peter Anvine2c80182005-01-15 22:15:51 +00002980 case PP_EXITREP:
2981 /*
2982 * We must search along istk->expansion until we hit a
2983 * macro-end marker for a macro with no name. Then we set
2984 * its `in_progress' flag to 0.
2985 */
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04002986 list_for_each(l, istk->expansion)
H. Peter Anvine2c80182005-01-15 22:15:51 +00002987 if (l->finishes && !l->finishes->name)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002988 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002989
H. Peter Anvine2c80182005-01-15 22:15:51 +00002990 if (l)
Charles Crayned4200be2008-07-12 16:42:33 -07002991 l->finishes->in_progress = 1;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002992 else
2993 error(ERR_NONFATAL, "`%%exitrep' not within `%%rep' block");
2994 free_tlist(origline);
2995 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002996
H. Peter Anvine2c80182005-01-15 22:15:51 +00002997 case PP_XDEFINE:
2998 case PP_IXDEFINE:
2999 case PP_DEFINE:
3000 case PP_IDEFINE:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003001 casesense = (i == PP_DEFINE || i == PP_XDEFINE);
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07003002
H. Peter Anvine2c80182005-01-15 22:15:51 +00003003 tline = tline->next;
3004 skip_white_(tline);
3005 tline = expand_id(tline);
3006 if (!tline || (tline->type != TOK_ID &&
3007 (tline->type != TOK_PREPROC_ID ||
3008 tline->text[1] != '$'))) {
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07003009 error(ERR_NONFATAL, "`%s' expects a macro identifier",
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003010 pp_directives[i]);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003011 free_tlist(origline);
3012 return DIRECTIVE_FOUND;
3013 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003014
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08003015 ctx = get_ctx(tline->text, &mname, false);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003016 last = tline;
3017 param_start = tline = tline->next;
3018 nparam = 0;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003019
H. Peter Anvine2c80182005-01-15 22:15:51 +00003020 /* Expand the macro definition now for %xdefine and %ixdefine */
3021 if ((i == PP_XDEFINE) || (i == PP_IXDEFINE))
3022 tline = expand_smacro(tline);
H. Peter Anvin734b1882002-04-30 21:01:08 +00003023
H. Peter Anvine2c80182005-01-15 22:15:51 +00003024 if (tok_is_(tline, "(")) {
3025 /*
3026 * This macro has parameters.
3027 */
H. Peter Anvin734b1882002-04-30 21:01:08 +00003028
H. Peter Anvine2c80182005-01-15 22:15:51 +00003029 tline = tline->next;
3030 while (1) {
3031 skip_white_(tline);
3032 if (!tline) {
3033 error(ERR_NONFATAL, "parameter identifier expected");
3034 free_tlist(origline);
3035 return DIRECTIVE_FOUND;
3036 }
3037 if (tline->type != TOK_ID) {
3038 error(ERR_NONFATAL,
3039 "`%s': parameter identifier expected",
3040 tline->text);
3041 free_tlist(origline);
3042 return DIRECTIVE_FOUND;
3043 }
3044 tline->type = TOK_SMAC_PARAM + nparam++;
3045 tline = tline->next;
3046 skip_white_(tline);
3047 if (tok_is_(tline, ",")) {
3048 tline = tline->next;
H. Peter Anvinca348b62008-07-23 10:49:26 -04003049 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003050 if (!tok_is_(tline, ")")) {
3051 error(ERR_NONFATAL,
3052 "`)' expected to terminate macro template");
3053 free_tlist(origline);
3054 return DIRECTIVE_FOUND;
3055 }
3056 break;
3057 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00003058 }
3059 last = tline;
3060 tline = tline->next;
3061 }
3062 if (tok_type_(tline, TOK_WHITESPACE))
3063 last = tline, tline = tline->next;
3064 macro_start = NULL;
3065 last->next = NULL;
3066 t = tline;
3067 while (t) {
3068 if (t->type == TOK_ID) {
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04003069 list_for_each(tt, param_start)
H. Peter Anvine2c80182005-01-15 22:15:51 +00003070 if (tt->type >= TOK_SMAC_PARAM &&
3071 !strcmp(tt->text, t->text))
3072 t->type = tt->type;
3073 }
3074 tt = t->next;
3075 t->next = macro_start;
3076 macro_start = t;
3077 t = tt;
3078 }
3079 /*
3080 * Good. We now have a macro name, a parameter count, and a
3081 * token list (in reverse order) for an expansion. We ought
3082 * to be OK just to create an SMacro, store it, and let
3083 * free_tlist have the rest of the line (which we have
3084 * carefully re-terminated after chopping off the expansion
3085 * from the end).
3086 */
H. Peter Anvin4db5a162007-10-11 13:42:09 -07003087 define_smacro(ctx, mname, casesense, nparam, macro_start);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003088 free_tlist(origline);
3089 return DIRECTIVE_FOUND;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003090
H. Peter Anvine2c80182005-01-15 22:15:51 +00003091 case PP_UNDEF:
3092 tline = tline->next;
3093 skip_white_(tline);
3094 tline = expand_id(tline);
3095 if (!tline || (tline->type != TOK_ID &&
3096 (tline->type != TOK_PREPROC_ID ||
3097 tline->text[1] != '$'))) {
3098 error(ERR_NONFATAL, "`%%undef' expects a macro identifier");
3099 free_tlist(origline);
3100 return DIRECTIVE_FOUND;
3101 }
3102 if (tline->next) {
H. Peter Anvin917a3492008-09-24 09:14:49 -07003103 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +00003104 "trailing garbage after macro name ignored");
3105 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003106
H. Peter Anvine2c80182005-01-15 22:15:51 +00003107 /* Find the context that symbol belongs to */
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08003108 ctx = get_ctx(tline->text, &mname, false);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003109 undef_smacro(ctx, mname);
3110 free_tlist(origline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003111 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003112
H. Peter Anvin9e200162008-06-04 17:23:14 -07003113 case PP_DEFSTR:
3114 case PP_IDEFSTR:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003115 casesense = (i == PP_DEFSTR);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003116
3117 tline = tline->next;
3118 skip_white_(tline);
3119 tline = expand_id(tline);
3120 if (!tline || (tline->type != TOK_ID &&
3121 (tline->type != TOK_PREPROC_ID ||
3122 tline->text[1] != '$'))) {
3123 error(ERR_NONFATAL, "`%s' expects a macro identifier",
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003124 pp_directives[i]);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003125 free_tlist(origline);
3126 return DIRECTIVE_FOUND;
3127 }
3128
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08003129 ctx = get_ctx(tline->text, &mname, false);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003130 last = tline;
3131 tline = expand_smacro(tline->next);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003132 last->next = NULL;
H. Peter Anvin9e200162008-06-04 17:23:14 -07003133
3134 while (tok_type_(tline, TOK_WHITESPACE))
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003135 tline = delete_Token(tline);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003136
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003137 p = detoken(tline, false);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003138 macro_start = nasm_malloc(sizeof(*macro_start));
3139 macro_start->next = NULL;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003140 macro_start->text = nasm_quote(p, strlen(p));
3141 macro_start->type = TOK_STRING;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003142 macro_start->a.mac = NULL;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003143 nasm_free(p);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003144
3145 /*
3146 * We now have a macro name, an implicit parameter count of
3147 * zero, and a string token to use as an expansion. Create
3148 * and store an SMacro.
3149 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003150 define_smacro(ctx, mname, casesense, 0, macro_start);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003151 free_tlist(origline);
3152 return DIRECTIVE_FOUND;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003153
H. Peter Anvin2f55bda2009-07-14 15:04:04 -04003154 case PP_DEFTOK:
3155 case PP_IDEFTOK:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003156 casesense = (i == PP_DEFTOK);
3157
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003158 tline = tline->next;
3159 skip_white_(tline);
3160 tline = expand_id(tline);
3161 if (!tline || (tline->type != TOK_ID &&
3162 (tline->type != TOK_PREPROC_ID ||
3163 tline->text[1] != '$'))) {
3164 error(ERR_NONFATAL,
3165 "`%s' expects a macro identifier as first parameter",
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003166 pp_directives[i]);
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003167 free_tlist(origline);
3168 return DIRECTIVE_FOUND;
3169 }
3170 ctx = get_ctx(tline->text, &mname, false);
3171 last = tline;
3172 tline = expand_smacro(tline->next);
3173 last->next = NULL;
3174
3175 t = tline;
3176 while (tok_type_(t, TOK_WHITESPACE))
3177 t = t->next;
3178 /* t should now point to the string */
Cyrill Gorcunov6908e582010-09-06 19:36:15 +04003179 if (!tok_type_(t, TOK_STRING)) {
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003180 error(ERR_NONFATAL,
3181 "`%s` requires string as second parameter",
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003182 pp_directives[i]);
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003183 free_tlist(tline);
3184 free_tlist(origline);
3185 return DIRECTIVE_FOUND;
3186 }
3187
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003188 nasm_unquote_cstr(t->text, i);
3189 macro_start = tokenize(t->text);
3190
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003191 /*
3192 * We now have a macro name, an implicit parameter count of
3193 * zero, and a numeric token to use as an expansion. Create
3194 * and store an SMacro.
3195 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003196 define_smacro(ctx, mname, casesense, 0, macro_start);
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003197 free_tlist(tline);
3198 free_tlist(origline);
3199 return DIRECTIVE_FOUND;
H. Peter Anvin9e200162008-06-04 17:23:14 -07003200
H. Peter Anvin418ca702008-05-30 10:42:30 -07003201 case PP_PATHSEARCH:
3202 {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003203 FILE *fp;
3204 StrList *xsl = NULL;
3205 StrList **xst = &xsl;
H. Peter Anvin418ca702008-05-30 10:42:30 -07003206
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003207 casesense = true;
H. Peter Anvin418ca702008-05-30 10:42:30 -07003208
3209 tline = tline->next;
3210 skip_white_(tline);
3211 tline = expand_id(tline);
3212 if (!tline || (tline->type != TOK_ID &&
3213 (tline->type != TOK_PREPROC_ID ||
3214 tline->text[1] != '$'))) {
3215 error(ERR_NONFATAL,
3216 "`%%pathsearch' expects a macro identifier as first parameter");
3217 free_tlist(origline);
3218 return DIRECTIVE_FOUND;
3219 }
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08003220 ctx = get_ctx(tline->text, &mname, false);
H. Peter Anvin418ca702008-05-30 10:42:30 -07003221 last = tline;
3222 tline = expand_smacro(tline->next);
3223 last->next = NULL;
3224
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003225 t = tline;
H. Peter Anvin418ca702008-05-30 10:42:30 -07003226 while (tok_type_(t, TOK_WHITESPACE))
3227 t = t->next;
3228
3229 if (!t || (t->type != TOK_STRING &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003230 t->type != TOK_INTERNAL_STRING)) {
H. Peter Anvin418ca702008-05-30 10:42:30 -07003231 error(ERR_NONFATAL, "`%%pathsearch' expects a file name");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003232 free_tlist(tline);
H. Peter Anvin418ca702008-05-30 10:42:30 -07003233 free_tlist(origline);
3234 return DIRECTIVE_FOUND; /* but we did _something_ */
3235 }
3236 if (t->next)
H. Peter Anvin917a3492008-09-24 09:14:49 -07003237 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvin418ca702008-05-30 10:42:30 -07003238 "trailing garbage after `%%pathsearch' ignored");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003239 p = t->text;
H. Peter Anvin427cc912008-06-01 21:43:03 -07003240 if (t->type != TOK_INTERNAL_STRING)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003241 nasm_unquote(p, NULL);
H. Peter Anvin418ca702008-05-30 10:42:30 -07003242
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003243 fp = inc_fopen(p, &xsl, &xst, true);
3244 if (fp) {
3245 p = xsl->str;
3246 fclose(fp); /* Don't actually care about the file */
3247 }
H. Peter Anvin418ca702008-05-30 10:42:30 -07003248 macro_start = nasm_malloc(sizeof(*macro_start));
3249 macro_start->next = NULL;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003250 macro_start->text = nasm_quote(p, strlen(p));
3251 macro_start->type = TOK_STRING;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003252 macro_start->a.mac = NULL;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003253 if (xsl)
3254 nasm_free(xsl);
H. Peter Anvin418ca702008-05-30 10:42:30 -07003255
3256 /*
3257 * We now have a macro name, an implicit parameter count of
3258 * zero, and a string token to use as an expansion. Create
3259 * and store an SMacro.
3260 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003261 define_smacro(ctx, mname, casesense, 0, macro_start);
3262 free_tlist(tline);
H. Peter Anvin418ca702008-05-30 10:42:30 -07003263 free_tlist(origline);
3264 return DIRECTIVE_FOUND;
3265 }
3266
H. Peter Anvine2c80182005-01-15 22:15:51 +00003267 case PP_STRLEN:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003268 casesense = true;
H. Peter Anvin70653092007-10-19 14:42:29 -07003269
H. Peter Anvine2c80182005-01-15 22:15:51 +00003270 tline = tline->next;
3271 skip_white_(tline);
3272 tline = expand_id(tline);
3273 if (!tline || (tline->type != TOK_ID &&
3274 (tline->type != TOK_PREPROC_ID ||
3275 tline->text[1] != '$'))) {
3276 error(ERR_NONFATAL,
3277 "`%%strlen' expects a macro identifier as first parameter");
3278 free_tlist(origline);
3279 return DIRECTIVE_FOUND;
3280 }
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08003281 ctx = get_ctx(tline->text, &mname, false);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003282 last = tline;
3283 tline = expand_smacro(tline->next);
3284 last->next = NULL;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003285
H. Peter Anvine2c80182005-01-15 22:15:51 +00003286 t = tline;
3287 while (tok_type_(t, TOK_WHITESPACE))
3288 t = t->next;
3289 /* t should now point to the string */
Cyrill Gorcunov4e1d5ab2010-07-23 18:51:51 +04003290 if (!tok_type_(t, TOK_STRING)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00003291 error(ERR_NONFATAL,
3292 "`%%strlen` requires string as second parameter");
3293 free_tlist(tline);
3294 free_tlist(origline);
3295 return DIRECTIVE_FOUND;
3296 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003297
H. Peter Anvine2c80182005-01-15 22:15:51 +00003298 macro_start = nasm_malloc(sizeof(*macro_start));
3299 macro_start->next = NULL;
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07003300 make_tok_num(macro_start, nasm_unquote(t->text, NULL));
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003301 macro_start->a.mac = NULL;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003302
H. Peter Anvine2c80182005-01-15 22:15:51 +00003303 /*
3304 * We now have a macro name, an implicit parameter count of
3305 * zero, and a numeric token to use as an expansion. Create
3306 * and store an SMacro.
3307 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003308 define_smacro(ctx, mname, casesense, 0, macro_start);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003309 free_tlist(tline);
3310 free_tlist(origline);
3311 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003312
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003313 case PP_STRCAT:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003314 casesense = true;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003315
3316 tline = tline->next;
3317 skip_white_(tline);
3318 tline = expand_id(tline);
3319 if (!tline || (tline->type != TOK_ID &&
3320 (tline->type != TOK_PREPROC_ID ||
3321 tline->text[1] != '$'))) {
3322 error(ERR_NONFATAL,
3323 "`%%strcat' expects a macro identifier as first parameter");
3324 free_tlist(origline);
3325 return DIRECTIVE_FOUND;
3326 }
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08003327 ctx = get_ctx(tline->text, &mname, false);
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003328 last = tline;
3329 tline = expand_smacro(tline->next);
3330 last->next = NULL;
3331
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003332 len = 0;
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04003333 list_for_each(t, tline) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003334 switch (t->type) {
3335 case TOK_WHITESPACE:
3336 break;
3337 case TOK_STRING:
3338 len += t->a.len = nasm_unquote(t->text, NULL);
3339 break;
3340 case TOK_OTHER:
3341 if (!strcmp(t->text, ",")) /* permit comma separators */
3342 break;
3343 /* else fall through */
3344 default:
3345 error(ERR_NONFATAL,
3346 "non-string passed to `%%strcat' (%d)", t->type);
3347 free_tlist(tline);
3348 free_tlist(origline);
3349 return DIRECTIVE_FOUND;
3350 }
3351 }
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003352
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003353 p = pp = nasm_malloc(len);
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04003354 list_for_each(t, tline) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003355 if (t->type == TOK_STRING) {
3356 memcpy(p, t->text, t->a.len);
3357 p += t->a.len;
3358 }
3359 }
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003360
3361 /*
3362 * We now have a macro name, an implicit parameter count of
3363 * zero, and a numeric token to use as an expansion. Create
3364 * and store an SMacro.
3365 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003366 macro_start = new_Token(NULL, TOK_STRING, NULL, 0);
3367 macro_start->text = nasm_quote(pp, len);
3368 nasm_free(pp);
3369 define_smacro(ctx, mname, casesense, 0, macro_start);
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003370 free_tlist(tline);
3371 free_tlist(origline);
3372 return DIRECTIVE_FOUND;
3373
H. Peter Anvine2c80182005-01-15 22:15:51 +00003374 case PP_SUBSTR:
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003375 {
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003376 int64_t start, count;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003377 size_t len;
H. Peter Anvind2456592008-06-19 15:04:18 -07003378
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003379 casesense = true;
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07003380
H. Peter Anvine2c80182005-01-15 22:15:51 +00003381 tline = tline->next;
3382 skip_white_(tline);
3383 tline = expand_id(tline);
3384 if (!tline || (tline->type != TOK_ID &&
3385 (tline->type != TOK_PREPROC_ID ||
3386 tline->text[1] != '$'))) {
3387 error(ERR_NONFATAL,
3388 "`%%substr' expects a macro identifier as first parameter");
3389 free_tlist(origline);
3390 return DIRECTIVE_FOUND;
3391 }
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08003392 ctx = get_ctx(tline->text, &mname, false);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003393 last = tline;
3394 tline = expand_smacro(tline->next);
3395 last->next = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003396
Cyrill Gorcunov35519d62010-09-06 23:49:52 +04003397 if (tline) /* skip expanded id */
3398 t = tline->next;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003399 while (tok_type_(t, TOK_WHITESPACE))
3400 t = t->next;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003401
H. Peter Anvine2c80182005-01-15 22:15:51 +00003402 /* t should now point to the string */
Cyrill Gorcunov35519d62010-09-06 23:49:52 +04003403 if (!tok_type_(t, TOK_STRING)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00003404 error(ERR_NONFATAL,
3405 "`%%substr` requires string as second parameter");
3406 free_tlist(tline);
3407 free_tlist(origline);
3408 return DIRECTIVE_FOUND;
3409 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003410
H. Peter Anvine2c80182005-01-15 22:15:51 +00003411 tt = t->next;
3412 tptr = &tt;
3413 tokval.t_type = TOKEN_INVALID;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003414 evalresult = evaluate(ppscan, tptr, &tokval, NULL,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003415 pass, error, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003416 if (!evalresult) {
3417 free_tlist(tline);
3418 free_tlist(origline);
3419 return DIRECTIVE_FOUND;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003420 } else if (!is_simple(evalresult)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00003421 error(ERR_NONFATAL, "non-constant value given to `%%substr`");
3422 free_tlist(tline);
3423 free_tlist(origline);
3424 return DIRECTIVE_FOUND;
3425 }
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003426 start = evalresult->value - 1;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003427
3428 while (tok_type_(tt, TOK_WHITESPACE))
3429 tt = tt->next;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003430 if (!tt) {
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003431 count = 1; /* Backwards compatibility: one character */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003432 } else {
3433 tokval.t_type = TOKEN_INVALID;
3434 evalresult = evaluate(ppscan, tptr, &tokval, NULL,
3435 pass, error, NULL);
3436 if (!evalresult) {
3437 free_tlist(tline);
3438 free_tlist(origline);
3439 return DIRECTIVE_FOUND;
3440 } else if (!is_simple(evalresult)) {
3441 error(ERR_NONFATAL, "non-constant value given to `%%substr`");
3442 free_tlist(tline);
3443 free_tlist(origline);
3444 return DIRECTIVE_FOUND;
3445 }
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003446 count = evalresult->value;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003447 }
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003448
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003449 len = nasm_unquote(t->text, NULL);
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003450
Cyrill Gorcunovcff031e2010-09-07 20:31:11 +04003451 /* make start and count being in range */
3452 if (start < 0)
3453 start = 0;
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003454 if (count < 0)
3455 count = len + count + 1 - start;
3456 if (start + count > (int64_t)len)
Cyrill Gorcunovcff031e2010-09-07 20:31:11 +04003457 count = len - start;
3458 if (!len || count < 0 || start >=(int64_t)len)
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003459 start = -1, count = 0; /* empty string */
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003460
H. Peter Anvine2c80182005-01-15 22:15:51 +00003461 macro_start = nasm_malloc(sizeof(*macro_start));
3462 macro_start->next = NULL;
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003463 macro_start->text = nasm_quote((start < 0) ? "" : t->text + start, count);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003464 macro_start->type = TOK_STRING;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003465 macro_start->a.mac = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003466
H. Peter Anvine2c80182005-01-15 22:15:51 +00003467 /*
3468 * We now have a macro name, an implicit parameter count of
3469 * zero, and a numeric token to use as an expansion. Create
3470 * and store an SMacro.
3471 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003472 define_smacro(ctx, mname, casesense, 0, macro_start);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003473 free_tlist(tline);
3474 free_tlist(origline);
3475 return DIRECTIVE_FOUND;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003476 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003477
H. Peter Anvine2c80182005-01-15 22:15:51 +00003478 case PP_ASSIGN:
3479 case PP_IASSIGN:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003480 casesense = (i == PP_ASSIGN);
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07003481
H. Peter Anvine2c80182005-01-15 22:15:51 +00003482 tline = tline->next;
3483 skip_white_(tline);
3484 tline = expand_id(tline);
3485 if (!tline || (tline->type != TOK_ID &&
3486 (tline->type != TOK_PREPROC_ID ||
3487 tline->text[1] != '$'))) {
3488 error(ERR_NONFATAL,
3489 "`%%%sassign' expects a macro identifier",
3490 (i == PP_IASSIGN ? "i" : ""));
3491 free_tlist(origline);
3492 return DIRECTIVE_FOUND;
3493 }
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08003494 ctx = get_ctx(tline->text, &mname, false);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003495 last = tline;
3496 tline = expand_smacro(tline->next);
3497 last->next = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003498
H. Peter Anvine2c80182005-01-15 22:15:51 +00003499 t = tline;
3500 tptr = &t;
3501 tokval.t_type = TOKEN_INVALID;
3502 evalresult =
3503 evaluate(ppscan, tptr, &tokval, NULL, pass, error, NULL);
3504 free_tlist(tline);
3505 if (!evalresult) {
3506 free_tlist(origline);
3507 return DIRECTIVE_FOUND;
3508 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003509
H. Peter Anvine2c80182005-01-15 22:15:51 +00003510 if (tokval.t_type)
H. Peter Anvin917a3492008-09-24 09:14:49 -07003511 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +00003512 "trailing garbage after expression ignored");
H. Peter Anvin734b1882002-04-30 21:01:08 +00003513
H. Peter Anvine2c80182005-01-15 22:15:51 +00003514 if (!is_simple(evalresult)) {
3515 error(ERR_NONFATAL,
3516 "non-constant value given to `%%%sassign'",
3517 (i == PP_IASSIGN ? "i" : ""));
3518 free_tlist(origline);
3519 return DIRECTIVE_FOUND;
3520 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003521
H. Peter Anvine2c80182005-01-15 22:15:51 +00003522 macro_start = nasm_malloc(sizeof(*macro_start));
3523 macro_start->next = NULL;
3524 make_tok_num(macro_start, reloc_value(evalresult));
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003525 macro_start->a.mac = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003526
H. Peter Anvine2c80182005-01-15 22:15:51 +00003527 /*
3528 * We now have a macro name, an implicit parameter count of
3529 * zero, and a numeric token to use as an expansion. Create
3530 * and store an SMacro.
3531 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003532 define_smacro(ctx, mname, casesense, 0, macro_start);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003533 free_tlist(origline);
3534 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003535
H. Peter Anvine2c80182005-01-15 22:15:51 +00003536 case PP_LINE:
3537 /*
3538 * Syntax is `%line nnn[+mmm] [filename]'
3539 */
3540 tline = tline->next;
3541 skip_white_(tline);
3542 if (!tok_type_(tline, TOK_NUMBER)) {
3543 error(ERR_NONFATAL, "`%%line' expects line number");
3544 free_tlist(origline);
3545 return DIRECTIVE_FOUND;
3546 }
H. Peter Anvin70055962007-10-11 00:05:31 -07003547 k = readnum(tline->text, &err);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003548 m = 1;
3549 tline = tline->next;
3550 if (tok_is_(tline, "+")) {
3551 tline = tline->next;
3552 if (!tok_type_(tline, TOK_NUMBER)) {
3553 error(ERR_NONFATAL, "`%%line' expects line increment");
3554 free_tlist(origline);
3555 return DIRECTIVE_FOUND;
3556 }
H. Peter Anvin70055962007-10-11 00:05:31 -07003557 m = readnum(tline->text, &err);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003558 tline = tline->next;
3559 }
3560 skip_white_(tline);
3561 src_set_linnum(k);
3562 istk->lineinc = m;
3563 if (tline) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07003564 nasm_free(src_set_fname(detoken(tline, false)));
H. Peter Anvine2c80182005-01-15 22:15:51 +00003565 }
3566 free_tlist(origline);
3567 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003568
H. Peter Anvine2c80182005-01-15 22:15:51 +00003569 default:
3570 error(ERR_FATAL,
3571 "preprocessor directive `%s' not yet implemented",
H. Peter Anvin4169a472007-09-12 01:29:43 +00003572 pp_directives[i]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003573 return DIRECTIVE_FOUND;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003574 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003575}
3576
3577/*
H. Peter Anvin76690a12002-04-30 20:52:49 +00003578 * Ensure that a macro parameter contains a condition code and
3579 * nothing else. Return the condition code index if so, or -1
3580 * otherwise.
3581 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003582static int find_cc(Token * t)
H. Peter Anvineba20a72002-04-30 20:53:55 +00003583{
H. Peter Anvin76690a12002-04-30 20:52:49 +00003584 Token *tt;
3585 int i, j, k, m;
3586
H. Peter Anvin25a99342007-09-22 17:45:45 -07003587 if (!t)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003588 return -1; /* Probably a %+ without a space */
H. Peter Anvin25a99342007-09-22 17:45:45 -07003589
H. Peter Anvineba20a72002-04-30 20:53:55 +00003590 skip_white_(t);
H. Peter Anvin76690a12002-04-30 20:52:49 +00003591 if (t->type != TOK_ID)
H. Peter Anvine2c80182005-01-15 22:15:51 +00003592 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003593 tt = t->next;
H. Peter Anvineba20a72002-04-30 20:53:55 +00003594 skip_white_(tt);
H. Peter Anvin76690a12002-04-30 20:52:49 +00003595 if (tt && (tt->type != TOK_OTHER || strcmp(tt->text, ",")))
H. Peter Anvine2c80182005-01-15 22:15:51 +00003596 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003597
3598 i = -1;
Cyrill Gorcunova7319242010-06-03 22:04:36 +04003599 j = ARRAY_SIZE(conditions);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003600 while (j - i > 1) {
3601 k = (j + i) / 2;
3602 m = nasm_stricmp(t->text, conditions[k]);
3603 if (m == 0) {
3604 i = k;
3605 j = -2;
3606 break;
3607 } else if (m < 0) {
3608 j = k;
3609 } else
3610 i = k;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003611 }
3612 if (j != -2)
H. Peter Anvine2c80182005-01-15 22:15:51 +00003613 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003614 return i;
3615}
3616
H. Peter Anvind784a082009-04-20 14:01:18 -07003617static bool paste_tokens(Token **head, bool handle_paste_tokens)
3618{
3619 Token **tail, *t, *tt;
3620 Token **paste_head;
3621 bool did_paste = false;
3622 char *tmp;
3623
3624 /* Now handle token pasting... */
3625 paste_head = NULL;
3626 tail = head;
3627 while ((t = *tail) && (tt = t->next)) {
3628 switch (t->type) {
3629 case TOK_WHITESPACE:
3630 if (tt->type == TOK_WHITESPACE) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003631 /* Zap adjacent whitespace tokens */
H. Peter Anvind784a082009-04-20 14:01:18 -07003632 t->next = delete_Token(tt);
3633 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003634 /* Do not advance paste_head here */
3635 tail = &t->next;
3636 }
H. Peter Anvind784a082009-04-20 14:01:18 -07003637 break;
3638 case TOK_ID:
H. Peter Anvind784a082009-04-20 14:01:18 -07003639 case TOK_NUMBER:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003640 case TOK_FLOAT:
3641 {
3642 size_t len = 0;
3643 char *tmp, *p;
H. Peter Anvind784a082009-04-20 14:01:18 -07003644
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003645 while (tt && (tt->type == TOK_ID || tt->type == TOK_PREPROC_ID ||
3646 tt->type == TOK_NUMBER || tt->type == TOK_FLOAT ||
3647 tt->type == TOK_OTHER)) {
3648 len += strlen(tt->text);
3649 tt = tt->next;
3650 }
H. Peter Anvind784a082009-04-20 14:01:18 -07003651
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003652 /*
3653 * Now tt points to the first token after
3654 * the potential paste area...
3655 */
3656 if (tt != t->next) {
3657 /* We have at least two tokens... */
3658 len += strlen(t->text);
3659 p = tmp = nasm_malloc(len+1);
H. Peter Anvind784a082009-04-20 14:01:18 -07003660
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003661 while (t != tt) {
3662 strcpy(p, t->text);
3663 p = strchr(p, '\0');
3664 t = delete_Token(t);
3665 }
H. Peter Anvind784a082009-04-20 14:01:18 -07003666
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003667 t = *tail = tokenize(tmp);
3668 nasm_free(tmp);
H. Peter Anvind784a082009-04-20 14:01:18 -07003669
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003670 while (t->next) {
3671 tail = &t->next;
3672 t = t->next;
3673 }
3674 t->next = tt; /* Attach the remaining token chain */
H. Peter Anvind784a082009-04-20 14:01:18 -07003675
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003676 did_paste = true;
3677 }
3678 paste_head = tail;
3679 tail = &t->next;
3680 break;
3681 }
3682 case TOK_PASTE: /* %+ */
3683 if (handle_paste_tokens) {
3684 /* Zap %+ and whitespace tokens to the right */
3685 while (t && (t->type == TOK_WHITESPACE ||
3686 t->type == TOK_PASTE))
3687 t = *tail = delete_Token(t);
3688 if (!paste_head || !t)
3689 break; /* Nothing to paste with */
3690 tail = paste_head;
3691 t = *tail;
3692 tt = t->next;
3693 while (tok_type_(tt, TOK_WHITESPACE))
3694 tt = t->next = delete_Token(tt);
H. Peter Anvind784a082009-04-20 14:01:18 -07003695
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003696 if (tt) {
3697 tmp = nasm_strcat(t->text, tt->text);
3698 delete_Token(t);
3699 tt = delete_Token(tt);
3700 t = *tail = tokenize(tmp);
3701 nasm_free(tmp);
3702 while (t->next) {
3703 tail = &t->next;
3704 t = t->next;
3705 }
3706 t->next = tt; /* Attach the remaining token chain */
3707 did_paste = true;
3708 }
3709 paste_head = tail;
3710 tail = &t->next;
3711 break;
3712 }
3713 /* else fall through */
3714 default:
Cyrill Gorcunovadabc152010-07-10 02:11:41 +04003715 tail = &t->next;
3716 if (!tok_type_(t->next, TOK_WHITESPACE))
3717 paste_head = tail;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003718 break;
H. Peter Anvind784a082009-04-20 14:01:18 -07003719 }
3720 }
3721 return did_paste;
3722}
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003723
3724/*
3725 * expands to a list of tokens from %{x:y}
3726 */
3727static Token *expand_mmac_params_range(MMacro *mac, Token *tline, Token ***last)
3728{
3729 Token *t = tline, **tt, *tm, *head;
3730 char *pos;
3731 int fst, lst, j, i;
3732
3733 pos = strchr(tline->text, ':');
3734 nasm_assert(pos);
3735
3736 lst = atoi(pos + 1);
3737 fst = atoi(tline->text + 1);
3738
3739 /*
3740 * only macros params are accounted so
3741 * if someone passes %0 -- we reject such
3742 * value(s)
3743 */
3744 if (lst == 0 || fst == 0)
3745 goto err;
3746
3747 /* the values should be sane */
Cyrill Gorcunov2f403752010-06-05 10:47:10 +04003748 if ((fst > (int)mac->nparam || fst < (-(int)mac->nparam)) ||
3749 (lst > (int)mac->nparam || lst < (-(int)mac->nparam)))
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003750 goto err;
3751
Cyrill Gorcunovefb35832010-06-20 01:52:19 +04003752 fst = fst < 0 ? fst + (int)mac->nparam + 1: fst;
3753 lst = lst < 0 ? lst + (int)mac->nparam + 1: lst;
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003754
3755 /* counted from zero */
3756 fst--, lst--;
3757
3758 /*
3759 * it will be at least one token
3760 */
3761 tm = mac->params[(fst + mac->rotate) % mac->nparam];
3762 t = new_Token(NULL, tm->type, tm->text, 0);
3763 head = t, tt = &t->next;
3764 if (fst < lst) {
3765 for (i = fst + 1; i <= lst; i++) {
3766 t = new_Token(NULL, TOK_OTHER, ",", 0);
3767 *tt = t, tt = &t->next;
3768 j = (i + mac->rotate) % mac->nparam;
3769 tm = mac->params[j];
3770 t = new_Token(NULL, tm->type, tm->text, 0);
3771 *tt = t, tt = &t->next;
3772 }
3773 } else {
3774 for (i = fst - 1; i >= lst; i--) {
3775 t = new_Token(NULL, TOK_OTHER, ",", 0);
3776 *tt = t, tt = &t->next;
3777 j = (i + mac->rotate) % mac->nparam;
3778 tm = mac->params[j];
3779 t = new_Token(NULL, tm->type, tm->text, 0);
3780 *tt = t, tt = &t->next;
3781 }
3782 }
3783
3784 *last = tt;
3785 return head;
3786
3787err:
3788 error(ERR_NONFATAL, "`%%{%s}': macro parameters out of range",
3789 &tline->text[1]);
3790 return tline;
3791}
3792
H. Peter Anvin76690a12002-04-30 20:52:49 +00003793/*
3794 * Expand MMacro-local things: parameter references (%0, %n, %+n,
H. Peter Anvin67c63722008-10-26 23:49:00 -07003795 * %-n) and MMacro-local identifiers (%%foo) as well as
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003796 * macro indirection (%[...]) and range (%{..:..}).
H. Peter Anvin76690a12002-04-30 20:52:49 +00003797 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003798static Token *expand_mmac_params(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00003799{
H. Peter Anvin734b1882002-04-30 21:01:08 +00003800 Token *t, *tt, **tail, *thead;
H. Peter Anvin6125b622009-04-08 14:02:25 -07003801 bool changed = false;
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003802 char *pos;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003803
3804 tail = &thead;
3805 thead = NULL;
3806
H. Peter Anvine2c80182005-01-15 22:15:51 +00003807 while (tline) {
3808 if (tline->type == TOK_PREPROC_ID &&
Cyrill Gorcunovca611192010-06-04 09:22:12 +04003809 (((tline->text[1] == '+' || tline->text[1] == '-') && tline->text[2]) ||
3810 (tline->text[1] >= '0' && tline->text[1] <= '9') ||
3811 tline->text[1] == '%')) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00003812 char *text = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003813 int type = 0, cc; /* type = 0 to placate optimisers */
Keith Kaniosa6dfa782007-04-13 16:47:53 +00003814 char tmpbuf[30];
H. Peter Anvin25a99342007-09-22 17:45:45 -07003815 unsigned int n;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003816 int i;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003817 MMacro *mac;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003818
H. Peter Anvine2c80182005-01-15 22:15:51 +00003819 t = tline;
3820 tline = tline->next;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003821
H. Peter Anvine2c80182005-01-15 22:15:51 +00003822 mac = istk->mstk;
3823 while (mac && !mac->name) /* avoid mistaking %reps for macros */
3824 mac = mac->next_active;
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003825 if (!mac) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00003826 error(ERR_NONFATAL, "`%s': not in a macro call", t->text);
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003827 } else {
3828 pos = strchr(t->text, ':');
3829 if (!pos) {
3830 switch (t->text[1]) {
3831 /*
3832 * We have to make a substitution of one of the
3833 * forms %1, %-1, %+1, %%foo, %0.
3834 */
3835 case '0':
3836 type = TOK_NUMBER;
3837 snprintf(tmpbuf, sizeof(tmpbuf), "%d", mac->nparam);
3838 text = nasm_strdup(tmpbuf);
3839 break;
3840 case '%':
H. Peter Anvine2c80182005-01-15 22:15:51 +00003841 type = TOK_ID;
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003842 snprintf(tmpbuf, sizeof(tmpbuf), "..@%"PRIu64".",
3843 mac->unique);
3844 text = nasm_strcat(tmpbuf, t->text + 2);
3845 break;
3846 case '-':
3847 n = atoi(t->text + 2) - 1;
3848 if (n >= mac->nparam)
3849 tt = NULL;
3850 else {
3851 if (mac->nparam > 1)
3852 n = (n + mac->rotate) % mac->nparam;
3853 tt = mac->params[n];
H. Peter Anvine2c80182005-01-15 22:15:51 +00003854 }
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003855 cc = find_cc(tt);
3856 if (cc == -1) {
3857 error(ERR_NONFATAL,
3858 "macro parameter %d is not a condition code",
3859 n + 1);
3860 text = NULL;
3861 } else {
3862 type = TOK_ID;
3863 if (inverse_ccs[cc] == -1) {
3864 error(ERR_NONFATAL,
3865 "condition code `%s' is not invertible",
3866 conditions[cc]);
3867 text = NULL;
3868 } else
3869 text = nasm_strdup(conditions[inverse_ccs[cc]]);
3870 }
3871 break;
3872 case '+':
3873 n = atoi(t->text + 2) - 1;
3874 if (n >= mac->nparam)
3875 tt = NULL;
3876 else {
3877 if (mac->nparam > 1)
3878 n = (n + mac->rotate) % mac->nparam;
3879 tt = mac->params[n];
3880 }
3881 cc = find_cc(tt);
3882 if (cc == -1) {
3883 error(ERR_NONFATAL,
3884 "macro parameter %d is not a condition code",
3885 n + 1);
3886 text = NULL;
3887 } else {
3888 type = TOK_ID;
3889 text = nasm_strdup(conditions[cc]);
3890 }
3891 break;
3892 default:
3893 n = atoi(t->text + 1) - 1;
3894 if (n >= mac->nparam)
3895 tt = NULL;
3896 else {
3897 if (mac->nparam > 1)
3898 n = (n + mac->rotate) % mac->nparam;
3899 tt = mac->params[n];
3900 }
3901 if (tt) {
3902 for (i = 0; i < mac->paramlen[n]; i++) {
3903 *tail = new_Token(NULL, tt->type, tt->text, 0);
3904 tail = &(*tail)->next;
3905 tt = tt->next;
3906 }
3907 }
3908 text = NULL; /* we've done it here */
3909 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003910 }
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003911 } else {
3912 /*
3913 * seems we have a parameters range here
3914 */
3915 Token *head, **last;
3916 head = expand_mmac_params_range(mac, t, &last);
3917 if (head != t) {
3918 *tail = head;
3919 *last = tline;
3920 tline = head;
3921 text = NULL;
3922 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00003923 }
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003924 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00003925 if (!text) {
3926 delete_Token(t);
3927 } else {
3928 *tail = t;
3929 tail = &t->next;
3930 t->type = type;
3931 nasm_free(t->text);
3932 t->text = text;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003933 t->a.mac = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003934 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003935 changed = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003936 continue;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003937 } else if (tline->type == TOK_INDIRECT) {
3938 t = tline;
3939 tline = tline->next;
3940 tt = tokenize(t->text);
3941 tt = expand_mmac_params(tt);
3942 tt = expand_smacro(tt);
3943 *tail = tt;
3944 while (tt) {
3945 tt->a.mac = NULL; /* Necessary? */
3946 tail = &tt->next;
3947 tt = tt->next;
3948 }
3949 delete_Token(t);
3950 changed = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003951 } else {
3952 t = *tail = tline;
3953 tline = tline->next;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003954 t->a.mac = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003955 tail = &t->next;
3956 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00003957 }
H. Peter Anvineba20a72002-04-30 20:53:55 +00003958 *tail = NULL;
H. Peter Anvin67c63722008-10-26 23:49:00 -07003959
H. Peter Anvind784a082009-04-20 14:01:18 -07003960 if (changed)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003961 paste_tokens(&thead, false);
H. Peter Anvin6125b622009-04-08 14:02:25 -07003962
H. Peter Anvin76690a12002-04-30 20:52:49 +00003963 return thead;
3964}
3965
3966/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003967 * Expand all single-line macro calls made in the given line.
3968 * Return the expanded version of the line. The original is deemed
3969 * to be destroyed in the process. (In reality we'll just move
3970 * Tokens from input to output a lot of the time, rather than
3971 * actually bothering to destroy and replicate.)
3972 */
H. Peter Anvincb1cf592007-11-19 12:26:50 -08003973
H. Peter Anvine2c80182005-01-15 22:15:51 +00003974static Token *expand_smacro(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00003975{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003976 Token *t, *tt, *mstart, **tail, *thead;
H. Peter Anvineba20a72002-04-30 20:53:55 +00003977 SMacro *head = NULL, *m;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003978 Token **params;
3979 int *paramsize;
H. Peter Anvin25a99342007-09-22 17:45:45 -07003980 unsigned int nparam, sparam;
H. Peter Anvind784a082009-04-20 14:01:18 -07003981 int brackets;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003982 Token *org_tline = tline;
3983 Context *ctx;
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08003984 const char *mname;
H. Peter Anvin2a15e692007-11-19 13:14:59 -08003985 int deadman = DEADMAN_LIMIT;
H. Peter Anvin8287daf2009-07-07 16:00:58 -07003986 bool expanded;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003987
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003988 /*
3989 * Trick: we should avoid changing the start token pointer since it can
3990 * be contained in "next" field of other token. Because of this
3991 * we allocate a copy of first token and work with it; at the end of
3992 * routine we copy it back
3993 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003994 if (org_tline) {
Cyrill Gorcunoved4a8052010-04-09 15:40:35 +04003995 tline = new_Token(org_tline->next, org_tline->type,
3996 org_tline->text, 0);
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003997 tline->a.mac = org_tline->a.mac;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003998 nasm_free(org_tline->text);
3999 org_tline->text = NULL;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004000 }
4001
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004002 expanded = true; /* Always expand %+ at least once */
H. Peter Anvin8287daf2009-07-07 16:00:58 -07004003
H. Peter Anvincb1cf592007-11-19 12:26:50 -08004004again:
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004005 thead = NULL;
Cyrill Gorcunoved4a8052010-04-09 15:40:35 +04004006 tail = &thead;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004007
H. Peter Anvine2c80182005-01-15 22:15:51 +00004008 while (tline) { /* main token loop */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004009 if (!--deadman) {
4010 error(ERR_NONFATAL, "interminable macro recursion");
Cyrill Gorcunovbd38c8f2009-11-21 11:11:23 +03004011 goto err;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004012 }
H. Peter Anvincb1cf592007-11-19 12:26:50 -08004013
H. Peter Anvine2c80182005-01-15 22:15:51 +00004014 if ((mname = tline->text)) {
4015 /* if this token is a local macro, look in local context */
Cyrill Gorcunovc56d9ad2010-02-11 15:12:19 +03004016 if (tline->type == TOK_ID) {
4017 head = (SMacro *)hash_findix(&smacros, mname);
4018 } else if (tline->type == TOK_PREPROC_ID) {
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08004019 ctx = get_ctx(mname, &mname, true);
Cyrill Gorcunovc56d9ad2010-02-11 15:12:19 +03004020 head = ctx ? (SMacro *)hash_findix(&ctx->localmac, mname) : NULL;
4021 } else
4022 head = NULL;
H. Peter Anvin072771e2008-05-22 13:17:51 -07004023
H. Peter Anvine2c80182005-01-15 22:15:51 +00004024 /*
4025 * We've hit an identifier. As in is_mmacro below, we first
4026 * check whether the identifier is a single-line macro at
4027 * all, then think about checking for parameters if
4028 * necessary.
4029 */
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04004030 list_for_each(m, head)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004031 if (!mstrcmp(m->name, mname, m->casesense))
4032 break;
4033 if (m) {
4034 mstart = tline;
4035 params = NULL;
4036 paramsize = NULL;
4037 if (m->nparam == 0) {
4038 /*
4039 * Simple case: the macro is parameterless. Discard the
4040 * one token that the macro call took, and push the
4041 * expansion back on the to-do stack.
4042 */
4043 if (!m->expansion) {
4044 if (!strcmp("__FILE__", m->name)) {
4045 int32_t num = 0;
4046 char *file = NULL;
4047 src_get(&num, &file);
4048 tline->text = nasm_quote(file, strlen(file));
4049 tline->type = TOK_STRING;
4050 nasm_free(file);
H. Peter Anvine2c80182005-01-15 22:15:51 +00004051 continue;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004052 }
4053 if (!strcmp("__LINE__", m->name)) {
4054 nasm_free(tline->text);
4055 make_tok_num(tline, src_get_linnum());
4056 continue;
4057 }
4058 if (!strcmp("__BITS__", m->name)) {
4059 nasm_free(tline->text);
4060 make_tok_num(tline, globalbits);
4061 continue;
4062 }
4063 tline = delete_Token(tline);
4064 continue;
4065 }
4066 } else {
4067 /*
4068 * Complicated case: at least one macro with this name
H. Peter Anvine2c80182005-01-15 22:15:51 +00004069 * exists and takes parameters. We must find the
4070 * parameters in the call, count them, find the SMacro
4071 * that corresponds to that form of the macro call, and
4072 * substitute for the parameters when we expand. What a
4073 * pain.
4074 */
4075 /*tline = tline->next;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004076 skip_white_(tline); */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004077 do {
4078 t = tline->next;
4079 while (tok_type_(t, TOK_SMAC_END)) {
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004080 t->a.mac->in_progress = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004081 t->text = NULL;
4082 t = tline->next = delete_Token(t);
4083 }
4084 tline = t;
4085 } while (tok_type_(tline, TOK_WHITESPACE));
4086 if (!tok_is_(tline, "(")) {
4087 /*
4088 * This macro wasn't called with parameters: ignore
4089 * the call. (Behaviour borrowed from gnu cpp.)
4090 */
4091 tline = mstart;
4092 m = NULL;
4093 } else {
4094 int paren = 0;
4095 int white = 0;
4096 brackets = 0;
4097 nparam = 0;
4098 sparam = PARAM_DELTA;
4099 params = nasm_malloc(sparam * sizeof(Token *));
4100 params[0] = tline->next;
4101 paramsize = nasm_malloc(sparam * sizeof(int));
4102 paramsize[0] = 0;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07004103 while (true) { /* parameter loop */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004104 /*
4105 * For some unusual expansions
4106 * which concatenates function call
4107 */
4108 t = tline->next;
4109 while (tok_type_(t, TOK_SMAC_END)) {
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004110 t->a.mac->in_progress = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004111 t->text = NULL;
4112 t = tline->next = delete_Token(t);
4113 }
4114 tline = t;
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00004115
H. Peter Anvine2c80182005-01-15 22:15:51 +00004116 if (!tline) {
4117 error(ERR_NONFATAL,
4118 "macro call expects terminating `)'");
4119 break;
4120 }
4121 if (tline->type == TOK_WHITESPACE
4122 && brackets <= 0) {
4123 if (paramsize[nparam])
4124 white++;
4125 else
4126 params[nparam] = tline->next;
4127 continue; /* parameter loop */
4128 }
4129 if (tline->type == TOK_OTHER
4130 && tline->text[1] == 0) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004131 char ch = tline->text[0];
H. Peter Anvine2c80182005-01-15 22:15:51 +00004132 if (ch == ',' && !paren && brackets <= 0) {
4133 if (++nparam >= sparam) {
4134 sparam += PARAM_DELTA;
4135 params = nasm_realloc(params,
Cyrill Gorcunoved4a8052010-04-09 15:40:35 +04004136 sparam * sizeof(Token *));
4137 paramsize = nasm_realloc(paramsize,
4138 sparam * sizeof(int));
H. Peter Anvine2c80182005-01-15 22:15:51 +00004139 }
4140 params[nparam] = tline->next;
4141 paramsize[nparam] = 0;
4142 white = 0;
4143 continue; /* parameter loop */
4144 }
4145 if (ch == '{' &&
4146 (brackets > 0 || (brackets == 0 &&
4147 !paramsize[nparam])))
4148 {
4149 if (!(brackets++)) {
4150 params[nparam] = tline->next;
4151 continue; /* parameter loop */
4152 }
4153 }
4154 if (ch == '}' && brackets > 0)
4155 if (--brackets == 0) {
4156 brackets = -1;
4157 continue; /* parameter loop */
4158 }
4159 if (ch == '(' && !brackets)
4160 paren++;
4161 if (ch == ')' && brackets <= 0)
4162 if (--paren < 0)
4163 break;
4164 }
4165 if (brackets < 0) {
4166 brackets = 0;
4167 error(ERR_NONFATAL, "braces do not "
4168 "enclose all of macro parameter");
4169 }
4170 paramsize[nparam] += white + 1;
4171 white = 0;
4172 } /* parameter loop */
4173 nparam++;
4174 while (m && (m->nparam != nparam ||
4175 mstrcmp(m->name, mname,
4176 m->casesense)))
4177 m = m->next;
4178 if (!m)
H. Peter Anvin917a3492008-09-24 09:14:49 -07004179 error(ERR_WARNING|ERR_PASS1|ERR_WARN_MNP,
H. Peter Anvine2c80182005-01-15 22:15:51 +00004180 "macro `%s' exists, "
4181 "but not taking %d parameters",
4182 mstart->text, nparam);
4183 }
4184 }
4185 if (m && m->in_progress)
4186 m = NULL;
4187 if (!m) { /* in progess or didn't find '(' or wrong nparam */
H. Peter Anvin70653092007-10-19 14:42:29 -07004188 /*
H. Peter Anvine2c80182005-01-15 22:15:51 +00004189 * Design question: should we handle !tline, which
4190 * indicates missing ')' here, or expand those
4191 * macros anyway, which requires the (t) test a few
H. Peter Anvin70653092007-10-19 14:42:29 -07004192 * lines down?
H. Peter Anvine2c80182005-01-15 22:15:51 +00004193 */
4194 nasm_free(params);
4195 nasm_free(paramsize);
4196 tline = mstart;
4197 } else {
4198 /*
4199 * Expand the macro: we are placed on the last token of the
4200 * call, so that we can easily split the call from the
4201 * following tokens. We also start by pushing an SMAC_END
4202 * token for the cycle removal.
4203 */
4204 t = tline;
4205 if (t) {
4206 tline = t->next;
4207 t->next = NULL;
4208 }
4209 tt = new_Token(tline, TOK_SMAC_END, NULL, 0);
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004210 tt->a.mac = m;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07004211 m->in_progress = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004212 tline = tt;
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04004213 list_for_each(t, m->expansion) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00004214 if (t->type >= TOK_SMAC_PARAM) {
4215 Token *pcopy = tline, **ptail = &pcopy;
4216 Token *ttt, *pt;
4217 int i;
H. Peter Anvin734b1882002-04-30 21:01:08 +00004218
H. Peter Anvine2c80182005-01-15 22:15:51 +00004219 ttt = params[t->type - TOK_SMAC_PARAM];
Cyrill Gorcunoved4a8052010-04-09 15:40:35 +04004220 i = paramsize[t->type - TOK_SMAC_PARAM];
4221 while (--i >= 0) {
4222 pt = *ptail = new_Token(tline, ttt->type,
4223 ttt->text, 0);
H. Peter Anvine2c80182005-01-15 22:15:51 +00004224 ptail = &pt->next;
4225 ttt = ttt->next;
4226 }
4227 tline = pcopy;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004228 } else if (t->type == TOK_PREPROC_Q) {
4229 tt = new_Token(tline, TOK_ID, mname, 0);
4230 tline = tt;
4231 } else if (t->type == TOK_PREPROC_QQ) {
4232 tt = new_Token(tline, TOK_ID, m->name, 0);
4233 tline = tt;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004234 } else {
4235 tt = new_Token(tline, t->type, t->text, 0);
4236 tline = tt;
4237 }
4238 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00004239
H. Peter Anvine2c80182005-01-15 22:15:51 +00004240 /*
4241 * Having done that, get rid of the macro call, and clean
4242 * up the parameters.
4243 */
4244 nasm_free(params);
4245 nasm_free(paramsize);
4246 free_tlist(mstart);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004247 expanded = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004248 continue; /* main token loop */
4249 }
4250 }
4251 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004252
H. Peter Anvine2c80182005-01-15 22:15:51 +00004253 if (tline->type == TOK_SMAC_END) {
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004254 tline->a.mac->in_progress = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004255 tline = delete_Token(tline);
4256 } else {
4257 t = *tail = tline;
4258 tline = tline->next;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004259 t->a.mac = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004260 t->next = NULL;
4261 tail = &t->next;
4262 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004263 }
4264
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004265 /*
4266 * Now scan the entire line and look for successive TOK_IDs that resulted
Keith Kaniosb7a89542007-04-12 02:40:54 +00004267 * after expansion (they can't be produced by tokenize()). The successive
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004268 * TOK_IDs should be concatenated.
4269 * Also we look for %+ tokens and concatenate the tokens before and after
4270 * them (without white spaces in between).
4271 */
H. Peter Anvin8287daf2009-07-07 16:00:58 -07004272 if (expanded && paste_tokens(&thead, true)) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004273 /*
4274 * If we concatenated something, *and* we had previously expanded
4275 * an actual macro, scan the lines again for macros...
4276 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004277 tline = thead;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004278 expanded = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004279 goto again;
H. Peter Anvin734b1882002-04-30 21:01:08 +00004280 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004281
Cyrill Gorcunovbd38c8f2009-11-21 11:11:23 +03004282err:
H. Peter Anvine2c80182005-01-15 22:15:51 +00004283 if (org_tline) {
4284 if (thead) {
4285 *org_tline = *thead;
4286 /* since we just gave text to org_line, don't free it */
4287 thead->text = NULL;
4288 delete_Token(thead);
4289 } else {
4290 /* the expression expanded to empty line;
4291 we can't return NULL for some reasons
4292 we just set the line to a single WHITESPACE token. */
4293 memset(org_tline, 0, sizeof(*org_tline));
4294 org_tline->text = NULL;
4295 org_tline->type = TOK_WHITESPACE;
4296 }
4297 thead = org_tline;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004298 }
4299
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004300 return thead;
4301}
4302
4303/*
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004304 * Similar to expand_smacro but used exclusively with macro identifiers
4305 * right before they are fetched in. The reason is that there can be
4306 * identifiers consisting of several subparts. We consider that if there
4307 * are more than one element forming the name, user wants a expansion,
4308 * otherwise it will be left as-is. Example:
4309 *
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004310 * %define %$abc cde
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004311 *
4312 * the identifier %$abc will be left as-is so that the handler for %define
4313 * will suck it and define the corresponding value. Other case:
4314 *
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004315 * %define _%$abc cde
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004316 *
4317 * In this case user wants name to be expanded *before* %define starts
4318 * working, so we'll expand %$abc into something (if it has a value;
4319 * otherwise it will be left as-is) then concatenate all successive
4320 * PP_IDs into one.
4321 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004322static Token *expand_id(Token * tline)
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004323{
4324 Token *cur, *oldnext = NULL;
4325
H. Peter Anvin734b1882002-04-30 21:01:08 +00004326 if (!tline || !tline->next)
H. Peter Anvine2c80182005-01-15 22:15:51 +00004327 return tline;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004328
4329 cur = tline;
4330 while (cur->next &&
H. Peter Anvine2c80182005-01-15 22:15:51 +00004331 (cur->next->type == TOK_ID ||
4332 cur->next->type == TOK_PREPROC_ID
4333 || cur->next->type == TOK_NUMBER))
4334 cur = cur->next;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004335
4336 /* If identifier consists of just one token, don't expand */
4337 if (cur == tline)
H. Peter Anvine2c80182005-01-15 22:15:51 +00004338 return tline;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004339
H. Peter Anvine2c80182005-01-15 22:15:51 +00004340 if (cur) {
4341 oldnext = cur->next; /* Detach the tail past identifier */
4342 cur->next = NULL; /* so that expand_smacro stops here */
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004343 }
4344
H. Peter Anvin734b1882002-04-30 21:01:08 +00004345 tline = expand_smacro(tline);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004346
H. Peter Anvine2c80182005-01-15 22:15:51 +00004347 if (cur) {
4348 /* expand_smacro possibly changhed tline; re-scan for EOL */
4349 cur = tline;
4350 while (cur && cur->next)
4351 cur = cur->next;
4352 if (cur)
4353 cur->next = oldnext;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004354 }
4355
4356 return tline;
4357}
4358
4359/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004360 * Determine whether the given line constitutes a multi-line macro
4361 * call, and return the MMacro structure called if so. Doesn't have
4362 * to check for an initial label - that's taken care of in
4363 * expand_mmacro - but must check numbers of parameters. Guaranteed
4364 * to be called with tline->type == TOK_ID, so the putative macro
4365 * name is easy to find.
4366 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004367static MMacro *is_mmacro(Token * tline, Token *** params_array)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004368{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004369 MMacro *head, *m;
4370 Token **params;
4371 int nparam;
4372
H. Peter Anvin166c2472008-05-28 12:28:58 -07004373 head = (MMacro *) hash_findix(&mmacros, tline->text);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004374
4375 /*
4376 * Efficiency: first we see if any macro exists with the given
4377 * name. If not, we can return NULL immediately. _Then_ we
4378 * count the parameters, and then we look further along the
4379 * list if necessary to find the proper MMacro.
4380 */
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04004381 list_for_each(m, head)
H. Peter Anvine2c80182005-01-15 22:15:51 +00004382 if (!mstrcmp(m->name, tline->text, m->casesense))
4383 break;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004384 if (!m)
H. Peter Anvine2c80182005-01-15 22:15:51 +00004385 return NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004386
4387 /*
4388 * OK, we have a potential macro. Count and demarcate the
4389 * parameters.
4390 */
H. Peter Anvin734b1882002-04-30 21:01:08 +00004391 count_mmac_params(tline->next, &nparam, &params);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004392
4393 /*
4394 * So we know how many parameters we've got. Find the MMacro
4395 * structure that handles this number.
4396 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004397 while (m) {
4398 if (m->nparam_min <= nparam
4399 && (m->plus || nparam <= m->nparam_max)) {
4400 /*
4401 * This one is right. Just check if cycle removal
4402 * prohibits us using it before we actually celebrate...
4403 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004404 if (m->in_progress > m->max_depth) {
4405 if (m->max_depth > 0) {
4406 error(ERR_WARNING,
4407 "reached maximum recursion depth of %i",
4408 m->max_depth);
4409 }
4410 nasm_free(params);
4411 return NULL;
4412 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00004413 /*
4414 * It's right, and we can use it. Add its default
4415 * parameters to the end of our list if necessary.
4416 */
4417 if (m->defaults && nparam < m->nparam_min + m->ndefs) {
4418 params =
4419 nasm_realloc(params,
4420 ((m->nparam_min + m->ndefs +
4421 1) * sizeof(*params)));
4422 while (nparam < m->nparam_min + m->ndefs) {
4423 params[nparam] = m->defaults[nparam - m->nparam_min];
4424 nparam++;
4425 }
4426 }
4427 /*
4428 * If we've gone over the maximum parameter count (and
4429 * we're in Plus mode), ignore parameters beyond
4430 * nparam_max.
4431 */
4432 if (m->plus && nparam > m->nparam_max)
4433 nparam = m->nparam_max;
4434 /*
4435 * Then terminate the parameter list, and leave.
4436 */
4437 if (!params) { /* need this special case */
4438 params = nasm_malloc(sizeof(*params));
4439 nparam = 0;
4440 }
4441 params[nparam] = NULL;
4442 *params_array = params;
4443 return m;
4444 }
4445 /*
4446 * This one wasn't right: look for the next one with the
4447 * same name.
4448 */
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04004449 list_for_each(m, m->next)
H. Peter Anvine2c80182005-01-15 22:15:51 +00004450 if (!mstrcmp(m->name, tline->text, m->casesense))
4451 break;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004452 }
4453
4454 /*
4455 * After all that, we didn't find one with the right number of
4456 * parameters. Issue a warning, and fail to expand the macro.
4457 */
H. Peter Anvin917a3492008-09-24 09:14:49 -07004458 error(ERR_WARNING|ERR_PASS1|ERR_WARN_MNP,
H. Peter Anvine2c80182005-01-15 22:15:51 +00004459 "macro `%s' exists, but not taking %d parameters",
4460 tline->text, nparam);
H. Peter Anvin734b1882002-04-30 21:01:08 +00004461 nasm_free(params);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004462 return NULL;
4463}
4464
Keith Kanios891775e2009-07-11 06:08:54 -05004465
4466/*
4467 * Save MMacro invocation specific fields in
4468 * preparation for a recursive macro expansion
4469 */
4470static void push_mmacro(MMacro *m)
4471{
4472 MMacroInvocation *i;
4473
H. Peter Anvin89cee572009-07-15 09:16:54 -04004474 i = nasm_malloc(sizeof(MMacroInvocation));
4475 i->prev = m->prev;
4476 i->params = m->params;
4477 i->iline = m->iline;
4478 i->nparam = m->nparam;
4479 i->rotate = m->rotate;
4480 i->paramlen = m->paramlen;
4481 i->unique = m->unique;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004482 i->condcnt = m->condcnt;
H. Peter Anvin89cee572009-07-15 09:16:54 -04004483 m->prev = i;
Keith Kanios891775e2009-07-11 06:08:54 -05004484}
4485
4486
4487/*
4488 * Restore MMacro invocation specific fields that were
4489 * saved during a previous recursive macro expansion
4490 */
4491static void pop_mmacro(MMacro *m)
4492{
4493 MMacroInvocation *i;
4494
H. Peter Anvin89cee572009-07-15 09:16:54 -04004495 if (m->prev) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004496 i = m->prev;
4497 m->prev = i->prev;
4498 m->params = i->params;
4499 m->iline = i->iline;
4500 m->nparam = i->nparam;
4501 m->rotate = i->rotate;
4502 m->paramlen = i->paramlen;
4503 m->unique = i->unique;
4504 m->condcnt = i->condcnt;
4505 nasm_free(i);
H. Peter Anvin89cee572009-07-15 09:16:54 -04004506 }
Keith Kanios891775e2009-07-11 06:08:54 -05004507}
4508
4509
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004510/*
4511 * Expand the multi-line macro call made by the given line, if
4512 * there is one to be expanded. If there is, push the expansion on
H. Peter Anvineba20a72002-04-30 20:53:55 +00004513 * istk->expansion and return 1. Otherwise return 0.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004514 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004515static int expand_mmacro(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004516{
4517 Token *startline = tline;
4518 Token *label = NULL;
4519 int dont_prepend = 0;
H. Peter Anvince2233b2008-05-25 21:57:00 -07004520 Token **params, *t, *mtok, *tt;
H. Peter Anvineba20a72002-04-30 20:53:55 +00004521 MMacro *m;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004522 Line *l, *ll;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004523 int i, nparam, *paramlen;
H. Peter Anvinc751e862008-06-09 10:18:45 -07004524 const char *mname;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004525
4526 t = tline;
H. Peter Anvineba20a72002-04-30 20:53:55 +00004527 skip_white_(t);
H. Peter Anvince2233b2008-05-25 21:57:00 -07004528 /* if (!tok_type_(t, TOK_ID)) Lino 02/25/02 */
H. Peter Anvindce1e2f2002-04-30 21:06:37 +00004529 if (!tok_type_(t, TOK_ID) && !tok_type_(t, TOK_PREPROC_ID))
H. Peter Anvine2c80182005-01-15 22:15:51 +00004530 return 0;
H. Peter Anvince2233b2008-05-25 21:57:00 -07004531 mtok = t;
H. Peter Anvin734b1882002-04-30 21:01:08 +00004532 m = is_mmacro(t, &params);
H. Peter Anvinc751e862008-06-09 10:18:45 -07004533 if (m) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004534 mname = t->text;
H. Peter Anvinc751e862008-06-09 10:18:45 -07004535 } else {
H. Peter Anvine2c80182005-01-15 22:15:51 +00004536 Token *last;
4537 /*
4538 * We have an id which isn't a macro call. We'll assume
4539 * it might be a label; we'll also check to see if a
4540 * colon follows it. Then, if there's another id after
4541 * that lot, we'll check it again for macro-hood.
4542 */
4543 label = last = t;
4544 t = t->next;
4545 if (tok_type_(t, TOK_WHITESPACE))
4546 last = t, t = t->next;
4547 if (tok_is_(t, ":")) {
4548 dont_prepend = 1;
4549 last = t, t = t->next;
4550 if (tok_type_(t, TOK_WHITESPACE))
4551 last = t, t = t->next;
4552 }
H. Peter Anvin89cee572009-07-15 09:16:54 -04004553 if (!tok_type_(t, TOK_ID) || !(m = is_mmacro(t, &params)))
H. Peter Anvine2c80182005-01-15 22:15:51 +00004554 return 0;
4555 last->next = NULL;
Keith Kanios891775e2009-07-11 06:08:54 -05004556 mname = t->text;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004557 tline = t;
H. Peter Anvineba20a72002-04-30 20:53:55 +00004558 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004559
4560 /*
4561 * Fix up the parameters: this involves stripping leading and
4562 * trailing whitespace, then stripping braces if they are
4563 * present.
4564 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004565 for (nparam = 0; params[nparam]; nparam++) ;
H. Peter Anvin734b1882002-04-30 21:01:08 +00004566 paramlen = nparam ? nasm_malloc(nparam * sizeof(*paramlen)) : NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004567
H. Peter Anvine2c80182005-01-15 22:15:51 +00004568 for (i = 0; params[i]; i++) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07004569 int brace = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004570 int comma = (!m->plus || i < nparam - 1);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004571
H. Peter Anvine2c80182005-01-15 22:15:51 +00004572 t = params[i];
4573 skip_white_(t);
4574 if (tok_is_(t, "{"))
H. Peter Anvin6867acc2007-10-10 14:58:45 -07004575 t = t->next, brace = true, comma = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004576 params[i] = t;
4577 paramlen[i] = 0;
4578 while (t) {
4579 if (comma && t->type == TOK_OTHER && !strcmp(t->text, ","))
4580 break; /* ... because we have hit a comma */
4581 if (comma && t->type == TOK_WHITESPACE
4582 && tok_is_(t->next, ","))
4583 break; /* ... or a space then a comma */
4584 if (brace && t->type == TOK_OTHER && !strcmp(t->text, "}"))
4585 break; /* ... or a brace */
4586 t = t->next;
4587 paramlen[i]++;
4588 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004589 }
4590
4591 /*
4592 * OK, we have a MMacro structure together with a set of
4593 * parameters. We must now go through the expansion and push
H. Peter Anvin76690a12002-04-30 20:52:49 +00004594 * copies of each Line on to istk->expansion. Substitution of
4595 * parameter tokens and macro-local tokens doesn't get done
4596 * until the single-line macro substitution process; this is
4597 * because delaying them allows us to change the semantics
4598 * later through %rotate.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004599 *
H. Peter Anvin76690a12002-04-30 20:52:49 +00004600 * First, push an end marker on to istk->expansion, mark this
4601 * macro as in progress, and set up its invocation-specific
4602 * variables.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004603 */
4604 ll = nasm_malloc(sizeof(Line));
4605 ll->next = istk->expansion;
4606 ll->finishes = m;
4607 ll->first = NULL;
4608 istk->expansion = ll;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004609
H. Peter Anvin89cee572009-07-15 09:16:54 -04004610 /*
4611 * Save the previous MMacro expansion in the case of
4612 * macro recursion
4613 */
4614 if (m->max_depth && m->in_progress)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004615 push_mmacro(m);
H. Peter Anvin76690a12002-04-30 20:52:49 +00004616
Keith Kanios891775e2009-07-11 06:08:54 -05004617 m->in_progress ++;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004618 m->params = params;
4619 m->iline = tline;
4620 m->nparam = nparam;
4621 m->rotate = 0;
4622 m->paramlen = paramlen;
4623 m->unique = unique++;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004624 m->lineno = 0;
Keith Kanios3c0d91f2009-10-25 13:28:03 -05004625 m->condcnt = 0;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004626
4627 m->next_active = istk->mstk;
4628 istk->mstk = m;
4629
Cyrill Gorcunov367d59e2010-04-09 15:43:03 +04004630 list_for_each(l, m->expansion) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00004631 Token **tail;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004632
H. Peter Anvine2c80182005-01-15 22:15:51 +00004633 ll = nasm_malloc(sizeof(Line));
4634 ll->finishes = NULL;
4635 ll->next = istk->expansion;
4636 istk->expansion = ll;
4637 tail = &ll->first;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004638
Cyrill Gorcunov367d59e2010-04-09 15:43:03 +04004639 list_for_each(t, l->first) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00004640 Token *x = t;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004641 switch (t->type) {
4642 case TOK_PREPROC_Q:
4643 tt = *tail = new_Token(NULL, TOK_ID, mname, 0);
4644 break;
4645 case TOK_PREPROC_QQ:
4646 tt = *tail = new_Token(NULL, TOK_ID, m->name, 0);
4647 break;
4648 case TOK_PREPROC_ID:
4649 if (t->text[1] == '0' && t->text[2] == '0') {
4650 dont_prepend = -1;
4651 x = label;
4652 if (!x)
4653 continue;
4654 }
4655 /* fall through */
4656 default:
4657 tt = *tail = new_Token(NULL, x->type, x->text, 0);
4658 break;
4659 }
4660 tail = &tt->next;
4661 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00004662 *tail = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004663 }
4664
4665 /*
H. Peter Anvineba20a72002-04-30 20:53:55 +00004666 * If we had a label, push it on as the first line of
4667 * the macro expansion.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004668 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004669 if (label) {
4670 if (dont_prepend < 0)
4671 free_tlist(startline);
4672 else {
4673 ll = nasm_malloc(sizeof(Line));
4674 ll->finishes = NULL;
4675 ll->next = istk->expansion;
4676 istk->expansion = ll;
4677 ll->first = startline;
4678 if (!dont_prepend) {
4679 while (label->next)
4680 label = label->next;
4681 label->next = tt = new_Token(NULL, TOK_OTHER, ":", 0);
4682 }
4683 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004684 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004685
H. Peter Anvin734b1882002-04-30 21:01:08 +00004686 list->uplevel(m->nolist ? LIST_MACRO_NOLIST : LIST_MACRO);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004687
H. Peter Anvineba20a72002-04-30 20:53:55 +00004688 return 1;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004689}
4690
Victor van den Elzen3b404c02008-09-18 13:51:36 +02004691/* The function that actually does the error reporting */
4692static void verror(int severity, const char *fmt, va_list arg)
4693{
4694 char buff[1024];
4695
4696 vsnprintf(buff, sizeof(buff), fmt, arg);
4697
4698 if (istk && istk->mstk && istk->mstk->name)
H. Peter Anvindbb640b2009-07-18 18:57:16 -07004699 nasm_error(severity, "(%s:%d) %s", istk->mstk->name,
Victor van den Elzen3b404c02008-09-18 13:51:36 +02004700 istk->mstk->lineno, buff);
4701 else
H. Peter Anvindbb640b2009-07-18 18:57:16 -07004702 nasm_error(severity, "%s", buff);
Victor van den Elzen3b404c02008-09-18 13:51:36 +02004703}
4704
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004705/*
4706 * Since preprocessor always operate only on the line that didn't
H. Peter Anvin917a3492008-09-24 09:14:49 -07004707 * arrived yet, we should always use ERR_OFFBY1.
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004708 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004709static void error(int severity, const char *fmt, ...)
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004710{
4711 va_list arg;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004712
4713 /* If we're in a dead branch of IF or something like it, ignore the error */
H. Peter Anvin77ba0c62002-05-14 03:18:53 +00004714 if (istk && istk->conds && !emitting(istk->conds->state))
H. Peter Anvine2c80182005-01-15 22:15:51 +00004715 return;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004716
H. Peter Anvin734b1882002-04-30 21:01:08 +00004717 va_start(arg, fmt);
Victor van den Elzen3b404c02008-09-18 13:51:36 +02004718 verror(severity, fmt, arg);
H. Peter Anvin734b1882002-04-30 21:01:08 +00004719 va_end(arg);
Victor van den Elzen3b404c02008-09-18 13:51:36 +02004720}
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004721
Victor van den Elzen3b404c02008-09-18 13:51:36 +02004722/*
4723 * Because %else etc are evaluated in the state context
4724 * of the previous branch, errors might get lost with error():
4725 * %if 0 ... %else trailing garbage ... %endif
4726 * So %else etc should report errors with this function.
4727 */
4728static void error_precond(int severity, const char *fmt, ...)
4729{
4730 va_list arg;
4731
4732 /* Only ignore the error if it's really in a dead branch */
4733 if (istk && istk->conds && istk->conds->state == COND_NEVER)
4734 return;
4735
4736 va_start(arg, fmt);
4737 verror(severity, fmt, arg);
4738 va_end(arg);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004739}
4740
H. Peter Anvin734b1882002-04-30 21:01:08 +00004741static void
H. Peter Anvindbb640b2009-07-18 18:57:16 -07004742pp_reset(char *file, int apass, ListGen * listgen, StrList **deplist)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004743{
H. Peter Anvin7383b402008-09-24 10:20:40 -07004744 Token *t;
4745
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004746 cstk = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004747 istk = nasm_malloc(sizeof(Include));
4748 istk->next = NULL;
4749 istk->conds = NULL;
4750 istk->expansion = NULL;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004751 istk->mstk = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004752 istk->fp = fopen(file, "r");
H. Peter Anvineba20a72002-04-30 20:53:55 +00004753 istk->fname = NULL;
4754 src_set_fname(nasm_strdup(file));
4755 src_set_linnum(0);
4756 istk->lineinc = 1;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004757 if (!istk->fp)
H. Peter Anvin917a3492008-09-24 09:14:49 -07004758 error(ERR_FATAL|ERR_NOFILE, "unable to open input file `%s'",
H. Peter Anvine2c80182005-01-15 22:15:51 +00004759 file);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004760 defining = NULL;
Charles Crayned4200be2008-07-12 16:42:33 -07004761 nested_mac_count = 0;
4762 nested_rep_count = 0;
H. Peter Anvin97a23472007-09-16 17:57:25 -07004763 init_macros();
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004764 unique = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004765 if (tasm_compatible_mode) {
H. Peter Anvina4835d42008-05-20 14:21:29 -07004766 stdmacpos = nasm_stdmac;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004767 } else {
H. Peter Anvina4835d42008-05-20 14:21:29 -07004768 stdmacpos = nasm_stdmac_after_tasm;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004769 }
H. Peter Anvind2456592008-06-19 15:04:18 -07004770 any_extrastdmac = extrastdmac && *extrastdmac;
4771 do_predef = true;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004772 list = listgen;
H. Peter Anvin61f130f2008-09-25 15:45:06 -07004773
4774 /*
4775 * 0 for dependencies, 1 for preparatory passes, 2 for final pass.
4776 * The caller, however, will also pass in 3 for preprocess-only so
4777 * we can set __PASS__ accordingly.
4778 */
4779 pass = apass > 2 ? 2 : apass;
4780
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07004781 dephead = deptail = deplist;
4782 if (deplist) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004783 StrList *sl = nasm_malloc(strlen(file)+1+sizeof sl->next);
4784 sl->next = NULL;
4785 strcpy(sl->str, file);
4786 *deptail = sl;
4787 deptail = &sl->next;
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07004788 }
H. Peter Anvin7383b402008-09-24 10:20:40 -07004789
H. Peter Anvin61f130f2008-09-25 15:45:06 -07004790 /*
4791 * Define the __PASS__ macro. This is defined here unlike
4792 * all the other builtins, because it is special -- it varies between
4793 * passes.
4794 */
H. Peter Anvin7383b402008-09-24 10:20:40 -07004795 t = nasm_malloc(sizeof(*t));
4796 t->next = NULL;
H. Peter Anvin61f130f2008-09-25 15:45:06 -07004797 make_tok_num(t, apass);
H. Peter Anvin7383b402008-09-24 10:20:40 -07004798 t->a.mac = NULL;
4799 define_smacro(NULL, "__PASS__", true, 0, t);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004800}
4801
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004802static char *pp_getline(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004803{
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004804 char *line;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004805 Token *tline;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004806
H. Peter Anvine2c80182005-01-15 22:15:51 +00004807 while (1) {
4808 /*
Keith Kaniosb7a89542007-04-12 02:40:54 +00004809 * Fetch a tokenized line, either from the macro-expansion
H. Peter Anvine2c80182005-01-15 22:15:51 +00004810 * buffer or from the input file.
4811 */
4812 tline = NULL;
4813 while (istk->expansion && istk->expansion->finishes) {
4814 Line *l = istk->expansion;
4815 if (!l->finishes->name && l->finishes->in_progress > 1) {
4816 Line *ll;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004817
H. Peter Anvine2c80182005-01-15 22:15:51 +00004818 /*
4819 * This is a macro-end marker for a macro with no
4820 * name, which means it's not really a macro at all
4821 * but a %rep block, and the `in_progress' field is
4822 * more than 1, meaning that we still need to
4823 * repeat. (1 means the natural last repetition; 0
4824 * means termination by %exitrep.) We have
4825 * therefore expanded up to the %endrep, and must
4826 * push the whole block on to the expansion buffer
4827 * again. We don't bother to remove the macro-end
4828 * marker: we'd only have to generate another one
4829 * if we did.
4830 */
4831 l->finishes->in_progress--;
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04004832 list_for_each(l, l->finishes->expansion) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00004833 Token *t, *tt, **tail;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004834
H. Peter Anvine2c80182005-01-15 22:15:51 +00004835 ll = nasm_malloc(sizeof(Line));
4836 ll->next = istk->expansion;
4837 ll->finishes = NULL;
4838 ll->first = NULL;
4839 tail = &ll->first;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004840
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04004841 list_for_each(t, l->first) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00004842 if (t->text || t->type == TOK_WHITESPACE) {
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04004843 tt = *tail = new_Token(NULL, t->type, t->text, 0);
H. Peter Anvine2c80182005-01-15 22:15:51 +00004844 tail = &tt->next;
4845 }
4846 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00004847
H. Peter Anvine2c80182005-01-15 22:15:51 +00004848 istk->expansion = ll;
4849 }
4850 } else {
4851 /*
4852 * Check whether a `%rep' was started and not ended
4853 * within this macro expansion. This can happen and
4854 * should be detected. It's a fatal error because
4855 * I'm too confused to work out how to recover
4856 * sensibly from it.
4857 */
4858 if (defining) {
4859 if (defining->name)
4860 error(ERR_PANIC,
4861 "defining with name in expansion");
4862 else if (istk->mstk->name)
4863 error(ERR_FATAL,
4864 "`%%rep' without `%%endrep' within"
4865 " expansion of macro `%s'",
4866 istk->mstk->name);
4867 }
H. Peter Anvin87bc6192002-04-30 20:53:16 +00004868
H. Peter Anvine2c80182005-01-15 22:15:51 +00004869 /*
4870 * FIXME: investigate the relationship at this point between
4871 * istk->mstk and l->finishes
4872 */
4873 {
4874 MMacro *m = istk->mstk;
4875 istk->mstk = m->next_active;
4876 if (m->name) {
4877 /*
4878 * This was a real macro call, not a %rep, and
4879 * therefore the parameter information needs to
4880 * be freed.
4881 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004882 if (m->prev) {
4883 pop_mmacro(m);
4884 l->finishes->in_progress --;
4885 } else {
Keith Kanios891775e2009-07-11 06:08:54 -05004886 nasm_free(m->params);
4887 free_tlist(m->iline);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004888 nasm_free(m->paramlen);
4889 l->finishes->in_progress = 0;
4890 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00004891 } else
4892 free_mmacro(m);
4893 }
4894 istk->expansion = l->next;
4895 nasm_free(l);
4896 list->downlevel(LIST_MACRO);
4897 }
4898 }
4899 while (1) { /* until we get a line we can use */
H. Peter Anvineba20a72002-04-30 20:53:55 +00004900
H. Peter Anvine2c80182005-01-15 22:15:51 +00004901 if (istk->expansion) { /* from a macro expansion */
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004902 char *p;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004903 Line *l = istk->expansion;
4904 if (istk->mstk)
4905 istk->mstk->lineno++;
4906 tline = l->first;
4907 istk->expansion = l->next;
4908 nasm_free(l);
H. Peter Anvin6867acc2007-10-10 14:58:45 -07004909 p = detoken(tline, false);
H. Peter Anvine2c80182005-01-15 22:15:51 +00004910 list->line(LIST_MACRO, p);
4911 nasm_free(p);
4912 break;
4913 }
4914 line = read_line();
4915 if (line) { /* from the current input file */
4916 line = prepreproc(line);
Keith Kaniosb7a89542007-04-12 02:40:54 +00004917 tline = tokenize(line);
H. Peter Anvine2c80182005-01-15 22:15:51 +00004918 nasm_free(line);
4919 break;
4920 }
4921 /*
4922 * The current file has ended; work down the istk
4923 */
4924 {
4925 Include *i = istk;
4926 fclose(i->fp);
Cyrill Gorcunov5ace91d2010-09-12 02:00:05 +04004927 if (i->conds) {
4928 /* nasm_error can't be conditionally suppressed */
4929 nasm_error(ERR_FATAL,
4930 "expected `%%endif' before end of file");
4931 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00004932 /* only set line and file name if there's a next node */
4933 if (i->next) {
4934 src_set_linnum(i->lineno);
4935 nasm_free(src_set_fname(i->fname));
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004936 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00004937 istk = i->next;
4938 list->downlevel(LIST_INCLUDE);
4939 nasm_free(i);
4940 if (!istk)
4941 return NULL;
Victor van den Elzen4c9d6222008-10-01 13:08:50 +02004942 if (istk->expansion && istk->expansion->finishes)
4943 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004944 }
4945 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004946
H. Peter Anvine2c80182005-01-15 22:15:51 +00004947 /*
4948 * We must expand MMacro parameters and MMacro-local labels
4949 * _before_ we plunge into directive processing, to cope
4950 * with things like `%define something %1' such as STRUC
4951 * uses. Unless we're _defining_ a MMacro, in which case
4952 * those tokens should be left alone to go into the
4953 * definition; and unless we're in a non-emitting
4954 * condition, in which case we don't want to meddle with
4955 * anything.
4956 */
Charles Crayned4200be2008-07-12 16:42:33 -07004957 if (!defining && !(istk->conds && !emitting(istk->conds->state))
H. Peter Anvin992fe752008-10-19 15:45:05 -07004958 && !(istk->mstk && !istk->mstk->in_progress)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00004959 tline = expand_mmac_params(tline);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004960 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00004961
H. Peter Anvine2c80182005-01-15 22:15:51 +00004962 /*
4963 * Check the line to see if it's a preprocessor directive.
4964 */
4965 if (do_directive(tline) == DIRECTIVE_FOUND) {
4966 continue;
4967 } else if (defining) {
4968 /*
4969 * We're defining a multi-line macro. We emit nothing
4970 * at all, and just
Keith Kaniosb7a89542007-04-12 02:40:54 +00004971 * shove the tokenized line on to the macro definition.
H. Peter Anvine2c80182005-01-15 22:15:51 +00004972 */
4973 Line *l = nasm_malloc(sizeof(Line));
4974 l->next = defining->expansion;
4975 l->first = tline;
H. Peter Anvin538002d2008-06-28 18:30:27 -07004976 l->finishes = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004977 defining->expansion = l;
4978 continue;
4979 } else if (istk->conds && !emitting(istk->conds->state)) {
4980 /*
4981 * We're in a non-emitting branch of a condition block.
4982 * Emit nothing at all, not even a blank line: when we
4983 * emerge from the condition we'll give a line-number
4984 * directive so we keep our place correctly.
4985 */
4986 free_tlist(tline);
4987 continue;
4988 } else if (istk->mstk && !istk->mstk->in_progress) {
4989 /*
4990 * We're in a %rep block which has been terminated, so
4991 * we're walking through to the %endrep without
4992 * emitting anything. Emit nothing at all, not even a
4993 * blank line: when we emerge from the %rep block we'll
4994 * give a line-number directive so we keep our place
4995 * correctly.
4996 */
4997 free_tlist(tline);
4998 continue;
4999 } else {
5000 tline = expand_smacro(tline);
5001 if (!expand_mmacro(tline)) {
5002 /*
Keith Kaniosb7a89542007-04-12 02:40:54 +00005003 * De-tokenize the line again, and emit it.
H. Peter Anvine2c80182005-01-15 22:15:51 +00005004 */
H. Peter Anvin6867acc2007-10-10 14:58:45 -07005005 line = detoken(tline, true);
H. Peter Anvine2c80182005-01-15 22:15:51 +00005006 free_tlist(tline);
5007 break;
5008 } else {
5009 continue; /* expand_mmacro calls free_tlist */
5010 }
5011 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005012 }
5013
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005014 return line;
5015}
5016
H. Peter Anvine2c80182005-01-15 22:15:51 +00005017static void pp_cleanup(int pass)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005018{
H. Peter Anvine2c80182005-01-15 22:15:51 +00005019 if (defining) {
H. Peter Anvin89cee572009-07-15 09:16:54 -04005020 if (defining->name) {
Victor van den Elzen8f1120f2008-07-16 13:41:37 +02005021 error(ERR_NONFATAL,
5022 "end of file while still defining macro `%s'",
5023 defining->name);
5024 } else {
5025 error(ERR_NONFATAL, "end of file while still in %%rep");
5026 }
5027
H. Peter Anvine2c80182005-01-15 22:15:51 +00005028 free_mmacro(defining);
Cyrill Gorcunova327c652010-02-14 17:19:38 +03005029 defining = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005030 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005031 while (cstk)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005032 ctx_pop();
H. Peter Anvin97a23472007-09-16 17:57:25 -07005033 free_macros();
H. Peter Anvine2c80182005-01-15 22:15:51 +00005034 while (istk) {
5035 Include *i = istk;
5036 istk = istk->next;
5037 fclose(i->fp);
5038 nasm_free(i->fname);
5039 nasm_free(i);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005040 }
5041 while (cstk)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005042 ctx_pop();
H. Peter Anvin86877b22008-06-20 15:55:45 -07005043 nasm_free(src_set_fname(NULL));
H. Peter Anvine2c80182005-01-15 22:15:51 +00005044 if (pass == 0) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03005045 IncPath *i;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005046 free_llist(predef);
5047 delete_Blocks();
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03005048 while ((i = ipath)) {
5049 ipath = i->next;
5050 if (i->path)
5051 nasm_free(i->path);
5052 nasm_free(i);
5053 }
H. Peter Anvin11dfa1a2008-07-02 18:11:04 -07005054 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005055}
5056
Keith Kaniosa6dfa782007-04-13 16:47:53 +00005057void pp_include_path(char *path)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005058{
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005059 IncPath *i;
H. Peter Anvin37a321f2007-09-24 13:41:58 -07005060
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005061 i = nasm_malloc(sizeof(IncPath));
H. Peter Anvin37a321f2007-09-24 13:41:58 -07005062 i->path = path ? nasm_strdup(path) : NULL;
Frank Kotlerd0ed6fd2003-08-27 11:33:56 +00005063 i->next = NULL;
5064
H. Peter Anvin89cee572009-07-15 09:16:54 -04005065 if (ipath) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00005066 IncPath *j = ipath;
H. Peter Anvin89cee572009-07-15 09:16:54 -04005067 while (j->next)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005068 j = j->next;
5069 j->next = i;
5070 } else {
5071 ipath = i;
Frank Kotlerd0ed6fd2003-08-27 11:33:56 +00005072 }
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005073}
Frank Kotlerd0ed6fd2003-08-27 11:33:56 +00005074
Keith Kaniosa6dfa782007-04-13 16:47:53 +00005075void pp_pre_include(char *fname)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005076{
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005077 Token *inc, *space, *name;
5078 Line *l;
5079
H. Peter Anvin734b1882002-04-30 21:01:08 +00005080 name = new_Token(NULL, TOK_INTERNAL_STRING, fname, 0);
5081 space = new_Token(name, TOK_WHITESPACE, NULL, 0);
5082 inc = new_Token(space, TOK_PREPROC_ID, "%include", 0);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005083
5084 l = nasm_malloc(sizeof(Line));
5085 l->next = predef;
5086 l->first = inc;
H. Peter Anvin538002d2008-06-28 18:30:27 -07005087 l->finishes = NULL;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005088 predef = l;
5089}
5090
Keith Kaniosa6dfa782007-04-13 16:47:53 +00005091void pp_pre_define(char *definition)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005092{
5093 Token *def, *space;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005094 Line *l;
Keith Kaniosa6dfa782007-04-13 16:47:53 +00005095 char *equals;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005096
5097 equals = strchr(definition, '=');
H. Peter Anvin734b1882002-04-30 21:01:08 +00005098 space = new_Token(NULL, TOK_WHITESPACE, NULL, 0);
5099 def = new_Token(space, TOK_PREPROC_ID, "%define", 0);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005100 if (equals)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005101 *equals = ' ';
Keith Kaniosb7a89542007-04-12 02:40:54 +00005102 space->next = tokenize(definition);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005103 if (equals)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005104 *equals = '=';
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005105
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005106 l = nasm_malloc(sizeof(Line));
5107 l->next = predef;
5108 l->first = def;
H. Peter Anvin538002d2008-06-28 18:30:27 -07005109 l->finishes = NULL;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005110 predef = l;
5111}
5112
Keith Kaniosa6dfa782007-04-13 16:47:53 +00005113void pp_pre_undefine(char *definition)
H. Peter Anvin620515a2002-04-30 20:57:38 +00005114{
5115 Token *def, *space;
5116 Line *l;
5117
H. Peter Anvin734b1882002-04-30 21:01:08 +00005118 space = new_Token(NULL, TOK_WHITESPACE, NULL, 0);
5119 def = new_Token(space, TOK_PREPROC_ID, "%undef", 0);
Keith Kaniosb7a89542007-04-12 02:40:54 +00005120 space->next = tokenize(definition);
H. Peter Anvin620515a2002-04-30 20:57:38 +00005121
5122 l = nasm_malloc(sizeof(Line));
5123 l->next = predef;
5124 l->first = def;
H. Peter Anvin538002d2008-06-28 18:30:27 -07005125 l->finishes = NULL;
H. Peter Anvin620515a2002-04-30 20:57:38 +00005126 predef = l;
5127}
5128
Keith Kaniosb7a89542007-04-12 02:40:54 +00005129/*
5130 * Added by Keith Kanios:
5131 *
5132 * This function is used to assist with "runtime" preprocessor
5133 * directives. (e.g. pp_runtime("%define __BITS__ 64");)
5134 *
5135 * ERRORS ARE IGNORED HERE, SO MAKE COMPLETELY SURE THAT YOU
5136 * PASS A VALID STRING TO THIS FUNCTION!!!!!
5137 */
5138
Keith Kaniosa6dfa782007-04-13 16:47:53 +00005139void pp_runtime(char *definition)
Keith Kaniosb7a89542007-04-12 02:40:54 +00005140{
5141 Token *def;
H. Peter Anvin70653092007-10-19 14:42:29 -07005142
Keith Kaniosb7a89542007-04-12 02:40:54 +00005143 def = tokenize(definition);
H. Peter Anvin89cee572009-07-15 09:16:54 -04005144 if (do_directive(def) == NO_DIRECTIVE_FOUND)
Keith Kaniosb7a89542007-04-12 02:40:54 +00005145 free_tlist(def);
H. Peter Anvin70653092007-10-19 14:42:29 -07005146
Keith Kaniosb7a89542007-04-12 02:40:54 +00005147}
5148
H. Peter Anvina70547f2008-07-19 21:44:26 -07005149void pp_extra_stdmac(macros_t *macros)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005150{
H. Peter Anvin76690a12002-04-30 20:52:49 +00005151 extrastdmac = macros;
5152}
5153
Keith Kaniosa5fc6462007-10-13 07:09:22 -07005154static void make_tok_num(Token * tok, int64_t val)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005155{
Keith Kaniosa6dfa782007-04-13 16:47:53 +00005156 char numbuf[20];
Keith Kaniosa5fc6462007-10-13 07:09:22 -07005157 snprintf(numbuf, sizeof(numbuf), "%"PRId64"", val);
H. Peter Anvineba20a72002-04-30 20:53:55 +00005158 tok->text = nasm_strdup(numbuf);
5159 tok->type = TOK_NUMBER;
5160}
5161
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005162Preproc nasmpp = {
5163 pp_reset,
5164 pp_getline,
5165 pp_cleanup
5166};