blob: 08f32eddb0f758a5450769576064b27bd98bc807 [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 Gorcunovb6c6ca92011-06-28 01:33:02 +0400706 nasm_free(ei->name);
707 nasm_free(ei->label_text);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +0300708 nasm_free(ei);
H. Peter Anvineba20a72002-04-30 20:53:55 +0000709}
710
711/*
H. Peter Anvin97a23472007-09-16 17:57:25 -0700712 * Free all currently defined macros, and free the hash tables
713 */
H. Peter Anvin072771e2008-05-22 13:17:51 -0700714static void free_smacro_table(struct hash_table *smt)
H. Peter Anvin97a23472007-09-16 17:57:25 -0700715{
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +0400716 SMacro *s, *tmp;
H. Peter Anvin072771e2008-05-22 13:17:51 -0700717 const char *key;
718 struct hash_tbl_node *it = NULL;
H. Peter Anvin97a23472007-09-16 17:57:25 -0700719
H. Peter Anvin072771e2008-05-22 13:17:51 -0700720 while ((s = hash_iterate(smt, &it, &key)) != NULL) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300721 nasm_free((void *)key);
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +0400722 list_for_each_safe(s, tmp, s) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300723 nasm_free(s->name);
724 free_tlist(s->expansion);
725 nasm_free(s);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300726 }
H. Peter Anvin97a23472007-09-16 17:57:25 -0700727 }
H. Peter Anvin072771e2008-05-22 13:17:51 -0700728 hash_free(smt);
729}
730
Keith Kaniosb307a4f2010-11-06 17:41:51 -0500731static void free_expdef_table(struct hash_table *edt)
H. Peter Anvin072771e2008-05-22 13:17:51 -0700732{
Keith Kaniosb307a4f2010-11-06 17:41:51 -0500733 ExpDef *ed, *tmp;
H. Peter Anvin072771e2008-05-22 13:17:51 -0700734 const char *key;
735 struct hash_tbl_node *it = NULL;
H. Peter Anvin97a23472007-09-16 17:57:25 -0700736
737 it = NULL;
Keith Kaniosb307a4f2010-11-06 17:41:51 -0500738 while ((ed = hash_iterate(edt, &it, &key)) != NULL) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300739 nasm_free((void *)key);
Keith Kaniosb307a4f2010-11-06 17:41:51 -0500740 list_for_each_safe(ed ,tmp, ed)
741 free_expdef(ed);
H. Peter Anvin97a23472007-09-16 17:57:25 -0700742 }
Keith Kaniosb307a4f2010-11-06 17:41:51 -0500743 hash_free(edt);
H. Peter Anvin072771e2008-05-22 13:17:51 -0700744}
745
746static void free_macros(void)
747{
H. Peter Anvin166c2472008-05-28 12:28:58 -0700748 free_smacro_table(&smacros);
Keith Kaniosb307a4f2010-11-06 17:41:51 -0500749 free_expdef_table(&expdefs);
H. Peter Anvin97a23472007-09-16 17:57:25 -0700750}
751
752/*
753 * Initialize the hash tables
754 */
755static void init_macros(void)
756{
H. Peter Anvin166c2472008-05-28 12:28:58 -0700757 hash_init(&smacros, HASH_LARGE);
Keith Kaniosb307a4f2010-11-06 17:41:51 -0500758 hash_init(&expdefs, HASH_LARGE);
H. Peter Anvin97a23472007-09-16 17:57:25 -0700759}
760
761/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000762 * Pop the context stack.
763 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000764static void ctx_pop(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000765{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000766 Context *c = cstk;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000767
768 cstk = cstk->next;
H. Peter Anvin166c2472008-05-28 12:28:58 -0700769 free_smacro_table(&c->localmac);
H. Peter Anvin734b1882002-04-30 21:01:08 +0000770 nasm_free(c->name);
771 nasm_free(c);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000772}
773
H. Peter Anvin072771e2008-05-22 13:17:51 -0700774/*
775 * Search for a key in the hash index; adding it if necessary
776 * (in which case we initialize the data pointer to NULL.)
777 */
778static void **
779hash_findi_add(struct hash_table *hash, const char *str)
780{
781 struct hash_insert hi;
782 void **r;
783 char *strx;
784
785 r = hash_findi(hash, str, &hi);
786 if (r)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300787 return r;
H. Peter Anvin072771e2008-05-22 13:17:51 -0700788
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300789 strx = nasm_strdup(str); /* Use a more efficient allocator here? */
H. Peter Anvin072771e2008-05-22 13:17:51 -0700790 return hash_add(&hi, strx, NULL);
791}
792
793/*
794 * Like hash_findi, but returns the data element rather than a pointer
795 * to it. Used only when not adding a new element, hence no third
796 * argument.
797 */
798static void *
799hash_findix(struct hash_table *hash, const char *str)
800{
801 void **p;
802
803 p = hash_findi(hash, str, NULL);
804 return p ? *p : NULL;
805}
806
Cyrill Gorcunov15bdc512010-07-13 11:27:41 +0400807/*
Keith Kaniosb307a4f2010-11-06 17:41:51 -0500808 * read line from standard macros set,
Cyrill Gorcunov15bdc512010-07-13 11:27:41 +0400809 * if there no more left -- return NULL
810 */
811static char *line_from_stdmac(void)
812{
813 unsigned char c;
814 const unsigned char *p = stdmacpos;
815 char *line, *q;
816 size_t len = 0;
817
818 if (!stdmacpos)
819 return NULL;
820
821 while ((c = *p++)) {
822 if (c >= 0x80)
823 len += pp_directives_len[c - 0x80] + 1;
824 else
825 len++;
826 }
827
828 line = nasm_malloc(len + 1);
829 q = line;
830 while ((c = *stdmacpos++)) {
831 if (c >= 0x80) {
832 memcpy(q, pp_directives[c - 0x80], pp_directives_len[c - 0x80]);
833 q += pp_directives_len[c - 0x80];
834 *q++ = ' ';
835 } else {
836 *q++ = c;
837 }
838 }
839 stdmacpos = p;
840 *q = '\0';
841
842 if (!*stdmacpos) {
843 /* This was the last of the standard macro chain... */
844 stdmacpos = NULL;
845 if (any_extrastdmac) {
846 stdmacpos = extrastdmac;
847 any_extrastdmac = false;
848 } else if (do_predef) {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +0300849 ExpInv *ei;
Cyrill Gorcunov15bdc512010-07-13 11:27:41 +0400850 Line *pd, *l;
851 Token *head, **tail, *t;
852
853 /*
854 * Nasty hack: here we push the contents of
855 * `predef' on to the top-level expansion stack,
856 * since this is the most convenient way to
857 * implement the pre-include and pre-define
858 * features.
859 */
860 list_for_each(pd, predef) {
861 head = NULL;
862 tail = &head;
863 list_for_each(t, pd->first) {
864 *tail = new_Token(NULL, t->type, t->text, 0);
865 tail = &(*tail)->next;
866 }
867
Keith Kaniosb307a4f2010-11-06 17:41:51 -0500868 l = new_Line();
869 l->first = head;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +0300870 ei = new_ExpInv(EXP_PREDEF, NULL);
871 ei->current = l;
872 ei->emitting = true;
873 ei->prev = istk->expansion;
Keith Kaniosb307a4f2010-11-06 17:41:51 -0500874 istk->expansion = ei;
Cyrill Gorcunov15bdc512010-07-13 11:27:41 +0400875 }
876 do_predef = false;
877 }
878 }
879
880 return line;
881}
882
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000883#define BUF_DELTA 512
884/*
885 * Read a line from the top file in istk, handling multiple CR/LFs
886 * at the end of the line read, and handling spurious ^Zs. Will
887 * return lines from the standard macro set if this has not already
888 * been done.
889 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000890static char *read_line(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000891{
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000892 char *buffer, *p, *q;
H. Peter Anvin9f394642002-04-30 21:07:51 +0000893 int bufsize, continued_count;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000894
Cyrill Gorcunov15bdc512010-07-13 11:27:41 +0400895 /*
896 * standart macros set (predefined) goes first
897 */
898 p = line_from_stdmac();
899 if (p)
900 return p;
H. Peter Anvin72edbb82008-06-19 16:00:04 -0700901
Cyrill Gorcunov15bdc512010-07-13 11:27:41 +0400902 /*
903 * regular read from a file
904 */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000905 bufsize = BUF_DELTA;
906 buffer = nasm_malloc(BUF_DELTA);
907 p = buffer;
H. Peter Anvin9f394642002-04-30 21:07:51 +0000908 continued_count = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000909 while (1) {
910 q = fgets(p, bufsize - (p - buffer), istk->fp);
911 if (!q)
912 break;
913 p += strlen(p);
914 if (p > buffer && p[-1] == '\n') {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300915 /*
916 * Convert backslash-CRLF line continuation sequences into
917 * nothing at all (for DOS and Windows)
918 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000919 if (((p - 2) > buffer) && (p[-3] == '\\') && (p[-2] == '\r')) {
920 p -= 3;
921 *p = 0;
922 continued_count++;
923 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300924 /*
925 * Also convert backslash-LF line continuation sequences into
926 * nothing at all (for Unix)
927 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000928 else if (((p - 1) > buffer) && (p[-2] == '\\')) {
929 p -= 2;
930 *p = 0;
931 continued_count++;
932 } else {
933 break;
934 }
935 }
936 if (p - buffer > bufsize - 10) {
Keith Kaniosb7a89542007-04-12 02:40:54 +0000937 int32_t offset = p - buffer;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000938 bufsize += BUF_DELTA;
939 buffer = nasm_realloc(buffer, bufsize);
940 p = buffer + offset; /* prevent stale-pointer problems */
941 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000942 }
943
H. Peter Anvine2c80182005-01-15 22:15:51 +0000944 if (!q && p == buffer) {
945 nasm_free(buffer);
946 return NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000947 }
948
Cyrill Gorcunova5aea572010-11-11 10:14:45 +0300949 src_set_linnum(src_get_linnum() + istk->lineinc +
950 (continued_count * istk->lineinc));
H. Peter Anvineba20a72002-04-30 20:53:55 +0000951
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000952 /*
953 * Play safe: remove CRs as well as LFs, if any of either are
954 * present at the end of the line.
955 */
H. Peter Anvineba20a72002-04-30 20:53:55 +0000956 while (--p >= buffer && (*p == '\n' || *p == '\r'))
H. Peter Anvine2c80182005-01-15 22:15:51 +0000957 *p = '\0';
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000958
959 /*
960 * Handle spurious ^Z, which may be inserted into source files
961 * by some file transfer utilities.
962 */
963 buffer[strcspn(buffer, "\032")] = '\0';
964
H. Peter Anvin734b1882002-04-30 21:01:08 +0000965 list->line(LIST_READ, buffer);
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000966
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000967 return buffer;
968}
969
970/*
Keith Kaniosb7a89542007-04-12 02:40:54 +0000971 * Tokenize a line of text. This is a very simple process since we
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000972 * don't need to parse the value out of e.g. numeric tokens: we
973 * simply split one string into many.
974 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000975static Token *tokenize(char *line)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000976{
H. Peter Anvinca544db2008-10-19 19:30:11 -0700977 char c, *p = line;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +0000978 enum pp_token_type type;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000979 Token *list = NULL;
980 Token *t, **tail = &list;
Keith Kanios6faad4e2010-12-18 14:08:02 -0600981 bool verbose = true;
Cyrill Gorcunov82667ff2011-06-25 19:34:19 +0400982
Cyrill Gorcunovfc0c1282011-06-25 19:51:44 +0400983 nasm_trace("Tokenize for '%s'", line);
984
Keith Kanios6faad4e2010-12-18 14:08:02 -0600985 if ((defining != NULL) && (defining->ignoring == true)) {
986 verbose = false;
987 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000988
H. Peter Anvine2c80182005-01-15 22:15:51 +0000989 while (*line) {
990 p = line;
991 if (*p == '%') {
992 p++;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300993 if (*p == '+' && !nasm_isdigit(p[1])) {
994 p++;
995 type = TOK_PASTE;
996 } else if (nasm_isdigit(*p) ||
997 ((*p == '-' || *p == '+') && nasm_isdigit(p[1]))) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000998 do {
999 p++;
1000 }
H. Peter Anvinbda7a6e2008-06-21 10:23:17 -07001001 while (nasm_isdigit(*p));
H. Peter Anvine2c80182005-01-15 22:15:51 +00001002 type = TOK_PREPROC_ID;
1003 } else if (*p == '{') {
1004 p++;
Keith Kaniosb307a4f2010-11-06 17:41:51 -05001005 while (*p && *p != '}') {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001006 p[-1] = *p;
1007 p++;
1008 }
1009 p[-1] = '\0';
1010 if (*p)
1011 p++;
1012 type = TOK_PREPROC_ID;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001013 } else if (*p == '[') {
1014 int lvl = 1;
1015 line += 2; /* Skip the leading %[ */
1016 p++;
1017 while (lvl && (c = *p++)) {
1018 switch (c) {
1019 case ']':
1020 lvl--;
1021 break;
1022 case '%':
1023 if (*p == '[')
1024 lvl++;
1025 break;
1026 case '\'':
1027 case '\"':
1028 case '`':
Cyrill Gorcunovc6360a72010-07-13 13:32:19 +04001029 p = nasm_skip_string(p - 1) + 1;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001030 break;
1031 default:
1032 break;
1033 }
1034 }
1035 p--;
1036 if (*p)
1037 *p++ = '\0';
Keith Kanios6faad4e2010-12-18 14:08:02 -06001038 if (lvl && verbose)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001039 error(ERR_NONFATAL, "unterminated %[ construct");
1040 type = TOK_INDIRECT;
1041 } else if (*p == '?') {
1042 type = TOK_PREPROC_Q; /* %? */
1043 p++;
1044 if (*p == '?') {
1045 type = TOK_PREPROC_QQ; /* %?? */
1046 p++;
1047 }
Cyrill Gorcunov84b4cba2010-09-12 21:39:40 +04001048 } else if (*p == '!') {
1049 type = TOK_PREPROC_ID;
1050 p++;
1051 if (isidchar(*p)) {
1052 do {
1053 p++;
1054 } while (isidchar(*p));
1055 } else if (*p == '\'' || *p == '\"' || *p == '`') {
1056 p = nasm_skip_string(p);
1057 if (*p)
1058 p++;
Keith Kanios6faad4e2010-12-18 14:08:02 -06001059 else if(verbose)
Cyrill Gorcunov84b4cba2010-09-12 21:39:40 +04001060 error(ERR_NONFATAL|ERR_PASS1, "unterminated %! string");
1061 } else {
1062 /* %! without string or identifier */
1063 type = TOK_OTHER; /* Legacy behavior... */
1064 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001065 } else if (isidchar(*p) ||
1066 ((*p == '!' || *p == '%' || *p == '$') &&
1067 isidchar(p[1]))) {
1068 do {
1069 p++;
1070 }
1071 while (isidchar(*p));
1072 type = TOK_PREPROC_ID;
1073 } else {
1074 type = TOK_OTHER;
1075 if (*p == '%')
1076 p++;
1077 }
1078 } else if (isidstart(*p) || (*p == '$' && isidstart(p[1]))) {
1079 type = TOK_ID;
1080 p++;
1081 while (*p && isidchar(*p))
1082 p++;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07001083 } else if (*p == '\'' || *p == '"' || *p == '`') {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001084 /*
1085 * A string token.
1086 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001087 type = TOK_STRING;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001088 p = nasm_skip_string(p);
Nickolay Yurchenkof3b3ce22003-09-21 20:38:43 +00001089
H. Peter Anvine2c80182005-01-15 22:15:51 +00001090 if (*p) {
1091 p++;
Keith Kanios6faad4e2010-12-18 14:08:02 -06001092 } else if(verbose) {
H. Peter Anvin917a3492008-09-24 09:14:49 -07001093 error(ERR_WARNING|ERR_PASS1, "unterminated string");
H. Peter Anvine2c80182005-01-15 22:15:51 +00001094 /* Handling unterminated strings by UNV */
1095 /* type = -1; */
1096 }
Victor van den Elzenfb5f2512009-04-17 16:17:59 +02001097 } else if (p[0] == '$' && p[1] == '$') {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001098 type = TOK_OTHER; /* TOKEN_BASE */
Victor van den Elzenfb5f2512009-04-17 16:17:59 +02001099 p += 2;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001100 } else if (isnumstart(*p)) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001101 bool is_hex = false;
1102 bool is_float = false;
1103 bool has_e = false;
1104 char c, *r;
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001105
H. Peter Anvine2c80182005-01-15 22:15:51 +00001106 /*
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001107 * A numeric token.
H. Peter Anvine2c80182005-01-15 22:15:51 +00001108 */
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001109
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001110 if (*p == '$') {
1111 p++;
1112 is_hex = true;
1113 }
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001114
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001115 for (;;) {
1116 c = *p++;
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001117
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001118 if (!is_hex && (c == 'e' || c == 'E')) {
1119 has_e = true;
1120 if (*p == '+' || *p == '-') {
1121 /*
1122 * e can only be followed by +/- if it is either a
1123 * prefixed hex number or a floating-point number
1124 */
1125 p++;
1126 is_float = true;
1127 }
1128 } else if (c == 'H' || c == 'h' || c == 'X' || c == 'x') {
1129 is_hex = true;
1130 } else if (c == 'P' || c == 'p') {
1131 is_float = true;
1132 if (*p == '+' || *p == '-')
1133 p++;
1134 } else if (isnumchar(c) || c == '_')
1135 ; /* just advance */
1136 else if (c == '.') {
1137 /*
1138 * we need to deal with consequences of the legacy
1139 * parser, like "1.nolist" being two tokens
1140 * (TOK_NUMBER, TOK_ID) here; at least give it
1141 * a shot for now. In the future, we probably need
1142 * a flex-based scanner with proper pattern matching
1143 * to do it as well as it can be done. Nothing in
1144 * the world is going to help the person who wants
1145 * 0x123.p16 interpreted as two tokens, though.
1146 */
1147 r = p;
1148 while (*r == '_')
1149 r++;
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001150
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001151 if (nasm_isdigit(*r) || (is_hex && nasm_isxdigit(*r)) ||
1152 (!is_hex && (*r == 'e' || *r == 'E')) ||
1153 (*r == 'p' || *r == 'P')) {
1154 p = r;
1155 is_float = true;
1156 } else
1157 break; /* Terminate the token */
1158 } else
1159 break;
1160 }
1161 p--; /* Point to first character beyond number */
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001162
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001163 if (p == line+1 && *line == '$') {
1164 type = TOK_OTHER; /* TOKEN_HERE */
1165 } else {
1166 if (has_e && !is_hex) {
1167 /* 1e13 is floating-point, but 1e13h is not */
1168 is_float = true;
1169 }
H. Peter Anvind784a082009-04-20 14:01:18 -07001170
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001171 type = is_float ? TOK_FLOAT : TOK_NUMBER;
1172 }
H. Peter Anvinbda7a6e2008-06-21 10:23:17 -07001173 } else if (nasm_isspace(*p)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001174 type = TOK_WHITESPACE;
Cyrill Gorcunovf66ac7d2009-10-12 20:41:13 +04001175 p = nasm_skip_spaces(p);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001176 /*
1177 * Whitespace just before end-of-line is discarded by
1178 * pretending it's a comment; whitespace just before a
1179 * comment gets lumped into the comment.
1180 */
1181 if (!*p || *p == ';') {
1182 type = TOK_COMMENT;
1183 while (*p)
1184 p++;
1185 }
1186 } else if (*p == ';') {
1187 type = TOK_COMMENT;
1188 while (*p)
1189 p++;
1190 } else {
1191 /*
1192 * Anything else is an operator of some kind. We check
1193 * for all the double-character operators (>>, <<, //,
1194 * %%, <=, >=, ==, !=, <>, &&, ||, ^^), but anything
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001195 * else is a single-character operator.
H. Peter Anvine2c80182005-01-15 22:15:51 +00001196 */
1197 type = TOK_OTHER;
1198 if ((p[0] == '>' && p[1] == '>') ||
1199 (p[0] == '<' && p[1] == '<') ||
1200 (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++;
1210 }
1211 p++;
1212 }
Nickolay Yurchenkof3b3ce22003-09-21 20:38:43 +00001213
H. Peter Anvine2c80182005-01-15 22:15:51 +00001214 /* Handling unterminated string by UNV */
1215 /*if (type == -1)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001216 {
1217 *tail = t = new_Token(NULL, TOK_STRING, line, p-line+1);
1218 t->text[p-line] = *line;
1219 tail = &t->next;
1220 }
1221 else */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001222 if (type != TOK_COMMENT) {
1223 *tail = t = new_Token(NULL, type, line, p - line);
1224 tail = &t->next;
1225 }
1226 line = p;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001227 }
Cyrill Gorcunovfc0c1282011-06-25 19:51:44 +04001228
1229 nasm_dump_token(list);
1230
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001231 return list;
1232}
1233
H. Peter Anvince616072002-04-30 21:02:23 +00001234/*
1235 * this function allocates a new managed block of memory and
H. Peter Anvin70653092007-10-19 14:42:29 -07001236 * returns a pointer to the block. The managed blocks are
H. Peter Anvince616072002-04-30 21:02:23 +00001237 * deleted only all at once by the delete_Blocks function.
1238 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001239static void *new_Block(size_t size)
H. Peter Anvince616072002-04-30 21:02:23 +00001240{
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001241 Blocks *b = &blocks;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001242
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001243 /* first, get to the end of the linked list */
1244 while (b->next)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001245 b = b->next;
Cyrill Gorcunov6b271292011-02-28 08:45:52 +03001246
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001247 /* now allocate the requested chunk */
1248 b->chunk = nasm_malloc(size);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001249
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001250 /* now allocate a new block for the next request */
Cyrill Gorcunov6b271292011-02-28 08:45:52 +03001251 b->next = nasm_zalloc(sizeof(Blocks));
1252
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001253 return b->chunk;
H. Peter Anvince616072002-04-30 21:02:23 +00001254}
1255
1256/*
1257 * this function deletes all managed blocks of memory
1258 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001259static void delete_Blocks(void)
H. Peter Anvince616072002-04-30 21:02:23 +00001260{
H. Peter Anvine2c80182005-01-15 22:15:51 +00001261 Blocks *a, *b = &blocks;
H. Peter Anvince616072002-04-30 21:02:23 +00001262
H. Peter Anvin70653092007-10-19 14:42:29 -07001263 /*
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001264 * keep in mind that the first block, pointed to by blocks
H. Peter Anvin70653092007-10-19 14:42:29 -07001265 * is a static and not dynamically allocated, so we don't
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001266 * free it.
1267 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001268 while (b) {
Cyrill Gorcunovb6c6ca92011-06-28 01:33:02 +04001269 nasm_free(b->chunk);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001270 a = b;
1271 b = b->next;
1272 if (a != &blocks)
1273 nasm_free(a);
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001274 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001275}
H. Peter Anvin734b1882002-04-30 21:01:08 +00001276
1277/*
H. Peter Anvin70653092007-10-19 14:42:29 -07001278 * this function creates a new Token and passes a pointer to it
H. Peter Anvin734b1882002-04-30 21:01:08 +00001279 * back to the caller. It sets the type and text elements, and
H. Peter Anvinf26e0972008-07-01 21:26:27 -07001280 * also the a.mac and next elements to NULL.
H. Peter Anvin734b1882002-04-30 21:01:08 +00001281 */
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07001282static Token *new_Token(Token * next, enum pp_token_type type,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001283 const char *text, int txtlen)
H. Peter Anvin734b1882002-04-30 21:01:08 +00001284{
1285 Token *t;
1286 int i;
1287
H. Peter Anvin89cee572009-07-15 09:16:54 -04001288 if (!freeTokens) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001289 freeTokens = (Token *) new_Block(TOKEN_BLOCKSIZE * sizeof(Token));
1290 for (i = 0; i < TOKEN_BLOCKSIZE - 1; i++)
1291 freeTokens[i].next = &freeTokens[i + 1];
1292 freeTokens[i].next = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001293 }
1294 t = freeTokens;
1295 freeTokens = t->next;
1296 t->next = next;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07001297 t->a.mac = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001298 t->type = type;
H. Peter Anvin89cee572009-07-15 09:16:54 -04001299 if (type == TOK_WHITESPACE || !text) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001300 t->text = NULL;
1301 } else {
1302 if (txtlen == 0)
1303 txtlen = strlen(text);
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07001304 t->text = nasm_malloc(txtlen+1);
1305 memcpy(t->text, text, txtlen);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001306 t->text[txtlen] = '\0';
H. Peter Anvin734b1882002-04-30 21:01:08 +00001307 }
1308 return t;
1309}
1310
Keith Kaniosb307a4f2010-11-06 17:41:51 -05001311static Token *copy_Token(Token * tline)
1312{
1313 Token *t, *tt, *first = NULL, *prev = NULL;
1314 int i;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03001315 for (tt = tline; tt != NULL; tt = tt->next) {
1316 if (!freeTokens) {
1317 freeTokens = (Token *) new_Block(TOKEN_BLOCKSIZE * sizeof(Token));
1318 for (i = 0; i < TOKEN_BLOCKSIZE - 1; i++)
1319 freeTokens[i].next = &freeTokens[i + 1];
1320 freeTokens[i].next = NULL;
1321 }
1322 t = freeTokens;
1323 freeTokens = t->next;
1324 t->next = NULL;
1325 t->text = tt->text ? nasm_strdup(tt->text) : NULL;
1326 t->a.mac = tt->a.mac;
1327 t->a.len = tt->a.len;
1328 t->type = tt->type;
1329 if (prev != NULL) {
1330 prev->next = t;
1331 } else {
1332 first = t;
1333 }
1334 prev = t;
1335 }
Keith Kaniosb307a4f2010-11-06 17:41:51 -05001336 return first;
1337}
1338
H. Peter Anvine2c80182005-01-15 22:15:51 +00001339static Token *delete_Token(Token * t)
H. Peter Anvin734b1882002-04-30 21:01:08 +00001340{
1341 Token *next = t->next;
1342 nasm_free(t->text);
H. Peter Anvin788e6c12002-04-30 21:02:01 +00001343 t->next = freeTokens;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001344 freeTokens = t;
1345 return next;
1346}
1347
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001348/*
1349 * Convert a line of tokens back into text.
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001350 * If expand_locals is not zero, identifiers of the form "%$*xxx"
1351 * will be transformed into ..@ctxnum.xxx
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001352 */
H. Peter Anvin9e200162008-06-04 17:23:14 -07001353static char *detoken(Token * tlist, bool expand_locals)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001354{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001355 Token *t;
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001356 char *line, *p;
H. Peter Anvinb4daadc2008-01-21 16:31:57 -08001357 const char *q;
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001358 int len = 0;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001359
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001360 list_for_each(t, tlist) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001361 if (t->type == TOK_PREPROC_ID && t->text[1] == '!') {
Cyrill Gorcunov84b4cba2010-09-12 21:39:40 +04001362 char *v;
1363 char *q = t->text;
H. Peter Anvin077fb932010-07-20 14:56:30 -07001364
Cyrill Gorcunov84b4cba2010-09-12 21:39:40 +04001365 v = t->text + 2;
1366 if (*v == '\'' || *v == '\"' || *v == '`') {
1367 size_t len = nasm_unquote(v, NULL);
1368 size_t clen = strlen(v);
H. Peter Anvin077fb932010-07-20 14:56:30 -07001369
Cyrill Gorcunov84b4cba2010-09-12 21:39:40 +04001370 if (len != clen) {
1371 error(ERR_NONFATAL | ERR_PASS1,
1372 "NUL character in %! string");
1373 v = NULL;
1374 }
1375 }
H. Peter Anvin077fb932010-07-20 14:56:30 -07001376
Cyrill Gorcunov84b4cba2010-09-12 21:39:40 +04001377 if (v) {
1378 char *p = getenv(v);
1379 if (!p) {
1380 error(ERR_NONFATAL | ERR_PASS1,
1381 "nonexistent environment variable `%s'", v);
1382 p = "";
1383 }
1384 t->text = nasm_strdup(p);
1385 }
1386 nasm_free(q);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001387 }
H. Peter Anvin077fb932010-07-20 14:56:30 -07001388
H. Peter Anvine2c80182005-01-15 22:15:51 +00001389 /* Expand local macros here and not during preprocessing */
1390 if (expand_locals &&
1391 t->type == TOK_PREPROC_ID && t->text &&
1392 t->text[0] == '%' && t->text[1] == '$') {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001393 const char *q;
1394 char *p;
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001395 Context *ctx = get_ctx(t->text, &q, false);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001396 if (ctx) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001397 char buffer[40];
Keith Kanios93f2e9a2007-04-14 00:10:59 +00001398 snprintf(buffer, sizeof(buffer), "..@%"PRIu32".", ctx->number);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001399 p = nasm_strcat(buffer, q);
1400 nasm_free(t->text);
1401 t->text = p;
1402 }
1403 }
Keith Kaniosb307a4f2010-11-06 17:41:51 -05001404
1405 /* Expand %? and %?? directives */
Keith Kanios3136d482010-11-13 09:34:34 -06001406 if ((istk->expansion != NULL) &&
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03001407 ((t->type == TOK_PREPROC_Q) ||
1408 (t->type == TOK_PREPROC_QQ))) {
Keith Kaniosb307a4f2010-11-06 17:41:51 -05001409 ExpInv *ei;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03001410 for (ei = istk->expansion; ei != NULL; ei = ei->prev){
1411 if (ei->type == EXP_MMACRO) {
1412 nasm_free(t->text);
1413 if (t->type == TOK_PREPROC_Q) {
1414 t->text = nasm_strdup(ei->name);
1415 } else {
1416 t->text = nasm_strdup(ei->def->name);
1417 }
1418 break;
1419 }
1420 }
1421 }
Keith Kaniosb307a4f2010-11-06 17:41:51 -05001422
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001423 if (t->type == TOK_WHITESPACE)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001424 len++;
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001425 else if (t->text)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001426 len += strlen(t->text);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001427 }
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001428
H. Peter Anvin734b1882002-04-30 21:01:08 +00001429 p = line = nasm_malloc(len + 1);
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001430
1431 list_for_each(t, tlist) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001432 if (t->type == TOK_WHITESPACE) {
H. Peter Anvinb4daadc2008-01-21 16:31:57 -08001433 *p++ = ' ';
H. Peter Anvine2c80182005-01-15 22:15:51 +00001434 } else if (t->text) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001435 q = t->text;
1436 while (*q)
1437 *p++ = *q++;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001438 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001439 }
1440 *p = '\0';
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001441
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001442 return line;
1443}
1444
1445/*
Keith Kaniosb307a4f2010-11-06 17:41:51 -05001446 * Initialize a new Line
1447 */
Cyrill Gorcunov29cb0bb2010-11-11 11:19:43 +03001448static inline Line *new_Line(void)
Keith Kaniosb307a4f2010-11-06 17:41:51 -05001449{
Cyrill Gorcunov6b271292011-02-28 08:45:52 +03001450 return (Line *)nasm_zalloc(sizeof(Line));
Keith Kaniosb307a4f2010-11-06 17:41:51 -05001451}
1452
1453
1454/*
1455 * Initialize a new Expansion Definition
1456 */
1457static ExpDef *new_ExpDef(int exp_type)
1458{
Cyrill Gorcunovc5157742010-11-11 11:29:40 +03001459 ExpDef *ed = (ExpDef*)nasm_zalloc(sizeof(ExpDef));
1460 ed->type = exp_type;
1461 ed->casesense = true;
1462 ed->state = COND_NEVER;
1463
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03001464 return ed;
Keith Kaniosb307a4f2010-11-06 17:41:51 -05001465}
1466
1467
1468/*
1469 * Initialize a new Expansion Instance
1470 */
1471static ExpInv *new_ExpInv(int exp_type, ExpDef *ed)
1472{
Cyrill Gorcunovc5157742010-11-11 11:29:40 +03001473 ExpInv *ei = (ExpInv*)nasm_zalloc(sizeof(ExpInv));
1474 ei->type = exp_type;
1475 ei->def = ed;
1476 ei->unique = ++unique;
1477
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03001478 if ((istk->mmac_depth < 1) &&
1479 (istk->expansion == NULL) &&
1480 (ed != NULL) &&
1481 (ed->type != EXP_MMACRO) &&
1482 (ed->type != EXP_REP) &&
1483 (ed->type != EXP_WHILE)) {
1484 ei->linnum = src_get_linnum();
1485 src_set_linnum(ei->linnum - ed->linecount - 1);
1486 } else {
1487 ei->linnum = -1;
1488 }
1489 if ((istk->expansion == NULL) ||
1490 (ei->type == EXP_MMACRO)) {
1491 ei->relno = 0;
1492 } else {
1493 ei->relno = istk->expansion->lineno;
1494 if (ed != NULL) {
1495 ei->relno -= (ed->linecount + 1);
1496 }
1497 }
1498 return ei;
Keith Kaniosb307a4f2010-11-06 17:41:51 -05001499}
1500
1501/*
H. Peter Anvin76690a12002-04-30 20:52:49 +00001502 * A scanner, suitable for use by the expression evaluator, which
1503 * operates on a line of Tokens. Expects a pointer to a pointer to
1504 * the first token in the line to be passed in as its private_data
1505 * field.
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001506 *
1507 * FIX: This really needs to be unified with stdscan.
H. Peter Anvin76690a12002-04-30 20:52:49 +00001508 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001509static int ppscan(void *private_data, struct tokenval *tokval)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001510{
H. Peter Anvin76690a12002-04-30 20:52:49 +00001511 Token **tlineptr = private_data;
1512 Token *tline;
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001513 char ourcopy[MAX_KEYWORD+1], *p, *r, *s;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001514
H. Peter Anvine2c80182005-01-15 22:15:51 +00001515 do {
1516 tline = *tlineptr;
1517 *tlineptr = tline ? tline->next : NULL;
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04001518 } while (tline && (tline->type == TOK_WHITESPACE ||
1519 tline->type == TOK_COMMENT));
H. Peter Anvin76690a12002-04-30 20:52:49 +00001520
1521 if (!tline)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001522 return tokval->t_type = TOKEN_EOS;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001523
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001524 tokval->t_charptr = tline->text;
1525
H. Peter Anvin76690a12002-04-30 20:52:49 +00001526 if (tline->text[0] == '$' && !tline->text[1])
H. Peter Anvine2c80182005-01-15 22:15:51 +00001527 return tokval->t_type = TOKEN_HERE;
H. Peter Anvin7cf897e2002-05-30 21:30:33 +00001528 if (tline->text[0] == '$' && tline->text[1] == '$' && !tline->text[2])
H. Peter Anvine2c80182005-01-15 22:15:51 +00001529 return tokval->t_type = TOKEN_BASE;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001530
H. Peter Anvine2c80182005-01-15 22:15:51 +00001531 if (tline->type == TOK_ID) {
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001532 p = tokval->t_charptr = tline->text;
1533 if (p[0] == '$') {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001534 tokval->t_charptr++;
1535 return tokval->t_type = TOKEN_ID;
1536 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00001537
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001538 for (r = p, s = ourcopy; *r; r++) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001539 if (r >= p+MAX_KEYWORD)
1540 return tokval->t_type = TOKEN_ID; /* Not a keyword */
H. Peter Anvinac8f8fc2008-06-11 15:49:41 -07001541 *s++ = nasm_tolower(*r);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001542 }
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001543 *s = '\0';
1544 /* right, so we have an identifier sitting in temp storage. now,
1545 * is it actually a register or instruction name, or what? */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001546 return nasm_token_hash(ourcopy, tokval);
H. Peter Anvin76690a12002-04-30 20:52:49 +00001547 }
1548
H. Peter Anvine2c80182005-01-15 22:15:51 +00001549 if (tline->type == TOK_NUMBER) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001550 bool rn_error;
1551 tokval->t_integer = readnum(tline->text, &rn_error);
1552 tokval->t_charptr = tline->text;
1553 if (rn_error)
1554 return tokval->t_type = TOKEN_ERRNUM;
1555 else
1556 return tokval->t_type = TOKEN_NUM;
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001557 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00001558
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001559 if (tline->type == TOK_FLOAT) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001560 return tokval->t_type = TOKEN_FLOAT;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001561 }
1562
H. Peter Anvine2c80182005-01-15 22:15:51 +00001563 if (tline->type == TOK_STRING) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001564 char bq, *ep;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001565
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001566 bq = tline->text[0];
H. Peter Anvin11627042008-06-09 20:45:19 -07001567 tokval->t_charptr = tline->text;
1568 tokval->t_inttwo = nasm_unquote(tline->text, &ep);
H. Peter Anvind2456592008-06-19 15:04:18 -07001569
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001570 if (ep[0] != bq || ep[1] != '\0')
1571 return tokval->t_type = TOKEN_ERRSTR;
1572 else
1573 return tokval->t_type = TOKEN_STR;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001574 }
1575
H. Peter Anvine2c80182005-01-15 22:15:51 +00001576 if (tline->type == TOK_OTHER) {
1577 if (!strcmp(tline->text, "<<"))
1578 return tokval->t_type = TOKEN_SHL;
1579 if (!strcmp(tline->text, ">>"))
1580 return tokval->t_type = TOKEN_SHR;
1581 if (!strcmp(tline->text, "//"))
1582 return tokval->t_type = TOKEN_SDIV;
1583 if (!strcmp(tline->text, "%%"))
1584 return tokval->t_type = TOKEN_SMOD;
1585 if (!strcmp(tline->text, "=="))
1586 return tokval->t_type = TOKEN_EQ;
1587 if (!strcmp(tline->text, "<>"))
1588 return tokval->t_type = TOKEN_NE;
1589 if (!strcmp(tline->text, "!="))
1590 return tokval->t_type = TOKEN_NE;
1591 if (!strcmp(tline->text, "<="))
1592 return tokval->t_type = TOKEN_LE;
1593 if (!strcmp(tline->text, ">="))
1594 return tokval->t_type = TOKEN_GE;
1595 if (!strcmp(tline->text, "&&"))
1596 return tokval->t_type = TOKEN_DBL_AND;
1597 if (!strcmp(tline->text, "^^"))
1598 return tokval->t_type = TOKEN_DBL_XOR;
1599 if (!strcmp(tline->text, "||"))
1600 return tokval->t_type = TOKEN_DBL_OR;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001601 }
1602
1603 /*
1604 * We have no other options: just return the first character of
1605 * the token text.
1606 */
1607 return tokval->t_type = tline->text[0];
1608}
1609
1610/*
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001611 * Compare a string to the name of an existing macro; this is a
1612 * simple wrapper which calls either strcmp or nasm_stricmp
1613 * depending on the value of the `casesense' parameter.
1614 */
H. Peter Anvin4db5a162007-10-11 13:42:09 -07001615static int mstrcmp(const char *p, const char *q, bool casesense)
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001616{
H. Peter Anvin734b1882002-04-30 21:01:08 +00001617 return casesense ? strcmp(p, q) : nasm_stricmp(p, q);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001618}
1619
1620/*
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07001621 * Compare a string to the name of an existing macro; this is a
1622 * simple wrapper which calls either strcmp or nasm_stricmp
1623 * depending on the value of the `casesense' parameter.
1624 */
1625static int mmemcmp(const char *p, const char *q, size_t l, bool casesense)
1626{
1627 return casesense ? memcmp(p, q, l) : nasm_memicmp(p, q, l);
1628}
1629
1630/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001631 * Return the Context structure associated with a %$ token. Return
1632 * NULL, having _already_ reported an error condition, if the
1633 * context stack isn't deep enough for the supplied number of $
1634 * signs.
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001635 * If all_contexts == true, contexts that enclose current are
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001636 * also scanned for such smacro, until it is found; if not -
1637 * only the context that directly results from the number of $'s
1638 * in variable's name.
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001639 *
1640 * If "namep" is non-NULL, set it to the pointer to the macro name
1641 * tail, i.e. the part beyond %$...
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001642 */
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001643static Context *get_ctx(const char *name, const char **namep,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001644 bool all_contexts)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001645{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001646 Context *ctx;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001647 SMacro *m;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001648 int i;
1649
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001650 if (namep)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001651 *namep = name;
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001652
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001653 if (!name || name[0] != '%' || name[1] != '$')
H. Peter Anvine2c80182005-01-15 22:15:51 +00001654 return NULL;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001655
H. Peter Anvine2c80182005-01-15 22:15:51 +00001656 if (!cstk) {
1657 error(ERR_NONFATAL, "`%s': context stack is empty", name);
1658 return NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001659 }
1660
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001661 name += 2;
1662 ctx = cstk;
1663 i = 0;
1664 while (ctx && *name == '$') {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001665 name++;
1666 i++;
1667 ctx = ctx->next;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001668 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001669 if (!ctx) {
1670 error(ERR_NONFATAL, "`%s': context stack is only"
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001671 " %d level%s deep", name, i, (i == 1 ? "" : "s"));
H. Peter Anvine2c80182005-01-15 22:15:51 +00001672 return NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001673 }
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001674
1675 if (namep)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001676 *namep = name;
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001677
Keith Kanios404589e2010-08-10 20:12:57 -05001678 if (!all_contexts)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001679 return ctx;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001680
Keith Kaniosb307a4f2010-11-06 17:41:51 -05001681 do {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001682 /* Search for this smacro in found context */
H. Peter Anvin166c2472008-05-28 12:28:58 -07001683 m = hash_findix(&ctx->localmac, name);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001684 while (m) {
Keith Kaniosb307a4f2010-11-06 17:41:51 -05001685 if (!mstrcmp(m->name, name, m->casesense))
H. Peter Anvine2c80182005-01-15 22:15:51 +00001686 return ctx;
1687 m = m->next;
1688 }
Keith Kaniosb307a4f2010-11-06 17:41:51 -05001689 ctx = ctx->next;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001690 }
Keith Kaniosb307a4f2010-11-06 17:41:51 -05001691 while (ctx);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001692 return NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001693}
1694
1695/*
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001696 * Check to see if a file is already in a string list
1697 */
1698static bool in_list(const StrList *list, const char *str)
1699{
1700 while (list) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001701 if (!strcmp(list->str, str))
1702 return true;
1703 list = list->next;
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001704 }
1705 return false;
1706}
1707
1708/*
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001709 * Open an include file. This routine must always return a valid
1710 * file pointer if it returns - it's responsible for throwing an
1711 * ERR_FATAL and bombing out completely if not. It should also try
1712 * the include path one by one until it finds the file or reaches
1713 * the end of the path.
1714 */
H. Peter Anvin2b1c3b92008-06-06 10:38:46 -07001715static FILE *inc_fopen(const char *file, StrList **dhead, StrList ***dtail,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001716 bool missing_ok)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001717{
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001718 FILE *fp;
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001719 char *prefix = "";
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001720 IncPath *ip = ipath;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001721 int len = strlen(file);
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001722 size_t prefix_len = 0;
1723 StrList *sl;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001724
H. Peter Anvine2c80182005-01-15 22:15:51 +00001725 while (1) {
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001726 sl = nasm_malloc(prefix_len+len+1+sizeof sl->next);
Cyrill Gorcunov6f38fe62010-11-11 11:32:16 +03001727 sl->next = NULL;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001728 memcpy(sl->str, prefix, prefix_len);
1729 memcpy(sl->str+prefix_len, file, len+1);
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001730 fp = fopen(sl->str, "r");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001731 if (fp && dhead && !in_list(*dhead, sl->str)) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001732 **dtail = sl;
1733 *dtail = &sl->next;
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001734 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001735 nasm_free(sl);
1736 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001737 if (fp)
1738 return fp;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001739 if (!ip) {
1740 if (!missing_ok)
1741 break;
1742 prefix = NULL;
1743 } else {
1744 prefix = ip->path;
1745 ip = ip->next;
1746 }
1747 if (prefix) {
1748 prefix_len = strlen(prefix);
1749 } else {
1750 /* -MG given and file not found */
1751 if (dhead && !in_list(*dhead, file)) {
1752 sl = nasm_malloc(len+1+sizeof sl->next);
1753 sl->next = NULL;
1754 strcpy(sl->str, file);
1755 **dtail = sl;
1756 *dtail = &sl->next;
1757 }
1758 return NULL;
1759 }
H. Peter Anvineba20a72002-04-30 20:53:55 +00001760 }
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001761
H. Peter Anvin734b1882002-04-30 21:01:08 +00001762 error(ERR_FATAL, "unable to open include file `%s'", file);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001763 return NULL;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001764}
1765
1766/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001767 * Determine if we should warn on defining a single-line macro of
H. Peter Anvinef7468f2002-04-30 20:57:59 +00001768 * name `name', with `nparam' parameters. If nparam is 0 or -1, will
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001769 * return true if _any_ single-line macro of that name is defined.
1770 * Otherwise, will return true if a single-line macro with either
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001771 * `nparam' or no parameters is defined.
1772 *
1773 * If a macro with precisely the right number of parameters is
H. Peter Anvinef7468f2002-04-30 20:57:59 +00001774 * defined, or nparam is -1, the address of the definition structure
1775 * will be returned in `defn'; otherwise NULL will be returned. If `defn'
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001776 * is NULL, no action will be taken regarding its contents, and no
1777 * error will occur.
1778 *
1779 * Note that this is also called with nparam zero to resolve
1780 * `ifdef'.
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001781 *
1782 * If you already know which context macro belongs to, you can pass
1783 * the context pointer as first parameter; if you won't but name begins
1784 * with %$ the context will be automatically computed. If all_contexts
1785 * is true, macro will be searched in outer contexts as well.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001786 */
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07001787static bool
H. Peter Anvinb2a5fda2008-06-19 21:42:42 -07001788smacro_defined(Context * ctx, const char *name, int nparam, SMacro ** defn,
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07001789 bool nocase)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001790{
H. Peter Anvin166c2472008-05-28 12:28:58 -07001791 struct hash_table *smtbl;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001792 SMacro *m;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001793
H. Peter Anvin97a23472007-09-16 17:57:25 -07001794 if (ctx) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001795 smtbl = &ctx->localmac;
H. Peter Anvin97a23472007-09-16 17:57:25 -07001796 } else if (name[0] == '%' && name[1] == '$') {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001797 if (cstk)
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001798 ctx = get_ctx(name, &name, false);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001799 if (!ctx)
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001800 return false; /* got to return _something_ */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001801 smtbl = &ctx->localmac;
H. Peter Anvin97a23472007-09-16 17:57:25 -07001802 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001803 smtbl = &smacros;
H. Peter Anvin97a23472007-09-16 17:57:25 -07001804 }
H. Peter Anvin166c2472008-05-28 12:28:58 -07001805 m = (SMacro *) hash_findix(smtbl, name);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001806
H. Peter Anvine2c80182005-01-15 22:15:51 +00001807 while (m) {
1808 if (!mstrcmp(m->name, name, m->casesense && nocase) &&
Charles Crayne192d5b52007-10-18 19:02:42 -07001809 (nparam <= 0 || m->nparam == 0 || nparam == (int) m->nparam)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001810 if (defn) {
Charles Crayne192d5b52007-10-18 19:02:42 -07001811 if (nparam == (int) m->nparam || nparam == -1)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001812 *defn = m;
1813 else
1814 *defn = NULL;
1815 }
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001816 return true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001817 }
1818 m = m->next;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001819 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001820
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001821 return false;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001822}
1823
1824/*
1825 * Count and mark off the parameters in a multi-line macro call.
1826 * This is called both from within the multi-line macro expansion
1827 * code, and also to mark off the default parameters when provided
1828 * in a %macro definition line.
1829 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001830static void count_mmac_params(Token * t, int *nparam, Token *** params)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001831{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001832 int paramsize, brace;
1833
1834 *nparam = paramsize = 0;
1835 *params = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001836 while (t) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001837 /* +1: we need space for the final NULL */
H. Peter Anvin91fb6f12008-09-01 10:56:33 -07001838 if (*nparam+1 >= paramsize) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001839 paramsize += PARAM_DELTA;
1840 *params = nasm_realloc(*params, sizeof(**params) * paramsize);
1841 }
1842 skip_white_(t);
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001843 brace = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001844 if (tok_is_(t, "{"))
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001845 brace = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001846 (*params)[(*nparam)++] = t;
1847 while (tok_isnt_(t, brace ? "}" : ","))
1848 t = t->next;
1849 if (t) { /* got a comma/brace */
1850 t = t->next;
1851 if (brace) {
1852 /*
1853 * Now we've found the closing brace, look further
1854 * for the comma.
1855 */
1856 skip_white_(t);
1857 if (tok_isnt_(t, ",")) {
1858 error(ERR_NONFATAL,
1859 "braces do not enclose all of macro parameter");
1860 while (tok_isnt_(t, ","))
1861 t = t->next;
1862 }
1863 if (t)
1864 t = t->next; /* eat the comma */
1865 }
1866 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001867 }
1868}
1869
1870/*
H. Peter Anvin76690a12002-04-30 20:52:49 +00001871 * Determine whether one of the various `if' conditions is true or
1872 * not.
1873 *
1874 * We must free the tline we get passed.
1875 */
H. Peter Anvin70055962007-10-11 00:05:31 -07001876static bool if_condition(Token * tline, enum preproc_token ct)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001877{
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001878 enum pp_conditional i = PP_COND(ct);
H. Peter Anvin70055962007-10-11 00:05:31 -07001879 bool j;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001880 Token *t, *tt, **tptr, *origline;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001881 struct tokenval tokval;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001882 expr *evalresult;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001883 enum pp_token_type needtype;
H. Peter Anvin077fb932010-07-20 14:56:30 -07001884 char *p;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001885
1886 origline = tline;
1887
H. Peter Anvine2c80182005-01-15 22:15:51 +00001888 switch (i) {
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001889 case PPC_IFCTX:
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001890 j = false; /* have we matched yet? */
Victor van den Elzen0e857f12008-07-23 13:21:29 +02001891 while (true) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001892 skip_white_(tline);
Victor van den Elzen0e857f12008-07-23 13:21:29 +02001893 if (!tline)
1894 break;
1895 if (tline->type != TOK_ID) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001896 error(ERR_NONFATAL,
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001897 "`%s' expects context identifiers", pp_directives[ct]);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001898 free_tlist(origline);
1899 return -1;
1900 }
Victor van den Elzen0e857f12008-07-23 13:21:29 +02001901 if (cstk && cstk->name && !nasm_stricmp(tline->text, cstk->name))
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001902 j = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001903 tline = tline->next;
1904 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001905 break;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001906
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001907 case PPC_IFDEF:
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001908 j = false; /* have we matched yet? */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001909 while (tline) {
1910 skip_white_(tline);
1911 if (!tline || (tline->type != TOK_ID &&
1912 (tline->type != TOK_PREPROC_ID ||
1913 tline->text[1] != '$'))) {
1914 error(ERR_NONFATAL,
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001915 "`%s' expects macro identifiers", pp_directives[ct]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001916 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001917 }
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07001918 if (smacro_defined(NULL, tline->text, 0, NULL, true))
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001919 j = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001920 tline = tline->next;
1921 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001922 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001923
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001924 case PPC_IFENV:
Cyrill Gorcunov84b4cba2010-09-12 21:39:40 +04001925 tline = expand_smacro(tline);
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001926 j = false; /* have we matched yet? */
1927 while (tline) {
1928 skip_white_(tline);
1929 if (!tline || (tline->type != TOK_ID &&
Cyrill Gorcunov84b4cba2010-09-12 21:39:40 +04001930 tline->type != TOK_STRING &&
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001931 (tline->type != TOK_PREPROC_ID ||
Cyrill Gorcunov84b4cba2010-09-12 21:39:40 +04001932 tline->text[1] != '!'))) {
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001933 error(ERR_NONFATAL,
1934 "`%s' expects environment variable names",
Cyrill Gorcunov84b4cba2010-09-12 21:39:40 +04001935 pp_directives[ct]);
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001936 goto fail;
1937 }
Cyrill Gorcunov84b4cba2010-09-12 21:39:40 +04001938 p = tline->text;
1939 if (tline->type == TOK_PREPROC_ID)
1940 p += 2; /* Skip leading %! */
1941 if (*p == '\'' || *p == '\"' || *p == '`')
1942 nasm_unquote_cstr(p, ct);
1943 if (getenv(p))
1944 j = true;
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001945 tline = tline->next;
1946 }
1947 break;
1948
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001949 case PPC_IFIDN:
1950 case PPC_IFIDNI:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001951 tline = expand_smacro(tline);
1952 t = tt = tline;
1953 while (tok_isnt_(tt, ","))
1954 tt = tt->next;
1955 if (!tt) {
1956 error(ERR_NONFATAL,
1957 "`%s' expects two comma-separated arguments",
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001958 pp_directives[ct]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001959 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001960 }
1961 tt = tt->next;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001962 j = true; /* assume equality unless proved not */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001963 while ((t->type != TOK_OTHER || strcmp(t->text, ",")) && tt) {
1964 if (tt->type == TOK_OTHER && !strcmp(tt->text, ",")) {
1965 error(ERR_NONFATAL, "`%s': more than one comma on line",
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001966 pp_directives[ct]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001967 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001968 }
1969 if (t->type == TOK_WHITESPACE) {
1970 t = t->next;
1971 continue;
1972 }
1973 if (tt->type == TOK_WHITESPACE) {
1974 tt = tt->next;
1975 continue;
1976 }
1977 if (tt->type != t->type) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001978 j = false; /* found mismatching tokens */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001979 break;
1980 }
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07001981 /* When comparing strings, need to unquote them first */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001982 if (t->type == TOK_STRING) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001983 size_t l1 = nasm_unquote(t->text, NULL);
1984 size_t l2 = nasm_unquote(tt->text, NULL);
H. Peter Anvind2456592008-06-19 15:04:18 -07001985
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001986 if (l1 != l2) {
1987 j = false;
1988 break;
1989 }
1990 if (mmemcmp(t->text, tt->text, l1, i == PPC_IFIDN)) {
1991 j = false;
1992 break;
1993 }
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07001994 } else if (mstrcmp(tt->text, t->text, i == PPC_IFIDN) != 0) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001995 j = false; /* found mismatching tokens */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001996 break;
1997 }
Nickolay Yurchenkof3b3ce22003-09-21 20:38:43 +00001998
H. Peter Anvine2c80182005-01-15 22:15:51 +00001999 t = t->next;
2000 tt = tt->next;
2001 }
2002 if ((t->type != TOK_OTHER || strcmp(t->text, ",")) || tt)
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002003 j = false; /* trailing gunk on one end or other */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002004 break;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002005
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002006 case PPC_IFMACRO:
H. Peter Anvin89cee572009-07-15 09:16:54 -04002007 {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002008 bool found = false;
Keith Kaniosb307a4f2010-11-06 17:41:51 -05002009 ExpDef searching, *ed;
H. Peter Anvin65747262002-05-07 00:10:05 +00002010
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002011 skip_white_(tline);
2012 tline = expand_id(tline);
2013 if (!tok_type_(tline, TOK_ID)) {
2014 error(ERR_NONFATAL,
2015 "`%s' expects a macro name", pp_directives[ct]);
2016 goto fail;
2017 }
Cyrill Gorcunova22e7a92010-11-11 11:42:40 +03002018 memset(&searching, 0, sizeof(searching));
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002019 searching.name = nasm_strdup(tline->text);
2020 searching.casesense = true;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002021 searching.nparam_max = INT_MAX;
2022 tline = expand_smacro(tline->next);
2023 skip_white_(tline);
2024 if (!tline) {
2025 } else if (!tok_type_(tline, TOK_NUMBER)) {
2026 error(ERR_NONFATAL,
2027 "`%s' expects a parameter count or nothing",
2028 pp_directives[ct]);
2029 } else {
2030 searching.nparam_min = searching.nparam_max =
2031 readnum(tline->text, &j);
2032 if (j)
2033 error(ERR_NONFATAL,
2034 "unable to parse parameter count `%s'",
2035 tline->text);
2036 }
2037 if (tline && tok_is_(tline->next, "-")) {
2038 tline = tline->next->next;
2039 if (tok_is_(tline, "*"))
2040 searching.nparam_max = INT_MAX;
2041 else if (!tok_type_(tline, TOK_NUMBER))
2042 error(ERR_NONFATAL,
2043 "`%s' expects a parameter count after `-'",
2044 pp_directives[ct]);
2045 else {
2046 searching.nparam_max = readnum(tline->text, &j);
2047 if (j)
2048 error(ERR_NONFATAL,
2049 "unable to parse parameter count `%s'",
2050 tline->text);
2051 if (searching.nparam_min > searching.nparam_max)
2052 error(ERR_NONFATAL,
2053 "minimum parameter count exceeds maximum");
2054 }
2055 }
2056 if (tline && tok_is_(tline->next, "+")) {
2057 tline = tline->next;
2058 searching.plus = true;
2059 }
Keith Kaniosb307a4f2010-11-06 17:41:51 -05002060 ed = (ExpDef *) hash_findix(&expdefs, searching.name);
2061 while (ed != NULL) {
Cyrill Gorcunova22e7a92010-11-11 11:42:40 +03002062 if (!strcmp(ed->name, searching.name) &&
2063 (ed->nparam_min <= searching.nparam_max || searching.plus) &&
2064 (searching.nparam_min <= ed->nparam_max || ed->plus)) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002065 found = true;
2066 break;
2067 }
Keith Kaniosb307a4f2010-11-06 17:41:51 -05002068 ed = ed->next;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002069 }
2070 if (tline && tline->next)
2071 error(ERR_WARNING|ERR_PASS1,
2072 "trailing garbage after %%ifmacro ignored");
2073 nasm_free(searching.name);
2074 j = found;
2075 break;
H. Peter Anvin89cee572009-07-15 09:16:54 -04002076 }
H. Peter Anvin65747262002-05-07 00:10:05 +00002077
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002078 case PPC_IFID:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002079 needtype = TOK_ID;
2080 goto iftype;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002081 case PPC_IFNUM:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002082 needtype = TOK_NUMBER;
2083 goto iftype;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002084 case PPC_IFSTR:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002085 needtype = TOK_STRING;
2086 goto iftype;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002087
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002088iftype:
2089 t = tline = expand_smacro(tline);
H. Peter Anvind85d2502008-05-04 17:53:31 -07002090
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002091 while (tok_type_(t, TOK_WHITESPACE) ||
2092 (needtype == TOK_NUMBER &&
2093 tok_type_(t, TOK_OTHER) &&
2094 (t->text[0] == '-' || t->text[0] == '+') &&
2095 !t->text[1]))
2096 t = t->next;
H. Peter Anvind85d2502008-05-04 17:53:31 -07002097
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002098 j = tok_type_(t, needtype);
2099 break;
H. Peter Anvincbf768d2008-02-16 16:41:25 -08002100
2101 case PPC_IFTOKEN:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002102 t = tline = expand_smacro(tline);
2103 while (tok_type_(t, TOK_WHITESPACE))
2104 t = t->next;
H. Peter Anvincbf768d2008-02-16 16:41:25 -08002105
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002106 j = false;
2107 if (t) {
2108 t = t->next; /* Skip the actual token */
2109 while (tok_type_(t, TOK_WHITESPACE))
2110 t = t->next;
2111 j = !t; /* Should be nothing left */
2112 }
2113 break;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002114
H. Peter Anvin134b9462008-02-16 17:01:40 -08002115 case PPC_IFEMPTY:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002116 t = tline = expand_smacro(tline);
2117 while (tok_type_(t, TOK_WHITESPACE))
2118 t = t->next;
H. Peter Anvin134b9462008-02-16 17:01:40 -08002119
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002120 j = !t; /* Should be empty */
2121 break;
H. Peter Anvin134b9462008-02-16 17:01:40 -08002122
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002123 case PPC_IF:
H. Peter Anvine2c80182005-01-15 22:15:51 +00002124 t = tline = expand_smacro(tline);
2125 tptr = &t;
2126 tokval.t_type = TOKEN_INVALID;
2127 evalresult = evaluate(ppscan, tptr, &tokval,
2128 NULL, pass | CRITICAL, error, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002129 if (!evalresult)
2130 return -1;
2131 if (tokval.t_type)
H. Peter Anvin917a3492008-09-24 09:14:49 -07002132 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +00002133 "trailing garbage after expression ignored");
2134 if (!is_simple(evalresult)) {
2135 error(ERR_NONFATAL,
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002136 "non-constant value given to `%s'", pp_directives[ct]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002137 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002138 }
Chuck Crayne60ae75d2007-05-02 01:59:16 +00002139 j = reloc_value(evalresult) != 0;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002140 break;
H. Peter Anvin95e28822007-09-12 04:20:08 +00002141
H. Peter Anvine2c80182005-01-15 22:15:51 +00002142 default:
2143 error(ERR_FATAL,
2144 "preprocessor directive `%s' not yet implemented",
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002145 pp_directives[ct]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002146 goto fail;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002147 }
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002148
2149 free_tlist(origline);
2150 return j ^ PP_NEGATIVE(ct);
H. Peter Anvin70653092007-10-19 14:42:29 -07002151
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002152fail:
2153 free_tlist(origline);
2154 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002155}
2156
2157/*
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002158 * Common code for defining an smacro
2159 */
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08002160static bool define_smacro(Context *ctx, const char *mname, bool casesense,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002161 int nparam, Token *expansion)
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002162{
2163 SMacro *smac, **smhead;
H. Peter Anvin166c2472008-05-28 12:28:58 -07002164 struct hash_table *smtbl;
H. Peter Anvin70653092007-10-19 14:42:29 -07002165
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002166 if (smacro_defined(ctx, mname, nparam, &smac, casesense)) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002167 if (!smac) {
2168 error(ERR_WARNING|ERR_PASS1,
2169 "single-line macro `%s' defined both with and"
2170 " without parameters", mname);
2171 /*
2172 * Some instances of the old code considered this a failure,
2173 * some others didn't. What is the right thing to do here?
2174 */
2175 free_tlist(expansion);
2176 return false; /* Failure */
2177 } else {
2178 /*
2179 * We're redefining, so we have to take over an
2180 * existing SMacro structure. This means freeing
2181 * what was already in it.
2182 */
2183 nasm_free(smac->name);
2184 free_tlist(smac->expansion);
2185 }
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002186 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002187 smtbl = ctx ? &ctx->localmac : &smacros;
2188 smhead = (SMacro **) hash_findi_add(smtbl, mname);
Cyrill Gorcunov574fbf12010-11-11 13:44:51 +03002189 smac = nasm_zalloc(sizeof(SMacro));
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002190 smac->next = *smhead;
2191 *smhead = smac;
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002192 }
2193 smac->name = nasm_strdup(mname);
2194 smac->casesense = casesense;
2195 smac->nparam = nparam;
2196 smac->expansion = expansion;
2197 smac->in_progress = false;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002198 return true; /* Success */
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002199}
2200
2201/*
2202 * Undefine an smacro
2203 */
2204static void undef_smacro(Context *ctx, const char *mname)
2205{
2206 SMacro **smhead, *s, **sp;
H. Peter Anvin166c2472008-05-28 12:28:58 -07002207 struct hash_table *smtbl;
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002208
H. Peter Anvin166c2472008-05-28 12:28:58 -07002209 smtbl = ctx ? &ctx->localmac : &smacros;
2210 smhead = (SMacro **)hash_findi(smtbl, mname, NULL);
H. Peter Anvin70653092007-10-19 14:42:29 -07002211
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002212 if (smhead) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002213 /*
2214 * We now have a macro name... go hunt for it.
2215 */
2216 sp = smhead;
2217 while ((s = *sp) != NULL) {
2218 if (!mstrcmp(s->name, mname, s->casesense)) {
2219 *sp = s->next;
2220 nasm_free(s->name);
2221 free_tlist(s->expansion);
2222 nasm_free(s);
2223 } else {
2224 sp = &s->next;
2225 }
2226 }
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002227 }
2228}
2229
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002230/*
H. Peter Anvina26433d2008-07-16 14:40:01 -07002231 * Parse a mmacro specification.
2232 */
Keith Kaniosb307a4f2010-11-06 17:41:51 -05002233static bool parse_mmacro_spec(Token *tline, ExpDef *def, const char *directive)
H. Peter Anvina26433d2008-07-16 14:40:01 -07002234{
2235 bool err;
2236
2237 tline = tline->next;
2238 skip_white_(tline);
2239 tline = expand_id(tline);
2240 if (!tok_type_(tline, TOK_ID)) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002241 error(ERR_NONFATAL, "`%s' expects a macro name", directive);
2242 return false;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002243 }
Victor van den Elzenb916ede2008-07-23 15:14:22 +02002244
H. Peter Anvina26433d2008-07-16 14:40:01 -07002245 def->name = nasm_strdup(tline->text);
2246 def->plus = false;
2247 def->nolist = false;
Keith Kaniosb307a4f2010-11-06 17:41:51 -05002248// def->in_progress = 0;
2249// def->rep_nest = NULL;
Victor van den Elzenb916ede2008-07-23 15:14:22 +02002250 def->nparam_min = 0;
2251 def->nparam_max = 0;
2252
H. Peter Anvina26433d2008-07-16 14:40:01 -07002253 tline = expand_smacro(tline->next);
2254 skip_white_(tline);
2255 if (!tok_type_(tline, TOK_NUMBER)) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002256 error(ERR_NONFATAL, "`%s' expects a parameter count", directive);
H. Peter Anvina26433d2008-07-16 14:40:01 -07002257 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002258 def->nparam_min = def->nparam_max =
2259 readnum(tline->text, &err);
2260 if (err)
2261 error(ERR_NONFATAL,
2262 "unable to parse parameter count `%s'", tline->text);
H. Peter Anvina26433d2008-07-16 14:40:01 -07002263 }
2264 if (tline && tok_is_(tline->next, "-")) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002265 tline = tline->next->next;
2266 if (tok_is_(tline, "*")) {
2267 def->nparam_max = INT_MAX;
2268 } else if (!tok_type_(tline, TOK_NUMBER)) {
2269 error(ERR_NONFATAL,
2270 "`%s' expects a parameter count after `-'", directive);
2271 } else {
2272 def->nparam_max = readnum(tline->text, &err);
2273 if (err) {
2274 error(ERR_NONFATAL, "unable to parse parameter count `%s'",
2275 tline->text);
2276 }
2277 if (def->nparam_min > def->nparam_max) {
2278 error(ERR_NONFATAL, "minimum parameter count exceeds maximum");
2279 }
2280 }
H. Peter Anvina26433d2008-07-16 14:40:01 -07002281 }
2282 if (tline && tok_is_(tline->next, "+")) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002283 tline = tline->next;
2284 def->plus = true;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002285 }
2286 if (tline && tok_type_(tline->next, TOK_ID) &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002287 !nasm_stricmp(tline->next->text, ".nolist")) {
2288 tline = tline->next;
2289 def->nolist = true;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002290 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002291
H. Peter Anvina26433d2008-07-16 14:40:01 -07002292 /*
2293 * Handle default parameters.
2294 */
2295 if (tline && tline->next) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002296 def->dlist = tline->next;
2297 tline->next = NULL;
2298 count_mmac_params(def->dlist, &def->ndefs, &def->defaults);
H. Peter Anvina26433d2008-07-16 14:40:01 -07002299 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002300 def->dlist = NULL;
2301 def->defaults = NULL;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002302 }
Keith Kaniosb307a4f2010-11-06 17:41:51 -05002303 def->line = NULL;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002304
H. Peter Anvin89cee572009-07-15 09:16:54 -04002305 if (def->defaults && def->ndefs > def->nparam_max - def->nparam_min &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002306 !def->plus)
2307 error(ERR_WARNING|ERR_PASS1|ERR_WARN_MDP,
2308 "too many default macro parameters");
Victor van den Elzenb916ede2008-07-23 15:14:22 +02002309
H. Peter Anvina26433d2008-07-16 14:40:01 -07002310 return true;
2311}
2312
2313
2314/*
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002315 * Decode a size directive
2316 */
2317static int parse_size(const char *str) {
2318 static const char *size_names[] =
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002319 { "byte", "dword", "oword", "qword", "tword", "word", "yword" };
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002320 static const int sizes[] =
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002321 { 0, 1, 4, 16, 8, 10, 2, 32 };
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002322
Cyrill Gorcunova7319242010-06-03 22:04:36 +04002323 return sizes[bsii(str, size_names, ARRAY_SIZE(size_names))+1];
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002324}
2325
Ed Beroset3ab3f412002-06-11 03:31:49 +00002326/**
2327 * find and process preprocessor directive in passed line
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002328 * Find out if a line contains a preprocessor directive, and deal
2329 * with it if so.
H. Peter Anvin70653092007-10-19 14:42:29 -07002330 *
Ed Beroset3ab3f412002-06-11 03:31:49 +00002331 * If a directive _is_ found, it is the responsibility of this routine
2332 * (and not the caller) to free_tlist() the line.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002333 *
Ed Beroset3ab3f412002-06-11 03:31:49 +00002334 * @param tline a pointer to the current tokeninzed line linked list
2335 * @return DIRECTIVE_FOUND or NO_DIRECTIVE_FOUND
H. Peter Anvin70653092007-10-19 14:42:29 -07002336 *
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002337 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002338static int do_directive(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00002339{
H. Peter Anvin4169a472007-09-12 01:29:43 +00002340 enum preproc_token i;
2341 int j;
H. Peter Anvin70055962007-10-11 00:05:31 -07002342 bool err;
2343 int nparam;
2344 bool nolist;
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07002345 bool casesense;
H. Peter Anvin8cfdb9d2007-09-14 18:36:01 -07002346 int k, m;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002347 int offset;
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08002348 char *p, *pp;
2349 const char *mname;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002350 Include *inc;
2351 Context *ctx;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002352 Line *l;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002353 Token *t, *tt, *param_start, *macro_start, *last, **tptr, *origline;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002354 struct tokenval tokval;
2355 expr *evalresult;
Keith Kaniosb307a4f2010-11-06 17:41:51 -05002356 ExpDef *ed, *eed, **edhead;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002357 ExpInv *ei, *eei;
H. Peter Anvinf8ba53e2007-10-11 10:11:57 -07002358 int64_t count;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07002359 size_t len;
H. Peter Anvin8e3f75e2008-09-24 00:21:58 -07002360 int severity;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002361
2362 origline = tline;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002363
H. Peter Anvineba20a72002-04-30 20:53:55 +00002364 skip_white_(tline);
H. Peter Anvinf2936d72008-06-04 15:11:23 -07002365 if (!tline || !tok_type_(tline, TOK_PREPROC_ID) ||
H. Peter Anvine2c80182005-01-15 22:15:51 +00002366 (tline->text[1] == '%' || tline->text[1] == '$'
2367 || tline->text[1] == '!'))
2368 return NO_DIRECTIVE_FOUND;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002369
H. Peter Anvin4169a472007-09-12 01:29:43 +00002370 i = pp_token_hash(tline->text);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002371
H. Peter Anvin4169a472007-09-12 01:29:43 +00002372 switch (i) {
2373 case PP_INVALID:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002374 if (defining != NULL) return NO_DIRECTIVE_FOUND;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002375 error(ERR_NONFATAL, "unknown preprocessor directive `%s'",
2376 tline->text);
2377 return NO_DIRECTIVE_FOUND; /* didn't get it */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002378
H. Peter Anvine2c80182005-01-15 22:15:51 +00002379 case PP_STACKSIZE:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002380 if (defining != NULL) return NO_DIRECTIVE_FOUND;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002381 /* Directive to tell NASM what the default stack size is. The
2382 * default is for a 16-bit stack, and this can be overriden with
2383 * %stacksize large.
H. Peter Anvine2c80182005-01-15 22:15:51 +00002384 */
2385 tline = tline->next;
2386 if (tline && tline->type == TOK_WHITESPACE)
2387 tline = tline->next;
2388 if (!tline || tline->type != TOK_ID) {
2389 error(ERR_NONFATAL, "`%%stacksize' missing size parameter");
2390 free_tlist(origline);
2391 return DIRECTIVE_FOUND;
2392 }
2393 if (nasm_stricmp(tline->text, "flat") == 0) {
2394 /* All subsequent ARG directives are for a 32-bit stack */
2395 StackSize = 4;
2396 StackPointer = "ebp";
2397 ArgOffset = 8;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002398 LocalOffset = 0;
Charles Crayne7eaf9192007-11-08 22:11:14 -08002399 } else if (nasm_stricmp(tline->text, "flat64") == 0) {
2400 /* All subsequent ARG directives are for a 64-bit stack */
2401 StackSize = 8;
2402 StackPointer = "rbp";
Per Jessen53252e02010-02-11 00:16:59 +03002403 ArgOffset = 16;
Charles Crayne7eaf9192007-11-08 22:11:14 -08002404 LocalOffset = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002405 } else if (nasm_stricmp(tline->text, "large") == 0) {
2406 /* All subsequent ARG directives are for a 16-bit stack,
2407 * far function call.
2408 */
2409 StackSize = 2;
2410 StackPointer = "bp";
2411 ArgOffset = 4;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002412 LocalOffset = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002413 } else if (nasm_stricmp(tline->text, "small") == 0) {
2414 /* All subsequent ARG directives are for a 16-bit stack,
2415 * far function call. We don't support near functions.
2416 */
2417 StackSize = 2;
2418 StackPointer = "bp";
2419 ArgOffset = 6;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002420 LocalOffset = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002421 } else {
2422 error(ERR_NONFATAL, "`%%stacksize' invalid size type");
2423 free_tlist(origline);
2424 return DIRECTIVE_FOUND;
2425 }
2426 free_tlist(origline);
2427 return DIRECTIVE_FOUND;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002428
H. Peter Anvine2c80182005-01-15 22:15:51 +00002429 case PP_ARG:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002430 if (defining != NULL) return NO_DIRECTIVE_FOUND;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002431 /* TASM like ARG directive to define arguments to functions, in
2432 * the following form:
2433 *
2434 * ARG arg1:WORD, arg2:DWORD, arg4:QWORD
2435 */
2436 offset = ArgOffset;
2437 do {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00002438 char *arg, directive[256];
H. Peter Anvine2c80182005-01-15 22:15:51 +00002439 int size = StackSize;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002440
H. Peter Anvine2c80182005-01-15 22:15:51 +00002441 /* Find the argument name */
2442 tline = tline->next;
2443 if (tline && tline->type == TOK_WHITESPACE)
2444 tline = tline->next;
2445 if (!tline || tline->type != TOK_ID) {
2446 error(ERR_NONFATAL, "`%%arg' missing argument parameter");
2447 free_tlist(origline);
2448 return DIRECTIVE_FOUND;
2449 }
2450 arg = tline->text;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002451
H. Peter Anvine2c80182005-01-15 22:15:51 +00002452 /* Find the argument size type */
2453 tline = tline->next;
2454 if (!tline || tline->type != TOK_OTHER
2455 || tline->text[0] != ':') {
2456 error(ERR_NONFATAL,
2457 "Syntax error processing `%%arg' directive");
2458 free_tlist(origline);
2459 return DIRECTIVE_FOUND;
2460 }
2461 tline = tline->next;
2462 if (!tline || tline->type != TOK_ID) {
2463 error(ERR_NONFATAL, "`%%arg' missing size type parameter");
2464 free_tlist(origline);
2465 return DIRECTIVE_FOUND;
2466 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002467
H. Peter Anvine2c80182005-01-15 22:15:51 +00002468 /* Allow macro expansion of type parameter */
Keith Kaniosb7a89542007-04-12 02:40:54 +00002469 tt = tokenize(tline->text);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002470 tt = expand_smacro(tt);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002471 size = parse_size(tt->text);
2472 if (!size) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002473 error(ERR_NONFATAL,
2474 "Invalid size type for `%%arg' missing directive");
2475 free_tlist(tt);
2476 free_tlist(origline);
2477 return DIRECTIVE_FOUND;
2478 }
2479 free_tlist(tt);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002480
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002481 /* Round up to even stack slots */
2482 size = ALIGN(size, StackSize);
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002483
H. Peter Anvine2c80182005-01-15 22:15:51 +00002484 /* Now define the macro for the argument */
2485 snprintf(directive, sizeof(directive), "%%define %s (%s+%d)",
2486 arg, StackPointer, offset);
Keith Kaniosb7a89542007-04-12 02:40:54 +00002487 do_directive(tokenize(directive));
H. Peter Anvine2c80182005-01-15 22:15:51 +00002488 offset += size;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002489
H. Peter Anvine2c80182005-01-15 22:15:51 +00002490 /* Move to the next argument in the list */
2491 tline = tline->next;
2492 if (tline && tline->type == TOK_WHITESPACE)
2493 tline = tline->next;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002494 } while (tline && tline->type == TOK_OTHER && tline->text[0] == ',');
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002495 ArgOffset = offset;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002496 free_tlist(origline);
2497 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002498
H. Peter Anvine2c80182005-01-15 22:15:51 +00002499 case PP_LOCAL:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002500 if (defining != NULL) return NO_DIRECTIVE_FOUND;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002501 /* TASM like LOCAL directive to define local variables for a
2502 * function, in the following form:
2503 *
2504 * LOCAL local1:WORD, local2:DWORD, local4:QWORD = LocalSize
2505 *
2506 * The '= LocalSize' at the end is ignored by NASM, but is
2507 * required by TASM to define the local parameter size (and used
2508 * by the TASM macro package).
2509 */
2510 offset = LocalOffset;
2511 do {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00002512 char *local, directive[256];
H. Peter Anvine2c80182005-01-15 22:15:51 +00002513 int size = StackSize;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002514
H. Peter Anvine2c80182005-01-15 22:15:51 +00002515 /* Find the argument name */
2516 tline = tline->next;
2517 if (tline && tline->type == TOK_WHITESPACE)
2518 tline = tline->next;
2519 if (!tline || tline->type != TOK_ID) {
2520 error(ERR_NONFATAL,
2521 "`%%local' missing argument parameter");
2522 free_tlist(origline);
2523 return DIRECTIVE_FOUND;
2524 }
2525 local = tline->text;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002526
H. Peter Anvine2c80182005-01-15 22:15:51 +00002527 /* Find the argument size type */
2528 tline = tline->next;
2529 if (!tline || tline->type != TOK_OTHER
2530 || tline->text[0] != ':') {
2531 error(ERR_NONFATAL,
2532 "Syntax error processing `%%local' directive");
2533 free_tlist(origline);
2534 return DIRECTIVE_FOUND;
2535 }
2536 tline = tline->next;
2537 if (!tline || tline->type != TOK_ID) {
2538 error(ERR_NONFATAL,
2539 "`%%local' missing size type parameter");
2540 free_tlist(origline);
2541 return DIRECTIVE_FOUND;
2542 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00002543
H. Peter Anvine2c80182005-01-15 22:15:51 +00002544 /* Allow macro expansion of type parameter */
Keith Kaniosb7a89542007-04-12 02:40:54 +00002545 tt = tokenize(tline->text);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002546 tt = expand_smacro(tt);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002547 size = parse_size(tt->text);
2548 if (!size) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002549 error(ERR_NONFATAL,
2550 "Invalid size type for `%%local' missing directive");
2551 free_tlist(tt);
2552 free_tlist(origline);
2553 return DIRECTIVE_FOUND;
2554 }
2555 free_tlist(tt);
H. Peter Anvin734b1882002-04-30 21:01:08 +00002556
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002557 /* Round up to even stack slots */
2558 size = ALIGN(size, StackSize);
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002559
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002560 offset += size; /* Negative offset, increment before */
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002561
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002562 /* Now define the macro for the argument */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002563 snprintf(directive, sizeof(directive), "%%define %s (%s-%d)",
2564 local, StackPointer, offset);
Keith Kaniosb7a89542007-04-12 02:40:54 +00002565 do_directive(tokenize(directive));
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002566
H. Peter Anvine2c80182005-01-15 22:15:51 +00002567 /* Now define the assign to setup the enter_c macro correctly */
2568 snprintf(directive, sizeof(directive),
2569 "%%assign %%$localsize %%$localsize+%d", size);
Keith Kaniosb7a89542007-04-12 02:40:54 +00002570 do_directive(tokenize(directive));
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002571
H. Peter Anvine2c80182005-01-15 22:15:51 +00002572 /* Move to the next argument in the list */
2573 tline = tline->next;
2574 if (tline && tline->type == TOK_WHITESPACE)
2575 tline = tline->next;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002576 } while (tline && tline->type == TOK_OTHER && tline->text[0] == ',');
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002577 LocalOffset = offset;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002578 free_tlist(origline);
2579 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002580
H. Peter Anvine2c80182005-01-15 22:15:51 +00002581 case PP_CLEAR:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002582 if (defining != NULL) return NO_DIRECTIVE_FOUND;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002583 if (tline->next)
H. Peter Anvin917a3492008-09-24 09:14:49 -07002584 error(ERR_WARNING|ERR_PASS1,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002585 "trailing garbage after `%%clear' ignored");
2586 free_macros();
2587 init_macros();
H. Peter Anvine2c80182005-01-15 22:15:51 +00002588 free_tlist(origline);
2589 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002590
H. Peter Anvin418ca702008-05-30 10:42:30 -07002591 case PP_DEPEND:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002592 if (defining != NULL) return NO_DIRECTIVE_FOUND;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002593 t = tline->next = expand_smacro(tline->next);
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002594 skip_white_(t);
2595 if (!t || (t->type != TOK_STRING &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002596 t->type != TOK_INTERNAL_STRING)) {
H. Peter Anvin418ca702008-05-30 10:42:30 -07002597 error(ERR_NONFATAL, "`%%depend' expects a file name");
2598 free_tlist(origline);
2599 return DIRECTIVE_FOUND; /* but we did _something_ */
2600 }
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002601 if (t->next)
H. Peter Anvin917a3492008-09-24 09:14:49 -07002602 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvin418ca702008-05-30 10:42:30 -07002603 "trailing garbage after `%%depend' ignored");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002604 p = t->text;
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002605 if (t->type != TOK_INTERNAL_STRING)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002606 nasm_unquote_cstr(p, i);
2607 if (dephead && !in_list(*dephead, p)) {
2608 StrList *sl = nasm_malloc(strlen(p)+1+sizeof sl->next);
2609 sl->next = NULL;
2610 strcpy(sl->str, p);
2611 *deptail = sl;
2612 deptail = &sl->next;
2613 }
2614 free_tlist(origline);
H. Peter Anvin418ca702008-05-30 10:42:30 -07002615 return DIRECTIVE_FOUND;
2616
2617 case PP_INCLUDE:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002618 if (defining != NULL) return NO_DIRECTIVE_FOUND;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002619 t = tline->next = expand_smacro(tline->next);
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002620 skip_white_(t);
H. Peter Anvind2456592008-06-19 15:04:18 -07002621
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002622 if (!t || (t->type != TOK_STRING &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002623 t->type != TOK_INTERNAL_STRING)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002624 error(ERR_NONFATAL, "`%%include' expects a file name");
2625 free_tlist(origline);
2626 return DIRECTIVE_FOUND; /* but we did _something_ */
2627 }
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002628 if (t->next)
H. Peter Anvin917a3492008-09-24 09:14:49 -07002629 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +00002630 "trailing garbage after `%%include' ignored");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002631 p = t->text;
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002632 if (t->type != TOK_INTERNAL_STRING)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002633 nasm_unquote_cstr(p, i);
Cyrill Gorcunov55cc4d02010-11-10 23:12:06 +03002634 inc = nasm_zalloc(sizeof(Include));
H. Peter Anvine2c80182005-01-15 22:15:51 +00002635 inc->next = istk;
H. Peter Anvin2b1c3b92008-06-06 10:38:46 -07002636 inc->fp = inc_fopen(p, dephead, &deptail, pass == 0);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002637 if (!inc->fp) {
2638 /* -MG given but file not found */
2639 nasm_free(inc);
2640 } else {
2641 inc->fname = src_set_fname(nasm_strdup(p));
2642 inc->lineno = src_set_linnum(0);
2643 inc->lineinc = 1;
2644 inc->expansion = NULL;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002645 istk = inc;
2646 list->uplevel(LIST_INCLUDE);
2647 }
2648 free_tlist(origline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002649 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002650
H. Peter Anvind2456592008-06-19 15:04:18 -07002651 case PP_USE:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002652 if (defining != NULL) return NO_DIRECTIVE_FOUND;
H. Peter Anvinf4ae5ad2008-06-19 18:39:24 -07002653 {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002654 static macros_t *use_pkg;
2655 const char *pkg_macro = NULL;
H. Peter Anvinf4ae5ad2008-06-19 18:39:24 -07002656
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002657 tline = tline->next;
2658 skip_white_(tline);
2659 tline = expand_id(tline);
H. Peter Anvind2456592008-06-19 15:04:18 -07002660
H. Peter Anvin264b7b92008-10-24 16:38:17 -07002661 if (!tline || (tline->type != TOK_STRING &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002662 tline->type != TOK_INTERNAL_STRING &&
2663 tline->type != TOK_ID)) {
H. Peter Anvin926fc402008-06-19 16:26:12 -07002664 error(ERR_NONFATAL, "`%%use' expects a package name");
H. Peter Anvind2456592008-06-19 15:04:18 -07002665 free_tlist(origline);
2666 return DIRECTIVE_FOUND; /* but we did _something_ */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002667 }
H. Peter Anvin264b7b92008-10-24 16:38:17 -07002668 if (tline->next)
H. Peter Anvin917a3492008-09-24 09:14:49 -07002669 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvind2456592008-06-19 15:04:18 -07002670 "trailing garbage after `%%use' ignored");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002671 if (tline->type == TOK_STRING)
2672 nasm_unquote_cstr(tline->text, i);
2673 use_pkg = nasm_stdmac_find_package(tline->text);
2674 if (!use_pkg)
2675 error(ERR_NONFATAL, "unknown `%%use' package: %s", tline->text);
2676 else
2677 pkg_macro = (char *)use_pkg + 1; /* The first string will be <%define>__USE_*__ */
Victor van den Elzen35eb2ea2010-03-10 22:33:48 +01002678 if (use_pkg && ! smacro_defined(NULL, pkg_macro, 0, NULL, true)) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002679 /* Not already included, go ahead and include it */
2680 stdmacpos = use_pkg;
2681 }
2682 free_tlist(origline);
H. Peter Anvind2456592008-06-19 15:04:18 -07002683 return DIRECTIVE_FOUND;
H. Peter Anvinf4ae5ad2008-06-19 18:39:24 -07002684 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002685 case PP_PUSH:
H. Peter Anvine2c80182005-01-15 22:15:51 +00002686 case PP_REPL:
H. Peter Anvin42b56392008-10-24 16:24:21 -07002687 case PP_POP:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002688 if (defining != NULL) return NO_DIRECTIVE_FOUND;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002689 tline = tline->next;
2690 skip_white_(tline);
2691 tline = expand_id(tline);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002692 if (tline) {
2693 if (!tok_type_(tline, TOK_ID)) {
2694 error(ERR_NONFATAL, "`%s' expects a context identifier",
2695 pp_directives[i]);
2696 free_tlist(origline);
2697 return DIRECTIVE_FOUND; /* but we did _something_ */
2698 }
2699 if (tline->next)
2700 error(ERR_WARNING|ERR_PASS1,
2701 "trailing garbage after `%s' ignored",
2702 pp_directives[i]);
2703 p = nasm_strdup(tline->text);
2704 } else {
2705 p = NULL; /* Anonymous */
2706 }
H. Peter Anvin42b56392008-10-24 16:24:21 -07002707
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002708 if (i == PP_PUSH) {
Cyrill Gorcunov574fbf12010-11-11 13:44:51 +03002709 ctx = nasm_zalloc(sizeof(Context));
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002710 ctx->next = cstk;
2711 hash_init(&ctx->localmac, HASH_SMALL);
2712 ctx->name = p;
2713 ctx->number = unique++;
2714 cstk = ctx;
2715 } else {
2716 /* %pop or %repl */
2717 if (!cstk) {
2718 error(ERR_NONFATAL, "`%s': context stack is empty",
2719 pp_directives[i]);
2720 } else if (i == PP_POP) {
2721 if (p && (!cstk->name || nasm_stricmp(p, cstk->name)))
2722 error(ERR_NONFATAL, "`%%pop' in wrong context: %s, "
2723 "expected %s",
2724 cstk->name ? cstk->name : "anonymous", p);
2725 else
2726 ctx_pop();
2727 } else {
2728 /* i == PP_REPL */
2729 nasm_free(cstk->name);
2730 cstk->name = p;
2731 p = NULL;
2732 }
2733 nasm_free(p);
2734 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002735 free_tlist(origline);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002736 return DIRECTIVE_FOUND;
H. Peter Anvin8e3f75e2008-09-24 00:21:58 -07002737 case PP_FATAL:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002738 severity = ERR_FATAL;
2739 goto issue_error;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002740 case PP_ERROR:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002741 severity = ERR_NONFATAL;
2742 goto issue_error;
H. Peter Anvin7df04172008-06-10 18:27:38 -07002743 case PP_WARNING:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002744 severity = ERR_WARNING|ERR_WARN_USER;
2745 goto issue_error;
H. Peter Anvin8e3f75e2008-09-24 00:21:58 -07002746
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002747issue_error:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002748 if (defining != NULL) return NO_DIRECTIVE_FOUND;
H. Peter Anvin7df04172008-06-10 18:27:38 -07002749 {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002750 /* Only error out if this is the final pass */
2751 if (pass != 2 && i != PP_FATAL)
2752 return DIRECTIVE_FOUND;
2753
2754 tline->next = expand_smacro(tline->next);
2755 tline = tline->next;
2756 skip_white_(tline);
2757 t = tline ? tline->next : NULL;
2758 skip_white_(t);
2759 if (tok_type_(tline, TOK_STRING) && !t) {
2760 /* The line contains only a quoted string */
2761 p = tline->text;
2762 nasm_unquote(p, NULL); /* Ignore NUL character truncation */
2763 error(severity, "%s", p);
2764 } else {
2765 /* Not a quoted string, or more than a quoted string */
2766 p = detoken(tline, false);
2767 error(severity, "%s", p);
2768 nasm_free(p);
2769 }
2770 free_tlist(origline);
2771 return DIRECTIVE_FOUND;
H. Peter Anvin7df04172008-06-10 18:27:38 -07002772 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00002773
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002774 CASE_PP_IF:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002775 if (defining != NULL) {
2776 if (defining->type == EXP_IF) {
2777 defining->def_depth ++;
2778 }
2779 return NO_DIRECTIVE_FOUND;
2780 }
2781 if ((istk->expansion != NULL) &&
2782 (istk->expansion->emitting == false)) {
2783 j = COND_NEVER;
2784 } else {
2785 j = if_condition(tline->next, i);
2786 tline->next = NULL; /* it got freed */
2787 j = (((j < 0) ? COND_NEVER : j) ? COND_IF_TRUE : COND_IF_FALSE);
2788 }
2789 ed = new_ExpDef(EXP_IF);
2790 ed->state = j;
2791 ed->nolist = NULL;
2792 ed->def_depth = 0;
2793 ed->cur_depth = 0;
2794 ed->max_depth = 0;
2795 ed->ignoring = ((ed->state == COND_IF_TRUE) ? false : true);
2796 ed->prev = defining;
2797 defining = ed;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002798 free_tlist(origline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002799 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002800
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002801 CASE_PP_ELIF:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002802 if (defining != NULL) {
2803 if ((defining->type != EXP_IF) || (defining->def_depth > 0)) {
2804 return NO_DIRECTIVE_FOUND;
2805 }
2806 }
Cyrill Gorcunov82667ff2011-06-25 19:34:19 +04002807 if ((defining == NULL) || (defining->type != EXP_IF)) {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002808 error(ERR_FATAL, "`%s': no matching `%%if'", pp_directives[i]);
2809 }
Keith Kaniosb307a4f2010-11-06 17:41:51 -05002810 switch (defining->state) {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002811 case COND_IF_TRUE:
2812 defining->state = COND_DONE;
2813 defining->ignoring = true;
2814 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002815
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002816 case COND_DONE:
2817 case COND_NEVER:
2818 defining->ignoring = true;
2819 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002820
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002821 case COND_ELSE_TRUE:
2822 case COND_ELSE_FALSE:
2823 error_precond(ERR_WARNING|ERR_PASS1,
2824 "`%%elif' after `%%else' ignored");
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002825 defining->state = COND_NEVER;
2826 defining->ignoring = true;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002827 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002828
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002829 case COND_IF_FALSE:
2830 /*
2831 * IMPORTANT: In the case of %if, we will already have
2832 * called expand_mmac_params(); however, if we're
2833 * processing an %elif we must have been in a
2834 * non-emitting mode, which would have inhibited
2835 * the normal invocation of expand_mmac_params().
2836 * Therefore, we have to do it explicitly here.
2837 */
2838 j = if_condition(expand_mmac_params(tline->next), i);
2839 tline->next = NULL; /* it got freed */
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002840 defining->state =
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002841 j < 0 ? COND_NEVER : j ? COND_IF_TRUE : COND_IF_FALSE;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002842 defining->ignoring = ((defining->state == COND_IF_TRUE) ? false : true);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002843 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002844 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002845 free_tlist(origline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002846 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002847
H. Peter Anvine2c80182005-01-15 22:15:51 +00002848 case PP_ELSE:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002849 if (defining != NULL) {
2850 if ((defining->type != EXP_IF) || (defining->def_depth > 0)) {
2851 return NO_DIRECTIVE_FOUND;
2852 }
2853 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002854 if (tline->next)
H. Peter Anvin917a3492008-09-24 09:14:49 -07002855 error_precond(ERR_WARNING|ERR_PASS1,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002856 "trailing garbage after `%%else' ignored");
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002857 if ((defining == NULL) || (defining->type != EXP_IF)) {
2858 error(ERR_FATAL, "`%s': no matching `%%if'", pp_directives[i]);
2859 }
Keith Kaniosb307a4f2010-11-06 17:41:51 -05002860 switch (defining->state) {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002861 case COND_IF_TRUE:
2862 case COND_DONE:
2863 defining->state = COND_ELSE_FALSE;
2864 defining->ignoring = true;
2865 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002866
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002867 case COND_NEVER:
2868 defining->ignoring = true;
2869 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002870
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002871 case COND_IF_FALSE:
2872 defining->state = COND_ELSE_TRUE;
2873 defining->ignoring = false;
2874 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002875
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002876 case COND_ELSE_TRUE:
2877 case COND_ELSE_FALSE:
2878 error_precond(ERR_WARNING|ERR_PASS1,
2879 "`%%else' after `%%else' ignored.");
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002880 defining->state = COND_NEVER;
2881 defining->ignoring = true;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002882 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002883 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002884 free_tlist(origline);
2885 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002886
H. Peter Anvine2c80182005-01-15 22:15:51 +00002887 case PP_ENDIF:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002888 if (defining != NULL) {
2889 if (defining->type == EXP_IF) {
2890 if (defining->def_depth > 0) {
2891 defining->def_depth --;
2892 return NO_DIRECTIVE_FOUND;
2893 }
2894 } else {
2895 return NO_DIRECTIVE_FOUND;
2896 }
2897 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002898 if (tline->next)
H. Peter Anvin917a3492008-09-24 09:14:49 -07002899 error_precond(ERR_WARNING|ERR_PASS1,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002900 "trailing garbage after `%%endif' ignored");
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002901 if ((defining == NULL) || (defining->type != EXP_IF)) {
2902 error(ERR_NONFATAL, "`%%endif': no matching `%%if'");
2903 return DIRECTIVE_FOUND;
2904 }
2905 ed = defining;
2906 defining = ed->prev;
2907 ed->prev = expansions;
2908 expansions = ed;
2909 ei = new_ExpInv(EXP_IF, ed);
2910 ei->current = ed->line;
2911 ei->emitting = true;
2912 ei->prev = istk->expansion;
2913 istk->expansion = ei;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002914 free_tlist(origline);
2915 return DIRECTIVE_FOUND;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002916
H. Peter Anvindb8f96e2009-07-15 09:07:29 -04002917 case PP_RMACRO:
2918 case PP_IRMACRO:
H. Peter Anvine2c80182005-01-15 22:15:51 +00002919 case PP_MACRO:
2920 case PP_IMACRO:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002921 if (defining != NULL) {
2922 if (defining->type == EXP_MMACRO) {
2923 defining->def_depth ++;
2924 }
2925 return NO_DIRECTIVE_FOUND;
2926 }
2927 ed = new_ExpDef(EXP_MMACRO);
2928 ed->max_depth =
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002929 (i == PP_RMACRO) || (i == PP_IRMACRO) ? DEADMAN_LIMIT : 0;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002930 ed->casesense = (i == PP_MACRO) || (i == PP_RMACRO);
Keith Kaniosb307a4f2010-11-06 17:41:51 -05002931 if (!parse_mmacro_spec(tline, ed, pp_directives[i])) {
2932 nasm_free(ed);
2933 ed = NULL;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002934 return DIRECTIVE_FOUND;
2935 }
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002936 ed->def_depth = 0;
2937 ed->cur_depth = 0;
2938 ed->max_depth = (ed->max_depth + 1);
2939 ed->ignoring = false;
2940 ed->prev = defining;
2941 defining = ed;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002942
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002943 eed = (ExpDef *) hash_findix(&expdefs, ed->name);
2944 while (eed) {
Cyrill Gorcunov574fbf12010-11-11 13:44:51 +03002945 if (!strcmp(eed->name, ed->name) &&
2946 (eed->nparam_min <= ed->nparam_max || ed->plus) &&
2947 (ed->nparam_min <= eed->nparam_max || eed->plus)) {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002948 error(ERR_WARNING|ERR_PASS1,
2949 "redefining multi-line macro `%s'", ed->name);
2950 return DIRECTIVE_FOUND;
2951 }
2952 eed = eed->next;
2953 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002954 free_tlist(origline);
2955 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002956
H. Peter Anvine2c80182005-01-15 22:15:51 +00002957 case PP_ENDM:
2958 case PP_ENDMACRO:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002959 if (defining != NULL) {
2960 if (defining->type == EXP_MMACRO) {
2961 if (defining->def_depth > 0) {
2962 defining->def_depth --;
2963 return NO_DIRECTIVE_FOUND;
2964 }
2965 } else {
2966 return NO_DIRECTIVE_FOUND;
2967 }
2968 }
2969 if (!(defining) || (defining->type != EXP_MMACRO)) {
2970 error(ERR_NONFATAL, "`%s': not defining a macro", tline->text);
2971 return DIRECTIVE_FOUND;
2972 }
2973 edhead = (ExpDef **) hash_findi_add(&expdefs, defining->name);
2974 defining->next = *edhead;
2975 *edhead = defining;
2976 ed = defining;
2977 defining = ed->prev;
2978 ed->prev = expansions;
2979 expansions = ed;
2980 ed = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002981 free_tlist(origline);
2982 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002983
H. Peter Anvin89cee572009-07-15 09:16:54 -04002984 case PP_EXITMACRO:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002985 if (defining != NULL) return NO_DIRECTIVE_FOUND;
2986 /*
2987 * We must search along istk->expansion until we hit a
2988 * macro invocation. Then we disable the emitting state(s)
2989 * between exitmacro and endmacro.
2990 */
2991 for (ei = istk->expansion; ei != NULL; ei = ei->prev) {
2992 if(ei->type == EXP_MMACRO) {
2993 break;
2994 }
2995 }
2996
2997 if (ei != NULL) {
2998 /*
2999 * Set all invocations leading back to the macro
3000 * invocation to a non-emitting state.
3001 */
3002 for (eei = istk->expansion; eei != ei; eei = eei->prev) {
3003 eei->emitting = false;
3004 }
3005 eei->emitting = false;
3006 } else {
3007 error(ERR_NONFATAL, "`%%exitmacro' not within `%%macro' block");
3008 }
Keith Kanios852f1ee2009-07-12 00:19:55 -05003009 free_tlist(origline);
3010 return DIRECTIVE_FOUND;
3011
H. Peter Anvina26433d2008-07-16 14:40:01 -07003012 case PP_UNMACRO:
3013 case PP_UNIMACRO:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003014 if (defining != NULL) return NO_DIRECTIVE_FOUND;
H. Peter Anvina26433d2008-07-16 14:40:01 -07003015 {
Keith Kaniosb307a4f2010-11-06 17:41:51 -05003016 ExpDef **ed_p;
3017 ExpDef spec;
H. Peter Anvina26433d2008-07-16 14:40:01 -07003018
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003019 spec.casesense = (i == PP_UNMACRO);
3020 if (!parse_mmacro_spec(tline, &spec, pp_directives[i])) {
3021 return DIRECTIVE_FOUND;
3022 }
Keith Kaniosb307a4f2010-11-06 17:41:51 -05003023 ed_p = (ExpDef **) hash_findi(&expdefs, spec.name, NULL);
3024 while (ed_p && *ed_p) {
3025 ed = *ed_p;
3026 if (ed->casesense == spec.casesense &&
3027 !mstrcmp(ed->name, spec.name, spec.casesense) &&
3028 ed->nparam_min == spec.nparam_min &&
3029 ed->nparam_max == spec.nparam_max &&
3030 ed->plus == spec.plus) {
Keith Kaniosc98a5b42010-12-18 12:17:31 -06003031 if (ed->cur_depth > 0) {
Keith Kanios21d885b2010-12-18 12:22:21 -06003032 error(ERR_NONFATAL, "`%s' ignored on active macro",
Keith Kaniosc98a5b42010-12-18 12:17:31 -06003033 pp_directives[i]);
3034 break;
3035 } else {
3036 *ed_p = ed->next;
3037 free_expdef(ed);
3038 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003039 } else {
Keith Kaniosb307a4f2010-11-06 17:41:51 -05003040 ed_p = &ed->next;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003041 }
3042 }
3043 free_tlist(origline);
3044 free_tlist(spec.dlist);
3045 return DIRECTIVE_FOUND;
H. Peter Anvina26433d2008-07-16 14:40:01 -07003046 }
3047
H. Peter Anvine2c80182005-01-15 22:15:51 +00003048 case PP_ROTATE:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003049 if (defining != NULL) return NO_DIRECTIVE_FOUND;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003050 if (tline->next && tline->next->type == TOK_WHITESPACE)
3051 tline = tline->next;
H. Peter Anvin89cee572009-07-15 09:16:54 -04003052 if (!tline->next) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00003053 free_tlist(origline);
3054 error(ERR_NONFATAL, "`%%rotate' missing rotate count");
3055 return DIRECTIVE_FOUND;
3056 }
3057 t = expand_smacro(tline->next);
3058 tline->next = NULL;
3059 free_tlist(origline);
3060 tline = t;
3061 tptr = &t;
3062 tokval.t_type = TOKEN_INVALID;
3063 evalresult =
3064 evaluate(ppscan, tptr, &tokval, NULL, pass, error, NULL);
3065 free_tlist(tline);
3066 if (!evalresult)
3067 return DIRECTIVE_FOUND;
3068 if (tokval.t_type)
H. Peter Anvin917a3492008-09-24 09:14:49 -07003069 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +00003070 "trailing garbage after expression ignored");
3071 if (!is_simple(evalresult)) {
3072 error(ERR_NONFATAL, "non-constant value given to `%%rotate'");
3073 return DIRECTIVE_FOUND;
3074 }
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003075 for (ei = istk->expansion; ei != NULL; ei = ei->prev) {
3076 if (ei->type == EXP_MMACRO) {
3077 break;
3078 }
3079 }
3080 if (ei == NULL) {
3081 error(ERR_NONFATAL, "`%%rotate' invoked outside a macro call");
3082 } else if (ei->nparam == 0) {
3083 error(ERR_NONFATAL,
3084 "`%%rotate' invoked within macro without parameters");
3085 } else {
3086 int rotate = ei->rotate + reloc_value(evalresult);
3087
3088 rotate %= (int)ei->nparam;
3089 if (rotate < 0)
3090 rotate += ei->nparam;
3091 ei->rotate = rotate;
3092 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00003093 return DIRECTIVE_FOUND;
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00003094
H. Peter Anvine2c80182005-01-15 22:15:51 +00003095 case PP_REP:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003096 if (defining != NULL) {
3097 if (defining->type == EXP_REP) {
3098 defining->def_depth ++;
3099 }
3100 return NO_DIRECTIVE_FOUND;
3101 }
H. Peter Anvin6867acc2007-10-10 14:58:45 -07003102 nolist = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003103 do {
3104 tline = tline->next;
3105 } while (tok_type_(tline, TOK_WHITESPACE));
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00003106
H. Peter Anvine2c80182005-01-15 22:15:51 +00003107 if (tok_type_(tline, TOK_ID) &&
3108 nasm_stricmp(tline->text, ".nolist") == 0) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07003109 nolist = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003110 do {
3111 tline = tline->next;
3112 } while (tok_type_(tline, TOK_WHITESPACE));
3113 }
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00003114
H. Peter Anvine2c80182005-01-15 22:15:51 +00003115 if (tline) {
3116 t = expand_smacro(tline);
3117 tptr = &t;
3118 tokval.t_type = TOKEN_INVALID;
3119 evalresult =
3120 evaluate(ppscan, tptr, &tokval, NULL, pass, error, NULL);
3121 if (!evalresult) {
3122 free_tlist(origline);
3123 return DIRECTIVE_FOUND;
3124 }
3125 if (tokval.t_type)
H. Peter Anvin917a3492008-09-24 09:14:49 -07003126 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +00003127 "trailing garbage after expression ignored");
3128 if (!is_simple(evalresult)) {
3129 error(ERR_NONFATAL, "non-constant value given to `%%rep'");
3130 return DIRECTIVE_FOUND;
3131 }
Cyrill Gorcunove091d6e2010-08-09 13:58:22 +04003132 count = reloc_value(evalresult);
3133 if (count >= REP_LIMIT) {
Cyrill Gorcunov71f4f842010-08-09 20:17:17 +04003134 error(ERR_NONFATAL, "`%%rep' value exceeds limit");
Cyrill Gorcunove091d6e2010-08-09 13:58:22 +04003135 count = 0;
3136 } else
3137 count++;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003138 } else {
3139 error(ERR_NONFATAL, "`%%rep' expects a repeat count");
H. Peter Anvinf8ba53e2007-10-11 10:11:57 -07003140 count = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003141 }
3142 free_tlist(origline);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003143 ed = new_ExpDef(EXP_REP);
3144 ed->nolist = nolist;
3145 ed->def_depth = 0;
3146 ed->cur_depth = 1;
3147 ed->max_depth = (count - 1);
3148 ed->ignoring = false;
3149 ed->prev = defining;
3150 defining = ed;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003151 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003152
H. Peter Anvine2c80182005-01-15 22:15:51 +00003153 case PP_ENDREP:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003154 if (defining != NULL) {
3155 if (defining->type == EXP_REP) {
3156 if (defining->def_depth > 0) {
3157 defining->def_depth --;
3158 return NO_DIRECTIVE_FOUND;
3159 }
3160 } else {
3161 return NO_DIRECTIVE_FOUND;
3162 }
3163 }
Keith Kaniosb307a4f2010-11-06 17:41:51 -05003164 if ((defining == NULL) || (defining->type != EXP_REP)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00003165 error(ERR_NONFATAL, "`%%endrep': no matching `%%rep'");
3166 return DIRECTIVE_FOUND;
3167 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003168
H. Peter Anvine2c80182005-01-15 22:15:51 +00003169 /*
3170 * Now we have a "macro" defined - although it has no name
3171 * and we won't be entering it in the hash tables - we must
3172 * push a macro-end marker for it on to istk->expansion.
3173 * After that, it will take care of propagating itself (a
3174 * macro-end marker line for a macro which is really a %rep
3175 * block will cause the macro to be re-expanded, complete
3176 * with another macro-end marker to ensure the process
3177 * continues) until the whole expansion is forcibly removed
3178 * from istk->expansion by a %exitrep.
3179 */
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003180 ed = defining;
3181 defining = ed->prev;
3182 ed->prev = expansions;
3183 expansions = ed;
3184 ei = new_ExpInv(EXP_REP, ed);
3185 ei->current = ed->line;
3186 ei->emitting = ((ed->max_depth > 0) ? true : false);
3187 list->uplevel(ed->nolist ? LIST_MACRO_NOLIST : LIST_MACRO);
3188 ei->prev = istk->expansion;
3189 istk->expansion = ei;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003190 free_tlist(origline);
3191 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003192
H. Peter Anvine2c80182005-01-15 22:15:51 +00003193 case PP_EXITREP:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003194 if (defining != NULL) return NO_DIRECTIVE_FOUND;
3195 /*
3196 * We must search along istk->expansion until we hit a
3197 * rep invocation. Then we disable the emitting state(s)
3198 * between exitrep and endrep.
3199 */
3200 for (ei = istk->expansion; ei != NULL; ei = ei->prev) {
3201 if (ei->type == EXP_REP) {
3202 break;
3203 }
3204 }
3205
3206 if (ei != NULL) {
3207 /*
3208 * Set all invocations leading back to the rep
3209 * invocation to a non-emitting state.
3210 */
3211 for (eei = istk->expansion; eei != ei; eei = eei->prev) {
3212 eei->emitting = false;
3213 }
3214 eei->emitting = false;
3215 eei->current = NULL;
3216 eei->def->cur_depth = eei->def->max_depth;
3217 } else {
3218 error(ERR_NONFATAL, "`%%exitrep' not within `%%rep' block");
3219 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00003220 free_tlist(origline);
3221 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003222
H. Peter Anvine2c80182005-01-15 22:15:51 +00003223 case PP_XDEFINE:
3224 case PP_IXDEFINE:
3225 case PP_DEFINE:
3226 case PP_IDEFINE:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003227 if (defining != NULL) return NO_DIRECTIVE_FOUND;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003228 casesense = (i == PP_DEFINE || i == PP_XDEFINE);
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07003229
H. Peter Anvine2c80182005-01-15 22:15:51 +00003230 tline = tline->next;
3231 skip_white_(tline);
3232 tline = expand_id(tline);
3233 if (!tline || (tline->type != TOK_ID &&
3234 (tline->type != TOK_PREPROC_ID ||
3235 tline->text[1] != '$'))) {
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07003236 error(ERR_NONFATAL, "`%s' expects a macro identifier",
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003237 pp_directives[i]);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003238 free_tlist(origline);
3239 return DIRECTIVE_FOUND;
3240 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003241
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08003242 ctx = get_ctx(tline->text, &mname, false);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003243 last = tline;
3244 param_start = tline = tline->next;
3245 nparam = 0;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003246
H. Peter Anvine2c80182005-01-15 22:15:51 +00003247 /* Expand the macro definition now for %xdefine and %ixdefine */
3248 if ((i == PP_XDEFINE) || (i == PP_IXDEFINE))
3249 tline = expand_smacro(tline);
H. Peter Anvin734b1882002-04-30 21:01:08 +00003250
H. Peter Anvine2c80182005-01-15 22:15:51 +00003251 if (tok_is_(tline, "(")) {
3252 /*
3253 * This macro has parameters.
3254 */
H. Peter Anvin734b1882002-04-30 21:01:08 +00003255
H. Peter Anvine2c80182005-01-15 22:15:51 +00003256 tline = tline->next;
3257 while (1) {
3258 skip_white_(tline);
3259 if (!tline) {
3260 error(ERR_NONFATAL, "parameter identifier expected");
3261 free_tlist(origline);
3262 return DIRECTIVE_FOUND;
3263 }
3264 if (tline->type != TOK_ID) {
3265 error(ERR_NONFATAL,
3266 "`%s': parameter identifier expected",
3267 tline->text);
3268 free_tlist(origline);
3269 return DIRECTIVE_FOUND;
3270 }
3271 tline->type = TOK_SMAC_PARAM + nparam++;
3272 tline = tline->next;
3273 skip_white_(tline);
3274 if (tok_is_(tline, ",")) {
3275 tline = tline->next;
H. Peter Anvinca348b62008-07-23 10:49:26 -04003276 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003277 if (!tok_is_(tline, ")")) {
3278 error(ERR_NONFATAL,
3279 "`)' expected to terminate macro template");
3280 free_tlist(origline);
3281 return DIRECTIVE_FOUND;
3282 }
3283 break;
3284 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00003285 }
3286 last = tline;
3287 tline = tline->next;
3288 }
3289 if (tok_type_(tline, TOK_WHITESPACE))
3290 last = tline, tline = tline->next;
3291 macro_start = NULL;
3292 last->next = NULL;
3293 t = tline;
3294 while (t) {
3295 if (t->type == TOK_ID) {
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04003296 list_for_each(tt, param_start)
H. Peter Anvine2c80182005-01-15 22:15:51 +00003297 if (tt->type >= TOK_SMAC_PARAM &&
3298 !strcmp(tt->text, t->text))
3299 t->type = tt->type;
3300 }
3301 tt = t->next;
3302 t->next = macro_start;
3303 macro_start = t;
3304 t = tt;
3305 }
3306 /*
3307 * Good. We now have a macro name, a parameter count, and a
3308 * token list (in reverse order) for an expansion. We ought
3309 * to be OK just to create an SMacro, store it, and let
3310 * free_tlist have the rest of the line (which we have
3311 * carefully re-terminated after chopping off the expansion
3312 * from the end).
3313 */
H. Peter Anvin4db5a162007-10-11 13:42:09 -07003314 define_smacro(ctx, mname, casesense, nparam, macro_start);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003315 free_tlist(origline);
3316 return DIRECTIVE_FOUND;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003317
H. Peter Anvine2c80182005-01-15 22:15:51 +00003318 case PP_UNDEF:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003319 if (defining != NULL) return NO_DIRECTIVE_FOUND;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003320 tline = tline->next;
3321 skip_white_(tline);
3322 tline = expand_id(tline);
3323 if (!tline || (tline->type != TOK_ID &&
3324 (tline->type != TOK_PREPROC_ID ||
3325 tline->text[1] != '$'))) {
3326 error(ERR_NONFATAL, "`%%undef' expects a macro identifier");
3327 free_tlist(origline);
3328 return DIRECTIVE_FOUND;
3329 }
3330 if (tline->next) {
H. Peter Anvin917a3492008-09-24 09:14:49 -07003331 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +00003332 "trailing garbage after macro name ignored");
3333 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003334
H. Peter Anvine2c80182005-01-15 22:15:51 +00003335 /* Find the context that symbol belongs to */
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08003336 ctx = get_ctx(tline->text, &mname, false);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003337 undef_smacro(ctx, mname);
3338 free_tlist(origline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003339 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003340
H. Peter Anvin9e200162008-06-04 17:23:14 -07003341 case PP_DEFSTR:
3342 case PP_IDEFSTR:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003343 if (defining != NULL) return NO_DIRECTIVE_FOUND;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003344 casesense = (i == PP_DEFSTR);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003345
3346 tline = tline->next;
3347 skip_white_(tline);
3348 tline = expand_id(tline);
3349 if (!tline || (tline->type != TOK_ID &&
3350 (tline->type != TOK_PREPROC_ID ||
3351 tline->text[1] != '$'))) {
3352 error(ERR_NONFATAL, "`%s' expects a macro identifier",
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003353 pp_directives[i]);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003354 free_tlist(origline);
3355 return DIRECTIVE_FOUND;
3356 }
3357
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08003358 ctx = get_ctx(tline->text, &mname, false);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003359 last = tline;
3360 tline = expand_smacro(tline->next);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003361 last->next = NULL;
H. Peter Anvin9e200162008-06-04 17:23:14 -07003362
3363 while (tok_type_(tline, TOK_WHITESPACE))
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003364 tline = delete_Token(tline);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003365
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003366 p = detoken(tline, false);
Cyrill Gorcunov6b271292011-02-28 08:45:52 +03003367 macro_start = nasm_zalloc(sizeof(*macro_start));
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003368 macro_start->text = nasm_quote(p, strlen(p));
3369 macro_start->type = TOK_STRING;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003370 nasm_free(p);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003371
3372 /*
3373 * We now have a macro name, an implicit parameter count of
3374 * zero, and a string token to use as an expansion. Create
3375 * and store an SMacro.
3376 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003377 define_smacro(ctx, mname, casesense, 0, macro_start);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003378 free_tlist(origline);
3379 return DIRECTIVE_FOUND;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003380
H. Peter Anvin2f55bda2009-07-14 15:04:04 -04003381 case PP_DEFTOK:
3382 case PP_IDEFTOK:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003383 if (defining != NULL) return NO_DIRECTIVE_FOUND;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003384 casesense = (i == PP_DEFTOK);
3385
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003386 tline = tline->next;
3387 skip_white_(tline);
3388 tline = expand_id(tline);
3389 if (!tline || (tline->type != TOK_ID &&
3390 (tline->type != TOK_PREPROC_ID ||
3391 tline->text[1] != '$'))) {
3392 error(ERR_NONFATAL,
3393 "`%s' expects a macro identifier as first parameter",
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003394 pp_directives[i]);
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003395 free_tlist(origline);
3396 return DIRECTIVE_FOUND;
3397 }
3398 ctx = get_ctx(tline->text, &mname, false);
3399 last = tline;
3400 tline = expand_smacro(tline->next);
3401 last->next = NULL;
3402
3403 t = tline;
3404 while (tok_type_(t, TOK_WHITESPACE))
3405 t = t->next;
3406 /* t should now point to the string */
Cyrill Gorcunov6908e582010-09-06 19:36:15 +04003407 if (!tok_type_(t, TOK_STRING)) {
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003408 error(ERR_NONFATAL,
3409 "`%s` requires string as second parameter",
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003410 pp_directives[i]);
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003411 free_tlist(tline);
3412 free_tlist(origline);
3413 return DIRECTIVE_FOUND;
3414 }
3415
Keith Kaniosb307a4f2010-11-06 17:41:51 -05003416 /*
3417 * Convert the string to a token stream. Note that smacros
3418 * are stored with the token stream reversed, so we have to
3419 * reverse the output of tokenize().
3420 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003421 nasm_unquote_cstr(t->text, i);
H. Peter Anvinb40992c2010-09-15 08:57:21 -07003422 macro_start = reverse_tokens(tokenize(t->text));
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003423
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003424 /*
3425 * We now have a macro name, an implicit parameter count of
3426 * zero, and a numeric token to use as an expansion. Create
3427 * and store an SMacro.
3428 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003429 define_smacro(ctx, mname, casesense, 0, macro_start);
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003430 free_tlist(tline);
3431 free_tlist(origline);
3432 return DIRECTIVE_FOUND;
H. Peter Anvin9e200162008-06-04 17:23:14 -07003433
H. Peter Anvin418ca702008-05-30 10:42:30 -07003434 case PP_PATHSEARCH:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003435 if (defining != NULL) return NO_DIRECTIVE_FOUND;
H. Peter Anvin418ca702008-05-30 10:42:30 -07003436 {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003437 FILE *fp;
3438 StrList *xsl = NULL;
3439 StrList **xst = &xsl;
H. Peter Anvin418ca702008-05-30 10:42:30 -07003440
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003441 casesense = true;
H. Peter Anvin418ca702008-05-30 10:42:30 -07003442
3443 tline = tline->next;
3444 skip_white_(tline);
3445 tline = expand_id(tline);
3446 if (!tline || (tline->type != TOK_ID &&
3447 (tline->type != TOK_PREPROC_ID ||
3448 tline->text[1] != '$'))) {
3449 error(ERR_NONFATAL,
3450 "`%%pathsearch' expects a macro identifier as first parameter");
3451 free_tlist(origline);
3452 return DIRECTIVE_FOUND;
3453 }
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08003454 ctx = get_ctx(tline->text, &mname, false);
H. Peter Anvin418ca702008-05-30 10:42:30 -07003455 last = tline;
3456 tline = expand_smacro(tline->next);
3457 last->next = NULL;
3458
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003459 t = tline;
H. Peter Anvin418ca702008-05-30 10:42:30 -07003460 while (tok_type_(t, TOK_WHITESPACE))
3461 t = t->next;
3462
3463 if (!t || (t->type != TOK_STRING &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003464 t->type != TOK_INTERNAL_STRING)) {
H. Peter Anvin418ca702008-05-30 10:42:30 -07003465 error(ERR_NONFATAL, "`%%pathsearch' expects a file name");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003466 free_tlist(tline);
H. Peter Anvin418ca702008-05-30 10:42:30 -07003467 free_tlist(origline);
3468 return DIRECTIVE_FOUND; /* but we did _something_ */
3469 }
3470 if (t->next)
H. Peter Anvin917a3492008-09-24 09:14:49 -07003471 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvin418ca702008-05-30 10:42:30 -07003472 "trailing garbage after `%%pathsearch' ignored");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003473 p = t->text;
H. Peter Anvin427cc912008-06-01 21:43:03 -07003474 if (t->type != TOK_INTERNAL_STRING)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003475 nasm_unquote(p, NULL);
H. Peter Anvin418ca702008-05-30 10:42:30 -07003476
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003477 fp = inc_fopen(p, &xsl, &xst, true);
3478 if (fp) {
3479 p = xsl->str;
3480 fclose(fp); /* Don't actually care about the file */
3481 }
Cyrill Gorcunov6b271292011-02-28 08:45:52 +03003482 macro_start = nasm_zalloc(sizeof(*macro_start));
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003483 macro_start->text = nasm_quote(p, strlen(p));
3484 macro_start->type = TOK_STRING;
Cyrill Gorcunovb6c6ca92011-06-28 01:33:02 +04003485 nasm_free(xsl);
H. Peter Anvin418ca702008-05-30 10:42:30 -07003486
3487 /*
3488 * We now have a macro name, an implicit parameter count of
3489 * zero, and a string token to use as an expansion. Create
3490 * and store an SMacro.
3491 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003492 define_smacro(ctx, mname, casesense, 0, macro_start);
3493 free_tlist(tline);
H. Peter Anvin418ca702008-05-30 10:42:30 -07003494 free_tlist(origline);
3495 return DIRECTIVE_FOUND;
3496 }
3497
H. Peter Anvine2c80182005-01-15 22:15:51 +00003498 case PP_STRLEN:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003499 if (defining != NULL) return NO_DIRECTIVE_FOUND;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003500 casesense = true;
H. Peter Anvin70653092007-10-19 14:42:29 -07003501
H. Peter Anvine2c80182005-01-15 22:15:51 +00003502 tline = tline->next;
3503 skip_white_(tline);
3504 tline = expand_id(tline);
3505 if (!tline || (tline->type != TOK_ID &&
3506 (tline->type != TOK_PREPROC_ID ||
3507 tline->text[1] != '$'))) {
3508 error(ERR_NONFATAL,
3509 "`%%strlen' expects a macro identifier as first parameter");
3510 free_tlist(origline);
3511 return DIRECTIVE_FOUND;
3512 }
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08003513 ctx = get_ctx(tline->text, &mname, false);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003514 last = tline;
3515 tline = expand_smacro(tline->next);
3516 last->next = NULL;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003517
H. Peter Anvine2c80182005-01-15 22:15:51 +00003518 t = tline;
3519 while (tok_type_(t, TOK_WHITESPACE))
3520 t = t->next;
3521 /* t should now point to the string */
Cyrill Gorcunov4e1d5ab2010-07-23 18:51:51 +04003522 if (!tok_type_(t, TOK_STRING)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00003523 error(ERR_NONFATAL,
3524 "`%%strlen` requires string as second parameter");
3525 free_tlist(tline);
3526 free_tlist(origline);
3527 return DIRECTIVE_FOUND;
3528 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003529
Cyrill Gorcunov6b271292011-02-28 08:45:52 +03003530 macro_start = nasm_zalloc(sizeof(*macro_start));
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07003531 make_tok_num(macro_start, nasm_unquote(t->text, NULL));
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003532
H. Peter Anvine2c80182005-01-15 22:15:51 +00003533 /*
3534 * We now have a macro name, an implicit parameter count of
3535 * zero, and a numeric token to use as an expansion. Create
3536 * and store an SMacro.
3537 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003538 define_smacro(ctx, mname, casesense, 0, macro_start);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003539 free_tlist(tline);
3540 free_tlist(origline);
3541 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003542
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003543 case PP_STRCAT:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003544 if (defining != NULL) return NO_DIRECTIVE_FOUND;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003545 casesense = true;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003546
3547 tline = tline->next;
3548 skip_white_(tline);
3549 tline = expand_id(tline);
3550 if (!tline || (tline->type != TOK_ID &&
3551 (tline->type != TOK_PREPROC_ID ||
3552 tline->text[1] != '$'))) {
3553 error(ERR_NONFATAL,
3554 "`%%strcat' expects a macro identifier as first parameter");
3555 free_tlist(origline);
3556 return DIRECTIVE_FOUND;
3557 }
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08003558 ctx = get_ctx(tline->text, &mname, false);
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003559 last = tline;
3560 tline = expand_smacro(tline->next);
3561 last->next = NULL;
3562
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003563 len = 0;
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04003564 list_for_each(t, tline) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003565 switch (t->type) {
3566 case TOK_WHITESPACE:
3567 break;
3568 case TOK_STRING:
3569 len += t->a.len = nasm_unquote(t->text, NULL);
3570 break;
3571 case TOK_OTHER:
3572 if (!strcmp(t->text, ",")) /* permit comma separators */
3573 break;
3574 /* else fall through */
3575 default:
3576 error(ERR_NONFATAL,
3577 "non-string passed to `%%strcat' (%d)", t->type);
3578 free_tlist(tline);
3579 free_tlist(origline);
3580 return DIRECTIVE_FOUND;
3581 }
3582 }
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003583
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003584 p = pp = nasm_malloc(len);
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04003585 list_for_each(t, tline) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003586 if (t->type == TOK_STRING) {
3587 memcpy(p, t->text, t->a.len);
3588 p += t->a.len;
3589 }
3590 }
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003591
3592 /*
3593 * We now have a macro name, an implicit parameter count of
3594 * zero, and a numeric token to use as an expansion. Create
3595 * and store an SMacro.
3596 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003597 macro_start = new_Token(NULL, TOK_STRING, NULL, 0);
3598 macro_start->text = nasm_quote(pp, len);
3599 nasm_free(pp);
3600 define_smacro(ctx, mname, casesense, 0, macro_start);
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003601 free_tlist(tline);
3602 free_tlist(origline);
3603 return DIRECTIVE_FOUND;
3604
H. Peter Anvine2c80182005-01-15 22:15:51 +00003605 case PP_SUBSTR:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003606 if (defining != NULL) return NO_DIRECTIVE_FOUND;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003607 {
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003608 int64_t start, count;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003609 size_t len;
H. Peter Anvind2456592008-06-19 15:04:18 -07003610
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003611 casesense = true;
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07003612
H. Peter Anvine2c80182005-01-15 22:15:51 +00003613 tline = tline->next;
3614 skip_white_(tline);
3615 tline = expand_id(tline);
3616 if (!tline || (tline->type != TOK_ID &&
3617 (tline->type != TOK_PREPROC_ID ||
3618 tline->text[1] != '$'))) {
3619 error(ERR_NONFATAL,
3620 "`%%substr' expects a macro identifier as first parameter");
3621 free_tlist(origline);
3622 return DIRECTIVE_FOUND;
3623 }
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08003624 ctx = get_ctx(tline->text, &mname, false);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003625 last = tline;
3626 tline = expand_smacro(tline->next);
3627 last->next = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003628
Cyrill Gorcunov35519d62010-09-06 23:49:52 +04003629 if (tline) /* skip expanded id */
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003630 t = tline->next;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003631 while (tok_type_(t, TOK_WHITESPACE))
3632 t = t->next;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003633
H. Peter Anvine2c80182005-01-15 22:15:51 +00003634 /* t should now point to the string */
Cyrill Gorcunov35519d62010-09-06 23:49:52 +04003635 if (!tok_type_(t, TOK_STRING)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00003636 error(ERR_NONFATAL,
3637 "`%%substr` requires string as second parameter");
3638 free_tlist(tline);
3639 free_tlist(origline);
3640 return DIRECTIVE_FOUND;
3641 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003642
H. Peter Anvine2c80182005-01-15 22:15:51 +00003643 tt = t->next;
3644 tptr = &tt;
3645 tokval.t_type = TOKEN_INVALID;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003646 evalresult = evaluate(ppscan, tptr, &tokval, NULL,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003647 pass, error, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003648 if (!evalresult) {
3649 free_tlist(tline);
3650 free_tlist(origline);
3651 return DIRECTIVE_FOUND;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003652 } else if (!is_simple(evalresult)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00003653 error(ERR_NONFATAL, "non-constant value given to `%%substr`");
3654 free_tlist(tline);
3655 free_tlist(origline);
3656 return DIRECTIVE_FOUND;
3657 }
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003658 start = evalresult->value - 1;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003659
3660 while (tok_type_(tt, TOK_WHITESPACE))
3661 tt = tt->next;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003662 if (!tt) {
Keith Kaniosb307a4f2010-11-06 17:41:51 -05003663 count = 1; /* Backwards compatibility: one character */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003664 } else {
3665 tokval.t_type = TOKEN_INVALID;
3666 evalresult = evaluate(ppscan, tptr, &tokval, NULL,
3667 pass, error, NULL);
3668 if (!evalresult) {
3669 free_tlist(tline);
3670 free_tlist(origline);
3671 return DIRECTIVE_FOUND;
3672 } else if (!is_simple(evalresult)) {
3673 error(ERR_NONFATAL, "non-constant value given to `%%substr`");
3674 free_tlist(tline);
3675 free_tlist(origline);
3676 return DIRECTIVE_FOUND;
3677 }
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003678 count = evalresult->value;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003679 }
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003680
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003681 len = nasm_unquote(t->text, NULL);
Cyrill Gorcunovcff031e2010-09-07 20:31:11 +04003682 /* make start and count being in range */
3683 if (start < 0)
3684 start = 0;
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003685 if (count < 0)
3686 count = len + count + 1 - start;
3687 if (start + count > (int64_t)len)
Cyrill Gorcunovcff031e2010-09-07 20:31:11 +04003688 count = len - start;
3689 if (!len || count < 0 || start >=(int64_t)len)
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003690 start = -1, count = 0; /* empty string */
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003691
Cyrill Gorcunov6b271292011-02-28 08:45:52 +03003692 macro_start = nasm_zalloc(sizeof(*macro_start));
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003693 macro_start->text = nasm_quote((start < 0) ? "" : t->text + start, count);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003694 macro_start->type = TOK_STRING;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003695
H. Peter Anvine2c80182005-01-15 22:15:51 +00003696 /*
3697 * We now have a macro name, an implicit parameter count of
3698 * zero, and a numeric token to use as an expansion. Create
3699 * and store an SMacro.
3700 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003701 define_smacro(ctx, mname, casesense, 0, macro_start);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003702 free_tlist(tline);
3703 free_tlist(origline);
3704 return DIRECTIVE_FOUND;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003705 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003706
H. Peter Anvine2c80182005-01-15 22:15:51 +00003707 case PP_ASSIGN:
3708 case PP_IASSIGN:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003709 if (defining != NULL) return NO_DIRECTIVE_FOUND;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003710 casesense = (i == PP_ASSIGN);
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07003711
H. Peter Anvine2c80182005-01-15 22:15:51 +00003712 tline = tline->next;
3713 skip_white_(tline);
3714 tline = expand_id(tline);
3715 if (!tline || (tline->type != TOK_ID &&
3716 (tline->type != TOK_PREPROC_ID ||
3717 tline->text[1] != '$'))) {
3718 error(ERR_NONFATAL,
3719 "`%%%sassign' expects a macro identifier",
3720 (i == PP_IASSIGN ? "i" : ""));
3721 free_tlist(origline);
3722 return DIRECTIVE_FOUND;
3723 }
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08003724 ctx = get_ctx(tline->text, &mname, false);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003725 last = tline;
3726 tline = expand_smacro(tline->next);
3727 last->next = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003728
H. Peter Anvine2c80182005-01-15 22:15:51 +00003729 t = tline;
3730 tptr = &t;
3731 tokval.t_type = TOKEN_INVALID;
3732 evalresult =
3733 evaluate(ppscan, tptr, &tokval, NULL, pass, error, NULL);
3734 free_tlist(tline);
3735 if (!evalresult) {
3736 free_tlist(origline);
3737 return DIRECTIVE_FOUND;
3738 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003739
H. Peter Anvine2c80182005-01-15 22:15:51 +00003740 if (tokval.t_type)
H. Peter Anvin917a3492008-09-24 09:14:49 -07003741 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +00003742 "trailing garbage after expression ignored");
H. Peter Anvin734b1882002-04-30 21:01:08 +00003743
H. Peter Anvine2c80182005-01-15 22:15:51 +00003744 if (!is_simple(evalresult)) {
3745 error(ERR_NONFATAL,
3746 "non-constant value given to `%%%sassign'",
3747 (i == PP_IASSIGN ? "i" : ""));
3748 free_tlist(origline);
3749 return DIRECTIVE_FOUND;
3750 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003751
Cyrill Gorcunov6b271292011-02-28 08:45:52 +03003752 macro_start = nasm_zalloc(sizeof(*macro_start));
H. Peter Anvine2c80182005-01-15 22:15:51 +00003753 make_tok_num(macro_start, reloc_value(evalresult));
H. Peter Anvin734b1882002-04-30 21:01:08 +00003754
H. Peter Anvine2c80182005-01-15 22:15:51 +00003755 /*
3756 * We now have a macro name, an implicit parameter count of
3757 * zero, and a numeric token to use as an expansion. Create
3758 * and store an SMacro.
3759 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003760 define_smacro(ctx, mname, casesense, 0, macro_start);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003761 free_tlist(origline);
3762 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003763
H. Peter Anvine2c80182005-01-15 22:15:51 +00003764 case PP_LINE:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003765 if (defining != NULL) return NO_DIRECTIVE_FOUND;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003766 /*
3767 * Syntax is `%line nnn[+mmm] [filename]'
3768 */
3769 tline = tline->next;
3770 skip_white_(tline);
3771 if (!tok_type_(tline, TOK_NUMBER)) {
3772 error(ERR_NONFATAL, "`%%line' expects line number");
3773 free_tlist(origline);
3774 return DIRECTIVE_FOUND;
3775 }
H. Peter Anvin70055962007-10-11 00:05:31 -07003776 k = readnum(tline->text, &err);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003777 m = 1;
3778 tline = tline->next;
3779 if (tok_is_(tline, "+")) {
3780 tline = tline->next;
3781 if (!tok_type_(tline, TOK_NUMBER)) {
3782 error(ERR_NONFATAL, "`%%line' expects line increment");
3783 free_tlist(origline);
3784 return DIRECTIVE_FOUND;
3785 }
H. Peter Anvin70055962007-10-11 00:05:31 -07003786 m = readnum(tline->text, &err);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003787 tline = tline->next;
3788 }
3789 skip_white_(tline);
3790 src_set_linnum(k);
3791 istk->lineinc = m;
3792 if (tline) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07003793 nasm_free(src_set_fname(detoken(tline, false)));
H. Peter Anvine2c80182005-01-15 22:15:51 +00003794 }
3795 free_tlist(origline);
3796 return DIRECTIVE_FOUND;
Keith Kaniosb307a4f2010-11-06 17:41:51 -05003797
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003798 case PP_WHILE:
3799 if (defining != NULL) {
3800 if (defining->type == EXP_WHILE) {
3801 defining->def_depth ++;
3802 }
3803 return NO_DIRECTIVE_FOUND;
3804 }
3805 l = NULL;
3806 if ((istk->expansion != NULL) &&
3807 (istk->expansion->emitting == false)) {
3808 j = COND_NEVER;
3809 } else {
3810 l = new_Line();
3811 l->first = copy_Token(tline->next);
3812 j = if_condition(tline->next, i);
3813 tline->next = NULL; /* it got freed */
3814 j = (((j < 0) ? COND_NEVER : j) ? COND_IF_TRUE : COND_IF_FALSE);
3815 }
3816 ed = new_ExpDef(EXP_WHILE);
3817 ed->state = j;
3818 ed->cur_depth = 1;
3819 ed->max_depth = DEADMAN_LIMIT;
3820 ed->ignoring = ((ed->state == COND_IF_TRUE) ? false : true);
3821 if (ed->ignoring == false) {
3822 ed->line = l;
3823 ed->last = l;
3824 } else if (l != NULL) {
3825 delete_Token(l->first);
3826 nasm_free(l);
3827 l = NULL;
3828 }
3829 ed->prev = defining;
3830 defining = ed;
3831 free_tlist(origline);
3832 return DIRECTIVE_FOUND;
3833
3834 case PP_ENDWHILE:
3835 if (defining != NULL) {
3836 if (defining->type == EXP_WHILE) {
3837 if (defining->def_depth > 0) {
3838 defining->def_depth --;
3839 return NO_DIRECTIVE_FOUND;
3840 }
3841 } else {
3842 return NO_DIRECTIVE_FOUND;
3843 }
3844 }
3845 if (tline->next != NULL) {
3846 error_precond(ERR_WARNING|ERR_PASS1,
3847 "trailing garbage after `%%endwhile' ignored");
3848 }
3849 if ((defining == NULL) || (defining->type != EXP_WHILE)) {
3850 error(ERR_NONFATAL, "`%%endwhile': no matching `%%while'");
3851 return DIRECTIVE_FOUND;
3852 }
3853 ed = defining;
3854 defining = ed->prev;
3855 if (ed->ignoring == false) {
3856 ed->prev = expansions;
3857 expansions = ed;
3858 ei = new_ExpInv(EXP_WHILE, ed);
3859 ei->current = ed->line->next;
3860 ei->emitting = true;
3861 ei->prev = istk->expansion;
3862 istk->expansion = ei;
3863 } else {
3864 nasm_free(ed);
3865 }
3866 free_tlist(origline);
3867 return DIRECTIVE_FOUND;
3868
3869 case PP_EXITWHILE:
3870 if (defining != NULL) return NO_DIRECTIVE_FOUND;
3871 /*
3872 * We must search along istk->expansion until we hit a
3873 * while invocation. Then we disable the emitting state(s)
3874 * between exitwhile and endwhile.
3875 */
3876 for (ei = istk->expansion; ei != NULL; ei = ei->prev) {
3877 if (ei->type == EXP_WHILE) {
3878 break;
3879 }
3880 }
3881
3882 if (ei != NULL) {
3883 /*
3884 * Set all invocations leading back to the while
3885 * invocation to a non-emitting state.
3886 */
3887 for (eei = istk->expansion; eei != ei; eei = eei->prev) {
3888 eei->emitting = false;
3889 }
3890 eei->emitting = false;
3891 eei->current = NULL;
3892 eei->def->cur_depth = eei->def->max_depth;
3893 } else {
3894 error(ERR_NONFATAL, "`%%exitwhile' not within `%%while' block");
3895 }
3896 free_tlist(origline);
3897 return DIRECTIVE_FOUND;
3898
3899 case PP_COMMENT:
3900 if (defining != NULL) {
3901 if (defining->type == EXP_COMMENT) {
3902 defining->def_depth ++;
3903 }
3904 return NO_DIRECTIVE_FOUND;
3905 }
3906 ed = new_ExpDef(EXP_COMMENT);
3907 ed->ignoring = true;
3908 ed->prev = defining;
3909 defining = ed;
3910 free_tlist(origline);
3911 return DIRECTIVE_FOUND;
3912
3913 case PP_ENDCOMMENT:
3914 if (defining != NULL) {
3915 if (defining->type == EXP_COMMENT) {
3916 if (defining->def_depth > 0) {
3917 defining->def_depth --;
3918 return NO_DIRECTIVE_FOUND;
3919 }
3920 } else {
3921 return NO_DIRECTIVE_FOUND;
3922 }
3923 }
3924 if ((defining == NULL) || (defining->type != EXP_COMMENT)) {
3925 error(ERR_NONFATAL, "`%%endcomment': no matching `%%comment'");
3926 return DIRECTIVE_FOUND;
3927 }
3928 ed = defining;
3929 defining = ed->prev;
3930 nasm_free(ed);
3931 free_tlist(origline);
3932 return DIRECTIVE_FOUND;
3933
3934 case PP_FINAL:
3935 if (defining != NULL) return NO_DIRECTIVE_FOUND;
3936 if (in_final != false) {
3937 error(ERR_FATAL, "`%%final' cannot be used recursively");
3938 }
3939 tline = tline->next;
3940 skip_white_(tline);
3941 if (tline == NULL) {
3942 error(ERR_NONFATAL, "`%%final' expects at least one parameter");
3943 } else {
3944 l = new_Line();
3945 l->first = copy_Token(tline);
3946 l->next = finals;
3947 finals = l;
3948 }
3949 free_tlist(origline);
3950 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003951
H. Peter Anvine2c80182005-01-15 22:15:51 +00003952 default:
3953 error(ERR_FATAL,
3954 "preprocessor directive `%s' not yet implemented",
H. Peter Anvin4169a472007-09-12 01:29:43 +00003955 pp_directives[i]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003956 return DIRECTIVE_FOUND;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003957 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003958}
3959
3960/*
H. Peter Anvin76690a12002-04-30 20:52:49 +00003961 * Ensure that a macro parameter contains a condition code and
3962 * nothing else. Return the condition code index if so, or -1
3963 * otherwise.
3964 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003965static int find_cc(Token * t)
H. Peter Anvineba20a72002-04-30 20:53:55 +00003966{
H. Peter Anvin76690a12002-04-30 20:52:49 +00003967 Token *tt;
3968 int i, j, k, m;
3969
H. Peter Anvin25a99342007-09-22 17:45:45 -07003970 if (!t)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003971 return -1; /* Probably a %+ without a space */
H. Peter Anvin25a99342007-09-22 17:45:45 -07003972
H. Peter Anvineba20a72002-04-30 20:53:55 +00003973 skip_white_(t);
H. Peter Anvin76690a12002-04-30 20:52:49 +00003974 if (t->type != TOK_ID)
H. Peter Anvine2c80182005-01-15 22:15:51 +00003975 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003976 tt = t->next;
H. Peter Anvineba20a72002-04-30 20:53:55 +00003977 skip_white_(tt);
H. Peter Anvin76690a12002-04-30 20:52:49 +00003978 if (tt && (tt->type != TOK_OTHER || strcmp(tt->text, ",")))
H. Peter Anvine2c80182005-01-15 22:15:51 +00003979 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003980
3981 i = -1;
Cyrill Gorcunova7319242010-06-03 22:04:36 +04003982 j = ARRAY_SIZE(conditions);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003983 while (j - i > 1) {
3984 k = (j + i) / 2;
3985 m = nasm_stricmp(t->text, conditions[k]);
3986 if (m == 0) {
3987 i = k;
3988 j = -2;
3989 break;
3990 } else if (m < 0) {
3991 j = k;
3992 } else
3993 i = k;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003994 }
3995 if (j != -2)
H. Peter Anvine2c80182005-01-15 22:15:51 +00003996 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003997 return i;
3998}
3999
Cyrill Gorcunov575d4282010-10-06 00:25:55 +04004000static bool paste_tokens(Token **head, const struct tokseq_match *m,
4001 int mnum, bool handle_paste_tokens)
H. Peter Anvind784a082009-04-20 14:01:18 -07004002{
4003 Token **tail, *t, *tt;
4004 Token **paste_head;
4005 bool did_paste = false;
4006 char *tmp;
Cyrill Gorcunov575d4282010-10-06 00:25:55 +04004007 int i;
H. Peter Anvind784a082009-04-20 14:01:18 -07004008
4009 /* Now handle token pasting... */
4010 paste_head = NULL;
4011 tail = head;
4012 while ((t = *tail) && (tt = t->next)) {
4013 switch (t->type) {
4014 case TOK_WHITESPACE:
4015 if (tt->type == TOK_WHITESPACE) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004016 /* Zap adjacent whitespace tokens */
H. Peter Anvind784a082009-04-20 14:01:18 -07004017 t->next = delete_Token(tt);
4018 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004019 /* Do not advance paste_head here */
4020 tail = &t->next;
4021 }
H. Peter Anvind784a082009-04-20 14:01:18 -07004022 break;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004023 case TOK_PASTE: /* %+ */
4024 if (handle_paste_tokens) {
4025 /* Zap %+ and whitespace tokens to the right */
4026 while (t && (t->type == TOK_WHITESPACE ||
4027 t->type == TOK_PASTE))
4028 t = *tail = delete_Token(t);
4029 if (!paste_head || !t)
4030 break; /* Nothing to paste with */
4031 tail = paste_head;
4032 t = *tail;
4033 tt = t->next;
4034 while (tok_type_(tt, TOK_WHITESPACE))
4035 tt = t->next = delete_Token(tt);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004036 if (tt) {
4037 tmp = nasm_strcat(t->text, tt->text);
4038 delete_Token(t);
4039 tt = delete_Token(tt);
4040 t = *tail = tokenize(tmp);
4041 nasm_free(tmp);
4042 while (t->next) {
4043 tail = &t->next;
4044 t = t->next;
4045 }
4046 t->next = tt; /* Attach the remaining token chain */
4047 did_paste = true;
4048 }
4049 paste_head = tail;
4050 tail = &t->next;
4051 break;
4052 }
4053 /* else fall through */
4054 default:
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +04004055 /*
4056 * Concatenation of tokens might look nontrivial
4057 * but in real it's pretty simple -- the caller
Cyrill Gorcunov575d4282010-10-06 00:25:55 +04004058 * prepares the masks of token types to be concatenated
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +04004059 * and we simply find matched sequences and slip
4060 * them together
4061 */
Cyrill Gorcunov575d4282010-10-06 00:25:55 +04004062 for (i = 0; i < mnum; i++) {
4063 if (PP_CONCAT_MASK(t->type) & m[i].mask_head) {
4064 size_t len = 0;
4065 char *tmp, *p;
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +04004066
Cyrill Gorcunov575d4282010-10-06 00:25:55 +04004067 while (tt && (PP_CONCAT_MASK(tt->type) & m[i].mask_tail)) {
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004068 len += strlen(tt->text);
4069 tt = tt->next;
Cyrill Gorcunov575d4282010-10-06 00:25:55 +04004070 }
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +04004071
Cyrill Gorcunovfdd0ac52011-06-27 01:22:27 +04004072 nasm_dump_token(tt);
4073
Cyrill Gorcunov575d4282010-10-06 00:25:55 +04004074 /*
4075 * Now tt points to the first token after
4076 * the potential paste area...
4077 */
4078 if (tt != t->next) {
4079 /* We have at least two tokens... */
4080 len += strlen(t->text);
4081 p = tmp = nasm_malloc(len+1);
4082 while (t != tt) {
4083 strcpy(p, t->text);
4084 p = strchr(p, '\0');
4085 t = delete_Token(t);
4086 }
4087 t = *tail = tokenize(tmp);
4088 nasm_free(tmp);
4089 while (t->next) {
4090 tail = &t->next;
4091 t = t->next;
4092 }
4093 t->next = tt; /* Attach the remaining token chain */
4094 did_paste = true;
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +04004095 }
Cyrill Gorcunov575d4282010-10-06 00:25:55 +04004096 paste_head = tail;
4097 tail = &t->next;
4098 break;
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +04004099 }
Cyrill Gorcunov575d4282010-10-06 00:25:55 +04004100 }
4101 if (i >= mnum) { /* no match */
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +04004102 tail = &t->next;
4103 if (!tok_type_(t->next, TOK_WHITESPACE))
4104 paste_head = tail;
4105 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004106 break;
H. Peter Anvind784a082009-04-20 14:01:18 -07004107 }
4108 }
4109 return did_paste;
4110}
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004111
4112/*
4113 * expands to a list of tokens from %{x:y}
4114 */
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004115static Token *expand_mmac_params_range(ExpInv *ei, Token *tline, Token ***last)
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004116{
4117 Token *t = tline, **tt, *tm, *head;
4118 char *pos;
4119 int fst, lst, j, i;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004120
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004121 pos = strchr(tline->text, ':');
4122 nasm_assert(pos);
4123
4124 lst = atoi(pos + 1);
4125 fst = atoi(tline->text + 1);
4126
4127 /*
4128 * only macros params are accounted so
4129 * if someone passes %0 -- we reject such
4130 * value(s)
4131 */
4132 if (lst == 0 || fst == 0)
4133 goto err;
4134
4135 /* the values should be sane */
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004136 if ((fst > (int)ei->nparam || fst < (-(int)ei->nparam)) ||
4137 (lst > (int)ei->nparam || lst < (-(int)ei->nparam)))
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004138 goto err;
4139
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004140 fst = fst < 0 ? fst + (int)ei->nparam + 1: fst;
4141 lst = lst < 0 ? lst + (int)ei->nparam + 1: lst;
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004142
4143 /* counted from zero */
4144 fst--, lst--;
4145
4146 /*
4147 * it will be at least one token
4148 */
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004149 tm = ei->params[(fst + ei->rotate) % ei->nparam];
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004150 t = new_Token(NULL, tm->type, tm->text, 0);
4151 head = t, tt = &t->next;
4152 if (fst < lst) {
4153 for (i = fst + 1; i <= lst; i++) {
4154 t = new_Token(NULL, TOK_OTHER, ",", 0);
4155 *tt = t, tt = &t->next;
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004156 j = (i + ei->rotate) % ei->nparam;
4157 tm = ei->params[j];
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004158 t = new_Token(NULL, tm->type, tm->text, 0);
4159 *tt = t, tt = &t->next;
4160 }
4161 } else {
4162 for (i = fst - 1; i >= lst; i--) {
4163 t = new_Token(NULL, TOK_OTHER, ",", 0);
4164 *tt = t, tt = &t->next;
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004165 j = (i + ei->rotate) % ei->nparam;
4166 tm = ei->params[j];
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004167 t = new_Token(NULL, tm->type, tm->text, 0);
4168 *tt = t, tt = &t->next;
4169 }
4170 }
4171
4172 *last = tt;
4173 return head;
4174
4175err:
4176 error(ERR_NONFATAL, "`%%{%s}': macro parameters out of range",
4177 &tline->text[1]);
4178 return tline;
4179}
4180
H. Peter Anvin76690a12002-04-30 20:52:49 +00004181/*
4182 * Expand MMacro-local things: parameter references (%0, %n, %+n,
H. Peter Anvin67c63722008-10-26 23:49:00 -07004183 * %-n) and MMacro-local identifiers (%%foo) as well as
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004184 * macro indirection (%[...]) and range (%{..:..}).
H. Peter Anvin76690a12002-04-30 20:52:49 +00004185 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004186static Token *expand_mmac_params(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004187{
H. Peter Anvin734b1882002-04-30 21:01:08 +00004188 Token *t, *tt, **tail, *thead;
H. Peter Anvin6125b622009-04-08 14:02:25 -07004189 bool changed = false;
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004190 char *pos;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004191
4192 tail = &thead;
4193 thead = NULL;
4194
Cyrill Gorcunov2e046002011-06-26 23:33:56 +04004195 nasm_dump_stream(tline);
4196
H. Peter Anvine2c80182005-01-15 22:15:51 +00004197 while (tline) {
4198 if (tline->type == TOK_PREPROC_ID &&
Cyrill Gorcunovca611192010-06-04 09:22:12 +04004199 (((tline->text[1] == '+' || tline->text[1] == '-') && tline->text[2]) ||
4200 (tline->text[1] >= '0' && tline->text[1] <= '9') ||
4201 tline->text[1] == '%')) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004202 char *text = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004203 int type = 0, cc; /* type = 0 to placate optimisers */
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004204 char tmpbuf[30];
H. Peter Anvin25a99342007-09-22 17:45:45 -07004205 unsigned int n;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004206 int i;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004207 ExpInv *ei;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004208
H. Peter Anvine2c80182005-01-15 22:15:51 +00004209 t = tline;
4210 tline = tline->next;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004211
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004212 for (ei = istk->expansion; ei != NULL; ei = ei->prev) {
4213 if (ei->type == EXP_MMACRO) {
4214 break;
4215 }
4216 }
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004217 if (ei == NULL) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00004218 error(ERR_NONFATAL, "`%s': not in a macro call", t->text);
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004219 } else {
4220 pos = strchr(t->text, ':');
4221 if (!pos) {
4222 switch (t->text[1]) {
4223 /*
4224 * We have to make a substitution of one of the
4225 * forms %1, %-1, %+1, %%foo, %0.
4226 */
4227 case '0':
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004228 if ((strlen(t->text) > 2) && (t->text[2] == '0')) {
4229 type = TOK_ID;
4230 text = nasm_strdup(ei->label_text);
4231 } else {
4232 type = TOK_NUMBER;
4233 snprintf(tmpbuf, sizeof(tmpbuf), "%d", ei->nparam);
4234 text = nasm_strdup(tmpbuf);
4235 }
4236 break;
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004237 case '%':
H. Peter Anvine2c80182005-01-15 22:15:51 +00004238 type = TOK_ID;
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004239 snprintf(tmpbuf, sizeof(tmpbuf), "..@%"PRIu64".",
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004240 ei->unique);
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004241 text = nasm_strcat(tmpbuf, t->text + 2);
4242 break;
4243 case '-':
4244 n = atoi(t->text + 2) - 1;
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004245 if (n >= ei->nparam)
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004246 tt = NULL;
4247 else {
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004248 if (ei->nparam > 1)
4249 n = (n + ei->rotate) % ei->nparam;
4250 tt = ei->params[n];
H. Peter Anvine2c80182005-01-15 22:15:51 +00004251 }
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004252 cc = find_cc(tt);
4253 if (cc == -1) {
4254 error(ERR_NONFATAL,
4255 "macro parameter %d is not a condition code",
4256 n + 1);
4257 text = NULL;
4258 } else {
4259 type = TOK_ID;
4260 if (inverse_ccs[cc] == -1) {
4261 error(ERR_NONFATAL,
4262 "condition code `%s' is not invertible",
4263 conditions[cc]);
4264 text = NULL;
4265 } else
4266 text = nasm_strdup(conditions[inverse_ccs[cc]]);
4267 }
4268 break;
4269 case '+':
4270 n = atoi(t->text + 2) - 1;
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004271 if (n >= ei->nparam)
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004272 tt = NULL;
4273 else {
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004274 if (ei->nparam > 1)
4275 n = (n + ei->rotate) % ei->nparam;
4276 tt = ei->params[n];
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004277 }
4278 cc = find_cc(tt);
4279 if (cc == -1) {
4280 error(ERR_NONFATAL,
4281 "macro parameter %d is not a condition code",
4282 n + 1);
4283 text = NULL;
4284 } else {
4285 type = TOK_ID;
4286 text = nasm_strdup(conditions[cc]);
4287 }
4288 break;
4289 default:
4290 n = atoi(t->text + 1) - 1;
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004291 if (n >= ei->nparam)
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004292 tt = NULL;
4293 else {
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004294 if (ei->nparam > 1)
4295 n = (n + ei->rotate) % ei->nparam;
4296 tt = ei->params[n];
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004297 }
4298 if (tt) {
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004299 for (i = 0; i < ei->paramlen[n]; i++) {
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004300 *tail = new_Token(NULL, tt->type, tt->text, 0);
4301 tail = &(*tail)->next;
4302 tt = tt->next;
4303 }
4304 }
4305 text = NULL; /* we've done it here */
4306 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004307 }
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004308 } else {
4309 /*
4310 * seems we have a parameters range here
4311 */
4312 Token *head, **last;
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004313 head = expand_mmac_params_range(ei, t, &last);
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004314 if (head != t) {
4315 *tail = head;
4316 *last = tline;
4317 tline = head;
4318 text = NULL;
4319 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00004320 }
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004321 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00004322 if (!text) {
4323 delete_Token(t);
4324 } else {
4325 *tail = t;
4326 tail = &t->next;
4327 t->type = type;
4328 nasm_free(t->text);
4329 t->text = text;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004330 t->a.mac = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004331 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004332 changed = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004333 continue;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004334 } else if (tline->type == TOK_INDIRECT) {
4335 t = tline;
4336 tline = tline->next;
4337 tt = tokenize(t->text);
4338 tt = expand_mmac_params(tt);
4339 tt = expand_smacro(tt);
4340 *tail = tt;
4341 while (tt) {
4342 tt->a.mac = NULL; /* Necessary? */
4343 tail = &tt->next;
4344 tt = tt->next;
4345 }
4346 delete_Token(t);
4347 changed = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004348 } else {
4349 t = *tail = tline;
4350 tline = tline->next;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004351 t->a.mac = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004352 tail = &t->next;
4353 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00004354 }
H. Peter Anvineba20a72002-04-30 20:53:55 +00004355 *tail = NULL;
H. Peter Anvin67c63722008-10-26 23:49:00 -07004356
Cyrill Gorcunovc6a742c2011-06-27 01:23:09 +04004357 if (changed) {
4358 const struct tokseq_match t[] = {
4359 {
4360 PP_CONCAT_MASK(TOK_ID) |
4361 PP_CONCAT_MASK(TOK_FLOAT), /* head */
4362 PP_CONCAT_MASK(TOK_ID) |
4363 PP_CONCAT_MASK(TOK_NUMBER) |
4364 PP_CONCAT_MASK(TOK_FLOAT) |
4365 PP_CONCAT_MASK(TOK_OTHER) /* tail */
4366 },
4367 {
4368 PP_CONCAT_MASK(TOK_NUMBER), /* head */
4369 PP_CONCAT_MASK(TOK_NUMBER) /* tail */
4370 }
4371 };
4372 paste_tokens(&thead, t, ARRAY_SIZE(t), false);
4373 }
H. Peter Anvin6125b622009-04-08 14:02:25 -07004374
Cyrill Gorcunov2e046002011-06-26 23:33:56 +04004375 nasm_dump_token(thead);
4376
H. Peter Anvin76690a12002-04-30 20:52:49 +00004377 return thead;
4378}
4379
4380/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004381 * Expand all single-line macro calls made in the given line.
4382 * Return the expanded version of the line. The original is deemed
4383 * to be destroyed in the process. (In reality we'll just move
4384 * Tokens from input to output a lot of the time, rather than
4385 * actually bothering to destroy and replicate.)
4386 */
H. Peter Anvincb1cf592007-11-19 12:26:50 -08004387
H. Peter Anvine2c80182005-01-15 22:15:51 +00004388static Token *expand_smacro(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004389{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004390 Token *t, *tt, *mstart, **tail, *thead;
H. Peter Anvineba20a72002-04-30 20:53:55 +00004391 SMacro *head = NULL, *m;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004392 Token **params;
4393 int *paramsize;
H. Peter Anvin25a99342007-09-22 17:45:45 -07004394 unsigned int nparam, sparam;
H. Peter Anvind784a082009-04-20 14:01:18 -07004395 int brackets;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004396 Token *org_tline = tline;
4397 Context *ctx;
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08004398 const char *mname;
H. Peter Anvin2a15e692007-11-19 13:14:59 -08004399 int deadman = DEADMAN_LIMIT;
H. Peter Anvin8287daf2009-07-07 16:00:58 -07004400 bool expanded;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004401
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004402 /*
4403 * Trick: we should avoid changing the start token pointer since it can
4404 * be contained in "next" field of other token. Because of this
4405 * we allocate a copy of first token and work with it; at the end of
4406 * routine we copy it back
4407 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004408 if (org_tline) {
Cyrill Gorcunoved4a8052010-04-09 15:40:35 +04004409 tline = new_Token(org_tline->next, org_tline->type,
4410 org_tline->text, 0);
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004411 tline->a.mac = org_tline->a.mac;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004412 nasm_free(org_tline->text);
4413 org_tline->text = NULL;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004414 }
4415
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004416 expanded = true; /* Always expand %+ at least once */
H. Peter Anvin8287daf2009-07-07 16:00:58 -07004417
H. Peter Anvincb1cf592007-11-19 12:26:50 -08004418again:
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004419 thead = NULL;
Cyrill Gorcunoved4a8052010-04-09 15:40:35 +04004420 tail = &thead;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004421
H. Peter Anvine2c80182005-01-15 22:15:51 +00004422 while (tline) { /* main token loop */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004423 if (!--deadman) {
4424 error(ERR_NONFATAL, "interminable macro recursion");
Cyrill Gorcunovbd38c8f2009-11-21 11:11:23 +03004425 goto err;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004426 }
H. Peter Anvincb1cf592007-11-19 12:26:50 -08004427
H. Peter Anvine2c80182005-01-15 22:15:51 +00004428 if ((mname = tline->text)) {
4429 /* if this token is a local macro, look in local context */
Cyrill Gorcunovc56d9ad2010-02-11 15:12:19 +03004430 if (tline->type == TOK_ID) {
4431 head = (SMacro *)hash_findix(&smacros, mname);
4432 } else if (tline->type == TOK_PREPROC_ID) {
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004433 ctx = get_ctx(mname, &mname, false);
Cyrill Gorcunovc56d9ad2010-02-11 15:12:19 +03004434 head = ctx ? (SMacro *)hash_findix(&ctx->localmac, mname) : NULL;
4435 } else
4436 head = NULL;
H. Peter Anvin072771e2008-05-22 13:17:51 -07004437
H. Peter Anvine2c80182005-01-15 22:15:51 +00004438 /*
4439 * We've hit an identifier. As in is_mmacro below, we first
4440 * check whether the identifier is a single-line macro at
4441 * all, then think about checking for parameters if
4442 * necessary.
4443 */
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04004444 list_for_each(m, head)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004445 if (!mstrcmp(m->name, mname, m->casesense))
4446 break;
4447 if (m) {
4448 mstart = tline;
4449 params = NULL;
4450 paramsize = NULL;
4451 if (m->nparam == 0) {
4452 /*
4453 * Simple case: the macro is parameterless. Discard the
4454 * one token that the macro call took, and push the
4455 * expansion back on the to-do stack.
4456 */
4457 if (!m->expansion) {
4458 if (!strcmp("__FILE__", m->name)) {
4459 int32_t num = 0;
4460 char *file = NULL;
4461 src_get(&num, &file);
4462 tline->text = nasm_quote(file, strlen(file));
4463 tline->type = TOK_STRING;
4464 nasm_free(file);
H. Peter Anvine2c80182005-01-15 22:15:51 +00004465 continue;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004466 }
4467 if (!strcmp("__LINE__", m->name)) {
4468 nasm_free(tline->text);
4469 make_tok_num(tline, src_get_linnum());
4470 continue;
4471 }
4472 if (!strcmp("__BITS__", m->name)) {
4473 nasm_free(tline->text);
4474 make_tok_num(tline, globalbits);
4475 continue;
4476 }
4477 tline = delete_Token(tline);
4478 continue;
4479 }
4480 } else {
4481 /*
4482 * Complicated case: at least one macro with this name
H. Peter Anvine2c80182005-01-15 22:15:51 +00004483 * exists and takes parameters. We must find the
4484 * parameters in the call, count them, find the SMacro
4485 * that corresponds to that form of the macro call, and
4486 * substitute for the parameters when we expand. What a
4487 * pain.
4488 */
4489 /*tline = tline->next;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004490 skip_white_(tline); */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004491 do {
4492 t = tline->next;
4493 while (tok_type_(t, TOK_SMAC_END)) {
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004494 t->a.mac->in_progress = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004495 t->text = NULL;
4496 t = tline->next = delete_Token(t);
4497 }
4498 tline = t;
4499 } while (tok_type_(tline, TOK_WHITESPACE));
4500 if (!tok_is_(tline, "(")) {
4501 /*
4502 * This macro wasn't called with parameters: ignore
4503 * the call. (Behaviour borrowed from gnu cpp.)
4504 */
4505 tline = mstart;
4506 m = NULL;
4507 } else {
4508 int paren = 0;
4509 int white = 0;
4510 brackets = 0;
4511 nparam = 0;
4512 sparam = PARAM_DELTA;
4513 params = nasm_malloc(sparam * sizeof(Token *));
4514 params[0] = tline->next;
4515 paramsize = nasm_malloc(sparam * sizeof(int));
4516 paramsize[0] = 0;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07004517 while (true) { /* parameter loop */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004518 /*
4519 * For some unusual expansions
4520 * which concatenates function call
4521 */
4522 t = tline->next;
4523 while (tok_type_(t, TOK_SMAC_END)) {
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004524 t->a.mac->in_progress = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004525 t->text = NULL;
4526 t = tline->next = delete_Token(t);
4527 }
4528 tline = t;
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00004529
H. Peter Anvine2c80182005-01-15 22:15:51 +00004530 if (!tline) {
4531 error(ERR_NONFATAL,
4532 "macro call expects terminating `)'");
4533 break;
4534 }
4535 if (tline->type == TOK_WHITESPACE
4536 && brackets <= 0) {
4537 if (paramsize[nparam])
4538 white++;
4539 else
4540 params[nparam] = tline->next;
4541 continue; /* parameter loop */
4542 }
4543 if (tline->type == TOK_OTHER
4544 && tline->text[1] == 0) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004545 char ch = tline->text[0];
H. Peter Anvine2c80182005-01-15 22:15:51 +00004546 if (ch == ',' && !paren && brackets <= 0) {
4547 if (++nparam >= sparam) {
4548 sparam += PARAM_DELTA;
4549 params = nasm_realloc(params,
Cyrill Gorcunoved4a8052010-04-09 15:40:35 +04004550 sparam * sizeof(Token *));
4551 paramsize = nasm_realloc(paramsize,
4552 sparam * sizeof(int));
H. Peter Anvine2c80182005-01-15 22:15:51 +00004553 }
4554 params[nparam] = tline->next;
4555 paramsize[nparam] = 0;
4556 white = 0;
4557 continue; /* parameter loop */
4558 }
4559 if (ch == '{' &&
4560 (brackets > 0 || (brackets == 0 &&
4561 !paramsize[nparam])))
4562 {
4563 if (!(brackets++)) {
4564 params[nparam] = tline->next;
4565 continue; /* parameter loop */
4566 }
4567 }
4568 if (ch == '}' && brackets > 0)
4569 if (--brackets == 0) {
4570 brackets = -1;
4571 continue; /* parameter loop */
4572 }
4573 if (ch == '(' && !brackets)
4574 paren++;
4575 if (ch == ')' && brackets <= 0)
4576 if (--paren < 0)
4577 break;
4578 }
4579 if (brackets < 0) {
4580 brackets = 0;
4581 error(ERR_NONFATAL, "braces do not "
4582 "enclose all of macro parameter");
4583 }
4584 paramsize[nparam] += white + 1;
4585 white = 0;
4586 } /* parameter loop */
4587 nparam++;
4588 while (m && (m->nparam != nparam ||
4589 mstrcmp(m->name, mname,
4590 m->casesense)))
4591 m = m->next;
4592 if (!m)
H. Peter Anvin917a3492008-09-24 09:14:49 -07004593 error(ERR_WARNING|ERR_PASS1|ERR_WARN_MNP,
H. Peter Anvine2c80182005-01-15 22:15:51 +00004594 "macro `%s' exists, "
4595 "but not taking %d parameters",
4596 mstart->text, nparam);
4597 }
4598 }
4599 if (m && m->in_progress)
4600 m = NULL;
4601 if (!m) { /* in progess or didn't find '(' or wrong nparam */
H. Peter Anvin70653092007-10-19 14:42:29 -07004602 /*
H. Peter Anvine2c80182005-01-15 22:15:51 +00004603 * Design question: should we handle !tline, which
4604 * indicates missing ')' here, or expand those
4605 * macros anyway, which requires the (t) test a few
H. Peter Anvin70653092007-10-19 14:42:29 -07004606 * lines down?
H. Peter Anvine2c80182005-01-15 22:15:51 +00004607 */
4608 nasm_free(params);
4609 nasm_free(paramsize);
4610 tline = mstart;
4611 } else {
4612 /*
4613 * Expand the macro: we are placed on the last token of the
4614 * call, so that we can easily split the call from the
4615 * following tokens. We also start by pushing an SMAC_END
4616 * token for the cycle removal.
4617 */
4618 t = tline;
4619 if (t) {
4620 tline = t->next;
4621 t->next = NULL;
4622 }
4623 tt = new_Token(tline, TOK_SMAC_END, NULL, 0);
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004624 tt->a.mac = m;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07004625 m->in_progress = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004626 tline = tt;
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04004627 list_for_each(t, m->expansion) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00004628 if (t->type >= TOK_SMAC_PARAM) {
4629 Token *pcopy = tline, **ptail = &pcopy;
4630 Token *ttt, *pt;
4631 int i;
H. Peter Anvin734b1882002-04-30 21:01:08 +00004632
H. Peter Anvine2c80182005-01-15 22:15:51 +00004633 ttt = params[t->type - TOK_SMAC_PARAM];
Cyrill Gorcunoved4a8052010-04-09 15:40:35 +04004634 i = paramsize[t->type - TOK_SMAC_PARAM];
4635 while (--i >= 0) {
4636 pt = *ptail = new_Token(tline, ttt->type,
4637 ttt->text, 0);
H. Peter Anvine2c80182005-01-15 22:15:51 +00004638 ptail = &pt->next;
4639 ttt = ttt->next;
4640 }
4641 tline = pcopy;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004642 } else if (t->type == TOK_PREPROC_Q) {
4643 tt = new_Token(tline, TOK_ID, mname, 0);
4644 tline = tt;
4645 } else if (t->type == TOK_PREPROC_QQ) {
4646 tt = new_Token(tline, TOK_ID, m->name, 0);
4647 tline = tt;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004648 } else {
4649 tt = new_Token(tline, t->type, t->text, 0);
4650 tline = tt;
4651 }
4652 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00004653
H. Peter Anvine2c80182005-01-15 22:15:51 +00004654 /*
4655 * Having done that, get rid of the macro call, and clean
4656 * up the parameters.
4657 */
4658 nasm_free(params);
4659 nasm_free(paramsize);
4660 free_tlist(mstart);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004661 expanded = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004662 continue; /* main token loop */
4663 }
4664 }
4665 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004666
H. Peter Anvine2c80182005-01-15 22:15:51 +00004667 if (tline->type == TOK_SMAC_END) {
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004668 tline->a.mac->in_progress = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004669 tline = delete_Token(tline);
4670 } else {
4671 t = *tail = tline;
4672 tline = tline->next;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004673 t->a.mac = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004674 t->next = NULL;
4675 tail = &t->next;
4676 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004677 }
4678
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004679 /*
4680 * Now scan the entire line and look for successive TOK_IDs that resulted
Keith Kaniosb7a89542007-04-12 02:40:54 +00004681 * after expansion (they can't be produced by tokenize()). The successive
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004682 * TOK_IDs should be concatenated.
4683 * Also we look for %+ tokens and concatenate the tokens before and after
4684 * them (without white spaces in between).
4685 */
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +04004686 if (expanded) {
Cyrill Gorcunovc6a742c2011-06-27 01:23:09 +04004687 const struct tokseq_match t[] = {
4688 {
4689 PP_CONCAT_MASK(TOK_ID) |
4690 PP_CONCAT_MASK(TOK_PREPROC_ID), /* head */
4691 PP_CONCAT_MASK(TOK_ID) |
4692 PP_CONCAT_MASK(TOK_PREPROC_ID) |
4693 PP_CONCAT_MASK(TOK_NUMBER) /* tail */
4694 }
4695 };
4696 if (paste_tokens(&thead, t, ARRAY_SIZE(t), true)) {
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +04004697 /*
4698 * If we concatenated something, *and* we had previously expanded
4699 * an actual macro, scan the lines again for macros...
4700 */
4701 tline = thead;
4702 expanded = false;
4703 goto again;
4704 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00004705 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004706
Cyrill Gorcunovbd38c8f2009-11-21 11:11:23 +03004707err:
H. Peter Anvine2c80182005-01-15 22:15:51 +00004708 if (org_tline) {
4709 if (thead) {
4710 *org_tline = *thead;
4711 /* since we just gave text to org_line, don't free it */
4712 thead->text = NULL;
4713 delete_Token(thead);
4714 } else {
4715 /* the expression expanded to empty line;
4716 we can't return NULL for some reasons
4717 we just set the line to a single WHITESPACE token. */
4718 memset(org_tline, 0, sizeof(*org_tline));
4719 org_tline->text = NULL;
4720 org_tline->type = TOK_WHITESPACE;
4721 }
4722 thead = org_tline;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004723 }
4724
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004725 return thead;
4726}
4727
4728/*
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004729 * Similar to expand_smacro but used exclusively with macro identifiers
4730 * right before they are fetched in. The reason is that there can be
4731 * identifiers consisting of several subparts. We consider that if there
4732 * are more than one element forming the name, user wants a expansion,
4733 * otherwise it will be left as-is. Example:
4734 *
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004735 * %define %$abc cde
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004736 *
4737 * the identifier %$abc will be left as-is so that the handler for %define
4738 * will suck it and define the corresponding value. Other case:
4739 *
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004740 * %define _%$abc cde
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004741 *
4742 * In this case user wants name to be expanded *before* %define starts
4743 * working, so we'll expand %$abc into something (if it has a value;
4744 * otherwise it will be left as-is) then concatenate all successive
4745 * PP_IDs into one.
4746 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004747static Token *expand_id(Token * tline)
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004748{
4749 Token *cur, *oldnext = NULL;
4750
H. Peter Anvin734b1882002-04-30 21:01:08 +00004751 if (!tline || !tline->next)
H. Peter Anvine2c80182005-01-15 22:15:51 +00004752 return tline;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004753
4754 cur = tline;
4755 while (cur->next &&
H. Peter Anvine2c80182005-01-15 22:15:51 +00004756 (cur->next->type == TOK_ID ||
4757 cur->next->type == TOK_PREPROC_ID
4758 || cur->next->type == TOK_NUMBER))
4759 cur = cur->next;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004760
4761 /* If identifier consists of just one token, don't expand */
4762 if (cur == tline)
H. Peter Anvine2c80182005-01-15 22:15:51 +00004763 return tline;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004764
H. Peter Anvine2c80182005-01-15 22:15:51 +00004765 if (cur) {
4766 oldnext = cur->next; /* Detach the tail past identifier */
4767 cur->next = NULL; /* so that expand_smacro stops here */
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004768 }
4769
H. Peter Anvin734b1882002-04-30 21:01:08 +00004770 tline = expand_smacro(tline);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004771
H. Peter Anvine2c80182005-01-15 22:15:51 +00004772 if (cur) {
4773 /* expand_smacro possibly changhed tline; re-scan for EOL */
4774 cur = tline;
4775 while (cur && cur->next)
4776 cur = cur->next;
4777 if (cur)
4778 cur->next = oldnext;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004779 }
4780
4781 return tline;
4782}
4783
4784/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004785 * Determine whether the given line constitutes a multi-line macro
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004786 * call, and return the ExpDef structure called if so. Doesn't have
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004787 * to check for an initial label - that's taken care of in
4788 * expand_mmacro - but must check numbers of parameters. Guaranteed
4789 * to be called with tline->type == TOK_ID, so the putative macro
4790 * name is easy to find.
4791 */
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004792static ExpDef *is_mmacro(Token * tline, Token *** params_array)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004793{
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004794 ExpDef *head, *ed;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004795 Token **params;
4796 int nparam;
4797
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004798 head = (ExpDef *) hash_findix(&expdefs, tline->text);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004799
4800 /*
4801 * Efficiency: first we see if any macro exists with the given
4802 * name. If not, we can return NULL immediately. _Then_ we
4803 * count the parameters, and then we look further along the
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004804 * list if necessary to find the proper ExpDef.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004805 */
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004806 list_for_each(ed, head)
4807 if (!mstrcmp(ed->name, tline->text, ed->casesense))
H. Peter Anvine2c80182005-01-15 22:15:51 +00004808 break;
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004809 if (!ed)
H. Peter Anvine2c80182005-01-15 22:15:51 +00004810 return NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004811
4812 /*
4813 * OK, we have a potential macro. Count and demarcate the
4814 * parameters.
4815 */
H. Peter Anvin734b1882002-04-30 21:01:08 +00004816 count_mmac_params(tline->next, &nparam, &params);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004817
4818 /*
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004819 * So we know how many parameters we've got. Find the ExpDef
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004820 * structure that handles this number.
4821 */
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004822 while (ed) {
4823 if (ed->nparam_min <= nparam
4824 && (ed->plus || nparam <= ed->nparam_max)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00004825 /*
4826 * It's right, and we can use it. Add its default
4827 * parameters to the end of our list if necessary.
4828 */
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004829 if (ed->defaults && nparam < ed->nparam_min + ed->ndefs) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00004830 params =
4831 nasm_realloc(params,
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004832 ((ed->nparam_min + ed->ndefs +
H. Peter Anvine2c80182005-01-15 22:15:51 +00004833 1) * sizeof(*params)));
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004834 while (nparam < ed->nparam_min + ed->ndefs) {
4835 params[nparam] = ed->defaults[nparam - ed->nparam_min];
H. Peter Anvine2c80182005-01-15 22:15:51 +00004836 nparam++;
4837 }
4838 }
4839 /*
4840 * If we've gone over the maximum parameter count (and
4841 * we're in Plus mode), ignore parameters beyond
4842 * nparam_max.
4843 */
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004844 if (ed->plus && nparam > ed->nparam_max)
4845 nparam = ed->nparam_max;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004846 /*
4847 * Then terminate the parameter list, and leave.
4848 */
4849 if (!params) { /* need this special case */
4850 params = nasm_malloc(sizeof(*params));
4851 nparam = 0;
4852 }
4853 params[nparam] = NULL;
4854 *params_array = params;
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004855 return ed;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004856 }
4857 /*
4858 * This one wasn't right: look for the next one with the
4859 * same name.
4860 */
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004861 list_for_each(ed, ed->next)
4862 if (!mstrcmp(ed->name, tline->text, ed->casesense))
H. Peter Anvine2c80182005-01-15 22:15:51 +00004863 break;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004864 }
4865
4866 /*
4867 * After all that, we didn't find one with the right number of
4868 * parameters. Issue a warning, and fail to expand the macro.
4869 */
H. Peter Anvin917a3492008-09-24 09:14:49 -07004870 error(ERR_WARNING|ERR_PASS1|ERR_WARN_MNP,
H. Peter Anvine2c80182005-01-15 22:15:51 +00004871 "macro `%s' exists, but not taking %d parameters",
4872 tline->text, nparam);
H. Peter Anvin734b1882002-04-30 21:01:08 +00004873 nasm_free(params);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004874 return NULL;
4875}
4876
4877/*
4878 * Expand the multi-line macro call made by the given line, if
4879 * there is one to be expanded. If there is, push the expansion on
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004880 * istk->expansion and return true. Otherwise return false.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004881 */
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004882static bool expand_mmacro(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004883{
H. Peter Anvineba20a72002-04-30 20:53:55 +00004884 Token *label = NULL;
4885 int dont_prepend = 0;
Cyrill Gorcunovfb27fc22011-06-25 12:08:30 +04004886 Token **params, *t;
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004887 Line *l = NULL;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004888 ExpDef *ed;
4889 ExpInv *ei;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004890 int i, nparam, *paramlen;
H. Peter Anvinc751e862008-06-09 10:18:45 -07004891 const char *mname;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004892
4893 t = tline;
H. Peter Anvineba20a72002-04-30 20:53:55 +00004894 skip_white_(t);
H. Peter Anvince2233b2008-05-25 21:57:00 -07004895 /* if (!tok_type_(t, TOK_ID)) Lino 02/25/02 */
H. Peter Anvindce1e2f2002-04-30 21:06:37 +00004896 if (!tok_type_(t, TOK_ID) && !tok_type_(t, TOK_PREPROC_ID))
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004897 return false;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004898 ed = is_mmacro(t, &params);
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004899 if (ed != NULL) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004900 mname = t->text;
H. Peter Anvinc751e862008-06-09 10:18:45 -07004901 } else {
H. Peter Anvine2c80182005-01-15 22:15:51 +00004902 Token *last;
4903 /*
4904 * We have an id which isn't a macro call. We'll assume
4905 * it might be a label; we'll also check to see if a
4906 * colon follows it. Then, if there's another id after
4907 * that lot, we'll check it again for macro-hood.
4908 */
4909 label = last = t;
4910 t = t->next;
4911 if (tok_type_(t, TOK_WHITESPACE))
4912 last = t, t = t->next;
4913 if (tok_is_(t, ":")) {
4914 dont_prepend = 1;
4915 last = t, t = t->next;
4916 if (tok_type_(t, TOK_WHITESPACE))
4917 last = t, t = t->next;
4918 }
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004919 if (!tok_type_(t, TOK_ID) || !(ed = is_mmacro(t, &params)))
4920 return false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004921 last->next = NULL;
Keith Kanios891775e2009-07-11 06:08:54 -05004922 mname = t->text;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004923 tline = t;
H. Peter Anvineba20a72002-04-30 20:53:55 +00004924 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004925
4926 /*
4927 * Fix up the parameters: this involves stripping leading and
4928 * trailing whitespace, then stripping braces if they are
4929 * present.
4930 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004931 for (nparam = 0; params[nparam]; nparam++) ;
H. Peter Anvin734b1882002-04-30 21:01:08 +00004932 paramlen = nparam ? nasm_malloc(nparam * sizeof(*paramlen)) : NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004933
H. Peter Anvine2c80182005-01-15 22:15:51 +00004934 for (i = 0; params[i]; i++) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07004935 int brace = false;
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004936 int comma = (!ed->plus || i < nparam - 1);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004937
H. Peter Anvine2c80182005-01-15 22:15:51 +00004938 t = params[i];
4939 skip_white_(t);
4940 if (tok_is_(t, "{"))
H. Peter Anvin6867acc2007-10-10 14:58:45 -07004941 t = t->next, brace = true, comma = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004942 params[i] = t;
4943 paramlen[i] = 0;
4944 while (t) {
4945 if (comma && t->type == TOK_OTHER && !strcmp(t->text, ","))
4946 break; /* ... because we have hit a comma */
4947 if (comma && t->type == TOK_WHITESPACE
4948 && tok_is_(t->next, ","))
4949 break; /* ... or a space then a comma */
4950 if (brace && t->type == TOK_OTHER && !strcmp(t->text, "}"))
4951 break; /* ... or a brace */
4952 t = t->next;
4953 paramlen[i]++;
4954 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004955 }
4956
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004957 if (ed->cur_depth >= ed->max_depth) {
4958 if (ed->max_depth > 1) {
4959 error(ERR_WARNING,
4960 "reached maximum macro recursion depth of %i for %s",
4961 ed->max_depth,ed->name);
4962 }
4963 return false;
4964 } else {
4965 ed->cur_depth ++;
4966 }
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004967
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004968 /*
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004969 * OK, we have found a ExpDef structure representing a
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004970 * previously defined mmacro. Create an expansion invocation
4971 * and point it back to the expansion definition. Substitution of
H. Peter Anvin76690a12002-04-30 20:52:49 +00004972 * parameter tokens and macro-local tokens doesn't get done
4973 * until the single-line macro substitution process; this is
4974 * because delaying them allows us to change the semantics
4975 * later through %rotate.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004976 */
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004977 ei = new_ExpInv(EXP_MMACRO, ed);
4978 ei->name = nasm_strdup(mname);
4979 //ei->label = label;
4980 //ei->label_text = detoken(label, false);
4981 ei->current = ed->line;
4982 ei->emitting = true;
4983 //ei->iline = tline;
4984 ei->params = params;
4985 ei->nparam = nparam;
4986 ei->rotate = 0;
4987 ei->paramlen = paramlen;
4988 ei->lineno = 0;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004989
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004990 ei->prev = istk->expansion;
4991 istk->expansion = ei;
4992
4993 /*
4994 * Special case: detect %00 on first invocation; if found,
4995 * avoid emitting any labels that precede the mmacro call.
4996 * ed->prepend is set to -1 when %00 is detected, else 1.
4997 */
4998 if (ed->prepend == 0) {
4999 for (l = ed->line; l != NULL; l = l->next) {
5000 for (t = l->first; t != NULL; t = t->next) {
5001 if ((t->type == TOK_PREPROC_ID) &&
5002 (strlen(t->text) == 3) &&
5003 (t->text[1] == '0') && (t->text[2] == '0')) {
5004 dont_prepend = -1;
5005 break;
5006 }
5007 }
5008 if (dont_prepend < 0) {
5009 break;
5010 }
5011 }
5012 ed->prepend = ((dont_prepend < 0) ? -1 : 1);
5013 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005014
5015 /*
H. Peter Anvineba20a72002-04-30 20:53:55 +00005016 * If we had a label, push it on as the first line of
5017 * the macro expansion.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005018 */
Keith Kaniosb307a4f2010-11-06 17:41:51 -05005019 if (label != NULL) {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005020 if (ed->prepend < 0) {
5021 ei->label_text = detoken(label, false);
5022 } else {
5023 if (dont_prepend == 0) {
5024 t = label;
5025 while (t->next != NULL) {
5026 t = t->next;
5027 }
5028 t->next = new_Token(NULL, TOK_OTHER, ":", 0);
5029 }
5030 l = new_Line();
5031 l->first = copy_Token(label);
5032 l->next = ei->current;
5033 ei->current = l;
5034 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005035 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005036
Keith Kaniosb307a4f2010-11-06 17:41:51 -05005037 list->uplevel(ed->nolist ? LIST_MACRO_NOLIST : LIST_MACRO);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005038
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005039 istk->mmac_depth++;
Keith Kaniosb307a4f2010-11-06 17:41:51 -05005040 return true;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005041}
5042
Victor van den Elzen3b404c02008-09-18 13:51:36 +02005043/* The function that actually does the error reporting */
5044static void verror(int severity, const char *fmt, va_list arg)
5045{
5046 char buff[1024];
5047
5048 vsnprintf(buff, sizeof(buff), fmt, arg);
5049
Cyrill Gorcunov55cc4d02010-11-10 23:12:06 +03005050 if (istk && istk->mmac_depth > 0) {
5051 ExpInv *ei = istk->expansion;
5052 int lineno = ei->lineno;
5053 while (ei) {
5054 if (ei->type == EXP_MMACRO)
5055 break;
5056 lineno += ei->relno;
5057 ei = ei->prev;
5058 }
Keith Kaniosb307a4f2010-11-06 17:41:51 -05005059 nasm_error(severity, "(%s:%d) %s", ei->def->name,
Cyrill Gorcunov55cc4d02010-11-10 23:12:06 +03005060 lineno, buff);
5061 } else
H. Peter Anvindbb640b2009-07-18 18:57:16 -07005062 nasm_error(severity, "%s", buff);
Victor van den Elzen3b404c02008-09-18 13:51:36 +02005063}
5064
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005065/*
5066 * Since preprocessor always operate only on the line that didn't
H. Peter Anvin917a3492008-09-24 09:14:49 -07005067 * arrived yet, we should always use ERR_OFFBY1.
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005068 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +00005069static void error(int severity, const char *fmt, ...)
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005070{
5071 va_list arg;
H. Peter Anvin734b1882002-04-30 21:01:08 +00005072 va_start(arg, fmt);
Victor van den Elzen3b404c02008-09-18 13:51:36 +02005073 verror(severity, fmt, arg);
H. Peter Anvin734b1882002-04-30 21:01:08 +00005074 va_end(arg);
Victor van den Elzen3b404c02008-09-18 13:51:36 +02005075}
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005076
Victor van den Elzen3b404c02008-09-18 13:51:36 +02005077/*
5078 * Because %else etc are evaluated in the state context
5079 * of the previous branch, errors might get lost with error():
5080 * %if 0 ... %else trailing garbage ... %endif
5081 * So %else etc should report errors with this function.
5082 */
5083static void error_precond(int severity, const char *fmt, ...)
5084{
5085 va_list arg;
5086
5087 /* Only ignore the error if it's really in a dead branch */
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005088 if ((istk != NULL) &&
5089 (istk->expansion != NULL) &&
5090 (istk->expansion->type == EXP_IF) &&
5091 (istk->expansion->def->state == COND_NEVER))
Victor van den Elzen3b404c02008-09-18 13:51:36 +02005092 return;
5093
5094 va_start(arg, fmt);
5095 verror(severity, fmt, arg);
5096 va_end(arg);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005097}
5098
H. Peter Anvin734b1882002-04-30 21:01:08 +00005099static void
H. Peter Anvindbb640b2009-07-18 18:57:16 -07005100pp_reset(char *file, int apass, ListGen * listgen, StrList **deplist)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005101{
H. Peter Anvin7383b402008-09-24 10:20:40 -07005102 Token *t;
5103
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005104 cstk = NULL;
Cyrill Gorcunov6b271292011-02-28 08:45:52 +03005105 istk = nasm_zalloc(sizeof(Include));
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005106 istk->fp = fopen(file, "r");
H. Peter Anvineba20a72002-04-30 20:53:55 +00005107 src_set_fname(nasm_strdup(file));
5108 src_set_linnum(0);
5109 istk->lineinc = 1;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005110 if (!istk->fp)
H. Peter Anvin917a3492008-09-24 09:14:49 -07005111 error(ERR_FATAL|ERR_NOFILE, "unable to open input file `%s'",
H. Peter Anvine2c80182005-01-15 22:15:51 +00005112 file);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005113 defining = NULL;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005114 finals = NULL;
5115 in_final = false;
Charles Crayned4200be2008-07-12 16:42:33 -07005116 nested_mac_count = 0;
5117 nested_rep_count = 0;
H. Peter Anvin97a23472007-09-16 17:57:25 -07005118 init_macros();
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005119 unique = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005120 if (tasm_compatible_mode) {
H. Peter Anvina4835d42008-05-20 14:21:29 -07005121 stdmacpos = nasm_stdmac;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005122 } else {
H. Peter Anvina4835d42008-05-20 14:21:29 -07005123 stdmacpos = nasm_stdmac_after_tasm;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005124 }
H. Peter Anvind2456592008-06-19 15:04:18 -07005125 any_extrastdmac = extrastdmac && *extrastdmac;
5126 do_predef = true;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005127 list = listgen;
H. Peter Anvin61f130f2008-09-25 15:45:06 -07005128
5129 /*
5130 * 0 for dependencies, 1 for preparatory passes, 2 for final pass.
5131 * The caller, however, will also pass in 3 for preprocess-only so
5132 * we can set __PASS__ accordingly.
5133 */
5134 pass = apass > 2 ? 2 : apass;
5135
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07005136 dephead = deptail = deplist;
5137 if (deplist) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03005138 StrList *sl = nasm_malloc(strlen(file)+1+sizeof sl->next);
5139 sl->next = NULL;
5140 strcpy(sl->str, file);
5141 *deptail = sl;
5142 deptail = &sl->next;
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07005143 }
H. Peter Anvin7383b402008-09-24 10:20:40 -07005144
H. Peter Anvin61f130f2008-09-25 15:45:06 -07005145 /*
5146 * Define the __PASS__ macro. This is defined here unlike
5147 * all the other builtins, because it is special -- it varies between
5148 * passes.
5149 */
Cyrill Gorcunov6b271292011-02-28 08:45:52 +03005150 t = nasm_zalloc(sizeof(*t));
H. Peter Anvin61f130f2008-09-25 15:45:06 -07005151 make_tok_num(t, apass);
H. Peter Anvin7383b402008-09-24 10:20:40 -07005152 define_smacro(NULL, "__PASS__", true, 0, t);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005153}
5154
Keith Kaniosa6dfa782007-04-13 16:47:53 +00005155static char *pp_getline(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005156{
Keith Kaniosa6dfa782007-04-13 16:47:53 +00005157 char *line;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005158 Token *tline;
Keith Kanios9858cec2010-11-08 00:58:02 -06005159 ExpDef *ed;
5160 ExpInv *ei;
5161 Line *l;
5162 int j;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005163
H. Peter Anvine2c80182005-01-15 22:15:51 +00005164 while (1) {
5165 /*
Keith Kaniosb307a4f2010-11-06 17:41:51 -05005166 * Fetch a tokenized line, either from the expansion
H. Peter Anvine2c80182005-01-15 22:15:51 +00005167 * buffer or from the input file.
5168 */
5169 tline = NULL;
H. Peter Anvineba20a72002-04-30 20:53:55 +00005170
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005171 while (1) { /* until we get a line we can use */
5172 /*
5173 * Fetch a tokenized line from the expansion buffer
5174 */
5175 if (istk->expansion != NULL) {
5176 ei = istk->expansion;
5177 if (ei->current != NULL) {
5178 if (ei->emitting == false) {
5179 ei->current = NULL;
5180 continue;
5181 }
5182 l = ei->current;
5183 ei->current = l->next;
5184 ei->lineno++;
5185 tline = copy_Token(l->first);
5186 if (((ei->type == EXP_REP) ||
5187 (ei->type == EXP_MMACRO) ||
5188 (ei->type == EXP_WHILE))
5189 && (ei->def->nolist == false)) {
5190 char *p = detoken(tline, false);
5191 list->line(LIST_MACRO, p);
5192 nasm_free(p);
5193 }
5194 if (ei->linnum > -1) {
5195 src_set_linnum(src_get_linnum() + 1);
5196 }
5197 break;
5198 } else if ((ei->type == EXP_REP) &&
5199 (ei->def->cur_depth < ei->def->max_depth)) {
5200 ei->def->cur_depth ++;
5201 ei->current = ei->def->line;
5202 ei->lineno = 0;
5203 continue;
5204 } else if ((ei->type == EXP_WHILE) &&
5205 (ei->def->cur_depth < ei->def->max_depth)) {
5206 ei->current = ei->def->line;
5207 ei->lineno = 0;
5208 tline = copy_Token(ei->current->first);
5209 j = if_condition(tline, PP_WHILE);
5210 tline = NULL;
5211 j = (((j < 0) ? COND_NEVER : j) ? COND_IF_TRUE : COND_IF_FALSE);
5212 if (j == COND_IF_TRUE) {
5213 ei->current = ei->current->next;
5214 ei->def->cur_depth ++;
5215 } else {
5216 ei->emitting = false;
5217 ei->current = NULL;
5218 ei->def->cur_depth = ei->def->max_depth;
5219 }
5220 continue;
5221 } else {
5222 istk->expansion = ei->prev;
5223 ed = ei->def;
5224 if (ed != NULL) {
5225 if ((ei->emitting == true) &&
5226 (ed->max_depth == DEADMAN_LIMIT) &&
5227 (ed->cur_depth == DEADMAN_LIMIT)
5228 ) {
5229 error(ERR_FATAL, "runaway expansion detected, aborting");
5230 }
5231 if (ed->cur_depth > 0) {
5232 ed->cur_depth --;
Keith Kanios94124652010-12-18 11:47:28 -06005233 } else if (ed->type != EXP_MMACRO) {
Keith Kanios6a7c3e92010-12-18 11:49:53 -06005234 expansions = ed->prev;
5235 free_expdef(ed);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005236 }
5237 if ((ei->type == EXP_REP) ||
5238 (ei->type == EXP_MMACRO) ||
5239 (ei->type == EXP_WHILE)) {
5240 list->downlevel(LIST_MACRO);
5241 if (ei->type == EXP_MMACRO) {
5242 istk->mmac_depth--;
5243 }
5244 }
5245 }
5246 if (ei->linnum > -1) {
5247 src_set_linnum(ei->linnum);
5248 }
5249 free_expinv(ei);
5250 continue;
5251 }
5252 }
5253
5254 /*
5255 * Read in line from input and tokenize
5256 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00005257 line = read_line();
5258 if (line) { /* from the current input file */
5259 line = prepreproc(line);
Keith Kaniosb7a89542007-04-12 02:40:54 +00005260 tline = tokenize(line);
H. Peter Anvine2c80182005-01-15 22:15:51 +00005261 nasm_free(line);
5262 break;
5263 }
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005264
H. Peter Anvine2c80182005-01-15 22:15:51 +00005265 /*
5266 * The current file has ended; work down the istk
5267 */
5268 {
5269 Include *i = istk;
5270 fclose(i->fp);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005271 if (i->expansion != NULL) {
5272 error(ERR_FATAL,
5273 "end of file while still in an expansion");
5274 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00005275 /* only set line and file name if there's a next node */
5276 if (i->next) {
5277 src_set_linnum(i->lineno);
Cyrill Gorcunovd34a1082011-03-07 11:23:08 +03005278 nasm_free(src_set_fname(nasm_strdup(i->fname)));
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03005279 }
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005280 if ((i->next == NULL) && (finals != NULL)) {
5281 in_final = true;
5282 ei = new_ExpInv(EXP_FINAL, NULL);
5283 ei->emitting = true;
5284 ei->current = finals;
5285 istk->expansion = ei;
5286 finals = NULL;
5287 continue;
5288 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00005289 istk = i->next;
5290 list->downlevel(LIST_INCLUDE);
5291 nasm_free(i);
Keith Kaniosb307a4f2010-11-06 17:41:51 -05005292 if (istk == NULL) {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005293 if (finals != NULL) {
5294 in_final = true;
5295 } else {
5296 return NULL;
5297 }
5298 }
5299 continue;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005300 }
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005301 }
5302
5303 if (defining == NULL) {
5304 tline = expand_mmac_params(tline);
5305 }
5306
H. Peter Anvine2c80182005-01-15 22:15:51 +00005307 /*
5308 * Check the line to see if it's a preprocessor directive.
5309 */
5310 if (do_directive(tline) == DIRECTIVE_FOUND) {
5311 continue;
Keith Kaniosb307a4f2010-11-06 17:41:51 -05005312 } else if (defining != NULL) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00005313 /*
Keith Kaniosb307a4f2010-11-06 17:41:51 -05005314 * We're defining an expansion. We emit nothing at all,
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005315 * and just shove the tokenized line on to the definition.
H. Peter Anvine2c80182005-01-15 22:15:51 +00005316 */
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005317 if (defining->ignoring == false) {
5318 Line *l = new_Line();
5319 l->first = tline;
5320 if (defining->line == NULL) {
5321 defining->line = l;
5322 defining->last = l;
5323 } else {
5324 defining->last->next = l;
5325 defining->last = l;
5326 }
5327 } else {
Keith Kanios104803d2010-12-18 11:05:46 -06005328 free_tlist(tline);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005329 }
5330 defining->linecount++;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005331 continue;
Keith Kaniosb307a4f2010-11-06 17:41:51 -05005332 } else if ((istk->expansion != NULL) &&
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005333 (istk->expansion->emitting != true)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00005334 /*
Keith Kaniosb307a4f2010-11-06 17:41:51 -05005335 * We're in a non-emitting branch of an expansion.
H. Peter Anvine2c80182005-01-15 22:15:51 +00005336 * Emit nothing at all, not even a blank line: when we
Keith Kaniosb307a4f2010-11-06 17:41:51 -05005337 * emerge from the expansion we'll give a line-number
H. Peter Anvine2c80182005-01-15 22:15:51 +00005338 * directive so we keep our place correctly.
5339 */
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005340 free_tlist(tline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00005341 continue;
5342 } else {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005343 tline = expand_smacro(tline);
Keith Kaniosb307a4f2010-11-06 17:41:51 -05005344 if (expand_mmacro(tline) != true) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00005345 /*
Keith Kaniosb7a89542007-04-12 02:40:54 +00005346 * De-tokenize the line again, and emit it.
H. Peter Anvine2c80182005-01-15 22:15:51 +00005347 */
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005348 line = detoken(tline, true);
5349 free_tlist(tline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00005350 break;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005351 } else {
5352 continue;
5353 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00005354 }
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005355 }
5356 return line;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005357}
5358
H. Peter Anvine2c80182005-01-15 22:15:51 +00005359static void pp_cleanup(int pass)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005360{
Keith Kaniosb307a4f2010-11-06 17:41:51 -05005361 if (defining != NULL) {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005362 error(ERR_NONFATAL, "end of file while still defining an expansion");
5363 while (defining != NULL) {
5364 ExpDef *ed = defining;
5365 defining = ed->prev;
5366 free_expdef(ed);
5367 }
5368 defining = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005369 }
Keith Kaniosb307a4f2010-11-06 17:41:51 -05005370 while (cstk != NULL)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005371 ctx_pop();
H. Peter Anvin97a23472007-09-16 17:57:25 -07005372 free_macros();
Keith Kaniosb307a4f2010-11-06 17:41:51 -05005373 while (istk != NULL) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00005374 Include *i = istk;
5375 istk = istk->next;
5376 fclose(i->fp);
5377 nasm_free(i->fname);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005378 while (i->expansion != NULL) {
5379 ExpInv *ei = i->expansion;
5380 i->expansion = ei->prev;
5381 free_expinv(ei);
5382 }
Cyrill Gorcunov8dcfd882011-03-03 09:18:56 +03005383 nasm_free(i);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005384 }
5385 while (cstk)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005386 ctx_pop();
H. Peter Anvin86877b22008-06-20 15:55:45 -07005387 nasm_free(src_set_fname(NULL));
H. Peter Anvine2c80182005-01-15 22:15:51 +00005388 if (pass == 0) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03005389 IncPath *i;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005390 free_llist(predef);
5391 delete_Blocks();
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03005392 while ((i = ipath)) {
5393 ipath = i->next;
Cyrill Gorcunovb6c6ca92011-06-28 01:33:02 +04005394 nasm_free(i->path);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03005395 nasm_free(i);
5396 }
H. Peter Anvin11dfa1a2008-07-02 18:11:04 -07005397 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005398}
5399
Keith Kaniosa6dfa782007-04-13 16:47:53 +00005400void pp_include_path(char *path)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005401{
Cyrill Gorcunov6b271292011-02-28 08:45:52 +03005402 IncPath *i = nasm_zalloc(sizeof(IncPath));
H. Peter Anvin37a321f2007-09-24 13:41:58 -07005403
Cyrill Gorcunov6b271292011-02-28 08:45:52 +03005404 if (path)
5405 i->path = nasm_strdup(path);
Frank Kotlerd0ed6fd2003-08-27 11:33:56 +00005406
H. Peter Anvin89cee572009-07-15 09:16:54 -04005407 if (ipath) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00005408 IncPath *j = ipath;
H. Peter Anvin89cee572009-07-15 09:16:54 -04005409 while (j->next)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005410 j = j->next;
5411 j->next = i;
5412 } else {
5413 ipath = i;
Frank Kotlerd0ed6fd2003-08-27 11:33:56 +00005414 }
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005415}
Frank Kotlerd0ed6fd2003-08-27 11:33:56 +00005416
Keith Kaniosa6dfa782007-04-13 16:47:53 +00005417void pp_pre_include(char *fname)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005418{
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005419 Token *inc, *space, *name;
5420 Line *l;
5421
H. Peter Anvin734b1882002-04-30 21:01:08 +00005422 name = new_Token(NULL, TOK_INTERNAL_STRING, fname, 0);
5423 space = new_Token(name, TOK_WHITESPACE, NULL, 0);
5424 inc = new_Token(space, TOK_PREPROC_ID, "%include", 0);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005425
Keith Kaniosb307a4f2010-11-06 17:41:51 -05005426 l = new_Line();
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005427 l->next = predef;
5428 l->first = inc;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005429 predef = l;
5430}
5431
Keith Kaniosa6dfa782007-04-13 16:47:53 +00005432void pp_pre_define(char *definition)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005433{
5434 Token *def, *space;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005435 Line *l;
Keith Kaniosa6dfa782007-04-13 16:47:53 +00005436 char *equals;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005437
5438 equals = strchr(definition, '=');
H. Peter Anvin734b1882002-04-30 21:01:08 +00005439 space = new_Token(NULL, TOK_WHITESPACE, NULL, 0);
5440 def = new_Token(space, TOK_PREPROC_ID, "%define", 0);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005441 if (equals)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005442 *equals = ' ';
Keith Kaniosb7a89542007-04-12 02:40:54 +00005443 space->next = tokenize(definition);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005444 if (equals)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005445 *equals = '=';
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005446
Keith Kaniosb307a4f2010-11-06 17:41:51 -05005447 l = new_Line();
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005448 l->next = predef;
5449 l->first = def;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005450 predef = l;
5451}
5452
Keith Kaniosa6dfa782007-04-13 16:47:53 +00005453void pp_pre_undefine(char *definition)
H. Peter Anvin620515a2002-04-30 20:57:38 +00005454{
5455 Token *def, *space;
5456 Line *l;
5457
H. Peter Anvin734b1882002-04-30 21:01:08 +00005458 space = new_Token(NULL, TOK_WHITESPACE, NULL, 0);
5459 def = new_Token(space, TOK_PREPROC_ID, "%undef", 0);
Keith Kaniosb7a89542007-04-12 02:40:54 +00005460 space->next = tokenize(definition);
H. Peter Anvin620515a2002-04-30 20:57:38 +00005461
Keith Kaniosb307a4f2010-11-06 17:41:51 -05005462 l = new_Line();
H. Peter Anvin620515a2002-04-30 20:57:38 +00005463 l->next = predef;
5464 l->first = def;
H. Peter Anvin620515a2002-04-30 20:57:38 +00005465 predef = l;
5466}
5467
Keith Kaniosb7a89542007-04-12 02:40:54 +00005468/*
Keith Kaniosb7a89542007-04-12 02:40:54 +00005469 * This function is used to assist with "runtime" preprocessor
Keith Kaniosb307a4f2010-11-06 17:41:51 -05005470 * directives, e.g. pp_runtime("%define __BITS__ 64");
Keith Kaniosb7a89542007-04-12 02:40:54 +00005471 *
5472 * ERRORS ARE IGNORED HERE, SO MAKE COMPLETELY SURE THAT YOU
5473 * PASS A VALID STRING TO THIS FUNCTION!!!!!
5474 */
5475
Keith Kaniosa6dfa782007-04-13 16:47:53 +00005476void pp_runtime(char *definition)
Keith Kaniosb7a89542007-04-12 02:40:54 +00005477{
5478 Token *def;
H. Peter Anvin70653092007-10-19 14:42:29 -07005479
Keith Kaniosb7a89542007-04-12 02:40:54 +00005480 def = tokenize(definition);
H. Peter Anvin89cee572009-07-15 09:16:54 -04005481 if (do_directive(def) == NO_DIRECTIVE_FOUND)
Keith Kaniosb7a89542007-04-12 02:40:54 +00005482 free_tlist(def);
H. Peter Anvin70653092007-10-19 14:42:29 -07005483
Keith Kaniosb7a89542007-04-12 02:40:54 +00005484}
5485
H. Peter Anvina70547f2008-07-19 21:44:26 -07005486void pp_extra_stdmac(macros_t *macros)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005487{
H. Peter Anvin76690a12002-04-30 20:52:49 +00005488 extrastdmac = macros;
5489}
5490
Keith Kaniosa5fc6462007-10-13 07:09:22 -07005491static void make_tok_num(Token * tok, int64_t val)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005492{
Keith Kaniosa6dfa782007-04-13 16:47:53 +00005493 char numbuf[20];
Keith Kaniosa5fc6462007-10-13 07:09:22 -07005494 snprintf(numbuf, sizeof(numbuf), "%"PRId64"", val);
H. Peter Anvineba20a72002-04-30 20:53:55 +00005495 tok->text = nasm_strdup(numbuf);
5496 tok->type = TOK_NUMBER;
5497}
5498
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005499Preproc nasmpp = {
5500 pp_reset,
5501 pp_getline,
5502 pp_cleanup
5503};