blob: af466637369c24430c80c98ad17508a3ccb07720 [file] [log] [blame]
H. Peter Anvin9e6747c2009-06-28 17:13:04 -07001/* ----------------------------------------------------------------------- *
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002 *
3 * Copyright 1996-2010 The NASM Authors - All Rights Reserved
H. Peter Anvin9e6747c2009-06-28 17:13:04 -07004 * See the file AUTHORS included with the NASM distribution for
5 * the specific copyright holders.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00006 *
H. Peter Anvin9e6747c2009-06-28 17:13:04 -07007 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following
9 * conditions are met:
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000010 *
H. Peter Anvin9e6747c2009-06-28 17:13:04 -070011 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above
14 * copyright notice, this list of conditions and the following
15 * disclaimer in the documentation and/or other materials provided
16 * with the distribution.
Cyrill Gorcunovaccda192010-02-16 10:27:56 +030017 *
H. Peter Anvin9e6747c2009-06-28 17:13:04 -070018 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
19 * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
20 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
21 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
23 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
26 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
29 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
30 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 *
32 * ----------------------------------------------------------------------- */
33
34/*
35 * preproc.c macro preprocessor for the Netwide Assembler
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000036 */
37
H. Peter Anvin4836e332002-04-30 20:56:43 +000038/* Typical flow of text through preproc
39 *
Keith Kaniosb7a89542007-04-12 02:40:54 +000040 * pp_getline gets tokenized lines, either
H. Peter Anvin4836e332002-04-30 20:56:43 +000041 *
42 * from a macro expansion
43 *
44 * or
45 * {
46 * read_line gets raw text from stdmacpos, or predef, or current input file
Keith Kaniosb7a89542007-04-12 02:40:54 +000047 * tokenize converts to tokens
H. Peter Anvin4836e332002-04-30 20:56:43 +000048 * }
49 *
50 * expand_mmac_params is used to expand %1 etc., unless a macro is being
51 * defined or a false conditional is being processed
52 * (%0, %1, %+1, %-1, %%foo
53 *
54 * do_directive checks for directives
55 *
56 * expand_smacro is used to expand single line macros
57 *
58 * expand_mmacro is used to expand multi-line macros
59 *
60 * detoken is used to convert the line back to text
61 */
H. Peter Anvineba20a72002-04-30 20:53:55 +000062
H. Peter Anvinfe501952007-10-02 21:53:51 -070063#include "compiler.h"
64
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000065#include <stdio.h>
H. Peter Anvinaf535c12002-04-30 20:59:21 +000066#include <stdarg.h>
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000067#include <stdlib.h>
68#include <stddef.h>
69#include <string.h>
70#include <ctype.h>
H. Peter Anvin76690a12002-04-30 20:52:49 +000071#include <limits.h>
Keith Kaniosb7a89542007-04-12 02:40:54 +000072#include <inttypes.h>
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000073
74#include "nasm.h"
75#include "nasmlib.h"
H. Peter Anvin4169a472007-09-12 01:29:43 +000076#include "preproc.h"
H. Peter Anvin97a23472007-09-16 17:57:25 -070077#include "hashtbl.h"
H. Peter Anvin8cad14b2008-06-01 17:23:51 -070078#include "quote.h"
H. Peter Anvinc2df2822007-10-24 15:29:28 -070079#include "stdscan.h"
H. Peter Anvindbb640b2009-07-18 18:57:16 -070080#include "eval.h"
H. Peter Anvinc2df2822007-10-24 15:29:28 -070081#include "tokens.h"
H. Peter Anvina4835d42008-05-20 14:21:29 -070082#include "tables.h"
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000083
84typedef struct SMacro SMacro;
85typedef struct MMacro MMacro;
Keith Kanios891775e2009-07-11 06:08:54 -050086typedef struct MMacroInvocation MMacroInvocation;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000087typedef struct Context Context;
88typedef struct Token Token;
H. Peter Anvince616072002-04-30 21:02:23 +000089typedef struct Blocks Blocks;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000090typedef struct Line Line;
91typedef struct Include Include;
92typedef struct Cond Cond;
H. Peter Anvin6768eb72002-04-30 20:52:26 +000093typedef struct IncPath IncPath;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000094
95/*
H. Peter Anvin97a23472007-09-16 17:57:25 -070096 * Note on the storage of both SMacro and MMacros: the hash table
97 * indexes them case-insensitively, and we then have to go through a
98 * linked list of potential case aliases (and, for MMacros, parameter
99 * ranges); this is to preserve the matching semantics of the earlier
100 * code. If the number of case aliases for a specific macro is a
101 * performance issue, you may want to reconsider your coding style.
102 */
103
104/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000105 * Store the definition of a single-line macro.
106 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000107struct SMacro {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000108 SMacro *next;
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000109 char *name;
H. Peter Anvin70055962007-10-11 00:05:31 -0700110 bool casesense;
H. Peter Anvin16ed4382007-10-11 10:06:19 -0700111 bool in_progress;
H. Peter Anvin70055962007-10-11 00:05:31 -0700112 unsigned int nparam;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000113 Token *expansion;
114};
115
116/*
H. Peter Anvin76690a12002-04-30 20:52:49 +0000117 * Store the definition of a multi-line macro. This is also used to
118 * store the interiors of `%rep...%endrep' blocks, which are
119 * effectively self-re-invoking multi-line macros which simply
120 * don't have a name or bother to appear in the hash tables. %rep
121 * blocks are signified by having a NULL `name' field.
122 *
123 * In a MMacro describing a `%rep' block, the `in_progress' field
124 * isn't merely boolean, but gives the number of repeats left to
125 * run.
126 *
127 * The `next' field is used for storing MMacros in hash tables; the
128 * `next_active' field is for stacking them on istk entries.
129 *
130 * When a MMacro is being expanded, `params', `iline', `nparam',
131 * `paramlen', `rotate' and `unique' are local to the invocation.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000132 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000133struct MMacro {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000134 MMacro *next;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300135 MMacroInvocation *prev; /* previous invocation */
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000136 char *name;
H. Peter Anvin8cfdb9d2007-09-14 18:36:01 -0700137 int nparam_min, nparam_max;
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700138 bool casesense;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300139 bool plus; /* is the last parameter greedy? */
140 bool nolist; /* is this macro listing-inhibited? */
141 int64_t in_progress; /* is this macro currently being expanded? */
142 int32_t max_depth; /* maximum number of recursive expansions allowed */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000143 Token *dlist; /* All defaults as one list */
144 Token **defaults; /* Parameter default pointers */
145 int ndefs; /* number of default parameters */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000146 Line *expansion;
H. Peter Anvin76690a12002-04-30 20:52:49 +0000147
148 MMacro *next_active;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000149 MMacro *rep_nest; /* used for nesting %rep */
150 Token **params; /* actual parameters */
151 Token *iline; /* invocation line */
H. Peter Anvin25a99342007-09-22 17:45:45 -0700152 unsigned int nparam, rotate;
153 int *paramlen;
H. Peter Anvinf8ba53e2007-10-11 10:11:57 -0700154 uint64_t unique;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000155 int lineno; /* Current line number on expansion */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300156 uint64_t condcnt; /* number of if blocks... */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000157};
158
Keith Kanios891775e2009-07-11 06:08:54 -0500159
160/* Store the definition of a multi-line macro, as defined in a
161 * previous recursive macro expansion.
162 */
163struct MMacroInvocation {
H. Peter Anvin89cee572009-07-15 09:16:54 -0400164 MMacroInvocation *prev; /* previous invocation */
165 Token **params; /* actual parameters */
166 Token *iline; /* invocation line */
Keith Kanios891775e2009-07-11 06:08:54 -0500167 unsigned int nparam, rotate;
168 int *paramlen;
169 uint64_t unique;
Keith Kanios3c0d91f2009-10-25 13:28:03 -0500170 uint64_t condcnt;
Keith Kanios891775e2009-07-11 06:08:54 -0500171};
172
173
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000174/*
175 * The context stack is composed of a linked list of these.
176 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000177struct Context {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000178 Context *next;
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000179 char *name;
H. Peter Anvin166c2472008-05-28 12:28:58 -0700180 struct hash_table localmac;
Keith Kaniosb7a89542007-04-12 02:40:54 +0000181 uint32_t number;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000182};
183
184/*
185 * This is the internal form which we break input lines up into.
186 * Typically stored in linked lists.
187 *
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000188 * Note that `type' serves a double meaning: TOK_SMAC_PARAM is not
189 * necessarily used as-is, but is intended to denote the number of
190 * the substituted parameter. So in the definition
191 *
192 * %define a(x,y) ( (x) & ~(y) )
H. Peter Anvin70653092007-10-19 14:42:29 -0700193 *
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000194 * the token representing `x' will have its type changed to
195 * TOK_SMAC_PARAM, but the one representing `y' will be
196 * TOK_SMAC_PARAM+1.
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000197 *
198 * TOK_INTERNAL_STRING is a dirty hack: it's a single string token
199 * which doesn't need quotes around it. Used in the pre-include
200 * mechanism as an alternative to trying to find a sensible type of
201 * quote to use on the filename we were passed.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000202 */
H. Peter Anvinda10e7b2007-09-12 04:18:37 +0000203enum pp_token_type {
204 TOK_NONE = 0, TOK_WHITESPACE, TOK_COMMENT, TOK_ID,
205 TOK_PREPROC_ID, TOK_STRING,
H. Peter Anvin6c81f0a2008-05-25 21:46:17 -0700206 TOK_NUMBER, TOK_FLOAT, TOK_SMAC_END, TOK_OTHER,
207 TOK_INTERNAL_STRING,
208 TOK_PREPROC_Q, TOK_PREPROC_QQ,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300209 TOK_PASTE, /* %+ */
210 TOK_INDIRECT, /* %[...] */
211 TOK_SMAC_PARAM, /* MUST BE LAST IN THE LIST!!! */
212 TOK_MAX = INT_MAX /* Keep compiler from reducing the range */
H. Peter Anvinda10e7b2007-09-12 04:18:37 +0000213};
214
H. Peter Anvine2c80182005-01-15 22:15:51 +0000215struct Token {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000216 Token *next;
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000217 char *text;
H. Peter Anvinf26e0972008-07-01 21:26:27 -0700218 union {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300219 SMacro *mac; /* associated macro for TOK_SMAC_END */
220 size_t len; /* scratch length field */
221 } a; /* Auxiliary data */
H. Peter Anvinda10e7b2007-09-12 04:18:37 +0000222 enum pp_token_type type;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000223};
224
225/*
226 * Multi-line macro definitions are stored as a linked list of
227 * these, which is essentially a container to allow several linked
228 * lists of Tokens.
H. Peter Anvin70653092007-10-19 14:42:29 -0700229 *
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000230 * Note that in this module, linked lists are treated as stacks
231 * wherever possible. For this reason, Lines are _pushed_ on to the
232 * `expansion' field in MMacro structures, so that the linked list,
233 * if walked, would give the macro lines in reverse order; this
234 * means that we can walk the list when expanding a macro, and thus
235 * push the lines on to the `expansion' field in _istk_ in reverse
236 * order (so that when popped back off they are in the right
237 * order). It may seem cockeyed, and it relies on my design having
238 * an even number of steps in, but it works...
239 *
240 * Some of these structures, rather than being actual lines, are
241 * markers delimiting the end of the expansion of a given macro.
H. Peter Anvin76690a12002-04-30 20:52:49 +0000242 * This is for use in the cycle-tracking and %rep-handling code.
243 * Such structures have `finishes' non-NULL, and `first' NULL. All
244 * others have `finishes' NULL, but `first' may still be NULL if
245 * the line is blank.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000246 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000247struct Line {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000248 Line *next;
249 MMacro *finishes;
250 Token *first;
251};
252
253/*
254 * To handle an arbitrary level of file inclusion, we maintain a
255 * stack (ie linked list) of these things.
256 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000257struct Include {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000258 Include *next;
259 FILE *fp;
260 Cond *conds;
261 Line *expansion;
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000262 char *fname;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000263 int lineno, lineinc;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300264 MMacro *mstk; /* stack of active macros/reps */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000265};
266
267/*
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000268 * Include search path. This is simply a list of strings which get
269 * prepended, in turn, to the name of an include file, in an
270 * attempt to find the file if it's not in the current directory.
271 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000272struct IncPath {
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000273 IncPath *next;
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000274 char *path;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000275};
276
277/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000278 * Conditional assembly: we maintain a separate stack of these for
279 * each level of file inclusion. (The only reason we keep the
280 * stacks separate is to ensure that a stray `%endif' in a file
281 * included from within the true branch of a `%if' won't terminate
282 * it and cause confusion: instead, rightly, it'll cause an error.)
283 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000284struct Cond {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000285 Cond *next;
286 int state;
287};
H. Peter Anvine2c80182005-01-15 22:15:51 +0000288enum {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000289 /*
290 * These states are for use just after %if or %elif: IF_TRUE
291 * means the condition has evaluated to truth so we are
292 * currently emitting, whereas IF_FALSE means we are not
293 * currently emitting but will start doing so if a %else comes
294 * up. In these states, all directives are admissible: %elif,
295 * %else and %endif. (And of course %if.)
296 */
297 COND_IF_TRUE, COND_IF_FALSE,
298 /*
299 * These states come up after a %else: ELSE_TRUE means we're
300 * emitting, and ELSE_FALSE means we're not. In ELSE_* states,
301 * any %elif or %else will cause an error.
302 */
303 COND_ELSE_TRUE, COND_ELSE_FALSE,
304 /*
Victor van den Elzen3b404c02008-09-18 13:51:36 +0200305 * These states mean that we're not emitting now, and also that
306 * nothing until %endif will be emitted at all. COND_DONE is
307 * used when we've had our moment of emission
308 * and have now started seeing %elifs. COND_NEVER is used when
309 * the condition construct in question is contained within a
310 * non-emitting branch of a larger condition construct,
311 * or if there is an error.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000312 */
Victor van den Elzen3b404c02008-09-18 13:51:36 +0200313 COND_DONE, COND_NEVER
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000314};
315#define emitting(x) ( (x) == COND_IF_TRUE || (x) == COND_ELSE_TRUE )
316
H. Peter Anvin70653092007-10-19 14:42:29 -0700317/*
Ed Beroset3ab3f412002-06-11 03:31:49 +0000318 * These defines are used as the possible return values for do_directive
319 */
320#define NO_DIRECTIVE_FOUND 0
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300321#define DIRECTIVE_FOUND 1
Ed Beroset3ab3f412002-06-11 03:31:49 +0000322
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000323/*
Keith Kanios852f1ee2009-07-12 00:19:55 -0500324 * This define sets the upper limit for smacro and recursive mmacro
325 * expansions
326 */
327#define DEADMAN_LIMIT (1 << 20)
328
Cyrill Gorcunove091d6e2010-08-09 13:58:22 +0400329/* max reps */
330#define REP_LIMIT ((INT64_C(1) << 62))
331
Keith Kanios852f1ee2009-07-12 00:19:55 -0500332/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000333 * Condition codes. Note that we use c_ prefix not C_ because C_ is
334 * used in nasm.h for the "real" condition codes. At _this_ level,
335 * we treat CXZ and ECXZ as condition codes, albeit non-invertible
336 * ones, so we need a different enum...
337 */
H. Peter Anvin476d2862007-10-02 22:04:15 -0700338static const char * const conditions[] = {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000339 "a", "ae", "b", "be", "c", "cxz", "e", "ecxz", "g", "ge", "l", "le",
340 "na", "nae", "nb", "nbe", "nc", "ne", "ng", "nge", "nl", "nle", "no",
H. Peter Anvince9be342007-09-12 00:22:29 +0000341 "np", "ns", "nz", "o", "p", "pe", "po", "rcxz", "s", "z"
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000342};
H. Peter Anvin476d2862007-10-02 22:04:15 -0700343enum pp_conds {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000344 c_A, c_AE, c_B, c_BE, c_C, c_CXZ, c_E, c_ECXZ, c_G, c_GE, c_L, c_LE,
345 c_NA, c_NAE, c_NB, c_NBE, c_NC, c_NE, c_NG, c_NGE, c_NL, c_NLE, c_NO,
H. Peter Anvin476d2862007-10-02 22:04:15 -0700346 c_NP, c_NS, c_NZ, c_O, c_P, c_PE, c_PO, c_RCXZ, c_S, c_Z,
347 c_none = -1
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000348};
H. Peter Anvin476d2862007-10-02 22:04:15 -0700349static const enum pp_conds inverse_ccs[] = {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000350 c_NA, c_NAE, c_NB, c_NBE, c_NC, -1, c_NE, -1, c_NG, c_NGE, c_NL, c_NLE,
351 c_A, c_AE, c_B, c_BE, c_C, c_E, c_G, c_GE, c_L, c_LE, c_O, c_P, c_S,
H. Peter Anvince9be342007-09-12 00:22:29 +0000352 c_Z, c_NO, c_NP, c_PO, c_PE, -1, c_NS, c_NZ
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000353};
354
H. Peter Anvin76690a12002-04-30 20:52:49 +0000355/*
356 * Directive names.
357 */
H. Peter Anvin65747262002-05-07 00:10:05 +0000358/* If this is a an IF, ELIF, ELSE or ENDIF keyword */
H. Peter Anvin9bf0aa72007-09-12 02:12:07 +0000359static int is_condition(enum preproc_token arg)
H. Peter Anvin65747262002-05-07 00:10:05 +0000360{
H. Peter Anvinda10e7b2007-09-12 04:18:37 +0000361 return PP_IS_COND(arg) || (arg == PP_ELSE) || (arg == PP_ENDIF);
H. Peter Anvin65747262002-05-07 00:10:05 +0000362}
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000363
364/* For TASM compatibility we need to be able to recognise TASM compatible
365 * conditional compilation directives. Using the NASM pre-processor does
366 * not work, so we look for them specifically from the following list and
367 * then jam in the equivalent NASM directive into the input stream.
368 */
369
H. Peter Anvine2c80182005-01-15 22:15:51 +0000370enum {
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000371 TM_ARG, TM_ELIF, TM_ELSE, TM_ENDIF, TM_IF, TM_IFDEF, TM_IFDIFI,
372 TM_IFNDEF, TM_INCLUDE, TM_LOCAL
373};
374
H. Peter Anvin476d2862007-10-02 22:04:15 -0700375static const char * const tasm_directives[] = {
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000376 "arg", "elif", "else", "endif", "if", "ifdef", "ifdifi",
377 "ifndef", "include", "local"
378};
379
380static int StackSize = 4;
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000381static char *StackPointer = "ebp";
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000382static int ArgOffset = 8;
H. Peter Anvin8781cb02007-11-08 20:01:11 -0800383static int LocalOffset = 0;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000384
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000385static Context *cstk;
386static Include *istk;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000387static IncPath *ipath = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000388
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300389static int pass; /* HACK: pass 0 = generate dependencies only */
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700390static StrList **dephead, **deptail; /* Dependency list */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000391
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300392static uint64_t unique; /* unique identifier numbers */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000393
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000394static Line *predef = NULL;
H. Peter Anvind2456592008-06-19 15:04:18 -0700395static bool do_predef;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000396
397static ListGen *list;
398
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000399/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000400 * The current set of multi-line macros we have defined.
401 */
H. Peter Anvin166c2472008-05-28 12:28:58 -0700402static struct hash_table mmacros;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000403
404/*
405 * The current set of single-line macros we have defined.
406 */
H. Peter Anvin166c2472008-05-28 12:28:58 -0700407static struct hash_table smacros;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000408
409/*
H. Peter Anvin76690a12002-04-30 20:52:49 +0000410 * The multi-line macro we are currently defining, or the %rep
411 * block we are currently reading, if any.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000412 */
413static MMacro *defining;
414
Charles Crayned4200be2008-07-12 16:42:33 -0700415static uint64_t nested_mac_count;
416static uint64_t nested_rep_count;
417
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000418/*
419 * The number of macro parameters to allocate space for at a time.
420 */
421#define PARAM_DELTA 16
422
423/*
H. Peter Anvina4835d42008-05-20 14:21:29 -0700424 * The standard macro set: defined in macros.c in the array nasm_stdmac.
425 * This gives our position in the macro set, when we're processing it.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000426 */
H. Peter Anvina70547f2008-07-19 21:44:26 -0700427static macros_t *stdmacpos;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000428
429/*
H. Peter Anvin76690a12002-04-30 20:52:49 +0000430 * The extra standard macros that come from the object format, if
431 * any.
432 */
H. Peter Anvina70547f2008-07-19 21:44:26 -0700433static macros_t *extrastdmac = NULL;
H. Peter Anvincfb71762008-06-20 15:20:16 -0700434static bool any_extrastdmac;
H. Peter Anvin76690a12002-04-30 20:52:49 +0000435
436/*
H. Peter Anvin734b1882002-04-30 21:01:08 +0000437 * Tokens are allocated in blocks to improve speed
438 */
439#define TOKEN_BLOCKSIZE 4096
440static Token *freeTokens = NULL;
H. Peter Anvince616072002-04-30 21:02:23 +0000441struct Blocks {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000442 Blocks *next;
443 void *chunk;
H. Peter Anvince616072002-04-30 21:02:23 +0000444};
445
446static Blocks blocks = { NULL, NULL };
H. Peter Anvin734b1882002-04-30 21:01:08 +0000447
448/*
H. Peter Anvin76690a12002-04-30 20:52:49 +0000449 * Forward declarations.
450 */
H. Peter Anvin734b1882002-04-30 21:01:08 +0000451static Token *expand_mmac_params(Token * tline);
452static Token *expand_smacro(Token * tline);
453static Token *expand_id(Token * tline);
H. Peter Anvinf8ad5322009-02-21 17:55:08 -0800454static Context *get_ctx(const char *name, const char **namep,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300455 bool all_contexts);
Keith Kaniosa5fc6462007-10-13 07:09:22 -0700456static void make_tok_num(Token * tok, int64_t val);
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000457static void error(int severity, const char *fmt, ...);
Victor van den Elzen3b404c02008-09-18 13:51:36 +0200458static void error_precond(int severity, const char *fmt, ...);
H. Peter Anvince616072002-04-30 21:02:23 +0000459static void *new_Block(size_t size);
460static void delete_Blocks(void);
H. Peter Anvinc751e862008-06-09 10:18:45 -0700461static Token *new_Token(Token * next, enum pp_token_type type,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300462 const char *text, int txtlen);
H. Peter Anvin734b1882002-04-30 21:01:08 +0000463static Token *delete_Token(Token * t);
H. Peter Anvineba20a72002-04-30 20:53:55 +0000464
465/*
466 * Macros for safe checking of token pointers, avoid *(NULL)
467 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300468#define tok_type_(x,t) ((x) && (x)->type == (t))
469#define skip_white_(x) if (tok_type_((x), TOK_WHITESPACE)) (x)=(x)->next
470#define tok_is_(x,v) (tok_type_((x), TOK_OTHER) && !strcmp((x)->text,(v)))
471#define tok_isnt_(x,v) ((x) && ((x)->type!=TOK_OTHER || strcmp((x)->text,(v))))
H. Peter Anvin76690a12002-04-30 20:52:49 +0000472
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300473/*
H. Peter Anvin077fb932010-07-20 14:56:30 -0700474 * nasm_unquote with error if the string contains NUL characters.
475 * If the string contains NUL characters, issue an error and return
476 * the C len, i.e. truncate at the NUL.
477 */
478static size_t nasm_unquote_cstr(char *qstr, enum preproc_token directive)
479{
480 size_t len = nasm_unquote(qstr, NULL);
481 size_t clen = strlen(qstr);
482
483 if (len != clen)
484 error(ERR_NONFATAL, "NUL character in `%s' directive",
485 pp_directives[directive]);
486
487 return clen;
488}
489
490/*
H. Peter Anvinb40992c2010-09-15 08:57:21 -0700491 * In-place reverse a list of tokens.
492 */
493static Token *reverse_tokens(Token *t)
494{
495 Token *prev = NULL;
496 Token *next;
497
498 while (t) {
499 next = t->next;
500 t->next = prev;
501 prev = t;
502 t = next;
503 }
504
505 return prev;
506}
507
508/*
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300509 * Handle TASM specific directives, which do not contain a % in
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000510 * front of them. We do it here because I could not find any other
511 * place to do it for the moment, and it is a hack (ideally it would
512 * be nice to be able to use the NASM pre-processor to do it).
513 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000514static char *check_tasm_directive(char *line)
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000515{
Keith Kaniosb7a89542007-04-12 02:40:54 +0000516 int32_t i, j, k, m, len;
Cyrill Gorcunovf66ac7d2009-10-12 20:41:13 +0400517 char *p, *q, *oldline, oldchar;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000518
Cyrill Gorcunovf66ac7d2009-10-12 20:41:13 +0400519 p = nasm_skip_spaces(line);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000520
521 /* Binary search for the directive name */
522 i = -1;
Cyrill Gorcunova7319242010-06-03 22:04:36 +0400523 j = ARRAY_SIZE(tasm_directives);
Cyrill Gorcunovf66ac7d2009-10-12 20:41:13 +0400524 q = nasm_skip_word(p);
525 len = q - p;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000526 if (len) {
527 oldchar = p[len];
528 p[len] = 0;
529 while (j - i > 1) {
530 k = (j + i) / 2;
531 m = nasm_stricmp(p, tasm_directives[k]);
532 if (m == 0) {
533 /* We have found a directive, so jam a % in front of it
534 * so that NASM will then recognise it as one if it's own.
535 */
536 p[len] = oldchar;
537 len = strlen(p);
538 oldline = line;
539 line = nasm_malloc(len + 2);
540 line[0] = '%';
541 if (k == TM_IFDIFI) {
H. Peter Anvin18f48792009-06-27 15:56:27 -0700542 /*
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300543 * NASM does not recognise IFDIFI, so we convert
544 * it to %if 0. This is not used in NASM
545 * compatible code, but does need to parse for the
546 * TASM macro package.
H. Peter Anvine2c80182005-01-15 22:15:51 +0000547 */
H. Peter Anvin18f48792009-06-27 15:56:27 -0700548 strcpy(line + 1, "if 0");
H. Peter Anvine2c80182005-01-15 22:15:51 +0000549 } else {
550 memcpy(line + 1, p, len + 1);
551 }
552 nasm_free(oldline);
553 return line;
554 } else if (m < 0) {
555 j = k;
556 } else
557 i = k;
558 }
559 p[len] = oldchar;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000560 }
561 return line;
562}
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000563
H. Peter Anvin76690a12002-04-30 20:52:49 +0000564/*
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000565 * The pre-preprocessing stage... This function translates line
566 * number indications as they emerge from GNU cpp (`# lineno "file"
567 * flags') into NASM preprocessor line number indications (`%line
568 * lineno file').
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000569 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000570static char *prepreproc(char *line)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000571{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000572 int lineno, fnlen;
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000573 char *fname, *oldline;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000574
H. Peter Anvine2c80182005-01-15 22:15:51 +0000575 if (line[0] == '#' && line[1] == ' ') {
576 oldline = line;
577 fname = oldline + 2;
578 lineno = atoi(fname);
579 fname += strspn(fname, "0123456789 ");
580 if (*fname == '"')
581 fname++;
582 fnlen = strcspn(fname, "\"");
583 line = nasm_malloc(20 + fnlen);
584 snprintf(line, 20 + fnlen, "%%line %d %.*s", lineno, fnlen, fname);
585 nasm_free(oldline);
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000586 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000587 if (tasm_compatible_mode)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000588 return check_tasm_directive(line);
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000589 return line;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000590}
591
592/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000593 * Free a linked list of tokens.
594 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000595static void free_tlist(Token * list)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000596{
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +0400597 while (list)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000598 list = delete_Token(list);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000599}
600
601/*
602 * Free a linked list of lines.
603 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000604static void free_llist(Line * list)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000605{
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +0400606 Line *l, *tmp;
607 list_for_each_safe(l, tmp, list) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000608 free_tlist(l->first);
609 nasm_free(l);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000610 }
611}
612
613/*
H. Peter Anvineba20a72002-04-30 20:53:55 +0000614 * Free an MMacro
615 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000616static void free_mmacro(MMacro * m)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000617{
H. Peter Anvin734b1882002-04-30 21:01:08 +0000618 nasm_free(m->name);
619 free_tlist(m->dlist);
620 nasm_free(m->defaults);
621 free_llist(m->expansion);
622 nasm_free(m);
H. Peter Anvineba20a72002-04-30 20:53:55 +0000623}
624
625/*
H. Peter Anvin97a23472007-09-16 17:57:25 -0700626 * Free all currently defined macros, and free the hash tables
627 */
H. Peter Anvin072771e2008-05-22 13:17:51 -0700628static void free_smacro_table(struct hash_table *smt)
H. Peter Anvin97a23472007-09-16 17:57:25 -0700629{
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +0400630 SMacro *s, *tmp;
H. Peter Anvin072771e2008-05-22 13:17:51 -0700631 const char *key;
632 struct hash_tbl_node *it = NULL;
H. Peter Anvin97a23472007-09-16 17:57:25 -0700633
H. Peter Anvin072771e2008-05-22 13:17:51 -0700634 while ((s = hash_iterate(smt, &it, &key)) != NULL) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300635 nasm_free((void *)key);
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +0400636 list_for_each_safe(s, tmp, s) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300637 nasm_free(s->name);
638 free_tlist(s->expansion);
639 nasm_free(s);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300640 }
H. Peter Anvin97a23472007-09-16 17:57:25 -0700641 }
H. Peter Anvin072771e2008-05-22 13:17:51 -0700642 hash_free(smt);
643}
644
645static void free_mmacro_table(struct hash_table *mmt)
646{
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +0400647 MMacro *m, *tmp;
H. Peter Anvin072771e2008-05-22 13:17:51 -0700648 const char *key;
649 struct hash_tbl_node *it = NULL;
H. Peter Anvin97a23472007-09-16 17:57:25 -0700650
651 it = NULL;
H. Peter Anvin072771e2008-05-22 13:17:51 -0700652 while ((m = hash_iterate(mmt, &it, &key)) != NULL) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300653 nasm_free((void *)key);
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +0400654 list_for_each_safe(m ,tmp, m)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300655 free_mmacro(m);
H. Peter Anvin97a23472007-09-16 17:57:25 -0700656 }
H. Peter Anvin072771e2008-05-22 13:17:51 -0700657 hash_free(mmt);
658}
659
660static void free_macros(void)
661{
H. Peter Anvin166c2472008-05-28 12:28:58 -0700662 free_smacro_table(&smacros);
663 free_mmacro_table(&mmacros);
H. Peter Anvin97a23472007-09-16 17:57:25 -0700664}
665
666/*
667 * Initialize the hash tables
668 */
669static void init_macros(void)
670{
H. Peter Anvin166c2472008-05-28 12:28:58 -0700671 hash_init(&smacros, HASH_LARGE);
672 hash_init(&mmacros, HASH_LARGE);
H. Peter Anvin97a23472007-09-16 17:57:25 -0700673}
674
675/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000676 * Pop the context stack.
677 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000678static void ctx_pop(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000679{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000680 Context *c = cstk;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000681
682 cstk = cstk->next;
H. Peter Anvin166c2472008-05-28 12:28:58 -0700683 free_smacro_table(&c->localmac);
H. Peter Anvin734b1882002-04-30 21:01:08 +0000684 nasm_free(c->name);
685 nasm_free(c);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000686}
687
H. Peter Anvin072771e2008-05-22 13:17:51 -0700688/*
689 * Search for a key in the hash index; adding it if necessary
690 * (in which case we initialize the data pointer to NULL.)
691 */
692static void **
693hash_findi_add(struct hash_table *hash, const char *str)
694{
695 struct hash_insert hi;
696 void **r;
697 char *strx;
698
699 r = hash_findi(hash, str, &hi);
700 if (r)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300701 return r;
H. Peter Anvin072771e2008-05-22 13:17:51 -0700702
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300703 strx = nasm_strdup(str); /* Use a more efficient allocator here? */
H. Peter Anvin072771e2008-05-22 13:17:51 -0700704 return hash_add(&hi, strx, NULL);
705}
706
707/*
708 * Like hash_findi, but returns the data element rather than a pointer
709 * to it. Used only when not adding a new element, hence no third
710 * argument.
711 */
712static void *
713hash_findix(struct hash_table *hash, const char *str)
714{
715 void **p;
716
717 p = hash_findi(hash, str, NULL);
718 return p ? *p : NULL;
719}
720
Cyrill Gorcunov15bdc512010-07-13 11:27:41 +0400721/*
722 * read line from standart macros set,
723 * if there no more left -- return NULL
724 */
725static char *line_from_stdmac(void)
726{
727 unsigned char c;
728 const unsigned char *p = stdmacpos;
729 char *line, *q;
730 size_t len = 0;
731
732 if (!stdmacpos)
733 return NULL;
734
735 while ((c = *p++)) {
736 if (c >= 0x80)
737 len += pp_directives_len[c - 0x80] + 1;
738 else
739 len++;
740 }
741
742 line = nasm_malloc(len + 1);
743 q = line;
744 while ((c = *stdmacpos++)) {
745 if (c >= 0x80) {
746 memcpy(q, pp_directives[c - 0x80], pp_directives_len[c - 0x80]);
747 q += pp_directives_len[c - 0x80];
748 *q++ = ' ';
749 } else {
750 *q++ = c;
751 }
752 }
753 stdmacpos = p;
754 *q = '\0';
755
756 if (!*stdmacpos) {
757 /* This was the last of the standard macro chain... */
758 stdmacpos = NULL;
759 if (any_extrastdmac) {
760 stdmacpos = extrastdmac;
761 any_extrastdmac = false;
762 } else if (do_predef) {
763 Line *pd, *l;
764 Token *head, **tail, *t;
765
766 /*
767 * Nasty hack: here we push the contents of
768 * `predef' on to the top-level expansion stack,
769 * since this is the most convenient way to
770 * implement the pre-include and pre-define
771 * features.
772 */
773 list_for_each(pd, predef) {
774 head = NULL;
775 tail = &head;
776 list_for_each(t, pd->first) {
777 *tail = new_Token(NULL, t->type, t->text, 0);
778 tail = &(*tail)->next;
779 }
780
781 l = nasm_malloc(sizeof(Line));
782 l->next = istk->expansion;
783 l->first = head;
784 l->finishes = NULL;
785
786 istk->expansion = l;
787 }
788 do_predef = false;
789 }
790 }
791
792 return line;
793}
794
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000795#define BUF_DELTA 512
796/*
797 * Read a line from the top file in istk, handling multiple CR/LFs
798 * at the end of the line read, and handling spurious ^Zs. Will
799 * return lines from the standard macro set if this has not already
800 * been done.
801 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000802static char *read_line(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000803{
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000804 char *buffer, *p, *q;
H. Peter Anvin9f394642002-04-30 21:07:51 +0000805 int bufsize, continued_count;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000806
Cyrill Gorcunov15bdc512010-07-13 11:27:41 +0400807 /*
808 * standart macros set (predefined) goes first
809 */
810 p = line_from_stdmac();
811 if (p)
812 return p;
H. Peter Anvin72edbb82008-06-19 16:00:04 -0700813
Cyrill Gorcunov15bdc512010-07-13 11:27:41 +0400814 /*
815 * regular read from a file
816 */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000817 bufsize = BUF_DELTA;
818 buffer = nasm_malloc(BUF_DELTA);
819 p = buffer;
H. Peter Anvin9f394642002-04-30 21:07:51 +0000820 continued_count = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000821 while (1) {
822 q = fgets(p, bufsize - (p - buffer), istk->fp);
823 if (!q)
824 break;
825 p += strlen(p);
826 if (p > buffer && p[-1] == '\n') {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300827 /*
828 * Convert backslash-CRLF line continuation sequences into
829 * nothing at all (for DOS and Windows)
830 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000831 if (((p - 2) > buffer) && (p[-3] == '\\') && (p[-2] == '\r')) {
832 p -= 3;
833 *p = 0;
834 continued_count++;
835 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300836 /*
837 * Also convert backslash-LF line continuation sequences into
838 * nothing at all (for Unix)
839 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000840 else if (((p - 1) > buffer) && (p[-2] == '\\')) {
841 p -= 2;
842 *p = 0;
843 continued_count++;
844 } else {
845 break;
846 }
847 }
848 if (p - buffer > bufsize - 10) {
Keith Kaniosb7a89542007-04-12 02:40:54 +0000849 int32_t offset = p - buffer;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000850 bufsize += BUF_DELTA;
851 buffer = nasm_realloc(buffer, bufsize);
852 p = buffer + offset; /* prevent stale-pointer problems */
853 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000854 }
855
H. Peter Anvine2c80182005-01-15 22:15:51 +0000856 if (!q && p == buffer) {
857 nasm_free(buffer);
858 return NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000859 }
860
H. Peter Anvine2c80182005-01-15 22:15:51 +0000861 src_set_linnum(src_get_linnum() + istk->lineinc +
862 (continued_count * istk->lineinc));
H. Peter Anvineba20a72002-04-30 20:53:55 +0000863
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000864 /*
865 * Play safe: remove CRs as well as LFs, if any of either are
866 * present at the end of the line.
867 */
H. Peter Anvineba20a72002-04-30 20:53:55 +0000868 while (--p >= buffer && (*p == '\n' || *p == '\r'))
H. Peter Anvine2c80182005-01-15 22:15:51 +0000869 *p = '\0';
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000870
871 /*
872 * Handle spurious ^Z, which may be inserted into source files
873 * by some file transfer utilities.
874 */
875 buffer[strcspn(buffer, "\032")] = '\0';
876
H. Peter Anvin734b1882002-04-30 21:01:08 +0000877 list->line(LIST_READ, buffer);
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000878
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000879 return buffer;
880}
881
882/*
Keith Kaniosb7a89542007-04-12 02:40:54 +0000883 * Tokenize a line of text. This is a very simple process since we
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000884 * don't need to parse the value out of e.g. numeric tokens: we
885 * simply split one string into many.
886 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000887static Token *tokenize(char *line)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000888{
H. Peter Anvinca544db2008-10-19 19:30:11 -0700889 char c, *p = line;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +0000890 enum pp_token_type type;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000891 Token *list = NULL;
892 Token *t, **tail = &list;
893
H. Peter Anvine2c80182005-01-15 22:15:51 +0000894 while (*line) {
895 p = line;
896 if (*p == '%') {
897 p++;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300898 if (*p == '+' && !nasm_isdigit(p[1])) {
899 p++;
900 type = TOK_PASTE;
901 } else if (nasm_isdigit(*p) ||
902 ((*p == '-' || *p == '+') && nasm_isdigit(p[1]))) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000903 do {
904 p++;
905 }
H. Peter Anvinbda7a6e2008-06-21 10:23:17 -0700906 while (nasm_isdigit(*p));
H. Peter Anvine2c80182005-01-15 22:15:51 +0000907 type = TOK_PREPROC_ID;
908 } else if (*p == '{') {
909 p++;
Cyrill Gorcunov0457bcb2010-10-07 19:30:54 +0400910 while (*p) {
911 if (*p == '}')
912 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000913 p[-1] = *p;
914 p++;
915 }
Cyrill Gorcunov0457bcb2010-10-07 19:30:54 +0400916 if (*p != '}')
917 error(ERR_WARNING | ERR_PASS1, "unterminated %{ construct");
H. Peter Anvine2c80182005-01-15 22:15:51 +0000918 p[-1] = '\0';
919 if (*p)
920 p++;
921 type = TOK_PREPROC_ID;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300922 } else if (*p == '[') {
923 int lvl = 1;
924 line += 2; /* Skip the leading %[ */
925 p++;
926 while (lvl && (c = *p++)) {
927 switch (c) {
928 case ']':
929 lvl--;
930 break;
931 case '%':
932 if (*p == '[')
933 lvl++;
934 break;
935 case '\'':
936 case '\"':
937 case '`':
Cyrill Gorcunovc6360a72010-07-13 13:32:19 +0400938 p = nasm_skip_string(p - 1) + 1;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300939 break;
940 default:
941 break;
942 }
943 }
944 p--;
945 if (*p)
946 *p++ = '\0';
947 if (lvl)
948 error(ERR_NONFATAL, "unterminated %[ construct");
949 type = TOK_INDIRECT;
950 } else if (*p == '?') {
951 type = TOK_PREPROC_Q; /* %? */
952 p++;
953 if (*p == '?') {
954 type = TOK_PREPROC_QQ; /* %?? */
955 p++;
956 }
H. Peter Anvin077fb932010-07-20 14:56:30 -0700957 } else if (*p == '!') {
958 type = TOK_PREPROC_ID;
959 p++;
960 if (isidchar(*p)) {
961 do {
962 p++;
963 }
964 while (isidchar(*p));
965 } else if (*p == '\'' || *p == '\"' || *p == '`') {
966 p = nasm_skip_string(p);
967 if (*p)
968 p++;
969 else
970 error(ERR_NONFATAL|ERR_PASS1, "unterminated %! string");
971 } else {
972 /* %! without string or identifier */
973 type = TOK_OTHER; /* Legacy behavior... */
974 }
H. Peter Anvine2c80182005-01-15 22:15:51 +0000975 } else if (isidchar(*p) ||
976 ((*p == '!' || *p == '%' || *p == '$') &&
977 isidchar(p[1]))) {
978 do {
979 p++;
980 }
981 while (isidchar(*p));
982 type = TOK_PREPROC_ID;
983 } else {
984 type = TOK_OTHER;
985 if (*p == '%')
986 p++;
987 }
988 } else if (isidstart(*p) || (*p == '$' && isidstart(p[1]))) {
989 type = TOK_ID;
990 p++;
991 while (*p && isidchar(*p))
992 p++;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -0700993 } else if (*p == '\'' || *p == '"' || *p == '`') {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000994 /*
995 * A string token.
996 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000997 type = TOK_STRING;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300998 p = nasm_skip_string(p);
Nickolay Yurchenkof3b3ce22003-09-21 20:38:43 +0000999
H. Peter Anvine2c80182005-01-15 22:15:51 +00001000 if (*p) {
1001 p++;
1002 } else {
H. Peter Anvin917a3492008-09-24 09:14:49 -07001003 error(ERR_WARNING|ERR_PASS1, "unterminated string");
H. Peter Anvine2c80182005-01-15 22:15:51 +00001004 /* Handling unterminated strings by UNV */
1005 /* type = -1; */
1006 }
Victor van den Elzenfb5f2512009-04-17 16:17:59 +02001007 } else if (p[0] == '$' && p[1] == '$') {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001008 type = TOK_OTHER; /* TOKEN_BASE */
Victor van den Elzenfb5f2512009-04-17 16:17:59 +02001009 p += 2;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001010 } else if (isnumstart(*p)) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001011 bool is_hex = false;
1012 bool is_float = false;
1013 bool has_e = false;
1014 char c, *r;
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001015
H. Peter Anvine2c80182005-01-15 22:15:51 +00001016 /*
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001017 * A numeric token.
H. Peter Anvine2c80182005-01-15 22:15:51 +00001018 */
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001019
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001020 if (*p == '$') {
1021 p++;
1022 is_hex = true;
1023 }
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001024
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001025 for (;;) {
1026 c = *p++;
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001027
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001028 if (!is_hex && (c == 'e' || c == 'E')) {
1029 has_e = true;
1030 if (*p == '+' || *p == '-') {
1031 /*
1032 * e can only be followed by +/- if it is either a
1033 * prefixed hex number or a floating-point number
1034 */
1035 p++;
1036 is_float = true;
1037 }
1038 } else if (c == 'H' || c == 'h' || c == 'X' || c == 'x') {
1039 is_hex = true;
1040 } else if (c == 'P' || c == 'p') {
1041 is_float = true;
1042 if (*p == '+' || *p == '-')
1043 p++;
1044 } else if (isnumchar(c) || c == '_')
1045 ; /* just advance */
1046 else if (c == '.') {
1047 /*
1048 * we need to deal with consequences of the legacy
1049 * parser, like "1.nolist" being two tokens
1050 * (TOK_NUMBER, TOK_ID) here; at least give it
1051 * a shot for now. In the future, we probably need
1052 * a flex-based scanner with proper pattern matching
1053 * to do it as well as it can be done. Nothing in
1054 * the world is going to help the person who wants
1055 * 0x123.p16 interpreted as two tokens, though.
1056 */
1057 r = p;
1058 while (*r == '_')
1059 r++;
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001060
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001061 if (nasm_isdigit(*r) || (is_hex && nasm_isxdigit(*r)) ||
1062 (!is_hex && (*r == 'e' || *r == 'E')) ||
1063 (*r == 'p' || *r == 'P')) {
1064 p = r;
1065 is_float = true;
1066 } else
1067 break; /* Terminate the token */
1068 } else
1069 break;
1070 }
1071 p--; /* Point to first character beyond number */
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001072
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001073 if (p == line+1 && *line == '$') {
1074 type = TOK_OTHER; /* TOKEN_HERE */
1075 } else {
1076 if (has_e && !is_hex) {
1077 /* 1e13 is floating-point, but 1e13h is not */
1078 is_float = true;
1079 }
H. Peter Anvind784a082009-04-20 14:01:18 -07001080
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001081 type = is_float ? TOK_FLOAT : TOK_NUMBER;
1082 }
H. Peter Anvinbda7a6e2008-06-21 10:23:17 -07001083 } else if (nasm_isspace(*p)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001084 type = TOK_WHITESPACE;
Cyrill Gorcunovf66ac7d2009-10-12 20:41:13 +04001085 p = nasm_skip_spaces(p);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001086 /*
1087 * Whitespace just before end-of-line is discarded by
1088 * pretending it's a comment; whitespace just before a
1089 * comment gets lumped into the comment.
1090 */
1091 if (!*p || *p == ';') {
1092 type = TOK_COMMENT;
1093 while (*p)
1094 p++;
1095 }
1096 } else if (*p == ';') {
1097 type = TOK_COMMENT;
1098 while (*p)
1099 p++;
1100 } else {
1101 /*
1102 * Anything else is an operator of some kind. We check
1103 * for all the double-character operators (>>, <<, //,
1104 * %%, <=, >=, ==, !=, <>, &&, ||, ^^), but anything
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001105 * else is a single-character operator.
H. Peter Anvine2c80182005-01-15 22:15:51 +00001106 */
1107 type = TOK_OTHER;
1108 if ((p[0] == '>' && p[1] == '>') ||
1109 (p[0] == '<' && p[1] == '<') ||
1110 (p[0] == '/' && p[1] == '/') ||
1111 (p[0] == '<' && p[1] == '=') ||
1112 (p[0] == '>' && p[1] == '=') ||
1113 (p[0] == '=' && p[1] == '=') ||
1114 (p[0] == '!' && p[1] == '=') ||
1115 (p[0] == '<' && p[1] == '>') ||
1116 (p[0] == '&' && p[1] == '&') ||
1117 (p[0] == '|' && p[1] == '|') ||
1118 (p[0] == '^' && p[1] == '^')) {
1119 p++;
1120 }
1121 p++;
1122 }
Nickolay Yurchenkof3b3ce22003-09-21 20:38:43 +00001123
H. Peter Anvine2c80182005-01-15 22:15:51 +00001124 /* Handling unterminated string by UNV */
1125 /*if (type == -1)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001126 {
1127 *tail = t = new_Token(NULL, TOK_STRING, line, p-line+1);
1128 t->text[p-line] = *line;
1129 tail = &t->next;
1130 }
1131 else */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001132 if (type != TOK_COMMENT) {
1133 *tail = t = new_Token(NULL, type, line, p - line);
1134 tail = &t->next;
1135 }
1136 line = p;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001137 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001138 return list;
1139}
1140
H. Peter Anvince616072002-04-30 21:02:23 +00001141/*
1142 * this function allocates a new managed block of memory and
H. Peter Anvin70653092007-10-19 14:42:29 -07001143 * returns a pointer to the block. The managed blocks are
H. Peter Anvince616072002-04-30 21:02:23 +00001144 * deleted only all at once by the delete_Blocks function.
1145 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001146static void *new_Block(size_t size)
H. Peter Anvince616072002-04-30 21:02:23 +00001147{
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001148 Blocks *b = &blocks;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001149
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001150 /* first, get to the end of the linked list */
1151 while (b->next)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001152 b = b->next;
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001153 /* now allocate the requested chunk */
1154 b->chunk = nasm_malloc(size);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001155
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001156 /* now allocate a new block for the next request */
1157 b->next = nasm_malloc(sizeof(Blocks));
1158 /* and initialize the contents of the new block */
1159 b->next->next = NULL;
1160 b->next->chunk = NULL;
1161 return b->chunk;
H. Peter Anvince616072002-04-30 21:02:23 +00001162}
1163
1164/*
1165 * this function deletes all managed blocks of memory
1166 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001167static void delete_Blocks(void)
H. Peter Anvince616072002-04-30 21:02:23 +00001168{
H. Peter Anvine2c80182005-01-15 22:15:51 +00001169 Blocks *a, *b = &blocks;
H. Peter Anvince616072002-04-30 21:02:23 +00001170
H. Peter Anvin70653092007-10-19 14:42:29 -07001171 /*
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001172 * keep in mind that the first block, pointed to by blocks
H. Peter Anvin70653092007-10-19 14:42:29 -07001173 * is a static and not dynamically allocated, so we don't
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001174 * free it.
1175 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001176 while (b) {
1177 if (b->chunk)
1178 nasm_free(b->chunk);
1179 a = b;
1180 b = b->next;
1181 if (a != &blocks)
1182 nasm_free(a);
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001183 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001184}
H. Peter Anvin734b1882002-04-30 21:01:08 +00001185
1186/*
H. Peter Anvin70653092007-10-19 14:42:29 -07001187 * this function creates a new Token and passes a pointer to it
H. Peter Anvin734b1882002-04-30 21:01:08 +00001188 * back to the caller. It sets the type and text elements, and
H. Peter Anvinf26e0972008-07-01 21:26:27 -07001189 * also the a.mac and next elements to NULL.
H. Peter Anvin734b1882002-04-30 21:01:08 +00001190 */
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07001191static Token *new_Token(Token * next, enum pp_token_type type,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001192 const char *text, int txtlen)
H. Peter Anvin734b1882002-04-30 21:01:08 +00001193{
1194 Token *t;
1195 int i;
1196
H. Peter Anvin89cee572009-07-15 09:16:54 -04001197 if (!freeTokens) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001198 freeTokens = (Token *) new_Block(TOKEN_BLOCKSIZE * sizeof(Token));
1199 for (i = 0; i < TOKEN_BLOCKSIZE - 1; i++)
1200 freeTokens[i].next = &freeTokens[i + 1];
1201 freeTokens[i].next = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001202 }
1203 t = freeTokens;
1204 freeTokens = t->next;
1205 t->next = next;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07001206 t->a.mac = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001207 t->type = type;
H. Peter Anvin89cee572009-07-15 09:16:54 -04001208 if (type == TOK_WHITESPACE || !text) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001209 t->text = NULL;
1210 } else {
1211 if (txtlen == 0)
1212 txtlen = strlen(text);
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07001213 t->text = nasm_malloc(txtlen+1);
1214 memcpy(t->text, text, txtlen);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001215 t->text[txtlen] = '\0';
H. Peter Anvin734b1882002-04-30 21:01:08 +00001216 }
1217 return t;
1218}
1219
H. Peter Anvine2c80182005-01-15 22:15:51 +00001220static Token *delete_Token(Token * t)
H. Peter Anvin734b1882002-04-30 21:01:08 +00001221{
1222 Token *next = t->next;
1223 nasm_free(t->text);
H. Peter Anvin788e6c12002-04-30 21:02:01 +00001224 t->next = freeTokens;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001225 freeTokens = t;
1226 return next;
1227}
1228
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001229/*
1230 * Convert a line of tokens back into text.
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001231 * If expand_locals is not zero, identifiers of the form "%$*xxx"
1232 * will be transformed into ..@ctxnum.xxx
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001233 */
H. Peter Anvin9e200162008-06-04 17:23:14 -07001234static char *detoken(Token * tlist, bool expand_locals)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001235{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001236 Token *t;
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001237 char *line, *p;
H. Peter Anvinb4daadc2008-01-21 16:31:57 -08001238 const char *q;
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001239 int len = 0;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001240
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001241 list_for_each(t, tlist) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001242 if (t->type == TOK_PREPROC_ID && t->text[1] == '!') {
H. Peter Anvin077fb932010-07-20 14:56:30 -07001243 char *v;
1244 char *q = t->text;
1245
1246 v = t->text + 2;
1247 if (*v == '\'' || *v == '\"' || *v == '`') {
1248 size_t len = nasm_unquote(v, NULL);
1249 size_t clen = strlen(v);
1250
1251 if (len != clen) {
1252 error(ERR_NONFATAL | ERR_PASS1,
1253 "NUL character in %! string");
1254 v = NULL;
1255 }
H. Peter Anvin5b00bf42010-07-13 11:43:06 -07001256 }
H. Peter Anvin077fb932010-07-20 14:56:30 -07001257
1258 if (v) {
1259 char *p = getenv(v);
1260 if (!p) {
1261 error(ERR_NONFATAL | ERR_PASS1,
1262 "nonexistent environment variable `%s'", v);
1263 p = "";
1264 }
1265 t->text = nasm_strdup(p);
1266 }
1267 nasm_free(q);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001268 }
H. Peter Anvin077fb932010-07-20 14:56:30 -07001269
H. Peter Anvine2c80182005-01-15 22:15:51 +00001270 /* Expand local macros here and not during preprocessing */
1271 if (expand_locals &&
1272 t->type == TOK_PREPROC_ID && t->text &&
1273 t->text[0] == '%' && t->text[1] == '$') {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001274 const char *q;
1275 char *p;
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001276 Context *ctx = get_ctx(t->text, &q, false);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001277 if (ctx) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001278 char buffer[40];
Keith Kanios93f2e9a2007-04-14 00:10:59 +00001279 snprintf(buffer, sizeof(buffer), "..@%"PRIu32".", ctx->number);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001280 p = nasm_strcat(buffer, q);
1281 nasm_free(t->text);
1282 t->text = p;
1283 }
1284 }
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001285 if (t->type == TOK_WHITESPACE)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001286 len++;
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001287 else if (t->text)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001288 len += strlen(t->text);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001289 }
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001290
H. Peter Anvin734b1882002-04-30 21:01:08 +00001291 p = line = nasm_malloc(len + 1);
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001292
1293 list_for_each(t, tlist) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001294 if (t->type == TOK_WHITESPACE) {
H. Peter Anvinb4daadc2008-01-21 16:31:57 -08001295 *p++ = ' ';
H. Peter Anvine2c80182005-01-15 22:15:51 +00001296 } else if (t->text) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001297 q = t->text;
1298 while (*q)
1299 *p++ = *q++;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001300 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001301 }
1302 *p = '\0';
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001303
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001304 return line;
1305}
1306
1307/*
H. Peter Anvin76690a12002-04-30 20:52:49 +00001308 * A scanner, suitable for use by the expression evaluator, which
1309 * operates on a line of Tokens. Expects a pointer to a pointer to
1310 * the first token in the line to be passed in as its private_data
1311 * field.
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001312 *
1313 * FIX: This really needs to be unified with stdscan.
H. Peter Anvin76690a12002-04-30 20:52:49 +00001314 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001315static int ppscan(void *private_data, struct tokenval *tokval)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001316{
H. Peter Anvin76690a12002-04-30 20:52:49 +00001317 Token **tlineptr = private_data;
1318 Token *tline;
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001319 char ourcopy[MAX_KEYWORD+1], *p, *r, *s;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001320
H. Peter Anvine2c80182005-01-15 22:15:51 +00001321 do {
1322 tline = *tlineptr;
1323 *tlineptr = tline ? tline->next : NULL;
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04001324 } while (tline && (tline->type == TOK_WHITESPACE ||
1325 tline->type == TOK_COMMENT));
H. Peter Anvin76690a12002-04-30 20:52:49 +00001326
1327 if (!tline)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001328 return tokval->t_type = TOKEN_EOS;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001329
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001330 tokval->t_charptr = tline->text;
1331
H. Peter Anvin76690a12002-04-30 20:52:49 +00001332 if (tline->text[0] == '$' && !tline->text[1])
H. Peter Anvine2c80182005-01-15 22:15:51 +00001333 return tokval->t_type = TOKEN_HERE;
H. Peter Anvin7cf897e2002-05-30 21:30:33 +00001334 if (tline->text[0] == '$' && tline->text[1] == '$' && !tline->text[2])
H. Peter Anvine2c80182005-01-15 22:15:51 +00001335 return tokval->t_type = TOKEN_BASE;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001336
H. Peter Anvine2c80182005-01-15 22:15:51 +00001337 if (tline->type == TOK_ID) {
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001338 p = tokval->t_charptr = tline->text;
1339 if (p[0] == '$') {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001340 tokval->t_charptr++;
1341 return tokval->t_type = TOKEN_ID;
1342 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00001343
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001344 for (r = p, s = ourcopy; *r; r++) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001345 if (r >= p+MAX_KEYWORD)
1346 return tokval->t_type = TOKEN_ID; /* Not a keyword */
H. Peter Anvinac8f8fc2008-06-11 15:49:41 -07001347 *s++ = nasm_tolower(*r);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001348 }
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001349 *s = '\0';
1350 /* right, so we have an identifier sitting in temp storage. now,
1351 * is it actually a register or instruction name, or what? */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001352 return nasm_token_hash(ourcopy, tokval);
H. Peter Anvin76690a12002-04-30 20:52:49 +00001353 }
1354
H. Peter Anvine2c80182005-01-15 22:15:51 +00001355 if (tline->type == TOK_NUMBER) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001356 bool rn_error;
1357 tokval->t_integer = readnum(tline->text, &rn_error);
1358 tokval->t_charptr = tline->text;
1359 if (rn_error)
1360 return tokval->t_type = TOKEN_ERRNUM;
1361 else
1362 return tokval->t_type = TOKEN_NUM;
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001363 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00001364
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001365 if (tline->type == TOK_FLOAT) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001366 return tokval->t_type = TOKEN_FLOAT;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001367 }
1368
H. Peter Anvine2c80182005-01-15 22:15:51 +00001369 if (tline->type == TOK_STRING) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001370 char bq, *ep;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001371
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001372 bq = tline->text[0];
H. Peter Anvin11627042008-06-09 20:45:19 -07001373 tokval->t_charptr = tline->text;
1374 tokval->t_inttwo = nasm_unquote(tline->text, &ep);
H. Peter Anvind2456592008-06-19 15:04:18 -07001375
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001376 if (ep[0] != bq || ep[1] != '\0')
1377 return tokval->t_type = TOKEN_ERRSTR;
1378 else
1379 return tokval->t_type = TOKEN_STR;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001380 }
1381
H. Peter Anvine2c80182005-01-15 22:15:51 +00001382 if (tline->type == TOK_OTHER) {
1383 if (!strcmp(tline->text, "<<"))
1384 return tokval->t_type = TOKEN_SHL;
1385 if (!strcmp(tline->text, ">>"))
1386 return tokval->t_type = TOKEN_SHR;
1387 if (!strcmp(tline->text, "//"))
1388 return tokval->t_type = TOKEN_SDIV;
1389 if (!strcmp(tline->text, "%%"))
1390 return tokval->t_type = TOKEN_SMOD;
1391 if (!strcmp(tline->text, "=="))
1392 return tokval->t_type = TOKEN_EQ;
1393 if (!strcmp(tline->text, "<>"))
1394 return tokval->t_type = TOKEN_NE;
1395 if (!strcmp(tline->text, "!="))
1396 return tokval->t_type = TOKEN_NE;
1397 if (!strcmp(tline->text, "<="))
1398 return tokval->t_type = TOKEN_LE;
1399 if (!strcmp(tline->text, ">="))
1400 return tokval->t_type = TOKEN_GE;
1401 if (!strcmp(tline->text, "&&"))
1402 return tokval->t_type = TOKEN_DBL_AND;
1403 if (!strcmp(tline->text, "^^"))
1404 return tokval->t_type = TOKEN_DBL_XOR;
1405 if (!strcmp(tline->text, "||"))
1406 return tokval->t_type = TOKEN_DBL_OR;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001407 }
1408
1409 /*
1410 * We have no other options: just return the first character of
1411 * the token text.
1412 */
1413 return tokval->t_type = tline->text[0];
1414}
1415
1416/*
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001417 * Compare a string to the name of an existing macro; this is a
1418 * simple wrapper which calls either strcmp or nasm_stricmp
1419 * depending on the value of the `casesense' parameter.
1420 */
H. Peter Anvin4db5a162007-10-11 13:42:09 -07001421static int mstrcmp(const char *p, const char *q, bool casesense)
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001422{
H. Peter Anvin734b1882002-04-30 21:01:08 +00001423 return casesense ? strcmp(p, q) : nasm_stricmp(p, q);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001424}
1425
1426/*
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07001427 * Compare a string to the name of an existing macro; this is a
1428 * simple wrapper which calls either strcmp or nasm_stricmp
1429 * depending on the value of the `casesense' parameter.
1430 */
1431static int mmemcmp(const char *p, const char *q, size_t l, bool casesense)
1432{
1433 return casesense ? memcmp(p, q, l) : nasm_memicmp(p, q, l);
1434}
1435
1436/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001437 * Return the Context structure associated with a %$ token. Return
1438 * NULL, having _already_ reported an error condition, if the
1439 * context stack isn't deep enough for the supplied number of $
1440 * signs.
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001441 * If all_contexts == true, contexts that enclose current are
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001442 * also scanned for such smacro, until it is found; if not -
1443 * only the context that directly results from the number of $'s
1444 * in variable's name.
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001445 *
1446 * If "namep" is non-NULL, set it to the pointer to the macro name
1447 * tail, i.e. the part beyond %$...
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001448 */
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001449static Context *get_ctx(const char *name, const char **namep,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001450 bool all_contexts)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001451{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001452 Context *ctx;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001453 SMacro *m;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001454 int i;
1455
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001456 if (namep)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001457 *namep = name;
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001458
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001459 if (!name || name[0] != '%' || name[1] != '$')
H. Peter Anvine2c80182005-01-15 22:15:51 +00001460 return NULL;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001461
H. Peter Anvine2c80182005-01-15 22:15:51 +00001462 if (!cstk) {
1463 error(ERR_NONFATAL, "`%s': context stack is empty", name);
1464 return NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001465 }
1466
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001467 name += 2;
1468 ctx = cstk;
1469 i = 0;
1470 while (ctx && *name == '$') {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001471 name++;
1472 i++;
1473 ctx = ctx->next;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001474 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001475 if (!ctx) {
1476 error(ERR_NONFATAL, "`%s': context stack is only"
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001477 " %d level%s deep", name, i, (i == 1 ? "" : "s"));
H. Peter Anvine2c80182005-01-15 22:15:51 +00001478 return NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001479 }
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001480
1481 if (namep)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001482 *namep = name;
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001483
Keith Kanios404589e2010-08-10 20:12:57 -05001484 if (!all_contexts)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001485 return ctx;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001486
Cyrill Gorcunovd90693c2010-08-11 20:31:46 +04001487 /*
1488 * NOTE: In 2.10 we will not need lookup in extarnal
1489 * contexts, so this is a gentle way to inform users
1490 * about their source code need to be updated
1491 */
1492
1493 /* first round -- check the current context */
1494 m = hash_findix(&ctx->localmac, name);
1495 while (m) {
1496 if (!mstrcmp(m->name, name, m->casesense))
1497 return ctx;
1498 m = m->next;
1499 }
1500
1501 /* second round - external contexts */
1502 while ((ctx = ctx->next)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001503 /* Search for this smacro in found context */
H. Peter Anvin166c2472008-05-28 12:28:58 -07001504 m = hash_findix(&ctx->localmac, name);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001505 while (m) {
Keith Kanios404589e2010-08-10 20:12:57 -05001506 if (!mstrcmp(m->name, name, m->casesense)) {
Keith Kanios88d947e2010-08-14 12:47:45 -05001507 /* NOTE: deprecated as of 2.10 */
Cyrill Gorcunovd90693c2010-08-11 20:31:46 +04001508 static int once = 0;
1509 if (!once) {
1510 error(ERR_WARNING, "context-local macro expansion"
Keith Kanios88d947e2010-08-14 12:47:45 -05001511 " fall-through (automatic searching of outer"
1512 " contexts) will be deprecated starting in"
1513 " NASM 2.10, please see the NASM Manual for"
1514 " more information");
Cyrill Gorcunovd90693c2010-08-11 20:31:46 +04001515 once = 1;
1516 }
Keith Kanios88d947e2010-08-14 12:47:45 -05001517 error(ERR_WARNING, "`%s': context-local macro expansion fall-through", name);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001518 return ctx;
Cyrill Gorcunovd90693c2010-08-11 20:31:46 +04001519 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001520 m = m->next;
1521 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00001522 }
Cyrill Gorcunovd90693c2010-08-11 20:31:46 +04001523
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001524 return NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001525}
1526
1527/*
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001528 * Check to see if a file is already in a string list
1529 */
1530static bool in_list(const StrList *list, const char *str)
1531{
1532 while (list) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001533 if (!strcmp(list->str, str))
1534 return true;
1535 list = list->next;
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001536 }
1537 return false;
1538}
1539
1540/*
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001541 * Open an include file. This routine must always return a valid
1542 * file pointer if it returns - it's responsible for throwing an
1543 * ERR_FATAL and bombing out completely if not. It should also try
1544 * the include path one by one until it finds the file or reaches
1545 * the end of the path.
1546 */
H. Peter Anvin2b1c3b92008-06-06 10:38:46 -07001547static FILE *inc_fopen(const char *file, StrList **dhead, StrList ***dtail,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001548 bool missing_ok)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001549{
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001550 FILE *fp;
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001551 char *prefix = "";
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001552 IncPath *ip = ipath;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001553 int len = strlen(file);
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001554 size_t prefix_len = 0;
1555 StrList *sl;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001556
H. Peter Anvine2c80182005-01-15 22:15:51 +00001557 while (1) {
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001558 sl = nasm_malloc(prefix_len+len+1+sizeof sl->next);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001559 memcpy(sl->str, prefix, prefix_len);
1560 memcpy(sl->str+prefix_len, file, len+1);
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001561 fp = fopen(sl->str, "r");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001562 if (fp && dhead && !in_list(*dhead, sl->str)) {
1563 sl->next = NULL;
1564 **dtail = sl;
1565 *dtail = &sl->next;
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001566 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001567 nasm_free(sl);
1568 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001569 if (fp)
1570 return fp;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001571 if (!ip) {
1572 if (!missing_ok)
1573 break;
1574 prefix = NULL;
1575 } else {
1576 prefix = ip->path;
1577 ip = ip->next;
1578 }
1579 if (prefix) {
1580 prefix_len = strlen(prefix);
1581 } else {
1582 /* -MG given and file not found */
1583 if (dhead && !in_list(*dhead, file)) {
1584 sl = nasm_malloc(len+1+sizeof sl->next);
1585 sl->next = NULL;
1586 strcpy(sl->str, file);
1587 **dtail = sl;
1588 *dtail = &sl->next;
1589 }
1590 return NULL;
1591 }
H. Peter Anvineba20a72002-04-30 20:53:55 +00001592 }
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001593
H. Peter Anvin734b1882002-04-30 21:01:08 +00001594 error(ERR_FATAL, "unable to open include file `%s'", file);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001595 return NULL;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001596}
1597
1598/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001599 * Determine if we should warn on defining a single-line macro of
H. Peter Anvinef7468f2002-04-30 20:57:59 +00001600 * name `name', with `nparam' parameters. If nparam is 0 or -1, will
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001601 * return true if _any_ single-line macro of that name is defined.
1602 * Otherwise, will return true if a single-line macro with either
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001603 * `nparam' or no parameters is defined.
1604 *
1605 * If a macro with precisely the right number of parameters is
H. Peter Anvinef7468f2002-04-30 20:57:59 +00001606 * defined, or nparam is -1, the address of the definition structure
1607 * will be returned in `defn'; otherwise NULL will be returned. If `defn'
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001608 * is NULL, no action will be taken regarding its contents, and no
1609 * error will occur.
1610 *
1611 * Note that this is also called with nparam zero to resolve
1612 * `ifdef'.
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001613 *
1614 * If you already know which context macro belongs to, you can pass
1615 * the context pointer as first parameter; if you won't but name begins
1616 * with %$ the context will be automatically computed. If all_contexts
1617 * is true, macro will be searched in outer contexts as well.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001618 */
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07001619static bool
H. Peter Anvinb2a5fda2008-06-19 21:42:42 -07001620smacro_defined(Context * ctx, const char *name, int nparam, SMacro ** defn,
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07001621 bool nocase)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001622{
H. Peter Anvin166c2472008-05-28 12:28:58 -07001623 struct hash_table *smtbl;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001624 SMacro *m;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001625
H. Peter Anvin97a23472007-09-16 17:57:25 -07001626 if (ctx) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001627 smtbl = &ctx->localmac;
H. Peter Anvin97a23472007-09-16 17:57:25 -07001628 } else if (name[0] == '%' && name[1] == '$') {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001629 if (cstk)
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001630 ctx = get_ctx(name, &name, false);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001631 if (!ctx)
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001632 return false; /* got to return _something_ */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001633 smtbl = &ctx->localmac;
H. Peter Anvin97a23472007-09-16 17:57:25 -07001634 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001635 smtbl = &smacros;
H. Peter Anvin97a23472007-09-16 17:57:25 -07001636 }
H. Peter Anvin166c2472008-05-28 12:28:58 -07001637 m = (SMacro *) hash_findix(smtbl, name);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001638
H. Peter Anvine2c80182005-01-15 22:15:51 +00001639 while (m) {
1640 if (!mstrcmp(m->name, name, m->casesense && nocase) &&
Charles Crayne192d5b52007-10-18 19:02:42 -07001641 (nparam <= 0 || m->nparam == 0 || nparam == (int) m->nparam)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001642 if (defn) {
Charles Crayne192d5b52007-10-18 19:02:42 -07001643 if (nparam == (int) m->nparam || nparam == -1)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001644 *defn = m;
1645 else
1646 *defn = NULL;
1647 }
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001648 return true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001649 }
1650 m = m->next;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001651 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001652
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001653 return false;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001654}
1655
1656/*
1657 * Count and mark off the parameters in a multi-line macro call.
1658 * This is called both from within the multi-line macro expansion
1659 * code, and also to mark off the default parameters when provided
1660 * in a %macro definition line.
1661 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001662static void count_mmac_params(Token * t, int *nparam, Token *** params)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001663{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001664 int paramsize, brace;
1665
1666 *nparam = paramsize = 0;
1667 *params = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001668 while (t) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001669 /* +1: we need space for the final NULL */
H. Peter Anvin91fb6f12008-09-01 10:56:33 -07001670 if (*nparam+1 >= paramsize) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001671 paramsize += PARAM_DELTA;
1672 *params = nasm_realloc(*params, sizeof(**params) * paramsize);
1673 }
1674 skip_white_(t);
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001675 brace = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001676 if (tok_is_(t, "{"))
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001677 brace = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001678 (*params)[(*nparam)++] = t;
1679 while (tok_isnt_(t, brace ? "}" : ","))
1680 t = t->next;
1681 if (t) { /* got a comma/brace */
1682 t = t->next;
1683 if (brace) {
1684 /*
1685 * Now we've found the closing brace, look further
1686 * for the comma.
1687 */
1688 skip_white_(t);
1689 if (tok_isnt_(t, ",")) {
1690 error(ERR_NONFATAL,
1691 "braces do not enclose all of macro parameter");
1692 while (tok_isnt_(t, ","))
1693 t = t->next;
1694 }
1695 if (t)
1696 t = t->next; /* eat the comma */
1697 }
1698 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001699 }
1700}
1701
1702/*
H. Peter Anvin76690a12002-04-30 20:52:49 +00001703 * Determine whether one of the various `if' conditions is true or
1704 * not.
1705 *
1706 * We must free the tline we get passed.
1707 */
H. Peter Anvin70055962007-10-11 00:05:31 -07001708static bool if_condition(Token * tline, enum preproc_token ct)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001709{
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001710 enum pp_conditional i = PP_COND(ct);
H. Peter Anvin70055962007-10-11 00:05:31 -07001711 bool j;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001712 Token *t, *tt, **tptr, *origline;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001713 struct tokenval tokval;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001714 expr *evalresult;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001715 enum pp_token_type needtype;
H. Peter Anvin077fb932010-07-20 14:56:30 -07001716 char *p;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001717
1718 origline = tline;
1719
H. Peter Anvine2c80182005-01-15 22:15:51 +00001720 switch (i) {
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001721 case PPC_IFCTX:
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001722 j = false; /* have we matched yet? */
Victor van den Elzen0e857f12008-07-23 13:21:29 +02001723 while (true) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001724 skip_white_(tline);
Victor van den Elzen0e857f12008-07-23 13:21:29 +02001725 if (!tline)
1726 break;
1727 if (tline->type != TOK_ID) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001728 error(ERR_NONFATAL,
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001729 "`%s' expects context identifiers", pp_directives[ct]);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001730 free_tlist(origline);
1731 return -1;
1732 }
Victor van den Elzen0e857f12008-07-23 13:21:29 +02001733 if (cstk && cstk->name && !nasm_stricmp(tline->text, cstk->name))
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001734 j = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001735 tline = tline->next;
1736 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001737 break;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001738
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001739 case PPC_IFDEF:
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001740 j = false; /* have we matched yet? */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001741 while (tline) {
1742 skip_white_(tline);
1743 if (!tline || (tline->type != TOK_ID &&
1744 (tline->type != TOK_PREPROC_ID ||
1745 tline->text[1] != '$'))) {
1746 error(ERR_NONFATAL,
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001747 "`%s' expects macro identifiers", pp_directives[ct]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001748 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001749 }
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07001750 if (smacro_defined(NULL, tline->text, 0, NULL, true))
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001751 j = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001752 tline = tline->next;
1753 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001754 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001755
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001756 case PPC_IFENV:
1757 tline = expand_smacro(tline);
1758 j = false; /* have we matched yet? */
1759 while (tline) {
1760 skip_white_(tline);
1761 if (!tline || (tline->type != TOK_ID &&
H. Peter Anvin077fb932010-07-20 14:56:30 -07001762 tline->type != TOK_STRING &&
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001763 (tline->type != TOK_PREPROC_ID ||
H. Peter Anvin077fb932010-07-20 14:56:30 -07001764 tline->text[1] != '!'))) {
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001765 error(ERR_NONFATAL,
1766 "`%s' expects environment variable names",
1767 pp_directives[ct]);
1768 goto fail;
1769 }
H. Peter Anvin077fb932010-07-20 14:56:30 -07001770 p = tline->text;
1771 if (tline->type == TOK_PREPROC_ID)
1772 p += 2; /* Skip leading %! */
1773 if (*p == '\'' || *p == '\"' || *p == '`')
1774 nasm_unquote_cstr(p, ct);
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001775 if (getenv(p))
1776 j = true;
1777 tline = tline->next;
1778 }
1779 break;
1780
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001781 case PPC_IFIDN:
1782 case PPC_IFIDNI:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001783 tline = expand_smacro(tline);
1784 t = tt = tline;
1785 while (tok_isnt_(tt, ","))
1786 tt = tt->next;
1787 if (!tt) {
1788 error(ERR_NONFATAL,
1789 "`%s' expects two comma-separated arguments",
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001790 pp_directives[ct]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001791 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001792 }
1793 tt = tt->next;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001794 j = true; /* assume equality unless proved not */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001795 while ((t->type != TOK_OTHER || strcmp(t->text, ",")) && tt) {
1796 if (tt->type == TOK_OTHER && !strcmp(tt->text, ",")) {
1797 error(ERR_NONFATAL, "`%s': more than one comma on line",
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001798 pp_directives[ct]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001799 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001800 }
1801 if (t->type == TOK_WHITESPACE) {
1802 t = t->next;
1803 continue;
1804 }
1805 if (tt->type == TOK_WHITESPACE) {
1806 tt = tt->next;
1807 continue;
1808 }
1809 if (tt->type != t->type) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001810 j = false; /* found mismatching tokens */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001811 break;
1812 }
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07001813 /* When comparing strings, need to unquote them first */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001814 if (t->type == TOK_STRING) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001815 size_t l1 = nasm_unquote(t->text, NULL);
1816 size_t l2 = nasm_unquote(tt->text, NULL);
H. Peter Anvind2456592008-06-19 15:04:18 -07001817
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001818 if (l1 != l2) {
1819 j = false;
1820 break;
1821 }
1822 if (mmemcmp(t->text, tt->text, l1, i == PPC_IFIDN)) {
1823 j = false;
1824 break;
1825 }
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07001826 } else if (mstrcmp(tt->text, t->text, i == PPC_IFIDN) != 0) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001827 j = false; /* found mismatching tokens */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001828 break;
1829 }
Nickolay Yurchenkof3b3ce22003-09-21 20:38:43 +00001830
H. Peter Anvine2c80182005-01-15 22:15:51 +00001831 t = t->next;
1832 tt = tt->next;
1833 }
1834 if ((t->type != TOK_OTHER || strcmp(t->text, ",")) || tt)
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001835 j = false; /* trailing gunk on one end or other */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001836 break;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001837
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001838 case PPC_IFMACRO:
H. Peter Anvin89cee572009-07-15 09:16:54 -04001839 {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001840 bool found = false;
1841 MMacro searching, *mmac;
H. Peter Anvin65747262002-05-07 00:10:05 +00001842
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001843 skip_white_(tline);
1844 tline = expand_id(tline);
1845 if (!tok_type_(tline, TOK_ID)) {
1846 error(ERR_NONFATAL,
1847 "`%s' expects a macro name", pp_directives[ct]);
1848 goto fail;
1849 }
1850 searching.name = nasm_strdup(tline->text);
1851 searching.casesense = true;
1852 searching.plus = false;
1853 searching.nolist = false;
1854 searching.in_progress = 0;
1855 searching.max_depth = 0;
1856 searching.rep_nest = NULL;
1857 searching.nparam_min = 0;
1858 searching.nparam_max = INT_MAX;
1859 tline = expand_smacro(tline->next);
1860 skip_white_(tline);
1861 if (!tline) {
1862 } else if (!tok_type_(tline, TOK_NUMBER)) {
1863 error(ERR_NONFATAL,
1864 "`%s' expects a parameter count or nothing",
1865 pp_directives[ct]);
1866 } else {
1867 searching.nparam_min = searching.nparam_max =
1868 readnum(tline->text, &j);
1869 if (j)
1870 error(ERR_NONFATAL,
1871 "unable to parse parameter count `%s'",
1872 tline->text);
1873 }
1874 if (tline && tok_is_(tline->next, "-")) {
1875 tline = tline->next->next;
1876 if (tok_is_(tline, "*"))
1877 searching.nparam_max = INT_MAX;
1878 else if (!tok_type_(tline, TOK_NUMBER))
1879 error(ERR_NONFATAL,
1880 "`%s' expects a parameter count after `-'",
1881 pp_directives[ct]);
1882 else {
1883 searching.nparam_max = readnum(tline->text, &j);
1884 if (j)
1885 error(ERR_NONFATAL,
1886 "unable to parse parameter count `%s'",
1887 tline->text);
1888 if (searching.nparam_min > searching.nparam_max)
1889 error(ERR_NONFATAL,
1890 "minimum parameter count exceeds maximum");
1891 }
1892 }
1893 if (tline && tok_is_(tline->next, "+")) {
1894 tline = tline->next;
1895 searching.plus = true;
1896 }
1897 mmac = (MMacro *) hash_findix(&mmacros, searching.name);
1898 while (mmac) {
1899 if (!strcmp(mmac->name, searching.name) &&
1900 (mmac->nparam_min <= searching.nparam_max
1901 || searching.plus)
1902 && (searching.nparam_min <= mmac->nparam_max
1903 || mmac->plus)) {
1904 found = true;
1905 break;
1906 }
1907 mmac = mmac->next;
1908 }
1909 if (tline && tline->next)
1910 error(ERR_WARNING|ERR_PASS1,
1911 "trailing garbage after %%ifmacro ignored");
1912 nasm_free(searching.name);
1913 j = found;
1914 break;
H. Peter Anvin89cee572009-07-15 09:16:54 -04001915 }
H. Peter Anvin65747262002-05-07 00:10:05 +00001916
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001917 case PPC_IFID:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001918 needtype = TOK_ID;
1919 goto iftype;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001920 case PPC_IFNUM:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001921 needtype = TOK_NUMBER;
1922 goto iftype;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001923 case PPC_IFSTR:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001924 needtype = TOK_STRING;
1925 goto iftype;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001926
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001927iftype:
1928 t = tline = expand_smacro(tline);
H. Peter Anvind85d2502008-05-04 17:53:31 -07001929
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001930 while (tok_type_(t, TOK_WHITESPACE) ||
1931 (needtype == TOK_NUMBER &&
1932 tok_type_(t, TOK_OTHER) &&
1933 (t->text[0] == '-' || t->text[0] == '+') &&
1934 !t->text[1]))
1935 t = t->next;
H. Peter Anvind85d2502008-05-04 17:53:31 -07001936
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001937 j = tok_type_(t, needtype);
1938 break;
H. Peter Anvincbf768d2008-02-16 16:41:25 -08001939
1940 case PPC_IFTOKEN:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001941 t = tline = expand_smacro(tline);
1942 while (tok_type_(t, TOK_WHITESPACE))
1943 t = t->next;
H. Peter Anvincbf768d2008-02-16 16:41:25 -08001944
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001945 j = false;
1946 if (t) {
1947 t = t->next; /* Skip the actual token */
1948 while (tok_type_(t, TOK_WHITESPACE))
1949 t = t->next;
1950 j = !t; /* Should be nothing left */
1951 }
1952 break;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001953
H. Peter Anvin134b9462008-02-16 17:01:40 -08001954 case PPC_IFEMPTY:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001955 t = tline = expand_smacro(tline);
1956 while (tok_type_(t, TOK_WHITESPACE))
1957 t = t->next;
H. Peter Anvin134b9462008-02-16 17:01:40 -08001958
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001959 j = !t; /* Should be empty */
1960 break;
H. Peter Anvin134b9462008-02-16 17:01:40 -08001961
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001962 case PPC_IF:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001963 t = tline = expand_smacro(tline);
1964 tptr = &t;
1965 tokval.t_type = TOKEN_INVALID;
1966 evalresult = evaluate(ppscan, tptr, &tokval,
1967 NULL, pass | CRITICAL, error, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001968 if (!evalresult)
1969 return -1;
1970 if (tokval.t_type)
H. Peter Anvin917a3492008-09-24 09:14:49 -07001971 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +00001972 "trailing garbage after expression ignored");
1973 if (!is_simple(evalresult)) {
1974 error(ERR_NONFATAL,
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001975 "non-constant value given to `%s'", pp_directives[ct]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001976 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001977 }
Chuck Crayne60ae75d2007-05-02 01:59:16 +00001978 j = reloc_value(evalresult) != 0;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001979 break;
H. Peter Anvin95e28822007-09-12 04:20:08 +00001980
H. Peter Anvine2c80182005-01-15 22:15:51 +00001981 default:
1982 error(ERR_FATAL,
1983 "preprocessor directive `%s' not yet implemented",
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001984 pp_directives[ct]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001985 goto fail;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001986 }
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001987
1988 free_tlist(origline);
1989 return j ^ PP_NEGATIVE(ct);
H. Peter Anvin70653092007-10-19 14:42:29 -07001990
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001991fail:
1992 free_tlist(origline);
1993 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001994}
1995
1996/*
H. Peter Anvin4db5a162007-10-11 13:42:09 -07001997 * Common code for defining an smacro
1998 */
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001999static bool define_smacro(Context *ctx, const char *mname, bool casesense,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002000 int nparam, Token *expansion)
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002001{
2002 SMacro *smac, **smhead;
H. Peter Anvin166c2472008-05-28 12:28:58 -07002003 struct hash_table *smtbl;
H. Peter Anvin70653092007-10-19 14:42:29 -07002004
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002005 if (smacro_defined(ctx, mname, nparam, &smac, casesense)) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002006 if (!smac) {
2007 error(ERR_WARNING|ERR_PASS1,
2008 "single-line macro `%s' defined both with and"
2009 " without parameters", mname);
2010 /*
2011 * Some instances of the old code considered this a failure,
2012 * some others didn't. What is the right thing to do here?
2013 */
2014 free_tlist(expansion);
2015 return false; /* Failure */
2016 } else {
2017 /*
2018 * We're redefining, so we have to take over an
2019 * existing SMacro structure. This means freeing
2020 * what was already in it.
2021 */
2022 nasm_free(smac->name);
2023 free_tlist(smac->expansion);
2024 }
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002025 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002026 smtbl = ctx ? &ctx->localmac : &smacros;
2027 smhead = (SMacro **) hash_findi_add(smtbl, mname);
2028 smac = nasm_malloc(sizeof(SMacro));
2029 smac->next = *smhead;
2030 *smhead = smac;
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002031 }
2032 smac->name = nasm_strdup(mname);
2033 smac->casesense = casesense;
2034 smac->nparam = nparam;
2035 smac->expansion = expansion;
2036 smac->in_progress = false;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002037 return true; /* Success */
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002038}
2039
2040/*
2041 * Undefine an smacro
2042 */
2043static void undef_smacro(Context *ctx, const char *mname)
2044{
2045 SMacro **smhead, *s, **sp;
H. Peter Anvin166c2472008-05-28 12:28:58 -07002046 struct hash_table *smtbl;
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002047
H. Peter Anvin166c2472008-05-28 12:28:58 -07002048 smtbl = ctx ? &ctx->localmac : &smacros;
2049 smhead = (SMacro **)hash_findi(smtbl, mname, NULL);
H. Peter Anvin70653092007-10-19 14:42:29 -07002050
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002051 if (smhead) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002052 /*
2053 * We now have a macro name... go hunt for it.
2054 */
2055 sp = smhead;
2056 while ((s = *sp) != NULL) {
2057 if (!mstrcmp(s->name, mname, s->casesense)) {
2058 *sp = s->next;
2059 nasm_free(s->name);
2060 free_tlist(s->expansion);
2061 nasm_free(s);
2062 } else {
2063 sp = &s->next;
2064 }
2065 }
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002066 }
2067}
2068
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002069/*
H. Peter Anvina26433d2008-07-16 14:40:01 -07002070 * Parse a mmacro specification.
2071 */
2072static bool parse_mmacro_spec(Token *tline, MMacro *def, const char *directive)
2073{
2074 bool err;
2075
2076 tline = tline->next;
2077 skip_white_(tline);
2078 tline = expand_id(tline);
2079 if (!tok_type_(tline, TOK_ID)) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002080 error(ERR_NONFATAL, "`%s' expects a macro name", directive);
2081 return false;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002082 }
Victor van den Elzenb916ede2008-07-23 15:14:22 +02002083
H. Peter Anvin89cee572009-07-15 09:16:54 -04002084 def->prev = NULL;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002085 def->name = nasm_strdup(tline->text);
2086 def->plus = false;
2087 def->nolist = false;
2088 def->in_progress = 0;
2089 def->rep_nest = NULL;
Victor van den Elzenb916ede2008-07-23 15:14:22 +02002090 def->nparam_min = 0;
2091 def->nparam_max = 0;
2092
H. Peter Anvina26433d2008-07-16 14:40:01 -07002093 tline = expand_smacro(tline->next);
2094 skip_white_(tline);
2095 if (!tok_type_(tline, TOK_NUMBER)) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002096 error(ERR_NONFATAL, "`%s' expects a parameter count", directive);
H. Peter Anvina26433d2008-07-16 14:40:01 -07002097 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002098 def->nparam_min = def->nparam_max =
2099 readnum(tline->text, &err);
2100 if (err)
2101 error(ERR_NONFATAL,
2102 "unable to parse parameter count `%s'", tline->text);
H. Peter Anvina26433d2008-07-16 14:40:01 -07002103 }
2104 if (tline && tok_is_(tline->next, "-")) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002105 tline = tline->next->next;
2106 if (tok_is_(tline, "*")) {
2107 def->nparam_max = INT_MAX;
2108 } else if (!tok_type_(tline, TOK_NUMBER)) {
2109 error(ERR_NONFATAL,
2110 "`%s' expects a parameter count after `-'", directive);
2111 } else {
2112 def->nparam_max = readnum(tline->text, &err);
2113 if (err) {
2114 error(ERR_NONFATAL, "unable to parse parameter count `%s'",
2115 tline->text);
2116 }
2117 if (def->nparam_min > def->nparam_max) {
2118 error(ERR_NONFATAL, "minimum parameter count exceeds maximum");
2119 }
2120 }
H. Peter Anvina26433d2008-07-16 14:40:01 -07002121 }
2122 if (tline && tok_is_(tline->next, "+")) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002123 tline = tline->next;
2124 def->plus = true;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002125 }
2126 if (tline && tok_type_(tline->next, TOK_ID) &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002127 !nasm_stricmp(tline->next->text, ".nolist")) {
2128 tline = tline->next;
2129 def->nolist = true;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002130 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002131
H. Peter Anvina26433d2008-07-16 14:40:01 -07002132 /*
2133 * Handle default parameters.
2134 */
2135 if (tline && tline->next) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002136 def->dlist = tline->next;
2137 tline->next = NULL;
2138 count_mmac_params(def->dlist, &def->ndefs, &def->defaults);
H. Peter Anvina26433d2008-07-16 14:40:01 -07002139 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002140 def->dlist = NULL;
2141 def->defaults = NULL;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002142 }
2143 def->expansion = NULL;
2144
H. Peter Anvin89cee572009-07-15 09:16:54 -04002145 if (def->defaults && def->ndefs > def->nparam_max - def->nparam_min &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002146 !def->plus)
2147 error(ERR_WARNING|ERR_PASS1|ERR_WARN_MDP,
2148 "too many default macro parameters");
Victor van den Elzenb916ede2008-07-23 15:14:22 +02002149
H. Peter Anvina26433d2008-07-16 14:40:01 -07002150 return true;
2151}
2152
2153
2154/*
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002155 * Decode a size directive
2156 */
2157static int parse_size(const char *str) {
2158 static const char *size_names[] =
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002159 { "byte", "dword", "oword", "qword", "tword", "word", "yword" };
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002160 static const int sizes[] =
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002161 { 0, 1, 4, 16, 8, 10, 2, 32 };
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002162
Cyrill Gorcunova7319242010-06-03 22:04:36 +04002163 return sizes[bsii(str, size_names, ARRAY_SIZE(size_names))+1];
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002164}
2165
Ed Beroset3ab3f412002-06-11 03:31:49 +00002166/**
2167 * find and process preprocessor directive in passed line
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002168 * Find out if a line contains a preprocessor directive, and deal
2169 * with it if so.
H. Peter Anvin70653092007-10-19 14:42:29 -07002170 *
Ed Beroset3ab3f412002-06-11 03:31:49 +00002171 * If a directive _is_ found, it is the responsibility of this routine
2172 * (and not the caller) to free_tlist() the line.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002173 *
Ed Beroset3ab3f412002-06-11 03:31:49 +00002174 * @param tline a pointer to the current tokeninzed line linked list
2175 * @return DIRECTIVE_FOUND or NO_DIRECTIVE_FOUND
H. Peter Anvin70653092007-10-19 14:42:29 -07002176 *
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002177 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002178static int do_directive(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00002179{
H. Peter Anvin4169a472007-09-12 01:29:43 +00002180 enum preproc_token i;
2181 int j;
H. Peter Anvin70055962007-10-11 00:05:31 -07002182 bool err;
2183 int nparam;
2184 bool nolist;
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07002185 bool casesense;
H. Peter Anvin8cfdb9d2007-09-14 18:36:01 -07002186 int k, m;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002187 int offset;
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08002188 char *p, *pp;
2189 const char *mname;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002190 Include *inc;
2191 Context *ctx;
2192 Cond *cond;
H. Peter Anvin97a23472007-09-16 17:57:25 -07002193 MMacro *mmac, **mmhead;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002194 Token *t, *tt, *param_start, *macro_start, *last, **tptr, *origline;
2195 Line *l;
2196 struct tokenval tokval;
2197 expr *evalresult;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002198 MMacro *tmp_defining; /* Used when manipulating rep_nest */
H. Peter Anvinf8ba53e2007-10-11 10:11:57 -07002199 int64_t count;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07002200 size_t len;
H. Peter Anvin8e3f75e2008-09-24 00:21:58 -07002201 int severity;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002202
2203 origline = tline;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002204
H. Peter Anvineba20a72002-04-30 20:53:55 +00002205 skip_white_(tline);
H. Peter Anvinf2936d72008-06-04 15:11:23 -07002206 if (!tline || !tok_type_(tline, TOK_PREPROC_ID) ||
H. Peter Anvine2c80182005-01-15 22:15:51 +00002207 (tline->text[1] == '%' || tline->text[1] == '$'
2208 || tline->text[1] == '!'))
2209 return NO_DIRECTIVE_FOUND;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002210
H. Peter Anvin4169a472007-09-12 01:29:43 +00002211 i = pp_token_hash(tline->text);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002212
2213 /*
Cyrill Gorcunovf09116f2010-02-28 11:52:53 +03002214 * FIXME: We zap execution of PP_RMACRO, PP_IRMACRO, PP_EXITMACRO
2215 * since they are known to be buggy at moment, we need to fix them
2216 * in future release (2.09-2.10)
2217 */
2218 if (i == PP_RMACRO || i == PP_RMACRO || i == PP_EXITMACRO) {
2219 error(ERR_NONFATAL, "unknown preprocessor directive `%s'",
2220 tline->text);
2221 return NO_DIRECTIVE_FOUND;
2222 }
2223
2224 /*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002225 * If we're in a non-emitting branch of a condition construct,
H. Peter Anvin76690a12002-04-30 20:52:49 +00002226 * or walking to the end of an already terminated %rep block,
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002227 * we should ignore all directives except for condition
2228 * directives.
2229 */
H. Peter Anvin76690a12002-04-30 20:52:49 +00002230 if (((istk->conds && !emitting(istk->conds->state)) ||
H. Peter Anvine2c80182005-01-15 22:15:51 +00002231 (istk->mstk && !istk->mstk->in_progress)) && !is_condition(i)) {
2232 return NO_DIRECTIVE_FOUND;
H. Peter Anvineba20a72002-04-30 20:53:55 +00002233 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002234
2235 /*
H. Peter Anvin76690a12002-04-30 20:52:49 +00002236 * If we're defining a macro or reading a %rep block, we should
Victor van den Elzen8f1120f2008-07-16 13:41:37 +02002237 * ignore all directives except for %macro/%imacro (which nest),
2238 * %endm/%endmacro, and (only if we're in a %rep block) %endrep.
2239 * If we're in a %rep block, another %rep nests, so should be let through.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002240 */
2241 if (defining && i != PP_MACRO && i != PP_IMACRO &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002242 i != PP_RMACRO && i != PP_IRMACRO &&
H. Peter Anvine2c80182005-01-15 22:15:51 +00002243 i != PP_ENDMACRO && i != PP_ENDM &&
2244 (defining->name || (i != PP_ENDREP && i != PP_REP))) {
2245 return NO_DIRECTIVE_FOUND;
H. Peter Anvineba20a72002-04-30 20:53:55 +00002246 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002247
Charles Crayned4200be2008-07-12 16:42:33 -07002248 if (defining) {
Keith Kanios852f1ee2009-07-12 00:19:55 -05002249 if (i == PP_MACRO || i == PP_IMACRO ||
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002250 i == PP_RMACRO || i == PP_IRMACRO) {
Charles Crayned4200be2008-07-12 16:42:33 -07002251 nested_mac_count++;
2252 return NO_DIRECTIVE_FOUND;
2253 } else if (nested_mac_count > 0) {
2254 if (i == PP_ENDMACRO) {
2255 nested_mac_count--;
2256 return NO_DIRECTIVE_FOUND;
2257 }
2258 }
2259 if (!defining->name) {
2260 if (i == PP_REP) {
2261 nested_rep_count++;
2262 return NO_DIRECTIVE_FOUND;
2263 } else if (nested_rep_count > 0) {
2264 if (i == PP_ENDREP) {
2265 nested_rep_count--;
2266 return NO_DIRECTIVE_FOUND;
2267 }
2268 }
2269 }
2270 }
2271
H. Peter Anvin4169a472007-09-12 01:29:43 +00002272 switch (i) {
2273 case PP_INVALID:
H. Peter Anvine2c80182005-01-15 22:15:51 +00002274 error(ERR_NONFATAL, "unknown preprocessor directive `%s'",
2275 tline->text);
2276 return NO_DIRECTIVE_FOUND; /* didn't get it */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002277
H. Peter Anvine2c80182005-01-15 22:15:51 +00002278 case PP_STACKSIZE:
2279 /* Directive to tell NASM what the default stack size is. The
2280 * default is for a 16-bit stack, and this can be overriden with
2281 * %stacksize large.
H. Peter Anvine2c80182005-01-15 22:15:51 +00002282 */
2283 tline = tline->next;
2284 if (tline && tline->type == TOK_WHITESPACE)
2285 tline = tline->next;
2286 if (!tline || tline->type != TOK_ID) {
2287 error(ERR_NONFATAL, "`%%stacksize' missing size parameter");
2288 free_tlist(origline);
2289 return DIRECTIVE_FOUND;
2290 }
2291 if (nasm_stricmp(tline->text, "flat") == 0) {
2292 /* All subsequent ARG directives are for a 32-bit stack */
2293 StackSize = 4;
2294 StackPointer = "ebp";
2295 ArgOffset = 8;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002296 LocalOffset = 0;
Charles Crayne7eaf9192007-11-08 22:11:14 -08002297 } else if (nasm_stricmp(tline->text, "flat64") == 0) {
2298 /* All subsequent ARG directives are for a 64-bit stack */
2299 StackSize = 8;
2300 StackPointer = "rbp";
Per Jessen53252e02010-02-11 00:16:59 +03002301 ArgOffset = 16;
Charles Crayne7eaf9192007-11-08 22:11:14 -08002302 LocalOffset = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002303 } else if (nasm_stricmp(tline->text, "large") == 0) {
2304 /* All subsequent ARG directives are for a 16-bit stack,
2305 * far function call.
2306 */
2307 StackSize = 2;
2308 StackPointer = "bp";
2309 ArgOffset = 4;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002310 LocalOffset = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002311 } else if (nasm_stricmp(tline->text, "small") == 0) {
2312 /* All subsequent ARG directives are for a 16-bit stack,
2313 * far function call. We don't support near functions.
2314 */
2315 StackSize = 2;
2316 StackPointer = "bp";
2317 ArgOffset = 6;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002318 LocalOffset = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002319 } else {
2320 error(ERR_NONFATAL, "`%%stacksize' invalid size type");
2321 free_tlist(origline);
2322 return DIRECTIVE_FOUND;
2323 }
2324 free_tlist(origline);
2325 return DIRECTIVE_FOUND;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002326
H. Peter Anvine2c80182005-01-15 22:15:51 +00002327 case PP_ARG:
2328 /* TASM like ARG directive to define arguments to functions, in
2329 * the following form:
2330 *
2331 * ARG arg1:WORD, arg2:DWORD, arg4:QWORD
2332 */
2333 offset = ArgOffset;
2334 do {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00002335 char *arg, directive[256];
H. Peter Anvine2c80182005-01-15 22:15:51 +00002336 int size = StackSize;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002337
H. Peter Anvine2c80182005-01-15 22:15:51 +00002338 /* Find the argument name */
2339 tline = tline->next;
2340 if (tline && tline->type == TOK_WHITESPACE)
2341 tline = tline->next;
2342 if (!tline || tline->type != TOK_ID) {
2343 error(ERR_NONFATAL, "`%%arg' missing argument parameter");
2344 free_tlist(origline);
2345 return DIRECTIVE_FOUND;
2346 }
2347 arg = tline->text;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002348
H. Peter Anvine2c80182005-01-15 22:15:51 +00002349 /* Find the argument size type */
2350 tline = tline->next;
2351 if (!tline || tline->type != TOK_OTHER
2352 || tline->text[0] != ':') {
2353 error(ERR_NONFATAL,
2354 "Syntax error processing `%%arg' directive");
2355 free_tlist(origline);
2356 return DIRECTIVE_FOUND;
2357 }
2358 tline = tline->next;
2359 if (!tline || tline->type != TOK_ID) {
2360 error(ERR_NONFATAL, "`%%arg' missing size type parameter");
2361 free_tlist(origline);
2362 return DIRECTIVE_FOUND;
2363 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002364
H. Peter Anvine2c80182005-01-15 22:15:51 +00002365 /* Allow macro expansion of type parameter */
Keith Kaniosb7a89542007-04-12 02:40:54 +00002366 tt = tokenize(tline->text);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002367 tt = expand_smacro(tt);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002368 size = parse_size(tt->text);
2369 if (!size) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002370 error(ERR_NONFATAL,
2371 "Invalid size type for `%%arg' missing directive");
2372 free_tlist(tt);
2373 free_tlist(origline);
2374 return DIRECTIVE_FOUND;
2375 }
2376 free_tlist(tt);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002377
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002378 /* Round up to even stack slots */
2379 size = ALIGN(size, StackSize);
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002380
H. Peter Anvine2c80182005-01-15 22:15:51 +00002381 /* Now define the macro for the argument */
2382 snprintf(directive, sizeof(directive), "%%define %s (%s+%d)",
2383 arg, StackPointer, offset);
Keith Kaniosb7a89542007-04-12 02:40:54 +00002384 do_directive(tokenize(directive));
H. Peter Anvine2c80182005-01-15 22:15:51 +00002385 offset += size;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002386
H. Peter Anvine2c80182005-01-15 22:15:51 +00002387 /* Move to the next argument in the list */
2388 tline = tline->next;
2389 if (tline && tline->type == TOK_WHITESPACE)
2390 tline = tline->next;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002391 } while (tline && tline->type == TOK_OTHER && tline->text[0] == ',');
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002392 ArgOffset = offset;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002393 free_tlist(origline);
2394 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002395
H. Peter Anvine2c80182005-01-15 22:15:51 +00002396 case PP_LOCAL:
2397 /* TASM like LOCAL directive to define local variables for a
2398 * function, in the following form:
2399 *
2400 * LOCAL local1:WORD, local2:DWORD, local4:QWORD = LocalSize
2401 *
2402 * The '= LocalSize' at the end is ignored by NASM, but is
2403 * required by TASM to define the local parameter size (and used
2404 * by the TASM macro package).
2405 */
2406 offset = LocalOffset;
2407 do {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00002408 char *local, directive[256];
H. Peter Anvine2c80182005-01-15 22:15:51 +00002409 int size = StackSize;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002410
H. Peter Anvine2c80182005-01-15 22:15:51 +00002411 /* Find the argument name */
2412 tline = tline->next;
2413 if (tline && tline->type == TOK_WHITESPACE)
2414 tline = tline->next;
2415 if (!tline || tline->type != TOK_ID) {
2416 error(ERR_NONFATAL,
2417 "`%%local' missing argument parameter");
2418 free_tlist(origline);
2419 return DIRECTIVE_FOUND;
2420 }
2421 local = tline->text;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002422
H. Peter Anvine2c80182005-01-15 22:15:51 +00002423 /* Find the argument size type */
2424 tline = tline->next;
2425 if (!tline || tline->type != TOK_OTHER
2426 || tline->text[0] != ':') {
2427 error(ERR_NONFATAL,
2428 "Syntax error processing `%%local' directive");
2429 free_tlist(origline);
2430 return DIRECTIVE_FOUND;
2431 }
2432 tline = tline->next;
2433 if (!tline || tline->type != TOK_ID) {
2434 error(ERR_NONFATAL,
2435 "`%%local' missing size type parameter");
2436 free_tlist(origline);
2437 return DIRECTIVE_FOUND;
2438 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00002439
H. Peter Anvine2c80182005-01-15 22:15:51 +00002440 /* Allow macro expansion of type parameter */
Keith Kaniosb7a89542007-04-12 02:40:54 +00002441 tt = tokenize(tline->text);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002442 tt = expand_smacro(tt);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002443 size = parse_size(tt->text);
2444 if (!size) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002445 error(ERR_NONFATAL,
2446 "Invalid size type for `%%local' missing directive");
2447 free_tlist(tt);
2448 free_tlist(origline);
2449 return DIRECTIVE_FOUND;
2450 }
2451 free_tlist(tt);
H. Peter Anvin734b1882002-04-30 21:01:08 +00002452
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002453 /* Round up to even stack slots */
2454 size = ALIGN(size, StackSize);
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002455
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002456 offset += size; /* Negative offset, increment before */
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002457
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002458 /* Now define the macro for the argument */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002459 snprintf(directive, sizeof(directive), "%%define %s (%s-%d)",
2460 local, StackPointer, offset);
Keith Kaniosb7a89542007-04-12 02:40:54 +00002461 do_directive(tokenize(directive));
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002462
H. Peter Anvine2c80182005-01-15 22:15:51 +00002463 /* Now define the assign to setup the enter_c macro correctly */
2464 snprintf(directive, sizeof(directive),
2465 "%%assign %%$localsize %%$localsize+%d", size);
Keith Kaniosb7a89542007-04-12 02:40:54 +00002466 do_directive(tokenize(directive));
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002467
H. Peter Anvine2c80182005-01-15 22:15:51 +00002468 /* Move to the next argument in the list */
2469 tline = tline->next;
2470 if (tline && tline->type == TOK_WHITESPACE)
2471 tline = tline->next;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002472 } while (tline && tline->type == TOK_OTHER && tline->text[0] == ',');
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002473 LocalOffset = offset;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002474 free_tlist(origline);
2475 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002476
H. Peter Anvine2c80182005-01-15 22:15:51 +00002477 case PP_CLEAR:
2478 if (tline->next)
H. Peter Anvin917a3492008-09-24 09:14:49 -07002479 error(ERR_WARNING|ERR_PASS1,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002480 "trailing garbage after `%%clear' ignored");
2481 free_macros();
2482 init_macros();
H. Peter Anvine2c80182005-01-15 22:15:51 +00002483 free_tlist(origline);
2484 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002485
H. Peter Anvin418ca702008-05-30 10:42:30 -07002486 case PP_DEPEND:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002487 t = tline->next = expand_smacro(tline->next);
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002488 skip_white_(t);
2489 if (!t || (t->type != TOK_STRING &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002490 t->type != TOK_INTERNAL_STRING)) {
H. Peter Anvin418ca702008-05-30 10:42:30 -07002491 error(ERR_NONFATAL, "`%%depend' expects a file name");
2492 free_tlist(origline);
2493 return DIRECTIVE_FOUND; /* but we did _something_ */
2494 }
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002495 if (t->next)
H. Peter Anvin917a3492008-09-24 09:14:49 -07002496 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvin418ca702008-05-30 10:42:30 -07002497 "trailing garbage after `%%depend' ignored");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002498 p = t->text;
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002499 if (t->type != TOK_INTERNAL_STRING)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002500 nasm_unquote_cstr(p, i);
2501 if (dephead && !in_list(*dephead, p)) {
2502 StrList *sl = nasm_malloc(strlen(p)+1+sizeof sl->next);
2503 sl->next = NULL;
2504 strcpy(sl->str, p);
2505 *deptail = sl;
2506 deptail = &sl->next;
2507 }
2508 free_tlist(origline);
H. Peter Anvin418ca702008-05-30 10:42:30 -07002509 return DIRECTIVE_FOUND;
2510
2511 case PP_INCLUDE:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002512 t = tline->next = expand_smacro(tline->next);
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002513 skip_white_(t);
H. Peter Anvind2456592008-06-19 15:04:18 -07002514
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002515 if (!t || (t->type != TOK_STRING &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002516 t->type != TOK_INTERNAL_STRING)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002517 error(ERR_NONFATAL, "`%%include' expects a file name");
2518 free_tlist(origline);
2519 return DIRECTIVE_FOUND; /* but we did _something_ */
2520 }
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002521 if (t->next)
H. Peter Anvin917a3492008-09-24 09:14:49 -07002522 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +00002523 "trailing garbage after `%%include' ignored");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002524 p = t->text;
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002525 if (t->type != TOK_INTERNAL_STRING)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002526 nasm_unquote_cstr(p, i);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002527 inc = nasm_malloc(sizeof(Include));
2528 inc->next = istk;
2529 inc->conds = NULL;
H. Peter Anvin2b1c3b92008-06-06 10:38:46 -07002530 inc->fp = inc_fopen(p, dephead, &deptail, pass == 0);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002531 if (!inc->fp) {
2532 /* -MG given but file not found */
2533 nasm_free(inc);
2534 } else {
2535 inc->fname = src_set_fname(nasm_strdup(p));
2536 inc->lineno = src_set_linnum(0);
2537 inc->lineinc = 1;
2538 inc->expansion = NULL;
2539 inc->mstk = NULL;
2540 istk = inc;
2541 list->uplevel(LIST_INCLUDE);
2542 }
2543 free_tlist(origline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002544 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002545
H. Peter Anvind2456592008-06-19 15:04:18 -07002546 case PP_USE:
H. Peter Anvinf4ae5ad2008-06-19 18:39:24 -07002547 {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002548 static macros_t *use_pkg;
2549 const char *pkg_macro = NULL;
H. Peter Anvinf4ae5ad2008-06-19 18:39:24 -07002550
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002551 tline = tline->next;
2552 skip_white_(tline);
2553 tline = expand_id(tline);
H. Peter Anvind2456592008-06-19 15:04:18 -07002554
H. Peter Anvin264b7b92008-10-24 16:38:17 -07002555 if (!tline || (tline->type != TOK_STRING &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002556 tline->type != TOK_INTERNAL_STRING &&
2557 tline->type != TOK_ID)) {
H. Peter Anvin926fc402008-06-19 16:26:12 -07002558 error(ERR_NONFATAL, "`%%use' expects a package name");
H. Peter Anvind2456592008-06-19 15:04:18 -07002559 free_tlist(origline);
2560 return DIRECTIVE_FOUND; /* but we did _something_ */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002561 }
H. Peter Anvin264b7b92008-10-24 16:38:17 -07002562 if (tline->next)
H. Peter Anvin917a3492008-09-24 09:14:49 -07002563 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvind2456592008-06-19 15:04:18 -07002564 "trailing garbage after `%%use' ignored");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002565 if (tline->type == TOK_STRING)
2566 nasm_unquote_cstr(tline->text, i);
2567 use_pkg = nasm_stdmac_find_package(tline->text);
2568 if (!use_pkg)
2569 error(ERR_NONFATAL, "unknown `%%use' package: %s", tline->text);
2570 else
2571 pkg_macro = (char *)use_pkg + 1; /* The first string will be <%define>__USE_*__ */
Victor van den Elzen35eb2ea2010-03-10 22:33:48 +01002572 if (use_pkg && ! smacro_defined(NULL, pkg_macro, 0, NULL, true)) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002573 /* Not already included, go ahead and include it */
2574 stdmacpos = use_pkg;
2575 }
2576 free_tlist(origline);
H. Peter Anvind2456592008-06-19 15:04:18 -07002577 return DIRECTIVE_FOUND;
H. Peter Anvinf4ae5ad2008-06-19 18:39:24 -07002578 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002579 case PP_PUSH:
H. Peter Anvine2c80182005-01-15 22:15:51 +00002580 case PP_REPL:
H. Peter Anvin42b56392008-10-24 16:24:21 -07002581 case PP_POP:
H. Peter Anvine2c80182005-01-15 22:15:51 +00002582 tline = tline->next;
2583 skip_white_(tline);
2584 tline = expand_id(tline);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002585 if (tline) {
2586 if (!tok_type_(tline, TOK_ID)) {
2587 error(ERR_NONFATAL, "`%s' expects a context identifier",
2588 pp_directives[i]);
2589 free_tlist(origline);
2590 return DIRECTIVE_FOUND; /* but we did _something_ */
2591 }
2592 if (tline->next)
2593 error(ERR_WARNING|ERR_PASS1,
2594 "trailing garbage after `%s' ignored",
2595 pp_directives[i]);
2596 p = nasm_strdup(tline->text);
2597 } else {
2598 p = NULL; /* Anonymous */
2599 }
H. Peter Anvin42b56392008-10-24 16:24:21 -07002600
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002601 if (i == PP_PUSH) {
2602 ctx = nasm_malloc(sizeof(Context));
2603 ctx->next = cstk;
2604 hash_init(&ctx->localmac, HASH_SMALL);
2605 ctx->name = p;
2606 ctx->number = unique++;
2607 cstk = ctx;
2608 } else {
2609 /* %pop or %repl */
2610 if (!cstk) {
2611 error(ERR_NONFATAL, "`%s': context stack is empty",
2612 pp_directives[i]);
2613 } else if (i == PP_POP) {
2614 if (p && (!cstk->name || nasm_stricmp(p, cstk->name)))
2615 error(ERR_NONFATAL, "`%%pop' in wrong context: %s, "
2616 "expected %s",
2617 cstk->name ? cstk->name : "anonymous", p);
2618 else
2619 ctx_pop();
2620 } else {
2621 /* i == PP_REPL */
2622 nasm_free(cstk->name);
2623 cstk->name = p;
2624 p = NULL;
2625 }
2626 nasm_free(p);
2627 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002628 free_tlist(origline);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002629 return DIRECTIVE_FOUND;
H. Peter Anvin8e3f75e2008-09-24 00:21:58 -07002630 case PP_FATAL:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002631 severity = ERR_FATAL;
2632 goto issue_error;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002633 case PP_ERROR:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002634 severity = ERR_NONFATAL;
2635 goto issue_error;
H. Peter Anvin7df04172008-06-10 18:27:38 -07002636 case PP_WARNING:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002637 severity = ERR_WARNING|ERR_WARN_USER;
2638 goto issue_error;
H. Peter Anvin8e3f75e2008-09-24 00:21:58 -07002639
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002640issue_error:
H. Peter Anvin7df04172008-06-10 18:27:38 -07002641 {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002642 /* Only error out if this is the final pass */
2643 if (pass != 2 && i != PP_FATAL)
2644 return DIRECTIVE_FOUND;
2645
2646 tline->next = expand_smacro(tline->next);
2647 tline = tline->next;
2648 skip_white_(tline);
2649 t = tline ? tline->next : NULL;
2650 skip_white_(t);
2651 if (tok_type_(tline, TOK_STRING) && !t) {
2652 /* The line contains only a quoted string */
2653 p = tline->text;
2654 nasm_unquote(p, NULL); /* Ignore NUL character truncation */
2655 error(severity, "%s", p);
2656 } else {
2657 /* Not a quoted string, or more than a quoted string */
2658 p = detoken(tline, false);
2659 error(severity, "%s", p);
2660 nasm_free(p);
2661 }
2662 free_tlist(origline);
2663 return DIRECTIVE_FOUND;
H. Peter Anvin7df04172008-06-10 18:27:38 -07002664 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00002665
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002666 CASE_PP_IF:
H. Peter Anvine2c80182005-01-15 22:15:51 +00002667 if (istk->conds && !emitting(istk->conds->state))
2668 j = COND_NEVER;
2669 else {
2670 j = if_condition(tline->next, i);
2671 tline->next = NULL; /* it got freed */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002672 j = j < 0 ? COND_NEVER : j ? COND_IF_TRUE : COND_IF_FALSE;
2673 }
2674 cond = nasm_malloc(sizeof(Cond));
2675 cond->next = istk->conds;
2676 cond->state = j;
2677 istk->conds = cond;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002678 if(istk->mstk)
Keith Kanios3c0d91f2009-10-25 13:28:03 -05002679 istk->mstk->condcnt ++;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002680 free_tlist(origline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002681 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002682
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002683 CASE_PP_ELIF:
H. Peter Anvine2c80182005-01-15 22:15:51 +00002684 if (!istk->conds)
H. Peter Anvin4169a472007-09-12 01:29:43 +00002685 error(ERR_FATAL, "`%s': no matching `%%if'", pp_directives[i]);
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002686 switch(istk->conds->state) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002687 case COND_IF_TRUE:
2688 istk->conds->state = COND_DONE;
2689 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002690
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002691 case COND_DONE:
2692 case COND_NEVER:
2693 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002694
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002695 case COND_ELSE_TRUE:
2696 case COND_ELSE_FALSE:
2697 error_precond(ERR_WARNING|ERR_PASS1,
2698 "`%%elif' after `%%else' ignored");
2699 istk->conds->state = COND_NEVER;
2700 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002701
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002702 case COND_IF_FALSE:
2703 /*
2704 * IMPORTANT: In the case of %if, we will already have
2705 * called expand_mmac_params(); however, if we're
2706 * processing an %elif we must have been in a
2707 * non-emitting mode, which would have inhibited
2708 * the normal invocation of expand_mmac_params().
2709 * Therefore, we have to do it explicitly here.
2710 */
2711 j = if_condition(expand_mmac_params(tline->next), i);
2712 tline->next = NULL; /* it got freed */
2713 istk->conds->state =
2714 j < 0 ? COND_NEVER : j ? COND_IF_TRUE : COND_IF_FALSE;
2715 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002716 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002717 free_tlist(origline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002718 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002719
H. Peter Anvine2c80182005-01-15 22:15:51 +00002720 case PP_ELSE:
2721 if (tline->next)
H. Peter Anvin917a3492008-09-24 09:14:49 -07002722 error_precond(ERR_WARNING|ERR_PASS1,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002723 "trailing garbage after `%%else' ignored");
H. Peter Anvine2c80182005-01-15 22:15:51 +00002724 if (!istk->conds)
2725 error(ERR_FATAL, "`%%else': no matching `%%if'");
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002726 switch(istk->conds->state) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002727 case COND_IF_TRUE:
2728 case COND_DONE:
2729 istk->conds->state = COND_ELSE_FALSE;
2730 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002731
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002732 case COND_NEVER:
2733 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002734
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002735 case COND_IF_FALSE:
2736 istk->conds->state = COND_ELSE_TRUE;
2737 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002738
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002739 case COND_ELSE_TRUE:
2740 case COND_ELSE_FALSE:
2741 error_precond(ERR_WARNING|ERR_PASS1,
2742 "`%%else' after `%%else' ignored.");
2743 istk->conds->state = COND_NEVER;
2744 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002745 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002746 free_tlist(origline);
2747 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002748
H. Peter Anvine2c80182005-01-15 22:15:51 +00002749 case PP_ENDIF:
2750 if (tline->next)
H. Peter Anvin917a3492008-09-24 09:14:49 -07002751 error_precond(ERR_WARNING|ERR_PASS1,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002752 "trailing garbage after `%%endif' ignored");
H. Peter Anvine2c80182005-01-15 22:15:51 +00002753 if (!istk->conds)
2754 error(ERR_FATAL, "`%%endif': no matching `%%if'");
2755 cond = istk->conds;
2756 istk->conds = cond->next;
2757 nasm_free(cond);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002758 if(istk->mstk)
Keith Kanios3c0d91f2009-10-25 13:28:03 -05002759 istk->mstk->condcnt --;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002760 free_tlist(origline);
2761 return DIRECTIVE_FOUND;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002762
H. Peter Anvindb8f96e2009-07-15 09:07:29 -04002763 case PP_RMACRO:
2764 case PP_IRMACRO:
H. Peter Anvine2c80182005-01-15 22:15:51 +00002765 case PP_MACRO:
2766 case PP_IMACRO:
H. Peter Anvina26433d2008-07-16 14:40:01 -07002767 if (defining) {
H. Peter Anvindb8f96e2009-07-15 09:07:29 -04002768 error(ERR_FATAL, "`%s': already defining a macro",
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002769 pp_directives[i]);
2770 return DIRECTIVE_FOUND;
2771 }
2772 defining = nasm_malloc(sizeof(MMacro));
2773 defining->max_depth =
2774 (i == PP_RMACRO) || (i == PP_IRMACRO) ? DEADMAN_LIMIT : 0;
2775 defining->casesense = (i == PP_MACRO) || (i == PP_RMACRO);
2776 if (!parse_mmacro_spec(tline, defining, pp_directives[i])) {
2777 nasm_free(defining);
2778 defining = NULL;
2779 return DIRECTIVE_FOUND;
2780 }
H. Peter Anvina26433d2008-07-16 14:40:01 -07002781
H. Peter Anvin166c2472008-05-28 12:28:58 -07002782 mmac = (MMacro *) hash_findix(&mmacros, defining->name);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002783 while (mmac) {
2784 if (!strcmp(mmac->name, defining->name) &&
2785 (mmac->nparam_min <= defining->nparam_max
2786 || defining->plus)
2787 && (defining->nparam_min <= mmac->nparam_max
2788 || mmac->plus)) {
H. Peter Anvin917a3492008-09-24 09:14:49 -07002789 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +00002790 "redefining multi-line macro `%s'", defining->name);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002791 return DIRECTIVE_FOUND;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002792 }
2793 mmac = mmac->next;
2794 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002795 free_tlist(origline);
2796 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002797
H. Peter Anvine2c80182005-01-15 22:15:51 +00002798 case PP_ENDM:
2799 case PP_ENDMACRO:
Victor van den Elzen8f1120f2008-07-16 13:41:37 +02002800 if (! (defining && defining->name)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002801 error(ERR_NONFATAL, "`%s': not defining a macro", tline->text);
2802 return DIRECTIVE_FOUND;
2803 }
Keith Kanios852f1ee2009-07-12 00:19:55 -05002804 mmhead = (MMacro **) hash_findi_add(&mmacros, defining->name);
H. Peter Anvin97a23472007-09-16 17:57:25 -07002805 defining->next = *mmhead;
Keith Kanios852f1ee2009-07-12 00:19:55 -05002806 *mmhead = defining;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002807 defining = NULL;
2808 free_tlist(origline);
2809 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002810
H. Peter Anvin89cee572009-07-15 09:16:54 -04002811 case PP_EXITMACRO:
Keith Kanios852f1ee2009-07-12 00:19:55 -05002812 /*
2813 * We must search along istk->expansion until we hit a
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002814 * macro-end marker for a macro with a name. Then we
Keith Kanios3c0d91f2009-10-25 13:28:03 -05002815 * bypass all lines between exitmacro and endmacro.
Keith Kanios852f1ee2009-07-12 00:19:55 -05002816 */
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04002817 list_for_each(l, istk->expansion)
Keith Kanios852f1ee2009-07-12 00:19:55 -05002818 if (l->finishes && l->finishes->name)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002819 break;
Keith Kanios852f1ee2009-07-12 00:19:55 -05002820
2821 if (l) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002822 /*
2823 * Remove all conditional entries relative to this
2824 * macro invocation. (safe to do in this context)
2825 */
Keith Kanios3c0d91f2009-10-25 13:28:03 -05002826 for ( ; l->finishes->condcnt > 0; l->finishes->condcnt --) {
2827 cond = istk->conds;
2828 istk->conds = cond->next;
2829 nasm_free(cond);
2830 }
2831 istk->expansion = l;
Keith Kanios852f1ee2009-07-12 00:19:55 -05002832 } else {
2833 error(ERR_NONFATAL, "`%%exitmacro' not within `%%macro' block");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002834 }
Keith Kanios852f1ee2009-07-12 00:19:55 -05002835 free_tlist(origline);
2836 return DIRECTIVE_FOUND;
2837
H. Peter Anvina26433d2008-07-16 14:40:01 -07002838 case PP_UNMACRO:
2839 case PP_UNIMACRO:
2840 {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002841 MMacro **mmac_p;
2842 MMacro spec;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002843
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002844 spec.casesense = (i == PP_UNMACRO);
2845 if (!parse_mmacro_spec(tline, &spec, pp_directives[i])) {
2846 return DIRECTIVE_FOUND;
2847 }
H. Peter Anvina26433d2008-07-16 14:40:01 -07002848 mmac_p = (MMacro **) hash_findi(&mmacros, spec.name, NULL);
2849 while (mmac_p && *mmac_p) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002850 mmac = *mmac_p;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002851 if (mmac->casesense == spec.casesense &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002852 !mstrcmp(mmac->name, spec.name, spec.casesense) &&
H. Peter Anvina26433d2008-07-16 14:40:01 -07002853 mmac->nparam_min == spec.nparam_min &&
2854 mmac->nparam_max == spec.nparam_max &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002855 mmac->plus == spec.plus) {
2856 *mmac_p = mmac->next;
2857 free_mmacro(mmac);
2858 } else {
2859 mmac_p = &mmac->next;
2860 }
2861 }
2862 free_tlist(origline);
2863 free_tlist(spec.dlist);
2864 return DIRECTIVE_FOUND;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002865 }
2866
H. Peter Anvine2c80182005-01-15 22:15:51 +00002867 case PP_ROTATE:
2868 if (tline->next && tline->next->type == TOK_WHITESPACE)
2869 tline = tline->next;
H. Peter Anvin89cee572009-07-15 09:16:54 -04002870 if (!tline->next) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002871 free_tlist(origline);
2872 error(ERR_NONFATAL, "`%%rotate' missing rotate count");
2873 return DIRECTIVE_FOUND;
2874 }
2875 t = expand_smacro(tline->next);
2876 tline->next = NULL;
2877 free_tlist(origline);
2878 tline = t;
2879 tptr = &t;
2880 tokval.t_type = TOKEN_INVALID;
2881 evalresult =
2882 evaluate(ppscan, tptr, &tokval, NULL, pass, error, NULL);
2883 free_tlist(tline);
2884 if (!evalresult)
2885 return DIRECTIVE_FOUND;
2886 if (tokval.t_type)
H. Peter Anvin917a3492008-09-24 09:14:49 -07002887 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +00002888 "trailing garbage after expression ignored");
2889 if (!is_simple(evalresult)) {
2890 error(ERR_NONFATAL, "non-constant value given to `%%rotate'");
2891 return DIRECTIVE_FOUND;
2892 }
2893 mmac = istk->mstk;
2894 while (mmac && !mmac->name) /* avoid mistaking %reps for macros */
2895 mmac = mmac->next_active;
2896 if (!mmac) {
2897 error(ERR_NONFATAL, "`%%rotate' invoked outside a macro call");
2898 } else if (mmac->nparam == 0) {
2899 error(ERR_NONFATAL,
2900 "`%%rotate' invoked within macro without parameters");
2901 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002902 int rotate = mmac->rotate + reloc_value(evalresult);
H. Peter Anvin734b1882002-04-30 21:01:08 +00002903
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002904 rotate %= (int)mmac->nparam;
2905 if (rotate < 0)
2906 rotate += mmac->nparam;
H. Peter Anvin70653092007-10-19 14:42:29 -07002907
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002908 mmac->rotate = rotate;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002909 }
2910 return DIRECTIVE_FOUND;
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00002911
H. Peter Anvine2c80182005-01-15 22:15:51 +00002912 case PP_REP:
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002913 nolist = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002914 do {
2915 tline = tline->next;
2916 } while (tok_type_(tline, TOK_WHITESPACE));
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00002917
H. Peter Anvine2c80182005-01-15 22:15:51 +00002918 if (tok_type_(tline, TOK_ID) &&
2919 nasm_stricmp(tline->text, ".nolist") == 0) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002920 nolist = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002921 do {
2922 tline = tline->next;
2923 } while (tok_type_(tline, TOK_WHITESPACE));
2924 }
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00002925
H. Peter Anvine2c80182005-01-15 22:15:51 +00002926 if (tline) {
2927 t = expand_smacro(tline);
2928 tptr = &t;
2929 tokval.t_type = TOKEN_INVALID;
2930 evalresult =
2931 evaluate(ppscan, tptr, &tokval, NULL, pass, error, NULL);
2932 if (!evalresult) {
2933 free_tlist(origline);
2934 return DIRECTIVE_FOUND;
2935 }
2936 if (tokval.t_type)
H. Peter Anvin917a3492008-09-24 09:14:49 -07002937 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +00002938 "trailing garbage after expression ignored");
2939 if (!is_simple(evalresult)) {
2940 error(ERR_NONFATAL, "non-constant value given to `%%rep'");
2941 return DIRECTIVE_FOUND;
2942 }
Cyrill Gorcunove091d6e2010-08-09 13:58:22 +04002943 count = reloc_value(evalresult);
2944 if (count >= REP_LIMIT) {
Cyrill Gorcunov71f4f842010-08-09 20:17:17 +04002945 error(ERR_NONFATAL, "`%%rep' value exceeds limit");
Cyrill Gorcunove091d6e2010-08-09 13:58:22 +04002946 count = 0;
2947 } else
2948 count++;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002949 } else {
2950 error(ERR_NONFATAL, "`%%rep' expects a repeat count");
H. Peter Anvinf8ba53e2007-10-11 10:11:57 -07002951 count = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002952 }
2953 free_tlist(origline);
H. Peter Anvin734b1882002-04-30 21:01:08 +00002954
H. Peter Anvine2c80182005-01-15 22:15:51 +00002955 tmp_defining = defining;
2956 defining = nasm_malloc(sizeof(MMacro));
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002957 defining->prev = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002958 defining->name = NULL; /* flags this macro as a %rep block */
H. Peter Anvin70055962007-10-11 00:05:31 -07002959 defining->casesense = false;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002960 defining->plus = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002961 defining->nolist = nolist;
H. Peter Anvinf8ba53e2007-10-11 10:11:57 -07002962 defining->in_progress = count;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002963 defining->max_depth = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002964 defining->nparam_min = defining->nparam_max = 0;
2965 defining->defaults = NULL;
2966 defining->dlist = NULL;
2967 defining->expansion = NULL;
2968 defining->next_active = istk->mstk;
2969 defining->rep_nest = tmp_defining;
2970 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002971
H. Peter Anvine2c80182005-01-15 22:15:51 +00002972 case PP_ENDREP:
2973 if (!defining || defining->name) {
2974 error(ERR_NONFATAL, "`%%endrep': no matching `%%rep'");
2975 return DIRECTIVE_FOUND;
2976 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00002977
H. Peter Anvine2c80182005-01-15 22:15:51 +00002978 /*
2979 * Now we have a "macro" defined - although it has no name
2980 * and we won't be entering it in the hash tables - we must
2981 * push a macro-end marker for it on to istk->expansion.
2982 * After that, it will take care of propagating itself (a
2983 * macro-end marker line for a macro which is really a %rep
2984 * block will cause the macro to be re-expanded, complete
2985 * with another macro-end marker to ensure the process
2986 * continues) until the whole expansion is forcibly removed
2987 * from istk->expansion by a %exitrep.
2988 */
2989 l = nasm_malloc(sizeof(Line));
2990 l->next = istk->expansion;
2991 l->finishes = defining;
2992 l->first = NULL;
2993 istk->expansion = l;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002994
H. Peter Anvine2c80182005-01-15 22:15:51 +00002995 istk->mstk = defining;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002996
H. Peter Anvine2c80182005-01-15 22:15:51 +00002997 list->uplevel(defining->nolist ? LIST_MACRO_NOLIST : LIST_MACRO);
2998 tmp_defining = defining;
2999 defining = defining->rep_nest;
3000 free_tlist(origline);
3001 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003002
H. Peter Anvine2c80182005-01-15 22:15:51 +00003003 case PP_EXITREP:
3004 /*
3005 * We must search along istk->expansion until we hit a
3006 * macro-end marker for a macro with no name. Then we set
3007 * its `in_progress' flag to 0.
3008 */
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04003009 list_for_each(l, istk->expansion)
H. Peter Anvine2c80182005-01-15 22:15:51 +00003010 if (l->finishes && !l->finishes->name)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003011 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003012
H. Peter Anvine2c80182005-01-15 22:15:51 +00003013 if (l)
Charles Crayned4200be2008-07-12 16:42:33 -07003014 l->finishes->in_progress = 1;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003015 else
3016 error(ERR_NONFATAL, "`%%exitrep' not within `%%rep' block");
3017 free_tlist(origline);
3018 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003019
H. Peter Anvine2c80182005-01-15 22:15:51 +00003020 case PP_XDEFINE:
3021 case PP_IXDEFINE:
3022 case PP_DEFINE:
3023 case PP_IDEFINE:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003024 casesense = (i == PP_DEFINE || i == PP_XDEFINE);
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07003025
H. Peter Anvine2c80182005-01-15 22:15:51 +00003026 tline = tline->next;
3027 skip_white_(tline);
3028 tline = expand_id(tline);
3029 if (!tline || (tline->type != TOK_ID &&
3030 (tline->type != TOK_PREPROC_ID ||
3031 tline->text[1] != '$'))) {
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07003032 error(ERR_NONFATAL, "`%s' expects a macro identifier",
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003033 pp_directives[i]);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003034 free_tlist(origline);
3035 return DIRECTIVE_FOUND;
3036 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003037
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08003038 ctx = get_ctx(tline->text, &mname, false);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003039 last = tline;
3040 param_start = tline = tline->next;
3041 nparam = 0;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003042
H. Peter Anvine2c80182005-01-15 22:15:51 +00003043 /* Expand the macro definition now for %xdefine and %ixdefine */
3044 if ((i == PP_XDEFINE) || (i == PP_IXDEFINE))
3045 tline = expand_smacro(tline);
H. Peter Anvin734b1882002-04-30 21:01:08 +00003046
H. Peter Anvine2c80182005-01-15 22:15:51 +00003047 if (tok_is_(tline, "(")) {
3048 /*
3049 * This macro has parameters.
3050 */
H. Peter Anvin734b1882002-04-30 21:01:08 +00003051
H. Peter Anvine2c80182005-01-15 22:15:51 +00003052 tline = tline->next;
3053 while (1) {
3054 skip_white_(tline);
3055 if (!tline) {
3056 error(ERR_NONFATAL, "parameter identifier expected");
3057 free_tlist(origline);
3058 return DIRECTIVE_FOUND;
3059 }
3060 if (tline->type != TOK_ID) {
3061 error(ERR_NONFATAL,
3062 "`%s': parameter identifier expected",
3063 tline->text);
3064 free_tlist(origline);
3065 return DIRECTIVE_FOUND;
3066 }
3067 tline->type = TOK_SMAC_PARAM + nparam++;
3068 tline = tline->next;
3069 skip_white_(tline);
3070 if (tok_is_(tline, ",")) {
3071 tline = tline->next;
H. Peter Anvinca348b62008-07-23 10:49:26 -04003072 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003073 if (!tok_is_(tline, ")")) {
3074 error(ERR_NONFATAL,
3075 "`)' expected to terminate macro template");
3076 free_tlist(origline);
3077 return DIRECTIVE_FOUND;
3078 }
3079 break;
3080 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00003081 }
3082 last = tline;
3083 tline = tline->next;
3084 }
3085 if (tok_type_(tline, TOK_WHITESPACE))
3086 last = tline, tline = tline->next;
3087 macro_start = NULL;
3088 last->next = NULL;
3089 t = tline;
3090 while (t) {
3091 if (t->type == TOK_ID) {
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04003092 list_for_each(tt, param_start)
H. Peter Anvine2c80182005-01-15 22:15:51 +00003093 if (tt->type >= TOK_SMAC_PARAM &&
3094 !strcmp(tt->text, t->text))
3095 t->type = tt->type;
3096 }
3097 tt = t->next;
3098 t->next = macro_start;
3099 macro_start = t;
3100 t = tt;
3101 }
3102 /*
3103 * Good. We now have a macro name, a parameter count, and a
3104 * token list (in reverse order) for an expansion. We ought
3105 * to be OK just to create an SMacro, store it, and let
3106 * free_tlist have the rest of the line (which we have
3107 * carefully re-terminated after chopping off the expansion
3108 * from the end).
3109 */
H. Peter Anvin4db5a162007-10-11 13:42:09 -07003110 define_smacro(ctx, mname, casesense, nparam, macro_start);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003111 free_tlist(origline);
3112 return DIRECTIVE_FOUND;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003113
H. Peter Anvine2c80182005-01-15 22:15:51 +00003114 case PP_UNDEF:
3115 tline = tline->next;
3116 skip_white_(tline);
3117 tline = expand_id(tline);
3118 if (!tline || (tline->type != TOK_ID &&
3119 (tline->type != TOK_PREPROC_ID ||
3120 tline->text[1] != '$'))) {
3121 error(ERR_NONFATAL, "`%%undef' expects a macro identifier");
3122 free_tlist(origline);
3123 return DIRECTIVE_FOUND;
3124 }
3125 if (tline->next) {
H. Peter Anvin917a3492008-09-24 09:14:49 -07003126 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +00003127 "trailing garbage after macro name ignored");
3128 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003129
H. Peter Anvine2c80182005-01-15 22:15:51 +00003130 /* Find the context that symbol belongs to */
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08003131 ctx = get_ctx(tline->text, &mname, false);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003132 undef_smacro(ctx, mname);
3133 free_tlist(origline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003134 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003135
H. Peter Anvin9e200162008-06-04 17:23:14 -07003136 case PP_DEFSTR:
3137 case PP_IDEFSTR:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003138 casesense = (i == PP_DEFSTR);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003139
3140 tline = tline->next;
3141 skip_white_(tline);
3142 tline = expand_id(tline);
3143 if (!tline || (tline->type != TOK_ID &&
3144 (tline->type != TOK_PREPROC_ID ||
3145 tline->text[1] != '$'))) {
3146 error(ERR_NONFATAL, "`%s' expects a macro identifier",
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003147 pp_directives[i]);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003148 free_tlist(origline);
3149 return DIRECTIVE_FOUND;
3150 }
3151
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08003152 ctx = get_ctx(tline->text, &mname, false);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003153 last = tline;
3154 tline = expand_smacro(tline->next);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003155 last->next = NULL;
H. Peter Anvin9e200162008-06-04 17:23:14 -07003156
3157 while (tok_type_(tline, TOK_WHITESPACE))
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003158 tline = delete_Token(tline);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003159
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003160 p = detoken(tline, false);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003161 macro_start = nasm_malloc(sizeof(*macro_start));
3162 macro_start->next = NULL;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003163 macro_start->text = nasm_quote(p, strlen(p));
3164 macro_start->type = TOK_STRING;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003165 macro_start->a.mac = NULL;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003166 nasm_free(p);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003167
3168 /*
3169 * We now have a macro name, an implicit parameter count of
3170 * zero, and a string token to use as an expansion. Create
3171 * and store an SMacro.
3172 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003173 define_smacro(ctx, mname, casesense, 0, macro_start);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003174 free_tlist(origline);
3175 return DIRECTIVE_FOUND;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003176
H. Peter Anvin2f55bda2009-07-14 15:04:04 -04003177 case PP_DEFTOK:
3178 case PP_IDEFTOK:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003179 casesense = (i == PP_DEFTOK);
3180
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003181 tline = tline->next;
3182 skip_white_(tline);
3183 tline = expand_id(tline);
3184 if (!tline || (tline->type != TOK_ID &&
3185 (tline->type != TOK_PREPROC_ID ||
3186 tline->text[1] != '$'))) {
3187 error(ERR_NONFATAL,
3188 "`%s' expects a macro identifier as first parameter",
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003189 pp_directives[i]);
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003190 free_tlist(origline);
3191 return DIRECTIVE_FOUND;
3192 }
3193 ctx = get_ctx(tline->text, &mname, false);
3194 last = tline;
3195 tline = expand_smacro(tline->next);
3196 last->next = NULL;
3197
3198 t = tline;
3199 while (tok_type_(t, TOK_WHITESPACE))
3200 t = t->next;
3201 /* t should now point to the string */
Cyrill Gorcunove12c50d2010-09-06 19:36:15 +04003202 if (!tok_type_(t, TOK_STRING)) {
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003203 error(ERR_NONFATAL,
3204 "`%s` requires string as second parameter",
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003205 pp_directives[i]);
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003206 free_tlist(tline);
3207 free_tlist(origline);
3208 return DIRECTIVE_FOUND;
3209 }
3210
H. Peter Anvinb40992c2010-09-15 08:57:21 -07003211 /*
3212 * Convert the string to a token stream. Note that smacros
3213 * are stored with the token stream reversed, so we have to
3214 * reverse the output of tokenize().
3215 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003216 nasm_unquote_cstr(t->text, i);
H. Peter Anvinb40992c2010-09-15 08:57:21 -07003217 macro_start = reverse_tokens(tokenize(t->text));
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003218
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003219 /*
3220 * We now have a macro name, an implicit parameter count of
3221 * zero, and a numeric token to use as an expansion. Create
3222 * and store an SMacro.
3223 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003224 define_smacro(ctx, mname, casesense, 0, macro_start);
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003225 free_tlist(tline);
3226 free_tlist(origline);
3227 return DIRECTIVE_FOUND;
H. Peter Anvin9e200162008-06-04 17:23:14 -07003228
H. Peter Anvin418ca702008-05-30 10:42:30 -07003229 case PP_PATHSEARCH:
3230 {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003231 FILE *fp;
3232 StrList *xsl = NULL;
3233 StrList **xst = &xsl;
H. Peter Anvin418ca702008-05-30 10:42:30 -07003234
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003235 casesense = true;
H. Peter Anvin418ca702008-05-30 10:42:30 -07003236
3237 tline = tline->next;
3238 skip_white_(tline);
3239 tline = expand_id(tline);
3240 if (!tline || (tline->type != TOK_ID &&
3241 (tline->type != TOK_PREPROC_ID ||
3242 tline->text[1] != '$'))) {
3243 error(ERR_NONFATAL,
3244 "`%%pathsearch' expects a macro identifier as first parameter");
3245 free_tlist(origline);
3246 return DIRECTIVE_FOUND;
3247 }
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08003248 ctx = get_ctx(tline->text, &mname, false);
H. Peter Anvin418ca702008-05-30 10:42:30 -07003249 last = tline;
3250 tline = expand_smacro(tline->next);
3251 last->next = NULL;
3252
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003253 t = tline;
H. Peter Anvin418ca702008-05-30 10:42:30 -07003254 while (tok_type_(t, TOK_WHITESPACE))
3255 t = t->next;
3256
3257 if (!t || (t->type != TOK_STRING &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003258 t->type != TOK_INTERNAL_STRING)) {
H. Peter Anvin418ca702008-05-30 10:42:30 -07003259 error(ERR_NONFATAL, "`%%pathsearch' expects a file name");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003260 free_tlist(tline);
H. Peter Anvin418ca702008-05-30 10:42:30 -07003261 free_tlist(origline);
3262 return DIRECTIVE_FOUND; /* but we did _something_ */
3263 }
3264 if (t->next)
H. Peter Anvin917a3492008-09-24 09:14:49 -07003265 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvin418ca702008-05-30 10:42:30 -07003266 "trailing garbage after `%%pathsearch' ignored");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003267 p = t->text;
H. Peter Anvin427cc912008-06-01 21:43:03 -07003268 if (t->type != TOK_INTERNAL_STRING)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003269 nasm_unquote(p, NULL);
H. Peter Anvin418ca702008-05-30 10:42:30 -07003270
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003271 fp = inc_fopen(p, &xsl, &xst, true);
3272 if (fp) {
3273 p = xsl->str;
3274 fclose(fp); /* Don't actually care about the file */
3275 }
H. Peter Anvin418ca702008-05-30 10:42:30 -07003276 macro_start = nasm_malloc(sizeof(*macro_start));
3277 macro_start->next = NULL;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003278 macro_start->text = nasm_quote(p, strlen(p));
3279 macro_start->type = TOK_STRING;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003280 macro_start->a.mac = NULL;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003281 if (xsl)
3282 nasm_free(xsl);
H. Peter Anvin418ca702008-05-30 10:42:30 -07003283
3284 /*
3285 * We now have a macro name, an implicit parameter count of
3286 * zero, and a string token to use as an expansion. Create
3287 * and store an SMacro.
3288 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003289 define_smacro(ctx, mname, casesense, 0, macro_start);
3290 free_tlist(tline);
H. Peter Anvin418ca702008-05-30 10:42:30 -07003291 free_tlist(origline);
3292 return DIRECTIVE_FOUND;
3293 }
3294
H. Peter Anvine2c80182005-01-15 22:15:51 +00003295 case PP_STRLEN:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003296 casesense = true;
H. Peter Anvin70653092007-10-19 14:42:29 -07003297
H. Peter Anvine2c80182005-01-15 22:15:51 +00003298 tline = tline->next;
3299 skip_white_(tline);
3300 tline = expand_id(tline);
3301 if (!tline || (tline->type != TOK_ID &&
3302 (tline->type != TOK_PREPROC_ID ||
3303 tline->text[1] != '$'))) {
3304 error(ERR_NONFATAL,
3305 "`%%strlen' expects a macro identifier as first parameter");
3306 free_tlist(origline);
3307 return DIRECTIVE_FOUND;
3308 }
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08003309 ctx = get_ctx(tline->text, &mname, false);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003310 last = tline;
3311 tline = expand_smacro(tline->next);
3312 last->next = NULL;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003313
H. Peter Anvine2c80182005-01-15 22:15:51 +00003314 t = tline;
3315 while (tok_type_(t, TOK_WHITESPACE))
3316 t = t->next;
3317 /* t should now point to the string */
Cyrill Gorcunov4e1d5ab2010-07-23 18:51:51 +04003318 if (!tok_type_(t, TOK_STRING)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00003319 error(ERR_NONFATAL,
3320 "`%%strlen` requires string as second parameter");
3321 free_tlist(tline);
3322 free_tlist(origline);
3323 return DIRECTIVE_FOUND;
3324 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003325
H. Peter Anvine2c80182005-01-15 22:15:51 +00003326 macro_start = nasm_malloc(sizeof(*macro_start));
3327 macro_start->next = NULL;
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07003328 make_tok_num(macro_start, nasm_unquote(t->text, NULL));
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003329 macro_start->a.mac = NULL;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003330
H. Peter Anvine2c80182005-01-15 22:15:51 +00003331 /*
3332 * We now have a macro name, an implicit parameter count of
3333 * zero, and a numeric token to use as an expansion. Create
3334 * and store an SMacro.
3335 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003336 define_smacro(ctx, mname, casesense, 0, macro_start);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003337 free_tlist(tline);
3338 free_tlist(origline);
3339 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003340
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003341 case PP_STRCAT:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003342 casesense = true;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003343
3344 tline = tline->next;
3345 skip_white_(tline);
3346 tline = expand_id(tline);
3347 if (!tline || (tline->type != TOK_ID &&
3348 (tline->type != TOK_PREPROC_ID ||
3349 tline->text[1] != '$'))) {
3350 error(ERR_NONFATAL,
3351 "`%%strcat' expects a macro identifier as first parameter");
3352 free_tlist(origline);
3353 return DIRECTIVE_FOUND;
3354 }
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08003355 ctx = get_ctx(tline->text, &mname, false);
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003356 last = tline;
3357 tline = expand_smacro(tline->next);
3358 last->next = NULL;
3359
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003360 len = 0;
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04003361 list_for_each(t, tline) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003362 switch (t->type) {
3363 case TOK_WHITESPACE:
3364 break;
3365 case TOK_STRING:
3366 len += t->a.len = nasm_unquote(t->text, NULL);
3367 break;
3368 case TOK_OTHER:
3369 if (!strcmp(t->text, ",")) /* permit comma separators */
3370 break;
3371 /* else fall through */
3372 default:
3373 error(ERR_NONFATAL,
3374 "non-string passed to `%%strcat' (%d)", t->type);
3375 free_tlist(tline);
3376 free_tlist(origline);
3377 return DIRECTIVE_FOUND;
3378 }
3379 }
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003380
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003381 p = pp = nasm_malloc(len);
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04003382 list_for_each(t, tline) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003383 if (t->type == TOK_STRING) {
3384 memcpy(p, t->text, t->a.len);
3385 p += t->a.len;
3386 }
3387 }
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003388
3389 /*
3390 * We now have a macro name, an implicit parameter count of
3391 * zero, and a numeric token to use as an expansion. Create
3392 * and store an SMacro.
3393 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003394 macro_start = new_Token(NULL, TOK_STRING, NULL, 0);
3395 macro_start->text = nasm_quote(pp, len);
3396 nasm_free(pp);
3397 define_smacro(ctx, mname, casesense, 0, macro_start);
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003398 free_tlist(tline);
3399 free_tlist(origline);
3400 return DIRECTIVE_FOUND;
3401
H. Peter Anvine2c80182005-01-15 22:15:51 +00003402 case PP_SUBSTR:
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003403 {
Cyrill Gorcunov8fccbf32010-09-07 10:42:02 +04003404 int64_t start, count;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003405 size_t len;
H. Peter Anvind2456592008-06-19 15:04:18 -07003406
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003407 casesense = true;
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07003408
H. Peter Anvine2c80182005-01-15 22:15:51 +00003409 tline = tline->next;
3410 skip_white_(tline);
3411 tline = expand_id(tline);
3412 if (!tline || (tline->type != TOK_ID &&
3413 (tline->type != TOK_PREPROC_ID ||
3414 tline->text[1] != '$'))) {
3415 error(ERR_NONFATAL,
3416 "`%%substr' expects a macro identifier as first parameter");
3417 free_tlist(origline);
3418 return DIRECTIVE_FOUND;
3419 }
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08003420 ctx = get_ctx(tline->text, &mname, false);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003421 last = tline;
3422 tline = expand_smacro(tline->next);
3423 last->next = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003424
Cyrill Gorcunov49cd6fb2010-09-06 23:49:52 +04003425 if (tline) /* skip expanded id */
3426 t = tline->next;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003427 while (tok_type_(t, TOK_WHITESPACE))
3428 t = t->next;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003429
H. Peter Anvine2c80182005-01-15 22:15:51 +00003430 /* t should now point to the string */
Cyrill Gorcunov49cd6fb2010-09-06 23:49:52 +04003431 if (!tok_type_(t, TOK_STRING)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00003432 error(ERR_NONFATAL,
3433 "`%%substr` requires string as second parameter");
3434 free_tlist(tline);
3435 free_tlist(origline);
3436 return DIRECTIVE_FOUND;
3437 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003438
H. Peter Anvine2c80182005-01-15 22:15:51 +00003439 tt = t->next;
3440 tptr = &tt;
3441 tokval.t_type = TOKEN_INVALID;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003442 evalresult = evaluate(ppscan, tptr, &tokval, NULL,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003443 pass, error, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003444 if (!evalresult) {
3445 free_tlist(tline);
3446 free_tlist(origline);
3447 return DIRECTIVE_FOUND;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003448 } else if (!is_simple(evalresult)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00003449 error(ERR_NONFATAL, "non-constant value given to `%%substr`");
3450 free_tlist(tline);
3451 free_tlist(origline);
3452 return DIRECTIVE_FOUND;
3453 }
Cyrill Gorcunov8fccbf32010-09-07 10:42:02 +04003454 start = evalresult->value - 1;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003455
3456 while (tok_type_(tt, TOK_WHITESPACE))
3457 tt = tt->next;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003458 if (!tt) {
Cyrill Gorcunov8fccbf32010-09-07 10:42:02 +04003459 count = 1; /* Backwards compatibility: one character */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003460 } else {
3461 tokval.t_type = TOKEN_INVALID;
3462 evalresult = evaluate(ppscan, tptr, &tokval, NULL,
3463 pass, error, NULL);
3464 if (!evalresult) {
3465 free_tlist(tline);
3466 free_tlist(origline);
3467 return DIRECTIVE_FOUND;
3468 } else if (!is_simple(evalresult)) {
3469 error(ERR_NONFATAL, "non-constant value given to `%%substr`");
3470 free_tlist(tline);
3471 free_tlist(origline);
3472 return DIRECTIVE_FOUND;
3473 }
Cyrill Gorcunov8fccbf32010-09-07 10:42:02 +04003474 count = evalresult->value;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003475 }
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003476
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003477 len = nasm_unquote(t->text, NULL);
Cyrill Gorcunov8fccbf32010-09-07 10:42:02 +04003478
Cyrill Gorcunovbf11db62010-09-07 20:31:11 +04003479 /* make start and count being in range */
3480 if (start < 0)
3481 start = 0;
Cyrill Gorcunov8fccbf32010-09-07 10:42:02 +04003482 if (count < 0)
3483 count = len + count + 1 - start;
3484 if (start + count > (int64_t)len)
Cyrill Gorcunovbf11db62010-09-07 20:31:11 +04003485 count = len - start;
3486 if (!len || count < 0 || start >=(int64_t)len)
Cyrill Gorcunov8fccbf32010-09-07 10:42:02 +04003487 start = -1, count = 0; /* empty string */
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003488
H. Peter Anvine2c80182005-01-15 22:15:51 +00003489 macro_start = nasm_malloc(sizeof(*macro_start));
3490 macro_start->next = NULL;
Cyrill Gorcunov8fccbf32010-09-07 10:42:02 +04003491 macro_start->text = nasm_quote((start < 0) ? "" : t->text + start, count);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003492 macro_start->type = TOK_STRING;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003493 macro_start->a.mac = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003494
H. Peter Anvine2c80182005-01-15 22:15:51 +00003495 /*
3496 * We now have a macro name, an implicit parameter count of
3497 * zero, and a numeric token to use as an expansion. Create
3498 * and store an SMacro.
3499 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003500 define_smacro(ctx, mname, casesense, 0, macro_start);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003501 free_tlist(tline);
3502 free_tlist(origline);
3503 return DIRECTIVE_FOUND;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003504 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003505
H. Peter Anvine2c80182005-01-15 22:15:51 +00003506 case PP_ASSIGN:
3507 case PP_IASSIGN:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003508 casesense = (i == PP_ASSIGN);
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07003509
H. Peter Anvine2c80182005-01-15 22:15:51 +00003510 tline = tline->next;
3511 skip_white_(tline);
3512 tline = expand_id(tline);
3513 if (!tline || (tline->type != TOK_ID &&
3514 (tline->type != TOK_PREPROC_ID ||
3515 tline->text[1] != '$'))) {
3516 error(ERR_NONFATAL,
3517 "`%%%sassign' expects a macro identifier",
3518 (i == PP_IASSIGN ? "i" : ""));
3519 free_tlist(origline);
3520 return DIRECTIVE_FOUND;
3521 }
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08003522 ctx = get_ctx(tline->text, &mname, false);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003523 last = tline;
3524 tline = expand_smacro(tline->next);
3525 last->next = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003526
H. Peter Anvine2c80182005-01-15 22:15:51 +00003527 t = tline;
3528 tptr = &t;
3529 tokval.t_type = TOKEN_INVALID;
3530 evalresult =
3531 evaluate(ppscan, tptr, &tokval, NULL, pass, error, NULL);
3532 free_tlist(tline);
3533 if (!evalresult) {
3534 free_tlist(origline);
3535 return DIRECTIVE_FOUND;
3536 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003537
H. Peter Anvine2c80182005-01-15 22:15:51 +00003538 if (tokval.t_type)
H. Peter Anvin917a3492008-09-24 09:14:49 -07003539 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +00003540 "trailing garbage after expression ignored");
H. Peter Anvin734b1882002-04-30 21:01:08 +00003541
H. Peter Anvine2c80182005-01-15 22:15:51 +00003542 if (!is_simple(evalresult)) {
3543 error(ERR_NONFATAL,
3544 "non-constant value given to `%%%sassign'",
3545 (i == PP_IASSIGN ? "i" : ""));
3546 free_tlist(origline);
3547 return DIRECTIVE_FOUND;
3548 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003549
H. Peter Anvine2c80182005-01-15 22:15:51 +00003550 macro_start = nasm_malloc(sizeof(*macro_start));
3551 macro_start->next = NULL;
3552 make_tok_num(macro_start, reloc_value(evalresult));
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003553 macro_start->a.mac = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003554
H. Peter Anvine2c80182005-01-15 22:15:51 +00003555 /*
3556 * We now have a macro name, an implicit parameter count of
3557 * zero, and a numeric token to use as an expansion. Create
3558 * and store an SMacro.
3559 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003560 define_smacro(ctx, mname, casesense, 0, macro_start);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003561 free_tlist(origline);
3562 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003563
H. Peter Anvine2c80182005-01-15 22:15:51 +00003564 case PP_LINE:
3565 /*
3566 * Syntax is `%line nnn[+mmm] [filename]'
3567 */
3568 tline = tline->next;
3569 skip_white_(tline);
3570 if (!tok_type_(tline, TOK_NUMBER)) {
3571 error(ERR_NONFATAL, "`%%line' expects line number");
3572 free_tlist(origline);
3573 return DIRECTIVE_FOUND;
3574 }
H. Peter Anvin70055962007-10-11 00:05:31 -07003575 k = readnum(tline->text, &err);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003576 m = 1;
3577 tline = tline->next;
3578 if (tok_is_(tline, "+")) {
3579 tline = tline->next;
3580 if (!tok_type_(tline, TOK_NUMBER)) {
3581 error(ERR_NONFATAL, "`%%line' expects line increment");
3582 free_tlist(origline);
3583 return DIRECTIVE_FOUND;
3584 }
H. Peter Anvin70055962007-10-11 00:05:31 -07003585 m = readnum(tline->text, &err);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003586 tline = tline->next;
3587 }
3588 skip_white_(tline);
3589 src_set_linnum(k);
3590 istk->lineinc = m;
3591 if (tline) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07003592 nasm_free(src_set_fname(detoken(tline, false)));
H. Peter Anvine2c80182005-01-15 22:15:51 +00003593 }
3594 free_tlist(origline);
3595 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003596
H. Peter Anvine2c80182005-01-15 22:15:51 +00003597 default:
3598 error(ERR_FATAL,
3599 "preprocessor directive `%s' not yet implemented",
H. Peter Anvin4169a472007-09-12 01:29:43 +00003600 pp_directives[i]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003601 return DIRECTIVE_FOUND;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003602 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003603}
3604
3605/*
H. Peter Anvin76690a12002-04-30 20:52:49 +00003606 * Ensure that a macro parameter contains a condition code and
3607 * nothing else. Return the condition code index if so, or -1
3608 * otherwise.
3609 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003610static int find_cc(Token * t)
H. Peter Anvineba20a72002-04-30 20:53:55 +00003611{
H. Peter Anvin76690a12002-04-30 20:52:49 +00003612 Token *tt;
3613 int i, j, k, m;
3614
H. Peter Anvin25a99342007-09-22 17:45:45 -07003615 if (!t)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003616 return -1; /* Probably a %+ without a space */
H. Peter Anvin25a99342007-09-22 17:45:45 -07003617
H. Peter Anvineba20a72002-04-30 20:53:55 +00003618 skip_white_(t);
H. Peter Anvin76690a12002-04-30 20:52:49 +00003619 if (t->type != TOK_ID)
H. Peter Anvine2c80182005-01-15 22:15:51 +00003620 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003621 tt = t->next;
H. Peter Anvineba20a72002-04-30 20:53:55 +00003622 skip_white_(tt);
H. Peter Anvin76690a12002-04-30 20:52:49 +00003623 if (tt && (tt->type != TOK_OTHER || strcmp(tt->text, ",")))
H. Peter Anvine2c80182005-01-15 22:15:51 +00003624 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003625
3626 i = -1;
Cyrill Gorcunova7319242010-06-03 22:04:36 +04003627 j = ARRAY_SIZE(conditions);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003628 while (j - i > 1) {
3629 k = (j + i) / 2;
3630 m = nasm_stricmp(t->text, conditions[k]);
3631 if (m == 0) {
3632 i = k;
3633 j = -2;
3634 break;
3635 } else if (m < 0) {
3636 j = k;
3637 } else
3638 i = k;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003639 }
3640 if (j != -2)
H. Peter Anvine2c80182005-01-15 22:15:51 +00003641 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003642 return i;
3643}
3644
H. Peter Anvind784a082009-04-20 14:01:18 -07003645static bool paste_tokens(Token **head, bool handle_paste_tokens)
3646{
3647 Token **tail, *t, *tt;
3648 Token **paste_head;
3649 bool did_paste = false;
3650 char *tmp;
3651
3652 /* Now handle token pasting... */
3653 paste_head = NULL;
3654 tail = head;
3655 while ((t = *tail) && (tt = t->next)) {
3656 switch (t->type) {
3657 case TOK_WHITESPACE:
3658 if (tt->type == TOK_WHITESPACE) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003659 /* Zap adjacent whitespace tokens */
H. Peter Anvind784a082009-04-20 14:01:18 -07003660 t->next = delete_Token(tt);
3661 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003662 /* Do not advance paste_head here */
3663 tail = &t->next;
3664 }
H. Peter Anvind784a082009-04-20 14:01:18 -07003665 break;
3666 case TOK_ID:
Cyrill Gorcunov8bc80172010-09-16 00:16:19 +04003667 case TOK_PREPROC_ID:
H. Peter Anvind784a082009-04-20 14:01:18 -07003668 case TOK_NUMBER:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003669 case TOK_FLOAT:
3670 {
3671 size_t len = 0;
3672 char *tmp, *p;
H. Peter Anvind784a082009-04-20 14:01:18 -07003673
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003674 while (tt && (tt->type == TOK_ID || tt->type == TOK_PREPROC_ID ||
3675 tt->type == TOK_NUMBER || tt->type == TOK_FLOAT ||
3676 tt->type == TOK_OTHER)) {
3677 len += strlen(tt->text);
3678 tt = tt->next;
3679 }
H. Peter Anvind784a082009-04-20 14:01:18 -07003680
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003681 /*
3682 * Now tt points to the first token after
3683 * the potential paste area...
3684 */
3685 if (tt != t->next) {
3686 /* We have at least two tokens... */
3687 len += strlen(t->text);
3688 p = tmp = nasm_malloc(len+1);
H. Peter Anvind784a082009-04-20 14:01:18 -07003689
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003690 while (t != tt) {
3691 strcpy(p, t->text);
3692 p = strchr(p, '\0');
3693 t = delete_Token(t);
3694 }
H. Peter Anvind784a082009-04-20 14:01:18 -07003695
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003696 t = *tail = tokenize(tmp);
3697 nasm_free(tmp);
H. Peter Anvind784a082009-04-20 14:01:18 -07003698
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003699 while (t->next) {
3700 tail = &t->next;
3701 t = t->next;
3702 }
3703 t->next = tt; /* Attach the remaining token chain */
H. Peter Anvind784a082009-04-20 14:01:18 -07003704
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003705 did_paste = true;
3706 }
3707 paste_head = tail;
3708 tail = &t->next;
3709 break;
3710 }
3711 case TOK_PASTE: /* %+ */
3712 if (handle_paste_tokens) {
3713 /* Zap %+ and whitespace tokens to the right */
3714 while (t && (t->type == TOK_WHITESPACE ||
3715 t->type == TOK_PASTE))
3716 t = *tail = delete_Token(t);
3717 if (!paste_head || !t)
3718 break; /* Nothing to paste with */
3719 tail = paste_head;
3720 t = *tail;
3721 tt = t->next;
3722 while (tok_type_(tt, TOK_WHITESPACE))
3723 tt = t->next = delete_Token(tt);
H. Peter Anvind784a082009-04-20 14:01:18 -07003724
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003725 if (tt) {
3726 tmp = nasm_strcat(t->text, tt->text);
3727 delete_Token(t);
3728 tt = delete_Token(tt);
3729 t = *tail = tokenize(tmp);
3730 nasm_free(tmp);
3731 while (t->next) {
3732 tail = &t->next;
3733 t = t->next;
3734 }
3735 t->next = tt; /* Attach the remaining token chain */
3736 did_paste = true;
3737 }
3738 paste_head = tail;
3739 tail = &t->next;
3740 break;
3741 }
3742 /* else fall through */
3743 default:
Cyrill Gorcunovadabc152010-07-10 02:11:41 +04003744 tail = &t->next;
3745 if (!tok_type_(t->next, TOK_WHITESPACE))
3746 paste_head = tail;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003747 break;
H. Peter Anvind784a082009-04-20 14:01:18 -07003748 }
3749 }
3750 return did_paste;
3751}
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003752
3753/*
3754 * expands to a list of tokens from %{x:y}
3755 */
3756static Token *expand_mmac_params_range(MMacro *mac, Token *tline, Token ***last)
3757{
3758 Token *t = tline, **tt, *tm, *head;
3759 char *pos;
3760 int fst, lst, j, i;
3761
3762 pos = strchr(tline->text, ':');
3763 nasm_assert(pos);
3764
3765 lst = atoi(pos + 1);
3766 fst = atoi(tline->text + 1);
3767
3768 /*
3769 * only macros params are accounted so
3770 * if someone passes %0 -- we reject such
3771 * value(s)
3772 */
3773 if (lst == 0 || fst == 0)
3774 goto err;
3775
3776 /* the values should be sane */
Cyrill Gorcunov2f403752010-06-05 10:47:10 +04003777 if ((fst > (int)mac->nparam || fst < (-(int)mac->nparam)) ||
3778 (lst > (int)mac->nparam || lst < (-(int)mac->nparam)))
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003779 goto err;
3780
Cyrill Gorcunovefb35832010-06-20 01:52:19 +04003781 fst = fst < 0 ? fst + (int)mac->nparam + 1: fst;
3782 lst = lst < 0 ? lst + (int)mac->nparam + 1: lst;
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003783
3784 /* counted from zero */
3785 fst--, lst--;
3786
3787 /*
3788 * it will be at least one token
3789 */
3790 tm = mac->params[(fst + mac->rotate) % mac->nparam];
3791 t = new_Token(NULL, tm->type, tm->text, 0);
3792 head = t, tt = &t->next;
3793 if (fst < lst) {
3794 for (i = fst + 1; i <= lst; i++) {
3795 t = new_Token(NULL, TOK_OTHER, ",", 0);
3796 *tt = t, tt = &t->next;
3797 j = (i + mac->rotate) % mac->nparam;
3798 tm = mac->params[j];
3799 t = new_Token(NULL, tm->type, tm->text, 0);
3800 *tt = t, tt = &t->next;
3801 }
3802 } else {
3803 for (i = fst - 1; i >= lst; i--) {
3804 t = new_Token(NULL, TOK_OTHER, ",", 0);
3805 *tt = t, tt = &t->next;
3806 j = (i + mac->rotate) % mac->nparam;
3807 tm = mac->params[j];
3808 t = new_Token(NULL, tm->type, tm->text, 0);
3809 *tt = t, tt = &t->next;
3810 }
3811 }
3812
3813 *last = tt;
3814 return head;
3815
3816err:
3817 error(ERR_NONFATAL, "`%%{%s}': macro parameters out of range",
3818 &tline->text[1]);
3819 return tline;
3820}
3821
H. Peter Anvin76690a12002-04-30 20:52:49 +00003822/*
3823 * Expand MMacro-local things: parameter references (%0, %n, %+n,
H. Peter Anvin67c63722008-10-26 23:49:00 -07003824 * %-n) and MMacro-local identifiers (%%foo) as well as
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003825 * macro indirection (%[...]) and range (%{..:..}).
H. Peter Anvin76690a12002-04-30 20:52:49 +00003826 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003827static Token *expand_mmac_params(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00003828{
H. Peter Anvin734b1882002-04-30 21:01:08 +00003829 Token *t, *tt, **tail, *thead;
H. Peter Anvin6125b622009-04-08 14:02:25 -07003830 bool changed = false;
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003831 char *pos;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003832
3833 tail = &thead;
3834 thead = NULL;
3835
H. Peter Anvine2c80182005-01-15 22:15:51 +00003836 while (tline) {
3837 if (tline->type == TOK_PREPROC_ID &&
Cyrill Gorcunovca611192010-06-04 09:22:12 +04003838 (((tline->text[1] == '+' || tline->text[1] == '-') && tline->text[2]) ||
3839 (tline->text[1] >= '0' && tline->text[1] <= '9') ||
3840 tline->text[1] == '%')) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00003841 char *text = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003842 int type = 0, cc; /* type = 0 to placate optimisers */
Keith Kaniosa6dfa782007-04-13 16:47:53 +00003843 char tmpbuf[30];
H. Peter Anvin25a99342007-09-22 17:45:45 -07003844 unsigned int n;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003845 int i;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003846 MMacro *mac;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003847
H. Peter Anvine2c80182005-01-15 22:15:51 +00003848 t = tline;
3849 tline = tline->next;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003850
H. Peter Anvine2c80182005-01-15 22:15:51 +00003851 mac = istk->mstk;
3852 while (mac && !mac->name) /* avoid mistaking %reps for macros */
3853 mac = mac->next_active;
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003854 if (!mac) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00003855 error(ERR_NONFATAL, "`%s': not in a macro call", t->text);
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003856 } else {
3857 pos = strchr(t->text, ':');
3858 if (!pos) {
3859 switch (t->text[1]) {
3860 /*
3861 * We have to make a substitution of one of the
3862 * forms %1, %-1, %+1, %%foo, %0.
3863 */
3864 case '0':
3865 type = TOK_NUMBER;
3866 snprintf(tmpbuf, sizeof(tmpbuf), "%d", mac->nparam);
3867 text = nasm_strdup(tmpbuf);
3868 break;
3869 case '%':
H. Peter Anvine2c80182005-01-15 22:15:51 +00003870 type = TOK_ID;
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003871 snprintf(tmpbuf, sizeof(tmpbuf), "..@%"PRIu64".",
3872 mac->unique);
3873 text = nasm_strcat(tmpbuf, t->text + 2);
3874 break;
3875 case '-':
3876 n = atoi(t->text + 2) - 1;
3877 if (n >= mac->nparam)
3878 tt = NULL;
3879 else {
3880 if (mac->nparam > 1)
3881 n = (n + mac->rotate) % mac->nparam;
3882 tt = mac->params[n];
H. Peter Anvine2c80182005-01-15 22:15:51 +00003883 }
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003884 cc = find_cc(tt);
3885 if (cc == -1) {
3886 error(ERR_NONFATAL,
3887 "macro parameter %d is not a condition code",
3888 n + 1);
3889 text = NULL;
3890 } else {
3891 type = TOK_ID;
3892 if (inverse_ccs[cc] == -1) {
3893 error(ERR_NONFATAL,
3894 "condition code `%s' is not invertible",
3895 conditions[cc]);
3896 text = NULL;
3897 } else
3898 text = nasm_strdup(conditions[inverse_ccs[cc]]);
3899 }
3900 break;
3901 case '+':
3902 n = atoi(t->text + 2) - 1;
3903 if (n >= mac->nparam)
3904 tt = NULL;
3905 else {
3906 if (mac->nparam > 1)
3907 n = (n + mac->rotate) % mac->nparam;
3908 tt = mac->params[n];
3909 }
3910 cc = find_cc(tt);
3911 if (cc == -1) {
3912 error(ERR_NONFATAL,
3913 "macro parameter %d is not a condition code",
3914 n + 1);
3915 text = NULL;
3916 } else {
3917 type = TOK_ID;
3918 text = nasm_strdup(conditions[cc]);
3919 }
3920 break;
3921 default:
3922 n = atoi(t->text + 1) - 1;
3923 if (n >= mac->nparam)
3924 tt = NULL;
3925 else {
3926 if (mac->nparam > 1)
3927 n = (n + mac->rotate) % mac->nparam;
3928 tt = mac->params[n];
3929 }
3930 if (tt) {
3931 for (i = 0; i < mac->paramlen[n]; i++) {
3932 *tail = new_Token(NULL, tt->type, tt->text, 0);
3933 tail = &(*tail)->next;
3934 tt = tt->next;
3935 }
3936 }
3937 text = NULL; /* we've done it here */
3938 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003939 }
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003940 } else {
3941 /*
3942 * seems we have a parameters range here
3943 */
3944 Token *head, **last;
3945 head = expand_mmac_params_range(mac, t, &last);
3946 if (head != t) {
3947 *tail = head;
3948 *last = tline;
3949 tline = head;
3950 text = NULL;
3951 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00003952 }
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003953 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00003954 if (!text) {
3955 delete_Token(t);
3956 } else {
3957 *tail = t;
3958 tail = &t->next;
3959 t->type = type;
3960 nasm_free(t->text);
3961 t->text = text;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003962 t->a.mac = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003963 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003964 changed = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003965 continue;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003966 } else if (tline->type == TOK_INDIRECT) {
3967 t = tline;
3968 tline = tline->next;
3969 tt = tokenize(t->text);
3970 tt = expand_mmac_params(tt);
3971 tt = expand_smacro(tt);
3972 *tail = tt;
3973 while (tt) {
3974 tt->a.mac = NULL; /* Necessary? */
3975 tail = &tt->next;
3976 tt = tt->next;
3977 }
3978 delete_Token(t);
3979 changed = true;
Cyrill Gorcunov8bc80172010-09-16 00:16:19 +04003980 } else if (tline->type == TOK_PREPROC_ID &&
3981 tline->text[0] == '%' &&
3982 tline->text[1] == '$' &&
3983 !tok_type_(tline->next, TOK_WHITESPACE) &&
3984 (tok_type_(tline->next, TOK_ID) ||
3985 tok_type_(tline->next, TOK_PREPROC_ID) ||
3986 tok_type_(tline->next, TOK_NUMBER) ||
3987 tok_type_(tline->next, TOK_OTHER) ||
3988 tok_type_(tline->next, TOK_FLOAT))) {
3989 /*
3990 * In a sake of backward compatibility we allow
3991 * to expand local single macro that early before
3992 * pasting token code have place
3993 *
3994 * NOTE: that new code MUST use %+ macro to obtain
3995 * same result
3996 */
3997 t = tline;
3998 tline = tline->next;
3999 tt = tokenize(t->text);
4000 tt = expand_smacro(tt);
4001 *tail = tt;
4002 while (tt) {
4003 tt->a.mac = NULL;
4004 tail = &tt->next;
4005 tt = tt->next;
4006 }
4007 delete_Token(t);
4008 changed = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004009 } else {
4010 t = *tail = tline;
4011 tline = tline->next;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004012 t->a.mac = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004013 tail = &t->next;
4014 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00004015 }
H. Peter Anvineba20a72002-04-30 20:53:55 +00004016 *tail = NULL;
H. Peter Anvin67c63722008-10-26 23:49:00 -07004017
H. Peter Anvind784a082009-04-20 14:01:18 -07004018 if (changed)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004019 paste_tokens(&thead, false);
H. Peter Anvin6125b622009-04-08 14:02:25 -07004020
H. Peter Anvin76690a12002-04-30 20:52:49 +00004021 return thead;
4022}
4023
4024/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004025 * Expand all single-line macro calls made in the given line.
4026 * Return the expanded version of the line. The original is deemed
4027 * to be destroyed in the process. (In reality we'll just move
4028 * Tokens from input to output a lot of the time, rather than
4029 * actually bothering to destroy and replicate.)
4030 */
H. Peter Anvincb1cf592007-11-19 12:26:50 -08004031
H. Peter Anvine2c80182005-01-15 22:15:51 +00004032static Token *expand_smacro(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004033{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004034 Token *t, *tt, *mstart, **tail, *thead;
H. Peter Anvineba20a72002-04-30 20:53:55 +00004035 SMacro *head = NULL, *m;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004036 Token **params;
4037 int *paramsize;
H. Peter Anvin25a99342007-09-22 17:45:45 -07004038 unsigned int nparam, sparam;
H. Peter Anvind784a082009-04-20 14:01:18 -07004039 int brackets;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004040 Token *org_tline = tline;
4041 Context *ctx;
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08004042 const char *mname;
H. Peter Anvin2a15e692007-11-19 13:14:59 -08004043 int deadman = DEADMAN_LIMIT;
H. Peter Anvin8287daf2009-07-07 16:00:58 -07004044 bool expanded;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004045
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004046 /*
4047 * Trick: we should avoid changing the start token pointer since it can
4048 * be contained in "next" field of other token. Because of this
4049 * we allocate a copy of first token and work with it; at the end of
4050 * routine we copy it back
4051 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004052 if (org_tline) {
Cyrill Gorcunoved4a8052010-04-09 15:40:35 +04004053 tline = new_Token(org_tline->next, org_tline->type,
4054 org_tline->text, 0);
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004055 tline->a.mac = org_tline->a.mac;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004056 nasm_free(org_tline->text);
4057 org_tline->text = NULL;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004058 }
4059
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004060 expanded = true; /* Always expand %+ at least once */
H. Peter Anvin8287daf2009-07-07 16:00:58 -07004061
H. Peter Anvincb1cf592007-11-19 12:26:50 -08004062again:
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004063 thead = NULL;
Cyrill Gorcunoved4a8052010-04-09 15:40:35 +04004064 tail = &thead;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004065
H. Peter Anvine2c80182005-01-15 22:15:51 +00004066 while (tline) { /* main token loop */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004067 if (!--deadman) {
4068 error(ERR_NONFATAL, "interminable macro recursion");
Cyrill Gorcunovbd38c8f2009-11-21 11:11:23 +03004069 goto err;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004070 }
H. Peter Anvincb1cf592007-11-19 12:26:50 -08004071
H. Peter Anvine2c80182005-01-15 22:15:51 +00004072 if ((mname = tline->text)) {
4073 /* if this token is a local macro, look in local context */
Cyrill Gorcunovc56d9ad2010-02-11 15:12:19 +03004074 if (tline->type == TOK_ID) {
4075 head = (SMacro *)hash_findix(&smacros, mname);
4076 } else if (tline->type == TOK_PREPROC_ID) {
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08004077 ctx = get_ctx(mname, &mname, true);
Cyrill Gorcunovc56d9ad2010-02-11 15:12:19 +03004078 head = ctx ? (SMacro *)hash_findix(&ctx->localmac, mname) : NULL;
4079 } else
4080 head = NULL;
H. Peter Anvin072771e2008-05-22 13:17:51 -07004081
H. Peter Anvine2c80182005-01-15 22:15:51 +00004082 /*
4083 * We've hit an identifier. As in is_mmacro below, we first
4084 * check whether the identifier is a single-line macro at
4085 * all, then think about checking for parameters if
4086 * necessary.
4087 */
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04004088 list_for_each(m, head)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004089 if (!mstrcmp(m->name, mname, m->casesense))
4090 break;
4091 if (m) {
4092 mstart = tline;
4093 params = NULL;
4094 paramsize = NULL;
4095 if (m->nparam == 0) {
4096 /*
4097 * Simple case: the macro is parameterless. Discard the
4098 * one token that the macro call took, and push the
4099 * expansion back on the to-do stack.
4100 */
4101 if (!m->expansion) {
4102 if (!strcmp("__FILE__", m->name)) {
4103 int32_t num = 0;
4104 char *file = NULL;
4105 src_get(&num, &file);
4106 tline->text = nasm_quote(file, strlen(file));
4107 tline->type = TOK_STRING;
4108 nasm_free(file);
H. Peter Anvine2c80182005-01-15 22:15:51 +00004109 continue;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004110 }
4111 if (!strcmp("__LINE__", m->name)) {
4112 nasm_free(tline->text);
4113 make_tok_num(tline, src_get_linnum());
4114 continue;
4115 }
4116 if (!strcmp("__BITS__", m->name)) {
4117 nasm_free(tline->text);
4118 make_tok_num(tline, globalbits);
4119 continue;
4120 }
4121 tline = delete_Token(tline);
4122 continue;
4123 }
4124 } else {
4125 /*
4126 * Complicated case: at least one macro with this name
H. Peter Anvine2c80182005-01-15 22:15:51 +00004127 * exists and takes parameters. We must find the
4128 * parameters in the call, count them, find the SMacro
4129 * that corresponds to that form of the macro call, and
4130 * substitute for the parameters when we expand. What a
4131 * pain.
4132 */
4133 /*tline = tline->next;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004134 skip_white_(tline); */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004135 do {
4136 t = tline->next;
4137 while (tok_type_(t, TOK_SMAC_END)) {
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004138 t->a.mac->in_progress = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004139 t->text = NULL;
4140 t = tline->next = delete_Token(t);
4141 }
4142 tline = t;
4143 } while (tok_type_(tline, TOK_WHITESPACE));
4144 if (!tok_is_(tline, "(")) {
4145 /*
4146 * This macro wasn't called with parameters: ignore
4147 * the call. (Behaviour borrowed from gnu cpp.)
4148 */
4149 tline = mstart;
4150 m = NULL;
4151 } else {
4152 int paren = 0;
4153 int white = 0;
4154 brackets = 0;
4155 nparam = 0;
4156 sparam = PARAM_DELTA;
4157 params = nasm_malloc(sparam * sizeof(Token *));
4158 params[0] = tline->next;
4159 paramsize = nasm_malloc(sparam * sizeof(int));
4160 paramsize[0] = 0;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07004161 while (true) { /* parameter loop */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004162 /*
4163 * For some unusual expansions
4164 * which concatenates function call
4165 */
4166 t = tline->next;
4167 while (tok_type_(t, TOK_SMAC_END)) {
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004168 t->a.mac->in_progress = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004169 t->text = NULL;
4170 t = tline->next = delete_Token(t);
4171 }
4172 tline = t;
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00004173
H. Peter Anvine2c80182005-01-15 22:15:51 +00004174 if (!tline) {
4175 error(ERR_NONFATAL,
4176 "macro call expects terminating `)'");
4177 break;
4178 }
4179 if (tline->type == TOK_WHITESPACE
4180 && brackets <= 0) {
4181 if (paramsize[nparam])
4182 white++;
4183 else
4184 params[nparam] = tline->next;
4185 continue; /* parameter loop */
4186 }
4187 if (tline->type == TOK_OTHER
4188 && tline->text[1] == 0) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004189 char ch = tline->text[0];
H. Peter Anvine2c80182005-01-15 22:15:51 +00004190 if (ch == ',' && !paren && brackets <= 0) {
4191 if (++nparam >= sparam) {
4192 sparam += PARAM_DELTA;
4193 params = nasm_realloc(params,
Cyrill Gorcunoved4a8052010-04-09 15:40:35 +04004194 sparam * sizeof(Token *));
4195 paramsize = nasm_realloc(paramsize,
4196 sparam * sizeof(int));
H. Peter Anvine2c80182005-01-15 22:15:51 +00004197 }
4198 params[nparam] = tline->next;
4199 paramsize[nparam] = 0;
4200 white = 0;
4201 continue; /* parameter loop */
4202 }
4203 if (ch == '{' &&
4204 (brackets > 0 || (brackets == 0 &&
4205 !paramsize[nparam])))
4206 {
4207 if (!(brackets++)) {
4208 params[nparam] = tline->next;
4209 continue; /* parameter loop */
4210 }
4211 }
4212 if (ch == '}' && brackets > 0)
4213 if (--brackets == 0) {
4214 brackets = -1;
4215 continue; /* parameter loop */
4216 }
4217 if (ch == '(' && !brackets)
4218 paren++;
4219 if (ch == ')' && brackets <= 0)
4220 if (--paren < 0)
4221 break;
4222 }
4223 if (brackets < 0) {
4224 brackets = 0;
4225 error(ERR_NONFATAL, "braces do not "
4226 "enclose all of macro parameter");
4227 }
4228 paramsize[nparam] += white + 1;
4229 white = 0;
4230 } /* parameter loop */
4231 nparam++;
4232 while (m && (m->nparam != nparam ||
4233 mstrcmp(m->name, mname,
4234 m->casesense)))
4235 m = m->next;
4236 if (!m)
H. Peter Anvin917a3492008-09-24 09:14:49 -07004237 error(ERR_WARNING|ERR_PASS1|ERR_WARN_MNP,
H. Peter Anvine2c80182005-01-15 22:15:51 +00004238 "macro `%s' exists, "
4239 "but not taking %d parameters",
4240 mstart->text, nparam);
4241 }
4242 }
4243 if (m && m->in_progress)
4244 m = NULL;
4245 if (!m) { /* in progess or didn't find '(' or wrong nparam */
H. Peter Anvin70653092007-10-19 14:42:29 -07004246 /*
H. Peter Anvine2c80182005-01-15 22:15:51 +00004247 * Design question: should we handle !tline, which
4248 * indicates missing ')' here, or expand those
4249 * macros anyway, which requires the (t) test a few
H. Peter Anvin70653092007-10-19 14:42:29 -07004250 * lines down?
H. Peter Anvine2c80182005-01-15 22:15:51 +00004251 */
4252 nasm_free(params);
4253 nasm_free(paramsize);
4254 tline = mstart;
4255 } else {
4256 /*
4257 * Expand the macro: we are placed on the last token of the
4258 * call, so that we can easily split the call from the
4259 * following tokens. We also start by pushing an SMAC_END
4260 * token for the cycle removal.
4261 */
4262 t = tline;
4263 if (t) {
4264 tline = t->next;
4265 t->next = NULL;
4266 }
4267 tt = new_Token(tline, TOK_SMAC_END, NULL, 0);
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004268 tt->a.mac = m;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07004269 m->in_progress = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004270 tline = tt;
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04004271 list_for_each(t, m->expansion) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00004272 if (t->type >= TOK_SMAC_PARAM) {
4273 Token *pcopy = tline, **ptail = &pcopy;
4274 Token *ttt, *pt;
4275 int i;
H. Peter Anvin734b1882002-04-30 21:01:08 +00004276
H. Peter Anvine2c80182005-01-15 22:15:51 +00004277 ttt = params[t->type - TOK_SMAC_PARAM];
Cyrill Gorcunoved4a8052010-04-09 15:40:35 +04004278 i = paramsize[t->type - TOK_SMAC_PARAM];
4279 while (--i >= 0) {
4280 pt = *ptail = new_Token(tline, ttt->type,
4281 ttt->text, 0);
H. Peter Anvine2c80182005-01-15 22:15:51 +00004282 ptail = &pt->next;
4283 ttt = ttt->next;
4284 }
4285 tline = pcopy;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004286 } else if (t->type == TOK_PREPROC_Q) {
4287 tt = new_Token(tline, TOK_ID, mname, 0);
4288 tline = tt;
4289 } else if (t->type == TOK_PREPROC_QQ) {
4290 tt = new_Token(tline, TOK_ID, m->name, 0);
4291 tline = tt;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004292 } else {
4293 tt = new_Token(tline, t->type, t->text, 0);
4294 tline = tt;
4295 }
4296 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00004297
H. Peter Anvine2c80182005-01-15 22:15:51 +00004298 /*
4299 * Having done that, get rid of the macro call, and clean
4300 * up the parameters.
4301 */
4302 nasm_free(params);
4303 nasm_free(paramsize);
4304 free_tlist(mstart);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004305 expanded = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004306 continue; /* main token loop */
4307 }
4308 }
4309 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004310
H. Peter Anvine2c80182005-01-15 22:15:51 +00004311 if (tline->type == TOK_SMAC_END) {
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004312 tline->a.mac->in_progress = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004313 tline = delete_Token(tline);
4314 } else {
4315 t = *tail = tline;
4316 tline = tline->next;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004317 t->a.mac = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004318 t->next = NULL;
4319 tail = &t->next;
4320 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004321 }
4322
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004323 /*
4324 * Now scan the entire line and look for successive TOK_IDs that resulted
Keith Kaniosb7a89542007-04-12 02:40:54 +00004325 * after expansion (they can't be produced by tokenize()). The successive
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004326 * TOK_IDs should be concatenated.
4327 * Also we look for %+ tokens and concatenate the tokens before and after
4328 * them (without white spaces in between).
4329 */
H. Peter Anvin8287daf2009-07-07 16:00:58 -07004330 if (expanded && paste_tokens(&thead, true)) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004331 /*
4332 * If we concatenated something, *and* we had previously expanded
4333 * an actual macro, scan the lines again for macros...
4334 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004335 tline = thead;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004336 expanded = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004337 goto again;
H. Peter Anvin734b1882002-04-30 21:01:08 +00004338 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004339
Cyrill Gorcunovbd38c8f2009-11-21 11:11:23 +03004340err:
H. Peter Anvine2c80182005-01-15 22:15:51 +00004341 if (org_tline) {
4342 if (thead) {
4343 *org_tline = *thead;
4344 /* since we just gave text to org_line, don't free it */
4345 thead->text = NULL;
4346 delete_Token(thead);
4347 } else {
4348 /* the expression expanded to empty line;
4349 we can't return NULL for some reasons
4350 we just set the line to a single WHITESPACE token. */
4351 memset(org_tline, 0, sizeof(*org_tline));
4352 org_tline->text = NULL;
4353 org_tline->type = TOK_WHITESPACE;
4354 }
4355 thead = org_tline;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004356 }
4357
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004358 return thead;
4359}
4360
4361/*
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004362 * Similar to expand_smacro but used exclusively with macro identifiers
4363 * right before they are fetched in. The reason is that there can be
4364 * identifiers consisting of several subparts. We consider that if there
4365 * are more than one element forming the name, user wants a expansion,
4366 * otherwise it will be left as-is. Example:
4367 *
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004368 * %define %$abc cde
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004369 *
4370 * the identifier %$abc will be left as-is so that the handler for %define
4371 * will suck it and define the corresponding value. Other case:
4372 *
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004373 * %define _%$abc cde
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004374 *
4375 * In this case user wants name to be expanded *before* %define starts
4376 * working, so we'll expand %$abc into something (if it has a value;
4377 * otherwise it will be left as-is) then concatenate all successive
4378 * PP_IDs into one.
4379 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004380static Token *expand_id(Token * tline)
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004381{
4382 Token *cur, *oldnext = NULL;
4383
H. Peter Anvin734b1882002-04-30 21:01:08 +00004384 if (!tline || !tline->next)
H. Peter Anvine2c80182005-01-15 22:15:51 +00004385 return tline;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004386
4387 cur = tline;
4388 while (cur->next &&
H. Peter Anvine2c80182005-01-15 22:15:51 +00004389 (cur->next->type == TOK_ID ||
4390 cur->next->type == TOK_PREPROC_ID
4391 || cur->next->type == TOK_NUMBER))
4392 cur = cur->next;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004393
4394 /* If identifier consists of just one token, don't expand */
4395 if (cur == tline)
H. Peter Anvine2c80182005-01-15 22:15:51 +00004396 return tline;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004397
H. Peter Anvine2c80182005-01-15 22:15:51 +00004398 if (cur) {
4399 oldnext = cur->next; /* Detach the tail past identifier */
4400 cur->next = NULL; /* so that expand_smacro stops here */
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004401 }
4402
H. Peter Anvin734b1882002-04-30 21:01:08 +00004403 tline = expand_smacro(tline);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004404
H. Peter Anvine2c80182005-01-15 22:15:51 +00004405 if (cur) {
4406 /* expand_smacro possibly changhed tline; re-scan for EOL */
4407 cur = tline;
4408 while (cur && cur->next)
4409 cur = cur->next;
4410 if (cur)
4411 cur->next = oldnext;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004412 }
4413
4414 return tline;
4415}
4416
4417/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004418 * Determine whether the given line constitutes a multi-line macro
4419 * call, and return the MMacro structure called if so. Doesn't have
4420 * to check for an initial label - that's taken care of in
4421 * expand_mmacro - but must check numbers of parameters. Guaranteed
4422 * to be called with tline->type == TOK_ID, so the putative macro
4423 * name is easy to find.
4424 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004425static MMacro *is_mmacro(Token * tline, Token *** params_array)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004426{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004427 MMacro *head, *m;
4428 Token **params;
4429 int nparam;
4430
H. Peter Anvin166c2472008-05-28 12:28:58 -07004431 head = (MMacro *) hash_findix(&mmacros, tline->text);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004432
4433 /*
4434 * Efficiency: first we see if any macro exists with the given
4435 * name. If not, we can return NULL immediately. _Then_ we
4436 * count the parameters, and then we look further along the
4437 * list if necessary to find the proper MMacro.
4438 */
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04004439 list_for_each(m, head)
H. Peter Anvine2c80182005-01-15 22:15:51 +00004440 if (!mstrcmp(m->name, tline->text, m->casesense))
4441 break;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004442 if (!m)
H. Peter Anvine2c80182005-01-15 22:15:51 +00004443 return NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004444
4445 /*
4446 * OK, we have a potential macro. Count and demarcate the
4447 * parameters.
4448 */
H. Peter Anvin734b1882002-04-30 21:01:08 +00004449 count_mmac_params(tline->next, &nparam, &params);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004450
4451 /*
4452 * So we know how many parameters we've got. Find the MMacro
4453 * structure that handles this number.
4454 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004455 while (m) {
4456 if (m->nparam_min <= nparam
4457 && (m->plus || nparam <= m->nparam_max)) {
4458 /*
4459 * This one is right. Just check if cycle removal
4460 * prohibits us using it before we actually celebrate...
4461 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004462 if (m->in_progress > m->max_depth) {
4463 if (m->max_depth > 0) {
4464 error(ERR_WARNING,
4465 "reached maximum recursion depth of %i",
4466 m->max_depth);
4467 }
4468 nasm_free(params);
4469 return NULL;
4470 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00004471 /*
4472 * It's right, and we can use it. Add its default
4473 * parameters to the end of our list if necessary.
4474 */
4475 if (m->defaults && nparam < m->nparam_min + m->ndefs) {
4476 params =
4477 nasm_realloc(params,
4478 ((m->nparam_min + m->ndefs +
4479 1) * sizeof(*params)));
4480 while (nparam < m->nparam_min + m->ndefs) {
4481 params[nparam] = m->defaults[nparam - m->nparam_min];
4482 nparam++;
4483 }
4484 }
4485 /*
4486 * If we've gone over the maximum parameter count (and
4487 * we're in Plus mode), ignore parameters beyond
4488 * nparam_max.
4489 */
4490 if (m->plus && nparam > m->nparam_max)
4491 nparam = m->nparam_max;
4492 /*
4493 * Then terminate the parameter list, and leave.
4494 */
4495 if (!params) { /* need this special case */
4496 params = nasm_malloc(sizeof(*params));
4497 nparam = 0;
4498 }
4499 params[nparam] = NULL;
4500 *params_array = params;
4501 return m;
4502 }
4503 /*
4504 * This one wasn't right: look for the next one with the
4505 * same name.
4506 */
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04004507 list_for_each(m, m->next)
H. Peter Anvine2c80182005-01-15 22:15:51 +00004508 if (!mstrcmp(m->name, tline->text, m->casesense))
4509 break;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004510 }
4511
4512 /*
4513 * After all that, we didn't find one with the right number of
4514 * parameters. Issue a warning, and fail to expand the macro.
4515 */
H. Peter Anvin917a3492008-09-24 09:14:49 -07004516 error(ERR_WARNING|ERR_PASS1|ERR_WARN_MNP,
H. Peter Anvine2c80182005-01-15 22:15:51 +00004517 "macro `%s' exists, but not taking %d parameters",
4518 tline->text, nparam);
H. Peter Anvin734b1882002-04-30 21:01:08 +00004519 nasm_free(params);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004520 return NULL;
4521}
4522
Keith Kanios891775e2009-07-11 06:08:54 -05004523
4524/*
4525 * Save MMacro invocation specific fields in
4526 * preparation for a recursive macro expansion
4527 */
4528static void push_mmacro(MMacro *m)
4529{
4530 MMacroInvocation *i;
4531
H. Peter Anvin89cee572009-07-15 09:16:54 -04004532 i = nasm_malloc(sizeof(MMacroInvocation));
4533 i->prev = m->prev;
4534 i->params = m->params;
4535 i->iline = m->iline;
4536 i->nparam = m->nparam;
4537 i->rotate = m->rotate;
4538 i->paramlen = m->paramlen;
4539 i->unique = m->unique;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004540 i->condcnt = m->condcnt;
H. Peter Anvin89cee572009-07-15 09:16:54 -04004541 m->prev = i;
Keith Kanios891775e2009-07-11 06:08:54 -05004542}
4543
4544
4545/*
4546 * Restore MMacro invocation specific fields that were
4547 * saved during a previous recursive macro expansion
4548 */
4549static void pop_mmacro(MMacro *m)
4550{
4551 MMacroInvocation *i;
4552
H. Peter Anvin89cee572009-07-15 09:16:54 -04004553 if (m->prev) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004554 i = m->prev;
4555 m->prev = i->prev;
4556 m->params = i->params;
4557 m->iline = i->iline;
4558 m->nparam = i->nparam;
4559 m->rotate = i->rotate;
4560 m->paramlen = i->paramlen;
4561 m->unique = i->unique;
4562 m->condcnt = i->condcnt;
4563 nasm_free(i);
H. Peter Anvin89cee572009-07-15 09:16:54 -04004564 }
Keith Kanios891775e2009-07-11 06:08:54 -05004565}
4566
4567
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004568/*
4569 * Expand the multi-line macro call made by the given line, if
4570 * there is one to be expanded. If there is, push the expansion on
H. Peter Anvineba20a72002-04-30 20:53:55 +00004571 * istk->expansion and return 1. Otherwise return 0.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004572 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004573static int expand_mmacro(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004574{
4575 Token *startline = tline;
4576 Token *label = NULL;
4577 int dont_prepend = 0;
H. Peter Anvince2233b2008-05-25 21:57:00 -07004578 Token **params, *t, *mtok, *tt;
H. Peter Anvineba20a72002-04-30 20:53:55 +00004579 MMacro *m;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004580 Line *l, *ll;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004581 int i, nparam, *paramlen;
H. Peter Anvinc751e862008-06-09 10:18:45 -07004582 const char *mname;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004583
4584 t = tline;
H. Peter Anvineba20a72002-04-30 20:53:55 +00004585 skip_white_(t);
H. Peter Anvince2233b2008-05-25 21:57:00 -07004586 /* if (!tok_type_(t, TOK_ID)) Lino 02/25/02 */
H. Peter Anvindce1e2f2002-04-30 21:06:37 +00004587 if (!tok_type_(t, TOK_ID) && !tok_type_(t, TOK_PREPROC_ID))
H. Peter Anvine2c80182005-01-15 22:15:51 +00004588 return 0;
H. Peter Anvince2233b2008-05-25 21:57:00 -07004589 mtok = t;
H. Peter Anvin734b1882002-04-30 21:01:08 +00004590 m = is_mmacro(t, &params);
H. Peter Anvinc751e862008-06-09 10:18:45 -07004591 if (m) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004592 mname = t->text;
H. Peter Anvinc751e862008-06-09 10:18:45 -07004593 } else {
H. Peter Anvine2c80182005-01-15 22:15:51 +00004594 Token *last;
4595 /*
4596 * We have an id which isn't a macro call. We'll assume
4597 * it might be a label; we'll also check to see if a
4598 * colon follows it. Then, if there's another id after
4599 * that lot, we'll check it again for macro-hood.
4600 */
4601 label = last = t;
4602 t = t->next;
4603 if (tok_type_(t, TOK_WHITESPACE))
4604 last = t, t = t->next;
4605 if (tok_is_(t, ":")) {
4606 dont_prepend = 1;
4607 last = t, t = t->next;
4608 if (tok_type_(t, TOK_WHITESPACE))
4609 last = t, t = t->next;
4610 }
H. Peter Anvin89cee572009-07-15 09:16:54 -04004611 if (!tok_type_(t, TOK_ID) || !(m = is_mmacro(t, &params)))
H. Peter Anvine2c80182005-01-15 22:15:51 +00004612 return 0;
4613 last->next = NULL;
Keith Kanios891775e2009-07-11 06:08:54 -05004614 mname = t->text;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004615 tline = t;
H. Peter Anvineba20a72002-04-30 20:53:55 +00004616 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004617
4618 /*
4619 * Fix up the parameters: this involves stripping leading and
4620 * trailing whitespace, then stripping braces if they are
4621 * present.
4622 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004623 for (nparam = 0; params[nparam]; nparam++) ;
H. Peter Anvin734b1882002-04-30 21:01:08 +00004624 paramlen = nparam ? nasm_malloc(nparam * sizeof(*paramlen)) : NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004625
H. Peter Anvine2c80182005-01-15 22:15:51 +00004626 for (i = 0; params[i]; i++) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07004627 int brace = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004628 int comma = (!m->plus || i < nparam - 1);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004629
H. Peter Anvine2c80182005-01-15 22:15:51 +00004630 t = params[i];
4631 skip_white_(t);
4632 if (tok_is_(t, "{"))
H. Peter Anvin6867acc2007-10-10 14:58:45 -07004633 t = t->next, brace = true, comma = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004634 params[i] = t;
4635 paramlen[i] = 0;
4636 while (t) {
4637 if (comma && t->type == TOK_OTHER && !strcmp(t->text, ","))
4638 break; /* ... because we have hit a comma */
4639 if (comma && t->type == TOK_WHITESPACE
4640 && tok_is_(t->next, ","))
4641 break; /* ... or a space then a comma */
4642 if (brace && t->type == TOK_OTHER && !strcmp(t->text, "}"))
4643 break; /* ... or a brace */
4644 t = t->next;
4645 paramlen[i]++;
4646 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004647 }
4648
4649 /*
4650 * OK, we have a MMacro structure together with a set of
4651 * parameters. We must now go through the expansion and push
H. Peter Anvin76690a12002-04-30 20:52:49 +00004652 * copies of each Line on to istk->expansion. Substitution of
4653 * parameter tokens and macro-local tokens doesn't get done
4654 * until the single-line macro substitution process; this is
4655 * because delaying them allows us to change the semantics
4656 * later through %rotate.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004657 *
H. Peter Anvin76690a12002-04-30 20:52:49 +00004658 * First, push an end marker on to istk->expansion, mark this
4659 * macro as in progress, and set up its invocation-specific
4660 * variables.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004661 */
4662 ll = nasm_malloc(sizeof(Line));
4663 ll->next = istk->expansion;
4664 ll->finishes = m;
4665 ll->first = NULL;
4666 istk->expansion = ll;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004667
H. Peter Anvin89cee572009-07-15 09:16:54 -04004668 /*
4669 * Save the previous MMacro expansion in the case of
4670 * macro recursion
4671 */
4672 if (m->max_depth && m->in_progress)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004673 push_mmacro(m);
H. Peter Anvin76690a12002-04-30 20:52:49 +00004674
Keith Kanios891775e2009-07-11 06:08:54 -05004675 m->in_progress ++;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004676 m->params = params;
4677 m->iline = tline;
4678 m->nparam = nparam;
4679 m->rotate = 0;
4680 m->paramlen = paramlen;
4681 m->unique = unique++;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004682 m->lineno = 0;
Keith Kanios3c0d91f2009-10-25 13:28:03 -05004683 m->condcnt = 0;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004684
4685 m->next_active = istk->mstk;
4686 istk->mstk = m;
4687
Cyrill Gorcunov367d59e2010-04-09 15:43:03 +04004688 list_for_each(l, m->expansion) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00004689 Token **tail;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004690
H. Peter Anvine2c80182005-01-15 22:15:51 +00004691 ll = nasm_malloc(sizeof(Line));
4692 ll->finishes = NULL;
4693 ll->next = istk->expansion;
4694 istk->expansion = ll;
4695 tail = &ll->first;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004696
Cyrill Gorcunov367d59e2010-04-09 15:43:03 +04004697 list_for_each(t, l->first) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00004698 Token *x = t;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004699 switch (t->type) {
4700 case TOK_PREPROC_Q:
4701 tt = *tail = new_Token(NULL, TOK_ID, mname, 0);
4702 break;
4703 case TOK_PREPROC_QQ:
4704 tt = *tail = new_Token(NULL, TOK_ID, m->name, 0);
4705 break;
4706 case TOK_PREPROC_ID:
4707 if (t->text[1] == '0' && t->text[2] == '0') {
4708 dont_prepend = -1;
4709 x = label;
4710 if (!x)
4711 continue;
4712 }
4713 /* fall through */
4714 default:
4715 tt = *tail = new_Token(NULL, x->type, x->text, 0);
4716 break;
4717 }
4718 tail = &tt->next;
4719 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00004720 *tail = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004721 }
4722
4723 /*
H. Peter Anvineba20a72002-04-30 20:53:55 +00004724 * If we had a label, push it on as the first line of
4725 * the macro expansion.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004726 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004727 if (label) {
4728 if (dont_prepend < 0)
4729 free_tlist(startline);
4730 else {
4731 ll = nasm_malloc(sizeof(Line));
4732 ll->finishes = NULL;
4733 ll->next = istk->expansion;
4734 istk->expansion = ll;
4735 ll->first = startline;
4736 if (!dont_prepend) {
4737 while (label->next)
4738 label = label->next;
4739 label->next = tt = new_Token(NULL, TOK_OTHER, ":", 0);
4740 }
4741 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004742 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004743
H. Peter Anvin734b1882002-04-30 21:01:08 +00004744 list->uplevel(m->nolist ? LIST_MACRO_NOLIST : LIST_MACRO);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004745
H. Peter Anvineba20a72002-04-30 20:53:55 +00004746 return 1;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004747}
4748
Victor van den Elzen3b404c02008-09-18 13:51:36 +02004749/* The function that actually does the error reporting */
4750static void verror(int severity, const char *fmt, va_list arg)
4751{
4752 char buff[1024];
Cyrill Gorcunov71787fd2010-09-24 15:24:42 +04004753 MMacro *mmac = NULL;
4754 int delta = 0;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02004755
4756 vsnprintf(buff, sizeof(buff), fmt, arg);
4757
Cyrill Gorcunov71787fd2010-09-24 15:24:42 +04004758 /* get %macro name */
4759 if (istk && istk->mstk) {
4760 mmac = istk->mstk;
4761 /* but %rep blocks should be skipped */
4762 while (mmac && !mmac->name)
4763 mmac = mmac->next_active, delta++;
4764 }
4765
4766 if (mmac)
4767 nasm_error(severity, "(%s:%d) %s",
4768 mmac->name, mmac->lineno - delta, buff);
Victor van den Elzen3b404c02008-09-18 13:51:36 +02004769 else
H. Peter Anvindbb640b2009-07-18 18:57:16 -07004770 nasm_error(severity, "%s", buff);
Victor van den Elzen3b404c02008-09-18 13:51:36 +02004771}
4772
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004773/*
4774 * Since preprocessor always operate only on the line that didn't
H. Peter Anvin917a3492008-09-24 09:14:49 -07004775 * arrived yet, we should always use ERR_OFFBY1.
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004776 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004777static void error(int severity, const char *fmt, ...)
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004778{
4779 va_list arg;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004780
4781 /* If we're in a dead branch of IF or something like it, ignore the error */
H. Peter Anvin77ba0c62002-05-14 03:18:53 +00004782 if (istk && istk->conds && !emitting(istk->conds->state))
H. Peter Anvine2c80182005-01-15 22:15:51 +00004783 return;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004784
H. Peter Anvin734b1882002-04-30 21:01:08 +00004785 va_start(arg, fmt);
Victor van den Elzen3b404c02008-09-18 13:51:36 +02004786 verror(severity, fmt, arg);
H. Peter Anvin734b1882002-04-30 21:01:08 +00004787 va_end(arg);
Victor van den Elzen3b404c02008-09-18 13:51:36 +02004788}
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004789
Victor van den Elzen3b404c02008-09-18 13:51:36 +02004790/*
4791 * Because %else etc are evaluated in the state context
4792 * of the previous branch, errors might get lost with error():
4793 * %if 0 ... %else trailing garbage ... %endif
4794 * So %else etc should report errors with this function.
4795 */
4796static void error_precond(int severity, const char *fmt, ...)
4797{
4798 va_list arg;
4799
4800 /* Only ignore the error if it's really in a dead branch */
4801 if (istk && istk->conds && istk->conds->state == COND_NEVER)
4802 return;
4803
4804 va_start(arg, fmt);
4805 verror(severity, fmt, arg);
4806 va_end(arg);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004807}
4808
H. Peter Anvin734b1882002-04-30 21:01:08 +00004809static void
H. Peter Anvindbb640b2009-07-18 18:57:16 -07004810pp_reset(char *file, int apass, ListGen * listgen, StrList **deplist)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004811{
H. Peter Anvin7383b402008-09-24 10:20:40 -07004812 Token *t;
4813
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004814 cstk = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004815 istk = nasm_malloc(sizeof(Include));
4816 istk->next = NULL;
4817 istk->conds = NULL;
4818 istk->expansion = NULL;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004819 istk->mstk = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004820 istk->fp = fopen(file, "r");
H. Peter Anvineba20a72002-04-30 20:53:55 +00004821 istk->fname = NULL;
4822 src_set_fname(nasm_strdup(file));
4823 src_set_linnum(0);
4824 istk->lineinc = 1;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004825 if (!istk->fp)
H. Peter Anvin917a3492008-09-24 09:14:49 -07004826 error(ERR_FATAL|ERR_NOFILE, "unable to open input file `%s'",
H. Peter Anvine2c80182005-01-15 22:15:51 +00004827 file);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004828 defining = NULL;
Charles Crayned4200be2008-07-12 16:42:33 -07004829 nested_mac_count = 0;
4830 nested_rep_count = 0;
H. Peter Anvin97a23472007-09-16 17:57:25 -07004831 init_macros();
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004832 unique = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004833 if (tasm_compatible_mode) {
H. Peter Anvina4835d42008-05-20 14:21:29 -07004834 stdmacpos = nasm_stdmac;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004835 } else {
H. Peter Anvina4835d42008-05-20 14:21:29 -07004836 stdmacpos = nasm_stdmac_after_tasm;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004837 }
H. Peter Anvind2456592008-06-19 15:04:18 -07004838 any_extrastdmac = extrastdmac && *extrastdmac;
4839 do_predef = true;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004840 list = listgen;
H. Peter Anvin61f130f2008-09-25 15:45:06 -07004841
4842 /*
4843 * 0 for dependencies, 1 for preparatory passes, 2 for final pass.
4844 * The caller, however, will also pass in 3 for preprocess-only so
4845 * we can set __PASS__ accordingly.
4846 */
4847 pass = apass > 2 ? 2 : apass;
4848
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07004849 dephead = deptail = deplist;
4850 if (deplist) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004851 StrList *sl = nasm_malloc(strlen(file)+1+sizeof sl->next);
4852 sl->next = NULL;
4853 strcpy(sl->str, file);
4854 *deptail = sl;
4855 deptail = &sl->next;
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07004856 }
H. Peter Anvin7383b402008-09-24 10:20:40 -07004857
H. Peter Anvin61f130f2008-09-25 15:45:06 -07004858 /*
4859 * Define the __PASS__ macro. This is defined here unlike
4860 * all the other builtins, because it is special -- it varies between
4861 * passes.
4862 */
H. Peter Anvin7383b402008-09-24 10:20:40 -07004863 t = nasm_malloc(sizeof(*t));
4864 t->next = NULL;
H. Peter Anvin61f130f2008-09-25 15:45:06 -07004865 make_tok_num(t, apass);
H. Peter Anvin7383b402008-09-24 10:20:40 -07004866 t->a.mac = NULL;
4867 define_smacro(NULL, "__PASS__", true, 0, t);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004868}
4869
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004870static char *pp_getline(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004871{
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004872 char *line;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004873 Token *tline;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004874
H. Peter Anvine2c80182005-01-15 22:15:51 +00004875 while (1) {
4876 /*
Keith Kaniosb7a89542007-04-12 02:40:54 +00004877 * Fetch a tokenized line, either from the macro-expansion
H. Peter Anvine2c80182005-01-15 22:15:51 +00004878 * buffer or from the input file.
4879 */
4880 tline = NULL;
4881 while (istk->expansion && istk->expansion->finishes) {
4882 Line *l = istk->expansion;
4883 if (!l->finishes->name && l->finishes->in_progress > 1) {
4884 Line *ll;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004885
H. Peter Anvine2c80182005-01-15 22:15:51 +00004886 /*
4887 * This is a macro-end marker for a macro with no
4888 * name, which means it's not really a macro at all
4889 * but a %rep block, and the `in_progress' field is
4890 * more than 1, meaning that we still need to
4891 * repeat. (1 means the natural last repetition; 0
4892 * means termination by %exitrep.) We have
4893 * therefore expanded up to the %endrep, and must
4894 * push the whole block on to the expansion buffer
4895 * again. We don't bother to remove the macro-end
4896 * marker: we'd only have to generate another one
4897 * if we did.
4898 */
4899 l->finishes->in_progress--;
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04004900 list_for_each(l, l->finishes->expansion) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00004901 Token *t, *tt, **tail;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004902
H. Peter Anvine2c80182005-01-15 22:15:51 +00004903 ll = nasm_malloc(sizeof(Line));
4904 ll->next = istk->expansion;
4905 ll->finishes = NULL;
4906 ll->first = NULL;
4907 tail = &ll->first;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004908
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04004909 list_for_each(t, l->first) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00004910 if (t->text || t->type == TOK_WHITESPACE) {
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04004911 tt = *tail = new_Token(NULL, t->type, t->text, 0);
H. Peter Anvine2c80182005-01-15 22:15:51 +00004912 tail = &tt->next;
4913 }
4914 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00004915
H. Peter Anvine2c80182005-01-15 22:15:51 +00004916 istk->expansion = ll;
4917 }
4918 } else {
4919 /*
4920 * Check whether a `%rep' was started and not ended
4921 * within this macro expansion. This can happen and
4922 * should be detected. It's a fatal error because
4923 * I'm too confused to work out how to recover
4924 * sensibly from it.
4925 */
4926 if (defining) {
4927 if (defining->name)
4928 error(ERR_PANIC,
4929 "defining with name in expansion");
4930 else if (istk->mstk->name)
4931 error(ERR_FATAL,
4932 "`%%rep' without `%%endrep' within"
4933 " expansion of macro `%s'",
4934 istk->mstk->name);
4935 }
H. Peter Anvin87bc6192002-04-30 20:53:16 +00004936
H. Peter Anvine2c80182005-01-15 22:15:51 +00004937 /*
4938 * FIXME: investigate the relationship at this point between
4939 * istk->mstk and l->finishes
4940 */
4941 {
4942 MMacro *m = istk->mstk;
4943 istk->mstk = m->next_active;
4944 if (m->name) {
4945 /*
4946 * This was a real macro call, not a %rep, and
4947 * therefore the parameter information needs to
4948 * be freed.
4949 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004950 if (m->prev) {
4951 pop_mmacro(m);
4952 l->finishes->in_progress --;
4953 } else {
Keith Kanios891775e2009-07-11 06:08:54 -05004954 nasm_free(m->params);
4955 free_tlist(m->iline);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004956 nasm_free(m->paramlen);
4957 l->finishes->in_progress = 0;
4958 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00004959 } else
4960 free_mmacro(m);
4961 }
4962 istk->expansion = l->next;
4963 nasm_free(l);
4964 list->downlevel(LIST_MACRO);
4965 }
4966 }
4967 while (1) { /* until we get a line we can use */
H. Peter Anvineba20a72002-04-30 20:53:55 +00004968
H. Peter Anvine2c80182005-01-15 22:15:51 +00004969 if (istk->expansion) { /* from a macro expansion */
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004970 char *p;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004971 Line *l = istk->expansion;
4972 if (istk->mstk)
4973 istk->mstk->lineno++;
4974 tline = l->first;
4975 istk->expansion = l->next;
4976 nasm_free(l);
H. Peter Anvin6867acc2007-10-10 14:58:45 -07004977 p = detoken(tline, false);
H. Peter Anvine2c80182005-01-15 22:15:51 +00004978 list->line(LIST_MACRO, p);
4979 nasm_free(p);
4980 break;
4981 }
4982 line = read_line();
4983 if (line) { /* from the current input file */
4984 line = prepreproc(line);
Keith Kaniosb7a89542007-04-12 02:40:54 +00004985 tline = tokenize(line);
H. Peter Anvine2c80182005-01-15 22:15:51 +00004986 nasm_free(line);
4987 break;
4988 }
4989 /*
4990 * The current file has ended; work down the istk
4991 */
4992 {
4993 Include *i = istk;
4994 fclose(i->fp);
Cyrill Gorcunov530c1ed2010-09-12 02:00:05 +04004995 if (i->conds) {
4996 /* nasm_error can't be conditionally suppressed */
4997 nasm_error(ERR_FATAL,
4998 "expected `%%endif' before end of file");
4999 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00005000 /* only set line and file name if there's a next node */
5001 if (i->next) {
5002 src_set_linnum(i->lineno);
5003 nasm_free(src_set_fname(i->fname));
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03005004 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00005005 istk = i->next;
5006 list->downlevel(LIST_INCLUDE);
5007 nasm_free(i);
5008 if (!istk)
5009 return NULL;
Victor van den Elzen4c9d6222008-10-01 13:08:50 +02005010 if (istk->expansion && istk->expansion->finishes)
5011 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005012 }
5013 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005014
H. Peter Anvine2c80182005-01-15 22:15:51 +00005015 /*
5016 * We must expand MMacro parameters and MMacro-local labels
5017 * _before_ we plunge into directive processing, to cope
5018 * with things like `%define something %1' such as STRUC
5019 * uses. Unless we're _defining_ a MMacro, in which case
5020 * those tokens should be left alone to go into the
5021 * definition; and unless we're in a non-emitting
5022 * condition, in which case we don't want to meddle with
5023 * anything.
5024 */
Charles Crayned4200be2008-07-12 16:42:33 -07005025 if (!defining && !(istk->conds && !emitting(istk->conds->state))
H. Peter Anvin992fe752008-10-19 15:45:05 -07005026 && !(istk->mstk && !istk->mstk->in_progress)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00005027 tline = expand_mmac_params(tline);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03005028 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00005029
H. Peter Anvine2c80182005-01-15 22:15:51 +00005030 /*
5031 * Check the line to see if it's a preprocessor directive.
5032 */
5033 if (do_directive(tline) == DIRECTIVE_FOUND) {
5034 continue;
5035 } else if (defining) {
5036 /*
5037 * We're defining a multi-line macro. We emit nothing
5038 * at all, and just
Keith Kaniosb7a89542007-04-12 02:40:54 +00005039 * shove the tokenized line on to the macro definition.
H. Peter Anvine2c80182005-01-15 22:15:51 +00005040 */
5041 Line *l = nasm_malloc(sizeof(Line));
5042 l->next = defining->expansion;
5043 l->first = tline;
H. Peter Anvin538002d2008-06-28 18:30:27 -07005044 l->finishes = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005045 defining->expansion = l;
5046 continue;
5047 } else if (istk->conds && !emitting(istk->conds->state)) {
5048 /*
5049 * We're in a non-emitting branch of a condition block.
5050 * Emit nothing at all, not even a blank line: when we
5051 * emerge from the condition we'll give a line-number
5052 * directive so we keep our place correctly.
5053 */
5054 free_tlist(tline);
5055 continue;
5056 } else if (istk->mstk && !istk->mstk->in_progress) {
5057 /*
5058 * We're in a %rep block which has been terminated, so
5059 * we're walking through to the %endrep without
5060 * emitting anything. Emit nothing at all, not even a
5061 * blank line: when we emerge from the %rep block we'll
5062 * give a line-number directive so we keep our place
5063 * correctly.
5064 */
5065 free_tlist(tline);
5066 continue;
5067 } else {
5068 tline = expand_smacro(tline);
5069 if (!expand_mmacro(tline)) {
5070 /*
Keith Kaniosb7a89542007-04-12 02:40:54 +00005071 * De-tokenize the line again, and emit it.
H. Peter Anvine2c80182005-01-15 22:15:51 +00005072 */
H. Peter Anvin6867acc2007-10-10 14:58:45 -07005073 line = detoken(tline, true);
H. Peter Anvine2c80182005-01-15 22:15:51 +00005074 free_tlist(tline);
5075 break;
5076 } else {
5077 continue; /* expand_mmacro calls free_tlist */
5078 }
5079 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005080 }
5081
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005082 return line;
5083}
5084
H. Peter Anvine2c80182005-01-15 22:15:51 +00005085static void pp_cleanup(int pass)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005086{
H. Peter Anvine2c80182005-01-15 22:15:51 +00005087 if (defining) {
H. Peter Anvin89cee572009-07-15 09:16:54 -04005088 if (defining->name) {
Victor van den Elzen8f1120f2008-07-16 13:41:37 +02005089 error(ERR_NONFATAL,
5090 "end of file while still defining macro `%s'",
5091 defining->name);
5092 } else {
5093 error(ERR_NONFATAL, "end of file while still in %%rep");
5094 }
5095
H. Peter Anvine2c80182005-01-15 22:15:51 +00005096 free_mmacro(defining);
Cyrill Gorcunova327c652010-02-14 17:19:38 +03005097 defining = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005098 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005099 while (cstk)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005100 ctx_pop();
H. Peter Anvin97a23472007-09-16 17:57:25 -07005101 free_macros();
H. Peter Anvine2c80182005-01-15 22:15:51 +00005102 while (istk) {
5103 Include *i = istk;
5104 istk = istk->next;
5105 fclose(i->fp);
5106 nasm_free(i->fname);
5107 nasm_free(i);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005108 }
5109 while (cstk)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005110 ctx_pop();
H. Peter Anvin86877b22008-06-20 15:55:45 -07005111 nasm_free(src_set_fname(NULL));
H. Peter Anvine2c80182005-01-15 22:15:51 +00005112 if (pass == 0) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03005113 IncPath *i;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005114 free_llist(predef);
5115 delete_Blocks();
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03005116 while ((i = ipath)) {
5117 ipath = i->next;
5118 if (i->path)
5119 nasm_free(i->path);
5120 nasm_free(i);
5121 }
H. Peter Anvin11dfa1a2008-07-02 18:11:04 -07005122 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005123}
5124
Keith Kaniosa6dfa782007-04-13 16:47:53 +00005125void pp_include_path(char *path)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005126{
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005127 IncPath *i;
H. Peter Anvin37a321f2007-09-24 13:41:58 -07005128
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005129 i = nasm_malloc(sizeof(IncPath));
H. Peter Anvin37a321f2007-09-24 13:41:58 -07005130 i->path = path ? nasm_strdup(path) : NULL;
Frank Kotlerd0ed6fd2003-08-27 11:33:56 +00005131 i->next = NULL;
5132
H. Peter Anvin89cee572009-07-15 09:16:54 -04005133 if (ipath) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00005134 IncPath *j = ipath;
H. Peter Anvin89cee572009-07-15 09:16:54 -04005135 while (j->next)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005136 j = j->next;
5137 j->next = i;
5138 } else {
5139 ipath = i;
Frank Kotlerd0ed6fd2003-08-27 11:33:56 +00005140 }
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005141}
Frank Kotlerd0ed6fd2003-08-27 11:33:56 +00005142
Keith Kaniosa6dfa782007-04-13 16:47:53 +00005143void pp_pre_include(char *fname)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005144{
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005145 Token *inc, *space, *name;
5146 Line *l;
5147
H. Peter Anvin734b1882002-04-30 21:01:08 +00005148 name = new_Token(NULL, TOK_INTERNAL_STRING, fname, 0);
5149 space = new_Token(name, TOK_WHITESPACE, NULL, 0);
5150 inc = new_Token(space, TOK_PREPROC_ID, "%include", 0);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005151
5152 l = nasm_malloc(sizeof(Line));
5153 l->next = predef;
5154 l->first = inc;
H. Peter Anvin538002d2008-06-28 18:30:27 -07005155 l->finishes = NULL;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005156 predef = l;
5157}
5158
Keith Kaniosa6dfa782007-04-13 16:47:53 +00005159void pp_pre_define(char *definition)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005160{
5161 Token *def, *space;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005162 Line *l;
Keith Kaniosa6dfa782007-04-13 16:47:53 +00005163 char *equals;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005164
5165 equals = strchr(definition, '=');
H. Peter Anvin734b1882002-04-30 21:01:08 +00005166 space = new_Token(NULL, TOK_WHITESPACE, NULL, 0);
5167 def = new_Token(space, TOK_PREPROC_ID, "%define", 0);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005168 if (equals)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005169 *equals = ' ';
Keith Kaniosb7a89542007-04-12 02:40:54 +00005170 space->next = tokenize(definition);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005171 if (equals)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005172 *equals = '=';
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005173
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005174 l = nasm_malloc(sizeof(Line));
5175 l->next = predef;
5176 l->first = def;
H. Peter Anvin538002d2008-06-28 18:30:27 -07005177 l->finishes = NULL;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005178 predef = l;
5179}
5180
Keith Kaniosa6dfa782007-04-13 16:47:53 +00005181void pp_pre_undefine(char *definition)
H. Peter Anvin620515a2002-04-30 20:57:38 +00005182{
5183 Token *def, *space;
5184 Line *l;
5185
H. Peter Anvin734b1882002-04-30 21:01:08 +00005186 space = new_Token(NULL, TOK_WHITESPACE, NULL, 0);
5187 def = new_Token(space, TOK_PREPROC_ID, "%undef", 0);
Keith Kaniosb7a89542007-04-12 02:40:54 +00005188 space->next = tokenize(definition);
H. Peter Anvin620515a2002-04-30 20:57:38 +00005189
5190 l = nasm_malloc(sizeof(Line));
5191 l->next = predef;
5192 l->first = def;
H. Peter Anvin538002d2008-06-28 18:30:27 -07005193 l->finishes = NULL;
H. Peter Anvin620515a2002-04-30 20:57:38 +00005194 predef = l;
5195}
5196
Keith Kaniosb7a89542007-04-12 02:40:54 +00005197/*
5198 * Added by Keith Kanios:
5199 *
5200 * This function is used to assist with "runtime" preprocessor
5201 * directives. (e.g. pp_runtime("%define __BITS__ 64");)
5202 *
5203 * ERRORS ARE IGNORED HERE, SO MAKE COMPLETELY SURE THAT YOU
5204 * PASS A VALID STRING TO THIS FUNCTION!!!!!
5205 */
5206
Keith Kaniosa6dfa782007-04-13 16:47:53 +00005207void pp_runtime(char *definition)
Keith Kaniosb7a89542007-04-12 02:40:54 +00005208{
5209 Token *def;
H. Peter Anvin70653092007-10-19 14:42:29 -07005210
Keith Kaniosb7a89542007-04-12 02:40:54 +00005211 def = tokenize(definition);
H. Peter Anvin89cee572009-07-15 09:16:54 -04005212 if (do_directive(def) == NO_DIRECTIVE_FOUND)
Keith Kaniosb7a89542007-04-12 02:40:54 +00005213 free_tlist(def);
H. Peter Anvin70653092007-10-19 14:42:29 -07005214
Keith Kaniosb7a89542007-04-12 02:40:54 +00005215}
5216
H. Peter Anvina70547f2008-07-19 21:44:26 -07005217void pp_extra_stdmac(macros_t *macros)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005218{
H. Peter Anvin76690a12002-04-30 20:52:49 +00005219 extrastdmac = macros;
5220}
5221
Keith Kaniosa5fc6462007-10-13 07:09:22 -07005222static void make_tok_num(Token * tok, int64_t val)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005223{
Keith Kaniosa6dfa782007-04-13 16:47:53 +00005224 char numbuf[20];
Keith Kaniosa5fc6462007-10-13 07:09:22 -07005225 snprintf(numbuf, sizeof(numbuf), "%"PRId64"", val);
H. Peter Anvineba20a72002-04-30 20:53:55 +00005226 tok->text = nasm_strdup(numbuf);
5227 tok->type = TOK_NUMBER;
5228}
5229
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005230Preproc nasmpp = {
5231 pp_reset,
5232 pp_getline,
5233 pp_cleanup
5234};