blob: be321f33780ff1a5fd552dc5815eb14f3c60fba5 [file] [log] [blame]
H. Peter Anvin9e6747c2009-06-28 17:13:04 -07001/* ----------------------------------------------------------------------- *
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002 *
3 * Copyright 1996-2010 The NASM Authors - All Rights Reserved
H. Peter Anvin9e6747c2009-06-28 17:13:04 -07004 * See the file AUTHORS included with the NASM distribution for
5 * the specific copyright holders.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00006 *
H. Peter Anvin9e6747c2009-06-28 17:13:04 -07007 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following
9 * conditions are met:
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000010 *
H. Peter Anvin9e6747c2009-06-28 17:13:04 -070011 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above
14 * copyright notice, this list of conditions and the following
15 * disclaimer in the documentation and/or other materials provided
16 * with the distribution.
Cyrill Gorcunovaccda192010-02-16 10:27:56 +030017 *
H. Peter Anvin9e6747c2009-06-28 17:13:04 -070018 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
19 * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
20 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
21 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
23 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
26 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
29 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
30 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 *
32 * ----------------------------------------------------------------------- */
33
34/*
35 * preproc.c macro preprocessor for the Netwide Assembler
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000036 */
37
H. Peter Anvin4836e332002-04-30 20:56:43 +000038/* Typical flow of text through preproc
39 *
Keith Kaniosb7a89542007-04-12 02:40:54 +000040 * pp_getline gets tokenized lines, either
H. Peter Anvin4836e332002-04-30 20:56:43 +000041 *
42 * from a macro expansion
43 *
44 * or
45 * {
46 * read_line gets raw text from stdmacpos, or predef, or current input file
Keith Kaniosb7a89542007-04-12 02:40:54 +000047 * tokenize converts to tokens
H. Peter Anvin4836e332002-04-30 20:56:43 +000048 * }
49 *
50 * expand_mmac_params is used to expand %1 etc., unless a macro is being
51 * defined or a false conditional is being processed
52 * (%0, %1, %+1, %-1, %%foo
53 *
54 * do_directive checks for directives
55 *
56 * expand_smacro is used to expand single line macros
57 *
58 * expand_mmacro is used to expand multi-line macros
59 *
60 * detoken is used to convert the line back to text
61 */
H. Peter Anvineba20a72002-04-30 20:53:55 +000062
H. Peter Anvinfe501952007-10-02 21:53:51 -070063#include "compiler.h"
64
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000065#include <stdio.h>
H. Peter Anvinaf535c12002-04-30 20:59:21 +000066#include <stdarg.h>
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000067#include <stdlib.h>
68#include <stddef.h>
69#include <string.h>
70#include <ctype.h>
H. Peter Anvin76690a12002-04-30 20:52:49 +000071#include <limits.h>
Keith Kaniosb7a89542007-04-12 02:40:54 +000072#include <inttypes.h>
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000073
74#include "nasm.h"
75#include "nasmlib.h"
H. Peter Anvin4169a472007-09-12 01:29:43 +000076#include "preproc.h"
H. Peter Anvin97a23472007-09-16 17:57:25 -070077#include "hashtbl.h"
H. Peter Anvin8cad14b2008-06-01 17:23:51 -070078#include "quote.h"
H. Peter Anvinc2df2822007-10-24 15:29:28 -070079#include "stdscan.h"
H. Peter Anvindbb640b2009-07-18 18:57:16 -070080#include "eval.h"
H. Peter Anvinc2df2822007-10-24 15:29:28 -070081#include "tokens.h"
H. Peter Anvina4835d42008-05-20 14:21:29 -070082#include "tables.h"
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000083
84typedef struct SMacro SMacro;
85typedef struct MMacro MMacro;
Keith Kanios891775e2009-07-11 06:08:54 -050086typedef struct MMacroInvocation MMacroInvocation;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000087typedef struct Context Context;
88typedef struct Token Token;
H. Peter Anvince616072002-04-30 21:02:23 +000089typedef struct Blocks Blocks;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000090typedef struct Line Line;
91typedef struct Include Include;
92typedef struct Cond Cond;
H. Peter Anvin6768eb72002-04-30 20:52:26 +000093typedef struct IncPath IncPath;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000094
95/*
H. Peter Anvin97a23472007-09-16 17:57:25 -070096 * Note on the storage of both SMacro and MMacros: the hash table
97 * indexes them case-insensitively, and we then have to go through a
98 * linked list of potential case aliases (and, for MMacros, parameter
99 * ranges); this is to preserve the matching semantics of the earlier
100 * code. If the number of case aliases for a specific macro is a
101 * performance issue, you may want to reconsider your coding style.
102 */
103
104/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000105 * Store the definition of a single-line macro.
106 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000107struct SMacro {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000108 SMacro *next;
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000109 char *name;
H. Peter Anvin70055962007-10-11 00:05:31 -0700110 bool casesense;
H. Peter Anvin16ed4382007-10-11 10:06:19 -0700111 bool in_progress;
H. Peter Anvin70055962007-10-11 00:05:31 -0700112 unsigned int nparam;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000113 Token *expansion;
114};
115
116/*
H. Peter Anvin76690a12002-04-30 20:52:49 +0000117 * Store the definition of a multi-line macro. This is also used to
118 * store the interiors of `%rep...%endrep' blocks, which are
119 * effectively self-re-invoking multi-line macros which simply
120 * don't have a name or bother to appear in the hash tables. %rep
121 * blocks are signified by having a NULL `name' field.
122 *
123 * In a MMacro describing a `%rep' block, the `in_progress' field
124 * isn't merely boolean, but gives the number of repeats left to
125 * run.
126 *
127 * The `next' field is used for storing MMacros in hash tables; the
128 * `next_active' field is for stacking them on istk entries.
129 *
130 * When a MMacro is being expanded, `params', `iline', `nparam',
131 * `paramlen', `rotate' and `unique' are local to the invocation.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000132 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000133struct MMacro {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000134 MMacro *next;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300135 MMacroInvocation *prev; /* previous invocation */
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000136 char *name;
H. Peter Anvin8cfdb9d2007-09-14 18:36:01 -0700137 int nparam_min, nparam_max;
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700138 bool casesense;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300139 bool plus; /* is the last parameter greedy? */
140 bool nolist; /* is this macro listing-inhibited? */
141 int64_t in_progress; /* is this macro currently being expanded? */
142 int32_t max_depth; /* maximum number of recursive expansions allowed */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000143 Token *dlist; /* All defaults as one list */
144 Token **defaults; /* Parameter default pointers */
145 int ndefs; /* number of default parameters */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000146 Line *expansion;
H. Peter Anvin76690a12002-04-30 20:52:49 +0000147
148 MMacro *next_active;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000149 MMacro *rep_nest; /* used for nesting %rep */
150 Token **params; /* actual parameters */
151 Token *iline; /* invocation line */
H. Peter Anvin25a99342007-09-22 17:45:45 -0700152 unsigned int nparam, rotate;
153 int *paramlen;
H. Peter Anvinf8ba53e2007-10-11 10:11:57 -0700154 uint64_t unique;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000155 int lineno; /* Current line number on expansion */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300156 uint64_t condcnt; /* number of if blocks... */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000157};
158
Keith Kanios891775e2009-07-11 06:08:54 -0500159
160/* Store the definition of a multi-line macro, as defined in a
161 * previous recursive macro expansion.
162 */
163struct MMacroInvocation {
H. Peter Anvin89cee572009-07-15 09:16:54 -0400164 MMacroInvocation *prev; /* previous invocation */
165 Token **params; /* actual parameters */
166 Token *iline; /* invocation line */
Keith Kanios891775e2009-07-11 06:08:54 -0500167 unsigned int nparam, rotate;
168 int *paramlen;
169 uint64_t unique;
Keith Kanios3c0d91f2009-10-25 13:28:03 -0500170 uint64_t condcnt;
Keith Kanios891775e2009-07-11 06:08:54 -0500171};
172
173
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000174/*
175 * The context stack is composed of a linked list of these.
176 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000177struct Context {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000178 Context *next;
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000179 char *name;
H. Peter Anvin166c2472008-05-28 12:28:58 -0700180 struct hash_table localmac;
Keith Kaniosb7a89542007-04-12 02:40:54 +0000181 uint32_t number;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000182};
183
184/*
185 * This is the internal form which we break input lines up into.
186 * Typically stored in linked lists.
187 *
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000188 * Note that `type' serves a double meaning: TOK_SMAC_PARAM is not
189 * necessarily used as-is, but is intended to denote the number of
190 * the substituted parameter. So in the definition
191 *
192 * %define a(x,y) ( (x) & ~(y) )
H. Peter Anvin70653092007-10-19 14:42:29 -0700193 *
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000194 * the token representing `x' will have its type changed to
195 * TOK_SMAC_PARAM, but the one representing `y' will be
196 * TOK_SMAC_PARAM+1.
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000197 *
198 * TOK_INTERNAL_STRING is a dirty hack: it's a single string token
199 * which doesn't need quotes around it. Used in the pre-include
200 * mechanism as an alternative to trying to find a sensible type of
201 * quote to use on the filename we were passed.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000202 */
H. Peter Anvinda10e7b2007-09-12 04:18:37 +0000203enum pp_token_type {
204 TOK_NONE = 0, TOK_WHITESPACE, TOK_COMMENT, TOK_ID,
205 TOK_PREPROC_ID, TOK_STRING,
H. Peter Anvin6c81f0a2008-05-25 21:46:17 -0700206 TOK_NUMBER, TOK_FLOAT, TOK_SMAC_END, TOK_OTHER,
207 TOK_INTERNAL_STRING,
208 TOK_PREPROC_Q, TOK_PREPROC_QQ,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300209 TOK_PASTE, /* %+ */
210 TOK_INDIRECT, /* %[...] */
211 TOK_SMAC_PARAM, /* MUST BE LAST IN THE LIST!!! */
212 TOK_MAX = INT_MAX /* Keep compiler from reducing the range */
H. Peter Anvinda10e7b2007-09-12 04:18:37 +0000213};
214
H. Peter Anvine2c80182005-01-15 22:15:51 +0000215struct Token {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000216 Token *next;
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000217 char *text;
H. Peter Anvinf26e0972008-07-01 21:26:27 -0700218 union {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300219 SMacro *mac; /* associated macro for TOK_SMAC_END */
220 size_t len; /* scratch length field */
221 } a; /* Auxiliary data */
H. Peter Anvinda10e7b2007-09-12 04:18:37 +0000222 enum pp_token_type type;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000223};
224
225/*
226 * Multi-line macro definitions are stored as a linked list of
227 * these, which is essentially a container to allow several linked
228 * lists of Tokens.
H. Peter Anvin70653092007-10-19 14:42:29 -0700229 *
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000230 * Note that in this module, linked lists are treated as stacks
231 * wherever possible. For this reason, Lines are _pushed_ on to the
232 * `expansion' field in MMacro structures, so that the linked list,
233 * if walked, would give the macro lines in reverse order; this
234 * means that we can walk the list when expanding a macro, and thus
235 * push the lines on to the `expansion' field in _istk_ in reverse
236 * order (so that when popped back off they are in the right
237 * order). It may seem cockeyed, and it relies on my design having
238 * an even number of steps in, but it works...
239 *
240 * Some of these structures, rather than being actual lines, are
241 * markers delimiting the end of the expansion of a given macro.
H. Peter Anvin76690a12002-04-30 20:52:49 +0000242 * This is for use in the cycle-tracking and %rep-handling code.
243 * Such structures have `finishes' non-NULL, and `first' NULL. All
244 * others have `finishes' NULL, but `first' may still be NULL if
245 * the line is blank.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000246 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000247struct Line {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000248 Line *next;
249 MMacro *finishes;
250 Token *first;
251};
252
253/*
254 * To handle an arbitrary level of file inclusion, we maintain a
255 * stack (ie linked list) of these things.
256 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000257struct Include {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000258 Include *next;
259 FILE *fp;
260 Cond *conds;
261 Line *expansion;
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000262 char *fname;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000263 int lineno, lineinc;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300264 MMacro *mstk; /* stack of active macros/reps */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000265};
266
267/*
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000268 * Include search path. This is simply a list of strings which get
269 * prepended, in turn, to the name of an include file, in an
270 * attempt to find the file if it's not in the current directory.
271 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000272struct IncPath {
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000273 IncPath *next;
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000274 char *path;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000275};
276
277/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000278 * Conditional assembly: we maintain a separate stack of these for
279 * each level of file inclusion. (The only reason we keep the
280 * stacks separate is to ensure that a stray `%endif' in a file
281 * included from within the true branch of a `%if' won't terminate
282 * it and cause confusion: instead, rightly, it'll cause an error.)
283 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000284struct Cond {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000285 Cond *next;
286 int state;
287};
H. Peter Anvine2c80182005-01-15 22:15:51 +0000288enum {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000289 /*
290 * These states are for use just after %if or %elif: IF_TRUE
291 * means the condition has evaluated to truth so we are
292 * currently emitting, whereas IF_FALSE means we are not
293 * currently emitting but will start doing so if a %else comes
294 * up. In these states, all directives are admissible: %elif,
295 * %else and %endif. (And of course %if.)
296 */
297 COND_IF_TRUE, COND_IF_FALSE,
298 /*
299 * These states come up after a %else: ELSE_TRUE means we're
300 * emitting, and ELSE_FALSE means we're not. In ELSE_* states,
301 * any %elif or %else will cause an error.
302 */
303 COND_ELSE_TRUE, COND_ELSE_FALSE,
304 /*
Victor van den Elzen3b404c02008-09-18 13:51:36 +0200305 * These states mean that we're not emitting now, and also that
306 * nothing until %endif will be emitted at all. COND_DONE is
307 * used when we've had our moment of emission
308 * and have now started seeing %elifs. COND_NEVER is used when
309 * the condition construct in question is contained within a
310 * non-emitting branch of a larger condition construct,
311 * or if there is an error.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000312 */
Victor van den Elzen3b404c02008-09-18 13:51:36 +0200313 COND_DONE, COND_NEVER
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000314};
315#define emitting(x) ( (x) == COND_IF_TRUE || (x) == COND_ELSE_TRUE )
316
H. Peter Anvin70653092007-10-19 14:42:29 -0700317/*
Ed Beroset3ab3f412002-06-11 03:31:49 +0000318 * These defines are used as the possible return values for do_directive
319 */
320#define NO_DIRECTIVE_FOUND 0
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300321#define DIRECTIVE_FOUND 1
Ed Beroset3ab3f412002-06-11 03:31:49 +0000322
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000323/*
Keith Kanios852f1ee2009-07-12 00:19:55 -0500324 * This define sets the upper limit for smacro and recursive mmacro
325 * expansions
326 */
327#define DEADMAN_LIMIT (1 << 20)
328
329/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000330 * Condition codes. Note that we use c_ prefix not C_ because C_ is
331 * used in nasm.h for the "real" condition codes. At _this_ level,
332 * we treat CXZ and ECXZ as condition codes, albeit non-invertible
333 * ones, so we need a different enum...
334 */
H. Peter Anvin476d2862007-10-02 22:04:15 -0700335static const char * const conditions[] = {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000336 "a", "ae", "b", "be", "c", "cxz", "e", "ecxz", "g", "ge", "l", "le",
337 "na", "nae", "nb", "nbe", "nc", "ne", "ng", "nge", "nl", "nle", "no",
H. Peter Anvince9be342007-09-12 00:22:29 +0000338 "np", "ns", "nz", "o", "p", "pe", "po", "rcxz", "s", "z"
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000339};
H. Peter Anvin476d2862007-10-02 22:04:15 -0700340enum pp_conds {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000341 c_A, c_AE, c_B, c_BE, c_C, c_CXZ, c_E, c_ECXZ, c_G, c_GE, c_L, c_LE,
342 c_NA, c_NAE, c_NB, c_NBE, c_NC, c_NE, c_NG, c_NGE, c_NL, c_NLE, c_NO,
H. Peter Anvin476d2862007-10-02 22:04:15 -0700343 c_NP, c_NS, c_NZ, c_O, c_P, c_PE, c_PO, c_RCXZ, c_S, c_Z,
344 c_none = -1
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000345};
H. Peter Anvin476d2862007-10-02 22:04:15 -0700346static const enum pp_conds inverse_ccs[] = {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000347 c_NA, c_NAE, c_NB, c_NBE, c_NC, -1, c_NE, -1, c_NG, c_NGE, c_NL, c_NLE,
348 c_A, c_AE, c_B, c_BE, c_C, c_E, c_G, c_GE, c_L, c_LE, c_O, c_P, c_S,
H. Peter Anvince9be342007-09-12 00:22:29 +0000349 c_Z, c_NO, c_NP, c_PO, c_PE, -1, c_NS, c_NZ
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000350};
351
H. Peter Anvin76690a12002-04-30 20:52:49 +0000352/*
353 * Directive names.
354 */
H. Peter Anvin65747262002-05-07 00:10:05 +0000355/* If this is a an IF, ELIF, ELSE or ENDIF keyword */
H. Peter Anvin9bf0aa72007-09-12 02:12:07 +0000356static int is_condition(enum preproc_token arg)
H. Peter Anvin65747262002-05-07 00:10:05 +0000357{
H. Peter Anvinda10e7b2007-09-12 04:18:37 +0000358 return PP_IS_COND(arg) || (arg == PP_ELSE) || (arg == PP_ENDIF);
H. Peter Anvin65747262002-05-07 00:10:05 +0000359}
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000360
361/* For TASM compatibility we need to be able to recognise TASM compatible
362 * conditional compilation directives. Using the NASM pre-processor does
363 * not work, so we look for them specifically from the following list and
364 * then jam in the equivalent NASM directive into the input stream.
365 */
366
H. Peter Anvine2c80182005-01-15 22:15:51 +0000367enum {
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000368 TM_ARG, TM_ELIF, TM_ELSE, TM_ENDIF, TM_IF, TM_IFDEF, TM_IFDIFI,
369 TM_IFNDEF, TM_INCLUDE, TM_LOCAL
370};
371
H. Peter Anvin476d2862007-10-02 22:04:15 -0700372static const char * const tasm_directives[] = {
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000373 "arg", "elif", "else", "endif", "if", "ifdef", "ifdifi",
374 "ifndef", "include", "local"
375};
376
377static int StackSize = 4;
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000378static char *StackPointer = "ebp";
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000379static int ArgOffset = 8;
H. Peter Anvin8781cb02007-11-08 20:01:11 -0800380static int LocalOffset = 0;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000381
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000382static Context *cstk;
383static Include *istk;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000384static IncPath *ipath = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000385
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300386static int pass; /* HACK: pass 0 = generate dependencies only */
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700387static StrList **dephead, **deptail; /* Dependency list */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000388
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300389static uint64_t unique; /* unique identifier numbers */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000390
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000391static Line *predef = NULL;
H. Peter Anvind2456592008-06-19 15:04:18 -0700392static bool do_predef;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000393
394static ListGen *list;
395
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000396/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000397 * The current set of multi-line macros we have defined.
398 */
H. Peter Anvin166c2472008-05-28 12:28:58 -0700399static struct hash_table mmacros;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000400
401/*
402 * The current set of single-line macros we have defined.
403 */
H. Peter Anvin166c2472008-05-28 12:28:58 -0700404static struct hash_table smacros;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000405
406/*
H. Peter Anvin76690a12002-04-30 20:52:49 +0000407 * The multi-line macro we are currently defining, or the %rep
408 * block we are currently reading, if any.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000409 */
410static MMacro *defining;
411
Charles Crayned4200be2008-07-12 16:42:33 -0700412static uint64_t nested_mac_count;
413static uint64_t nested_rep_count;
414
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000415/*
416 * The number of macro parameters to allocate space for at a time.
417 */
418#define PARAM_DELTA 16
419
420/*
H. Peter Anvina4835d42008-05-20 14:21:29 -0700421 * The standard macro set: defined in macros.c in the array nasm_stdmac.
422 * This gives our position in the macro set, when we're processing it.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000423 */
H. Peter Anvina70547f2008-07-19 21:44:26 -0700424static macros_t *stdmacpos;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000425
426/*
H. Peter Anvin76690a12002-04-30 20:52:49 +0000427 * The extra standard macros that come from the object format, if
428 * any.
429 */
H. Peter Anvina70547f2008-07-19 21:44:26 -0700430static macros_t *extrastdmac = NULL;
H. Peter Anvincfb71762008-06-20 15:20:16 -0700431static bool any_extrastdmac;
H. Peter Anvin76690a12002-04-30 20:52:49 +0000432
433/*
H. Peter Anvin734b1882002-04-30 21:01:08 +0000434 * Tokens are allocated in blocks to improve speed
435 */
436#define TOKEN_BLOCKSIZE 4096
437static Token *freeTokens = NULL;
H. Peter Anvince616072002-04-30 21:02:23 +0000438struct Blocks {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000439 Blocks *next;
440 void *chunk;
H. Peter Anvince616072002-04-30 21:02:23 +0000441};
442
443static Blocks blocks = { NULL, NULL };
H. Peter Anvin734b1882002-04-30 21:01:08 +0000444
445/*
H. Peter Anvin76690a12002-04-30 20:52:49 +0000446 * Forward declarations.
447 */
H. Peter Anvin734b1882002-04-30 21:01:08 +0000448static Token *expand_mmac_params(Token * tline);
449static Token *expand_smacro(Token * tline);
450static Token *expand_id(Token * tline);
H. Peter Anvinf8ad5322009-02-21 17:55:08 -0800451static Context *get_ctx(const char *name, const char **namep,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300452 bool all_contexts);
Keith Kaniosa5fc6462007-10-13 07:09:22 -0700453static void make_tok_num(Token * tok, int64_t val);
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000454static void error(int severity, const char *fmt, ...);
Victor van den Elzen3b404c02008-09-18 13:51:36 +0200455static void error_precond(int severity, const char *fmt, ...);
H. Peter Anvince616072002-04-30 21:02:23 +0000456static void *new_Block(size_t size);
457static void delete_Blocks(void);
H. Peter Anvinc751e862008-06-09 10:18:45 -0700458static Token *new_Token(Token * next, enum pp_token_type type,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300459 const char *text, int txtlen);
H. Peter Anvin734b1882002-04-30 21:01:08 +0000460static Token *delete_Token(Token * t);
H. Peter Anvineba20a72002-04-30 20:53:55 +0000461
462/*
463 * Macros for safe checking of token pointers, avoid *(NULL)
464 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300465#define tok_type_(x,t) ((x) && (x)->type == (t))
466#define skip_white_(x) if (tok_type_((x), TOK_WHITESPACE)) (x)=(x)->next
467#define tok_is_(x,v) (tok_type_((x), TOK_OTHER) && !strcmp((x)->text,(v)))
468#define tok_isnt_(x,v) ((x) && ((x)->type!=TOK_OTHER || strcmp((x)->text,(v))))
H. Peter Anvin76690a12002-04-30 20:52:49 +0000469
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300470/*
471 * Handle TASM specific directives, which do not contain a % in
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000472 * front of them. We do it here because I could not find any other
473 * place to do it for the moment, and it is a hack (ideally it would
474 * be nice to be able to use the NASM pre-processor to do it).
475 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000476static char *check_tasm_directive(char *line)
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000477{
Keith Kaniosb7a89542007-04-12 02:40:54 +0000478 int32_t i, j, k, m, len;
Cyrill Gorcunovf66ac7d2009-10-12 20:41:13 +0400479 char *p, *q, *oldline, oldchar;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000480
Cyrill Gorcunovf66ac7d2009-10-12 20:41:13 +0400481 p = nasm_skip_spaces(line);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000482
483 /* Binary search for the directive name */
484 i = -1;
Cyrill Gorcunova7319242010-06-03 22:04:36 +0400485 j = ARRAY_SIZE(tasm_directives);
Cyrill Gorcunovf66ac7d2009-10-12 20:41:13 +0400486 q = nasm_skip_word(p);
487 len = q - p;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000488 if (len) {
489 oldchar = p[len];
490 p[len] = 0;
491 while (j - i > 1) {
492 k = (j + i) / 2;
493 m = nasm_stricmp(p, tasm_directives[k]);
494 if (m == 0) {
495 /* We have found a directive, so jam a % in front of it
496 * so that NASM will then recognise it as one if it's own.
497 */
498 p[len] = oldchar;
499 len = strlen(p);
500 oldline = line;
501 line = nasm_malloc(len + 2);
502 line[0] = '%';
503 if (k == TM_IFDIFI) {
H. Peter Anvin18f48792009-06-27 15:56:27 -0700504 /*
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300505 * NASM does not recognise IFDIFI, so we convert
506 * it to %if 0. This is not used in NASM
507 * compatible code, but does need to parse for the
508 * TASM macro package.
H. Peter Anvine2c80182005-01-15 22:15:51 +0000509 */
H. Peter Anvin18f48792009-06-27 15:56:27 -0700510 strcpy(line + 1, "if 0");
H. Peter Anvine2c80182005-01-15 22:15:51 +0000511 } else {
512 memcpy(line + 1, p, len + 1);
513 }
514 nasm_free(oldline);
515 return line;
516 } else if (m < 0) {
517 j = k;
518 } else
519 i = k;
520 }
521 p[len] = oldchar;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000522 }
523 return line;
524}
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000525
H. Peter Anvin76690a12002-04-30 20:52:49 +0000526/*
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000527 * The pre-preprocessing stage... This function translates line
528 * number indications as they emerge from GNU cpp (`# lineno "file"
529 * flags') into NASM preprocessor line number indications (`%line
530 * lineno file').
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000531 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000532static char *prepreproc(char *line)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000533{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000534 int lineno, fnlen;
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000535 char *fname, *oldline;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000536
H. Peter Anvine2c80182005-01-15 22:15:51 +0000537 if (line[0] == '#' && line[1] == ' ') {
538 oldline = line;
539 fname = oldline + 2;
540 lineno = atoi(fname);
541 fname += strspn(fname, "0123456789 ");
542 if (*fname == '"')
543 fname++;
544 fnlen = strcspn(fname, "\"");
545 line = nasm_malloc(20 + fnlen);
546 snprintf(line, 20 + fnlen, "%%line %d %.*s", lineno, fnlen, fname);
547 nasm_free(oldline);
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000548 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000549 if (tasm_compatible_mode)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000550 return check_tasm_directive(line);
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000551 return line;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000552}
553
554/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000555 * Free a linked list of tokens.
556 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000557static void free_tlist(Token * list)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000558{
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +0400559 while (list)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000560 list = delete_Token(list);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000561}
562
563/*
564 * Free a linked list of lines.
565 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000566static void free_llist(Line * list)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000567{
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +0400568 Line *l, *tmp;
569 list_for_each_safe(l, tmp, list) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000570 free_tlist(l->first);
571 nasm_free(l);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000572 }
573}
574
575/*
H. Peter Anvineba20a72002-04-30 20:53:55 +0000576 * Free an MMacro
577 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000578static void free_mmacro(MMacro * m)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000579{
H. Peter Anvin734b1882002-04-30 21:01:08 +0000580 nasm_free(m->name);
581 free_tlist(m->dlist);
582 nasm_free(m->defaults);
583 free_llist(m->expansion);
584 nasm_free(m);
H. Peter Anvineba20a72002-04-30 20:53:55 +0000585}
586
587/*
H. Peter Anvin97a23472007-09-16 17:57:25 -0700588 * Free all currently defined macros, and free the hash tables
589 */
H. Peter Anvin072771e2008-05-22 13:17:51 -0700590static void free_smacro_table(struct hash_table *smt)
H. Peter Anvin97a23472007-09-16 17:57:25 -0700591{
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +0400592 SMacro *s, *tmp;
H. Peter Anvin072771e2008-05-22 13:17:51 -0700593 const char *key;
594 struct hash_tbl_node *it = NULL;
H. Peter Anvin97a23472007-09-16 17:57:25 -0700595
H. Peter Anvin072771e2008-05-22 13:17:51 -0700596 while ((s = hash_iterate(smt, &it, &key)) != NULL) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300597 nasm_free((void *)key);
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +0400598 list_for_each_safe(s, tmp, s) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300599 nasm_free(s->name);
600 free_tlist(s->expansion);
601 nasm_free(s);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300602 }
H. Peter Anvin97a23472007-09-16 17:57:25 -0700603 }
H. Peter Anvin072771e2008-05-22 13:17:51 -0700604 hash_free(smt);
605}
606
607static void free_mmacro_table(struct hash_table *mmt)
608{
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +0400609 MMacro *m, *tmp;
H. Peter Anvin072771e2008-05-22 13:17:51 -0700610 const char *key;
611 struct hash_tbl_node *it = NULL;
H. Peter Anvin97a23472007-09-16 17:57:25 -0700612
613 it = NULL;
H. Peter Anvin072771e2008-05-22 13:17:51 -0700614 while ((m = hash_iterate(mmt, &it, &key)) != NULL) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300615 nasm_free((void *)key);
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +0400616 list_for_each_safe(m ,tmp, m)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300617 free_mmacro(m);
H. Peter Anvin97a23472007-09-16 17:57:25 -0700618 }
H. Peter Anvin072771e2008-05-22 13:17:51 -0700619 hash_free(mmt);
620}
621
622static void free_macros(void)
623{
H. Peter Anvin166c2472008-05-28 12:28:58 -0700624 free_smacro_table(&smacros);
625 free_mmacro_table(&mmacros);
H. Peter Anvin97a23472007-09-16 17:57:25 -0700626}
627
628/*
629 * Initialize the hash tables
630 */
631static void init_macros(void)
632{
H. Peter Anvin166c2472008-05-28 12:28:58 -0700633 hash_init(&smacros, HASH_LARGE);
634 hash_init(&mmacros, HASH_LARGE);
H. Peter Anvin97a23472007-09-16 17:57:25 -0700635}
636
637/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000638 * Pop the context stack.
639 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000640static void ctx_pop(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000641{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000642 Context *c = cstk;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000643
644 cstk = cstk->next;
H. Peter Anvin166c2472008-05-28 12:28:58 -0700645 free_smacro_table(&c->localmac);
H. Peter Anvin734b1882002-04-30 21:01:08 +0000646 nasm_free(c->name);
647 nasm_free(c);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000648}
649
H. Peter Anvin072771e2008-05-22 13:17:51 -0700650/*
651 * Search for a key in the hash index; adding it if necessary
652 * (in which case we initialize the data pointer to NULL.)
653 */
654static void **
655hash_findi_add(struct hash_table *hash, const char *str)
656{
657 struct hash_insert hi;
658 void **r;
659 char *strx;
660
661 r = hash_findi(hash, str, &hi);
662 if (r)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300663 return r;
H. Peter Anvin072771e2008-05-22 13:17:51 -0700664
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300665 strx = nasm_strdup(str); /* Use a more efficient allocator here? */
H. Peter Anvin072771e2008-05-22 13:17:51 -0700666 return hash_add(&hi, strx, NULL);
667}
668
669/*
670 * Like hash_findi, but returns the data element rather than a pointer
671 * to it. Used only when not adding a new element, hence no third
672 * argument.
673 */
674static void *
675hash_findix(struct hash_table *hash, const char *str)
676{
677 void **p;
678
679 p = hash_findi(hash, str, NULL);
680 return p ? *p : NULL;
681}
682
Cyrill Gorcunov15bdc512010-07-13 11:27:41 +0400683/*
684 * read line from standart macros set,
685 * if there no more left -- return NULL
686 */
687static char *line_from_stdmac(void)
688{
689 unsigned char c;
690 const unsigned char *p = stdmacpos;
691 char *line, *q;
692 size_t len = 0;
693
694 if (!stdmacpos)
695 return NULL;
696
697 while ((c = *p++)) {
698 if (c >= 0x80)
699 len += pp_directives_len[c - 0x80] + 1;
700 else
701 len++;
702 }
703
704 line = nasm_malloc(len + 1);
705 q = line;
706 while ((c = *stdmacpos++)) {
707 if (c >= 0x80) {
708 memcpy(q, pp_directives[c - 0x80], pp_directives_len[c - 0x80]);
709 q += pp_directives_len[c - 0x80];
710 *q++ = ' ';
711 } else {
712 *q++ = c;
713 }
714 }
715 stdmacpos = p;
716 *q = '\0';
717
718 if (!*stdmacpos) {
719 /* This was the last of the standard macro chain... */
720 stdmacpos = NULL;
721 if (any_extrastdmac) {
722 stdmacpos = extrastdmac;
723 any_extrastdmac = false;
724 } else if (do_predef) {
725 Line *pd, *l;
726 Token *head, **tail, *t;
727
728 /*
729 * Nasty hack: here we push the contents of
730 * `predef' on to the top-level expansion stack,
731 * since this is the most convenient way to
732 * implement the pre-include and pre-define
733 * features.
734 */
735 list_for_each(pd, predef) {
736 head = NULL;
737 tail = &head;
738 list_for_each(t, pd->first) {
739 *tail = new_Token(NULL, t->type, t->text, 0);
740 tail = &(*tail)->next;
741 }
742
743 l = nasm_malloc(sizeof(Line));
744 l->next = istk->expansion;
745 l->first = head;
746 l->finishes = NULL;
747
748 istk->expansion = l;
749 }
750 do_predef = false;
751 }
752 }
753
754 return line;
755}
756
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000757#define BUF_DELTA 512
758/*
759 * Read a line from the top file in istk, handling multiple CR/LFs
760 * at the end of the line read, and handling spurious ^Zs. Will
761 * return lines from the standard macro set if this has not already
762 * been done.
763 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000764static char *read_line(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000765{
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000766 char *buffer, *p, *q;
H. Peter Anvin9f394642002-04-30 21:07:51 +0000767 int bufsize, continued_count;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000768
Cyrill Gorcunov15bdc512010-07-13 11:27:41 +0400769 /*
770 * standart macros set (predefined) goes first
771 */
772 p = line_from_stdmac();
773 if (p)
774 return p;
H. Peter Anvin72edbb82008-06-19 16:00:04 -0700775
Cyrill Gorcunov15bdc512010-07-13 11:27:41 +0400776 /*
777 * regular read from a file
778 */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000779 bufsize = BUF_DELTA;
780 buffer = nasm_malloc(BUF_DELTA);
781 p = buffer;
H. Peter Anvin9f394642002-04-30 21:07:51 +0000782 continued_count = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000783 while (1) {
784 q = fgets(p, bufsize - (p - buffer), istk->fp);
785 if (!q)
786 break;
787 p += strlen(p);
788 if (p > buffer && p[-1] == '\n') {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300789 /*
790 * Convert backslash-CRLF line continuation sequences into
791 * nothing at all (for DOS and Windows)
792 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000793 if (((p - 2) > buffer) && (p[-3] == '\\') && (p[-2] == '\r')) {
794 p -= 3;
795 *p = 0;
796 continued_count++;
797 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300798 /*
799 * Also convert backslash-LF line continuation sequences into
800 * nothing at all (for Unix)
801 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000802 else if (((p - 1) > buffer) && (p[-2] == '\\')) {
803 p -= 2;
804 *p = 0;
805 continued_count++;
806 } else {
807 break;
808 }
809 }
810 if (p - buffer > bufsize - 10) {
Keith Kaniosb7a89542007-04-12 02:40:54 +0000811 int32_t offset = p - buffer;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000812 bufsize += BUF_DELTA;
813 buffer = nasm_realloc(buffer, bufsize);
814 p = buffer + offset; /* prevent stale-pointer problems */
815 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000816 }
817
H. Peter Anvine2c80182005-01-15 22:15:51 +0000818 if (!q && p == buffer) {
819 nasm_free(buffer);
820 return NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000821 }
822
H. Peter Anvine2c80182005-01-15 22:15:51 +0000823 src_set_linnum(src_get_linnum() + istk->lineinc +
824 (continued_count * istk->lineinc));
H. Peter Anvineba20a72002-04-30 20:53:55 +0000825
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000826 /*
827 * Play safe: remove CRs as well as LFs, if any of either are
828 * present at the end of the line.
829 */
H. Peter Anvineba20a72002-04-30 20:53:55 +0000830 while (--p >= buffer && (*p == '\n' || *p == '\r'))
H. Peter Anvine2c80182005-01-15 22:15:51 +0000831 *p = '\0';
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000832
833 /*
834 * Handle spurious ^Z, which may be inserted into source files
835 * by some file transfer utilities.
836 */
837 buffer[strcspn(buffer, "\032")] = '\0';
838
H. Peter Anvin734b1882002-04-30 21:01:08 +0000839 list->line(LIST_READ, buffer);
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000840
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000841 return buffer;
842}
843
844/*
Keith Kaniosb7a89542007-04-12 02:40:54 +0000845 * Tokenize a line of text. This is a very simple process since we
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000846 * don't need to parse the value out of e.g. numeric tokens: we
847 * simply split one string into many.
848 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000849static Token *tokenize(char *line)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000850{
H. Peter Anvinca544db2008-10-19 19:30:11 -0700851 char c, *p = line;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +0000852 enum pp_token_type type;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000853 Token *list = NULL;
854 Token *t, **tail = &list;
855
H. Peter Anvine2c80182005-01-15 22:15:51 +0000856 while (*line) {
857 p = line;
858 if (*p == '%') {
859 p++;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300860 if (*p == '+' && !nasm_isdigit(p[1])) {
861 p++;
862 type = TOK_PASTE;
863 } else if (nasm_isdigit(*p) ||
864 ((*p == '-' || *p == '+') && nasm_isdigit(p[1]))) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000865 do {
866 p++;
867 }
H. Peter Anvinbda7a6e2008-06-21 10:23:17 -0700868 while (nasm_isdigit(*p));
H. Peter Anvine2c80182005-01-15 22:15:51 +0000869 type = TOK_PREPROC_ID;
870 } else if (*p == '{') {
871 p++;
872 while (*p && *p != '}') {
873 p[-1] = *p;
874 p++;
875 }
876 p[-1] = '\0';
877 if (*p)
878 p++;
879 type = TOK_PREPROC_ID;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300880 } else if (*p == '[') {
881 int lvl = 1;
882 line += 2; /* Skip the leading %[ */
883 p++;
884 while (lvl && (c = *p++)) {
885 switch (c) {
886 case ']':
887 lvl--;
888 break;
889 case '%':
890 if (*p == '[')
891 lvl++;
892 break;
893 case '\'':
894 case '\"':
895 case '`':
896 p = nasm_skip_string(p)+1;
897 break;
898 default:
899 break;
900 }
901 }
902 p--;
903 if (*p)
904 *p++ = '\0';
905 if (lvl)
906 error(ERR_NONFATAL, "unterminated %[ construct");
907 type = TOK_INDIRECT;
908 } else if (*p == '?') {
909 type = TOK_PREPROC_Q; /* %? */
910 p++;
911 if (*p == '?') {
912 type = TOK_PREPROC_QQ; /* %?? */
913 p++;
914 }
H. Peter Anvine2c80182005-01-15 22:15:51 +0000915 } else if (isidchar(*p) ||
916 ((*p == '!' || *p == '%' || *p == '$') &&
917 isidchar(p[1]))) {
918 do {
919 p++;
920 }
921 while (isidchar(*p));
922 type = TOK_PREPROC_ID;
923 } else {
924 type = TOK_OTHER;
925 if (*p == '%')
926 p++;
927 }
928 } else if (isidstart(*p) || (*p == '$' && isidstart(p[1]))) {
929 type = TOK_ID;
930 p++;
931 while (*p && isidchar(*p))
932 p++;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -0700933 } else if (*p == '\'' || *p == '"' || *p == '`') {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000934 /*
935 * A string token.
936 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000937 type = TOK_STRING;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300938 p = nasm_skip_string(p);
Nickolay Yurchenkof3b3ce22003-09-21 20:38:43 +0000939
H. Peter Anvine2c80182005-01-15 22:15:51 +0000940 if (*p) {
941 p++;
942 } else {
H. Peter Anvin917a3492008-09-24 09:14:49 -0700943 error(ERR_WARNING|ERR_PASS1, "unterminated string");
H. Peter Anvine2c80182005-01-15 22:15:51 +0000944 /* Handling unterminated strings by UNV */
945 /* type = -1; */
946 }
Victor van den Elzenfb5f2512009-04-17 16:17:59 +0200947 } else if (p[0] == '$' && p[1] == '$') {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300948 type = TOK_OTHER; /* TOKEN_BASE */
Victor van den Elzenfb5f2512009-04-17 16:17:59 +0200949 p += 2;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000950 } else if (isnumstart(*p)) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300951 bool is_hex = false;
952 bool is_float = false;
953 bool has_e = false;
954 char c, *r;
H. Peter Anvinc2df2822007-10-24 15:29:28 -0700955
H. Peter Anvine2c80182005-01-15 22:15:51 +0000956 /*
H. Peter Anvinc2df2822007-10-24 15:29:28 -0700957 * A numeric token.
H. Peter Anvine2c80182005-01-15 22:15:51 +0000958 */
H. Peter Anvinc2df2822007-10-24 15:29:28 -0700959
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300960 if (*p == '$') {
961 p++;
962 is_hex = true;
963 }
H. Peter Anvinc2df2822007-10-24 15:29:28 -0700964
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300965 for (;;) {
966 c = *p++;
H. Peter Anvinc2df2822007-10-24 15:29:28 -0700967
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300968 if (!is_hex && (c == 'e' || c == 'E')) {
969 has_e = true;
970 if (*p == '+' || *p == '-') {
971 /*
972 * e can only be followed by +/- if it is either a
973 * prefixed hex number or a floating-point number
974 */
975 p++;
976 is_float = true;
977 }
978 } else if (c == 'H' || c == 'h' || c == 'X' || c == 'x') {
979 is_hex = true;
980 } else if (c == 'P' || c == 'p') {
981 is_float = true;
982 if (*p == '+' || *p == '-')
983 p++;
984 } else if (isnumchar(c) || c == '_')
985 ; /* just advance */
986 else if (c == '.') {
987 /*
988 * we need to deal with consequences of the legacy
989 * parser, like "1.nolist" being two tokens
990 * (TOK_NUMBER, TOK_ID) here; at least give it
991 * a shot for now. In the future, we probably need
992 * a flex-based scanner with proper pattern matching
993 * to do it as well as it can be done. Nothing in
994 * the world is going to help the person who wants
995 * 0x123.p16 interpreted as two tokens, though.
996 */
997 r = p;
998 while (*r == '_')
999 r++;
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001000
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001001 if (nasm_isdigit(*r) || (is_hex && nasm_isxdigit(*r)) ||
1002 (!is_hex && (*r == 'e' || *r == 'E')) ||
1003 (*r == 'p' || *r == 'P')) {
1004 p = r;
1005 is_float = true;
1006 } else
1007 break; /* Terminate the token */
1008 } else
1009 break;
1010 }
1011 p--; /* Point to first character beyond number */
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001012
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001013 if (p == line+1 && *line == '$') {
1014 type = TOK_OTHER; /* TOKEN_HERE */
1015 } else {
1016 if (has_e && !is_hex) {
1017 /* 1e13 is floating-point, but 1e13h is not */
1018 is_float = true;
1019 }
H. Peter Anvind784a082009-04-20 14:01:18 -07001020
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001021 type = is_float ? TOK_FLOAT : TOK_NUMBER;
1022 }
H. Peter Anvinbda7a6e2008-06-21 10:23:17 -07001023 } else if (nasm_isspace(*p)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001024 type = TOK_WHITESPACE;
Cyrill Gorcunovf66ac7d2009-10-12 20:41:13 +04001025 p = nasm_skip_spaces(p);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001026 /*
1027 * Whitespace just before end-of-line is discarded by
1028 * pretending it's a comment; whitespace just before a
1029 * comment gets lumped into the comment.
1030 */
1031 if (!*p || *p == ';') {
1032 type = TOK_COMMENT;
1033 while (*p)
1034 p++;
1035 }
1036 } else if (*p == ';') {
1037 type = TOK_COMMENT;
1038 while (*p)
1039 p++;
1040 } else {
1041 /*
1042 * Anything else is an operator of some kind. We check
1043 * for all the double-character operators (>>, <<, //,
1044 * %%, <=, >=, ==, !=, <>, &&, ||, ^^), but anything
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001045 * else is a single-character operator.
H. Peter Anvine2c80182005-01-15 22:15:51 +00001046 */
1047 type = TOK_OTHER;
1048 if ((p[0] == '>' && p[1] == '>') ||
1049 (p[0] == '<' && p[1] == '<') ||
1050 (p[0] == '/' && p[1] == '/') ||
1051 (p[0] == '<' && p[1] == '=') ||
1052 (p[0] == '>' && p[1] == '=') ||
1053 (p[0] == '=' && p[1] == '=') ||
1054 (p[0] == '!' && p[1] == '=') ||
1055 (p[0] == '<' && p[1] == '>') ||
1056 (p[0] == '&' && p[1] == '&') ||
1057 (p[0] == '|' && p[1] == '|') ||
1058 (p[0] == '^' && p[1] == '^')) {
1059 p++;
1060 }
1061 p++;
1062 }
Nickolay Yurchenkof3b3ce22003-09-21 20:38:43 +00001063
H. Peter Anvine2c80182005-01-15 22:15:51 +00001064 /* Handling unterminated string by UNV */
1065 /*if (type == -1)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001066 {
1067 *tail = t = new_Token(NULL, TOK_STRING, line, p-line+1);
1068 t->text[p-line] = *line;
1069 tail = &t->next;
1070 }
1071 else */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001072 if (type != TOK_COMMENT) {
1073 *tail = t = new_Token(NULL, type, line, p - line);
1074 tail = &t->next;
1075 }
1076 line = p;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001077 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001078 return list;
1079}
1080
H. Peter Anvince616072002-04-30 21:02:23 +00001081/*
1082 * this function allocates a new managed block of memory and
H. Peter Anvin70653092007-10-19 14:42:29 -07001083 * returns a pointer to the block. The managed blocks are
H. Peter Anvince616072002-04-30 21:02:23 +00001084 * deleted only all at once by the delete_Blocks function.
1085 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001086static void *new_Block(size_t size)
H. Peter Anvince616072002-04-30 21:02:23 +00001087{
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001088 Blocks *b = &blocks;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001089
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001090 /* first, get to the end of the linked list */
1091 while (b->next)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001092 b = b->next;
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001093 /* now allocate the requested chunk */
1094 b->chunk = nasm_malloc(size);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001095
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001096 /* now allocate a new block for the next request */
1097 b->next = nasm_malloc(sizeof(Blocks));
1098 /* and initialize the contents of the new block */
1099 b->next->next = NULL;
1100 b->next->chunk = NULL;
1101 return b->chunk;
H. Peter Anvince616072002-04-30 21:02:23 +00001102}
1103
1104/*
1105 * this function deletes all managed blocks of memory
1106 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001107static void delete_Blocks(void)
H. Peter Anvince616072002-04-30 21:02:23 +00001108{
H. Peter Anvine2c80182005-01-15 22:15:51 +00001109 Blocks *a, *b = &blocks;
H. Peter Anvince616072002-04-30 21:02:23 +00001110
H. Peter Anvin70653092007-10-19 14:42:29 -07001111 /*
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001112 * keep in mind that the first block, pointed to by blocks
H. Peter Anvin70653092007-10-19 14:42:29 -07001113 * is a static and not dynamically allocated, so we don't
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001114 * free it.
1115 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001116 while (b) {
1117 if (b->chunk)
1118 nasm_free(b->chunk);
1119 a = b;
1120 b = b->next;
1121 if (a != &blocks)
1122 nasm_free(a);
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001123 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001124}
H. Peter Anvin734b1882002-04-30 21:01:08 +00001125
1126/*
H. Peter Anvin70653092007-10-19 14:42:29 -07001127 * this function creates a new Token and passes a pointer to it
H. Peter Anvin734b1882002-04-30 21:01:08 +00001128 * back to the caller. It sets the type and text elements, and
H. Peter Anvinf26e0972008-07-01 21:26:27 -07001129 * also the a.mac and next elements to NULL.
H. Peter Anvin734b1882002-04-30 21:01:08 +00001130 */
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07001131static Token *new_Token(Token * next, enum pp_token_type type,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001132 const char *text, int txtlen)
H. Peter Anvin734b1882002-04-30 21:01:08 +00001133{
1134 Token *t;
1135 int i;
1136
H. Peter Anvin89cee572009-07-15 09:16:54 -04001137 if (!freeTokens) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001138 freeTokens = (Token *) new_Block(TOKEN_BLOCKSIZE * sizeof(Token));
1139 for (i = 0; i < TOKEN_BLOCKSIZE - 1; i++)
1140 freeTokens[i].next = &freeTokens[i + 1];
1141 freeTokens[i].next = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001142 }
1143 t = freeTokens;
1144 freeTokens = t->next;
1145 t->next = next;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07001146 t->a.mac = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001147 t->type = type;
H. Peter Anvin89cee572009-07-15 09:16:54 -04001148 if (type == TOK_WHITESPACE || !text) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001149 t->text = NULL;
1150 } else {
1151 if (txtlen == 0)
1152 txtlen = strlen(text);
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07001153 t->text = nasm_malloc(txtlen+1);
1154 memcpy(t->text, text, txtlen);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001155 t->text[txtlen] = '\0';
H. Peter Anvin734b1882002-04-30 21:01:08 +00001156 }
1157 return t;
1158}
1159
H. Peter Anvine2c80182005-01-15 22:15:51 +00001160static Token *delete_Token(Token * t)
H. Peter Anvin734b1882002-04-30 21:01:08 +00001161{
1162 Token *next = t->next;
1163 nasm_free(t->text);
H. Peter Anvin788e6c12002-04-30 21:02:01 +00001164 t->next = freeTokens;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001165 freeTokens = t;
1166 return next;
1167}
1168
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001169/*
1170 * Convert a line of tokens back into text.
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001171 * If expand_locals is not zero, identifiers of the form "%$*xxx"
1172 * will be transformed into ..@ctxnum.xxx
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001173 */
H. Peter Anvin9e200162008-06-04 17:23:14 -07001174static char *detoken(Token * tlist, bool expand_locals)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001175{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001176 Token *t;
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001177 char *line, *p;
H. Peter Anvinb4daadc2008-01-21 16:31:57 -08001178 const char *q;
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001179 int len = 0;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001180
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001181 list_for_each(t, tlist) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001182 if (t->type == TOK_PREPROC_ID && t->text[1] == '!') {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001183 char *p = getenv(t->text + 2);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001184 nasm_free(t->text);
1185 if (p)
1186 t->text = nasm_strdup(p);
1187 else
1188 t->text = NULL;
1189 }
1190 /* Expand local macros here and not during preprocessing */
1191 if (expand_locals &&
1192 t->type == TOK_PREPROC_ID && t->text &&
1193 t->text[0] == '%' && t->text[1] == '$') {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001194 const char *q;
1195 char *p;
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001196 Context *ctx = get_ctx(t->text, &q, false);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001197 if (ctx) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001198 char buffer[40];
Keith Kanios93f2e9a2007-04-14 00:10:59 +00001199 snprintf(buffer, sizeof(buffer), "..@%"PRIu32".", ctx->number);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001200 p = nasm_strcat(buffer, q);
1201 nasm_free(t->text);
1202 t->text = p;
1203 }
1204 }
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001205 if (t->type == TOK_WHITESPACE)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001206 len++;
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001207 else if (t->text)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001208 len += strlen(t->text);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001209 }
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001210
H. Peter Anvin734b1882002-04-30 21:01:08 +00001211 p = line = nasm_malloc(len + 1);
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001212
1213 list_for_each(t, tlist) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001214 if (t->type == TOK_WHITESPACE) {
H. Peter Anvinb4daadc2008-01-21 16:31:57 -08001215 *p++ = ' ';
H. Peter Anvine2c80182005-01-15 22:15:51 +00001216 } else if (t->text) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001217 q = t->text;
1218 while (*q)
1219 *p++ = *q++;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001220 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001221 }
1222 *p = '\0';
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001223
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001224 return line;
1225}
1226
1227/*
H. Peter Anvin76690a12002-04-30 20:52:49 +00001228 * A scanner, suitable for use by the expression evaluator, which
1229 * operates on a line of Tokens. Expects a pointer to a pointer to
1230 * the first token in the line to be passed in as its private_data
1231 * field.
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001232 *
1233 * FIX: This really needs to be unified with stdscan.
H. Peter Anvin76690a12002-04-30 20:52:49 +00001234 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001235static int ppscan(void *private_data, struct tokenval *tokval)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001236{
H. Peter Anvin76690a12002-04-30 20:52:49 +00001237 Token **tlineptr = private_data;
1238 Token *tline;
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001239 char ourcopy[MAX_KEYWORD+1], *p, *r, *s;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001240
H. Peter Anvine2c80182005-01-15 22:15:51 +00001241 do {
1242 tline = *tlineptr;
1243 *tlineptr = tline ? tline->next : NULL;
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04001244 } while (tline && (tline->type == TOK_WHITESPACE ||
1245 tline->type == TOK_COMMENT));
H. Peter Anvin76690a12002-04-30 20:52:49 +00001246
1247 if (!tline)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001248 return tokval->t_type = TOKEN_EOS;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001249
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001250 tokval->t_charptr = tline->text;
1251
H. Peter Anvin76690a12002-04-30 20:52:49 +00001252 if (tline->text[0] == '$' && !tline->text[1])
H. Peter Anvine2c80182005-01-15 22:15:51 +00001253 return tokval->t_type = TOKEN_HERE;
H. Peter Anvin7cf897e2002-05-30 21:30:33 +00001254 if (tline->text[0] == '$' && tline->text[1] == '$' && !tline->text[2])
H. Peter Anvine2c80182005-01-15 22:15:51 +00001255 return tokval->t_type = TOKEN_BASE;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001256
H. Peter Anvine2c80182005-01-15 22:15:51 +00001257 if (tline->type == TOK_ID) {
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001258 p = tokval->t_charptr = tline->text;
1259 if (p[0] == '$') {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001260 tokval->t_charptr++;
1261 return tokval->t_type = TOKEN_ID;
1262 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00001263
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001264 for (r = p, s = ourcopy; *r; r++) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001265 if (r >= p+MAX_KEYWORD)
1266 return tokval->t_type = TOKEN_ID; /* Not a keyword */
H. Peter Anvinac8f8fc2008-06-11 15:49:41 -07001267 *s++ = nasm_tolower(*r);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001268 }
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001269 *s = '\0';
1270 /* right, so we have an identifier sitting in temp storage. now,
1271 * is it actually a register or instruction name, or what? */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001272 return nasm_token_hash(ourcopy, tokval);
H. Peter Anvin76690a12002-04-30 20:52:49 +00001273 }
1274
H. Peter Anvine2c80182005-01-15 22:15:51 +00001275 if (tline->type == TOK_NUMBER) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001276 bool rn_error;
1277 tokval->t_integer = readnum(tline->text, &rn_error);
1278 tokval->t_charptr = tline->text;
1279 if (rn_error)
1280 return tokval->t_type = TOKEN_ERRNUM;
1281 else
1282 return tokval->t_type = TOKEN_NUM;
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001283 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00001284
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001285 if (tline->type == TOK_FLOAT) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001286 return tokval->t_type = TOKEN_FLOAT;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001287 }
1288
H. Peter Anvine2c80182005-01-15 22:15:51 +00001289 if (tline->type == TOK_STRING) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001290 char bq, *ep;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001291
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001292 bq = tline->text[0];
H. Peter Anvin11627042008-06-09 20:45:19 -07001293 tokval->t_charptr = tline->text;
1294 tokval->t_inttwo = nasm_unquote(tline->text, &ep);
H. Peter Anvind2456592008-06-19 15:04:18 -07001295
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001296 if (ep[0] != bq || ep[1] != '\0')
1297 return tokval->t_type = TOKEN_ERRSTR;
1298 else
1299 return tokval->t_type = TOKEN_STR;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001300 }
1301
H. Peter Anvine2c80182005-01-15 22:15:51 +00001302 if (tline->type == TOK_OTHER) {
1303 if (!strcmp(tline->text, "<<"))
1304 return tokval->t_type = TOKEN_SHL;
1305 if (!strcmp(tline->text, ">>"))
1306 return tokval->t_type = TOKEN_SHR;
1307 if (!strcmp(tline->text, "//"))
1308 return tokval->t_type = TOKEN_SDIV;
1309 if (!strcmp(tline->text, "%%"))
1310 return tokval->t_type = TOKEN_SMOD;
1311 if (!strcmp(tline->text, "=="))
1312 return tokval->t_type = TOKEN_EQ;
1313 if (!strcmp(tline->text, "<>"))
1314 return tokval->t_type = TOKEN_NE;
1315 if (!strcmp(tline->text, "!="))
1316 return tokval->t_type = TOKEN_NE;
1317 if (!strcmp(tline->text, "<="))
1318 return tokval->t_type = TOKEN_LE;
1319 if (!strcmp(tline->text, ">="))
1320 return tokval->t_type = TOKEN_GE;
1321 if (!strcmp(tline->text, "&&"))
1322 return tokval->t_type = TOKEN_DBL_AND;
1323 if (!strcmp(tline->text, "^^"))
1324 return tokval->t_type = TOKEN_DBL_XOR;
1325 if (!strcmp(tline->text, "||"))
1326 return tokval->t_type = TOKEN_DBL_OR;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001327 }
1328
1329 /*
1330 * We have no other options: just return the first character of
1331 * the token text.
1332 */
1333 return tokval->t_type = tline->text[0];
1334}
1335
1336/*
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001337 * Compare a string to the name of an existing macro; this is a
1338 * simple wrapper which calls either strcmp or nasm_stricmp
1339 * depending on the value of the `casesense' parameter.
1340 */
H. Peter Anvin4db5a162007-10-11 13:42:09 -07001341static int mstrcmp(const char *p, const char *q, bool casesense)
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001342{
H. Peter Anvin734b1882002-04-30 21:01:08 +00001343 return casesense ? strcmp(p, q) : nasm_stricmp(p, q);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001344}
1345
1346/*
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07001347 * Compare a string to the name of an existing macro; this is a
1348 * simple wrapper which calls either strcmp or nasm_stricmp
1349 * depending on the value of the `casesense' parameter.
1350 */
1351static int mmemcmp(const char *p, const char *q, size_t l, bool casesense)
1352{
1353 return casesense ? memcmp(p, q, l) : nasm_memicmp(p, q, l);
1354}
1355
1356/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001357 * Return the Context structure associated with a %$ token. Return
1358 * NULL, having _already_ reported an error condition, if the
1359 * context stack isn't deep enough for the supplied number of $
1360 * signs.
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001361 * If all_contexts == true, contexts that enclose current are
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001362 * also scanned for such smacro, until it is found; if not -
1363 * only the context that directly results from the number of $'s
1364 * in variable's name.
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001365 *
1366 * If "namep" is non-NULL, set it to the pointer to the macro name
1367 * tail, i.e. the part beyond %$...
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001368 */
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001369static Context *get_ctx(const char *name, const char **namep,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001370 bool all_contexts)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001371{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001372 Context *ctx;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001373 SMacro *m;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001374 int i;
1375
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001376 if (namep)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001377 *namep = name;
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001378
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001379 if (!name || name[0] != '%' || name[1] != '$')
H. Peter Anvine2c80182005-01-15 22:15:51 +00001380 return NULL;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001381
H. Peter Anvine2c80182005-01-15 22:15:51 +00001382 if (!cstk) {
1383 error(ERR_NONFATAL, "`%s': context stack is empty", name);
1384 return NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001385 }
1386
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001387 name += 2;
1388 ctx = cstk;
1389 i = 0;
1390 while (ctx && *name == '$') {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001391 name++;
1392 i++;
1393 ctx = ctx->next;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001394 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001395 if (!ctx) {
1396 error(ERR_NONFATAL, "`%s': context stack is only"
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001397 " %d level%s deep", name, i, (i == 1 ? "" : "s"));
H. Peter Anvine2c80182005-01-15 22:15:51 +00001398 return NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001399 }
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001400
1401 if (namep)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001402 *namep = name;
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001403
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001404 if (!all_contexts)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001405 return ctx;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001406
H. Peter Anvine2c80182005-01-15 22:15:51 +00001407 do {
1408 /* Search for this smacro in found context */
H. Peter Anvin166c2472008-05-28 12:28:58 -07001409 m = hash_findix(&ctx->localmac, name);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001410 while (m) {
1411 if (!mstrcmp(m->name, name, m->casesense))
1412 return ctx;
1413 m = m->next;
1414 }
1415 ctx = ctx->next;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001416 }
1417 while (ctx);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001418 return NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001419}
1420
1421/*
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001422 * Check to see if a file is already in a string list
1423 */
1424static bool in_list(const StrList *list, const char *str)
1425{
1426 while (list) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001427 if (!strcmp(list->str, str))
1428 return true;
1429 list = list->next;
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001430 }
1431 return false;
1432}
1433
1434/*
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001435 * Open an include file. This routine must always return a valid
1436 * file pointer if it returns - it's responsible for throwing an
1437 * ERR_FATAL and bombing out completely if not. It should also try
1438 * the include path one by one until it finds the file or reaches
1439 * the end of the path.
1440 */
H. Peter Anvin2b1c3b92008-06-06 10:38:46 -07001441static FILE *inc_fopen(const char *file, StrList **dhead, StrList ***dtail,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001442 bool missing_ok)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001443{
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001444 FILE *fp;
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001445 char *prefix = "";
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001446 IncPath *ip = ipath;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001447 int len = strlen(file);
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001448 size_t prefix_len = 0;
1449 StrList *sl;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001450
H. Peter Anvine2c80182005-01-15 22:15:51 +00001451 while (1) {
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001452 sl = nasm_malloc(prefix_len+len+1+sizeof sl->next);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001453 memcpy(sl->str, prefix, prefix_len);
1454 memcpy(sl->str+prefix_len, file, len+1);
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001455 fp = fopen(sl->str, "r");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001456 if (fp && dhead && !in_list(*dhead, sl->str)) {
1457 sl->next = NULL;
1458 **dtail = sl;
1459 *dtail = &sl->next;
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001460 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001461 nasm_free(sl);
1462 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001463 if (fp)
1464 return fp;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001465 if (!ip) {
1466 if (!missing_ok)
1467 break;
1468 prefix = NULL;
1469 } else {
1470 prefix = ip->path;
1471 ip = ip->next;
1472 }
1473 if (prefix) {
1474 prefix_len = strlen(prefix);
1475 } else {
1476 /* -MG given and file not found */
1477 if (dhead && !in_list(*dhead, file)) {
1478 sl = nasm_malloc(len+1+sizeof sl->next);
1479 sl->next = NULL;
1480 strcpy(sl->str, file);
1481 **dtail = sl;
1482 *dtail = &sl->next;
1483 }
1484 return NULL;
1485 }
H. Peter Anvineba20a72002-04-30 20:53:55 +00001486 }
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001487
H. Peter Anvin734b1882002-04-30 21:01:08 +00001488 error(ERR_FATAL, "unable to open include file `%s'", file);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001489 return NULL;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001490}
1491
1492/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001493 * Determine if we should warn on defining a single-line macro of
H. Peter Anvinef7468f2002-04-30 20:57:59 +00001494 * name `name', with `nparam' parameters. If nparam is 0 or -1, will
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001495 * return true if _any_ single-line macro of that name is defined.
1496 * Otherwise, will return true if a single-line macro with either
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001497 * `nparam' or no parameters is defined.
1498 *
1499 * If a macro with precisely the right number of parameters is
H. Peter Anvinef7468f2002-04-30 20:57:59 +00001500 * defined, or nparam is -1, the address of the definition structure
1501 * will be returned in `defn'; otherwise NULL will be returned. If `defn'
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001502 * is NULL, no action will be taken regarding its contents, and no
1503 * error will occur.
1504 *
1505 * Note that this is also called with nparam zero to resolve
1506 * `ifdef'.
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001507 *
1508 * If you already know which context macro belongs to, you can pass
1509 * the context pointer as first parameter; if you won't but name begins
1510 * with %$ the context will be automatically computed. If all_contexts
1511 * is true, macro will be searched in outer contexts as well.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001512 */
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07001513static bool
H. Peter Anvinb2a5fda2008-06-19 21:42:42 -07001514smacro_defined(Context * ctx, const char *name, int nparam, SMacro ** defn,
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07001515 bool nocase)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001516{
H. Peter Anvin166c2472008-05-28 12:28:58 -07001517 struct hash_table *smtbl;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001518 SMacro *m;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001519
H. Peter Anvin97a23472007-09-16 17:57:25 -07001520 if (ctx) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001521 smtbl = &ctx->localmac;
H. Peter Anvin97a23472007-09-16 17:57:25 -07001522 } else if (name[0] == '%' && name[1] == '$') {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001523 if (cstk)
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001524 ctx = get_ctx(name, &name, false);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001525 if (!ctx)
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001526 return false; /* got to return _something_ */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001527 smtbl = &ctx->localmac;
H. Peter Anvin97a23472007-09-16 17:57:25 -07001528 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001529 smtbl = &smacros;
H. Peter Anvin97a23472007-09-16 17:57:25 -07001530 }
H. Peter Anvin166c2472008-05-28 12:28:58 -07001531 m = (SMacro *) hash_findix(smtbl, name);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001532
H. Peter Anvine2c80182005-01-15 22:15:51 +00001533 while (m) {
1534 if (!mstrcmp(m->name, name, m->casesense && nocase) &&
Charles Crayne192d5b52007-10-18 19:02:42 -07001535 (nparam <= 0 || m->nparam == 0 || nparam == (int) m->nparam)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001536 if (defn) {
Charles Crayne192d5b52007-10-18 19:02:42 -07001537 if (nparam == (int) m->nparam || nparam == -1)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001538 *defn = m;
1539 else
1540 *defn = NULL;
1541 }
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001542 return true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001543 }
1544 m = m->next;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001545 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001546
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001547 return false;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001548}
1549
1550/*
1551 * Count and mark off the parameters in a multi-line macro call.
1552 * This is called both from within the multi-line macro expansion
1553 * code, and also to mark off the default parameters when provided
1554 * in a %macro definition line.
1555 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001556static void count_mmac_params(Token * t, int *nparam, Token *** params)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001557{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001558 int paramsize, brace;
1559
1560 *nparam = paramsize = 0;
1561 *params = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001562 while (t) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001563 /* +1: we need space for the final NULL */
H. Peter Anvin91fb6f12008-09-01 10:56:33 -07001564 if (*nparam+1 >= paramsize) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001565 paramsize += PARAM_DELTA;
1566 *params = nasm_realloc(*params, sizeof(**params) * paramsize);
1567 }
1568 skip_white_(t);
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001569 brace = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001570 if (tok_is_(t, "{"))
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001571 brace = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001572 (*params)[(*nparam)++] = t;
1573 while (tok_isnt_(t, brace ? "}" : ","))
1574 t = t->next;
1575 if (t) { /* got a comma/brace */
1576 t = t->next;
1577 if (brace) {
1578 /*
1579 * Now we've found the closing brace, look further
1580 * for the comma.
1581 */
1582 skip_white_(t);
1583 if (tok_isnt_(t, ",")) {
1584 error(ERR_NONFATAL,
1585 "braces do not enclose all of macro parameter");
1586 while (tok_isnt_(t, ","))
1587 t = t->next;
1588 }
1589 if (t)
1590 t = t->next; /* eat the comma */
1591 }
1592 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001593 }
1594}
1595
1596/*
H. Peter Anvin76690a12002-04-30 20:52:49 +00001597 * Determine whether one of the various `if' conditions is true or
1598 * not.
1599 *
1600 * We must free the tline we get passed.
1601 */
H. Peter Anvin70055962007-10-11 00:05:31 -07001602static bool if_condition(Token * tline, enum preproc_token ct)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001603{
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001604 enum pp_conditional i = PP_COND(ct);
H. Peter Anvin70055962007-10-11 00:05:31 -07001605 bool j;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001606 Token *t, *tt, **tptr, *origline;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001607 struct tokenval tokval;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001608 expr *evalresult;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001609 enum pp_token_type needtype;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001610
1611 origline = tline;
1612
H. Peter Anvine2c80182005-01-15 22:15:51 +00001613 switch (i) {
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001614 case PPC_IFCTX:
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001615 j = false; /* have we matched yet? */
Victor van den Elzen0e857f12008-07-23 13:21:29 +02001616 while (true) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001617 skip_white_(tline);
Victor van den Elzen0e857f12008-07-23 13:21:29 +02001618 if (!tline)
1619 break;
1620 if (tline->type != TOK_ID) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001621 error(ERR_NONFATAL,
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001622 "`%s' expects context identifiers", pp_directives[ct]);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001623 free_tlist(origline);
1624 return -1;
1625 }
Victor van den Elzen0e857f12008-07-23 13:21:29 +02001626 if (cstk && cstk->name && !nasm_stricmp(tline->text, cstk->name))
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001627 j = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001628 tline = tline->next;
1629 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001630 break;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001631
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001632 case PPC_IFDEF:
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001633 j = false; /* have we matched yet? */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001634 while (tline) {
1635 skip_white_(tline);
1636 if (!tline || (tline->type != TOK_ID &&
1637 (tline->type != TOK_PREPROC_ID ||
1638 tline->text[1] != '$'))) {
1639 error(ERR_NONFATAL,
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001640 "`%s' expects macro identifiers", pp_directives[ct]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001641 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001642 }
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07001643 if (smacro_defined(NULL, tline->text, 0, NULL, true))
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001644 j = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001645 tline = tline->next;
1646 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001647 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001648
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001649 case PPC_IFIDN:
1650 case PPC_IFIDNI:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001651 tline = expand_smacro(tline);
1652 t = tt = tline;
1653 while (tok_isnt_(tt, ","))
1654 tt = tt->next;
1655 if (!tt) {
1656 error(ERR_NONFATAL,
1657 "`%s' expects two comma-separated arguments",
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001658 pp_directives[ct]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001659 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001660 }
1661 tt = tt->next;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001662 j = true; /* assume equality unless proved not */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001663 while ((t->type != TOK_OTHER || strcmp(t->text, ",")) && tt) {
1664 if (tt->type == TOK_OTHER && !strcmp(tt->text, ",")) {
1665 error(ERR_NONFATAL, "`%s': more than one comma on line",
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001666 pp_directives[ct]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001667 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001668 }
1669 if (t->type == TOK_WHITESPACE) {
1670 t = t->next;
1671 continue;
1672 }
1673 if (tt->type == TOK_WHITESPACE) {
1674 tt = tt->next;
1675 continue;
1676 }
1677 if (tt->type != t->type) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001678 j = false; /* found mismatching tokens */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001679 break;
1680 }
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07001681 /* When comparing strings, need to unquote them first */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001682 if (t->type == TOK_STRING) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001683 size_t l1 = nasm_unquote(t->text, NULL);
1684 size_t l2 = nasm_unquote(tt->text, NULL);
H. Peter Anvind2456592008-06-19 15:04:18 -07001685
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001686 if (l1 != l2) {
1687 j = false;
1688 break;
1689 }
1690 if (mmemcmp(t->text, tt->text, l1, i == PPC_IFIDN)) {
1691 j = false;
1692 break;
1693 }
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07001694 } else if (mstrcmp(tt->text, t->text, i == PPC_IFIDN) != 0) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001695 j = false; /* found mismatching tokens */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001696 break;
1697 }
Nickolay Yurchenkof3b3ce22003-09-21 20:38:43 +00001698
H. Peter Anvine2c80182005-01-15 22:15:51 +00001699 t = t->next;
1700 tt = tt->next;
1701 }
1702 if ((t->type != TOK_OTHER || strcmp(t->text, ",")) || tt)
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001703 j = false; /* trailing gunk on one end or other */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001704 break;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001705
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001706 case PPC_IFMACRO:
H. Peter Anvin89cee572009-07-15 09:16:54 -04001707 {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001708 bool found = false;
1709 MMacro searching, *mmac;
H. Peter Anvin65747262002-05-07 00:10:05 +00001710
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001711 skip_white_(tline);
1712 tline = expand_id(tline);
1713 if (!tok_type_(tline, TOK_ID)) {
1714 error(ERR_NONFATAL,
1715 "`%s' expects a macro name", pp_directives[ct]);
1716 goto fail;
1717 }
1718 searching.name = nasm_strdup(tline->text);
1719 searching.casesense = true;
1720 searching.plus = false;
1721 searching.nolist = false;
1722 searching.in_progress = 0;
1723 searching.max_depth = 0;
1724 searching.rep_nest = NULL;
1725 searching.nparam_min = 0;
1726 searching.nparam_max = INT_MAX;
1727 tline = expand_smacro(tline->next);
1728 skip_white_(tline);
1729 if (!tline) {
1730 } else if (!tok_type_(tline, TOK_NUMBER)) {
1731 error(ERR_NONFATAL,
1732 "`%s' expects a parameter count or nothing",
1733 pp_directives[ct]);
1734 } else {
1735 searching.nparam_min = searching.nparam_max =
1736 readnum(tline->text, &j);
1737 if (j)
1738 error(ERR_NONFATAL,
1739 "unable to parse parameter count `%s'",
1740 tline->text);
1741 }
1742 if (tline && tok_is_(tline->next, "-")) {
1743 tline = tline->next->next;
1744 if (tok_is_(tline, "*"))
1745 searching.nparam_max = INT_MAX;
1746 else if (!tok_type_(tline, TOK_NUMBER))
1747 error(ERR_NONFATAL,
1748 "`%s' expects a parameter count after `-'",
1749 pp_directives[ct]);
1750 else {
1751 searching.nparam_max = readnum(tline->text, &j);
1752 if (j)
1753 error(ERR_NONFATAL,
1754 "unable to parse parameter count `%s'",
1755 tline->text);
1756 if (searching.nparam_min > searching.nparam_max)
1757 error(ERR_NONFATAL,
1758 "minimum parameter count exceeds maximum");
1759 }
1760 }
1761 if (tline && tok_is_(tline->next, "+")) {
1762 tline = tline->next;
1763 searching.plus = true;
1764 }
1765 mmac = (MMacro *) hash_findix(&mmacros, searching.name);
1766 while (mmac) {
1767 if (!strcmp(mmac->name, searching.name) &&
1768 (mmac->nparam_min <= searching.nparam_max
1769 || searching.plus)
1770 && (searching.nparam_min <= mmac->nparam_max
1771 || mmac->plus)) {
1772 found = true;
1773 break;
1774 }
1775 mmac = mmac->next;
1776 }
1777 if (tline && tline->next)
1778 error(ERR_WARNING|ERR_PASS1,
1779 "trailing garbage after %%ifmacro ignored");
1780 nasm_free(searching.name);
1781 j = found;
1782 break;
H. Peter Anvin89cee572009-07-15 09:16:54 -04001783 }
H. Peter Anvin65747262002-05-07 00:10:05 +00001784
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001785 case PPC_IFID:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001786 needtype = TOK_ID;
1787 goto iftype;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001788 case PPC_IFNUM:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001789 needtype = TOK_NUMBER;
1790 goto iftype;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001791 case PPC_IFSTR:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001792 needtype = TOK_STRING;
1793 goto iftype;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001794
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001795iftype:
1796 t = tline = expand_smacro(tline);
H. Peter Anvind85d2502008-05-04 17:53:31 -07001797
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001798 while (tok_type_(t, TOK_WHITESPACE) ||
1799 (needtype == TOK_NUMBER &&
1800 tok_type_(t, TOK_OTHER) &&
1801 (t->text[0] == '-' || t->text[0] == '+') &&
1802 !t->text[1]))
1803 t = t->next;
H. Peter Anvind85d2502008-05-04 17:53:31 -07001804
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001805 j = tok_type_(t, needtype);
1806 break;
H. Peter Anvincbf768d2008-02-16 16:41:25 -08001807
1808 case PPC_IFTOKEN:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001809 t = tline = expand_smacro(tline);
1810 while (tok_type_(t, TOK_WHITESPACE))
1811 t = t->next;
H. Peter Anvincbf768d2008-02-16 16:41:25 -08001812
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001813 j = false;
1814 if (t) {
1815 t = t->next; /* Skip the actual token */
1816 while (tok_type_(t, TOK_WHITESPACE))
1817 t = t->next;
1818 j = !t; /* Should be nothing left */
1819 }
1820 break;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001821
H. Peter Anvin134b9462008-02-16 17:01:40 -08001822 case PPC_IFEMPTY:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001823 t = tline = expand_smacro(tline);
1824 while (tok_type_(t, TOK_WHITESPACE))
1825 t = t->next;
H. Peter Anvin134b9462008-02-16 17:01:40 -08001826
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001827 j = !t; /* Should be empty */
1828 break;
H. Peter Anvin134b9462008-02-16 17:01:40 -08001829
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001830 case PPC_IF:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001831 t = tline = expand_smacro(tline);
1832 tptr = &t;
1833 tokval.t_type = TOKEN_INVALID;
1834 evalresult = evaluate(ppscan, tptr, &tokval,
1835 NULL, pass | CRITICAL, error, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001836 if (!evalresult)
1837 return -1;
1838 if (tokval.t_type)
H. Peter Anvin917a3492008-09-24 09:14:49 -07001839 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +00001840 "trailing garbage after expression ignored");
1841 if (!is_simple(evalresult)) {
1842 error(ERR_NONFATAL,
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001843 "non-constant value given to `%s'", pp_directives[ct]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001844 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001845 }
Chuck Crayne60ae75d2007-05-02 01:59:16 +00001846 j = reloc_value(evalresult) != 0;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001847 break;
H. Peter Anvin95e28822007-09-12 04:20:08 +00001848
H. Peter Anvine2c80182005-01-15 22:15:51 +00001849 default:
1850 error(ERR_FATAL,
1851 "preprocessor directive `%s' not yet implemented",
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001852 pp_directives[ct]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001853 goto fail;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001854 }
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001855
1856 free_tlist(origline);
1857 return j ^ PP_NEGATIVE(ct);
H. Peter Anvin70653092007-10-19 14:42:29 -07001858
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001859fail:
1860 free_tlist(origline);
1861 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001862}
1863
1864/*
H. Peter Anvin4db5a162007-10-11 13:42:09 -07001865 * Common code for defining an smacro
1866 */
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001867static bool define_smacro(Context *ctx, const char *mname, bool casesense,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001868 int nparam, Token *expansion)
H. Peter Anvin4db5a162007-10-11 13:42:09 -07001869{
1870 SMacro *smac, **smhead;
H. Peter Anvin166c2472008-05-28 12:28:58 -07001871 struct hash_table *smtbl;
H. Peter Anvin70653092007-10-19 14:42:29 -07001872
H. Peter Anvin4db5a162007-10-11 13:42:09 -07001873 if (smacro_defined(ctx, mname, nparam, &smac, casesense)) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001874 if (!smac) {
1875 error(ERR_WARNING|ERR_PASS1,
1876 "single-line macro `%s' defined both with and"
1877 " without parameters", mname);
1878 /*
1879 * Some instances of the old code considered this a failure,
1880 * some others didn't. What is the right thing to do here?
1881 */
1882 free_tlist(expansion);
1883 return false; /* Failure */
1884 } else {
1885 /*
1886 * We're redefining, so we have to take over an
1887 * existing SMacro structure. This means freeing
1888 * what was already in it.
1889 */
1890 nasm_free(smac->name);
1891 free_tlist(smac->expansion);
1892 }
H. Peter Anvin4db5a162007-10-11 13:42:09 -07001893 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001894 smtbl = ctx ? &ctx->localmac : &smacros;
1895 smhead = (SMacro **) hash_findi_add(smtbl, mname);
1896 smac = nasm_malloc(sizeof(SMacro));
1897 smac->next = *smhead;
1898 *smhead = smac;
H. Peter Anvin4db5a162007-10-11 13:42:09 -07001899 }
1900 smac->name = nasm_strdup(mname);
1901 smac->casesense = casesense;
1902 smac->nparam = nparam;
1903 smac->expansion = expansion;
1904 smac->in_progress = false;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001905 return true; /* Success */
H. Peter Anvin4db5a162007-10-11 13:42:09 -07001906}
1907
1908/*
1909 * Undefine an smacro
1910 */
1911static void undef_smacro(Context *ctx, const char *mname)
1912{
1913 SMacro **smhead, *s, **sp;
H. Peter Anvin166c2472008-05-28 12:28:58 -07001914 struct hash_table *smtbl;
H. Peter Anvin4db5a162007-10-11 13:42:09 -07001915
H. Peter Anvin166c2472008-05-28 12:28:58 -07001916 smtbl = ctx ? &ctx->localmac : &smacros;
1917 smhead = (SMacro **)hash_findi(smtbl, mname, NULL);
H. Peter Anvin70653092007-10-19 14:42:29 -07001918
H. Peter Anvin4db5a162007-10-11 13:42:09 -07001919 if (smhead) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001920 /*
1921 * We now have a macro name... go hunt for it.
1922 */
1923 sp = smhead;
1924 while ((s = *sp) != NULL) {
1925 if (!mstrcmp(s->name, mname, s->casesense)) {
1926 *sp = s->next;
1927 nasm_free(s->name);
1928 free_tlist(s->expansion);
1929 nasm_free(s);
1930 } else {
1931 sp = &s->next;
1932 }
1933 }
H. Peter Anvin4db5a162007-10-11 13:42:09 -07001934 }
1935}
1936
H. Peter Anvin8781cb02007-11-08 20:01:11 -08001937/*
H. Peter Anvina26433d2008-07-16 14:40:01 -07001938 * Parse a mmacro specification.
1939 */
1940static bool parse_mmacro_spec(Token *tline, MMacro *def, const char *directive)
1941{
1942 bool err;
1943
1944 tline = tline->next;
1945 skip_white_(tline);
1946 tline = expand_id(tline);
1947 if (!tok_type_(tline, TOK_ID)) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001948 error(ERR_NONFATAL, "`%s' expects a macro name", directive);
1949 return false;
H. Peter Anvina26433d2008-07-16 14:40:01 -07001950 }
Victor van den Elzenb916ede2008-07-23 15:14:22 +02001951
H. Peter Anvin89cee572009-07-15 09:16:54 -04001952 def->prev = NULL;
H. Peter Anvina26433d2008-07-16 14:40:01 -07001953 def->name = nasm_strdup(tline->text);
1954 def->plus = false;
1955 def->nolist = false;
1956 def->in_progress = 0;
1957 def->rep_nest = NULL;
Victor van den Elzenb916ede2008-07-23 15:14:22 +02001958 def->nparam_min = 0;
1959 def->nparam_max = 0;
1960
H. Peter Anvina26433d2008-07-16 14:40:01 -07001961 tline = expand_smacro(tline->next);
1962 skip_white_(tline);
1963 if (!tok_type_(tline, TOK_NUMBER)) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001964 error(ERR_NONFATAL, "`%s' expects a parameter count", directive);
H. Peter Anvina26433d2008-07-16 14:40:01 -07001965 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001966 def->nparam_min = def->nparam_max =
1967 readnum(tline->text, &err);
1968 if (err)
1969 error(ERR_NONFATAL,
1970 "unable to parse parameter count `%s'", tline->text);
H. Peter Anvina26433d2008-07-16 14:40:01 -07001971 }
1972 if (tline && tok_is_(tline->next, "-")) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001973 tline = tline->next->next;
1974 if (tok_is_(tline, "*")) {
1975 def->nparam_max = INT_MAX;
1976 } else if (!tok_type_(tline, TOK_NUMBER)) {
1977 error(ERR_NONFATAL,
1978 "`%s' expects a parameter count after `-'", directive);
1979 } else {
1980 def->nparam_max = readnum(tline->text, &err);
1981 if (err) {
1982 error(ERR_NONFATAL, "unable to parse parameter count `%s'",
1983 tline->text);
1984 }
1985 if (def->nparam_min > def->nparam_max) {
1986 error(ERR_NONFATAL, "minimum parameter count exceeds maximum");
1987 }
1988 }
H. Peter Anvina26433d2008-07-16 14:40:01 -07001989 }
1990 if (tline && tok_is_(tline->next, "+")) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001991 tline = tline->next;
1992 def->plus = true;
H. Peter Anvina26433d2008-07-16 14:40:01 -07001993 }
1994 if (tline && tok_type_(tline->next, TOK_ID) &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001995 !nasm_stricmp(tline->next->text, ".nolist")) {
1996 tline = tline->next;
1997 def->nolist = true;
H. Peter Anvina26433d2008-07-16 14:40:01 -07001998 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001999
H. Peter Anvina26433d2008-07-16 14:40:01 -07002000 /*
2001 * Handle default parameters.
2002 */
2003 if (tline && tline->next) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002004 def->dlist = tline->next;
2005 tline->next = NULL;
2006 count_mmac_params(def->dlist, &def->ndefs, &def->defaults);
H. Peter Anvina26433d2008-07-16 14:40:01 -07002007 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002008 def->dlist = NULL;
2009 def->defaults = NULL;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002010 }
2011 def->expansion = NULL;
2012
H. Peter Anvin89cee572009-07-15 09:16:54 -04002013 if (def->defaults && def->ndefs > def->nparam_max - def->nparam_min &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002014 !def->plus)
2015 error(ERR_WARNING|ERR_PASS1|ERR_WARN_MDP,
2016 "too many default macro parameters");
Victor van den Elzenb916ede2008-07-23 15:14:22 +02002017
H. Peter Anvina26433d2008-07-16 14:40:01 -07002018 return true;
2019}
2020
2021
2022/*
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002023 * Decode a size directive
2024 */
2025static int parse_size(const char *str) {
2026 static const char *size_names[] =
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002027 { "byte", "dword", "oword", "qword", "tword", "word", "yword" };
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002028 static const int sizes[] =
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002029 { 0, 1, 4, 16, 8, 10, 2, 32 };
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002030
Cyrill Gorcunova7319242010-06-03 22:04:36 +04002031 return sizes[bsii(str, size_names, ARRAY_SIZE(size_names))+1];
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002032}
2033
H. Peter Anvinf9c9a672009-07-14 15:14:05 -04002034/*
2035 * nasm_unquote with error if the string contains NUL characters.
2036 * If the string contains NUL characters, issue an error and return
2037 * the C len, i.e. truncate at the NUL.
2038 */
2039static size_t nasm_unquote_cstr(char *qstr, enum preproc_token directive)
2040{
2041 size_t len = nasm_unquote(qstr, NULL);
2042 size_t clen = strlen(qstr);
2043
2044 if (len != clen)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002045 error(ERR_NONFATAL, "NUL character in `%s' directive",
2046 pp_directives[directive]);
H. Peter Anvinf9c9a672009-07-14 15:14:05 -04002047
2048 return clen;
2049}
2050
Ed Beroset3ab3f412002-06-11 03:31:49 +00002051/**
2052 * find and process preprocessor directive in passed line
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002053 * Find out if a line contains a preprocessor directive, and deal
2054 * with it if so.
H. Peter Anvin70653092007-10-19 14:42:29 -07002055 *
Ed Beroset3ab3f412002-06-11 03:31:49 +00002056 * If a directive _is_ found, it is the responsibility of this routine
2057 * (and not the caller) to free_tlist() the line.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002058 *
Ed Beroset3ab3f412002-06-11 03:31:49 +00002059 * @param tline a pointer to the current tokeninzed line linked list
2060 * @return DIRECTIVE_FOUND or NO_DIRECTIVE_FOUND
H. Peter Anvin70653092007-10-19 14:42:29 -07002061 *
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002062 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002063static int do_directive(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00002064{
H. Peter Anvin4169a472007-09-12 01:29:43 +00002065 enum preproc_token i;
2066 int j;
H. Peter Anvin70055962007-10-11 00:05:31 -07002067 bool err;
2068 int nparam;
2069 bool nolist;
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07002070 bool casesense;
H. Peter Anvin8cfdb9d2007-09-14 18:36:01 -07002071 int k, m;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002072 int offset;
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08002073 char *p, *pp;
2074 const char *mname;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002075 Include *inc;
2076 Context *ctx;
2077 Cond *cond;
H. Peter Anvin97a23472007-09-16 17:57:25 -07002078 MMacro *mmac, **mmhead;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002079 Token *t, *tt, *param_start, *macro_start, *last, **tptr, *origline;
2080 Line *l;
2081 struct tokenval tokval;
2082 expr *evalresult;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002083 MMacro *tmp_defining; /* Used when manipulating rep_nest */
H. Peter Anvinf8ba53e2007-10-11 10:11:57 -07002084 int64_t count;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07002085 size_t len;
H. Peter Anvin8e3f75e2008-09-24 00:21:58 -07002086 int severity;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002087
2088 origline = tline;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002089
H. Peter Anvineba20a72002-04-30 20:53:55 +00002090 skip_white_(tline);
H. Peter Anvinf2936d72008-06-04 15:11:23 -07002091 if (!tline || !tok_type_(tline, TOK_PREPROC_ID) ||
H. Peter Anvine2c80182005-01-15 22:15:51 +00002092 (tline->text[1] == '%' || tline->text[1] == '$'
2093 || tline->text[1] == '!'))
2094 return NO_DIRECTIVE_FOUND;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002095
H. Peter Anvin4169a472007-09-12 01:29:43 +00002096 i = pp_token_hash(tline->text);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002097
2098 /*
Cyrill Gorcunovf09116f2010-02-28 11:52:53 +03002099 * FIXME: We zap execution of PP_RMACRO, PP_IRMACRO, PP_EXITMACRO
2100 * since they are known to be buggy at moment, we need to fix them
2101 * in future release (2.09-2.10)
2102 */
2103 if (i == PP_RMACRO || i == PP_RMACRO || i == PP_EXITMACRO) {
2104 error(ERR_NONFATAL, "unknown preprocessor directive `%s'",
2105 tline->text);
2106 return NO_DIRECTIVE_FOUND;
2107 }
2108
2109 /*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002110 * If we're in a non-emitting branch of a condition construct,
H. Peter Anvin76690a12002-04-30 20:52:49 +00002111 * or walking to the end of an already terminated %rep block,
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002112 * we should ignore all directives except for condition
2113 * directives.
2114 */
H. Peter Anvin76690a12002-04-30 20:52:49 +00002115 if (((istk->conds && !emitting(istk->conds->state)) ||
H. Peter Anvine2c80182005-01-15 22:15:51 +00002116 (istk->mstk && !istk->mstk->in_progress)) && !is_condition(i)) {
2117 return NO_DIRECTIVE_FOUND;
H. Peter Anvineba20a72002-04-30 20:53:55 +00002118 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002119
2120 /*
H. Peter Anvin76690a12002-04-30 20:52:49 +00002121 * If we're defining a macro or reading a %rep block, we should
Victor van den Elzen8f1120f2008-07-16 13:41:37 +02002122 * ignore all directives except for %macro/%imacro (which nest),
2123 * %endm/%endmacro, and (only if we're in a %rep block) %endrep.
2124 * If we're in a %rep block, another %rep nests, so should be let through.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002125 */
2126 if (defining && i != PP_MACRO && i != PP_IMACRO &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002127 i != PP_RMACRO && i != PP_IRMACRO &&
H. Peter Anvine2c80182005-01-15 22:15:51 +00002128 i != PP_ENDMACRO && i != PP_ENDM &&
2129 (defining->name || (i != PP_ENDREP && i != PP_REP))) {
2130 return NO_DIRECTIVE_FOUND;
H. Peter Anvineba20a72002-04-30 20:53:55 +00002131 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002132
Charles Crayned4200be2008-07-12 16:42:33 -07002133 if (defining) {
Keith Kanios852f1ee2009-07-12 00:19:55 -05002134 if (i == PP_MACRO || i == PP_IMACRO ||
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002135 i == PP_RMACRO || i == PP_IRMACRO) {
Charles Crayned4200be2008-07-12 16:42:33 -07002136 nested_mac_count++;
2137 return NO_DIRECTIVE_FOUND;
2138 } else if (nested_mac_count > 0) {
2139 if (i == PP_ENDMACRO) {
2140 nested_mac_count--;
2141 return NO_DIRECTIVE_FOUND;
2142 }
2143 }
2144 if (!defining->name) {
2145 if (i == PP_REP) {
2146 nested_rep_count++;
2147 return NO_DIRECTIVE_FOUND;
2148 } else if (nested_rep_count > 0) {
2149 if (i == PP_ENDREP) {
2150 nested_rep_count--;
2151 return NO_DIRECTIVE_FOUND;
2152 }
2153 }
2154 }
2155 }
2156
H. Peter Anvin4169a472007-09-12 01:29:43 +00002157 switch (i) {
2158 case PP_INVALID:
H. Peter Anvine2c80182005-01-15 22:15:51 +00002159 error(ERR_NONFATAL, "unknown preprocessor directive `%s'",
2160 tline->text);
2161 return NO_DIRECTIVE_FOUND; /* didn't get it */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002162
H. Peter Anvine2c80182005-01-15 22:15:51 +00002163 case PP_STACKSIZE:
2164 /* Directive to tell NASM what the default stack size is. The
2165 * default is for a 16-bit stack, and this can be overriden with
2166 * %stacksize large.
H. Peter Anvine2c80182005-01-15 22:15:51 +00002167 */
2168 tline = tline->next;
2169 if (tline && tline->type == TOK_WHITESPACE)
2170 tline = tline->next;
2171 if (!tline || tline->type != TOK_ID) {
2172 error(ERR_NONFATAL, "`%%stacksize' missing size parameter");
2173 free_tlist(origline);
2174 return DIRECTIVE_FOUND;
2175 }
2176 if (nasm_stricmp(tline->text, "flat") == 0) {
2177 /* All subsequent ARG directives are for a 32-bit stack */
2178 StackSize = 4;
2179 StackPointer = "ebp";
2180 ArgOffset = 8;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002181 LocalOffset = 0;
Charles Crayne7eaf9192007-11-08 22:11:14 -08002182 } else if (nasm_stricmp(tline->text, "flat64") == 0) {
2183 /* All subsequent ARG directives are for a 64-bit stack */
2184 StackSize = 8;
2185 StackPointer = "rbp";
Per Jessen53252e02010-02-11 00:16:59 +03002186 ArgOffset = 16;
Charles Crayne7eaf9192007-11-08 22:11:14 -08002187 LocalOffset = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002188 } else if (nasm_stricmp(tline->text, "large") == 0) {
2189 /* All subsequent ARG directives are for a 16-bit stack,
2190 * far function call.
2191 */
2192 StackSize = 2;
2193 StackPointer = "bp";
2194 ArgOffset = 4;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002195 LocalOffset = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002196 } else if (nasm_stricmp(tline->text, "small") == 0) {
2197 /* All subsequent ARG directives are for a 16-bit stack,
2198 * far function call. We don't support near functions.
2199 */
2200 StackSize = 2;
2201 StackPointer = "bp";
2202 ArgOffset = 6;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002203 LocalOffset = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002204 } else {
2205 error(ERR_NONFATAL, "`%%stacksize' invalid size type");
2206 free_tlist(origline);
2207 return DIRECTIVE_FOUND;
2208 }
2209 free_tlist(origline);
2210 return DIRECTIVE_FOUND;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002211
H. Peter Anvine2c80182005-01-15 22:15:51 +00002212 case PP_ARG:
2213 /* TASM like ARG directive to define arguments to functions, in
2214 * the following form:
2215 *
2216 * ARG arg1:WORD, arg2:DWORD, arg4:QWORD
2217 */
2218 offset = ArgOffset;
2219 do {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00002220 char *arg, directive[256];
H. Peter Anvine2c80182005-01-15 22:15:51 +00002221 int size = StackSize;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002222
H. Peter Anvine2c80182005-01-15 22:15:51 +00002223 /* Find the argument name */
2224 tline = tline->next;
2225 if (tline && tline->type == TOK_WHITESPACE)
2226 tline = tline->next;
2227 if (!tline || tline->type != TOK_ID) {
2228 error(ERR_NONFATAL, "`%%arg' missing argument parameter");
2229 free_tlist(origline);
2230 return DIRECTIVE_FOUND;
2231 }
2232 arg = tline->text;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002233
H. Peter Anvine2c80182005-01-15 22:15:51 +00002234 /* Find the argument size type */
2235 tline = tline->next;
2236 if (!tline || tline->type != TOK_OTHER
2237 || tline->text[0] != ':') {
2238 error(ERR_NONFATAL,
2239 "Syntax error processing `%%arg' directive");
2240 free_tlist(origline);
2241 return DIRECTIVE_FOUND;
2242 }
2243 tline = tline->next;
2244 if (!tline || tline->type != TOK_ID) {
2245 error(ERR_NONFATAL, "`%%arg' missing size type parameter");
2246 free_tlist(origline);
2247 return DIRECTIVE_FOUND;
2248 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002249
H. Peter Anvine2c80182005-01-15 22:15:51 +00002250 /* Allow macro expansion of type parameter */
Keith Kaniosb7a89542007-04-12 02:40:54 +00002251 tt = tokenize(tline->text);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002252 tt = expand_smacro(tt);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002253 size = parse_size(tt->text);
2254 if (!size) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002255 error(ERR_NONFATAL,
2256 "Invalid size type for `%%arg' missing directive");
2257 free_tlist(tt);
2258 free_tlist(origline);
2259 return DIRECTIVE_FOUND;
2260 }
2261 free_tlist(tt);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002262
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002263 /* Round up to even stack slots */
2264 size = ALIGN(size, StackSize);
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002265
H. Peter Anvine2c80182005-01-15 22:15:51 +00002266 /* Now define the macro for the argument */
2267 snprintf(directive, sizeof(directive), "%%define %s (%s+%d)",
2268 arg, StackPointer, offset);
Keith Kaniosb7a89542007-04-12 02:40:54 +00002269 do_directive(tokenize(directive));
H. Peter Anvine2c80182005-01-15 22:15:51 +00002270 offset += size;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002271
H. Peter Anvine2c80182005-01-15 22:15:51 +00002272 /* Move to the next argument in the list */
2273 tline = tline->next;
2274 if (tline && tline->type == TOK_WHITESPACE)
2275 tline = tline->next;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002276 } while (tline && tline->type == TOK_OTHER && tline->text[0] == ',');
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002277 ArgOffset = offset;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002278 free_tlist(origline);
2279 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002280
H. Peter Anvine2c80182005-01-15 22:15:51 +00002281 case PP_LOCAL:
2282 /* TASM like LOCAL directive to define local variables for a
2283 * function, in the following form:
2284 *
2285 * LOCAL local1:WORD, local2:DWORD, local4:QWORD = LocalSize
2286 *
2287 * The '= LocalSize' at the end is ignored by NASM, but is
2288 * required by TASM to define the local parameter size (and used
2289 * by the TASM macro package).
2290 */
2291 offset = LocalOffset;
2292 do {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00002293 char *local, directive[256];
H. Peter Anvine2c80182005-01-15 22:15:51 +00002294 int size = StackSize;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002295
H. Peter Anvine2c80182005-01-15 22:15:51 +00002296 /* Find the argument name */
2297 tline = tline->next;
2298 if (tline && tline->type == TOK_WHITESPACE)
2299 tline = tline->next;
2300 if (!tline || tline->type != TOK_ID) {
2301 error(ERR_NONFATAL,
2302 "`%%local' missing argument parameter");
2303 free_tlist(origline);
2304 return DIRECTIVE_FOUND;
2305 }
2306 local = tline->text;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002307
H. Peter Anvine2c80182005-01-15 22:15:51 +00002308 /* Find the argument size type */
2309 tline = tline->next;
2310 if (!tline || tline->type != TOK_OTHER
2311 || tline->text[0] != ':') {
2312 error(ERR_NONFATAL,
2313 "Syntax error processing `%%local' directive");
2314 free_tlist(origline);
2315 return DIRECTIVE_FOUND;
2316 }
2317 tline = tline->next;
2318 if (!tline || tline->type != TOK_ID) {
2319 error(ERR_NONFATAL,
2320 "`%%local' missing size type parameter");
2321 free_tlist(origline);
2322 return DIRECTIVE_FOUND;
2323 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00002324
H. Peter Anvine2c80182005-01-15 22:15:51 +00002325 /* Allow macro expansion of type parameter */
Keith Kaniosb7a89542007-04-12 02:40:54 +00002326 tt = tokenize(tline->text);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002327 tt = expand_smacro(tt);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002328 size = parse_size(tt->text);
2329 if (!size) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002330 error(ERR_NONFATAL,
2331 "Invalid size type for `%%local' missing directive");
2332 free_tlist(tt);
2333 free_tlist(origline);
2334 return DIRECTIVE_FOUND;
2335 }
2336 free_tlist(tt);
H. Peter Anvin734b1882002-04-30 21:01:08 +00002337
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002338 /* Round up to even stack slots */
2339 size = ALIGN(size, StackSize);
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002340
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002341 offset += size; /* Negative offset, increment before */
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002342
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002343 /* Now define the macro for the argument */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002344 snprintf(directive, sizeof(directive), "%%define %s (%s-%d)",
2345 local, StackPointer, offset);
Keith Kaniosb7a89542007-04-12 02:40:54 +00002346 do_directive(tokenize(directive));
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002347
H. Peter Anvine2c80182005-01-15 22:15:51 +00002348 /* Now define the assign to setup the enter_c macro correctly */
2349 snprintf(directive, sizeof(directive),
2350 "%%assign %%$localsize %%$localsize+%d", size);
Keith Kaniosb7a89542007-04-12 02:40:54 +00002351 do_directive(tokenize(directive));
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002352
H. Peter Anvine2c80182005-01-15 22:15:51 +00002353 /* Move to the next argument in the list */
2354 tline = tline->next;
2355 if (tline && tline->type == TOK_WHITESPACE)
2356 tline = tline->next;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002357 } while (tline && tline->type == TOK_OTHER && tline->text[0] == ',');
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002358 LocalOffset = offset;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002359 free_tlist(origline);
2360 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002361
H. Peter Anvine2c80182005-01-15 22:15:51 +00002362 case PP_CLEAR:
2363 if (tline->next)
H. Peter Anvin917a3492008-09-24 09:14:49 -07002364 error(ERR_WARNING|ERR_PASS1,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002365 "trailing garbage after `%%clear' ignored");
2366 free_macros();
2367 init_macros();
H. Peter Anvine2c80182005-01-15 22:15:51 +00002368 free_tlist(origline);
2369 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002370
H. Peter Anvin418ca702008-05-30 10:42:30 -07002371 case PP_DEPEND:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002372 t = tline->next = expand_smacro(tline->next);
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002373 skip_white_(t);
2374 if (!t || (t->type != TOK_STRING &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002375 t->type != TOK_INTERNAL_STRING)) {
H. Peter Anvin418ca702008-05-30 10:42:30 -07002376 error(ERR_NONFATAL, "`%%depend' 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 Anvin418ca702008-05-30 10:42:30 -07002382 "trailing garbage after `%%depend' 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);
2386 if (dephead && !in_list(*dephead, p)) {
2387 StrList *sl = nasm_malloc(strlen(p)+1+sizeof sl->next);
2388 sl->next = NULL;
2389 strcpy(sl->str, p);
2390 *deptail = sl;
2391 deptail = &sl->next;
2392 }
2393 free_tlist(origline);
H. Peter Anvin418ca702008-05-30 10:42:30 -07002394 return DIRECTIVE_FOUND;
2395
2396 case PP_INCLUDE:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002397 t = tline->next = expand_smacro(tline->next);
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002398 skip_white_(t);
H. Peter Anvind2456592008-06-19 15:04:18 -07002399
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002400 if (!t || (t->type != TOK_STRING &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002401 t->type != TOK_INTERNAL_STRING)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002402 error(ERR_NONFATAL, "`%%include' expects a file name");
2403 free_tlist(origline);
2404 return DIRECTIVE_FOUND; /* but we did _something_ */
2405 }
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002406 if (t->next)
H. Peter Anvin917a3492008-09-24 09:14:49 -07002407 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +00002408 "trailing garbage after `%%include' ignored");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002409 p = t->text;
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002410 if (t->type != TOK_INTERNAL_STRING)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002411 nasm_unquote_cstr(p, i);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002412 inc = nasm_malloc(sizeof(Include));
2413 inc->next = istk;
2414 inc->conds = NULL;
H. Peter Anvin2b1c3b92008-06-06 10:38:46 -07002415 inc->fp = inc_fopen(p, dephead, &deptail, pass == 0);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002416 if (!inc->fp) {
2417 /* -MG given but file not found */
2418 nasm_free(inc);
2419 } else {
2420 inc->fname = src_set_fname(nasm_strdup(p));
2421 inc->lineno = src_set_linnum(0);
2422 inc->lineinc = 1;
2423 inc->expansion = NULL;
2424 inc->mstk = NULL;
2425 istk = inc;
2426 list->uplevel(LIST_INCLUDE);
2427 }
2428 free_tlist(origline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002429 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002430
H. Peter Anvind2456592008-06-19 15:04:18 -07002431 case PP_USE:
H. Peter Anvinf4ae5ad2008-06-19 18:39:24 -07002432 {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002433 static macros_t *use_pkg;
2434 const char *pkg_macro = NULL;
H. Peter Anvinf4ae5ad2008-06-19 18:39:24 -07002435
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002436 tline = tline->next;
2437 skip_white_(tline);
2438 tline = expand_id(tline);
H. Peter Anvind2456592008-06-19 15:04:18 -07002439
H. Peter Anvin264b7b92008-10-24 16:38:17 -07002440 if (!tline || (tline->type != TOK_STRING &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002441 tline->type != TOK_INTERNAL_STRING &&
2442 tline->type != TOK_ID)) {
H. Peter Anvin926fc402008-06-19 16:26:12 -07002443 error(ERR_NONFATAL, "`%%use' expects a package name");
H. Peter Anvind2456592008-06-19 15:04:18 -07002444 free_tlist(origline);
2445 return DIRECTIVE_FOUND; /* but we did _something_ */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002446 }
H. Peter Anvin264b7b92008-10-24 16:38:17 -07002447 if (tline->next)
H. Peter Anvin917a3492008-09-24 09:14:49 -07002448 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvind2456592008-06-19 15:04:18 -07002449 "trailing garbage after `%%use' ignored");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002450 if (tline->type == TOK_STRING)
2451 nasm_unquote_cstr(tline->text, i);
2452 use_pkg = nasm_stdmac_find_package(tline->text);
2453 if (!use_pkg)
2454 error(ERR_NONFATAL, "unknown `%%use' package: %s", tline->text);
2455 else
2456 pkg_macro = (char *)use_pkg + 1; /* The first string will be <%define>__USE_*__ */
Victor van den Elzen35eb2ea2010-03-10 22:33:48 +01002457 if (use_pkg && ! smacro_defined(NULL, pkg_macro, 0, NULL, true)) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002458 /* Not already included, go ahead and include it */
2459 stdmacpos = use_pkg;
2460 }
2461 free_tlist(origline);
H. Peter Anvind2456592008-06-19 15:04:18 -07002462 return DIRECTIVE_FOUND;
H. Peter Anvinf4ae5ad2008-06-19 18:39:24 -07002463 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002464 case PP_PUSH:
H. Peter Anvine2c80182005-01-15 22:15:51 +00002465 case PP_REPL:
H. Peter Anvin42b56392008-10-24 16:24:21 -07002466 case PP_POP:
H. Peter Anvine2c80182005-01-15 22:15:51 +00002467 tline = tline->next;
2468 skip_white_(tline);
2469 tline = expand_id(tline);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002470 if (tline) {
2471 if (!tok_type_(tline, TOK_ID)) {
2472 error(ERR_NONFATAL, "`%s' expects a context identifier",
2473 pp_directives[i]);
2474 free_tlist(origline);
2475 return DIRECTIVE_FOUND; /* but we did _something_ */
2476 }
2477 if (tline->next)
2478 error(ERR_WARNING|ERR_PASS1,
2479 "trailing garbage after `%s' ignored",
2480 pp_directives[i]);
2481 p = nasm_strdup(tline->text);
2482 } else {
2483 p = NULL; /* Anonymous */
2484 }
H. Peter Anvin42b56392008-10-24 16:24:21 -07002485
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002486 if (i == PP_PUSH) {
2487 ctx = nasm_malloc(sizeof(Context));
2488 ctx->next = cstk;
2489 hash_init(&ctx->localmac, HASH_SMALL);
2490 ctx->name = p;
2491 ctx->number = unique++;
2492 cstk = ctx;
2493 } else {
2494 /* %pop or %repl */
2495 if (!cstk) {
2496 error(ERR_NONFATAL, "`%s': context stack is empty",
2497 pp_directives[i]);
2498 } else if (i == PP_POP) {
2499 if (p && (!cstk->name || nasm_stricmp(p, cstk->name)))
2500 error(ERR_NONFATAL, "`%%pop' in wrong context: %s, "
2501 "expected %s",
2502 cstk->name ? cstk->name : "anonymous", p);
2503 else
2504 ctx_pop();
2505 } else {
2506 /* i == PP_REPL */
2507 nasm_free(cstk->name);
2508 cstk->name = p;
2509 p = NULL;
2510 }
2511 nasm_free(p);
2512 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002513 free_tlist(origline);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002514 return DIRECTIVE_FOUND;
H. Peter Anvin8e3f75e2008-09-24 00:21:58 -07002515 case PP_FATAL:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002516 severity = ERR_FATAL;
2517 goto issue_error;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002518 case PP_ERROR:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002519 severity = ERR_NONFATAL;
2520 goto issue_error;
H. Peter Anvin7df04172008-06-10 18:27:38 -07002521 case PP_WARNING:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002522 severity = ERR_WARNING|ERR_WARN_USER;
2523 goto issue_error;
H. Peter Anvin8e3f75e2008-09-24 00:21:58 -07002524
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002525issue_error:
H. Peter Anvin7df04172008-06-10 18:27:38 -07002526 {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002527 /* Only error out if this is the final pass */
2528 if (pass != 2 && i != PP_FATAL)
2529 return DIRECTIVE_FOUND;
2530
2531 tline->next = expand_smacro(tline->next);
2532 tline = tline->next;
2533 skip_white_(tline);
2534 t = tline ? tline->next : NULL;
2535 skip_white_(t);
2536 if (tok_type_(tline, TOK_STRING) && !t) {
2537 /* The line contains only a quoted string */
2538 p = tline->text;
2539 nasm_unquote(p, NULL); /* Ignore NUL character truncation */
2540 error(severity, "%s", p);
2541 } else {
2542 /* Not a quoted string, or more than a quoted string */
2543 p = detoken(tline, false);
2544 error(severity, "%s", p);
2545 nasm_free(p);
2546 }
2547 free_tlist(origline);
2548 return DIRECTIVE_FOUND;
H. Peter Anvin7df04172008-06-10 18:27:38 -07002549 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00002550
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002551 CASE_PP_IF:
H. Peter Anvine2c80182005-01-15 22:15:51 +00002552 if (istk->conds && !emitting(istk->conds->state))
2553 j = COND_NEVER;
2554 else {
2555 j = if_condition(tline->next, i);
2556 tline->next = NULL; /* it got freed */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002557 j = j < 0 ? COND_NEVER : j ? COND_IF_TRUE : COND_IF_FALSE;
2558 }
2559 cond = nasm_malloc(sizeof(Cond));
2560 cond->next = istk->conds;
2561 cond->state = j;
2562 istk->conds = cond;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002563 if(istk->mstk)
Keith Kanios3c0d91f2009-10-25 13:28:03 -05002564 istk->mstk->condcnt ++;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002565 free_tlist(origline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002566 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002567
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002568 CASE_PP_ELIF:
H. Peter Anvine2c80182005-01-15 22:15:51 +00002569 if (!istk->conds)
H. Peter Anvin4169a472007-09-12 01:29:43 +00002570 error(ERR_FATAL, "`%s': no matching `%%if'", pp_directives[i]);
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002571 switch(istk->conds->state) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002572 case COND_IF_TRUE:
2573 istk->conds->state = COND_DONE;
2574 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002575
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002576 case COND_DONE:
2577 case COND_NEVER:
2578 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002579
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002580 case COND_ELSE_TRUE:
2581 case COND_ELSE_FALSE:
2582 error_precond(ERR_WARNING|ERR_PASS1,
2583 "`%%elif' after `%%else' ignored");
2584 istk->conds->state = COND_NEVER;
2585 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002586
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002587 case COND_IF_FALSE:
2588 /*
2589 * IMPORTANT: In the case of %if, we will already have
2590 * called expand_mmac_params(); however, if we're
2591 * processing an %elif we must have been in a
2592 * non-emitting mode, which would have inhibited
2593 * the normal invocation of expand_mmac_params().
2594 * Therefore, we have to do it explicitly here.
2595 */
2596 j = if_condition(expand_mmac_params(tline->next), i);
2597 tline->next = NULL; /* it got freed */
2598 istk->conds->state =
2599 j < 0 ? COND_NEVER : j ? COND_IF_TRUE : COND_IF_FALSE;
2600 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002601 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002602 free_tlist(origline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002603 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002604
H. Peter Anvine2c80182005-01-15 22:15:51 +00002605 case PP_ELSE:
2606 if (tline->next)
H. Peter Anvin917a3492008-09-24 09:14:49 -07002607 error_precond(ERR_WARNING|ERR_PASS1,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002608 "trailing garbage after `%%else' ignored");
H. Peter Anvine2c80182005-01-15 22:15:51 +00002609 if (!istk->conds)
2610 error(ERR_FATAL, "`%%else': no matching `%%if'");
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002611 switch(istk->conds->state) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002612 case COND_IF_TRUE:
2613 case COND_DONE:
2614 istk->conds->state = COND_ELSE_FALSE;
2615 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002616
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002617 case COND_NEVER:
2618 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002619
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002620 case COND_IF_FALSE:
2621 istk->conds->state = COND_ELSE_TRUE;
2622 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002623
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002624 case COND_ELSE_TRUE:
2625 case COND_ELSE_FALSE:
2626 error_precond(ERR_WARNING|ERR_PASS1,
2627 "`%%else' after `%%else' ignored.");
2628 istk->conds->state = COND_NEVER;
2629 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002630 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002631 free_tlist(origline);
2632 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002633
H. Peter Anvine2c80182005-01-15 22:15:51 +00002634 case PP_ENDIF:
2635 if (tline->next)
H. Peter Anvin917a3492008-09-24 09:14:49 -07002636 error_precond(ERR_WARNING|ERR_PASS1,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002637 "trailing garbage after `%%endif' ignored");
H. Peter Anvine2c80182005-01-15 22:15:51 +00002638 if (!istk->conds)
2639 error(ERR_FATAL, "`%%endif': no matching `%%if'");
2640 cond = istk->conds;
2641 istk->conds = cond->next;
2642 nasm_free(cond);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002643 if(istk->mstk)
Keith Kanios3c0d91f2009-10-25 13:28:03 -05002644 istk->mstk->condcnt --;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002645 free_tlist(origline);
2646 return DIRECTIVE_FOUND;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002647
H. Peter Anvindb8f96e2009-07-15 09:07:29 -04002648 case PP_RMACRO:
2649 case PP_IRMACRO:
H. Peter Anvine2c80182005-01-15 22:15:51 +00002650 case PP_MACRO:
2651 case PP_IMACRO:
H. Peter Anvina26433d2008-07-16 14:40:01 -07002652 if (defining) {
H. Peter Anvindb8f96e2009-07-15 09:07:29 -04002653 error(ERR_FATAL, "`%s': already defining a macro",
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002654 pp_directives[i]);
2655 return DIRECTIVE_FOUND;
2656 }
2657 defining = nasm_malloc(sizeof(MMacro));
2658 defining->max_depth =
2659 (i == PP_RMACRO) || (i == PP_IRMACRO) ? DEADMAN_LIMIT : 0;
2660 defining->casesense = (i == PP_MACRO) || (i == PP_RMACRO);
2661 if (!parse_mmacro_spec(tline, defining, pp_directives[i])) {
2662 nasm_free(defining);
2663 defining = NULL;
2664 return DIRECTIVE_FOUND;
2665 }
H. Peter Anvina26433d2008-07-16 14:40:01 -07002666
H. Peter Anvin166c2472008-05-28 12:28:58 -07002667 mmac = (MMacro *) hash_findix(&mmacros, defining->name);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002668 while (mmac) {
2669 if (!strcmp(mmac->name, defining->name) &&
2670 (mmac->nparam_min <= defining->nparam_max
2671 || defining->plus)
2672 && (defining->nparam_min <= mmac->nparam_max
2673 || mmac->plus)) {
H. Peter Anvin917a3492008-09-24 09:14:49 -07002674 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +00002675 "redefining multi-line macro `%s'", defining->name);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002676 return DIRECTIVE_FOUND;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002677 }
2678 mmac = mmac->next;
2679 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002680 free_tlist(origline);
2681 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002682
H. Peter Anvine2c80182005-01-15 22:15:51 +00002683 case PP_ENDM:
2684 case PP_ENDMACRO:
Victor van den Elzen8f1120f2008-07-16 13:41:37 +02002685 if (! (defining && defining->name)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002686 error(ERR_NONFATAL, "`%s': not defining a macro", tline->text);
2687 return DIRECTIVE_FOUND;
2688 }
Keith Kanios852f1ee2009-07-12 00:19:55 -05002689 mmhead = (MMacro **) hash_findi_add(&mmacros, defining->name);
H. Peter Anvin97a23472007-09-16 17:57:25 -07002690 defining->next = *mmhead;
Keith Kanios852f1ee2009-07-12 00:19:55 -05002691 *mmhead = defining;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002692 defining = NULL;
2693 free_tlist(origline);
2694 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002695
H. Peter Anvin89cee572009-07-15 09:16:54 -04002696 case PP_EXITMACRO:
Keith Kanios852f1ee2009-07-12 00:19:55 -05002697 /*
2698 * We must search along istk->expansion until we hit a
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002699 * macro-end marker for a macro with a name. Then we
Keith Kanios3c0d91f2009-10-25 13:28:03 -05002700 * bypass all lines between exitmacro and endmacro.
Keith Kanios852f1ee2009-07-12 00:19:55 -05002701 */
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04002702 list_for_each(l, istk->expansion)
Keith Kanios852f1ee2009-07-12 00:19:55 -05002703 if (l->finishes && l->finishes->name)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002704 break;
Keith Kanios852f1ee2009-07-12 00:19:55 -05002705
2706 if (l) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002707 /*
2708 * Remove all conditional entries relative to this
2709 * macro invocation. (safe to do in this context)
2710 */
Keith Kanios3c0d91f2009-10-25 13:28:03 -05002711 for ( ; l->finishes->condcnt > 0; l->finishes->condcnt --) {
2712 cond = istk->conds;
2713 istk->conds = cond->next;
2714 nasm_free(cond);
2715 }
2716 istk->expansion = l;
Keith Kanios852f1ee2009-07-12 00:19:55 -05002717 } else {
2718 error(ERR_NONFATAL, "`%%exitmacro' not within `%%macro' block");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002719 }
Keith Kanios852f1ee2009-07-12 00:19:55 -05002720 free_tlist(origline);
2721 return DIRECTIVE_FOUND;
2722
H. Peter Anvina26433d2008-07-16 14:40:01 -07002723 case PP_UNMACRO:
2724 case PP_UNIMACRO:
2725 {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002726 MMacro **mmac_p;
2727 MMacro spec;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002728
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002729 spec.casesense = (i == PP_UNMACRO);
2730 if (!parse_mmacro_spec(tline, &spec, pp_directives[i])) {
2731 return DIRECTIVE_FOUND;
2732 }
H. Peter Anvina26433d2008-07-16 14:40:01 -07002733 mmac_p = (MMacro **) hash_findi(&mmacros, spec.name, NULL);
2734 while (mmac_p && *mmac_p) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002735 mmac = *mmac_p;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002736 if (mmac->casesense == spec.casesense &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002737 !mstrcmp(mmac->name, spec.name, spec.casesense) &&
H. Peter Anvina26433d2008-07-16 14:40:01 -07002738 mmac->nparam_min == spec.nparam_min &&
2739 mmac->nparam_max == spec.nparam_max &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002740 mmac->plus == spec.plus) {
2741 *mmac_p = mmac->next;
2742 free_mmacro(mmac);
2743 } else {
2744 mmac_p = &mmac->next;
2745 }
2746 }
2747 free_tlist(origline);
2748 free_tlist(spec.dlist);
2749 return DIRECTIVE_FOUND;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002750 }
2751
H. Peter Anvine2c80182005-01-15 22:15:51 +00002752 case PP_ROTATE:
2753 if (tline->next && tline->next->type == TOK_WHITESPACE)
2754 tline = tline->next;
H. Peter Anvin89cee572009-07-15 09:16:54 -04002755 if (!tline->next) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002756 free_tlist(origline);
2757 error(ERR_NONFATAL, "`%%rotate' missing rotate count");
2758 return DIRECTIVE_FOUND;
2759 }
2760 t = expand_smacro(tline->next);
2761 tline->next = NULL;
2762 free_tlist(origline);
2763 tline = t;
2764 tptr = &t;
2765 tokval.t_type = TOKEN_INVALID;
2766 evalresult =
2767 evaluate(ppscan, tptr, &tokval, NULL, pass, error, NULL);
2768 free_tlist(tline);
2769 if (!evalresult)
2770 return DIRECTIVE_FOUND;
2771 if (tokval.t_type)
H. Peter Anvin917a3492008-09-24 09:14:49 -07002772 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +00002773 "trailing garbage after expression ignored");
2774 if (!is_simple(evalresult)) {
2775 error(ERR_NONFATAL, "non-constant value given to `%%rotate'");
2776 return DIRECTIVE_FOUND;
2777 }
2778 mmac = istk->mstk;
2779 while (mmac && !mmac->name) /* avoid mistaking %reps for macros */
2780 mmac = mmac->next_active;
2781 if (!mmac) {
2782 error(ERR_NONFATAL, "`%%rotate' invoked outside a macro call");
2783 } else if (mmac->nparam == 0) {
2784 error(ERR_NONFATAL,
2785 "`%%rotate' invoked within macro without parameters");
2786 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002787 int rotate = mmac->rotate + reloc_value(evalresult);
H. Peter Anvin734b1882002-04-30 21:01:08 +00002788
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002789 rotate %= (int)mmac->nparam;
2790 if (rotate < 0)
2791 rotate += mmac->nparam;
H. Peter Anvin70653092007-10-19 14:42:29 -07002792
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002793 mmac->rotate = rotate;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002794 }
2795 return DIRECTIVE_FOUND;
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00002796
H. Peter Anvine2c80182005-01-15 22:15:51 +00002797 case PP_REP:
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002798 nolist = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002799 do {
2800 tline = tline->next;
2801 } while (tok_type_(tline, TOK_WHITESPACE));
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00002802
H. Peter Anvine2c80182005-01-15 22:15:51 +00002803 if (tok_type_(tline, TOK_ID) &&
2804 nasm_stricmp(tline->text, ".nolist") == 0) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002805 nolist = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002806 do {
2807 tline = tline->next;
2808 } while (tok_type_(tline, TOK_WHITESPACE));
2809 }
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00002810
H. Peter Anvine2c80182005-01-15 22:15:51 +00002811 if (tline) {
2812 t = expand_smacro(tline);
2813 tptr = &t;
2814 tokval.t_type = TOKEN_INVALID;
2815 evalresult =
2816 evaluate(ppscan, tptr, &tokval, NULL, pass, error, NULL);
2817 if (!evalresult) {
2818 free_tlist(origline);
2819 return DIRECTIVE_FOUND;
2820 }
2821 if (tokval.t_type)
H. Peter Anvin917a3492008-09-24 09:14:49 -07002822 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +00002823 "trailing garbage after expression ignored");
2824 if (!is_simple(evalresult)) {
2825 error(ERR_NONFATAL, "non-constant value given to `%%rep'");
2826 return DIRECTIVE_FOUND;
2827 }
H. Peter Anvinf8ba53e2007-10-11 10:11:57 -07002828 count = reloc_value(evalresult) + 1;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002829 } else {
2830 error(ERR_NONFATAL, "`%%rep' expects a repeat count");
H. Peter Anvinf8ba53e2007-10-11 10:11:57 -07002831 count = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002832 }
2833 free_tlist(origline);
H. Peter Anvin734b1882002-04-30 21:01:08 +00002834
H. Peter Anvine2c80182005-01-15 22:15:51 +00002835 tmp_defining = defining;
2836 defining = nasm_malloc(sizeof(MMacro));
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002837 defining->prev = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002838 defining->name = NULL; /* flags this macro as a %rep block */
H. Peter Anvin70055962007-10-11 00:05:31 -07002839 defining->casesense = false;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002840 defining->plus = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002841 defining->nolist = nolist;
H. Peter Anvinf8ba53e2007-10-11 10:11:57 -07002842 defining->in_progress = count;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002843 defining->max_depth = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002844 defining->nparam_min = defining->nparam_max = 0;
2845 defining->defaults = NULL;
2846 defining->dlist = NULL;
2847 defining->expansion = NULL;
2848 defining->next_active = istk->mstk;
2849 defining->rep_nest = tmp_defining;
2850 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002851
H. Peter Anvine2c80182005-01-15 22:15:51 +00002852 case PP_ENDREP:
2853 if (!defining || defining->name) {
2854 error(ERR_NONFATAL, "`%%endrep': no matching `%%rep'");
2855 return DIRECTIVE_FOUND;
2856 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00002857
H. Peter Anvine2c80182005-01-15 22:15:51 +00002858 /*
2859 * Now we have a "macro" defined - although it has no name
2860 * and we won't be entering it in the hash tables - we must
2861 * push a macro-end marker for it on to istk->expansion.
2862 * After that, it will take care of propagating itself (a
2863 * macro-end marker line for a macro which is really a %rep
2864 * block will cause the macro to be re-expanded, complete
2865 * with another macro-end marker to ensure the process
2866 * continues) until the whole expansion is forcibly removed
2867 * from istk->expansion by a %exitrep.
2868 */
2869 l = nasm_malloc(sizeof(Line));
2870 l->next = istk->expansion;
2871 l->finishes = defining;
2872 l->first = NULL;
2873 istk->expansion = l;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002874
H. Peter Anvine2c80182005-01-15 22:15:51 +00002875 istk->mstk = defining;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002876
H. Peter Anvine2c80182005-01-15 22:15:51 +00002877 list->uplevel(defining->nolist ? LIST_MACRO_NOLIST : LIST_MACRO);
2878 tmp_defining = defining;
2879 defining = defining->rep_nest;
2880 free_tlist(origline);
2881 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002882
H. Peter Anvine2c80182005-01-15 22:15:51 +00002883 case PP_EXITREP:
2884 /*
2885 * We must search along istk->expansion until we hit a
2886 * macro-end marker for a macro with no name. Then we set
2887 * its `in_progress' flag to 0.
2888 */
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04002889 list_for_each(l, istk->expansion)
H. Peter Anvine2c80182005-01-15 22:15:51 +00002890 if (l->finishes && !l->finishes->name)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002891 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002892
H. Peter Anvine2c80182005-01-15 22:15:51 +00002893 if (l)
Charles Crayned4200be2008-07-12 16:42:33 -07002894 l->finishes->in_progress = 1;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002895 else
2896 error(ERR_NONFATAL, "`%%exitrep' not within `%%rep' block");
2897 free_tlist(origline);
2898 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002899
H. Peter Anvine2c80182005-01-15 22:15:51 +00002900 case PP_XDEFINE:
2901 case PP_IXDEFINE:
2902 case PP_DEFINE:
2903 case PP_IDEFINE:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002904 casesense = (i == PP_DEFINE || i == PP_XDEFINE);
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07002905
H. Peter Anvine2c80182005-01-15 22:15:51 +00002906 tline = tline->next;
2907 skip_white_(tline);
2908 tline = expand_id(tline);
2909 if (!tline || (tline->type != TOK_ID &&
2910 (tline->type != TOK_PREPROC_ID ||
2911 tline->text[1] != '$'))) {
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07002912 error(ERR_NONFATAL, "`%s' expects a macro identifier",
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002913 pp_directives[i]);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002914 free_tlist(origline);
2915 return DIRECTIVE_FOUND;
2916 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00002917
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08002918 ctx = get_ctx(tline->text, &mname, false);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002919 last = tline;
2920 param_start = tline = tline->next;
2921 nparam = 0;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002922
H. Peter Anvine2c80182005-01-15 22:15:51 +00002923 /* Expand the macro definition now for %xdefine and %ixdefine */
2924 if ((i == PP_XDEFINE) || (i == PP_IXDEFINE))
2925 tline = expand_smacro(tline);
H. Peter Anvin734b1882002-04-30 21:01:08 +00002926
H. Peter Anvine2c80182005-01-15 22:15:51 +00002927 if (tok_is_(tline, "(")) {
2928 /*
2929 * This macro has parameters.
2930 */
H. Peter Anvin734b1882002-04-30 21:01:08 +00002931
H. Peter Anvine2c80182005-01-15 22:15:51 +00002932 tline = tline->next;
2933 while (1) {
2934 skip_white_(tline);
2935 if (!tline) {
2936 error(ERR_NONFATAL, "parameter identifier expected");
2937 free_tlist(origline);
2938 return DIRECTIVE_FOUND;
2939 }
2940 if (tline->type != TOK_ID) {
2941 error(ERR_NONFATAL,
2942 "`%s': parameter identifier expected",
2943 tline->text);
2944 free_tlist(origline);
2945 return DIRECTIVE_FOUND;
2946 }
2947 tline->type = TOK_SMAC_PARAM + nparam++;
2948 tline = tline->next;
2949 skip_white_(tline);
2950 if (tok_is_(tline, ",")) {
2951 tline = tline->next;
H. Peter Anvinca348b62008-07-23 10:49:26 -04002952 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002953 if (!tok_is_(tline, ")")) {
2954 error(ERR_NONFATAL,
2955 "`)' expected to terminate macro template");
2956 free_tlist(origline);
2957 return DIRECTIVE_FOUND;
2958 }
2959 break;
2960 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002961 }
2962 last = tline;
2963 tline = tline->next;
2964 }
2965 if (tok_type_(tline, TOK_WHITESPACE))
2966 last = tline, tline = tline->next;
2967 macro_start = NULL;
2968 last->next = NULL;
2969 t = tline;
2970 while (t) {
2971 if (t->type == TOK_ID) {
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04002972 list_for_each(tt, param_start)
H. Peter Anvine2c80182005-01-15 22:15:51 +00002973 if (tt->type >= TOK_SMAC_PARAM &&
2974 !strcmp(tt->text, t->text))
2975 t->type = tt->type;
2976 }
2977 tt = t->next;
2978 t->next = macro_start;
2979 macro_start = t;
2980 t = tt;
2981 }
2982 /*
2983 * Good. We now have a macro name, a parameter count, and a
2984 * token list (in reverse order) for an expansion. We ought
2985 * to be OK just to create an SMacro, store it, and let
2986 * free_tlist have the rest of the line (which we have
2987 * carefully re-terminated after chopping off the expansion
2988 * from the end).
2989 */
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002990 define_smacro(ctx, mname, casesense, nparam, macro_start);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002991 free_tlist(origline);
2992 return DIRECTIVE_FOUND;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002993
H. Peter Anvine2c80182005-01-15 22:15:51 +00002994 case PP_UNDEF:
2995 tline = tline->next;
2996 skip_white_(tline);
2997 tline = expand_id(tline);
2998 if (!tline || (tline->type != TOK_ID &&
2999 (tline->type != TOK_PREPROC_ID ||
3000 tline->text[1] != '$'))) {
3001 error(ERR_NONFATAL, "`%%undef' expects a macro identifier");
3002 free_tlist(origline);
3003 return DIRECTIVE_FOUND;
3004 }
3005 if (tline->next) {
H. Peter Anvin917a3492008-09-24 09:14:49 -07003006 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +00003007 "trailing garbage after macro name ignored");
3008 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003009
H. Peter Anvine2c80182005-01-15 22:15:51 +00003010 /* Find the context that symbol belongs to */
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08003011 ctx = get_ctx(tline->text, &mname, false);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003012 undef_smacro(ctx, mname);
3013 free_tlist(origline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003014 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003015
H. Peter Anvin9e200162008-06-04 17:23:14 -07003016 case PP_DEFSTR:
3017 case PP_IDEFSTR:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003018 casesense = (i == PP_DEFSTR);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003019
3020 tline = tline->next;
3021 skip_white_(tline);
3022 tline = expand_id(tline);
3023 if (!tline || (tline->type != TOK_ID &&
3024 (tline->type != TOK_PREPROC_ID ||
3025 tline->text[1] != '$'))) {
3026 error(ERR_NONFATAL, "`%s' expects a macro identifier",
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003027 pp_directives[i]);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003028 free_tlist(origline);
3029 return DIRECTIVE_FOUND;
3030 }
3031
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08003032 ctx = get_ctx(tline->text, &mname, false);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003033 last = tline;
3034 tline = expand_smacro(tline->next);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003035 last->next = NULL;
H. Peter Anvin9e200162008-06-04 17:23:14 -07003036
3037 while (tok_type_(tline, TOK_WHITESPACE))
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003038 tline = delete_Token(tline);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003039
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003040 p = detoken(tline, false);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003041 macro_start = nasm_malloc(sizeof(*macro_start));
3042 macro_start->next = NULL;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003043 macro_start->text = nasm_quote(p, strlen(p));
3044 macro_start->type = TOK_STRING;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003045 macro_start->a.mac = NULL;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003046 nasm_free(p);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003047
3048 /*
3049 * We now have a macro name, an implicit parameter count of
3050 * zero, and a string token to use as an expansion. Create
3051 * and store an SMacro.
3052 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003053 define_smacro(ctx, mname, casesense, 0, macro_start);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003054 free_tlist(origline);
3055 return DIRECTIVE_FOUND;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003056
H. Peter Anvin2f55bda2009-07-14 15:04:04 -04003057 case PP_DEFTOK:
3058 case PP_IDEFTOK:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003059 casesense = (i == PP_DEFTOK);
3060
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003061 tline = tline->next;
3062 skip_white_(tline);
3063 tline = expand_id(tline);
3064 if (!tline || (tline->type != TOK_ID &&
3065 (tline->type != TOK_PREPROC_ID ||
3066 tline->text[1] != '$'))) {
3067 error(ERR_NONFATAL,
3068 "`%s' expects a macro identifier as first parameter",
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003069 pp_directives[i]);
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003070 free_tlist(origline);
3071 return DIRECTIVE_FOUND;
3072 }
3073 ctx = get_ctx(tline->text, &mname, false);
3074 last = tline;
3075 tline = expand_smacro(tline->next);
3076 last->next = NULL;
3077
3078 t = tline;
3079 while (tok_type_(t, TOK_WHITESPACE))
3080 t = t->next;
3081 /* t should now point to the string */
3082 if (t->type != TOK_STRING) {
3083 error(ERR_NONFATAL,
3084 "`%s` requires string as second parameter",
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003085 pp_directives[i]);
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003086 free_tlist(tline);
3087 free_tlist(origline);
3088 return DIRECTIVE_FOUND;
3089 }
3090
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003091 nasm_unquote_cstr(t->text, i);
3092 macro_start = tokenize(t->text);
3093
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003094 /*
3095 * We now have a macro name, an implicit parameter count of
3096 * zero, and a numeric token to use as an expansion. Create
3097 * and store an SMacro.
3098 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003099 define_smacro(ctx, mname, casesense, 0, macro_start);
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003100 free_tlist(tline);
3101 free_tlist(origline);
3102 return DIRECTIVE_FOUND;
H. Peter Anvin9e200162008-06-04 17:23:14 -07003103
H. Peter Anvin418ca702008-05-30 10:42:30 -07003104 case PP_PATHSEARCH:
3105 {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003106 FILE *fp;
3107 StrList *xsl = NULL;
3108 StrList **xst = &xsl;
H. Peter Anvin418ca702008-05-30 10:42:30 -07003109
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003110 casesense = true;
H. Peter Anvin418ca702008-05-30 10:42:30 -07003111
3112 tline = tline->next;
3113 skip_white_(tline);
3114 tline = expand_id(tline);
3115 if (!tline || (tline->type != TOK_ID &&
3116 (tline->type != TOK_PREPROC_ID ||
3117 tline->text[1] != '$'))) {
3118 error(ERR_NONFATAL,
3119 "`%%pathsearch' expects a macro identifier as first parameter");
3120 free_tlist(origline);
3121 return DIRECTIVE_FOUND;
3122 }
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08003123 ctx = get_ctx(tline->text, &mname, false);
H. Peter Anvin418ca702008-05-30 10:42:30 -07003124 last = tline;
3125 tline = expand_smacro(tline->next);
3126 last->next = NULL;
3127
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003128 t = tline;
H. Peter Anvin418ca702008-05-30 10:42:30 -07003129 while (tok_type_(t, TOK_WHITESPACE))
3130 t = t->next;
3131
3132 if (!t || (t->type != TOK_STRING &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003133 t->type != TOK_INTERNAL_STRING)) {
H. Peter Anvin418ca702008-05-30 10:42:30 -07003134 error(ERR_NONFATAL, "`%%pathsearch' expects a file name");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003135 free_tlist(tline);
H. Peter Anvin418ca702008-05-30 10:42:30 -07003136 free_tlist(origline);
3137 return DIRECTIVE_FOUND; /* but we did _something_ */
3138 }
3139 if (t->next)
H. Peter Anvin917a3492008-09-24 09:14:49 -07003140 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvin418ca702008-05-30 10:42:30 -07003141 "trailing garbage after `%%pathsearch' ignored");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003142 p = t->text;
H. Peter Anvin427cc912008-06-01 21:43:03 -07003143 if (t->type != TOK_INTERNAL_STRING)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003144 nasm_unquote(p, NULL);
H. Peter Anvin418ca702008-05-30 10:42:30 -07003145
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003146 fp = inc_fopen(p, &xsl, &xst, true);
3147 if (fp) {
3148 p = xsl->str;
3149 fclose(fp); /* Don't actually care about the file */
3150 }
H. Peter Anvin418ca702008-05-30 10:42:30 -07003151 macro_start = nasm_malloc(sizeof(*macro_start));
3152 macro_start->next = NULL;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003153 macro_start->text = nasm_quote(p, strlen(p));
3154 macro_start->type = TOK_STRING;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003155 macro_start->a.mac = NULL;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003156 if (xsl)
3157 nasm_free(xsl);
H. Peter Anvin418ca702008-05-30 10:42:30 -07003158
3159 /*
3160 * We now have a macro name, an implicit parameter count of
3161 * zero, and a string token to use as an expansion. Create
3162 * and store an SMacro.
3163 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003164 define_smacro(ctx, mname, casesense, 0, macro_start);
3165 free_tlist(tline);
H. Peter Anvin418ca702008-05-30 10:42:30 -07003166 free_tlist(origline);
3167 return DIRECTIVE_FOUND;
3168 }
3169
H. Peter Anvine2c80182005-01-15 22:15:51 +00003170 case PP_STRLEN:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003171 casesense = true;
H. Peter Anvin70653092007-10-19 14:42:29 -07003172
H. Peter Anvine2c80182005-01-15 22:15:51 +00003173 tline = tline->next;
3174 skip_white_(tline);
3175 tline = expand_id(tline);
3176 if (!tline || (tline->type != TOK_ID &&
3177 (tline->type != TOK_PREPROC_ID ||
3178 tline->text[1] != '$'))) {
3179 error(ERR_NONFATAL,
3180 "`%%strlen' expects a macro identifier as first parameter");
3181 free_tlist(origline);
3182 return DIRECTIVE_FOUND;
3183 }
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08003184 ctx = get_ctx(tline->text, &mname, false);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003185 last = tline;
3186 tline = expand_smacro(tline->next);
3187 last->next = NULL;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003188
H. Peter Anvine2c80182005-01-15 22:15:51 +00003189 t = tline;
3190 while (tok_type_(t, TOK_WHITESPACE))
3191 t = t->next;
3192 /* t should now point to the string */
3193 if (t->type != TOK_STRING) {
3194 error(ERR_NONFATAL,
3195 "`%%strlen` requires string as second parameter");
3196 free_tlist(tline);
3197 free_tlist(origline);
3198 return DIRECTIVE_FOUND;
3199 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003200
H. Peter Anvine2c80182005-01-15 22:15:51 +00003201 macro_start = nasm_malloc(sizeof(*macro_start));
3202 macro_start->next = NULL;
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07003203 make_tok_num(macro_start, nasm_unquote(t->text, NULL));
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003204 macro_start->a.mac = NULL;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003205
H. Peter Anvine2c80182005-01-15 22:15:51 +00003206 /*
3207 * We now have a macro name, an implicit parameter count of
3208 * zero, and a numeric token to use as an expansion. Create
3209 * and store an SMacro.
3210 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003211 define_smacro(ctx, mname, casesense, 0, macro_start);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003212 free_tlist(tline);
3213 free_tlist(origline);
3214 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003215
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003216 case PP_STRCAT:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003217 casesense = true;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003218
3219 tline = tline->next;
3220 skip_white_(tline);
3221 tline = expand_id(tline);
3222 if (!tline || (tline->type != TOK_ID &&
3223 (tline->type != TOK_PREPROC_ID ||
3224 tline->text[1] != '$'))) {
3225 error(ERR_NONFATAL,
3226 "`%%strcat' expects a macro identifier as first parameter");
3227 free_tlist(origline);
3228 return DIRECTIVE_FOUND;
3229 }
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08003230 ctx = get_ctx(tline->text, &mname, false);
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003231 last = tline;
3232 tline = expand_smacro(tline->next);
3233 last->next = NULL;
3234
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003235 len = 0;
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04003236 list_for_each(t, tline) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003237 switch (t->type) {
3238 case TOK_WHITESPACE:
3239 break;
3240 case TOK_STRING:
3241 len += t->a.len = nasm_unquote(t->text, NULL);
3242 break;
3243 case TOK_OTHER:
3244 if (!strcmp(t->text, ",")) /* permit comma separators */
3245 break;
3246 /* else fall through */
3247 default:
3248 error(ERR_NONFATAL,
3249 "non-string passed to `%%strcat' (%d)", t->type);
3250 free_tlist(tline);
3251 free_tlist(origline);
3252 return DIRECTIVE_FOUND;
3253 }
3254 }
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003255
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003256 p = pp = nasm_malloc(len);
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04003257 list_for_each(t, tline) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003258 if (t->type == TOK_STRING) {
3259 memcpy(p, t->text, t->a.len);
3260 p += t->a.len;
3261 }
3262 }
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003263
3264 /*
3265 * We now have a macro name, an implicit parameter count of
3266 * zero, and a numeric token to use as an expansion. Create
3267 * and store an SMacro.
3268 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003269 macro_start = new_Token(NULL, TOK_STRING, NULL, 0);
3270 macro_start->text = nasm_quote(pp, len);
3271 nasm_free(pp);
3272 define_smacro(ctx, mname, casesense, 0, macro_start);
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003273 free_tlist(tline);
3274 free_tlist(origline);
3275 return DIRECTIVE_FOUND;
3276
H. Peter Anvine2c80182005-01-15 22:15:51 +00003277 case PP_SUBSTR:
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003278 {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003279 int64_t a1, a2;
3280 size_t len;
H. Peter Anvind2456592008-06-19 15:04:18 -07003281
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003282 casesense = true;
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07003283
H. Peter Anvine2c80182005-01-15 22:15:51 +00003284 tline = tline->next;
3285 skip_white_(tline);
3286 tline = expand_id(tline);
3287 if (!tline || (tline->type != TOK_ID &&
3288 (tline->type != TOK_PREPROC_ID ||
3289 tline->text[1] != '$'))) {
3290 error(ERR_NONFATAL,
3291 "`%%substr' expects a macro identifier as first parameter");
3292 free_tlist(origline);
3293 return DIRECTIVE_FOUND;
3294 }
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08003295 ctx = get_ctx(tline->text, &mname, false);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003296 last = tline;
3297 tline = expand_smacro(tline->next);
3298 last->next = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003299
H. Peter Anvine2c80182005-01-15 22:15:51 +00003300 t = tline->next;
3301 while (tok_type_(t, TOK_WHITESPACE))
3302 t = t->next;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003303
H. Peter Anvine2c80182005-01-15 22:15:51 +00003304 /* t should now point to the string */
3305 if (t->type != TOK_STRING) {
3306 error(ERR_NONFATAL,
3307 "`%%substr` requires string as second parameter");
3308 free_tlist(tline);
3309 free_tlist(origline);
3310 return DIRECTIVE_FOUND;
3311 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003312
H. Peter Anvine2c80182005-01-15 22:15:51 +00003313 tt = t->next;
3314 tptr = &tt;
3315 tokval.t_type = TOKEN_INVALID;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003316 evalresult = evaluate(ppscan, tptr, &tokval, NULL,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003317 pass, error, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003318 if (!evalresult) {
3319 free_tlist(tline);
3320 free_tlist(origline);
3321 return DIRECTIVE_FOUND;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003322 } else if (!is_simple(evalresult)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00003323 error(ERR_NONFATAL, "non-constant value given to `%%substr`");
3324 free_tlist(tline);
3325 free_tlist(origline);
3326 return DIRECTIVE_FOUND;
3327 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003328 a1 = evalresult->value-1;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003329
3330 while (tok_type_(tt, TOK_WHITESPACE))
3331 tt = tt->next;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003332 if (!tt) {
3333 a2 = 1; /* Backwards compatibility: one character */
3334 } else {
3335 tokval.t_type = TOKEN_INVALID;
3336 evalresult = evaluate(ppscan, tptr, &tokval, NULL,
3337 pass, error, NULL);
3338 if (!evalresult) {
3339 free_tlist(tline);
3340 free_tlist(origline);
3341 return DIRECTIVE_FOUND;
3342 } else if (!is_simple(evalresult)) {
3343 error(ERR_NONFATAL, "non-constant value given to `%%substr`");
3344 free_tlist(tline);
3345 free_tlist(origline);
3346 return DIRECTIVE_FOUND;
3347 }
3348 a2 = evalresult->value;
3349 }
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003350
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003351 len = nasm_unquote(t->text, NULL);
3352 if (a2 < 0)
3353 a2 = a2+1+len-a1;
3354 if (a1+a2 > (int64_t)len)
3355 a2 = len-a1;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003356
H. Peter Anvine2c80182005-01-15 22:15:51 +00003357 macro_start = nasm_malloc(sizeof(*macro_start));
3358 macro_start->next = NULL;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003359 macro_start->text = nasm_quote((a1 < 0) ? "" : t->text+a1, a2);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003360 macro_start->type = TOK_STRING;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003361 macro_start->a.mac = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003362
H. Peter Anvine2c80182005-01-15 22:15:51 +00003363 /*
3364 * We now have a macro name, an implicit parameter count of
3365 * zero, and a numeric token to use as an expansion. Create
3366 * and store an SMacro.
3367 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003368 define_smacro(ctx, mname, casesense, 0, macro_start);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003369 free_tlist(tline);
3370 free_tlist(origline);
3371 return DIRECTIVE_FOUND;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003372 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003373
H. Peter Anvine2c80182005-01-15 22:15:51 +00003374 case PP_ASSIGN:
3375 case PP_IASSIGN:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003376 casesense = (i == PP_ASSIGN);
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07003377
H. Peter Anvine2c80182005-01-15 22:15:51 +00003378 tline = tline->next;
3379 skip_white_(tline);
3380 tline = expand_id(tline);
3381 if (!tline || (tline->type != TOK_ID &&
3382 (tline->type != TOK_PREPROC_ID ||
3383 tline->text[1] != '$'))) {
3384 error(ERR_NONFATAL,
3385 "`%%%sassign' expects a macro identifier",
3386 (i == PP_IASSIGN ? "i" : ""));
3387 free_tlist(origline);
3388 return DIRECTIVE_FOUND;
3389 }
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08003390 ctx = get_ctx(tline->text, &mname, false);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003391 last = tline;
3392 tline = expand_smacro(tline->next);
3393 last->next = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003394
H. Peter Anvine2c80182005-01-15 22:15:51 +00003395 t = tline;
3396 tptr = &t;
3397 tokval.t_type = TOKEN_INVALID;
3398 evalresult =
3399 evaluate(ppscan, tptr, &tokval, NULL, pass, error, NULL);
3400 free_tlist(tline);
3401 if (!evalresult) {
3402 free_tlist(origline);
3403 return DIRECTIVE_FOUND;
3404 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003405
H. Peter Anvine2c80182005-01-15 22:15:51 +00003406 if (tokval.t_type)
H. Peter Anvin917a3492008-09-24 09:14:49 -07003407 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +00003408 "trailing garbage after expression ignored");
H. Peter Anvin734b1882002-04-30 21:01:08 +00003409
H. Peter Anvine2c80182005-01-15 22:15:51 +00003410 if (!is_simple(evalresult)) {
3411 error(ERR_NONFATAL,
3412 "non-constant value given to `%%%sassign'",
3413 (i == PP_IASSIGN ? "i" : ""));
3414 free_tlist(origline);
3415 return DIRECTIVE_FOUND;
3416 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003417
H. Peter Anvine2c80182005-01-15 22:15:51 +00003418 macro_start = nasm_malloc(sizeof(*macro_start));
3419 macro_start->next = NULL;
3420 make_tok_num(macro_start, reloc_value(evalresult));
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003421 macro_start->a.mac = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003422
H. Peter Anvine2c80182005-01-15 22:15:51 +00003423 /*
3424 * We now have a macro name, an implicit parameter count of
3425 * zero, and a numeric token to use as an expansion. Create
3426 * and store an SMacro.
3427 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003428 define_smacro(ctx, mname, casesense, 0, macro_start);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003429 free_tlist(origline);
3430 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003431
H. Peter Anvine2c80182005-01-15 22:15:51 +00003432 case PP_LINE:
3433 /*
3434 * Syntax is `%line nnn[+mmm] [filename]'
3435 */
3436 tline = tline->next;
3437 skip_white_(tline);
3438 if (!tok_type_(tline, TOK_NUMBER)) {
3439 error(ERR_NONFATAL, "`%%line' expects line number");
3440 free_tlist(origline);
3441 return DIRECTIVE_FOUND;
3442 }
H. Peter Anvin70055962007-10-11 00:05:31 -07003443 k = readnum(tline->text, &err);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003444 m = 1;
3445 tline = tline->next;
3446 if (tok_is_(tline, "+")) {
3447 tline = tline->next;
3448 if (!tok_type_(tline, TOK_NUMBER)) {
3449 error(ERR_NONFATAL, "`%%line' expects line increment");
3450 free_tlist(origline);
3451 return DIRECTIVE_FOUND;
3452 }
H. Peter Anvin70055962007-10-11 00:05:31 -07003453 m = readnum(tline->text, &err);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003454 tline = tline->next;
3455 }
3456 skip_white_(tline);
3457 src_set_linnum(k);
3458 istk->lineinc = m;
3459 if (tline) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07003460 nasm_free(src_set_fname(detoken(tline, false)));
H. Peter Anvine2c80182005-01-15 22:15:51 +00003461 }
3462 free_tlist(origline);
3463 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003464
H. Peter Anvine2c80182005-01-15 22:15:51 +00003465 default:
3466 error(ERR_FATAL,
3467 "preprocessor directive `%s' not yet implemented",
H. Peter Anvin4169a472007-09-12 01:29:43 +00003468 pp_directives[i]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003469 return DIRECTIVE_FOUND;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003470 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003471}
3472
3473/*
H. Peter Anvin76690a12002-04-30 20:52:49 +00003474 * Ensure that a macro parameter contains a condition code and
3475 * nothing else. Return the condition code index if so, or -1
3476 * otherwise.
3477 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003478static int find_cc(Token * t)
H. Peter Anvineba20a72002-04-30 20:53:55 +00003479{
H. Peter Anvin76690a12002-04-30 20:52:49 +00003480 Token *tt;
3481 int i, j, k, m;
3482
H. Peter Anvin25a99342007-09-22 17:45:45 -07003483 if (!t)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003484 return -1; /* Probably a %+ without a space */
H. Peter Anvin25a99342007-09-22 17:45:45 -07003485
H. Peter Anvineba20a72002-04-30 20:53:55 +00003486 skip_white_(t);
H. Peter Anvin76690a12002-04-30 20:52:49 +00003487 if (t->type != TOK_ID)
H. Peter Anvine2c80182005-01-15 22:15:51 +00003488 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003489 tt = t->next;
H. Peter Anvineba20a72002-04-30 20:53:55 +00003490 skip_white_(tt);
H. Peter Anvin76690a12002-04-30 20:52:49 +00003491 if (tt && (tt->type != TOK_OTHER || strcmp(tt->text, ",")))
H. Peter Anvine2c80182005-01-15 22:15:51 +00003492 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003493
3494 i = -1;
Cyrill Gorcunova7319242010-06-03 22:04:36 +04003495 j = ARRAY_SIZE(conditions);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003496 while (j - i > 1) {
3497 k = (j + i) / 2;
3498 m = nasm_stricmp(t->text, conditions[k]);
3499 if (m == 0) {
3500 i = k;
3501 j = -2;
3502 break;
3503 } else if (m < 0) {
3504 j = k;
3505 } else
3506 i = k;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003507 }
3508 if (j != -2)
H. Peter Anvine2c80182005-01-15 22:15:51 +00003509 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003510 return i;
3511}
3512
H. Peter Anvind784a082009-04-20 14:01:18 -07003513static bool paste_tokens(Token **head, bool handle_paste_tokens)
3514{
3515 Token **tail, *t, *tt;
3516 Token **paste_head;
3517 bool did_paste = false;
3518 char *tmp;
3519
3520 /* Now handle token pasting... */
3521 paste_head = NULL;
3522 tail = head;
3523 while ((t = *tail) && (tt = t->next)) {
3524 switch (t->type) {
3525 case TOK_WHITESPACE:
3526 if (tt->type == TOK_WHITESPACE) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003527 /* Zap adjacent whitespace tokens */
H. Peter Anvind784a082009-04-20 14:01:18 -07003528 t->next = delete_Token(tt);
3529 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003530 /* Do not advance paste_head here */
3531 tail = &t->next;
3532 }
H. Peter Anvind784a082009-04-20 14:01:18 -07003533 break;
3534 case TOK_ID:
H. Peter Anvind784a082009-04-20 14:01:18 -07003535 case TOK_NUMBER:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003536 case TOK_FLOAT:
3537 {
3538 size_t len = 0;
3539 char *tmp, *p;
H. Peter Anvind784a082009-04-20 14:01:18 -07003540
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003541 while (tt && (tt->type == TOK_ID || tt->type == TOK_PREPROC_ID ||
3542 tt->type == TOK_NUMBER || tt->type == TOK_FLOAT ||
3543 tt->type == TOK_OTHER)) {
3544 len += strlen(tt->text);
3545 tt = tt->next;
3546 }
H. Peter Anvind784a082009-04-20 14:01:18 -07003547
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003548 /*
3549 * Now tt points to the first token after
3550 * the potential paste area...
3551 */
3552 if (tt != t->next) {
3553 /* We have at least two tokens... */
3554 len += strlen(t->text);
3555 p = tmp = nasm_malloc(len+1);
H. Peter Anvind784a082009-04-20 14:01:18 -07003556
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003557 while (t != tt) {
3558 strcpy(p, t->text);
3559 p = strchr(p, '\0');
3560 t = delete_Token(t);
3561 }
H. Peter Anvind784a082009-04-20 14:01:18 -07003562
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003563 t = *tail = tokenize(tmp);
3564 nasm_free(tmp);
H. Peter Anvind784a082009-04-20 14:01:18 -07003565
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003566 while (t->next) {
3567 tail = &t->next;
3568 t = t->next;
3569 }
3570 t->next = tt; /* Attach the remaining token chain */
H. Peter Anvind784a082009-04-20 14:01:18 -07003571
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003572 did_paste = true;
3573 }
3574 paste_head = tail;
3575 tail = &t->next;
3576 break;
3577 }
3578 case TOK_PASTE: /* %+ */
3579 if (handle_paste_tokens) {
3580 /* Zap %+ and whitespace tokens to the right */
3581 while (t && (t->type == TOK_WHITESPACE ||
3582 t->type == TOK_PASTE))
3583 t = *tail = delete_Token(t);
3584 if (!paste_head || !t)
3585 break; /* Nothing to paste with */
3586 tail = paste_head;
3587 t = *tail;
3588 tt = t->next;
3589 while (tok_type_(tt, TOK_WHITESPACE))
3590 tt = t->next = delete_Token(tt);
H. Peter Anvind784a082009-04-20 14:01:18 -07003591
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003592 if (tt) {
3593 tmp = nasm_strcat(t->text, tt->text);
3594 delete_Token(t);
3595 tt = delete_Token(tt);
3596 t = *tail = tokenize(tmp);
3597 nasm_free(tmp);
3598 while (t->next) {
3599 tail = &t->next;
3600 t = t->next;
3601 }
3602 t->next = tt; /* Attach the remaining token chain */
3603 did_paste = true;
3604 }
3605 paste_head = tail;
3606 tail = &t->next;
3607 break;
3608 }
3609 /* else fall through */
3610 default:
Cyrill Gorcunovadabc152010-07-10 02:11:41 +04003611 tail = &t->next;
3612 if (!tok_type_(t->next, TOK_WHITESPACE))
3613 paste_head = tail;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003614 break;
H. Peter Anvind784a082009-04-20 14:01:18 -07003615 }
3616 }
3617 return did_paste;
3618}
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003619
3620/*
3621 * expands to a list of tokens from %{x:y}
3622 */
3623static Token *expand_mmac_params_range(MMacro *mac, Token *tline, Token ***last)
3624{
3625 Token *t = tline, **tt, *tm, *head;
3626 char *pos;
3627 int fst, lst, j, i;
3628
3629 pos = strchr(tline->text, ':');
3630 nasm_assert(pos);
3631
3632 lst = atoi(pos + 1);
3633 fst = atoi(tline->text + 1);
3634
3635 /*
3636 * only macros params are accounted so
3637 * if someone passes %0 -- we reject such
3638 * value(s)
3639 */
3640 if (lst == 0 || fst == 0)
3641 goto err;
3642
3643 /* the values should be sane */
Cyrill Gorcunov2f403752010-06-05 10:47:10 +04003644 if ((fst > (int)mac->nparam || fst < (-(int)mac->nparam)) ||
3645 (lst > (int)mac->nparam || lst < (-(int)mac->nparam)))
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003646 goto err;
3647
Cyrill Gorcunovefb35832010-06-20 01:52:19 +04003648 fst = fst < 0 ? fst + (int)mac->nparam + 1: fst;
3649 lst = lst < 0 ? lst + (int)mac->nparam + 1: lst;
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003650
3651 /* counted from zero */
3652 fst--, lst--;
3653
3654 /*
3655 * it will be at least one token
3656 */
3657 tm = mac->params[(fst + mac->rotate) % mac->nparam];
3658 t = new_Token(NULL, tm->type, tm->text, 0);
3659 head = t, tt = &t->next;
3660 if (fst < lst) {
3661 for (i = fst + 1; i <= lst; i++) {
3662 t = new_Token(NULL, TOK_OTHER, ",", 0);
3663 *tt = t, tt = &t->next;
3664 j = (i + mac->rotate) % mac->nparam;
3665 tm = mac->params[j];
3666 t = new_Token(NULL, tm->type, tm->text, 0);
3667 *tt = t, tt = &t->next;
3668 }
3669 } else {
3670 for (i = fst - 1; i >= lst; i--) {
3671 t = new_Token(NULL, TOK_OTHER, ",", 0);
3672 *tt = t, tt = &t->next;
3673 j = (i + mac->rotate) % mac->nparam;
3674 tm = mac->params[j];
3675 t = new_Token(NULL, tm->type, tm->text, 0);
3676 *tt = t, tt = &t->next;
3677 }
3678 }
3679
3680 *last = tt;
3681 return head;
3682
3683err:
3684 error(ERR_NONFATAL, "`%%{%s}': macro parameters out of range",
3685 &tline->text[1]);
3686 return tline;
3687}
3688
H. Peter Anvin76690a12002-04-30 20:52:49 +00003689/*
3690 * Expand MMacro-local things: parameter references (%0, %n, %+n,
H. Peter Anvin67c63722008-10-26 23:49:00 -07003691 * %-n) and MMacro-local identifiers (%%foo) as well as
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003692 * macro indirection (%[...]) and range (%{..:..}).
H. Peter Anvin76690a12002-04-30 20:52:49 +00003693 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003694static Token *expand_mmac_params(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00003695{
H. Peter Anvin734b1882002-04-30 21:01:08 +00003696 Token *t, *tt, **tail, *thead;
H. Peter Anvin6125b622009-04-08 14:02:25 -07003697 bool changed = false;
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003698 char *pos;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003699
3700 tail = &thead;
3701 thead = NULL;
3702
H. Peter Anvine2c80182005-01-15 22:15:51 +00003703 while (tline) {
3704 if (tline->type == TOK_PREPROC_ID &&
Cyrill Gorcunovca611192010-06-04 09:22:12 +04003705 (((tline->text[1] == '+' || tline->text[1] == '-') && tline->text[2]) ||
3706 (tline->text[1] >= '0' && tline->text[1] <= '9') ||
3707 tline->text[1] == '%')) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00003708 char *text = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003709 int type = 0, cc; /* type = 0 to placate optimisers */
Keith Kaniosa6dfa782007-04-13 16:47:53 +00003710 char tmpbuf[30];
H. Peter Anvin25a99342007-09-22 17:45:45 -07003711 unsigned int n;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003712 int i;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003713 MMacro *mac;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003714
H. Peter Anvine2c80182005-01-15 22:15:51 +00003715 t = tline;
3716 tline = tline->next;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003717
H. Peter Anvine2c80182005-01-15 22:15:51 +00003718 mac = istk->mstk;
3719 while (mac && !mac->name) /* avoid mistaking %reps for macros */
3720 mac = mac->next_active;
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003721 if (!mac) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00003722 error(ERR_NONFATAL, "`%s': not in a macro call", t->text);
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003723 } else {
3724 pos = strchr(t->text, ':');
3725 if (!pos) {
3726 switch (t->text[1]) {
3727 /*
3728 * We have to make a substitution of one of the
3729 * forms %1, %-1, %+1, %%foo, %0.
3730 */
3731 case '0':
3732 type = TOK_NUMBER;
3733 snprintf(tmpbuf, sizeof(tmpbuf), "%d", mac->nparam);
3734 text = nasm_strdup(tmpbuf);
3735 break;
3736 case '%':
H. Peter Anvine2c80182005-01-15 22:15:51 +00003737 type = TOK_ID;
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003738 snprintf(tmpbuf, sizeof(tmpbuf), "..@%"PRIu64".",
3739 mac->unique);
3740 text = nasm_strcat(tmpbuf, t->text + 2);
3741 break;
3742 case '-':
3743 n = atoi(t->text + 2) - 1;
3744 if (n >= mac->nparam)
3745 tt = NULL;
3746 else {
3747 if (mac->nparam > 1)
3748 n = (n + mac->rotate) % mac->nparam;
3749 tt = mac->params[n];
H. Peter Anvine2c80182005-01-15 22:15:51 +00003750 }
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003751 cc = find_cc(tt);
3752 if (cc == -1) {
3753 error(ERR_NONFATAL,
3754 "macro parameter %d is not a condition code",
3755 n + 1);
3756 text = NULL;
3757 } else {
3758 type = TOK_ID;
3759 if (inverse_ccs[cc] == -1) {
3760 error(ERR_NONFATAL,
3761 "condition code `%s' is not invertible",
3762 conditions[cc]);
3763 text = NULL;
3764 } else
3765 text = nasm_strdup(conditions[inverse_ccs[cc]]);
3766 }
3767 break;
3768 case '+':
3769 n = atoi(t->text + 2) - 1;
3770 if (n >= mac->nparam)
3771 tt = NULL;
3772 else {
3773 if (mac->nparam > 1)
3774 n = (n + mac->rotate) % mac->nparam;
3775 tt = mac->params[n];
3776 }
3777 cc = find_cc(tt);
3778 if (cc == -1) {
3779 error(ERR_NONFATAL,
3780 "macro parameter %d is not a condition code",
3781 n + 1);
3782 text = NULL;
3783 } else {
3784 type = TOK_ID;
3785 text = nasm_strdup(conditions[cc]);
3786 }
3787 break;
3788 default:
3789 n = atoi(t->text + 1) - 1;
3790 if (n >= mac->nparam)
3791 tt = NULL;
3792 else {
3793 if (mac->nparam > 1)
3794 n = (n + mac->rotate) % mac->nparam;
3795 tt = mac->params[n];
3796 }
3797 if (tt) {
3798 for (i = 0; i < mac->paramlen[n]; i++) {
3799 *tail = new_Token(NULL, tt->type, tt->text, 0);
3800 tail = &(*tail)->next;
3801 tt = tt->next;
3802 }
3803 }
3804 text = NULL; /* we've done it here */
3805 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003806 }
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003807 } else {
3808 /*
3809 * seems we have a parameters range here
3810 */
3811 Token *head, **last;
3812 head = expand_mmac_params_range(mac, t, &last);
3813 if (head != t) {
3814 *tail = head;
3815 *last = tline;
3816 tline = head;
3817 text = NULL;
3818 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00003819 }
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003820 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00003821 if (!text) {
3822 delete_Token(t);
3823 } else {
3824 *tail = t;
3825 tail = &t->next;
3826 t->type = type;
3827 nasm_free(t->text);
3828 t->text = text;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003829 t->a.mac = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003830 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003831 changed = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003832 continue;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003833 } else if (tline->type == TOK_INDIRECT) {
3834 t = tline;
3835 tline = tline->next;
3836 tt = tokenize(t->text);
3837 tt = expand_mmac_params(tt);
3838 tt = expand_smacro(tt);
3839 *tail = tt;
3840 while (tt) {
3841 tt->a.mac = NULL; /* Necessary? */
3842 tail = &tt->next;
3843 tt = tt->next;
3844 }
3845 delete_Token(t);
3846 changed = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003847 } else {
3848 t = *tail = tline;
3849 tline = tline->next;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003850 t->a.mac = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003851 tail = &t->next;
3852 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00003853 }
H. Peter Anvineba20a72002-04-30 20:53:55 +00003854 *tail = NULL;
H. Peter Anvin67c63722008-10-26 23:49:00 -07003855
H. Peter Anvind784a082009-04-20 14:01:18 -07003856 if (changed)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003857 paste_tokens(&thead, false);
H. Peter Anvin6125b622009-04-08 14:02:25 -07003858
H. Peter Anvin76690a12002-04-30 20:52:49 +00003859 return thead;
3860}
3861
3862/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003863 * Expand all single-line macro calls made in the given line.
3864 * Return the expanded version of the line. The original is deemed
3865 * to be destroyed in the process. (In reality we'll just move
3866 * Tokens from input to output a lot of the time, rather than
3867 * actually bothering to destroy and replicate.)
3868 */
H. Peter Anvincb1cf592007-11-19 12:26:50 -08003869
H. Peter Anvine2c80182005-01-15 22:15:51 +00003870static Token *expand_smacro(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00003871{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003872 Token *t, *tt, *mstart, **tail, *thead;
H. Peter Anvineba20a72002-04-30 20:53:55 +00003873 SMacro *head = NULL, *m;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003874 Token **params;
3875 int *paramsize;
H. Peter Anvin25a99342007-09-22 17:45:45 -07003876 unsigned int nparam, sparam;
H. Peter Anvind784a082009-04-20 14:01:18 -07003877 int brackets;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003878 Token *org_tline = tline;
3879 Context *ctx;
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08003880 const char *mname;
H. Peter Anvin2a15e692007-11-19 13:14:59 -08003881 int deadman = DEADMAN_LIMIT;
H. Peter Anvin8287daf2009-07-07 16:00:58 -07003882 bool expanded;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003883
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003884 /*
3885 * Trick: we should avoid changing the start token pointer since it can
3886 * be contained in "next" field of other token. Because of this
3887 * we allocate a copy of first token and work with it; at the end of
3888 * routine we copy it back
3889 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003890 if (org_tline) {
Cyrill Gorcunoved4a8052010-04-09 15:40:35 +04003891 tline = new_Token(org_tline->next, org_tline->type,
3892 org_tline->text, 0);
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003893 tline->a.mac = org_tline->a.mac;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003894 nasm_free(org_tline->text);
3895 org_tline->text = NULL;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003896 }
3897
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003898 expanded = true; /* Always expand %+ at least once */
H. Peter Anvin8287daf2009-07-07 16:00:58 -07003899
H. Peter Anvincb1cf592007-11-19 12:26:50 -08003900again:
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003901 thead = NULL;
Cyrill Gorcunoved4a8052010-04-09 15:40:35 +04003902 tail = &thead;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003903
H. Peter Anvine2c80182005-01-15 22:15:51 +00003904 while (tline) { /* main token loop */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003905 if (!--deadman) {
3906 error(ERR_NONFATAL, "interminable macro recursion");
Cyrill Gorcunovbd38c8f2009-11-21 11:11:23 +03003907 goto err;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003908 }
H. Peter Anvincb1cf592007-11-19 12:26:50 -08003909
H. Peter Anvine2c80182005-01-15 22:15:51 +00003910 if ((mname = tline->text)) {
3911 /* if this token is a local macro, look in local context */
Cyrill Gorcunovc56d9ad2010-02-11 15:12:19 +03003912 if (tline->type == TOK_ID) {
3913 head = (SMacro *)hash_findix(&smacros, mname);
3914 } else if (tline->type == TOK_PREPROC_ID) {
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08003915 ctx = get_ctx(mname, &mname, true);
Cyrill Gorcunovc56d9ad2010-02-11 15:12:19 +03003916 head = ctx ? (SMacro *)hash_findix(&ctx->localmac, mname) : NULL;
3917 } else
3918 head = NULL;
H. Peter Anvin072771e2008-05-22 13:17:51 -07003919
H. Peter Anvine2c80182005-01-15 22:15:51 +00003920 /*
3921 * We've hit an identifier. As in is_mmacro below, we first
3922 * check whether the identifier is a single-line macro at
3923 * all, then think about checking for parameters if
3924 * necessary.
3925 */
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04003926 list_for_each(m, head)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003927 if (!mstrcmp(m->name, mname, m->casesense))
3928 break;
3929 if (m) {
3930 mstart = tline;
3931 params = NULL;
3932 paramsize = NULL;
3933 if (m->nparam == 0) {
3934 /*
3935 * Simple case: the macro is parameterless. Discard the
3936 * one token that the macro call took, and push the
3937 * expansion back on the to-do stack.
3938 */
3939 if (!m->expansion) {
3940 if (!strcmp("__FILE__", m->name)) {
3941 int32_t num = 0;
3942 char *file = NULL;
3943 src_get(&num, &file);
3944 tline->text = nasm_quote(file, strlen(file));
3945 tline->type = TOK_STRING;
3946 nasm_free(file);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003947 continue;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003948 }
3949 if (!strcmp("__LINE__", m->name)) {
3950 nasm_free(tline->text);
3951 make_tok_num(tline, src_get_linnum());
3952 continue;
3953 }
3954 if (!strcmp("__BITS__", m->name)) {
3955 nasm_free(tline->text);
3956 make_tok_num(tline, globalbits);
3957 continue;
3958 }
3959 tline = delete_Token(tline);
3960 continue;
3961 }
3962 } else {
3963 /*
3964 * Complicated case: at least one macro with this name
H. Peter Anvine2c80182005-01-15 22:15:51 +00003965 * exists and takes parameters. We must find the
3966 * parameters in the call, count them, find the SMacro
3967 * that corresponds to that form of the macro call, and
3968 * substitute for the parameters when we expand. What a
3969 * pain.
3970 */
3971 /*tline = tline->next;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003972 skip_white_(tline); */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003973 do {
3974 t = tline->next;
3975 while (tok_type_(t, TOK_SMAC_END)) {
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003976 t->a.mac->in_progress = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003977 t->text = NULL;
3978 t = tline->next = delete_Token(t);
3979 }
3980 tline = t;
3981 } while (tok_type_(tline, TOK_WHITESPACE));
3982 if (!tok_is_(tline, "(")) {
3983 /*
3984 * This macro wasn't called with parameters: ignore
3985 * the call. (Behaviour borrowed from gnu cpp.)
3986 */
3987 tline = mstart;
3988 m = NULL;
3989 } else {
3990 int paren = 0;
3991 int white = 0;
3992 brackets = 0;
3993 nparam = 0;
3994 sparam = PARAM_DELTA;
3995 params = nasm_malloc(sparam * sizeof(Token *));
3996 params[0] = tline->next;
3997 paramsize = nasm_malloc(sparam * sizeof(int));
3998 paramsize[0] = 0;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07003999 while (true) { /* parameter loop */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004000 /*
4001 * For some unusual expansions
4002 * which concatenates function call
4003 */
4004 t = tline->next;
4005 while (tok_type_(t, TOK_SMAC_END)) {
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004006 t->a.mac->in_progress = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004007 t->text = NULL;
4008 t = tline->next = delete_Token(t);
4009 }
4010 tline = t;
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00004011
H. Peter Anvine2c80182005-01-15 22:15:51 +00004012 if (!tline) {
4013 error(ERR_NONFATAL,
4014 "macro call expects terminating `)'");
4015 break;
4016 }
4017 if (tline->type == TOK_WHITESPACE
4018 && brackets <= 0) {
4019 if (paramsize[nparam])
4020 white++;
4021 else
4022 params[nparam] = tline->next;
4023 continue; /* parameter loop */
4024 }
4025 if (tline->type == TOK_OTHER
4026 && tline->text[1] == 0) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004027 char ch = tline->text[0];
H. Peter Anvine2c80182005-01-15 22:15:51 +00004028 if (ch == ',' && !paren && brackets <= 0) {
4029 if (++nparam >= sparam) {
4030 sparam += PARAM_DELTA;
4031 params = nasm_realloc(params,
Cyrill Gorcunoved4a8052010-04-09 15:40:35 +04004032 sparam * sizeof(Token *));
4033 paramsize = nasm_realloc(paramsize,
4034 sparam * sizeof(int));
H. Peter Anvine2c80182005-01-15 22:15:51 +00004035 }
4036 params[nparam] = tline->next;
4037 paramsize[nparam] = 0;
4038 white = 0;
4039 continue; /* parameter loop */
4040 }
4041 if (ch == '{' &&
4042 (brackets > 0 || (brackets == 0 &&
4043 !paramsize[nparam])))
4044 {
4045 if (!(brackets++)) {
4046 params[nparam] = tline->next;
4047 continue; /* parameter loop */
4048 }
4049 }
4050 if (ch == '}' && brackets > 0)
4051 if (--brackets == 0) {
4052 brackets = -1;
4053 continue; /* parameter loop */
4054 }
4055 if (ch == '(' && !brackets)
4056 paren++;
4057 if (ch == ')' && brackets <= 0)
4058 if (--paren < 0)
4059 break;
4060 }
4061 if (brackets < 0) {
4062 brackets = 0;
4063 error(ERR_NONFATAL, "braces do not "
4064 "enclose all of macro parameter");
4065 }
4066 paramsize[nparam] += white + 1;
4067 white = 0;
4068 } /* parameter loop */
4069 nparam++;
4070 while (m && (m->nparam != nparam ||
4071 mstrcmp(m->name, mname,
4072 m->casesense)))
4073 m = m->next;
4074 if (!m)
H. Peter Anvin917a3492008-09-24 09:14:49 -07004075 error(ERR_WARNING|ERR_PASS1|ERR_WARN_MNP,
H. Peter Anvine2c80182005-01-15 22:15:51 +00004076 "macro `%s' exists, "
4077 "but not taking %d parameters",
4078 mstart->text, nparam);
4079 }
4080 }
4081 if (m && m->in_progress)
4082 m = NULL;
4083 if (!m) { /* in progess or didn't find '(' or wrong nparam */
H. Peter Anvin70653092007-10-19 14:42:29 -07004084 /*
H. Peter Anvine2c80182005-01-15 22:15:51 +00004085 * Design question: should we handle !tline, which
4086 * indicates missing ')' here, or expand those
4087 * macros anyway, which requires the (t) test a few
H. Peter Anvin70653092007-10-19 14:42:29 -07004088 * lines down?
H. Peter Anvine2c80182005-01-15 22:15:51 +00004089 */
4090 nasm_free(params);
4091 nasm_free(paramsize);
4092 tline = mstart;
4093 } else {
4094 /*
4095 * Expand the macro: we are placed on the last token of the
4096 * call, so that we can easily split the call from the
4097 * following tokens. We also start by pushing an SMAC_END
4098 * token for the cycle removal.
4099 */
4100 t = tline;
4101 if (t) {
4102 tline = t->next;
4103 t->next = NULL;
4104 }
4105 tt = new_Token(tline, TOK_SMAC_END, NULL, 0);
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004106 tt->a.mac = m;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07004107 m->in_progress = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004108 tline = tt;
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04004109 list_for_each(t, m->expansion) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00004110 if (t->type >= TOK_SMAC_PARAM) {
4111 Token *pcopy = tline, **ptail = &pcopy;
4112 Token *ttt, *pt;
4113 int i;
H. Peter Anvin734b1882002-04-30 21:01:08 +00004114
H. Peter Anvine2c80182005-01-15 22:15:51 +00004115 ttt = params[t->type - TOK_SMAC_PARAM];
Cyrill Gorcunoved4a8052010-04-09 15:40:35 +04004116 i = paramsize[t->type - TOK_SMAC_PARAM];
4117 while (--i >= 0) {
4118 pt = *ptail = new_Token(tline, ttt->type,
4119 ttt->text, 0);
H. Peter Anvine2c80182005-01-15 22:15:51 +00004120 ptail = &pt->next;
4121 ttt = ttt->next;
4122 }
4123 tline = pcopy;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004124 } else if (t->type == TOK_PREPROC_Q) {
4125 tt = new_Token(tline, TOK_ID, mname, 0);
4126 tline = tt;
4127 } else if (t->type == TOK_PREPROC_QQ) {
4128 tt = new_Token(tline, TOK_ID, m->name, 0);
4129 tline = tt;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004130 } else {
4131 tt = new_Token(tline, t->type, t->text, 0);
4132 tline = tt;
4133 }
4134 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00004135
H. Peter Anvine2c80182005-01-15 22:15:51 +00004136 /*
4137 * Having done that, get rid of the macro call, and clean
4138 * up the parameters.
4139 */
4140 nasm_free(params);
4141 nasm_free(paramsize);
4142 free_tlist(mstart);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004143 expanded = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004144 continue; /* main token loop */
4145 }
4146 }
4147 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004148
H. Peter Anvine2c80182005-01-15 22:15:51 +00004149 if (tline->type == TOK_SMAC_END) {
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004150 tline->a.mac->in_progress = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004151 tline = delete_Token(tline);
4152 } else {
4153 t = *tail = tline;
4154 tline = tline->next;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004155 t->a.mac = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004156 t->next = NULL;
4157 tail = &t->next;
4158 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004159 }
4160
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004161 /*
4162 * Now scan the entire line and look for successive TOK_IDs that resulted
Keith Kaniosb7a89542007-04-12 02:40:54 +00004163 * after expansion (they can't be produced by tokenize()). The successive
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004164 * TOK_IDs should be concatenated.
4165 * Also we look for %+ tokens and concatenate the tokens before and after
4166 * them (without white spaces in between).
4167 */
H. Peter Anvin8287daf2009-07-07 16:00:58 -07004168 if (expanded && paste_tokens(&thead, true)) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004169 /*
4170 * If we concatenated something, *and* we had previously expanded
4171 * an actual macro, scan the lines again for macros...
4172 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004173 tline = thead;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004174 expanded = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004175 goto again;
H. Peter Anvin734b1882002-04-30 21:01:08 +00004176 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004177
Cyrill Gorcunovbd38c8f2009-11-21 11:11:23 +03004178err:
H. Peter Anvine2c80182005-01-15 22:15:51 +00004179 if (org_tline) {
4180 if (thead) {
4181 *org_tline = *thead;
4182 /* since we just gave text to org_line, don't free it */
4183 thead->text = NULL;
4184 delete_Token(thead);
4185 } else {
4186 /* the expression expanded to empty line;
4187 we can't return NULL for some reasons
4188 we just set the line to a single WHITESPACE token. */
4189 memset(org_tline, 0, sizeof(*org_tline));
4190 org_tline->text = NULL;
4191 org_tline->type = TOK_WHITESPACE;
4192 }
4193 thead = org_tline;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004194 }
4195
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004196 return thead;
4197}
4198
4199/*
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004200 * Similar to expand_smacro but used exclusively with macro identifiers
4201 * right before they are fetched in. The reason is that there can be
4202 * identifiers consisting of several subparts. We consider that if there
4203 * are more than one element forming the name, user wants a expansion,
4204 * otherwise it will be left as-is. Example:
4205 *
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004206 * %define %$abc cde
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004207 *
4208 * the identifier %$abc will be left as-is so that the handler for %define
4209 * will suck it and define the corresponding value. Other case:
4210 *
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004211 * %define _%$abc cde
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004212 *
4213 * In this case user wants name to be expanded *before* %define starts
4214 * working, so we'll expand %$abc into something (if it has a value;
4215 * otherwise it will be left as-is) then concatenate all successive
4216 * PP_IDs into one.
4217 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004218static Token *expand_id(Token * tline)
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004219{
4220 Token *cur, *oldnext = NULL;
4221
H. Peter Anvin734b1882002-04-30 21:01:08 +00004222 if (!tline || !tline->next)
H. Peter Anvine2c80182005-01-15 22:15:51 +00004223 return tline;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004224
4225 cur = tline;
4226 while (cur->next &&
H. Peter Anvine2c80182005-01-15 22:15:51 +00004227 (cur->next->type == TOK_ID ||
4228 cur->next->type == TOK_PREPROC_ID
4229 || cur->next->type == TOK_NUMBER))
4230 cur = cur->next;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004231
4232 /* If identifier consists of just one token, don't expand */
4233 if (cur == tline)
H. Peter Anvine2c80182005-01-15 22:15:51 +00004234 return tline;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004235
H. Peter Anvine2c80182005-01-15 22:15:51 +00004236 if (cur) {
4237 oldnext = cur->next; /* Detach the tail past identifier */
4238 cur->next = NULL; /* so that expand_smacro stops here */
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004239 }
4240
H. Peter Anvin734b1882002-04-30 21:01:08 +00004241 tline = expand_smacro(tline);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004242
H. Peter Anvine2c80182005-01-15 22:15:51 +00004243 if (cur) {
4244 /* expand_smacro possibly changhed tline; re-scan for EOL */
4245 cur = tline;
4246 while (cur && cur->next)
4247 cur = cur->next;
4248 if (cur)
4249 cur->next = oldnext;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004250 }
4251
4252 return tline;
4253}
4254
4255/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004256 * Determine whether the given line constitutes a multi-line macro
4257 * call, and return the MMacro structure called if so. Doesn't have
4258 * to check for an initial label - that's taken care of in
4259 * expand_mmacro - but must check numbers of parameters. Guaranteed
4260 * to be called with tline->type == TOK_ID, so the putative macro
4261 * name is easy to find.
4262 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004263static MMacro *is_mmacro(Token * tline, Token *** params_array)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004264{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004265 MMacro *head, *m;
4266 Token **params;
4267 int nparam;
4268
H. Peter Anvin166c2472008-05-28 12:28:58 -07004269 head = (MMacro *) hash_findix(&mmacros, tline->text);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004270
4271 /*
4272 * Efficiency: first we see if any macro exists with the given
4273 * name. If not, we can return NULL immediately. _Then_ we
4274 * count the parameters, and then we look further along the
4275 * list if necessary to find the proper MMacro.
4276 */
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04004277 list_for_each(m, head)
H. Peter Anvine2c80182005-01-15 22:15:51 +00004278 if (!mstrcmp(m->name, tline->text, m->casesense))
4279 break;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004280 if (!m)
H. Peter Anvine2c80182005-01-15 22:15:51 +00004281 return NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004282
4283 /*
4284 * OK, we have a potential macro. Count and demarcate the
4285 * parameters.
4286 */
H. Peter Anvin734b1882002-04-30 21:01:08 +00004287 count_mmac_params(tline->next, &nparam, &params);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004288
4289 /*
4290 * So we know how many parameters we've got. Find the MMacro
4291 * structure that handles this number.
4292 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004293 while (m) {
4294 if (m->nparam_min <= nparam
4295 && (m->plus || nparam <= m->nparam_max)) {
4296 /*
4297 * This one is right. Just check if cycle removal
4298 * prohibits us using it before we actually celebrate...
4299 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004300 if (m->in_progress > m->max_depth) {
4301 if (m->max_depth > 0) {
4302 error(ERR_WARNING,
4303 "reached maximum recursion depth of %i",
4304 m->max_depth);
4305 }
4306 nasm_free(params);
4307 return NULL;
4308 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00004309 /*
4310 * It's right, and we can use it. Add its default
4311 * parameters to the end of our list if necessary.
4312 */
4313 if (m->defaults && nparam < m->nparam_min + m->ndefs) {
4314 params =
4315 nasm_realloc(params,
4316 ((m->nparam_min + m->ndefs +
4317 1) * sizeof(*params)));
4318 while (nparam < m->nparam_min + m->ndefs) {
4319 params[nparam] = m->defaults[nparam - m->nparam_min];
4320 nparam++;
4321 }
4322 }
4323 /*
4324 * If we've gone over the maximum parameter count (and
4325 * we're in Plus mode), ignore parameters beyond
4326 * nparam_max.
4327 */
4328 if (m->plus && nparam > m->nparam_max)
4329 nparam = m->nparam_max;
4330 /*
4331 * Then terminate the parameter list, and leave.
4332 */
4333 if (!params) { /* need this special case */
4334 params = nasm_malloc(sizeof(*params));
4335 nparam = 0;
4336 }
4337 params[nparam] = NULL;
4338 *params_array = params;
4339 return m;
4340 }
4341 /*
4342 * This one wasn't right: look for the next one with the
4343 * same name.
4344 */
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04004345 list_for_each(m, m->next)
H. Peter Anvine2c80182005-01-15 22:15:51 +00004346 if (!mstrcmp(m->name, tline->text, m->casesense))
4347 break;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004348 }
4349
4350 /*
4351 * After all that, we didn't find one with the right number of
4352 * parameters. Issue a warning, and fail to expand the macro.
4353 */
H. Peter Anvin917a3492008-09-24 09:14:49 -07004354 error(ERR_WARNING|ERR_PASS1|ERR_WARN_MNP,
H. Peter Anvine2c80182005-01-15 22:15:51 +00004355 "macro `%s' exists, but not taking %d parameters",
4356 tline->text, nparam);
H. Peter Anvin734b1882002-04-30 21:01:08 +00004357 nasm_free(params);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004358 return NULL;
4359}
4360
Keith Kanios891775e2009-07-11 06:08:54 -05004361
4362/*
4363 * Save MMacro invocation specific fields in
4364 * preparation for a recursive macro expansion
4365 */
4366static void push_mmacro(MMacro *m)
4367{
4368 MMacroInvocation *i;
4369
H. Peter Anvin89cee572009-07-15 09:16:54 -04004370 i = nasm_malloc(sizeof(MMacroInvocation));
4371 i->prev = m->prev;
4372 i->params = m->params;
4373 i->iline = m->iline;
4374 i->nparam = m->nparam;
4375 i->rotate = m->rotate;
4376 i->paramlen = m->paramlen;
4377 i->unique = m->unique;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004378 i->condcnt = m->condcnt;
H. Peter Anvin89cee572009-07-15 09:16:54 -04004379 m->prev = i;
Keith Kanios891775e2009-07-11 06:08:54 -05004380}
4381
4382
4383/*
4384 * Restore MMacro invocation specific fields that were
4385 * saved during a previous recursive macro expansion
4386 */
4387static void pop_mmacro(MMacro *m)
4388{
4389 MMacroInvocation *i;
4390
H. Peter Anvin89cee572009-07-15 09:16:54 -04004391 if (m->prev) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004392 i = m->prev;
4393 m->prev = i->prev;
4394 m->params = i->params;
4395 m->iline = i->iline;
4396 m->nparam = i->nparam;
4397 m->rotate = i->rotate;
4398 m->paramlen = i->paramlen;
4399 m->unique = i->unique;
4400 m->condcnt = i->condcnt;
4401 nasm_free(i);
H. Peter Anvin89cee572009-07-15 09:16:54 -04004402 }
Keith Kanios891775e2009-07-11 06:08:54 -05004403}
4404
4405
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004406/*
4407 * Expand the multi-line macro call made by the given line, if
4408 * there is one to be expanded. If there is, push the expansion on
H. Peter Anvineba20a72002-04-30 20:53:55 +00004409 * istk->expansion and return 1. Otherwise return 0.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004410 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004411static int expand_mmacro(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004412{
4413 Token *startline = tline;
4414 Token *label = NULL;
4415 int dont_prepend = 0;
H. Peter Anvince2233b2008-05-25 21:57:00 -07004416 Token **params, *t, *mtok, *tt;
H. Peter Anvineba20a72002-04-30 20:53:55 +00004417 MMacro *m;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004418 Line *l, *ll;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004419 int i, nparam, *paramlen;
H. Peter Anvinc751e862008-06-09 10:18:45 -07004420 const char *mname;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004421
4422 t = tline;
H. Peter Anvineba20a72002-04-30 20:53:55 +00004423 skip_white_(t);
H. Peter Anvince2233b2008-05-25 21:57:00 -07004424 /* if (!tok_type_(t, TOK_ID)) Lino 02/25/02 */
H. Peter Anvindce1e2f2002-04-30 21:06:37 +00004425 if (!tok_type_(t, TOK_ID) && !tok_type_(t, TOK_PREPROC_ID))
H. Peter Anvine2c80182005-01-15 22:15:51 +00004426 return 0;
H. Peter Anvince2233b2008-05-25 21:57:00 -07004427 mtok = t;
H. Peter Anvin734b1882002-04-30 21:01:08 +00004428 m = is_mmacro(t, &params);
H. Peter Anvinc751e862008-06-09 10:18:45 -07004429 if (m) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004430 mname = t->text;
H. Peter Anvinc751e862008-06-09 10:18:45 -07004431 } else {
H. Peter Anvine2c80182005-01-15 22:15:51 +00004432 Token *last;
4433 /*
4434 * We have an id which isn't a macro call. We'll assume
4435 * it might be a label; we'll also check to see if a
4436 * colon follows it. Then, if there's another id after
4437 * that lot, we'll check it again for macro-hood.
4438 */
4439 label = last = t;
4440 t = t->next;
4441 if (tok_type_(t, TOK_WHITESPACE))
4442 last = t, t = t->next;
4443 if (tok_is_(t, ":")) {
4444 dont_prepend = 1;
4445 last = t, t = t->next;
4446 if (tok_type_(t, TOK_WHITESPACE))
4447 last = t, t = t->next;
4448 }
H. Peter Anvin89cee572009-07-15 09:16:54 -04004449 if (!tok_type_(t, TOK_ID) || !(m = is_mmacro(t, &params)))
H. Peter Anvine2c80182005-01-15 22:15:51 +00004450 return 0;
4451 last->next = NULL;
Keith Kanios891775e2009-07-11 06:08:54 -05004452 mname = t->text;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004453 tline = t;
H. Peter Anvineba20a72002-04-30 20:53:55 +00004454 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004455
4456 /*
4457 * Fix up the parameters: this involves stripping leading and
4458 * trailing whitespace, then stripping braces if they are
4459 * present.
4460 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004461 for (nparam = 0; params[nparam]; nparam++) ;
H. Peter Anvin734b1882002-04-30 21:01:08 +00004462 paramlen = nparam ? nasm_malloc(nparam * sizeof(*paramlen)) : NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004463
H. Peter Anvine2c80182005-01-15 22:15:51 +00004464 for (i = 0; params[i]; i++) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07004465 int brace = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004466 int comma = (!m->plus || i < nparam - 1);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004467
H. Peter Anvine2c80182005-01-15 22:15:51 +00004468 t = params[i];
4469 skip_white_(t);
4470 if (tok_is_(t, "{"))
H. Peter Anvin6867acc2007-10-10 14:58:45 -07004471 t = t->next, brace = true, comma = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004472 params[i] = t;
4473 paramlen[i] = 0;
4474 while (t) {
4475 if (comma && t->type == TOK_OTHER && !strcmp(t->text, ","))
4476 break; /* ... because we have hit a comma */
4477 if (comma && t->type == TOK_WHITESPACE
4478 && tok_is_(t->next, ","))
4479 break; /* ... or a space then a comma */
4480 if (brace && t->type == TOK_OTHER && !strcmp(t->text, "}"))
4481 break; /* ... or a brace */
4482 t = t->next;
4483 paramlen[i]++;
4484 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004485 }
4486
4487 /*
4488 * OK, we have a MMacro structure together with a set of
4489 * parameters. We must now go through the expansion and push
H. Peter Anvin76690a12002-04-30 20:52:49 +00004490 * copies of each Line on to istk->expansion. Substitution of
4491 * parameter tokens and macro-local tokens doesn't get done
4492 * until the single-line macro substitution process; this is
4493 * because delaying them allows us to change the semantics
4494 * later through %rotate.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004495 *
H. Peter Anvin76690a12002-04-30 20:52:49 +00004496 * First, push an end marker on to istk->expansion, mark this
4497 * macro as in progress, and set up its invocation-specific
4498 * variables.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004499 */
4500 ll = nasm_malloc(sizeof(Line));
4501 ll->next = istk->expansion;
4502 ll->finishes = m;
4503 ll->first = NULL;
4504 istk->expansion = ll;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004505
H. Peter Anvin89cee572009-07-15 09:16:54 -04004506 /*
4507 * Save the previous MMacro expansion in the case of
4508 * macro recursion
4509 */
4510 if (m->max_depth && m->in_progress)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004511 push_mmacro(m);
H. Peter Anvin76690a12002-04-30 20:52:49 +00004512
Keith Kanios891775e2009-07-11 06:08:54 -05004513 m->in_progress ++;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004514 m->params = params;
4515 m->iline = tline;
4516 m->nparam = nparam;
4517 m->rotate = 0;
4518 m->paramlen = paramlen;
4519 m->unique = unique++;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004520 m->lineno = 0;
Keith Kanios3c0d91f2009-10-25 13:28:03 -05004521 m->condcnt = 0;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004522
4523 m->next_active = istk->mstk;
4524 istk->mstk = m;
4525
Cyrill Gorcunov367d59e2010-04-09 15:43:03 +04004526 list_for_each(l, m->expansion) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00004527 Token **tail;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004528
H. Peter Anvine2c80182005-01-15 22:15:51 +00004529 ll = nasm_malloc(sizeof(Line));
4530 ll->finishes = NULL;
4531 ll->next = istk->expansion;
4532 istk->expansion = ll;
4533 tail = &ll->first;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004534
Cyrill Gorcunov367d59e2010-04-09 15:43:03 +04004535 list_for_each(t, l->first) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00004536 Token *x = t;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004537 switch (t->type) {
4538 case TOK_PREPROC_Q:
4539 tt = *tail = new_Token(NULL, TOK_ID, mname, 0);
4540 break;
4541 case TOK_PREPROC_QQ:
4542 tt = *tail = new_Token(NULL, TOK_ID, m->name, 0);
4543 break;
4544 case TOK_PREPROC_ID:
4545 if (t->text[1] == '0' && t->text[2] == '0') {
4546 dont_prepend = -1;
4547 x = label;
4548 if (!x)
4549 continue;
4550 }
4551 /* fall through */
4552 default:
4553 tt = *tail = new_Token(NULL, x->type, x->text, 0);
4554 break;
4555 }
4556 tail = &tt->next;
4557 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00004558 *tail = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004559 }
4560
4561 /*
H. Peter Anvineba20a72002-04-30 20:53:55 +00004562 * If we had a label, push it on as the first line of
4563 * the macro expansion.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004564 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004565 if (label) {
4566 if (dont_prepend < 0)
4567 free_tlist(startline);
4568 else {
4569 ll = nasm_malloc(sizeof(Line));
4570 ll->finishes = NULL;
4571 ll->next = istk->expansion;
4572 istk->expansion = ll;
4573 ll->first = startline;
4574 if (!dont_prepend) {
4575 while (label->next)
4576 label = label->next;
4577 label->next = tt = new_Token(NULL, TOK_OTHER, ":", 0);
4578 }
4579 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004580 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004581
H. Peter Anvin734b1882002-04-30 21:01:08 +00004582 list->uplevel(m->nolist ? LIST_MACRO_NOLIST : LIST_MACRO);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004583
H. Peter Anvineba20a72002-04-30 20:53:55 +00004584 return 1;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004585}
4586
Victor van den Elzen3b404c02008-09-18 13:51:36 +02004587/* The function that actually does the error reporting */
4588static void verror(int severity, const char *fmt, va_list arg)
4589{
4590 char buff[1024];
4591
4592 vsnprintf(buff, sizeof(buff), fmt, arg);
4593
4594 if (istk && istk->mstk && istk->mstk->name)
H. Peter Anvindbb640b2009-07-18 18:57:16 -07004595 nasm_error(severity, "(%s:%d) %s", istk->mstk->name,
Victor van den Elzen3b404c02008-09-18 13:51:36 +02004596 istk->mstk->lineno, buff);
4597 else
H. Peter Anvindbb640b2009-07-18 18:57:16 -07004598 nasm_error(severity, "%s", buff);
Victor van den Elzen3b404c02008-09-18 13:51:36 +02004599}
4600
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004601/*
4602 * Since preprocessor always operate only on the line that didn't
H. Peter Anvin917a3492008-09-24 09:14:49 -07004603 * arrived yet, we should always use ERR_OFFBY1.
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004604 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004605static void error(int severity, const char *fmt, ...)
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004606{
4607 va_list arg;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004608
4609 /* If we're in a dead branch of IF or something like it, ignore the error */
H. Peter Anvin77ba0c62002-05-14 03:18:53 +00004610 if (istk && istk->conds && !emitting(istk->conds->state))
H. Peter Anvine2c80182005-01-15 22:15:51 +00004611 return;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004612
H. Peter Anvin734b1882002-04-30 21:01:08 +00004613 va_start(arg, fmt);
Victor van den Elzen3b404c02008-09-18 13:51:36 +02004614 verror(severity, fmt, arg);
H. Peter Anvin734b1882002-04-30 21:01:08 +00004615 va_end(arg);
Victor van den Elzen3b404c02008-09-18 13:51:36 +02004616}
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004617
Victor van den Elzen3b404c02008-09-18 13:51:36 +02004618/*
4619 * Because %else etc are evaluated in the state context
4620 * of the previous branch, errors might get lost with error():
4621 * %if 0 ... %else trailing garbage ... %endif
4622 * So %else etc should report errors with this function.
4623 */
4624static void error_precond(int severity, const char *fmt, ...)
4625{
4626 va_list arg;
4627
4628 /* Only ignore the error if it's really in a dead branch */
4629 if (istk && istk->conds && istk->conds->state == COND_NEVER)
4630 return;
4631
4632 va_start(arg, fmt);
4633 verror(severity, fmt, arg);
4634 va_end(arg);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004635}
4636
H. Peter Anvin734b1882002-04-30 21:01:08 +00004637static void
H. Peter Anvindbb640b2009-07-18 18:57:16 -07004638pp_reset(char *file, int apass, ListGen * listgen, StrList **deplist)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004639{
H. Peter Anvin7383b402008-09-24 10:20:40 -07004640 Token *t;
4641
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004642 cstk = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004643 istk = nasm_malloc(sizeof(Include));
4644 istk->next = NULL;
4645 istk->conds = NULL;
4646 istk->expansion = NULL;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004647 istk->mstk = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004648 istk->fp = fopen(file, "r");
H. Peter Anvineba20a72002-04-30 20:53:55 +00004649 istk->fname = NULL;
4650 src_set_fname(nasm_strdup(file));
4651 src_set_linnum(0);
4652 istk->lineinc = 1;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004653 if (!istk->fp)
H. Peter Anvin917a3492008-09-24 09:14:49 -07004654 error(ERR_FATAL|ERR_NOFILE, "unable to open input file `%s'",
H. Peter Anvine2c80182005-01-15 22:15:51 +00004655 file);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004656 defining = NULL;
Charles Crayned4200be2008-07-12 16:42:33 -07004657 nested_mac_count = 0;
4658 nested_rep_count = 0;
H. Peter Anvin97a23472007-09-16 17:57:25 -07004659 init_macros();
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004660 unique = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004661 if (tasm_compatible_mode) {
H. Peter Anvina4835d42008-05-20 14:21:29 -07004662 stdmacpos = nasm_stdmac;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004663 } else {
H. Peter Anvina4835d42008-05-20 14:21:29 -07004664 stdmacpos = nasm_stdmac_after_tasm;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004665 }
H. Peter Anvind2456592008-06-19 15:04:18 -07004666 any_extrastdmac = extrastdmac && *extrastdmac;
4667 do_predef = true;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004668 list = listgen;
H. Peter Anvin61f130f2008-09-25 15:45:06 -07004669
4670 /*
4671 * 0 for dependencies, 1 for preparatory passes, 2 for final pass.
4672 * The caller, however, will also pass in 3 for preprocess-only so
4673 * we can set __PASS__ accordingly.
4674 */
4675 pass = apass > 2 ? 2 : apass;
4676
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07004677 dephead = deptail = deplist;
4678 if (deplist) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004679 StrList *sl = nasm_malloc(strlen(file)+1+sizeof sl->next);
4680 sl->next = NULL;
4681 strcpy(sl->str, file);
4682 *deptail = sl;
4683 deptail = &sl->next;
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07004684 }
H. Peter Anvin7383b402008-09-24 10:20:40 -07004685
H. Peter Anvin61f130f2008-09-25 15:45:06 -07004686 /*
4687 * Define the __PASS__ macro. This is defined here unlike
4688 * all the other builtins, because it is special -- it varies between
4689 * passes.
4690 */
H. Peter Anvin7383b402008-09-24 10:20:40 -07004691 t = nasm_malloc(sizeof(*t));
4692 t->next = NULL;
H. Peter Anvin61f130f2008-09-25 15:45:06 -07004693 make_tok_num(t, apass);
H. Peter Anvin7383b402008-09-24 10:20:40 -07004694 t->a.mac = NULL;
4695 define_smacro(NULL, "__PASS__", true, 0, t);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004696}
4697
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004698static char *pp_getline(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004699{
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004700 char *line;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004701 Token *tline;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004702
H. Peter Anvine2c80182005-01-15 22:15:51 +00004703 while (1) {
4704 /*
Keith Kaniosb7a89542007-04-12 02:40:54 +00004705 * Fetch a tokenized line, either from the macro-expansion
H. Peter Anvine2c80182005-01-15 22:15:51 +00004706 * buffer or from the input file.
4707 */
4708 tline = NULL;
4709 while (istk->expansion && istk->expansion->finishes) {
4710 Line *l = istk->expansion;
4711 if (!l->finishes->name && l->finishes->in_progress > 1) {
4712 Line *ll;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004713
H. Peter Anvine2c80182005-01-15 22:15:51 +00004714 /*
4715 * This is a macro-end marker for a macro with no
4716 * name, which means it's not really a macro at all
4717 * but a %rep block, and the `in_progress' field is
4718 * more than 1, meaning that we still need to
4719 * repeat. (1 means the natural last repetition; 0
4720 * means termination by %exitrep.) We have
4721 * therefore expanded up to the %endrep, and must
4722 * push the whole block on to the expansion buffer
4723 * again. We don't bother to remove the macro-end
4724 * marker: we'd only have to generate another one
4725 * if we did.
4726 */
4727 l->finishes->in_progress--;
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04004728 list_for_each(l, l->finishes->expansion) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00004729 Token *t, *tt, **tail;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004730
H. Peter Anvine2c80182005-01-15 22:15:51 +00004731 ll = nasm_malloc(sizeof(Line));
4732 ll->next = istk->expansion;
4733 ll->finishes = NULL;
4734 ll->first = NULL;
4735 tail = &ll->first;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004736
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04004737 list_for_each(t, l->first) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00004738 if (t->text || t->type == TOK_WHITESPACE) {
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04004739 tt = *tail = new_Token(NULL, t->type, t->text, 0);
H. Peter Anvine2c80182005-01-15 22:15:51 +00004740 tail = &tt->next;
4741 }
4742 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00004743
H. Peter Anvine2c80182005-01-15 22:15:51 +00004744 istk->expansion = ll;
4745 }
4746 } else {
4747 /*
4748 * Check whether a `%rep' was started and not ended
4749 * within this macro expansion. This can happen and
4750 * should be detected. It's a fatal error because
4751 * I'm too confused to work out how to recover
4752 * sensibly from it.
4753 */
4754 if (defining) {
4755 if (defining->name)
4756 error(ERR_PANIC,
4757 "defining with name in expansion");
4758 else if (istk->mstk->name)
4759 error(ERR_FATAL,
4760 "`%%rep' without `%%endrep' within"
4761 " expansion of macro `%s'",
4762 istk->mstk->name);
4763 }
H. Peter Anvin87bc6192002-04-30 20:53:16 +00004764
H. Peter Anvine2c80182005-01-15 22:15:51 +00004765 /*
4766 * FIXME: investigate the relationship at this point between
4767 * istk->mstk and l->finishes
4768 */
4769 {
4770 MMacro *m = istk->mstk;
4771 istk->mstk = m->next_active;
4772 if (m->name) {
4773 /*
4774 * This was a real macro call, not a %rep, and
4775 * therefore the parameter information needs to
4776 * be freed.
4777 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004778 if (m->prev) {
4779 pop_mmacro(m);
4780 l->finishes->in_progress --;
4781 } else {
Keith Kanios891775e2009-07-11 06:08:54 -05004782 nasm_free(m->params);
4783 free_tlist(m->iline);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004784 nasm_free(m->paramlen);
4785 l->finishes->in_progress = 0;
4786 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00004787 } else
4788 free_mmacro(m);
4789 }
4790 istk->expansion = l->next;
4791 nasm_free(l);
4792 list->downlevel(LIST_MACRO);
4793 }
4794 }
4795 while (1) { /* until we get a line we can use */
H. Peter Anvineba20a72002-04-30 20:53:55 +00004796
H. Peter Anvine2c80182005-01-15 22:15:51 +00004797 if (istk->expansion) { /* from a macro expansion */
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004798 char *p;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004799 Line *l = istk->expansion;
4800 if (istk->mstk)
4801 istk->mstk->lineno++;
4802 tline = l->first;
4803 istk->expansion = l->next;
4804 nasm_free(l);
H. Peter Anvin6867acc2007-10-10 14:58:45 -07004805 p = detoken(tline, false);
H. Peter Anvine2c80182005-01-15 22:15:51 +00004806 list->line(LIST_MACRO, p);
4807 nasm_free(p);
4808 break;
4809 }
4810 line = read_line();
4811 if (line) { /* from the current input file */
4812 line = prepreproc(line);
Keith Kaniosb7a89542007-04-12 02:40:54 +00004813 tline = tokenize(line);
H. Peter Anvine2c80182005-01-15 22:15:51 +00004814 nasm_free(line);
4815 break;
4816 }
4817 /*
4818 * The current file has ended; work down the istk
4819 */
4820 {
4821 Include *i = istk;
4822 fclose(i->fp);
4823 if (i->conds)
4824 error(ERR_FATAL,
4825 "expected `%%endif' before end of file");
4826 /* only set line and file name if there's a next node */
4827 if (i->next) {
4828 src_set_linnum(i->lineno);
4829 nasm_free(src_set_fname(i->fname));
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004830 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00004831 istk = i->next;
4832 list->downlevel(LIST_INCLUDE);
4833 nasm_free(i);
4834 if (!istk)
4835 return NULL;
Victor van den Elzen4c9d6222008-10-01 13:08:50 +02004836 if (istk->expansion && istk->expansion->finishes)
4837 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004838 }
4839 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004840
H. Peter Anvine2c80182005-01-15 22:15:51 +00004841 /*
4842 * We must expand MMacro parameters and MMacro-local labels
4843 * _before_ we plunge into directive processing, to cope
4844 * with things like `%define something %1' such as STRUC
4845 * uses. Unless we're _defining_ a MMacro, in which case
4846 * those tokens should be left alone to go into the
4847 * definition; and unless we're in a non-emitting
4848 * condition, in which case we don't want to meddle with
4849 * anything.
4850 */
Charles Crayned4200be2008-07-12 16:42:33 -07004851 if (!defining && !(istk->conds && !emitting(istk->conds->state))
H. Peter Anvin992fe752008-10-19 15:45:05 -07004852 && !(istk->mstk && !istk->mstk->in_progress)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00004853 tline = expand_mmac_params(tline);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004854 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00004855
H. Peter Anvine2c80182005-01-15 22:15:51 +00004856 /*
4857 * Check the line to see if it's a preprocessor directive.
4858 */
4859 if (do_directive(tline) == DIRECTIVE_FOUND) {
4860 continue;
4861 } else if (defining) {
4862 /*
4863 * We're defining a multi-line macro. We emit nothing
4864 * at all, and just
Keith Kaniosb7a89542007-04-12 02:40:54 +00004865 * shove the tokenized line on to the macro definition.
H. Peter Anvine2c80182005-01-15 22:15:51 +00004866 */
4867 Line *l = nasm_malloc(sizeof(Line));
4868 l->next = defining->expansion;
4869 l->first = tline;
H. Peter Anvin538002d2008-06-28 18:30:27 -07004870 l->finishes = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004871 defining->expansion = l;
4872 continue;
4873 } else if (istk->conds && !emitting(istk->conds->state)) {
4874 /*
4875 * We're in a non-emitting branch of a condition block.
4876 * Emit nothing at all, not even a blank line: when we
4877 * emerge from the condition we'll give a line-number
4878 * directive so we keep our place correctly.
4879 */
4880 free_tlist(tline);
4881 continue;
4882 } else if (istk->mstk && !istk->mstk->in_progress) {
4883 /*
4884 * We're in a %rep block which has been terminated, so
4885 * we're walking through to the %endrep without
4886 * emitting anything. Emit nothing at all, not even a
4887 * blank line: when we emerge from the %rep block we'll
4888 * give a line-number directive so we keep our place
4889 * correctly.
4890 */
4891 free_tlist(tline);
4892 continue;
4893 } else {
4894 tline = expand_smacro(tline);
4895 if (!expand_mmacro(tline)) {
4896 /*
Keith Kaniosb7a89542007-04-12 02:40:54 +00004897 * De-tokenize the line again, and emit it.
H. Peter Anvine2c80182005-01-15 22:15:51 +00004898 */
H. Peter Anvin6867acc2007-10-10 14:58:45 -07004899 line = detoken(tline, true);
H. Peter Anvine2c80182005-01-15 22:15:51 +00004900 free_tlist(tline);
4901 break;
4902 } else {
4903 continue; /* expand_mmacro calls free_tlist */
4904 }
4905 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004906 }
4907
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004908 return line;
4909}
4910
H. Peter Anvine2c80182005-01-15 22:15:51 +00004911static void pp_cleanup(int pass)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004912{
H. Peter Anvine2c80182005-01-15 22:15:51 +00004913 if (defining) {
H. Peter Anvin89cee572009-07-15 09:16:54 -04004914 if (defining->name) {
Victor van den Elzen8f1120f2008-07-16 13:41:37 +02004915 error(ERR_NONFATAL,
4916 "end of file while still defining macro `%s'",
4917 defining->name);
4918 } else {
4919 error(ERR_NONFATAL, "end of file while still in %%rep");
4920 }
4921
H. Peter Anvine2c80182005-01-15 22:15:51 +00004922 free_mmacro(defining);
Cyrill Gorcunova327c652010-02-14 17:19:38 +03004923 defining = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004924 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004925 while (cstk)
H. Peter Anvine2c80182005-01-15 22:15:51 +00004926 ctx_pop();
H. Peter Anvin97a23472007-09-16 17:57:25 -07004927 free_macros();
H. Peter Anvine2c80182005-01-15 22:15:51 +00004928 while (istk) {
4929 Include *i = istk;
4930 istk = istk->next;
4931 fclose(i->fp);
4932 nasm_free(i->fname);
4933 nasm_free(i);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004934 }
4935 while (cstk)
H. Peter Anvine2c80182005-01-15 22:15:51 +00004936 ctx_pop();
H. Peter Anvin86877b22008-06-20 15:55:45 -07004937 nasm_free(src_set_fname(NULL));
H. Peter Anvine2c80182005-01-15 22:15:51 +00004938 if (pass == 0) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004939 IncPath *i;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004940 free_llist(predef);
4941 delete_Blocks();
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004942 while ((i = ipath)) {
4943 ipath = i->next;
4944 if (i->path)
4945 nasm_free(i->path);
4946 nasm_free(i);
4947 }
H. Peter Anvin11dfa1a2008-07-02 18:11:04 -07004948 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004949}
4950
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004951void pp_include_path(char *path)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004952{
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004953 IncPath *i;
H. Peter Anvin37a321f2007-09-24 13:41:58 -07004954
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004955 i = nasm_malloc(sizeof(IncPath));
H. Peter Anvin37a321f2007-09-24 13:41:58 -07004956 i->path = path ? nasm_strdup(path) : NULL;
Frank Kotlerd0ed6fd2003-08-27 11:33:56 +00004957 i->next = NULL;
4958
H. Peter Anvin89cee572009-07-15 09:16:54 -04004959 if (ipath) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00004960 IncPath *j = ipath;
H. Peter Anvin89cee572009-07-15 09:16:54 -04004961 while (j->next)
H. Peter Anvine2c80182005-01-15 22:15:51 +00004962 j = j->next;
4963 j->next = i;
4964 } else {
4965 ipath = i;
Frank Kotlerd0ed6fd2003-08-27 11:33:56 +00004966 }
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004967}
Frank Kotlerd0ed6fd2003-08-27 11:33:56 +00004968
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004969void pp_pre_include(char *fname)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004970{
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004971 Token *inc, *space, *name;
4972 Line *l;
4973
H. Peter Anvin734b1882002-04-30 21:01:08 +00004974 name = new_Token(NULL, TOK_INTERNAL_STRING, fname, 0);
4975 space = new_Token(name, TOK_WHITESPACE, NULL, 0);
4976 inc = new_Token(space, TOK_PREPROC_ID, "%include", 0);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004977
4978 l = nasm_malloc(sizeof(Line));
4979 l->next = predef;
4980 l->first = inc;
H. Peter Anvin538002d2008-06-28 18:30:27 -07004981 l->finishes = NULL;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004982 predef = l;
4983}
4984
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004985void pp_pre_define(char *definition)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004986{
4987 Token *def, *space;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004988 Line *l;
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004989 char *equals;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004990
4991 equals = strchr(definition, '=');
H. Peter Anvin734b1882002-04-30 21:01:08 +00004992 space = new_Token(NULL, TOK_WHITESPACE, NULL, 0);
4993 def = new_Token(space, TOK_PREPROC_ID, "%define", 0);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004994 if (equals)
H. Peter Anvine2c80182005-01-15 22:15:51 +00004995 *equals = ' ';
Keith Kaniosb7a89542007-04-12 02:40:54 +00004996 space->next = tokenize(definition);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004997 if (equals)
H. Peter Anvine2c80182005-01-15 22:15:51 +00004998 *equals = '=';
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004999
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005000 l = nasm_malloc(sizeof(Line));
5001 l->next = predef;
5002 l->first = def;
H. Peter Anvin538002d2008-06-28 18:30:27 -07005003 l->finishes = NULL;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005004 predef = l;
5005}
5006
Keith Kaniosa6dfa782007-04-13 16:47:53 +00005007void pp_pre_undefine(char *definition)
H. Peter Anvin620515a2002-04-30 20:57:38 +00005008{
5009 Token *def, *space;
5010 Line *l;
5011
H. Peter Anvin734b1882002-04-30 21:01:08 +00005012 space = new_Token(NULL, TOK_WHITESPACE, NULL, 0);
5013 def = new_Token(space, TOK_PREPROC_ID, "%undef", 0);
Keith Kaniosb7a89542007-04-12 02:40:54 +00005014 space->next = tokenize(definition);
H. Peter Anvin620515a2002-04-30 20:57:38 +00005015
5016 l = nasm_malloc(sizeof(Line));
5017 l->next = predef;
5018 l->first = def;
H. Peter Anvin538002d2008-06-28 18:30:27 -07005019 l->finishes = NULL;
H. Peter Anvin620515a2002-04-30 20:57:38 +00005020 predef = l;
5021}
5022
Keith Kaniosb7a89542007-04-12 02:40:54 +00005023/*
5024 * Added by Keith Kanios:
5025 *
5026 * This function is used to assist with "runtime" preprocessor
5027 * directives. (e.g. pp_runtime("%define __BITS__ 64");)
5028 *
5029 * ERRORS ARE IGNORED HERE, SO MAKE COMPLETELY SURE THAT YOU
5030 * PASS A VALID STRING TO THIS FUNCTION!!!!!
5031 */
5032
Keith Kaniosa6dfa782007-04-13 16:47:53 +00005033void pp_runtime(char *definition)
Keith Kaniosb7a89542007-04-12 02:40:54 +00005034{
5035 Token *def;
H. Peter Anvin70653092007-10-19 14:42:29 -07005036
Keith Kaniosb7a89542007-04-12 02:40:54 +00005037 def = tokenize(definition);
H. Peter Anvin89cee572009-07-15 09:16:54 -04005038 if (do_directive(def) == NO_DIRECTIVE_FOUND)
Keith Kaniosb7a89542007-04-12 02:40:54 +00005039 free_tlist(def);
H. Peter Anvin70653092007-10-19 14:42:29 -07005040
Keith Kaniosb7a89542007-04-12 02:40:54 +00005041}
5042
H. Peter Anvina70547f2008-07-19 21:44:26 -07005043void pp_extra_stdmac(macros_t *macros)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005044{
H. Peter Anvin76690a12002-04-30 20:52:49 +00005045 extrastdmac = macros;
5046}
5047
Keith Kaniosa5fc6462007-10-13 07:09:22 -07005048static void make_tok_num(Token * tok, int64_t val)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005049{
Keith Kaniosa6dfa782007-04-13 16:47:53 +00005050 char numbuf[20];
Keith Kaniosa5fc6462007-10-13 07:09:22 -07005051 snprintf(numbuf, sizeof(numbuf), "%"PRId64"", val);
H. Peter Anvineba20a72002-04-30 20:53:55 +00005052 tok->text = nasm_strdup(numbuf);
5053 tok->type = TOK_NUMBER;
5054}
5055
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005056Preproc nasmpp = {
5057 pp_reset,
5058 pp_getline,
5059 pp_cleanup
5060};