blob: 736bbff9bf88b9b164b64ee346e882585a8affcd [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);
3214 t = tline;
3215 for (t = tline; t; t = t->next) {
3216 if (t->type == TOK_STRING) {
3217 memcpy(p, t->text, t->a.len);
3218 p += t->a.len;
3219 }
3220 }
3221
3222 /*
3223 * We now have a macro name, an implicit parameter count of
3224 * zero, and a numeric token to use as an expansion. Create
3225 * and store an SMacro.
3226 */
3227 macro_start = new_Token(NULL, TOK_STRING, NULL, 0);
3228 macro_start->text = nasm_quote(pp, len);
3229 nasm_free(pp);
3230 define_smacro(ctx, mname, casesense, 0, macro_start);
3231 free_tlist(tline);
3232 free_tlist(origline);
3233 return DIRECTIVE_FOUND;
3234
H. Peter Anvine2c80182005-01-15 22:15:51 +00003235 case PP_SUBSTR:
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003236 {
3237 int64_t a1, a2;
3238 size_t len;
H. Peter Anvind2456592008-06-19 15:04:18 -07003239
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07003240 casesense = true;
3241
H. Peter Anvine2c80182005-01-15 22:15:51 +00003242 tline = tline->next;
3243 skip_white_(tline);
3244 tline = expand_id(tline);
3245 if (!tline || (tline->type != TOK_ID &&
3246 (tline->type != TOK_PREPROC_ID ||
3247 tline->text[1] != '$'))) {
3248 error(ERR_NONFATAL,
3249 "`%%substr' expects a macro identifier as first parameter");
3250 free_tlist(origline);
3251 return DIRECTIVE_FOUND;
3252 }
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08003253 ctx = get_ctx(tline->text, &mname, false);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003254 last = tline;
3255 tline = expand_smacro(tline->next);
3256 last->next = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003257
H. Peter Anvine2c80182005-01-15 22:15:51 +00003258 t = tline->next;
3259 while (tok_type_(t, TOK_WHITESPACE))
3260 t = t->next;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003261
H. Peter Anvine2c80182005-01-15 22:15:51 +00003262 /* t should now point to the string */
3263 if (t->type != TOK_STRING) {
3264 error(ERR_NONFATAL,
3265 "`%%substr` requires string as second parameter");
3266 free_tlist(tline);
3267 free_tlist(origline);
3268 return DIRECTIVE_FOUND;
3269 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003270
H. Peter Anvine2c80182005-01-15 22:15:51 +00003271 tt = t->next;
3272 tptr = &tt;
3273 tokval.t_type = TOKEN_INVALID;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003274 evalresult = evaluate(ppscan, tptr, &tokval, NULL,
3275 pass, error, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003276 if (!evalresult) {
3277 free_tlist(tline);
3278 free_tlist(origline);
3279 return DIRECTIVE_FOUND;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003280 } else if (!is_simple(evalresult)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00003281 error(ERR_NONFATAL, "non-constant value given to `%%substr`");
3282 free_tlist(tline);
3283 free_tlist(origline);
3284 return DIRECTIVE_FOUND;
3285 }
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003286 a1 = evalresult->value-1;
3287
3288 while (tok_type_(tt, TOK_WHITESPACE))
3289 tt = tt->next;
3290 if (!tt) {
3291 a2 = 1; /* Backwards compatibility: one character */
3292 } else {
3293 tokval.t_type = TOKEN_INVALID;
3294 evalresult = evaluate(ppscan, tptr, &tokval, NULL,
3295 pass, error, NULL);
3296 if (!evalresult) {
3297 free_tlist(tline);
3298 free_tlist(origline);
3299 return DIRECTIVE_FOUND;
3300 } else if (!is_simple(evalresult)) {
3301 error(ERR_NONFATAL, "non-constant value given to `%%substr`");
3302 free_tlist(tline);
3303 free_tlist(origline);
3304 return DIRECTIVE_FOUND;
3305 }
3306 a2 = evalresult->value;
3307 }
3308
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07003309 len = nasm_unquote(t->text, NULL);
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003310 if (a2 < 0)
3311 a2 = a2+1+len-a1;
3312 if (a1+a2 > (int64_t)len)
3313 a2 = len-a1;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003314
H. Peter Anvine2c80182005-01-15 22:15:51 +00003315 macro_start = nasm_malloc(sizeof(*macro_start));
3316 macro_start->next = NULL;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003317 macro_start->text = nasm_quote((a1 < 0) ? "" : t->text+a1, a2);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003318 macro_start->type = TOK_STRING;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003319 macro_start->a.mac = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003320
H. Peter Anvine2c80182005-01-15 22:15:51 +00003321 /*
3322 * We now have a macro name, an implicit parameter count of
3323 * zero, and a numeric token to use as an expansion. Create
3324 * and store an SMacro.
3325 */
H. Peter Anvin4db5a162007-10-11 13:42:09 -07003326 define_smacro(ctx, mname, casesense, 0, macro_start);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003327 free_tlist(tline);
3328 free_tlist(origline);
3329 return DIRECTIVE_FOUND;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003330 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003331
H. Peter Anvine2c80182005-01-15 22:15:51 +00003332 case PP_ASSIGN:
3333 case PP_IASSIGN:
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07003334 casesense = (i == PP_ASSIGN);
3335
H. Peter Anvine2c80182005-01-15 22:15:51 +00003336 tline = tline->next;
3337 skip_white_(tline);
3338 tline = expand_id(tline);
3339 if (!tline || (tline->type != TOK_ID &&
3340 (tline->type != TOK_PREPROC_ID ||
3341 tline->text[1] != '$'))) {
3342 error(ERR_NONFATAL,
3343 "`%%%sassign' expects a macro identifier",
3344 (i == PP_IASSIGN ? "i" : ""));
3345 free_tlist(origline);
3346 return DIRECTIVE_FOUND;
3347 }
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08003348 ctx = get_ctx(tline->text, &mname, false);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003349 last = tline;
3350 tline = expand_smacro(tline->next);
3351 last->next = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003352
H. Peter Anvine2c80182005-01-15 22:15:51 +00003353 t = tline;
3354 tptr = &t;
3355 tokval.t_type = TOKEN_INVALID;
3356 evalresult =
3357 evaluate(ppscan, tptr, &tokval, NULL, pass, error, NULL);
3358 free_tlist(tline);
3359 if (!evalresult) {
3360 free_tlist(origline);
3361 return DIRECTIVE_FOUND;
3362 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003363
H. Peter Anvine2c80182005-01-15 22:15:51 +00003364 if (tokval.t_type)
H. Peter Anvin917a3492008-09-24 09:14:49 -07003365 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +00003366 "trailing garbage after expression ignored");
H. Peter Anvin734b1882002-04-30 21:01:08 +00003367
H. Peter Anvine2c80182005-01-15 22:15:51 +00003368 if (!is_simple(evalresult)) {
3369 error(ERR_NONFATAL,
3370 "non-constant value given to `%%%sassign'",
3371 (i == PP_IASSIGN ? "i" : ""));
3372 free_tlist(origline);
3373 return DIRECTIVE_FOUND;
3374 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003375
H. Peter Anvine2c80182005-01-15 22:15:51 +00003376 macro_start = nasm_malloc(sizeof(*macro_start));
3377 macro_start->next = NULL;
3378 make_tok_num(macro_start, reloc_value(evalresult));
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003379 macro_start->a.mac = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003380
H. Peter Anvine2c80182005-01-15 22:15:51 +00003381 /*
3382 * We now have a macro name, an implicit parameter count of
3383 * zero, and a numeric token to use as an expansion. Create
3384 * and store an SMacro.
3385 */
H. Peter Anvin4db5a162007-10-11 13:42:09 -07003386 define_smacro(ctx, mname, casesense, 0, macro_start);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003387 free_tlist(origline);
3388 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003389
H. Peter Anvine2c80182005-01-15 22:15:51 +00003390 case PP_LINE:
3391 /*
3392 * Syntax is `%line nnn[+mmm] [filename]'
3393 */
3394 tline = tline->next;
3395 skip_white_(tline);
3396 if (!tok_type_(tline, TOK_NUMBER)) {
3397 error(ERR_NONFATAL, "`%%line' expects line number");
3398 free_tlist(origline);
3399 return DIRECTIVE_FOUND;
3400 }
H. Peter Anvin70055962007-10-11 00:05:31 -07003401 k = readnum(tline->text, &err);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003402 m = 1;
3403 tline = tline->next;
3404 if (tok_is_(tline, "+")) {
3405 tline = tline->next;
3406 if (!tok_type_(tline, TOK_NUMBER)) {
3407 error(ERR_NONFATAL, "`%%line' expects line increment");
3408 free_tlist(origline);
3409 return DIRECTIVE_FOUND;
3410 }
H. Peter Anvin70055962007-10-11 00:05:31 -07003411 m = readnum(tline->text, &err);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003412 tline = tline->next;
3413 }
3414 skip_white_(tline);
3415 src_set_linnum(k);
3416 istk->lineinc = m;
3417 if (tline) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07003418 nasm_free(src_set_fname(detoken(tline, false)));
H. Peter Anvine2c80182005-01-15 22:15:51 +00003419 }
3420 free_tlist(origline);
3421 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003422
H. Peter Anvine2c80182005-01-15 22:15:51 +00003423 default:
3424 error(ERR_FATAL,
3425 "preprocessor directive `%s' not yet implemented",
H. Peter Anvin4169a472007-09-12 01:29:43 +00003426 pp_directives[i]);
H. Peter Anvina26433d2008-07-16 14:40:01 -07003427 return DIRECTIVE_FOUND;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003428 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003429}
3430
3431/*
H. Peter Anvin76690a12002-04-30 20:52:49 +00003432 * Ensure that a macro parameter contains a condition code and
3433 * nothing else. Return the condition code index if so, or -1
3434 * otherwise.
3435 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003436static int find_cc(Token * t)
H. Peter Anvineba20a72002-04-30 20:53:55 +00003437{
H. Peter Anvin76690a12002-04-30 20:52:49 +00003438 Token *tt;
3439 int i, j, k, m;
3440
H. Peter Anvin25a99342007-09-22 17:45:45 -07003441 if (!t)
H. Peter Anvin89cee572009-07-15 09:16:54 -04003442 return -1; /* Probably a %+ without a space */
H. Peter Anvin25a99342007-09-22 17:45:45 -07003443
H. Peter Anvineba20a72002-04-30 20:53:55 +00003444 skip_white_(t);
H. Peter Anvin76690a12002-04-30 20:52:49 +00003445 if (t->type != TOK_ID)
H. Peter Anvine2c80182005-01-15 22:15:51 +00003446 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003447 tt = t->next;
H. Peter Anvineba20a72002-04-30 20:53:55 +00003448 skip_white_(tt);
H. Peter Anvin76690a12002-04-30 20:52:49 +00003449 if (tt && (tt->type != TOK_OTHER || strcmp(tt->text, ",")))
H. Peter Anvine2c80182005-01-15 22:15:51 +00003450 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003451
3452 i = -1;
Ed Beroset3ab3f412002-06-11 03:31:49 +00003453 j = elements(conditions);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003454 while (j - i > 1) {
3455 k = (j + i) / 2;
3456 m = nasm_stricmp(t->text, conditions[k]);
3457 if (m == 0) {
3458 i = k;
3459 j = -2;
3460 break;
3461 } else if (m < 0) {
3462 j = k;
3463 } else
3464 i = k;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003465 }
3466 if (j != -2)
H. Peter Anvine2c80182005-01-15 22:15:51 +00003467 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003468 return i;
3469}
3470
H. Peter Anvind784a082009-04-20 14:01:18 -07003471static bool paste_tokens(Token **head, bool handle_paste_tokens)
3472{
3473 Token **tail, *t, *tt;
3474 Token **paste_head;
3475 bool did_paste = false;
3476 char *tmp;
3477
3478 /* Now handle token pasting... */
3479 paste_head = NULL;
3480 tail = head;
3481 while ((t = *tail) && (tt = t->next)) {
3482 switch (t->type) {
3483 case TOK_WHITESPACE:
3484 if (tt->type == TOK_WHITESPACE) {
3485 /* Zap adjacent whitespace tokens */
3486 t->next = delete_Token(tt);
3487 } else {
3488 /* Do not advance paste_head here */
3489 tail = &t->next;
3490 }
3491 break;
3492 case TOK_ID:
3493 case TOK_PREPROC_ID:
3494 case TOK_NUMBER:
3495 case TOK_FLOAT:
3496 {
3497 size_t len = 0;
3498 char *tmp, *p;
3499
3500 while (tt && (tt->type == TOK_ID || tt->type == TOK_PREPROC_ID ||
3501 tt->type == TOK_NUMBER || tt->type == TOK_FLOAT ||
3502 tt->type == TOK_OTHER)) {
3503 len += strlen(tt->text);
3504 tt = tt->next;
3505 }
3506
3507 /* Now tt points to the first token after the potential
3508 paste area... */
3509 if (tt != t->next) {
3510 /* We have at least two tokens... */
3511 len += strlen(t->text);
3512 p = tmp = nasm_malloc(len+1);
3513
3514 while (t != tt) {
3515 strcpy(p, t->text);
3516 p = strchr(p, '\0');
3517 t = delete_Token(t);
3518 }
3519
3520 t = *tail = tokenize(tmp);
3521 nasm_free(tmp);
3522
3523 while (t->next) {
3524 tail = &t->next;
3525 t = t->next;
3526 }
3527 t->next = tt; /* Attach the remaining token chain */
3528
3529 did_paste = true;
3530 }
3531 paste_head = tail;
3532 tail = &t->next;
3533 break;
3534 }
3535 case TOK_PASTE: /* %+ */
3536 if (handle_paste_tokens) {
3537 /* Zap %+ and whitespace tokens to the right */
3538 while (t && (t->type == TOK_WHITESPACE ||
3539 t->type == TOK_PASTE))
3540 t = *tail = delete_Token(t);
3541 if (!paste_head || !t)
3542 break; /* Nothing to paste with */
3543 tail = paste_head;
3544 t = *tail;
3545 tt = t->next;
3546 while (tok_type_(tt, TOK_WHITESPACE))
3547 tt = t->next = delete_Token(tt);
3548
3549 if (tt) {
3550 tmp = nasm_strcat(t->text, tt->text);
3551 delete_Token(t);
3552 tt = delete_Token(tt);
3553 t = *tail = tokenize(tmp);
3554 nasm_free(tmp);
3555 while (t->next) {
3556 tail = &t->next;
3557 t = t->next;
3558 }
3559 t->next = tt; /* Attach the remaining token chain */
3560 did_paste = true;
3561 }
3562 paste_head = tail;
3563 tail = &t->next;
3564 break;
3565 }
3566 /* else fall through */
3567 default:
3568 tail = paste_head = &t->next;
3569 break;
3570 }
3571 }
3572 return did_paste;
3573}
H. Peter Anvin76690a12002-04-30 20:52:49 +00003574/*
3575 * Expand MMacro-local things: parameter references (%0, %n, %+n,
H. Peter Anvin67c63722008-10-26 23:49:00 -07003576 * %-n) and MMacro-local identifiers (%%foo) as well as
3577 * macro indirection (%[...]).
H. Peter Anvin76690a12002-04-30 20:52:49 +00003578 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003579static Token *expand_mmac_params(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00003580{
H. Peter Anvin734b1882002-04-30 21:01:08 +00003581 Token *t, *tt, **tail, *thead;
H. Peter Anvin6125b622009-04-08 14:02:25 -07003582 bool changed = false;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003583
3584 tail = &thead;
3585 thead = NULL;
3586
H. Peter Anvine2c80182005-01-15 22:15:51 +00003587 while (tline) {
3588 if (tline->type == TOK_PREPROC_ID &&
3589 (((tline->text[1] == '+' || tline->text[1] == '-')
3590 && tline->text[2]) || tline->text[1] == '%'
3591 || (tline->text[1] >= '0' && tline->text[1] <= '9'))) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00003592 char *text = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003593 int type = 0, cc; /* type = 0 to placate optimisers */
Keith Kaniosa6dfa782007-04-13 16:47:53 +00003594 char tmpbuf[30];
H. Peter Anvin25a99342007-09-22 17:45:45 -07003595 unsigned int n;
3596 int i;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003597 MMacro *mac;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003598
H. Peter Anvine2c80182005-01-15 22:15:51 +00003599 t = tline;
3600 tline = tline->next;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003601
H. Peter Anvine2c80182005-01-15 22:15:51 +00003602 mac = istk->mstk;
3603 while (mac && !mac->name) /* avoid mistaking %reps for macros */
3604 mac = mac->next_active;
3605 if (!mac)
3606 error(ERR_NONFATAL, "`%s': not in a macro call", t->text);
3607 else
3608 switch (t->text[1]) {
3609 /*
3610 * We have to make a substitution of one of the
3611 * forms %1, %-1, %+1, %%foo, %0.
3612 */
3613 case '0':
3614 type = TOK_NUMBER;
3615 snprintf(tmpbuf, sizeof(tmpbuf), "%d", mac->nparam);
3616 text = nasm_strdup(tmpbuf);
3617 break;
3618 case '%':
3619 type = TOK_ID;
H. Peter Anvinf8ba53e2007-10-11 10:11:57 -07003620 snprintf(tmpbuf, sizeof(tmpbuf), "..@%"PRIu64".",
H. Peter Anvine2c80182005-01-15 22:15:51 +00003621 mac->unique);
3622 text = nasm_strcat(tmpbuf, t->text + 2);
3623 break;
3624 case '-':
3625 n = atoi(t->text + 2) - 1;
3626 if (n >= mac->nparam)
3627 tt = NULL;
3628 else {
3629 if (mac->nparam > 1)
3630 n = (n + mac->rotate) % mac->nparam;
3631 tt = mac->params[n];
3632 }
3633 cc = find_cc(tt);
3634 if (cc == -1) {
3635 error(ERR_NONFATAL,
3636 "macro parameter %d is not a condition code",
3637 n + 1);
3638 text = NULL;
3639 } else {
3640 type = TOK_ID;
3641 if (inverse_ccs[cc] == -1) {
3642 error(ERR_NONFATAL,
3643 "condition code `%s' is not invertible",
3644 conditions[cc]);
3645 text = NULL;
3646 } else
H. Peter Anvin67c63722008-10-26 23:49:00 -07003647 text = nasm_strdup(conditions[inverse_ccs[cc]]);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003648 }
3649 break;
3650 case '+':
3651 n = atoi(t->text + 2) - 1;
3652 if (n >= mac->nparam)
3653 tt = NULL;
3654 else {
3655 if (mac->nparam > 1)
3656 n = (n + mac->rotate) % mac->nparam;
3657 tt = mac->params[n];
3658 }
3659 cc = find_cc(tt);
3660 if (cc == -1) {
3661 error(ERR_NONFATAL,
3662 "macro parameter %d is not a condition code",
3663 n + 1);
3664 text = NULL;
3665 } else {
3666 type = TOK_ID;
3667 text = nasm_strdup(conditions[cc]);
3668 }
3669 break;
3670 default:
3671 n = atoi(t->text + 1) - 1;
3672 if (n >= mac->nparam)
3673 tt = NULL;
3674 else {
3675 if (mac->nparam > 1)
3676 n = (n + mac->rotate) % mac->nparam;
3677 tt = mac->params[n];
3678 }
3679 if (tt) {
3680 for (i = 0; i < mac->paramlen[n]; i++) {
3681 *tail = new_Token(NULL, tt->type, tt->text, 0);
3682 tail = &(*tail)->next;
3683 tt = tt->next;
3684 }
3685 }
3686 text = NULL; /* we've done it here */
3687 break;
3688 }
3689 if (!text) {
3690 delete_Token(t);
3691 } else {
3692 *tail = t;
3693 tail = &t->next;
3694 t->type = type;
3695 nasm_free(t->text);
3696 t->text = text;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003697 t->a.mac = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003698 }
H. Peter Anvin6125b622009-04-08 14:02:25 -07003699 changed = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003700 continue;
H. Peter Anvin67c63722008-10-26 23:49:00 -07003701 } else if (tline->type == TOK_INDIRECT) {
3702 t = tline;
3703 tline = tline->next;
3704 tt = tokenize(t->text);
3705 tt = expand_mmac_params(tt);
3706 tt = expand_smacro(tt);
3707 *tail = tt;
3708 while (tt) {
3709 tt->a.mac = NULL; /* Necessary? */
3710 tail = &tt->next;
3711 tt = tt->next;
3712 }
3713 delete_Token(t);
H. Peter Anvin6125b622009-04-08 14:02:25 -07003714 changed = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003715 } else {
3716 t = *tail = tline;
3717 tline = tline->next;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003718 t->a.mac = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003719 tail = &t->next;
3720 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00003721 }
H. Peter Anvineba20a72002-04-30 20:53:55 +00003722 *tail = NULL;
H. Peter Anvin67c63722008-10-26 23:49:00 -07003723
H. Peter Anvind784a082009-04-20 14:01:18 -07003724 if (changed)
H. Peter Anvinfc30f8c2009-07-06 18:48:23 -07003725 paste_tokens(&thead, false);
H. Peter Anvin6125b622009-04-08 14:02:25 -07003726
H. Peter Anvin76690a12002-04-30 20:52:49 +00003727 return thead;
3728}
3729
3730/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003731 * Expand all single-line macro calls made in the given line.
3732 * Return the expanded version of the line. The original is deemed
3733 * to be destroyed in the process. (In reality we'll just move
3734 * Tokens from input to output a lot of the time, rather than
3735 * actually bothering to destroy and replicate.)
3736 */
H. Peter Anvincb1cf592007-11-19 12:26:50 -08003737
H. Peter Anvine2c80182005-01-15 22:15:51 +00003738static Token *expand_smacro(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00003739{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003740 Token *t, *tt, *mstart, **tail, *thead;
H. Peter Anvin166c2472008-05-28 12:28:58 -07003741 struct hash_table *smtbl;
H. Peter Anvineba20a72002-04-30 20:53:55 +00003742 SMacro *head = NULL, *m;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003743 Token **params;
3744 int *paramsize;
H. Peter Anvin25a99342007-09-22 17:45:45 -07003745 unsigned int nparam, sparam;
H. Peter Anvind784a082009-04-20 14:01:18 -07003746 int brackets;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003747 Token *org_tline = tline;
3748 Context *ctx;
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08003749 const char *mname;
H. Peter Anvin2a15e692007-11-19 13:14:59 -08003750 int deadman = DEADMAN_LIMIT;
H. Peter Anvin8287daf2009-07-07 16:00:58 -07003751 bool expanded;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003752
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003753 /*
3754 * Trick: we should avoid changing the start token pointer since it can
3755 * be contained in "next" field of other token. Because of this
3756 * we allocate a copy of first token and work with it; at the end of
3757 * routine we copy it back
3758 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003759 if (org_tline) {
3760 tline =
3761 new_Token(org_tline->next, org_tline->type, org_tline->text,
3762 0);
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003763 tline->a.mac = org_tline->a.mac;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003764 nasm_free(org_tline->text);
3765 org_tline->text = NULL;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003766 }
3767
H. Peter Anvin8287daf2009-07-07 16:00:58 -07003768 expanded = true; /* Always expand %+ at least once */
3769
H. Peter Anvincb1cf592007-11-19 12:26:50 -08003770again:
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003771 tail = &thead;
3772 thead = NULL;
3773
H. Peter Anvine2c80182005-01-15 22:15:51 +00003774 while (tline) { /* main token loop */
H. Peter Anvin2a15e692007-11-19 13:14:59 -08003775 if (!--deadman) {
H. Peter Anvincb1cf592007-11-19 12:26:50 -08003776 error(ERR_NONFATAL, "interminable macro recursion");
3777 break;
3778 }
3779
H. Peter Anvine2c80182005-01-15 22:15:51 +00003780 if ((mname = tline->text)) {
3781 /* if this token is a local macro, look in local context */
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08003782 if (tline->type == TOK_ID || tline->type == TOK_PREPROC_ID)
3783 ctx = get_ctx(mname, &mname, true);
3784 else
3785 ctx = NULL;
3786 smtbl = ctx ? &ctx->localmac : &smacros;
H. Peter Anvin166c2472008-05-28 12:28:58 -07003787 head = (SMacro *) hash_findix(smtbl, mname);
H. Peter Anvin072771e2008-05-22 13:17:51 -07003788
H. Peter Anvine2c80182005-01-15 22:15:51 +00003789 /*
3790 * We've hit an identifier. As in is_mmacro below, we first
3791 * check whether the identifier is a single-line macro at
3792 * all, then think about checking for parameters if
3793 * necessary.
3794 */
H. Peter Anvin97a23472007-09-16 17:57:25 -07003795 for (m = head; m; m = m->next)
3796 if (!mstrcmp(m->name, mname, m->casesense))
3797 break;
3798 if (m) {
3799 mstart = tline;
3800 params = NULL;
3801 paramsize = NULL;
3802 if (m->nparam == 0) {
3803 /*
3804 * Simple case: the macro is parameterless. Discard the
3805 * one token that the macro call took, and push the
3806 * expansion back on the to-do stack.
3807 */
3808 if (!m->expansion) {
3809 if (!strcmp("__FILE__", m->name)) {
3810 int32_t num = 0;
H. Peter Anvin932de6c2008-07-31 18:46:11 -07003811 char *file = NULL;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003812 src_get(&num, &file);
3813 tline->text = nasm_quote(file, strlen(file));
H. Peter Anvin97a23472007-09-16 17:57:25 -07003814 tline->type = TOK_STRING;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003815 nasm_free(file);
H. Peter Anvin97a23472007-09-16 17:57:25 -07003816 continue;
3817 }
3818 if (!strcmp("__LINE__", m->name)) {
3819 nasm_free(tline->text);
3820 make_tok_num(tline, src_get_linnum());
3821 continue;
3822 }
3823 if (!strcmp("__BITS__", m->name)) {
3824 nasm_free(tline->text);
3825 make_tok_num(tline, globalbits);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003826 continue;
H. Peter Anvin97a23472007-09-16 17:57:25 -07003827 }
3828 tline = delete_Token(tline);
3829 continue;
3830 }
3831 } else {
3832 /*
3833 * Complicated case: at least one macro with this name
H. Peter Anvine2c80182005-01-15 22:15:51 +00003834 * exists and takes parameters. We must find the
3835 * parameters in the call, count them, find the SMacro
3836 * that corresponds to that form of the macro call, and
3837 * substitute for the parameters when we expand. What a
3838 * pain.
3839 */
3840 /*tline = tline->next;
H. Peter Anvin89cee572009-07-15 09:16:54 -04003841 skip_white_(tline); */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003842 do {
3843 t = tline->next;
3844 while (tok_type_(t, TOK_SMAC_END)) {
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003845 t->a.mac->in_progress = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003846 t->text = NULL;
3847 t = tline->next = delete_Token(t);
3848 }
3849 tline = t;
3850 } while (tok_type_(tline, TOK_WHITESPACE));
3851 if (!tok_is_(tline, "(")) {
3852 /*
3853 * This macro wasn't called with parameters: ignore
3854 * the call. (Behaviour borrowed from gnu cpp.)
3855 */
3856 tline = mstart;
3857 m = NULL;
3858 } else {
3859 int paren = 0;
3860 int white = 0;
3861 brackets = 0;
3862 nparam = 0;
3863 sparam = PARAM_DELTA;
3864 params = nasm_malloc(sparam * sizeof(Token *));
3865 params[0] = tline->next;
3866 paramsize = nasm_malloc(sparam * sizeof(int));
3867 paramsize[0] = 0;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07003868 while (true) { /* parameter loop */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003869 /*
3870 * For some unusual expansions
3871 * which concatenates function call
3872 */
3873 t = tline->next;
3874 while (tok_type_(t, TOK_SMAC_END)) {
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003875 t->a.mac->in_progress = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003876 t->text = NULL;
3877 t = tline->next = delete_Token(t);
3878 }
3879 tline = t;
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00003880
H. Peter Anvine2c80182005-01-15 22:15:51 +00003881 if (!tline) {
3882 error(ERR_NONFATAL,
3883 "macro call expects terminating `)'");
3884 break;
3885 }
3886 if (tline->type == TOK_WHITESPACE
3887 && brackets <= 0) {
3888 if (paramsize[nparam])
3889 white++;
3890 else
3891 params[nparam] = tline->next;
3892 continue; /* parameter loop */
3893 }
3894 if (tline->type == TOK_OTHER
3895 && tline->text[1] == 0) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00003896 char ch = tline->text[0];
H. Peter Anvine2c80182005-01-15 22:15:51 +00003897 if (ch == ',' && !paren && brackets <= 0) {
3898 if (++nparam >= sparam) {
3899 sparam += PARAM_DELTA;
3900 params = nasm_realloc(params,
3901 sparam *
3902 sizeof(Token
3903 *));
3904 paramsize =
3905 nasm_realloc(paramsize,
3906 sparam *
3907 sizeof(int));
3908 }
3909 params[nparam] = tline->next;
3910 paramsize[nparam] = 0;
3911 white = 0;
3912 continue; /* parameter loop */
3913 }
3914 if (ch == '{' &&
3915 (brackets > 0 || (brackets == 0 &&
3916 !paramsize[nparam])))
3917 {
3918 if (!(brackets++)) {
3919 params[nparam] = tline->next;
3920 continue; /* parameter loop */
3921 }
3922 }
3923 if (ch == '}' && brackets > 0)
3924 if (--brackets == 0) {
3925 brackets = -1;
3926 continue; /* parameter loop */
3927 }
3928 if (ch == '(' && !brackets)
3929 paren++;
3930 if (ch == ')' && brackets <= 0)
3931 if (--paren < 0)
3932 break;
3933 }
3934 if (brackets < 0) {
3935 brackets = 0;
3936 error(ERR_NONFATAL, "braces do not "
3937 "enclose all of macro parameter");
3938 }
3939 paramsize[nparam] += white + 1;
3940 white = 0;
3941 } /* parameter loop */
3942 nparam++;
3943 while (m && (m->nparam != nparam ||
3944 mstrcmp(m->name, mname,
3945 m->casesense)))
3946 m = m->next;
3947 if (!m)
H. Peter Anvin917a3492008-09-24 09:14:49 -07003948 error(ERR_WARNING|ERR_PASS1|ERR_WARN_MNP,
H. Peter Anvine2c80182005-01-15 22:15:51 +00003949 "macro `%s' exists, "
3950 "but not taking %d parameters",
3951 mstart->text, nparam);
3952 }
3953 }
3954 if (m && m->in_progress)
3955 m = NULL;
3956 if (!m) { /* in progess or didn't find '(' or wrong nparam */
H. Peter Anvin70653092007-10-19 14:42:29 -07003957 /*
H. Peter Anvine2c80182005-01-15 22:15:51 +00003958 * Design question: should we handle !tline, which
3959 * indicates missing ')' here, or expand those
3960 * macros anyway, which requires the (t) test a few
H. Peter Anvin70653092007-10-19 14:42:29 -07003961 * lines down?
H. Peter Anvine2c80182005-01-15 22:15:51 +00003962 */
3963 nasm_free(params);
3964 nasm_free(paramsize);
3965 tline = mstart;
3966 } else {
3967 /*
3968 * Expand the macro: we are placed on the last token of the
3969 * call, so that we can easily split the call from the
3970 * following tokens. We also start by pushing an SMAC_END
3971 * token for the cycle removal.
3972 */
3973 t = tline;
3974 if (t) {
3975 tline = t->next;
3976 t->next = NULL;
3977 }
3978 tt = new_Token(tline, TOK_SMAC_END, NULL, 0);
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003979 tt->a.mac = m;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07003980 m->in_progress = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003981 tline = tt;
3982 for (t = m->expansion; t; t = t->next) {
3983 if (t->type >= TOK_SMAC_PARAM) {
3984 Token *pcopy = tline, **ptail = &pcopy;
3985 Token *ttt, *pt;
3986 int i;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003987
H. Peter Anvine2c80182005-01-15 22:15:51 +00003988 ttt = params[t->type - TOK_SMAC_PARAM];
3989 for (i = paramsize[t->type - TOK_SMAC_PARAM];
3990 --i >= 0;) {
3991 pt = *ptail =
3992 new_Token(tline, ttt->type, ttt->text,
3993 0);
3994 ptail = &pt->next;
3995 ttt = ttt->next;
3996 }
3997 tline = pcopy;
H. Peter Anvin6c81f0a2008-05-25 21:46:17 -07003998 } else if (t->type == TOK_PREPROC_Q) {
3999 tt = new_Token(tline, TOK_ID, mname, 0);
4000 tline = tt;
4001 } else if (t->type == TOK_PREPROC_QQ) {
4002 tt = new_Token(tline, TOK_ID, m->name, 0);
4003 tline = tt;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004004 } else {
4005 tt = new_Token(tline, t->type, t->text, 0);
4006 tline = tt;
4007 }
4008 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00004009
H. Peter Anvine2c80182005-01-15 22:15:51 +00004010 /*
4011 * Having done that, get rid of the macro call, and clean
4012 * up the parameters.
4013 */
4014 nasm_free(params);
4015 nasm_free(paramsize);
4016 free_tlist(mstart);
H. Peter Anvind784a082009-04-20 14:01:18 -07004017 expanded = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004018 continue; /* main token loop */
4019 }
4020 }
4021 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004022
H. Peter Anvine2c80182005-01-15 22:15:51 +00004023 if (tline->type == TOK_SMAC_END) {
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004024 tline->a.mac->in_progress = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004025 tline = delete_Token(tline);
4026 } else {
4027 t = *tail = tline;
4028 tline = tline->next;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004029 t->a.mac = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004030 t->next = NULL;
4031 tail = &t->next;
4032 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004033 }
4034
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004035 /*
4036 * Now scan the entire line and look for successive TOK_IDs that resulted
Keith Kaniosb7a89542007-04-12 02:40:54 +00004037 * after expansion (they can't be produced by tokenize()). The successive
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004038 * TOK_IDs should be concatenated.
4039 * Also we look for %+ tokens and concatenate the tokens before and after
4040 * them (without white spaces in between).
4041 */
H. Peter Anvin8287daf2009-07-07 16:00:58 -07004042 if (expanded && paste_tokens(&thead, true)) {
H. Peter Anvinfc30f8c2009-07-06 18:48:23 -07004043 /*
4044 * If we concatenated something, *and* we had previously expanded
4045 * an actual macro, scan the lines again for macros...
4046 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004047 tline = thead;
H. Peter Anvin8287daf2009-07-07 16:00:58 -07004048 expanded = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004049 goto again;
H. Peter Anvin734b1882002-04-30 21:01:08 +00004050 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004051
H. Peter Anvine2c80182005-01-15 22:15:51 +00004052 if (org_tline) {
4053 if (thead) {
4054 *org_tline = *thead;
4055 /* since we just gave text to org_line, don't free it */
4056 thead->text = NULL;
4057 delete_Token(thead);
4058 } else {
4059 /* the expression expanded to empty line;
4060 we can't return NULL for some reasons
4061 we just set the line to a single WHITESPACE token. */
4062 memset(org_tline, 0, sizeof(*org_tline));
4063 org_tline->text = NULL;
4064 org_tline->type = TOK_WHITESPACE;
4065 }
4066 thead = org_tline;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004067 }
4068
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004069 return thead;
4070}
4071
4072/*
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004073 * Similar to expand_smacro but used exclusively with macro identifiers
4074 * right before they are fetched in. The reason is that there can be
4075 * identifiers consisting of several subparts. We consider that if there
4076 * are more than one element forming the name, user wants a expansion,
4077 * otherwise it will be left as-is. Example:
4078 *
4079 * %define %$abc cde
4080 *
4081 * the identifier %$abc will be left as-is so that the handler for %define
4082 * will suck it and define the corresponding value. Other case:
4083 *
4084 * %define _%$abc cde
4085 *
4086 * In this case user wants name to be expanded *before* %define starts
4087 * working, so we'll expand %$abc into something (if it has a value;
4088 * otherwise it will be left as-is) then concatenate all successive
4089 * PP_IDs into one.
4090 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004091static Token *expand_id(Token * tline)
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004092{
4093 Token *cur, *oldnext = NULL;
4094
H. Peter Anvin734b1882002-04-30 21:01:08 +00004095 if (!tline || !tline->next)
H. Peter Anvine2c80182005-01-15 22:15:51 +00004096 return tline;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004097
4098 cur = tline;
4099 while (cur->next &&
H. Peter Anvine2c80182005-01-15 22:15:51 +00004100 (cur->next->type == TOK_ID ||
4101 cur->next->type == TOK_PREPROC_ID
4102 || cur->next->type == TOK_NUMBER))
4103 cur = cur->next;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004104
4105 /* If identifier consists of just one token, don't expand */
4106 if (cur == tline)
H. Peter Anvine2c80182005-01-15 22:15:51 +00004107 return tline;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004108
H. Peter Anvine2c80182005-01-15 22:15:51 +00004109 if (cur) {
4110 oldnext = cur->next; /* Detach the tail past identifier */
4111 cur->next = NULL; /* so that expand_smacro stops here */
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004112 }
4113
H. Peter Anvin734b1882002-04-30 21:01:08 +00004114 tline = expand_smacro(tline);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004115
H. Peter Anvine2c80182005-01-15 22:15:51 +00004116 if (cur) {
4117 /* expand_smacro possibly changhed tline; re-scan for EOL */
4118 cur = tline;
4119 while (cur && cur->next)
4120 cur = cur->next;
4121 if (cur)
4122 cur->next = oldnext;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004123 }
4124
4125 return tline;
4126}
4127
4128/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004129 * Determine whether the given line constitutes a multi-line macro
4130 * call, and return the MMacro structure called if so. Doesn't have
4131 * to check for an initial label - that's taken care of in
4132 * expand_mmacro - but must check numbers of parameters. Guaranteed
4133 * to be called with tline->type == TOK_ID, so the putative macro
4134 * name is easy to find.
4135 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004136static MMacro *is_mmacro(Token * tline, Token *** params_array)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004137{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004138 MMacro *head, *m;
4139 Token **params;
4140 int nparam;
4141
H. Peter Anvin166c2472008-05-28 12:28:58 -07004142 head = (MMacro *) hash_findix(&mmacros, tline->text);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004143
4144 /*
4145 * Efficiency: first we see if any macro exists with the given
4146 * name. If not, we can return NULL immediately. _Then_ we
4147 * count the parameters, and then we look further along the
4148 * list if necessary to find the proper MMacro.
4149 */
4150 for (m = head; m; m = m->next)
H. Peter Anvine2c80182005-01-15 22:15:51 +00004151 if (!mstrcmp(m->name, tline->text, m->casesense))
4152 break;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004153 if (!m)
H. Peter Anvine2c80182005-01-15 22:15:51 +00004154 return NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004155
4156 /*
4157 * OK, we have a potential macro. Count and demarcate the
4158 * parameters.
4159 */
H. Peter Anvin734b1882002-04-30 21:01:08 +00004160 count_mmac_params(tline->next, &nparam, &params);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004161
4162 /*
4163 * So we know how many parameters we've got. Find the MMacro
4164 * structure that handles this number.
4165 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004166 while (m) {
4167 if (m->nparam_min <= nparam
4168 && (m->plus || nparam <= m->nparam_max)) {
4169 /*
4170 * This one is right. Just check if cycle removal
4171 * prohibits us using it before we actually celebrate...
4172 */
H. Peter Anvin89cee572009-07-15 09:16:54 -04004173 if (m->in_progress > m->max_depth) {
4174 if (m->max_depth > 0) {
4175 error(ERR_WARNING,
4176 "reached maximum recursion depth of %i",
4177 m->max_depth);
4178 }
4179 nasm_free(params);
4180 return NULL;
4181 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00004182 /*
4183 * It's right, and we can use it. Add its default
4184 * parameters to the end of our list if necessary.
4185 */
4186 if (m->defaults && nparam < m->nparam_min + m->ndefs) {
4187 params =
4188 nasm_realloc(params,
4189 ((m->nparam_min + m->ndefs +
4190 1) * sizeof(*params)));
4191 while (nparam < m->nparam_min + m->ndefs) {
4192 params[nparam] = m->defaults[nparam - m->nparam_min];
4193 nparam++;
4194 }
4195 }
4196 /*
4197 * If we've gone over the maximum parameter count (and
4198 * we're in Plus mode), ignore parameters beyond
4199 * nparam_max.
4200 */
4201 if (m->plus && nparam > m->nparam_max)
4202 nparam = m->nparam_max;
4203 /*
4204 * Then terminate the parameter list, and leave.
4205 */
4206 if (!params) { /* need this special case */
4207 params = nasm_malloc(sizeof(*params));
4208 nparam = 0;
4209 }
4210 params[nparam] = NULL;
4211 *params_array = params;
4212 return m;
4213 }
4214 /*
4215 * This one wasn't right: look for the next one with the
4216 * same name.
4217 */
4218 for (m = m->next; m; m = m->next)
4219 if (!mstrcmp(m->name, tline->text, m->casesense))
4220 break;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004221 }
4222
4223 /*
4224 * After all that, we didn't find one with the right number of
4225 * parameters. Issue a warning, and fail to expand the macro.
4226 */
H. Peter Anvin917a3492008-09-24 09:14:49 -07004227 error(ERR_WARNING|ERR_PASS1|ERR_WARN_MNP,
H. Peter Anvine2c80182005-01-15 22:15:51 +00004228 "macro `%s' exists, but not taking %d parameters",
4229 tline->text, nparam);
H. Peter Anvin734b1882002-04-30 21:01:08 +00004230 nasm_free(params);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004231 return NULL;
4232}
4233
Keith Kanios891775e2009-07-11 06:08:54 -05004234
4235/*
4236 * Save MMacro invocation specific fields in
4237 * preparation for a recursive macro expansion
4238 */
4239static void push_mmacro(MMacro *m)
4240{
4241 MMacroInvocation *i;
4242
H. Peter Anvin89cee572009-07-15 09:16:54 -04004243 i = nasm_malloc(sizeof(MMacroInvocation));
4244 i->prev = m->prev;
4245 i->params = m->params;
4246 i->iline = m->iline;
4247 i->nparam = m->nparam;
4248 i->rotate = m->rotate;
4249 i->paramlen = m->paramlen;
4250 i->unique = m->unique;
4251 m->prev = i;
Keith Kanios891775e2009-07-11 06:08:54 -05004252}
4253
4254
4255/*
4256 * Restore MMacro invocation specific fields that were
4257 * saved during a previous recursive macro expansion
4258 */
4259static void pop_mmacro(MMacro *m)
4260{
4261 MMacroInvocation *i;
4262
H. Peter Anvin89cee572009-07-15 09:16:54 -04004263 if (m->prev) {
4264 i = m->prev;
4265 m->prev = i->prev;
4266 m->params = i->params;
4267 m->iline = i->iline;
4268 m->nparam = i->nparam;
4269 m->rotate = i->rotate;
4270 m->paramlen = i->paramlen;
4271 m->unique = i->unique;
4272 nasm_free(i);
4273 }
Keith Kanios891775e2009-07-11 06:08:54 -05004274}
4275
4276
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004277/*
4278 * Expand the multi-line macro call made by the given line, if
4279 * there is one to be expanded. If there is, push the expansion on
H. Peter Anvineba20a72002-04-30 20:53:55 +00004280 * istk->expansion and return 1. Otherwise return 0.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004281 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004282static int expand_mmacro(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004283{
4284 Token *startline = tline;
4285 Token *label = NULL;
4286 int dont_prepend = 0;
H. Peter Anvince2233b2008-05-25 21:57:00 -07004287 Token **params, *t, *mtok, *tt;
H. Peter Anvineba20a72002-04-30 20:53:55 +00004288 MMacro *m;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004289 Line *l, *ll;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004290 int i, nparam, *paramlen;
H. Peter Anvinc751e862008-06-09 10:18:45 -07004291 const char *mname;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004292
4293 t = tline;
H. Peter Anvineba20a72002-04-30 20:53:55 +00004294 skip_white_(t);
H. Peter Anvince2233b2008-05-25 21:57:00 -07004295 /* if (!tok_type_(t, TOK_ID)) Lino 02/25/02 */
H. Peter Anvindce1e2f2002-04-30 21:06:37 +00004296 if (!tok_type_(t, TOK_ID) && !tok_type_(t, TOK_PREPROC_ID))
H. Peter Anvine2c80182005-01-15 22:15:51 +00004297 return 0;
H. Peter Anvince2233b2008-05-25 21:57:00 -07004298 mtok = t;
H. Peter Anvin734b1882002-04-30 21:01:08 +00004299 m = is_mmacro(t, &params);
H. Peter Anvinc751e862008-06-09 10:18:45 -07004300 if (m) {
4301 mname = t->text;
4302 } else {
H. Peter Anvine2c80182005-01-15 22:15:51 +00004303 Token *last;
4304 /*
4305 * We have an id which isn't a macro call. We'll assume
4306 * it might be a label; we'll also check to see if a
4307 * colon follows it. Then, if there's another id after
4308 * that lot, we'll check it again for macro-hood.
4309 */
4310 label = last = t;
4311 t = t->next;
4312 if (tok_type_(t, TOK_WHITESPACE))
4313 last = t, t = t->next;
4314 if (tok_is_(t, ":")) {
4315 dont_prepend = 1;
4316 last = t, t = t->next;
4317 if (tok_type_(t, TOK_WHITESPACE))
4318 last = t, t = t->next;
4319 }
H. Peter Anvin89cee572009-07-15 09:16:54 -04004320 if (!tok_type_(t, TOK_ID) || !(m = is_mmacro(t, &params)))
H. Peter Anvine2c80182005-01-15 22:15:51 +00004321 return 0;
4322 last->next = NULL;
Keith Kanios891775e2009-07-11 06:08:54 -05004323 mname = t->text;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004324 tline = t;
H. Peter Anvineba20a72002-04-30 20:53:55 +00004325 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004326
4327 /*
4328 * Fix up the parameters: this involves stripping leading and
4329 * trailing whitespace, then stripping braces if they are
4330 * present.
4331 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004332 for (nparam = 0; params[nparam]; nparam++) ;
H. Peter Anvin734b1882002-04-30 21:01:08 +00004333 paramlen = nparam ? nasm_malloc(nparam * sizeof(*paramlen)) : NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004334
H. Peter Anvine2c80182005-01-15 22:15:51 +00004335 for (i = 0; params[i]; i++) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07004336 int brace = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004337 int comma = (!m->plus || i < nparam - 1);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004338
H. Peter Anvine2c80182005-01-15 22:15:51 +00004339 t = params[i];
4340 skip_white_(t);
4341 if (tok_is_(t, "{"))
H. Peter Anvin6867acc2007-10-10 14:58:45 -07004342 t = t->next, brace = true, comma = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004343 params[i] = t;
4344 paramlen[i] = 0;
4345 while (t) {
4346 if (comma && t->type == TOK_OTHER && !strcmp(t->text, ","))
4347 break; /* ... because we have hit a comma */
4348 if (comma && t->type == TOK_WHITESPACE
4349 && tok_is_(t->next, ","))
4350 break; /* ... or a space then a comma */
4351 if (brace && t->type == TOK_OTHER && !strcmp(t->text, "}"))
4352 break; /* ... or a brace */
4353 t = t->next;
4354 paramlen[i]++;
4355 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004356 }
4357
4358 /*
4359 * OK, we have a MMacro structure together with a set of
4360 * parameters. We must now go through the expansion and push
H. Peter Anvin76690a12002-04-30 20:52:49 +00004361 * copies of each Line on to istk->expansion. Substitution of
4362 * parameter tokens and macro-local tokens doesn't get done
4363 * until the single-line macro substitution process; this is
4364 * because delaying them allows us to change the semantics
4365 * later through %rotate.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004366 *
H. Peter Anvin76690a12002-04-30 20:52:49 +00004367 * First, push an end marker on to istk->expansion, mark this
4368 * macro as in progress, and set up its invocation-specific
4369 * variables.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004370 */
4371 ll = nasm_malloc(sizeof(Line));
4372 ll->next = istk->expansion;
4373 ll->finishes = m;
4374 ll->first = NULL;
4375 istk->expansion = ll;
Keith Kanios891775e2009-07-11 06:08:54 -05004376
H. Peter Anvin89cee572009-07-15 09:16:54 -04004377 /*
4378 * Save the previous MMacro expansion in the case of
4379 * macro recursion
4380 */
4381 if (m->max_depth && m->in_progress)
4382 push_mmacro(m);
H. Peter Anvin76690a12002-04-30 20:52:49 +00004383
Keith Kanios891775e2009-07-11 06:08:54 -05004384 m->in_progress ++;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004385 m->params = params;
4386 m->iline = tline;
4387 m->nparam = nparam;
4388 m->rotate = 0;
4389 m->paramlen = paramlen;
4390 m->unique = unique++;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004391 m->lineno = 0;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004392
4393 m->next_active = istk->mstk;
4394 istk->mstk = m;
4395
H. Peter Anvine2c80182005-01-15 22:15:51 +00004396 for (l = m->expansion; l; l = l->next) {
4397 Token **tail;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004398
H. Peter Anvine2c80182005-01-15 22:15:51 +00004399 ll = nasm_malloc(sizeof(Line));
4400 ll->finishes = NULL;
4401 ll->next = istk->expansion;
4402 istk->expansion = ll;
4403 tail = &ll->first;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004404
H. Peter Anvine2c80182005-01-15 22:15:51 +00004405 for (t = l->first; t; t = t->next) {
4406 Token *x = t;
H. Peter Anvince2233b2008-05-25 21:57:00 -07004407 switch (t->type) {
4408 case TOK_PREPROC_Q:
H. Peter Anvinc751e862008-06-09 10:18:45 -07004409 tt = *tail = new_Token(NULL, TOK_ID, mname, 0);
H. Peter Anvince2233b2008-05-25 21:57:00 -07004410 break;
4411 case TOK_PREPROC_QQ:
4412 tt = *tail = new_Token(NULL, TOK_ID, m->name, 0);
4413 break;
4414 case TOK_PREPROC_ID:
4415 if (t->text[1] == '0' && t->text[2] == '0') {
4416 dont_prepend = -1;
4417 x = label;
4418 if (!x)
4419 continue;
4420 }
4421 /* fall through */
4422 default:
4423 tt = *tail = new_Token(NULL, x->type, x->text, 0);
4424 break;
H. Peter Anvin166c2472008-05-28 12:28:58 -07004425 }
H. Peter Anvince2233b2008-05-25 21:57:00 -07004426 tail = &tt->next;
4427 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00004428 *tail = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004429 }
4430
4431 /*
H. Peter Anvineba20a72002-04-30 20:53:55 +00004432 * If we had a label, push it on as the first line of
4433 * the macro expansion.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004434 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004435 if (label) {
4436 if (dont_prepend < 0)
4437 free_tlist(startline);
4438 else {
4439 ll = nasm_malloc(sizeof(Line));
4440 ll->finishes = NULL;
4441 ll->next = istk->expansion;
4442 istk->expansion = ll;
4443 ll->first = startline;
4444 if (!dont_prepend) {
4445 while (label->next)
4446 label = label->next;
4447 label->next = tt = new_Token(NULL, TOK_OTHER, ":", 0);
4448 }
4449 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004450 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004451
H. Peter Anvin734b1882002-04-30 21:01:08 +00004452 list->uplevel(m->nolist ? LIST_MACRO_NOLIST : LIST_MACRO);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004453
H. Peter Anvineba20a72002-04-30 20:53:55 +00004454 return 1;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004455}
4456
Victor van den Elzen3b404c02008-09-18 13:51:36 +02004457/* The function that actually does the error reporting */
4458static void verror(int severity, const char *fmt, va_list arg)
4459{
4460 char buff[1024];
4461
4462 vsnprintf(buff, sizeof(buff), fmt, arg);
4463
4464 if (istk && istk->mstk && istk->mstk->name)
H. Peter Anvindbb640b2009-07-18 18:57:16 -07004465 nasm_error(severity, "(%s:%d) %s", istk->mstk->name,
Victor van den Elzen3b404c02008-09-18 13:51:36 +02004466 istk->mstk->lineno, buff);
4467 else
H. Peter Anvindbb640b2009-07-18 18:57:16 -07004468 nasm_error(severity, "%s", buff);
Victor van den Elzen3b404c02008-09-18 13:51:36 +02004469}
4470
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004471/*
4472 * Since preprocessor always operate only on the line that didn't
H. Peter Anvin917a3492008-09-24 09:14:49 -07004473 * arrived yet, we should always use ERR_OFFBY1.
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004474 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004475static void error(int severity, const char *fmt, ...)
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004476{
4477 va_list arg;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004478
4479 /* If we're in a dead branch of IF or something like it, ignore the error */
H. Peter Anvin77ba0c62002-05-14 03:18:53 +00004480 if (istk && istk->conds && !emitting(istk->conds->state))
H. Peter Anvine2c80182005-01-15 22:15:51 +00004481 return;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004482
H. Peter Anvin734b1882002-04-30 21:01:08 +00004483 va_start(arg, fmt);
Victor van den Elzen3b404c02008-09-18 13:51:36 +02004484 verror(severity, fmt, arg);
H. Peter Anvin734b1882002-04-30 21:01:08 +00004485 va_end(arg);
Victor van den Elzen3b404c02008-09-18 13:51:36 +02004486}
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004487
Victor van den Elzen3b404c02008-09-18 13:51:36 +02004488/*
4489 * Because %else etc are evaluated in the state context
4490 * of the previous branch, errors might get lost with error():
4491 * %if 0 ... %else trailing garbage ... %endif
4492 * So %else etc should report errors with this function.
4493 */
4494static void error_precond(int severity, const char *fmt, ...)
4495{
4496 va_list arg;
4497
4498 /* Only ignore the error if it's really in a dead branch */
4499 if (istk && istk->conds && istk->conds->state == COND_NEVER)
4500 return;
4501
4502 va_start(arg, fmt);
4503 verror(severity, fmt, arg);
4504 va_end(arg);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004505}
4506
H. Peter Anvin734b1882002-04-30 21:01:08 +00004507static void
H. Peter Anvindbb640b2009-07-18 18:57:16 -07004508pp_reset(char *file, int apass, ListGen * listgen, StrList **deplist)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004509{
H. Peter Anvin7383b402008-09-24 10:20:40 -07004510 Token *t;
4511
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004512 cstk = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004513 istk = nasm_malloc(sizeof(Include));
4514 istk->next = NULL;
4515 istk->conds = NULL;
4516 istk->expansion = NULL;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004517 istk->mstk = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004518 istk->fp = fopen(file, "r");
H. Peter Anvineba20a72002-04-30 20:53:55 +00004519 istk->fname = NULL;
4520 src_set_fname(nasm_strdup(file));
4521 src_set_linnum(0);
4522 istk->lineinc = 1;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004523 if (!istk->fp)
H. Peter Anvin917a3492008-09-24 09:14:49 -07004524 error(ERR_FATAL|ERR_NOFILE, "unable to open input file `%s'",
H. Peter Anvine2c80182005-01-15 22:15:51 +00004525 file);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004526 defining = NULL;
Charles Crayned4200be2008-07-12 16:42:33 -07004527 nested_mac_count = 0;
4528 nested_rep_count = 0;
H. Peter Anvin97a23472007-09-16 17:57:25 -07004529 init_macros();
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004530 unique = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004531 if (tasm_compatible_mode) {
H. Peter Anvina4835d42008-05-20 14:21:29 -07004532 stdmacpos = nasm_stdmac;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004533 } else {
H. Peter Anvina4835d42008-05-20 14:21:29 -07004534 stdmacpos = nasm_stdmac_after_tasm;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004535 }
H. Peter Anvind2456592008-06-19 15:04:18 -07004536 any_extrastdmac = extrastdmac && *extrastdmac;
4537 do_predef = true;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004538 list = listgen;
H. Peter Anvin61f130f2008-09-25 15:45:06 -07004539
4540 /*
4541 * 0 for dependencies, 1 for preparatory passes, 2 for final pass.
4542 * The caller, however, will also pass in 3 for preprocess-only so
4543 * we can set __PASS__ accordingly.
4544 */
4545 pass = apass > 2 ? 2 : apass;
4546
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07004547 dephead = deptail = deplist;
4548 if (deplist) {
4549 StrList *sl = nasm_malloc(strlen(file)+1+sizeof sl->next);
4550 sl->next = NULL;
4551 strcpy(sl->str, file);
4552 *deptail = sl;
4553 deptail = &sl->next;
4554 }
H. Peter Anvin7383b402008-09-24 10:20:40 -07004555
H. Peter Anvin61f130f2008-09-25 15:45:06 -07004556 /*
4557 * Define the __PASS__ macro. This is defined here unlike
4558 * all the other builtins, because it is special -- it varies between
4559 * passes.
4560 */
H. Peter Anvin7383b402008-09-24 10:20:40 -07004561 t = nasm_malloc(sizeof(*t));
4562 t->next = NULL;
H. Peter Anvin61f130f2008-09-25 15:45:06 -07004563 make_tok_num(t, apass);
H. Peter Anvin7383b402008-09-24 10:20:40 -07004564 t->a.mac = NULL;
4565 define_smacro(NULL, "__PASS__", true, 0, t);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004566}
4567
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004568static char *pp_getline(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004569{
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004570 char *line;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004571 Token *tline;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004572
H. Peter Anvine2c80182005-01-15 22:15:51 +00004573 while (1) {
4574 /*
Keith Kaniosb7a89542007-04-12 02:40:54 +00004575 * Fetch a tokenized line, either from the macro-expansion
H. Peter Anvine2c80182005-01-15 22:15:51 +00004576 * buffer or from the input file.
4577 */
4578 tline = NULL;
4579 while (istk->expansion && istk->expansion->finishes) {
4580 Line *l = istk->expansion;
4581 if (!l->finishes->name && l->finishes->in_progress > 1) {
4582 Line *ll;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004583
H. Peter Anvine2c80182005-01-15 22:15:51 +00004584 /*
4585 * This is a macro-end marker for a macro with no
4586 * name, which means it's not really a macro at all
4587 * but a %rep block, and the `in_progress' field is
4588 * more than 1, meaning that we still need to
4589 * repeat. (1 means the natural last repetition; 0
4590 * means termination by %exitrep.) We have
4591 * therefore expanded up to the %endrep, and must
4592 * push the whole block on to the expansion buffer
4593 * again. We don't bother to remove the macro-end
4594 * marker: we'd only have to generate another one
4595 * if we did.
4596 */
4597 l->finishes->in_progress--;
4598 for (l = l->finishes->expansion; l; l = l->next) {
4599 Token *t, *tt, **tail;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004600
H. Peter Anvine2c80182005-01-15 22:15:51 +00004601 ll = nasm_malloc(sizeof(Line));
4602 ll->next = istk->expansion;
4603 ll->finishes = NULL;
4604 ll->first = NULL;
4605 tail = &ll->first;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004606
H. Peter Anvine2c80182005-01-15 22:15:51 +00004607 for (t = l->first; t; t = t->next) {
4608 if (t->text || t->type == TOK_WHITESPACE) {
4609 tt = *tail =
4610 new_Token(NULL, t->type, t->text, 0);
4611 tail = &tt->next;
4612 }
4613 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00004614
H. Peter Anvine2c80182005-01-15 22:15:51 +00004615 istk->expansion = ll;
4616 }
4617 } else {
4618 /*
4619 * Check whether a `%rep' was started and not ended
4620 * within this macro expansion. This can happen and
4621 * should be detected. It's a fatal error because
4622 * I'm too confused to work out how to recover
4623 * sensibly from it.
4624 */
4625 if (defining) {
4626 if (defining->name)
4627 error(ERR_PANIC,
4628 "defining with name in expansion");
4629 else if (istk->mstk->name)
4630 error(ERR_FATAL,
4631 "`%%rep' without `%%endrep' within"
4632 " expansion of macro `%s'",
4633 istk->mstk->name);
4634 }
H. Peter Anvin87bc6192002-04-30 20:53:16 +00004635
H. Peter Anvine2c80182005-01-15 22:15:51 +00004636 /*
4637 * FIXME: investigate the relationship at this point between
4638 * istk->mstk and l->finishes
4639 */
4640 {
4641 MMacro *m = istk->mstk;
4642 istk->mstk = m->next_active;
4643 if (m->name) {
4644 /*
4645 * This was a real macro call, not a %rep, and
4646 * therefore the parameter information needs to
4647 * be freed.
4648 */
H. Peter Anvin89cee572009-07-15 09:16:54 -04004649 if (m->prev) {
4650 pop_mmacro(m);
4651 l->finishes->in_progress --;
4652 } else {
Keith Kanios891775e2009-07-11 06:08:54 -05004653 nasm_free(m->params);
4654 free_tlist(m->iline);
H. Peter Anvin89cee572009-07-15 09:16:54 -04004655 nasm_free(m->paramlen);
4656 l->finishes->in_progress = 0;
4657 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00004658 } else
4659 free_mmacro(m);
4660 }
4661 istk->expansion = l->next;
4662 nasm_free(l);
4663 list->downlevel(LIST_MACRO);
4664 }
4665 }
4666 while (1) { /* until we get a line we can use */
H. Peter Anvineba20a72002-04-30 20:53:55 +00004667
H. Peter Anvine2c80182005-01-15 22:15:51 +00004668 if (istk->expansion) { /* from a macro expansion */
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004669 char *p;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004670 Line *l = istk->expansion;
4671 if (istk->mstk)
4672 istk->mstk->lineno++;
4673 tline = l->first;
4674 istk->expansion = l->next;
4675 nasm_free(l);
H. Peter Anvin6867acc2007-10-10 14:58:45 -07004676 p = detoken(tline, false);
H. Peter Anvine2c80182005-01-15 22:15:51 +00004677 list->line(LIST_MACRO, p);
4678 nasm_free(p);
4679 break;
4680 }
4681 line = read_line();
4682 if (line) { /* from the current input file */
4683 line = prepreproc(line);
Keith Kaniosb7a89542007-04-12 02:40:54 +00004684 tline = tokenize(line);
H. Peter Anvine2c80182005-01-15 22:15:51 +00004685 nasm_free(line);
4686 break;
4687 }
4688 /*
4689 * The current file has ended; work down the istk
4690 */
4691 {
4692 Include *i = istk;
4693 fclose(i->fp);
4694 if (i->conds)
4695 error(ERR_FATAL,
4696 "expected `%%endif' before end of file");
4697 /* only set line and file name if there's a next node */
4698 if (i->next) {
4699 src_set_linnum(i->lineno);
4700 nasm_free(src_set_fname(i->fname));
H. Peter Anvin86877b22008-06-20 15:55:45 -07004701 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00004702 istk = i->next;
4703 list->downlevel(LIST_INCLUDE);
4704 nasm_free(i);
4705 if (!istk)
4706 return NULL;
Victor van den Elzen4c9d6222008-10-01 13:08:50 +02004707 if (istk->expansion && istk->expansion->finishes)
4708 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004709 }
4710 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004711
H. Peter Anvine2c80182005-01-15 22:15:51 +00004712 /*
4713 * We must expand MMacro parameters and MMacro-local labels
4714 * _before_ we plunge into directive processing, to cope
4715 * with things like `%define something %1' such as STRUC
4716 * uses. Unless we're _defining_ a MMacro, in which case
4717 * those tokens should be left alone to go into the
4718 * definition; and unless we're in a non-emitting
4719 * condition, in which case we don't want to meddle with
4720 * anything.
4721 */
Charles Crayned4200be2008-07-12 16:42:33 -07004722 if (!defining && !(istk->conds && !emitting(istk->conds->state))
H. Peter Anvin992fe752008-10-19 15:45:05 -07004723 && !(istk->mstk && !istk->mstk->in_progress)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00004724 tline = expand_mmac_params(tline);
H. Peter Anvin992fe752008-10-19 15:45:05 -07004725 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00004726
H. Peter Anvine2c80182005-01-15 22:15:51 +00004727 /*
4728 * Check the line to see if it's a preprocessor directive.
4729 */
4730 if (do_directive(tline) == DIRECTIVE_FOUND) {
4731 continue;
4732 } else if (defining) {
4733 /*
4734 * We're defining a multi-line macro. We emit nothing
4735 * at all, and just
Keith Kaniosb7a89542007-04-12 02:40:54 +00004736 * shove the tokenized line on to the macro definition.
H. Peter Anvine2c80182005-01-15 22:15:51 +00004737 */
4738 Line *l = nasm_malloc(sizeof(Line));
4739 l->next = defining->expansion;
4740 l->first = tline;
H. Peter Anvin538002d2008-06-28 18:30:27 -07004741 l->finishes = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004742 defining->expansion = l;
4743 continue;
4744 } else if (istk->conds && !emitting(istk->conds->state)) {
4745 /*
4746 * We're in a non-emitting branch of a condition block.
4747 * Emit nothing at all, not even a blank line: when we
4748 * emerge from the condition we'll give a line-number
4749 * directive so we keep our place correctly.
4750 */
4751 free_tlist(tline);
4752 continue;
4753 } else if (istk->mstk && !istk->mstk->in_progress) {
4754 /*
4755 * We're in a %rep block which has been terminated, so
4756 * we're walking through to the %endrep without
4757 * emitting anything. Emit nothing at all, not even a
4758 * blank line: when we emerge from the %rep block we'll
4759 * give a line-number directive so we keep our place
4760 * correctly.
4761 */
4762 free_tlist(tline);
4763 continue;
4764 } else {
4765 tline = expand_smacro(tline);
4766 if (!expand_mmacro(tline)) {
4767 /*
Keith Kaniosb7a89542007-04-12 02:40:54 +00004768 * De-tokenize the line again, and emit it.
H. Peter Anvine2c80182005-01-15 22:15:51 +00004769 */
H. Peter Anvin6867acc2007-10-10 14:58:45 -07004770 line = detoken(tline, true);
H. Peter Anvine2c80182005-01-15 22:15:51 +00004771 free_tlist(tline);
4772 break;
4773 } else {
4774 continue; /* expand_mmacro calls free_tlist */
4775 }
4776 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004777 }
4778
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004779 return line;
4780}
4781
H. Peter Anvine2c80182005-01-15 22:15:51 +00004782static void pp_cleanup(int pass)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004783{
H. Peter Anvine2c80182005-01-15 22:15:51 +00004784 if (defining) {
H. Peter Anvin89cee572009-07-15 09:16:54 -04004785 if (defining->name) {
Victor van den Elzen8f1120f2008-07-16 13:41:37 +02004786 error(ERR_NONFATAL,
4787 "end of file while still defining macro `%s'",
4788 defining->name);
4789 } else {
4790 error(ERR_NONFATAL, "end of file while still in %%rep");
4791 }
4792
H. Peter Anvine2c80182005-01-15 22:15:51 +00004793 free_mmacro(defining);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004794 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004795 while (cstk)
H. Peter Anvine2c80182005-01-15 22:15:51 +00004796 ctx_pop();
H. Peter Anvin97a23472007-09-16 17:57:25 -07004797 free_macros();
H. Peter Anvine2c80182005-01-15 22:15:51 +00004798 while (istk) {
4799 Include *i = istk;
4800 istk = istk->next;
4801 fclose(i->fp);
4802 nasm_free(i->fname);
4803 nasm_free(i);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004804 }
4805 while (cstk)
H. Peter Anvine2c80182005-01-15 22:15:51 +00004806 ctx_pop();
H. Peter Anvin86877b22008-06-20 15:55:45 -07004807 nasm_free(src_set_fname(NULL));
H. Peter Anvine2c80182005-01-15 22:15:51 +00004808 if (pass == 0) {
H. Peter Anvin86877b22008-06-20 15:55:45 -07004809 IncPath *i;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004810 free_llist(predef);
4811 delete_Blocks();
H. Peter Anvin86877b22008-06-20 15:55:45 -07004812 while ((i = ipath)) {
4813 ipath = i->next;
4814 if (i->path)
4815 nasm_free(i->path);
4816 nasm_free(i);
4817 }
H. Peter Anvin11dfa1a2008-07-02 18:11:04 -07004818 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004819}
4820
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004821void pp_include_path(char *path)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004822{
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004823 IncPath *i;
H. Peter Anvin37a321f2007-09-24 13:41:58 -07004824
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004825 i = nasm_malloc(sizeof(IncPath));
H. Peter Anvin37a321f2007-09-24 13:41:58 -07004826 i->path = path ? nasm_strdup(path) : NULL;
Frank Kotlerd0ed6fd2003-08-27 11:33:56 +00004827 i->next = NULL;
4828
H. Peter Anvin89cee572009-07-15 09:16:54 -04004829 if (ipath) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00004830 IncPath *j = ipath;
H. Peter Anvin89cee572009-07-15 09:16:54 -04004831 while (j->next)
H. Peter Anvine2c80182005-01-15 22:15:51 +00004832 j = j->next;
4833 j->next = i;
4834 } else {
4835 ipath = i;
Frank Kotlerd0ed6fd2003-08-27 11:33:56 +00004836 }
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004837}
Frank Kotlerd0ed6fd2003-08-27 11:33:56 +00004838
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004839void pp_pre_include(char *fname)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004840{
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004841 Token *inc, *space, *name;
4842 Line *l;
4843
H. Peter Anvin734b1882002-04-30 21:01:08 +00004844 name = new_Token(NULL, TOK_INTERNAL_STRING, fname, 0);
4845 space = new_Token(name, TOK_WHITESPACE, NULL, 0);
4846 inc = new_Token(space, TOK_PREPROC_ID, "%include", 0);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004847
4848 l = nasm_malloc(sizeof(Line));
4849 l->next = predef;
4850 l->first = inc;
H. Peter Anvin538002d2008-06-28 18:30:27 -07004851 l->finishes = NULL;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004852 predef = l;
4853}
4854
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004855void pp_pre_define(char *definition)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004856{
4857 Token *def, *space;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004858 Line *l;
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004859 char *equals;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004860
4861 equals = strchr(definition, '=');
H. Peter Anvin734b1882002-04-30 21:01:08 +00004862 space = new_Token(NULL, TOK_WHITESPACE, NULL, 0);
4863 def = new_Token(space, TOK_PREPROC_ID, "%define", 0);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004864 if (equals)
H. Peter Anvine2c80182005-01-15 22:15:51 +00004865 *equals = ' ';
Keith Kaniosb7a89542007-04-12 02:40:54 +00004866 space->next = tokenize(definition);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004867 if (equals)
H. Peter Anvine2c80182005-01-15 22:15:51 +00004868 *equals = '=';
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004869
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004870 l = nasm_malloc(sizeof(Line));
4871 l->next = predef;
4872 l->first = def;
H. Peter Anvin538002d2008-06-28 18:30:27 -07004873 l->finishes = NULL;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004874 predef = l;
4875}
4876
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004877void pp_pre_undefine(char *definition)
H. Peter Anvin620515a2002-04-30 20:57:38 +00004878{
4879 Token *def, *space;
4880 Line *l;
4881
H. Peter Anvin734b1882002-04-30 21:01:08 +00004882 space = new_Token(NULL, TOK_WHITESPACE, NULL, 0);
4883 def = new_Token(space, TOK_PREPROC_ID, "%undef", 0);
Keith Kaniosb7a89542007-04-12 02:40:54 +00004884 space->next = tokenize(definition);
H. Peter Anvin620515a2002-04-30 20:57:38 +00004885
4886 l = nasm_malloc(sizeof(Line));
4887 l->next = predef;
4888 l->first = def;
H. Peter Anvin538002d2008-06-28 18:30:27 -07004889 l->finishes = NULL;
H. Peter Anvin620515a2002-04-30 20:57:38 +00004890 predef = l;
4891}
4892
Keith Kaniosb7a89542007-04-12 02:40:54 +00004893/*
4894 * Added by Keith Kanios:
4895 *
4896 * This function is used to assist with "runtime" preprocessor
4897 * directives. (e.g. pp_runtime("%define __BITS__ 64");)
4898 *
4899 * ERRORS ARE IGNORED HERE, SO MAKE COMPLETELY SURE THAT YOU
4900 * PASS A VALID STRING TO THIS FUNCTION!!!!!
4901 */
4902
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004903void pp_runtime(char *definition)
Keith Kaniosb7a89542007-04-12 02:40:54 +00004904{
4905 Token *def;
H. Peter Anvin70653092007-10-19 14:42:29 -07004906
Keith Kaniosb7a89542007-04-12 02:40:54 +00004907 def = tokenize(definition);
H. Peter Anvin89cee572009-07-15 09:16:54 -04004908 if (do_directive(def) == NO_DIRECTIVE_FOUND)
Keith Kaniosb7a89542007-04-12 02:40:54 +00004909 free_tlist(def);
H. Peter Anvin70653092007-10-19 14:42:29 -07004910
Keith Kaniosb7a89542007-04-12 02:40:54 +00004911}
4912
H. Peter Anvina70547f2008-07-19 21:44:26 -07004913void pp_extra_stdmac(macros_t *macros)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004914{
H. Peter Anvin76690a12002-04-30 20:52:49 +00004915 extrastdmac = macros;
4916}
4917
Keith Kaniosa5fc6462007-10-13 07:09:22 -07004918static void make_tok_num(Token * tok, int64_t val)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004919{
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004920 char numbuf[20];
Keith Kaniosa5fc6462007-10-13 07:09:22 -07004921 snprintf(numbuf, sizeof(numbuf), "%"PRId64"", val);
H. Peter Anvineba20a72002-04-30 20:53:55 +00004922 tok->text = nasm_strdup(numbuf);
4923 tok->type = TOK_NUMBER;
4924}
4925
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004926Preproc nasmpp = {
4927 pp_reset,
4928 pp_getline,
4929 pp_cleanup
4930};