blob: 7fc3e74d90df3af790ddf17736e50ae5a2f12b4e [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/*
H. Peter Anvin077fb932010-07-20 14:56:30 -0700471 * nasm_unquote with error if the string contains NUL characters.
472 * If the string contains NUL characters, issue an error and return
473 * the C len, i.e. truncate at the NUL.
474 */
475static size_t nasm_unquote_cstr(char *qstr, enum preproc_token directive)
476{
477 size_t len = nasm_unquote(qstr, NULL);
478 size_t clen = strlen(qstr);
479
480 if (len != clen)
481 error(ERR_NONFATAL, "NUL character in `%s' directive",
482 pp_directives[directive]);
483
484 return clen;
485}
486
487/*
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300488 * Handle TASM specific directives, which do not contain a % in
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000489 * front of them. We do it here because I could not find any other
490 * place to do it for the moment, and it is a hack (ideally it would
491 * be nice to be able to use the NASM pre-processor to do it).
492 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000493static char *check_tasm_directive(char *line)
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000494{
Keith Kaniosb7a89542007-04-12 02:40:54 +0000495 int32_t i, j, k, m, len;
Cyrill Gorcunovf66ac7d2009-10-12 20:41:13 +0400496 char *p, *q, *oldline, oldchar;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000497
Cyrill Gorcunovf66ac7d2009-10-12 20:41:13 +0400498 p = nasm_skip_spaces(line);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000499
500 /* Binary search for the directive name */
501 i = -1;
Cyrill Gorcunova7319242010-06-03 22:04:36 +0400502 j = ARRAY_SIZE(tasm_directives);
Cyrill Gorcunovf66ac7d2009-10-12 20:41:13 +0400503 q = nasm_skip_word(p);
504 len = q - p;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000505 if (len) {
506 oldchar = p[len];
507 p[len] = 0;
508 while (j - i > 1) {
509 k = (j + i) / 2;
510 m = nasm_stricmp(p, tasm_directives[k]);
511 if (m == 0) {
512 /* We have found a directive, so jam a % in front of it
513 * so that NASM will then recognise it as one if it's own.
514 */
515 p[len] = oldchar;
516 len = strlen(p);
517 oldline = line;
518 line = nasm_malloc(len + 2);
519 line[0] = '%';
520 if (k == TM_IFDIFI) {
H. Peter Anvin18f48792009-06-27 15:56:27 -0700521 /*
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300522 * NASM does not recognise IFDIFI, so we convert
523 * it to %if 0. This is not used in NASM
524 * compatible code, but does need to parse for the
525 * TASM macro package.
H. Peter Anvine2c80182005-01-15 22:15:51 +0000526 */
H. Peter Anvin18f48792009-06-27 15:56:27 -0700527 strcpy(line + 1, "if 0");
H. Peter Anvine2c80182005-01-15 22:15:51 +0000528 } else {
529 memcpy(line + 1, p, len + 1);
530 }
531 nasm_free(oldline);
532 return line;
533 } else if (m < 0) {
534 j = k;
535 } else
536 i = k;
537 }
538 p[len] = oldchar;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000539 }
540 return line;
541}
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000542
H. Peter Anvin76690a12002-04-30 20:52:49 +0000543/*
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000544 * The pre-preprocessing stage... This function translates line
545 * number indications as they emerge from GNU cpp (`# lineno "file"
546 * flags') into NASM preprocessor line number indications (`%line
547 * lineno file').
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000548 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000549static char *prepreproc(char *line)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000550{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000551 int lineno, fnlen;
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000552 char *fname, *oldline;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000553
H. Peter Anvine2c80182005-01-15 22:15:51 +0000554 if (line[0] == '#' && line[1] == ' ') {
555 oldline = line;
556 fname = oldline + 2;
557 lineno = atoi(fname);
558 fname += strspn(fname, "0123456789 ");
559 if (*fname == '"')
560 fname++;
561 fnlen = strcspn(fname, "\"");
562 line = nasm_malloc(20 + fnlen);
563 snprintf(line, 20 + fnlen, "%%line %d %.*s", lineno, fnlen, fname);
564 nasm_free(oldline);
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000565 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000566 if (tasm_compatible_mode)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000567 return check_tasm_directive(line);
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000568 return line;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000569}
570
571/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000572 * Free a linked list of tokens.
573 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000574static void free_tlist(Token * list)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000575{
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +0400576 while (list)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000577 list = delete_Token(list);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000578}
579
580/*
581 * Free a linked list of lines.
582 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000583static void free_llist(Line * list)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000584{
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +0400585 Line *l, *tmp;
586 list_for_each_safe(l, tmp, list) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000587 free_tlist(l->first);
588 nasm_free(l);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000589 }
590}
591
592/*
H. Peter Anvineba20a72002-04-30 20:53:55 +0000593 * Free an MMacro
594 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000595static void free_mmacro(MMacro * m)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000596{
H. Peter Anvin734b1882002-04-30 21:01:08 +0000597 nasm_free(m->name);
598 free_tlist(m->dlist);
599 nasm_free(m->defaults);
600 free_llist(m->expansion);
601 nasm_free(m);
H. Peter Anvineba20a72002-04-30 20:53:55 +0000602}
603
604/*
H. Peter Anvin97a23472007-09-16 17:57:25 -0700605 * Free all currently defined macros, and free the hash tables
606 */
H. Peter Anvin072771e2008-05-22 13:17:51 -0700607static void free_smacro_table(struct hash_table *smt)
H. Peter Anvin97a23472007-09-16 17:57:25 -0700608{
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +0400609 SMacro *s, *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
H. Peter Anvin072771e2008-05-22 13:17:51 -0700613 while ((s = hash_iterate(smt, &it, &key)) != NULL) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300614 nasm_free((void *)key);
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +0400615 list_for_each_safe(s, tmp, s) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300616 nasm_free(s->name);
617 free_tlist(s->expansion);
618 nasm_free(s);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300619 }
H. Peter Anvin97a23472007-09-16 17:57:25 -0700620 }
H. Peter Anvin072771e2008-05-22 13:17:51 -0700621 hash_free(smt);
622}
623
624static void free_mmacro_table(struct hash_table *mmt)
625{
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +0400626 MMacro *m, *tmp;
H. Peter Anvin072771e2008-05-22 13:17:51 -0700627 const char *key;
628 struct hash_tbl_node *it = NULL;
H. Peter Anvin97a23472007-09-16 17:57:25 -0700629
630 it = NULL;
H. Peter Anvin072771e2008-05-22 13:17:51 -0700631 while ((m = hash_iterate(mmt, &it, &key)) != NULL) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300632 nasm_free((void *)key);
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +0400633 list_for_each_safe(m ,tmp, m)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300634 free_mmacro(m);
H. Peter Anvin97a23472007-09-16 17:57:25 -0700635 }
H. Peter Anvin072771e2008-05-22 13:17:51 -0700636 hash_free(mmt);
637}
638
639static void free_macros(void)
640{
H. Peter Anvin166c2472008-05-28 12:28:58 -0700641 free_smacro_table(&smacros);
642 free_mmacro_table(&mmacros);
H. Peter Anvin97a23472007-09-16 17:57:25 -0700643}
644
645/*
646 * Initialize the hash tables
647 */
648static void init_macros(void)
649{
H. Peter Anvin166c2472008-05-28 12:28:58 -0700650 hash_init(&smacros, HASH_LARGE);
651 hash_init(&mmacros, HASH_LARGE);
H. Peter Anvin97a23472007-09-16 17:57:25 -0700652}
653
654/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000655 * Pop the context stack.
656 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000657static void ctx_pop(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000658{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000659 Context *c = cstk;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000660
661 cstk = cstk->next;
H. Peter Anvin166c2472008-05-28 12:28:58 -0700662 free_smacro_table(&c->localmac);
H. Peter Anvin734b1882002-04-30 21:01:08 +0000663 nasm_free(c->name);
664 nasm_free(c);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000665}
666
H. Peter Anvin072771e2008-05-22 13:17:51 -0700667/*
668 * Search for a key in the hash index; adding it if necessary
669 * (in which case we initialize the data pointer to NULL.)
670 */
671static void **
672hash_findi_add(struct hash_table *hash, const char *str)
673{
674 struct hash_insert hi;
675 void **r;
676 char *strx;
677
678 r = hash_findi(hash, str, &hi);
679 if (r)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300680 return r;
H. Peter Anvin072771e2008-05-22 13:17:51 -0700681
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300682 strx = nasm_strdup(str); /* Use a more efficient allocator here? */
H. Peter Anvin072771e2008-05-22 13:17:51 -0700683 return hash_add(&hi, strx, NULL);
684}
685
686/*
687 * Like hash_findi, but returns the data element rather than a pointer
688 * to it. Used only when not adding a new element, hence no third
689 * argument.
690 */
691static void *
692hash_findix(struct hash_table *hash, const char *str)
693{
694 void **p;
695
696 p = hash_findi(hash, str, NULL);
697 return p ? *p : NULL;
698}
699
Cyrill Gorcunov15bdc512010-07-13 11:27:41 +0400700/*
701 * read line from standart macros set,
702 * if there no more left -- return NULL
703 */
704static char *line_from_stdmac(void)
705{
706 unsigned char c;
707 const unsigned char *p = stdmacpos;
708 char *line, *q;
709 size_t len = 0;
710
711 if (!stdmacpos)
712 return NULL;
713
714 while ((c = *p++)) {
715 if (c >= 0x80)
716 len += pp_directives_len[c - 0x80] + 1;
717 else
718 len++;
719 }
720
721 line = nasm_malloc(len + 1);
722 q = line;
723 while ((c = *stdmacpos++)) {
724 if (c >= 0x80) {
725 memcpy(q, pp_directives[c - 0x80], pp_directives_len[c - 0x80]);
726 q += pp_directives_len[c - 0x80];
727 *q++ = ' ';
728 } else {
729 *q++ = c;
730 }
731 }
732 stdmacpos = p;
733 *q = '\0';
734
735 if (!*stdmacpos) {
736 /* This was the last of the standard macro chain... */
737 stdmacpos = NULL;
738 if (any_extrastdmac) {
739 stdmacpos = extrastdmac;
740 any_extrastdmac = false;
741 } else if (do_predef) {
742 Line *pd, *l;
743 Token *head, **tail, *t;
744
745 /*
746 * Nasty hack: here we push the contents of
747 * `predef' on to the top-level expansion stack,
748 * since this is the most convenient way to
749 * implement the pre-include and pre-define
750 * features.
751 */
752 list_for_each(pd, predef) {
753 head = NULL;
754 tail = &head;
755 list_for_each(t, pd->first) {
756 *tail = new_Token(NULL, t->type, t->text, 0);
757 tail = &(*tail)->next;
758 }
759
760 l = nasm_malloc(sizeof(Line));
761 l->next = istk->expansion;
762 l->first = head;
763 l->finishes = NULL;
764
765 istk->expansion = l;
766 }
767 do_predef = false;
768 }
769 }
770
771 return line;
772}
773
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000774#define BUF_DELTA 512
775/*
776 * Read a line from the top file in istk, handling multiple CR/LFs
777 * at the end of the line read, and handling spurious ^Zs. Will
778 * return lines from the standard macro set if this has not already
779 * been done.
780 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000781static char *read_line(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000782{
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000783 char *buffer, *p, *q;
H. Peter Anvin9f394642002-04-30 21:07:51 +0000784 int bufsize, continued_count;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000785
Cyrill Gorcunov15bdc512010-07-13 11:27:41 +0400786 /*
787 * standart macros set (predefined) goes first
788 */
789 p = line_from_stdmac();
790 if (p)
791 return p;
H. Peter Anvin72edbb82008-06-19 16:00:04 -0700792
Cyrill Gorcunov15bdc512010-07-13 11:27:41 +0400793 /*
794 * regular read from a file
795 */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000796 bufsize = BUF_DELTA;
797 buffer = nasm_malloc(BUF_DELTA);
798 p = buffer;
H. Peter Anvin9f394642002-04-30 21:07:51 +0000799 continued_count = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000800 while (1) {
801 q = fgets(p, bufsize - (p - buffer), istk->fp);
802 if (!q)
803 break;
804 p += strlen(p);
805 if (p > buffer && p[-1] == '\n') {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300806 /*
807 * Convert backslash-CRLF line continuation sequences into
808 * nothing at all (for DOS and Windows)
809 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000810 if (((p - 2) > buffer) && (p[-3] == '\\') && (p[-2] == '\r')) {
811 p -= 3;
812 *p = 0;
813 continued_count++;
814 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300815 /*
816 * Also convert backslash-LF line continuation sequences into
817 * nothing at all (for Unix)
818 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000819 else if (((p - 1) > buffer) && (p[-2] == '\\')) {
820 p -= 2;
821 *p = 0;
822 continued_count++;
823 } else {
824 break;
825 }
826 }
827 if (p - buffer > bufsize - 10) {
Keith Kaniosb7a89542007-04-12 02:40:54 +0000828 int32_t offset = p - buffer;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000829 bufsize += BUF_DELTA;
830 buffer = nasm_realloc(buffer, bufsize);
831 p = buffer + offset; /* prevent stale-pointer problems */
832 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000833 }
834
H. Peter Anvine2c80182005-01-15 22:15:51 +0000835 if (!q && p == buffer) {
836 nasm_free(buffer);
837 return NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000838 }
839
H. Peter Anvine2c80182005-01-15 22:15:51 +0000840 src_set_linnum(src_get_linnum() + istk->lineinc +
841 (continued_count * istk->lineinc));
H. Peter Anvineba20a72002-04-30 20:53:55 +0000842
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000843 /*
844 * Play safe: remove CRs as well as LFs, if any of either are
845 * present at the end of the line.
846 */
H. Peter Anvineba20a72002-04-30 20:53:55 +0000847 while (--p >= buffer && (*p == '\n' || *p == '\r'))
H. Peter Anvine2c80182005-01-15 22:15:51 +0000848 *p = '\0';
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000849
850 /*
851 * Handle spurious ^Z, which may be inserted into source files
852 * by some file transfer utilities.
853 */
854 buffer[strcspn(buffer, "\032")] = '\0';
855
H. Peter Anvin734b1882002-04-30 21:01:08 +0000856 list->line(LIST_READ, buffer);
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000857
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000858 return buffer;
859}
860
861/*
Keith Kaniosb7a89542007-04-12 02:40:54 +0000862 * Tokenize a line of text. This is a very simple process since we
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000863 * don't need to parse the value out of e.g. numeric tokens: we
864 * simply split one string into many.
865 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000866static Token *tokenize(char *line)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000867{
H. Peter Anvinca544db2008-10-19 19:30:11 -0700868 char c, *p = line;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +0000869 enum pp_token_type type;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000870 Token *list = NULL;
871 Token *t, **tail = &list;
872
H. Peter Anvine2c80182005-01-15 22:15:51 +0000873 while (*line) {
874 p = line;
875 if (*p == '%') {
876 p++;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300877 if (*p == '+' && !nasm_isdigit(p[1])) {
878 p++;
879 type = TOK_PASTE;
880 } else if (nasm_isdigit(*p) ||
881 ((*p == '-' || *p == '+') && nasm_isdigit(p[1]))) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000882 do {
883 p++;
884 }
H. Peter Anvinbda7a6e2008-06-21 10:23:17 -0700885 while (nasm_isdigit(*p));
H. Peter Anvine2c80182005-01-15 22:15:51 +0000886 type = TOK_PREPROC_ID;
887 } else if (*p == '{') {
888 p++;
889 while (*p && *p != '}') {
890 p[-1] = *p;
891 p++;
892 }
893 p[-1] = '\0';
894 if (*p)
895 p++;
896 type = TOK_PREPROC_ID;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300897 } else if (*p == '[') {
898 int lvl = 1;
899 line += 2; /* Skip the leading %[ */
900 p++;
901 while (lvl && (c = *p++)) {
902 switch (c) {
903 case ']':
904 lvl--;
905 break;
906 case '%':
907 if (*p == '[')
908 lvl++;
909 break;
910 case '\'':
911 case '\"':
912 case '`':
Cyrill Gorcunovc6360a72010-07-13 13:32:19 +0400913 p = nasm_skip_string(p - 1) + 1;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300914 break;
915 default:
916 break;
917 }
918 }
919 p--;
920 if (*p)
921 *p++ = '\0';
922 if (lvl)
923 error(ERR_NONFATAL, "unterminated %[ construct");
924 type = TOK_INDIRECT;
925 } else if (*p == '?') {
926 type = TOK_PREPROC_Q; /* %? */
927 p++;
928 if (*p == '?') {
929 type = TOK_PREPROC_QQ; /* %?? */
930 p++;
931 }
H. Peter Anvin077fb932010-07-20 14:56:30 -0700932 } else if (*p == '!') {
933 type = TOK_PREPROC_ID;
934 p++;
935 if (isidchar(*p)) {
936 do {
937 p++;
938 }
939 while (isidchar(*p));
940 } else if (*p == '\'' || *p == '\"' || *p == '`') {
941 p = nasm_skip_string(p);
942 if (*p)
943 p++;
944 else
945 error(ERR_NONFATAL|ERR_PASS1, "unterminated %! string");
946 } else {
947 /* %! without string or identifier */
948 type = TOK_OTHER; /* Legacy behavior... */
949 }
H. Peter Anvine2c80182005-01-15 22:15:51 +0000950 } else if (isidchar(*p) ||
951 ((*p == '!' || *p == '%' || *p == '$') &&
952 isidchar(p[1]))) {
953 do {
954 p++;
955 }
956 while (isidchar(*p));
957 type = TOK_PREPROC_ID;
958 } else {
959 type = TOK_OTHER;
960 if (*p == '%')
961 p++;
962 }
963 } else if (isidstart(*p) || (*p == '$' && isidstart(p[1]))) {
964 type = TOK_ID;
965 p++;
966 while (*p && isidchar(*p))
967 p++;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -0700968 } else if (*p == '\'' || *p == '"' || *p == '`') {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000969 /*
970 * A string token.
971 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000972 type = TOK_STRING;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300973 p = nasm_skip_string(p);
Nickolay Yurchenkof3b3ce22003-09-21 20:38:43 +0000974
H. Peter Anvine2c80182005-01-15 22:15:51 +0000975 if (*p) {
976 p++;
977 } else {
H. Peter Anvin917a3492008-09-24 09:14:49 -0700978 error(ERR_WARNING|ERR_PASS1, "unterminated string");
H. Peter Anvine2c80182005-01-15 22:15:51 +0000979 /* Handling unterminated strings by UNV */
980 /* type = -1; */
981 }
Victor van den Elzenfb5f2512009-04-17 16:17:59 +0200982 } else if (p[0] == '$' && p[1] == '$') {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300983 type = TOK_OTHER; /* TOKEN_BASE */
Victor van den Elzenfb5f2512009-04-17 16:17:59 +0200984 p += 2;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000985 } else if (isnumstart(*p)) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300986 bool is_hex = false;
987 bool is_float = false;
988 bool has_e = false;
989 char c, *r;
H. Peter Anvinc2df2822007-10-24 15:29:28 -0700990
H. Peter Anvine2c80182005-01-15 22:15:51 +0000991 /*
H. Peter Anvinc2df2822007-10-24 15:29:28 -0700992 * A numeric token.
H. Peter Anvine2c80182005-01-15 22:15:51 +0000993 */
H. Peter Anvinc2df2822007-10-24 15:29:28 -0700994
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300995 if (*p == '$') {
996 p++;
997 is_hex = true;
998 }
H. Peter Anvinc2df2822007-10-24 15:29:28 -0700999
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001000 for (;;) {
1001 c = *p++;
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001002
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001003 if (!is_hex && (c == 'e' || c == 'E')) {
1004 has_e = true;
1005 if (*p == '+' || *p == '-') {
1006 /*
1007 * e can only be followed by +/- if it is either a
1008 * prefixed hex number or a floating-point number
1009 */
1010 p++;
1011 is_float = true;
1012 }
1013 } else if (c == 'H' || c == 'h' || c == 'X' || c == 'x') {
1014 is_hex = true;
1015 } else if (c == 'P' || c == 'p') {
1016 is_float = true;
1017 if (*p == '+' || *p == '-')
1018 p++;
1019 } else if (isnumchar(c) || c == '_')
1020 ; /* just advance */
1021 else if (c == '.') {
1022 /*
1023 * we need to deal with consequences of the legacy
1024 * parser, like "1.nolist" being two tokens
1025 * (TOK_NUMBER, TOK_ID) here; at least give it
1026 * a shot for now. In the future, we probably need
1027 * a flex-based scanner with proper pattern matching
1028 * to do it as well as it can be done. Nothing in
1029 * the world is going to help the person who wants
1030 * 0x123.p16 interpreted as two tokens, though.
1031 */
1032 r = p;
1033 while (*r == '_')
1034 r++;
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001035
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001036 if (nasm_isdigit(*r) || (is_hex && nasm_isxdigit(*r)) ||
1037 (!is_hex && (*r == 'e' || *r == 'E')) ||
1038 (*r == 'p' || *r == 'P')) {
1039 p = r;
1040 is_float = true;
1041 } else
1042 break; /* Terminate the token */
1043 } else
1044 break;
1045 }
1046 p--; /* Point to first character beyond number */
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001047
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001048 if (p == line+1 && *line == '$') {
1049 type = TOK_OTHER; /* TOKEN_HERE */
1050 } else {
1051 if (has_e && !is_hex) {
1052 /* 1e13 is floating-point, but 1e13h is not */
1053 is_float = true;
1054 }
H. Peter Anvind784a082009-04-20 14:01:18 -07001055
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001056 type = is_float ? TOK_FLOAT : TOK_NUMBER;
1057 }
H. Peter Anvinbda7a6e2008-06-21 10:23:17 -07001058 } else if (nasm_isspace(*p)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001059 type = TOK_WHITESPACE;
Cyrill Gorcunovf66ac7d2009-10-12 20:41:13 +04001060 p = nasm_skip_spaces(p);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001061 /*
1062 * Whitespace just before end-of-line is discarded by
1063 * pretending it's a comment; whitespace just before a
1064 * comment gets lumped into the comment.
1065 */
1066 if (!*p || *p == ';') {
1067 type = TOK_COMMENT;
1068 while (*p)
1069 p++;
1070 }
1071 } else if (*p == ';') {
1072 type = TOK_COMMENT;
1073 while (*p)
1074 p++;
1075 } else {
1076 /*
1077 * Anything else is an operator of some kind. We check
1078 * for all the double-character operators (>>, <<, //,
1079 * %%, <=, >=, ==, !=, <>, &&, ||, ^^), but anything
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001080 * else is a single-character operator.
H. Peter Anvine2c80182005-01-15 22:15:51 +00001081 */
1082 type = TOK_OTHER;
1083 if ((p[0] == '>' && p[1] == '>') ||
1084 (p[0] == '<' && p[1] == '<') ||
1085 (p[0] == '/' && p[1] == '/') ||
1086 (p[0] == '<' && p[1] == '=') ||
1087 (p[0] == '>' && p[1] == '=') ||
1088 (p[0] == '=' && p[1] == '=') ||
1089 (p[0] == '!' && p[1] == '=') ||
1090 (p[0] == '<' && p[1] == '>') ||
1091 (p[0] == '&' && p[1] == '&') ||
1092 (p[0] == '|' && p[1] == '|') ||
1093 (p[0] == '^' && p[1] == '^')) {
1094 p++;
1095 }
1096 p++;
1097 }
Nickolay Yurchenkof3b3ce22003-09-21 20:38:43 +00001098
H. Peter Anvine2c80182005-01-15 22:15:51 +00001099 /* Handling unterminated string by UNV */
1100 /*if (type == -1)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001101 {
1102 *tail = t = new_Token(NULL, TOK_STRING, line, p-line+1);
1103 t->text[p-line] = *line;
1104 tail = &t->next;
1105 }
1106 else */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001107 if (type != TOK_COMMENT) {
1108 *tail = t = new_Token(NULL, type, line, p - line);
1109 tail = &t->next;
1110 }
1111 line = p;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001112 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001113 return list;
1114}
1115
H. Peter Anvince616072002-04-30 21:02:23 +00001116/*
1117 * this function allocates a new managed block of memory and
H. Peter Anvin70653092007-10-19 14:42:29 -07001118 * returns a pointer to the block. The managed blocks are
H. Peter Anvince616072002-04-30 21:02:23 +00001119 * deleted only all at once by the delete_Blocks function.
1120 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001121static void *new_Block(size_t size)
H. Peter Anvince616072002-04-30 21:02:23 +00001122{
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001123 Blocks *b = &blocks;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001124
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001125 /* first, get to the end of the linked list */
1126 while (b->next)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001127 b = b->next;
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001128 /* now allocate the requested chunk */
1129 b->chunk = nasm_malloc(size);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001130
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001131 /* now allocate a new block for the next request */
1132 b->next = nasm_malloc(sizeof(Blocks));
1133 /* and initialize the contents of the new block */
1134 b->next->next = NULL;
1135 b->next->chunk = NULL;
1136 return b->chunk;
H. Peter Anvince616072002-04-30 21:02:23 +00001137}
1138
1139/*
1140 * this function deletes all managed blocks of memory
1141 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001142static void delete_Blocks(void)
H. Peter Anvince616072002-04-30 21:02:23 +00001143{
H. Peter Anvine2c80182005-01-15 22:15:51 +00001144 Blocks *a, *b = &blocks;
H. Peter Anvince616072002-04-30 21:02:23 +00001145
H. Peter Anvin70653092007-10-19 14:42:29 -07001146 /*
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001147 * keep in mind that the first block, pointed to by blocks
H. Peter Anvin70653092007-10-19 14:42:29 -07001148 * is a static and not dynamically allocated, so we don't
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001149 * free it.
1150 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001151 while (b) {
1152 if (b->chunk)
1153 nasm_free(b->chunk);
1154 a = b;
1155 b = b->next;
1156 if (a != &blocks)
1157 nasm_free(a);
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001158 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001159}
H. Peter Anvin734b1882002-04-30 21:01:08 +00001160
1161/*
H. Peter Anvin70653092007-10-19 14:42:29 -07001162 * this function creates a new Token and passes a pointer to it
H. Peter Anvin734b1882002-04-30 21:01:08 +00001163 * back to the caller. It sets the type and text elements, and
H. Peter Anvinf26e0972008-07-01 21:26:27 -07001164 * also the a.mac and next elements to NULL.
H. Peter Anvin734b1882002-04-30 21:01:08 +00001165 */
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07001166static Token *new_Token(Token * next, enum pp_token_type type,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001167 const char *text, int txtlen)
H. Peter Anvin734b1882002-04-30 21:01:08 +00001168{
1169 Token *t;
1170 int i;
1171
H. Peter Anvin89cee572009-07-15 09:16:54 -04001172 if (!freeTokens) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001173 freeTokens = (Token *) new_Block(TOKEN_BLOCKSIZE * sizeof(Token));
1174 for (i = 0; i < TOKEN_BLOCKSIZE - 1; i++)
1175 freeTokens[i].next = &freeTokens[i + 1];
1176 freeTokens[i].next = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001177 }
1178 t = freeTokens;
1179 freeTokens = t->next;
1180 t->next = next;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07001181 t->a.mac = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001182 t->type = type;
H. Peter Anvin89cee572009-07-15 09:16:54 -04001183 if (type == TOK_WHITESPACE || !text) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001184 t->text = NULL;
1185 } else {
1186 if (txtlen == 0)
1187 txtlen = strlen(text);
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07001188 t->text = nasm_malloc(txtlen+1);
1189 memcpy(t->text, text, txtlen);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001190 t->text[txtlen] = '\0';
H. Peter Anvin734b1882002-04-30 21:01:08 +00001191 }
1192 return t;
1193}
1194
H. Peter Anvine2c80182005-01-15 22:15:51 +00001195static Token *delete_Token(Token * t)
H. Peter Anvin734b1882002-04-30 21:01:08 +00001196{
1197 Token *next = t->next;
1198 nasm_free(t->text);
H. Peter Anvin788e6c12002-04-30 21:02:01 +00001199 t->next = freeTokens;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001200 freeTokens = t;
1201 return next;
1202}
1203
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001204/*
1205 * Convert a line of tokens back into text.
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001206 * If expand_locals is not zero, identifiers of the form "%$*xxx"
1207 * will be transformed into ..@ctxnum.xxx
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001208 */
H. Peter Anvin9e200162008-06-04 17:23:14 -07001209static char *detoken(Token * tlist, bool expand_locals)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001210{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001211 Token *t;
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001212 char *line, *p;
H. Peter Anvinb4daadc2008-01-21 16:31:57 -08001213 const char *q;
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001214 int len = 0;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001215
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001216 list_for_each(t, tlist) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001217 if (t->type == TOK_PREPROC_ID && t->text[1] == '!') {
H. Peter Anvin077fb932010-07-20 14:56:30 -07001218 char *v;
1219 char *q = t->text;
1220
1221 v = t->text + 2;
1222 if (*v == '\'' || *v == '\"' || *v == '`') {
1223 size_t len = nasm_unquote(v, NULL);
1224 size_t clen = strlen(v);
1225
1226 if (len != clen) {
1227 error(ERR_NONFATAL | ERR_PASS1,
1228 "NUL character in %! string");
1229 v = NULL;
1230 }
H. Peter Anvin5b00bf42010-07-13 11:43:06 -07001231 }
H. Peter Anvin077fb932010-07-20 14:56:30 -07001232
1233 if (v) {
1234 char *p = getenv(v);
1235 if (!p) {
1236 error(ERR_NONFATAL | ERR_PASS1,
1237 "nonexistent environment variable `%s'", v);
1238 p = "";
1239 }
1240 t->text = nasm_strdup(p);
1241 }
1242 nasm_free(q);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001243 }
H. Peter Anvin077fb932010-07-20 14:56:30 -07001244
H. Peter Anvine2c80182005-01-15 22:15:51 +00001245 /* Expand local macros here and not during preprocessing */
1246 if (expand_locals &&
1247 t->type == TOK_PREPROC_ID && t->text &&
1248 t->text[0] == '%' && t->text[1] == '$') {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001249 const char *q;
1250 char *p;
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001251 Context *ctx = get_ctx(t->text, &q, false);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001252 if (ctx) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001253 char buffer[40];
Keith Kanios93f2e9a2007-04-14 00:10:59 +00001254 snprintf(buffer, sizeof(buffer), "..@%"PRIu32".", ctx->number);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001255 p = nasm_strcat(buffer, q);
1256 nasm_free(t->text);
1257 t->text = p;
1258 }
1259 }
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001260 if (t->type == TOK_WHITESPACE)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001261 len++;
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001262 else if (t->text)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001263 len += strlen(t->text);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001264 }
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001265
H. Peter Anvin734b1882002-04-30 21:01:08 +00001266 p = line = nasm_malloc(len + 1);
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001267
1268 list_for_each(t, tlist) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001269 if (t->type == TOK_WHITESPACE) {
H. Peter Anvinb4daadc2008-01-21 16:31:57 -08001270 *p++ = ' ';
H. Peter Anvine2c80182005-01-15 22:15:51 +00001271 } else if (t->text) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001272 q = t->text;
1273 while (*q)
1274 *p++ = *q++;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001275 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001276 }
1277 *p = '\0';
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001278
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001279 return line;
1280}
1281
1282/*
H. Peter Anvin76690a12002-04-30 20:52:49 +00001283 * A scanner, suitable for use by the expression evaluator, which
1284 * operates on a line of Tokens. Expects a pointer to a pointer to
1285 * the first token in the line to be passed in as its private_data
1286 * field.
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001287 *
1288 * FIX: This really needs to be unified with stdscan.
H. Peter Anvin76690a12002-04-30 20:52:49 +00001289 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001290static int ppscan(void *private_data, struct tokenval *tokval)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001291{
H. Peter Anvin76690a12002-04-30 20:52:49 +00001292 Token **tlineptr = private_data;
1293 Token *tline;
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001294 char ourcopy[MAX_KEYWORD+1], *p, *r, *s;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001295
H. Peter Anvine2c80182005-01-15 22:15:51 +00001296 do {
1297 tline = *tlineptr;
1298 *tlineptr = tline ? tline->next : NULL;
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04001299 } while (tline && (tline->type == TOK_WHITESPACE ||
1300 tline->type == TOK_COMMENT));
H. Peter Anvin76690a12002-04-30 20:52:49 +00001301
1302 if (!tline)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001303 return tokval->t_type = TOKEN_EOS;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001304
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001305 tokval->t_charptr = tline->text;
1306
H. Peter Anvin76690a12002-04-30 20:52:49 +00001307 if (tline->text[0] == '$' && !tline->text[1])
H. Peter Anvine2c80182005-01-15 22:15:51 +00001308 return tokval->t_type = TOKEN_HERE;
H. Peter Anvin7cf897e2002-05-30 21:30:33 +00001309 if (tline->text[0] == '$' && tline->text[1] == '$' && !tline->text[2])
H. Peter Anvine2c80182005-01-15 22:15:51 +00001310 return tokval->t_type = TOKEN_BASE;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001311
H. Peter Anvine2c80182005-01-15 22:15:51 +00001312 if (tline->type == TOK_ID) {
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001313 p = tokval->t_charptr = tline->text;
1314 if (p[0] == '$') {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001315 tokval->t_charptr++;
1316 return tokval->t_type = TOKEN_ID;
1317 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00001318
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001319 for (r = p, s = ourcopy; *r; r++) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001320 if (r >= p+MAX_KEYWORD)
1321 return tokval->t_type = TOKEN_ID; /* Not a keyword */
H. Peter Anvinac8f8fc2008-06-11 15:49:41 -07001322 *s++ = nasm_tolower(*r);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001323 }
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001324 *s = '\0';
1325 /* right, so we have an identifier sitting in temp storage. now,
1326 * is it actually a register or instruction name, or what? */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001327 return nasm_token_hash(ourcopy, tokval);
H. Peter Anvin76690a12002-04-30 20:52:49 +00001328 }
1329
H. Peter Anvine2c80182005-01-15 22:15:51 +00001330 if (tline->type == TOK_NUMBER) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001331 bool rn_error;
1332 tokval->t_integer = readnum(tline->text, &rn_error);
1333 tokval->t_charptr = tline->text;
1334 if (rn_error)
1335 return tokval->t_type = TOKEN_ERRNUM;
1336 else
1337 return tokval->t_type = TOKEN_NUM;
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001338 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00001339
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001340 if (tline->type == TOK_FLOAT) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001341 return tokval->t_type = TOKEN_FLOAT;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001342 }
1343
H. Peter Anvine2c80182005-01-15 22:15:51 +00001344 if (tline->type == TOK_STRING) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001345 char bq, *ep;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001346
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001347 bq = tline->text[0];
H. Peter Anvin11627042008-06-09 20:45:19 -07001348 tokval->t_charptr = tline->text;
1349 tokval->t_inttwo = nasm_unquote(tline->text, &ep);
H. Peter Anvind2456592008-06-19 15:04:18 -07001350
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001351 if (ep[0] != bq || ep[1] != '\0')
1352 return tokval->t_type = TOKEN_ERRSTR;
1353 else
1354 return tokval->t_type = TOKEN_STR;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001355 }
1356
H. Peter Anvine2c80182005-01-15 22:15:51 +00001357 if (tline->type == TOK_OTHER) {
1358 if (!strcmp(tline->text, "<<"))
1359 return tokval->t_type = TOKEN_SHL;
1360 if (!strcmp(tline->text, ">>"))
1361 return tokval->t_type = TOKEN_SHR;
1362 if (!strcmp(tline->text, "//"))
1363 return tokval->t_type = TOKEN_SDIV;
1364 if (!strcmp(tline->text, "%%"))
1365 return tokval->t_type = TOKEN_SMOD;
1366 if (!strcmp(tline->text, "=="))
1367 return tokval->t_type = TOKEN_EQ;
1368 if (!strcmp(tline->text, "<>"))
1369 return tokval->t_type = TOKEN_NE;
1370 if (!strcmp(tline->text, "!="))
1371 return tokval->t_type = TOKEN_NE;
1372 if (!strcmp(tline->text, "<="))
1373 return tokval->t_type = TOKEN_LE;
1374 if (!strcmp(tline->text, ">="))
1375 return tokval->t_type = TOKEN_GE;
1376 if (!strcmp(tline->text, "&&"))
1377 return tokval->t_type = TOKEN_DBL_AND;
1378 if (!strcmp(tline->text, "^^"))
1379 return tokval->t_type = TOKEN_DBL_XOR;
1380 if (!strcmp(tline->text, "||"))
1381 return tokval->t_type = TOKEN_DBL_OR;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001382 }
1383
1384 /*
1385 * We have no other options: just return the first character of
1386 * the token text.
1387 */
1388 return tokval->t_type = tline->text[0];
1389}
1390
1391/*
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001392 * Compare a string to the name of an existing macro; this is a
1393 * simple wrapper which calls either strcmp or nasm_stricmp
1394 * depending on the value of the `casesense' parameter.
1395 */
H. Peter Anvin4db5a162007-10-11 13:42:09 -07001396static int mstrcmp(const char *p, const char *q, bool casesense)
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001397{
H. Peter Anvin734b1882002-04-30 21:01:08 +00001398 return casesense ? strcmp(p, q) : nasm_stricmp(p, q);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001399}
1400
1401/*
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07001402 * Compare a string to the name of an existing macro; this is a
1403 * simple wrapper which calls either strcmp or nasm_stricmp
1404 * depending on the value of the `casesense' parameter.
1405 */
1406static int mmemcmp(const char *p, const char *q, size_t l, bool casesense)
1407{
1408 return casesense ? memcmp(p, q, l) : nasm_memicmp(p, q, l);
1409}
1410
1411/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001412 * Return the Context structure associated with a %$ token. Return
1413 * NULL, having _already_ reported an error condition, if the
1414 * context stack isn't deep enough for the supplied number of $
1415 * signs.
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001416 * If all_contexts == true, contexts that enclose current are
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001417 * also scanned for such smacro, until it is found; if not -
1418 * only the context that directly results from the number of $'s
1419 * in variable's name.
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001420 *
1421 * If "namep" is non-NULL, set it to the pointer to the macro name
1422 * tail, i.e. the part beyond %$...
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001423 */
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001424static Context *get_ctx(const char *name, const char **namep,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001425 bool all_contexts)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001426{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001427 Context *ctx;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001428 SMacro *m;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001429 int i;
1430
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001431 if (namep)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001432 *namep = name;
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001433
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001434 if (!name || name[0] != '%' || name[1] != '$')
H. Peter Anvine2c80182005-01-15 22:15:51 +00001435 return NULL;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001436
H. Peter Anvine2c80182005-01-15 22:15:51 +00001437 if (!cstk) {
1438 error(ERR_NONFATAL, "`%s': context stack is empty", name);
1439 return NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001440 }
1441
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001442 name += 2;
1443 ctx = cstk;
1444 i = 0;
1445 while (ctx && *name == '$') {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001446 name++;
1447 i++;
1448 ctx = ctx->next;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001449 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001450 if (!ctx) {
1451 error(ERR_NONFATAL, "`%s': context stack is only"
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001452 " %d level%s deep", name, i, (i == 1 ? "" : "s"));
H. Peter Anvine2c80182005-01-15 22:15:51 +00001453 return NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001454 }
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001455
1456 if (namep)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001457 *namep = name;
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001458
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001459 if (!all_contexts)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001460 return ctx;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001461
H. Peter Anvine2c80182005-01-15 22:15:51 +00001462 do {
1463 /* Search for this smacro in found context */
H. Peter Anvin166c2472008-05-28 12:28:58 -07001464 m = hash_findix(&ctx->localmac, name);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001465 while (m) {
1466 if (!mstrcmp(m->name, name, m->casesense))
1467 return ctx;
1468 m = m->next;
1469 }
1470 ctx = ctx->next;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001471 }
1472 while (ctx);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001473 return NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001474}
1475
1476/*
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001477 * Check to see if a file is already in a string list
1478 */
1479static bool in_list(const StrList *list, const char *str)
1480{
1481 while (list) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001482 if (!strcmp(list->str, str))
1483 return true;
1484 list = list->next;
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001485 }
1486 return false;
1487}
1488
1489/*
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001490 * Open an include file. This routine must always return a valid
1491 * file pointer if it returns - it's responsible for throwing an
1492 * ERR_FATAL and bombing out completely if not. It should also try
1493 * the include path one by one until it finds the file or reaches
1494 * the end of the path.
1495 */
H. Peter Anvin2b1c3b92008-06-06 10:38:46 -07001496static FILE *inc_fopen(const char *file, StrList **dhead, StrList ***dtail,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001497 bool missing_ok)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001498{
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001499 FILE *fp;
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001500 char *prefix = "";
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001501 IncPath *ip = ipath;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001502 int len = strlen(file);
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001503 size_t prefix_len = 0;
1504 StrList *sl;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001505
H. Peter Anvine2c80182005-01-15 22:15:51 +00001506 while (1) {
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001507 sl = nasm_malloc(prefix_len+len+1+sizeof sl->next);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001508 memcpy(sl->str, prefix, prefix_len);
1509 memcpy(sl->str+prefix_len, file, len+1);
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001510 fp = fopen(sl->str, "r");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001511 if (fp && dhead && !in_list(*dhead, sl->str)) {
1512 sl->next = NULL;
1513 **dtail = sl;
1514 *dtail = &sl->next;
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001515 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001516 nasm_free(sl);
1517 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001518 if (fp)
1519 return fp;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001520 if (!ip) {
1521 if (!missing_ok)
1522 break;
1523 prefix = NULL;
1524 } else {
1525 prefix = ip->path;
1526 ip = ip->next;
1527 }
1528 if (prefix) {
1529 prefix_len = strlen(prefix);
1530 } else {
1531 /* -MG given and file not found */
1532 if (dhead && !in_list(*dhead, file)) {
1533 sl = nasm_malloc(len+1+sizeof sl->next);
1534 sl->next = NULL;
1535 strcpy(sl->str, file);
1536 **dtail = sl;
1537 *dtail = &sl->next;
1538 }
1539 return NULL;
1540 }
H. Peter Anvineba20a72002-04-30 20:53:55 +00001541 }
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001542
H. Peter Anvin734b1882002-04-30 21:01:08 +00001543 error(ERR_FATAL, "unable to open include file `%s'", file);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001544 return NULL;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001545}
1546
1547/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001548 * Determine if we should warn on defining a single-line macro of
H. Peter Anvinef7468f2002-04-30 20:57:59 +00001549 * name `name', with `nparam' parameters. If nparam is 0 or -1, will
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001550 * return true if _any_ single-line macro of that name is defined.
1551 * Otherwise, will return true if a single-line macro with either
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001552 * `nparam' or no parameters is defined.
1553 *
1554 * If a macro with precisely the right number of parameters is
H. Peter Anvinef7468f2002-04-30 20:57:59 +00001555 * defined, or nparam is -1, the address of the definition structure
1556 * will be returned in `defn'; otherwise NULL will be returned. If `defn'
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001557 * is NULL, no action will be taken regarding its contents, and no
1558 * error will occur.
1559 *
1560 * Note that this is also called with nparam zero to resolve
1561 * `ifdef'.
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001562 *
1563 * If you already know which context macro belongs to, you can pass
1564 * the context pointer as first parameter; if you won't but name begins
1565 * with %$ the context will be automatically computed. If all_contexts
1566 * is true, macro will be searched in outer contexts as well.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001567 */
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07001568static bool
H. Peter Anvinb2a5fda2008-06-19 21:42:42 -07001569smacro_defined(Context * ctx, const char *name, int nparam, SMacro ** defn,
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07001570 bool nocase)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001571{
H. Peter Anvin166c2472008-05-28 12:28:58 -07001572 struct hash_table *smtbl;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001573 SMacro *m;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001574
H. Peter Anvin97a23472007-09-16 17:57:25 -07001575 if (ctx) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001576 smtbl = &ctx->localmac;
H. Peter Anvin97a23472007-09-16 17:57:25 -07001577 } else if (name[0] == '%' && name[1] == '$') {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001578 if (cstk)
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001579 ctx = get_ctx(name, &name, false);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001580 if (!ctx)
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001581 return false; /* got to return _something_ */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001582 smtbl = &ctx->localmac;
H. Peter Anvin97a23472007-09-16 17:57:25 -07001583 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001584 smtbl = &smacros;
H. Peter Anvin97a23472007-09-16 17:57:25 -07001585 }
H. Peter Anvin166c2472008-05-28 12:28:58 -07001586 m = (SMacro *) hash_findix(smtbl, name);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001587
H. Peter Anvine2c80182005-01-15 22:15:51 +00001588 while (m) {
1589 if (!mstrcmp(m->name, name, m->casesense && nocase) &&
Charles Crayne192d5b52007-10-18 19:02:42 -07001590 (nparam <= 0 || m->nparam == 0 || nparam == (int) m->nparam)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001591 if (defn) {
Charles Crayne192d5b52007-10-18 19:02:42 -07001592 if (nparam == (int) m->nparam || nparam == -1)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001593 *defn = m;
1594 else
1595 *defn = NULL;
1596 }
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001597 return true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001598 }
1599 m = m->next;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001600 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001601
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001602 return false;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001603}
1604
1605/*
1606 * Count and mark off the parameters in a multi-line macro call.
1607 * This is called both from within the multi-line macro expansion
1608 * code, and also to mark off the default parameters when provided
1609 * in a %macro definition line.
1610 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001611static void count_mmac_params(Token * t, int *nparam, Token *** params)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001612{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001613 int paramsize, brace;
1614
1615 *nparam = paramsize = 0;
1616 *params = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001617 while (t) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001618 /* +1: we need space for the final NULL */
H. Peter Anvin91fb6f12008-09-01 10:56:33 -07001619 if (*nparam+1 >= paramsize) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001620 paramsize += PARAM_DELTA;
1621 *params = nasm_realloc(*params, sizeof(**params) * paramsize);
1622 }
1623 skip_white_(t);
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001624 brace = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001625 if (tok_is_(t, "{"))
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001626 brace = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001627 (*params)[(*nparam)++] = t;
1628 while (tok_isnt_(t, brace ? "}" : ","))
1629 t = t->next;
1630 if (t) { /* got a comma/brace */
1631 t = t->next;
1632 if (brace) {
1633 /*
1634 * Now we've found the closing brace, look further
1635 * for the comma.
1636 */
1637 skip_white_(t);
1638 if (tok_isnt_(t, ",")) {
1639 error(ERR_NONFATAL,
1640 "braces do not enclose all of macro parameter");
1641 while (tok_isnt_(t, ","))
1642 t = t->next;
1643 }
1644 if (t)
1645 t = t->next; /* eat the comma */
1646 }
1647 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001648 }
1649}
1650
1651/*
H. Peter Anvin76690a12002-04-30 20:52:49 +00001652 * Determine whether one of the various `if' conditions is true or
1653 * not.
1654 *
1655 * We must free the tline we get passed.
1656 */
H. Peter Anvin70055962007-10-11 00:05:31 -07001657static bool if_condition(Token * tline, enum preproc_token ct)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001658{
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001659 enum pp_conditional i = PP_COND(ct);
H. Peter Anvin70055962007-10-11 00:05:31 -07001660 bool j;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001661 Token *t, *tt, **tptr, *origline;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001662 struct tokenval tokval;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001663 expr *evalresult;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001664 enum pp_token_type needtype;
H. Peter Anvin077fb932010-07-20 14:56:30 -07001665 char *p;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001666
1667 origline = tline;
1668
H. Peter Anvine2c80182005-01-15 22:15:51 +00001669 switch (i) {
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001670 case PPC_IFCTX:
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001671 j = false; /* have we matched yet? */
Victor van den Elzen0e857f12008-07-23 13:21:29 +02001672 while (true) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001673 skip_white_(tline);
Victor van den Elzen0e857f12008-07-23 13:21:29 +02001674 if (!tline)
1675 break;
1676 if (tline->type != TOK_ID) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001677 error(ERR_NONFATAL,
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001678 "`%s' expects context identifiers", pp_directives[ct]);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001679 free_tlist(origline);
1680 return -1;
1681 }
Victor van den Elzen0e857f12008-07-23 13:21:29 +02001682 if (cstk && cstk->name && !nasm_stricmp(tline->text, cstk->name))
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001683 j = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001684 tline = tline->next;
1685 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001686 break;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001687
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001688 case PPC_IFDEF:
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001689 j = false; /* have we matched yet? */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001690 while (tline) {
1691 skip_white_(tline);
1692 if (!tline || (tline->type != TOK_ID &&
1693 (tline->type != TOK_PREPROC_ID ||
1694 tline->text[1] != '$'))) {
1695 error(ERR_NONFATAL,
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001696 "`%s' expects macro identifiers", pp_directives[ct]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001697 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001698 }
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07001699 if (smacro_defined(NULL, tline->text, 0, NULL, true))
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001700 j = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001701 tline = tline->next;
1702 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001703 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001704
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001705 case PPC_IFENV:
1706 tline = expand_smacro(tline);
1707 j = false; /* have we matched yet? */
1708 while (tline) {
1709 skip_white_(tline);
1710 if (!tline || (tline->type != TOK_ID &&
H. Peter Anvin077fb932010-07-20 14:56:30 -07001711 tline->type != TOK_STRING &&
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001712 (tline->type != TOK_PREPROC_ID ||
H. Peter Anvin077fb932010-07-20 14:56:30 -07001713 tline->text[1] != '!'))) {
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001714 error(ERR_NONFATAL,
1715 "`%s' expects environment variable names",
1716 pp_directives[ct]);
1717 goto fail;
1718 }
H. Peter Anvin077fb932010-07-20 14:56:30 -07001719 p = tline->text;
1720 if (tline->type == TOK_PREPROC_ID)
1721 p += 2; /* Skip leading %! */
1722 if (*p == '\'' || *p == '\"' || *p == '`')
1723 nasm_unquote_cstr(p, ct);
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001724 if (getenv(p))
1725 j = true;
1726 tline = tline->next;
1727 }
1728 break;
1729
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001730 case PPC_IFIDN:
1731 case PPC_IFIDNI:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001732 tline = expand_smacro(tline);
1733 t = tt = tline;
1734 while (tok_isnt_(tt, ","))
1735 tt = tt->next;
1736 if (!tt) {
1737 error(ERR_NONFATAL,
1738 "`%s' expects two comma-separated arguments",
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001739 pp_directives[ct]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001740 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001741 }
1742 tt = tt->next;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001743 j = true; /* assume equality unless proved not */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001744 while ((t->type != TOK_OTHER || strcmp(t->text, ",")) && tt) {
1745 if (tt->type == TOK_OTHER && !strcmp(tt->text, ",")) {
1746 error(ERR_NONFATAL, "`%s': more than one comma on line",
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001747 pp_directives[ct]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001748 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001749 }
1750 if (t->type == TOK_WHITESPACE) {
1751 t = t->next;
1752 continue;
1753 }
1754 if (tt->type == TOK_WHITESPACE) {
1755 tt = tt->next;
1756 continue;
1757 }
1758 if (tt->type != t->type) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001759 j = false; /* found mismatching tokens */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001760 break;
1761 }
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07001762 /* When comparing strings, need to unquote them first */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001763 if (t->type == TOK_STRING) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001764 size_t l1 = nasm_unquote(t->text, NULL);
1765 size_t l2 = nasm_unquote(tt->text, NULL);
H. Peter Anvind2456592008-06-19 15:04:18 -07001766
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001767 if (l1 != l2) {
1768 j = false;
1769 break;
1770 }
1771 if (mmemcmp(t->text, tt->text, l1, i == PPC_IFIDN)) {
1772 j = false;
1773 break;
1774 }
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07001775 } else if (mstrcmp(tt->text, t->text, i == PPC_IFIDN) != 0) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001776 j = false; /* found mismatching tokens */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001777 break;
1778 }
Nickolay Yurchenkof3b3ce22003-09-21 20:38:43 +00001779
H. Peter Anvine2c80182005-01-15 22:15:51 +00001780 t = t->next;
1781 tt = tt->next;
1782 }
1783 if ((t->type != TOK_OTHER || strcmp(t->text, ",")) || tt)
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001784 j = false; /* trailing gunk on one end or other */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001785 break;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001786
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001787 case PPC_IFMACRO:
H. Peter Anvin89cee572009-07-15 09:16:54 -04001788 {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001789 bool found = false;
1790 MMacro searching, *mmac;
H. Peter Anvin65747262002-05-07 00:10:05 +00001791
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001792 skip_white_(tline);
1793 tline = expand_id(tline);
1794 if (!tok_type_(tline, TOK_ID)) {
1795 error(ERR_NONFATAL,
1796 "`%s' expects a macro name", pp_directives[ct]);
1797 goto fail;
1798 }
1799 searching.name = nasm_strdup(tline->text);
1800 searching.casesense = true;
1801 searching.plus = false;
1802 searching.nolist = false;
1803 searching.in_progress = 0;
1804 searching.max_depth = 0;
1805 searching.rep_nest = NULL;
1806 searching.nparam_min = 0;
1807 searching.nparam_max = INT_MAX;
1808 tline = expand_smacro(tline->next);
1809 skip_white_(tline);
1810 if (!tline) {
1811 } else if (!tok_type_(tline, TOK_NUMBER)) {
1812 error(ERR_NONFATAL,
1813 "`%s' expects a parameter count or nothing",
1814 pp_directives[ct]);
1815 } else {
1816 searching.nparam_min = searching.nparam_max =
1817 readnum(tline->text, &j);
1818 if (j)
1819 error(ERR_NONFATAL,
1820 "unable to parse parameter count `%s'",
1821 tline->text);
1822 }
1823 if (tline && tok_is_(tline->next, "-")) {
1824 tline = tline->next->next;
1825 if (tok_is_(tline, "*"))
1826 searching.nparam_max = INT_MAX;
1827 else if (!tok_type_(tline, TOK_NUMBER))
1828 error(ERR_NONFATAL,
1829 "`%s' expects a parameter count after `-'",
1830 pp_directives[ct]);
1831 else {
1832 searching.nparam_max = readnum(tline->text, &j);
1833 if (j)
1834 error(ERR_NONFATAL,
1835 "unable to parse parameter count `%s'",
1836 tline->text);
1837 if (searching.nparam_min > searching.nparam_max)
1838 error(ERR_NONFATAL,
1839 "minimum parameter count exceeds maximum");
1840 }
1841 }
1842 if (tline && tok_is_(tline->next, "+")) {
1843 tline = tline->next;
1844 searching.plus = true;
1845 }
1846 mmac = (MMacro *) hash_findix(&mmacros, searching.name);
1847 while (mmac) {
1848 if (!strcmp(mmac->name, searching.name) &&
1849 (mmac->nparam_min <= searching.nparam_max
1850 || searching.plus)
1851 && (searching.nparam_min <= mmac->nparam_max
1852 || mmac->plus)) {
1853 found = true;
1854 break;
1855 }
1856 mmac = mmac->next;
1857 }
1858 if (tline && tline->next)
1859 error(ERR_WARNING|ERR_PASS1,
1860 "trailing garbage after %%ifmacro ignored");
1861 nasm_free(searching.name);
1862 j = found;
1863 break;
H. Peter Anvin89cee572009-07-15 09:16:54 -04001864 }
H. Peter Anvin65747262002-05-07 00:10:05 +00001865
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001866 case PPC_IFID:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001867 needtype = TOK_ID;
1868 goto iftype;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001869 case PPC_IFNUM:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001870 needtype = TOK_NUMBER;
1871 goto iftype;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001872 case PPC_IFSTR:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001873 needtype = TOK_STRING;
1874 goto iftype;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001875
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001876iftype:
1877 t = tline = expand_smacro(tline);
H. Peter Anvind85d2502008-05-04 17:53:31 -07001878
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001879 while (tok_type_(t, TOK_WHITESPACE) ||
1880 (needtype == TOK_NUMBER &&
1881 tok_type_(t, TOK_OTHER) &&
1882 (t->text[0] == '-' || t->text[0] == '+') &&
1883 !t->text[1]))
1884 t = t->next;
H. Peter Anvind85d2502008-05-04 17:53:31 -07001885
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001886 j = tok_type_(t, needtype);
1887 break;
H. Peter Anvincbf768d2008-02-16 16:41:25 -08001888
1889 case PPC_IFTOKEN:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001890 t = tline = expand_smacro(tline);
1891 while (tok_type_(t, TOK_WHITESPACE))
1892 t = t->next;
H. Peter Anvincbf768d2008-02-16 16:41:25 -08001893
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001894 j = false;
1895 if (t) {
1896 t = t->next; /* Skip the actual token */
1897 while (tok_type_(t, TOK_WHITESPACE))
1898 t = t->next;
1899 j = !t; /* Should be nothing left */
1900 }
1901 break;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001902
H. Peter Anvin134b9462008-02-16 17:01:40 -08001903 case PPC_IFEMPTY:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001904 t = tline = expand_smacro(tline);
1905 while (tok_type_(t, TOK_WHITESPACE))
1906 t = t->next;
H. Peter Anvin134b9462008-02-16 17:01:40 -08001907
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001908 j = !t; /* Should be empty */
1909 break;
H. Peter Anvin134b9462008-02-16 17:01:40 -08001910
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001911 case PPC_IF:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001912 t = tline = expand_smacro(tline);
1913 tptr = &t;
1914 tokval.t_type = TOKEN_INVALID;
1915 evalresult = evaluate(ppscan, tptr, &tokval,
1916 NULL, pass | CRITICAL, error, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001917 if (!evalresult)
1918 return -1;
1919 if (tokval.t_type)
H. Peter Anvin917a3492008-09-24 09:14:49 -07001920 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +00001921 "trailing garbage after expression ignored");
1922 if (!is_simple(evalresult)) {
1923 error(ERR_NONFATAL,
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001924 "non-constant value given to `%s'", pp_directives[ct]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001925 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001926 }
Chuck Crayne60ae75d2007-05-02 01:59:16 +00001927 j = reloc_value(evalresult) != 0;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001928 break;
H. Peter Anvin95e28822007-09-12 04:20:08 +00001929
H. Peter Anvine2c80182005-01-15 22:15:51 +00001930 default:
1931 error(ERR_FATAL,
1932 "preprocessor directive `%s' not yet implemented",
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001933 pp_directives[ct]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001934 goto fail;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001935 }
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001936
1937 free_tlist(origline);
1938 return j ^ PP_NEGATIVE(ct);
H. Peter Anvin70653092007-10-19 14:42:29 -07001939
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001940fail:
1941 free_tlist(origline);
1942 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001943}
1944
1945/*
H. Peter Anvin4db5a162007-10-11 13:42:09 -07001946 * Common code for defining an smacro
1947 */
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001948static bool define_smacro(Context *ctx, const char *mname, bool casesense,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001949 int nparam, Token *expansion)
H. Peter Anvin4db5a162007-10-11 13:42:09 -07001950{
1951 SMacro *smac, **smhead;
H. Peter Anvin166c2472008-05-28 12:28:58 -07001952 struct hash_table *smtbl;
H. Peter Anvin70653092007-10-19 14:42:29 -07001953
H. Peter Anvin4db5a162007-10-11 13:42:09 -07001954 if (smacro_defined(ctx, mname, nparam, &smac, casesense)) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001955 if (!smac) {
1956 error(ERR_WARNING|ERR_PASS1,
1957 "single-line macro `%s' defined both with and"
1958 " without parameters", mname);
1959 /*
1960 * Some instances of the old code considered this a failure,
1961 * some others didn't. What is the right thing to do here?
1962 */
1963 free_tlist(expansion);
1964 return false; /* Failure */
1965 } else {
1966 /*
1967 * We're redefining, so we have to take over an
1968 * existing SMacro structure. This means freeing
1969 * what was already in it.
1970 */
1971 nasm_free(smac->name);
1972 free_tlist(smac->expansion);
1973 }
H. Peter Anvin4db5a162007-10-11 13:42:09 -07001974 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001975 smtbl = ctx ? &ctx->localmac : &smacros;
1976 smhead = (SMacro **) hash_findi_add(smtbl, mname);
1977 smac = nasm_malloc(sizeof(SMacro));
1978 smac->next = *smhead;
1979 *smhead = smac;
H. Peter Anvin4db5a162007-10-11 13:42:09 -07001980 }
1981 smac->name = nasm_strdup(mname);
1982 smac->casesense = casesense;
1983 smac->nparam = nparam;
1984 smac->expansion = expansion;
1985 smac->in_progress = false;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001986 return true; /* Success */
H. Peter Anvin4db5a162007-10-11 13:42:09 -07001987}
1988
1989/*
1990 * Undefine an smacro
1991 */
1992static void undef_smacro(Context *ctx, const char *mname)
1993{
1994 SMacro **smhead, *s, **sp;
H. Peter Anvin166c2472008-05-28 12:28:58 -07001995 struct hash_table *smtbl;
H. Peter Anvin4db5a162007-10-11 13:42:09 -07001996
H. Peter Anvin166c2472008-05-28 12:28:58 -07001997 smtbl = ctx ? &ctx->localmac : &smacros;
1998 smhead = (SMacro **)hash_findi(smtbl, mname, NULL);
H. Peter Anvin70653092007-10-19 14:42:29 -07001999
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002000 if (smhead) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002001 /*
2002 * We now have a macro name... go hunt for it.
2003 */
2004 sp = smhead;
2005 while ((s = *sp) != NULL) {
2006 if (!mstrcmp(s->name, mname, s->casesense)) {
2007 *sp = s->next;
2008 nasm_free(s->name);
2009 free_tlist(s->expansion);
2010 nasm_free(s);
2011 } else {
2012 sp = &s->next;
2013 }
2014 }
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002015 }
2016}
2017
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002018/*
H. Peter Anvina26433d2008-07-16 14:40:01 -07002019 * Parse a mmacro specification.
2020 */
2021static bool parse_mmacro_spec(Token *tline, MMacro *def, const char *directive)
2022{
2023 bool err;
2024
2025 tline = tline->next;
2026 skip_white_(tline);
2027 tline = expand_id(tline);
2028 if (!tok_type_(tline, TOK_ID)) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002029 error(ERR_NONFATAL, "`%s' expects a macro name", directive);
2030 return false;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002031 }
Victor van den Elzenb916ede2008-07-23 15:14:22 +02002032
H. Peter Anvin89cee572009-07-15 09:16:54 -04002033 def->prev = NULL;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002034 def->name = nasm_strdup(tline->text);
2035 def->plus = false;
2036 def->nolist = false;
2037 def->in_progress = 0;
2038 def->rep_nest = NULL;
Victor van den Elzenb916ede2008-07-23 15:14:22 +02002039 def->nparam_min = 0;
2040 def->nparam_max = 0;
2041
H. Peter Anvina26433d2008-07-16 14:40:01 -07002042 tline = expand_smacro(tline->next);
2043 skip_white_(tline);
2044 if (!tok_type_(tline, TOK_NUMBER)) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002045 error(ERR_NONFATAL, "`%s' expects a parameter count", directive);
H. Peter Anvina26433d2008-07-16 14:40:01 -07002046 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002047 def->nparam_min = def->nparam_max =
2048 readnum(tline->text, &err);
2049 if (err)
2050 error(ERR_NONFATAL,
2051 "unable to parse parameter count `%s'", tline->text);
H. Peter Anvina26433d2008-07-16 14:40:01 -07002052 }
2053 if (tline && tok_is_(tline->next, "-")) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002054 tline = tline->next->next;
2055 if (tok_is_(tline, "*")) {
2056 def->nparam_max = INT_MAX;
2057 } else if (!tok_type_(tline, TOK_NUMBER)) {
2058 error(ERR_NONFATAL,
2059 "`%s' expects a parameter count after `-'", directive);
2060 } else {
2061 def->nparam_max = readnum(tline->text, &err);
2062 if (err) {
2063 error(ERR_NONFATAL, "unable to parse parameter count `%s'",
2064 tline->text);
2065 }
2066 if (def->nparam_min > def->nparam_max) {
2067 error(ERR_NONFATAL, "minimum parameter count exceeds maximum");
2068 }
2069 }
H. Peter Anvina26433d2008-07-16 14:40:01 -07002070 }
2071 if (tline && tok_is_(tline->next, "+")) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002072 tline = tline->next;
2073 def->plus = true;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002074 }
2075 if (tline && tok_type_(tline->next, TOK_ID) &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002076 !nasm_stricmp(tline->next->text, ".nolist")) {
2077 tline = tline->next;
2078 def->nolist = true;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002079 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002080
H. Peter Anvina26433d2008-07-16 14:40:01 -07002081 /*
2082 * Handle default parameters.
2083 */
2084 if (tline && tline->next) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002085 def->dlist = tline->next;
2086 tline->next = NULL;
2087 count_mmac_params(def->dlist, &def->ndefs, &def->defaults);
H. Peter Anvina26433d2008-07-16 14:40:01 -07002088 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002089 def->dlist = NULL;
2090 def->defaults = NULL;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002091 }
2092 def->expansion = NULL;
2093
H. Peter Anvin89cee572009-07-15 09:16:54 -04002094 if (def->defaults && def->ndefs > def->nparam_max - def->nparam_min &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002095 !def->plus)
2096 error(ERR_WARNING|ERR_PASS1|ERR_WARN_MDP,
2097 "too many default macro parameters");
Victor van den Elzenb916ede2008-07-23 15:14:22 +02002098
H. Peter Anvina26433d2008-07-16 14:40:01 -07002099 return true;
2100}
2101
2102
2103/*
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002104 * Decode a size directive
2105 */
2106static int parse_size(const char *str) {
2107 static const char *size_names[] =
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002108 { "byte", "dword", "oword", "qword", "tword", "word", "yword" };
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002109 static const int sizes[] =
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002110 { 0, 1, 4, 16, 8, 10, 2, 32 };
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002111
Cyrill Gorcunova7319242010-06-03 22:04:36 +04002112 return sizes[bsii(str, size_names, ARRAY_SIZE(size_names))+1];
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002113}
2114
Ed Beroset3ab3f412002-06-11 03:31:49 +00002115/**
2116 * find and process preprocessor directive in passed line
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002117 * Find out if a line contains a preprocessor directive, and deal
2118 * with it if so.
H. Peter Anvin70653092007-10-19 14:42:29 -07002119 *
Ed Beroset3ab3f412002-06-11 03:31:49 +00002120 * If a directive _is_ found, it is the responsibility of this routine
2121 * (and not the caller) to free_tlist() the line.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002122 *
Ed Beroset3ab3f412002-06-11 03:31:49 +00002123 * @param tline a pointer to the current tokeninzed line linked list
2124 * @return DIRECTIVE_FOUND or NO_DIRECTIVE_FOUND
H. Peter Anvin70653092007-10-19 14:42:29 -07002125 *
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002126 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002127static int do_directive(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00002128{
H. Peter Anvin4169a472007-09-12 01:29:43 +00002129 enum preproc_token i;
2130 int j;
H. Peter Anvin70055962007-10-11 00:05:31 -07002131 bool err;
2132 int nparam;
2133 bool nolist;
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07002134 bool casesense;
H. Peter Anvin8cfdb9d2007-09-14 18:36:01 -07002135 int k, m;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002136 int offset;
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08002137 char *p, *pp;
2138 const char *mname;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002139 Include *inc;
2140 Context *ctx;
2141 Cond *cond;
H. Peter Anvin97a23472007-09-16 17:57:25 -07002142 MMacro *mmac, **mmhead;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002143 Token *t, *tt, *param_start, *macro_start, *last, **tptr, *origline;
2144 Line *l;
2145 struct tokenval tokval;
2146 expr *evalresult;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002147 MMacro *tmp_defining; /* Used when manipulating rep_nest */
H. Peter Anvinf8ba53e2007-10-11 10:11:57 -07002148 int64_t count;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07002149 size_t len;
H. Peter Anvin8e3f75e2008-09-24 00:21:58 -07002150 int severity;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002151
2152 origline = tline;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002153
H. Peter Anvineba20a72002-04-30 20:53:55 +00002154 skip_white_(tline);
H. Peter Anvinf2936d72008-06-04 15:11:23 -07002155 if (!tline || !tok_type_(tline, TOK_PREPROC_ID) ||
H. Peter Anvine2c80182005-01-15 22:15:51 +00002156 (tline->text[1] == '%' || tline->text[1] == '$'
2157 || tline->text[1] == '!'))
2158 return NO_DIRECTIVE_FOUND;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002159
H. Peter Anvin4169a472007-09-12 01:29:43 +00002160 i = pp_token_hash(tline->text);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002161
2162 /*
Cyrill Gorcunovf09116f2010-02-28 11:52:53 +03002163 * FIXME: We zap execution of PP_RMACRO, PP_IRMACRO, PP_EXITMACRO
2164 * since they are known to be buggy at moment, we need to fix them
2165 * in future release (2.09-2.10)
2166 */
2167 if (i == PP_RMACRO || i == PP_RMACRO || i == PP_EXITMACRO) {
2168 error(ERR_NONFATAL, "unknown preprocessor directive `%s'",
2169 tline->text);
2170 return NO_DIRECTIVE_FOUND;
2171 }
2172
2173 /*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002174 * If we're in a non-emitting branch of a condition construct,
H. Peter Anvin76690a12002-04-30 20:52:49 +00002175 * or walking to the end of an already terminated %rep block,
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002176 * we should ignore all directives except for condition
2177 * directives.
2178 */
H. Peter Anvin76690a12002-04-30 20:52:49 +00002179 if (((istk->conds && !emitting(istk->conds->state)) ||
H. Peter Anvine2c80182005-01-15 22:15:51 +00002180 (istk->mstk && !istk->mstk->in_progress)) && !is_condition(i)) {
2181 return NO_DIRECTIVE_FOUND;
H. Peter Anvineba20a72002-04-30 20:53:55 +00002182 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002183
2184 /*
H. Peter Anvin76690a12002-04-30 20:52:49 +00002185 * If we're defining a macro or reading a %rep block, we should
Victor van den Elzen8f1120f2008-07-16 13:41:37 +02002186 * ignore all directives except for %macro/%imacro (which nest),
2187 * %endm/%endmacro, and (only if we're in a %rep block) %endrep.
2188 * If we're in a %rep block, another %rep nests, so should be let through.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002189 */
2190 if (defining && i != PP_MACRO && i != PP_IMACRO &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002191 i != PP_RMACRO && i != PP_IRMACRO &&
H. Peter Anvine2c80182005-01-15 22:15:51 +00002192 i != PP_ENDMACRO && i != PP_ENDM &&
2193 (defining->name || (i != PP_ENDREP && i != PP_REP))) {
2194 return NO_DIRECTIVE_FOUND;
H. Peter Anvineba20a72002-04-30 20:53:55 +00002195 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002196
Charles Crayned4200be2008-07-12 16:42:33 -07002197 if (defining) {
Keith Kanios852f1ee2009-07-12 00:19:55 -05002198 if (i == PP_MACRO || i == PP_IMACRO ||
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002199 i == PP_RMACRO || i == PP_IRMACRO) {
Charles Crayned4200be2008-07-12 16:42:33 -07002200 nested_mac_count++;
2201 return NO_DIRECTIVE_FOUND;
2202 } else if (nested_mac_count > 0) {
2203 if (i == PP_ENDMACRO) {
2204 nested_mac_count--;
2205 return NO_DIRECTIVE_FOUND;
2206 }
2207 }
2208 if (!defining->name) {
2209 if (i == PP_REP) {
2210 nested_rep_count++;
2211 return NO_DIRECTIVE_FOUND;
2212 } else if (nested_rep_count > 0) {
2213 if (i == PP_ENDREP) {
2214 nested_rep_count--;
2215 return NO_DIRECTIVE_FOUND;
2216 }
2217 }
2218 }
2219 }
2220
H. Peter Anvin4169a472007-09-12 01:29:43 +00002221 switch (i) {
2222 case PP_INVALID:
H. Peter Anvine2c80182005-01-15 22:15:51 +00002223 error(ERR_NONFATAL, "unknown preprocessor directive `%s'",
2224 tline->text);
2225 return NO_DIRECTIVE_FOUND; /* didn't get it */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002226
H. Peter Anvine2c80182005-01-15 22:15:51 +00002227 case PP_STACKSIZE:
2228 /* Directive to tell NASM what the default stack size is. The
2229 * default is for a 16-bit stack, and this can be overriden with
2230 * %stacksize large.
H. Peter Anvine2c80182005-01-15 22:15:51 +00002231 */
2232 tline = tline->next;
2233 if (tline && tline->type == TOK_WHITESPACE)
2234 tline = tline->next;
2235 if (!tline || tline->type != TOK_ID) {
2236 error(ERR_NONFATAL, "`%%stacksize' missing size parameter");
2237 free_tlist(origline);
2238 return DIRECTIVE_FOUND;
2239 }
2240 if (nasm_stricmp(tline->text, "flat") == 0) {
2241 /* All subsequent ARG directives are for a 32-bit stack */
2242 StackSize = 4;
2243 StackPointer = "ebp";
2244 ArgOffset = 8;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002245 LocalOffset = 0;
Charles Crayne7eaf9192007-11-08 22:11:14 -08002246 } else if (nasm_stricmp(tline->text, "flat64") == 0) {
2247 /* All subsequent ARG directives are for a 64-bit stack */
2248 StackSize = 8;
2249 StackPointer = "rbp";
Per Jessen53252e02010-02-11 00:16:59 +03002250 ArgOffset = 16;
Charles Crayne7eaf9192007-11-08 22:11:14 -08002251 LocalOffset = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002252 } else if (nasm_stricmp(tline->text, "large") == 0) {
2253 /* All subsequent ARG directives are for a 16-bit stack,
2254 * far function call.
2255 */
2256 StackSize = 2;
2257 StackPointer = "bp";
2258 ArgOffset = 4;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002259 LocalOffset = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002260 } else if (nasm_stricmp(tline->text, "small") == 0) {
2261 /* All subsequent ARG directives are for a 16-bit stack,
2262 * far function call. We don't support near functions.
2263 */
2264 StackSize = 2;
2265 StackPointer = "bp";
2266 ArgOffset = 6;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002267 LocalOffset = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002268 } else {
2269 error(ERR_NONFATAL, "`%%stacksize' invalid size type");
2270 free_tlist(origline);
2271 return DIRECTIVE_FOUND;
2272 }
2273 free_tlist(origline);
2274 return DIRECTIVE_FOUND;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002275
H. Peter Anvine2c80182005-01-15 22:15:51 +00002276 case PP_ARG:
2277 /* TASM like ARG directive to define arguments to functions, in
2278 * the following form:
2279 *
2280 * ARG arg1:WORD, arg2:DWORD, arg4:QWORD
2281 */
2282 offset = ArgOffset;
2283 do {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00002284 char *arg, directive[256];
H. Peter Anvine2c80182005-01-15 22:15:51 +00002285 int size = StackSize;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002286
H. Peter Anvine2c80182005-01-15 22:15:51 +00002287 /* Find the argument name */
2288 tline = tline->next;
2289 if (tline && tline->type == TOK_WHITESPACE)
2290 tline = tline->next;
2291 if (!tline || tline->type != TOK_ID) {
2292 error(ERR_NONFATAL, "`%%arg' missing argument parameter");
2293 free_tlist(origline);
2294 return DIRECTIVE_FOUND;
2295 }
2296 arg = tline->text;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002297
H. Peter Anvine2c80182005-01-15 22:15:51 +00002298 /* Find the argument size type */
2299 tline = tline->next;
2300 if (!tline || tline->type != TOK_OTHER
2301 || tline->text[0] != ':') {
2302 error(ERR_NONFATAL,
2303 "Syntax error processing `%%arg' directive");
2304 free_tlist(origline);
2305 return DIRECTIVE_FOUND;
2306 }
2307 tline = tline->next;
2308 if (!tline || tline->type != TOK_ID) {
2309 error(ERR_NONFATAL, "`%%arg' missing size type parameter");
2310 free_tlist(origline);
2311 return DIRECTIVE_FOUND;
2312 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002313
H. Peter Anvine2c80182005-01-15 22:15:51 +00002314 /* Allow macro expansion of type parameter */
Keith Kaniosb7a89542007-04-12 02:40:54 +00002315 tt = tokenize(tline->text);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002316 tt = expand_smacro(tt);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002317 size = parse_size(tt->text);
2318 if (!size) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002319 error(ERR_NONFATAL,
2320 "Invalid size type for `%%arg' missing directive");
2321 free_tlist(tt);
2322 free_tlist(origline);
2323 return DIRECTIVE_FOUND;
2324 }
2325 free_tlist(tt);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002326
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002327 /* Round up to even stack slots */
2328 size = ALIGN(size, StackSize);
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002329
H. Peter Anvine2c80182005-01-15 22:15:51 +00002330 /* Now define the macro for the argument */
2331 snprintf(directive, sizeof(directive), "%%define %s (%s+%d)",
2332 arg, StackPointer, offset);
Keith Kaniosb7a89542007-04-12 02:40:54 +00002333 do_directive(tokenize(directive));
H. Peter Anvine2c80182005-01-15 22:15:51 +00002334 offset += size;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002335
H. Peter Anvine2c80182005-01-15 22:15:51 +00002336 /* Move to the next argument in the list */
2337 tline = tline->next;
2338 if (tline && tline->type == TOK_WHITESPACE)
2339 tline = tline->next;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002340 } while (tline && tline->type == TOK_OTHER && tline->text[0] == ',');
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002341 ArgOffset = offset;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002342 free_tlist(origline);
2343 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002344
H. Peter Anvine2c80182005-01-15 22:15:51 +00002345 case PP_LOCAL:
2346 /* TASM like LOCAL directive to define local variables for a
2347 * function, in the following form:
2348 *
2349 * LOCAL local1:WORD, local2:DWORD, local4:QWORD = LocalSize
2350 *
2351 * The '= LocalSize' at the end is ignored by NASM, but is
2352 * required by TASM to define the local parameter size (and used
2353 * by the TASM macro package).
2354 */
2355 offset = LocalOffset;
2356 do {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00002357 char *local, directive[256];
H. Peter Anvine2c80182005-01-15 22:15:51 +00002358 int size = StackSize;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002359
H. Peter Anvine2c80182005-01-15 22:15:51 +00002360 /* Find the argument name */
2361 tline = tline->next;
2362 if (tline && tline->type == TOK_WHITESPACE)
2363 tline = tline->next;
2364 if (!tline || tline->type != TOK_ID) {
2365 error(ERR_NONFATAL,
2366 "`%%local' missing argument parameter");
2367 free_tlist(origline);
2368 return DIRECTIVE_FOUND;
2369 }
2370 local = tline->text;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002371
H. Peter Anvine2c80182005-01-15 22:15:51 +00002372 /* Find the argument size type */
2373 tline = tline->next;
2374 if (!tline || tline->type != TOK_OTHER
2375 || tline->text[0] != ':') {
2376 error(ERR_NONFATAL,
2377 "Syntax error processing `%%local' directive");
2378 free_tlist(origline);
2379 return DIRECTIVE_FOUND;
2380 }
2381 tline = tline->next;
2382 if (!tline || tline->type != TOK_ID) {
2383 error(ERR_NONFATAL,
2384 "`%%local' missing size type parameter");
2385 free_tlist(origline);
2386 return DIRECTIVE_FOUND;
2387 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00002388
H. Peter Anvine2c80182005-01-15 22:15:51 +00002389 /* Allow macro expansion of type parameter */
Keith Kaniosb7a89542007-04-12 02:40:54 +00002390 tt = tokenize(tline->text);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002391 tt = expand_smacro(tt);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002392 size = parse_size(tt->text);
2393 if (!size) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002394 error(ERR_NONFATAL,
2395 "Invalid size type for `%%local' missing directive");
2396 free_tlist(tt);
2397 free_tlist(origline);
2398 return DIRECTIVE_FOUND;
2399 }
2400 free_tlist(tt);
H. Peter Anvin734b1882002-04-30 21:01:08 +00002401
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002402 /* Round up to even stack slots */
2403 size = ALIGN(size, StackSize);
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002404
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002405 offset += size; /* Negative offset, increment before */
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002406
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002407 /* Now define the macro for the argument */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002408 snprintf(directive, sizeof(directive), "%%define %s (%s-%d)",
2409 local, StackPointer, offset);
Keith Kaniosb7a89542007-04-12 02:40:54 +00002410 do_directive(tokenize(directive));
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002411
H. Peter Anvine2c80182005-01-15 22:15:51 +00002412 /* Now define the assign to setup the enter_c macro correctly */
2413 snprintf(directive, sizeof(directive),
2414 "%%assign %%$localsize %%$localsize+%d", size);
Keith Kaniosb7a89542007-04-12 02:40:54 +00002415 do_directive(tokenize(directive));
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002416
H. Peter Anvine2c80182005-01-15 22:15:51 +00002417 /* Move to the next argument in the list */
2418 tline = tline->next;
2419 if (tline && tline->type == TOK_WHITESPACE)
2420 tline = tline->next;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002421 } while (tline && tline->type == TOK_OTHER && tline->text[0] == ',');
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002422 LocalOffset = offset;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002423 free_tlist(origline);
2424 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002425
H. Peter Anvine2c80182005-01-15 22:15:51 +00002426 case PP_CLEAR:
2427 if (tline->next)
H. Peter Anvin917a3492008-09-24 09:14:49 -07002428 error(ERR_WARNING|ERR_PASS1,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002429 "trailing garbage after `%%clear' ignored");
2430 free_macros();
2431 init_macros();
H. Peter Anvine2c80182005-01-15 22:15:51 +00002432 free_tlist(origline);
2433 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002434
H. Peter Anvin418ca702008-05-30 10:42:30 -07002435 case PP_DEPEND:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002436 t = tline->next = expand_smacro(tline->next);
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002437 skip_white_(t);
2438 if (!t || (t->type != TOK_STRING &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002439 t->type != TOK_INTERNAL_STRING)) {
H. Peter Anvin418ca702008-05-30 10:42:30 -07002440 error(ERR_NONFATAL, "`%%depend' expects a file name");
2441 free_tlist(origline);
2442 return DIRECTIVE_FOUND; /* but we did _something_ */
2443 }
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002444 if (t->next)
H. Peter Anvin917a3492008-09-24 09:14:49 -07002445 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvin418ca702008-05-30 10:42:30 -07002446 "trailing garbage after `%%depend' ignored");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002447 p = t->text;
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002448 if (t->type != TOK_INTERNAL_STRING)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002449 nasm_unquote_cstr(p, i);
2450 if (dephead && !in_list(*dephead, p)) {
2451 StrList *sl = nasm_malloc(strlen(p)+1+sizeof sl->next);
2452 sl->next = NULL;
2453 strcpy(sl->str, p);
2454 *deptail = sl;
2455 deptail = &sl->next;
2456 }
2457 free_tlist(origline);
H. Peter Anvin418ca702008-05-30 10:42:30 -07002458 return DIRECTIVE_FOUND;
2459
2460 case PP_INCLUDE:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002461 t = tline->next = expand_smacro(tline->next);
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002462 skip_white_(t);
H. Peter Anvind2456592008-06-19 15:04:18 -07002463
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002464 if (!t || (t->type != TOK_STRING &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002465 t->type != TOK_INTERNAL_STRING)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002466 error(ERR_NONFATAL, "`%%include' expects a file name");
2467 free_tlist(origline);
2468 return DIRECTIVE_FOUND; /* but we did _something_ */
2469 }
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002470 if (t->next)
H. Peter Anvin917a3492008-09-24 09:14:49 -07002471 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +00002472 "trailing garbage after `%%include' ignored");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002473 p = t->text;
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002474 if (t->type != TOK_INTERNAL_STRING)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002475 nasm_unquote_cstr(p, i);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002476 inc = nasm_malloc(sizeof(Include));
2477 inc->next = istk;
2478 inc->conds = NULL;
H. Peter Anvin2b1c3b92008-06-06 10:38:46 -07002479 inc->fp = inc_fopen(p, dephead, &deptail, pass == 0);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002480 if (!inc->fp) {
2481 /* -MG given but file not found */
2482 nasm_free(inc);
2483 } else {
2484 inc->fname = src_set_fname(nasm_strdup(p));
2485 inc->lineno = src_set_linnum(0);
2486 inc->lineinc = 1;
2487 inc->expansion = NULL;
2488 inc->mstk = NULL;
2489 istk = inc;
2490 list->uplevel(LIST_INCLUDE);
2491 }
2492 free_tlist(origline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002493 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002494
H. Peter Anvind2456592008-06-19 15:04:18 -07002495 case PP_USE:
H. Peter Anvinf4ae5ad2008-06-19 18:39:24 -07002496 {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002497 static macros_t *use_pkg;
2498 const char *pkg_macro = NULL;
H. Peter Anvinf4ae5ad2008-06-19 18:39:24 -07002499
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002500 tline = tline->next;
2501 skip_white_(tline);
2502 tline = expand_id(tline);
H. Peter Anvind2456592008-06-19 15:04:18 -07002503
H. Peter Anvin264b7b92008-10-24 16:38:17 -07002504 if (!tline || (tline->type != TOK_STRING &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002505 tline->type != TOK_INTERNAL_STRING &&
2506 tline->type != TOK_ID)) {
H. Peter Anvin926fc402008-06-19 16:26:12 -07002507 error(ERR_NONFATAL, "`%%use' expects a package name");
H. Peter Anvind2456592008-06-19 15:04:18 -07002508 free_tlist(origline);
2509 return DIRECTIVE_FOUND; /* but we did _something_ */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002510 }
H. Peter Anvin264b7b92008-10-24 16:38:17 -07002511 if (tline->next)
H. Peter Anvin917a3492008-09-24 09:14:49 -07002512 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvind2456592008-06-19 15:04:18 -07002513 "trailing garbage after `%%use' ignored");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002514 if (tline->type == TOK_STRING)
2515 nasm_unquote_cstr(tline->text, i);
2516 use_pkg = nasm_stdmac_find_package(tline->text);
2517 if (!use_pkg)
2518 error(ERR_NONFATAL, "unknown `%%use' package: %s", tline->text);
2519 else
2520 pkg_macro = (char *)use_pkg + 1; /* The first string will be <%define>__USE_*__ */
Victor van den Elzen35eb2ea2010-03-10 22:33:48 +01002521 if (use_pkg && ! smacro_defined(NULL, pkg_macro, 0, NULL, true)) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002522 /* Not already included, go ahead and include it */
2523 stdmacpos = use_pkg;
2524 }
2525 free_tlist(origline);
H. Peter Anvind2456592008-06-19 15:04:18 -07002526 return DIRECTIVE_FOUND;
H. Peter Anvinf4ae5ad2008-06-19 18:39:24 -07002527 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002528 case PP_PUSH:
H. Peter Anvine2c80182005-01-15 22:15:51 +00002529 case PP_REPL:
H. Peter Anvin42b56392008-10-24 16:24:21 -07002530 case PP_POP:
H. Peter Anvine2c80182005-01-15 22:15:51 +00002531 tline = tline->next;
2532 skip_white_(tline);
2533 tline = expand_id(tline);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002534 if (tline) {
2535 if (!tok_type_(tline, TOK_ID)) {
2536 error(ERR_NONFATAL, "`%s' expects a context identifier",
2537 pp_directives[i]);
2538 free_tlist(origline);
2539 return DIRECTIVE_FOUND; /* but we did _something_ */
2540 }
2541 if (tline->next)
2542 error(ERR_WARNING|ERR_PASS1,
2543 "trailing garbage after `%s' ignored",
2544 pp_directives[i]);
2545 p = nasm_strdup(tline->text);
2546 } else {
2547 p = NULL; /* Anonymous */
2548 }
H. Peter Anvin42b56392008-10-24 16:24:21 -07002549
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002550 if (i == PP_PUSH) {
2551 ctx = nasm_malloc(sizeof(Context));
2552 ctx->next = cstk;
2553 hash_init(&ctx->localmac, HASH_SMALL);
2554 ctx->name = p;
2555 ctx->number = unique++;
2556 cstk = ctx;
2557 } else {
2558 /* %pop or %repl */
2559 if (!cstk) {
2560 error(ERR_NONFATAL, "`%s': context stack is empty",
2561 pp_directives[i]);
2562 } else if (i == PP_POP) {
2563 if (p && (!cstk->name || nasm_stricmp(p, cstk->name)))
2564 error(ERR_NONFATAL, "`%%pop' in wrong context: %s, "
2565 "expected %s",
2566 cstk->name ? cstk->name : "anonymous", p);
2567 else
2568 ctx_pop();
2569 } else {
2570 /* i == PP_REPL */
2571 nasm_free(cstk->name);
2572 cstk->name = p;
2573 p = NULL;
2574 }
2575 nasm_free(p);
2576 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002577 free_tlist(origline);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002578 return DIRECTIVE_FOUND;
H. Peter Anvin8e3f75e2008-09-24 00:21:58 -07002579 case PP_FATAL:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002580 severity = ERR_FATAL;
2581 goto issue_error;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002582 case PP_ERROR:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002583 severity = ERR_NONFATAL;
2584 goto issue_error;
H. Peter Anvin7df04172008-06-10 18:27:38 -07002585 case PP_WARNING:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002586 severity = ERR_WARNING|ERR_WARN_USER;
2587 goto issue_error;
H. Peter Anvin8e3f75e2008-09-24 00:21:58 -07002588
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002589issue_error:
H. Peter Anvin7df04172008-06-10 18:27:38 -07002590 {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002591 /* Only error out if this is the final pass */
2592 if (pass != 2 && i != PP_FATAL)
2593 return DIRECTIVE_FOUND;
2594
2595 tline->next = expand_smacro(tline->next);
2596 tline = tline->next;
2597 skip_white_(tline);
2598 t = tline ? tline->next : NULL;
2599 skip_white_(t);
2600 if (tok_type_(tline, TOK_STRING) && !t) {
2601 /* The line contains only a quoted string */
2602 p = tline->text;
2603 nasm_unquote(p, NULL); /* Ignore NUL character truncation */
2604 error(severity, "%s", p);
2605 } else {
2606 /* Not a quoted string, or more than a quoted string */
2607 p = detoken(tline, false);
2608 error(severity, "%s", p);
2609 nasm_free(p);
2610 }
2611 free_tlist(origline);
2612 return DIRECTIVE_FOUND;
H. Peter Anvin7df04172008-06-10 18:27:38 -07002613 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00002614
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002615 CASE_PP_IF:
H. Peter Anvine2c80182005-01-15 22:15:51 +00002616 if (istk->conds && !emitting(istk->conds->state))
2617 j = COND_NEVER;
2618 else {
2619 j = if_condition(tline->next, i);
2620 tline->next = NULL; /* it got freed */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002621 j = j < 0 ? COND_NEVER : j ? COND_IF_TRUE : COND_IF_FALSE;
2622 }
2623 cond = nasm_malloc(sizeof(Cond));
2624 cond->next = istk->conds;
2625 cond->state = j;
2626 istk->conds = cond;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002627 if(istk->mstk)
Keith Kanios3c0d91f2009-10-25 13:28:03 -05002628 istk->mstk->condcnt ++;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002629 free_tlist(origline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002630 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002631
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002632 CASE_PP_ELIF:
H. Peter Anvine2c80182005-01-15 22:15:51 +00002633 if (!istk->conds)
H. Peter Anvin4169a472007-09-12 01:29:43 +00002634 error(ERR_FATAL, "`%s': no matching `%%if'", pp_directives[i]);
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 istk->conds->state = COND_DONE;
2638 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002639
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002640 case COND_DONE:
2641 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_ELSE_TRUE:
2645 case COND_ELSE_FALSE:
2646 error_precond(ERR_WARNING|ERR_PASS1,
2647 "`%%elif' after `%%else' ignored");
2648 istk->conds->state = COND_NEVER;
2649 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002650
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002651 case COND_IF_FALSE:
2652 /*
2653 * IMPORTANT: In the case of %if, we will already have
2654 * called expand_mmac_params(); however, if we're
2655 * processing an %elif we must have been in a
2656 * non-emitting mode, which would have inhibited
2657 * the normal invocation of expand_mmac_params().
2658 * Therefore, we have to do it explicitly here.
2659 */
2660 j = if_condition(expand_mmac_params(tline->next), i);
2661 tline->next = NULL; /* it got freed */
2662 istk->conds->state =
2663 j < 0 ? COND_NEVER : j ? COND_IF_TRUE : COND_IF_FALSE;
2664 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002665 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002666 free_tlist(origline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002667 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002668
H. Peter Anvine2c80182005-01-15 22:15:51 +00002669 case PP_ELSE:
2670 if (tline->next)
H. Peter Anvin917a3492008-09-24 09:14:49 -07002671 error_precond(ERR_WARNING|ERR_PASS1,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002672 "trailing garbage after `%%else' ignored");
H. Peter Anvine2c80182005-01-15 22:15:51 +00002673 if (!istk->conds)
2674 error(ERR_FATAL, "`%%else': no matching `%%if'");
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002675 switch(istk->conds->state) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002676 case COND_IF_TRUE:
2677 case COND_DONE:
2678 istk->conds->state = COND_ELSE_FALSE;
2679 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002680
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002681 case COND_NEVER:
2682 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002683
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002684 case COND_IF_FALSE:
2685 istk->conds->state = COND_ELSE_TRUE;
2686 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002687
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002688 case COND_ELSE_TRUE:
2689 case COND_ELSE_FALSE:
2690 error_precond(ERR_WARNING|ERR_PASS1,
2691 "`%%else' after `%%else' ignored.");
2692 istk->conds->state = COND_NEVER;
2693 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002694 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002695 free_tlist(origline);
2696 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002697
H. Peter Anvine2c80182005-01-15 22:15:51 +00002698 case PP_ENDIF:
2699 if (tline->next)
H. Peter Anvin917a3492008-09-24 09:14:49 -07002700 error_precond(ERR_WARNING|ERR_PASS1,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002701 "trailing garbage after `%%endif' ignored");
H. Peter Anvine2c80182005-01-15 22:15:51 +00002702 if (!istk->conds)
2703 error(ERR_FATAL, "`%%endif': no matching `%%if'");
2704 cond = istk->conds;
2705 istk->conds = cond->next;
2706 nasm_free(cond);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002707 if(istk->mstk)
Keith Kanios3c0d91f2009-10-25 13:28:03 -05002708 istk->mstk->condcnt --;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002709 free_tlist(origline);
2710 return DIRECTIVE_FOUND;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002711
H. Peter Anvindb8f96e2009-07-15 09:07:29 -04002712 case PP_RMACRO:
2713 case PP_IRMACRO:
H. Peter Anvine2c80182005-01-15 22:15:51 +00002714 case PP_MACRO:
2715 case PP_IMACRO:
H. Peter Anvina26433d2008-07-16 14:40:01 -07002716 if (defining) {
H. Peter Anvindb8f96e2009-07-15 09:07:29 -04002717 error(ERR_FATAL, "`%s': already defining a macro",
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002718 pp_directives[i]);
2719 return DIRECTIVE_FOUND;
2720 }
2721 defining = nasm_malloc(sizeof(MMacro));
2722 defining->max_depth =
2723 (i == PP_RMACRO) || (i == PP_IRMACRO) ? DEADMAN_LIMIT : 0;
2724 defining->casesense = (i == PP_MACRO) || (i == PP_RMACRO);
2725 if (!parse_mmacro_spec(tline, defining, pp_directives[i])) {
2726 nasm_free(defining);
2727 defining = NULL;
2728 return DIRECTIVE_FOUND;
2729 }
H. Peter Anvina26433d2008-07-16 14:40:01 -07002730
H. Peter Anvin166c2472008-05-28 12:28:58 -07002731 mmac = (MMacro *) hash_findix(&mmacros, defining->name);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002732 while (mmac) {
2733 if (!strcmp(mmac->name, defining->name) &&
2734 (mmac->nparam_min <= defining->nparam_max
2735 || defining->plus)
2736 && (defining->nparam_min <= mmac->nparam_max
2737 || mmac->plus)) {
H. Peter Anvin917a3492008-09-24 09:14:49 -07002738 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +00002739 "redefining multi-line macro `%s'", defining->name);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002740 return DIRECTIVE_FOUND;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002741 }
2742 mmac = mmac->next;
2743 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002744 free_tlist(origline);
2745 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002746
H. Peter Anvine2c80182005-01-15 22:15:51 +00002747 case PP_ENDM:
2748 case PP_ENDMACRO:
Victor van den Elzen8f1120f2008-07-16 13:41:37 +02002749 if (! (defining && defining->name)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002750 error(ERR_NONFATAL, "`%s': not defining a macro", tline->text);
2751 return DIRECTIVE_FOUND;
2752 }
Keith Kanios852f1ee2009-07-12 00:19:55 -05002753 mmhead = (MMacro **) hash_findi_add(&mmacros, defining->name);
H. Peter Anvin97a23472007-09-16 17:57:25 -07002754 defining->next = *mmhead;
Keith Kanios852f1ee2009-07-12 00:19:55 -05002755 *mmhead = defining;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002756 defining = NULL;
2757 free_tlist(origline);
2758 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002759
H. Peter Anvin89cee572009-07-15 09:16:54 -04002760 case PP_EXITMACRO:
Keith Kanios852f1ee2009-07-12 00:19:55 -05002761 /*
2762 * We must search along istk->expansion until we hit a
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002763 * macro-end marker for a macro with a name. Then we
Keith Kanios3c0d91f2009-10-25 13:28:03 -05002764 * bypass all lines between exitmacro and endmacro.
Keith Kanios852f1ee2009-07-12 00:19:55 -05002765 */
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04002766 list_for_each(l, istk->expansion)
Keith Kanios852f1ee2009-07-12 00:19:55 -05002767 if (l->finishes && l->finishes->name)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002768 break;
Keith Kanios852f1ee2009-07-12 00:19:55 -05002769
2770 if (l) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002771 /*
2772 * Remove all conditional entries relative to this
2773 * macro invocation. (safe to do in this context)
2774 */
Keith Kanios3c0d91f2009-10-25 13:28:03 -05002775 for ( ; l->finishes->condcnt > 0; l->finishes->condcnt --) {
2776 cond = istk->conds;
2777 istk->conds = cond->next;
2778 nasm_free(cond);
2779 }
2780 istk->expansion = l;
Keith Kanios852f1ee2009-07-12 00:19:55 -05002781 } else {
2782 error(ERR_NONFATAL, "`%%exitmacro' not within `%%macro' block");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002783 }
Keith Kanios852f1ee2009-07-12 00:19:55 -05002784 free_tlist(origline);
2785 return DIRECTIVE_FOUND;
2786
H. Peter Anvina26433d2008-07-16 14:40:01 -07002787 case PP_UNMACRO:
2788 case PP_UNIMACRO:
2789 {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002790 MMacro **mmac_p;
2791 MMacro spec;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002792
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002793 spec.casesense = (i == PP_UNMACRO);
2794 if (!parse_mmacro_spec(tline, &spec, pp_directives[i])) {
2795 return DIRECTIVE_FOUND;
2796 }
H. Peter Anvina26433d2008-07-16 14:40:01 -07002797 mmac_p = (MMacro **) hash_findi(&mmacros, spec.name, NULL);
2798 while (mmac_p && *mmac_p) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002799 mmac = *mmac_p;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002800 if (mmac->casesense == spec.casesense &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002801 !mstrcmp(mmac->name, spec.name, spec.casesense) &&
H. Peter Anvina26433d2008-07-16 14:40:01 -07002802 mmac->nparam_min == spec.nparam_min &&
2803 mmac->nparam_max == spec.nparam_max &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002804 mmac->plus == spec.plus) {
2805 *mmac_p = mmac->next;
2806 free_mmacro(mmac);
2807 } else {
2808 mmac_p = &mmac->next;
2809 }
2810 }
2811 free_tlist(origline);
2812 free_tlist(spec.dlist);
2813 return DIRECTIVE_FOUND;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002814 }
2815
H. Peter Anvine2c80182005-01-15 22:15:51 +00002816 case PP_ROTATE:
2817 if (tline->next && tline->next->type == TOK_WHITESPACE)
2818 tline = tline->next;
H. Peter Anvin89cee572009-07-15 09:16:54 -04002819 if (!tline->next) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002820 free_tlist(origline);
2821 error(ERR_NONFATAL, "`%%rotate' missing rotate count");
2822 return DIRECTIVE_FOUND;
2823 }
2824 t = expand_smacro(tline->next);
2825 tline->next = NULL;
2826 free_tlist(origline);
2827 tline = t;
2828 tptr = &t;
2829 tokval.t_type = TOKEN_INVALID;
2830 evalresult =
2831 evaluate(ppscan, tptr, &tokval, NULL, pass, error, NULL);
2832 free_tlist(tline);
2833 if (!evalresult)
2834 return DIRECTIVE_FOUND;
2835 if (tokval.t_type)
H. Peter Anvin917a3492008-09-24 09:14:49 -07002836 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +00002837 "trailing garbage after expression ignored");
2838 if (!is_simple(evalresult)) {
2839 error(ERR_NONFATAL, "non-constant value given to `%%rotate'");
2840 return DIRECTIVE_FOUND;
2841 }
2842 mmac = istk->mstk;
2843 while (mmac && !mmac->name) /* avoid mistaking %reps for macros */
2844 mmac = mmac->next_active;
2845 if (!mmac) {
2846 error(ERR_NONFATAL, "`%%rotate' invoked outside a macro call");
2847 } else if (mmac->nparam == 0) {
2848 error(ERR_NONFATAL,
2849 "`%%rotate' invoked within macro without parameters");
2850 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002851 int rotate = mmac->rotate + reloc_value(evalresult);
H. Peter Anvin734b1882002-04-30 21:01:08 +00002852
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002853 rotate %= (int)mmac->nparam;
2854 if (rotate < 0)
2855 rotate += mmac->nparam;
H. Peter Anvin70653092007-10-19 14:42:29 -07002856
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002857 mmac->rotate = rotate;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002858 }
2859 return DIRECTIVE_FOUND;
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00002860
H. Peter Anvine2c80182005-01-15 22:15:51 +00002861 case PP_REP:
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002862 nolist = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002863 do {
2864 tline = tline->next;
2865 } while (tok_type_(tline, TOK_WHITESPACE));
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00002866
H. Peter Anvine2c80182005-01-15 22:15:51 +00002867 if (tok_type_(tline, TOK_ID) &&
2868 nasm_stricmp(tline->text, ".nolist") == 0) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002869 nolist = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002870 do {
2871 tline = tline->next;
2872 } while (tok_type_(tline, TOK_WHITESPACE));
2873 }
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00002874
H. Peter Anvine2c80182005-01-15 22:15:51 +00002875 if (tline) {
2876 t = expand_smacro(tline);
2877 tptr = &t;
2878 tokval.t_type = TOKEN_INVALID;
2879 evalresult =
2880 evaluate(ppscan, tptr, &tokval, NULL, pass, error, NULL);
2881 if (!evalresult) {
2882 free_tlist(origline);
2883 return DIRECTIVE_FOUND;
2884 }
2885 if (tokval.t_type)
H. Peter Anvin917a3492008-09-24 09:14:49 -07002886 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +00002887 "trailing garbage after expression ignored");
2888 if (!is_simple(evalresult)) {
2889 error(ERR_NONFATAL, "non-constant value given to `%%rep'");
2890 return DIRECTIVE_FOUND;
2891 }
H. Peter Anvinf8ba53e2007-10-11 10:11:57 -07002892 count = reloc_value(evalresult) + 1;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002893 } else {
2894 error(ERR_NONFATAL, "`%%rep' expects a repeat count");
H. Peter Anvinf8ba53e2007-10-11 10:11:57 -07002895 count = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002896 }
2897 free_tlist(origline);
H. Peter Anvin734b1882002-04-30 21:01:08 +00002898
H. Peter Anvine2c80182005-01-15 22:15:51 +00002899 tmp_defining = defining;
2900 defining = nasm_malloc(sizeof(MMacro));
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002901 defining->prev = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002902 defining->name = NULL; /* flags this macro as a %rep block */
H. Peter Anvin70055962007-10-11 00:05:31 -07002903 defining->casesense = false;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002904 defining->plus = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002905 defining->nolist = nolist;
H. Peter Anvinf8ba53e2007-10-11 10:11:57 -07002906 defining->in_progress = count;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002907 defining->max_depth = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002908 defining->nparam_min = defining->nparam_max = 0;
2909 defining->defaults = NULL;
2910 defining->dlist = NULL;
2911 defining->expansion = NULL;
2912 defining->next_active = istk->mstk;
2913 defining->rep_nest = tmp_defining;
2914 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002915
H. Peter Anvine2c80182005-01-15 22:15:51 +00002916 case PP_ENDREP:
2917 if (!defining || defining->name) {
2918 error(ERR_NONFATAL, "`%%endrep': no matching `%%rep'");
2919 return DIRECTIVE_FOUND;
2920 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00002921
H. Peter Anvine2c80182005-01-15 22:15:51 +00002922 /*
2923 * Now we have a "macro" defined - although it has no name
2924 * and we won't be entering it in the hash tables - we must
2925 * push a macro-end marker for it on to istk->expansion.
2926 * After that, it will take care of propagating itself (a
2927 * macro-end marker line for a macro which is really a %rep
2928 * block will cause the macro to be re-expanded, complete
2929 * with another macro-end marker to ensure the process
2930 * continues) until the whole expansion is forcibly removed
2931 * from istk->expansion by a %exitrep.
2932 */
2933 l = nasm_malloc(sizeof(Line));
2934 l->next = istk->expansion;
2935 l->finishes = defining;
2936 l->first = NULL;
2937 istk->expansion = l;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002938
H. Peter Anvine2c80182005-01-15 22:15:51 +00002939 istk->mstk = defining;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002940
H. Peter Anvine2c80182005-01-15 22:15:51 +00002941 list->uplevel(defining->nolist ? LIST_MACRO_NOLIST : LIST_MACRO);
2942 tmp_defining = defining;
2943 defining = defining->rep_nest;
2944 free_tlist(origline);
2945 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002946
H. Peter Anvine2c80182005-01-15 22:15:51 +00002947 case PP_EXITREP:
2948 /*
2949 * We must search along istk->expansion until we hit a
2950 * macro-end marker for a macro with no name. Then we set
2951 * its `in_progress' flag to 0.
2952 */
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04002953 list_for_each(l, istk->expansion)
H. Peter Anvine2c80182005-01-15 22:15:51 +00002954 if (l->finishes && !l->finishes->name)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002955 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002956
H. Peter Anvine2c80182005-01-15 22:15:51 +00002957 if (l)
Charles Crayned4200be2008-07-12 16:42:33 -07002958 l->finishes->in_progress = 1;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002959 else
2960 error(ERR_NONFATAL, "`%%exitrep' not within `%%rep' block");
2961 free_tlist(origline);
2962 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002963
H. Peter Anvine2c80182005-01-15 22:15:51 +00002964 case PP_XDEFINE:
2965 case PP_IXDEFINE:
2966 case PP_DEFINE:
2967 case PP_IDEFINE:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002968 casesense = (i == PP_DEFINE || i == PP_XDEFINE);
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07002969
H. Peter Anvine2c80182005-01-15 22:15:51 +00002970 tline = tline->next;
2971 skip_white_(tline);
2972 tline = expand_id(tline);
2973 if (!tline || (tline->type != TOK_ID &&
2974 (tline->type != TOK_PREPROC_ID ||
2975 tline->text[1] != '$'))) {
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07002976 error(ERR_NONFATAL, "`%s' expects a macro identifier",
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002977 pp_directives[i]);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002978 free_tlist(origline);
2979 return DIRECTIVE_FOUND;
2980 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00002981
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08002982 ctx = get_ctx(tline->text, &mname, false);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002983 last = tline;
2984 param_start = tline = tline->next;
2985 nparam = 0;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002986
H. Peter Anvine2c80182005-01-15 22:15:51 +00002987 /* Expand the macro definition now for %xdefine and %ixdefine */
2988 if ((i == PP_XDEFINE) || (i == PP_IXDEFINE))
2989 tline = expand_smacro(tline);
H. Peter Anvin734b1882002-04-30 21:01:08 +00002990
H. Peter Anvine2c80182005-01-15 22:15:51 +00002991 if (tok_is_(tline, "(")) {
2992 /*
2993 * This macro has parameters.
2994 */
H. Peter Anvin734b1882002-04-30 21:01:08 +00002995
H. Peter Anvine2c80182005-01-15 22:15:51 +00002996 tline = tline->next;
2997 while (1) {
2998 skip_white_(tline);
2999 if (!tline) {
3000 error(ERR_NONFATAL, "parameter identifier expected");
3001 free_tlist(origline);
3002 return DIRECTIVE_FOUND;
3003 }
3004 if (tline->type != TOK_ID) {
3005 error(ERR_NONFATAL,
3006 "`%s': parameter identifier expected",
3007 tline->text);
3008 free_tlist(origline);
3009 return DIRECTIVE_FOUND;
3010 }
3011 tline->type = TOK_SMAC_PARAM + nparam++;
3012 tline = tline->next;
3013 skip_white_(tline);
3014 if (tok_is_(tline, ",")) {
3015 tline = tline->next;
H. Peter Anvinca348b62008-07-23 10:49:26 -04003016 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003017 if (!tok_is_(tline, ")")) {
3018 error(ERR_NONFATAL,
3019 "`)' expected to terminate macro template");
3020 free_tlist(origline);
3021 return DIRECTIVE_FOUND;
3022 }
3023 break;
3024 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00003025 }
3026 last = tline;
3027 tline = tline->next;
3028 }
3029 if (tok_type_(tline, TOK_WHITESPACE))
3030 last = tline, tline = tline->next;
3031 macro_start = NULL;
3032 last->next = NULL;
3033 t = tline;
3034 while (t) {
3035 if (t->type == TOK_ID) {
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04003036 list_for_each(tt, param_start)
H. Peter Anvine2c80182005-01-15 22:15:51 +00003037 if (tt->type >= TOK_SMAC_PARAM &&
3038 !strcmp(tt->text, t->text))
3039 t->type = tt->type;
3040 }
3041 tt = t->next;
3042 t->next = macro_start;
3043 macro_start = t;
3044 t = tt;
3045 }
3046 /*
3047 * Good. We now have a macro name, a parameter count, and a
3048 * token list (in reverse order) for an expansion. We ought
3049 * to be OK just to create an SMacro, store it, and let
3050 * free_tlist have the rest of the line (which we have
3051 * carefully re-terminated after chopping off the expansion
3052 * from the end).
3053 */
H. Peter Anvin4db5a162007-10-11 13:42:09 -07003054 define_smacro(ctx, mname, casesense, nparam, macro_start);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003055 free_tlist(origline);
3056 return DIRECTIVE_FOUND;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003057
H. Peter Anvine2c80182005-01-15 22:15:51 +00003058 case PP_UNDEF:
3059 tline = tline->next;
3060 skip_white_(tline);
3061 tline = expand_id(tline);
3062 if (!tline || (tline->type != TOK_ID &&
3063 (tline->type != TOK_PREPROC_ID ||
3064 tline->text[1] != '$'))) {
3065 error(ERR_NONFATAL, "`%%undef' expects a macro identifier");
3066 free_tlist(origline);
3067 return DIRECTIVE_FOUND;
3068 }
3069 if (tline->next) {
H. Peter Anvin917a3492008-09-24 09:14:49 -07003070 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +00003071 "trailing garbage after macro name ignored");
3072 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003073
H. Peter Anvine2c80182005-01-15 22:15:51 +00003074 /* Find the context that symbol belongs to */
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08003075 ctx = get_ctx(tline->text, &mname, false);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003076 undef_smacro(ctx, mname);
3077 free_tlist(origline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003078 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003079
H. Peter Anvin9e200162008-06-04 17:23:14 -07003080 case PP_DEFSTR:
3081 case PP_IDEFSTR:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003082 casesense = (i == PP_DEFSTR);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003083
3084 tline = tline->next;
3085 skip_white_(tline);
3086 tline = expand_id(tline);
3087 if (!tline || (tline->type != TOK_ID &&
3088 (tline->type != TOK_PREPROC_ID ||
3089 tline->text[1] != '$'))) {
3090 error(ERR_NONFATAL, "`%s' expects a macro identifier",
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003091 pp_directives[i]);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003092 free_tlist(origline);
3093 return DIRECTIVE_FOUND;
3094 }
3095
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08003096 ctx = get_ctx(tline->text, &mname, false);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003097 last = tline;
3098 tline = expand_smacro(tline->next);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003099 last->next = NULL;
H. Peter Anvin9e200162008-06-04 17:23:14 -07003100
3101 while (tok_type_(tline, TOK_WHITESPACE))
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003102 tline = delete_Token(tline);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003103
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003104 p = detoken(tline, false);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003105 macro_start = nasm_malloc(sizeof(*macro_start));
3106 macro_start->next = NULL;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003107 macro_start->text = nasm_quote(p, strlen(p));
3108 macro_start->type = TOK_STRING;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003109 macro_start->a.mac = NULL;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003110 nasm_free(p);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003111
3112 /*
3113 * We now have a macro name, an implicit parameter count of
3114 * zero, and a string token to use as an expansion. Create
3115 * and store an SMacro.
3116 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003117 define_smacro(ctx, mname, casesense, 0, macro_start);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003118 free_tlist(origline);
3119 return DIRECTIVE_FOUND;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003120
H. Peter Anvin2f55bda2009-07-14 15:04:04 -04003121 case PP_DEFTOK:
3122 case PP_IDEFTOK:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003123 casesense = (i == PP_DEFTOK);
3124
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003125 tline = tline->next;
3126 skip_white_(tline);
3127 tline = expand_id(tline);
3128 if (!tline || (tline->type != TOK_ID &&
3129 (tline->type != TOK_PREPROC_ID ||
3130 tline->text[1] != '$'))) {
3131 error(ERR_NONFATAL,
3132 "`%s' expects a macro identifier as first parameter",
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003133 pp_directives[i]);
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003134 free_tlist(origline);
3135 return DIRECTIVE_FOUND;
3136 }
3137 ctx = get_ctx(tline->text, &mname, false);
3138 last = tline;
3139 tline = expand_smacro(tline->next);
3140 last->next = NULL;
3141
3142 t = tline;
3143 while (tok_type_(t, TOK_WHITESPACE))
3144 t = t->next;
3145 /* t should now point to the string */
3146 if (t->type != TOK_STRING) {
3147 error(ERR_NONFATAL,
3148 "`%s` requires string as second parameter",
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003149 pp_directives[i]);
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003150 free_tlist(tline);
3151 free_tlist(origline);
3152 return DIRECTIVE_FOUND;
3153 }
3154
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003155 nasm_unquote_cstr(t->text, i);
3156 macro_start = tokenize(t->text);
3157
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003158 /*
3159 * We now have a macro name, an implicit parameter count of
3160 * zero, and a numeric token to use as an expansion. Create
3161 * and store an SMacro.
3162 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003163 define_smacro(ctx, mname, casesense, 0, macro_start);
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003164 free_tlist(tline);
3165 free_tlist(origline);
3166 return DIRECTIVE_FOUND;
H. Peter Anvin9e200162008-06-04 17:23:14 -07003167
H. Peter Anvin418ca702008-05-30 10:42:30 -07003168 case PP_PATHSEARCH:
3169 {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003170 FILE *fp;
3171 StrList *xsl = NULL;
3172 StrList **xst = &xsl;
H. Peter Anvin418ca702008-05-30 10:42:30 -07003173
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003174 casesense = true;
H. Peter Anvin418ca702008-05-30 10:42:30 -07003175
3176 tline = tline->next;
3177 skip_white_(tline);
3178 tline = expand_id(tline);
3179 if (!tline || (tline->type != TOK_ID &&
3180 (tline->type != TOK_PREPROC_ID ||
3181 tline->text[1] != '$'))) {
3182 error(ERR_NONFATAL,
3183 "`%%pathsearch' expects a macro identifier as first parameter");
3184 free_tlist(origline);
3185 return DIRECTIVE_FOUND;
3186 }
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08003187 ctx = get_ctx(tline->text, &mname, false);
H. Peter Anvin418ca702008-05-30 10:42:30 -07003188 last = tline;
3189 tline = expand_smacro(tline->next);
3190 last->next = NULL;
3191
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003192 t = tline;
H. Peter Anvin418ca702008-05-30 10:42:30 -07003193 while (tok_type_(t, TOK_WHITESPACE))
3194 t = t->next;
3195
3196 if (!t || (t->type != TOK_STRING &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003197 t->type != TOK_INTERNAL_STRING)) {
H. Peter Anvin418ca702008-05-30 10:42:30 -07003198 error(ERR_NONFATAL, "`%%pathsearch' expects a file name");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003199 free_tlist(tline);
H. Peter Anvin418ca702008-05-30 10:42:30 -07003200 free_tlist(origline);
3201 return DIRECTIVE_FOUND; /* but we did _something_ */
3202 }
3203 if (t->next)
H. Peter Anvin917a3492008-09-24 09:14:49 -07003204 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvin418ca702008-05-30 10:42:30 -07003205 "trailing garbage after `%%pathsearch' ignored");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003206 p = t->text;
H. Peter Anvin427cc912008-06-01 21:43:03 -07003207 if (t->type != TOK_INTERNAL_STRING)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003208 nasm_unquote(p, NULL);
H. Peter Anvin418ca702008-05-30 10:42:30 -07003209
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003210 fp = inc_fopen(p, &xsl, &xst, true);
3211 if (fp) {
3212 p = xsl->str;
3213 fclose(fp); /* Don't actually care about the file */
3214 }
H. Peter Anvin418ca702008-05-30 10:42:30 -07003215 macro_start = nasm_malloc(sizeof(*macro_start));
3216 macro_start->next = NULL;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003217 macro_start->text = nasm_quote(p, strlen(p));
3218 macro_start->type = TOK_STRING;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003219 macro_start->a.mac = NULL;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003220 if (xsl)
3221 nasm_free(xsl);
H. Peter Anvin418ca702008-05-30 10:42:30 -07003222
3223 /*
3224 * We now have a macro name, an implicit parameter count of
3225 * zero, and a string token to use as an expansion. Create
3226 * and store an SMacro.
3227 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003228 define_smacro(ctx, mname, casesense, 0, macro_start);
3229 free_tlist(tline);
H. Peter Anvin418ca702008-05-30 10:42:30 -07003230 free_tlist(origline);
3231 return DIRECTIVE_FOUND;
3232 }
3233
H. Peter Anvine2c80182005-01-15 22:15:51 +00003234 case PP_STRLEN:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003235 casesense = true;
H. Peter Anvin70653092007-10-19 14:42:29 -07003236
H. Peter Anvine2c80182005-01-15 22:15:51 +00003237 tline = tline->next;
3238 skip_white_(tline);
3239 tline = expand_id(tline);
3240 if (!tline || (tline->type != TOK_ID &&
3241 (tline->type != TOK_PREPROC_ID ||
3242 tline->text[1] != '$'))) {
3243 error(ERR_NONFATAL,
3244 "`%%strlen' expects a macro identifier as first parameter");
3245 free_tlist(origline);
3246 return DIRECTIVE_FOUND;
3247 }
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08003248 ctx = get_ctx(tline->text, &mname, false);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003249 last = tline;
3250 tline = expand_smacro(tline->next);
3251 last->next = NULL;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003252
H. Peter Anvine2c80182005-01-15 22:15:51 +00003253 t = tline;
3254 while (tok_type_(t, TOK_WHITESPACE))
3255 t = t->next;
3256 /* t should now point to the string */
3257 if (t->type != TOK_STRING) {
3258 error(ERR_NONFATAL,
3259 "`%%strlen` requires string as second parameter");
3260 free_tlist(tline);
3261 free_tlist(origline);
3262 return DIRECTIVE_FOUND;
3263 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003264
H. Peter Anvine2c80182005-01-15 22:15:51 +00003265 macro_start = nasm_malloc(sizeof(*macro_start));
3266 macro_start->next = NULL;
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07003267 make_tok_num(macro_start, nasm_unquote(t->text, NULL));
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003268 macro_start->a.mac = NULL;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003269
H. Peter Anvine2c80182005-01-15 22:15:51 +00003270 /*
3271 * We now have a macro name, an implicit parameter count of
3272 * zero, and a numeric token to use as an expansion. Create
3273 * and store an SMacro.
3274 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003275 define_smacro(ctx, mname, casesense, 0, macro_start);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003276 free_tlist(tline);
3277 free_tlist(origline);
3278 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003279
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003280 case PP_STRCAT:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003281 casesense = true;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003282
3283 tline = tline->next;
3284 skip_white_(tline);
3285 tline = expand_id(tline);
3286 if (!tline || (tline->type != TOK_ID &&
3287 (tline->type != TOK_PREPROC_ID ||
3288 tline->text[1] != '$'))) {
3289 error(ERR_NONFATAL,
3290 "`%%strcat' expects a macro identifier as first parameter");
3291 free_tlist(origline);
3292 return DIRECTIVE_FOUND;
3293 }
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08003294 ctx = get_ctx(tline->text, &mname, false);
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003295 last = tline;
3296 tline = expand_smacro(tline->next);
3297 last->next = NULL;
3298
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003299 len = 0;
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04003300 list_for_each(t, tline) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003301 switch (t->type) {
3302 case TOK_WHITESPACE:
3303 break;
3304 case TOK_STRING:
3305 len += t->a.len = nasm_unquote(t->text, NULL);
3306 break;
3307 case TOK_OTHER:
3308 if (!strcmp(t->text, ",")) /* permit comma separators */
3309 break;
3310 /* else fall through */
3311 default:
3312 error(ERR_NONFATAL,
3313 "non-string passed to `%%strcat' (%d)", t->type);
3314 free_tlist(tline);
3315 free_tlist(origline);
3316 return DIRECTIVE_FOUND;
3317 }
3318 }
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003319
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003320 p = pp = nasm_malloc(len);
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04003321 list_for_each(t, tline) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003322 if (t->type == TOK_STRING) {
3323 memcpy(p, t->text, t->a.len);
3324 p += t->a.len;
3325 }
3326 }
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003327
3328 /*
3329 * We now have a macro name, an implicit parameter count of
3330 * zero, and a numeric token to use as an expansion. Create
3331 * and store an SMacro.
3332 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003333 macro_start = new_Token(NULL, TOK_STRING, NULL, 0);
3334 macro_start->text = nasm_quote(pp, len);
3335 nasm_free(pp);
3336 define_smacro(ctx, mname, casesense, 0, macro_start);
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003337 free_tlist(tline);
3338 free_tlist(origline);
3339 return DIRECTIVE_FOUND;
3340
H. Peter Anvine2c80182005-01-15 22:15:51 +00003341 case PP_SUBSTR:
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003342 {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003343 int64_t a1, a2;
3344 size_t len;
H. Peter Anvind2456592008-06-19 15:04:18 -07003345
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003346 casesense = true;
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07003347
H. Peter Anvine2c80182005-01-15 22:15:51 +00003348 tline = tline->next;
3349 skip_white_(tline);
3350 tline = expand_id(tline);
3351 if (!tline || (tline->type != TOK_ID &&
3352 (tline->type != TOK_PREPROC_ID ||
3353 tline->text[1] != '$'))) {
3354 error(ERR_NONFATAL,
3355 "`%%substr' expects a macro identifier as first parameter");
3356 free_tlist(origline);
3357 return DIRECTIVE_FOUND;
3358 }
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08003359 ctx = get_ctx(tline->text, &mname, false);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003360 last = tline;
3361 tline = expand_smacro(tline->next);
3362 last->next = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003363
H. Peter Anvine2c80182005-01-15 22:15:51 +00003364 t = tline->next;
3365 while (tok_type_(t, TOK_WHITESPACE))
3366 t = t->next;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003367
H. Peter Anvine2c80182005-01-15 22:15:51 +00003368 /* t should now point to the string */
3369 if (t->type != TOK_STRING) {
3370 error(ERR_NONFATAL,
3371 "`%%substr` requires string as second parameter");
3372 free_tlist(tline);
3373 free_tlist(origline);
3374 return DIRECTIVE_FOUND;
3375 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003376
H. Peter Anvine2c80182005-01-15 22:15:51 +00003377 tt = t->next;
3378 tptr = &tt;
3379 tokval.t_type = TOKEN_INVALID;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003380 evalresult = evaluate(ppscan, tptr, &tokval, NULL,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003381 pass, error, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003382 if (!evalresult) {
3383 free_tlist(tline);
3384 free_tlist(origline);
3385 return DIRECTIVE_FOUND;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003386 } else if (!is_simple(evalresult)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00003387 error(ERR_NONFATAL, "non-constant value given to `%%substr`");
3388 free_tlist(tline);
3389 free_tlist(origline);
3390 return DIRECTIVE_FOUND;
3391 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003392 a1 = evalresult->value-1;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003393
3394 while (tok_type_(tt, TOK_WHITESPACE))
3395 tt = tt->next;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003396 if (!tt) {
3397 a2 = 1; /* Backwards compatibility: one character */
3398 } else {
3399 tokval.t_type = TOKEN_INVALID;
3400 evalresult = evaluate(ppscan, tptr, &tokval, NULL,
3401 pass, error, NULL);
3402 if (!evalresult) {
3403 free_tlist(tline);
3404 free_tlist(origline);
3405 return DIRECTIVE_FOUND;
3406 } else if (!is_simple(evalresult)) {
3407 error(ERR_NONFATAL, "non-constant value given to `%%substr`");
3408 free_tlist(tline);
3409 free_tlist(origline);
3410 return DIRECTIVE_FOUND;
3411 }
3412 a2 = evalresult->value;
3413 }
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003414
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003415 len = nasm_unquote(t->text, NULL);
3416 if (a2 < 0)
3417 a2 = a2+1+len-a1;
3418 if (a1+a2 > (int64_t)len)
3419 a2 = len-a1;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003420
H. Peter Anvine2c80182005-01-15 22:15:51 +00003421 macro_start = nasm_malloc(sizeof(*macro_start));
3422 macro_start->next = NULL;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003423 macro_start->text = nasm_quote((a1 < 0) ? "" : t->text+a1, a2);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003424 macro_start->type = TOK_STRING;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003425 macro_start->a.mac = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003426
H. Peter Anvine2c80182005-01-15 22:15:51 +00003427 /*
3428 * We now have a macro name, an implicit parameter count of
3429 * zero, and a numeric token to use as an expansion. Create
3430 * and store an SMacro.
3431 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003432 define_smacro(ctx, mname, casesense, 0, macro_start);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003433 free_tlist(tline);
3434 free_tlist(origline);
3435 return DIRECTIVE_FOUND;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003436 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003437
H. Peter Anvine2c80182005-01-15 22:15:51 +00003438 case PP_ASSIGN:
3439 case PP_IASSIGN:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003440 casesense = (i == PP_ASSIGN);
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07003441
H. Peter Anvine2c80182005-01-15 22:15:51 +00003442 tline = tline->next;
3443 skip_white_(tline);
3444 tline = expand_id(tline);
3445 if (!tline || (tline->type != TOK_ID &&
3446 (tline->type != TOK_PREPROC_ID ||
3447 tline->text[1] != '$'))) {
3448 error(ERR_NONFATAL,
3449 "`%%%sassign' expects a macro identifier",
3450 (i == PP_IASSIGN ? "i" : ""));
3451 free_tlist(origline);
3452 return DIRECTIVE_FOUND;
3453 }
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08003454 ctx = get_ctx(tline->text, &mname, false);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003455 last = tline;
3456 tline = expand_smacro(tline->next);
3457 last->next = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003458
H. Peter Anvine2c80182005-01-15 22:15:51 +00003459 t = tline;
3460 tptr = &t;
3461 tokval.t_type = TOKEN_INVALID;
3462 evalresult =
3463 evaluate(ppscan, tptr, &tokval, NULL, pass, error, NULL);
3464 free_tlist(tline);
3465 if (!evalresult) {
3466 free_tlist(origline);
3467 return DIRECTIVE_FOUND;
3468 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003469
H. Peter Anvine2c80182005-01-15 22:15:51 +00003470 if (tokval.t_type)
H. Peter Anvin917a3492008-09-24 09:14:49 -07003471 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +00003472 "trailing garbage after expression ignored");
H. Peter Anvin734b1882002-04-30 21:01:08 +00003473
H. Peter Anvine2c80182005-01-15 22:15:51 +00003474 if (!is_simple(evalresult)) {
3475 error(ERR_NONFATAL,
3476 "non-constant value given to `%%%sassign'",
3477 (i == PP_IASSIGN ? "i" : ""));
3478 free_tlist(origline);
3479 return DIRECTIVE_FOUND;
3480 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003481
H. Peter Anvine2c80182005-01-15 22:15:51 +00003482 macro_start = nasm_malloc(sizeof(*macro_start));
3483 macro_start->next = NULL;
3484 make_tok_num(macro_start, reloc_value(evalresult));
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003485 macro_start->a.mac = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003486
H. Peter Anvine2c80182005-01-15 22:15:51 +00003487 /*
3488 * We now have a macro name, an implicit parameter count of
3489 * zero, and a numeric token to use as an expansion. Create
3490 * and store an SMacro.
3491 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003492 define_smacro(ctx, mname, casesense, 0, macro_start);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003493 free_tlist(origline);
3494 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003495
H. Peter Anvine2c80182005-01-15 22:15:51 +00003496 case PP_LINE:
3497 /*
3498 * Syntax is `%line nnn[+mmm] [filename]'
3499 */
3500 tline = tline->next;
3501 skip_white_(tline);
3502 if (!tok_type_(tline, TOK_NUMBER)) {
3503 error(ERR_NONFATAL, "`%%line' expects line number");
3504 free_tlist(origline);
3505 return DIRECTIVE_FOUND;
3506 }
H. Peter Anvin70055962007-10-11 00:05:31 -07003507 k = readnum(tline->text, &err);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003508 m = 1;
3509 tline = tline->next;
3510 if (tok_is_(tline, "+")) {
3511 tline = tline->next;
3512 if (!tok_type_(tline, TOK_NUMBER)) {
3513 error(ERR_NONFATAL, "`%%line' expects line increment");
3514 free_tlist(origline);
3515 return DIRECTIVE_FOUND;
3516 }
H. Peter Anvin70055962007-10-11 00:05:31 -07003517 m = readnum(tline->text, &err);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003518 tline = tline->next;
3519 }
3520 skip_white_(tline);
3521 src_set_linnum(k);
3522 istk->lineinc = m;
3523 if (tline) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07003524 nasm_free(src_set_fname(detoken(tline, false)));
H. Peter Anvine2c80182005-01-15 22:15:51 +00003525 }
3526 free_tlist(origline);
3527 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003528
H. Peter Anvine2c80182005-01-15 22:15:51 +00003529 default:
3530 error(ERR_FATAL,
3531 "preprocessor directive `%s' not yet implemented",
H. Peter Anvin4169a472007-09-12 01:29:43 +00003532 pp_directives[i]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003533 return DIRECTIVE_FOUND;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003534 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003535}
3536
3537/*
H. Peter Anvin76690a12002-04-30 20:52:49 +00003538 * Ensure that a macro parameter contains a condition code and
3539 * nothing else. Return the condition code index if so, or -1
3540 * otherwise.
3541 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003542static int find_cc(Token * t)
H. Peter Anvineba20a72002-04-30 20:53:55 +00003543{
H. Peter Anvin76690a12002-04-30 20:52:49 +00003544 Token *tt;
3545 int i, j, k, m;
3546
H. Peter Anvin25a99342007-09-22 17:45:45 -07003547 if (!t)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003548 return -1; /* Probably a %+ without a space */
H. Peter Anvin25a99342007-09-22 17:45:45 -07003549
H. Peter Anvineba20a72002-04-30 20:53:55 +00003550 skip_white_(t);
H. Peter Anvin76690a12002-04-30 20:52:49 +00003551 if (t->type != TOK_ID)
H. Peter Anvine2c80182005-01-15 22:15:51 +00003552 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003553 tt = t->next;
H. Peter Anvineba20a72002-04-30 20:53:55 +00003554 skip_white_(tt);
H. Peter Anvin76690a12002-04-30 20:52:49 +00003555 if (tt && (tt->type != TOK_OTHER || strcmp(tt->text, ",")))
H. Peter Anvine2c80182005-01-15 22:15:51 +00003556 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003557
3558 i = -1;
Cyrill Gorcunova7319242010-06-03 22:04:36 +04003559 j = ARRAY_SIZE(conditions);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003560 while (j - i > 1) {
3561 k = (j + i) / 2;
3562 m = nasm_stricmp(t->text, conditions[k]);
3563 if (m == 0) {
3564 i = k;
3565 j = -2;
3566 break;
3567 } else if (m < 0) {
3568 j = k;
3569 } else
3570 i = k;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003571 }
3572 if (j != -2)
H. Peter Anvine2c80182005-01-15 22:15:51 +00003573 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003574 return i;
3575}
3576
H. Peter Anvind784a082009-04-20 14:01:18 -07003577static bool paste_tokens(Token **head, bool handle_paste_tokens)
3578{
3579 Token **tail, *t, *tt;
3580 Token **paste_head;
3581 bool did_paste = false;
3582 char *tmp;
3583
3584 /* Now handle token pasting... */
3585 paste_head = NULL;
3586 tail = head;
3587 while ((t = *tail) && (tt = t->next)) {
3588 switch (t->type) {
3589 case TOK_WHITESPACE:
3590 if (tt->type == TOK_WHITESPACE) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003591 /* Zap adjacent whitespace tokens */
H. Peter Anvind784a082009-04-20 14:01:18 -07003592 t->next = delete_Token(tt);
3593 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003594 /* Do not advance paste_head here */
3595 tail = &t->next;
3596 }
H. Peter Anvind784a082009-04-20 14:01:18 -07003597 break;
3598 case TOK_ID:
H. Peter Anvind784a082009-04-20 14:01:18 -07003599 case TOK_NUMBER:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003600 case TOK_FLOAT:
3601 {
3602 size_t len = 0;
3603 char *tmp, *p;
H. Peter Anvind784a082009-04-20 14:01:18 -07003604
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003605 while (tt && (tt->type == TOK_ID || tt->type == TOK_PREPROC_ID ||
3606 tt->type == TOK_NUMBER || tt->type == TOK_FLOAT ||
3607 tt->type == TOK_OTHER)) {
3608 len += strlen(tt->text);
3609 tt = tt->next;
3610 }
H. Peter Anvind784a082009-04-20 14:01:18 -07003611
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003612 /*
3613 * Now tt points to the first token after
3614 * the potential paste area...
3615 */
3616 if (tt != t->next) {
3617 /* We have at least two tokens... */
3618 len += strlen(t->text);
3619 p = tmp = nasm_malloc(len+1);
H. Peter Anvind784a082009-04-20 14:01:18 -07003620
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003621 while (t != tt) {
3622 strcpy(p, t->text);
3623 p = strchr(p, '\0');
3624 t = delete_Token(t);
3625 }
H. Peter Anvind784a082009-04-20 14:01:18 -07003626
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003627 t = *tail = tokenize(tmp);
3628 nasm_free(tmp);
H. Peter Anvind784a082009-04-20 14:01:18 -07003629
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003630 while (t->next) {
3631 tail = &t->next;
3632 t = t->next;
3633 }
3634 t->next = tt; /* Attach the remaining token chain */
H. Peter Anvind784a082009-04-20 14:01:18 -07003635
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003636 did_paste = true;
3637 }
3638 paste_head = tail;
3639 tail = &t->next;
3640 break;
3641 }
3642 case TOK_PASTE: /* %+ */
3643 if (handle_paste_tokens) {
3644 /* Zap %+ and whitespace tokens to the right */
3645 while (t && (t->type == TOK_WHITESPACE ||
3646 t->type == TOK_PASTE))
3647 t = *tail = delete_Token(t);
3648 if (!paste_head || !t)
3649 break; /* Nothing to paste with */
3650 tail = paste_head;
3651 t = *tail;
3652 tt = t->next;
3653 while (tok_type_(tt, TOK_WHITESPACE))
3654 tt = t->next = delete_Token(tt);
H. Peter Anvind784a082009-04-20 14:01:18 -07003655
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003656 if (tt) {
3657 tmp = nasm_strcat(t->text, tt->text);
3658 delete_Token(t);
3659 tt = delete_Token(tt);
3660 t = *tail = tokenize(tmp);
3661 nasm_free(tmp);
3662 while (t->next) {
3663 tail = &t->next;
3664 t = t->next;
3665 }
3666 t->next = tt; /* Attach the remaining token chain */
3667 did_paste = true;
3668 }
3669 paste_head = tail;
3670 tail = &t->next;
3671 break;
3672 }
3673 /* else fall through */
3674 default:
Cyrill Gorcunovadabc152010-07-10 02:11:41 +04003675 tail = &t->next;
3676 if (!tok_type_(t->next, TOK_WHITESPACE))
3677 paste_head = tail;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003678 break;
H. Peter Anvind784a082009-04-20 14:01:18 -07003679 }
3680 }
3681 return did_paste;
3682}
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003683
3684/*
3685 * expands to a list of tokens from %{x:y}
3686 */
3687static Token *expand_mmac_params_range(MMacro *mac, Token *tline, Token ***last)
3688{
3689 Token *t = tline, **tt, *tm, *head;
3690 char *pos;
3691 int fst, lst, j, i;
3692
3693 pos = strchr(tline->text, ':');
3694 nasm_assert(pos);
3695
3696 lst = atoi(pos + 1);
3697 fst = atoi(tline->text + 1);
3698
3699 /*
3700 * only macros params are accounted so
3701 * if someone passes %0 -- we reject such
3702 * value(s)
3703 */
3704 if (lst == 0 || fst == 0)
3705 goto err;
3706
3707 /* the values should be sane */
Cyrill Gorcunov2f403752010-06-05 10:47:10 +04003708 if ((fst > (int)mac->nparam || fst < (-(int)mac->nparam)) ||
3709 (lst > (int)mac->nparam || lst < (-(int)mac->nparam)))
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003710 goto err;
3711
Cyrill Gorcunovefb35832010-06-20 01:52:19 +04003712 fst = fst < 0 ? fst + (int)mac->nparam + 1: fst;
3713 lst = lst < 0 ? lst + (int)mac->nparam + 1: lst;
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003714
3715 /* counted from zero */
3716 fst--, lst--;
3717
3718 /*
3719 * it will be at least one token
3720 */
3721 tm = mac->params[(fst + mac->rotate) % mac->nparam];
3722 t = new_Token(NULL, tm->type, tm->text, 0);
3723 head = t, tt = &t->next;
3724 if (fst < lst) {
3725 for (i = fst + 1; i <= lst; i++) {
3726 t = new_Token(NULL, TOK_OTHER, ",", 0);
3727 *tt = t, tt = &t->next;
3728 j = (i + mac->rotate) % mac->nparam;
3729 tm = mac->params[j];
3730 t = new_Token(NULL, tm->type, tm->text, 0);
3731 *tt = t, tt = &t->next;
3732 }
3733 } else {
3734 for (i = fst - 1; i >= lst; i--) {
3735 t = new_Token(NULL, TOK_OTHER, ",", 0);
3736 *tt = t, tt = &t->next;
3737 j = (i + mac->rotate) % mac->nparam;
3738 tm = mac->params[j];
3739 t = new_Token(NULL, tm->type, tm->text, 0);
3740 *tt = t, tt = &t->next;
3741 }
3742 }
3743
3744 *last = tt;
3745 return head;
3746
3747err:
3748 error(ERR_NONFATAL, "`%%{%s}': macro parameters out of range",
3749 &tline->text[1]);
3750 return tline;
3751}
3752
H. Peter Anvin76690a12002-04-30 20:52:49 +00003753/*
3754 * Expand MMacro-local things: parameter references (%0, %n, %+n,
H. Peter Anvin67c63722008-10-26 23:49:00 -07003755 * %-n) and MMacro-local identifiers (%%foo) as well as
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003756 * macro indirection (%[...]) and range (%{..:..}).
H. Peter Anvin76690a12002-04-30 20:52:49 +00003757 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003758static Token *expand_mmac_params(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00003759{
H. Peter Anvin734b1882002-04-30 21:01:08 +00003760 Token *t, *tt, **tail, *thead;
H. Peter Anvin6125b622009-04-08 14:02:25 -07003761 bool changed = false;
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003762 char *pos;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003763
3764 tail = &thead;
3765 thead = NULL;
3766
H. Peter Anvine2c80182005-01-15 22:15:51 +00003767 while (tline) {
3768 if (tline->type == TOK_PREPROC_ID &&
Cyrill Gorcunovca611192010-06-04 09:22:12 +04003769 (((tline->text[1] == '+' || tline->text[1] == '-') && tline->text[2]) ||
3770 (tline->text[1] >= '0' && tline->text[1] <= '9') ||
3771 tline->text[1] == '%')) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00003772 char *text = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003773 int type = 0, cc; /* type = 0 to placate optimisers */
Keith Kaniosa6dfa782007-04-13 16:47:53 +00003774 char tmpbuf[30];
H. Peter Anvin25a99342007-09-22 17:45:45 -07003775 unsigned int n;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003776 int i;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003777 MMacro *mac;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003778
H. Peter Anvine2c80182005-01-15 22:15:51 +00003779 t = tline;
3780 tline = tline->next;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003781
H. Peter Anvine2c80182005-01-15 22:15:51 +00003782 mac = istk->mstk;
3783 while (mac && !mac->name) /* avoid mistaking %reps for macros */
3784 mac = mac->next_active;
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003785 if (!mac) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00003786 error(ERR_NONFATAL, "`%s': not in a macro call", t->text);
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003787 } else {
3788 pos = strchr(t->text, ':');
3789 if (!pos) {
3790 switch (t->text[1]) {
3791 /*
3792 * We have to make a substitution of one of the
3793 * forms %1, %-1, %+1, %%foo, %0.
3794 */
3795 case '0':
3796 type = TOK_NUMBER;
3797 snprintf(tmpbuf, sizeof(tmpbuf), "%d", mac->nparam);
3798 text = nasm_strdup(tmpbuf);
3799 break;
3800 case '%':
H. Peter Anvine2c80182005-01-15 22:15:51 +00003801 type = TOK_ID;
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003802 snprintf(tmpbuf, sizeof(tmpbuf), "..@%"PRIu64".",
3803 mac->unique);
3804 text = nasm_strcat(tmpbuf, t->text + 2);
3805 break;
3806 case '-':
3807 n = atoi(t->text + 2) - 1;
3808 if (n >= mac->nparam)
3809 tt = NULL;
3810 else {
3811 if (mac->nparam > 1)
3812 n = (n + mac->rotate) % mac->nparam;
3813 tt = mac->params[n];
H. Peter Anvine2c80182005-01-15 22:15:51 +00003814 }
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003815 cc = find_cc(tt);
3816 if (cc == -1) {
3817 error(ERR_NONFATAL,
3818 "macro parameter %d is not a condition code",
3819 n + 1);
3820 text = NULL;
3821 } else {
3822 type = TOK_ID;
3823 if (inverse_ccs[cc] == -1) {
3824 error(ERR_NONFATAL,
3825 "condition code `%s' is not invertible",
3826 conditions[cc]);
3827 text = NULL;
3828 } else
3829 text = nasm_strdup(conditions[inverse_ccs[cc]]);
3830 }
3831 break;
3832 case '+':
3833 n = atoi(t->text + 2) - 1;
3834 if (n >= mac->nparam)
3835 tt = NULL;
3836 else {
3837 if (mac->nparam > 1)
3838 n = (n + mac->rotate) % mac->nparam;
3839 tt = mac->params[n];
3840 }
3841 cc = find_cc(tt);
3842 if (cc == -1) {
3843 error(ERR_NONFATAL,
3844 "macro parameter %d is not a condition code",
3845 n + 1);
3846 text = NULL;
3847 } else {
3848 type = TOK_ID;
3849 text = nasm_strdup(conditions[cc]);
3850 }
3851 break;
3852 default:
3853 n = atoi(t->text + 1) - 1;
3854 if (n >= mac->nparam)
3855 tt = NULL;
3856 else {
3857 if (mac->nparam > 1)
3858 n = (n + mac->rotate) % mac->nparam;
3859 tt = mac->params[n];
3860 }
3861 if (tt) {
3862 for (i = 0; i < mac->paramlen[n]; i++) {
3863 *tail = new_Token(NULL, tt->type, tt->text, 0);
3864 tail = &(*tail)->next;
3865 tt = tt->next;
3866 }
3867 }
3868 text = NULL; /* we've done it here */
3869 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003870 }
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003871 } else {
3872 /*
3873 * seems we have a parameters range here
3874 */
3875 Token *head, **last;
3876 head = expand_mmac_params_range(mac, t, &last);
3877 if (head != t) {
3878 *tail = head;
3879 *last = tline;
3880 tline = head;
3881 text = NULL;
3882 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00003883 }
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003884 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00003885 if (!text) {
3886 delete_Token(t);
3887 } else {
3888 *tail = t;
3889 tail = &t->next;
3890 t->type = type;
3891 nasm_free(t->text);
3892 t->text = text;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003893 t->a.mac = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003894 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003895 changed = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003896 continue;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003897 } else if (tline->type == TOK_INDIRECT) {
3898 t = tline;
3899 tline = tline->next;
3900 tt = tokenize(t->text);
3901 tt = expand_mmac_params(tt);
3902 tt = expand_smacro(tt);
3903 *tail = tt;
3904 while (tt) {
3905 tt->a.mac = NULL; /* Necessary? */
3906 tail = &tt->next;
3907 tt = tt->next;
3908 }
3909 delete_Token(t);
3910 changed = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003911 } else {
3912 t = *tail = tline;
3913 tline = tline->next;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003914 t->a.mac = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003915 tail = &t->next;
3916 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00003917 }
H. Peter Anvineba20a72002-04-30 20:53:55 +00003918 *tail = NULL;
H. Peter Anvin67c63722008-10-26 23:49:00 -07003919
H. Peter Anvind784a082009-04-20 14:01:18 -07003920 if (changed)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003921 paste_tokens(&thead, false);
H. Peter Anvin6125b622009-04-08 14:02:25 -07003922
H. Peter Anvin76690a12002-04-30 20:52:49 +00003923 return thead;
3924}
3925
3926/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003927 * Expand all single-line macro calls made in the given line.
3928 * Return the expanded version of the line. The original is deemed
3929 * to be destroyed in the process. (In reality we'll just move
3930 * Tokens from input to output a lot of the time, rather than
3931 * actually bothering to destroy and replicate.)
3932 */
H. Peter Anvincb1cf592007-11-19 12:26:50 -08003933
H. Peter Anvine2c80182005-01-15 22:15:51 +00003934static Token *expand_smacro(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00003935{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003936 Token *t, *tt, *mstart, **tail, *thead;
H. Peter Anvineba20a72002-04-30 20:53:55 +00003937 SMacro *head = NULL, *m;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003938 Token **params;
3939 int *paramsize;
H. Peter Anvin25a99342007-09-22 17:45:45 -07003940 unsigned int nparam, sparam;
H. Peter Anvind784a082009-04-20 14:01:18 -07003941 int brackets;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003942 Token *org_tline = tline;
3943 Context *ctx;
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08003944 const char *mname;
H. Peter Anvin2a15e692007-11-19 13:14:59 -08003945 int deadman = DEADMAN_LIMIT;
H. Peter Anvin8287daf2009-07-07 16:00:58 -07003946 bool expanded;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003947
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003948 /*
3949 * Trick: we should avoid changing the start token pointer since it can
3950 * be contained in "next" field of other token. Because of this
3951 * we allocate a copy of first token and work with it; at the end of
3952 * routine we copy it back
3953 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003954 if (org_tline) {
Cyrill Gorcunoved4a8052010-04-09 15:40:35 +04003955 tline = new_Token(org_tline->next, org_tline->type,
3956 org_tline->text, 0);
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003957 tline->a.mac = org_tline->a.mac;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003958 nasm_free(org_tline->text);
3959 org_tline->text = NULL;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003960 }
3961
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003962 expanded = true; /* Always expand %+ at least once */
H. Peter Anvin8287daf2009-07-07 16:00:58 -07003963
H. Peter Anvincb1cf592007-11-19 12:26:50 -08003964again:
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003965 thead = NULL;
Cyrill Gorcunoved4a8052010-04-09 15:40:35 +04003966 tail = &thead;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003967
H. Peter Anvine2c80182005-01-15 22:15:51 +00003968 while (tline) { /* main token loop */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003969 if (!--deadman) {
3970 error(ERR_NONFATAL, "interminable macro recursion");
Cyrill Gorcunovbd38c8f2009-11-21 11:11:23 +03003971 goto err;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003972 }
H. Peter Anvincb1cf592007-11-19 12:26:50 -08003973
H. Peter Anvine2c80182005-01-15 22:15:51 +00003974 if ((mname = tline->text)) {
3975 /* if this token is a local macro, look in local context */
Cyrill Gorcunovc56d9ad2010-02-11 15:12:19 +03003976 if (tline->type == TOK_ID) {
3977 head = (SMacro *)hash_findix(&smacros, mname);
3978 } else if (tline->type == TOK_PREPROC_ID) {
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08003979 ctx = get_ctx(mname, &mname, true);
Cyrill Gorcunovc56d9ad2010-02-11 15:12:19 +03003980 head = ctx ? (SMacro *)hash_findix(&ctx->localmac, mname) : NULL;
3981 } else
3982 head = NULL;
H. Peter Anvin072771e2008-05-22 13:17:51 -07003983
H. Peter Anvine2c80182005-01-15 22:15:51 +00003984 /*
3985 * We've hit an identifier. As in is_mmacro below, we first
3986 * check whether the identifier is a single-line macro at
3987 * all, then think about checking for parameters if
3988 * necessary.
3989 */
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04003990 list_for_each(m, head)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003991 if (!mstrcmp(m->name, mname, m->casesense))
3992 break;
3993 if (m) {
3994 mstart = tline;
3995 params = NULL;
3996 paramsize = NULL;
3997 if (m->nparam == 0) {
3998 /*
3999 * Simple case: the macro is parameterless. Discard the
4000 * one token that the macro call took, and push the
4001 * expansion back on the to-do stack.
4002 */
4003 if (!m->expansion) {
4004 if (!strcmp("__FILE__", m->name)) {
4005 int32_t num = 0;
4006 char *file = NULL;
4007 src_get(&num, &file);
4008 tline->text = nasm_quote(file, strlen(file));
4009 tline->type = TOK_STRING;
4010 nasm_free(file);
H. Peter Anvine2c80182005-01-15 22:15:51 +00004011 continue;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004012 }
4013 if (!strcmp("__LINE__", m->name)) {
4014 nasm_free(tline->text);
4015 make_tok_num(tline, src_get_linnum());
4016 continue;
4017 }
4018 if (!strcmp("__BITS__", m->name)) {
4019 nasm_free(tline->text);
4020 make_tok_num(tline, globalbits);
4021 continue;
4022 }
4023 tline = delete_Token(tline);
4024 continue;
4025 }
4026 } else {
4027 /*
4028 * Complicated case: at least one macro with this name
H. Peter Anvine2c80182005-01-15 22:15:51 +00004029 * exists and takes parameters. We must find the
4030 * parameters in the call, count them, find the SMacro
4031 * that corresponds to that form of the macro call, and
4032 * substitute for the parameters when we expand. What a
4033 * pain.
4034 */
4035 /*tline = tline->next;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004036 skip_white_(tline); */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004037 do {
4038 t = tline->next;
4039 while (tok_type_(t, TOK_SMAC_END)) {
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004040 t->a.mac->in_progress = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004041 t->text = NULL;
4042 t = tline->next = delete_Token(t);
4043 }
4044 tline = t;
4045 } while (tok_type_(tline, TOK_WHITESPACE));
4046 if (!tok_is_(tline, "(")) {
4047 /*
4048 * This macro wasn't called with parameters: ignore
4049 * the call. (Behaviour borrowed from gnu cpp.)
4050 */
4051 tline = mstart;
4052 m = NULL;
4053 } else {
4054 int paren = 0;
4055 int white = 0;
4056 brackets = 0;
4057 nparam = 0;
4058 sparam = PARAM_DELTA;
4059 params = nasm_malloc(sparam * sizeof(Token *));
4060 params[0] = tline->next;
4061 paramsize = nasm_malloc(sparam * sizeof(int));
4062 paramsize[0] = 0;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07004063 while (true) { /* parameter loop */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004064 /*
4065 * For some unusual expansions
4066 * which concatenates function call
4067 */
4068 t = tline->next;
4069 while (tok_type_(t, TOK_SMAC_END)) {
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004070 t->a.mac->in_progress = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004071 t->text = NULL;
4072 t = tline->next = delete_Token(t);
4073 }
4074 tline = t;
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00004075
H. Peter Anvine2c80182005-01-15 22:15:51 +00004076 if (!tline) {
4077 error(ERR_NONFATAL,
4078 "macro call expects terminating `)'");
4079 break;
4080 }
4081 if (tline->type == TOK_WHITESPACE
4082 && brackets <= 0) {
4083 if (paramsize[nparam])
4084 white++;
4085 else
4086 params[nparam] = tline->next;
4087 continue; /* parameter loop */
4088 }
4089 if (tline->type == TOK_OTHER
4090 && tline->text[1] == 0) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004091 char ch = tline->text[0];
H. Peter Anvine2c80182005-01-15 22:15:51 +00004092 if (ch == ',' && !paren && brackets <= 0) {
4093 if (++nparam >= sparam) {
4094 sparam += PARAM_DELTA;
4095 params = nasm_realloc(params,
Cyrill Gorcunoved4a8052010-04-09 15:40:35 +04004096 sparam * sizeof(Token *));
4097 paramsize = nasm_realloc(paramsize,
4098 sparam * sizeof(int));
H. Peter Anvine2c80182005-01-15 22:15:51 +00004099 }
4100 params[nparam] = tline->next;
4101 paramsize[nparam] = 0;
4102 white = 0;
4103 continue; /* parameter loop */
4104 }
4105 if (ch == '{' &&
4106 (brackets > 0 || (brackets == 0 &&
4107 !paramsize[nparam])))
4108 {
4109 if (!(brackets++)) {
4110 params[nparam] = tline->next;
4111 continue; /* parameter loop */
4112 }
4113 }
4114 if (ch == '}' && brackets > 0)
4115 if (--brackets == 0) {
4116 brackets = -1;
4117 continue; /* parameter loop */
4118 }
4119 if (ch == '(' && !brackets)
4120 paren++;
4121 if (ch == ')' && brackets <= 0)
4122 if (--paren < 0)
4123 break;
4124 }
4125 if (brackets < 0) {
4126 brackets = 0;
4127 error(ERR_NONFATAL, "braces do not "
4128 "enclose all of macro parameter");
4129 }
4130 paramsize[nparam] += white + 1;
4131 white = 0;
4132 } /* parameter loop */
4133 nparam++;
4134 while (m && (m->nparam != nparam ||
4135 mstrcmp(m->name, mname,
4136 m->casesense)))
4137 m = m->next;
4138 if (!m)
H. Peter Anvin917a3492008-09-24 09:14:49 -07004139 error(ERR_WARNING|ERR_PASS1|ERR_WARN_MNP,
H. Peter Anvine2c80182005-01-15 22:15:51 +00004140 "macro `%s' exists, "
4141 "but not taking %d parameters",
4142 mstart->text, nparam);
4143 }
4144 }
4145 if (m && m->in_progress)
4146 m = NULL;
4147 if (!m) { /* in progess or didn't find '(' or wrong nparam */
H. Peter Anvin70653092007-10-19 14:42:29 -07004148 /*
H. Peter Anvine2c80182005-01-15 22:15:51 +00004149 * Design question: should we handle !tline, which
4150 * indicates missing ')' here, or expand those
4151 * macros anyway, which requires the (t) test a few
H. Peter Anvin70653092007-10-19 14:42:29 -07004152 * lines down?
H. Peter Anvine2c80182005-01-15 22:15:51 +00004153 */
4154 nasm_free(params);
4155 nasm_free(paramsize);
4156 tline = mstart;
4157 } else {
4158 /*
4159 * Expand the macro: we are placed on the last token of the
4160 * call, so that we can easily split the call from the
4161 * following tokens. We also start by pushing an SMAC_END
4162 * token for the cycle removal.
4163 */
4164 t = tline;
4165 if (t) {
4166 tline = t->next;
4167 t->next = NULL;
4168 }
4169 tt = new_Token(tline, TOK_SMAC_END, NULL, 0);
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004170 tt->a.mac = m;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07004171 m->in_progress = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004172 tline = tt;
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04004173 list_for_each(t, m->expansion) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00004174 if (t->type >= TOK_SMAC_PARAM) {
4175 Token *pcopy = tline, **ptail = &pcopy;
4176 Token *ttt, *pt;
4177 int i;
H. Peter Anvin734b1882002-04-30 21:01:08 +00004178
H. Peter Anvine2c80182005-01-15 22:15:51 +00004179 ttt = params[t->type - TOK_SMAC_PARAM];
Cyrill Gorcunoved4a8052010-04-09 15:40:35 +04004180 i = paramsize[t->type - TOK_SMAC_PARAM];
4181 while (--i >= 0) {
4182 pt = *ptail = new_Token(tline, ttt->type,
4183 ttt->text, 0);
H. Peter Anvine2c80182005-01-15 22:15:51 +00004184 ptail = &pt->next;
4185 ttt = ttt->next;
4186 }
4187 tline = pcopy;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004188 } else if (t->type == TOK_PREPROC_Q) {
4189 tt = new_Token(tline, TOK_ID, mname, 0);
4190 tline = tt;
4191 } else if (t->type == TOK_PREPROC_QQ) {
4192 tt = new_Token(tline, TOK_ID, m->name, 0);
4193 tline = tt;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004194 } else {
4195 tt = new_Token(tline, t->type, t->text, 0);
4196 tline = tt;
4197 }
4198 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00004199
H. Peter Anvine2c80182005-01-15 22:15:51 +00004200 /*
4201 * Having done that, get rid of the macro call, and clean
4202 * up the parameters.
4203 */
4204 nasm_free(params);
4205 nasm_free(paramsize);
4206 free_tlist(mstart);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004207 expanded = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004208 continue; /* main token loop */
4209 }
4210 }
4211 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004212
H. Peter Anvine2c80182005-01-15 22:15:51 +00004213 if (tline->type == TOK_SMAC_END) {
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004214 tline->a.mac->in_progress = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004215 tline = delete_Token(tline);
4216 } else {
4217 t = *tail = tline;
4218 tline = tline->next;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004219 t->a.mac = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004220 t->next = NULL;
4221 tail = &t->next;
4222 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004223 }
4224
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004225 /*
4226 * Now scan the entire line and look for successive TOK_IDs that resulted
Keith Kaniosb7a89542007-04-12 02:40:54 +00004227 * after expansion (they can't be produced by tokenize()). The successive
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004228 * TOK_IDs should be concatenated.
4229 * Also we look for %+ tokens and concatenate the tokens before and after
4230 * them (without white spaces in between).
4231 */
H. Peter Anvin8287daf2009-07-07 16:00:58 -07004232 if (expanded && paste_tokens(&thead, true)) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004233 /*
4234 * If we concatenated something, *and* we had previously expanded
4235 * an actual macro, scan the lines again for macros...
4236 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004237 tline = thead;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004238 expanded = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004239 goto again;
H. Peter Anvin734b1882002-04-30 21:01:08 +00004240 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004241
Cyrill Gorcunovbd38c8f2009-11-21 11:11:23 +03004242err:
H. Peter Anvine2c80182005-01-15 22:15:51 +00004243 if (org_tline) {
4244 if (thead) {
4245 *org_tline = *thead;
4246 /* since we just gave text to org_line, don't free it */
4247 thead->text = NULL;
4248 delete_Token(thead);
4249 } else {
4250 /* the expression expanded to empty line;
4251 we can't return NULL for some reasons
4252 we just set the line to a single WHITESPACE token. */
4253 memset(org_tline, 0, sizeof(*org_tline));
4254 org_tline->text = NULL;
4255 org_tline->type = TOK_WHITESPACE;
4256 }
4257 thead = org_tline;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004258 }
4259
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004260 return thead;
4261}
4262
4263/*
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004264 * Similar to expand_smacro but used exclusively with macro identifiers
4265 * right before they are fetched in. The reason is that there can be
4266 * identifiers consisting of several subparts. We consider that if there
4267 * are more than one element forming the name, user wants a expansion,
4268 * otherwise it will be left as-is. Example:
4269 *
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004270 * %define %$abc cde
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004271 *
4272 * the identifier %$abc will be left as-is so that the handler for %define
4273 * will suck it and define the corresponding value. Other case:
4274 *
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004275 * %define _%$abc cde
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004276 *
4277 * In this case user wants name to be expanded *before* %define starts
4278 * working, so we'll expand %$abc into something (if it has a value;
4279 * otherwise it will be left as-is) then concatenate all successive
4280 * PP_IDs into one.
4281 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004282static Token *expand_id(Token * tline)
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004283{
4284 Token *cur, *oldnext = NULL;
4285
H. Peter Anvin734b1882002-04-30 21:01:08 +00004286 if (!tline || !tline->next)
H. Peter Anvine2c80182005-01-15 22:15:51 +00004287 return tline;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004288
4289 cur = tline;
4290 while (cur->next &&
H. Peter Anvine2c80182005-01-15 22:15:51 +00004291 (cur->next->type == TOK_ID ||
4292 cur->next->type == TOK_PREPROC_ID
4293 || cur->next->type == TOK_NUMBER))
4294 cur = cur->next;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004295
4296 /* If identifier consists of just one token, don't expand */
4297 if (cur == tline)
H. Peter Anvine2c80182005-01-15 22:15:51 +00004298 return tline;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004299
H. Peter Anvine2c80182005-01-15 22:15:51 +00004300 if (cur) {
4301 oldnext = cur->next; /* Detach the tail past identifier */
4302 cur->next = NULL; /* so that expand_smacro stops here */
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004303 }
4304
H. Peter Anvin734b1882002-04-30 21:01:08 +00004305 tline = expand_smacro(tline);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004306
H. Peter Anvine2c80182005-01-15 22:15:51 +00004307 if (cur) {
4308 /* expand_smacro possibly changhed tline; re-scan for EOL */
4309 cur = tline;
4310 while (cur && cur->next)
4311 cur = cur->next;
4312 if (cur)
4313 cur->next = oldnext;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004314 }
4315
4316 return tline;
4317}
4318
4319/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004320 * Determine whether the given line constitutes a multi-line macro
4321 * call, and return the MMacro structure called if so. Doesn't have
4322 * to check for an initial label - that's taken care of in
4323 * expand_mmacro - but must check numbers of parameters. Guaranteed
4324 * to be called with tline->type == TOK_ID, so the putative macro
4325 * name is easy to find.
4326 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004327static MMacro *is_mmacro(Token * tline, Token *** params_array)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004328{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004329 MMacro *head, *m;
4330 Token **params;
4331 int nparam;
4332
H. Peter Anvin166c2472008-05-28 12:28:58 -07004333 head = (MMacro *) hash_findix(&mmacros, tline->text);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004334
4335 /*
4336 * Efficiency: first we see if any macro exists with the given
4337 * name. If not, we can return NULL immediately. _Then_ we
4338 * count the parameters, and then we look further along the
4339 * list if necessary to find the proper MMacro.
4340 */
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04004341 list_for_each(m, head)
H. Peter Anvine2c80182005-01-15 22:15:51 +00004342 if (!mstrcmp(m->name, tline->text, m->casesense))
4343 break;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004344 if (!m)
H. Peter Anvine2c80182005-01-15 22:15:51 +00004345 return NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004346
4347 /*
4348 * OK, we have a potential macro. Count and demarcate the
4349 * parameters.
4350 */
H. Peter Anvin734b1882002-04-30 21:01:08 +00004351 count_mmac_params(tline->next, &nparam, &params);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004352
4353 /*
4354 * So we know how many parameters we've got. Find the MMacro
4355 * structure that handles this number.
4356 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004357 while (m) {
4358 if (m->nparam_min <= nparam
4359 && (m->plus || nparam <= m->nparam_max)) {
4360 /*
4361 * This one is right. Just check if cycle removal
4362 * prohibits us using it before we actually celebrate...
4363 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004364 if (m->in_progress > m->max_depth) {
4365 if (m->max_depth > 0) {
4366 error(ERR_WARNING,
4367 "reached maximum recursion depth of %i",
4368 m->max_depth);
4369 }
4370 nasm_free(params);
4371 return NULL;
4372 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00004373 /*
4374 * It's right, and we can use it. Add its default
4375 * parameters to the end of our list if necessary.
4376 */
4377 if (m->defaults && nparam < m->nparam_min + m->ndefs) {
4378 params =
4379 nasm_realloc(params,
4380 ((m->nparam_min + m->ndefs +
4381 1) * sizeof(*params)));
4382 while (nparam < m->nparam_min + m->ndefs) {
4383 params[nparam] = m->defaults[nparam - m->nparam_min];
4384 nparam++;
4385 }
4386 }
4387 /*
4388 * If we've gone over the maximum parameter count (and
4389 * we're in Plus mode), ignore parameters beyond
4390 * nparam_max.
4391 */
4392 if (m->plus && nparam > m->nparam_max)
4393 nparam = m->nparam_max;
4394 /*
4395 * Then terminate the parameter list, and leave.
4396 */
4397 if (!params) { /* need this special case */
4398 params = nasm_malloc(sizeof(*params));
4399 nparam = 0;
4400 }
4401 params[nparam] = NULL;
4402 *params_array = params;
4403 return m;
4404 }
4405 /*
4406 * This one wasn't right: look for the next one with the
4407 * same name.
4408 */
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04004409 list_for_each(m, m->next)
H. Peter Anvine2c80182005-01-15 22:15:51 +00004410 if (!mstrcmp(m->name, tline->text, m->casesense))
4411 break;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004412 }
4413
4414 /*
4415 * After all that, we didn't find one with the right number of
4416 * parameters. Issue a warning, and fail to expand the macro.
4417 */
H. Peter Anvin917a3492008-09-24 09:14:49 -07004418 error(ERR_WARNING|ERR_PASS1|ERR_WARN_MNP,
H. Peter Anvine2c80182005-01-15 22:15:51 +00004419 "macro `%s' exists, but not taking %d parameters",
4420 tline->text, nparam);
H. Peter Anvin734b1882002-04-30 21:01:08 +00004421 nasm_free(params);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004422 return NULL;
4423}
4424
Keith Kanios891775e2009-07-11 06:08:54 -05004425
4426/*
4427 * Save MMacro invocation specific fields in
4428 * preparation for a recursive macro expansion
4429 */
4430static void push_mmacro(MMacro *m)
4431{
4432 MMacroInvocation *i;
4433
H. Peter Anvin89cee572009-07-15 09:16:54 -04004434 i = nasm_malloc(sizeof(MMacroInvocation));
4435 i->prev = m->prev;
4436 i->params = m->params;
4437 i->iline = m->iline;
4438 i->nparam = m->nparam;
4439 i->rotate = m->rotate;
4440 i->paramlen = m->paramlen;
4441 i->unique = m->unique;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004442 i->condcnt = m->condcnt;
H. Peter Anvin89cee572009-07-15 09:16:54 -04004443 m->prev = i;
Keith Kanios891775e2009-07-11 06:08:54 -05004444}
4445
4446
4447/*
4448 * Restore MMacro invocation specific fields that were
4449 * saved during a previous recursive macro expansion
4450 */
4451static void pop_mmacro(MMacro *m)
4452{
4453 MMacroInvocation *i;
4454
H. Peter Anvin89cee572009-07-15 09:16:54 -04004455 if (m->prev) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004456 i = m->prev;
4457 m->prev = i->prev;
4458 m->params = i->params;
4459 m->iline = i->iline;
4460 m->nparam = i->nparam;
4461 m->rotate = i->rotate;
4462 m->paramlen = i->paramlen;
4463 m->unique = i->unique;
4464 m->condcnt = i->condcnt;
4465 nasm_free(i);
H. Peter Anvin89cee572009-07-15 09:16:54 -04004466 }
Keith Kanios891775e2009-07-11 06:08:54 -05004467}
4468
4469
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004470/*
4471 * Expand the multi-line macro call made by the given line, if
4472 * there is one to be expanded. If there is, push the expansion on
H. Peter Anvineba20a72002-04-30 20:53:55 +00004473 * istk->expansion and return 1. Otherwise return 0.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004474 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004475static int expand_mmacro(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004476{
4477 Token *startline = tline;
4478 Token *label = NULL;
4479 int dont_prepend = 0;
H. Peter Anvince2233b2008-05-25 21:57:00 -07004480 Token **params, *t, *mtok, *tt;
H. Peter Anvineba20a72002-04-30 20:53:55 +00004481 MMacro *m;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004482 Line *l, *ll;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004483 int i, nparam, *paramlen;
H. Peter Anvinc751e862008-06-09 10:18:45 -07004484 const char *mname;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004485
4486 t = tline;
H. Peter Anvineba20a72002-04-30 20:53:55 +00004487 skip_white_(t);
H. Peter Anvince2233b2008-05-25 21:57:00 -07004488 /* if (!tok_type_(t, TOK_ID)) Lino 02/25/02 */
H. Peter Anvindce1e2f2002-04-30 21:06:37 +00004489 if (!tok_type_(t, TOK_ID) && !tok_type_(t, TOK_PREPROC_ID))
H. Peter Anvine2c80182005-01-15 22:15:51 +00004490 return 0;
H. Peter Anvince2233b2008-05-25 21:57:00 -07004491 mtok = t;
H. Peter Anvin734b1882002-04-30 21:01:08 +00004492 m = is_mmacro(t, &params);
H. Peter Anvinc751e862008-06-09 10:18:45 -07004493 if (m) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004494 mname = t->text;
H. Peter Anvinc751e862008-06-09 10:18:45 -07004495 } else {
H. Peter Anvine2c80182005-01-15 22:15:51 +00004496 Token *last;
4497 /*
4498 * We have an id which isn't a macro call. We'll assume
4499 * it might be a label; we'll also check to see if a
4500 * colon follows it. Then, if there's another id after
4501 * that lot, we'll check it again for macro-hood.
4502 */
4503 label = last = t;
4504 t = t->next;
4505 if (tok_type_(t, TOK_WHITESPACE))
4506 last = t, t = t->next;
4507 if (tok_is_(t, ":")) {
4508 dont_prepend = 1;
4509 last = t, t = t->next;
4510 if (tok_type_(t, TOK_WHITESPACE))
4511 last = t, t = t->next;
4512 }
H. Peter Anvin89cee572009-07-15 09:16:54 -04004513 if (!tok_type_(t, TOK_ID) || !(m = is_mmacro(t, &params)))
H. Peter Anvine2c80182005-01-15 22:15:51 +00004514 return 0;
4515 last->next = NULL;
Keith Kanios891775e2009-07-11 06:08:54 -05004516 mname = t->text;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004517 tline = t;
H. Peter Anvineba20a72002-04-30 20:53:55 +00004518 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004519
4520 /*
4521 * Fix up the parameters: this involves stripping leading and
4522 * trailing whitespace, then stripping braces if they are
4523 * present.
4524 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004525 for (nparam = 0; params[nparam]; nparam++) ;
H. Peter Anvin734b1882002-04-30 21:01:08 +00004526 paramlen = nparam ? nasm_malloc(nparam * sizeof(*paramlen)) : NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004527
H. Peter Anvine2c80182005-01-15 22:15:51 +00004528 for (i = 0; params[i]; i++) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07004529 int brace = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004530 int comma = (!m->plus || i < nparam - 1);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004531
H. Peter Anvine2c80182005-01-15 22:15:51 +00004532 t = params[i];
4533 skip_white_(t);
4534 if (tok_is_(t, "{"))
H. Peter Anvin6867acc2007-10-10 14:58:45 -07004535 t = t->next, brace = true, comma = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004536 params[i] = t;
4537 paramlen[i] = 0;
4538 while (t) {
4539 if (comma && t->type == TOK_OTHER && !strcmp(t->text, ","))
4540 break; /* ... because we have hit a comma */
4541 if (comma && t->type == TOK_WHITESPACE
4542 && tok_is_(t->next, ","))
4543 break; /* ... or a space then a comma */
4544 if (brace && t->type == TOK_OTHER && !strcmp(t->text, "}"))
4545 break; /* ... or a brace */
4546 t = t->next;
4547 paramlen[i]++;
4548 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004549 }
4550
4551 /*
4552 * OK, we have a MMacro structure together with a set of
4553 * parameters. We must now go through the expansion and push
H. Peter Anvin76690a12002-04-30 20:52:49 +00004554 * copies of each Line on to istk->expansion. Substitution of
4555 * parameter tokens and macro-local tokens doesn't get done
4556 * until the single-line macro substitution process; this is
4557 * because delaying them allows us to change the semantics
4558 * later through %rotate.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004559 *
H. Peter Anvin76690a12002-04-30 20:52:49 +00004560 * First, push an end marker on to istk->expansion, mark this
4561 * macro as in progress, and set up its invocation-specific
4562 * variables.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004563 */
4564 ll = nasm_malloc(sizeof(Line));
4565 ll->next = istk->expansion;
4566 ll->finishes = m;
4567 ll->first = NULL;
4568 istk->expansion = ll;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004569
H. Peter Anvin89cee572009-07-15 09:16:54 -04004570 /*
4571 * Save the previous MMacro expansion in the case of
4572 * macro recursion
4573 */
4574 if (m->max_depth && m->in_progress)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004575 push_mmacro(m);
H. Peter Anvin76690a12002-04-30 20:52:49 +00004576
Keith Kanios891775e2009-07-11 06:08:54 -05004577 m->in_progress ++;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004578 m->params = params;
4579 m->iline = tline;
4580 m->nparam = nparam;
4581 m->rotate = 0;
4582 m->paramlen = paramlen;
4583 m->unique = unique++;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004584 m->lineno = 0;
Keith Kanios3c0d91f2009-10-25 13:28:03 -05004585 m->condcnt = 0;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004586
4587 m->next_active = istk->mstk;
4588 istk->mstk = m;
4589
Cyrill Gorcunov367d59e2010-04-09 15:43:03 +04004590 list_for_each(l, m->expansion) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00004591 Token **tail;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004592
H. Peter Anvine2c80182005-01-15 22:15:51 +00004593 ll = nasm_malloc(sizeof(Line));
4594 ll->finishes = NULL;
4595 ll->next = istk->expansion;
4596 istk->expansion = ll;
4597 tail = &ll->first;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004598
Cyrill Gorcunov367d59e2010-04-09 15:43:03 +04004599 list_for_each(t, l->first) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00004600 Token *x = t;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004601 switch (t->type) {
4602 case TOK_PREPROC_Q:
4603 tt = *tail = new_Token(NULL, TOK_ID, mname, 0);
4604 break;
4605 case TOK_PREPROC_QQ:
4606 tt = *tail = new_Token(NULL, TOK_ID, m->name, 0);
4607 break;
4608 case TOK_PREPROC_ID:
4609 if (t->text[1] == '0' && t->text[2] == '0') {
4610 dont_prepend = -1;
4611 x = label;
4612 if (!x)
4613 continue;
4614 }
4615 /* fall through */
4616 default:
4617 tt = *tail = new_Token(NULL, x->type, x->text, 0);
4618 break;
4619 }
4620 tail = &tt->next;
4621 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00004622 *tail = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004623 }
4624
4625 /*
H. Peter Anvineba20a72002-04-30 20:53:55 +00004626 * If we had a label, push it on as the first line of
4627 * the macro expansion.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004628 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004629 if (label) {
4630 if (dont_prepend < 0)
4631 free_tlist(startline);
4632 else {
4633 ll = nasm_malloc(sizeof(Line));
4634 ll->finishes = NULL;
4635 ll->next = istk->expansion;
4636 istk->expansion = ll;
4637 ll->first = startline;
4638 if (!dont_prepend) {
4639 while (label->next)
4640 label = label->next;
4641 label->next = tt = new_Token(NULL, TOK_OTHER, ":", 0);
4642 }
4643 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004644 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004645
H. Peter Anvin734b1882002-04-30 21:01:08 +00004646 list->uplevel(m->nolist ? LIST_MACRO_NOLIST : LIST_MACRO);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004647
H. Peter Anvineba20a72002-04-30 20:53:55 +00004648 return 1;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004649}
4650
Victor van den Elzen3b404c02008-09-18 13:51:36 +02004651/* The function that actually does the error reporting */
4652static void verror(int severity, const char *fmt, va_list arg)
4653{
4654 char buff[1024];
4655
4656 vsnprintf(buff, sizeof(buff), fmt, arg);
4657
4658 if (istk && istk->mstk && istk->mstk->name)
H. Peter Anvindbb640b2009-07-18 18:57:16 -07004659 nasm_error(severity, "(%s:%d) %s", istk->mstk->name,
Victor van den Elzen3b404c02008-09-18 13:51:36 +02004660 istk->mstk->lineno, buff);
4661 else
H. Peter Anvindbb640b2009-07-18 18:57:16 -07004662 nasm_error(severity, "%s", buff);
Victor van den Elzen3b404c02008-09-18 13:51:36 +02004663}
4664
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004665/*
4666 * Since preprocessor always operate only on the line that didn't
H. Peter Anvin917a3492008-09-24 09:14:49 -07004667 * arrived yet, we should always use ERR_OFFBY1.
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004668 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004669static void error(int severity, const char *fmt, ...)
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004670{
4671 va_list arg;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004672
4673 /* If we're in a dead branch of IF or something like it, ignore the error */
H. Peter Anvin77ba0c62002-05-14 03:18:53 +00004674 if (istk && istk->conds && !emitting(istk->conds->state))
H. Peter Anvine2c80182005-01-15 22:15:51 +00004675 return;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004676
H. Peter Anvin734b1882002-04-30 21:01:08 +00004677 va_start(arg, fmt);
Victor van den Elzen3b404c02008-09-18 13:51:36 +02004678 verror(severity, fmt, arg);
H. Peter Anvin734b1882002-04-30 21:01:08 +00004679 va_end(arg);
Victor van den Elzen3b404c02008-09-18 13:51:36 +02004680}
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004681
Victor van den Elzen3b404c02008-09-18 13:51:36 +02004682/*
4683 * Because %else etc are evaluated in the state context
4684 * of the previous branch, errors might get lost with error():
4685 * %if 0 ... %else trailing garbage ... %endif
4686 * So %else etc should report errors with this function.
4687 */
4688static void error_precond(int severity, const char *fmt, ...)
4689{
4690 va_list arg;
4691
4692 /* Only ignore the error if it's really in a dead branch */
4693 if (istk && istk->conds && istk->conds->state == COND_NEVER)
4694 return;
4695
4696 va_start(arg, fmt);
4697 verror(severity, fmt, arg);
4698 va_end(arg);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004699}
4700
H. Peter Anvin734b1882002-04-30 21:01:08 +00004701static void
H. Peter Anvindbb640b2009-07-18 18:57:16 -07004702pp_reset(char *file, int apass, ListGen * listgen, StrList **deplist)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004703{
H. Peter Anvin7383b402008-09-24 10:20:40 -07004704 Token *t;
4705
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004706 cstk = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004707 istk = nasm_malloc(sizeof(Include));
4708 istk->next = NULL;
4709 istk->conds = NULL;
4710 istk->expansion = NULL;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004711 istk->mstk = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004712 istk->fp = fopen(file, "r");
H. Peter Anvineba20a72002-04-30 20:53:55 +00004713 istk->fname = NULL;
4714 src_set_fname(nasm_strdup(file));
4715 src_set_linnum(0);
4716 istk->lineinc = 1;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004717 if (!istk->fp)
H. Peter Anvin917a3492008-09-24 09:14:49 -07004718 error(ERR_FATAL|ERR_NOFILE, "unable to open input file `%s'",
H. Peter Anvine2c80182005-01-15 22:15:51 +00004719 file);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004720 defining = NULL;
Charles Crayned4200be2008-07-12 16:42:33 -07004721 nested_mac_count = 0;
4722 nested_rep_count = 0;
H. Peter Anvin97a23472007-09-16 17:57:25 -07004723 init_macros();
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004724 unique = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004725 if (tasm_compatible_mode) {
H. Peter Anvina4835d42008-05-20 14:21:29 -07004726 stdmacpos = nasm_stdmac;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004727 } else {
H. Peter Anvina4835d42008-05-20 14:21:29 -07004728 stdmacpos = nasm_stdmac_after_tasm;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004729 }
H. Peter Anvind2456592008-06-19 15:04:18 -07004730 any_extrastdmac = extrastdmac && *extrastdmac;
4731 do_predef = true;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004732 list = listgen;
H. Peter Anvin61f130f2008-09-25 15:45:06 -07004733
4734 /*
4735 * 0 for dependencies, 1 for preparatory passes, 2 for final pass.
4736 * The caller, however, will also pass in 3 for preprocess-only so
4737 * we can set __PASS__ accordingly.
4738 */
4739 pass = apass > 2 ? 2 : apass;
4740
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07004741 dephead = deptail = deplist;
4742 if (deplist) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004743 StrList *sl = nasm_malloc(strlen(file)+1+sizeof sl->next);
4744 sl->next = NULL;
4745 strcpy(sl->str, file);
4746 *deptail = sl;
4747 deptail = &sl->next;
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07004748 }
H. Peter Anvin7383b402008-09-24 10:20:40 -07004749
H. Peter Anvin61f130f2008-09-25 15:45:06 -07004750 /*
4751 * Define the __PASS__ macro. This is defined here unlike
4752 * all the other builtins, because it is special -- it varies between
4753 * passes.
4754 */
H. Peter Anvin7383b402008-09-24 10:20:40 -07004755 t = nasm_malloc(sizeof(*t));
4756 t->next = NULL;
H. Peter Anvin61f130f2008-09-25 15:45:06 -07004757 make_tok_num(t, apass);
H. Peter Anvin7383b402008-09-24 10:20:40 -07004758 t->a.mac = NULL;
4759 define_smacro(NULL, "__PASS__", true, 0, t);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004760}
4761
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004762static char *pp_getline(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004763{
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004764 char *line;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004765 Token *tline;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004766
H. Peter Anvine2c80182005-01-15 22:15:51 +00004767 while (1) {
4768 /*
Keith Kaniosb7a89542007-04-12 02:40:54 +00004769 * Fetch a tokenized line, either from the macro-expansion
H. Peter Anvine2c80182005-01-15 22:15:51 +00004770 * buffer or from the input file.
4771 */
4772 tline = NULL;
4773 while (istk->expansion && istk->expansion->finishes) {
4774 Line *l = istk->expansion;
4775 if (!l->finishes->name && l->finishes->in_progress > 1) {
4776 Line *ll;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004777
H. Peter Anvine2c80182005-01-15 22:15:51 +00004778 /*
4779 * This is a macro-end marker for a macro with no
4780 * name, which means it's not really a macro at all
4781 * but a %rep block, and the `in_progress' field is
4782 * more than 1, meaning that we still need to
4783 * repeat. (1 means the natural last repetition; 0
4784 * means termination by %exitrep.) We have
4785 * therefore expanded up to the %endrep, and must
4786 * push the whole block on to the expansion buffer
4787 * again. We don't bother to remove the macro-end
4788 * marker: we'd only have to generate another one
4789 * if we did.
4790 */
4791 l->finishes->in_progress--;
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04004792 list_for_each(l, l->finishes->expansion) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00004793 Token *t, *tt, **tail;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004794
H. Peter Anvine2c80182005-01-15 22:15:51 +00004795 ll = nasm_malloc(sizeof(Line));
4796 ll->next = istk->expansion;
4797 ll->finishes = NULL;
4798 ll->first = NULL;
4799 tail = &ll->first;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004800
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04004801 list_for_each(t, l->first) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00004802 if (t->text || t->type == TOK_WHITESPACE) {
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04004803 tt = *tail = new_Token(NULL, t->type, t->text, 0);
H. Peter Anvine2c80182005-01-15 22:15:51 +00004804 tail = &tt->next;
4805 }
4806 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00004807
H. Peter Anvine2c80182005-01-15 22:15:51 +00004808 istk->expansion = ll;
4809 }
4810 } else {
4811 /*
4812 * Check whether a `%rep' was started and not ended
4813 * within this macro expansion. This can happen and
4814 * should be detected. It's a fatal error because
4815 * I'm too confused to work out how to recover
4816 * sensibly from it.
4817 */
4818 if (defining) {
4819 if (defining->name)
4820 error(ERR_PANIC,
4821 "defining with name in expansion");
4822 else if (istk->mstk->name)
4823 error(ERR_FATAL,
4824 "`%%rep' without `%%endrep' within"
4825 " expansion of macro `%s'",
4826 istk->mstk->name);
4827 }
H. Peter Anvin87bc6192002-04-30 20:53:16 +00004828
H. Peter Anvine2c80182005-01-15 22:15:51 +00004829 /*
4830 * FIXME: investigate the relationship at this point between
4831 * istk->mstk and l->finishes
4832 */
4833 {
4834 MMacro *m = istk->mstk;
4835 istk->mstk = m->next_active;
4836 if (m->name) {
4837 /*
4838 * This was a real macro call, not a %rep, and
4839 * therefore the parameter information needs to
4840 * be freed.
4841 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004842 if (m->prev) {
4843 pop_mmacro(m);
4844 l->finishes->in_progress --;
4845 } else {
Keith Kanios891775e2009-07-11 06:08:54 -05004846 nasm_free(m->params);
4847 free_tlist(m->iline);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004848 nasm_free(m->paramlen);
4849 l->finishes->in_progress = 0;
4850 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00004851 } else
4852 free_mmacro(m);
4853 }
4854 istk->expansion = l->next;
4855 nasm_free(l);
4856 list->downlevel(LIST_MACRO);
4857 }
4858 }
4859 while (1) { /* until we get a line we can use */
H. Peter Anvineba20a72002-04-30 20:53:55 +00004860
H. Peter Anvine2c80182005-01-15 22:15:51 +00004861 if (istk->expansion) { /* from a macro expansion */
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004862 char *p;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004863 Line *l = istk->expansion;
4864 if (istk->mstk)
4865 istk->mstk->lineno++;
4866 tline = l->first;
4867 istk->expansion = l->next;
4868 nasm_free(l);
H. Peter Anvin6867acc2007-10-10 14:58:45 -07004869 p = detoken(tline, false);
H. Peter Anvine2c80182005-01-15 22:15:51 +00004870 list->line(LIST_MACRO, p);
4871 nasm_free(p);
4872 break;
4873 }
4874 line = read_line();
4875 if (line) { /* from the current input file */
4876 line = prepreproc(line);
Keith Kaniosb7a89542007-04-12 02:40:54 +00004877 tline = tokenize(line);
H. Peter Anvine2c80182005-01-15 22:15:51 +00004878 nasm_free(line);
4879 break;
4880 }
4881 /*
4882 * The current file has ended; work down the istk
4883 */
4884 {
4885 Include *i = istk;
4886 fclose(i->fp);
4887 if (i->conds)
4888 error(ERR_FATAL,
4889 "expected `%%endif' before end of file");
4890 /* only set line and file name if there's a next node */
4891 if (i->next) {
4892 src_set_linnum(i->lineno);
4893 nasm_free(src_set_fname(i->fname));
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004894 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00004895 istk = i->next;
4896 list->downlevel(LIST_INCLUDE);
4897 nasm_free(i);
4898 if (!istk)
4899 return NULL;
Victor van den Elzen4c9d6222008-10-01 13:08:50 +02004900 if (istk->expansion && istk->expansion->finishes)
4901 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004902 }
4903 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004904
H. Peter Anvine2c80182005-01-15 22:15:51 +00004905 /*
4906 * We must expand MMacro parameters and MMacro-local labels
4907 * _before_ we plunge into directive processing, to cope
4908 * with things like `%define something %1' such as STRUC
4909 * uses. Unless we're _defining_ a MMacro, in which case
4910 * those tokens should be left alone to go into the
4911 * definition; and unless we're in a non-emitting
4912 * condition, in which case we don't want to meddle with
4913 * anything.
4914 */
Charles Crayned4200be2008-07-12 16:42:33 -07004915 if (!defining && !(istk->conds && !emitting(istk->conds->state))
H. Peter Anvin992fe752008-10-19 15:45:05 -07004916 && !(istk->mstk && !istk->mstk->in_progress)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00004917 tline = expand_mmac_params(tline);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004918 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00004919
H. Peter Anvine2c80182005-01-15 22:15:51 +00004920 /*
4921 * Check the line to see if it's a preprocessor directive.
4922 */
4923 if (do_directive(tline) == DIRECTIVE_FOUND) {
4924 continue;
4925 } else if (defining) {
4926 /*
4927 * We're defining a multi-line macro. We emit nothing
4928 * at all, and just
Keith Kaniosb7a89542007-04-12 02:40:54 +00004929 * shove the tokenized line on to the macro definition.
H. Peter Anvine2c80182005-01-15 22:15:51 +00004930 */
4931 Line *l = nasm_malloc(sizeof(Line));
4932 l->next = defining->expansion;
4933 l->first = tline;
H. Peter Anvin538002d2008-06-28 18:30:27 -07004934 l->finishes = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004935 defining->expansion = l;
4936 continue;
4937 } else if (istk->conds && !emitting(istk->conds->state)) {
4938 /*
4939 * We're in a non-emitting branch of a condition block.
4940 * Emit nothing at all, not even a blank line: when we
4941 * emerge from the condition we'll give a line-number
4942 * directive so we keep our place correctly.
4943 */
4944 free_tlist(tline);
4945 continue;
4946 } else if (istk->mstk && !istk->mstk->in_progress) {
4947 /*
4948 * We're in a %rep block which has been terminated, so
4949 * we're walking through to the %endrep without
4950 * emitting anything. Emit nothing at all, not even a
4951 * blank line: when we emerge from the %rep block we'll
4952 * give a line-number directive so we keep our place
4953 * correctly.
4954 */
4955 free_tlist(tline);
4956 continue;
4957 } else {
4958 tline = expand_smacro(tline);
4959 if (!expand_mmacro(tline)) {
4960 /*
Keith Kaniosb7a89542007-04-12 02:40:54 +00004961 * De-tokenize the line again, and emit it.
H. Peter Anvine2c80182005-01-15 22:15:51 +00004962 */
H. Peter Anvin6867acc2007-10-10 14:58:45 -07004963 line = detoken(tline, true);
H. Peter Anvine2c80182005-01-15 22:15:51 +00004964 free_tlist(tline);
4965 break;
4966 } else {
4967 continue; /* expand_mmacro calls free_tlist */
4968 }
4969 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004970 }
4971
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004972 return line;
4973}
4974
H. Peter Anvine2c80182005-01-15 22:15:51 +00004975static void pp_cleanup(int pass)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004976{
H. Peter Anvine2c80182005-01-15 22:15:51 +00004977 if (defining) {
H. Peter Anvin89cee572009-07-15 09:16:54 -04004978 if (defining->name) {
Victor van den Elzen8f1120f2008-07-16 13:41:37 +02004979 error(ERR_NONFATAL,
4980 "end of file while still defining macro `%s'",
4981 defining->name);
4982 } else {
4983 error(ERR_NONFATAL, "end of file while still in %%rep");
4984 }
4985
H. Peter Anvine2c80182005-01-15 22:15:51 +00004986 free_mmacro(defining);
Cyrill Gorcunova327c652010-02-14 17:19:38 +03004987 defining = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004988 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004989 while (cstk)
H. Peter Anvine2c80182005-01-15 22:15:51 +00004990 ctx_pop();
H. Peter Anvin97a23472007-09-16 17:57:25 -07004991 free_macros();
H. Peter Anvine2c80182005-01-15 22:15:51 +00004992 while (istk) {
4993 Include *i = istk;
4994 istk = istk->next;
4995 fclose(i->fp);
4996 nasm_free(i->fname);
4997 nasm_free(i);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004998 }
4999 while (cstk)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005000 ctx_pop();
H. Peter Anvin86877b22008-06-20 15:55:45 -07005001 nasm_free(src_set_fname(NULL));
H. Peter Anvine2c80182005-01-15 22:15:51 +00005002 if (pass == 0) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03005003 IncPath *i;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005004 free_llist(predef);
5005 delete_Blocks();
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03005006 while ((i = ipath)) {
5007 ipath = i->next;
5008 if (i->path)
5009 nasm_free(i->path);
5010 nasm_free(i);
5011 }
H. Peter Anvin11dfa1a2008-07-02 18:11:04 -07005012 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005013}
5014
Keith Kaniosa6dfa782007-04-13 16:47:53 +00005015void pp_include_path(char *path)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005016{
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005017 IncPath *i;
H. Peter Anvin37a321f2007-09-24 13:41:58 -07005018
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005019 i = nasm_malloc(sizeof(IncPath));
H. Peter Anvin37a321f2007-09-24 13:41:58 -07005020 i->path = path ? nasm_strdup(path) : NULL;
Frank Kotlerd0ed6fd2003-08-27 11:33:56 +00005021 i->next = NULL;
5022
H. Peter Anvin89cee572009-07-15 09:16:54 -04005023 if (ipath) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00005024 IncPath *j = ipath;
H. Peter Anvin89cee572009-07-15 09:16:54 -04005025 while (j->next)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005026 j = j->next;
5027 j->next = i;
5028 } else {
5029 ipath = i;
Frank Kotlerd0ed6fd2003-08-27 11:33:56 +00005030 }
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005031}
Frank Kotlerd0ed6fd2003-08-27 11:33:56 +00005032
Keith Kaniosa6dfa782007-04-13 16:47:53 +00005033void pp_pre_include(char *fname)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005034{
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005035 Token *inc, *space, *name;
5036 Line *l;
5037
H. Peter Anvin734b1882002-04-30 21:01:08 +00005038 name = new_Token(NULL, TOK_INTERNAL_STRING, fname, 0);
5039 space = new_Token(name, TOK_WHITESPACE, NULL, 0);
5040 inc = new_Token(space, TOK_PREPROC_ID, "%include", 0);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005041
5042 l = nasm_malloc(sizeof(Line));
5043 l->next = predef;
5044 l->first = inc;
H. Peter Anvin538002d2008-06-28 18:30:27 -07005045 l->finishes = NULL;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005046 predef = l;
5047}
5048
Keith Kaniosa6dfa782007-04-13 16:47:53 +00005049void pp_pre_define(char *definition)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005050{
5051 Token *def, *space;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005052 Line *l;
Keith Kaniosa6dfa782007-04-13 16:47:53 +00005053 char *equals;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005054
5055 equals = strchr(definition, '=');
H. Peter Anvin734b1882002-04-30 21:01:08 +00005056 space = new_Token(NULL, TOK_WHITESPACE, NULL, 0);
5057 def = new_Token(space, TOK_PREPROC_ID, "%define", 0);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005058 if (equals)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005059 *equals = ' ';
Keith Kaniosb7a89542007-04-12 02:40:54 +00005060 space->next = tokenize(definition);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005061 if (equals)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005062 *equals = '=';
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005063
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005064 l = nasm_malloc(sizeof(Line));
5065 l->next = predef;
5066 l->first = def;
H. Peter Anvin538002d2008-06-28 18:30:27 -07005067 l->finishes = NULL;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005068 predef = l;
5069}
5070
Keith Kaniosa6dfa782007-04-13 16:47:53 +00005071void pp_pre_undefine(char *definition)
H. Peter Anvin620515a2002-04-30 20:57:38 +00005072{
5073 Token *def, *space;
5074 Line *l;
5075
H. Peter Anvin734b1882002-04-30 21:01:08 +00005076 space = new_Token(NULL, TOK_WHITESPACE, NULL, 0);
5077 def = new_Token(space, TOK_PREPROC_ID, "%undef", 0);
Keith Kaniosb7a89542007-04-12 02:40:54 +00005078 space->next = tokenize(definition);
H. Peter Anvin620515a2002-04-30 20:57:38 +00005079
5080 l = nasm_malloc(sizeof(Line));
5081 l->next = predef;
5082 l->first = def;
H. Peter Anvin538002d2008-06-28 18:30:27 -07005083 l->finishes = NULL;
H. Peter Anvin620515a2002-04-30 20:57:38 +00005084 predef = l;
5085}
5086
Keith Kaniosb7a89542007-04-12 02:40:54 +00005087/*
5088 * Added by Keith Kanios:
5089 *
5090 * This function is used to assist with "runtime" preprocessor
5091 * directives. (e.g. pp_runtime("%define __BITS__ 64");)
5092 *
5093 * ERRORS ARE IGNORED HERE, SO MAKE COMPLETELY SURE THAT YOU
5094 * PASS A VALID STRING TO THIS FUNCTION!!!!!
5095 */
5096
Keith Kaniosa6dfa782007-04-13 16:47:53 +00005097void pp_runtime(char *definition)
Keith Kaniosb7a89542007-04-12 02:40:54 +00005098{
5099 Token *def;
H. Peter Anvin70653092007-10-19 14:42:29 -07005100
Keith Kaniosb7a89542007-04-12 02:40:54 +00005101 def = tokenize(definition);
H. Peter Anvin89cee572009-07-15 09:16:54 -04005102 if (do_directive(def) == NO_DIRECTIVE_FOUND)
Keith Kaniosb7a89542007-04-12 02:40:54 +00005103 free_tlist(def);
H. Peter Anvin70653092007-10-19 14:42:29 -07005104
Keith Kaniosb7a89542007-04-12 02:40:54 +00005105}
5106
H. Peter Anvina70547f2008-07-19 21:44:26 -07005107void pp_extra_stdmac(macros_t *macros)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005108{
H. Peter Anvin76690a12002-04-30 20:52:49 +00005109 extrastdmac = macros;
5110}
5111
Keith Kaniosa5fc6462007-10-13 07:09:22 -07005112static void make_tok_num(Token * tok, int64_t val)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005113{
Keith Kaniosa6dfa782007-04-13 16:47:53 +00005114 char numbuf[20];
Keith Kaniosa5fc6462007-10-13 07:09:22 -07005115 snprintf(numbuf, sizeof(numbuf), "%"PRId64"", val);
H. Peter Anvineba20a72002-04-30 20:53:55 +00005116 tok->text = nasm_strdup(numbuf);
5117 tok->type = TOK_NUMBER;
5118}
5119
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005120Preproc nasmpp = {
5121 pp_reset,
5122 pp_getline,
5123 pp_cleanup
5124};