blob: feccc33ac2ff30920906319ce1c6ed82c49c98c0 [file] [log] [blame]
H. Peter Anvin9e6747c2009-06-28 17:13:04 -07001/* ----------------------------------------------------------------------- *
2 *
3 * Copyright 1996-2009 The NASM Authors - All Rights Reserved
4 * 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.
17 *
18 * 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;
H. Peter Anvin89cee572009-07-15 09:16:54 -0400135 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;
139 bool plus; /* is the last parameter greedy? */
140 bool nolist; /* is this macro listing-inhibited? */
Keith Kanios891775e2009-07-11 06:08:54 -0500141 int64_t in_progress; /* is this macro currently being expanded? */
H. Peter Anvin89cee572009-07-15 09:16:54 -0400142 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 */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000156};
157
Keith Kanios891775e2009-07-11 06:08:54 -0500158
159/* Store the definition of a multi-line macro, as defined in a
160 * previous recursive macro expansion.
161 */
162struct MMacroInvocation {
H. Peter Anvin89cee572009-07-15 09:16:54 -0400163 MMacroInvocation *prev; /* previous invocation */
164 Token **params; /* actual parameters */
165 Token *iline; /* invocation line */
Keith Kanios891775e2009-07-11 06:08:54 -0500166 unsigned int nparam, rotate;
167 int *paramlen;
168 uint64_t unique;
169};
170
171
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000172/*
173 * The context stack is composed of a linked list of these.
174 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000175struct Context {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000176 Context *next;
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000177 char *name;
H. Peter Anvin166c2472008-05-28 12:28:58 -0700178 struct hash_table localmac;
Keith Kaniosb7a89542007-04-12 02:40:54 +0000179 uint32_t number;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000180};
181
182/*
183 * This is the internal form which we break input lines up into.
184 * Typically stored in linked lists.
185 *
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000186 * Note that `type' serves a double meaning: TOK_SMAC_PARAM is not
187 * necessarily used as-is, but is intended to denote the number of
188 * the substituted parameter. So in the definition
189 *
190 * %define a(x,y) ( (x) & ~(y) )
H. Peter Anvin70653092007-10-19 14:42:29 -0700191 *
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000192 * the token representing `x' will have its type changed to
193 * TOK_SMAC_PARAM, but the one representing `y' will be
194 * TOK_SMAC_PARAM+1.
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000195 *
196 * TOK_INTERNAL_STRING is a dirty hack: it's a single string token
197 * which doesn't need quotes around it. Used in the pre-include
198 * mechanism as an alternative to trying to find a sensible type of
199 * quote to use on the filename we were passed.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000200 */
H. Peter Anvinda10e7b2007-09-12 04:18:37 +0000201enum pp_token_type {
202 TOK_NONE = 0, TOK_WHITESPACE, TOK_COMMENT, TOK_ID,
203 TOK_PREPROC_ID, TOK_STRING,
H. Peter Anvin6c81f0a2008-05-25 21:46:17 -0700204 TOK_NUMBER, TOK_FLOAT, TOK_SMAC_END, TOK_OTHER,
205 TOK_INTERNAL_STRING,
206 TOK_PREPROC_Q, TOK_PREPROC_QQ,
H. Peter Anvind784a082009-04-20 14:01:18 -0700207 TOK_PASTE, /* %+ */
H. Peter Anvin9bb46df2009-04-07 21:59:24 -0700208 TOK_INDIRECT, /* %[...] */
H. Peter Anvin5b76fa22008-05-26 11:14:38 -0700209 TOK_SMAC_PARAM, /* MUST BE LAST IN THE LIST!!! */
210 TOK_MAX = INT_MAX /* Keep compiler from reducing the range */
H. Peter Anvinda10e7b2007-09-12 04:18:37 +0000211};
212
H. Peter Anvine2c80182005-01-15 22:15:51 +0000213struct Token {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000214 Token *next;
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000215 char *text;
H. Peter Anvinf26e0972008-07-01 21:26:27 -0700216 union {
217 SMacro *mac; /* associated macro for TOK_SMAC_END */
218 size_t len; /* scratch length field */
219 } a; /* Auxiliary data */
H. Peter Anvinda10e7b2007-09-12 04:18:37 +0000220 enum pp_token_type type;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000221};
222
223/*
224 * Multi-line macro definitions are stored as a linked list of
225 * these, which is essentially a container to allow several linked
226 * lists of Tokens.
H. Peter Anvin70653092007-10-19 14:42:29 -0700227 *
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000228 * Note that in this module, linked lists are treated as stacks
229 * wherever possible. For this reason, Lines are _pushed_ on to the
230 * `expansion' field in MMacro structures, so that the linked list,
231 * if walked, would give the macro lines in reverse order; this
232 * means that we can walk the list when expanding a macro, and thus
233 * push the lines on to the `expansion' field in _istk_ in reverse
234 * order (so that when popped back off they are in the right
235 * order). It may seem cockeyed, and it relies on my design having
236 * an even number of steps in, but it works...
237 *
238 * Some of these structures, rather than being actual lines, are
239 * markers delimiting the end of the expansion of a given macro.
H. Peter Anvin76690a12002-04-30 20:52:49 +0000240 * This is for use in the cycle-tracking and %rep-handling code.
241 * Such structures have `finishes' non-NULL, and `first' NULL. All
242 * others have `finishes' NULL, but `first' may still be NULL if
243 * the line is blank.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000244 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000245struct Line {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000246 Line *next;
247 MMacro *finishes;
248 Token *first;
249};
250
251/*
252 * To handle an arbitrary level of file inclusion, we maintain a
253 * stack (ie linked list) of these things.
254 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000255struct Include {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000256 Include *next;
257 FILE *fp;
258 Cond *conds;
259 Line *expansion;
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000260 char *fname;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000261 int lineno, lineinc;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000262 MMacro *mstk; /* stack of active macros/reps */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000263};
264
265/*
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000266 * Include search path. This is simply a list of strings which get
267 * prepended, in turn, to the name of an include file, in an
268 * attempt to find the file if it's not in the current directory.
269 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000270struct IncPath {
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000271 IncPath *next;
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000272 char *path;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000273};
274
275/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000276 * Conditional assembly: we maintain a separate stack of these for
277 * each level of file inclusion. (The only reason we keep the
278 * stacks separate is to ensure that a stray `%endif' in a file
279 * included from within the true branch of a `%if' won't terminate
280 * it and cause confusion: instead, rightly, it'll cause an error.)
281 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000282struct Cond {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000283 Cond *next;
284 int state;
285};
H. Peter Anvine2c80182005-01-15 22:15:51 +0000286enum {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000287 /*
288 * These states are for use just after %if or %elif: IF_TRUE
289 * means the condition has evaluated to truth so we are
290 * currently emitting, whereas IF_FALSE means we are not
291 * currently emitting but will start doing so if a %else comes
292 * up. In these states, all directives are admissible: %elif,
293 * %else and %endif. (And of course %if.)
294 */
295 COND_IF_TRUE, COND_IF_FALSE,
296 /*
297 * These states come up after a %else: ELSE_TRUE means we're
298 * emitting, and ELSE_FALSE means we're not. In ELSE_* states,
299 * any %elif or %else will cause an error.
300 */
301 COND_ELSE_TRUE, COND_ELSE_FALSE,
302 /*
Victor van den Elzen3b404c02008-09-18 13:51:36 +0200303 * These states mean that we're not emitting now, and also that
304 * nothing until %endif will be emitted at all. COND_DONE is
305 * used when we've had our moment of emission
306 * and have now started seeing %elifs. COND_NEVER is used when
307 * the condition construct in question is contained within a
308 * non-emitting branch of a larger condition construct,
309 * or if there is an error.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000310 */
Victor van den Elzen3b404c02008-09-18 13:51:36 +0200311 COND_DONE, COND_NEVER
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000312};
313#define emitting(x) ( (x) == COND_IF_TRUE || (x) == COND_ELSE_TRUE )
314
H. Peter Anvin70653092007-10-19 14:42:29 -0700315/*
Ed Beroset3ab3f412002-06-11 03:31:49 +0000316 * These defines are used as the possible return values for do_directive
317 */
318#define NO_DIRECTIVE_FOUND 0
319#define DIRECTIVE_FOUND 1
320
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000321/*
Keith Kanios852f1ee2009-07-12 00:19:55 -0500322 * This define sets the upper limit for smacro and recursive mmacro
323 * expansions
324 */
325#define DEADMAN_LIMIT (1 << 20)
326
327/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000328 * Condition codes. Note that we use c_ prefix not C_ because C_ is
329 * used in nasm.h for the "real" condition codes. At _this_ level,
330 * we treat CXZ and ECXZ as condition codes, albeit non-invertible
331 * ones, so we need a different enum...
332 */
H. Peter Anvin476d2862007-10-02 22:04:15 -0700333static const char * const conditions[] = {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000334 "a", "ae", "b", "be", "c", "cxz", "e", "ecxz", "g", "ge", "l", "le",
335 "na", "nae", "nb", "nbe", "nc", "ne", "ng", "nge", "nl", "nle", "no",
H. Peter Anvince9be342007-09-12 00:22:29 +0000336 "np", "ns", "nz", "o", "p", "pe", "po", "rcxz", "s", "z"
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000337};
H. Peter Anvin476d2862007-10-02 22:04:15 -0700338enum pp_conds {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000339 c_A, c_AE, c_B, c_BE, c_C, c_CXZ, c_E, c_ECXZ, c_G, c_GE, c_L, c_LE,
340 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 -0700341 c_NP, c_NS, c_NZ, c_O, c_P, c_PE, c_PO, c_RCXZ, c_S, c_Z,
342 c_none = -1
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000343};
H. Peter Anvin476d2862007-10-02 22:04:15 -0700344static const enum pp_conds inverse_ccs[] = {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000345 c_NA, c_NAE, c_NB, c_NBE, c_NC, -1, c_NE, -1, c_NG, c_NGE, c_NL, c_NLE,
346 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 +0000347 c_Z, c_NO, c_NP, c_PO, c_PE, -1, c_NS, c_NZ
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000348};
349
H. Peter Anvin76690a12002-04-30 20:52:49 +0000350/*
351 * Directive names.
352 */
H. Peter Anvin65747262002-05-07 00:10:05 +0000353/* If this is a an IF, ELIF, ELSE or ENDIF keyword */
H. Peter Anvin9bf0aa72007-09-12 02:12:07 +0000354static int is_condition(enum preproc_token arg)
H. Peter Anvin65747262002-05-07 00:10:05 +0000355{
H. Peter Anvinda10e7b2007-09-12 04:18:37 +0000356 return PP_IS_COND(arg) || (arg == PP_ELSE) || (arg == PP_ENDIF);
H. Peter Anvin65747262002-05-07 00:10:05 +0000357}
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000358
359/* For TASM compatibility we need to be able to recognise TASM compatible
360 * conditional compilation directives. Using the NASM pre-processor does
361 * not work, so we look for them specifically from the following list and
362 * then jam in the equivalent NASM directive into the input stream.
363 */
364
H. Peter Anvine2c80182005-01-15 22:15:51 +0000365enum {
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000366 TM_ARG, TM_ELIF, TM_ELSE, TM_ENDIF, TM_IF, TM_IFDEF, TM_IFDIFI,
367 TM_IFNDEF, TM_INCLUDE, TM_LOCAL
368};
369
H. Peter Anvin476d2862007-10-02 22:04:15 -0700370static const char * const tasm_directives[] = {
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000371 "arg", "elif", "else", "endif", "if", "ifdef", "ifdifi",
372 "ifndef", "include", "local"
373};
374
375static int StackSize = 4;
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000376static char *StackPointer = "ebp";
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000377static int ArgOffset = 8;
H. Peter Anvin8781cb02007-11-08 20:01:11 -0800378static int LocalOffset = 0;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000379
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000380static Context *cstk;
381static Include *istk;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000382static IncPath *ipath = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000383
H. Peter Anvine2c80182005-01-15 22:15:51 +0000384static int pass; /* HACK: pass 0 = generate dependencies only */
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700385static StrList **dephead, **deptail; /* Dependency list */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000386
H. Peter Anvinf8ba53e2007-10-11 10:11:57 -0700387static uint64_t unique; /* unique identifier numbers */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000388
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000389static Line *predef = NULL;
H. Peter Anvind2456592008-06-19 15:04:18 -0700390static bool do_predef;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000391
392static ListGen *list;
393
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000394/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000395 * The current set of multi-line macros we have defined.
396 */
H. Peter Anvin166c2472008-05-28 12:28:58 -0700397static struct hash_table mmacros;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000398
399/*
400 * The current set of single-line macros we have defined.
401 */
H. Peter Anvin166c2472008-05-28 12:28:58 -0700402static struct hash_table smacros;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000403
404/*
H. Peter Anvin76690a12002-04-30 20:52:49 +0000405 * The multi-line macro we are currently defining, or the %rep
406 * block we are currently reading, if any.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000407 */
408static MMacro *defining;
409
Charles Crayned4200be2008-07-12 16:42:33 -0700410static uint64_t nested_mac_count;
411static uint64_t nested_rep_count;
412
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000413/*
414 * The number of macro parameters to allocate space for at a time.
415 */
416#define PARAM_DELTA 16
417
418/*
H. Peter Anvina4835d42008-05-20 14:21:29 -0700419 * The standard macro set: defined in macros.c in the array nasm_stdmac.
420 * This gives our position in the macro set, when we're processing it.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000421 */
H. Peter Anvina70547f2008-07-19 21:44:26 -0700422static macros_t *stdmacpos;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000423
424/*
H. Peter Anvin76690a12002-04-30 20:52:49 +0000425 * The extra standard macros that come from the object format, if
426 * any.
427 */
H. Peter Anvina70547f2008-07-19 21:44:26 -0700428static macros_t *extrastdmac = NULL;
H. Peter Anvincfb71762008-06-20 15:20:16 -0700429static bool any_extrastdmac;
H. Peter Anvin76690a12002-04-30 20:52:49 +0000430
431/*
H. Peter Anvin734b1882002-04-30 21:01:08 +0000432 * Tokens are allocated in blocks to improve speed
433 */
434#define TOKEN_BLOCKSIZE 4096
435static Token *freeTokens = NULL;
H. Peter Anvince616072002-04-30 21:02:23 +0000436struct Blocks {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000437 Blocks *next;
438 void *chunk;
H. Peter Anvince616072002-04-30 21:02:23 +0000439};
440
441static Blocks blocks = { NULL, NULL };
H. Peter Anvin734b1882002-04-30 21:01:08 +0000442
443/*
H. Peter Anvin76690a12002-04-30 20:52:49 +0000444 * Forward declarations.
445 */
H. Peter Anvin734b1882002-04-30 21:01:08 +0000446static Token *expand_mmac_params(Token * tline);
447static Token *expand_smacro(Token * tline);
448static Token *expand_id(Token * tline);
H. Peter Anvinf8ad5322009-02-21 17:55:08 -0800449static Context *get_ctx(const char *name, const char **namep,
450 bool all_contexts);
Keith Kaniosa5fc6462007-10-13 07:09:22 -0700451static void make_tok_num(Token * tok, int64_t val);
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000452static void error(int severity, const char *fmt, ...);
Victor van den Elzen3b404c02008-09-18 13:51:36 +0200453static void error_precond(int severity, const char *fmt, ...);
H. Peter Anvince616072002-04-30 21:02:23 +0000454static void *new_Block(size_t size);
455static void delete_Blocks(void);
H. Peter Anvinc751e862008-06-09 10:18:45 -0700456static Token *new_Token(Token * next, enum pp_token_type type,
457 const char *text, int txtlen);
H. Peter Anvin734b1882002-04-30 21:01:08 +0000458static Token *delete_Token(Token * t);
H. Peter Anvineba20a72002-04-30 20:53:55 +0000459
460/*
461 * Macros for safe checking of token pointers, avoid *(NULL)
462 */
463#define tok_type_(x,t) ((x) && (x)->type == (t))
464#define skip_white_(x) if (tok_type_((x), TOK_WHITESPACE)) (x)=(x)->next
465#define tok_is_(x,v) (tok_type_((x), TOK_OTHER) && !strcmp((x)->text,(v)))
466#define tok_isnt_(x,v) ((x) && ((x)->type!=TOK_OTHER || strcmp((x)->text,(v))))
H. Peter Anvin76690a12002-04-30 20:52:49 +0000467
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000468/* Handle TASM specific directives, which do not contain a % in
469 * front of them. We do it here because I could not find any other
470 * place to do it for the moment, and it is a hack (ideally it would
471 * be nice to be able to use the NASM pre-processor to do it).
472 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000473static char *check_tasm_directive(char *line)
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000474{
Keith Kaniosb7a89542007-04-12 02:40:54 +0000475 int32_t i, j, k, m, len;
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000476 char *p = line, *oldline, oldchar;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000477
478 /* Skip whitespace */
H. Peter Anvinbda7a6e2008-06-21 10:23:17 -0700479 while (nasm_isspace(*p) && *p != 0)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000480 p++;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000481
482 /* Binary search for the directive name */
483 i = -1;
Ed Beroset3ab3f412002-06-11 03:31:49 +0000484 j = elements(tasm_directives);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000485 len = 0;
H. Peter Anvinbda7a6e2008-06-21 10:23:17 -0700486 while (!nasm_isspace(p[len]) && p[len] != 0)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000487 len++;
488 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 /*
505 * NASM does not recognise IFDIFI, so we convert
506 * it to %if 0. This is not used in NASM
507 * compatible code, but does need to parse for the
508 * TASM macro package.
H. Peter Anvine2c80182005-01-15 22:15:51 +0000509 */
H. Peter Anvin18f48792009-06-27 15:56:27 -0700510 strcpy(line + 1, "if 0");
H. Peter Anvine2c80182005-01-15 22:15:51 +0000511 } else {
512 memcpy(line + 1, p, len + 1);
513 }
514 nasm_free(oldline);
515 return line;
516 } else if (m < 0) {
517 j = k;
518 } else
519 i = k;
520 }
521 p[len] = oldchar;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000522 }
523 return line;
524}
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000525
H. Peter Anvin76690a12002-04-30 20:52:49 +0000526/*
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000527 * The pre-preprocessing stage... This function translates line
528 * number indications as they emerge from GNU cpp (`# lineno "file"
529 * flags') into NASM preprocessor line number indications (`%line
530 * lineno file').
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000531 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000532static char *prepreproc(char *line)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000533{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000534 int lineno, fnlen;
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000535 char *fname, *oldline;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000536
H. Peter Anvine2c80182005-01-15 22:15:51 +0000537 if (line[0] == '#' && line[1] == ' ') {
538 oldline = line;
539 fname = oldline + 2;
540 lineno = atoi(fname);
541 fname += strspn(fname, "0123456789 ");
542 if (*fname == '"')
543 fname++;
544 fnlen = strcspn(fname, "\"");
545 line = nasm_malloc(20 + fnlen);
546 snprintf(line, 20 + fnlen, "%%line %d %.*s", lineno, fnlen, fname);
547 nasm_free(oldline);
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000548 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000549 if (tasm_compatible_mode)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000550 return check_tasm_directive(line);
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000551 return line;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000552}
553
554/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000555 * Free a linked list of tokens.
556 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000557static void free_tlist(Token * list)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000558{
H. Peter Anvine2c80182005-01-15 22:15:51 +0000559 while (list) {
560 list = delete_Token(list);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000561 }
562}
563
564/*
565 * Free a linked list of lines.
566 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000567static void free_llist(Line * list)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000568{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000569 Line *l;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000570 while (list) {
571 l = list;
572 list = list->next;
573 free_tlist(l->first);
574 nasm_free(l);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000575 }
576}
577
578/*
H. Peter Anvineba20a72002-04-30 20:53:55 +0000579 * Free an MMacro
580 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000581static void free_mmacro(MMacro * m)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000582{
H. Peter Anvin734b1882002-04-30 21:01:08 +0000583 nasm_free(m->name);
584 free_tlist(m->dlist);
585 nasm_free(m->defaults);
586 free_llist(m->expansion);
587 nasm_free(m);
H. Peter Anvineba20a72002-04-30 20:53:55 +0000588}
589
590/*
H. Peter Anvin97a23472007-09-16 17:57:25 -0700591 * Free all currently defined macros, and free the hash tables
592 */
H. Peter Anvin072771e2008-05-22 13:17:51 -0700593static void free_smacro_table(struct hash_table *smt)
H. Peter Anvin97a23472007-09-16 17:57:25 -0700594{
H. Peter Anvin97a23472007-09-16 17:57:25 -0700595 SMacro *s;
H. Peter Anvin072771e2008-05-22 13:17:51 -0700596 const char *key;
597 struct hash_tbl_node *it = NULL;
H. Peter Anvin97a23472007-09-16 17:57:25 -0700598
H. Peter Anvin072771e2008-05-22 13:17:51 -0700599 while ((s = hash_iterate(smt, &it, &key)) != NULL) {
H. Peter Anvin97a23472007-09-16 17:57:25 -0700600 nasm_free((void *)key);
601 while (s) {
602 SMacro *ns = s->next;
603 nasm_free(s->name);
604 free_tlist(s->expansion);
605 nasm_free(s);
606 s = ns;
607 }
608 }
H. Peter Anvin072771e2008-05-22 13:17:51 -0700609 hash_free(smt);
610}
611
612static void free_mmacro_table(struct hash_table *mmt)
613{
614 MMacro *m;
615 const char *key;
616 struct hash_tbl_node *it = NULL;
H. Peter Anvin97a23472007-09-16 17:57:25 -0700617
618 it = NULL;
H. Peter Anvin072771e2008-05-22 13:17:51 -0700619 while ((m = hash_iterate(mmt, &it, &key)) != NULL) {
H. Peter Anvin97a23472007-09-16 17:57:25 -0700620 nasm_free((void *)key);
621 while (m) {
622 MMacro *nm = m->next;
623 free_mmacro(m);
624 m = nm;
625 }
626 }
H. Peter Anvin072771e2008-05-22 13:17:51 -0700627 hash_free(mmt);
628}
629
630static void free_macros(void)
631{
H. Peter Anvin166c2472008-05-28 12:28:58 -0700632 free_smacro_table(&smacros);
633 free_mmacro_table(&mmacros);
H. Peter Anvin97a23472007-09-16 17:57:25 -0700634}
635
636/*
637 * Initialize the hash tables
638 */
639static void init_macros(void)
640{
H. Peter Anvin166c2472008-05-28 12:28:58 -0700641 hash_init(&smacros, HASH_LARGE);
642 hash_init(&mmacros, HASH_LARGE);
H. Peter Anvin97a23472007-09-16 17:57:25 -0700643}
644
645/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000646 * Pop the context stack.
647 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000648static void ctx_pop(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000649{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000650 Context *c = cstk;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000651
652 cstk = cstk->next;
H. Peter Anvin166c2472008-05-28 12:28:58 -0700653 free_smacro_table(&c->localmac);
H. Peter Anvin734b1882002-04-30 21:01:08 +0000654 nasm_free(c->name);
655 nasm_free(c);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000656}
657
H. Peter Anvin072771e2008-05-22 13:17:51 -0700658/*
659 * Search for a key in the hash index; adding it if necessary
660 * (in which case we initialize the data pointer to NULL.)
661 */
662static void **
663hash_findi_add(struct hash_table *hash, const char *str)
664{
665 struct hash_insert hi;
666 void **r;
667 char *strx;
668
669 r = hash_findi(hash, str, &hi);
670 if (r)
671 return r;
672
673 strx = nasm_strdup(str); /* Use a more efficient allocator here? */
674 return hash_add(&hi, strx, NULL);
675}
676
677/*
678 * Like hash_findi, but returns the data element rather than a pointer
679 * to it. Used only when not adding a new element, hence no third
680 * argument.
681 */
682static void *
683hash_findix(struct hash_table *hash, const char *str)
684{
685 void **p;
686
687 p = hash_findi(hash, str, NULL);
688 return p ? *p : NULL;
689}
690
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000691#define BUF_DELTA 512
692/*
693 * Read a line from the top file in istk, handling multiple CR/LFs
694 * at the end of the line read, and handling spurious ^Zs. Will
695 * return lines from the standard macro set if this has not already
696 * been done.
697 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000698static char *read_line(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000699{
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000700 char *buffer, *p, *q;
H. Peter Anvin9f394642002-04-30 21:07:51 +0000701 int bufsize, continued_count;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000702
H. Peter Anvine2c80182005-01-15 22:15:51 +0000703 if (stdmacpos) {
H. Peter Anvin72edbb82008-06-19 16:00:04 -0700704 unsigned char c;
H. Peter Anvin7e50d232008-06-25 14:54:14 -0700705 const unsigned char *p = stdmacpos;
H. Peter Anvin72edbb82008-06-19 16:00:04 -0700706 char *ret, *q;
H. Peter Anvin72edbb82008-06-19 16:00:04 -0700707 size_t len = 0;
H. Peter Anvin72edbb82008-06-19 16:00:04 -0700708 while ((c = *p++)) {
709 if (c >= 0x80)
710 len += pp_directives_len[c-0x80]+1;
711 else
712 len++;
713 }
714 ret = nasm_malloc(len+1);
H. Peter Anvincda81632008-06-21 15:15:40 -0700715 q = ret;
716 while ((c = *stdmacpos++)) {
H. Peter Anvin72edbb82008-06-19 16:00:04 -0700717 if (c >= 0x80) {
718 memcpy(q, pp_directives[c-0x80], pp_directives_len[c-0x80]);
719 q += pp_directives_len[c-0x80];
720 *q++ = ' ';
721 } else {
722 *q++ = c;
723 }
724 }
H. Peter Anvincda81632008-06-21 15:15:40 -0700725 stdmacpos = p;
H. Peter Anvin72edbb82008-06-19 16:00:04 -0700726 *q = '\0';
727
H. Peter Anvind2456592008-06-19 15:04:18 -0700728 if (!*stdmacpos) {
729 /* This was the last of the standard macro chain... */
730 stdmacpos = NULL;
731 if (any_extrastdmac) {
732 stdmacpos = extrastdmac;
733 any_extrastdmac = false;
734 } else if (do_predef) {
735 Line *pd, *l;
736 Token *head, **tail, *t;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000737
H. Peter Anvind2456592008-06-19 15:04:18 -0700738 /*
739 * Nasty hack: here we push the contents of
740 * `predef' on to the top-level expansion stack,
741 * since this is the most convenient way to
742 * implement the pre-include and pre-define
743 * features.
744 */
745 for (pd = predef; pd; pd = pd->next) {
746 head = NULL;
747 tail = &head;
748 for (t = pd->first; t; t = t->next) {
749 *tail = new_Token(NULL, t->type, t->text, 0);
750 tail = &(*tail)->next;
751 }
752 l = nasm_malloc(sizeof(Line));
753 l->next = istk->expansion;
754 l->first = head;
H. Peter Anvin538002d2008-06-28 18:30:27 -0700755 l->finishes = NULL;
H. Peter Anvind2456592008-06-19 15:04:18 -0700756 istk->expansion = l;
757 }
758 do_predef = false;
759 }
760 }
761 return ret;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000762 }
763
764 bufsize = BUF_DELTA;
765 buffer = nasm_malloc(BUF_DELTA);
766 p = buffer;
H. Peter Anvin9f394642002-04-30 21:07:51 +0000767 continued_count = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000768 while (1) {
769 q = fgets(p, bufsize - (p - buffer), istk->fp);
770 if (!q)
771 break;
772 p += strlen(p);
773 if (p > buffer && p[-1] == '\n') {
774 /* Convert backslash-CRLF line continuation sequences into
775 nothing at all (for DOS and Windows) */
776 if (((p - 2) > buffer) && (p[-3] == '\\') && (p[-2] == '\r')) {
777 p -= 3;
778 *p = 0;
779 continued_count++;
780 }
781 /* Also convert backslash-LF line continuation sequences into
782 nothing at all (for Unix) */
783 else if (((p - 1) > buffer) && (p[-2] == '\\')) {
784 p -= 2;
785 *p = 0;
786 continued_count++;
787 } else {
788 break;
789 }
790 }
791 if (p - buffer > bufsize - 10) {
Keith Kaniosb7a89542007-04-12 02:40:54 +0000792 int32_t offset = p - buffer;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000793 bufsize += BUF_DELTA;
794 buffer = nasm_realloc(buffer, bufsize);
795 p = buffer + offset; /* prevent stale-pointer problems */
796 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000797 }
798
H. Peter Anvine2c80182005-01-15 22:15:51 +0000799 if (!q && p == buffer) {
800 nasm_free(buffer);
801 return NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000802 }
803
H. Peter Anvine2c80182005-01-15 22:15:51 +0000804 src_set_linnum(src_get_linnum() + istk->lineinc +
805 (continued_count * istk->lineinc));
H. Peter Anvineba20a72002-04-30 20:53:55 +0000806
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000807 /*
808 * Play safe: remove CRs as well as LFs, if any of either are
809 * present at the end of the line.
810 */
H. Peter Anvineba20a72002-04-30 20:53:55 +0000811 while (--p >= buffer && (*p == '\n' || *p == '\r'))
H. Peter Anvine2c80182005-01-15 22:15:51 +0000812 *p = '\0';
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000813
814 /*
815 * Handle spurious ^Z, which may be inserted into source files
816 * by some file transfer utilities.
817 */
818 buffer[strcspn(buffer, "\032")] = '\0';
819
H. Peter Anvin734b1882002-04-30 21:01:08 +0000820 list->line(LIST_READ, buffer);
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000821
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000822 return buffer;
823}
824
825/*
Keith Kaniosb7a89542007-04-12 02:40:54 +0000826 * Tokenize a line of text. This is a very simple process since we
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000827 * don't need to parse the value out of e.g. numeric tokens: we
828 * simply split one string into many.
829 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000830static Token *tokenize(char *line)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000831{
H. Peter Anvinca544db2008-10-19 19:30:11 -0700832 char c, *p = line;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +0000833 enum pp_token_type type;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000834 Token *list = NULL;
835 Token *t, **tail = &list;
836
H. Peter Anvine2c80182005-01-15 22:15:51 +0000837 while (*line) {
838 p = line;
839 if (*p == '%') {
840 p++;
H. Peter Anvind784a082009-04-20 14:01:18 -0700841 if (*p == '+' && !nasm_isdigit(p[1])) {
842 p++;
843 type = TOK_PASTE;
844 } else if (nasm_isdigit(*p) ||
845 ((*p == '-' || *p == '+') && nasm_isdigit(p[1]))) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000846 do {
847 p++;
848 }
H. Peter Anvinbda7a6e2008-06-21 10:23:17 -0700849 while (nasm_isdigit(*p));
H. Peter Anvine2c80182005-01-15 22:15:51 +0000850 type = TOK_PREPROC_ID;
851 } else if (*p == '{') {
852 p++;
853 while (*p && *p != '}') {
854 p[-1] = *p;
855 p++;
856 }
857 p[-1] = '\0';
858 if (*p)
859 p++;
860 type = TOK_PREPROC_ID;
H. Peter Anvin992fe752008-10-19 15:45:05 -0700861 } else if (*p == '[') {
862 int lvl = 1;
863 line += 2; /* Skip the leading %[ */
864 p++;
H. Peter Anvinec033012008-10-19 22:12:06 -0700865 while (lvl && (c = *p++)) {
H. Peter Anvinca544db2008-10-19 19:30:11 -0700866 switch (c) {
867 case ']':
868 lvl--;
869 break;
870 case '%':
H. Peter Anvinca544db2008-10-19 19:30:11 -0700871 if (*p == '[')
872 lvl++;
873 break;
874 case '\'':
875 case '\"':
876 case '`':
H. Peter Anvinec033012008-10-19 22:12:06 -0700877 p = nasm_skip_string(p)+1;
H. Peter Anvinca544db2008-10-19 19:30:11 -0700878 break;
879 default:
880 break;
H. Peter Anvin992fe752008-10-19 15:45:05 -0700881 }
H. Peter Anvin992fe752008-10-19 15:45:05 -0700882 }
H. Peter Anvinec033012008-10-19 22:12:06 -0700883 p--;
H. Peter Anvin992fe752008-10-19 15:45:05 -0700884 if (*p)
885 *p++ = '\0';
H. Peter Anvine1265812008-10-19 22:14:30 -0700886 if (lvl)
887 error(ERR_NONFATAL, "unterminated %[ construct");
H. Peter Anvin992fe752008-10-19 15:45:05 -0700888 type = TOK_INDIRECT;
H. Peter Anvin6c81f0a2008-05-25 21:46:17 -0700889 } else if (*p == '?') {
890 type = TOK_PREPROC_Q; /* %? */
891 p++;
892 if (*p == '?') {
893 type = TOK_PREPROC_QQ; /* %?? */
894 p++;
895 }
H. Peter Anvine2c80182005-01-15 22:15:51 +0000896 } else if (isidchar(*p) ||
897 ((*p == '!' || *p == '%' || *p == '$') &&
898 isidchar(p[1]))) {
899 do {
900 p++;
901 }
902 while (isidchar(*p));
903 type = TOK_PREPROC_ID;
904 } else {
905 type = TOK_OTHER;
906 if (*p == '%')
907 p++;
908 }
909 } else if (isidstart(*p) || (*p == '$' && isidstart(p[1]))) {
910 type = TOK_ID;
911 p++;
912 while (*p && isidchar(*p))
913 p++;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -0700914 } else if (*p == '\'' || *p == '"' || *p == '`') {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000915 /*
916 * A string token.
917 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000918 type = TOK_STRING;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -0700919 p = nasm_skip_string(p);
Nickolay Yurchenkof3b3ce22003-09-21 20:38:43 +0000920
H. Peter Anvine2c80182005-01-15 22:15:51 +0000921 if (*p) {
922 p++;
923 } else {
H. Peter Anvin917a3492008-09-24 09:14:49 -0700924 error(ERR_WARNING|ERR_PASS1, "unterminated string");
H. Peter Anvine2c80182005-01-15 22:15:51 +0000925 /* Handling unterminated strings by UNV */
926 /* type = -1; */
927 }
Victor van den Elzenfb5f2512009-04-17 16:17:59 +0200928 } else if (p[0] == '$' && p[1] == '$') {
H. Peter Anvin8e1f8112009-04-17 14:20:44 -0700929 type = TOK_OTHER; /* TOKEN_BASE */
Victor van den Elzenfb5f2512009-04-17 16:17:59 +0200930 p += 2;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000931 } else if (isnumstart(*p)) {
H. Peter Anvinc2df2822007-10-24 15:29:28 -0700932 bool is_hex = false;
933 bool is_float = false;
934 bool has_e = false;
935 char c, *r;
936
H. Peter Anvine2c80182005-01-15 22:15:51 +0000937 /*
H. Peter Anvinc2df2822007-10-24 15:29:28 -0700938 * A numeric token.
H. Peter Anvine2c80182005-01-15 22:15:51 +0000939 */
H. Peter Anvinc2df2822007-10-24 15:29:28 -0700940
941 if (*p == '$') {
942 p++;
943 is_hex = true;
944 }
945
946 for (;;) {
947 c = *p++;
948
949 if (!is_hex && (c == 'e' || c == 'E')) {
950 has_e = true;
951 if (*p == '+' || *p == '-') {
952 /* e can only be followed by +/- if it is either a
953 prefixed hex number or a floating-point number */
954 p++;
955 is_float = true;
956 }
957 } else if (c == 'H' || c == 'h' || c == 'X' || c == 'x') {
958 is_hex = true;
959 } else if (c == 'P' || c == 'p') {
960 is_float = true;
961 if (*p == '+' || *p == '-')
962 p++;
963 } else if (isnumchar(c) || c == '_')
964 ; /* just advance */
965 else if (c == '.') {
966 /* we need to deal with consequences of the legacy
967 parser, like "1.nolist" being two tokens
968 (TOK_NUMBER, TOK_ID) here; at least give it
969 a shot for now. In the future, we probably need
970 a flex-based scanner with proper pattern matching
971 to do it as well as it can be done. Nothing in
972 the world is going to help the person who wants
973 0x123.p16 interpreted as two tokens, though. */
974 r = p;
975 while (*r == '_')
976 r++;
977
H. Peter Anvinf221b9e2008-06-21 11:03:51 -0700978 if (nasm_isdigit(*r) || (is_hex && nasm_isxdigit(*r)) ||
H. Peter Anvinc2df2822007-10-24 15:29:28 -0700979 (!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 */
989
H. Peter Anvin8e1f8112009-04-17 14:20:44 -0700990 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
H. Peter Anvin8e1f8112009-04-17 14:20:44 -0700998 type = is_float ? TOK_FLOAT : TOK_NUMBER;
H. Peter Anvinc2df2822007-10-24 15:29:28 -0700999 }
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;
1002 p++;
H. Peter Anvinbda7a6e2008-06-21 10:23:17 -07001003 while (*p && nasm_isspace(*p))
H. Peter Anvine2c80182005-01-15 22:15:51 +00001004 p++;
1005 /*
1006 * Whitespace just before end-of-line is discarded by
1007 * pretending it's a comment; whitespace just before a
1008 * comment gets lumped into the comment.
1009 */
1010 if (!*p || *p == ';') {
1011 type = TOK_COMMENT;
1012 while (*p)
1013 p++;
1014 }
1015 } else if (*p == ';') {
1016 type = TOK_COMMENT;
1017 while (*p)
1018 p++;
1019 } else {
1020 /*
1021 * Anything else is an operator of some kind. We check
1022 * for all the double-character operators (>>, <<, //,
1023 * %%, <=, >=, ==, !=, <>, &&, ||, ^^), but anything
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001024 * else is a single-character operator.
H. Peter Anvine2c80182005-01-15 22:15:51 +00001025 */
1026 type = TOK_OTHER;
1027 if ((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[0] == '|' && p[1] == '|') ||
1037 (p[0] == '^' && p[1] == '^')) {
1038 p++;
1039 }
1040 p++;
1041 }
Nickolay Yurchenkof3b3ce22003-09-21 20:38:43 +00001042
H. Peter Anvine2c80182005-01-15 22:15:51 +00001043 /* Handling unterminated string by UNV */
1044 /*if (type == -1)
H. Peter Anvin89cee572009-07-15 09:16:54 -04001045 {
1046 *tail = t = new_Token(NULL, TOK_STRING, line, p-line+1);
1047 t->text[p-line] = *line;
1048 tail = &t->next;
1049 }
1050 else */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001051 if (type != TOK_COMMENT) {
1052 *tail = t = new_Token(NULL, type, line, p - line);
1053 tail = &t->next;
1054 }
1055 line = p;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001056 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001057 return list;
1058}
1059
H. Peter Anvince616072002-04-30 21:02:23 +00001060/*
1061 * this function allocates a new managed block of memory and
H. Peter Anvin70653092007-10-19 14:42:29 -07001062 * returns a pointer to the block. The managed blocks are
H. Peter Anvince616072002-04-30 21:02:23 +00001063 * deleted only all at once by the delete_Blocks function.
1064 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001065static void *new_Block(size_t size)
H. Peter Anvince616072002-04-30 21:02:23 +00001066{
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001067 Blocks *b = &blocks;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001068
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001069 /* first, get to the end of the linked list */
1070 while (b->next)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001071 b = b->next;
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001072 /* now allocate the requested chunk */
1073 b->chunk = nasm_malloc(size);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001074
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001075 /* now allocate a new block for the next request */
1076 b->next = nasm_malloc(sizeof(Blocks));
1077 /* and initialize the contents of the new block */
1078 b->next->next = NULL;
1079 b->next->chunk = NULL;
1080 return b->chunk;
H. Peter Anvince616072002-04-30 21:02:23 +00001081}
1082
1083/*
1084 * this function deletes all managed blocks of memory
1085 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001086static void delete_Blocks(void)
H. Peter Anvince616072002-04-30 21:02:23 +00001087{
H. Peter Anvine2c80182005-01-15 22:15:51 +00001088 Blocks *a, *b = &blocks;
H. Peter Anvince616072002-04-30 21:02:23 +00001089
H. Peter Anvin70653092007-10-19 14:42:29 -07001090 /*
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001091 * keep in mind that the first block, pointed to by blocks
H. Peter Anvin70653092007-10-19 14:42:29 -07001092 * is a static and not dynamically allocated, so we don't
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001093 * free it.
1094 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001095 while (b) {
1096 if (b->chunk)
1097 nasm_free(b->chunk);
1098 a = b;
1099 b = b->next;
1100 if (a != &blocks)
1101 nasm_free(a);
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001102 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001103}
H. Peter Anvin734b1882002-04-30 21:01:08 +00001104
1105/*
H. Peter Anvin70653092007-10-19 14:42:29 -07001106 * this function creates a new Token and passes a pointer to it
H. Peter Anvin734b1882002-04-30 21:01:08 +00001107 * back to the caller. It sets the type and text elements, and
H. Peter Anvinf26e0972008-07-01 21:26:27 -07001108 * also the a.mac and next elements to NULL.
H. Peter Anvin734b1882002-04-30 21:01:08 +00001109 */
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07001110static Token *new_Token(Token * next, enum pp_token_type type,
H. Peter Anvinc751e862008-06-09 10:18:45 -07001111 const char *text, int txtlen)
H. Peter Anvin734b1882002-04-30 21:01:08 +00001112{
1113 Token *t;
1114 int i;
1115
H. Peter Anvin89cee572009-07-15 09:16:54 -04001116 if (!freeTokens) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001117 freeTokens = (Token *) new_Block(TOKEN_BLOCKSIZE * sizeof(Token));
1118 for (i = 0; i < TOKEN_BLOCKSIZE - 1; i++)
1119 freeTokens[i].next = &freeTokens[i + 1];
1120 freeTokens[i].next = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001121 }
1122 t = freeTokens;
1123 freeTokens = t->next;
1124 t->next = next;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07001125 t->a.mac = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001126 t->type = type;
H. Peter Anvin89cee572009-07-15 09:16:54 -04001127 if (type == TOK_WHITESPACE || !text) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001128 t->text = NULL;
1129 } else {
1130 if (txtlen == 0)
1131 txtlen = strlen(text);
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07001132 t->text = nasm_malloc(txtlen+1);
1133 memcpy(t->text, text, txtlen);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001134 t->text[txtlen] = '\0';
H. Peter Anvin734b1882002-04-30 21:01:08 +00001135 }
1136 return t;
1137}
1138
H. Peter Anvine2c80182005-01-15 22:15:51 +00001139static Token *delete_Token(Token * t)
H. Peter Anvin734b1882002-04-30 21:01:08 +00001140{
1141 Token *next = t->next;
1142 nasm_free(t->text);
H. Peter Anvin788e6c12002-04-30 21:02:01 +00001143 t->next = freeTokens;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001144 freeTokens = t;
1145 return next;
1146}
1147
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001148/*
1149 * Convert a line of tokens back into text.
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001150 * If expand_locals is not zero, identifiers of the form "%$*xxx"
1151 * will be transformed into ..@ctxnum.xxx
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001152 */
H. Peter Anvin9e200162008-06-04 17:23:14 -07001153static char *detoken(Token * tlist, bool expand_locals)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001154{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001155 Token *t;
1156 int len;
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001157 char *line, *p;
H. Peter Anvinb4daadc2008-01-21 16:31:57 -08001158 const char *q;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001159
1160 len = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001161 for (t = tlist; t; t = t->next) {
1162 if (t->type == TOK_PREPROC_ID && t->text[1] == '!') {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001163 char *p = getenv(t->text + 2);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001164 nasm_free(t->text);
1165 if (p)
1166 t->text = nasm_strdup(p);
1167 else
1168 t->text = NULL;
1169 }
1170 /* Expand local macros here and not during preprocessing */
1171 if (expand_locals &&
1172 t->type == TOK_PREPROC_ID && t->text &&
1173 t->text[0] == '%' && t->text[1] == '$') {
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001174 const char *q;
1175 char *p;
1176 Context *ctx = get_ctx(t->text, &q, false);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001177 if (ctx) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001178 char buffer[40];
Keith Kanios93f2e9a2007-04-14 00:10:59 +00001179 snprintf(buffer, sizeof(buffer), "..@%"PRIu32".", ctx->number);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001180 p = nasm_strcat(buffer, q);
1181 nasm_free(t->text);
1182 t->text = p;
1183 }
1184 }
1185 if (t->type == TOK_WHITESPACE) {
1186 len++;
1187 } else if (t->text) {
1188 len += strlen(t->text);
1189 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001190 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00001191 p = line = nasm_malloc(len + 1);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001192 for (t = tlist; t; t = t->next) {
1193 if (t->type == TOK_WHITESPACE) {
H. Peter Anvinb4daadc2008-01-21 16:31:57 -08001194 *p++ = ' ';
H. Peter Anvine2c80182005-01-15 22:15:51 +00001195 } else if (t->text) {
H. Peter Anvinb4daadc2008-01-21 16:31:57 -08001196 q = t->text;
1197 while (*q)
1198 *p++ = *q++;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001199 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001200 }
1201 *p = '\0';
1202 return line;
1203}
1204
1205/*
H. Peter Anvin76690a12002-04-30 20:52:49 +00001206 * A scanner, suitable for use by the expression evaluator, which
1207 * operates on a line of Tokens. Expects a pointer to a pointer to
1208 * the first token in the line to be passed in as its private_data
1209 * field.
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001210 *
1211 * FIX: This really needs to be unified with stdscan.
H. Peter Anvin76690a12002-04-30 20:52:49 +00001212 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001213static int ppscan(void *private_data, struct tokenval *tokval)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001214{
H. Peter Anvin76690a12002-04-30 20:52:49 +00001215 Token **tlineptr = private_data;
1216 Token *tline;
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001217 char ourcopy[MAX_KEYWORD+1], *p, *r, *s;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001218
H. Peter Anvine2c80182005-01-15 22:15:51 +00001219 do {
1220 tline = *tlineptr;
1221 *tlineptr = tline ? tline->next : NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001222 }
1223 while (tline && (tline->type == TOK_WHITESPACE ||
H. Peter Anvine2c80182005-01-15 22:15:51 +00001224 tline->type == TOK_COMMENT));
H. Peter Anvin76690a12002-04-30 20:52:49 +00001225
1226 if (!tline)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001227 return tokval->t_type = TOKEN_EOS;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001228
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001229 tokval->t_charptr = tline->text;
1230
H. Peter Anvin76690a12002-04-30 20:52:49 +00001231 if (tline->text[0] == '$' && !tline->text[1])
H. Peter Anvine2c80182005-01-15 22:15:51 +00001232 return tokval->t_type = TOKEN_HERE;
H. Peter Anvin7cf897e2002-05-30 21:30:33 +00001233 if (tline->text[0] == '$' && tline->text[1] == '$' && !tline->text[2])
H. Peter Anvine2c80182005-01-15 22:15:51 +00001234 return tokval->t_type = TOKEN_BASE;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001235
H. Peter Anvine2c80182005-01-15 22:15:51 +00001236 if (tline->type == TOK_ID) {
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001237 p = tokval->t_charptr = tline->text;
1238 if (p[0] == '$') {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001239 tokval->t_charptr++;
1240 return tokval->t_type = TOKEN_ID;
1241 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00001242
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001243 for (r = p, s = ourcopy; *r; r++) {
Philipp Thomas76ec8e72008-05-21 08:53:21 -07001244 if (r >= p+MAX_KEYWORD)
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001245 return tokval->t_type = TOKEN_ID; /* Not a keyword */
H. Peter Anvinac8f8fc2008-06-11 15:49:41 -07001246 *s++ = nasm_tolower(*r);
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001247 }
1248 *s = '\0';
1249 /* right, so we have an identifier sitting in temp storage. now,
1250 * is it actually a register or instruction name, or what? */
1251 return nasm_token_hash(ourcopy, tokval);
H. Peter Anvin76690a12002-04-30 20:52:49 +00001252 }
1253
H. Peter Anvine2c80182005-01-15 22:15:51 +00001254 if (tline->type == TOK_NUMBER) {
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001255 bool rn_error;
1256 tokval->t_integer = readnum(tline->text, &rn_error);
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001257 tokval->t_charptr = tline->text;
H. Peter Anvin11627042008-06-09 20:45:19 -07001258 if (rn_error)
1259 return tokval->t_type = TOKEN_ERRNUM;
1260 else
1261 return tokval->t_type = TOKEN_NUM;
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001262 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00001263
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001264 if (tline->type == TOK_FLOAT) {
1265 return tokval->t_type = TOKEN_FLOAT;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001266 }
1267
H. Peter Anvine2c80182005-01-15 22:15:51 +00001268 if (tline->type == TOK_STRING) {
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07001269 char bq, *ep;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001270
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07001271 bq = tline->text[0];
H. Peter Anvin11627042008-06-09 20:45:19 -07001272 tokval->t_charptr = tline->text;
1273 tokval->t_inttwo = nasm_unquote(tline->text, &ep);
H. Peter Anvind2456592008-06-19 15:04:18 -07001274
H. Peter Anvin11627042008-06-09 20:45:19 -07001275 if (ep[0] != bq || ep[1] != '\0')
1276 return tokval->t_type = TOKEN_ERRSTR;
1277 else
1278 return tokval->t_type = TOKEN_STR;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001279 }
1280
H. Peter Anvine2c80182005-01-15 22:15:51 +00001281 if (tline->type == TOK_OTHER) {
1282 if (!strcmp(tline->text, "<<"))
1283 return tokval->t_type = TOKEN_SHL;
1284 if (!strcmp(tline->text, ">>"))
1285 return tokval->t_type = TOKEN_SHR;
1286 if (!strcmp(tline->text, "//"))
1287 return tokval->t_type = TOKEN_SDIV;
1288 if (!strcmp(tline->text, "%%"))
1289 return tokval->t_type = TOKEN_SMOD;
1290 if (!strcmp(tline->text, "=="))
1291 return tokval->t_type = TOKEN_EQ;
1292 if (!strcmp(tline->text, "<>"))
1293 return tokval->t_type = TOKEN_NE;
1294 if (!strcmp(tline->text, "!="))
1295 return tokval->t_type = TOKEN_NE;
1296 if (!strcmp(tline->text, "<="))
1297 return tokval->t_type = TOKEN_LE;
1298 if (!strcmp(tline->text, ">="))
1299 return tokval->t_type = TOKEN_GE;
1300 if (!strcmp(tline->text, "&&"))
1301 return tokval->t_type = TOKEN_DBL_AND;
1302 if (!strcmp(tline->text, "^^"))
1303 return tokval->t_type = TOKEN_DBL_XOR;
1304 if (!strcmp(tline->text, "||"))
1305 return tokval->t_type = TOKEN_DBL_OR;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001306 }
1307
1308 /*
1309 * We have no other options: just return the first character of
1310 * the token text.
1311 */
1312 return tokval->t_type = tline->text[0];
1313}
1314
1315/*
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001316 * Compare a string to the name of an existing macro; this is a
1317 * simple wrapper which calls either strcmp or nasm_stricmp
1318 * depending on the value of the `casesense' parameter.
1319 */
H. Peter Anvin4db5a162007-10-11 13:42:09 -07001320static int mstrcmp(const char *p, const char *q, bool casesense)
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001321{
H. Peter Anvin734b1882002-04-30 21:01:08 +00001322 return casesense ? strcmp(p, q) : nasm_stricmp(p, q);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001323}
1324
1325/*
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07001326 * Compare a string to the name of an existing macro; this is a
1327 * simple wrapper which calls either strcmp or nasm_stricmp
1328 * depending on the value of the `casesense' parameter.
1329 */
1330static int mmemcmp(const char *p, const char *q, size_t l, bool casesense)
1331{
1332 return casesense ? memcmp(p, q, l) : nasm_memicmp(p, q, l);
1333}
1334
1335/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001336 * Return the Context structure associated with a %$ token. Return
1337 * NULL, having _already_ reported an error condition, if the
1338 * context stack isn't deep enough for the supplied number of $
1339 * signs.
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001340 * If all_contexts == true, contexts that enclose current are
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001341 * also scanned for such smacro, until it is found; if not -
1342 * only the context that directly results from the number of $'s
1343 * in variable's name.
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001344 *
1345 * If "namep" is non-NULL, set it to the pointer to the macro name
1346 * tail, i.e. the part beyond %$...
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001347 */
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001348static Context *get_ctx(const char *name, const char **namep,
1349 bool all_contexts)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001350{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001351 Context *ctx;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001352 SMacro *m;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001353 int i;
1354
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001355 if (namep)
1356 *namep = name;
1357
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001358 if (!name || name[0] != '%' || name[1] != '$')
H. Peter Anvine2c80182005-01-15 22:15:51 +00001359 return NULL;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001360
H. Peter Anvine2c80182005-01-15 22:15:51 +00001361 if (!cstk) {
1362 error(ERR_NONFATAL, "`%s': context stack is empty", name);
1363 return NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001364 }
1365
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001366 name += 2;
1367 ctx = cstk;
1368 i = 0;
1369 while (ctx && *name == '$') {
1370 name++;
1371 i++;
1372 ctx = ctx->next;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001373 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001374 if (!ctx) {
1375 error(ERR_NONFATAL, "`%s': context stack is only"
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001376 " %d level%s deep", name, i, (i == 1 ? "" : "s"));
H. Peter Anvine2c80182005-01-15 22:15:51 +00001377 return NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001378 }
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001379
1380 if (namep)
1381 *namep = name;
1382
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001383 if (!all_contexts)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001384 return ctx;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001385
H. Peter Anvine2c80182005-01-15 22:15:51 +00001386 do {
1387 /* Search for this smacro in found context */
H. Peter Anvin166c2472008-05-28 12:28:58 -07001388 m = hash_findix(&ctx->localmac, name);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001389 while (m) {
1390 if (!mstrcmp(m->name, name, m->casesense))
1391 return ctx;
1392 m = m->next;
1393 }
1394 ctx = ctx->next;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001395 }
1396 while (ctx);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001397 return NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001398}
1399
1400/*
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001401 * Check to see if a file is already in a string list
1402 */
1403static bool in_list(const StrList *list, const char *str)
1404{
1405 while (list) {
1406 if (!strcmp(list->str, str))
1407 return true;
1408 list = list->next;
1409 }
1410 return false;
1411}
1412
1413/*
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001414 * Open an include file. This routine must always return a valid
1415 * file pointer if it returns - it's responsible for throwing an
1416 * ERR_FATAL and bombing out completely if not. It should also try
1417 * the include path one by one until it finds the file or reaches
1418 * the end of the path.
1419 */
H. Peter Anvin2b1c3b92008-06-06 10:38:46 -07001420static FILE *inc_fopen(const char *file, StrList **dhead, StrList ***dtail,
H. Peter Anvin418ca702008-05-30 10:42:30 -07001421 bool missing_ok)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001422{
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001423 FILE *fp;
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001424 char *prefix = "";
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001425 IncPath *ip = ipath;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001426 int len = strlen(file);
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001427 size_t prefix_len = 0;
1428 StrList *sl;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001429
H. Peter Anvine2c80182005-01-15 22:15:51 +00001430 while (1) {
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001431 sl = nasm_malloc(prefix_len+len+1+sizeof sl->next);
1432 memcpy(sl->str, prefix, prefix_len);
1433 memcpy(sl->str+prefix_len, file, len+1);
1434 fp = fopen(sl->str, "r");
H. Peter Anvin418ca702008-05-30 10:42:30 -07001435 if (fp && dhead && !in_list(*dhead, sl->str)) {
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001436 sl->next = NULL;
H. Peter Anvin2b1c3b92008-06-06 10:38:46 -07001437 **dtail = sl;
1438 *dtail = &sl->next;
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001439 } else {
1440 nasm_free(sl);
1441 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001442 if (fp)
1443 return fp;
H. Peter Anvin418ca702008-05-30 10:42:30 -07001444 if (!ip) {
1445 if (!missing_ok)
1446 break;
1447 prefix = NULL;
1448 } else {
1449 prefix = ip->path;
1450 ip = ip->next;
1451 }
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001452 if (prefix) {
1453 prefix_len = strlen(prefix);
1454 } else {
1455 /* -MG given and file not found */
H. Peter Anvin418ca702008-05-30 10:42:30 -07001456 if (dhead && !in_list(*dhead, file)) {
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001457 sl = nasm_malloc(len+1+sizeof sl->next);
1458 sl->next = NULL;
1459 strcpy(sl->str, file);
H. Peter Anvin2b1c3b92008-06-06 10:38:46 -07001460 **dtail = sl;
1461 *dtail = &sl->next;
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001462 }
H. Peter Anvin37a321f2007-09-24 13:41:58 -07001463 return NULL;
1464 }
H. Peter Anvineba20a72002-04-30 20:53:55 +00001465 }
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001466
H. Peter Anvin734b1882002-04-30 21:01:08 +00001467 error(ERR_FATAL, "unable to open include file `%s'", file);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001468 return NULL; /* never reached - placate compilers */
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001469}
1470
1471/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001472 * Determine if we should warn on defining a single-line macro of
H. Peter Anvinef7468f2002-04-30 20:57:59 +00001473 * name `name', with `nparam' parameters. If nparam is 0 or -1, will
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001474 * return true if _any_ single-line macro of that name is defined.
1475 * Otherwise, will return true if a single-line macro with either
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001476 * `nparam' or no parameters is defined.
1477 *
1478 * If a macro with precisely the right number of parameters is
H. Peter Anvinef7468f2002-04-30 20:57:59 +00001479 * defined, or nparam is -1, the address of the definition structure
1480 * will be returned in `defn'; otherwise NULL will be returned. If `defn'
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001481 * is NULL, no action will be taken regarding its contents, and no
1482 * error will occur.
1483 *
1484 * Note that this is also called with nparam zero to resolve
1485 * `ifdef'.
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001486 *
1487 * If you already know which context macro belongs to, you can pass
1488 * the context pointer as first parameter; if you won't but name begins
1489 * with %$ the context will be automatically computed. If all_contexts
1490 * is true, macro will be searched in outer contexts as well.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001491 */
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07001492static bool
H. Peter Anvinb2a5fda2008-06-19 21:42:42 -07001493smacro_defined(Context * ctx, const char *name, int nparam, SMacro ** defn,
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07001494 bool nocase)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001495{
H. Peter Anvin166c2472008-05-28 12:28:58 -07001496 struct hash_table *smtbl;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001497 SMacro *m;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001498
H. Peter Anvin97a23472007-09-16 17:57:25 -07001499 if (ctx) {
H. Peter Anvin166c2472008-05-28 12:28:58 -07001500 smtbl = &ctx->localmac;
H. Peter Anvin97a23472007-09-16 17:57:25 -07001501 } else if (name[0] == '%' && name[1] == '$') {
1502 if (cstk)
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001503 ctx = get_ctx(name, &name, false);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001504 if (!ctx)
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001505 return false; /* got to return _something_ */
H. Peter Anvin166c2472008-05-28 12:28:58 -07001506 smtbl = &ctx->localmac;
H. Peter Anvin97a23472007-09-16 17:57:25 -07001507 } else {
H. Peter Anvin166c2472008-05-28 12:28:58 -07001508 smtbl = &smacros;
H. Peter Anvin97a23472007-09-16 17:57:25 -07001509 }
H. Peter Anvin166c2472008-05-28 12:28:58 -07001510 m = (SMacro *) hash_findix(smtbl, name);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001511
H. Peter Anvine2c80182005-01-15 22:15:51 +00001512 while (m) {
1513 if (!mstrcmp(m->name, name, m->casesense && nocase) &&
Charles Crayne192d5b52007-10-18 19:02:42 -07001514 (nparam <= 0 || m->nparam == 0 || nparam == (int) m->nparam)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001515 if (defn) {
Charles Crayne192d5b52007-10-18 19:02:42 -07001516 if (nparam == (int) m->nparam || nparam == -1)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001517 *defn = m;
1518 else
1519 *defn = NULL;
1520 }
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001521 return true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001522 }
1523 m = m->next;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001524 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001525
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001526 return false;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001527}
1528
1529/*
1530 * Count and mark off the parameters in a multi-line macro call.
1531 * This is called both from within the multi-line macro expansion
1532 * code, and also to mark off the default parameters when provided
1533 * in a %macro definition line.
1534 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001535static void count_mmac_params(Token * t, int *nparam, Token *** params)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001536{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001537 int paramsize, brace;
1538
1539 *nparam = paramsize = 0;
1540 *params = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001541 while (t) {
H. Peter Anvin91fb6f12008-09-01 10:56:33 -07001542 /* +1: we need space for the final NULL */
1543 if (*nparam+1 >= paramsize) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001544 paramsize += PARAM_DELTA;
1545 *params = nasm_realloc(*params, sizeof(**params) * paramsize);
1546 }
1547 skip_white_(t);
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001548 brace = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001549 if (tok_is_(t, "{"))
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001550 brace = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001551 (*params)[(*nparam)++] = t;
1552 while (tok_isnt_(t, brace ? "}" : ","))
1553 t = t->next;
1554 if (t) { /* got a comma/brace */
1555 t = t->next;
1556 if (brace) {
1557 /*
1558 * Now we've found the closing brace, look further
1559 * for the comma.
1560 */
1561 skip_white_(t);
1562 if (tok_isnt_(t, ",")) {
1563 error(ERR_NONFATAL,
1564 "braces do not enclose all of macro parameter");
1565 while (tok_isnt_(t, ","))
1566 t = t->next;
1567 }
1568 if (t)
1569 t = t->next; /* eat the comma */
1570 }
1571 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001572 }
1573}
1574
1575/*
H. Peter Anvin76690a12002-04-30 20:52:49 +00001576 * Determine whether one of the various `if' conditions is true or
1577 * not.
1578 *
1579 * We must free the tline we get passed.
1580 */
H. Peter Anvin70055962007-10-11 00:05:31 -07001581static bool if_condition(Token * tline, enum preproc_token ct)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001582{
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001583 enum pp_conditional i = PP_COND(ct);
H. Peter Anvin70055962007-10-11 00:05:31 -07001584 bool j;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001585 Token *t, *tt, **tptr, *origline;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001586 struct tokenval tokval;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001587 expr *evalresult;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001588 enum pp_token_type needtype;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001589
1590 origline = tline;
1591
H. Peter Anvine2c80182005-01-15 22:15:51 +00001592 switch (i) {
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001593 case PPC_IFCTX:
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001594 j = false; /* have we matched yet? */
Victor van den Elzen0e857f12008-07-23 13:21:29 +02001595 while (true) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001596 skip_white_(tline);
Victor van den Elzen0e857f12008-07-23 13:21:29 +02001597 if (!tline)
1598 break;
1599 if (tline->type != TOK_ID) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001600 error(ERR_NONFATAL,
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001601 "`%s' expects context identifiers", pp_directives[ct]);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001602 free_tlist(origline);
1603 return -1;
1604 }
Victor van den Elzen0e857f12008-07-23 13:21:29 +02001605 if (cstk && cstk->name && !nasm_stricmp(tline->text, cstk->name))
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001606 j = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001607 tline = tline->next;
1608 }
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001609 break;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001610
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001611 case PPC_IFDEF:
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001612 j = false; /* have we matched yet? */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001613 while (tline) {
1614 skip_white_(tline);
1615 if (!tline || (tline->type != TOK_ID &&
1616 (tline->type != TOK_PREPROC_ID ||
1617 tline->text[1] != '$'))) {
1618 error(ERR_NONFATAL,
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001619 "`%s' expects macro identifiers", pp_directives[ct]);
1620 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001621 }
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07001622 if (smacro_defined(NULL, tline->text, 0, NULL, true))
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001623 j = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001624 tline = tline->next;
1625 }
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001626 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001627
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001628 case PPC_IFIDN:
1629 case PPC_IFIDNI:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001630 tline = expand_smacro(tline);
1631 t = tt = tline;
1632 while (tok_isnt_(tt, ","))
1633 tt = tt->next;
1634 if (!tt) {
1635 error(ERR_NONFATAL,
1636 "`%s' expects two comma-separated arguments",
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001637 pp_directives[ct]);
1638 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001639 }
1640 tt = tt->next;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001641 j = true; /* assume equality unless proved not */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001642 while ((t->type != TOK_OTHER || strcmp(t->text, ",")) && tt) {
1643 if (tt->type == TOK_OTHER && !strcmp(tt->text, ",")) {
1644 error(ERR_NONFATAL, "`%s': more than one comma on line",
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001645 pp_directives[ct]);
1646 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001647 }
1648 if (t->type == TOK_WHITESPACE) {
1649 t = t->next;
1650 continue;
1651 }
1652 if (tt->type == TOK_WHITESPACE) {
1653 tt = tt->next;
1654 continue;
1655 }
1656 if (tt->type != t->type) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001657 j = false; /* found mismatching tokens */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001658 break;
1659 }
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07001660 /* When comparing strings, need to unquote them first */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001661 if (t->type == TOK_STRING) {
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07001662 size_t l1 = nasm_unquote(t->text, NULL);
1663 size_t l2 = nasm_unquote(tt->text, NULL);
H. Peter Anvind2456592008-06-19 15:04:18 -07001664
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07001665 if (l1 != l2) {
1666 j = false;
1667 break;
1668 }
1669 if (mmemcmp(t->text, tt->text, l1, i == PPC_IFIDN)) {
1670 j = false;
1671 break;
1672 }
1673 } else if (mstrcmp(tt->text, t->text, i == PPC_IFIDN) != 0) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001674 j = false; /* found mismatching tokens */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001675 break;
1676 }
Nickolay Yurchenkof3b3ce22003-09-21 20:38:43 +00001677
H. Peter Anvine2c80182005-01-15 22:15:51 +00001678 t = t->next;
1679 tt = tt->next;
1680 }
1681 if ((t->type != TOK_OTHER || strcmp(t->text, ",")) || tt)
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001682 j = false; /* trailing gunk on one end or other */
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001683 break;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001684
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001685 case PPC_IFMACRO:
H. Peter Anvin89cee572009-07-15 09:16:54 -04001686 {
1687 bool found = false;
1688 MMacro searching, *mmac;
H. Peter Anvin65747262002-05-07 00:10:05 +00001689
H. Peter Anvin89cee572009-07-15 09:16:54 -04001690 skip_white_(tline);
1691 tline = expand_id(tline);
1692 if (!tok_type_(tline, TOK_ID)) {
1693 error(ERR_NONFATAL,
1694 "`%s' expects a macro name", pp_directives[ct]);
1695 goto fail;
1696 }
1697 searching.name = nasm_strdup(tline->text);
1698 searching.casesense = true;
1699 searching.plus = false;
1700 searching.nolist = false;
1701 searching.in_progress = 0;
1702 searching.max_depth = 0;
1703 searching.rep_nest = NULL;
1704 searching.nparam_min = 0;
1705 searching.nparam_max = INT_MAX;
1706 tline = expand_smacro(tline->next);
1707 skip_white_(tline);
1708 if (!tline) {
1709 } else if (!tok_type_(tline, TOK_NUMBER)) {
1710 error(ERR_NONFATAL,
1711 "`%s' expects a parameter count or nothing",
1712 pp_directives[ct]);
1713 } else {
1714 searching.nparam_min = searching.nparam_max =
1715 readnum(tline->text, &j);
1716 if (j)
1717 error(ERR_NONFATAL,
1718 "unable to parse parameter count `%s'",
1719 tline->text);
1720 }
1721 if (tline && tok_is_(tline->next, "-")) {
1722 tline = tline->next->next;
1723 if (tok_is_(tline, "*"))
1724 searching.nparam_max = INT_MAX;
1725 else if (!tok_type_(tline, TOK_NUMBER))
1726 error(ERR_NONFATAL,
1727 "`%s' expects a parameter count after `-'",
1728 pp_directives[ct]);
1729 else {
1730 searching.nparam_max = readnum(tline->text, &j);
1731 if (j)
1732 error(ERR_NONFATAL,
1733 "unable to parse parameter count `%s'",
1734 tline->text);
1735 if (searching.nparam_min > searching.nparam_max)
1736 error(ERR_NONFATAL,
1737 "minimum parameter count exceeds maximum");
H. Peter Anvin97a23472007-09-16 17:57:25 -07001738 }
H. Peter Anvin89cee572009-07-15 09:16:54 -04001739 }
1740 if (tline && tok_is_(tline->next, "+")) {
1741 tline = tline->next;
1742 searching.plus = true;
1743 }
1744 mmac = (MMacro *) hash_findix(&mmacros, searching.name);
1745 while (mmac) {
1746 if (!strcmp(mmac->name, searching.name) &&
1747 (mmac->nparam_min <= searching.nparam_max
1748 || searching.plus)
1749 && (searching.nparam_min <= mmac->nparam_max
1750 || mmac->plus)) {
1751 found = true;
1752 break;
1753 }
1754 mmac = mmac->next;
1755 }
1756 if (tline && tline->next)
1757 error(ERR_WARNING|ERR_PASS1,
1758 "trailing garbage after %%ifmacro ignored");
1759 nasm_free(searching.name);
1760 j = found;
1761 break;
1762 }
H. Peter Anvin65747262002-05-07 00:10:05 +00001763
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001764 case PPC_IFID:
1765 needtype = TOK_ID;
1766 goto iftype;
1767 case PPC_IFNUM:
1768 needtype = TOK_NUMBER;
1769 goto iftype;
1770 case PPC_IFSTR:
1771 needtype = TOK_STRING;
1772 goto iftype;
1773
1774 iftype:
H. Peter Anvin134b9462008-02-16 17:01:40 -08001775 t = tline = expand_smacro(tline);
H. Peter Anvind85d2502008-05-04 17:53:31 -07001776
H. Peter Anvin927c92b2008-02-16 13:44:52 -08001777 while (tok_type_(t, TOK_WHITESPACE) ||
1778 (needtype == TOK_NUMBER &&
1779 tok_type_(t, TOK_OTHER) &&
1780 (t->text[0] == '-' || t->text[0] == '+') &&
1781 !t->text[1]))
1782 t = t->next;
H. Peter Anvind85d2502008-05-04 17:53:31 -07001783
H. Peter Anvincbf768d2008-02-16 16:41:25 -08001784 j = tok_type_(t, needtype);
1785 break;
1786
1787 case PPC_IFTOKEN:
1788 t = tline = expand_smacro(tline);
1789 while (tok_type_(t, TOK_WHITESPACE))
1790 t = t->next;
1791
1792 j = false;
1793 if (t) {
1794 t = t->next; /* Skip the actual token */
1795 while (tok_type_(t, TOK_WHITESPACE))
1796 t = t->next;
1797 j = !t; /* Should be nothing left */
1798 }
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001799 break;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001800
H. Peter Anvin134b9462008-02-16 17:01:40 -08001801 case PPC_IFEMPTY:
1802 t = tline = expand_smacro(tline);
1803 while (tok_type_(t, TOK_WHITESPACE))
1804 t = t->next;
1805
1806 j = !t; /* Should be empty */
1807 break;
1808
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001809 case PPC_IF:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001810 t = tline = expand_smacro(tline);
1811 tptr = &t;
1812 tokval.t_type = TOKEN_INVALID;
1813 evalresult = evaluate(ppscan, tptr, &tokval,
1814 NULL, pass | CRITICAL, error, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001815 if (!evalresult)
1816 return -1;
1817 if (tokval.t_type)
H. Peter Anvin917a3492008-09-24 09:14:49 -07001818 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +00001819 "trailing garbage after expression ignored");
1820 if (!is_simple(evalresult)) {
1821 error(ERR_NONFATAL,
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001822 "non-constant value given to `%s'", pp_directives[ct]);
1823 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001824 }
Chuck Crayne60ae75d2007-05-02 01:59:16 +00001825 j = reloc_value(evalresult) != 0;
H. Peter Anvin90e6e812008-07-16 14:38:24 -07001826 break;
H. Peter Anvin95e28822007-09-12 04:20:08 +00001827
H. Peter Anvine2c80182005-01-15 22:15:51 +00001828 default:
1829 error(ERR_FATAL,
1830 "preprocessor directive `%s' not yet implemented",
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001831 pp_directives[ct]);
1832 goto fail;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001833 }
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001834
1835 free_tlist(origline);
1836 return j ^ PP_NEGATIVE(ct);
H. Peter Anvin70653092007-10-19 14:42:29 -07001837
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001838fail:
1839 free_tlist(origline);
1840 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001841}
1842
1843/*
H. Peter Anvin4db5a162007-10-11 13:42:09 -07001844 * Common code for defining an smacro
1845 */
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001846static bool define_smacro(Context *ctx, const char *mname, bool casesense,
H. Peter Anvin4db5a162007-10-11 13:42:09 -07001847 int nparam, Token *expansion)
1848{
1849 SMacro *smac, **smhead;
H. Peter Anvin166c2472008-05-28 12:28:58 -07001850 struct hash_table *smtbl;
H. Peter Anvin70653092007-10-19 14:42:29 -07001851
H. Peter Anvin4db5a162007-10-11 13:42:09 -07001852 if (smacro_defined(ctx, mname, nparam, &smac, casesense)) {
1853 if (!smac) {
H. Peter Anvin917a3492008-09-24 09:14:49 -07001854 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvin4db5a162007-10-11 13:42:09 -07001855 "single-line macro `%s' defined both with and"
1856 " without parameters", mname);
1857
1858 /* Some instances of the old code considered this a failure,
1859 some others didn't. What is the right thing to do here? */
1860 free_tlist(expansion);
1861 return false; /* Failure */
1862 } else {
1863 /*
1864 * We're redefining, so we have to take over an
1865 * existing SMacro structure. This means freeing
1866 * what was already in it.
1867 */
1868 nasm_free(smac->name);
1869 free_tlist(smac->expansion);
1870 }
1871 } else {
H. Peter Anvin166c2472008-05-28 12:28:58 -07001872 smtbl = ctx ? &ctx->localmac : &smacros;
1873 smhead = (SMacro **) hash_findi_add(smtbl, mname);
H. Peter Anvin4db5a162007-10-11 13:42:09 -07001874 smac = nasm_malloc(sizeof(SMacro));
1875 smac->next = *smhead;
1876 *smhead = smac;
1877 }
1878 smac->name = nasm_strdup(mname);
1879 smac->casesense = casesense;
1880 smac->nparam = nparam;
1881 smac->expansion = expansion;
1882 smac->in_progress = false;
1883 return true; /* Success */
1884}
1885
1886/*
1887 * Undefine an smacro
1888 */
1889static void undef_smacro(Context *ctx, const char *mname)
1890{
1891 SMacro **smhead, *s, **sp;
H. Peter Anvin166c2472008-05-28 12:28:58 -07001892 struct hash_table *smtbl;
H. Peter Anvin4db5a162007-10-11 13:42:09 -07001893
H. Peter Anvin166c2472008-05-28 12:28:58 -07001894 smtbl = ctx ? &ctx->localmac : &smacros;
1895 smhead = (SMacro **)hash_findi(smtbl, mname, NULL);
H. Peter Anvin70653092007-10-19 14:42:29 -07001896
H. Peter Anvin4db5a162007-10-11 13:42:09 -07001897 if (smhead) {
1898 /*
1899 * We now have a macro name... go hunt for it.
1900 */
1901 sp = smhead;
1902 while ((s = *sp) != NULL) {
1903 if (!mstrcmp(s->name, mname, s->casesense)) {
1904 *sp = s->next;
1905 nasm_free(s->name);
1906 free_tlist(s->expansion);
1907 nasm_free(s);
1908 } else {
1909 sp = &s->next;
1910 }
1911 }
1912 }
1913}
1914
H. Peter Anvin8781cb02007-11-08 20:01:11 -08001915/*
H. Peter Anvina26433d2008-07-16 14:40:01 -07001916 * Parse a mmacro specification.
1917 */
1918static bool parse_mmacro_spec(Token *tline, MMacro *def, const char *directive)
1919{
1920 bool err;
1921
1922 tline = tline->next;
1923 skip_white_(tline);
1924 tline = expand_id(tline);
1925 if (!tok_type_(tline, TOK_ID)) {
1926 error(ERR_NONFATAL, "`%s' expects a macro name", directive);
1927 return false;
1928 }
Victor van den Elzenb916ede2008-07-23 15:14:22 +02001929
H. Peter Anvin89cee572009-07-15 09:16:54 -04001930 def->prev = NULL;
H. Peter Anvina26433d2008-07-16 14:40:01 -07001931 def->name = nasm_strdup(tline->text);
1932 def->plus = false;
1933 def->nolist = false;
1934 def->in_progress = 0;
1935 def->rep_nest = NULL;
Victor van den Elzenb916ede2008-07-23 15:14:22 +02001936 def->nparam_min = 0;
1937 def->nparam_max = 0;
1938
H. Peter Anvina26433d2008-07-16 14:40:01 -07001939 tline = expand_smacro(tline->next);
1940 skip_white_(tline);
1941 if (!tok_type_(tline, TOK_NUMBER)) {
1942 error(ERR_NONFATAL, "`%s' expects a parameter count", directive);
H. Peter Anvina26433d2008-07-16 14:40:01 -07001943 } else {
1944 def->nparam_min = def->nparam_max =
1945 readnum(tline->text, &err);
1946 if (err)
1947 error(ERR_NONFATAL,
1948 "unable to parse parameter count `%s'", tline->text);
1949 }
1950 if (tline && tok_is_(tline->next, "-")) {
1951 tline = tline->next->next;
1952 if (tok_is_(tline, "*")) {
1953 def->nparam_max = INT_MAX;
1954 } else if (!tok_type_(tline, TOK_NUMBER)) {
1955 error(ERR_NONFATAL,
1956 "`%s' expects a parameter count after `-'", directive);
1957 } else {
1958 def->nparam_max = readnum(tline->text, &err);
1959 if (err) {
1960 error(ERR_NONFATAL, "unable to parse parameter count `%s'",
1961 tline->text);
1962 }
1963 if (def->nparam_min > def->nparam_max) {
1964 error(ERR_NONFATAL, "minimum parameter count exceeds maximum");
1965 }
1966 }
1967 }
1968 if (tline && tok_is_(tline->next, "+")) {
1969 tline = tline->next;
1970 def->plus = true;
1971 }
1972 if (tline && tok_type_(tline->next, TOK_ID) &&
1973 !nasm_stricmp(tline->next->text, ".nolist")) {
1974 tline = tline->next;
1975 def->nolist = true;
1976 }
Keith Kanios891775e2009-07-11 06:08:54 -05001977
H. Peter Anvina26433d2008-07-16 14:40:01 -07001978 /*
1979 * Handle default parameters.
1980 */
1981 if (tline && tline->next) {
1982 def->dlist = tline->next;
1983 tline->next = NULL;
1984 count_mmac_params(def->dlist, &def->ndefs, &def->defaults);
1985 } else {
1986 def->dlist = NULL;
1987 def->defaults = NULL;
1988 }
1989 def->expansion = NULL;
1990
H. Peter Anvin89cee572009-07-15 09:16:54 -04001991 if (def->defaults && def->ndefs > def->nparam_max - def->nparam_min &&
1992 !def->plus)
1993 error(ERR_WARNING|ERR_PASS1|ERR_WARN_MDP,
1994 "too many default macro parameters");
Victor van den Elzenb916ede2008-07-23 15:14:22 +02001995
H. Peter Anvina26433d2008-07-16 14:40:01 -07001996 return true;
1997}
1998
1999
2000/*
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002001 * Decode a size directive
2002 */
2003static int parse_size(const char *str) {
2004 static const char *size_names[] =
H. Peter Anvindfb91802008-05-20 11:43:53 -07002005 { "byte", "dword", "oword", "qword", "tword", "word", "yword" };
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002006 static const int sizes[] =
H. Peter Anvindfb91802008-05-20 11:43:53 -07002007 { 0, 1, 4, 16, 8, 10, 2, 32 };
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002008
2009 return sizes[bsii(str, size_names, elements(size_names))+1];
2010}
2011
H. Peter Anvinf9c9a672009-07-14 15:14:05 -04002012/*
2013 * nasm_unquote with error if the string contains NUL characters.
2014 * If the string contains NUL characters, issue an error and return
2015 * the C len, i.e. truncate at the NUL.
2016 */
2017static size_t nasm_unquote_cstr(char *qstr, enum preproc_token directive)
2018{
2019 size_t len = nasm_unquote(qstr, NULL);
2020 size_t clen = strlen(qstr);
2021
2022 if (len != clen)
H. Peter Anvincd0943e2009-07-14 15:17:11 -04002023 error(ERR_NONFATAL, "NUL character in `%s' directive",
2024 pp_directives[directive]);
H. Peter Anvinf9c9a672009-07-14 15:14:05 -04002025
2026 return clen;
2027}
2028
Ed Beroset3ab3f412002-06-11 03:31:49 +00002029/**
2030 * find and process preprocessor directive in passed line
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002031 * Find out if a line contains a preprocessor directive, and deal
2032 * with it if so.
H. Peter Anvin70653092007-10-19 14:42:29 -07002033 *
Ed Beroset3ab3f412002-06-11 03:31:49 +00002034 * If a directive _is_ found, it is the responsibility of this routine
2035 * (and not the caller) to free_tlist() the line.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002036 *
Ed Beroset3ab3f412002-06-11 03:31:49 +00002037 * @param tline a pointer to the current tokeninzed line linked list
2038 * @return DIRECTIVE_FOUND or NO_DIRECTIVE_FOUND
H. Peter Anvin70653092007-10-19 14:42:29 -07002039 *
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002040 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002041static int do_directive(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00002042{
H. Peter Anvin4169a472007-09-12 01:29:43 +00002043 enum preproc_token i;
2044 int j;
H. Peter Anvin70055962007-10-11 00:05:31 -07002045 bool err;
2046 int nparam;
2047 bool nolist;
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07002048 bool casesense;
H. Peter Anvin8cfdb9d2007-09-14 18:36:01 -07002049 int k, m;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002050 int offset;
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08002051 char *p, *pp;
2052 const char *mname;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002053 Include *inc;
2054 Context *ctx;
2055 Cond *cond;
H. Peter Anvin97a23472007-09-16 17:57:25 -07002056 MMacro *mmac, **mmhead;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002057 Token *t, *tt, *param_start, *macro_start, *last, **tptr, *origline;
2058 Line *l;
2059 struct tokenval tokval;
2060 expr *evalresult;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002061 MMacro *tmp_defining; /* Used when manipulating rep_nest */
H. Peter Anvinf8ba53e2007-10-11 10:11:57 -07002062 int64_t count;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07002063 size_t len;
H. Peter Anvin8e3f75e2008-09-24 00:21:58 -07002064 int severity;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002065
2066 origline = tline;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002067
H. Peter Anvineba20a72002-04-30 20:53:55 +00002068 skip_white_(tline);
H. Peter Anvinf2936d72008-06-04 15:11:23 -07002069 if (!tline || !tok_type_(tline, TOK_PREPROC_ID) ||
H. Peter Anvine2c80182005-01-15 22:15:51 +00002070 (tline->text[1] == '%' || tline->text[1] == '$'
2071 || tline->text[1] == '!'))
2072 return NO_DIRECTIVE_FOUND;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002073
H. Peter Anvin4169a472007-09-12 01:29:43 +00002074 i = pp_token_hash(tline->text);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002075
2076 /*
2077 * If we're in a non-emitting branch of a condition construct,
H. Peter Anvin76690a12002-04-30 20:52:49 +00002078 * or walking to the end of an already terminated %rep block,
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002079 * we should ignore all directives except for condition
2080 * directives.
2081 */
H. Peter Anvin76690a12002-04-30 20:52:49 +00002082 if (((istk->conds && !emitting(istk->conds->state)) ||
H. Peter Anvine2c80182005-01-15 22:15:51 +00002083 (istk->mstk && !istk->mstk->in_progress)) && !is_condition(i)) {
2084 return NO_DIRECTIVE_FOUND;
H. Peter Anvineba20a72002-04-30 20:53:55 +00002085 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002086
2087 /*
H. Peter Anvin76690a12002-04-30 20:52:49 +00002088 * If we're defining a macro or reading a %rep block, we should
Victor van den Elzen8f1120f2008-07-16 13:41:37 +02002089 * ignore all directives except for %macro/%imacro (which nest),
2090 * %endm/%endmacro, and (only if we're in a %rep block) %endrep.
2091 * If we're in a %rep block, another %rep nests, so should be let through.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002092 */
2093 if (defining && i != PP_MACRO && i != PP_IMACRO &&
H. Peter Anvindb8f96e2009-07-15 09:07:29 -04002094 i != PP_RMACRO && i != PP_IRMACRO &&
H. Peter Anvine2c80182005-01-15 22:15:51 +00002095 i != PP_ENDMACRO && i != PP_ENDM &&
2096 (defining->name || (i != PP_ENDREP && i != PP_REP))) {
2097 return NO_DIRECTIVE_FOUND;
H. Peter Anvineba20a72002-04-30 20:53:55 +00002098 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002099
Charles Crayned4200be2008-07-12 16:42:33 -07002100 if (defining) {
Keith Kanios852f1ee2009-07-12 00:19:55 -05002101 if (i == PP_MACRO || i == PP_IMACRO ||
H. Peter Anvindb8f96e2009-07-15 09:07:29 -04002102 i == PP_RMACRO || i == PP_IRMACRO) {
Charles Crayned4200be2008-07-12 16:42:33 -07002103 nested_mac_count++;
2104 return NO_DIRECTIVE_FOUND;
2105 } else if (nested_mac_count > 0) {
2106 if (i == PP_ENDMACRO) {
2107 nested_mac_count--;
2108 return NO_DIRECTIVE_FOUND;
2109 }
2110 }
2111 if (!defining->name) {
2112 if (i == PP_REP) {
2113 nested_rep_count++;
2114 return NO_DIRECTIVE_FOUND;
2115 } else if (nested_rep_count > 0) {
2116 if (i == PP_ENDREP) {
2117 nested_rep_count--;
2118 return NO_DIRECTIVE_FOUND;
2119 }
2120 }
2121 }
2122 }
2123
H. Peter Anvin4169a472007-09-12 01:29:43 +00002124 switch (i) {
2125 case PP_INVALID:
H. Peter Anvine2c80182005-01-15 22:15:51 +00002126 error(ERR_NONFATAL, "unknown preprocessor directive `%s'",
2127 tline->text);
2128 return NO_DIRECTIVE_FOUND; /* didn't get it */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002129
H. Peter Anvine2c80182005-01-15 22:15:51 +00002130 case PP_STACKSIZE:
2131 /* Directive to tell NASM what the default stack size is. The
2132 * default is for a 16-bit stack, and this can be overriden with
2133 * %stacksize large.
2134 * the following form:
2135 *
2136 * ARG arg1:WORD, arg2:DWORD, arg4:QWORD
2137 */
2138 tline = tline->next;
2139 if (tline && tline->type == TOK_WHITESPACE)
2140 tline = tline->next;
2141 if (!tline || tline->type != TOK_ID) {
2142 error(ERR_NONFATAL, "`%%stacksize' missing size parameter");
2143 free_tlist(origline);
2144 return DIRECTIVE_FOUND;
2145 }
2146 if (nasm_stricmp(tline->text, "flat") == 0) {
2147 /* All subsequent ARG directives are for a 32-bit stack */
2148 StackSize = 4;
2149 StackPointer = "ebp";
2150 ArgOffset = 8;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002151 LocalOffset = 0;
Charles Crayne7eaf9192007-11-08 22:11:14 -08002152 } else if (nasm_stricmp(tline->text, "flat64") == 0) {
2153 /* All subsequent ARG directives are for a 64-bit stack */
2154 StackSize = 8;
2155 StackPointer = "rbp";
2156 ArgOffset = 8;
2157 LocalOffset = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002158 } else if (nasm_stricmp(tline->text, "large") == 0) {
2159 /* All subsequent ARG directives are for a 16-bit stack,
2160 * far function call.
2161 */
2162 StackSize = 2;
2163 StackPointer = "bp";
2164 ArgOffset = 4;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002165 LocalOffset = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002166 } else if (nasm_stricmp(tline->text, "small") == 0) {
2167 /* All subsequent ARG directives are for a 16-bit stack,
2168 * far function call. We don't support near functions.
2169 */
2170 StackSize = 2;
2171 StackPointer = "bp";
2172 ArgOffset = 6;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002173 LocalOffset = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002174 } else {
2175 error(ERR_NONFATAL, "`%%stacksize' invalid size type");
2176 free_tlist(origline);
2177 return DIRECTIVE_FOUND;
2178 }
2179 free_tlist(origline);
2180 return DIRECTIVE_FOUND;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002181
H. Peter Anvine2c80182005-01-15 22:15:51 +00002182 case PP_ARG:
2183 /* TASM like ARG directive to define arguments to functions, in
2184 * the following form:
2185 *
2186 * ARG arg1:WORD, arg2:DWORD, arg4:QWORD
2187 */
2188 offset = ArgOffset;
2189 do {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00002190 char *arg, directive[256];
H. Peter Anvine2c80182005-01-15 22:15:51 +00002191 int size = StackSize;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002192
H. Peter Anvine2c80182005-01-15 22:15:51 +00002193 /* Find the argument name */
2194 tline = tline->next;
2195 if (tline && tline->type == TOK_WHITESPACE)
2196 tline = tline->next;
2197 if (!tline || tline->type != TOK_ID) {
2198 error(ERR_NONFATAL, "`%%arg' missing argument parameter");
2199 free_tlist(origline);
2200 return DIRECTIVE_FOUND;
2201 }
2202 arg = tline->text;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002203
H. Peter Anvine2c80182005-01-15 22:15:51 +00002204 /* Find the argument size type */
2205 tline = tline->next;
2206 if (!tline || tline->type != TOK_OTHER
2207 || tline->text[0] != ':') {
2208 error(ERR_NONFATAL,
2209 "Syntax error processing `%%arg' directive");
2210 free_tlist(origline);
2211 return DIRECTIVE_FOUND;
2212 }
2213 tline = tline->next;
2214 if (!tline || tline->type != TOK_ID) {
2215 error(ERR_NONFATAL, "`%%arg' missing size type parameter");
2216 free_tlist(origline);
2217 return DIRECTIVE_FOUND;
2218 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002219
H. Peter Anvine2c80182005-01-15 22:15:51 +00002220 /* Allow macro expansion of type parameter */
Keith Kaniosb7a89542007-04-12 02:40:54 +00002221 tt = tokenize(tline->text);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002222 tt = expand_smacro(tt);
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002223 size = parse_size(tt->text);
2224 if (!size) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002225 error(ERR_NONFATAL,
2226 "Invalid size type for `%%arg' missing directive");
2227 free_tlist(tt);
2228 free_tlist(origline);
2229 return DIRECTIVE_FOUND;
2230 }
2231 free_tlist(tt);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002232
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002233 /* Round up to even stack slots */
2234 size = (size+StackSize-1) & ~(StackSize-1);
2235
H. Peter Anvine2c80182005-01-15 22:15:51 +00002236 /* Now define the macro for the argument */
2237 snprintf(directive, sizeof(directive), "%%define %s (%s+%d)",
2238 arg, StackPointer, offset);
Keith Kaniosb7a89542007-04-12 02:40:54 +00002239 do_directive(tokenize(directive));
H. Peter Anvine2c80182005-01-15 22:15:51 +00002240 offset += size;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002241
H. Peter Anvine2c80182005-01-15 22:15:51 +00002242 /* Move to the next argument in the list */
2243 tline = tline->next;
2244 if (tline && tline->type == TOK_WHITESPACE)
2245 tline = tline->next;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002246 } while (tline && tline->type == TOK_OTHER && tline->text[0] == ',');
2247 ArgOffset = offset;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002248 free_tlist(origline);
2249 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002250
H. Peter Anvine2c80182005-01-15 22:15:51 +00002251 case PP_LOCAL:
2252 /* TASM like LOCAL directive to define local variables for a
2253 * function, in the following form:
2254 *
2255 * LOCAL local1:WORD, local2:DWORD, local4:QWORD = LocalSize
2256 *
2257 * The '= LocalSize' at the end is ignored by NASM, but is
2258 * required by TASM to define the local parameter size (and used
2259 * by the TASM macro package).
2260 */
2261 offset = LocalOffset;
2262 do {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00002263 char *local, directive[256];
H. Peter Anvine2c80182005-01-15 22:15:51 +00002264 int size = StackSize;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002265
H. Peter Anvine2c80182005-01-15 22:15:51 +00002266 /* Find the argument name */
2267 tline = tline->next;
2268 if (tline && tline->type == TOK_WHITESPACE)
2269 tline = tline->next;
2270 if (!tline || tline->type != TOK_ID) {
2271 error(ERR_NONFATAL,
2272 "`%%local' missing argument parameter");
2273 free_tlist(origline);
2274 return DIRECTIVE_FOUND;
2275 }
2276 local = tline->text;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002277
H. Peter Anvine2c80182005-01-15 22:15:51 +00002278 /* Find the argument size type */
2279 tline = tline->next;
2280 if (!tline || tline->type != TOK_OTHER
2281 || tline->text[0] != ':') {
2282 error(ERR_NONFATAL,
2283 "Syntax error processing `%%local' directive");
2284 free_tlist(origline);
2285 return DIRECTIVE_FOUND;
2286 }
2287 tline = tline->next;
2288 if (!tline || tline->type != TOK_ID) {
2289 error(ERR_NONFATAL,
2290 "`%%local' missing size type parameter");
2291 free_tlist(origline);
2292 return DIRECTIVE_FOUND;
2293 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00002294
H. Peter Anvine2c80182005-01-15 22:15:51 +00002295 /* Allow macro expansion of type parameter */
Keith Kaniosb7a89542007-04-12 02:40:54 +00002296 tt = tokenize(tline->text);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002297 tt = expand_smacro(tt);
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002298 size = parse_size(tt->text);
2299 if (!size) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002300 error(ERR_NONFATAL,
2301 "Invalid size type for `%%local' missing directive");
2302 free_tlist(tt);
2303 free_tlist(origline);
2304 return DIRECTIVE_FOUND;
2305 }
2306 free_tlist(tt);
H. Peter Anvin734b1882002-04-30 21:01:08 +00002307
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002308 /* Round up to even stack slots */
2309 size = (size+StackSize-1) & ~(StackSize-1);
2310
2311 offset += size; /* Negative offset, increment before */
2312
2313 /* Now define the macro for the argument */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002314 snprintf(directive, sizeof(directive), "%%define %s (%s-%d)",
2315 local, StackPointer, offset);
Keith Kaniosb7a89542007-04-12 02:40:54 +00002316 do_directive(tokenize(directive));
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002317
H. Peter Anvine2c80182005-01-15 22:15:51 +00002318 /* Now define the assign to setup the enter_c macro correctly */
2319 snprintf(directive, sizeof(directive),
2320 "%%assign %%$localsize %%$localsize+%d", size);
Keith Kaniosb7a89542007-04-12 02:40:54 +00002321 do_directive(tokenize(directive));
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002322
H. Peter Anvine2c80182005-01-15 22:15:51 +00002323 /* Move to the next argument in the list */
2324 tline = tline->next;
2325 if (tline && tline->type == TOK_WHITESPACE)
2326 tline = tline->next;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002327 } while (tline && tline->type == TOK_OTHER && tline->text[0] == ',');
2328 LocalOffset = offset;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002329 free_tlist(origline);
2330 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002331
H. Peter Anvine2c80182005-01-15 22:15:51 +00002332 case PP_CLEAR:
2333 if (tline->next)
H. Peter Anvin917a3492008-09-24 09:14:49 -07002334 error(ERR_WARNING|ERR_PASS1,
2335 "trailing garbage after `%%clear' ignored");
H. Peter Anvin97a23472007-09-16 17:57:25 -07002336 free_macros();
2337 init_macros();
H. Peter Anvine2c80182005-01-15 22:15:51 +00002338 free_tlist(origline);
2339 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002340
H. Peter Anvin418ca702008-05-30 10:42:30 -07002341 case PP_DEPEND:
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002342 t = tline->next = expand_smacro(tline->next);
2343 skip_white_(t);
2344 if (!t || (t->type != TOK_STRING &&
H. Peter Anvin89cee572009-07-15 09:16:54 -04002345 t->type != TOK_INTERNAL_STRING)) {
H. Peter Anvin418ca702008-05-30 10:42:30 -07002346 error(ERR_NONFATAL, "`%%depend' expects a file name");
2347 free_tlist(origline);
2348 return DIRECTIVE_FOUND; /* but we did _something_ */
2349 }
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002350 if (t->next)
H. Peter Anvin917a3492008-09-24 09:14:49 -07002351 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvin418ca702008-05-30 10:42:30 -07002352 "trailing garbage after `%%depend' ignored");
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002353 p = t->text;
2354 if (t->type != TOK_INTERNAL_STRING)
H. Peter Anvinf9c9a672009-07-14 15:14:05 -04002355 nasm_unquote_cstr(p, i);
H. Peter Anvin418ca702008-05-30 10:42:30 -07002356 if (dephead && !in_list(*dephead, p)) {
2357 StrList *sl = nasm_malloc(strlen(p)+1+sizeof sl->next);
2358 sl->next = NULL;
2359 strcpy(sl->str, p);
2360 *deptail = sl;
2361 deptail = &sl->next;
2362 }
2363 free_tlist(origline);
2364 return DIRECTIVE_FOUND;
2365
2366 case PP_INCLUDE:
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002367 t = tline->next = expand_smacro(tline->next);
2368 skip_white_(t);
H. Peter Anvind2456592008-06-19 15:04:18 -07002369
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002370 if (!t || (t->type != TOK_STRING &&
H. Peter Anvin89cee572009-07-15 09:16:54 -04002371 t->type != TOK_INTERNAL_STRING)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002372 error(ERR_NONFATAL, "`%%include' expects a file name");
2373 free_tlist(origline);
2374 return DIRECTIVE_FOUND; /* but we did _something_ */
2375 }
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002376 if (t->next)
H. Peter Anvin917a3492008-09-24 09:14:49 -07002377 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +00002378 "trailing garbage after `%%include' ignored");
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002379 p = t->text;
2380 if (t->type != TOK_INTERNAL_STRING)
H. Peter Anvinf9c9a672009-07-14 15:14:05 -04002381 nasm_unquote_cstr(p, i);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002382 inc = nasm_malloc(sizeof(Include));
2383 inc->next = istk;
2384 inc->conds = NULL;
H. Peter Anvin2b1c3b92008-06-06 10:38:46 -07002385 inc->fp = inc_fopen(p, dephead, &deptail, pass == 0);
H. Peter Anvin418ca702008-05-30 10:42:30 -07002386 if (!inc->fp) {
H. Peter Anvin37a321f2007-09-24 13:41:58 -07002387 /* -MG given but file not found */
2388 nasm_free(inc);
2389 } else {
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002390 inc->fname = src_set_fname(nasm_strdup(p));
H. Peter Anvin37a321f2007-09-24 13:41:58 -07002391 inc->lineno = src_set_linnum(0);
2392 inc->lineinc = 1;
2393 inc->expansion = NULL;
2394 inc->mstk = NULL;
2395 istk = inc;
2396 list->uplevel(LIST_INCLUDE);
2397 }
2398 free_tlist(origline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002399 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002400
H. Peter Anvind2456592008-06-19 15:04:18 -07002401 case PP_USE:
H. Peter Anvinf4ae5ad2008-06-19 18:39:24 -07002402 {
H. Peter Anvina70547f2008-07-19 21:44:26 -07002403 static macros_t *use_pkg;
H. Peter Anvinb2a5fda2008-06-19 21:42:42 -07002404 const char *pkg_macro;
H. Peter Anvinf4ae5ad2008-06-19 18:39:24 -07002405
H. Peter Anvin264b7b92008-10-24 16:38:17 -07002406 tline = tline->next;
2407 skip_white_(tline);
2408 tline = expand_id(tline);
H. Peter Anvind2456592008-06-19 15:04:18 -07002409
H. Peter Anvin264b7b92008-10-24 16:38:17 -07002410 if (!tline || (tline->type != TOK_STRING &&
2411 tline->type != TOK_INTERNAL_STRING &&
2412 tline->type != TOK_ID)) {
H. Peter Anvin926fc402008-06-19 16:26:12 -07002413 error(ERR_NONFATAL, "`%%use' expects a package name");
H. Peter Anvind2456592008-06-19 15:04:18 -07002414 free_tlist(origline);
2415 return DIRECTIVE_FOUND; /* but we did _something_ */
2416 }
H. Peter Anvin264b7b92008-10-24 16:38:17 -07002417 if (tline->next)
H. Peter Anvin917a3492008-09-24 09:14:49 -07002418 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvind2456592008-06-19 15:04:18 -07002419 "trailing garbage after `%%use' ignored");
H. Peter Anvin264b7b92008-10-24 16:38:17 -07002420 if (tline->type == TOK_STRING)
H. Peter Anvinf9c9a672009-07-14 15:14:05 -04002421 nasm_unquote_cstr(tline->text, i);
H. Peter Anvin264b7b92008-10-24 16:38:17 -07002422 use_pkg = nasm_stdmac_find_package(tline->text);
H. Peter Anvinf4ae5ad2008-06-19 18:39:24 -07002423 if (!use_pkg)
H. Peter Anvin264b7b92008-10-24 16:38:17 -07002424 error(ERR_NONFATAL, "unknown `%%use' package: %s", tline->text);
H. Peter Anvinb2a5fda2008-06-19 21:42:42 -07002425 /* The first string will be <%define>__USE_*__ */
H. Peter Anvin7e50d232008-06-25 14:54:14 -07002426 pkg_macro = (char *)use_pkg + 1;
H. Peter Anvinf4ae5ad2008-06-19 18:39:24 -07002427 if (!smacro_defined(NULL, pkg_macro, 0, NULL, true)) {
2428 /* Not already included, go ahead and include it */
H. Peter Anvinf4ae5ad2008-06-19 18:39:24 -07002429 stdmacpos = use_pkg;
2430 }
H. Peter Anvind2456592008-06-19 15:04:18 -07002431 free_tlist(origline);
2432 return DIRECTIVE_FOUND;
H. Peter Anvinf4ae5ad2008-06-19 18:39:24 -07002433 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002434 case PP_PUSH:
H. Peter Anvine2c80182005-01-15 22:15:51 +00002435 case PP_REPL:
H. Peter Anvin42b56392008-10-24 16:24:21 -07002436 case PP_POP:
H. Peter Anvine2c80182005-01-15 22:15:51 +00002437 tline = tline->next;
2438 skip_white_(tline);
2439 tline = expand_id(tline);
H. Peter Anvin11dfa1a2008-07-02 18:11:04 -07002440 if (tline) {
2441 if (!tok_type_(tline, TOK_ID)) {
H. Peter Anvin42b56392008-10-24 16:24:21 -07002442 error(ERR_NONFATAL, "`%s' expects a context identifier",
2443 pp_directives[i]);
H. Peter Anvin11dfa1a2008-07-02 18:11:04 -07002444 free_tlist(origline);
2445 return DIRECTIVE_FOUND; /* but we did _something_ */
2446 }
2447 if (tline->next)
H. Peter Anvin917a3492008-09-24 09:14:49 -07002448 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvin42b56392008-10-24 16:24:21 -07002449 "trailing garbage after `%s' ignored",
2450 pp_directives[i]);
H. Peter Anvin11dfa1a2008-07-02 18:11:04 -07002451 p = nasm_strdup(tline->text);
2452 } else {
H. Peter Anvin42b56392008-10-24 16:24:21 -07002453 p = NULL; /* Anonymous */
H. Peter Anvin11dfa1a2008-07-02 18:11:04 -07002454 }
H. Peter Anvin42b56392008-10-24 16:24:21 -07002455
2456 if (i == PP_PUSH) {
2457 ctx = nasm_malloc(sizeof(Context));
2458 ctx->next = cstk;
2459 hash_init(&ctx->localmac, HASH_SMALL);
2460 ctx->name = p;
2461 ctx->number = unique++;
2462 cstk = ctx;
2463 } else {
2464 /* %pop or %repl */
2465 if (!cstk) {
2466 error(ERR_NONFATAL, "`%s': context stack is empty",
2467 pp_directives[i]);
2468 } else if (i == PP_POP) {
2469 if (p && (!cstk->name || nasm_stricmp(p, cstk->name)))
2470 error(ERR_NONFATAL, "`%%pop' in wrong context: %s, "
2471 "expected %s",
2472 cstk->name ? cstk->name : "anonymous", p);
2473 else
2474 ctx_pop();
2475 } else {
2476 /* i == PP_REPL */
2477 nasm_free(cstk->name);
2478 cstk->name = p;
2479 p = NULL;
2480 }
2481 nasm_free(p);
2482 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002483 free_tlist(origline);
H. Peter Anvina26433d2008-07-16 14:40:01 -07002484 return DIRECTIVE_FOUND;
H. Peter Anvin8e3f75e2008-09-24 00:21:58 -07002485 case PP_FATAL:
H. Peter Anvin931cab62009-07-07 16:06:21 -07002486 severity = ERR_FATAL;
H. Peter Anvin8e3f75e2008-09-24 00:21:58 -07002487 goto issue_error;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002488 case PP_ERROR:
H. Peter Anvin931cab62009-07-07 16:06:21 -07002489 severity = ERR_NONFATAL;
H. Peter Anvin8e3f75e2008-09-24 00:21:58 -07002490 goto issue_error;
H. Peter Anvin7df04172008-06-10 18:27:38 -07002491 case PP_WARNING:
H. Peter Anvin931cab62009-07-07 16:06:21 -07002492 severity = ERR_WARNING|ERR_WARN_USER;
H. Peter Anvin8e3f75e2008-09-24 00:21:58 -07002493 goto issue_error;
2494
2495 issue_error:
H. Peter Anvin7df04172008-06-10 18:27:38 -07002496 {
H. Peter Anvin8e3f75e2008-09-24 00:21:58 -07002497 /* Only error out if this is the final pass */
2498 if (pass != 2 && i != PP_FATAL)
2499 return DIRECTIVE_FOUND;
H. Peter Anvin89cee572009-07-15 09:16:54 -04002500
2501 tline->next = expand_smacro(tline->next);
2502 tline = tline->next;
2503 skip_white_(tline);
H. Peter Anvin7df04172008-06-10 18:27:38 -07002504 t = tline ? tline->next : NULL;
2505 skip_white_(t);
H. Peter Anvin89cee572009-07-15 09:16:54 -04002506 if (tok_type_(tline, TOK_STRING) && !t) {
H. Peter Anvin7df04172008-06-10 18:27:38 -07002507 /* The line contains only a quoted string */
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07002508 p = tline->text;
H. Peter Anvinf9c9a672009-07-14 15:14:05 -04002509 nasm_unquote(p, NULL); /* Ignore NUL character truncation */
H. Peter Anvin931cab62009-07-07 16:06:21 -07002510 error(severity, "%s", p);
H. Peter Anvin7df04172008-06-10 18:27:38 -07002511 } else {
2512 /* Not a quoted string, or more than a quoted string */
H. Peter Anvin89cee572009-07-15 09:16:54 -04002513 p = detoken(tline, false);
H. Peter Anvin931cab62009-07-07 16:06:21 -07002514 error(severity, "%s", p);
H. Peter Anvin7df04172008-06-10 18:27:38 -07002515 nasm_free(p);
H. Peter Anvind2456592008-06-19 15:04:18 -07002516 }
H. Peter Anvin89cee572009-07-15 09:16:54 -04002517 free_tlist(origline);
H. Peter Anvina26433d2008-07-16 14:40:01 -07002518 return DIRECTIVE_FOUND;
H. Peter Anvin7df04172008-06-10 18:27:38 -07002519 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00002520
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002521 CASE_PP_IF:
H. Peter Anvine2c80182005-01-15 22:15:51 +00002522 if (istk->conds && !emitting(istk->conds->state))
2523 j = COND_NEVER;
2524 else {
2525 j = if_condition(tline->next, i);
2526 tline->next = NULL; /* it got freed */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002527 j = j < 0 ? COND_NEVER : j ? COND_IF_TRUE : COND_IF_FALSE;
2528 }
2529 cond = nasm_malloc(sizeof(Cond));
2530 cond->next = istk->conds;
2531 cond->state = j;
2532 istk->conds = cond;
H. Peter Anvin7061ad72007-11-26 22:02:21 -08002533 free_tlist(origline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002534 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002535
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002536 CASE_PP_ELIF:
H. Peter Anvine2c80182005-01-15 22:15:51 +00002537 if (!istk->conds)
H. Peter Anvin4169a472007-09-12 01:29:43 +00002538 error(ERR_FATAL, "`%s': no matching `%%if'", pp_directives[i]);
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002539 switch(istk->conds->state) {
H. Peter Anvin89cee572009-07-15 09:16:54 -04002540 case COND_IF_TRUE:
2541 istk->conds->state = COND_DONE;
2542 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002543
H. Peter Anvin89cee572009-07-15 09:16:54 -04002544 case COND_DONE:
2545 case COND_NEVER:
2546 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002547
H. Peter Anvin89cee572009-07-15 09:16:54 -04002548 case COND_ELSE_TRUE:
2549 case COND_ELSE_FALSE:
2550 error_precond(ERR_WARNING|ERR_PASS1,
2551 "`%%elif' after `%%else' ignored");
2552 istk->conds->state = COND_NEVER;
2553 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002554
H. Peter Anvin89cee572009-07-15 09:16:54 -04002555 case COND_IF_FALSE:
2556 /*
2557 * IMPORTANT: In the case of %if, we will already have
2558 * called expand_mmac_params(); however, if we're
2559 * processing an %elif we must have been in a
2560 * non-emitting mode, which would have inhibited
2561 * the normal invocation of expand_mmac_params().
2562 * Therefore, we have to do it explicitly here.
2563 */
2564 j = if_condition(expand_mmac_params(tline->next), i);
2565 tline->next = NULL; /* it got freed */
2566 istk->conds->state =
2567 j < 0 ? COND_NEVER : j ? COND_IF_TRUE : COND_IF_FALSE;
2568 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002569 }
H. Peter Anvin7061ad72007-11-26 22:02:21 -08002570 free_tlist(origline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002571 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002572
H. Peter Anvine2c80182005-01-15 22:15:51 +00002573 case PP_ELSE:
2574 if (tline->next)
H. Peter Anvin917a3492008-09-24 09:14:49 -07002575 error_precond(ERR_WARNING|ERR_PASS1,
2576 "trailing garbage after `%%else' ignored");
H. Peter Anvine2c80182005-01-15 22:15:51 +00002577 if (!istk->conds)
2578 error(ERR_FATAL, "`%%else': no matching `%%if'");
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002579 switch(istk->conds->state) {
H. Peter Anvin89cee572009-07-15 09:16:54 -04002580 case COND_IF_TRUE:
2581 case COND_DONE:
2582 istk->conds->state = COND_ELSE_FALSE;
2583 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002584
H. Peter Anvin89cee572009-07-15 09:16:54 -04002585 case COND_NEVER:
2586 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002587
H. Peter Anvin89cee572009-07-15 09:16:54 -04002588 case COND_IF_FALSE:
2589 istk->conds->state = COND_ELSE_TRUE;
2590 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002591
H. Peter Anvin89cee572009-07-15 09:16:54 -04002592 case COND_ELSE_TRUE:
2593 case COND_ELSE_FALSE:
2594 error_precond(ERR_WARNING|ERR_PASS1,
2595 "`%%else' after `%%else' ignored.");
2596 istk->conds->state = COND_NEVER;
2597 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002598 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002599 free_tlist(origline);
2600 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002601
H. Peter Anvine2c80182005-01-15 22:15:51 +00002602 case PP_ENDIF:
2603 if (tline->next)
H. Peter Anvin917a3492008-09-24 09:14:49 -07002604 error_precond(ERR_WARNING|ERR_PASS1,
2605 "trailing garbage after `%%endif' ignored");
H. Peter Anvine2c80182005-01-15 22:15:51 +00002606 if (!istk->conds)
2607 error(ERR_FATAL, "`%%endif': no matching `%%if'");
2608 cond = istk->conds;
2609 istk->conds = cond->next;
2610 nasm_free(cond);
2611 free_tlist(origline);
2612 return DIRECTIVE_FOUND;
Keith Kanios0af5ee22009-07-11 14:26:34 -05002613
H. Peter Anvindb8f96e2009-07-15 09:07:29 -04002614 case PP_RMACRO:
2615 case PP_IRMACRO:
H. Peter Anvine2c80182005-01-15 22:15:51 +00002616 case PP_MACRO:
2617 case PP_IMACRO:
H. Peter Anvina26433d2008-07-16 14:40:01 -07002618 if (defining) {
H. Peter Anvindb8f96e2009-07-15 09:07:29 -04002619 error(ERR_FATAL, "`%s': already defining a macro",
2620 pp_directives[i]);
H. Peter Anvina26433d2008-07-16 14:40:01 -07002621 return DIRECTIVE_FOUND;
2622 }
H. Peter Anvindb8f96e2009-07-15 09:07:29 -04002623 defining = nasm_malloc(sizeof(MMacro));
2624 defining->max_depth =
2625 (i == PP_RMACRO) || (i == PP_IRMACRO) ? DEADMAN_LIMIT : 0;
2626 defining->casesense = (i == PP_MACRO) || (i == PP_RMACRO);
H. Peter Anvina26433d2008-07-16 14:40:01 -07002627 if (!parse_mmacro_spec(tline, defining, pp_directives[i])) {
2628 nasm_free(defining);
2629 defining = NULL;
2630 return DIRECTIVE_FOUND;
2631 }
2632
H. Peter Anvin166c2472008-05-28 12:28:58 -07002633 mmac = (MMacro *) hash_findix(&mmacros, defining->name);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002634 while (mmac) {
2635 if (!strcmp(mmac->name, defining->name) &&
2636 (mmac->nparam_min <= defining->nparam_max
2637 || defining->plus)
2638 && (defining->nparam_min <= mmac->nparam_max
2639 || mmac->plus)) {
H. Peter Anvin917a3492008-09-24 09:14:49 -07002640 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +00002641 "redefining multi-line macro `%s'", defining->name);
H. Peter Anvina26433d2008-07-16 14:40:01 -07002642 return DIRECTIVE_FOUND;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002643 }
2644 mmac = mmac->next;
2645 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002646 free_tlist(origline);
2647 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002648
H. Peter Anvine2c80182005-01-15 22:15:51 +00002649 case PP_ENDM:
2650 case PP_ENDMACRO:
Victor van den Elzen8f1120f2008-07-16 13:41:37 +02002651 if (! (defining && defining->name)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002652 error(ERR_NONFATAL, "`%s': not defining a macro", tline->text);
2653 return DIRECTIVE_FOUND;
2654 }
Keith Kanios852f1ee2009-07-12 00:19:55 -05002655 mmhead = (MMacro **) hash_findi_add(&mmacros, defining->name);
H. Peter Anvin97a23472007-09-16 17:57:25 -07002656 defining->next = *mmhead;
Keith Kanios852f1ee2009-07-12 00:19:55 -05002657 *mmhead = defining;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002658 defining = NULL;
2659 free_tlist(origline);
2660 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002661
H. Peter Anvin89cee572009-07-15 09:16:54 -04002662 case PP_EXITMACRO:
Keith Kanios852f1ee2009-07-12 00:19:55 -05002663 /*
2664 * We must search along istk->expansion until we hit a
2665 * macro-end marker for a macro with a name. Then we set
2666 * its `in_progress' flag to 0.
2667 */
H. Peter Anvin89cee572009-07-15 09:16:54 -04002668 for (l = istk->expansion; l; l = l->next)
Keith Kanios852f1ee2009-07-12 00:19:55 -05002669 if (l->finishes && l->finishes->name)
2670 break;
2671
2672 if (l) {
H. Peter Anvin89cee572009-07-15 09:16:54 -04002673 l->finishes->in_progress = 0;
Keith Kanios852f1ee2009-07-12 00:19:55 -05002674 } else {
2675 error(ERR_NONFATAL, "`%%exitmacro' not within `%%macro' block");
H. Peter Anvin89cee572009-07-15 09:16:54 -04002676 }
Keith Kanios852f1ee2009-07-12 00:19:55 -05002677 free_tlist(origline);
2678 return DIRECTIVE_FOUND;
2679
H. Peter Anvina26433d2008-07-16 14:40:01 -07002680 case PP_UNMACRO:
2681 case PP_UNIMACRO:
2682 {
2683 MMacro **mmac_p;
2684 MMacro spec;
2685
2686 spec.casesense = (i == PP_UNMACRO);
2687 if (!parse_mmacro_spec(tline, &spec, pp_directives[i])) {
2688 return DIRECTIVE_FOUND;
2689 }
2690 mmac_p = (MMacro **) hash_findi(&mmacros, spec.name, NULL);
2691 while (mmac_p && *mmac_p) {
2692 mmac = *mmac_p;
2693 if (mmac->casesense == spec.casesense &&
2694 !mstrcmp(mmac->name, spec.name, spec.casesense) &&
2695 mmac->nparam_min == spec.nparam_min &&
2696 mmac->nparam_max == spec.nparam_max &&
2697 mmac->plus == spec.plus) {
2698 *mmac_p = mmac->next;
2699 free_mmacro(mmac);
2700 } else {
2701 mmac_p = &mmac->next;
2702 }
2703 }
2704 free_tlist(origline);
2705 free_tlist(spec.dlist);
2706 return DIRECTIVE_FOUND;
2707 }
2708
H. Peter Anvine2c80182005-01-15 22:15:51 +00002709 case PP_ROTATE:
2710 if (tline->next && tline->next->type == TOK_WHITESPACE)
2711 tline = tline->next;
H. Peter Anvin89cee572009-07-15 09:16:54 -04002712 if (!tline->next) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002713 free_tlist(origline);
2714 error(ERR_NONFATAL, "`%%rotate' missing rotate count");
2715 return DIRECTIVE_FOUND;
2716 }
2717 t = expand_smacro(tline->next);
2718 tline->next = NULL;
2719 free_tlist(origline);
2720 tline = t;
2721 tptr = &t;
2722 tokval.t_type = TOKEN_INVALID;
2723 evalresult =
2724 evaluate(ppscan, tptr, &tokval, NULL, pass, error, NULL);
2725 free_tlist(tline);
2726 if (!evalresult)
2727 return DIRECTIVE_FOUND;
2728 if (tokval.t_type)
H. Peter Anvin917a3492008-09-24 09:14:49 -07002729 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +00002730 "trailing garbage after expression ignored");
2731 if (!is_simple(evalresult)) {
2732 error(ERR_NONFATAL, "non-constant value given to `%%rotate'");
2733 return DIRECTIVE_FOUND;
2734 }
2735 mmac = istk->mstk;
2736 while (mmac && !mmac->name) /* avoid mistaking %reps for macros */
2737 mmac = mmac->next_active;
2738 if (!mmac) {
2739 error(ERR_NONFATAL, "`%%rotate' invoked outside a macro call");
2740 } else if (mmac->nparam == 0) {
2741 error(ERR_NONFATAL,
2742 "`%%rotate' invoked within macro without parameters");
2743 } else {
H. Peter Anvin25a99342007-09-22 17:45:45 -07002744 int rotate = mmac->rotate + reloc_value(evalresult);
H. Peter Anvin734b1882002-04-30 21:01:08 +00002745
H. Peter Anvin25a99342007-09-22 17:45:45 -07002746 rotate %= (int)mmac->nparam;
2747 if (rotate < 0)
2748 rotate += mmac->nparam;
H. Peter Anvin70653092007-10-19 14:42:29 -07002749
H. Peter Anvin25a99342007-09-22 17:45:45 -07002750 mmac->rotate = rotate;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002751 }
2752 return DIRECTIVE_FOUND;
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00002753
H. Peter Anvine2c80182005-01-15 22:15:51 +00002754 case PP_REP:
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002755 nolist = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002756 do {
2757 tline = tline->next;
2758 } while (tok_type_(tline, TOK_WHITESPACE));
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00002759
H. Peter Anvine2c80182005-01-15 22:15:51 +00002760 if (tok_type_(tline, TOK_ID) &&
2761 nasm_stricmp(tline->text, ".nolist") == 0) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002762 nolist = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002763 do {
2764 tline = tline->next;
2765 } while (tok_type_(tline, TOK_WHITESPACE));
2766 }
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00002767
H. Peter Anvine2c80182005-01-15 22:15:51 +00002768 if (tline) {
2769 t = expand_smacro(tline);
2770 tptr = &t;
2771 tokval.t_type = TOKEN_INVALID;
2772 evalresult =
2773 evaluate(ppscan, tptr, &tokval, NULL, pass, error, NULL);
2774 if (!evalresult) {
2775 free_tlist(origline);
2776 return DIRECTIVE_FOUND;
2777 }
2778 if (tokval.t_type)
H. Peter Anvin917a3492008-09-24 09:14:49 -07002779 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +00002780 "trailing garbage after expression ignored");
2781 if (!is_simple(evalresult)) {
2782 error(ERR_NONFATAL, "non-constant value given to `%%rep'");
2783 return DIRECTIVE_FOUND;
2784 }
H. Peter Anvinf8ba53e2007-10-11 10:11:57 -07002785 count = reloc_value(evalresult) + 1;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002786 } else {
2787 error(ERR_NONFATAL, "`%%rep' expects a repeat count");
H. Peter Anvinf8ba53e2007-10-11 10:11:57 -07002788 count = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002789 }
2790 free_tlist(origline);
H. Peter Anvin734b1882002-04-30 21:01:08 +00002791
H. Peter Anvine2c80182005-01-15 22:15:51 +00002792 tmp_defining = defining;
2793 defining = nasm_malloc(sizeof(MMacro));
H. Peter Anvin89cee572009-07-15 09:16:54 -04002794 defining->prev = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002795 defining->name = NULL; /* flags this macro as a %rep block */
H. Peter Anvin70055962007-10-11 00:05:31 -07002796 defining->casesense = false;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002797 defining->plus = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002798 defining->nolist = nolist;
H. Peter Anvinf8ba53e2007-10-11 10:11:57 -07002799 defining->in_progress = count;
H. Peter Anvin89cee572009-07-15 09:16:54 -04002800 defining->max_depth = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002801 defining->nparam_min = defining->nparam_max = 0;
2802 defining->defaults = NULL;
2803 defining->dlist = NULL;
2804 defining->expansion = NULL;
2805 defining->next_active = istk->mstk;
2806 defining->rep_nest = tmp_defining;
2807 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002808
H. Peter Anvine2c80182005-01-15 22:15:51 +00002809 case PP_ENDREP:
2810 if (!defining || defining->name) {
2811 error(ERR_NONFATAL, "`%%endrep': no matching `%%rep'");
2812 return DIRECTIVE_FOUND;
2813 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00002814
H. Peter Anvine2c80182005-01-15 22:15:51 +00002815 /*
2816 * Now we have a "macro" defined - although it has no name
2817 * and we won't be entering it in the hash tables - we must
2818 * push a macro-end marker for it on to istk->expansion.
2819 * After that, it will take care of propagating itself (a
2820 * macro-end marker line for a macro which is really a %rep
2821 * block will cause the macro to be re-expanded, complete
2822 * with another macro-end marker to ensure the process
2823 * continues) until the whole expansion is forcibly removed
2824 * from istk->expansion by a %exitrep.
2825 */
2826 l = nasm_malloc(sizeof(Line));
2827 l->next = istk->expansion;
2828 l->finishes = defining;
2829 l->first = NULL;
2830 istk->expansion = l;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002831
H. Peter Anvine2c80182005-01-15 22:15:51 +00002832 istk->mstk = defining;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002833
H. Peter Anvine2c80182005-01-15 22:15:51 +00002834 list->uplevel(defining->nolist ? LIST_MACRO_NOLIST : LIST_MACRO);
2835 tmp_defining = defining;
2836 defining = defining->rep_nest;
2837 free_tlist(origline);
2838 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002839
H. Peter Anvine2c80182005-01-15 22:15:51 +00002840 case PP_EXITREP:
2841 /*
2842 * We must search along istk->expansion until we hit a
2843 * macro-end marker for a macro with no name. Then we set
2844 * its `in_progress' flag to 0.
2845 */
2846 for (l = istk->expansion; l; l = l->next)
2847 if (l->finishes && !l->finishes->name)
H. Peter Anvinca348b62008-07-23 10:49:26 -04002848 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002849
H. Peter Anvine2c80182005-01-15 22:15:51 +00002850 if (l)
Charles Crayned4200be2008-07-12 16:42:33 -07002851 l->finishes->in_progress = 1;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002852 else
2853 error(ERR_NONFATAL, "`%%exitrep' not within `%%rep' block");
2854 free_tlist(origline);
2855 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002856
H. Peter Anvine2c80182005-01-15 22:15:51 +00002857 case PP_XDEFINE:
2858 case PP_IXDEFINE:
2859 case PP_DEFINE:
2860 case PP_IDEFINE:
H. Peter Anvin95e7f952007-10-11 13:38:38 -07002861 casesense = (i == PP_DEFINE || i == PP_XDEFINE);
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07002862
H. Peter Anvine2c80182005-01-15 22:15:51 +00002863 tline = tline->next;
2864 skip_white_(tline);
2865 tline = expand_id(tline);
2866 if (!tline || (tline->type != TOK_ID &&
2867 (tline->type != TOK_PREPROC_ID ||
2868 tline->text[1] != '$'))) {
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07002869 error(ERR_NONFATAL, "`%s' expects a macro identifier",
2870 pp_directives[i]);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002871 free_tlist(origline);
2872 return DIRECTIVE_FOUND;
2873 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00002874
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08002875 ctx = get_ctx(tline->text, &mname, false);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002876 last = tline;
2877 param_start = tline = tline->next;
2878 nparam = 0;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002879
H. Peter Anvine2c80182005-01-15 22:15:51 +00002880 /* Expand the macro definition now for %xdefine and %ixdefine */
2881 if ((i == PP_XDEFINE) || (i == PP_IXDEFINE))
2882 tline = expand_smacro(tline);
H. Peter Anvin734b1882002-04-30 21:01:08 +00002883
H. Peter Anvine2c80182005-01-15 22:15:51 +00002884 if (tok_is_(tline, "(")) {
2885 /*
2886 * This macro has parameters.
2887 */
H. Peter Anvin734b1882002-04-30 21:01:08 +00002888
H. Peter Anvine2c80182005-01-15 22:15:51 +00002889 tline = tline->next;
2890 while (1) {
2891 skip_white_(tline);
2892 if (!tline) {
2893 error(ERR_NONFATAL, "parameter identifier expected");
2894 free_tlist(origline);
2895 return DIRECTIVE_FOUND;
2896 }
2897 if (tline->type != TOK_ID) {
2898 error(ERR_NONFATAL,
2899 "`%s': parameter identifier expected",
2900 tline->text);
2901 free_tlist(origline);
2902 return DIRECTIVE_FOUND;
2903 }
2904 tline->type = TOK_SMAC_PARAM + nparam++;
2905 tline = tline->next;
2906 skip_white_(tline);
2907 if (tok_is_(tline, ",")) {
2908 tline = tline->next;
H. Peter Anvinca348b62008-07-23 10:49:26 -04002909 } else {
2910 if (!tok_is_(tline, ")")) {
2911 error(ERR_NONFATAL,
2912 "`)' expected to terminate macro template");
2913 free_tlist(origline);
2914 return DIRECTIVE_FOUND;
2915 }
2916 break;
2917 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002918 }
2919 last = tline;
2920 tline = tline->next;
2921 }
2922 if (tok_type_(tline, TOK_WHITESPACE))
2923 last = tline, tline = tline->next;
2924 macro_start = NULL;
2925 last->next = NULL;
2926 t = tline;
2927 while (t) {
2928 if (t->type == TOK_ID) {
2929 for (tt = param_start; tt; tt = tt->next)
2930 if (tt->type >= TOK_SMAC_PARAM &&
2931 !strcmp(tt->text, t->text))
2932 t->type = tt->type;
2933 }
2934 tt = t->next;
2935 t->next = macro_start;
2936 macro_start = t;
2937 t = tt;
2938 }
2939 /*
2940 * Good. We now have a macro name, a parameter count, and a
2941 * token list (in reverse order) for an expansion. We ought
2942 * to be OK just to create an SMacro, store it, and let
2943 * free_tlist have the rest of the line (which we have
2944 * carefully re-terminated after chopping off the expansion
2945 * from the end).
2946 */
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002947 define_smacro(ctx, mname, casesense, nparam, macro_start);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002948 free_tlist(origline);
2949 return DIRECTIVE_FOUND;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002950
H. Peter Anvine2c80182005-01-15 22:15:51 +00002951 case PP_UNDEF:
2952 tline = tline->next;
2953 skip_white_(tline);
2954 tline = expand_id(tline);
2955 if (!tline || (tline->type != TOK_ID &&
2956 (tline->type != TOK_PREPROC_ID ||
2957 tline->text[1] != '$'))) {
2958 error(ERR_NONFATAL, "`%%undef' expects a macro identifier");
2959 free_tlist(origline);
2960 return DIRECTIVE_FOUND;
2961 }
2962 if (tline->next) {
H. Peter Anvin917a3492008-09-24 09:14:49 -07002963 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +00002964 "trailing garbage after macro name ignored");
2965 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002966
H. Peter Anvine2c80182005-01-15 22:15:51 +00002967 /* Find the context that symbol belongs to */
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08002968 ctx = get_ctx(tline->text, &mname, false);
2969 undef_smacro(ctx, mname);
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002970 free_tlist(origline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002971 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002972
H. Peter Anvin9e200162008-06-04 17:23:14 -07002973 case PP_DEFSTR:
2974 case PP_IDEFSTR:
2975 casesense = (i == PP_DEFSTR);
2976
2977 tline = tline->next;
2978 skip_white_(tline);
2979 tline = expand_id(tline);
2980 if (!tline || (tline->type != TOK_ID &&
2981 (tline->type != TOK_PREPROC_ID ||
2982 tline->text[1] != '$'))) {
2983 error(ERR_NONFATAL, "`%s' expects a macro identifier",
2984 pp_directives[i]);
2985 free_tlist(origline);
2986 return DIRECTIVE_FOUND;
2987 }
2988
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08002989 ctx = get_ctx(tline->text, &mname, false);
H. Peter Anvin9e200162008-06-04 17:23:14 -07002990 last = tline;
2991 tline = expand_smacro(tline->next);
2992 last->next = NULL;
2993
2994 while (tok_type_(tline, TOK_WHITESPACE))
2995 tline = delete_Token(tline);
2996
2997 p = detoken(tline, false);
2998 macro_start = nasm_malloc(sizeof(*macro_start));
2999 macro_start->next = NULL;
3000 macro_start->text = nasm_quote(p, strlen(p));
3001 macro_start->type = TOK_STRING;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003002 macro_start->a.mac = NULL;
H. Peter Anvin9e200162008-06-04 17:23:14 -07003003 nasm_free(p);
3004
3005 /*
3006 * We now have a macro name, an implicit parameter count of
3007 * zero, and a string token to use as an expansion. Create
3008 * and store an SMacro.
3009 */
3010 define_smacro(ctx, mname, casesense, 0, macro_start);
3011 free_tlist(origline);
3012 return DIRECTIVE_FOUND;
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003013
H. Peter Anvin2f55bda2009-07-14 15:04:04 -04003014 case PP_DEFTOK:
3015 case PP_IDEFTOK:
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003016 casesense = (i == PP_DEFTOK);
H. Peter Anvin89cee572009-07-15 09:16:54 -04003017
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003018 tline = tline->next;
3019 skip_white_(tline);
3020 tline = expand_id(tline);
3021 if (!tline || (tline->type != TOK_ID &&
3022 (tline->type != TOK_PREPROC_ID ||
3023 tline->text[1] != '$'))) {
3024 error(ERR_NONFATAL,
3025 "`%s' expects a macro identifier as first parameter",
H. Peter Anvin89cee572009-07-15 09:16:54 -04003026 pp_directives[i]);
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003027 free_tlist(origline);
3028 return DIRECTIVE_FOUND;
3029 }
3030 ctx = get_ctx(tline->text, &mname, false);
3031 last = tline;
3032 tline = expand_smacro(tline->next);
3033 last->next = NULL;
3034
3035 t = tline;
3036 while (tok_type_(t, TOK_WHITESPACE))
3037 t = t->next;
3038 /* t should now point to the string */
3039 if (t->type != TOK_STRING) {
3040 error(ERR_NONFATAL,
3041 "`%s` requires string as second parameter",
H. Peter Anvin89cee572009-07-15 09:16:54 -04003042 pp_directives[i]);
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003043 free_tlist(tline);
3044 free_tlist(origline);
3045 return DIRECTIVE_FOUND;
3046 }
3047
H. Peter Anvinf9c9a672009-07-14 15:14:05 -04003048 nasm_unquote_cstr(t->text, i);
H. Peter Anvin2f55bda2009-07-14 15:04:04 -04003049 macro_start = tokenize(t->text);
H. Peter Anvin89cee572009-07-15 09:16:54 -04003050
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003051 /*
3052 * We now have a macro name, an implicit parameter count of
3053 * zero, and a numeric token to use as an expansion. Create
3054 * and store an SMacro.
3055 */
3056 define_smacro(ctx, mname, casesense, 0, macro_start);
3057 free_tlist(tline);
3058 free_tlist(origline);
3059 return DIRECTIVE_FOUND;
H. Peter Anvin9e200162008-06-04 17:23:14 -07003060
H. Peter Anvin418ca702008-05-30 10:42:30 -07003061 case PP_PATHSEARCH:
3062 {
3063 FILE *fp;
3064 StrList *xsl = NULL;
H. Peter Anvin2b1c3b92008-06-06 10:38:46 -07003065 StrList **xst = &xsl;
H. Peter Anvin418ca702008-05-30 10:42:30 -07003066
3067 casesense = true;
3068
3069 tline = tline->next;
3070 skip_white_(tline);
3071 tline = expand_id(tline);
3072 if (!tline || (tline->type != TOK_ID &&
3073 (tline->type != TOK_PREPROC_ID ||
3074 tline->text[1] != '$'))) {
3075 error(ERR_NONFATAL,
3076 "`%%pathsearch' expects a macro identifier as first parameter");
3077 free_tlist(origline);
3078 return DIRECTIVE_FOUND;
3079 }
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08003080 ctx = get_ctx(tline->text, &mname, false);
H. Peter Anvin418ca702008-05-30 10:42:30 -07003081 last = tline;
3082 tline = expand_smacro(tline->next);
3083 last->next = NULL;
3084
3085 t = tline;
3086 while (tok_type_(t, TOK_WHITESPACE))
3087 t = t->next;
3088
3089 if (!t || (t->type != TOK_STRING &&
3090 t->type != TOK_INTERNAL_STRING)) {
3091 error(ERR_NONFATAL, "`%%pathsearch' expects a file name");
3092 free_tlist(tline);
3093 free_tlist(origline);
3094 return DIRECTIVE_FOUND; /* but we did _something_ */
3095 }
3096 if (t->next)
H. Peter Anvin917a3492008-09-24 09:14:49 -07003097 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvin418ca702008-05-30 10:42:30 -07003098 "trailing garbage after `%%pathsearch' ignored");
H. Peter Anvin427cc912008-06-01 21:43:03 -07003099 p = t->text;
3100 if (t->type != TOK_INTERNAL_STRING)
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07003101 nasm_unquote(p, NULL);
H. Peter Anvin418ca702008-05-30 10:42:30 -07003102
H. Peter Anvin2b1c3b92008-06-06 10:38:46 -07003103 fp = inc_fopen(p, &xsl, &xst, true);
H. Peter Anvin418ca702008-05-30 10:42:30 -07003104 if (fp) {
3105 p = xsl->str;
3106 fclose(fp); /* Don't actually care about the file */
3107 }
3108 macro_start = nasm_malloc(sizeof(*macro_start));
3109 macro_start->next = NULL;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003110 macro_start->text = nasm_quote(p, strlen(p));
H. Peter Anvin418ca702008-05-30 10:42:30 -07003111 macro_start->type = TOK_STRING;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003112 macro_start->a.mac = NULL;
H. Peter Anvin418ca702008-05-30 10:42:30 -07003113 if (xsl)
3114 nasm_free(xsl);
3115
3116 /*
3117 * We now have a macro name, an implicit parameter count of
3118 * zero, and a string token to use as an expansion. Create
3119 * and store an SMacro.
3120 */
3121 define_smacro(ctx, mname, casesense, 0, macro_start);
3122 free_tlist(tline);
3123 free_tlist(origline);
3124 return DIRECTIVE_FOUND;
3125 }
3126
H. Peter Anvine2c80182005-01-15 22:15:51 +00003127 case PP_STRLEN:
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07003128 casesense = true;
H. Peter Anvin70653092007-10-19 14:42:29 -07003129
H. Peter Anvine2c80182005-01-15 22:15:51 +00003130 tline = tline->next;
3131 skip_white_(tline);
3132 tline = expand_id(tline);
3133 if (!tline || (tline->type != TOK_ID &&
3134 (tline->type != TOK_PREPROC_ID ||
3135 tline->text[1] != '$'))) {
3136 error(ERR_NONFATAL,
3137 "`%%strlen' expects a macro identifier as first parameter");
3138 free_tlist(origline);
3139 return DIRECTIVE_FOUND;
3140 }
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08003141 ctx = get_ctx(tline->text, &mname, false);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003142 last = tline;
3143 tline = expand_smacro(tline->next);
3144 last->next = NULL;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003145
H. Peter Anvine2c80182005-01-15 22:15:51 +00003146 t = tline;
3147 while (tok_type_(t, TOK_WHITESPACE))
3148 t = t->next;
3149 /* t should now point to the string */
3150 if (t->type != TOK_STRING) {
3151 error(ERR_NONFATAL,
3152 "`%%strlen` requires string as second parameter");
3153 free_tlist(tline);
3154 free_tlist(origline);
3155 return DIRECTIVE_FOUND;
3156 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003157
H. Peter Anvine2c80182005-01-15 22:15:51 +00003158 macro_start = nasm_malloc(sizeof(*macro_start));
3159 macro_start->next = NULL;
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07003160 make_tok_num(macro_start, nasm_unquote(t->text, NULL));
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003161 macro_start->a.mac = NULL;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003162
H. Peter Anvine2c80182005-01-15 22:15:51 +00003163 /*
3164 * We now have a macro name, an implicit parameter count of
3165 * zero, and a numeric token to use as an expansion. Create
3166 * and store an SMacro.
3167 */
H. Peter Anvin4db5a162007-10-11 13:42:09 -07003168 define_smacro(ctx, mname, casesense, 0, macro_start);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003169 free_tlist(tline);
3170 free_tlist(origline);
3171 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003172
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003173 case PP_STRCAT:
3174 casesense = true;
3175
3176 tline = tline->next;
3177 skip_white_(tline);
3178 tline = expand_id(tline);
3179 if (!tline || (tline->type != TOK_ID &&
3180 (tline->type != TOK_PREPROC_ID ||
3181 tline->text[1] != '$'))) {
3182 error(ERR_NONFATAL,
3183 "`%%strcat' expects a macro identifier as first parameter");
3184 free_tlist(origline);
3185 return DIRECTIVE_FOUND;
3186 }
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08003187 ctx = get_ctx(tline->text, &mname, false);
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003188 last = tline;
3189 tline = expand_smacro(tline->next);
3190 last->next = NULL;
3191
3192 len = 0;
3193 for (t = tline; t; t = t->next) {
3194 switch (t->type) {
3195 case TOK_WHITESPACE:
3196 break;
3197 case TOK_STRING:
3198 len += t->a.len = nasm_unquote(t->text, NULL);
3199 break;
H. Peter Anvinaccf4332008-07-01 21:42:08 -07003200 case TOK_OTHER:
3201 if (!strcmp(t->text, ",")) /* permit comma separators */
3202 break;
3203 /* else fall through */
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003204 default:
3205 error(ERR_NONFATAL,
3206 "non-string passed to `%%strcat' (%d)", t->type);
3207 free_tlist(tline);
3208 free_tlist(origline);
3209 return DIRECTIVE_FOUND;
3210 }
3211 }
3212
3213 p = pp = nasm_malloc(len);
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003214 for (t = tline; t; t = t->next) {
3215 if (t->type == TOK_STRING) {
3216 memcpy(p, t->text, t->a.len);
3217 p += t->a.len;
3218 }
3219 }
3220
3221 /*
3222 * We now have a macro name, an implicit parameter count of
3223 * zero, and a numeric token to use as an expansion. Create
3224 * and store an SMacro.
3225 */
3226 macro_start = new_Token(NULL, TOK_STRING, NULL, 0);
3227 macro_start->text = nasm_quote(pp, len);
3228 nasm_free(pp);
3229 define_smacro(ctx, mname, casesense, 0, macro_start);
3230 free_tlist(tline);
3231 free_tlist(origline);
3232 return DIRECTIVE_FOUND;
3233
H. Peter Anvine2c80182005-01-15 22:15:51 +00003234 case PP_SUBSTR:
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003235 {
3236 int64_t a1, a2;
3237 size_t len;
H. Peter Anvind2456592008-06-19 15:04:18 -07003238
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07003239 casesense = true;
3240
H. Peter Anvine2c80182005-01-15 22:15:51 +00003241 tline = tline->next;
3242 skip_white_(tline);
3243 tline = expand_id(tline);
3244 if (!tline || (tline->type != TOK_ID &&
3245 (tline->type != TOK_PREPROC_ID ||
3246 tline->text[1] != '$'))) {
3247 error(ERR_NONFATAL,
3248 "`%%substr' expects a macro identifier as first parameter");
3249 free_tlist(origline);
3250 return DIRECTIVE_FOUND;
3251 }
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08003252 ctx = get_ctx(tline->text, &mname, false);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003253 last = tline;
3254 tline = expand_smacro(tline->next);
3255 last->next = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003256
H. Peter Anvine2c80182005-01-15 22:15:51 +00003257 t = tline->next;
3258 while (tok_type_(t, TOK_WHITESPACE))
3259 t = t->next;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003260
H. Peter Anvine2c80182005-01-15 22:15:51 +00003261 /* t should now point to the string */
3262 if (t->type != TOK_STRING) {
3263 error(ERR_NONFATAL,
3264 "`%%substr` requires string as second parameter");
3265 free_tlist(tline);
3266 free_tlist(origline);
3267 return DIRECTIVE_FOUND;
3268 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003269
H. Peter Anvine2c80182005-01-15 22:15:51 +00003270 tt = t->next;
3271 tptr = &tt;
3272 tokval.t_type = TOKEN_INVALID;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003273 evalresult = evaluate(ppscan, tptr, &tokval, NULL,
3274 pass, error, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003275 if (!evalresult) {
3276 free_tlist(tline);
3277 free_tlist(origline);
3278 return DIRECTIVE_FOUND;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003279 } else if (!is_simple(evalresult)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00003280 error(ERR_NONFATAL, "non-constant value given to `%%substr`");
3281 free_tlist(tline);
3282 free_tlist(origline);
3283 return DIRECTIVE_FOUND;
3284 }
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003285 a1 = evalresult->value-1;
3286
3287 while (tok_type_(tt, TOK_WHITESPACE))
3288 tt = tt->next;
3289 if (!tt) {
3290 a2 = 1; /* Backwards compatibility: one character */
3291 } else {
3292 tokval.t_type = TOKEN_INVALID;
3293 evalresult = evaluate(ppscan, tptr, &tokval, NULL,
3294 pass, error, NULL);
3295 if (!evalresult) {
3296 free_tlist(tline);
3297 free_tlist(origline);
3298 return DIRECTIVE_FOUND;
3299 } else if (!is_simple(evalresult)) {
3300 error(ERR_NONFATAL, "non-constant value given to `%%substr`");
3301 free_tlist(tline);
3302 free_tlist(origline);
3303 return DIRECTIVE_FOUND;
3304 }
3305 a2 = evalresult->value;
3306 }
3307
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07003308 len = nasm_unquote(t->text, NULL);
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003309 if (a2 < 0)
3310 a2 = a2+1+len-a1;
3311 if (a1+a2 > (int64_t)len)
3312 a2 = len-a1;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003313
H. Peter Anvine2c80182005-01-15 22:15:51 +00003314 macro_start = nasm_malloc(sizeof(*macro_start));
3315 macro_start->next = NULL;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003316 macro_start->text = nasm_quote((a1 < 0) ? "" : t->text+a1, a2);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003317 macro_start->type = TOK_STRING;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003318 macro_start->a.mac = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003319
H. Peter Anvine2c80182005-01-15 22:15:51 +00003320 /*
3321 * We now have a macro name, an implicit parameter count of
3322 * zero, and a numeric token to use as an expansion. Create
3323 * and store an SMacro.
3324 */
H. Peter Anvin4db5a162007-10-11 13:42:09 -07003325 define_smacro(ctx, mname, casesense, 0, macro_start);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003326 free_tlist(tline);
3327 free_tlist(origline);
3328 return DIRECTIVE_FOUND;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003329 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003330
H. Peter Anvine2c80182005-01-15 22:15:51 +00003331 case PP_ASSIGN:
3332 case PP_IASSIGN:
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07003333 casesense = (i == PP_ASSIGN);
3334
H. Peter Anvine2c80182005-01-15 22:15:51 +00003335 tline = tline->next;
3336 skip_white_(tline);
3337 tline = expand_id(tline);
3338 if (!tline || (tline->type != TOK_ID &&
3339 (tline->type != TOK_PREPROC_ID ||
3340 tline->text[1] != '$'))) {
3341 error(ERR_NONFATAL,
3342 "`%%%sassign' expects a macro identifier",
3343 (i == PP_IASSIGN ? "i" : ""));
3344 free_tlist(origline);
3345 return DIRECTIVE_FOUND;
3346 }
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08003347 ctx = get_ctx(tline->text, &mname, false);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003348 last = tline;
3349 tline = expand_smacro(tline->next);
3350 last->next = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003351
H. Peter Anvine2c80182005-01-15 22:15:51 +00003352 t = tline;
3353 tptr = &t;
3354 tokval.t_type = TOKEN_INVALID;
3355 evalresult =
3356 evaluate(ppscan, tptr, &tokval, NULL, pass, error, NULL);
3357 free_tlist(tline);
3358 if (!evalresult) {
3359 free_tlist(origline);
3360 return DIRECTIVE_FOUND;
3361 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003362
H. Peter Anvine2c80182005-01-15 22:15:51 +00003363 if (tokval.t_type)
H. Peter Anvin917a3492008-09-24 09:14:49 -07003364 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +00003365 "trailing garbage after expression ignored");
H. Peter Anvin734b1882002-04-30 21:01:08 +00003366
H. Peter Anvine2c80182005-01-15 22:15:51 +00003367 if (!is_simple(evalresult)) {
3368 error(ERR_NONFATAL,
3369 "non-constant value given to `%%%sassign'",
3370 (i == PP_IASSIGN ? "i" : ""));
3371 free_tlist(origline);
3372 return DIRECTIVE_FOUND;
3373 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003374
H. Peter Anvine2c80182005-01-15 22:15:51 +00003375 macro_start = nasm_malloc(sizeof(*macro_start));
3376 macro_start->next = NULL;
3377 make_tok_num(macro_start, reloc_value(evalresult));
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003378 macro_start->a.mac = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003379
H. Peter Anvine2c80182005-01-15 22:15:51 +00003380 /*
3381 * We now have a macro name, an implicit parameter count of
3382 * zero, and a numeric token to use as an expansion. Create
3383 * and store an SMacro.
3384 */
H. Peter Anvin4db5a162007-10-11 13:42:09 -07003385 define_smacro(ctx, mname, casesense, 0, macro_start);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003386 free_tlist(origline);
3387 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003388
H. Peter Anvine2c80182005-01-15 22:15:51 +00003389 case PP_LINE:
3390 /*
3391 * Syntax is `%line nnn[+mmm] [filename]'
3392 */
3393 tline = tline->next;
3394 skip_white_(tline);
3395 if (!tok_type_(tline, TOK_NUMBER)) {
3396 error(ERR_NONFATAL, "`%%line' expects line number");
3397 free_tlist(origline);
3398 return DIRECTIVE_FOUND;
3399 }
H. Peter Anvin70055962007-10-11 00:05:31 -07003400 k = readnum(tline->text, &err);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003401 m = 1;
3402 tline = tline->next;
3403 if (tok_is_(tline, "+")) {
3404 tline = tline->next;
3405 if (!tok_type_(tline, TOK_NUMBER)) {
3406 error(ERR_NONFATAL, "`%%line' expects line increment");
3407 free_tlist(origline);
3408 return DIRECTIVE_FOUND;
3409 }
H. Peter Anvin70055962007-10-11 00:05:31 -07003410 m = readnum(tline->text, &err);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003411 tline = tline->next;
3412 }
3413 skip_white_(tline);
3414 src_set_linnum(k);
3415 istk->lineinc = m;
3416 if (tline) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07003417 nasm_free(src_set_fname(detoken(tline, false)));
H. Peter Anvine2c80182005-01-15 22:15:51 +00003418 }
3419 free_tlist(origline);
3420 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003421
H. Peter Anvine2c80182005-01-15 22:15:51 +00003422 default:
3423 error(ERR_FATAL,
3424 "preprocessor directive `%s' not yet implemented",
H. Peter Anvin4169a472007-09-12 01:29:43 +00003425 pp_directives[i]);
H. Peter Anvina26433d2008-07-16 14:40:01 -07003426 return DIRECTIVE_FOUND;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003427 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003428}
3429
3430/*
H. Peter Anvin76690a12002-04-30 20:52:49 +00003431 * Ensure that a macro parameter contains a condition code and
3432 * nothing else. Return the condition code index if so, or -1
3433 * otherwise.
3434 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003435static int find_cc(Token * t)
H. Peter Anvineba20a72002-04-30 20:53:55 +00003436{
H. Peter Anvin76690a12002-04-30 20:52:49 +00003437 Token *tt;
3438 int i, j, k, m;
3439
H. Peter Anvin25a99342007-09-22 17:45:45 -07003440 if (!t)
H. Peter Anvin89cee572009-07-15 09:16:54 -04003441 return -1; /* Probably a %+ without a space */
H. Peter Anvin25a99342007-09-22 17:45:45 -07003442
H. Peter Anvineba20a72002-04-30 20:53:55 +00003443 skip_white_(t);
H. Peter Anvin76690a12002-04-30 20:52:49 +00003444 if (t->type != TOK_ID)
H. Peter Anvine2c80182005-01-15 22:15:51 +00003445 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003446 tt = t->next;
H. Peter Anvineba20a72002-04-30 20:53:55 +00003447 skip_white_(tt);
H. Peter Anvin76690a12002-04-30 20:52:49 +00003448 if (tt && (tt->type != TOK_OTHER || strcmp(tt->text, ",")))
H. Peter Anvine2c80182005-01-15 22:15:51 +00003449 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003450
3451 i = -1;
Ed Beroset3ab3f412002-06-11 03:31:49 +00003452 j = elements(conditions);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003453 while (j - i > 1) {
3454 k = (j + i) / 2;
3455 m = nasm_stricmp(t->text, conditions[k]);
3456 if (m == 0) {
3457 i = k;
3458 j = -2;
3459 break;
3460 } else if (m < 0) {
3461 j = k;
3462 } else
3463 i = k;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003464 }
3465 if (j != -2)
H. Peter Anvine2c80182005-01-15 22:15:51 +00003466 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003467 return i;
3468}
3469
H. Peter Anvind784a082009-04-20 14:01:18 -07003470static bool paste_tokens(Token **head, bool handle_paste_tokens)
3471{
3472 Token **tail, *t, *tt;
3473 Token **paste_head;
3474 bool did_paste = false;
3475 char *tmp;
3476
3477 /* Now handle token pasting... */
3478 paste_head = NULL;
3479 tail = head;
3480 while ((t = *tail) && (tt = t->next)) {
3481 switch (t->type) {
3482 case TOK_WHITESPACE:
3483 if (tt->type == TOK_WHITESPACE) {
3484 /* Zap adjacent whitespace tokens */
3485 t->next = delete_Token(tt);
3486 } else {
3487 /* Do not advance paste_head here */
3488 tail = &t->next;
3489 }
3490 break;
3491 case TOK_ID:
3492 case TOK_PREPROC_ID:
3493 case TOK_NUMBER:
3494 case TOK_FLOAT:
3495 {
3496 size_t len = 0;
3497 char *tmp, *p;
3498
3499 while (tt && (tt->type == TOK_ID || tt->type == TOK_PREPROC_ID ||
3500 tt->type == TOK_NUMBER || tt->type == TOK_FLOAT ||
3501 tt->type == TOK_OTHER)) {
3502 len += strlen(tt->text);
3503 tt = tt->next;
3504 }
3505
3506 /* Now tt points to the first token after the potential
3507 paste area... */
3508 if (tt != t->next) {
3509 /* We have at least two tokens... */
3510 len += strlen(t->text);
3511 p = tmp = nasm_malloc(len+1);
3512
3513 while (t != tt) {
3514 strcpy(p, t->text);
3515 p = strchr(p, '\0');
3516 t = delete_Token(t);
3517 }
3518
3519 t = *tail = tokenize(tmp);
3520 nasm_free(tmp);
3521
3522 while (t->next) {
3523 tail = &t->next;
3524 t = t->next;
3525 }
3526 t->next = tt; /* Attach the remaining token chain */
3527
3528 did_paste = true;
3529 }
3530 paste_head = tail;
3531 tail = &t->next;
3532 break;
3533 }
3534 case TOK_PASTE: /* %+ */
3535 if (handle_paste_tokens) {
3536 /* Zap %+ and whitespace tokens to the right */
3537 while (t && (t->type == TOK_WHITESPACE ||
3538 t->type == TOK_PASTE))
3539 t = *tail = delete_Token(t);
3540 if (!paste_head || !t)
3541 break; /* Nothing to paste with */
3542 tail = paste_head;
3543 t = *tail;
3544 tt = t->next;
3545 while (tok_type_(tt, TOK_WHITESPACE))
3546 tt = t->next = delete_Token(tt);
3547
3548 if (tt) {
3549 tmp = nasm_strcat(t->text, tt->text);
3550 delete_Token(t);
3551 tt = delete_Token(tt);
3552 t = *tail = tokenize(tmp);
3553 nasm_free(tmp);
3554 while (t->next) {
3555 tail = &t->next;
3556 t = t->next;
3557 }
3558 t->next = tt; /* Attach the remaining token chain */
3559 did_paste = true;
3560 }
3561 paste_head = tail;
3562 tail = &t->next;
3563 break;
3564 }
3565 /* else fall through */
3566 default:
3567 tail = paste_head = &t->next;
3568 break;
3569 }
3570 }
3571 return did_paste;
3572}
H. Peter Anvin76690a12002-04-30 20:52:49 +00003573/*
3574 * Expand MMacro-local things: parameter references (%0, %n, %+n,
H. Peter Anvin67c63722008-10-26 23:49:00 -07003575 * %-n) and MMacro-local identifiers (%%foo) as well as
3576 * macro indirection (%[...]).
H. Peter Anvin76690a12002-04-30 20:52:49 +00003577 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003578static Token *expand_mmac_params(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00003579{
H. Peter Anvin734b1882002-04-30 21:01:08 +00003580 Token *t, *tt, **tail, *thead;
H. Peter Anvin6125b622009-04-08 14:02:25 -07003581 bool changed = false;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003582
3583 tail = &thead;
3584 thead = NULL;
3585
H. Peter Anvine2c80182005-01-15 22:15:51 +00003586 while (tline) {
3587 if (tline->type == TOK_PREPROC_ID &&
3588 (((tline->text[1] == '+' || tline->text[1] == '-')
3589 && tline->text[2]) || tline->text[1] == '%'
3590 || (tline->text[1] >= '0' && tline->text[1] <= '9'))) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00003591 char *text = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003592 int type = 0, cc; /* type = 0 to placate optimisers */
Keith Kaniosa6dfa782007-04-13 16:47:53 +00003593 char tmpbuf[30];
H. Peter Anvin25a99342007-09-22 17:45:45 -07003594 unsigned int n;
3595 int i;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003596 MMacro *mac;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003597
H. Peter Anvine2c80182005-01-15 22:15:51 +00003598 t = tline;
3599 tline = tline->next;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003600
H. Peter Anvine2c80182005-01-15 22:15:51 +00003601 mac = istk->mstk;
3602 while (mac && !mac->name) /* avoid mistaking %reps for macros */
3603 mac = mac->next_active;
3604 if (!mac)
3605 error(ERR_NONFATAL, "`%s': not in a macro call", t->text);
3606 else
3607 switch (t->text[1]) {
3608 /*
3609 * We have to make a substitution of one of the
3610 * forms %1, %-1, %+1, %%foo, %0.
3611 */
3612 case '0':
3613 type = TOK_NUMBER;
3614 snprintf(tmpbuf, sizeof(tmpbuf), "%d", mac->nparam);
3615 text = nasm_strdup(tmpbuf);
3616 break;
3617 case '%':
3618 type = TOK_ID;
H. Peter Anvinf8ba53e2007-10-11 10:11:57 -07003619 snprintf(tmpbuf, sizeof(tmpbuf), "..@%"PRIu64".",
H. Peter Anvine2c80182005-01-15 22:15:51 +00003620 mac->unique);
3621 text = nasm_strcat(tmpbuf, t->text + 2);
3622 break;
3623 case '-':
3624 n = atoi(t->text + 2) - 1;
3625 if (n >= mac->nparam)
3626 tt = NULL;
3627 else {
3628 if (mac->nparam > 1)
3629 n = (n + mac->rotate) % mac->nparam;
3630 tt = mac->params[n];
3631 }
3632 cc = find_cc(tt);
3633 if (cc == -1) {
3634 error(ERR_NONFATAL,
3635 "macro parameter %d is not a condition code",
3636 n + 1);
3637 text = NULL;
3638 } else {
3639 type = TOK_ID;
3640 if (inverse_ccs[cc] == -1) {
3641 error(ERR_NONFATAL,
3642 "condition code `%s' is not invertible",
3643 conditions[cc]);
3644 text = NULL;
3645 } else
H. Peter Anvin67c63722008-10-26 23:49:00 -07003646 text = nasm_strdup(conditions[inverse_ccs[cc]]);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003647 }
3648 break;
3649 case '+':
3650 n = atoi(t->text + 2) - 1;
3651 if (n >= mac->nparam)
3652 tt = NULL;
3653 else {
3654 if (mac->nparam > 1)
3655 n = (n + mac->rotate) % mac->nparam;
3656 tt = mac->params[n];
3657 }
3658 cc = find_cc(tt);
3659 if (cc == -1) {
3660 error(ERR_NONFATAL,
3661 "macro parameter %d is not a condition code",
3662 n + 1);
3663 text = NULL;
3664 } else {
3665 type = TOK_ID;
3666 text = nasm_strdup(conditions[cc]);
3667 }
3668 break;
3669 default:
3670 n = atoi(t->text + 1) - 1;
3671 if (n >= mac->nparam)
3672 tt = NULL;
3673 else {
3674 if (mac->nparam > 1)
3675 n = (n + mac->rotate) % mac->nparam;
3676 tt = mac->params[n];
3677 }
3678 if (tt) {
3679 for (i = 0; i < mac->paramlen[n]; i++) {
3680 *tail = new_Token(NULL, tt->type, tt->text, 0);
3681 tail = &(*tail)->next;
3682 tt = tt->next;
3683 }
3684 }
3685 text = NULL; /* we've done it here */
3686 break;
3687 }
3688 if (!text) {
3689 delete_Token(t);
3690 } else {
3691 *tail = t;
3692 tail = &t->next;
3693 t->type = type;
3694 nasm_free(t->text);
3695 t->text = text;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003696 t->a.mac = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003697 }
H. Peter Anvin6125b622009-04-08 14:02:25 -07003698 changed = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003699 continue;
H. Peter Anvin67c63722008-10-26 23:49:00 -07003700 } else if (tline->type == TOK_INDIRECT) {
3701 t = tline;
3702 tline = tline->next;
3703 tt = tokenize(t->text);
3704 tt = expand_mmac_params(tt);
3705 tt = expand_smacro(tt);
3706 *tail = tt;
3707 while (tt) {
3708 tt->a.mac = NULL; /* Necessary? */
3709 tail = &tt->next;
3710 tt = tt->next;
3711 }
3712 delete_Token(t);
H. Peter Anvin6125b622009-04-08 14:02:25 -07003713 changed = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003714 } else {
3715 t = *tail = tline;
3716 tline = tline->next;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003717 t->a.mac = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003718 tail = &t->next;
3719 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00003720 }
H. Peter Anvineba20a72002-04-30 20:53:55 +00003721 *tail = NULL;
H. Peter Anvin67c63722008-10-26 23:49:00 -07003722
H. Peter Anvind784a082009-04-20 14:01:18 -07003723 if (changed)
H. Peter Anvinfc30f8c2009-07-06 18:48:23 -07003724 paste_tokens(&thead, false);
H. Peter Anvin6125b622009-04-08 14:02:25 -07003725
H. Peter Anvin76690a12002-04-30 20:52:49 +00003726 return thead;
3727}
3728
3729/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003730 * Expand all single-line macro calls made in the given line.
3731 * Return the expanded version of the line. The original is deemed
3732 * to be destroyed in the process. (In reality we'll just move
3733 * Tokens from input to output a lot of the time, rather than
3734 * actually bothering to destroy and replicate.)
3735 */
H. Peter Anvincb1cf592007-11-19 12:26:50 -08003736
H. Peter Anvine2c80182005-01-15 22:15:51 +00003737static Token *expand_smacro(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00003738{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003739 Token *t, *tt, *mstart, **tail, *thead;
H. Peter Anvin166c2472008-05-28 12:28:58 -07003740 struct hash_table *smtbl;
H. Peter Anvineba20a72002-04-30 20:53:55 +00003741 SMacro *head = NULL, *m;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003742 Token **params;
3743 int *paramsize;
H. Peter Anvin25a99342007-09-22 17:45:45 -07003744 unsigned int nparam, sparam;
H. Peter Anvind784a082009-04-20 14:01:18 -07003745 int brackets;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003746 Token *org_tline = tline;
3747 Context *ctx;
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08003748 const char *mname;
H. Peter Anvin2a15e692007-11-19 13:14:59 -08003749 int deadman = DEADMAN_LIMIT;
H. Peter Anvin8287daf2009-07-07 16:00:58 -07003750 bool expanded;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003751
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003752 /*
3753 * Trick: we should avoid changing the start token pointer since it can
3754 * be contained in "next" field of other token. Because of this
3755 * we allocate a copy of first token and work with it; at the end of
3756 * routine we copy it back
3757 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003758 if (org_tline) {
3759 tline =
3760 new_Token(org_tline->next, org_tline->type, org_tline->text,
3761 0);
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003762 tline->a.mac = org_tline->a.mac;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003763 nasm_free(org_tline->text);
3764 org_tline->text = NULL;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003765 }
3766
H. Peter Anvin8287daf2009-07-07 16:00:58 -07003767 expanded = true; /* Always expand %+ at least once */
3768
H. Peter Anvincb1cf592007-11-19 12:26:50 -08003769again:
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003770 tail = &thead;
3771 thead = NULL;
3772
H. Peter Anvine2c80182005-01-15 22:15:51 +00003773 while (tline) { /* main token loop */
H. Peter Anvin2a15e692007-11-19 13:14:59 -08003774 if (!--deadman) {
H. Peter Anvincb1cf592007-11-19 12:26:50 -08003775 error(ERR_NONFATAL, "interminable macro recursion");
3776 break;
3777 }
3778
H. Peter Anvine2c80182005-01-15 22:15:51 +00003779 if ((mname = tline->text)) {
3780 /* if this token is a local macro, look in local context */
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08003781 if (tline->type == TOK_ID || tline->type == TOK_PREPROC_ID)
3782 ctx = get_ctx(mname, &mname, true);
3783 else
3784 ctx = NULL;
3785 smtbl = ctx ? &ctx->localmac : &smacros;
H. Peter Anvin166c2472008-05-28 12:28:58 -07003786 head = (SMacro *) hash_findix(smtbl, mname);
H. Peter Anvin072771e2008-05-22 13:17:51 -07003787
H. Peter Anvine2c80182005-01-15 22:15:51 +00003788 /*
3789 * We've hit an identifier. As in is_mmacro below, we first
3790 * check whether the identifier is a single-line macro at
3791 * all, then think about checking for parameters if
3792 * necessary.
3793 */
H. Peter Anvin97a23472007-09-16 17:57:25 -07003794 for (m = head; m; m = m->next)
3795 if (!mstrcmp(m->name, mname, m->casesense))
3796 break;
3797 if (m) {
3798 mstart = tline;
3799 params = NULL;
3800 paramsize = NULL;
3801 if (m->nparam == 0) {
3802 /*
3803 * Simple case: the macro is parameterless. Discard the
3804 * one token that the macro call took, and push the
3805 * expansion back on the to-do stack.
3806 */
3807 if (!m->expansion) {
3808 if (!strcmp("__FILE__", m->name)) {
3809 int32_t num = 0;
H. Peter Anvin932de6c2008-07-31 18:46:11 -07003810 char *file = NULL;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003811 src_get(&num, &file);
3812 tline->text = nasm_quote(file, strlen(file));
H. Peter Anvin97a23472007-09-16 17:57:25 -07003813 tline->type = TOK_STRING;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003814 nasm_free(file);
H. Peter Anvin97a23472007-09-16 17:57:25 -07003815 continue;
3816 }
3817 if (!strcmp("__LINE__", m->name)) {
3818 nasm_free(tline->text);
3819 make_tok_num(tline, src_get_linnum());
3820 continue;
3821 }
3822 if (!strcmp("__BITS__", m->name)) {
3823 nasm_free(tline->text);
3824 make_tok_num(tline, globalbits);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003825 continue;
H. Peter Anvin97a23472007-09-16 17:57:25 -07003826 }
3827 tline = delete_Token(tline);
3828 continue;
3829 }
3830 } else {
3831 /*
3832 * Complicated case: at least one macro with this name
H. Peter Anvine2c80182005-01-15 22:15:51 +00003833 * exists and takes parameters. We must find the
3834 * parameters in the call, count them, find the SMacro
3835 * that corresponds to that form of the macro call, and
3836 * substitute for the parameters when we expand. What a
3837 * pain.
3838 */
3839 /*tline = tline->next;
H. Peter Anvin89cee572009-07-15 09:16:54 -04003840 skip_white_(tline); */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003841 do {
3842 t = tline->next;
3843 while (tok_type_(t, TOK_SMAC_END)) {
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003844 t->a.mac->in_progress = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003845 t->text = NULL;
3846 t = tline->next = delete_Token(t);
3847 }
3848 tline = t;
3849 } while (tok_type_(tline, TOK_WHITESPACE));
3850 if (!tok_is_(tline, "(")) {
3851 /*
3852 * This macro wasn't called with parameters: ignore
3853 * the call. (Behaviour borrowed from gnu cpp.)
3854 */
3855 tline = mstart;
3856 m = NULL;
3857 } else {
3858 int paren = 0;
3859 int white = 0;
3860 brackets = 0;
3861 nparam = 0;
3862 sparam = PARAM_DELTA;
3863 params = nasm_malloc(sparam * sizeof(Token *));
3864 params[0] = tline->next;
3865 paramsize = nasm_malloc(sparam * sizeof(int));
3866 paramsize[0] = 0;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07003867 while (true) { /* parameter loop */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003868 /*
3869 * For some unusual expansions
3870 * which concatenates function call
3871 */
3872 t = tline->next;
3873 while (tok_type_(t, TOK_SMAC_END)) {
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003874 t->a.mac->in_progress = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003875 t->text = NULL;
3876 t = tline->next = delete_Token(t);
3877 }
3878 tline = t;
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00003879
H. Peter Anvine2c80182005-01-15 22:15:51 +00003880 if (!tline) {
3881 error(ERR_NONFATAL,
3882 "macro call expects terminating `)'");
3883 break;
3884 }
3885 if (tline->type == TOK_WHITESPACE
3886 && brackets <= 0) {
3887 if (paramsize[nparam])
3888 white++;
3889 else
3890 params[nparam] = tline->next;
3891 continue; /* parameter loop */
3892 }
3893 if (tline->type == TOK_OTHER
3894 && tline->text[1] == 0) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00003895 char ch = tline->text[0];
H. Peter Anvine2c80182005-01-15 22:15:51 +00003896 if (ch == ',' && !paren && brackets <= 0) {
3897 if (++nparam >= sparam) {
3898 sparam += PARAM_DELTA;
3899 params = nasm_realloc(params,
3900 sparam *
3901 sizeof(Token
3902 *));
3903 paramsize =
3904 nasm_realloc(paramsize,
3905 sparam *
3906 sizeof(int));
3907 }
3908 params[nparam] = tline->next;
3909 paramsize[nparam] = 0;
3910 white = 0;
3911 continue; /* parameter loop */
3912 }
3913 if (ch == '{' &&
3914 (brackets > 0 || (brackets == 0 &&
3915 !paramsize[nparam])))
3916 {
3917 if (!(brackets++)) {
3918 params[nparam] = tline->next;
3919 continue; /* parameter loop */
3920 }
3921 }
3922 if (ch == '}' && brackets > 0)
3923 if (--brackets == 0) {
3924 brackets = -1;
3925 continue; /* parameter loop */
3926 }
3927 if (ch == '(' && !brackets)
3928 paren++;
3929 if (ch == ')' && brackets <= 0)
3930 if (--paren < 0)
3931 break;
3932 }
3933 if (brackets < 0) {
3934 brackets = 0;
3935 error(ERR_NONFATAL, "braces do not "
3936 "enclose all of macro parameter");
3937 }
3938 paramsize[nparam] += white + 1;
3939 white = 0;
3940 } /* parameter loop */
3941 nparam++;
3942 while (m && (m->nparam != nparam ||
3943 mstrcmp(m->name, mname,
3944 m->casesense)))
3945 m = m->next;
3946 if (!m)
H. Peter Anvin917a3492008-09-24 09:14:49 -07003947 error(ERR_WARNING|ERR_PASS1|ERR_WARN_MNP,
H. Peter Anvine2c80182005-01-15 22:15:51 +00003948 "macro `%s' exists, "
3949 "but not taking %d parameters",
3950 mstart->text, nparam);
3951 }
3952 }
3953 if (m && m->in_progress)
3954 m = NULL;
3955 if (!m) { /* in progess or didn't find '(' or wrong nparam */
H. Peter Anvin70653092007-10-19 14:42:29 -07003956 /*
H. Peter Anvine2c80182005-01-15 22:15:51 +00003957 * Design question: should we handle !tline, which
3958 * indicates missing ')' here, or expand those
3959 * macros anyway, which requires the (t) test a few
H. Peter Anvin70653092007-10-19 14:42:29 -07003960 * lines down?
H. Peter Anvine2c80182005-01-15 22:15:51 +00003961 */
3962 nasm_free(params);
3963 nasm_free(paramsize);
3964 tline = mstart;
3965 } else {
3966 /*
3967 * Expand the macro: we are placed on the last token of the
3968 * call, so that we can easily split the call from the
3969 * following tokens. We also start by pushing an SMAC_END
3970 * token for the cycle removal.
3971 */
3972 t = tline;
3973 if (t) {
3974 tline = t->next;
3975 t->next = NULL;
3976 }
3977 tt = new_Token(tline, TOK_SMAC_END, NULL, 0);
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003978 tt->a.mac = m;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07003979 m->in_progress = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003980 tline = tt;
3981 for (t = m->expansion; t; t = t->next) {
3982 if (t->type >= TOK_SMAC_PARAM) {
3983 Token *pcopy = tline, **ptail = &pcopy;
3984 Token *ttt, *pt;
3985 int i;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003986
H. Peter Anvine2c80182005-01-15 22:15:51 +00003987 ttt = params[t->type - TOK_SMAC_PARAM];
3988 for (i = paramsize[t->type - TOK_SMAC_PARAM];
3989 --i >= 0;) {
3990 pt = *ptail =
3991 new_Token(tline, ttt->type, ttt->text,
3992 0);
3993 ptail = &pt->next;
3994 ttt = ttt->next;
3995 }
3996 tline = pcopy;
H. Peter Anvin6c81f0a2008-05-25 21:46:17 -07003997 } else if (t->type == TOK_PREPROC_Q) {
3998 tt = new_Token(tline, TOK_ID, mname, 0);
3999 tline = tt;
4000 } else if (t->type == TOK_PREPROC_QQ) {
4001 tt = new_Token(tline, TOK_ID, m->name, 0);
4002 tline = tt;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004003 } else {
4004 tt = new_Token(tline, t->type, t->text, 0);
4005 tline = tt;
4006 }
4007 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00004008
H. Peter Anvine2c80182005-01-15 22:15:51 +00004009 /*
4010 * Having done that, get rid of the macro call, and clean
4011 * up the parameters.
4012 */
4013 nasm_free(params);
4014 nasm_free(paramsize);
4015 free_tlist(mstart);
H. Peter Anvind784a082009-04-20 14:01:18 -07004016 expanded = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004017 continue; /* main token loop */
4018 }
4019 }
4020 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004021
H. Peter Anvine2c80182005-01-15 22:15:51 +00004022 if (tline->type == TOK_SMAC_END) {
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004023 tline->a.mac->in_progress = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004024 tline = delete_Token(tline);
4025 } else {
4026 t = *tail = tline;
4027 tline = tline->next;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004028 t->a.mac = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004029 t->next = NULL;
4030 tail = &t->next;
4031 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004032 }
4033
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004034 /*
4035 * Now scan the entire line and look for successive TOK_IDs that resulted
Keith Kaniosb7a89542007-04-12 02:40:54 +00004036 * after expansion (they can't be produced by tokenize()). The successive
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004037 * TOK_IDs should be concatenated.
4038 * Also we look for %+ tokens and concatenate the tokens before and after
4039 * them (without white spaces in between).
4040 */
H. Peter Anvin8287daf2009-07-07 16:00:58 -07004041 if (expanded && paste_tokens(&thead, true)) {
H. Peter Anvinfc30f8c2009-07-06 18:48:23 -07004042 /*
4043 * If we concatenated something, *and* we had previously expanded
4044 * an actual macro, scan the lines again for macros...
4045 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004046 tline = thead;
H. Peter Anvin8287daf2009-07-07 16:00:58 -07004047 expanded = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004048 goto again;
H. Peter Anvin734b1882002-04-30 21:01:08 +00004049 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004050
H. Peter Anvine2c80182005-01-15 22:15:51 +00004051 if (org_tline) {
4052 if (thead) {
4053 *org_tline = *thead;
4054 /* since we just gave text to org_line, don't free it */
4055 thead->text = NULL;
4056 delete_Token(thead);
4057 } else {
4058 /* the expression expanded to empty line;
4059 we can't return NULL for some reasons
4060 we just set the line to a single WHITESPACE token. */
4061 memset(org_tline, 0, sizeof(*org_tline));
4062 org_tline->text = NULL;
4063 org_tline->type = TOK_WHITESPACE;
4064 }
4065 thead = org_tline;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004066 }
4067
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004068 return thead;
4069}
4070
4071/*
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004072 * Similar to expand_smacro but used exclusively with macro identifiers
4073 * right before they are fetched in. The reason is that there can be
4074 * identifiers consisting of several subparts. We consider that if there
4075 * are more than one element forming the name, user wants a expansion,
4076 * otherwise it will be left as-is. Example:
4077 *
4078 * %define %$abc cde
4079 *
4080 * the identifier %$abc will be left as-is so that the handler for %define
4081 * will suck it and define the corresponding value. Other case:
4082 *
4083 * %define _%$abc cde
4084 *
4085 * In this case user wants name to be expanded *before* %define starts
4086 * working, so we'll expand %$abc into something (if it has a value;
4087 * otherwise it will be left as-is) then concatenate all successive
4088 * PP_IDs into one.
4089 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004090static Token *expand_id(Token * tline)
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004091{
4092 Token *cur, *oldnext = NULL;
4093
H. Peter Anvin734b1882002-04-30 21:01:08 +00004094 if (!tline || !tline->next)
H. Peter Anvine2c80182005-01-15 22:15:51 +00004095 return tline;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004096
4097 cur = tline;
4098 while (cur->next &&
H. Peter Anvine2c80182005-01-15 22:15:51 +00004099 (cur->next->type == TOK_ID ||
4100 cur->next->type == TOK_PREPROC_ID
4101 || cur->next->type == TOK_NUMBER))
4102 cur = cur->next;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004103
4104 /* If identifier consists of just one token, don't expand */
4105 if (cur == tline)
H. Peter Anvine2c80182005-01-15 22:15:51 +00004106 return tline;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004107
H. Peter Anvine2c80182005-01-15 22:15:51 +00004108 if (cur) {
4109 oldnext = cur->next; /* Detach the tail past identifier */
4110 cur->next = NULL; /* so that expand_smacro stops here */
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004111 }
4112
H. Peter Anvin734b1882002-04-30 21:01:08 +00004113 tline = expand_smacro(tline);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004114
H. Peter Anvine2c80182005-01-15 22:15:51 +00004115 if (cur) {
4116 /* expand_smacro possibly changhed tline; re-scan for EOL */
4117 cur = tline;
4118 while (cur && cur->next)
4119 cur = cur->next;
4120 if (cur)
4121 cur->next = oldnext;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004122 }
4123
4124 return tline;
4125}
4126
4127/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004128 * Determine whether the given line constitutes a multi-line macro
4129 * call, and return the MMacro structure called if so. Doesn't have
4130 * to check for an initial label - that's taken care of in
4131 * expand_mmacro - but must check numbers of parameters. Guaranteed
4132 * to be called with tline->type == TOK_ID, so the putative macro
4133 * name is easy to find.
4134 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004135static MMacro *is_mmacro(Token * tline, Token *** params_array)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004136{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004137 MMacro *head, *m;
4138 Token **params;
4139 int nparam;
4140
H. Peter Anvin166c2472008-05-28 12:28:58 -07004141 head = (MMacro *) hash_findix(&mmacros, tline->text);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004142
4143 /*
4144 * Efficiency: first we see if any macro exists with the given
4145 * name. If not, we can return NULL immediately. _Then_ we
4146 * count the parameters, and then we look further along the
4147 * list if necessary to find the proper MMacro.
4148 */
4149 for (m = head; m; m = m->next)
H. Peter Anvine2c80182005-01-15 22:15:51 +00004150 if (!mstrcmp(m->name, tline->text, m->casesense))
4151 break;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004152 if (!m)
H. Peter Anvine2c80182005-01-15 22:15:51 +00004153 return NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004154
4155 /*
4156 * OK, we have a potential macro. Count and demarcate the
4157 * parameters.
4158 */
H. Peter Anvin734b1882002-04-30 21:01:08 +00004159 count_mmac_params(tline->next, &nparam, &params);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004160
4161 /*
4162 * So we know how many parameters we've got. Find the MMacro
4163 * structure that handles this number.
4164 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004165 while (m) {
4166 if (m->nparam_min <= nparam
4167 && (m->plus || nparam <= m->nparam_max)) {
4168 /*
4169 * This one is right. Just check if cycle removal
4170 * prohibits us using it before we actually celebrate...
4171 */
H. Peter Anvin89cee572009-07-15 09:16:54 -04004172 if (m->in_progress > m->max_depth) {
4173 if (m->max_depth > 0) {
4174 error(ERR_WARNING,
4175 "reached maximum recursion depth of %i",
4176 m->max_depth);
4177 }
4178 nasm_free(params);
4179 return NULL;
4180 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00004181 /*
4182 * It's right, and we can use it. Add its default
4183 * parameters to the end of our list if necessary.
4184 */
4185 if (m->defaults && nparam < m->nparam_min + m->ndefs) {
4186 params =
4187 nasm_realloc(params,
4188 ((m->nparam_min + m->ndefs +
4189 1) * sizeof(*params)));
4190 while (nparam < m->nparam_min + m->ndefs) {
4191 params[nparam] = m->defaults[nparam - m->nparam_min];
4192 nparam++;
4193 }
4194 }
4195 /*
4196 * If we've gone over the maximum parameter count (and
4197 * we're in Plus mode), ignore parameters beyond
4198 * nparam_max.
4199 */
4200 if (m->plus && nparam > m->nparam_max)
4201 nparam = m->nparam_max;
4202 /*
4203 * Then terminate the parameter list, and leave.
4204 */
4205 if (!params) { /* need this special case */
4206 params = nasm_malloc(sizeof(*params));
4207 nparam = 0;
4208 }
4209 params[nparam] = NULL;
4210 *params_array = params;
4211 return m;
4212 }
4213 /*
4214 * This one wasn't right: look for the next one with the
4215 * same name.
4216 */
4217 for (m = m->next; m; m = m->next)
4218 if (!mstrcmp(m->name, tline->text, m->casesense))
4219 break;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004220 }
4221
4222 /*
4223 * After all that, we didn't find one with the right number of
4224 * parameters. Issue a warning, and fail to expand the macro.
4225 */
H. Peter Anvin917a3492008-09-24 09:14:49 -07004226 error(ERR_WARNING|ERR_PASS1|ERR_WARN_MNP,
H. Peter Anvine2c80182005-01-15 22:15:51 +00004227 "macro `%s' exists, but not taking %d parameters",
4228 tline->text, nparam);
H. Peter Anvin734b1882002-04-30 21:01:08 +00004229 nasm_free(params);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004230 return NULL;
4231}
4232
Keith Kanios891775e2009-07-11 06:08:54 -05004233
4234/*
4235 * Save MMacro invocation specific fields in
4236 * preparation for a recursive macro expansion
4237 */
4238static void push_mmacro(MMacro *m)
4239{
4240 MMacroInvocation *i;
4241
H. Peter Anvin89cee572009-07-15 09:16:54 -04004242 i = nasm_malloc(sizeof(MMacroInvocation));
4243 i->prev = m->prev;
4244 i->params = m->params;
4245 i->iline = m->iline;
4246 i->nparam = m->nparam;
4247 i->rotate = m->rotate;
4248 i->paramlen = m->paramlen;
4249 i->unique = m->unique;
4250 m->prev = i;
Keith Kanios891775e2009-07-11 06:08:54 -05004251}
4252
4253
4254/*
4255 * Restore MMacro invocation specific fields that were
4256 * saved during a previous recursive macro expansion
4257 */
4258static void pop_mmacro(MMacro *m)
4259{
4260 MMacroInvocation *i;
4261
H. Peter Anvin89cee572009-07-15 09:16:54 -04004262 if (m->prev) {
4263 i = m->prev;
4264 m->prev = i->prev;
4265 m->params = i->params;
4266 m->iline = i->iline;
4267 m->nparam = i->nparam;
4268 m->rotate = i->rotate;
4269 m->paramlen = i->paramlen;
4270 m->unique = i->unique;
4271 nasm_free(i);
4272 }
Keith Kanios891775e2009-07-11 06:08:54 -05004273}
4274
4275
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004276/*
4277 * Expand the multi-line macro call made by the given line, if
4278 * there is one to be expanded. If there is, push the expansion on
H. Peter Anvineba20a72002-04-30 20:53:55 +00004279 * istk->expansion and return 1. Otherwise return 0.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004280 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004281static int expand_mmacro(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004282{
4283 Token *startline = tline;
4284 Token *label = NULL;
4285 int dont_prepend = 0;
H. Peter Anvince2233b2008-05-25 21:57:00 -07004286 Token **params, *t, *mtok, *tt;
H. Peter Anvineba20a72002-04-30 20:53:55 +00004287 MMacro *m;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004288 Line *l, *ll;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004289 int i, nparam, *paramlen;
H. Peter Anvinc751e862008-06-09 10:18:45 -07004290 const char *mname;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004291
4292 t = tline;
H. Peter Anvineba20a72002-04-30 20:53:55 +00004293 skip_white_(t);
H. Peter Anvince2233b2008-05-25 21:57:00 -07004294 /* if (!tok_type_(t, TOK_ID)) Lino 02/25/02 */
H. Peter Anvindce1e2f2002-04-30 21:06:37 +00004295 if (!tok_type_(t, TOK_ID) && !tok_type_(t, TOK_PREPROC_ID))
H. Peter Anvine2c80182005-01-15 22:15:51 +00004296 return 0;
H. Peter Anvince2233b2008-05-25 21:57:00 -07004297 mtok = t;
H. Peter Anvin734b1882002-04-30 21:01:08 +00004298 m = is_mmacro(t, &params);
H. Peter Anvinc751e862008-06-09 10:18:45 -07004299 if (m) {
4300 mname = t->text;
4301 } else {
H. Peter Anvine2c80182005-01-15 22:15:51 +00004302 Token *last;
4303 /*
4304 * We have an id which isn't a macro call. We'll assume
4305 * it might be a label; we'll also check to see if a
4306 * colon follows it. Then, if there's another id after
4307 * that lot, we'll check it again for macro-hood.
4308 */
4309 label = last = t;
4310 t = t->next;
4311 if (tok_type_(t, TOK_WHITESPACE))
4312 last = t, t = t->next;
4313 if (tok_is_(t, ":")) {
4314 dont_prepend = 1;
4315 last = t, t = t->next;
4316 if (tok_type_(t, TOK_WHITESPACE))
4317 last = t, t = t->next;
4318 }
H. Peter Anvin89cee572009-07-15 09:16:54 -04004319 if (!tok_type_(t, TOK_ID) || !(m = is_mmacro(t, &params)))
H. Peter Anvine2c80182005-01-15 22:15:51 +00004320 return 0;
4321 last->next = NULL;
Keith Kanios891775e2009-07-11 06:08:54 -05004322 mname = t->text;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004323 tline = t;
H. Peter Anvineba20a72002-04-30 20:53:55 +00004324 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004325
4326 /*
4327 * Fix up the parameters: this involves stripping leading and
4328 * trailing whitespace, then stripping braces if they are
4329 * present.
4330 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004331 for (nparam = 0; params[nparam]; nparam++) ;
H. Peter Anvin734b1882002-04-30 21:01:08 +00004332 paramlen = nparam ? nasm_malloc(nparam * sizeof(*paramlen)) : NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004333
H. Peter Anvine2c80182005-01-15 22:15:51 +00004334 for (i = 0; params[i]; i++) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07004335 int brace = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004336 int comma = (!m->plus || i < nparam - 1);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004337
H. Peter Anvine2c80182005-01-15 22:15:51 +00004338 t = params[i];
4339 skip_white_(t);
4340 if (tok_is_(t, "{"))
H. Peter Anvin6867acc2007-10-10 14:58:45 -07004341 t = t->next, brace = true, comma = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004342 params[i] = t;
4343 paramlen[i] = 0;
4344 while (t) {
4345 if (comma && t->type == TOK_OTHER && !strcmp(t->text, ","))
4346 break; /* ... because we have hit a comma */
4347 if (comma && t->type == TOK_WHITESPACE
4348 && tok_is_(t->next, ","))
4349 break; /* ... or a space then a comma */
4350 if (brace && t->type == TOK_OTHER && !strcmp(t->text, "}"))
4351 break; /* ... or a brace */
4352 t = t->next;
4353 paramlen[i]++;
4354 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004355 }
4356
4357 /*
4358 * OK, we have a MMacro structure together with a set of
4359 * parameters. We must now go through the expansion and push
H. Peter Anvin76690a12002-04-30 20:52:49 +00004360 * copies of each Line on to istk->expansion. Substitution of
4361 * parameter tokens and macro-local tokens doesn't get done
4362 * until the single-line macro substitution process; this is
4363 * because delaying them allows us to change the semantics
4364 * later through %rotate.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004365 *
H. Peter Anvin76690a12002-04-30 20:52:49 +00004366 * First, push an end marker on to istk->expansion, mark this
4367 * macro as in progress, and set up its invocation-specific
4368 * variables.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004369 */
4370 ll = nasm_malloc(sizeof(Line));
4371 ll->next = istk->expansion;
4372 ll->finishes = m;
4373 ll->first = NULL;
4374 istk->expansion = ll;
Keith Kanios891775e2009-07-11 06:08:54 -05004375
H. Peter Anvin89cee572009-07-15 09:16:54 -04004376 /*
4377 * Save the previous MMacro expansion in the case of
4378 * macro recursion
4379 */
4380 if (m->max_depth && m->in_progress)
4381 push_mmacro(m);
H. Peter Anvin76690a12002-04-30 20:52:49 +00004382
Keith Kanios891775e2009-07-11 06:08:54 -05004383 m->in_progress ++;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004384 m->params = params;
4385 m->iline = tline;
4386 m->nparam = nparam;
4387 m->rotate = 0;
4388 m->paramlen = paramlen;
4389 m->unique = unique++;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004390 m->lineno = 0;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004391
4392 m->next_active = istk->mstk;
4393 istk->mstk = m;
4394
H. Peter Anvine2c80182005-01-15 22:15:51 +00004395 for (l = m->expansion; l; l = l->next) {
4396 Token **tail;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004397
H. Peter Anvine2c80182005-01-15 22:15:51 +00004398 ll = nasm_malloc(sizeof(Line));
4399 ll->finishes = NULL;
4400 ll->next = istk->expansion;
4401 istk->expansion = ll;
4402 tail = &ll->first;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004403
H. Peter Anvine2c80182005-01-15 22:15:51 +00004404 for (t = l->first; t; t = t->next) {
4405 Token *x = t;
H. Peter Anvince2233b2008-05-25 21:57:00 -07004406 switch (t->type) {
4407 case TOK_PREPROC_Q:
H. Peter Anvinc751e862008-06-09 10:18:45 -07004408 tt = *tail = new_Token(NULL, TOK_ID, mname, 0);
H. Peter Anvince2233b2008-05-25 21:57:00 -07004409 break;
4410 case TOK_PREPROC_QQ:
4411 tt = *tail = new_Token(NULL, TOK_ID, m->name, 0);
4412 break;
4413 case TOK_PREPROC_ID:
4414 if (t->text[1] == '0' && t->text[2] == '0') {
4415 dont_prepend = -1;
4416 x = label;
4417 if (!x)
4418 continue;
4419 }
4420 /* fall through */
4421 default:
4422 tt = *tail = new_Token(NULL, x->type, x->text, 0);
4423 break;
H. Peter Anvin166c2472008-05-28 12:28:58 -07004424 }
H. Peter Anvince2233b2008-05-25 21:57:00 -07004425 tail = &tt->next;
4426 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00004427 *tail = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004428 }
4429
4430 /*
H. Peter Anvineba20a72002-04-30 20:53:55 +00004431 * If we had a label, push it on as the first line of
4432 * the macro expansion.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004433 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004434 if (label) {
4435 if (dont_prepend < 0)
4436 free_tlist(startline);
4437 else {
4438 ll = nasm_malloc(sizeof(Line));
4439 ll->finishes = NULL;
4440 ll->next = istk->expansion;
4441 istk->expansion = ll;
4442 ll->first = startline;
4443 if (!dont_prepend) {
4444 while (label->next)
4445 label = label->next;
4446 label->next = tt = new_Token(NULL, TOK_OTHER, ":", 0);
4447 }
4448 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004449 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004450
H. Peter Anvin734b1882002-04-30 21:01:08 +00004451 list->uplevel(m->nolist ? LIST_MACRO_NOLIST : LIST_MACRO);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004452
H. Peter Anvineba20a72002-04-30 20:53:55 +00004453 return 1;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004454}
4455
Victor van den Elzen3b404c02008-09-18 13:51:36 +02004456/* The function that actually does the error reporting */
4457static void verror(int severity, const char *fmt, va_list arg)
4458{
4459 char buff[1024];
4460
4461 vsnprintf(buff, sizeof(buff), fmt, arg);
4462
4463 if (istk && istk->mstk && istk->mstk->name)
H. Peter Anvindbb640b2009-07-18 18:57:16 -07004464 nasm_error(severity, "(%s:%d) %s", istk->mstk->name,
Victor van den Elzen3b404c02008-09-18 13:51:36 +02004465 istk->mstk->lineno, buff);
4466 else
H. Peter Anvindbb640b2009-07-18 18:57:16 -07004467 nasm_error(severity, "%s", buff);
Victor van den Elzen3b404c02008-09-18 13:51:36 +02004468}
4469
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004470/*
4471 * Since preprocessor always operate only on the line that didn't
H. Peter Anvin917a3492008-09-24 09:14:49 -07004472 * arrived yet, we should always use ERR_OFFBY1.
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004473 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004474static void error(int severity, const char *fmt, ...)
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004475{
4476 va_list arg;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004477
4478 /* If we're in a dead branch of IF or something like it, ignore the error */
H. Peter Anvin77ba0c62002-05-14 03:18:53 +00004479 if (istk && istk->conds && !emitting(istk->conds->state))
H. Peter Anvine2c80182005-01-15 22:15:51 +00004480 return;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004481
H. Peter Anvin734b1882002-04-30 21:01:08 +00004482 va_start(arg, fmt);
Victor van den Elzen3b404c02008-09-18 13:51:36 +02004483 verror(severity, fmt, arg);
H. Peter Anvin734b1882002-04-30 21:01:08 +00004484 va_end(arg);
Victor van den Elzen3b404c02008-09-18 13:51:36 +02004485}
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004486
Victor van den Elzen3b404c02008-09-18 13:51:36 +02004487/*
4488 * Because %else etc are evaluated in the state context
4489 * of the previous branch, errors might get lost with error():
4490 * %if 0 ... %else trailing garbage ... %endif
4491 * So %else etc should report errors with this function.
4492 */
4493static void error_precond(int severity, const char *fmt, ...)
4494{
4495 va_list arg;
4496
4497 /* Only ignore the error if it's really in a dead branch */
4498 if (istk && istk->conds && istk->conds->state == COND_NEVER)
4499 return;
4500
4501 va_start(arg, fmt);
4502 verror(severity, fmt, arg);
4503 va_end(arg);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004504}
4505
H. Peter Anvin734b1882002-04-30 21:01:08 +00004506static void
H. Peter Anvindbb640b2009-07-18 18:57:16 -07004507pp_reset(char *file, int apass, ListGen * listgen, StrList **deplist)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004508{
H. Peter Anvin7383b402008-09-24 10:20:40 -07004509 Token *t;
4510
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004511 cstk = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004512 istk = nasm_malloc(sizeof(Include));
4513 istk->next = NULL;
4514 istk->conds = NULL;
4515 istk->expansion = NULL;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004516 istk->mstk = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004517 istk->fp = fopen(file, "r");
H. Peter Anvineba20a72002-04-30 20:53:55 +00004518 istk->fname = NULL;
4519 src_set_fname(nasm_strdup(file));
4520 src_set_linnum(0);
4521 istk->lineinc = 1;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004522 if (!istk->fp)
H. Peter Anvin917a3492008-09-24 09:14:49 -07004523 error(ERR_FATAL|ERR_NOFILE, "unable to open input file `%s'",
H. Peter Anvine2c80182005-01-15 22:15:51 +00004524 file);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004525 defining = NULL;
Charles Crayned4200be2008-07-12 16:42:33 -07004526 nested_mac_count = 0;
4527 nested_rep_count = 0;
H. Peter Anvin97a23472007-09-16 17:57:25 -07004528 init_macros();
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004529 unique = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004530 if (tasm_compatible_mode) {
H. Peter Anvina4835d42008-05-20 14:21:29 -07004531 stdmacpos = nasm_stdmac;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004532 } else {
H. Peter Anvina4835d42008-05-20 14:21:29 -07004533 stdmacpos = nasm_stdmac_after_tasm;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004534 }
H. Peter Anvind2456592008-06-19 15:04:18 -07004535 any_extrastdmac = extrastdmac && *extrastdmac;
4536 do_predef = true;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004537 list = listgen;
H. Peter Anvin61f130f2008-09-25 15:45:06 -07004538
4539 /*
4540 * 0 for dependencies, 1 for preparatory passes, 2 for final pass.
4541 * The caller, however, will also pass in 3 for preprocess-only so
4542 * we can set __PASS__ accordingly.
4543 */
4544 pass = apass > 2 ? 2 : apass;
4545
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07004546 dephead = deptail = deplist;
4547 if (deplist) {
4548 StrList *sl = nasm_malloc(strlen(file)+1+sizeof sl->next);
4549 sl->next = NULL;
4550 strcpy(sl->str, file);
4551 *deptail = sl;
4552 deptail = &sl->next;
4553 }
H. Peter Anvin7383b402008-09-24 10:20:40 -07004554
H. Peter Anvin61f130f2008-09-25 15:45:06 -07004555 /*
4556 * Define the __PASS__ macro. This is defined here unlike
4557 * all the other builtins, because it is special -- it varies between
4558 * passes.
4559 */
H. Peter Anvin7383b402008-09-24 10:20:40 -07004560 t = nasm_malloc(sizeof(*t));
4561 t->next = NULL;
H. Peter Anvin61f130f2008-09-25 15:45:06 -07004562 make_tok_num(t, apass);
H. Peter Anvin7383b402008-09-24 10:20:40 -07004563 t->a.mac = NULL;
4564 define_smacro(NULL, "__PASS__", true, 0, t);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004565}
4566
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004567static char *pp_getline(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004568{
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004569 char *line;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004570 Token *tline;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004571
H. Peter Anvine2c80182005-01-15 22:15:51 +00004572 while (1) {
4573 /*
Keith Kaniosb7a89542007-04-12 02:40:54 +00004574 * Fetch a tokenized line, either from the macro-expansion
H. Peter Anvine2c80182005-01-15 22:15:51 +00004575 * buffer or from the input file.
4576 */
4577 tline = NULL;
4578 while (istk->expansion && istk->expansion->finishes) {
4579 Line *l = istk->expansion;
4580 if (!l->finishes->name && l->finishes->in_progress > 1) {
4581 Line *ll;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004582
H. Peter Anvine2c80182005-01-15 22:15:51 +00004583 /*
4584 * This is a macro-end marker for a macro with no
4585 * name, which means it's not really a macro at all
4586 * but a %rep block, and the `in_progress' field is
4587 * more than 1, meaning that we still need to
4588 * repeat. (1 means the natural last repetition; 0
4589 * means termination by %exitrep.) We have
4590 * therefore expanded up to the %endrep, and must
4591 * push the whole block on to the expansion buffer
4592 * again. We don't bother to remove the macro-end
4593 * marker: we'd only have to generate another one
4594 * if we did.
4595 */
4596 l->finishes->in_progress--;
4597 for (l = l->finishes->expansion; l; l = l->next) {
4598 Token *t, *tt, **tail;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004599
H. Peter Anvine2c80182005-01-15 22:15:51 +00004600 ll = nasm_malloc(sizeof(Line));
4601 ll->next = istk->expansion;
4602 ll->finishes = NULL;
4603 ll->first = NULL;
4604 tail = &ll->first;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004605
H. Peter Anvine2c80182005-01-15 22:15:51 +00004606 for (t = l->first; t; t = t->next) {
4607 if (t->text || t->type == TOK_WHITESPACE) {
4608 tt = *tail =
4609 new_Token(NULL, t->type, t->text, 0);
4610 tail = &tt->next;
4611 }
4612 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00004613
H. Peter Anvine2c80182005-01-15 22:15:51 +00004614 istk->expansion = ll;
4615 }
4616 } else {
4617 /*
4618 * Check whether a `%rep' was started and not ended
4619 * within this macro expansion. This can happen and
4620 * should be detected. It's a fatal error because
4621 * I'm too confused to work out how to recover
4622 * sensibly from it.
4623 */
4624 if (defining) {
4625 if (defining->name)
4626 error(ERR_PANIC,
4627 "defining with name in expansion");
4628 else if (istk->mstk->name)
4629 error(ERR_FATAL,
4630 "`%%rep' without `%%endrep' within"
4631 " expansion of macro `%s'",
4632 istk->mstk->name);
4633 }
H. Peter Anvin87bc6192002-04-30 20:53:16 +00004634
H. Peter Anvine2c80182005-01-15 22:15:51 +00004635 /*
4636 * FIXME: investigate the relationship at this point between
4637 * istk->mstk and l->finishes
4638 */
4639 {
4640 MMacro *m = istk->mstk;
4641 istk->mstk = m->next_active;
4642 if (m->name) {
4643 /*
4644 * This was a real macro call, not a %rep, and
4645 * therefore the parameter information needs to
4646 * be freed.
4647 */
H. Peter Anvin89cee572009-07-15 09:16:54 -04004648 if (m->prev) {
4649 pop_mmacro(m);
4650 l->finishes->in_progress --;
4651 } else {
Keith Kanios891775e2009-07-11 06:08:54 -05004652 nasm_free(m->params);
4653 free_tlist(m->iline);
H. Peter Anvin89cee572009-07-15 09:16:54 -04004654 nasm_free(m->paramlen);
4655 l->finishes->in_progress = 0;
4656 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00004657 } else
4658 free_mmacro(m);
4659 }
4660 istk->expansion = l->next;
4661 nasm_free(l);
4662 list->downlevel(LIST_MACRO);
4663 }
4664 }
4665 while (1) { /* until we get a line we can use */
H. Peter Anvineba20a72002-04-30 20:53:55 +00004666
H. Peter Anvine2c80182005-01-15 22:15:51 +00004667 if (istk->expansion) { /* from a macro expansion */
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004668 char *p;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004669 Line *l = istk->expansion;
4670 if (istk->mstk)
4671 istk->mstk->lineno++;
4672 tline = l->first;
4673 istk->expansion = l->next;
4674 nasm_free(l);
H. Peter Anvin6867acc2007-10-10 14:58:45 -07004675 p = detoken(tline, false);
H. Peter Anvine2c80182005-01-15 22:15:51 +00004676 list->line(LIST_MACRO, p);
4677 nasm_free(p);
4678 break;
4679 }
4680 line = read_line();
4681 if (line) { /* from the current input file */
4682 line = prepreproc(line);
Keith Kaniosb7a89542007-04-12 02:40:54 +00004683 tline = tokenize(line);
H. Peter Anvine2c80182005-01-15 22:15:51 +00004684 nasm_free(line);
4685 break;
4686 }
4687 /*
4688 * The current file has ended; work down the istk
4689 */
4690 {
4691 Include *i = istk;
4692 fclose(i->fp);
4693 if (i->conds)
4694 error(ERR_FATAL,
4695 "expected `%%endif' before end of file");
4696 /* only set line and file name if there's a next node */
4697 if (i->next) {
4698 src_set_linnum(i->lineno);
4699 nasm_free(src_set_fname(i->fname));
H. Peter Anvin86877b22008-06-20 15:55:45 -07004700 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00004701 istk = i->next;
4702 list->downlevel(LIST_INCLUDE);
4703 nasm_free(i);
4704 if (!istk)
4705 return NULL;
Victor van den Elzen4c9d6222008-10-01 13:08:50 +02004706 if (istk->expansion && istk->expansion->finishes)
4707 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004708 }
4709 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004710
H. Peter Anvine2c80182005-01-15 22:15:51 +00004711 /*
4712 * We must expand MMacro parameters and MMacro-local labels
4713 * _before_ we plunge into directive processing, to cope
4714 * with things like `%define something %1' such as STRUC
4715 * uses. Unless we're _defining_ a MMacro, in which case
4716 * those tokens should be left alone to go into the
4717 * definition; and unless we're in a non-emitting
4718 * condition, in which case we don't want to meddle with
4719 * anything.
4720 */
Charles Crayned4200be2008-07-12 16:42:33 -07004721 if (!defining && !(istk->conds && !emitting(istk->conds->state))
H. Peter Anvin992fe752008-10-19 15:45:05 -07004722 && !(istk->mstk && !istk->mstk->in_progress)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00004723 tline = expand_mmac_params(tline);
H. Peter Anvin992fe752008-10-19 15:45:05 -07004724 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00004725
H. Peter Anvine2c80182005-01-15 22:15:51 +00004726 /*
4727 * Check the line to see if it's a preprocessor directive.
4728 */
4729 if (do_directive(tline) == DIRECTIVE_FOUND) {
4730 continue;
4731 } else if (defining) {
4732 /*
4733 * We're defining a multi-line macro. We emit nothing
4734 * at all, and just
Keith Kaniosb7a89542007-04-12 02:40:54 +00004735 * shove the tokenized line on to the macro definition.
H. Peter Anvine2c80182005-01-15 22:15:51 +00004736 */
4737 Line *l = nasm_malloc(sizeof(Line));
4738 l->next = defining->expansion;
4739 l->first = tline;
H. Peter Anvin538002d2008-06-28 18:30:27 -07004740 l->finishes = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004741 defining->expansion = l;
4742 continue;
4743 } else if (istk->conds && !emitting(istk->conds->state)) {
4744 /*
4745 * We're in a non-emitting branch of a condition block.
4746 * Emit nothing at all, not even a blank line: when we
4747 * emerge from the condition we'll give a line-number
4748 * directive so we keep our place correctly.
4749 */
4750 free_tlist(tline);
4751 continue;
4752 } else if (istk->mstk && !istk->mstk->in_progress) {
4753 /*
4754 * We're in a %rep block which has been terminated, so
4755 * we're walking through to the %endrep without
4756 * emitting anything. Emit nothing at all, not even a
4757 * blank line: when we emerge from the %rep block we'll
4758 * give a line-number directive so we keep our place
4759 * correctly.
4760 */
4761 free_tlist(tline);
4762 continue;
4763 } else {
4764 tline = expand_smacro(tline);
4765 if (!expand_mmacro(tline)) {
4766 /*
Keith Kaniosb7a89542007-04-12 02:40:54 +00004767 * De-tokenize the line again, and emit it.
H. Peter Anvine2c80182005-01-15 22:15:51 +00004768 */
H. Peter Anvin6867acc2007-10-10 14:58:45 -07004769 line = detoken(tline, true);
H. Peter Anvine2c80182005-01-15 22:15:51 +00004770 free_tlist(tline);
4771 break;
4772 } else {
4773 continue; /* expand_mmacro calls free_tlist */
4774 }
4775 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004776 }
4777
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004778 return line;
4779}
4780
H. Peter Anvine2c80182005-01-15 22:15:51 +00004781static void pp_cleanup(int pass)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004782{
H. Peter Anvine2c80182005-01-15 22:15:51 +00004783 if (defining) {
H. Peter Anvin89cee572009-07-15 09:16:54 -04004784 if (defining->name) {
Victor van den Elzen8f1120f2008-07-16 13:41:37 +02004785 error(ERR_NONFATAL,
4786 "end of file while still defining macro `%s'",
4787 defining->name);
4788 } else {
4789 error(ERR_NONFATAL, "end of file while still in %%rep");
4790 }
4791
H. Peter Anvine2c80182005-01-15 22:15:51 +00004792 free_mmacro(defining);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004793 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004794 while (cstk)
H. Peter Anvine2c80182005-01-15 22:15:51 +00004795 ctx_pop();
H. Peter Anvin97a23472007-09-16 17:57:25 -07004796 free_macros();
H. Peter Anvine2c80182005-01-15 22:15:51 +00004797 while (istk) {
4798 Include *i = istk;
4799 istk = istk->next;
4800 fclose(i->fp);
4801 nasm_free(i->fname);
4802 nasm_free(i);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004803 }
4804 while (cstk)
H. Peter Anvine2c80182005-01-15 22:15:51 +00004805 ctx_pop();
H. Peter Anvin86877b22008-06-20 15:55:45 -07004806 nasm_free(src_set_fname(NULL));
H. Peter Anvine2c80182005-01-15 22:15:51 +00004807 if (pass == 0) {
H. Peter Anvin86877b22008-06-20 15:55:45 -07004808 IncPath *i;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004809 free_llist(predef);
4810 delete_Blocks();
H. Peter Anvin86877b22008-06-20 15:55:45 -07004811 while ((i = ipath)) {
4812 ipath = i->next;
4813 if (i->path)
4814 nasm_free(i->path);
4815 nasm_free(i);
4816 }
H. Peter Anvin11dfa1a2008-07-02 18:11:04 -07004817 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004818}
4819
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004820void pp_include_path(char *path)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004821{
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004822 IncPath *i;
H. Peter Anvin37a321f2007-09-24 13:41:58 -07004823
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004824 i = nasm_malloc(sizeof(IncPath));
H. Peter Anvin37a321f2007-09-24 13:41:58 -07004825 i->path = path ? nasm_strdup(path) : NULL;
Frank Kotlerd0ed6fd2003-08-27 11:33:56 +00004826 i->next = NULL;
4827
H. Peter Anvin89cee572009-07-15 09:16:54 -04004828 if (ipath) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00004829 IncPath *j = ipath;
H. Peter Anvin89cee572009-07-15 09:16:54 -04004830 while (j->next)
H. Peter Anvine2c80182005-01-15 22:15:51 +00004831 j = j->next;
4832 j->next = i;
4833 } else {
4834 ipath = i;
Frank Kotlerd0ed6fd2003-08-27 11:33:56 +00004835 }
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004836}
Frank Kotlerd0ed6fd2003-08-27 11:33:56 +00004837
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004838void pp_pre_include(char *fname)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004839{
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004840 Token *inc, *space, *name;
4841 Line *l;
4842
H. Peter Anvin734b1882002-04-30 21:01:08 +00004843 name = new_Token(NULL, TOK_INTERNAL_STRING, fname, 0);
4844 space = new_Token(name, TOK_WHITESPACE, NULL, 0);
4845 inc = new_Token(space, TOK_PREPROC_ID, "%include", 0);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004846
4847 l = nasm_malloc(sizeof(Line));
4848 l->next = predef;
4849 l->first = inc;
H. Peter Anvin538002d2008-06-28 18:30:27 -07004850 l->finishes = NULL;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004851 predef = l;
4852}
4853
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004854void pp_pre_define(char *definition)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004855{
4856 Token *def, *space;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004857 Line *l;
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004858 char *equals;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004859
4860 equals = strchr(definition, '=');
H. Peter Anvin734b1882002-04-30 21:01:08 +00004861 space = new_Token(NULL, TOK_WHITESPACE, NULL, 0);
4862 def = new_Token(space, TOK_PREPROC_ID, "%define", 0);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004863 if (equals)
H. Peter Anvine2c80182005-01-15 22:15:51 +00004864 *equals = ' ';
Keith Kaniosb7a89542007-04-12 02:40:54 +00004865 space->next = tokenize(definition);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004866 if (equals)
H. Peter Anvine2c80182005-01-15 22:15:51 +00004867 *equals = '=';
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004868
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004869 l = nasm_malloc(sizeof(Line));
4870 l->next = predef;
4871 l->first = def;
H. Peter Anvin538002d2008-06-28 18:30:27 -07004872 l->finishes = NULL;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004873 predef = l;
4874}
4875
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004876void pp_pre_undefine(char *definition)
H. Peter Anvin620515a2002-04-30 20:57:38 +00004877{
4878 Token *def, *space;
4879 Line *l;
4880
H. Peter Anvin734b1882002-04-30 21:01:08 +00004881 space = new_Token(NULL, TOK_WHITESPACE, NULL, 0);
4882 def = new_Token(space, TOK_PREPROC_ID, "%undef", 0);
Keith Kaniosb7a89542007-04-12 02:40:54 +00004883 space->next = tokenize(definition);
H. Peter Anvin620515a2002-04-30 20:57:38 +00004884
4885 l = nasm_malloc(sizeof(Line));
4886 l->next = predef;
4887 l->first = def;
H. Peter Anvin538002d2008-06-28 18:30:27 -07004888 l->finishes = NULL;
H. Peter Anvin620515a2002-04-30 20:57:38 +00004889 predef = l;
4890}
4891
Keith Kaniosb7a89542007-04-12 02:40:54 +00004892/*
4893 * Added by Keith Kanios:
4894 *
4895 * This function is used to assist with "runtime" preprocessor
4896 * directives. (e.g. pp_runtime("%define __BITS__ 64");)
4897 *
4898 * ERRORS ARE IGNORED HERE, SO MAKE COMPLETELY SURE THAT YOU
4899 * PASS A VALID STRING TO THIS FUNCTION!!!!!
4900 */
4901
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004902void pp_runtime(char *definition)
Keith Kaniosb7a89542007-04-12 02:40:54 +00004903{
4904 Token *def;
H. Peter Anvin70653092007-10-19 14:42:29 -07004905
Keith Kaniosb7a89542007-04-12 02:40:54 +00004906 def = tokenize(definition);
H. Peter Anvin89cee572009-07-15 09:16:54 -04004907 if (do_directive(def) == NO_DIRECTIVE_FOUND)
Keith Kaniosb7a89542007-04-12 02:40:54 +00004908 free_tlist(def);
H. Peter Anvin70653092007-10-19 14:42:29 -07004909
Keith Kaniosb7a89542007-04-12 02:40:54 +00004910}
4911
H. Peter Anvina70547f2008-07-19 21:44:26 -07004912void pp_extra_stdmac(macros_t *macros)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004913{
H. Peter Anvin76690a12002-04-30 20:52:49 +00004914 extrastdmac = macros;
4915}
4916
Keith Kaniosa5fc6462007-10-13 07:09:22 -07004917static void make_tok_num(Token * tok, int64_t val)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004918{
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004919 char numbuf[20];
Keith Kaniosa5fc6462007-10-13 07:09:22 -07004920 snprintf(numbuf, sizeof(numbuf), "%"PRId64"", val);
H. Peter Anvineba20a72002-04-30 20:53:55 +00004921 tok->text = nasm_strdup(numbuf);
4922 tok->type = TOK_NUMBER;
4923}
4924
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004925Preproc nasmpp = {
4926 pp_reset,
4927 pp_getline,
4928 pp_cleanup
4929};