blob: 3353b287d09b5b69fde13113da7b40b8b2ba974a [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);
Cyrill Gorcunov290eac72011-06-28 01:59:05 +0400463static Context *get_ctx(const char *name, const char **namep);
Keith Kaniosa5fc6462007-10-13 07:09:22 -0700464static void make_tok_num(Token * tok, int64_t val);
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000465static void error(int severity, const char *fmt, ...);
Victor van den Elzen3b404c02008-09-18 13:51:36 +0200466static void error_precond(int severity, const char *fmt, ...);
H. Peter Anvince616072002-04-30 21:02:23 +0000467static void *new_Block(size_t size);
468static void delete_Blocks(void);
H. Peter Anvinc751e862008-06-09 10:18:45 -0700469static Token *new_Token(Token * next, enum pp_token_type type,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300470 const char *text, int txtlen);
Keith Kaniosb307a4f2010-11-06 17:41:51 -0500471static Token *copy_Token(Token * tline);
H. Peter Anvin734b1882002-04-30 21:01:08 +0000472static Token *delete_Token(Token * t);
Keith Kaniosb307a4f2010-11-06 17:41:51 -0500473static Line *new_Line(void);
474static ExpDef *new_ExpDef(int exp_type);
475static ExpInv *new_ExpInv(int exp_type, ExpDef *ed);
H. Peter Anvineba20a72002-04-30 20:53:55 +0000476
477/*
478 * Macros for safe checking of token pointers, avoid *(NULL)
479 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300480#define tok_type_(x,t) ((x) && (x)->type == (t))
481#define skip_white_(x) if (tok_type_((x), TOK_WHITESPACE)) (x)=(x)->next
482#define tok_is_(x,v) (tok_type_((x), TOK_OTHER) && !strcmp((x)->text,(v)))
483#define tok_isnt_(x,v) ((x) && ((x)->type!=TOK_OTHER || strcmp((x)->text,(v))))
H. Peter Anvin76690a12002-04-30 20:52:49 +0000484
Cyrill Gorcunov194ba892011-06-30 01:16:35 +0400485/*
486 * A few helpers for single macros
487 */
488
489/* We might be not smacro parameter at all */
490static bool is_smacro_param(Token *t)
491{
492 return t->type >= TOK_SMAC_PARAM;
493}
494
495/* smacro parameters are counted in a special way */
496static int smacro_get_param_idx(Token *t)
497{
498 return t->type - TOK_SMAC_PARAM;
499}
500
501/* encode smacro parameter index */
502static int smacro_set_param_idx(Token *t, unsigned int index)
503{
504 return t->type = TOK_SMAC_PARAM + index;
505}
506
Cyrill Gorcunov49e8f692010-11-11 15:06:12 +0300507#ifdef NASM_TRACE
508
Cyrill Gorcunovfc0c1282011-06-25 19:51:44 +0400509#define stringify(x) #x
510
Cyrill Gorcunov9d1141a2011-06-26 23:07:35 +0400511#define nasm_trace(msg, ...) printf("(%s:%d): " msg "\n", __func__, __LINE__, ##__VA_ARGS__)
Cyrill Gorcunovfc0c1282011-06-25 19:51:44 +0400512#define nasm_dump_token(t) nasm_raw_dump_token(t, __FILE__, __LINE__, __func__);
Cyrill Gorcunov2e046002011-06-26 23:33:56 +0400513#define nasm_dump_stream(t) nasm_raw_dump_stream(t, __FILE__, __LINE__, __func__);
Cyrill Gorcunovfc0c1282011-06-25 19:51:44 +0400514
515/* FIXME: we really need some compound type here instead of inplace code */
516static const char *nasm_get_tok_type_str(enum pp_token_type type)
517{
518#define SWITCH_TOK_NAME(type) \
519 case (type): \
520 return stringify(type)
521
522 switch (type) {
523 SWITCH_TOK_NAME(TOK_NONE);
524 SWITCH_TOK_NAME(TOK_WHITESPACE);
525 SWITCH_TOK_NAME(TOK_COMMENT);
526 SWITCH_TOK_NAME(TOK_ID);
527 SWITCH_TOK_NAME(TOK_PREPROC_ID);
528 SWITCH_TOK_NAME(TOK_STRING);
529 SWITCH_TOK_NAME(TOK_NUMBER);
530 SWITCH_TOK_NAME(TOK_FLOAT);
531 SWITCH_TOK_NAME(TOK_SMAC_END);
532 SWITCH_TOK_NAME(TOK_OTHER);
533 SWITCH_TOK_NAME(TOK_INTERNAL_STRING);
534 SWITCH_TOK_NAME(TOK_PREPROC_Q);
535 SWITCH_TOK_NAME(TOK_PREPROC_QQ);
536 SWITCH_TOK_NAME(TOK_PASTE);
537 SWITCH_TOK_NAME(TOK_INDIRECT);
538 SWITCH_TOK_NAME(TOK_SMAC_PARAM);
539 SWITCH_TOK_NAME(TOK_MAX);
540 }
541
542 return NULL;
543}
544
545static void nasm_raw_dump_token(Token *token, const char *file, int line, const char *func)
Cyrill Gorcunov49e8f692010-11-11 15:06:12 +0300546{
547 printf("---[%s (%s:%d): %p]---\n", func, file, line, (void *)token);
548 if (token) {
549 Token *t;
550 list_for_each(t, token) {
551 if (t->text)
Cyrill Gorcunovfc0c1282011-06-25 19:51:44 +0400552 printf("'%s'(%s) ", t->text,
553 nasm_get_tok_type_str(t->type));
Cyrill Gorcunov49e8f692010-11-11 15:06:12 +0300554 }
Cyrill Gorcunovfc0c1282011-06-25 19:51:44 +0400555 printf("\n\n");
Cyrill Gorcunov49e8f692010-11-11 15:06:12 +0300556 }
557}
558
Cyrill Gorcunov2e046002011-06-26 23:33:56 +0400559static void nasm_raw_dump_stream(Token *token, const char *file, int line, const char *func)
560{
561 printf("---[%s (%s:%d): %p]---\n", func, file, line, (void *)token);
562 if (token) {
563 Token *t;
564 list_for_each(t, token)
565 printf("%s", t->text ? t->text : " ");
566 printf("\n\n");
567 }
568}
569
Cyrill Gorcunovfc0c1282011-06-25 19:51:44 +0400570#else
571#define nasm_trace(msg, ...)
572#define nasm_dump_token(t)
Cyrill Gorcunov2e046002011-06-26 23:33:56 +0400573#define nasm_dump_stream(t)
Cyrill Gorcunov49e8f692010-11-11 15:06:12 +0300574#endif
575
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300576/*
H. Peter Anvin077fb932010-07-20 14:56:30 -0700577 * nasm_unquote with error if the string contains NUL characters.
578 * If the string contains NUL characters, issue an error and return
579 * the C len, i.e. truncate at the NUL.
580 */
581static size_t nasm_unquote_cstr(char *qstr, enum preproc_token directive)
582{
583 size_t len = nasm_unquote(qstr, NULL);
584 size_t clen = strlen(qstr);
585
586 if (len != clen)
587 error(ERR_NONFATAL, "NUL character in `%s' directive",
588 pp_directives[directive]);
589
590 return clen;
591}
592
593/*
H. Peter Anvinb40992c2010-09-15 08:57:21 -0700594 * In-place reverse a list of tokens.
595 */
596static Token *reverse_tokens(Token *t)
597{
Cyrill Gorcunov3eba69a2011-04-13 13:15:02 +0400598 Token *prev, *next;
H. Peter Anvinb40992c2010-09-15 08:57:21 -0700599
Cyrill Gorcunov3eba69a2011-04-13 13:15:02 +0400600 list_reverse(t, prev, next);
H. Peter Anvinb40992c2010-09-15 08:57:21 -0700601
Cyrill Gorcunov3eba69a2011-04-13 13:15:02 +0400602 return t;
H. Peter Anvinb40992c2010-09-15 08:57:21 -0700603}
604
605/*
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300606 * Handle TASM specific directives, which do not contain a % in
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000607 * front of them. We do it here because I could not find any other
608 * place to do it for the moment, and it is a hack (ideally it would
609 * be nice to be able to use the NASM pre-processor to do it).
610 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000611static char *check_tasm_directive(char *line)
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000612{
Keith Kaniosb7a89542007-04-12 02:40:54 +0000613 int32_t i, j, k, m, len;
Cyrill Gorcunovf66ac7d2009-10-12 20:41:13 +0400614 char *p, *q, *oldline, oldchar;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000615
Cyrill Gorcunovf66ac7d2009-10-12 20:41:13 +0400616 p = nasm_skip_spaces(line);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000617
618 /* Binary search for the directive name */
619 i = -1;
Cyrill Gorcunova7319242010-06-03 22:04:36 +0400620 j = ARRAY_SIZE(tasm_directives);
Cyrill Gorcunovf66ac7d2009-10-12 20:41:13 +0400621 q = nasm_skip_word(p);
622 len = q - p;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000623 if (len) {
624 oldchar = p[len];
625 p[len] = 0;
626 while (j - i > 1) {
627 k = (j + i) / 2;
628 m = nasm_stricmp(p, tasm_directives[k]);
629 if (m == 0) {
630 /* We have found a directive, so jam a % in front of it
631 * so that NASM will then recognise it as one if it's own.
632 */
633 p[len] = oldchar;
634 len = strlen(p);
635 oldline = line;
636 line = nasm_malloc(len + 2);
637 line[0] = '%';
638 if (k == TM_IFDIFI) {
H. Peter Anvin18f48792009-06-27 15:56:27 -0700639 /*
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300640 * NASM does not recognise IFDIFI, so we convert
641 * it to %if 0. This is not used in NASM
642 * compatible code, but does need to parse for the
643 * TASM macro package.
H. Peter Anvine2c80182005-01-15 22:15:51 +0000644 */
H. Peter Anvin18f48792009-06-27 15:56:27 -0700645 strcpy(line + 1, "if 0");
H. Peter Anvine2c80182005-01-15 22:15:51 +0000646 } else {
647 memcpy(line + 1, p, len + 1);
648 }
649 nasm_free(oldline);
650 return line;
651 } else if (m < 0) {
652 j = k;
653 } else
654 i = k;
655 }
656 p[len] = oldchar;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000657 }
658 return line;
659}
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000660
H. Peter Anvin76690a12002-04-30 20:52:49 +0000661/*
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000662 * The pre-preprocessing stage... This function translates line
663 * number indications as they emerge from GNU cpp (`# lineno "file"
664 * flags') into NASM preprocessor line number indications (`%line
665 * lineno file').
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000666 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000667static char *prepreproc(char *line)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000668{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000669 int lineno, fnlen;
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000670 char *fname, *oldline;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000671
H. Peter Anvine2c80182005-01-15 22:15:51 +0000672 if (line[0] == '#' && line[1] == ' ') {
673 oldline = line;
674 fname = oldline + 2;
675 lineno = atoi(fname);
676 fname += strspn(fname, "0123456789 ");
677 if (*fname == '"')
678 fname++;
679 fnlen = strcspn(fname, "\"");
680 line = nasm_malloc(20 + fnlen);
681 snprintf(line, 20 + fnlen, "%%line %d %.*s", lineno, fnlen, fname);
682 nasm_free(oldline);
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000683 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000684 if (tasm_compatible_mode)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000685 return check_tasm_directive(line);
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000686 return line;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000687}
688
689/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000690 * Free a linked list of tokens.
691 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000692static void free_tlist(Token * list)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000693{
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +0400694 while (list)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000695 list = delete_Token(list);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000696}
697
698/*
699 * Free a linked list of lines.
700 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000701static void free_llist(Line * list)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000702{
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +0400703 Line *l, *tmp;
704 list_for_each_safe(l, tmp, list) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000705 free_tlist(l->first);
706 nasm_free(l);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000707 }
708}
709
710/*
Keith Kaniosb307a4f2010-11-06 17:41:51 -0500711 * Free an ExpDef
H. Peter Anvineba20a72002-04-30 20:53:55 +0000712 */
Keith Kaniosb307a4f2010-11-06 17:41:51 -0500713static void free_expdef(ExpDef * ed)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000714{
Keith Kaniosb307a4f2010-11-06 17:41:51 -0500715 nasm_free(ed->name);
716 free_tlist(ed->dlist);
717 nasm_free(ed->defaults);
718 free_llist(ed->line);
719 nasm_free(ed);
720}
721
722/*
723 * Free an ExpInv
724 */
725static void free_expinv(ExpInv * ei)
726{
Cyrill Gorcunovb6c6ca92011-06-28 01:33:02 +0400727 nasm_free(ei->name);
728 nasm_free(ei->label_text);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +0300729 nasm_free(ei);
H. Peter Anvineba20a72002-04-30 20:53:55 +0000730}
731
732/*
H. Peter Anvin97a23472007-09-16 17:57:25 -0700733 * Free all currently defined macros, and free the hash tables
734 */
H. Peter Anvin072771e2008-05-22 13:17:51 -0700735static void free_smacro_table(struct hash_table *smt)
H. Peter Anvin97a23472007-09-16 17:57:25 -0700736{
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +0400737 SMacro *s, *tmp;
H. Peter Anvin072771e2008-05-22 13:17:51 -0700738 const char *key;
739 struct hash_tbl_node *it = NULL;
H. Peter Anvin97a23472007-09-16 17:57:25 -0700740
H. Peter Anvin072771e2008-05-22 13:17:51 -0700741 while ((s = hash_iterate(smt, &it, &key)) != NULL) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300742 nasm_free((void *)key);
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +0400743 list_for_each_safe(s, tmp, s) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300744 nasm_free(s->name);
745 free_tlist(s->expansion);
746 nasm_free(s);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300747 }
H. Peter Anvin97a23472007-09-16 17:57:25 -0700748 }
H. Peter Anvin072771e2008-05-22 13:17:51 -0700749 hash_free(smt);
750}
751
Keith Kaniosb307a4f2010-11-06 17:41:51 -0500752static void free_expdef_table(struct hash_table *edt)
H. Peter Anvin072771e2008-05-22 13:17:51 -0700753{
Keith Kaniosb307a4f2010-11-06 17:41:51 -0500754 ExpDef *ed, *tmp;
H. Peter Anvin072771e2008-05-22 13:17:51 -0700755 const char *key;
756 struct hash_tbl_node *it = NULL;
H. Peter Anvin97a23472007-09-16 17:57:25 -0700757
758 it = NULL;
Keith Kaniosb307a4f2010-11-06 17:41:51 -0500759 while ((ed = hash_iterate(edt, &it, &key)) != NULL) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300760 nasm_free((void *)key);
Keith Kaniosb307a4f2010-11-06 17:41:51 -0500761 list_for_each_safe(ed ,tmp, ed)
762 free_expdef(ed);
H. Peter Anvin97a23472007-09-16 17:57:25 -0700763 }
Keith Kaniosb307a4f2010-11-06 17:41:51 -0500764 hash_free(edt);
H. Peter Anvin072771e2008-05-22 13:17:51 -0700765}
766
767static void free_macros(void)
768{
H. Peter Anvin166c2472008-05-28 12:28:58 -0700769 free_smacro_table(&smacros);
Keith Kaniosb307a4f2010-11-06 17:41:51 -0500770 free_expdef_table(&expdefs);
H. Peter Anvin97a23472007-09-16 17:57:25 -0700771}
772
773/*
774 * Initialize the hash tables
775 */
776static void init_macros(void)
777{
H. Peter Anvin166c2472008-05-28 12:28:58 -0700778 hash_init(&smacros, HASH_LARGE);
Keith Kaniosb307a4f2010-11-06 17:41:51 -0500779 hash_init(&expdefs, HASH_LARGE);
H. Peter Anvin97a23472007-09-16 17:57:25 -0700780}
781
782/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000783 * Pop the context stack.
784 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000785static void ctx_pop(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000786{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000787 Context *c = cstk;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000788
789 cstk = cstk->next;
H. Peter Anvin166c2472008-05-28 12:28:58 -0700790 free_smacro_table(&c->localmac);
H. Peter Anvin734b1882002-04-30 21:01:08 +0000791 nasm_free(c->name);
792 nasm_free(c);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000793}
794
H. Peter Anvin072771e2008-05-22 13:17:51 -0700795/*
796 * Search for a key in the hash index; adding it if necessary
797 * (in which case we initialize the data pointer to NULL.)
798 */
799static void **
800hash_findi_add(struct hash_table *hash, const char *str)
801{
802 struct hash_insert hi;
803 void **r;
804 char *strx;
805
806 r = hash_findi(hash, str, &hi);
807 if (r)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300808 return r;
H. Peter Anvin072771e2008-05-22 13:17:51 -0700809
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300810 strx = nasm_strdup(str); /* Use a more efficient allocator here? */
H. Peter Anvin072771e2008-05-22 13:17:51 -0700811 return hash_add(&hi, strx, NULL);
812}
813
814/*
815 * Like hash_findi, but returns the data element rather than a pointer
816 * to it. Used only when not adding a new element, hence no third
817 * argument.
818 */
819static void *
820hash_findix(struct hash_table *hash, const char *str)
821{
822 void **p;
823
824 p = hash_findi(hash, str, NULL);
825 return p ? *p : NULL;
826}
827
Cyrill Gorcunov15bdc512010-07-13 11:27:41 +0400828/*
Keith Kaniosb307a4f2010-11-06 17:41:51 -0500829 * read line from standard macros set,
Cyrill Gorcunov15bdc512010-07-13 11:27:41 +0400830 * if there no more left -- return NULL
831 */
832static char *line_from_stdmac(void)
833{
834 unsigned char c;
835 const unsigned char *p = stdmacpos;
836 char *line, *q;
837 size_t len = 0;
838
839 if (!stdmacpos)
840 return NULL;
841
842 while ((c = *p++)) {
843 if (c >= 0x80)
844 len += pp_directives_len[c - 0x80] + 1;
845 else
846 len++;
847 }
848
849 line = nasm_malloc(len + 1);
850 q = line;
851 while ((c = *stdmacpos++)) {
852 if (c >= 0x80) {
853 memcpy(q, pp_directives[c - 0x80], pp_directives_len[c - 0x80]);
854 q += pp_directives_len[c - 0x80];
855 *q++ = ' ';
856 } else {
857 *q++ = c;
858 }
859 }
860 stdmacpos = p;
861 *q = '\0';
862
863 if (!*stdmacpos) {
864 /* This was the last of the standard macro chain... */
865 stdmacpos = NULL;
866 if (any_extrastdmac) {
867 stdmacpos = extrastdmac;
868 any_extrastdmac = false;
869 } else if (do_predef) {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +0300870 ExpInv *ei;
Cyrill Gorcunov15bdc512010-07-13 11:27:41 +0400871 Line *pd, *l;
872 Token *head, **tail, *t;
873
874 /*
875 * Nasty hack: here we push the contents of
876 * `predef' on to the top-level expansion stack,
877 * since this is the most convenient way to
878 * implement the pre-include and pre-define
879 * features.
880 */
881 list_for_each(pd, predef) {
882 head = NULL;
883 tail = &head;
884 list_for_each(t, pd->first) {
885 *tail = new_Token(NULL, t->type, t->text, 0);
886 tail = &(*tail)->next;
887 }
888
Keith Kaniosb307a4f2010-11-06 17:41:51 -0500889 l = new_Line();
890 l->first = head;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +0300891 ei = new_ExpInv(EXP_PREDEF, NULL);
892 ei->current = l;
893 ei->emitting = true;
894 ei->prev = istk->expansion;
Keith Kaniosb307a4f2010-11-06 17:41:51 -0500895 istk->expansion = ei;
Cyrill Gorcunov15bdc512010-07-13 11:27:41 +0400896 }
897 do_predef = false;
898 }
899 }
900
901 return line;
902}
903
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000904#define BUF_DELTA 512
905/*
906 * Read a line from the top file in istk, handling multiple CR/LFs
907 * at the end of the line read, and handling spurious ^Zs. Will
908 * return lines from the standard macro set if this has not already
909 * been done.
910 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000911static char *read_line(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000912{
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000913 char *buffer, *p, *q;
H. Peter Anvin9f394642002-04-30 21:07:51 +0000914 int bufsize, continued_count;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000915
Cyrill Gorcunov15bdc512010-07-13 11:27:41 +0400916 /*
917 * standart macros set (predefined) goes first
918 */
919 p = line_from_stdmac();
920 if (p)
921 return p;
H. Peter Anvin72edbb82008-06-19 16:00:04 -0700922
Cyrill Gorcunov15bdc512010-07-13 11:27:41 +0400923 /*
924 * regular read from a file
925 */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000926 bufsize = BUF_DELTA;
927 buffer = nasm_malloc(BUF_DELTA);
928 p = buffer;
H. Peter Anvin9f394642002-04-30 21:07:51 +0000929 continued_count = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000930 while (1) {
931 q = fgets(p, bufsize - (p - buffer), istk->fp);
932 if (!q)
933 break;
934 p += strlen(p);
935 if (p > buffer && p[-1] == '\n') {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300936 /*
937 * Convert backslash-CRLF line continuation sequences into
938 * nothing at all (for DOS and Windows)
939 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000940 if (((p - 2) > buffer) && (p[-3] == '\\') && (p[-2] == '\r')) {
941 p -= 3;
942 *p = 0;
943 continued_count++;
944 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300945 /*
946 * Also convert backslash-LF line continuation sequences into
947 * nothing at all (for Unix)
948 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000949 else if (((p - 1) > buffer) && (p[-2] == '\\')) {
950 p -= 2;
951 *p = 0;
952 continued_count++;
953 } else {
954 break;
955 }
956 }
957 if (p - buffer > bufsize - 10) {
Keith Kaniosb7a89542007-04-12 02:40:54 +0000958 int32_t offset = p - buffer;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000959 bufsize += BUF_DELTA;
960 buffer = nasm_realloc(buffer, bufsize);
961 p = buffer + offset; /* prevent stale-pointer problems */
962 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000963 }
964
H. Peter Anvine2c80182005-01-15 22:15:51 +0000965 if (!q && p == buffer) {
966 nasm_free(buffer);
967 return NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000968 }
969
Cyrill Gorcunova5aea572010-11-11 10:14:45 +0300970 src_set_linnum(src_get_linnum() + istk->lineinc +
971 (continued_count * istk->lineinc));
H. Peter Anvineba20a72002-04-30 20:53:55 +0000972
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000973 /*
974 * Play safe: remove CRs as well as LFs, if any of either are
975 * present at the end of the line.
976 */
H. Peter Anvineba20a72002-04-30 20:53:55 +0000977 while (--p >= buffer && (*p == '\n' || *p == '\r'))
H. Peter Anvine2c80182005-01-15 22:15:51 +0000978 *p = '\0';
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000979
980 /*
981 * Handle spurious ^Z, which may be inserted into source files
982 * by some file transfer utilities.
983 */
984 buffer[strcspn(buffer, "\032")] = '\0';
985
H. Peter Anvin734b1882002-04-30 21:01:08 +0000986 list->line(LIST_READ, buffer);
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000987
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000988 return buffer;
989}
990
991/*
Keith Kaniosb7a89542007-04-12 02:40:54 +0000992 * Tokenize a line of text. This is a very simple process since we
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000993 * don't need to parse the value out of e.g. numeric tokens: we
994 * simply split one string into many.
995 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000996static Token *tokenize(char *line)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000997{
H. Peter Anvinca544db2008-10-19 19:30:11 -0700998 char c, *p = line;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +0000999 enum pp_token_type type;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001000 Token *list = NULL;
1001 Token *t, **tail = &list;
Keith Kanios6faad4e2010-12-18 14:08:02 -06001002 bool verbose = true;
Cyrill Gorcunov82667ff2011-06-25 19:34:19 +04001003
Cyrill Gorcunovfc0c1282011-06-25 19:51:44 +04001004 nasm_trace("Tokenize for '%s'", line);
1005
Keith Kanios6faad4e2010-12-18 14:08:02 -06001006 if ((defining != NULL) && (defining->ignoring == true)) {
1007 verbose = false;
1008 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001009
H. Peter Anvine2c80182005-01-15 22:15:51 +00001010 while (*line) {
1011 p = line;
1012 if (*p == '%') {
1013 p++;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001014 if (*p == '+' && !nasm_isdigit(p[1])) {
1015 p++;
1016 type = TOK_PASTE;
1017 } else if (nasm_isdigit(*p) ||
1018 ((*p == '-' || *p == '+') && nasm_isdigit(p[1]))) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001019 do {
1020 p++;
1021 }
H. Peter Anvinbda7a6e2008-06-21 10:23:17 -07001022 while (nasm_isdigit(*p));
H. Peter Anvine2c80182005-01-15 22:15:51 +00001023 type = TOK_PREPROC_ID;
1024 } else if (*p == '{') {
1025 p++;
Keith Kaniosb307a4f2010-11-06 17:41:51 -05001026 while (*p && *p != '}') {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001027 p[-1] = *p;
1028 p++;
1029 }
1030 p[-1] = '\0';
1031 if (*p)
1032 p++;
1033 type = TOK_PREPROC_ID;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001034 } else if (*p == '[') {
1035 int lvl = 1;
1036 line += 2; /* Skip the leading %[ */
1037 p++;
1038 while (lvl && (c = *p++)) {
1039 switch (c) {
1040 case ']':
1041 lvl--;
1042 break;
1043 case '%':
1044 if (*p == '[')
1045 lvl++;
1046 break;
1047 case '\'':
1048 case '\"':
1049 case '`':
Cyrill Gorcunovc6360a72010-07-13 13:32:19 +04001050 p = nasm_skip_string(p - 1) + 1;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001051 break;
1052 default:
1053 break;
1054 }
1055 }
1056 p--;
1057 if (*p)
1058 *p++ = '\0';
Keith Kanios6faad4e2010-12-18 14:08:02 -06001059 if (lvl && verbose)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001060 error(ERR_NONFATAL, "unterminated %[ construct");
1061 type = TOK_INDIRECT;
1062 } else if (*p == '?') {
1063 type = TOK_PREPROC_Q; /* %? */
1064 p++;
1065 if (*p == '?') {
1066 type = TOK_PREPROC_QQ; /* %?? */
1067 p++;
1068 }
Cyrill Gorcunov84b4cba2010-09-12 21:39:40 +04001069 } else if (*p == '!') {
1070 type = TOK_PREPROC_ID;
1071 p++;
1072 if (isidchar(*p)) {
1073 do {
1074 p++;
1075 } while (isidchar(*p));
1076 } else if (*p == '\'' || *p == '\"' || *p == '`') {
1077 p = nasm_skip_string(p);
1078 if (*p)
1079 p++;
Keith Kanios6faad4e2010-12-18 14:08:02 -06001080 else if(verbose)
Cyrill Gorcunov84b4cba2010-09-12 21:39:40 +04001081 error(ERR_NONFATAL|ERR_PASS1, "unterminated %! string");
1082 } else {
1083 /* %! without string or identifier */
1084 type = TOK_OTHER; /* Legacy behavior... */
1085 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001086 } else if (isidchar(*p) ||
1087 ((*p == '!' || *p == '%' || *p == '$') &&
1088 isidchar(p[1]))) {
1089 do {
1090 p++;
1091 }
1092 while (isidchar(*p));
1093 type = TOK_PREPROC_ID;
1094 } else {
1095 type = TOK_OTHER;
1096 if (*p == '%')
1097 p++;
1098 }
1099 } else if (isidstart(*p) || (*p == '$' && isidstart(p[1]))) {
1100 type = TOK_ID;
1101 p++;
1102 while (*p && isidchar(*p))
1103 p++;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07001104 } else if (*p == '\'' || *p == '"' || *p == '`') {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001105 /*
1106 * A string token.
1107 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001108 type = TOK_STRING;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001109 p = nasm_skip_string(p);
Nickolay Yurchenkof3b3ce22003-09-21 20:38:43 +00001110
H. Peter Anvine2c80182005-01-15 22:15:51 +00001111 if (*p) {
1112 p++;
Keith Kanios6faad4e2010-12-18 14:08:02 -06001113 } else if(verbose) {
H. Peter Anvin917a3492008-09-24 09:14:49 -07001114 error(ERR_WARNING|ERR_PASS1, "unterminated string");
H. Peter Anvine2c80182005-01-15 22:15:51 +00001115 /* Handling unterminated strings by UNV */
1116 /* type = -1; */
1117 }
Victor van den Elzenfb5f2512009-04-17 16:17:59 +02001118 } else if (p[0] == '$' && p[1] == '$') {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001119 type = TOK_OTHER; /* TOKEN_BASE */
Victor van den Elzenfb5f2512009-04-17 16:17:59 +02001120 p += 2;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001121 } else if (isnumstart(*p)) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001122 bool is_hex = false;
1123 bool is_float = false;
1124 bool has_e = false;
1125 char c, *r;
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001126
H. Peter Anvine2c80182005-01-15 22:15:51 +00001127 /*
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001128 * A numeric token.
H. Peter Anvine2c80182005-01-15 22:15:51 +00001129 */
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001130
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001131 if (*p == '$') {
1132 p++;
1133 is_hex = true;
1134 }
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001135
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001136 for (;;) {
1137 c = *p++;
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001138
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001139 if (!is_hex && (c == 'e' || c == 'E')) {
1140 has_e = true;
1141 if (*p == '+' || *p == '-') {
1142 /*
1143 * e can only be followed by +/- if it is either a
1144 * prefixed hex number or a floating-point number
1145 */
1146 p++;
1147 is_float = true;
1148 }
1149 } else if (c == 'H' || c == 'h' || c == 'X' || c == 'x') {
1150 is_hex = true;
1151 } else if (c == 'P' || c == 'p') {
1152 is_float = true;
1153 if (*p == '+' || *p == '-')
1154 p++;
1155 } else if (isnumchar(c) || c == '_')
1156 ; /* just advance */
1157 else if (c == '.') {
1158 /*
1159 * we need to deal with consequences of the legacy
1160 * parser, like "1.nolist" being two tokens
1161 * (TOK_NUMBER, TOK_ID) here; at least give it
1162 * a shot for now. In the future, we probably need
1163 * a flex-based scanner with proper pattern matching
1164 * to do it as well as it can be done. Nothing in
1165 * the world is going to help the person who wants
1166 * 0x123.p16 interpreted as two tokens, though.
1167 */
1168 r = p;
1169 while (*r == '_')
1170 r++;
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001171
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001172 if (nasm_isdigit(*r) || (is_hex && nasm_isxdigit(*r)) ||
1173 (!is_hex && (*r == 'e' || *r == 'E')) ||
1174 (*r == 'p' || *r == 'P')) {
1175 p = r;
1176 is_float = true;
1177 } else
1178 break; /* Terminate the token */
1179 } else
1180 break;
1181 }
1182 p--; /* Point to first character beyond number */
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001183
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001184 if (p == line+1 && *line == '$') {
1185 type = TOK_OTHER; /* TOKEN_HERE */
1186 } else {
1187 if (has_e && !is_hex) {
1188 /* 1e13 is floating-point, but 1e13h is not */
1189 is_float = true;
1190 }
H. Peter Anvind784a082009-04-20 14:01:18 -07001191
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001192 type = is_float ? TOK_FLOAT : TOK_NUMBER;
1193 }
H. Peter Anvinbda7a6e2008-06-21 10:23:17 -07001194 } else if (nasm_isspace(*p)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001195 type = TOK_WHITESPACE;
Cyrill Gorcunovf66ac7d2009-10-12 20:41:13 +04001196 p = nasm_skip_spaces(p);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001197 /*
1198 * Whitespace just before end-of-line is discarded by
1199 * pretending it's a comment; whitespace just before a
1200 * comment gets lumped into the comment.
1201 */
1202 if (!*p || *p == ';') {
1203 type = TOK_COMMENT;
1204 while (*p)
1205 p++;
1206 }
1207 } else if (*p == ';') {
1208 type = TOK_COMMENT;
1209 while (*p)
1210 p++;
1211 } else {
1212 /*
1213 * Anything else is an operator of some kind. We check
1214 * for all the double-character operators (>>, <<, //,
1215 * %%, <=, >=, ==, !=, <>, &&, ||, ^^), but anything
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001216 * else is a single-character operator.
H. Peter Anvine2c80182005-01-15 22:15:51 +00001217 */
1218 type = TOK_OTHER;
1219 if ((p[0] == '>' && p[1] == '>') ||
1220 (p[0] == '<' && p[1] == '<') ||
1221 (p[0] == '/' && p[1] == '/') ||
1222 (p[0] == '<' && p[1] == '=') ||
1223 (p[0] == '>' && p[1] == '=') ||
1224 (p[0] == '=' && p[1] == '=') ||
1225 (p[0] == '!' && p[1] == '=') ||
1226 (p[0] == '<' && p[1] == '>') ||
1227 (p[0] == '&' && p[1] == '&') ||
1228 (p[0] == '|' && p[1] == '|') ||
1229 (p[0] == '^' && p[1] == '^')) {
1230 p++;
1231 }
1232 p++;
1233 }
Nickolay Yurchenkof3b3ce22003-09-21 20:38:43 +00001234
H. Peter Anvine2c80182005-01-15 22:15:51 +00001235 /* Handling unterminated string by UNV */
1236 /*if (type == -1)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001237 {
1238 *tail = t = new_Token(NULL, TOK_STRING, line, p-line+1);
1239 t->text[p-line] = *line;
1240 tail = &t->next;
1241 }
1242 else */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001243 if (type != TOK_COMMENT) {
1244 *tail = t = new_Token(NULL, type, line, p - line);
1245 tail = &t->next;
1246 }
1247 line = p;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001248 }
Cyrill Gorcunovfc0c1282011-06-25 19:51:44 +04001249
1250 nasm_dump_token(list);
1251
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001252 return list;
1253}
1254
H. Peter Anvince616072002-04-30 21:02:23 +00001255/*
1256 * this function allocates a new managed block of memory and
H. Peter Anvin70653092007-10-19 14:42:29 -07001257 * returns a pointer to the block. The managed blocks are
H. Peter Anvince616072002-04-30 21:02:23 +00001258 * deleted only all at once by the delete_Blocks function.
1259 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001260static void *new_Block(size_t size)
H. Peter Anvince616072002-04-30 21:02:23 +00001261{
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001262 Blocks *b = &blocks;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001263
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001264 /* first, get to the end of the linked list */
1265 while (b->next)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001266 b = b->next;
Cyrill Gorcunov6b271292011-02-28 08:45:52 +03001267
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001268 /* now allocate the requested chunk */
1269 b->chunk = nasm_malloc(size);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001270
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001271 /* now allocate a new block for the next request */
Cyrill Gorcunov6b271292011-02-28 08:45:52 +03001272 b->next = nasm_zalloc(sizeof(Blocks));
1273
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001274 return b->chunk;
H. Peter Anvince616072002-04-30 21:02:23 +00001275}
1276
1277/*
1278 * this function deletes all managed blocks of memory
1279 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001280static void delete_Blocks(void)
H. Peter Anvince616072002-04-30 21:02:23 +00001281{
H. Peter Anvine2c80182005-01-15 22:15:51 +00001282 Blocks *a, *b = &blocks;
H. Peter Anvince616072002-04-30 21:02:23 +00001283
H. Peter Anvin70653092007-10-19 14:42:29 -07001284 /*
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001285 * keep in mind that the first block, pointed to by blocks
H. Peter Anvin70653092007-10-19 14:42:29 -07001286 * is a static and not dynamically allocated, so we don't
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001287 * free it.
1288 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001289 while (b) {
Cyrill Gorcunovb6c6ca92011-06-28 01:33:02 +04001290 nasm_free(b->chunk);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001291 a = b;
1292 b = b->next;
1293 if (a != &blocks)
1294 nasm_free(a);
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001295 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001296}
H. Peter Anvin734b1882002-04-30 21:01:08 +00001297
1298/*
H. Peter Anvin70653092007-10-19 14:42:29 -07001299 * this function creates a new Token and passes a pointer to it
H. Peter Anvin734b1882002-04-30 21:01:08 +00001300 * back to the caller. It sets the type and text elements, and
H. Peter Anvinf26e0972008-07-01 21:26:27 -07001301 * also the a.mac and next elements to NULL.
H. Peter Anvin734b1882002-04-30 21:01:08 +00001302 */
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07001303static Token *new_Token(Token * next, enum pp_token_type type,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001304 const char *text, int txtlen)
H. Peter Anvin734b1882002-04-30 21:01:08 +00001305{
1306 Token *t;
1307 int i;
1308
H. Peter Anvin89cee572009-07-15 09:16:54 -04001309 if (!freeTokens) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001310 freeTokens = (Token *) new_Block(TOKEN_BLOCKSIZE * sizeof(Token));
1311 for (i = 0; i < TOKEN_BLOCKSIZE - 1; i++)
1312 freeTokens[i].next = &freeTokens[i + 1];
1313 freeTokens[i].next = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001314 }
1315 t = freeTokens;
1316 freeTokens = t->next;
1317 t->next = next;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07001318 t->a.mac = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001319 t->type = type;
H. Peter Anvin89cee572009-07-15 09:16:54 -04001320 if (type == TOK_WHITESPACE || !text) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001321 t->text = NULL;
1322 } else {
1323 if (txtlen == 0)
1324 txtlen = strlen(text);
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07001325 t->text = nasm_malloc(txtlen+1);
1326 memcpy(t->text, text, txtlen);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001327 t->text[txtlen] = '\0';
H. Peter Anvin734b1882002-04-30 21:01:08 +00001328 }
1329 return t;
1330}
1331
Keith Kaniosb307a4f2010-11-06 17:41:51 -05001332static Token *copy_Token(Token * tline)
1333{
1334 Token *t, *tt, *first = NULL, *prev = NULL;
1335 int i;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03001336 for (tt = tline; tt != NULL; tt = tt->next) {
1337 if (!freeTokens) {
1338 freeTokens = (Token *) new_Block(TOKEN_BLOCKSIZE * sizeof(Token));
1339 for (i = 0; i < TOKEN_BLOCKSIZE - 1; i++)
1340 freeTokens[i].next = &freeTokens[i + 1];
1341 freeTokens[i].next = NULL;
1342 }
1343 t = freeTokens;
1344 freeTokens = t->next;
1345 t->next = NULL;
1346 t->text = tt->text ? nasm_strdup(tt->text) : NULL;
1347 t->a.mac = tt->a.mac;
1348 t->a.len = tt->a.len;
1349 t->type = tt->type;
1350 if (prev != NULL) {
1351 prev->next = t;
1352 } else {
1353 first = t;
1354 }
1355 prev = t;
1356 }
Keith Kaniosb307a4f2010-11-06 17:41:51 -05001357 return first;
1358}
1359
H. Peter Anvine2c80182005-01-15 22:15:51 +00001360static Token *delete_Token(Token * t)
H. Peter Anvin734b1882002-04-30 21:01:08 +00001361{
1362 Token *next = t->next;
1363 nasm_free(t->text);
H. Peter Anvin788e6c12002-04-30 21:02:01 +00001364 t->next = freeTokens;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001365 freeTokens = t;
1366 return next;
1367}
1368
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001369/*
1370 * Convert a line of tokens back into text.
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001371 * If expand_locals is not zero, identifiers of the form "%$*xxx"
1372 * will be transformed into ..@ctxnum.xxx
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001373 */
H. Peter Anvin9e200162008-06-04 17:23:14 -07001374static char *detoken(Token * tlist, bool expand_locals)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001375{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001376 Token *t;
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001377 char *line, *p;
H. Peter Anvinb4daadc2008-01-21 16:31:57 -08001378 const char *q;
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001379 int len = 0;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001380
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001381 list_for_each(t, tlist) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001382 if (t->type == TOK_PREPROC_ID && t->text[1] == '!') {
Cyrill Gorcunov84b4cba2010-09-12 21:39:40 +04001383 char *v;
1384 char *q = t->text;
H. Peter Anvin077fb932010-07-20 14:56:30 -07001385
Cyrill Gorcunov84b4cba2010-09-12 21:39:40 +04001386 v = t->text + 2;
1387 if (*v == '\'' || *v == '\"' || *v == '`') {
1388 size_t len = nasm_unquote(v, NULL);
1389 size_t clen = strlen(v);
H. Peter Anvin077fb932010-07-20 14:56:30 -07001390
Cyrill Gorcunov84b4cba2010-09-12 21:39:40 +04001391 if (len != clen) {
1392 error(ERR_NONFATAL | ERR_PASS1,
1393 "NUL character in %! string");
1394 v = NULL;
1395 }
1396 }
H. Peter Anvin077fb932010-07-20 14:56:30 -07001397
Cyrill Gorcunov84b4cba2010-09-12 21:39:40 +04001398 if (v) {
1399 char *p = getenv(v);
1400 if (!p) {
1401 error(ERR_NONFATAL | ERR_PASS1,
1402 "nonexistent environment variable `%s'", v);
1403 p = "";
1404 }
1405 t->text = nasm_strdup(p);
1406 }
1407 nasm_free(q);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001408 }
H. Peter Anvin077fb932010-07-20 14:56:30 -07001409
H. Peter Anvine2c80182005-01-15 22:15:51 +00001410 /* Expand local macros here and not during preprocessing */
1411 if (expand_locals &&
1412 t->type == TOK_PREPROC_ID && t->text &&
1413 t->text[0] == '%' && t->text[1] == '$') {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001414 const char *q;
1415 char *p;
Cyrill Gorcunov290eac72011-06-28 01:59:05 +04001416 Context *ctx = get_ctx(t->text, &q);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001417 if (ctx) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001418 char buffer[40];
Keith Kanios93f2e9a2007-04-14 00:10:59 +00001419 snprintf(buffer, sizeof(buffer), "..@%"PRIu32".", ctx->number);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001420 p = nasm_strcat(buffer, q);
1421 nasm_free(t->text);
1422 t->text = p;
1423 }
1424 }
Keith Kaniosb307a4f2010-11-06 17:41:51 -05001425
1426 /* Expand %? and %?? directives */
Keith Kanios3136d482010-11-13 09:34:34 -06001427 if ((istk->expansion != NULL) &&
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03001428 ((t->type == TOK_PREPROC_Q) ||
1429 (t->type == TOK_PREPROC_QQ))) {
Keith Kaniosb307a4f2010-11-06 17:41:51 -05001430 ExpInv *ei;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03001431 for (ei = istk->expansion; ei != NULL; ei = ei->prev){
1432 if (ei->type == EXP_MMACRO) {
1433 nasm_free(t->text);
1434 if (t->type == TOK_PREPROC_Q) {
1435 t->text = nasm_strdup(ei->name);
1436 } else {
1437 t->text = nasm_strdup(ei->def->name);
1438 }
1439 break;
1440 }
1441 }
1442 }
Keith Kaniosb307a4f2010-11-06 17:41:51 -05001443
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001444 if (t->type == TOK_WHITESPACE)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001445 len++;
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001446 else if (t->text)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001447 len += strlen(t->text);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001448 }
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001449
H. Peter Anvin734b1882002-04-30 21:01:08 +00001450 p = line = nasm_malloc(len + 1);
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001451
1452 list_for_each(t, tlist) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001453 if (t->type == TOK_WHITESPACE) {
H. Peter Anvinb4daadc2008-01-21 16:31:57 -08001454 *p++ = ' ';
H. Peter Anvine2c80182005-01-15 22:15:51 +00001455 } else if (t->text) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001456 q = t->text;
1457 while (*q)
1458 *p++ = *q++;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001459 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001460 }
1461 *p = '\0';
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001462
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001463 return line;
1464}
1465
1466/*
Keith Kaniosb307a4f2010-11-06 17:41:51 -05001467 * Initialize a new Line
1468 */
Cyrill Gorcunov29cb0bb2010-11-11 11:19:43 +03001469static inline Line *new_Line(void)
Keith Kaniosb307a4f2010-11-06 17:41:51 -05001470{
Cyrill Gorcunov6b271292011-02-28 08:45:52 +03001471 return (Line *)nasm_zalloc(sizeof(Line));
Keith Kaniosb307a4f2010-11-06 17:41:51 -05001472}
1473
1474
1475/*
1476 * Initialize a new Expansion Definition
1477 */
1478static ExpDef *new_ExpDef(int exp_type)
1479{
Cyrill Gorcunovc5157742010-11-11 11:29:40 +03001480 ExpDef *ed = (ExpDef*)nasm_zalloc(sizeof(ExpDef));
1481 ed->type = exp_type;
1482 ed->casesense = true;
1483 ed->state = COND_NEVER;
1484
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03001485 return ed;
Keith Kaniosb307a4f2010-11-06 17:41:51 -05001486}
1487
1488
1489/*
1490 * Initialize a new Expansion Instance
1491 */
1492static ExpInv *new_ExpInv(int exp_type, ExpDef *ed)
1493{
Cyrill Gorcunovc5157742010-11-11 11:29:40 +03001494 ExpInv *ei = (ExpInv*)nasm_zalloc(sizeof(ExpInv));
1495 ei->type = exp_type;
1496 ei->def = ed;
1497 ei->unique = ++unique;
1498
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03001499 if ((istk->mmac_depth < 1) &&
1500 (istk->expansion == NULL) &&
1501 (ed != NULL) &&
1502 (ed->type != EXP_MMACRO) &&
1503 (ed->type != EXP_REP) &&
1504 (ed->type != EXP_WHILE)) {
1505 ei->linnum = src_get_linnum();
1506 src_set_linnum(ei->linnum - ed->linecount - 1);
1507 } else {
1508 ei->linnum = -1;
1509 }
1510 if ((istk->expansion == NULL) ||
1511 (ei->type == EXP_MMACRO)) {
1512 ei->relno = 0;
1513 } else {
1514 ei->relno = istk->expansion->lineno;
1515 if (ed != NULL) {
1516 ei->relno -= (ed->linecount + 1);
1517 }
1518 }
1519 return ei;
Keith Kaniosb307a4f2010-11-06 17:41:51 -05001520}
1521
1522/*
H. Peter Anvin76690a12002-04-30 20:52:49 +00001523 * A scanner, suitable for use by the expression evaluator, which
1524 * operates on a line of Tokens. Expects a pointer to a pointer to
1525 * the first token in the line to be passed in as its private_data
1526 * field.
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001527 *
1528 * FIX: This really needs to be unified with stdscan.
H. Peter Anvin76690a12002-04-30 20:52:49 +00001529 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001530static int ppscan(void *private_data, struct tokenval *tokval)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001531{
H. Peter Anvin76690a12002-04-30 20:52:49 +00001532 Token **tlineptr = private_data;
1533 Token *tline;
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001534 char ourcopy[MAX_KEYWORD+1], *p, *r, *s;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001535
H. Peter Anvine2c80182005-01-15 22:15:51 +00001536 do {
1537 tline = *tlineptr;
1538 *tlineptr = tline ? tline->next : NULL;
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04001539 } while (tline && (tline->type == TOK_WHITESPACE ||
1540 tline->type == TOK_COMMENT));
H. Peter Anvin76690a12002-04-30 20:52:49 +00001541
1542 if (!tline)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001543 return tokval->t_type = TOKEN_EOS;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001544
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001545 tokval->t_charptr = tline->text;
1546
H. Peter Anvin76690a12002-04-30 20:52:49 +00001547 if (tline->text[0] == '$' && !tline->text[1])
H. Peter Anvine2c80182005-01-15 22:15:51 +00001548 return tokval->t_type = TOKEN_HERE;
H. Peter Anvin7cf897e2002-05-30 21:30:33 +00001549 if (tline->text[0] == '$' && tline->text[1] == '$' && !tline->text[2])
H. Peter Anvine2c80182005-01-15 22:15:51 +00001550 return tokval->t_type = TOKEN_BASE;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001551
H. Peter Anvine2c80182005-01-15 22:15:51 +00001552 if (tline->type == TOK_ID) {
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001553 p = tokval->t_charptr = tline->text;
1554 if (p[0] == '$') {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001555 tokval->t_charptr++;
1556 return tokval->t_type = TOKEN_ID;
1557 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00001558
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001559 for (r = p, s = ourcopy; *r; r++) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001560 if (r >= p+MAX_KEYWORD)
1561 return tokval->t_type = TOKEN_ID; /* Not a keyword */
H. Peter Anvinac8f8fc2008-06-11 15:49:41 -07001562 *s++ = nasm_tolower(*r);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001563 }
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001564 *s = '\0';
1565 /* right, so we have an identifier sitting in temp storage. now,
1566 * is it actually a register or instruction name, or what? */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001567 return nasm_token_hash(ourcopy, tokval);
H. Peter Anvin76690a12002-04-30 20:52:49 +00001568 }
1569
H. Peter Anvine2c80182005-01-15 22:15:51 +00001570 if (tline->type == TOK_NUMBER) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001571 bool rn_error;
1572 tokval->t_integer = readnum(tline->text, &rn_error);
1573 tokval->t_charptr = tline->text;
1574 if (rn_error)
1575 return tokval->t_type = TOKEN_ERRNUM;
1576 else
1577 return tokval->t_type = TOKEN_NUM;
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001578 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00001579
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001580 if (tline->type == TOK_FLOAT) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001581 return tokval->t_type = TOKEN_FLOAT;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001582 }
1583
H. Peter Anvine2c80182005-01-15 22:15:51 +00001584 if (tline->type == TOK_STRING) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001585 char bq, *ep;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001586
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001587 bq = tline->text[0];
H. Peter Anvin11627042008-06-09 20:45:19 -07001588 tokval->t_charptr = tline->text;
1589 tokval->t_inttwo = nasm_unquote(tline->text, &ep);
H. Peter Anvind2456592008-06-19 15:04:18 -07001590
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001591 if (ep[0] != bq || ep[1] != '\0')
1592 return tokval->t_type = TOKEN_ERRSTR;
1593 else
1594 return tokval->t_type = TOKEN_STR;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001595 }
1596
H. Peter Anvine2c80182005-01-15 22:15:51 +00001597 if (tline->type == TOK_OTHER) {
1598 if (!strcmp(tline->text, "<<"))
1599 return tokval->t_type = TOKEN_SHL;
1600 if (!strcmp(tline->text, ">>"))
1601 return tokval->t_type = TOKEN_SHR;
1602 if (!strcmp(tline->text, "//"))
1603 return tokval->t_type = TOKEN_SDIV;
1604 if (!strcmp(tline->text, "%%"))
1605 return tokval->t_type = TOKEN_SMOD;
1606 if (!strcmp(tline->text, "=="))
1607 return tokval->t_type = TOKEN_EQ;
1608 if (!strcmp(tline->text, "<>"))
1609 return tokval->t_type = TOKEN_NE;
1610 if (!strcmp(tline->text, "!="))
1611 return tokval->t_type = TOKEN_NE;
1612 if (!strcmp(tline->text, "<="))
1613 return tokval->t_type = TOKEN_LE;
1614 if (!strcmp(tline->text, ">="))
1615 return tokval->t_type = TOKEN_GE;
1616 if (!strcmp(tline->text, "&&"))
1617 return tokval->t_type = TOKEN_DBL_AND;
1618 if (!strcmp(tline->text, "^^"))
1619 return tokval->t_type = TOKEN_DBL_XOR;
1620 if (!strcmp(tline->text, "||"))
1621 return tokval->t_type = TOKEN_DBL_OR;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001622 }
1623
1624 /*
1625 * We have no other options: just return the first character of
1626 * the token text.
1627 */
1628 return tokval->t_type = tline->text[0];
1629}
1630
1631/*
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001632 * Compare a string to the name of an existing macro; this is a
1633 * simple wrapper which calls either strcmp or nasm_stricmp
1634 * depending on the value of the `casesense' parameter.
1635 */
H. Peter Anvin4db5a162007-10-11 13:42:09 -07001636static int mstrcmp(const char *p, const char *q, bool casesense)
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001637{
H. Peter Anvin734b1882002-04-30 21:01:08 +00001638 return casesense ? strcmp(p, q) : nasm_stricmp(p, q);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001639}
1640
1641/*
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07001642 * Compare a string to the name of an existing macro; this is a
1643 * simple wrapper which calls either strcmp or nasm_stricmp
1644 * depending on the value of the `casesense' parameter.
1645 */
1646static int mmemcmp(const char *p, const char *q, size_t l, bool casesense)
1647{
1648 return casesense ? memcmp(p, q, l) : nasm_memicmp(p, q, l);
1649}
1650
1651/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001652 * Return the Context structure associated with a %$ token. Return
1653 * NULL, having _already_ reported an error condition, if the
1654 * context stack isn't deep enough for the supplied number of $
1655 * signs.
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001656 *
1657 * If "namep" is non-NULL, set it to the pointer to the macro name
1658 * tail, i.e. the part beyond %$...
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001659 */
Cyrill Gorcunov290eac72011-06-28 01:59:05 +04001660static Context *get_ctx(const char *name, const char **namep)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001661{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001662 Context *ctx;
1663 int i;
1664
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001665 if (namep)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001666 *namep = name;
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001667
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001668 if (!name || name[0] != '%' || name[1] != '$')
H. Peter Anvine2c80182005-01-15 22:15:51 +00001669 return NULL;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001670
H. Peter Anvine2c80182005-01-15 22:15:51 +00001671 if (!cstk) {
1672 error(ERR_NONFATAL, "`%s': context stack is empty", name);
1673 return NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001674 }
1675
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001676 name += 2;
1677 ctx = cstk;
1678 i = 0;
1679 while (ctx && *name == '$') {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001680 name++;
1681 i++;
1682 ctx = ctx->next;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001683 }
Cyrill Gorcunov290eac72011-06-28 01:59:05 +04001684
H. Peter Anvine2c80182005-01-15 22:15:51 +00001685 if (!ctx) {
1686 error(ERR_NONFATAL, "`%s': context stack is only"
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001687 " %d level%s deep", name, i, (i == 1 ? "" : "s"));
H. Peter Anvine2c80182005-01-15 22:15:51 +00001688 return NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001689 }
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001690
1691 if (namep)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001692 *namep = name;
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001693
Cyrill Gorcunov290eac72011-06-28 01:59:05 +04001694 return ctx;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001695}
1696
1697/*
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001698 * Check to see if a file is already in a string list
1699 */
1700static bool in_list(const StrList *list, const char *str)
1701{
1702 while (list) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001703 if (!strcmp(list->str, str))
1704 return true;
1705 list = list->next;
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001706 }
1707 return false;
1708}
1709
1710/*
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001711 * Open an include file. This routine must always return a valid
1712 * file pointer if it returns - it's responsible for throwing an
1713 * ERR_FATAL and bombing out completely if not. It should also try
1714 * the include path one by one until it finds the file or reaches
1715 * the end of the path.
1716 */
H. Peter Anvin2b1c3b92008-06-06 10:38:46 -07001717static FILE *inc_fopen(const char *file, StrList **dhead, StrList ***dtail,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001718 bool missing_ok)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001719{
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001720 FILE *fp;
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001721 char *prefix = "";
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001722 IncPath *ip = ipath;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001723 int len = strlen(file);
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001724 size_t prefix_len = 0;
1725 StrList *sl;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001726
H. Peter Anvine2c80182005-01-15 22:15:51 +00001727 while (1) {
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001728 sl = nasm_malloc(prefix_len+len+1+sizeof sl->next);
Cyrill Gorcunov6f38fe62010-11-11 11:32:16 +03001729 sl->next = NULL;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001730 memcpy(sl->str, prefix, prefix_len);
1731 memcpy(sl->str+prefix_len, file, len+1);
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001732 fp = fopen(sl->str, "r");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001733 if (fp && dhead && !in_list(*dhead, sl->str)) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001734 **dtail = sl;
1735 *dtail = &sl->next;
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001736 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001737 nasm_free(sl);
1738 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001739 if (fp)
1740 return fp;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001741 if (!ip) {
1742 if (!missing_ok)
1743 break;
1744 prefix = NULL;
1745 } else {
1746 prefix = ip->path;
1747 ip = ip->next;
1748 }
1749 if (prefix) {
1750 prefix_len = strlen(prefix);
1751 } else {
1752 /* -MG given and file not found */
1753 if (dhead && !in_list(*dhead, file)) {
1754 sl = nasm_malloc(len+1+sizeof sl->next);
1755 sl->next = NULL;
1756 strcpy(sl->str, file);
1757 **dtail = sl;
1758 *dtail = &sl->next;
1759 }
1760 return NULL;
1761 }
H. Peter Anvineba20a72002-04-30 20:53:55 +00001762 }
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001763
H. Peter Anvin734b1882002-04-30 21:01:08 +00001764 error(ERR_FATAL, "unable to open include file `%s'", file);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001765 return NULL;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001766}
1767
1768/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001769 * Determine if we should warn on defining a single-line macro of
H. Peter Anvinef7468f2002-04-30 20:57:59 +00001770 * name `name', with `nparam' parameters. If nparam is 0 or -1, will
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001771 * return true if _any_ single-line macro of that name is defined.
1772 * Otherwise, will return true if a single-line macro with either
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001773 * `nparam' or no parameters is defined.
1774 *
1775 * If a macro with precisely the right number of parameters is
H. Peter Anvinef7468f2002-04-30 20:57:59 +00001776 * defined, or nparam is -1, the address of the definition structure
1777 * will be returned in `defn'; otherwise NULL will be returned. If `defn'
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001778 * is NULL, no action will be taken regarding its contents, and no
1779 * error will occur.
1780 *
1781 * Note that this is also called with nparam zero to resolve
1782 * `ifdef'.
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001783 *
1784 * If you already know which context macro belongs to, you can pass
1785 * the context pointer as first parameter; if you won't but name begins
1786 * with %$ the context will be automatically computed. If all_contexts
1787 * is true, macro will be searched in outer contexts as well.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001788 */
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07001789static bool
H. Peter Anvinb2a5fda2008-06-19 21:42:42 -07001790smacro_defined(Context * ctx, const char *name, int nparam, SMacro ** defn,
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07001791 bool nocase)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001792{
H. Peter Anvin166c2472008-05-28 12:28:58 -07001793 struct hash_table *smtbl;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001794 SMacro *m;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001795
H. Peter Anvin97a23472007-09-16 17:57:25 -07001796 if (ctx) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001797 smtbl = &ctx->localmac;
H. Peter Anvin97a23472007-09-16 17:57:25 -07001798 } else if (name[0] == '%' && name[1] == '$') {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001799 if (cstk)
Cyrill Gorcunov290eac72011-06-28 01:59:05 +04001800 ctx = get_ctx(name, &name);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001801 if (!ctx)
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001802 return false; /* got to return _something_ */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001803 smtbl = &ctx->localmac;
H. Peter Anvin97a23472007-09-16 17:57:25 -07001804 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001805 smtbl = &smacros;
H. Peter Anvin97a23472007-09-16 17:57:25 -07001806 }
H. Peter Anvin166c2472008-05-28 12:28:58 -07001807 m = (SMacro *) hash_findix(smtbl, name);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001808
H. Peter Anvine2c80182005-01-15 22:15:51 +00001809 while (m) {
1810 if (!mstrcmp(m->name, name, m->casesense && nocase) &&
Charles Crayne192d5b52007-10-18 19:02:42 -07001811 (nparam <= 0 || m->nparam == 0 || nparam == (int) m->nparam)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001812 if (defn) {
Charles Crayne192d5b52007-10-18 19:02:42 -07001813 if (nparam == (int) m->nparam || nparam == -1)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001814 *defn = m;
1815 else
1816 *defn = NULL;
1817 }
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001818 return true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001819 }
1820 m = m->next;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001821 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001822
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001823 return false;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001824}
1825
1826/*
1827 * Count and mark off the parameters in a multi-line macro call.
1828 * This is called both from within the multi-line macro expansion
1829 * code, and also to mark off the default parameters when provided
1830 * in a %macro definition line.
1831 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001832static void count_mmac_params(Token * t, int *nparam, Token *** params)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001833{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001834 int paramsize, brace;
1835
1836 *nparam = paramsize = 0;
1837 *params = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001838 while (t) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001839 /* +1: we need space for the final NULL */
H. Peter Anvin91fb6f12008-09-01 10:56:33 -07001840 if (*nparam+1 >= paramsize) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001841 paramsize += PARAM_DELTA;
1842 *params = nasm_realloc(*params, sizeof(**params) * paramsize);
1843 }
1844 skip_white_(t);
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001845 brace = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001846 if (tok_is_(t, "{"))
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001847 brace = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001848 (*params)[(*nparam)++] = t;
1849 while (tok_isnt_(t, brace ? "}" : ","))
1850 t = t->next;
1851 if (t) { /* got a comma/brace */
1852 t = t->next;
1853 if (brace) {
1854 /*
1855 * Now we've found the closing brace, look further
1856 * for the comma.
1857 */
1858 skip_white_(t);
1859 if (tok_isnt_(t, ",")) {
1860 error(ERR_NONFATAL,
1861 "braces do not enclose all of macro parameter");
1862 while (tok_isnt_(t, ","))
1863 t = t->next;
1864 }
1865 if (t)
1866 t = t->next; /* eat the comma */
1867 }
1868 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001869 }
1870}
1871
1872/*
H. Peter Anvin76690a12002-04-30 20:52:49 +00001873 * Determine whether one of the various `if' conditions is true or
1874 * not.
1875 *
1876 * We must free the tline we get passed.
1877 */
H. Peter Anvin70055962007-10-11 00:05:31 -07001878static bool if_condition(Token * tline, enum preproc_token ct)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001879{
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001880 enum pp_conditional i = PP_COND(ct);
H. Peter Anvin70055962007-10-11 00:05:31 -07001881 bool j;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001882 Token *t, *tt, **tptr, *origline;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001883 struct tokenval tokval;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001884 expr *evalresult;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001885 enum pp_token_type needtype;
H. Peter Anvin077fb932010-07-20 14:56:30 -07001886 char *p;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001887
1888 origline = tline;
1889
H. Peter Anvine2c80182005-01-15 22:15:51 +00001890 switch (i) {
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001891 case PPC_IFCTX:
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001892 j = false; /* have we matched yet? */
Victor van den Elzen0e857f12008-07-23 13:21:29 +02001893 while (true) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001894 skip_white_(tline);
Victor van den Elzen0e857f12008-07-23 13:21:29 +02001895 if (!tline)
1896 break;
1897 if (tline->type != TOK_ID) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001898 error(ERR_NONFATAL,
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001899 "`%s' expects context identifiers", pp_directives[ct]);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001900 free_tlist(origline);
1901 return -1;
1902 }
Victor van den Elzen0e857f12008-07-23 13:21:29 +02001903 if (cstk && cstk->name && !nasm_stricmp(tline->text, cstk->name))
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001904 j = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001905 tline = tline->next;
1906 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001907 break;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001908
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001909 case PPC_IFDEF:
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001910 j = false; /* have we matched yet? */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001911 while (tline) {
1912 skip_white_(tline);
1913 if (!tline || (tline->type != TOK_ID &&
1914 (tline->type != TOK_PREPROC_ID ||
1915 tline->text[1] != '$'))) {
1916 error(ERR_NONFATAL,
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001917 "`%s' expects macro identifiers", pp_directives[ct]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001918 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001919 }
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07001920 if (smacro_defined(NULL, tline->text, 0, NULL, true))
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001921 j = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001922 tline = tline->next;
1923 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001924 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001925
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001926 case PPC_IFENV:
Cyrill Gorcunov84b4cba2010-09-12 21:39:40 +04001927 tline = expand_smacro(tline);
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001928 j = false; /* have we matched yet? */
1929 while (tline) {
1930 skip_white_(tline);
1931 if (!tline || (tline->type != TOK_ID &&
Cyrill Gorcunov84b4cba2010-09-12 21:39:40 +04001932 tline->type != TOK_STRING &&
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001933 (tline->type != TOK_PREPROC_ID ||
Cyrill Gorcunov84b4cba2010-09-12 21:39:40 +04001934 tline->text[1] != '!'))) {
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001935 error(ERR_NONFATAL,
1936 "`%s' expects environment variable names",
Cyrill Gorcunov84b4cba2010-09-12 21:39:40 +04001937 pp_directives[ct]);
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001938 goto fail;
1939 }
Cyrill Gorcunov84b4cba2010-09-12 21:39:40 +04001940 p = tline->text;
1941 if (tline->type == TOK_PREPROC_ID)
1942 p += 2; /* Skip leading %! */
1943 if (*p == '\'' || *p == '\"' || *p == '`')
1944 nasm_unquote_cstr(p, ct);
1945 if (getenv(p))
1946 j = true;
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001947 tline = tline->next;
1948 }
1949 break;
1950
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001951 case PPC_IFIDN:
1952 case PPC_IFIDNI:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001953 tline = expand_smacro(tline);
1954 t = tt = tline;
1955 while (tok_isnt_(tt, ","))
1956 tt = tt->next;
1957 if (!tt) {
1958 error(ERR_NONFATAL,
1959 "`%s' expects two comma-separated arguments",
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001960 pp_directives[ct]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001961 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001962 }
1963 tt = tt->next;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001964 j = true; /* assume equality unless proved not */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001965 while ((t->type != TOK_OTHER || strcmp(t->text, ",")) && tt) {
1966 if (tt->type == TOK_OTHER && !strcmp(tt->text, ",")) {
1967 error(ERR_NONFATAL, "`%s': more than one comma on line",
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001968 pp_directives[ct]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001969 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001970 }
1971 if (t->type == TOK_WHITESPACE) {
1972 t = t->next;
1973 continue;
1974 }
1975 if (tt->type == TOK_WHITESPACE) {
1976 tt = tt->next;
1977 continue;
1978 }
1979 if (tt->type != t->type) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001980 j = false; /* found mismatching tokens */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001981 break;
1982 }
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07001983 /* When comparing strings, need to unquote them first */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001984 if (t->type == TOK_STRING) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001985 size_t l1 = nasm_unquote(t->text, NULL);
1986 size_t l2 = nasm_unquote(tt->text, NULL);
H. Peter Anvind2456592008-06-19 15:04:18 -07001987
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001988 if (l1 != l2) {
1989 j = false;
1990 break;
1991 }
1992 if (mmemcmp(t->text, tt->text, l1, i == PPC_IFIDN)) {
1993 j = false;
1994 break;
1995 }
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07001996 } else if (mstrcmp(tt->text, t->text, i == PPC_IFIDN) != 0) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001997 j = false; /* found mismatching tokens */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001998 break;
1999 }
Nickolay Yurchenkof3b3ce22003-09-21 20:38:43 +00002000
H. Peter Anvine2c80182005-01-15 22:15:51 +00002001 t = t->next;
2002 tt = tt->next;
2003 }
2004 if ((t->type != TOK_OTHER || strcmp(t->text, ",")) || tt)
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002005 j = false; /* trailing gunk on one end or other */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002006 break;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002007
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002008 case PPC_IFMACRO:
H. Peter Anvin89cee572009-07-15 09:16:54 -04002009 {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002010 bool found = false;
Keith Kaniosb307a4f2010-11-06 17:41:51 -05002011 ExpDef searching, *ed;
H. Peter Anvin65747262002-05-07 00:10:05 +00002012
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002013 skip_white_(tline);
2014 tline = expand_id(tline);
2015 if (!tok_type_(tline, TOK_ID)) {
2016 error(ERR_NONFATAL,
2017 "`%s' expects a macro name", pp_directives[ct]);
2018 goto fail;
2019 }
Cyrill Gorcunova22e7a92010-11-11 11:42:40 +03002020 memset(&searching, 0, sizeof(searching));
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002021 searching.name = nasm_strdup(tline->text);
2022 searching.casesense = true;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002023 searching.nparam_max = INT_MAX;
2024 tline = expand_smacro(tline->next);
2025 skip_white_(tline);
2026 if (!tline) {
2027 } else if (!tok_type_(tline, TOK_NUMBER)) {
2028 error(ERR_NONFATAL,
2029 "`%s' expects a parameter count or nothing",
2030 pp_directives[ct]);
2031 } else {
2032 searching.nparam_min = searching.nparam_max =
2033 readnum(tline->text, &j);
2034 if (j)
2035 error(ERR_NONFATAL,
2036 "unable to parse parameter count `%s'",
2037 tline->text);
2038 }
2039 if (tline && tok_is_(tline->next, "-")) {
2040 tline = tline->next->next;
2041 if (tok_is_(tline, "*"))
2042 searching.nparam_max = INT_MAX;
2043 else if (!tok_type_(tline, TOK_NUMBER))
2044 error(ERR_NONFATAL,
2045 "`%s' expects a parameter count after `-'",
2046 pp_directives[ct]);
2047 else {
2048 searching.nparam_max = readnum(tline->text, &j);
2049 if (j)
2050 error(ERR_NONFATAL,
2051 "unable to parse parameter count `%s'",
2052 tline->text);
2053 if (searching.nparam_min > searching.nparam_max)
2054 error(ERR_NONFATAL,
2055 "minimum parameter count exceeds maximum");
2056 }
2057 }
2058 if (tline && tok_is_(tline->next, "+")) {
2059 tline = tline->next;
2060 searching.plus = true;
2061 }
Keith Kaniosb307a4f2010-11-06 17:41:51 -05002062 ed = (ExpDef *) hash_findix(&expdefs, searching.name);
2063 while (ed != NULL) {
Cyrill Gorcunova22e7a92010-11-11 11:42:40 +03002064 if (!strcmp(ed->name, searching.name) &&
2065 (ed->nparam_min <= searching.nparam_max || searching.plus) &&
2066 (searching.nparam_min <= ed->nparam_max || ed->plus)) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002067 found = true;
2068 break;
2069 }
Keith Kaniosb307a4f2010-11-06 17:41:51 -05002070 ed = ed->next;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002071 }
2072 if (tline && tline->next)
2073 error(ERR_WARNING|ERR_PASS1,
2074 "trailing garbage after %%ifmacro ignored");
2075 nasm_free(searching.name);
2076 j = found;
2077 break;
H. Peter Anvin89cee572009-07-15 09:16:54 -04002078 }
H. Peter Anvin65747262002-05-07 00:10:05 +00002079
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002080 case PPC_IFID:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002081 needtype = TOK_ID;
2082 goto iftype;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002083 case PPC_IFNUM:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002084 needtype = TOK_NUMBER;
2085 goto iftype;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002086 case PPC_IFSTR:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002087 needtype = TOK_STRING;
2088 goto iftype;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002089
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002090iftype:
2091 t = tline = expand_smacro(tline);
H. Peter Anvind85d2502008-05-04 17:53:31 -07002092
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002093 while (tok_type_(t, TOK_WHITESPACE) ||
2094 (needtype == TOK_NUMBER &&
2095 tok_type_(t, TOK_OTHER) &&
2096 (t->text[0] == '-' || t->text[0] == '+') &&
2097 !t->text[1]))
2098 t = t->next;
H. Peter Anvind85d2502008-05-04 17:53:31 -07002099
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002100 j = tok_type_(t, needtype);
2101 break;
H. Peter Anvincbf768d2008-02-16 16:41:25 -08002102
2103 case PPC_IFTOKEN:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002104 t = tline = expand_smacro(tline);
2105 while (tok_type_(t, TOK_WHITESPACE))
2106 t = t->next;
H. Peter Anvincbf768d2008-02-16 16:41:25 -08002107
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002108 j = false;
2109 if (t) {
2110 t = t->next; /* Skip the actual token */
2111 while (tok_type_(t, TOK_WHITESPACE))
2112 t = t->next;
2113 j = !t; /* Should be nothing left */
2114 }
2115 break;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002116
H. Peter Anvin134b9462008-02-16 17:01:40 -08002117 case PPC_IFEMPTY:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002118 t = tline = expand_smacro(tline);
2119 while (tok_type_(t, TOK_WHITESPACE))
2120 t = t->next;
H. Peter Anvin134b9462008-02-16 17:01:40 -08002121
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002122 j = !t; /* Should be empty */
2123 break;
H. Peter Anvin134b9462008-02-16 17:01:40 -08002124
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002125 case PPC_IF:
H. Peter Anvine2c80182005-01-15 22:15:51 +00002126 t = tline = expand_smacro(tline);
2127 tptr = &t;
2128 tokval.t_type = TOKEN_INVALID;
2129 evalresult = evaluate(ppscan, tptr, &tokval,
2130 NULL, pass | CRITICAL, error, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002131 if (!evalresult)
2132 return -1;
2133 if (tokval.t_type)
H. Peter Anvin917a3492008-09-24 09:14:49 -07002134 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +00002135 "trailing garbage after expression ignored");
2136 if (!is_simple(evalresult)) {
2137 error(ERR_NONFATAL,
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002138 "non-constant value given to `%s'", pp_directives[ct]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002139 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002140 }
Chuck Crayne60ae75d2007-05-02 01:59:16 +00002141 j = reloc_value(evalresult) != 0;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002142 break;
H. Peter Anvin95e28822007-09-12 04:20:08 +00002143
H. Peter Anvine2c80182005-01-15 22:15:51 +00002144 default:
2145 error(ERR_FATAL,
2146 "preprocessor directive `%s' not yet implemented",
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002147 pp_directives[ct]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002148 goto fail;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002149 }
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002150
2151 free_tlist(origline);
2152 return j ^ PP_NEGATIVE(ct);
H. Peter Anvin70653092007-10-19 14:42:29 -07002153
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002154fail:
2155 free_tlist(origline);
2156 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002157}
2158
2159/*
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002160 * Common code for defining an smacro
2161 */
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08002162static bool define_smacro(Context *ctx, const char *mname, bool casesense,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002163 int nparam, Token *expansion)
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002164{
2165 SMacro *smac, **smhead;
H. Peter Anvin166c2472008-05-28 12:28:58 -07002166 struct hash_table *smtbl;
H. Peter Anvin70653092007-10-19 14:42:29 -07002167
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002168 if (smacro_defined(ctx, mname, nparam, &smac, casesense)) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002169 if (!smac) {
2170 error(ERR_WARNING|ERR_PASS1,
2171 "single-line macro `%s' defined both with and"
2172 " without parameters", mname);
2173 /*
2174 * Some instances of the old code considered this a failure,
2175 * some others didn't. What is the right thing to do here?
2176 */
2177 free_tlist(expansion);
2178 return false; /* Failure */
2179 } else {
2180 /*
2181 * We're redefining, so we have to take over an
2182 * existing SMacro structure. This means freeing
2183 * what was already in it.
2184 */
2185 nasm_free(smac->name);
2186 free_tlist(smac->expansion);
2187 }
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002188 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002189 smtbl = ctx ? &ctx->localmac : &smacros;
2190 smhead = (SMacro **) hash_findi_add(smtbl, mname);
Cyrill Gorcunov574fbf12010-11-11 13:44:51 +03002191 smac = nasm_zalloc(sizeof(SMacro));
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002192 smac->next = *smhead;
2193 *smhead = smac;
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002194 }
2195 smac->name = nasm_strdup(mname);
2196 smac->casesense = casesense;
2197 smac->nparam = nparam;
2198 smac->expansion = expansion;
2199 smac->in_progress = false;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002200 return true; /* Success */
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002201}
2202
2203/*
2204 * Undefine an smacro
2205 */
2206static void undef_smacro(Context *ctx, const char *mname)
2207{
2208 SMacro **smhead, *s, **sp;
H. Peter Anvin166c2472008-05-28 12:28:58 -07002209 struct hash_table *smtbl;
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002210
H. Peter Anvin166c2472008-05-28 12:28:58 -07002211 smtbl = ctx ? &ctx->localmac : &smacros;
2212 smhead = (SMacro **)hash_findi(smtbl, mname, NULL);
H. Peter Anvin70653092007-10-19 14:42:29 -07002213
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002214 if (smhead) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002215 /*
2216 * We now have a macro name... go hunt for it.
2217 */
2218 sp = smhead;
2219 while ((s = *sp) != NULL) {
2220 if (!mstrcmp(s->name, mname, s->casesense)) {
2221 *sp = s->next;
2222 nasm_free(s->name);
2223 free_tlist(s->expansion);
2224 nasm_free(s);
2225 } else {
2226 sp = &s->next;
2227 }
2228 }
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002229 }
2230}
2231
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002232/*
H. Peter Anvina26433d2008-07-16 14:40:01 -07002233 * Parse a mmacro specification.
2234 */
Keith Kaniosb307a4f2010-11-06 17:41:51 -05002235static bool parse_mmacro_spec(Token *tline, ExpDef *def, const char *directive)
H. Peter Anvina26433d2008-07-16 14:40:01 -07002236{
2237 bool err;
2238
2239 tline = tline->next;
2240 skip_white_(tline);
2241 tline = expand_id(tline);
2242 if (!tok_type_(tline, TOK_ID)) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002243 error(ERR_NONFATAL, "`%s' expects a macro name", directive);
2244 return false;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002245 }
Victor van den Elzenb916ede2008-07-23 15:14:22 +02002246
H. Peter Anvina26433d2008-07-16 14:40:01 -07002247 def->name = nasm_strdup(tline->text);
2248 def->plus = false;
2249 def->nolist = false;
Keith Kaniosb307a4f2010-11-06 17:41:51 -05002250// def->in_progress = 0;
2251// def->rep_nest = NULL;
Victor van den Elzenb916ede2008-07-23 15:14:22 +02002252 def->nparam_min = 0;
2253 def->nparam_max = 0;
2254
H. Peter Anvina26433d2008-07-16 14:40:01 -07002255 tline = expand_smacro(tline->next);
2256 skip_white_(tline);
2257 if (!tok_type_(tline, TOK_NUMBER)) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002258 error(ERR_NONFATAL, "`%s' expects a parameter count", directive);
H. Peter Anvina26433d2008-07-16 14:40:01 -07002259 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002260 def->nparam_min = def->nparam_max =
2261 readnum(tline->text, &err);
2262 if (err)
2263 error(ERR_NONFATAL,
2264 "unable to parse parameter count `%s'", tline->text);
H. Peter Anvina26433d2008-07-16 14:40:01 -07002265 }
2266 if (tline && tok_is_(tline->next, "-")) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002267 tline = tline->next->next;
2268 if (tok_is_(tline, "*")) {
2269 def->nparam_max = INT_MAX;
2270 } else if (!tok_type_(tline, TOK_NUMBER)) {
2271 error(ERR_NONFATAL,
2272 "`%s' expects a parameter count after `-'", directive);
2273 } else {
2274 def->nparam_max = readnum(tline->text, &err);
2275 if (err) {
2276 error(ERR_NONFATAL, "unable to parse parameter count `%s'",
2277 tline->text);
2278 }
2279 if (def->nparam_min > def->nparam_max) {
2280 error(ERR_NONFATAL, "minimum parameter count exceeds maximum");
2281 }
2282 }
H. Peter Anvina26433d2008-07-16 14:40:01 -07002283 }
2284 if (tline && tok_is_(tline->next, "+")) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002285 tline = tline->next;
2286 def->plus = true;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002287 }
2288 if (tline && tok_type_(tline->next, TOK_ID) &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002289 !nasm_stricmp(tline->next->text, ".nolist")) {
2290 tline = tline->next;
2291 def->nolist = true;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002292 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002293
H. Peter Anvina26433d2008-07-16 14:40:01 -07002294 /*
2295 * Handle default parameters.
2296 */
2297 if (tline && tline->next) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002298 def->dlist = tline->next;
2299 tline->next = NULL;
2300 count_mmac_params(def->dlist, &def->ndefs, &def->defaults);
H. Peter Anvina26433d2008-07-16 14:40:01 -07002301 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002302 def->dlist = NULL;
2303 def->defaults = NULL;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002304 }
Keith Kaniosb307a4f2010-11-06 17:41:51 -05002305 def->line = NULL;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002306
H. Peter Anvin89cee572009-07-15 09:16:54 -04002307 if (def->defaults && def->ndefs > def->nparam_max - def->nparam_min &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002308 !def->plus)
2309 error(ERR_WARNING|ERR_PASS1|ERR_WARN_MDP,
2310 "too many default macro parameters");
Victor van den Elzenb916ede2008-07-23 15:14:22 +02002311
H. Peter Anvina26433d2008-07-16 14:40:01 -07002312 return true;
2313}
2314
2315
2316/*
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002317 * Decode a size directive
2318 */
2319static int parse_size(const char *str) {
2320 static const char *size_names[] =
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002321 { "byte", "dword", "oword", "qword", "tword", "word", "yword" };
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002322 static const int sizes[] =
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002323 { 0, 1, 4, 16, 8, 10, 2, 32 };
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002324
Cyrill Gorcunova7319242010-06-03 22:04:36 +04002325 return sizes[bsii(str, size_names, ARRAY_SIZE(size_names))+1];
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002326}
2327
Ed Beroset3ab3f412002-06-11 03:31:49 +00002328/**
2329 * find and process preprocessor directive in passed line
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002330 * Find out if a line contains a preprocessor directive, and deal
2331 * with it if so.
H. Peter Anvin70653092007-10-19 14:42:29 -07002332 *
Ed Beroset3ab3f412002-06-11 03:31:49 +00002333 * If a directive _is_ found, it is the responsibility of this routine
2334 * (and not the caller) to free_tlist() the line.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002335 *
Ed Beroset3ab3f412002-06-11 03:31:49 +00002336 * @param tline a pointer to the current tokeninzed line linked list
2337 * @return DIRECTIVE_FOUND or NO_DIRECTIVE_FOUND
H. Peter Anvin70653092007-10-19 14:42:29 -07002338 *
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002339 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002340static int do_directive(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00002341{
H. Peter Anvin4169a472007-09-12 01:29:43 +00002342 enum preproc_token i;
2343 int j;
H. Peter Anvin70055962007-10-11 00:05:31 -07002344 bool err;
2345 int nparam;
2346 bool nolist;
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07002347 bool casesense;
H. Peter Anvin8cfdb9d2007-09-14 18:36:01 -07002348 int k, m;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002349 int offset;
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08002350 char *p, *pp;
2351 const char *mname;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002352 Include *inc;
2353 Context *ctx;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002354 Line *l;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002355 Token *t, *tt, *param_start, *macro_start, *last, **tptr, *origline;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002356 struct tokenval tokval;
2357 expr *evalresult;
Keith Kaniosb307a4f2010-11-06 17:41:51 -05002358 ExpDef *ed, *eed, **edhead;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002359 ExpInv *ei, *eei;
H. Peter Anvinf8ba53e2007-10-11 10:11:57 -07002360 int64_t count;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07002361 size_t len;
H. Peter Anvin8e3f75e2008-09-24 00:21:58 -07002362 int severity;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002363
2364 origline = tline;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002365
H. Peter Anvineba20a72002-04-30 20:53:55 +00002366 skip_white_(tline);
H. Peter Anvinf2936d72008-06-04 15:11:23 -07002367 if (!tline || !tok_type_(tline, TOK_PREPROC_ID) ||
H. Peter Anvine2c80182005-01-15 22:15:51 +00002368 (tline->text[1] == '%' || tline->text[1] == '$'
2369 || tline->text[1] == '!'))
2370 return NO_DIRECTIVE_FOUND;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002371
H. Peter Anvin4169a472007-09-12 01:29:43 +00002372 i = pp_token_hash(tline->text);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002373
H. Peter Anvin4169a472007-09-12 01:29:43 +00002374 switch (i) {
2375 case PP_INVALID:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002376 if (defining != NULL) return NO_DIRECTIVE_FOUND;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002377 error(ERR_NONFATAL, "unknown preprocessor directive `%s'",
2378 tline->text);
2379 return NO_DIRECTIVE_FOUND; /* didn't get it */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002380
H. Peter Anvine2c80182005-01-15 22:15:51 +00002381 case PP_STACKSIZE:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002382 if (defining != NULL) return NO_DIRECTIVE_FOUND;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002383 /* Directive to tell NASM what the default stack size is. The
2384 * default is for a 16-bit stack, and this can be overriden with
2385 * %stacksize large.
H. Peter Anvine2c80182005-01-15 22:15:51 +00002386 */
2387 tline = tline->next;
2388 if (tline && tline->type == TOK_WHITESPACE)
2389 tline = tline->next;
2390 if (!tline || tline->type != TOK_ID) {
2391 error(ERR_NONFATAL, "`%%stacksize' missing size parameter");
2392 free_tlist(origline);
2393 return DIRECTIVE_FOUND;
2394 }
2395 if (nasm_stricmp(tline->text, "flat") == 0) {
2396 /* All subsequent ARG directives are for a 32-bit stack */
2397 StackSize = 4;
2398 StackPointer = "ebp";
2399 ArgOffset = 8;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002400 LocalOffset = 0;
Charles Crayne7eaf9192007-11-08 22:11:14 -08002401 } else if (nasm_stricmp(tline->text, "flat64") == 0) {
2402 /* All subsequent ARG directives are for a 64-bit stack */
2403 StackSize = 8;
2404 StackPointer = "rbp";
Per Jessen53252e02010-02-11 00:16:59 +03002405 ArgOffset = 16;
Charles Crayne7eaf9192007-11-08 22:11:14 -08002406 LocalOffset = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002407 } else if (nasm_stricmp(tline->text, "large") == 0) {
2408 /* All subsequent ARG directives are for a 16-bit stack,
2409 * far function call.
2410 */
2411 StackSize = 2;
2412 StackPointer = "bp";
2413 ArgOffset = 4;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002414 LocalOffset = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002415 } else if (nasm_stricmp(tline->text, "small") == 0) {
2416 /* All subsequent ARG directives are for a 16-bit stack,
2417 * far function call. We don't support near functions.
2418 */
2419 StackSize = 2;
2420 StackPointer = "bp";
2421 ArgOffset = 6;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002422 LocalOffset = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002423 } else {
2424 error(ERR_NONFATAL, "`%%stacksize' invalid size type");
2425 free_tlist(origline);
2426 return DIRECTIVE_FOUND;
2427 }
2428 free_tlist(origline);
2429 return DIRECTIVE_FOUND;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002430
H. Peter Anvine2c80182005-01-15 22:15:51 +00002431 case PP_ARG:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002432 if (defining != NULL) return NO_DIRECTIVE_FOUND;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002433 /* TASM like ARG directive to define arguments to functions, in
2434 * the following form:
2435 *
2436 * ARG arg1:WORD, arg2:DWORD, arg4:QWORD
2437 */
2438 offset = ArgOffset;
2439 do {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00002440 char *arg, directive[256];
H. Peter Anvine2c80182005-01-15 22:15:51 +00002441 int size = StackSize;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002442
H. Peter Anvine2c80182005-01-15 22:15:51 +00002443 /* Find the argument name */
2444 tline = tline->next;
2445 if (tline && tline->type == TOK_WHITESPACE)
2446 tline = tline->next;
2447 if (!tline || tline->type != TOK_ID) {
2448 error(ERR_NONFATAL, "`%%arg' missing argument parameter");
2449 free_tlist(origline);
2450 return DIRECTIVE_FOUND;
2451 }
2452 arg = tline->text;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002453
H. Peter Anvine2c80182005-01-15 22:15:51 +00002454 /* Find the argument size type */
2455 tline = tline->next;
2456 if (!tline || tline->type != TOK_OTHER
2457 || tline->text[0] != ':') {
2458 error(ERR_NONFATAL,
2459 "Syntax error processing `%%arg' directive");
2460 free_tlist(origline);
2461 return DIRECTIVE_FOUND;
2462 }
2463 tline = tline->next;
2464 if (!tline || tline->type != TOK_ID) {
2465 error(ERR_NONFATAL, "`%%arg' missing size type parameter");
2466 free_tlist(origline);
2467 return DIRECTIVE_FOUND;
2468 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002469
H. Peter Anvine2c80182005-01-15 22:15:51 +00002470 /* Allow macro expansion of type parameter */
Keith Kaniosb7a89542007-04-12 02:40:54 +00002471 tt = tokenize(tline->text);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002472 tt = expand_smacro(tt);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002473 size = parse_size(tt->text);
2474 if (!size) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002475 error(ERR_NONFATAL,
2476 "Invalid size type for `%%arg' missing directive");
2477 free_tlist(tt);
2478 free_tlist(origline);
2479 return DIRECTIVE_FOUND;
2480 }
2481 free_tlist(tt);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002482
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002483 /* Round up to even stack slots */
2484 size = ALIGN(size, StackSize);
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002485
H. Peter Anvine2c80182005-01-15 22:15:51 +00002486 /* Now define the macro for the argument */
2487 snprintf(directive, sizeof(directive), "%%define %s (%s+%d)",
2488 arg, StackPointer, offset);
Keith Kaniosb7a89542007-04-12 02:40:54 +00002489 do_directive(tokenize(directive));
H. Peter Anvine2c80182005-01-15 22:15:51 +00002490 offset += size;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002491
H. Peter Anvine2c80182005-01-15 22:15:51 +00002492 /* Move to the next argument in the list */
2493 tline = tline->next;
2494 if (tline && tline->type == TOK_WHITESPACE)
2495 tline = tline->next;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002496 } while (tline && tline->type == TOK_OTHER && tline->text[0] == ',');
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002497 ArgOffset = offset;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002498 free_tlist(origline);
2499 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002500
H. Peter Anvine2c80182005-01-15 22:15:51 +00002501 case PP_LOCAL:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002502 if (defining != NULL) return NO_DIRECTIVE_FOUND;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002503 /* TASM like LOCAL directive to define local variables for a
2504 * function, in the following form:
2505 *
2506 * LOCAL local1:WORD, local2:DWORD, local4:QWORD = LocalSize
2507 *
2508 * The '= LocalSize' at the end is ignored by NASM, but is
2509 * required by TASM to define the local parameter size (and used
2510 * by the TASM macro package).
2511 */
2512 offset = LocalOffset;
2513 do {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00002514 char *local, directive[256];
H. Peter Anvine2c80182005-01-15 22:15:51 +00002515 int size = StackSize;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002516
H. Peter Anvine2c80182005-01-15 22:15:51 +00002517 /* Find the argument name */
2518 tline = tline->next;
2519 if (tline && tline->type == TOK_WHITESPACE)
2520 tline = tline->next;
2521 if (!tline || tline->type != TOK_ID) {
2522 error(ERR_NONFATAL,
2523 "`%%local' missing argument parameter");
2524 free_tlist(origline);
2525 return DIRECTIVE_FOUND;
2526 }
2527 local = tline->text;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002528
H. Peter Anvine2c80182005-01-15 22:15:51 +00002529 /* Find the argument size type */
2530 tline = tline->next;
2531 if (!tline || tline->type != TOK_OTHER
2532 || tline->text[0] != ':') {
2533 error(ERR_NONFATAL,
2534 "Syntax error processing `%%local' directive");
2535 free_tlist(origline);
2536 return DIRECTIVE_FOUND;
2537 }
2538 tline = tline->next;
2539 if (!tline || tline->type != TOK_ID) {
2540 error(ERR_NONFATAL,
2541 "`%%local' missing size type parameter");
2542 free_tlist(origline);
2543 return DIRECTIVE_FOUND;
2544 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00002545
H. Peter Anvine2c80182005-01-15 22:15:51 +00002546 /* Allow macro expansion of type parameter */
Keith Kaniosb7a89542007-04-12 02:40:54 +00002547 tt = tokenize(tline->text);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002548 tt = expand_smacro(tt);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002549 size = parse_size(tt->text);
2550 if (!size) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002551 error(ERR_NONFATAL,
2552 "Invalid size type for `%%local' missing directive");
2553 free_tlist(tt);
2554 free_tlist(origline);
2555 return DIRECTIVE_FOUND;
2556 }
2557 free_tlist(tt);
H. Peter Anvin734b1882002-04-30 21:01:08 +00002558
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002559 /* Round up to even stack slots */
2560 size = ALIGN(size, StackSize);
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002561
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002562 offset += size; /* Negative offset, increment before */
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002563
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002564 /* Now define the macro for the argument */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002565 snprintf(directive, sizeof(directive), "%%define %s (%s-%d)",
2566 local, StackPointer, offset);
Keith Kaniosb7a89542007-04-12 02:40:54 +00002567 do_directive(tokenize(directive));
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002568
H. Peter Anvine2c80182005-01-15 22:15:51 +00002569 /* Now define the assign to setup the enter_c macro correctly */
2570 snprintf(directive, sizeof(directive),
2571 "%%assign %%$localsize %%$localsize+%d", size);
Keith Kaniosb7a89542007-04-12 02:40:54 +00002572 do_directive(tokenize(directive));
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002573
H. Peter Anvine2c80182005-01-15 22:15:51 +00002574 /* Move to the next argument in the list */
2575 tline = tline->next;
2576 if (tline && tline->type == TOK_WHITESPACE)
2577 tline = tline->next;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002578 } while (tline && tline->type == TOK_OTHER && tline->text[0] == ',');
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002579 LocalOffset = offset;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002580 free_tlist(origline);
2581 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002582
H. Peter Anvine2c80182005-01-15 22:15:51 +00002583 case PP_CLEAR:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002584 if (defining != NULL) return NO_DIRECTIVE_FOUND;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002585 if (tline->next)
H. Peter Anvin917a3492008-09-24 09:14:49 -07002586 error(ERR_WARNING|ERR_PASS1,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002587 "trailing garbage after `%%clear' ignored");
2588 free_macros();
2589 init_macros();
H. Peter Anvine2c80182005-01-15 22:15:51 +00002590 free_tlist(origline);
2591 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002592
H. Peter Anvin418ca702008-05-30 10:42:30 -07002593 case PP_DEPEND:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002594 if (defining != NULL) return NO_DIRECTIVE_FOUND;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002595 t = tline->next = expand_smacro(tline->next);
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002596 skip_white_(t);
2597 if (!t || (t->type != TOK_STRING &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002598 t->type != TOK_INTERNAL_STRING)) {
H. Peter Anvin418ca702008-05-30 10:42:30 -07002599 error(ERR_NONFATAL, "`%%depend' expects a file name");
2600 free_tlist(origline);
2601 return DIRECTIVE_FOUND; /* but we did _something_ */
2602 }
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002603 if (t->next)
H. Peter Anvin917a3492008-09-24 09:14:49 -07002604 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvin418ca702008-05-30 10:42:30 -07002605 "trailing garbage after `%%depend' ignored");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002606 p = t->text;
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002607 if (t->type != TOK_INTERNAL_STRING)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002608 nasm_unquote_cstr(p, i);
2609 if (dephead && !in_list(*dephead, p)) {
2610 StrList *sl = nasm_malloc(strlen(p)+1+sizeof sl->next);
2611 sl->next = NULL;
2612 strcpy(sl->str, p);
2613 *deptail = sl;
2614 deptail = &sl->next;
2615 }
2616 free_tlist(origline);
H. Peter Anvin418ca702008-05-30 10:42:30 -07002617 return DIRECTIVE_FOUND;
2618
2619 case PP_INCLUDE:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002620 if (defining != NULL) return NO_DIRECTIVE_FOUND;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002621 t = tline->next = expand_smacro(tline->next);
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002622 skip_white_(t);
H. Peter Anvind2456592008-06-19 15:04:18 -07002623
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002624 if (!t || (t->type != TOK_STRING &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002625 t->type != TOK_INTERNAL_STRING)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002626 error(ERR_NONFATAL, "`%%include' expects a file name");
2627 free_tlist(origline);
2628 return DIRECTIVE_FOUND; /* but we did _something_ */
2629 }
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002630 if (t->next)
H. Peter Anvin917a3492008-09-24 09:14:49 -07002631 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +00002632 "trailing garbage after `%%include' ignored");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002633 p = t->text;
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002634 if (t->type != TOK_INTERNAL_STRING)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002635 nasm_unquote_cstr(p, i);
Cyrill Gorcunov55cc4d02010-11-10 23:12:06 +03002636 inc = nasm_zalloc(sizeof(Include));
H. Peter Anvine2c80182005-01-15 22:15:51 +00002637 inc->next = istk;
H. Peter Anvin2b1c3b92008-06-06 10:38:46 -07002638 inc->fp = inc_fopen(p, dephead, &deptail, pass == 0);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002639 if (!inc->fp) {
2640 /* -MG given but file not found */
2641 nasm_free(inc);
2642 } else {
2643 inc->fname = src_set_fname(nasm_strdup(p));
2644 inc->lineno = src_set_linnum(0);
2645 inc->lineinc = 1;
2646 inc->expansion = NULL;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002647 istk = inc;
2648 list->uplevel(LIST_INCLUDE);
2649 }
2650 free_tlist(origline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002651 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002652
H. Peter Anvind2456592008-06-19 15:04:18 -07002653 case PP_USE:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002654 if (defining != NULL) return NO_DIRECTIVE_FOUND;
H. Peter Anvinf4ae5ad2008-06-19 18:39:24 -07002655 {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002656 static macros_t *use_pkg;
2657 const char *pkg_macro = NULL;
H. Peter Anvinf4ae5ad2008-06-19 18:39:24 -07002658
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002659 tline = tline->next;
2660 skip_white_(tline);
2661 tline = expand_id(tline);
H. Peter Anvind2456592008-06-19 15:04:18 -07002662
H. Peter Anvin264b7b92008-10-24 16:38:17 -07002663 if (!tline || (tline->type != TOK_STRING &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002664 tline->type != TOK_INTERNAL_STRING &&
2665 tline->type != TOK_ID)) {
H. Peter Anvin926fc402008-06-19 16:26:12 -07002666 error(ERR_NONFATAL, "`%%use' expects a package name");
H. Peter Anvind2456592008-06-19 15:04:18 -07002667 free_tlist(origline);
2668 return DIRECTIVE_FOUND; /* but we did _something_ */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002669 }
H. Peter Anvin264b7b92008-10-24 16:38:17 -07002670 if (tline->next)
H. Peter Anvin917a3492008-09-24 09:14:49 -07002671 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvind2456592008-06-19 15:04:18 -07002672 "trailing garbage after `%%use' ignored");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002673 if (tline->type == TOK_STRING)
2674 nasm_unquote_cstr(tline->text, i);
2675 use_pkg = nasm_stdmac_find_package(tline->text);
2676 if (!use_pkg)
2677 error(ERR_NONFATAL, "unknown `%%use' package: %s", tline->text);
2678 else
2679 pkg_macro = (char *)use_pkg + 1; /* The first string will be <%define>__USE_*__ */
Victor van den Elzen35eb2ea2010-03-10 22:33:48 +01002680 if (use_pkg && ! smacro_defined(NULL, pkg_macro, 0, NULL, true)) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002681 /* Not already included, go ahead and include it */
2682 stdmacpos = use_pkg;
2683 }
2684 free_tlist(origline);
H. Peter Anvind2456592008-06-19 15:04:18 -07002685 return DIRECTIVE_FOUND;
H. Peter Anvinf4ae5ad2008-06-19 18:39:24 -07002686 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002687 case PP_PUSH:
H. Peter Anvine2c80182005-01-15 22:15:51 +00002688 case PP_REPL:
H. Peter Anvin42b56392008-10-24 16:24:21 -07002689 case PP_POP:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002690 if (defining != NULL) return NO_DIRECTIVE_FOUND;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002691 tline = tline->next;
2692 skip_white_(tline);
2693 tline = expand_id(tline);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002694 if (tline) {
2695 if (!tok_type_(tline, TOK_ID)) {
2696 error(ERR_NONFATAL, "`%s' expects a context identifier",
2697 pp_directives[i]);
2698 free_tlist(origline);
2699 return DIRECTIVE_FOUND; /* but we did _something_ */
2700 }
2701 if (tline->next)
2702 error(ERR_WARNING|ERR_PASS1,
2703 "trailing garbage after `%s' ignored",
2704 pp_directives[i]);
2705 p = nasm_strdup(tline->text);
2706 } else {
2707 p = NULL; /* Anonymous */
2708 }
H. Peter Anvin42b56392008-10-24 16:24:21 -07002709
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002710 if (i == PP_PUSH) {
Cyrill Gorcunov574fbf12010-11-11 13:44:51 +03002711 ctx = nasm_zalloc(sizeof(Context));
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002712 ctx->next = cstk;
2713 hash_init(&ctx->localmac, HASH_SMALL);
2714 ctx->name = p;
2715 ctx->number = unique++;
2716 cstk = ctx;
2717 } else {
2718 /* %pop or %repl */
2719 if (!cstk) {
2720 error(ERR_NONFATAL, "`%s': context stack is empty",
2721 pp_directives[i]);
2722 } else if (i == PP_POP) {
2723 if (p && (!cstk->name || nasm_stricmp(p, cstk->name)))
2724 error(ERR_NONFATAL, "`%%pop' in wrong context: %s, "
2725 "expected %s",
2726 cstk->name ? cstk->name : "anonymous", p);
2727 else
2728 ctx_pop();
2729 } else {
2730 /* i == PP_REPL */
2731 nasm_free(cstk->name);
2732 cstk->name = p;
2733 p = NULL;
2734 }
2735 nasm_free(p);
2736 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002737 free_tlist(origline);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002738 return DIRECTIVE_FOUND;
H. Peter Anvin8e3f75e2008-09-24 00:21:58 -07002739 case PP_FATAL:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002740 severity = ERR_FATAL;
2741 goto issue_error;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002742 case PP_ERROR:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002743 severity = ERR_NONFATAL;
2744 goto issue_error;
H. Peter Anvin7df04172008-06-10 18:27:38 -07002745 case PP_WARNING:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002746 severity = ERR_WARNING|ERR_WARN_USER;
2747 goto issue_error;
H. Peter Anvin8e3f75e2008-09-24 00:21:58 -07002748
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002749issue_error:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002750 if (defining != NULL) return NO_DIRECTIVE_FOUND;
H. Peter Anvin7df04172008-06-10 18:27:38 -07002751 {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002752 /* Only error out if this is the final pass */
2753 if (pass != 2 && i != PP_FATAL)
2754 return DIRECTIVE_FOUND;
2755
2756 tline->next = expand_smacro(tline->next);
2757 tline = tline->next;
2758 skip_white_(tline);
2759 t = tline ? tline->next : NULL;
2760 skip_white_(t);
2761 if (tok_type_(tline, TOK_STRING) && !t) {
2762 /* The line contains only a quoted string */
2763 p = tline->text;
2764 nasm_unquote(p, NULL); /* Ignore NUL character truncation */
2765 error(severity, "%s", p);
2766 } else {
2767 /* Not a quoted string, or more than a quoted string */
2768 p = detoken(tline, false);
2769 error(severity, "%s", p);
2770 nasm_free(p);
2771 }
2772 free_tlist(origline);
2773 return DIRECTIVE_FOUND;
H. Peter Anvin7df04172008-06-10 18:27:38 -07002774 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00002775
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002776 CASE_PP_IF:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002777 if (defining != NULL) {
2778 if (defining->type == EXP_IF) {
2779 defining->def_depth ++;
2780 }
2781 return NO_DIRECTIVE_FOUND;
2782 }
2783 if ((istk->expansion != NULL) &&
2784 (istk->expansion->emitting == false)) {
2785 j = COND_NEVER;
2786 } else {
2787 j = if_condition(tline->next, i);
2788 tline->next = NULL; /* it got freed */
2789 j = (((j < 0) ? COND_NEVER : j) ? COND_IF_TRUE : COND_IF_FALSE);
2790 }
2791 ed = new_ExpDef(EXP_IF);
2792 ed->state = j;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002793 ed->ignoring = ((ed->state == COND_IF_TRUE) ? false : true);
2794 ed->prev = defining;
2795 defining = ed;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002796 free_tlist(origline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002797 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002798
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002799 CASE_PP_ELIF:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002800 if (defining != NULL) {
2801 if ((defining->type != EXP_IF) || (defining->def_depth > 0)) {
2802 return NO_DIRECTIVE_FOUND;
2803 }
2804 }
Cyrill Gorcunov82667ff2011-06-25 19:34:19 +04002805 if ((defining == NULL) || (defining->type != EXP_IF)) {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002806 error(ERR_FATAL, "`%s': no matching `%%if'", pp_directives[i]);
2807 }
Keith Kaniosb307a4f2010-11-06 17:41:51 -05002808 switch (defining->state) {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002809 case COND_IF_TRUE:
2810 defining->state = COND_DONE;
2811 defining->ignoring = true;
2812 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002813
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002814 case COND_DONE:
2815 case COND_NEVER:
2816 defining->ignoring = true;
2817 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002818
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002819 case COND_ELSE_TRUE:
2820 case COND_ELSE_FALSE:
2821 error_precond(ERR_WARNING|ERR_PASS1,
2822 "`%%elif' after `%%else' ignored");
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002823 defining->state = COND_NEVER;
2824 defining->ignoring = true;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002825 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002826
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002827 case COND_IF_FALSE:
2828 /*
2829 * IMPORTANT: In the case of %if, we will already have
2830 * called expand_mmac_params(); however, if we're
2831 * processing an %elif we must have been in a
2832 * non-emitting mode, which would have inhibited
2833 * the normal invocation of expand_mmac_params().
2834 * Therefore, we have to do it explicitly here.
2835 */
2836 j = if_condition(expand_mmac_params(tline->next), i);
2837 tline->next = NULL; /* it got freed */
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002838 defining->state =
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002839 j < 0 ? COND_NEVER : j ? COND_IF_TRUE : COND_IF_FALSE;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002840 defining->ignoring = ((defining->state == COND_IF_TRUE) ? false : true);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002841 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002842 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002843 free_tlist(origline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002844 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002845
H. Peter Anvine2c80182005-01-15 22:15:51 +00002846 case PP_ELSE:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002847 if (defining != NULL) {
2848 if ((defining->type != EXP_IF) || (defining->def_depth > 0)) {
2849 return NO_DIRECTIVE_FOUND;
2850 }
2851 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002852 if (tline->next)
H. Peter Anvin917a3492008-09-24 09:14:49 -07002853 error_precond(ERR_WARNING|ERR_PASS1,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002854 "trailing garbage after `%%else' ignored");
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002855 if ((defining == NULL) || (defining->type != EXP_IF)) {
2856 error(ERR_FATAL, "`%s': no matching `%%if'", pp_directives[i]);
2857 }
Keith Kaniosb307a4f2010-11-06 17:41:51 -05002858 switch (defining->state) {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002859 case COND_IF_TRUE:
2860 case COND_DONE:
2861 defining->state = COND_ELSE_FALSE;
2862 defining->ignoring = true;
2863 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002864
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002865 case COND_NEVER:
2866 defining->ignoring = true;
2867 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002868
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002869 case COND_IF_FALSE:
2870 defining->state = COND_ELSE_TRUE;
2871 defining->ignoring = false;
2872 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002873
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002874 case COND_ELSE_TRUE:
2875 case COND_ELSE_FALSE:
2876 error_precond(ERR_WARNING|ERR_PASS1,
2877 "`%%else' after `%%else' ignored.");
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002878 defining->state = COND_NEVER;
2879 defining->ignoring = true;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002880 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002881 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002882 free_tlist(origline);
2883 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002884
H. Peter Anvine2c80182005-01-15 22:15:51 +00002885 case PP_ENDIF:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002886 if (defining != NULL) {
2887 if (defining->type == EXP_IF) {
2888 if (defining->def_depth > 0) {
2889 defining->def_depth --;
2890 return NO_DIRECTIVE_FOUND;
2891 }
2892 } else {
2893 return NO_DIRECTIVE_FOUND;
2894 }
2895 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002896 if (tline->next)
H. Peter Anvin917a3492008-09-24 09:14:49 -07002897 error_precond(ERR_WARNING|ERR_PASS1,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002898 "trailing garbage after `%%endif' ignored");
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002899 if ((defining == NULL) || (defining->type != EXP_IF)) {
2900 error(ERR_NONFATAL, "`%%endif': no matching `%%if'");
2901 return DIRECTIVE_FOUND;
2902 }
2903 ed = defining;
2904 defining = ed->prev;
2905 ed->prev = expansions;
2906 expansions = ed;
2907 ei = new_ExpInv(EXP_IF, ed);
2908 ei->current = ed->line;
2909 ei->emitting = true;
2910 ei->prev = istk->expansion;
2911 istk->expansion = ei;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002912 free_tlist(origline);
2913 return DIRECTIVE_FOUND;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002914
H. Peter Anvindb8f96e2009-07-15 09:07:29 -04002915 case PP_RMACRO:
2916 case PP_IRMACRO:
H. Peter Anvine2c80182005-01-15 22:15:51 +00002917 case PP_MACRO:
2918 case PP_IMACRO:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002919 if (defining != NULL) {
2920 if (defining->type == EXP_MMACRO) {
2921 defining->def_depth ++;
2922 }
2923 return NO_DIRECTIVE_FOUND;
2924 }
2925 ed = new_ExpDef(EXP_MMACRO);
2926 ed->max_depth =
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002927 (i == PP_RMACRO) || (i == PP_IRMACRO) ? DEADMAN_LIMIT : 0;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002928 ed->casesense = (i == PP_MACRO) || (i == PP_RMACRO);
Keith Kaniosb307a4f2010-11-06 17:41:51 -05002929 if (!parse_mmacro_spec(tline, ed, pp_directives[i])) {
2930 nasm_free(ed);
2931 ed = NULL;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002932 return DIRECTIVE_FOUND;
2933 }
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002934 ed->def_depth = 0;
2935 ed->cur_depth = 0;
2936 ed->max_depth = (ed->max_depth + 1);
2937 ed->ignoring = false;
2938 ed->prev = defining;
2939 defining = ed;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002940
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002941 eed = (ExpDef *) hash_findix(&expdefs, ed->name);
2942 while (eed) {
Cyrill Gorcunov574fbf12010-11-11 13:44:51 +03002943 if (!strcmp(eed->name, ed->name) &&
2944 (eed->nparam_min <= ed->nparam_max || ed->plus) &&
2945 (ed->nparam_min <= eed->nparam_max || eed->plus)) {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002946 error(ERR_WARNING|ERR_PASS1,
2947 "redefining multi-line macro `%s'", ed->name);
2948 return DIRECTIVE_FOUND;
2949 }
2950 eed = eed->next;
2951 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002952 free_tlist(origline);
2953 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002954
H. Peter Anvine2c80182005-01-15 22:15:51 +00002955 case PP_ENDM:
2956 case PP_ENDMACRO:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002957 if (defining != NULL) {
2958 if (defining->type == EXP_MMACRO) {
2959 if (defining->def_depth > 0) {
2960 defining->def_depth --;
2961 return NO_DIRECTIVE_FOUND;
2962 }
2963 } else {
2964 return NO_DIRECTIVE_FOUND;
2965 }
2966 }
2967 if (!(defining) || (defining->type != EXP_MMACRO)) {
2968 error(ERR_NONFATAL, "`%s': not defining a macro", tline->text);
2969 return DIRECTIVE_FOUND;
2970 }
2971 edhead = (ExpDef **) hash_findi_add(&expdefs, defining->name);
2972 defining->next = *edhead;
2973 *edhead = defining;
2974 ed = defining;
2975 defining = ed->prev;
2976 ed->prev = expansions;
2977 expansions = ed;
2978 ed = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002979 free_tlist(origline);
2980 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002981
H. Peter Anvin89cee572009-07-15 09:16:54 -04002982 case PP_EXITMACRO:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002983 if (defining != NULL) return NO_DIRECTIVE_FOUND;
2984 /*
2985 * We must search along istk->expansion until we hit a
2986 * macro invocation. Then we disable the emitting state(s)
2987 * between exitmacro and endmacro.
2988 */
2989 for (ei = istk->expansion; ei != NULL; ei = ei->prev) {
2990 if(ei->type == EXP_MMACRO) {
2991 break;
2992 }
2993 }
2994
2995 if (ei != NULL) {
2996 /*
2997 * Set all invocations leading back to the macro
2998 * invocation to a non-emitting state.
2999 */
3000 for (eei = istk->expansion; eei != ei; eei = eei->prev) {
3001 eei->emitting = false;
3002 }
3003 eei->emitting = false;
3004 } else {
3005 error(ERR_NONFATAL, "`%%exitmacro' not within `%%macro' block");
3006 }
Keith Kanios852f1ee2009-07-12 00:19:55 -05003007 free_tlist(origline);
3008 return DIRECTIVE_FOUND;
3009
H. Peter Anvina26433d2008-07-16 14:40:01 -07003010 case PP_UNMACRO:
3011 case PP_UNIMACRO:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003012 if (defining != NULL) return NO_DIRECTIVE_FOUND;
H. Peter Anvina26433d2008-07-16 14:40:01 -07003013 {
Keith Kaniosb307a4f2010-11-06 17:41:51 -05003014 ExpDef **ed_p;
3015 ExpDef spec;
H. Peter Anvina26433d2008-07-16 14:40:01 -07003016
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003017 spec.casesense = (i == PP_UNMACRO);
3018 if (!parse_mmacro_spec(tline, &spec, pp_directives[i])) {
3019 return DIRECTIVE_FOUND;
3020 }
Keith Kaniosb307a4f2010-11-06 17:41:51 -05003021 ed_p = (ExpDef **) hash_findi(&expdefs, spec.name, NULL);
3022 while (ed_p && *ed_p) {
3023 ed = *ed_p;
3024 if (ed->casesense == spec.casesense &&
3025 !mstrcmp(ed->name, spec.name, spec.casesense) &&
3026 ed->nparam_min == spec.nparam_min &&
3027 ed->nparam_max == spec.nparam_max &&
3028 ed->plus == spec.plus) {
Keith Kaniosc98a5b42010-12-18 12:17:31 -06003029 if (ed->cur_depth > 0) {
Keith Kanios21d885b2010-12-18 12:22:21 -06003030 error(ERR_NONFATAL, "`%s' ignored on active macro",
Keith Kaniosc98a5b42010-12-18 12:17:31 -06003031 pp_directives[i]);
3032 break;
3033 } else {
3034 *ed_p = ed->next;
3035 free_expdef(ed);
3036 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003037 } else {
Keith Kaniosb307a4f2010-11-06 17:41:51 -05003038 ed_p = &ed->next;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003039 }
3040 }
3041 free_tlist(origline);
3042 free_tlist(spec.dlist);
3043 return DIRECTIVE_FOUND;
H. Peter Anvina26433d2008-07-16 14:40:01 -07003044 }
3045
H. Peter Anvine2c80182005-01-15 22:15:51 +00003046 case PP_ROTATE:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003047 if (defining != NULL) return NO_DIRECTIVE_FOUND;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003048 if (tline->next && tline->next->type == TOK_WHITESPACE)
3049 tline = tline->next;
H. Peter Anvin89cee572009-07-15 09:16:54 -04003050 if (!tline->next) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00003051 free_tlist(origline);
3052 error(ERR_NONFATAL, "`%%rotate' missing rotate count");
3053 return DIRECTIVE_FOUND;
3054 }
3055 t = expand_smacro(tline->next);
3056 tline->next = NULL;
3057 free_tlist(origline);
3058 tline = t;
3059 tptr = &t;
3060 tokval.t_type = TOKEN_INVALID;
3061 evalresult =
3062 evaluate(ppscan, tptr, &tokval, NULL, pass, error, NULL);
3063 free_tlist(tline);
3064 if (!evalresult)
3065 return DIRECTIVE_FOUND;
3066 if (tokval.t_type)
H. Peter Anvin917a3492008-09-24 09:14:49 -07003067 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +00003068 "trailing garbage after expression ignored");
3069 if (!is_simple(evalresult)) {
3070 error(ERR_NONFATAL, "non-constant value given to `%%rotate'");
3071 return DIRECTIVE_FOUND;
3072 }
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003073 for (ei = istk->expansion; ei != NULL; ei = ei->prev) {
3074 if (ei->type == EXP_MMACRO) {
3075 break;
3076 }
3077 }
3078 if (ei == NULL) {
3079 error(ERR_NONFATAL, "`%%rotate' invoked outside a macro call");
3080 } else if (ei->nparam == 0) {
3081 error(ERR_NONFATAL,
3082 "`%%rotate' invoked within macro without parameters");
3083 } else {
3084 int rotate = ei->rotate + reloc_value(evalresult);
3085
3086 rotate %= (int)ei->nparam;
3087 if (rotate < 0)
3088 rotate += ei->nparam;
3089 ei->rotate = rotate;
3090 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00003091 return DIRECTIVE_FOUND;
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00003092
H. Peter Anvine2c80182005-01-15 22:15:51 +00003093 case PP_REP:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003094 if (defining != NULL) {
3095 if (defining->type == EXP_REP) {
3096 defining->def_depth ++;
3097 }
3098 return NO_DIRECTIVE_FOUND;
3099 }
H. Peter Anvin6867acc2007-10-10 14:58:45 -07003100 nolist = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003101 do {
3102 tline = tline->next;
3103 } while (tok_type_(tline, TOK_WHITESPACE));
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00003104
H. Peter Anvine2c80182005-01-15 22:15:51 +00003105 if (tok_type_(tline, TOK_ID) &&
3106 nasm_stricmp(tline->text, ".nolist") == 0) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07003107 nolist = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003108 do {
3109 tline = tline->next;
3110 } while (tok_type_(tline, TOK_WHITESPACE));
3111 }
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00003112
H. Peter Anvine2c80182005-01-15 22:15:51 +00003113 if (tline) {
3114 t = expand_smacro(tline);
3115 tptr = &t;
3116 tokval.t_type = TOKEN_INVALID;
3117 evalresult =
3118 evaluate(ppscan, tptr, &tokval, NULL, pass, error, NULL);
3119 if (!evalresult) {
3120 free_tlist(origline);
3121 return DIRECTIVE_FOUND;
3122 }
3123 if (tokval.t_type)
H. Peter Anvin917a3492008-09-24 09:14:49 -07003124 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +00003125 "trailing garbage after expression ignored");
3126 if (!is_simple(evalresult)) {
3127 error(ERR_NONFATAL, "non-constant value given to `%%rep'");
3128 return DIRECTIVE_FOUND;
3129 }
Cyrill Gorcunove091d6e2010-08-09 13:58:22 +04003130 count = reloc_value(evalresult);
3131 if (count >= REP_LIMIT) {
Cyrill Gorcunov71f4f842010-08-09 20:17:17 +04003132 error(ERR_NONFATAL, "`%%rep' value exceeds limit");
Cyrill Gorcunove091d6e2010-08-09 13:58:22 +04003133 count = 0;
3134 } else
3135 count++;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003136 } else {
3137 error(ERR_NONFATAL, "`%%rep' expects a repeat count");
H. Peter Anvinf8ba53e2007-10-11 10:11:57 -07003138 count = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003139 }
3140 free_tlist(origline);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003141 ed = new_ExpDef(EXP_REP);
3142 ed->nolist = nolist;
3143 ed->def_depth = 0;
3144 ed->cur_depth = 1;
3145 ed->max_depth = (count - 1);
3146 ed->ignoring = false;
3147 ed->prev = defining;
3148 defining = ed;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003149 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003150
H. Peter Anvine2c80182005-01-15 22:15:51 +00003151 case PP_ENDREP:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003152 if (defining != NULL) {
3153 if (defining->type == EXP_REP) {
3154 if (defining->def_depth > 0) {
3155 defining->def_depth --;
3156 return NO_DIRECTIVE_FOUND;
3157 }
3158 } else {
3159 return NO_DIRECTIVE_FOUND;
3160 }
3161 }
Keith Kaniosb307a4f2010-11-06 17:41:51 -05003162 if ((defining == NULL) || (defining->type != EXP_REP)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00003163 error(ERR_NONFATAL, "`%%endrep': no matching `%%rep'");
3164 return DIRECTIVE_FOUND;
3165 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003166
H. Peter Anvine2c80182005-01-15 22:15:51 +00003167 /*
3168 * Now we have a "macro" defined - although it has no name
3169 * and we won't be entering it in the hash tables - we must
3170 * push a macro-end marker for it on to istk->expansion.
3171 * After that, it will take care of propagating itself (a
3172 * macro-end marker line for a macro which is really a %rep
3173 * block will cause the macro to be re-expanded, complete
3174 * with another macro-end marker to ensure the process
3175 * continues) until the whole expansion is forcibly removed
3176 * from istk->expansion by a %exitrep.
3177 */
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003178 ed = defining;
3179 defining = ed->prev;
3180 ed->prev = expansions;
3181 expansions = ed;
3182 ei = new_ExpInv(EXP_REP, ed);
3183 ei->current = ed->line;
3184 ei->emitting = ((ed->max_depth > 0) ? true : false);
3185 list->uplevel(ed->nolist ? LIST_MACRO_NOLIST : LIST_MACRO);
3186 ei->prev = istk->expansion;
3187 istk->expansion = ei;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003188 free_tlist(origline);
3189 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003190
H. Peter Anvine2c80182005-01-15 22:15:51 +00003191 case PP_EXITREP:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003192 if (defining != NULL) return NO_DIRECTIVE_FOUND;
3193 /*
3194 * We must search along istk->expansion until we hit a
3195 * rep invocation. Then we disable the emitting state(s)
3196 * between exitrep and endrep.
3197 */
3198 for (ei = istk->expansion; ei != NULL; ei = ei->prev) {
3199 if (ei->type == EXP_REP) {
3200 break;
3201 }
3202 }
3203
3204 if (ei != NULL) {
3205 /*
3206 * Set all invocations leading back to the rep
3207 * invocation to a non-emitting state.
3208 */
3209 for (eei = istk->expansion; eei != ei; eei = eei->prev) {
3210 eei->emitting = false;
3211 }
3212 eei->emitting = false;
3213 eei->current = NULL;
3214 eei->def->cur_depth = eei->def->max_depth;
3215 } else {
3216 error(ERR_NONFATAL, "`%%exitrep' not within `%%rep' block");
3217 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00003218 free_tlist(origline);
3219 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003220
H. Peter Anvine2c80182005-01-15 22:15:51 +00003221 case PP_XDEFINE:
3222 case PP_IXDEFINE:
3223 case PP_DEFINE:
3224 case PP_IDEFINE:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003225 if (defining != NULL) return NO_DIRECTIVE_FOUND;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003226 casesense = (i == PP_DEFINE || i == PP_XDEFINE);
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07003227
H. Peter Anvine2c80182005-01-15 22:15:51 +00003228 tline = tline->next;
3229 skip_white_(tline);
3230 tline = expand_id(tline);
3231 if (!tline || (tline->type != TOK_ID &&
3232 (tline->type != TOK_PREPROC_ID ||
3233 tline->text[1] != '$'))) {
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07003234 error(ERR_NONFATAL, "`%s' expects a macro identifier",
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003235 pp_directives[i]);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003236 free_tlist(origline);
3237 return DIRECTIVE_FOUND;
3238 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003239
Cyrill Gorcunov290eac72011-06-28 01:59:05 +04003240 ctx = get_ctx(tline->text, &mname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003241 last = tline;
3242 param_start = tline = tline->next;
3243 nparam = 0;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003244
H. Peter Anvine2c80182005-01-15 22:15:51 +00003245 /* Expand the macro definition now for %xdefine and %ixdefine */
3246 if ((i == PP_XDEFINE) || (i == PP_IXDEFINE))
3247 tline = expand_smacro(tline);
H. Peter Anvin734b1882002-04-30 21:01:08 +00003248
H. Peter Anvine2c80182005-01-15 22:15:51 +00003249 if (tok_is_(tline, "(")) {
3250 /*
3251 * This macro has parameters.
3252 */
H. Peter Anvin734b1882002-04-30 21:01:08 +00003253
H. Peter Anvine2c80182005-01-15 22:15:51 +00003254 tline = tline->next;
3255 while (1) {
3256 skip_white_(tline);
3257 if (!tline) {
3258 error(ERR_NONFATAL, "parameter identifier expected");
3259 free_tlist(origline);
3260 return DIRECTIVE_FOUND;
3261 }
3262 if (tline->type != TOK_ID) {
3263 error(ERR_NONFATAL,
3264 "`%s': parameter identifier expected",
3265 tline->text);
3266 free_tlist(origline);
3267 return DIRECTIVE_FOUND;
3268 }
Cyrill Gorcunov194ba892011-06-30 01:16:35 +04003269
3270 smacro_set_param_idx(tline, nparam);
3271 nparam++;
3272
H. Peter Anvine2c80182005-01-15 22:15:51 +00003273 tline = tline->next;
3274 skip_white_(tline);
3275 if (tok_is_(tline, ",")) {
3276 tline = tline->next;
H. Peter Anvinca348b62008-07-23 10:49:26 -04003277 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003278 if (!tok_is_(tline, ")")) {
3279 error(ERR_NONFATAL,
3280 "`)' expected to terminate macro template");
3281 free_tlist(origline);
3282 return DIRECTIVE_FOUND;
3283 }
3284 break;
3285 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00003286 }
3287 last = tline;
3288 tline = tline->next;
3289 }
3290 if (tok_type_(tline, TOK_WHITESPACE))
3291 last = tline, tline = tline->next;
3292 macro_start = NULL;
3293 last->next = NULL;
3294 t = tline;
3295 while (t) {
3296 if (t->type == TOK_ID) {
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04003297 list_for_each(tt, param_start)
Cyrill Gorcunov194ba892011-06-30 01:16:35 +04003298 if (is_smacro_param(tt) &&
H. Peter Anvine2c80182005-01-15 22:15:51 +00003299 !strcmp(tt->text, t->text))
3300 t->type = tt->type;
3301 }
3302 tt = t->next;
3303 t->next = macro_start;
3304 macro_start = t;
3305 t = tt;
3306 }
3307 /*
3308 * Good. We now have a macro name, a parameter count, and a
3309 * token list (in reverse order) for an expansion. We ought
3310 * to be OK just to create an SMacro, store it, and let
3311 * free_tlist have the rest of the line (which we have
3312 * carefully re-terminated after chopping off the expansion
3313 * from the end).
3314 */
H. Peter Anvin4db5a162007-10-11 13:42:09 -07003315 define_smacro(ctx, mname, casesense, nparam, macro_start);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003316 free_tlist(origline);
3317 return DIRECTIVE_FOUND;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003318
H. Peter Anvine2c80182005-01-15 22:15:51 +00003319 case PP_UNDEF:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003320 if (defining != NULL) return NO_DIRECTIVE_FOUND;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003321 tline = tline->next;
3322 skip_white_(tline);
3323 tline = expand_id(tline);
3324 if (!tline || (tline->type != TOK_ID &&
3325 (tline->type != TOK_PREPROC_ID ||
3326 tline->text[1] != '$'))) {
3327 error(ERR_NONFATAL, "`%%undef' expects a macro identifier");
3328 free_tlist(origline);
3329 return DIRECTIVE_FOUND;
3330 }
3331 if (tline->next) {
H. Peter Anvin917a3492008-09-24 09:14:49 -07003332 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +00003333 "trailing garbage after macro name ignored");
3334 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003335
H. Peter Anvine2c80182005-01-15 22:15:51 +00003336 /* Find the context that symbol belongs to */
Cyrill Gorcunov290eac72011-06-28 01:59:05 +04003337 ctx = get_ctx(tline->text, &mname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003338 undef_smacro(ctx, mname);
3339 free_tlist(origline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003340 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003341
H. Peter Anvin9e200162008-06-04 17:23:14 -07003342 case PP_DEFSTR:
3343 case PP_IDEFSTR:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003344 if (defining != NULL) return NO_DIRECTIVE_FOUND;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003345 casesense = (i == PP_DEFSTR);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003346
3347 tline = tline->next;
3348 skip_white_(tline);
3349 tline = expand_id(tline);
3350 if (!tline || (tline->type != TOK_ID &&
3351 (tline->type != TOK_PREPROC_ID ||
3352 tline->text[1] != '$'))) {
3353 error(ERR_NONFATAL, "`%s' expects a macro identifier",
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003354 pp_directives[i]);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003355 free_tlist(origline);
3356 return DIRECTIVE_FOUND;
3357 }
3358
Cyrill Gorcunov290eac72011-06-28 01:59:05 +04003359 ctx = get_ctx(tline->text, &mname);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003360 last = tline;
3361 tline = expand_smacro(tline->next);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003362 last->next = NULL;
H. Peter Anvin9e200162008-06-04 17:23:14 -07003363
3364 while (tok_type_(tline, TOK_WHITESPACE))
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003365 tline = delete_Token(tline);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003366
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003367 p = detoken(tline, false);
Cyrill Gorcunov6b271292011-02-28 08:45:52 +03003368 macro_start = nasm_zalloc(sizeof(*macro_start));
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003369 macro_start->text = nasm_quote(p, strlen(p));
3370 macro_start->type = TOK_STRING;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003371 nasm_free(p);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003372
3373 /*
3374 * We now have a macro name, an implicit parameter count of
3375 * zero, and a string token to use as an expansion. Create
3376 * and store an SMacro.
3377 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003378 define_smacro(ctx, mname, casesense, 0, macro_start);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003379 free_tlist(origline);
3380 return DIRECTIVE_FOUND;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003381
H. Peter Anvin2f55bda2009-07-14 15:04:04 -04003382 case PP_DEFTOK:
3383 case PP_IDEFTOK:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003384 if (defining != NULL) return NO_DIRECTIVE_FOUND;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003385 casesense = (i == PP_DEFTOK);
3386
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003387 tline = tline->next;
3388 skip_white_(tline);
3389 tline = expand_id(tline);
3390 if (!tline || (tline->type != TOK_ID &&
3391 (tline->type != TOK_PREPROC_ID ||
3392 tline->text[1] != '$'))) {
3393 error(ERR_NONFATAL,
3394 "`%s' expects a macro identifier as first parameter",
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003395 pp_directives[i]);
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003396 free_tlist(origline);
3397 return DIRECTIVE_FOUND;
3398 }
Cyrill Gorcunov290eac72011-06-28 01:59:05 +04003399 ctx = get_ctx(tline->text, &mname);
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003400 last = tline;
3401 tline = expand_smacro(tline->next);
3402 last->next = NULL;
3403
3404 t = tline;
3405 while (tok_type_(t, TOK_WHITESPACE))
3406 t = t->next;
3407 /* t should now point to the string */
Cyrill Gorcunov6908e582010-09-06 19:36:15 +04003408 if (!tok_type_(t, TOK_STRING)) {
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003409 error(ERR_NONFATAL,
3410 "`%s` requires string as second parameter",
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003411 pp_directives[i]);
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003412 free_tlist(tline);
3413 free_tlist(origline);
3414 return DIRECTIVE_FOUND;
3415 }
3416
Keith Kaniosb307a4f2010-11-06 17:41:51 -05003417 /*
3418 * Convert the string to a token stream. Note that smacros
3419 * are stored with the token stream reversed, so we have to
3420 * reverse the output of tokenize().
3421 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003422 nasm_unquote_cstr(t->text, i);
H. Peter Anvinb40992c2010-09-15 08:57:21 -07003423 macro_start = reverse_tokens(tokenize(t->text));
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003424
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003425 /*
3426 * We now have a macro name, an implicit parameter count of
3427 * zero, and a numeric token to use as an expansion. Create
3428 * and store an SMacro.
3429 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003430 define_smacro(ctx, mname, casesense, 0, macro_start);
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003431 free_tlist(tline);
3432 free_tlist(origline);
3433 return DIRECTIVE_FOUND;
H. Peter Anvin9e200162008-06-04 17:23:14 -07003434
H. Peter Anvin418ca702008-05-30 10:42:30 -07003435 case PP_PATHSEARCH:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003436 if (defining != NULL) return NO_DIRECTIVE_FOUND;
H. Peter Anvin418ca702008-05-30 10:42:30 -07003437 {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003438 FILE *fp;
3439 StrList *xsl = NULL;
3440 StrList **xst = &xsl;
H. Peter Anvin418ca702008-05-30 10:42:30 -07003441
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003442 casesense = true;
H. Peter Anvin418ca702008-05-30 10:42:30 -07003443
3444 tline = tline->next;
3445 skip_white_(tline);
3446 tline = expand_id(tline);
3447 if (!tline || (tline->type != TOK_ID &&
3448 (tline->type != TOK_PREPROC_ID ||
3449 tline->text[1] != '$'))) {
3450 error(ERR_NONFATAL,
3451 "`%%pathsearch' expects a macro identifier as first parameter");
3452 free_tlist(origline);
3453 return DIRECTIVE_FOUND;
3454 }
Cyrill Gorcunov290eac72011-06-28 01:59:05 +04003455 ctx = get_ctx(tline->text, &mname);
H. Peter Anvin418ca702008-05-30 10:42:30 -07003456 last = tline;
3457 tline = expand_smacro(tline->next);
3458 last->next = NULL;
3459
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003460 t = tline;
H. Peter Anvin418ca702008-05-30 10:42:30 -07003461 while (tok_type_(t, TOK_WHITESPACE))
3462 t = t->next;
3463
3464 if (!t || (t->type != TOK_STRING &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003465 t->type != TOK_INTERNAL_STRING)) {
H. Peter Anvin418ca702008-05-30 10:42:30 -07003466 error(ERR_NONFATAL, "`%%pathsearch' expects a file name");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003467 free_tlist(tline);
H. Peter Anvin418ca702008-05-30 10:42:30 -07003468 free_tlist(origline);
3469 return DIRECTIVE_FOUND; /* but we did _something_ */
3470 }
3471 if (t->next)
H. Peter Anvin917a3492008-09-24 09:14:49 -07003472 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvin418ca702008-05-30 10:42:30 -07003473 "trailing garbage after `%%pathsearch' ignored");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003474 p = t->text;
H. Peter Anvin427cc912008-06-01 21:43:03 -07003475 if (t->type != TOK_INTERNAL_STRING)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003476 nasm_unquote(p, NULL);
H. Peter Anvin418ca702008-05-30 10:42:30 -07003477
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003478 fp = inc_fopen(p, &xsl, &xst, true);
3479 if (fp) {
3480 p = xsl->str;
3481 fclose(fp); /* Don't actually care about the file */
3482 }
Cyrill Gorcunov6b271292011-02-28 08:45:52 +03003483 macro_start = nasm_zalloc(sizeof(*macro_start));
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003484 macro_start->text = nasm_quote(p, strlen(p));
3485 macro_start->type = TOK_STRING;
Cyrill Gorcunovb6c6ca92011-06-28 01:33:02 +04003486 nasm_free(xsl);
H. Peter Anvin418ca702008-05-30 10:42:30 -07003487
3488 /*
3489 * We now have a macro name, an implicit parameter count of
3490 * zero, and a string token to use as an expansion. Create
3491 * and store an SMacro.
3492 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003493 define_smacro(ctx, mname, casesense, 0, macro_start);
3494 free_tlist(tline);
H. Peter Anvin418ca702008-05-30 10:42:30 -07003495 free_tlist(origline);
3496 return DIRECTIVE_FOUND;
3497 }
3498
H. Peter Anvine2c80182005-01-15 22:15:51 +00003499 case PP_STRLEN:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003500 if (defining != NULL) return NO_DIRECTIVE_FOUND;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003501 casesense = true;
H. Peter Anvin70653092007-10-19 14:42:29 -07003502
H. Peter Anvine2c80182005-01-15 22:15:51 +00003503 tline = tline->next;
3504 skip_white_(tline);
3505 tline = expand_id(tline);
3506 if (!tline || (tline->type != TOK_ID &&
3507 (tline->type != TOK_PREPROC_ID ||
3508 tline->text[1] != '$'))) {
3509 error(ERR_NONFATAL,
3510 "`%%strlen' expects a macro identifier as first parameter");
3511 free_tlist(origline);
3512 return DIRECTIVE_FOUND;
3513 }
Cyrill Gorcunov290eac72011-06-28 01:59:05 +04003514 ctx = get_ctx(tline->text, &mname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003515 last = tline;
3516 tline = expand_smacro(tline->next);
3517 last->next = NULL;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003518
H. Peter Anvine2c80182005-01-15 22:15:51 +00003519 t = tline;
3520 while (tok_type_(t, TOK_WHITESPACE))
3521 t = t->next;
3522 /* t should now point to the string */
Cyrill Gorcunov4e1d5ab2010-07-23 18:51:51 +04003523 if (!tok_type_(t, TOK_STRING)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00003524 error(ERR_NONFATAL,
3525 "`%%strlen` requires string as second parameter");
3526 free_tlist(tline);
3527 free_tlist(origline);
3528 return DIRECTIVE_FOUND;
3529 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003530
Cyrill Gorcunov6b271292011-02-28 08:45:52 +03003531 macro_start = nasm_zalloc(sizeof(*macro_start));
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07003532 make_tok_num(macro_start, nasm_unquote(t->text, NULL));
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003533
H. Peter Anvine2c80182005-01-15 22:15:51 +00003534 /*
3535 * We now have a macro name, an implicit parameter count of
3536 * zero, and a numeric token to use as an expansion. Create
3537 * and store an SMacro.
3538 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003539 define_smacro(ctx, mname, casesense, 0, macro_start);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003540 free_tlist(tline);
3541 free_tlist(origline);
3542 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003543
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003544 case PP_STRCAT:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003545 if (defining != NULL) return NO_DIRECTIVE_FOUND;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003546 casesense = true;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003547
3548 tline = tline->next;
3549 skip_white_(tline);
3550 tline = expand_id(tline);
3551 if (!tline || (tline->type != TOK_ID &&
3552 (tline->type != TOK_PREPROC_ID ||
3553 tline->text[1] != '$'))) {
3554 error(ERR_NONFATAL,
3555 "`%%strcat' expects a macro identifier as first parameter");
3556 free_tlist(origline);
3557 return DIRECTIVE_FOUND;
3558 }
Cyrill Gorcunov290eac72011-06-28 01:59:05 +04003559 ctx = get_ctx(tline->text, &mname);
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003560 last = tline;
3561 tline = expand_smacro(tline->next);
3562 last->next = NULL;
3563
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003564 len = 0;
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04003565 list_for_each(t, tline) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003566 switch (t->type) {
3567 case TOK_WHITESPACE:
3568 break;
3569 case TOK_STRING:
3570 len += t->a.len = nasm_unquote(t->text, NULL);
3571 break;
3572 case TOK_OTHER:
3573 if (!strcmp(t->text, ",")) /* permit comma separators */
3574 break;
3575 /* else fall through */
3576 default:
3577 error(ERR_NONFATAL,
3578 "non-string passed to `%%strcat' (%d)", t->type);
3579 free_tlist(tline);
3580 free_tlist(origline);
3581 return DIRECTIVE_FOUND;
3582 }
3583 }
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003584
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003585 p = pp = nasm_malloc(len);
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04003586 list_for_each(t, tline) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003587 if (t->type == TOK_STRING) {
3588 memcpy(p, t->text, t->a.len);
3589 p += t->a.len;
3590 }
3591 }
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003592
3593 /*
3594 * We now have a macro name, an implicit parameter count of
3595 * zero, and a numeric token to use as an expansion. Create
3596 * and store an SMacro.
3597 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003598 macro_start = new_Token(NULL, TOK_STRING, NULL, 0);
3599 macro_start->text = nasm_quote(pp, len);
3600 nasm_free(pp);
3601 define_smacro(ctx, mname, casesense, 0, macro_start);
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003602 free_tlist(tline);
3603 free_tlist(origline);
3604 return DIRECTIVE_FOUND;
3605
H. Peter Anvine2c80182005-01-15 22:15:51 +00003606 case PP_SUBSTR:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003607 if (defining != NULL) return NO_DIRECTIVE_FOUND;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003608 {
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003609 int64_t start, count;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003610 size_t len;
H. Peter Anvind2456592008-06-19 15:04:18 -07003611
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003612 casesense = true;
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07003613
H. Peter Anvine2c80182005-01-15 22:15:51 +00003614 tline = tline->next;
3615 skip_white_(tline);
3616 tline = expand_id(tline);
3617 if (!tline || (tline->type != TOK_ID &&
3618 (tline->type != TOK_PREPROC_ID ||
3619 tline->text[1] != '$'))) {
3620 error(ERR_NONFATAL,
3621 "`%%substr' expects a macro identifier as first parameter");
3622 free_tlist(origline);
3623 return DIRECTIVE_FOUND;
3624 }
Cyrill Gorcunov290eac72011-06-28 01:59:05 +04003625 ctx = get_ctx(tline->text, &mname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003626 last = tline;
3627 tline = expand_smacro(tline->next);
3628 last->next = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003629
Cyrill Gorcunov35519d62010-09-06 23:49:52 +04003630 if (tline) /* skip expanded id */
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003631 t = tline->next;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003632 while (tok_type_(t, TOK_WHITESPACE))
3633 t = t->next;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003634
H. Peter Anvine2c80182005-01-15 22:15:51 +00003635 /* t should now point to the string */
Cyrill Gorcunov35519d62010-09-06 23:49:52 +04003636 if (!tok_type_(t, TOK_STRING)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00003637 error(ERR_NONFATAL,
3638 "`%%substr` requires string as second parameter");
3639 free_tlist(tline);
3640 free_tlist(origline);
3641 return DIRECTIVE_FOUND;
3642 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003643
H. Peter Anvine2c80182005-01-15 22:15:51 +00003644 tt = t->next;
3645 tptr = &tt;
3646 tokval.t_type = TOKEN_INVALID;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003647 evalresult = evaluate(ppscan, tptr, &tokval, NULL,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003648 pass, error, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003649 if (!evalresult) {
3650 free_tlist(tline);
3651 free_tlist(origline);
3652 return DIRECTIVE_FOUND;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003653 } else if (!is_simple(evalresult)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00003654 error(ERR_NONFATAL, "non-constant value given to `%%substr`");
3655 free_tlist(tline);
3656 free_tlist(origline);
3657 return DIRECTIVE_FOUND;
3658 }
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003659 start = evalresult->value - 1;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003660
3661 while (tok_type_(tt, TOK_WHITESPACE))
3662 tt = tt->next;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003663 if (!tt) {
Keith Kaniosb307a4f2010-11-06 17:41:51 -05003664 count = 1; /* Backwards compatibility: one character */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003665 } else {
3666 tokval.t_type = TOKEN_INVALID;
3667 evalresult = evaluate(ppscan, tptr, &tokval, NULL,
3668 pass, error, NULL);
3669 if (!evalresult) {
3670 free_tlist(tline);
3671 free_tlist(origline);
3672 return DIRECTIVE_FOUND;
3673 } else if (!is_simple(evalresult)) {
3674 error(ERR_NONFATAL, "non-constant value given to `%%substr`");
3675 free_tlist(tline);
3676 free_tlist(origline);
3677 return DIRECTIVE_FOUND;
3678 }
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003679 count = evalresult->value;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003680 }
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003681
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003682 len = nasm_unquote(t->text, NULL);
Cyrill Gorcunovcff031e2010-09-07 20:31:11 +04003683 /* make start and count being in range */
3684 if (start < 0)
3685 start = 0;
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003686 if (count < 0)
3687 count = len + count + 1 - start;
3688 if (start + count > (int64_t)len)
Cyrill Gorcunovcff031e2010-09-07 20:31:11 +04003689 count = len - start;
3690 if (!len || count < 0 || start >=(int64_t)len)
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003691 start = -1, count = 0; /* empty string */
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003692
Cyrill Gorcunov6b271292011-02-28 08:45:52 +03003693 macro_start = nasm_zalloc(sizeof(*macro_start));
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003694 macro_start->text = nasm_quote((start < 0) ? "" : t->text + start, count);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003695 macro_start->type = TOK_STRING;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003696
H. Peter Anvine2c80182005-01-15 22:15:51 +00003697 /*
3698 * We now have a macro name, an implicit parameter count of
3699 * zero, and a numeric token to use as an expansion. Create
3700 * and store an SMacro.
3701 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003702 define_smacro(ctx, mname, casesense, 0, macro_start);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003703 free_tlist(tline);
3704 free_tlist(origline);
3705 return DIRECTIVE_FOUND;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003706 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003707
H. Peter Anvine2c80182005-01-15 22:15:51 +00003708 case PP_ASSIGN:
3709 case PP_IASSIGN:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003710 if (defining != NULL) return NO_DIRECTIVE_FOUND;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003711 casesense = (i == PP_ASSIGN);
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07003712
H. Peter Anvine2c80182005-01-15 22:15:51 +00003713 tline = tline->next;
3714 skip_white_(tline);
3715 tline = expand_id(tline);
3716 if (!tline || (tline->type != TOK_ID &&
3717 (tline->type != TOK_PREPROC_ID ||
3718 tline->text[1] != '$'))) {
3719 error(ERR_NONFATAL,
3720 "`%%%sassign' expects a macro identifier",
3721 (i == PP_IASSIGN ? "i" : ""));
3722 free_tlist(origline);
3723 return DIRECTIVE_FOUND;
3724 }
Cyrill Gorcunov290eac72011-06-28 01:59:05 +04003725 ctx = get_ctx(tline->text, &mname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003726 last = tline;
3727 tline = expand_smacro(tline->next);
3728 last->next = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003729
H. Peter Anvine2c80182005-01-15 22:15:51 +00003730 t = tline;
3731 tptr = &t;
3732 tokval.t_type = TOKEN_INVALID;
3733 evalresult =
3734 evaluate(ppscan, tptr, &tokval, NULL, pass, error, NULL);
3735 free_tlist(tline);
3736 if (!evalresult) {
3737 free_tlist(origline);
3738 return DIRECTIVE_FOUND;
3739 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003740
H. Peter Anvine2c80182005-01-15 22:15:51 +00003741 if (tokval.t_type)
H. Peter Anvin917a3492008-09-24 09:14:49 -07003742 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +00003743 "trailing garbage after expression ignored");
H. Peter Anvin734b1882002-04-30 21:01:08 +00003744
H. Peter Anvine2c80182005-01-15 22:15:51 +00003745 if (!is_simple(evalresult)) {
3746 error(ERR_NONFATAL,
3747 "non-constant value given to `%%%sassign'",
3748 (i == PP_IASSIGN ? "i" : ""));
3749 free_tlist(origline);
3750 return DIRECTIVE_FOUND;
3751 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003752
Cyrill Gorcunov6b271292011-02-28 08:45:52 +03003753 macro_start = nasm_zalloc(sizeof(*macro_start));
H. Peter Anvine2c80182005-01-15 22:15:51 +00003754 make_tok_num(macro_start, reloc_value(evalresult));
H. Peter Anvin734b1882002-04-30 21:01:08 +00003755
H. Peter Anvine2c80182005-01-15 22:15:51 +00003756 /*
3757 * We now have a macro name, an implicit parameter count of
3758 * zero, and a numeric token to use as an expansion. Create
3759 * and store an SMacro.
3760 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003761 define_smacro(ctx, mname, casesense, 0, macro_start);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003762 free_tlist(origline);
3763 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003764
H. Peter Anvine2c80182005-01-15 22:15:51 +00003765 case PP_LINE:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003766 if (defining != NULL) return NO_DIRECTIVE_FOUND;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003767 /*
3768 * Syntax is `%line nnn[+mmm] [filename]'
3769 */
3770 tline = tline->next;
3771 skip_white_(tline);
3772 if (!tok_type_(tline, TOK_NUMBER)) {
3773 error(ERR_NONFATAL, "`%%line' expects line number");
3774 free_tlist(origline);
3775 return DIRECTIVE_FOUND;
3776 }
H. Peter Anvin70055962007-10-11 00:05:31 -07003777 k = readnum(tline->text, &err);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003778 m = 1;
3779 tline = tline->next;
3780 if (tok_is_(tline, "+")) {
3781 tline = tline->next;
3782 if (!tok_type_(tline, TOK_NUMBER)) {
3783 error(ERR_NONFATAL, "`%%line' expects line increment");
3784 free_tlist(origline);
3785 return DIRECTIVE_FOUND;
3786 }
H. Peter Anvin70055962007-10-11 00:05:31 -07003787 m = readnum(tline->text, &err);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003788 tline = tline->next;
3789 }
3790 skip_white_(tline);
3791 src_set_linnum(k);
3792 istk->lineinc = m;
3793 if (tline) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07003794 nasm_free(src_set_fname(detoken(tline, false)));
H. Peter Anvine2c80182005-01-15 22:15:51 +00003795 }
3796 free_tlist(origline);
3797 return DIRECTIVE_FOUND;
Keith Kaniosb307a4f2010-11-06 17:41:51 -05003798
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003799 case PP_WHILE:
3800 if (defining != NULL) {
3801 if (defining->type == EXP_WHILE) {
3802 defining->def_depth ++;
3803 }
3804 return NO_DIRECTIVE_FOUND;
3805 }
3806 l = NULL;
3807 if ((istk->expansion != NULL) &&
3808 (istk->expansion->emitting == false)) {
3809 j = COND_NEVER;
3810 } else {
3811 l = new_Line();
3812 l->first = copy_Token(tline->next);
3813 j = if_condition(tline->next, i);
3814 tline->next = NULL; /* it got freed */
3815 j = (((j < 0) ? COND_NEVER : j) ? COND_IF_TRUE : COND_IF_FALSE);
3816 }
3817 ed = new_ExpDef(EXP_WHILE);
3818 ed->state = j;
3819 ed->cur_depth = 1;
3820 ed->max_depth = DEADMAN_LIMIT;
3821 ed->ignoring = ((ed->state == COND_IF_TRUE) ? false : true);
3822 if (ed->ignoring == false) {
3823 ed->line = l;
3824 ed->last = l;
3825 } else if (l != NULL) {
3826 delete_Token(l->first);
3827 nasm_free(l);
3828 l = NULL;
3829 }
3830 ed->prev = defining;
3831 defining = ed;
3832 free_tlist(origline);
3833 return DIRECTIVE_FOUND;
3834
3835 case PP_ENDWHILE:
3836 if (defining != NULL) {
3837 if (defining->type == EXP_WHILE) {
3838 if (defining->def_depth > 0) {
3839 defining->def_depth --;
3840 return NO_DIRECTIVE_FOUND;
3841 }
3842 } else {
3843 return NO_DIRECTIVE_FOUND;
3844 }
3845 }
3846 if (tline->next != NULL) {
3847 error_precond(ERR_WARNING|ERR_PASS1,
3848 "trailing garbage after `%%endwhile' ignored");
3849 }
3850 if ((defining == NULL) || (defining->type != EXP_WHILE)) {
3851 error(ERR_NONFATAL, "`%%endwhile': no matching `%%while'");
3852 return DIRECTIVE_FOUND;
3853 }
3854 ed = defining;
3855 defining = ed->prev;
3856 if (ed->ignoring == false) {
3857 ed->prev = expansions;
3858 expansions = ed;
3859 ei = new_ExpInv(EXP_WHILE, ed);
3860 ei->current = ed->line->next;
3861 ei->emitting = true;
3862 ei->prev = istk->expansion;
3863 istk->expansion = ei;
3864 } else {
3865 nasm_free(ed);
3866 }
3867 free_tlist(origline);
3868 return DIRECTIVE_FOUND;
3869
3870 case PP_EXITWHILE:
3871 if (defining != NULL) return NO_DIRECTIVE_FOUND;
3872 /*
3873 * We must search along istk->expansion until we hit a
3874 * while invocation. Then we disable the emitting state(s)
3875 * between exitwhile and endwhile.
3876 */
3877 for (ei = istk->expansion; ei != NULL; ei = ei->prev) {
3878 if (ei->type == EXP_WHILE) {
3879 break;
3880 }
3881 }
3882
3883 if (ei != NULL) {
3884 /*
3885 * Set all invocations leading back to the while
3886 * invocation to a non-emitting state.
3887 */
3888 for (eei = istk->expansion; eei != ei; eei = eei->prev) {
3889 eei->emitting = false;
3890 }
3891 eei->emitting = false;
3892 eei->current = NULL;
3893 eei->def->cur_depth = eei->def->max_depth;
3894 } else {
3895 error(ERR_NONFATAL, "`%%exitwhile' not within `%%while' block");
3896 }
3897 free_tlist(origline);
3898 return DIRECTIVE_FOUND;
3899
3900 case PP_COMMENT:
3901 if (defining != NULL) {
3902 if (defining->type == EXP_COMMENT) {
3903 defining->def_depth ++;
3904 }
3905 return NO_DIRECTIVE_FOUND;
3906 }
3907 ed = new_ExpDef(EXP_COMMENT);
3908 ed->ignoring = true;
3909 ed->prev = defining;
3910 defining = ed;
3911 free_tlist(origline);
3912 return DIRECTIVE_FOUND;
3913
3914 case PP_ENDCOMMENT:
3915 if (defining != NULL) {
3916 if (defining->type == EXP_COMMENT) {
3917 if (defining->def_depth > 0) {
3918 defining->def_depth --;
3919 return NO_DIRECTIVE_FOUND;
3920 }
3921 } else {
3922 return NO_DIRECTIVE_FOUND;
3923 }
3924 }
3925 if ((defining == NULL) || (defining->type != EXP_COMMENT)) {
3926 error(ERR_NONFATAL, "`%%endcomment': no matching `%%comment'");
3927 return DIRECTIVE_FOUND;
3928 }
3929 ed = defining;
3930 defining = ed->prev;
3931 nasm_free(ed);
3932 free_tlist(origline);
3933 return DIRECTIVE_FOUND;
3934
3935 case PP_FINAL:
3936 if (defining != NULL) return NO_DIRECTIVE_FOUND;
3937 if (in_final != false) {
3938 error(ERR_FATAL, "`%%final' cannot be used recursively");
3939 }
3940 tline = tline->next;
3941 skip_white_(tline);
3942 if (tline == NULL) {
3943 error(ERR_NONFATAL, "`%%final' expects at least one parameter");
3944 } else {
3945 l = new_Line();
3946 l->first = copy_Token(tline);
3947 l->next = finals;
3948 finals = l;
3949 }
3950 free_tlist(origline);
3951 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003952
H. Peter Anvine2c80182005-01-15 22:15:51 +00003953 default:
3954 error(ERR_FATAL,
3955 "preprocessor directive `%s' not yet implemented",
H. Peter Anvin4169a472007-09-12 01:29:43 +00003956 pp_directives[i]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003957 return DIRECTIVE_FOUND;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003958 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003959}
3960
3961/*
H. Peter Anvin76690a12002-04-30 20:52:49 +00003962 * Ensure that a macro parameter contains a condition code and
3963 * nothing else. Return the condition code index if so, or -1
3964 * otherwise.
3965 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003966static int find_cc(Token * t)
H. Peter Anvineba20a72002-04-30 20:53:55 +00003967{
H. Peter Anvin76690a12002-04-30 20:52:49 +00003968 Token *tt;
3969 int i, j, k, m;
3970
H. Peter Anvin25a99342007-09-22 17:45:45 -07003971 if (!t)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003972 return -1; /* Probably a %+ without a space */
H. Peter Anvin25a99342007-09-22 17:45:45 -07003973
H. Peter Anvineba20a72002-04-30 20:53:55 +00003974 skip_white_(t);
H. Peter Anvin76690a12002-04-30 20:52:49 +00003975 if (t->type != TOK_ID)
H. Peter Anvine2c80182005-01-15 22:15:51 +00003976 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003977 tt = t->next;
H. Peter Anvineba20a72002-04-30 20:53:55 +00003978 skip_white_(tt);
H. Peter Anvin76690a12002-04-30 20:52:49 +00003979 if (tt && (tt->type != TOK_OTHER || strcmp(tt->text, ",")))
H. Peter Anvine2c80182005-01-15 22:15:51 +00003980 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003981
3982 i = -1;
Cyrill Gorcunova7319242010-06-03 22:04:36 +04003983 j = ARRAY_SIZE(conditions);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003984 while (j - i > 1) {
3985 k = (j + i) / 2;
3986 m = nasm_stricmp(t->text, conditions[k]);
3987 if (m == 0) {
3988 i = k;
3989 j = -2;
3990 break;
3991 } else if (m < 0) {
3992 j = k;
3993 } else
3994 i = k;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003995 }
3996 if (j != -2)
H. Peter Anvine2c80182005-01-15 22:15:51 +00003997 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003998 return i;
3999}
4000
Cyrill Gorcunov575d4282010-10-06 00:25:55 +04004001static bool paste_tokens(Token **head, const struct tokseq_match *m,
4002 int mnum, bool handle_paste_tokens)
H. Peter Anvind784a082009-04-20 14:01:18 -07004003{
4004 Token **tail, *t, *tt;
4005 Token **paste_head;
4006 bool did_paste = false;
4007 char *tmp;
Cyrill Gorcunov575d4282010-10-06 00:25:55 +04004008 int i;
H. Peter Anvind784a082009-04-20 14:01:18 -07004009
4010 /* Now handle token pasting... */
4011 paste_head = NULL;
4012 tail = head;
4013 while ((t = *tail) && (tt = t->next)) {
4014 switch (t->type) {
4015 case TOK_WHITESPACE:
4016 if (tt->type == TOK_WHITESPACE) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004017 /* Zap adjacent whitespace tokens */
H. Peter Anvind784a082009-04-20 14:01:18 -07004018 t->next = delete_Token(tt);
4019 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004020 /* Do not advance paste_head here */
4021 tail = &t->next;
4022 }
H. Peter Anvind784a082009-04-20 14:01:18 -07004023 break;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004024 case TOK_PASTE: /* %+ */
4025 if (handle_paste_tokens) {
4026 /* Zap %+ and whitespace tokens to the right */
4027 while (t && (t->type == TOK_WHITESPACE ||
4028 t->type == TOK_PASTE))
4029 t = *tail = delete_Token(t);
4030 if (!paste_head || !t)
4031 break; /* Nothing to paste with */
4032 tail = paste_head;
4033 t = *tail;
4034 tt = t->next;
4035 while (tok_type_(tt, TOK_WHITESPACE))
4036 tt = t->next = delete_Token(tt);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004037 if (tt) {
4038 tmp = nasm_strcat(t->text, tt->text);
4039 delete_Token(t);
4040 tt = delete_Token(tt);
4041 t = *tail = tokenize(tmp);
4042 nasm_free(tmp);
4043 while (t->next) {
4044 tail = &t->next;
4045 t = t->next;
4046 }
4047 t->next = tt; /* Attach the remaining token chain */
4048 did_paste = true;
4049 }
4050 paste_head = tail;
4051 tail = &t->next;
4052 break;
4053 }
4054 /* else fall through */
4055 default:
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +04004056 /*
4057 * Concatenation of tokens might look nontrivial
4058 * but in real it's pretty simple -- the caller
Cyrill Gorcunov575d4282010-10-06 00:25:55 +04004059 * prepares the masks of token types to be concatenated
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +04004060 * and we simply find matched sequences and slip
4061 * them together
4062 */
Cyrill Gorcunov575d4282010-10-06 00:25:55 +04004063 for (i = 0; i < mnum; i++) {
4064 if (PP_CONCAT_MASK(t->type) & m[i].mask_head) {
4065 size_t len = 0;
4066 char *tmp, *p;
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +04004067
Cyrill Gorcunov575d4282010-10-06 00:25:55 +04004068 while (tt && (PP_CONCAT_MASK(tt->type) & m[i].mask_tail)) {
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004069 len += strlen(tt->text);
4070 tt = tt->next;
Cyrill Gorcunov575d4282010-10-06 00:25:55 +04004071 }
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +04004072
Cyrill Gorcunovfdd0ac52011-06-27 01:22:27 +04004073 nasm_dump_token(tt);
4074
Cyrill Gorcunov575d4282010-10-06 00:25:55 +04004075 /*
4076 * Now tt points to the first token after
4077 * the potential paste area...
4078 */
4079 if (tt != t->next) {
4080 /* We have at least two tokens... */
4081 len += strlen(t->text);
4082 p = tmp = nasm_malloc(len+1);
4083 while (t != tt) {
4084 strcpy(p, t->text);
4085 p = strchr(p, '\0');
4086 t = delete_Token(t);
4087 }
4088 t = *tail = tokenize(tmp);
4089 nasm_free(tmp);
4090 while (t->next) {
4091 tail = &t->next;
4092 t = t->next;
4093 }
4094 t->next = tt; /* Attach the remaining token chain */
4095 did_paste = true;
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +04004096 }
Cyrill Gorcunov575d4282010-10-06 00:25:55 +04004097 paste_head = tail;
4098 tail = &t->next;
4099 break;
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +04004100 }
Cyrill Gorcunov575d4282010-10-06 00:25:55 +04004101 }
4102 if (i >= mnum) { /* no match */
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +04004103 tail = &t->next;
4104 if (!tok_type_(t->next, TOK_WHITESPACE))
4105 paste_head = tail;
4106 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004107 break;
H. Peter Anvind784a082009-04-20 14:01:18 -07004108 }
4109 }
4110 return did_paste;
4111}
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004112
4113/*
4114 * expands to a list of tokens from %{x:y}
4115 */
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004116static Token *expand_mmac_params_range(ExpInv *ei, Token *tline, Token ***last)
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004117{
4118 Token *t = tline, **tt, *tm, *head;
4119 char *pos;
4120 int fst, lst, j, i;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004121
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004122 pos = strchr(tline->text, ':');
4123 nasm_assert(pos);
4124
4125 lst = atoi(pos + 1);
4126 fst = atoi(tline->text + 1);
4127
4128 /*
4129 * only macros params are accounted so
4130 * if someone passes %0 -- we reject such
4131 * value(s)
4132 */
4133 if (lst == 0 || fst == 0)
4134 goto err;
4135
4136 /* the values should be sane */
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004137 if ((fst > (int)ei->nparam || fst < (-(int)ei->nparam)) ||
4138 (lst > (int)ei->nparam || lst < (-(int)ei->nparam)))
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004139 goto err;
4140
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004141 fst = fst < 0 ? fst + (int)ei->nparam + 1: fst;
4142 lst = lst < 0 ? lst + (int)ei->nparam + 1: lst;
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004143
4144 /* counted from zero */
4145 fst--, lst--;
4146
4147 /*
4148 * it will be at least one token
4149 */
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004150 tm = ei->params[(fst + ei->rotate) % ei->nparam];
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004151 t = new_Token(NULL, tm->type, tm->text, 0);
4152 head = t, tt = &t->next;
4153 if (fst < lst) {
4154 for (i = fst + 1; i <= lst; i++) {
4155 t = new_Token(NULL, TOK_OTHER, ",", 0);
4156 *tt = t, tt = &t->next;
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004157 j = (i + ei->rotate) % ei->nparam;
4158 tm = ei->params[j];
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004159 t = new_Token(NULL, tm->type, tm->text, 0);
4160 *tt = t, tt = &t->next;
4161 }
4162 } else {
4163 for (i = fst - 1; i >= lst; i--) {
4164 t = new_Token(NULL, TOK_OTHER, ",", 0);
4165 *tt = t, tt = &t->next;
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004166 j = (i + ei->rotate) % ei->nparam;
4167 tm = ei->params[j];
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004168 t = new_Token(NULL, tm->type, tm->text, 0);
4169 *tt = t, tt = &t->next;
4170 }
4171 }
4172
4173 *last = tt;
4174 return head;
4175
4176err:
4177 error(ERR_NONFATAL, "`%%{%s}': macro parameters out of range",
4178 &tline->text[1]);
4179 return tline;
4180}
4181
H. Peter Anvin76690a12002-04-30 20:52:49 +00004182/*
4183 * Expand MMacro-local things: parameter references (%0, %n, %+n,
H. Peter Anvin67c63722008-10-26 23:49:00 -07004184 * %-n) and MMacro-local identifiers (%%foo) as well as
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004185 * macro indirection (%[...]) and range (%{..:..}).
H. Peter Anvin76690a12002-04-30 20:52:49 +00004186 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004187static Token *expand_mmac_params(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004188{
H. Peter Anvin734b1882002-04-30 21:01:08 +00004189 Token *t, *tt, **tail, *thead;
H. Peter Anvin6125b622009-04-08 14:02:25 -07004190 bool changed = false;
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004191 char *pos;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004192
4193 tail = &thead;
4194 thead = NULL;
4195
Cyrill Gorcunov2e046002011-06-26 23:33:56 +04004196 nasm_dump_stream(tline);
4197
H. Peter Anvine2c80182005-01-15 22:15:51 +00004198 while (tline) {
4199 if (tline->type == TOK_PREPROC_ID &&
Cyrill Gorcunovca611192010-06-04 09:22:12 +04004200 (((tline->text[1] == '+' || tline->text[1] == '-') && tline->text[2]) ||
4201 (tline->text[1] >= '0' && tline->text[1] <= '9') ||
4202 tline->text[1] == '%')) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004203 char *text = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004204 int type = 0, cc; /* type = 0 to placate optimisers */
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004205 char tmpbuf[30];
H. Peter Anvin25a99342007-09-22 17:45:45 -07004206 unsigned int n;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004207 int i;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004208 ExpInv *ei;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004209
H. Peter Anvine2c80182005-01-15 22:15:51 +00004210 t = tline;
4211 tline = tline->next;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004212
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004213 for (ei = istk->expansion; ei != NULL; ei = ei->prev) {
4214 if (ei->type == EXP_MMACRO) {
4215 break;
4216 }
4217 }
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004218 if (ei == NULL) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00004219 error(ERR_NONFATAL, "`%s': not in a macro call", t->text);
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004220 } else {
4221 pos = strchr(t->text, ':');
4222 if (!pos) {
4223 switch (t->text[1]) {
4224 /*
4225 * We have to make a substitution of one of the
4226 * forms %1, %-1, %+1, %%foo, %0.
4227 */
4228 case '0':
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004229 if ((strlen(t->text) > 2) && (t->text[2] == '0')) {
4230 type = TOK_ID;
4231 text = nasm_strdup(ei->label_text);
4232 } else {
4233 type = TOK_NUMBER;
4234 snprintf(tmpbuf, sizeof(tmpbuf), "%d", ei->nparam);
4235 text = nasm_strdup(tmpbuf);
4236 }
4237 break;
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004238 case '%':
H. Peter Anvine2c80182005-01-15 22:15:51 +00004239 type = TOK_ID;
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004240 snprintf(tmpbuf, sizeof(tmpbuf), "..@%"PRIu64".",
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004241 ei->unique);
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004242 text = nasm_strcat(tmpbuf, t->text + 2);
4243 break;
4244 case '-':
4245 n = atoi(t->text + 2) - 1;
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004246 if (n >= ei->nparam)
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004247 tt = NULL;
4248 else {
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004249 if (ei->nparam > 1)
4250 n = (n + ei->rotate) % ei->nparam;
4251 tt = ei->params[n];
H. Peter Anvine2c80182005-01-15 22:15:51 +00004252 }
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004253 cc = find_cc(tt);
4254 if (cc == -1) {
4255 error(ERR_NONFATAL,
4256 "macro parameter %d is not a condition code",
4257 n + 1);
4258 text = NULL;
4259 } else {
4260 type = TOK_ID;
4261 if (inverse_ccs[cc] == -1) {
4262 error(ERR_NONFATAL,
4263 "condition code `%s' is not invertible",
4264 conditions[cc]);
4265 text = NULL;
4266 } else
4267 text = nasm_strdup(conditions[inverse_ccs[cc]]);
4268 }
4269 break;
4270 case '+':
4271 n = atoi(t->text + 2) - 1;
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004272 if (n >= ei->nparam)
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004273 tt = NULL;
4274 else {
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004275 if (ei->nparam > 1)
4276 n = (n + ei->rotate) % ei->nparam;
4277 tt = ei->params[n];
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004278 }
4279 cc = find_cc(tt);
4280 if (cc == -1) {
4281 error(ERR_NONFATAL,
4282 "macro parameter %d is not a condition code",
4283 n + 1);
4284 text = NULL;
4285 } else {
4286 type = TOK_ID;
4287 text = nasm_strdup(conditions[cc]);
4288 }
4289 break;
4290 default:
4291 n = atoi(t->text + 1) - 1;
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004292 if (n >= ei->nparam)
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004293 tt = NULL;
4294 else {
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004295 if (ei->nparam > 1)
4296 n = (n + ei->rotate) % ei->nparam;
4297 tt = ei->params[n];
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004298 }
4299 if (tt) {
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004300 for (i = 0; i < ei->paramlen[n]; i++) {
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004301 *tail = new_Token(NULL, tt->type, tt->text, 0);
4302 tail = &(*tail)->next;
4303 tt = tt->next;
4304 }
4305 }
4306 text = NULL; /* we've done it here */
4307 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004308 }
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004309 } else {
4310 /*
4311 * seems we have a parameters range here
4312 */
4313 Token *head, **last;
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004314 head = expand_mmac_params_range(ei, t, &last);
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004315 if (head != t) {
4316 *tail = head;
4317 *last = tline;
4318 tline = head;
4319 text = NULL;
4320 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00004321 }
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004322 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00004323 if (!text) {
4324 delete_Token(t);
4325 } else {
4326 *tail = t;
4327 tail = &t->next;
4328 t->type = type;
4329 nasm_free(t->text);
4330 t->text = text;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004331 t->a.mac = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004332 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004333 changed = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004334 continue;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004335 } else if (tline->type == TOK_INDIRECT) {
4336 t = tline;
4337 tline = tline->next;
4338 tt = tokenize(t->text);
4339 tt = expand_mmac_params(tt);
4340 tt = expand_smacro(tt);
4341 *tail = tt;
4342 while (tt) {
4343 tt->a.mac = NULL; /* Necessary? */
4344 tail = &tt->next;
4345 tt = tt->next;
4346 }
4347 delete_Token(t);
4348 changed = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004349 } else {
4350 t = *tail = tline;
4351 tline = tline->next;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004352 t->a.mac = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004353 tail = &t->next;
4354 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00004355 }
H. Peter Anvineba20a72002-04-30 20:53:55 +00004356 *tail = NULL;
H. Peter Anvin67c63722008-10-26 23:49:00 -07004357
Cyrill Gorcunovc6a742c2011-06-27 01:23:09 +04004358 if (changed) {
4359 const struct tokseq_match t[] = {
4360 {
4361 PP_CONCAT_MASK(TOK_ID) |
4362 PP_CONCAT_MASK(TOK_FLOAT), /* head */
4363 PP_CONCAT_MASK(TOK_ID) |
4364 PP_CONCAT_MASK(TOK_NUMBER) |
4365 PP_CONCAT_MASK(TOK_FLOAT) |
4366 PP_CONCAT_MASK(TOK_OTHER) /* tail */
4367 },
4368 {
4369 PP_CONCAT_MASK(TOK_NUMBER), /* head */
4370 PP_CONCAT_MASK(TOK_NUMBER) /* tail */
4371 }
4372 };
4373 paste_tokens(&thead, t, ARRAY_SIZE(t), false);
4374 }
H. Peter Anvin6125b622009-04-08 14:02:25 -07004375
Cyrill Gorcunov2e046002011-06-26 23:33:56 +04004376 nasm_dump_token(thead);
4377
H. Peter Anvin76690a12002-04-30 20:52:49 +00004378 return thead;
4379}
4380
4381/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004382 * Expand all single-line macro calls made in the given line.
4383 * Return the expanded version of the line. The original is deemed
4384 * to be destroyed in the process. (In reality we'll just move
4385 * Tokens from input to output a lot of the time, rather than
4386 * actually bothering to destroy and replicate.)
4387 */
H. Peter Anvincb1cf592007-11-19 12:26:50 -08004388
H. Peter Anvine2c80182005-01-15 22:15:51 +00004389static Token *expand_smacro(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004390{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004391 Token *t, *tt, *mstart, **tail, *thead;
H. Peter Anvineba20a72002-04-30 20:53:55 +00004392 SMacro *head = NULL, *m;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004393 Token **params;
4394 int *paramsize;
H. Peter Anvin25a99342007-09-22 17:45:45 -07004395 unsigned int nparam, sparam;
H. Peter Anvind784a082009-04-20 14:01:18 -07004396 int brackets;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004397 Token *org_tline = tline;
4398 Context *ctx;
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08004399 const char *mname;
H. Peter Anvin2a15e692007-11-19 13:14:59 -08004400 int deadman = DEADMAN_LIMIT;
H. Peter Anvin8287daf2009-07-07 16:00:58 -07004401 bool expanded;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004402
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004403 /*
4404 * Trick: we should avoid changing the start token pointer since it can
4405 * be contained in "next" field of other token. Because of this
4406 * we allocate a copy of first token and work with it; at the end of
4407 * routine we copy it back
4408 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004409 if (org_tline) {
Cyrill Gorcunoved4a8052010-04-09 15:40:35 +04004410 tline = new_Token(org_tline->next, org_tline->type,
4411 org_tline->text, 0);
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004412 tline->a.mac = org_tline->a.mac;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004413 nasm_free(org_tline->text);
4414 org_tline->text = NULL;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004415 }
4416
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004417 expanded = true; /* Always expand %+ at least once */
H. Peter Anvin8287daf2009-07-07 16:00:58 -07004418
H. Peter Anvincb1cf592007-11-19 12:26:50 -08004419again:
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004420 thead = NULL;
Cyrill Gorcunoved4a8052010-04-09 15:40:35 +04004421 tail = &thead;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004422
H. Peter Anvine2c80182005-01-15 22:15:51 +00004423 while (tline) { /* main token loop */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004424 if (!--deadman) {
4425 error(ERR_NONFATAL, "interminable macro recursion");
Cyrill Gorcunovbd38c8f2009-11-21 11:11:23 +03004426 goto err;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004427 }
H. Peter Anvincb1cf592007-11-19 12:26:50 -08004428
H. Peter Anvine2c80182005-01-15 22:15:51 +00004429 if ((mname = tline->text)) {
4430 /* if this token is a local macro, look in local context */
Cyrill Gorcunovc56d9ad2010-02-11 15:12:19 +03004431 if (tline->type == TOK_ID) {
4432 head = (SMacro *)hash_findix(&smacros, mname);
4433 } else if (tline->type == TOK_PREPROC_ID) {
Cyrill Gorcunov290eac72011-06-28 01:59:05 +04004434 ctx = get_ctx(mname, &mname);
Cyrill Gorcunovc56d9ad2010-02-11 15:12:19 +03004435 head = ctx ? (SMacro *)hash_findix(&ctx->localmac, mname) : NULL;
4436 } else
4437 head = NULL;
H. Peter Anvin072771e2008-05-22 13:17:51 -07004438
H. Peter Anvine2c80182005-01-15 22:15:51 +00004439 /*
4440 * We've hit an identifier. As in is_mmacro below, we first
4441 * check whether the identifier is a single-line macro at
4442 * all, then think about checking for parameters if
4443 * necessary.
4444 */
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04004445 list_for_each(m, head)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004446 if (!mstrcmp(m->name, mname, m->casesense))
4447 break;
4448 if (m) {
4449 mstart = tline;
4450 params = NULL;
4451 paramsize = NULL;
4452 if (m->nparam == 0) {
4453 /*
4454 * Simple case: the macro is parameterless. Discard the
4455 * one token that the macro call took, and push the
4456 * expansion back on the to-do stack.
4457 */
4458 if (!m->expansion) {
4459 if (!strcmp("__FILE__", m->name)) {
4460 int32_t num = 0;
4461 char *file = NULL;
4462 src_get(&num, &file);
4463 tline->text = nasm_quote(file, strlen(file));
4464 tline->type = TOK_STRING;
4465 nasm_free(file);
H. Peter Anvine2c80182005-01-15 22:15:51 +00004466 continue;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004467 }
4468 if (!strcmp("__LINE__", m->name)) {
4469 nasm_free(tline->text);
4470 make_tok_num(tline, src_get_linnum());
4471 continue;
4472 }
4473 if (!strcmp("__BITS__", m->name)) {
4474 nasm_free(tline->text);
4475 make_tok_num(tline, globalbits);
4476 continue;
4477 }
4478 tline = delete_Token(tline);
4479 continue;
4480 }
4481 } else {
4482 /*
4483 * Complicated case: at least one macro with this name
H. Peter Anvine2c80182005-01-15 22:15:51 +00004484 * exists and takes parameters. We must find the
4485 * parameters in the call, count them, find the SMacro
4486 * that corresponds to that form of the macro call, and
4487 * substitute for the parameters when we expand. What a
4488 * pain.
4489 */
4490 /*tline = tline->next;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004491 skip_white_(tline); */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004492 do {
4493 t = tline->next;
4494 while (tok_type_(t, TOK_SMAC_END)) {
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004495 t->a.mac->in_progress = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004496 t->text = NULL;
4497 t = tline->next = delete_Token(t);
4498 }
4499 tline = t;
4500 } while (tok_type_(tline, TOK_WHITESPACE));
4501 if (!tok_is_(tline, "(")) {
4502 /*
4503 * This macro wasn't called with parameters: ignore
4504 * the call. (Behaviour borrowed from gnu cpp.)
4505 */
4506 tline = mstart;
4507 m = NULL;
4508 } else {
4509 int paren = 0;
4510 int white = 0;
4511 brackets = 0;
4512 nparam = 0;
4513 sparam = PARAM_DELTA;
4514 params = nasm_malloc(sparam * sizeof(Token *));
4515 params[0] = tline->next;
4516 paramsize = nasm_malloc(sparam * sizeof(int));
4517 paramsize[0] = 0;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07004518 while (true) { /* parameter loop */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004519 /*
4520 * For some unusual expansions
4521 * which concatenates function call
4522 */
4523 t = tline->next;
4524 while (tok_type_(t, TOK_SMAC_END)) {
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004525 t->a.mac->in_progress = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004526 t->text = NULL;
4527 t = tline->next = delete_Token(t);
4528 }
4529 tline = t;
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00004530
H. Peter Anvine2c80182005-01-15 22:15:51 +00004531 if (!tline) {
4532 error(ERR_NONFATAL,
4533 "macro call expects terminating `)'");
4534 break;
4535 }
4536 if (tline->type == TOK_WHITESPACE
4537 && brackets <= 0) {
4538 if (paramsize[nparam])
4539 white++;
4540 else
4541 params[nparam] = tline->next;
4542 continue; /* parameter loop */
4543 }
4544 if (tline->type == TOK_OTHER
4545 && tline->text[1] == 0) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004546 char ch = tline->text[0];
H. Peter Anvine2c80182005-01-15 22:15:51 +00004547 if (ch == ',' && !paren && brackets <= 0) {
4548 if (++nparam >= sparam) {
4549 sparam += PARAM_DELTA;
4550 params = nasm_realloc(params,
Cyrill Gorcunoved4a8052010-04-09 15:40:35 +04004551 sparam * sizeof(Token *));
4552 paramsize = nasm_realloc(paramsize,
4553 sparam * sizeof(int));
H. Peter Anvine2c80182005-01-15 22:15:51 +00004554 }
4555 params[nparam] = tline->next;
4556 paramsize[nparam] = 0;
4557 white = 0;
4558 continue; /* parameter loop */
4559 }
4560 if (ch == '{' &&
4561 (brackets > 0 || (brackets == 0 &&
4562 !paramsize[nparam])))
4563 {
4564 if (!(brackets++)) {
4565 params[nparam] = tline->next;
4566 continue; /* parameter loop */
4567 }
4568 }
4569 if (ch == '}' && brackets > 0)
4570 if (--brackets == 0) {
4571 brackets = -1;
4572 continue; /* parameter loop */
4573 }
4574 if (ch == '(' && !brackets)
4575 paren++;
4576 if (ch == ')' && brackets <= 0)
4577 if (--paren < 0)
4578 break;
4579 }
4580 if (brackets < 0) {
4581 brackets = 0;
4582 error(ERR_NONFATAL, "braces do not "
4583 "enclose all of macro parameter");
4584 }
4585 paramsize[nparam] += white + 1;
4586 white = 0;
4587 } /* parameter loop */
4588 nparam++;
4589 while (m && (m->nparam != nparam ||
4590 mstrcmp(m->name, mname,
4591 m->casesense)))
4592 m = m->next;
4593 if (!m)
H. Peter Anvin917a3492008-09-24 09:14:49 -07004594 error(ERR_WARNING|ERR_PASS1|ERR_WARN_MNP,
H. Peter Anvine2c80182005-01-15 22:15:51 +00004595 "macro `%s' exists, "
4596 "but not taking %d parameters",
4597 mstart->text, nparam);
4598 }
4599 }
4600 if (m && m->in_progress)
4601 m = NULL;
4602 if (!m) { /* in progess or didn't find '(' or wrong nparam */
H. Peter Anvin70653092007-10-19 14:42:29 -07004603 /*
H. Peter Anvine2c80182005-01-15 22:15:51 +00004604 * Design question: should we handle !tline, which
4605 * indicates missing ')' here, or expand those
4606 * macros anyway, which requires the (t) test a few
H. Peter Anvin70653092007-10-19 14:42:29 -07004607 * lines down?
H. Peter Anvine2c80182005-01-15 22:15:51 +00004608 */
4609 nasm_free(params);
4610 nasm_free(paramsize);
4611 tline = mstart;
4612 } else {
4613 /*
4614 * Expand the macro: we are placed on the last token of the
4615 * call, so that we can easily split the call from the
4616 * following tokens. We also start by pushing an SMAC_END
4617 * token for the cycle removal.
4618 */
4619 t = tline;
4620 if (t) {
4621 tline = t->next;
4622 t->next = NULL;
4623 }
4624 tt = new_Token(tline, TOK_SMAC_END, NULL, 0);
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004625 tt->a.mac = m;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07004626 m->in_progress = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004627 tline = tt;
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04004628 list_for_each(t, m->expansion) {
Cyrill Gorcunov194ba892011-06-30 01:16:35 +04004629 if (is_smacro_param(t)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00004630 Token *pcopy = tline, **ptail = &pcopy;
Cyrill Gorcunov0ad6a7b2011-06-30 01:36:45 +04004631 Token *ttt;
Cyrill Gorcunov194ba892011-06-30 01:16:35 +04004632 int i, idx;
H. Peter Anvin734b1882002-04-30 21:01:08 +00004633
Cyrill Gorcunov194ba892011-06-30 01:16:35 +04004634 idx = smacro_get_param_idx(t);
4635 ttt = params[idx];
Cyrill Gorcunov0ad6a7b2011-06-30 01:36:45 +04004636
4637 /*
4638 * We need smacro paramters appended.
4639 */
4640 for (i = paramsize[idx]; i > 0; i--) {
4641 *ptail = new_Token(tline, ttt->type, ttt->text, 0);
4642 ptail = &(*ptail)->next;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004643 ttt = ttt->next;
4644 }
Cyrill Gorcunov0ad6a7b2011-06-30 01:36:45 +04004645
H. Peter Anvine2c80182005-01-15 22:15:51 +00004646 tline = pcopy;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004647 } else if (t->type == TOK_PREPROC_Q) {
4648 tt = new_Token(tline, TOK_ID, mname, 0);
4649 tline = tt;
4650 } else if (t->type == TOK_PREPROC_QQ) {
4651 tt = new_Token(tline, TOK_ID, m->name, 0);
4652 tline = tt;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004653 } else {
4654 tt = new_Token(tline, t->type, t->text, 0);
4655 tline = tt;
4656 }
4657 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00004658
H. Peter Anvine2c80182005-01-15 22:15:51 +00004659 /*
4660 * Having done that, get rid of the macro call, and clean
4661 * up the parameters.
4662 */
4663 nasm_free(params);
4664 nasm_free(paramsize);
4665 free_tlist(mstart);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004666 expanded = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004667 continue; /* main token loop */
4668 }
4669 }
4670 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004671
H. Peter Anvine2c80182005-01-15 22:15:51 +00004672 if (tline->type == TOK_SMAC_END) {
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004673 tline->a.mac->in_progress = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004674 tline = delete_Token(tline);
4675 } else {
4676 t = *tail = tline;
4677 tline = tline->next;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004678 t->a.mac = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004679 t->next = NULL;
4680 tail = &t->next;
4681 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004682 }
4683
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004684 /*
4685 * Now scan the entire line and look for successive TOK_IDs that resulted
Keith Kaniosb7a89542007-04-12 02:40:54 +00004686 * after expansion (they can't be produced by tokenize()). The successive
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004687 * TOK_IDs should be concatenated.
4688 * Also we look for %+ tokens and concatenate the tokens before and after
4689 * them (without white spaces in between).
4690 */
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +04004691 if (expanded) {
Cyrill Gorcunovc6a742c2011-06-27 01:23:09 +04004692 const struct tokseq_match t[] = {
4693 {
4694 PP_CONCAT_MASK(TOK_ID) |
4695 PP_CONCAT_MASK(TOK_PREPROC_ID), /* head */
4696 PP_CONCAT_MASK(TOK_ID) |
4697 PP_CONCAT_MASK(TOK_PREPROC_ID) |
4698 PP_CONCAT_MASK(TOK_NUMBER) /* tail */
4699 }
4700 };
4701 if (paste_tokens(&thead, t, ARRAY_SIZE(t), true)) {
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +04004702 /*
4703 * If we concatenated something, *and* we had previously expanded
4704 * an actual macro, scan the lines again for macros...
4705 */
4706 tline = thead;
4707 expanded = false;
4708 goto again;
4709 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00004710 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004711
Cyrill Gorcunovbd38c8f2009-11-21 11:11:23 +03004712err:
H. Peter Anvine2c80182005-01-15 22:15:51 +00004713 if (org_tline) {
4714 if (thead) {
4715 *org_tline = *thead;
4716 /* since we just gave text to org_line, don't free it */
4717 thead->text = NULL;
4718 delete_Token(thead);
4719 } else {
4720 /* the expression expanded to empty line;
4721 we can't return NULL for some reasons
4722 we just set the line to a single WHITESPACE token. */
4723 memset(org_tline, 0, sizeof(*org_tline));
4724 org_tline->text = NULL;
4725 org_tline->type = TOK_WHITESPACE;
4726 }
4727 thead = org_tline;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004728 }
4729
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004730 return thead;
4731}
4732
4733/*
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004734 * Similar to expand_smacro but used exclusively with macro identifiers
4735 * right before they are fetched in. The reason is that there can be
4736 * identifiers consisting of several subparts. We consider that if there
4737 * are more than one element forming the name, user wants a expansion,
4738 * otherwise it will be left as-is. Example:
4739 *
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004740 * %define %$abc cde
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004741 *
4742 * the identifier %$abc will be left as-is so that the handler for %define
4743 * will suck it and define the corresponding value. Other case:
4744 *
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004745 * %define _%$abc cde
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004746 *
4747 * In this case user wants name to be expanded *before* %define starts
4748 * working, so we'll expand %$abc into something (if it has a value;
4749 * otherwise it will be left as-is) then concatenate all successive
4750 * PP_IDs into one.
4751 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004752static Token *expand_id(Token * tline)
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004753{
4754 Token *cur, *oldnext = NULL;
4755
H. Peter Anvin734b1882002-04-30 21:01:08 +00004756 if (!tline || !tline->next)
H. Peter Anvine2c80182005-01-15 22:15:51 +00004757 return tline;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004758
4759 cur = tline;
4760 while (cur->next &&
Cyrill Gorcunov5b6c96b2011-06-30 00:22:53 +04004761 (cur->next->type == TOK_ID ||
4762 cur->next->type == TOK_PREPROC_ID ||
4763 cur->next->type == TOK_NUMBER))
H. Peter Anvine2c80182005-01-15 22:15:51 +00004764 cur = cur->next;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004765
4766 /* If identifier consists of just one token, don't expand */
4767 if (cur == tline)
H. Peter Anvine2c80182005-01-15 22:15:51 +00004768 return tline;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004769
H. Peter Anvine2c80182005-01-15 22:15:51 +00004770 if (cur) {
4771 oldnext = cur->next; /* Detach the tail past identifier */
4772 cur->next = NULL; /* so that expand_smacro stops here */
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004773 }
4774
H. Peter Anvin734b1882002-04-30 21:01:08 +00004775 tline = expand_smacro(tline);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004776
H. Peter Anvine2c80182005-01-15 22:15:51 +00004777 if (cur) {
4778 /* expand_smacro possibly changhed tline; re-scan for EOL */
4779 cur = tline;
4780 while (cur && cur->next)
4781 cur = cur->next;
4782 if (cur)
4783 cur->next = oldnext;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004784 }
4785
4786 return tline;
4787}
4788
4789/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004790 * Determine whether the given line constitutes a multi-line macro
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004791 * call, and return the ExpDef structure called if so. Doesn't have
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004792 * to check for an initial label - that's taken care of in
4793 * expand_mmacro - but must check numbers of parameters. Guaranteed
4794 * to be called with tline->type == TOK_ID, so the putative macro
4795 * name is easy to find.
4796 */
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004797static ExpDef *is_mmacro(Token * tline, Token *** params_array)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004798{
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004799 ExpDef *head, *ed;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004800 Token **params;
4801 int nparam;
4802
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004803 head = (ExpDef *) hash_findix(&expdefs, tline->text);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004804
4805 /*
4806 * Efficiency: first we see if any macro exists with the given
4807 * name. If not, we can return NULL immediately. _Then_ we
4808 * count the parameters, and then we look further along the
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004809 * list if necessary to find the proper ExpDef.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004810 */
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004811 list_for_each(ed, head)
4812 if (!mstrcmp(ed->name, tline->text, ed->casesense))
H. Peter Anvine2c80182005-01-15 22:15:51 +00004813 break;
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004814 if (!ed)
H. Peter Anvine2c80182005-01-15 22:15:51 +00004815 return NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004816
4817 /*
4818 * OK, we have a potential macro. Count and demarcate the
4819 * parameters.
4820 */
H. Peter Anvin734b1882002-04-30 21:01:08 +00004821 count_mmac_params(tline->next, &nparam, &params);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004822
4823 /*
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004824 * So we know how many parameters we've got. Find the ExpDef
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004825 * structure that handles this number.
4826 */
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004827 while (ed) {
4828 if (ed->nparam_min <= nparam
4829 && (ed->plus || nparam <= ed->nparam_max)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00004830 /*
4831 * It's right, and we can use it. Add its default
4832 * parameters to the end of our list if necessary.
4833 */
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004834 if (ed->defaults && nparam < ed->nparam_min + ed->ndefs) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00004835 params =
4836 nasm_realloc(params,
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004837 ((ed->nparam_min + ed->ndefs +
H. Peter Anvine2c80182005-01-15 22:15:51 +00004838 1) * sizeof(*params)));
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004839 while (nparam < ed->nparam_min + ed->ndefs) {
4840 params[nparam] = ed->defaults[nparam - ed->nparam_min];
H. Peter Anvine2c80182005-01-15 22:15:51 +00004841 nparam++;
4842 }
4843 }
4844 /*
4845 * If we've gone over the maximum parameter count (and
4846 * we're in Plus mode), ignore parameters beyond
4847 * nparam_max.
4848 */
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004849 if (ed->plus && nparam > ed->nparam_max)
4850 nparam = ed->nparam_max;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004851 /*
4852 * Then terminate the parameter list, and leave.
4853 */
4854 if (!params) { /* need this special case */
4855 params = nasm_malloc(sizeof(*params));
4856 nparam = 0;
4857 }
4858 params[nparam] = NULL;
4859 *params_array = params;
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004860 return ed;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004861 }
4862 /*
4863 * This one wasn't right: look for the next one with the
4864 * same name.
4865 */
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004866 list_for_each(ed, ed->next)
4867 if (!mstrcmp(ed->name, tline->text, ed->casesense))
H. Peter Anvine2c80182005-01-15 22:15:51 +00004868 break;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004869 }
4870
4871 /*
4872 * After all that, we didn't find one with the right number of
4873 * parameters. Issue a warning, and fail to expand the macro.
4874 */
H. Peter Anvin917a3492008-09-24 09:14:49 -07004875 error(ERR_WARNING|ERR_PASS1|ERR_WARN_MNP,
H. Peter Anvine2c80182005-01-15 22:15:51 +00004876 "macro `%s' exists, but not taking %d parameters",
4877 tline->text, nparam);
H. Peter Anvin734b1882002-04-30 21:01:08 +00004878 nasm_free(params);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004879 return NULL;
4880}
4881
4882/*
4883 * Expand the multi-line macro call made by the given line, if
4884 * there is one to be expanded. If there is, push the expansion on
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004885 * istk->expansion and return true. Otherwise return false.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004886 */
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004887static bool expand_mmacro(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004888{
H. Peter Anvineba20a72002-04-30 20:53:55 +00004889 Token *label = NULL;
4890 int dont_prepend = 0;
Cyrill Gorcunovfb27fc22011-06-25 12:08:30 +04004891 Token **params, *t;
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004892 Line *l = NULL;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004893 ExpDef *ed;
4894 ExpInv *ei;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004895 int i, nparam, *paramlen;
H. Peter Anvinc751e862008-06-09 10:18:45 -07004896 const char *mname;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004897
4898 t = tline;
H. Peter Anvineba20a72002-04-30 20:53:55 +00004899 skip_white_(t);
H. Peter Anvince2233b2008-05-25 21:57:00 -07004900 /* if (!tok_type_(t, TOK_ID)) Lino 02/25/02 */
H. Peter Anvindce1e2f2002-04-30 21:06:37 +00004901 if (!tok_type_(t, TOK_ID) && !tok_type_(t, TOK_PREPROC_ID))
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004902 return false;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004903 ed = is_mmacro(t, &params);
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004904 if (ed != NULL) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004905 mname = t->text;
H. Peter Anvinc751e862008-06-09 10:18:45 -07004906 } else {
H. Peter Anvine2c80182005-01-15 22:15:51 +00004907 Token *last;
4908 /*
4909 * We have an id which isn't a macro call. We'll assume
4910 * it might be a label; we'll also check to see if a
4911 * colon follows it. Then, if there's another id after
4912 * that lot, we'll check it again for macro-hood.
4913 */
4914 label = last = t;
4915 t = t->next;
4916 if (tok_type_(t, TOK_WHITESPACE))
4917 last = t, t = t->next;
4918 if (tok_is_(t, ":")) {
4919 dont_prepend = 1;
4920 last = t, t = t->next;
4921 if (tok_type_(t, TOK_WHITESPACE))
4922 last = t, t = t->next;
4923 }
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004924 if (!tok_type_(t, TOK_ID) || !(ed = is_mmacro(t, &params)))
4925 return false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004926 last->next = NULL;
Keith Kanios891775e2009-07-11 06:08:54 -05004927 mname = t->text;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004928 tline = t;
H. Peter Anvineba20a72002-04-30 20:53:55 +00004929 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004930
4931 /*
4932 * Fix up the parameters: this involves stripping leading and
4933 * trailing whitespace, then stripping braces if they are
4934 * present.
4935 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004936 for (nparam = 0; params[nparam]; nparam++) ;
H. Peter Anvin734b1882002-04-30 21:01:08 +00004937 paramlen = nparam ? nasm_malloc(nparam * sizeof(*paramlen)) : NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004938
H. Peter Anvine2c80182005-01-15 22:15:51 +00004939 for (i = 0; params[i]; i++) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07004940 int brace = false;
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004941 int comma = (!ed->plus || i < nparam - 1);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004942
H. Peter Anvine2c80182005-01-15 22:15:51 +00004943 t = params[i];
4944 skip_white_(t);
4945 if (tok_is_(t, "{"))
H. Peter Anvin6867acc2007-10-10 14:58:45 -07004946 t = t->next, brace = true, comma = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004947 params[i] = t;
4948 paramlen[i] = 0;
4949 while (t) {
4950 if (comma && t->type == TOK_OTHER && !strcmp(t->text, ","))
4951 break; /* ... because we have hit a comma */
4952 if (comma && t->type == TOK_WHITESPACE
4953 && tok_is_(t->next, ","))
4954 break; /* ... or a space then a comma */
4955 if (brace && t->type == TOK_OTHER && !strcmp(t->text, "}"))
4956 break; /* ... or a brace */
4957 t = t->next;
4958 paramlen[i]++;
4959 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004960 }
4961
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004962 if (ed->cur_depth >= ed->max_depth) {
4963 if (ed->max_depth > 1) {
4964 error(ERR_WARNING,
4965 "reached maximum macro recursion depth of %i for %s",
4966 ed->max_depth,ed->name);
4967 }
4968 return false;
4969 } else {
4970 ed->cur_depth ++;
4971 }
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004972
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004973 /*
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004974 * OK, we have found a ExpDef structure representing a
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004975 * previously defined mmacro. Create an expansion invocation
4976 * and point it back to the expansion definition. Substitution of
H. Peter Anvin76690a12002-04-30 20:52:49 +00004977 * parameter tokens and macro-local tokens doesn't get done
4978 * until the single-line macro substitution process; this is
4979 * because delaying them allows us to change the semantics
4980 * later through %rotate.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004981 */
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004982 ei = new_ExpInv(EXP_MMACRO, ed);
4983 ei->name = nasm_strdup(mname);
4984 //ei->label = label;
4985 //ei->label_text = detoken(label, false);
4986 ei->current = ed->line;
4987 ei->emitting = true;
4988 //ei->iline = tline;
4989 ei->params = params;
4990 ei->nparam = nparam;
4991 ei->rotate = 0;
4992 ei->paramlen = paramlen;
4993 ei->lineno = 0;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004994
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004995 ei->prev = istk->expansion;
4996 istk->expansion = ei;
4997
4998 /*
4999 * Special case: detect %00 on first invocation; if found,
5000 * avoid emitting any labels that precede the mmacro call.
5001 * ed->prepend is set to -1 when %00 is detected, else 1.
5002 */
5003 if (ed->prepend == 0) {
5004 for (l = ed->line; l != NULL; l = l->next) {
5005 for (t = l->first; t != NULL; t = t->next) {
5006 if ((t->type == TOK_PREPROC_ID) &&
5007 (strlen(t->text) == 3) &&
5008 (t->text[1] == '0') && (t->text[2] == '0')) {
5009 dont_prepend = -1;
5010 break;
5011 }
5012 }
5013 if (dont_prepend < 0) {
5014 break;
5015 }
5016 }
5017 ed->prepend = ((dont_prepend < 0) ? -1 : 1);
5018 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005019
5020 /*
H. Peter Anvineba20a72002-04-30 20:53:55 +00005021 * If we had a label, push it on as the first line of
5022 * the macro expansion.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005023 */
Keith Kaniosb307a4f2010-11-06 17:41:51 -05005024 if (label != NULL) {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005025 if (ed->prepend < 0) {
5026 ei->label_text = detoken(label, false);
5027 } else {
5028 if (dont_prepend == 0) {
5029 t = label;
5030 while (t->next != NULL) {
5031 t = t->next;
5032 }
5033 t->next = new_Token(NULL, TOK_OTHER, ":", 0);
5034 }
5035 l = new_Line();
5036 l->first = copy_Token(label);
5037 l->next = ei->current;
5038 ei->current = l;
5039 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005040 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005041
Keith Kaniosb307a4f2010-11-06 17:41:51 -05005042 list->uplevel(ed->nolist ? LIST_MACRO_NOLIST : LIST_MACRO);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005043
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005044 istk->mmac_depth++;
Keith Kaniosb307a4f2010-11-06 17:41:51 -05005045 return true;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005046}
5047
Victor van den Elzen3b404c02008-09-18 13:51:36 +02005048/* The function that actually does the error reporting */
5049static void verror(int severity, const char *fmt, va_list arg)
5050{
5051 char buff[1024];
5052
5053 vsnprintf(buff, sizeof(buff), fmt, arg);
5054
Cyrill Gorcunov55cc4d02010-11-10 23:12:06 +03005055 if (istk && istk->mmac_depth > 0) {
5056 ExpInv *ei = istk->expansion;
5057 int lineno = ei->lineno;
5058 while (ei) {
5059 if (ei->type == EXP_MMACRO)
5060 break;
5061 lineno += ei->relno;
5062 ei = ei->prev;
5063 }
Keith Kaniosb307a4f2010-11-06 17:41:51 -05005064 nasm_error(severity, "(%s:%d) %s", ei->def->name,
Cyrill Gorcunov55cc4d02010-11-10 23:12:06 +03005065 lineno, buff);
5066 } else
H. Peter Anvindbb640b2009-07-18 18:57:16 -07005067 nasm_error(severity, "%s", buff);
Victor van den Elzen3b404c02008-09-18 13:51:36 +02005068}
5069
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005070/*
5071 * Since preprocessor always operate only on the line that didn't
H. Peter Anvin917a3492008-09-24 09:14:49 -07005072 * arrived yet, we should always use ERR_OFFBY1.
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005073 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +00005074static void error(int severity, const char *fmt, ...)
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005075{
5076 va_list arg;
H. Peter Anvin734b1882002-04-30 21:01:08 +00005077 va_start(arg, fmt);
Victor van den Elzen3b404c02008-09-18 13:51:36 +02005078 verror(severity, fmt, arg);
H. Peter Anvin734b1882002-04-30 21:01:08 +00005079 va_end(arg);
Victor van den Elzen3b404c02008-09-18 13:51:36 +02005080}
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005081
Victor van den Elzen3b404c02008-09-18 13:51:36 +02005082/*
5083 * Because %else etc are evaluated in the state context
5084 * of the previous branch, errors might get lost with error():
5085 * %if 0 ... %else trailing garbage ... %endif
5086 * So %else etc should report errors with this function.
5087 */
5088static void error_precond(int severity, const char *fmt, ...)
5089{
5090 va_list arg;
5091
5092 /* Only ignore the error if it's really in a dead branch */
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005093 if ((istk != NULL) &&
5094 (istk->expansion != NULL) &&
5095 (istk->expansion->type == EXP_IF) &&
5096 (istk->expansion->def->state == COND_NEVER))
Victor van den Elzen3b404c02008-09-18 13:51:36 +02005097 return;
5098
5099 va_start(arg, fmt);
5100 verror(severity, fmt, arg);
5101 va_end(arg);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005102}
5103
H. Peter Anvin734b1882002-04-30 21:01:08 +00005104static void
H. Peter Anvindbb640b2009-07-18 18:57:16 -07005105pp_reset(char *file, int apass, ListGen * listgen, StrList **deplist)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005106{
H. Peter Anvin7383b402008-09-24 10:20:40 -07005107 Token *t;
5108
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005109 cstk = NULL;
Cyrill Gorcunov6b271292011-02-28 08:45:52 +03005110 istk = nasm_zalloc(sizeof(Include));
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005111 istk->fp = fopen(file, "r");
H. Peter Anvineba20a72002-04-30 20:53:55 +00005112 src_set_fname(nasm_strdup(file));
5113 src_set_linnum(0);
5114 istk->lineinc = 1;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005115 if (!istk->fp)
H. Peter Anvin917a3492008-09-24 09:14:49 -07005116 error(ERR_FATAL|ERR_NOFILE, "unable to open input file `%s'",
H. Peter Anvine2c80182005-01-15 22:15:51 +00005117 file);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005118 defining = NULL;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005119 finals = NULL;
5120 in_final = false;
Charles Crayned4200be2008-07-12 16:42:33 -07005121 nested_mac_count = 0;
5122 nested_rep_count = 0;
H. Peter Anvin97a23472007-09-16 17:57:25 -07005123 init_macros();
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005124 unique = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005125 if (tasm_compatible_mode) {
H. Peter Anvina4835d42008-05-20 14:21:29 -07005126 stdmacpos = nasm_stdmac;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005127 } else {
H. Peter Anvina4835d42008-05-20 14:21:29 -07005128 stdmacpos = nasm_stdmac_after_tasm;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005129 }
H. Peter Anvind2456592008-06-19 15:04:18 -07005130 any_extrastdmac = extrastdmac && *extrastdmac;
5131 do_predef = true;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005132 list = listgen;
H. Peter Anvin61f130f2008-09-25 15:45:06 -07005133
5134 /*
5135 * 0 for dependencies, 1 for preparatory passes, 2 for final pass.
5136 * The caller, however, will also pass in 3 for preprocess-only so
5137 * we can set __PASS__ accordingly.
5138 */
5139 pass = apass > 2 ? 2 : apass;
5140
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07005141 dephead = deptail = deplist;
5142 if (deplist) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03005143 StrList *sl = nasm_malloc(strlen(file)+1+sizeof sl->next);
5144 sl->next = NULL;
5145 strcpy(sl->str, file);
5146 *deptail = sl;
5147 deptail = &sl->next;
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07005148 }
H. Peter Anvin7383b402008-09-24 10:20:40 -07005149
H. Peter Anvin61f130f2008-09-25 15:45:06 -07005150 /*
5151 * Define the __PASS__ macro. This is defined here unlike
5152 * all the other builtins, because it is special -- it varies between
5153 * passes.
5154 */
Cyrill Gorcunov6b271292011-02-28 08:45:52 +03005155 t = nasm_zalloc(sizeof(*t));
H. Peter Anvin61f130f2008-09-25 15:45:06 -07005156 make_tok_num(t, apass);
H. Peter Anvin7383b402008-09-24 10:20:40 -07005157 define_smacro(NULL, "__PASS__", true, 0, t);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005158}
5159
Keith Kaniosa6dfa782007-04-13 16:47:53 +00005160static char *pp_getline(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005161{
Keith Kaniosa6dfa782007-04-13 16:47:53 +00005162 char *line;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005163 Token *tline;
Keith Kanios9858cec2010-11-08 00:58:02 -06005164 ExpDef *ed;
5165 ExpInv *ei;
5166 Line *l;
5167 int j;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005168
H. Peter Anvine2c80182005-01-15 22:15:51 +00005169 while (1) {
5170 /*
Keith Kaniosb307a4f2010-11-06 17:41:51 -05005171 * Fetch a tokenized line, either from the expansion
H. Peter Anvine2c80182005-01-15 22:15:51 +00005172 * buffer or from the input file.
5173 */
5174 tline = NULL;
H. Peter Anvineba20a72002-04-30 20:53:55 +00005175
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005176 while (1) { /* until we get a line we can use */
5177 /*
5178 * Fetch a tokenized line from the expansion buffer
5179 */
5180 if (istk->expansion != NULL) {
5181 ei = istk->expansion;
5182 if (ei->current != NULL) {
5183 if (ei->emitting == false) {
5184 ei->current = NULL;
5185 continue;
5186 }
5187 l = ei->current;
5188 ei->current = l->next;
5189 ei->lineno++;
5190 tline = copy_Token(l->first);
5191 if (((ei->type == EXP_REP) ||
5192 (ei->type == EXP_MMACRO) ||
5193 (ei->type == EXP_WHILE))
5194 && (ei->def->nolist == false)) {
5195 char *p = detoken(tline, false);
5196 list->line(LIST_MACRO, p);
5197 nasm_free(p);
5198 }
5199 if (ei->linnum > -1) {
5200 src_set_linnum(src_get_linnum() + 1);
5201 }
5202 break;
5203 } else if ((ei->type == EXP_REP) &&
5204 (ei->def->cur_depth < ei->def->max_depth)) {
5205 ei->def->cur_depth ++;
5206 ei->current = ei->def->line;
5207 ei->lineno = 0;
5208 continue;
5209 } else if ((ei->type == EXP_WHILE) &&
5210 (ei->def->cur_depth < ei->def->max_depth)) {
5211 ei->current = ei->def->line;
5212 ei->lineno = 0;
5213 tline = copy_Token(ei->current->first);
5214 j = if_condition(tline, PP_WHILE);
5215 tline = NULL;
5216 j = (((j < 0) ? COND_NEVER : j) ? COND_IF_TRUE : COND_IF_FALSE);
5217 if (j == COND_IF_TRUE) {
5218 ei->current = ei->current->next;
5219 ei->def->cur_depth ++;
5220 } else {
5221 ei->emitting = false;
5222 ei->current = NULL;
5223 ei->def->cur_depth = ei->def->max_depth;
5224 }
5225 continue;
5226 } else {
5227 istk->expansion = ei->prev;
5228 ed = ei->def;
5229 if (ed != NULL) {
5230 if ((ei->emitting == true) &&
5231 (ed->max_depth == DEADMAN_LIMIT) &&
5232 (ed->cur_depth == DEADMAN_LIMIT)
5233 ) {
5234 error(ERR_FATAL, "runaway expansion detected, aborting");
5235 }
5236 if (ed->cur_depth > 0) {
5237 ed->cur_depth --;
Keith Kanios94124652010-12-18 11:47:28 -06005238 } else if (ed->type != EXP_MMACRO) {
Keith Kanios6a7c3e92010-12-18 11:49:53 -06005239 expansions = ed->prev;
5240 free_expdef(ed);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005241 }
5242 if ((ei->type == EXP_REP) ||
5243 (ei->type == EXP_MMACRO) ||
5244 (ei->type == EXP_WHILE)) {
5245 list->downlevel(LIST_MACRO);
5246 if (ei->type == EXP_MMACRO) {
5247 istk->mmac_depth--;
5248 }
5249 }
5250 }
5251 if (ei->linnum > -1) {
5252 src_set_linnum(ei->linnum);
5253 }
5254 free_expinv(ei);
5255 continue;
5256 }
5257 }
5258
5259 /*
5260 * Read in line from input and tokenize
5261 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00005262 line = read_line();
5263 if (line) { /* from the current input file */
5264 line = prepreproc(line);
Keith Kaniosb7a89542007-04-12 02:40:54 +00005265 tline = tokenize(line);
H. Peter Anvine2c80182005-01-15 22:15:51 +00005266 nasm_free(line);
5267 break;
5268 }
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005269
H. Peter Anvine2c80182005-01-15 22:15:51 +00005270 /*
5271 * The current file has ended; work down the istk
5272 */
5273 {
5274 Include *i = istk;
5275 fclose(i->fp);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005276 if (i->expansion != NULL) {
5277 error(ERR_FATAL,
5278 "end of file while still in an expansion");
5279 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00005280 /* only set line and file name if there's a next node */
5281 if (i->next) {
5282 src_set_linnum(i->lineno);
Cyrill Gorcunovd34a1082011-03-07 11:23:08 +03005283 nasm_free(src_set_fname(nasm_strdup(i->fname)));
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03005284 }
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005285 if ((i->next == NULL) && (finals != NULL)) {
5286 in_final = true;
5287 ei = new_ExpInv(EXP_FINAL, NULL);
5288 ei->emitting = true;
5289 ei->current = finals;
5290 istk->expansion = ei;
5291 finals = NULL;
5292 continue;
5293 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00005294 istk = i->next;
5295 list->downlevel(LIST_INCLUDE);
5296 nasm_free(i);
Keith Kaniosb307a4f2010-11-06 17:41:51 -05005297 if (istk == NULL) {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005298 if (finals != NULL) {
5299 in_final = true;
5300 } else {
5301 return NULL;
5302 }
5303 }
5304 continue;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005305 }
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005306 }
5307
5308 if (defining == NULL) {
5309 tline = expand_mmac_params(tline);
5310 }
5311
H. Peter Anvine2c80182005-01-15 22:15:51 +00005312 /*
5313 * Check the line to see if it's a preprocessor directive.
5314 */
5315 if (do_directive(tline) == DIRECTIVE_FOUND) {
5316 continue;
Keith Kaniosb307a4f2010-11-06 17:41:51 -05005317 } else if (defining != NULL) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00005318 /*
Keith Kaniosb307a4f2010-11-06 17:41:51 -05005319 * We're defining an expansion. We emit nothing at all,
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005320 * and just shove the tokenized line on to the definition.
H. Peter Anvine2c80182005-01-15 22:15:51 +00005321 */
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005322 if (defining->ignoring == false) {
5323 Line *l = new_Line();
5324 l->first = tline;
5325 if (defining->line == NULL) {
5326 defining->line = l;
5327 defining->last = l;
5328 } else {
5329 defining->last->next = l;
5330 defining->last = l;
5331 }
5332 } else {
Keith Kanios104803d2010-12-18 11:05:46 -06005333 free_tlist(tline);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005334 }
5335 defining->linecount++;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005336 continue;
Keith Kaniosb307a4f2010-11-06 17:41:51 -05005337 } else if ((istk->expansion != NULL) &&
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005338 (istk->expansion->emitting != true)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00005339 /*
Keith Kaniosb307a4f2010-11-06 17:41:51 -05005340 * We're in a non-emitting branch of an expansion.
H. Peter Anvine2c80182005-01-15 22:15:51 +00005341 * Emit nothing at all, not even a blank line: when we
Keith Kaniosb307a4f2010-11-06 17:41:51 -05005342 * emerge from the expansion we'll give a line-number
H. Peter Anvine2c80182005-01-15 22:15:51 +00005343 * directive so we keep our place correctly.
5344 */
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005345 free_tlist(tline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00005346 continue;
5347 } else {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005348 tline = expand_smacro(tline);
Keith Kaniosb307a4f2010-11-06 17:41:51 -05005349 if (expand_mmacro(tline) != true) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00005350 /*
Keith Kaniosb7a89542007-04-12 02:40:54 +00005351 * De-tokenize the line again, and emit it.
H. Peter Anvine2c80182005-01-15 22:15:51 +00005352 */
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005353 line = detoken(tline, true);
5354 free_tlist(tline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00005355 break;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005356 } else {
5357 continue;
5358 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00005359 }
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005360 }
5361 return line;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005362}
5363
H. Peter Anvine2c80182005-01-15 22:15:51 +00005364static void pp_cleanup(int pass)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005365{
Keith Kaniosb307a4f2010-11-06 17:41:51 -05005366 if (defining != NULL) {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005367 error(ERR_NONFATAL, "end of file while still defining an expansion");
5368 while (defining != NULL) {
5369 ExpDef *ed = defining;
5370 defining = ed->prev;
5371 free_expdef(ed);
5372 }
5373 defining = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005374 }
Keith Kaniosb307a4f2010-11-06 17:41:51 -05005375 while (cstk != NULL)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005376 ctx_pop();
H. Peter Anvin97a23472007-09-16 17:57:25 -07005377 free_macros();
Keith Kaniosb307a4f2010-11-06 17:41:51 -05005378 while (istk != NULL) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00005379 Include *i = istk;
5380 istk = istk->next;
5381 fclose(i->fp);
5382 nasm_free(i->fname);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005383 while (i->expansion != NULL) {
5384 ExpInv *ei = i->expansion;
5385 i->expansion = ei->prev;
5386 free_expinv(ei);
5387 }
Cyrill Gorcunov8dcfd882011-03-03 09:18:56 +03005388 nasm_free(i);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005389 }
5390 while (cstk)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005391 ctx_pop();
H. Peter Anvin86877b22008-06-20 15:55:45 -07005392 nasm_free(src_set_fname(NULL));
H. Peter Anvine2c80182005-01-15 22:15:51 +00005393 if (pass == 0) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03005394 IncPath *i;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005395 free_llist(predef);
5396 delete_Blocks();
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03005397 while ((i = ipath)) {
5398 ipath = i->next;
Cyrill Gorcunovb6c6ca92011-06-28 01:33:02 +04005399 nasm_free(i->path);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03005400 nasm_free(i);
5401 }
H. Peter Anvin11dfa1a2008-07-02 18:11:04 -07005402 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005403}
5404
Keith Kaniosa6dfa782007-04-13 16:47:53 +00005405void pp_include_path(char *path)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005406{
Cyrill Gorcunov6b271292011-02-28 08:45:52 +03005407 IncPath *i = nasm_zalloc(sizeof(IncPath));
H. Peter Anvin37a321f2007-09-24 13:41:58 -07005408
Cyrill Gorcunov6b271292011-02-28 08:45:52 +03005409 if (path)
5410 i->path = nasm_strdup(path);
Frank Kotlerd0ed6fd2003-08-27 11:33:56 +00005411
H. Peter Anvin89cee572009-07-15 09:16:54 -04005412 if (ipath) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00005413 IncPath *j = ipath;
H. Peter Anvin89cee572009-07-15 09:16:54 -04005414 while (j->next)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005415 j = j->next;
5416 j->next = i;
5417 } else {
5418 ipath = i;
Frank Kotlerd0ed6fd2003-08-27 11:33:56 +00005419 }
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005420}
Frank Kotlerd0ed6fd2003-08-27 11:33:56 +00005421
Keith Kaniosa6dfa782007-04-13 16:47:53 +00005422void pp_pre_include(char *fname)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005423{
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005424 Token *inc, *space, *name;
5425 Line *l;
5426
H. Peter Anvin734b1882002-04-30 21:01:08 +00005427 name = new_Token(NULL, TOK_INTERNAL_STRING, fname, 0);
5428 space = new_Token(name, TOK_WHITESPACE, NULL, 0);
5429 inc = new_Token(space, TOK_PREPROC_ID, "%include", 0);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005430
Keith Kaniosb307a4f2010-11-06 17:41:51 -05005431 l = new_Line();
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005432 l->next = predef;
5433 l->first = inc;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005434 predef = l;
5435}
5436
Keith Kaniosa6dfa782007-04-13 16:47:53 +00005437void pp_pre_define(char *definition)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005438{
5439 Token *def, *space;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005440 Line *l;
Keith Kaniosa6dfa782007-04-13 16:47:53 +00005441 char *equals;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005442
5443 equals = strchr(definition, '=');
H. Peter Anvin734b1882002-04-30 21:01:08 +00005444 space = new_Token(NULL, TOK_WHITESPACE, NULL, 0);
5445 def = new_Token(space, TOK_PREPROC_ID, "%define", 0);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005446 if (equals)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005447 *equals = ' ';
Keith Kaniosb7a89542007-04-12 02:40:54 +00005448 space->next = tokenize(definition);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005449 if (equals)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005450 *equals = '=';
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005451
Keith Kaniosb307a4f2010-11-06 17:41:51 -05005452 l = new_Line();
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005453 l->next = predef;
5454 l->first = def;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005455 predef = l;
5456}
5457
Keith Kaniosa6dfa782007-04-13 16:47:53 +00005458void pp_pre_undefine(char *definition)
H. Peter Anvin620515a2002-04-30 20:57:38 +00005459{
5460 Token *def, *space;
5461 Line *l;
5462
H. Peter Anvin734b1882002-04-30 21:01:08 +00005463 space = new_Token(NULL, TOK_WHITESPACE, NULL, 0);
5464 def = new_Token(space, TOK_PREPROC_ID, "%undef", 0);
Keith Kaniosb7a89542007-04-12 02:40:54 +00005465 space->next = tokenize(definition);
H. Peter Anvin620515a2002-04-30 20:57:38 +00005466
Keith Kaniosb307a4f2010-11-06 17:41:51 -05005467 l = new_Line();
H. Peter Anvin620515a2002-04-30 20:57:38 +00005468 l->next = predef;
5469 l->first = def;
H. Peter Anvin620515a2002-04-30 20:57:38 +00005470 predef = l;
5471}
5472
Keith Kaniosb7a89542007-04-12 02:40:54 +00005473/*
Keith Kaniosb7a89542007-04-12 02:40:54 +00005474 * This function is used to assist with "runtime" preprocessor
Keith Kaniosb307a4f2010-11-06 17:41:51 -05005475 * directives, e.g. pp_runtime("%define __BITS__ 64");
Keith Kaniosb7a89542007-04-12 02:40:54 +00005476 *
5477 * ERRORS ARE IGNORED HERE, SO MAKE COMPLETELY SURE THAT YOU
5478 * PASS A VALID STRING TO THIS FUNCTION!!!!!
5479 */
5480
Keith Kaniosa6dfa782007-04-13 16:47:53 +00005481void pp_runtime(char *definition)
Keith Kaniosb7a89542007-04-12 02:40:54 +00005482{
5483 Token *def;
H. Peter Anvin70653092007-10-19 14:42:29 -07005484
Keith Kaniosb7a89542007-04-12 02:40:54 +00005485 def = tokenize(definition);
H. Peter Anvin89cee572009-07-15 09:16:54 -04005486 if (do_directive(def) == NO_DIRECTIVE_FOUND)
Keith Kaniosb7a89542007-04-12 02:40:54 +00005487 free_tlist(def);
H. Peter Anvin70653092007-10-19 14:42:29 -07005488
Keith Kaniosb7a89542007-04-12 02:40:54 +00005489}
5490
H. Peter Anvina70547f2008-07-19 21:44:26 -07005491void pp_extra_stdmac(macros_t *macros)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005492{
H. Peter Anvin76690a12002-04-30 20:52:49 +00005493 extrastdmac = macros;
5494}
5495
Keith Kaniosa5fc6462007-10-13 07:09:22 -07005496static void make_tok_num(Token * tok, int64_t val)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005497{
Keith Kaniosa6dfa782007-04-13 16:47:53 +00005498 char numbuf[20];
Keith Kaniosa5fc6462007-10-13 07:09:22 -07005499 snprintf(numbuf, sizeof(numbuf), "%"PRId64"", val);
H. Peter Anvineba20a72002-04-30 20:53:55 +00005500 tok->text = nasm_strdup(numbuf);
5501 tok->type = TOK_NUMBER;
5502}
5503
Cyrill Gorcunov86b2ad02011-07-01 10:38:25 +04005504struct preproc_ops nasmpp = {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005505 pp_reset,
5506 pp_getline,
5507 pp_cleanup
5508};