blob: 406d568ef53f3d5fa111370bccdc17bfc4063939 [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
329/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000330 * Condition codes. Note that we use c_ prefix not C_ because C_ is
331 * used in nasm.h for the "real" condition codes. At _this_ level,
332 * we treat CXZ and ECXZ as condition codes, albeit non-invertible
333 * ones, so we need a different enum...
334 */
H. Peter Anvin476d2862007-10-02 22:04:15 -0700335static const char * const conditions[] = {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000336 "a", "ae", "b", "be", "c", "cxz", "e", "ecxz", "g", "ge", "l", "le",
337 "na", "nae", "nb", "nbe", "nc", "ne", "ng", "nge", "nl", "nle", "no",
H. Peter Anvince9be342007-09-12 00:22:29 +0000338 "np", "ns", "nz", "o", "p", "pe", "po", "rcxz", "s", "z"
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000339};
H. Peter Anvin476d2862007-10-02 22:04:15 -0700340enum pp_conds {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000341 c_A, c_AE, c_B, c_BE, c_C, c_CXZ, c_E, c_ECXZ, c_G, c_GE, c_L, c_LE,
342 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 -0700343 c_NP, c_NS, c_NZ, c_O, c_P, c_PE, c_PO, c_RCXZ, c_S, c_Z,
344 c_none = -1
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000345};
H. Peter Anvin476d2862007-10-02 22:04:15 -0700346static const enum pp_conds inverse_ccs[] = {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000347 c_NA, c_NAE, c_NB, c_NBE, c_NC, -1, c_NE, -1, c_NG, c_NGE, c_NL, c_NLE,
348 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 +0000349 c_Z, c_NO, c_NP, c_PO, c_PE, -1, c_NS, c_NZ
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000350};
351
H. Peter Anvin76690a12002-04-30 20:52:49 +0000352/*
353 * Directive names.
354 */
H. Peter Anvin65747262002-05-07 00:10:05 +0000355/* If this is a an IF, ELIF, ELSE or ENDIF keyword */
H. Peter Anvin9bf0aa72007-09-12 02:12:07 +0000356static int is_condition(enum preproc_token arg)
H. Peter Anvin65747262002-05-07 00:10:05 +0000357{
H. Peter Anvinda10e7b2007-09-12 04:18:37 +0000358 return PP_IS_COND(arg) || (arg == PP_ELSE) || (arg == PP_ENDIF);
H. Peter Anvin65747262002-05-07 00:10:05 +0000359}
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000360
361/* For TASM compatibility we need to be able to recognise TASM compatible
362 * conditional compilation directives. Using the NASM pre-processor does
363 * not work, so we look for them specifically from the following list and
364 * then jam in the equivalent NASM directive into the input stream.
365 */
366
H. Peter Anvine2c80182005-01-15 22:15:51 +0000367enum {
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000368 TM_ARG, TM_ELIF, TM_ELSE, TM_ENDIF, TM_IF, TM_IFDEF, TM_IFDIFI,
369 TM_IFNDEF, TM_INCLUDE, TM_LOCAL
370};
371
H. Peter Anvin476d2862007-10-02 22:04:15 -0700372static const char * const tasm_directives[] = {
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000373 "arg", "elif", "else", "endif", "if", "ifdef", "ifdifi",
374 "ifndef", "include", "local"
375};
376
377static int StackSize = 4;
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000378static char *StackPointer = "ebp";
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000379static int ArgOffset = 8;
H. Peter Anvin8781cb02007-11-08 20:01:11 -0800380static int LocalOffset = 0;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000381
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000382static Context *cstk;
383static Include *istk;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000384static IncPath *ipath = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000385
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300386static int pass; /* HACK: pass 0 = generate dependencies only */
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700387static StrList **dephead, **deptail; /* Dependency list */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000388
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300389static uint64_t unique; /* unique identifier numbers */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000390
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000391static Line *predef = NULL;
H. Peter Anvind2456592008-06-19 15:04:18 -0700392static bool do_predef;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000393
394static ListGen *list;
395
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000396/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000397 * The current set of multi-line macros we have defined.
398 */
H. Peter Anvin166c2472008-05-28 12:28:58 -0700399static struct hash_table mmacros;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000400
401/*
402 * The current set of single-line macros we have defined.
403 */
H. Peter Anvin166c2472008-05-28 12:28:58 -0700404static struct hash_table smacros;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000405
406/*
H. Peter Anvin76690a12002-04-30 20:52:49 +0000407 * The multi-line macro we are currently defining, or the %rep
408 * block we are currently reading, if any.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000409 */
410static MMacro *defining;
411
Charles Crayned4200be2008-07-12 16:42:33 -0700412static uint64_t nested_mac_count;
413static uint64_t nested_rep_count;
414
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000415/*
416 * The number of macro parameters to allocate space for at a time.
417 */
418#define PARAM_DELTA 16
419
420/*
H. Peter Anvina4835d42008-05-20 14:21:29 -0700421 * The standard macro set: defined in macros.c in the array nasm_stdmac.
422 * This gives our position in the macro set, when we're processing it.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000423 */
H. Peter Anvina70547f2008-07-19 21:44:26 -0700424static macros_t *stdmacpos;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000425
426/*
H. Peter Anvin76690a12002-04-30 20:52:49 +0000427 * The extra standard macros that come from the object format, if
428 * any.
429 */
H. Peter Anvina70547f2008-07-19 21:44:26 -0700430static macros_t *extrastdmac = NULL;
H. Peter Anvincfb71762008-06-20 15:20:16 -0700431static bool any_extrastdmac;
H. Peter Anvin76690a12002-04-30 20:52:49 +0000432
433/*
H. Peter Anvin734b1882002-04-30 21:01:08 +0000434 * Tokens are allocated in blocks to improve speed
435 */
436#define TOKEN_BLOCKSIZE 4096
437static Token *freeTokens = NULL;
H. Peter Anvince616072002-04-30 21:02:23 +0000438struct Blocks {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000439 Blocks *next;
440 void *chunk;
H. Peter Anvince616072002-04-30 21:02:23 +0000441};
442
443static Blocks blocks = { NULL, NULL };
H. Peter Anvin734b1882002-04-30 21:01:08 +0000444
445/*
H. Peter Anvin76690a12002-04-30 20:52:49 +0000446 * Forward declarations.
447 */
H. Peter Anvin734b1882002-04-30 21:01:08 +0000448static Token *expand_mmac_params(Token * tline);
449static Token *expand_smacro(Token * tline);
450static Token *expand_id(Token * tline);
H. Peter Anvinf8ad5322009-02-21 17:55:08 -0800451static Context *get_ctx(const char *name, const char **namep,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300452 bool all_contexts);
Keith Kaniosa5fc6462007-10-13 07:09:22 -0700453static void make_tok_num(Token * tok, int64_t val);
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000454static void error(int severity, const char *fmt, ...);
Victor van den Elzen3b404c02008-09-18 13:51:36 +0200455static void error_precond(int severity, const char *fmt, ...);
H. Peter Anvince616072002-04-30 21:02:23 +0000456static void *new_Block(size_t size);
457static void delete_Blocks(void);
H. Peter Anvinc751e862008-06-09 10:18:45 -0700458static Token *new_Token(Token * next, enum pp_token_type type,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300459 const char *text, int txtlen);
H. Peter Anvin734b1882002-04-30 21:01:08 +0000460static Token *delete_Token(Token * t);
H. Peter Anvineba20a72002-04-30 20:53:55 +0000461
462/*
463 * Macros for safe checking of token pointers, avoid *(NULL)
464 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300465#define tok_type_(x,t) ((x) && (x)->type == (t))
466#define skip_white_(x) if (tok_type_((x), TOK_WHITESPACE)) (x)=(x)->next
467#define tok_is_(x,v) (tok_type_((x), TOK_OTHER) && !strcmp((x)->text,(v)))
468#define tok_isnt_(x,v) ((x) && ((x)->type!=TOK_OTHER || strcmp((x)->text,(v))))
H. Peter Anvin76690a12002-04-30 20:52:49 +0000469
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300470/*
471 * Handle TASM specific directives, which do not contain a % in
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000472 * front of them. We do it here because I could not find any other
473 * place to do it for the moment, and it is a hack (ideally it would
474 * be nice to be able to use the NASM pre-processor to do it).
475 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000476static char *check_tasm_directive(char *line)
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000477{
Keith Kaniosb7a89542007-04-12 02:40:54 +0000478 int32_t i, j, k, m, len;
Cyrill Gorcunovf66ac7d2009-10-12 20:41:13 +0400479 char *p, *q, *oldline, oldchar;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000480
Cyrill Gorcunovf66ac7d2009-10-12 20:41:13 +0400481 p = nasm_skip_spaces(line);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000482
483 /* Binary search for the directive name */
484 i = -1;
Ed Beroset3ab3f412002-06-11 03:31:49 +0000485 j = elements(tasm_directives);
Cyrill Gorcunovf66ac7d2009-10-12 20:41:13 +0400486 q = nasm_skip_word(p);
487 len = q - p;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000488 if (len) {
489 oldchar = p[len];
490 p[len] = 0;
491 while (j - i > 1) {
492 k = (j + i) / 2;
493 m = nasm_stricmp(p, tasm_directives[k]);
494 if (m == 0) {
495 /* We have found a directive, so jam a % in front of it
496 * so that NASM will then recognise it as one if it's own.
497 */
498 p[len] = oldchar;
499 len = strlen(p);
500 oldline = line;
501 line = nasm_malloc(len + 2);
502 line[0] = '%';
503 if (k == TM_IFDIFI) {
H. Peter Anvin18f48792009-06-27 15:56:27 -0700504 /*
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300505 * NASM does not recognise IFDIFI, so we convert
506 * it to %if 0. This is not used in NASM
507 * compatible code, but does need to parse for the
508 * TASM macro package.
H. Peter Anvine2c80182005-01-15 22:15:51 +0000509 */
H. Peter Anvin18f48792009-06-27 15:56:27 -0700510 strcpy(line + 1, "if 0");
H. Peter Anvine2c80182005-01-15 22:15:51 +0000511 } else {
512 memcpy(line + 1, p, len + 1);
513 }
514 nasm_free(oldline);
515 return line;
516 } else if (m < 0) {
517 j = k;
518 } else
519 i = k;
520 }
521 p[len] = oldchar;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000522 }
523 return line;
524}
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000525
H. Peter Anvin76690a12002-04-30 20:52:49 +0000526/*
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000527 * The pre-preprocessing stage... This function translates line
528 * number indications as they emerge from GNU cpp (`# lineno "file"
529 * flags') into NASM preprocessor line number indications (`%line
530 * lineno file').
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000531 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000532static char *prepreproc(char *line)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000533{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000534 int lineno, fnlen;
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000535 char *fname, *oldline;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000536
H. Peter Anvine2c80182005-01-15 22:15:51 +0000537 if (line[0] == '#' && line[1] == ' ') {
538 oldline = line;
539 fname = oldline + 2;
540 lineno = atoi(fname);
541 fname += strspn(fname, "0123456789 ");
542 if (*fname == '"')
543 fname++;
544 fnlen = strcspn(fname, "\"");
545 line = nasm_malloc(20 + fnlen);
546 snprintf(line, 20 + fnlen, "%%line %d %.*s", lineno, fnlen, fname);
547 nasm_free(oldline);
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000548 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000549 if (tasm_compatible_mode)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000550 return check_tasm_directive(line);
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000551 return line;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000552}
553
554/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000555 * Free a linked list of tokens.
556 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000557static void free_tlist(Token * list)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000558{
H. Peter Anvine2c80182005-01-15 22:15:51 +0000559 while (list) {
560 list = delete_Token(list);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000561 }
562}
563
564/*
565 * Free a linked list of lines.
566 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000567static void free_llist(Line * list)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000568{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000569 Line *l;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000570 while (list) {
571 l = list;
572 list = list->next;
573 free_tlist(l->first);
574 nasm_free(l);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000575 }
576}
577
578/*
H. Peter Anvineba20a72002-04-30 20:53:55 +0000579 * Free an MMacro
580 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000581static void free_mmacro(MMacro * m)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000582{
H. Peter Anvin734b1882002-04-30 21:01:08 +0000583 nasm_free(m->name);
584 free_tlist(m->dlist);
585 nasm_free(m->defaults);
586 free_llist(m->expansion);
587 nasm_free(m);
H. Peter Anvineba20a72002-04-30 20:53:55 +0000588}
589
590/*
H. Peter Anvin97a23472007-09-16 17:57:25 -0700591 * Free all currently defined macros, and free the hash tables
592 */
H. Peter Anvin072771e2008-05-22 13:17:51 -0700593static void free_smacro_table(struct hash_table *smt)
H. Peter Anvin97a23472007-09-16 17:57:25 -0700594{
H. Peter Anvin97a23472007-09-16 17:57:25 -0700595 SMacro *s;
H. Peter Anvin072771e2008-05-22 13:17:51 -0700596 const char *key;
597 struct hash_tbl_node *it = NULL;
H. Peter Anvin97a23472007-09-16 17:57:25 -0700598
H. Peter Anvin072771e2008-05-22 13:17:51 -0700599 while ((s = hash_iterate(smt, &it, &key)) != NULL) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300600 nasm_free((void *)key);
601 while (s) {
602 SMacro *ns = s->next;
603 nasm_free(s->name);
604 free_tlist(s->expansion);
605 nasm_free(s);
606 s = ns;
607 }
H. Peter Anvin97a23472007-09-16 17:57:25 -0700608 }
H. Peter Anvin072771e2008-05-22 13:17:51 -0700609 hash_free(smt);
610}
611
612static void free_mmacro_table(struct hash_table *mmt)
613{
614 MMacro *m;
615 const char *key;
616 struct hash_tbl_node *it = NULL;
H. Peter Anvin97a23472007-09-16 17:57:25 -0700617
618 it = NULL;
H. Peter Anvin072771e2008-05-22 13:17:51 -0700619 while ((m = hash_iterate(mmt, &it, &key)) != NULL) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300620 nasm_free((void *)key);
621 while (m) {
622 MMacro *nm = m->next;
623 free_mmacro(m);
624 m = nm;
625 }
H. Peter Anvin97a23472007-09-16 17:57:25 -0700626 }
H. Peter Anvin072771e2008-05-22 13:17:51 -0700627 hash_free(mmt);
628}
629
630static void free_macros(void)
631{
H. Peter Anvin166c2472008-05-28 12:28:58 -0700632 free_smacro_table(&smacros);
633 free_mmacro_table(&mmacros);
H. Peter Anvin97a23472007-09-16 17:57:25 -0700634}
635
636/*
637 * Initialize the hash tables
638 */
639static void init_macros(void)
640{
H. Peter Anvin166c2472008-05-28 12:28:58 -0700641 hash_init(&smacros, HASH_LARGE);
642 hash_init(&mmacros, HASH_LARGE);
H. Peter Anvin97a23472007-09-16 17:57:25 -0700643}
644
645/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000646 * Pop the context stack.
647 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000648static void ctx_pop(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000649{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000650 Context *c = cstk;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000651
652 cstk = cstk->next;
H. Peter Anvin166c2472008-05-28 12:28:58 -0700653 free_smacro_table(&c->localmac);
H. Peter Anvin734b1882002-04-30 21:01:08 +0000654 nasm_free(c->name);
655 nasm_free(c);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000656}
657
H. Peter Anvin072771e2008-05-22 13:17:51 -0700658/*
659 * Search for a key in the hash index; adding it if necessary
660 * (in which case we initialize the data pointer to NULL.)
661 */
662static void **
663hash_findi_add(struct hash_table *hash, const char *str)
664{
665 struct hash_insert hi;
666 void **r;
667 char *strx;
668
669 r = hash_findi(hash, str, &hi);
670 if (r)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300671 return r;
H. Peter Anvin072771e2008-05-22 13:17:51 -0700672
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300673 strx = nasm_strdup(str); /* Use a more efficient allocator here? */
H. Peter Anvin072771e2008-05-22 13:17:51 -0700674 return hash_add(&hi, strx, NULL);
675}
676
677/*
678 * Like hash_findi, but returns the data element rather than a pointer
679 * to it. Used only when not adding a new element, hence no third
680 * argument.
681 */
682static void *
683hash_findix(struct hash_table *hash, const char *str)
684{
685 void **p;
686
687 p = hash_findi(hash, str, NULL);
688 return p ? *p : NULL;
689}
690
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000691#define BUF_DELTA 512
692/*
693 * Read a line from the top file in istk, handling multiple CR/LFs
694 * at the end of the line read, and handling spurious ^Zs. Will
695 * return lines from the standard macro set if this has not already
696 * been done.
697 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000698static char *read_line(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000699{
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000700 char *buffer, *p, *q;
H. Peter Anvin9f394642002-04-30 21:07:51 +0000701 int bufsize, continued_count;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000702
H. Peter Anvine2c80182005-01-15 22:15:51 +0000703 if (stdmacpos) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300704 unsigned char c;
705 const unsigned char *p = stdmacpos;
706 char *ret, *q;
707 size_t len = 0;
708 while ((c = *p++)) {
709 if (c >= 0x80)
710 len += pp_directives_len[c-0x80]+1;
711 else
712 len++;
713 }
714 ret = nasm_malloc(len+1);
715 q = ret;
716 while ((c = *stdmacpos++)) {
717 if (c >= 0x80) {
718 memcpy(q, pp_directives[c-0x80], pp_directives_len[c-0x80]);
719 q += pp_directives_len[c-0x80];
720 *q++ = ' ';
721 } else {
722 *q++ = c;
723 }
724 }
725 stdmacpos = p;
726 *q = '\0';
H. Peter Anvin72edbb82008-06-19 16:00:04 -0700727
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300728 if (!*stdmacpos) {
729 /* This was the last of the standard macro chain... */
730 stdmacpos = NULL;
731 if (any_extrastdmac) {
732 stdmacpos = extrastdmac;
733 any_extrastdmac = false;
734 } else if (do_predef) {
735 Line *pd, *l;
736 Token *head, **tail, *t;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000737
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300738 /*
739 * Nasty hack: here we push the contents of
740 * `predef' on to the top-level expansion stack,
741 * since this is the most convenient way to
742 * implement the pre-include and pre-define
743 * features.
744 */
745 for (pd = predef; pd; pd = pd->next) {
746 head = NULL;
747 tail = &head;
748 for (t = pd->first; t; t = t->next) {
749 *tail = new_Token(NULL, t->type, t->text, 0);
750 tail = &(*tail)->next;
751 }
752 l = nasm_malloc(sizeof(Line));
753 l->next = istk->expansion;
754 l->first = head;
755 l->finishes = NULL;
756 istk->expansion = l;
757 }
758 do_predef = false;
759 }
760 }
761 return ret;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000762 }
763
764 bufsize = BUF_DELTA;
765 buffer = nasm_malloc(BUF_DELTA);
766 p = buffer;
H. Peter Anvin9f394642002-04-30 21:07:51 +0000767 continued_count = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000768 while (1) {
769 q = fgets(p, bufsize - (p - buffer), istk->fp);
770 if (!q)
771 break;
772 p += strlen(p);
773 if (p > buffer && p[-1] == '\n') {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300774 /*
775 * Convert backslash-CRLF line continuation sequences into
776 * nothing at all (for DOS and Windows)
777 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000778 if (((p - 2) > buffer) && (p[-3] == '\\') && (p[-2] == '\r')) {
779 p -= 3;
780 *p = 0;
781 continued_count++;
782 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300783 /*
784 * Also convert backslash-LF line continuation sequences into
785 * nothing at all (for Unix)
786 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000787 else if (((p - 1) > buffer) && (p[-2] == '\\')) {
788 p -= 2;
789 *p = 0;
790 continued_count++;
791 } else {
792 break;
793 }
794 }
795 if (p - buffer > bufsize - 10) {
Keith Kaniosb7a89542007-04-12 02:40:54 +0000796 int32_t offset = p - buffer;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000797 bufsize += BUF_DELTA;
798 buffer = nasm_realloc(buffer, bufsize);
799 p = buffer + offset; /* prevent stale-pointer problems */
800 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000801 }
802
H. Peter Anvine2c80182005-01-15 22:15:51 +0000803 if (!q && p == buffer) {
804 nasm_free(buffer);
805 return NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000806 }
807
H. Peter Anvine2c80182005-01-15 22:15:51 +0000808 src_set_linnum(src_get_linnum() + istk->lineinc +
809 (continued_count * istk->lineinc));
H. Peter Anvineba20a72002-04-30 20:53:55 +0000810
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000811 /*
812 * Play safe: remove CRs as well as LFs, if any of either are
813 * present at the end of the line.
814 */
H. Peter Anvineba20a72002-04-30 20:53:55 +0000815 while (--p >= buffer && (*p == '\n' || *p == '\r'))
H. Peter Anvine2c80182005-01-15 22:15:51 +0000816 *p = '\0';
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000817
818 /*
819 * Handle spurious ^Z, which may be inserted into source files
820 * by some file transfer utilities.
821 */
822 buffer[strcspn(buffer, "\032")] = '\0';
823
H. Peter Anvin734b1882002-04-30 21:01:08 +0000824 list->line(LIST_READ, buffer);
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000825
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000826 return buffer;
827}
828
829/*
Keith Kaniosb7a89542007-04-12 02:40:54 +0000830 * Tokenize a line of text. This is a very simple process since we
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000831 * don't need to parse the value out of e.g. numeric tokens: we
832 * simply split one string into many.
833 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000834static Token *tokenize(char *line)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000835{
H. Peter Anvinca544db2008-10-19 19:30:11 -0700836 char c, *p = line;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +0000837 enum pp_token_type type;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000838 Token *list = NULL;
839 Token *t, **tail = &list;
840
H. Peter Anvine2c80182005-01-15 22:15:51 +0000841 while (*line) {
842 p = line;
843 if (*p == '%') {
844 p++;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300845 if (*p == '+' && !nasm_isdigit(p[1])) {
846 p++;
847 type = TOK_PASTE;
848 } else if (nasm_isdigit(*p) ||
849 ((*p == '-' || *p == '+') && nasm_isdigit(p[1]))) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000850 do {
851 p++;
852 }
H. Peter Anvinbda7a6e2008-06-21 10:23:17 -0700853 while (nasm_isdigit(*p));
H. Peter Anvine2c80182005-01-15 22:15:51 +0000854 type = TOK_PREPROC_ID;
855 } else if (*p == '{') {
856 p++;
857 while (*p && *p != '}') {
858 p[-1] = *p;
859 p++;
860 }
861 p[-1] = '\0';
862 if (*p)
863 p++;
864 type = TOK_PREPROC_ID;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300865 } else if (*p == '[') {
866 int lvl = 1;
867 line += 2; /* Skip the leading %[ */
868 p++;
869 while (lvl && (c = *p++)) {
870 switch (c) {
871 case ']':
872 lvl--;
873 break;
874 case '%':
875 if (*p == '[')
876 lvl++;
877 break;
878 case '\'':
879 case '\"':
880 case '`':
881 p = nasm_skip_string(p)+1;
882 break;
883 default:
884 break;
885 }
886 }
887 p--;
888 if (*p)
889 *p++ = '\0';
890 if (lvl)
891 error(ERR_NONFATAL, "unterminated %[ construct");
892 type = TOK_INDIRECT;
893 } else if (*p == '?') {
894 type = TOK_PREPROC_Q; /* %? */
895 p++;
896 if (*p == '?') {
897 type = TOK_PREPROC_QQ; /* %?? */
898 p++;
899 }
H. Peter Anvine2c80182005-01-15 22:15:51 +0000900 } else if (isidchar(*p) ||
901 ((*p == '!' || *p == '%' || *p == '$') &&
902 isidchar(p[1]))) {
903 do {
904 p++;
905 }
906 while (isidchar(*p));
907 type = TOK_PREPROC_ID;
908 } else {
909 type = TOK_OTHER;
910 if (*p == '%')
911 p++;
912 }
913 } else if (isidstart(*p) || (*p == '$' && isidstart(p[1]))) {
914 type = TOK_ID;
915 p++;
916 while (*p && isidchar(*p))
917 p++;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -0700918 } else if (*p == '\'' || *p == '"' || *p == '`') {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000919 /*
920 * A string token.
921 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000922 type = TOK_STRING;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300923 p = nasm_skip_string(p);
Nickolay Yurchenkof3b3ce22003-09-21 20:38:43 +0000924
H. Peter Anvine2c80182005-01-15 22:15:51 +0000925 if (*p) {
926 p++;
927 } else {
H. Peter Anvin917a3492008-09-24 09:14:49 -0700928 error(ERR_WARNING|ERR_PASS1, "unterminated string");
H. Peter Anvine2c80182005-01-15 22:15:51 +0000929 /* Handling unterminated strings by UNV */
930 /* type = -1; */
931 }
Victor van den Elzenfb5f2512009-04-17 16:17:59 +0200932 } else if (p[0] == '$' && p[1] == '$') {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300933 type = TOK_OTHER; /* TOKEN_BASE */
Victor van den Elzenfb5f2512009-04-17 16:17:59 +0200934 p += 2;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000935 } else if (isnumstart(*p)) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300936 bool is_hex = false;
937 bool is_float = false;
938 bool has_e = false;
939 char c, *r;
H. Peter Anvinc2df2822007-10-24 15:29:28 -0700940
H. Peter Anvine2c80182005-01-15 22:15:51 +0000941 /*
H. Peter Anvinc2df2822007-10-24 15:29:28 -0700942 * A numeric token.
H. Peter Anvine2c80182005-01-15 22:15:51 +0000943 */
H. Peter Anvinc2df2822007-10-24 15:29:28 -0700944
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300945 if (*p == '$') {
946 p++;
947 is_hex = true;
948 }
H. Peter Anvinc2df2822007-10-24 15:29:28 -0700949
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300950 for (;;) {
951 c = *p++;
H. Peter Anvinc2df2822007-10-24 15:29:28 -0700952
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300953 if (!is_hex && (c == 'e' || c == 'E')) {
954 has_e = true;
955 if (*p == '+' || *p == '-') {
956 /*
957 * e can only be followed by +/- if it is either a
958 * prefixed hex number or a floating-point number
959 */
960 p++;
961 is_float = true;
962 }
963 } else if (c == 'H' || c == 'h' || c == 'X' || c == 'x') {
964 is_hex = true;
965 } else if (c == 'P' || c == 'p') {
966 is_float = true;
967 if (*p == '+' || *p == '-')
968 p++;
969 } else if (isnumchar(c) || c == '_')
970 ; /* just advance */
971 else if (c == '.') {
972 /*
973 * we need to deal with consequences of the legacy
974 * parser, like "1.nolist" being two tokens
975 * (TOK_NUMBER, TOK_ID) here; at least give it
976 * a shot for now. In the future, we probably need
977 * a flex-based scanner with proper pattern matching
978 * to do it as well as it can be done. Nothing in
979 * the world is going to help the person who wants
980 * 0x123.p16 interpreted as two tokens, though.
981 */
982 r = p;
983 while (*r == '_')
984 r++;
H. Peter Anvinc2df2822007-10-24 15:29:28 -0700985
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300986 if (nasm_isdigit(*r) || (is_hex && nasm_isxdigit(*r)) ||
987 (!is_hex && (*r == 'e' || *r == 'E')) ||
988 (*r == 'p' || *r == 'P')) {
989 p = r;
990 is_float = true;
991 } else
992 break; /* Terminate the token */
993 } else
994 break;
995 }
996 p--; /* Point to first character beyond number */
H. Peter Anvinc2df2822007-10-24 15:29:28 -0700997
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300998 if (p == line+1 && *line == '$') {
999 type = TOK_OTHER; /* TOKEN_HERE */
1000 } else {
1001 if (has_e && !is_hex) {
1002 /* 1e13 is floating-point, but 1e13h is not */
1003 is_float = true;
1004 }
H. Peter Anvind784a082009-04-20 14:01:18 -07001005
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001006 type = is_float ? TOK_FLOAT : TOK_NUMBER;
1007 }
H. Peter Anvinbda7a6e2008-06-21 10:23:17 -07001008 } else if (nasm_isspace(*p)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001009 type = TOK_WHITESPACE;
Cyrill Gorcunovf66ac7d2009-10-12 20:41:13 +04001010 p = nasm_skip_spaces(p);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001011 /*
1012 * Whitespace just before end-of-line is discarded by
1013 * pretending it's a comment; whitespace just before a
1014 * comment gets lumped into the comment.
1015 */
1016 if (!*p || *p == ';') {
1017 type = TOK_COMMENT;
1018 while (*p)
1019 p++;
1020 }
1021 } else if (*p == ';') {
1022 type = TOK_COMMENT;
1023 while (*p)
1024 p++;
1025 } else {
1026 /*
1027 * Anything else is an operator of some kind. We check
1028 * for all the double-character operators (>>, <<, //,
1029 * %%, <=, >=, ==, !=, <>, &&, ||, ^^), but anything
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001030 * else is a single-character operator.
H. Peter Anvine2c80182005-01-15 22:15:51 +00001031 */
1032 type = TOK_OTHER;
1033 if ((p[0] == '>' && p[1] == '>') ||
1034 (p[0] == '<' && p[1] == '<') ||
1035 (p[0] == '/' && p[1] == '/') ||
1036 (p[0] == '<' && p[1] == '=') ||
1037 (p[0] == '>' && p[1] == '=') ||
1038 (p[0] == '=' && p[1] == '=') ||
1039 (p[0] == '!' && p[1] == '=') ||
1040 (p[0] == '<' && p[1] == '>') ||
1041 (p[0] == '&' && p[1] == '&') ||
1042 (p[0] == '|' && p[1] == '|') ||
1043 (p[0] == '^' && p[1] == '^')) {
1044 p++;
1045 }
1046 p++;
1047 }
Nickolay Yurchenkof3b3ce22003-09-21 20:38:43 +00001048
H. Peter Anvine2c80182005-01-15 22:15:51 +00001049 /* Handling unterminated string by UNV */
1050 /*if (type == -1)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001051 {
1052 *tail = t = new_Token(NULL, TOK_STRING, line, p-line+1);
1053 t->text[p-line] = *line;
1054 tail = &t->next;
1055 }
1056 else */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001057 if (type != TOK_COMMENT) {
1058 *tail = t = new_Token(NULL, type, line, p - line);
1059 tail = &t->next;
1060 }
1061 line = p;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001062 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001063 return list;
1064}
1065
H. Peter Anvince616072002-04-30 21:02:23 +00001066/*
1067 * this function allocates a new managed block of memory and
H. Peter Anvin70653092007-10-19 14:42:29 -07001068 * returns a pointer to the block. The managed blocks are
H. Peter Anvince616072002-04-30 21:02:23 +00001069 * deleted only all at once by the delete_Blocks function.
1070 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001071static void *new_Block(size_t size)
H. Peter Anvince616072002-04-30 21:02:23 +00001072{
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001073 Blocks *b = &blocks;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001074
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001075 /* first, get to the end of the linked list */
1076 while (b->next)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001077 b = b->next;
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001078 /* now allocate the requested chunk */
1079 b->chunk = nasm_malloc(size);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001080
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001081 /* now allocate a new block for the next request */
1082 b->next = nasm_malloc(sizeof(Blocks));
1083 /* and initialize the contents of the new block */
1084 b->next->next = NULL;
1085 b->next->chunk = NULL;
1086 return b->chunk;
H. Peter Anvince616072002-04-30 21:02:23 +00001087}
1088
1089/*
1090 * this function deletes all managed blocks of memory
1091 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001092static void delete_Blocks(void)
H. Peter Anvince616072002-04-30 21:02:23 +00001093{
H. Peter Anvine2c80182005-01-15 22:15:51 +00001094 Blocks *a, *b = &blocks;
H. Peter Anvince616072002-04-30 21:02:23 +00001095
H. Peter Anvin70653092007-10-19 14:42:29 -07001096 /*
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001097 * keep in mind that the first block, pointed to by blocks
H. Peter Anvin70653092007-10-19 14:42:29 -07001098 * is a static and not dynamically allocated, so we don't
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001099 * free it.
1100 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001101 while (b) {
1102 if (b->chunk)
1103 nasm_free(b->chunk);
1104 a = b;
1105 b = b->next;
1106 if (a != &blocks)
1107 nasm_free(a);
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001108 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001109}
H. Peter Anvin734b1882002-04-30 21:01:08 +00001110
1111/*
H. Peter Anvin70653092007-10-19 14:42:29 -07001112 * this function creates a new Token and passes a pointer to it
H. Peter Anvin734b1882002-04-30 21:01:08 +00001113 * back to the caller. It sets the type and text elements, and
H. Peter Anvinf26e0972008-07-01 21:26:27 -07001114 * also the a.mac and next elements to NULL.
H. Peter Anvin734b1882002-04-30 21:01:08 +00001115 */
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07001116static Token *new_Token(Token * next, enum pp_token_type type,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001117 const char *text, int txtlen)
H. Peter Anvin734b1882002-04-30 21:01:08 +00001118{
1119 Token *t;
1120 int i;
1121
H. Peter Anvin89cee572009-07-15 09:16:54 -04001122 if (!freeTokens) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001123 freeTokens = (Token *) new_Block(TOKEN_BLOCKSIZE * sizeof(Token));
1124 for (i = 0; i < TOKEN_BLOCKSIZE - 1; i++)
1125 freeTokens[i].next = &freeTokens[i + 1];
1126 freeTokens[i].next = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001127 }
1128 t = freeTokens;
1129 freeTokens = t->next;
1130 t->next = next;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07001131 t->a.mac = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001132 t->type = type;
H. Peter Anvin89cee572009-07-15 09:16:54 -04001133 if (type == TOK_WHITESPACE || !text) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001134 t->text = NULL;
1135 } else {
1136 if (txtlen == 0)
1137 txtlen = strlen(text);
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07001138 t->text = nasm_malloc(txtlen+1);
1139 memcpy(t->text, text, txtlen);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001140 t->text[txtlen] = '\0';
H. Peter Anvin734b1882002-04-30 21:01:08 +00001141 }
1142 return t;
1143}
1144
H. Peter Anvine2c80182005-01-15 22:15:51 +00001145static Token *delete_Token(Token * t)
H. Peter Anvin734b1882002-04-30 21:01:08 +00001146{
1147 Token *next = t->next;
1148 nasm_free(t->text);
H. Peter Anvin788e6c12002-04-30 21:02:01 +00001149 t->next = freeTokens;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001150 freeTokens = t;
1151 return next;
1152}
1153
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001154/*
1155 * Convert a line of tokens back into text.
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001156 * If expand_locals is not zero, identifiers of the form "%$*xxx"
1157 * will be transformed into ..@ctxnum.xxx
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001158 */
H. Peter Anvin9e200162008-06-04 17:23:14 -07001159static char *detoken(Token * tlist, bool expand_locals)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001160{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001161 Token *t;
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001162 char *line, *p;
H. Peter Anvinb4daadc2008-01-21 16:31:57 -08001163 const char *q;
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001164 int len = 0;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001165
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001166 list_for_each(t, tlist) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001167 if (t->type == TOK_PREPROC_ID && t->text[1] == '!') {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001168 char *p = getenv(t->text + 2);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001169 nasm_free(t->text);
1170 if (p)
1171 t->text = nasm_strdup(p);
1172 else
1173 t->text = NULL;
1174 }
1175 /* Expand local macros here and not during preprocessing */
1176 if (expand_locals &&
1177 t->type == TOK_PREPROC_ID && t->text &&
1178 t->text[0] == '%' && t->text[1] == '$') {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001179 const char *q;
1180 char *p;
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001181 Context *ctx = get_ctx(t->text, &q, false);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001182 if (ctx) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001183 char buffer[40];
Keith Kanios93f2e9a2007-04-14 00:10:59 +00001184 snprintf(buffer, sizeof(buffer), "..@%"PRIu32".", ctx->number);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001185 p = nasm_strcat(buffer, q);
1186 nasm_free(t->text);
1187 t->text = p;
1188 }
1189 }
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001190 if (t->type == TOK_WHITESPACE)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001191 len++;
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001192 else if (t->text)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001193 len += strlen(t->text);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001194 }
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001195
H. Peter Anvin734b1882002-04-30 21:01:08 +00001196 p = line = nasm_malloc(len + 1);
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001197
1198 list_for_each(t, tlist) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001199 if (t->type == TOK_WHITESPACE) {
H. Peter Anvinb4daadc2008-01-21 16:31:57 -08001200 *p++ = ' ';
H. Peter Anvine2c80182005-01-15 22:15:51 +00001201 } else if (t->text) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001202 q = t->text;
1203 while (*q)
1204 *p++ = *q++;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001205 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001206 }
1207 *p = '\0';
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001208
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001209 return line;
1210}
1211
1212/*
H. Peter Anvin76690a12002-04-30 20:52:49 +00001213 * A scanner, suitable for use by the expression evaluator, which
1214 * operates on a line of Tokens. Expects a pointer to a pointer to
1215 * the first token in the line to be passed in as its private_data
1216 * field.
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001217 *
1218 * FIX: This really needs to be unified with stdscan.
H. Peter Anvin76690a12002-04-30 20:52:49 +00001219 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001220static int ppscan(void *private_data, struct tokenval *tokval)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001221{
H. Peter Anvin76690a12002-04-30 20:52:49 +00001222 Token **tlineptr = private_data;
1223 Token *tline;
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001224 char ourcopy[MAX_KEYWORD+1], *p, *r, *s;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001225
H. Peter Anvine2c80182005-01-15 22:15:51 +00001226 do {
1227 tline = *tlineptr;
1228 *tlineptr = tline ? tline->next : NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001229 }
1230 while (tline && (tline->type == TOK_WHITESPACE ||
H. Peter Anvine2c80182005-01-15 22:15:51 +00001231 tline->type == TOK_COMMENT));
H. Peter Anvin76690a12002-04-30 20:52:49 +00001232
1233 if (!tline)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001234 return tokval->t_type = TOKEN_EOS;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001235
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001236 tokval->t_charptr = tline->text;
1237
H. Peter Anvin76690a12002-04-30 20:52:49 +00001238 if (tline->text[0] == '$' && !tline->text[1])
H. Peter Anvine2c80182005-01-15 22:15:51 +00001239 return tokval->t_type = TOKEN_HERE;
H. Peter Anvin7cf897e2002-05-30 21:30:33 +00001240 if (tline->text[0] == '$' && tline->text[1] == '$' && !tline->text[2])
H. Peter Anvine2c80182005-01-15 22:15:51 +00001241 return tokval->t_type = TOKEN_BASE;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001242
H. Peter Anvine2c80182005-01-15 22:15:51 +00001243 if (tline->type == TOK_ID) {
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001244 p = tokval->t_charptr = tline->text;
1245 if (p[0] == '$') {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001246 tokval->t_charptr++;
1247 return tokval->t_type = TOKEN_ID;
1248 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00001249
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001250 for (r = p, s = ourcopy; *r; r++) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001251 if (r >= p+MAX_KEYWORD)
1252 return tokval->t_type = TOKEN_ID; /* Not a keyword */
H. Peter Anvinac8f8fc2008-06-11 15:49:41 -07001253 *s++ = nasm_tolower(*r);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001254 }
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001255 *s = '\0';
1256 /* right, so we have an identifier sitting in temp storage. now,
1257 * is it actually a register or instruction name, or what? */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001258 return nasm_token_hash(ourcopy, tokval);
H. Peter Anvin76690a12002-04-30 20:52:49 +00001259 }
1260
H. Peter Anvine2c80182005-01-15 22:15:51 +00001261 if (tline->type == TOK_NUMBER) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001262 bool rn_error;
1263 tokval->t_integer = readnum(tline->text, &rn_error);
1264 tokval->t_charptr = tline->text;
1265 if (rn_error)
1266 return tokval->t_type = TOKEN_ERRNUM;
1267 else
1268 return tokval->t_type = TOKEN_NUM;
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001269 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00001270
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001271 if (tline->type == TOK_FLOAT) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001272 return tokval->t_type = TOKEN_FLOAT;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001273 }
1274
H. Peter Anvine2c80182005-01-15 22:15:51 +00001275 if (tline->type == TOK_STRING) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001276 char bq, *ep;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001277
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001278 bq = tline->text[0];
H. Peter Anvin11627042008-06-09 20:45:19 -07001279 tokval->t_charptr = tline->text;
1280 tokval->t_inttwo = nasm_unquote(tline->text, &ep);
H. Peter Anvind2456592008-06-19 15:04:18 -07001281
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001282 if (ep[0] != bq || ep[1] != '\0')
1283 return tokval->t_type = TOKEN_ERRSTR;
1284 else
1285 return tokval->t_type = TOKEN_STR;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001286 }
1287
H. Peter Anvine2c80182005-01-15 22:15:51 +00001288 if (tline->type == TOK_OTHER) {
1289 if (!strcmp(tline->text, "<<"))
1290 return tokval->t_type = TOKEN_SHL;
1291 if (!strcmp(tline->text, ">>"))
1292 return tokval->t_type = TOKEN_SHR;
1293 if (!strcmp(tline->text, "//"))
1294 return tokval->t_type = TOKEN_SDIV;
1295 if (!strcmp(tline->text, "%%"))
1296 return tokval->t_type = TOKEN_SMOD;
1297 if (!strcmp(tline->text, "=="))
1298 return tokval->t_type = TOKEN_EQ;
1299 if (!strcmp(tline->text, "<>"))
1300 return tokval->t_type = TOKEN_NE;
1301 if (!strcmp(tline->text, "!="))
1302 return tokval->t_type = TOKEN_NE;
1303 if (!strcmp(tline->text, "<="))
1304 return tokval->t_type = TOKEN_LE;
1305 if (!strcmp(tline->text, ">="))
1306 return tokval->t_type = TOKEN_GE;
1307 if (!strcmp(tline->text, "&&"))
1308 return tokval->t_type = TOKEN_DBL_AND;
1309 if (!strcmp(tline->text, "^^"))
1310 return tokval->t_type = TOKEN_DBL_XOR;
1311 if (!strcmp(tline->text, "||"))
1312 return tokval->t_type = TOKEN_DBL_OR;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001313 }
1314
1315 /*
1316 * We have no other options: just return the first character of
1317 * the token text.
1318 */
1319 return tokval->t_type = tline->text[0];
1320}
1321
1322/*
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001323 * Compare a string to the name of an existing macro; this is a
1324 * simple wrapper which calls either strcmp or nasm_stricmp
1325 * depending on the value of the `casesense' parameter.
1326 */
H. Peter Anvin4db5a162007-10-11 13:42:09 -07001327static int mstrcmp(const char *p, const char *q, bool casesense)
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001328{
H. Peter Anvin734b1882002-04-30 21:01:08 +00001329 return casesense ? strcmp(p, q) : nasm_stricmp(p, q);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001330}
1331
1332/*
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07001333 * Compare a string to the name of an existing macro; this is a
1334 * simple wrapper which calls either strcmp or nasm_stricmp
1335 * depending on the value of the `casesense' parameter.
1336 */
1337static int mmemcmp(const char *p, const char *q, size_t l, bool casesense)
1338{
1339 return casesense ? memcmp(p, q, l) : nasm_memicmp(p, q, l);
1340}
1341
1342/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001343 * Return the Context structure associated with a %$ token. Return
1344 * NULL, having _already_ reported an error condition, if the
1345 * context stack isn't deep enough for the supplied number of $
1346 * signs.
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001347 * If all_contexts == true, contexts that enclose current are
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001348 * also scanned for such smacro, until it is found; if not -
1349 * only the context that directly results from the number of $'s
1350 * in variable's name.
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001351 *
1352 * If "namep" is non-NULL, set it to the pointer to the macro name
1353 * tail, i.e. the part beyond %$...
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001354 */
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001355static Context *get_ctx(const char *name, const char **namep,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001356 bool all_contexts)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001357{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001358 Context *ctx;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001359 SMacro *m;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001360 int i;
1361
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001362 if (namep)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001363 *namep = name;
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001364
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001365 if (!name || name[0] != '%' || name[1] != '$')
H. Peter Anvine2c80182005-01-15 22:15:51 +00001366 return NULL;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001367
H. Peter Anvine2c80182005-01-15 22:15:51 +00001368 if (!cstk) {
1369 error(ERR_NONFATAL, "`%s': context stack is empty", name);
1370 return NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001371 }
1372
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001373 name += 2;
1374 ctx = cstk;
1375 i = 0;
1376 while (ctx && *name == '$') {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001377 name++;
1378 i++;
1379 ctx = ctx->next;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001380 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001381 if (!ctx) {
1382 error(ERR_NONFATAL, "`%s': context stack is only"
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001383 " %d level%s deep", name, i, (i == 1 ? "" : "s"));
H. Peter Anvine2c80182005-01-15 22:15:51 +00001384 return NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001385 }
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001386
1387 if (namep)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001388 *namep = name;
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001389
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001390 if (!all_contexts)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001391 return ctx;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001392
H. Peter Anvine2c80182005-01-15 22:15:51 +00001393 do {
1394 /* Search for this smacro in found context */
H. Peter Anvin166c2472008-05-28 12:28:58 -07001395 m = hash_findix(&ctx->localmac, name);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001396 while (m) {
1397 if (!mstrcmp(m->name, name, m->casesense))
1398 return ctx;
1399 m = m->next;
1400 }
1401 ctx = ctx->next;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001402 }
1403 while (ctx);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001404 return NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001405}
1406
1407/*
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001408 * Check to see if a file is already in a string list
1409 */
1410static bool in_list(const StrList *list, const char *str)
1411{
1412 while (list) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001413 if (!strcmp(list->str, str))
1414 return true;
1415 list = list->next;
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001416 }
1417 return false;
1418}
1419
1420/*
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001421 * Open an include file. This routine must always return a valid
1422 * file pointer if it returns - it's responsible for throwing an
1423 * ERR_FATAL and bombing out completely if not. It should also try
1424 * the include path one by one until it finds the file or reaches
1425 * the end of the path.
1426 */
H. Peter Anvin2b1c3b92008-06-06 10:38:46 -07001427static FILE *inc_fopen(const char *file, StrList **dhead, StrList ***dtail,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001428 bool missing_ok)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001429{
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001430 FILE *fp;
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001431 char *prefix = "";
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001432 IncPath *ip = ipath;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001433 int len = strlen(file);
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001434 size_t prefix_len = 0;
1435 StrList *sl;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001436
H. Peter Anvine2c80182005-01-15 22:15:51 +00001437 while (1) {
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001438 sl = nasm_malloc(prefix_len+len+1+sizeof sl->next);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001439 memcpy(sl->str, prefix, prefix_len);
1440 memcpy(sl->str+prefix_len, file, len+1);
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001441 fp = fopen(sl->str, "r");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001442 if (fp && dhead && !in_list(*dhead, sl->str)) {
1443 sl->next = NULL;
1444 **dtail = sl;
1445 *dtail = &sl->next;
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001446 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001447 nasm_free(sl);
1448 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001449 if (fp)
1450 return fp;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001451 if (!ip) {
1452 if (!missing_ok)
1453 break;
1454 prefix = NULL;
1455 } else {
1456 prefix = ip->path;
1457 ip = ip->next;
1458 }
1459 if (prefix) {
1460 prefix_len = strlen(prefix);
1461 } else {
1462 /* -MG given and file not found */
1463 if (dhead && !in_list(*dhead, file)) {
1464 sl = nasm_malloc(len+1+sizeof sl->next);
1465 sl->next = NULL;
1466 strcpy(sl->str, file);
1467 **dtail = sl;
1468 *dtail = &sl->next;
1469 }
1470 return NULL;
1471 }
H. Peter Anvineba20a72002-04-30 20:53:55 +00001472 }
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001473
H. Peter Anvin734b1882002-04-30 21:01:08 +00001474 error(ERR_FATAL, "unable to open include file `%s'", file);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001475 return NULL;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001476}
1477
1478/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001479 * Determine if we should warn on defining a single-line macro of
H. Peter Anvinef7468f2002-04-30 20:57:59 +00001480 * name `name', with `nparam' parameters. If nparam is 0 or -1, will
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001481 * return true if _any_ single-line macro of that name is defined.
1482 * Otherwise, will return true if a single-line macro with either
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001483 * `nparam' or no parameters is defined.
1484 *
1485 * If a macro with precisely the right number of parameters is
H. Peter Anvinef7468f2002-04-30 20:57:59 +00001486 * defined, or nparam is -1, the address of the definition structure
1487 * will be returned in `defn'; otherwise NULL will be returned. If `defn'
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001488 * is NULL, no action will be taken regarding its contents, and no
1489 * error will occur.
1490 *
1491 * Note that this is also called with nparam zero to resolve
1492 * `ifdef'.
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001493 *
1494 * If you already know which context macro belongs to, you can pass
1495 * the context pointer as first parameter; if you won't but name begins
1496 * with %$ the context will be automatically computed. If all_contexts
1497 * is true, macro will be searched in outer contexts as well.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001498 */
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07001499static bool
H. Peter Anvinb2a5fda2008-06-19 21:42:42 -07001500smacro_defined(Context * ctx, const char *name, int nparam, SMacro ** defn,
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07001501 bool nocase)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001502{
H. Peter Anvin166c2472008-05-28 12:28:58 -07001503 struct hash_table *smtbl;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001504 SMacro *m;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001505
H. Peter Anvin97a23472007-09-16 17:57:25 -07001506 if (ctx) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001507 smtbl = &ctx->localmac;
H. Peter Anvin97a23472007-09-16 17:57:25 -07001508 } else if (name[0] == '%' && name[1] == '$') {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001509 if (cstk)
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001510 ctx = get_ctx(name, &name, false);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001511 if (!ctx)
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001512 return false; /* got to return _something_ */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001513 smtbl = &ctx->localmac;
H. Peter Anvin97a23472007-09-16 17:57:25 -07001514 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001515 smtbl = &smacros;
H. Peter Anvin97a23472007-09-16 17:57:25 -07001516 }
H. Peter Anvin166c2472008-05-28 12:28:58 -07001517 m = (SMacro *) hash_findix(smtbl, name);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001518
H. Peter Anvine2c80182005-01-15 22:15:51 +00001519 while (m) {
1520 if (!mstrcmp(m->name, name, m->casesense && nocase) &&
Charles Crayne192d5b52007-10-18 19:02:42 -07001521 (nparam <= 0 || m->nparam == 0 || nparam == (int) m->nparam)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001522 if (defn) {
Charles Crayne192d5b52007-10-18 19:02:42 -07001523 if (nparam == (int) m->nparam || nparam == -1)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001524 *defn = m;
1525 else
1526 *defn = NULL;
1527 }
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001528 return true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001529 }
1530 m = m->next;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001531 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001532
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001533 return false;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001534}
1535
1536/*
1537 * Count and mark off the parameters in a multi-line macro call.
1538 * This is called both from within the multi-line macro expansion
1539 * code, and also to mark off the default parameters when provided
1540 * in a %macro definition line.
1541 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001542static void count_mmac_params(Token * t, int *nparam, Token *** params)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001543{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001544 int paramsize, brace;
1545
1546 *nparam = paramsize = 0;
1547 *params = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001548 while (t) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001549 /* +1: we need space for the final NULL */
H. Peter Anvin91fb6f12008-09-01 10:56:33 -07001550 if (*nparam+1 >= paramsize) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001551 paramsize += PARAM_DELTA;
1552 *params = nasm_realloc(*params, sizeof(**params) * paramsize);
1553 }
1554 skip_white_(t);
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001555 brace = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001556 if (tok_is_(t, "{"))
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001557 brace = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001558 (*params)[(*nparam)++] = t;
1559 while (tok_isnt_(t, brace ? "}" : ","))
1560 t = t->next;
1561 if (t) { /* got a comma/brace */
1562 t = t->next;
1563 if (brace) {
1564 /*
1565 * Now we've found the closing brace, look further
1566 * for the comma.
1567 */
1568 skip_white_(t);
1569 if (tok_isnt_(t, ",")) {
1570 error(ERR_NONFATAL,
1571 "braces do not enclose all of macro parameter");
1572 while (tok_isnt_(t, ","))
1573 t = t->next;
1574 }
1575 if (t)
1576 t = t->next; /* eat the comma */
1577 }
1578 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001579 }
1580}
1581
1582/*
H. Peter Anvin76690a12002-04-30 20:52:49 +00001583 * Determine whether one of the various `if' conditions is true or
1584 * not.
1585 *
1586 * We must free the tline we get passed.
1587 */
H. Peter Anvin70055962007-10-11 00:05:31 -07001588static bool if_condition(Token * tline, enum preproc_token ct)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001589{
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001590 enum pp_conditional i = PP_COND(ct);
H. Peter Anvin70055962007-10-11 00:05:31 -07001591 bool j;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001592 Token *t, *tt, **tptr, *origline;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001593 struct tokenval tokval;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001594 expr *evalresult;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001595 enum pp_token_type needtype;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001596
1597 origline = tline;
1598
H. Peter Anvine2c80182005-01-15 22:15:51 +00001599 switch (i) {
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001600 case PPC_IFCTX:
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001601 j = false; /* have we matched yet? */
Victor van den Elzen0e857f12008-07-23 13:21:29 +02001602 while (true) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001603 skip_white_(tline);
Victor van den Elzen0e857f12008-07-23 13:21:29 +02001604 if (!tline)
1605 break;
1606 if (tline->type != TOK_ID) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001607 error(ERR_NONFATAL,
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001608 "`%s' expects context identifiers", pp_directives[ct]);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001609 free_tlist(origline);
1610 return -1;
1611 }
Victor van den Elzen0e857f12008-07-23 13:21:29 +02001612 if (cstk && cstk->name && !nasm_stricmp(tline->text, cstk->name))
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001613 j = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001614 tline = tline->next;
1615 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001616 break;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001617
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001618 case PPC_IFDEF:
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001619 j = false; /* have we matched yet? */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001620 while (tline) {
1621 skip_white_(tline);
1622 if (!tline || (tline->type != TOK_ID &&
1623 (tline->type != TOK_PREPROC_ID ||
1624 tline->text[1] != '$'))) {
1625 error(ERR_NONFATAL,
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001626 "`%s' expects macro identifiers", pp_directives[ct]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001627 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001628 }
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07001629 if (smacro_defined(NULL, tline->text, 0, NULL, true))
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001630 j = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001631 tline = tline->next;
1632 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001633 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001634
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001635 case PPC_IFIDN:
1636 case PPC_IFIDNI:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001637 tline = expand_smacro(tline);
1638 t = tt = tline;
1639 while (tok_isnt_(tt, ","))
1640 tt = tt->next;
1641 if (!tt) {
1642 error(ERR_NONFATAL,
1643 "`%s' expects two comma-separated arguments",
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001644 pp_directives[ct]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001645 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001646 }
1647 tt = tt->next;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001648 j = true; /* assume equality unless proved not */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001649 while ((t->type != TOK_OTHER || strcmp(t->text, ",")) && tt) {
1650 if (tt->type == TOK_OTHER && !strcmp(tt->text, ",")) {
1651 error(ERR_NONFATAL, "`%s': more than one comma on line",
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001652 pp_directives[ct]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001653 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001654 }
1655 if (t->type == TOK_WHITESPACE) {
1656 t = t->next;
1657 continue;
1658 }
1659 if (tt->type == TOK_WHITESPACE) {
1660 tt = tt->next;
1661 continue;
1662 }
1663 if (tt->type != t->type) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001664 j = false; /* found mismatching tokens */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001665 break;
1666 }
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07001667 /* When comparing strings, need to unquote them first */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001668 if (t->type == TOK_STRING) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001669 size_t l1 = nasm_unquote(t->text, NULL);
1670 size_t l2 = nasm_unquote(tt->text, NULL);
H. Peter Anvind2456592008-06-19 15:04:18 -07001671
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001672 if (l1 != l2) {
1673 j = false;
1674 break;
1675 }
1676 if (mmemcmp(t->text, tt->text, l1, i == PPC_IFIDN)) {
1677 j = false;
1678 break;
1679 }
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07001680 } else if (mstrcmp(tt->text, t->text, i == PPC_IFIDN) != 0) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001681 j = false; /* found mismatching tokens */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001682 break;
1683 }
Nickolay Yurchenkof3b3ce22003-09-21 20:38:43 +00001684
H. Peter Anvine2c80182005-01-15 22:15:51 +00001685 t = t->next;
1686 tt = tt->next;
1687 }
1688 if ((t->type != TOK_OTHER || strcmp(t->text, ",")) || tt)
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001689 j = false; /* trailing gunk on one end or other */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001690 break;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001691
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001692 case PPC_IFMACRO:
H. Peter Anvin89cee572009-07-15 09:16:54 -04001693 {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001694 bool found = false;
1695 MMacro searching, *mmac;
H. Peter Anvin65747262002-05-07 00:10:05 +00001696
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001697 skip_white_(tline);
1698 tline = expand_id(tline);
1699 if (!tok_type_(tline, TOK_ID)) {
1700 error(ERR_NONFATAL,
1701 "`%s' expects a macro name", pp_directives[ct]);
1702 goto fail;
1703 }
1704 searching.name = nasm_strdup(tline->text);
1705 searching.casesense = true;
1706 searching.plus = false;
1707 searching.nolist = false;
1708 searching.in_progress = 0;
1709 searching.max_depth = 0;
1710 searching.rep_nest = NULL;
1711 searching.nparam_min = 0;
1712 searching.nparam_max = INT_MAX;
1713 tline = expand_smacro(tline->next);
1714 skip_white_(tline);
1715 if (!tline) {
1716 } else if (!tok_type_(tline, TOK_NUMBER)) {
1717 error(ERR_NONFATAL,
1718 "`%s' expects a parameter count or nothing",
1719 pp_directives[ct]);
1720 } else {
1721 searching.nparam_min = searching.nparam_max =
1722 readnum(tline->text, &j);
1723 if (j)
1724 error(ERR_NONFATAL,
1725 "unable to parse parameter count `%s'",
1726 tline->text);
1727 }
1728 if (tline && tok_is_(tline->next, "-")) {
1729 tline = tline->next->next;
1730 if (tok_is_(tline, "*"))
1731 searching.nparam_max = INT_MAX;
1732 else if (!tok_type_(tline, TOK_NUMBER))
1733 error(ERR_NONFATAL,
1734 "`%s' expects a parameter count after `-'",
1735 pp_directives[ct]);
1736 else {
1737 searching.nparam_max = readnum(tline->text, &j);
1738 if (j)
1739 error(ERR_NONFATAL,
1740 "unable to parse parameter count `%s'",
1741 tline->text);
1742 if (searching.nparam_min > searching.nparam_max)
1743 error(ERR_NONFATAL,
1744 "minimum parameter count exceeds maximum");
1745 }
1746 }
1747 if (tline && tok_is_(tline->next, "+")) {
1748 tline = tline->next;
1749 searching.plus = true;
1750 }
1751 mmac = (MMacro *) hash_findix(&mmacros, searching.name);
1752 while (mmac) {
1753 if (!strcmp(mmac->name, searching.name) &&
1754 (mmac->nparam_min <= searching.nparam_max
1755 || searching.plus)
1756 && (searching.nparam_min <= mmac->nparam_max
1757 || mmac->plus)) {
1758 found = true;
1759 break;
1760 }
1761 mmac = mmac->next;
1762 }
1763 if (tline && tline->next)
1764 error(ERR_WARNING|ERR_PASS1,
1765 "trailing garbage after %%ifmacro ignored");
1766 nasm_free(searching.name);
1767 j = found;
1768 break;
H. Peter Anvin89cee572009-07-15 09:16:54 -04001769 }
H. Peter Anvin65747262002-05-07 00:10:05 +00001770
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001771 case PPC_IFID:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001772 needtype = TOK_ID;
1773 goto iftype;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001774 case PPC_IFNUM:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001775 needtype = TOK_NUMBER;
1776 goto iftype;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001777 case PPC_IFSTR:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001778 needtype = TOK_STRING;
1779 goto iftype;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001780
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001781iftype:
1782 t = tline = expand_smacro(tline);
H. Peter Anvind85d2502008-05-04 17:53:31 -07001783
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001784 while (tok_type_(t, TOK_WHITESPACE) ||
1785 (needtype == TOK_NUMBER &&
1786 tok_type_(t, TOK_OTHER) &&
1787 (t->text[0] == '-' || t->text[0] == '+') &&
1788 !t->text[1]))
1789 t = t->next;
H. Peter Anvind85d2502008-05-04 17:53:31 -07001790
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001791 j = tok_type_(t, needtype);
1792 break;
H. Peter Anvincbf768d2008-02-16 16:41:25 -08001793
1794 case PPC_IFTOKEN:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001795 t = tline = expand_smacro(tline);
1796 while (tok_type_(t, TOK_WHITESPACE))
1797 t = t->next;
H. Peter Anvincbf768d2008-02-16 16:41:25 -08001798
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001799 j = false;
1800 if (t) {
1801 t = t->next; /* Skip the actual token */
1802 while (tok_type_(t, TOK_WHITESPACE))
1803 t = t->next;
1804 j = !t; /* Should be nothing left */
1805 }
1806 break;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001807
H. Peter Anvin134b9462008-02-16 17:01:40 -08001808 case PPC_IFEMPTY:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001809 t = tline = expand_smacro(tline);
1810 while (tok_type_(t, TOK_WHITESPACE))
1811 t = t->next;
H. Peter Anvin134b9462008-02-16 17:01:40 -08001812
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001813 j = !t; /* Should be empty */
1814 break;
H. Peter Anvin134b9462008-02-16 17:01:40 -08001815
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001816 case PPC_IF:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001817 t = tline = expand_smacro(tline);
1818 tptr = &t;
1819 tokval.t_type = TOKEN_INVALID;
1820 evalresult = evaluate(ppscan, tptr, &tokval,
1821 NULL, pass | CRITICAL, error, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001822 if (!evalresult)
1823 return -1;
1824 if (tokval.t_type)
H. Peter Anvin917a3492008-09-24 09:14:49 -07001825 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +00001826 "trailing garbage after expression ignored");
1827 if (!is_simple(evalresult)) {
1828 error(ERR_NONFATAL,
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001829 "non-constant value given to `%s'", pp_directives[ct]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001830 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001831 }
Chuck Crayne60ae75d2007-05-02 01:59:16 +00001832 j = reloc_value(evalresult) != 0;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001833 break;
H. Peter Anvin95e28822007-09-12 04:20:08 +00001834
H. Peter Anvine2c80182005-01-15 22:15:51 +00001835 default:
1836 error(ERR_FATAL,
1837 "preprocessor directive `%s' not yet implemented",
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001838 pp_directives[ct]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001839 goto fail;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001840 }
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001841
1842 free_tlist(origline);
1843 return j ^ PP_NEGATIVE(ct);
H. Peter Anvin70653092007-10-19 14:42:29 -07001844
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001845fail:
1846 free_tlist(origline);
1847 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001848}
1849
1850/*
H. Peter Anvin4db5a162007-10-11 13:42:09 -07001851 * Common code for defining an smacro
1852 */
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001853static bool define_smacro(Context *ctx, const char *mname, bool casesense,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001854 int nparam, Token *expansion)
H. Peter Anvin4db5a162007-10-11 13:42:09 -07001855{
1856 SMacro *smac, **smhead;
H. Peter Anvin166c2472008-05-28 12:28:58 -07001857 struct hash_table *smtbl;
H. Peter Anvin70653092007-10-19 14:42:29 -07001858
H. Peter Anvin4db5a162007-10-11 13:42:09 -07001859 if (smacro_defined(ctx, mname, nparam, &smac, casesense)) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001860 if (!smac) {
1861 error(ERR_WARNING|ERR_PASS1,
1862 "single-line macro `%s' defined both with and"
1863 " without parameters", mname);
1864 /*
1865 * Some instances of the old code considered this a failure,
1866 * some others didn't. What is the right thing to do here?
1867 */
1868 free_tlist(expansion);
1869 return false; /* Failure */
1870 } else {
1871 /*
1872 * We're redefining, so we have to take over an
1873 * existing SMacro structure. This means freeing
1874 * what was already in it.
1875 */
1876 nasm_free(smac->name);
1877 free_tlist(smac->expansion);
1878 }
H. Peter Anvin4db5a162007-10-11 13:42:09 -07001879 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001880 smtbl = ctx ? &ctx->localmac : &smacros;
1881 smhead = (SMacro **) hash_findi_add(smtbl, mname);
1882 smac = nasm_malloc(sizeof(SMacro));
1883 smac->next = *smhead;
1884 *smhead = smac;
H. Peter Anvin4db5a162007-10-11 13:42:09 -07001885 }
1886 smac->name = nasm_strdup(mname);
1887 smac->casesense = casesense;
1888 smac->nparam = nparam;
1889 smac->expansion = expansion;
1890 smac->in_progress = false;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001891 return true; /* Success */
H. Peter Anvin4db5a162007-10-11 13:42:09 -07001892}
1893
1894/*
1895 * Undefine an smacro
1896 */
1897static void undef_smacro(Context *ctx, const char *mname)
1898{
1899 SMacro **smhead, *s, **sp;
H. Peter Anvin166c2472008-05-28 12:28:58 -07001900 struct hash_table *smtbl;
H. Peter Anvin4db5a162007-10-11 13:42:09 -07001901
H. Peter Anvin166c2472008-05-28 12:28:58 -07001902 smtbl = ctx ? &ctx->localmac : &smacros;
1903 smhead = (SMacro **)hash_findi(smtbl, mname, NULL);
H. Peter Anvin70653092007-10-19 14:42:29 -07001904
H. Peter Anvin4db5a162007-10-11 13:42:09 -07001905 if (smhead) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001906 /*
1907 * We now have a macro name... go hunt for it.
1908 */
1909 sp = smhead;
1910 while ((s = *sp) != NULL) {
1911 if (!mstrcmp(s->name, mname, s->casesense)) {
1912 *sp = s->next;
1913 nasm_free(s->name);
1914 free_tlist(s->expansion);
1915 nasm_free(s);
1916 } else {
1917 sp = &s->next;
1918 }
1919 }
H. Peter Anvin4db5a162007-10-11 13:42:09 -07001920 }
1921}
1922
H. Peter Anvin8781cb02007-11-08 20:01:11 -08001923/*
H. Peter Anvina26433d2008-07-16 14:40:01 -07001924 * Parse a mmacro specification.
1925 */
1926static bool parse_mmacro_spec(Token *tline, MMacro *def, const char *directive)
1927{
1928 bool err;
1929
1930 tline = tline->next;
1931 skip_white_(tline);
1932 tline = expand_id(tline);
1933 if (!tok_type_(tline, TOK_ID)) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001934 error(ERR_NONFATAL, "`%s' expects a macro name", directive);
1935 return false;
H. Peter Anvina26433d2008-07-16 14:40:01 -07001936 }
Victor van den Elzenb916ede2008-07-23 15:14:22 +02001937
H. Peter Anvin89cee572009-07-15 09:16:54 -04001938 def->prev = NULL;
H. Peter Anvina26433d2008-07-16 14:40:01 -07001939 def->name = nasm_strdup(tline->text);
1940 def->plus = false;
1941 def->nolist = false;
1942 def->in_progress = 0;
1943 def->rep_nest = NULL;
Victor van den Elzenb916ede2008-07-23 15:14:22 +02001944 def->nparam_min = 0;
1945 def->nparam_max = 0;
1946
H. Peter Anvina26433d2008-07-16 14:40:01 -07001947 tline = expand_smacro(tline->next);
1948 skip_white_(tline);
1949 if (!tok_type_(tline, TOK_NUMBER)) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001950 error(ERR_NONFATAL, "`%s' expects a parameter count", directive);
H. Peter Anvina26433d2008-07-16 14:40:01 -07001951 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001952 def->nparam_min = def->nparam_max =
1953 readnum(tline->text, &err);
1954 if (err)
1955 error(ERR_NONFATAL,
1956 "unable to parse parameter count `%s'", tline->text);
H. Peter Anvina26433d2008-07-16 14:40:01 -07001957 }
1958 if (tline && tok_is_(tline->next, "-")) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001959 tline = tline->next->next;
1960 if (tok_is_(tline, "*")) {
1961 def->nparam_max = INT_MAX;
1962 } else if (!tok_type_(tline, TOK_NUMBER)) {
1963 error(ERR_NONFATAL,
1964 "`%s' expects a parameter count after `-'", directive);
1965 } else {
1966 def->nparam_max = readnum(tline->text, &err);
1967 if (err) {
1968 error(ERR_NONFATAL, "unable to parse parameter count `%s'",
1969 tline->text);
1970 }
1971 if (def->nparam_min > def->nparam_max) {
1972 error(ERR_NONFATAL, "minimum parameter count exceeds maximum");
1973 }
1974 }
H. Peter Anvina26433d2008-07-16 14:40:01 -07001975 }
1976 if (tline && tok_is_(tline->next, "+")) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001977 tline = tline->next;
1978 def->plus = true;
H. Peter Anvina26433d2008-07-16 14:40:01 -07001979 }
1980 if (tline && tok_type_(tline->next, TOK_ID) &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001981 !nasm_stricmp(tline->next->text, ".nolist")) {
1982 tline = tline->next;
1983 def->nolist = true;
H. Peter Anvina26433d2008-07-16 14:40:01 -07001984 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001985
H. Peter Anvina26433d2008-07-16 14:40:01 -07001986 /*
1987 * Handle default parameters.
1988 */
1989 if (tline && tline->next) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001990 def->dlist = tline->next;
1991 tline->next = NULL;
1992 count_mmac_params(def->dlist, &def->ndefs, &def->defaults);
H. Peter Anvina26433d2008-07-16 14:40:01 -07001993 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001994 def->dlist = NULL;
1995 def->defaults = NULL;
H. Peter Anvina26433d2008-07-16 14:40:01 -07001996 }
1997 def->expansion = NULL;
1998
H. Peter Anvin89cee572009-07-15 09:16:54 -04001999 if (def->defaults && def->ndefs > def->nparam_max - def->nparam_min &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002000 !def->plus)
2001 error(ERR_WARNING|ERR_PASS1|ERR_WARN_MDP,
2002 "too many default macro parameters");
Victor van den Elzenb916ede2008-07-23 15:14:22 +02002003
H. Peter Anvina26433d2008-07-16 14:40:01 -07002004 return true;
2005}
2006
2007
2008/*
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002009 * Decode a size directive
2010 */
2011static int parse_size(const char *str) {
2012 static const char *size_names[] =
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002013 { "byte", "dword", "oword", "qword", "tword", "word", "yword" };
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002014 static const int sizes[] =
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002015 { 0, 1, 4, 16, 8, 10, 2, 32 };
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002016
2017 return sizes[bsii(str, size_names, elements(size_names))+1];
2018}
2019
H. Peter Anvinf9c9a672009-07-14 15:14:05 -04002020/*
2021 * nasm_unquote with error if the string contains NUL characters.
2022 * If the string contains NUL characters, issue an error and return
2023 * the C len, i.e. truncate at the NUL.
2024 */
2025static size_t nasm_unquote_cstr(char *qstr, enum preproc_token directive)
2026{
2027 size_t len = nasm_unquote(qstr, NULL);
2028 size_t clen = strlen(qstr);
2029
2030 if (len != clen)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002031 error(ERR_NONFATAL, "NUL character in `%s' directive",
2032 pp_directives[directive]);
H. Peter Anvinf9c9a672009-07-14 15:14:05 -04002033
2034 return clen;
2035}
2036
Ed Beroset3ab3f412002-06-11 03:31:49 +00002037/**
2038 * find and process preprocessor directive in passed line
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002039 * Find out if a line contains a preprocessor directive, and deal
2040 * with it if so.
H. Peter Anvin70653092007-10-19 14:42:29 -07002041 *
Ed Beroset3ab3f412002-06-11 03:31:49 +00002042 * If a directive _is_ found, it is the responsibility of this routine
2043 * (and not the caller) to free_tlist() the line.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002044 *
Ed Beroset3ab3f412002-06-11 03:31:49 +00002045 * @param tline a pointer to the current tokeninzed line linked list
2046 * @return DIRECTIVE_FOUND or NO_DIRECTIVE_FOUND
H. Peter Anvin70653092007-10-19 14:42:29 -07002047 *
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002048 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002049static int do_directive(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00002050{
H. Peter Anvin4169a472007-09-12 01:29:43 +00002051 enum preproc_token i;
2052 int j;
H. Peter Anvin70055962007-10-11 00:05:31 -07002053 bool err;
2054 int nparam;
2055 bool nolist;
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07002056 bool casesense;
H. Peter Anvin8cfdb9d2007-09-14 18:36:01 -07002057 int k, m;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002058 int offset;
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08002059 char *p, *pp;
2060 const char *mname;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002061 Include *inc;
2062 Context *ctx;
2063 Cond *cond;
H. Peter Anvin97a23472007-09-16 17:57:25 -07002064 MMacro *mmac, **mmhead;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002065 Token *t, *tt, *param_start, *macro_start, *last, **tptr, *origline;
2066 Line *l;
2067 struct tokenval tokval;
2068 expr *evalresult;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002069 MMacro *tmp_defining; /* Used when manipulating rep_nest */
H. Peter Anvinf8ba53e2007-10-11 10:11:57 -07002070 int64_t count;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07002071 size_t len;
H. Peter Anvin8e3f75e2008-09-24 00:21:58 -07002072 int severity;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002073
2074 origline = tline;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002075
H. Peter Anvineba20a72002-04-30 20:53:55 +00002076 skip_white_(tline);
H. Peter Anvinf2936d72008-06-04 15:11:23 -07002077 if (!tline || !tok_type_(tline, TOK_PREPROC_ID) ||
H. Peter Anvine2c80182005-01-15 22:15:51 +00002078 (tline->text[1] == '%' || tline->text[1] == '$'
2079 || tline->text[1] == '!'))
2080 return NO_DIRECTIVE_FOUND;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002081
H. Peter Anvin4169a472007-09-12 01:29:43 +00002082 i = pp_token_hash(tline->text);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002083
2084 /*
Cyrill Gorcunovf09116f2010-02-28 11:52:53 +03002085 * FIXME: We zap execution of PP_RMACRO, PP_IRMACRO, PP_EXITMACRO
2086 * since they are known to be buggy at moment, we need to fix them
2087 * in future release (2.09-2.10)
2088 */
2089 if (i == PP_RMACRO || i == PP_RMACRO || i == PP_EXITMACRO) {
2090 error(ERR_NONFATAL, "unknown preprocessor directive `%s'",
2091 tline->text);
2092 return NO_DIRECTIVE_FOUND;
2093 }
2094
2095 /*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002096 * If we're in a non-emitting branch of a condition construct,
H. Peter Anvin76690a12002-04-30 20:52:49 +00002097 * or walking to the end of an already terminated %rep block,
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002098 * we should ignore all directives except for condition
2099 * directives.
2100 */
H. Peter Anvin76690a12002-04-30 20:52:49 +00002101 if (((istk->conds && !emitting(istk->conds->state)) ||
H. Peter Anvine2c80182005-01-15 22:15:51 +00002102 (istk->mstk && !istk->mstk->in_progress)) && !is_condition(i)) {
2103 return NO_DIRECTIVE_FOUND;
H. Peter Anvineba20a72002-04-30 20:53:55 +00002104 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002105
2106 /*
H. Peter Anvin76690a12002-04-30 20:52:49 +00002107 * If we're defining a macro or reading a %rep block, we should
Victor van den Elzen8f1120f2008-07-16 13:41:37 +02002108 * ignore all directives except for %macro/%imacro (which nest),
2109 * %endm/%endmacro, and (only if we're in a %rep block) %endrep.
2110 * If we're in a %rep block, another %rep nests, so should be let through.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002111 */
2112 if (defining && i != PP_MACRO && i != PP_IMACRO &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002113 i != PP_RMACRO && i != PP_IRMACRO &&
H. Peter Anvine2c80182005-01-15 22:15:51 +00002114 i != PP_ENDMACRO && i != PP_ENDM &&
2115 (defining->name || (i != PP_ENDREP && i != PP_REP))) {
2116 return NO_DIRECTIVE_FOUND;
H. Peter Anvineba20a72002-04-30 20:53:55 +00002117 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002118
Charles Crayned4200be2008-07-12 16:42:33 -07002119 if (defining) {
Keith Kanios852f1ee2009-07-12 00:19:55 -05002120 if (i == PP_MACRO || i == PP_IMACRO ||
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002121 i == PP_RMACRO || i == PP_IRMACRO) {
Charles Crayned4200be2008-07-12 16:42:33 -07002122 nested_mac_count++;
2123 return NO_DIRECTIVE_FOUND;
2124 } else if (nested_mac_count > 0) {
2125 if (i == PP_ENDMACRO) {
2126 nested_mac_count--;
2127 return NO_DIRECTIVE_FOUND;
2128 }
2129 }
2130 if (!defining->name) {
2131 if (i == PP_REP) {
2132 nested_rep_count++;
2133 return NO_DIRECTIVE_FOUND;
2134 } else if (nested_rep_count > 0) {
2135 if (i == PP_ENDREP) {
2136 nested_rep_count--;
2137 return NO_DIRECTIVE_FOUND;
2138 }
2139 }
2140 }
2141 }
2142
H. Peter Anvin4169a472007-09-12 01:29:43 +00002143 switch (i) {
2144 case PP_INVALID:
H. Peter Anvine2c80182005-01-15 22:15:51 +00002145 error(ERR_NONFATAL, "unknown preprocessor directive `%s'",
2146 tline->text);
2147 return NO_DIRECTIVE_FOUND; /* didn't get it */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002148
H. Peter Anvine2c80182005-01-15 22:15:51 +00002149 case PP_STACKSIZE:
2150 /* Directive to tell NASM what the default stack size is. The
2151 * default is for a 16-bit stack, and this can be overriden with
2152 * %stacksize large.
H. Peter Anvine2c80182005-01-15 22:15:51 +00002153 */
2154 tline = tline->next;
2155 if (tline && tline->type == TOK_WHITESPACE)
2156 tline = tline->next;
2157 if (!tline || tline->type != TOK_ID) {
2158 error(ERR_NONFATAL, "`%%stacksize' missing size parameter");
2159 free_tlist(origline);
2160 return DIRECTIVE_FOUND;
2161 }
2162 if (nasm_stricmp(tline->text, "flat") == 0) {
2163 /* All subsequent ARG directives are for a 32-bit stack */
2164 StackSize = 4;
2165 StackPointer = "ebp";
2166 ArgOffset = 8;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002167 LocalOffset = 0;
Charles Crayne7eaf9192007-11-08 22:11:14 -08002168 } else if (nasm_stricmp(tline->text, "flat64") == 0) {
2169 /* All subsequent ARG directives are for a 64-bit stack */
2170 StackSize = 8;
2171 StackPointer = "rbp";
Per Jessen53252e02010-02-11 00:16:59 +03002172 ArgOffset = 16;
Charles Crayne7eaf9192007-11-08 22:11:14 -08002173 LocalOffset = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002174 } else if (nasm_stricmp(tline->text, "large") == 0) {
2175 /* All subsequent ARG directives are for a 16-bit stack,
2176 * far function call.
2177 */
2178 StackSize = 2;
2179 StackPointer = "bp";
2180 ArgOffset = 4;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002181 LocalOffset = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002182 } else if (nasm_stricmp(tline->text, "small") == 0) {
2183 /* All subsequent ARG directives are for a 16-bit stack,
2184 * far function call. We don't support near functions.
2185 */
2186 StackSize = 2;
2187 StackPointer = "bp";
2188 ArgOffset = 6;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002189 LocalOffset = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002190 } else {
2191 error(ERR_NONFATAL, "`%%stacksize' invalid size type");
2192 free_tlist(origline);
2193 return DIRECTIVE_FOUND;
2194 }
2195 free_tlist(origline);
2196 return DIRECTIVE_FOUND;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002197
H. Peter Anvine2c80182005-01-15 22:15:51 +00002198 case PP_ARG:
2199 /* TASM like ARG directive to define arguments to functions, in
2200 * the following form:
2201 *
2202 * ARG arg1:WORD, arg2:DWORD, arg4:QWORD
2203 */
2204 offset = ArgOffset;
2205 do {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00002206 char *arg, directive[256];
H. Peter Anvine2c80182005-01-15 22:15:51 +00002207 int size = StackSize;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002208
H. Peter Anvine2c80182005-01-15 22:15:51 +00002209 /* Find the argument name */
2210 tline = tline->next;
2211 if (tline && tline->type == TOK_WHITESPACE)
2212 tline = tline->next;
2213 if (!tline || tline->type != TOK_ID) {
2214 error(ERR_NONFATAL, "`%%arg' missing argument parameter");
2215 free_tlist(origline);
2216 return DIRECTIVE_FOUND;
2217 }
2218 arg = tline->text;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002219
H. Peter Anvine2c80182005-01-15 22:15:51 +00002220 /* Find the argument size type */
2221 tline = tline->next;
2222 if (!tline || tline->type != TOK_OTHER
2223 || tline->text[0] != ':') {
2224 error(ERR_NONFATAL,
2225 "Syntax error processing `%%arg' directive");
2226 free_tlist(origline);
2227 return DIRECTIVE_FOUND;
2228 }
2229 tline = tline->next;
2230 if (!tline || tline->type != TOK_ID) {
2231 error(ERR_NONFATAL, "`%%arg' missing size type parameter");
2232 free_tlist(origline);
2233 return DIRECTIVE_FOUND;
2234 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002235
H. Peter Anvine2c80182005-01-15 22:15:51 +00002236 /* Allow macro expansion of type parameter */
Keith Kaniosb7a89542007-04-12 02:40:54 +00002237 tt = tokenize(tline->text);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002238 tt = expand_smacro(tt);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002239 size = parse_size(tt->text);
2240 if (!size) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002241 error(ERR_NONFATAL,
2242 "Invalid size type for `%%arg' missing directive");
2243 free_tlist(tt);
2244 free_tlist(origline);
2245 return DIRECTIVE_FOUND;
2246 }
2247 free_tlist(tt);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002248
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002249 /* Round up to even stack slots */
2250 size = ALIGN(size, StackSize);
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002251
H. Peter Anvine2c80182005-01-15 22:15:51 +00002252 /* Now define the macro for the argument */
2253 snprintf(directive, sizeof(directive), "%%define %s (%s+%d)",
2254 arg, StackPointer, offset);
Keith Kaniosb7a89542007-04-12 02:40:54 +00002255 do_directive(tokenize(directive));
H. Peter Anvine2c80182005-01-15 22:15:51 +00002256 offset += size;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002257
H. Peter Anvine2c80182005-01-15 22:15:51 +00002258 /* Move to the next argument in the list */
2259 tline = tline->next;
2260 if (tline && tline->type == TOK_WHITESPACE)
2261 tline = tline->next;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002262 } while (tline && tline->type == TOK_OTHER && tline->text[0] == ',');
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002263 ArgOffset = offset;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002264 free_tlist(origline);
2265 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002266
H. Peter Anvine2c80182005-01-15 22:15:51 +00002267 case PP_LOCAL:
2268 /* TASM like LOCAL directive to define local variables for a
2269 * function, in the following form:
2270 *
2271 * LOCAL local1:WORD, local2:DWORD, local4:QWORD = LocalSize
2272 *
2273 * The '= LocalSize' at the end is ignored by NASM, but is
2274 * required by TASM to define the local parameter size (and used
2275 * by the TASM macro package).
2276 */
2277 offset = LocalOffset;
2278 do {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00002279 char *local, directive[256];
H. Peter Anvine2c80182005-01-15 22:15:51 +00002280 int size = StackSize;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002281
H. Peter Anvine2c80182005-01-15 22:15:51 +00002282 /* Find the argument name */
2283 tline = tline->next;
2284 if (tline && tline->type == TOK_WHITESPACE)
2285 tline = tline->next;
2286 if (!tline || tline->type != TOK_ID) {
2287 error(ERR_NONFATAL,
2288 "`%%local' missing argument parameter");
2289 free_tlist(origline);
2290 return DIRECTIVE_FOUND;
2291 }
2292 local = tline->text;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002293
H. Peter Anvine2c80182005-01-15 22:15:51 +00002294 /* Find the argument size type */
2295 tline = tline->next;
2296 if (!tline || tline->type != TOK_OTHER
2297 || tline->text[0] != ':') {
2298 error(ERR_NONFATAL,
2299 "Syntax error processing `%%local' directive");
2300 free_tlist(origline);
2301 return DIRECTIVE_FOUND;
2302 }
2303 tline = tline->next;
2304 if (!tline || tline->type != TOK_ID) {
2305 error(ERR_NONFATAL,
2306 "`%%local' missing size type parameter");
2307 free_tlist(origline);
2308 return DIRECTIVE_FOUND;
2309 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00002310
H. Peter Anvine2c80182005-01-15 22:15:51 +00002311 /* Allow macro expansion of type parameter */
Keith Kaniosb7a89542007-04-12 02:40:54 +00002312 tt = tokenize(tline->text);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002313 tt = expand_smacro(tt);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002314 size = parse_size(tt->text);
2315 if (!size) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002316 error(ERR_NONFATAL,
2317 "Invalid size type for `%%local' missing directive");
2318 free_tlist(tt);
2319 free_tlist(origline);
2320 return DIRECTIVE_FOUND;
2321 }
2322 free_tlist(tt);
H. Peter Anvin734b1882002-04-30 21:01:08 +00002323
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002324 /* Round up to even stack slots */
2325 size = ALIGN(size, StackSize);
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002326
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002327 offset += size; /* Negative offset, increment before */
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002328
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002329 /* Now define the macro for the argument */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002330 snprintf(directive, sizeof(directive), "%%define %s (%s-%d)",
2331 local, StackPointer, offset);
Keith Kaniosb7a89542007-04-12 02:40:54 +00002332 do_directive(tokenize(directive));
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002333
H. Peter Anvine2c80182005-01-15 22:15:51 +00002334 /* Now define the assign to setup the enter_c macro correctly */
2335 snprintf(directive, sizeof(directive),
2336 "%%assign %%$localsize %%$localsize+%d", size);
Keith Kaniosb7a89542007-04-12 02:40:54 +00002337 do_directive(tokenize(directive));
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002338
H. Peter Anvine2c80182005-01-15 22:15:51 +00002339 /* Move to the next argument in the list */
2340 tline = tline->next;
2341 if (tline && tline->type == TOK_WHITESPACE)
2342 tline = tline->next;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002343 } while (tline && tline->type == TOK_OTHER && tline->text[0] == ',');
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002344 LocalOffset = offset;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002345 free_tlist(origline);
2346 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002347
H. Peter Anvine2c80182005-01-15 22:15:51 +00002348 case PP_CLEAR:
2349 if (tline->next)
H. Peter Anvin917a3492008-09-24 09:14:49 -07002350 error(ERR_WARNING|ERR_PASS1,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002351 "trailing garbage after `%%clear' ignored");
2352 free_macros();
2353 init_macros();
H. Peter Anvine2c80182005-01-15 22:15:51 +00002354 free_tlist(origline);
2355 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002356
H. Peter Anvin418ca702008-05-30 10:42:30 -07002357 case PP_DEPEND:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002358 t = tline->next = expand_smacro(tline->next);
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002359 skip_white_(t);
2360 if (!t || (t->type != TOK_STRING &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002361 t->type != TOK_INTERNAL_STRING)) {
H. Peter Anvin418ca702008-05-30 10:42:30 -07002362 error(ERR_NONFATAL, "`%%depend' expects a file name");
2363 free_tlist(origline);
2364 return DIRECTIVE_FOUND; /* but we did _something_ */
2365 }
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002366 if (t->next)
H. Peter Anvin917a3492008-09-24 09:14:49 -07002367 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvin418ca702008-05-30 10:42:30 -07002368 "trailing garbage after `%%depend' ignored");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002369 p = t->text;
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002370 if (t->type != TOK_INTERNAL_STRING)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002371 nasm_unquote_cstr(p, i);
2372 if (dephead && !in_list(*dephead, p)) {
2373 StrList *sl = nasm_malloc(strlen(p)+1+sizeof sl->next);
2374 sl->next = NULL;
2375 strcpy(sl->str, p);
2376 *deptail = sl;
2377 deptail = &sl->next;
2378 }
2379 free_tlist(origline);
H. Peter Anvin418ca702008-05-30 10:42:30 -07002380 return DIRECTIVE_FOUND;
2381
2382 case PP_INCLUDE:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002383 t = tline->next = expand_smacro(tline->next);
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002384 skip_white_(t);
H. Peter Anvind2456592008-06-19 15:04:18 -07002385
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002386 if (!t || (t->type != TOK_STRING &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002387 t->type != TOK_INTERNAL_STRING)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002388 error(ERR_NONFATAL, "`%%include' expects a file name");
2389 free_tlist(origline);
2390 return DIRECTIVE_FOUND; /* but we did _something_ */
2391 }
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002392 if (t->next)
H. Peter Anvin917a3492008-09-24 09:14:49 -07002393 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +00002394 "trailing garbage after `%%include' ignored");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002395 p = t->text;
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002396 if (t->type != TOK_INTERNAL_STRING)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002397 nasm_unquote_cstr(p, i);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002398 inc = nasm_malloc(sizeof(Include));
2399 inc->next = istk;
2400 inc->conds = NULL;
H. Peter Anvin2b1c3b92008-06-06 10:38:46 -07002401 inc->fp = inc_fopen(p, dephead, &deptail, pass == 0);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002402 if (!inc->fp) {
2403 /* -MG given but file not found */
2404 nasm_free(inc);
2405 } else {
2406 inc->fname = src_set_fname(nasm_strdup(p));
2407 inc->lineno = src_set_linnum(0);
2408 inc->lineinc = 1;
2409 inc->expansion = NULL;
2410 inc->mstk = NULL;
2411 istk = inc;
2412 list->uplevel(LIST_INCLUDE);
2413 }
2414 free_tlist(origline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002415 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002416
H. Peter Anvind2456592008-06-19 15:04:18 -07002417 case PP_USE:
H. Peter Anvinf4ae5ad2008-06-19 18:39:24 -07002418 {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002419 static macros_t *use_pkg;
2420 const char *pkg_macro = NULL;
H. Peter Anvinf4ae5ad2008-06-19 18:39:24 -07002421
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002422 tline = tline->next;
2423 skip_white_(tline);
2424 tline = expand_id(tline);
H. Peter Anvind2456592008-06-19 15:04:18 -07002425
H. Peter Anvin264b7b92008-10-24 16:38:17 -07002426 if (!tline || (tline->type != TOK_STRING &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002427 tline->type != TOK_INTERNAL_STRING &&
2428 tline->type != TOK_ID)) {
H. Peter Anvin926fc402008-06-19 16:26:12 -07002429 error(ERR_NONFATAL, "`%%use' expects a package name");
H. Peter Anvind2456592008-06-19 15:04:18 -07002430 free_tlist(origline);
2431 return DIRECTIVE_FOUND; /* but we did _something_ */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002432 }
H. Peter Anvin264b7b92008-10-24 16:38:17 -07002433 if (tline->next)
H. Peter Anvin917a3492008-09-24 09:14:49 -07002434 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvind2456592008-06-19 15:04:18 -07002435 "trailing garbage after `%%use' ignored");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002436 if (tline->type == TOK_STRING)
2437 nasm_unquote_cstr(tline->text, i);
2438 use_pkg = nasm_stdmac_find_package(tline->text);
2439 if (!use_pkg)
2440 error(ERR_NONFATAL, "unknown `%%use' package: %s", tline->text);
2441 else
2442 pkg_macro = (char *)use_pkg + 1; /* The first string will be <%define>__USE_*__ */
Victor van den Elzen35eb2ea2010-03-10 22:33:48 +01002443 if (use_pkg && ! smacro_defined(NULL, pkg_macro, 0, NULL, true)) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002444 /* Not already included, go ahead and include it */
2445 stdmacpos = use_pkg;
2446 }
2447 free_tlist(origline);
H. Peter Anvind2456592008-06-19 15:04:18 -07002448 return DIRECTIVE_FOUND;
H. Peter Anvinf4ae5ad2008-06-19 18:39:24 -07002449 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002450 case PP_PUSH:
H. Peter Anvine2c80182005-01-15 22:15:51 +00002451 case PP_REPL:
H. Peter Anvin42b56392008-10-24 16:24:21 -07002452 case PP_POP:
H. Peter Anvine2c80182005-01-15 22:15:51 +00002453 tline = tline->next;
2454 skip_white_(tline);
2455 tline = expand_id(tline);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002456 if (tline) {
2457 if (!tok_type_(tline, TOK_ID)) {
2458 error(ERR_NONFATAL, "`%s' expects a context identifier",
2459 pp_directives[i]);
2460 free_tlist(origline);
2461 return DIRECTIVE_FOUND; /* but we did _something_ */
2462 }
2463 if (tline->next)
2464 error(ERR_WARNING|ERR_PASS1,
2465 "trailing garbage after `%s' ignored",
2466 pp_directives[i]);
2467 p = nasm_strdup(tline->text);
2468 } else {
2469 p = NULL; /* Anonymous */
2470 }
H. Peter Anvin42b56392008-10-24 16:24:21 -07002471
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002472 if (i == PP_PUSH) {
2473 ctx = nasm_malloc(sizeof(Context));
2474 ctx->next = cstk;
2475 hash_init(&ctx->localmac, HASH_SMALL);
2476 ctx->name = p;
2477 ctx->number = unique++;
2478 cstk = ctx;
2479 } else {
2480 /* %pop or %repl */
2481 if (!cstk) {
2482 error(ERR_NONFATAL, "`%s': context stack is empty",
2483 pp_directives[i]);
2484 } else if (i == PP_POP) {
2485 if (p && (!cstk->name || nasm_stricmp(p, cstk->name)))
2486 error(ERR_NONFATAL, "`%%pop' in wrong context: %s, "
2487 "expected %s",
2488 cstk->name ? cstk->name : "anonymous", p);
2489 else
2490 ctx_pop();
2491 } else {
2492 /* i == PP_REPL */
2493 nasm_free(cstk->name);
2494 cstk->name = p;
2495 p = NULL;
2496 }
2497 nasm_free(p);
2498 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002499 free_tlist(origline);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002500 return DIRECTIVE_FOUND;
H. Peter Anvin8e3f75e2008-09-24 00:21:58 -07002501 case PP_FATAL:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002502 severity = ERR_FATAL;
2503 goto issue_error;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002504 case PP_ERROR:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002505 severity = ERR_NONFATAL;
2506 goto issue_error;
H. Peter Anvin7df04172008-06-10 18:27:38 -07002507 case PP_WARNING:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002508 severity = ERR_WARNING|ERR_WARN_USER;
2509 goto issue_error;
H. Peter Anvin8e3f75e2008-09-24 00:21:58 -07002510
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002511issue_error:
H. Peter Anvin7df04172008-06-10 18:27:38 -07002512 {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002513 /* Only error out if this is the final pass */
2514 if (pass != 2 && i != PP_FATAL)
2515 return DIRECTIVE_FOUND;
2516
2517 tline->next = expand_smacro(tline->next);
2518 tline = tline->next;
2519 skip_white_(tline);
2520 t = tline ? tline->next : NULL;
2521 skip_white_(t);
2522 if (tok_type_(tline, TOK_STRING) && !t) {
2523 /* The line contains only a quoted string */
2524 p = tline->text;
2525 nasm_unquote(p, NULL); /* Ignore NUL character truncation */
2526 error(severity, "%s", p);
2527 } else {
2528 /* Not a quoted string, or more than a quoted string */
2529 p = detoken(tline, false);
2530 error(severity, "%s", p);
2531 nasm_free(p);
2532 }
2533 free_tlist(origline);
2534 return DIRECTIVE_FOUND;
H. Peter Anvin7df04172008-06-10 18:27:38 -07002535 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00002536
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002537 CASE_PP_IF:
H. Peter Anvine2c80182005-01-15 22:15:51 +00002538 if (istk->conds && !emitting(istk->conds->state))
2539 j = COND_NEVER;
2540 else {
2541 j = if_condition(tline->next, i);
2542 tline->next = NULL; /* it got freed */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002543 j = j < 0 ? COND_NEVER : j ? COND_IF_TRUE : COND_IF_FALSE;
2544 }
2545 cond = nasm_malloc(sizeof(Cond));
2546 cond->next = istk->conds;
2547 cond->state = j;
2548 istk->conds = cond;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002549 if(istk->mstk)
Keith Kanios3c0d91f2009-10-25 13:28:03 -05002550 istk->mstk->condcnt ++;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002551 free_tlist(origline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002552 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002553
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002554 CASE_PP_ELIF:
H. Peter Anvine2c80182005-01-15 22:15:51 +00002555 if (!istk->conds)
H. Peter Anvin4169a472007-09-12 01:29:43 +00002556 error(ERR_FATAL, "`%s': no matching `%%if'", pp_directives[i]);
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002557 switch(istk->conds->state) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002558 case COND_IF_TRUE:
2559 istk->conds->state = COND_DONE;
2560 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002561
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002562 case COND_DONE:
2563 case COND_NEVER:
2564 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002565
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002566 case COND_ELSE_TRUE:
2567 case COND_ELSE_FALSE:
2568 error_precond(ERR_WARNING|ERR_PASS1,
2569 "`%%elif' after `%%else' ignored");
2570 istk->conds->state = COND_NEVER;
2571 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002572
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002573 case COND_IF_FALSE:
2574 /*
2575 * IMPORTANT: In the case of %if, we will already have
2576 * called expand_mmac_params(); however, if we're
2577 * processing an %elif we must have been in a
2578 * non-emitting mode, which would have inhibited
2579 * the normal invocation of expand_mmac_params().
2580 * Therefore, we have to do it explicitly here.
2581 */
2582 j = if_condition(expand_mmac_params(tline->next), i);
2583 tline->next = NULL; /* it got freed */
2584 istk->conds->state =
2585 j < 0 ? COND_NEVER : j ? COND_IF_TRUE : COND_IF_FALSE;
2586 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002587 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002588 free_tlist(origline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002589 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002590
H. Peter Anvine2c80182005-01-15 22:15:51 +00002591 case PP_ELSE:
2592 if (tline->next)
H. Peter Anvin917a3492008-09-24 09:14:49 -07002593 error_precond(ERR_WARNING|ERR_PASS1,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002594 "trailing garbage after `%%else' ignored");
H. Peter Anvine2c80182005-01-15 22:15:51 +00002595 if (!istk->conds)
2596 error(ERR_FATAL, "`%%else': no matching `%%if'");
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002597 switch(istk->conds->state) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002598 case COND_IF_TRUE:
2599 case COND_DONE:
2600 istk->conds->state = COND_ELSE_FALSE;
2601 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002602
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002603 case COND_NEVER:
2604 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002605
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002606 case COND_IF_FALSE:
2607 istk->conds->state = COND_ELSE_TRUE;
2608 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002609
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002610 case COND_ELSE_TRUE:
2611 case COND_ELSE_FALSE:
2612 error_precond(ERR_WARNING|ERR_PASS1,
2613 "`%%else' after `%%else' ignored.");
2614 istk->conds->state = COND_NEVER;
2615 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002616 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002617 free_tlist(origline);
2618 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002619
H. Peter Anvine2c80182005-01-15 22:15:51 +00002620 case PP_ENDIF:
2621 if (tline->next)
H. Peter Anvin917a3492008-09-24 09:14:49 -07002622 error_precond(ERR_WARNING|ERR_PASS1,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002623 "trailing garbage after `%%endif' ignored");
H. Peter Anvine2c80182005-01-15 22:15:51 +00002624 if (!istk->conds)
2625 error(ERR_FATAL, "`%%endif': no matching `%%if'");
2626 cond = istk->conds;
2627 istk->conds = cond->next;
2628 nasm_free(cond);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002629 if(istk->mstk)
Keith Kanios3c0d91f2009-10-25 13:28:03 -05002630 istk->mstk->condcnt --;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002631 free_tlist(origline);
2632 return DIRECTIVE_FOUND;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002633
H. Peter Anvindb8f96e2009-07-15 09:07:29 -04002634 case PP_RMACRO:
2635 case PP_IRMACRO:
H. Peter Anvine2c80182005-01-15 22:15:51 +00002636 case PP_MACRO:
2637 case PP_IMACRO:
H. Peter Anvina26433d2008-07-16 14:40:01 -07002638 if (defining) {
H. Peter Anvindb8f96e2009-07-15 09:07:29 -04002639 error(ERR_FATAL, "`%s': already defining a macro",
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002640 pp_directives[i]);
2641 return DIRECTIVE_FOUND;
2642 }
2643 defining = nasm_malloc(sizeof(MMacro));
2644 defining->max_depth =
2645 (i == PP_RMACRO) || (i == PP_IRMACRO) ? DEADMAN_LIMIT : 0;
2646 defining->casesense = (i == PP_MACRO) || (i == PP_RMACRO);
2647 if (!parse_mmacro_spec(tline, defining, pp_directives[i])) {
2648 nasm_free(defining);
2649 defining = NULL;
2650 return DIRECTIVE_FOUND;
2651 }
H. Peter Anvina26433d2008-07-16 14:40:01 -07002652
H. Peter Anvin166c2472008-05-28 12:28:58 -07002653 mmac = (MMacro *) hash_findix(&mmacros, defining->name);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002654 while (mmac) {
2655 if (!strcmp(mmac->name, defining->name) &&
2656 (mmac->nparam_min <= defining->nparam_max
2657 || defining->plus)
2658 && (defining->nparam_min <= mmac->nparam_max
2659 || mmac->plus)) {
H. Peter Anvin917a3492008-09-24 09:14:49 -07002660 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +00002661 "redefining multi-line macro `%s'", defining->name);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002662 return DIRECTIVE_FOUND;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002663 }
2664 mmac = mmac->next;
2665 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002666 free_tlist(origline);
2667 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002668
H. Peter Anvine2c80182005-01-15 22:15:51 +00002669 case PP_ENDM:
2670 case PP_ENDMACRO:
Victor van den Elzen8f1120f2008-07-16 13:41:37 +02002671 if (! (defining && defining->name)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002672 error(ERR_NONFATAL, "`%s': not defining a macro", tline->text);
2673 return DIRECTIVE_FOUND;
2674 }
Keith Kanios852f1ee2009-07-12 00:19:55 -05002675 mmhead = (MMacro **) hash_findi_add(&mmacros, defining->name);
H. Peter Anvin97a23472007-09-16 17:57:25 -07002676 defining->next = *mmhead;
Keith Kanios852f1ee2009-07-12 00:19:55 -05002677 *mmhead = defining;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002678 defining = NULL;
2679 free_tlist(origline);
2680 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002681
H. Peter Anvin89cee572009-07-15 09:16:54 -04002682 case PP_EXITMACRO:
Keith Kanios852f1ee2009-07-12 00:19:55 -05002683 /*
2684 * We must search along istk->expansion until we hit a
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002685 * macro-end marker for a macro with a name. Then we
Keith Kanios3c0d91f2009-10-25 13:28:03 -05002686 * bypass all lines between exitmacro and endmacro.
Keith Kanios852f1ee2009-07-12 00:19:55 -05002687 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002688 for (l = istk->expansion; l; l = l->next)
Keith Kanios852f1ee2009-07-12 00:19:55 -05002689 if (l->finishes && l->finishes->name)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002690 break;
Keith Kanios852f1ee2009-07-12 00:19:55 -05002691
2692 if (l) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002693 /*
2694 * Remove all conditional entries relative to this
2695 * macro invocation. (safe to do in this context)
2696 */
Keith Kanios3c0d91f2009-10-25 13:28:03 -05002697 for ( ; l->finishes->condcnt > 0; l->finishes->condcnt --) {
2698 cond = istk->conds;
2699 istk->conds = cond->next;
2700 nasm_free(cond);
2701 }
2702 istk->expansion = l;
Keith Kanios852f1ee2009-07-12 00:19:55 -05002703 } else {
2704 error(ERR_NONFATAL, "`%%exitmacro' not within `%%macro' block");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002705 }
Keith Kanios852f1ee2009-07-12 00:19:55 -05002706 free_tlist(origline);
2707 return DIRECTIVE_FOUND;
2708
H. Peter Anvina26433d2008-07-16 14:40:01 -07002709 case PP_UNMACRO:
2710 case PP_UNIMACRO:
2711 {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002712 MMacro **mmac_p;
2713 MMacro spec;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002714
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002715 spec.casesense = (i == PP_UNMACRO);
2716 if (!parse_mmacro_spec(tline, &spec, pp_directives[i])) {
2717 return DIRECTIVE_FOUND;
2718 }
H. Peter Anvina26433d2008-07-16 14:40:01 -07002719 mmac_p = (MMacro **) hash_findi(&mmacros, spec.name, NULL);
2720 while (mmac_p && *mmac_p) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002721 mmac = *mmac_p;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002722 if (mmac->casesense == spec.casesense &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002723 !mstrcmp(mmac->name, spec.name, spec.casesense) &&
H. Peter Anvina26433d2008-07-16 14:40:01 -07002724 mmac->nparam_min == spec.nparam_min &&
2725 mmac->nparam_max == spec.nparam_max &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002726 mmac->plus == spec.plus) {
2727 *mmac_p = mmac->next;
2728 free_mmacro(mmac);
2729 } else {
2730 mmac_p = &mmac->next;
2731 }
2732 }
2733 free_tlist(origline);
2734 free_tlist(spec.dlist);
2735 return DIRECTIVE_FOUND;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002736 }
2737
H. Peter Anvine2c80182005-01-15 22:15:51 +00002738 case PP_ROTATE:
2739 if (tline->next && tline->next->type == TOK_WHITESPACE)
2740 tline = tline->next;
H. Peter Anvin89cee572009-07-15 09:16:54 -04002741 if (!tline->next) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002742 free_tlist(origline);
2743 error(ERR_NONFATAL, "`%%rotate' missing rotate count");
2744 return DIRECTIVE_FOUND;
2745 }
2746 t = expand_smacro(tline->next);
2747 tline->next = NULL;
2748 free_tlist(origline);
2749 tline = t;
2750 tptr = &t;
2751 tokval.t_type = TOKEN_INVALID;
2752 evalresult =
2753 evaluate(ppscan, tptr, &tokval, NULL, pass, error, NULL);
2754 free_tlist(tline);
2755 if (!evalresult)
2756 return DIRECTIVE_FOUND;
2757 if (tokval.t_type)
H. Peter Anvin917a3492008-09-24 09:14:49 -07002758 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +00002759 "trailing garbage after expression ignored");
2760 if (!is_simple(evalresult)) {
2761 error(ERR_NONFATAL, "non-constant value given to `%%rotate'");
2762 return DIRECTIVE_FOUND;
2763 }
2764 mmac = istk->mstk;
2765 while (mmac && !mmac->name) /* avoid mistaking %reps for macros */
2766 mmac = mmac->next_active;
2767 if (!mmac) {
2768 error(ERR_NONFATAL, "`%%rotate' invoked outside a macro call");
2769 } else if (mmac->nparam == 0) {
2770 error(ERR_NONFATAL,
2771 "`%%rotate' invoked within macro without parameters");
2772 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002773 int rotate = mmac->rotate + reloc_value(evalresult);
H. Peter Anvin734b1882002-04-30 21:01:08 +00002774
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002775 rotate %= (int)mmac->nparam;
2776 if (rotate < 0)
2777 rotate += mmac->nparam;
H. Peter Anvin70653092007-10-19 14:42:29 -07002778
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002779 mmac->rotate = rotate;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002780 }
2781 return DIRECTIVE_FOUND;
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00002782
H. Peter Anvine2c80182005-01-15 22:15:51 +00002783 case PP_REP:
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002784 nolist = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002785 do {
2786 tline = tline->next;
2787 } while (tok_type_(tline, TOK_WHITESPACE));
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00002788
H. Peter Anvine2c80182005-01-15 22:15:51 +00002789 if (tok_type_(tline, TOK_ID) &&
2790 nasm_stricmp(tline->text, ".nolist") == 0) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002791 nolist = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002792 do {
2793 tline = tline->next;
2794 } while (tok_type_(tline, TOK_WHITESPACE));
2795 }
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00002796
H. Peter Anvine2c80182005-01-15 22:15:51 +00002797 if (tline) {
2798 t = expand_smacro(tline);
2799 tptr = &t;
2800 tokval.t_type = TOKEN_INVALID;
2801 evalresult =
2802 evaluate(ppscan, tptr, &tokval, NULL, pass, error, NULL);
2803 if (!evalresult) {
2804 free_tlist(origline);
2805 return DIRECTIVE_FOUND;
2806 }
2807 if (tokval.t_type)
H. Peter Anvin917a3492008-09-24 09:14:49 -07002808 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +00002809 "trailing garbage after expression ignored");
2810 if (!is_simple(evalresult)) {
2811 error(ERR_NONFATAL, "non-constant value given to `%%rep'");
2812 return DIRECTIVE_FOUND;
2813 }
H. Peter Anvinf8ba53e2007-10-11 10:11:57 -07002814 count = reloc_value(evalresult) + 1;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002815 } else {
2816 error(ERR_NONFATAL, "`%%rep' expects a repeat count");
H. Peter Anvinf8ba53e2007-10-11 10:11:57 -07002817 count = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002818 }
2819 free_tlist(origline);
H. Peter Anvin734b1882002-04-30 21:01:08 +00002820
H. Peter Anvine2c80182005-01-15 22:15:51 +00002821 tmp_defining = defining;
2822 defining = nasm_malloc(sizeof(MMacro));
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002823 defining->prev = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002824 defining->name = NULL; /* flags this macro as a %rep block */
H. Peter Anvin70055962007-10-11 00:05:31 -07002825 defining->casesense = false;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002826 defining->plus = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002827 defining->nolist = nolist;
H. Peter Anvinf8ba53e2007-10-11 10:11:57 -07002828 defining->in_progress = count;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002829 defining->max_depth = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002830 defining->nparam_min = defining->nparam_max = 0;
2831 defining->defaults = NULL;
2832 defining->dlist = NULL;
2833 defining->expansion = NULL;
2834 defining->next_active = istk->mstk;
2835 defining->rep_nest = tmp_defining;
2836 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002837
H. Peter Anvine2c80182005-01-15 22:15:51 +00002838 case PP_ENDREP:
2839 if (!defining || defining->name) {
2840 error(ERR_NONFATAL, "`%%endrep': no matching `%%rep'");
2841 return DIRECTIVE_FOUND;
2842 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00002843
H. Peter Anvine2c80182005-01-15 22:15:51 +00002844 /*
2845 * Now we have a "macro" defined - although it has no name
2846 * and we won't be entering it in the hash tables - we must
2847 * push a macro-end marker for it on to istk->expansion.
2848 * After that, it will take care of propagating itself (a
2849 * macro-end marker line for a macro which is really a %rep
2850 * block will cause the macro to be re-expanded, complete
2851 * with another macro-end marker to ensure the process
2852 * continues) until the whole expansion is forcibly removed
2853 * from istk->expansion by a %exitrep.
2854 */
2855 l = nasm_malloc(sizeof(Line));
2856 l->next = istk->expansion;
2857 l->finishes = defining;
2858 l->first = NULL;
2859 istk->expansion = l;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002860
H. Peter Anvine2c80182005-01-15 22:15:51 +00002861 istk->mstk = defining;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002862
H. Peter Anvine2c80182005-01-15 22:15:51 +00002863 list->uplevel(defining->nolist ? LIST_MACRO_NOLIST : LIST_MACRO);
2864 tmp_defining = defining;
2865 defining = defining->rep_nest;
2866 free_tlist(origline);
2867 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002868
H. Peter Anvine2c80182005-01-15 22:15:51 +00002869 case PP_EXITREP:
2870 /*
2871 * We must search along istk->expansion until we hit a
2872 * macro-end marker for a macro with no name. Then we set
2873 * its `in_progress' flag to 0.
2874 */
2875 for (l = istk->expansion; l; l = l->next)
2876 if (l->finishes && !l->finishes->name)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002877 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002878
H. Peter Anvine2c80182005-01-15 22:15:51 +00002879 if (l)
Charles Crayned4200be2008-07-12 16:42:33 -07002880 l->finishes->in_progress = 1;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002881 else
2882 error(ERR_NONFATAL, "`%%exitrep' not within `%%rep' block");
2883 free_tlist(origline);
2884 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002885
H. Peter Anvine2c80182005-01-15 22:15:51 +00002886 case PP_XDEFINE:
2887 case PP_IXDEFINE:
2888 case PP_DEFINE:
2889 case PP_IDEFINE:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002890 casesense = (i == PP_DEFINE || i == PP_XDEFINE);
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07002891
H. Peter Anvine2c80182005-01-15 22:15:51 +00002892 tline = tline->next;
2893 skip_white_(tline);
2894 tline = expand_id(tline);
2895 if (!tline || (tline->type != TOK_ID &&
2896 (tline->type != TOK_PREPROC_ID ||
2897 tline->text[1] != '$'))) {
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07002898 error(ERR_NONFATAL, "`%s' expects a macro identifier",
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002899 pp_directives[i]);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002900 free_tlist(origline);
2901 return DIRECTIVE_FOUND;
2902 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00002903
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08002904 ctx = get_ctx(tline->text, &mname, false);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002905 last = tline;
2906 param_start = tline = tline->next;
2907 nparam = 0;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002908
H. Peter Anvine2c80182005-01-15 22:15:51 +00002909 /* Expand the macro definition now for %xdefine and %ixdefine */
2910 if ((i == PP_XDEFINE) || (i == PP_IXDEFINE))
2911 tline = expand_smacro(tline);
H. Peter Anvin734b1882002-04-30 21:01:08 +00002912
H. Peter Anvine2c80182005-01-15 22:15:51 +00002913 if (tok_is_(tline, "(")) {
2914 /*
2915 * This macro has parameters.
2916 */
H. Peter Anvin734b1882002-04-30 21:01:08 +00002917
H. Peter Anvine2c80182005-01-15 22:15:51 +00002918 tline = tline->next;
2919 while (1) {
2920 skip_white_(tline);
2921 if (!tline) {
2922 error(ERR_NONFATAL, "parameter identifier expected");
2923 free_tlist(origline);
2924 return DIRECTIVE_FOUND;
2925 }
2926 if (tline->type != TOK_ID) {
2927 error(ERR_NONFATAL,
2928 "`%s': parameter identifier expected",
2929 tline->text);
2930 free_tlist(origline);
2931 return DIRECTIVE_FOUND;
2932 }
2933 tline->type = TOK_SMAC_PARAM + nparam++;
2934 tline = tline->next;
2935 skip_white_(tline);
2936 if (tok_is_(tline, ",")) {
2937 tline = tline->next;
H. Peter Anvinca348b62008-07-23 10:49:26 -04002938 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002939 if (!tok_is_(tline, ")")) {
2940 error(ERR_NONFATAL,
2941 "`)' expected to terminate macro template");
2942 free_tlist(origline);
2943 return DIRECTIVE_FOUND;
2944 }
2945 break;
2946 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002947 }
2948 last = tline;
2949 tline = tline->next;
2950 }
2951 if (tok_type_(tline, TOK_WHITESPACE))
2952 last = tline, tline = tline->next;
2953 macro_start = NULL;
2954 last->next = NULL;
2955 t = tline;
2956 while (t) {
2957 if (t->type == TOK_ID) {
2958 for (tt = param_start; tt; tt = tt->next)
2959 if (tt->type >= TOK_SMAC_PARAM &&
2960 !strcmp(tt->text, t->text))
2961 t->type = tt->type;
2962 }
2963 tt = t->next;
2964 t->next = macro_start;
2965 macro_start = t;
2966 t = tt;
2967 }
2968 /*
2969 * Good. We now have a macro name, a parameter count, and a
2970 * token list (in reverse order) for an expansion. We ought
2971 * to be OK just to create an SMacro, store it, and let
2972 * free_tlist have the rest of the line (which we have
2973 * carefully re-terminated after chopping off the expansion
2974 * from the end).
2975 */
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002976 define_smacro(ctx, mname, casesense, nparam, macro_start);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002977 free_tlist(origline);
2978 return DIRECTIVE_FOUND;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002979
H. Peter Anvine2c80182005-01-15 22:15:51 +00002980 case PP_UNDEF:
2981 tline = tline->next;
2982 skip_white_(tline);
2983 tline = expand_id(tline);
2984 if (!tline || (tline->type != TOK_ID &&
2985 (tline->type != TOK_PREPROC_ID ||
2986 tline->text[1] != '$'))) {
2987 error(ERR_NONFATAL, "`%%undef' expects a macro identifier");
2988 free_tlist(origline);
2989 return DIRECTIVE_FOUND;
2990 }
2991 if (tline->next) {
H. Peter Anvin917a3492008-09-24 09:14:49 -07002992 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +00002993 "trailing garbage after macro name ignored");
2994 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002995
H. Peter Anvine2c80182005-01-15 22:15:51 +00002996 /* Find the context that symbol belongs to */
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08002997 ctx = get_ctx(tline->text, &mname, false);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002998 undef_smacro(ctx, mname);
2999 free_tlist(origline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003000 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003001
H. Peter Anvin9e200162008-06-04 17:23:14 -07003002 case PP_DEFSTR:
3003 case PP_IDEFSTR:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003004 casesense = (i == PP_DEFSTR);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003005
3006 tline = tline->next;
3007 skip_white_(tline);
3008 tline = expand_id(tline);
3009 if (!tline || (tline->type != TOK_ID &&
3010 (tline->type != TOK_PREPROC_ID ||
3011 tline->text[1] != '$'))) {
3012 error(ERR_NONFATAL, "`%s' expects a macro identifier",
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003013 pp_directives[i]);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003014 free_tlist(origline);
3015 return DIRECTIVE_FOUND;
3016 }
3017
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08003018 ctx = get_ctx(tline->text, &mname, false);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003019 last = tline;
3020 tline = expand_smacro(tline->next);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003021 last->next = NULL;
H. Peter Anvin9e200162008-06-04 17:23:14 -07003022
3023 while (tok_type_(tline, TOK_WHITESPACE))
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003024 tline = delete_Token(tline);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003025
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003026 p = detoken(tline, false);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003027 macro_start = nasm_malloc(sizeof(*macro_start));
3028 macro_start->next = NULL;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003029 macro_start->text = nasm_quote(p, strlen(p));
3030 macro_start->type = TOK_STRING;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003031 macro_start->a.mac = NULL;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003032 nasm_free(p);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003033
3034 /*
3035 * We now have a macro name, an implicit parameter count of
3036 * zero, and a string token to use as an expansion. Create
3037 * and store an SMacro.
3038 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003039 define_smacro(ctx, mname, casesense, 0, macro_start);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003040 free_tlist(origline);
3041 return DIRECTIVE_FOUND;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003042
H. Peter Anvin2f55bda2009-07-14 15:04:04 -04003043 case PP_DEFTOK:
3044 case PP_IDEFTOK:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003045 casesense = (i == PP_DEFTOK);
3046
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003047 tline = tline->next;
3048 skip_white_(tline);
3049 tline = expand_id(tline);
3050 if (!tline || (tline->type != TOK_ID &&
3051 (tline->type != TOK_PREPROC_ID ||
3052 tline->text[1] != '$'))) {
3053 error(ERR_NONFATAL,
3054 "`%s' expects a macro identifier as first parameter",
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003055 pp_directives[i]);
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003056 free_tlist(origline);
3057 return DIRECTIVE_FOUND;
3058 }
3059 ctx = get_ctx(tline->text, &mname, false);
3060 last = tline;
3061 tline = expand_smacro(tline->next);
3062 last->next = NULL;
3063
3064 t = tline;
3065 while (tok_type_(t, TOK_WHITESPACE))
3066 t = t->next;
3067 /* t should now point to the string */
3068 if (t->type != TOK_STRING) {
3069 error(ERR_NONFATAL,
3070 "`%s` requires string as second parameter",
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003071 pp_directives[i]);
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003072 free_tlist(tline);
3073 free_tlist(origline);
3074 return DIRECTIVE_FOUND;
3075 }
3076
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003077 nasm_unquote_cstr(t->text, i);
3078 macro_start = tokenize(t->text);
3079
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003080 /*
3081 * We now have a macro name, an implicit parameter count of
3082 * zero, and a numeric token to use as an expansion. Create
3083 * and store an SMacro.
3084 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003085 define_smacro(ctx, mname, casesense, 0, macro_start);
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003086 free_tlist(tline);
3087 free_tlist(origline);
3088 return DIRECTIVE_FOUND;
H. Peter Anvin9e200162008-06-04 17:23:14 -07003089
H. Peter Anvin418ca702008-05-30 10:42:30 -07003090 case PP_PATHSEARCH:
3091 {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003092 FILE *fp;
3093 StrList *xsl = NULL;
3094 StrList **xst = &xsl;
H. Peter Anvin418ca702008-05-30 10:42:30 -07003095
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003096 casesense = true;
H. Peter Anvin418ca702008-05-30 10:42:30 -07003097
3098 tline = tline->next;
3099 skip_white_(tline);
3100 tline = expand_id(tline);
3101 if (!tline || (tline->type != TOK_ID &&
3102 (tline->type != TOK_PREPROC_ID ||
3103 tline->text[1] != '$'))) {
3104 error(ERR_NONFATAL,
3105 "`%%pathsearch' expects a macro identifier as first parameter");
3106 free_tlist(origline);
3107 return DIRECTIVE_FOUND;
3108 }
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08003109 ctx = get_ctx(tline->text, &mname, false);
H. Peter Anvin418ca702008-05-30 10:42:30 -07003110 last = tline;
3111 tline = expand_smacro(tline->next);
3112 last->next = NULL;
3113
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003114 t = tline;
H. Peter Anvin418ca702008-05-30 10:42:30 -07003115 while (tok_type_(t, TOK_WHITESPACE))
3116 t = t->next;
3117
3118 if (!t || (t->type != TOK_STRING &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003119 t->type != TOK_INTERNAL_STRING)) {
H. Peter Anvin418ca702008-05-30 10:42:30 -07003120 error(ERR_NONFATAL, "`%%pathsearch' expects a file name");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003121 free_tlist(tline);
H. Peter Anvin418ca702008-05-30 10:42:30 -07003122 free_tlist(origline);
3123 return DIRECTIVE_FOUND; /* but we did _something_ */
3124 }
3125 if (t->next)
H. Peter Anvin917a3492008-09-24 09:14:49 -07003126 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvin418ca702008-05-30 10:42:30 -07003127 "trailing garbage after `%%pathsearch' ignored");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003128 p = t->text;
H. Peter Anvin427cc912008-06-01 21:43:03 -07003129 if (t->type != TOK_INTERNAL_STRING)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003130 nasm_unquote(p, NULL);
H. Peter Anvin418ca702008-05-30 10:42:30 -07003131
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003132 fp = inc_fopen(p, &xsl, &xst, true);
3133 if (fp) {
3134 p = xsl->str;
3135 fclose(fp); /* Don't actually care about the file */
3136 }
H. Peter Anvin418ca702008-05-30 10:42:30 -07003137 macro_start = nasm_malloc(sizeof(*macro_start));
3138 macro_start->next = NULL;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003139 macro_start->text = nasm_quote(p, strlen(p));
3140 macro_start->type = TOK_STRING;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003141 macro_start->a.mac = NULL;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003142 if (xsl)
3143 nasm_free(xsl);
H. Peter Anvin418ca702008-05-30 10:42:30 -07003144
3145 /*
3146 * We now have a macro name, an implicit parameter count of
3147 * zero, and a string token to use as an expansion. Create
3148 * and store an SMacro.
3149 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003150 define_smacro(ctx, mname, casesense, 0, macro_start);
3151 free_tlist(tline);
H. Peter Anvin418ca702008-05-30 10:42:30 -07003152 free_tlist(origline);
3153 return DIRECTIVE_FOUND;
3154 }
3155
H. Peter Anvine2c80182005-01-15 22:15:51 +00003156 case PP_STRLEN:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003157 casesense = true;
H. Peter Anvin70653092007-10-19 14:42:29 -07003158
H. Peter Anvine2c80182005-01-15 22:15:51 +00003159 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 "`%%strlen' expects a macro identifier as first parameter");
3167 free_tlist(origline);
3168 return DIRECTIVE_FOUND;
3169 }
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08003170 ctx = get_ctx(tline->text, &mname, false);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003171 last = tline;
3172 tline = expand_smacro(tline->next);
3173 last->next = NULL;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003174
H. Peter Anvine2c80182005-01-15 22:15:51 +00003175 t = tline;
3176 while (tok_type_(t, TOK_WHITESPACE))
3177 t = t->next;
3178 /* t should now point to the string */
3179 if (t->type != TOK_STRING) {
3180 error(ERR_NONFATAL,
3181 "`%%strlen` requires string as second parameter");
3182 free_tlist(tline);
3183 free_tlist(origline);
3184 return DIRECTIVE_FOUND;
3185 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003186
H. Peter Anvine2c80182005-01-15 22:15:51 +00003187 macro_start = nasm_malloc(sizeof(*macro_start));
3188 macro_start->next = NULL;
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07003189 make_tok_num(macro_start, nasm_unquote(t->text, NULL));
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003190 macro_start->a.mac = NULL;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003191
H. Peter Anvine2c80182005-01-15 22:15:51 +00003192 /*
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);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003198 free_tlist(tline);
3199 free_tlist(origline);
3200 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003201
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003202 case PP_STRCAT:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003203 casesense = true;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003204
3205 tline = tline->next;
3206 skip_white_(tline);
3207 tline = expand_id(tline);
3208 if (!tline || (tline->type != TOK_ID &&
3209 (tline->type != TOK_PREPROC_ID ||
3210 tline->text[1] != '$'))) {
3211 error(ERR_NONFATAL,
3212 "`%%strcat' expects a macro identifier as first parameter");
3213 free_tlist(origline);
3214 return DIRECTIVE_FOUND;
3215 }
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08003216 ctx = get_ctx(tline->text, &mname, false);
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003217 last = tline;
3218 tline = expand_smacro(tline->next);
3219 last->next = NULL;
3220
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003221 len = 0;
3222 for (t = tline; t; t = t->next) {
3223 switch (t->type) {
3224 case TOK_WHITESPACE:
3225 break;
3226 case TOK_STRING:
3227 len += t->a.len = nasm_unquote(t->text, NULL);
3228 break;
3229 case TOK_OTHER:
3230 if (!strcmp(t->text, ",")) /* permit comma separators */
3231 break;
3232 /* else fall through */
3233 default:
3234 error(ERR_NONFATAL,
3235 "non-string passed to `%%strcat' (%d)", t->type);
3236 free_tlist(tline);
3237 free_tlist(origline);
3238 return DIRECTIVE_FOUND;
3239 }
3240 }
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003241
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003242 p = pp = nasm_malloc(len);
3243 for (t = tline; t; t = t->next) {
3244 if (t->type == TOK_STRING) {
3245 memcpy(p, t->text, t->a.len);
3246 p += t->a.len;
3247 }
3248 }
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003249
3250 /*
3251 * We now have a macro name, an implicit parameter count of
3252 * zero, and a numeric token to use as an expansion. Create
3253 * and store an SMacro.
3254 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003255 macro_start = new_Token(NULL, TOK_STRING, NULL, 0);
3256 macro_start->text = nasm_quote(pp, len);
3257 nasm_free(pp);
3258 define_smacro(ctx, mname, casesense, 0, macro_start);
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003259 free_tlist(tline);
3260 free_tlist(origline);
3261 return DIRECTIVE_FOUND;
3262
H. Peter Anvine2c80182005-01-15 22:15:51 +00003263 case PP_SUBSTR:
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003264 {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003265 int64_t a1, a2;
3266 size_t len;
H. Peter Anvind2456592008-06-19 15:04:18 -07003267
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003268 casesense = true;
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07003269
H. Peter Anvine2c80182005-01-15 22:15:51 +00003270 tline = tline->next;
3271 skip_white_(tline);
3272 tline = expand_id(tline);
3273 if (!tline || (tline->type != TOK_ID &&
3274 (tline->type != TOK_PREPROC_ID ||
3275 tline->text[1] != '$'))) {
3276 error(ERR_NONFATAL,
3277 "`%%substr' expects a macro identifier as first parameter");
3278 free_tlist(origline);
3279 return DIRECTIVE_FOUND;
3280 }
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08003281 ctx = get_ctx(tline->text, &mname, false);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003282 last = tline;
3283 tline = expand_smacro(tline->next);
3284 last->next = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003285
H. Peter Anvine2c80182005-01-15 22:15:51 +00003286 t = tline->next;
3287 while (tok_type_(t, TOK_WHITESPACE))
3288 t = t->next;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003289
H. Peter Anvine2c80182005-01-15 22:15:51 +00003290 /* t should now point to the string */
3291 if (t->type != TOK_STRING) {
3292 error(ERR_NONFATAL,
3293 "`%%substr` requires string as second parameter");
3294 free_tlist(tline);
3295 free_tlist(origline);
3296 return DIRECTIVE_FOUND;
3297 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003298
H. Peter Anvine2c80182005-01-15 22:15:51 +00003299 tt = t->next;
3300 tptr = &tt;
3301 tokval.t_type = TOKEN_INVALID;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003302 evalresult = evaluate(ppscan, tptr, &tokval, NULL,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003303 pass, error, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003304 if (!evalresult) {
3305 free_tlist(tline);
3306 free_tlist(origline);
3307 return DIRECTIVE_FOUND;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003308 } else if (!is_simple(evalresult)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00003309 error(ERR_NONFATAL, "non-constant value given to `%%substr`");
3310 free_tlist(tline);
3311 free_tlist(origline);
3312 return DIRECTIVE_FOUND;
3313 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003314 a1 = evalresult->value-1;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003315
3316 while (tok_type_(tt, TOK_WHITESPACE))
3317 tt = tt->next;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003318 if (!tt) {
3319 a2 = 1; /* Backwards compatibility: one character */
3320 } else {
3321 tokval.t_type = TOKEN_INVALID;
3322 evalresult = evaluate(ppscan, tptr, &tokval, NULL,
3323 pass, error, NULL);
3324 if (!evalresult) {
3325 free_tlist(tline);
3326 free_tlist(origline);
3327 return DIRECTIVE_FOUND;
3328 } else if (!is_simple(evalresult)) {
3329 error(ERR_NONFATAL, "non-constant value given to `%%substr`");
3330 free_tlist(tline);
3331 free_tlist(origline);
3332 return DIRECTIVE_FOUND;
3333 }
3334 a2 = evalresult->value;
3335 }
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003336
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003337 len = nasm_unquote(t->text, NULL);
3338 if (a2 < 0)
3339 a2 = a2+1+len-a1;
3340 if (a1+a2 > (int64_t)len)
3341 a2 = len-a1;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003342
H. Peter Anvine2c80182005-01-15 22:15:51 +00003343 macro_start = nasm_malloc(sizeof(*macro_start));
3344 macro_start->next = NULL;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003345 macro_start->text = nasm_quote((a1 < 0) ? "" : t->text+a1, a2);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003346 macro_start->type = TOK_STRING;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003347 macro_start->a.mac = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003348
H. Peter Anvine2c80182005-01-15 22:15:51 +00003349 /*
3350 * We now have a macro name, an implicit parameter count of
3351 * zero, and a numeric token to use as an expansion. Create
3352 * and store an SMacro.
3353 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003354 define_smacro(ctx, mname, casesense, 0, macro_start);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003355 free_tlist(tline);
3356 free_tlist(origline);
3357 return DIRECTIVE_FOUND;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003358 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003359
H. Peter Anvine2c80182005-01-15 22:15:51 +00003360 case PP_ASSIGN:
3361 case PP_IASSIGN:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003362 casesense = (i == PP_ASSIGN);
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07003363
H. Peter Anvine2c80182005-01-15 22:15:51 +00003364 tline = tline->next;
3365 skip_white_(tline);
3366 tline = expand_id(tline);
3367 if (!tline || (tline->type != TOK_ID &&
3368 (tline->type != TOK_PREPROC_ID ||
3369 tline->text[1] != '$'))) {
3370 error(ERR_NONFATAL,
3371 "`%%%sassign' expects a macro identifier",
3372 (i == PP_IASSIGN ? "i" : ""));
3373 free_tlist(origline);
3374 return DIRECTIVE_FOUND;
3375 }
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08003376 ctx = get_ctx(tline->text, &mname, false);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003377 last = tline;
3378 tline = expand_smacro(tline->next);
3379 last->next = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003380
H. Peter Anvine2c80182005-01-15 22:15:51 +00003381 t = tline;
3382 tptr = &t;
3383 tokval.t_type = TOKEN_INVALID;
3384 evalresult =
3385 evaluate(ppscan, tptr, &tokval, NULL, pass, error, NULL);
3386 free_tlist(tline);
3387 if (!evalresult) {
3388 free_tlist(origline);
3389 return DIRECTIVE_FOUND;
3390 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003391
H. Peter Anvine2c80182005-01-15 22:15:51 +00003392 if (tokval.t_type)
H. Peter Anvin917a3492008-09-24 09:14:49 -07003393 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +00003394 "trailing garbage after expression ignored");
H. Peter Anvin734b1882002-04-30 21:01:08 +00003395
H. Peter Anvine2c80182005-01-15 22:15:51 +00003396 if (!is_simple(evalresult)) {
3397 error(ERR_NONFATAL,
3398 "non-constant value given to `%%%sassign'",
3399 (i == PP_IASSIGN ? "i" : ""));
3400 free_tlist(origline);
3401 return DIRECTIVE_FOUND;
3402 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003403
H. Peter Anvine2c80182005-01-15 22:15:51 +00003404 macro_start = nasm_malloc(sizeof(*macro_start));
3405 macro_start->next = NULL;
3406 make_tok_num(macro_start, reloc_value(evalresult));
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003407 macro_start->a.mac = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003408
H. Peter Anvine2c80182005-01-15 22:15:51 +00003409 /*
3410 * We now have a macro name, an implicit parameter count of
3411 * zero, and a numeric token to use as an expansion. Create
3412 * and store an SMacro.
3413 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003414 define_smacro(ctx, mname, casesense, 0, macro_start);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003415 free_tlist(origline);
3416 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003417
H. Peter Anvine2c80182005-01-15 22:15:51 +00003418 case PP_LINE:
3419 /*
3420 * Syntax is `%line nnn[+mmm] [filename]'
3421 */
3422 tline = tline->next;
3423 skip_white_(tline);
3424 if (!tok_type_(tline, TOK_NUMBER)) {
3425 error(ERR_NONFATAL, "`%%line' expects line number");
3426 free_tlist(origline);
3427 return DIRECTIVE_FOUND;
3428 }
H. Peter Anvin70055962007-10-11 00:05:31 -07003429 k = readnum(tline->text, &err);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003430 m = 1;
3431 tline = tline->next;
3432 if (tok_is_(tline, "+")) {
3433 tline = tline->next;
3434 if (!tok_type_(tline, TOK_NUMBER)) {
3435 error(ERR_NONFATAL, "`%%line' expects line increment");
3436 free_tlist(origline);
3437 return DIRECTIVE_FOUND;
3438 }
H. Peter Anvin70055962007-10-11 00:05:31 -07003439 m = readnum(tline->text, &err);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003440 tline = tline->next;
3441 }
3442 skip_white_(tline);
3443 src_set_linnum(k);
3444 istk->lineinc = m;
3445 if (tline) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07003446 nasm_free(src_set_fname(detoken(tline, false)));
H. Peter Anvine2c80182005-01-15 22:15:51 +00003447 }
3448 free_tlist(origline);
3449 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003450
H. Peter Anvine2c80182005-01-15 22:15:51 +00003451 default:
3452 error(ERR_FATAL,
3453 "preprocessor directive `%s' not yet implemented",
H. Peter Anvin4169a472007-09-12 01:29:43 +00003454 pp_directives[i]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003455 return DIRECTIVE_FOUND;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003456 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003457}
3458
3459/*
H. Peter Anvin76690a12002-04-30 20:52:49 +00003460 * Ensure that a macro parameter contains a condition code and
3461 * nothing else. Return the condition code index if so, or -1
3462 * otherwise.
3463 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003464static int find_cc(Token * t)
H. Peter Anvineba20a72002-04-30 20:53:55 +00003465{
H. Peter Anvin76690a12002-04-30 20:52:49 +00003466 Token *tt;
3467 int i, j, k, m;
3468
H. Peter Anvin25a99342007-09-22 17:45:45 -07003469 if (!t)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003470 return -1; /* Probably a %+ without a space */
H. Peter Anvin25a99342007-09-22 17:45:45 -07003471
H. Peter Anvineba20a72002-04-30 20:53:55 +00003472 skip_white_(t);
H. Peter Anvin76690a12002-04-30 20:52:49 +00003473 if (t->type != TOK_ID)
H. Peter Anvine2c80182005-01-15 22:15:51 +00003474 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003475 tt = t->next;
H. Peter Anvineba20a72002-04-30 20:53:55 +00003476 skip_white_(tt);
H. Peter Anvin76690a12002-04-30 20:52:49 +00003477 if (tt && (tt->type != TOK_OTHER || strcmp(tt->text, ",")))
H. Peter Anvine2c80182005-01-15 22:15:51 +00003478 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003479
3480 i = -1;
Ed Beroset3ab3f412002-06-11 03:31:49 +00003481 j = elements(conditions);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003482 while (j - i > 1) {
3483 k = (j + i) / 2;
3484 m = nasm_stricmp(t->text, conditions[k]);
3485 if (m == 0) {
3486 i = k;
3487 j = -2;
3488 break;
3489 } else if (m < 0) {
3490 j = k;
3491 } else
3492 i = k;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003493 }
3494 if (j != -2)
H. Peter Anvine2c80182005-01-15 22:15:51 +00003495 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003496 return i;
3497}
3498
H. Peter Anvind784a082009-04-20 14:01:18 -07003499static bool paste_tokens(Token **head, bool handle_paste_tokens)
3500{
3501 Token **tail, *t, *tt;
3502 Token **paste_head;
3503 bool did_paste = false;
3504 char *tmp;
3505
3506 /* Now handle token pasting... */
3507 paste_head = NULL;
3508 tail = head;
3509 while ((t = *tail) && (tt = t->next)) {
3510 switch (t->type) {
3511 case TOK_WHITESPACE:
3512 if (tt->type == TOK_WHITESPACE) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003513 /* Zap adjacent whitespace tokens */
H. Peter Anvind784a082009-04-20 14:01:18 -07003514 t->next = delete_Token(tt);
3515 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003516 /* Do not advance paste_head here */
3517 tail = &t->next;
3518 }
H. Peter Anvind784a082009-04-20 14:01:18 -07003519 break;
3520 case TOK_ID:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003521 case TOK_PREPROC_ID:
H. Peter Anvind784a082009-04-20 14:01:18 -07003522 case TOK_NUMBER:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003523 case TOK_FLOAT:
3524 {
3525 size_t len = 0;
3526 char *tmp, *p;
H. Peter Anvind784a082009-04-20 14:01:18 -07003527
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003528 while (tt && (tt->type == TOK_ID || tt->type == TOK_PREPROC_ID ||
3529 tt->type == TOK_NUMBER || tt->type == TOK_FLOAT ||
3530 tt->type == TOK_OTHER)) {
3531 len += strlen(tt->text);
3532 tt = tt->next;
3533 }
H. Peter Anvind784a082009-04-20 14:01:18 -07003534
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003535 /*
3536 * Now tt points to the first token after
3537 * the potential paste area...
3538 */
3539 if (tt != t->next) {
3540 /* We have at least two tokens... */
3541 len += strlen(t->text);
3542 p = tmp = nasm_malloc(len+1);
H. Peter Anvind784a082009-04-20 14:01:18 -07003543
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003544 while (t != tt) {
3545 strcpy(p, t->text);
3546 p = strchr(p, '\0');
3547 t = delete_Token(t);
3548 }
H. Peter Anvind784a082009-04-20 14:01:18 -07003549
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003550 t = *tail = tokenize(tmp);
3551 nasm_free(tmp);
H. Peter Anvind784a082009-04-20 14:01:18 -07003552
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003553 while (t->next) {
3554 tail = &t->next;
3555 t = t->next;
3556 }
3557 t->next = tt; /* Attach the remaining token chain */
H. Peter Anvind784a082009-04-20 14:01:18 -07003558
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003559 did_paste = true;
3560 }
3561 paste_head = tail;
3562 tail = &t->next;
3563 break;
3564 }
3565 case TOK_PASTE: /* %+ */
3566 if (handle_paste_tokens) {
3567 /* Zap %+ and whitespace tokens to the right */
3568 while (t && (t->type == TOK_WHITESPACE ||
3569 t->type == TOK_PASTE))
3570 t = *tail = delete_Token(t);
3571 if (!paste_head || !t)
3572 break; /* Nothing to paste with */
3573 tail = paste_head;
3574 t = *tail;
3575 tt = t->next;
3576 while (tok_type_(tt, TOK_WHITESPACE))
3577 tt = t->next = delete_Token(tt);
H. Peter Anvind784a082009-04-20 14:01:18 -07003578
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003579 if (tt) {
3580 tmp = nasm_strcat(t->text, tt->text);
3581 delete_Token(t);
3582 tt = delete_Token(tt);
3583 t = *tail = tokenize(tmp);
3584 nasm_free(tmp);
3585 while (t->next) {
3586 tail = &t->next;
3587 t = t->next;
3588 }
3589 t->next = tt; /* Attach the remaining token chain */
3590 did_paste = true;
3591 }
3592 paste_head = tail;
3593 tail = &t->next;
3594 break;
3595 }
3596 /* else fall through */
3597 default:
3598 tail = paste_head = &t->next;
3599 break;
H. Peter Anvind784a082009-04-20 14:01:18 -07003600 }
3601 }
3602 return did_paste;
3603}
H. Peter Anvin76690a12002-04-30 20:52:49 +00003604/*
3605 * Expand MMacro-local things: parameter references (%0, %n, %+n,
H. Peter Anvin67c63722008-10-26 23:49:00 -07003606 * %-n) and MMacro-local identifiers (%%foo) as well as
3607 * macro indirection (%[...]).
H. Peter Anvin76690a12002-04-30 20:52:49 +00003608 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003609static Token *expand_mmac_params(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00003610{
H. Peter Anvin734b1882002-04-30 21:01:08 +00003611 Token *t, *tt, **tail, *thead;
H. Peter Anvin6125b622009-04-08 14:02:25 -07003612 bool changed = false;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003613
3614 tail = &thead;
3615 thead = NULL;
3616
H. Peter Anvine2c80182005-01-15 22:15:51 +00003617 while (tline) {
3618 if (tline->type == TOK_PREPROC_ID &&
3619 (((tline->text[1] == '+' || tline->text[1] == '-')
3620 && tline->text[2]) || tline->text[1] == '%'
3621 || (tline->text[1] >= '0' && tline->text[1] <= '9'))) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00003622 char *text = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003623 int type = 0, cc; /* type = 0 to placate optimisers */
Keith Kaniosa6dfa782007-04-13 16:47:53 +00003624 char tmpbuf[30];
H. Peter Anvin25a99342007-09-22 17:45:45 -07003625 unsigned int n;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003626 int i;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003627 MMacro *mac;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003628
H. Peter Anvine2c80182005-01-15 22:15:51 +00003629 t = tline;
3630 tline = tline->next;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003631
H. Peter Anvine2c80182005-01-15 22:15:51 +00003632 mac = istk->mstk;
3633 while (mac && !mac->name) /* avoid mistaking %reps for macros */
3634 mac = mac->next_active;
3635 if (!mac)
3636 error(ERR_NONFATAL, "`%s': not in a macro call", t->text);
3637 else
3638 switch (t->text[1]) {
3639 /*
3640 * We have to make a substitution of one of the
3641 * forms %1, %-1, %+1, %%foo, %0.
3642 */
3643 case '0':
3644 type = TOK_NUMBER;
3645 snprintf(tmpbuf, sizeof(tmpbuf), "%d", mac->nparam);
3646 text = nasm_strdup(tmpbuf);
3647 break;
3648 case '%':
3649 type = TOK_ID;
H. Peter Anvinf8ba53e2007-10-11 10:11:57 -07003650 snprintf(tmpbuf, sizeof(tmpbuf), "..@%"PRIu64".",
H. Peter Anvine2c80182005-01-15 22:15:51 +00003651 mac->unique);
3652 text = nasm_strcat(tmpbuf, t->text + 2);
3653 break;
3654 case '-':
3655 n = atoi(t->text + 2) - 1;
3656 if (n >= mac->nparam)
3657 tt = NULL;
3658 else {
3659 if (mac->nparam > 1)
3660 n = (n + mac->rotate) % mac->nparam;
3661 tt = mac->params[n];
3662 }
3663 cc = find_cc(tt);
3664 if (cc == -1) {
3665 error(ERR_NONFATAL,
3666 "macro parameter %d is not a condition code",
3667 n + 1);
3668 text = NULL;
3669 } else {
3670 type = TOK_ID;
3671 if (inverse_ccs[cc] == -1) {
3672 error(ERR_NONFATAL,
3673 "condition code `%s' is not invertible",
3674 conditions[cc]);
3675 text = NULL;
3676 } else
H. Peter Anvin67c63722008-10-26 23:49:00 -07003677 text = nasm_strdup(conditions[inverse_ccs[cc]]);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003678 }
3679 break;
3680 case '+':
3681 n = atoi(t->text + 2) - 1;
3682 if (n >= mac->nparam)
3683 tt = NULL;
3684 else {
3685 if (mac->nparam > 1)
3686 n = (n + mac->rotate) % mac->nparam;
3687 tt = mac->params[n];
3688 }
3689 cc = find_cc(tt);
3690 if (cc == -1) {
3691 error(ERR_NONFATAL,
3692 "macro parameter %d is not a condition code",
3693 n + 1);
3694 text = NULL;
3695 } else {
3696 type = TOK_ID;
3697 text = nasm_strdup(conditions[cc]);
3698 }
3699 break;
3700 default:
3701 n = atoi(t->text + 1) - 1;
3702 if (n >= mac->nparam)
3703 tt = NULL;
3704 else {
3705 if (mac->nparam > 1)
3706 n = (n + mac->rotate) % mac->nparam;
3707 tt = mac->params[n];
3708 }
3709 if (tt) {
3710 for (i = 0; i < mac->paramlen[n]; i++) {
3711 *tail = new_Token(NULL, tt->type, tt->text, 0);
3712 tail = &(*tail)->next;
3713 tt = tt->next;
3714 }
3715 }
3716 text = NULL; /* we've done it here */
3717 break;
3718 }
3719 if (!text) {
3720 delete_Token(t);
3721 } else {
3722 *tail = t;
3723 tail = &t->next;
3724 t->type = type;
3725 nasm_free(t->text);
3726 t->text = text;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003727 t->a.mac = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003728 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003729 changed = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003730 continue;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003731 } else if (tline->type == TOK_INDIRECT) {
3732 t = tline;
3733 tline = tline->next;
3734 tt = tokenize(t->text);
3735 tt = expand_mmac_params(tt);
3736 tt = expand_smacro(tt);
3737 *tail = tt;
3738 while (tt) {
3739 tt->a.mac = NULL; /* Necessary? */
3740 tail = &tt->next;
3741 tt = tt->next;
3742 }
3743 delete_Token(t);
3744 changed = true;
Cyrill Gorcunovec88c1b2010-06-02 00:59:21 +04003745 } else if (tline->type == TOK_PREPROC_ID &&
3746 tline->text[0] == '%' && tline->text[1] == '$' &&
3747 (tok_type_(tline->next, TOK_ID) ||
3748 tok_type_(tline->next, TOK_PREPROC_ID) ||
Cyrill Gorcunov51fd86e2010-06-02 11:57:05 +04003749 tok_type_(tline->next, TOK_FLOAT) ||
Cyrill Gorcunovec88c1b2010-06-02 00:59:21 +04003750 tok_type_(tline->next, TOK_NUMBER) ||
Cyrill Gorcunov51fd86e2010-06-02 11:57:05 +04003751 tok_type_(tline->next, TOK_OTHER))) {
Cyrill Gorcunovec88c1b2010-06-02 00:59:21 +04003752 /*
3753 * In a sake of backward compatibility we allow
3754 * to expand local single macro that early before
3755 * pasting token code have place
3756 *
3757 * NOTE: that new code MUST use %+ macro to obtain
3758 * same result
3759 */
3760 t = tline;
3761 tline = tline->next;
3762 tt = tokenize(t->text);
3763 tt = expand_smacro(tt);
3764 *tail = tt;
3765 while (tt) {
3766 tt->a.mac = NULL;
3767 tail = &tt->next;
3768 tt = tt->next;
3769 }
3770 delete_Token(t);
3771 changed = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003772 } else {
3773 t = *tail = tline;
3774 tline = tline->next;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003775 t->a.mac = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003776 tail = &t->next;
3777 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00003778 }
H. Peter Anvineba20a72002-04-30 20:53:55 +00003779 *tail = NULL;
H. Peter Anvin67c63722008-10-26 23:49:00 -07003780
H. Peter Anvind784a082009-04-20 14:01:18 -07003781 if (changed)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003782 paste_tokens(&thead, false);
H. Peter Anvin6125b622009-04-08 14:02:25 -07003783
H. Peter Anvin76690a12002-04-30 20:52:49 +00003784 return thead;
3785}
3786
3787/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003788 * Expand all single-line macro calls made in the given line.
3789 * Return the expanded version of the line. The original is deemed
3790 * to be destroyed in the process. (In reality we'll just move
3791 * Tokens from input to output a lot of the time, rather than
3792 * actually bothering to destroy and replicate.)
3793 */
H. Peter Anvincb1cf592007-11-19 12:26:50 -08003794
H. Peter Anvine2c80182005-01-15 22:15:51 +00003795static Token *expand_smacro(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00003796{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003797 Token *t, *tt, *mstart, **tail, *thead;
H. Peter Anvineba20a72002-04-30 20:53:55 +00003798 SMacro *head = NULL, *m;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003799 Token **params;
3800 int *paramsize;
H. Peter Anvin25a99342007-09-22 17:45:45 -07003801 unsigned int nparam, sparam;
H. Peter Anvind784a082009-04-20 14:01:18 -07003802 int brackets;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003803 Token *org_tline = tline;
3804 Context *ctx;
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08003805 const char *mname;
H. Peter Anvin2a15e692007-11-19 13:14:59 -08003806 int deadman = DEADMAN_LIMIT;
H. Peter Anvin8287daf2009-07-07 16:00:58 -07003807 bool expanded;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003808
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003809 /*
3810 * Trick: we should avoid changing the start token pointer since it can
3811 * be contained in "next" field of other token. Because of this
3812 * we allocate a copy of first token and work with it; at the end of
3813 * routine we copy it back
3814 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003815 if (org_tline) {
Cyrill Gorcunoved4a8052010-04-09 15:40:35 +04003816 tline = new_Token(org_tline->next, org_tline->type,
3817 org_tline->text, 0);
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003818 tline->a.mac = org_tline->a.mac;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003819 nasm_free(org_tline->text);
3820 org_tline->text = NULL;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003821 }
3822
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003823 expanded = true; /* Always expand %+ at least once */
H. Peter Anvin8287daf2009-07-07 16:00:58 -07003824
H. Peter Anvincb1cf592007-11-19 12:26:50 -08003825again:
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003826 thead = NULL;
Cyrill Gorcunoved4a8052010-04-09 15:40:35 +04003827 tail = &thead;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003828
H. Peter Anvine2c80182005-01-15 22:15:51 +00003829 while (tline) { /* main token loop */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003830 if (!--deadman) {
3831 error(ERR_NONFATAL, "interminable macro recursion");
Cyrill Gorcunovbd38c8f2009-11-21 11:11:23 +03003832 goto err;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003833 }
H. Peter Anvincb1cf592007-11-19 12:26:50 -08003834
H. Peter Anvine2c80182005-01-15 22:15:51 +00003835 if ((mname = tline->text)) {
3836 /* if this token is a local macro, look in local context */
Cyrill Gorcunovc56d9ad2010-02-11 15:12:19 +03003837 if (tline->type == TOK_ID) {
3838 head = (SMacro *)hash_findix(&smacros, mname);
3839 } else if (tline->type == TOK_PREPROC_ID) {
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08003840 ctx = get_ctx(mname, &mname, true);
Cyrill Gorcunovc56d9ad2010-02-11 15:12:19 +03003841 head = ctx ? (SMacro *)hash_findix(&ctx->localmac, mname) : NULL;
3842 } else
3843 head = NULL;
H. Peter Anvin072771e2008-05-22 13:17:51 -07003844
H. Peter Anvine2c80182005-01-15 22:15:51 +00003845 /*
3846 * We've hit an identifier. As in is_mmacro below, we first
3847 * check whether the identifier is a single-line macro at
3848 * all, then think about checking for parameters if
3849 * necessary.
3850 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003851 for (m = head; m; m = m->next)
3852 if (!mstrcmp(m->name, mname, m->casesense))
3853 break;
3854 if (m) {
3855 mstart = tline;
3856 params = NULL;
3857 paramsize = NULL;
3858 if (m->nparam == 0) {
3859 /*
3860 * Simple case: the macro is parameterless. Discard the
3861 * one token that the macro call took, and push the
3862 * expansion back on the to-do stack.
3863 */
3864 if (!m->expansion) {
3865 if (!strcmp("__FILE__", m->name)) {
3866 int32_t num = 0;
3867 char *file = NULL;
3868 src_get(&num, &file);
3869 tline->text = nasm_quote(file, strlen(file));
3870 tline->type = TOK_STRING;
3871 nasm_free(file);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003872 continue;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003873 }
3874 if (!strcmp("__LINE__", m->name)) {
3875 nasm_free(tline->text);
3876 make_tok_num(tline, src_get_linnum());
3877 continue;
3878 }
3879 if (!strcmp("__BITS__", m->name)) {
3880 nasm_free(tline->text);
3881 make_tok_num(tline, globalbits);
3882 continue;
3883 }
3884 tline = delete_Token(tline);
3885 continue;
3886 }
3887 } else {
3888 /*
3889 * Complicated case: at least one macro with this name
H. Peter Anvine2c80182005-01-15 22:15:51 +00003890 * exists and takes parameters. We must find the
3891 * parameters in the call, count them, find the SMacro
3892 * that corresponds to that form of the macro call, and
3893 * substitute for the parameters when we expand. What a
3894 * pain.
3895 */
3896 /*tline = tline->next;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003897 skip_white_(tline); */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003898 do {
3899 t = tline->next;
3900 while (tok_type_(t, TOK_SMAC_END)) {
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003901 t->a.mac->in_progress = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003902 t->text = NULL;
3903 t = tline->next = delete_Token(t);
3904 }
3905 tline = t;
3906 } while (tok_type_(tline, TOK_WHITESPACE));
3907 if (!tok_is_(tline, "(")) {
3908 /*
3909 * This macro wasn't called with parameters: ignore
3910 * the call. (Behaviour borrowed from gnu cpp.)
3911 */
3912 tline = mstart;
3913 m = NULL;
3914 } else {
3915 int paren = 0;
3916 int white = 0;
3917 brackets = 0;
3918 nparam = 0;
3919 sparam = PARAM_DELTA;
3920 params = nasm_malloc(sparam * sizeof(Token *));
3921 params[0] = tline->next;
3922 paramsize = nasm_malloc(sparam * sizeof(int));
3923 paramsize[0] = 0;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07003924 while (true) { /* parameter loop */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003925 /*
3926 * For some unusual expansions
3927 * which concatenates function call
3928 */
3929 t = tline->next;
3930 while (tok_type_(t, TOK_SMAC_END)) {
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003931 t->a.mac->in_progress = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003932 t->text = NULL;
3933 t = tline->next = delete_Token(t);
3934 }
3935 tline = t;
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00003936
H. Peter Anvine2c80182005-01-15 22:15:51 +00003937 if (!tline) {
3938 error(ERR_NONFATAL,
3939 "macro call expects terminating `)'");
3940 break;
3941 }
3942 if (tline->type == TOK_WHITESPACE
3943 && brackets <= 0) {
3944 if (paramsize[nparam])
3945 white++;
3946 else
3947 params[nparam] = tline->next;
3948 continue; /* parameter loop */
3949 }
3950 if (tline->type == TOK_OTHER
3951 && tline->text[1] == 0) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00003952 char ch = tline->text[0];
H. Peter Anvine2c80182005-01-15 22:15:51 +00003953 if (ch == ',' && !paren && brackets <= 0) {
3954 if (++nparam >= sparam) {
3955 sparam += PARAM_DELTA;
3956 params = nasm_realloc(params,
Cyrill Gorcunoved4a8052010-04-09 15:40:35 +04003957 sparam * sizeof(Token *));
3958 paramsize = nasm_realloc(paramsize,
3959 sparam * sizeof(int));
H. Peter Anvine2c80182005-01-15 22:15:51 +00003960 }
3961 params[nparam] = tline->next;
3962 paramsize[nparam] = 0;
3963 white = 0;
3964 continue; /* parameter loop */
3965 }
3966 if (ch == '{' &&
3967 (brackets > 0 || (brackets == 0 &&
3968 !paramsize[nparam])))
3969 {
3970 if (!(brackets++)) {
3971 params[nparam] = tline->next;
3972 continue; /* parameter loop */
3973 }
3974 }
3975 if (ch == '}' && brackets > 0)
3976 if (--brackets == 0) {
3977 brackets = -1;
3978 continue; /* parameter loop */
3979 }
3980 if (ch == '(' && !brackets)
3981 paren++;
3982 if (ch == ')' && brackets <= 0)
3983 if (--paren < 0)
3984 break;
3985 }
3986 if (brackets < 0) {
3987 brackets = 0;
3988 error(ERR_NONFATAL, "braces do not "
3989 "enclose all of macro parameter");
3990 }
3991 paramsize[nparam] += white + 1;
3992 white = 0;
3993 } /* parameter loop */
3994 nparam++;
3995 while (m && (m->nparam != nparam ||
3996 mstrcmp(m->name, mname,
3997 m->casesense)))
3998 m = m->next;
3999 if (!m)
H. Peter Anvin917a3492008-09-24 09:14:49 -07004000 error(ERR_WARNING|ERR_PASS1|ERR_WARN_MNP,
H. Peter Anvine2c80182005-01-15 22:15:51 +00004001 "macro `%s' exists, "
4002 "but not taking %d parameters",
4003 mstart->text, nparam);
4004 }
4005 }
4006 if (m && m->in_progress)
4007 m = NULL;
4008 if (!m) { /* in progess or didn't find '(' or wrong nparam */
H. Peter Anvin70653092007-10-19 14:42:29 -07004009 /*
H. Peter Anvine2c80182005-01-15 22:15:51 +00004010 * Design question: should we handle !tline, which
4011 * indicates missing ')' here, or expand those
4012 * macros anyway, which requires the (t) test a few
H. Peter Anvin70653092007-10-19 14:42:29 -07004013 * lines down?
H. Peter Anvine2c80182005-01-15 22:15:51 +00004014 */
4015 nasm_free(params);
4016 nasm_free(paramsize);
4017 tline = mstart;
4018 } else {
4019 /*
4020 * Expand the macro: we are placed on the last token of the
4021 * call, so that we can easily split the call from the
4022 * following tokens. We also start by pushing an SMAC_END
4023 * token for the cycle removal.
4024 */
4025 t = tline;
4026 if (t) {
4027 tline = t->next;
4028 t->next = NULL;
4029 }
4030 tt = new_Token(tline, TOK_SMAC_END, NULL, 0);
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004031 tt->a.mac = m;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07004032 m->in_progress = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004033 tline = tt;
4034 for (t = m->expansion; t; t = t->next) {
4035 if (t->type >= TOK_SMAC_PARAM) {
4036 Token *pcopy = tline, **ptail = &pcopy;
4037 Token *ttt, *pt;
4038 int i;
H. Peter Anvin734b1882002-04-30 21:01:08 +00004039
H. Peter Anvine2c80182005-01-15 22:15:51 +00004040 ttt = params[t->type - TOK_SMAC_PARAM];
Cyrill Gorcunoved4a8052010-04-09 15:40:35 +04004041 i = paramsize[t->type - TOK_SMAC_PARAM];
4042 while (--i >= 0) {
4043 pt = *ptail = new_Token(tline, ttt->type,
4044 ttt->text, 0);
H. Peter Anvine2c80182005-01-15 22:15:51 +00004045 ptail = &pt->next;
4046 ttt = ttt->next;
4047 }
4048 tline = pcopy;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004049 } else if (t->type == TOK_PREPROC_Q) {
4050 tt = new_Token(tline, TOK_ID, mname, 0);
4051 tline = tt;
4052 } else if (t->type == TOK_PREPROC_QQ) {
4053 tt = new_Token(tline, TOK_ID, m->name, 0);
4054 tline = tt;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004055 } else {
4056 tt = new_Token(tline, t->type, t->text, 0);
4057 tline = tt;
4058 }
4059 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00004060
H. Peter Anvine2c80182005-01-15 22:15:51 +00004061 /*
4062 * Having done that, get rid of the macro call, and clean
4063 * up the parameters.
4064 */
4065 nasm_free(params);
4066 nasm_free(paramsize);
4067 free_tlist(mstart);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004068 expanded = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004069 continue; /* main token loop */
4070 }
4071 }
4072 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004073
H. Peter Anvine2c80182005-01-15 22:15:51 +00004074 if (tline->type == TOK_SMAC_END) {
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004075 tline->a.mac->in_progress = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004076 tline = delete_Token(tline);
4077 } else {
4078 t = *tail = tline;
4079 tline = tline->next;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004080 t->a.mac = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004081 t->next = NULL;
4082 tail = &t->next;
4083 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004084 }
4085
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004086 /*
4087 * Now scan the entire line and look for successive TOK_IDs that resulted
Keith Kaniosb7a89542007-04-12 02:40:54 +00004088 * after expansion (they can't be produced by tokenize()). The successive
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004089 * TOK_IDs should be concatenated.
4090 * Also we look for %+ tokens and concatenate the tokens before and after
4091 * them (without white spaces in between).
4092 */
H. Peter Anvin8287daf2009-07-07 16:00:58 -07004093 if (expanded && paste_tokens(&thead, true)) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004094 /*
4095 * If we concatenated something, *and* we had previously expanded
4096 * an actual macro, scan the lines again for macros...
4097 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004098 tline = thead;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004099 expanded = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004100 goto again;
H. Peter Anvin734b1882002-04-30 21:01:08 +00004101 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004102
Cyrill Gorcunovbd38c8f2009-11-21 11:11:23 +03004103err:
H. Peter Anvine2c80182005-01-15 22:15:51 +00004104 if (org_tline) {
4105 if (thead) {
4106 *org_tline = *thead;
4107 /* since we just gave text to org_line, don't free it */
4108 thead->text = NULL;
4109 delete_Token(thead);
4110 } else {
4111 /* the expression expanded to empty line;
4112 we can't return NULL for some reasons
4113 we just set the line to a single WHITESPACE token. */
4114 memset(org_tline, 0, sizeof(*org_tline));
4115 org_tline->text = NULL;
4116 org_tline->type = TOK_WHITESPACE;
4117 }
4118 thead = org_tline;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004119 }
4120
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004121 return thead;
4122}
4123
4124/*
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004125 * Similar to expand_smacro but used exclusively with macro identifiers
4126 * right before they are fetched in. The reason is that there can be
4127 * identifiers consisting of several subparts. We consider that if there
4128 * are more than one element forming the name, user wants a expansion,
4129 * otherwise it will be left as-is. Example:
4130 *
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004131 * %define %$abc cde
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004132 *
4133 * the identifier %$abc will be left as-is so that the handler for %define
4134 * will suck it and define the corresponding value. Other case:
4135 *
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004136 * %define _%$abc cde
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004137 *
4138 * In this case user wants name to be expanded *before* %define starts
4139 * working, so we'll expand %$abc into something (if it has a value;
4140 * otherwise it will be left as-is) then concatenate all successive
4141 * PP_IDs into one.
4142 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004143static Token *expand_id(Token * tline)
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004144{
4145 Token *cur, *oldnext = NULL;
4146
H. Peter Anvin734b1882002-04-30 21:01:08 +00004147 if (!tline || !tline->next)
H. Peter Anvine2c80182005-01-15 22:15:51 +00004148 return tline;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004149
4150 cur = tline;
4151 while (cur->next &&
H. Peter Anvine2c80182005-01-15 22:15:51 +00004152 (cur->next->type == TOK_ID ||
4153 cur->next->type == TOK_PREPROC_ID
4154 || cur->next->type == TOK_NUMBER))
4155 cur = cur->next;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004156
4157 /* If identifier consists of just one token, don't expand */
4158 if (cur == tline)
H. Peter Anvine2c80182005-01-15 22:15:51 +00004159 return tline;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004160
H. Peter Anvine2c80182005-01-15 22:15:51 +00004161 if (cur) {
4162 oldnext = cur->next; /* Detach the tail past identifier */
4163 cur->next = NULL; /* so that expand_smacro stops here */
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004164 }
4165
H. Peter Anvin734b1882002-04-30 21:01:08 +00004166 tline = expand_smacro(tline);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004167
H. Peter Anvine2c80182005-01-15 22:15:51 +00004168 if (cur) {
4169 /* expand_smacro possibly changhed tline; re-scan for EOL */
4170 cur = tline;
4171 while (cur && cur->next)
4172 cur = cur->next;
4173 if (cur)
4174 cur->next = oldnext;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004175 }
4176
4177 return tline;
4178}
4179
4180/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004181 * Determine whether the given line constitutes a multi-line macro
4182 * call, and return the MMacro structure called if so. Doesn't have
4183 * to check for an initial label - that's taken care of in
4184 * expand_mmacro - but must check numbers of parameters. Guaranteed
4185 * to be called with tline->type == TOK_ID, so the putative macro
4186 * name is easy to find.
4187 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004188static MMacro *is_mmacro(Token * tline, Token *** params_array)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004189{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004190 MMacro *head, *m;
4191 Token **params;
4192 int nparam;
4193
H. Peter Anvin166c2472008-05-28 12:28:58 -07004194 head = (MMacro *) hash_findix(&mmacros, tline->text);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004195
4196 /*
4197 * Efficiency: first we see if any macro exists with the given
4198 * name. If not, we can return NULL immediately. _Then_ we
4199 * count the parameters, and then we look further along the
4200 * list if necessary to find the proper MMacro.
4201 */
4202 for (m = head; m; m = m->next)
H. Peter Anvine2c80182005-01-15 22:15:51 +00004203 if (!mstrcmp(m->name, tline->text, m->casesense))
4204 break;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004205 if (!m)
H. Peter Anvine2c80182005-01-15 22:15:51 +00004206 return NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004207
4208 /*
4209 * OK, we have a potential macro. Count and demarcate the
4210 * parameters.
4211 */
H. Peter Anvin734b1882002-04-30 21:01:08 +00004212 count_mmac_params(tline->next, &nparam, &params);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004213
4214 /*
4215 * So we know how many parameters we've got. Find the MMacro
4216 * structure that handles this number.
4217 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004218 while (m) {
4219 if (m->nparam_min <= nparam
4220 && (m->plus || nparam <= m->nparam_max)) {
4221 /*
4222 * This one is right. Just check if cycle removal
4223 * prohibits us using it before we actually celebrate...
4224 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004225 if (m->in_progress > m->max_depth) {
4226 if (m->max_depth > 0) {
4227 error(ERR_WARNING,
4228 "reached maximum recursion depth of %i",
4229 m->max_depth);
4230 }
4231 nasm_free(params);
4232 return NULL;
4233 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00004234 /*
4235 * It's right, and we can use it. Add its default
4236 * parameters to the end of our list if necessary.
4237 */
4238 if (m->defaults && nparam < m->nparam_min + m->ndefs) {
4239 params =
4240 nasm_realloc(params,
4241 ((m->nparam_min + m->ndefs +
4242 1) * sizeof(*params)));
4243 while (nparam < m->nparam_min + m->ndefs) {
4244 params[nparam] = m->defaults[nparam - m->nparam_min];
4245 nparam++;
4246 }
4247 }
4248 /*
4249 * If we've gone over the maximum parameter count (and
4250 * we're in Plus mode), ignore parameters beyond
4251 * nparam_max.
4252 */
4253 if (m->plus && nparam > m->nparam_max)
4254 nparam = m->nparam_max;
4255 /*
4256 * Then terminate the parameter list, and leave.
4257 */
4258 if (!params) { /* need this special case */
4259 params = nasm_malloc(sizeof(*params));
4260 nparam = 0;
4261 }
4262 params[nparam] = NULL;
4263 *params_array = params;
4264 return m;
4265 }
4266 /*
4267 * This one wasn't right: look for the next one with the
4268 * same name.
4269 */
4270 for (m = m->next; m; m = m->next)
4271 if (!mstrcmp(m->name, tline->text, m->casesense))
4272 break;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004273 }
4274
4275 /*
4276 * After all that, we didn't find one with the right number of
4277 * parameters. Issue a warning, and fail to expand the macro.
4278 */
H. Peter Anvin917a3492008-09-24 09:14:49 -07004279 error(ERR_WARNING|ERR_PASS1|ERR_WARN_MNP,
H. Peter Anvine2c80182005-01-15 22:15:51 +00004280 "macro `%s' exists, but not taking %d parameters",
4281 tline->text, nparam);
H. Peter Anvin734b1882002-04-30 21:01:08 +00004282 nasm_free(params);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004283 return NULL;
4284}
4285
Keith Kanios891775e2009-07-11 06:08:54 -05004286
4287/*
4288 * Save MMacro invocation specific fields in
4289 * preparation for a recursive macro expansion
4290 */
4291static void push_mmacro(MMacro *m)
4292{
4293 MMacroInvocation *i;
4294
H. Peter Anvin89cee572009-07-15 09:16:54 -04004295 i = nasm_malloc(sizeof(MMacroInvocation));
4296 i->prev = m->prev;
4297 i->params = m->params;
4298 i->iline = m->iline;
4299 i->nparam = m->nparam;
4300 i->rotate = m->rotate;
4301 i->paramlen = m->paramlen;
4302 i->unique = m->unique;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004303 i->condcnt = m->condcnt;
H. Peter Anvin89cee572009-07-15 09:16:54 -04004304 m->prev = i;
Keith Kanios891775e2009-07-11 06:08:54 -05004305}
4306
4307
4308/*
4309 * Restore MMacro invocation specific fields that were
4310 * saved during a previous recursive macro expansion
4311 */
4312static void pop_mmacro(MMacro *m)
4313{
4314 MMacroInvocation *i;
4315
H. Peter Anvin89cee572009-07-15 09:16:54 -04004316 if (m->prev) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004317 i = m->prev;
4318 m->prev = i->prev;
4319 m->params = i->params;
4320 m->iline = i->iline;
4321 m->nparam = i->nparam;
4322 m->rotate = i->rotate;
4323 m->paramlen = i->paramlen;
4324 m->unique = i->unique;
4325 m->condcnt = i->condcnt;
4326 nasm_free(i);
H. Peter Anvin89cee572009-07-15 09:16:54 -04004327 }
Keith Kanios891775e2009-07-11 06:08:54 -05004328}
4329
4330
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004331/*
4332 * Expand the multi-line macro call made by the given line, if
4333 * there is one to be expanded. If there is, push the expansion on
H. Peter Anvineba20a72002-04-30 20:53:55 +00004334 * istk->expansion and return 1. Otherwise return 0.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004335 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004336static int expand_mmacro(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004337{
4338 Token *startline = tline;
4339 Token *label = NULL;
4340 int dont_prepend = 0;
H. Peter Anvince2233b2008-05-25 21:57:00 -07004341 Token **params, *t, *mtok, *tt;
H. Peter Anvineba20a72002-04-30 20:53:55 +00004342 MMacro *m;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004343 Line *l, *ll;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004344 int i, nparam, *paramlen;
H. Peter Anvinc751e862008-06-09 10:18:45 -07004345 const char *mname;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004346
4347 t = tline;
H. Peter Anvineba20a72002-04-30 20:53:55 +00004348 skip_white_(t);
H. Peter Anvince2233b2008-05-25 21:57:00 -07004349 /* if (!tok_type_(t, TOK_ID)) Lino 02/25/02 */
H. Peter Anvindce1e2f2002-04-30 21:06:37 +00004350 if (!tok_type_(t, TOK_ID) && !tok_type_(t, TOK_PREPROC_ID))
H. Peter Anvine2c80182005-01-15 22:15:51 +00004351 return 0;
H. Peter Anvince2233b2008-05-25 21:57:00 -07004352 mtok = t;
H. Peter Anvin734b1882002-04-30 21:01:08 +00004353 m = is_mmacro(t, &params);
H. Peter Anvinc751e862008-06-09 10:18:45 -07004354 if (m) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004355 mname = t->text;
H. Peter Anvinc751e862008-06-09 10:18:45 -07004356 } else {
H. Peter Anvine2c80182005-01-15 22:15:51 +00004357 Token *last;
4358 /*
4359 * We have an id which isn't a macro call. We'll assume
4360 * it might be a label; we'll also check to see if a
4361 * colon follows it. Then, if there's another id after
4362 * that lot, we'll check it again for macro-hood.
4363 */
4364 label = last = t;
4365 t = t->next;
4366 if (tok_type_(t, TOK_WHITESPACE))
4367 last = t, t = t->next;
4368 if (tok_is_(t, ":")) {
4369 dont_prepend = 1;
4370 last = t, t = t->next;
4371 if (tok_type_(t, TOK_WHITESPACE))
4372 last = t, t = t->next;
4373 }
H. Peter Anvin89cee572009-07-15 09:16:54 -04004374 if (!tok_type_(t, TOK_ID) || !(m = is_mmacro(t, &params)))
H. Peter Anvine2c80182005-01-15 22:15:51 +00004375 return 0;
4376 last->next = NULL;
Keith Kanios891775e2009-07-11 06:08:54 -05004377 mname = t->text;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004378 tline = t;
H. Peter Anvineba20a72002-04-30 20:53:55 +00004379 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004380
4381 /*
4382 * Fix up the parameters: this involves stripping leading and
4383 * trailing whitespace, then stripping braces if they are
4384 * present.
4385 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004386 for (nparam = 0; params[nparam]; nparam++) ;
H. Peter Anvin734b1882002-04-30 21:01:08 +00004387 paramlen = nparam ? nasm_malloc(nparam * sizeof(*paramlen)) : NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004388
H. Peter Anvine2c80182005-01-15 22:15:51 +00004389 for (i = 0; params[i]; i++) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07004390 int brace = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004391 int comma = (!m->plus || i < nparam - 1);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004392
H. Peter Anvine2c80182005-01-15 22:15:51 +00004393 t = params[i];
4394 skip_white_(t);
4395 if (tok_is_(t, "{"))
H. Peter Anvin6867acc2007-10-10 14:58:45 -07004396 t = t->next, brace = true, comma = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004397 params[i] = t;
4398 paramlen[i] = 0;
4399 while (t) {
4400 if (comma && t->type == TOK_OTHER && !strcmp(t->text, ","))
4401 break; /* ... because we have hit a comma */
4402 if (comma && t->type == TOK_WHITESPACE
4403 && tok_is_(t->next, ","))
4404 break; /* ... or a space then a comma */
4405 if (brace && t->type == TOK_OTHER && !strcmp(t->text, "}"))
4406 break; /* ... or a brace */
4407 t = t->next;
4408 paramlen[i]++;
4409 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004410 }
4411
4412 /*
4413 * OK, we have a MMacro structure together with a set of
4414 * parameters. We must now go through the expansion and push
H. Peter Anvin76690a12002-04-30 20:52:49 +00004415 * copies of each Line on to istk->expansion. Substitution of
4416 * parameter tokens and macro-local tokens doesn't get done
4417 * until the single-line macro substitution process; this is
4418 * because delaying them allows us to change the semantics
4419 * later through %rotate.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004420 *
H. Peter Anvin76690a12002-04-30 20:52:49 +00004421 * First, push an end marker on to istk->expansion, mark this
4422 * macro as in progress, and set up its invocation-specific
4423 * variables.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004424 */
4425 ll = nasm_malloc(sizeof(Line));
4426 ll->next = istk->expansion;
4427 ll->finishes = m;
4428 ll->first = NULL;
4429 istk->expansion = ll;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004430
H. Peter Anvin89cee572009-07-15 09:16:54 -04004431 /*
4432 * Save the previous MMacro expansion in the case of
4433 * macro recursion
4434 */
4435 if (m->max_depth && m->in_progress)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004436 push_mmacro(m);
H. Peter Anvin76690a12002-04-30 20:52:49 +00004437
Keith Kanios891775e2009-07-11 06:08:54 -05004438 m->in_progress ++;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004439 m->params = params;
4440 m->iline = tline;
4441 m->nparam = nparam;
4442 m->rotate = 0;
4443 m->paramlen = paramlen;
4444 m->unique = unique++;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004445 m->lineno = 0;
Keith Kanios3c0d91f2009-10-25 13:28:03 -05004446 m->condcnt = 0;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004447
4448 m->next_active = istk->mstk;
4449 istk->mstk = m;
4450
Cyrill Gorcunov367d59e2010-04-09 15:43:03 +04004451 list_for_each(l, m->expansion) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00004452 Token **tail;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004453
H. Peter Anvine2c80182005-01-15 22:15:51 +00004454 ll = nasm_malloc(sizeof(Line));
4455 ll->finishes = NULL;
4456 ll->next = istk->expansion;
4457 istk->expansion = ll;
4458 tail = &ll->first;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004459
Cyrill Gorcunov367d59e2010-04-09 15:43:03 +04004460 list_for_each(t, l->first) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00004461 Token *x = t;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004462 switch (t->type) {
4463 case TOK_PREPROC_Q:
4464 tt = *tail = new_Token(NULL, TOK_ID, mname, 0);
4465 break;
4466 case TOK_PREPROC_QQ:
4467 tt = *tail = new_Token(NULL, TOK_ID, m->name, 0);
4468 break;
4469 case TOK_PREPROC_ID:
4470 if (t->text[1] == '0' && t->text[2] == '0') {
4471 dont_prepend = -1;
4472 x = label;
4473 if (!x)
4474 continue;
4475 }
4476 /* fall through */
4477 default:
4478 tt = *tail = new_Token(NULL, x->type, x->text, 0);
4479 break;
4480 }
4481 tail = &tt->next;
4482 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00004483 *tail = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004484 }
4485
4486 /*
H. Peter Anvineba20a72002-04-30 20:53:55 +00004487 * If we had a label, push it on as the first line of
4488 * the macro expansion.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004489 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004490 if (label) {
4491 if (dont_prepend < 0)
4492 free_tlist(startline);
4493 else {
4494 ll = nasm_malloc(sizeof(Line));
4495 ll->finishes = NULL;
4496 ll->next = istk->expansion;
4497 istk->expansion = ll;
4498 ll->first = startline;
4499 if (!dont_prepend) {
4500 while (label->next)
4501 label = label->next;
4502 label->next = tt = new_Token(NULL, TOK_OTHER, ":", 0);
4503 }
4504 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004505 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004506
H. Peter Anvin734b1882002-04-30 21:01:08 +00004507 list->uplevel(m->nolist ? LIST_MACRO_NOLIST : LIST_MACRO);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004508
H. Peter Anvineba20a72002-04-30 20:53:55 +00004509 return 1;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004510}
4511
Victor van den Elzen3b404c02008-09-18 13:51:36 +02004512/* The function that actually does the error reporting */
4513static void verror(int severity, const char *fmt, va_list arg)
4514{
4515 char buff[1024];
4516
4517 vsnprintf(buff, sizeof(buff), fmt, arg);
4518
4519 if (istk && istk->mstk && istk->mstk->name)
H. Peter Anvindbb640b2009-07-18 18:57:16 -07004520 nasm_error(severity, "(%s:%d) %s", istk->mstk->name,
Victor van den Elzen3b404c02008-09-18 13:51:36 +02004521 istk->mstk->lineno, buff);
4522 else
H. Peter Anvindbb640b2009-07-18 18:57:16 -07004523 nasm_error(severity, "%s", buff);
Victor van den Elzen3b404c02008-09-18 13:51:36 +02004524}
4525
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004526/*
4527 * Since preprocessor always operate only on the line that didn't
H. Peter Anvin917a3492008-09-24 09:14:49 -07004528 * arrived yet, we should always use ERR_OFFBY1.
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004529 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004530static void error(int severity, const char *fmt, ...)
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004531{
4532 va_list arg;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004533
4534 /* If we're in a dead branch of IF or something like it, ignore the error */
H. Peter Anvin77ba0c62002-05-14 03:18:53 +00004535 if (istk && istk->conds && !emitting(istk->conds->state))
H. Peter Anvine2c80182005-01-15 22:15:51 +00004536 return;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004537
H. Peter Anvin734b1882002-04-30 21:01:08 +00004538 va_start(arg, fmt);
Victor van den Elzen3b404c02008-09-18 13:51:36 +02004539 verror(severity, fmt, arg);
H. Peter Anvin734b1882002-04-30 21:01:08 +00004540 va_end(arg);
Victor van den Elzen3b404c02008-09-18 13:51:36 +02004541}
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004542
Victor van den Elzen3b404c02008-09-18 13:51:36 +02004543/*
4544 * Because %else etc are evaluated in the state context
4545 * of the previous branch, errors might get lost with error():
4546 * %if 0 ... %else trailing garbage ... %endif
4547 * So %else etc should report errors with this function.
4548 */
4549static void error_precond(int severity, const char *fmt, ...)
4550{
4551 va_list arg;
4552
4553 /* Only ignore the error if it's really in a dead branch */
4554 if (istk && istk->conds && istk->conds->state == COND_NEVER)
4555 return;
4556
4557 va_start(arg, fmt);
4558 verror(severity, fmt, arg);
4559 va_end(arg);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004560}
4561
H. Peter Anvin734b1882002-04-30 21:01:08 +00004562static void
H. Peter Anvindbb640b2009-07-18 18:57:16 -07004563pp_reset(char *file, int apass, ListGen * listgen, StrList **deplist)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004564{
H. Peter Anvin7383b402008-09-24 10:20:40 -07004565 Token *t;
4566
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004567 cstk = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004568 istk = nasm_malloc(sizeof(Include));
4569 istk->next = NULL;
4570 istk->conds = NULL;
4571 istk->expansion = NULL;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004572 istk->mstk = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004573 istk->fp = fopen(file, "r");
H. Peter Anvineba20a72002-04-30 20:53:55 +00004574 istk->fname = NULL;
4575 src_set_fname(nasm_strdup(file));
4576 src_set_linnum(0);
4577 istk->lineinc = 1;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004578 if (!istk->fp)
H. Peter Anvin917a3492008-09-24 09:14:49 -07004579 error(ERR_FATAL|ERR_NOFILE, "unable to open input file `%s'",
H. Peter Anvine2c80182005-01-15 22:15:51 +00004580 file);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004581 defining = NULL;
Charles Crayned4200be2008-07-12 16:42:33 -07004582 nested_mac_count = 0;
4583 nested_rep_count = 0;
H. Peter Anvin97a23472007-09-16 17:57:25 -07004584 init_macros();
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004585 unique = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004586 if (tasm_compatible_mode) {
H. Peter Anvina4835d42008-05-20 14:21:29 -07004587 stdmacpos = nasm_stdmac;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004588 } else {
H. Peter Anvina4835d42008-05-20 14:21:29 -07004589 stdmacpos = nasm_stdmac_after_tasm;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004590 }
H. Peter Anvind2456592008-06-19 15:04:18 -07004591 any_extrastdmac = extrastdmac && *extrastdmac;
4592 do_predef = true;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004593 list = listgen;
H. Peter Anvin61f130f2008-09-25 15:45:06 -07004594
4595 /*
4596 * 0 for dependencies, 1 for preparatory passes, 2 for final pass.
4597 * The caller, however, will also pass in 3 for preprocess-only so
4598 * we can set __PASS__ accordingly.
4599 */
4600 pass = apass > 2 ? 2 : apass;
4601
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07004602 dephead = deptail = deplist;
4603 if (deplist) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004604 StrList *sl = nasm_malloc(strlen(file)+1+sizeof sl->next);
4605 sl->next = NULL;
4606 strcpy(sl->str, file);
4607 *deptail = sl;
4608 deptail = &sl->next;
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07004609 }
H. Peter Anvin7383b402008-09-24 10:20:40 -07004610
H. Peter Anvin61f130f2008-09-25 15:45:06 -07004611 /*
4612 * Define the __PASS__ macro. This is defined here unlike
4613 * all the other builtins, because it is special -- it varies between
4614 * passes.
4615 */
H. Peter Anvin7383b402008-09-24 10:20:40 -07004616 t = nasm_malloc(sizeof(*t));
4617 t->next = NULL;
H. Peter Anvin61f130f2008-09-25 15:45:06 -07004618 make_tok_num(t, apass);
H. Peter Anvin7383b402008-09-24 10:20:40 -07004619 t->a.mac = NULL;
4620 define_smacro(NULL, "__PASS__", true, 0, t);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004621}
4622
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004623static char *pp_getline(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004624{
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004625 char *line;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004626 Token *tline;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004627
H. Peter Anvine2c80182005-01-15 22:15:51 +00004628 while (1) {
4629 /*
Keith Kaniosb7a89542007-04-12 02:40:54 +00004630 * Fetch a tokenized line, either from the macro-expansion
H. Peter Anvine2c80182005-01-15 22:15:51 +00004631 * buffer or from the input file.
4632 */
4633 tline = NULL;
4634 while (istk->expansion && istk->expansion->finishes) {
4635 Line *l = istk->expansion;
4636 if (!l->finishes->name && l->finishes->in_progress > 1) {
4637 Line *ll;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004638
H. Peter Anvine2c80182005-01-15 22:15:51 +00004639 /*
4640 * This is a macro-end marker for a macro with no
4641 * name, which means it's not really a macro at all
4642 * but a %rep block, and the `in_progress' field is
4643 * more than 1, meaning that we still need to
4644 * repeat. (1 means the natural last repetition; 0
4645 * means termination by %exitrep.) We have
4646 * therefore expanded up to the %endrep, and must
4647 * push the whole block on to the expansion buffer
4648 * again. We don't bother to remove the macro-end
4649 * marker: we'd only have to generate another one
4650 * if we did.
4651 */
4652 l->finishes->in_progress--;
4653 for (l = l->finishes->expansion; l; l = l->next) {
4654 Token *t, *tt, **tail;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004655
H. Peter Anvine2c80182005-01-15 22:15:51 +00004656 ll = nasm_malloc(sizeof(Line));
4657 ll->next = istk->expansion;
4658 ll->finishes = NULL;
4659 ll->first = NULL;
4660 tail = &ll->first;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004661
H. Peter Anvine2c80182005-01-15 22:15:51 +00004662 for (t = l->first; t; t = t->next) {
4663 if (t->text || t->type == TOK_WHITESPACE) {
4664 tt = *tail =
4665 new_Token(NULL, t->type, t->text, 0);
4666 tail = &tt->next;
4667 }
4668 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00004669
H. Peter Anvine2c80182005-01-15 22:15:51 +00004670 istk->expansion = ll;
4671 }
4672 } else {
4673 /*
4674 * Check whether a `%rep' was started and not ended
4675 * within this macro expansion. This can happen and
4676 * should be detected. It's a fatal error because
4677 * I'm too confused to work out how to recover
4678 * sensibly from it.
4679 */
4680 if (defining) {
4681 if (defining->name)
4682 error(ERR_PANIC,
4683 "defining with name in expansion");
4684 else if (istk->mstk->name)
4685 error(ERR_FATAL,
4686 "`%%rep' without `%%endrep' within"
4687 " expansion of macro `%s'",
4688 istk->mstk->name);
4689 }
H. Peter Anvin87bc6192002-04-30 20:53:16 +00004690
H. Peter Anvine2c80182005-01-15 22:15:51 +00004691 /*
4692 * FIXME: investigate the relationship at this point between
4693 * istk->mstk and l->finishes
4694 */
4695 {
4696 MMacro *m = istk->mstk;
4697 istk->mstk = m->next_active;
4698 if (m->name) {
4699 /*
4700 * This was a real macro call, not a %rep, and
4701 * therefore the parameter information needs to
4702 * be freed.
4703 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004704 if (m->prev) {
4705 pop_mmacro(m);
4706 l->finishes->in_progress --;
4707 } else {
Keith Kanios891775e2009-07-11 06:08:54 -05004708 nasm_free(m->params);
4709 free_tlist(m->iline);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004710 nasm_free(m->paramlen);
4711 l->finishes->in_progress = 0;
4712 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00004713 } else
4714 free_mmacro(m);
4715 }
4716 istk->expansion = l->next;
4717 nasm_free(l);
4718 list->downlevel(LIST_MACRO);
4719 }
4720 }
4721 while (1) { /* until we get a line we can use */
H. Peter Anvineba20a72002-04-30 20:53:55 +00004722
H. Peter Anvine2c80182005-01-15 22:15:51 +00004723 if (istk->expansion) { /* from a macro expansion */
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004724 char *p;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004725 Line *l = istk->expansion;
4726 if (istk->mstk)
4727 istk->mstk->lineno++;
4728 tline = l->first;
4729 istk->expansion = l->next;
4730 nasm_free(l);
H. Peter Anvin6867acc2007-10-10 14:58:45 -07004731 p = detoken(tline, false);
H. Peter Anvine2c80182005-01-15 22:15:51 +00004732 list->line(LIST_MACRO, p);
4733 nasm_free(p);
4734 break;
4735 }
4736 line = read_line();
4737 if (line) { /* from the current input file */
4738 line = prepreproc(line);
Keith Kaniosb7a89542007-04-12 02:40:54 +00004739 tline = tokenize(line);
H. Peter Anvine2c80182005-01-15 22:15:51 +00004740 nasm_free(line);
4741 break;
4742 }
4743 /*
4744 * The current file has ended; work down the istk
4745 */
4746 {
4747 Include *i = istk;
4748 fclose(i->fp);
4749 if (i->conds)
4750 error(ERR_FATAL,
4751 "expected `%%endif' before end of file");
4752 /* only set line and file name if there's a next node */
4753 if (i->next) {
4754 src_set_linnum(i->lineno);
4755 nasm_free(src_set_fname(i->fname));
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004756 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00004757 istk = i->next;
4758 list->downlevel(LIST_INCLUDE);
4759 nasm_free(i);
4760 if (!istk)
4761 return NULL;
Victor van den Elzen4c9d6222008-10-01 13:08:50 +02004762 if (istk->expansion && istk->expansion->finishes)
4763 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004764 }
4765 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004766
H. Peter Anvine2c80182005-01-15 22:15:51 +00004767 /*
4768 * We must expand MMacro parameters and MMacro-local labels
4769 * _before_ we plunge into directive processing, to cope
4770 * with things like `%define something %1' such as STRUC
4771 * uses. Unless we're _defining_ a MMacro, in which case
4772 * those tokens should be left alone to go into the
4773 * definition; and unless we're in a non-emitting
4774 * condition, in which case we don't want to meddle with
4775 * anything.
4776 */
Charles Crayned4200be2008-07-12 16:42:33 -07004777 if (!defining && !(istk->conds && !emitting(istk->conds->state))
H. Peter Anvin992fe752008-10-19 15:45:05 -07004778 && !(istk->mstk && !istk->mstk->in_progress)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00004779 tline = expand_mmac_params(tline);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004780 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00004781
H. Peter Anvine2c80182005-01-15 22:15:51 +00004782 /*
4783 * Check the line to see if it's a preprocessor directive.
4784 */
4785 if (do_directive(tline) == DIRECTIVE_FOUND) {
4786 continue;
4787 } else if (defining) {
4788 /*
4789 * We're defining a multi-line macro. We emit nothing
4790 * at all, and just
Keith Kaniosb7a89542007-04-12 02:40:54 +00004791 * shove the tokenized line on to the macro definition.
H. Peter Anvine2c80182005-01-15 22:15:51 +00004792 */
4793 Line *l = nasm_malloc(sizeof(Line));
4794 l->next = defining->expansion;
4795 l->first = tline;
H. Peter Anvin538002d2008-06-28 18:30:27 -07004796 l->finishes = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004797 defining->expansion = l;
4798 continue;
4799 } else if (istk->conds && !emitting(istk->conds->state)) {
4800 /*
4801 * We're in a non-emitting branch of a condition block.
4802 * Emit nothing at all, not even a blank line: when we
4803 * emerge from the condition we'll give a line-number
4804 * directive so we keep our place correctly.
4805 */
4806 free_tlist(tline);
4807 continue;
4808 } else if (istk->mstk && !istk->mstk->in_progress) {
4809 /*
4810 * We're in a %rep block which has been terminated, so
4811 * we're walking through to the %endrep without
4812 * emitting anything. Emit nothing at all, not even a
4813 * blank line: when we emerge from the %rep block we'll
4814 * give a line-number directive so we keep our place
4815 * correctly.
4816 */
4817 free_tlist(tline);
4818 continue;
4819 } else {
4820 tline = expand_smacro(tline);
4821 if (!expand_mmacro(tline)) {
4822 /*
Keith Kaniosb7a89542007-04-12 02:40:54 +00004823 * De-tokenize the line again, and emit it.
H. Peter Anvine2c80182005-01-15 22:15:51 +00004824 */
H. Peter Anvin6867acc2007-10-10 14:58:45 -07004825 line = detoken(tline, true);
H. Peter Anvine2c80182005-01-15 22:15:51 +00004826 free_tlist(tline);
4827 break;
4828 } else {
4829 continue; /* expand_mmacro calls free_tlist */
4830 }
4831 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004832 }
4833
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004834 return line;
4835}
4836
H. Peter Anvine2c80182005-01-15 22:15:51 +00004837static void pp_cleanup(int pass)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004838{
H. Peter Anvine2c80182005-01-15 22:15:51 +00004839 if (defining) {
H. Peter Anvin89cee572009-07-15 09:16:54 -04004840 if (defining->name) {
Victor van den Elzen8f1120f2008-07-16 13:41:37 +02004841 error(ERR_NONFATAL,
4842 "end of file while still defining macro `%s'",
4843 defining->name);
4844 } else {
4845 error(ERR_NONFATAL, "end of file while still in %%rep");
4846 }
4847
H. Peter Anvine2c80182005-01-15 22:15:51 +00004848 free_mmacro(defining);
Cyrill Gorcunova327c652010-02-14 17:19:38 +03004849 defining = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004850 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004851 while (cstk)
H. Peter Anvine2c80182005-01-15 22:15:51 +00004852 ctx_pop();
H. Peter Anvin97a23472007-09-16 17:57:25 -07004853 free_macros();
H. Peter Anvine2c80182005-01-15 22:15:51 +00004854 while (istk) {
4855 Include *i = istk;
4856 istk = istk->next;
4857 fclose(i->fp);
4858 nasm_free(i->fname);
4859 nasm_free(i);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004860 }
4861 while (cstk)
H. Peter Anvine2c80182005-01-15 22:15:51 +00004862 ctx_pop();
H. Peter Anvin86877b22008-06-20 15:55:45 -07004863 nasm_free(src_set_fname(NULL));
H. Peter Anvine2c80182005-01-15 22:15:51 +00004864 if (pass == 0) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004865 IncPath *i;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004866 free_llist(predef);
4867 delete_Blocks();
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004868 while ((i = ipath)) {
4869 ipath = i->next;
4870 if (i->path)
4871 nasm_free(i->path);
4872 nasm_free(i);
4873 }
H. Peter Anvin11dfa1a2008-07-02 18:11:04 -07004874 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004875}
4876
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004877void pp_include_path(char *path)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004878{
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004879 IncPath *i;
H. Peter Anvin37a321f2007-09-24 13:41:58 -07004880
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004881 i = nasm_malloc(sizeof(IncPath));
H. Peter Anvin37a321f2007-09-24 13:41:58 -07004882 i->path = path ? nasm_strdup(path) : NULL;
Frank Kotlerd0ed6fd2003-08-27 11:33:56 +00004883 i->next = NULL;
4884
H. Peter Anvin89cee572009-07-15 09:16:54 -04004885 if (ipath) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00004886 IncPath *j = ipath;
H. Peter Anvin89cee572009-07-15 09:16:54 -04004887 while (j->next)
H. Peter Anvine2c80182005-01-15 22:15:51 +00004888 j = j->next;
4889 j->next = i;
4890 } else {
4891 ipath = i;
Frank Kotlerd0ed6fd2003-08-27 11:33:56 +00004892 }
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004893}
Frank Kotlerd0ed6fd2003-08-27 11:33:56 +00004894
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004895void pp_pre_include(char *fname)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004896{
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004897 Token *inc, *space, *name;
4898 Line *l;
4899
H. Peter Anvin734b1882002-04-30 21:01:08 +00004900 name = new_Token(NULL, TOK_INTERNAL_STRING, fname, 0);
4901 space = new_Token(name, TOK_WHITESPACE, NULL, 0);
4902 inc = new_Token(space, TOK_PREPROC_ID, "%include", 0);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004903
4904 l = nasm_malloc(sizeof(Line));
4905 l->next = predef;
4906 l->first = inc;
H. Peter Anvin538002d2008-06-28 18:30:27 -07004907 l->finishes = NULL;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004908 predef = l;
4909}
4910
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004911void pp_pre_define(char *definition)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004912{
4913 Token *def, *space;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004914 Line *l;
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004915 char *equals;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004916
4917 equals = strchr(definition, '=');
H. Peter Anvin734b1882002-04-30 21:01:08 +00004918 space = new_Token(NULL, TOK_WHITESPACE, NULL, 0);
4919 def = new_Token(space, TOK_PREPROC_ID, "%define", 0);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004920 if (equals)
H. Peter Anvine2c80182005-01-15 22:15:51 +00004921 *equals = ' ';
Keith Kaniosb7a89542007-04-12 02:40:54 +00004922 space->next = tokenize(definition);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004923 if (equals)
H. Peter Anvine2c80182005-01-15 22:15:51 +00004924 *equals = '=';
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004925
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004926 l = nasm_malloc(sizeof(Line));
4927 l->next = predef;
4928 l->first = def;
H. Peter Anvin538002d2008-06-28 18:30:27 -07004929 l->finishes = NULL;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004930 predef = l;
4931}
4932
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004933void pp_pre_undefine(char *definition)
H. Peter Anvin620515a2002-04-30 20:57:38 +00004934{
4935 Token *def, *space;
4936 Line *l;
4937
H. Peter Anvin734b1882002-04-30 21:01:08 +00004938 space = new_Token(NULL, TOK_WHITESPACE, NULL, 0);
4939 def = new_Token(space, TOK_PREPROC_ID, "%undef", 0);
Keith Kaniosb7a89542007-04-12 02:40:54 +00004940 space->next = tokenize(definition);
H. Peter Anvin620515a2002-04-30 20:57:38 +00004941
4942 l = nasm_malloc(sizeof(Line));
4943 l->next = predef;
4944 l->first = def;
H. Peter Anvin538002d2008-06-28 18:30:27 -07004945 l->finishes = NULL;
H. Peter Anvin620515a2002-04-30 20:57:38 +00004946 predef = l;
4947}
4948
Keith Kaniosb7a89542007-04-12 02:40:54 +00004949/*
4950 * Added by Keith Kanios:
4951 *
4952 * This function is used to assist with "runtime" preprocessor
4953 * directives. (e.g. pp_runtime("%define __BITS__ 64");)
4954 *
4955 * ERRORS ARE IGNORED HERE, SO MAKE COMPLETELY SURE THAT YOU
4956 * PASS A VALID STRING TO THIS FUNCTION!!!!!
4957 */
4958
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004959void pp_runtime(char *definition)
Keith Kaniosb7a89542007-04-12 02:40:54 +00004960{
4961 Token *def;
H. Peter Anvin70653092007-10-19 14:42:29 -07004962
Keith Kaniosb7a89542007-04-12 02:40:54 +00004963 def = tokenize(definition);
H. Peter Anvin89cee572009-07-15 09:16:54 -04004964 if (do_directive(def) == NO_DIRECTIVE_FOUND)
Keith Kaniosb7a89542007-04-12 02:40:54 +00004965 free_tlist(def);
H. Peter Anvin70653092007-10-19 14:42:29 -07004966
Keith Kaniosb7a89542007-04-12 02:40:54 +00004967}
4968
H. Peter Anvina70547f2008-07-19 21:44:26 -07004969void pp_extra_stdmac(macros_t *macros)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004970{
H. Peter Anvin76690a12002-04-30 20:52:49 +00004971 extrastdmac = macros;
4972}
4973
Keith Kaniosa5fc6462007-10-13 07:09:22 -07004974static void make_tok_num(Token * tok, int64_t val)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004975{
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004976 char numbuf[20];
Keith Kaniosa5fc6462007-10-13 07:09:22 -07004977 snprintf(numbuf, sizeof(numbuf), "%"PRId64"", val);
H. Peter Anvineba20a72002-04-30 20:53:55 +00004978 tok->text = nasm_strdup(numbuf);
4979 tok->type = TOK_NUMBER;
4980}
4981
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004982Preproc nasmpp = {
4983 pp_reset,
4984 pp_getline,
4985 pp_cleanup
4986};