blob: 24fb7947d52e0daebe4781d59e61537247987165 [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
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000683#define BUF_DELTA 512
684/*
685 * Read a line from the top file in istk, handling multiple CR/LFs
686 * at the end of the line read, and handling spurious ^Zs. Will
687 * return lines from the standard macro set if this has not already
688 * been done.
689 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000690static char *read_line(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000691{
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000692 char *buffer, *p, *q;
H. Peter Anvin9f394642002-04-30 21:07:51 +0000693 int bufsize, continued_count;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000694
H. Peter Anvine2c80182005-01-15 22:15:51 +0000695 if (stdmacpos) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300696 unsigned char c;
697 const unsigned char *p = stdmacpos;
698 char *ret, *q;
699 size_t len = 0;
700 while ((c = *p++)) {
701 if (c >= 0x80)
702 len += pp_directives_len[c-0x80]+1;
703 else
704 len++;
705 }
706 ret = nasm_malloc(len+1);
707 q = ret;
708 while ((c = *stdmacpos++)) {
709 if (c >= 0x80) {
710 memcpy(q, pp_directives[c-0x80], pp_directives_len[c-0x80]);
711 q += pp_directives_len[c-0x80];
712 *q++ = ' ';
713 } else {
714 *q++ = c;
715 }
716 }
717 stdmacpos = p;
718 *q = '\0';
H. Peter Anvin72edbb82008-06-19 16:00:04 -0700719
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300720 if (!*stdmacpos) {
721 /* This was the last of the standard macro chain... */
722 stdmacpos = NULL;
723 if (any_extrastdmac) {
724 stdmacpos = extrastdmac;
725 any_extrastdmac = false;
726 } else if (do_predef) {
727 Line *pd, *l;
728 Token *head, **tail, *t;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000729
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300730 /*
731 * Nasty hack: here we push the contents of
732 * `predef' on to the top-level expansion stack,
733 * since this is the most convenient way to
734 * implement the pre-include and pre-define
735 * features.
736 */
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +0400737 list_for_each(pd, predef) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300738 head = NULL;
739 tail = &head;
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +0400740 list_for_each(t, pd->first) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300741 *tail = new_Token(NULL, t->type, t->text, 0);
742 tail = &(*tail)->next;
743 }
744 l = nasm_malloc(sizeof(Line));
745 l->next = istk->expansion;
746 l->first = head;
747 l->finishes = NULL;
748 istk->expansion = l;
749 }
750 do_predef = false;
751 }
752 }
753 return ret;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000754 }
755
756 bufsize = BUF_DELTA;
757 buffer = nasm_malloc(BUF_DELTA);
758 p = buffer;
H. Peter Anvin9f394642002-04-30 21:07:51 +0000759 continued_count = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000760 while (1) {
761 q = fgets(p, bufsize - (p - buffer), istk->fp);
762 if (!q)
763 break;
764 p += strlen(p);
765 if (p > buffer && p[-1] == '\n') {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300766 /*
767 * Convert backslash-CRLF line continuation sequences into
768 * nothing at all (for DOS and Windows)
769 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000770 if (((p - 2) > buffer) && (p[-3] == '\\') && (p[-2] == '\r')) {
771 p -= 3;
772 *p = 0;
773 continued_count++;
774 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300775 /*
776 * Also convert backslash-LF line continuation sequences into
777 * nothing at all (for Unix)
778 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000779 else if (((p - 1) > buffer) && (p[-2] == '\\')) {
780 p -= 2;
781 *p = 0;
782 continued_count++;
783 } else {
784 break;
785 }
786 }
787 if (p - buffer > bufsize - 10) {
Keith Kaniosb7a89542007-04-12 02:40:54 +0000788 int32_t offset = p - buffer;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000789 bufsize += BUF_DELTA;
790 buffer = nasm_realloc(buffer, bufsize);
791 p = buffer + offset; /* prevent stale-pointer problems */
792 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000793 }
794
H. Peter Anvine2c80182005-01-15 22:15:51 +0000795 if (!q && p == buffer) {
796 nasm_free(buffer);
797 return NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000798 }
799
H. Peter Anvine2c80182005-01-15 22:15:51 +0000800 src_set_linnum(src_get_linnum() + istk->lineinc +
801 (continued_count * istk->lineinc));
H. Peter Anvineba20a72002-04-30 20:53:55 +0000802
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000803 /*
804 * Play safe: remove CRs as well as LFs, if any of either are
805 * present at the end of the line.
806 */
H. Peter Anvineba20a72002-04-30 20:53:55 +0000807 while (--p >= buffer && (*p == '\n' || *p == '\r'))
H. Peter Anvine2c80182005-01-15 22:15:51 +0000808 *p = '\0';
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000809
810 /*
811 * Handle spurious ^Z, which may be inserted into source files
812 * by some file transfer utilities.
813 */
814 buffer[strcspn(buffer, "\032")] = '\0';
815
H. Peter Anvin734b1882002-04-30 21:01:08 +0000816 list->line(LIST_READ, buffer);
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000817
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000818 return buffer;
819}
820
821/*
Keith Kaniosb7a89542007-04-12 02:40:54 +0000822 * Tokenize a line of text. This is a very simple process since we
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000823 * don't need to parse the value out of e.g. numeric tokens: we
824 * simply split one string into many.
825 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000826static Token *tokenize(char *line)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000827{
H. Peter Anvinca544db2008-10-19 19:30:11 -0700828 char c, *p = line;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +0000829 enum pp_token_type type;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000830 Token *list = NULL;
831 Token *t, **tail = &list;
832
H. Peter Anvine2c80182005-01-15 22:15:51 +0000833 while (*line) {
834 p = line;
835 if (*p == '%') {
836 p++;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300837 if (*p == '+' && !nasm_isdigit(p[1])) {
838 p++;
839 type = TOK_PASTE;
840 } else if (nasm_isdigit(*p) ||
841 ((*p == '-' || *p == '+') && nasm_isdigit(p[1]))) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000842 do {
843 p++;
844 }
H. Peter Anvinbda7a6e2008-06-21 10:23:17 -0700845 while (nasm_isdigit(*p));
H. Peter Anvine2c80182005-01-15 22:15:51 +0000846 type = TOK_PREPROC_ID;
847 } else if (*p == '{') {
848 p++;
849 while (*p && *p != '}') {
850 p[-1] = *p;
851 p++;
852 }
853 p[-1] = '\0';
854 if (*p)
855 p++;
856 type = TOK_PREPROC_ID;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300857 } else if (*p == '[') {
858 int lvl = 1;
859 line += 2; /* Skip the leading %[ */
860 p++;
861 while (lvl && (c = *p++)) {
862 switch (c) {
863 case ']':
864 lvl--;
865 break;
866 case '%':
867 if (*p == '[')
868 lvl++;
869 break;
870 case '\'':
871 case '\"':
872 case '`':
873 p = nasm_skip_string(p)+1;
874 break;
875 default:
876 break;
877 }
878 }
879 p--;
880 if (*p)
881 *p++ = '\0';
882 if (lvl)
883 error(ERR_NONFATAL, "unterminated %[ construct");
884 type = TOK_INDIRECT;
885 } else if (*p == '?') {
886 type = TOK_PREPROC_Q; /* %? */
887 p++;
888 if (*p == '?') {
889 type = TOK_PREPROC_QQ; /* %?? */
890 p++;
891 }
H. Peter Anvine2c80182005-01-15 22:15:51 +0000892 } else if (isidchar(*p) ||
893 ((*p == '!' || *p == '%' || *p == '$') &&
894 isidchar(p[1]))) {
895 do {
896 p++;
897 }
898 while (isidchar(*p));
899 type = TOK_PREPROC_ID;
900 } else {
901 type = TOK_OTHER;
902 if (*p == '%')
903 p++;
904 }
905 } else if (isidstart(*p) || (*p == '$' && isidstart(p[1]))) {
906 type = TOK_ID;
907 p++;
908 while (*p && isidchar(*p))
909 p++;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -0700910 } else if (*p == '\'' || *p == '"' || *p == '`') {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000911 /*
912 * A string token.
913 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000914 type = TOK_STRING;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300915 p = nasm_skip_string(p);
Nickolay Yurchenkof3b3ce22003-09-21 20:38:43 +0000916
H. Peter Anvine2c80182005-01-15 22:15:51 +0000917 if (*p) {
918 p++;
919 } else {
H. Peter Anvin917a3492008-09-24 09:14:49 -0700920 error(ERR_WARNING|ERR_PASS1, "unterminated string");
H. Peter Anvine2c80182005-01-15 22:15:51 +0000921 /* Handling unterminated strings by UNV */
922 /* type = -1; */
923 }
Victor van den Elzenfb5f2512009-04-17 16:17:59 +0200924 } else if (p[0] == '$' && p[1] == '$') {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300925 type = TOK_OTHER; /* TOKEN_BASE */
Victor van den Elzenfb5f2512009-04-17 16:17:59 +0200926 p += 2;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000927 } else if (isnumstart(*p)) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300928 bool is_hex = false;
929 bool is_float = false;
930 bool has_e = false;
931 char c, *r;
H. Peter Anvinc2df2822007-10-24 15:29:28 -0700932
H. Peter Anvine2c80182005-01-15 22:15:51 +0000933 /*
H. Peter Anvinc2df2822007-10-24 15:29:28 -0700934 * A numeric token.
H. Peter Anvine2c80182005-01-15 22:15:51 +0000935 */
H. Peter Anvinc2df2822007-10-24 15:29:28 -0700936
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300937 if (*p == '$') {
938 p++;
939 is_hex = true;
940 }
H. Peter Anvinc2df2822007-10-24 15:29:28 -0700941
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300942 for (;;) {
943 c = *p++;
H. Peter Anvinc2df2822007-10-24 15:29:28 -0700944
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300945 if (!is_hex && (c == 'e' || c == 'E')) {
946 has_e = true;
947 if (*p == '+' || *p == '-') {
948 /*
949 * e can only be followed by +/- if it is either a
950 * prefixed hex number or a floating-point number
951 */
952 p++;
953 is_float = true;
954 }
955 } else if (c == 'H' || c == 'h' || c == 'X' || c == 'x') {
956 is_hex = true;
957 } else if (c == 'P' || c == 'p') {
958 is_float = true;
959 if (*p == '+' || *p == '-')
960 p++;
961 } else if (isnumchar(c) || c == '_')
962 ; /* just advance */
963 else if (c == '.') {
964 /*
965 * we need to deal with consequences of the legacy
966 * parser, like "1.nolist" being two tokens
967 * (TOK_NUMBER, TOK_ID) here; at least give it
968 * a shot for now. In the future, we probably need
969 * a flex-based scanner with proper pattern matching
970 * to do it as well as it can be done. Nothing in
971 * the world is going to help the person who wants
972 * 0x123.p16 interpreted as two tokens, though.
973 */
974 r = p;
975 while (*r == '_')
976 r++;
H. Peter Anvinc2df2822007-10-24 15:29:28 -0700977
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300978 if (nasm_isdigit(*r) || (is_hex && nasm_isxdigit(*r)) ||
979 (!is_hex && (*r == 'e' || *r == 'E')) ||
980 (*r == 'p' || *r == 'P')) {
981 p = r;
982 is_float = true;
983 } else
984 break; /* Terminate the token */
985 } else
986 break;
987 }
988 p--; /* Point to first character beyond number */
H. Peter Anvinc2df2822007-10-24 15:29:28 -0700989
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300990 if (p == line+1 && *line == '$') {
991 type = TOK_OTHER; /* TOKEN_HERE */
992 } else {
993 if (has_e && !is_hex) {
994 /* 1e13 is floating-point, but 1e13h is not */
995 is_float = true;
996 }
H. Peter Anvind784a082009-04-20 14:01:18 -0700997
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300998 type = is_float ? TOK_FLOAT : TOK_NUMBER;
999 }
H. Peter Anvinbda7a6e2008-06-21 10:23:17 -07001000 } else if (nasm_isspace(*p)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001001 type = TOK_WHITESPACE;
Cyrill Gorcunovf66ac7d2009-10-12 20:41:13 +04001002 p = nasm_skip_spaces(p);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001003 /*
1004 * Whitespace just before end-of-line is discarded by
1005 * pretending it's a comment; whitespace just before a
1006 * comment gets lumped into the comment.
1007 */
1008 if (!*p || *p == ';') {
1009 type = TOK_COMMENT;
1010 while (*p)
1011 p++;
1012 }
1013 } else if (*p == ';') {
1014 type = TOK_COMMENT;
1015 while (*p)
1016 p++;
1017 } else {
1018 /*
1019 * Anything else is an operator of some kind. We check
1020 * for all the double-character operators (>>, <<, //,
1021 * %%, <=, >=, ==, !=, <>, &&, ||, ^^), but anything
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001022 * else is a single-character operator.
H. Peter Anvine2c80182005-01-15 22:15:51 +00001023 */
1024 type = TOK_OTHER;
1025 if ((p[0] == '>' && p[1] == '>') ||
1026 (p[0] == '<' && p[1] == '<') ||
1027 (p[0] == '/' && p[1] == '/') ||
1028 (p[0] == '<' && p[1] == '=') ||
1029 (p[0] == '>' && p[1] == '=') ||
1030 (p[0] == '=' && p[1] == '=') ||
1031 (p[0] == '!' && p[1] == '=') ||
1032 (p[0] == '<' && p[1] == '>') ||
1033 (p[0] == '&' && p[1] == '&') ||
1034 (p[0] == '|' && p[1] == '|') ||
1035 (p[0] == '^' && p[1] == '^')) {
1036 p++;
1037 }
1038 p++;
1039 }
Nickolay Yurchenkof3b3ce22003-09-21 20:38:43 +00001040
H. Peter Anvine2c80182005-01-15 22:15:51 +00001041 /* Handling unterminated string by UNV */
1042 /*if (type == -1)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001043 {
1044 *tail = t = new_Token(NULL, TOK_STRING, line, p-line+1);
1045 t->text[p-line] = *line;
1046 tail = &t->next;
1047 }
1048 else */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001049 if (type != TOK_COMMENT) {
1050 *tail = t = new_Token(NULL, type, line, p - line);
1051 tail = &t->next;
1052 }
1053 line = p;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001054 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001055 return list;
1056}
1057
H. Peter Anvince616072002-04-30 21:02:23 +00001058/*
1059 * this function allocates a new managed block of memory and
H. Peter Anvin70653092007-10-19 14:42:29 -07001060 * returns a pointer to the block. The managed blocks are
H. Peter Anvince616072002-04-30 21:02:23 +00001061 * deleted only all at once by the delete_Blocks function.
1062 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001063static void *new_Block(size_t size)
H. Peter Anvince616072002-04-30 21:02:23 +00001064{
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001065 Blocks *b = &blocks;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001066
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001067 /* first, get to the end of the linked list */
1068 while (b->next)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001069 b = b->next;
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001070 /* now allocate the requested chunk */
1071 b->chunk = nasm_malloc(size);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001072
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001073 /* now allocate a new block for the next request */
1074 b->next = nasm_malloc(sizeof(Blocks));
1075 /* and initialize the contents of the new block */
1076 b->next->next = NULL;
1077 b->next->chunk = NULL;
1078 return b->chunk;
H. Peter Anvince616072002-04-30 21:02:23 +00001079}
1080
1081/*
1082 * this function deletes all managed blocks of memory
1083 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001084static void delete_Blocks(void)
H. Peter Anvince616072002-04-30 21:02:23 +00001085{
H. Peter Anvine2c80182005-01-15 22:15:51 +00001086 Blocks *a, *b = &blocks;
H. Peter Anvince616072002-04-30 21:02:23 +00001087
H. Peter Anvin70653092007-10-19 14:42:29 -07001088 /*
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001089 * keep in mind that the first block, pointed to by blocks
H. Peter Anvin70653092007-10-19 14:42:29 -07001090 * is a static and not dynamically allocated, so we don't
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001091 * free it.
1092 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001093 while (b) {
1094 if (b->chunk)
1095 nasm_free(b->chunk);
1096 a = b;
1097 b = b->next;
1098 if (a != &blocks)
1099 nasm_free(a);
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001100 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001101}
H. Peter Anvin734b1882002-04-30 21:01:08 +00001102
1103/*
H. Peter Anvin70653092007-10-19 14:42:29 -07001104 * this function creates a new Token and passes a pointer to it
H. Peter Anvin734b1882002-04-30 21:01:08 +00001105 * back to the caller. It sets the type and text elements, and
H. Peter Anvinf26e0972008-07-01 21:26:27 -07001106 * also the a.mac and next elements to NULL.
H. Peter Anvin734b1882002-04-30 21:01:08 +00001107 */
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07001108static Token *new_Token(Token * next, enum pp_token_type type,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001109 const char *text, int txtlen)
H. Peter Anvin734b1882002-04-30 21:01:08 +00001110{
1111 Token *t;
1112 int i;
1113
H. Peter Anvin89cee572009-07-15 09:16:54 -04001114 if (!freeTokens) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001115 freeTokens = (Token *) new_Block(TOKEN_BLOCKSIZE * sizeof(Token));
1116 for (i = 0; i < TOKEN_BLOCKSIZE - 1; i++)
1117 freeTokens[i].next = &freeTokens[i + 1];
1118 freeTokens[i].next = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001119 }
1120 t = freeTokens;
1121 freeTokens = t->next;
1122 t->next = next;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07001123 t->a.mac = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001124 t->type = type;
H. Peter Anvin89cee572009-07-15 09:16:54 -04001125 if (type == TOK_WHITESPACE || !text) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001126 t->text = NULL;
1127 } else {
1128 if (txtlen == 0)
1129 txtlen = strlen(text);
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07001130 t->text = nasm_malloc(txtlen+1);
1131 memcpy(t->text, text, txtlen);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001132 t->text[txtlen] = '\0';
H. Peter Anvin734b1882002-04-30 21:01:08 +00001133 }
1134 return t;
1135}
1136
H. Peter Anvine2c80182005-01-15 22:15:51 +00001137static Token *delete_Token(Token * t)
H. Peter Anvin734b1882002-04-30 21:01:08 +00001138{
1139 Token *next = t->next;
1140 nasm_free(t->text);
H. Peter Anvin788e6c12002-04-30 21:02:01 +00001141 t->next = freeTokens;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001142 freeTokens = t;
1143 return next;
1144}
1145
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001146/*
1147 * Convert a line of tokens back into text.
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001148 * If expand_locals is not zero, identifiers of the form "%$*xxx"
1149 * will be transformed into ..@ctxnum.xxx
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001150 */
H. Peter Anvin9e200162008-06-04 17:23:14 -07001151static char *detoken(Token * tlist, bool expand_locals)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001152{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001153 Token *t;
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001154 char *line, *p;
H. Peter Anvinb4daadc2008-01-21 16:31:57 -08001155 const char *q;
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001156 int len = 0;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001157
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001158 list_for_each(t, tlist) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001159 if (t->type == TOK_PREPROC_ID && t->text[1] == '!') {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001160 char *p = getenv(t->text + 2);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001161 nasm_free(t->text);
1162 if (p)
1163 t->text = nasm_strdup(p);
1164 else
1165 t->text = NULL;
1166 }
1167 /* Expand local macros here and not during preprocessing */
1168 if (expand_locals &&
1169 t->type == TOK_PREPROC_ID && t->text &&
1170 t->text[0] == '%' && t->text[1] == '$') {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001171 const char *q;
1172 char *p;
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001173 Context *ctx = get_ctx(t->text, &q, false);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001174 if (ctx) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001175 char buffer[40];
Keith Kanios93f2e9a2007-04-14 00:10:59 +00001176 snprintf(buffer, sizeof(buffer), "..@%"PRIu32".", ctx->number);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001177 p = nasm_strcat(buffer, q);
1178 nasm_free(t->text);
1179 t->text = p;
1180 }
1181 }
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001182 if (t->type == TOK_WHITESPACE)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001183 len++;
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001184 else if (t->text)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001185 len += strlen(t->text);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001186 }
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001187
H. Peter Anvin734b1882002-04-30 21:01:08 +00001188 p = line = nasm_malloc(len + 1);
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001189
1190 list_for_each(t, tlist) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001191 if (t->type == TOK_WHITESPACE) {
H. Peter Anvinb4daadc2008-01-21 16:31:57 -08001192 *p++ = ' ';
H. Peter Anvine2c80182005-01-15 22:15:51 +00001193 } else if (t->text) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001194 q = t->text;
1195 while (*q)
1196 *p++ = *q++;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001197 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001198 }
1199 *p = '\0';
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001200
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001201 return line;
1202}
1203
1204/*
H. Peter Anvin76690a12002-04-30 20:52:49 +00001205 * A scanner, suitable for use by the expression evaluator, which
1206 * operates on a line of Tokens. Expects a pointer to a pointer to
1207 * the first token in the line to be passed in as its private_data
1208 * field.
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001209 *
1210 * FIX: This really needs to be unified with stdscan.
H. Peter Anvin76690a12002-04-30 20:52:49 +00001211 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001212static int ppscan(void *private_data, struct tokenval *tokval)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001213{
H. Peter Anvin76690a12002-04-30 20:52:49 +00001214 Token **tlineptr = private_data;
1215 Token *tline;
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001216 char ourcopy[MAX_KEYWORD+1], *p, *r, *s;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001217
H. Peter Anvine2c80182005-01-15 22:15:51 +00001218 do {
1219 tline = *tlineptr;
1220 *tlineptr = tline ? tline->next : NULL;
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04001221 } while (tline && (tline->type == TOK_WHITESPACE ||
1222 tline->type == TOK_COMMENT));
H. Peter Anvin76690a12002-04-30 20:52:49 +00001223
1224 if (!tline)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001225 return tokval->t_type = TOKEN_EOS;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001226
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001227 tokval->t_charptr = tline->text;
1228
H. Peter Anvin76690a12002-04-30 20:52:49 +00001229 if (tline->text[0] == '$' && !tline->text[1])
H. Peter Anvine2c80182005-01-15 22:15:51 +00001230 return tokval->t_type = TOKEN_HERE;
H. Peter Anvin7cf897e2002-05-30 21:30:33 +00001231 if (tline->text[0] == '$' && tline->text[1] == '$' && !tline->text[2])
H. Peter Anvine2c80182005-01-15 22:15:51 +00001232 return tokval->t_type = TOKEN_BASE;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001233
H. Peter Anvine2c80182005-01-15 22:15:51 +00001234 if (tline->type == TOK_ID) {
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001235 p = tokval->t_charptr = tline->text;
1236 if (p[0] == '$') {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001237 tokval->t_charptr++;
1238 return tokval->t_type = TOKEN_ID;
1239 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00001240
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001241 for (r = p, s = ourcopy; *r; r++) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001242 if (r >= p+MAX_KEYWORD)
1243 return tokval->t_type = TOKEN_ID; /* Not a keyword */
H. Peter Anvinac8f8fc2008-06-11 15:49:41 -07001244 *s++ = nasm_tolower(*r);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001245 }
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001246 *s = '\0';
1247 /* right, so we have an identifier sitting in temp storage. now,
1248 * is it actually a register or instruction name, or what? */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001249 return nasm_token_hash(ourcopy, tokval);
H. Peter Anvin76690a12002-04-30 20:52:49 +00001250 }
1251
H. Peter Anvine2c80182005-01-15 22:15:51 +00001252 if (tline->type == TOK_NUMBER) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001253 bool rn_error;
1254 tokval->t_integer = readnum(tline->text, &rn_error);
1255 tokval->t_charptr = tline->text;
1256 if (rn_error)
1257 return tokval->t_type = TOKEN_ERRNUM;
1258 else
1259 return tokval->t_type = TOKEN_NUM;
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001260 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00001261
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001262 if (tline->type == TOK_FLOAT) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001263 return tokval->t_type = TOKEN_FLOAT;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001264 }
1265
H. Peter Anvine2c80182005-01-15 22:15:51 +00001266 if (tline->type == TOK_STRING) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001267 char bq, *ep;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001268
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001269 bq = tline->text[0];
H. Peter Anvin11627042008-06-09 20:45:19 -07001270 tokval->t_charptr = tline->text;
1271 tokval->t_inttwo = nasm_unquote(tline->text, &ep);
H. Peter Anvind2456592008-06-19 15:04:18 -07001272
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001273 if (ep[0] != bq || ep[1] != '\0')
1274 return tokval->t_type = TOKEN_ERRSTR;
1275 else
1276 return tokval->t_type = TOKEN_STR;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001277 }
1278
H. Peter Anvine2c80182005-01-15 22:15:51 +00001279 if (tline->type == TOK_OTHER) {
1280 if (!strcmp(tline->text, "<<"))
1281 return tokval->t_type = TOKEN_SHL;
1282 if (!strcmp(tline->text, ">>"))
1283 return tokval->t_type = TOKEN_SHR;
1284 if (!strcmp(tline->text, "//"))
1285 return tokval->t_type = TOKEN_SDIV;
1286 if (!strcmp(tline->text, "%%"))
1287 return tokval->t_type = TOKEN_SMOD;
1288 if (!strcmp(tline->text, "=="))
1289 return tokval->t_type = TOKEN_EQ;
1290 if (!strcmp(tline->text, "<>"))
1291 return tokval->t_type = TOKEN_NE;
1292 if (!strcmp(tline->text, "!="))
1293 return tokval->t_type = TOKEN_NE;
1294 if (!strcmp(tline->text, "<="))
1295 return tokval->t_type = TOKEN_LE;
1296 if (!strcmp(tline->text, ">="))
1297 return tokval->t_type = TOKEN_GE;
1298 if (!strcmp(tline->text, "&&"))
1299 return tokval->t_type = TOKEN_DBL_AND;
1300 if (!strcmp(tline->text, "^^"))
1301 return tokval->t_type = TOKEN_DBL_XOR;
1302 if (!strcmp(tline->text, "||"))
1303 return tokval->t_type = TOKEN_DBL_OR;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001304 }
1305
1306 /*
1307 * We have no other options: just return the first character of
1308 * the token text.
1309 */
1310 return tokval->t_type = tline->text[0];
1311}
1312
1313/*
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001314 * Compare a string to the name of an existing macro; this is a
1315 * simple wrapper which calls either strcmp or nasm_stricmp
1316 * depending on the value of the `casesense' parameter.
1317 */
H. Peter Anvin4db5a162007-10-11 13:42:09 -07001318static int mstrcmp(const char *p, const char *q, bool casesense)
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001319{
H. Peter Anvin734b1882002-04-30 21:01:08 +00001320 return casesense ? strcmp(p, q) : nasm_stricmp(p, q);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001321}
1322
1323/*
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07001324 * Compare a string to the name of an existing macro; this is a
1325 * simple wrapper which calls either strcmp or nasm_stricmp
1326 * depending on the value of the `casesense' parameter.
1327 */
1328static int mmemcmp(const char *p, const char *q, size_t l, bool casesense)
1329{
1330 return casesense ? memcmp(p, q, l) : nasm_memicmp(p, q, l);
1331}
1332
1333/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001334 * Return the Context structure associated with a %$ token. Return
1335 * NULL, having _already_ reported an error condition, if the
1336 * context stack isn't deep enough for the supplied number of $
1337 * signs.
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001338 * If all_contexts == true, contexts that enclose current are
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001339 * also scanned for such smacro, until it is found; if not -
1340 * only the context that directly results from the number of $'s
1341 * in variable's name.
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001342 *
1343 * If "namep" is non-NULL, set it to the pointer to the macro name
1344 * tail, i.e. the part beyond %$...
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001345 */
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001346static Context *get_ctx(const char *name, const char **namep,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001347 bool all_contexts)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001348{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001349 Context *ctx;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001350 SMacro *m;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001351 int i;
1352
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001353 if (namep)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001354 *namep = name;
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001355
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001356 if (!name || name[0] != '%' || name[1] != '$')
H. Peter Anvine2c80182005-01-15 22:15:51 +00001357 return NULL;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001358
H. Peter Anvine2c80182005-01-15 22:15:51 +00001359 if (!cstk) {
1360 error(ERR_NONFATAL, "`%s': context stack is empty", name);
1361 return NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001362 }
1363
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001364 name += 2;
1365 ctx = cstk;
1366 i = 0;
1367 while (ctx && *name == '$') {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001368 name++;
1369 i++;
1370 ctx = ctx->next;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001371 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001372 if (!ctx) {
1373 error(ERR_NONFATAL, "`%s': context stack is only"
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001374 " %d level%s deep", name, i, (i == 1 ? "" : "s"));
H. Peter Anvine2c80182005-01-15 22:15:51 +00001375 return NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001376 }
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001377
1378 if (namep)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001379 *namep = name;
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001380
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001381 if (!all_contexts)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001382 return ctx;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001383
H. Peter Anvine2c80182005-01-15 22:15:51 +00001384 do {
1385 /* Search for this smacro in found context */
H. Peter Anvin166c2472008-05-28 12:28:58 -07001386 m = hash_findix(&ctx->localmac, name);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001387 while (m) {
1388 if (!mstrcmp(m->name, name, m->casesense))
1389 return ctx;
1390 m = m->next;
1391 }
1392 ctx = ctx->next;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001393 }
1394 while (ctx);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001395 return NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001396}
1397
1398/*
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001399 * Check to see if a file is already in a string list
1400 */
1401static bool in_list(const StrList *list, const char *str)
1402{
1403 while (list) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001404 if (!strcmp(list->str, str))
1405 return true;
1406 list = list->next;
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001407 }
1408 return false;
1409}
1410
1411/*
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001412 * Open an include file. This routine must always return a valid
1413 * file pointer if it returns - it's responsible for throwing an
1414 * ERR_FATAL and bombing out completely if not. It should also try
1415 * the include path one by one until it finds the file or reaches
1416 * the end of the path.
1417 */
H. Peter Anvin2b1c3b92008-06-06 10:38:46 -07001418static FILE *inc_fopen(const char *file, StrList **dhead, StrList ***dtail,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001419 bool missing_ok)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001420{
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001421 FILE *fp;
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001422 char *prefix = "";
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001423 IncPath *ip = ipath;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001424 int len = strlen(file);
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001425 size_t prefix_len = 0;
1426 StrList *sl;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001427
H. Peter Anvine2c80182005-01-15 22:15:51 +00001428 while (1) {
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001429 sl = nasm_malloc(prefix_len+len+1+sizeof sl->next);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001430 memcpy(sl->str, prefix, prefix_len);
1431 memcpy(sl->str+prefix_len, file, len+1);
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001432 fp = fopen(sl->str, "r");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001433 if (fp && dhead && !in_list(*dhead, sl->str)) {
1434 sl->next = NULL;
1435 **dtail = sl;
1436 *dtail = &sl->next;
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001437 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001438 nasm_free(sl);
1439 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001440 if (fp)
1441 return fp;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001442 if (!ip) {
1443 if (!missing_ok)
1444 break;
1445 prefix = NULL;
1446 } else {
1447 prefix = ip->path;
1448 ip = ip->next;
1449 }
1450 if (prefix) {
1451 prefix_len = strlen(prefix);
1452 } else {
1453 /* -MG given and file not found */
1454 if (dhead && !in_list(*dhead, file)) {
1455 sl = nasm_malloc(len+1+sizeof sl->next);
1456 sl->next = NULL;
1457 strcpy(sl->str, file);
1458 **dtail = sl;
1459 *dtail = &sl->next;
1460 }
1461 return NULL;
1462 }
H. Peter Anvineba20a72002-04-30 20:53:55 +00001463 }
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001464
H. Peter Anvin734b1882002-04-30 21:01:08 +00001465 error(ERR_FATAL, "unable to open include file `%s'", file);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001466 return NULL;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001467}
1468
1469/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001470 * Determine if we should warn on defining a single-line macro of
H. Peter Anvinef7468f2002-04-30 20:57:59 +00001471 * name `name', with `nparam' parameters. If nparam is 0 or -1, will
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001472 * return true if _any_ single-line macro of that name is defined.
1473 * Otherwise, will return true if a single-line macro with either
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001474 * `nparam' or no parameters is defined.
1475 *
1476 * If a macro with precisely the right number of parameters is
H. Peter Anvinef7468f2002-04-30 20:57:59 +00001477 * defined, or nparam is -1, the address of the definition structure
1478 * will be returned in `defn'; otherwise NULL will be returned. If `defn'
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001479 * is NULL, no action will be taken regarding its contents, and no
1480 * error will occur.
1481 *
1482 * Note that this is also called with nparam zero to resolve
1483 * `ifdef'.
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001484 *
1485 * If you already know which context macro belongs to, you can pass
1486 * the context pointer as first parameter; if you won't but name begins
1487 * with %$ the context will be automatically computed. If all_contexts
1488 * is true, macro will be searched in outer contexts as well.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001489 */
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07001490static bool
H. Peter Anvinb2a5fda2008-06-19 21:42:42 -07001491smacro_defined(Context * ctx, const char *name, int nparam, SMacro ** defn,
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07001492 bool nocase)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001493{
H. Peter Anvin166c2472008-05-28 12:28:58 -07001494 struct hash_table *smtbl;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001495 SMacro *m;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001496
H. Peter Anvin97a23472007-09-16 17:57:25 -07001497 if (ctx) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001498 smtbl = &ctx->localmac;
H. Peter Anvin97a23472007-09-16 17:57:25 -07001499 } else if (name[0] == '%' && name[1] == '$') {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001500 if (cstk)
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001501 ctx = get_ctx(name, &name, false);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001502 if (!ctx)
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001503 return false; /* got to return _something_ */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001504 smtbl = &ctx->localmac;
H. Peter Anvin97a23472007-09-16 17:57:25 -07001505 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001506 smtbl = &smacros;
H. Peter Anvin97a23472007-09-16 17:57:25 -07001507 }
H. Peter Anvin166c2472008-05-28 12:28:58 -07001508 m = (SMacro *) hash_findix(smtbl, name);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001509
H. Peter Anvine2c80182005-01-15 22:15:51 +00001510 while (m) {
1511 if (!mstrcmp(m->name, name, m->casesense && nocase) &&
Charles Crayne192d5b52007-10-18 19:02:42 -07001512 (nparam <= 0 || m->nparam == 0 || nparam == (int) m->nparam)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001513 if (defn) {
Charles Crayne192d5b52007-10-18 19:02:42 -07001514 if (nparam == (int) m->nparam || nparam == -1)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001515 *defn = m;
1516 else
1517 *defn = NULL;
1518 }
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001519 return true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001520 }
1521 m = m->next;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001522 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001523
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001524 return false;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001525}
1526
1527/*
1528 * Count and mark off the parameters in a multi-line macro call.
1529 * This is called both from within the multi-line macro expansion
1530 * code, and also to mark off the default parameters when provided
1531 * in a %macro definition line.
1532 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001533static void count_mmac_params(Token * t, int *nparam, Token *** params)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001534{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001535 int paramsize, brace;
1536
1537 *nparam = paramsize = 0;
1538 *params = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001539 while (t) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001540 /* +1: we need space for the final NULL */
H. Peter Anvin91fb6f12008-09-01 10:56:33 -07001541 if (*nparam+1 >= paramsize) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001542 paramsize += PARAM_DELTA;
1543 *params = nasm_realloc(*params, sizeof(**params) * paramsize);
1544 }
1545 skip_white_(t);
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001546 brace = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001547 if (tok_is_(t, "{"))
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001548 brace = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001549 (*params)[(*nparam)++] = t;
1550 while (tok_isnt_(t, brace ? "}" : ","))
1551 t = t->next;
1552 if (t) { /* got a comma/brace */
1553 t = t->next;
1554 if (brace) {
1555 /*
1556 * Now we've found the closing brace, look further
1557 * for the comma.
1558 */
1559 skip_white_(t);
1560 if (tok_isnt_(t, ",")) {
1561 error(ERR_NONFATAL,
1562 "braces do not enclose all of macro parameter");
1563 while (tok_isnt_(t, ","))
1564 t = t->next;
1565 }
1566 if (t)
1567 t = t->next; /* eat the comma */
1568 }
1569 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001570 }
1571}
1572
1573/*
H. Peter Anvin76690a12002-04-30 20:52:49 +00001574 * Determine whether one of the various `if' conditions is true or
1575 * not.
1576 *
1577 * We must free the tline we get passed.
1578 */
H. Peter Anvin70055962007-10-11 00:05:31 -07001579static bool if_condition(Token * tline, enum preproc_token ct)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001580{
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001581 enum pp_conditional i = PP_COND(ct);
H. Peter Anvin70055962007-10-11 00:05:31 -07001582 bool j;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001583 Token *t, *tt, **tptr, *origline;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001584 struct tokenval tokval;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001585 expr *evalresult;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001586 enum pp_token_type needtype;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001587
1588 origline = tline;
1589
H. Peter Anvine2c80182005-01-15 22:15:51 +00001590 switch (i) {
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001591 case PPC_IFCTX:
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001592 j = false; /* have we matched yet? */
Victor van den Elzen0e857f12008-07-23 13:21:29 +02001593 while (true) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001594 skip_white_(tline);
Victor van den Elzen0e857f12008-07-23 13:21:29 +02001595 if (!tline)
1596 break;
1597 if (tline->type != TOK_ID) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001598 error(ERR_NONFATAL,
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001599 "`%s' expects context identifiers", pp_directives[ct]);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001600 free_tlist(origline);
1601 return -1;
1602 }
Victor van den Elzen0e857f12008-07-23 13:21:29 +02001603 if (cstk && cstk->name && !nasm_stricmp(tline->text, cstk->name))
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001604 j = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001605 tline = tline->next;
1606 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001607 break;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001608
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001609 case PPC_IFDEF:
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001610 j = false; /* have we matched yet? */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001611 while (tline) {
1612 skip_white_(tline);
1613 if (!tline || (tline->type != TOK_ID &&
1614 (tline->type != TOK_PREPROC_ID ||
1615 tline->text[1] != '$'))) {
1616 error(ERR_NONFATAL,
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001617 "`%s' expects macro identifiers", pp_directives[ct]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001618 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001619 }
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07001620 if (smacro_defined(NULL, tline->text, 0, NULL, true))
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001621 j = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001622 tline = tline->next;
1623 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001624 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001625
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001626 case PPC_IFIDN:
1627 case PPC_IFIDNI:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001628 tline = expand_smacro(tline);
1629 t = tt = tline;
1630 while (tok_isnt_(tt, ","))
1631 tt = tt->next;
1632 if (!tt) {
1633 error(ERR_NONFATAL,
1634 "`%s' expects two comma-separated arguments",
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001635 pp_directives[ct]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001636 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001637 }
1638 tt = tt->next;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001639 j = true; /* assume equality unless proved not */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001640 while ((t->type != TOK_OTHER || strcmp(t->text, ",")) && tt) {
1641 if (tt->type == TOK_OTHER && !strcmp(tt->text, ",")) {
1642 error(ERR_NONFATAL, "`%s': more than one comma on line",
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001643 pp_directives[ct]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001644 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001645 }
1646 if (t->type == TOK_WHITESPACE) {
1647 t = t->next;
1648 continue;
1649 }
1650 if (tt->type == TOK_WHITESPACE) {
1651 tt = tt->next;
1652 continue;
1653 }
1654 if (tt->type != t->type) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001655 j = false; /* found mismatching tokens */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001656 break;
1657 }
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07001658 /* When comparing strings, need to unquote them first */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001659 if (t->type == TOK_STRING) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001660 size_t l1 = nasm_unquote(t->text, NULL);
1661 size_t l2 = nasm_unquote(tt->text, NULL);
H. Peter Anvind2456592008-06-19 15:04:18 -07001662
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001663 if (l1 != l2) {
1664 j = false;
1665 break;
1666 }
1667 if (mmemcmp(t->text, tt->text, l1, i == PPC_IFIDN)) {
1668 j = false;
1669 break;
1670 }
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07001671 } else if (mstrcmp(tt->text, t->text, i == PPC_IFIDN) != 0) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001672 j = false; /* found mismatching tokens */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001673 break;
1674 }
Nickolay Yurchenkof3b3ce22003-09-21 20:38:43 +00001675
H. Peter Anvine2c80182005-01-15 22:15:51 +00001676 t = t->next;
1677 tt = tt->next;
1678 }
1679 if ((t->type != TOK_OTHER || strcmp(t->text, ",")) || tt)
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001680 j = false; /* trailing gunk on one end or other */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001681 break;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001682
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001683 case PPC_IFMACRO:
H. Peter Anvin89cee572009-07-15 09:16:54 -04001684 {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001685 bool found = false;
1686 MMacro searching, *mmac;
H. Peter Anvin65747262002-05-07 00:10:05 +00001687
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001688 skip_white_(tline);
1689 tline = expand_id(tline);
1690 if (!tok_type_(tline, TOK_ID)) {
1691 error(ERR_NONFATAL,
1692 "`%s' expects a macro name", pp_directives[ct]);
1693 goto fail;
1694 }
1695 searching.name = nasm_strdup(tline->text);
1696 searching.casesense = true;
1697 searching.plus = false;
1698 searching.nolist = false;
1699 searching.in_progress = 0;
1700 searching.max_depth = 0;
1701 searching.rep_nest = NULL;
1702 searching.nparam_min = 0;
1703 searching.nparam_max = INT_MAX;
1704 tline = expand_smacro(tline->next);
1705 skip_white_(tline);
1706 if (!tline) {
1707 } else if (!tok_type_(tline, TOK_NUMBER)) {
1708 error(ERR_NONFATAL,
1709 "`%s' expects a parameter count or nothing",
1710 pp_directives[ct]);
1711 } else {
1712 searching.nparam_min = searching.nparam_max =
1713 readnum(tline->text, &j);
1714 if (j)
1715 error(ERR_NONFATAL,
1716 "unable to parse parameter count `%s'",
1717 tline->text);
1718 }
1719 if (tline && tok_is_(tline->next, "-")) {
1720 tline = tline->next->next;
1721 if (tok_is_(tline, "*"))
1722 searching.nparam_max = INT_MAX;
1723 else if (!tok_type_(tline, TOK_NUMBER))
1724 error(ERR_NONFATAL,
1725 "`%s' expects a parameter count after `-'",
1726 pp_directives[ct]);
1727 else {
1728 searching.nparam_max = readnum(tline->text, &j);
1729 if (j)
1730 error(ERR_NONFATAL,
1731 "unable to parse parameter count `%s'",
1732 tline->text);
1733 if (searching.nparam_min > searching.nparam_max)
1734 error(ERR_NONFATAL,
1735 "minimum parameter count exceeds maximum");
1736 }
1737 }
1738 if (tline && tok_is_(tline->next, "+")) {
1739 tline = tline->next;
1740 searching.plus = true;
1741 }
1742 mmac = (MMacro *) hash_findix(&mmacros, searching.name);
1743 while (mmac) {
1744 if (!strcmp(mmac->name, searching.name) &&
1745 (mmac->nparam_min <= searching.nparam_max
1746 || searching.plus)
1747 && (searching.nparam_min <= mmac->nparam_max
1748 || mmac->plus)) {
1749 found = true;
1750 break;
1751 }
1752 mmac = mmac->next;
1753 }
1754 if (tline && tline->next)
1755 error(ERR_WARNING|ERR_PASS1,
1756 "trailing garbage after %%ifmacro ignored");
1757 nasm_free(searching.name);
1758 j = found;
1759 break;
H. Peter Anvin89cee572009-07-15 09:16:54 -04001760 }
H. Peter Anvin65747262002-05-07 00:10:05 +00001761
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001762 case PPC_IFID:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001763 needtype = TOK_ID;
1764 goto iftype;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001765 case PPC_IFNUM:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001766 needtype = TOK_NUMBER;
1767 goto iftype;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001768 case PPC_IFSTR:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001769 needtype = TOK_STRING;
1770 goto iftype;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001771
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001772iftype:
1773 t = tline = expand_smacro(tline);
H. Peter Anvind85d2502008-05-04 17:53:31 -07001774
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001775 while (tok_type_(t, TOK_WHITESPACE) ||
1776 (needtype == TOK_NUMBER &&
1777 tok_type_(t, TOK_OTHER) &&
1778 (t->text[0] == '-' || t->text[0] == '+') &&
1779 !t->text[1]))
1780 t = t->next;
H. Peter Anvind85d2502008-05-04 17:53:31 -07001781
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001782 j = tok_type_(t, needtype);
1783 break;
H. Peter Anvincbf768d2008-02-16 16:41:25 -08001784
1785 case PPC_IFTOKEN:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001786 t = tline = expand_smacro(tline);
1787 while (tok_type_(t, TOK_WHITESPACE))
1788 t = t->next;
H. Peter Anvincbf768d2008-02-16 16:41:25 -08001789
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001790 j = false;
1791 if (t) {
1792 t = t->next; /* Skip the actual token */
1793 while (tok_type_(t, TOK_WHITESPACE))
1794 t = t->next;
1795 j = !t; /* Should be nothing left */
1796 }
1797 break;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001798
H. Peter Anvin134b9462008-02-16 17:01:40 -08001799 case PPC_IFEMPTY:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001800 t = tline = expand_smacro(tline);
1801 while (tok_type_(t, TOK_WHITESPACE))
1802 t = t->next;
H. Peter Anvin134b9462008-02-16 17:01:40 -08001803
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001804 j = !t; /* Should be empty */
1805 break;
H. Peter Anvin134b9462008-02-16 17:01:40 -08001806
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001807 case PPC_IF:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001808 t = tline = expand_smacro(tline);
1809 tptr = &t;
1810 tokval.t_type = TOKEN_INVALID;
1811 evalresult = evaluate(ppscan, tptr, &tokval,
1812 NULL, pass | CRITICAL, error, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001813 if (!evalresult)
1814 return -1;
1815 if (tokval.t_type)
H. Peter Anvin917a3492008-09-24 09:14:49 -07001816 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +00001817 "trailing garbage after expression ignored");
1818 if (!is_simple(evalresult)) {
1819 error(ERR_NONFATAL,
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001820 "non-constant value given to `%s'", pp_directives[ct]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001821 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001822 }
Chuck Crayne60ae75d2007-05-02 01:59:16 +00001823 j = reloc_value(evalresult) != 0;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001824 break;
H. Peter Anvin95e28822007-09-12 04:20:08 +00001825
H. Peter Anvine2c80182005-01-15 22:15:51 +00001826 default:
1827 error(ERR_FATAL,
1828 "preprocessor directive `%s' not yet implemented",
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001829 pp_directives[ct]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001830 goto fail;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001831 }
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001832
1833 free_tlist(origline);
1834 return j ^ PP_NEGATIVE(ct);
H. Peter Anvin70653092007-10-19 14:42:29 -07001835
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001836fail:
1837 free_tlist(origline);
1838 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001839}
1840
1841/*
H. Peter Anvin4db5a162007-10-11 13:42:09 -07001842 * Common code for defining an smacro
1843 */
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001844static bool define_smacro(Context *ctx, const char *mname, bool casesense,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001845 int nparam, Token *expansion)
H. Peter Anvin4db5a162007-10-11 13:42:09 -07001846{
1847 SMacro *smac, **smhead;
H. Peter Anvin166c2472008-05-28 12:28:58 -07001848 struct hash_table *smtbl;
H. Peter Anvin70653092007-10-19 14:42:29 -07001849
H. Peter Anvin4db5a162007-10-11 13:42:09 -07001850 if (smacro_defined(ctx, mname, nparam, &smac, casesense)) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001851 if (!smac) {
1852 error(ERR_WARNING|ERR_PASS1,
1853 "single-line macro `%s' defined both with and"
1854 " without parameters", mname);
1855 /*
1856 * Some instances of the old code considered this a failure,
1857 * some others didn't. What is the right thing to do here?
1858 */
1859 free_tlist(expansion);
1860 return false; /* Failure */
1861 } else {
1862 /*
1863 * We're redefining, so we have to take over an
1864 * existing SMacro structure. This means freeing
1865 * what was already in it.
1866 */
1867 nasm_free(smac->name);
1868 free_tlist(smac->expansion);
1869 }
H. Peter Anvin4db5a162007-10-11 13:42:09 -07001870 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001871 smtbl = ctx ? &ctx->localmac : &smacros;
1872 smhead = (SMacro **) hash_findi_add(smtbl, mname);
1873 smac = nasm_malloc(sizeof(SMacro));
1874 smac->next = *smhead;
1875 *smhead = smac;
H. Peter Anvin4db5a162007-10-11 13:42:09 -07001876 }
1877 smac->name = nasm_strdup(mname);
1878 smac->casesense = casesense;
1879 smac->nparam = nparam;
1880 smac->expansion = expansion;
1881 smac->in_progress = false;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001882 return true; /* Success */
H. Peter Anvin4db5a162007-10-11 13:42:09 -07001883}
1884
1885/*
1886 * Undefine an smacro
1887 */
1888static void undef_smacro(Context *ctx, const char *mname)
1889{
1890 SMacro **smhead, *s, **sp;
H. Peter Anvin166c2472008-05-28 12:28:58 -07001891 struct hash_table *smtbl;
H. Peter Anvin4db5a162007-10-11 13:42:09 -07001892
H. Peter Anvin166c2472008-05-28 12:28:58 -07001893 smtbl = ctx ? &ctx->localmac : &smacros;
1894 smhead = (SMacro **)hash_findi(smtbl, mname, NULL);
H. Peter Anvin70653092007-10-19 14:42:29 -07001895
H. Peter Anvin4db5a162007-10-11 13:42:09 -07001896 if (smhead) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001897 /*
1898 * We now have a macro name... go hunt for it.
1899 */
1900 sp = smhead;
1901 while ((s = *sp) != NULL) {
1902 if (!mstrcmp(s->name, mname, s->casesense)) {
1903 *sp = s->next;
1904 nasm_free(s->name);
1905 free_tlist(s->expansion);
1906 nasm_free(s);
1907 } else {
1908 sp = &s->next;
1909 }
1910 }
H. Peter Anvin4db5a162007-10-11 13:42:09 -07001911 }
1912}
1913
H. Peter Anvin8781cb02007-11-08 20:01:11 -08001914/*
H. Peter Anvina26433d2008-07-16 14:40:01 -07001915 * Parse a mmacro specification.
1916 */
1917static bool parse_mmacro_spec(Token *tline, MMacro *def, const char *directive)
1918{
1919 bool err;
1920
1921 tline = tline->next;
1922 skip_white_(tline);
1923 tline = expand_id(tline);
1924 if (!tok_type_(tline, TOK_ID)) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001925 error(ERR_NONFATAL, "`%s' expects a macro name", directive);
1926 return false;
H. Peter Anvina26433d2008-07-16 14:40:01 -07001927 }
Victor van den Elzenb916ede2008-07-23 15:14:22 +02001928
H. Peter Anvin89cee572009-07-15 09:16:54 -04001929 def->prev = NULL;
H. Peter Anvina26433d2008-07-16 14:40:01 -07001930 def->name = nasm_strdup(tline->text);
1931 def->plus = false;
1932 def->nolist = false;
1933 def->in_progress = 0;
1934 def->rep_nest = NULL;
Victor van den Elzenb916ede2008-07-23 15:14:22 +02001935 def->nparam_min = 0;
1936 def->nparam_max = 0;
1937
H. Peter Anvina26433d2008-07-16 14:40:01 -07001938 tline = expand_smacro(tline->next);
1939 skip_white_(tline);
1940 if (!tok_type_(tline, TOK_NUMBER)) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001941 error(ERR_NONFATAL, "`%s' expects a parameter count", directive);
H. Peter Anvina26433d2008-07-16 14:40:01 -07001942 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001943 def->nparam_min = def->nparam_max =
1944 readnum(tline->text, &err);
1945 if (err)
1946 error(ERR_NONFATAL,
1947 "unable to parse parameter count `%s'", tline->text);
H. Peter Anvina26433d2008-07-16 14:40:01 -07001948 }
1949 if (tline && tok_is_(tline->next, "-")) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001950 tline = tline->next->next;
1951 if (tok_is_(tline, "*")) {
1952 def->nparam_max = INT_MAX;
1953 } else if (!tok_type_(tline, TOK_NUMBER)) {
1954 error(ERR_NONFATAL,
1955 "`%s' expects a parameter count after `-'", directive);
1956 } else {
1957 def->nparam_max = readnum(tline->text, &err);
1958 if (err) {
1959 error(ERR_NONFATAL, "unable to parse parameter count `%s'",
1960 tline->text);
1961 }
1962 if (def->nparam_min > def->nparam_max) {
1963 error(ERR_NONFATAL, "minimum parameter count exceeds maximum");
1964 }
1965 }
H. Peter Anvina26433d2008-07-16 14:40:01 -07001966 }
1967 if (tline && tok_is_(tline->next, "+")) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001968 tline = tline->next;
1969 def->plus = true;
H. Peter Anvina26433d2008-07-16 14:40:01 -07001970 }
1971 if (tline && tok_type_(tline->next, TOK_ID) &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001972 !nasm_stricmp(tline->next->text, ".nolist")) {
1973 tline = tline->next;
1974 def->nolist = true;
H. Peter Anvina26433d2008-07-16 14:40:01 -07001975 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001976
H. Peter Anvina26433d2008-07-16 14:40:01 -07001977 /*
1978 * Handle default parameters.
1979 */
1980 if (tline && tline->next) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001981 def->dlist = tline->next;
1982 tline->next = NULL;
1983 count_mmac_params(def->dlist, &def->ndefs, &def->defaults);
H. Peter Anvina26433d2008-07-16 14:40:01 -07001984 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001985 def->dlist = NULL;
1986 def->defaults = NULL;
H. Peter Anvina26433d2008-07-16 14:40:01 -07001987 }
1988 def->expansion = NULL;
1989
H. Peter Anvin89cee572009-07-15 09:16:54 -04001990 if (def->defaults && def->ndefs > def->nparam_max - def->nparam_min &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001991 !def->plus)
1992 error(ERR_WARNING|ERR_PASS1|ERR_WARN_MDP,
1993 "too many default macro parameters");
Victor van den Elzenb916ede2008-07-23 15:14:22 +02001994
H. Peter Anvina26433d2008-07-16 14:40:01 -07001995 return true;
1996}
1997
1998
1999/*
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002000 * Decode a size directive
2001 */
2002static int parse_size(const char *str) {
2003 static const char *size_names[] =
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002004 { "byte", "dword", "oword", "qword", "tword", "word", "yword" };
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002005 static const int sizes[] =
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002006 { 0, 1, 4, 16, 8, 10, 2, 32 };
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002007
Cyrill Gorcunova7319242010-06-03 22:04:36 +04002008 return sizes[bsii(str, size_names, ARRAY_SIZE(size_names))+1];
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002009}
2010
H. Peter Anvinf9c9a672009-07-14 15:14:05 -04002011/*
2012 * nasm_unquote with error if the string contains NUL characters.
2013 * If the string contains NUL characters, issue an error and return
2014 * the C len, i.e. truncate at the NUL.
2015 */
2016static size_t nasm_unquote_cstr(char *qstr, enum preproc_token directive)
2017{
2018 size_t len = nasm_unquote(qstr, NULL);
2019 size_t clen = strlen(qstr);
2020
2021 if (len != clen)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002022 error(ERR_NONFATAL, "NUL character in `%s' directive",
2023 pp_directives[directive]);
H. Peter Anvinf9c9a672009-07-14 15:14:05 -04002024
2025 return clen;
2026}
2027
Ed Beroset3ab3f412002-06-11 03:31:49 +00002028/**
2029 * find and process preprocessor directive in passed line
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002030 * Find out if a line contains a preprocessor directive, and deal
2031 * with it if so.
H. Peter Anvin70653092007-10-19 14:42:29 -07002032 *
Ed Beroset3ab3f412002-06-11 03:31:49 +00002033 * If a directive _is_ found, it is the responsibility of this routine
2034 * (and not the caller) to free_tlist() the line.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002035 *
Ed Beroset3ab3f412002-06-11 03:31:49 +00002036 * @param tline a pointer to the current tokeninzed line linked list
2037 * @return DIRECTIVE_FOUND or NO_DIRECTIVE_FOUND
H. Peter Anvin70653092007-10-19 14:42:29 -07002038 *
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002039 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002040static int do_directive(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00002041{
H. Peter Anvin4169a472007-09-12 01:29:43 +00002042 enum preproc_token i;
2043 int j;
H. Peter Anvin70055962007-10-11 00:05:31 -07002044 bool err;
2045 int nparam;
2046 bool nolist;
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07002047 bool casesense;
H. Peter Anvin8cfdb9d2007-09-14 18:36:01 -07002048 int k, m;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002049 int offset;
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08002050 char *p, *pp;
2051 const char *mname;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002052 Include *inc;
2053 Context *ctx;
2054 Cond *cond;
H. Peter Anvin97a23472007-09-16 17:57:25 -07002055 MMacro *mmac, **mmhead;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002056 Token *t, *tt, *param_start, *macro_start, *last, **tptr, *origline;
2057 Line *l;
2058 struct tokenval tokval;
2059 expr *evalresult;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002060 MMacro *tmp_defining; /* Used when manipulating rep_nest */
H. Peter Anvinf8ba53e2007-10-11 10:11:57 -07002061 int64_t count;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07002062 size_t len;
H. Peter Anvin8e3f75e2008-09-24 00:21:58 -07002063 int severity;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002064
2065 origline = tline;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002066
H. Peter Anvineba20a72002-04-30 20:53:55 +00002067 skip_white_(tline);
H. Peter Anvinf2936d72008-06-04 15:11:23 -07002068 if (!tline || !tok_type_(tline, TOK_PREPROC_ID) ||
H. Peter Anvine2c80182005-01-15 22:15:51 +00002069 (tline->text[1] == '%' || tline->text[1] == '$'
2070 || tline->text[1] == '!'))
2071 return NO_DIRECTIVE_FOUND;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002072
H. Peter Anvin4169a472007-09-12 01:29:43 +00002073 i = pp_token_hash(tline->text);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002074
2075 /*
Cyrill Gorcunovf09116f2010-02-28 11:52:53 +03002076 * FIXME: We zap execution of PP_RMACRO, PP_IRMACRO, PP_EXITMACRO
2077 * since they are known to be buggy at moment, we need to fix them
2078 * in future release (2.09-2.10)
2079 */
2080 if (i == PP_RMACRO || i == PP_RMACRO || i == PP_EXITMACRO) {
2081 error(ERR_NONFATAL, "unknown preprocessor directive `%s'",
2082 tline->text);
2083 return NO_DIRECTIVE_FOUND;
2084 }
2085
2086 /*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002087 * If we're in a non-emitting branch of a condition construct,
H. Peter Anvin76690a12002-04-30 20:52:49 +00002088 * or walking to the end of an already terminated %rep block,
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002089 * we should ignore all directives except for condition
2090 * directives.
2091 */
H. Peter Anvin76690a12002-04-30 20:52:49 +00002092 if (((istk->conds && !emitting(istk->conds->state)) ||
H. Peter Anvine2c80182005-01-15 22:15:51 +00002093 (istk->mstk && !istk->mstk->in_progress)) && !is_condition(i)) {
2094 return NO_DIRECTIVE_FOUND;
H. Peter Anvineba20a72002-04-30 20:53:55 +00002095 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002096
2097 /*
H. Peter Anvin76690a12002-04-30 20:52:49 +00002098 * If we're defining a macro or reading a %rep block, we should
Victor van den Elzen8f1120f2008-07-16 13:41:37 +02002099 * ignore all directives except for %macro/%imacro (which nest),
2100 * %endm/%endmacro, and (only if we're in a %rep block) %endrep.
2101 * If we're in a %rep block, another %rep nests, so should be let through.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002102 */
2103 if (defining && i != PP_MACRO && i != PP_IMACRO &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002104 i != PP_RMACRO && i != PP_IRMACRO &&
H. Peter Anvine2c80182005-01-15 22:15:51 +00002105 i != PP_ENDMACRO && i != PP_ENDM &&
2106 (defining->name || (i != PP_ENDREP && i != PP_REP))) {
2107 return NO_DIRECTIVE_FOUND;
H. Peter Anvineba20a72002-04-30 20:53:55 +00002108 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002109
Charles Crayned4200be2008-07-12 16:42:33 -07002110 if (defining) {
Keith Kanios852f1ee2009-07-12 00:19:55 -05002111 if (i == PP_MACRO || i == PP_IMACRO ||
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002112 i == PP_RMACRO || i == PP_IRMACRO) {
Charles Crayned4200be2008-07-12 16:42:33 -07002113 nested_mac_count++;
2114 return NO_DIRECTIVE_FOUND;
2115 } else if (nested_mac_count > 0) {
2116 if (i == PP_ENDMACRO) {
2117 nested_mac_count--;
2118 return NO_DIRECTIVE_FOUND;
2119 }
2120 }
2121 if (!defining->name) {
2122 if (i == PP_REP) {
2123 nested_rep_count++;
2124 return NO_DIRECTIVE_FOUND;
2125 } else if (nested_rep_count > 0) {
2126 if (i == PP_ENDREP) {
2127 nested_rep_count--;
2128 return NO_DIRECTIVE_FOUND;
2129 }
2130 }
2131 }
2132 }
2133
H. Peter Anvin4169a472007-09-12 01:29:43 +00002134 switch (i) {
2135 case PP_INVALID:
H. Peter Anvine2c80182005-01-15 22:15:51 +00002136 error(ERR_NONFATAL, "unknown preprocessor directive `%s'",
2137 tline->text);
2138 return NO_DIRECTIVE_FOUND; /* didn't get it */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002139
H. Peter Anvine2c80182005-01-15 22:15:51 +00002140 case PP_STACKSIZE:
2141 /* Directive to tell NASM what the default stack size is. The
2142 * default is for a 16-bit stack, and this can be overriden with
2143 * %stacksize large.
H. Peter Anvine2c80182005-01-15 22:15:51 +00002144 */
2145 tline = tline->next;
2146 if (tline && tline->type == TOK_WHITESPACE)
2147 tline = tline->next;
2148 if (!tline || tline->type != TOK_ID) {
2149 error(ERR_NONFATAL, "`%%stacksize' missing size parameter");
2150 free_tlist(origline);
2151 return DIRECTIVE_FOUND;
2152 }
2153 if (nasm_stricmp(tline->text, "flat") == 0) {
2154 /* All subsequent ARG directives are for a 32-bit stack */
2155 StackSize = 4;
2156 StackPointer = "ebp";
2157 ArgOffset = 8;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002158 LocalOffset = 0;
Charles Crayne7eaf9192007-11-08 22:11:14 -08002159 } else if (nasm_stricmp(tline->text, "flat64") == 0) {
2160 /* All subsequent ARG directives are for a 64-bit stack */
2161 StackSize = 8;
2162 StackPointer = "rbp";
Per Jessen53252e02010-02-11 00:16:59 +03002163 ArgOffset = 16;
Charles Crayne7eaf9192007-11-08 22:11:14 -08002164 LocalOffset = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002165 } else if (nasm_stricmp(tline->text, "large") == 0) {
2166 /* All subsequent ARG directives are for a 16-bit stack,
2167 * far function call.
2168 */
2169 StackSize = 2;
2170 StackPointer = "bp";
2171 ArgOffset = 4;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002172 LocalOffset = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002173 } else if (nasm_stricmp(tline->text, "small") == 0) {
2174 /* All subsequent ARG directives are for a 16-bit stack,
2175 * far function call. We don't support near functions.
2176 */
2177 StackSize = 2;
2178 StackPointer = "bp";
2179 ArgOffset = 6;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002180 LocalOffset = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002181 } else {
2182 error(ERR_NONFATAL, "`%%stacksize' invalid size type");
2183 free_tlist(origline);
2184 return DIRECTIVE_FOUND;
2185 }
2186 free_tlist(origline);
2187 return DIRECTIVE_FOUND;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002188
H. Peter Anvine2c80182005-01-15 22:15:51 +00002189 case PP_ARG:
2190 /* TASM like ARG directive to define arguments to functions, in
2191 * the following form:
2192 *
2193 * ARG arg1:WORD, arg2:DWORD, arg4:QWORD
2194 */
2195 offset = ArgOffset;
2196 do {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00002197 char *arg, directive[256];
H. Peter Anvine2c80182005-01-15 22:15:51 +00002198 int size = StackSize;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002199
H. Peter Anvine2c80182005-01-15 22:15:51 +00002200 /* Find the argument name */
2201 tline = tline->next;
2202 if (tline && tline->type == TOK_WHITESPACE)
2203 tline = tline->next;
2204 if (!tline || tline->type != TOK_ID) {
2205 error(ERR_NONFATAL, "`%%arg' missing argument parameter");
2206 free_tlist(origline);
2207 return DIRECTIVE_FOUND;
2208 }
2209 arg = tline->text;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002210
H. Peter Anvine2c80182005-01-15 22:15:51 +00002211 /* Find the argument size type */
2212 tline = tline->next;
2213 if (!tline || tline->type != TOK_OTHER
2214 || tline->text[0] != ':') {
2215 error(ERR_NONFATAL,
2216 "Syntax error processing `%%arg' directive");
2217 free_tlist(origline);
2218 return DIRECTIVE_FOUND;
2219 }
2220 tline = tline->next;
2221 if (!tline || tline->type != TOK_ID) {
2222 error(ERR_NONFATAL, "`%%arg' missing size type parameter");
2223 free_tlist(origline);
2224 return DIRECTIVE_FOUND;
2225 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002226
H. Peter Anvine2c80182005-01-15 22:15:51 +00002227 /* Allow macro expansion of type parameter */
Keith Kaniosb7a89542007-04-12 02:40:54 +00002228 tt = tokenize(tline->text);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002229 tt = expand_smacro(tt);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002230 size = parse_size(tt->text);
2231 if (!size) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002232 error(ERR_NONFATAL,
2233 "Invalid size type for `%%arg' missing directive");
2234 free_tlist(tt);
2235 free_tlist(origline);
2236 return DIRECTIVE_FOUND;
2237 }
2238 free_tlist(tt);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002239
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002240 /* Round up to even stack slots */
2241 size = ALIGN(size, StackSize);
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002242
H. Peter Anvine2c80182005-01-15 22:15:51 +00002243 /* Now define the macro for the argument */
2244 snprintf(directive, sizeof(directive), "%%define %s (%s+%d)",
2245 arg, StackPointer, offset);
Keith Kaniosb7a89542007-04-12 02:40:54 +00002246 do_directive(tokenize(directive));
H. Peter Anvine2c80182005-01-15 22:15:51 +00002247 offset += size;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002248
H. Peter Anvine2c80182005-01-15 22:15:51 +00002249 /* Move to the next argument in the list */
2250 tline = tline->next;
2251 if (tline && tline->type == TOK_WHITESPACE)
2252 tline = tline->next;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002253 } while (tline && tline->type == TOK_OTHER && tline->text[0] == ',');
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002254 ArgOffset = offset;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002255 free_tlist(origline);
2256 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002257
H. Peter Anvine2c80182005-01-15 22:15:51 +00002258 case PP_LOCAL:
2259 /* TASM like LOCAL directive to define local variables for a
2260 * function, in the following form:
2261 *
2262 * LOCAL local1:WORD, local2:DWORD, local4:QWORD = LocalSize
2263 *
2264 * The '= LocalSize' at the end is ignored by NASM, but is
2265 * required by TASM to define the local parameter size (and used
2266 * by the TASM macro package).
2267 */
2268 offset = LocalOffset;
2269 do {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00002270 char *local, directive[256];
H. Peter Anvine2c80182005-01-15 22:15:51 +00002271 int size = StackSize;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002272
H. Peter Anvine2c80182005-01-15 22:15:51 +00002273 /* Find the argument name */
2274 tline = tline->next;
2275 if (tline && tline->type == TOK_WHITESPACE)
2276 tline = tline->next;
2277 if (!tline || tline->type != TOK_ID) {
2278 error(ERR_NONFATAL,
2279 "`%%local' missing argument parameter");
2280 free_tlist(origline);
2281 return DIRECTIVE_FOUND;
2282 }
2283 local = tline->text;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002284
H. Peter Anvine2c80182005-01-15 22:15:51 +00002285 /* Find the argument size type */
2286 tline = tline->next;
2287 if (!tline || tline->type != TOK_OTHER
2288 || tline->text[0] != ':') {
2289 error(ERR_NONFATAL,
2290 "Syntax error processing `%%local' directive");
2291 free_tlist(origline);
2292 return DIRECTIVE_FOUND;
2293 }
2294 tline = tline->next;
2295 if (!tline || tline->type != TOK_ID) {
2296 error(ERR_NONFATAL,
2297 "`%%local' missing size type parameter");
2298 free_tlist(origline);
2299 return DIRECTIVE_FOUND;
2300 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00002301
H. Peter Anvine2c80182005-01-15 22:15:51 +00002302 /* Allow macro expansion of type parameter */
Keith Kaniosb7a89542007-04-12 02:40:54 +00002303 tt = tokenize(tline->text);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002304 tt = expand_smacro(tt);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002305 size = parse_size(tt->text);
2306 if (!size) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002307 error(ERR_NONFATAL,
2308 "Invalid size type for `%%local' missing directive");
2309 free_tlist(tt);
2310 free_tlist(origline);
2311 return DIRECTIVE_FOUND;
2312 }
2313 free_tlist(tt);
H. Peter Anvin734b1882002-04-30 21:01:08 +00002314
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002315 /* Round up to even stack slots */
2316 size = ALIGN(size, StackSize);
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002317
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002318 offset += size; /* Negative offset, increment before */
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002319
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002320 /* Now define the macro for the argument */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002321 snprintf(directive, sizeof(directive), "%%define %s (%s-%d)",
2322 local, StackPointer, offset);
Keith Kaniosb7a89542007-04-12 02:40:54 +00002323 do_directive(tokenize(directive));
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002324
H. Peter Anvine2c80182005-01-15 22:15:51 +00002325 /* Now define the assign to setup the enter_c macro correctly */
2326 snprintf(directive, sizeof(directive),
2327 "%%assign %%$localsize %%$localsize+%d", size);
Keith Kaniosb7a89542007-04-12 02:40:54 +00002328 do_directive(tokenize(directive));
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002329
H. Peter Anvine2c80182005-01-15 22:15:51 +00002330 /* Move to the next argument in the list */
2331 tline = tline->next;
2332 if (tline && tline->type == TOK_WHITESPACE)
2333 tline = tline->next;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002334 } while (tline && tline->type == TOK_OTHER && tline->text[0] == ',');
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002335 LocalOffset = offset;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002336 free_tlist(origline);
2337 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002338
H. Peter Anvine2c80182005-01-15 22:15:51 +00002339 case PP_CLEAR:
2340 if (tline->next)
H. Peter Anvin917a3492008-09-24 09:14:49 -07002341 error(ERR_WARNING|ERR_PASS1,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002342 "trailing garbage after `%%clear' ignored");
2343 free_macros();
2344 init_macros();
H. Peter Anvine2c80182005-01-15 22:15:51 +00002345 free_tlist(origline);
2346 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002347
H. Peter Anvin418ca702008-05-30 10:42:30 -07002348 case PP_DEPEND:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002349 t = tline->next = expand_smacro(tline->next);
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002350 skip_white_(t);
2351 if (!t || (t->type != TOK_STRING &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002352 t->type != TOK_INTERNAL_STRING)) {
H. Peter Anvin418ca702008-05-30 10:42:30 -07002353 error(ERR_NONFATAL, "`%%depend' expects a file name");
2354 free_tlist(origline);
2355 return DIRECTIVE_FOUND; /* but we did _something_ */
2356 }
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002357 if (t->next)
H. Peter Anvin917a3492008-09-24 09:14:49 -07002358 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvin418ca702008-05-30 10:42:30 -07002359 "trailing garbage after `%%depend' ignored");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002360 p = t->text;
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002361 if (t->type != TOK_INTERNAL_STRING)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002362 nasm_unquote_cstr(p, i);
2363 if (dephead && !in_list(*dephead, p)) {
2364 StrList *sl = nasm_malloc(strlen(p)+1+sizeof sl->next);
2365 sl->next = NULL;
2366 strcpy(sl->str, p);
2367 *deptail = sl;
2368 deptail = &sl->next;
2369 }
2370 free_tlist(origline);
H. Peter Anvin418ca702008-05-30 10:42:30 -07002371 return DIRECTIVE_FOUND;
2372
2373 case PP_INCLUDE:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002374 t = tline->next = expand_smacro(tline->next);
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002375 skip_white_(t);
H. Peter Anvind2456592008-06-19 15:04:18 -07002376
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002377 if (!t || (t->type != TOK_STRING &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002378 t->type != TOK_INTERNAL_STRING)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002379 error(ERR_NONFATAL, "`%%include' expects a file name");
2380 free_tlist(origline);
2381 return DIRECTIVE_FOUND; /* but we did _something_ */
2382 }
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002383 if (t->next)
H. Peter Anvin917a3492008-09-24 09:14:49 -07002384 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +00002385 "trailing garbage after `%%include' ignored");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002386 p = t->text;
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002387 if (t->type != TOK_INTERNAL_STRING)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002388 nasm_unquote_cstr(p, i);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002389 inc = nasm_malloc(sizeof(Include));
2390 inc->next = istk;
2391 inc->conds = NULL;
H. Peter Anvin2b1c3b92008-06-06 10:38:46 -07002392 inc->fp = inc_fopen(p, dephead, &deptail, pass == 0);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002393 if (!inc->fp) {
2394 /* -MG given but file not found */
2395 nasm_free(inc);
2396 } else {
2397 inc->fname = src_set_fname(nasm_strdup(p));
2398 inc->lineno = src_set_linnum(0);
2399 inc->lineinc = 1;
2400 inc->expansion = NULL;
2401 inc->mstk = NULL;
2402 istk = inc;
2403 list->uplevel(LIST_INCLUDE);
2404 }
2405 free_tlist(origline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002406 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002407
H. Peter Anvind2456592008-06-19 15:04:18 -07002408 case PP_USE:
H. Peter Anvinf4ae5ad2008-06-19 18:39:24 -07002409 {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002410 static macros_t *use_pkg;
2411 const char *pkg_macro = NULL;
H. Peter Anvinf4ae5ad2008-06-19 18:39:24 -07002412
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002413 tline = tline->next;
2414 skip_white_(tline);
2415 tline = expand_id(tline);
H. Peter Anvind2456592008-06-19 15:04:18 -07002416
H. Peter Anvin264b7b92008-10-24 16:38:17 -07002417 if (!tline || (tline->type != TOK_STRING &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002418 tline->type != TOK_INTERNAL_STRING &&
2419 tline->type != TOK_ID)) {
H. Peter Anvin926fc402008-06-19 16:26:12 -07002420 error(ERR_NONFATAL, "`%%use' expects a package name");
H. Peter Anvind2456592008-06-19 15:04:18 -07002421 free_tlist(origline);
2422 return DIRECTIVE_FOUND; /* but we did _something_ */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002423 }
H. Peter Anvin264b7b92008-10-24 16:38:17 -07002424 if (tline->next)
H. Peter Anvin917a3492008-09-24 09:14:49 -07002425 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvind2456592008-06-19 15:04:18 -07002426 "trailing garbage after `%%use' ignored");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002427 if (tline->type == TOK_STRING)
2428 nasm_unquote_cstr(tline->text, i);
2429 use_pkg = nasm_stdmac_find_package(tline->text);
2430 if (!use_pkg)
2431 error(ERR_NONFATAL, "unknown `%%use' package: %s", tline->text);
2432 else
2433 pkg_macro = (char *)use_pkg + 1; /* The first string will be <%define>__USE_*__ */
Victor van den Elzen35eb2ea2010-03-10 22:33:48 +01002434 if (use_pkg && ! smacro_defined(NULL, pkg_macro, 0, NULL, true)) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002435 /* Not already included, go ahead and include it */
2436 stdmacpos = use_pkg;
2437 }
2438 free_tlist(origline);
H. Peter Anvind2456592008-06-19 15:04:18 -07002439 return DIRECTIVE_FOUND;
H. Peter Anvinf4ae5ad2008-06-19 18:39:24 -07002440 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002441 case PP_PUSH:
H. Peter Anvine2c80182005-01-15 22:15:51 +00002442 case PP_REPL:
H. Peter Anvin42b56392008-10-24 16:24:21 -07002443 case PP_POP:
H. Peter Anvine2c80182005-01-15 22:15:51 +00002444 tline = tline->next;
2445 skip_white_(tline);
2446 tline = expand_id(tline);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002447 if (tline) {
2448 if (!tok_type_(tline, TOK_ID)) {
2449 error(ERR_NONFATAL, "`%s' expects a context identifier",
2450 pp_directives[i]);
2451 free_tlist(origline);
2452 return DIRECTIVE_FOUND; /* but we did _something_ */
2453 }
2454 if (tline->next)
2455 error(ERR_WARNING|ERR_PASS1,
2456 "trailing garbage after `%s' ignored",
2457 pp_directives[i]);
2458 p = nasm_strdup(tline->text);
2459 } else {
2460 p = NULL; /* Anonymous */
2461 }
H. Peter Anvin42b56392008-10-24 16:24:21 -07002462
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002463 if (i == PP_PUSH) {
2464 ctx = nasm_malloc(sizeof(Context));
2465 ctx->next = cstk;
2466 hash_init(&ctx->localmac, HASH_SMALL);
2467 ctx->name = p;
2468 ctx->number = unique++;
2469 cstk = ctx;
2470 } else {
2471 /* %pop or %repl */
2472 if (!cstk) {
2473 error(ERR_NONFATAL, "`%s': context stack is empty",
2474 pp_directives[i]);
2475 } else if (i == PP_POP) {
2476 if (p && (!cstk->name || nasm_stricmp(p, cstk->name)))
2477 error(ERR_NONFATAL, "`%%pop' in wrong context: %s, "
2478 "expected %s",
2479 cstk->name ? cstk->name : "anonymous", p);
2480 else
2481 ctx_pop();
2482 } else {
2483 /* i == PP_REPL */
2484 nasm_free(cstk->name);
2485 cstk->name = p;
2486 p = NULL;
2487 }
2488 nasm_free(p);
2489 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002490 free_tlist(origline);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002491 return DIRECTIVE_FOUND;
H. Peter Anvin8e3f75e2008-09-24 00:21:58 -07002492 case PP_FATAL:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002493 severity = ERR_FATAL;
2494 goto issue_error;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002495 case PP_ERROR:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002496 severity = ERR_NONFATAL;
2497 goto issue_error;
H. Peter Anvin7df04172008-06-10 18:27:38 -07002498 case PP_WARNING:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002499 severity = ERR_WARNING|ERR_WARN_USER;
2500 goto issue_error;
H. Peter Anvin8e3f75e2008-09-24 00:21:58 -07002501
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002502issue_error:
H. Peter Anvin7df04172008-06-10 18:27:38 -07002503 {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002504 /* Only error out if this is the final pass */
2505 if (pass != 2 && i != PP_FATAL)
2506 return DIRECTIVE_FOUND;
2507
2508 tline->next = expand_smacro(tline->next);
2509 tline = tline->next;
2510 skip_white_(tline);
2511 t = tline ? tline->next : NULL;
2512 skip_white_(t);
2513 if (tok_type_(tline, TOK_STRING) && !t) {
2514 /* The line contains only a quoted string */
2515 p = tline->text;
2516 nasm_unquote(p, NULL); /* Ignore NUL character truncation */
2517 error(severity, "%s", p);
2518 } else {
2519 /* Not a quoted string, or more than a quoted string */
2520 p = detoken(tline, false);
2521 error(severity, "%s", p);
2522 nasm_free(p);
2523 }
2524 free_tlist(origline);
2525 return DIRECTIVE_FOUND;
H. Peter Anvin7df04172008-06-10 18:27:38 -07002526 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00002527
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002528 CASE_PP_IF:
H. Peter Anvine2c80182005-01-15 22:15:51 +00002529 if (istk->conds && !emitting(istk->conds->state))
2530 j = COND_NEVER;
2531 else {
2532 j = if_condition(tline->next, i);
2533 tline->next = NULL; /* it got freed */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002534 j = j < 0 ? COND_NEVER : j ? COND_IF_TRUE : COND_IF_FALSE;
2535 }
2536 cond = nasm_malloc(sizeof(Cond));
2537 cond->next = istk->conds;
2538 cond->state = j;
2539 istk->conds = cond;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002540 if(istk->mstk)
Keith Kanios3c0d91f2009-10-25 13:28:03 -05002541 istk->mstk->condcnt ++;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002542 free_tlist(origline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002543 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002544
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002545 CASE_PP_ELIF:
H. Peter Anvine2c80182005-01-15 22:15:51 +00002546 if (!istk->conds)
H. Peter Anvin4169a472007-09-12 01:29:43 +00002547 error(ERR_FATAL, "`%s': no matching `%%if'", pp_directives[i]);
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002548 switch(istk->conds->state) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002549 case COND_IF_TRUE:
2550 istk->conds->state = COND_DONE;
2551 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002552
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002553 case COND_DONE:
2554 case COND_NEVER:
2555 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002556
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002557 case COND_ELSE_TRUE:
2558 case COND_ELSE_FALSE:
2559 error_precond(ERR_WARNING|ERR_PASS1,
2560 "`%%elif' after `%%else' ignored");
2561 istk->conds->state = COND_NEVER;
2562 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002563
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002564 case COND_IF_FALSE:
2565 /*
2566 * IMPORTANT: In the case of %if, we will already have
2567 * called expand_mmac_params(); however, if we're
2568 * processing an %elif we must have been in a
2569 * non-emitting mode, which would have inhibited
2570 * the normal invocation of expand_mmac_params().
2571 * Therefore, we have to do it explicitly here.
2572 */
2573 j = if_condition(expand_mmac_params(tline->next), i);
2574 tline->next = NULL; /* it got freed */
2575 istk->conds->state =
2576 j < 0 ? COND_NEVER : j ? COND_IF_TRUE : COND_IF_FALSE;
2577 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002578 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002579 free_tlist(origline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002580 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002581
H. Peter Anvine2c80182005-01-15 22:15:51 +00002582 case PP_ELSE:
2583 if (tline->next)
H. Peter Anvin917a3492008-09-24 09:14:49 -07002584 error_precond(ERR_WARNING|ERR_PASS1,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002585 "trailing garbage after `%%else' ignored");
H. Peter Anvine2c80182005-01-15 22:15:51 +00002586 if (!istk->conds)
2587 error(ERR_FATAL, "`%%else': no matching `%%if'");
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002588 switch(istk->conds->state) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002589 case COND_IF_TRUE:
2590 case COND_DONE:
2591 istk->conds->state = COND_ELSE_FALSE;
2592 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002593
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002594 case COND_NEVER:
2595 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002596
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002597 case COND_IF_FALSE:
2598 istk->conds->state = COND_ELSE_TRUE;
2599 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002600
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002601 case COND_ELSE_TRUE:
2602 case COND_ELSE_FALSE:
2603 error_precond(ERR_WARNING|ERR_PASS1,
2604 "`%%else' after `%%else' ignored.");
2605 istk->conds->state = COND_NEVER;
2606 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002607 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002608 free_tlist(origline);
2609 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002610
H. Peter Anvine2c80182005-01-15 22:15:51 +00002611 case PP_ENDIF:
2612 if (tline->next)
H. Peter Anvin917a3492008-09-24 09:14:49 -07002613 error_precond(ERR_WARNING|ERR_PASS1,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002614 "trailing garbage after `%%endif' ignored");
H. Peter Anvine2c80182005-01-15 22:15:51 +00002615 if (!istk->conds)
2616 error(ERR_FATAL, "`%%endif': no matching `%%if'");
2617 cond = istk->conds;
2618 istk->conds = cond->next;
2619 nasm_free(cond);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002620 if(istk->mstk)
Keith Kanios3c0d91f2009-10-25 13:28:03 -05002621 istk->mstk->condcnt --;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002622 free_tlist(origline);
2623 return DIRECTIVE_FOUND;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002624
H. Peter Anvindb8f96e2009-07-15 09:07:29 -04002625 case PP_RMACRO:
2626 case PP_IRMACRO:
H. Peter Anvine2c80182005-01-15 22:15:51 +00002627 case PP_MACRO:
2628 case PP_IMACRO:
H. Peter Anvina26433d2008-07-16 14:40:01 -07002629 if (defining) {
H. Peter Anvindb8f96e2009-07-15 09:07:29 -04002630 error(ERR_FATAL, "`%s': already defining a macro",
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002631 pp_directives[i]);
2632 return DIRECTIVE_FOUND;
2633 }
2634 defining = nasm_malloc(sizeof(MMacro));
2635 defining->max_depth =
2636 (i == PP_RMACRO) || (i == PP_IRMACRO) ? DEADMAN_LIMIT : 0;
2637 defining->casesense = (i == PP_MACRO) || (i == PP_RMACRO);
2638 if (!parse_mmacro_spec(tline, defining, pp_directives[i])) {
2639 nasm_free(defining);
2640 defining = NULL;
2641 return DIRECTIVE_FOUND;
2642 }
H. Peter Anvina26433d2008-07-16 14:40:01 -07002643
H. Peter Anvin166c2472008-05-28 12:28:58 -07002644 mmac = (MMacro *) hash_findix(&mmacros, defining->name);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002645 while (mmac) {
2646 if (!strcmp(mmac->name, defining->name) &&
2647 (mmac->nparam_min <= defining->nparam_max
2648 || defining->plus)
2649 && (defining->nparam_min <= mmac->nparam_max
2650 || mmac->plus)) {
H. Peter Anvin917a3492008-09-24 09:14:49 -07002651 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +00002652 "redefining multi-line macro `%s'", defining->name);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002653 return DIRECTIVE_FOUND;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002654 }
2655 mmac = mmac->next;
2656 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002657 free_tlist(origline);
2658 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002659
H. Peter Anvine2c80182005-01-15 22:15:51 +00002660 case PP_ENDM:
2661 case PP_ENDMACRO:
Victor van den Elzen8f1120f2008-07-16 13:41:37 +02002662 if (! (defining && defining->name)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002663 error(ERR_NONFATAL, "`%s': not defining a macro", tline->text);
2664 return DIRECTIVE_FOUND;
2665 }
Keith Kanios852f1ee2009-07-12 00:19:55 -05002666 mmhead = (MMacro **) hash_findi_add(&mmacros, defining->name);
H. Peter Anvin97a23472007-09-16 17:57:25 -07002667 defining->next = *mmhead;
Keith Kanios852f1ee2009-07-12 00:19:55 -05002668 *mmhead = defining;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002669 defining = NULL;
2670 free_tlist(origline);
2671 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002672
H. Peter Anvin89cee572009-07-15 09:16:54 -04002673 case PP_EXITMACRO:
Keith Kanios852f1ee2009-07-12 00:19:55 -05002674 /*
2675 * We must search along istk->expansion until we hit a
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002676 * macro-end marker for a macro with a name. Then we
Keith Kanios3c0d91f2009-10-25 13:28:03 -05002677 * bypass all lines between exitmacro and endmacro.
Keith Kanios852f1ee2009-07-12 00:19:55 -05002678 */
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04002679 list_for_each(l, istk->expansion)
Keith Kanios852f1ee2009-07-12 00:19:55 -05002680 if (l->finishes && l->finishes->name)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002681 break;
Keith Kanios852f1ee2009-07-12 00:19:55 -05002682
2683 if (l) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002684 /*
2685 * Remove all conditional entries relative to this
2686 * macro invocation. (safe to do in this context)
2687 */
Keith Kanios3c0d91f2009-10-25 13:28:03 -05002688 for ( ; l->finishes->condcnt > 0; l->finishes->condcnt --) {
2689 cond = istk->conds;
2690 istk->conds = cond->next;
2691 nasm_free(cond);
2692 }
2693 istk->expansion = l;
Keith Kanios852f1ee2009-07-12 00:19:55 -05002694 } else {
2695 error(ERR_NONFATAL, "`%%exitmacro' not within `%%macro' block");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002696 }
Keith Kanios852f1ee2009-07-12 00:19:55 -05002697 free_tlist(origline);
2698 return DIRECTIVE_FOUND;
2699
H. Peter Anvina26433d2008-07-16 14:40:01 -07002700 case PP_UNMACRO:
2701 case PP_UNIMACRO:
2702 {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002703 MMacro **mmac_p;
2704 MMacro spec;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002705
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002706 spec.casesense = (i == PP_UNMACRO);
2707 if (!parse_mmacro_spec(tline, &spec, pp_directives[i])) {
2708 return DIRECTIVE_FOUND;
2709 }
H. Peter Anvina26433d2008-07-16 14:40:01 -07002710 mmac_p = (MMacro **) hash_findi(&mmacros, spec.name, NULL);
2711 while (mmac_p && *mmac_p) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002712 mmac = *mmac_p;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002713 if (mmac->casesense == spec.casesense &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002714 !mstrcmp(mmac->name, spec.name, spec.casesense) &&
H. Peter Anvina26433d2008-07-16 14:40:01 -07002715 mmac->nparam_min == spec.nparam_min &&
2716 mmac->nparam_max == spec.nparam_max &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002717 mmac->plus == spec.plus) {
2718 *mmac_p = mmac->next;
2719 free_mmacro(mmac);
2720 } else {
2721 mmac_p = &mmac->next;
2722 }
2723 }
2724 free_tlist(origline);
2725 free_tlist(spec.dlist);
2726 return DIRECTIVE_FOUND;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002727 }
2728
H. Peter Anvine2c80182005-01-15 22:15:51 +00002729 case PP_ROTATE:
2730 if (tline->next && tline->next->type == TOK_WHITESPACE)
2731 tline = tline->next;
H. Peter Anvin89cee572009-07-15 09:16:54 -04002732 if (!tline->next) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002733 free_tlist(origline);
2734 error(ERR_NONFATAL, "`%%rotate' missing rotate count");
2735 return DIRECTIVE_FOUND;
2736 }
2737 t = expand_smacro(tline->next);
2738 tline->next = NULL;
2739 free_tlist(origline);
2740 tline = t;
2741 tptr = &t;
2742 tokval.t_type = TOKEN_INVALID;
2743 evalresult =
2744 evaluate(ppscan, tptr, &tokval, NULL, pass, error, NULL);
2745 free_tlist(tline);
2746 if (!evalresult)
2747 return DIRECTIVE_FOUND;
2748 if (tokval.t_type)
H. Peter Anvin917a3492008-09-24 09:14:49 -07002749 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +00002750 "trailing garbage after expression ignored");
2751 if (!is_simple(evalresult)) {
2752 error(ERR_NONFATAL, "non-constant value given to `%%rotate'");
2753 return DIRECTIVE_FOUND;
2754 }
2755 mmac = istk->mstk;
2756 while (mmac && !mmac->name) /* avoid mistaking %reps for macros */
2757 mmac = mmac->next_active;
2758 if (!mmac) {
2759 error(ERR_NONFATAL, "`%%rotate' invoked outside a macro call");
2760 } else if (mmac->nparam == 0) {
2761 error(ERR_NONFATAL,
2762 "`%%rotate' invoked within macro without parameters");
2763 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002764 int rotate = mmac->rotate + reloc_value(evalresult);
H. Peter Anvin734b1882002-04-30 21:01:08 +00002765
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002766 rotate %= (int)mmac->nparam;
2767 if (rotate < 0)
2768 rotate += mmac->nparam;
H. Peter Anvin70653092007-10-19 14:42:29 -07002769
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002770 mmac->rotate = rotate;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002771 }
2772 return DIRECTIVE_FOUND;
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00002773
H. Peter Anvine2c80182005-01-15 22:15:51 +00002774 case PP_REP:
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002775 nolist = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002776 do {
2777 tline = tline->next;
2778 } while (tok_type_(tline, TOK_WHITESPACE));
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00002779
H. Peter Anvine2c80182005-01-15 22:15:51 +00002780 if (tok_type_(tline, TOK_ID) &&
2781 nasm_stricmp(tline->text, ".nolist") == 0) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002782 nolist = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002783 do {
2784 tline = tline->next;
2785 } while (tok_type_(tline, TOK_WHITESPACE));
2786 }
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00002787
H. Peter Anvine2c80182005-01-15 22:15:51 +00002788 if (tline) {
2789 t = expand_smacro(tline);
2790 tptr = &t;
2791 tokval.t_type = TOKEN_INVALID;
2792 evalresult =
2793 evaluate(ppscan, tptr, &tokval, NULL, pass, error, NULL);
2794 if (!evalresult) {
2795 free_tlist(origline);
2796 return DIRECTIVE_FOUND;
2797 }
2798 if (tokval.t_type)
H. Peter Anvin917a3492008-09-24 09:14:49 -07002799 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +00002800 "trailing garbage after expression ignored");
2801 if (!is_simple(evalresult)) {
2802 error(ERR_NONFATAL, "non-constant value given to `%%rep'");
2803 return DIRECTIVE_FOUND;
2804 }
H. Peter Anvinf8ba53e2007-10-11 10:11:57 -07002805 count = reloc_value(evalresult) + 1;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002806 } else {
2807 error(ERR_NONFATAL, "`%%rep' expects a repeat count");
H. Peter Anvinf8ba53e2007-10-11 10:11:57 -07002808 count = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002809 }
2810 free_tlist(origline);
H. Peter Anvin734b1882002-04-30 21:01:08 +00002811
H. Peter Anvine2c80182005-01-15 22:15:51 +00002812 tmp_defining = defining;
2813 defining = nasm_malloc(sizeof(MMacro));
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002814 defining->prev = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002815 defining->name = NULL; /* flags this macro as a %rep block */
H. Peter Anvin70055962007-10-11 00:05:31 -07002816 defining->casesense = false;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002817 defining->plus = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002818 defining->nolist = nolist;
H. Peter Anvinf8ba53e2007-10-11 10:11:57 -07002819 defining->in_progress = count;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002820 defining->max_depth = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002821 defining->nparam_min = defining->nparam_max = 0;
2822 defining->defaults = NULL;
2823 defining->dlist = NULL;
2824 defining->expansion = NULL;
2825 defining->next_active = istk->mstk;
2826 defining->rep_nest = tmp_defining;
2827 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002828
H. Peter Anvine2c80182005-01-15 22:15:51 +00002829 case PP_ENDREP:
2830 if (!defining || defining->name) {
2831 error(ERR_NONFATAL, "`%%endrep': no matching `%%rep'");
2832 return DIRECTIVE_FOUND;
2833 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00002834
H. Peter Anvine2c80182005-01-15 22:15:51 +00002835 /*
2836 * Now we have a "macro" defined - although it has no name
2837 * and we won't be entering it in the hash tables - we must
2838 * push a macro-end marker for it on to istk->expansion.
2839 * After that, it will take care of propagating itself (a
2840 * macro-end marker line for a macro which is really a %rep
2841 * block will cause the macro to be re-expanded, complete
2842 * with another macro-end marker to ensure the process
2843 * continues) until the whole expansion is forcibly removed
2844 * from istk->expansion by a %exitrep.
2845 */
2846 l = nasm_malloc(sizeof(Line));
2847 l->next = istk->expansion;
2848 l->finishes = defining;
2849 l->first = NULL;
2850 istk->expansion = l;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002851
H. Peter Anvine2c80182005-01-15 22:15:51 +00002852 istk->mstk = defining;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002853
H. Peter Anvine2c80182005-01-15 22:15:51 +00002854 list->uplevel(defining->nolist ? LIST_MACRO_NOLIST : LIST_MACRO);
2855 tmp_defining = defining;
2856 defining = defining->rep_nest;
2857 free_tlist(origline);
2858 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002859
H. Peter Anvine2c80182005-01-15 22:15:51 +00002860 case PP_EXITREP:
2861 /*
2862 * We must search along istk->expansion until we hit a
2863 * macro-end marker for a macro with no name. Then we set
2864 * its `in_progress' flag to 0.
2865 */
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04002866 list_for_each(l, istk->expansion)
H. Peter Anvine2c80182005-01-15 22:15:51 +00002867 if (l->finishes && !l->finishes->name)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002868 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002869
H. Peter Anvine2c80182005-01-15 22:15:51 +00002870 if (l)
Charles Crayned4200be2008-07-12 16:42:33 -07002871 l->finishes->in_progress = 1;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002872 else
2873 error(ERR_NONFATAL, "`%%exitrep' not within `%%rep' block");
2874 free_tlist(origline);
2875 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002876
H. Peter Anvine2c80182005-01-15 22:15:51 +00002877 case PP_XDEFINE:
2878 case PP_IXDEFINE:
2879 case PP_DEFINE:
2880 case PP_IDEFINE:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002881 casesense = (i == PP_DEFINE || i == PP_XDEFINE);
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07002882
H. Peter Anvine2c80182005-01-15 22:15:51 +00002883 tline = tline->next;
2884 skip_white_(tline);
2885 tline = expand_id(tline);
2886 if (!tline || (tline->type != TOK_ID &&
2887 (tline->type != TOK_PREPROC_ID ||
2888 tline->text[1] != '$'))) {
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07002889 error(ERR_NONFATAL, "`%s' expects a macro identifier",
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002890 pp_directives[i]);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002891 free_tlist(origline);
2892 return DIRECTIVE_FOUND;
2893 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00002894
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08002895 ctx = get_ctx(tline->text, &mname, false);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002896 last = tline;
2897 param_start = tline = tline->next;
2898 nparam = 0;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002899
H. Peter Anvine2c80182005-01-15 22:15:51 +00002900 /* Expand the macro definition now for %xdefine and %ixdefine */
2901 if ((i == PP_XDEFINE) || (i == PP_IXDEFINE))
2902 tline = expand_smacro(tline);
H. Peter Anvin734b1882002-04-30 21:01:08 +00002903
H. Peter Anvine2c80182005-01-15 22:15:51 +00002904 if (tok_is_(tline, "(")) {
2905 /*
2906 * This macro has parameters.
2907 */
H. Peter Anvin734b1882002-04-30 21:01:08 +00002908
H. Peter Anvine2c80182005-01-15 22:15:51 +00002909 tline = tline->next;
2910 while (1) {
2911 skip_white_(tline);
2912 if (!tline) {
2913 error(ERR_NONFATAL, "parameter identifier expected");
2914 free_tlist(origline);
2915 return DIRECTIVE_FOUND;
2916 }
2917 if (tline->type != TOK_ID) {
2918 error(ERR_NONFATAL,
2919 "`%s': parameter identifier expected",
2920 tline->text);
2921 free_tlist(origline);
2922 return DIRECTIVE_FOUND;
2923 }
2924 tline->type = TOK_SMAC_PARAM + nparam++;
2925 tline = tline->next;
2926 skip_white_(tline);
2927 if (tok_is_(tline, ",")) {
2928 tline = tline->next;
H. Peter Anvinca348b62008-07-23 10:49:26 -04002929 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002930 if (!tok_is_(tline, ")")) {
2931 error(ERR_NONFATAL,
2932 "`)' expected to terminate macro template");
2933 free_tlist(origline);
2934 return DIRECTIVE_FOUND;
2935 }
2936 break;
2937 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002938 }
2939 last = tline;
2940 tline = tline->next;
2941 }
2942 if (tok_type_(tline, TOK_WHITESPACE))
2943 last = tline, tline = tline->next;
2944 macro_start = NULL;
2945 last->next = NULL;
2946 t = tline;
2947 while (t) {
2948 if (t->type == TOK_ID) {
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04002949 list_for_each(tt, param_start)
H. Peter Anvine2c80182005-01-15 22:15:51 +00002950 if (tt->type >= TOK_SMAC_PARAM &&
2951 !strcmp(tt->text, t->text))
2952 t->type = tt->type;
2953 }
2954 tt = t->next;
2955 t->next = macro_start;
2956 macro_start = t;
2957 t = tt;
2958 }
2959 /*
2960 * Good. We now have a macro name, a parameter count, and a
2961 * token list (in reverse order) for an expansion. We ought
2962 * to be OK just to create an SMacro, store it, and let
2963 * free_tlist have the rest of the line (which we have
2964 * carefully re-terminated after chopping off the expansion
2965 * from the end).
2966 */
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002967 define_smacro(ctx, mname, casesense, nparam, macro_start);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002968 free_tlist(origline);
2969 return DIRECTIVE_FOUND;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002970
H. Peter Anvine2c80182005-01-15 22:15:51 +00002971 case PP_UNDEF:
2972 tline = tline->next;
2973 skip_white_(tline);
2974 tline = expand_id(tline);
2975 if (!tline || (tline->type != TOK_ID &&
2976 (tline->type != TOK_PREPROC_ID ||
2977 tline->text[1] != '$'))) {
2978 error(ERR_NONFATAL, "`%%undef' expects a macro identifier");
2979 free_tlist(origline);
2980 return DIRECTIVE_FOUND;
2981 }
2982 if (tline->next) {
H. Peter Anvin917a3492008-09-24 09:14:49 -07002983 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +00002984 "trailing garbage after macro name ignored");
2985 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002986
H. Peter Anvine2c80182005-01-15 22:15:51 +00002987 /* Find the context that symbol belongs to */
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08002988 ctx = get_ctx(tline->text, &mname, false);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002989 undef_smacro(ctx, mname);
2990 free_tlist(origline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002991 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002992
H. Peter Anvin9e200162008-06-04 17:23:14 -07002993 case PP_DEFSTR:
2994 case PP_IDEFSTR:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002995 casesense = (i == PP_DEFSTR);
H. Peter Anvin9e200162008-06-04 17:23:14 -07002996
2997 tline = tline->next;
2998 skip_white_(tline);
2999 tline = expand_id(tline);
3000 if (!tline || (tline->type != TOK_ID &&
3001 (tline->type != TOK_PREPROC_ID ||
3002 tline->text[1] != '$'))) {
3003 error(ERR_NONFATAL, "`%s' expects a macro identifier",
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003004 pp_directives[i]);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003005 free_tlist(origline);
3006 return DIRECTIVE_FOUND;
3007 }
3008
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08003009 ctx = get_ctx(tline->text, &mname, false);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003010 last = tline;
3011 tline = expand_smacro(tline->next);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003012 last->next = NULL;
H. Peter Anvin9e200162008-06-04 17:23:14 -07003013
3014 while (tok_type_(tline, TOK_WHITESPACE))
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003015 tline = delete_Token(tline);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003016
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003017 p = detoken(tline, false);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003018 macro_start = nasm_malloc(sizeof(*macro_start));
3019 macro_start->next = NULL;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003020 macro_start->text = nasm_quote(p, strlen(p));
3021 macro_start->type = TOK_STRING;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003022 macro_start->a.mac = NULL;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003023 nasm_free(p);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003024
3025 /*
3026 * We now have a macro name, an implicit parameter count of
3027 * zero, and a string token to use as an expansion. Create
3028 * and store an SMacro.
3029 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003030 define_smacro(ctx, mname, casesense, 0, macro_start);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003031 free_tlist(origline);
3032 return DIRECTIVE_FOUND;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003033
H. Peter Anvin2f55bda2009-07-14 15:04:04 -04003034 case PP_DEFTOK:
3035 case PP_IDEFTOK:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003036 casesense = (i == PP_DEFTOK);
3037
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003038 tline = tline->next;
3039 skip_white_(tline);
3040 tline = expand_id(tline);
3041 if (!tline || (tline->type != TOK_ID &&
3042 (tline->type != TOK_PREPROC_ID ||
3043 tline->text[1] != '$'))) {
3044 error(ERR_NONFATAL,
3045 "`%s' expects a macro identifier as first parameter",
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003046 pp_directives[i]);
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003047 free_tlist(origline);
3048 return DIRECTIVE_FOUND;
3049 }
3050 ctx = get_ctx(tline->text, &mname, false);
3051 last = tline;
3052 tline = expand_smacro(tline->next);
3053 last->next = NULL;
3054
3055 t = tline;
3056 while (tok_type_(t, TOK_WHITESPACE))
3057 t = t->next;
3058 /* t should now point to the string */
3059 if (t->type != TOK_STRING) {
3060 error(ERR_NONFATAL,
3061 "`%s` requires string as second parameter",
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003062 pp_directives[i]);
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003063 free_tlist(tline);
3064 free_tlist(origline);
3065 return DIRECTIVE_FOUND;
3066 }
3067
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003068 nasm_unquote_cstr(t->text, i);
3069 macro_start = tokenize(t->text);
3070
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003071 /*
3072 * We now have a macro name, an implicit parameter count of
3073 * zero, and a numeric token to use as an expansion. Create
3074 * and store an SMacro.
3075 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003076 define_smacro(ctx, mname, casesense, 0, macro_start);
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003077 free_tlist(tline);
3078 free_tlist(origline);
3079 return DIRECTIVE_FOUND;
H. Peter Anvin9e200162008-06-04 17:23:14 -07003080
H. Peter Anvin418ca702008-05-30 10:42:30 -07003081 case PP_PATHSEARCH:
3082 {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003083 FILE *fp;
3084 StrList *xsl = NULL;
3085 StrList **xst = &xsl;
H. Peter Anvin418ca702008-05-30 10:42:30 -07003086
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003087 casesense = true;
H. Peter Anvin418ca702008-05-30 10:42:30 -07003088
3089 tline = tline->next;
3090 skip_white_(tline);
3091 tline = expand_id(tline);
3092 if (!tline || (tline->type != TOK_ID &&
3093 (tline->type != TOK_PREPROC_ID ||
3094 tline->text[1] != '$'))) {
3095 error(ERR_NONFATAL,
3096 "`%%pathsearch' expects a macro identifier as first parameter");
3097 free_tlist(origline);
3098 return DIRECTIVE_FOUND;
3099 }
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08003100 ctx = get_ctx(tline->text, &mname, false);
H. Peter Anvin418ca702008-05-30 10:42:30 -07003101 last = tline;
3102 tline = expand_smacro(tline->next);
3103 last->next = NULL;
3104
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003105 t = tline;
H. Peter Anvin418ca702008-05-30 10:42:30 -07003106 while (tok_type_(t, TOK_WHITESPACE))
3107 t = t->next;
3108
3109 if (!t || (t->type != TOK_STRING &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003110 t->type != TOK_INTERNAL_STRING)) {
H. Peter Anvin418ca702008-05-30 10:42:30 -07003111 error(ERR_NONFATAL, "`%%pathsearch' expects a file name");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003112 free_tlist(tline);
H. Peter Anvin418ca702008-05-30 10:42:30 -07003113 free_tlist(origline);
3114 return DIRECTIVE_FOUND; /* but we did _something_ */
3115 }
3116 if (t->next)
H. Peter Anvin917a3492008-09-24 09:14:49 -07003117 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvin418ca702008-05-30 10:42:30 -07003118 "trailing garbage after `%%pathsearch' ignored");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003119 p = t->text;
H. Peter Anvin427cc912008-06-01 21:43:03 -07003120 if (t->type != TOK_INTERNAL_STRING)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003121 nasm_unquote(p, NULL);
H. Peter Anvin418ca702008-05-30 10:42:30 -07003122
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003123 fp = inc_fopen(p, &xsl, &xst, true);
3124 if (fp) {
3125 p = xsl->str;
3126 fclose(fp); /* Don't actually care about the file */
3127 }
H. Peter Anvin418ca702008-05-30 10:42:30 -07003128 macro_start = nasm_malloc(sizeof(*macro_start));
3129 macro_start->next = NULL;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003130 macro_start->text = nasm_quote(p, strlen(p));
3131 macro_start->type = TOK_STRING;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003132 macro_start->a.mac = NULL;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003133 if (xsl)
3134 nasm_free(xsl);
H. Peter Anvin418ca702008-05-30 10:42:30 -07003135
3136 /*
3137 * We now have a macro name, an implicit parameter count of
3138 * zero, and a string token to use as an expansion. Create
3139 * and store an SMacro.
3140 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003141 define_smacro(ctx, mname, casesense, 0, macro_start);
3142 free_tlist(tline);
H. Peter Anvin418ca702008-05-30 10:42:30 -07003143 free_tlist(origline);
3144 return DIRECTIVE_FOUND;
3145 }
3146
H. Peter Anvine2c80182005-01-15 22:15:51 +00003147 case PP_STRLEN:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003148 casesense = true;
H. Peter Anvin70653092007-10-19 14:42:29 -07003149
H. Peter Anvine2c80182005-01-15 22:15:51 +00003150 tline = tline->next;
3151 skip_white_(tline);
3152 tline = expand_id(tline);
3153 if (!tline || (tline->type != TOK_ID &&
3154 (tline->type != TOK_PREPROC_ID ||
3155 tline->text[1] != '$'))) {
3156 error(ERR_NONFATAL,
3157 "`%%strlen' expects a macro identifier as first parameter");
3158 free_tlist(origline);
3159 return DIRECTIVE_FOUND;
3160 }
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08003161 ctx = get_ctx(tline->text, &mname, false);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003162 last = tline;
3163 tline = expand_smacro(tline->next);
3164 last->next = NULL;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003165
H. Peter Anvine2c80182005-01-15 22:15:51 +00003166 t = tline;
3167 while (tok_type_(t, TOK_WHITESPACE))
3168 t = t->next;
3169 /* t should now point to the string */
3170 if (t->type != TOK_STRING) {
3171 error(ERR_NONFATAL,
3172 "`%%strlen` requires string as second parameter");
3173 free_tlist(tline);
3174 free_tlist(origline);
3175 return DIRECTIVE_FOUND;
3176 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003177
H. Peter Anvine2c80182005-01-15 22:15:51 +00003178 macro_start = nasm_malloc(sizeof(*macro_start));
3179 macro_start->next = NULL;
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07003180 make_tok_num(macro_start, nasm_unquote(t->text, NULL));
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003181 macro_start->a.mac = NULL;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003182
H. Peter Anvine2c80182005-01-15 22:15:51 +00003183 /*
3184 * We now have a macro name, an implicit parameter count of
3185 * zero, and a numeric token to use as an expansion. Create
3186 * and store an SMacro.
3187 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003188 define_smacro(ctx, mname, casesense, 0, macro_start);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003189 free_tlist(tline);
3190 free_tlist(origline);
3191 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003192
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003193 case PP_STRCAT:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003194 casesense = true;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003195
3196 tline = tline->next;
3197 skip_white_(tline);
3198 tline = expand_id(tline);
3199 if (!tline || (tline->type != TOK_ID &&
3200 (tline->type != TOK_PREPROC_ID ||
3201 tline->text[1] != '$'))) {
3202 error(ERR_NONFATAL,
3203 "`%%strcat' expects a macro identifier as first parameter");
3204 free_tlist(origline);
3205 return DIRECTIVE_FOUND;
3206 }
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08003207 ctx = get_ctx(tline->text, &mname, false);
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003208 last = tline;
3209 tline = expand_smacro(tline->next);
3210 last->next = NULL;
3211
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003212 len = 0;
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04003213 list_for_each(t, tline) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003214 switch (t->type) {
3215 case TOK_WHITESPACE:
3216 break;
3217 case TOK_STRING:
3218 len += t->a.len = nasm_unquote(t->text, NULL);
3219 break;
3220 case TOK_OTHER:
3221 if (!strcmp(t->text, ",")) /* permit comma separators */
3222 break;
3223 /* else fall through */
3224 default:
3225 error(ERR_NONFATAL,
3226 "non-string passed to `%%strcat' (%d)", t->type);
3227 free_tlist(tline);
3228 free_tlist(origline);
3229 return DIRECTIVE_FOUND;
3230 }
3231 }
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003232
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003233 p = pp = nasm_malloc(len);
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04003234 list_for_each(t, tline) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003235 if (t->type == TOK_STRING) {
3236 memcpy(p, t->text, t->a.len);
3237 p += t->a.len;
3238 }
3239 }
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003240
3241 /*
3242 * We now have a macro name, an implicit parameter count of
3243 * zero, and a numeric token to use as an expansion. Create
3244 * and store an SMacro.
3245 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003246 macro_start = new_Token(NULL, TOK_STRING, NULL, 0);
3247 macro_start->text = nasm_quote(pp, len);
3248 nasm_free(pp);
3249 define_smacro(ctx, mname, casesense, 0, macro_start);
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003250 free_tlist(tline);
3251 free_tlist(origline);
3252 return DIRECTIVE_FOUND;
3253
H. Peter Anvine2c80182005-01-15 22:15:51 +00003254 case PP_SUBSTR:
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003255 {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003256 int64_t a1, a2;
3257 size_t len;
H. Peter Anvind2456592008-06-19 15:04:18 -07003258
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003259 casesense = true;
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07003260
H. Peter Anvine2c80182005-01-15 22:15:51 +00003261 tline = tline->next;
3262 skip_white_(tline);
3263 tline = expand_id(tline);
3264 if (!tline || (tline->type != TOK_ID &&
3265 (tline->type != TOK_PREPROC_ID ||
3266 tline->text[1] != '$'))) {
3267 error(ERR_NONFATAL,
3268 "`%%substr' expects a macro identifier as first parameter");
3269 free_tlist(origline);
3270 return DIRECTIVE_FOUND;
3271 }
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08003272 ctx = get_ctx(tline->text, &mname, false);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003273 last = tline;
3274 tline = expand_smacro(tline->next);
3275 last->next = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003276
H. Peter Anvine2c80182005-01-15 22:15:51 +00003277 t = tline->next;
3278 while (tok_type_(t, TOK_WHITESPACE))
3279 t = t->next;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003280
H. Peter Anvine2c80182005-01-15 22:15:51 +00003281 /* t should now point to the string */
3282 if (t->type != TOK_STRING) {
3283 error(ERR_NONFATAL,
3284 "`%%substr` requires string as second parameter");
3285 free_tlist(tline);
3286 free_tlist(origline);
3287 return DIRECTIVE_FOUND;
3288 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003289
H. Peter Anvine2c80182005-01-15 22:15:51 +00003290 tt = t->next;
3291 tptr = &tt;
3292 tokval.t_type = TOKEN_INVALID;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003293 evalresult = evaluate(ppscan, tptr, &tokval, NULL,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003294 pass, error, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003295 if (!evalresult) {
3296 free_tlist(tline);
3297 free_tlist(origline);
3298 return DIRECTIVE_FOUND;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003299 } else if (!is_simple(evalresult)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00003300 error(ERR_NONFATAL, "non-constant value given to `%%substr`");
3301 free_tlist(tline);
3302 free_tlist(origline);
3303 return DIRECTIVE_FOUND;
3304 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003305 a1 = evalresult->value-1;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003306
3307 while (tok_type_(tt, TOK_WHITESPACE))
3308 tt = tt->next;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003309 if (!tt) {
3310 a2 = 1; /* Backwards compatibility: one character */
3311 } else {
3312 tokval.t_type = TOKEN_INVALID;
3313 evalresult = evaluate(ppscan, tptr, &tokval, NULL,
3314 pass, error, NULL);
3315 if (!evalresult) {
3316 free_tlist(tline);
3317 free_tlist(origline);
3318 return DIRECTIVE_FOUND;
3319 } else if (!is_simple(evalresult)) {
3320 error(ERR_NONFATAL, "non-constant value given to `%%substr`");
3321 free_tlist(tline);
3322 free_tlist(origline);
3323 return DIRECTIVE_FOUND;
3324 }
3325 a2 = evalresult->value;
3326 }
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003327
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003328 len = nasm_unquote(t->text, NULL);
3329 if (a2 < 0)
3330 a2 = a2+1+len-a1;
3331 if (a1+a2 > (int64_t)len)
3332 a2 = len-a1;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003333
H. Peter Anvine2c80182005-01-15 22:15:51 +00003334 macro_start = nasm_malloc(sizeof(*macro_start));
3335 macro_start->next = NULL;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003336 macro_start->text = nasm_quote((a1 < 0) ? "" : t->text+a1, a2);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003337 macro_start->type = TOK_STRING;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003338 macro_start->a.mac = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003339
H. Peter Anvine2c80182005-01-15 22:15:51 +00003340 /*
3341 * We now have a macro name, an implicit parameter count of
3342 * zero, and a numeric token to use as an expansion. Create
3343 * and store an SMacro.
3344 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003345 define_smacro(ctx, mname, casesense, 0, macro_start);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003346 free_tlist(tline);
3347 free_tlist(origline);
3348 return DIRECTIVE_FOUND;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003349 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003350
H. Peter Anvine2c80182005-01-15 22:15:51 +00003351 case PP_ASSIGN:
3352 case PP_IASSIGN:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003353 casesense = (i == PP_ASSIGN);
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07003354
H. Peter Anvine2c80182005-01-15 22:15:51 +00003355 tline = tline->next;
3356 skip_white_(tline);
3357 tline = expand_id(tline);
3358 if (!tline || (tline->type != TOK_ID &&
3359 (tline->type != TOK_PREPROC_ID ||
3360 tline->text[1] != '$'))) {
3361 error(ERR_NONFATAL,
3362 "`%%%sassign' expects a macro identifier",
3363 (i == PP_IASSIGN ? "i" : ""));
3364 free_tlist(origline);
3365 return DIRECTIVE_FOUND;
3366 }
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08003367 ctx = get_ctx(tline->text, &mname, false);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003368 last = tline;
3369 tline = expand_smacro(tline->next);
3370 last->next = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003371
H. Peter Anvine2c80182005-01-15 22:15:51 +00003372 t = tline;
3373 tptr = &t;
3374 tokval.t_type = TOKEN_INVALID;
3375 evalresult =
3376 evaluate(ppscan, tptr, &tokval, NULL, pass, error, NULL);
3377 free_tlist(tline);
3378 if (!evalresult) {
3379 free_tlist(origline);
3380 return DIRECTIVE_FOUND;
3381 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003382
H. Peter Anvine2c80182005-01-15 22:15:51 +00003383 if (tokval.t_type)
H. Peter Anvin917a3492008-09-24 09:14:49 -07003384 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +00003385 "trailing garbage after expression ignored");
H. Peter Anvin734b1882002-04-30 21:01:08 +00003386
H. Peter Anvine2c80182005-01-15 22:15:51 +00003387 if (!is_simple(evalresult)) {
3388 error(ERR_NONFATAL,
3389 "non-constant value given to `%%%sassign'",
3390 (i == PP_IASSIGN ? "i" : ""));
3391 free_tlist(origline);
3392 return DIRECTIVE_FOUND;
3393 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003394
H. Peter Anvine2c80182005-01-15 22:15:51 +00003395 macro_start = nasm_malloc(sizeof(*macro_start));
3396 macro_start->next = NULL;
3397 make_tok_num(macro_start, reloc_value(evalresult));
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003398 macro_start->a.mac = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003399
H. Peter Anvine2c80182005-01-15 22:15:51 +00003400 /*
3401 * We now have a macro name, an implicit parameter count of
3402 * zero, and a numeric token to use as an expansion. Create
3403 * and store an SMacro.
3404 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003405 define_smacro(ctx, mname, casesense, 0, macro_start);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003406 free_tlist(origline);
3407 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003408
H. Peter Anvine2c80182005-01-15 22:15:51 +00003409 case PP_LINE:
3410 /*
3411 * Syntax is `%line nnn[+mmm] [filename]'
3412 */
3413 tline = tline->next;
3414 skip_white_(tline);
3415 if (!tok_type_(tline, TOK_NUMBER)) {
3416 error(ERR_NONFATAL, "`%%line' expects line number");
3417 free_tlist(origline);
3418 return DIRECTIVE_FOUND;
3419 }
H. Peter Anvin70055962007-10-11 00:05:31 -07003420 k = readnum(tline->text, &err);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003421 m = 1;
3422 tline = tline->next;
3423 if (tok_is_(tline, "+")) {
3424 tline = tline->next;
3425 if (!tok_type_(tline, TOK_NUMBER)) {
3426 error(ERR_NONFATAL, "`%%line' expects line increment");
3427 free_tlist(origline);
3428 return DIRECTIVE_FOUND;
3429 }
H. Peter Anvin70055962007-10-11 00:05:31 -07003430 m = readnum(tline->text, &err);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003431 tline = tline->next;
3432 }
3433 skip_white_(tline);
3434 src_set_linnum(k);
3435 istk->lineinc = m;
3436 if (tline) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07003437 nasm_free(src_set_fname(detoken(tline, false)));
H. Peter Anvine2c80182005-01-15 22:15:51 +00003438 }
3439 free_tlist(origline);
3440 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003441
H. Peter Anvine2c80182005-01-15 22:15:51 +00003442 default:
3443 error(ERR_FATAL,
3444 "preprocessor directive `%s' not yet implemented",
H. Peter Anvin4169a472007-09-12 01:29:43 +00003445 pp_directives[i]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003446 return DIRECTIVE_FOUND;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003447 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003448}
3449
3450/*
H. Peter Anvin76690a12002-04-30 20:52:49 +00003451 * Ensure that a macro parameter contains a condition code and
3452 * nothing else. Return the condition code index if so, or -1
3453 * otherwise.
3454 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003455static int find_cc(Token * t)
H. Peter Anvineba20a72002-04-30 20:53:55 +00003456{
H. Peter Anvin76690a12002-04-30 20:52:49 +00003457 Token *tt;
3458 int i, j, k, m;
3459
H. Peter Anvin25a99342007-09-22 17:45:45 -07003460 if (!t)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003461 return -1; /* Probably a %+ without a space */
H. Peter Anvin25a99342007-09-22 17:45:45 -07003462
H. Peter Anvineba20a72002-04-30 20:53:55 +00003463 skip_white_(t);
H. Peter Anvin76690a12002-04-30 20:52:49 +00003464 if (t->type != TOK_ID)
H. Peter Anvine2c80182005-01-15 22:15:51 +00003465 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003466 tt = t->next;
H. Peter Anvineba20a72002-04-30 20:53:55 +00003467 skip_white_(tt);
H. Peter Anvin76690a12002-04-30 20:52:49 +00003468 if (tt && (tt->type != TOK_OTHER || strcmp(tt->text, ",")))
H. Peter Anvine2c80182005-01-15 22:15:51 +00003469 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003470
3471 i = -1;
Cyrill Gorcunova7319242010-06-03 22:04:36 +04003472 j = ARRAY_SIZE(conditions);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003473 while (j - i > 1) {
3474 k = (j + i) / 2;
3475 m = nasm_stricmp(t->text, conditions[k]);
3476 if (m == 0) {
3477 i = k;
3478 j = -2;
3479 break;
3480 } else if (m < 0) {
3481 j = k;
3482 } else
3483 i = k;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003484 }
3485 if (j != -2)
H. Peter Anvine2c80182005-01-15 22:15:51 +00003486 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003487 return i;
3488}
3489
H. Peter Anvind784a082009-04-20 14:01:18 -07003490static bool paste_tokens(Token **head, bool handle_paste_tokens)
3491{
3492 Token **tail, *t, *tt;
3493 Token **paste_head;
3494 bool did_paste = false;
3495 char *tmp;
3496
3497 /* Now handle token pasting... */
3498 paste_head = NULL;
3499 tail = head;
3500 while ((t = *tail) && (tt = t->next)) {
3501 switch (t->type) {
3502 case TOK_WHITESPACE:
3503 if (tt->type == TOK_WHITESPACE) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003504 /* Zap adjacent whitespace tokens */
H. Peter Anvind784a082009-04-20 14:01:18 -07003505 t->next = delete_Token(tt);
3506 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003507 /* Do not advance paste_head here */
3508 tail = &t->next;
3509 }
H. Peter Anvind784a082009-04-20 14:01:18 -07003510 break;
3511 case TOK_ID:
H. Peter Anvin20a94ad2010-07-08 11:52:57 -07003512 // case TOK_PREPROC_ID:
H. Peter Anvind784a082009-04-20 14:01:18 -07003513 case TOK_NUMBER:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003514 case TOK_FLOAT:
3515 {
3516 size_t len = 0;
3517 char *tmp, *p;
H. Peter Anvind784a082009-04-20 14:01:18 -07003518
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003519 while (tt && (tt->type == TOK_ID || tt->type == TOK_PREPROC_ID ||
3520 tt->type == TOK_NUMBER || tt->type == TOK_FLOAT ||
3521 tt->type == TOK_OTHER)) {
3522 len += strlen(tt->text);
3523 tt = tt->next;
3524 }
H. Peter Anvind784a082009-04-20 14:01:18 -07003525
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003526 /*
3527 * Now tt points to the first token after
3528 * the potential paste area...
3529 */
3530 if (tt != t->next) {
3531 /* We have at least two tokens... */
3532 len += strlen(t->text);
3533 p = tmp = nasm_malloc(len+1);
H. Peter Anvind784a082009-04-20 14:01:18 -07003534
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003535 while (t != tt) {
3536 strcpy(p, t->text);
3537 p = strchr(p, '\0');
3538 t = delete_Token(t);
3539 }
H. Peter Anvind784a082009-04-20 14:01:18 -07003540
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003541 t = *tail = tokenize(tmp);
3542 nasm_free(tmp);
H. Peter Anvind784a082009-04-20 14:01:18 -07003543
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003544 while (t->next) {
3545 tail = &t->next;
3546 t = t->next;
3547 }
3548 t->next = tt; /* Attach the remaining token chain */
H. Peter Anvind784a082009-04-20 14:01:18 -07003549
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003550 did_paste = true;
3551 }
3552 paste_head = tail;
3553 tail = &t->next;
3554 break;
3555 }
3556 case TOK_PASTE: /* %+ */
3557 if (handle_paste_tokens) {
3558 /* Zap %+ and whitespace tokens to the right */
3559 while (t && (t->type == TOK_WHITESPACE ||
3560 t->type == TOK_PASTE))
3561 t = *tail = delete_Token(t);
3562 if (!paste_head || !t)
3563 break; /* Nothing to paste with */
3564 tail = paste_head;
3565 t = *tail;
3566 tt = t->next;
3567 while (tok_type_(tt, TOK_WHITESPACE))
3568 tt = t->next = delete_Token(tt);
H. Peter Anvind784a082009-04-20 14:01:18 -07003569
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003570 if (tt) {
3571 tmp = nasm_strcat(t->text, tt->text);
3572 delete_Token(t);
3573 tt = delete_Token(tt);
3574 t = *tail = tokenize(tmp);
3575 nasm_free(tmp);
3576 while (t->next) {
3577 tail = &t->next;
3578 t = t->next;
3579 }
3580 t->next = tt; /* Attach the remaining token chain */
3581 did_paste = true;
3582 }
3583 paste_head = tail;
3584 tail = &t->next;
3585 break;
3586 }
3587 /* else fall through */
3588 default:
Cyrill Gorcunovadabc152010-07-10 02:11:41 +04003589 tail = &t->next;
3590 if (!tok_type_(t->next, TOK_WHITESPACE))
3591 paste_head = tail;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003592 break;
H. Peter Anvind784a082009-04-20 14:01:18 -07003593 }
3594 }
3595 return did_paste;
3596}
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003597
3598/*
3599 * expands to a list of tokens from %{x:y}
3600 */
3601static Token *expand_mmac_params_range(MMacro *mac, Token *tline, Token ***last)
3602{
3603 Token *t = tline, **tt, *tm, *head;
3604 char *pos;
3605 int fst, lst, j, i;
3606
3607 pos = strchr(tline->text, ':');
3608 nasm_assert(pos);
3609
3610 lst = atoi(pos + 1);
3611 fst = atoi(tline->text + 1);
3612
3613 /*
3614 * only macros params are accounted so
3615 * if someone passes %0 -- we reject such
3616 * value(s)
3617 */
3618 if (lst == 0 || fst == 0)
3619 goto err;
3620
3621 /* the values should be sane */
Cyrill Gorcunov2f403752010-06-05 10:47:10 +04003622 if ((fst > (int)mac->nparam || fst < (-(int)mac->nparam)) ||
3623 (lst > (int)mac->nparam || lst < (-(int)mac->nparam)))
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003624 goto err;
3625
Cyrill Gorcunovefb35832010-06-20 01:52:19 +04003626 fst = fst < 0 ? fst + (int)mac->nparam + 1: fst;
3627 lst = lst < 0 ? lst + (int)mac->nparam + 1: lst;
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003628
3629 /* counted from zero */
3630 fst--, lst--;
3631
3632 /*
3633 * it will be at least one token
3634 */
3635 tm = mac->params[(fst + mac->rotate) % mac->nparam];
3636 t = new_Token(NULL, tm->type, tm->text, 0);
3637 head = t, tt = &t->next;
3638 if (fst < lst) {
3639 for (i = fst + 1; i <= lst; i++) {
3640 t = new_Token(NULL, TOK_OTHER, ",", 0);
3641 *tt = t, tt = &t->next;
3642 j = (i + mac->rotate) % mac->nparam;
3643 tm = mac->params[j];
3644 t = new_Token(NULL, tm->type, tm->text, 0);
3645 *tt = t, tt = &t->next;
3646 }
3647 } else {
3648 for (i = fst - 1; i >= lst; i--) {
3649 t = new_Token(NULL, TOK_OTHER, ",", 0);
3650 *tt = t, tt = &t->next;
3651 j = (i + mac->rotate) % mac->nparam;
3652 tm = mac->params[j];
3653 t = new_Token(NULL, tm->type, tm->text, 0);
3654 *tt = t, tt = &t->next;
3655 }
3656 }
3657
3658 *last = tt;
3659 return head;
3660
3661err:
3662 error(ERR_NONFATAL, "`%%{%s}': macro parameters out of range",
3663 &tline->text[1]);
3664 return tline;
3665}
3666
H. Peter Anvin76690a12002-04-30 20:52:49 +00003667/*
3668 * Expand MMacro-local things: parameter references (%0, %n, %+n,
H. Peter Anvin67c63722008-10-26 23:49:00 -07003669 * %-n) and MMacro-local identifiers (%%foo) as well as
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003670 * macro indirection (%[...]) and range (%{..:..}).
H. Peter Anvin76690a12002-04-30 20:52:49 +00003671 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003672static Token *expand_mmac_params(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00003673{
H. Peter Anvin734b1882002-04-30 21:01:08 +00003674 Token *t, *tt, **tail, *thead;
H. Peter Anvin6125b622009-04-08 14:02:25 -07003675 bool changed = false;
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003676 char *pos;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003677
3678 tail = &thead;
3679 thead = NULL;
3680
H. Peter Anvine2c80182005-01-15 22:15:51 +00003681 while (tline) {
3682 if (tline->type == TOK_PREPROC_ID &&
Cyrill Gorcunovca611192010-06-04 09:22:12 +04003683 (((tline->text[1] == '+' || tline->text[1] == '-') && tline->text[2]) ||
3684 (tline->text[1] >= '0' && tline->text[1] <= '9') ||
3685 tline->text[1] == '%')) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00003686 char *text = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003687 int type = 0, cc; /* type = 0 to placate optimisers */
Keith Kaniosa6dfa782007-04-13 16:47:53 +00003688 char tmpbuf[30];
H. Peter Anvin25a99342007-09-22 17:45:45 -07003689 unsigned int n;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003690 int i;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003691 MMacro *mac;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003692
H. Peter Anvine2c80182005-01-15 22:15:51 +00003693 t = tline;
3694 tline = tline->next;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003695
H. Peter Anvine2c80182005-01-15 22:15:51 +00003696 mac = istk->mstk;
3697 while (mac && !mac->name) /* avoid mistaking %reps for macros */
3698 mac = mac->next_active;
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003699 if (!mac) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00003700 error(ERR_NONFATAL, "`%s': not in a macro call", t->text);
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003701 } else {
3702 pos = strchr(t->text, ':');
3703 if (!pos) {
3704 switch (t->text[1]) {
3705 /*
3706 * We have to make a substitution of one of the
3707 * forms %1, %-1, %+1, %%foo, %0.
3708 */
3709 case '0':
3710 type = TOK_NUMBER;
3711 snprintf(tmpbuf, sizeof(tmpbuf), "%d", mac->nparam);
3712 text = nasm_strdup(tmpbuf);
3713 break;
3714 case '%':
H. Peter Anvine2c80182005-01-15 22:15:51 +00003715 type = TOK_ID;
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003716 snprintf(tmpbuf, sizeof(tmpbuf), "..@%"PRIu64".",
3717 mac->unique);
3718 text = nasm_strcat(tmpbuf, t->text + 2);
3719 break;
3720 case '-':
3721 n = atoi(t->text + 2) - 1;
3722 if (n >= mac->nparam)
3723 tt = NULL;
3724 else {
3725 if (mac->nparam > 1)
3726 n = (n + mac->rotate) % mac->nparam;
3727 tt = mac->params[n];
H. Peter Anvine2c80182005-01-15 22:15:51 +00003728 }
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003729 cc = find_cc(tt);
3730 if (cc == -1) {
3731 error(ERR_NONFATAL,
3732 "macro parameter %d is not a condition code",
3733 n + 1);
3734 text = NULL;
3735 } else {
3736 type = TOK_ID;
3737 if (inverse_ccs[cc] == -1) {
3738 error(ERR_NONFATAL,
3739 "condition code `%s' is not invertible",
3740 conditions[cc]);
3741 text = NULL;
3742 } else
3743 text = nasm_strdup(conditions[inverse_ccs[cc]]);
3744 }
3745 break;
3746 case '+':
3747 n = atoi(t->text + 2) - 1;
3748 if (n >= mac->nparam)
3749 tt = NULL;
3750 else {
3751 if (mac->nparam > 1)
3752 n = (n + mac->rotate) % mac->nparam;
3753 tt = mac->params[n];
3754 }
3755 cc = find_cc(tt);
3756 if (cc == -1) {
3757 error(ERR_NONFATAL,
3758 "macro parameter %d is not a condition code",
3759 n + 1);
3760 text = NULL;
3761 } else {
3762 type = TOK_ID;
3763 text = nasm_strdup(conditions[cc]);
3764 }
3765 break;
3766 default:
3767 n = atoi(t->text + 1) - 1;
3768 if (n >= mac->nparam)
3769 tt = NULL;
3770 else {
3771 if (mac->nparam > 1)
3772 n = (n + mac->rotate) % mac->nparam;
3773 tt = mac->params[n];
3774 }
3775 if (tt) {
3776 for (i = 0; i < mac->paramlen[n]; i++) {
3777 *tail = new_Token(NULL, tt->type, tt->text, 0);
3778 tail = &(*tail)->next;
3779 tt = tt->next;
3780 }
3781 }
3782 text = NULL; /* we've done it here */
3783 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003784 }
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003785 } else {
3786 /*
3787 * seems we have a parameters range here
3788 */
3789 Token *head, **last;
3790 head = expand_mmac_params_range(mac, t, &last);
3791 if (head != t) {
3792 *tail = head;
3793 *last = tline;
3794 tline = head;
3795 text = NULL;
3796 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00003797 }
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003798 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00003799 if (!text) {
3800 delete_Token(t);
3801 } else {
3802 *tail = t;
3803 tail = &t->next;
3804 t->type = type;
3805 nasm_free(t->text);
3806 t->text = text;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003807 t->a.mac = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003808 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003809 changed = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003810 continue;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003811 } else if (tline->type == TOK_INDIRECT) {
3812 t = tline;
3813 tline = tline->next;
3814 tt = tokenize(t->text);
3815 tt = expand_mmac_params(tt);
3816 tt = expand_smacro(tt);
3817 *tail = tt;
3818 while (tt) {
3819 tt->a.mac = NULL; /* Necessary? */
3820 tail = &tt->next;
3821 tt = tt->next;
3822 }
3823 delete_Token(t);
3824 changed = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003825 } else {
3826 t = *tail = tline;
3827 tline = tline->next;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003828 t->a.mac = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003829 tail = &t->next;
3830 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00003831 }
H. Peter Anvineba20a72002-04-30 20:53:55 +00003832 *tail = NULL;
H. Peter Anvin67c63722008-10-26 23:49:00 -07003833
H. Peter Anvind784a082009-04-20 14:01:18 -07003834 if (changed)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003835 paste_tokens(&thead, false);
H. Peter Anvin6125b622009-04-08 14:02:25 -07003836
H. Peter Anvin76690a12002-04-30 20:52:49 +00003837 return thead;
3838}
3839
3840/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003841 * Expand all single-line macro calls made in the given line.
3842 * Return the expanded version of the line. The original is deemed
3843 * to be destroyed in the process. (In reality we'll just move
3844 * Tokens from input to output a lot of the time, rather than
3845 * actually bothering to destroy and replicate.)
3846 */
H. Peter Anvincb1cf592007-11-19 12:26:50 -08003847
H. Peter Anvine2c80182005-01-15 22:15:51 +00003848static Token *expand_smacro(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00003849{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003850 Token *t, *tt, *mstart, **tail, *thead;
H. Peter Anvineba20a72002-04-30 20:53:55 +00003851 SMacro *head = NULL, *m;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003852 Token **params;
3853 int *paramsize;
H. Peter Anvin25a99342007-09-22 17:45:45 -07003854 unsigned int nparam, sparam;
H. Peter Anvind784a082009-04-20 14:01:18 -07003855 int brackets;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003856 Token *org_tline = tline;
3857 Context *ctx;
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08003858 const char *mname;
H. Peter Anvin2a15e692007-11-19 13:14:59 -08003859 int deadman = DEADMAN_LIMIT;
H. Peter Anvin8287daf2009-07-07 16:00:58 -07003860 bool expanded;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003861
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003862 /*
3863 * Trick: we should avoid changing the start token pointer since it can
3864 * be contained in "next" field of other token. Because of this
3865 * we allocate a copy of first token and work with it; at the end of
3866 * routine we copy it back
3867 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003868 if (org_tline) {
Cyrill Gorcunoved4a8052010-04-09 15:40:35 +04003869 tline = new_Token(org_tline->next, org_tline->type,
3870 org_tline->text, 0);
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003871 tline->a.mac = org_tline->a.mac;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003872 nasm_free(org_tline->text);
3873 org_tline->text = NULL;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003874 }
3875
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003876 expanded = true; /* Always expand %+ at least once */
H. Peter Anvin8287daf2009-07-07 16:00:58 -07003877
H. Peter Anvincb1cf592007-11-19 12:26:50 -08003878again:
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003879 thead = NULL;
Cyrill Gorcunoved4a8052010-04-09 15:40:35 +04003880 tail = &thead;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003881
H. Peter Anvine2c80182005-01-15 22:15:51 +00003882 while (tline) { /* main token loop */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003883 if (!--deadman) {
3884 error(ERR_NONFATAL, "interminable macro recursion");
Cyrill Gorcunovbd38c8f2009-11-21 11:11:23 +03003885 goto err;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003886 }
H. Peter Anvincb1cf592007-11-19 12:26:50 -08003887
H. Peter Anvine2c80182005-01-15 22:15:51 +00003888 if ((mname = tline->text)) {
3889 /* if this token is a local macro, look in local context */
Cyrill Gorcunovc56d9ad2010-02-11 15:12:19 +03003890 if (tline->type == TOK_ID) {
3891 head = (SMacro *)hash_findix(&smacros, mname);
3892 } else if (tline->type == TOK_PREPROC_ID) {
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08003893 ctx = get_ctx(mname, &mname, true);
Cyrill Gorcunovc56d9ad2010-02-11 15:12:19 +03003894 head = ctx ? (SMacro *)hash_findix(&ctx->localmac, mname) : NULL;
3895 } else
3896 head = NULL;
H. Peter Anvin072771e2008-05-22 13:17:51 -07003897
H. Peter Anvine2c80182005-01-15 22:15:51 +00003898 /*
3899 * We've hit an identifier. As in is_mmacro below, we first
3900 * check whether the identifier is a single-line macro at
3901 * all, then think about checking for parameters if
3902 * necessary.
3903 */
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04003904 list_for_each(m, head)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003905 if (!mstrcmp(m->name, mname, m->casesense))
3906 break;
3907 if (m) {
3908 mstart = tline;
3909 params = NULL;
3910 paramsize = NULL;
3911 if (m->nparam == 0) {
3912 /*
3913 * Simple case: the macro is parameterless. Discard the
3914 * one token that the macro call took, and push the
3915 * expansion back on the to-do stack.
3916 */
3917 if (!m->expansion) {
3918 if (!strcmp("__FILE__", m->name)) {
3919 int32_t num = 0;
3920 char *file = NULL;
3921 src_get(&num, &file);
3922 tline->text = nasm_quote(file, strlen(file));
3923 tline->type = TOK_STRING;
3924 nasm_free(file);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003925 continue;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003926 }
3927 if (!strcmp("__LINE__", m->name)) {
3928 nasm_free(tline->text);
3929 make_tok_num(tline, src_get_linnum());
3930 continue;
3931 }
3932 if (!strcmp("__BITS__", m->name)) {
3933 nasm_free(tline->text);
3934 make_tok_num(tline, globalbits);
3935 continue;
3936 }
3937 tline = delete_Token(tline);
3938 continue;
3939 }
3940 } else {
3941 /*
3942 * Complicated case: at least one macro with this name
H. Peter Anvine2c80182005-01-15 22:15:51 +00003943 * exists and takes parameters. We must find the
3944 * parameters in the call, count them, find the SMacro
3945 * that corresponds to that form of the macro call, and
3946 * substitute for the parameters when we expand. What a
3947 * pain.
3948 */
3949 /*tline = tline->next;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003950 skip_white_(tline); */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003951 do {
3952 t = tline->next;
3953 while (tok_type_(t, TOK_SMAC_END)) {
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003954 t->a.mac->in_progress = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003955 t->text = NULL;
3956 t = tline->next = delete_Token(t);
3957 }
3958 tline = t;
3959 } while (tok_type_(tline, TOK_WHITESPACE));
3960 if (!tok_is_(tline, "(")) {
3961 /*
3962 * This macro wasn't called with parameters: ignore
3963 * the call. (Behaviour borrowed from gnu cpp.)
3964 */
3965 tline = mstart;
3966 m = NULL;
3967 } else {
3968 int paren = 0;
3969 int white = 0;
3970 brackets = 0;
3971 nparam = 0;
3972 sparam = PARAM_DELTA;
3973 params = nasm_malloc(sparam * sizeof(Token *));
3974 params[0] = tline->next;
3975 paramsize = nasm_malloc(sparam * sizeof(int));
3976 paramsize[0] = 0;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07003977 while (true) { /* parameter loop */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003978 /*
3979 * For some unusual expansions
3980 * which concatenates function call
3981 */
3982 t = tline->next;
3983 while (tok_type_(t, TOK_SMAC_END)) {
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003984 t->a.mac->in_progress = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003985 t->text = NULL;
3986 t = tline->next = delete_Token(t);
3987 }
3988 tline = t;
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00003989
H. Peter Anvine2c80182005-01-15 22:15:51 +00003990 if (!tline) {
3991 error(ERR_NONFATAL,
3992 "macro call expects terminating `)'");
3993 break;
3994 }
3995 if (tline->type == TOK_WHITESPACE
3996 && brackets <= 0) {
3997 if (paramsize[nparam])
3998 white++;
3999 else
4000 params[nparam] = tline->next;
4001 continue; /* parameter loop */
4002 }
4003 if (tline->type == TOK_OTHER
4004 && tline->text[1] == 0) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004005 char ch = tline->text[0];
H. Peter Anvine2c80182005-01-15 22:15:51 +00004006 if (ch == ',' && !paren && brackets <= 0) {
4007 if (++nparam >= sparam) {
4008 sparam += PARAM_DELTA;
4009 params = nasm_realloc(params,
Cyrill Gorcunoved4a8052010-04-09 15:40:35 +04004010 sparam * sizeof(Token *));
4011 paramsize = nasm_realloc(paramsize,
4012 sparam * sizeof(int));
H. Peter Anvine2c80182005-01-15 22:15:51 +00004013 }
4014 params[nparam] = tline->next;
4015 paramsize[nparam] = 0;
4016 white = 0;
4017 continue; /* parameter loop */
4018 }
4019 if (ch == '{' &&
4020 (brackets > 0 || (brackets == 0 &&
4021 !paramsize[nparam])))
4022 {
4023 if (!(brackets++)) {
4024 params[nparam] = tline->next;
4025 continue; /* parameter loop */
4026 }
4027 }
4028 if (ch == '}' && brackets > 0)
4029 if (--brackets == 0) {
4030 brackets = -1;
4031 continue; /* parameter loop */
4032 }
4033 if (ch == '(' && !brackets)
4034 paren++;
4035 if (ch == ')' && brackets <= 0)
4036 if (--paren < 0)
4037 break;
4038 }
4039 if (brackets < 0) {
4040 brackets = 0;
4041 error(ERR_NONFATAL, "braces do not "
4042 "enclose all of macro parameter");
4043 }
4044 paramsize[nparam] += white + 1;
4045 white = 0;
4046 } /* parameter loop */
4047 nparam++;
4048 while (m && (m->nparam != nparam ||
4049 mstrcmp(m->name, mname,
4050 m->casesense)))
4051 m = m->next;
4052 if (!m)
H. Peter Anvin917a3492008-09-24 09:14:49 -07004053 error(ERR_WARNING|ERR_PASS1|ERR_WARN_MNP,
H. Peter Anvine2c80182005-01-15 22:15:51 +00004054 "macro `%s' exists, "
4055 "but not taking %d parameters",
4056 mstart->text, nparam);
4057 }
4058 }
4059 if (m && m->in_progress)
4060 m = NULL;
4061 if (!m) { /* in progess or didn't find '(' or wrong nparam */
H. Peter Anvin70653092007-10-19 14:42:29 -07004062 /*
H. Peter Anvine2c80182005-01-15 22:15:51 +00004063 * Design question: should we handle !tline, which
4064 * indicates missing ')' here, or expand those
4065 * macros anyway, which requires the (t) test a few
H. Peter Anvin70653092007-10-19 14:42:29 -07004066 * lines down?
H. Peter Anvine2c80182005-01-15 22:15:51 +00004067 */
4068 nasm_free(params);
4069 nasm_free(paramsize);
4070 tline = mstart;
4071 } else {
4072 /*
4073 * Expand the macro: we are placed on the last token of the
4074 * call, so that we can easily split the call from the
4075 * following tokens. We also start by pushing an SMAC_END
4076 * token for the cycle removal.
4077 */
4078 t = tline;
4079 if (t) {
4080 tline = t->next;
4081 t->next = NULL;
4082 }
4083 tt = new_Token(tline, TOK_SMAC_END, NULL, 0);
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004084 tt->a.mac = m;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07004085 m->in_progress = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004086 tline = tt;
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04004087 list_for_each(t, m->expansion) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00004088 if (t->type >= TOK_SMAC_PARAM) {
4089 Token *pcopy = tline, **ptail = &pcopy;
4090 Token *ttt, *pt;
4091 int i;
H. Peter Anvin734b1882002-04-30 21:01:08 +00004092
H. Peter Anvine2c80182005-01-15 22:15:51 +00004093 ttt = params[t->type - TOK_SMAC_PARAM];
Cyrill Gorcunoved4a8052010-04-09 15:40:35 +04004094 i = paramsize[t->type - TOK_SMAC_PARAM];
4095 while (--i >= 0) {
4096 pt = *ptail = new_Token(tline, ttt->type,
4097 ttt->text, 0);
H. Peter Anvine2c80182005-01-15 22:15:51 +00004098 ptail = &pt->next;
4099 ttt = ttt->next;
4100 }
4101 tline = pcopy;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004102 } else if (t->type == TOK_PREPROC_Q) {
4103 tt = new_Token(tline, TOK_ID, mname, 0);
4104 tline = tt;
4105 } else if (t->type == TOK_PREPROC_QQ) {
4106 tt = new_Token(tline, TOK_ID, m->name, 0);
4107 tline = tt;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004108 } else {
4109 tt = new_Token(tline, t->type, t->text, 0);
4110 tline = tt;
4111 }
4112 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00004113
H. Peter Anvine2c80182005-01-15 22:15:51 +00004114 /*
4115 * Having done that, get rid of the macro call, and clean
4116 * up the parameters.
4117 */
4118 nasm_free(params);
4119 nasm_free(paramsize);
4120 free_tlist(mstart);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004121 expanded = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004122 continue; /* main token loop */
4123 }
4124 }
4125 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004126
H. Peter Anvine2c80182005-01-15 22:15:51 +00004127 if (tline->type == TOK_SMAC_END) {
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004128 tline->a.mac->in_progress = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004129 tline = delete_Token(tline);
4130 } else {
4131 t = *tail = tline;
4132 tline = tline->next;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004133 t->a.mac = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004134 t->next = NULL;
4135 tail = &t->next;
4136 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004137 }
4138
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004139 /*
4140 * Now scan the entire line and look for successive TOK_IDs that resulted
Keith Kaniosb7a89542007-04-12 02:40:54 +00004141 * after expansion (they can't be produced by tokenize()). The successive
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004142 * TOK_IDs should be concatenated.
4143 * Also we look for %+ tokens and concatenate the tokens before and after
4144 * them (without white spaces in between).
4145 */
H. Peter Anvin8287daf2009-07-07 16:00:58 -07004146 if (expanded && paste_tokens(&thead, true)) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004147 /*
4148 * If we concatenated something, *and* we had previously expanded
4149 * an actual macro, scan the lines again for macros...
4150 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004151 tline = thead;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004152 expanded = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004153 goto again;
H. Peter Anvin734b1882002-04-30 21:01:08 +00004154 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004155
Cyrill Gorcunovbd38c8f2009-11-21 11:11:23 +03004156err:
H. Peter Anvine2c80182005-01-15 22:15:51 +00004157 if (org_tline) {
4158 if (thead) {
4159 *org_tline = *thead;
4160 /* since we just gave text to org_line, don't free it */
4161 thead->text = NULL;
4162 delete_Token(thead);
4163 } else {
4164 /* the expression expanded to empty line;
4165 we can't return NULL for some reasons
4166 we just set the line to a single WHITESPACE token. */
4167 memset(org_tline, 0, sizeof(*org_tline));
4168 org_tline->text = NULL;
4169 org_tline->type = TOK_WHITESPACE;
4170 }
4171 thead = org_tline;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004172 }
4173
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004174 return thead;
4175}
4176
4177/*
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004178 * Similar to expand_smacro but used exclusively with macro identifiers
4179 * right before they are fetched in. The reason is that there can be
4180 * identifiers consisting of several subparts. We consider that if there
4181 * are more than one element forming the name, user wants a expansion,
4182 * otherwise it will be left as-is. Example:
4183 *
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004184 * %define %$abc cde
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004185 *
4186 * the identifier %$abc will be left as-is so that the handler for %define
4187 * will suck it and define the corresponding value. Other case:
4188 *
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004189 * %define _%$abc cde
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004190 *
4191 * In this case user wants name to be expanded *before* %define starts
4192 * working, so we'll expand %$abc into something (if it has a value;
4193 * otherwise it will be left as-is) then concatenate all successive
4194 * PP_IDs into one.
4195 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004196static Token *expand_id(Token * tline)
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004197{
4198 Token *cur, *oldnext = NULL;
4199
H. Peter Anvin734b1882002-04-30 21:01:08 +00004200 if (!tline || !tline->next)
H. Peter Anvine2c80182005-01-15 22:15:51 +00004201 return tline;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004202
4203 cur = tline;
4204 while (cur->next &&
H. Peter Anvine2c80182005-01-15 22:15:51 +00004205 (cur->next->type == TOK_ID ||
4206 cur->next->type == TOK_PREPROC_ID
4207 || cur->next->type == TOK_NUMBER))
4208 cur = cur->next;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004209
4210 /* If identifier consists of just one token, don't expand */
4211 if (cur == tline)
H. Peter Anvine2c80182005-01-15 22:15:51 +00004212 return tline;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004213
H. Peter Anvine2c80182005-01-15 22:15:51 +00004214 if (cur) {
4215 oldnext = cur->next; /* Detach the tail past identifier */
4216 cur->next = NULL; /* so that expand_smacro stops here */
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004217 }
4218
H. Peter Anvin734b1882002-04-30 21:01:08 +00004219 tline = expand_smacro(tline);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004220
H. Peter Anvine2c80182005-01-15 22:15:51 +00004221 if (cur) {
4222 /* expand_smacro possibly changhed tline; re-scan for EOL */
4223 cur = tline;
4224 while (cur && cur->next)
4225 cur = cur->next;
4226 if (cur)
4227 cur->next = oldnext;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004228 }
4229
4230 return tline;
4231}
4232
4233/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004234 * Determine whether the given line constitutes a multi-line macro
4235 * call, and return the MMacro structure called if so. Doesn't have
4236 * to check for an initial label - that's taken care of in
4237 * expand_mmacro - but must check numbers of parameters. Guaranteed
4238 * to be called with tline->type == TOK_ID, so the putative macro
4239 * name is easy to find.
4240 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004241static MMacro *is_mmacro(Token * tline, Token *** params_array)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004242{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004243 MMacro *head, *m;
4244 Token **params;
4245 int nparam;
4246
H. Peter Anvin166c2472008-05-28 12:28:58 -07004247 head = (MMacro *) hash_findix(&mmacros, tline->text);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004248
4249 /*
4250 * Efficiency: first we see if any macro exists with the given
4251 * name. If not, we can return NULL immediately. _Then_ we
4252 * count the parameters, and then we look further along the
4253 * list if necessary to find the proper MMacro.
4254 */
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04004255 list_for_each(m, head)
H. Peter Anvine2c80182005-01-15 22:15:51 +00004256 if (!mstrcmp(m->name, tline->text, m->casesense))
4257 break;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004258 if (!m)
H. Peter Anvine2c80182005-01-15 22:15:51 +00004259 return NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004260
4261 /*
4262 * OK, we have a potential macro. Count and demarcate the
4263 * parameters.
4264 */
H. Peter Anvin734b1882002-04-30 21:01:08 +00004265 count_mmac_params(tline->next, &nparam, &params);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004266
4267 /*
4268 * So we know how many parameters we've got. Find the MMacro
4269 * structure that handles this number.
4270 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004271 while (m) {
4272 if (m->nparam_min <= nparam
4273 && (m->plus || nparam <= m->nparam_max)) {
4274 /*
4275 * This one is right. Just check if cycle removal
4276 * prohibits us using it before we actually celebrate...
4277 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004278 if (m->in_progress > m->max_depth) {
4279 if (m->max_depth > 0) {
4280 error(ERR_WARNING,
4281 "reached maximum recursion depth of %i",
4282 m->max_depth);
4283 }
4284 nasm_free(params);
4285 return NULL;
4286 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00004287 /*
4288 * It's right, and we can use it. Add its default
4289 * parameters to the end of our list if necessary.
4290 */
4291 if (m->defaults && nparam < m->nparam_min + m->ndefs) {
4292 params =
4293 nasm_realloc(params,
4294 ((m->nparam_min + m->ndefs +
4295 1) * sizeof(*params)));
4296 while (nparam < m->nparam_min + m->ndefs) {
4297 params[nparam] = m->defaults[nparam - m->nparam_min];
4298 nparam++;
4299 }
4300 }
4301 /*
4302 * If we've gone over the maximum parameter count (and
4303 * we're in Plus mode), ignore parameters beyond
4304 * nparam_max.
4305 */
4306 if (m->plus && nparam > m->nparam_max)
4307 nparam = m->nparam_max;
4308 /*
4309 * Then terminate the parameter list, and leave.
4310 */
4311 if (!params) { /* need this special case */
4312 params = nasm_malloc(sizeof(*params));
4313 nparam = 0;
4314 }
4315 params[nparam] = NULL;
4316 *params_array = params;
4317 return m;
4318 }
4319 /*
4320 * This one wasn't right: look for the next one with the
4321 * same name.
4322 */
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04004323 list_for_each(m, m->next)
H. Peter Anvine2c80182005-01-15 22:15:51 +00004324 if (!mstrcmp(m->name, tline->text, m->casesense))
4325 break;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004326 }
4327
4328 /*
4329 * After all that, we didn't find one with the right number of
4330 * parameters. Issue a warning, and fail to expand the macro.
4331 */
H. Peter Anvin917a3492008-09-24 09:14:49 -07004332 error(ERR_WARNING|ERR_PASS1|ERR_WARN_MNP,
H. Peter Anvine2c80182005-01-15 22:15:51 +00004333 "macro `%s' exists, but not taking %d parameters",
4334 tline->text, nparam);
H. Peter Anvin734b1882002-04-30 21:01:08 +00004335 nasm_free(params);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004336 return NULL;
4337}
4338
Keith Kanios891775e2009-07-11 06:08:54 -05004339
4340/*
4341 * Save MMacro invocation specific fields in
4342 * preparation for a recursive macro expansion
4343 */
4344static void push_mmacro(MMacro *m)
4345{
4346 MMacroInvocation *i;
4347
H. Peter Anvin89cee572009-07-15 09:16:54 -04004348 i = nasm_malloc(sizeof(MMacroInvocation));
4349 i->prev = m->prev;
4350 i->params = m->params;
4351 i->iline = m->iline;
4352 i->nparam = m->nparam;
4353 i->rotate = m->rotate;
4354 i->paramlen = m->paramlen;
4355 i->unique = m->unique;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004356 i->condcnt = m->condcnt;
H. Peter Anvin89cee572009-07-15 09:16:54 -04004357 m->prev = i;
Keith Kanios891775e2009-07-11 06:08:54 -05004358}
4359
4360
4361/*
4362 * Restore MMacro invocation specific fields that were
4363 * saved during a previous recursive macro expansion
4364 */
4365static void pop_mmacro(MMacro *m)
4366{
4367 MMacroInvocation *i;
4368
H. Peter Anvin89cee572009-07-15 09:16:54 -04004369 if (m->prev) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004370 i = m->prev;
4371 m->prev = i->prev;
4372 m->params = i->params;
4373 m->iline = i->iline;
4374 m->nparam = i->nparam;
4375 m->rotate = i->rotate;
4376 m->paramlen = i->paramlen;
4377 m->unique = i->unique;
4378 m->condcnt = i->condcnt;
4379 nasm_free(i);
H. Peter Anvin89cee572009-07-15 09:16:54 -04004380 }
Keith Kanios891775e2009-07-11 06:08:54 -05004381}
4382
4383
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004384/*
4385 * Expand the multi-line macro call made by the given line, if
4386 * there is one to be expanded. If there is, push the expansion on
H. Peter Anvineba20a72002-04-30 20:53:55 +00004387 * istk->expansion and return 1. Otherwise return 0.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004388 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004389static int expand_mmacro(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004390{
4391 Token *startline = tline;
4392 Token *label = NULL;
4393 int dont_prepend = 0;
H. Peter Anvince2233b2008-05-25 21:57:00 -07004394 Token **params, *t, *mtok, *tt;
H. Peter Anvineba20a72002-04-30 20:53:55 +00004395 MMacro *m;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004396 Line *l, *ll;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004397 int i, nparam, *paramlen;
H. Peter Anvinc751e862008-06-09 10:18:45 -07004398 const char *mname;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004399
4400 t = tline;
H. Peter Anvineba20a72002-04-30 20:53:55 +00004401 skip_white_(t);
H. Peter Anvince2233b2008-05-25 21:57:00 -07004402 /* if (!tok_type_(t, TOK_ID)) Lino 02/25/02 */
H. Peter Anvindce1e2f2002-04-30 21:06:37 +00004403 if (!tok_type_(t, TOK_ID) && !tok_type_(t, TOK_PREPROC_ID))
H. Peter Anvine2c80182005-01-15 22:15:51 +00004404 return 0;
H. Peter Anvince2233b2008-05-25 21:57:00 -07004405 mtok = t;
H. Peter Anvin734b1882002-04-30 21:01:08 +00004406 m = is_mmacro(t, &params);
H. Peter Anvinc751e862008-06-09 10:18:45 -07004407 if (m) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004408 mname = t->text;
H. Peter Anvinc751e862008-06-09 10:18:45 -07004409 } else {
H. Peter Anvine2c80182005-01-15 22:15:51 +00004410 Token *last;
4411 /*
4412 * We have an id which isn't a macro call. We'll assume
4413 * it might be a label; we'll also check to see if a
4414 * colon follows it. Then, if there's another id after
4415 * that lot, we'll check it again for macro-hood.
4416 */
4417 label = last = t;
4418 t = t->next;
4419 if (tok_type_(t, TOK_WHITESPACE))
4420 last = t, t = t->next;
4421 if (tok_is_(t, ":")) {
4422 dont_prepend = 1;
4423 last = t, t = t->next;
4424 if (tok_type_(t, TOK_WHITESPACE))
4425 last = t, t = t->next;
4426 }
H. Peter Anvin89cee572009-07-15 09:16:54 -04004427 if (!tok_type_(t, TOK_ID) || !(m = is_mmacro(t, &params)))
H. Peter Anvine2c80182005-01-15 22:15:51 +00004428 return 0;
4429 last->next = NULL;
Keith Kanios891775e2009-07-11 06:08:54 -05004430 mname = t->text;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004431 tline = t;
H. Peter Anvineba20a72002-04-30 20:53:55 +00004432 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004433
4434 /*
4435 * Fix up the parameters: this involves stripping leading and
4436 * trailing whitespace, then stripping braces if they are
4437 * present.
4438 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004439 for (nparam = 0; params[nparam]; nparam++) ;
H. Peter Anvin734b1882002-04-30 21:01:08 +00004440 paramlen = nparam ? nasm_malloc(nparam * sizeof(*paramlen)) : NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004441
H. Peter Anvine2c80182005-01-15 22:15:51 +00004442 for (i = 0; params[i]; i++) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07004443 int brace = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004444 int comma = (!m->plus || i < nparam - 1);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004445
H. Peter Anvine2c80182005-01-15 22:15:51 +00004446 t = params[i];
4447 skip_white_(t);
4448 if (tok_is_(t, "{"))
H. Peter Anvin6867acc2007-10-10 14:58:45 -07004449 t = t->next, brace = true, comma = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004450 params[i] = t;
4451 paramlen[i] = 0;
4452 while (t) {
4453 if (comma && t->type == TOK_OTHER && !strcmp(t->text, ","))
4454 break; /* ... because we have hit a comma */
4455 if (comma && t->type == TOK_WHITESPACE
4456 && tok_is_(t->next, ","))
4457 break; /* ... or a space then a comma */
4458 if (brace && t->type == TOK_OTHER && !strcmp(t->text, "}"))
4459 break; /* ... or a brace */
4460 t = t->next;
4461 paramlen[i]++;
4462 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004463 }
4464
4465 /*
4466 * OK, we have a MMacro structure together with a set of
4467 * parameters. We must now go through the expansion and push
H. Peter Anvin76690a12002-04-30 20:52:49 +00004468 * copies of each Line on to istk->expansion. Substitution of
4469 * parameter tokens and macro-local tokens doesn't get done
4470 * until the single-line macro substitution process; this is
4471 * because delaying them allows us to change the semantics
4472 * later through %rotate.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004473 *
H. Peter Anvin76690a12002-04-30 20:52:49 +00004474 * First, push an end marker on to istk->expansion, mark this
4475 * macro as in progress, and set up its invocation-specific
4476 * variables.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004477 */
4478 ll = nasm_malloc(sizeof(Line));
4479 ll->next = istk->expansion;
4480 ll->finishes = m;
4481 ll->first = NULL;
4482 istk->expansion = ll;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004483
H. Peter Anvin89cee572009-07-15 09:16:54 -04004484 /*
4485 * Save the previous MMacro expansion in the case of
4486 * macro recursion
4487 */
4488 if (m->max_depth && m->in_progress)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004489 push_mmacro(m);
H. Peter Anvin76690a12002-04-30 20:52:49 +00004490
Keith Kanios891775e2009-07-11 06:08:54 -05004491 m->in_progress ++;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004492 m->params = params;
4493 m->iline = tline;
4494 m->nparam = nparam;
4495 m->rotate = 0;
4496 m->paramlen = paramlen;
4497 m->unique = unique++;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004498 m->lineno = 0;
Keith Kanios3c0d91f2009-10-25 13:28:03 -05004499 m->condcnt = 0;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004500
4501 m->next_active = istk->mstk;
4502 istk->mstk = m;
4503
Cyrill Gorcunov367d59e2010-04-09 15:43:03 +04004504 list_for_each(l, m->expansion) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00004505 Token **tail;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004506
H. Peter Anvine2c80182005-01-15 22:15:51 +00004507 ll = nasm_malloc(sizeof(Line));
4508 ll->finishes = NULL;
4509 ll->next = istk->expansion;
4510 istk->expansion = ll;
4511 tail = &ll->first;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004512
Cyrill Gorcunov367d59e2010-04-09 15:43:03 +04004513 list_for_each(t, l->first) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00004514 Token *x = t;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004515 switch (t->type) {
4516 case TOK_PREPROC_Q:
4517 tt = *tail = new_Token(NULL, TOK_ID, mname, 0);
4518 break;
4519 case TOK_PREPROC_QQ:
4520 tt = *tail = new_Token(NULL, TOK_ID, m->name, 0);
4521 break;
4522 case TOK_PREPROC_ID:
4523 if (t->text[1] == '0' && t->text[2] == '0') {
4524 dont_prepend = -1;
4525 x = label;
4526 if (!x)
4527 continue;
4528 }
4529 /* fall through */
4530 default:
4531 tt = *tail = new_Token(NULL, x->type, x->text, 0);
4532 break;
4533 }
4534 tail = &tt->next;
4535 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00004536 *tail = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004537 }
4538
4539 /*
H. Peter Anvineba20a72002-04-30 20:53:55 +00004540 * If we had a label, push it on as the first line of
4541 * the macro expansion.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004542 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004543 if (label) {
4544 if (dont_prepend < 0)
4545 free_tlist(startline);
4546 else {
4547 ll = nasm_malloc(sizeof(Line));
4548 ll->finishes = NULL;
4549 ll->next = istk->expansion;
4550 istk->expansion = ll;
4551 ll->first = startline;
4552 if (!dont_prepend) {
4553 while (label->next)
4554 label = label->next;
4555 label->next = tt = new_Token(NULL, TOK_OTHER, ":", 0);
4556 }
4557 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004558 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004559
H. Peter Anvin734b1882002-04-30 21:01:08 +00004560 list->uplevel(m->nolist ? LIST_MACRO_NOLIST : LIST_MACRO);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004561
H. Peter Anvineba20a72002-04-30 20:53:55 +00004562 return 1;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004563}
4564
Victor van den Elzen3b404c02008-09-18 13:51:36 +02004565/* The function that actually does the error reporting */
4566static void verror(int severity, const char *fmt, va_list arg)
4567{
4568 char buff[1024];
4569
4570 vsnprintf(buff, sizeof(buff), fmt, arg);
4571
4572 if (istk && istk->mstk && istk->mstk->name)
H. Peter Anvindbb640b2009-07-18 18:57:16 -07004573 nasm_error(severity, "(%s:%d) %s", istk->mstk->name,
Victor van den Elzen3b404c02008-09-18 13:51:36 +02004574 istk->mstk->lineno, buff);
4575 else
H. Peter Anvindbb640b2009-07-18 18:57:16 -07004576 nasm_error(severity, "%s", buff);
Victor van den Elzen3b404c02008-09-18 13:51:36 +02004577}
4578
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004579/*
4580 * Since preprocessor always operate only on the line that didn't
H. Peter Anvin917a3492008-09-24 09:14:49 -07004581 * arrived yet, we should always use ERR_OFFBY1.
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004582 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004583static void error(int severity, const char *fmt, ...)
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004584{
4585 va_list arg;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004586
4587 /* If we're in a dead branch of IF or something like it, ignore the error */
H. Peter Anvin77ba0c62002-05-14 03:18:53 +00004588 if (istk && istk->conds && !emitting(istk->conds->state))
H. Peter Anvine2c80182005-01-15 22:15:51 +00004589 return;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004590
H. Peter Anvin734b1882002-04-30 21:01:08 +00004591 va_start(arg, fmt);
Victor van den Elzen3b404c02008-09-18 13:51:36 +02004592 verror(severity, fmt, arg);
H. Peter Anvin734b1882002-04-30 21:01:08 +00004593 va_end(arg);
Victor van den Elzen3b404c02008-09-18 13:51:36 +02004594}
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004595
Victor van den Elzen3b404c02008-09-18 13:51:36 +02004596/*
4597 * Because %else etc are evaluated in the state context
4598 * of the previous branch, errors might get lost with error():
4599 * %if 0 ... %else trailing garbage ... %endif
4600 * So %else etc should report errors with this function.
4601 */
4602static void error_precond(int severity, const char *fmt, ...)
4603{
4604 va_list arg;
4605
4606 /* Only ignore the error if it's really in a dead branch */
4607 if (istk && istk->conds && istk->conds->state == COND_NEVER)
4608 return;
4609
4610 va_start(arg, fmt);
4611 verror(severity, fmt, arg);
4612 va_end(arg);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004613}
4614
H. Peter Anvin734b1882002-04-30 21:01:08 +00004615static void
H. Peter Anvindbb640b2009-07-18 18:57:16 -07004616pp_reset(char *file, int apass, ListGen * listgen, StrList **deplist)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004617{
H. Peter Anvin7383b402008-09-24 10:20:40 -07004618 Token *t;
4619
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004620 cstk = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004621 istk = nasm_malloc(sizeof(Include));
4622 istk->next = NULL;
4623 istk->conds = NULL;
4624 istk->expansion = NULL;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004625 istk->mstk = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004626 istk->fp = fopen(file, "r");
H. Peter Anvineba20a72002-04-30 20:53:55 +00004627 istk->fname = NULL;
4628 src_set_fname(nasm_strdup(file));
4629 src_set_linnum(0);
4630 istk->lineinc = 1;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004631 if (!istk->fp)
H. Peter Anvin917a3492008-09-24 09:14:49 -07004632 error(ERR_FATAL|ERR_NOFILE, "unable to open input file `%s'",
H. Peter Anvine2c80182005-01-15 22:15:51 +00004633 file);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004634 defining = NULL;
Charles Crayned4200be2008-07-12 16:42:33 -07004635 nested_mac_count = 0;
4636 nested_rep_count = 0;
H. Peter Anvin97a23472007-09-16 17:57:25 -07004637 init_macros();
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004638 unique = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004639 if (tasm_compatible_mode) {
H. Peter Anvina4835d42008-05-20 14:21:29 -07004640 stdmacpos = nasm_stdmac;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004641 } else {
H. Peter Anvina4835d42008-05-20 14:21:29 -07004642 stdmacpos = nasm_stdmac_after_tasm;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004643 }
H. Peter Anvind2456592008-06-19 15:04:18 -07004644 any_extrastdmac = extrastdmac && *extrastdmac;
4645 do_predef = true;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004646 list = listgen;
H. Peter Anvin61f130f2008-09-25 15:45:06 -07004647
4648 /*
4649 * 0 for dependencies, 1 for preparatory passes, 2 for final pass.
4650 * The caller, however, will also pass in 3 for preprocess-only so
4651 * we can set __PASS__ accordingly.
4652 */
4653 pass = apass > 2 ? 2 : apass;
4654
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07004655 dephead = deptail = deplist;
4656 if (deplist) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004657 StrList *sl = nasm_malloc(strlen(file)+1+sizeof sl->next);
4658 sl->next = NULL;
4659 strcpy(sl->str, file);
4660 *deptail = sl;
4661 deptail = &sl->next;
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07004662 }
H. Peter Anvin7383b402008-09-24 10:20:40 -07004663
H. Peter Anvin61f130f2008-09-25 15:45:06 -07004664 /*
4665 * Define the __PASS__ macro. This is defined here unlike
4666 * all the other builtins, because it is special -- it varies between
4667 * passes.
4668 */
H. Peter Anvin7383b402008-09-24 10:20:40 -07004669 t = nasm_malloc(sizeof(*t));
4670 t->next = NULL;
H. Peter Anvin61f130f2008-09-25 15:45:06 -07004671 make_tok_num(t, apass);
H. Peter Anvin7383b402008-09-24 10:20:40 -07004672 t->a.mac = NULL;
4673 define_smacro(NULL, "__PASS__", true, 0, t);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004674}
4675
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004676static char *pp_getline(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004677{
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004678 char *line;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004679 Token *tline;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004680
H. Peter Anvine2c80182005-01-15 22:15:51 +00004681 while (1) {
4682 /*
Keith Kaniosb7a89542007-04-12 02:40:54 +00004683 * Fetch a tokenized line, either from the macro-expansion
H. Peter Anvine2c80182005-01-15 22:15:51 +00004684 * buffer or from the input file.
4685 */
4686 tline = NULL;
4687 while (istk->expansion && istk->expansion->finishes) {
4688 Line *l = istk->expansion;
4689 if (!l->finishes->name && l->finishes->in_progress > 1) {
4690 Line *ll;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004691
H. Peter Anvine2c80182005-01-15 22:15:51 +00004692 /*
4693 * This is a macro-end marker for a macro with no
4694 * name, which means it's not really a macro at all
4695 * but a %rep block, and the `in_progress' field is
4696 * more than 1, meaning that we still need to
4697 * repeat. (1 means the natural last repetition; 0
4698 * means termination by %exitrep.) We have
4699 * therefore expanded up to the %endrep, and must
4700 * push the whole block on to the expansion buffer
4701 * again. We don't bother to remove the macro-end
4702 * marker: we'd only have to generate another one
4703 * if we did.
4704 */
4705 l->finishes->in_progress--;
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04004706 list_for_each(l, l->finishes->expansion) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00004707 Token *t, *tt, **tail;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004708
H. Peter Anvine2c80182005-01-15 22:15:51 +00004709 ll = nasm_malloc(sizeof(Line));
4710 ll->next = istk->expansion;
4711 ll->finishes = NULL;
4712 ll->first = NULL;
4713 tail = &ll->first;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004714
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04004715 list_for_each(t, l->first) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00004716 if (t->text || t->type == TOK_WHITESPACE) {
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04004717 tt = *tail = new_Token(NULL, t->type, t->text, 0);
H. Peter Anvine2c80182005-01-15 22:15:51 +00004718 tail = &tt->next;
4719 }
4720 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00004721
H. Peter Anvine2c80182005-01-15 22:15:51 +00004722 istk->expansion = ll;
4723 }
4724 } else {
4725 /*
4726 * Check whether a `%rep' was started and not ended
4727 * within this macro expansion. This can happen and
4728 * should be detected. It's a fatal error because
4729 * I'm too confused to work out how to recover
4730 * sensibly from it.
4731 */
4732 if (defining) {
4733 if (defining->name)
4734 error(ERR_PANIC,
4735 "defining with name in expansion");
4736 else if (istk->mstk->name)
4737 error(ERR_FATAL,
4738 "`%%rep' without `%%endrep' within"
4739 " expansion of macro `%s'",
4740 istk->mstk->name);
4741 }
H. Peter Anvin87bc6192002-04-30 20:53:16 +00004742
H. Peter Anvine2c80182005-01-15 22:15:51 +00004743 /*
4744 * FIXME: investigate the relationship at this point between
4745 * istk->mstk and l->finishes
4746 */
4747 {
4748 MMacro *m = istk->mstk;
4749 istk->mstk = m->next_active;
4750 if (m->name) {
4751 /*
4752 * This was a real macro call, not a %rep, and
4753 * therefore the parameter information needs to
4754 * be freed.
4755 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004756 if (m->prev) {
4757 pop_mmacro(m);
4758 l->finishes->in_progress --;
4759 } else {
Keith Kanios891775e2009-07-11 06:08:54 -05004760 nasm_free(m->params);
4761 free_tlist(m->iline);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004762 nasm_free(m->paramlen);
4763 l->finishes->in_progress = 0;
4764 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00004765 } else
4766 free_mmacro(m);
4767 }
4768 istk->expansion = l->next;
4769 nasm_free(l);
4770 list->downlevel(LIST_MACRO);
4771 }
4772 }
4773 while (1) { /* until we get a line we can use */
H. Peter Anvineba20a72002-04-30 20:53:55 +00004774
H. Peter Anvine2c80182005-01-15 22:15:51 +00004775 if (istk->expansion) { /* from a macro expansion */
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004776 char *p;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004777 Line *l = istk->expansion;
4778 if (istk->mstk)
4779 istk->mstk->lineno++;
4780 tline = l->first;
4781 istk->expansion = l->next;
4782 nasm_free(l);
H. Peter Anvin6867acc2007-10-10 14:58:45 -07004783 p = detoken(tline, false);
H. Peter Anvine2c80182005-01-15 22:15:51 +00004784 list->line(LIST_MACRO, p);
4785 nasm_free(p);
4786 break;
4787 }
4788 line = read_line();
4789 if (line) { /* from the current input file */
4790 line = prepreproc(line);
Keith Kaniosb7a89542007-04-12 02:40:54 +00004791 tline = tokenize(line);
H. Peter Anvine2c80182005-01-15 22:15:51 +00004792 nasm_free(line);
4793 break;
4794 }
4795 /*
4796 * The current file has ended; work down the istk
4797 */
4798 {
4799 Include *i = istk;
4800 fclose(i->fp);
4801 if (i->conds)
4802 error(ERR_FATAL,
4803 "expected `%%endif' before end of file");
4804 /* only set line and file name if there's a next node */
4805 if (i->next) {
4806 src_set_linnum(i->lineno);
4807 nasm_free(src_set_fname(i->fname));
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004808 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00004809 istk = i->next;
4810 list->downlevel(LIST_INCLUDE);
4811 nasm_free(i);
4812 if (!istk)
4813 return NULL;
Victor van den Elzen4c9d6222008-10-01 13:08:50 +02004814 if (istk->expansion && istk->expansion->finishes)
4815 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004816 }
4817 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004818
H. Peter Anvine2c80182005-01-15 22:15:51 +00004819 /*
4820 * We must expand MMacro parameters and MMacro-local labels
4821 * _before_ we plunge into directive processing, to cope
4822 * with things like `%define something %1' such as STRUC
4823 * uses. Unless we're _defining_ a MMacro, in which case
4824 * those tokens should be left alone to go into the
4825 * definition; and unless we're in a non-emitting
4826 * condition, in which case we don't want to meddle with
4827 * anything.
4828 */
Charles Crayned4200be2008-07-12 16:42:33 -07004829 if (!defining && !(istk->conds && !emitting(istk->conds->state))
H. Peter Anvin992fe752008-10-19 15:45:05 -07004830 && !(istk->mstk && !istk->mstk->in_progress)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00004831 tline = expand_mmac_params(tline);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004832 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00004833
H. Peter Anvine2c80182005-01-15 22:15:51 +00004834 /*
4835 * Check the line to see if it's a preprocessor directive.
4836 */
4837 if (do_directive(tline) == DIRECTIVE_FOUND) {
4838 continue;
4839 } else if (defining) {
4840 /*
4841 * We're defining a multi-line macro. We emit nothing
4842 * at all, and just
Keith Kaniosb7a89542007-04-12 02:40:54 +00004843 * shove the tokenized line on to the macro definition.
H. Peter Anvine2c80182005-01-15 22:15:51 +00004844 */
4845 Line *l = nasm_malloc(sizeof(Line));
4846 l->next = defining->expansion;
4847 l->first = tline;
H. Peter Anvin538002d2008-06-28 18:30:27 -07004848 l->finishes = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004849 defining->expansion = l;
4850 continue;
4851 } else if (istk->conds && !emitting(istk->conds->state)) {
4852 /*
4853 * We're in a non-emitting branch of a condition block.
4854 * Emit nothing at all, not even a blank line: when we
4855 * emerge from the condition we'll give a line-number
4856 * directive so we keep our place correctly.
4857 */
4858 free_tlist(tline);
4859 continue;
4860 } else if (istk->mstk && !istk->mstk->in_progress) {
4861 /*
4862 * We're in a %rep block which has been terminated, so
4863 * we're walking through to the %endrep without
4864 * emitting anything. Emit nothing at all, not even a
4865 * blank line: when we emerge from the %rep block we'll
4866 * give a line-number directive so we keep our place
4867 * correctly.
4868 */
4869 free_tlist(tline);
4870 continue;
4871 } else {
4872 tline = expand_smacro(tline);
4873 if (!expand_mmacro(tline)) {
4874 /*
Keith Kaniosb7a89542007-04-12 02:40:54 +00004875 * De-tokenize the line again, and emit it.
H. Peter Anvine2c80182005-01-15 22:15:51 +00004876 */
H. Peter Anvin6867acc2007-10-10 14:58:45 -07004877 line = detoken(tline, true);
H. Peter Anvine2c80182005-01-15 22:15:51 +00004878 free_tlist(tline);
4879 break;
4880 } else {
4881 continue; /* expand_mmacro calls free_tlist */
4882 }
4883 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004884 }
4885
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004886 return line;
4887}
4888
H. Peter Anvine2c80182005-01-15 22:15:51 +00004889static void pp_cleanup(int pass)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004890{
H. Peter Anvine2c80182005-01-15 22:15:51 +00004891 if (defining) {
H. Peter Anvin89cee572009-07-15 09:16:54 -04004892 if (defining->name) {
Victor van den Elzen8f1120f2008-07-16 13:41:37 +02004893 error(ERR_NONFATAL,
4894 "end of file while still defining macro `%s'",
4895 defining->name);
4896 } else {
4897 error(ERR_NONFATAL, "end of file while still in %%rep");
4898 }
4899
H. Peter Anvine2c80182005-01-15 22:15:51 +00004900 free_mmacro(defining);
Cyrill Gorcunova327c652010-02-14 17:19:38 +03004901 defining = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004902 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004903 while (cstk)
H. Peter Anvine2c80182005-01-15 22:15:51 +00004904 ctx_pop();
H. Peter Anvin97a23472007-09-16 17:57:25 -07004905 free_macros();
H. Peter Anvine2c80182005-01-15 22:15:51 +00004906 while (istk) {
4907 Include *i = istk;
4908 istk = istk->next;
4909 fclose(i->fp);
4910 nasm_free(i->fname);
4911 nasm_free(i);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004912 }
4913 while (cstk)
H. Peter Anvine2c80182005-01-15 22:15:51 +00004914 ctx_pop();
H. Peter Anvin86877b22008-06-20 15:55:45 -07004915 nasm_free(src_set_fname(NULL));
H. Peter Anvine2c80182005-01-15 22:15:51 +00004916 if (pass == 0) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004917 IncPath *i;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004918 free_llist(predef);
4919 delete_Blocks();
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004920 while ((i = ipath)) {
4921 ipath = i->next;
4922 if (i->path)
4923 nasm_free(i->path);
4924 nasm_free(i);
4925 }
H. Peter Anvin11dfa1a2008-07-02 18:11:04 -07004926 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004927}
4928
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004929void pp_include_path(char *path)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004930{
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004931 IncPath *i;
H. Peter Anvin37a321f2007-09-24 13:41:58 -07004932
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004933 i = nasm_malloc(sizeof(IncPath));
H. Peter Anvin37a321f2007-09-24 13:41:58 -07004934 i->path = path ? nasm_strdup(path) : NULL;
Frank Kotlerd0ed6fd2003-08-27 11:33:56 +00004935 i->next = NULL;
4936
H. Peter Anvin89cee572009-07-15 09:16:54 -04004937 if (ipath) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00004938 IncPath *j = ipath;
H. Peter Anvin89cee572009-07-15 09:16:54 -04004939 while (j->next)
H. Peter Anvine2c80182005-01-15 22:15:51 +00004940 j = j->next;
4941 j->next = i;
4942 } else {
4943 ipath = i;
Frank Kotlerd0ed6fd2003-08-27 11:33:56 +00004944 }
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004945}
Frank Kotlerd0ed6fd2003-08-27 11:33:56 +00004946
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004947void pp_pre_include(char *fname)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004948{
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004949 Token *inc, *space, *name;
4950 Line *l;
4951
H. Peter Anvin734b1882002-04-30 21:01:08 +00004952 name = new_Token(NULL, TOK_INTERNAL_STRING, fname, 0);
4953 space = new_Token(name, TOK_WHITESPACE, NULL, 0);
4954 inc = new_Token(space, TOK_PREPROC_ID, "%include", 0);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004955
4956 l = nasm_malloc(sizeof(Line));
4957 l->next = predef;
4958 l->first = inc;
H. Peter Anvin538002d2008-06-28 18:30:27 -07004959 l->finishes = NULL;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004960 predef = l;
4961}
4962
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004963void pp_pre_define(char *definition)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004964{
4965 Token *def, *space;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004966 Line *l;
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004967 char *equals;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004968
4969 equals = strchr(definition, '=');
H. Peter Anvin734b1882002-04-30 21:01:08 +00004970 space = new_Token(NULL, TOK_WHITESPACE, NULL, 0);
4971 def = new_Token(space, TOK_PREPROC_ID, "%define", 0);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004972 if (equals)
H. Peter Anvine2c80182005-01-15 22:15:51 +00004973 *equals = ' ';
Keith Kaniosb7a89542007-04-12 02:40:54 +00004974 space->next = tokenize(definition);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004975 if (equals)
H. Peter Anvine2c80182005-01-15 22:15:51 +00004976 *equals = '=';
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004977
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004978 l = nasm_malloc(sizeof(Line));
4979 l->next = predef;
4980 l->first = def;
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_undefine(char *definition)
H. Peter Anvin620515a2002-04-30 20:57:38 +00004986{
4987 Token *def, *space;
4988 Line *l;
4989
H. Peter Anvin734b1882002-04-30 21:01:08 +00004990 space = new_Token(NULL, TOK_WHITESPACE, NULL, 0);
4991 def = new_Token(space, TOK_PREPROC_ID, "%undef", 0);
Keith Kaniosb7a89542007-04-12 02:40:54 +00004992 space->next = tokenize(definition);
H. Peter Anvin620515a2002-04-30 20:57:38 +00004993
4994 l = nasm_malloc(sizeof(Line));
4995 l->next = predef;
4996 l->first = def;
H. Peter Anvin538002d2008-06-28 18:30:27 -07004997 l->finishes = NULL;
H. Peter Anvin620515a2002-04-30 20:57:38 +00004998 predef = l;
4999}
5000
Keith Kaniosb7a89542007-04-12 02:40:54 +00005001/*
5002 * Added by Keith Kanios:
5003 *
5004 * This function is used to assist with "runtime" preprocessor
5005 * directives. (e.g. pp_runtime("%define __BITS__ 64");)
5006 *
5007 * ERRORS ARE IGNORED HERE, SO MAKE COMPLETELY SURE THAT YOU
5008 * PASS A VALID STRING TO THIS FUNCTION!!!!!
5009 */
5010
Keith Kaniosa6dfa782007-04-13 16:47:53 +00005011void pp_runtime(char *definition)
Keith Kaniosb7a89542007-04-12 02:40:54 +00005012{
5013 Token *def;
H. Peter Anvin70653092007-10-19 14:42:29 -07005014
Keith Kaniosb7a89542007-04-12 02:40:54 +00005015 def = tokenize(definition);
H. Peter Anvin89cee572009-07-15 09:16:54 -04005016 if (do_directive(def) == NO_DIRECTIVE_FOUND)
Keith Kaniosb7a89542007-04-12 02:40:54 +00005017 free_tlist(def);
H. Peter Anvin70653092007-10-19 14:42:29 -07005018
Keith Kaniosb7a89542007-04-12 02:40:54 +00005019}
5020
H. Peter Anvina70547f2008-07-19 21:44:26 -07005021void pp_extra_stdmac(macros_t *macros)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005022{
H. Peter Anvin76690a12002-04-30 20:52:49 +00005023 extrastdmac = macros;
5024}
5025
Keith Kaniosa5fc6462007-10-13 07:09:22 -07005026static void make_tok_num(Token * tok, int64_t val)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005027{
Keith Kaniosa6dfa782007-04-13 16:47:53 +00005028 char numbuf[20];
Keith Kaniosa5fc6462007-10-13 07:09:22 -07005029 snprintf(numbuf, sizeof(numbuf), "%"PRId64"", val);
H. Peter Anvineba20a72002-04-30 20:53:55 +00005030 tok->text = nasm_strdup(numbuf);
5031 tok->type = TOK_NUMBER;
5032}
5033
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005034Preproc nasmpp = {
5035 pp_reset,
5036 pp_getline,
5037 pp_cleanup
5038};