blob: aa171e9b6107a969c05fd51006bbc51f0d0aca3e [file] [log] [blame]
H. Peter Anvin9e6747c2009-06-28 17:13:04 -07001/* ----------------------------------------------------------------------- *
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002 *
3 * Copyright 1996-2010 The NASM Authors - All Rights Reserved
H. Peter Anvin9e6747c2009-06-28 17:13:04 -07004 * See the file AUTHORS included with the NASM distribution for
5 * the specific copyright holders.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00006 *
H. Peter Anvin9e6747c2009-06-28 17:13:04 -07007 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following
9 * conditions are met:
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000010 *
H. Peter Anvin9e6747c2009-06-28 17:13:04 -070011 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above
14 * copyright notice, this list of conditions and the following
15 * disclaimer in the documentation and/or other materials provided
16 * with the distribution.
Cyrill Gorcunovaccda192010-02-16 10:27:56 +030017 *
H. Peter Anvin9e6747c2009-06-28 17:13:04 -070018 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
19 * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
20 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
21 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
23 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
26 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
29 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
30 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 *
32 * ----------------------------------------------------------------------- */
33
34/*
35 * preproc.c macro preprocessor for the Netwide Assembler
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000036 */
37
H. Peter Anvin4836e332002-04-30 20:56:43 +000038/* Typical flow of text through preproc
39 *
Keith Kaniosb7a89542007-04-12 02:40:54 +000040 * pp_getline gets tokenized lines, either
H. Peter Anvin4836e332002-04-30 20:56:43 +000041 *
42 * from a macro expansion
43 *
44 * or
45 * {
46 * read_line gets raw text from stdmacpos, or predef, or current input file
Keith Kaniosb7a89542007-04-12 02:40:54 +000047 * tokenize converts to tokens
H. Peter Anvin4836e332002-04-30 20:56:43 +000048 * }
49 *
50 * expand_mmac_params is used to expand %1 etc., unless a macro is being
51 * defined or a false conditional is being processed
52 * (%0, %1, %+1, %-1, %%foo
53 *
54 * do_directive checks for directives
55 *
56 * expand_smacro is used to expand single line macros
57 *
58 * expand_mmacro is used to expand multi-line macros
59 *
60 * detoken is used to convert the line back to text
61 */
H. Peter Anvineba20a72002-04-30 20:53:55 +000062
H. Peter Anvinfe501952007-10-02 21:53:51 -070063#include "compiler.h"
64
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000065#include <stdio.h>
H. Peter Anvinaf535c12002-04-30 20:59:21 +000066#include <stdarg.h>
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000067#include <stdlib.h>
68#include <stddef.h>
69#include <string.h>
70#include <ctype.h>
H. Peter Anvin76690a12002-04-30 20:52:49 +000071#include <limits.h>
Keith Kaniosb7a89542007-04-12 02:40:54 +000072#include <inttypes.h>
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000073
74#include "nasm.h"
75#include "nasmlib.h"
H. Peter Anvin4169a472007-09-12 01:29:43 +000076#include "preproc.h"
H. Peter Anvin97a23472007-09-16 17:57:25 -070077#include "hashtbl.h"
H. Peter Anvin8cad14b2008-06-01 17:23:51 -070078#include "quote.h"
H. Peter Anvinc2df2822007-10-24 15:29:28 -070079#include "stdscan.h"
H. Peter Anvindbb640b2009-07-18 18:57:16 -070080#include "eval.h"
H. Peter Anvinc2df2822007-10-24 15:29:28 -070081#include "tokens.h"
H. Peter Anvina4835d42008-05-20 14:21:29 -070082#include "tables.h"
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000083
84typedef struct SMacro SMacro;
85typedef struct MMacro MMacro;
Keith Kanios891775e2009-07-11 06:08:54 -050086typedef struct MMacroInvocation MMacroInvocation;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000087typedef struct Context Context;
88typedef struct Token Token;
H. Peter Anvince616072002-04-30 21:02:23 +000089typedef struct Blocks Blocks;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000090typedef struct Line Line;
91typedef struct Include Include;
92typedef struct Cond Cond;
H. Peter Anvin6768eb72002-04-30 20:52:26 +000093typedef struct IncPath IncPath;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000094
95/*
H. Peter Anvin97a23472007-09-16 17:57:25 -070096 * Note on the storage of both SMacro and MMacros: the hash table
97 * indexes them case-insensitively, and we then have to go through a
98 * linked list of potential case aliases (and, for MMacros, parameter
99 * ranges); this is to preserve the matching semantics of the earlier
100 * code. If the number of case aliases for a specific macro is a
101 * performance issue, you may want to reconsider your coding style.
102 */
103
104/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000105 * Store the definition of a single-line macro.
106 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000107struct SMacro {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000108 SMacro *next;
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000109 char *name;
H. Peter Anvin70055962007-10-11 00:05:31 -0700110 bool casesense;
H. Peter Anvin16ed4382007-10-11 10:06:19 -0700111 bool in_progress;
H. Peter Anvin70055962007-10-11 00:05:31 -0700112 unsigned int nparam;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000113 Token *expansion;
114};
115
116/*
H. Peter Anvin76690a12002-04-30 20:52:49 +0000117 * Store the definition of a multi-line macro. This is also used to
118 * store the interiors of `%rep...%endrep' blocks, which are
119 * effectively self-re-invoking multi-line macros which simply
120 * don't have a name or bother to appear in the hash tables. %rep
121 * blocks are signified by having a NULL `name' field.
122 *
123 * In a MMacro describing a `%rep' block, the `in_progress' field
124 * isn't merely boolean, but gives the number of repeats left to
125 * run.
126 *
127 * The `next' field is used for storing MMacros in hash tables; the
128 * `next_active' field is for stacking them on istk entries.
129 *
130 * When a MMacro is being expanded, `params', `iline', `nparam',
131 * `paramlen', `rotate' and `unique' are local to the invocation.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000132 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000133struct MMacro {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000134 MMacro *next;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300135 MMacroInvocation *prev; /* previous invocation */
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000136 char *name;
H. Peter Anvin8cfdb9d2007-09-14 18:36:01 -0700137 int nparam_min, nparam_max;
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700138 bool casesense;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300139 bool plus; /* is the last parameter greedy? */
140 bool nolist; /* is this macro listing-inhibited? */
141 int64_t in_progress; /* is this macro currently being expanded? */
142 int32_t max_depth; /* maximum number of recursive expansions allowed */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000143 Token *dlist; /* All defaults as one list */
144 Token **defaults; /* Parameter default pointers */
145 int ndefs; /* number of default parameters */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000146 Line *expansion;
H. Peter Anvin76690a12002-04-30 20:52:49 +0000147
148 MMacro *next_active;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000149 MMacro *rep_nest; /* used for nesting %rep */
150 Token **params; /* actual parameters */
151 Token *iline; /* invocation line */
H. Peter Anvin25a99342007-09-22 17:45:45 -0700152 unsigned int nparam, rotate;
153 int *paramlen;
H. Peter Anvinf8ba53e2007-10-11 10:11:57 -0700154 uint64_t unique;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000155 int lineno; /* Current line number on expansion */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300156 uint64_t condcnt; /* number of if blocks... */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000157};
158
Keith Kanios891775e2009-07-11 06:08:54 -0500159
160/* Store the definition of a multi-line macro, as defined in a
161 * previous recursive macro expansion.
162 */
163struct MMacroInvocation {
H. Peter Anvin89cee572009-07-15 09:16:54 -0400164 MMacroInvocation *prev; /* previous invocation */
165 Token **params; /* actual parameters */
166 Token *iline; /* invocation line */
Keith Kanios891775e2009-07-11 06:08:54 -0500167 unsigned int nparam, rotate;
168 int *paramlen;
169 uint64_t unique;
Keith Kanios3c0d91f2009-10-25 13:28:03 -0500170 uint64_t condcnt;
Keith Kanios891775e2009-07-11 06:08:54 -0500171};
172
173
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000174/*
175 * The context stack is composed of a linked list of these.
176 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000177struct Context {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000178 Context *next;
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000179 char *name;
H. Peter Anvin166c2472008-05-28 12:28:58 -0700180 struct hash_table localmac;
Keith Kaniosb7a89542007-04-12 02:40:54 +0000181 uint32_t number;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000182};
183
184/*
185 * This is the internal form which we break input lines up into.
186 * Typically stored in linked lists.
187 *
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000188 * Note that `type' serves a double meaning: TOK_SMAC_PARAM is not
189 * necessarily used as-is, but is intended to denote the number of
190 * the substituted parameter. So in the definition
191 *
192 * %define a(x,y) ( (x) & ~(y) )
H. Peter Anvin70653092007-10-19 14:42:29 -0700193 *
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000194 * the token representing `x' will have its type changed to
195 * TOK_SMAC_PARAM, but the one representing `y' will be
196 * TOK_SMAC_PARAM+1.
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000197 *
198 * TOK_INTERNAL_STRING is a dirty hack: it's a single string token
199 * which doesn't need quotes around it. Used in the pre-include
200 * mechanism as an alternative to trying to find a sensible type of
201 * quote to use on the filename we were passed.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000202 */
H. Peter Anvinda10e7b2007-09-12 04:18:37 +0000203enum pp_token_type {
204 TOK_NONE = 0, TOK_WHITESPACE, TOK_COMMENT, TOK_ID,
205 TOK_PREPROC_ID, TOK_STRING,
H. Peter Anvin6c81f0a2008-05-25 21:46:17 -0700206 TOK_NUMBER, TOK_FLOAT, TOK_SMAC_END, TOK_OTHER,
207 TOK_INTERNAL_STRING,
208 TOK_PREPROC_Q, TOK_PREPROC_QQ,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300209 TOK_PASTE, /* %+ */
210 TOK_INDIRECT, /* %[...] */
211 TOK_SMAC_PARAM, /* MUST BE LAST IN THE LIST!!! */
212 TOK_MAX = INT_MAX /* Keep compiler from reducing the range */
H. Peter Anvinda10e7b2007-09-12 04:18:37 +0000213};
214
H. Peter Anvine2c80182005-01-15 22:15:51 +0000215struct Token {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000216 Token *next;
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000217 char *text;
H. Peter Anvinf26e0972008-07-01 21:26:27 -0700218 union {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300219 SMacro *mac; /* associated macro for TOK_SMAC_END */
220 size_t len; /* scratch length field */
221 } a; /* Auxiliary data */
H. Peter Anvinda10e7b2007-09-12 04:18:37 +0000222 enum pp_token_type type;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000223};
224
225/*
226 * Multi-line macro definitions are stored as a linked list of
227 * these, which is essentially a container to allow several linked
228 * lists of Tokens.
H. Peter Anvin70653092007-10-19 14:42:29 -0700229 *
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000230 * Note that in this module, linked lists are treated as stacks
231 * wherever possible. For this reason, Lines are _pushed_ on to the
232 * `expansion' field in MMacro structures, so that the linked list,
233 * if walked, would give the macro lines in reverse order; this
234 * means that we can walk the list when expanding a macro, and thus
235 * push the lines on to the `expansion' field in _istk_ in reverse
236 * order (so that when popped back off they are in the right
237 * order). It may seem cockeyed, and it relies on my design having
238 * an even number of steps in, but it works...
239 *
240 * Some of these structures, rather than being actual lines, are
241 * markers delimiting the end of the expansion of a given macro.
H. Peter Anvin76690a12002-04-30 20:52:49 +0000242 * This is for use in the cycle-tracking and %rep-handling code.
243 * Such structures have `finishes' non-NULL, and `first' NULL. All
244 * others have `finishes' NULL, but `first' may still be NULL if
245 * the line is blank.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000246 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000247struct Line {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000248 Line *next;
249 MMacro *finishes;
250 Token *first;
251};
252
253/*
254 * To handle an arbitrary level of file inclusion, we maintain a
255 * stack (ie linked list) of these things.
256 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000257struct Include {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000258 Include *next;
259 FILE *fp;
260 Cond *conds;
261 Line *expansion;
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000262 char *fname;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000263 int lineno, lineinc;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300264 MMacro *mstk; /* stack of active macros/reps */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000265};
266
267/*
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000268 * Include search path. This is simply a list of strings which get
269 * prepended, in turn, to the name of an include file, in an
270 * attempt to find the file if it's not in the current directory.
271 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000272struct IncPath {
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000273 IncPath *next;
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000274 char *path;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000275};
276
277/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000278 * Conditional assembly: we maintain a separate stack of these for
279 * each level of file inclusion. (The only reason we keep the
280 * stacks separate is to ensure that a stray `%endif' in a file
281 * included from within the true branch of a `%if' won't terminate
282 * it and cause confusion: instead, rightly, it'll cause an error.)
283 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000284struct Cond {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000285 Cond *next;
286 int state;
287};
H. Peter Anvine2c80182005-01-15 22:15:51 +0000288enum {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000289 /*
290 * These states are for use just after %if or %elif: IF_TRUE
291 * means the condition has evaluated to truth so we are
292 * currently emitting, whereas IF_FALSE means we are not
293 * currently emitting but will start doing so if a %else comes
294 * up. In these states, all directives are admissible: %elif,
295 * %else and %endif. (And of course %if.)
296 */
297 COND_IF_TRUE, COND_IF_FALSE,
298 /*
299 * These states come up after a %else: ELSE_TRUE means we're
300 * emitting, and ELSE_FALSE means we're not. In ELSE_* states,
301 * any %elif or %else will cause an error.
302 */
303 COND_ELSE_TRUE, COND_ELSE_FALSE,
304 /*
Victor van den Elzen3b404c02008-09-18 13:51:36 +0200305 * These states mean that we're not emitting now, and also that
306 * nothing until %endif will be emitted at all. COND_DONE is
307 * used when we've had our moment of emission
308 * and have now started seeing %elifs. COND_NEVER is used when
309 * the condition construct in question is contained within a
310 * non-emitting branch of a larger condition construct,
311 * or if there is an error.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000312 */
Victor van den Elzen3b404c02008-09-18 13:51:36 +0200313 COND_DONE, COND_NEVER
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000314};
315#define emitting(x) ( (x) == COND_IF_TRUE || (x) == COND_ELSE_TRUE )
316
H. Peter Anvin70653092007-10-19 14:42:29 -0700317/*
Ed Beroset3ab3f412002-06-11 03:31:49 +0000318 * These defines are used as the possible return values for do_directive
319 */
320#define NO_DIRECTIVE_FOUND 0
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300321#define DIRECTIVE_FOUND 1
Ed Beroset3ab3f412002-06-11 03:31:49 +0000322
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000323/*
Keith Kanios852f1ee2009-07-12 00:19:55 -0500324 * This define sets the upper limit for smacro and recursive mmacro
325 * expansions
326 */
327#define DEADMAN_LIMIT (1 << 20)
328
329/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000330 * Condition codes. Note that we use c_ prefix not C_ because C_ is
331 * used in nasm.h for the "real" condition codes. At _this_ level,
332 * we treat CXZ and ECXZ as condition codes, albeit non-invertible
333 * ones, so we need a different enum...
334 */
H. Peter Anvin476d2862007-10-02 22:04:15 -0700335static const char * const conditions[] = {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000336 "a", "ae", "b", "be", "c", "cxz", "e", "ecxz", "g", "ge", "l", "le",
337 "na", "nae", "nb", "nbe", "nc", "ne", "ng", "nge", "nl", "nle", "no",
H. Peter Anvince9be342007-09-12 00:22:29 +0000338 "np", "ns", "nz", "o", "p", "pe", "po", "rcxz", "s", "z"
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000339};
H. Peter Anvin476d2862007-10-02 22:04:15 -0700340enum pp_conds {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000341 c_A, c_AE, c_B, c_BE, c_C, c_CXZ, c_E, c_ECXZ, c_G, c_GE, c_L, c_LE,
342 c_NA, c_NAE, c_NB, c_NBE, c_NC, c_NE, c_NG, c_NGE, c_NL, c_NLE, c_NO,
H. Peter Anvin476d2862007-10-02 22:04:15 -0700343 c_NP, c_NS, c_NZ, c_O, c_P, c_PE, c_PO, c_RCXZ, c_S, c_Z,
344 c_none = -1
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000345};
H. Peter Anvin476d2862007-10-02 22:04:15 -0700346static const enum pp_conds inverse_ccs[] = {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000347 c_NA, c_NAE, c_NB, c_NBE, c_NC, -1, c_NE, -1, c_NG, c_NGE, c_NL, c_NLE,
348 c_A, c_AE, c_B, c_BE, c_C, c_E, c_G, c_GE, c_L, c_LE, c_O, c_P, c_S,
H. Peter Anvince9be342007-09-12 00:22:29 +0000349 c_Z, c_NO, c_NP, c_PO, c_PE, -1, c_NS, c_NZ
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000350};
351
H. Peter Anvin76690a12002-04-30 20:52:49 +0000352/*
353 * Directive names.
354 */
H. Peter Anvin65747262002-05-07 00:10:05 +0000355/* If this is a an IF, ELIF, ELSE or ENDIF keyword */
H. Peter Anvin9bf0aa72007-09-12 02:12:07 +0000356static int is_condition(enum preproc_token arg)
H. Peter Anvin65747262002-05-07 00:10:05 +0000357{
H. Peter Anvinda10e7b2007-09-12 04:18:37 +0000358 return PP_IS_COND(arg) || (arg == PP_ELSE) || (arg == PP_ENDIF);
H. Peter Anvin65747262002-05-07 00:10:05 +0000359}
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000360
361/* For TASM compatibility we need to be able to recognise TASM compatible
362 * conditional compilation directives. Using the NASM pre-processor does
363 * not work, so we look for them specifically from the following list and
364 * then jam in the equivalent NASM directive into the input stream.
365 */
366
H. Peter Anvine2c80182005-01-15 22:15:51 +0000367enum {
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000368 TM_ARG, TM_ELIF, TM_ELSE, TM_ENDIF, TM_IF, TM_IFDEF, TM_IFDIFI,
369 TM_IFNDEF, TM_INCLUDE, TM_LOCAL
370};
371
H. Peter Anvin476d2862007-10-02 22:04:15 -0700372static const char * const tasm_directives[] = {
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000373 "arg", "elif", "else", "endif", "if", "ifdef", "ifdifi",
374 "ifndef", "include", "local"
375};
376
377static int StackSize = 4;
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000378static char *StackPointer = "ebp";
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000379static int ArgOffset = 8;
H. Peter Anvin8781cb02007-11-08 20:01:11 -0800380static int LocalOffset = 0;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000381
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000382static Context *cstk;
383static Include *istk;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000384static IncPath *ipath = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000385
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300386static int pass; /* HACK: pass 0 = generate dependencies only */
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700387static StrList **dephead, **deptail; /* Dependency list */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000388
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300389static uint64_t unique; /* unique identifier numbers */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000390
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000391static Line *predef = NULL;
H. Peter Anvind2456592008-06-19 15:04:18 -0700392static bool do_predef;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000393
394static ListGen *list;
395
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000396/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000397 * The current set of multi-line macros we have defined.
398 */
H. Peter Anvin166c2472008-05-28 12:28:58 -0700399static struct hash_table mmacros;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000400
401/*
402 * The current set of single-line macros we have defined.
403 */
H. Peter Anvin166c2472008-05-28 12:28:58 -0700404static struct hash_table smacros;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000405
406/*
H. Peter Anvin76690a12002-04-30 20:52:49 +0000407 * The multi-line macro we are currently defining, or the %rep
408 * block we are currently reading, if any.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000409 */
410static MMacro *defining;
411
Charles Crayned4200be2008-07-12 16:42:33 -0700412static uint64_t nested_mac_count;
413static uint64_t nested_rep_count;
414
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000415/*
416 * The number of macro parameters to allocate space for at a time.
417 */
418#define PARAM_DELTA 16
419
420/*
H. Peter Anvina4835d42008-05-20 14:21:29 -0700421 * The standard macro set: defined in macros.c in the array nasm_stdmac.
422 * This gives our position in the macro set, when we're processing it.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000423 */
H. Peter Anvina70547f2008-07-19 21:44:26 -0700424static macros_t *stdmacpos;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000425
426/*
H. Peter Anvin76690a12002-04-30 20:52:49 +0000427 * The extra standard macros that come from the object format, if
428 * any.
429 */
H. Peter Anvina70547f2008-07-19 21:44:26 -0700430static macros_t *extrastdmac = NULL;
H. Peter Anvincfb71762008-06-20 15:20:16 -0700431static bool any_extrastdmac;
H. Peter Anvin76690a12002-04-30 20:52:49 +0000432
433/*
H. Peter Anvin734b1882002-04-30 21:01:08 +0000434 * Tokens are allocated in blocks to improve speed
435 */
436#define TOKEN_BLOCKSIZE 4096
437static Token *freeTokens = NULL;
H. Peter Anvince616072002-04-30 21:02:23 +0000438struct Blocks {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000439 Blocks *next;
440 void *chunk;
H. Peter Anvince616072002-04-30 21:02:23 +0000441};
442
443static Blocks blocks = { NULL, NULL };
H. Peter Anvin734b1882002-04-30 21:01:08 +0000444
445/*
H. Peter Anvin76690a12002-04-30 20:52:49 +0000446 * Forward declarations.
447 */
H. Peter Anvin734b1882002-04-30 21:01:08 +0000448static Token *expand_mmac_params(Token * tline);
449static Token *expand_smacro(Token * tline);
450static Token *expand_id(Token * tline);
H. Peter Anvinf8ad5322009-02-21 17:55:08 -0800451static Context *get_ctx(const char *name, const char **namep,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300452 bool all_contexts);
Keith Kaniosa5fc6462007-10-13 07:09:22 -0700453static void make_tok_num(Token * tok, int64_t val);
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000454static void error(int severity, const char *fmt, ...);
Victor van den Elzen3b404c02008-09-18 13:51:36 +0200455static void error_precond(int severity, const char *fmt, ...);
H. Peter Anvince616072002-04-30 21:02:23 +0000456static void *new_Block(size_t size);
457static void delete_Blocks(void);
H. Peter Anvinc751e862008-06-09 10:18:45 -0700458static Token *new_Token(Token * next, enum pp_token_type type,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300459 const char *text, int txtlen);
H. Peter Anvin734b1882002-04-30 21:01:08 +0000460static Token *delete_Token(Token * t);
H. Peter Anvineba20a72002-04-30 20:53:55 +0000461
462/*
463 * Macros for safe checking of token pointers, avoid *(NULL)
464 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300465#define tok_type_(x,t) ((x) && (x)->type == (t))
466#define skip_white_(x) if (tok_type_((x), TOK_WHITESPACE)) (x)=(x)->next
467#define tok_is_(x,v) (tok_type_((x), TOK_OTHER) && !strcmp((x)->text,(v)))
468#define tok_isnt_(x,v) ((x) && ((x)->type!=TOK_OTHER || strcmp((x)->text,(v))))
H. Peter Anvin76690a12002-04-30 20:52:49 +0000469
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300470/*
471 * Handle TASM specific directives, which do not contain a % in
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000472 * front of them. We do it here because I could not find any other
473 * place to do it for the moment, and it is a hack (ideally it would
474 * be nice to be able to use the NASM pre-processor to do it).
475 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000476static char *check_tasm_directive(char *line)
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000477{
Keith Kaniosb7a89542007-04-12 02:40:54 +0000478 int32_t i, j, k, m, len;
Cyrill Gorcunovf66ac7d2009-10-12 20:41:13 +0400479 char *p, *q, *oldline, oldchar;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000480
Cyrill Gorcunovf66ac7d2009-10-12 20:41:13 +0400481 p = nasm_skip_spaces(line);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000482
483 /* Binary search for the directive name */
484 i = -1;
Ed Beroset3ab3f412002-06-11 03:31:49 +0000485 j = elements(tasm_directives);
Cyrill Gorcunovf66ac7d2009-10-12 20:41:13 +0400486 q = nasm_skip_word(p);
487 len = q - p;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000488 if (len) {
489 oldchar = p[len];
490 p[len] = 0;
491 while (j - i > 1) {
492 k = (j + i) / 2;
493 m = nasm_stricmp(p, tasm_directives[k]);
494 if (m == 0) {
495 /* We have found a directive, so jam a % in front of it
496 * so that NASM will then recognise it as one if it's own.
497 */
498 p[len] = oldchar;
499 len = strlen(p);
500 oldline = line;
501 line = nasm_malloc(len + 2);
502 line[0] = '%';
503 if (k == TM_IFDIFI) {
H. Peter Anvin18f48792009-06-27 15:56:27 -0700504 /*
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300505 * NASM does not recognise IFDIFI, so we convert
506 * it to %if 0. This is not used in NASM
507 * compatible code, but does need to parse for the
508 * TASM macro package.
H. Peter Anvine2c80182005-01-15 22:15:51 +0000509 */
H. Peter Anvin18f48792009-06-27 15:56:27 -0700510 strcpy(line + 1, "if 0");
H. Peter Anvine2c80182005-01-15 22:15:51 +0000511 } else {
512 memcpy(line + 1, p, len + 1);
513 }
514 nasm_free(oldline);
515 return line;
516 } else if (m < 0) {
517 j = k;
518 } else
519 i = k;
520 }
521 p[len] = oldchar;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000522 }
523 return line;
524}
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000525
H. Peter Anvin76690a12002-04-30 20:52:49 +0000526/*
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000527 * The pre-preprocessing stage... This function translates line
528 * number indications as they emerge from GNU cpp (`# lineno "file"
529 * flags') into NASM preprocessor line number indications (`%line
530 * lineno file').
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000531 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000532static char *prepreproc(char *line)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000533{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000534 int lineno, fnlen;
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000535 char *fname, *oldline;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000536
H. Peter Anvine2c80182005-01-15 22:15:51 +0000537 if (line[0] == '#' && line[1] == ' ') {
538 oldline = line;
539 fname = oldline + 2;
540 lineno = atoi(fname);
541 fname += strspn(fname, "0123456789 ");
542 if (*fname == '"')
543 fname++;
544 fnlen = strcspn(fname, "\"");
545 line = nasm_malloc(20 + fnlen);
546 snprintf(line, 20 + fnlen, "%%line %d %.*s", lineno, fnlen, fname);
547 nasm_free(oldline);
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000548 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000549 if (tasm_compatible_mode)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000550 return check_tasm_directive(line);
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000551 return line;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000552}
553
554/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000555 * Free a linked list of tokens.
556 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000557static void free_tlist(Token * list)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000558{
H. Peter Anvine2c80182005-01-15 22:15:51 +0000559 while (list) {
560 list = delete_Token(list);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000561 }
562}
563
564/*
565 * Free a linked list of lines.
566 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000567static void free_llist(Line * list)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000568{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000569 Line *l;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000570 while (list) {
571 l = list;
572 list = list->next;
573 free_tlist(l->first);
574 nasm_free(l);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000575 }
576}
577
578/*
H. Peter Anvineba20a72002-04-30 20:53:55 +0000579 * Free an MMacro
580 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000581static void free_mmacro(MMacro * m)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000582{
H. Peter Anvin734b1882002-04-30 21:01:08 +0000583 nasm_free(m->name);
584 free_tlist(m->dlist);
585 nasm_free(m->defaults);
586 free_llist(m->expansion);
587 nasm_free(m);
H. Peter Anvineba20a72002-04-30 20:53:55 +0000588}
589
590/*
H. Peter Anvin97a23472007-09-16 17:57:25 -0700591 * Free all currently defined macros, and free the hash tables
592 */
H. Peter Anvin072771e2008-05-22 13:17:51 -0700593static void free_smacro_table(struct hash_table *smt)
H. Peter Anvin97a23472007-09-16 17:57:25 -0700594{
H. Peter Anvin97a23472007-09-16 17:57:25 -0700595 SMacro *s;
H. Peter Anvin072771e2008-05-22 13:17:51 -0700596 const char *key;
597 struct hash_tbl_node *it = NULL;
H. Peter Anvin97a23472007-09-16 17:57:25 -0700598
H. Peter Anvin072771e2008-05-22 13:17:51 -0700599 while ((s = hash_iterate(smt, &it, &key)) != NULL) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300600 nasm_free((void *)key);
601 while (s) {
602 SMacro *ns = s->next;
603 nasm_free(s->name);
604 free_tlist(s->expansion);
605 nasm_free(s);
606 s = ns;
607 }
H. Peter Anvin97a23472007-09-16 17:57:25 -0700608 }
H. Peter Anvin072771e2008-05-22 13:17:51 -0700609 hash_free(smt);
610}
611
612static void free_mmacro_table(struct hash_table *mmt)
613{
614 MMacro *m;
615 const char *key;
616 struct hash_tbl_node *it = NULL;
H. Peter Anvin97a23472007-09-16 17:57:25 -0700617
618 it = NULL;
H. Peter Anvin072771e2008-05-22 13:17:51 -0700619 while ((m = hash_iterate(mmt, &it, &key)) != NULL) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300620 nasm_free((void *)key);
621 while (m) {
622 MMacro *nm = m->next;
623 free_mmacro(m);
624 m = nm;
625 }
H. Peter Anvin97a23472007-09-16 17:57:25 -0700626 }
H. Peter Anvin072771e2008-05-22 13:17:51 -0700627 hash_free(mmt);
628}
629
630static void free_macros(void)
631{
H. Peter Anvin166c2472008-05-28 12:28:58 -0700632 free_smacro_table(&smacros);
633 free_mmacro_table(&mmacros);
H. Peter Anvin97a23472007-09-16 17:57:25 -0700634}
635
636/*
637 * Initialize the hash tables
638 */
639static void init_macros(void)
640{
H. Peter Anvin166c2472008-05-28 12:28:58 -0700641 hash_init(&smacros, HASH_LARGE);
642 hash_init(&mmacros, HASH_LARGE);
H. Peter Anvin97a23472007-09-16 17:57:25 -0700643}
644
645/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000646 * Pop the context stack.
647 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000648static void ctx_pop(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000649{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000650 Context *c = cstk;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000651
652 cstk = cstk->next;
H. Peter Anvin166c2472008-05-28 12:28:58 -0700653 free_smacro_table(&c->localmac);
H. Peter Anvin734b1882002-04-30 21:01:08 +0000654 nasm_free(c->name);
655 nasm_free(c);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000656}
657
H. Peter Anvin072771e2008-05-22 13:17:51 -0700658/*
659 * Search for a key in the hash index; adding it if necessary
660 * (in which case we initialize the data pointer to NULL.)
661 */
662static void **
663hash_findi_add(struct hash_table *hash, const char *str)
664{
665 struct hash_insert hi;
666 void **r;
667 char *strx;
668
669 r = hash_findi(hash, str, &hi);
670 if (r)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300671 return r;
H. Peter Anvin072771e2008-05-22 13:17:51 -0700672
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300673 strx = nasm_strdup(str); /* Use a more efficient allocator here? */
H. Peter Anvin072771e2008-05-22 13:17:51 -0700674 return hash_add(&hi, strx, NULL);
675}
676
677/*
678 * Like hash_findi, but returns the data element rather than a pointer
679 * to it. Used only when not adding a new element, hence no third
680 * argument.
681 */
682static void *
683hash_findix(struct hash_table *hash, const char *str)
684{
685 void **p;
686
687 p = hash_findi(hash, str, NULL);
688 return p ? *p : NULL;
689}
690
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000691#define BUF_DELTA 512
692/*
693 * Read a line from the top file in istk, handling multiple CR/LFs
694 * at the end of the line read, and handling spurious ^Zs. Will
695 * return lines from the standard macro set if this has not already
696 * been done.
697 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000698static char *read_line(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000699{
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000700 char *buffer, *p, *q;
H. Peter Anvin9f394642002-04-30 21:07:51 +0000701 int bufsize, continued_count;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000702
H. Peter Anvine2c80182005-01-15 22:15:51 +0000703 if (stdmacpos) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300704 unsigned char c;
705 const unsigned char *p = stdmacpos;
706 char *ret, *q;
707 size_t len = 0;
708 while ((c = *p++)) {
709 if (c >= 0x80)
710 len += pp_directives_len[c-0x80]+1;
711 else
712 len++;
713 }
714 ret = nasm_malloc(len+1);
715 q = ret;
716 while ((c = *stdmacpos++)) {
717 if (c >= 0x80) {
718 memcpy(q, pp_directives[c-0x80], pp_directives_len[c-0x80]);
719 q += pp_directives_len[c-0x80];
720 *q++ = ' ';
721 } else {
722 *q++ = c;
723 }
724 }
725 stdmacpos = p;
726 *q = '\0';
H. Peter Anvin72edbb82008-06-19 16:00:04 -0700727
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300728 if (!*stdmacpos) {
729 /* This was the last of the standard macro chain... */
730 stdmacpos = NULL;
731 if (any_extrastdmac) {
732 stdmacpos = extrastdmac;
733 any_extrastdmac = false;
734 } else if (do_predef) {
735 Line *pd, *l;
736 Token *head, **tail, *t;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000737
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300738 /*
739 * Nasty hack: here we push the contents of
740 * `predef' on to the top-level expansion stack,
741 * since this is the most convenient way to
742 * implement the pre-include and pre-define
743 * features.
744 */
745 for (pd = predef; pd; pd = pd->next) {
746 head = NULL;
747 tail = &head;
748 for (t = pd->first; t; t = t->next) {
749 *tail = new_Token(NULL, t->type, t->text, 0);
750 tail = &(*tail)->next;
751 }
752 l = nasm_malloc(sizeof(Line));
753 l->next = istk->expansion;
754 l->first = head;
755 l->finishes = NULL;
756 istk->expansion = l;
757 }
758 do_predef = false;
759 }
760 }
761 return ret;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000762 }
763
764 bufsize = BUF_DELTA;
765 buffer = nasm_malloc(BUF_DELTA);
766 p = buffer;
H. Peter Anvin9f394642002-04-30 21:07:51 +0000767 continued_count = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000768 while (1) {
769 q = fgets(p, bufsize - (p - buffer), istk->fp);
770 if (!q)
771 break;
772 p += strlen(p);
773 if (p > buffer && p[-1] == '\n') {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300774 /*
775 * Convert backslash-CRLF line continuation sequences into
776 * nothing at all (for DOS and Windows)
777 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000778 if (((p - 2) > buffer) && (p[-3] == '\\') && (p[-2] == '\r')) {
779 p -= 3;
780 *p = 0;
781 continued_count++;
782 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300783 /*
784 * Also convert backslash-LF line continuation sequences into
785 * nothing at all (for Unix)
786 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000787 else if (((p - 1) > buffer) && (p[-2] == '\\')) {
788 p -= 2;
789 *p = 0;
790 continued_count++;
791 } else {
792 break;
793 }
794 }
795 if (p - buffer > bufsize - 10) {
Keith Kaniosb7a89542007-04-12 02:40:54 +0000796 int32_t offset = p - buffer;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000797 bufsize += BUF_DELTA;
798 buffer = nasm_realloc(buffer, bufsize);
799 p = buffer + offset; /* prevent stale-pointer problems */
800 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000801 }
802
H. Peter Anvine2c80182005-01-15 22:15:51 +0000803 if (!q && p == buffer) {
804 nasm_free(buffer);
805 return NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000806 }
807
H. Peter Anvine2c80182005-01-15 22:15:51 +0000808 src_set_linnum(src_get_linnum() + istk->lineinc +
809 (continued_count * istk->lineinc));
H. Peter Anvineba20a72002-04-30 20:53:55 +0000810
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000811 /*
812 * Play safe: remove CRs as well as LFs, if any of either are
813 * present at the end of the line.
814 */
H. Peter Anvineba20a72002-04-30 20:53:55 +0000815 while (--p >= buffer && (*p == '\n' || *p == '\r'))
H. Peter Anvine2c80182005-01-15 22:15:51 +0000816 *p = '\0';
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000817
818 /*
819 * Handle spurious ^Z, which may be inserted into source files
820 * by some file transfer utilities.
821 */
822 buffer[strcspn(buffer, "\032")] = '\0';
823
H. Peter Anvin734b1882002-04-30 21:01:08 +0000824 list->line(LIST_READ, buffer);
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000825
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000826 return buffer;
827}
828
829/*
Keith Kaniosb7a89542007-04-12 02:40:54 +0000830 * Tokenize a line of text. This is a very simple process since we
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000831 * don't need to parse the value out of e.g. numeric tokens: we
832 * simply split one string into many.
833 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000834static Token *tokenize(char *line)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000835{
H. Peter Anvinca544db2008-10-19 19:30:11 -0700836 char c, *p = line;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +0000837 enum pp_token_type type;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000838 Token *list = NULL;
839 Token *t, **tail = &list;
840
H. Peter Anvine2c80182005-01-15 22:15:51 +0000841 while (*line) {
842 p = line;
843 if (*p == '%') {
844 p++;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300845 if (*p == '+' && !nasm_isdigit(p[1])) {
846 p++;
847 type = TOK_PASTE;
848 } else if (nasm_isdigit(*p) ||
849 ((*p == '-' || *p == '+') && nasm_isdigit(p[1]))) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000850 do {
851 p++;
852 }
H. Peter Anvinbda7a6e2008-06-21 10:23:17 -0700853 while (nasm_isdigit(*p));
H. Peter Anvine2c80182005-01-15 22:15:51 +0000854 type = TOK_PREPROC_ID;
855 } else if (*p == '{') {
856 p++;
857 while (*p && *p != '}') {
858 p[-1] = *p;
859 p++;
860 }
861 p[-1] = '\0';
862 if (*p)
863 p++;
864 type = TOK_PREPROC_ID;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300865 } else if (*p == '[') {
866 int lvl = 1;
867 line += 2; /* Skip the leading %[ */
868 p++;
869 while (lvl && (c = *p++)) {
870 switch (c) {
871 case ']':
872 lvl--;
873 break;
874 case '%':
875 if (*p == '[')
876 lvl++;
877 break;
878 case '\'':
879 case '\"':
880 case '`':
881 p = nasm_skip_string(p)+1;
882 break;
883 default:
884 break;
885 }
886 }
887 p--;
888 if (*p)
889 *p++ = '\0';
890 if (lvl)
891 error(ERR_NONFATAL, "unterminated %[ construct");
892 type = TOK_INDIRECT;
893 } else if (*p == '?') {
894 type = TOK_PREPROC_Q; /* %? */
895 p++;
896 if (*p == '?') {
897 type = TOK_PREPROC_QQ; /* %?? */
898 p++;
899 }
H. Peter Anvine2c80182005-01-15 22:15:51 +0000900 } else if (isidchar(*p) ||
901 ((*p == '!' || *p == '%' || *p == '$') &&
902 isidchar(p[1]))) {
903 do {
904 p++;
905 }
906 while (isidchar(*p));
907 type = TOK_PREPROC_ID;
908 } else {
909 type = TOK_OTHER;
910 if (*p == '%')
911 p++;
912 }
913 } else if (isidstart(*p) || (*p == '$' && isidstart(p[1]))) {
914 type = TOK_ID;
915 p++;
916 while (*p && isidchar(*p))
917 p++;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -0700918 } else if (*p == '\'' || *p == '"' || *p == '`') {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000919 /*
920 * A string token.
921 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000922 type = TOK_STRING;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300923 p = nasm_skip_string(p);
Nickolay Yurchenkof3b3ce22003-09-21 20:38:43 +0000924
H. Peter Anvine2c80182005-01-15 22:15:51 +0000925 if (*p) {
926 p++;
927 } else {
H. Peter Anvin917a3492008-09-24 09:14:49 -0700928 error(ERR_WARNING|ERR_PASS1, "unterminated string");
H. Peter Anvine2c80182005-01-15 22:15:51 +0000929 /* Handling unterminated strings by UNV */
930 /* type = -1; */
931 }
Victor van den Elzenfb5f2512009-04-17 16:17:59 +0200932 } else if (p[0] == '$' && p[1] == '$') {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300933 type = TOK_OTHER; /* TOKEN_BASE */
Victor van den Elzenfb5f2512009-04-17 16:17:59 +0200934 p += 2;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000935 } else if (isnumstart(*p)) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300936 bool is_hex = false;
937 bool is_float = false;
938 bool has_e = false;
939 char c, *r;
H. Peter Anvinc2df2822007-10-24 15:29:28 -0700940
H. Peter Anvine2c80182005-01-15 22:15:51 +0000941 /*
H. Peter Anvinc2df2822007-10-24 15:29:28 -0700942 * A numeric token.
H. Peter Anvine2c80182005-01-15 22:15:51 +0000943 */
H. Peter Anvinc2df2822007-10-24 15:29:28 -0700944
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300945 if (*p == '$') {
946 p++;
947 is_hex = true;
948 }
H. Peter Anvinc2df2822007-10-24 15:29:28 -0700949
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300950 for (;;) {
951 c = *p++;
H. Peter Anvinc2df2822007-10-24 15:29:28 -0700952
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300953 if (!is_hex && (c == 'e' || c == 'E')) {
954 has_e = true;
955 if (*p == '+' || *p == '-') {
956 /*
957 * e can only be followed by +/- if it is either a
958 * prefixed hex number or a floating-point number
959 */
960 p++;
961 is_float = true;
962 }
963 } else if (c == 'H' || c == 'h' || c == 'X' || c == 'x') {
964 is_hex = true;
965 } else if (c == 'P' || c == 'p') {
966 is_float = true;
967 if (*p == '+' || *p == '-')
968 p++;
969 } else if (isnumchar(c) || c == '_')
970 ; /* just advance */
971 else if (c == '.') {
972 /*
973 * we need to deal with consequences of the legacy
974 * parser, like "1.nolist" being two tokens
975 * (TOK_NUMBER, TOK_ID) here; at least give it
976 * a shot for now. In the future, we probably need
977 * a flex-based scanner with proper pattern matching
978 * to do it as well as it can be done. Nothing in
979 * the world is going to help the person who wants
980 * 0x123.p16 interpreted as two tokens, though.
981 */
982 r = p;
983 while (*r == '_')
984 r++;
H. Peter Anvinc2df2822007-10-24 15:29:28 -0700985
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300986 if (nasm_isdigit(*r) || (is_hex && nasm_isxdigit(*r)) ||
987 (!is_hex && (*r == 'e' || *r == 'E')) ||
988 (*r == 'p' || *r == 'P')) {
989 p = r;
990 is_float = true;
991 } else
992 break; /* Terminate the token */
993 } else
994 break;
995 }
996 p--; /* Point to first character beyond number */
H. Peter Anvinc2df2822007-10-24 15:29:28 -0700997
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300998 if (p == line+1 && *line == '$') {
999 type = TOK_OTHER; /* TOKEN_HERE */
1000 } else {
1001 if (has_e && !is_hex) {
1002 /* 1e13 is floating-point, but 1e13h is not */
1003 is_float = true;
1004 }
H. Peter Anvind784a082009-04-20 14:01:18 -07001005
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001006 type = is_float ? TOK_FLOAT : TOK_NUMBER;
1007 }
H. Peter Anvinbda7a6e2008-06-21 10:23:17 -07001008 } else if (nasm_isspace(*p)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001009 type = TOK_WHITESPACE;
Cyrill Gorcunovf66ac7d2009-10-12 20:41:13 +04001010 p = nasm_skip_spaces(p);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001011 /*
1012 * Whitespace just before end-of-line is discarded by
1013 * pretending it's a comment; whitespace just before a
1014 * comment gets lumped into the comment.
1015 */
1016 if (!*p || *p == ';') {
1017 type = TOK_COMMENT;
1018 while (*p)
1019 p++;
1020 }
1021 } else if (*p == ';') {
1022 type = TOK_COMMENT;
1023 while (*p)
1024 p++;
1025 } else {
1026 /*
1027 * Anything else is an operator of some kind. We check
1028 * for all the double-character operators (>>, <<, //,
1029 * %%, <=, >=, ==, !=, <>, &&, ||, ^^), but anything
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001030 * else is a single-character operator.
H. Peter Anvine2c80182005-01-15 22:15:51 +00001031 */
1032 type = TOK_OTHER;
1033 if ((p[0] == '>' && p[1] == '>') ||
1034 (p[0] == '<' && p[1] == '<') ||
1035 (p[0] == '/' && p[1] == '/') ||
1036 (p[0] == '<' && p[1] == '=') ||
1037 (p[0] == '>' && p[1] == '=') ||
1038 (p[0] == '=' && p[1] == '=') ||
1039 (p[0] == '!' && p[1] == '=') ||
1040 (p[0] == '<' && p[1] == '>') ||
1041 (p[0] == '&' && p[1] == '&') ||
1042 (p[0] == '|' && p[1] == '|') ||
1043 (p[0] == '^' && p[1] == '^')) {
1044 p++;
1045 }
1046 p++;
1047 }
Nickolay Yurchenkof3b3ce22003-09-21 20:38:43 +00001048
H. Peter Anvine2c80182005-01-15 22:15:51 +00001049 /* Handling unterminated string by UNV */
1050 /*if (type == -1)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001051 {
1052 *tail = t = new_Token(NULL, TOK_STRING, line, p-line+1);
1053 t->text[p-line] = *line;
1054 tail = &t->next;
1055 }
1056 else */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001057 if (type != TOK_COMMENT) {
1058 *tail = t = new_Token(NULL, type, line, p - line);
1059 tail = &t->next;
1060 }
1061 line = p;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001062 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001063 return list;
1064}
1065
H. Peter Anvince616072002-04-30 21:02:23 +00001066/*
1067 * this function allocates a new managed block of memory and
H. Peter Anvin70653092007-10-19 14:42:29 -07001068 * returns a pointer to the block. The managed blocks are
H. Peter Anvince616072002-04-30 21:02:23 +00001069 * deleted only all at once by the delete_Blocks function.
1070 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001071static void *new_Block(size_t size)
H. Peter Anvince616072002-04-30 21:02:23 +00001072{
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001073 Blocks *b = &blocks;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001074
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001075 /* first, get to the end of the linked list */
1076 while (b->next)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001077 b = b->next;
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001078 /* now allocate the requested chunk */
1079 b->chunk = nasm_malloc(size);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001080
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001081 /* now allocate a new block for the next request */
1082 b->next = nasm_malloc(sizeof(Blocks));
1083 /* and initialize the contents of the new block */
1084 b->next->next = NULL;
1085 b->next->chunk = NULL;
1086 return b->chunk;
H. Peter Anvince616072002-04-30 21:02:23 +00001087}
1088
1089/*
1090 * this function deletes all managed blocks of memory
1091 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001092static void delete_Blocks(void)
H. Peter Anvince616072002-04-30 21:02:23 +00001093{
H. Peter Anvine2c80182005-01-15 22:15:51 +00001094 Blocks *a, *b = &blocks;
H. Peter Anvince616072002-04-30 21:02:23 +00001095
H. Peter Anvin70653092007-10-19 14:42:29 -07001096 /*
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001097 * keep in mind that the first block, pointed to by blocks
H. Peter Anvin70653092007-10-19 14:42:29 -07001098 * is a static and not dynamically allocated, so we don't
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001099 * free it.
1100 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001101 while (b) {
1102 if (b->chunk)
1103 nasm_free(b->chunk);
1104 a = b;
1105 b = b->next;
1106 if (a != &blocks)
1107 nasm_free(a);
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001108 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001109}
H. Peter Anvin734b1882002-04-30 21:01:08 +00001110
1111/*
H. Peter Anvin70653092007-10-19 14:42:29 -07001112 * this function creates a new Token and passes a pointer to it
H. Peter Anvin734b1882002-04-30 21:01:08 +00001113 * back to the caller. It sets the type and text elements, and
H. Peter Anvinf26e0972008-07-01 21:26:27 -07001114 * also the a.mac and next elements to NULL.
H. Peter Anvin734b1882002-04-30 21:01:08 +00001115 */
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07001116static Token *new_Token(Token * next, enum pp_token_type type,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001117 const char *text, int txtlen)
H. Peter Anvin734b1882002-04-30 21:01:08 +00001118{
1119 Token *t;
1120 int i;
1121
H. Peter Anvin89cee572009-07-15 09:16:54 -04001122 if (!freeTokens) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001123 freeTokens = (Token *) new_Block(TOKEN_BLOCKSIZE * sizeof(Token));
1124 for (i = 0; i < TOKEN_BLOCKSIZE - 1; i++)
1125 freeTokens[i].next = &freeTokens[i + 1];
1126 freeTokens[i].next = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001127 }
1128 t = freeTokens;
1129 freeTokens = t->next;
1130 t->next = next;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07001131 t->a.mac = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001132 t->type = type;
H. Peter Anvin89cee572009-07-15 09:16:54 -04001133 if (type == TOK_WHITESPACE || !text) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001134 t->text = NULL;
1135 } else {
1136 if (txtlen == 0)
1137 txtlen = strlen(text);
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07001138 t->text = nasm_malloc(txtlen+1);
1139 memcpy(t->text, text, txtlen);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001140 t->text[txtlen] = '\0';
H. Peter Anvin734b1882002-04-30 21:01:08 +00001141 }
1142 return t;
1143}
1144
H. Peter Anvine2c80182005-01-15 22:15:51 +00001145static Token *delete_Token(Token * t)
H. Peter Anvin734b1882002-04-30 21:01:08 +00001146{
1147 Token *next = t->next;
1148 nasm_free(t->text);
H. Peter Anvin788e6c12002-04-30 21:02:01 +00001149 t->next = freeTokens;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001150 freeTokens = t;
1151 return next;
1152}
1153
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001154/*
1155 * Convert a line of tokens back into text.
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001156 * If expand_locals is not zero, identifiers of the form "%$*xxx"
1157 * will be transformed into ..@ctxnum.xxx
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001158 */
H. Peter Anvin9e200162008-06-04 17:23:14 -07001159static char *detoken(Token * tlist, bool expand_locals)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001160{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001161 Token *t;
1162 int len;
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001163 char *line, *p;
H. Peter Anvinb4daadc2008-01-21 16:31:57 -08001164 const char *q;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001165
1166 len = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001167 for (t = tlist; t; t = t->next) {
1168 if (t->type == TOK_PREPROC_ID && t->text[1] == '!') {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001169 char *p = getenv(t->text + 2);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001170 nasm_free(t->text);
1171 if (p)
1172 t->text = nasm_strdup(p);
1173 else
1174 t->text = NULL;
1175 }
1176 /* Expand local macros here and not during preprocessing */
1177 if (expand_locals &&
1178 t->type == TOK_PREPROC_ID && t->text &&
1179 t->text[0] == '%' && t->text[1] == '$') {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001180 const char *q;
1181 char *p;
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001182 Context *ctx = get_ctx(t->text, &q, false);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001183 if (ctx) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001184 char buffer[40];
Keith Kanios93f2e9a2007-04-14 00:10:59 +00001185 snprintf(buffer, sizeof(buffer), "..@%"PRIu32".", ctx->number);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001186 p = nasm_strcat(buffer, q);
1187 nasm_free(t->text);
1188 t->text = p;
1189 }
1190 }
1191 if (t->type == TOK_WHITESPACE) {
1192 len++;
1193 } else if (t->text) {
1194 len += strlen(t->text);
1195 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001196 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00001197 p = line = nasm_malloc(len + 1);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001198 for (t = tlist; t; t = t->next) {
1199 if (t->type == TOK_WHITESPACE) {
H. Peter Anvinb4daadc2008-01-21 16:31:57 -08001200 *p++ = ' ';
H. Peter Anvine2c80182005-01-15 22:15:51 +00001201 } else if (t->text) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001202 q = t->text;
1203 while (*q)
1204 *p++ = *q++;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001205 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001206 }
1207 *p = '\0';
1208 return line;
1209}
1210
1211/*
H. Peter Anvin76690a12002-04-30 20:52:49 +00001212 * A scanner, suitable for use by the expression evaluator, which
1213 * operates on a line of Tokens. Expects a pointer to a pointer to
1214 * the first token in the line to be passed in as its private_data
1215 * field.
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001216 *
1217 * FIX: This really needs to be unified with stdscan.
H. Peter Anvin76690a12002-04-30 20:52:49 +00001218 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001219static int ppscan(void *private_data, struct tokenval *tokval)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001220{
H. Peter Anvin76690a12002-04-30 20:52:49 +00001221 Token **tlineptr = private_data;
1222 Token *tline;
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001223 char ourcopy[MAX_KEYWORD+1], *p, *r, *s;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001224
H. Peter Anvine2c80182005-01-15 22:15:51 +00001225 do {
1226 tline = *tlineptr;
1227 *tlineptr = tline ? tline->next : NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001228 }
1229 while (tline && (tline->type == TOK_WHITESPACE ||
H. Peter Anvine2c80182005-01-15 22:15:51 +00001230 tline->type == TOK_COMMENT));
H. Peter Anvin76690a12002-04-30 20:52:49 +00001231
1232 if (!tline)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001233 return tokval->t_type = TOKEN_EOS;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001234
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001235 tokval->t_charptr = tline->text;
1236
H. Peter Anvin76690a12002-04-30 20:52:49 +00001237 if (tline->text[0] == '$' && !tline->text[1])
H. Peter Anvine2c80182005-01-15 22:15:51 +00001238 return tokval->t_type = TOKEN_HERE;
H. Peter Anvin7cf897e2002-05-30 21:30:33 +00001239 if (tline->text[0] == '$' && tline->text[1] == '$' && !tline->text[2])
H. Peter Anvine2c80182005-01-15 22:15:51 +00001240 return tokval->t_type = TOKEN_BASE;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001241
H. Peter Anvine2c80182005-01-15 22:15:51 +00001242 if (tline->type == TOK_ID) {
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001243 p = tokval->t_charptr = tline->text;
1244 if (p[0] == '$') {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001245 tokval->t_charptr++;
1246 return tokval->t_type = TOKEN_ID;
1247 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00001248
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001249 for (r = p, s = ourcopy; *r; r++) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001250 if (r >= p+MAX_KEYWORD)
1251 return tokval->t_type = TOKEN_ID; /* Not a keyword */
H. Peter Anvinac8f8fc2008-06-11 15:49:41 -07001252 *s++ = nasm_tolower(*r);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001253 }
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001254 *s = '\0';
1255 /* right, so we have an identifier sitting in temp storage. now,
1256 * is it actually a register or instruction name, or what? */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001257 return nasm_token_hash(ourcopy, tokval);
H. Peter Anvin76690a12002-04-30 20:52:49 +00001258 }
1259
H. Peter Anvine2c80182005-01-15 22:15:51 +00001260 if (tline->type == TOK_NUMBER) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001261 bool rn_error;
1262 tokval->t_integer = readnum(tline->text, &rn_error);
1263 tokval->t_charptr = tline->text;
1264 if (rn_error)
1265 return tokval->t_type = TOKEN_ERRNUM;
1266 else
1267 return tokval->t_type = TOKEN_NUM;
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001268 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00001269
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001270 if (tline->type == TOK_FLOAT) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001271 return tokval->t_type = TOKEN_FLOAT;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001272 }
1273
H. Peter Anvine2c80182005-01-15 22:15:51 +00001274 if (tline->type == TOK_STRING) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001275 char bq, *ep;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001276
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001277 bq = tline->text[0];
H. Peter Anvin11627042008-06-09 20:45:19 -07001278 tokval->t_charptr = tline->text;
1279 tokval->t_inttwo = nasm_unquote(tline->text, &ep);
H. Peter Anvind2456592008-06-19 15:04:18 -07001280
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001281 if (ep[0] != bq || ep[1] != '\0')
1282 return tokval->t_type = TOKEN_ERRSTR;
1283 else
1284 return tokval->t_type = TOKEN_STR;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001285 }
1286
H. Peter Anvine2c80182005-01-15 22:15:51 +00001287 if (tline->type == TOK_OTHER) {
1288 if (!strcmp(tline->text, "<<"))
1289 return tokval->t_type = TOKEN_SHL;
1290 if (!strcmp(tline->text, ">>"))
1291 return tokval->t_type = TOKEN_SHR;
1292 if (!strcmp(tline->text, "//"))
1293 return tokval->t_type = TOKEN_SDIV;
1294 if (!strcmp(tline->text, "%%"))
1295 return tokval->t_type = TOKEN_SMOD;
1296 if (!strcmp(tline->text, "=="))
1297 return tokval->t_type = TOKEN_EQ;
1298 if (!strcmp(tline->text, "<>"))
1299 return tokval->t_type = TOKEN_NE;
1300 if (!strcmp(tline->text, "!="))
1301 return tokval->t_type = TOKEN_NE;
1302 if (!strcmp(tline->text, "<="))
1303 return tokval->t_type = TOKEN_LE;
1304 if (!strcmp(tline->text, ">="))
1305 return tokval->t_type = TOKEN_GE;
1306 if (!strcmp(tline->text, "&&"))
1307 return tokval->t_type = TOKEN_DBL_AND;
1308 if (!strcmp(tline->text, "^^"))
1309 return tokval->t_type = TOKEN_DBL_XOR;
1310 if (!strcmp(tline->text, "||"))
1311 return tokval->t_type = TOKEN_DBL_OR;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001312 }
1313
1314 /*
1315 * We have no other options: just return the first character of
1316 * the token text.
1317 */
1318 return tokval->t_type = tline->text[0];
1319}
1320
1321/*
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001322 * Compare a string to the name of an existing macro; this is a
1323 * simple wrapper which calls either strcmp or nasm_stricmp
1324 * depending on the value of the `casesense' parameter.
1325 */
H. Peter Anvin4db5a162007-10-11 13:42:09 -07001326static int mstrcmp(const char *p, const char *q, bool casesense)
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001327{
H. Peter Anvin734b1882002-04-30 21:01:08 +00001328 return casesense ? strcmp(p, q) : nasm_stricmp(p, q);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001329}
1330
1331/*
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07001332 * Compare a string to the name of an existing macro; this is a
1333 * simple wrapper which calls either strcmp or nasm_stricmp
1334 * depending on the value of the `casesense' parameter.
1335 */
1336static int mmemcmp(const char *p, const char *q, size_t l, bool casesense)
1337{
1338 return casesense ? memcmp(p, q, l) : nasm_memicmp(p, q, l);
1339}
1340
1341/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001342 * Return the Context structure associated with a %$ token. Return
1343 * NULL, having _already_ reported an error condition, if the
1344 * context stack isn't deep enough for the supplied number of $
1345 * signs.
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001346 * If all_contexts == true, contexts that enclose current are
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001347 * also scanned for such smacro, until it is found; if not -
1348 * only the context that directly results from the number of $'s
1349 * in variable's name.
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001350 *
1351 * If "namep" is non-NULL, set it to the pointer to the macro name
1352 * tail, i.e. the part beyond %$...
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001353 */
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001354static Context *get_ctx(const char *name, const char **namep,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001355 bool all_contexts)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001356{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001357 Context *ctx;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001358 SMacro *m;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001359 int i;
1360
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001361 if (namep)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001362 *namep = name;
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001363
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001364 if (!name || name[0] != '%' || name[1] != '$')
H. Peter Anvine2c80182005-01-15 22:15:51 +00001365 return NULL;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001366
H. Peter Anvine2c80182005-01-15 22:15:51 +00001367 if (!cstk) {
1368 error(ERR_NONFATAL, "`%s': context stack is empty", name);
1369 return NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001370 }
1371
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001372 name += 2;
1373 ctx = cstk;
1374 i = 0;
1375 while (ctx && *name == '$') {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001376 name++;
1377 i++;
1378 ctx = ctx->next;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001379 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001380 if (!ctx) {
1381 error(ERR_NONFATAL, "`%s': context stack is only"
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001382 " %d level%s deep", name, i, (i == 1 ? "" : "s"));
H. Peter Anvine2c80182005-01-15 22:15:51 +00001383 return NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001384 }
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001385
1386 if (namep)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001387 *namep = name;
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001388
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001389 if (!all_contexts)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001390 return ctx;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001391
H. Peter Anvine2c80182005-01-15 22:15:51 +00001392 do {
1393 /* Search for this smacro in found context */
H. Peter Anvin166c2472008-05-28 12:28:58 -07001394 m = hash_findix(&ctx->localmac, name);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001395 while (m) {
1396 if (!mstrcmp(m->name, name, m->casesense))
1397 return ctx;
1398 m = m->next;
1399 }
1400 ctx = ctx->next;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001401 }
1402 while (ctx);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001403 return NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001404}
1405
1406/*
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001407 * Check to see if a file is already in a string list
1408 */
1409static bool in_list(const StrList *list, const char *str)
1410{
1411 while (list) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001412 if (!strcmp(list->str, str))
1413 return true;
1414 list = list->next;
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001415 }
1416 return false;
1417}
1418
1419/*
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001420 * Open an include file. This routine must always return a valid
1421 * file pointer if it returns - it's responsible for throwing an
1422 * ERR_FATAL and bombing out completely if not. It should also try
1423 * the include path one by one until it finds the file or reaches
1424 * the end of the path.
1425 */
H. Peter Anvin2b1c3b92008-06-06 10:38:46 -07001426static FILE *inc_fopen(const char *file, StrList **dhead, StrList ***dtail,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001427 bool missing_ok)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001428{
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001429 FILE *fp;
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001430 char *prefix = "";
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001431 IncPath *ip = ipath;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001432 int len = strlen(file);
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001433 size_t prefix_len = 0;
1434 StrList *sl;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001435
H. Peter Anvine2c80182005-01-15 22:15:51 +00001436 while (1) {
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001437 sl = nasm_malloc(prefix_len+len+1+sizeof sl->next);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001438 memcpy(sl->str, prefix, prefix_len);
1439 memcpy(sl->str+prefix_len, file, len+1);
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001440 fp = fopen(sl->str, "r");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001441 if (fp && dhead && !in_list(*dhead, sl->str)) {
1442 sl->next = NULL;
1443 **dtail = sl;
1444 *dtail = &sl->next;
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001445 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001446 nasm_free(sl);
1447 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001448 if (fp)
1449 return fp;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001450 if (!ip) {
1451 if (!missing_ok)
1452 break;
1453 prefix = NULL;
1454 } else {
1455 prefix = ip->path;
1456 ip = ip->next;
1457 }
1458 if (prefix) {
1459 prefix_len = strlen(prefix);
1460 } else {
1461 /* -MG given and file not found */
1462 if (dhead && !in_list(*dhead, file)) {
1463 sl = nasm_malloc(len+1+sizeof sl->next);
1464 sl->next = NULL;
1465 strcpy(sl->str, file);
1466 **dtail = sl;
1467 *dtail = &sl->next;
1468 }
1469 return NULL;
1470 }
H. Peter Anvineba20a72002-04-30 20:53:55 +00001471 }
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001472
H. Peter Anvin734b1882002-04-30 21:01:08 +00001473 error(ERR_FATAL, "unable to open include file `%s'", file);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001474 return NULL;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001475}
1476
1477/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001478 * Determine if we should warn on defining a single-line macro of
H. Peter Anvinef7468f2002-04-30 20:57:59 +00001479 * name `name', with `nparam' parameters. If nparam is 0 or -1, will
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001480 * return true if _any_ single-line macro of that name is defined.
1481 * Otherwise, will return true if a single-line macro with either
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001482 * `nparam' or no parameters is defined.
1483 *
1484 * If a macro with precisely the right number of parameters is
H. Peter Anvinef7468f2002-04-30 20:57:59 +00001485 * defined, or nparam is -1, the address of the definition structure
1486 * will be returned in `defn'; otherwise NULL will be returned. If `defn'
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001487 * is NULL, no action will be taken regarding its contents, and no
1488 * error will occur.
1489 *
1490 * Note that this is also called with nparam zero to resolve
1491 * `ifdef'.
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001492 *
1493 * If you already know which context macro belongs to, you can pass
1494 * the context pointer as first parameter; if you won't but name begins
1495 * with %$ the context will be automatically computed. If all_contexts
1496 * is true, macro will be searched in outer contexts as well.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001497 */
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07001498static bool
H. Peter Anvinb2a5fda2008-06-19 21:42:42 -07001499smacro_defined(Context * ctx, const char *name, int nparam, SMacro ** defn,
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07001500 bool nocase)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001501{
H. Peter Anvin166c2472008-05-28 12:28:58 -07001502 struct hash_table *smtbl;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001503 SMacro *m;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001504
H. Peter Anvin97a23472007-09-16 17:57:25 -07001505 if (ctx) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001506 smtbl = &ctx->localmac;
H. Peter Anvin97a23472007-09-16 17:57:25 -07001507 } else if (name[0] == '%' && name[1] == '$') {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001508 if (cstk)
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001509 ctx = get_ctx(name, &name, false);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001510 if (!ctx)
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001511 return false; /* got to return _something_ */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001512 smtbl = &ctx->localmac;
H. Peter Anvin97a23472007-09-16 17:57:25 -07001513 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001514 smtbl = &smacros;
H. Peter Anvin97a23472007-09-16 17:57:25 -07001515 }
H. Peter Anvin166c2472008-05-28 12:28:58 -07001516 m = (SMacro *) hash_findix(smtbl, name);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001517
H. Peter Anvine2c80182005-01-15 22:15:51 +00001518 while (m) {
1519 if (!mstrcmp(m->name, name, m->casesense && nocase) &&
Charles Crayne192d5b52007-10-18 19:02:42 -07001520 (nparam <= 0 || m->nparam == 0 || nparam == (int) m->nparam)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001521 if (defn) {
Charles Crayne192d5b52007-10-18 19:02:42 -07001522 if (nparam == (int) m->nparam || nparam == -1)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001523 *defn = m;
1524 else
1525 *defn = NULL;
1526 }
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001527 return true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001528 }
1529 m = m->next;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001530 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001531
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001532 return false;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001533}
1534
1535/*
1536 * Count and mark off the parameters in a multi-line macro call.
1537 * This is called both from within the multi-line macro expansion
1538 * code, and also to mark off the default parameters when provided
1539 * in a %macro definition line.
1540 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001541static void count_mmac_params(Token * t, int *nparam, Token *** params)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001542{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001543 int paramsize, brace;
1544
1545 *nparam = paramsize = 0;
1546 *params = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001547 while (t) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001548 /* +1: we need space for the final NULL */
H. Peter Anvin91fb6f12008-09-01 10:56:33 -07001549 if (*nparam+1 >= paramsize) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001550 paramsize += PARAM_DELTA;
1551 *params = nasm_realloc(*params, sizeof(**params) * paramsize);
1552 }
1553 skip_white_(t);
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001554 brace = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001555 if (tok_is_(t, "{"))
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001556 brace = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001557 (*params)[(*nparam)++] = t;
1558 while (tok_isnt_(t, brace ? "}" : ","))
1559 t = t->next;
1560 if (t) { /* got a comma/brace */
1561 t = t->next;
1562 if (brace) {
1563 /*
1564 * Now we've found the closing brace, look further
1565 * for the comma.
1566 */
1567 skip_white_(t);
1568 if (tok_isnt_(t, ",")) {
1569 error(ERR_NONFATAL,
1570 "braces do not enclose all of macro parameter");
1571 while (tok_isnt_(t, ","))
1572 t = t->next;
1573 }
1574 if (t)
1575 t = t->next; /* eat the comma */
1576 }
1577 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001578 }
1579}
1580
1581/*
H. Peter Anvin76690a12002-04-30 20:52:49 +00001582 * Determine whether one of the various `if' conditions is true or
1583 * not.
1584 *
1585 * We must free the tline we get passed.
1586 */
H. Peter Anvin70055962007-10-11 00:05:31 -07001587static bool if_condition(Token * tline, enum preproc_token ct)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001588{
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001589 enum pp_conditional i = PP_COND(ct);
H. Peter Anvin70055962007-10-11 00:05:31 -07001590 bool j;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001591 Token *t, *tt, **tptr, *origline;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001592 struct tokenval tokval;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001593 expr *evalresult;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001594 enum pp_token_type needtype;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001595
1596 origline = tline;
1597
H. Peter Anvine2c80182005-01-15 22:15:51 +00001598 switch (i) {
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001599 case PPC_IFCTX:
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001600 j = false; /* have we matched yet? */
Victor van den Elzen0e857f12008-07-23 13:21:29 +02001601 while (true) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001602 skip_white_(tline);
Victor van den Elzen0e857f12008-07-23 13:21:29 +02001603 if (!tline)
1604 break;
1605 if (tline->type != TOK_ID) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001606 error(ERR_NONFATAL,
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001607 "`%s' expects context identifiers", pp_directives[ct]);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001608 free_tlist(origline);
1609 return -1;
1610 }
Victor van den Elzen0e857f12008-07-23 13:21:29 +02001611 if (cstk && cstk->name && !nasm_stricmp(tline->text, cstk->name))
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001612 j = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001613 tline = tline->next;
1614 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001615 break;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001616
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001617 case PPC_IFDEF:
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001618 j = false; /* have we matched yet? */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001619 while (tline) {
1620 skip_white_(tline);
1621 if (!tline || (tline->type != TOK_ID &&
1622 (tline->type != TOK_PREPROC_ID ||
1623 tline->text[1] != '$'))) {
1624 error(ERR_NONFATAL,
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001625 "`%s' expects macro identifiers", pp_directives[ct]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001626 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001627 }
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07001628 if (smacro_defined(NULL, tline->text, 0, NULL, true))
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001629 j = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001630 tline = tline->next;
1631 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001632 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001633
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001634 case PPC_IFIDN:
1635 case PPC_IFIDNI:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001636 tline = expand_smacro(tline);
1637 t = tt = tline;
1638 while (tok_isnt_(tt, ","))
1639 tt = tt->next;
1640 if (!tt) {
1641 error(ERR_NONFATAL,
1642 "`%s' expects two comma-separated arguments",
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001643 pp_directives[ct]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001644 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001645 }
1646 tt = tt->next;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001647 j = true; /* assume equality unless proved not */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001648 while ((t->type != TOK_OTHER || strcmp(t->text, ",")) && tt) {
1649 if (tt->type == TOK_OTHER && !strcmp(tt->text, ",")) {
1650 error(ERR_NONFATAL, "`%s': more than one comma on line",
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001651 pp_directives[ct]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001652 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001653 }
1654 if (t->type == TOK_WHITESPACE) {
1655 t = t->next;
1656 continue;
1657 }
1658 if (tt->type == TOK_WHITESPACE) {
1659 tt = tt->next;
1660 continue;
1661 }
1662 if (tt->type != t->type) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001663 j = false; /* found mismatching tokens */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001664 break;
1665 }
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07001666 /* When comparing strings, need to unquote them first */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001667 if (t->type == TOK_STRING) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001668 size_t l1 = nasm_unquote(t->text, NULL);
1669 size_t l2 = nasm_unquote(tt->text, NULL);
H. Peter Anvind2456592008-06-19 15:04:18 -07001670
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001671 if (l1 != l2) {
1672 j = false;
1673 break;
1674 }
1675 if (mmemcmp(t->text, tt->text, l1, i == PPC_IFIDN)) {
1676 j = false;
1677 break;
1678 }
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07001679 } else if (mstrcmp(tt->text, t->text, i == PPC_IFIDN) != 0) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001680 j = false; /* found mismatching tokens */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001681 break;
1682 }
Nickolay Yurchenkof3b3ce22003-09-21 20:38:43 +00001683
H. Peter Anvine2c80182005-01-15 22:15:51 +00001684 t = t->next;
1685 tt = tt->next;
1686 }
1687 if ((t->type != TOK_OTHER || strcmp(t->text, ",")) || tt)
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001688 j = false; /* trailing gunk on one end or other */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001689 break;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001690
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001691 case PPC_IFMACRO:
H. Peter Anvin89cee572009-07-15 09:16:54 -04001692 {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001693 bool found = false;
1694 MMacro searching, *mmac;
H. Peter Anvin65747262002-05-07 00:10:05 +00001695
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001696 skip_white_(tline);
1697 tline = expand_id(tline);
1698 if (!tok_type_(tline, TOK_ID)) {
1699 error(ERR_NONFATAL,
1700 "`%s' expects a macro name", pp_directives[ct]);
1701 goto fail;
1702 }
1703 searching.name = nasm_strdup(tline->text);
1704 searching.casesense = true;
1705 searching.plus = false;
1706 searching.nolist = false;
1707 searching.in_progress = 0;
1708 searching.max_depth = 0;
1709 searching.rep_nest = NULL;
1710 searching.nparam_min = 0;
1711 searching.nparam_max = INT_MAX;
1712 tline = expand_smacro(tline->next);
1713 skip_white_(tline);
1714 if (!tline) {
1715 } else if (!tok_type_(tline, TOK_NUMBER)) {
1716 error(ERR_NONFATAL,
1717 "`%s' expects a parameter count or nothing",
1718 pp_directives[ct]);
1719 } else {
1720 searching.nparam_min = searching.nparam_max =
1721 readnum(tline->text, &j);
1722 if (j)
1723 error(ERR_NONFATAL,
1724 "unable to parse parameter count `%s'",
1725 tline->text);
1726 }
1727 if (tline && tok_is_(tline->next, "-")) {
1728 tline = tline->next->next;
1729 if (tok_is_(tline, "*"))
1730 searching.nparam_max = INT_MAX;
1731 else if (!tok_type_(tline, TOK_NUMBER))
1732 error(ERR_NONFATAL,
1733 "`%s' expects a parameter count after `-'",
1734 pp_directives[ct]);
1735 else {
1736 searching.nparam_max = readnum(tline->text, &j);
1737 if (j)
1738 error(ERR_NONFATAL,
1739 "unable to parse parameter count `%s'",
1740 tline->text);
1741 if (searching.nparam_min > searching.nparam_max)
1742 error(ERR_NONFATAL,
1743 "minimum parameter count exceeds maximum");
1744 }
1745 }
1746 if (tline && tok_is_(tline->next, "+")) {
1747 tline = tline->next;
1748 searching.plus = true;
1749 }
1750 mmac = (MMacro *) hash_findix(&mmacros, searching.name);
1751 while (mmac) {
1752 if (!strcmp(mmac->name, searching.name) &&
1753 (mmac->nparam_min <= searching.nparam_max
1754 || searching.plus)
1755 && (searching.nparam_min <= mmac->nparam_max
1756 || mmac->plus)) {
1757 found = true;
1758 break;
1759 }
1760 mmac = mmac->next;
1761 }
1762 if (tline && tline->next)
1763 error(ERR_WARNING|ERR_PASS1,
1764 "trailing garbage after %%ifmacro ignored");
1765 nasm_free(searching.name);
1766 j = found;
1767 break;
H. Peter Anvin89cee572009-07-15 09:16:54 -04001768 }
H. Peter Anvin65747262002-05-07 00:10:05 +00001769
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001770 case PPC_IFID:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001771 needtype = TOK_ID;
1772 goto iftype;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001773 case PPC_IFNUM:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001774 needtype = TOK_NUMBER;
1775 goto iftype;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001776 case PPC_IFSTR:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001777 needtype = TOK_STRING;
1778 goto iftype;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001779
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001780iftype:
1781 t = tline = expand_smacro(tline);
H. Peter Anvind85d2502008-05-04 17:53:31 -07001782
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001783 while (tok_type_(t, TOK_WHITESPACE) ||
1784 (needtype == TOK_NUMBER &&
1785 tok_type_(t, TOK_OTHER) &&
1786 (t->text[0] == '-' || t->text[0] == '+') &&
1787 !t->text[1]))
1788 t = t->next;
H. Peter Anvind85d2502008-05-04 17:53:31 -07001789
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001790 j = tok_type_(t, needtype);
1791 break;
H. Peter Anvincbf768d2008-02-16 16:41:25 -08001792
1793 case PPC_IFTOKEN:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001794 t = tline = expand_smacro(tline);
1795 while (tok_type_(t, TOK_WHITESPACE))
1796 t = t->next;
H. Peter Anvincbf768d2008-02-16 16:41:25 -08001797
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001798 j = false;
1799 if (t) {
1800 t = t->next; /* Skip the actual token */
1801 while (tok_type_(t, TOK_WHITESPACE))
1802 t = t->next;
1803 j = !t; /* Should be nothing left */
1804 }
1805 break;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001806
H. Peter Anvin134b9462008-02-16 17:01:40 -08001807 case PPC_IFEMPTY:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001808 t = tline = expand_smacro(tline);
1809 while (tok_type_(t, TOK_WHITESPACE))
1810 t = t->next;
H. Peter Anvin134b9462008-02-16 17:01:40 -08001811
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001812 j = !t; /* Should be empty */
1813 break;
H. Peter Anvin134b9462008-02-16 17:01:40 -08001814
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001815 case PPC_IF:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001816 t = tline = expand_smacro(tline);
1817 tptr = &t;
1818 tokval.t_type = TOKEN_INVALID;
1819 evalresult = evaluate(ppscan, tptr, &tokval,
1820 NULL, pass | CRITICAL, error, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001821 if (!evalresult)
1822 return -1;
1823 if (tokval.t_type)
H. Peter Anvin917a3492008-09-24 09:14:49 -07001824 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +00001825 "trailing garbage after expression ignored");
1826 if (!is_simple(evalresult)) {
1827 error(ERR_NONFATAL,
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001828 "non-constant value given to `%s'", pp_directives[ct]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001829 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001830 }
Chuck Crayne60ae75d2007-05-02 01:59:16 +00001831 j = reloc_value(evalresult) != 0;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001832 break;
H. Peter Anvin95e28822007-09-12 04:20:08 +00001833
H. Peter Anvine2c80182005-01-15 22:15:51 +00001834 default:
1835 error(ERR_FATAL,
1836 "preprocessor directive `%s' not yet implemented",
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001837 pp_directives[ct]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001838 goto fail;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001839 }
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001840
1841 free_tlist(origline);
1842 return j ^ PP_NEGATIVE(ct);
H. Peter Anvin70653092007-10-19 14:42:29 -07001843
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001844fail:
1845 free_tlist(origline);
1846 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001847}
1848
1849/*
H. Peter Anvin4db5a162007-10-11 13:42:09 -07001850 * Common code for defining an smacro
1851 */
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001852static bool define_smacro(Context *ctx, const char *mname, bool casesense,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001853 int nparam, Token *expansion)
H. Peter Anvin4db5a162007-10-11 13:42:09 -07001854{
1855 SMacro *smac, **smhead;
H. Peter Anvin166c2472008-05-28 12:28:58 -07001856 struct hash_table *smtbl;
H. Peter Anvin70653092007-10-19 14:42:29 -07001857
H. Peter Anvin4db5a162007-10-11 13:42:09 -07001858 if (smacro_defined(ctx, mname, nparam, &smac, casesense)) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001859 if (!smac) {
1860 error(ERR_WARNING|ERR_PASS1,
1861 "single-line macro `%s' defined both with and"
1862 " without parameters", mname);
1863 /*
1864 * Some instances of the old code considered this a failure,
1865 * some others didn't. What is the right thing to do here?
1866 */
1867 free_tlist(expansion);
1868 return false; /* Failure */
1869 } else {
1870 /*
1871 * We're redefining, so we have to take over an
1872 * existing SMacro structure. This means freeing
1873 * what was already in it.
1874 */
1875 nasm_free(smac->name);
1876 free_tlist(smac->expansion);
1877 }
H. Peter Anvin4db5a162007-10-11 13:42:09 -07001878 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001879 smtbl = ctx ? &ctx->localmac : &smacros;
1880 smhead = (SMacro **) hash_findi_add(smtbl, mname);
1881 smac = nasm_malloc(sizeof(SMacro));
1882 smac->next = *smhead;
1883 *smhead = smac;
H. Peter Anvin4db5a162007-10-11 13:42:09 -07001884 }
1885 smac->name = nasm_strdup(mname);
1886 smac->casesense = casesense;
1887 smac->nparam = nparam;
1888 smac->expansion = expansion;
1889 smac->in_progress = false;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001890 return true; /* Success */
H. Peter Anvin4db5a162007-10-11 13:42:09 -07001891}
1892
1893/*
1894 * Undefine an smacro
1895 */
1896static void undef_smacro(Context *ctx, const char *mname)
1897{
1898 SMacro **smhead, *s, **sp;
H. Peter Anvin166c2472008-05-28 12:28:58 -07001899 struct hash_table *smtbl;
H. Peter Anvin4db5a162007-10-11 13:42:09 -07001900
H. Peter Anvin166c2472008-05-28 12:28:58 -07001901 smtbl = ctx ? &ctx->localmac : &smacros;
1902 smhead = (SMacro **)hash_findi(smtbl, mname, NULL);
H. Peter Anvin70653092007-10-19 14:42:29 -07001903
H. Peter Anvin4db5a162007-10-11 13:42:09 -07001904 if (smhead) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001905 /*
1906 * We now have a macro name... go hunt for it.
1907 */
1908 sp = smhead;
1909 while ((s = *sp) != NULL) {
1910 if (!mstrcmp(s->name, mname, s->casesense)) {
1911 *sp = s->next;
1912 nasm_free(s->name);
1913 free_tlist(s->expansion);
1914 nasm_free(s);
1915 } else {
1916 sp = &s->next;
1917 }
1918 }
H. Peter Anvin4db5a162007-10-11 13:42:09 -07001919 }
1920}
1921
H. Peter Anvin8781cb02007-11-08 20:01:11 -08001922/*
H. Peter Anvina26433d2008-07-16 14:40:01 -07001923 * Parse a mmacro specification.
1924 */
1925static bool parse_mmacro_spec(Token *tline, MMacro *def, const char *directive)
1926{
1927 bool err;
1928
1929 tline = tline->next;
1930 skip_white_(tline);
1931 tline = expand_id(tline);
1932 if (!tok_type_(tline, TOK_ID)) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001933 error(ERR_NONFATAL, "`%s' expects a macro name", directive);
1934 return false;
H. Peter Anvina26433d2008-07-16 14:40:01 -07001935 }
Victor van den Elzenb916ede2008-07-23 15:14:22 +02001936
H. Peter Anvin89cee572009-07-15 09:16:54 -04001937 def->prev = NULL;
H. Peter Anvina26433d2008-07-16 14:40:01 -07001938 def->name = nasm_strdup(tline->text);
1939 def->plus = false;
1940 def->nolist = false;
1941 def->in_progress = 0;
1942 def->rep_nest = NULL;
Victor van den Elzenb916ede2008-07-23 15:14:22 +02001943 def->nparam_min = 0;
1944 def->nparam_max = 0;
1945
H. Peter Anvina26433d2008-07-16 14:40:01 -07001946 tline = expand_smacro(tline->next);
1947 skip_white_(tline);
1948 if (!tok_type_(tline, TOK_NUMBER)) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001949 error(ERR_NONFATAL, "`%s' expects a parameter count", directive);
H. Peter Anvina26433d2008-07-16 14:40:01 -07001950 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001951 def->nparam_min = def->nparam_max =
1952 readnum(tline->text, &err);
1953 if (err)
1954 error(ERR_NONFATAL,
1955 "unable to parse parameter count `%s'", tline->text);
H. Peter Anvina26433d2008-07-16 14:40:01 -07001956 }
1957 if (tline && tok_is_(tline->next, "-")) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001958 tline = tline->next->next;
1959 if (tok_is_(tline, "*")) {
1960 def->nparam_max = INT_MAX;
1961 } else if (!tok_type_(tline, TOK_NUMBER)) {
1962 error(ERR_NONFATAL,
1963 "`%s' expects a parameter count after `-'", directive);
1964 } else {
1965 def->nparam_max = readnum(tline->text, &err);
1966 if (err) {
1967 error(ERR_NONFATAL, "unable to parse parameter count `%s'",
1968 tline->text);
1969 }
1970 if (def->nparam_min > def->nparam_max) {
1971 error(ERR_NONFATAL, "minimum parameter count exceeds maximum");
1972 }
1973 }
H. Peter Anvina26433d2008-07-16 14:40:01 -07001974 }
1975 if (tline && tok_is_(tline->next, "+")) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001976 tline = tline->next;
1977 def->plus = true;
H. Peter Anvina26433d2008-07-16 14:40:01 -07001978 }
1979 if (tline && tok_type_(tline->next, TOK_ID) &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001980 !nasm_stricmp(tline->next->text, ".nolist")) {
1981 tline = tline->next;
1982 def->nolist = true;
H. Peter Anvina26433d2008-07-16 14:40:01 -07001983 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001984
H. Peter Anvina26433d2008-07-16 14:40:01 -07001985 /*
1986 * Handle default parameters.
1987 */
1988 if (tline && tline->next) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001989 def->dlist = tline->next;
1990 tline->next = NULL;
1991 count_mmac_params(def->dlist, &def->ndefs, &def->defaults);
H. Peter Anvina26433d2008-07-16 14:40:01 -07001992 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001993 def->dlist = NULL;
1994 def->defaults = NULL;
H. Peter Anvina26433d2008-07-16 14:40:01 -07001995 }
1996 def->expansion = NULL;
1997
H. Peter Anvin89cee572009-07-15 09:16:54 -04001998 if (def->defaults && def->ndefs > def->nparam_max - def->nparam_min &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001999 !def->plus)
2000 error(ERR_WARNING|ERR_PASS1|ERR_WARN_MDP,
2001 "too many default macro parameters");
Victor van den Elzenb916ede2008-07-23 15:14:22 +02002002
H. Peter Anvina26433d2008-07-16 14:40:01 -07002003 return true;
2004}
2005
2006
2007/*
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002008 * Decode a size directive
2009 */
2010static int parse_size(const char *str) {
2011 static const char *size_names[] =
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002012 { "byte", "dword", "oword", "qword", "tword", "word", "yword" };
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002013 static const int sizes[] =
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002014 { 0, 1, 4, 16, 8, 10, 2, 32 };
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002015
2016 return sizes[bsii(str, size_names, elements(size_names))+1];
2017}
2018
H. Peter Anvinf9c9a672009-07-14 15:14:05 -04002019/*
2020 * nasm_unquote with error if the string contains NUL characters.
2021 * If the string contains NUL characters, issue an error and return
2022 * the C len, i.e. truncate at the NUL.
2023 */
2024static size_t nasm_unquote_cstr(char *qstr, enum preproc_token directive)
2025{
2026 size_t len = nasm_unquote(qstr, NULL);
2027 size_t clen = strlen(qstr);
2028
2029 if (len != clen)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002030 error(ERR_NONFATAL, "NUL character in `%s' directive",
2031 pp_directives[directive]);
H. Peter Anvinf9c9a672009-07-14 15:14:05 -04002032
2033 return clen;
2034}
2035
Ed Beroset3ab3f412002-06-11 03:31:49 +00002036/**
2037 * find and process preprocessor directive in passed line
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002038 * Find out if a line contains a preprocessor directive, and deal
2039 * with it if so.
H. Peter Anvin70653092007-10-19 14:42:29 -07002040 *
Ed Beroset3ab3f412002-06-11 03:31:49 +00002041 * If a directive _is_ found, it is the responsibility of this routine
2042 * (and not the caller) to free_tlist() the line.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002043 *
Ed Beroset3ab3f412002-06-11 03:31:49 +00002044 * @param tline a pointer to the current tokeninzed line linked list
2045 * @return DIRECTIVE_FOUND or NO_DIRECTIVE_FOUND
H. Peter Anvin70653092007-10-19 14:42:29 -07002046 *
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002047 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002048static int do_directive(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00002049{
H. Peter Anvin4169a472007-09-12 01:29:43 +00002050 enum preproc_token i;
2051 int j;
H. Peter Anvin70055962007-10-11 00:05:31 -07002052 bool err;
2053 int nparam;
2054 bool nolist;
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07002055 bool casesense;
H. Peter Anvin8cfdb9d2007-09-14 18:36:01 -07002056 int k, m;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002057 int offset;
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08002058 char *p, *pp;
2059 const char *mname;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002060 Include *inc;
2061 Context *ctx;
2062 Cond *cond;
H. Peter Anvin97a23472007-09-16 17:57:25 -07002063 MMacro *mmac, **mmhead;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002064 Token *t, *tt, *param_start, *macro_start, *last, **tptr, *origline;
2065 Line *l;
2066 struct tokenval tokval;
2067 expr *evalresult;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002068 MMacro *tmp_defining; /* Used when manipulating rep_nest */
H. Peter Anvinf8ba53e2007-10-11 10:11:57 -07002069 int64_t count;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07002070 size_t len;
H. Peter Anvin8e3f75e2008-09-24 00:21:58 -07002071 int severity;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002072
2073 origline = tline;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002074
H. Peter Anvineba20a72002-04-30 20:53:55 +00002075 skip_white_(tline);
H. Peter Anvinf2936d72008-06-04 15:11:23 -07002076 if (!tline || !tok_type_(tline, TOK_PREPROC_ID) ||
H. Peter Anvine2c80182005-01-15 22:15:51 +00002077 (tline->text[1] == '%' || tline->text[1] == '$'
2078 || tline->text[1] == '!'))
2079 return NO_DIRECTIVE_FOUND;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002080
H. Peter Anvin4169a472007-09-12 01:29:43 +00002081 i = pp_token_hash(tline->text);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002082
2083 /*
2084 * If we're in a non-emitting branch of a condition construct,
H. Peter Anvin76690a12002-04-30 20:52:49 +00002085 * or walking to the end of an already terminated %rep block,
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002086 * we should ignore all directives except for condition
2087 * directives.
2088 */
H. Peter Anvin76690a12002-04-30 20:52:49 +00002089 if (((istk->conds && !emitting(istk->conds->state)) ||
H. Peter Anvine2c80182005-01-15 22:15:51 +00002090 (istk->mstk && !istk->mstk->in_progress)) && !is_condition(i)) {
2091 return NO_DIRECTIVE_FOUND;
H. Peter Anvineba20a72002-04-30 20:53:55 +00002092 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002093
2094 /*
H. Peter Anvin76690a12002-04-30 20:52:49 +00002095 * If we're defining a macro or reading a %rep block, we should
Victor van den Elzen8f1120f2008-07-16 13:41:37 +02002096 * ignore all directives except for %macro/%imacro (which nest),
2097 * %endm/%endmacro, and (only if we're in a %rep block) %endrep.
2098 * If we're in a %rep block, another %rep nests, so should be let through.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002099 */
2100 if (defining && i != PP_MACRO && i != PP_IMACRO &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002101 i != PP_RMACRO && i != PP_IRMACRO &&
H. Peter Anvine2c80182005-01-15 22:15:51 +00002102 i != PP_ENDMACRO && i != PP_ENDM &&
2103 (defining->name || (i != PP_ENDREP && i != PP_REP))) {
2104 return NO_DIRECTIVE_FOUND;
H. Peter Anvineba20a72002-04-30 20:53:55 +00002105 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002106
Charles Crayned4200be2008-07-12 16:42:33 -07002107 if (defining) {
Keith Kanios852f1ee2009-07-12 00:19:55 -05002108 if (i == PP_MACRO || i == PP_IMACRO ||
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002109 i == PP_RMACRO || i == PP_IRMACRO) {
Charles Crayned4200be2008-07-12 16:42:33 -07002110 nested_mac_count++;
2111 return NO_DIRECTIVE_FOUND;
2112 } else if (nested_mac_count > 0) {
2113 if (i == PP_ENDMACRO) {
2114 nested_mac_count--;
2115 return NO_DIRECTIVE_FOUND;
2116 }
2117 }
2118 if (!defining->name) {
2119 if (i == PP_REP) {
2120 nested_rep_count++;
2121 return NO_DIRECTIVE_FOUND;
2122 } else if (nested_rep_count > 0) {
2123 if (i == PP_ENDREP) {
2124 nested_rep_count--;
2125 return NO_DIRECTIVE_FOUND;
2126 }
2127 }
2128 }
2129 }
2130
H. Peter Anvin4169a472007-09-12 01:29:43 +00002131 switch (i) {
2132 case PP_INVALID:
H. Peter Anvine2c80182005-01-15 22:15:51 +00002133 error(ERR_NONFATAL, "unknown preprocessor directive `%s'",
2134 tline->text);
2135 return NO_DIRECTIVE_FOUND; /* didn't get it */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002136
H. Peter Anvine2c80182005-01-15 22:15:51 +00002137 case PP_STACKSIZE:
2138 /* Directive to tell NASM what the default stack size is. The
2139 * default is for a 16-bit stack, and this can be overriden with
2140 * %stacksize large.
H. Peter Anvine2c80182005-01-15 22:15:51 +00002141 */
2142 tline = tline->next;
2143 if (tline && tline->type == TOK_WHITESPACE)
2144 tline = tline->next;
2145 if (!tline || tline->type != TOK_ID) {
2146 error(ERR_NONFATAL, "`%%stacksize' missing size parameter");
2147 free_tlist(origline);
2148 return DIRECTIVE_FOUND;
2149 }
2150 if (nasm_stricmp(tline->text, "flat") == 0) {
2151 /* All subsequent ARG directives are for a 32-bit stack */
2152 StackSize = 4;
2153 StackPointer = "ebp";
2154 ArgOffset = 8;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002155 LocalOffset = 0;
Charles Crayne7eaf9192007-11-08 22:11:14 -08002156 } else if (nasm_stricmp(tline->text, "flat64") == 0) {
2157 /* All subsequent ARG directives are for a 64-bit stack */
2158 StackSize = 8;
2159 StackPointer = "rbp";
Per Jessen53252e02010-02-11 00:16:59 +03002160 ArgOffset = 16;
Charles Crayne7eaf9192007-11-08 22:11:14 -08002161 LocalOffset = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002162 } else if (nasm_stricmp(tline->text, "large") == 0) {
2163 /* All subsequent ARG directives are for a 16-bit stack,
2164 * far function call.
2165 */
2166 StackSize = 2;
2167 StackPointer = "bp";
2168 ArgOffset = 4;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002169 LocalOffset = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002170 } else if (nasm_stricmp(tline->text, "small") == 0) {
2171 /* All subsequent ARG directives are for a 16-bit stack,
2172 * far function call. We don't support near functions.
2173 */
2174 StackSize = 2;
2175 StackPointer = "bp";
2176 ArgOffset = 6;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002177 LocalOffset = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002178 } else {
2179 error(ERR_NONFATAL, "`%%stacksize' invalid size type");
2180 free_tlist(origline);
2181 return DIRECTIVE_FOUND;
2182 }
2183 free_tlist(origline);
2184 return DIRECTIVE_FOUND;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002185
H. Peter Anvine2c80182005-01-15 22:15:51 +00002186 case PP_ARG:
2187 /* TASM like ARG directive to define arguments to functions, in
2188 * the following form:
2189 *
2190 * ARG arg1:WORD, arg2:DWORD, arg4:QWORD
2191 */
2192 offset = ArgOffset;
2193 do {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00002194 char *arg, directive[256];
H. Peter Anvine2c80182005-01-15 22:15:51 +00002195 int size = StackSize;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002196
H. Peter Anvine2c80182005-01-15 22:15:51 +00002197 /* Find the argument name */
2198 tline = tline->next;
2199 if (tline && tline->type == TOK_WHITESPACE)
2200 tline = tline->next;
2201 if (!tline || tline->type != TOK_ID) {
2202 error(ERR_NONFATAL, "`%%arg' missing argument parameter");
2203 free_tlist(origline);
2204 return DIRECTIVE_FOUND;
2205 }
2206 arg = tline->text;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002207
H. Peter Anvine2c80182005-01-15 22:15:51 +00002208 /* Find the argument size type */
2209 tline = tline->next;
2210 if (!tline || tline->type != TOK_OTHER
2211 || tline->text[0] != ':') {
2212 error(ERR_NONFATAL,
2213 "Syntax error processing `%%arg' directive");
2214 free_tlist(origline);
2215 return DIRECTIVE_FOUND;
2216 }
2217 tline = tline->next;
2218 if (!tline || tline->type != TOK_ID) {
2219 error(ERR_NONFATAL, "`%%arg' missing size type parameter");
2220 free_tlist(origline);
2221 return DIRECTIVE_FOUND;
2222 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002223
H. Peter Anvine2c80182005-01-15 22:15:51 +00002224 /* Allow macro expansion of type parameter */
Keith Kaniosb7a89542007-04-12 02:40:54 +00002225 tt = tokenize(tline->text);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002226 tt = expand_smacro(tt);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002227 size = parse_size(tt->text);
2228 if (!size) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002229 error(ERR_NONFATAL,
2230 "Invalid size type for `%%arg' missing directive");
2231 free_tlist(tt);
2232 free_tlist(origline);
2233 return DIRECTIVE_FOUND;
2234 }
2235 free_tlist(tt);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002236
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002237 /* Round up to even stack slots */
2238 size = ALIGN(size, StackSize);
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002239
H. Peter Anvine2c80182005-01-15 22:15:51 +00002240 /* Now define the macro for the argument */
2241 snprintf(directive, sizeof(directive), "%%define %s (%s+%d)",
2242 arg, StackPointer, offset);
Keith Kaniosb7a89542007-04-12 02:40:54 +00002243 do_directive(tokenize(directive));
H. Peter Anvine2c80182005-01-15 22:15:51 +00002244 offset += size;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002245
H. Peter Anvine2c80182005-01-15 22:15:51 +00002246 /* Move to the next argument in the list */
2247 tline = tline->next;
2248 if (tline && tline->type == TOK_WHITESPACE)
2249 tline = tline->next;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002250 } while (tline && tline->type == TOK_OTHER && tline->text[0] == ',');
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002251 ArgOffset = offset;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002252 free_tlist(origline);
2253 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002254
H. Peter Anvine2c80182005-01-15 22:15:51 +00002255 case PP_LOCAL:
2256 /* TASM like LOCAL directive to define local variables for a
2257 * function, in the following form:
2258 *
2259 * LOCAL local1:WORD, local2:DWORD, local4:QWORD = LocalSize
2260 *
2261 * The '= LocalSize' at the end is ignored by NASM, but is
2262 * required by TASM to define the local parameter size (and used
2263 * by the TASM macro package).
2264 */
2265 offset = LocalOffset;
2266 do {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00002267 char *local, directive[256];
H. Peter Anvine2c80182005-01-15 22:15:51 +00002268 int size = StackSize;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002269
H. Peter Anvine2c80182005-01-15 22:15:51 +00002270 /* Find the argument name */
2271 tline = tline->next;
2272 if (tline && tline->type == TOK_WHITESPACE)
2273 tline = tline->next;
2274 if (!tline || tline->type != TOK_ID) {
2275 error(ERR_NONFATAL,
2276 "`%%local' missing argument parameter");
2277 free_tlist(origline);
2278 return DIRECTIVE_FOUND;
2279 }
2280 local = tline->text;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002281
H. Peter Anvine2c80182005-01-15 22:15:51 +00002282 /* Find the argument size type */
2283 tline = tline->next;
2284 if (!tline || tline->type != TOK_OTHER
2285 || tline->text[0] != ':') {
2286 error(ERR_NONFATAL,
2287 "Syntax error processing `%%local' directive");
2288 free_tlist(origline);
2289 return DIRECTIVE_FOUND;
2290 }
2291 tline = tline->next;
2292 if (!tline || tline->type != TOK_ID) {
2293 error(ERR_NONFATAL,
2294 "`%%local' missing size type parameter");
2295 free_tlist(origline);
2296 return DIRECTIVE_FOUND;
2297 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00002298
H. Peter Anvine2c80182005-01-15 22:15:51 +00002299 /* Allow macro expansion of type parameter */
Keith Kaniosb7a89542007-04-12 02:40:54 +00002300 tt = tokenize(tline->text);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002301 tt = expand_smacro(tt);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002302 size = parse_size(tt->text);
2303 if (!size) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002304 error(ERR_NONFATAL,
2305 "Invalid size type for `%%local' missing directive");
2306 free_tlist(tt);
2307 free_tlist(origline);
2308 return DIRECTIVE_FOUND;
2309 }
2310 free_tlist(tt);
H. Peter Anvin734b1882002-04-30 21:01:08 +00002311
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002312 /* Round up to even stack slots */
2313 size = ALIGN(size, StackSize);
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002314
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002315 offset += size; /* Negative offset, increment before */
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002316
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002317 /* Now define the macro for the argument */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002318 snprintf(directive, sizeof(directive), "%%define %s (%s-%d)",
2319 local, StackPointer, offset);
Keith Kaniosb7a89542007-04-12 02:40:54 +00002320 do_directive(tokenize(directive));
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002321
H. Peter Anvine2c80182005-01-15 22:15:51 +00002322 /* Now define the assign to setup the enter_c macro correctly */
2323 snprintf(directive, sizeof(directive),
2324 "%%assign %%$localsize %%$localsize+%d", size);
Keith Kaniosb7a89542007-04-12 02:40:54 +00002325 do_directive(tokenize(directive));
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002326
H. Peter Anvine2c80182005-01-15 22:15:51 +00002327 /* Move to the next argument in the list */
2328 tline = tline->next;
2329 if (tline && tline->type == TOK_WHITESPACE)
2330 tline = tline->next;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002331 } while (tline && tline->type == TOK_OTHER && tline->text[0] == ',');
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002332 LocalOffset = offset;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002333 free_tlist(origline);
2334 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002335
H. Peter Anvine2c80182005-01-15 22:15:51 +00002336 case PP_CLEAR:
2337 if (tline->next)
H. Peter Anvin917a3492008-09-24 09:14:49 -07002338 error(ERR_WARNING|ERR_PASS1,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002339 "trailing garbage after `%%clear' ignored");
2340 free_macros();
2341 init_macros();
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 Anvin418ca702008-05-30 10:42:30 -07002345 case PP_DEPEND:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002346 t = tline->next = expand_smacro(tline->next);
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002347 skip_white_(t);
2348 if (!t || (t->type != TOK_STRING &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002349 t->type != TOK_INTERNAL_STRING)) {
H. Peter Anvin418ca702008-05-30 10:42:30 -07002350 error(ERR_NONFATAL, "`%%depend' expects a file name");
2351 free_tlist(origline);
2352 return DIRECTIVE_FOUND; /* but we did _something_ */
2353 }
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002354 if (t->next)
H. Peter Anvin917a3492008-09-24 09:14:49 -07002355 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvin418ca702008-05-30 10:42:30 -07002356 "trailing garbage after `%%depend' ignored");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002357 p = t->text;
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002358 if (t->type != TOK_INTERNAL_STRING)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002359 nasm_unquote_cstr(p, i);
2360 if (dephead && !in_list(*dephead, p)) {
2361 StrList *sl = nasm_malloc(strlen(p)+1+sizeof sl->next);
2362 sl->next = NULL;
2363 strcpy(sl->str, p);
2364 *deptail = sl;
2365 deptail = &sl->next;
2366 }
2367 free_tlist(origline);
H. Peter Anvin418ca702008-05-30 10:42:30 -07002368 return DIRECTIVE_FOUND;
2369
2370 case PP_INCLUDE:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002371 t = tline->next = expand_smacro(tline->next);
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002372 skip_white_(t);
H. Peter Anvind2456592008-06-19 15:04:18 -07002373
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002374 if (!t || (t->type != TOK_STRING &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002375 t->type != TOK_INTERNAL_STRING)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002376 error(ERR_NONFATAL, "`%%include' expects a file name");
2377 free_tlist(origline);
2378 return DIRECTIVE_FOUND; /* but we did _something_ */
2379 }
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002380 if (t->next)
H. Peter Anvin917a3492008-09-24 09:14:49 -07002381 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +00002382 "trailing garbage after `%%include' ignored");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002383 p = t->text;
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002384 if (t->type != TOK_INTERNAL_STRING)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002385 nasm_unquote_cstr(p, i);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002386 inc = nasm_malloc(sizeof(Include));
2387 inc->next = istk;
2388 inc->conds = NULL;
H. Peter Anvin2b1c3b92008-06-06 10:38:46 -07002389 inc->fp = inc_fopen(p, dephead, &deptail, pass == 0);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002390 if (!inc->fp) {
2391 /* -MG given but file not found */
2392 nasm_free(inc);
2393 } else {
2394 inc->fname = src_set_fname(nasm_strdup(p));
2395 inc->lineno = src_set_linnum(0);
2396 inc->lineinc = 1;
2397 inc->expansion = NULL;
2398 inc->mstk = NULL;
2399 istk = inc;
2400 list->uplevel(LIST_INCLUDE);
2401 }
2402 free_tlist(origline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002403 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002404
H. Peter Anvind2456592008-06-19 15:04:18 -07002405 case PP_USE:
H. Peter Anvinf4ae5ad2008-06-19 18:39:24 -07002406 {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002407 static macros_t *use_pkg;
2408 const char *pkg_macro = NULL;
H. Peter Anvinf4ae5ad2008-06-19 18:39:24 -07002409
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002410 tline = tline->next;
2411 skip_white_(tline);
2412 tline = expand_id(tline);
H. Peter Anvind2456592008-06-19 15:04:18 -07002413
H. Peter Anvin264b7b92008-10-24 16:38:17 -07002414 if (!tline || (tline->type != TOK_STRING &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002415 tline->type != TOK_INTERNAL_STRING &&
2416 tline->type != TOK_ID)) {
H. Peter Anvin926fc402008-06-19 16:26:12 -07002417 error(ERR_NONFATAL, "`%%use' expects a package name");
H. Peter Anvind2456592008-06-19 15:04:18 -07002418 free_tlist(origline);
2419 return DIRECTIVE_FOUND; /* but we did _something_ */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002420 }
H. Peter Anvin264b7b92008-10-24 16:38:17 -07002421 if (tline->next)
H. Peter Anvin917a3492008-09-24 09:14:49 -07002422 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvind2456592008-06-19 15:04:18 -07002423 "trailing garbage after `%%use' ignored");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002424 if (tline->type == TOK_STRING)
2425 nasm_unquote_cstr(tline->text, i);
2426 use_pkg = nasm_stdmac_find_package(tline->text);
2427 if (!use_pkg)
2428 error(ERR_NONFATAL, "unknown `%%use' package: %s", tline->text);
2429 else
2430 pkg_macro = (char *)use_pkg + 1; /* The first string will be <%define>__USE_*__ */
2431 if (use_pkg && smacro_defined(NULL, pkg_macro, 0, NULL, true)) {
2432 /* Not already included, go ahead and include it */
2433 stdmacpos = use_pkg;
2434 }
2435 free_tlist(origline);
H. Peter Anvind2456592008-06-19 15:04:18 -07002436 return DIRECTIVE_FOUND;
H. Peter Anvinf4ae5ad2008-06-19 18:39:24 -07002437 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002438 case PP_PUSH:
H. Peter Anvine2c80182005-01-15 22:15:51 +00002439 case PP_REPL:
H. Peter Anvin42b56392008-10-24 16:24:21 -07002440 case PP_POP:
H. Peter Anvine2c80182005-01-15 22:15:51 +00002441 tline = tline->next;
2442 skip_white_(tline);
2443 tline = expand_id(tline);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002444 if (tline) {
2445 if (!tok_type_(tline, TOK_ID)) {
2446 error(ERR_NONFATAL, "`%s' expects a context identifier",
2447 pp_directives[i]);
2448 free_tlist(origline);
2449 return DIRECTIVE_FOUND; /* but we did _something_ */
2450 }
2451 if (tline->next)
2452 error(ERR_WARNING|ERR_PASS1,
2453 "trailing garbage after `%s' ignored",
2454 pp_directives[i]);
2455 p = nasm_strdup(tline->text);
2456 } else {
2457 p = NULL; /* Anonymous */
2458 }
H. Peter Anvin42b56392008-10-24 16:24:21 -07002459
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002460 if (i == PP_PUSH) {
2461 ctx = nasm_malloc(sizeof(Context));
2462 ctx->next = cstk;
2463 hash_init(&ctx->localmac, HASH_SMALL);
2464 ctx->name = p;
2465 ctx->number = unique++;
2466 cstk = ctx;
2467 } else {
2468 /* %pop or %repl */
2469 if (!cstk) {
2470 error(ERR_NONFATAL, "`%s': context stack is empty",
2471 pp_directives[i]);
2472 } else if (i == PP_POP) {
2473 if (p && (!cstk->name || nasm_stricmp(p, cstk->name)))
2474 error(ERR_NONFATAL, "`%%pop' in wrong context: %s, "
2475 "expected %s",
2476 cstk->name ? cstk->name : "anonymous", p);
2477 else
2478 ctx_pop();
2479 } else {
2480 /* i == PP_REPL */
2481 nasm_free(cstk->name);
2482 cstk->name = p;
2483 p = NULL;
2484 }
2485 nasm_free(p);
2486 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002487 free_tlist(origline);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002488 return DIRECTIVE_FOUND;
H. Peter Anvin8e3f75e2008-09-24 00:21:58 -07002489 case PP_FATAL:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002490 severity = ERR_FATAL;
2491 goto issue_error;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002492 case PP_ERROR:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002493 severity = ERR_NONFATAL;
2494 goto issue_error;
H. Peter Anvin7df04172008-06-10 18:27:38 -07002495 case PP_WARNING:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002496 severity = ERR_WARNING|ERR_WARN_USER;
2497 goto issue_error;
H. Peter Anvin8e3f75e2008-09-24 00:21:58 -07002498
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002499issue_error:
H. Peter Anvin7df04172008-06-10 18:27:38 -07002500 {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002501 /* Only error out if this is the final pass */
2502 if (pass != 2 && i != PP_FATAL)
2503 return DIRECTIVE_FOUND;
2504
2505 tline->next = expand_smacro(tline->next);
2506 tline = tline->next;
2507 skip_white_(tline);
2508 t = tline ? tline->next : NULL;
2509 skip_white_(t);
2510 if (tok_type_(tline, TOK_STRING) && !t) {
2511 /* The line contains only a quoted string */
2512 p = tline->text;
2513 nasm_unquote(p, NULL); /* Ignore NUL character truncation */
2514 error(severity, "%s", p);
2515 } else {
2516 /* Not a quoted string, or more than a quoted string */
2517 p = detoken(tline, false);
2518 error(severity, "%s", p);
2519 nasm_free(p);
2520 }
2521 free_tlist(origline);
2522 return DIRECTIVE_FOUND;
H. Peter Anvin7df04172008-06-10 18:27:38 -07002523 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00002524
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002525 CASE_PP_IF:
H. Peter Anvine2c80182005-01-15 22:15:51 +00002526 if (istk->conds && !emitting(istk->conds->state))
2527 j = COND_NEVER;
2528 else {
2529 j = if_condition(tline->next, i);
2530 tline->next = NULL; /* it got freed */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002531 j = j < 0 ? COND_NEVER : j ? COND_IF_TRUE : COND_IF_FALSE;
2532 }
2533 cond = nasm_malloc(sizeof(Cond));
2534 cond->next = istk->conds;
2535 cond->state = j;
2536 istk->conds = cond;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002537 if(istk->mstk)
Keith Kanios3c0d91f2009-10-25 13:28:03 -05002538 istk->mstk->condcnt ++;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002539 free_tlist(origline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002540 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002541
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002542 CASE_PP_ELIF:
H. Peter Anvine2c80182005-01-15 22:15:51 +00002543 if (!istk->conds)
H. Peter Anvin4169a472007-09-12 01:29:43 +00002544 error(ERR_FATAL, "`%s': no matching `%%if'", pp_directives[i]);
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002545 switch(istk->conds->state) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002546 case COND_IF_TRUE:
2547 istk->conds->state = COND_DONE;
2548 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002549
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002550 case COND_DONE:
2551 case COND_NEVER:
2552 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002553
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002554 case COND_ELSE_TRUE:
2555 case COND_ELSE_FALSE:
2556 error_precond(ERR_WARNING|ERR_PASS1,
2557 "`%%elif' after `%%else' ignored");
2558 istk->conds->state = COND_NEVER;
2559 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002560
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002561 case COND_IF_FALSE:
2562 /*
2563 * IMPORTANT: In the case of %if, we will already have
2564 * called expand_mmac_params(); however, if we're
2565 * processing an %elif we must have been in a
2566 * non-emitting mode, which would have inhibited
2567 * the normal invocation of expand_mmac_params().
2568 * Therefore, we have to do it explicitly here.
2569 */
2570 j = if_condition(expand_mmac_params(tline->next), i);
2571 tline->next = NULL; /* it got freed */
2572 istk->conds->state =
2573 j < 0 ? COND_NEVER : j ? COND_IF_TRUE : COND_IF_FALSE;
2574 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002575 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002576 free_tlist(origline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002577 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002578
H. Peter Anvine2c80182005-01-15 22:15:51 +00002579 case PP_ELSE:
2580 if (tline->next)
H. Peter Anvin917a3492008-09-24 09:14:49 -07002581 error_precond(ERR_WARNING|ERR_PASS1,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002582 "trailing garbage after `%%else' ignored");
H. Peter Anvine2c80182005-01-15 22:15:51 +00002583 if (!istk->conds)
2584 error(ERR_FATAL, "`%%else': no matching `%%if'");
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002585 switch(istk->conds->state) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002586 case COND_IF_TRUE:
2587 case COND_DONE:
2588 istk->conds->state = COND_ELSE_FALSE;
2589 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002590
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002591 case COND_NEVER:
2592 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002593
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002594 case COND_IF_FALSE:
2595 istk->conds->state = COND_ELSE_TRUE;
2596 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002597
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002598 case COND_ELSE_TRUE:
2599 case COND_ELSE_FALSE:
2600 error_precond(ERR_WARNING|ERR_PASS1,
2601 "`%%else' after `%%else' ignored.");
2602 istk->conds->state = COND_NEVER;
2603 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002604 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002605 free_tlist(origline);
2606 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002607
H. Peter Anvine2c80182005-01-15 22:15:51 +00002608 case PP_ENDIF:
2609 if (tline->next)
H. Peter Anvin917a3492008-09-24 09:14:49 -07002610 error_precond(ERR_WARNING|ERR_PASS1,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002611 "trailing garbage after `%%endif' ignored");
H. Peter Anvine2c80182005-01-15 22:15:51 +00002612 if (!istk->conds)
2613 error(ERR_FATAL, "`%%endif': no matching `%%if'");
2614 cond = istk->conds;
2615 istk->conds = cond->next;
2616 nasm_free(cond);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002617 if(istk->mstk)
Keith Kanios3c0d91f2009-10-25 13:28:03 -05002618 istk->mstk->condcnt --;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002619 free_tlist(origline);
2620 return DIRECTIVE_FOUND;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002621
H. Peter Anvindb8f96e2009-07-15 09:07:29 -04002622 case PP_RMACRO:
2623 case PP_IRMACRO:
H. Peter Anvine2c80182005-01-15 22:15:51 +00002624 case PP_MACRO:
2625 case PP_IMACRO:
H. Peter Anvina26433d2008-07-16 14:40:01 -07002626 if (defining) {
H. Peter Anvindb8f96e2009-07-15 09:07:29 -04002627 error(ERR_FATAL, "`%s': already defining a macro",
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002628 pp_directives[i]);
2629 return DIRECTIVE_FOUND;
2630 }
2631 defining = nasm_malloc(sizeof(MMacro));
2632 defining->max_depth =
2633 (i == PP_RMACRO) || (i == PP_IRMACRO) ? DEADMAN_LIMIT : 0;
2634 defining->casesense = (i == PP_MACRO) || (i == PP_RMACRO);
2635 if (!parse_mmacro_spec(tline, defining, pp_directives[i])) {
2636 nasm_free(defining);
2637 defining = NULL;
2638 return DIRECTIVE_FOUND;
2639 }
H. Peter Anvina26433d2008-07-16 14:40:01 -07002640
H. Peter Anvin166c2472008-05-28 12:28:58 -07002641 mmac = (MMacro *) hash_findix(&mmacros, defining->name);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002642 while (mmac) {
2643 if (!strcmp(mmac->name, defining->name) &&
2644 (mmac->nparam_min <= defining->nparam_max
2645 || defining->plus)
2646 && (defining->nparam_min <= mmac->nparam_max
2647 || mmac->plus)) {
H. Peter Anvin917a3492008-09-24 09:14:49 -07002648 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +00002649 "redefining multi-line macro `%s'", defining->name);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002650 return DIRECTIVE_FOUND;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002651 }
2652 mmac = mmac->next;
2653 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002654 free_tlist(origline);
2655 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002656
H. Peter Anvine2c80182005-01-15 22:15:51 +00002657 case PP_ENDM:
2658 case PP_ENDMACRO:
Victor van den Elzen8f1120f2008-07-16 13:41:37 +02002659 if (! (defining && defining->name)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002660 error(ERR_NONFATAL, "`%s': not defining a macro", tline->text);
2661 return DIRECTIVE_FOUND;
2662 }
Keith Kanios852f1ee2009-07-12 00:19:55 -05002663 mmhead = (MMacro **) hash_findi_add(&mmacros, defining->name);
H. Peter Anvin97a23472007-09-16 17:57:25 -07002664 defining->next = *mmhead;
Keith Kanios852f1ee2009-07-12 00:19:55 -05002665 *mmhead = defining;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002666 defining = NULL;
2667 free_tlist(origline);
2668 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002669
H. Peter Anvin89cee572009-07-15 09:16:54 -04002670 case PP_EXITMACRO:
Keith Kanios852f1ee2009-07-12 00:19:55 -05002671 /*
2672 * We must search along istk->expansion until we hit a
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002673 * macro-end marker for a macro with a name. Then we
Keith Kanios3c0d91f2009-10-25 13:28:03 -05002674 * bypass all lines between exitmacro and endmacro.
Keith Kanios852f1ee2009-07-12 00:19:55 -05002675 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002676 for (l = istk->expansion; l; l = l->next)
Keith Kanios852f1ee2009-07-12 00:19:55 -05002677 if (l->finishes && l->finishes->name)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002678 break;
Keith Kanios852f1ee2009-07-12 00:19:55 -05002679
2680 if (l) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002681 /*
2682 * Remove all conditional entries relative to this
2683 * macro invocation. (safe to do in this context)
2684 */
Keith Kanios3c0d91f2009-10-25 13:28:03 -05002685 for ( ; l->finishes->condcnt > 0; l->finishes->condcnt --) {
2686 cond = istk->conds;
2687 istk->conds = cond->next;
2688 nasm_free(cond);
2689 }
2690 istk->expansion = l;
Keith Kanios852f1ee2009-07-12 00:19:55 -05002691 } else {
2692 error(ERR_NONFATAL, "`%%exitmacro' not within `%%macro' block");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002693 }
Keith Kanios852f1ee2009-07-12 00:19:55 -05002694 free_tlist(origline);
2695 return DIRECTIVE_FOUND;
2696
H. Peter Anvina26433d2008-07-16 14:40:01 -07002697 case PP_UNMACRO:
2698 case PP_UNIMACRO:
2699 {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002700 MMacro **mmac_p;
2701 MMacro spec;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002702
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002703 spec.casesense = (i == PP_UNMACRO);
2704 if (!parse_mmacro_spec(tline, &spec, pp_directives[i])) {
2705 return DIRECTIVE_FOUND;
2706 }
H. Peter Anvina26433d2008-07-16 14:40:01 -07002707 mmac_p = (MMacro **) hash_findi(&mmacros, spec.name, NULL);
2708 while (mmac_p && *mmac_p) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002709 mmac = *mmac_p;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002710 if (mmac->casesense == spec.casesense &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002711 !mstrcmp(mmac->name, spec.name, spec.casesense) &&
H. Peter Anvina26433d2008-07-16 14:40:01 -07002712 mmac->nparam_min == spec.nparam_min &&
2713 mmac->nparam_max == spec.nparam_max &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002714 mmac->plus == spec.plus) {
2715 *mmac_p = mmac->next;
2716 free_mmacro(mmac);
2717 } else {
2718 mmac_p = &mmac->next;
2719 }
2720 }
2721 free_tlist(origline);
2722 free_tlist(spec.dlist);
2723 return DIRECTIVE_FOUND;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002724 }
2725
H. Peter Anvine2c80182005-01-15 22:15:51 +00002726 case PP_ROTATE:
2727 if (tline->next && tline->next->type == TOK_WHITESPACE)
2728 tline = tline->next;
H. Peter Anvin89cee572009-07-15 09:16:54 -04002729 if (!tline->next) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002730 free_tlist(origline);
2731 error(ERR_NONFATAL, "`%%rotate' missing rotate count");
2732 return DIRECTIVE_FOUND;
2733 }
2734 t = expand_smacro(tline->next);
2735 tline->next = NULL;
2736 free_tlist(origline);
2737 tline = t;
2738 tptr = &t;
2739 tokval.t_type = TOKEN_INVALID;
2740 evalresult =
2741 evaluate(ppscan, tptr, &tokval, NULL, pass, error, NULL);
2742 free_tlist(tline);
2743 if (!evalresult)
2744 return DIRECTIVE_FOUND;
2745 if (tokval.t_type)
H. Peter Anvin917a3492008-09-24 09:14:49 -07002746 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +00002747 "trailing garbage after expression ignored");
2748 if (!is_simple(evalresult)) {
2749 error(ERR_NONFATAL, "non-constant value given to `%%rotate'");
2750 return DIRECTIVE_FOUND;
2751 }
2752 mmac = istk->mstk;
2753 while (mmac && !mmac->name) /* avoid mistaking %reps for macros */
2754 mmac = mmac->next_active;
2755 if (!mmac) {
2756 error(ERR_NONFATAL, "`%%rotate' invoked outside a macro call");
2757 } else if (mmac->nparam == 0) {
2758 error(ERR_NONFATAL,
2759 "`%%rotate' invoked within macro without parameters");
2760 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002761 int rotate = mmac->rotate + reloc_value(evalresult);
H. Peter Anvin734b1882002-04-30 21:01:08 +00002762
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002763 rotate %= (int)mmac->nparam;
2764 if (rotate < 0)
2765 rotate += mmac->nparam;
H. Peter Anvin70653092007-10-19 14:42:29 -07002766
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002767 mmac->rotate = rotate;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002768 }
2769 return DIRECTIVE_FOUND;
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00002770
H. Peter Anvine2c80182005-01-15 22:15:51 +00002771 case PP_REP:
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002772 nolist = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002773 do {
2774 tline = tline->next;
2775 } while (tok_type_(tline, TOK_WHITESPACE));
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00002776
H. Peter Anvine2c80182005-01-15 22:15:51 +00002777 if (tok_type_(tline, TOK_ID) &&
2778 nasm_stricmp(tline->text, ".nolist") == 0) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002779 nolist = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002780 do {
2781 tline = tline->next;
2782 } while (tok_type_(tline, TOK_WHITESPACE));
2783 }
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00002784
H. Peter Anvine2c80182005-01-15 22:15:51 +00002785 if (tline) {
2786 t = expand_smacro(tline);
2787 tptr = &t;
2788 tokval.t_type = TOKEN_INVALID;
2789 evalresult =
2790 evaluate(ppscan, tptr, &tokval, NULL, pass, error, NULL);
2791 if (!evalresult) {
2792 free_tlist(origline);
2793 return DIRECTIVE_FOUND;
2794 }
2795 if (tokval.t_type)
H. Peter Anvin917a3492008-09-24 09:14:49 -07002796 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +00002797 "trailing garbage after expression ignored");
2798 if (!is_simple(evalresult)) {
2799 error(ERR_NONFATAL, "non-constant value given to `%%rep'");
2800 return DIRECTIVE_FOUND;
2801 }
H. Peter Anvinf8ba53e2007-10-11 10:11:57 -07002802 count = reloc_value(evalresult) + 1;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002803 } else {
2804 error(ERR_NONFATAL, "`%%rep' expects a repeat count");
H. Peter Anvinf8ba53e2007-10-11 10:11:57 -07002805 count = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002806 }
2807 free_tlist(origline);
H. Peter Anvin734b1882002-04-30 21:01:08 +00002808
H. Peter Anvine2c80182005-01-15 22:15:51 +00002809 tmp_defining = defining;
2810 defining = nasm_malloc(sizeof(MMacro));
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002811 defining->prev = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002812 defining->name = NULL; /* flags this macro as a %rep block */
H. Peter Anvin70055962007-10-11 00:05:31 -07002813 defining->casesense = false;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002814 defining->plus = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002815 defining->nolist = nolist;
H. Peter Anvinf8ba53e2007-10-11 10:11:57 -07002816 defining->in_progress = count;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002817 defining->max_depth = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002818 defining->nparam_min = defining->nparam_max = 0;
2819 defining->defaults = NULL;
2820 defining->dlist = NULL;
2821 defining->expansion = NULL;
2822 defining->next_active = istk->mstk;
2823 defining->rep_nest = tmp_defining;
2824 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002825
H. Peter Anvine2c80182005-01-15 22:15:51 +00002826 case PP_ENDREP:
2827 if (!defining || defining->name) {
2828 error(ERR_NONFATAL, "`%%endrep': no matching `%%rep'");
2829 return DIRECTIVE_FOUND;
2830 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00002831
H. Peter Anvine2c80182005-01-15 22:15:51 +00002832 /*
2833 * Now we have a "macro" defined - although it has no name
2834 * and we won't be entering it in the hash tables - we must
2835 * push a macro-end marker for it on to istk->expansion.
2836 * After that, it will take care of propagating itself (a
2837 * macro-end marker line for a macro which is really a %rep
2838 * block will cause the macro to be re-expanded, complete
2839 * with another macro-end marker to ensure the process
2840 * continues) until the whole expansion is forcibly removed
2841 * from istk->expansion by a %exitrep.
2842 */
2843 l = nasm_malloc(sizeof(Line));
2844 l->next = istk->expansion;
2845 l->finishes = defining;
2846 l->first = NULL;
2847 istk->expansion = l;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002848
H. Peter Anvine2c80182005-01-15 22:15:51 +00002849 istk->mstk = defining;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002850
H. Peter Anvine2c80182005-01-15 22:15:51 +00002851 list->uplevel(defining->nolist ? LIST_MACRO_NOLIST : LIST_MACRO);
2852 tmp_defining = defining;
2853 defining = defining->rep_nest;
2854 free_tlist(origline);
2855 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002856
H. Peter Anvine2c80182005-01-15 22:15:51 +00002857 case PP_EXITREP:
2858 /*
2859 * We must search along istk->expansion until we hit a
2860 * macro-end marker for a macro with no name. Then we set
2861 * its `in_progress' flag to 0.
2862 */
2863 for (l = istk->expansion; l; l = l->next)
2864 if (l->finishes && !l->finishes->name)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002865 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002866
H. Peter Anvine2c80182005-01-15 22:15:51 +00002867 if (l)
Charles Crayned4200be2008-07-12 16:42:33 -07002868 l->finishes->in_progress = 1;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002869 else
2870 error(ERR_NONFATAL, "`%%exitrep' not within `%%rep' block");
2871 free_tlist(origline);
2872 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002873
H. Peter Anvine2c80182005-01-15 22:15:51 +00002874 case PP_XDEFINE:
2875 case PP_IXDEFINE:
2876 case PP_DEFINE:
2877 case PP_IDEFINE:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002878 casesense = (i == PP_DEFINE || i == PP_XDEFINE);
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07002879
H. Peter Anvine2c80182005-01-15 22:15:51 +00002880 tline = tline->next;
2881 skip_white_(tline);
2882 tline = expand_id(tline);
2883 if (!tline || (tline->type != TOK_ID &&
2884 (tline->type != TOK_PREPROC_ID ||
2885 tline->text[1] != '$'))) {
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07002886 error(ERR_NONFATAL, "`%s' expects a macro identifier",
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002887 pp_directives[i]);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002888 free_tlist(origline);
2889 return DIRECTIVE_FOUND;
2890 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00002891
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08002892 ctx = get_ctx(tline->text, &mname, false);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002893 last = tline;
2894 param_start = tline = tline->next;
2895 nparam = 0;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002896
H. Peter Anvine2c80182005-01-15 22:15:51 +00002897 /* Expand the macro definition now for %xdefine and %ixdefine */
2898 if ((i == PP_XDEFINE) || (i == PP_IXDEFINE))
2899 tline = expand_smacro(tline);
H. Peter Anvin734b1882002-04-30 21:01:08 +00002900
H. Peter Anvine2c80182005-01-15 22:15:51 +00002901 if (tok_is_(tline, "(")) {
2902 /*
2903 * This macro has parameters.
2904 */
H. Peter Anvin734b1882002-04-30 21:01:08 +00002905
H. Peter Anvine2c80182005-01-15 22:15:51 +00002906 tline = tline->next;
2907 while (1) {
2908 skip_white_(tline);
2909 if (!tline) {
2910 error(ERR_NONFATAL, "parameter identifier expected");
2911 free_tlist(origline);
2912 return DIRECTIVE_FOUND;
2913 }
2914 if (tline->type != TOK_ID) {
2915 error(ERR_NONFATAL,
2916 "`%s': parameter identifier expected",
2917 tline->text);
2918 free_tlist(origline);
2919 return DIRECTIVE_FOUND;
2920 }
2921 tline->type = TOK_SMAC_PARAM + nparam++;
2922 tline = tline->next;
2923 skip_white_(tline);
2924 if (tok_is_(tline, ",")) {
2925 tline = tline->next;
H. Peter Anvinca348b62008-07-23 10:49:26 -04002926 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002927 if (!tok_is_(tline, ")")) {
2928 error(ERR_NONFATAL,
2929 "`)' expected to terminate macro template");
2930 free_tlist(origline);
2931 return DIRECTIVE_FOUND;
2932 }
2933 break;
2934 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002935 }
2936 last = tline;
2937 tline = tline->next;
2938 }
2939 if (tok_type_(tline, TOK_WHITESPACE))
2940 last = tline, tline = tline->next;
2941 macro_start = NULL;
2942 last->next = NULL;
2943 t = tline;
2944 while (t) {
2945 if (t->type == TOK_ID) {
2946 for (tt = param_start; tt; tt = tt->next)
2947 if (tt->type >= TOK_SMAC_PARAM &&
2948 !strcmp(tt->text, t->text))
2949 t->type = tt->type;
2950 }
2951 tt = t->next;
2952 t->next = macro_start;
2953 macro_start = t;
2954 t = tt;
2955 }
2956 /*
2957 * Good. We now have a macro name, a parameter count, and a
2958 * token list (in reverse order) for an expansion. We ought
2959 * to be OK just to create an SMacro, store it, and let
2960 * free_tlist have the rest of the line (which we have
2961 * carefully re-terminated after chopping off the expansion
2962 * from the end).
2963 */
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002964 define_smacro(ctx, mname, casesense, nparam, macro_start);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002965 free_tlist(origline);
2966 return DIRECTIVE_FOUND;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002967
H. Peter Anvine2c80182005-01-15 22:15:51 +00002968 case PP_UNDEF:
2969 tline = tline->next;
2970 skip_white_(tline);
2971 tline = expand_id(tline);
2972 if (!tline || (tline->type != TOK_ID &&
2973 (tline->type != TOK_PREPROC_ID ||
2974 tline->text[1] != '$'))) {
2975 error(ERR_NONFATAL, "`%%undef' expects a macro identifier");
2976 free_tlist(origline);
2977 return DIRECTIVE_FOUND;
2978 }
2979 if (tline->next) {
H. Peter Anvin917a3492008-09-24 09:14:49 -07002980 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +00002981 "trailing garbage after macro name ignored");
2982 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002983
H. Peter Anvine2c80182005-01-15 22:15:51 +00002984 /* Find the context that symbol belongs to */
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08002985 ctx = get_ctx(tline->text, &mname, false);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002986 undef_smacro(ctx, mname);
2987 free_tlist(origline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002988 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002989
H. Peter Anvin9e200162008-06-04 17:23:14 -07002990 case PP_DEFSTR:
2991 case PP_IDEFSTR:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002992 casesense = (i == PP_DEFSTR);
H. Peter Anvin9e200162008-06-04 17:23:14 -07002993
2994 tline = tline->next;
2995 skip_white_(tline);
2996 tline = expand_id(tline);
2997 if (!tline || (tline->type != TOK_ID &&
2998 (tline->type != TOK_PREPROC_ID ||
2999 tline->text[1] != '$'))) {
3000 error(ERR_NONFATAL, "`%s' expects a macro identifier",
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003001 pp_directives[i]);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003002 free_tlist(origline);
3003 return DIRECTIVE_FOUND;
3004 }
3005
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08003006 ctx = get_ctx(tline->text, &mname, false);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003007 last = tline;
3008 tline = expand_smacro(tline->next);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003009 last->next = NULL;
H. Peter Anvin9e200162008-06-04 17:23:14 -07003010
3011 while (tok_type_(tline, TOK_WHITESPACE))
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003012 tline = delete_Token(tline);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003013
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003014 p = detoken(tline, false);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003015 macro_start = nasm_malloc(sizeof(*macro_start));
3016 macro_start->next = NULL;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003017 macro_start->text = nasm_quote(p, strlen(p));
3018 macro_start->type = TOK_STRING;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003019 macro_start->a.mac = NULL;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003020 nasm_free(p);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003021
3022 /*
3023 * We now have a macro name, an implicit parameter count of
3024 * zero, and a string token to use as an expansion. Create
3025 * and store an SMacro.
3026 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003027 define_smacro(ctx, mname, casesense, 0, macro_start);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003028 free_tlist(origline);
3029 return DIRECTIVE_FOUND;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003030
H. Peter Anvin2f55bda2009-07-14 15:04:04 -04003031 case PP_DEFTOK:
3032 case PP_IDEFTOK:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003033 casesense = (i == PP_DEFTOK);
3034
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003035 tline = tline->next;
3036 skip_white_(tline);
3037 tline = expand_id(tline);
3038 if (!tline || (tline->type != TOK_ID &&
3039 (tline->type != TOK_PREPROC_ID ||
3040 tline->text[1] != '$'))) {
3041 error(ERR_NONFATAL,
3042 "`%s' expects a macro identifier as first parameter",
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003043 pp_directives[i]);
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003044 free_tlist(origline);
3045 return DIRECTIVE_FOUND;
3046 }
3047 ctx = get_ctx(tline->text, &mname, false);
3048 last = tline;
3049 tline = expand_smacro(tline->next);
3050 last->next = NULL;
3051
3052 t = tline;
3053 while (tok_type_(t, TOK_WHITESPACE))
3054 t = t->next;
3055 /* t should now point to the string */
3056 if (t->type != TOK_STRING) {
3057 error(ERR_NONFATAL,
3058 "`%s` requires string as second parameter",
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003059 pp_directives[i]);
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003060 free_tlist(tline);
3061 free_tlist(origline);
3062 return DIRECTIVE_FOUND;
3063 }
3064
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003065 nasm_unquote_cstr(t->text, i);
3066 macro_start = tokenize(t->text);
3067
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003068 /*
3069 * We now have a macro name, an implicit parameter count of
3070 * zero, and a numeric token to use as an expansion. Create
3071 * and store an SMacro.
3072 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003073 define_smacro(ctx, mname, casesense, 0, macro_start);
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003074 free_tlist(tline);
3075 free_tlist(origline);
3076 return DIRECTIVE_FOUND;
H. Peter Anvin9e200162008-06-04 17:23:14 -07003077
H. Peter Anvin418ca702008-05-30 10:42:30 -07003078 case PP_PATHSEARCH:
3079 {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003080 FILE *fp;
3081 StrList *xsl = NULL;
3082 StrList **xst = &xsl;
H. Peter Anvin418ca702008-05-30 10:42:30 -07003083
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003084 casesense = true;
H. Peter Anvin418ca702008-05-30 10:42:30 -07003085
3086 tline = tline->next;
3087 skip_white_(tline);
3088 tline = expand_id(tline);
3089 if (!tline || (tline->type != TOK_ID &&
3090 (tline->type != TOK_PREPROC_ID ||
3091 tline->text[1] != '$'))) {
3092 error(ERR_NONFATAL,
3093 "`%%pathsearch' expects a macro identifier as first parameter");
3094 free_tlist(origline);
3095 return DIRECTIVE_FOUND;
3096 }
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08003097 ctx = get_ctx(tline->text, &mname, false);
H. Peter Anvin418ca702008-05-30 10:42:30 -07003098 last = tline;
3099 tline = expand_smacro(tline->next);
3100 last->next = NULL;
3101
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003102 t = tline;
H. Peter Anvin418ca702008-05-30 10:42:30 -07003103 while (tok_type_(t, TOK_WHITESPACE))
3104 t = t->next;
3105
3106 if (!t || (t->type != TOK_STRING &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003107 t->type != TOK_INTERNAL_STRING)) {
H. Peter Anvin418ca702008-05-30 10:42:30 -07003108 error(ERR_NONFATAL, "`%%pathsearch' expects a file name");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003109 free_tlist(tline);
H. Peter Anvin418ca702008-05-30 10:42:30 -07003110 free_tlist(origline);
3111 return DIRECTIVE_FOUND; /* but we did _something_ */
3112 }
3113 if (t->next)
H. Peter Anvin917a3492008-09-24 09:14:49 -07003114 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvin418ca702008-05-30 10:42:30 -07003115 "trailing garbage after `%%pathsearch' ignored");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003116 p = t->text;
H. Peter Anvin427cc912008-06-01 21:43:03 -07003117 if (t->type != TOK_INTERNAL_STRING)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003118 nasm_unquote(p, NULL);
H. Peter Anvin418ca702008-05-30 10:42:30 -07003119
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003120 fp = inc_fopen(p, &xsl, &xst, true);
3121 if (fp) {
3122 p = xsl->str;
3123 fclose(fp); /* Don't actually care about the file */
3124 }
H. Peter Anvin418ca702008-05-30 10:42:30 -07003125 macro_start = nasm_malloc(sizeof(*macro_start));
3126 macro_start->next = NULL;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003127 macro_start->text = nasm_quote(p, strlen(p));
3128 macro_start->type = TOK_STRING;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003129 macro_start->a.mac = NULL;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003130 if (xsl)
3131 nasm_free(xsl);
H. Peter Anvin418ca702008-05-30 10:42:30 -07003132
3133 /*
3134 * We now have a macro name, an implicit parameter count of
3135 * zero, and a string token to use as an expansion. Create
3136 * and store an SMacro.
3137 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003138 define_smacro(ctx, mname, casesense, 0, macro_start);
3139 free_tlist(tline);
H. Peter Anvin418ca702008-05-30 10:42:30 -07003140 free_tlist(origline);
3141 return DIRECTIVE_FOUND;
3142 }
3143
H. Peter Anvine2c80182005-01-15 22:15:51 +00003144 case PP_STRLEN:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003145 casesense = true;
H. Peter Anvin70653092007-10-19 14:42:29 -07003146
H. Peter Anvine2c80182005-01-15 22:15:51 +00003147 tline = tline->next;
3148 skip_white_(tline);
3149 tline = expand_id(tline);
3150 if (!tline || (tline->type != TOK_ID &&
3151 (tline->type != TOK_PREPROC_ID ||
3152 tline->text[1] != '$'))) {
3153 error(ERR_NONFATAL,
3154 "`%%strlen' expects a macro identifier as first parameter");
3155 free_tlist(origline);
3156 return DIRECTIVE_FOUND;
3157 }
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08003158 ctx = get_ctx(tline->text, &mname, false);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003159 last = tline;
3160 tline = expand_smacro(tline->next);
3161 last->next = NULL;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003162
H. Peter Anvine2c80182005-01-15 22:15:51 +00003163 t = tline;
3164 while (tok_type_(t, TOK_WHITESPACE))
3165 t = t->next;
3166 /* t should now point to the string */
3167 if (t->type != TOK_STRING) {
3168 error(ERR_NONFATAL,
3169 "`%%strlen` requires string as second parameter");
3170 free_tlist(tline);
3171 free_tlist(origline);
3172 return DIRECTIVE_FOUND;
3173 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003174
H. Peter Anvine2c80182005-01-15 22:15:51 +00003175 macro_start = nasm_malloc(sizeof(*macro_start));
3176 macro_start->next = NULL;
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07003177 make_tok_num(macro_start, nasm_unquote(t->text, NULL));
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003178 macro_start->a.mac = NULL;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003179
H. Peter Anvine2c80182005-01-15 22:15:51 +00003180 /*
3181 * We now have a macro name, an implicit parameter count of
3182 * zero, and a numeric token to use as an expansion. Create
3183 * and store an SMacro.
3184 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003185 define_smacro(ctx, mname, casesense, 0, macro_start);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003186 free_tlist(tline);
3187 free_tlist(origline);
3188 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003189
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003190 case PP_STRCAT:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003191 casesense = true;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003192
3193 tline = tline->next;
3194 skip_white_(tline);
3195 tline = expand_id(tline);
3196 if (!tline || (tline->type != TOK_ID &&
3197 (tline->type != TOK_PREPROC_ID ||
3198 tline->text[1] != '$'))) {
3199 error(ERR_NONFATAL,
3200 "`%%strcat' expects a macro identifier as first parameter");
3201 free_tlist(origline);
3202 return DIRECTIVE_FOUND;
3203 }
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08003204 ctx = get_ctx(tline->text, &mname, false);
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003205 last = tline;
3206 tline = expand_smacro(tline->next);
3207 last->next = NULL;
3208
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003209 len = 0;
3210 for (t = tline; t; t = t->next) {
3211 switch (t->type) {
3212 case TOK_WHITESPACE:
3213 break;
3214 case TOK_STRING:
3215 len += t->a.len = nasm_unquote(t->text, NULL);
3216 break;
3217 case TOK_OTHER:
3218 if (!strcmp(t->text, ",")) /* permit comma separators */
3219 break;
3220 /* else fall through */
3221 default:
3222 error(ERR_NONFATAL,
3223 "non-string passed to `%%strcat' (%d)", t->type);
3224 free_tlist(tline);
3225 free_tlist(origline);
3226 return DIRECTIVE_FOUND;
3227 }
3228 }
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003229
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003230 p = pp = nasm_malloc(len);
3231 for (t = tline; t; t = t->next) {
3232 if (t->type == TOK_STRING) {
3233 memcpy(p, t->text, t->a.len);
3234 p += t->a.len;
3235 }
3236 }
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003237
3238 /*
3239 * We now have a macro name, an implicit parameter count of
3240 * zero, and a numeric token to use as an expansion. Create
3241 * and store an SMacro.
3242 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003243 macro_start = new_Token(NULL, TOK_STRING, NULL, 0);
3244 macro_start->text = nasm_quote(pp, len);
3245 nasm_free(pp);
3246 define_smacro(ctx, mname, casesense, 0, macro_start);
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003247 free_tlist(tline);
3248 free_tlist(origline);
3249 return DIRECTIVE_FOUND;
3250
H. Peter Anvine2c80182005-01-15 22:15:51 +00003251 case PP_SUBSTR:
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003252 {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003253 int64_t a1, a2;
3254 size_t len;
H. Peter Anvind2456592008-06-19 15:04:18 -07003255
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003256 casesense = true;
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07003257
H. Peter Anvine2c80182005-01-15 22:15:51 +00003258 tline = tline->next;
3259 skip_white_(tline);
3260 tline = expand_id(tline);
3261 if (!tline || (tline->type != TOK_ID &&
3262 (tline->type != TOK_PREPROC_ID ||
3263 tline->text[1] != '$'))) {
3264 error(ERR_NONFATAL,
3265 "`%%substr' expects a macro identifier as first parameter");
3266 free_tlist(origline);
3267 return DIRECTIVE_FOUND;
3268 }
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08003269 ctx = get_ctx(tline->text, &mname, false);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003270 last = tline;
3271 tline = expand_smacro(tline->next);
3272 last->next = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003273
H. Peter Anvine2c80182005-01-15 22:15:51 +00003274 t = tline->next;
3275 while (tok_type_(t, TOK_WHITESPACE))
3276 t = t->next;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003277
H. Peter Anvine2c80182005-01-15 22:15:51 +00003278 /* t should now point to the string */
3279 if (t->type != TOK_STRING) {
3280 error(ERR_NONFATAL,
3281 "`%%substr` requires string as second parameter");
3282 free_tlist(tline);
3283 free_tlist(origline);
3284 return DIRECTIVE_FOUND;
3285 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003286
H. Peter Anvine2c80182005-01-15 22:15:51 +00003287 tt = t->next;
3288 tptr = &tt;
3289 tokval.t_type = TOKEN_INVALID;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003290 evalresult = evaluate(ppscan, tptr, &tokval, NULL,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003291 pass, error, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003292 if (!evalresult) {
3293 free_tlist(tline);
3294 free_tlist(origline);
3295 return DIRECTIVE_FOUND;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003296 } else if (!is_simple(evalresult)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00003297 error(ERR_NONFATAL, "non-constant value given to `%%substr`");
3298 free_tlist(tline);
3299 free_tlist(origline);
3300 return DIRECTIVE_FOUND;
3301 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003302 a1 = evalresult->value-1;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003303
3304 while (tok_type_(tt, TOK_WHITESPACE))
3305 tt = tt->next;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003306 if (!tt) {
3307 a2 = 1; /* Backwards compatibility: one character */
3308 } else {
3309 tokval.t_type = TOKEN_INVALID;
3310 evalresult = evaluate(ppscan, tptr, &tokval, NULL,
3311 pass, error, NULL);
3312 if (!evalresult) {
3313 free_tlist(tline);
3314 free_tlist(origline);
3315 return DIRECTIVE_FOUND;
3316 } else if (!is_simple(evalresult)) {
3317 error(ERR_NONFATAL, "non-constant value given to `%%substr`");
3318 free_tlist(tline);
3319 free_tlist(origline);
3320 return DIRECTIVE_FOUND;
3321 }
3322 a2 = evalresult->value;
3323 }
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003324
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003325 len = nasm_unquote(t->text, NULL);
3326 if (a2 < 0)
3327 a2 = a2+1+len-a1;
3328 if (a1+a2 > (int64_t)len)
3329 a2 = len-a1;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003330
H. Peter Anvine2c80182005-01-15 22:15:51 +00003331 macro_start = nasm_malloc(sizeof(*macro_start));
3332 macro_start->next = NULL;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003333 macro_start->text = nasm_quote((a1 < 0) ? "" : t->text+a1, a2);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003334 macro_start->type = TOK_STRING;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003335 macro_start->a.mac = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003336
H. Peter Anvine2c80182005-01-15 22:15:51 +00003337 /*
3338 * We now have a macro name, an implicit parameter count of
3339 * zero, and a numeric token to use as an expansion. Create
3340 * and store an SMacro.
3341 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003342 define_smacro(ctx, mname, casesense, 0, macro_start);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003343 free_tlist(tline);
3344 free_tlist(origline);
3345 return DIRECTIVE_FOUND;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003346 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003347
H. Peter Anvine2c80182005-01-15 22:15:51 +00003348 case PP_ASSIGN:
3349 case PP_IASSIGN:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003350 casesense = (i == PP_ASSIGN);
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07003351
H. Peter Anvine2c80182005-01-15 22:15:51 +00003352 tline = tline->next;
3353 skip_white_(tline);
3354 tline = expand_id(tline);
3355 if (!tline || (tline->type != TOK_ID &&
3356 (tline->type != TOK_PREPROC_ID ||
3357 tline->text[1] != '$'))) {
3358 error(ERR_NONFATAL,
3359 "`%%%sassign' expects a macro identifier",
3360 (i == PP_IASSIGN ? "i" : ""));
3361 free_tlist(origline);
3362 return DIRECTIVE_FOUND;
3363 }
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08003364 ctx = get_ctx(tline->text, &mname, false);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003365 last = tline;
3366 tline = expand_smacro(tline->next);
3367 last->next = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003368
H. Peter Anvine2c80182005-01-15 22:15:51 +00003369 t = tline;
3370 tptr = &t;
3371 tokval.t_type = TOKEN_INVALID;
3372 evalresult =
3373 evaluate(ppscan, tptr, &tokval, NULL, pass, error, NULL);
3374 free_tlist(tline);
3375 if (!evalresult) {
3376 free_tlist(origline);
3377 return DIRECTIVE_FOUND;
3378 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003379
H. Peter Anvine2c80182005-01-15 22:15:51 +00003380 if (tokval.t_type)
H. Peter Anvin917a3492008-09-24 09:14:49 -07003381 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +00003382 "trailing garbage after expression ignored");
H. Peter Anvin734b1882002-04-30 21:01:08 +00003383
H. Peter Anvine2c80182005-01-15 22:15:51 +00003384 if (!is_simple(evalresult)) {
3385 error(ERR_NONFATAL,
3386 "non-constant value given to `%%%sassign'",
3387 (i == PP_IASSIGN ? "i" : ""));
3388 free_tlist(origline);
3389 return DIRECTIVE_FOUND;
3390 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003391
H. Peter Anvine2c80182005-01-15 22:15:51 +00003392 macro_start = nasm_malloc(sizeof(*macro_start));
3393 macro_start->next = NULL;
3394 make_tok_num(macro_start, reloc_value(evalresult));
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003395 macro_start->a.mac = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003396
H. Peter Anvine2c80182005-01-15 22:15:51 +00003397 /*
3398 * We now have a macro name, an implicit parameter count of
3399 * zero, and a numeric token to use as an expansion. Create
3400 * and store an SMacro.
3401 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003402 define_smacro(ctx, mname, casesense, 0, macro_start);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003403 free_tlist(origline);
3404 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003405
H. Peter Anvine2c80182005-01-15 22:15:51 +00003406 case PP_LINE:
3407 /*
3408 * Syntax is `%line nnn[+mmm] [filename]'
3409 */
3410 tline = tline->next;
3411 skip_white_(tline);
3412 if (!tok_type_(tline, TOK_NUMBER)) {
3413 error(ERR_NONFATAL, "`%%line' expects line number");
3414 free_tlist(origline);
3415 return DIRECTIVE_FOUND;
3416 }
H. Peter Anvin70055962007-10-11 00:05:31 -07003417 k = readnum(tline->text, &err);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003418 m = 1;
3419 tline = tline->next;
3420 if (tok_is_(tline, "+")) {
3421 tline = tline->next;
3422 if (!tok_type_(tline, TOK_NUMBER)) {
3423 error(ERR_NONFATAL, "`%%line' expects line increment");
3424 free_tlist(origline);
3425 return DIRECTIVE_FOUND;
3426 }
H. Peter Anvin70055962007-10-11 00:05:31 -07003427 m = readnum(tline->text, &err);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003428 tline = tline->next;
3429 }
3430 skip_white_(tline);
3431 src_set_linnum(k);
3432 istk->lineinc = m;
3433 if (tline) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07003434 nasm_free(src_set_fname(detoken(tline, false)));
H. Peter Anvine2c80182005-01-15 22:15:51 +00003435 }
3436 free_tlist(origline);
3437 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003438
H. Peter Anvine2c80182005-01-15 22:15:51 +00003439 default:
3440 error(ERR_FATAL,
3441 "preprocessor directive `%s' not yet implemented",
H. Peter Anvin4169a472007-09-12 01:29:43 +00003442 pp_directives[i]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003443 return DIRECTIVE_FOUND;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003444 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003445}
3446
3447/*
H. Peter Anvin76690a12002-04-30 20:52:49 +00003448 * Ensure that a macro parameter contains a condition code and
3449 * nothing else. Return the condition code index if so, or -1
3450 * otherwise.
3451 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003452static int find_cc(Token * t)
H. Peter Anvineba20a72002-04-30 20:53:55 +00003453{
H. Peter Anvin76690a12002-04-30 20:52:49 +00003454 Token *tt;
3455 int i, j, k, m;
3456
H. Peter Anvin25a99342007-09-22 17:45:45 -07003457 if (!t)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003458 return -1; /* Probably a %+ without a space */
H. Peter Anvin25a99342007-09-22 17:45:45 -07003459
H. Peter Anvineba20a72002-04-30 20:53:55 +00003460 skip_white_(t);
H. Peter Anvin76690a12002-04-30 20:52:49 +00003461 if (t->type != TOK_ID)
H. Peter Anvine2c80182005-01-15 22:15:51 +00003462 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003463 tt = t->next;
H. Peter Anvineba20a72002-04-30 20:53:55 +00003464 skip_white_(tt);
H. Peter Anvin76690a12002-04-30 20:52:49 +00003465 if (tt && (tt->type != TOK_OTHER || strcmp(tt->text, ",")))
H. Peter Anvine2c80182005-01-15 22:15:51 +00003466 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003467
3468 i = -1;
Ed Beroset3ab3f412002-06-11 03:31:49 +00003469 j = elements(conditions);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003470 while (j - i > 1) {
3471 k = (j + i) / 2;
3472 m = nasm_stricmp(t->text, conditions[k]);
3473 if (m == 0) {
3474 i = k;
3475 j = -2;
3476 break;
3477 } else if (m < 0) {
3478 j = k;
3479 } else
3480 i = k;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003481 }
3482 if (j != -2)
H. Peter Anvine2c80182005-01-15 22:15:51 +00003483 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003484 return i;
3485}
3486
H. Peter Anvind784a082009-04-20 14:01:18 -07003487static bool paste_tokens(Token **head, bool handle_paste_tokens)
3488{
3489 Token **tail, *t, *tt;
3490 Token **paste_head;
3491 bool did_paste = false;
3492 char *tmp;
3493
3494 /* Now handle token pasting... */
3495 paste_head = NULL;
3496 tail = head;
3497 while ((t = *tail) && (tt = t->next)) {
3498 switch (t->type) {
3499 case TOK_WHITESPACE:
3500 if (tt->type == TOK_WHITESPACE) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003501 /* Zap adjacent whitespace tokens */
H. Peter Anvind784a082009-04-20 14:01:18 -07003502 t->next = delete_Token(tt);
3503 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003504 /* Do not advance paste_head here */
3505 tail = &t->next;
3506 }
H. Peter Anvind784a082009-04-20 14:01:18 -07003507 break;
3508 case TOK_ID:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003509 case TOK_PREPROC_ID:
H. Peter Anvind784a082009-04-20 14:01:18 -07003510 case TOK_NUMBER:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003511 case TOK_FLOAT:
3512 {
3513 size_t len = 0;
3514 char *tmp, *p;
H. Peter Anvind784a082009-04-20 14:01:18 -07003515
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003516 while (tt && (tt->type == TOK_ID || tt->type == TOK_PREPROC_ID ||
3517 tt->type == TOK_NUMBER || tt->type == TOK_FLOAT ||
3518 tt->type == TOK_OTHER)) {
3519 len += strlen(tt->text);
3520 tt = tt->next;
3521 }
H. Peter Anvind784a082009-04-20 14:01:18 -07003522
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003523 /*
3524 * Now tt points to the first token after
3525 * the potential paste area...
3526 */
3527 if (tt != t->next) {
3528 /* We have at least two tokens... */
3529 len += strlen(t->text);
3530 p = tmp = nasm_malloc(len+1);
H. Peter Anvind784a082009-04-20 14:01:18 -07003531
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003532 while (t != tt) {
3533 strcpy(p, t->text);
3534 p = strchr(p, '\0');
3535 t = delete_Token(t);
3536 }
H. Peter Anvind784a082009-04-20 14:01:18 -07003537
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003538 t = *tail = tokenize(tmp);
3539 nasm_free(tmp);
H. Peter Anvind784a082009-04-20 14:01:18 -07003540
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003541 while (t->next) {
3542 tail = &t->next;
3543 t = t->next;
3544 }
3545 t->next = tt; /* Attach the remaining token chain */
H. Peter Anvind784a082009-04-20 14:01:18 -07003546
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003547 did_paste = true;
3548 }
3549 paste_head = tail;
3550 tail = &t->next;
3551 break;
3552 }
3553 case TOK_PASTE: /* %+ */
3554 if (handle_paste_tokens) {
3555 /* Zap %+ and whitespace tokens to the right */
3556 while (t && (t->type == TOK_WHITESPACE ||
3557 t->type == TOK_PASTE))
3558 t = *tail = delete_Token(t);
3559 if (!paste_head || !t)
3560 break; /* Nothing to paste with */
3561 tail = paste_head;
3562 t = *tail;
3563 tt = t->next;
3564 while (tok_type_(tt, TOK_WHITESPACE))
3565 tt = t->next = delete_Token(tt);
H. Peter Anvind784a082009-04-20 14:01:18 -07003566
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003567 if (tt) {
3568 tmp = nasm_strcat(t->text, tt->text);
3569 delete_Token(t);
3570 tt = delete_Token(tt);
3571 t = *tail = tokenize(tmp);
3572 nasm_free(tmp);
3573 while (t->next) {
3574 tail = &t->next;
3575 t = t->next;
3576 }
3577 t->next = tt; /* Attach the remaining token chain */
3578 did_paste = true;
3579 }
3580 paste_head = tail;
3581 tail = &t->next;
3582 break;
3583 }
3584 /* else fall through */
3585 default:
3586 tail = paste_head = &t->next;
3587 break;
H. Peter Anvind784a082009-04-20 14:01:18 -07003588 }
3589 }
3590 return did_paste;
3591}
H. Peter Anvin76690a12002-04-30 20:52:49 +00003592/*
3593 * Expand MMacro-local things: parameter references (%0, %n, %+n,
H. Peter Anvin67c63722008-10-26 23:49:00 -07003594 * %-n) and MMacro-local identifiers (%%foo) as well as
3595 * macro indirection (%[...]).
H. Peter Anvin76690a12002-04-30 20:52:49 +00003596 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003597static Token *expand_mmac_params(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00003598{
H. Peter Anvin734b1882002-04-30 21:01:08 +00003599 Token *t, *tt, **tail, *thead;
H. Peter Anvin6125b622009-04-08 14:02:25 -07003600 bool changed = false;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003601
3602 tail = &thead;
3603 thead = NULL;
3604
H. Peter Anvine2c80182005-01-15 22:15:51 +00003605 while (tline) {
3606 if (tline->type == TOK_PREPROC_ID &&
3607 (((tline->text[1] == '+' || tline->text[1] == '-')
3608 && tline->text[2]) || tline->text[1] == '%'
3609 || (tline->text[1] >= '0' && tline->text[1] <= '9'))) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00003610 char *text = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003611 int type = 0, cc; /* type = 0 to placate optimisers */
Keith Kaniosa6dfa782007-04-13 16:47:53 +00003612 char tmpbuf[30];
H. Peter Anvin25a99342007-09-22 17:45:45 -07003613 unsigned int n;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003614 int i;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003615 MMacro *mac;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003616
H. Peter Anvine2c80182005-01-15 22:15:51 +00003617 t = tline;
3618 tline = tline->next;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003619
H. Peter Anvine2c80182005-01-15 22:15:51 +00003620 mac = istk->mstk;
3621 while (mac && !mac->name) /* avoid mistaking %reps for macros */
3622 mac = mac->next_active;
3623 if (!mac)
3624 error(ERR_NONFATAL, "`%s': not in a macro call", t->text);
3625 else
3626 switch (t->text[1]) {
3627 /*
3628 * We have to make a substitution of one of the
3629 * forms %1, %-1, %+1, %%foo, %0.
3630 */
3631 case '0':
3632 type = TOK_NUMBER;
3633 snprintf(tmpbuf, sizeof(tmpbuf), "%d", mac->nparam);
3634 text = nasm_strdup(tmpbuf);
3635 break;
3636 case '%':
3637 type = TOK_ID;
H. Peter Anvinf8ba53e2007-10-11 10:11:57 -07003638 snprintf(tmpbuf, sizeof(tmpbuf), "..@%"PRIu64".",
H. Peter Anvine2c80182005-01-15 22:15:51 +00003639 mac->unique);
3640 text = nasm_strcat(tmpbuf, t->text + 2);
3641 break;
3642 case '-':
3643 n = atoi(t->text + 2) - 1;
3644 if (n >= mac->nparam)
3645 tt = NULL;
3646 else {
3647 if (mac->nparam > 1)
3648 n = (n + mac->rotate) % mac->nparam;
3649 tt = mac->params[n];
3650 }
3651 cc = find_cc(tt);
3652 if (cc == -1) {
3653 error(ERR_NONFATAL,
3654 "macro parameter %d is not a condition code",
3655 n + 1);
3656 text = NULL;
3657 } else {
3658 type = TOK_ID;
3659 if (inverse_ccs[cc] == -1) {
3660 error(ERR_NONFATAL,
3661 "condition code `%s' is not invertible",
3662 conditions[cc]);
3663 text = NULL;
3664 } else
H. Peter Anvin67c63722008-10-26 23:49:00 -07003665 text = nasm_strdup(conditions[inverse_ccs[cc]]);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003666 }
3667 break;
3668 case '+':
3669 n = atoi(t->text + 2) - 1;
3670 if (n >= mac->nparam)
3671 tt = NULL;
3672 else {
3673 if (mac->nparam > 1)
3674 n = (n + mac->rotate) % mac->nparam;
3675 tt = mac->params[n];
3676 }
3677 cc = find_cc(tt);
3678 if (cc == -1) {
3679 error(ERR_NONFATAL,
3680 "macro parameter %d is not a condition code",
3681 n + 1);
3682 text = NULL;
3683 } else {
3684 type = TOK_ID;
3685 text = nasm_strdup(conditions[cc]);
3686 }
3687 break;
3688 default:
3689 n = atoi(t->text + 1) - 1;
3690 if (n >= mac->nparam)
3691 tt = NULL;
3692 else {
3693 if (mac->nparam > 1)
3694 n = (n + mac->rotate) % mac->nparam;
3695 tt = mac->params[n];
3696 }
3697 if (tt) {
3698 for (i = 0; i < mac->paramlen[n]; i++) {
3699 *tail = new_Token(NULL, tt->type, tt->text, 0);
3700 tail = &(*tail)->next;
3701 tt = tt->next;
3702 }
3703 }
3704 text = NULL; /* we've done it here */
3705 break;
3706 }
3707 if (!text) {
3708 delete_Token(t);
3709 } else {
3710 *tail = t;
3711 tail = &t->next;
3712 t->type = type;
3713 nasm_free(t->text);
3714 t->text = text;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003715 t->a.mac = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003716 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003717 changed = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003718 continue;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003719 } else if (tline->type == TOK_INDIRECT) {
3720 t = tline;
3721 tline = tline->next;
3722 tt = tokenize(t->text);
3723 tt = expand_mmac_params(tt);
3724 tt = expand_smacro(tt);
3725 *tail = tt;
3726 while (tt) {
3727 tt->a.mac = NULL; /* Necessary? */
3728 tail = &tt->next;
3729 tt = tt->next;
3730 }
3731 delete_Token(t);
3732 changed = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003733 } else {
3734 t = *tail = tline;
3735 tline = tline->next;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003736 t->a.mac = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003737 tail = &t->next;
3738 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00003739 }
H. Peter Anvineba20a72002-04-30 20:53:55 +00003740 *tail = NULL;
H. Peter Anvin67c63722008-10-26 23:49:00 -07003741
H. Peter Anvind784a082009-04-20 14:01:18 -07003742 if (changed)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003743 paste_tokens(&thead, false);
H. Peter Anvin6125b622009-04-08 14:02:25 -07003744
H. Peter Anvin76690a12002-04-30 20:52:49 +00003745 return thead;
3746}
3747
3748/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003749 * Expand all single-line macro calls made in the given line.
3750 * Return the expanded version of the line. The original is deemed
3751 * to be destroyed in the process. (In reality we'll just move
3752 * Tokens from input to output a lot of the time, rather than
3753 * actually bothering to destroy and replicate.)
3754 */
H. Peter Anvincb1cf592007-11-19 12:26:50 -08003755
H. Peter Anvine2c80182005-01-15 22:15:51 +00003756static Token *expand_smacro(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00003757{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003758 Token *t, *tt, *mstart, **tail, *thead;
H. Peter Anvineba20a72002-04-30 20:53:55 +00003759 SMacro *head = NULL, *m;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003760 Token **params;
3761 int *paramsize;
H. Peter Anvin25a99342007-09-22 17:45:45 -07003762 unsigned int nparam, sparam;
H. Peter Anvind784a082009-04-20 14:01:18 -07003763 int brackets;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003764 Token *org_tline = tline;
3765 Context *ctx;
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08003766 const char *mname;
H. Peter Anvin2a15e692007-11-19 13:14:59 -08003767 int deadman = DEADMAN_LIMIT;
H. Peter Anvin8287daf2009-07-07 16:00:58 -07003768 bool expanded;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003769
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003770 /*
3771 * Trick: we should avoid changing the start token pointer since it can
3772 * be contained in "next" field of other token. Because of this
3773 * we allocate a copy of first token and work with it; at the end of
3774 * routine we copy it back
3775 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003776 if (org_tline) {
3777 tline =
3778 new_Token(org_tline->next, org_tline->type, org_tline->text,
3779 0);
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003780 tline->a.mac = org_tline->a.mac;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003781 nasm_free(org_tline->text);
3782 org_tline->text = NULL;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003783 }
3784
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003785 expanded = true; /* Always expand %+ at least once */
H. Peter Anvin8287daf2009-07-07 16:00:58 -07003786
H. Peter Anvincb1cf592007-11-19 12:26:50 -08003787again:
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003788 tail = &thead;
3789 thead = NULL;
3790
H. Peter Anvine2c80182005-01-15 22:15:51 +00003791 while (tline) { /* main token loop */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003792 if (!--deadman) {
3793 error(ERR_NONFATAL, "interminable macro recursion");
Cyrill Gorcunovbd38c8f2009-11-21 11:11:23 +03003794 goto err;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003795 }
H. Peter Anvincb1cf592007-11-19 12:26:50 -08003796
H. Peter Anvine2c80182005-01-15 22:15:51 +00003797 if ((mname = tline->text)) {
3798 /* if this token is a local macro, look in local context */
Cyrill Gorcunovc56d9ad2010-02-11 15:12:19 +03003799 if (tline->type == TOK_ID) {
3800 head = (SMacro *)hash_findix(&smacros, mname);
3801 } else if (tline->type == TOK_PREPROC_ID) {
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08003802 ctx = get_ctx(mname, &mname, true);
Cyrill Gorcunovc56d9ad2010-02-11 15:12:19 +03003803 head = ctx ? (SMacro *)hash_findix(&ctx->localmac, mname) : NULL;
3804 } else
3805 head = NULL;
H. Peter Anvin072771e2008-05-22 13:17:51 -07003806
H. Peter Anvine2c80182005-01-15 22:15:51 +00003807 /*
3808 * We've hit an identifier. As in is_mmacro below, we first
3809 * check whether the identifier is a single-line macro at
3810 * all, then think about checking for parameters if
3811 * necessary.
3812 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003813 for (m = head; m; m = m->next)
3814 if (!mstrcmp(m->name, mname, m->casesense))
3815 break;
3816 if (m) {
3817 mstart = tline;
3818 params = NULL;
3819 paramsize = NULL;
3820 if (m->nparam == 0) {
3821 /*
3822 * Simple case: the macro is parameterless. Discard the
3823 * one token that the macro call took, and push the
3824 * expansion back on the to-do stack.
3825 */
3826 if (!m->expansion) {
3827 if (!strcmp("__FILE__", m->name)) {
3828 int32_t num = 0;
3829 char *file = NULL;
3830 src_get(&num, &file);
3831 tline->text = nasm_quote(file, strlen(file));
3832 tline->type = TOK_STRING;
3833 nasm_free(file);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003834 continue;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003835 }
3836 if (!strcmp("__LINE__", m->name)) {
3837 nasm_free(tline->text);
3838 make_tok_num(tline, src_get_linnum());
3839 continue;
3840 }
3841 if (!strcmp("__BITS__", m->name)) {
3842 nasm_free(tline->text);
3843 make_tok_num(tline, globalbits);
3844 continue;
3845 }
3846 tline = delete_Token(tline);
3847 continue;
3848 }
3849 } else {
3850 /*
3851 * Complicated case: at least one macro with this name
H. Peter Anvine2c80182005-01-15 22:15:51 +00003852 * exists and takes parameters. We must find the
3853 * parameters in the call, count them, find the SMacro
3854 * that corresponds to that form of the macro call, and
3855 * substitute for the parameters when we expand. What a
3856 * pain.
3857 */
3858 /*tline = tline->next;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003859 skip_white_(tline); */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003860 do {
3861 t = tline->next;
3862 while (tok_type_(t, TOK_SMAC_END)) {
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003863 t->a.mac->in_progress = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003864 t->text = NULL;
3865 t = tline->next = delete_Token(t);
3866 }
3867 tline = t;
3868 } while (tok_type_(tline, TOK_WHITESPACE));
3869 if (!tok_is_(tline, "(")) {
3870 /*
3871 * This macro wasn't called with parameters: ignore
3872 * the call. (Behaviour borrowed from gnu cpp.)
3873 */
3874 tline = mstart;
3875 m = NULL;
3876 } else {
3877 int paren = 0;
3878 int white = 0;
3879 brackets = 0;
3880 nparam = 0;
3881 sparam = PARAM_DELTA;
3882 params = nasm_malloc(sparam * sizeof(Token *));
3883 params[0] = tline->next;
3884 paramsize = nasm_malloc(sparam * sizeof(int));
3885 paramsize[0] = 0;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07003886 while (true) { /* parameter loop */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003887 /*
3888 * For some unusual expansions
3889 * which concatenates function call
3890 */
3891 t = tline->next;
3892 while (tok_type_(t, TOK_SMAC_END)) {
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003893 t->a.mac->in_progress = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003894 t->text = NULL;
3895 t = tline->next = delete_Token(t);
3896 }
3897 tline = t;
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00003898
H. Peter Anvine2c80182005-01-15 22:15:51 +00003899 if (!tline) {
3900 error(ERR_NONFATAL,
3901 "macro call expects terminating `)'");
3902 break;
3903 }
3904 if (tline->type == TOK_WHITESPACE
3905 && brackets <= 0) {
3906 if (paramsize[nparam])
3907 white++;
3908 else
3909 params[nparam] = tline->next;
3910 continue; /* parameter loop */
3911 }
3912 if (tline->type == TOK_OTHER
3913 && tline->text[1] == 0) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00003914 char ch = tline->text[0];
H. Peter Anvine2c80182005-01-15 22:15:51 +00003915 if (ch == ',' && !paren && brackets <= 0) {
3916 if (++nparam >= sparam) {
3917 sparam += PARAM_DELTA;
3918 params = nasm_realloc(params,
3919 sparam *
3920 sizeof(Token
3921 *));
3922 paramsize =
3923 nasm_realloc(paramsize,
3924 sparam *
3925 sizeof(int));
3926 }
3927 params[nparam] = tline->next;
3928 paramsize[nparam] = 0;
3929 white = 0;
3930 continue; /* parameter loop */
3931 }
3932 if (ch == '{' &&
3933 (brackets > 0 || (brackets == 0 &&
3934 !paramsize[nparam])))
3935 {
3936 if (!(brackets++)) {
3937 params[nparam] = tline->next;
3938 continue; /* parameter loop */
3939 }
3940 }
3941 if (ch == '}' && brackets > 0)
3942 if (--brackets == 0) {
3943 brackets = -1;
3944 continue; /* parameter loop */
3945 }
3946 if (ch == '(' && !brackets)
3947 paren++;
3948 if (ch == ')' && brackets <= 0)
3949 if (--paren < 0)
3950 break;
3951 }
3952 if (brackets < 0) {
3953 brackets = 0;
3954 error(ERR_NONFATAL, "braces do not "
3955 "enclose all of macro parameter");
3956 }
3957 paramsize[nparam] += white + 1;
3958 white = 0;
3959 } /* parameter loop */
3960 nparam++;
3961 while (m && (m->nparam != nparam ||
3962 mstrcmp(m->name, mname,
3963 m->casesense)))
3964 m = m->next;
3965 if (!m)
H. Peter Anvin917a3492008-09-24 09:14:49 -07003966 error(ERR_WARNING|ERR_PASS1|ERR_WARN_MNP,
H. Peter Anvine2c80182005-01-15 22:15:51 +00003967 "macro `%s' exists, "
3968 "but not taking %d parameters",
3969 mstart->text, nparam);
3970 }
3971 }
3972 if (m && m->in_progress)
3973 m = NULL;
3974 if (!m) { /* in progess or didn't find '(' or wrong nparam */
H. Peter Anvin70653092007-10-19 14:42:29 -07003975 /*
H. Peter Anvine2c80182005-01-15 22:15:51 +00003976 * Design question: should we handle !tline, which
3977 * indicates missing ')' here, or expand those
3978 * macros anyway, which requires the (t) test a few
H. Peter Anvin70653092007-10-19 14:42:29 -07003979 * lines down?
H. Peter Anvine2c80182005-01-15 22:15:51 +00003980 */
3981 nasm_free(params);
3982 nasm_free(paramsize);
3983 tline = mstart;
3984 } else {
3985 /*
3986 * Expand the macro: we are placed on the last token of the
3987 * call, so that we can easily split the call from the
3988 * following tokens. We also start by pushing an SMAC_END
3989 * token for the cycle removal.
3990 */
3991 t = tline;
3992 if (t) {
3993 tline = t->next;
3994 t->next = NULL;
3995 }
3996 tt = new_Token(tline, TOK_SMAC_END, NULL, 0);
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003997 tt->a.mac = m;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07003998 m->in_progress = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003999 tline = tt;
4000 for (t = m->expansion; t; t = t->next) {
4001 if (t->type >= TOK_SMAC_PARAM) {
4002 Token *pcopy = tline, **ptail = &pcopy;
4003 Token *ttt, *pt;
4004 int i;
H. Peter Anvin734b1882002-04-30 21:01:08 +00004005
H. Peter Anvine2c80182005-01-15 22:15:51 +00004006 ttt = params[t->type - TOK_SMAC_PARAM];
4007 for (i = paramsize[t->type - TOK_SMAC_PARAM];
4008 --i >= 0;) {
4009 pt = *ptail =
4010 new_Token(tline, ttt->type, ttt->text,
4011 0);
4012 ptail = &pt->next;
4013 ttt = ttt->next;
4014 }
4015 tline = pcopy;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004016 } else if (t->type == TOK_PREPROC_Q) {
4017 tt = new_Token(tline, TOK_ID, mname, 0);
4018 tline = tt;
4019 } else if (t->type == TOK_PREPROC_QQ) {
4020 tt = new_Token(tline, TOK_ID, m->name, 0);
4021 tline = tt;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004022 } else {
4023 tt = new_Token(tline, t->type, t->text, 0);
4024 tline = tt;
4025 }
4026 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00004027
H. Peter Anvine2c80182005-01-15 22:15:51 +00004028 /*
4029 * Having done that, get rid of the macro call, and clean
4030 * up the parameters.
4031 */
4032 nasm_free(params);
4033 nasm_free(paramsize);
4034 free_tlist(mstart);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004035 expanded = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004036 continue; /* main token loop */
4037 }
4038 }
4039 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004040
H. Peter Anvine2c80182005-01-15 22:15:51 +00004041 if (tline->type == TOK_SMAC_END) {
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004042 tline->a.mac->in_progress = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004043 tline = delete_Token(tline);
4044 } else {
4045 t = *tail = tline;
4046 tline = tline->next;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004047 t->a.mac = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004048 t->next = NULL;
4049 tail = &t->next;
4050 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004051 }
4052
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004053 /*
4054 * Now scan the entire line and look for successive TOK_IDs that resulted
Keith Kaniosb7a89542007-04-12 02:40:54 +00004055 * after expansion (they can't be produced by tokenize()). The successive
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004056 * TOK_IDs should be concatenated.
4057 * Also we look for %+ tokens and concatenate the tokens before and after
4058 * them (without white spaces in between).
4059 */
H. Peter Anvin8287daf2009-07-07 16:00:58 -07004060 if (expanded && paste_tokens(&thead, true)) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004061 /*
4062 * If we concatenated something, *and* we had previously expanded
4063 * an actual macro, scan the lines again for macros...
4064 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004065 tline = thead;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004066 expanded = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004067 goto again;
H. Peter Anvin734b1882002-04-30 21:01:08 +00004068 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004069
Cyrill Gorcunovbd38c8f2009-11-21 11:11:23 +03004070err:
H. Peter Anvine2c80182005-01-15 22:15:51 +00004071 if (org_tline) {
4072 if (thead) {
4073 *org_tline = *thead;
4074 /* since we just gave text to org_line, don't free it */
4075 thead->text = NULL;
4076 delete_Token(thead);
4077 } else {
4078 /* the expression expanded to empty line;
4079 we can't return NULL for some reasons
4080 we just set the line to a single WHITESPACE token. */
4081 memset(org_tline, 0, sizeof(*org_tline));
4082 org_tline->text = NULL;
4083 org_tline->type = TOK_WHITESPACE;
4084 }
4085 thead = org_tline;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004086 }
4087
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004088 return thead;
4089}
4090
4091/*
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004092 * Similar to expand_smacro but used exclusively with macro identifiers
4093 * right before they are fetched in. The reason is that there can be
4094 * identifiers consisting of several subparts. We consider that if there
4095 * are more than one element forming the name, user wants a expansion,
4096 * otherwise it will be left as-is. Example:
4097 *
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004098 * %define %$abc cde
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004099 *
4100 * the identifier %$abc will be left as-is so that the handler for %define
4101 * will suck it and define the corresponding value. Other case:
4102 *
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004103 * %define _%$abc cde
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004104 *
4105 * In this case user wants name to be expanded *before* %define starts
4106 * working, so we'll expand %$abc into something (if it has a value;
4107 * otherwise it will be left as-is) then concatenate all successive
4108 * PP_IDs into one.
4109 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004110static Token *expand_id(Token * tline)
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004111{
4112 Token *cur, *oldnext = NULL;
4113
H. Peter Anvin734b1882002-04-30 21:01:08 +00004114 if (!tline || !tline->next)
H. Peter Anvine2c80182005-01-15 22:15:51 +00004115 return tline;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004116
4117 cur = tline;
4118 while (cur->next &&
H. Peter Anvine2c80182005-01-15 22:15:51 +00004119 (cur->next->type == TOK_ID ||
4120 cur->next->type == TOK_PREPROC_ID
4121 || cur->next->type == TOK_NUMBER))
4122 cur = cur->next;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004123
4124 /* If identifier consists of just one token, don't expand */
4125 if (cur == tline)
H. Peter Anvine2c80182005-01-15 22:15:51 +00004126 return tline;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004127
H. Peter Anvine2c80182005-01-15 22:15:51 +00004128 if (cur) {
4129 oldnext = cur->next; /* Detach the tail past identifier */
4130 cur->next = NULL; /* so that expand_smacro stops here */
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004131 }
4132
H. Peter Anvin734b1882002-04-30 21:01:08 +00004133 tline = expand_smacro(tline);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004134
H. Peter Anvine2c80182005-01-15 22:15:51 +00004135 if (cur) {
4136 /* expand_smacro possibly changhed tline; re-scan for EOL */
4137 cur = tline;
4138 while (cur && cur->next)
4139 cur = cur->next;
4140 if (cur)
4141 cur->next = oldnext;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004142 }
4143
4144 return tline;
4145}
4146
4147/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004148 * Determine whether the given line constitutes a multi-line macro
4149 * call, and return the MMacro structure called if so. Doesn't have
4150 * to check for an initial label - that's taken care of in
4151 * expand_mmacro - but must check numbers of parameters. Guaranteed
4152 * to be called with tline->type == TOK_ID, so the putative macro
4153 * name is easy to find.
4154 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004155static MMacro *is_mmacro(Token * tline, Token *** params_array)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004156{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004157 MMacro *head, *m;
4158 Token **params;
4159 int nparam;
4160
H. Peter Anvin166c2472008-05-28 12:28:58 -07004161 head = (MMacro *) hash_findix(&mmacros, tline->text);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004162
4163 /*
4164 * Efficiency: first we see if any macro exists with the given
4165 * name. If not, we can return NULL immediately. _Then_ we
4166 * count the parameters, and then we look further along the
4167 * list if necessary to find the proper MMacro.
4168 */
4169 for (m = head; m; m = m->next)
H. Peter Anvine2c80182005-01-15 22:15:51 +00004170 if (!mstrcmp(m->name, tline->text, m->casesense))
4171 break;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004172 if (!m)
H. Peter Anvine2c80182005-01-15 22:15:51 +00004173 return NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004174
4175 /*
4176 * OK, we have a potential macro. Count and demarcate the
4177 * parameters.
4178 */
H. Peter Anvin734b1882002-04-30 21:01:08 +00004179 count_mmac_params(tline->next, &nparam, &params);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004180
4181 /*
4182 * So we know how many parameters we've got. Find the MMacro
4183 * structure that handles this number.
4184 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004185 while (m) {
4186 if (m->nparam_min <= nparam
4187 && (m->plus || nparam <= m->nparam_max)) {
4188 /*
4189 * This one is right. Just check if cycle removal
4190 * prohibits us using it before we actually celebrate...
4191 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004192 if (m->in_progress > m->max_depth) {
4193 if (m->max_depth > 0) {
4194 error(ERR_WARNING,
4195 "reached maximum recursion depth of %i",
4196 m->max_depth);
4197 }
4198 nasm_free(params);
4199 return NULL;
4200 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00004201 /*
4202 * It's right, and we can use it. Add its default
4203 * parameters to the end of our list if necessary.
4204 */
4205 if (m->defaults && nparam < m->nparam_min + m->ndefs) {
4206 params =
4207 nasm_realloc(params,
4208 ((m->nparam_min + m->ndefs +
4209 1) * sizeof(*params)));
4210 while (nparam < m->nparam_min + m->ndefs) {
4211 params[nparam] = m->defaults[nparam - m->nparam_min];
4212 nparam++;
4213 }
4214 }
4215 /*
4216 * If we've gone over the maximum parameter count (and
4217 * we're in Plus mode), ignore parameters beyond
4218 * nparam_max.
4219 */
4220 if (m->plus && nparam > m->nparam_max)
4221 nparam = m->nparam_max;
4222 /*
4223 * Then terminate the parameter list, and leave.
4224 */
4225 if (!params) { /* need this special case */
4226 params = nasm_malloc(sizeof(*params));
4227 nparam = 0;
4228 }
4229 params[nparam] = NULL;
4230 *params_array = params;
4231 return m;
4232 }
4233 /*
4234 * This one wasn't right: look for the next one with the
4235 * same name.
4236 */
4237 for (m = m->next; m; m = m->next)
4238 if (!mstrcmp(m->name, tline->text, m->casesense))
4239 break;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004240 }
4241
4242 /*
4243 * After all that, we didn't find one with the right number of
4244 * parameters. Issue a warning, and fail to expand the macro.
4245 */
H. Peter Anvin917a3492008-09-24 09:14:49 -07004246 error(ERR_WARNING|ERR_PASS1|ERR_WARN_MNP,
H. Peter Anvine2c80182005-01-15 22:15:51 +00004247 "macro `%s' exists, but not taking %d parameters",
4248 tline->text, nparam);
H. Peter Anvin734b1882002-04-30 21:01:08 +00004249 nasm_free(params);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004250 return NULL;
4251}
4252
Keith Kanios891775e2009-07-11 06:08:54 -05004253
4254/*
4255 * Save MMacro invocation specific fields in
4256 * preparation for a recursive macro expansion
4257 */
4258static void push_mmacro(MMacro *m)
4259{
4260 MMacroInvocation *i;
4261
H. Peter Anvin89cee572009-07-15 09:16:54 -04004262 i = nasm_malloc(sizeof(MMacroInvocation));
4263 i->prev = m->prev;
4264 i->params = m->params;
4265 i->iline = m->iline;
4266 i->nparam = m->nparam;
4267 i->rotate = m->rotate;
4268 i->paramlen = m->paramlen;
4269 i->unique = m->unique;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004270 i->condcnt = m->condcnt;
H. Peter Anvin89cee572009-07-15 09:16:54 -04004271 m->prev = i;
Keith Kanios891775e2009-07-11 06:08:54 -05004272}
4273
4274
4275/*
4276 * Restore MMacro invocation specific fields that were
4277 * saved during a previous recursive macro expansion
4278 */
4279static void pop_mmacro(MMacro *m)
4280{
4281 MMacroInvocation *i;
4282
H. Peter Anvin89cee572009-07-15 09:16:54 -04004283 if (m->prev) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004284 i = m->prev;
4285 m->prev = i->prev;
4286 m->params = i->params;
4287 m->iline = i->iline;
4288 m->nparam = i->nparam;
4289 m->rotate = i->rotate;
4290 m->paramlen = i->paramlen;
4291 m->unique = i->unique;
4292 m->condcnt = i->condcnt;
4293 nasm_free(i);
H. Peter Anvin89cee572009-07-15 09:16:54 -04004294 }
Keith Kanios891775e2009-07-11 06:08:54 -05004295}
4296
4297
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004298/*
4299 * Expand the multi-line macro call made by the given line, if
4300 * there is one to be expanded. If there is, push the expansion on
H. Peter Anvineba20a72002-04-30 20:53:55 +00004301 * istk->expansion and return 1. Otherwise return 0.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004302 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004303static int expand_mmacro(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004304{
4305 Token *startline = tline;
4306 Token *label = NULL;
4307 int dont_prepend = 0;
H. Peter Anvince2233b2008-05-25 21:57:00 -07004308 Token **params, *t, *mtok, *tt;
H. Peter Anvineba20a72002-04-30 20:53:55 +00004309 MMacro *m;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004310 Line *l, *ll;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004311 int i, nparam, *paramlen;
H. Peter Anvinc751e862008-06-09 10:18:45 -07004312 const char *mname;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004313
4314 t = tline;
H. Peter Anvineba20a72002-04-30 20:53:55 +00004315 skip_white_(t);
H. Peter Anvince2233b2008-05-25 21:57:00 -07004316 /* if (!tok_type_(t, TOK_ID)) Lino 02/25/02 */
H. Peter Anvindce1e2f2002-04-30 21:06:37 +00004317 if (!tok_type_(t, TOK_ID) && !tok_type_(t, TOK_PREPROC_ID))
H. Peter Anvine2c80182005-01-15 22:15:51 +00004318 return 0;
H. Peter Anvince2233b2008-05-25 21:57:00 -07004319 mtok = t;
H. Peter Anvin734b1882002-04-30 21:01:08 +00004320 m = is_mmacro(t, &params);
H. Peter Anvinc751e862008-06-09 10:18:45 -07004321 if (m) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004322 mname = t->text;
H. Peter Anvinc751e862008-06-09 10:18:45 -07004323 } else {
H. Peter Anvine2c80182005-01-15 22:15:51 +00004324 Token *last;
4325 /*
4326 * We have an id which isn't a macro call. We'll assume
4327 * it might be a label; we'll also check to see if a
4328 * colon follows it. Then, if there's another id after
4329 * that lot, we'll check it again for macro-hood.
4330 */
4331 label = last = t;
4332 t = t->next;
4333 if (tok_type_(t, TOK_WHITESPACE))
4334 last = t, t = t->next;
4335 if (tok_is_(t, ":")) {
4336 dont_prepend = 1;
4337 last = t, t = t->next;
4338 if (tok_type_(t, TOK_WHITESPACE))
4339 last = t, t = t->next;
4340 }
H. Peter Anvin89cee572009-07-15 09:16:54 -04004341 if (!tok_type_(t, TOK_ID) || !(m = is_mmacro(t, &params)))
H. Peter Anvine2c80182005-01-15 22:15:51 +00004342 return 0;
4343 last->next = NULL;
Keith Kanios891775e2009-07-11 06:08:54 -05004344 mname = t->text;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004345 tline = t;
H. Peter Anvineba20a72002-04-30 20:53:55 +00004346 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004347
4348 /*
4349 * Fix up the parameters: this involves stripping leading and
4350 * trailing whitespace, then stripping braces if they are
4351 * present.
4352 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004353 for (nparam = 0; params[nparam]; nparam++) ;
H. Peter Anvin734b1882002-04-30 21:01:08 +00004354 paramlen = nparam ? nasm_malloc(nparam * sizeof(*paramlen)) : NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004355
H. Peter Anvine2c80182005-01-15 22:15:51 +00004356 for (i = 0; params[i]; i++) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07004357 int brace = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004358 int comma = (!m->plus || i < nparam - 1);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004359
H. Peter Anvine2c80182005-01-15 22:15:51 +00004360 t = params[i];
4361 skip_white_(t);
4362 if (tok_is_(t, "{"))
H. Peter Anvin6867acc2007-10-10 14:58:45 -07004363 t = t->next, brace = true, comma = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004364 params[i] = t;
4365 paramlen[i] = 0;
4366 while (t) {
4367 if (comma && t->type == TOK_OTHER && !strcmp(t->text, ","))
4368 break; /* ... because we have hit a comma */
4369 if (comma && t->type == TOK_WHITESPACE
4370 && tok_is_(t->next, ","))
4371 break; /* ... or a space then a comma */
4372 if (brace && t->type == TOK_OTHER && !strcmp(t->text, "}"))
4373 break; /* ... or a brace */
4374 t = t->next;
4375 paramlen[i]++;
4376 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004377 }
4378
4379 /*
4380 * OK, we have a MMacro structure together with a set of
4381 * parameters. We must now go through the expansion and push
H. Peter Anvin76690a12002-04-30 20:52:49 +00004382 * copies of each Line on to istk->expansion. Substitution of
4383 * parameter tokens and macro-local tokens doesn't get done
4384 * until the single-line macro substitution process; this is
4385 * because delaying them allows us to change the semantics
4386 * later through %rotate.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004387 *
H. Peter Anvin76690a12002-04-30 20:52:49 +00004388 * First, push an end marker on to istk->expansion, mark this
4389 * macro as in progress, and set up its invocation-specific
4390 * variables.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004391 */
4392 ll = nasm_malloc(sizeof(Line));
4393 ll->next = istk->expansion;
4394 ll->finishes = m;
4395 ll->first = NULL;
4396 istk->expansion = ll;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004397
H. Peter Anvin89cee572009-07-15 09:16:54 -04004398 /*
4399 * Save the previous MMacro expansion in the case of
4400 * macro recursion
4401 */
4402 if (m->max_depth && m->in_progress)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004403 push_mmacro(m);
H. Peter Anvin76690a12002-04-30 20:52:49 +00004404
Keith Kanios891775e2009-07-11 06:08:54 -05004405 m->in_progress ++;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004406 m->params = params;
4407 m->iline = tline;
4408 m->nparam = nparam;
4409 m->rotate = 0;
4410 m->paramlen = paramlen;
4411 m->unique = unique++;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004412 m->lineno = 0;
Keith Kanios3c0d91f2009-10-25 13:28:03 -05004413 m->condcnt = 0;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004414
4415 m->next_active = istk->mstk;
4416 istk->mstk = m;
4417
H. Peter Anvine2c80182005-01-15 22:15:51 +00004418 for (l = m->expansion; l; l = l->next) {
4419 Token **tail;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004420
H. Peter Anvine2c80182005-01-15 22:15:51 +00004421 ll = nasm_malloc(sizeof(Line));
4422 ll->finishes = NULL;
4423 ll->next = istk->expansion;
4424 istk->expansion = ll;
4425 tail = &ll->first;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004426
H. Peter Anvine2c80182005-01-15 22:15:51 +00004427 for (t = l->first; t; t = t->next) {
4428 Token *x = t;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004429 switch (t->type) {
4430 case TOK_PREPROC_Q:
4431 tt = *tail = new_Token(NULL, TOK_ID, mname, 0);
4432 break;
4433 case TOK_PREPROC_QQ:
4434 tt = *tail = new_Token(NULL, TOK_ID, m->name, 0);
4435 break;
4436 case TOK_PREPROC_ID:
4437 if (t->text[1] == '0' && t->text[2] == '0') {
4438 dont_prepend = -1;
4439 x = label;
4440 if (!x)
4441 continue;
4442 }
4443 /* fall through */
4444 default:
4445 tt = *tail = new_Token(NULL, x->type, x->text, 0);
4446 break;
4447 }
4448 tail = &tt->next;
4449 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00004450 *tail = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004451 }
4452
4453 /*
H. Peter Anvineba20a72002-04-30 20:53:55 +00004454 * If we had a label, push it on as the first line of
4455 * the macro expansion.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004456 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004457 if (label) {
4458 if (dont_prepend < 0)
4459 free_tlist(startline);
4460 else {
4461 ll = nasm_malloc(sizeof(Line));
4462 ll->finishes = NULL;
4463 ll->next = istk->expansion;
4464 istk->expansion = ll;
4465 ll->first = startline;
4466 if (!dont_prepend) {
4467 while (label->next)
4468 label = label->next;
4469 label->next = tt = new_Token(NULL, TOK_OTHER, ":", 0);
4470 }
4471 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004472 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004473
H. Peter Anvin734b1882002-04-30 21:01:08 +00004474 list->uplevel(m->nolist ? LIST_MACRO_NOLIST : LIST_MACRO);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004475
H. Peter Anvineba20a72002-04-30 20:53:55 +00004476 return 1;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004477}
4478
Victor van den Elzen3b404c02008-09-18 13:51:36 +02004479/* The function that actually does the error reporting */
4480static void verror(int severity, const char *fmt, va_list arg)
4481{
4482 char buff[1024];
4483
4484 vsnprintf(buff, sizeof(buff), fmt, arg);
4485
4486 if (istk && istk->mstk && istk->mstk->name)
H. Peter Anvindbb640b2009-07-18 18:57:16 -07004487 nasm_error(severity, "(%s:%d) %s", istk->mstk->name,
Victor van den Elzen3b404c02008-09-18 13:51:36 +02004488 istk->mstk->lineno, buff);
4489 else
H. Peter Anvindbb640b2009-07-18 18:57:16 -07004490 nasm_error(severity, "%s", buff);
Victor van den Elzen3b404c02008-09-18 13:51:36 +02004491}
4492
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004493/*
4494 * Since preprocessor always operate only on the line that didn't
H. Peter Anvin917a3492008-09-24 09:14:49 -07004495 * arrived yet, we should always use ERR_OFFBY1.
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004496 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004497static void error(int severity, const char *fmt, ...)
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004498{
4499 va_list arg;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004500
4501 /* If we're in a dead branch of IF or something like it, ignore the error */
H. Peter Anvin77ba0c62002-05-14 03:18:53 +00004502 if (istk && istk->conds && !emitting(istk->conds->state))
H. Peter Anvine2c80182005-01-15 22:15:51 +00004503 return;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004504
H. Peter Anvin734b1882002-04-30 21:01:08 +00004505 va_start(arg, fmt);
Victor van den Elzen3b404c02008-09-18 13:51:36 +02004506 verror(severity, fmt, arg);
H. Peter Anvin734b1882002-04-30 21:01:08 +00004507 va_end(arg);
Victor van den Elzen3b404c02008-09-18 13:51:36 +02004508}
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004509
Victor van den Elzen3b404c02008-09-18 13:51:36 +02004510/*
4511 * Because %else etc are evaluated in the state context
4512 * of the previous branch, errors might get lost with error():
4513 * %if 0 ... %else trailing garbage ... %endif
4514 * So %else etc should report errors with this function.
4515 */
4516static void error_precond(int severity, const char *fmt, ...)
4517{
4518 va_list arg;
4519
4520 /* Only ignore the error if it's really in a dead branch */
4521 if (istk && istk->conds && istk->conds->state == COND_NEVER)
4522 return;
4523
4524 va_start(arg, fmt);
4525 verror(severity, fmt, arg);
4526 va_end(arg);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004527}
4528
H. Peter Anvin734b1882002-04-30 21:01:08 +00004529static void
H. Peter Anvindbb640b2009-07-18 18:57:16 -07004530pp_reset(char *file, int apass, ListGen * listgen, StrList **deplist)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004531{
H. Peter Anvin7383b402008-09-24 10:20:40 -07004532 Token *t;
4533
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004534 cstk = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004535 istk = nasm_malloc(sizeof(Include));
4536 istk->next = NULL;
4537 istk->conds = NULL;
4538 istk->expansion = NULL;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004539 istk->mstk = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004540 istk->fp = fopen(file, "r");
H. Peter Anvineba20a72002-04-30 20:53:55 +00004541 istk->fname = NULL;
4542 src_set_fname(nasm_strdup(file));
4543 src_set_linnum(0);
4544 istk->lineinc = 1;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004545 if (!istk->fp)
H. Peter Anvin917a3492008-09-24 09:14:49 -07004546 error(ERR_FATAL|ERR_NOFILE, "unable to open input file `%s'",
H. Peter Anvine2c80182005-01-15 22:15:51 +00004547 file);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004548 defining = NULL;
Charles Crayned4200be2008-07-12 16:42:33 -07004549 nested_mac_count = 0;
4550 nested_rep_count = 0;
H. Peter Anvin97a23472007-09-16 17:57:25 -07004551 init_macros();
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004552 unique = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004553 if (tasm_compatible_mode) {
H. Peter Anvina4835d42008-05-20 14:21:29 -07004554 stdmacpos = nasm_stdmac;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004555 } else {
H. Peter Anvina4835d42008-05-20 14:21:29 -07004556 stdmacpos = nasm_stdmac_after_tasm;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004557 }
H. Peter Anvind2456592008-06-19 15:04:18 -07004558 any_extrastdmac = extrastdmac && *extrastdmac;
4559 do_predef = true;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004560 list = listgen;
H. Peter Anvin61f130f2008-09-25 15:45:06 -07004561
4562 /*
4563 * 0 for dependencies, 1 for preparatory passes, 2 for final pass.
4564 * The caller, however, will also pass in 3 for preprocess-only so
4565 * we can set __PASS__ accordingly.
4566 */
4567 pass = apass > 2 ? 2 : apass;
4568
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07004569 dephead = deptail = deplist;
4570 if (deplist) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004571 StrList *sl = nasm_malloc(strlen(file)+1+sizeof sl->next);
4572 sl->next = NULL;
4573 strcpy(sl->str, file);
4574 *deptail = sl;
4575 deptail = &sl->next;
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07004576 }
H. Peter Anvin7383b402008-09-24 10:20:40 -07004577
H. Peter Anvin61f130f2008-09-25 15:45:06 -07004578 /*
4579 * Define the __PASS__ macro. This is defined here unlike
4580 * all the other builtins, because it is special -- it varies between
4581 * passes.
4582 */
H. Peter Anvin7383b402008-09-24 10:20:40 -07004583 t = nasm_malloc(sizeof(*t));
4584 t->next = NULL;
H. Peter Anvin61f130f2008-09-25 15:45:06 -07004585 make_tok_num(t, apass);
H. Peter Anvin7383b402008-09-24 10:20:40 -07004586 t->a.mac = NULL;
4587 define_smacro(NULL, "__PASS__", true, 0, t);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004588}
4589
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004590static char *pp_getline(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004591{
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004592 char *line;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004593 Token *tline;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004594
H. Peter Anvine2c80182005-01-15 22:15:51 +00004595 while (1) {
4596 /*
Keith Kaniosb7a89542007-04-12 02:40:54 +00004597 * Fetch a tokenized line, either from the macro-expansion
H. Peter Anvine2c80182005-01-15 22:15:51 +00004598 * buffer or from the input file.
4599 */
4600 tline = NULL;
4601 while (istk->expansion && istk->expansion->finishes) {
4602 Line *l = istk->expansion;
4603 if (!l->finishes->name && l->finishes->in_progress > 1) {
4604 Line *ll;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004605
H. Peter Anvine2c80182005-01-15 22:15:51 +00004606 /*
4607 * This is a macro-end marker for a macro with no
4608 * name, which means it's not really a macro at all
4609 * but a %rep block, and the `in_progress' field is
4610 * more than 1, meaning that we still need to
4611 * repeat. (1 means the natural last repetition; 0
4612 * means termination by %exitrep.) We have
4613 * therefore expanded up to the %endrep, and must
4614 * push the whole block on to the expansion buffer
4615 * again. We don't bother to remove the macro-end
4616 * marker: we'd only have to generate another one
4617 * if we did.
4618 */
4619 l->finishes->in_progress--;
4620 for (l = l->finishes->expansion; l; l = l->next) {
4621 Token *t, *tt, **tail;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004622
H. Peter Anvine2c80182005-01-15 22:15:51 +00004623 ll = nasm_malloc(sizeof(Line));
4624 ll->next = istk->expansion;
4625 ll->finishes = NULL;
4626 ll->first = NULL;
4627 tail = &ll->first;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004628
H. Peter Anvine2c80182005-01-15 22:15:51 +00004629 for (t = l->first; t; t = t->next) {
4630 if (t->text || t->type == TOK_WHITESPACE) {
4631 tt = *tail =
4632 new_Token(NULL, t->type, t->text, 0);
4633 tail = &tt->next;
4634 }
4635 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00004636
H. Peter Anvine2c80182005-01-15 22:15:51 +00004637 istk->expansion = ll;
4638 }
4639 } else {
4640 /*
4641 * Check whether a `%rep' was started and not ended
4642 * within this macro expansion. This can happen and
4643 * should be detected. It's a fatal error because
4644 * I'm too confused to work out how to recover
4645 * sensibly from it.
4646 */
4647 if (defining) {
4648 if (defining->name)
4649 error(ERR_PANIC,
4650 "defining with name in expansion");
4651 else if (istk->mstk->name)
4652 error(ERR_FATAL,
4653 "`%%rep' without `%%endrep' within"
4654 " expansion of macro `%s'",
4655 istk->mstk->name);
4656 }
H. Peter Anvin87bc6192002-04-30 20:53:16 +00004657
H. Peter Anvine2c80182005-01-15 22:15:51 +00004658 /*
4659 * FIXME: investigate the relationship at this point between
4660 * istk->mstk and l->finishes
4661 */
4662 {
4663 MMacro *m = istk->mstk;
4664 istk->mstk = m->next_active;
4665 if (m->name) {
4666 /*
4667 * This was a real macro call, not a %rep, and
4668 * therefore the parameter information needs to
4669 * be freed.
4670 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004671 if (m->prev) {
4672 pop_mmacro(m);
4673 l->finishes->in_progress --;
4674 } else {
Keith Kanios891775e2009-07-11 06:08:54 -05004675 nasm_free(m->params);
4676 free_tlist(m->iline);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004677 nasm_free(m->paramlen);
4678 l->finishes->in_progress = 0;
4679 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00004680 } else
4681 free_mmacro(m);
4682 }
4683 istk->expansion = l->next;
4684 nasm_free(l);
4685 list->downlevel(LIST_MACRO);
4686 }
4687 }
4688 while (1) { /* until we get a line we can use */
H. Peter Anvineba20a72002-04-30 20:53:55 +00004689
H. Peter Anvine2c80182005-01-15 22:15:51 +00004690 if (istk->expansion) { /* from a macro expansion */
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004691 char *p;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004692 Line *l = istk->expansion;
4693 if (istk->mstk)
4694 istk->mstk->lineno++;
4695 tline = l->first;
4696 istk->expansion = l->next;
4697 nasm_free(l);
H. Peter Anvin6867acc2007-10-10 14:58:45 -07004698 p = detoken(tline, false);
H. Peter Anvine2c80182005-01-15 22:15:51 +00004699 list->line(LIST_MACRO, p);
4700 nasm_free(p);
4701 break;
4702 }
4703 line = read_line();
4704 if (line) { /* from the current input file */
4705 line = prepreproc(line);
Keith Kaniosb7a89542007-04-12 02:40:54 +00004706 tline = tokenize(line);
H. Peter Anvine2c80182005-01-15 22:15:51 +00004707 nasm_free(line);
4708 break;
4709 }
4710 /*
4711 * The current file has ended; work down the istk
4712 */
4713 {
4714 Include *i = istk;
4715 fclose(i->fp);
4716 if (i->conds)
4717 error(ERR_FATAL,
4718 "expected `%%endif' before end of file");
4719 /* only set line and file name if there's a next node */
4720 if (i->next) {
4721 src_set_linnum(i->lineno);
4722 nasm_free(src_set_fname(i->fname));
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004723 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00004724 istk = i->next;
4725 list->downlevel(LIST_INCLUDE);
4726 nasm_free(i);
4727 if (!istk)
4728 return NULL;
Victor van den Elzen4c9d6222008-10-01 13:08:50 +02004729 if (istk->expansion && istk->expansion->finishes)
4730 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004731 }
4732 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004733
H. Peter Anvine2c80182005-01-15 22:15:51 +00004734 /*
4735 * We must expand MMacro parameters and MMacro-local labels
4736 * _before_ we plunge into directive processing, to cope
4737 * with things like `%define something %1' such as STRUC
4738 * uses. Unless we're _defining_ a MMacro, in which case
4739 * those tokens should be left alone to go into the
4740 * definition; and unless we're in a non-emitting
4741 * condition, in which case we don't want to meddle with
4742 * anything.
4743 */
Charles Crayned4200be2008-07-12 16:42:33 -07004744 if (!defining && !(istk->conds && !emitting(istk->conds->state))
H. Peter Anvin992fe752008-10-19 15:45:05 -07004745 && !(istk->mstk && !istk->mstk->in_progress)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00004746 tline = expand_mmac_params(tline);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004747 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00004748
H. Peter Anvine2c80182005-01-15 22:15:51 +00004749 /*
4750 * Check the line to see if it's a preprocessor directive.
4751 */
4752 if (do_directive(tline) == DIRECTIVE_FOUND) {
4753 continue;
4754 } else if (defining) {
4755 /*
4756 * We're defining a multi-line macro. We emit nothing
4757 * at all, and just
Keith Kaniosb7a89542007-04-12 02:40:54 +00004758 * shove the tokenized line on to the macro definition.
H. Peter Anvine2c80182005-01-15 22:15:51 +00004759 */
4760 Line *l = nasm_malloc(sizeof(Line));
4761 l->next = defining->expansion;
4762 l->first = tline;
H. Peter Anvin538002d2008-06-28 18:30:27 -07004763 l->finishes = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004764 defining->expansion = l;
4765 continue;
4766 } else if (istk->conds && !emitting(istk->conds->state)) {
4767 /*
4768 * We're in a non-emitting branch of a condition block.
4769 * Emit nothing at all, not even a blank line: when we
4770 * emerge from the condition we'll give a line-number
4771 * directive so we keep our place correctly.
4772 */
4773 free_tlist(tline);
4774 continue;
4775 } else if (istk->mstk && !istk->mstk->in_progress) {
4776 /*
4777 * We're in a %rep block which has been terminated, so
4778 * we're walking through to the %endrep without
4779 * emitting anything. Emit nothing at all, not even a
4780 * blank line: when we emerge from the %rep block we'll
4781 * give a line-number directive so we keep our place
4782 * correctly.
4783 */
4784 free_tlist(tline);
4785 continue;
4786 } else {
4787 tline = expand_smacro(tline);
4788 if (!expand_mmacro(tline)) {
4789 /*
Keith Kaniosb7a89542007-04-12 02:40:54 +00004790 * De-tokenize the line again, and emit it.
H. Peter Anvine2c80182005-01-15 22:15:51 +00004791 */
H. Peter Anvin6867acc2007-10-10 14:58:45 -07004792 line = detoken(tline, true);
H. Peter Anvine2c80182005-01-15 22:15:51 +00004793 free_tlist(tline);
4794 break;
4795 } else {
4796 continue; /* expand_mmacro calls free_tlist */
4797 }
4798 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004799 }
4800
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004801 return line;
4802}
4803
H. Peter Anvine2c80182005-01-15 22:15:51 +00004804static void pp_cleanup(int pass)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004805{
H. Peter Anvine2c80182005-01-15 22:15:51 +00004806 if (defining) {
H. Peter Anvin89cee572009-07-15 09:16:54 -04004807 if (defining->name) {
Victor van den Elzen8f1120f2008-07-16 13:41:37 +02004808 error(ERR_NONFATAL,
4809 "end of file while still defining macro `%s'",
4810 defining->name);
4811 } else {
4812 error(ERR_NONFATAL, "end of file while still in %%rep");
4813 }
4814
H. Peter Anvine2c80182005-01-15 22:15:51 +00004815 free_mmacro(defining);
Cyrill Gorcunova327c652010-02-14 17:19:38 +03004816 defining = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004817 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004818 while (cstk)
H. Peter Anvine2c80182005-01-15 22:15:51 +00004819 ctx_pop();
H. Peter Anvin97a23472007-09-16 17:57:25 -07004820 free_macros();
H. Peter Anvine2c80182005-01-15 22:15:51 +00004821 while (istk) {
4822 Include *i = istk;
4823 istk = istk->next;
4824 fclose(i->fp);
4825 nasm_free(i->fname);
4826 nasm_free(i);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004827 }
4828 while (cstk)
H. Peter Anvine2c80182005-01-15 22:15:51 +00004829 ctx_pop();
H. Peter Anvin86877b22008-06-20 15:55:45 -07004830 nasm_free(src_set_fname(NULL));
H. Peter Anvine2c80182005-01-15 22:15:51 +00004831 if (pass == 0) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004832 IncPath *i;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004833 free_llist(predef);
4834 delete_Blocks();
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004835 while ((i = ipath)) {
4836 ipath = i->next;
4837 if (i->path)
4838 nasm_free(i->path);
4839 nasm_free(i);
4840 }
H. Peter Anvin11dfa1a2008-07-02 18:11:04 -07004841 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004842}
4843
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004844void pp_include_path(char *path)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004845{
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004846 IncPath *i;
H. Peter Anvin37a321f2007-09-24 13:41:58 -07004847
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004848 i = nasm_malloc(sizeof(IncPath));
H. Peter Anvin37a321f2007-09-24 13:41:58 -07004849 i->path = path ? nasm_strdup(path) : NULL;
Frank Kotlerd0ed6fd2003-08-27 11:33:56 +00004850 i->next = NULL;
4851
H. Peter Anvin89cee572009-07-15 09:16:54 -04004852 if (ipath) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00004853 IncPath *j = ipath;
H. Peter Anvin89cee572009-07-15 09:16:54 -04004854 while (j->next)
H. Peter Anvine2c80182005-01-15 22:15:51 +00004855 j = j->next;
4856 j->next = i;
4857 } else {
4858 ipath = i;
Frank Kotlerd0ed6fd2003-08-27 11:33:56 +00004859 }
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004860}
Frank Kotlerd0ed6fd2003-08-27 11:33:56 +00004861
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004862void pp_pre_include(char *fname)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004863{
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004864 Token *inc, *space, *name;
4865 Line *l;
4866
H. Peter Anvin734b1882002-04-30 21:01:08 +00004867 name = new_Token(NULL, TOK_INTERNAL_STRING, fname, 0);
4868 space = new_Token(name, TOK_WHITESPACE, NULL, 0);
4869 inc = new_Token(space, TOK_PREPROC_ID, "%include", 0);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004870
4871 l = nasm_malloc(sizeof(Line));
4872 l->next = predef;
4873 l->first = inc;
H. Peter Anvin538002d2008-06-28 18:30:27 -07004874 l->finishes = NULL;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004875 predef = l;
4876}
4877
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004878void pp_pre_define(char *definition)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004879{
4880 Token *def, *space;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004881 Line *l;
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004882 char *equals;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004883
4884 equals = strchr(definition, '=');
H. Peter Anvin734b1882002-04-30 21:01:08 +00004885 space = new_Token(NULL, TOK_WHITESPACE, NULL, 0);
4886 def = new_Token(space, TOK_PREPROC_ID, "%define", 0);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004887 if (equals)
H. Peter Anvine2c80182005-01-15 22:15:51 +00004888 *equals = ' ';
Keith Kaniosb7a89542007-04-12 02:40:54 +00004889 space->next = tokenize(definition);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004890 if (equals)
H. Peter Anvine2c80182005-01-15 22:15:51 +00004891 *equals = '=';
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004892
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004893 l = nasm_malloc(sizeof(Line));
4894 l->next = predef;
4895 l->first = def;
H. Peter Anvin538002d2008-06-28 18:30:27 -07004896 l->finishes = NULL;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004897 predef = l;
4898}
4899
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004900void pp_pre_undefine(char *definition)
H. Peter Anvin620515a2002-04-30 20:57:38 +00004901{
4902 Token *def, *space;
4903 Line *l;
4904
H. Peter Anvin734b1882002-04-30 21:01:08 +00004905 space = new_Token(NULL, TOK_WHITESPACE, NULL, 0);
4906 def = new_Token(space, TOK_PREPROC_ID, "%undef", 0);
Keith Kaniosb7a89542007-04-12 02:40:54 +00004907 space->next = tokenize(definition);
H. Peter Anvin620515a2002-04-30 20:57:38 +00004908
4909 l = nasm_malloc(sizeof(Line));
4910 l->next = predef;
4911 l->first = def;
H. Peter Anvin538002d2008-06-28 18:30:27 -07004912 l->finishes = NULL;
H. Peter Anvin620515a2002-04-30 20:57:38 +00004913 predef = l;
4914}
4915
Keith Kaniosb7a89542007-04-12 02:40:54 +00004916/*
4917 * Added by Keith Kanios:
4918 *
4919 * This function is used to assist with "runtime" preprocessor
4920 * directives. (e.g. pp_runtime("%define __BITS__ 64");)
4921 *
4922 * ERRORS ARE IGNORED HERE, SO MAKE COMPLETELY SURE THAT YOU
4923 * PASS A VALID STRING TO THIS FUNCTION!!!!!
4924 */
4925
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004926void pp_runtime(char *definition)
Keith Kaniosb7a89542007-04-12 02:40:54 +00004927{
4928 Token *def;
H. Peter Anvin70653092007-10-19 14:42:29 -07004929
Keith Kaniosb7a89542007-04-12 02:40:54 +00004930 def = tokenize(definition);
H. Peter Anvin89cee572009-07-15 09:16:54 -04004931 if (do_directive(def) == NO_DIRECTIVE_FOUND)
Keith Kaniosb7a89542007-04-12 02:40:54 +00004932 free_tlist(def);
H. Peter Anvin70653092007-10-19 14:42:29 -07004933
Keith Kaniosb7a89542007-04-12 02:40:54 +00004934}
4935
H. Peter Anvina70547f2008-07-19 21:44:26 -07004936void pp_extra_stdmac(macros_t *macros)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004937{
H. Peter Anvin76690a12002-04-30 20:52:49 +00004938 extrastdmac = macros;
4939}
4940
Keith Kaniosa5fc6462007-10-13 07:09:22 -07004941static void make_tok_num(Token * tok, int64_t val)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004942{
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004943 char numbuf[20];
Keith Kaniosa5fc6462007-10-13 07:09:22 -07004944 snprintf(numbuf, sizeof(numbuf), "%"PRId64"", val);
H. Peter Anvineba20a72002-04-30 20:53:55 +00004945 tok->text = nasm_strdup(numbuf);
4946 tok->type = TOK_NUMBER;
4947}
4948
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004949Preproc nasmpp = {
4950 pp_reset,
4951 pp_getline,
4952 pp_cleanup
4953};