blob: 5d94309d6a7a34d37fcb7e593cf6034fe204417b [file] [log] [blame]
H. Peter Anvin9e6747c2009-06-28 17:13:04 -07001/* ----------------------------------------------------------------------- *
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002 *
Cyrill Gorcunov82667ff2011-06-25 19:34:19 +04003 * Copyright 1996-2011 The NASM Authors - All Rights Reserved
H. Peter Anvin9e6747c2009-06-28 17:13:04 -07004 * See the file AUTHORS included with the NASM distribution for
5 * the specific copyright holders.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00006 *
H. Peter Anvin9e6747c2009-06-28 17:13:04 -07007 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following
9 * conditions are met:
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000010 *
H. Peter Anvin9e6747c2009-06-28 17:13:04 -070011 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above
14 * copyright notice, this list of conditions and the following
15 * disclaimer in the documentation and/or other materials provided
16 * with the distribution.
Cyrill Gorcunovaccda192010-02-16 10:27:56 +030017 *
H. Peter Anvin9e6747c2009-06-28 17:13:04 -070018 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
19 * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
20 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
21 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
23 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
26 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
29 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
30 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 *
32 * ----------------------------------------------------------------------- */
33
34/*
35 * preproc.c macro preprocessor for the Netwide Assembler
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000036 */
37
H. Peter Anvin4836e332002-04-30 20:56:43 +000038/* Typical flow of text through preproc
39 *
Keith Kaniosb7a89542007-04-12 02:40:54 +000040 * pp_getline gets tokenized lines, either
H. Peter Anvin4836e332002-04-30 20:56:43 +000041 *
42 * from a macro expansion
43 *
44 * or
45 * {
46 * read_line gets raw text from stdmacpos, or predef, or current input file
Keith Kaniosb7a89542007-04-12 02:40:54 +000047 * tokenize converts to tokens
H. Peter Anvin4836e332002-04-30 20:56:43 +000048 * }
49 *
50 * expand_mmac_params is used to expand %1 etc., unless a macro is being
51 * defined or a false conditional is being processed
52 * (%0, %1, %+1, %-1, %%foo
53 *
54 * do_directive checks for directives
55 *
56 * expand_smacro is used to expand single line macros
57 *
58 * expand_mmacro is used to expand multi-line macros
59 *
60 * detoken is used to convert the line back to text
61 */
H. Peter Anvineba20a72002-04-30 20:53:55 +000062
H. Peter Anvinfe501952007-10-02 21:53:51 -070063#include "compiler.h"
64
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000065#include <stdio.h>
H. Peter Anvinaf535c12002-04-30 20:59:21 +000066#include <stdarg.h>
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000067#include <stdlib.h>
68#include <stddef.h>
69#include <string.h>
70#include <ctype.h>
H. Peter Anvin76690a12002-04-30 20:52:49 +000071#include <limits.h>
Keith Kaniosb7a89542007-04-12 02:40:54 +000072#include <inttypes.h>
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000073
74#include "nasm.h"
75#include "nasmlib.h"
H. Peter Anvin4169a472007-09-12 01:29:43 +000076#include "preproc.h"
H. Peter Anvin97a23472007-09-16 17:57:25 -070077#include "hashtbl.h"
H. Peter Anvin8cad14b2008-06-01 17:23:51 -070078#include "quote.h"
H. Peter Anvinc2df2822007-10-24 15:29:28 -070079#include "stdscan.h"
H. Peter Anvindbb640b2009-07-18 18:57:16 -070080#include "eval.h"
H. Peter Anvinc2df2822007-10-24 15:29:28 -070081#include "tokens.h"
H. Peter Anvina4835d42008-05-20 14:21:29 -070082#include "tables.h"
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000083
84typedef struct SMacro SMacro;
Keith Kaniosb307a4f2010-11-06 17:41:51 -050085typedef struct ExpDef ExpDef;
86typedef struct ExpInv ExpInv;
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 Anvind7ed89e2002-04-30 20:52:08 +0000117 * The context stack is composed of a linked list of these.
118 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000119struct Context {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000120 Context *next;
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000121 char *name;
H. Peter Anvin166c2472008-05-28 12:28:58 -0700122 struct hash_table localmac;
Keith Kaniosb7a89542007-04-12 02:40:54 +0000123 uint32_t number;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000124};
125
126/*
127 * This is the internal form which we break input lines up into.
128 * Typically stored in linked lists.
129 *
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000130 * Note that `type' serves a double meaning: TOK_SMAC_PARAM is not
131 * necessarily used as-is, but is intended to denote the number of
132 * the substituted parameter. So in the definition
133 *
134 * %define a(x,y) ( (x) & ~(y) )
H. Peter Anvin70653092007-10-19 14:42:29 -0700135 *
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000136 * the token representing `x' will have its type changed to
137 * TOK_SMAC_PARAM, but the one representing `y' will be
138 * TOK_SMAC_PARAM+1.
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000139 *
140 * TOK_INTERNAL_STRING is a dirty hack: it's a single string token
141 * which doesn't need quotes around it. Used in the pre-include
142 * mechanism as an alternative to trying to find a sensible type of
143 * quote to use on the filename we were passed.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000144 */
H. Peter Anvinda10e7b2007-09-12 04:18:37 +0000145enum pp_token_type {
146 TOK_NONE = 0, TOK_WHITESPACE, TOK_COMMENT, TOK_ID,
147 TOK_PREPROC_ID, TOK_STRING,
H. Peter Anvin6c81f0a2008-05-25 21:46:17 -0700148 TOK_NUMBER, TOK_FLOAT, TOK_SMAC_END, TOK_OTHER,
149 TOK_INTERNAL_STRING,
150 TOK_PREPROC_Q, TOK_PREPROC_QQ,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300151 TOK_PASTE, /* %+ */
152 TOK_INDIRECT, /* %[...] */
153 TOK_SMAC_PARAM, /* MUST BE LAST IN THE LIST!!! */
154 TOK_MAX = INT_MAX /* Keep compiler from reducing the range */
H. Peter Anvinda10e7b2007-09-12 04:18:37 +0000155};
156
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +0400157#define PP_CONCAT_MASK(x) (1 << (x))
158
Cyrill Gorcunov575d4282010-10-06 00:25:55 +0400159struct tokseq_match {
160 int mask_head;
161 int mask_tail;
162};
163
H. Peter Anvine2c80182005-01-15 22:15:51 +0000164struct Token {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000165 Token *next;
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000166 char *text;
H. Peter Anvinf26e0972008-07-01 21:26:27 -0700167 union {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300168 SMacro *mac; /* associated macro for TOK_SMAC_END */
169 size_t len; /* scratch length field */
170 } a; /* Auxiliary data */
H. Peter Anvinda10e7b2007-09-12 04:18:37 +0000171 enum pp_token_type type;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000172};
173
174/*
Keith Kaniosb307a4f2010-11-06 17:41:51 -0500175 * Expansion definitions are stored as a linked list of
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000176 * these, which is essentially a container to allow several linked
177 * lists of Tokens.
H. Peter Anvin70653092007-10-19 14:42:29 -0700178 *
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000179 * Note that in this module, linked lists are treated as stacks
180 * wherever possible. For this reason, Lines are _pushed_ on to the
Keith Kaniosb307a4f2010-11-06 17:41:51 -0500181 * `last' field in ExpDef structures, so that the linked list,
182 * if walked, would emit the expansion lines in the proper order.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000183 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000184struct Line {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000185 Line *next;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000186 Token *first;
187};
188
189/*
Keith Kaniosb307a4f2010-11-06 17:41:51 -0500190 * Expansion Types
191 */
192enum pp_exp_type {
193 EXP_NONE = 0, EXP_PREDEF,
Cyrill Gorcunova5aea572010-11-11 10:14:45 +0300194 EXP_MMACRO, EXP_REP,
Keith Kaniosb307a4f2010-11-06 17:41:51 -0500195 EXP_IF, EXP_WHILE,
Cyrill Gorcunova5aea572010-11-11 10:14:45 +0300196 EXP_COMMENT, EXP_FINAL,
Keith Kaniosb307a4f2010-11-06 17:41:51 -0500197 EXP_MAX = INT_MAX /* Keep compiler from reducing the range */
198};
199
200/*
201 * Store the definition of an expansion, in which is any
202 * preprocessor directive that has an ending pair.
203 *
204 * This design allows for arbitrary expansion/recursion depth,
205 * upto the DEADMAN_LIMIT.
206 *
207 * The `next' field is used for storing ExpDef in hash tables; the
208 * `prev' field is for the global `expansions` linked-list.
209 */
210struct ExpDef {
211 ExpDef *prev; /* previous definition */
Cyrill Gorcunova5aea572010-11-11 10:14:45 +0300212 ExpDef *next; /* next in hash table */
Keith Kaniosb307a4f2010-11-06 17:41:51 -0500213 enum pp_exp_type type; /* expansion type */
214 char *name; /* definition name */
215 int nparam_min, nparam_max;
216 bool casesense;
217 bool plus; /* is the last parameter greedy? */
218 bool nolist; /* is this expansion listing-inhibited? */
219 Token *dlist; /* all defaults as one list */
220 Token **defaults; /* parameter default pointers */
221 int ndefs; /* number of default parameters */
Cyrill Gorcunova5aea572010-11-11 10:14:45 +0300222
223 int prepend; /* label prepend state */
224 Line *label;
Keith Kaniosb307a4f2010-11-06 17:41:51 -0500225 Line *line;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +0300226 Line *last;
227 int linecount; /* number of lines within expansion */
228
229 int64_t def_depth; /* current number of definition pairs deep */
Keith Kaniosb307a4f2010-11-06 17:41:51 -0500230 int64_t cur_depth; /* current number of expansions */
231 int64_t max_depth; /* maximum number of expansions allowed */
Cyrill Gorcunova5aea572010-11-11 10:14:45 +0300232
233 int state; /* condition state */
234 bool ignoring; /* ignoring definition lines */
Keith Kaniosb307a4f2010-11-06 17:41:51 -0500235};
236
237/*
238 * Store the invocation of an expansion.
239 *
240 * The `prev' field is for the `istk->expansion` linked-list.
241 *
242 * When an expansion is being expanded, `params', `iline', `nparam',
243 * `paramlen', `rotate' and `unique' are local to the invocation.
244 */
245struct ExpInv {
246 ExpInv *prev; /* previous invocation */
247 enum pp_exp_type type; /* expansion type */
248 ExpDef *def; /* pointer to expansion definition */
Cyrill Gorcunova5aea572010-11-11 10:14:45 +0300249 char *name; /* invocation name */
250 Line *label; /* pointer to label */
251 char *label_text; /* pointer to label text */
Keith Kaniosb307a4f2010-11-06 17:41:51 -0500252 Line *current; /* pointer to current line in invocation */
Cyrill Gorcunova5aea572010-11-11 10:14:45 +0300253
Keith Kaniosb307a4f2010-11-06 17:41:51 -0500254 Token **params; /* actual parameters */
255 Token *iline; /* invocation line */
256 unsigned int nparam, rotate;
257 int *paramlen;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +0300258
259 uint64_t unique;
Keith Kaniosb307a4f2010-11-06 17:41:51 -0500260 bool emitting;
261 int lineno; /* current line number in expansion */
Cyrill Gorcunova5aea572010-11-11 10:14:45 +0300262 int linnum; /* line number at invocation */
263 int relno; /* relative line number at invocation */
Keith Kaniosb307a4f2010-11-06 17:41:51 -0500264};
265
266/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000267 * To handle an arbitrary level of file inclusion, we maintain a
268 * stack (ie linked list) of these things.
269 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000270struct Include {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000271 Include *next;
272 FILE *fp;
273 Cond *conds;
Keith Kaniosb307a4f2010-11-06 17:41:51 -0500274 ExpInv *expansion;
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000275 char *fname;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000276 int lineno, lineinc;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +0300277 int mmac_depth;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000278};
279
280/*
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000281 * Include search path. This is simply a list of strings which get
282 * prepended, in turn, to the name of an include file, in an
283 * attempt to find the file if it's not in the current directory.
284 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000285struct IncPath {
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000286 IncPath *next;
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000287 char *path;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000288};
289
290/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000291 * Conditional assembly: we maintain a separate stack of these for
292 * each level of file inclusion. (The only reason we keep the
293 * stacks separate is to ensure that a stray `%endif' in a file
294 * included from within the true branch of a `%if' won't terminate
295 * it and cause confusion: instead, rightly, it'll cause an error.)
296 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000297enum {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000298 /*
299 * These states are for use just after %if or %elif: IF_TRUE
300 * means the condition has evaluated to truth so we are
301 * currently emitting, whereas IF_FALSE means we are not
302 * currently emitting but will start doing so if a %else comes
303 * up. In these states, all directives are admissible: %elif,
304 * %else and %endif. (And of course %if.)
305 */
306 COND_IF_TRUE, COND_IF_FALSE,
307 /*
308 * These states come up after a %else: ELSE_TRUE means we're
309 * emitting, and ELSE_FALSE means we're not. In ELSE_* states,
310 * any %elif or %else will cause an error.
311 */
312 COND_ELSE_TRUE, COND_ELSE_FALSE,
313 /*
Victor van den Elzen3b404c02008-09-18 13:51:36 +0200314 * These states mean that we're not emitting now, and also that
315 * nothing until %endif will be emitted at all. COND_DONE is
316 * used when we've had our moment of emission
317 * and have now started seeing %elifs. COND_NEVER is used when
318 * the condition construct in question is contained within a
319 * non-emitting branch of a larger condition construct,
320 * or if there is an error.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000321 */
Victor van den Elzen3b404c02008-09-18 13:51:36 +0200322 COND_DONE, COND_NEVER
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000323};
324#define emitting(x) ( (x) == COND_IF_TRUE || (x) == COND_ELSE_TRUE )
325
H. Peter Anvin70653092007-10-19 14:42:29 -0700326/*
Ed Beroset3ab3f412002-06-11 03:31:49 +0000327 * These defines are used as the possible return values for do_directive
328 */
329#define NO_DIRECTIVE_FOUND 0
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300330#define DIRECTIVE_FOUND 1
Ed Beroset3ab3f412002-06-11 03:31:49 +0000331
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000332/*
Keith Kaniosb307a4f2010-11-06 17:41:51 -0500333 * This define sets the upper limit for smacro and expansions
Keith Kanios852f1ee2009-07-12 00:19:55 -0500334 */
335#define DEADMAN_LIMIT (1 << 20)
336
Cyrill Gorcunove091d6e2010-08-09 13:58:22 +0400337/* max reps */
338#define REP_LIMIT ((INT64_C(1) << 62))
339
Keith Kanios852f1ee2009-07-12 00:19:55 -0500340/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000341 * Condition codes. Note that we use c_ prefix not C_ because C_ is
342 * used in nasm.h for the "real" condition codes. At _this_ level,
343 * we treat CXZ and ECXZ as condition codes, albeit non-invertible
344 * ones, so we need a different enum...
345 */
H. Peter Anvin476d2862007-10-02 22:04:15 -0700346static const char * const conditions[] = {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000347 "a", "ae", "b", "be", "c", "cxz", "e", "ecxz", "g", "ge", "l", "le",
348 "na", "nae", "nb", "nbe", "nc", "ne", "ng", "nge", "nl", "nle", "no",
H. Peter Anvince9be342007-09-12 00:22:29 +0000349 "np", "ns", "nz", "o", "p", "pe", "po", "rcxz", "s", "z"
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000350};
H. Peter Anvin476d2862007-10-02 22:04:15 -0700351enum pp_conds {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000352 c_A, c_AE, c_B, c_BE, c_C, c_CXZ, c_E, c_ECXZ, c_G, c_GE, c_L, c_LE,
353 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 -0700354 c_NP, c_NS, c_NZ, c_O, c_P, c_PE, c_PO, c_RCXZ, c_S, c_Z,
355 c_none = -1
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000356};
H. Peter Anvin476d2862007-10-02 22:04:15 -0700357static const enum pp_conds inverse_ccs[] = {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000358 c_NA, c_NAE, c_NB, c_NBE, c_NC, -1, c_NE, -1, c_NG, c_NGE, c_NL, c_NLE,
359 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 +0000360 c_Z, c_NO, c_NP, c_PO, c_PE, -1, c_NS, c_NZ
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000361};
362
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000363/* For TASM compatibility we need to be able to recognise TASM compatible
364 * conditional compilation directives. Using the NASM pre-processor does
365 * not work, so we look for them specifically from the following list and
366 * then jam in the equivalent NASM directive into the input stream.
367 */
368
H. Peter Anvine2c80182005-01-15 22:15:51 +0000369enum {
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000370 TM_ARG, TM_ELIF, TM_ELSE, TM_ENDIF, TM_IF, TM_IFDEF, TM_IFDIFI,
371 TM_IFNDEF, TM_INCLUDE, TM_LOCAL
372};
373
H. Peter Anvin476d2862007-10-02 22:04:15 -0700374static const char * const tasm_directives[] = {
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000375 "arg", "elif", "else", "endif", "if", "ifdef", "ifdifi",
376 "ifndef", "include", "local"
377};
378
379static int StackSize = 4;
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000380static char *StackPointer = "ebp";
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000381static int ArgOffset = 8;
H. Peter Anvin8781cb02007-11-08 20:01:11 -0800382static int LocalOffset = 0;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000383
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000384static Context *cstk;
385static Include *istk;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000386static IncPath *ipath = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000387
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300388static int pass; /* HACK: pass 0 = generate dependencies only */
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700389static StrList **dephead, **deptail; /* Dependency list */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000390
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300391static uint64_t unique; /* unique identifier numbers */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000392
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000393static Line *predef = NULL;
H. Peter Anvind2456592008-06-19 15:04:18 -0700394static bool do_predef;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000395
396static ListGen *list;
397
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000398/*
Keith Kaniosb307a4f2010-11-06 17:41:51 -0500399 * The current set of expansion definitions we have defined.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000400 */
Keith Kaniosb307a4f2010-11-06 17:41:51 -0500401static struct hash_table expdefs;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000402
403/*
404 * The current set of single-line macros we have defined.
405 */
H. Peter Anvin166c2472008-05-28 12:28:58 -0700406static struct hash_table smacros;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000407
408/*
Keith Kaniosb307a4f2010-11-06 17:41:51 -0500409 * Linked List of all active expansion definitions
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000410 */
Keith Kaniosb307a4f2010-11-06 17:41:51 -0500411struct ExpDef *expansions = NULL;
412
413/*
414 * The expansion we are currently defining
415 */
416static ExpDef *defining = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000417
Charles Crayned4200be2008-07-12 16:42:33 -0700418static uint64_t nested_mac_count;
419static uint64_t nested_rep_count;
420
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000421/*
Keith Kaniosb307a4f2010-11-06 17:41:51 -0500422 * Linked-list of lines to preprocess, prior to cleanup
423 */
424static Line *finals = NULL;
425static bool in_final = false;
426
427/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000428 * The number of macro parameters to allocate space for at a time.
429 */
430#define PARAM_DELTA 16
431
432/*
H. Peter Anvina4835d42008-05-20 14:21:29 -0700433 * The standard macro set: defined in macros.c in the array nasm_stdmac.
434 * This gives our position in the macro set, when we're processing it.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000435 */
H. Peter Anvina70547f2008-07-19 21:44:26 -0700436static macros_t *stdmacpos;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000437
438/*
H. Peter Anvin76690a12002-04-30 20:52:49 +0000439 * The extra standard macros that come from the object format, if
440 * any.
441 */
H. Peter Anvina70547f2008-07-19 21:44:26 -0700442static macros_t *extrastdmac = NULL;
H. Peter Anvincfb71762008-06-20 15:20:16 -0700443static bool any_extrastdmac;
H. Peter Anvin76690a12002-04-30 20:52:49 +0000444
445/*
H. Peter Anvin734b1882002-04-30 21:01:08 +0000446 * Tokens are allocated in blocks to improve speed
447 */
448#define TOKEN_BLOCKSIZE 4096
449static Token *freeTokens = NULL;
H. Peter Anvince616072002-04-30 21:02:23 +0000450struct Blocks {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000451 Blocks *next;
452 void *chunk;
H. Peter Anvince616072002-04-30 21:02:23 +0000453};
454
455static Blocks blocks = { NULL, NULL };
H. Peter Anvin734b1882002-04-30 21:01:08 +0000456
457/*
H. Peter Anvin76690a12002-04-30 20:52:49 +0000458 * Forward declarations.
459 */
H. Peter Anvin734b1882002-04-30 21:01:08 +0000460static Token *expand_mmac_params(Token * tline);
461static Token *expand_smacro(Token * tline);
462static Token *expand_id(Token * tline);
H. Peter Anvinf8ad5322009-02-21 17:55:08 -0800463static Context *get_ctx(const char *name, const char **namep,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300464 bool all_contexts);
Keith Kaniosa5fc6462007-10-13 07:09:22 -0700465static void make_tok_num(Token * tok, int64_t val);
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000466static void error(int severity, const char *fmt, ...);
Victor van den Elzen3b404c02008-09-18 13:51:36 +0200467static void error_precond(int severity, const char *fmt, ...);
H. Peter Anvince616072002-04-30 21:02:23 +0000468static void *new_Block(size_t size);
469static void delete_Blocks(void);
H. Peter Anvinc751e862008-06-09 10:18:45 -0700470static Token *new_Token(Token * next, enum pp_token_type type,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300471 const char *text, int txtlen);
Keith Kaniosb307a4f2010-11-06 17:41:51 -0500472static Token *copy_Token(Token * tline);
H. Peter Anvin734b1882002-04-30 21:01:08 +0000473static Token *delete_Token(Token * t);
Keith Kaniosb307a4f2010-11-06 17:41:51 -0500474static Line *new_Line(void);
475static ExpDef *new_ExpDef(int exp_type);
476static ExpInv *new_ExpInv(int exp_type, ExpDef *ed);
H. Peter Anvineba20a72002-04-30 20:53:55 +0000477
478/*
479 * Macros for safe checking of token pointers, avoid *(NULL)
480 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300481#define tok_type_(x,t) ((x) && (x)->type == (t))
482#define skip_white_(x) if (tok_type_((x), TOK_WHITESPACE)) (x)=(x)->next
483#define tok_is_(x,v) (tok_type_((x), TOK_OTHER) && !strcmp((x)->text,(v)))
484#define tok_isnt_(x,v) ((x) && ((x)->type!=TOK_OTHER || strcmp((x)->text,(v))))
H. Peter Anvin76690a12002-04-30 20:52:49 +0000485
Cyrill Gorcunov49e8f692010-11-11 15:06:12 +0300486#ifdef NASM_TRACE
487
Cyrill Gorcunovfc0c1282011-06-25 19:51:44 +0400488#define stringify(x) #x
489
Cyrill Gorcunov9d1141a2011-06-26 23:07:35 +0400490#define nasm_trace(msg, ...) printf("(%s:%d): " msg "\n", __func__, __LINE__, ##__VA_ARGS__)
Cyrill Gorcunovfc0c1282011-06-25 19:51:44 +0400491#define nasm_dump_token(t) nasm_raw_dump_token(t, __FILE__, __LINE__, __func__);
Cyrill Gorcunov2e046002011-06-26 23:33:56 +0400492#define nasm_dump_stream(t) nasm_raw_dump_stream(t, __FILE__, __LINE__, __func__);
Cyrill Gorcunovfc0c1282011-06-25 19:51:44 +0400493
494/* FIXME: we really need some compound type here instead of inplace code */
495static const char *nasm_get_tok_type_str(enum pp_token_type type)
496{
497#define SWITCH_TOK_NAME(type) \
498 case (type): \
499 return stringify(type)
500
501 switch (type) {
502 SWITCH_TOK_NAME(TOK_NONE);
503 SWITCH_TOK_NAME(TOK_WHITESPACE);
504 SWITCH_TOK_NAME(TOK_COMMENT);
505 SWITCH_TOK_NAME(TOK_ID);
506 SWITCH_TOK_NAME(TOK_PREPROC_ID);
507 SWITCH_TOK_NAME(TOK_STRING);
508 SWITCH_TOK_NAME(TOK_NUMBER);
509 SWITCH_TOK_NAME(TOK_FLOAT);
510 SWITCH_TOK_NAME(TOK_SMAC_END);
511 SWITCH_TOK_NAME(TOK_OTHER);
512 SWITCH_TOK_NAME(TOK_INTERNAL_STRING);
513 SWITCH_TOK_NAME(TOK_PREPROC_Q);
514 SWITCH_TOK_NAME(TOK_PREPROC_QQ);
515 SWITCH_TOK_NAME(TOK_PASTE);
516 SWITCH_TOK_NAME(TOK_INDIRECT);
517 SWITCH_TOK_NAME(TOK_SMAC_PARAM);
518 SWITCH_TOK_NAME(TOK_MAX);
519 }
520
521 return NULL;
522}
523
524static void nasm_raw_dump_token(Token *token, const char *file, int line, const char *func)
Cyrill Gorcunov49e8f692010-11-11 15:06:12 +0300525{
526 printf("---[%s (%s:%d): %p]---\n", func, file, line, (void *)token);
527 if (token) {
528 Token *t;
529 list_for_each(t, token) {
530 if (t->text)
Cyrill Gorcunovfc0c1282011-06-25 19:51:44 +0400531 printf("'%s'(%s) ", t->text,
532 nasm_get_tok_type_str(t->type));
Cyrill Gorcunov49e8f692010-11-11 15:06:12 +0300533 }
Cyrill Gorcunovfc0c1282011-06-25 19:51:44 +0400534 printf("\n\n");
Cyrill Gorcunov49e8f692010-11-11 15:06:12 +0300535 }
536}
537
Cyrill Gorcunov2e046002011-06-26 23:33:56 +0400538static void nasm_raw_dump_stream(Token *token, const char *file, int line, const char *func)
539{
540 printf("---[%s (%s:%d): %p]---\n", func, file, line, (void *)token);
541 if (token) {
542 Token *t;
543 list_for_each(t, token)
544 printf("%s", t->text ? t->text : " ");
545 printf("\n\n");
546 }
547}
548
Cyrill Gorcunovfc0c1282011-06-25 19:51:44 +0400549#else
550#define nasm_trace(msg, ...)
551#define nasm_dump_token(t)
Cyrill Gorcunov2e046002011-06-26 23:33:56 +0400552#define nasm_dump_stream(t)
Cyrill Gorcunov49e8f692010-11-11 15:06:12 +0300553#endif
554
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300555/*
H. Peter Anvin077fb932010-07-20 14:56:30 -0700556 * nasm_unquote with error if the string contains NUL characters.
557 * If the string contains NUL characters, issue an error and return
558 * the C len, i.e. truncate at the NUL.
559 */
560static size_t nasm_unquote_cstr(char *qstr, enum preproc_token directive)
561{
562 size_t len = nasm_unquote(qstr, NULL);
563 size_t clen = strlen(qstr);
564
565 if (len != clen)
566 error(ERR_NONFATAL, "NUL character in `%s' directive",
567 pp_directives[directive]);
568
569 return clen;
570}
571
572/*
H. Peter Anvinb40992c2010-09-15 08:57:21 -0700573 * In-place reverse a list of tokens.
574 */
575static Token *reverse_tokens(Token *t)
576{
Cyrill Gorcunov3eba69a2011-04-13 13:15:02 +0400577 Token *prev, *next;
H. Peter Anvinb40992c2010-09-15 08:57:21 -0700578
Cyrill Gorcunov3eba69a2011-04-13 13:15:02 +0400579 list_reverse(t, prev, next);
H. Peter Anvinb40992c2010-09-15 08:57:21 -0700580
Cyrill Gorcunov3eba69a2011-04-13 13:15:02 +0400581 return t;
H. Peter Anvinb40992c2010-09-15 08:57:21 -0700582}
583
584/*
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300585 * Handle TASM specific directives, which do not contain a % in
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000586 * front of them. We do it here because I could not find any other
587 * place to do it for the moment, and it is a hack (ideally it would
588 * be nice to be able to use the NASM pre-processor to do it).
589 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000590static char *check_tasm_directive(char *line)
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000591{
Keith Kaniosb7a89542007-04-12 02:40:54 +0000592 int32_t i, j, k, m, len;
Cyrill Gorcunovf66ac7d2009-10-12 20:41:13 +0400593 char *p, *q, *oldline, oldchar;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000594
Cyrill Gorcunovf66ac7d2009-10-12 20:41:13 +0400595 p = nasm_skip_spaces(line);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000596
597 /* Binary search for the directive name */
598 i = -1;
Cyrill Gorcunova7319242010-06-03 22:04:36 +0400599 j = ARRAY_SIZE(tasm_directives);
Cyrill Gorcunovf66ac7d2009-10-12 20:41:13 +0400600 q = nasm_skip_word(p);
601 len = q - p;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000602 if (len) {
603 oldchar = p[len];
604 p[len] = 0;
605 while (j - i > 1) {
606 k = (j + i) / 2;
607 m = nasm_stricmp(p, tasm_directives[k]);
608 if (m == 0) {
609 /* We have found a directive, so jam a % in front of it
610 * so that NASM will then recognise it as one if it's own.
611 */
612 p[len] = oldchar;
613 len = strlen(p);
614 oldline = line;
615 line = nasm_malloc(len + 2);
616 line[0] = '%';
617 if (k == TM_IFDIFI) {
H. Peter Anvin18f48792009-06-27 15:56:27 -0700618 /*
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300619 * NASM does not recognise IFDIFI, so we convert
620 * it to %if 0. This is not used in NASM
621 * compatible code, but does need to parse for the
622 * TASM macro package.
H. Peter Anvine2c80182005-01-15 22:15:51 +0000623 */
H. Peter Anvin18f48792009-06-27 15:56:27 -0700624 strcpy(line + 1, "if 0");
H. Peter Anvine2c80182005-01-15 22:15:51 +0000625 } else {
626 memcpy(line + 1, p, len + 1);
627 }
628 nasm_free(oldline);
629 return line;
630 } else if (m < 0) {
631 j = k;
632 } else
633 i = k;
634 }
635 p[len] = oldchar;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000636 }
637 return line;
638}
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000639
H. Peter Anvin76690a12002-04-30 20:52:49 +0000640/*
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000641 * The pre-preprocessing stage... This function translates line
642 * number indications as they emerge from GNU cpp (`# lineno "file"
643 * flags') into NASM preprocessor line number indications (`%line
644 * lineno file').
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000645 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000646static char *prepreproc(char *line)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000647{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000648 int lineno, fnlen;
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000649 char *fname, *oldline;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000650
H. Peter Anvine2c80182005-01-15 22:15:51 +0000651 if (line[0] == '#' && line[1] == ' ') {
652 oldline = line;
653 fname = oldline + 2;
654 lineno = atoi(fname);
655 fname += strspn(fname, "0123456789 ");
656 if (*fname == '"')
657 fname++;
658 fnlen = strcspn(fname, "\"");
659 line = nasm_malloc(20 + fnlen);
660 snprintf(line, 20 + fnlen, "%%line %d %.*s", lineno, fnlen, fname);
661 nasm_free(oldline);
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000662 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000663 if (tasm_compatible_mode)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000664 return check_tasm_directive(line);
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000665 return line;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000666}
667
668/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000669 * Free a linked list of tokens.
670 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000671static void free_tlist(Token * list)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000672{
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +0400673 while (list)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000674 list = delete_Token(list);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000675}
676
677/*
678 * Free a linked list of lines.
679 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000680static void free_llist(Line * list)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000681{
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +0400682 Line *l, *tmp;
683 list_for_each_safe(l, tmp, list) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000684 free_tlist(l->first);
685 nasm_free(l);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000686 }
687}
688
689/*
Keith Kaniosb307a4f2010-11-06 17:41:51 -0500690 * Free an ExpDef
H. Peter Anvineba20a72002-04-30 20:53:55 +0000691 */
Keith Kaniosb307a4f2010-11-06 17:41:51 -0500692static void free_expdef(ExpDef * ed)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000693{
Keith Kaniosb307a4f2010-11-06 17:41:51 -0500694 nasm_free(ed->name);
695 free_tlist(ed->dlist);
696 nasm_free(ed->defaults);
697 free_llist(ed->line);
698 nasm_free(ed);
699}
700
701/*
702 * Free an ExpInv
703 */
704static void free_expinv(ExpInv * ei)
705{
Cyrill Gorcunova5aea572010-11-11 10:14:45 +0300706 if (ei->name != NULL)
707 nasm_free(ei->name);
708 if (ei->label_text != NULL)
709 nasm_free(ei->label_text);
710 nasm_free(ei);
H. Peter Anvineba20a72002-04-30 20:53:55 +0000711}
712
713/*
H. Peter Anvin97a23472007-09-16 17:57:25 -0700714 * Free all currently defined macros, and free the hash tables
715 */
H. Peter Anvin072771e2008-05-22 13:17:51 -0700716static void free_smacro_table(struct hash_table *smt)
H. Peter Anvin97a23472007-09-16 17:57:25 -0700717{
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +0400718 SMacro *s, *tmp;
H. Peter Anvin072771e2008-05-22 13:17:51 -0700719 const char *key;
720 struct hash_tbl_node *it = NULL;
H. Peter Anvin97a23472007-09-16 17:57:25 -0700721
H. Peter Anvin072771e2008-05-22 13:17:51 -0700722 while ((s = hash_iterate(smt, &it, &key)) != NULL) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300723 nasm_free((void *)key);
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +0400724 list_for_each_safe(s, tmp, s) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300725 nasm_free(s->name);
726 free_tlist(s->expansion);
727 nasm_free(s);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300728 }
H. Peter Anvin97a23472007-09-16 17:57:25 -0700729 }
H. Peter Anvin072771e2008-05-22 13:17:51 -0700730 hash_free(smt);
731}
732
Keith Kaniosb307a4f2010-11-06 17:41:51 -0500733static void free_expdef_table(struct hash_table *edt)
H. Peter Anvin072771e2008-05-22 13:17:51 -0700734{
Keith Kaniosb307a4f2010-11-06 17:41:51 -0500735 ExpDef *ed, *tmp;
H. Peter Anvin072771e2008-05-22 13:17:51 -0700736 const char *key;
737 struct hash_tbl_node *it = NULL;
H. Peter Anvin97a23472007-09-16 17:57:25 -0700738
739 it = NULL;
Keith Kaniosb307a4f2010-11-06 17:41:51 -0500740 while ((ed = hash_iterate(edt, &it, &key)) != NULL) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300741 nasm_free((void *)key);
Keith Kaniosb307a4f2010-11-06 17:41:51 -0500742 list_for_each_safe(ed ,tmp, ed)
743 free_expdef(ed);
H. Peter Anvin97a23472007-09-16 17:57:25 -0700744 }
Keith Kaniosb307a4f2010-11-06 17:41:51 -0500745 hash_free(edt);
H. Peter Anvin072771e2008-05-22 13:17:51 -0700746}
747
748static void free_macros(void)
749{
H. Peter Anvin166c2472008-05-28 12:28:58 -0700750 free_smacro_table(&smacros);
Keith Kaniosb307a4f2010-11-06 17:41:51 -0500751 free_expdef_table(&expdefs);
H. Peter Anvin97a23472007-09-16 17:57:25 -0700752}
753
754/*
755 * Initialize the hash tables
756 */
757static void init_macros(void)
758{
H. Peter Anvin166c2472008-05-28 12:28:58 -0700759 hash_init(&smacros, HASH_LARGE);
Keith Kaniosb307a4f2010-11-06 17:41:51 -0500760 hash_init(&expdefs, HASH_LARGE);
H. Peter Anvin97a23472007-09-16 17:57:25 -0700761}
762
763/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000764 * Pop the context stack.
765 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000766static void ctx_pop(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000767{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000768 Context *c = cstk;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000769
770 cstk = cstk->next;
H. Peter Anvin166c2472008-05-28 12:28:58 -0700771 free_smacro_table(&c->localmac);
H. Peter Anvin734b1882002-04-30 21:01:08 +0000772 nasm_free(c->name);
773 nasm_free(c);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000774}
775
H. Peter Anvin072771e2008-05-22 13:17:51 -0700776/*
777 * Search for a key in the hash index; adding it if necessary
778 * (in which case we initialize the data pointer to NULL.)
779 */
780static void **
781hash_findi_add(struct hash_table *hash, const char *str)
782{
783 struct hash_insert hi;
784 void **r;
785 char *strx;
786
787 r = hash_findi(hash, str, &hi);
788 if (r)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300789 return r;
H. Peter Anvin072771e2008-05-22 13:17:51 -0700790
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300791 strx = nasm_strdup(str); /* Use a more efficient allocator here? */
H. Peter Anvin072771e2008-05-22 13:17:51 -0700792 return hash_add(&hi, strx, NULL);
793}
794
795/*
796 * Like hash_findi, but returns the data element rather than a pointer
797 * to it. Used only when not adding a new element, hence no third
798 * argument.
799 */
800static void *
801hash_findix(struct hash_table *hash, const char *str)
802{
803 void **p;
804
805 p = hash_findi(hash, str, NULL);
806 return p ? *p : NULL;
807}
808
Cyrill Gorcunov15bdc512010-07-13 11:27:41 +0400809/*
Keith Kaniosb307a4f2010-11-06 17:41:51 -0500810 * read line from standard macros set,
Cyrill Gorcunov15bdc512010-07-13 11:27:41 +0400811 * if there no more left -- return NULL
812 */
813static char *line_from_stdmac(void)
814{
815 unsigned char c;
816 const unsigned char *p = stdmacpos;
817 char *line, *q;
818 size_t len = 0;
819
820 if (!stdmacpos)
821 return NULL;
822
823 while ((c = *p++)) {
824 if (c >= 0x80)
825 len += pp_directives_len[c - 0x80] + 1;
826 else
827 len++;
828 }
829
830 line = nasm_malloc(len + 1);
831 q = line;
832 while ((c = *stdmacpos++)) {
833 if (c >= 0x80) {
834 memcpy(q, pp_directives[c - 0x80], pp_directives_len[c - 0x80]);
835 q += pp_directives_len[c - 0x80];
836 *q++ = ' ';
837 } else {
838 *q++ = c;
839 }
840 }
841 stdmacpos = p;
842 *q = '\0';
843
844 if (!*stdmacpos) {
845 /* This was the last of the standard macro chain... */
846 stdmacpos = NULL;
847 if (any_extrastdmac) {
848 stdmacpos = extrastdmac;
849 any_extrastdmac = false;
850 } else if (do_predef) {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +0300851 ExpInv *ei;
Cyrill Gorcunov15bdc512010-07-13 11:27:41 +0400852 Line *pd, *l;
853 Token *head, **tail, *t;
854
855 /*
856 * Nasty hack: here we push the contents of
857 * `predef' on to the top-level expansion stack,
858 * since this is the most convenient way to
859 * implement the pre-include and pre-define
860 * features.
861 */
862 list_for_each(pd, predef) {
863 head = NULL;
864 tail = &head;
865 list_for_each(t, pd->first) {
866 *tail = new_Token(NULL, t->type, t->text, 0);
867 tail = &(*tail)->next;
868 }
869
Keith Kaniosb307a4f2010-11-06 17:41:51 -0500870 l = new_Line();
871 l->first = head;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +0300872 ei = new_ExpInv(EXP_PREDEF, NULL);
873 ei->current = l;
874 ei->emitting = true;
875 ei->prev = istk->expansion;
Keith Kaniosb307a4f2010-11-06 17:41:51 -0500876 istk->expansion = ei;
Cyrill Gorcunov15bdc512010-07-13 11:27:41 +0400877 }
878 do_predef = false;
879 }
880 }
881
882 return line;
883}
884
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000885#define BUF_DELTA 512
886/*
887 * Read a line from the top file in istk, handling multiple CR/LFs
888 * at the end of the line read, and handling spurious ^Zs. Will
889 * return lines from the standard macro set if this has not already
890 * been done.
891 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000892static char *read_line(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000893{
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000894 char *buffer, *p, *q;
H. Peter Anvin9f394642002-04-30 21:07:51 +0000895 int bufsize, continued_count;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000896
Cyrill Gorcunov15bdc512010-07-13 11:27:41 +0400897 /*
898 * standart macros set (predefined) goes first
899 */
900 p = line_from_stdmac();
901 if (p)
902 return p;
H. Peter Anvin72edbb82008-06-19 16:00:04 -0700903
Cyrill Gorcunov15bdc512010-07-13 11:27:41 +0400904 /*
905 * regular read from a file
906 */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000907 bufsize = BUF_DELTA;
908 buffer = nasm_malloc(BUF_DELTA);
909 p = buffer;
H. Peter Anvin9f394642002-04-30 21:07:51 +0000910 continued_count = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000911 while (1) {
912 q = fgets(p, bufsize - (p - buffer), istk->fp);
913 if (!q)
914 break;
915 p += strlen(p);
916 if (p > buffer && p[-1] == '\n') {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300917 /*
918 * Convert backslash-CRLF line continuation sequences into
919 * nothing at all (for DOS and Windows)
920 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000921 if (((p - 2) > buffer) && (p[-3] == '\\') && (p[-2] == '\r')) {
922 p -= 3;
923 *p = 0;
924 continued_count++;
925 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300926 /*
927 * Also convert backslash-LF line continuation sequences into
928 * nothing at all (for Unix)
929 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000930 else if (((p - 1) > buffer) && (p[-2] == '\\')) {
931 p -= 2;
932 *p = 0;
933 continued_count++;
934 } else {
935 break;
936 }
937 }
938 if (p - buffer > bufsize - 10) {
Keith Kaniosb7a89542007-04-12 02:40:54 +0000939 int32_t offset = p - buffer;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000940 bufsize += BUF_DELTA;
941 buffer = nasm_realloc(buffer, bufsize);
942 p = buffer + offset; /* prevent stale-pointer problems */
943 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000944 }
945
H. Peter Anvine2c80182005-01-15 22:15:51 +0000946 if (!q && p == buffer) {
947 nasm_free(buffer);
948 return NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000949 }
950
Cyrill Gorcunova5aea572010-11-11 10:14:45 +0300951 src_set_linnum(src_get_linnum() + istk->lineinc +
952 (continued_count * istk->lineinc));
H. Peter Anvineba20a72002-04-30 20:53:55 +0000953
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000954 /*
955 * Play safe: remove CRs as well as LFs, if any of either are
956 * present at the end of the line.
957 */
H. Peter Anvineba20a72002-04-30 20:53:55 +0000958 while (--p >= buffer && (*p == '\n' || *p == '\r'))
H. Peter Anvine2c80182005-01-15 22:15:51 +0000959 *p = '\0';
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000960
961 /*
962 * Handle spurious ^Z, which may be inserted into source files
963 * by some file transfer utilities.
964 */
965 buffer[strcspn(buffer, "\032")] = '\0';
966
H. Peter Anvin734b1882002-04-30 21:01:08 +0000967 list->line(LIST_READ, buffer);
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000968
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000969 return buffer;
970}
971
972/*
Keith Kaniosb7a89542007-04-12 02:40:54 +0000973 * Tokenize a line of text. This is a very simple process since we
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000974 * don't need to parse the value out of e.g. numeric tokens: we
975 * simply split one string into many.
976 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000977static Token *tokenize(char *line)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000978{
H. Peter Anvinca544db2008-10-19 19:30:11 -0700979 char c, *p = line;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +0000980 enum pp_token_type type;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000981 Token *list = NULL;
982 Token *t, **tail = &list;
Keith Kanios6faad4e2010-12-18 14:08:02 -0600983 bool verbose = true;
Cyrill Gorcunov82667ff2011-06-25 19:34:19 +0400984
Cyrill Gorcunovfc0c1282011-06-25 19:51:44 +0400985 nasm_trace("Tokenize for '%s'", line);
986
Keith Kanios6faad4e2010-12-18 14:08:02 -0600987 if ((defining != NULL) && (defining->ignoring == true)) {
988 verbose = false;
989 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000990
H. Peter Anvine2c80182005-01-15 22:15:51 +0000991 while (*line) {
992 p = line;
993 if (*p == '%') {
994 p++;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300995 if (*p == '+' && !nasm_isdigit(p[1])) {
996 p++;
997 type = TOK_PASTE;
998 } else if (nasm_isdigit(*p) ||
999 ((*p == '-' || *p == '+') && nasm_isdigit(p[1]))) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001000 do {
1001 p++;
1002 }
H. Peter Anvinbda7a6e2008-06-21 10:23:17 -07001003 while (nasm_isdigit(*p));
H. Peter Anvine2c80182005-01-15 22:15:51 +00001004 type = TOK_PREPROC_ID;
1005 } else if (*p == '{') {
1006 p++;
Keith Kaniosb307a4f2010-11-06 17:41:51 -05001007 while (*p && *p != '}') {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001008 p[-1] = *p;
1009 p++;
1010 }
1011 p[-1] = '\0';
1012 if (*p)
1013 p++;
1014 type = TOK_PREPROC_ID;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001015 } else if (*p == '[') {
1016 int lvl = 1;
1017 line += 2; /* Skip the leading %[ */
1018 p++;
1019 while (lvl && (c = *p++)) {
1020 switch (c) {
1021 case ']':
1022 lvl--;
1023 break;
1024 case '%':
1025 if (*p == '[')
1026 lvl++;
1027 break;
1028 case '\'':
1029 case '\"':
1030 case '`':
Cyrill Gorcunovc6360a72010-07-13 13:32:19 +04001031 p = nasm_skip_string(p - 1) + 1;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001032 break;
1033 default:
1034 break;
1035 }
1036 }
1037 p--;
1038 if (*p)
1039 *p++ = '\0';
Keith Kanios6faad4e2010-12-18 14:08:02 -06001040 if (lvl && verbose)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001041 error(ERR_NONFATAL, "unterminated %[ construct");
1042 type = TOK_INDIRECT;
1043 } else if (*p == '?') {
1044 type = TOK_PREPROC_Q; /* %? */
1045 p++;
1046 if (*p == '?') {
1047 type = TOK_PREPROC_QQ; /* %?? */
1048 p++;
1049 }
Cyrill Gorcunov84b4cba2010-09-12 21:39:40 +04001050 } else if (*p == '!') {
1051 type = TOK_PREPROC_ID;
1052 p++;
1053 if (isidchar(*p)) {
1054 do {
1055 p++;
1056 } while (isidchar(*p));
1057 } else if (*p == '\'' || *p == '\"' || *p == '`') {
1058 p = nasm_skip_string(p);
1059 if (*p)
1060 p++;
Keith Kanios6faad4e2010-12-18 14:08:02 -06001061 else if(verbose)
Cyrill Gorcunov84b4cba2010-09-12 21:39:40 +04001062 error(ERR_NONFATAL|ERR_PASS1, "unterminated %! string");
1063 } else {
1064 /* %! without string or identifier */
1065 type = TOK_OTHER; /* Legacy behavior... */
1066 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001067 } else if (isidchar(*p) ||
1068 ((*p == '!' || *p == '%' || *p == '$') &&
1069 isidchar(p[1]))) {
1070 do {
1071 p++;
1072 }
1073 while (isidchar(*p));
1074 type = TOK_PREPROC_ID;
1075 } else {
1076 type = TOK_OTHER;
1077 if (*p == '%')
1078 p++;
1079 }
1080 } else if (isidstart(*p) || (*p == '$' && isidstart(p[1]))) {
1081 type = TOK_ID;
1082 p++;
1083 while (*p && isidchar(*p))
1084 p++;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07001085 } else if (*p == '\'' || *p == '"' || *p == '`') {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001086 /*
1087 * A string token.
1088 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001089 type = TOK_STRING;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001090 p = nasm_skip_string(p);
Nickolay Yurchenkof3b3ce22003-09-21 20:38:43 +00001091
H. Peter Anvine2c80182005-01-15 22:15:51 +00001092 if (*p) {
1093 p++;
Keith Kanios6faad4e2010-12-18 14:08:02 -06001094 } else if(verbose) {
H. Peter Anvin917a3492008-09-24 09:14:49 -07001095 error(ERR_WARNING|ERR_PASS1, "unterminated string");
H. Peter Anvine2c80182005-01-15 22:15:51 +00001096 /* Handling unterminated strings by UNV */
1097 /* type = -1; */
1098 }
Victor van den Elzenfb5f2512009-04-17 16:17:59 +02001099 } else if (p[0] == '$' && p[1] == '$') {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001100 type = TOK_OTHER; /* TOKEN_BASE */
Victor van den Elzenfb5f2512009-04-17 16:17:59 +02001101 p += 2;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001102 } else if (isnumstart(*p)) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001103 bool is_hex = false;
1104 bool is_float = false;
1105 bool has_e = false;
1106 char c, *r;
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001107
H. Peter Anvine2c80182005-01-15 22:15:51 +00001108 /*
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001109 * A numeric token.
H. Peter Anvine2c80182005-01-15 22:15:51 +00001110 */
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001111
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001112 if (*p == '$') {
1113 p++;
1114 is_hex = true;
1115 }
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001116
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001117 for (;;) {
1118 c = *p++;
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001119
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001120 if (!is_hex && (c == 'e' || c == 'E')) {
1121 has_e = true;
1122 if (*p == '+' || *p == '-') {
1123 /*
1124 * e can only be followed by +/- if it is either a
1125 * prefixed hex number or a floating-point number
1126 */
1127 p++;
1128 is_float = true;
1129 }
1130 } else if (c == 'H' || c == 'h' || c == 'X' || c == 'x') {
1131 is_hex = true;
1132 } else if (c == 'P' || c == 'p') {
1133 is_float = true;
1134 if (*p == '+' || *p == '-')
1135 p++;
1136 } else if (isnumchar(c) || c == '_')
1137 ; /* just advance */
1138 else if (c == '.') {
1139 /*
1140 * we need to deal with consequences of the legacy
1141 * parser, like "1.nolist" being two tokens
1142 * (TOK_NUMBER, TOK_ID) here; at least give it
1143 * a shot for now. In the future, we probably need
1144 * a flex-based scanner with proper pattern matching
1145 * to do it as well as it can be done. Nothing in
1146 * the world is going to help the person who wants
1147 * 0x123.p16 interpreted as two tokens, though.
1148 */
1149 r = p;
1150 while (*r == '_')
1151 r++;
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001152
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001153 if (nasm_isdigit(*r) || (is_hex && nasm_isxdigit(*r)) ||
1154 (!is_hex && (*r == 'e' || *r == 'E')) ||
1155 (*r == 'p' || *r == 'P')) {
1156 p = r;
1157 is_float = true;
1158 } else
1159 break; /* Terminate the token */
1160 } else
1161 break;
1162 }
1163 p--; /* Point to first character beyond number */
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001164
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001165 if (p == line+1 && *line == '$') {
1166 type = TOK_OTHER; /* TOKEN_HERE */
1167 } else {
1168 if (has_e && !is_hex) {
1169 /* 1e13 is floating-point, but 1e13h is not */
1170 is_float = true;
1171 }
H. Peter Anvind784a082009-04-20 14:01:18 -07001172
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001173 type = is_float ? TOK_FLOAT : TOK_NUMBER;
1174 }
H. Peter Anvinbda7a6e2008-06-21 10:23:17 -07001175 } else if (nasm_isspace(*p)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001176 type = TOK_WHITESPACE;
Cyrill Gorcunovf66ac7d2009-10-12 20:41:13 +04001177 p = nasm_skip_spaces(p);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001178 /*
1179 * Whitespace just before end-of-line is discarded by
1180 * pretending it's a comment; whitespace just before a
1181 * comment gets lumped into the comment.
1182 */
1183 if (!*p || *p == ';') {
1184 type = TOK_COMMENT;
1185 while (*p)
1186 p++;
1187 }
1188 } else if (*p == ';') {
1189 type = TOK_COMMENT;
1190 while (*p)
1191 p++;
1192 } else {
1193 /*
1194 * Anything else is an operator of some kind. We check
1195 * for all the double-character operators (>>, <<, //,
1196 * %%, <=, >=, ==, !=, <>, &&, ||, ^^), but anything
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001197 * else is a single-character operator.
H. Peter Anvine2c80182005-01-15 22:15:51 +00001198 */
1199 type = TOK_OTHER;
1200 if ((p[0] == '>' && p[1] == '>') ||
1201 (p[0] == '<' && p[1] == '<') ||
1202 (p[0] == '/' && p[1] == '/') ||
1203 (p[0] == '<' && p[1] == '=') ||
1204 (p[0] == '>' && p[1] == '=') ||
1205 (p[0] == '=' && p[1] == '=') ||
1206 (p[0] == '!' && p[1] == '=') ||
1207 (p[0] == '<' && p[1] == '>') ||
1208 (p[0] == '&' && p[1] == '&') ||
1209 (p[0] == '|' && p[1] == '|') ||
1210 (p[0] == '^' && p[1] == '^')) {
1211 p++;
1212 }
1213 p++;
1214 }
Nickolay Yurchenkof3b3ce22003-09-21 20:38:43 +00001215
H. Peter Anvine2c80182005-01-15 22:15:51 +00001216 /* Handling unterminated string by UNV */
1217 /*if (type == -1)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001218 {
1219 *tail = t = new_Token(NULL, TOK_STRING, line, p-line+1);
1220 t->text[p-line] = *line;
1221 tail = &t->next;
1222 }
1223 else */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001224 if (type != TOK_COMMENT) {
1225 *tail = t = new_Token(NULL, type, line, p - line);
1226 tail = &t->next;
1227 }
1228 line = p;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001229 }
Cyrill Gorcunovfc0c1282011-06-25 19:51:44 +04001230
1231 nasm_dump_token(list);
1232
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001233 return list;
1234}
1235
H. Peter Anvince616072002-04-30 21:02:23 +00001236/*
1237 * this function allocates a new managed block of memory and
H. Peter Anvin70653092007-10-19 14:42:29 -07001238 * returns a pointer to the block. The managed blocks are
H. Peter Anvince616072002-04-30 21:02:23 +00001239 * deleted only all at once by the delete_Blocks function.
1240 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001241static void *new_Block(size_t size)
H. Peter Anvince616072002-04-30 21:02:23 +00001242{
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001243 Blocks *b = &blocks;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001244
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001245 /* first, get to the end of the linked list */
1246 while (b->next)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001247 b = b->next;
Cyrill Gorcunov6b271292011-02-28 08:45:52 +03001248
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001249 /* now allocate the requested chunk */
1250 b->chunk = nasm_malloc(size);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001251
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001252 /* now allocate a new block for the next request */
Cyrill Gorcunov6b271292011-02-28 08:45:52 +03001253 b->next = nasm_zalloc(sizeof(Blocks));
1254
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001255 return b->chunk;
H. Peter Anvince616072002-04-30 21:02:23 +00001256}
1257
1258/*
1259 * this function deletes all managed blocks of memory
1260 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001261static void delete_Blocks(void)
H. Peter Anvince616072002-04-30 21:02:23 +00001262{
H. Peter Anvine2c80182005-01-15 22:15:51 +00001263 Blocks *a, *b = &blocks;
H. Peter Anvince616072002-04-30 21:02:23 +00001264
H. Peter Anvin70653092007-10-19 14:42:29 -07001265 /*
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001266 * keep in mind that the first block, pointed to by blocks
H. Peter Anvin70653092007-10-19 14:42:29 -07001267 * is a static and not dynamically allocated, so we don't
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001268 * free it.
1269 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001270 while (b) {
1271 if (b->chunk)
1272 nasm_free(b->chunk);
1273 a = b;
1274 b = b->next;
1275 if (a != &blocks)
1276 nasm_free(a);
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001277 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001278}
H. Peter Anvin734b1882002-04-30 21:01:08 +00001279
1280/*
H. Peter Anvin70653092007-10-19 14:42:29 -07001281 * this function creates a new Token and passes a pointer to it
H. Peter Anvin734b1882002-04-30 21:01:08 +00001282 * back to the caller. It sets the type and text elements, and
H. Peter Anvinf26e0972008-07-01 21:26:27 -07001283 * also the a.mac and next elements to NULL.
H. Peter Anvin734b1882002-04-30 21:01:08 +00001284 */
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07001285static Token *new_Token(Token * next, enum pp_token_type type,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001286 const char *text, int txtlen)
H. Peter Anvin734b1882002-04-30 21:01:08 +00001287{
1288 Token *t;
1289 int i;
1290
H. Peter Anvin89cee572009-07-15 09:16:54 -04001291 if (!freeTokens) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001292 freeTokens = (Token *) new_Block(TOKEN_BLOCKSIZE * sizeof(Token));
1293 for (i = 0; i < TOKEN_BLOCKSIZE - 1; i++)
1294 freeTokens[i].next = &freeTokens[i + 1];
1295 freeTokens[i].next = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001296 }
1297 t = freeTokens;
1298 freeTokens = t->next;
1299 t->next = next;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07001300 t->a.mac = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001301 t->type = type;
H. Peter Anvin89cee572009-07-15 09:16:54 -04001302 if (type == TOK_WHITESPACE || !text) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001303 t->text = NULL;
1304 } else {
1305 if (txtlen == 0)
1306 txtlen = strlen(text);
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07001307 t->text = nasm_malloc(txtlen+1);
1308 memcpy(t->text, text, txtlen);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001309 t->text[txtlen] = '\0';
H. Peter Anvin734b1882002-04-30 21:01:08 +00001310 }
1311 return t;
1312}
1313
Keith Kaniosb307a4f2010-11-06 17:41:51 -05001314static Token *copy_Token(Token * tline)
1315{
1316 Token *t, *tt, *first = NULL, *prev = NULL;
1317 int i;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03001318 for (tt = tline; tt != NULL; tt = tt->next) {
1319 if (!freeTokens) {
1320 freeTokens = (Token *) new_Block(TOKEN_BLOCKSIZE * sizeof(Token));
1321 for (i = 0; i < TOKEN_BLOCKSIZE - 1; i++)
1322 freeTokens[i].next = &freeTokens[i + 1];
1323 freeTokens[i].next = NULL;
1324 }
1325 t = freeTokens;
1326 freeTokens = t->next;
1327 t->next = NULL;
1328 t->text = tt->text ? nasm_strdup(tt->text) : NULL;
1329 t->a.mac = tt->a.mac;
1330 t->a.len = tt->a.len;
1331 t->type = tt->type;
1332 if (prev != NULL) {
1333 prev->next = t;
1334 } else {
1335 first = t;
1336 }
1337 prev = t;
1338 }
Keith Kaniosb307a4f2010-11-06 17:41:51 -05001339 return first;
1340}
1341
H. Peter Anvine2c80182005-01-15 22:15:51 +00001342static Token *delete_Token(Token * t)
H. Peter Anvin734b1882002-04-30 21:01:08 +00001343{
1344 Token *next = t->next;
1345 nasm_free(t->text);
H. Peter Anvin788e6c12002-04-30 21:02:01 +00001346 t->next = freeTokens;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001347 freeTokens = t;
1348 return next;
1349}
1350
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001351/*
1352 * Convert a line of tokens back into text.
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001353 * If expand_locals is not zero, identifiers of the form "%$*xxx"
1354 * will be transformed into ..@ctxnum.xxx
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001355 */
H. Peter Anvin9e200162008-06-04 17:23:14 -07001356static char *detoken(Token * tlist, bool expand_locals)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001357{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001358 Token *t;
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001359 char *line, *p;
H. Peter Anvinb4daadc2008-01-21 16:31:57 -08001360 const char *q;
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001361 int len = 0;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001362
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001363 list_for_each(t, tlist) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001364 if (t->type == TOK_PREPROC_ID && t->text[1] == '!') {
Cyrill Gorcunov84b4cba2010-09-12 21:39:40 +04001365 char *v;
1366 char *q = t->text;
H. Peter Anvin077fb932010-07-20 14:56:30 -07001367
Cyrill Gorcunov84b4cba2010-09-12 21:39:40 +04001368 v = t->text + 2;
1369 if (*v == '\'' || *v == '\"' || *v == '`') {
1370 size_t len = nasm_unquote(v, NULL);
1371 size_t clen = strlen(v);
H. Peter Anvin077fb932010-07-20 14:56:30 -07001372
Cyrill Gorcunov84b4cba2010-09-12 21:39:40 +04001373 if (len != clen) {
1374 error(ERR_NONFATAL | ERR_PASS1,
1375 "NUL character in %! string");
1376 v = NULL;
1377 }
1378 }
H. Peter Anvin077fb932010-07-20 14:56:30 -07001379
Cyrill Gorcunov84b4cba2010-09-12 21:39:40 +04001380 if (v) {
1381 char *p = getenv(v);
1382 if (!p) {
1383 error(ERR_NONFATAL | ERR_PASS1,
1384 "nonexistent environment variable `%s'", v);
1385 p = "";
1386 }
1387 t->text = nasm_strdup(p);
1388 }
1389 nasm_free(q);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001390 }
H. Peter Anvin077fb932010-07-20 14:56:30 -07001391
H. Peter Anvine2c80182005-01-15 22:15:51 +00001392 /* Expand local macros here and not during preprocessing */
1393 if (expand_locals &&
1394 t->type == TOK_PREPROC_ID && t->text &&
1395 t->text[0] == '%' && t->text[1] == '$') {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001396 const char *q;
1397 char *p;
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001398 Context *ctx = get_ctx(t->text, &q, false);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001399 if (ctx) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001400 char buffer[40];
Keith Kanios93f2e9a2007-04-14 00:10:59 +00001401 snprintf(buffer, sizeof(buffer), "..@%"PRIu32".", ctx->number);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001402 p = nasm_strcat(buffer, q);
1403 nasm_free(t->text);
1404 t->text = p;
1405 }
1406 }
Keith Kaniosb307a4f2010-11-06 17:41:51 -05001407
1408 /* Expand %? and %?? directives */
Keith Kanios3136d482010-11-13 09:34:34 -06001409 if ((istk->expansion != NULL) &&
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03001410 ((t->type == TOK_PREPROC_Q) ||
1411 (t->type == TOK_PREPROC_QQ))) {
Keith Kaniosb307a4f2010-11-06 17:41:51 -05001412 ExpInv *ei;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03001413 for (ei = istk->expansion; ei != NULL; ei = ei->prev){
1414 if (ei->type == EXP_MMACRO) {
1415 nasm_free(t->text);
1416 if (t->type == TOK_PREPROC_Q) {
1417 t->text = nasm_strdup(ei->name);
1418 } else {
1419 t->text = nasm_strdup(ei->def->name);
1420 }
1421 break;
1422 }
1423 }
1424 }
Keith Kaniosb307a4f2010-11-06 17:41:51 -05001425
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001426 if (t->type == TOK_WHITESPACE)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001427 len++;
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001428 else if (t->text)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001429 len += strlen(t->text);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001430 }
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001431
H. Peter Anvin734b1882002-04-30 21:01:08 +00001432 p = line = nasm_malloc(len + 1);
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001433
1434 list_for_each(t, tlist) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001435 if (t->type == TOK_WHITESPACE) {
H. Peter Anvinb4daadc2008-01-21 16:31:57 -08001436 *p++ = ' ';
H. Peter Anvine2c80182005-01-15 22:15:51 +00001437 } else if (t->text) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001438 q = t->text;
1439 while (*q)
1440 *p++ = *q++;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001441 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001442 }
1443 *p = '\0';
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001444
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001445 return line;
1446}
1447
1448/*
Keith Kaniosb307a4f2010-11-06 17:41:51 -05001449 * Initialize a new Line
1450 */
Cyrill Gorcunov29cb0bb2010-11-11 11:19:43 +03001451static inline Line *new_Line(void)
Keith Kaniosb307a4f2010-11-06 17:41:51 -05001452{
Cyrill Gorcunov6b271292011-02-28 08:45:52 +03001453 return (Line *)nasm_zalloc(sizeof(Line));
Keith Kaniosb307a4f2010-11-06 17:41:51 -05001454}
1455
1456
1457/*
1458 * Initialize a new Expansion Definition
1459 */
1460static ExpDef *new_ExpDef(int exp_type)
1461{
Cyrill Gorcunovc5157742010-11-11 11:29:40 +03001462 ExpDef *ed = (ExpDef*)nasm_zalloc(sizeof(ExpDef));
1463 ed->type = exp_type;
1464 ed->casesense = true;
1465 ed->state = COND_NEVER;
1466
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03001467 return ed;
Keith Kaniosb307a4f2010-11-06 17:41:51 -05001468}
1469
1470
1471/*
1472 * Initialize a new Expansion Instance
1473 */
1474static ExpInv *new_ExpInv(int exp_type, ExpDef *ed)
1475{
Cyrill Gorcunovc5157742010-11-11 11:29:40 +03001476 ExpInv *ei = (ExpInv*)nasm_zalloc(sizeof(ExpInv));
1477 ei->type = exp_type;
1478 ei->def = ed;
1479 ei->unique = ++unique;
1480
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03001481 if ((istk->mmac_depth < 1) &&
1482 (istk->expansion == NULL) &&
1483 (ed != NULL) &&
1484 (ed->type != EXP_MMACRO) &&
1485 (ed->type != EXP_REP) &&
1486 (ed->type != EXP_WHILE)) {
1487 ei->linnum = src_get_linnum();
1488 src_set_linnum(ei->linnum - ed->linecount - 1);
1489 } else {
1490 ei->linnum = -1;
1491 }
1492 if ((istk->expansion == NULL) ||
1493 (ei->type == EXP_MMACRO)) {
1494 ei->relno = 0;
1495 } else {
1496 ei->relno = istk->expansion->lineno;
1497 if (ed != NULL) {
1498 ei->relno -= (ed->linecount + 1);
1499 }
1500 }
1501 return ei;
Keith Kaniosb307a4f2010-11-06 17:41:51 -05001502}
1503
1504/*
H. Peter Anvin76690a12002-04-30 20:52:49 +00001505 * A scanner, suitable for use by the expression evaluator, which
1506 * operates on a line of Tokens. Expects a pointer to a pointer to
1507 * the first token in the line to be passed in as its private_data
1508 * field.
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001509 *
1510 * FIX: This really needs to be unified with stdscan.
H. Peter Anvin76690a12002-04-30 20:52:49 +00001511 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001512static int ppscan(void *private_data, struct tokenval *tokval)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001513{
H. Peter Anvin76690a12002-04-30 20:52:49 +00001514 Token **tlineptr = private_data;
1515 Token *tline;
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001516 char ourcopy[MAX_KEYWORD+1], *p, *r, *s;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001517
H. Peter Anvine2c80182005-01-15 22:15:51 +00001518 do {
1519 tline = *tlineptr;
1520 *tlineptr = tline ? tline->next : NULL;
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04001521 } while (tline && (tline->type == TOK_WHITESPACE ||
1522 tline->type == TOK_COMMENT));
H. Peter Anvin76690a12002-04-30 20:52:49 +00001523
1524 if (!tline)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001525 return tokval->t_type = TOKEN_EOS;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001526
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001527 tokval->t_charptr = tline->text;
1528
H. Peter Anvin76690a12002-04-30 20:52:49 +00001529 if (tline->text[0] == '$' && !tline->text[1])
H. Peter Anvine2c80182005-01-15 22:15:51 +00001530 return tokval->t_type = TOKEN_HERE;
H. Peter Anvin7cf897e2002-05-30 21:30:33 +00001531 if (tline->text[0] == '$' && tline->text[1] == '$' && !tline->text[2])
H. Peter Anvine2c80182005-01-15 22:15:51 +00001532 return tokval->t_type = TOKEN_BASE;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001533
H. Peter Anvine2c80182005-01-15 22:15:51 +00001534 if (tline->type == TOK_ID) {
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001535 p = tokval->t_charptr = tline->text;
1536 if (p[0] == '$') {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001537 tokval->t_charptr++;
1538 return tokval->t_type = TOKEN_ID;
1539 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00001540
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001541 for (r = p, s = ourcopy; *r; r++) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001542 if (r >= p+MAX_KEYWORD)
1543 return tokval->t_type = TOKEN_ID; /* Not a keyword */
H. Peter Anvinac8f8fc2008-06-11 15:49:41 -07001544 *s++ = nasm_tolower(*r);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001545 }
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001546 *s = '\0';
1547 /* right, so we have an identifier sitting in temp storage. now,
1548 * is it actually a register or instruction name, or what? */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001549 return nasm_token_hash(ourcopy, tokval);
H. Peter Anvin76690a12002-04-30 20:52:49 +00001550 }
1551
H. Peter Anvine2c80182005-01-15 22:15:51 +00001552 if (tline->type == TOK_NUMBER) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001553 bool rn_error;
1554 tokval->t_integer = readnum(tline->text, &rn_error);
1555 tokval->t_charptr = tline->text;
1556 if (rn_error)
1557 return tokval->t_type = TOKEN_ERRNUM;
1558 else
1559 return tokval->t_type = TOKEN_NUM;
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001560 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00001561
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001562 if (tline->type == TOK_FLOAT) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001563 return tokval->t_type = TOKEN_FLOAT;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001564 }
1565
H. Peter Anvine2c80182005-01-15 22:15:51 +00001566 if (tline->type == TOK_STRING) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001567 char bq, *ep;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001568
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001569 bq = tline->text[0];
H. Peter Anvin11627042008-06-09 20:45:19 -07001570 tokval->t_charptr = tline->text;
1571 tokval->t_inttwo = nasm_unquote(tline->text, &ep);
H. Peter Anvind2456592008-06-19 15:04:18 -07001572
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001573 if (ep[0] != bq || ep[1] != '\0')
1574 return tokval->t_type = TOKEN_ERRSTR;
1575 else
1576 return tokval->t_type = TOKEN_STR;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001577 }
1578
H. Peter Anvine2c80182005-01-15 22:15:51 +00001579 if (tline->type == TOK_OTHER) {
1580 if (!strcmp(tline->text, "<<"))
1581 return tokval->t_type = TOKEN_SHL;
1582 if (!strcmp(tline->text, ">>"))
1583 return tokval->t_type = TOKEN_SHR;
1584 if (!strcmp(tline->text, "//"))
1585 return tokval->t_type = TOKEN_SDIV;
1586 if (!strcmp(tline->text, "%%"))
1587 return tokval->t_type = TOKEN_SMOD;
1588 if (!strcmp(tline->text, "=="))
1589 return tokval->t_type = TOKEN_EQ;
1590 if (!strcmp(tline->text, "<>"))
1591 return tokval->t_type = TOKEN_NE;
1592 if (!strcmp(tline->text, "!="))
1593 return tokval->t_type = TOKEN_NE;
1594 if (!strcmp(tline->text, "<="))
1595 return tokval->t_type = TOKEN_LE;
1596 if (!strcmp(tline->text, ">="))
1597 return tokval->t_type = TOKEN_GE;
1598 if (!strcmp(tline->text, "&&"))
1599 return tokval->t_type = TOKEN_DBL_AND;
1600 if (!strcmp(tline->text, "^^"))
1601 return tokval->t_type = TOKEN_DBL_XOR;
1602 if (!strcmp(tline->text, "||"))
1603 return tokval->t_type = TOKEN_DBL_OR;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001604 }
1605
1606 /*
1607 * We have no other options: just return the first character of
1608 * the token text.
1609 */
1610 return tokval->t_type = tline->text[0];
1611}
1612
1613/*
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001614 * Compare a string to the name of an existing macro; this is a
1615 * simple wrapper which calls either strcmp or nasm_stricmp
1616 * depending on the value of the `casesense' parameter.
1617 */
H. Peter Anvin4db5a162007-10-11 13:42:09 -07001618static int mstrcmp(const char *p, const char *q, bool casesense)
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001619{
H. Peter Anvin734b1882002-04-30 21:01:08 +00001620 return casesense ? strcmp(p, q) : nasm_stricmp(p, q);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001621}
1622
1623/*
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07001624 * Compare a string to the name of an existing macro; this is a
1625 * simple wrapper which calls either strcmp or nasm_stricmp
1626 * depending on the value of the `casesense' parameter.
1627 */
1628static int mmemcmp(const char *p, const char *q, size_t l, bool casesense)
1629{
1630 return casesense ? memcmp(p, q, l) : nasm_memicmp(p, q, l);
1631}
1632
1633/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001634 * Return the Context structure associated with a %$ token. Return
1635 * NULL, having _already_ reported an error condition, if the
1636 * context stack isn't deep enough for the supplied number of $
1637 * signs.
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001638 * If all_contexts == true, contexts that enclose current are
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001639 * also scanned for such smacro, until it is found; if not -
1640 * only the context that directly results from the number of $'s
1641 * in variable's name.
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001642 *
1643 * If "namep" is non-NULL, set it to the pointer to the macro name
1644 * tail, i.e. the part beyond %$...
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001645 */
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001646static Context *get_ctx(const char *name, const char **namep,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001647 bool all_contexts)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001648{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001649 Context *ctx;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001650 SMacro *m;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001651 int i;
1652
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001653 if (namep)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001654 *namep = name;
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001655
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001656 if (!name || name[0] != '%' || name[1] != '$')
H. Peter Anvine2c80182005-01-15 22:15:51 +00001657 return NULL;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001658
H. Peter Anvine2c80182005-01-15 22:15:51 +00001659 if (!cstk) {
1660 error(ERR_NONFATAL, "`%s': context stack is empty", name);
1661 return NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001662 }
1663
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001664 name += 2;
1665 ctx = cstk;
1666 i = 0;
1667 while (ctx && *name == '$') {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001668 name++;
1669 i++;
1670 ctx = ctx->next;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001671 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001672 if (!ctx) {
1673 error(ERR_NONFATAL, "`%s': context stack is only"
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001674 " %d level%s deep", name, i, (i == 1 ? "" : "s"));
H. Peter Anvine2c80182005-01-15 22:15:51 +00001675 return NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001676 }
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001677
1678 if (namep)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001679 *namep = name;
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001680
Keith Kanios404589e2010-08-10 20:12:57 -05001681 if (!all_contexts)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001682 return ctx;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001683
Keith Kaniosb307a4f2010-11-06 17:41:51 -05001684 do {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001685 /* Search for this smacro in found context */
H. Peter Anvin166c2472008-05-28 12:28:58 -07001686 m = hash_findix(&ctx->localmac, name);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001687 while (m) {
Keith Kaniosb307a4f2010-11-06 17:41:51 -05001688 if (!mstrcmp(m->name, name, m->casesense))
H. Peter Anvine2c80182005-01-15 22:15:51 +00001689 return ctx;
1690 m = m->next;
1691 }
Keith Kaniosb307a4f2010-11-06 17:41:51 -05001692 ctx = ctx->next;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001693 }
Keith Kaniosb307a4f2010-11-06 17:41:51 -05001694 while (ctx);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001695 return NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001696}
1697
1698/*
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001699 * Check to see if a file is already in a string list
1700 */
1701static bool in_list(const StrList *list, const char *str)
1702{
1703 while (list) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001704 if (!strcmp(list->str, str))
1705 return true;
1706 list = list->next;
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001707 }
1708 return false;
1709}
1710
1711/*
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001712 * Open an include file. This routine must always return a valid
1713 * file pointer if it returns - it's responsible for throwing an
1714 * ERR_FATAL and bombing out completely if not. It should also try
1715 * the include path one by one until it finds the file or reaches
1716 * the end of the path.
1717 */
H. Peter Anvin2b1c3b92008-06-06 10:38:46 -07001718static FILE *inc_fopen(const char *file, StrList **dhead, StrList ***dtail,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001719 bool missing_ok)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001720{
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001721 FILE *fp;
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001722 char *prefix = "";
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001723 IncPath *ip = ipath;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001724 int len = strlen(file);
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001725 size_t prefix_len = 0;
1726 StrList *sl;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001727
H. Peter Anvine2c80182005-01-15 22:15:51 +00001728 while (1) {
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001729 sl = nasm_malloc(prefix_len+len+1+sizeof sl->next);
Cyrill Gorcunov6f38fe62010-11-11 11:32:16 +03001730 sl->next = NULL;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001731 memcpy(sl->str, prefix, prefix_len);
1732 memcpy(sl->str+prefix_len, file, len+1);
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001733 fp = fopen(sl->str, "r");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001734 if (fp && dhead && !in_list(*dhead, sl->str)) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001735 **dtail = sl;
1736 *dtail = &sl->next;
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001737 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001738 nasm_free(sl);
1739 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001740 if (fp)
1741 return fp;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001742 if (!ip) {
1743 if (!missing_ok)
1744 break;
1745 prefix = NULL;
1746 } else {
1747 prefix = ip->path;
1748 ip = ip->next;
1749 }
1750 if (prefix) {
1751 prefix_len = strlen(prefix);
1752 } else {
1753 /* -MG given and file not found */
1754 if (dhead && !in_list(*dhead, file)) {
1755 sl = nasm_malloc(len+1+sizeof sl->next);
1756 sl->next = NULL;
1757 strcpy(sl->str, file);
1758 **dtail = sl;
1759 *dtail = &sl->next;
1760 }
1761 return NULL;
1762 }
H. Peter Anvineba20a72002-04-30 20:53:55 +00001763 }
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001764
H. Peter Anvin734b1882002-04-30 21:01:08 +00001765 error(ERR_FATAL, "unable to open include file `%s'", file);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001766 return NULL;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001767}
1768
1769/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001770 * Determine if we should warn on defining a single-line macro of
H. Peter Anvinef7468f2002-04-30 20:57:59 +00001771 * name `name', with `nparam' parameters. If nparam is 0 or -1, will
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001772 * return true if _any_ single-line macro of that name is defined.
1773 * Otherwise, will return true if a single-line macro with either
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001774 * `nparam' or no parameters is defined.
1775 *
1776 * If a macro with precisely the right number of parameters is
H. Peter Anvinef7468f2002-04-30 20:57:59 +00001777 * defined, or nparam is -1, the address of the definition structure
1778 * will be returned in `defn'; otherwise NULL will be returned. If `defn'
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001779 * is NULL, no action will be taken regarding its contents, and no
1780 * error will occur.
1781 *
1782 * Note that this is also called with nparam zero to resolve
1783 * `ifdef'.
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001784 *
1785 * If you already know which context macro belongs to, you can pass
1786 * the context pointer as first parameter; if you won't but name begins
1787 * with %$ the context will be automatically computed. If all_contexts
1788 * is true, macro will be searched in outer contexts as well.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001789 */
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07001790static bool
H. Peter Anvinb2a5fda2008-06-19 21:42:42 -07001791smacro_defined(Context * ctx, const char *name, int nparam, SMacro ** defn,
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07001792 bool nocase)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001793{
H. Peter Anvin166c2472008-05-28 12:28:58 -07001794 struct hash_table *smtbl;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001795 SMacro *m;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001796
H. Peter Anvin97a23472007-09-16 17:57:25 -07001797 if (ctx) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001798 smtbl = &ctx->localmac;
H. Peter Anvin97a23472007-09-16 17:57:25 -07001799 } else if (name[0] == '%' && name[1] == '$') {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001800 if (cstk)
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001801 ctx = get_ctx(name, &name, false);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001802 if (!ctx)
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001803 return false; /* got to return _something_ */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001804 smtbl = &ctx->localmac;
H. Peter Anvin97a23472007-09-16 17:57:25 -07001805 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001806 smtbl = &smacros;
H. Peter Anvin97a23472007-09-16 17:57:25 -07001807 }
H. Peter Anvin166c2472008-05-28 12:28:58 -07001808 m = (SMacro *) hash_findix(smtbl, name);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001809
H. Peter Anvine2c80182005-01-15 22:15:51 +00001810 while (m) {
1811 if (!mstrcmp(m->name, name, m->casesense && nocase) &&
Charles Crayne192d5b52007-10-18 19:02:42 -07001812 (nparam <= 0 || m->nparam == 0 || nparam == (int) m->nparam)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001813 if (defn) {
Charles Crayne192d5b52007-10-18 19:02:42 -07001814 if (nparam == (int) m->nparam || nparam == -1)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001815 *defn = m;
1816 else
1817 *defn = NULL;
1818 }
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001819 return true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001820 }
1821 m = m->next;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001822 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001823
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001824 return false;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001825}
1826
1827/*
1828 * Count and mark off the parameters in a multi-line macro call.
1829 * This is called both from within the multi-line macro expansion
1830 * code, and also to mark off the default parameters when provided
1831 * in a %macro definition line.
1832 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001833static void count_mmac_params(Token * t, int *nparam, Token *** params)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001834{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001835 int paramsize, brace;
1836
1837 *nparam = paramsize = 0;
1838 *params = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001839 while (t) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001840 /* +1: we need space for the final NULL */
H. Peter Anvin91fb6f12008-09-01 10:56:33 -07001841 if (*nparam+1 >= paramsize) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001842 paramsize += PARAM_DELTA;
1843 *params = nasm_realloc(*params, sizeof(**params) * paramsize);
1844 }
1845 skip_white_(t);
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001846 brace = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001847 if (tok_is_(t, "{"))
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001848 brace = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001849 (*params)[(*nparam)++] = t;
1850 while (tok_isnt_(t, brace ? "}" : ","))
1851 t = t->next;
1852 if (t) { /* got a comma/brace */
1853 t = t->next;
1854 if (brace) {
1855 /*
1856 * Now we've found the closing brace, look further
1857 * for the comma.
1858 */
1859 skip_white_(t);
1860 if (tok_isnt_(t, ",")) {
1861 error(ERR_NONFATAL,
1862 "braces do not enclose all of macro parameter");
1863 while (tok_isnt_(t, ","))
1864 t = t->next;
1865 }
1866 if (t)
1867 t = t->next; /* eat the comma */
1868 }
1869 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001870 }
1871}
1872
1873/*
H. Peter Anvin76690a12002-04-30 20:52:49 +00001874 * Determine whether one of the various `if' conditions is true or
1875 * not.
1876 *
1877 * We must free the tline we get passed.
1878 */
H. Peter Anvin70055962007-10-11 00:05:31 -07001879static bool if_condition(Token * tline, enum preproc_token ct)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001880{
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001881 enum pp_conditional i = PP_COND(ct);
H. Peter Anvin70055962007-10-11 00:05:31 -07001882 bool j;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001883 Token *t, *tt, **tptr, *origline;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001884 struct tokenval tokval;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001885 expr *evalresult;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001886 enum pp_token_type needtype;
H. Peter Anvin077fb932010-07-20 14:56:30 -07001887 char *p;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001888
1889 origline = tline;
1890
H. Peter Anvine2c80182005-01-15 22:15:51 +00001891 switch (i) {
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001892 case PPC_IFCTX:
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001893 j = false; /* have we matched yet? */
Victor van den Elzen0e857f12008-07-23 13:21:29 +02001894 while (true) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001895 skip_white_(tline);
Victor van den Elzen0e857f12008-07-23 13:21:29 +02001896 if (!tline)
1897 break;
1898 if (tline->type != TOK_ID) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001899 error(ERR_NONFATAL,
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001900 "`%s' expects context identifiers", pp_directives[ct]);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001901 free_tlist(origline);
1902 return -1;
1903 }
Victor van den Elzen0e857f12008-07-23 13:21:29 +02001904 if (cstk && cstk->name && !nasm_stricmp(tline->text, cstk->name))
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001905 j = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001906 tline = tline->next;
1907 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001908 break;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001909
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001910 case PPC_IFDEF:
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001911 j = false; /* have we matched yet? */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001912 while (tline) {
1913 skip_white_(tline);
1914 if (!tline || (tline->type != TOK_ID &&
1915 (tline->type != TOK_PREPROC_ID ||
1916 tline->text[1] != '$'))) {
1917 error(ERR_NONFATAL,
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001918 "`%s' expects macro identifiers", pp_directives[ct]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001919 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001920 }
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07001921 if (smacro_defined(NULL, tline->text, 0, NULL, true))
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001922 j = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001923 tline = tline->next;
1924 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001925 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001926
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001927 case PPC_IFENV:
Cyrill Gorcunov84b4cba2010-09-12 21:39:40 +04001928 tline = expand_smacro(tline);
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001929 j = false; /* have we matched yet? */
1930 while (tline) {
1931 skip_white_(tline);
1932 if (!tline || (tline->type != TOK_ID &&
Cyrill Gorcunov84b4cba2010-09-12 21:39:40 +04001933 tline->type != TOK_STRING &&
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001934 (tline->type != TOK_PREPROC_ID ||
Cyrill Gorcunov84b4cba2010-09-12 21:39:40 +04001935 tline->text[1] != '!'))) {
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001936 error(ERR_NONFATAL,
1937 "`%s' expects environment variable names",
Cyrill Gorcunov84b4cba2010-09-12 21:39:40 +04001938 pp_directives[ct]);
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001939 goto fail;
1940 }
Cyrill Gorcunov84b4cba2010-09-12 21:39:40 +04001941 p = tline->text;
1942 if (tline->type == TOK_PREPROC_ID)
1943 p += 2; /* Skip leading %! */
1944 if (*p == '\'' || *p == '\"' || *p == '`')
1945 nasm_unquote_cstr(p, ct);
1946 if (getenv(p))
1947 j = true;
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001948 tline = tline->next;
1949 }
1950 break;
1951
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001952 case PPC_IFIDN:
1953 case PPC_IFIDNI:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001954 tline = expand_smacro(tline);
1955 t = tt = tline;
1956 while (tok_isnt_(tt, ","))
1957 tt = tt->next;
1958 if (!tt) {
1959 error(ERR_NONFATAL,
1960 "`%s' expects two comma-separated arguments",
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001961 pp_directives[ct]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001962 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001963 }
1964 tt = tt->next;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001965 j = true; /* assume equality unless proved not */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001966 while ((t->type != TOK_OTHER || strcmp(t->text, ",")) && tt) {
1967 if (tt->type == TOK_OTHER && !strcmp(tt->text, ",")) {
1968 error(ERR_NONFATAL, "`%s': more than one comma on line",
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001969 pp_directives[ct]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001970 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001971 }
1972 if (t->type == TOK_WHITESPACE) {
1973 t = t->next;
1974 continue;
1975 }
1976 if (tt->type == TOK_WHITESPACE) {
1977 tt = tt->next;
1978 continue;
1979 }
1980 if (tt->type != t->type) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001981 j = false; /* found mismatching tokens */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001982 break;
1983 }
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07001984 /* When comparing strings, need to unquote them first */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001985 if (t->type == TOK_STRING) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001986 size_t l1 = nasm_unquote(t->text, NULL);
1987 size_t l2 = nasm_unquote(tt->text, NULL);
H. Peter Anvind2456592008-06-19 15:04:18 -07001988
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001989 if (l1 != l2) {
1990 j = false;
1991 break;
1992 }
1993 if (mmemcmp(t->text, tt->text, l1, i == PPC_IFIDN)) {
1994 j = false;
1995 break;
1996 }
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07001997 } else if (mstrcmp(tt->text, t->text, i == PPC_IFIDN) != 0) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001998 j = false; /* found mismatching tokens */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001999 break;
2000 }
Nickolay Yurchenkof3b3ce22003-09-21 20:38:43 +00002001
H. Peter Anvine2c80182005-01-15 22:15:51 +00002002 t = t->next;
2003 tt = tt->next;
2004 }
2005 if ((t->type != TOK_OTHER || strcmp(t->text, ",")) || tt)
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002006 j = false; /* trailing gunk on one end or other */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002007 break;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002008
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002009 case PPC_IFMACRO:
H. Peter Anvin89cee572009-07-15 09:16:54 -04002010 {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002011 bool found = false;
Keith Kaniosb307a4f2010-11-06 17:41:51 -05002012 ExpDef searching, *ed;
H. Peter Anvin65747262002-05-07 00:10:05 +00002013
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002014 skip_white_(tline);
2015 tline = expand_id(tline);
2016 if (!tok_type_(tline, TOK_ID)) {
2017 error(ERR_NONFATAL,
2018 "`%s' expects a macro name", pp_directives[ct]);
2019 goto fail;
2020 }
Cyrill Gorcunova22e7a92010-11-11 11:42:40 +03002021 memset(&searching, 0, sizeof(searching));
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002022 searching.name = nasm_strdup(tline->text);
2023 searching.casesense = true;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002024 searching.nparam_max = INT_MAX;
2025 tline = expand_smacro(tline->next);
2026 skip_white_(tline);
2027 if (!tline) {
2028 } else if (!tok_type_(tline, TOK_NUMBER)) {
2029 error(ERR_NONFATAL,
2030 "`%s' expects a parameter count or nothing",
2031 pp_directives[ct]);
2032 } else {
2033 searching.nparam_min = searching.nparam_max =
2034 readnum(tline->text, &j);
2035 if (j)
2036 error(ERR_NONFATAL,
2037 "unable to parse parameter count `%s'",
2038 tline->text);
2039 }
2040 if (tline && tok_is_(tline->next, "-")) {
2041 tline = tline->next->next;
2042 if (tok_is_(tline, "*"))
2043 searching.nparam_max = INT_MAX;
2044 else if (!tok_type_(tline, TOK_NUMBER))
2045 error(ERR_NONFATAL,
2046 "`%s' expects a parameter count after `-'",
2047 pp_directives[ct]);
2048 else {
2049 searching.nparam_max = readnum(tline->text, &j);
2050 if (j)
2051 error(ERR_NONFATAL,
2052 "unable to parse parameter count `%s'",
2053 tline->text);
2054 if (searching.nparam_min > searching.nparam_max)
2055 error(ERR_NONFATAL,
2056 "minimum parameter count exceeds maximum");
2057 }
2058 }
2059 if (tline && tok_is_(tline->next, "+")) {
2060 tline = tline->next;
2061 searching.plus = true;
2062 }
Keith Kaniosb307a4f2010-11-06 17:41:51 -05002063 ed = (ExpDef *) hash_findix(&expdefs, searching.name);
2064 while (ed != NULL) {
Cyrill Gorcunova22e7a92010-11-11 11:42:40 +03002065 if (!strcmp(ed->name, searching.name) &&
2066 (ed->nparam_min <= searching.nparam_max || searching.plus) &&
2067 (searching.nparam_min <= ed->nparam_max || ed->plus)) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002068 found = true;
2069 break;
2070 }
Keith Kaniosb307a4f2010-11-06 17:41:51 -05002071 ed = ed->next;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002072 }
2073 if (tline && tline->next)
2074 error(ERR_WARNING|ERR_PASS1,
2075 "trailing garbage after %%ifmacro ignored");
2076 nasm_free(searching.name);
2077 j = found;
2078 break;
H. Peter Anvin89cee572009-07-15 09:16:54 -04002079 }
H. Peter Anvin65747262002-05-07 00:10:05 +00002080
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002081 case PPC_IFID:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002082 needtype = TOK_ID;
2083 goto iftype;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002084 case PPC_IFNUM:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002085 needtype = TOK_NUMBER;
2086 goto iftype;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002087 case PPC_IFSTR:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002088 needtype = TOK_STRING;
2089 goto iftype;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002090
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002091iftype:
2092 t = tline = expand_smacro(tline);
H. Peter Anvind85d2502008-05-04 17:53:31 -07002093
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002094 while (tok_type_(t, TOK_WHITESPACE) ||
2095 (needtype == TOK_NUMBER &&
2096 tok_type_(t, TOK_OTHER) &&
2097 (t->text[0] == '-' || t->text[0] == '+') &&
2098 !t->text[1]))
2099 t = t->next;
H. Peter Anvind85d2502008-05-04 17:53:31 -07002100
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002101 j = tok_type_(t, needtype);
2102 break;
H. Peter Anvincbf768d2008-02-16 16:41:25 -08002103
2104 case PPC_IFTOKEN:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002105 t = tline = expand_smacro(tline);
2106 while (tok_type_(t, TOK_WHITESPACE))
2107 t = t->next;
H. Peter Anvincbf768d2008-02-16 16:41:25 -08002108
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002109 j = false;
2110 if (t) {
2111 t = t->next; /* Skip the actual token */
2112 while (tok_type_(t, TOK_WHITESPACE))
2113 t = t->next;
2114 j = !t; /* Should be nothing left */
2115 }
2116 break;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002117
H. Peter Anvin134b9462008-02-16 17:01:40 -08002118 case PPC_IFEMPTY:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002119 t = tline = expand_smacro(tline);
2120 while (tok_type_(t, TOK_WHITESPACE))
2121 t = t->next;
H. Peter Anvin134b9462008-02-16 17:01:40 -08002122
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002123 j = !t; /* Should be empty */
2124 break;
H. Peter Anvin134b9462008-02-16 17:01:40 -08002125
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002126 case PPC_IF:
H. Peter Anvine2c80182005-01-15 22:15:51 +00002127 t = tline = expand_smacro(tline);
2128 tptr = &t;
2129 tokval.t_type = TOKEN_INVALID;
2130 evalresult = evaluate(ppscan, tptr, &tokval,
2131 NULL, pass | CRITICAL, error, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002132 if (!evalresult)
2133 return -1;
2134 if (tokval.t_type)
H. Peter Anvin917a3492008-09-24 09:14:49 -07002135 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +00002136 "trailing garbage after expression ignored");
2137 if (!is_simple(evalresult)) {
2138 error(ERR_NONFATAL,
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002139 "non-constant value given to `%s'", pp_directives[ct]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002140 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002141 }
Chuck Crayne60ae75d2007-05-02 01:59:16 +00002142 j = reloc_value(evalresult) != 0;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002143 break;
H. Peter Anvin95e28822007-09-12 04:20:08 +00002144
H. Peter Anvine2c80182005-01-15 22:15:51 +00002145 default:
2146 error(ERR_FATAL,
2147 "preprocessor directive `%s' not yet implemented",
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002148 pp_directives[ct]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002149 goto fail;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002150 }
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002151
2152 free_tlist(origline);
2153 return j ^ PP_NEGATIVE(ct);
H. Peter Anvin70653092007-10-19 14:42:29 -07002154
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002155fail:
2156 free_tlist(origline);
2157 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002158}
2159
2160/*
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002161 * Common code for defining an smacro
2162 */
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08002163static bool define_smacro(Context *ctx, const char *mname, bool casesense,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002164 int nparam, Token *expansion)
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002165{
2166 SMacro *smac, **smhead;
H. Peter Anvin166c2472008-05-28 12:28:58 -07002167 struct hash_table *smtbl;
H. Peter Anvin70653092007-10-19 14:42:29 -07002168
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002169 if (smacro_defined(ctx, mname, nparam, &smac, casesense)) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002170 if (!smac) {
2171 error(ERR_WARNING|ERR_PASS1,
2172 "single-line macro `%s' defined both with and"
2173 " without parameters", mname);
2174 /*
2175 * Some instances of the old code considered this a failure,
2176 * some others didn't. What is the right thing to do here?
2177 */
2178 free_tlist(expansion);
2179 return false; /* Failure */
2180 } else {
2181 /*
2182 * We're redefining, so we have to take over an
2183 * existing SMacro structure. This means freeing
2184 * what was already in it.
2185 */
2186 nasm_free(smac->name);
2187 free_tlist(smac->expansion);
2188 }
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002189 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002190 smtbl = ctx ? &ctx->localmac : &smacros;
2191 smhead = (SMacro **) hash_findi_add(smtbl, mname);
Cyrill Gorcunov574fbf12010-11-11 13:44:51 +03002192 smac = nasm_zalloc(sizeof(SMacro));
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002193 smac->next = *smhead;
2194 *smhead = smac;
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002195 }
2196 smac->name = nasm_strdup(mname);
2197 smac->casesense = casesense;
2198 smac->nparam = nparam;
2199 smac->expansion = expansion;
2200 smac->in_progress = false;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002201 return true; /* Success */
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002202}
2203
2204/*
2205 * Undefine an smacro
2206 */
2207static void undef_smacro(Context *ctx, const char *mname)
2208{
2209 SMacro **smhead, *s, **sp;
H. Peter Anvin166c2472008-05-28 12:28:58 -07002210 struct hash_table *smtbl;
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002211
H. Peter Anvin166c2472008-05-28 12:28:58 -07002212 smtbl = ctx ? &ctx->localmac : &smacros;
2213 smhead = (SMacro **)hash_findi(smtbl, mname, NULL);
H. Peter Anvin70653092007-10-19 14:42:29 -07002214
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002215 if (smhead) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002216 /*
2217 * We now have a macro name... go hunt for it.
2218 */
2219 sp = smhead;
2220 while ((s = *sp) != NULL) {
2221 if (!mstrcmp(s->name, mname, s->casesense)) {
2222 *sp = s->next;
2223 nasm_free(s->name);
2224 free_tlist(s->expansion);
2225 nasm_free(s);
2226 } else {
2227 sp = &s->next;
2228 }
2229 }
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002230 }
2231}
2232
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002233/*
H. Peter Anvina26433d2008-07-16 14:40:01 -07002234 * Parse a mmacro specification.
2235 */
Keith Kaniosb307a4f2010-11-06 17:41:51 -05002236static bool parse_mmacro_spec(Token *tline, ExpDef *def, const char *directive)
H. Peter Anvina26433d2008-07-16 14:40:01 -07002237{
2238 bool err;
2239
2240 tline = tline->next;
2241 skip_white_(tline);
2242 tline = expand_id(tline);
2243 if (!tok_type_(tline, TOK_ID)) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002244 error(ERR_NONFATAL, "`%s' expects a macro name", directive);
2245 return false;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002246 }
Victor van den Elzenb916ede2008-07-23 15:14:22 +02002247
H. Peter Anvina26433d2008-07-16 14:40:01 -07002248 def->name = nasm_strdup(tline->text);
2249 def->plus = false;
2250 def->nolist = false;
Keith Kaniosb307a4f2010-11-06 17:41:51 -05002251// def->in_progress = 0;
2252// def->rep_nest = NULL;
Victor van den Elzenb916ede2008-07-23 15:14:22 +02002253 def->nparam_min = 0;
2254 def->nparam_max = 0;
2255
H. Peter Anvina26433d2008-07-16 14:40:01 -07002256 tline = expand_smacro(tline->next);
2257 skip_white_(tline);
2258 if (!tok_type_(tline, TOK_NUMBER)) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002259 error(ERR_NONFATAL, "`%s' expects a parameter count", directive);
H. Peter Anvina26433d2008-07-16 14:40:01 -07002260 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002261 def->nparam_min = def->nparam_max =
2262 readnum(tline->text, &err);
2263 if (err)
2264 error(ERR_NONFATAL,
2265 "unable to parse parameter count `%s'", tline->text);
H. Peter Anvina26433d2008-07-16 14:40:01 -07002266 }
2267 if (tline && tok_is_(tline->next, "-")) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002268 tline = tline->next->next;
2269 if (tok_is_(tline, "*")) {
2270 def->nparam_max = INT_MAX;
2271 } else if (!tok_type_(tline, TOK_NUMBER)) {
2272 error(ERR_NONFATAL,
2273 "`%s' expects a parameter count after `-'", directive);
2274 } else {
2275 def->nparam_max = readnum(tline->text, &err);
2276 if (err) {
2277 error(ERR_NONFATAL, "unable to parse parameter count `%s'",
2278 tline->text);
2279 }
2280 if (def->nparam_min > def->nparam_max) {
2281 error(ERR_NONFATAL, "minimum parameter count exceeds maximum");
2282 }
2283 }
H. Peter Anvina26433d2008-07-16 14:40:01 -07002284 }
2285 if (tline && tok_is_(tline->next, "+")) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002286 tline = tline->next;
2287 def->plus = true;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002288 }
2289 if (tline && tok_type_(tline->next, TOK_ID) &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002290 !nasm_stricmp(tline->next->text, ".nolist")) {
2291 tline = tline->next;
2292 def->nolist = true;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002293 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002294
H. Peter Anvina26433d2008-07-16 14:40:01 -07002295 /*
2296 * Handle default parameters.
2297 */
2298 if (tline && tline->next) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002299 def->dlist = tline->next;
2300 tline->next = NULL;
2301 count_mmac_params(def->dlist, &def->ndefs, &def->defaults);
H. Peter Anvina26433d2008-07-16 14:40:01 -07002302 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002303 def->dlist = NULL;
2304 def->defaults = NULL;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002305 }
Keith Kaniosb307a4f2010-11-06 17:41:51 -05002306 def->line = NULL;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002307
H. Peter Anvin89cee572009-07-15 09:16:54 -04002308 if (def->defaults && def->ndefs > def->nparam_max - def->nparam_min &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002309 !def->plus)
2310 error(ERR_WARNING|ERR_PASS1|ERR_WARN_MDP,
2311 "too many default macro parameters");
Victor van den Elzenb916ede2008-07-23 15:14:22 +02002312
H. Peter Anvina26433d2008-07-16 14:40:01 -07002313 return true;
2314}
2315
2316
2317/*
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002318 * Decode a size directive
2319 */
2320static int parse_size(const char *str) {
2321 static const char *size_names[] =
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002322 { "byte", "dword", "oword", "qword", "tword", "word", "yword" };
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002323 static const int sizes[] =
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002324 { 0, 1, 4, 16, 8, 10, 2, 32 };
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002325
Cyrill Gorcunova7319242010-06-03 22:04:36 +04002326 return sizes[bsii(str, size_names, ARRAY_SIZE(size_names))+1];
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002327}
2328
Ed Beroset3ab3f412002-06-11 03:31:49 +00002329/**
2330 * find and process preprocessor directive in passed line
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002331 * Find out if a line contains a preprocessor directive, and deal
2332 * with it if so.
H. Peter Anvin70653092007-10-19 14:42:29 -07002333 *
Ed Beroset3ab3f412002-06-11 03:31:49 +00002334 * If a directive _is_ found, it is the responsibility of this routine
2335 * (and not the caller) to free_tlist() the line.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002336 *
Ed Beroset3ab3f412002-06-11 03:31:49 +00002337 * @param tline a pointer to the current tokeninzed line linked list
2338 * @return DIRECTIVE_FOUND or NO_DIRECTIVE_FOUND
H. Peter Anvin70653092007-10-19 14:42:29 -07002339 *
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002340 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002341static int do_directive(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00002342{
H. Peter Anvin4169a472007-09-12 01:29:43 +00002343 enum preproc_token i;
2344 int j;
H. Peter Anvin70055962007-10-11 00:05:31 -07002345 bool err;
2346 int nparam;
2347 bool nolist;
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07002348 bool casesense;
H. Peter Anvin8cfdb9d2007-09-14 18:36:01 -07002349 int k, m;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002350 int offset;
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08002351 char *p, *pp;
2352 const char *mname;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002353 Include *inc;
2354 Context *ctx;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002355 Line *l;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002356 Token *t, *tt, *param_start, *macro_start, *last, **tptr, *origline;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002357 struct tokenval tokval;
2358 expr *evalresult;
Keith Kaniosb307a4f2010-11-06 17:41:51 -05002359 ExpDef *ed, *eed, **edhead;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002360 ExpInv *ei, *eei;
H. Peter Anvinf8ba53e2007-10-11 10:11:57 -07002361 int64_t count;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07002362 size_t len;
H. Peter Anvin8e3f75e2008-09-24 00:21:58 -07002363 int severity;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002364
2365 origline = tline;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002366
H. Peter Anvineba20a72002-04-30 20:53:55 +00002367 skip_white_(tline);
H. Peter Anvinf2936d72008-06-04 15:11:23 -07002368 if (!tline || !tok_type_(tline, TOK_PREPROC_ID) ||
H. Peter Anvine2c80182005-01-15 22:15:51 +00002369 (tline->text[1] == '%' || tline->text[1] == '$'
2370 || tline->text[1] == '!'))
2371 return NO_DIRECTIVE_FOUND;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002372
H. Peter Anvin4169a472007-09-12 01:29:43 +00002373 i = pp_token_hash(tline->text);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002374
H. Peter Anvin4169a472007-09-12 01:29:43 +00002375 switch (i) {
2376 case PP_INVALID:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002377 if (defining != NULL) return NO_DIRECTIVE_FOUND;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002378 error(ERR_NONFATAL, "unknown preprocessor directive `%s'",
2379 tline->text);
2380 return NO_DIRECTIVE_FOUND; /* didn't get it */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002381
H. Peter Anvine2c80182005-01-15 22:15:51 +00002382 case PP_STACKSIZE:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002383 if (defining != NULL) return NO_DIRECTIVE_FOUND;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002384 /* Directive to tell NASM what the default stack size is. The
2385 * default is for a 16-bit stack, and this can be overriden with
2386 * %stacksize large.
H. Peter Anvine2c80182005-01-15 22:15:51 +00002387 */
2388 tline = tline->next;
2389 if (tline && tline->type == TOK_WHITESPACE)
2390 tline = tline->next;
2391 if (!tline || tline->type != TOK_ID) {
2392 error(ERR_NONFATAL, "`%%stacksize' missing size parameter");
2393 free_tlist(origline);
2394 return DIRECTIVE_FOUND;
2395 }
2396 if (nasm_stricmp(tline->text, "flat") == 0) {
2397 /* All subsequent ARG directives are for a 32-bit stack */
2398 StackSize = 4;
2399 StackPointer = "ebp";
2400 ArgOffset = 8;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002401 LocalOffset = 0;
Charles Crayne7eaf9192007-11-08 22:11:14 -08002402 } else if (nasm_stricmp(tline->text, "flat64") == 0) {
2403 /* All subsequent ARG directives are for a 64-bit stack */
2404 StackSize = 8;
2405 StackPointer = "rbp";
Per Jessen53252e02010-02-11 00:16:59 +03002406 ArgOffset = 16;
Charles Crayne7eaf9192007-11-08 22:11:14 -08002407 LocalOffset = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002408 } else if (nasm_stricmp(tline->text, "large") == 0) {
2409 /* All subsequent ARG directives are for a 16-bit stack,
2410 * far function call.
2411 */
2412 StackSize = 2;
2413 StackPointer = "bp";
2414 ArgOffset = 4;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002415 LocalOffset = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002416 } else if (nasm_stricmp(tline->text, "small") == 0) {
2417 /* All subsequent ARG directives are for a 16-bit stack,
2418 * far function call. We don't support near functions.
2419 */
2420 StackSize = 2;
2421 StackPointer = "bp";
2422 ArgOffset = 6;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002423 LocalOffset = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002424 } else {
2425 error(ERR_NONFATAL, "`%%stacksize' invalid size type");
2426 free_tlist(origline);
2427 return DIRECTIVE_FOUND;
2428 }
2429 free_tlist(origline);
2430 return DIRECTIVE_FOUND;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002431
H. Peter Anvine2c80182005-01-15 22:15:51 +00002432 case PP_ARG:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002433 if (defining != NULL) return NO_DIRECTIVE_FOUND;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002434 /* TASM like ARG directive to define arguments to functions, in
2435 * the following form:
2436 *
2437 * ARG arg1:WORD, arg2:DWORD, arg4:QWORD
2438 */
2439 offset = ArgOffset;
2440 do {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00002441 char *arg, directive[256];
H. Peter Anvine2c80182005-01-15 22:15:51 +00002442 int size = StackSize;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002443
H. Peter Anvine2c80182005-01-15 22:15:51 +00002444 /* Find the argument name */
2445 tline = tline->next;
2446 if (tline && tline->type == TOK_WHITESPACE)
2447 tline = tline->next;
2448 if (!tline || tline->type != TOK_ID) {
2449 error(ERR_NONFATAL, "`%%arg' missing argument parameter");
2450 free_tlist(origline);
2451 return DIRECTIVE_FOUND;
2452 }
2453 arg = tline->text;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002454
H. Peter Anvine2c80182005-01-15 22:15:51 +00002455 /* Find the argument size type */
2456 tline = tline->next;
2457 if (!tline || tline->type != TOK_OTHER
2458 || tline->text[0] != ':') {
2459 error(ERR_NONFATAL,
2460 "Syntax error processing `%%arg' directive");
2461 free_tlist(origline);
2462 return DIRECTIVE_FOUND;
2463 }
2464 tline = tline->next;
2465 if (!tline || tline->type != TOK_ID) {
2466 error(ERR_NONFATAL, "`%%arg' missing size type parameter");
2467 free_tlist(origline);
2468 return DIRECTIVE_FOUND;
2469 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002470
H. Peter Anvine2c80182005-01-15 22:15:51 +00002471 /* Allow macro expansion of type parameter */
Keith Kaniosb7a89542007-04-12 02:40:54 +00002472 tt = tokenize(tline->text);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002473 tt = expand_smacro(tt);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002474 size = parse_size(tt->text);
2475 if (!size) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002476 error(ERR_NONFATAL,
2477 "Invalid size type for `%%arg' missing directive");
2478 free_tlist(tt);
2479 free_tlist(origline);
2480 return DIRECTIVE_FOUND;
2481 }
2482 free_tlist(tt);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002483
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002484 /* Round up to even stack slots */
2485 size = ALIGN(size, StackSize);
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002486
H. Peter Anvine2c80182005-01-15 22:15:51 +00002487 /* Now define the macro for the argument */
2488 snprintf(directive, sizeof(directive), "%%define %s (%s+%d)",
2489 arg, StackPointer, offset);
Keith Kaniosb7a89542007-04-12 02:40:54 +00002490 do_directive(tokenize(directive));
H. Peter Anvine2c80182005-01-15 22:15:51 +00002491 offset += size;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002492
H. Peter Anvine2c80182005-01-15 22:15:51 +00002493 /* Move to the next argument in the list */
2494 tline = tline->next;
2495 if (tline && tline->type == TOK_WHITESPACE)
2496 tline = tline->next;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002497 } while (tline && tline->type == TOK_OTHER && tline->text[0] == ',');
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002498 ArgOffset = offset;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002499 free_tlist(origline);
2500 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002501
H. Peter Anvine2c80182005-01-15 22:15:51 +00002502 case PP_LOCAL:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002503 if (defining != NULL) return NO_DIRECTIVE_FOUND;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002504 /* TASM like LOCAL directive to define local variables for a
2505 * function, in the following form:
2506 *
2507 * LOCAL local1:WORD, local2:DWORD, local4:QWORD = LocalSize
2508 *
2509 * The '= LocalSize' at the end is ignored by NASM, but is
2510 * required by TASM to define the local parameter size (and used
2511 * by the TASM macro package).
2512 */
2513 offset = LocalOffset;
2514 do {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00002515 char *local, directive[256];
H. Peter Anvine2c80182005-01-15 22:15:51 +00002516 int size = StackSize;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002517
H. Peter Anvine2c80182005-01-15 22:15:51 +00002518 /* Find the argument name */
2519 tline = tline->next;
2520 if (tline && tline->type == TOK_WHITESPACE)
2521 tline = tline->next;
2522 if (!tline || tline->type != TOK_ID) {
2523 error(ERR_NONFATAL,
2524 "`%%local' missing argument parameter");
2525 free_tlist(origline);
2526 return DIRECTIVE_FOUND;
2527 }
2528 local = tline->text;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002529
H. Peter Anvine2c80182005-01-15 22:15:51 +00002530 /* Find the argument size type */
2531 tline = tline->next;
2532 if (!tline || tline->type != TOK_OTHER
2533 || tline->text[0] != ':') {
2534 error(ERR_NONFATAL,
2535 "Syntax error processing `%%local' directive");
2536 free_tlist(origline);
2537 return DIRECTIVE_FOUND;
2538 }
2539 tline = tline->next;
2540 if (!tline || tline->type != TOK_ID) {
2541 error(ERR_NONFATAL,
2542 "`%%local' missing size type parameter");
2543 free_tlist(origline);
2544 return DIRECTIVE_FOUND;
2545 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00002546
H. Peter Anvine2c80182005-01-15 22:15:51 +00002547 /* Allow macro expansion of type parameter */
Keith Kaniosb7a89542007-04-12 02:40:54 +00002548 tt = tokenize(tline->text);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002549 tt = expand_smacro(tt);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002550 size = parse_size(tt->text);
2551 if (!size) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002552 error(ERR_NONFATAL,
2553 "Invalid size type for `%%local' missing directive");
2554 free_tlist(tt);
2555 free_tlist(origline);
2556 return DIRECTIVE_FOUND;
2557 }
2558 free_tlist(tt);
H. Peter Anvin734b1882002-04-30 21:01:08 +00002559
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002560 /* Round up to even stack slots */
2561 size = ALIGN(size, StackSize);
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002562
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002563 offset += size; /* Negative offset, increment before */
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002564
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002565 /* Now define the macro for the argument */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002566 snprintf(directive, sizeof(directive), "%%define %s (%s-%d)",
2567 local, StackPointer, offset);
Keith Kaniosb7a89542007-04-12 02:40:54 +00002568 do_directive(tokenize(directive));
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002569
H. Peter Anvine2c80182005-01-15 22:15:51 +00002570 /* Now define the assign to setup the enter_c macro correctly */
2571 snprintf(directive, sizeof(directive),
2572 "%%assign %%$localsize %%$localsize+%d", size);
Keith Kaniosb7a89542007-04-12 02:40:54 +00002573 do_directive(tokenize(directive));
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002574
H. Peter Anvine2c80182005-01-15 22:15:51 +00002575 /* Move to the next argument in the list */
2576 tline = tline->next;
2577 if (tline && tline->type == TOK_WHITESPACE)
2578 tline = tline->next;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002579 } while (tline && tline->type == TOK_OTHER && tline->text[0] == ',');
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002580 LocalOffset = offset;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002581 free_tlist(origline);
2582 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002583
H. Peter Anvine2c80182005-01-15 22:15:51 +00002584 case PP_CLEAR:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002585 if (defining != NULL) return NO_DIRECTIVE_FOUND;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002586 if (tline->next)
H. Peter Anvin917a3492008-09-24 09:14:49 -07002587 error(ERR_WARNING|ERR_PASS1,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002588 "trailing garbage after `%%clear' ignored");
2589 free_macros();
2590 init_macros();
H. Peter Anvine2c80182005-01-15 22:15:51 +00002591 free_tlist(origline);
2592 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002593
H. Peter Anvin418ca702008-05-30 10:42:30 -07002594 case PP_DEPEND:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002595 if (defining != NULL) return NO_DIRECTIVE_FOUND;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002596 t = tline->next = expand_smacro(tline->next);
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002597 skip_white_(t);
2598 if (!t || (t->type != TOK_STRING &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002599 t->type != TOK_INTERNAL_STRING)) {
H. Peter Anvin418ca702008-05-30 10:42:30 -07002600 error(ERR_NONFATAL, "`%%depend' expects a file name");
2601 free_tlist(origline);
2602 return DIRECTIVE_FOUND; /* but we did _something_ */
2603 }
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002604 if (t->next)
H. Peter Anvin917a3492008-09-24 09:14:49 -07002605 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvin418ca702008-05-30 10:42:30 -07002606 "trailing garbage after `%%depend' ignored");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002607 p = t->text;
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002608 if (t->type != TOK_INTERNAL_STRING)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002609 nasm_unquote_cstr(p, i);
2610 if (dephead && !in_list(*dephead, p)) {
2611 StrList *sl = nasm_malloc(strlen(p)+1+sizeof sl->next);
2612 sl->next = NULL;
2613 strcpy(sl->str, p);
2614 *deptail = sl;
2615 deptail = &sl->next;
2616 }
2617 free_tlist(origline);
H. Peter Anvin418ca702008-05-30 10:42:30 -07002618 return DIRECTIVE_FOUND;
2619
2620 case PP_INCLUDE:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002621 if (defining != NULL) return NO_DIRECTIVE_FOUND;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002622 t = tline->next = expand_smacro(tline->next);
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002623 skip_white_(t);
H. Peter Anvind2456592008-06-19 15:04:18 -07002624
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002625 if (!t || (t->type != TOK_STRING &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002626 t->type != TOK_INTERNAL_STRING)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002627 error(ERR_NONFATAL, "`%%include' expects a file name");
2628 free_tlist(origline);
2629 return DIRECTIVE_FOUND; /* but we did _something_ */
2630 }
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002631 if (t->next)
H. Peter Anvin917a3492008-09-24 09:14:49 -07002632 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +00002633 "trailing garbage after `%%include' ignored");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002634 p = t->text;
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002635 if (t->type != TOK_INTERNAL_STRING)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002636 nasm_unquote_cstr(p, i);
Cyrill Gorcunov55cc4d02010-11-10 23:12:06 +03002637 inc = nasm_zalloc(sizeof(Include));
H. Peter Anvine2c80182005-01-15 22:15:51 +00002638 inc->next = istk;
H. Peter Anvin2b1c3b92008-06-06 10:38:46 -07002639 inc->fp = inc_fopen(p, dephead, &deptail, pass == 0);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002640 if (!inc->fp) {
2641 /* -MG given but file not found */
2642 nasm_free(inc);
2643 } else {
2644 inc->fname = src_set_fname(nasm_strdup(p));
2645 inc->lineno = src_set_linnum(0);
2646 inc->lineinc = 1;
2647 inc->expansion = NULL;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002648 istk = inc;
2649 list->uplevel(LIST_INCLUDE);
2650 }
2651 free_tlist(origline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002652 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002653
H. Peter Anvind2456592008-06-19 15:04:18 -07002654 case PP_USE:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002655 if (defining != NULL) return NO_DIRECTIVE_FOUND;
H. Peter Anvinf4ae5ad2008-06-19 18:39:24 -07002656 {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002657 static macros_t *use_pkg;
2658 const char *pkg_macro = NULL;
H. Peter Anvinf4ae5ad2008-06-19 18:39:24 -07002659
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002660 tline = tline->next;
2661 skip_white_(tline);
2662 tline = expand_id(tline);
H. Peter Anvind2456592008-06-19 15:04:18 -07002663
H. Peter Anvin264b7b92008-10-24 16:38:17 -07002664 if (!tline || (tline->type != TOK_STRING &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002665 tline->type != TOK_INTERNAL_STRING &&
2666 tline->type != TOK_ID)) {
H. Peter Anvin926fc402008-06-19 16:26:12 -07002667 error(ERR_NONFATAL, "`%%use' expects a package name");
H. Peter Anvind2456592008-06-19 15:04:18 -07002668 free_tlist(origline);
2669 return DIRECTIVE_FOUND; /* but we did _something_ */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002670 }
H. Peter Anvin264b7b92008-10-24 16:38:17 -07002671 if (tline->next)
H. Peter Anvin917a3492008-09-24 09:14:49 -07002672 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvind2456592008-06-19 15:04:18 -07002673 "trailing garbage after `%%use' ignored");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002674 if (tline->type == TOK_STRING)
2675 nasm_unquote_cstr(tline->text, i);
2676 use_pkg = nasm_stdmac_find_package(tline->text);
2677 if (!use_pkg)
2678 error(ERR_NONFATAL, "unknown `%%use' package: %s", tline->text);
2679 else
2680 pkg_macro = (char *)use_pkg + 1; /* The first string will be <%define>__USE_*__ */
Victor van den Elzen35eb2ea2010-03-10 22:33:48 +01002681 if (use_pkg && ! smacro_defined(NULL, pkg_macro, 0, NULL, true)) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002682 /* Not already included, go ahead and include it */
2683 stdmacpos = use_pkg;
2684 }
2685 free_tlist(origline);
H. Peter Anvind2456592008-06-19 15:04:18 -07002686 return DIRECTIVE_FOUND;
H. Peter Anvinf4ae5ad2008-06-19 18:39:24 -07002687 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002688 case PP_PUSH:
H. Peter Anvine2c80182005-01-15 22:15:51 +00002689 case PP_REPL:
H. Peter Anvin42b56392008-10-24 16:24:21 -07002690 case PP_POP:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002691 if (defining != NULL) return NO_DIRECTIVE_FOUND;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002692 tline = tline->next;
2693 skip_white_(tline);
2694 tline = expand_id(tline);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002695 if (tline) {
2696 if (!tok_type_(tline, TOK_ID)) {
2697 error(ERR_NONFATAL, "`%s' expects a context identifier",
2698 pp_directives[i]);
2699 free_tlist(origline);
2700 return DIRECTIVE_FOUND; /* but we did _something_ */
2701 }
2702 if (tline->next)
2703 error(ERR_WARNING|ERR_PASS1,
2704 "trailing garbage after `%s' ignored",
2705 pp_directives[i]);
2706 p = nasm_strdup(tline->text);
2707 } else {
2708 p = NULL; /* Anonymous */
2709 }
H. Peter Anvin42b56392008-10-24 16:24:21 -07002710
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002711 if (i == PP_PUSH) {
Cyrill Gorcunov574fbf12010-11-11 13:44:51 +03002712 ctx = nasm_zalloc(sizeof(Context));
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002713 ctx->next = cstk;
2714 hash_init(&ctx->localmac, HASH_SMALL);
2715 ctx->name = p;
2716 ctx->number = unique++;
2717 cstk = ctx;
2718 } else {
2719 /* %pop or %repl */
2720 if (!cstk) {
2721 error(ERR_NONFATAL, "`%s': context stack is empty",
2722 pp_directives[i]);
2723 } else if (i == PP_POP) {
2724 if (p && (!cstk->name || nasm_stricmp(p, cstk->name)))
2725 error(ERR_NONFATAL, "`%%pop' in wrong context: %s, "
2726 "expected %s",
2727 cstk->name ? cstk->name : "anonymous", p);
2728 else
2729 ctx_pop();
2730 } else {
2731 /* i == PP_REPL */
2732 nasm_free(cstk->name);
2733 cstk->name = p;
2734 p = NULL;
2735 }
2736 nasm_free(p);
2737 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002738 free_tlist(origline);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002739 return DIRECTIVE_FOUND;
H. Peter Anvin8e3f75e2008-09-24 00:21:58 -07002740 case PP_FATAL:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002741 severity = ERR_FATAL;
2742 goto issue_error;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002743 case PP_ERROR:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002744 severity = ERR_NONFATAL;
2745 goto issue_error;
H. Peter Anvin7df04172008-06-10 18:27:38 -07002746 case PP_WARNING:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002747 severity = ERR_WARNING|ERR_WARN_USER;
2748 goto issue_error;
H. Peter Anvin8e3f75e2008-09-24 00:21:58 -07002749
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002750issue_error:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002751 if (defining != NULL) return NO_DIRECTIVE_FOUND;
H. Peter Anvin7df04172008-06-10 18:27:38 -07002752 {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002753 /* Only error out if this is the final pass */
2754 if (pass != 2 && i != PP_FATAL)
2755 return DIRECTIVE_FOUND;
2756
2757 tline->next = expand_smacro(tline->next);
2758 tline = tline->next;
2759 skip_white_(tline);
2760 t = tline ? tline->next : NULL;
2761 skip_white_(t);
2762 if (tok_type_(tline, TOK_STRING) && !t) {
2763 /* The line contains only a quoted string */
2764 p = tline->text;
2765 nasm_unquote(p, NULL); /* Ignore NUL character truncation */
2766 error(severity, "%s", p);
2767 } else {
2768 /* Not a quoted string, or more than a quoted string */
2769 p = detoken(tline, false);
2770 error(severity, "%s", p);
2771 nasm_free(p);
2772 }
2773 free_tlist(origline);
2774 return DIRECTIVE_FOUND;
H. Peter Anvin7df04172008-06-10 18:27:38 -07002775 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00002776
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002777 CASE_PP_IF:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002778 if (defining != NULL) {
2779 if (defining->type == EXP_IF) {
2780 defining->def_depth ++;
2781 }
2782 return NO_DIRECTIVE_FOUND;
2783 }
2784 if ((istk->expansion != NULL) &&
2785 (istk->expansion->emitting == false)) {
2786 j = COND_NEVER;
2787 } else {
2788 j = if_condition(tline->next, i);
2789 tline->next = NULL; /* it got freed */
2790 j = (((j < 0) ? COND_NEVER : j) ? COND_IF_TRUE : COND_IF_FALSE);
2791 }
2792 ed = new_ExpDef(EXP_IF);
2793 ed->state = j;
2794 ed->nolist = NULL;
2795 ed->def_depth = 0;
2796 ed->cur_depth = 0;
2797 ed->max_depth = 0;
2798 ed->ignoring = ((ed->state == COND_IF_TRUE) ? false : true);
2799 ed->prev = defining;
2800 defining = ed;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002801 free_tlist(origline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002802 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002803
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002804 CASE_PP_ELIF:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002805 if (defining != NULL) {
2806 if ((defining->type != EXP_IF) || (defining->def_depth > 0)) {
2807 return NO_DIRECTIVE_FOUND;
2808 }
2809 }
Cyrill Gorcunov82667ff2011-06-25 19:34:19 +04002810 if ((defining == NULL) || (defining->type != EXP_IF)) {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002811 error(ERR_FATAL, "`%s': no matching `%%if'", pp_directives[i]);
2812 }
Keith Kaniosb307a4f2010-11-06 17:41:51 -05002813 switch (defining->state) {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002814 case COND_IF_TRUE:
2815 defining->state = COND_DONE;
2816 defining->ignoring = true;
2817 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002818
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002819 case COND_DONE:
2820 case COND_NEVER:
2821 defining->ignoring = true;
2822 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002823
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002824 case COND_ELSE_TRUE:
2825 case COND_ELSE_FALSE:
2826 error_precond(ERR_WARNING|ERR_PASS1,
2827 "`%%elif' after `%%else' ignored");
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002828 defining->state = COND_NEVER;
2829 defining->ignoring = true;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002830 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002831
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002832 case COND_IF_FALSE:
2833 /*
2834 * IMPORTANT: In the case of %if, we will already have
2835 * called expand_mmac_params(); however, if we're
2836 * processing an %elif we must have been in a
2837 * non-emitting mode, which would have inhibited
2838 * the normal invocation of expand_mmac_params().
2839 * Therefore, we have to do it explicitly here.
2840 */
2841 j = if_condition(expand_mmac_params(tline->next), i);
2842 tline->next = NULL; /* it got freed */
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002843 defining->state =
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002844 j < 0 ? COND_NEVER : j ? COND_IF_TRUE : COND_IF_FALSE;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002845 defining->ignoring = ((defining->state == COND_IF_TRUE) ? false : true);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002846 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002847 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002848 free_tlist(origline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002849 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002850
H. Peter Anvine2c80182005-01-15 22:15:51 +00002851 case PP_ELSE:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002852 if (defining != NULL) {
2853 if ((defining->type != EXP_IF) || (defining->def_depth > 0)) {
2854 return NO_DIRECTIVE_FOUND;
2855 }
2856 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002857 if (tline->next)
H. Peter Anvin917a3492008-09-24 09:14:49 -07002858 error_precond(ERR_WARNING|ERR_PASS1,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002859 "trailing garbage after `%%else' ignored");
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002860 if ((defining == NULL) || (defining->type != EXP_IF)) {
2861 error(ERR_FATAL, "`%s': no matching `%%if'", pp_directives[i]);
2862 }
Keith Kaniosb307a4f2010-11-06 17:41:51 -05002863 switch (defining->state) {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002864 case COND_IF_TRUE:
2865 case COND_DONE:
2866 defining->state = COND_ELSE_FALSE;
2867 defining->ignoring = true;
2868 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002869
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002870 case COND_NEVER:
2871 defining->ignoring = true;
2872 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002873
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002874 case COND_IF_FALSE:
2875 defining->state = COND_ELSE_TRUE;
2876 defining->ignoring = false;
2877 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002878
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002879 case COND_ELSE_TRUE:
2880 case COND_ELSE_FALSE:
2881 error_precond(ERR_WARNING|ERR_PASS1,
2882 "`%%else' after `%%else' ignored.");
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002883 defining->state = COND_NEVER;
2884 defining->ignoring = true;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002885 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002886 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002887 free_tlist(origline);
2888 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002889
H. Peter Anvine2c80182005-01-15 22:15:51 +00002890 case PP_ENDIF:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002891 if (defining != NULL) {
2892 if (defining->type == EXP_IF) {
2893 if (defining->def_depth > 0) {
2894 defining->def_depth --;
2895 return NO_DIRECTIVE_FOUND;
2896 }
2897 } else {
2898 return NO_DIRECTIVE_FOUND;
2899 }
2900 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002901 if (tline->next)
H. Peter Anvin917a3492008-09-24 09:14:49 -07002902 error_precond(ERR_WARNING|ERR_PASS1,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002903 "trailing garbage after `%%endif' ignored");
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002904 if ((defining == NULL) || (defining->type != EXP_IF)) {
2905 error(ERR_NONFATAL, "`%%endif': no matching `%%if'");
2906 return DIRECTIVE_FOUND;
2907 }
2908 ed = defining;
2909 defining = ed->prev;
2910 ed->prev = expansions;
2911 expansions = ed;
2912 ei = new_ExpInv(EXP_IF, ed);
2913 ei->current = ed->line;
2914 ei->emitting = true;
2915 ei->prev = istk->expansion;
2916 istk->expansion = ei;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002917 free_tlist(origline);
2918 return DIRECTIVE_FOUND;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002919
H. Peter Anvindb8f96e2009-07-15 09:07:29 -04002920 case PP_RMACRO:
2921 case PP_IRMACRO:
H. Peter Anvine2c80182005-01-15 22:15:51 +00002922 case PP_MACRO:
2923 case PP_IMACRO:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002924 if (defining != NULL) {
2925 if (defining->type == EXP_MMACRO) {
2926 defining->def_depth ++;
2927 }
2928 return NO_DIRECTIVE_FOUND;
2929 }
2930 ed = new_ExpDef(EXP_MMACRO);
2931 ed->max_depth =
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002932 (i == PP_RMACRO) || (i == PP_IRMACRO) ? DEADMAN_LIMIT : 0;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002933 ed->casesense = (i == PP_MACRO) || (i == PP_RMACRO);
Keith Kaniosb307a4f2010-11-06 17:41:51 -05002934 if (!parse_mmacro_spec(tline, ed, pp_directives[i])) {
2935 nasm_free(ed);
2936 ed = NULL;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002937 return DIRECTIVE_FOUND;
2938 }
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002939 ed->def_depth = 0;
2940 ed->cur_depth = 0;
2941 ed->max_depth = (ed->max_depth + 1);
2942 ed->ignoring = false;
2943 ed->prev = defining;
2944 defining = ed;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002945
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002946 eed = (ExpDef *) hash_findix(&expdefs, ed->name);
2947 while (eed) {
Cyrill Gorcunov574fbf12010-11-11 13:44:51 +03002948 if (!strcmp(eed->name, ed->name) &&
2949 (eed->nparam_min <= ed->nparam_max || ed->plus) &&
2950 (ed->nparam_min <= eed->nparam_max || eed->plus)) {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002951 error(ERR_WARNING|ERR_PASS1,
2952 "redefining multi-line macro `%s'", ed->name);
2953 return DIRECTIVE_FOUND;
2954 }
2955 eed = eed->next;
2956 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002957 free_tlist(origline);
2958 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002959
H. Peter Anvine2c80182005-01-15 22:15:51 +00002960 case PP_ENDM:
2961 case PP_ENDMACRO:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002962 if (defining != NULL) {
2963 if (defining->type == EXP_MMACRO) {
2964 if (defining->def_depth > 0) {
2965 defining->def_depth --;
2966 return NO_DIRECTIVE_FOUND;
2967 }
2968 } else {
2969 return NO_DIRECTIVE_FOUND;
2970 }
2971 }
2972 if (!(defining) || (defining->type != EXP_MMACRO)) {
2973 error(ERR_NONFATAL, "`%s': not defining a macro", tline->text);
2974 return DIRECTIVE_FOUND;
2975 }
2976 edhead = (ExpDef **) hash_findi_add(&expdefs, defining->name);
2977 defining->next = *edhead;
2978 *edhead = defining;
2979 ed = defining;
2980 defining = ed->prev;
2981 ed->prev = expansions;
2982 expansions = ed;
2983 ed = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002984 free_tlist(origline);
2985 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002986
H. Peter Anvin89cee572009-07-15 09:16:54 -04002987 case PP_EXITMACRO:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002988 if (defining != NULL) return NO_DIRECTIVE_FOUND;
2989 /*
2990 * We must search along istk->expansion until we hit a
2991 * macro invocation. Then we disable the emitting state(s)
2992 * between exitmacro and endmacro.
2993 */
2994 for (ei = istk->expansion; ei != NULL; ei = ei->prev) {
2995 if(ei->type == EXP_MMACRO) {
2996 break;
2997 }
2998 }
2999
3000 if (ei != NULL) {
3001 /*
3002 * Set all invocations leading back to the macro
3003 * invocation to a non-emitting state.
3004 */
3005 for (eei = istk->expansion; eei != ei; eei = eei->prev) {
3006 eei->emitting = false;
3007 }
3008 eei->emitting = false;
3009 } else {
3010 error(ERR_NONFATAL, "`%%exitmacro' not within `%%macro' block");
3011 }
Keith Kanios852f1ee2009-07-12 00:19:55 -05003012 free_tlist(origline);
3013 return DIRECTIVE_FOUND;
3014
H. Peter Anvina26433d2008-07-16 14:40:01 -07003015 case PP_UNMACRO:
3016 case PP_UNIMACRO:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003017 if (defining != NULL) return NO_DIRECTIVE_FOUND;
H. Peter Anvina26433d2008-07-16 14:40:01 -07003018 {
Keith Kaniosb307a4f2010-11-06 17:41:51 -05003019 ExpDef **ed_p;
3020 ExpDef spec;
H. Peter Anvina26433d2008-07-16 14:40:01 -07003021
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003022 spec.casesense = (i == PP_UNMACRO);
3023 if (!parse_mmacro_spec(tline, &spec, pp_directives[i])) {
3024 return DIRECTIVE_FOUND;
3025 }
Keith Kaniosb307a4f2010-11-06 17:41:51 -05003026 ed_p = (ExpDef **) hash_findi(&expdefs, spec.name, NULL);
3027 while (ed_p && *ed_p) {
3028 ed = *ed_p;
3029 if (ed->casesense == spec.casesense &&
3030 !mstrcmp(ed->name, spec.name, spec.casesense) &&
3031 ed->nparam_min == spec.nparam_min &&
3032 ed->nparam_max == spec.nparam_max &&
3033 ed->plus == spec.plus) {
Keith Kaniosc98a5b42010-12-18 12:17:31 -06003034 if (ed->cur_depth > 0) {
Keith Kanios21d885b2010-12-18 12:22:21 -06003035 error(ERR_NONFATAL, "`%s' ignored on active macro",
Keith Kaniosc98a5b42010-12-18 12:17:31 -06003036 pp_directives[i]);
3037 break;
3038 } else {
3039 *ed_p = ed->next;
3040 free_expdef(ed);
3041 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003042 } else {
Keith Kaniosb307a4f2010-11-06 17:41:51 -05003043 ed_p = &ed->next;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003044 }
3045 }
3046 free_tlist(origline);
3047 free_tlist(spec.dlist);
3048 return DIRECTIVE_FOUND;
H. Peter Anvina26433d2008-07-16 14:40:01 -07003049 }
3050
H. Peter Anvine2c80182005-01-15 22:15:51 +00003051 case PP_ROTATE:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003052 if (defining != NULL) return NO_DIRECTIVE_FOUND;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003053 if (tline->next && tline->next->type == TOK_WHITESPACE)
3054 tline = tline->next;
H. Peter Anvin89cee572009-07-15 09:16:54 -04003055 if (!tline->next) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00003056 free_tlist(origline);
3057 error(ERR_NONFATAL, "`%%rotate' missing rotate count");
3058 return DIRECTIVE_FOUND;
3059 }
3060 t = expand_smacro(tline->next);
3061 tline->next = NULL;
3062 free_tlist(origline);
3063 tline = t;
3064 tptr = &t;
3065 tokval.t_type = TOKEN_INVALID;
3066 evalresult =
3067 evaluate(ppscan, tptr, &tokval, NULL, pass, error, NULL);
3068 free_tlist(tline);
3069 if (!evalresult)
3070 return DIRECTIVE_FOUND;
3071 if (tokval.t_type)
H. Peter Anvin917a3492008-09-24 09:14:49 -07003072 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +00003073 "trailing garbage after expression ignored");
3074 if (!is_simple(evalresult)) {
3075 error(ERR_NONFATAL, "non-constant value given to `%%rotate'");
3076 return DIRECTIVE_FOUND;
3077 }
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003078 for (ei = istk->expansion; ei != NULL; ei = ei->prev) {
3079 if (ei->type == EXP_MMACRO) {
3080 break;
3081 }
3082 }
3083 if (ei == NULL) {
3084 error(ERR_NONFATAL, "`%%rotate' invoked outside a macro call");
3085 } else if (ei->nparam == 0) {
3086 error(ERR_NONFATAL,
3087 "`%%rotate' invoked within macro without parameters");
3088 } else {
3089 int rotate = ei->rotate + reloc_value(evalresult);
3090
3091 rotate %= (int)ei->nparam;
3092 if (rotate < 0)
3093 rotate += ei->nparam;
3094 ei->rotate = rotate;
3095 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00003096 return DIRECTIVE_FOUND;
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00003097
H. Peter Anvine2c80182005-01-15 22:15:51 +00003098 case PP_REP:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003099 if (defining != NULL) {
3100 if (defining->type == EXP_REP) {
3101 defining->def_depth ++;
3102 }
3103 return NO_DIRECTIVE_FOUND;
3104 }
H. Peter Anvin6867acc2007-10-10 14:58:45 -07003105 nolist = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003106 do {
3107 tline = tline->next;
3108 } while (tok_type_(tline, TOK_WHITESPACE));
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00003109
H. Peter Anvine2c80182005-01-15 22:15:51 +00003110 if (tok_type_(tline, TOK_ID) &&
3111 nasm_stricmp(tline->text, ".nolist") == 0) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07003112 nolist = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003113 do {
3114 tline = tline->next;
3115 } while (tok_type_(tline, TOK_WHITESPACE));
3116 }
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00003117
H. Peter Anvine2c80182005-01-15 22:15:51 +00003118 if (tline) {
3119 t = expand_smacro(tline);
3120 tptr = &t;
3121 tokval.t_type = TOKEN_INVALID;
3122 evalresult =
3123 evaluate(ppscan, tptr, &tokval, NULL, pass, error, NULL);
3124 if (!evalresult) {
3125 free_tlist(origline);
3126 return DIRECTIVE_FOUND;
3127 }
3128 if (tokval.t_type)
H. Peter Anvin917a3492008-09-24 09:14:49 -07003129 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +00003130 "trailing garbage after expression ignored");
3131 if (!is_simple(evalresult)) {
3132 error(ERR_NONFATAL, "non-constant value given to `%%rep'");
3133 return DIRECTIVE_FOUND;
3134 }
Cyrill Gorcunove091d6e2010-08-09 13:58:22 +04003135 count = reloc_value(evalresult);
3136 if (count >= REP_LIMIT) {
Cyrill Gorcunov71f4f842010-08-09 20:17:17 +04003137 error(ERR_NONFATAL, "`%%rep' value exceeds limit");
Cyrill Gorcunove091d6e2010-08-09 13:58:22 +04003138 count = 0;
3139 } else
3140 count++;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003141 } else {
3142 error(ERR_NONFATAL, "`%%rep' expects a repeat count");
H. Peter Anvinf8ba53e2007-10-11 10:11:57 -07003143 count = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003144 }
3145 free_tlist(origline);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003146 ed = new_ExpDef(EXP_REP);
3147 ed->nolist = nolist;
3148 ed->def_depth = 0;
3149 ed->cur_depth = 1;
3150 ed->max_depth = (count - 1);
3151 ed->ignoring = false;
3152 ed->prev = defining;
3153 defining = ed;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003154 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003155
H. Peter Anvine2c80182005-01-15 22:15:51 +00003156 case PP_ENDREP:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003157 if (defining != NULL) {
3158 if (defining->type == EXP_REP) {
3159 if (defining->def_depth > 0) {
3160 defining->def_depth --;
3161 return NO_DIRECTIVE_FOUND;
3162 }
3163 } else {
3164 return NO_DIRECTIVE_FOUND;
3165 }
3166 }
Keith Kaniosb307a4f2010-11-06 17:41:51 -05003167 if ((defining == NULL) || (defining->type != EXP_REP)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00003168 error(ERR_NONFATAL, "`%%endrep': no matching `%%rep'");
3169 return DIRECTIVE_FOUND;
3170 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003171
H. Peter Anvine2c80182005-01-15 22:15:51 +00003172 /*
3173 * Now we have a "macro" defined - although it has no name
3174 * and we won't be entering it in the hash tables - we must
3175 * push a macro-end marker for it on to istk->expansion.
3176 * After that, it will take care of propagating itself (a
3177 * macro-end marker line for a macro which is really a %rep
3178 * block will cause the macro to be re-expanded, complete
3179 * with another macro-end marker to ensure the process
3180 * continues) until the whole expansion is forcibly removed
3181 * from istk->expansion by a %exitrep.
3182 */
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003183 ed = defining;
3184 defining = ed->prev;
3185 ed->prev = expansions;
3186 expansions = ed;
3187 ei = new_ExpInv(EXP_REP, ed);
3188 ei->current = ed->line;
3189 ei->emitting = ((ed->max_depth > 0) ? true : false);
3190 list->uplevel(ed->nolist ? LIST_MACRO_NOLIST : LIST_MACRO);
3191 ei->prev = istk->expansion;
3192 istk->expansion = ei;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003193 free_tlist(origline);
3194 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003195
H. Peter Anvine2c80182005-01-15 22:15:51 +00003196 case PP_EXITREP:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003197 if (defining != NULL) return NO_DIRECTIVE_FOUND;
3198 /*
3199 * We must search along istk->expansion until we hit a
3200 * rep invocation. Then we disable the emitting state(s)
3201 * between exitrep and endrep.
3202 */
3203 for (ei = istk->expansion; ei != NULL; ei = ei->prev) {
3204 if (ei->type == EXP_REP) {
3205 break;
3206 }
3207 }
3208
3209 if (ei != NULL) {
3210 /*
3211 * Set all invocations leading back to the rep
3212 * invocation to a non-emitting state.
3213 */
3214 for (eei = istk->expansion; eei != ei; eei = eei->prev) {
3215 eei->emitting = false;
3216 }
3217 eei->emitting = false;
3218 eei->current = NULL;
3219 eei->def->cur_depth = eei->def->max_depth;
3220 } else {
3221 error(ERR_NONFATAL, "`%%exitrep' not within `%%rep' block");
3222 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00003223 free_tlist(origline);
3224 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003225
H. Peter Anvine2c80182005-01-15 22:15:51 +00003226 case PP_XDEFINE:
3227 case PP_IXDEFINE:
3228 case PP_DEFINE:
3229 case PP_IDEFINE:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003230 if (defining != NULL) return NO_DIRECTIVE_FOUND;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003231 casesense = (i == PP_DEFINE || i == PP_XDEFINE);
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07003232
H. Peter Anvine2c80182005-01-15 22:15:51 +00003233 tline = tline->next;
3234 skip_white_(tline);
3235 tline = expand_id(tline);
3236 if (!tline || (tline->type != TOK_ID &&
3237 (tline->type != TOK_PREPROC_ID ||
3238 tline->text[1] != '$'))) {
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07003239 error(ERR_NONFATAL, "`%s' expects a macro identifier",
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003240 pp_directives[i]);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003241 free_tlist(origline);
3242 return DIRECTIVE_FOUND;
3243 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003244
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08003245 ctx = get_ctx(tline->text, &mname, false);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003246 last = tline;
3247 param_start = tline = tline->next;
3248 nparam = 0;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003249
H. Peter Anvine2c80182005-01-15 22:15:51 +00003250 /* Expand the macro definition now for %xdefine and %ixdefine */
3251 if ((i == PP_XDEFINE) || (i == PP_IXDEFINE))
3252 tline = expand_smacro(tline);
H. Peter Anvin734b1882002-04-30 21:01:08 +00003253
H. Peter Anvine2c80182005-01-15 22:15:51 +00003254 if (tok_is_(tline, "(")) {
3255 /*
3256 * This macro has parameters.
3257 */
H. Peter Anvin734b1882002-04-30 21:01:08 +00003258
H. Peter Anvine2c80182005-01-15 22:15:51 +00003259 tline = tline->next;
3260 while (1) {
3261 skip_white_(tline);
3262 if (!tline) {
3263 error(ERR_NONFATAL, "parameter identifier expected");
3264 free_tlist(origline);
3265 return DIRECTIVE_FOUND;
3266 }
3267 if (tline->type != TOK_ID) {
3268 error(ERR_NONFATAL,
3269 "`%s': parameter identifier expected",
3270 tline->text);
3271 free_tlist(origline);
3272 return DIRECTIVE_FOUND;
3273 }
3274 tline->type = TOK_SMAC_PARAM + nparam++;
3275 tline = tline->next;
3276 skip_white_(tline);
3277 if (tok_is_(tline, ",")) {
3278 tline = tline->next;
H. Peter Anvinca348b62008-07-23 10:49:26 -04003279 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003280 if (!tok_is_(tline, ")")) {
3281 error(ERR_NONFATAL,
3282 "`)' expected to terminate macro template");
3283 free_tlist(origline);
3284 return DIRECTIVE_FOUND;
3285 }
3286 break;
3287 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00003288 }
3289 last = tline;
3290 tline = tline->next;
3291 }
3292 if (tok_type_(tline, TOK_WHITESPACE))
3293 last = tline, tline = tline->next;
3294 macro_start = NULL;
3295 last->next = NULL;
3296 t = tline;
3297 while (t) {
3298 if (t->type == TOK_ID) {
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04003299 list_for_each(tt, param_start)
H. Peter Anvine2c80182005-01-15 22:15:51 +00003300 if (tt->type >= TOK_SMAC_PARAM &&
3301 !strcmp(tt->text, t->text))
3302 t->type = tt->type;
3303 }
3304 tt = t->next;
3305 t->next = macro_start;
3306 macro_start = t;
3307 t = tt;
3308 }
3309 /*
3310 * Good. We now have a macro name, a parameter count, and a
3311 * token list (in reverse order) for an expansion. We ought
3312 * to be OK just to create an SMacro, store it, and let
3313 * free_tlist have the rest of the line (which we have
3314 * carefully re-terminated after chopping off the expansion
3315 * from the end).
3316 */
H. Peter Anvin4db5a162007-10-11 13:42:09 -07003317 define_smacro(ctx, mname, casesense, nparam, macro_start);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003318 free_tlist(origline);
3319 return DIRECTIVE_FOUND;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003320
H. Peter Anvine2c80182005-01-15 22:15:51 +00003321 case PP_UNDEF:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003322 if (defining != NULL) return NO_DIRECTIVE_FOUND;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003323 tline = tline->next;
3324 skip_white_(tline);
3325 tline = expand_id(tline);
3326 if (!tline || (tline->type != TOK_ID &&
3327 (tline->type != TOK_PREPROC_ID ||
3328 tline->text[1] != '$'))) {
3329 error(ERR_NONFATAL, "`%%undef' expects a macro identifier");
3330 free_tlist(origline);
3331 return DIRECTIVE_FOUND;
3332 }
3333 if (tline->next) {
H. Peter Anvin917a3492008-09-24 09:14:49 -07003334 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +00003335 "trailing garbage after macro name ignored");
3336 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003337
H. Peter Anvine2c80182005-01-15 22:15:51 +00003338 /* Find the context that symbol belongs to */
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08003339 ctx = get_ctx(tline->text, &mname, false);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003340 undef_smacro(ctx, mname);
3341 free_tlist(origline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003342 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003343
H. Peter Anvin9e200162008-06-04 17:23:14 -07003344 case PP_DEFSTR:
3345 case PP_IDEFSTR:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003346 if (defining != NULL) return NO_DIRECTIVE_FOUND;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003347 casesense = (i == PP_DEFSTR);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003348
3349 tline = tline->next;
3350 skip_white_(tline);
3351 tline = expand_id(tline);
3352 if (!tline || (tline->type != TOK_ID &&
3353 (tline->type != TOK_PREPROC_ID ||
3354 tline->text[1] != '$'))) {
3355 error(ERR_NONFATAL, "`%s' expects a macro identifier",
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003356 pp_directives[i]);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003357 free_tlist(origline);
3358 return DIRECTIVE_FOUND;
3359 }
3360
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08003361 ctx = get_ctx(tline->text, &mname, false);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003362 last = tline;
3363 tline = expand_smacro(tline->next);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003364 last->next = NULL;
H. Peter Anvin9e200162008-06-04 17:23:14 -07003365
3366 while (tok_type_(tline, TOK_WHITESPACE))
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003367 tline = delete_Token(tline);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003368
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003369 p = detoken(tline, false);
Cyrill Gorcunov6b271292011-02-28 08:45:52 +03003370 macro_start = nasm_zalloc(sizeof(*macro_start));
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003371 macro_start->text = nasm_quote(p, strlen(p));
3372 macro_start->type = TOK_STRING;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003373 nasm_free(p);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003374
3375 /*
3376 * We now have a macro name, an implicit parameter count of
3377 * zero, and a string token to use as an expansion. Create
3378 * and store an SMacro.
3379 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003380 define_smacro(ctx, mname, casesense, 0, macro_start);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003381 free_tlist(origline);
3382 return DIRECTIVE_FOUND;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003383
H. Peter Anvin2f55bda2009-07-14 15:04:04 -04003384 case PP_DEFTOK:
3385 case PP_IDEFTOK:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003386 if (defining != NULL) return NO_DIRECTIVE_FOUND;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003387 casesense = (i == PP_DEFTOK);
3388
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003389 tline = tline->next;
3390 skip_white_(tline);
3391 tline = expand_id(tline);
3392 if (!tline || (tline->type != TOK_ID &&
3393 (tline->type != TOK_PREPROC_ID ||
3394 tline->text[1] != '$'))) {
3395 error(ERR_NONFATAL,
3396 "`%s' expects a macro identifier as first parameter",
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003397 pp_directives[i]);
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003398 free_tlist(origline);
3399 return DIRECTIVE_FOUND;
3400 }
3401 ctx = get_ctx(tline->text, &mname, false);
3402 last = tline;
3403 tline = expand_smacro(tline->next);
3404 last->next = NULL;
3405
3406 t = tline;
3407 while (tok_type_(t, TOK_WHITESPACE))
3408 t = t->next;
3409 /* t should now point to the string */
Cyrill Gorcunov6908e582010-09-06 19:36:15 +04003410 if (!tok_type_(t, TOK_STRING)) {
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003411 error(ERR_NONFATAL,
3412 "`%s` requires string as second parameter",
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003413 pp_directives[i]);
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003414 free_tlist(tline);
3415 free_tlist(origline);
3416 return DIRECTIVE_FOUND;
3417 }
3418
Keith Kaniosb307a4f2010-11-06 17:41:51 -05003419 /*
3420 * Convert the string to a token stream. Note that smacros
3421 * are stored with the token stream reversed, so we have to
3422 * reverse the output of tokenize().
3423 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003424 nasm_unquote_cstr(t->text, i);
H. Peter Anvinb40992c2010-09-15 08:57:21 -07003425 macro_start = reverse_tokens(tokenize(t->text));
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003426
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003427 /*
3428 * We now have a macro name, an implicit parameter count of
3429 * zero, and a numeric token to use as an expansion. Create
3430 * and store an SMacro.
3431 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003432 define_smacro(ctx, mname, casesense, 0, macro_start);
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003433 free_tlist(tline);
3434 free_tlist(origline);
3435 return DIRECTIVE_FOUND;
H. Peter Anvin9e200162008-06-04 17:23:14 -07003436
H. Peter Anvin418ca702008-05-30 10:42:30 -07003437 case PP_PATHSEARCH:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003438 if (defining != NULL) return NO_DIRECTIVE_FOUND;
H. Peter Anvin418ca702008-05-30 10:42:30 -07003439 {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003440 FILE *fp;
3441 StrList *xsl = NULL;
3442 StrList **xst = &xsl;
H. Peter Anvin418ca702008-05-30 10:42:30 -07003443
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003444 casesense = true;
H. Peter Anvin418ca702008-05-30 10:42:30 -07003445
3446 tline = tline->next;
3447 skip_white_(tline);
3448 tline = expand_id(tline);
3449 if (!tline || (tline->type != TOK_ID &&
3450 (tline->type != TOK_PREPROC_ID ||
3451 tline->text[1] != '$'))) {
3452 error(ERR_NONFATAL,
3453 "`%%pathsearch' expects a macro identifier as first parameter");
3454 free_tlist(origline);
3455 return DIRECTIVE_FOUND;
3456 }
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08003457 ctx = get_ctx(tline->text, &mname, false);
H. Peter Anvin418ca702008-05-30 10:42:30 -07003458 last = tline;
3459 tline = expand_smacro(tline->next);
3460 last->next = NULL;
3461
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003462 t = tline;
H. Peter Anvin418ca702008-05-30 10:42:30 -07003463 while (tok_type_(t, TOK_WHITESPACE))
3464 t = t->next;
3465
3466 if (!t || (t->type != TOK_STRING &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003467 t->type != TOK_INTERNAL_STRING)) {
H. Peter Anvin418ca702008-05-30 10:42:30 -07003468 error(ERR_NONFATAL, "`%%pathsearch' expects a file name");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003469 free_tlist(tline);
H. Peter Anvin418ca702008-05-30 10:42:30 -07003470 free_tlist(origline);
3471 return DIRECTIVE_FOUND; /* but we did _something_ */
3472 }
3473 if (t->next)
H. Peter Anvin917a3492008-09-24 09:14:49 -07003474 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvin418ca702008-05-30 10:42:30 -07003475 "trailing garbage after `%%pathsearch' ignored");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003476 p = t->text;
H. Peter Anvin427cc912008-06-01 21:43:03 -07003477 if (t->type != TOK_INTERNAL_STRING)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003478 nasm_unquote(p, NULL);
H. Peter Anvin418ca702008-05-30 10:42:30 -07003479
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003480 fp = inc_fopen(p, &xsl, &xst, true);
3481 if (fp) {
3482 p = xsl->str;
3483 fclose(fp); /* Don't actually care about the file */
3484 }
Cyrill Gorcunov6b271292011-02-28 08:45:52 +03003485 macro_start = nasm_zalloc(sizeof(*macro_start));
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003486 macro_start->text = nasm_quote(p, strlen(p));
3487 macro_start->type = TOK_STRING;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003488 if (xsl)
3489 nasm_free(xsl);
H. Peter Anvin418ca702008-05-30 10:42:30 -07003490
3491 /*
3492 * We now have a macro name, an implicit parameter count of
3493 * zero, and a string token to use as an expansion. Create
3494 * and store an SMacro.
3495 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003496 define_smacro(ctx, mname, casesense, 0, macro_start);
3497 free_tlist(tline);
H. Peter Anvin418ca702008-05-30 10:42:30 -07003498 free_tlist(origline);
3499 return DIRECTIVE_FOUND;
3500 }
3501
H. Peter Anvine2c80182005-01-15 22:15:51 +00003502 case PP_STRLEN:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003503 if (defining != NULL) return NO_DIRECTIVE_FOUND;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003504 casesense = true;
H. Peter Anvin70653092007-10-19 14:42:29 -07003505
H. Peter Anvine2c80182005-01-15 22:15:51 +00003506 tline = tline->next;
3507 skip_white_(tline);
3508 tline = expand_id(tline);
3509 if (!tline || (tline->type != TOK_ID &&
3510 (tline->type != TOK_PREPROC_ID ||
3511 tline->text[1] != '$'))) {
3512 error(ERR_NONFATAL,
3513 "`%%strlen' expects a macro identifier as first parameter");
3514 free_tlist(origline);
3515 return DIRECTIVE_FOUND;
3516 }
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08003517 ctx = get_ctx(tline->text, &mname, false);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003518 last = tline;
3519 tline = expand_smacro(tline->next);
3520 last->next = NULL;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003521
H. Peter Anvine2c80182005-01-15 22:15:51 +00003522 t = tline;
3523 while (tok_type_(t, TOK_WHITESPACE))
3524 t = t->next;
3525 /* t should now point to the string */
Cyrill Gorcunov4e1d5ab2010-07-23 18:51:51 +04003526 if (!tok_type_(t, TOK_STRING)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00003527 error(ERR_NONFATAL,
3528 "`%%strlen` requires string as second parameter");
3529 free_tlist(tline);
3530 free_tlist(origline);
3531 return DIRECTIVE_FOUND;
3532 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003533
Cyrill Gorcunov6b271292011-02-28 08:45:52 +03003534 macro_start = nasm_zalloc(sizeof(*macro_start));
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07003535 make_tok_num(macro_start, nasm_unquote(t->text, NULL));
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003536
H. Peter Anvine2c80182005-01-15 22:15:51 +00003537 /*
3538 * We now have a macro name, an implicit parameter count of
3539 * zero, and a numeric token to use as an expansion. Create
3540 * and store an SMacro.
3541 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003542 define_smacro(ctx, mname, casesense, 0, macro_start);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003543 free_tlist(tline);
3544 free_tlist(origline);
3545 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003546
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003547 case PP_STRCAT:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003548 if (defining != NULL) return NO_DIRECTIVE_FOUND;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003549 casesense = true;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003550
3551 tline = tline->next;
3552 skip_white_(tline);
3553 tline = expand_id(tline);
3554 if (!tline || (tline->type != TOK_ID &&
3555 (tline->type != TOK_PREPROC_ID ||
3556 tline->text[1] != '$'))) {
3557 error(ERR_NONFATAL,
3558 "`%%strcat' expects a macro identifier as first parameter");
3559 free_tlist(origline);
3560 return DIRECTIVE_FOUND;
3561 }
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08003562 ctx = get_ctx(tline->text, &mname, false);
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003563 last = tline;
3564 tline = expand_smacro(tline->next);
3565 last->next = NULL;
3566
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003567 len = 0;
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04003568 list_for_each(t, tline) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003569 switch (t->type) {
3570 case TOK_WHITESPACE:
3571 break;
3572 case TOK_STRING:
3573 len += t->a.len = nasm_unquote(t->text, NULL);
3574 break;
3575 case TOK_OTHER:
3576 if (!strcmp(t->text, ",")) /* permit comma separators */
3577 break;
3578 /* else fall through */
3579 default:
3580 error(ERR_NONFATAL,
3581 "non-string passed to `%%strcat' (%d)", t->type);
3582 free_tlist(tline);
3583 free_tlist(origline);
3584 return DIRECTIVE_FOUND;
3585 }
3586 }
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003587
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003588 p = pp = nasm_malloc(len);
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04003589 list_for_each(t, tline) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003590 if (t->type == TOK_STRING) {
3591 memcpy(p, t->text, t->a.len);
3592 p += t->a.len;
3593 }
3594 }
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003595
3596 /*
3597 * We now have a macro name, an implicit parameter count of
3598 * zero, and a numeric token to use as an expansion. Create
3599 * and store an SMacro.
3600 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003601 macro_start = new_Token(NULL, TOK_STRING, NULL, 0);
3602 macro_start->text = nasm_quote(pp, len);
3603 nasm_free(pp);
3604 define_smacro(ctx, mname, casesense, 0, macro_start);
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003605 free_tlist(tline);
3606 free_tlist(origline);
3607 return DIRECTIVE_FOUND;
3608
H. Peter Anvine2c80182005-01-15 22:15:51 +00003609 case PP_SUBSTR:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003610 if (defining != NULL) return NO_DIRECTIVE_FOUND;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003611 {
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003612 int64_t start, count;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003613 size_t len;
H. Peter Anvind2456592008-06-19 15:04:18 -07003614
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003615 casesense = true;
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07003616
H. Peter Anvine2c80182005-01-15 22:15:51 +00003617 tline = tline->next;
3618 skip_white_(tline);
3619 tline = expand_id(tline);
3620 if (!tline || (tline->type != TOK_ID &&
3621 (tline->type != TOK_PREPROC_ID ||
3622 tline->text[1] != '$'))) {
3623 error(ERR_NONFATAL,
3624 "`%%substr' expects a macro identifier as first parameter");
3625 free_tlist(origline);
3626 return DIRECTIVE_FOUND;
3627 }
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08003628 ctx = get_ctx(tline->text, &mname, false);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003629 last = tline;
3630 tline = expand_smacro(tline->next);
3631 last->next = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003632
Cyrill Gorcunov35519d62010-09-06 23:49:52 +04003633 if (tline) /* skip expanded id */
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003634 t = tline->next;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003635 while (tok_type_(t, TOK_WHITESPACE))
3636 t = t->next;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003637
H. Peter Anvine2c80182005-01-15 22:15:51 +00003638 /* t should now point to the string */
Cyrill Gorcunov35519d62010-09-06 23:49:52 +04003639 if (!tok_type_(t, TOK_STRING)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00003640 error(ERR_NONFATAL,
3641 "`%%substr` requires string as second parameter");
3642 free_tlist(tline);
3643 free_tlist(origline);
3644 return DIRECTIVE_FOUND;
3645 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003646
H. Peter Anvine2c80182005-01-15 22:15:51 +00003647 tt = t->next;
3648 tptr = &tt;
3649 tokval.t_type = TOKEN_INVALID;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003650 evalresult = evaluate(ppscan, tptr, &tokval, NULL,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003651 pass, error, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003652 if (!evalresult) {
3653 free_tlist(tline);
3654 free_tlist(origline);
3655 return DIRECTIVE_FOUND;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003656 } else if (!is_simple(evalresult)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00003657 error(ERR_NONFATAL, "non-constant value given to `%%substr`");
3658 free_tlist(tline);
3659 free_tlist(origline);
3660 return DIRECTIVE_FOUND;
3661 }
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003662 start = evalresult->value - 1;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003663
3664 while (tok_type_(tt, TOK_WHITESPACE))
3665 tt = tt->next;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003666 if (!tt) {
Keith Kaniosb307a4f2010-11-06 17:41:51 -05003667 count = 1; /* Backwards compatibility: one character */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003668 } else {
3669 tokval.t_type = TOKEN_INVALID;
3670 evalresult = evaluate(ppscan, tptr, &tokval, NULL,
3671 pass, error, NULL);
3672 if (!evalresult) {
3673 free_tlist(tline);
3674 free_tlist(origline);
3675 return DIRECTIVE_FOUND;
3676 } else if (!is_simple(evalresult)) {
3677 error(ERR_NONFATAL, "non-constant value given to `%%substr`");
3678 free_tlist(tline);
3679 free_tlist(origline);
3680 return DIRECTIVE_FOUND;
3681 }
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003682 count = evalresult->value;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003683 }
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003684
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003685 len = nasm_unquote(t->text, NULL);
Cyrill Gorcunovcff031e2010-09-07 20:31:11 +04003686 /* make start and count being in range */
3687 if (start < 0)
3688 start = 0;
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003689 if (count < 0)
3690 count = len + count + 1 - start;
3691 if (start + count > (int64_t)len)
Cyrill Gorcunovcff031e2010-09-07 20:31:11 +04003692 count = len - start;
3693 if (!len || count < 0 || start >=(int64_t)len)
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003694 start = -1, count = 0; /* empty string */
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003695
Cyrill Gorcunov6b271292011-02-28 08:45:52 +03003696 macro_start = nasm_zalloc(sizeof(*macro_start));
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003697 macro_start->text = nasm_quote((start < 0) ? "" : t->text + start, count);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003698 macro_start->type = TOK_STRING;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003699
H. Peter Anvine2c80182005-01-15 22:15:51 +00003700 /*
3701 * We now have a macro name, an implicit parameter count of
3702 * zero, and a numeric token to use as an expansion. Create
3703 * and store an SMacro.
3704 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003705 define_smacro(ctx, mname, casesense, 0, macro_start);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003706 free_tlist(tline);
3707 free_tlist(origline);
3708 return DIRECTIVE_FOUND;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003709 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003710
H. Peter Anvine2c80182005-01-15 22:15:51 +00003711 case PP_ASSIGN:
3712 case PP_IASSIGN:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003713 if (defining != NULL) return NO_DIRECTIVE_FOUND;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003714 casesense = (i == PP_ASSIGN);
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07003715
H. Peter Anvine2c80182005-01-15 22:15:51 +00003716 tline = tline->next;
3717 skip_white_(tline);
3718 tline = expand_id(tline);
3719 if (!tline || (tline->type != TOK_ID &&
3720 (tline->type != TOK_PREPROC_ID ||
3721 tline->text[1] != '$'))) {
3722 error(ERR_NONFATAL,
3723 "`%%%sassign' expects a macro identifier",
3724 (i == PP_IASSIGN ? "i" : ""));
3725 free_tlist(origline);
3726 return DIRECTIVE_FOUND;
3727 }
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08003728 ctx = get_ctx(tline->text, &mname, false);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003729 last = tline;
3730 tline = expand_smacro(tline->next);
3731 last->next = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003732
H. Peter Anvine2c80182005-01-15 22:15:51 +00003733 t = tline;
3734 tptr = &t;
3735 tokval.t_type = TOKEN_INVALID;
3736 evalresult =
3737 evaluate(ppscan, tptr, &tokval, NULL, pass, error, NULL);
3738 free_tlist(tline);
3739 if (!evalresult) {
3740 free_tlist(origline);
3741 return DIRECTIVE_FOUND;
3742 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003743
H. Peter Anvine2c80182005-01-15 22:15:51 +00003744 if (tokval.t_type)
H. Peter Anvin917a3492008-09-24 09:14:49 -07003745 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +00003746 "trailing garbage after expression ignored");
H. Peter Anvin734b1882002-04-30 21:01:08 +00003747
H. Peter Anvine2c80182005-01-15 22:15:51 +00003748 if (!is_simple(evalresult)) {
3749 error(ERR_NONFATAL,
3750 "non-constant value given to `%%%sassign'",
3751 (i == PP_IASSIGN ? "i" : ""));
3752 free_tlist(origline);
3753 return DIRECTIVE_FOUND;
3754 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003755
Cyrill Gorcunov6b271292011-02-28 08:45:52 +03003756 macro_start = nasm_zalloc(sizeof(*macro_start));
H. Peter Anvine2c80182005-01-15 22:15:51 +00003757 make_tok_num(macro_start, reloc_value(evalresult));
H. Peter Anvin734b1882002-04-30 21:01:08 +00003758
H. Peter Anvine2c80182005-01-15 22:15:51 +00003759 /*
3760 * We now have a macro name, an implicit parameter count of
3761 * zero, and a numeric token to use as an expansion. Create
3762 * and store an SMacro.
3763 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003764 define_smacro(ctx, mname, casesense, 0, macro_start);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003765 free_tlist(origline);
3766 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003767
H. Peter Anvine2c80182005-01-15 22:15:51 +00003768 case PP_LINE:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003769 if (defining != NULL) return NO_DIRECTIVE_FOUND;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003770 /*
3771 * Syntax is `%line nnn[+mmm] [filename]'
3772 */
3773 tline = tline->next;
3774 skip_white_(tline);
3775 if (!tok_type_(tline, TOK_NUMBER)) {
3776 error(ERR_NONFATAL, "`%%line' expects line number");
3777 free_tlist(origline);
3778 return DIRECTIVE_FOUND;
3779 }
H. Peter Anvin70055962007-10-11 00:05:31 -07003780 k = readnum(tline->text, &err);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003781 m = 1;
3782 tline = tline->next;
3783 if (tok_is_(tline, "+")) {
3784 tline = tline->next;
3785 if (!tok_type_(tline, TOK_NUMBER)) {
3786 error(ERR_NONFATAL, "`%%line' expects line increment");
3787 free_tlist(origline);
3788 return DIRECTIVE_FOUND;
3789 }
H. Peter Anvin70055962007-10-11 00:05:31 -07003790 m = readnum(tline->text, &err);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003791 tline = tline->next;
3792 }
3793 skip_white_(tline);
3794 src_set_linnum(k);
3795 istk->lineinc = m;
3796 if (tline) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07003797 nasm_free(src_set_fname(detoken(tline, false)));
H. Peter Anvine2c80182005-01-15 22:15:51 +00003798 }
3799 free_tlist(origline);
3800 return DIRECTIVE_FOUND;
Keith Kaniosb307a4f2010-11-06 17:41:51 -05003801
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003802 case PP_WHILE:
3803 if (defining != NULL) {
3804 if (defining->type == EXP_WHILE) {
3805 defining->def_depth ++;
3806 }
3807 return NO_DIRECTIVE_FOUND;
3808 }
3809 l = NULL;
3810 if ((istk->expansion != NULL) &&
3811 (istk->expansion->emitting == false)) {
3812 j = COND_NEVER;
3813 } else {
3814 l = new_Line();
3815 l->first = copy_Token(tline->next);
3816 j = if_condition(tline->next, i);
3817 tline->next = NULL; /* it got freed */
3818 j = (((j < 0) ? COND_NEVER : j) ? COND_IF_TRUE : COND_IF_FALSE);
3819 }
3820 ed = new_ExpDef(EXP_WHILE);
3821 ed->state = j;
3822 ed->cur_depth = 1;
3823 ed->max_depth = DEADMAN_LIMIT;
3824 ed->ignoring = ((ed->state == COND_IF_TRUE) ? false : true);
3825 if (ed->ignoring == false) {
3826 ed->line = l;
3827 ed->last = l;
3828 } else if (l != NULL) {
3829 delete_Token(l->first);
3830 nasm_free(l);
3831 l = NULL;
3832 }
3833 ed->prev = defining;
3834 defining = ed;
3835 free_tlist(origline);
3836 return DIRECTIVE_FOUND;
3837
3838 case PP_ENDWHILE:
3839 if (defining != NULL) {
3840 if (defining->type == EXP_WHILE) {
3841 if (defining->def_depth > 0) {
3842 defining->def_depth --;
3843 return NO_DIRECTIVE_FOUND;
3844 }
3845 } else {
3846 return NO_DIRECTIVE_FOUND;
3847 }
3848 }
3849 if (tline->next != NULL) {
3850 error_precond(ERR_WARNING|ERR_PASS1,
3851 "trailing garbage after `%%endwhile' ignored");
3852 }
3853 if ((defining == NULL) || (defining->type != EXP_WHILE)) {
3854 error(ERR_NONFATAL, "`%%endwhile': no matching `%%while'");
3855 return DIRECTIVE_FOUND;
3856 }
3857 ed = defining;
3858 defining = ed->prev;
3859 if (ed->ignoring == false) {
3860 ed->prev = expansions;
3861 expansions = ed;
3862 ei = new_ExpInv(EXP_WHILE, ed);
3863 ei->current = ed->line->next;
3864 ei->emitting = true;
3865 ei->prev = istk->expansion;
3866 istk->expansion = ei;
3867 } else {
3868 nasm_free(ed);
3869 }
3870 free_tlist(origline);
3871 return DIRECTIVE_FOUND;
3872
3873 case PP_EXITWHILE:
3874 if (defining != NULL) return NO_DIRECTIVE_FOUND;
3875 /*
3876 * We must search along istk->expansion until we hit a
3877 * while invocation. Then we disable the emitting state(s)
3878 * between exitwhile and endwhile.
3879 */
3880 for (ei = istk->expansion; ei != NULL; ei = ei->prev) {
3881 if (ei->type == EXP_WHILE) {
3882 break;
3883 }
3884 }
3885
3886 if (ei != NULL) {
3887 /*
3888 * Set all invocations leading back to the while
3889 * invocation to a non-emitting state.
3890 */
3891 for (eei = istk->expansion; eei != ei; eei = eei->prev) {
3892 eei->emitting = false;
3893 }
3894 eei->emitting = false;
3895 eei->current = NULL;
3896 eei->def->cur_depth = eei->def->max_depth;
3897 } else {
3898 error(ERR_NONFATAL, "`%%exitwhile' not within `%%while' block");
3899 }
3900 free_tlist(origline);
3901 return DIRECTIVE_FOUND;
3902
3903 case PP_COMMENT:
3904 if (defining != NULL) {
3905 if (defining->type == EXP_COMMENT) {
3906 defining->def_depth ++;
3907 }
3908 return NO_DIRECTIVE_FOUND;
3909 }
3910 ed = new_ExpDef(EXP_COMMENT);
3911 ed->ignoring = true;
3912 ed->prev = defining;
3913 defining = ed;
3914 free_tlist(origline);
3915 return DIRECTIVE_FOUND;
3916
3917 case PP_ENDCOMMENT:
3918 if (defining != NULL) {
3919 if (defining->type == EXP_COMMENT) {
3920 if (defining->def_depth > 0) {
3921 defining->def_depth --;
3922 return NO_DIRECTIVE_FOUND;
3923 }
3924 } else {
3925 return NO_DIRECTIVE_FOUND;
3926 }
3927 }
3928 if ((defining == NULL) || (defining->type != EXP_COMMENT)) {
3929 error(ERR_NONFATAL, "`%%endcomment': no matching `%%comment'");
3930 return DIRECTIVE_FOUND;
3931 }
3932 ed = defining;
3933 defining = ed->prev;
3934 nasm_free(ed);
3935 free_tlist(origline);
3936 return DIRECTIVE_FOUND;
3937
3938 case PP_FINAL:
3939 if (defining != NULL) return NO_DIRECTIVE_FOUND;
3940 if (in_final != false) {
3941 error(ERR_FATAL, "`%%final' cannot be used recursively");
3942 }
3943 tline = tline->next;
3944 skip_white_(tline);
3945 if (tline == NULL) {
3946 error(ERR_NONFATAL, "`%%final' expects at least one parameter");
3947 } else {
3948 l = new_Line();
3949 l->first = copy_Token(tline);
3950 l->next = finals;
3951 finals = l;
3952 }
3953 free_tlist(origline);
3954 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003955
H. Peter Anvine2c80182005-01-15 22:15:51 +00003956 default:
3957 error(ERR_FATAL,
3958 "preprocessor directive `%s' not yet implemented",
H. Peter Anvin4169a472007-09-12 01:29:43 +00003959 pp_directives[i]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003960 return DIRECTIVE_FOUND;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003961 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003962}
3963
3964/*
H. Peter Anvin76690a12002-04-30 20:52:49 +00003965 * Ensure that a macro parameter contains a condition code and
3966 * nothing else. Return the condition code index if so, or -1
3967 * otherwise.
3968 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003969static int find_cc(Token * t)
H. Peter Anvineba20a72002-04-30 20:53:55 +00003970{
H. Peter Anvin76690a12002-04-30 20:52:49 +00003971 Token *tt;
3972 int i, j, k, m;
3973
H. Peter Anvin25a99342007-09-22 17:45:45 -07003974 if (!t)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003975 return -1; /* Probably a %+ without a space */
H. Peter Anvin25a99342007-09-22 17:45:45 -07003976
H. Peter Anvineba20a72002-04-30 20:53:55 +00003977 skip_white_(t);
H. Peter Anvin76690a12002-04-30 20:52:49 +00003978 if (t->type != TOK_ID)
H. Peter Anvine2c80182005-01-15 22:15:51 +00003979 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003980 tt = t->next;
H. Peter Anvineba20a72002-04-30 20:53:55 +00003981 skip_white_(tt);
H. Peter Anvin76690a12002-04-30 20:52:49 +00003982 if (tt && (tt->type != TOK_OTHER || strcmp(tt->text, ",")))
H. Peter Anvine2c80182005-01-15 22:15:51 +00003983 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003984
3985 i = -1;
Cyrill Gorcunova7319242010-06-03 22:04:36 +04003986 j = ARRAY_SIZE(conditions);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003987 while (j - i > 1) {
3988 k = (j + i) / 2;
3989 m = nasm_stricmp(t->text, conditions[k]);
3990 if (m == 0) {
3991 i = k;
3992 j = -2;
3993 break;
3994 } else if (m < 0) {
3995 j = k;
3996 } else
3997 i = k;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003998 }
3999 if (j != -2)
H. Peter Anvine2c80182005-01-15 22:15:51 +00004000 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004001 return i;
4002}
4003
Cyrill Gorcunov575d4282010-10-06 00:25:55 +04004004static bool paste_tokens(Token **head, const struct tokseq_match *m,
4005 int mnum, bool handle_paste_tokens)
H. Peter Anvind784a082009-04-20 14:01:18 -07004006{
4007 Token **tail, *t, *tt;
4008 Token **paste_head;
4009 bool did_paste = false;
4010 char *tmp;
Cyrill Gorcunov575d4282010-10-06 00:25:55 +04004011 int i;
H. Peter Anvind784a082009-04-20 14:01:18 -07004012
4013 /* Now handle token pasting... */
4014 paste_head = NULL;
4015 tail = head;
4016 while ((t = *tail) && (tt = t->next)) {
4017 switch (t->type) {
4018 case TOK_WHITESPACE:
4019 if (tt->type == TOK_WHITESPACE) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004020 /* Zap adjacent whitespace tokens */
H. Peter Anvind784a082009-04-20 14:01:18 -07004021 t->next = delete_Token(tt);
4022 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004023 /* Do not advance paste_head here */
4024 tail = &t->next;
4025 }
H. Peter Anvind784a082009-04-20 14:01:18 -07004026 break;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004027 case TOK_PASTE: /* %+ */
4028 if (handle_paste_tokens) {
4029 /* Zap %+ and whitespace tokens to the right */
4030 while (t && (t->type == TOK_WHITESPACE ||
4031 t->type == TOK_PASTE))
4032 t = *tail = delete_Token(t);
4033 if (!paste_head || !t)
4034 break; /* Nothing to paste with */
4035 tail = paste_head;
4036 t = *tail;
4037 tt = t->next;
4038 while (tok_type_(tt, TOK_WHITESPACE))
4039 tt = t->next = delete_Token(tt);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004040 if (tt) {
4041 tmp = nasm_strcat(t->text, tt->text);
4042 delete_Token(t);
4043 tt = delete_Token(tt);
4044 t = *tail = tokenize(tmp);
4045 nasm_free(tmp);
4046 while (t->next) {
4047 tail = &t->next;
4048 t = t->next;
4049 }
4050 t->next = tt; /* Attach the remaining token chain */
4051 did_paste = true;
4052 }
4053 paste_head = tail;
4054 tail = &t->next;
4055 break;
4056 }
4057 /* else fall through */
4058 default:
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +04004059 /*
4060 * Concatenation of tokens might look nontrivial
4061 * but in real it's pretty simple -- the caller
Cyrill Gorcunov575d4282010-10-06 00:25:55 +04004062 * prepares the masks of token types to be concatenated
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +04004063 * and we simply find matched sequences and slip
4064 * them together
4065 */
Cyrill Gorcunov575d4282010-10-06 00:25:55 +04004066 for (i = 0; i < mnum; i++) {
4067 if (PP_CONCAT_MASK(t->type) & m[i].mask_head) {
4068 size_t len = 0;
4069 char *tmp, *p;
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +04004070
Cyrill Gorcunov575d4282010-10-06 00:25:55 +04004071 while (tt && (PP_CONCAT_MASK(tt->type) & m[i].mask_tail)) {
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004072 len += strlen(tt->text);
4073 tt = tt->next;
Cyrill Gorcunov575d4282010-10-06 00:25:55 +04004074 }
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +04004075
Cyrill Gorcunovfdd0ac52011-06-27 01:22:27 +04004076 nasm_dump_token(tt);
4077
Cyrill Gorcunov575d4282010-10-06 00:25:55 +04004078 /*
4079 * Now tt points to the first token after
4080 * the potential paste area...
4081 */
4082 if (tt != t->next) {
4083 /* We have at least two tokens... */
4084 len += strlen(t->text);
4085 p = tmp = nasm_malloc(len+1);
4086 while (t != tt) {
4087 strcpy(p, t->text);
4088 p = strchr(p, '\0');
4089 t = delete_Token(t);
4090 }
4091 t = *tail = tokenize(tmp);
4092 nasm_free(tmp);
4093 while (t->next) {
4094 tail = &t->next;
4095 t = t->next;
4096 }
4097 t->next = tt; /* Attach the remaining token chain */
4098 did_paste = true;
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +04004099 }
Cyrill Gorcunov575d4282010-10-06 00:25:55 +04004100 paste_head = tail;
4101 tail = &t->next;
4102 break;
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +04004103 }
Cyrill Gorcunov575d4282010-10-06 00:25:55 +04004104 }
4105 if (i >= mnum) { /* no match */
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +04004106 tail = &t->next;
4107 if (!tok_type_(t->next, TOK_WHITESPACE))
4108 paste_head = tail;
4109 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004110 break;
H. Peter Anvind784a082009-04-20 14:01:18 -07004111 }
4112 }
4113 return did_paste;
4114}
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004115
4116/*
4117 * expands to a list of tokens from %{x:y}
4118 */
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004119static Token *expand_mmac_params_range(ExpInv *ei, Token *tline, Token ***last)
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004120{
4121 Token *t = tline, **tt, *tm, *head;
4122 char *pos;
4123 int fst, lst, j, i;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004124
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004125 pos = strchr(tline->text, ':');
4126 nasm_assert(pos);
4127
4128 lst = atoi(pos + 1);
4129 fst = atoi(tline->text + 1);
4130
4131 /*
4132 * only macros params are accounted so
4133 * if someone passes %0 -- we reject such
4134 * value(s)
4135 */
4136 if (lst == 0 || fst == 0)
4137 goto err;
4138
4139 /* the values should be sane */
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004140 if ((fst > (int)ei->nparam || fst < (-(int)ei->nparam)) ||
4141 (lst > (int)ei->nparam || lst < (-(int)ei->nparam)))
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004142 goto err;
4143
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004144 fst = fst < 0 ? fst + (int)ei->nparam + 1: fst;
4145 lst = lst < 0 ? lst + (int)ei->nparam + 1: lst;
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004146
4147 /* counted from zero */
4148 fst--, lst--;
4149
4150 /*
4151 * it will be at least one token
4152 */
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004153 tm = ei->params[(fst + ei->rotate) % ei->nparam];
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004154 t = new_Token(NULL, tm->type, tm->text, 0);
4155 head = t, tt = &t->next;
4156 if (fst < lst) {
4157 for (i = fst + 1; i <= lst; i++) {
4158 t = new_Token(NULL, TOK_OTHER, ",", 0);
4159 *tt = t, tt = &t->next;
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004160 j = (i + ei->rotate) % ei->nparam;
4161 tm = ei->params[j];
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004162 t = new_Token(NULL, tm->type, tm->text, 0);
4163 *tt = t, tt = &t->next;
4164 }
4165 } else {
4166 for (i = fst - 1; i >= lst; i--) {
4167 t = new_Token(NULL, TOK_OTHER, ",", 0);
4168 *tt = t, tt = &t->next;
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004169 j = (i + ei->rotate) % ei->nparam;
4170 tm = ei->params[j];
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004171 t = new_Token(NULL, tm->type, tm->text, 0);
4172 *tt = t, tt = &t->next;
4173 }
4174 }
4175
4176 *last = tt;
4177 return head;
4178
4179err:
4180 error(ERR_NONFATAL, "`%%{%s}': macro parameters out of range",
4181 &tline->text[1]);
4182 return tline;
4183}
4184
H. Peter Anvin76690a12002-04-30 20:52:49 +00004185/*
4186 * Expand MMacro-local things: parameter references (%0, %n, %+n,
H. Peter Anvin67c63722008-10-26 23:49:00 -07004187 * %-n) and MMacro-local identifiers (%%foo) as well as
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004188 * macro indirection (%[...]) and range (%{..:..}).
H. Peter Anvin76690a12002-04-30 20:52:49 +00004189 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004190static Token *expand_mmac_params(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004191{
H. Peter Anvin734b1882002-04-30 21:01:08 +00004192 Token *t, *tt, **tail, *thead;
H. Peter Anvin6125b622009-04-08 14:02:25 -07004193 bool changed = false;
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004194 char *pos;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004195
4196 tail = &thead;
4197 thead = NULL;
4198
Cyrill Gorcunov2e046002011-06-26 23:33:56 +04004199 nasm_dump_stream(tline);
4200
H. Peter Anvine2c80182005-01-15 22:15:51 +00004201 while (tline) {
4202 if (tline->type == TOK_PREPROC_ID &&
Cyrill Gorcunovca611192010-06-04 09:22:12 +04004203 (((tline->text[1] == '+' || tline->text[1] == '-') && tline->text[2]) ||
4204 (tline->text[1] >= '0' && tline->text[1] <= '9') ||
4205 tline->text[1] == '%')) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004206 char *text = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004207 int type = 0, cc; /* type = 0 to placate optimisers */
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004208 char tmpbuf[30];
H. Peter Anvin25a99342007-09-22 17:45:45 -07004209 unsigned int n;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004210 int i;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004211 ExpInv *ei;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004212
H. Peter Anvine2c80182005-01-15 22:15:51 +00004213 t = tline;
4214 tline = tline->next;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004215
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004216 for (ei = istk->expansion; ei != NULL; ei = ei->prev) {
4217 if (ei->type == EXP_MMACRO) {
4218 break;
4219 }
4220 }
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004221 if (ei == NULL) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00004222 error(ERR_NONFATAL, "`%s': not in a macro call", t->text);
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004223 } else {
4224 pos = strchr(t->text, ':');
4225 if (!pos) {
4226 switch (t->text[1]) {
4227 /*
4228 * We have to make a substitution of one of the
4229 * forms %1, %-1, %+1, %%foo, %0.
4230 */
4231 case '0':
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004232 if ((strlen(t->text) > 2) && (t->text[2] == '0')) {
4233 type = TOK_ID;
4234 text = nasm_strdup(ei->label_text);
4235 } else {
4236 type = TOK_NUMBER;
4237 snprintf(tmpbuf, sizeof(tmpbuf), "%d", ei->nparam);
4238 text = nasm_strdup(tmpbuf);
4239 }
4240 break;
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004241 case '%':
H. Peter Anvine2c80182005-01-15 22:15:51 +00004242 type = TOK_ID;
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004243 snprintf(tmpbuf, sizeof(tmpbuf), "..@%"PRIu64".",
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004244 ei->unique);
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004245 text = nasm_strcat(tmpbuf, t->text + 2);
4246 break;
4247 case '-':
4248 n = atoi(t->text + 2) - 1;
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004249 if (n >= ei->nparam)
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004250 tt = NULL;
4251 else {
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004252 if (ei->nparam > 1)
4253 n = (n + ei->rotate) % ei->nparam;
4254 tt = ei->params[n];
H. Peter Anvine2c80182005-01-15 22:15:51 +00004255 }
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004256 cc = find_cc(tt);
4257 if (cc == -1) {
4258 error(ERR_NONFATAL,
4259 "macro parameter %d is not a condition code",
4260 n + 1);
4261 text = NULL;
4262 } else {
4263 type = TOK_ID;
4264 if (inverse_ccs[cc] == -1) {
4265 error(ERR_NONFATAL,
4266 "condition code `%s' is not invertible",
4267 conditions[cc]);
4268 text = NULL;
4269 } else
4270 text = nasm_strdup(conditions[inverse_ccs[cc]]);
4271 }
4272 break;
4273 case '+':
4274 n = atoi(t->text + 2) - 1;
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004275 if (n >= ei->nparam)
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004276 tt = NULL;
4277 else {
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004278 if (ei->nparam > 1)
4279 n = (n + ei->rotate) % ei->nparam;
4280 tt = ei->params[n];
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004281 }
4282 cc = find_cc(tt);
4283 if (cc == -1) {
4284 error(ERR_NONFATAL,
4285 "macro parameter %d is not a condition code",
4286 n + 1);
4287 text = NULL;
4288 } else {
4289 type = TOK_ID;
4290 text = nasm_strdup(conditions[cc]);
4291 }
4292 break;
4293 default:
4294 n = atoi(t->text + 1) - 1;
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004295 if (n >= ei->nparam)
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004296 tt = NULL;
4297 else {
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004298 if (ei->nparam > 1)
4299 n = (n + ei->rotate) % ei->nparam;
4300 tt = ei->params[n];
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004301 }
4302 if (tt) {
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004303 for (i = 0; i < ei->paramlen[n]; i++) {
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004304 *tail = new_Token(NULL, tt->type, tt->text, 0);
4305 tail = &(*tail)->next;
4306 tt = tt->next;
4307 }
4308 }
4309 text = NULL; /* we've done it here */
4310 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004311 }
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004312 } else {
4313 /*
4314 * seems we have a parameters range here
4315 */
4316 Token *head, **last;
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004317 head = expand_mmac_params_range(ei, t, &last);
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004318 if (head != t) {
4319 *tail = head;
4320 *last = tline;
4321 tline = head;
4322 text = NULL;
4323 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00004324 }
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004325 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00004326 if (!text) {
4327 delete_Token(t);
4328 } else {
4329 *tail = t;
4330 tail = &t->next;
4331 t->type = type;
4332 nasm_free(t->text);
4333 t->text = text;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004334 t->a.mac = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004335 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004336 changed = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004337 continue;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004338 } else if (tline->type == TOK_INDIRECT) {
4339 t = tline;
4340 tline = tline->next;
4341 tt = tokenize(t->text);
4342 tt = expand_mmac_params(tt);
4343 tt = expand_smacro(tt);
4344 *tail = tt;
4345 while (tt) {
4346 tt->a.mac = NULL; /* Necessary? */
4347 tail = &tt->next;
4348 tt = tt->next;
4349 }
4350 delete_Token(t);
4351 changed = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004352 } else {
4353 t = *tail = tline;
4354 tline = tline->next;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004355 t->a.mac = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004356 tail = &t->next;
4357 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00004358 }
H. Peter Anvineba20a72002-04-30 20:53:55 +00004359 *tail = NULL;
H. Peter Anvin67c63722008-10-26 23:49:00 -07004360
Cyrill Gorcunovc6a742c2011-06-27 01:23:09 +04004361 if (changed) {
4362 const struct tokseq_match t[] = {
4363 {
4364 PP_CONCAT_MASK(TOK_ID) |
4365 PP_CONCAT_MASK(TOK_FLOAT), /* head */
4366 PP_CONCAT_MASK(TOK_ID) |
4367 PP_CONCAT_MASK(TOK_NUMBER) |
4368 PP_CONCAT_MASK(TOK_FLOAT) |
4369 PP_CONCAT_MASK(TOK_OTHER) /* tail */
4370 },
4371 {
4372 PP_CONCAT_MASK(TOK_NUMBER), /* head */
4373 PP_CONCAT_MASK(TOK_NUMBER) /* tail */
4374 }
4375 };
4376 paste_tokens(&thead, t, ARRAY_SIZE(t), false);
4377 }
H. Peter Anvin6125b622009-04-08 14:02:25 -07004378
Cyrill Gorcunov2e046002011-06-26 23:33:56 +04004379 nasm_dump_token(thead);
4380
H. Peter Anvin76690a12002-04-30 20:52:49 +00004381 return thead;
4382}
4383
4384/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004385 * Expand all single-line macro calls made in the given line.
4386 * Return the expanded version of the line. The original is deemed
4387 * to be destroyed in the process. (In reality we'll just move
4388 * Tokens from input to output a lot of the time, rather than
4389 * actually bothering to destroy and replicate.)
4390 */
H. Peter Anvincb1cf592007-11-19 12:26:50 -08004391
H. Peter Anvine2c80182005-01-15 22:15:51 +00004392static Token *expand_smacro(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004393{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004394 Token *t, *tt, *mstart, **tail, *thead;
H. Peter Anvineba20a72002-04-30 20:53:55 +00004395 SMacro *head = NULL, *m;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004396 Token **params;
4397 int *paramsize;
H. Peter Anvin25a99342007-09-22 17:45:45 -07004398 unsigned int nparam, sparam;
H. Peter Anvind784a082009-04-20 14:01:18 -07004399 int brackets;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004400 Token *org_tline = tline;
4401 Context *ctx;
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08004402 const char *mname;
H. Peter Anvin2a15e692007-11-19 13:14:59 -08004403 int deadman = DEADMAN_LIMIT;
H. Peter Anvin8287daf2009-07-07 16:00:58 -07004404 bool expanded;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004405
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004406 /*
4407 * Trick: we should avoid changing the start token pointer since it can
4408 * be contained in "next" field of other token. Because of this
4409 * we allocate a copy of first token and work with it; at the end of
4410 * routine we copy it back
4411 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004412 if (org_tline) {
Cyrill Gorcunoved4a8052010-04-09 15:40:35 +04004413 tline = new_Token(org_tline->next, org_tline->type,
4414 org_tline->text, 0);
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004415 tline->a.mac = org_tline->a.mac;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004416 nasm_free(org_tline->text);
4417 org_tline->text = NULL;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004418 }
4419
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004420 expanded = true; /* Always expand %+ at least once */
H. Peter Anvin8287daf2009-07-07 16:00:58 -07004421
H. Peter Anvincb1cf592007-11-19 12:26:50 -08004422again:
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004423 thead = NULL;
Cyrill Gorcunoved4a8052010-04-09 15:40:35 +04004424 tail = &thead;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004425
H. Peter Anvine2c80182005-01-15 22:15:51 +00004426 while (tline) { /* main token loop */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004427 if (!--deadman) {
4428 error(ERR_NONFATAL, "interminable macro recursion");
Cyrill Gorcunovbd38c8f2009-11-21 11:11:23 +03004429 goto err;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004430 }
H. Peter Anvincb1cf592007-11-19 12:26:50 -08004431
H. Peter Anvine2c80182005-01-15 22:15:51 +00004432 if ((mname = tline->text)) {
4433 /* if this token is a local macro, look in local context */
Cyrill Gorcunovc56d9ad2010-02-11 15:12:19 +03004434 if (tline->type == TOK_ID) {
4435 head = (SMacro *)hash_findix(&smacros, mname);
4436 } else if (tline->type == TOK_PREPROC_ID) {
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004437 ctx = get_ctx(mname, &mname, false);
Cyrill Gorcunovc56d9ad2010-02-11 15:12:19 +03004438 head = ctx ? (SMacro *)hash_findix(&ctx->localmac, mname) : NULL;
4439 } else
4440 head = NULL;
H. Peter Anvin072771e2008-05-22 13:17:51 -07004441
H. Peter Anvine2c80182005-01-15 22:15:51 +00004442 /*
4443 * We've hit an identifier. As in is_mmacro below, we first
4444 * check whether the identifier is a single-line macro at
4445 * all, then think about checking for parameters if
4446 * necessary.
4447 */
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04004448 list_for_each(m, head)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004449 if (!mstrcmp(m->name, mname, m->casesense))
4450 break;
4451 if (m) {
4452 mstart = tline;
4453 params = NULL;
4454 paramsize = NULL;
4455 if (m->nparam == 0) {
4456 /*
4457 * Simple case: the macro is parameterless. Discard the
4458 * one token that the macro call took, and push the
4459 * expansion back on the to-do stack.
4460 */
4461 if (!m->expansion) {
4462 if (!strcmp("__FILE__", m->name)) {
4463 int32_t num = 0;
4464 char *file = NULL;
4465 src_get(&num, &file);
4466 tline->text = nasm_quote(file, strlen(file));
4467 tline->type = TOK_STRING;
4468 nasm_free(file);
H. Peter Anvine2c80182005-01-15 22:15:51 +00004469 continue;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004470 }
4471 if (!strcmp("__LINE__", m->name)) {
4472 nasm_free(tline->text);
4473 make_tok_num(tline, src_get_linnum());
4474 continue;
4475 }
4476 if (!strcmp("__BITS__", m->name)) {
4477 nasm_free(tline->text);
4478 make_tok_num(tline, globalbits);
4479 continue;
4480 }
4481 tline = delete_Token(tline);
4482 continue;
4483 }
4484 } else {
4485 /*
4486 * Complicated case: at least one macro with this name
H. Peter Anvine2c80182005-01-15 22:15:51 +00004487 * exists and takes parameters. We must find the
4488 * parameters in the call, count them, find the SMacro
4489 * that corresponds to that form of the macro call, and
4490 * substitute for the parameters when we expand. What a
4491 * pain.
4492 */
4493 /*tline = tline->next;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004494 skip_white_(tline); */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004495 do {
4496 t = tline->next;
4497 while (tok_type_(t, TOK_SMAC_END)) {
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004498 t->a.mac->in_progress = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004499 t->text = NULL;
4500 t = tline->next = delete_Token(t);
4501 }
4502 tline = t;
4503 } while (tok_type_(tline, TOK_WHITESPACE));
4504 if (!tok_is_(tline, "(")) {
4505 /*
4506 * This macro wasn't called with parameters: ignore
4507 * the call. (Behaviour borrowed from gnu cpp.)
4508 */
4509 tline = mstart;
4510 m = NULL;
4511 } else {
4512 int paren = 0;
4513 int white = 0;
4514 brackets = 0;
4515 nparam = 0;
4516 sparam = PARAM_DELTA;
4517 params = nasm_malloc(sparam * sizeof(Token *));
4518 params[0] = tline->next;
4519 paramsize = nasm_malloc(sparam * sizeof(int));
4520 paramsize[0] = 0;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07004521 while (true) { /* parameter loop */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004522 /*
4523 * For some unusual expansions
4524 * which concatenates function call
4525 */
4526 t = tline->next;
4527 while (tok_type_(t, TOK_SMAC_END)) {
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004528 t->a.mac->in_progress = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004529 t->text = NULL;
4530 t = tline->next = delete_Token(t);
4531 }
4532 tline = t;
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00004533
H. Peter Anvine2c80182005-01-15 22:15:51 +00004534 if (!tline) {
4535 error(ERR_NONFATAL,
4536 "macro call expects terminating `)'");
4537 break;
4538 }
4539 if (tline->type == TOK_WHITESPACE
4540 && brackets <= 0) {
4541 if (paramsize[nparam])
4542 white++;
4543 else
4544 params[nparam] = tline->next;
4545 continue; /* parameter loop */
4546 }
4547 if (tline->type == TOK_OTHER
4548 && tline->text[1] == 0) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004549 char ch = tline->text[0];
H. Peter Anvine2c80182005-01-15 22:15:51 +00004550 if (ch == ',' && !paren && brackets <= 0) {
4551 if (++nparam >= sparam) {
4552 sparam += PARAM_DELTA;
4553 params = nasm_realloc(params,
Cyrill Gorcunoved4a8052010-04-09 15:40:35 +04004554 sparam * sizeof(Token *));
4555 paramsize = nasm_realloc(paramsize,
4556 sparam * sizeof(int));
H. Peter Anvine2c80182005-01-15 22:15:51 +00004557 }
4558 params[nparam] = tline->next;
4559 paramsize[nparam] = 0;
4560 white = 0;
4561 continue; /* parameter loop */
4562 }
4563 if (ch == '{' &&
4564 (brackets > 0 || (brackets == 0 &&
4565 !paramsize[nparam])))
4566 {
4567 if (!(brackets++)) {
4568 params[nparam] = tline->next;
4569 continue; /* parameter loop */
4570 }
4571 }
4572 if (ch == '}' && brackets > 0)
4573 if (--brackets == 0) {
4574 brackets = -1;
4575 continue; /* parameter loop */
4576 }
4577 if (ch == '(' && !brackets)
4578 paren++;
4579 if (ch == ')' && brackets <= 0)
4580 if (--paren < 0)
4581 break;
4582 }
4583 if (brackets < 0) {
4584 brackets = 0;
4585 error(ERR_NONFATAL, "braces do not "
4586 "enclose all of macro parameter");
4587 }
4588 paramsize[nparam] += white + 1;
4589 white = 0;
4590 } /* parameter loop */
4591 nparam++;
4592 while (m && (m->nparam != nparam ||
4593 mstrcmp(m->name, mname,
4594 m->casesense)))
4595 m = m->next;
4596 if (!m)
H. Peter Anvin917a3492008-09-24 09:14:49 -07004597 error(ERR_WARNING|ERR_PASS1|ERR_WARN_MNP,
H. Peter Anvine2c80182005-01-15 22:15:51 +00004598 "macro `%s' exists, "
4599 "but not taking %d parameters",
4600 mstart->text, nparam);
4601 }
4602 }
4603 if (m && m->in_progress)
4604 m = NULL;
4605 if (!m) { /* in progess or didn't find '(' or wrong nparam */
H. Peter Anvin70653092007-10-19 14:42:29 -07004606 /*
H. Peter Anvine2c80182005-01-15 22:15:51 +00004607 * Design question: should we handle !tline, which
4608 * indicates missing ')' here, or expand those
4609 * macros anyway, which requires the (t) test a few
H. Peter Anvin70653092007-10-19 14:42:29 -07004610 * lines down?
H. Peter Anvine2c80182005-01-15 22:15:51 +00004611 */
4612 nasm_free(params);
4613 nasm_free(paramsize);
4614 tline = mstart;
4615 } else {
4616 /*
4617 * Expand the macro: we are placed on the last token of the
4618 * call, so that we can easily split the call from the
4619 * following tokens. We also start by pushing an SMAC_END
4620 * token for the cycle removal.
4621 */
4622 t = tline;
4623 if (t) {
4624 tline = t->next;
4625 t->next = NULL;
4626 }
4627 tt = new_Token(tline, TOK_SMAC_END, NULL, 0);
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004628 tt->a.mac = m;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07004629 m->in_progress = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004630 tline = tt;
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04004631 list_for_each(t, m->expansion) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00004632 if (t->type >= TOK_SMAC_PARAM) {
4633 Token *pcopy = tline, **ptail = &pcopy;
4634 Token *ttt, *pt;
4635 int i;
H. Peter Anvin734b1882002-04-30 21:01:08 +00004636
H. Peter Anvine2c80182005-01-15 22:15:51 +00004637 ttt = params[t->type - TOK_SMAC_PARAM];
Cyrill Gorcunoved4a8052010-04-09 15:40:35 +04004638 i = paramsize[t->type - TOK_SMAC_PARAM];
4639 while (--i >= 0) {
4640 pt = *ptail = new_Token(tline, ttt->type,
4641 ttt->text, 0);
H. Peter Anvine2c80182005-01-15 22:15:51 +00004642 ptail = &pt->next;
4643 ttt = ttt->next;
4644 }
4645 tline = pcopy;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004646 } else if (t->type == TOK_PREPROC_Q) {
4647 tt = new_Token(tline, TOK_ID, mname, 0);
4648 tline = tt;
4649 } else if (t->type == TOK_PREPROC_QQ) {
4650 tt = new_Token(tline, TOK_ID, m->name, 0);
4651 tline = tt;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004652 } else {
4653 tt = new_Token(tline, t->type, t->text, 0);
4654 tline = tt;
4655 }
4656 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00004657
H. Peter Anvine2c80182005-01-15 22:15:51 +00004658 /*
4659 * Having done that, get rid of the macro call, and clean
4660 * up the parameters.
4661 */
4662 nasm_free(params);
4663 nasm_free(paramsize);
4664 free_tlist(mstart);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004665 expanded = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004666 continue; /* main token loop */
4667 }
4668 }
4669 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004670
H. Peter Anvine2c80182005-01-15 22:15:51 +00004671 if (tline->type == TOK_SMAC_END) {
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004672 tline->a.mac->in_progress = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004673 tline = delete_Token(tline);
4674 } else {
4675 t = *tail = tline;
4676 tline = tline->next;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004677 t->a.mac = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004678 t->next = NULL;
4679 tail = &t->next;
4680 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004681 }
4682
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004683 /*
4684 * Now scan the entire line and look for successive TOK_IDs that resulted
Keith Kaniosb7a89542007-04-12 02:40:54 +00004685 * after expansion (they can't be produced by tokenize()). The successive
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004686 * TOK_IDs should be concatenated.
4687 * Also we look for %+ tokens and concatenate the tokens before and after
4688 * them (without white spaces in between).
4689 */
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +04004690 if (expanded) {
Cyrill Gorcunovc6a742c2011-06-27 01:23:09 +04004691 const struct tokseq_match t[] = {
4692 {
4693 PP_CONCAT_MASK(TOK_ID) |
4694 PP_CONCAT_MASK(TOK_PREPROC_ID), /* head */
4695 PP_CONCAT_MASK(TOK_ID) |
4696 PP_CONCAT_MASK(TOK_PREPROC_ID) |
4697 PP_CONCAT_MASK(TOK_NUMBER) /* tail */
4698 }
4699 };
4700 if (paste_tokens(&thead, t, ARRAY_SIZE(t), true)) {
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +04004701 /*
4702 * If we concatenated something, *and* we had previously expanded
4703 * an actual macro, scan the lines again for macros...
4704 */
4705 tline = thead;
4706 expanded = false;
4707 goto again;
4708 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00004709 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004710
Cyrill Gorcunovbd38c8f2009-11-21 11:11:23 +03004711err:
H. Peter Anvine2c80182005-01-15 22:15:51 +00004712 if (org_tline) {
4713 if (thead) {
4714 *org_tline = *thead;
4715 /* since we just gave text to org_line, don't free it */
4716 thead->text = NULL;
4717 delete_Token(thead);
4718 } else {
4719 /* the expression expanded to empty line;
4720 we can't return NULL for some reasons
4721 we just set the line to a single WHITESPACE token. */
4722 memset(org_tline, 0, sizeof(*org_tline));
4723 org_tline->text = NULL;
4724 org_tline->type = TOK_WHITESPACE;
4725 }
4726 thead = org_tline;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004727 }
4728
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004729 return thead;
4730}
4731
4732/*
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004733 * Similar to expand_smacro but used exclusively with macro identifiers
4734 * right before they are fetched in. The reason is that there can be
4735 * identifiers consisting of several subparts. We consider that if there
4736 * are more than one element forming the name, user wants a expansion,
4737 * otherwise it will be left as-is. Example:
4738 *
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004739 * %define %$abc cde
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004740 *
4741 * the identifier %$abc will be left as-is so that the handler for %define
4742 * will suck it and define the corresponding value. Other case:
4743 *
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004744 * %define _%$abc cde
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004745 *
4746 * In this case user wants name to be expanded *before* %define starts
4747 * working, so we'll expand %$abc into something (if it has a value;
4748 * otherwise it will be left as-is) then concatenate all successive
4749 * PP_IDs into one.
4750 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004751static Token *expand_id(Token * tline)
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004752{
4753 Token *cur, *oldnext = NULL;
4754
H. Peter Anvin734b1882002-04-30 21:01:08 +00004755 if (!tline || !tline->next)
H. Peter Anvine2c80182005-01-15 22:15:51 +00004756 return tline;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004757
4758 cur = tline;
4759 while (cur->next &&
H. Peter Anvine2c80182005-01-15 22:15:51 +00004760 (cur->next->type == TOK_ID ||
4761 cur->next->type == TOK_PREPROC_ID
4762 || cur->next->type == TOK_NUMBER))
4763 cur = cur->next;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004764
4765 /* If identifier consists of just one token, don't expand */
4766 if (cur == tline)
H. Peter Anvine2c80182005-01-15 22:15:51 +00004767 return tline;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004768
H. Peter Anvine2c80182005-01-15 22:15:51 +00004769 if (cur) {
4770 oldnext = cur->next; /* Detach the tail past identifier */
4771 cur->next = NULL; /* so that expand_smacro stops here */
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004772 }
4773
H. Peter Anvin734b1882002-04-30 21:01:08 +00004774 tline = expand_smacro(tline);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004775
H. Peter Anvine2c80182005-01-15 22:15:51 +00004776 if (cur) {
4777 /* expand_smacro possibly changhed tline; re-scan for EOL */
4778 cur = tline;
4779 while (cur && cur->next)
4780 cur = cur->next;
4781 if (cur)
4782 cur->next = oldnext;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004783 }
4784
4785 return tline;
4786}
4787
4788/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004789 * Determine whether the given line constitutes a multi-line macro
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004790 * call, and return the ExpDef structure called if so. Doesn't have
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004791 * to check for an initial label - that's taken care of in
4792 * expand_mmacro - but must check numbers of parameters. Guaranteed
4793 * to be called with tline->type == TOK_ID, so the putative macro
4794 * name is easy to find.
4795 */
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004796static ExpDef *is_mmacro(Token * tline, Token *** params_array)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004797{
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004798 ExpDef *head, *ed;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004799 Token **params;
4800 int nparam;
4801
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004802 head = (ExpDef *) hash_findix(&expdefs, tline->text);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004803
4804 /*
4805 * Efficiency: first we see if any macro exists with the given
4806 * name. If not, we can return NULL immediately. _Then_ we
4807 * count the parameters, and then we look further along the
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004808 * list if necessary to find the proper ExpDef.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004809 */
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004810 list_for_each(ed, head)
4811 if (!mstrcmp(ed->name, tline->text, ed->casesense))
H. Peter Anvine2c80182005-01-15 22:15:51 +00004812 break;
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004813 if (!ed)
H. Peter Anvine2c80182005-01-15 22:15:51 +00004814 return NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004815
4816 /*
4817 * OK, we have a potential macro. Count and demarcate the
4818 * parameters.
4819 */
H. Peter Anvin734b1882002-04-30 21:01:08 +00004820 count_mmac_params(tline->next, &nparam, &params);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004821
4822 /*
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004823 * So we know how many parameters we've got. Find the ExpDef
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004824 * structure that handles this number.
4825 */
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004826 while (ed) {
4827 if (ed->nparam_min <= nparam
4828 && (ed->plus || nparam <= ed->nparam_max)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00004829 /*
4830 * It's right, and we can use it. Add its default
4831 * parameters to the end of our list if necessary.
4832 */
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004833 if (ed->defaults && nparam < ed->nparam_min + ed->ndefs) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00004834 params =
4835 nasm_realloc(params,
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004836 ((ed->nparam_min + ed->ndefs +
H. Peter Anvine2c80182005-01-15 22:15:51 +00004837 1) * sizeof(*params)));
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004838 while (nparam < ed->nparam_min + ed->ndefs) {
4839 params[nparam] = ed->defaults[nparam - ed->nparam_min];
H. Peter Anvine2c80182005-01-15 22:15:51 +00004840 nparam++;
4841 }
4842 }
4843 /*
4844 * If we've gone over the maximum parameter count (and
4845 * we're in Plus mode), ignore parameters beyond
4846 * nparam_max.
4847 */
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004848 if (ed->plus && nparam > ed->nparam_max)
4849 nparam = ed->nparam_max;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004850 /*
4851 * Then terminate the parameter list, and leave.
4852 */
4853 if (!params) { /* need this special case */
4854 params = nasm_malloc(sizeof(*params));
4855 nparam = 0;
4856 }
4857 params[nparam] = NULL;
4858 *params_array = params;
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004859 return ed;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004860 }
4861 /*
4862 * This one wasn't right: look for the next one with the
4863 * same name.
4864 */
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004865 list_for_each(ed, ed->next)
4866 if (!mstrcmp(ed->name, tline->text, ed->casesense))
H. Peter Anvine2c80182005-01-15 22:15:51 +00004867 break;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004868 }
4869
4870 /*
4871 * After all that, we didn't find one with the right number of
4872 * parameters. Issue a warning, and fail to expand the macro.
4873 */
H. Peter Anvin917a3492008-09-24 09:14:49 -07004874 error(ERR_WARNING|ERR_PASS1|ERR_WARN_MNP,
H. Peter Anvine2c80182005-01-15 22:15:51 +00004875 "macro `%s' exists, but not taking %d parameters",
4876 tline->text, nparam);
H. Peter Anvin734b1882002-04-30 21:01:08 +00004877 nasm_free(params);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004878 return NULL;
4879}
4880
4881/*
4882 * Expand the multi-line macro call made by the given line, if
4883 * there is one to be expanded. If there is, push the expansion on
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004884 * istk->expansion and return true. Otherwise return false.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004885 */
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004886static bool expand_mmacro(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004887{
H. Peter Anvineba20a72002-04-30 20:53:55 +00004888 Token *label = NULL;
4889 int dont_prepend = 0;
Cyrill Gorcunovfb27fc22011-06-25 12:08:30 +04004890 Token **params, *t;
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004891 Line *l = NULL;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004892 ExpDef *ed;
4893 ExpInv *ei;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004894 int i, nparam, *paramlen;
H. Peter Anvinc751e862008-06-09 10:18:45 -07004895 const char *mname;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004896
4897 t = tline;
H. Peter Anvineba20a72002-04-30 20:53:55 +00004898 skip_white_(t);
H. Peter Anvince2233b2008-05-25 21:57:00 -07004899 /* if (!tok_type_(t, TOK_ID)) Lino 02/25/02 */
H. Peter Anvindce1e2f2002-04-30 21:06:37 +00004900 if (!tok_type_(t, TOK_ID) && !tok_type_(t, TOK_PREPROC_ID))
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004901 return false;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004902 ed = is_mmacro(t, &params);
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004903 if (ed != NULL) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004904 mname = t->text;
H. Peter Anvinc751e862008-06-09 10:18:45 -07004905 } else {
H. Peter Anvine2c80182005-01-15 22:15:51 +00004906 Token *last;
4907 /*
4908 * We have an id which isn't a macro call. We'll assume
4909 * it might be a label; we'll also check to see if a
4910 * colon follows it. Then, if there's another id after
4911 * that lot, we'll check it again for macro-hood.
4912 */
4913 label = last = t;
4914 t = t->next;
4915 if (tok_type_(t, TOK_WHITESPACE))
4916 last = t, t = t->next;
4917 if (tok_is_(t, ":")) {
4918 dont_prepend = 1;
4919 last = t, t = t->next;
4920 if (tok_type_(t, TOK_WHITESPACE))
4921 last = t, t = t->next;
4922 }
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004923 if (!tok_type_(t, TOK_ID) || !(ed = is_mmacro(t, &params)))
4924 return false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004925 last->next = NULL;
Keith Kanios891775e2009-07-11 06:08:54 -05004926 mname = t->text;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004927 tline = t;
H. Peter Anvineba20a72002-04-30 20:53:55 +00004928 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004929
4930 /*
4931 * Fix up the parameters: this involves stripping leading and
4932 * trailing whitespace, then stripping braces if they are
4933 * present.
4934 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004935 for (nparam = 0; params[nparam]; nparam++) ;
H. Peter Anvin734b1882002-04-30 21:01:08 +00004936 paramlen = nparam ? nasm_malloc(nparam * sizeof(*paramlen)) : NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004937
H. Peter Anvine2c80182005-01-15 22:15:51 +00004938 for (i = 0; params[i]; i++) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07004939 int brace = false;
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004940 int comma = (!ed->plus || i < nparam - 1);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004941
H. Peter Anvine2c80182005-01-15 22:15:51 +00004942 t = params[i];
4943 skip_white_(t);
4944 if (tok_is_(t, "{"))
H. Peter Anvin6867acc2007-10-10 14:58:45 -07004945 t = t->next, brace = true, comma = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004946 params[i] = t;
4947 paramlen[i] = 0;
4948 while (t) {
4949 if (comma && t->type == TOK_OTHER && !strcmp(t->text, ","))
4950 break; /* ... because we have hit a comma */
4951 if (comma && t->type == TOK_WHITESPACE
4952 && tok_is_(t->next, ","))
4953 break; /* ... or a space then a comma */
4954 if (brace && t->type == TOK_OTHER && !strcmp(t->text, "}"))
4955 break; /* ... or a brace */
4956 t = t->next;
4957 paramlen[i]++;
4958 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004959 }
4960
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004961 if (ed->cur_depth >= ed->max_depth) {
4962 if (ed->max_depth > 1) {
4963 error(ERR_WARNING,
4964 "reached maximum macro recursion depth of %i for %s",
4965 ed->max_depth,ed->name);
4966 }
4967 return false;
4968 } else {
4969 ed->cur_depth ++;
4970 }
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004971
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004972 /*
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004973 * OK, we have found a ExpDef structure representing a
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004974 * previously defined mmacro. Create an expansion invocation
4975 * and point it back to the expansion definition. Substitution of
H. Peter Anvin76690a12002-04-30 20:52:49 +00004976 * parameter tokens and macro-local tokens doesn't get done
4977 * until the single-line macro substitution process; this is
4978 * because delaying them allows us to change the semantics
4979 * later through %rotate.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004980 */
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004981 ei = new_ExpInv(EXP_MMACRO, ed);
4982 ei->name = nasm_strdup(mname);
4983 //ei->label = label;
4984 //ei->label_text = detoken(label, false);
4985 ei->current = ed->line;
4986 ei->emitting = true;
4987 //ei->iline = tline;
4988 ei->params = params;
4989 ei->nparam = nparam;
4990 ei->rotate = 0;
4991 ei->paramlen = paramlen;
4992 ei->lineno = 0;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004993
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004994 ei->prev = istk->expansion;
4995 istk->expansion = ei;
4996
4997 /*
4998 * Special case: detect %00 on first invocation; if found,
4999 * avoid emitting any labels that precede the mmacro call.
5000 * ed->prepend is set to -1 when %00 is detected, else 1.
5001 */
5002 if (ed->prepend == 0) {
5003 for (l = ed->line; l != NULL; l = l->next) {
5004 for (t = l->first; t != NULL; t = t->next) {
5005 if ((t->type == TOK_PREPROC_ID) &&
5006 (strlen(t->text) == 3) &&
5007 (t->text[1] == '0') && (t->text[2] == '0')) {
5008 dont_prepend = -1;
5009 break;
5010 }
5011 }
5012 if (dont_prepend < 0) {
5013 break;
5014 }
5015 }
5016 ed->prepend = ((dont_prepend < 0) ? -1 : 1);
5017 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005018
5019 /*
H. Peter Anvineba20a72002-04-30 20:53:55 +00005020 * If we had a label, push it on as the first line of
5021 * the macro expansion.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005022 */
Keith Kaniosb307a4f2010-11-06 17:41:51 -05005023 if (label != NULL) {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005024 if (ed->prepend < 0) {
5025 ei->label_text = detoken(label, false);
5026 } else {
5027 if (dont_prepend == 0) {
5028 t = label;
5029 while (t->next != NULL) {
5030 t = t->next;
5031 }
5032 t->next = new_Token(NULL, TOK_OTHER, ":", 0);
5033 }
5034 l = new_Line();
5035 l->first = copy_Token(label);
5036 l->next = ei->current;
5037 ei->current = l;
5038 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005039 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005040
Keith Kaniosb307a4f2010-11-06 17:41:51 -05005041 list->uplevel(ed->nolist ? LIST_MACRO_NOLIST : LIST_MACRO);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005042
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005043 istk->mmac_depth++;
Keith Kaniosb307a4f2010-11-06 17:41:51 -05005044 return true;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005045}
5046
Victor van den Elzen3b404c02008-09-18 13:51:36 +02005047/* The function that actually does the error reporting */
5048static void verror(int severity, const char *fmt, va_list arg)
5049{
5050 char buff[1024];
5051
5052 vsnprintf(buff, sizeof(buff), fmt, arg);
5053
Cyrill Gorcunov55cc4d02010-11-10 23:12:06 +03005054 if (istk && istk->mmac_depth > 0) {
5055 ExpInv *ei = istk->expansion;
5056 int lineno = ei->lineno;
5057 while (ei) {
5058 if (ei->type == EXP_MMACRO)
5059 break;
5060 lineno += ei->relno;
5061 ei = ei->prev;
5062 }
Keith Kaniosb307a4f2010-11-06 17:41:51 -05005063 nasm_error(severity, "(%s:%d) %s", ei->def->name,
Cyrill Gorcunov55cc4d02010-11-10 23:12:06 +03005064 lineno, buff);
5065 } else
H. Peter Anvindbb640b2009-07-18 18:57:16 -07005066 nasm_error(severity, "%s", buff);
Victor van den Elzen3b404c02008-09-18 13:51:36 +02005067}
5068
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005069/*
5070 * Since preprocessor always operate only on the line that didn't
H. Peter Anvin917a3492008-09-24 09:14:49 -07005071 * arrived yet, we should always use ERR_OFFBY1.
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005072 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +00005073static void error(int severity, const char *fmt, ...)
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005074{
5075 va_list arg;
H. Peter Anvin734b1882002-04-30 21:01:08 +00005076 va_start(arg, fmt);
Victor van den Elzen3b404c02008-09-18 13:51:36 +02005077 verror(severity, fmt, arg);
H. Peter Anvin734b1882002-04-30 21:01:08 +00005078 va_end(arg);
Victor van den Elzen3b404c02008-09-18 13:51:36 +02005079}
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005080
Victor van den Elzen3b404c02008-09-18 13:51:36 +02005081/*
5082 * Because %else etc are evaluated in the state context
5083 * of the previous branch, errors might get lost with error():
5084 * %if 0 ... %else trailing garbage ... %endif
5085 * So %else etc should report errors with this function.
5086 */
5087static void error_precond(int severity, const char *fmt, ...)
5088{
5089 va_list arg;
5090
5091 /* Only ignore the error if it's really in a dead branch */
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005092 if ((istk != NULL) &&
5093 (istk->expansion != NULL) &&
5094 (istk->expansion->type == EXP_IF) &&
5095 (istk->expansion->def->state == COND_NEVER))
Victor van den Elzen3b404c02008-09-18 13:51:36 +02005096 return;
5097
5098 va_start(arg, fmt);
5099 verror(severity, fmt, arg);
5100 va_end(arg);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005101}
5102
H. Peter Anvin734b1882002-04-30 21:01:08 +00005103static void
H. Peter Anvindbb640b2009-07-18 18:57:16 -07005104pp_reset(char *file, int apass, ListGen * listgen, StrList **deplist)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005105{
H. Peter Anvin7383b402008-09-24 10:20:40 -07005106 Token *t;
5107
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005108 cstk = NULL;
Cyrill Gorcunov6b271292011-02-28 08:45:52 +03005109 istk = nasm_zalloc(sizeof(Include));
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005110 istk->fp = fopen(file, "r");
H. Peter Anvineba20a72002-04-30 20:53:55 +00005111 src_set_fname(nasm_strdup(file));
5112 src_set_linnum(0);
5113 istk->lineinc = 1;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005114 if (!istk->fp)
H. Peter Anvin917a3492008-09-24 09:14:49 -07005115 error(ERR_FATAL|ERR_NOFILE, "unable to open input file `%s'",
H. Peter Anvine2c80182005-01-15 22:15:51 +00005116 file);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005117 defining = NULL;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005118 finals = NULL;
5119 in_final = false;
Charles Crayned4200be2008-07-12 16:42:33 -07005120 nested_mac_count = 0;
5121 nested_rep_count = 0;
H. Peter Anvin97a23472007-09-16 17:57:25 -07005122 init_macros();
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005123 unique = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005124 if (tasm_compatible_mode) {
H. Peter Anvina4835d42008-05-20 14:21:29 -07005125 stdmacpos = nasm_stdmac;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005126 } else {
H. Peter Anvina4835d42008-05-20 14:21:29 -07005127 stdmacpos = nasm_stdmac_after_tasm;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005128 }
H. Peter Anvind2456592008-06-19 15:04:18 -07005129 any_extrastdmac = extrastdmac && *extrastdmac;
5130 do_predef = true;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005131 list = listgen;
H. Peter Anvin61f130f2008-09-25 15:45:06 -07005132
5133 /*
5134 * 0 for dependencies, 1 for preparatory passes, 2 for final pass.
5135 * The caller, however, will also pass in 3 for preprocess-only so
5136 * we can set __PASS__ accordingly.
5137 */
5138 pass = apass > 2 ? 2 : apass;
5139
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07005140 dephead = deptail = deplist;
5141 if (deplist) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03005142 StrList *sl = nasm_malloc(strlen(file)+1+sizeof sl->next);
5143 sl->next = NULL;
5144 strcpy(sl->str, file);
5145 *deptail = sl;
5146 deptail = &sl->next;
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07005147 }
H. Peter Anvin7383b402008-09-24 10:20:40 -07005148
H. Peter Anvin61f130f2008-09-25 15:45:06 -07005149 /*
5150 * Define the __PASS__ macro. This is defined here unlike
5151 * all the other builtins, because it is special -- it varies between
5152 * passes.
5153 */
Cyrill Gorcunov6b271292011-02-28 08:45:52 +03005154 t = nasm_zalloc(sizeof(*t));
H. Peter Anvin61f130f2008-09-25 15:45:06 -07005155 make_tok_num(t, apass);
H. Peter Anvin7383b402008-09-24 10:20:40 -07005156 define_smacro(NULL, "__PASS__", true, 0, t);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005157}
5158
Keith Kaniosa6dfa782007-04-13 16:47:53 +00005159static char *pp_getline(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005160{
Keith Kaniosa6dfa782007-04-13 16:47:53 +00005161 char *line;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005162 Token *tline;
Keith Kanios9858cec2010-11-08 00:58:02 -06005163 ExpDef *ed;
5164 ExpInv *ei;
5165 Line *l;
5166 int j;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005167
H. Peter Anvine2c80182005-01-15 22:15:51 +00005168 while (1) {
5169 /*
Keith Kaniosb307a4f2010-11-06 17:41:51 -05005170 * Fetch a tokenized line, either from the expansion
H. Peter Anvine2c80182005-01-15 22:15:51 +00005171 * buffer or from the input file.
5172 */
5173 tline = NULL;
H. Peter Anvineba20a72002-04-30 20:53:55 +00005174
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005175 while (1) { /* until we get a line we can use */
5176 /*
5177 * Fetch a tokenized line from the expansion buffer
5178 */
5179 if (istk->expansion != NULL) {
5180 ei = istk->expansion;
5181 if (ei->current != NULL) {
5182 if (ei->emitting == false) {
5183 ei->current = NULL;
5184 continue;
5185 }
5186 l = ei->current;
5187 ei->current = l->next;
5188 ei->lineno++;
5189 tline = copy_Token(l->first);
5190 if (((ei->type == EXP_REP) ||
5191 (ei->type == EXP_MMACRO) ||
5192 (ei->type == EXP_WHILE))
5193 && (ei->def->nolist == false)) {
5194 char *p = detoken(tline, false);
5195 list->line(LIST_MACRO, p);
5196 nasm_free(p);
5197 }
5198 if (ei->linnum > -1) {
5199 src_set_linnum(src_get_linnum() + 1);
5200 }
5201 break;
5202 } else if ((ei->type == EXP_REP) &&
5203 (ei->def->cur_depth < ei->def->max_depth)) {
5204 ei->def->cur_depth ++;
5205 ei->current = ei->def->line;
5206 ei->lineno = 0;
5207 continue;
5208 } else if ((ei->type == EXP_WHILE) &&
5209 (ei->def->cur_depth < ei->def->max_depth)) {
5210 ei->current = ei->def->line;
5211 ei->lineno = 0;
5212 tline = copy_Token(ei->current->first);
5213 j = if_condition(tline, PP_WHILE);
5214 tline = NULL;
5215 j = (((j < 0) ? COND_NEVER : j) ? COND_IF_TRUE : COND_IF_FALSE);
5216 if (j == COND_IF_TRUE) {
5217 ei->current = ei->current->next;
5218 ei->def->cur_depth ++;
5219 } else {
5220 ei->emitting = false;
5221 ei->current = NULL;
5222 ei->def->cur_depth = ei->def->max_depth;
5223 }
5224 continue;
5225 } else {
5226 istk->expansion = ei->prev;
5227 ed = ei->def;
5228 if (ed != NULL) {
5229 if ((ei->emitting == true) &&
5230 (ed->max_depth == DEADMAN_LIMIT) &&
5231 (ed->cur_depth == DEADMAN_LIMIT)
5232 ) {
5233 error(ERR_FATAL, "runaway expansion detected, aborting");
5234 }
5235 if (ed->cur_depth > 0) {
5236 ed->cur_depth --;
Keith Kanios94124652010-12-18 11:47:28 -06005237 } else if (ed->type != EXP_MMACRO) {
Keith Kanios6a7c3e92010-12-18 11:49:53 -06005238 expansions = ed->prev;
5239 free_expdef(ed);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005240 }
5241 if ((ei->type == EXP_REP) ||
5242 (ei->type == EXP_MMACRO) ||
5243 (ei->type == EXP_WHILE)) {
5244 list->downlevel(LIST_MACRO);
5245 if (ei->type == EXP_MMACRO) {
5246 istk->mmac_depth--;
5247 }
5248 }
5249 }
5250 if (ei->linnum > -1) {
5251 src_set_linnum(ei->linnum);
5252 }
5253 free_expinv(ei);
5254 continue;
5255 }
5256 }
5257
5258 /*
5259 * Read in line from input and tokenize
5260 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00005261 line = read_line();
5262 if (line) { /* from the current input file */
5263 line = prepreproc(line);
Keith Kaniosb7a89542007-04-12 02:40:54 +00005264 tline = tokenize(line);
H. Peter Anvine2c80182005-01-15 22:15:51 +00005265 nasm_free(line);
5266 break;
5267 }
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005268
H. Peter Anvine2c80182005-01-15 22:15:51 +00005269 /*
5270 * The current file has ended; work down the istk
5271 */
5272 {
5273 Include *i = istk;
5274 fclose(i->fp);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005275 if (i->expansion != NULL) {
5276 error(ERR_FATAL,
5277 "end of file while still in an expansion");
5278 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00005279 /* only set line and file name if there's a next node */
5280 if (i->next) {
5281 src_set_linnum(i->lineno);
Cyrill Gorcunovd34a1082011-03-07 11:23:08 +03005282 nasm_free(src_set_fname(nasm_strdup(i->fname)));
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03005283 }
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005284 if ((i->next == NULL) && (finals != NULL)) {
5285 in_final = true;
5286 ei = new_ExpInv(EXP_FINAL, NULL);
5287 ei->emitting = true;
5288 ei->current = finals;
5289 istk->expansion = ei;
5290 finals = NULL;
5291 continue;
5292 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00005293 istk = i->next;
5294 list->downlevel(LIST_INCLUDE);
5295 nasm_free(i);
Keith Kaniosb307a4f2010-11-06 17:41:51 -05005296 if (istk == NULL) {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005297 if (finals != NULL) {
5298 in_final = true;
5299 } else {
5300 return NULL;
5301 }
5302 }
5303 continue;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005304 }
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005305 }
5306
5307 if (defining == NULL) {
5308 tline = expand_mmac_params(tline);
5309 }
5310
H. Peter Anvine2c80182005-01-15 22:15:51 +00005311 /*
5312 * Check the line to see if it's a preprocessor directive.
5313 */
5314 if (do_directive(tline) == DIRECTIVE_FOUND) {
5315 continue;
Keith Kaniosb307a4f2010-11-06 17:41:51 -05005316 } else if (defining != NULL) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00005317 /*
Keith Kaniosb307a4f2010-11-06 17:41:51 -05005318 * We're defining an expansion. We emit nothing at all,
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005319 * and just shove the tokenized line on to the definition.
H. Peter Anvine2c80182005-01-15 22:15:51 +00005320 */
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005321 if (defining->ignoring == false) {
5322 Line *l = new_Line();
5323 l->first = tline;
5324 if (defining->line == NULL) {
5325 defining->line = l;
5326 defining->last = l;
5327 } else {
5328 defining->last->next = l;
5329 defining->last = l;
5330 }
5331 } else {
Keith Kanios104803d2010-12-18 11:05:46 -06005332 free_tlist(tline);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005333 }
5334 defining->linecount++;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005335 continue;
Keith Kaniosb307a4f2010-11-06 17:41:51 -05005336 } else if ((istk->expansion != NULL) &&
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005337 (istk->expansion->emitting != true)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00005338 /*
Keith Kaniosb307a4f2010-11-06 17:41:51 -05005339 * We're in a non-emitting branch of an expansion.
H. Peter Anvine2c80182005-01-15 22:15:51 +00005340 * Emit nothing at all, not even a blank line: when we
Keith Kaniosb307a4f2010-11-06 17:41:51 -05005341 * emerge from the expansion we'll give a line-number
H. Peter Anvine2c80182005-01-15 22:15:51 +00005342 * directive so we keep our place correctly.
5343 */
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005344 free_tlist(tline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00005345 continue;
5346 } else {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005347 tline = expand_smacro(tline);
Keith Kaniosb307a4f2010-11-06 17:41:51 -05005348 if (expand_mmacro(tline) != true) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00005349 /*
Keith Kaniosb7a89542007-04-12 02:40:54 +00005350 * De-tokenize the line again, and emit it.
H. Peter Anvine2c80182005-01-15 22:15:51 +00005351 */
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005352 line = detoken(tline, true);
5353 free_tlist(tline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00005354 break;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005355 } else {
5356 continue;
5357 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00005358 }
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005359 }
5360 return line;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005361}
5362
H. Peter Anvine2c80182005-01-15 22:15:51 +00005363static void pp_cleanup(int pass)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005364{
Keith Kaniosb307a4f2010-11-06 17:41:51 -05005365 if (defining != NULL) {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005366 error(ERR_NONFATAL, "end of file while still defining an expansion");
5367 while (defining != NULL) {
5368 ExpDef *ed = defining;
5369 defining = ed->prev;
5370 free_expdef(ed);
5371 }
5372 defining = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005373 }
Keith Kaniosb307a4f2010-11-06 17:41:51 -05005374 while (cstk != NULL)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005375 ctx_pop();
H. Peter Anvin97a23472007-09-16 17:57:25 -07005376 free_macros();
Keith Kaniosb307a4f2010-11-06 17:41:51 -05005377 while (istk != NULL) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00005378 Include *i = istk;
5379 istk = istk->next;
5380 fclose(i->fp);
5381 nasm_free(i->fname);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005382 while (i->expansion != NULL) {
5383 ExpInv *ei = i->expansion;
5384 i->expansion = ei->prev;
5385 free_expinv(ei);
5386 }
Cyrill Gorcunov8dcfd882011-03-03 09:18:56 +03005387 nasm_free(i);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005388 }
5389 while (cstk)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005390 ctx_pop();
H. Peter Anvin86877b22008-06-20 15:55:45 -07005391 nasm_free(src_set_fname(NULL));
H. Peter Anvine2c80182005-01-15 22:15:51 +00005392 if (pass == 0) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03005393 IncPath *i;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005394 free_llist(predef);
5395 delete_Blocks();
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03005396 while ((i = ipath)) {
5397 ipath = i->next;
5398 if (i->path)
5399 nasm_free(i->path);
5400 nasm_free(i);
5401 }
H. Peter Anvin11dfa1a2008-07-02 18:11:04 -07005402 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005403}
5404
Keith Kaniosa6dfa782007-04-13 16:47:53 +00005405void pp_include_path(char *path)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005406{
Cyrill Gorcunov6b271292011-02-28 08:45:52 +03005407 IncPath *i = nasm_zalloc(sizeof(IncPath));
H. Peter Anvin37a321f2007-09-24 13:41:58 -07005408
Cyrill Gorcunov6b271292011-02-28 08:45:52 +03005409 if (path)
5410 i->path = nasm_strdup(path);
Frank Kotlerd0ed6fd2003-08-27 11:33:56 +00005411
H. Peter Anvin89cee572009-07-15 09:16:54 -04005412 if (ipath) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00005413 IncPath *j = ipath;
H. Peter Anvin89cee572009-07-15 09:16:54 -04005414 while (j->next)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005415 j = j->next;
5416 j->next = i;
5417 } else {
5418 ipath = i;
Frank Kotlerd0ed6fd2003-08-27 11:33:56 +00005419 }
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005420}
Frank Kotlerd0ed6fd2003-08-27 11:33:56 +00005421
Keith Kaniosa6dfa782007-04-13 16:47:53 +00005422void pp_pre_include(char *fname)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005423{
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005424 Token *inc, *space, *name;
5425 Line *l;
5426
H. Peter Anvin734b1882002-04-30 21:01:08 +00005427 name = new_Token(NULL, TOK_INTERNAL_STRING, fname, 0);
5428 space = new_Token(name, TOK_WHITESPACE, NULL, 0);
5429 inc = new_Token(space, TOK_PREPROC_ID, "%include", 0);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005430
Keith Kaniosb307a4f2010-11-06 17:41:51 -05005431 l = new_Line();
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005432 l->next = predef;
5433 l->first = inc;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005434 predef = l;
5435}
5436
Keith Kaniosa6dfa782007-04-13 16:47:53 +00005437void pp_pre_define(char *definition)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005438{
5439 Token *def, *space;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005440 Line *l;
Keith Kaniosa6dfa782007-04-13 16:47:53 +00005441 char *equals;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005442
5443 equals = strchr(definition, '=');
H. Peter Anvin734b1882002-04-30 21:01:08 +00005444 space = new_Token(NULL, TOK_WHITESPACE, NULL, 0);
5445 def = new_Token(space, TOK_PREPROC_ID, "%define", 0);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005446 if (equals)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005447 *equals = ' ';
Keith Kaniosb7a89542007-04-12 02:40:54 +00005448 space->next = tokenize(definition);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005449 if (equals)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005450 *equals = '=';
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005451
Keith Kaniosb307a4f2010-11-06 17:41:51 -05005452 l = new_Line();
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005453 l->next = predef;
5454 l->first = def;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005455 predef = l;
5456}
5457
Keith Kaniosa6dfa782007-04-13 16:47:53 +00005458void pp_pre_undefine(char *definition)
H. Peter Anvin620515a2002-04-30 20:57:38 +00005459{
5460 Token *def, *space;
5461 Line *l;
5462
H. Peter Anvin734b1882002-04-30 21:01:08 +00005463 space = new_Token(NULL, TOK_WHITESPACE, NULL, 0);
5464 def = new_Token(space, TOK_PREPROC_ID, "%undef", 0);
Keith Kaniosb7a89542007-04-12 02:40:54 +00005465 space->next = tokenize(definition);
H. Peter Anvin620515a2002-04-30 20:57:38 +00005466
Keith Kaniosb307a4f2010-11-06 17:41:51 -05005467 l = new_Line();
H. Peter Anvin620515a2002-04-30 20:57:38 +00005468 l->next = predef;
5469 l->first = def;
H. Peter Anvin620515a2002-04-30 20:57:38 +00005470 predef = l;
5471}
5472
Keith Kaniosb7a89542007-04-12 02:40:54 +00005473/*
Keith Kaniosb7a89542007-04-12 02:40:54 +00005474 * This function is used to assist with "runtime" preprocessor
Keith Kaniosb307a4f2010-11-06 17:41:51 -05005475 * directives, e.g. pp_runtime("%define __BITS__ 64");
Keith Kaniosb7a89542007-04-12 02:40:54 +00005476 *
5477 * ERRORS ARE IGNORED HERE, SO MAKE COMPLETELY SURE THAT YOU
5478 * PASS A VALID STRING TO THIS FUNCTION!!!!!
5479 */
5480
Keith Kaniosa6dfa782007-04-13 16:47:53 +00005481void pp_runtime(char *definition)
Keith Kaniosb7a89542007-04-12 02:40:54 +00005482{
5483 Token *def;
H. Peter Anvin70653092007-10-19 14:42:29 -07005484
Keith Kaniosb7a89542007-04-12 02:40:54 +00005485 def = tokenize(definition);
H. Peter Anvin89cee572009-07-15 09:16:54 -04005486 if (do_directive(def) == NO_DIRECTIVE_FOUND)
Keith Kaniosb7a89542007-04-12 02:40:54 +00005487 free_tlist(def);
H. Peter Anvin70653092007-10-19 14:42:29 -07005488
Keith Kaniosb7a89542007-04-12 02:40:54 +00005489}
5490
H. Peter Anvina70547f2008-07-19 21:44:26 -07005491void pp_extra_stdmac(macros_t *macros)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005492{
H. Peter Anvin76690a12002-04-30 20:52:49 +00005493 extrastdmac = macros;
5494}
5495
Keith Kaniosa5fc6462007-10-13 07:09:22 -07005496static void make_tok_num(Token * tok, int64_t val)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005497{
Keith Kaniosa6dfa782007-04-13 16:47:53 +00005498 char numbuf[20];
Keith Kaniosa5fc6462007-10-13 07:09:22 -07005499 snprintf(numbuf, sizeof(numbuf), "%"PRId64"", val);
H. Peter Anvineba20a72002-04-30 20:53:55 +00005500 tok->text = nasm_strdup(numbuf);
5501 tok->type = TOK_NUMBER;
5502}
5503
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005504Preproc nasmpp = {
5505 pp_reset,
5506 pp_getline,
5507 pp_cleanup
5508};