blob: ba1acd55507179da7987757dd18955fe34510cc4 [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;
Cyrill Gorcunova7319242010-06-03 22:04:36 +0400485 j = ARRAY_SIZE(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{
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +0400559 while (list)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000560 list = delete_Token(list);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000561}
562
563/*
564 * Free a linked list of lines.
565 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000566static void free_llist(Line * list)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000567{
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +0400568 Line *l, *tmp;
569 list_for_each_safe(l, tmp, list) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000570 free_tlist(l->first);
571 nasm_free(l);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000572 }
573}
574
575/*
H. Peter Anvineba20a72002-04-30 20:53:55 +0000576 * Free an MMacro
577 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000578static void free_mmacro(MMacro * m)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000579{
H. Peter Anvin734b1882002-04-30 21:01:08 +0000580 nasm_free(m->name);
581 free_tlist(m->dlist);
582 nasm_free(m->defaults);
583 free_llist(m->expansion);
584 nasm_free(m);
H. Peter Anvineba20a72002-04-30 20:53:55 +0000585}
586
587/*
H. Peter Anvin97a23472007-09-16 17:57:25 -0700588 * Free all currently defined macros, and free the hash tables
589 */
H. Peter Anvin072771e2008-05-22 13:17:51 -0700590static void free_smacro_table(struct hash_table *smt)
H. Peter Anvin97a23472007-09-16 17:57:25 -0700591{
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +0400592 SMacro *s, *tmp;
H. Peter Anvin072771e2008-05-22 13:17:51 -0700593 const char *key;
594 struct hash_tbl_node *it = NULL;
H. Peter Anvin97a23472007-09-16 17:57:25 -0700595
H. Peter Anvin072771e2008-05-22 13:17:51 -0700596 while ((s = hash_iterate(smt, &it, &key)) != NULL) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300597 nasm_free((void *)key);
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +0400598 list_for_each_safe(s, tmp, s) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300599 nasm_free(s->name);
600 free_tlist(s->expansion);
601 nasm_free(s);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300602 }
H. Peter Anvin97a23472007-09-16 17:57:25 -0700603 }
H. Peter Anvin072771e2008-05-22 13:17:51 -0700604 hash_free(smt);
605}
606
607static void free_mmacro_table(struct hash_table *mmt)
608{
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +0400609 MMacro *m, *tmp;
H. Peter Anvin072771e2008-05-22 13:17:51 -0700610 const char *key;
611 struct hash_tbl_node *it = NULL;
H. Peter Anvin97a23472007-09-16 17:57:25 -0700612
613 it = NULL;
H. Peter Anvin072771e2008-05-22 13:17:51 -0700614 while ((m = hash_iterate(mmt, &it, &key)) != NULL) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300615 nasm_free((void *)key);
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +0400616 list_for_each_safe(m ,tmp, m)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300617 free_mmacro(m);
H. Peter Anvin97a23472007-09-16 17:57:25 -0700618 }
H. Peter Anvin072771e2008-05-22 13:17:51 -0700619 hash_free(mmt);
620}
621
622static void free_macros(void)
623{
H. Peter Anvin166c2472008-05-28 12:28:58 -0700624 free_smacro_table(&smacros);
625 free_mmacro_table(&mmacros);
H. Peter Anvin97a23472007-09-16 17:57:25 -0700626}
627
628/*
629 * Initialize the hash tables
630 */
631static void init_macros(void)
632{
H. Peter Anvin166c2472008-05-28 12:28:58 -0700633 hash_init(&smacros, HASH_LARGE);
634 hash_init(&mmacros, HASH_LARGE);
H. Peter Anvin97a23472007-09-16 17:57:25 -0700635}
636
637/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000638 * Pop the context stack.
639 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000640static void ctx_pop(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000641{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000642 Context *c = cstk;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000643
644 cstk = cstk->next;
H. Peter Anvin166c2472008-05-28 12:28:58 -0700645 free_smacro_table(&c->localmac);
H. Peter Anvin734b1882002-04-30 21:01:08 +0000646 nasm_free(c->name);
647 nasm_free(c);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000648}
649
H. Peter Anvin072771e2008-05-22 13:17:51 -0700650/*
651 * Search for a key in the hash index; adding it if necessary
652 * (in which case we initialize the data pointer to NULL.)
653 */
654static void **
655hash_findi_add(struct hash_table *hash, const char *str)
656{
657 struct hash_insert hi;
658 void **r;
659 char *strx;
660
661 r = hash_findi(hash, str, &hi);
662 if (r)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300663 return r;
H. Peter Anvin072771e2008-05-22 13:17:51 -0700664
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300665 strx = nasm_strdup(str); /* Use a more efficient allocator here? */
H. Peter Anvin072771e2008-05-22 13:17:51 -0700666 return hash_add(&hi, strx, NULL);
667}
668
669/*
670 * Like hash_findi, but returns the data element rather than a pointer
671 * to it. Used only when not adding a new element, hence no third
672 * argument.
673 */
674static void *
675hash_findix(struct hash_table *hash, const char *str)
676{
677 void **p;
678
679 p = hash_findi(hash, str, NULL);
680 return p ? *p : NULL;
681}
682
Cyrill Gorcunov15bdc512010-07-13 11:27:41 +0400683/*
684 * read line from standart macros set,
685 * if there no more left -- return NULL
686 */
687static char *line_from_stdmac(void)
688{
689 unsigned char c;
690 const unsigned char *p = stdmacpos;
691 char *line, *q;
692 size_t len = 0;
693
694 if (!stdmacpos)
695 return NULL;
696
697 while ((c = *p++)) {
698 if (c >= 0x80)
699 len += pp_directives_len[c - 0x80] + 1;
700 else
701 len++;
702 }
703
704 line = nasm_malloc(len + 1);
705 q = line;
706 while ((c = *stdmacpos++)) {
707 if (c >= 0x80) {
708 memcpy(q, pp_directives[c - 0x80], pp_directives_len[c - 0x80]);
709 q += pp_directives_len[c - 0x80];
710 *q++ = ' ';
711 } else {
712 *q++ = c;
713 }
714 }
715 stdmacpos = p;
716 *q = '\0';
717
718 if (!*stdmacpos) {
719 /* This was the last of the standard macro chain... */
720 stdmacpos = NULL;
721 if (any_extrastdmac) {
722 stdmacpos = extrastdmac;
723 any_extrastdmac = false;
724 } else if (do_predef) {
725 Line *pd, *l;
726 Token *head, **tail, *t;
727
728 /*
729 * Nasty hack: here we push the contents of
730 * `predef' on to the top-level expansion stack,
731 * since this is the most convenient way to
732 * implement the pre-include and pre-define
733 * features.
734 */
735 list_for_each(pd, predef) {
736 head = NULL;
737 tail = &head;
738 list_for_each(t, pd->first) {
739 *tail = new_Token(NULL, t->type, t->text, 0);
740 tail = &(*tail)->next;
741 }
742
743 l = nasm_malloc(sizeof(Line));
744 l->next = istk->expansion;
745 l->first = head;
746 l->finishes = NULL;
747
748 istk->expansion = l;
749 }
750 do_predef = false;
751 }
752 }
753
754 return line;
755}
756
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000757#define BUF_DELTA 512
758/*
759 * Read a line from the top file in istk, handling multiple CR/LFs
760 * at the end of the line read, and handling spurious ^Zs. Will
761 * return lines from the standard macro set if this has not already
762 * been done.
763 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000764static char *read_line(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000765{
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000766 char *buffer, *p, *q;
H. Peter Anvin9f394642002-04-30 21:07:51 +0000767 int bufsize, continued_count;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000768
Cyrill Gorcunov15bdc512010-07-13 11:27:41 +0400769 /*
770 * standart macros set (predefined) goes first
771 */
772 p = line_from_stdmac();
773 if (p)
774 return p;
H. Peter Anvin72edbb82008-06-19 16:00:04 -0700775
Cyrill Gorcunov15bdc512010-07-13 11:27:41 +0400776 /*
777 * regular read from a file
778 */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000779 bufsize = BUF_DELTA;
780 buffer = nasm_malloc(BUF_DELTA);
781 p = buffer;
H. Peter Anvin9f394642002-04-30 21:07:51 +0000782 continued_count = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000783 while (1) {
784 q = fgets(p, bufsize - (p - buffer), istk->fp);
785 if (!q)
786 break;
787 p += strlen(p);
788 if (p > buffer && p[-1] == '\n') {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300789 /*
790 * Convert backslash-CRLF line continuation sequences into
791 * nothing at all (for DOS and Windows)
792 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000793 if (((p - 2) > buffer) && (p[-3] == '\\') && (p[-2] == '\r')) {
794 p -= 3;
795 *p = 0;
796 continued_count++;
797 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300798 /*
799 * Also convert backslash-LF line continuation sequences into
800 * nothing at all (for Unix)
801 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000802 else if (((p - 1) > buffer) && (p[-2] == '\\')) {
803 p -= 2;
804 *p = 0;
805 continued_count++;
806 } else {
807 break;
808 }
809 }
810 if (p - buffer > bufsize - 10) {
Keith Kaniosb7a89542007-04-12 02:40:54 +0000811 int32_t offset = p - buffer;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000812 bufsize += BUF_DELTA;
813 buffer = nasm_realloc(buffer, bufsize);
814 p = buffer + offset; /* prevent stale-pointer problems */
815 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000816 }
817
H. Peter Anvine2c80182005-01-15 22:15:51 +0000818 if (!q && p == buffer) {
819 nasm_free(buffer);
820 return NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000821 }
822
H. Peter Anvine2c80182005-01-15 22:15:51 +0000823 src_set_linnum(src_get_linnum() + istk->lineinc +
824 (continued_count * istk->lineinc));
H. Peter Anvineba20a72002-04-30 20:53:55 +0000825
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000826 /*
827 * Play safe: remove CRs as well as LFs, if any of either are
828 * present at the end of the line.
829 */
H. Peter Anvineba20a72002-04-30 20:53:55 +0000830 while (--p >= buffer && (*p == '\n' || *p == '\r'))
H. Peter Anvine2c80182005-01-15 22:15:51 +0000831 *p = '\0';
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000832
833 /*
834 * Handle spurious ^Z, which may be inserted into source files
835 * by some file transfer utilities.
836 */
837 buffer[strcspn(buffer, "\032")] = '\0';
838
H. Peter Anvin734b1882002-04-30 21:01:08 +0000839 list->line(LIST_READ, buffer);
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000840
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000841 return buffer;
842}
843
844/*
Keith Kaniosb7a89542007-04-12 02:40:54 +0000845 * Tokenize a line of text. This is a very simple process since we
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000846 * don't need to parse the value out of e.g. numeric tokens: we
847 * simply split one string into many.
848 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000849static Token *tokenize(char *line)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000850{
H. Peter Anvinca544db2008-10-19 19:30:11 -0700851 char c, *p = line;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +0000852 enum pp_token_type type;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000853 Token *list = NULL;
854 Token *t, **tail = &list;
855
H. Peter Anvine2c80182005-01-15 22:15:51 +0000856 while (*line) {
857 p = line;
858 if (*p == '%') {
859 p++;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300860 if (*p == '+' && !nasm_isdigit(p[1])) {
861 p++;
862 type = TOK_PASTE;
863 } else if (nasm_isdigit(*p) ||
864 ((*p == '-' || *p == '+') && nasm_isdigit(p[1]))) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000865 do {
866 p++;
867 }
H. Peter Anvinbda7a6e2008-06-21 10:23:17 -0700868 while (nasm_isdigit(*p));
H. Peter Anvine2c80182005-01-15 22:15:51 +0000869 type = TOK_PREPROC_ID;
870 } else if (*p == '{') {
871 p++;
872 while (*p && *p != '}') {
873 p[-1] = *p;
874 p++;
875 }
876 p[-1] = '\0';
877 if (*p)
878 p++;
879 type = TOK_PREPROC_ID;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300880 } else if (*p == '[') {
881 int lvl = 1;
882 line += 2; /* Skip the leading %[ */
883 p++;
884 while (lvl && (c = *p++)) {
885 switch (c) {
886 case ']':
887 lvl--;
888 break;
889 case '%':
890 if (*p == '[')
891 lvl++;
892 break;
893 case '\'':
894 case '\"':
895 case '`':
Cyrill Gorcunovc6360a72010-07-13 13:32:19 +0400896 p = nasm_skip_string(p - 1) + 1;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300897 break;
898 default:
899 break;
900 }
901 }
902 p--;
903 if (*p)
904 *p++ = '\0';
905 if (lvl)
906 error(ERR_NONFATAL, "unterminated %[ construct");
907 type = TOK_INDIRECT;
908 } else if (*p == '?') {
909 type = TOK_PREPROC_Q; /* %? */
910 p++;
911 if (*p == '?') {
912 type = TOK_PREPROC_QQ; /* %?? */
913 p++;
914 }
H. Peter Anvine2c80182005-01-15 22:15:51 +0000915 } else if (isidchar(*p) ||
916 ((*p == '!' || *p == '%' || *p == '$') &&
917 isidchar(p[1]))) {
918 do {
919 p++;
920 }
921 while (isidchar(*p));
922 type = TOK_PREPROC_ID;
923 } else {
924 type = TOK_OTHER;
925 if (*p == '%')
926 p++;
927 }
928 } else if (isidstart(*p) || (*p == '$' && isidstart(p[1]))) {
929 type = TOK_ID;
930 p++;
931 while (*p && isidchar(*p))
932 p++;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -0700933 } else if (*p == '\'' || *p == '"' || *p == '`') {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000934 /*
935 * A string token.
936 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000937 type = TOK_STRING;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300938 p = nasm_skip_string(p);
Nickolay Yurchenkof3b3ce22003-09-21 20:38:43 +0000939
H. Peter Anvine2c80182005-01-15 22:15:51 +0000940 if (*p) {
941 p++;
942 } else {
H. Peter Anvin917a3492008-09-24 09:14:49 -0700943 error(ERR_WARNING|ERR_PASS1, "unterminated string");
H. Peter Anvine2c80182005-01-15 22:15:51 +0000944 /* Handling unterminated strings by UNV */
945 /* type = -1; */
946 }
Victor van den Elzenfb5f2512009-04-17 16:17:59 +0200947 } else if (p[0] == '$' && p[1] == '$') {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300948 type = TOK_OTHER; /* TOKEN_BASE */
Victor van den Elzenfb5f2512009-04-17 16:17:59 +0200949 p += 2;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000950 } else if (isnumstart(*p)) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300951 bool is_hex = false;
952 bool is_float = false;
953 bool has_e = false;
954 char c, *r;
H. Peter Anvinc2df2822007-10-24 15:29:28 -0700955
H. Peter Anvine2c80182005-01-15 22:15:51 +0000956 /*
H. Peter Anvinc2df2822007-10-24 15:29:28 -0700957 * A numeric token.
H. Peter Anvine2c80182005-01-15 22:15:51 +0000958 */
H. Peter Anvinc2df2822007-10-24 15:29:28 -0700959
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300960 if (*p == '$') {
961 p++;
962 is_hex = true;
963 }
H. Peter Anvinc2df2822007-10-24 15:29:28 -0700964
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300965 for (;;) {
966 c = *p++;
H. Peter Anvinc2df2822007-10-24 15:29:28 -0700967
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300968 if (!is_hex && (c == 'e' || c == 'E')) {
969 has_e = true;
970 if (*p == '+' || *p == '-') {
971 /*
972 * e can only be followed by +/- if it is either a
973 * prefixed hex number or a floating-point number
974 */
975 p++;
976 is_float = true;
977 }
978 } else if (c == 'H' || c == 'h' || c == 'X' || c == 'x') {
979 is_hex = true;
980 } else if (c == 'P' || c == 'p') {
981 is_float = true;
982 if (*p == '+' || *p == '-')
983 p++;
984 } else if (isnumchar(c) || c == '_')
985 ; /* just advance */
986 else if (c == '.') {
987 /*
988 * we need to deal with consequences of the legacy
989 * parser, like "1.nolist" being two tokens
990 * (TOK_NUMBER, TOK_ID) here; at least give it
991 * a shot for now. In the future, we probably need
992 * a flex-based scanner with proper pattern matching
993 * to do it as well as it can be done. Nothing in
994 * the world is going to help the person who wants
995 * 0x123.p16 interpreted as two tokens, though.
996 */
997 r = p;
998 while (*r == '_')
999 r++;
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001000
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001001 if (nasm_isdigit(*r) || (is_hex && nasm_isxdigit(*r)) ||
1002 (!is_hex && (*r == 'e' || *r == 'E')) ||
1003 (*r == 'p' || *r == 'P')) {
1004 p = r;
1005 is_float = true;
1006 } else
1007 break; /* Terminate the token */
1008 } else
1009 break;
1010 }
1011 p--; /* Point to first character beyond number */
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001012
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001013 if (p == line+1 && *line == '$') {
1014 type = TOK_OTHER; /* TOKEN_HERE */
1015 } else {
1016 if (has_e && !is_hex) {
1017 /* 1e13 is floating-point, but 1e13h is not */
1018 is_float = true;
1019 }
H. Peter Anvind784a082009-04-20 14:01:18 -07001020
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001021 type = is_float ? TOK_FLOAT : TOK_NUMBER;
1022 }
H. Peter Anvinbda7a6e2008-06-21 10:23:17 -07001023 } else if (nasm_isspace(*p)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001024 type = TOK_WHITESPACE;
Cyrill Gorcunovf66ac7d2009-10-12 20:41:13 +04001025 p = nasm_skip_spaces(p);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001026 /*
1027 * Whitespace just before end-of-line is discarded by
1028 * pretending it's a comment; whitespace just before a
1029 * comment gets lumped into the comment.
1030 */
1031 if (!*p || *p == ';') {
1032 type = TOK_COMMENT;
1033 while (*p)
1034 p++;
1035 }
1036 } else if (*p == ';') {
1037 type = TOK_COMMENT;
1038 while (*p)
1039 p++;
1040 } else {
1041 /*
1042 * Anything else is an operator of some kind. We check
1043 * for all the double-character operators (>>, <<, //,
1044 * %%, <=, >=, ==, !=, <>, &&, ||, ^^), but anything
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001045 * else is a single-character operator.
H. Peter Anvine2c80182005-01-15 22:15:51 +00001046 */
1047 type = TOK_OTHER;
1048 if ((p[0] == '>' && p[1] == '>') ||
1049 (p[0] == '<' && p[1] == '<') ||
1050 (p[0] == '/' && p[1] == '/') ||
1051 (p[0] == '<' && p[1] == '=') ||
1052 (p[0] == '>' && p[1] == '=') ||
1053 (p[0] == '=' && p[1] == '=') ||
1054 (p[0] == '!' && p[1] == '=') ||
1055 (p[0] == '<' && p[1] == '>') ||
1056 (p[0] == '&' && p[1] == '&') ||
1057 (p[0] == '|' && p[1] == '|') ||
1058 (p[0] == '^' && p[1] == '^')) {
1059 p++;
1060 }
1061 p++;
1062 }
Nickolay Yurchenkof3b3ce22003-09-21 20:38:43 +00001063
H. Peter Anvine2c80182005-01-15 22:15:51 +00001064 /* Handling unterminated string by UNV */
1065 /*if (type == -1)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001066 {
1067 *tail = t = new_Token(NULL, TOK_STRING, line, p-line+1);
1068 t->text[p-line] = *line;
1069 tail = &t->next;
1070 }
1071 else */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001072 if (type != TOK_COMMENT) {
1073 *tail = t = new_Token(NULL, type, line, p - line);
1074 tail = &t->next;
1075 }
1076 line = p;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001077 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001078 return list;
1079}
1080
H. Peter Anvince616072002-04-30 21:02:23 +00001081/*
1082 * this function allocates a new managed block of memory and
H. Peter Anvin70653092007-10-19 14:42:29 -07001083 * returns a pointer to the block. The managed blocks are
H. Peter Anvince616072002-04-30 21:02:23 +00001084 * deleted only all at once by the delete_Blocks function.
1085 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001086static void *new_Block(size_t size)
H. Peter Anvince616072002-04-30 21:02:23 +00001087{
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001088 Blocks *b = &blocks;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001089
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001090 /* first, get to the end of the linked list */
1091 while (b->next)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001092 b = b->next;
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001093 /* now allocate the requested chunk */
1094 b->chunk = nasm_malloc(size);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001095
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001096 /* now allocate a new block for the next request */
1097 b->next = nasm_malloc(sizeof(Blocks));
1098 /* and initialize the contents of the new block */
1099 b->next->next = NULL;
1100 b->next->chunk = NULL;
1101 return b->chunk;
H. Peter Anvince616072002-04-30 21:02:23 +00001102}
1103
1104/*
1105 * this function deletes all managed blocks of memory
1106 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001107static void delete_Blocks(void)
H. Peter Anvince616072002-04-30 21:02:23 +00001108{
H. Peter Anvine2c80182005-01-15 22:15:51 +00001109 Blocks *a, *b = &blocks;
H. Peter Anvince616072002-04-30 21:02:23 +00001110
H. Peter Anvin70653092007-10-19 14:42:29 -07001111 /*
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001112 * keep in mind that the first block, pointed to by blocks
H. Peter Anvin70653092007-10-19 14:42:29 -07001113 * is a static and not dynamically allocated, so we don't
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001114 * free it.
1115 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001116 while (b) {
1117 if (b->chunk)
1118 nasm_free(b->chunk);
1119 a = b;
1120 b = b->next;
1121 if (a != &blocks)
1122 nasm_free(a);
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001123 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001124}
H. Peter Anvin734b1882002-04-30 21:01:08 +00001125
1126/*
H. Peter Anvin70653092007-10-19 14:42:29 -07001127 * this function creates a new Token and passes a pointer to it
H. Peter Anvin734b1882002-04-30 21:01:08 +00001128 * back to the caller. It sets the type and text elements, and
H. Peter Anvinf26e0972008-07-01 21:26:27 -07001129 * also the a.mac and next elements to NULL.
H. Peter Anvin734b1882002-04-30 21:01:08 +00001130 */
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07001131static Token *new_Token(Token * next, enum pp_token_type type,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001132 const char *text, int txtlen)
H. Peter Anvin734b1882002-04-30 21:01:08 +00001133{
1134 Token *t;
1135 int i;
1136
H. Peter Anvin89cee572009-07-15 09:16:54 -04001137 if (!freeTokens) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001138 freeTokens = (Token *) new_Block(TOKEN_BLOCKSIZE * sizeof(Token));
1139 for (i = 0; i < TOKEN_BLOCKSIZE - 1; i++)
1140 freeTokens[i].next = &freeTokens[i + 1];
1141 freeTokens[i].next = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001142 }
1143 t = freeTokens;
1144 freeTokens = t->next;
1145 t->next = next;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07001146 t->a.mac = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001147 t->type = type;
H. Peter Anvin89cee572009-07-15 09:16:54 -04001148 if (type == TOK_WHITESPACE || !text) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001149 t->text = NULL;
1150 } else {
1151 if (txtlen == 0)
1152 txtlen = strlen(text);
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07001153 t->text = nasm_malloc(txtlen+1);
1154 memcpy(t->text, text, txtlen);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001155 t->text[txtlen] = '\0';
H. Peter Anvin734b1882002-04-30 21:01:08 +00001156 }
1157 return t;
1158}
1159
H. Peter Anvine2c80182005-01-15 22:15:51 +00001160static Token *delete_Token(Token * t)
H. Peter Anvin734b1882002-04-30 21:01:08 +00001161{
1162 Token *next = t->next;
1163 nasm_free(t->text);
H. Peter Anvin788e6c12002-04-30 21:02:01 +00001164 t->next = freeTokens;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001165 freeTokens = t;
1166 return next;
1167}
1168
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001169/*
1170 * Convert a line of tokens back into text.
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001171 * If expand_locals is not zero, identifiers of the form "%$*xxx"
1172 * will be transformed into ..@ctxnum.xxx
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001173 */
H. Peter Anvin9e200162008-06-04 17:23:14 -07001174static char *detoken(Token * tlist, bool expand_locals)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001175{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001176 Token *t;
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001177 char *line, *p;
H. Peter Anvinb4daadc2008-01-21 16:31:57 -08001178 const char *q;
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001179 int len = 0;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001180
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001181 list_for_each(t, tlist) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001182 if (t->type == TOK_PREPROC_ID && t->text[1] == '!') {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001183 char *p = getenv(t->text + 2);
Cyrill Gorcunov385d3e92010-07-13 19:08:15 +04001184 char *q = t->text;
H. Peter Anvin5b00bf42010-07-13 11:43:06 -07001185 if (!p) {
1186 error(ERR_NONFATAL | ERR_PASS1,
1187 "nonexistent environment variable `%s'", q + 2);
1188 p = "";
1189 }
1190 t->text = nasm_strdup(p);
Cyrill Gorcunov385d3e92010-07-13 19:08:15 +04001191 nasm_free(q);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001192 }
1193 /* Expand local macros here and not during preprocessing */
1194 if (expand_locals &&
1195 t->type == TOK_PREPROC_ID && t->text &&
1196 t->text[0] == '%' && t->text[1] == '$') {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001197 const char *q;
1198 char *p;
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001199 Context *ctx = get_ctx(t->text, &q, false);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001200 if (ctx) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001201 char buffer[40];
Keith Kanios93f2e9a2007-04-14 00:10:59 +00001202 snprintf(buffer, sizeof(buffer), "..@%"PRIu32".", ctx->number);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001203 p = nasm_strcat(buffer, q);
1204 nasm_free(t->text);
1205 t->text = p;
1206 }
1207 }
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001208 if (t->type == TOK_WHITESPACE)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001209 len++;
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001210 else if (t->text)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001211 len += strlen(t->text);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001212 }
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001213
H. Peter Anvin734b1882002-04-30 21:01:08 +00001214 p = line = nasm_malloc(len + 1);
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001215
1216 list_for_each(t, tlist) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001217 if (t->type == TOK_WHITESPACE) {
H. Peter Anvinb4daadc2008-01-21 16:31:57 -08001218 *p++ = ' ';
H. Peter Anvine2c80182005-01-15 22:15:51 +00001219 } else if (t->text) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001220 q = t->text;
1221 while (*q)
1222 *p++ = *q++;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001223 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001224 }
1225 *p = '\0';
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001226
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001227 return line;
1228}
1229
1230/*
H. Peter Anvin76690a12002-04-30 20:52:49 +00001231 * A scanner, suitable for use by the expression evaluator, which
1232 * operates on a line of Tokens. Expects a pointer to a pointer to
1233 * the first token in the line to be passed in as its private_data
1234 * field.
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001235 *
1236 * FIX: This really needs to be unified with stdscan.
H. Peter Anvin76690a12002-04-30 20:52:49 +00001237 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001238static int ppscan(void *private_data, struct tokenval *tokval)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001239{
H. Peter Anvin76690a12002-04-30 20:52:49 +00001240 Token **tlineptr = private_data;
1241 Token *tline;
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001242 char ourcopy[MAX_KEYWORD+1], *p, *r, *s;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001243
H. Peter Anvine2c80182005-01-15 22:15:51 +00001244 do {
1245 tline = *tlineptr;
1246 *tlineptr = tline ? tline->next : NULL;
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04001247 } while (tline && (tline->type == TOK_WHITESPACE ||
1248 tline->type == TOK_COMMENT));
H. Peter Anvin76690a12002-04-30 20:52:49 +00001249
1250 if (!tline)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001251 return tokval->t_type = TOKEN_EOS;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001252
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001253 tokval->t_charptr = tline->text;
1254
H. Peter Anvin76690a12002-04-30 20:52:49 +00001255 if (tline->text[0] == '$' && !tline->text[1])
H. Peter Anvine2c80182005-01-15 22:15:51 +00001256 return tokval->t_type = TOKEN_HERE;
H. Peter Anvin7cf897e2002-05-30 21:30:33 +00001257 if (tline->text[0] == '$' && tline->text[1] == '$' && !tline->text[2])
H. Peter Anvine2c80182005-01-15 22:15:51 +00001258 return tokval->t_type = TOKEN_BASE;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001259
H. Peter Anvine2c80182005-01-15 22:15:51 +00001260 if (tline->type == TOK_ID) {
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001261 p = tokval->t_charptr = tline->text;
1262 if (p[0] == '$') {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001263 tokval->t_charptr++;
1264 return tokval->t_type = TOKEN_ID;
1265 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00001266
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001267 for (r = p, s = ourcopy; *r; r++) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001268 if (r >= p+MAX_KEYWORD)
1269 return tokval->t_type = TOKEN_ID; /* Not a keyword */
H. Peter Anvinac8f8fc2008-06-11 15:49:41 -07001270 *s++ = nasm_tolower(*r);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001271 }
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001272 *s = '\0';
1273 /* right, so we have an identifier sitting in temp storage. now,
1274 * is it actually a register or instruction name, or what? */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001275 return nasm_token_hash(ourcopy, tokval);
H. Peter Anvin76690a12002-04-30 20:52:49 +00001276 }
1277
H. Peter Anvine2c80182005-01-15 22:15:51 +00001278 if (tline->type == TOK_NUMBER) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001279 bool rn_error;
1280 tokval->t_integer = readnum(tline->text, &rn_error);
1281 tokval->t_charptr = tline->text;
1282 if (rn_error)
1283 return tokval->t_type = TOKEN_ERRNUM;
1284 else
1285 return tokval->t_type = TOKEN_NUM;
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001286 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00001287
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001288 if (tline->type == TOK_FLOAT) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001289 return tokval->t_type = TOKEN_FLOAT;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001290 }
1291
H. Peter Anvine2c80182005-01-15 22:15:51 +00001292 if (tline->type == TOK_STRING) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001293 char bq, *ep;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001294
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001295 bq = tline->text[0];
H. Peter Anvin11627042008-06-09 20:45:19 -07001296 tokval->t_charptr = tline->text;
1297 tokval->t_inttwo = nasm_unquote(tline->text, &ep);
H. Peter Anvind2456592008-06-19 15:04:18 -07001298
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001299 if (ep[0] != bq || ep[1] != '\0')
1300 return tokval->t_type = TOKEN_ERRSTR;
1301 else
1302 return tokval->t_type = TOKEN_STR;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001303 }
1304
H. Peter Anvine2c80182005-01-15 22:15:51 +00001305 if (tline->type == TOK_OTHER) {
1306 if (!strcmp(tline->text, "<<"))
1307 return tokval->t_type = TOKEN_SHL;
1308 if (!strcmp(tline->text, ">>"))
1309 return tokval->t_type = TOKEN_SHR;
1310 if (!strcmp(tline->text, "//"))
1311 return tokval->t_type = TOKEN_SDIV;
1312 if (!strcmp(tline->text, "%%"))
1313 return tokval->t_type = TOKEN_SMOD;
1314 if (!strcmp(tline->text, "=="))
1315 return tokval->t_type = TOKEN_EQ;
1316 if (!strcmp(tline->text, "<>"))
1317 return tokval->t_type = TOKEN_NE;
1318 if (!strcmp(tline->text, "!="))
1319 return tokval->t_type = TOKEN_NE;
1320 if (!strcmp(tline->text, "<="))
1321 return tokval->t_type = TOKEN_LE;
1322 if (!strcmp(tline->text, ">="))
1323 return tokval->t_type = TOKEN_GE;
1324 if (!strcmp(tline->text, "&&"))
1325 return tokval->t_type = TOKEN_DBL_AND;
1326 if (!strcmp(tline->text, "^^"))
1327 return tokval->t_type = TOKEN_DBL_XOR;
1328 if (!strcmp(tline->text, "||"))
1329 return tokval->t_type = TOKEN_DBL_OR;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001330 }
1331
1332 /*
1333 * We have no other options: just return the first character of
1334 * the token text.
1335 */
1336 return tokval->t_type = tline->text[0];
1337}
1338
1339/*
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001340 * Compare a string to the name of an existing macro; this is a
1341 * simple wrapper which calls either strcmp or nasm_stricmp
1342 * depending on the value of the `casesense' parameter.
1343 */
H. Peter Anvin4db5a162007-10-11 13:42:09 -07001344static int mstrcmp(const char *p, const char *q, bool casesense)
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001345{
H. Peter Anvin734b1882002-04-30 21:01:08 +00001346 return casesense ? strcmp(p, q) : nasm_stricmp(p, q);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001347}
1348
1349/*
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07001350 * Compare a string to the name of an existing macro; this is a
1351 * simple wrapper which calls either strcmp or nasm_stricmp
1352 * depending on the value of the `casesense' parameter.
1353 */
1354static int mmemcmp(const char *p, const char *q, size_t l, bool casesense)
1355{
1356 return casesense ? memcmp(p, q, l) : nasm_memicmp(p, q, l);
1357}
1358
1359/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001360 * Return the Context structure associated with a %$ token. Return
1361 * NULL, having _already_ reported an error condition, if the
1362 * context stack isn't deep enough for the supplied number of $
1363 * signs.
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001364 * If all_contexts == true, contexts that enclose current are
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001365 * also scanned for such smacro, until it is found; if not -
1366 * only the context that directly results from the number of $'s
1367 * in variable's name.
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001368 *
1369 * If "namep" is non-NULL, set it to the pointer to the macro name
1370 * tail, i.e. the part beyond %$...
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001371 */
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001372static Context *get_ctx(const char *name, const char **namep,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001373 bool all_contexts)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001374{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001375 Context *ctx;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001376 SMacro *m;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001377 int i;
1378
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001379 if (namep)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001380 *namep = name;
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001381
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001382 if (!name || name[0] != '%' || name[1] != '$')
H. Peter Anvine2c80182005-01-15 22:15:51 +00001383 return NULL;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001384
H. Peter Anvine2c80182005-01-15 22:15:51 +00001385 if (!cstk) {
1386 error(ERR_NONFATAL, "`%s': context stack is empty", name);
1387 return NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001388 }
1389
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001390 name += 2;
1391 ctx = cstk;
1392 i = 0;
1393 while (ctx && *name == '$') {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001394 name++;
1395 i++;
1396 ctx = ctx->next;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001397 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001398 if (!ctx) {
1399 error(ERR_NONFATAL, "`%s': context stack is only"
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001400 " %d level%s deep", name, i, (i == 1 ? "" : "s"));
H. Peter Anvine2c80182005-01-15 22:15:51 +00001401 return NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001402 }
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001403
1404 if (namep)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001405 *namep = name;
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001406
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001407 if (!all_contexts)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001408 return ctx;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001409
H. Peter Anvine2c80182005-01-15 22:15:51 +00001410 do {
1411 /* Search for this smacro in found context */
H. Peter Anvin166c2472008-05-28 12:28:58 -07001412 m = hash_findix(&ctx->localmac, name);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001413 while (m) {
1414 if (!mstrcmp(m->name, name, m->casesense))
1415 return ctx;
1416 m = m->next;
1417 }
1418 ctx = ctx->next;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001419 }
1420 while (ctx);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001421 return NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001422}
1423
1424/*
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001425 * Check to see if a file is already in a string list
1426 */
1427static bool in_list(const StrList *list, const char *str)
1428{
1429 while (list) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001430 if (!strcmp(list->str, str))
1431 return true;
1432 list = list->next;
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001433 }
1434 return false;
1435}
1436
1437/*
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001438 * Open an include file. This routine must always return a valid
1439 * file pointer if it returns - it's responsible for throwing an
1440 * ERR_FATAL and bombing out completely if not. It should also try
1441 * the include path one by one until it finds the file or reaches
1442 * the end of the path.
1443 */
H. Peter Anvin2b1c3b92008-06-06 10:38:46 -07001444static FILE *inc_fopen(const char *file, StrList **dhead, StrList ***dtail,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001445 bool missing_ok)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001446{
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001447 FILE *fp;
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001448 char *prefix = "";
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001449 IncPath *ip = ipath;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001450 int len = strlen(file);
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001451 size_t prefix_len = 0;
1452 StrList *sl;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001453
H. Peter Anvine2c80182005-01-15 22:15:51 +00001454 while (1) {
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001455 sl = nasm_malloc(prefix_len+len+1+sizeof sl->next);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001456 memcpy(sl->str, prefix, prefix_len);
1457 memcpy(sl->str+prefix_len, file, len+1);
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001458 fp = fopen(sl->str, "r");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001459 if (fp && dhead && !in_list(*dhead, sl->str)) {
1460 sl->next = NULL;
1461 **dtail = sl;
1462 *dtail = &sl->next;
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001463 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001464 nasm_free(sl);
1465 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001466 if (fp)
1467 return fp;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001468 if (!ip) {
1469 if (!missing_ok)
1470 break;
1471 prefix = NULL;
1472 } else {
1473 prefix = ip->path;
1474 ip = ip->next;
1475 }
1476 if (prefix) {
1477 prefix_len = strlen(prefix);
1478 } else {
1479 /* -MG given and file not found */
1480 if (dhead && !in_list(*dhead, file)) {
1481 sl = nasm_malloc(len+1+sizeof sl->next);
1482 sl->next = NULL;
1483 strcpy(sl->str, file);
1484 **dtail = sl;
1485 *dtail = &sl->next;
1486 }
1487 return NULL;
1488 }
H. Peter Anvineba20a72002-04-30 20:53:55 +00001489 }
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001490
H. Peter Anvin734b1882002-04-30 21:01:08 +00001491 error(ERR_FATAL, "unable to open include file `%s'", file);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001492 return NULL;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001493}
1494
1495/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001496 * Determine if we should warn on defining a single-line macro of
H. Peter Anvinef7468f2002-04-30 20:57:59 +00001497 * name `name', with `nparam' parameters. If nparam is 0 or -1, will
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001498 * return true if _any_ single-line macro of that name is defined.
1499 * Otherwise, will return true if a single-line macro with either
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001500 * `nparam' or no parameters is defined.
1501 *
1502 * If a macro with precisely the right number of parameters is
H. Peter Anvinef7468f2002-04-30 20:57:59 +00001503 * defined, or nparam is -1, the address of the definition structure
1504 * will be returned in `defn'; otherwise NULL will be returned. If `defn'
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001505 * is NULL, no action will be taken regarding its contents, and no
1506 * error will occur.
1507 *
1508 * Note that this is also called with nparam zero to resolve
1509 * `ifdef'.
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001510 *
1511 * If you already know which context macro belongs to, you can pass
1512 * the context pointer as first parameter; if you won't but name begins
1513 * with %$ the context will be automatically computed. If all_contexts
1514 * is true, macro will be searched in outer contexts as well.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001515 */
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07001516static bool
H. Peter Anvinb2a5fda2008-06-19 21:42:42 -07001517smacro_defined(Context * ctx, const char *name, int nparam, SMacro ** defn,
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07001518 bool nocase)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001519{
H. Peter Anvin166c2472008-05-28 12:28:58 -07001520 struct hash_table *smtbl;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001521 SMacro *m;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001522
H. Peter Anvin97a23472007-09-16 17:57:25 -07001523 if (ctx) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001524 smtbl = &ctx->localmac;
H. Peter Anvin97a23472007-09-16 17:57:25 -07001525 } else if (name[0] == '%' && name[1] == '$') {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001526 if (cstk)
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001527 ctx = get_ctx(name, &name, false);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001528 if (!ctx)
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001529 return false; /* got to return _something_ */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001530 smtbl = &ctx->localmac;
H. Peter Anvin97a23472007-09-16 17:57:25 -07001531 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001532 smtbl = &smacros;
H. Peter Anvin97a23472007-09-16 17:57:25 -07001533 }
H. Peter Anvin166c2472008-05-28 12:28:58 -07001534 m = (SMacro *) hash_findix(smtbl, name);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001535
H. Peter Anvine2c80182005-01-15 22:15:51 +00001536 while (m) {
1537 if (!mstrcmp(m->name, name, m->casesense && nocase) &&
Charles Crayne192d5b52007-10-18 19:02:42 -07001538 (nparam <= 0 || m->nparam == 0 || nparam == (int) m->nparam)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001539 if (defn) {
Charles Crayne192d5b52007-10-18 19:02:42 -07001540 if (nparam == (int) m->nparam || nparam == -1)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001541 *defn = m;
1542 else
1543 *defn = NULL;
1544 }
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001545 return true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001546 }
1547 m = m->next;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001548 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001549
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001550 return false;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001551}
1552
1553/*
1554 * Count and mark off the parameters in a multi-line macro call.
1555 * This is called both from within the multi-line macro expansion
1556 * code, and also to mark off the default parameters when provided
1557 * in a %macro definition line.
1558 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001559static void count_mmac_params(Token * t, int *nparam, Token *** params)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001560{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001561 int paramsize, brace;
1562
1563 *nparam = paramsize = 0;
1564 *params = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001565 while (t) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001566 /* +1: we need space for the final NULL */
H. Peter Anvin91fb6f12008-09-01 10:56:33 -07001567 if (*nparam+1 >= paramsize) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001568 paramsize += PARAM_DELTA;
1569 *params = nasm_realloc(*params, sizeof(**params) * paramsize);
1570 }
1571 skip_white_(t);
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001572 brace = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001573 if (tok_is_(t, "{"))
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001574 brace = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001575 (*params)[(*nparam)++] = t;
1576 while (tok_isnt_(t, brace ? "}" : ","))
1577 t = t->next;
1578 if (t) { /* got a comma/brace */
1579 t = t->next;
1580 if (brace) {
1581 /*
1582 * Now we've found the closing brace, look further
1583 * for the comma.
1584 */
1585 skip_white_(t);
1586 if (tok_isnt_(t, ",")) {
1587 error(ERR_NONFATAL,
1588 "braces do not enclose all of macro parameter");
1589 while (tok_isnt_(t, ","))
1590 t = t->next;
1591 }
1592 if (t)
1593 t = t->next; /* eat the comma */
1594 }
1595 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001596 }
1597}
1598
1599/*
H. Peter Anvin76690a12002-04-30 20:52:49 +00001600 * Determine whether one of the various `if' conditions is true or
1601 * not.
1602 *
1603 * We must free the tline we get passed.
1604 */
H. Peter Anvin70055962007-10-11 00:05:31 -07001605static bool if_condition(Token * tline, enum preproc_token ct)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001606{
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001607 enum pp_conditional i = PP_COND(ct);
H. Peter Anvin70055962007-10-11 00:05:31 -07001608 bool j;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001609 Token *t, *tt, **tptr, *origline;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001610 struct tokenval tokval;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001611 expr *evalresult;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001612 enum pp_token_type needtype;
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001613 const char *p;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001614
1615 origline = tline;
1616
H. Peter Anvine2c80182005-01-15 22:15:51 +00001617 switch (i) {
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001618 case PPC_IFCTX:
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001619 j = false; /* have we matched yet? */
Victor van den Elzen0e857f12008-07-23 13:21:29 +02001620 while (true) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001621 skip_white_(tline);
Victor van den Elzen0e857f12008-07-23 13:21:29 +02001622 if (!tline)
1623 break;
1624 if (tline->type != TOK_ID) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001625 error(ERR_NONFATAL,
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001626 "`%s' expects context identifiers", pp_directives[ct]);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001627 free_tlist(origline);
1628 return -1;
1629 }
Victor van den Elzen0e857f12008-07-23 13:21:29 +02001630 if (cstk && cstk->name && !nasm_stricmp(tline->text, cstk->name))
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001631 j = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001632 tline = tline->next;
1633 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001634 break;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001635
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001636 case PPC_IFDEF:
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001637 j = false; /* have we matched yet? */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001638 while (tline) {
1639 skip_white_(tline);
1640 if (!tline || (tline->type != TOK_ID &&
1641 (tline->type != TOK_PREPROC_ID ||
1642 tline->text[1] != '$'))) {
1643 error(ERR_NONFATAL,
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001644 "`%s' expects macro identifiers", pp_directives[ct]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001645 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001646 }
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07001647 if (smacro_defined(NULL, tline->text, 0, NULL, true))
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001648 j = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001649 tline = tline->next;
1650 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001651 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001652
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001653 case PPC_IFENV:
1654 tline = expand_smacro(tline);
1655 j = false; /* have we matched yet? */
1656 while (tline) {
1657 skip_white_(tline);
1658 if (!tline || (tline->type != TOK_ID &&
1659 (tline->type != TOK_PREPROC_ID ||
1660 tline->text[1] != '!'))) {
1661 error(ERR_NONFATAL,
1662 "`%s' expects environment variable names",
1663 pp_directives[ct]);
1664 goto fail;
1665 }
1666 p = tline->type == TOK_ID ? tline->text : tline->text + 2;
1667 if (getenv(p))
1668 j = true;
1669 tline = tline->next;
1670 }
1671 break;
1672
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001673 case PPC_IFIDN:
1674 case PPC_IFIDNI:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001675 tline = expand_smacro(tline);
1676 t = tt = tline;
1677 while (tok_isnt_(tt, ","))
1678 tt = tt->next;
1679 if (!tt) {
1680 error(ERR_NONFATAL,
1681 "`%s' expects two comma-separated arguments",
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001682 pp_directives[ct]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001683 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001684 }
1685 tt = tt->next;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001686 j = true; /* assume equality unless proved not */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001687 while ((t->type != TOK_OTHER || strcmp(t->text, ",")) && tt) {
1688 if (tt->type == TOK_OTHER && !strcmp(tt->text, ",")) {
1689 error(ERR_NONFATAL, "`%s': more than one comma on line",
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001690 pp_directives[ct]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001691 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001692 }
1693 if (t->type == TOK_WHITESPACE) {
1694 t = t->next;
1695 continue;
1696 }
1697 if (tt->type == TOK_WHITESPACE) {
1698 tt = tt->next;
1699 continue;
1700 }
1701 if (tt->type != t->type) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001702 j = false; /* found mismatching tokens */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001703 break;
1704 }
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07001705 /* When comparing strings, need to unquote them first */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001706 if (t->type == TOK_STRING) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001707 size_t l1 = nasm_unquote(t->text, NULL);
1708 size_t l2 = nasm_unquote(tt->text, NULL);
H. Peter Anvind2456592008-06-19 15:04:18 -07001709
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001710 if (l1 != l2) {
1711 j = false;
1712 break;
1713 }
1714 if (mmemcmp(t->text, tt->text, l1, i == PPC_IFIDN)) {
1715 j = false;
1716 break;
1717 }
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07001718 } else if (mstrcmp(tt->text, t->text, i == PPC_IFIDN) != 0) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001719 j = false; /* found mismatching tokens */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001720 break;
1721 }
Nickolay Yurchenkof3b3ce22003-09-21 20:38:43 +00001722
H. Peter Anvine2c80182005-01-15 22:15:51 +00001723 t = t->next;
1724 tt = tt->next;
1725 }
1726 if ((t->type != TOK_OTHER || strcmp(t->text, ",")) || tt)
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001727 j = false; /* trailing gunk on one end or other */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001728 break;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001729
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001730 case PPC_IFMACRO:
H. Peter Anvin89cee572009-07-15 09:16:54 -04001731 {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001732 bool found = false;
1733 MMacro searching, *mmac;
H. Peter Anvin65747262002-05-07 00:10:05 +00001734
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001735 skip_white_(tline);
1736 tline = expand_id(tline);
1737 if (!tok_type_(tline, TOK_ID)) {
1738 error(ERR_NONFATAL,
1739 "`%s' expects a macro name", pp_directives[ct]);
1740 goto fail;
1741 }
1742 searching.name = nasm_strdup(tline->text);
1743 searching.casesense = true;
1744 searching.plus = false;
1745 searching.nolist = false;
1746 searching.in_progress = 0;
1747 searching.max_depth = 0;
1748 searching.rep_nest = NULL;
1749 searching.nparam_min = 0;
1750 searching.nparam_max = INT_MAX;
1751 tline = expand_smacro(tline->next);
1752 skip_white_(tline);
1753 if (!tline) {
1754 } else if (!tok_type_(tline, TOK_NUMBER)) {
1755 error(ERR_NONFATAL,
1756 "`%s' expects a parameter count or nothing",
1757 pp_directives[ct]);
1758 } else {
1759 searching.nparam_min = searching.nparam_max =
1760 readnum(tline->text, &j);
1761 if (j)
1762 error(ERR_NONFATAL,
1763 "unable to parse parameter count `%s'",
1764 tline->text);
1765 }
1766 if (tline && tok_is_(tline->next, "-")) {
1767 tline = tline->next->next;
1768 if (tok_is_(tline, "*"))
1769 searching.nparam_max = INT_MAX;
1770 else if (!tok_type_(tline, TOK_NUMBER))
1771 error(ERR_NONFATAL,
1772 "`%s' expects a parameter count after `-'",
1773 pp_directives[ct]);
1774 else {
1775 searching.nparam_max = readnum(tline->text, &j);
1776 if (j)
1777 error(ERR_NONFATAL,
1778 "unable to parse parameter count `%s'",
1779 tline->text);
1780 if (searching.nparam_min > searching.nparam_max)
1781 error(ERR_NONFATAL,
1782 "minimum parameter count exceeds maximum");
1783 }
1784 }
1785 if (tline && tok_is_(tline->next, "+")) {
1786 tline = tline->next;
1787 searching.plus = true;
1788 }
1789 mmac = (MMacro *) hash_findix(&mmacros, searching.name);
1790 while (mmac) {
1791 if (!strcmp(mmac->name, searching.name) &&
1792 (mmac->nparam_min <= searching.nparam_max
1793 || searching.plus)
1794 && (searching.nparam_min <= mmac->nparam_max
1795 || mmac->plus)) {
1796 found = true;
1797 break;
1798 }
1799 mmac = mmac->next;
1800 }
1801 if (tline && tline->next)
1802 error(ERR_WARNING|ERR_PASS1,
1803 "trailing garbage after %%ifmacro ignored");
1804 nasm_free(searching.name);
1805 j = found;
1806 break;
H. Peter Anvin89cee572009-07-15 09:16:54 -04001807 }
H. Peter Anvin65747262002-05-07 00:10:05 +00001808
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001809 case PPC_IFID:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001810 needtype = TOK_ID;
1811 goto iftype;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001812 case PPC_IFNUM:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001813 needtype = TOK_NUMBER;
1814 goto iftype;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001815 case PPC_IFSTR:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001816 needtype = TOK_STRING;
1817 goto iftype;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001818
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001819iftype:
1820 t = tline = expand_smacro(tline);
H. Peter Anvind85d2502008-05-04 17:53:31 -07001821
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001822 while (tok_type_(t, TOK_WHITESPACE) ||
1823 (needtype == TOK_NUMBER &&
1824 tok_type_(t, TOK_OTHER) &&
1825 (t->text[0] == '-' || t->text[0] == '+') &&
1826 !t->text[1]))
1827 t = t->next;
H. Peter Anvind85d2502008-05-04 17:53:31 -07001828
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001829 j = tok_type_(t, needtype);
1830 break;
H. Peter Anvincbf768d2008-02-16 16:41:25 -08001831
1832 case PPC_IFTOKEN:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001833 t = tline = expand_smacro(tline);
1834 while (tok_type_(t, TOK_WHITESPACE))
1835 t = t->next;
H. Peter Anvincbf768d2008-02-16 16:41:25 -08001836
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001837 j = false;
1838 if (t) {
1839 t = t->next; /* Skip the actual token */
1840 while (tok_type_(t, TOK_WHITESPACE))
1841 t = t->next;
1842 j = !t; /* Should be nothing left */
1843 }
1844 break;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001845
H. Peter Anvin134b9462008-02-16 17:01:40 -08001846 case PPC_IFEMPTY:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001847 t = tline = expand_smacro(tline);
1848 while (tok_type_(t, TOK_WHITESPACE))
1849 t = t->next;
H. Peter Anvin134b9462008-02-16 17:01:40 -08001850
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001851 j = !t; /* Should be empty */
1852 break;
H. Peter Anvin134b9462008-02-16 17:01:40 -08001853
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001854 case PPC_IF:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001855 t = tline = expand_smacro(tline);
1856 tptr = &t;
1857 tokval.t_type = TOKEN_INVALID;
1858 evalresult = evaluate(ppscan, tptr, &tokval,
1859 NULL, pass | CRITICAL, error, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001860 if (!evalresult)
1861 return -1;
1862 if (tokval.t_type)
H. Peter Anvin917a3492008-09-24 09:14:49 -07001863 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +00001864 "trailing garbage after expression ignored");
1865 if (!is_simple(evalresult)) {
1866 error(ERR_NONFATAL,
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001867 "non-constant value given to `%s'", pp_directives[ct]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001868 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001869 }
Chuck Crayne60ae75d2007-05-02 01:59:16 +00001870 j = reloc_value(evalresult) != 0;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001871 break;
H. Peter Anvin95e28822007-09-12 04:20:08 +00001872
H. Peter Anvine2c80182005-01-15 22:15:51 +00001873 default:
1874 error(ERR_FATAL,
1875 "preprocessor directive `%s' not yet implemented",
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001876 pp_directives[ct]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001877 goto fail;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001878 }
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001879
1880 free_tlist(origline);
1881 return j ^ PP_NEGATIVE(ct);
H. Peter Anvin70653092007-10-19 14:42:29 -07001882
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001883fail:
1884 free_tlist(origline);
1885 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001886}
1887
1888/*
H. Peter Anvin4db5a162007-10-11 13:42:09 -07001889 * Common code for defining an smacro
1890 */
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001891static bool define_smacro(Context *ctx, const char *mname, bool casesense,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001892 int nparam, Token *expansion)
H. Peter Anvin4db5a162007-10-11 13:42:09 -07001893{
1894 SMacro *smac, **smhead;
H. Peter Anvin166c2472008-05-28 12:28:58 -07001895 struct hash_table *smtbl;
H. Peter Anvin70653092007-10-19 14:42:29 -07001896
H. Peter Anvin4db5a162007-10-11 13:42:09 -07001897 if (smacro_defined(ctx, mname, nparam, &smac, casesense)) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001898 if (!smac) {
1899 error(ERR_WARNING|ERR_PASS1,
1900 "single-line macro `%s' defined both with and"
1901 " without parameters", mname);
1902 /*
1903 * Some instances of the old code considered this a failure,
1904 * some others didn't. What is the right thing to do here?
1905 */
1906 free_tlist(expansion);
1907 return false; /* Failure */
1908 } else {
1909 /*
1910 * We're redefining, so we have to take over an
1911 * existing SMacro structure. This means freeing
1912 * what was already in it.
1913 */
1914 nasm_free(smac->name);
1915 free_tlist(smac->expansion);
1916 }
H. Peter Anvin4db5a162007-10-11 13:42:09 -07001917 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001918 smtbl = ctx ? &ctx->localmac : &smacros;
1919 smhead = (SMacro **) hash_findi_add(smtbl, mname);
1920 smac = nasm_malloc(sizeof(SMacro));
1921 smac->next = *smhead;
1922 *smhead = smac;
H. Peter Anvin4db5a162007-10-11 13:42:09 -07001923 }
1924 smac->name = nasm_strdup(mname);
1925 smac->casesense = casesense;
1926 smac->nparam = nparam;
1927 smac->expansion = expansion;
1928 smac->in_progress = false;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001929 return true; /* Success */
H. Peter Anvin4db5a162007-10-11 13:42:09 -07001930}
1931
1932/*
1933 * Undefine an smacro
1934 */
1935static void undef_smacro(Context *ctx, const char *mname)
1936{
1937 SMacro **smhead, *s, **sp;
H. Peter Anvin166c2472008-05-28 12:28:58 -07001938 struct hash_table *smtbl;
H. Peter Anvin4db5a162007-10-11 13:42:09 -07001939
H. Peter Anvin166c2472008-05-28 12:28:58 -07001940 smtbl = ctx ? &ctx->localmac : &smacros;
1941 smhead = (SMacro **)hash_findi(smtbl, mname, NULL);
H. Peter Anvin70653092007-10-19 14:42:29 -07001942
H. Peter Anvin4db5a162007-10-11 13:42:09 -07001943 if (smhead) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001944 /*
1945 * We now have a macro name... go hunt for it.
1946 */
1947 sp = smhead;
1948 while ((s = *sp) != NULL) {
1949 if (!mstrcmp(s->name, mname, s->casesense)) {
1950 *sp = s->next;
1951 nasm_free(s->name);
1952 free_tlist(s->expansion);
1953 nasm_free(s);
1954 } else {
1955 sp = &s->next;
1956 }
1957 }
H. Peter Anvin4db5a162007-10-11 13:42:09 -07001958 }
1959}
1960
H. Peter Anvin8781cb02007-11-08 20:01:11 -08001961/*
H. Peter Anvina26433d2008-07-16 14:40:01 -07001962 * Parse a mmacro specification.
1963 */
1964static bool parse_mmacro_spec(Token *tline, MMacro *def, const char *directive)
1965{
1966 bool err;
1967
1968 tline = tline->next;
1969 skip_white_(tline);
1970 tline = expand_id(tline);
1971 if (!tok_type_(tline, TOK_ID)) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001972 error(ERR_NONFATAL, "`%s' expects a macro name", directive);
1973 return false;
H. Peter Anvina26433d2008-07-16 14:40:01 -07001974 }
Victor van den Elzenb916ede2008-07-23 15:14:22 +02001975
H. Peter Anvin89cee572009-07-15 09:16:54 -04001976 def->prev = NULL;
H. Peter Anvina26433d2008-07-16 14:40:01 -07001977 def->name = nasm_strdup(tline->text);
1978 def->plus = false;
1979 def->nolist = false;
1980 def->in_progress = 0;
1981 def->rep_nest = NULL;
Victor van den Elzenb916ede2008-07-23 15:14:22 +02001982 def->nparam_min = 0;
1983 def->nparam_max = 0;
1984
H. Peter Anvina26433d2008-07-16 14:40:01 -07001985 tline = expand_smacro(tline->next);
1986 skip_white_(tline);
1987 if (!tok_type_(tline, TOK_NUMBER)) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001988 error(ERR_NONFATAL, "`%s' expects a parameter count", directive);
H. Peter Anvina26433d2008-07-16 14:40:01 -07001989 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001990 def->nparam_min = def->nparam_max =
1991 readnum(tline->text, &err);
1992 if (err)
1993 error(ERR_NONFATAL,
1994 "unable to parse parameter count `%s'", tline->text);
H. Peter Anvina26433d2008-07-16 14:40:01 -07001995 }
1996 if (tline && tok_is_(tline->next, "-")) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001997 tline = tline->next->next;
1998 if (tok_is_(tline, "*")) {
1999 def->nparam_max = INT_MAX;
2000 } else if (!tok_type_(tline, TOK_NUMBER)) {
2001 error(ERR_NONFATAL,
2002 "`%s' expects a parameter count after `-'", directive);
2003 } else {
2004 def->nparam_max = readnum(tline->text, &err);
2005 if (err) {
2006 error(ERR_NONFATAL, "unable to parse parameter count `%s'",
2007 tline->text);
2008 }
2009 if (def->nparam_min > def->nparam_max) {
2010 error(ERR_NONFATAL, "minimum parameter count exceeds maximum");
2011 }
2012 }
H. Peter Anvina26433d2008-07-16 14:40:01 -07002013 }
2014 if (tline && tok_is_(tline->next, "+")) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002015 tline = tline->next;
2016 def->plus = true;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002017 }
2018 if (tline && tok_type_(tline->next, TOK_ID) &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002019 !nasm_stricmp(tline->next->text, ".nolist")) {
2020 tline = tline->next;
2021 def->nolist = true;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002022 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002023
H. Peter Anvina26433d2008-07-16 14:40:01 -07002024 /*
2025 * Handle default parameters.
2026 */
2027 if (tline && tline->next) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002028 def->dlist = tline->next;
2029 tline->next = NULL;
2030 count_mmac_params(def->dlist, &def->ndefs, &def->defaults);
H. Peter Anvina26433d2008-07-16 14:40:01 -07002031 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002032 def->dlist = NULL;
2033 def->defaults = NULL;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002034 }
2035 def->expansion = NULL;
2036
H. Peter Anvin89cee572009-07-15 09:16:54 -04002037 if (def->defaults && def->ndefs > def->nparam_max - def->nparam_min &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002038 !def->plus)
2039 error(ERR_WARNING|ERR_PASS1|ERR_WARN_MDP,
2040 "too many default macro parameters");
Victor van den Elzenb916ede2008-07-23 15:14:22 +02002041
H. Peter Anvina26433d2008-07-16 14:40:01 -07002042 return true;
2043}
2044
2045
2046/*
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002047 * Decode a size directive
2048 */
2049static int parse_size(const char *str) {
2050 static const char *size_names[] =
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002051 { "byte", "dword", "oword", "qword", "tword", "word", "yword" };
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002052 static const int sizes[] =
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002053 { 0, 1, 4, 16, 8, 10, 2, 32 };
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002054
Cyrill Gorcunova7319242010-06-03 22:04:36 +04002055 return sizes[bsii(str, size_names, ARRAY_SIZE(size_names))+1];
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002056}
2057
H. Peter Anvinf9c9a672009-07-14 15:14:05 -04002058/*
2059 * nasm_unquote with error if the string contains NUL characters.
2060 * If the string contains NUL characters, issue an error and return
2061 * the C len, i.e. truncate at the NUL.
2062 */
2063static size_t nasm_unquote_cstr(char *qstr, enum preproc_token directive)
2064{
2065 size_t len = nasm_unquote(qstr, NULL);
2066 size_t clen = strlen(qstr);
2067
2068 if (len != clen)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002069 error(ERR_NONFATAL, "NUL character in `%s' directive",
2070 pp_directives[directive]);
H. Peter Anvinf9c9a672009-07-14 15:14:05 -04002071
2072 return clen;
2073}
2074
Ed Beroset3ab3f412002-06-11 03:31:49 +00002075/**
2076 * find and process preprocessor directive in passed line
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002077 * Find out if a line contains a preprocessor directive, and deal
2078 * with it if so.
H. Peter Anvin70653092007-10-19 14:42:29 -07002079 *
Ed Beroset3ab3f412002-06-11 03:31:49 +00002080 * If a directive _is_ found, it is the responsibility of this routine
2081 * (and not the caller) to free_tlist() the line.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002082 *
Ed Beroset3ab3f412002-06-11 03:31:49 +00002083 * @param tline a pointer to the current tokeninzed line linked list
2084 * @return DIRECTIVE_FOUND or NO_DIRECTIVE_FOUND
H. Peter Anvin70653092007-10-19 14:42:29 -07002085 *
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002086 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002087static int do_directive(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00002088{
H. Peter Anvin4169a472007-09-12 01:29:43 +00002089 enum preproc_token i;
2090 int j;
H. Peter Anvin70055962007-10-11 00:05:31 -07002091 bool err;
2092 int nparam;
2093 bool nolist;
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07002094 bool casesense;
H. Peter Anvin8cfdb9d2007-09-14 18:36:01 -07002095 int k, m;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002096 int offset;
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08002097 char *p, *pp;
2098 const char *mname;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002099 Include *inc;
2100 Context *ctx;
2101 Cond *cond;
H. Peter Anvin97a23472007-09-16 17:57:25 -07002102 MMacro *mmac, **mmhead;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002103 Token *t, *tt, *param_start, *macro_start, *last, **tptr, *origline;
2104 Line *l;
2105 struct tokenval tokval;
2106 expr *evalresult;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002107 MMacro *tmp_defining; /* Used when manipulating rep_nest */
H. Peter Anvinf8ba53e2007-10-11 10:11:57 -07002108 int64_t count;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07002109 size_t len;
H. Peter Anvin8e3f75e2008-09-24 00:21:58 -07002110 int severity;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002111
2112 origline = tline;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002113
H. Peter Anvineba20a72002-04-30 20:53:55 +00002114 skip_white_(tline);
H. Peter Anvinf2936d72008-06-04 15:11:23 -07002115 if (!tline || !tok_type_(tline, TOK_PREPROC_ID) ||
H. Peter Anvine2c80182005-01-15 22:15:51 +00002116 (tline->text[1] == '%' || tline->text[1] == '$'
2117 || tline->text[1] == '!'))
2118 return NO_DIRECTIVE_FOUND;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002119
H. Peter Anvin4169a472007-09-12 01:29:43 +00002120 i = pp_token_hash(tline->text);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002121
2122 /*
Cyrill Gorcunovf09116f2010-02-28 11:52:53 +03002123 * FIXME: We zap execution of PP_RMACRO, PP_IRMACRO, PP_EXITMACRO
2124 * since they are known to be buggy at moment, we need to fix them
2125 * in future release (2.09-2.10)
2126 */
2127 if (i == PP_RMACRO || i == PP_RMACRO || i == PP_EXITMACRO) {
2128 error(ERR_NONFATAL, "unknown preprocessor directive `%s'",
2129 tline->text);
2130 return NO_DIRECTIVE_FOUND;
2131 }
2132
2133 /*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002134 * If we're in a non-emitting branch of a condition construct,
H. Peter Anvin76690a12002-04-30 20:52:49 +00002135 * or walking to the end of an already terminated %rep block,
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002136 * we should ignore all directives except for condition
2137 * directives.
2138 */
H. Peter Anvin76690a12002-04-30 20:52:49 +00002139 if (((istk->conds && !emitting(istk->conds->state)) ||
H. Peter Anvine2c80182005-01-15 22:15:51 +00002140 (istk->mstk && !istk->mstk->in_progress)) && !is_condition(i)) {
2141 return NO_DIRECTIVE_FOUND;
H. Peter Anvineba20a72002-04-30 20:53:55 +00002142 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002143
2144 /*
H. Peter Anvin76690a12002-04-30 20:52:49 +00002145 * If we're defining a macro or reading a %rep block, we should
Victor van den Elzen8f1120f2008-07-16 13:41:37 +02002146 * ignore all directives except for %macro/%imacro (which nest),
2147 * %endm/%endmacro, and (only if we're in a %rep block) %endrep.
2148 * If we're in a %rep block, another %rep nests, so should be let through.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002149 */
2150 if (defining && i != PP_MACRO && i != PP_IMACRO &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002151 i != PP_RMACRO && i != PP_IRMACRO &&
H. Peter Anvine2c80182005-01-15 22:15:51 +00002152 i != PP_ENDMACRO && i != PP_ENDM &&
2153 (defining->name || (i != PP_ENDREP && i != PP_REP))) {
2154 return NO_DIRECTIVE_FOUND;
H. Peter Anvineba20a72002-04-30 20:53:55 +00002155 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002156
Charles Crayned4200be2008-07-12 16:42:33 -07002157 if (defining) {
Keith Kanios852f1ee2009-07-12 00:19:55 -05002158 if (i == PP_MACRO || i == PP_IMACRO ||
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002159 i == PP_RMACRO || i == PP_IRMACRO) {
Charles Crayned4200be2008-07-12 16:42:33 -07002160 nested_mac_count++;
2161 return NO_DIRECTIVE_FOUND;
2162 } else if (nested_mac_count > 0) {
2163 if (i == PP_ENDMACRO) {
2164 nested_mac_count--;
2165 return NO_DIRECTIVE_FOUND;
2166 }
2167 }
2168 if (!defining->name) {
2169 if (i == PP_REP) {
2170 nested_rep_count++;
2171 return NO_DIRECTIVE_FOUND;
2172 } else if (nested_rep_count > 0) {
2173 if (i == PP_ENDREP) {
2174 nested_rep_count--;
2175 return NO_DIRECTIVE_FOUND;
2176 }
2177 }
2178 }
2179 }
2180
H. Peter Anvin4169a472007-09-12 01:29:43 +00002181 switch (i) {
2182 case PP_INVALID:
H. Peter Anvine2c80182005-01-15 22:15:51 +00002183 error(ERR_NONFATAL, "unknown preprocessor directive `%s'",
2184 tline->text);
2185 return NO_DIRECTIVE_FOUND; /* didn't get it */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002186
H. Peter Anvine2c80182005-01-15 22:15:51 +00002187 case PP_STACKSIZE:
2188 /* Directive to tell NASM what the default stack size is. The
2189 * default is for a 16-bit stack, and this can be overriden with
2190 * %stacksize large.
H. Peter Anvine2c80182005-01-15 22:15:51 +00002191 */
2192 tline = tline->next;
2193 if (tline && tline->type == TOK_WHITESPACE)
2194 tline = tline->next;
2195 if (!tline || tline->type != TOK_ID) {
2196 error(ERR_NONFATAL, "`%%stacksize' missing size parameter");
2197 free_tlist(origline);
2198 return DIRECTIVE_FOUND;
2199 }
2200 if (nasm_stricmp(tline->text, "flat") == 0) {
2201 /* All subsequent ARG directives are for a 32-bit stack */
2202 StackSize = 4;
2203 StackPointer = "ebp";
2204 ArgOffset = 8;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002205 LocalOffset = 0;
Charles Crayne7eaf9192007-11-08 22:11:14 -08002206 } else if (nasm_stricmp(tline->text, "flat64") == 0) {
2207 /* All subsequent ARG directives are for a 64-bit stack */
2208 StackSize = 8;
2209 StackPointer = "rbp";
Per Jessen53252e02010-02-11 00:16:59 +03002210 ArgOffset = 16;
Charles Crayne7eaf9192007-11-08 22:11:14 -08002211 LocalOffset = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002212 } else if (nasm_stricmp(tline->text, "large") == 0) {
2213 /* All subsequent ARG directives are for a 16-bit stack,
2214 * far function call.
2215 */
2216 StackSize = 2;
2217 StackPointer = "bp";
2218 ArgOffset = 4;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002219 LocalOffset = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002220 } else if (nasm_stricmp(tline->text, "small") == 0) {
2221 /* All subsequent ARG directives are for a 16-bit stack,
2222 * far function call. We don't support near functions.
2223 */
2224 StackSize = 2;
2225 StackPointer = "bp";
2226 ArgOffset = 6;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002227 LocalOffset = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002228 } else {
2229 error(ERR_NONFATAL, "`%%stacksize' invalid size type");
2230 free_tlist(origline);
2231 return DIRECTIVE_FOUND;
2232 }
2233 free_tlist(origline);
2234 return DIRECTIVE_FOUND;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002235
H. Peter Anvine2c80182005-01-15 22:15:51 +00002236 case PP_ARG:
2237 /* TASM like ARG directive to define arguments to functions, in
2238 * the following form:
2239 *
2240 * ARG arg1:WORD, arg2:DWORD, arg4:QWORD
2241 */
2242 offset = ArgOffset;
2243 do {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00002244 char *arg, directive[256];
H. Peter Anvine2c80182005-01-15 22:15:51 +00002245 int size = StackSize;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002246
H. Peter Anvine2c80182005-01-15 22:15:51 +00002247 /* Find the argument name */
2248 tline = tline->next;
2249 if (tline && tline->type == TOK_WHITESPACE)
2250 tline = tline->next;
2251 if (!tline || tline->type != TOK_ID) {
2252 error(ERR_NONFATAL, "`%%arg' missing argument parameter");
2253 free_tlist(origline);
2254 return DIRECTIVE_FOUND;
2255 }
2256 arg = tline->text;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002257
H. Peter Anvine2c80182005-01-15 22:15:51 +00002258 /* Find the argument size type */
2259 tline = tline->next;
2260 if (!tline || tline->type != TOK_OTHER
2261 || tline->text[0] != ':') {
2262 error(ERR_NONFATAL,
2263 "Syntax error processing `%%arg' directive");
2264 free_tlist(origline);
2265 return DIRECTIVE_FOUND;
2266 }
2267 tline = tline->next;
2268 if (!tline || tline->type != TOK_ID) {
2269 error(ERR_NONFATAL, "`%%arg' missing size type parameter");
2270 free_tlist(origline);
2271 return DIRECTIVE_FOUND;
2272 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002273
H. Peter Anvine2c80182005-01-15 22:15:51 +00002274 /* Allow macro expansion of type parameter */
Keith Kaniosb7a89542007-04-12 02:40:54 +00002275 tt = tokenize(tline->text);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002276 tt = expand_smacro(tt);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002277 size = parse_size(tt->text);
2278 if (!size) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002279 error(ERR_NONFATAL,
2280 "Invalid size type for `%%arg' missing directive");
2281 free_tlist(tt);
2282 free_tlist(origline);
2283 return DIRECTIVE_FOUND;
2284 }
2285 free_tlist(tt);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002286
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002287 /* Round up to even stack slots */
2288 size = ALIGN(size, StackSize);
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002289
H. Peter Anvine2c80182005-01-15 22:15:51 +00002290 /* Now define the macro for the argument */
2291 snprintf(directive, sizeof(directive), "%%define %s (%s+%d)",
2292 arg, StackPointer, offset);
Keith Kaniosb7a89542007-04-12 02:40:54 +00002293 do_directive(tokenize(directive));
H. Peter Anvine2c80182005-01-15 22:15:51 +00002294 offset += size;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002295
H. Peter Anvine2c80182005-01-15 22:15:51 +00002296 /* Move to the next argument in the list */
2297 tline = tline->next;
2298 if (tline && tline->type == TOK_WHITESPACE)
2299 tline = tline->next;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002300 } while (tline && tline->type == TOK_OTHER && tline->text[0] == ',');
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002301 ArgOffset = offset;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002302 free_tlist(origline);
2303 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002304
H. Peter Anvine2c80182005-01-15 22:15:51 +00002305 case PP_LOCAL:
2306 /* TASM like LOCAL directive to define local variables for a
2307 * function, in the following form:
2308 *
2309 * LOCAL local1:WORD, local2:DWORD, local4:QWORD = LocalSize
2310 *
2311 * The '= LocalSize' at the end is ignored by NASM, but is
2312 * required by TASM to define the local parameter size (and used
2313 * by the TASM macro package).
2314 */
2315 offset = LocalOffset;
2316 do {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00002317 char *local, directive[256];
H. Peter Anvine2c80182005-01-15 22:15:51 +00002318 int size = StackSize;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002319
H. Peter Anvine2c80182005-01-15 22:15:51 +00002320 /* Find the argument name */
2321 tline = tline->next;
2322 if (tline && tline->type == TOK_WHITESPACE)
2323 tline = tline->next;
2324 if (!tline || tline->type != TOK_ID) {
2325 error(ERR_NONFATAL,
2326 "`%%local' missing argument parameter");
2327 free_tlist(origline);
2328 return DIRECTIVE_FOUND;
2329 }
2330 local = tline->text;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002331
H. Peter Anvine2c80182005-01-15 22:15:51 +00002332 /* Find the argument size type */
2333 tline = tline->next;
2334 if (!tline || tline->type != TOK_OTHER
2335 || tline->text[0] != ':') {
2336 error(ERR_NONFATAL,
2337 "Syntax error processing `%%local' directive");
2338 free_tlist(origline);
2339 return DIRECTIVE_FOUND;
2340 }
2341 tline = tline->next;
2342 if (!tline || tline->type != TOK_ID) {
2343 error(ERR_NONFATAL,
2344 "`%%local' missing size type parameter");
2345 free_tlist(origline);
2346 return DIRECTIVE_FOUND;
2347 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00002348
H. Peter Anvine2c80182005-01-15 22:15:51 +00002349 /* Allow macro expansion of type parameter */
Keith Kaniosb7a89542007-04-12 02:40:54 +00002350 tt = tokenize(tline->text);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002351 tt = expand_smacro(tt);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002352 size = parse_size(tt->text);
2353 if (!size) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002354 error(ERR_NONFATAL,
2355 "Invalid size type for `%%local' missing directive");
2356 free_tlist(tt);
2357 free_tlist(origline);
2358 return DIRECTIVE_FOUND;
2359 }
2360 free_tlist(tt);
H. Peter Anvin734b1882002-04-30 21:01:08 +00002361
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002362 /* Round up to even stack slots */
2363 size = ALIGN(size, StackSize);
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002364
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002365 offset += size; /* Negative offset, increment before */
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002366
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002367 /* Now define the macro for the argument */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002368 snprintf(directive, sizeof(directive), "%%define %s (%s-%d)",
2369 local, StackPointer, offset);
Keith Kaniosb7a89542007-04-12 02:40:54 +00002370 do_directive(tokenize(directive));
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002371
H. Peter Anvine2c80182005-01-15 22:15:51 +00002372 /* Now define the assign to setup the enter_c macro correctly */
2373 snprintf(directive, sizeof(directive),
2374 "%%assign %%$localsize %%$localsize+%d", size);
Keith Kaniosb7a89542007-04-12 02:40:54 +00002375 do_directive(tokenize(directive));
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002376
H. Peter Anvine2c80182005-01-15 22:15:51 +00002377 /* Move to the next argument in the list */
2378 tline = tline->next;
2379 if (tline && tline->type == TOK_WHITESPACE)
2380 tline = tline->next;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002381 } while (tline && tline->type == TOK_OTHER && tline->text[0] == ',');
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002382 LocalOffset = offset;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002383 free_tlist(origline);
2384 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002385
H. Peter Anvine2c80182005-01-15 22:15:51 +00002386 case PP_CLEAR:
2387 if (tline->next)
H. Peter Anvin917a3492008-09-24 09:14:49 -07002388 error(ERR_WARNING|ERR_PASS1,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002389 "trailing garbage after `%%clear' ignored");
2390 free_macros();
2391 init_macros();
H. Peter Anvine2c80182005-01-15 22:15:51 +00002392 free_tlist(origline);
2393 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002394
H. Peter Anvin418ca702008-05-30 10:42:30 -07002395 case PP_DEPEND:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002396 t = tline->next = expand_smacro(tline->next);
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002397 skip_white_(t);
2398 if (!t || (t->type != TOK_STRING &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002399 t->type != TOK_INTERNAL_STRING)) {
H. Peter Anvin418ca702008-05-30 10:42:30 -07002400 error(ERR_NONFATAL, "`%%depend' expects a file name");
2401 free_tlist(origline);
2402 return DIRECTIVE_FOUND; /* but we did _something_ */
2403 }
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002404 if (t->next)
H. Peter Anvin917a3492008-09-24 09:14:49 -07002405 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvin418ca702008-05-30 10:42:30 -07002406 "trailing garbage after `%%depend' ignored");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002407 p = t->text;
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002408 if (t->type != TOK_INTERNAL_STRING)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002409 nasm_unquote_cstr(p, i);
2410 if (dephead && !in_list(*dephead, p)) {
2411 StrList *sl = nasm_malloc(strlen(p)+1+sizeof sl->next);
2412 sl->next = NULL;
2413 strcpy(sl->str, p);
2414 *deptail = sl;
2415 deptail = &sl->next;
2416 }
2417 free_tlist(origline);
H. Peter Anvin418ca702008-05-30 10:42:30 -07002418 return DIRECTIVE_FOUND;
2419
2420 case PP_INCLUDE:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002421 t = tline->next = expand_smacro(tline->next);
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002422 skip_white_(t);
H. Peter Anvind2456592008-06-19 15:04:18 -07002423
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002424 if (!t || (t->type != TOK_STRING &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002425 t->type != TOK_INTERNAL_STRING)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002426 error(ERR_NONFATAL, "`%%include' expects a file name");
2427 free_tlist(origline);
2428 return DIRECTIVE_FOUND; /* but we did _something_ */
2429 }
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002430 if (t->next)
H. Peter Anvin917a3492008-09-24 09:14:49 -07002431 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +00002432 "trailing garbage after `%%include' ignored");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002433 p = t->text;
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002434 if (t->type != TOK_INTERNAL_STRING)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002435 nasm_unquote_cstr(p, i);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002436 inc = nasm_malloc(sizeof(Include));
2437 inc->next = istk;
2438 inc->conds = NULL;
H. Peter Anvin2b1c3b92008-06-06 10:38:46 -07002439 inc->fp = inc_fopen(p, dephead, &deptail, pass == 0);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002440 if (!inc->fp) {
2441 /* -MG given but file not found */
2442 nasm_free(inc);
2443 } else {
2444 inc->fname = src_set_fname(nasm_strdup(p));
2445 inc->lineno = src_set_linnum(0);
2446 inc->lineinc = 1;
2447 inc->expansion = NULL;
2448 inc->mstk = NULL;
2449 istk = inc;
2450 list->uplevel(LIST_INCLUDE);
2451 }
2452 free_tlist(origline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002453 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002454
H. Peter Anvind2456592008-06-19 15:04:18 -07002455 case PP_USE:
H. Peter Anvinf4ae5ad2008-06-19 18:39:24 -07002456 {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002457 static macros_t *use_pkg;
2458 const char *pkg_macro = NULL;
H. Peter Anvinf4ae5ad2008-06-19 18:39:24 -07002459
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002460 tline = tline->next;
2461 skip_white_(tline);
2462 tline = expand_id(tline);
H. Peter Anvind2456592008-06-19 15:04:18 -07002463
H. Peter Anvin264b7b92008-10-24 16:38:17 -07002464 if (!tline || (tline->type != TOK_STRING &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002465 tline->type != TOK_INTERNAL_STRING &&
2466 tline->type != TOK_ID)) {
H. Peter Anvin926fc402008-06-19 16:26:12 -07002467 error(ERR_NONFATAL, "`%%use' expects a package name");
H. Peter Anvind2456592008-06-19 15:04:18 -07002468 free_tlist(origline);
2469 return DIRECTIVE_FOUND; /* but we did _something_ */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002470 }
H. Peter Anvin264b7b92008-10-24 16:38:17 -07002471 if (tline->next)
H. Peter Anvin917a3492008-09-24 09:14:49 -07002472 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvind2456592008-06-19 15:04:18 -07002473 "trailing garbage after `%%use' ignored");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002474 if (tline->type == TOK_STRING)
2475 nasm_unquote_cstr(tline->text, i);
2476 use_pkg = nasm_stdmac_find_package(tline->text);
2477 if (!use_pkg)
2478 error(ERR_NONFATAL, "unknown `%%use' package: %s", tline->text);
2479 else
2480 pkg_macro = (char *)use_pkg + 1; /* The first string will be <%define>__USE_*__ */
Victor van den Elzen35eb2ea2010-03-10 22:33:48 +01002481 if (use_pkg && ! smacro_defined(NULL, pkg_macro, 0, NULL, true)) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002482 /* Not already included, go ahead and include it */
2483 stdmacpos = use_pkg;
2484 }
2485 free_tlist(origline);
H. Peter Anvind2456592008-06-19 15:04:18 -07002486 return DIRECTIVE_FOUND;
H. Peter Anvinf4ae5ad2008-06-19 18:39:24 -07002487 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002488 case PP_PUSH:
H. Peter Anvine2c80182005-01-15 22:15:51 +00002489 case PP_REPL:
H. Peter Anvin42b56392008-10-24 16:24:21 -07002490 case PP_POP:
H. Peter Anvine2c80182005-01-15 22:15:51 +00002491 tline = tline->next;
2492 skip_white_(tline);
2493 tline = expand_id(tline);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002494 if (tline) {
2495 if (!tok_type_(tline, TOK_ID)) {
2496 error(ERR_NONFATAL, "`%s' expects a context identifier",
2497 pp_directives[i]);
2498 free_tlist(origline);
2499 return DIRECTIVE_FOUND; /* but we did _something_ */
2500 }
2501 if (tline->next)
2502 error(ERR_WARNING|ERR_PASS1,
2503 "trailing garbage after `%s' ignored",
2504 pp_directives[i]);
2505 p = nasm_strdup(tline->text);
2506 } else {
2507 p = NULL; /* Anonymous */
2508 }
H. Peter Anvin42b56392008-10-24 16:24:21 -07002509
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002510 if (i == PP_PUSH) {
2511 ctx = nasm_malloc(sizeof(Context));
2512 ctx->next = cstk;
2513 hash_init(&ctx->localmac, HASH_SMALL);
2514 ctx->name = p;
2515 ctx->number = unique++;
2516 cstk = ctx;
2517 } else {
2518 /* %pop or %repl */
2519 if (!cstk) {
2520 error(ERR_NONFATAL, "`%s': context stack is empty",
2521 pp_directives[i]);
2522 } else if (i == PP_POP) {
2523 if (p && (!cstk->name || nasm_stricmp(p, cstk->name)))
2524 error(ERR_NONFATAL, "`%%pop' in wrong context: %s, "
2525 "expected %s",
2526 cstk->name ? cstk->name : "anonymous", p);
2527 else
2528 ctx_pop();
2529 } else {
2530 /* i == PP_REPL */
2531 nasm_free(cstk->name);
2532 cstk->name = p;
2533 p = NULL;
2534 }
2535 nasm_free(p);
2536 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002537 free_tlist(origline);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002538 return DIRECTIVE_FOUND;
H. Peter Anvin8e3f75e2008-09-24 00:21:58 -07002539 case PP_FATAL:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002540 severity = ERR_FATAL;
2541 goto issue_error;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002542 case PP_ERROR:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002543 severity = ERR_NONFATAL;
2544 goto issue_error;
H. Peter Anvin7df04172008-06-10 18:27:38 -07002545 case PP_WARNING:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002546 severity = ERR_WARNING|ERR_WARN_USER;
2547 goto issue_error;
H. Peter Anvin8e3f75e2008-09-24 00:21:58 -07002548
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002549issue_error:
H. Peter Anvin7df04172008-06-10 18:27:38 -07002550 {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002551 /* Only error out if this is the final pass */
2552 if (pass != 2 && i != PP_FATAL)
2553 return DIRECTIVE_FOUND;
2554
2555 tline->next = expand_smacro(tline->next);
2556 tline = tline->next;
2557 skip_white_(tline);
2558 t = tline ? tline->next : NULL;
2559 skip_white_(t);
2560 if (tok_type_(tline, TOK_STRING) && !t) {
2561 /* The line contains only a quoted string */
2562 p = tline->text;
2563 nasm_unquote(p, NULL); /* Ignore NUL character truncation */
2564 error(severity, "%s", p);
2565 } else {
2566 /* Not a quoted string, or more than a quoted string */
2567 p = detoken(tline, false);
2568 error(severity, "%s", p);
2569 nasm_free(p);
2570 }
2571 free_tlist(origline);
2572 return DIRECTIVE_FOUND;
H. Peter Anvin7df04172008-06-10 18:27:38 -07002573 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00002574
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002575 CASE_PP_IF:
H. Peter Anvine2c80182005-01-15 22:15:51 +00002576 if (istk->conds && !emitting(istk->conds->state))
2577 j = COND_NEVER;
2578 else {
2579 j = if_condition(tline->next, i);
2580 tline->next = NULL; /* it got freed */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002581 j = j < 0 ? COND_NEVER : j ? COND_IF_TRUE : COND_IF_FALSE;
2582 }
2583 cond = nasm_malloc(sizeof(Cond));
2584 cond->next = istk->conds;
2585 cond->state = j;
2586 istk->conds = cond;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002587 if(istk->mstk)
Keith Kanios3c0d91f2009-10-25 13:28:03 -05002588 istk->mstk->condcnt ++;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002589 free_tlist(origline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002590 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002591
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002592 CASE_PP_ELIF:
H. Peter Anvine2c80182005-01-15 22:15:51 +00002593 if (!istk->conds)
H. Peter Anvin4169a472007-09-12 01:29:43 +00002594 error(ERR_FATAL, "`%s': no matching `%%if'", pp_directives[i]);
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002595 switch(istk->conds->state) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002596 case COND_IF_TRUE:
2597 istk->conds->state = COND_DONE;
2598 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002599
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002600 case COND_DONE:
2601 case COND_NEVER:
2602 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002603
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002604 case COND_ELSE_TRUE:
2605 case COND_ELSE_FALSE:
2606 error_precond(ERR_WARNING|ERR_PASS1,
2607 "`%%elif' after `%%else' ignored");
2608 istk->conds->state = COND_NEVER;
2609 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002610
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002611 case COND_IF_FALSE:
2612 /*
2613 * IMPORTANT: In the case of %if, we will already have
2614 * called expand_mmac_params(); however, if we're
2615 * processing an %elif we must have been in a
2616 * non-emitting mode, which would have inhibited
2617 * the normal invocation of expand_mmac_params().
2618 * Therefore, we have to do it explicitly here.
2619 */
2620 j = if_condition(expand_mmac_params(tline->next), i);
2621 tline->next = NULL; /* it got freed */
2622 istk->conds->state =
2623 j < 0 ? COND_NEVER : j ? COND_IF_TRUE : COND_IF_FALSE;
2624 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002625 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002626 free_tlist(origline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002627 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002628
H. Peter Anvine2c80182005-01-15 22:15:51 +00002629 case PP_ELSE:
2630 if (tline->next)
H. Peter Anvin917a3492008-09-24 09:14:49 -07002631 error_precond(ERR_WARNING|ERR_PASS1,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002632 "trailing garbage after `%%else' ignored");
H. Peter Anvine2c80182005-01-15 22:15:51 +00002633 if (!istk->conds)
2634 error(ERR_FATAL, "`%%else': no matching `%%if'");
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002635 switch(istk->conds->state) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002636 case COND_IF_TRUE:
2637 case COND_DONE:
2638 istk->conds->state = COND_ELSE_FALSE;
2639 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002640
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002641 case COND_NEVER:
2642 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002643
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002644 case COND_IF_FALSE:
2645 istk->conds->state = COND_ELSE_TRUE;
2646 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002647
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002648 case COND_ELSE_TRUE:
2649 case COND_ELSE_FALSE:
2650 error_precond(ERR_WARNING|ERR_PASS1,
2651 "`%%else' after `%%else' ignored.");
2652 istk->conds->state = COND_NEVER;
2653 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002654 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002655 free_tlist(origline);
2656 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002657
H. Peter Anvine2c80182005-01-15 22:15:51 +00002658 case PP_ENDIF:
2659 if (tline->next)
H. Peter Anvin917a3492008-09-24 09:14:49 -07002660 error_precond(ERR_WARNING|ERR_PASS1,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002661 "trailing garbage after `%%endif' ignored");
H. Peter Anvine2c80182005-01-15 22:15:51 +00002662 if (!istk->conds)
2663 error(ERR_FATAL, "`%%endif': no matching `%%if'");
2664 cond = istk->conds;
2665 istk->conds = cond->next;
2666 nasm_free(cond);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002667 if(istk->mstk)
Keith Kanios3c0d91f2009-10-25 13:28:03 -05002668 istk->mstk->condcnt --;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002669 free_tlist(origline);
2670 return DIRECTIVE_FOUND;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002671
H. Peter Anvindb8f96e2009-07-15 09:07:29 -04002672 case PP_RMACRO:
2673 case PP_IRMACRO:
H. Peter Anvine2c80182005-01-15 22:15:51 +00002674 case PP_MACRO:
2675 case PP_IMACRO:
H. Peter Anvina26433d2008-07-16 14:40:01 -07002676 if (defining) {
H. Peter Anvindb8f96e2009-07-15 09:07:29 -04002677 error(ERR_FATAL, "`%s': already defining a macro",
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002678 pp_directives[i]);
2679 return DIRECTIVE_FOUND;
2680 }
2681 defining = nasm_malloc(sizeof(MMacro));
2682 defining->max_depth =
2683 (i == PP_RMACRO) || (i == PP_IRMACRO) ? DEADMAN_LIMIT : 0;
2684 defining->casesense = (i == PP_MACRO) || (i == PP_RMACRO);
2685 if (!parse_mmacro_spec(tline, defining, pp_directives[i])) {
2686 nasm_free(defining);
2687 defining = NULL;
2688 return DIRECTIVE_FOUND;
2689 }
H. Peter Anvina26433d2008-07-16 14:40:01 -07002690
H. Peter Anvin166c2472008-05-28 12:28:58 -07002691 mmac = (MMacro *) hash_findix(&mmacros, defining->name);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002692 while (mmac) {
2693 if (!strcmp(mmac->name, defining->name) &&
2694 (mmac->nparam_min <= defining->nparam_max
2695 || defining->plus)
2696 && (defining->nparam_min <= mmac->nparam_max
2697 || mmac->plus)) {
H. Peter Anvin917a3492008-09-24 09:14:49 -07002698 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +00002699 "redefining multi-line macro `%s'", defining->name);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002700 return DIRECTIVE_FOUND;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002701 }
2702 mmac = mmac->next;
2703 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002704 free_tlist(origline);
2705 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002706
H. Peter Anvine2c80182005-01-15 22:15:51 +00002707 case PP_ENDM:
2708 case PP_ENDMACRO:
Victor van den Elzen8f1120f2008-07-16 13:41:37 +02002709 if (! (defining && defining->name)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002710 error(ERR_NONFATAL, "`%s': not defining a macro", tline->text);
2711 return DIRECTIVE_FOUND;
2712 }
Keith Kanios852f1ee2009-07-12 00:19:55 -05002713 mmhead = (MMacro **) hash_findi_add(&mmacros, defining->name);
H. Peter Anvin97a23472007-09-16 17:57:25 -07002714 defining->next = *mmhead;
Keith Kanios852f1ee2009-07-12 00:19:55 -05002715 *mmhead = defining;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002716 defining = NULL;
2717 free_tlist(origline);
2718 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002719
H. Peter Anvin89cee572009-07-15 09:16:54 -04002720 case PP_EXITMACRO:
Keith Kanios852f1ee2009-07-12 00:19:55 -05002721 /*
2722 * We must search along istk->expansion until we hit a
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002723 * macro-end marker for a macro with a name. Then we
Keith Kanios3c0d91f2009-10-25 13:28:03 -05002724 * bypass all lines between exitmacro and endmacro.
Keith Kanios852f1ee2009-07-12 00:19:55 -05002725 */
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04002726 list_for_each(l, istk->expansion)
Keith Kanios852f1ee2009-07-12 00:19:55 -05002727 if (l->finishes && l->finishes->name)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002728 break;
Keith Kanios852f1ee2009-07-12 00:19:55 -05002729
2730 if (l) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002731 /*
2732 * Remove all conditional entries relative to this
2733 * macro invocation. (safe to do in this context)
2734 */
Keith Kanios3c0d91f2009-10-25 13:28:03 -05002735 for ( ; l->finishes->condcnt > 0; l->finishes->condcnt --) {
2736 cond = istk->conds;
2737 istk->conds = cond->next;
2738 nasm_free(cond);
2739 }
2740 istk->expansion = l;
Keith Kanios852f1ee2009-07-12 00:19:55 -05002741 } else {
2742 error(ERR_NONFATAL, "`%%exitmacro' not within `%%macro' block");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002743 }
Keith Kanios852f1ee2009-07-12 00:19:55 -05002744 free_tlist(origline);
2745 return DIRECTIVE_FOUND;
2746
H. Peter Anvina26433d2008-07-16 14:40:01 -07002747 case PP_UNMACRO:
2748 case PP_UNIMACRO:
2749 {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002750 MMacro **mmac_p;
2751 MMacro spec;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002752
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002753 spec.casesense = (i == PP_UNMACRO);
2754 if (!parse_mmacro_spec(tline, &spec, pp_directives[i])) {
2755 return DIRECTIVE_FOUND;
2756 }
H. Peter Anvina26433d2008-07-16 14:40:01 -07002757 mmac_p = (MMacro **) hash_findi(&mmacros, spec.name, NULL);
2758 while (mmac_p && *mmac_p) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002759 mmac = *mmac_p;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002760 if (mmac->casesense == spec.casesense &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002761 !mstrcmp(mmac->name, spec.name, spec.casesense) &&
H. Peter Anvina26433d2008-07-16 14:40:01 -07002762 mmac->nparam_min == spec.nparam_min &&
2763 mmac->nparam_max == spec.nparam_max &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002764 mmac->plus == spec.plus) {
2765 *mmac_p = mmac->next;
2766 free_mmacro(mmac);
2767 } else {
2768 mmac_p = &mmac->next;
2769 }
2770 }
2771 free_tlist(origline);
2772 free_tlist(spec.dlist);
2773 return DIRECTIVE_FOUND;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002774 }
2775
H. Peter Anvine2c80182005-01-15 22:15:51 +00002776 case PP_ROTATE:
2777 if (tline->next && tline->next->type == TOK_WHITESPACE)
2778 tline = tline->next;
H. Peter Anvin89cee572009-07-15 09:16:54 -04002779 if (!tline->next) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002780 free_tlist(origline);
2781 error(ERR_NONFATAL, "`%%rotate' missing rotate count");
2782 return DIRECTIVE_FOUND;
2783 }
2784 t = expand_smacro(tline->next);
2785 tline->next = NULL;
2786 free_tlist(origline);
2787 tline = t;
2788 tptr = &t;
2789 tokval.t_type = TOKEN_INVALID;
2790 evalresult =
2791 evaluate(ppscan, tptr, &tokval, NULL, pass, error, NULL);
2792 free_tlist(tline);
2793 if (!evalresult)
2794 return DIRECTIVE_FOUND;
2795 if (tokval.t_type)
H. Peter Anvin917a3492008-09-24 09:14:49 -07002796 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +00002797 "trailing garbage after expression ignored");
2798 if (!is_simple(evalresult)) {
2799 error(ERR_NONFATAL, "non-constant value given to `%%rotate'");
2800 return DIRECTIVE_FOUND;
2801 }
2802 mmac = istk->mstk;
2803 while (mmac && !mmac->name) /* avoid mistaking %reps for macros */
2804 mmac = mmac->next_active;
2805 if (!mmac) {
2806 error(ERR_NONFATAL, "`%%rotate' invoked outside a macro call");
2807 } else if (mmac->nparam == 0) {
2808 error(ERR_NONFATAL,
2809 "`%%rotate' invoked within macro without parameters");
2810 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002811 int rotate = mmac->rotate + reloc_value(evalresult);
H. Peter Anvin734b1882002-04-30 21:01:08 +00002812
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002813 rotate %= (int)mmac->nparam;
2814 if (rotate < 0)
2815 rotate += mmac->nparam;
H. Peter Anvin70653092007-10-19 14:42:29 -07002816
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002817 mmac->rotate = rotate;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002818 }
2819 return DIRECTIVE_FOUND;
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00002820
H. Peter Anvine2c80182005-01-15 22:15:51 +00002821 case PP_REP:
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002822 nolist = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002823 do {
2824 tline = tline->next;
2825 } while (tok_type_(tline, TOK_WHITESPACE));
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00002826
H. Peter Anvine2c80182005-01-15 22:15:51 +00002827 if (tok_type_(tline, TOK_ID) &&
2828 nasm_stricmp(tline->text, ".nolist") == 0) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002829 nolist = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002830 do {
2831 tline = tline->next;
2832 } while (tok_type_(tline, TOK_WHITESPACE));
2833 }
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00002834
H. Peter Anvine2c80182005-01-15 22:15:51 +00002835 if (tline) {
2836 t = expand_smacro(tline);
2837 tptr = &t;
2838 tokval.t_type = TOKEN_INVALID;
2839 evalresult =
2840 evaluate(ppscan, tptr, &tokval, NULL, pass, error, NULL);
2841 if (!evalresult) {
2842 free_tlist(origline);
2843 return DIRECTIVE_FOUND;
2844 }
2845 if (tokval.t_type)
H. Peter Anvin917a3492008-09-24 09:14:49 -07002846 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +00002847 "trailing garbage after expression ignored");
2848 if (!is_simple(evalresult)) {
2849 error(ERR_NONFATAL, "non-constant value given to `%%rep'");
2850 return DIRECTIVE_FOUND;
2851 }
H. Peter Anvinf8ba53e2007-10-11 10:11:57 -07002852 count = reloc_value(evalresult) + 1;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002853 } else {
2854 error(ERR_NONFATAL, "`%%rep' expects a repeat count");
H. Peter Anvinf8ba53e2007-10-11 10:11:57 -07002855 count = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002856 }
2857 free_tlist(origline);
H. Peter Anvin734b1882002-04-30 21:01:08 +00002858
H. Peter Anvine2c80182005-01-15 22:15:51 +00002859 tmp_defining = defining;
2860 defining = nasm_malloc(sizeof(MMacro));
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002861 defining->prev = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002862 defining->name = NULL; /* flags this macro as a %rep block */
H. Peter Anvin70055962007-10-11 00:05:31 -07002863 defining->casesense = false;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002864 defining->plus = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002865 defining->nolist = nolist;
H. Peter Anvinf8ba53e2007-10-11 10:11:57 -07002866 defining->in_progress = count;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002867 defining->max_depth = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002868 defining->nparam_min = defining->nparam_max = 0;
2869 defining->defaults = NULL;
2870 defining->dlist = NULL;
2871 defining->expansion = NULL;
2872 defining->next_active = istk->mstk;
2873 defining->rep_nest = tmp_defining;
2874 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002875
H. Peter Anvine2c80182005-01-15 22:15:51 +00002876 case PP_ENDREP:
2877 if (!defining || defining->name) {
2878 error(ERR_NONFATAL, "`%%endrep': no matching `%%rep'");
2879 return DIRECTIVE_FOUND;
2880 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00002881
H. Peter Anvine2c80182005-01-15 22:15:51 +00002882 /*
2883 * Now we have a "macro" defined - although it has no name
2884 * and we won't be entering it in the hash tables - we must
2885 * push a macro-end marker for it on to istk->expansion.
2886 * After that, it will take care of propagating itself (a
2887 * macro-end marker line for a macro which is really a %rep
2888 * block will cause the macro to be re-expanded, complete
2889 * with another macro-end marker to ensure the process
2890 * continues) until the whole expansion is forcibly removed
2891 * from istk->expansion by a %exitrep.
2892 */
2893 l = nasm_malloc(sizeof(Line));
2894 l->next = istk->expansion;
2895 l->finishes = defining;
2896 l->first = NULL;
2897 istk->expansion = l;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002898
H. Peter Anvine2c80182005-01-15 22:15:51 +00002899 istk->mstk = defining;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002900
H. Peter Anvine2c80182005-01-15 22:15:51 +00002901 list->uplevel(defining->nolist ? LIST_MACRO_NOLIST : LIST_MACRO);
2902 tmp_defining = defining;
2903 defining = defining->rep_nest;
2904 free_tlist(origline);
2905 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002906
H. Peter Anvine2c80182005-01-15 22:15:51 +00002907 case PP_EXITREP:
2908 /*
2909 * We must search along istk->expansion until we hit a
2910 * macro-end marker for a macro with no name. Then we set
2911 * its `in_progress' flag to 0.
2912 */
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04002913 list_for_each(l, istk->expansion)
H. Peter Anvine2c80182005-01-15 22:15:51 +00002914 if (l->finishes && !l->finishes->name)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002915 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002916
H. Peter Anvine2c80182005-01-15 22:15:51 +00002917 if (l)
Charles Crayned4200be2008-07-12 16:42:33 -07002918 l->finishes->in_progress = 1;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002919 else
2920 error(ERR_NONFATAL, "`%%exitrep' not within `%%rep' block");
2921 free_tlist(origline);
2922 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002923
H. Peter Anvine2c80182005-01-15 22:15:51 +00002924 case PP_XDEFINE:
2925 case PP_IXDEFINE:
2926 case PP_DEFINE:
2927 case PP_IDEFINE:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002928 casesense = (i == PP_DEFINE || i == PP_XDEFINE);
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07002929
H. Peter Anvine2c80182005-01-15 22:15:51 +00002930 tline = tline->next;
2931 skip_white_(tline);
2932 tline = expand_id(tline);
2933 if (!tline || (tline->type != TOK_ID &&
2934 (tline->type != TOK_PREPROC_ID ||
2935 tline->text[1] != '$'))) {
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07002936 error(ERR_NONFATAL, "`%s' expects a macro identifier",
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002937 pp_directives[i]);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002938 free_tlist(origline);
2939 return DIRECTIVE_FOUND;
2940 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00002941
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08002942 ctx = get_ctx(tline->text, &mname, false);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002943 last = tline;
2944 param_start = tline = tline->next;
2945 nparam = 0;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002946
H. Peter Anvine2c80182005-01-15 22:15:51 +00002947 /* Expand the macro definition now for %xdefine and %ixdefine */
2948 if ((i == PP_XDEFINE) || (i == PP_IXDEFINE))
2949 tline = expand_smacro(tline);
H. Peter Anvin734b1882002-04-30 21:01:08 +00002950
H. Peter Anvine2c80182005-01-15 22:15:51 +00002951 if (tok_is_(tline, "(")) {
2952 /*
2953 * This macro has parameters.
2954 */
H. Peter Anvin734b1882002-04-30 21:01:08 +00002955
H. Peter Anvine2c80182005-01-15 22:15:51 +00002956 tline = tline->next;
2957 while (1) {
2958 skip_white_(tline);
2959 if (!tline) {
2960 error(ERR_NONFATAL, "parameter identifier expected");
2961 free_tlist(origline);
2962 return DIRECTIVE_FOUND;
2963 }
2964 if (tline->type != TOK_ID) {
2965 error(ERR_NONFATAL,
2966 "`%s': parameter identifier expected",
2967 tline->text);
2968 free_tlist(origline);
2969 return DIRECTIVE_FOUND;
2970 }
2971 tline->type = TOK_SMAC_PARAM + nparam++;
2972 tline = tline->next;
2973 skip_white_(tline);
2974 if (tok_is_(tline, ",")) {
2975 tline = tline->next;
H. Peter Anvinca348b62008-07-23 10:49:26 -04002976 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002977 if (!tok_is_(tline, ")")) {
2978 error(ERR_NONFATAL,
2979 "`)' expected to terminate macro template");
2980 free_tlist(origline);
2981 return DIRECTIVE_FOUND;
2982 }
2983 break;
2984 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002985 }
2986 last = tline;
2987 tline = tline->next;
2988 }
2989 if (tok_type_(tline, TOK_WHITESPACE))
2990 last = tline, tline = tline->next;
2991 macro_start = NULL;
2992 last->next = NULL;
2993 t = tline;
2994 while (t) {
2995 if (t->type == TOK_ID) {
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04002996 list_for_each(tt, param_start)
H. Peter Anvine2c80182005-01-15 22:15:51 +00002997 if (tt->type >= TOK_SMAC_PARAM &&
2998 !strcmp(tt->text, t->text))
2999 t->type = tt->type;
3000 }
3001 tt = t->next;
3002 t->next = macro_start;
3003 macro_start = t;
3004 t = tt;
3005 }
3006 /*
3007 * Good. We now have a macro name, a parameter count, and a
3008 * token list (in reverse order) for an expansion. We ought
3009 * to be OK just to create an SMacro, store it, and let
3010 * free_tlist have the rest of the line (which we have
3011 * carefully re-terminated after chopping off the expansion
3012 * from the end).
3013 */
H. Peter Anvin4db5a162007-10-11 13:42:09 -07003014 define_smacro(ctx, mname, casesense, nparam, macro_start);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003015 free_tlist(origline);
3016 return DIRECTIVE_FOUND;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003017
H. Peter Anvine2c80182005-01-15 22:15:51 +00003018 case PP_UNDEF:
3019 tline = tline->next;
3020 skip_white_(tline);
3021 tline = expand_id(tline);
3022 if (!tline || (tline->type != TOK_ID &&
3023 (tline->type != TOK_PREPROC_ID ||
3024 tline->text[1] != '$'))) {
3025 error(ERR_NONFATAL, "`%%undef' expects a macro identifier");
3026 free_tlist(origline);
3027 return DIRECTIVE_FOUND;
3028 }
3029 if (tline->next) {
H. Peter Anvin917a3492008-09-24 09:14:49 -07003030 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +00003031 "trailing garbage after macro name ignored");
3032 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003033
H. Peter Anvine2c80182005-01-15 22:15:51 +00003034 /* Find the context that symbol belongs to */
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08003035 ctx = get_ctx(tline->text, &mname, false);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003036 undef_smacro(ctx, mname);
3037 free_tlist(origline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003038 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003039
H. Peter Anvin9e200162008-06-04 17:23:14 -07003040 case PP_DEFSTR:
3041 case PP_IDEFSTR:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003042 casesense = (i == PP_DEFSTR);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003043
3044 tline = tline->next;
3045 skip_white_(tline);
3046 tline = expand_id(tline);
3047 if (!tline || (tline->type != TOK_ID &&
3048 (tline->type != TOK_PREPROC_ID ||
3049 tline->text[1] != '$'))) {
3050 error(ERR_NONFATAL, "`%s' expects a macro identifier",
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003051 pp_directives[i]);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003052 free_tlist(origline);
3053 return DIRECTIVE_FOUND;
3054 }
3055
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08003056 ctx = get_ctx(tline->text, &mname, false);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003057 last = tline;
3058 tline = expand_smacro(tline->next);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003059 last->next = NULL;
H. Peter Anvin9e200162008-06-04 17:23:14 -07003060
3061 while (tok_type_(tline, TOK_WHITESPACE))
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003062 tline = delete_Token(tline);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003063
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003064 p = detoken(tline, false);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003065 macro_start = nasm_malloc(sizeof(*macro_start));
3066 macro_start->next = NULL;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003067 macro_start->text = nasm_quote(p, strlen(p));
3068 macro_start->type = TOK_STRING;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003069 macro_start->a.mac = NULL;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003070 nasm_free(p);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003071
3072 /*
3073 * We now have a macro name, an implicit parameter count of
3074 * zero, and a string token to use as an expansion. Create
3075 * and store an SMacro.
3076 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003077 define_smacro(ctx, mname, casesense, 0, macro_start);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003078 free_tlist(origline);
3079 return DIRECTIVE_FOUND;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003080
H. Peter Anvin2f55bda2009-07-14 15:04:04 -04003081 case PP_DEFTOK:
3082 case PP_IDEFTOK:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003083 casesense = (i == PP_DEFTOK);
3084
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003085 tline = tline->next;
3086 skip_white_(tline);
3087 tline = expand_id(tline);
3088 if (!tline || (tline->type != TOK_ID &&
3089 (tline->type != TOK_PREPROC_ID ||
3090 tline->text[1] != '$'))) {
3091 error(ERR_NONFATAL,
3092 "`%s' expects a macro identifier as first parameter",
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003093 pp_directives[i]);
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003094 free_tlist(origline);
3095 return DIRECTIVE_FOUND;
3096 }
3097 ctx = get_ctx(tline->text, &mname, false);
3098 last = tline;
3099 tline = expand_smacro(tline->next);
3100 last->next = NULL;
3101
3102 t = tline;
3103 while (tok_type_(t, TOK_WHITESPACE))
3104 t = t->next;
3105 /* t should now point to the string */
3106 if (t->type != TOK_STRING) {
3107 error(ERR_NONFATAL,
3108 "`%s` requires string as second parameter",
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003109 pp_directives[i]);
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003110 free_tlist(tline);
3111 free_tlist(origline);
3112 return DIRECTIVE_FOUND;
3113 }
3114
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003115 nasm_unquote_cstr(t->text, i);
3116 macro_start = tokenize(t->text);
3117
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003118 /*
3119 * We now have a macro name, an implicit parameter count of
3120 * zero, and a numeric token to use as an expansion. Create
3121 * and store an SMacro.
3122 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003123 define_smacro(ctx, mname, casesense, 0, macro_start);
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003124 free_tlist(tline);
3125 free_tlist(origline);
3126 return DIRECTIVE_FOUND;
H. Peter Anvin9e200162008-06-04 17:23:14 -07003127
H. Peter Anvin418ca702008-05-30 10:42:30 -07003128 case PP_PATHSEARCH:
3129 {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003130 FILE *fp;
3131 StrList *xsl = NULL;
3132 StrList **xst = &xsl;
H. Peter Anvin418ca702008-05-30 10:42:30 -07003133
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003134 casesense = true;
H. Peter Anvin418ca702008-05-30 10:42:30 -07003135
3136 tline = tline->next;
3137 skip_white_(tline);
3138 tline = expand_id(tline);
3139 if (!tline || (tline->type != TOK_ID &&
3140 (tline->type != TOK_PREPROC_ID ||
3141 tline->text[1] != '$'))) {
3142 error(ERR_NONFATAL,
3143 "`%%pathsearch' expects a macro identifier as first parameter");
3144 free_tlist(origline);
3145 return DIRECTIVE_FOUND;
3146 }
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08003147 ctx = get_ctx(tline->text, &mname, false);
H. Peter Anvin418ca702008-05-30 10:42:30 -07003148 last = tline;
3149 tline = expand_smacro(tline->next);
3150 last->next = NULL;
3151
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003152 t = tline;
H. Peter Anvin418ca702008-05-30 10:42:30 -07003153 while (tok_type_(t, TOK_WHITESPACE))
3154 t = t->next;
3155
3156 if (!t || (t->type != TOK_STRING &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003157 t->type != TOK_INTERNAL_STRING)) {
H. Peter Anvin418ca702008-05-30 10:42:30 -07003158 error(ERR_NONFATAL, "`%%pathsearch' expects a file name");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003159 free_tlist(tline);
H. Peter Anvin418ca702008-05-30 10:42:30 -07003160 free_tlist(origline);
3161 return DIRECTIVE_FOUND; /* but we did _something_ */
3162 }
3163 if (t->next)
H. Peter Anvin917a3492008-09-24 09:14:49 -07003164 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvin418ca702008-05-30 10:42:30 -07003165 "trailing garbage after `%%pathsearch' ignored");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003166 p = t->text;
H. Peter Anvin427cc912008-06-01 21:43:03 -07003167 if (t->type != TOK_INTERNAL_STRING)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003168 nasm_unquote(p, NULL);
H. Peter Anvin418ca702008-05-30 10:42:30 -07003169
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003170 fp = inc_fopen(p, &xsl, &xst, true);
3171 if (fp) {
3172 p = xsl->str;
3173 fclose(fp); /* Don't actually care about the file */
3174 }
H. Peter Anvin418ca702008-05-30 10:42:30 -07003175 macro_start = nasm_malloc(sizeof(*macro_start));
3176 macro_start->next = NULL;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003177 macro_start->text = nasm_quote(p, strlen(p));
3178 macro_start->type = TOK_STRING;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003179 macro_start->a.mac = NULL;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003180 if (xsl)
3181 nasm_free(xsl);
H. Peter Anvin418ca702008-05-30 10:42:30 -07003182
3183 /*
3184 * We now have a macro name, an implicit parameter count of
3185 * zero, and a string token to use as an expansion. Create
3186 * and store an SMacro.
3187 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003188 define_smacro(ctx, mname, casesense, 0, macro_start);
3189 free_tlist(tline);
H. Peter Anvin418ca702008-05-30 10:42:30 -07003190 free_tlist(origline);
3191 return DIRECTIVE_FOUND;
3192 }
3193
H. Peter Anvine2c80182005-01-15 22:15:51 +00003194 case PP_STRLEN:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003195 casesense = true;
H. Peter Anvin70653092007-10-19 14:42:29 -07003196
H. Peter Anvine2c80182005-01-15 22:15:51 +00003197 tline = tline->next;
3198 skip_white_(tline);
3199 tline = expand_id(tline);
3200 if (!tline || (tline->type != TOK_ID &&
3201 (tline->type != TOK_PREPROC_ID ||
3202 tline->text[1] != '$'))) {
3203 error(ERR_NONFATAL,
3204 "`%%strlen' expects a macro identifier as first parameter");
3205 free_tlist(origline);
3206 return DIRECTIVE_FOUND;
3207 }
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08003208 ctx = get_ctx(tline->text, &mname, false);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003209 last = tline;
3210 tline = expand_smacro(tline->next);
3211 last->next = NULL;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003212
H. Peter Anvine2c80182005-01-15 22:15:51 +00003213 t = tline;
3214 while (tok_type_(t, TOK_WHITESPACE))
3215 t = t->next;
3216 /* t should now point to the string */
3217 if (t->type != TOK_STRING) {
3218 error(ERR_NONFATAL,
3219 "`%%strlen` requires string as second parameter");
3220 free_tlist(tline);
3221 free_tlist(origline);
3222 return DIRECTIVE_FOUND;
3223 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003224
H. Peter Anvine2c80182005-01-15 22:15:51 +00003225 macro_start = nasm_malloc(sizeof(*macro_start));
3226 macro_start->next = NULL;
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07003227 make_tok_num(macro_start, nasm_unquote(t->text, NULL));
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003228 macro_start->a.mac = NULL;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003229
H. Peter Anvine2c80182005-01-15 22:15:51 +00003230 /*
3231 * We now have a macro name, an implicit parameter count of
3232 * zero, and a numeric token to use as an expansion. Create
3233 * and store an SMacro.
3234 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003235 define_smacro(ctx, mname, casesense, 0, macro_start);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003236 free_tlist(tline);
3237 free_tlist(origline);
3238 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003239
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003240 case PP_STRCAT:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003241 casesense = true;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003242
3243 tline = tline->next;
3244 skip_white_(tline);
3245 tline = expand_id(tline);
3246 if (!tline || (tline->type != TOK_ID &&
3247 (tline->type != TOK_PREPROC_ID ||
3248 tline->text[1] != '$'))) {
3249 error(ERR_NONFATAL,
3250 "`%%strcat' expects a macro identifier as first parameter");
3251 free_tlist(origline);
3252 return DIRECTIVE_FOUND;
3253 }
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08003254 ctx = get_ctx(tline->text, &mname, false);
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003255 last = tline;
3256 tline = expand_smacro(tline->next);
3257 last->next = NULL;
3258
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003259 len = 0;
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04003260 list_for_each(t, tline) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003261 switch (t->type) {
3262 case TOK_WHITESPACE:
3263 break;
3264 case TOK_STRING:
3265 len += t->a.len = nasm_unquote(t->text, NULL);
3266 break;
3267 case TOK_OTHER:
3268 if (!strcmp(t->text, ",")) /* permit comma separators */
3269 break;
3270 /* else fall through */
3271 default:
3272 error(ERR_NONFATAL,
3273 "non-string passed to `%%strcat' (%d)", t->type);
3274 free_tlist(tline);
3275 free_tlist(origline);
3276 return DIRECTIVE_FOUND;
3277 }
3278 }
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003279
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003280 p = pp = nasm_malloc(len);
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04003281 list_for_each(t, tline) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003282 if (t->type == TOK_STRING) {
3283 memcpy(p, t->text, t->a.len);
3284 p += t->a.len;
3285 }
3286 }
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003287
3288 /*
3289 * We now have a macro name, an implicit parameter count of
3290 * zero, and a numeric token to use as an expansion. Create
3291 * and store an SMacro.
3292 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003293 macro_start = new_Token(NULL, TOK_STRING, NULL, 0);
3294 macro_start->text = nasm_quote(pp, len);
3295 nasm_free(pp);
3296 define_smacro(ctx, mname, casesense, 0, macro_start);
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003297 free_tlist(tline);
3298 free_tlist(origline);
3299 return DIRECTIVE_FOUND;
3300
H. Peter Anvine2c80182005-01-15 22:15:51 +00003301 case PP_SUBSTR:
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003302 {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003303 int64_t a1, a2;
3304 size_t len;
H. Peter Anvind2456592008-06-19 15:04:18 -07003305
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003306 casesense = true;
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07003307
H. Peter Anvine2c80182005-01-15 22:15:51 +00003308 tline = tline->next;
3309 skip_white_(tline);
3310 tline = expand_id(tline);
3311 if (!tline || (tline->type != TOK_ID &&
3312 (tline->type != TOK_PREPROC_ID ||
3313 tline->text[1] != '$'))) {
3314 error(ERR_NONFATAL,
3315 "`%%substr' expects a macro identifier as first parameter");
3316 free_tlist(origline);
3317 return DIRECTIVE_FOUND;
3318 }
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08003319 ctx = get_ctx(tline->text, &mname, false);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003320 last = tline;
3321 tline = expand_smacro(tline->next);
3322 last->next = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003323
H. Peter Anvine2c80182005-01-15 22:15:51 +00003324 t = tline->next;
3325 while (tok_type_(t, TOK_WHITESPACE))
3326 t = t->next;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003327
H. Peter Anvine2c80182005-01-15 22:15:51 +00003328 /* t should now point to the string */
3329 if (t->type != TOK_STRING) {
3330 error(ERR_NONFATAL,
3331 "`%%substr` requires string as second parameter");
3332 free_tlist(tline);
3333 free_tlist(origline);
3334 return DIRECTIVE_FOUND;
3335 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003336
H. Peter Anvine2c80182005-01-15 22:15:51 +00003337 tt = t->next;
3338 tptr = &tt;
3339 tokval.t_type = TOKEN_INVALID;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003340 evalresult = evaluate(ppscan, tptr, &tokval, NULL,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003341 pass, error, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003342 if (!evalresult) {
3343 free_tlist(tline);
3344 free_tlist(origline);
3345 return DIRECTIVE_FOUND;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003346 } else if (!is_simple(evalresult)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00003347 error(ERR_NONFATAL, "non-constant value given to `%%substr`");
3348 free_tlist(tline);
3349 free_tlist(origline);
3350 return DIRECTIVE_FOUND;
3351 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003352 a1 = evalresult->value-1;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003353
3354 while (tok_type_(tt, TOK_WHITESPACE))
3355 tt = tt->next;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003356 if (!tt) {
3357 a2 = 1; /* Backwards compatibility: one character */
3358 } else {
3359 tokval.t_type = TOKEN_INVALID;
3360 evalresult = evaluate(ppscan, tptr, &tokval, NULL,
3361 pass, error, NULL);
3362 if (!evalresult) {
3363 free_tlist(tline);
3364 free_tlist(origline);
3365 return DIRECTIVE_FOUND;
3366 } else if (!is_simple(evalresult)) {
3367 error(ERR_NONFATAL, "non-constant value given to `%%substr`");
3368 free_tlist(tline);
3369 free_tlist(origline);
3370 return DIRECTIVE_FOUND;
3371 }
3372 a2 = evalresult->value;
3373 }
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003374
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003375 len = nasm_unquote(t->text, NULL);
3376 if (a2 < 0)
3377 a2 = a2+1+len-a1;
3378 if (a1+a2 > (int64_t)len)
3379 a2 = len-a1;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003380
H. Peter Anvine2c80182005-01-15 22:15:51 +00003381 macro_start = nasm_malloc(sizeof(*macro_start));
3382 macro_start->next = NULL;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003383 macro_start->text = nasm_quote((a1 < 0) ? "" : t->text+a1, a2);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003384 macro_start->type = TOK_STRING;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003385 macro_start->a.mac = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003386
H. Peter Anvine2c80182005-01-15 22:15:51 +00003387 /*
3388 * We now have a macro name, an implicit parameter count of
3389 * zero, and a numeric token to use as an expansion. Create
3390 * and store an SMacro.
3391 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003392 define_smacro(ctx, mname, casesense, 0, macro_start);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003393 free_tlist(tline);
3394 free_tlist(origline);
3395 return DIRECTIVE_FOUND;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003396 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003397
H. Peter Anvine2c80182005-01-15 22:15:51 +00003398 case PP_ASSIGN:
3399 case PP_IASSIGN:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003400 casesense = (i == PP_ASSIGN);
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07003401
H. Peter Anvine2c80182005-01-15 22:15:51 +00003402 tline = tline->next;
3403 skip_white_(tline);
3404 tline = expand_id(tline);
3405 if (!tline || (tline->type != TOK_ID &&
3406 (tline->type != TOK_PREPROC_ID ||
3407 tline->text[1] != '$'))) {
3408 error(ERR_NONFATAL,
3409 "`%%%sassign' expects a macro identifier",
3410 (i == PP_IASSIGN ? "i" : ""));
3411 free_tlist(origline);
3412 return DIRECTIVE_FOUND;
3413 }
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08003414 ctx = get_ctx(tline->text, &mname, false);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003415 last = tline;
3416 tline = expand_smacro(tline->next);
3417 last->next = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003418
H. Peter Anvine2c80182005-01-15 22:15:51 +00003419 t = tline;
3420 tptr = &t;
3421 tokval.t_type = TOKEN_INVALID;
3422 evalresult =
3423 evaluate(ppscan, tptr, &tokval, NULL, pass, error, NULL);
3424 free_tlist(tline);
3425 if (!evalresult) {
3426 free_tlist(origline);
3427 return DIRECTIVE_FOUND;
3428 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003429
H. Peter Anvine2c80182005-01-15 22:15:51 +00003430 if (tokval.t_type)
H. Peter Anvin917a3492008-09-24 09:14:49 -07003431 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +00003432 "trailing garbage after expression ignored");
H. Peter Anvin734b1882002-04-30 21:01:08 +00003433
H. Peter Anvine2c80182005-01-15 22:15:51 +00003434 if (!is_simple(evalresult)) {
3435 error(ERR_NONFATAL,
3436 "non-constant value given to `%%%sassign'",
3437 (i == PP_IASSIGN ? "i" : ""));
3438 free_tlist(origline);
3439 return DIRECTIVE_FOUND;
3440 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003441
H. Peter Anvine2c80182005-01-15 22:15:51 +00003442 macro_start = nasm_malloc(sizeof(*macro_start));
3443 macro_start->next = NULL;
3444 make_tok_num(macro_start, reloc_value(evalresult));
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003445 macro_start->a.mac = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003446
H. Peter Anvine2c80182005-01-15 22:15:51 +00003447 /*
3448 * We now have a macro name, an implicit parameter count of
3449 * zero, and a numeric token to use as an expansion. Create
3450 * and store an SMacro.
3451 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003452 define_smacro(ctx, mname, casesense, 0, macro_start);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003453 free_tlist(origline);
3454 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003455
H. Peter Anvine2c80182005-01-15 22:15:51 +00003456 case PP_LINE:
3457 /*
3458 * Syntax is `%line nnn[+mmm] [filename]'
3459 */
3460 tline = tline->next;
3461 skip_white_(tline);
3462 if (!tok_type_(tline, TOK_NUMBER)) {
3463 error(ERR_NONFATAL, "`%%line' expects line number");
3464 free_tlist(origline);
3465 return DIRECTIVE_FOUND;
3466 }
H. Peter Anvin70055962007-10-11 00:05:31 -07003467 k = readnum(tline->text, &err);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003468 m = 1;
3469 tline = tline->next;
3470 if (tok_is_(tline, "+")) {
3471 tline = tline->next;
3472 if (!tok_type_(tline, TOK_NUMBER)) {
3473 error(ERR_NONFATAL, "`%%line' expects line increment");
3474 free_tlist(origline);
3475 return DIRECTIVE_FOUND;
3476 }
H. Peter Anvin70055962007-10-11 00:05:31 -07003477 m = readnum(tline->text, &err);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003478 tline = tline->next;
3479 }
3480 skip_white_(tline);
3481 src_set_linnum(k);
3482 istk->lineinc = m;
3483 if (tline) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07003484 nasm_free(src_set_fname(detoken(tline, false)));
H. Peter Anvine2c80182005-01-15 22:15:51 +00003485 }
3486 free_tlist(origline);
3487 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003488
H. Peter Anvine2c80182005-01-15 22:15:51 +00003489 default:
3490 error(ERR_FATAL,
3491 "preprocessor directive `%s' not yet implemented",
H. Peter Anvin4169a472007-09-12 01:29:43 +00003492 pp_directives[i]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003493 return DIRECTIVE_FOUND;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003494 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003495}
3496
3497/*
H. Peter Anvin76690a12002-04-30 20:52:49 +00003498 * Ensure that a macro parameter contains a condition code and
3499 * nothing else. Return the condition code index if so, or -1
3500 * otherwise.
3501 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003502static int find_cc(Token * t)
H. Peter Anvineba20a72002-04-30 20:53:55 +00003503{
H. Peter Anvin76690a12002-04-30 20:52:49 +00003504 Token *tt;
3505 int i, j, k, m;
3506
H. Peter Anvin25a99342007-09-22 17:45:45 -07003507 if (!t)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003508 return -1; /* Probably a %+ without a space */
H. Peter Anvin25a99342007-09-22 17:45:45 -07003509
H. Peter Anvineba20a72002-04-30 20:53:55 +00003510 skip_white_(t);
H. Peter Anvin76690a12002-04-30 20:52:49 +00003511 if (t->type != TOK_ID)
H. Peter Anvine2c80182005-01-15 22:15:51 +00003512 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003513 tt = t->next;
H. Peter Anvineba20a72002-04-30 20:53:55 +00003514 skip_white_(tt);
H. Peter Anvin76690a12002-04-30 20:52:49 +00003515 if (tt && (tt->type != TOK_OTHER || strcmp(tt->text, ",")))
H. Peter Anvine2c80182005-01-15 22:15:51 +00003516 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003517
3518 i = -1;
Cyrill Gorcunova7319242010-06-03 22:04:36 +04003519 j = ARRAY_SIZE(conditions);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003520 while (j - i > 1) {
3521 k = (j + i) / 2;
3522 m = nasm_stricmp(t->text, conditions[k]);
3523 if (m == 0) {
3524 i = k;
3525 j = -2;
3526 break;
3527 } else if (m < 0) {
3528 j = k;
3529 } else
3530 i = k;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003531 }
3532 if (j != -2)
H. Peter Anvine2c80182005-01-15 22:15:51 +00003533 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003534 return i;
3535}
3536
H. Peter Anvind784a082009-04-20 14:01:18 -07003537static bool paste_tokens(Token **head, bool handle_paste_tokens)
3538{
3539 Token **tail, *t, *tt;
3540 Token **paste_head;
3541 bool did_paste = false;
3542 char *tmp;
3543
3544 /* Now handle token pasting... */
3545 paste_head = NULL;
3546 tail = head;
3547 while ((t = *tail) && (tt = t->next)) {
3548 switch (t->type) {
3549 case TOK_WHITESPACE:
3550 if (tt->type == TOK_WHITESPACE) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003551 /* Zap adjacent whitespace tokens */
H. Peter Anvind784a082009-04-20 14:01:18 -07003552 t->next = delete_Token(tt);
3553 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003554 /* Do not advance paste_head here */
3555 tail = &t->next;
3556 }
H. Peter Anvind784a082009-04-20 14:01:18 -07003557 break;
3558 case TOK_ID:
H. Peter Anvind784a082009-04-20 14:01:18 -07003559 case TOK_NUMBER:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003560 case TOK_FLOAT:
3561 {
3562 size_t len = 0;
3563 char *tmp, *p;
H. Peter Anvind784a082009-04-20 14:01:18 -07003564
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003565 while (tt && (tt->type == TOK_ID || tt->type == TOK_PREPROC_ID ||
3566 tt->type == TOK_NUMBER || tt->type == TOK_FLOAT ||
3567 tt->type == TOK_OTHER)) {
3568 len += strlen(tt->text);
3569 tt = tt->next;
3570 }
H. Peter Anvind784a082009-04-20 14:01:18 -07003571
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003572 /*
3573 * Now tt points to the first token after
3574 * the potential paste area...
3575 */
3576 if (tt != t->next) {
3577 /* We have at least two tokens... */
3578 len += strlen(t->text);
3579 p = tmp = nasm_malloc(len+1);
H. Peter Anvind784a082009-04-20 14:01:18 -07003580
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003581 while (t != tt) {
3582 strcpy(p, t->text);
3583 p = strchr(p, '\0');
3584 t = delete_Token(t);
3585 }
H. Peter Anvind784a082009-04-20 14:01:18 -07003586
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003587 t = *tail = tokenize(tmp);
3588 nasm_free(tmp);
H. Peter Anvind784a082009-04-20 14:01:18 -07003589
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003590 while (t->next) {
3591 tail = &t->next;
3592 t = t->next;
3593 }
3594 t->next = tt; /* Attach the remaining token chain */
H. Peter Anvind784a082009-04-20 14:01:18 -07003595
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003596 did_paste = true;
3597 }
3598 paste_head = tail;
3599 tail = &t->next;
3600 break;
3601 }
3602 case TOK_PASTE: /* %+ */
3603 if (handle_paste_tokens) {
3604 /* Zap %+ and whitespace tokens to the right */
3605 while (t && (t->type == TOK_WHITESPACE ||
3606 t->type == TOK_PASTE))
3607 t = *tail = delete_Token(t);
3608 if (!paste_head || !t)
3609 break; /* Nothing to paste with */
3610 tail = paste_head;
3611 t = *tail;
3612 tt = t->next;
3613 while (tok_type_(tt, TOK_WHITESPACE))
3614 tt = t->next = delete_Token(tt);
H. Peter Anvind784a082009-04-20 14:01:18 -07003615
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003616 if (tt) {
3617 tmp = nasm_strcat(t->text, tt->text);
3618 delete_Token(t);
3619 tt = delete_Token(tt);
3620 t = *tail = tokenize(tmp);
3621 nasm_free(tmp);
3622 while (t->next) {
3623 tail = &t->next;
3624 t = t->next;
3625 }
3626 t->next = tt; /* Attach the remaining token chain */
3627 did_paste = true;
3628 }
3629 paste_head = tail;
3630 tail = &t->next;
3631 break;
3632 }
3633 /* else fall through */
3634 default:
Cyrill Gorcunovadabc152010-07-10 02:11:41 +04003635 tail = &t->next;
3636 if (!tok_type_(t->next, TOK_WHITESPACE))
3637 paste_head = tail;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003638 break;
H. Peter Anvind784a082009-04-20 14:01:18 -07003639 }
3640 }
3641 return did_paste;
3642}
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003643
3644/*
3645 * expands to a list of tokens from %{x:y}
3646 */
3647static Token *expand_mmac_params_range(MMacro *mac, Token *tline, Token ***last)
3648{
3649 Token *t = tline, **tt, *tm, *head;
3650 char *pos;
3651 int fst, lst, j, i;
3652
3653 pos = strchr(tline->text, ':');
3654 nasm_assert(pos);
3655
3656 lst = atoi(pos + 1);
3657 fst = atoi(tline->text + 1);
3658
3659 /*
3660 * only macros params are accounted so
3661 * if someone passes %0 -- we reject such
3662 * value(s)
3663 */
3664 if (lst == 0 || fst == 0)
3665 goto err;
3666
3667 /* the values should be sane */
Cyrill Gorcunov2f403752010-06-05 10:47:10 +04003668 if ((fst > (int)mac->nparam || fst < (-(int)mac->nparam)) ||
3669 (lst > (int)mac->nparam || lst < (-(int)mac->nparam)))
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003670 goto err;
3671
Cyrill Gorcunovefb35832010-06-20 01:52:19 +04003672 fst = fst < 0 ? fst + (int)mac->nparam + 1: fst;
3673 lst = lst < 0 ? lst + (int)mac->nparam + 1: lst;
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003674
3675 /* counted from zero */
3676 fst--, lst--;
3677
3678 /*
3679 * it will be at least one token
3680 */
3681 tm = mac->params[(fst + mac->rotate) % mac->nparam];
3682 t = new_Token(NULL, tm->type, tm->text, 0);
3683 head = t, tt = &t->next;
3684 if (fst < lst) {
3685 for (i = fst + 1; i <= lst; i++) {
3686 t = new_Token(NULL, TOK_OTHER, ",", 0);
3687 *tt = t, tt = &t->next;
3688 j = (i + mac->rotate) % mac->nparam;
3689 tm = mac->params[j];
3690 t = new_Token(NULL, tm->type, tm->text, 0);
3691 *tt = t, tt = &t->next;
3692 }
3693 } else {
3694 for (i = fst - 1; i >= lst; i--) {
3695 t = new_Token(NULL, TOK_OTHER, ",", 0);
3696 *tt = t, tt = &t->next;
3697 j = (i + mac->rotate) % mac->nparam;
3698 tm = mac->params[j];
3699 t = new_Token(NULL, tm->type, tm->text, 0);
3700 *tt = t, tt = &t->next;
3701 }
3702 }
3703
3704 *last = tt;
3705 return head;
3706
3707err:
3708 error(ERR_NONFATAL, "`%%{%s}': macro parameters out of range",
3709 &tline->text[1]);
3710 return tline;
3711}
3712
H. Peter Anvin76690a12002-04-30 20:52:49 +00003713/*
3714 * Expand MMacro-local things: parameter references (%0, %n, %+n,
H. Peter Anvin67c63722008-10-26 23:49:00 -07003715 * %-n) and MMacro-local identifiers (%%foo) as well as
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003716 * macro indirection (%[...]) and range (%{..:..}).
H. Peter Anvin76690a12002-04-30 20:52:49 +00003717 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003718static Token *expand_mmac_params(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00003719{
H. Peter Anvin734b1882002-04-30 21:01:08 +00003720 Token *t, *tt, **tail, *thead;
H. Peter Anvin6125b622009-04-08 14:02:25 -07003721 bool changed = false;
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003722 char *pos;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003723
3724 tail = &thead;
3725 thead = NULL;
3726
H. Peter Anvine2c80182005-01-15 22:15:51 +00003727 while (tline) {
3728 if (tline->type == TOK_PREPROC_ID &&
Cyrill Gorcunovca611192010-06-04 09:22:12 +04003729 (((tline->text[1] == '+' || tline->text[1] == '-') && tline->text[2]) ||
3730 (tline->text[1] >= '0' && tline->text[1] <= '9') ||
3731 tline->text[1] == '%')) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00003732 char *text = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003733 int type = 0, cc; /* type = 0 to placate optimisers */
Keith Kaniosa6dfa782007-04-13 16:47:53 +00003734 char tmpbuf[30];
H. Peter Anvin25a99342007-09-22 17:45:45 -07003735 unsigned int n;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003736 int i;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003737 MMacro *mac;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003738
H. Peter Anvine2c80182005-01-15 22:15:51 +00003739 t = tline;
3740 tline = tline->next;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003741
H. Peter Anvine2c80182005-01-15 22:15:51 +00003742 mac = istk->mstk;
3743 while (mac && !mac->name) /* avoid mistaking %reps for macros */
3744 mac = mac->next_active;
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003745 if (!mac) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00003746 error(ERR_NONFATAL, "`%s': not in a macro call", t->text);
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003747 } else {
3748 pos = strchr(t->text, ':');
3749 if (!pos) {
3750 switch (t->text[1]) {
3751 /*
3752 * We have to make a substitution of one of the
3753 * forms %1, %-1, %+1, %%foo, %0.
3754 */
3755 case '0':
3756 type = TOK_NUMBER;
3757 snprintf(tmpbuf, sizeof(tmpbuf), "%d", mac->nparam);
3758 text = nasm_strdup(tmpbuf);
3759 break;
3760 case '%':
H. Peter Anvine2c80182005-01-15 22:15:51 +00003761 type = TOK_ID;
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003762 snprintf(tmpbuf, sizeof(tmpbuf), "..@%"PRIu64".",
3763 mac->unique);
3764 text = nasm_strcat(tmpbuf, t->text + 2);
3765 break;
3766 case '-':
3767 n = atoi(t->text + 2) - 1;
3768 if (n >= mac->nparam)
3769 tt = NULL;
3770 else {
3771 if (mac->nparam > 1)
3772 n = (n + mac->rotate) % mac->nparam;
3773 tt = mac->params[n];
H. Peter Anvine2c80182005-01-15 22:15:51 +00003774 }
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003775 cc = find_cc(tt);
3776 if (cc == -1) {
3777 error(ERR_NONFATAL,
3778 "macro parameter %d is not a condition code",
3779 n + 1);
3780 text = NULL;
3781 } else {
3782 type = TOK_ID;
3783 if (inverse_ccs[cc] == -1) {
3784 error(ERR_NONFATAL,
3785 "condition code `%s' is not invertible",
3786 conditions[cc]);
3787 text = NULL;
3788 } else
3789 text = nasm_strdup(conditions[inverse_ccs[cc]]);
3790 }
3791 break;
3792 case '+':
3793 n = atoi(t->text + 2) - 1;
3794 if (n >= mac->nparam)
3795 tt = NULL;
3796 else {
3797 if (mac->nparam > 1)
3798 n = (n + mac->rotate) % mac->nparam;
3799 tt = mac->params[n];
3800 }
3801 cc = find_cc(tt);
3802 if (cc == -1) {
3803 error(ERR_NONFATAL,
3804 "macro parameter %d is not a condition code",
3805 n + 1);
3806 text = NULL;
3807 } else {
3808 type = TOK_ID;
3809 text = nasm_strdup(conditions[cc]);
3810 }
3811 break;
3812 default:
3813 n = atoi(t->text + 1) - 1;
3814 if (n >= mac->nparam)
3815 tt = NULL;
3816 else {
3817 if (mac->nparam > 1)
3818 n = (n + mac->rotate) % mac->nparam;
3819 tt = mac->params[n];
3820 }
3821 if (tt) {
3822 for (i = 0; i < mac->paramlen[n]; i++) {
3823 *tail = new_Token(NULL, tt->type, tt->text, 0);
3824 tail = &(*tail)->next;
3825 tt = tt->next;
3826 }
3827 }
3828 text = NULL; /* we've done it here */
3829 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003830 }
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003831 } else {
3832 /*
3833 * seems we have a parameters range here
3834 */
3835 Token *head, **last;
3836 head = expand_mmac_params_range(mac, t, &last);
3837 if (head != t) {
3838 *tail = head;
3839 *last = tline;
3840 tline = head;
3841 text = NULL;
3842 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00003843 }
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003844 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00003845 if (!text) {
3846 delete_Token(t);
3847 } else {
3848 *tail = t;
3849 tail = &t->next;
3850 t->type = type;
3851 nasm_free(t->text);
3852 t->text = text;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003853 t->a.mac = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003854 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003855 changed = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003856 continue;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003857 } else if (tline->type == TOK_INDIRECT) {
3858 t = tline;
3859 tline = tline->next;
3860 tt = tokenize(t->text);
3861 tt = expand_mmac_params(tt);
3862 tt = expand_smacro(tt);
3863 *tail = tt;
3864 while (tt) {
3865 tt->a.mac = NULL; /* Necessary? */
3866 tail = &tt->next;
3867 tt = tt->next;
3868 }
3869 delete_Token(t);
3870 changed = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003871 } else {
3872 t = *tail = tline;
3873 tline = tline->next;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003874 t->a.mac = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003875 tail = &t->next;
3876 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00003877 }
H. Peter Anvineba20a72002-04-30 20:53:55 +00003878 *tail = NULL;
H. Peter Anvin67c63722008-10-26 23:49:00 -07003879
H. Peter Anvind784a082009-04-20 14:01:18 -07003880 if (changed)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003881 paste_tokens(&thead, false);
H. Peter Anvin6125b622009-04-08 14:02:25 -07003882
H. Peter Anvin76690a12002-04-30 20:52:49 +00003883 return thead;
3884}
3885
3886/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003887 * Expand all single-line macro calls made in the given line.
3888 * Return the expanded version of the line. The original is deemed
3889 * to be destroyed in the process. (In reality we'll just move
3890 * Tokens from input to output a lot of the time, rather than
3891 * actually bothering to destroy and replicate.)
3892 */
H. Peter Anvincb1cf592007-11-19 12:26:50 -08003893
H. Peter Anvine2c80182005-01-15 22:15:51 +00003894static Token *expand_smacro(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00003895{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003896 Token *t, *tt, *mstart, **tail, *thead;
H. Peter Anvineba20a72002-04-30 20:53:55 +00003897 SMacro *head = NULL, *m;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003898 Token **params;
3899 int *paramsize;
H. Peter Anvin25a99342007-09-22 17:45:45 -07003900 unsigned int nparam, sparam;
H. Peter Anvind784a082009-04-20 14:01:18 -07003901 int brackets;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003902 Token *org_tline = tline;
3903 Context *ctx;
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08003904 const char *mname;
H. Peter Anvin2a15e692007-11-19 13:14:59 -08003905 int deadman = DEADMAN_LIMIT;
H. Peter Anvin8287daf2009-07-07 16:00:58 -07003906 bool expanded;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003907
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003908 /*
3909 * Trick: we should avoid changing the start token pointer since it can
3910 * be contained in "next" field of other token. Because of this
3911 * we allocate a copy of first token and work with it; at the end of
3912 * routine we copy it back
3913 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003914 if (org_tline) {
Cyrill Gorcunoved4a8052010-04-09 15:40:35 +04003915 tline = new_Token(org_tline->next, org_tline->type,
3916 org_tline->text, 0);
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003917 tline->a.mac = org_tline->a.mac;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003918 nasm_free(org_tline->text);
3919 org_tline->text = NULL;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003920 }
3921
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003922 expanded = true; /* Always expand %+ at least once */
H. Peter Anvin8287daf2009-07-07 16:00:58 -07003923
H. Peter Anvincb1cf592007-11-19 12:26:50 -08003924again:
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003925 thead = NULL;
Cyrill Gorcunoved4a8052010-04-09 15:40:35 +04003926 tail = &thead;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003927
H. Peter Anvine2c80182005-01-15 22:15:51 +00003928 while (tline) { /* main token loop */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003929 if (!--deadman) {
3930 error(ERR_NONFATAL, "interminable macro recursion");
Cyrill Gorcunovbd38c8f2009-11-21 11:11:23 +03003931 goto err;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003932 }
H. Peter Anvincb1cf592007-11-19 12:26:50 -08003933
H. Peter Anvine2c80182005-01-15 22:15:51 +00003934 if ((mname = tline->text)) {
3935 /* if this token is a local macro, look in local context */
Cyrill Gorcunovc56d9ad2010-02-11 15:12:19 +03003936 if (tline->type == TOK_ID) {
3937 head = (SMacro *)hash_findix(&smacros, mname);
3938 } else if (tline->type == TOK_PREPROC_ID) {
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08003939 ctx = get_ctx(mname, &mname, true);
Cyrill Gorcunovc56d9ad2010-02-11 15:12:19 +03003940 head = ctx ? (SMacro *)hash_findix(&ctx->localmac, mname) : NULL;
3941 } else
3942 head = NULL;
H. Peter Anvin072771e2008-05-22 13:17:51 -07003943
H. Peter Anvine2c80182005-01-15 22:15:51 +00003944 /*
3945 * We've hit an identifier. As in is_mmacro below, we first
3946 * check whether the identifier is a single-line macro at
3947 * all, then think about checking for parameters if
3948 * necessary.
3949 */
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04003950 list_for_each(m, head)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003951 if (!mstrcmp(m->name, mname, m->casesense))
3952 break;
3953 if (m) {
3954 mstart = tline;
3955 params = NULL;
3956 paramsize = NULL;
3957 if (m->nparam == 0) {
3958 /*
3959 * Simple case: the macro is parameterless. Discard the
3960 * one token that the macro call took, and push the
3961 * expansion back on the to-do stack.
3962 */
3963 if (!m->expansion) {
3964 if (!strcmp("__FILE__", m->name)) {
3965 int32_t num = 0;
3966 char *file = NULL;
3967 src_get(&num, &file);
3968 tline->text = nasm_quote(file, strlen(file));
3969 tline->type = TOK_STRING;
3970 nasm_free(file);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003971 continue;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003972 }
3973 if (!strcmp("__LINE__", m->name)) {
3974 nasm_free(tline->text);
3975 make_tok_num(tline, src_get_linnum());
3976 continue;
3977 }
3978 if (!strcmp("__BITS__", m->name)) {
3979 nasm_free(tline->text);
3980 make_tok_num(tline, globalbits);
3981 continue;
3982 }
3983 tline = delete_Token(tline);
3984 continue;
3985 }
3986 } else {
3987 /*
3988 * Complicated case: at least one macro with this name
H. Peter Anvine2c80182005-01-15 22:15:51 +00003989 * exists and takes parameters. We must find the
3990 * parameters in the call, count them, find the SMacro
3991 * that corresponds to that form of the macro call, and
3992 * substitute for the parameters when we expand. What a
3993 * pain.
3994 */
3995 /*tline = tline->next;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003996 skip_white_(tline); */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003997 do {
3998 t = tline->next;
3999 while (tok_type_(t, TOK_SMAC_END)) {
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004000 t->a.mac->in_progress = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004001 t->text = NULL;
4002 t = tline->next = delete_Token(t);
4003 }
4004 tline = t;
4005 } while (tok_type_(tline, TOK_WHITESPACE));
4006 if (!tok_is_(tline, "(")) {
4007 /*
4008 * This macro wasn't called with parameters: ignore
4009 * the call. (Behaviour borrowed from gnu cpp.)
4010 */
4011 tline = mstart;
4012 m = NULL;
4013 } else {
4014 int paren = 0;
4015 int white = 0;
4016 brackets = 0;
4017 nparam = 0;
4018 sparam = PARAM_DELTA;
4019 params = nasm_malloc(sparam * sizeof(Token *));
4020 params[0] = tline->next;
4021 paramsize = nasm_malloc(sparam * sizeof(int));
4022 paramsize[0] = 0;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07004023 while (true) { /* parameter loop */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004024 /*
4025 * For some unusual expansions
4026 * which concatenates function call
4027 */
4028 t = tline->next;
4029 while (tok_type_(t, TOK_SMAC_END)) {
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004030 t->a.mac->in_progress = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004031 t->text = NULL;
4032 t = tline->next = delete_Token(t);
4033 }
4034 tline = t;
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00004035
H. Peter Anvine2c80182005-01-15 22:15:51 +00004036 if (!tline) {
4037 error(ERR_NONFATAL,
4038 "macro call expects terminating `)'");
4039 break;
4040 }
4041 if (tline->type == TOK_WHITESPACE
4042 && brackets <= 0) {
4043 if (paramsize[nparam])
4044 white++;
4045 else
4046 params[nparam] = tline->next;
4047 continue; /* parameter loop */
4048 }
4049 if (tline->type == TOK_OTHER
4050 && tline->text[1] == 0) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004051 char ch = tline->text[0];
H. Peter Anvine2c80182005-01-15 22:15:51 +00004052 if (ch == ',' && !paren && brackets <= 0) {
4053 if (++nparam >= sparam) {
4054 sparam += PARAM_DELTA;
4055 params = nasm_realloc(params,
Cyrill Gorcunoved4a8052010-04-09 15:40:35 +04004056 sparam * sizeof(Token *));
4057 paramsize = nasm_realloc(paramsize,
4058 sparam * sizeof(int));
H. Peter Anvine2c80182005-01-15 22:15:51 +00004059 }
4060 params[nparam] = tline->next;
4061 paramsize[nparam] = 0;
4062 white = 0;
4063 continue; /* parameter loop */
4064 }
4065 if (ch == '{' &&
4066 (brackets > 0 || (brackets == 0 &&
4067 !paramsize[nparam])))
4068 {
4069 if (!(brackets++)) {
4070 params[nparam] = tline->next;
4071 continue; /* parameter loop */
4072 }
4073 }
4074 if (ch == '}' && brackets > 0)
4075 if (--brackets == 0) {
4076 brackets = -1;
4077 continue; /* parameter loop */
4078 }
4079 if (ch == '(' && !brackets)
4080 paren++;
4081 if (ch == ')' && brackets <= 0)
4082 if (--paren < 0)
4083 break;
4084 }
4085 if (brackets < 0) {
4086 brackets = 0;
4087 error(ERR_NONFATAL, "braces do not "
4088 "enclose all of macro parameter");
4089 }
4090 paramsize[nparam] += white + 1;
4091 white = 0;
4092 } /* parameter loop */
4093 nparam++;
4094 while (m && (m->nparam != nparam ||
4095 mstrcmp(m->name, mname,
4096 m->casesense)))
4097 m = m->next;
4098 if (!m)
H. Peter Anvin917a3492008-09-24 09:14:49 -07004099 error(ERR_WARNING|ERR_PASS1|ERR_WARN_MNP,
H. Peter Anvine2c80182005-01-15 22:15:51 +00004100 "macro `%s' exists, "
4101 "but not taking %d parameters",
4102 mstart->text, nparam);
4103 }
4104 }
4105 if (m && m->in_progress)
4106 m = NULL;
4107 if (!m) { /* in progess or didn't find '(' or wrong nparam */
H. Peter Anvin70653092007-10-19 14:42:29 -07004108 /*
H. Peter Anvine2c80182005-01-15 22:15:51 +00004109 * Design question: should we handle !tline, which
4110 * indicates missing ')' here, or expand those
4111 * macros anyway, which requires the (t) test a few
H. Peter Anvin70653092007-10-19 14:42:29 -07004112 * lines down?
H. Peter Anvine2c80182005-01-15 22:15:51 +00004113 */
4114 nasm_free(params);
4115 nasm_free(paramsize);
4116 tline = mstart;
4117 } else {
4118 /*
4119 * Expand the macro: we are placed on the last token of the
4120 * call, so that we can easily split the call from the
4121 * following tokens. We also start by pushing an SMAC_END
4122 * token for the cycle removal.
4123 */
4124 t = tline;
4125 if (t) {
4126 tline = t->next;
4127 t->next = NULL;
4128 }
4129 tt = new_Token(tline, TOK_SMAC_END, NULL, 0);
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004130 tt->a.mac = m;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07004131 m->in_progress = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004132 tline = tt;
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04004133 list_for_each(t, m->expansion) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00004134 if (t->type >= TOK_SMAC_PARAM) {
4135 Token *pcopy = tline, **ptail = &pcopy;
4136 Token *ttt, *pt;
4137 int i;
H. Peter Anvin734b1882002-04-30 21:01:08 +00004138
H. Peter Anvine2c80182005-01-15 22:15:51 +00004139 ttt = params[t->type - TOK_SMAC_PARAM];
Cyrill Gorcunoved4a8052010-04-09 15:40:35 +04004140 i = paramsize[t->type - TOK_SMAC_PARAM];
4141 while (--i >= 0) {
4142 pt = *ptail = new_Token(tline, ttt->type,
4143 ttt->text, 0);
H. Peter Anvine2c80182005-01-15 22:15:51 +00004144 ptail = &pt->next;
4145 ttt = ttt->next;
4146 }
4147 tline = pcopy;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004148 } else if (t->type == TOK_PREPROC_Q) {
4149 tt = new_Token(tline, TOK_ID, mname, 0);
4150 tline = tt;
4151 } else if (t->type == TOK_PREPROC_QQ) {
4152 tt = new_Token(tline, TOK_ID, m->name, 0);
4153 tline = tt;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004154 } else {
4155 tt = new_Token(tline, t->type, t->text, 0);
4156 tline = tt;
4157 }
4158 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00004159
H. Peter Anvine2c80182005-01-15 22:15:51 +00004160 /*
4161 * Having done that, get rid of the macro call, and clean
4162 * up the parameters.
4163 */
4164 nasm_free(params);
4165 nasm_free(paramsize);
4166 free_tlist(mstart);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004167 expanded = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004168 continue; /* main token loop */
4169 }
4170 }
4171 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004172
H. Peter Anvine2c80182005-01-15 22:15:51 +00004173 if (tline->type == TOK_SMAC_END) {
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004174 tline->a.mac->in_progress = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004175 tline = delete_Token(tline);
4176 } else {
4177 t = *tail = tline;
4178 tline = tline->next;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004179 t->a.mac = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004180 t->next = NULL;
4181 tail = &t->next;
4182 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004183 }
4184
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004185 /*
4186 * Now scan the entire line and look for successive TOK_IDs that resulted
Keith Kaniosb7a89542007-04-12 02:40:54 +00004187 * after expansion (they can't be produced by tokenize()). The successive
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004188 * TOK_IDs should be concatenated.
4189 * Also we look for %+ tokens and concatenate the tokens before and after
4190 * them (without white spaces in between).
4191 */
H. Peter Anvin8287daf2009-07-07 16:00:58 -07004192 if (expanded && paste_tokens(&thead, true)) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004193 /*
4194 * If we concatenated something, *and* we had previously expanded
4195 * an actual macro, scan the lines again for macros...
4196 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004197 tline = thead;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004198 expanded = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004199 goto again;
H. Peter Anvin734b1882002-04-30 21:01:08 +00004200 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004201
Cyrill Gorcunovbd38c8f2009-11-21 11:11:23 +03004202err:
H. Peter Anvine2c80182005-01-15 22:15:51 +00004203 if (org_tline) {
4204 if (thead) {
4205 *org_tline = *thead;
4206 /* since we just gave text to org_line, don't free it */
4207 thead->text = NULL;
4208 delete_Token(thead);
4209 } else {
4210 /* the expression expanded to empty line;
4211 we can't return NULL for some reasons
4212 we just set the line to a single WHITESPACE token. */
4213 memset(org_tline, 0, sizeof(*org_tline));
4214 org_tline->text = NULL;
4215 org_tline->type = TOK_WHITESPACE;
4216 }
4217 thead = org_tline;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004218 }
4219
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004220 return thead;
4221}
4222
4223/*
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004224 * Similar to expand_smacro but used exclusively with macro identifiers
4225 * right before they are fetched in. The reason is that there can be
4226 * identifiers consisting of several subparts. We consider that if there
4227 * are more than one element forming the name, user wants a expansion,
4228 * otherwise it will be left as-is. Example:
4229 *
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004230 * %define %$abc cde
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004231 *
4232 * the identifier %$abc will be left as-is so that the handler for %define
4233 * will suck it and define the corresponding value. Other case:
4234 *
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004235 * %define _%$abc cde
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004236 *
4237 * In this case user wants name to be expanded *before* %define starts
4238 * working, so we'll expand %$abc into something (if it has a value;
4239 * otherwise it will be left as-is) then concatenate all successive
4240 * PP_IDs into one.
4241 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004242static Token *expand_id(Token * tline)
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004243{
4244 Token *cur, *oldnext = NULL;
4245
H. Peter Anvin734b1882002-04-30 21:01:08 +00004246 if (!tline || !tline->next)
H. Peter Anvine2c80182005-01-15 22:15:51 +00004247 return tline;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004248
4249 cur = tline;
4250 while (cur->next &&
H. Peter Anvine2c80182005-01-15 22:15:51 +00004251 (cur->next->type == TOK_ID ||
4252 cur->next->type == TOK_PREPROC_ID
4253 || cur->next->type == TOK_NUMBER))
4254 cur = cur->next;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004255
4256 /* If identifier consists of just one token, don't expand */
4257 if (cur == tline)
H. Peter Anvine2c80182005-01-15 22:15:51 +00004258 return tline;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004259
H. Peter Anvine2c80182005-01-15 22:15:51 +00004260 if (cur) {
4261 oldnext = cur->next; /* Detach the tail past identifier */
4262 cur->next = NULL; /* so that expand_smacro stops here */
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004263 }
4264
H. Peter Anvin734b1882002-04-30 21:01:08 +00004265 tline = expand_smacro(tline);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004266
H. Peter Anvine2c80182005-01-15 22:15:51 +00004267 if (cur) {
4268 /* expand_smacro possibly changhed tline; re-scan for EOL */
4269 cur = tline;
4270 while (cur && cur->next)
4271 cur = cur->next;
4272 if (cur)
4273 cur->next = oldnext;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004274 }
4275
4276 return tline;
4277}
4278
4279/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004280 * Determine whether the given line constitutes a multi-line macro
4281 * call, and return the MMacro structure called if so. Doesn't have
4282 * to check for an initial label - that's taken care of in
4283 * expand_mmacro - but must check numbers of parameters. Guaranteed
4284 * to be called with tline->type == TOK_ID, so the putative macro
4285 * name is easy to find.
4286 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004287static MMacro *is_mmacro(Token * tline, Token *** params_array)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004288{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004289 MMacro *head, *m;
4290 Token **params;
4291 int nparam;
4292
H. Peter Anvin166c2472008-05-28 12:28:58 -07004293 head = (MMacro *) hash_findix(&mmacros, tline->text);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004294
4295 /*
4296 * Efficiency: first we see if any macro exists with the given
4297 * name. If not, we can return NULL immediately. _Then_ we
4298 * count the parameters, and then we look further along the
4299 * list if necessary to find the proper MMacro.
4300 */
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04004301 list_for_each(m, head)
H. Peter Anvine2c80182005-01-15 22:15:51 +00004302 if (!mstrcmp(m->name, tline->text, m->casesense))
4303 break;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004304 if (!m)
H. Peter Anvine2c80182005-01-15 22:15:51 +00004305 return NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004306
4307 /*
4308 * OK, we have a potential macro. Count and demarcate the
4309 * parameters.
4310 */
H. Peter Anvin734b1882002-04-30 21:01:08 +00004311 count_mmac_params(tline->next, &nparam, &params);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004312
4313 /*
4314 * So we know how many parameters we've got. Find the MMacro
4315 * structure that handles this number.
4316 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004317 while (m) {
4318 if (m->nparam_min <= nparam
4319 && (m->plus || nparam <= m->nparam_max)) {
4320 /*
4321 * This one is right. Just check if cycle removal
4322 * prohibits us using it before we actually celebrate...
4323 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004324 if (m->in_progress > m->max_depth) {
4325 if (m->max_depth > 0) {
4326 error(ERR_WARNING,
4327 "reached maximum recursion depth of %i",
4328 m->max_depth);
4329 }
4330 nasm_free(params);
4331 return NULL;
4332 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00004333 /*
4334 * It's right, and we can use it. Add its default
4335 * parameters to the end of our list if necessary.
4336 */
4337 if (m->defaults && nparam < m->nparam_min + m->ndefs) {
4338 params =
4339 nasm_realloc(params,
4340 ((m->nparam_min + m->ndefs +
4341 1) * sizeof(*params)));
4342 while (nparam < m->nparam_min + m->ndefs) {
4343 params[nparam] = m->defaults[nparam - m->nparam_min];
4344 nparam++;
4345 }
4346 }
4347 /*
4348 * If we've gone over the maximum parameter count (and
4349 * we're in Plus mode), ignore parameters beyond
4350 * nparam_max.
4351 */
4352 if (m->plus && nparam > m->nparam_max)
4353 nparam = m->nparam_max;
4354 /*
4355 * Then terminate the parameter list, and leave.
4356 */
4357 if (!params) { /* need this special case */
4358 params = nasm_malloc(sizeof(*params));
4359 nparam = 0;
4360 }
4361 params[nparam] = NULL;
4362 *params_array = params;
4363 return m;
4364 }
4365 /*
4366 * This one wasn't right: look for the next one with the
4367 * same name.
4368 */
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04004369 list_for_each(m, m->next)
H. Peter Anvine2c80182005-01-15 22:15:51 +00004370 if (!mstrcmp(m->name, tline->text, m->casesense))
4371 break;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004372 }
4373
4374 /*
4375 * After all that, we didn't find one with the right number of
4376 * parameters. Issue a warning, and fail to expand the macro.
4377 */
H. Peter Anvin917a3492008-09-24 09:14:49 -07004378 error(ERR_WARNING|ERR_PASS1|ERR_WARN_MNP,
H. Peter Anvine2c80182005-01-15 22:15:51 +00004379 "macro `%s' exists, but not taking %d parameters",
4380 tline->text, nparam);
H. Peter Anvin734b1882002-04-30 21:01:08 +00004381 nasm_free(params);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004382 return NULL;
4383}
4384
Keith Kanios891775e2009-07-11 06:08:54 -05004385
4386/*
4387 * Save MMacro invocation specific fields in
4388 * preparation for a recursive macro expansion
4389 */
4390static void push_mmacro(MMacro *m)
4391{
4392 MMacroInvocation *i;
4393
H. Peter Anvin89cee572009-07-15 09:16:54 -04004394 i = nasm_malloc(sizeof(MMacroInvocation));
4395 i->prev = m->prev;
4396 i->params = m->params;
4397 i->iline = m->iline;
4398 i->nparam = m->nparam;
4399 i->rotate = m->rotate;
4400 i->paramlen = m->paramlen;
4401 i->unique = m->unique;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004402 i->condcnt = m->condcnt;
H. Peter Anvin89cee572009-07-15 09:16:54 -04004403 m->prev = i;
Keith Kanios891775e2009-07-11 06:08:54 -05004404}
4405
4406
4407/*
4408 * Restore MMacro invocation specific fields that were
4409 * saved during a previous recursive macro expansion
4410 */
4411static void pop_mmacro(MMacro *m)
4412{
4413 MMacroInvocation *i;
4414
H. Peter Anvin89cee572009-07-15 09:16:54 -04004415 if (m->prev) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004416 i = m->prev;
4417 m->prev = i->prev;
4418 m->params = i->params;
4419 m->iline = i->iline;
4420 m->nparam = i->nparam;
4421 m->rotate = i->rotate;
4422 m->paramlen = i->paramlen;
4423 m->unique = i->unique;
4424 m->condcnt = i->condcnt;
4425 nasm_free(i);
H. Peter Anvin89cee572009-07-15 09:16:54 -04004426 }
Keith Kanios891775e2009-07-11 06:08:54 -05004427}
4428
4429
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004430/*
4431 * Expand the multi-line macro call made by the given line, if
4432 * there is one to be expanded. If there is, push the expansion on
H. Peter Anvineba20a72002-04-30 20:53:55 +00004433 * istk->expansion and return 1. Otherwise return 0.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004434 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004435static int expand_mmacro(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004436{
4437 Token *startline = tline;
4438 Token *label = NULL;
4439 int dont_prepend = 0;
H. Peter Anvince2233b2008-05-25 21:57:00 -07004440 Token **params, *t, *mtok, *tt;
H. Peter Anvineba20a72002-04-30 20:53:55 +00004441 MMacro *m;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004442 Line *l, *ll;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004443 int i, nparam, *paramlen;
H. Peter Anvinc751e862008-06-09 10:18:45 -07004444 const char *mname;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004445
4446 t = tline;
H. Peter Anvineba20a72002-04-30 20:53:55 +00004447 skip_white_(t);
H. Peter Anvince2233b2008-05-25 21:57:00 -07004448 /* if (!tok_type_(t, TOK_ID)) Lino 02/25/02 */
H. Peter Anvindce1e2f2002-04-30 21:06:37 +00004449 if (!tok_type_(t, TOK_ID) && !tok_type_(t, TOK_PREPROC_ID))
H. Peter Anvine2c80182005-01-15 22:15:51 +00004450 return 0;
H. Peter Anvince2233b2008-05-25 21:57:00 -07004451 mtok = t;
H. Peter Anvin734b1882002-04-30 21:01:08 +00004452 m = is_mmacro(t, &params);
H. Peter Anvinc751e862008-06-09 10:18:45 -07004453 if (m) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004454 mname = t->text;
H. Peter Anvinc751e862008-06-09 10:18:45 -07004455 } else {
H. Peter Anvine2c80182005-01-15 22:15:51 +00004456 Token *last;
4457 /*
4458 * We have an id which isn't a macro call. We'll assume
4459 * it might be a label; we'll also check to see if a
4460 * colon follows it. Then, if there's another id after
4461 * that lot, we'll check it again for macro-hood.
4462 */
4463 label = last = t;
4464 t = t->next;
4465 if (tok_type_(t, TOK_WHITESPACE))
4466 last = t, t = t->next;
4467 if (tok_is_(t, ":")) {
4468 dont_prepend = 1;
4469 last = t, t = t->next;
4470 if (tok_type_(t, TOK_WHITESPACE))
4471 last = t, t = t->next;
4472 }
H. Peter Anvin89cee572009-07-15 09:16:54 -04004473 if (!tok_type_(t, TOK_ID) || !(m = is_mmacro(t, &params)))
H. Peter Anvine2c80182005-01-15 22:15:51 +00004474 return 0;
4475 last->next = NULL;
Keith Kanios891775e2009-07-11 06:08:54 -05004476 mname = t->text;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004477 tline = t;
H. Peter Anvineba20a72002-04-30 20:53:55 +00004478 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004479
4480 /*
4481 * Fix up the parameters: this involves stripping leading and
4482 * trailing whitespace, then stripping braces if they are
4483 * present.
4484 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004485 for (nparam = 0; params[nparam]; nparam++) ;
H. Peter Anvin734b1882002-04-30 21:01:08 +00004486 paramlen = nparam ? nasm_malloc(nparam * sizeof(*paramlen)) : NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004487
H. Peter Anvine2c80182005-01-15 22:15:51 +00004488 for (i = 0; params[i]; i++) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07004489 int brace = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004490 int comma = (!m->plus || i < nparam - 1);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004491
H. Peter Anvine2c80182005-01-15 22:15:51 +00004492 t = params[i];
4493 skip_white_(t);
4494 if (tok_is_(t, "{"))
H. Peter Anvin6867acc2007-10-10 14:58:45 -07004495 t = t->next, brace = true, comma = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004496 params[i] = t;
4497 paramlen[i] = 0;
4498 while (t) {
4499 if (comma && t->type == TOK_OTHER && !strcmp(t->text, ","))
4500 break; /* ... because we have hit a comma */
4501 if (comma && t->type == TOK_WHITESPACE
4502 && tok_is_(t->next, ","))
4503 break; /* ... or a space then a comma */
4504 if (brace && t->type == TOK_OTHER && !strcmp(t->text, "}"))
4505 break; /* ... or a brace */
4506 t = t->next;
4507 paramlen[i]++;
4508 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004509 }
4510
4511 /*
4512 * OK, we have a MMacro structure together with a set of
4513 * parameters. We must now go through the expansion and push
H. Peter Anvin76690a12002-04-30 20:52:49 +00004514 * copies of each Line on to istk->expansion. Substitution of
4515 * parameter tokens and macro-local tokens doesn't get done
4516 * until the single-line macro substitution process; this is
4517 * because delaying them allows us to change the semantics
4518 * later through %rotate.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004519 *
H. Peter Anvin76690a12002-04-30 20:52:49 +00004520 * First, push an end marker on to istk->expansion, mark this
4521 * macro as in progress, and set up its invocation-specific
4522 * variables.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004523 */
4524 ll = nasm_malloc(sizeof(Line));
4525 ll->next = istk->expansion;
4526 ll->finishes = m;
4527 ll->first = NULL;
4528 istk->expansion = ll;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004529
H. Peter Anvin89cee572009-07-15 09:16:54 -04004530 /*
4531 * Save the previous MMacro expansion in the case of
4532 * macro recursion
4533 */
4534 if (m->max_depth && m->in_progress)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004535 push_mmacro(m);
H. Peter Anvin76690a12002-04-30 20:52:49 +00004536
Keith Kanios891775e2009-07-11 06:08:54 -05004537 m->in_progress ++;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004538 m->params = params;
4539 m->iline = tline;
4540 m->nparam = nparam;
4541 m->rotate = 0;
4542 m->paramlen = paramlen;
4543 m->unique = unique++;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004544 m->lineno = 0;
Keith Kanios3c0d91f2009-10-25 13:28:03 -05004545 m->condcnt = 0;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004546
4547 m->next_active = istk->mstk;
4548 istk->mstk = m;
4549
Cyrill Gorcunov367d59e2010-04-09 15:43:03 +04004550 list_for_each(l, m->expansion) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00004551 Token **tail;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004552
H. Peter Anvine2c80182005-01-15 22:15:51 +00004553 ll = nasm_malloc(sizeof(Line));
4554 ll->finishes = NULL;
4555 ll->next = istk->expansion;
4556 istk->expansion = ll;
4557 tail = &ll->first;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004558
Cyrill Gorcunov367d59e2010-04-09 15:43:03 +04004559 list_for_each(t, l->first) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00004560 Token *x = t;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004561 switch (t->type) {
4562 case TOK_PREPROC_Q:
4563 tt = *tail = new_Token(NULL, TOK_ID, mname, 0);
4564 break;
4565 case TOK_PREPROC_QQ:
4566 tt = *tail = new_Token(NULL, TOK_ID, m->name, 0);
4567 break;
4568 case TOK_PREPROC_ID:
4569 if (t->text[1] == '0' && t->text[2] == '0') {
4570 dont_prepend = -1;
4571 x = label;
4572 if (!x)
4573 continue;
4574 }
4575 /* fall through */
4576 default:
4577 tt = *tail = new_Token(NULL, x->type, x->text, 0);
4578 break;
4579 }
4580 tail = &tt->next;
4581 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00004582 *tail = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004583 }
4584
4585 /*
H. Peter Anvineba20a72002-04-30 20:53:55 +00004586 * If we had a label, push it on as the first line of
4587 * the macro expansion.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004588 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004589 if (label) {
4590 if (dont_prepend < 0)
4591 free_tlist(startline);
4592 else {
4593 ll = nasm_malloc(sizeof(Line));
4594 ll->finishes = NULL;
4595 ll->next = istk->expansion;
4596 istk->expansion = ll;
4597 ll->first = startline;
4598 if (!dont_prepend) {
4599 while (label->next)
4600 label = label->next;
4601 label->next = tt = new_Token(NULL, TOK_OTHER, ":", 0);
4602 }
4603 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004604 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004605
H. Peter Anvin734b1882002-04-30 21:01:08 +00004606 list->uplevel(m->nolist ? LIST_MACRO_NOLIST : LIST_MACRO);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004607
H. Peter Anvineba20a72002-04-30 20:53:55 +00004608 return 1;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004609}
4610
Victor van den Elzen3b404c02008-09-18 13:51:36 +02004611/* The function that actually does the error reporting */
4612static void verror(int severity, const char *fmt, va_list arg)
4613{
4614 char buff[1024];
4615
4616 vsnprintf(buff, sizeof(buff), fmt, arg);
4617
4618 if (istk && istk->mstk && istk->mstk->name)
H. Peter Anvindbb640b2009-07-18 18:57:16 -07004619 nasm_error(severity, "(%s:%d) %s", istk->mstk->name,
Victor van den Elzen3b404c02008-09-18 13:51:36 +02004620 istk->mstk->lineno, buff);
4621 else
H. Peter Anvindbb640b2009-07-18 18:57:16 -07004622 nasm_error(severity, "%s", buff);
Victor van den Elzen3b404c02008-09-18 13:51:36 +02004623}
4624
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004625/*
4626 * Since preprocessor always operate only on the line that didn't
H. Peter Anvin917a3492008-09-24 09:14:49 -07004627 * arrived yet, we should always use ERR_OFFBY1.
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004628 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004629static void error(int severity, const char *fmt, ...)
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004630{
4631 va_list arg;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004632
4633 /* If we're in a dead branch of IF or something like it, ignore the error */
H. Peter Anvin77ba0c62002-05-14 03:18:53 +00004634 if (istk && istk->conds && !emitting(istk->conds->state))
H. Peter Anvine2c80182005-01-15 22:15:51 +00004635 return;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004636
H. Peter Anvin734b1882002-04-30 21:01:08 +00004637 va_start(arg, fmt);
Victor van den Elzen3b404c02008-09-18 13:51:36 +02004638 verror(severity, fmt, arg);
H. Peter Anvin734b1882002-04-30 21:01:08 +00004639 va_end(arg);
Victor van den Elzen3b404c02008-09-18 13:51:36 +02004640}
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004641
Victor van den Elzen3b404c02008-09-18 13:51:36 +02004642/*
4643 * Because %else etc are evaluated in the state context
4644 * of the previous branch, errors might get lost with error():
4645 * %if 0 ... %else trailing garbage ... %endif
4646 * So %else etc should report errors with this function.
4647 */
4648static void error_precond(int severity, const char *fmt, ...)
4649{
4650 va_list arg;
4651
4652 /* Only ignore the error if it's really in a dead branch */
4653 if (istk && istk->conds && istk->conds->state == COND_NEVER)
4654 return;
4655
4656 va_start(arg, fmt);
4657 verror(severity, fmt, arg);
4658 va_end(arg);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004659}
4660
H. Peter Anvin734b1882002-04-30 21:01:08 +00004661static void
H. Peter Anvindbb640b2009-07-18 18:57:16 -07004662pp_reset(char *file, int apass, ListGen * listgen, StrList **deplist)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004663{
H. Peter Anvin7383b402008-09-24 10:20:40 -07004664 Token *t;
4665
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004666 cstk = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004667 istk = nasm_malloc(sizeof(Include));
4668 istk->next = NULL;
4669 istk->conds = NULL;
4670 istk->expansion = NULL;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004671 istk->mstk = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004672 istk->fp = fopen(file, "r");
H. Peter Anvineba20a72002-04-30 20:53:55 +00004673 istk->fname = NULL;
4674 src_set_fname(nasm_strdup(file));
4675 src_set_linnum(0);
4676 istk->lineinc = 1;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004677 if (!istk->fp)
H. Peter Anvin917a3492008-09-24 09:14:49 -07004678 error(ERR_FATAL|ERR_NOFILE, "unable to open input file `%s'",
H. Peter Anvine2c80182005-01-15 22:15:51 +00004679 file);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004680 defining = NULL;
Charles Crayned4200be2008-07-12 16:42:33 -07004681 nested_mac_count = 0;
4682 nested_rep_count = 0;
H. Peter Anvin97a23472007-09-16 17:57:25 -07004683 init_macros();
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004684 unique = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004685 if (tasm_compatible_mode) {
H. Peter Anvina4835d42008-05-20 14:21:29 -07004686 stdmacpos = nasm_stdmac;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004687 } else {
H. Peter Anvina4835d42008-05-20 14:21:29 -07004688 stdmacpos = nasm_stdmac_after_tasm;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004689 }
H. Peter Anvind2456592008-06-19 15:04:18 -07004690 any_extrastdmac = extrastdmac && *extrastdmac;
4691 do_predef = true;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004692 list = listgen;
H. Peter Anvin61f130f2008-09-25 15:45:06 -07004693
4694 /*
4695 * 0 for dependencies, 1 for preparatory passes, 2 for final pass.
4696 * The caller, however, will also pass in 3 for preprocess-only so
4697 * we can set __PASS__ accordingly.
4698 */
4699 pass = apass > 2 ? 2 : apass;
4700
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07004701 dephead = deptail = deplist;
4702 if (deplist) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004703 StrList *sl = nasm_malloc(strlen(file)+1+sizeof sl->next);
4704 sl->next = NULL;
4705 strcpy(sl->str, file);
4706 *deptail = sl;
4707 deptail = &sl->next;
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07004708 }
H. Peter Anvin7383b402008-09-24 10:20:40 -07004709
H. Peter Anvin61f130f2008-09-25 15:45:06 -07004710 /*
4711 * Define the __PASS__ macro. This is defined here unlike
4712 * all the other builtins, because it is special -- it varies between
4713 * passes.
4714 */
H. Peter Anvin7383b402008-09-24 10:20:40 -07004715 t = nasm_malloc(sizeof(*t));
4716 t->next = NULL;
H. Peter Anvin61f130f2008-09-25 15:45:06 -07004717 make_tok_num(t, apass);
H. Peter Anvin7383b402008-09-24 10:20:40 -07004718 t->a.mac = NULL;
4719 define_smacro(NULL, "__PASS__", true, 0, t);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004720}
4721
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004722static char *pp_getline(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004723{
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004724 char *line;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004725 Token *tline;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004726
H. Peter Anvine2c80182005-01-15 22:15:51 +00004727 while (1) {
4728 /*
Keith Kaniosb7a89542007-04-12 02:40:54 +00004729 * Fetch a tokenized line, either from the macro-expansion
H. Peter Anvine2c80182005-01-15 22:15:51 +00004730 * buffer or from the input file.
4731 */
4732 tline = NULL;
4733 while (istk->expansion && istk->expansion->finishes) {
4734 Line *l = istk->expansion;
4735 if (!l->finishes->name && l->finishes->in_progress > 1) {
4736 Line *ll;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004737
H. Peter Anvine2c80182005-01-15 22:15:51 +00004738 /*
4739 * This is a macro-end marker for a macro with no
4740 * name, which means it's not really a macro at all
4741 * but a %rep block, and the `in_progress' field is
4742 * more than 1, meaning that we still need to
4743 * repeat. (1 means the natural last repetition; 0
4744 * means termination by %exitrep.) We have
4745 * therefore expanded up to the %endrep, and must
4746 * push the whole block on to the expansion buffer
4747 * again. We don't bother to remove the macro-end
4748 * marker: we'd only have to generate another one
4749 * if we did.
4750 */
4751 l->finishes->in_progress--;
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04004752 list_for_each(l, l->finishes->expansion) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00004753 Token *t, *tt, **tail;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004754
H. Peter Anvine2c80182005-01-15 22:15:51 +00004755 ll = nasm_malloc(sizeof(Line));
4756 ll->next = istk->expansion;
4757 ll->finishes = NULL;
4758 ll->first = NULL;
4759 tail = &ll->first;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004760
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04004761 list_for_each(t, l->first) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00004762 if (t->text || t->type == TOK_WHITESPACE) {
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04004763 tt = *tail = new_Token(NULL, t->type, t->text, 0);
H. Peter Anvine2c80182005-01-15 22:15:51 +00004764 tail = &tt->next;
4765 }
4766 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00004767
H. Peter Anvine2c80182005-01-15 22:15:51 +00004768 istk->expansion = ll;
4769 }
4770 } else {
4771 /*
4772 * Check whether a `%rep' was started and not ended
4773 * within this macro expansion. This can happen and
4774 * should be detected. It's a fatal error because
4775 * I'm too confused to work out how to recover
4776 * sensibly from it.
4777 */
4778 if (defining) {
4779 if (defining->name)
4780 error(ERR_PANIC,
4781 "defining with name in expansion");
4782 else if (istk->mstk->name)
4783 error(ERR_FATAL,
4784 "`%%rep' without `%%endrep' within"
4785 " expansion of macro `%s'",
4786 istk->mstk->name);
4787 }
H. Peter Anvin87bc6192002-04-30 20:53:16 +00004788
H. Peter Anvine2c80182005-01-15 22:15:51 +00004789 /*
4790 * FIXME: investigate the relationship at this point between
4791 * istk->mstk and l->finishes
4792 */
4793 {
4794 MMacro *m = istk->mstk;
4795 istk->mstk = m->next_active;
4796 if (m->name) {
4797 /*
4798 * This was a real macro call, not a %rep, and
4799 * therefore the parameter information needs to
4800 * be freed.
4801 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004802 if (m->prev) {
4803 pop_mmacro(m);
4804 l->finishes->in_progress --;
4805 } else {
Keith Kanios891775e2009-07-11 06:08:54 -05004806 nasm_free(m->params);
4807 free_tlist(m->iline);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004808 nasm_free(m->paramlen);
4809 l->finishes->in_progress = 0;
4810 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00004811 } else
4812 free_mmacro(m);
4813 }
4814 istk->expansion = l->next;
4815 nasm_free(l);
4816 list->downlevel(LIST_MACRO);
4817 }
4818 }
4819 while (1) { /* until we get a line we can use */
H. Peter Anvineba20a72002-04-30 20:53:55 +00004820
H. Peter Anvine2c80182005-01-15 22:15:51 +00004821 if (istk->expansion) { /* from a macro expansion */
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004822 char *p;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004823 Line *l = istk->expansion;
4824 if (istk->mstk)
4825 istk->mstk->lineno++;
4826 tline = l->first;
4827 istk->expansion = l->next;
4828 nasm_free(l);
H. Peter Anvin6867acc2007-10-10 14:58:45 -07004829 p = detoken(tline, false);
H. Peter Anvine2c80182005-01-15 22:15:51 +00004830 list->line(LIST_MACRO, p);
4831 nasm_free(p);
4832 break;
4833 }
4834 line = read_line();
4835 if (line) { /* from the current input file */
4836 line = prepreproc(line);
Keith Kaniosb7a89542007-04-12 02:40:54 +00004837 tline = tokenize(line);
H. Peter Anvine2c80182005-01-15 22:15:51 +00004838 nasm_free(line);
4839 break;
4840 }
4841 /*
4842 * The current file has ended; work down the istk
4843 */
4844 {
4845 Include *i = istk;
4846 fclose(i->fp);
4847 if (i->conds)
4848 error(ERR_FATAL,
4849 "expected `%%endif' before end of file");
4850 /* only set line and file name if there's a next node */
4851 if (i->next) {
4852 src_set_linnum(i->lineno);
4853 nasm_free(src_set_fname(i->fname));
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004854 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00004855 istk = i->next;
4856 list->downlevel(LIST_INCLUDE);
4857 nasm_free(i);
4858 if (!istk)
4859 return NULL;
Victor van den Elzen4c9d6222008-10-01 13:08:50 +02004860 if (istk->expansion && istk->expansion->finishes)
4861 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004862 }
4863 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004864
H. Peter Anvine2c80182005-01-15 22:15:51 +00004865 /*
4866 * We must expand MMacro parameters and MMacro-local labels
4867 * _before_ we plunge into directive processing, to cope
4868 * with things like `%define something %1' such as STRUC
4869 * uses. Unless we're _defining_ a MMacro, in which case
4870 * those tokens should be left alone to go into the
4871 * definition; and unless we're in a non-emitting
4872 * condition, in which case we don't want to meddle with
4873 * anything.
4874 */
Charles Crayned4200be2008-07-12 16:42:33 -07004875 if (!defining && !(istk->conds && !emitting(istk->conds->state))
H. Peter Anvin992fe752008-10-19 15:45:05 -07004876 && !(istk->mstk && !istk->mstk->in_progress)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00004877 tline = expand_mmac_params(tline);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004878 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00004879
H. Peter Anvine2c80182005-01-15 22:15:51 +00004880 /*
4881 * Check the line to see if it's a preprocessor directive.
4882 */
4883 if (do_directive(tline) == DIRECTIVE_FOUND) {
4884 continue;
4885 } else if (defining) {
4886 /*
4887 * We're defining a multi-line macro. We emit nothing
4888 * at all, and just
Keith Kaniosb7a89542007-04-12 02:40:54 +00004889 * shove the tokenized line on to the macro definition.
H. Peter Anvine2c80182005-01-15 22:15:51 +00004890 */
4891 Line *l = nasm_malloc(sizeof(Line));
4892 l->next = defining->expansion;
4893 l->first = tline;
H. Peter Anvin538002d2008-06-28 18:30:27 -07004894 l->finishes = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004895 defining->expansion = l;
4896 continue;
4897 } else if (istk->conds && !emitting(istk->conds->state)) {
4898 /*
4899 * We're in a non-emitting branch of a condition block.
4900 * Emit nothing at all, not even a blank line: when we
4901 * emerge from the condition we'll give a line-number
4902 * directive so we keep our place correctly.
4903 */
4904 free_tlist(tline);
4905 continue;
4906 } else if (istk->mstk && !istk->mstk->in_progress) {
4907 /*
4908 * We're in a %rep block which has been terminated, so
4909 * we're walking through to the %endrep without
4910 * emitting anything. Emit nothing at all, not even a
4911 * blank line: when we emerge from the %rep block we'll
4912 * give a line-number directive so we keep our place
4913 * correctly.
4914 */
4915 free_tlist(tline);
4916 continue;
4917 } else {
4918 tline = expand_smacro(tline);
4919 if (!expand_mmacro(tline)) {
4920 /*
Keith Kaniosb7a89542007-04-12 02:40:54 +00004921 * De-tokenize the line again, and emit it.
H. Peter Anvine2c80182005-01-15 22:15:51 +00004922 */
H. Peter Anvin6867acc2007-10-10 14:58:45 -07004923 line = detoken(tline, true);
H. Peter Anvine2c80182005-01-15 22:15:51 +00004924 free_tlist(tline);
4925 break;
4926 } else {
4927 continue; /* expand_mmacro calls free_tlist */
4928 }
4929 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004930 }
4931
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004932 return line;
4933}
4934
H. Peter Anvine2c80182005-01-15 22:15:51 +00004935static void pp_cleanup(int pass)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004936{
H. Peter Anvine2c80182005-01-15 22:15:51 +00004937 if (defining) {
H. Peter Anvin89cee572009-07-15 09:16:54 -04004938 if (defining->name) {
Victor van den Elzen8f1120f2008-07-16 13:41:37 +02004939 error(ERR_NONFATAL,
4940 "end of file while still defining macro `%s'",
4941 defining->name);
4942 } else {
4943 error(ERR_NONFATAL, "end of file while still in %%rep");
4944 }
4945
H. Peter Anvine2c80182005-01-15 22:15:51 +00004946 free_mmacro(defining);
Cyrill Gorcunova327c652010-02-14 17:19:38 +03004947 defining = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004948 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004949 while (cstk)
H. Peter Anvine2c80182005-01-15 22:15:51 +00004950 ctx_pop();
H. Peter Anvin97a23472007-09-16 17:57:25 -07004951 free_macros();
H. Peter Anvine2c80182005-01-15 22:15:51 +00004952 while (istk) {
4953 Include *i = istk;
4954 istk = istk->next;
4955 fclose(i->fp);
4956 nasm_free(i->fname);
4957 nasm_free(i);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004958 }
4959 while (cstk)
H. Peter Anvine2c80182005-01-15 22:15:51 +00004960 ctx_pop();
H. Peter Anvin86877b22008-06-20 15:55:45 -07004961 nasm_free(src_set_fname(NULL));
H. Peter Anvine2c80182005-01-15 22:15:51 +00004962 if (pass == 0) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004963 IncPath *i;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004964 free_llist(predef);
4965 delete_Blocks();
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004966 while ((i = ipath)) {
4967 ipath = i->next;
4968 if (i->path)
4969 nasm_free(i->path);
4970 nasm_free(i);
4971 }
H. Peter Anvin11dfa1a2008-07-02 18:11:04 -07004972 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004973}
4974
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004975void pp_include_path(char *path)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004976{
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004977 IncPath *i;
H. Peter Anvin37a321f2007-09-24 13:41:58 -07004978
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004979 i = nasm_malloc(sizeof(IncPath));
H. Peter Anvin37a321f2007-09-24 13:41:58 -07004980 i->path = path ? nasm_strdup(path) : NULL;
Frank Kotlerd0ed6fd2003-08-27 11:33:56 +00004981 i->next = NULL;
4982
H. Peter Anvin89cee572009-07-15 09:16:54 -04004983 if (ipath) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00004984 IncPath *j = ipath;
H. Peter Anvin89cee572009-07-15 09:16:54 -04004985 while (j->next)
H. Peter Anvine2c80182005-01-15 22:15:51 +00004986 j = j->next;
4987 j->next = i;
4988 } else {
4989 ipath = i;
Frank Kotlerd0ed6fd2003-08-27 11:33:56 +00004990 }
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004991}
Frank Kotlerd0ed6fd2003-08-27 11:33:56 +00004992
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004993void pp_pre_include(char *fname)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004994{
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004995 Token *inc, *space, *name;
4996 Line *l;
4997
H. Peter Anvin734b1882002-04-30 21:01:08 +00004998 name = new_Token(NULL, TOK_INTERNAL_STRING, fname, 0);
4999 space = new_Token(name, TOK_WHITESPACE, NULL, 0);
5000 inc = new_Token(space, TOK_PREPROC_ID, "%include", 0);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005001
5002 l = nasm_malloc(sizeof(Line));
5003 l->next = predef;
5004 l->first = inc;
H. Peter Anvin538002d2008-06-28 18:30:27 -07005005 l->finishes = NULL;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005006 predef = l;
5007}
5008
Keith Kaniosa6dfa782007-04-13 16:47:53 +00005009void pp_pre_define(char *definition)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005010{
5011 Token *def, *space;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005012 Line *l;
Keith Kaniosa6dfa782007-04-13 16:47:53 +00005013 char *equals;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005014
5015 equals = strchr(definition, '=');
H. Peter Anvin734b1882002-04-30 21:01:08 +00005016 space = new_Token(NULL, TOK_WHITESPACE, NULL, 0);
5017 def = new_Token(space, TOK_PREPROC_ID, "%define", 0);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005018 if (equals)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005019 *equals = ' ';
Keith Kaniosb7a89542007-04-12 02:40:54 +00005020 space->next = tokenize(definition);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005021 if (equals)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005022 *equals = '=';
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005023
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005024 l = nasm_malloc(sizeof(Line));
5025 l->next = predef;
5026 l->first = def;
H. Peter Anvin538002d2008-06-28 18:30:27 -07005027 l->finishes = NULL;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005028 predef = l;
5029}
5030
Keith Kaniosa6dfa782007-04-13 16:47:53 +00005031void pp_pre_undefine(char *definition)
H. Peter Anvin620515a2002-04-30 20:57:38 +00005032{
5033 Token *def, *space;
5034 Line *l;
5035
H. Peter Anvin734b1882002-04-30 21:01:08 +00005036 space = new_Token(NULL, TOK_WHITESPACE, NULL, 0);
5037 def = new_Token(space, TOK_PREPROC_ID, "%undef", 0);
Keith Kaniosb7a89542007-04-12 02:40:54 +00005038 space->next = tokenize(definition);
H. Peter Anvin620515a2002-04-30 20:57:38 +00005039
5040 l = nasm_malloc(sizeof(Line));
5041 l->next = predef;
5042 l->first = def;
H. Peter Anvin538002d2008-06-28 18:30:27 -07005043 l->finishes = NULL;
H. Peter Anvin620515a2002-04-30 20:57:38 +00005044 predef = l;
5045}
5046
Keith Kaniosb7a89542007-04-12 02:40:54 +00005047/*
5048 * Added by Keith Kanios:
5049 *
5050 * This function is used to assist with "runtime" preprocessor
5051 * directives. (e.g. pp_runtime("%define __BITS__ 64");)
5052 *
5053 * ERRORS ARE IGNORED HERE, SO MAKE COMPLETELY SURE THAT YOU
5054 * PASS A VALID STRING TO THIS FUNCTION!!!!!
5055 */
5056
Keith Kaniosa6dfa782007-04-13 16:47:53 +00005057void pp_runtime(char *definition)
Keith Kaniosb7a89542007-04-12 02:40:54 +00005058{
5059 Token *def;
H. Peter Anvin70653092007-10-19 14:42:29 -07005060
Keith Kaniosb7a89542007-04-12 02:40:54 +00005061 def = tokenize(definition);
H. Peter Anvin89cee572009-07-15 09:16:54 -04005062 if (do_directive(def) == NO_DIRECTIVE_FOUND)
Keith Kaniosb7a89542007-04-12 02:40:54 +00005063 free_tlist(def);
H. Peter Anvin70653092007-10-19 14:42:29 -07005064
Keith Kaniosb7a89542007-04-12 02:40:54 +00005065}
5066
H. Peter Anvina70547f2008-07-19 21:44:26 -07005067void pp_extra_stdmac(macros_t *macros)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005068{
H. Peter Anvin76690a12002-04-30 20:52:49 +00005069 extrastdmac = macros;
5070}
5071
Keith Kaniosa5fc6462007-10-13 07:09:22 -07005072static void make_tok_num(Token * tok, int64_t val)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005073{
Keith Kaniosa6dfa782007-04-13 16:47:53 +00005074 char numbuf[20];
Keith Kaniosa5fc6462007-10-13 07:09:22 -07005075 snprintf(numbuf, sizeof(numbuf), "%"PRId64"", val);
H. Peter Anvineba20a72002-04-30 20:53:55 +00005076 tok->text = nasm_strdup(numbuf);
5077 tok->type = TOK_NUMBER;
5078}
5079
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005080Preproc nasmpp = {
5081 pp_reset,
5082 pp_getline,
5083 pp_cleanup
5084};