blob: 70e550d0c1f51f927dce3c90fb777c4d1ff46857 [file] [log] [blame]
H. Peter Anvin9e6747c2009-06-28 17:13:04 -07001/* ----------------------------------------------------------------------- *
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002 *
3 * Copyright 1996-2010 The NASM Authors - All Rights Reserved
H. Peter Anvin9e6747c2009-06-28 17:13:04 -07004 * See the file AUTHORS included with the NASM distribution for
5 * the specific copyright holders.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00006 *
H. Peter Anvin9e6747c2009-06-28 17:13:04 -07007 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following
9 * conditions are met:
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000010 *
H. Peter Anvin9e6747c2009-06-28 17:13:04 -070011 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above
14 * copyright notice, this list of conditions and the following
15 * disclaimer in the documentation and/or other materials provided
16 * with the distribution.
Cyrill Gorcunovaccda192010-02-16 10:27:56 +030017 *
H. Peter Anvin9e6747c2009-06-28 17:13:04 -070018 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
19 * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
20 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
21 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
23 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
26 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
29 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
30 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 *
32 * ----------------------------------------------------------------------- */
33
34/*
35 * preproc.c macro preprocessor for the Netwide Assembler
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000036 */
37
H. Peter Anvin4836e332002-04-30 20:56:43 +000038/* Typical flow of text through preproc
39 *
Keith Kaniosb7a89542007-04-12 02:40:54 +000040 * pp_getline gets tokenized lines, either
H. Peter Anvin4836e332002-04-30 20:56:43 +000041 *
42 * from a macro expansion
43 *
44 * or
45 * {
46 * read_line gets raw text from stdmacpos, or predef, or current input file
Keith Kaniosb7a89542007-04-12 02:40:54 +000047 * tokenize converts to tokens
H. Peter Anvin4836e332002-04-30 20:56:43 +000048 * }
49 *
50 * expand_mmac_params is used to expand %1 etc., unless a macro is being
51 * defined or a false conditional is being processed
52 * (%0, %1, %+1, %-1, %%foo
53 *
54 * do_directive checks for directives
55 *
56 * expand_smacro is used to expand single line macros
57 *
58 * expand_mmacro is used to expand multi-line macros
59 *
60 * detoken is used to convert the line back to text
61 */
H. Peter Anvineba20a72002-04-30 20:53:55 +000062
H. Peter Anvinfe501952007-10-02 21:53:51 -070063#include "compiler.h"
64
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000065#include <stdio.h>
H. Peter Anvinaf535c12002-04-30 20:59:21 +000066#include <stdarg.h>
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000067#include <stdlib.h>
68#include <stddef.h>
69#include <string.h>
70#include <ctype.h>
H. Peter Anvin76690a12002-04-30 20:52:49 +000071#include <limits.h>
Keith Kaniosb7a89542007-04-12 02:40:54 +000072#include <inttypes.h>
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000073
74#include "nasm.h"
75#include "nasmlib.h"
H. Peter Anvin4169a472007-09-12 01:29:43 +000076#include "preproc.h"
H. Peter Anvin97a23472007-09-16 17:57:25 -070077#include "hashtbl.h"
H. Peter Anvin8cad14b2008-06-01 17:23:51 -070078#include "quote.h"
H. Peter Anvinc2df2822007-10-24 15:29:28 -070079#include "stdscan.h"
H. Peter Anvindbb640b2009-07-18 18:57:16 -070080#include "eval.h"
H. Peter Anvinc2df2822007-10-24 15:29:28 -070081#include "tokens.h"
H. Peter Anvina4835d42008-05-20 14:21:29 -070082#include "tables.h"
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000083
84typedef struct SMacro SMacro;
85typedef struct MMacro MMacro;
Keith Kanios891775e2009-07-11 06:08:54 -050086typedef struct MMacroInvocation MMacroInvocation;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000087typedef struct Context Context;
88typedef struct Token Token;
H. Peter Anvince616072002-04-30 21:02:23 +000089typedef struct Blocks Blocks;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000090typedef struct Line Line;
91typedef struct Include Include;
92typedef struct Cond Cond;
H. Peter Anvin6768eb72002-04-30 20:52:26 +000093typedef struct IncPath IncPath;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000094
95/*
H. Peter Anvin97a23472007-09-16 17:57:25 -070096 * Note on the storage of both SMacro and MMacros: the hash table
97 * indexes them case-insensitively, and we then have to go through a
98 * linked list of potential case aliases (and, for MMacros, parameter
99 * ranges); this is to preserve the matching semantics of the earlier
100 * code. If the number of case aliases for a specific macro is a
101 * performance issue, you may want to reconsider your coding style.
102 */
103
104/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000105 * Store the definition of a single-line macro.
106 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000107struct SMacro {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000108 SMacro *next;
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000109 char *name;
H. Peter Anvin70055962007-10-11 00:05:31 -0700110 bool casesense;
H. Peter Anvin16ed4382007-10-11 10:06:19 -0700111 bool in_progress;
H. Peter Anvin70055962007-10-11 00:05:31 -0700112 unsigned int nparam;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000113 Token *expansion;
114};
115
116/*
H. Peter Anvin76690a12002-04-30 20:52:49 +0000117 * Store the definition of a multi-line macro. This is also used to
118 * store the interiors of `%rep...%endrep' blocks, which are
119 * effectively self-re-invoking multi-line macros which simply
120 * don't have a name or bother to appear in the hash tables. %rep
121 * blocks are signified by having a NULL `name' field.
122 *
123 * In a MMacro describing a `%rep' block, the `in_progress' field
124 * isn't merely boolean, but gives the number of repeats left to
125 * run.
126 *
127 * The `next' field is used for storing MMacros in hash tables; the
128 * `next_active' field is for stacking them on istk entries.
129 *
130 * When a MMacro is being expanded, `params', `iline', `nparam',
131 * `paramlen', `rotate' and `unique' are local to the invocation.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000132 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000133struct MMacro {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000134 MMacro *next;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300135 MMacroInvocation *prev; /* previous invocation */
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000136 char *name;
H. Peter Anvin8cfdb9d2007-09-14 18:36:01 -0700137 int nparam_min, nparam_max;
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700138 bool casesense;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300139 bool plus; /* is the last parameter greedy? */
140 bool nolist; /* is this macro listing-inhibited? */
141 int64_t in_progress; /* is this macro currently being expanded? */
142 int32_t max_depth; /* maximum number of recursive expansions allowed */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000143 Token *dlist; /* All defaults as one list */
144 Token **defaults; /* Parameter default pointers */
145 int ndefs; /* number of default parameters */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000146 Line *expansion;
H. Peter Anvin76690a12002-04-30 20:52:49 +0000147
148 MMacro *next_active;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000149 MMacro *rep_nest; /* used for nesting %rep */
150 Token **params; /* actual parameters */
151 Token *iline; /* invocation line */
H. Peter Anvin25a99342007-09-22 17:45:45 -0700152 unsigned int nparam, rotate;
153 int *paramlen;
H. Peter Anvinf8ba53e2007-10-11 10:11:57 -0700154 uint64_t unique;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000155 int lineno; /* Current line number on expansion */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300156 uint64_t condcnt; /* number of if blocks... */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000157};
158
Keith Kanios891775e2009-07-11 06:08:54 -0500159
160/* Store the definition of a multi-line macro, as defined in a
161 * previous recursive macro expansion.
162 */
163struct MMacroInvocation {
H. Peter Anvin89cee572009-07-15 09:16:54 -0400164 MMacroInvocation *prev; /* previous invocation */
165 Token **params; /* actual parameters */
166 Token *iline; /* invocation line */
Keith Kanios891775e2009-07-11 06:08:54 -0500167 unsigned int nparam, rotate;
168 int *paramlen;
169 uint64_t unique;
Keith Kanios3c0d91f2009-10-25 13:28:03 -0500170 uint64_t condcnt;
Keith Kanios891775e2009-07-11 06:08:54 -0500171};
172
173
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000174/*
175 * The context stack is composed of a linked list of these.
176 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000177struct Context {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000178 Context *next;
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000179 char *name;
H. Peter Anvin166c2472008-05-28 12:28:58 -0700180 struct hash_table localmac;
Keith Kaniosb7a89542007-04-12 02:40:54 +0000181 uint32_t number;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000182};
183
184/*
185 * This is the internal form which we break input lines up into.
186 * Typically stored in linked lists.
187 *
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000188 * Note that `type' serves a double meaning: TOK_SMAC_PARAM is not
189 * necessarily used as-is, but is intended to denote the number of
190 * the substituted parameter. So in the definition
191 *
192 * %define a(x,y) ( (x) & ~(y) )
H. Peter Anvin70653092007-10-19 14:42:29 -0700193 *
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000194 * the token representing `x' will have its type changed to
195 * TOK_SMAC_PARAM, but the one representing `y' will be
196 * TOK_SMAC_PARAM+1.
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000197 *
198 * TOK_INTERNAL_STRING is a dirty hack: it's a single string token
199 * which doesn't need quotes around it. Used in the pre-include
200 * mechanism as an alternative to trying to find a sensible type of
201 * quote to use on the filename we were passed.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000202 */
H. Peter Anvinda10e7b2007-09-12 04:18:37 +0000203enum pp_token_type {
204 TOK_NONE = 0, TOK_WHITESPACE, TOK_COMMENT, TOK_ID,
205 TOK_PREPROC_ID, TOK_STRING,
H. Peter Anvin6c81f0a2008-05-25 21:46:17 -0700206 TOK_NUMBER, TOK_FLOAT, TOK_SMAC_END, TOK_OTHER,
207 TOK_INTERNAL_STRING,
208 TOK_PREPROC_Q, TOK_PREPROC_QQ,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300209 TOK_PASTE, /* %+ */
210 TOK_INDIRECT, /* %[...] */
211 TOK_SMAC_PARAM, /* MUST BE LAST IN THE LIST!!! */
212 TOK_MAX = INT_MAX /* Keep compiler from reducing the range */
H. Peter Anvinda10e7b2007-09-12 04:18:37 +0000213};
214
H. Peter Anvine2c80182005-01-15 22:15:51 +0000215struct Token {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000216 Token *next;
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000217 char *text;
H. Peter Anvinf26e0972008-07-01 21:26:27 -0700218 union {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300219 SMacro *mac; /* associated macro for TOK_SMAC_END */
220 size_t len; /* scratch length field */
221 } a; /* Auxiliary data */
H. Peter Anvinda10e7b2007-09-12 04:18:37 +0000222 enum pp_token_type type;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000223};
224
225/*
226 * Multi-line macro definitions are stored as a linked list of
227 * these, which is essentially a container to allow several linked
228 * lists of Tokens.
H. Peter Anvin70653092007-10-19 14:42:29 -0700229 *
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000230 * Note that in this module, linked lists are treated as stacks
231 * wherever possible. For this reason, Lines are _pushed_ on to the
232 * `expansion' field in MMacro structures, so that the linked list,
233 * if walked, would give the macro lines in reverse order; this
234 * means that we can walk the list when expanding a macro, and thus
235 * push the lines on to the `expansion' field in _istk_ in reverse
236 * order (so that when popped back off they are in the right
237 * order). It may seem cockeyed, and it relies on my design having
238 * an even number of steps in, but it works...
239 *
240 * Some of these structures, rather than being actual lines, are
241 * markers delimiting the end of the expansion of a given macro.
H. Peter Anvin76690a12002-04-30 20:52:49 +0000242 * This is for use in the cycle-tracking and %rep-handling code.
243 * Such structures have `finishes' non-NULL, and `first' NULL. All
244 * others have `finishes' NULL, but `first' may still be NULL if
245 * the line is blank.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000246 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000247struct Line {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000248 Line *next;
249 MMacro *finishes;
250 Token *first;
251};
252
253/*
254 * To handle an arbitrary level of file inclusion, we maintain a
255 * stack (ie linked list) of these things.
256 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000257struct Include {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000258 Include *next;
259 FILE *fp;
260 Cond *conds;
261 Line *expansion;
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000262 char *fname;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000263 int lineno, lineinc;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300264 MMacro *mstk; /* stack of active macros/reps */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000265};
266
267/*
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000268 * Include search path. This is simply a list of strings which get
269 * prepended, in turn, to the name of an include file, in an
270 * attempt to find the file if it's not in the current directory.
271 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000272struct IncPath {
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000273 IncPath *next;
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000274 char *path;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000275};
276
277/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000278 * Conditional assembly: we maintain a separate stack of these for
279 * each level of file inclusion. (The only reason we keep the
280 * stacks separate is to ensure that a stray `%endif' in a file
281 * included from within the true branch of a `%if' won't terminate
282 * it and cause confusion: instead, rightly, it'll cause an error.)
283 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000284struct Cond {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000285 Cond *next;
286 int state;
287};
H. Peter Anvine2c80182005-01-15 22:15:51 +0000288enum {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000289 /*
290 * These states are for use just after %if or %elif: IF_TRUE
291 * means the condition has evaluated to truth so we are
292 * currently emitting, whereas IF_FALSE means we are not
293 * currently emitting but will start doing so if a %else comes
294 * up. In these states, all directives are admissible: %elif,
295 * %else and %endif. (And of course %if.)
296 */
297 COND_IF_TRUE, COND_IF_FALSE,
298 /*
299 * These states come up after a %else: ELSE_TRUE means we're
300 * emitting, and ELSE_FALSE means we're not. In ELSE_* states,
301 * any %elif or %else will cause an error.
302 */
303 COND_ELSE_TRUE, COND_ELSE_FALSE,
304 /*
Victor van den Elzen3b404c02008-09-18 13:51:36 +0200305 * These states mean that we're not emitting now, and also that
306 * nothing until %endif will be emitted at all. COND_DONE is
307 * used when we've had our moment of emission
308 * and have now started seeing %elifs. COND_NEVER is used when
309 * the condition construct in question is contained within a
310 * non-emitting branch of a larger condition construct,
311 * or if there is an error.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000312 */
Victor van den Elzen3b404c02008-09-18 13:51:36 +0200313 COND_DONE, COND_NEVER
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000314};
315#define emitting(x) ( (x) == COND_IF_TRUE || (x) == COND_ELSE_TRUE )
316
H. Peter Anvin70653092007-10-19 14:42:29 -0700317/*
Ed Beroset3ab3f412002-06-11 03:31:49 +0000318 * These defines are used as the possible return values for do_directive
319 */
320#define NO_DIRECTIVE_FOUND 0
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300321#define DIRECTIVE_FOUND 1
Ed Beroset3ab3f412002-06-11 03:31:49 +0000322
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000323/*
Keith Kanios852f1ee2009-07-12 00:19:55 -0500324 * This define sets the upper limit for smacro and recursive mmacro
325 * expansions
326 */
327#define DEADMAN_LIMIT (1 << 20)
328
Cyrill Gorcunove091d6e2010-08-09 13:58:22 +0400329/* max reps */
330#define REP_LIMIT ((INT64_C(1) << 62))
331
Keith Kanios852f1ee2009-07-12 00:19:55 -0500332/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000333 * Condition codes. Note that we use c_ prefix not C_ because C_ is
334 * used in nasm.h for the "real" condition codes. At _this_ level,
335 * we treat CXZ and ECXZ as condition codes, albeit non-invertible
336 * ones, so we need a different enum...
337 */
H. Peter Anvin476d2862007-10-02 22:04:15 -0700338static const char * const conditions[] = {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000339 "a", "ae", "b", "be", "c", "cxz", "e", "ecxz", "g", "ge", "l", "le",
340 "na", "nae", "nb", "nbe", "nc", "ne", "ng", "nge", "nl", "nle", "no",
H. Peter Anvince9be342007-09-12 00:22:29 +0000341 "np", "ns", "nz", "o", "p", "pe", "po", "rcxz", "s", "z"
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000342};
H. Peter Anvin476d2862007-10-02 22:04:15 -0700343enum pp_conds {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000344 c_A, c_AE, c_B, c_BE, c_C, c_CXZ, c_E, c_ECXZ, c_G, c_GE, c_L, c_LE,
345 c_NA, c_NAE, c_NB, c_NBE, c_NC, c_NE, c_NG, c_NGE, c_NL, c_NLE, c_NO,
H. Peter Anvin476d2862007-10-02 22:04:15 -0700346 c_NP, c_NS, c_NZ, c_O, c_P, c_PE, c_PO, c_RCXZ, c_S, c_Z,
347 c_none = -1
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000348};
H. Peter Anvin476d2862007-10-02 22:04:15 -0700349static const enum pp_conds inverse_ccs[] = {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000350 c_NA, c_NAE, c_NB, c_NBE, c_NC, -1, c_NE, -1, c_NG, c_NGE, c_NL, c_NLE,
351 c_A, c_AE, c_B, c_BE, c_C, c_E, c_G, c_GE, c_L, c_LE, c_O, c_P, c_S,
H. Peter Anvince9be342007-09-12 00:22:29 +0000352 c_Z, c_NO, c_NP, c_PO, c_PE, -1, c_NS, c_NZ
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000353};
354
H. Peter Anvin76690a12002-04-30 20:52:49 +0000355/*
356 * Directive names.
357 */
H. Peter Anvin65747262002-05-07 00:10:05 +0000358/* If this is a an IF, ELIF, ELSE or ENDIF keyword */
H. Peter Anvin9bf0aa72007-09-12 02:12:07 +0000359static int is_condition(enum preproc_token arg)
H. Peter Anvin65747262002-05-07 00:10:05 +0000360{
H. Peter Anvinda10e7b2007-09-12 04:18:37 +0000361 return PP_IS_COND(arg) || (arg == PP_ELSE) || (arg == PP_ENDIF);
H. Peter Anvin65747262002-05-07 00:10:05 +0000362}
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000363
364/* For TASM compatibility we need to be able to recognise TASM compatible
365 * conditional compilation directives. Using the NASM pre-processor does
366 * not work, so we look for them specifically from the following list and
367 * then jam in the equivalent NASM directive into the input stream.
368 */
369
H. Peter Anvine2c80182005-01-15 22:15:51 +0000370enum {
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000371 TM_ARG, TM_ELIF, TM_ELSE, TM_ENDIF, TM_IF, TM_IFDEF, TM_IFDIFI,
372 TM_IFNDEF, TM_INCLUDE, TM_LOCAL
373};
374
H. Peter Anvin476d2862007-10-02 22:04:15 -0700375static const char * const tasm_directives[] = {
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000376 "arg", "elif", "else", "endif", "if", "ifdef", "ifdifi",
377 "ifndef", "include", "local"
378};
379
380static int StackSize = 4;
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000381static char *StackPointer = "ebp";
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000382static int ArgOffset = 8;
H. Peter Anvin8781cb02007-11-08 20:01:11 -0800383static int LocalOffset = 0;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000384
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000385static Context *cstk;
386static Include *istk;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000387static IncPath *ipath = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000388
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300389static int pass; /* HACK: pass 0 = generate dependencies only */
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700390static StrList **dephead, **deptail; /* Dependency list */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000391
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300392static uint64_t unique; /* unique identifier numbers */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000393
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000394static Line *predef = NULL;
H. Peter Anvind2456592008-06-19 15:04:18 -0700395static bool do_predef;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000396
397static ListGen *list;
398
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000399/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000400 * The current set of multi-line macros we have defined.
401 */
H. Peter Anvin166c2472008-05-28 12:28:58 -0700402static struct hash_table mmacros;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000403
404/*
405 * The current set of single-line macros we have defined.
406 */
H. Peter Anvin166c2472008-05-28 12:28:58 -0700407static struct hash_table smacros;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000408
409/*
H. Peter Anvin76690a12002-04-30 20:52:49 +0000410 * The multi-line macro we are currently defining, or the %rep
411 * block we are currently reading, if any.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000412 */
413static MMacro *defining;
414
Charles Crayned4200be2008-07-12 16:42:33 -0700415static uint64_t nested_mac_count;
416static uint64_t nested_rep_count;
417
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000418/*
419 * The number of macro parameters to allocate space for at a time.
420 */
421#define PARAM_DELTA 16
422
423/*
H. Peter Anvina4835d42008-05-20 14:21:29 -0700424 * The standard macro set: defined in macros.c in the array nasm_stdmac.
425 * This gives our position in the macro set, when we're processing it.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000426 */
H. Peter Anvina70547f2008-07-19 21:44:26 -0700427static macros_t *stdmacpos;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000428
429/*
H. Peter Anvin76690a12002-04-30 20:52:49 +0000430 * The extra standard macros that come from the object format, if
431 * any.
432 */
H. Peter Anvina70547f2008-07-19 21:44:26 -0700433static macros_t *extrastdmac = NULL;
H. Peter Anvincfb71762008-06-20 15:20:16 -0700434static bool any_extrastdmac;
H. Peter Anvin76690a12002-04-30 20:52:49 +0000435
436/*
H. Peter Anvin734b1882002-04-30 21:01:08 +0000437 * Tokens are allocated in blocks to improve speed
438 */
439#define TOKEN_BLOCKSIZE 4096
440static Token *freeTokens = NULL;
H. Peter Anvince616072002-04-30 21:02:23 +0000441struct Blocks {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000442 Blocks *next;
443 void *chunk;
H. Peter Anvince616072002-04-30 21:02:23 +0000444};
445
446static Blocks blocks = { NULL, NULL };
H. Peter Anvin734b1882002-04-30 21:01:08 +0000447
448/*
H. Peter Anvin76690a12002-04-30 20:52:49 +0000449 * Forward declarations.
450 */
H. Peter Anvin734b1882002-04-30 21:01:08 +0000451static Token *expand_mmac_params(Token * tline);
452static Token *expand_smacro(Token * tline);
453static Token *expand_id(Token * tline);
H. Peter Anvinf8ad5322009-02-21 17:55:08 -0800454static Context *get_ctx(const char *name, const char **namep,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300455 bool all_contexts);
Keith Kaniosa5fc6462007-10-13 07:09:22 -0700456static void make_tok_num(Token * tok, int64_t val);
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000457static void error(int severity, const char *fmt, ...);
Victor van den Elzen3b404c02008-09-18 13:51:36 +0200458static void error_precond(int severity, const char *fmt, ...);
H. Peter Anvince616072002-04-30 21:02:23 +0000459static void *new_Block(size_t size);
460static void delete_Blocks(void);
H. Peter Anvinc751e862008-06-09 10:18:45 -0700461static Token *new_Token(Token * next, enum pp_token_type type,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300462 const char *text, int txtlen);
H. Peter Anvin734b1882002-04-30 21:01:08 +0000463static Token *delete_Token(Token * t);
H. Peter Anvineba20a72002-04-30 20:53:55 +0000464
465/*
466 * Macros for safe checking of token pointers, avoid *(NULL)
467 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300468#define tok_type_(x,t) ((x) && (x)->type == (t))
469#define skip_white_(x) if (tok_type_((x), TOK_WHITESPACE)) (x)=(x)->next
470#define tok_is_(x,v) (tok_type_((x), TOK_OTHER) && !strcmp((x)->text,(v)))
471#define tok_isnt_(x,v) ((x) && ((x)->type!=TOK_OTHER || strcmp((x)->text,(v))))
H. Peter Anvin76690a12002-04-30 20:52:49 +0000472
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300473/*
H. Peter Anvin077fb932010-07-20 14:56:30 -0700474 * nasm_unquote with error if the string contains NUL characters.
475 * If the string contains NUL characters, issue an error and return
476 * the C len, i.e. truncate at the NUL.
477 */
478static size_t nasm_unquote_cstr(char *qstr, enum preproc_token directive)
479{
480 size_t len = nasm_unquote(qstr, NULL);
481 size_t clen = strlen(qstr);
482
483 if (len != clen)
484 error(ERR_NONFATAL, "NUL character in `%s' directive",
485 pp_directives[directive]);
486
487 return clen;
488}
489
490/*
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300491 * Handle TASM specific directives, which do not contain a % in
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000492 * front of them. We do it here because I could not find any other
493 * place to do it for the moment, and it is a hack (ideally it would
494 * be nice to be able to use the NASM pre-processor to do it).
495 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000496static char *check_tasm_directive(char *line)
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000497{
Keith Kaniosb7a89542007-04-12 02:40:54 +0000498 int32_t i, j, k, m, len;
Cyrill Gorcunovf66ac7d2009-10-12 20:41:13 +0400499 char *p, *q, *oldline, oldchar;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000500
Cyrill Gorcunovf66ac7d2009-10-12 20:41:13 +0400501 p = nasm_skip_spaces(line);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000502
503 /* Binary search for the directive name */
504 i = -1;
Cyrill Gorcunova7319242010-06-03 22:04:36 +0400505 j = ARRAY_SIZE(tasm_directives);
Cyrill Gorcunovf66ac7d2009-10-12 20:41:13 +0400506 q = nasm_skip_word(p);
507 len = q - p;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000508 if (len) {
509 oldchar = p[len];
510 p[len] = 0;
511 while (j - i > 1) {
512 k = (j + i) / 2;
513 m = nasm_stricmp(p, tasm_directives[k]);
514 if (m == 0) {
515 /* We have found a directive, so jam a % in front of it
516 * so that NASM will then recognise it as one if it's own.
517 */
518 p[len] = oldchar;
519 len = strlen(p);
520 oldline = line;
521 line = nasm_malloc(len + 2);
522 line[0] = '%';
523 if (k == TM_IFDIFI) {
H. Peter Anvin18f48792009-06-27 15:56:27 -0700524 /*
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300525 * NASM does not recognise IFDIFI, so we convert
526 * it to %if 0. This is not used in NASM
527 * compatible code, but does need to parse for the
528 * TASM macro package.
H. Peter Anvine2c80182005-01-15 22:15:51 +0000529 */
H. Peter Anvin18f48792009-06-27 15:56:27 -0700530 strcpy(line + 1, "if 0");
H. Peter Anvine2c80182005-01-15 22:15:51 +0000531 } else {
532 memcpy(line + 1, p, len + 1);
533 }
534 nasm_free(oldline);
535 return line;
536 } else if (m < 0) {
537 j = k;
538 } else
539 i = k;
540 }
541 p[len] = oldchar;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000542 }
543 return line;
544}
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000545
H. Peter Anvin76690a12002-04-30 20:52:49 +0000546/*
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000547 * The pre-preprocessing stage... This function translates line
548 * number indications as they emerge from GNU cpp (`# lineno "file"
549 * flags') into NASM preprocessor line number indications (`%line
550 * lineno file').
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000551 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000552static char *prepreproc(char *line)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000553{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000554 int lineno, fnlen;
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000555 char *fname, *oldline;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000556
H. Peter Anvine2c80182005-01-15 22:15:51 +0000557 if (line[0] == '#' && line[1] == ' ') {
558 oldline = line;
559 fname = oldline + 2;
560 lineno = atoi(fname);
561 fname += strspn(fname, "0123456789 ");
562 if (*fname == '"')
563 fname++;
564 fnlen = strcspn(fname, "\"");
565 line = nasm_malloc(20 + fnlen);
566 snprintf(line, 20 + fnlen, "%%line %d %.*s", lineno, fnlen, fname);
567 nasm_free(oldline);
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000568 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000569 if (tasm_compatible_mode)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000570 return check_tasm_directive(line);
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000571 return line;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000572}
573
574/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000575 * Free a linked list of tokens.
576 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000577static void free_tlist(Token * list)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000578{
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +0400579 while (list)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000580 list = delete_Token(list);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000581}
582
583/*
584 * Free a linked list of lines.
585 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000586static void free_llist(Line * list)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000587{
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +0400588 Line *l, *tmp;
589 list_for_each_safe(l, tmp, list) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000590 free_tlist(l->first);
591 nasm_free(l);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000592 }
593}
594
595/*
H. Peter Anvineba20a72002-04-30 20:53:55 +0000596 * Free an MMacro
597 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000598static void free_mmacro(MMacro * m)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000599{
H. Peter Anvin734b1882002-04-30 21:01:08 +0000600 nasm_free(m->name);
601 free_tlist(m->dlist);
602 nasm_free(m->defaults);
603 free_llist(m->expansion);
604 nasm_free(m);
H. Peter Anvineba20a72002-04-30 20:53:55 +0000605}
606
607/*
H. Peter Anvin97a23472007-09-16 17:57:25 -0700608 * Free all currently defined macros, and free the hash tables
609 */
H. Peter Anvin072771e2008-05-22 13:17:51 -0700610static void free_smacro_table(struct hash_table *smt)
H. Peter Anvin97a23472007-09-16 17:57:25 -0700611{
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +0400612 SMacro *s, *tmp;
H. Peter Anvin072771e2008-05-22 13:17:51 -0700613 const char *key;
614 struct hash_tbl_node *it = NULL;
H. Peter Anvin97a23472007-09-16 17:57:25 -0700615
H. Peter Anvin072771e2008-05-22 13:17:51 -0700616 while ((s = hash_iterate(smt, &it, &key)) != NULL) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300617 nasm_free((void *)key);
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +0400618 list_for_each_safe(s, tmp, s) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300619 nasm_free(s->name);
620 free_tlist(s->expansion);
621 nasm_free(s);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300622 }
H. Peter Anvin97a23472007-09-16 17:57:25 -0700623 }
H. Peter Anvin072771e2008-05-22 13:17:51 -0700624 hash_free(smt);
625}
626
627static void free_mmacro_table(struct hash_table *mmt)
628{
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +0400629 MMacro *m, *tmp;
H. Peter Anvin072771e2008-05-22 13:17:51 -0700630 const char *key;
631 struct hash_tbl_node *it = NULL;
H. Peter Anvin97a23472007-09-16 17:57:25 -0700632
633 it = NULL;
H. Peter Anvin072771e2008-05-22 13:17:51 -0700634 while ((m = hash_iterate(mmt, &it, &key)) != NULL) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300635 nasm_free((void *)key);
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +0400636 list_for_each_safe(m ,tmp, m)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300637 free_mmacro(m);
H. Peter Anvin97a23472007-09-16 17:57:25 -0700638 }
H. Peter Anvin072771e2008-05-22 13:17:51 -0700639 hash_free(mmt);
640}
641
642static void free_macros(void)
643{
H. Peter Anvin166c2472008-05-28 12:28:58 -0700644 free_smacro_table(&smacros);
645 free_mmacro_table(&mmacros);
H. Peter Anvin97a23472007-09-16 17:57:25 -0700646}
647
648/*
649 * Initialize the hash tables
650 */
651static void init_macros(void)
652{
H. Peter Anvin166c2472008-05-28 12:28:58 -0700653 hash_init(&smacros, HASH_LARGE);
654 hash_init(&mmacros, HASH_LARGE);
H. Peter Anvin97a23472007-09-16 17:57:25 -0700655}
656
657/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000658 * Pop the context stack.
659 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000660static void ctx_pop(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000661{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000662 Context *c = cstk;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000663
664 cstk = cstk->next;
H. Peter Anvin166c2472008-05-28 12:28:58 -0700665 free_smacro_table(&c->localmac);
H. Peter Anvin734b1882002-04-30 21:01:08 +0000666 nasm_free(c->name);
667 nasm_free(c);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000668}
669
H. Peter Anvin072771e2008-05-22 13:17:51 -0700670/*
671 * Search for a key in the hash index; adding it if necessary
672 * (in which case we initialize the data pointer to NULL.)
673 */
674static void **
675hash_findi_add(struct hash_table *hash, const char *str)
676{
677 struct hash_insert hi;
678 void **r;
679 char *strx;
680
681 r = hash_findi(hash, str, &hi);
682 if (r)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300683 return r;
H. Peter Anvin072771e2008-05-22 13:17:51 -0700684
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300685 strx = nasm_strdup(str); /* Use a more efficient allocator here? */
H. Peter Anvin072771e2008-05-22 13:17:51 -0700686 return hash_add(&hi, strx, NULL);
687}
688
689/*
690 * Like hash_findi, but returns the data element rather than a pointer
691 * to it. Used only when not adding a new element, hence no third
692 * argument.
693 */
694static void *
695hash_findix(struct hash_table *hash, const char *str)
696{
697 void **p;
698
699 p = hash_findi(hash, str, NULL);
700 return p ? *p : NULL;
701}
702
Cyrill Gorcunov15bdc512010-07-13 11:27:41 +0400703/*
704 * read line from standart macros set,
705 * if there no more left -- return NULL
706 */
707static char *line_from_stdmac(void)
708{
709 unsigned char c;
710 const unsigned char *p = stdmacpos;
711 char *line, *q;
712 size_t len = 0;
713
714 if (!stdmacpos)
715 return NULL;
716
717 while ((c = *p++)) {
718 if (c >= 0x80)
719 len += pp_directives_len[c - 0x80] + 1;
720 else
721 len++;
722 }
723
724 line = nasm_malloc(len + 1);
725 q = line;
726 while ((c = *stdmacpos++)) {
727 if (c >= 0x80) {
728 memcpy(q, pp_directives[c - 0x80], pp_directives_len[c - 0x80]);
729 q += pp_directives_len[c - 0x80];
730 *q++ = ' ';
731 } else {
732 *q++ = c;
733 }
734 }
735 stdmacpos = p;
736 *q = '\0';
737
738 if (!*stdmacpos) {
739 /* This was the last of the standard macro chain... */
740 stdmacpos = NULL;
741 if (any_extrastdmac) {
742 stdmacpos = extrastdmac;
743 any_extrastdmac = false;
744 } else if (do_predef) {
745 Line *pd, *l;
746 Token *head, **tail, *t;
747
748 /*
749 * Nasty hack: here we push the contents of
750 * `predef' on to the top-level expansion stack,
751 * since this is the most convenient way to
752 * implement the pre-include and pre-define
753 * features.
754 */
755 list_for_each(pd, predef) {
756 head = NULL;
757 tail = &head;
758 list_for_each(t, pd->first) {
759 *tail = new_Token(NULL, t->type, t->text, 0);
760 tail = &(*tail)->next;
761 }
762
763 l = nasm_malloc(sizeof(Line));
764 l->next = istk->expansion;
765 l->first = head;
766 l->finishes = NULL;
767
768 istk->expansion = l;
769 }
770 do_predef = false;
771 }
772 }
773
774 return line;
775}
776
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000777#define BUF_DELTA 512
778/*
779 * Read a line from the top file in istk, handling multiple CR/LFs
780 * at the end of the line read, and handling spurious ^Zs. Will
781 * return lines from the standard macro set if this has not already
782 * been done.
783 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000784static char *read_line(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000785{
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000786 char *buffer, *p, *q;
H. Peter Anvin9f394642002-04-30 21:07:51 +0000787 int bufsize, continued_count;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000788
Cyrill Gorcunov15bdc512010-07-13 11:27:41 +0400789 /*
790 * standart macros set (predefined) goes first
791 */
792 p = line_from_stdmac();
793 if (p)
794 return p;
H. Peter Anvin72edbb82008-06-19 16:00:04 -0700795
Cyrill Gorcunov15bdc512010-07-13 11:27:41 +0400796 /*
797 * regular read from a file
798 */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000799 bufsize = BUF_DELTA;
800 buffer = nasm_malloc(BUF_DELTA);
801 p = buffer;
H. Peter Anvin9f394642002-04-30 21:07:51 +0000802 continued_count = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000803 while (1) {
804 q = fgets(p, bufsize - (p - buffer), istk->fp);
805 if (!q)
806 break;
807 p += strlen(p);
808 if (p > buffer && p[-1] == '\n') {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300809 /*
810 * Convert backslash-CRLF line continuation sequences into
811 * nothing at all (for DOS and Windows)
812 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000813 if (((p - 2) > buffer) && (p[-3] == '\\') && (p[-2] == '\r')) {
814 p -= 3;
815 *p = 0;
816 continued_count++;
817 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300818 /*
819 * Also convert backslash-LF line continuation sequences into
820 * nothing at all (for Unix)
821 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000822 else if (((p - 1) > buffer) && (p[-2] == '\\')) {
823 p -= 2;
824 *p = 0;
825 continued_count++;
826 } else {
827 break;
828 }
829 }
830 if (p - buffer > bufsize - 10) {
Keith Kaniosb7a89542007-04-12 02:40:54 +0000831 int32_t offset = p - buffer;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000832 bufsize += BUF_DELTA;
833 buffer = nasm_realloc(buffer, bufsize);
834 p = buffer + offset; /* prevent stale-pointer problems */
835 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000836 }
837
H. Peter Anvine2c80182005-01-15 22:15:51 +0000838 if (!q && p == buffer) {
839 nasm_free(buffer);
840 return NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000841 }
842
H. Peter Anvine2c80182005-01-15 22:15:51 +0000843 src_set_linnum(src_get_linnum() + istk->lineinc +
844 (continued_count * istk->lineinc));
H. Peter Anvineba20a72002-04-30 20:53:55 +0000845
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000846 /*
847 * Play safe: remove CRs as well as LFs, if any of either are
848 * present at the end of the line.
849 */
H. Peter Anvineba20a72002-04-30 20:53:55 +0000850 while (--p >= buffer && (*p == '\n' || *p == '\r'))
H. Peter Anvine2c80182005-01-15 22:15:51 +0000851 *p = '\0';
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000852
853 /*
854 * Handle spurious ^Z, which may be inserted into source files
855 * by some file transfer utilities.
856 */
857 buffer[strcspn(buffer, "\032")] = '\0';
858
H. Peter Anvin734b1882002-04-30 21:01:08 +0000859 list->line(LIST_READ, buffer);
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000860
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000861 return buffer;
862}
863
864/*
Keith Kaniosb7a89542007-04-12 02:40:54 +0000865 * Tokenize a line of text. This is a very simple process since we
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000866 * don't need to parse the value out of e.g. numeric tokens: we
867 * simply split one string into many.
868 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000869static Token *tokenize(char *line)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000870{
H. Peter Anvinca544db2008-10-19 19:30:11 -0700871 char c, *p = line;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +0000872 enum pp_token_type type;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000873 Token *list = NULL;
874 Token *t, **tail = &list;
875
H. Peter Anvine2c80182005-01-15 22:15:51 +0000876 while (*line) {
877 p = line;
878 if (*p == '%') {
879 p++;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300880 if (*p == '+' && !nasm_isdigit(p[1])) {
881 p++;
882 type = TOK_PASTE;
883 } else if (nasm_isdigit(*p) ||
884 ((*p == '-' || *p == '+') && nasm_isdigit(p[1]))) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000885 do {
886 p++;
887 }
H. Peter Anvinbda7a6e2008-06-21 10:23:17 -0700888 while (nasm_isdigit(*p));
H. Peter Anvine2c80182005-01-15 22:15:51 +0000889 type = TOK_PREPROC_ID;
890 } else if (*p == '{') {
891 p++;
892 while (*p && *p != '}') {
893 p[-1] = *p;
894 p++;
895 }
896 p[-1] = '\0';
897 if (*p)
898 p++;
899 type = TOK_PREPROC_ID;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300900 } else if (*p == '[') {
901 int lvl = 1;
902 line += 2; /* Skip the leading %[ */
903 p++;
904 while (lvl && (c = *p++)) {
905 switch (c) {
906 case ']':
907 lvl--;
908 break;
909 case '%':
910 if (*p == '[')
911 lvl++;
912 break;
913 case '\'':
914 case '\"':
915 case '`':
Cyrill Gorcunovc6360a72010-07-13 13:32:19 +0400916 p = nasm_skip_string(p - 1) + 1;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300917 break;
918 default:
919 break;
920 }
921 }
922 p--;
923 if (*p)
924 *p++ = '\0';
925 if (lvl)
926 error(ERR_NONFATAL, "unterminated %[ construct");
927 type = TOK_INDIRECT;
928 } else if (*p == '?') {
929 type = TOK_PREPROC_Q; /* %? */
930 p++;
931 if (*p == '?') {
932 type = TOK_PREPROC_QQ; /* %?? */
933 p++;
934 }
H. Peter Anvin077fb932010-07-20 14:56:30 -0700935 } else if (*p == '!') {
936 type = TOK_PREPROC_ID;
937 p++;
938 if (isidchar(*p)) {
939 do {
940 p++;
941 }
942 while (isidchar(*p));
943 } else if (*p == '\'' || *p == '\"' || *p == '`') {
944 p = nasm_skip_string(p);
945 if (*p)
946 p++;
947 else
948 error(ERR_NONFATAL|ERR_PASS1, "unterminated %! string");
949 } else {
950 /* %! without string or identifier */
951 type = TOK_OTHER; /* Legacy behavior... */
952 }
H. Peter Anvine2c80182005-01-15 22:15:51 +0000953 } else if (isidchar(*p) ||
954 ((*p == '!' || *p == '%' || *p == '$') &&
955 isidchar(p[1]))) {
956 do {
957 p++;
958 }
959 while (isidchar(*p));
960 type = TOK_PREPROC_ID;
961 } else {
962 type = TOK_OTHER;
963 if (*p == '%')
964 p++;
965 }
966 } else if (isidstart(*p) || (*p == '$' && isidstart(p[1]))) {
967 type = TOK_ID;
968 p++;
969 while (*p && isidchar(*p))
970 p++;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -0700971 } else if (*p == '\'' || *p == '"' || *p == '`') {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000972 /*
973 * A string token.
974 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000975 type = TOK_STRING;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300976 p = nasm_skip_string(p);
Nickolay Yurchenkof3b3ce22003-09-21 20:38:43 +0000977
H. Peter Anvine2c80182005-01-15 22:15:51 +0000978 if (*p) {
979 p++;
980 } else {
H. Peter Anvin917a3492008-09-24 09:14:49 -0700981 error(ERR_WARNING|ERR_PASS1, "unterminated string");
H. Peter Anvine2c80182005-01-15 22:15:51 +0000982 /* Handling unterminated strings by UNV */
983 /* type = -1; */
984 }
Victor van den Elzenfb5f2512009-04-17 16:17:59 +0200985 } else if (p[0] == '$' && p[1] == '$') {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300986 type = TOK_OTHER; /* TOKEN_BASE */
Victor van den Elzenfb5f2512009-04-17 16:17:59 +0200987 p += 2;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000988 } else if (isnumstart(*p)) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300989 bool is_hex = false;
990 bool is_float = false;
991 bool has_e = false;
992 char c, *r;
H. Peter Anvinc2df2822007-10-24 15:29:28 -0700993
H. Peter Anvine2c80182005-01-15 22:15:51 +0000994 /*
H. Peter Anvinc2df2822007-10-24 15:29:28 -0700995 * A numeric token.
H. Peter Anvine2c80182005-01-15 22:15:51 +0000996 */
H. Peter Anvinc2df2822007-10-24 15:29:28 -0700997
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300998 if (*p == '$') {
999 p++;
1000 is_hex = true;
1001 }
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001002
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001003 for (;;) {
1004 c = *p++;
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001005
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001006 if (!is_hex && (c == 'e' || c == 'E')) {
1007 has_e = true;
1008 if (*p == '+' || *p == '-') {
1009 /*
1010 * e can only be followed by +/- if it is either a
1011 * prefixed hex number or a floating-point number
1012 */
1013 p++;
1014 is_float = true;
1015 }
1016 } else if (c == 'H' || c == 'h' || c == 'X' || c == 'x') {
1017 is_hex = true;
1018 } else if (c == 'P' || c == 'p') {
1019 is_float = true;
1020 if (*p == '+' || *p == '-')
1021 p++;
1022 } else if (isnumchar(c) || c == '_')
1023 ; /* just advance */
1024 else if (c == '.') {
1025 /*
1026 * we need to deal with consequences of the legacy
1027 * parser, like "1.nolist" being two tokens
1028 * (TOK_NUMBER, TOK_ID) here; at least give it
1029 * a shot for now. In the future, we probably need
1030 * a flex-based scanner with proper pattern matching
1031 * to do it as well as it can be done. Nothing in
1032 * the world is going to help the person who wants
1033 * 0x123.p16 interpreted as two tokens, though.
1034 */
1035 r = p;
1036 while (*r == '_')
1037 r++;
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001038
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001039 if (nasm_isdigit(*r) || (is_hex && nasm_isxdigit(*r)) ||
1040 (!is_hex && (*r == 'e' || *r == 'E')) ||
1041 (*r == 'p' || *r == 'P')) {
1042 p = r;
1043 is_float = true;
1044 } else
1045 break; /* Terminate the token */
1046 } else
1047 break;
1048 }
1049 p--; /* Point to first character beyond number */
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001050
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001051 if (p == line+1 && *line == '$') {
1052 type = TOK_OTHER; /* TOKEN_HERE */
1053 } else {
1054 if (has_e && !is_hex) {
1055 /* 1e13 is floating-point, but 1e13h is not */
1056 is_float = true;
1057 }
H. Peter Anvind784a082009-04-20 14:01:18 -07001058
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001059 type = is_float ? TOK_FLOAT : TOK_NUMBER;
1060 }
H. Peter Anvinbda7a6e2008-06-21 10:23:17 -07001061 } else if (nasm_isspace(*p)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001062 type = TOK_WHITESPACE;
Cyrill Gorcunovf66ac7d2009-10-12 20:41:13 +04001063 p = nasm_skip_spaces(p);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001064 /*
1065 * Whitespace just before end-of-line is discarded by
1066 * pretending it's a comment; whitespace just before a
1067 * comment gets lumped into the comment.
1068 */
1069 if (!*p || *p == ';') {
1070 type = TOK_COMMENT;
1071 while (*p)
1072 p++;
1073 }
1074 } else if (*p == ';') {
1075 type = TOK_COMMENT;
1076 while (*p)
1077 p++;
1078 } else {
1079 /*
1080 * Anything else is an operator of some kind. We check
1081 * for all the double-character operators (>>, <<, //,
1082 * %%, <=, >=, ==, !=, <>, &&, ||, ^^), but anything
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001083 * else is a single-character operator.
H. Peter Anvine2c80182005-01-15 22:15:51 +00001084 */
1085 type = TOK_OTHER;
1086 if ((p[0] == '>' && p[1] == '>') ||
1087 (p[0] == '<' && p[1] == '<') ||
1088 (p[0] == '/' && p[1] == '/') ||
1089 (p[0] == '<' && p[1] == '=') ||
1090 (p[0] == '>' && p[1] == '=') ||
1091 (p[0] == '=' && p[1] == '=') ||
1092 (p[0] == '!' && p[1] == '=') ||
1093 (p[0] == '<' && p[1] == '>') ||
1094 (p[0] == '&' && p[1] == '&') ||
1095 (p[0] == '|' && p[1] == '|') ||
1096 (p[0] == '^' && p[1] == '^')) {
1097 p++;
1098 }
1099 p++;
1100 }
Nickolay Yurchenkof3b3ce22003-09-21 20:38:43 +00001101
H. Peter Anvine2c80182005-01-15 22:15:51 +00001102 /* Handling unterminated string by UNV */
1103 /*if (type == -1)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001104 {
1105 *tail = t = new_Token(NULL, TOK_STRING, line, p-line+1);
1106 t->text[p-line] = *line;
1107 tail = &t->next;
1108 }
1109 else */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001110 if (type != TOK_COMMENT) {
1111 *tail = t = new_Token(NULL, type, line, p - line);
1112 tail = &t->next;
1113 }
1114 line = p;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001115 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001116 return list;
1117}
1118
H. Peter Anvince616072002-04-30 21:02:23 +00001119/*
1120 * this function allocates a new managed block of memory and
H. Peter Anvin70653092007-10-19 14:42:29 -07001121 * returns a pointer to the block. The managed blocks are
H. Peter Anvince616072002-04-30 21:02:23 +00001122 * deleted only all at once by the delete_Blocks function.
1123 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001124static void *new_Block(size_t size)
H. Peter Anvince616072002-04-30 21:02:23 +00001125{
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001126 Blocks *b = &blocks;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001127
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001128 /* first, get to the end of the linked list */
1129 while (b->next)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001130 b = b->next;
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001131 /* now allocate the requested chunk */
1132 b->chunk = nasm_malloc(size);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001133
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001134 /* now allocate a new block for the next request */
1135 b->next = nasm_malloc(sizeof(Blocks));
1136 /* and initialize the contents of the new block */
1137 b->next->next = NULL;
1138 b->next->chunk = NULL;
1139 return b->chunk;
H. Peter Anvince616072002-04-30 21:02:23 +00001140}
1141
1142/*
1143 * this function deletes all managed blocks of memory
1144 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001145static void delete_Blocks(void)
H. Peter Anvince616072002-04-30 21:02:23 +00001146{
H. Peter Anvine2c80182005-01-15 22:15:51 +00001147 Blocks *a, *b = &blocks;
H. Peter Anvince616072002-04-30 21:02:23 +00001148
H. Peter Anvin70653092007-10-19 14:42:29 -07001149 /*
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001150 * keep in mind that the first block, pointed to by blocks
H. Peter Anvin70653092007-10-19 14:42:29 -07001151 * is a static and not dynamically allocated, so we don't
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001152 * free it.
1153 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001154 while (b) {
1155 if (b->chunk)
1156 nasm_free(b->chunk);
1157 a = b;
1158 b = b->next;
1159 if (a != &blocks)
1160 nasm_free(a);
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001161 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001162}
H. Peter Anvin734b1882002-04-30 21:01:08 +00001163
1164/*
H. Peter Anvin70653092007-10-19 14:42:29 -07001165 * this function creates a new Token and passes a pointer to it
H. Peter Anvin734b1882002-04-30 21:01:08 +00001166 * back to the caller. It sets the type and text elements, and
H. Peter Anvinf26e0972008-07-01 21:26:27 -07001167 * also the a.mac and next elements to NULL.
H. Peter Anvin734b1882002-04-30 21:01:08 +00001168 */
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07001169static Token *new_Token(Token * next, enum pp_token_type type,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001170 const char *text, int txtlen)
H. Peter Anvin734b1882002-04-30 21:01:08 +00001171{
1172 Token *t;
1173 int i;
1174
H. Peter Anvin89cee572009-07-15 09:16:54 -04001175 if (!freeTokens) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001176 freeTokens = (Token *) new_Block(TOKEN_BLOCKSIZE * sizeof(Token));
1177 for (i = 0; i < TOKEN_BLOCKSIZE - 1; i++)
1178 freeTokens[i].next = &freeTokens[i + 1];
1179 freeTokens[i].next = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001180 }
1181 t = freeTokens;
1182 freeTokens = t->next;
1183 t->next = next;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07001184 t->a.mac = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001185 t->type = type;
H. Peter Anvin89cee572009-07-15 09:16:54 -04001186 if (type == TOK_WHITESPACE || !text) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001187 t->text = NULL;
1188 } else {
1189 if (txtlen == 0)
1190 txtlen = strlen(text);
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07001191 t->text = nasm_malloc(txtlen+1);
1192 memcpy(t->text, text, txtlen);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001193 t->text[txtlen] = '\0';
H. Peter Anvin734b1882002-04-30 21:01:08 +00001194 }
1195 return t;
1196}
1197
H. Peter Anvine2c80182005-01-15 22:15:51 +00001198static Token *delete_Token(Token * t)
H. Peter Anvin734b1882002-04-30 21:01:08 +00001199{
1200 Token *next = t->next;
1201 nasm_free(t->text);
H. Peter Anvin788e6c12002-04-30 21:02:01 +00001202 t->next = freeTokens;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001203 freeTokens = t;
1204 return next;
1205}
1206
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001207/*
1208 * Convert a line of tokens back into text.
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001209 * If expand_locals is not zero, identifiers of the form "%$*xxx"
1210 * will be transformed into ..@ctxnum.xxx
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001211 */
H. Peter Anvin9e200162008-06-04 17:23:14 -07001212static char *detoken(Token * tlist, bool expand_locals)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001213{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001214 Token *t;
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001215 char *line, *p;
H. Peter Anvinb4daadc2008-01-21 16:31:57 -08001216 const char *q;
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001217 int len = 0;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001218
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001219 list_for_each(t, tlist) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001220 if (t->type == TOK_PREPROC_ID && t->text[1] == '!') {
H. Peter Anvin077fb932010-07-20 14:56:30 -07001221 char *v;
1222 char *q = t->text;
1223
1224 v = t->text + 2;
1225 if (*v == '\'' || *v == '\"' || *v == '`') {
1226 size_t len = nasm_unquote(v, NULL);
1227 size_t clen = strlen(v);
1228
1229 if (len != clen) {
1230 error(ERR_NONFATAL | ERR_PASS1,
1231 "NUL character in %! string");
1232 v = NULL;
1233 }
H. Peter Anvin5b00bf42010-07-13 11:43:06 -07001234 }
H. Peter Anvin077fb932010-07-20 14:56:30 -07001235
1236 if (v) {
1237 char *p = getenv(v);
1238 if (!p) {
1239 error(ERR_NONFATAL | ERR_PASS1,
1240 "nonexistent environment variable `%s'", v);
1241 p = "";
1242 }
1243 t->text = nasm_strdup(p);
1244 }
1245 nasm_free(q);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001246 }
H. Peter Anvin077fb932010-07-20 14:56:30 -07001247
H. Peter Anvine2c80182005-01-15 22:15:51 +00001248 /* Expand local macros here and not during preprocessing */
1249 if (expand_locals &&
1250 t->type == TOK_PREPROC_ID && t->text &&
1251 t->text[0] == '%' && t->text[1] == '$') {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001252 const char *q;
1253 char *p;
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001254 Context *ctx = get_ctx(t->text, &q, false);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001255 if (ctx) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001256 char buffer[40];
Keith Kanios93f2e9a2007-04-14 00:10:59 +00001257 snprintf(buffer, sizeof(buffer), "..@%"PRIu32".", ctx->number);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001258 p = nasm_strcat(buffer, q);
1259 nasm_free(t->text);
1260 t->text = p;
1261 }
1262 }
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001263 if (t->type == TOK_WHITESPACE)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001264 len++;
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001265 else if (t->text)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001266 len += strlen(t->text);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001267 }
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001268
H. Peter Anvin734b1882002-04-30 21:01:08 +00001269 p = line = nasm_malloc(len + 1);
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001270
1271 list_for_each(t, tlist) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001272 if (t->type == TOK_WHITESPACE) {
H. Peter Anvinb4daadc2008-01-21 16:31:57 -08001273 *p++ = ' ';
H. Peter Anvine2c80182005-01-15 22:15:51 +00001274 } else if (t->text) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001275 q = t->text;
1276 while (*q)
1277 *p++ = *q++;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001278 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001279 }
1280 *p = '\0';
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001281
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001282 return line;
1283}
1284
1285/*
H. Peter Anvin76690a12002-04-30 20:52:49 +00001286 * A scanner, suitable for use by the expression evaluator, which
1287 * operates on a line of Tokens. Expects a pointer to a pointer to
1288 * the first token in the line to be passed in as its private_data
1289 * field.
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001290 *
1291 * FIX: This really needs to be unified with stdscan.
H. Peter Anvin76690a12002-04-30 20:52:49 +00001292 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001293static int ppscan(void *private_data, struct tokenval *tokval)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001294{
H. Peter Anvin76690a12002-04-30 20:52:49 +00001295 Token **tlineptr = private_data;
1296 Token *tline;
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001297 char ourcopy[MAX_KEYWORD+1], *p, *r, *s;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001298
H. Peter Anvine2c80182005-01-15 22:15:51 +00001299 do {
1300 tline = *tlineptr;
1301 *tlineptr = tline ? tline->next : NULL;
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04001302 } while (tline && (tline->type == TOK_WHITESPACE ||
1303 tline->type == TOK_COMMENT));
H. Peter Anvin76690a12002-04-30 20:52:49 +00001304
1305 if (!tline)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001306 return tokval->t_type = TOKEN_EOS;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001307
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001308 tokval->t_charptr = tline->text;
1309
H. Peter Anvin76690a12002-04-30 20:52:49 +00001310 if (tline->text[0] == '$' && !tline->text[1])
H. Peter Anvine2c80182005-01-15 22:15:51 +00001311 return tokval->t_type = TOKEN_HERE;
H. Peter Anvin7cf897e2002-05-30 21:30:33 +00001312 if (tline->text[0] == '$' && tline->text[1] == '$' && !tline->text[2])
H. Peter Anvine2c80182005-01-15 22:15:51 +00001313 return tokval->t_type = TOKEN_BASE;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001314
H. Peter Anvine2c80182005-01-15 22:15:51 +00001315 if (tline->type == TOK_ID) {
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001316 p = tokval->t_charptr = tline->text;
1317 if (p[0] == '$') {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001318 tokval->t_charptr++;
1319 return tokval->t_type = TOKEN_ID;
1320 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00001321
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001322 for (r = p, s = ourcopy; *r; r++) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001323 if (r >= p+MAX_KEYWORD)
1324 return tokval->t_type = TOKEN_ID; /* Not a keyword */
H. Peter Anvinac8f8fc2008-06-11 15:49:41 -07001325 *s++ = nasm_tolower(*r);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001326 }
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001327 *s = '\0';
1328 /* right, so we have an identifier sitting in temp storage. now,
1329 * is it actually a register or instruction name, or what? */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001330 return nasm_token_hash(ourcopy, tokval);
H. Peter Anvin76690a12002-04-30 20:52:49 +00001331 }
1332
H. Peter Anvine2c80182005-01-15 22:15:51 +00001333 if (tline->type == TOK_NUMBER) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001334 bool rn_error;
1335 tokval->t_integer = readnum(tline->text, &rn_error);
1336 tokval->t_charptr = tline->text;
1337 if (rn_error)
1338 return tokval->t_type = TOKEN_ERRNUM;
1339 else
1340 return tokval->t_type = TOKEN_NUM;
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001341 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00001342
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001343 if (tline->type == TOK_FLOAT) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001344 return tokval->t_type = TOKEN_FLOAT;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001345 }
1346
H. Peter Anvine2c80182005-01-15 22:15:51 +00001347 if (tline->type == TOK_STRING) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001348 char bq, *ep;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001349
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001350 bq = tline->text[0];
H. Peter Anvin11627042008-06-09 20:45:19 -07001351 tokval->t_charptr = tline->text;
1352 tokval->t_inttwo = nasm_unquote(tline->text, &ep);
H. Peter Anvind2456592008-06-19 15:04:18 -07001353
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001354 if (ep[0] != bq || ep[1] != '\0')
1355 return tokval->t_type = TOKEN_ERRSTR;
1356 else
1357 return tokval->t_type = TOKEN_STR;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001358 }
1359
H. Peter Anvine2c80182005-01-15 22:15:51 +00001360 if (tline->type == TOK_OTHER) {
1361 if (!strcmp(tline->text, "<<"))
1362 return tokval->t_type = TOKEN_SHL;
1363 if (!strcmp(tline->text, ">>"))
1364 return tokval->t_type = TOKEN_SHR;
1365 if (!strcmp(tline->text, "//"))
1366 return tokval->t_type = TOKEN_SDIV;
1367 if (!strcmp(tline->text, "%%"))
1368 return tokval->t_type = TOKEN_SMOD;
1369 if (!strcmp(tline->text, "=="))
1370 return tokval->t_type = TOKEN_EQ;
1371 if (!strcmp(tline->text, "<>"))
1372 return tokval->t_type = TOKEN_NE;
1373 if (!strcmp(tline->text, "!="))
1374 return tokval->t_type = TOKEN_NE;
1375 if (!strcmp(tline->text, "<="))
1376 return tokval->t_type = TOKEN_LE;
1377 if (!strcmp(tline->text, ">="))
1378 return tokval->t_type = TOKEN_GE;
1379 if (!strcmp(tline->text, "&&"))
1380 return tokval->t_type = TOKEN_DBL_AND;
1381 if (!strcmp(tline->text, "^^"))
1382 return tokval->t_type = TOKEN_DBL_XOR;
1383 if (!strcmp(tline->text, "||"))
1384 return tokval->t_type = TOKEN_DBL_OR;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001385 }
1386
1387 /*
1388 * We have no other options: just return the first character of
1389 * the token text.
1390 */
1391 return tokval->t_type = tline->text[0];
1392}
1393
1394/*
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001395 * Compare a string to the name of an existing macro; this is a
1396 * simple wrapper which calls either strcmp or nasm_stricmp
1397 * depending on the value of the `casesense' parameter.
1398 */
H. Peter Anvin4db5a162007-10-11 13:42:09 -07001399static int mstrcmp(const char *p, const char *q, bool casesense)
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001400{
H. Peter Anvin734b1882002-04-30 21:01:08 +00001401 return casesense ? strcmp(p, q) : nasm_stricmp(p, q);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001402}
1403
1404/*
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07001405 * Compare a string to the name of an existing macro; this is a
1406 * simple wrapper which calls either strcmp or nasm_stricmp
1407 * depending on the value of the `casesense' parameter.
1408 */
1409static int mmemcmp(const char *p, const char *q, size_t l, bool casesense)
1410{
1411 return casesense ? memcmp(p, q, l) : nasm_memicmp(p, q, l);
1412}
1413
1414/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001415 * Return the Context structure associated with a %$ token. Return
1416 * NULL, having _already_ reported an error condition, if the
1417 * context stack isn't deep enough for the supplied number of $
1418 * signs.
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001419 * If all_contexts == true, contexts that enclose current are
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001420 * also scanned for such smacro, until it is found; if not -
1421 * only the context that directly results from the number of $'s
1422 * in variable's name.
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001423 *
1424 * If "namep" is non-NULL, set it to the pointer to the macro name
1425 * tail, i.e. the part beyond %$...
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001426 */
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001427static Context *get_ctx(const char *name, const char **namep,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001428 bool all_contexts)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001429{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001430 Context *ctx;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001431 SMacro *m;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001432 int i;
1433
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001434 if (namep)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001435 *namep = name;
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001436
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001437 if (!name || name[0] != '%' || name[1] != '$')
H. Peter Anvine2c80182005-01-15 22:15:51 +00001438 return NULL;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001439
H. Peter Anvine2c80182005-01-15 22:15:51 +00001440 if (!cstk) {
1441 error(ERR_NONFATAL, "`%s': context stack is empty", name);
1442 return NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001443 }
1444
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001445 name += 2;
1446 ctx = cstk;
1447 i = 0;
1448 while (ctx && *name == '$') {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001449 name++;
1450 i++;
1451 ctx = ctx->next;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001452 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001453 if (!ctx) {
1454 error(ERR_NONFATAL, "`%s': context stack is only"
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001455 " %d level%s deep", name, i, (i == 1 ? "" : "s"));
H. Peter Anvine2c80182005-01-15 22:15:51 +00001456 return NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001457 }
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001458
1459 if (namep)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001460 *namep = name;
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001461
Keith Kanios404589e2010-08-10 20:12:57 -05001462 if (!all_contexts)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001463 return ctx;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001464
Cyrill Gorcunovd90693c2010-08-11 20:31:46 +04001465 /*
1466 * NOTE: In 2.10 we will not need lookup in extarnal
1467 * contexts, so this is a gentle way to inform users
1468 * about their source code need to be updated
1469 */
1470
1471 /* first round -- check the current context */
1472 m = hash_findix(&ctx->localmac, name);
1473 while (m) {
1474 if (!mstrcmp(m->name, name, m->casesense))
1475 return ctx;
1476 m = m->next;
1477 }
1478
1479 /* second round - external contexts */
1480 while ((ctx = ctx->next)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001481 /* Search for this smacro in found context */
H. Peter Anvin166c2472008-05-28 12:28:58 -07001482 m = hash_findix(&ctx->localmac, name);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001483 while (m) {
Keith Kanios404589e2010-08-10 20:12:57 -05001484 if (!mstrcmp(m->name, name, m->casesense)) {
Keith Kanios88d947e2010-08-14 12:47:45 -05001485 /* NOTE: deprecated as of 2.10 */
Cyrill Gorcunovd90693c2010-08-11 20:31:46 +04001486 static int once = 0;
1487 if (!once) {
1488 error(ERR_WARNING, "context-local macro expansion"
Keith Kanios88d947e2010-08-14 12:47:45 -05001489 " fall-through (automatic searching of outer"
1490 " contexts) will be deprecated starting in"
1491 " NASM 2.10, please see the NASM Manual for"
1492 " more information");
Cyrill Gorcunovd90693c2010-08-11 20:31:46 +04001493 once = 1;
1494 }
Keith Kanios88d947e2010-08-14 12:47:45 -05001495 error(ERR_WARNING, "`%s': context-local macro expansion fall-through", name);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001496 return ctx;
Cyrill Gorcunovd90693c2010-08-11 20:31:46 +04001497 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001498 m = m->next;
1499 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00001500 }
Cyrill Gorcunovd90693c2010-08-11 20:31:46 +04001501
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001502 return NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001503}
1504
1505/*
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001506 * Check to see if a file is already in a string list
1507 */
1508static bool in_list(const StrList *list, const char *str)
1509{
1510 while (list) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001511 if (!strcmp(list->str, str))
1512 return true;
1513 list = list->next;
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001514 }
1515 return false;
1516}
1517
1518/*
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001519 * Open an include file. This routine must always return a valid
1520 * file pointer if it returns - it's responsible for throwing an
1521 * ERR_FATAL and bombing out completely if not. It should also try
1522 * the include path one by one until it finds the file or reaches
1523 * the end of the path.
1524 */
H. Peter Anvin2b1c3b92008-06-06 10:38:46 -07001525static FILE *inc_fopen(const char *file, StrList **dhead, StrList ***dtail,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001526 bool missing_ok)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001527{
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001528 FILE *fp;
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001529 char *prefix = "";
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001530 IncPath *ip = ipath;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001531 int len = strlen(file);
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001532 size_t prefix_len = 0;
1533 StrList *sl;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001534
H. Peter Anvine2c80182005-01-15 22:15:51 +00001535 while (1) {
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001536 sl = nasm_malloc(prefix_len+len+1+sizeof sl->next);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001537 memcpy(sl->str, prefix, prefix_len);
1538 memcpy(sl->str+prefix_len, file, len+1);
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001539 fp = fopen(sl->str, "r");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001540 if (fp && dhead && !in_list(*dhead, sl->str)) {
1541 sl->next = NULL;
1542 **dtail = sl;
1543 *dtail = &sl->next;
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001544 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001545 nasm_free(sl);
1546 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001547 if (fp)
1548 return fp;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001549 if (!ip) {
1550 if (!missing_ok)
1551 break;
1552 prefix = NULL;
1553 } else {
1554 prefix = ip->path;
1555 ip = ip->next;
1556 }
1557 if (prefix) {
1558 prefix_len = strlen(prefix);
1559 } else {
1560 /* -MG given and file not found */
1561 if (dhead && !in_list(*dhead, file)) {
1562 sl = nasm_malloc(len+1+sizeof sl->next);
1563 sl->next = NULL;
1564 strcpy(sl->str, file);
1565 **dtail = sl;
1566 *dtail = &sl->next;
1567 }
1568 return NULL;
1569 }
H. Peter Anvineba20a72002-04-30 20:53:55 +00001570 }
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001571
H. Peter Anvin734b1882002-04-30 21:01:08 +00001572 error(ERR_FATAL, "unable to open include file `%s'", file);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001573 return NULL;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001574}
1575
1576/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001577 * Determine if we should warn on defining a single-line macro of
H. Peter Anvinef7468f2002-04-30 20:57:59 +00001578 * name `name', with `nparam' parameters. If nparam is 0 or -1, will
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001579 * return true if _any_ single-line macro of that name is defined.
1580 * Otherwise, will return true if a single-line macro with either
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001581 * `nparam' or no parameters is defined.
1582 *
1583 * If a macro with precisely the right number of parameters is
H. Peter Anvinef7468f2002-04-30 20:57:59 +00001584 * defined, or nparam is -1, the address of the definition structure
1585 * will be returned in `defn'; otherwise NULL will be returned. If `defn'
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001586 * is NULL, no action will be taken regarding its contents, and no
1587 * error will occur.
1588 *
1589 * Note that this is also called with nparam zero to resolve
1590 * `ifdef'.
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001591 *
1592 * If you already know which context macro belongs to, you can pass
1593 * the context pointer as first parameter; if you won't but name begins
1594 * with %$ the context will be automatically computed. If all_contexts
1595 * is true, macro will be searched in outer contexts as well.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001596 */
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07001597static bool
H. Peter Anvinb2a5fda2008-06-19 21:42:42 -07001598smacro_defined(Context * ctx, const char *name, int nparam, SMacro ** defn,
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07001599 bool nocase)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001600{
H. Peter Anvin166c2472008-05-28 12:28:58 -07001601 struct hash_table *smtbl;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001602 SMacro *m;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001603
H. Peter Anvin97a23472007-09-16 17:57:25 -07001604 if (ctx) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001605 smtbl = &ctx->localmac;
H. Peter Anvin97a23472007-09-16 17:57:25 -07001606 } else if (name[0] == '%' && name[1] == '$') {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001607 if (cstk)
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001608 ctx = get_ctx(name, &name, false);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001609 if (!ctx)
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001610 return false; /* got to return _something_ */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001611 smtbl = &ctx->localmac;
H. Peter Anvin97a23472007-09-16 17:57:25 -07001612 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001613 smtbl = &smacros;
H. Peter Anvin97a23472007-09-16 17:57:25 -07001614 }
H. Peter Anvin166c2472008-05-28 12:28:58 -07001615 m = (SMacro *) hash_findix(smtbl, name);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001616
H. Peter Anvine2c80182005-01-15 22:15:51 +00001617 while (m) {
1618 if (!mstrcmp(m->name, name, m->casesense && nocase) &&
Charles Crayne192d5b52007-10-18 19:02:42 -07001619 (nparam <= 0 || m->nparam == 0 || nparam == (int) m->nparam)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001620 if (defn) {
Charles Crayne192d5b52007-10-18 19:02:42 -07001621 if (nparam == (int) m->nparam || nparam == -1)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001622 *defn = m;
1623 else
1624 *defn = NULL;
1625 }
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001626 return true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001627 }
1628 m = m->next;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001629 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001630
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001631 return false;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001632}
1633
1634/*
1635 * Count and mark off the parameters in a multi-line macro call.
1636 * This is called both from within the multi-line macro expansion
1637 * code, and also to mark off the default parameters when provided
1638 * in a %macro definition line.
1639 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001640static void count_mmac_params(Token * t, int *nparam, Token *** params)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001641{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001642 int paramsize, brace;
1643
1644 *nparam = paramsize = 0;
1645 *params = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001646 while (t) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001647 /* +1: we need space for the final NULL */
H. Peter Anvin91fb6f12008-09-01 10:56:33 -07001648 if (*nparam+1 >= paramsize) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001649 paramsize += PARAM_DELTA;
1650 *params = nasm_realloc(*params, sizeof(**params) * paramsize);
1651 }
1652 skip_white_(t);
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001653 brace = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001654 if (tok_is_(t, "{"))
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001655 brace = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001656 (*params)[(*nparam)++] = t;
1657 while (tok_isnt_(t, brace ? "}" : ","))
1658 t = t->next;
1659 if (t) { /* got a comma/brace */
1660 t = t->next;
1661 if (brace) {
1662 /*
1663 * Now we've found the closing brace, look further
1664 * for the comma.
1665 */
1666 skip_white_(t);
1667 if (tok_isnt_(t, ",")) {
1668 error(ERR_NONFATAL,
1669 "braces do not enclose all of macro parameter");
1670 while (tok_isnt_(t, ","))
1671 t = t->next;
1672 }
1673 if (t)
1674 t = t->next; /* eat the comma */
1675 }
1676 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001677 }
1678}
1679
1680/*
H. Peter Anvin76690a12002-04-30 20:52:49 +00001681 * Determine whether one of the various `if' conditions is true or
1682 * not.
1683 *
1684 * We must free the tline we get passed.
1685 */
H. Peter Anvin70055962007-10-11 00:05:31 -07001686static bool if_condition(Token * tline, enum preproc_token ct)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001687{
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001688 enum pp_conditional i = PP_COND(ct);
H. Peter Anvin70055962007-10-11 00:05:31 -07001689 bool j;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001690 Token *t, *tt, **tptr, *origline;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001691 struct tokenval tokval;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001692 expr *evalresult;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001693 enum pp_token_type needtype;
H. Peter Anvin077fb932010-07-20 14:56:30 -07001694 char *p;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001695
1696 origline = tline;
1697
H. Peter Anvine2c80182005-01-15 22:15:51 +00001698 switch (i) {
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001699 case PPC_IFCTX:
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001700 j = false; /* have we matched yet? */
Victor van den Elzen0e857f12008-07-23 13:21:29 +02001701 while (true) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001702 skip_white_(tline);
Victor van den Elzen0e857f12008-07-23 13:21:29 +02001703 if (!tline)
1704 break;
1705 if (tline->type != TOK_ID) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001706 error(ERR_NONFATAL,
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001707 "`%s' expects context identifiers", pp_directives[ct]);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001708 free_tlist(origline);
1709 return -1;
1710 }
Victor van den Elzen0e857f12008-07-23 13:21:29 +02001711 if (cstk && cstk->name && !nasm_stricmp(tline->text, cstk->name))
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001712 j = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001713 tline = tline->next;
1714 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001715 break;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001716
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001717 case PPC_IFDEF:
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001718 j = false; /* have we matched yet? */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001719 while (tline) {
1720 skip_white_(tline);
1721 if (!tline || (tline->type != TOK_ID &&
1722 (tline->type != TOK_PREPROC_ID ||
1723 tline->text[1] != '$'))) {
1724 error(ERR_NONFATAL,
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001725 "`%s' expects macro identifiers", pp_directives[ct]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001726 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001727 }
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07001728 if (smacro_defined(NULL, tline->text, 0, NULL, true))
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001729 j = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001730 tline = tline->next;
1731 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001732 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001733
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001734 case PPC_IFENV:
1735 tline = expand_smacro(tline);
1736 j = false; /* have we matched yet? */
1737 while (tline) {
1738 skip_white_(tline);
1739 if (!tline || (tline->type != TOK_ID &&
H. Peter Anvin077fb932010-07-20 14:56:30 -07001740 tline->type != TOK_STRING &&
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001741 (tline->type != TOK_PREPROC_ID ||
H. Peter Anvin077fb932010-07-20 14:56:30 -07001742 tline->text[1] != '!'))) {
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001743 error(ERR_NONFATAL,
1744 "`%s' expects environment variable names",
1745 pp_directives[ct]);
1746 goto fail;
1747 }
H. Peter Anvin077fb932010-07-20 14:56:30 -07001748 p = tline->text;
1749 if (tline->type == TOK_PREPROC_ID)
1750 p += 2; /* Skip leading %! */
1751 if (*p == '\'' || *p == '\"' || *p == '`')
1752 nasm_unquote_cstr(p, ct);
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001753 if (getenv(p))
1754 j = true;
1755 tline = tline->next;
1756 }
1757 break;
1758
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001759 case PPC_IFIDN:
1760 case PPC_IFIDNI:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001761 tline = expand_smacro(tline);
1762 t = tt = tline;
1763 while (tok_isnt_(tt, ","))
1764 tt = tt->next;
1765 if (!tt) {
1766 error(ERR_NONFATAL,
1767 "`%s' expects two comma-separated arguments",
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001768 pp_directives[ct]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001769 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001770 }
1771 tt = tt->next;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001772 j = true; /* assume equality unless proved not */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001773 while ((t->type != TOK_OTHER || strcmp(t->text, ",")) && tt) {
1774 if (tt->type == TOK_OTHER && !strcmp(tt->text, ",")) {
1775 error(ERR_NONFATAL, "`%s': more than one comma on line",
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001776 pp_directives[ct]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001777 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001778 }
1779 if (t->type == TOK_WHITESPACE) {
1780 t = t->next;
1781 continue;
1782 }
1783 if (tt->type == TOK_WHITESPACE) {
1784 tt = tt->next;
1785 continue;
1786 }
1787 if (tt->type != t->type) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001788 j = false; /* found mismatching tokens */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001789 break;
1790 }
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07001791 /* When comparing strings, need to unquote them first */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001792 if (t->type == TOK_STRING) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001793 size_t l1 = nasm_unquote(t->text, NULL);
1794 size_t l2 = nasm_unquote(tt->text, NULL);
H. Peter Anvind2456592008-06-19 15:04:18 -07001795
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001796 if (l1 != l2) {
1797 j = false;
1798 break;
1799 }
1800 if (mmemcmp(t->text, tt->text, l1, i == PPC_IFIDN)) {
1801 j = false;
1802 break;
1803 }
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07001804 } else if (mstrcmp(tt->text, t->text, i == PPC_IFIDN) != 0) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001805 j = false; /* found mismatching tokens */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001806 break;
1807 }
Nickolay Yurchenkof3b3ce22003-09-21 20:38:43 +00001808
H. Peter Anvine2c80182005-01-15 22:15:51 +00001809 t = t->next;
1810 tt = tt->next;
1811 }
1812 if ((t->type != TOK_OTHER || strcmp(t->text, ",")) || tt)
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001813 j = false; /* trailing gunk on one end or other */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001814 break;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001815
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001816 case PPC_IFMACRO:
H. Peter Anvin89cee572009-07-15 09:16:54 -04001817 {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001818 bool found = false;
1819 MMacro searching, *mmac;
H. Peter Anvin65747262002-05-07 00:10:05 +00001820
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001821 skip_white_(tline);
1822 tline = expand_id(tline);
1823 if (!tok_type_(tline, TOK_ID)) {
1824 error(ERR_NONFATAL,
1825 "`%s' expects a macro name", pp_directives[ct]);
1826 goto fail;
1827 }
1828 searching.name = nasm_strdup(tline->text);
1829 searching.casesense = true;
1830 searching.plus = false;
1831 searching.nolist = false;
1832 searching.in_progress = 0;
1833 searching.max_depth = 0;
1834 searching.rep_nest = NULL;
1835 searching.nparam_min = 0;
1836 searching.nparam_max = INT_MAX;
1837 tline = expand_smacro(tline->next);
1838 skip_white_(tline);
1839 if (!tline) {
1840 } else if (!tok_type_(tline, TOK_NUMBER)) {
1841 error(ERR_NONFATAL,
1842 "`%s' expects a parameter count or nothing",
1843 pp_directives[ct]);
1844 } else {
1845 searching.nparam_min = searching.nparam_max =
1846 readnum(tline->text, &j);
1847 if (j)
1848 error(ERR_NONFATAL,
1849 "unable to parse parameter count `%s'",
1850 tline->text);
1851 }
1852 if (tline && tok_is_(tline->next, "-")) {
1853 tline = tline->next->next;
1854 if (tok_is_(tline, "*"))
1855 searching.nparam_max = INT_MAX;
1856 else if (!tok_type_(tline, TOK_NUMBER))
1857 error(ERR_NONFATAL,
1858 "`%s' expects a parameter count after `-'",
1859 pp_directives[ct]);
1860 else {
1861 searching.nparam_max = readnum(tline->text, &j);
1862 if (j)
1863 error(ERR_NONFATAL,
1864 "unable to parse parameter count `%s'",
1865 tline->text);
1866 if (searching.nparam_min > searching.nparam_max)
1867 error(ERR_NONFATAL,
1868 "minimum parameter count exceeds maximum");
1869 }
1870 }
1871 if (tline && tok_is_(tline->next, "+")) {
1872 tline = tline->next;
1873 searching.plus = true;
1874 }
1875 mmac = (MMacro *) hash_findix(&mmacros, searching.name);
1876 while (mmac) {
1877 if (!strcmp(mmac->name, searching.name) &&
1878 (mmac->nparam_min <= searching.nparam_max
1879 || searching.plus)
1880 && (searching.nparam_min <= mmac->nparam_max
1881 || mmac->plus)) {
1882 found = true;
1883 break;
1884 }
1885 mmac = mmac->next;
1886 }
1887 if (tline && tline->next)
1888 error(ERR_WARNING|ERR_PASS1,
1889 "trailing garbage after %%ifmacro ignored");
1890 nasm_free(searching.name);
1891 j = found;
1892 break;
H. Peter Anvin89cee572009-07-15 09:16:54 -04001893 }
H. Peter Anvin65747262002-05-07 00:10:05 +00001894
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001895 case PPC_IFID:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001896 needtype = TOK_ID;
1897 goto iftype;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001898 case PPC_IFNUM:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001899 needtype = TOK_NUMBER;
1900 goto iftype;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001901 case PPC_IFSTR:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001902 needtype = TOK_STRING;
1903 goto iftype;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001904
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001905iftype:
1906 t = tline = expand_smacro(tline);
H. Peter Anvind85d2502008-05-04 17:53:31 -07001907
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001908 while (tok_type_(t, TOK_WHITESPACE) ||
1909 (needtype == TOK_NUMBER &&
1910 tok_type_(t, TOK_OTHER) &&
1911 (t->text[0] == '-' || t->text[0] == '+') &&
1912 !t->text[1]))
1913 t = t->next;
H. Peter Anvind85d2502008-05-04 17:53:31 -07001914
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001915 j = tok_type_(t, needtype);
1916 break;
H. Peter Anvincbf768d2008-02-16 16:41:25 -08001917
1918 case PPC_IFTOKEN:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001919 t = tline = expand_smacro(tline);
1920 while (tok_type_(t, TOK_WHITESPACE))
1921 t = t->next;
H. Peter Anvincbf768d2008-02-16 16:41:25 -08001922
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001923 j = false;
1924 if (t) {
1925 t = t->next; /* Skip the actual token */
1926 while (tok_type_(t, TOK_WHITESPACE))
1927 t = t->next;
1928 j = !t; /* Should be nothing left */
1929 }
1930 break;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001931
H. Peter Anvin134b9462008-02-16 17:01:40 -08001932 case PPC_IFEMPTY:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001933 t = tline = expand_smacro(tline);
1934 while (tok_type_(t, TOK_WHITESPACE))
1935 t = t->next;
H. Peter Anvin134b9462008-02-16 17:01:40 -08001936
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001937 j = !t; /* Should be empty */
1938 break;
H. Peter Anvin134b9462008-02-16 17:01:40 -08001939
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001940 case PPC_IF:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001941 t = tline = expand_smacro(tline);
1942 tptr = &t;
1943 tokval.t_type = TOKEN_INVALID;
1944 evalresult = evaluate(ppscan, tptr, &tokval,
1945 NULL, pass | CRITICAL, error, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001946 if (!evalresult)
1947 return -1;
1948 if (tokval.t_type)
H. Peter Anvin917a3492008-09-24 09:14:49 -07001949 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +00001950 "trailing garbage after expression ignored");
1951 if (!is_simple(evalresult)) {
1952 error(ERR_NONFATAL,
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001953 "non-constant value given to `%s'", pp_directives[ct]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001954 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001955 }
Chuck Crayne60ae75d2007-05-02 01:59:16 +00001956 j = reloc_value(evalresult) != 0;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001957 break;
H. Peter Anvin95e28822007-09-12 04:20:08 +00001958
H. Peter Anvine2c80182005-01-15 22:15:51 +00001959 default:
1960 error(ERR_FATAL,
1961 "preprocessor directive `%s' not yet implemented",
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001962 pp_directives[ct]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001963 goto fail;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001964 }
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001965
1966 free_tlist(origline);
1967 return j ^ PP_NEGATIVE(ct);
H. Peter Anvin70653092007-10-19 14:42:29 -07001968
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001969fail:
1970 free_tlist(origline);
1971 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001972}
1973
1974/*
H. Peter Anvin4db5a162007-10-11 13:42:09 -07001975 * Common code for defining an smacro
1976 */
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001977static bool define_smacro(Context *ctx, const char *mname, bool casesense,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001978 int nparam, Token *expansion)
H. Peter Anvin4db5a162007-10-11 13:42:09 -07001979{
1980 SMacro *smac, **smhead;
H. Peter Anvin166c2472008-05-28 12:28:58 -07001981 struct hash_table *smtbl;
H. Peter Anvin70653092007-10-19 14:42:29 -07001982
H. Peter Anvin4db5a162007-10-11 13:42:09 -07001983 if (smacro_defined(ctx, mname, nparam, &smac, casesense)) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001984 if (!smac) {
1985 error(ERR_WARNING|ERR_PASS1,
1986 "single-line macro `%s' defined both with and"
1987 " without parameters", mname);
1988 /*
1989 * Some instances of the old code considered this a failure,
1990 * some others didn't. What is the right thing to do here?
1991 */
1992 free_tlist(expansion);
1993 return false; /* Failure */
1994 } else {
1995 /*
1996 * We're redefining, so we have to take over an
1997 * existing SMacro structure. This means freeing
1998 * what was already in it.
1999 */
2000 nasm_free(smac->name);
2001 free_tlist(smac->expansion);
2002 }
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002003 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002004 smtbl = ctx ? &ctx->localmac : &smacros;
2005 smhead = (SMacro **) hash_findi_add(smtbl, mname);
2006 smac = nasm_malloc(sizeof(SMacro));
2007 smac->next = *smhead;
2008 *smhead = smac;
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002009 }
2010 smac->name = nasm_strdup(mname);
2011 smac->casesense = casesense;
2012 smac->nparam = nparam;
2013 smac->expansion = expansion;
2014 smac->in_progress = false;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002015 return true; /* Success */
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002016}
2017
2018/*
2019 * Undefine an smacro
2020 */
2021static void undef_smacro(Context *ctx, const char *mname)
2022{
2023 SMacro **smhead, *s, **sp;
H. Peter Anvin166c2472008-05-28 12:28:58 -07002024 struct hash_table *smtbl;
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002025
H. Peter Anvin166c2472008-05-28 12:28:58 -07002026 smtbl = ctx ? &ctx->localmac : &smacros;
2027 smhead = (SMacro **)hash_findi(smtbl, mname, NULL);
H. Peter Anvin70653092007-10-19 14:42:29 -07002028
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002029 if (smhead) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002030 /*
2031 * We now have a macro name... go hunt for it.
2032 */
2033 sp = smhead;
2034 while ((s = *sp) != NULL) {
2035 if (!mstrcmp(s->name, mname, s->casesense)) {
2036 *sp = s->next;
2037 nasm_free(s->name);
2038 free_tlist(s->expansion);
2039 nasm_free(s);
2040 } else {
2041 sp = &s->next;
2042 }
2043 }
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002044 }
2045}
2046
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002047/*
H. Peter Anvina26433d2008-07-16 14:40:01 -07002048 * Parse a mmacro specification.
2049 */
2050static bool parse_mmacro_spec(Token *tline, MMacro *def, const char *directive)
2051{
2052 bool err;
2053
2054 tline = tline->next;
2055 skip_white_(tline);
2056 tline = expand_id(tline);
2057 if (!tok_type_(tline, TOK_ID)) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002058 error(ERR_NONFATAL, "`%s' expects a macro name", directive);
2059 return false;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002060 }
Victor van den Elzenb916ede2008-07-23 15:14:22 +02002061
H. Peter Anvin89cee572009-07-15 09:16:54 -04002062 def->prev = NULL;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002063 def->name = nasm_strdup(tline->text);
2064 def->plus = false;
2065 def->nolist = false;
2066 def->in_progress = 0;
2067 def->rep_nest = NULL;
Victor van den Elzenb916ede2008-07-23 15:14:22 +02002068 def->nparam_min = 0;
2069 def->nparam_max = 0;
2070
H. Peter Anvina26433d2008-07-16 14:40:01 -07002071 tline = expand_smacro(tline->next);
2072 skip_white_(tline);
2073 if (!tok_type_(tline, TOK_NUMBER)) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002074 error(ERR_NONFATAL, "`%s' expects a parameter count", directive);
H. Peter Anvina26433d2008-07-16 14:40:01 -07002075 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002076 def->nparam_min = def->nparam_max =
2077 readnum(tline->text, &err);
2078 if (err)
2079 error(ERR_NONFATAL,
2080 "unable to parse parameter count `%s'", tline->text);
H. Peter Anvina26433d2008-07-16 14:40:01 -07002081 }
2082 if (tline && tok_is_(tline->next, "-")) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002083 tline = tline->next->next;
2084 if (tok_is_(tline, "*")) {
2085 def->nparam_max = INT_MAX;
2086 } else if (!tok_type_(tline, TOK_NUMBER)) {
2087 error(ERR_NONFATAL,
2088 "`%s' expects a parameter count after `-'", directive);
2089 } else {
2090 def->nparam_max = readnum(tline->text, &err);
2091 if (err) {
2092 error(ERR_NONFATAL, "unable to parse parameter count `%s'",
2093 tline->text);
2094 }
2095 if (def->nparam_min > def->nparam_max) {
2096 error(ERR_NONFATAL, "minimum parameter count exceeds maximum");
2097 }
2098 }
H. Peter Anvina26433d2008-07-16 14:40:01 -07002099 }
2100 if (tline && tok_is_(tline->next, "+")) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002101 tline = tline->next;
2102 def->plus = true;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002103 }
2104 if (tline && tok_type_(tline->next, TOK_ID) &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002105 !nasm_stricmp(tline->next->text, ".nolist")) {
2106 tline = tline->next;
2107 def->nolist = true;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002108 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002109
H. Peter Anvina26433d2008-07-16 14:40:01 -07002110 /*
2111 * Handle default parameters.
2112 */
2113 if (tline && tline->next) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002114 def->dlist = tline->next;
2115 tline->next = NULL;
2116 count_mmac_params(def->dlist, &def->ndefs, &def->defaults);
H. Peter Anvina26433d2008-07-16 14:40:01 -07002117 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002118 def->dlist = NULL;
2119 def->defaults = NULL;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002120 }
2121 def->expansion = NULL;
2122
H. Peter Anvin89cee572009-07-15 09:16:54 -04002123 if (def->defaults && def->ndefs > def->nparam_max - def->nparam_min &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002124 !def->plus)
2125 error(ERR_WARNING|ERR_PASS1|ERR_WARN_MDP,
2126 "too many default macro parameters");
Victor van den Elzenb916ede2008-07-23 15:14:22 +02002127
H. Peter Anvina26433d2008-07-16 14:40:01 -07002128 return true;
2129}
2130
2131
2132/*
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002133 * Decode a size directive
2134 */
2135static int parse_size(const char *str) {
2136 static const char *size_names[] =
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002137 { "byte", "dword", "oword", "qword", "tword", "word", "yword" };
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002138 static const int sizes[] =
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002139 { 0, 1, 4, 16, 8, 10, 2, 32 };
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002140
Cyrill Gorcunova7319242010-06-03 22:04:36 +04002141 return sizes[bsii(str, size_names, ARRAY_SIZE(size_names))+1];
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002142}
2143
Ed Beroset3ab3f412002-06-11 03:31:49 +00002144/**
2145 * find and process preprocessor directive in passed line
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002146 * Find out if a line contains a preprocessor directive, and deal
2147 * with it if so.
H. Peter Anvin70653092007-10-19 14:42:29 -07002148 *
Ed Beroset3ab3f412002-06-11 03:31:49 +00002149 * If a directive _is_ found, it is the responsibility of this routine
2150 * (and not the caller) to free_tlist() the line.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002151 *
Ed Beroset3ab3f412002-06-11 03:31:49 +00002152 * @param tline a pointer to the current tokeninzed line linked list
2153 * @return DIRECTIVE_FOUND or NO_DIRECTIVE_FOUND
H. Peter Anvin70653092007-10-19 14:42:29 -07002154 *
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002155 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002156static int do_directive(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00002157{
H. Peter Anvin4169a472007-09-12 01:29:43 +00002158 enum preproc_token i;
2159 int j;
H. Peter Anvin70055962007-10-11 00:05:31 -07002160 bool err;
2161 int nparam;
2162 bool nolist;
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07002163 bool casesense;
H. Peter Anvin8cfdb9d2007-09-14 18:36:01 -07002164 int k, m;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002165 int offset;
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08002166 char *p, *pp;
2167 const char *mname;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002168 Include *inc;
2169 Context *ctx;
2170 Cond *cond;
H. Peter Anvin97a23472007-09-16 17:57:25 -07002171 MMacro *mmac, **mmhead;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002172 Token *t, *tt, *param_start, *macro_start, *last, **tptr, *origline;
2173 Line *l;
2174 struct tokenval tokval;
2175 expr *evalresult;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002176 MMacro *tmp_defining; /* Used when manipulating rep_nest */
H. Peter Anvinf8ba53e2007-10-11 10:11:57 -07002177 int64_t count;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07002178 size_t len;
H. Peter Anvin8e3f75e2008-09-24 00:21:58 -07002179 int severity;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002180
2181 origline = tline;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002182
H. Peter Anvineba20a72002-04-30 20:53:55 +00002183 skip_white_(tline);
H. Peter Anvinf2936d72008-06-04 15:11:23 -07002184 if (!tline || !tok_type_(tline, TOK_PREPROC_ID) ||
H. Peter Anvine2c80182005-01-15 22:15:51 +00002185 (tline->text[1] == '%' || tline->text[1] == '$'
2186 || tline->text[1] == '!'))
2187 return NO_DIRECTIVE_FOUND;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002188
H. Peter Anvin4169a472007-09-12 01:29:43 +00002189 i = pp_token_hash(tline->text);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002190
2191 /*
Cyrill Gorcunovf09116f2010-02-28 11:52:53 +03002192 * FIXME: We zap execution of PP_RMACRO, PP_IRMACRO, PP_EXITMACRO
2193 * since they are known to be buggy at moment, we need to fix them
2194 * in future release (2.09-2.10)
2195 */
2196 if (i == PP_RMACRO || i == PP_RMACRO || i == PP_EXITMACRO) {
2197 error(ERR_NONFATAL, "unknown preprocessor directive `%s'",
2198 tline->text);
2199 return NO_DIRECTIVE_FOUND;
2200 }
2201
2202 /*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002203 * If we're in a non-emitting branch of a condition construct,
H. Peter Anvin76690a12002-04-30 20:52:49 +00002204 * or walking to the end of an already terminated %rep block,
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002205 * we should ignore all directives except for condition
2206 * directives.
2207 */
H. Peter Anvin76690a12002-04-30 20:52:49 +00002208 if (((istk->conds && !emitting(istk->conds->state)) ||
H. Peter Anvine2c80182005-01-15 22:15:51 +00002209 (istk->mstk && !istk->mstk->in_progress)) && !is_condition(i)) {
2210 return NO_DIRECTIVE_FOUND;
H. Peter Anvineba20a72002-04-30 20:53:55 +00002211 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002212
2213 /*
H. Peter Anvin76690a12002-04-30 20:52:49 +00002214 * If we're defining a macro or reading a %rep block, we should
Victor van den Elzen8f1120f2008-07-16 13:41:37 +02002215 * ignore all directives except for %macro/%imacro (which nest),
2216 * %endm/%endmacro, and (only if we're in a %rep block) %endrep.
2217 * If we're in a %rep block, another %rep nests, so should be let through.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002218 */
2219 if (defining && i != PP_MACRO && i != PP_IMACRO &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002220 i != PP_RMACRO && i != PP_IRMACRO &&
H. Peter Anvine2c80182005-01-15 22:15:51 +00002221 i != PP_ENDMACRO && i != PP_ENDM &&
2222 (defining->name || (i != PP_ENDREP && i != PP_REP))) {
2223 return NO_DIRECTIVE_FOUND;
H. Peter Anvineba20a72002-04-30 20:53:55 +00002224 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002225
Charles Crayned4200be2008-07-12 16:42:33 -07002226 if (defining) {
Keith Kanios852f1ee2009-07-12 00:19:55 -05002227 if (i == PP_MACRO || i == PP_IMACRO ||
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002228 i == PP_RMACRO || i == PP_IRMACRO) {
Charles Crayned4200be2008-07-12 16:42:33 -07002229 nested_mac_count++;
2230 return NO_DIRECTIVE_FOUND;
2231 } else if (nested_mac_count > 0) {
2232 if (i == PP_ENDMACRO) {
2233 nested_mac_count--;
2234 return NO_DIRECTIVE_FOUND;
2235 }
2236 }
2237 if (!defining->name) {
2238 if (i == PP_REP) {
2239 nested_rep_count++;
2240 return NO_DIRECTIVE_FOUND;
2241 } else if (nested_rep_count > 0) {
2242 if (i == PP_ENDREP) {
2243 nested_rep_count--;
2244 return NO_DIRECTIVE_FOUND;
2245 }
2246 }
2247 }
2248 }
2249
H. Peter Anvin4169a472007-09-12 01:29:43 +00002250 switch (i) {
2251 case PP_INVALID:
H. Peter Anvine2c80182005-01-15 22:15:51 +00002252 error(ERR_NONFATAL, "unknown preprocessor directive `%s'",
2253 tline->text);
2254 return NO_DIRECTIVE_FOUND; /* didn't get it */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002255
H. Peter Anvine2c80182005-01-15 22:15:51 +00002256 case PP_STACKSIZE:
2257 /* Directive to tell NASM what the default stack size is. The
2258 * default is for a 16-bit stack, and this can be overriden with
2259 * %stacksize large.
H. Peter Anvine2c80182005-01-15 22:15:51 +00002260 */
2261 tline = tline->next;
2262 if (tline && tline->type == TOK_WHITESPACE)
2263 tline = tline->next;
2264 if (!tline || tline->type != TOK_ID) {
2265 error(ERR_NONFATAL, "`%%stacksize' missing size parameter");
2266 free_tlist(origline);
2267 return DIRECTIVE_FOUND;
2268 }
2269 if (nasm_stricmp(tline->text, "flat") == 0) {
2270 /* All subsequent ARG directives are for a 32-bit stack */
2271 StackSize = 4;
2272 StackPointer = "ebp";
2273 ArgOffset = 8;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002274 LocalOffset = 0;
Charles Crayne7eaf9192007-11-08 22:11:14 -08002275 } else if (nasm_stricmp(tline->text, "flat64") == 0) {
2276 /* All subsequent ARG directives are for a 64-bit stack */
2277 StackSize = 8;
2278 StackPointer = "rbp";
Per Jessen53252e02010-02-11 00:16:59 +03002279 ArgOffset = 16;
Charles Crayne7eaf9192007-11-08 22:11:14 -08002280 LocalOffset = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002281 } else if (nasm_stricmp(tline->text, "large") == 0) {
2282 /* All subsequent ARG directives are for a 16-bit stack,
2283 * far function call.
2284 */
2285 StackSize = 2;
2286 StackPointer = "bp";
2287 ArgOffset = 4;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002288 LocalOffset = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002289 } else if (nasm_stricmp(tline->text, "small") == 0) {
2290 /* All subsequent ARG directives are for a 16-bit stack,
2291 * far function call. We don't support near functions.
2292 */
2293 StackSize = 2;
2294 StackPointer = "bp";
2295 ArgOffset = 6;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002296 LocalOffset = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002297 } else {
2298 error(ERR_NONFATAL, "`%%stacksize' invalid size type");
2299 free_tlist(origline);
2300 return DIRECTIVE_FOUND;
2301 }
2302 free_tlist(origline);
2303 return DIRECTIVE_FOUND;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002304
H. Peter Anvine2c80182005-01-15 22:15:51 +00002305 case PP_ARG:
2306 /* TASM like ARG directive to define arguments to functions, in
2307 * the following form:
2308 *
2309 * ARG arg1:WORD, arg2:DWORD, arg4:QWORD
2310 */
2311 offset = ArgOffset;
2312 do {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00002313 char *arg, directive[256];
H. Peter Anvine2c80182005-01-15 22:15:51 +00002314 int size = StackSize;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002315
H. Peter Anvine2c80182005-01-15 22:15:51 +00002316 /* Find the argument name */
2317 tline = tline->next;
2318 if (tline && tline->type == TOK_WHITESPACE)
2319 tline = tline->next;
2320 if (!tline || tline->type != TOK_ID) {
2321 error(ERR_NONFATAL, "`%%arg' missing argument parameter");
2322 free_tlist(origline);
2323 return DIRECTIVE_FOUND;
2324 }
2325 arg = tline->text;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002326
H. Peter Anvine2c80182005-01-15 22:15:51 +00002327 /* Find the argument size type */
2328 tline = tline->next;
2329 if (!tline || tline->type != TOK_OTHER
2330 || tline->text[0] != ':') {
2331 error(ERR_NONFATAL,
2332 "Syntax error processing `%%arg' directive");
2333 free_tlist(origline);
2334 return DIRECTIVE_FOUND;
2335 }
2336 tline = tline->next;
2337 if (!tline || tline->type != TOK_ID) {
2338 error(ERR_NONFATAL, "`%%arg' missing size type parameter");
2339 free_tlist(origline);
2340 return DIRECTIVE_FOUND;
2341 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002342
H. Peter Anvine2c80182005-01-15 22:15:51 +00002343 /* Allow macro expansion of type parameter */
Keith Kaniosb7a89542007-04-12 02:40:54 +00002344 tt = tokenize(tline->text);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002345 tt = expand_smacro(tt);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002346 size = parse_size(tt->text);
2347 if (!size) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002348 error(ERR_NONFATAL,
2349 "Invalid size type for `%%arg' missing directive");
2350 free_tlist(tt);
2351 free_tlist(origline);
2352 return DIRECTIVE_FOUND;
2353 }
2354 free_tlist(tt);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002355
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002356 /* Round up to even stack slots */
2357 size = ALIGN(size, StackSize);
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002358
H. Peter Anvine2c80182005-01-15 22:15:51 +00002359 /* Now define the macro for the argument */
2360 snprintf(directive, sizeof(directive), "%%define %s (%s+%d)",
2361 arg, StackPointer, offset);
Keith Kaniosb7a89542007-04-12 02:40:54 +00002362 do_directive(tokenize(directive));
H. Peter Anvine2c80182005-01-15 22:15:51 +00002363 offset += size;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002364
H. Peter Anvine2c80182005-01-15 22:15:51 +00002365 /* Move to the next argument in the list */
2366 tline = tline->next;
2367 if (tline && tline->type == TOK_WHITESPACE)
2368 tline = tline->next;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002369 } while (tline && tline->type == TOK_OTHER && tline->text[0] == ',');
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002370 ArgOffset = offset;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002371 free_tlist(origline);
2372 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002373
H. Peter Anvine2c80182005-01-15 22:15:51 +00002374 case PP_LOCAL:
2375 /* TASM like LOCAL directive to define local variables for a
2376 * function, in the following form:
2377 *
2378 * LOCAL local1:WORD, local2:DWORD, local4:QWORD = LocalSize
2379 *
2380 * The '= LocalSize' at the end is ignored by NASM, but is
2381 * required by TASM to define the local parameter size (and used
2382 * by the TASM macro package).
2383 */
2384 offset = LocalOffset;
2385 do {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00002386 char *local, directive[256];
H. Peter Anvine2c80182005-01-15 22:15:51 +00002387 int size = StackSize;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002388
H. Peter Anvine2c80182005-01-15 22:15:51 +00002389 /* Find the argument name */
2390 tline = tline->next;
2391 if (tline && tline->type == TOK_WHITESPACE)
2392 tline = tline->next;
2393 if (!tline || tline->type != TOK_ID) {
2394 error(ERR_NONFATAL,
2395 "`%%local' missing argument parameter");
2396 free_tlist(origline);
2397 return DIRECTIVE_FOUND;
2398 }
2399 local = tline->text;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002400
H. Peter Anvine2c80182005-01-15 22:15:51 +00002401 /* Find the argument size type */
2402 tline = tline->next;
2403 if (!tline || tline->type != TOK_OTHER
2404 || tline->text[0] != ':') {
2405 error(ERR_NONFATAL,
2406 "Syntax error processing `%%local' directive");
2407 free_tlist(origline);
2408 return DIRECTIVE_FOUND;
2409 }
2410 tline = tline->next;
2411 if (!tline || tline->type != TOK_ID) {
2412 error(ERR_NONFATAL,
2413 "`%%local' missing size type parameter");
2414 free_tlist(origline);
2415 return DIRECTIVE_FOUND;
2416 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00002417
H. Peter Anvine2c80182005-01-15 22:15:51 +00002418 /* Allow macro expansion of type parameter */
Keith Kaniosb7a89542007-04-12 02:40:54 +00002419 tt = tokenize(tline->text);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002420 tt = expand_smacro(tt);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002421 size = parse_size(tt->text);
2422 if (!size) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002423 error(ERR_NONFATAL,
2424 "Invalid size type for `%%local' missing directive");
2425 free_tlist(tt);
2426 free_tlist(origline);
2427 return DIRECTIVE_FOUND;
2428 }
2429 free_tlist(tt);
H. Peter Anvin734b1882002-04-30 21:01:08 +00002430
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002431 /* Round up to even stack slots */
2432 size = ALIGN(size, StackSize);
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002433
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002434 offset += size; /* Negative offset, increment before */
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002435
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002436 /* Now define the macro for the argument */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002437 snprintf(directive, sizeof(directive), "%%define %s (%s-%d)",
2438 local, StackPointer, offset);
Keith Kaniosb7a89542007-04-12 02:40:54 +00002439 do_directive(tokenize(directive));
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002440
H. Peter Anvine2c80182005-01-15 22:15:51 +00002441 /* Now define the assign to setup the enter_c macro correctly */
2442 snprintf(directive, sizeof(directive),
2443 "%%assign %%$localsize %%$localsize+%d", size);
Keith Kaniosb7a89542007-04-12 02:40:54 +00002444 do_directive(tokenize(directive));
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002445
H. Peter Anvine2c80182005-01-15 22:15:51 +00002446 /* Move to the next argument in the list */
2447 tline = tline->next;
2448 if (tline && tline->type == TOK_WHITESPACE)
2449 tline = tline->next;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002450 } while (tline && tline->type == TOK_OTHER && tline->text[0] == ',');
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002451 LocalOffset = offset;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002452 free_tlist(origline);
2453 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002454
H. Peter Anvine2c80182005-01-15 22:15:51 +00002455 case PP_CLEAR:
2456 if (tline->next)
H. Peter Anvin917a3492008-09-24 09:14:49 -07002457 error(ERR_WARNING|ERR_PASS1,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002458 "trailing garbage after `%%clear' ignored");
2459 free_macros();
2460 init_macros();
H. Peter Anvine2c80182005-01-15 22:15:51 +00002461 free_tlist(origline);
2462 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002463
H. Peter Anvin418ca702008-05-30 10:42:30 -07002464 case PP_DEPEND:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002465 t = tline->next = expand_smacro(tline->next);
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002466 skip_white_(t);
2467 if (!t || (t->type != TOK_STRING &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002468 t->type != TOK_INTERNAL_STRING)) {
H. Peter Anvin418ca702008-05-30 10:42:30 -07002469 error(ERR_NONFATAL, "`%%depend' expects a file name");
2470 free_tlist(origline);
2471 return DIRECTIVE_FOUND; /* but we did _something_ */
2472 }
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002473 if (t->next)
H. Peter Anvin917a3492008-09-24 09:14:49 -07002474 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvin418ca702008-05-30 10:42:30 -07002475 "trailing garbage after `%%depend' ignored");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002476 p = t->text;
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002477 if (t->type != TOK_INTERNAL_STRING)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002478 nasm_unquote_cstr(p, i);
2479 if (dephead && !in_list(*dephead, p)) {
2480 StrList *sl = nasm_malloc(strlen(p)+1+sizeof sl->next);
2481 sl->next = NULL;
2482 strcpy(sl->str, p);
2483 *deptail = sl;
2484 deptail = &sl->next;
2485 }
2486 free_tlist(origline);
H. Peter Anvin418ca702008-05-30 10:42:30 -07002487 return DIRECTIVE_FOUND;
2488
2489 case PP_INCLUDE:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002490 t = tline->next = expand_smacro(tline->next);
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002491 skip_white_(t);
H. Peter Anvind2456592008-06-19 15:04:18 -07002492
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002493 if (!t || (t->type != TOK_STRING &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002494 t->type != TOK_INTERNAL_STRING)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002495 error(ERR_NONFATAL, "`%%include' expects a file name");
2496 free_tlist(origline);
2497 return DIRECTIVE_FOUND; /* but we did _something_ */
2498 }
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002499 if (t->next)
H. Peter Anvin917a3492008-09-24 09:14:49 -07002500 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +00002501 "trailing garbage after `%%include' ignored");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002502 p = t->text;
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002503 if (t->type != TOK_INTERNAL_STRING)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002504 nasm_unquote_cstr(p, i);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002505 inc = nasm_malloc(sizeof(Include));
2506 inc->next = istk;
2507 inc->conds = NULL;
H. Peter Anvin2b1c3b92008-06-06 10:38:46 -07002508 inc->fp = inc_fopen(p, dephead, &deptail, pass == 0);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002509 if (!inc->fp) {
2510 /* -MG given but file not found */
2511 nasm_free(inc);
2512 } else {
2513 inc->fname = src_set_fname(nasm_strdup(p));
2514 inc->lineno = src_set_linnum(0);
2515 inc->lineinc = 1;
2516 inc->expansion = NULL;
2517 inc->mstk = NULL;
2518 istk = inc;
2519 list->uplevel(LIST_INCLUDE);
2520 }
2521 free_tlist(origline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002522 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002523
H. Peter Anvind2456592008-06-19 15:04:18 -07002524 case PP_USE:
H. Peter Anvinf4ae5ad2008-06-19 18:39:24 -07002525 {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002526 static macros_t *use_pkg;
2527 const char *pkg_macro = NULL;
H. Peter Anvinf4ae5ad2008-06-19 18:39:24 -07002528
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002529 tline = tline->next;
2530 skip_white_(tline);
2531 tline = expand_id(tline);
H. Peter Anvind2456592008-06-19 15:04:18 -07002532
H. Peter Anvin264b7b92008-10-24 16:38:17 -07002533 if (!tline || (tline->type != TOK_STRING &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002534 tline->type != TOK_INTERNAL_STRING &&
2535 tline->type != TOK_ID)) {
H. Peter Anvin926fc402008-06-19 16:26:12 -07002536 error(ERR_NONFATAL, "`%%use' expects a package name");
H. Peter Anvind2456592008-06-19 15:04:18 -07002537 free_tlist(origline);
2538 return DIRECTIVE_FOUND; /* but we did _something_ */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002539 }
H. Peter Anvin264b7b92008-10-24 16:38:17 -07002540 if (tline->next)
H. Peter Anvin917a3492008-09-24 09:14:49 -07002541 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvind2456592008-06-19 15:04:18 -07002542 "trailing garbage after `%%use' ignored");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002543 if (tline->type == TOK_STRING)
2544 nasm_unquote_cstr(tline->text, i);
2545 use_pkg = nasm_stdmac_find_package(tline->text);
2546 if (!use_pkg)
2547 error(ERR_NONFATAL, "unknown `%%use' package: %s", tline->text);
2548 else
2549 pkg_macro = (char *)use_pkg + 1; /* The first string will be <%define>__USE_*__ */
Victor van den Elzen35eb2ea2010-03-10 22:33:48 +01002550 if (use_pkg && ! smacro_defined(NULL, pkg_macro, 0, NULL, true)) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002551 /* Not already included, go ahead and include it */
2552 stdmacpos = use_pkg;
2553 }
2554 free_tlist(origline);
H. Peter Anvind2456592008-06-19 15:04:18 -07002555 return DIRECTIVE_FOUND;
H. Peter Anvinf4ae5ad2008-06-19 18:39:24 -07002556 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002557 case PP_PUSH:
H. Peter Anvine2c80182005-01-15 22:15:51 +00002558 case PP_REPL:
H. Peter Anvin42b56392008-10-24 16:24:21 -07002559 case PP_POP:
H. Peter Anvine2c80182005-01-15 22:15:51 +00002560 tline = tline->next;
2561 skip_white_(tline);
2562 tline = expand_id(tline);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002563 if (tline) {
2564 if (!tok_type_(tline, TOK_ID)) {
2565 error(ERR_NONFATAL, "`%s' expects a context identifier",
2566 pp_directives[i]);
2567 free_tlist(origline);
2568 return DIRECTIVE_FOUND; /* but we did _something_ */
2569 }
2570 if (tline->next)
2571 error(ERR_WARNING|ERR_PASS1,
2572 "trailing garbage after `%s' ignored",
2573 pp_directives[i]);
2574 p = nasm_strdup(tline->text);
2575 } else {
2576 p = NULL; /* Anonymous */
2577 }
H. Peter Anvin42b56392008-10-24 16:24:21 -07002578
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002579 if (i == PP_PUSH) {
2580 ctx = nasm_malloc(sizeof(Context));
2581 ctx->next = cstk;
2582 hash_init(&ctx->localmac, HASH_SMALL);
2583 ctx->name = p;
2584 ctx->number = unique++;
2585 cstk = ctx;
2586 } else {
2587 /* %pop or %repl */
2588 if (!cstk) {
2589 error(ERR_NONFATAL, "`%s': context stack is empty",
2590 pp_directives[i]);
2591 } else if (i == PP_POP) {
2592 if (p && (!cstk->name || nasm_stricmp(p, cstk->name)))
2593 error(ERR_NONFATAL, "`%%pop' in wrong context: %s, "
2594 "expected %s",
2595 cstk->name ? cstk->name : "anonymous", p);
2596 else
2597 ctx_pop();
2598 } else {
2599 /* i == PP_REPL */
2600 nasm_free(cstk->name);
2601 cstk->name = p;
2602 p = NULL;
2603 }
2604 nasm_free(p);
2605 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002606 free_tlist(origline);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002607 return DIRECTIVE_FOUND;
H. Peter Anvin8e3f75e2008-09-24 00:21:58 -07002608 case PP_FATAL:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002609 severity = ERR_FATAL;
2610 goto issue_error;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002611 case PP_ERROR:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002612 severity = ERR_NONFATAL;
2613 goto issue_error;
H. Peter Anvin7df04172008-06-10 18:27:38 -07002614 case PP_WARNING:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002615 severity = ERR_WARNING|ERR_WARN_USER;
2616 goto issue_error;
H. Peter Anvin8e3f75e2008-09-24 00:21:58 -07002617
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002618issue_error:
H. Peter Anvin7df04172008-06-10 18:27:38 -07002619 {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002620 /* Only error out if this is the final pass */
2621 if (pass != 2 && i != PP_FATAL)
2622 return DIRECTIVE_FOUND;
2623
2624 tline->next = expand_smacro(tline->next);
2625 tline = tline->next;
2626 skip_white_(tline);
2627 t = tline ? tline->next : NULL;
2628 skip_white_(t);
2629 if (tok_type_(tline, TOK_STRING) && !t) {
2630 /* The line contains only a quoted string */
2631 p = tline->text;
2632 nasm_unquote(p, NULL); /* Ignore NUL character truncation */
2633 error(severity, "%s", p);
2634 } else {
2635 /* Not a quoted string, or more than a quoted string */
2636 p = detoken(tline, false);
2637 error(severity, "%s", p);
2638 nasm_free(p);
2639 }
2640 free_tlist(origline);
2641 return DIRECTIVE_FOUND;
H. Peter Anvin7df04172008-06-10 18:27:38 -07002642 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00002643
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002644 CASE_PP_IF:
H. Peter Anvine2c80182005-01-15 22:15:51 +00002645 if (istk->conds && !emitting(istk->conds->state))
2646 j = COND_NEVER;
2647 else {
2648 j = if_condition(tline->next, i);
2649 tline->next = NULL; /* it got freed */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002650 j = j < 0 ? COND_NEVER : j ? COND_IF_TRUE : COND_IF_FALSE;
2651 }
2652 cond = nasm_malloc(sizeof(Cond));
2653 cond->next = istk->conds;
2654 cond->state = j;
2655 istk->conds = cond;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002656 if(istk->mstk)
Keith Kanios3c0d91f2009-10-25 13:28:03 -05002657 istk->mstk->condcnt ++;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002658 free_tlist(origline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002659 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002660
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002661 CASE_PP_ELIF:
H. Peter Anvine2c80182005-01-15 22:15:51 +00002662 if (!istk->conds)
H. Peter Anvin4169a472007-09-12 01:29:43 +00002663 error(ERR_FATAL, "`%s': no matching `%%if'", pp_directives[i]);
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002664 switch(istk->conds->state) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002665 case COND_IF_TRUE:
2666 istk->conds->state = COND_DONE;
2667 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002668
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002669 case COND_DONE:
2670 case COND_NEVER:
2671 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002672
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002673 case COND_ELSE_TRUE:
2674 case COND_ELSE_FALSE:
2675 error_precond(ERR_WARNING|ERR_PASS1,
2676 "`%%elif' after `%%else' ignored");
2677 istk->conds->state = COND_NEVER;
2678 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002679
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002680 case COND_IF_FALSE:
2681 /*
2682 * IMPORTANT: In the case of %if, we will already have
2683 * called expand_mmac_params(); however, if we're
2684 * processing an %elif we must have been in a
2685 * non-emitting mode, which would have inhibited
2686 * the normal invocation of expand_mmac_params().
2687 * Therefore, we have to do it explicitly here.
2688 */
2689 j = if_condition(expand_mmac_params(tline->next), i);
2690 tline->next = NULL; /* it got freed */
2691 istk->conds->state =
2692 j < 0 ? COND_NEVER : j ? COND_IF_TRUE : COND_IF_FALSE;
2693 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002694 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002695 free_tlist(origline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002696 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002697
H. Peter Anvine2c80182005-01-15 22:15:51 +00002698 case PP_ELSE:
2699 if (tline->next)
H. Peter Anvin917a3492008-09-24 09:14:49 -07002700 error_precond(ERR_WARNING|ERR_PASS1,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002701 "trailing garbage after `%%else' ignored");
H. Peter Anvine2c80182005-01-15 22:15:51 +00002702 if (!istk->conds)
2703 error(ERR_FATAL, "`%%else': no matching `%%if'");
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002704 switch(istk->conds->state) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002705 case COND_IF_TRUE:
2706 case COND_DONE:
2707 istk->conds->state = COND_ELSE_FALSE;
2708 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002709
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002710 case COND_NEVER:
2711 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002712
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002713 case COND_IF_FALSE:
2714 istk->conds->state = COND_ELSE_TRUE;
2715 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002716
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002717 case COND_ELSE_TRUE:
2718 case COND_ELSE_FALSE:
2719 error_precond(ERR_WARNING|ERR_PASS1,
2720 "`%%else' after `%%else' ignored.");
2721 istk->conds->state = COND_NEVER;
2722 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002723 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002724 free_tlist(origline);
2725 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002726
H. Peter Anvine2c80182005-01-15 22:15:51 +00002727 case PP_ENDIF:
2728 if (tline->next)
H. Peter Anvin917a3492008-09-24 09:14:49 -07002729 error_precond(ERR_WARNING|ERR_PASS1,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002730 "trailing garbage after `%%endif' ignored");
H. Peter Anvine2c80182005-01-15 22:15:51 +00002731 if (!istk->conds)
2732 error(ERR_FATAL, "`%%endif': no matching `%%if'");
2733 cond = istk->conds;
2734 istk->conds = cond->next;
2735 nasm_free(cond);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002736 if(istk->mstk)
Keith Kanios3c0d91f2009-10-25 13:28:03 -05002737 istk->mstk->condcnt --;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002738 free_tlist(origline);
2739 return DIRECTIVE_FOUND;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002740
H. Peter Anvindb8f96e2009-07-15 09:07:29 -04002741 case PP_RMACRO:
2742 case PP_IRMACRO:
H. Peter Anvine2c80182005-01-15 22:15:51 +00002743 case PP_MACRO:
2744 case PP_IMACRO:
H. Peter Anvina26433d2008-07-16 14:40:01 -07002745 if (defining) {
H. Peter Anvindb8f96e2009-07-15 09:07:29 -04002746 error(ERR_FATAL, "`%s': already defining a macro",
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002747 pp_directives[i]);
2748 return DIRECTIVE_FOUND;
2749 }
2750 defining = nasm_malloc(sizeof(MMacro));
2751 defining->max_depth =
2752 (i == PP_RMACRO) || (i == PP_IRMACRO) ? DEADMAN_LIMIT : 0;
2753 defining->casesense = (i == PP_MACRO) || (i == PP_RMACRO);
2754 if (!parse_mmacro_spec(tline, defining, pp_directives[i])) {
2755 nasm_free(defining);
2756 defining = NULL;
2757 return DIRECTIVE_FOUND;
2758 }
H. Peter Anvina26433d2008-07-16 14:40:01 -07002759
H. Peter Anvin166c2472008-05-28 12:28:58 -07002760 mmac = (MMacro *) hash_findix(&mmacros, defining->name);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002761 while (mmac) {
2762 if (!strcmp(mmac->name, defining->name) &&
2763 (mmac->nparam_min <= defining->nparam_max
2764 || defining->plus)
2765 && (defining->nparam_min <= mmac->nparam_max
2766 || mmac->plus)) {
H. Peter Anvin917a3492008-09-24 09:14:49 -07002767 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +00002768 "redefining multi-line macro `%s'", defining->name);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002769 return DIRECTIVE_FOUND;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002770 }
2771 mmac = mmac->next;
2772 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002773 free_tlist(origline);
2774 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002775
H. Peter Anvine2c80182005-01-15 22:15:51 +00002776 case PP_ENDM:
2777 case PP_ENDMACRO:
Victor van den Elzen8f1120f2008-07-16 13:41:37 +02002778 if (! (defining && defining->name)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002779 error(ERR_NONFATAL, "`%s': not defining a macro", tline->text);
2780 return DIRECTIVE_FOUND;
2781 }
Keith Kanios852f1ee2009-07-12 00:19:55 -05002782 mmhead = (MMacro **) hash_findi_add(&mmacros, defining->name);
H. Peter Anvin97a23472007-09-16 17:57:25 -07002783 defining->next = *mmhead;
Keith Kanios852f1ee2009-07-12 00:19:55 -05002784 *mmhead = defining;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002785 defining = NULL;
2786 free_tlist(origline);
2787 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002788
H. Peter Anvin89cee572009-07-15 09:16:54 -04002789 case PP_EXITMACRO:
Keith Kanios852f1ee2009-07-12 00:19:55 -05002790 /*
2791 * We must search along istk->expansion until we hit a
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002792 * macro-end marker for a macro with a name. Then we
Keith Kanios3c0d91f2009-10-25 13:28:03 -05002793 * bypass all lines between exitmacro and endmacro.
Keith Kanios852f1ee2009-07-12 00:19:55 -05002794 */
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04002795 list_for_each(l, istk->expansion)
Keith Kanios852f1ee2009-07-12 00:19:55 -05002796 if (l->finishes && l->finishes->name)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002797 break;
Keith Kanios852f1ee2009-07-12 00:19:55 -05002798
2799 if (l) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002800 /*
2801 * Remove all conditional entries relative to this
2802 * macro invocation. (safe to do in this context)
2803 */
Keith Kanios3c0d91f2009-10-25 13:28:03 -05002804 for ( ; l->finishes->condcnt > 0; l->finishes->condcnt --) {
2805 cond = istk->conds;
2806 istk->conds = cond->next;
2807 nasm_free(cond);
2808 }
2809 istk->expansion = l;
Keith Kanios852f1ee2009-07-12 00:19:55 -05002810 } else {
2811 error(ERR_NONFATAL, "`%%exitmacro' not within `%%macro' block");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002812 }
Keith Kanios852f1ee2009-07-12 00:19:55 -05002813 free_tlist(origline);
2814 return DIRECTIVE_FOUND;
2815
H. Peter Anvina26433d2008-07-16 14:40:01 -07002816 case PP_UNMACRO:
2817 case PP_UNIMACRO:
2818 {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002819 MMacro **mmac_p;
2820 MMacro spec;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002821
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002822 spec.casesense = (i == PP_UNMACRO);
2823 if (!parse_mmacro_spec(tline, &spec, pp_directives[i])) {
2824 return DIRECTIVE_FOUND;
2825 }
H. Peter Anvina26433d2008-07-16 14:40:01 -07002826 mmac_p = (MMacro **) hash_findi(&mmacros, spec.name, NULL);
2827 while (mmac_p && *mmac_p) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002828 mmac = *mmac_p;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002829 if (mmac->casesense == spec.casesense &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002830 !mstrcmp(mmac->name, spec.name, spec.casesense) &&
H. Peter Anvina26433d2008-07-16 14:40:01 -07002831 mmac->nparam_min == spec.nparam_min &&
2832 mmac->nparam_max == spec.nparam_max &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002833 mmac->plus == spec.plus) {
2834 *mmac_p = mmac->next;
2835 free_mmacro(mmac);
2836 } else {
2837 mmac_p = &mmac->next;
2838 }
2839 }
2840 free_tlist(origline);
2841 free_tlist(spec.dlist);
2842 return DIRECTIVE_FOUND;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002843 }
2844
H. Peter Anvine2c80182005-01-15 22:15:51 +00002845 case PP_ROTATE:
2846 if (tline->next && tline->next->type == TOK_WHITESPACE)
2847 tline = tline->next;
H. Peter Anvin89cee572009-07-15 09:16:54 -04002848 if (!tline->next) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002849 free_tlist(origline);
2850 error(ERR_NONFATAL, "`%%rotate' missing rotate count");
2851 return DIRECTIVE_FOUND;
2852 }
2853 t = expand_smacro(tline->next);
2854 tline->next = NULL;
2855 free_tlist(origline);
2856 tline = t;
2857 tptr = &t;
2858 tokval.t_type = TOKEN_INVALID;
2859 evalresult =
2860 evaluate(ppscan, tptr, &tokval, NULL, pass, error, NULL);
2861 free_tlist(tline);
2862 if (!evalresult)
2863 return DIRECTIVE_FOUND;
2864 if (tokval.t_type)
H. Peter Anvin917a3492008-09-24 09:14:49 -07002865 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +00002866 "trailing garbage after expression ignored");
2867 if (!is_simple(evalresult)) {
2868 error(ERR_NONFATAL, "non-constant value given to `%%rotate'");
2869 return DIRECTIVE_FOUND;
2870 }
2871 mmac = istk->mstk;
2872 while (mmac && !mmac->name) /* avoid mistaking %reps for macros */
2873 mmac = mmac->next_active;
2874 if (!mmac) {
2875 error(ERR_NONFATAL, "`%%rotate' invoked outside a macro call");
2876 } else if (mmac->nparam == 0) {
2877 error(ERR_NONFATAL,
2878 "`%%rotate' invoked within macro without parameters");
2879 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002880 int rotate = mmac->rotate + reloc_value(evalresult);
H. Peter Anvin734b1882002-04-30 21:01:08 +00002881
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002882 rotate %= (int)mmac->nparam;
2883 if (rotate < 0)
2884 rotate += mmac->nparam;
H. Peter Anvin70653092007-10-19 14:42:29 -07002885
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002886 mmac->rotate = rotate;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002887 }
2888 return DIRECTIVE_FOUND;
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00002889
H. Peter Anvine2c80182005-01-15 22:15:51 +00002890 case PP_REP:
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002891 nolist = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002892 do {
2893 tline = tline->next;
2894 } while (tok_type_(tline, TOK_WHITESPACE));
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00002895
H. Peter Anvine2c80182005-01-15 22:15:51 +00002896 if (tok_type_(tline, TOK_ID) &&
2897 nasm_stricmp(tline->text, ".nolist") == 0) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002898 nolist = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002899 do {
2900 tline = tline->next;
2901 } while (tok_type_(tline, TOK_WHITESPACE));
2902 }
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00002903
H. Peter Anvine2c80182005-01-15 22:15:51 +00002904 if (tline) {
2905 t = expand_smacro(tline);
2906 tptr = &t;
2907 tokval.t_type = TOKEN_INVALID;
2908 evalresult =
2909 evaluate(ppscan, tptr, &tokval, NULL, pass, error, NULL);
2910 if (!evalresult) {
2911 free_tlist(origline);
2912 return DIRECTIVE_FOUND;
2913 }
2914 if (tokval.t_type)
H. Peter Anvin917a3492008-09-24 09:14:49 -07002915 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +00002916 "trailing garbage after expression ignored");
2917 if (!is_simple(evalresult)) {
2918 error(ERR_NONFATAL, "non-constant value given to `%%rep'");
2919 return DIRECTIVE_FOUND;
2920 }
Cyrill Gorcunove091d6e2010-08-09 13:58:22 +04002921 count = reloc_value(evalresult);
2922 if (count >= REP_LIMIT) {
Cyrill Gorcunov71f4f842010-08-09 20:17:17 +04002923 error(ERR_NONFATAL, "`%%rep' value exceeds limit");
Cyrill Gorcunove091d6e2010-08-09 13:58:22 +04002924 count = 0;
2925 } else
2926 count++;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002927 } else {
2928 error(ERR_NONFATAL, "`%%rep' expects a repeat count");
H. Peter Anvinf8ba53e2007-10-11 10:11:57 -07002929 count = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002930 }
2931 free_tlist(origline);
H. Peter Anvin734b1882002-04-30 21:01:08 +00002932
H. Peter Anvine2c80182005-01-15 22:15:51 +00002933 tmp_defining = defining;
2934 defining = nasm_malloc(sizeof(MMacro));
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002935 defining->prev = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002936 defining->name = NULL; /* flags this macro as a %rep block */
H. Peter Anvin70055962007-10-11 00:05:31 -07002937 defining->casesense = false;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002938 defining->plus = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002939 defining->nolist = nolist;
H. Peter Anvinf8ba53e2007-10-11 10:11:57 -07002940 defining->in_progress = count;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002941 defining->max_depth = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002942 defining->nparam_min = defining->nparam_max = 0;
2943 defining->defaults = NULL;
2944 defining->dlist = NULL;
2945 defining->expansion = NULL;
2946 defining->next_active = istk->mstk;
2947 defining->rep_nest = tmp_defining;
2948 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002949
H. Peter Anvine2c80182005-01-15 22:15:51 +00002950 case PP_ENDREP:
2951 if (!defining || defining->name) {
2952 error(ERR_NONFATAL, "`%%endrep': no matching `%%rep'");
2953 return DIRECTIVE_FOUND;
2954 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00002955
H. Peter Anvine2c80182005-01-15 22:15:51 +00002956 /*
2957 * Now we have a "macro" defined - although it has no name
2958 * and we won't be entering it in the hash tables - we must
2959 * push a macro-end marker for it on to istk->expansion.
2960 * After that, it will take care of propagating itself (a
2961 * macro-end marker line for a macro which is really a %rep
2962 * block will cause the macro to be re-expanded, complete
2963 * with another macro-end marker to ensure the process
2964 * continues) until the whole expansion is forcibly removed
2965 * from istk->expansion by a %exitrep.
2966 */
2967 l = nasm_malloc(sizeof(Line));
2968 l->next = istk->expansion;
2969 l->finishes = defining;
2970 l->first = NULL;
2971 istk->expansion = l;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002972
H. Peter Anvine2c80182005-01-15 22:15:51 +00002973 istk->mstk = defining;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002974
H. Peter Anvine2c80182005-01-15 22:15:51 +00002975 list->uplevel(defining->nolist ? LIST_MACRO_NOLIST : LIST_MACRO);
2976 tmp_defining = defining;
2977 defining = defining->rep_nest;
2978 free_tlist(origline);
2979 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002980
H. Peter Anvine2c80182005-01-15 22:15:51 +00002981 case PP_EXITREP:
2982 /*
2983 * We must search along istk->expansion until we hit a
2984 * macro-end marker for a macro with no name. Then we set
2985 * its `in_progress' flag to 0.
2986 */
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04002987 list_for_each(l, istk->expansion)
H. Peter Anvine2c80182005-01-15 22:15:51 +00002988 if (l->finishes && !l->finishes->name)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002989 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002990
H. Peter Anvine2c80182005-01-15 22:15:51 +00002991 if (l)
Charles Crayned4200be2008-07-12 16:42:33 -07002992 l->finishes->in_progress = 1;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002993 else
2994 error(ERR_NONFATAL, "`%%exitrep' not within `%%rep' block");
2995 free_tlist(origline);
2996 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002997
H. Peter Anvine2c80182005-01-15 22:15:51 +00002998 case PP_XDEFINE:
2999 case PP_IXDEFINE:
3000 case PP_DEFINE:
3001 case PP_IDEFINE:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003002 casesense = (i == PP_DEFINE || i == PP_XDEFINE);
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07003003
H. Peter Anvine2c80182005-01-15 22:15:51 +00003004 tline = tline->next;
3005 skip_white_(tline);
3006 tline = expand_id(tline);
3007 if (!tline || (tline->type != TOK_ID &&
3008 (tline->type != TOK_PREPROC_ID ||
3009 tline->text[1] != '$'))) {
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07003010 error(ERR_NONFATAL, "`%s' expects a macro identifier",
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003011 pp_directives[i]);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003012 free_tlist(origline);
3013 return DIRECTIVE_FOUND;
3014 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003015
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08003016 ctx = get_ctx(tline->text, &mname, false);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003017 last = tline;
3018 param_start = tline = tline->next;
3019 nparam = 0;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003020
H. Peter Anvine2c80182005-01-15 22:15:51 +00003021 /* Expand the macro definition now for %xdefine and %ixdefine */
3022 if ((i == PP_XDEFINE) || (i == PP_IXDEFINE))
3023 tline = expand_smacro(tline);
H. Peter Anvin734b1882002-04-30 21:01:08 +00003024
H. Peter Anvine2c80182005-01-15 22:15:51 +00003025 if (tok_is_(tline, "(")) {
3026 /*
3027 * This macro has parameters.
3028 */
H. Peter Anvin734b1882002-04-30 21:01:08 +00003029
H. Peter Anvine2c80182005-01-15 22:15:51 +00003030 tline = tline->next;
3031 while (1) {
3032 skip_white_(tline);
3033 if (!tline) {
3034 error(ERR_NONFATAL, "parameter identifier expected");
3035 free_tlist(origline);
3036 return DIRECTIVE_FOUND;
3037 }
3038 if (tline->type != TOK_ID) {
3039 error(ERR_NONFATAL,
3040 "`%s': parameter identifier expected",
3041 tline->text);
3042 free_tlist(origline);
3043 return DIRECTIVE_FOUND;
3044 }
3045 tline->type = TOK_SMAC_PARAM + nparam++;
3046 tline = tline->next;
3047 skip_white_(tline);
3048 if (tok_is_(tline, ",")) {
3049 tline = tline->next;
H. Peter Anvinca348b62008-07-23 10:49:26 -04003050 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003051 if (!tok_is_(tline, ")")) {
3052 error(ERR_NONFATAL,
3053 "`)' expected to terminate macro template");
3054 free_tlist(origline);
3055 return DIRECTIVE_FOUND;
3056 }
3057 break;
3058 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00003059 }
3060 last = tline;
3061 tline = tline->next;
3062 }
3063 if (tok_type_(tline, TOK_WHITESPACE))
3064 last = tline, tline = tline->next;
3065 macro_start = NULL;
3066 last->next = NULL;
3067 t = tline;
3068 while (t) {
3069 if (t->type == TOK_ID) {
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04003070 list_for_each(tt, param_start)
H. Peter Anvine2c80182005-01-15 22:15:51 +00003071 if (tt->type >= TOK_SMAC_PARAM &&
3072 !strcmp(tt->text, t->text))
3073 t->type = tt->type;
3074 }
3075 tt = t->next;
3076 t->next = macro_start;
3077 macro_start = t;
3078 t = tt;
3079 }
3080 /*
3081 * Good. We now have a macro name, a parameter count, and a
3082 * token list (in reverse order) for an expansion. We ought
3083 * to be OK just to create an SMacro, store it, and let
3084 * free_tlist have the rest of the line (which we have
3085 * carefully re-terminated after chopping off the expansion
3086 * from the end).
3087 */
H. Peter Anvin4db5a162007-10-11 13:42:09 -07003088 define_smacro(ctx, mname, casesense, nparam, macro_start);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003089 free_tlist(origline);
3090 return DIRECTIVE_FOUND;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003091
H. Peter Anvine2c80182005-01-15 22:15:51 +00003092 case PP_UNDEF:
3093 tline = tline->next;
3094 skip_white_(tline);
3095 tline = expand_id(tline);
3096 if (!tline || (tline->type != TOK_ID &&
3097 (tline->type != TOK_PREPROC_ID ||
3098 tline->text[1] != '$'))) {
3099 error(ERR_NONFATAL, "`%%undef' expects a macro identifier");
3100 free_tlist(origline);
3101 return DIRECTIVE_FOUND;
3102 }
3103 if (tline->next) {
H. Peter Anvin917a3492008-09-24 09:14:49 -07003104 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +00003105 "trailing garbage after macro name ignored");
3106 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003107
H. Peter Anvine2c80182005-01-15 22:15:51 +00003108 /* Find the context that symbol belongs to */
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08003109 ctx = get_ctx(tline->text, &mname, false);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003110 undef_smacro(ctx, mname);
3111 free_tlist(origline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003112 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003113
H. Peter Anvin9e200162008-06-04 17:23:14 -07003114 case PP_DEFSTR:
3115 case PP_IDEFSTR:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003116 casesense = (i == PP_DEFSTR);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003117
3118 tline = tline->next;
3119 skip_white_(tline);
3120 tline = expand_id(tline);
3121 if (!tline || (tline->type != TOK_ID &&
3122 (tline->type != TOK_PREPROC_ID ||
3123 tline->text[1] != '$'))) {
3124 error(ERR_NONFATAL, "`%s' expects a macro identifier",
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003125 pp_directives[i]);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003126 free_tlist(origline);
3127 return DIRECTIVE_FOUND;
3128 }
3129
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08003130 ctx = get_ctx(tline->text, &mname, false);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003131 last = tline;
3132 tline = expand_smacro(tline->next);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003133 last->next = NULL;
H. Peter Anvin9e200162008-06-04 17:23:14 -07003134
3135 while (tok_type_(tline, TOK_WHITESPACE))
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003136 tline = delete_Token(tline);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003137
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003138 p = detoken(tline, false);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003139 macro_start = nasm_malloc(sizeof(*macro_start));
3140 macro_start->next = NULL;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003141 macro_start->text = nasm_quote(p, strlen(p));
3142 macro_start->type = TOK_STRING;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003143 macro_start->a.mac = NULL;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003144 nasm_free(p);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003145
3146 /*
3147 * We now have a macro name, an implicit parameter count of
3148 * zero, and a string token to use as an expansion. Create
3149 * and store an SMacro.
3150 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003151 define_smacro(ctx, mname, casesense, 0, macro_start);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003152 free_tlist(origline);
3153 return DIRECTIVE_FOUND;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003154
H. Peter Anvin2f55bda2009-07-14 15:04:04 -04003155 case PP_DEFTOK:
3156 case PP_IDEFTOK:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003157 casesense = (i == PP_DEFTOK);
3158
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003159 tline = tline->next;
3160 skip_white_(tline);
3161 tline = expand_id(tline);
3162 if (!tline || (tline->type != TOK_ID &&
3163 (tline->type != TOK_PREPROC_ID ||
3164 tline->text[1] != '$'))) {
3165 error(ERR_NONFATAL,
3166 "`%s' expects a macro identifier as first parameter",
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003167 pp_directives[i]);
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003168 free_tlist(origline);
3169 return DIRECTIVE_FOUND;
3170 }
3171 ctx = get_ctx(tline->text, &mname, false);
3172 last = tline;
3173 tline = expand_smacro(tline->next);
3174 last->next = NULL;
3175
3176 t = tline;
3177 while (tok_type_(t, TOK_WHITESPACE))
3178 t = t->next;
3179 /* t should now point to the string */
Cyrill Gorcunov6908e582010-09-06 19:36:15 +04003180 if (!tok_type_(t, TOK_STRING)) {
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003181 error(ERR_NONFATAL,
3182 "`%s` requires string as second parameter",
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003183 pp_directives[i]);
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003184 free_tlist(tline);
3185 free_tlist(origline);
3186 return DIRECTIVE_FOUND;
3187 }
3188
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003189 nasm_unquote_cstr(t->text, i);
3190 macro_start = tokenize(t->text);
3191
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003192 /*
3193 * We now have a macro name, an implicit parameter count of
3194 * zero, and a numeric token to use as an expansion. Create
3195 * and store an SMacro.
3196 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003197 define_smacro(ctx, mname, casesense, 0, macro_start);
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003198 free_tlist(tline);
3199 free_tlist(origline);
3200 return DIRECTIVE_FOUND;
H. Peter Anvin9e200162008-06-04 17:23:14 -07003201
H. Peter Anvin418ca702008-05-30 10:42:30 -07003202 case PP_PATHSEARCH:
3203 {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003204 FILE *fp;
3205 StrList *xsl = NULL;
3206 StrList **xst = &xsl;
H. Peter Anvin418ca702008-05-30 10:42:30 -07003207
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003208 casesense = true;
H. Peter Anvin418ca702008-05-30 10:42:30 -07003209
3210 tline = tline->next;
3211 skip_white_(tline);
3212 tline = expand_id(tline);
3213 if (!tline || (tline->type != TOK_ID &&
3214 (tline->type != TOK_PREPROC_ID ||
3215 tline->text[1] != '$'))) {
3216 error(ERR_NONFATAL,
3217 "`%%pathsearch' expects a macro identifier as first parameter");
3218 free_tlist(origline);
3219 return DIRECTIVE_FOUND;
3220 }
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08003221 ctx = get_ctx(tline->text, &mname, false);
H. Peter Anvin418ca702008-05-30 10:42:30 -07003222 last = tline;
3223 tline = expand_smacro(tline->next);
3224 last->next = NULL;
3225
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003226 t = tline;
H. Peter Anvin418ca702008-05-30 10:42:30 -07003227 while (tok_type_(t, TOK_WHITESPACE))
3228 t = t->next;
3229
3230 if (!t || (t->type != TOK_STRING &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003231 t->type != TOK_INTERNAL_STRING)) {
H. Peter Anvin418ca702008-05-30 10:42:30 -07003232 error(ERR_NONFATAL, "`%%pathsearch' expects a file name");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003233 free_tlist(tline);
H. Peter Anvin418ca702008-05-30 10:42:30 -07003234 free_tlist(origline);
3235 return DIRECTIVE_FOUND; /* but we did _something_ */
3236 }
3237 if (t->next)
H. Peter Anvin917a3492008-09-24 09:14:49 -07003238 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvin418ca702008-05-30 10:42:30 -07003239 "trailing garbage after `%%pathsearch' ignored");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003240 p = t->text;
H. Peter Anvin427cc912008-06-01 21:43:03 -07003241 if (t->type != TOK_INTERNAL_STRING)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003242 nasm_unquote(p, NULL);
H. Peter Anvin418ca702008-05-30 10:42:30 -07003243
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003244 fp = inc_fopen(p, &xsl, &xst, true);
3245 if (fp) {
3246 p = xsl->str;
3247 fclose(fp); /* Don't actually care about the file */
3248 }
H. Peter Anvin418ca702008-05-30 10:42:30 -07003249 macro_start = nasm_malloc(sizeof(*macro_start));
3250 macro_start->next = NULL;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003251 macro_start->text = nasm_quote(p, strlen(p));
3252 macro_start->type = TOK_STRING;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003253 macro_start->a.mac = NULL;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003254 if (xsl)
3255 nasm_free(xsl);
H. Peter Anvin418ca702008-05-30 10:42:30 -07003256
3257 /*
3258 * We now have a macro name, an implicit parameter count of
3259 * zero, and a string token to use as an expansion. Create
3260 * and store an SMacro.
3261 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003262 define_smacro(ctx, mname, casesense, 0, macro_start);
3263 free_tlist(tline);
H. Peter Anvin418ca702008-05-30 10:42:30 -07003264 free_tlist(origline);
3265 return DIRECTIVE_FOUND;
3266 }
3267
H. Peter Anvine2c80182005-01-15 22:15:51 +00003268 case PP_STRLEN:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003269 casesense = true;
H. Peter Anvin70653092007-10-19 14:42:29 -07003270
H. Peter Anvine2c80182005-01-15 22:15:51 +00003271 tline = tline->next;
3272 skip_white_(tline);
3273 tline = expand_id(tline);
3274 if (!tline || (tline->type != TOK_ID &&
3275 (tline->type != TOK_PREPROC_ID ||
3276 tline->text[1] != '$'))) {
3277 error(ERR_NONFATAL,
3278 "`%%strlen' expects a macro identifier as first parameter");
3279 free_tlist(origline);
3280 return DIRECTIVE_FOUND;
3281 }
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08003282 ctx = get_ctx(tline->text, &mname, false);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003283 last = tline;
3284 tline = expand_smacro(tline->next);
3285 last->next = NULL;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003286
H. Peter Anvine2c80182005-01-15 22:15:51 +00003287 t = tline;
3288 while (tok_type_(t, TOK_WHITESPACE))
3289 t = t->next;
3290 /* t should now point to the string */
Cyrill Gorcunov4e1d5ab2010-07-23 18:51:51 +04003291 if (!tok_type_(t, TOK_STRING)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00003292 error(ERR_NONFATAL,
3293 "`%%strlen` requires string as second parameter");
3294 free_tlist(tline);
3295 free_tlist(origline);
3296 return DIRECTIVE_FOUND;
3297 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003298
H. Peter Anvine2c80182005-01-15 22:15:51 +00003299 macro_start = nasm_malloc(sizeof(*macro_start));
3300 macro_start->next = NULL;
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07003301 make_tok_num(macro_start, nasm_unquote(t->text, NULL));
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003302 macro_start->a.mac = NULL;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003303
H. Peter Anvine2c80182005-01-15 22:15:51 +00003304 /*
3305 * We now have a macro name, an implicit parameter count of
3306 * zero, and a numeric token to use as an expansion. Create
3307 * and store an SMacro.
3308 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003309 define_smacro(ctx, mname, casesense, 0, macro_start);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003310 free_tlist(tline);
3311 free_tlist(origline);
3312 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003313
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003314 case PP_STRCAT:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003315 casesense = true;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003316
3317 tline = tline->next;
3318 skip_white_(tline);
3319 tline = expand_id(tline);
3320 if (!tline || (tline->type != TOK_ID &&
3321 (tline->type != TOK_PREPROC_ID ||
3322 tline->text[1] != '$'))) {
3323 error(ERR_NONFATAL,
3324 "`%%strcat' expects a macro identifier as first parameter");
3325 free_tlist(origline);
3326 return DIRECTIVE_FOUND;
3327 }
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08003328 ctx = get_ctx(tline->text, &mname, false);
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003329 last = tline;
3330 tline = expand_smacro(tline->next);
3331 last->next = NULL;
3332
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003333 len = 0;
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04003334 list_for_each(t, tline) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003335 switch (t->type) {
3336 case TOK_WHITESPACE:
3337 break;
3338 case TOK_STRING:
3339 len += t->a.len = nasm_unquote(t->text, NULL);
3340 break;
3341 case TOK_OTHER:
3342 if (!strcmp(t->text, ",")) /* permit comma separators */
3343 break;
3344 /* else fall through */
3345 default:
3346 error(ERR_NONFATAL,
3347 "non-string passed to `%%strcat' (%d)", t->type);
3348 free_tlist(tline);
3349 free_tlist(origline);
3350 return DIRECTIVE_FOUND;
3351 }
3352 }
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003353
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003354 p = pp = nasm_malloc(len);
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04003355 list_for_each(t, tline) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003356 if (t->type == TOK_STRING) {
3357 memcpy(p, t->text, t->a.len);
3358 p += t->a.len;
3359 }
3360 }
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003361
3362 /*
3363 * We now have a macro name, an implicit parameter count of
3364 * zero, and a numeric token to use as an expansion. Create
3365 * and store an SMacro.
3366 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003367 macro_start = new_Token(NULL, TOK_STRING, NULL, 0);
3368 macro_start->text = nasm_quote(pp, len);
3369 nasm_free(pp);
3370 define_smacro(ctx, mname, casesense, 0, macro_start);
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003371 free_tlist(tline);
3372 free_tlist(origline);
3373 return DIRECTIVE_FOUND;
3374
H. Peter Anvine2c80182005-01-15 22:15:51 +00003375 case PP_SUBSTR:
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003376 {
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003377 int64_t start, count;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003378 size_t len;
H. Peter Anvind2456592008-06-19 15:04:18 -07003379
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003380 casesense = true;
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07003381
H. Peter Anvine2c80182005-01-15 22:15:51 +00003382 tline = tline->next;
3383 skip_white_(tline);
3384 tline = expand_id(tline);
3385 if (!tline || (tline->type != TOK_ID &&
3386 (tline->type != TOK_PREPROC_ID ||
3387 tline->text[1] != '$'))) {
3388 error(ERR_NONFATAL,
3389 "`%%substr' expects a macro identifier as first parameter");
3390 free_tlist(origline);
3391 return DIRECTIVE_FOUND;
3392 }
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08003393 ctx = get_ctx(tline->text, &mname, false);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003394 last = tline;
3395 tline = expand_smacro(tline->next);
3396 last->next = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003397
Cyrill Gorcunov35519d62010-09-06 23:49:52 +04003398 if (tline) /* skip expanded id */
3399 t = tline->next;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003400 while (tok_type_(t, TOK_WHITESPACE))
3401 t = t->next;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003402
H. Peter Anvine2c80182005-01-15 22:15:51 +00003403 /* t should now point to the string */
Cyrill Gorcunov35519d62010-09-06 23:49:52 +04003404 if (!tok_type_(t, TOK_STRING)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00003405 error(ERR_NONFATAL,
3406 "`%%substr` requires string as second parameter");
3407 free_tlist(tline);
3408 free_tlist(origline);
3409 return DIRECTIVE_FOUND;
3410 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003411
H. Peter Anvine2c80182005-01-15 22:15:51 +00003412 tt = t->next;
3413 tptr = &tt;
3414 tokval.t_type = TOKEN_INVALID;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003415 evalresult = evaluate(ppscan, tptr, &tokval, NULL,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003416 pass, error, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003417 if (!evalresult) {
3418 free_tlist(tline);
3419 free_tlist(origline);
3420 return DIRECTIVE_FOUND;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003421 } else if (!is_simple(evalresult)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00003422 error(ERR_NONFATAL, "non-constant value given to `%%substr`");
3423 free_tlist(tline);
3424 free_tlist(origline);
3425 return DIRECTIVE_FOUND;
3426 }
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003427 start = evalresult->value - 1;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003428
3429 while (tok_type_(tt, TOK_WHITESPACE))
3430 tt = tt->next;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003431 if (!tt) {
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003432 count = 1; /* Backwards compatibility: one character */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003433 } else {
3434 tokval.t_type = TOKEN_INVALID;
3435 evalresult = evaluate(ppscan, tptr, &tokval, NULL,
3436 pass, error, NULL);
3437 if (!evalresult) {
3438 free_tlist(tline);
3439 free_tlist(origline);
3440 return DIRECTIVE_FOUND;
3441 } else if (!is_simple(evalresult)) {
3442 error(ERR_NONFATAL, "non-constant value given to `%%substr`");
3443 free_tlist(tline);
3444 free_tlist(origline);
3445 return DIRECTIVE_FOUND;
3446 }
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003447 count = evalresult->value;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003448 }
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003449
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003450 len = nasm_unquote(t->text, NULL);
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003451
Cyrill Gorcunovcff031e2010-09-07 20:31:11 +04003452 /* make start and count being in range */
3453 if (start < 0)
3454 start = 0;
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003455 if (count < 0)
3456 count = len + count + 1 - start;
3457 if (start + count > (int64_t)len)
Cyrill Gorcunovcff031e2010-09-07 20:31:11 +04003458 count = len - start;
3459 if (!len || count < 0 || start >=(int64_t)len)
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003460 start = -1, count = 0; /* empty string */
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003461
H. Peter Anvine2c80182005-01-15 22:15:51 +00003462 macro_start = nasm_malloc(sizeof(*macro_start));
3463 macro_start->next = NULL;
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003464 macro_start->text = nasm_quote((start < 0) ? "" : t->text + start, count);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003465 macro_start->type = TOK_STRING;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003466 macro_start->a.mac = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003467
H. Peter Anvine2c80182005-01-15 22:15:51 +00003468 /*
3469 * We now have a macro name, an implicit parameter count of
3470 * zero, and a numeric token to use as an expansion. Create
3471 * and store an SMacro.
3472 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003473 define_smacro(ctx, mname, casesense, 0, macro_start);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003474 free_tlist(tline);
3475 free_tlist(origline);
3476 return DIRECTIVE_FOUND;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003477 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003478
H. Peter Anvine2c80182005-01-15 22:15:51 +00003479 case PP_ASSIGN:
3480 case PP_IASSIGN:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003481 casesense = (i == PP_ASSIGN);
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07003482
H. Peter Anvine2c80182005-01-15 22:15:51 +00003483 tline = tline->next;
3484 skip_white_(tline);
3485 tline = expand_id(tline);
3486 if (!tline || (tline->type != TOK_ID &&
3487 (tline->type != TOK_PREPROC_ID ||
3488 tline->text[1] != '$'))) {
3489 error(ERR_NONFATAL,
3490 "`%%%sassign' expects a macro identifier",
3491 (i == PP_IASSIGN ? "i" : ""));
3492 free_tlist(origline);
3493 return DIRECTIVE_FOUND;
3494 }
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08003495 ctx = get_ctx(tline->text, &mname, false);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003496 last = tline;
3497 tline = expand_smacro(tline->next);
3498 last->next = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003499
H. Peter Anvine2c80182005-01-15 22:15:51 +00003500 t = tline;
3501 tptr = &t;
3502 tokval.t_type = TOKEN_INVALID;
3503 evalresult =
3504 evaluate(ppscan, tptr, &tokval, NULL, pass, error, NULL);
3505 free_tlist(tline);
3506 if (!evalresult) {
3507 free_tlist(origline);
3508 return DIRECTIVE_FOUND;
3509 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003510
H. Peter Anvine2c80182005-01-15 22:15:51 +00003511 if (tokval.t_type)
H. Peter Anvin917a3492008-09-24 09:14:49 -07003512 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +00003513 "trailing garbage after expression ignored");
H. Peter Anvin734b1882002-04-30 21:01:08 +00003514
H. Peter Anvine2c80182005-01-15 22:15:51 +00003515 if (!is_simple(evalresult)) {
3516 error(ERR_NONFATAL,
3517 "non-constant value given to `%%%sassign'",
3518 (i == PP_IASSIGN ? "i" : ""));
3519 free_tlist(origline);
3520 return DIRECTIVE_FOUND;
3521 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003522
H. Peter Anvine2c80182005-01-15 22:15:51 +00003523 macro_start = nasm_malloc(sizeof(*macro_start));
3524 macro_start->next = NULL;
3525 make_tok_num(macro_start, reloc_value(evalresult));
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003526 macro_start->a.mac = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003527
H. Peter Anvine2c80182005-01-15 22:15:51 +00003528 /*
3529 * We now have a macro name, an implicit parameter count of
3530 * zero, and a numeric token to use as an expansion. Create
3531 * and store an SMacro.
3532 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003533 define_smacro(ctx, mname, casesense, 0, macro_start);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003534 free_tlist(origline);
3535 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003536
H. Peter Anvine2c80182005-01-15 22:15:51 +00003537 case PP_LINE:
3538 /*
3539 * Syntax is `%line nnn[+mmm] [filename]'
3540 */
3541 tline = tline->next;
3542 skip_white_(tline);
3543 if (!tok_type_(tline, TOK_NUMBER)) {
3544 error(ERR_NONFATAL, "`%%line' expects line number");
3545 free_tlist(origline);
3546 return DIRECTIVE_FOUND;
3547 }
H. Peter Anvin70055962007-10-11 00:05:31 -07003548 k = readnum(tline->text, &err);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003549 m = 1;
3550 tline = tline->next;
3551 if (tok_is_(tline, "+")) {
3552 tline = tline->next;
3553 if (!tok_type_(tline, TOK_NUMBER)) {
3554 error(ERR_NONFATAL, "`%%line' expects line increment");
3555 free_tlist(origline);
3556 return DIRECTIVE_FOUND;
3557 }
H. Peter Anvin70055962007-10-11 00:05:31 -07003558 m = readnum(tline->text, &err);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003559 tline = tline->next;
3560 }
3561 skip_white_(tline);
3562 src_set_linnum(k);
3563 istk->lineinc = m;
3564 if (tline) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07003565 nasm_free(src_set_fname(detoken(tline, false)));
H. Peter Anvine2c80182005-01-15 22:15:51 +00003566 }
3567 free_tlist(origline);
3568 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003569
H. Peter Anvine2c80182005-01-15 22:15:51 +00003570 default:
3571 error(ERR_FATAL,
3572 "preprocessor directive `%s' not yet implemented",
H. Peter Anvin4169a472007-09-12 01:29:43 +00003573 pp_directives[i]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003574 return DIRECTIVE_FOUND;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003575 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003576}
3577
3578/*
H. Peter Anvin76690a12002-04-30 20:52:49 +00003579 * Ensure that a macro parameter contains a condition code and
3580 * nothing else. Return the condition code index if so, or -1
3581 * otherwise.
3582 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003583static int find_cc(Token * t)
H. Peter Anvineba20a72002-04-30 20:53:55 +00003584{
H. Peter Anvin76690a12002-04-30 20:52:49 +00003585 Token *tt;
3586 int i, j, k, m;
3587
H. Peter Anvin25a99342007-09-22 17:45:45 -07003588 if (!t)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003589 return -1; /* Probably a %+ without a space */
H. Peter Anvin25a99342007-09-22 17:45:45 -07003590
H. Peter Anvineba20a72002-04-30 20:53:55 +00003591 skip_white_(t);
H. Peter Anvin76690a12002-04-30 20:52:49 +00003592 if (t->type != TOK_ID)
H. Peter Anvine2c80182005-01-15 22:15:51 +00003593 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003594 tt = t->next;
H. Peter Anvineba20a72002-04-30 20:53:55 +00003595 skip_white_(tt);
H. Peter Anvin76690a12002-04-30 20:52:49 +00003596 if (tt && (tt->type != TOK_OTHER || strcmp(tt->text, ",")))
H. Peter Anvine2c80182005-01-15 22:15:51 +00003597 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003598
3599 i = -1;
Cyrill Gorcunova7319242010-06-03 22:04:36 +04003600 j = ARRAY_SIZE(conditions);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003601 while (j - i > 1) {
3602 k = (j + i) / 2;
3603 m = nasm_stricmp(t->text, conditions[k]);
3604 if (m == 0) {
3605 i = k;
3606 j = -2;
3607 break;
3608 } else if (m < 0) {
3609 j = k;
3610 } else
3611 i = k;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003612 }
3613 if (j != -2)
H. Peter Anvine2c80182005-01-15 22:15:51 +00003614 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003615 return i;
3616}
3617
H. Peter Anvind784a082009-04-20 14:01:18 -07003618static bool paste_tokens(Token **head, bool handle_paste_tokens)
3619{
3620 Token **tail, *t, *tt;
3621 Token **paste_head;
3622 bool did_paste = false;
3623 char *tmp;
3624
3625 /* Now handle token pasting... */
3626 paste_head = NULL;
3627 tail = head;
3628 while ((t = *tail) && (tt = t->next)) {
3629 switch (t->type) {
3630 case TOK_WHITESPACE:
3631 if (tt->type == TOK_WHITESPACE) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003632 /* Zap adjacent whitespace tokens */
H. Peter Anvind784a082009-04-20 14:01:18 -07003633 t->next = delete_Token(tt);
3634 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003635 /* Do not advance paste_head here */
3636 tail = &t->next;
3637 }
H. Peter Anvind784a082009-04-20 14:01:18 -07003638 break;
3639 case TOK_ID:
H. Peter Anvind784a082009-04-20 14:01:18 -07003640 case TOK_NUMBER:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003641 case TOK_FLOAT:
3642 {
3643 size_t len = 0;
3644 char *tmp, *p;
H. Peter Anvind784a082009-04-20 14:01:18 -07003645
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003646 while (tt && (tt->type == TOK_ID || tt->type == TOK_PREPROC_ID ||
3647 tt->type == TOK_NUMBER || tt->type == TOK_FLOAT ||
3648 tt->type == TOK_OTHER)) {
3649 len += strlen(tt->text);
3650 tt = tt->next;
3651 }
H. Peter Anvind784a082009-04-20 14:01:18 -07003652
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003653 /*
3654 * Now tt points to the first token after
3655 * the potential paste area...
3656 */
3657 if (tt != t->next) {
3658 /* We have at least two tokens... */
3659 len += strlen(t->text);
3660 p = tmp = nasm_malloc(len+1);
H. Peter Anvind784a082009-04-20 14:01:18 -07003661
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003662 while (t != tt) {
3663 strcpy(p, t->text);
3664 p = strchr(p, '\0');
3665 t = delete_Token(t);
3666 }
H. Peter Anvind784a082009-04-20 14:01:18 -07003667
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003668 t = *tail = tokenize(tmp);
3669 nasm_free(tmp);
H. Peter Anvind784a082009-04-20 14:01:18 -07003670
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003671 while (t->next) {
3672 tail = &t->next;
3673 t = t->next;
3674 }
3675 t->next = tt; /* Attach the remaining token chain */
H. Peter Anvind784a082009-04-20 14:01:18 -07003676
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003677 did_paste = true;
3678 }
3679 paste_head = tail;
3680 tail = &t->next;
3681 break;
3682 }
3683 case TOK_PASTE: /* %+ */
3684 if (handle_paste_tokens) {
3685 /* Zap %+ and whitespace tokens to the right */
3686 while (t && (t->type == TOK_WHITESPACE ||
3687 t->type == TOK_PASTE))
3688 t = *tail = delete_Token(t);
3689 if (!paste_head || !t)
3690 break; /* Nothing to paste with */
3691 tail = paste_head;
3692 t = *tail;
3693 tt = t->next;
3694 while (tok_type_(tt, TOK_WHITESPACE))
3695 tt = t->next = delete_Token(tt);
H. Peter Anvind784a082009-04-20 14:01:18 -07003696
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003697 if (tt) {
3698 tmp = nasm_strcat(t->text, tt->text);
3699 delete_Token(t);
3700 tt = delete_Token(tt);
3701 t = *tail = tokenize(tmp);
3702 nasm_free(tmp);
3703 while (t->next) {
3704 tail = &t->next;
3705 t = t->next;
3706 }
3707 t->next = tt; /* Attach the remaining token chain */
3708 did_paste = true;
3709 }
3710 paste_head = tail;
3711 tail = &t->next;
3712 break;
3713 }
3714 /* else fall through */
3715 default:
Cyrill Gorcunovadabc152010-07-10 02:11:41 +04003716 tail = &t->next;
3717 if (!tok_type_(t->next, TOK_WHITESPACE))
3718 paste_head = tail;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003719 break;
H. Peter Anvind784a082009-04-20 14:01:18 -07003720 }
3721 }
3722 return did_paste;
3723}
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003724
3725/*
3726 * expands to a list of tokens from %{x:y}
3727 */
3728static Token *expand_mmac_params_range(MMacro *mac, Token *tline, Token ***last)
3729{
3730 Token *t = tline, **tt, *tm, *head;
3731 char *pos;
3732 int fst, lst, j, i;
3733
3734 pos = strchr(tline->text, ':');
3735 nasm_assert(pos);
3736
3737 lst = atoi(pos + 1);
3738 fst = atoi(tline->text + 1);
3739
3740 /*
3741 * only macros params are accounted so
3742 * if someone passes %0 -- we reject such
3743 * value(s)
3744 */
3745 if (lst == 0 || fst == 0)
3746 goto err;
3747
3748 /* the values should be sane */
Cyrill Gorcunov2f403752010-06-05 10:47:10 +04003749 if ((fst > (int)mac->nparam || fst < (-(int)mac->nparam)) ||
3750 (lst > (int)mac->nparam || lst < (-(int)mac->nparam)))
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003751 goto err;
3752
Cyrill Gorcunovefb35832010-06-20 01:52:19 +04003753 fst = fst < 0 ? fst + (int)mac->nparam + 1: fst;
3754 lst = lst < 0 ? lst + (int)mac->nparam + 1: lst;
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003755
3756 /* counted from zero */
3757 fst--, lst--;
3758
3759 /*
3760 * it will be at least one token
3761 */
3762 tm = mac->params[(fst + mac->rotate) % mac->nparam];
3763 t = new_Token(NULL, tm->type, tm->text, 0);
3764 head = t, tt = &t->next;
3765 if (fst < lst) {
3766 for (i = fst + 1; i <= lst; i++) {
3767 t = new_Token(NULL, TOK_OTHER, ",", 0);
3768 *tt = t, tt = &t->next;
3769 j = (i + mac->rotate) % mac->nparam;
3770 tm = mac->params[j];
3771 t = new_Token(NULL, tm->type, tm->text, 0);
3772 *tt = t, tt = &t->next;
3773 }
3774 } else {
3775 for (i = fst - 1; i >= lst; i--) {
3776 t = new_Token(NULL, TOK_OTHER, ",", 0);
3777 *tt = t, tt = &t->next;
3778 j = (i + mac->rotate) % mac->nparam;
3779 tm = mac->params[j];
3780 t = new_Token(NULL, tm->type, tm->text, 0);
3781 *tt = t, tt = &t->next;
3782 }
3783 }
3784
3785 *last = tt;
3786 return head;
3787
3788err:
3789 error(ERR_NONFATAL, "`%%{%s}': macro parameters out of range",
3790 &tline->text[1]);
3791 return tline;
3792}
3793
H. Peter Anvin76690a12002-04-30 20:52:49 +00003794/*
3795 * Expand MMacro-local things: parameter references (%0, %n, %+n,
H. Peter Anvin67c63722008-10-26 23:49:00 -07003796 * %-n) and MMacro-local identifiers (%%foo) as well as
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003797 * macro indirection (%[...]) and range (%{..:..}).
H. Peter Anvin76690a12002-04-30 20:52:49 +00003798 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003799static Token *expand_mmac_params(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00003800{
H. Peter Anvin734b1882002-04-30 21:01:08 +00003801 Token *t, *tt, **tail, *thead;
H. Peter Anvin6125b622009-04-08 14:02:25 -07003802 bool changed = false;
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003803 char *pos;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003804
3805 tail = &thead;
3806 thead = NULL;
3807
H. Peter Anvine2c80182005-01-15 22:15:51 +00003808 while (tline) {
3809 if (tline->type == TOK_PREPROC_ID &&
Cyrill Gorcunovca611192010-06-04 09:22:12 +04003810 (((tline->text[1] == '+' || tline->text[1] == '-') && tline->text[2]) ||
3811 (tline->text[1] >= '0' && tline->text[1] <= '9') ||
3812 tline->text[1] == '%')) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00003813 char *text = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003814 int type = 0, cc; /* type = 0 to placate optimisers */
Keith Kaniosa6dfa782007-04-13 16:47:53 +00003815 char tmpbuf[30];
H. Peter Anvin25a99342007-09-22 17:45:45 -07003816 unsigned int n;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003817 int i;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003818 MMacro *mac;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003819
H. Peter Anvine2c80182005-01-15 22:15:51 +00003820 t = tline;
3821 tline = tline->next;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003822
H. Peter Anvine2c80182005-01-15 22:15:51 +00003823 mac = istk->mstk;
3824 while (mac && !mac->name) /* avoid mistaking %reps for macros */
3825 mac = mac->next_active;
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003826 if (!mac) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00003827 error(ERR_NONFATAL, "`%s': not in a macro call", t->text);
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003828 } else {
3829 pos = strchr(t->text, ':');
3830 if (!pos) {
3831 switch (t->text[1]) {
3832 /*
3833 * We have to make a substitution of one of the
3834 * forms %1, %-1, %+1, %%foo, %0.
3835 */
3836 case '0':
3837 type = TOK_NUMBER;
3838 snprintf(tmpbuf, sizeof(tmpbuf), "%d", mac->nparam);
3839 text = nasm_strdup(tmpbuf);
3840 break;
3841 case '%':
H. Peter Anvine2c80182005-01-15 22:15:51 +00003842 type = TOK_ID;
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003843 snprintf(tmpbuf, sizeof(tmpbuf), "..@%"PRIu64".",
3844 mac->unique);
3845 text = nasm_strcat(tmpbuf, t->text + 2);
3846 break;
3847 case '-':
3848 n = atoi(t->text + 2) - 1;
3849 if (n >= mac->nparam)
3850 tt = NULL;
3851 else {
3852 if (mac->nparam > 1)
3853 n = (n + mac->rotate) % mac->nparam;
3854 tt = mac->params[n];
H. Peter Anvine2c80182005-01-15 22:15:51 +00003855 }
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003856 cc = find_cc(tt);
3857 if (cc == -1) {
3858 error(ERR_NONFATAL,
3859 "macro parameter %d is not a condition code",
3860 n + 1);
3861 text = NULL;
3862 } else {
3863 type = TOK_ID;
3864 if (inverse_ccs[cc] == -1) {
3865 error(ERR_NONFATAL,
3866 "condition code `%s' is not invertible",
3867 conditions[cc]);
3868 text = NULL;
3869 } else
3870 text = nasm_strdup(conditions[inverse_ccs[cc]]);
3871 }
3872 break;
3873 case '+':
3874 n = atoi(t->text + 2) - 1;
3875 if (n >= mac->nparam)
3876 tt = NULL;
3877 else {
3878 if (mac->nparam > 1)
3879 n = (n + mac->rotate) % mac->nparam;
3880 tt = mac->params[n];
3881 }
3882 cc = find_cc(tt);
3883 if (cc == -1) {
3884 error(ERR_NONFATAL,
3885 "macro parameter %d is not a condition code",
3886 n + 1);
3887 text = NULL;
3888 } else {
3889 type = TOK_ID;
3890 text = nasm_strdup(conditions[cc]);
3891 }
3892 break;
3893 default:
3894 n = atoi(t->text + 1) - 1;
3895 if (n >= mac->nparam)
3896 tt = NULL;
3897 else {
3898 if (mac->nparam > 1)
3899 n = (n + mac->rotate) % mac->nparam;
3900 tt = mac->params[n];
3901 }
3902 if (tt) {
3903 for (i = 0; i < mac->paramlen[n]; i++) {
3904 *tail = new_Token(NULL, tt->type, tt->text, 0);
3905 tail = &(*tail)->next;
3906 tt = tt->next;
3907 }
3908 }
3909 text = NULL; /* we've done it here */
3910 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003911 }
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003912 } else {
3913 /*
3914 * seems we have a parameters range here
3915 */
3916 Token *head, **last;
3917 head = expand_mmac_params_range(mac, t, &last);
3918 if (head != t) {
3919 *tail = head;
3920 *last = tline;
3921 tline = head;
3922 text = NULL;
3923 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00003924 }
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003925 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00003926 if (!text) {
3927 delete_Token(t);
3928 } else {
3929 *tail = t;
3930 tail = &t->next;
3931 t->type = type;
3932 nasm_free(t->text);
3933 t->text = text;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003934 t->a.mac = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003935 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003936 changed = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003937 continue;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003938 } else if (tline->type == TOK_INDIRECT) {
3939 t = tline;
3940 tline = tline->next;
3941 tt = tokenize(t->text);
3942 tt = expand_mmac_params(tt);
3943 tt = expand_smacro(tt);
3944 *tail = tt;
3945 while (tt) {
3946 tt->a.mac = NULL; /* Necessary? */
3947 tail = &tt->next;
3948 tt = tt->next;
3949 }
3950 delete_Token(t);
3951 changed = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003952 } else {
3953 t = *tail = tline;
3954 tline = tline->next;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003955 t->a.mac = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003956 tail = &t->next;
3957 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00003958 }
H. Peter Anvineba20a72002-04-30 20:53:55 +00003959 *tail = NULL;
H. Peter Anvin67c63722008-10-26 23:49:00 -07003960
H. Peter Anvind784a082009-04-20 14:01:18 -07003961 if (changed)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003962 paste_tokens(&thead, false);
H. Peter Anvin6125b622009-04-08 14:02:25 -07003963
H. Peter Anvin76690a12002-04-30 20:52:49 +00003964 return thead;
3965}
3966
3967/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003968 * Expand all single-line macro calls made in the given line.
3969 * Return the expanded version of the line. The original is deemed
3970 * to be destroyed in the process. (In reality we'll just move
3971 * Tokens from input to output a lot of the time, rather than
3972 * actually bothering to destroy and replicate.)
3973 */
H. Peter Anvincb1cf592007-11-19 12:26:50 -08003974
H. Peter Anvine2c80182005-01-15 22:15:51 +00003975static Token *expand_smacro(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00003976{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003977 Token *t, *tt, *mstart, **tail, *thead;
H. Peter Anvineba20a72002-04-30 20:53:55 +00003978 SMacro *head = NULL, *m;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003979 Token **params;
3980 int *paramsize;
H. Peter Anvin25a99342007-09-22 17:45:45 -07003981 unsigned int nparam, sparam;
H. Peter Anvind784a082009-04-20 14:01:18 -07003982 int brackets;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003983 Token *org_tline = tline;
3984 Context *ctx;
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08003985 const char *mname;
H. Peter Anvin2a15e692007-11-19 13:14:59 -08003986 int deadman = DEADMAN_LIMIT;
H. Peter Anvin8287daf2009-07-07 16:00:58 -07003987 bool expanded;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003988
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003989 /*
3990 * Trick: we should avoid changing the start token pointer since it can
3991 * be contained in "next" field of other token. Because of this
3992 * we allocate a copy of first token and work with it; at the end of
3993 * routine we copy it back
3994 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003995 if (org_tline) {
Cyrill Gorcunoved4a8052010-04-09 15:40:35 +04003996 tline = new_Token(org_tline->next, org_tline->type,
3997 org_tline->text, 0);
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003998 tline->a.mac = org_tline->a.mac;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003999 nasm_free(org_tline->text);
4000 org_tline->text = NULL;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004001 }
4002
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004003 expanded = true; /* Always expand %+ at least once */
H. Peter Anvin8287daf2009-07-07 16:00:58 -07004004
H. Peter Anvincb1cf592007-11-19 12:26:50 -08004005again:
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004006 thead = NULL;
Cyrill Gorcunoved4a8052010-04-09 15:40:35 +04004007 tail = &thead;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004008
H. Peter Anvine2c80182005-01-15 22:15:51 +00004009 while (tline) { /* main token loop */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004010 if (!--deadman) {
4011 error(ERR_NONFATAL, "interminable macro recursion");
Cyrill Gorcunovbd38c8f2009-11-21 11:11:23 +03004012 goto err;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004013 }
H. Peter Anvincb1cf592007-11-19 12:26:50 -08004014
H. Peter Anvine2c80182005-01-15 22:15:51 +00004015 if ((mname = tline->text)) {
4016 /* if this token is a local macro, look in local context */
Cyrill Gorcunovc56d9ad2010-02-11 15:12:19 +03004017 if (tline->type == TOK_ID) {
4018 head = (SMacro *)hash_findix(&smacros, mname);
4019 } else if (tline->type == TOK_PREPROC_ID) {
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08004020 ctx = get_ctx(mname, &mname, true);
Cyrill Gorcunovc56d9ad2010-02-11 15:12:19 +03004021 head = ctx ? (SMacro *)hash_findix(&ctx->localmac, mname) : NULL;
4022 } else
4023 head = NULL;
H. Peter Anvin072771e2008-05-22 13:17:51 -07004024
H. Peter Anvine2c80182005-01-15 22:15:51 +00004025 /*
4026 * We've hit an identifier. As in is_mmacro below, we first
4027 * check whether the identifier is a single-line macro at
4028 * all, then think about checking for parameters if
4029 * necessary.
4030 */
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04004031 list_for_each(m, head)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004032 if (!mstrcmp(m->name, mname, m->casesense))
4033 break;
4034 if (m) {
4035 mstart = tline;
4036 params = NULL;
4037 paramsize = NULL;
4038 if (m->nparam == 0) {
4039 /*
4040 * Simple case: the macro is parameterless. Discard the
4041 * one token that the macro call took, and push the
4042 * expansion back on the to-do stack.
4043 */
4044 if (!m->expansion) {
4045 if (!strcmp("__FILE__", m->name)) {
4046 int32_t num = 0;
4047 char *file = NULL;
4048 src_get(&num, &file);
4049 tline->text = nasm_quote(file, strlen(file));
4050 tline->type = TOK_STRING;
4051 nasm_free(file);
H. Peter Anvine2c80182005-01-15 22:15:51 +00004052 continue;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004053 }
4054 if (!strcmp("__LINE__", m->name)) {
4055 nasm_free(tline->text);
4056 make_tok_num(tline, src_get_linnum());
4057 continue;
4058 }
4059 if (!strcmp("__BITS__", m->name)) {
4060 nasm_free(tline->text);
4061 make_tok_num(tline, globalbits);
4062 continue;
4063 }
4064 tline = delete_Token(tline);
4065 continue;
4066 }
4067 } else {
4068 /*
4069 * Complicated case: at least one macro with this name
H. Peter Anvine2c80182005-01-15 22:15:51 +00004070 * exists and takes parameters. We must find the
4071 * parameters in the call, count them, find the SMacro
4072 * that corresponds to that form of the macro call, and
4073 * substitute for the parameters when we expand. What a
4074 * pain.
4075 */
4076 /*tline = tline->next;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004077 skip_white_(tline); */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004078 do {
4079 t = tline->next;
4080 while (tok_type_(t, TOK_SMAC_END)) {
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004081 t->a.mac->in_progress = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004082 t->text = NULL;
4083 t = tline->next = delete_Token(t);
4084 }
4085 tline = t;
4086 } while (tok_type_(tline, TOK_WHITESPACE));
4087 if (!tok_is_(tline, "(")) {
4088 /*
4089 * This macro wasn't called with parameters: ignore
4090 * the call. (Behaviour borrowed from gnu cpp.)
4091 */
4092 tline = mstart;
4093 m = NULL;
4094 } else {
4095 int paren = 0;
4096 int white = 0;
4097 brackets = 0;
4098 nparam = 0;
4099 sparam = PARAM_DELTA;
4100 params = nasm_malloc(sparam * sizeof(Token *));
4101 params[0] = tline->next;
4102 paramsize = nasm_malloc(sparam * sizeof(int));
4103 paramsize[0] = 0;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07004104 while (true) { /* parameter loop */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004105 /*
4106 * For some unusual expansions
4107 * which concatenates function call
4108 */
4109 t = tline->next;
4110 while (tok_type_(t, TOK_SMAC_END)) {
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004111 t->a.mac->in_progress = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004112 t->text = NULL;
4113 t = tline->next = delete_Token(t);
4114 }
4115 tline = t;
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00004116
H. Peter Anvine2c80182005-01-15 22:15:51 +00004117 if (!tline) {
4118 error(ERR_NONFATAL,
4119 "macro call expects terminating `)'");
4120 break;
4121 }
4122 if (tline->type == TOK_WHITESPACE
4123 && brackets <= 0) {
4124 if (paramsize[nparam])
4125 white++;
4126 else
4127 params[nparam] = tline->next;
4128 continue; /* parameter loop */
4129 }
4130 if (tline->type == TOK_OTHER
4131 && tline->text[1] == 0) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004132 char ch = tline->text[0];
H. Peter Anvine2c80182005-01-15 22:15:51 +00004133 if (ch == ',' && !paren && brackets <= 0) {
4134 if (++nparam >= sparam) {
4135 sparam += PARAM_DELTA;
4136 params = nasm_realloc(params,
Cyrill Gorcunoved4a8052010-04-09 15:40:35 +04004137 sparam * sizeof(Token *));
4138 paramsize = nasm_realloc(paramsize,
4139 sparam * sizeof(int));
H. Peter Anvine2c80182005-01-15 22:15:51 +00004140 }
4141 params[nparam] = tline->next;
4142 paramsize[nparam] = 0;
4143 white = 0;
4144 continue; /* parameter loop */
4145 }
4146 if (ch == '{' &&
4147 (brackets > 0 || (brackets == 0 &&
4148 !paramsize[nparam])))
4149 {
4150 if (!(brackets++)) {
4151 params[nparam] = tline->next;
4152 continue; /* parameter loop */
4153 }
4154 }
4155 if (ch == '}' && brackets > 0)
4156 if (--brackets == 0) {
4157 brackets = -1;
4158 continue; /* parameter loop */
4159 }
4160 if (ch == '(' && !brackets)
4161 paren++;
4162 if (ch == ')' && brackets <= 0)
4163 if (--paren < 0)
4164 break;
4165 }
4166 if (brackets < 0) {
4167 brackets = 0;
4168 error(ERR_NONFATAL, "braces do not "
4169 "enclose all of macro parameter");
4170 }
4171 paramsize[nparam] += white + 1;
4172 white = 0;
4173 } /* parameter loop */
4174 nparam++;
4175 while (m && (m->nparam != nparam ||
4176 mstrcmp(m->name, mname,
4177 m->casesense)))
4178 m = m->next;
4179 if (!m)
H. Peter Anvin917a3492008-09-24 09:14:49 -07004180 error(ERR_WARNING|ERR_PASS1|ERR_WARN_MNP,
H. Peter Anvine2c80182005-01-15 22:15:51 +00004181 "macro `%s' exists, "
4182 "but not taking %d parameters",
4183 mstart->text, nparam);
4184 }
4185 }
4186 if (m && m->in_progress)
4187 m = NULL;
4188 if (!m) { /* in progess or didn't find '(' or wrong nparam */
H. Peter Anvin70653092007-10-19 14:42:29 -07004189 /*
H. Peter Anvine2c80182005-01-15 22:15:51 +00004190 * Design question: should we handle !tline, which
4191 * indicates missing ')' here, or expand those
4192 * macros anyway, which requires the (t) test a few
H. Peter Anvin70653092007-10-19 14:42:29 -07004193 * lines down?
H. Peter Anvine2c80182005-01-15 22:15:51 +00004194 */
4195 nasm_free(params);
4196 nasm_free(paramsize);
4197 tline = mstart;
4198 } else {
4199 /*
4200 * Expand the macro: we are placed on the last token of the
4201 * call, so that we can easily split the call from the
4202 * following tokens. We also start by pushing an SMAC_END
4203 * token for the cycle removal.
4204 */
4205 t = tline;
4206 if (t) {
4207 tline = t->next;
4208 t->next = NULL;
4209 }
4210 tt = new_Token(tline, TOK_SMAC_END, NULL, 0);
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004211 tt->a.mac = m;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07004212 m->in_progress = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004213 tline = tt;
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04004214 list_for_each(t, m->expansion) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00004215 if (t->type >= TOK_SMAC_PARAM) {
4216 Token *pcopy = tline, **ptail = &pcopy;
4217 Token *ttt, *pt;
4218 int i;
H. Peter Anvin734b1882002-04-30 21:01:08 +00004219
H. Peter Anvine2c80182005-01-15 22:15:51 +00004220 ttt = params[t->type - TOK_SMAC_PARAM];
Cyrill Gorcunoved4a8052010-04-09 15:40:35 +04004221 i = paramsize[t->type - TOK_SMAC_PARAM];
4222 while (--i >= 0) {
4223 pt = *ptail = new_Token(tline, ttt->type,
4224 ttt->text, 0);
H. Peter Anvine2c80182005-01-15 22:15:51 +00004225 ptail = &pt->next;
4226 ttt = ttt->next;
4227 }
4228 tline = pcopy;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004229 } else if (t->type == TOK_PREPROC_Q) {
4230 tt = new_Token(tline, TOK_ID, mname, 0);
4231 tline = tt;
4232 } else if (t->type == TOK_PREPROC_QQ) {
4233 tt = new_Token(tline, TOK_ID, m->name, 0);
4234 tline = tt;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004235 } else {
4236 tt = new_Token(tline, t->type, t->text, 0);
4237 tline = tt;
4238 }
4239 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00004240
H. Peter Anvine2c80182005-01-15 22:15:51 +00004241 /*
4242 * Having done that, get rid of the macro call, and clean
4243 * up the parameters.
4244 */
4245 nasm_free(params);
4246 nasm_free(paramsize);
4247 free_tlist(mstart);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004248 expanded = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004249 continue; /* main token loop */
4250 }
4251 }
4252 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004253
H. Peter Anvine2c80182005-01-15 22:15:51 +00004254 if (tline->type == TOK_SMAC_END) {
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004255 tline->a.mac->in_progress = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004256 tline = delete_Token(tline);
4257 } else {
4258 t = *tail = tline;
4259 tline = tline->next;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004260 t->a.mac = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004261 t->next = NULL;
4262 tail = &t->next;
4263 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004264 }
4265
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004266 /*
4267 * Now scan the entire line and look for successive TOK_IDs that resulted
Keith Kaniosb7a89542007-04-12 02:40:54 +00004268 * after expansion (they can't be produced by tokenize()). The successive
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004269 * TOK_IDs should be concatenated.
4270 * Also we look for %+ tokens and concatenate the tokens before and after
4271 * them (without white spaces in between).
4272 */
H. Peter Anvin8287daf2009-07-07 16:00:58 -07004273 if (expanded && paste_tokens(&thead, true)) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004274 /*
4275 * If we concatenated something, *and* we had previously expanded
4276 * an actual macro, scan the lines again for macros...
4277 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004278 tline = thead;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004279 expanded = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004280 goto again;
H. Peter Anvin734b1882002-04-30 21:01:08 +00004281 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004282
Cyrill Gorcunovbd38c8f2009-11-21 11:11:23 +03004283err:
H. Peter Anvine2c80182005-01-15 22:15:51 +00004284 if (org_tline) {
4285 if (thead) {
4286 *org_tline = *thead;
4287 /* since we just gave text to org_line, don't free it */
4288 thead->text = NULL;
4289 delete_Token(thead);
4290 } else {
4291 /* the expression expanded to empty line;
4292 we can't return NULL for some reasons
4293 we just set the line to a single WHITESPACE token. */
4294 memset(org_tline, 0, sizeof(*org_tline));
4295 org_tline->text = NULL;
4296 org_tline->type = TOK_WHITESPACE;
4297 }
4298 thead = org_tline;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004299 }
4300
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004301 return thead;
4302}
4303
4304/*
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004305 * Similar to expand_smacro but used exclusively with macro identifiers
4306 * right before they are fetched in. The reason is that there can be
4307 * identifiers consisting of several subparts. We consider that if there
4308 * are more than one element forming the name, user wants a expansion,
4309 * otherwise it will be left as-is. Example:
4310 *
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004311 * %define %$abc cde
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004312 *
4313 * the identifier %$abc will be left as-is so that the handler for %define
4314 * will suck it and define the corresponding value. Other case:
4315 *
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004316 * %define _%$abc cde
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004317 *
4318 * In this case user wants name to be expanded *before* %define starts
4319 * working, so we'll expand %$abc into something (if it has a value;
4320 * otherwise it will be left as-is) then concatenate all successive
4321 * PP_IDs into one.
4322 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004323static Token *expand_id(Token * tline)
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004324{
4325 Token *cur, *oldnext = NULL;
4326
H. Peter Anvin734b1882002-04-30 21:01:08 +00004327 if (!tline || !tline->next)
H. Peter Anvine2c80182005-01-15 22:15:51 +00004328 return tline;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004329
4330 cur = tline;
4331 while (cur->next &&
H. Peter Anvine2c80182005-01-15 22:15:51 +00004332 (cur->next->type == TOK_ID ||
4333 cur->next->type == TOK_PREPROC_ID
4334 || cur->next->type == TOK_NUMBER))
4335 cur = cur->next;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004336
4337 /* If identifier consists of just one token, don't expand */
4338 if (cur == tline)
H. Peter Anvine2c80182005-01-15 22:15:51 +00004339 return tline;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004340
H. Peter Anvine2c80182005-01-15 22:15:51 +00004341 if (cur) {
4342 oldnext = cur->next; /* Detach the tail past identifier */
4343 cur->next = NULL; /* so that expand_smacro stops here */
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004344 }
4345
H. Peter Anvin734b1882002-04-30 21:01:08 +00004346 tline = expand_smacro(tline);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004347
H. Peter Anvine2c80182005-01-15 22:15:51 +00004348 if (cur) {
4349 /* expand_smacro possibly changhed tline; re-scan for EOL */
4350 cur = tline;
4351 while (cur && cur->next)
4352 cur = cur->next;
4353 if (cur)
4354 cur->next = oldnext;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004355 }
4356
4357 return tline;
4358}
4359
4360/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004361 * Determine whether the given line constitutes a multi-line macro
4362 * call, and return the MMacro structure called if so. Doesn't have
4363 * to check for an initial label - that's taken care of in
4364 * expand_mmacro - but must check numbers of parameters. Guaranteed
4365 * to be called with tline->type == TOK_ID, so the putative macro
4366 * name is easy to find.
4367 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004368static MMacro *is_mmacro(Token * tline, Token *** params_array)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004369{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004370 MMacro *head, *m;
4371 Token **params;
4372 int nparam;
4373
H. Peter Anvin166c2472008-05-28 12:28:58 -07004374 head = (MMacro *) hash_findix(&mmacros, tline->text);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004375
4376 /*
4377 * Efficiency: first we see if any macro exists with the given
4378 * name. If not, we can return NULL immediately. _Then_ we
4379 * count the parameters, and then we look further along the
4380 * list if necessary to find the proper MMacro.
4381 */
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04004382 list_for_each(m, head)
H. Peter Anvine2c80182005-01-15 22:15:51 +00004383 if (!mstrcmp(m->name, tline->text, m->casesense))
4384 break;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004385 if (!m)
H. Peter Anvine2c80182005-01-15 22:15:51 +00004386 return NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004387
4388 /*
4389 * OK, we have a potential macro. Count and demarcate the
4390 * parameters.
4391 */
H. Peter Anvin734b1882002-04-30 21:01:08 +00004392 count_mmac_params(tline->next, &nparam, &params);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004393
4394 /*
4395 * So we know how many parameters we've got. Find the MMacro
4396 * structure that handles this number.
4397 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004398 while (m) {
4399 if (m->nparam_min <= nparam
4400 && (m->plus || nparam <= m->nparam_max)) {
4401 /*
4402 * This one is right. Just check if cycle removal
4403 * prohibits us using it before we actually celebrate...
4404 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004405 if (m->in_progress > m->max_depth) {
4406 if (m->max_depth > 0) {
4407 error(ERR_WARNING,
4408 "reached maximum recursion depth of %i",
4409 m->max_depth);
4410 }
4411 nasm_free(params);
4412 return NULL;
4413 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00004414 /*
4415 * It's right, and we can use it. Add its default
4416 * parameters to the end of our list if necessary.
4417 */
4418 if (m->defaults && nparam < m->nparam_min + m->ndefs) {
4419 params =
4420 nasm_realloc(params,
4421 ((m->nparam_min + m->ndefs +
4422 1) * sizeof(*params)));
4423 while (nparam < m->nparam_min + m->ndefs) {
4424 params[nparam] = m->defaults[nparam - m->nparam_min];
4425 nparam++;
4426 }
4427 }
4428 /*
4429 * If we've gone over the maximum parameter count (and
4430 * we're in Plus mode), ignore parameters beyond
4431 * nparam_max.
4432 */
4433 if (m->plus && nparam > m->nparam_max)
4434 nparam = m->nparam_max;
4435 /*
4436 * Then terminate the parameter list, and leave.
4437 */
4438 if (!params) { /* need this special case */
4439 params = nasm_malloc(sizeof(*params));
4440 nparam = 0;
4441 }
4442 params[nparam] = NULL;
4443 *params_array = params;
4444 return m;
4445 }
4446 /*
4447 * This one wasn't right: look for the next one with the
4448 * same name.
4449 */
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04004450 list_for_each(m, m->next)
H. Peter Anvine2c80182005-01-15 22:15:51 +00004451 if (!mstrcmp(m->name, tline->text, m->casesense))
4452 break;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004453 }
4454
4455 /*
4456 * After all that, we didn't find one with the right number of
4457 * parameters. Issue a warning, and fail to expand the macro.
4458 */
H. Peter Anvin917a3492008-09-24 09:14:49 -07004459 error(ERR_WARNING|ERR_PASS1|ERR_WARN_MNP,
H. Peter Anvine2c80182005-01-15 22:15:51 +00004460 "macro `%s' exists, but not taking %d parameters",
4461 tline->text, nparam);
H. Peter Anvin734b1882002-04-30 21:01:08 +00004462 nasm_free(params);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004463 return NULL;
4464}
4465
Keith Kanios891775e2009-07-11 06:08:54 -05004466
4467/*
4468 * Save MMacro invocation specific fields in
4469 * preparation for a recursive macro expansion
4470 */
4471static void push_mmacro(MMacro *m)
4472{
4473 MMacroInvocation *i;
4474
H. Peter Anvin89cee572009-07-15 09:16:54 -04004475 i = nasm_malloc(sizeof(MMacroInvocation));
4476 i->prev = m->prev;
4477 i->params = m->params;
4478 i->iline = m->iline;
4479 i->nparam = m->nparam;
4480 i->rotate = m->rotate;
4481 i->paramlen = m->paramlen;
4482 i->unique = m->unique;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004483 i->condcnt = m->condcnt;
H. Peter Anvin89cee572009-07-15 09:16:54 -04004484 m->prev = i;
Keith Kanios891775e2009-07-11 06:08:54 -05004485}
4486
4487
4488/*
4489 * Restore MMacro invocation specific fields that were
4490 * saved during a previous recursive macro expansion
4491 */
4492static void pop_mmacro(MMacro *m)
4493{
4494 MMacroInvocation *i;
4495
H. Peter Anvin89cee572009-07-15 09:16:54 -04004496 if (m->prev) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004497 i = m->prev;
4498 m->prev = i->prev;
4499 m->params = i->params;
4500 m->iline = i->iline;
4501 m->nparam = i->nparam;
4502 m->rotate = i->rotate;
4503 m->paramlen = i->paramlen;
4504 m->unique = i->unique;
4505 m->condcnt = i->condcnt;
4506 nasm_free(i);
H. Peter Anvin89cee572009-07-15 09:16:54 -04004507 }
Keith Kanios891775e2009-07-11 06:08:54 -05004508}
4509
4510
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004511/*
4512 * Expand the multi-line macro call made by the given line, if
4513 * there is one to be expanded. If there is, push the expansion on
H. Peter Anvineba20a72002-04-30 20:53:55 +00004514 * istk->expansion and return 1. Otherwise return 0.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004515 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004516static int expand_mmacro(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004517{
4518 Token *startline = tline;
4519 Token *label = NULL;
4520 int dont_prepend = 0;
H. Peter Anvince2233b2008-05-25 21:57:00 -07004521 Token **params, *t, *mtok, *tt;
H. Peter Anvineba20a72002-04-30 20:53:55 +00004522 MMacro *m;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004523 Line *l, *ll;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004524 int i, nparam, *paramlen;
H. Peter Anvinc751e862008-06-09 10:18:45 -07004525 const char *mname;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004526
4527 t = tline;
H. Peter Anvineba20a72002-04-30 20:53:55 +00004528 skip_white_(t);
H. Peter Anvince2233b2008-05-25 21:57:00 -07004529 /* if (!tok_type_(t, TOK_ID)) Lino 02/25/02 */
H. Peter Anvindce1e2f2002-04-30 21:06:37 +00004530 if (!tok_type_(t, TOK_ID) && !tok_type_(t, TOK_PREPROC_ID))
H. Peter Anvine2c80182005-01-15 22:15:51 +00004531 return 0;
H. Peter Anvince2233b2008-05-25 21:57:00 -07004532 mtok = t;
H. Peter Anvin734b1882002-04-30 21:01:08 +00004533 m = is_mmacro(t, &params);
H. Peter Anvinc751e862008-06-09 10:18:45 -07004534 if (m) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004535 mname = t->text;
H. Peter Anvinc751e862008-06-09 10:18:45 -07004536 } else {
H. Peter Anvine2c80182005-01-15 22:15:51 +00004537 Token *last;
4538 /*
4539 * We have an id which isn't a macro call. We'll assume
4540 * it might be a label; we'll also check to see if a
4541 * colon follows it. Then, if there's another id after
4542 * that lot, we'll check it again for macro-hood.
4543 */
4544 label = last = t;
4545 t = t->next;
4546 if (tok_type_(t, TOK_WHITESPACE))
4547 last = t, t = t->next;
4548 if (tok_is_(t, ":")) {
4549 dont_prepend = 1;
4550 last = t, t = t->next;
4551 if (tok_type_(t, TOK_WHITESPACE))
4552 last = t, t = t->next;
4553 }
H. Peter Anvin89cee572009-07-15 09:16:54 -04004554 if (!tok_type_(t, TOK_ID) || !(m = is_mmacro(t, &params)))
H. Peter Anvine2c80182005-01-15 22:15:51 +00004555 return 0;
4556 last->next = NULL;
Keith Kanios891775e2009-07-11 06:08:54 -05004557 mname = t->text;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004558 tline = t;
H. Peter Anvineba20a72002-04-30 20:53:55 +00004559 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004560
4561 /*
4562 * Fix up the parameters: this involves stripping leading and
4563 * trailing whitespace, then stripping braces if they are
4564 * present.
4565 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004566 for (nparam = 0; params[nparam]; nparam++) ;
H. Peter Anvin734b1882002-04-30 21:01:08 +00004567 paramlen = nparam ? nasm_malloc(nparam * sizeof(*paramlen)) : NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004568
H. Peter Anvine2c80182005-01-15 22:15:51 +00004569 for (i = 0; params[i]; i++) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07004570 int brace = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004571 int comma = (!m->plus || i < nparam - 1);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004572
H. Peter Anvine2c80182005-01-15 22:15:51 +00004573 t = params[i];
4574 skip_white_(t);
4575 if (tok_is_(t, "{"))
H. Peter Anvin6867acc2007-10-10 14:58:45 -07004576 t = t->next, brace = true, comma = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004577 params[i] = t;
4578 paramlen[i] = 0;
4579 while (t) {
4580 if (comma && t->type == TOK_OTHER && !strcmp(t->text, ","))
4581 break; /* ... because we have hit a comma */
4582 if (comma && t->type == TOK_WHITESPACE
4583 && tok_is_(t->next, ","))
4584 break; /* ... or a space then a comma */
4585 if (brace && t->type == TOK_OTHER && !strcmp(t->text, "}"))
4586 break; /* ... or a brace */
4587 t = t->next;
4588 paramlen[i]++;
4589 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004590 }
4591
4592 /*
4593 * OK, we have a MMacro structure together with a set of
4594 * parameters. We must now go through the expansion and push
H. Peter Anvin76690a12002-04-30 20:52:49 +00004595 * copies of each Line on to istk->expansion. Substitution of
4596 * parameter tokens and macro-local tokens doesn't get done
4597 * until the single-line macro substitution process; this is
4598 * because delaying them allows us to change the semantics
4599 * later through %rotate.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004600 *
H. Peter Anvin76690a12002-04-30 20:52:49 +00004601 * First, push an end marker on to istk->expansion, mark this
4602 * macro as in progress, and set up its invocation-specific
4603 * variables.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004604 */
4605 ll = nasm_malloc(sizeof(Line));
4606 ll->next = istk->expansion;
4607 ll->finishes = m;
4608 ll->first = NULL;
4609 istk->expansion = ll;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004610
H. Peter Anvin89cee572009-07-15 09:16:54 -04004611 /*
4612 * Save the previous MMacro expansion in the case of
4613 * macro recursion
4614 */
4615 if (m->max_depth && m->in_progress)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004616 push_mmacro(m);
H. Peter Anvin76690a12002-04-30 20:52:49 +00004617
Keith Kanios891775e2009-07-11 06:08:54 -05004618 m->in_progress ++;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004619 m->params = params;
4620 m->iline = tline;
4621 m->nparam = nparam;
4622 m->rotate = 0;
4623 m->paramlen = paramlen;
4624 m->unique = unique++;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004625 m->lineno = 0;
Keith Kanios3c0d91f2009-10-25 13:28:03 -05004626 m->condcnt = 0;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004627
4628 m->next_active = istk->mstk;
4629 istk->mstk = m;
4630
Cyrill Gorcunov367d59e2010-04-09 15:43:03 +04004631 list_for_each(l, m->expansion) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00004632 Token **tail;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004633
H. Peter Anvine2c80182005-01-15 22:15:51 +00004634 ll = nasm_malloc(sizeof(Line));
4635 ll->finishes = NULL;
4636 ll->next = istk->expansion;
4637 istk->expansion = ll;
4638 tail = &ll->first;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004639
Cyrill Gorcunov367d59e2010-04-09 15:43:03 +04004640 list_for_each(t, l->first) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00004641 Token *x = t;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004642 switch (t->type) {
4643 case TOK_PREPROC_Q:
4644 tt = *tail = new_Token(NULL, TOK_ID, mname, 0);
4645 break;
4646 case TOK_PREPROC_QQ:
4647 tt = *tail = new_Token(NULL, TOK_ID, m->name, 0);
4648 break;
4649 case TOK_PREPROC_ID:
4650 if (t->text[1] == '0' && t->text[2] == '0') {
4651 dont_prepend = -1;
4652 x = label;
4653 if (!x)
4654 continue;
4655 }
4656 /* fall through */
4657 default:
4658 tt = *tail = new_Token(NULL, x->type, x->text, 0);
4659 break;
4660 }
4661 tail = &tt->next;
4662 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00004663 *tail = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004664 }
4665
4666 /*
H. Peter Anvineba20a72002-04-30 20:53:55 +00004667 * If we had a label, push it on as the first line of
4668 * the macro expansion.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004669 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004670 if (label) {
4671 if (dont_prepend < 0)
4672 free_tlist(startline);
4673 else {
4674 ll = nasm_malloc(sizeof(Line));
4675 ll->finishes = NULL;
4676 ll->next = istk->expansion;
4677 istk->expansion = ll;
4678 ll->first = startline;
4679 if (!dont_prepend) {
4680 while (label->next)
4681 label = label->next;
4682 label->next = tt = new_Token(NULL, TOK_OTHER, ":", 0);
4683 }
4684 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004685 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004686
H. Peter Anvin734b1882002-04-30 21:01:08 +00004687 list->uplevel(m->nolist ? LIST_MACRO_NOLIST : LIST_MACRO);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004688
H. Peter Anvineba20a72002-04-30 20:53:55 +00004689 return 1;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004690}
4691
Victor van den Elzen3b404c02008-09-18 13:51:36 +02004692/* The function that actually does the error reporting */
4693static void verror(int severity, const char *fmt, va_list arg)
4694{
4695 char buff[1024];
4696
4697 vsnprintf(buff, sizeof(buff), fmt, arg);
4698
4699 if (istk && istk->mstk && istk->mstk->name)
H. Peter Anvindbb640b2009-07-18 18:57:16 -07004700 nasm_error(severity, "(%s:%d) %s", istk->mstk->name,
Victor van den Elzen3b404c02008-09-18 13:51:36 +02004701 istk->mstk->lineno, buff);
4702 else
H. Peter Anvindbb640b2009-07-18 18:57:16 -07004703 nasm_error(severity, "%s", buff);
Victor van den Elzen3b404c02008-09-18 13:51:36 +02004704}
4705
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004706/*
4707 * Since preprocessor always operate only on the line that didn't
H. Peter Anvin917a3492008-09-24 09:14:49 -07004708 * arrived yet, we should always use ERR_OFFBY1.
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004709 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004710static void error(int severity, const char *fmt, ...)
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004711{
4712 va_list arg;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004713
4714 /* If we're in a dead branch of IF or something like it, ignore the error */
H. Peter Anvin77ba0c62002-05-14 03:18:53 +00004715 if (istk && istk->conds && !emitting(istk->conds->state))
H. Peter Anvine2c80182005-01-15 22:15:51 +00004716 return;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004717
H. Peter Anvin734b1882002-04-30 21:01:08 +00004718 va_start(arg, fmt);
Victor van den Elzen3b404c02008-09-18 13:51:36 +02004719 verror(severity, fmt, arg);
H. Peter Anvin734b1882002-04-30 21:01:08 +00004720 va_end(arg);
Victor van den Elzen3b404c02008-09-18 13:51:36 +02004721}
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004722
Victor van den Elzen3b404c02008-09-18 13:51:36 +02004723/*
4724 * Because %else etc are evaluated in the state context
4725 * of the previous branch, errors might get lost with error():
4726 * %if 0 ... %else trailing garbage ... %endif
4727 * So %else etc should report errors with this function.
4728 */
4729static void error_precond(int severity, const char *fmt, ...)
4730{
4731 va_list arg;
4732
4733 /* Only ignore the error if it's really in a dead branch */
4734 if (istk && istk->conds && istk->conds->state == COND_NEVER)
4735 return;
4736
4737 va_start(arg, fmt);
4738 verror(severity, fmt, arg);
4739 va_end(arg);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004740}
4741
H. Peter Anvin734b1882002-04-30 21:01:08 +00004742static void
H. Peter Anvindbb640b2009-07-18 18:57:16 -07004743pp_reset(char *file, int apass, ListGen * listgen, StrList **deplist)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004744{
H. Peter Anvin7383b402008-09-24 10:20:40 -07004745 Token *t;
4746
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004747 cstk = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004748 istk = nasm_malloc(sizeof(Include));
4749 istk->next = NULL;
4750 istk->conds = NULL;
4751 istk->expansion = NULL;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004752 istk->mstk = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004753 istk->fp = fopen(file, "r");
H. Peter Anvineba20a72002-04-30 20:53:55 +00004754 istk->fname = NULL;
4755 src_set_fname(nasm_strdup(file));
4756 src_set_linnum(0);
4757 istk->lineinc = 1;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004758 if (!istk->fp)
H. Peter Anvin917a3492008-09-24 09:14:49 -07004759 error(ERR_FATAL|ERR_NOFILE, "unable to open input file `%s'",
H. Peter Anvine2c80182005-01-15 22:15:51 +00004760 file);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004761 defining = NULL;
Charles Crayned4200be2008-07-12 16:42:33 -07004762 nested_mac_count = 0;
4763 nested_rep_count = 0;
H. Peter Anvin97a23472007-09-16 17:57:25 -07004764 init_macros();
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004765 unique = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004766 if (tasm_compatible_mode) {
H. Peter Anvina4835d42008-05-20 14:21:29 -07004767 stdmacpos = nasm_stdmac;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004768 } else {
H. Peter Anvina4835d42008-05-20 14:21:29 -07004769 stdmacpos = nasm_stdmac_after_tasm;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004770 }
H. Peter Anvind2456592008-06-19 15:04:18 -07004771 any_extrastdmac = extrastdmac && *extrastdmac;
4772 do_predef = true;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004773 list = listgen;
H. Peter Anvin61f130f2008-09-25 15:45:06 -07004774
4775 /*
4776 * 0 for dependencies, 1 for preparatory passes, 2 for final pass.
4777 * The caller, however, will also pass in 3 for preprocess-only so
4778 * we can set __PASS__ accordingly.
4779 */
4780 pass = apass > 2 ? 2 : apass;
4781
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07004782 dephead = deptail = deplist;
4783 if (deplist) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004784 StrList *sl = nasm_malloc(strlen(file)+1+sizeof sl->next);
4785 sl->next = NULL;
4786 strcpy(sl->str, file);
4787 *deptail = sl;
4788 deptail = &sl->next;
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07004789 }
H. Peter Anvin7383b402008-09-24 10:20:40 -07004790
H. Peter Anvin61f130f2008-09-25 15:45:06 -07004791 /*
4792 * Define the __PASS__ macro. This is defined here unlike
4793 * all the other builtins, because it is special -- it varies between
4794 * passes.
4795 */
H. Peter Anvin7383b402008-09-24 10:20:40 -07004796 t = nasm_malloc(sizeof(*t));
4797 t->next = NULL;
H. Peter Anvin61f130f2008-09-25 15:45:06 -07004798 make_tok_num(t, apass);
H. Peter Anvin7383b402008-09-24 10:20:40 -07004799 t->a.mac = NULL;
4800 define_smacro(NULL, "__PASS__", true, 0, t);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004801}
4802
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004803static char *pp_getline(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004804{
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004805 char *line;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004806 Token *tline;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004807
H. Peter Anvine2c80182005-01-15 22:15:51 +00004808 while (1) {
4809 /*
Keith Kaniosb7a89542007-04-12 02:40:54 +00004810 * Fetch a tokenized line, either from the macro-expansion
H. Peter Anvine2c80182005-01-15 22:15:51 +00004811 * buffer or from the input file.
4812 */
4813 tline = NULL;
4814 while (istk->expansion && istk->expansion->finishes) {
4815 Line *l = istk->expansion;
4816 if (!l->finishes->name && l->finishes->in_progress > 1) {
4817 Line *ll;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004818
H. Peter Anvine2c80182005-01-15 22:15:51 +00004819 /*
4820 * This is a macro-end marker for a macro with no
4821 * name, which means it's not really a macro at all
4822 * but a %rep block, and the `in_progress' field is
4823 * more than 1, meaning that we still need to
4824 * repeat. (1 means the natural last repetition; 0
4825 * means termination by %exitrep.) We have
4826 * therefore expanded up to the %endrep, and must
4827 * push the whole block on to the expansion buffer
4828 * again. We don't bother to remove the macro-end
4829 * marker: we'd only have to generate another one
4830 * if we did.
4831 */
4832 l->finishes->in_progress--;
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04004833 list_for_each(l, l->finishes->expansion) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00004834 Token *t, *tt, **tail;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004835
H. Peter Anvine2c80182005-01-15 22:15:51 +00004836 ll = nasm_malloc(sizeof(Line));
4837 ll->next = istk->expansion;
4838 ll->finishes = NULL;
4839 ll->first = NULL;
4840 tail = &ll->first;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004841
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04004842 list_for_each(t, l->first) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00004843 if (t->text || t->type == TOK_WHITESPACE) {
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04004844 tt = *tail = new_Token(NULL, t->type, t->text, 0);
H. Peter Anvine2c80182005-01-15 22:15:51 +00004845 tail = &tt->next;
4846 }
4847 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00004848
H. Peter Anvine2c80182005-01-15 22:15:51 +00004849 istk->expansion = ll;
4850 }
4851 } else {
4852 /*
4853 * Check whether a `%rep' was started and not ended
4854 * within this macro expansion. This can happen and
4855 * should be detected. It's a fatal error because
4856 * I'm too confused to work out how to recover
4857 * sensibly from it.
4858 */
4859 if (defining) {
4860 if (defining->name)
4861 error(ERR_PANIC,
4862 "defining with name in expansion");
4863 else if (istk->mstk->name)
4864 error(ERR_FATAL,
4865 "`%%rep' without `%%endrep' within"
4866 " expansion of macro `%s'",
4867 istk->mstk->name);
4868 }
H. Peter Anvin87bc6192002-04-30 20:53:16 +00004869
H. Peter Anvine2c80182005-01-15 22:15:51 +00004870 /*
4871 * FIXME: investigate the relationship at this point between
4872 * istk->mstk and l->finishes
4873 */
4874 {
4875 MMacro *m = istk->mstk;
4876 istk->mstk = m->next_active;
4877 if (m->name) {
4878 /*
4879 * This was a real macro call, not a %rep, and
4880 * therefore the parameter information needs to
4881 * be freed.
4882 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004883 if (m->prev) {
4884 pop_mmacro(m);
4885 l->finishes->in_progress --;
4886 } else {
Keith Kanios891775e2009-07-11 06:08:54 -05004887 nasm_free(m->params);
4888 free_tlist(m->iline);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004889 nasm_free(m->paramlen);
4890 l->finishes->in_progress = 0;
4891 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00004892 } else
4893 free_mmacro(m);
4894 }
4895 istk->expansion = l->next;
4896 nasm_free(l);
4897 list->downlevel(LIST_MACRO);
4898 }
4899 }
4900 while (1) { /* until we get a line we can use */
H. Peter Anvineba20a72002-04-30 20:53:55 +00004901
H. Peter Anvine2c80182005-01-15 22:15:51 +00004902 if (istk->expansion) { /* from a macro expansion */
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004903 char *p;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004904 Line *l = istk->expansion;
4905 if (istk->mstk)
4906 istk->mstk->lineno++;
4907 tline = l->first;
4908 istk->expansion = l->next;
4909 nasm_free(l);
H. Peter Anvin6867acc2007-10-10 14:58:45 -07004910 p = detoken(tline, false);
H. Peter Anvine2c80182005-01-15 22:15:51 +00004911 list->line(LIST_MACRO, p);
4912 nasm_free(p);
4913 break;
4914 }
4915 line = read_line();
4916 if (line) { /* from the current input file */
4917 line = prepreproc(line);
Keith Kaniosb7a89542007-04-12 02:40:54 +00004918 tline = tokenize(line);
H. Peter Anvine2c80182005-01-15 22:15:51 +00004919 nasm_free(line);
4920 break;
4921 }
4922 /*
4923 * The current file has ended; work down the istk
4924 */
4925 {
4926 Include *i = istk;
4927 fclose(i->fp);
4928 if (i->conds)
4929 error(ERR_FATAL,
4930 "expected `%%endif' before end of file");
4931 /* only set line and file name if there's a next node */
4932 if (i->next) {
4933 src_set_linnum(i->lineno);
4934 nasm_free(src_set_fname(i->fname));
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004935 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00004936 istk = i->next;
4937 list->downlevel(LIST_INCLUDE);
4938 nasm_free(i);
4939 if (!istk)
4940 return NULL;
Victor van den Elzen4c9d6222008-10-01 13:08:50 +02004941 if (istk->expansion && istk->expansion->finishes)
4942 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004943 }
4944 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004945
H. Peter Anvine2c80182005-01-15 22:15:51 +00004946 /*
4947 * We must expand MMacro parameters and MMacro-local labels
4948 * _before_ we plunge into directive processing, to cope
4949 * with things like `%define something %1' such as STRUC
4950 * uses. Unless we're _defining_ a MMacro, in which case
4951 * those tokens should be left alone to go into the
4952 * definition; and unless we're in a non-emitting
4953 * condition, in which case we don't want to meddle with
4954 * anything.
4955 */
Charles Crayned4200be2008-07-12 16:42:33 -07004956 if (!defining && !(istk->conds && !emitting(istk->conds->state))
H. Peter Anvin992fe752008-10-19 15:45:05 -07004957 && !(istk->mstk && !istk->mstk->in_progress)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00004958 tline = expand_mmac_params(tline);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004959 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00004960
H. Peter Anvine2c80182005-01-15 22:15:51 +00004961 /*
4962 * Check the line to see if it's a preprocessor directive.
4963 */
4964 if (do_directive(tline) == DIRECTIVE_FOUND) {
4965 continue;
4966 } else if (defining) {
4967 /*
4968 * We're defining a multi-line macro. We emit nothing
4969 * at all, and just
Keith Kaniosb7a89542007-04-12 02:40:54 +00004970 * shove the tokenized line on to the macro definition.
H. Peter Anvine2c80182005-01-15 22:15:51 +00004971 */
4972 Line *l = nasm_malloc(sizeof(Line));
4973 l->next = defining->expansion;
4974 l->first = tline;
H. Peter Anvin538002d2008-06-28 18:30:27 -07004975 l->finishes = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004976 defining->expansion = l;
4977 continue;
4978 } else if (istk->conds && !emitting(istk->conds->state)) {
4979 /*
4980 * We're in a non-emitting branch of a condition block.
4981 * Emit nothing at all, not even a blank line: when we
4982 * emerge from the condition we'll give a line-number
4983 * directive so we keep our place correctly.
4984 */
4985 free_tlist(tline);
4986 continue;
4987 } else if (istk->mstk && !istk->mstk->in_progress) {
4988 /*
4989 * We're in a %rep block which has been terminated, so
4990 * we're walking through to the %endrep without
4991 * emitting anything. Emit nothing at all, not even a
4992 * blank line: when we emerge from the %rep block we'll
4993 * give a line-number directive so we keep our place
4994 * correctly.
4995 */
4996 free_tlist(tline);
4997 continue;
4998 } else {
4999 tline = expand_smacro(tline);
5000 if (!expand_mmacro(tline)) {
5001 /*
Keith Kaniosb7a89542007-04-12 02:40:54 +00005002 * De-tokenize the line again, and emit it.
H. Peter Anvine2c80182005-01-15 22:15:51 +00005003 */
H. Peter Anvin6867acc2007-10-10 14:58:45 -07005004 line = detoken(tline, true);
H. Peter Anvine2c80182005-01-15 22:15:51 +00005005 free_tlist(tline);
5006 break;
5007 } else {
5008 continue; /* expand_mmacro calls free_tlist */
5009 }
5010 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005011 }
5012
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005013 return line;
5014}
5015
H. Peter Anvine2c80182005-01-15 22:15:51 +00005016static void pp_cleanup(int pass)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005017{
H. Peter Anvine2c80182005-01-15 22:15:51 +00005018 if (defining) {
H. Peter Anvin89cee572009-07-15 09:16:54 -04005019 if (defining->name) {
Victor van den Elzen8f1120f2008-07-16 13:41:37 +02005020 error(ERR_NONFATAL,
5021 "end of file while still defining macro `%s'",
5022 defining->name);
5023 } else {
5024 error(ERR_NONFATAL, "end of file while still in %%rep");
5025 }
5026
H. Peter Anvine2c80182005-01-15 22:15:51 +00005027 free_mmacro(defining);
Cyrill Gorcunova327c652010-02-14 17:19:38 +03005028 defining = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005029 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005030 while (cstk)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005031 ctx_pop();
H. Peter Anvin97a23472007-09-16 17:57:25 -07005032 free_macros();
H. Peter Anvine2c80182005-01-15 22:15:51 +00005033 while (istk) {
5034 Include *i = istk;
5035 istk = istk->next;
5036 fclose(i->fp);
5037 nasm_free(i->fname);
5038 nasm_free(i);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005039 }
5040 while (cstk)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005041 ctx_pop();
H. Peter Anvin86877b22008-06-20 15:55:45 -07005042 nasm_free(src_set_fname(NULL));
H. Peter Anvine2c80182005-01-15 22:15:51 +00005043 if (pass == 0) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03005044 IncPath *i;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005045 free_llist(predef);
5046 delete_Blocks();
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03005047 while ((i = ipath)) {
5048 ipath = i->next;
5049 if (i->path)
5050 nasm_free(i->path);
5051 nasm_free(i);
5052 }
H. Peter Anvin11dfa1a2008-07-02 18:11:04 -07005053 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005054}
5055
Keith Kaniosa6dfa782007-04-13 16:47:53 +00005056void pp_include_path(char *path)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005057{
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005058 IncPath *i;
H. Peter Anvin37a321f2007-09-24 13:41:58 -07005059
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005060 i = nasm_malloc(sizeof(IncPath));
H. Peter Anvin37a321f2007-09-24 13:41:58 -07005061 i->path = path ? nasm_strdup(path) : NULL;
Frank Kotlerd0ed6fd2003-08-27 11:33:56 +00005062 i->next = NULL;
5063
H. Peter Anvin89cee572009-07-15 09:16:54 -04005064 if (ipath) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00005065 IncPath *j = ipath;
H. Peter Anvin89cee572009-07-15 09:16:54 -04005066 while (j->next)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005067 j = j->next;
5068 j->next = i;
5069 } else {
5070 ipath = i;
Frank Kotlerd0ed6fd2003-08-27 11:33:56 +00005071 }
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005072}
Frank Kotlerd0ed6fd2003-08-27 11:33:56 +00005073
Keith Kaniosa6dfa782007-04-13 16:47:53 +00005074void pp_pre_include(char *fname)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005075{
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005076 Token *inc, *space, *name;
5077 Line *l;
5078
H. Peter Anvin734b1882002-04-30 21:01:08 +00005079 name = new_Token(NULL, TOK_INTERNAL_STRING, fname, 0);
5080 space = new_Token(name, TOK_WHITESPACE, NULL, 0);
5081 inc = new_Token(space, TOK_PREPROC_ID, "%include", 0);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005082
5083 l = nasm_malloc(sizeof(Line));
5084 l->next = predef;
5085 l->first = inc;
H. Peter Anvin538002d2008-06-28 18:30:27 -07005086 l->finishes = NULL;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005087 predef = l;
5088}
5089
Keith Kaniosa6dfa782007-04-13 16:47:53 +00005090void pp_pre_define(char *definition)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005091{
5092 Token *def, *space;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005093 Line *l;
Keith Kaniosa6dfa782007-04-13 16:47:53 +00005094 char *equals;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005095
5096 equals = strchr(definition, '=');
H. Peter Anvin734b1882002-04-30 21:01:08 +00005097 space = new_Token(NULL, TOK_WHITESPACE, NULL, 0);
5098 def = new_Token(space, TOK_PREPROC_ID, "%define", 0);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005099 if (equals)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005100 *equals = ' ';
Keith Kaniosb7a89542007-04-12 02:40:54 +00005101 space->next = tokenize(definition);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005102 if (equals)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005103 *equals = '=';
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005104
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005105 l = nasm_malloc(sizeof(Line));
5106 l->next = predef;
5107 l->first = def;
H. Peter Anvin538002d2008-06-28 18:30:27 -07005108 l->finishes = NULL;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005109 predef = l;
5110}
5111
Keith Kaniosa6dfa782007-04-13 16:47:53 +00005112void pp_pre_undefine(char *definition)
H. Peter Anvin620515a2002-04-30 20:57:38 +00005113{
5114 Token *def, *space;
5115 Line *l;
5116
H. Peter Anvin734b1882002-04-30 21:01:08 +00005117 space = new_Token(NULL, TOK_WHITESPACE, NULL, 0);
5118 def = new_Token(space, TOK_PREPROC_ID, "%undef", 0);
Keith Kaniosb7a89542007-04-12 02:40:54 +00005119 space->next = tokenize(definition);
H. Peter Anvin620515a2002-04-30 20:57:38 +00005120
5121 l = nasm_malloc(sizeof(Line));
5122 l->next = predef;
5123 l->first = def;
H. Peter Anvin538002d2008-06-28 18:30:27 -07005124 l->finishes = NULL;
H. Peter Anvin620515a2002-04-30 20:57:38 +00005125 predef = l;
5126}
5127
Keith Kaniosb7a89542007-04-12 02:40:54 +00005128/*
5129 * Added by Keith Kanios:
5130 *
5131 * This function is used to assist with "runtime" preprocessor
5132 * directives. (e.g. pp_runtime("%define __BITS__ 64");)
5133 *
5134 * ERRORS ARE IGNORED HERE, SO MAKE COMPLETELY SURE THAT YOU
5135 * PASS A VALID STRING TO THIS FUNCTION!!!!!
5136 */
5137
Keith Kaniosa6dfa782007-04-13 16:47:53 +00005138void pp_runtime(char *definition)
Keith Kaniosb7a89542007-04-12 02:40:54 +00005139{
5140 Token *def;
H. Peter Anvin70653092007-10-19 14:42:29 -07005141
Keith Kaniosb7a89542007-04-12 02:40:54 +00005142 def = tokenize(definition);
H. Peter Anvin89cee572009-07-15 09:16:54 -04005143 if (do_directive(def) == NO_DIRECTIVE_FOUND)
Keith Kaniosb7a89542007-04-12 02:40:54 +00005144 free_tlist(def);
H. Peter Anvin70653092007-10-19 14:42:29 -07005145
Keith Kaniosb7a89542007-04-12 02:40:54 +00005146}
5147
H. Peter Anvina70547f2008-07-19 21:44:26 -07005148void pp_extra_stdmac(macros_t *macros)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005149{
H. Peter Anvin76690a12002-04-30 20:52:49 +00005150 extrastdmac = macros;
5151}
5152
Keith Kaniosa5fc6462007-10-13 07:09:22 -07005153static void make_tok_num(Token * tok, int64_t val)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005154{
Keith Kaniosa6dfa782007-04-13 16:47:53 +00005155 char numbuf[20];
Keith Kaniosa5fc6462007-10-13 07:09:22 -07005156 snprintf(numbuf, sizeof(numbuf), "%"PRId64"", val);
H. Peter Anvineba20a72002-04-30 20:53:55 +00005157 tok->text = nasm_strdup(numbuf);
5158 tok->type = TOK_NUMBER;
5159}
5160
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005161Preproc nasmpp = {
5162 pp_reset,
5163 pp_getline,
5164 pp_cleanup
5165};