blob: 03686f4540354d27d5e8bd4973f9a7939765b7db [file] [log] [blame]
H. Peter Anvin9e6747c2009-06-28 17:13:04 -07001/* ----------------------------------------------------------------------- *
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002 *
3 * Copyright 1996-2010 The NASM Authors - All Rights Reserved
H. Peter Anvin9e6747c2009-06-28 17:13:04 -07004 * See the file AUTHORS included with the NASM distribution for
5 * the specific copyright holders.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00006 *
H. Peter Anvin9e6747c2009-06-28 17:13:04 -07007 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following
9 * conditions are met:
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000010 *
H. Peter Anvin9e6747c2009-06-28 17:13:04 -070011 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above
14 * copyright notice, this list of conditions and the following
15 * disclaimer in the documentation and/or other materials provided
16 * with the distribution.
Cyrill Gorcunovaccda192010-02-16 10:27:56 +030017 *
H. Peter Anvin9e6747c2009-06-28 17:13:04 -070018 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
19 * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
20 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
21 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
23 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
26 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
29 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
30 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 *
32 * ----------------------------------------------------------------------- */
33
34/*
35 * preproc.c macro preprocessor for the Netwide Assembler
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000036 */
37
H. Peter Anvin4836e332002-04-30 20:56:43 +000038/* Typical flow of text through preproc
39 *
Keith Kaniosb7a89542007-04-12 02:40:54 +000040 * pp_getline gets tokenized lines, either
H. Peter Anvin4836e332002-04-30 20:56:43 +000041 *
42 * from a macro expansion
43 *
44 * or
45 * {
46 * read_line gets raw text from stdmacpos, or predef, or current input file
Keith Kaniosb7a89542007-04-12 02:40:54 +000047 * tokenize converts to tokens
H. Peter Anvin4836e332002-04-30 20:56:43 +000048 * }
49 *
50 * expand_mmac_params is used to expand %1 etc., unless a macro is being
51 * defined or a false conditional is being processed
52 * (%0, %1, %+1, %-1, %%foo
53 *
54 * do_directive checks for directives
55 *
56 * expand_smacro is used to expand single line macros
57 *
58 * expand_mmacro is used to expand multi-line macros
59 *
60 * detoken is used to convert the line back to text
61 */
H. Peter Anvineba20a72002-04-30 20:53:55 +000062
H. Peter Anvinfe501952007-10-02 21:53:51 -070063#include "compiler.h"
64
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000065#include <stdio.h>
H. Peter Anvinaf535c12002-04-30 20:59:21 +000066#include <stdarg.h>
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000067#include <stdlib.h>
68#include <stddef.h>
69#include <string.h>
70#include <ctype.h>
H. Peter Anvin76690a12002-04-30 20:52:49 +000071#include <limits.h>
Keith Kaniosb7a89542007-04-12 02:40:54 +000072#include <inttypes.h>
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000073
74#include "nasm.h"
75#include "nasmlib.h"
H. Peter Anvin4169a472007-09-12 01:29:43 +000076#include "preproc.h"
H. Peter Anvin97a23472007-09-16 17:57:25 -070077#include "hashtbl.h"
H. Peter Anvin8cad14b2008-06-01 17:23:51 -070078#include "quote.h"
H. Peter Anvinc2df2822007-10-24 15:29:28 -070079#include "stdscan.h"
H. Peter Anvindbb640b2009-07-18 18:57:16 -070080#include "eval.h"
H. Peter Anvinc2df2822007-10-24 15:29:28 -070081#include "tokens.h"
H. Peter Anvina4835d42008-05-20 14:21:29 -070082#include "tables.h"
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000083
84typedef struct SMacro SMacro;
Keith Kaniosb307a4f2010-11-06 17:41:51 -050085typedef struct ExpDef ExpDef;
86typedef struct ExpInv ExpInv;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000087typedef struct Context Context;
88typedef struct Token Token;
H. Peter Anvince616072002-04-30 21:02:23 +000089typedef struct Blocks Blocks;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000090typedef struct Line Line;
91typedef struct Include Include;
92typedef struct Cond Cond;
H. Peter Anvin6768eb72002-04-30 20:52:26 +000093typedef struct IncPath IncPath;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000094
95/*
H. Peter Anvin97a23472007-09-16 17:57:25 -070096 * Note on the storage of both SMacro and MMacros: the hash table
97 * indexes them case-insensitively, and we then have to go through a
98 * linked list of potential case aliases (and, for MMacros, parameter
99 * ranges); this is to preserve the matching semantics of the earlier
100 * code. If the number of case aliases for a specific macro is a
101 * performance issue, you may want to reconsider your coding style.
102 */
103
104/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000105 * Store the definition of a single-line macro.
106 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000107struct SMacro {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000108 SMacro *next;
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000109 char *name;
H. Peter Anvin70055962007-10-11 00:05:31 -0700110 bool casesense;
H. Peter Anvin16ed4382007-10-11 10:06:19 -0700111 bool in_progress;
H. Peter Anvin70055962007-10-11 00:05:31 -0700112 unsigned int nparam;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000113 Token *expansion;
114};
115
116/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000117 * The context stack is composed of a linked list of these.
118 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000119struct Context {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000120 Context *next;
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000121 char *name;
H. Peter Anvin166c2472008-05-28 12:28:58 -0700122 struct hash_table localmac;
Keith Kaniosb7a89542007-04-12 02:40:54 +0000123 uint32_t number;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000124};
125
126/*
127 * This is the internal form which we break input lines up into.
128 * Typically stored in linked lists.
129 *
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000130 * Note that `type' serves a double meaning: TOK_SMAC_PARAM is not
131 * necessarily used as-is, but is intended to denote the number of
132 * the substituted parameter. So in the definition
133 *
134 * %define a(x,y) ( (x) & ~(y) )
H. Peter Anvin70653092007-10-19 14:42:29 -0700135 *
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000136 * the token representing `x' will have its type changed to
137 * TOK_SMAC_PARAM, but the one representing `y' will be
138 * TOK_SMAC_PARAM+1.
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000139 *
140 * TOK_INTERNAL_STRING is a dirty hack: it's a single string token
141 * which doesn't need quotes around it. Used in the pre-include
142 * mechanism as an alternative to trying to find a sensible type of
143 * quote to use on the filename we were passed.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000144 */
H. Peter Anvinda10e7b2007-09-12 04:18:37 +0000145enum pp_token_type {
146 TOK_NONE = 0, TOK_WHITESPACE, TOK_COMMENT, TOK_ID,
147 TOK_PREPROC_ID, TOK_STRING,
H. Peter Anvin6c81f0a2008-05-25 21:46:17 -0700148 TOK_NUMBER, TOK_FLOAT, TOK_SMAC_END, TOK_OTHER,
149 TOK_INTERNAL_STRING,
150 TOK_PREPROC_Q, TOK_PREPROC_QQ,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300151 TOK_PASTE, /* %+ */
152 TOK_INDIRECT, /* %[...] */
153 TOK_SMAC_PARAM, /* MUST BE LAST IN THE LIST!!! */
154 TOK_MAX = INT_MAX /* Keep compiler from reducing the range */
H. Peter Anvinda10e7b2007-09-12 04:18:37 +0000155};
156
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +0400157#define PP_CONCAT_MASK(x) (1 << (x))
158
Cyrill Gorcunov575d4282010-10-06 00:25:55 +0400159struct tokseq_match {
160 int mask_head;
161 int mask_tail;
162};
163
H. Peter Anvine2c80182005-01-15 22:15:51 +0000164struct Token {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000165 Token *next;
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000166 char *text;
H. Peter Anvinf26e0972008-07-01 21:26:27 -0700167 union {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300168 SMacro *mac; /* associated macro for TOK_SMAC_END */
169 size_t len; /* scratch length field */
170 } a; /* Auxiliary data */
H. Peter Anvinda10e7b2007-09-12 04:18:37 +0000171 enum pp_token_type type;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000172};
173
174/*
Keith Kaniosb307a4f2010-11-06 17:41:51 -0500175 * Expansion definitions are stored as a linked list of
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000176 * these, which is essentially a container to allow several linked
177 * lists of Tokens.
H. Peter Anvin70653092007-10-19 14:42:29 -0700178 *
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000179 * Note that in this module, linked lists are treated as stacks
180 * wherever possible. For this reason, Lines are _pushed_ on to the
Keith Kaniosb307a4f2010-11-06 17:41:51 -0500181 * `last' field in ExpDef structures, so that the linked list,
182 * if walked, would emit the expansion lines in the proper order.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000183 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000184struct Line {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000185 Line *next;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000186 Token *first;
187};
188
189/*
Keith Kaniosb307a4f2010-11-06 17:41:51 -0500190 * Expansion Types
191 */
192enum pp_exp_type {
193 EXP_NONE = 0, EXP_PREDEF,
Cyrill Gorcunova5aea572010-11-11 10:14:45 +0300194 EXP_MMACRO, EXP_REP,
Keith Kaniosb307a4f2010-11-06 17:41:51 -0500195 EXP_IF, EXP_WHILE,
Cyrill Gorcunova5aea572010-11-11 10:14:45 +0300196 EXP_COMMENT, EXP_FINAL,
Keith Kaniosb307a4f2010-11-06 17:41:51 -0500197 EXP_MAX = INT_MAX /* Keep compiler from reducing the range */
198};
199
200/*
201 * Store the definition of an expansion, in which is any
202 * preprocessor directive that has an ending pair.
203 *
204 * This design allows for arbitrary expansion/recursion depth,
205 * upto the DEADMAN_LIMIT.
206 *
207 * The `next' field is used for storing ExpDef in hash tables; the
208 * `prev' field is for the global `expansions` linked-list.
209 */
210struct ExpDef {
211 ExpDef *prev; /* previous definition */
Cyrill Gorcunova5aea572010-11-11 10:14:45 +0300212 ExpDef *next; /* next in hash table */
Keith Kaniosb307a4f2010-11-06 17:41:51 -0500213 enum pp_exp_type type; /* expansion type */
214 char *name; /* definition name */
215 int nparam_min, nparam_max;
216 bool casesense;
217 bool plus; /* is the last parameter greedy? */
218 bool nolist; /* is this expansion listing-inhibited? */
219 Token *dlist; /* all defaults as one list */
220 Token **defaults; /* parameter default pointers */
221 int ndefs; /* number of default parameters */
Cyrill Gorcunova5aea572010-11-11 10:14:45 +0300222
223 int prepend; /* label prepend state */
224 Line *label;
Keith Kaniosb307a4f2010-11-06 17:41:51 -0500225 Line *line;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +0300226 Line *last;
227 int linecount; /* number of lines within expansion */
228
229 int64_t def_depth; /* current number of definition pairs deep */
Keith Kaniosb307a4f2010-11-06 17:41:51 -0500230 int64_t cur_depth; /* current number of expansions */
231 int64_t max_depth; /* maximum number of expansions allowed */
Cyrill Gorcunova5aea572010-11-11 10:14:45 +0300232
233 int state; /* condition state */
234 bool ignoring; /* ignoring definition lines */
Keith Kaniosb307a4f2010-11-06 17:41:51 -0500235};
236
237/*
238 * Store the invocation of an expansion.
239 *
240 * The `prev' field is for the `istk->expansion` linked-list.
241 *
242 * When an expansion is being expanded, `params', `iline', `nparam',
243 * `paramlen', `rotate' and `unique' are local to the invocation.
244 */
245struct ExpInv {
246 ExpInv *prev; /* previous invocation */
247 enum pp_exp_type type; /* expansion type */
248 ExpDef *def; /* pointer to expansion definition */
Cyrill Gorcunova5aea572010-11-11 10:14:45 +0300249 char *name; /* invocation name */
250 Line *label; /* pointer to label */
251 char *label_text; /* pointer to label text */
Keith Kaniosb307a4f2010-11-06 17:41:51 -0500252 Line *current; /* pointer to current line in invocation */
Cyrill Gorcunova5aea572010-11-11 10:14:45 +0300253
Keith Kaniosb307a4f2010-11-06 17:41:51 -0500254 Token **params; /* actual parameters */
255 Token *iline; /* invocation line */
256 unsigned int nparam, rotate;
257 int *paramlen;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +0300258
259 uint64_t unique;
Keith Kaniosb307a4f2010-11-06 17:41:51 -0500260 bool emitting;
261 int lineno; /* current line number in expansion */
Cyrill Gorcunova5aea572010-11-11 10:14:45 +0300262 int linnum; /* line number at invocation */
263 int relno; /* relative line number at invocation */
Keith Kaniosb307a4f2010-11-06 17:41:51 -0500264};
265
266/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000267 * To handle an arbitrary level of file inclusion, we maintain a
268 * stack (ie linked list) of these things.
269 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000270struct Include {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000271 Include *next;
272 FILE *fp;
273 Cond *conds;
Keith Kaniosb307a4f2010-11-06 17:41:51 -0500274 ExpInv *expansion;
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000275 char *fname;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000276 int lineno, lineinc;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +0300277 int mmac_depth;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000278};
279
280/*
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000281 * Include search path. This is simply a list of strings which get
282 * prepended, in turn, to the name of an include file, in an
283 * attempt to find the file if it's not in the current directory.
284 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000285struct IncPath {
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000286 IncPath *next;
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000287 char *path;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000288};
289
290/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000291 * Conditional assembly: we maintain a separate stack of these for
292 * each level of file inclusion. (The only reason we keep the
293 * stacks separate is to ensure that a stray `%endif' in a file
294 * included from within the true branch of a `%if' won't terminate
295 * it and cause confusion: instead, rightly, it'll cause an error.)
296 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000297enum {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000298 /*
299 * These states are for use just after %if or %elif: IF_TRUE
300 * means the condition has evaluated to truth so we are
301 * currently emitting, whereas IF_FALSE means we are not
302 * currently emitting but will start doing so if a %else comes
303 * up. In these states, all directives are admissible: %elif,
304 * %else and %endif. (And of course %if.)
305 */
306 COND_IF_TRUE, COND_IF_FALSE,
307 /*
308 * These states come up after a %else: ELSE_TRUE means we're
309 * emitting, and ELSE_FALSE means we're not. In ELSE_* states,
310 * any %elif or %else will cause an error.
311 */
312 COND_ELSE_TRUE, COND_ELSE_FALSE,
313 /*
Victor van den Elzen3b404c02008-09-18 13:51:36 +0200314 * These states mean that we're not emitting now, and also that
315 * nothing until %endif will be emitted at all. COND_DONE is
316 * used when we've had our moment of emission
317 * and have now started seeing %elifs. COND_NEVER is used when
318 * the condition construct in question is contained within a
319 * non-emitting branch of a larger condition construct,
320 * or if there is an error.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000321 */
Victor van den Elzen3b404c02008-09-18 13:51:36 +0200322 COND_DONE, COND_NEVER
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000323};
324#define emitting(x) ( (x) == COND_IF_TRUE || (x) == COND_ELSE_TRUE )
325
H. Peter Anvin70653092007-10-19 14:42:29 -0700326/*
Ed Beroset3ab3f412002-06-11 03:31:49 +0000327 * These defines are used as the possible return values for do_directive
328 */
329#define NO_DIRECTIVE_FOUND 0
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300330#define DIRECTIVE_FOUND 1
Ed Beroset3ab3f412002-06-11 03:31:49 +0000331
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000332/*
Keith Kaniosb307a4f2010-11-06 17:41:51 -0500333 * This define sets the upper limit for smacro and expansions
Keith Kanios852f1ee2009-07-12 00:19:55 -0500334 */
335#define DEADMAN_LIMIT (1 << 20)
336
Cyrill Gorcunove091d6e2010-08-09 13:58:22 +0400337/* max reps */
338#define REP_LIMIT ((INT64_C(1) << 62))
339
Keith Kanios852f1ee2009-07-12 00:19:55 -0500340/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000341 * Condition codes. Note that we use c_ prefix not C_ because C_ is
342 * used in nasm.h for the "real" condition codes. At _this_ level,
343 * we treat CXZ and ECXZ as condition codes, albeit non-invertible
344 * ones, so we need a different enum...
345 */
H. Peter Anvin476d2862007-10-02 22:04:15 -0700346static const char * const conditions[] = {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000347 "a", "ae", "b", "be", "c", "cxz", "e", "ecxz", "g", "ge", "l", "le",
348 "na", "nae", "nb", "nbe", "nc", "ne", "ng", "nge", "nl", "nle", "no",
H. Peter Anvince9be342007-09-12 00:22:29 +0000349 "np", "ns", "nz", "o", "p", "pe", "po", "rcxz", "s", "z"
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000350};
H. Peter Anvin476d2862007-10-02 22:04:15 -0700351enum pp_conds {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000352 c_A, c_AE, c_B, c_BE, c_C, c_CXZ, c_E, c_ECXZ, c_G, c_GE, c_L, c_LE,
353 c_NA, c_NAE, c_NB, c_NBE, c_NC, c_NE, c_NG, c_NGE, c_NL, c_NLE, c_NO,
H. Peter Anvin476d2862007-10-02 22:04:15 -0700354 c_NP, c_NS, c_NZ, c_O, c_P, c_PE, c_PO, c_RCXZ, c_S, c_Z,
355 c_none = -1
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000356};
H. Peter Anvin476d2862007-10-02 22:04:15 -0700357static const enum pp_conds inverse_ccs[] = {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000358 c_NA, c_NAE, c_NB, c_NBE, c_NC, -1, c_NE, -1, c_NG, c_NGE, c_NL, c_NLE,
359 c_A, c_AE, c_B, c_BE, c_C, c_E, c_G, c_GE, c_L, c_LE, c_O, c_P, c_S,
H. Peter Anvince9be342007-09-12 00:22:29 +0000360 c_Z, c_NO, c_NP, c_PO, c_PE, -1, c_NS, c_NZ
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000361};
362
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000363/* For TASM compatibility we need to be able to recognise TASM compatible
364 * conditional compilation directives. Using the NASM pre-processor does
365 * not work, so we look for them specifically from the following list and
366 * then jam in the equivalent NASM directive into the input stream.
367 */
368
H. Peter Anvine2c80182005-01-15 22:15:51 +0000369enum {
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000370 TM_ARG, TM_ELIF, TM_ELSE, TM_ENDIF, TM_IF, TM_IFDEF, TM_IFDIFI,
371 TM_IFNDEF, TM_INCLUDE, TM_LOCAL
372};
373
H. Peter Anvin476d2862007-10-02 22:04:15 -0700374static const char * const tasm_directives[] = {
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000375 "arg", "elif", "else", "endif", "if", "ifdef", "ifdifi",
376 "ifndef", "include", "local"
377};
378
379static int StackSize = 4;
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000380static char *StackPointer = "ebp";
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000381static int ArgOffset = 8;
H. Peter Anvin8781cb02007-11-08 20:01:11 -0800382static int LocalOffset = 0;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000383
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000384static Context *cstk;
385static Include *istk;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000386static IncPath *ipath = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000387
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300388static int pass; /* HACK: pass 0 = generate dependencies only */
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700389static StrList **dephead, **deptail; /* Dependency list */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000390
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300391static uint64_t unique; /* unique identifier numbers */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000392
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000393static Line *predef = NULL;
H. Peter Anvind2456592008-06-19 15:04:18 -0700394static bool do_predef;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000395
396static ListGen *list;
397
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000398/*
Keith Kaniosb307a4f2010-11-06 17:41:51 -0500399 * The current set of expansion definitions we have defined.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000400 */
Keith Kaniosb307a4f2010-11-06 17:41:51 -0500401static struct hash_table expdefs;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000402
403/*
404 * The current set of single-line macros we have defined.
405 */
H. Peter Anvin166c2472008-05-28 12:28:58 -0700406static struct hash_table smacros;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000407
408/*
Keith Kaniosb307a4f2010-11-06 17:41:51 -0500409 * Linked List of all active expansion definitions
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000410 */
Keith Kaniosb307a4f2010-11-06 17:41:51 -0500411struct ExpDef *expansions = NULL;
412
413/*
414 * The expansion we are currently defining
415 */
416static ExpDef *defining = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000417
Charles Crayned4200be2008-07-12 16:42:33 -0700418static uint64_t nested_mac_count;
419static uint64_t nested_rep_count;
420
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000421/*
Keith Kaniosb307a4f2010-11-06 17:41:51 -0500422 * Linked-list of lines to preprocess, prior to cleanup
423 */
424static Line *finals = NULL;
425static bool in_final = false;
426
427/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000428 * The number of macro parameters to allocate space for at a time.
429 */
430#define PARAM_DELTA 16
431
432/*
H. Peter Anvina4835d42008-05-20 14:21:29 -0700433 * The standard macro set: defined in macros.c in the array nasm_stdmac.
434 * This gives our position in the macro set, when we're processing it.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000435 */
H. Peter Anvina70547f2008-07-19 21:44:26 -0700436static macros_t *stdmacpos;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000437
438/*
H. Peter Anvin76690a12002-04-30 20:52:49 +0000439 * The extra standard macros that come from the object format, if
440 * any.
441 */
H. Peter Anvina70547f2008-07-19 21:44:26 -0700442static macros_t *extrastdmac = NULL;
H. Peter Anvincfb71762008-06-20 15:20:16 -0700443static bool any_extrastdmac;
H. Peter Anvin76690a12002-04-30 20:52:49 +0000444
445/*
H. Peter Anvin734b1882002-04-30 21:01:08 +0000446 * Tokens are allocated in blocks to improve speed
447 */
448#define TOKEN_BLOCKSIZE 4096
449static Token *freeTokens = NULL;
H. Peter Anvince616072002-04-30 21:02:23 +0000450struct Blocks {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000451 Blocks *next;
452 void *chunk;
H. Peter Anvince616072002-04-30 21:02:23 +0000453};
454
455static Blocks blocks = { NULL, NULL };
H. Peter Anvin734b1882002-04-30 21:01:08 +0000456
457/*
H. Peter Anvin76690a12002-04-30 20:52:49 +0000458 * Forward declarations.
459 */
H. Peter Anvin734b1882002-04-30 21:01:08 +0000460static Token *expand_mmac_params(Token * tline);
461static Token *expand_smacro(Token * tline);
462static Token *expand_id(Token * tline);
H. Peter Anvinf8ad5322009-02-21 17:55:08 -0800463static Context *get_ctx(const char *name, const char **namep,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300464 bool all_contexts);
Keith Kaniosa5fc6462007-10-13 07:09:22 -0700465static void make_tok_num(Token * tok, int64_t val);
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000466static void error(int severity, const char *fmt, ...);
Victor van den Elzen3b404c02008-09-18 13:51:36 +0200467static void error_precond(int severity, const char *fmt, ...);
H. Peter Anvince616072002-04-30 21:02:23 +0000468static void *new_Block(size_t size);
469static void delete_Blocks(void);
H. Peter Anvinc751e862008-06-09 10:18:45 -0700470static Token *new_Token(Token * next, enum pp_token_type type,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300471 const char *text, int txtlen);
Keith Kaniosb307a4f2010-11-06 17:41:51 -0500472static Token *copy_Token(Token * tline);
H. Peter Anvin734b1882002-04-30 21:01:08 +0000473static Token *delete_Token(Token * t);
Keith Kaniosb307a4f2010-11-06 17:41:51 -0500474static Line *new_Line(void);
475static ExpDef *new_ExpDef(int exp_type);
476static ExpInv *new_ExpInv(int exp_type, ExpDef *ed);
H. Peter Anvineba20a72002-04-30 20:53:55 +0000477
478/*
479 * Macros for safe checking of token pointers, avoid *(NULL)
480 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300481#define tok_type_(x,t) ((x) && (x)->type == (t))
482#define skip_white_(x) if (tok_type_((x), TOK_WHITESPACE)) (x)=(x)->next
483#define tok_is_(x,v) (tok_type_((x), TOK_OTHER) && !strcmp((x)->text,(v)))
484#define tok_isnt_(x,v) ((x) && ((x)->type!=TOK_OTHER || strcmp((x)->text,(v))))
H. Peter Anvin76690a12002-04-30 20:52:49 +0000485
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300486/*
H. Peter Anvin077fb932010-07-20 14:56:30 -0700487 * nasm_unquote with error if the string contains NUL characters.
488 * If the string contains NUL characters, issue an error and return
489 * the C len, i.e. truncate at the NUL.
490 */
491static size_t nasm_unquote_cstr(char *qstr, enum preproc_token directive)
492{
493 size_t len = nasm_unquote(qstr, NULL);
494 size_t clen = strlen(qstr);
495
496 if (len != clen)
497 error(ERR_NONFATAL, "NUL character in `%s' directive",
498 pp_directives[directive]);
499
500 return clen;
501}
502
503/*
H. Peter Anvinb40992c2010-09-15 08:57:21 -0700504 * In-place reverse a list of tokens.
505 */
506static Token *reverse_tokens(Token *t)
507{
508 Token *prev = NULL;
509 Token *next;
510
511 while (t) {
Keith Kaniosb307a4f2010-11-06 17:41:51 -0500512 next = t->next;
513 t->next = prev;
514 prev = t;
515 t = next;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +0300516 }
H. Peter Anvinb40992c2010-09-15 08:57:21 -0700517
518 return prev;
519}
520
521/*
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300522 * Handle TASM specific directives, which do not contain a % in
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000523 * front of them. We do it here because I could not find any other
524 * place to do it for the moment, and it is a hack (ideally it would
525 * be nice to be able to use the NASM pre-processor to do it).
526 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000527static char *check_tasm_directive(char *line)
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000528{
Keith Kaniosb7a89542007-04-12 02:40:54 +0000529 int32_t i, j, k, m, len;
Cyrill Gorcunovf66ac7d2009-10-12 20:41:13 +0400530 char *p, *q, *oldline, oldchar;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000531
Cyrill Gorcunovf66ac7d2009-10-12 20:41:13 +0400532 p = nasm_skip_spaces(line);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000533
534 /* Binary search for the directive name */
535 i = -1;
Cyrill Gorcunova7319242010-06-03 22:04:36 +0400536 j = ARRAY_SIZE(tasm_directives);
Cyrill Gorcunovf66ac7d2009-10-12 20:41:13 +0400537 q = nasm_skip_word(p);
538 len = q - p;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000539 if (len) {
540 oldchar = p[len];
541 p[len] = 0;
542 while (j - i > 1) {
543 k = (j + i) / 2;
544 m = nasm_stricmp(p, tasm_directives[k]);
545 if (m == 0) {
546 /* We have found a directive, so jam a % in front of it
547 * so that NASM will then recognise it as one if it's own.
548 */
549 p[len] = oldchar;
550 len = strlen(p);
551 oldline = line;
552 line = nasm_malloc(len + 2);
553 line[0] = '%';
554 if (k == TM_IFDIFI) {
H. Peter Anvin18f48792009-06-27 15:56:27 -0700555 /*
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300556 * NASM does not recognise IFDIFI, so we convert
557 * it to %if 0. This is not used in NASM
558 * compatible code, but does need to parse for the
559 * TASM macro package.
H. Peter Anvine2c80182005-01-15 22:15:51 +0000560 */
H. Peter Anvin18f48792009-06-27 15:56:27 -0700561 strcpy(line + 1, "if 0");
H. Peter Anvine2c80182005-01-15 22:15:51 +0000562 } else {
563 memcpy(line + 1, p, len + 1);
564 }
565 nasm_free(oldline);
566 return line;
567 } else if (m < 0) {
568 j = k;
569 } else
570 i = k;
571 }
572 p[len] = oldchar;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000573 }
574 return line;
575}
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000576
H. Peter Anvin76690a12002-04-30 20:52:49 +0000577/*
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000578 * The pre-preprocessing stage... This function translates line
579 * number indications as they emerge from GNU cpp (`# lineno "file"
580 * flags') into NASM preprocessor line number indications (`%line
581 * lineno file').
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000582 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000583static char *prepreproc(char *line)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000584{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000585 int lineno, fnlen;
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000586 char *fname, *oldline;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000587
H. Peter Anvine2c80182005-01-15 22:15:51 +0000588 if (line[0] == '#' && line[1] == ' ') {
589 oldline = line;
590 fname = oldline + 2;
591 lineno = atoi(fname);
592 fname += strspn(fname, "0123456789 ");
593 if (*fname == '"')
594 fname++;
595 fnlen = strcspn(fname, "\"");
596 line = nasm_malloc(20 + fnlen);
597 snprintf(line, 20 + fnlen, "%%line %d %.*s", lineno, fnlen, fname);
598 nasm_free(oldline);
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000599 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000600 if (tasm_compatible_mode)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000601 return check_tasm_directive(line);
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000602 return line;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000603}
604
605/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000606 * Free a linked list of tokens.
607 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000608static void free_tlist(Token * list)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000609{
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +0400610 while (list)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000611 list = delete_Token(list);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000612}
613
614/*
615 * Free a linked list of lines.
616 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000617static void free_llist(Line * list)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000618{
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +0400619 Line *l, *tmp;
620 list_for_each_safe(l, tmp, list) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000621 free_tlist(l->first);
622 nasm_free(l);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000623 }
624}
625
626/*
Keith Kaniosb307a4f2010-11-06 17:41:51 -0500627 * Free an ExpDef
H. Peter Anvineba20a72002-04-30 20:53:55 +0000628 */
Keith Kaniosb307a4f2010-11-06 17:41:51 -0500629static void free_expdef(ExpDef * ed)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000630{
Keith Kaniosb307a4f2010-11-06 17:41:51 -0500631 nasm_free(ed->name);
632 free_tlist(ed->dlist);
633 nasm_free(ed->defaults);
634 free_llist(ed->line);
635 nasm_free(ed);
636}
637
638/*
639 * Free an ExpInv
640 */
641static void free_expinv(ExpInv * ei)
642{
Cyrill Gorcunova5aea572010-11-11 10:14:45 +0300643 if (ei->name != NULL)
644 nasm_free(ei->name);
645 if (ei->label_text != NULL)
646 nasm_free(ei->label_text);
647 nasm_free(ei);
H. Peter Anvineba20a72002-04-30 20:53:55 +0000648}
649
650/*
H. Peter Anvin97a23472007-09-16 17:57:25 -0700651 * Free all currently defined macros, and free the hash tables
652 */
H. Peter Anvin072771e2008-05-22 13:17:51 -0700653static void free_smacro_table(struct hash_table *smt)
H. Peter Anvin97a23472007-09-16 17:57:25 -0700654{
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +0400655 SMacro *s, *tmp;
H. Peter Anvin072771e2008-05-22 13:17:51 -0700656 const char *key;
657 struct hash_tbl_node *it = NULL;
H. Peter Anvin97a23472007-09-16 17:57:25 -0700658
H. Peter Anvin072771e2008-05-22 13:17:51 -0700659 while ((s = hash_iterate(smt, &it, &key)) != NULL) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300660 nasm_free((void *)key);
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +0400661 list_for_each_safe(s, tmp, s) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300662 nasm_free(s->name);
663 free_tlist(s->expansion);
664 nasm_free(s);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300665 }
H. Peter Anvin97a23472007-09-16 17:57:25 -0700666 }
H. Peter Anvin072771e2008-05-22 13:17:51 -0700667 hash_free(smt);
668}
669
Keith Kaniosb307a4f2010-11-06 17:41:51 -0500670static void free_expdef_table(struct hash_table *edt)
H. Peter Anvin072771e2008-05-22 13:17:51 -0700671{
Keith Kaniosb307a4f2010-11-06 17:41:51 -0500672 ExpDef *ed, *tmp;
H. Peter Anvin072771e2008-05-22 13:17:51 -0700673 const char *key;
674 struct hash_tbl_node *it = NULL;
H. Peter Anvin97a23472007-09-16 17:57:25 -0700675
676 it = NULL;
Keith Kaniosb307a4f2010-11-06 17:41:51 -0500677 while ((ed = hash_iterate(edt, &it, &key)) != NULL) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300678 nasm_free((void *)key);
Keith Kaniosb307a4f2010-11-06 17:41:51 -0500679 list_for_each_safe(ed ,tmp, ed)
680 free_expdef(ed);
H. Peter Anvin97a23472007-09-16 17:57:25 -0700681 }
Keith Kaniosb307a4f2010-11-06 17:41:51 -0500682 hash_free(edt);
H. Peter Anvin072771e2008-05-22 13:17:51 -0700683}
684
685static void free_macros(void)
686{
H. Peter Anvin166c2472008-05-28 12:28:58 -0700687 free_smacro_table(&smacros);
Keith Kaniosb307a4f2010-11-06 17:41:51 -0500688 free_expdef_table(&expdefs);
H. Peter Anvin97a23472007-09-16 17:57:25 -0700689}
690
691/*
692 * Initialize the hash tables
693 */
694static void init_macros(void)
695{
H. Peter Anvin166c2472008-05-28 12:28:58 -0700696 hash_init(&smacros, HASH_LARGE);
Keith Kaniosb307a4f2010-11-06 17:41:51 -0500697 hash_init(&expdefs, HASH_LARGE);
H. Peter Anvin97a23472007-09-16 17:57:25 -0700698}
699
700/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000701 * Pop the context stack.
702 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000703static void ctx_pop(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000704{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000705 Context *c = cstk;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000706
707 cstk = cstk->next;
H. Peter Anvin166c2472008-05-28 12:28:58 -0700708 free_smacro_table(&c->localmac);
H. Peter Anvin734b1882002-04-30 21:01:08 +0000709 nasm_free(c->name);
710 nasm_free(c);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000711}
712
H. Peter Anvin072771e2008-05-22 13:17:51 -0700713/*
714 * Search for a key in the hash index; adding it if necessary
715 * (in which case we initialize the data pointer to NULL.)
716 */
717static void **
718hash_findi_add(struct hash_table *hash, const char *str)
719{
720 struct hash_insert hi;
721 void **r;
722 char *strx;
723
724 r = hash_findi(hash, str, &hi);
725 if (r)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300726 return r;
H. Peter Anvin072771e2008-05-22 13:17:51 -0700727
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300728 strx = nasm_strdup(str); /* Use a more efficient allocator here? */
H. Peter Anvin072771e2008-05-22 13:17:51 -0700729 return hash_add(&hi, strx, NULL);
730}
731
732/*
733 * Like hash_findi, but returns the data element rather than a pointer
734 * to it. Used only when not adding a new element, hence no third
735 * argument.
736 */
737static void *
738hash_findix(struct hash_table *hash, const char *str)
739{
740 void **p;
741
742 p = hash_findi(hash, str, NULL);
743 return p ? *p : NULL;
744}
745
Cyrill Gorcunov15bdc512010-07-13 11:27:41 +0400746/*
Keith Kaniosb307a4f2010-11-06 17:41:51 -0500747 * read line from standard macros set,
Cyrill Gorcunov15bdc512010-07-13 11:27:41 +0400748 * if there no more left -- return NULL
749 */
750static char *line_from_stdmac(void)
751{
752 unsigned char c;
753 const unsigned char *p = stdmacpos;
754 char *line, *q;
755 size_t len = 0;
756
757 if (!stdmacpos)
758 return NULL;
759
760 while ((c = *p++)) {
761 if (c >= 0x80)
762 len += pp_directives_len[c - 0x80] + 1;
763 else
764 len++;
765 }
766
767 line = nasm_malloc(len + 1);
768 q = line;
769 while ((c = *stdmacpos++)) {
770 if (c >= 0x80) {
771 memcpy(q, pp_directives[c - 0x80], pp_directives_len[c - 0x80]);
772 q += pp_directives_len[c - 0x80];
773 *q++ = ' ';
774 } else {
775 *q++ = c;
776 }
777 }
778 stdmacpos = p;
779 *q = '\0';
780
781 if (!*stdmacpos) {
782 /* This was the last of the standard macro chain... */
783 stdmacpos = NULL;
784 if (any_extrastdmac) {
785 stdmacpos = extrastdmac;
786 any_extrastdmac = false;
787 } else if (do_predef) {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +0300788 ExpInv *ei;
Cyrill Gorcunov15bdc512010-07-13 11:27:41 +0400789 Line *pd, *l;
790 Token *head, **tail, *t;
791
792 /*
793 * Nasty hack: here we push the contents of
794 * `predef' on to the top-level expansion stack,
795 * since this is the most convenient way to
796 * implement the pre-include and pre-define
797 * features.
798 */
799 list_for_each(pd, predef) {
800 head = NULL;
801 tail = &head;
802 list_for_each(t, pd->first) {
803 *tail = new_Token(NULL, t->type, t->text, 0);
804 tail = &(*tail)->next;
805 }
806
Keith Kaniosb307a4f2010-11-06 17:41:51 -0500807 l = new_Line();
808 l->first = head;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +0300809 ei = new_ExpInv(EXP_PREDEF, NULL);
810 ei->current = l;
811 ei->emitting = true;
812 ei->prev = istk->expansion;
Keith Kaniosb307a4f2010-11-06 17:41:51 -0500813 istk->expansion = ei;
Cyrill Gorcunov15bdc512010-07-13 11:27:41 +0400814 }
815 do_predef = false;
816 }
817 }
818
819 return line;
820}
821
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000822#define BUF_DELTA 512
823/*
824 * Read a line from the top file in istk, handling multiple CR/LFs
825 * at the end of the line read, and handling spurious ^Zs. Will
826 * return lines from the standard macro set if this has not already
827 * been done.
828 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000829static char *read_line(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000830{
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000831 char *buffer, *p, *q;
H. Peter Anvin9f394642002-04-30 21:07:51 +0000832 int bufsize, continued_count;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000833
Cyrill Gorcunov15bdc512010-07-13 11:27:41 +0400834 /*
835 * standart macros set (predefined) goes first
836 */
837 p = line_from_stdmac();
838 if (p)
839 return p;
H. Peter Anvin72edbb82008-06-19 16:00:04 -0700840
Cyrill Gorcunov15bdc512010-07-13 11:27:41 +0400841 /*
842 * regular read from a file
843 */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000844 bufsize = BUF_DELTA;
845 buffer = nasm_malloc(BUF_DELTA);
846 p = buffer;
H. Peter Anvin9f394642002-04-30 21:07:51 +0000847 continued_count = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000848 while (1) {
849 q = fgets(p, bufsize - (p - buffer), istk->fp);
850 if (!q)
851 break;
852 p += strlen(p);
853 if (p > buffer && p[-1] == '\n') {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300854 /*
855 * Convert backslash-CRLF line continuation sequences into
856 * nothing at all (for DOS and Windows)
857 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000858 if (((p - 2) > buffer) && (p[-3] == '\\') && (p[-2] == '\r')) {
859 p -= 3;
860 *p = 0;
861 continued_count++;
862 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300863 /*
864 * Also convert backslash-LF line continuation sequences into
865 * nothing at all (for Unix)
866 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000867 else if (((p - 1) > buffer) && (p[-2] == '\\')) {
868 p -= 2;
869 *p = 0;
870 continued_count++;
871 } else {
872 break;
873 }
874 }
875 if (p - buffer > bufsize - 10) {
Keith Kaniosb7a89542007-04-12 02:40:54 +0000876 int32_t offset = p - buffer;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000877 bufsize += BUF_DELTA;
878 buffer = nasm_realloc(buffer, bufsize);
879 p = buffer + offset; /* prevent stale-pointer problems */
880 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000881 }
882
H. Peter Anvine2c80182005-01-15 22:15:51 +0000883 if (!q && p == buffer) {
884 nasm_free(buffer);
885 return NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000886 }
887
Cyrill Gorcunova5aea572010-11-11 10:14:45 +0300888 src_set_linnum(src_get_linnum() + istk->lineinc +
889 (continued_count * istk->lineinc));
H. Peter Anvineba20a72002-04-30 20:53:55 +0000890
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000891 /*
892 * Play safe: remove CRs as well as LFs, if any of either are
893 * present at the end of the line.
894 */
H. Peter Anvineba20a72002-04-30 20:53:55 +0000895 while (--p >= buffer && (*p == '\n' || *p == '\r'))
H. Peter Anvine2c80182005-01-15 22:15:51 +0000896 *p = '\0';
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000897
898 /*
899 * Handle spurious ^Z, which may be inserted into source files
900 * by some file transfer utilities.
901 */
902 buffer[strcspn(buffer, "\032")] = '\0';
903
H. Peter Anvin734b1882002-04-30 21:01:08 +0000904 list->line(LIST_READ, buffer);
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000905
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000906 return buffer;
907}
908
909/*
Keith Kaniosb7a89542007-04-12 02:40:54 +0000910 * Tokenize a line of text. This is a very simple process since we
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000911 * don't need to parse the value out of e.g. numeric tokens: we
912 * simply split one string into many.
913 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000914static Token *tokenize(char *line)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000915{
H. Peter Anvinca544db2008-10-19 19:30:11 -0700916 char c, *p = line;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +0000917 enum pp_token_type type;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000918 Token *list = NULL;
919 Token *t, **tail = &list;
920
H. Peter Anvine2c80182005-01-15 22:15:51 +0000921 while (*line) {
922 p = line;
923 if (*p == '%') {
924 p++;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300925 if (*p == '+' && !nasm_isdigit(p[1])) {
926 p++;
927 type = TOK_PASTE;
928 } else if (nasm_isdigit(*p) ||
929 ((*p == '-' || *p == '+') && nasm_isdigit(p[1]))) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000930 do {
931 p++;
932 }
H. Peter Anvinbda7a6e2008-06-21 10:23:17 -0700933 while (nasm_isdigit(*p));
H. Peter Anvine2c80182005-01-15 22:15:51 +0000934 type = TOK_PREPROC_ID;
935 } else if (*p == '{') {
936 p++;
Keith Kaniosb307a4f2010-11-06 17:41:51 -0500937 while (*p && *p != '}') {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000938 p[-1] = *p;
939 p++;
940 }
941 p[-1] = '\0';
942 if (*p)
943 p++;
944 type = TOK_PREPROC_ID;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300945 } else if (*p == '[') {
946 int lvl = 1;
947 line += 2; /* Skip the leading %[ */
948 p++;
949 while (lvl && (c = *p++)) {
950 switch (c) {
951 case ']':
952 lvl--;
953 break;
954 case '%':
955 if (*p == '[')
956 lvl++;
957 break;
958 case '\'':
959 case '\"':
960 case '`':
Cyrill Gorcunovc6360a72010-07-13 13:32:19 +0400961 p = nasm_skip_string(p - 1) + 1;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300962 break;
963 default:
964 break;
965 }
966 }
967 p--;
968 if (*p)
969 *p++ = '\0';
970 if (lvl)
971 error(ERR_NONFATAL, "unterminated %[ construct");
972 type = TOK_INDIRECT;
973 } else if (*p == '?') {
974 type = TOK_PREPROC_Q; /* %? */
975 p++;
976 if (*p == '?') {
977 type = TOK_PREPROC_QQ; /* %?? */
978 p++;
979 }
Cyrill Gorcunov84b4cba2010-09-12 21:39:40 +0400980 } else if (*p == '!') {
981 type = TOK_PREPROC_ID;
982 p++;
983 if (isidchar(*p)) {
984 do {
985 p++;
986 } while (isidchar(*p));
987 } else if (*p == '\'' || *p == '\"' || *p == '`') {
988 p = nasm_skip_string(p);
989 if (*p)
990 p++;
991 else
992 error(ERR_NONFATAL|ERR_PASS1, "unterminated %! string");
993 } else {
994 /* %! without string or identifier */
995 type = TOK_OTHER; /* Legacy behavior... */
996 }
H. Peter Anvine2c80182005-01-15 22:15:51 +0000997 } else if (isidchar(*p) ||
998 ((*p == '!' || *p == '%' || *p == '$') &&
999 isidchar(p[1]))) {
1000 do {
1001 p++;
1002 }
1003 while (isidchar(*p));
1004 type = TOK_PREPROC_ID;
1005 } else {
1006 type = TOK_OTHER;
1007 if (*p == '%')
1008 p++;
1009 }
1010 } else if (isidstart(*p) || (*p == '$' && isidstart(p[1]))) {
1011 type = TOK_ID;
1012 p++;
1013 while (*p && isidchar(*p))
1014 p++;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07001015 } else if (*p == '\'' || *p == '"' || *p == '`') {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001016 /*
1017 * A string token.
1018 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001019 type = TOK_STRING;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001020 p = nasm_skip_string(p);
Nickolay Yurchenkof3b3ce22003-09-21 20:38:43 +00001021
H. Peter Anvine2c80182005-01-15 22:15:51 +00001022 if (*p) {
1023 p++;
1024 } else {
H. Peter Anvin917a3492008-09-24 09:14:49 -07001025 error(ERR_WARNING|ERR_PASS1, "unterminated string");
H. Peter Anvine2c80182005-01-15 22:15:51 +00001026 /* Handling unterminated strings by UNV */
1027 /* type = -1; */
1028 }
Victor van den Elzenfb5f2512009-04-17 16:17:59 +02001029 } else if (p[0] == '$' && p[1] == '$') {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001030 type = TOK_OTHER; /* TOKEN_BASE */
Victor van den Elzenfb5f2512009-04-17 16:17:59 +02001031 p += 2;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001032 } else if (isnumstart(*p)) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001033 bool is_hex = false;
1034 bool is_float = false;
1035 bool has_e = false;
1036 char c, *r;
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001037
H. Peter Anvine2c80182005-01-15 22:15:51 +00001038 /*
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001039 * A numeric token.
H. Peter Anvine2c80182005-01-15 22:15:51 +00001040 */
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001041
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001042 if (*p == '$') {
1043 p++;
1044 is_hex = true;
1045 }
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001046
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001047 for (;;) {
1048 c = *p++;
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001049
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001050 if (!is_hex && (c == 'e' || c == 'E')) {
1051 has_e = true;
1052 if (*p == '+' || *p == '-') {
1053 /*
1054 * e can only be followed by +/- if it is either a
1055 * prefixed hex number or a floating-point number
1056 */
1057 p++;
1058 is_float = true;
1059 }
1060 } else if (c == 'H' || c == 'h' || c == 'X' || c == 'x') {
1061 is_hex = true;
1062 } else if (c == 'P' || c == 'p') {
1063 is_float = true;
1064 if (*p == '+' || *p == '-')
1065 p++;
1066 } else if (isnumchar(c) || c == '_')
1067 ; /* just advance */
1068 else if (c == '.') {
1069 /*
1070 * we need to deal with consequences of the legacy
1071 * parser, like "1.nolist" being two tokens
1072 * (TOK_NUMBER, TOK_ID) here; at least give it
1073 * a shot for now. In the future, we probably need
1074 * a flex-based scanner with proper pattern matching
1075 * to do it as well as it can be done. Nothing in
1076 * the world is going to help the person who wants
1077 * 0x123.p16 interpreted as two tokens, though.
1078 */
1079 r = p;
1080 while (*r == '_')
1081 r++;
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001082
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001083 if (nasm_isdigit(*r) || (is_hex && nasm_isxdigit(*r)) ||
1084 (!is_hex && (*r == 'e' || *r == 'E')) ||
1085 (*r == 'p' || *r == 'P')) {
1086 p = r;
1087 is_float = true;
1088 } else
1089 break; /* Terminate the token */
1090 } else
1091 break;
1092 }
1093 p--; /* Point to first character beyond number */
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001094
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001095 if (p == line+1 && *line == '$') {
1096 type = TOK_OTHER; /* TOKEN_HERE */
1097 } else {
1098 if (has_e && !is_hex) {
1099 /* 1e13 is floating-point, but 1e13h is not */
1100 is_float = true;
1101 }
H. Peter Anvind784a082009-04-20 14:01:18 -07001102
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001103 type = is_float ? TOK_FLOAT : TOK_NUMBER;
1104 }
H. Peter Anvinbda7a6e2008-06-21 10:23:17 -07001105 } else if (nasm_isspace(*p)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001106 type = TOK_WHITESPACE;
Cyrill Gorcunovf66ac7d2009-10-12 20:41:13 +04001107 p = nasm_skip_spaces(p);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001108 /*
1109 * Whitespace just before end-of-line is discarded by
1110 * pretending it's a comment; whitespace just before a
1111 * comment gets lumped into the comment.
1112 */
1113 if (!*p || *p == ';') {
1114 type = TOK_COMMENT;
1115 while (*p)
1116 p++;
1117 }
1118 } else if (*p == ';') {
1119 type = TOK_COMMENT;
1120 while (*p)
1121 p++;
1122 } else {
1123 /*
1124 * Anything else is an operator of some kind. We check
1125 * for all the double-character operators (>>, <<, //,
1126 * %%, <=, >=, ==, !=, <>, &&, ||, ^^), but anything
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001127 * else is a single-character operator.
H. Peter Anvine2c80182005-01-15 22:15:51 +00001128 */
1129 type = TOK_OTHER;
1130 if ((p[0] == '>' && p[1] == '>') ||
1131 (p[0] == '<' && p[1] == '<') ||
1132 (p[0] == '/' && p[1] == '/') ||
1133 (p[0] == '<' && p[1] == '=') ||
1134 (p[0] == '>' && p[1] == '=') ||
1135 (p[0] == '=' && p[1] == '=') ||
1136 (p[0] == '!' && p[1] == '=') ||
1137 (p[0] == '<' && p[1] == '>') ||
1138 (p[0] == '&' && p[1] == '&') ||
1139 (p[0] == '|' && p[1] == '|') ||
1140 (p[0] == '^' && p[1] == '^')) {
1141 p++;
1142 }
1143 p++;
1144 }
Nickolay Yurchenkof3b3ce22003-09-21 20:38:43 +00001145
H. Peter Anvine2c80182005-01-15 22:15:51 +00001146 /* Handling unterminated string by UNV */
1147 /*if (type == -1)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001148 {
1149 *tail = t = new_Token(NULL, TOK_STRING, line, p-line+1);
1150 t->text[p-line] = *line;
1151 tail = &t->next;
1152 }
1153 else */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001154 if (type != TOK_COMMENT) {
1155 *tail = t = new_Token(NULL, type, line, p - line);
1156 tail = &t->next;
1157 }
1158 line = p;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001159 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001160 return list;
1161}
1162
H. Peter Anvince616072002-04-30 21:02:23 +00001163/*
1164 * this function allocates a new managed block of memory and
H. Peter Anvin70653092007-10-19 14:42:29 -07001165 * returns a pointer to the block. The managed blocks are
H. Peter Anvince616072002-04-30 21:02:23 +00001166 * deleted only all at once by the delete_Blocks function.
1167 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001168static void *new_Block(size_t size)
H. Peter Anvince616072002-04-30 21:02:23 +00001169{
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001170 Blocks *b = &blocks;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001171
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001172 /* first, get to the end of the linked list */
1173 while (b->next)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001174 b = b->next;
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001175 /* now allocate the requested chunk */
1176 b->chunk = nasm_malloc(size);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001177
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001178 /* now allocate a new block for the next request */
1179 b->next = nasm_malloc(sizeof(Blocks));
1180 /* and initialize the contents of the new block */
1181 b->next->next = NULL;
1182 b->next->chunk = NULL;
1183 return b->chunk;
H. Peter Anvince616072002-04-30 21:02:23 +00001184}
1185
1186/*
1187 * this function deletes all managed blocks of memory
1188 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001189static void delete_Blocks(void)
H. Peter Anvince616072002-04-30 21:02:23 +00001190{
H. Peter Anvine2c80182005-01-15 22:15:51 +00001191 Blocks *a, *b = &blocks;
H. Peter Anvince616072002-04-30 21:02:23 +00001192
H. Peter Anvin70653092007-10-19 14:42:29 -07001193 /*
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001194 * keep in mind that the first block, pointed to by blocks
H. Peter Anvin70653092007-10-19 14:42:29 -07001195 * is a static and not dynamically allocated, so we don't
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001196 * free it.
1197 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001198 while (b) {
1199 if (b->chunk)
1200 nasm_free(b->chunk);
1201 a = b;
1202 b = b->next;
1203 if (a != &blocks)
1204 nasm_free(a);
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001205 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001206}
H. Peter Anvin734b1882002-04-30 21:01:08 +00001207
1208/*
H. Peter Anvin70653092007-10-19 14:42:29 -07001209 * this function creates a new Token and passes a pointer to it
H. Peter Anvin734b1882002-04-30 21:01:08 +00001210 * back to the caller. It sets the type and text elements, and
H. Peter Anvinf26e0972008-07-01 21:26:27 -07001211 * also the a.mac and next elements to NULL.
H. Peter Anvin734b1882002-04-30 21:01:08 +00001212 */
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07001213static Token *new_Token(Token * next, enum pp_token_type type,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001214 const char *text, int txtlen)
H. Peter Anvin734b1882002-04-30 21:01:08 +00001215{
1216 Token *t;
1217 int i;
1218
H. Peter Anvin89cee572009-07-15 09:16:54 -04001219 if (!freeTokens) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001220 freeTokens = (Token *) new_Block(TOKEN_BLOCKSIZE * sizeof(Token));
1221 for (i = 0; i < TOKEN_BLOCKSIZE - 1; i++)
1222 freeTokens[i].next = &freeTokens[i + 1];
1223 freeTokens[i].next = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001224 }
1225 t = freeTokens;
1226 freeTokens = t->next;
1227 t->next = next;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07001228 t->a.mac = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001229 t->type = type;
H. Peter Anvin89cee572009-07-15 09:16:54 -04001230 if (type == TOK_WHITESPACE || !text) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001231 t->text = NULL;
1232 } else {
1233 if (txtlen == 0)
1234 txtlen = strlen(text);
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07001235 t->text = nasm_malloc(txtlen+1);
1236 memcpy(t->text, text, txtlen);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001237 t->text[txtlen] = '\0';
H. Peter Anvin734b1882002-04-30 21:01:08 +00001238 }
1239 return t;
1240}
1241
Keith Kaniosb307a4f2010-11-06 17:41:51 -05001242static Token *copy_Token(Token * tline)
1243{
1244 Token *t, *tt, *first = NULL, *prev = NULL;
1245 int i;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03001246 for (tt = tline; tt != NULL; tt = tt->next) {
1247 if (!freeTokens) {
1248 freeTokens = (Token *) new_Block(TOKEN_BLOCKSIZE * sizeof(Token));
1249 for (i = 0; i < TOKEN_BLOCKSIZE - 1; i++)
1250 freeTokens[i].next = &freeTokens[i + 1];
1251 freeTokens[i].next = NULL;
1252 }
1253 t = freeTokens;
1254 freeTokens = t->next;
1255 t->next = NULL;
1256 t->text = tt->text ? nasm_strdup(tt->text) : NULL;
1257 t->a.mac = tt->a.mac;
1258 t->a.len = tt->a.len;
1259 t->type = tt->type;
1260 if (prev != NULL) {
1261 prev->next = t;
1262 } else {
1263 first = t;
1264 }
1265 prev = t;
1266 }
Keith Kaniosb307a4f2010-11-06 17:41:51 -05001267 return first;
1268}
1269
H. Peter Anvine2c80182005-01-15 22:15:51 +00001270static Token *delete_Token(Token * t)
H. Peter Anvin734b1882002-04-30 21:01:08 +00001271{
1272 Token *next = t->next;
1273 nasm_free(t->text);
H. Peter Anvin788e6c12002-04-30 21:02:01 +00001274 t->next = freeTokens;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001275 freeTokens = t;
1276 return next;
1277}
1278
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001279/*
1280 * Convert a line of tokens back into text.
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001281 * If expand_locals is not zero, identifiers of the form "%$*xxx"
1282 * will be transformed into ..@ctxnum.xxx
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001283 */
H. Peter Anvin9e200162008-06-04 17:23:14 -07001284static char *detoken(Token * tlist, bool expand_locals)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001285{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001286 Token *t;
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001287 char *line, *p;
H. Peter Anvinb4daadc2008-01-21 16:31:57 -08001288 const char *q;
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001289 int len = 0;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001290
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001291 list_for_each(t, tlist) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001292 if (t->type == TOK_PREPROC_ID && t->text[1] == '!') {
Cyrill Gorcunov84b4cba2010-09-12 21:39:40 +04001293 char *v;
1294 char *q = t->text;
H. Peter Anvin077fb932010-07-20 14:56:30 -07001295
Cyrill Gorcunov84b4cba2010-09-12 21:39:40 +04001296 v = t->text + 2;
1297 if (*v == '\'' || *v == '\"' || *v == '`') {
1298 size_t len = nasm_unquote(v, NULL);
1299 size_t clen = strlen(v);
H. Peter Anvin077fb932010-07-20 14:56:30 -07001300
Cyrill Gorcunov84b4cba2010-09-12 21:39:40 +04001301 if (len != clen) {
1302 error(ERR_NONFATAL | ERR_PASS1,
1303 "NUL character in %! string");
1304 v = NULL;
1305 }
1306 }
H. Peter Anvin077fb932010-07-20 14:56:30 -07001307
Cyrill Gorcunov84b4cba2010-09-12 21:39:40 +04001308 if (v) {
1309 char *p = getenv(v);
1310 if (!p) {
1311 error(ERR_NONFATAL | ERR_PASS1,
1312 "nonexistent environment variable `%s'", v);
1313 p = "";
1314 }
1315 t->text = nasm_strdup(p);
1316 }
1317 nasm_free(q);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001318 }
H. Peter Anvin077fb932010-07-20 14:56:30 -07001319
H. Peter Anvine2c80182005-01-15 22:15:51 +00001320 /* Expand local macros here and not during preprocessing */
1321 if (expand_locals &&
1322 t->type == TOK_PREPROC_ID && t->text &&
1323 t->text[0] == '%' && t->text[1] == '$') {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001324 const char *q;
1325 char *p;
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001326 Context *ctx = get_ctx(t->text, &q, false);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001327 if (ctx) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001328 char buffer[40];
Keith Kanios93f2e9a2007-04-14 00:10:59 +00001329 snprintf(buffer, sizeof(buffer), "..@%"PRIu32".", ctx->number);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001330 p = nasm_strcat(buffer, q);
1331 nasm_free(t->text);
1332 t->text = p;
1333 }
1334 }
Keith Kaniosb307a4f2010-11-06 17:41:51 -05001335
1336 /* Expand %? and %?? directives */
1337 if (expand_locals && (istk->expansion != NULL) &&
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03001338 ((t->type == TOK_PREPROC_Q) ||
1339 (t->type == TOK_PREPROC_QQ))) {
Keith Kaniosb307a4f2010-11-06 17:41:51 -05001340 ExpInv *ei;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03001341 for (ei = istk->expansion; ei != NULL; ei = ei->prev){
1342 if (ei->type == EXP_MMACRO) {
1343 nasm_free(t->text);
1344 if (t->type == TOK_PREPROC_Q) {
1345 t->text = nasm_strdup(ei->name);
1346 } else {
1347 t->text = nasm_strdup(ei->def->name);
1348 }
1349 break;
1350 }
1351 }
1352 }
Keith Kaniosb307a4f2010-11-06 17:41:51 -05001353
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001354 if (t->type == TOK_WHITESPACE)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001355 len++;
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001356 else if (t->text)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001357 len += strlen(t->text);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001358 }
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001359
H. Peter Anvin734b1882002-04-30 21:01:08 +00001360 p = line = nasm_malloc(len + 1);
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001361
1362 list_for_each(t, tlist) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001363 if (t->type == TOK_WHITESPACE) {
H. Peter Anvinb4daadc2008-01-21 16:31:57 -08001364 *p++ = ' ';
H. Peter Anvine2c80182005-01-15 22:15:51 +00001365 } else if (t->text) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001366 q = t->text;
1367 while (*q)
1368 *p++ = *q++;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001369 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001370 }
1371 *p = '\0';
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001372
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001373 return line;
1374}
1375
1376/*
Keith Kaniosb307a4f2010-11-06 17:41:51 -05001377 * Initialize a new Line
1378 */
Cyrill Gorcunov29cb0bb2010-11-11 11:19:43 +03001379static inline Line *new_Line(void)
Keith Kaniosb307a4f2010-11-06 17:41:51 -05001380{
Cyrill Gorcunov29cb0bb2010-11-11 11:19:43 +03001381 return (Line *)nasm_zalloc(sizeof(Line));
Keith Kaniosb307a4f2010-11-06 17:41:51 -05001382}
1383
1384
1385/*
1386 * Initialize a new Expansion Definition
1387 */
1388static ExpDef *new_ExpDef(int exp_type)
1389{
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03001390 ExpDef *ed = (ExpDef*)nasm_malloc(sizeof(ExpDef));
1391 ed->prev = NULL;
1392 ed->next = NULL;
1393 ed->type = exp_type;
1394 ed->name = NULL;
1395 ed->nparam_min = 0;
1396 ed->nparam_max = 0;
1397 ed->casesense = true;
1398 ed->plus = false;
1399 ed->prepend = 0;
1400 ed->label = NULL;
1401 ed->line = NULL;
1402 ed->last = NULL;
1403 ed->linecount = 0;
1404 ed->dlist = NULL;
1405 ed->defaults = NULL;
1406 ed->ndefs = 0;
1407 ed->state = COND_NEVER;
1408 ed->nolist = false;
1409 ed->def_depth = 0;
1410 ed->cur_depth = 0;
1411 ed->max_depth = 0;
1412 ed->ignoring = false;
1413 return ed;
Keith Kaniosb307a4f2010-11-06 17:41:51 -05001414}
1415
1416
1417/*
1418 * Initialize a new Expansion Instance
1419 */
1420static ExpInv *new_ExpInv(int exp_type, ExpDef *ed)
1421{
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03001422 ExpInv *ei = (ExpInv*)nasm_malloc(sizeof(ExpInv));
1423 ei->prev = NULL;
1424 ei->type = exp_type;
1425 ei->def = ed;
1426 ei->name = NULL;
1427 ei->label = NULL;
1428 ei->label_text = NULL;
1429 ei->current = NULL;
1430 ei->params = NULL;
1431 ei->iline = NULL;
1432 ei->nparam = 0;
1433 ei->rotate = 0;
1434 ei->paramlen = NULL;
1435 ei->unique = ++unique;
1436 ei->emitting = false;
1437 ei->lineno = 0;
1438 if ((istk->mmac_depth < 1) &&
1439 (istk->expansion == NULL) &&
1440 (ed != NULL) &&
1441 (ed->type != EXP_MMACRO) &&
1442 (ed->type != EXP_REP) &&
1443 (ed->type != EXP_WHILE)) {
1444 ei->linnum = src_get_linnum();
1445 src_set_linnum(ei->linnum - ed->linecount - 1);
1446 } else {
1447 ei->linnum = -1;
1448 }
1449 if ((istk->expansion == NULL) ||
1450 (ei->type == EXP_MMACRO)) {
1451 ei->relno = 0;
1452 } else {
1453 ei->relno = istk->expansion->lineno;
1454 if (ed != NULL) {
1455 ei->relno -= (ed->linecount + 1);
1456 }
1457 }
1458 return ei;
Keith Kaniosb307a4f2010-11-06 17:41:51 -05001459}
1460
1461/*
H. Peter Anvin76690a12002-04-30 20:52:49 +00001462 * A scanner, suitable for use by the expression evaluator, which
1463 * operates on a line of Tokens. Expects a pointer to a pointer to
1464 * the first token in the line to be passed in as its private_data
1465 * field.
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001466 *
1467 * FIX: This really needs to be unified with stdscan.
H. Peter Anvin76690a12002-04-30 20:52:49 +00001468 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001469static int ppscan(void *private_data, struct tokenval *tokval)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001470{
H. Peter Anvin76690a12002-04-30 20:52:49 +00001471 Token **tlineptr = private_data;
1472 Token *tline;
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001473 char ourcopy[MAX_KEYWORD+1], *p, *r, *s;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001474
H. Peter Anvine2c80182005-01-15 22:15:51 +00001475 do {
1476 tline = *tlineptr;
1477 *tlineptr = tline ? tline->next : NULL;
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04001478 } while (tline && (tline->type == TOK_WHITESPACE ||
1479 tline->type == TOK_COMMENT));
H. Peter Anvin76690a12002-04-30 20:52:49 +00001480
1481 if (!tline)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001482 return tokval->t_type = TOKEN_EOS;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001483
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001484 tokval->t_charptr = tline->text;
1485
H. Peter Anvin76690a12002-04-30 20:52:49 +00001486 if (tline->text[0] == '$' && !tline->text[1])
H. Peter Anvine2c80182005-01-15 22:15:51 +00001487 return tokval->t_type = TOKEN_HERE;
H. Peter Anvin7cf897e2002-05-30 21:30:33 +00001488 if (tline->text[0] == '$' && tline->text[1] == '$' && !tline->text[2])
H. Peter Anvine2c80182005-01-15 22:15:51 +00001489 return tokval->t_type = TOKEN_BASE;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001490
H. Peter Anvine2c80182005-01-15 22:15:51 +00001491 if (tline->type == TOK_ID) {
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001492 p = tokval->t_charptr = tline->text;
1493 if (p[0] == '$') {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001494 tokval->t_charptr++;
1495 return tokval->t_type = TOKEN_ID;
1496 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00001497
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001498 for (r = p, s = ourcopy; *r; r++) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001499 if (r >= p+MAX_KEYWORD)
1500 return tokval->t_type = TOKEN_ID; /* Not a keyword */
H. Peter Anvinac8f8fc2008-06-11 15:49:41 -07001501 *s++ = nasm_tolower(*r);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001502 }
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001503 *s = '\0';
1504 /* right, so we have an identifier sitting in temp storage. now,
1505 * is it actually a register or instruction name, or what? */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001506 return nasm_token_hash(ourcopy, tokval);
H. Peter Anvin76690a12002-04-30 20:52:49 +00001507 }
1508
H. Peter Anvine2c80182005-01-15 22:15:51 +00001509 if (tline->type == TOK_NUMBER) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001510 bool rn_error;
1511 tokval->t_integer = readnum(tline->text, &rn_error);
1512 tokval->t_charptr = tline->text;
1513 if (rn_error)
1514 return tokval->t_type = TOKEN_ERRNUM;
1515 else
1516 return tokval->t_type = TOKEN_NUM;
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001517 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00001518
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001519 if (tline->type == TOK_FLOAT) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001520 return tokval->t_type = TOKEN_FLOAT;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001521 }
1522
H. Peter Anvine2c80182005-01-15 22:15:51 +00001523 if (tline->type == TOK_STRING) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001524 char bq, *ep;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001525
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001526 bq = tline->text[0];
H. Peter Anvin11627042008-06-09 20:45:19 -07001527 tokval->t_charptr = tline->text;
1528 tokval->t_inttwo = nasm_unquote(tline->text, &ep);
H. Peter Anvind2456592008-06-19 15:04:18 -07001529
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001530 if (ep[0] != bq || ep[1] != '\0')
1531 return tokval->t_type = TOKEN_ERRSTR;
1532 else
1533 return tokval->t_type = TOKEN_STR;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001534 }
1535
H. Peter Anvine2c80182005-01-15 22:15:51 +00001536 if (tline->type == TOK_OTHER) {
1537 if (!strcmp(tline->text, "<<"))
1538 return tokval->t_type = TOKEN_SHL;
1539 if (!strcmp(tline->text, ">>"))
1540 return tokval->t_type = TOKEN_SHR;
1541 if (!strcmp(tline->text, "//"))
1542 return tokval->t_type = TOKEN_SDIV;
1543 if (!strcmp(tline->text, "%%"))
1544 return tokval->t_type = TOKEN_SMOD;
1545 if (!strcmp(tline->text, "=="))
1546 return tokval->t_type = TOKEN_EQ;
1547 if (!strcmp(tline->text, "<>"))
1548 return tokval->t_type = TOKEN_NE;
1549 if (!strcmp(tline->text, "!="))
1550 return tokval->t_type = TOKEN_NE;
1551 if (!strcmp(tline->text, "<="))
1552 return tokval->t_type = TOKEN_LE;
1553 if (!strcmp(tline->text, ">="))
1554 return tokval->t_type = TOKEN_GE;
1555 if (!strcmp(tline->text, "&&"))
1556 return tokval->t_type = TOKEN_DBL_AND;
1557 if (!strcmp(tline->text, "^^"))
1558 return tokval->t_type = TOKEN_DBL_XOR;
1559 if (!strcmp(tline->text, "||"))
1560 return tokval->t_type = TOKEN_DBL_OR;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001561 }
1562
1563 /*
1564 * We have no other options: just return the first character of
1565 * the token text.
1566 */
1567 return tokval->t_type = tline->text[0];
1568}
1569
1570/*
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001571 * Compare a string to the name of an existing macro; this is a
1572 * simple wrapper which calls either strcmp or nasm_stricmp
1573 * depending on the value of the `casesense' parameter.
1574 */
H. Peter Anvin4db5a162007-10-11 13:42:09 -07001575static int mstrcmp(const char *p, const char *q, bool casesense)
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001576{
H. Peter Anvin734b1882002-04-30 21:01:08 +00001577 return casesense ? strcmp(p, q) : nasm_stricmp(p, q);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001578}
1579
1580/*
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07001581 * Compare a string to the name of an existing macro; this is a
1582 * simple wrapper which calls either strcmp or nasm_stricmp
1583 * depending on the value of the `casesense' parameter.
1584 */
1585static int mmemcmp(const char *p, const char *q, size_t l, bool casesense)
1586{
1587 return casesense ? memcmp(p, q, l) : nasm_memicmp(p, q, l);
1588}
1589
1590/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001591 * Return the Context structure associated with a %$ token. Return
1592 * NULL, having _already_ reported an error condition, if the
1593 * context stack isn't deep enough for the supplied number of $
1594 * signs.
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001595 * If all_contexts == true, contexts that enclose current are
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001596 * also scanned for such smacro, until it is found; if not -
1597 * only the context that directly results from the number of $'s
1598 * in variable's name.
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001599 *
1600 * If "namep" is non-NULL, set it to the pointer to the macro name
1601 * tail, i.e. the part beyond %$...
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001602 */
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001603static Context *get_ctx(const char *name, const char **namep,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001604 bool all_contexts)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001605{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001606 Context *ctx;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001607 SMacro *m;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001608 int i;
1609
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001610 if (namep)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001611 *namep = name;
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001612
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001613 if (!name || name[0] != '%' || name[1] != '$')
H. Peter Anvine2c80182005-01-15 22:15:51 +00001614 return NULL;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001615
H. Peter Anvine2c80182005-01-15 22:15:51 +00001616 if (!cstk) {
1617 error(ERR_NONFATAL, "`%s': context stack is empty", name);
1618 return NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001619 }
1620
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001621 name += 2;
1622 ctx = cstk;
1623 i = 0;
1624 while (ctx && *name == '$') {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001625 name++;
1626 i++;
1627 ctx = ctx->next;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001628 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001629 if (!ctx) {
1630 error(ERR_NONFATAL, "`%s': context stack is only"
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001631 " %d level%s deep", name, i, (i == 1 ? "" : "s"));
H. Peter Anvine2c80182005-01-15 22:15:51 +00001632 return NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001633 }
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001634
1635 if (namep)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001636 *namep = name;
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001637
Keith Kanios404589e2010-08-10 20:12:57 -05001638 if (!all_contexts)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001639 return ctx;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001640
Keith Kaniosb307a4f2010-11-06 17:41:51 -05001641 do {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001642 /* Search for this smacro in found context */
H. Peter Anvin166c2472008-05-28 12:28:58 -07001643 m = hash_findix(&ctx->localmac, name);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001644 while (m) {
Keith Kaniosb307a4f2010-11-06 17:41:51 -05001645 if (!mstrcmp(m->name, name, m->casesense))
H. Peter Anvine2c80182005-01-15 22:15:51 +00001646 return ctx;
1647 m = m->next;
1648 }
Keith Kaniosb307a4f2010-11-06 17:41:51 -05001649 ctx = ctx->next;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001650 }
Keith Kaniosb307a4f2010-11-06 17:41:51 -05001651 while (ctx);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001652 return NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001653}
1654
1655/*
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001656 * Check to see if a file is already in a string list
1657 */
1658static bool in_list(const StrList *list, const char *str)
1659{
1660 while (list) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001661 if (!strcmp(list->str, str))
1662 return true;
1663 list = list->next;
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001664 }
1665 return false;
1666}
1667
1668/*
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001669 * Open an include file. This routine must always return a valid
1670 * file pointer if it returns - it's responsible for throwing an
1671 * ERR_FATAL and bombing out completely if not. It should also try
1672 * the include path one by one until it finds the file or reaches
1673 * the end of the path.
1674 */
H. Peter Anvin2b1c3b92008-06-06 10:38:46 -07001675static FILE *inc_fopen(const char *file, StrList **dhead, StrList ***dtail,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001676 bool missing_ok)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001677{
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001678 FILE *fp;
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001679 char *prefix = "";
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001680 IncPath *ip = ipath;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001681 int len = strlen(file);
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001682 size_t prefix_len = 0;
1683 StrList *sl;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001684
H. Peter Anvine2c80182005-01-15 22:15:51 +00001685 while (1) {
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001686 sl = nasm_malloc(prefix_len+len+1+sizeof sl->next);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001687 memcpy(sl->str, prefix, prefix_len);
1688 memcpy(sl->str+prefix_len, file, len+1);
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001689 fp = fopen(sl->str, "r");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001690 if (fp && dhead && !in_list(*dhead, sl->str)) {
1691 sl->next = NULL;
1692 **dtail = sl;
1693 *dtail = &sl->next;
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001694 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001695 nasm_free(sl);
1696 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001697 if (fp)
1698 return fp;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001699 if (!ip) {
1700 if (!missing_ok)
1701 break;
1702 prefix = NULL;
1703 } else {
1704 prefix = ip->path;
1705 ip = ip->next;
1706 }
1707 if (prefix) {
1708 prefix_len = strlen(prefix);
1709 } else {
1710 /* -MG given and file not found */
1711 if (dhead && !in_list(*dhead, file)) {
1712 sl = nasm_malloc(len+1+sizeof sl->next);
1713 sl->next = NULL;
1714 strcpy(sl->str, file);
1715 **dtail = sl;
1716 *dtail = &sl->next;
1717 }
1718 return NULL;
1719 }
H. Peter Anvineba20a72002-04-30 20:53:55 +00001720 }
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001721
H. Peter Anvin734b1882002-04-30 21:01:08 +00001722 error(ERR_FATAL, "unable to open include file `%s'", file);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001723 return NULL;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001724}
1725
1726/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001727 * Determine if we should warn on defining a single-line macro of
H. Peter Anvinef7468f2002-04-30 20:57:59 +00001728 * name `name', with `nparam' parameters. If nparam is 0 or -1, will
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001729 * return true if _any_ single-line macro of that name is defined.
1730 * Otherwise, will return true if a single-line macro with either
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001731 * `nparam' or no parameters is defined.
1732 *
1733 * If a macro with precisely the right number of parameters is
H. Peter Anvinef7468f2002-04-30 20:57:59 +00001734 * defined, or nparam is -1, the address of the definition structure
1735 * will be returned in `defn'; otherwise NULL will be returned. If `defn'
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001736 * is NULL, no action will be taken regarding its contents, and no
1737 * error will occur.
1738 *
1739 * Note that this is also called with nparam zero to resolve
1740 * `ifdef'.
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001741 *
1742 * If you already know which context macro belongs to, you can pass
1743 * the context pointer as first parameter; if you won't but name begins
1744 * with %$ the context will be automatically computed. If all_contexts
1745 * is true, macro will be searched in outer contexts as well.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001746 */
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07001747static bool
H. Peter Anvinb2a5fda2008-06-19 21:42:42 -07001748smacro_defined(Context * ctx, const char *name, int nparam, SMacro ** defn,
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07001749 bool nocase)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001750{
H. Peter Anvin166c2472008-05-28 12:28:58 -07001751 struct hash_table *smtbl;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001752 SMacro *m;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001753
H. Peter Anvin97a23472007-09-16 17:57:25 -07001754 if (ctx) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001755 smtbl = &ctx->localmac;
H. Peter Anvin97a23472007-09-16 17:57:25 -07001756 } else if (name[0] == '%' && name[1] == '$') {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001757 if (cstk)
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001758 ctx = get_ctx(name, &name, false);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001759 if (!ctx)
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001760 return false; /* got to return _something_ */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001761 smtbl = &ctx->localmac;
H. Peter Anvin97a23472007-09-16 17:57:25 -07001762 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001763 smtbl = &smacros;
H. Peter Anvin97a23472007-09-16 17:57:25 -07001764 }
H. Peter Anvin166c2472008-05-28 12:28:58 -07001765 m = (SMacro *) hash_findix(smtbl, name);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001766
H. Peter Anvine2c80182005-01-15 22:15:51 +00001767 while (m) {
1768 if (!mstrcmp(m->name, name, m->casesense && nocase) &&
Charles Crayne192d5b52007-10-18 19:02:42 -07001769 (nparam <= 0 || m->nparam == 0 || nparam == (int) m->nparam)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001770 if (defn) {
Charles Crayne192d5b52007-10-18 19:02:42 -07001771 if (nparam == (int) m->nparam || nparam == -1)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001772 *defn = m;
1773 else
1774 *defn = NULL;
1775 }
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001776 return true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001777 }
1778 m = m->next;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001779 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001780
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001781 return false;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001782}
1783
1784/*
1785 * Count and mark off the parameters in a multi-line macro call.
1786 * This is called both from within the multi-line macro expansion
1787 * code, and also to mark off the default parameters when provided
1788 * in a %macro definition line.
1789 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001790static void count_mmac_params(Token * t, int *nparam, Token *** params)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001791{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001792 int paramsize, brace;
1793
1794 *nparam = paramsize = 0;
1795 *params = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001796 while (t) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001797 /* +1: we need space for the final NULL */
H. Peter Anvin91fb6f12008-09-01 10:56:33 -07001798 if (*nparam+1 >= paramsize) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001799 paramsize += PARAM_DELTA;
1800 *params = nasm_realloc(*params, sizeof(**params) * paramsize);
1801 }
1802 skip_white_(t);
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001803 brace = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001804 if (tok_is_(t, "{"))
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001805 brace = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001806 (*params)[(*nparam)++] = t;
1807 while (tok_isnt_(t, brace ? "}" : ","))
1808 t = t->next;
1809 if (t) { /* got a comma/brace */
1810 t = t->next;
1811 if (brace) {
1812 /*
1813 * Now we've found the closing brace, look further
1814 * for the comma.
1815 */
1816 skip_white_(t);
1817 if (tok_isnt_(t, ",")) {
1818 error(ERR_NONFATAL,
1819 "braces do not enclose all of macro parameter");
1820 while (tok_isnt_(t, ","))
1821 t = t->next;
1822 }
1823 if (t)
1824 t = t->next; /* eat the comma */
1825 }
1826 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001827 }
1828}
1829
1830/*
H. Peter Anvin76690a12002-04-30 20:52:49 +00001831 * Determine whether one of the various `if' conditions is true or
1832 * not.
1833 *
1834 * We must free the tline we get passed.
1835 */
H. Peter Anvin70055962007-10-11 00:05:31 -07001836static bool if_condition(Token * tline, enum preproc_token ct)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001837{
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001838 enum pp_conditional i = PP_COND(ct);
H. Peter Anvin70055962007-10-11 00:05:31 -07001839 bool j;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001840 Token *t, *tt, **tptr, *origline;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001841 struct tokenval tokval;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001842 expr *evalresult;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001843 enum pp_token_type needtype;
H. Peter Anvin077fb932010-07-20 14:56:30 -07001844 char *p;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001845
1846 origline = tline;
1847
H. Peter Anvine2c80182005-01-15 22:15:51 +00001848 switch (i) {
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001849 case PPC_IFCTX:
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001850 j = false; /* have we matched yet? */
Victor van den Elzen0e857f12008-07-23 13:21:29 +02001851 while (true) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001852 skip_white_(tline);
Victor van den Elzen0e857f12008-07-23 13:21:29 +02001853 if (!tline)
1854 break;
1855 if (tline->type != TOK_ID) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001856 error(ERR_NONFATAL,
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001857 "`%s' expects context identifiers", pp_directives[ct]);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001858 free_tlist(origline);
1859 return -1;
1860 }
Victor van den Elzen0e857f12008-07-23 13:21:29 +02001861 if (cstk && cstk->name && !nasm_stricmp(tline->text, cstk->name))
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001862 j = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001863 tline = tline->next;
1864 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001865 break;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001866
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001867 case PPC_IFDEF:
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001868 j = false; /* have we matched yet? */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001869 while (tline) {
1870 skip_white_(tline);
1871 if (!tline || (tline->type != TOK_ID &&
1872 (tline->type != TOK_PREPROC_ID ||
1873 tline->text[1] != '$'))) {
1874 error(ERR_NONFATAL,
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001875 "`%s' expects macro identifiers", pp_directives[ct]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001876 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001877 }
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07001878 if (smacro_defined(NULL, tline->text, 0, NULL, true))
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001879 j = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001880 tline = tline->next;
1881 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001882 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001883
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001884 case PPC_IFENV:
Cyrill Gorcunov84b4cba2010-09-12 21:39:40 +04001885 tline = expand_smacro(tline);
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001886 j = false; /* have we matched yet? */
1887 while (tline) {
1888 skip_white_(tline);
1889 if (!tline || (tline->type != TOK_ID &&
Cyrill Gorcunov84b4cba2010-09-12 21:39:40 +04001890 tline->type != TOK_STRING &&
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001891 (tline->type != TOK_PREPROC_ID ||
Cyrill Gorcunov84b4cba2010-09-12 21:39:40 +04001892 tline->text[1] != '!'))) {
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001893 error(ERR_NONFATAL,
1894 "`%s' expects environment variable names",
Cyrill Gorcunov84b4cba2010-09-12 21:39:40 +04001895 pp_directives[ct]);
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001896 goto fail;
1897 }
Cyrill Gorcunov84b4cba2010-09-12 21:39:40 +04001898 p = tline->text;
1899 if (tline->type == TOK_PREPROC_ID)
1900 p += 2; /* Skip leading %! */
1901 if (*p == '\'' || *p == '\"' || *p == '`')
1902 nasm_unquote_cstr(p, ct);
1903 if (getenv(p))
1904 j = true;
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001905 tline = tline->next;
1906 }
1907 break;
1908
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001909 case PPC_IFIDN:
1910 case PPC_IFIDNI:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001911 tline = expand_smacro(tline);
1912 t = tt = tline;
1913 while (tok_isnt_(tt, ","))
1914 tt = tt->next;
1915 if (!tt) {
1916 error(ERR_NONFATAL,
1917 "`%s' expects two comma-separated arguments",
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001918 pp_directives[ct]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001919 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001920 }
1921 tt = tt->next;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001922 j = true; /* assume equality unless proved not */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001923 while ((t->type != TOK_OTHER || strcmp(t->text, ",")) && tt) {
1924 if (tt->type == TOK_OTHER && !strcmp(tt->text, ",")) {
1925 error(ERR_NONFATAL, "`%s': more than one comma on line",
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001926 pp_directives[ct]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001927 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001928 }
1929 if (t->type == TOK_WHITESPACE) {
1930 t = t->next;
1931 continue;
1932 }
1933 if (tt->type == TOK_WHITESPACE) {
1934 tt = tt->next;
1935 continue;
1936 }
1937 if (tt->type != t->type) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001938 j = false; /* found mismatching tokens */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001939 break;
1940 }
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07001941 /* When comparing strings, need to unquote them first */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001942 if (t->type == TOK_STRING) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001943 size_t l1 = nasm_unquote(t->text, NULL);
1944 size_t l2 = nasm_unquote(tt->text, NULL);
H. Peter Anvind2456592008-06-19 15:04:18 -07001945
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001946 if (l1 != l2) {
1947 j = false;
1948 break;
1949 }
1950 if (mmemcmp(t->text, tt->text, l1, i == PPC_IFIDN)) {
1951 j = false;
1952 break;
1953 }
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07001954 } else if (mstrcmp(tt->text, t->text, i == PPC_IFIDN) != 0) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001955 j = false; /* found mismatching tokens */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001956 break;
1957 }
Nickolay Yurchenkof3b3ce22003-09-21 20:38:43 +00001958
H. Peter Anvine2c80182005-01-15 22:15:51 +00001959 t = t->next;
1960 tt = tt->next;
1961 }
1962 if ((t->type != TOK_OTHER || strcmp(t->text, ",")) || tt)
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001963 j = false; /* trailing gunk on one end or other */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001964 break;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001965
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001966 case PPC_IFMACRO:
H. Peter Anvin89cee572009-07-15 09:16:54 -04001967 {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001968 bool found = false;
Keith Kaniosb307a4f2010-11-06 17:41:51 -05001969 ExpDef searching, *ed;
H. Peter Anvin65747262002-05-07 00:10:05 +00001970
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001971 skip_white_(tline);
1972 tline = expand_id(tline);
1973 if (!tok_type_(tline, TOK_ID)) {
1974 error(ERR_NONFATAL,
1975 "`%s' expects a macro name", pp_directives[ct]);
1976 goto fail;
1977 }
1978 searching.name = nasm_strdup(tline->text);
1979 searching.casesense = true;
1980 searching.plus = false;
1981 searching.nolist = false;
Keith Kaniosb307a4f2010-11-06 17:41:51 -05001982 //searching.in_progress = 0;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001983 searching.max_depth = 0;
Keith Kaniosb307a4f2010-11-06 17:41:51 -05001984 //searching.rep_nest = NULL;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001985 searching.nparam_min = 0;
1986 searching.nparam_max = INT_MAX;
1987 tline = expand_smacro(tline->next);
1988 skip_white_(tline);
1989 if (!tline) {
1990 } else if (!tok_type_(tline, TOK_NUMBER)) {
1991 error(ERR_NONFATAL,
1992 "`%s' expects a parameter count or nothing",
1993 pp_directives[ct]);
1994 } else {
1995 searching.nparam_min = searching.nparam_max =
1996 readnum(tline->text, &j);
1997 if (j)
1998 error(ERR_NONFATAL,
1999 "unable to parse parameter count `%s'",
2000 tline->text);
2001 }
2002 if (tline && tok_is_(tline->next, "-")) {
2003 tline = tline->next->next;
2004 if (tok_is_(tline, "*"))
2005 searching.nparam_max = INT_MAX;
2006 else if (!tok_type_(tline, TOK_NUMBER))
2007 error(ERR_NONFATAL,
2008 "`%s' expects a parameter count after `-'",
2009 pp_directives[ct]);
2010 else {
2011 searching.nparam_max = readnum(tline->text, &j);
2012 if (j)
2013 error(ERR_NONFATAL,
2014 "unable to parse parameter count `%s'",
2015 tline->text);
2016 if (searching.nparam_min > searching.nparam_max)
2017 error(ERR_NONFATAL,
2018 "minimum parameter count exceeds maximum");
2019 }
2020 }
2021 if (tline && tok_is_(tline->next, "+")) {
2022 tline = tline->next;
2023 searching.plus = true;
2024 }
Keith Kaniosb307a4f2010-11-06 17:41:51 -05002025 ed = (ExpDef *) hash_findix(&expdefs, searching.name);
2026 while (ed != NULL) {
2027 if (!strcmp(ed->name, searching.name) &&
2028 (ed->nparam_min <= searching.nparam_max
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002029 || searching.plus)
Keith Kaniosb307a4f2010-11-06 17:41:51 -05002030 && (searching.nparam_min <= ed->nparam_max
2031 || ed->plus)) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002032 found = true;
2033 break;
2034 }
Keith Kaniosb307a4f2010-11-06 17:41:51 -05002035 ed = ed->next;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002036 }
2037 if (tline && tline->next)
2038 error(ERR_WARNING|ERR_PASS1,
2039 "trailing garbage after %%ifmacro ignored");
2040 nasm_free(searching.name);
2041 j = found;
2042 break;
H. Peter Anvin89cee572009-07-15 09:16:54 -04002043 }
H. Peter Anvin65747262002-05-07 00:10:05 +00002044
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002045 case PPC_IFID:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002046 needtype = TOK_ID;
2047 goto iftype;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002048 case PPC_IFNUM:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002049 needtype = TOK_NUMBER;
2050 goto iftype;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002051 case PPC_IFSTR:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002052 needtype = TOK_STRING;
2053 goto iftype;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002054
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002055iftype:
2056 t = tline = expand_smacro(tline);
H. Peter Anvind85d2502008-05-04 17:53:31 -07002057
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002058 while (tok_type_(t, TOK_WHITESPACE) ||
2059 (needtype == TOK_NUMBER &&
2060 tok_type_(t, TOK_OTHER) &&
2061 (t->text[0] == '-' || t->text[0] == '+') &&
2062 !t->text[1]))
2063 t = t->next;
H. Peter Anvind85d2502008-05-04 17:53:31 -07002064
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002065 j = tok_type_(t, needtype);
2066 break;
H. Peter Anvincbf768d2008-02-16 16:41:25 -08002067
2068 case PPC_IFTOKEN:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002069 t = tline = expand_smacro(tline);
2070 while (tok_type_(t, TOK_WHITESPACE))
2071 t = t->next;
H. Peter Anvincbf768d2008-02-16 16:41:25 -08002072
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002073 j = false;
2074 if (t) {
2075 t = t->next; /* Skip the actual token */
2076 while (tok_type_(t, TOK_WHITESPACE))
2077 t = t->next;
2078 j = !t; /* Should be nothing left */
2079 }
2080 break;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002081
H. Peter Anvin134b9462008-02-16 17:01:40 -08002082 case PPC_IFEMPTY:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002083 t = tline = expand_smacro(tline);
2084 while (tok_type_(t, TOK_WHITESPACE))
2085 t = t->next;
H. Peter Anvin134b9462008-02-16 17:01:40 -08002086
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002087 j = !t; /* Should be empty */
2088 break;
H. Peter Anvin134b9462008-02-16 17:01:40 -08002089
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002090 case PPC_IF:
H. Peter Anvine2c80182005-01-15 22:15:51 +00002091 t = tline = expand_smacro(tline);
2092 tptr = &t;
2093 tokval.t_type = TOKEN_INVALID;
2094 evalresult = evaluate(ppscan, tptr, &tokval,
2095 NULL, pass | CRITICAL, error, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002096 if (!evalresult)
2097 return -1;
2098 if (tokval.t_type)
H. Peter Anvin917a3492008-09-24 09:14:49 -07002099 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +00002100 "trailing garbage after expression ignored");
2101 if (!is_simple(evalresult)) {
2102 error(ERR_NONFATAL,
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002103 "non-constant value given to `%s'", pp_directives[ct]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002104 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002105 }
Chuck Crayne60ae75d2007-05-02 01:59:16 +00002106 j = reloc_value(evalresult) != 0;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002107 break;
H. Peter Anvin95e28822007-09-12 04:20:08 +00002108
H. Peter Anvine2c80182005-01-15 22:15:51 +00002109 default:
2110 error(ERR_FATAL,
2111 "preprocessor directive `%s' not yet implemented",
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002112 pp_directives[ct]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002113 goto fail;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002114 }
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002115
2116 free_tlist(origline);
2117 return j ^ PP_NEGATIVE(ct);
H. Peter Anvin70653092007-10-19 14:42:29 -07002118
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002119fail:
2120 free_tlist(origline);
2121 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002122}
2123
2124/*
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002125 * Common code for defining an smacro
2126 */
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08002127static bool define_smacro(Context *ctx, const char *mname, bool casesense,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002128 int nparam, Token *expansion)
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002129{
2130 SMacro *smac, **smhead;
H. Peter Anvin166c2472008-05-28 12:28:58 -07002131 struct hash_table *smtbl;
H. Peter Anvin70653092007-10-19 14:42:29 -07002132
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002133 if (smacro_defined(ctx, mname, nparam, &smac, casesense)) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002134 if (!smac) {
2135 error(ERR_WARNING|ERR_PASS1,
2136 "single-line macro `%s' defined both with and"
2137 " without parameters", mname);
2138 /*
2139 * Some instances of the old code considered this a failure,
2140 * some others didn't. What is the right thing to do here?
2141 */
2142 free_tlist(expansion);
2143 return false; /* Failure */
2144 } else {
2145 /*
2146 * We're redefining, so we have to take over an
2147 * existing SMacro structure. This means freeing
2148 * what was already in it.
2149 */
2150 nasm_free(smac->name);
2151 free_tlist(smac->expansion);
2152 }
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002153 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002154 smtbl = ctx ? &ctx->localmac : &smacros;
2155 smhead = (SMacro **) hash_findi_add(smtbl, mname);
2156 smac = nasm_malloc(sizeof(SMacro));
2157 smac->next = *smhead;
2158 *smhead = smac;
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002159 }
2160 smac->name = nasm_strdup(mname);
2161 smac->casesense = casesense;
2162 smac->nparam = nparam;
2163 smac->expansion = expansion;
2164 smac->in_progress = false;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002165 return true; /* Success */
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002166}
2167
2168/*
2169 * Undefine an smacro
2170 */
2171static void undef_smacro(Context *ctx, const char *mname)
2172{
2173 SMacro **smhead, *s, **sp;
H. Peter Anvin166c2472008-05-28 12:28:58 -07002174 struct hash_table *smtbl;
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002175
H. Peter Anvin166c2472008-05-28 12:28:58 -07002176 smtbl = ctx ? &ctx->localmac : &smacros;
2177 smhead = (SMacro **)hash_findi(smtbl, mname, NULL);
H. Peter Anvin70653092007-10-19 14:42:29 -07002178
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002179 if (smhead) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002180 /*
2181 * We now have a macro name... go hunt for it.
2182 */
2183 sp = smhead;
2184 while ((s = *sp) != NULL) {
2185 if (!mstrcmp(s->name, mname, s->casesense)) {
2186 *sp = s->next;
2187 nasm_free(s->name);
2188 free_tlist(s->expansion);
2189 nasm_free(s);
2190 } else {
2191 sp = &s->next;
2192 }
2193 }
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002194 }
2195}
2196
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002197/*
H. Peter Anvina26433d2008-07-16 14:40:01 -07002198 * Parse a mmacro specification.
2199 */
Keith Kaniosb307a4f2010-11-06 17:41:51 -05002200static bool parse_mmacro_spec(Token *tline, ExpDef *def, const char *directive)
H. Peter Anvina26433d2008-07-16 14:40:01 -07002201{
2202 bool err;
2203
2204 tline = tline->next;
2205 skip_white_(tline);
2206 tline = expand_id(tline);
2207 if (!tok_type_(tline, TOK_ID)) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002208 error(ERR_NONFATAL, "`%s' expects a macro name", directive);
2209 return false;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002210 }
Victor van den Elzenb916ede2008-07-23 15:14:22 +02002211
H. Peter Anvina26433d2008-07-16 14:40:01 -07002212 def->name = nasm_strdup(tline->text);
2213 def->plus = false;
2214 def->nolist = false;
Keith Kaniosb307a4f2010-11-06 17:41:51 -05002215// def->in_progress = 0;
2216// def->rep_nest = NULL;
Victor van den Elzenb916ede2008-07-23 15:14:22 +02002217 def->nparam_min = 0;
2218 def->nparam_max = 0;
2219
H. Peter Anvina26433d2008-07-16 14:40:01 -07002220 tline = expand_smacro(tline->next);
2221 skip_white_(tline);
2222 if (!tok_type_(tline, TOK_NUMBER)) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002223 error(ERR_NONFATAL, "`%s' expects a parameter count", directive);
H. Peter Anvina26433d2008-07-16 14:40:01 -07002224 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002225 def->nparam_min = def->nparam_max =
2226 readnum(tline->text, &err);
2227 if (err)
2228 error(ERR_NONFATAL,
2229 "unable to parse parameter count `%s'", tline->text);
H. Peter Anvina26433d2008-07-16 14:40:01 -07002230 }
2231 if (tline && tok_is_(tline->next, "-")) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002232 tline = tline->next->next;
2233 if (tok_is_(tline, "*")) {
2234 def->nparam_max = INT_MAX;
2235 } else if (!tok_type_(tline, TOK_NUMBER)) {
2236 error(ERR_NONFATAL,
2237 "`%s' expects a parameter count after `-'", directive);
2238 } else {
2239 def->nparam_max = readnum(tline->text, &err);
2240 if (err) {
2241 error(ERR_NONFATAL, "unable to parse parameter count `%s'",
2242 tline->text);
2243 }
2244 if (def->nparam_min > def->nparam_max) {
2245 error(ERR_NONFATAL, "minimum parameter count exceeds maximum");
2246 }
2247 }
H. Peter Anvina26433d2008-07-16 14:40:01 -07002248 }
2249 if (tline && tok_is_(tline->next, "+")) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002250 tline = tline->next;
2251 def->plus = true;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002252 }
2253 if (tline && tok_type_(tline->next, TOK_ID) &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002254 !nasm_stricmp(tline->next->text, ".nolist")) {
2255 tline = tline->next;
2256 def->nolist = true;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002257 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002258
H. Peter Anvina26433d2008-07-16 14:40:01 -07002259 /*
2260 * Handle default parameters.
2261 */
2262 if (tline && tline->next) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002263 def->dlist = tline->next;
2264 tline->next = NULL;
2265 count_mmac_params(def->dlist, &def->ndefs, &def->defaults);
H. Peter Anvina26433d2008-07-16 14:40:01 -07002266 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002267 def->dlist = NULL;
2268 def->defaults = NULL;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002269 }
Keith Kaniosb307a4f2010-11-06 17:41:51 -05002270 def->line = NULL;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002271
H. Peter Anvin89cee572009-07-15 09:16:54 -04002272 if (def->defaults && def->ndefs > def->nparam_max - def->nparam_min &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002273 !def->plus)
2274 error(ERR_WARNING|ERR_PASS1|ERR_WARN_MDP,
2275 "too many default macro parameters");
Victor van den Elzenb916ede2008-07-23 15:14:22 +02002276
H. Peter Anvina26433d2008-07-16 14:40:01 -07002277 return true;
2278}
2279
2280
2281/*
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002282 * Decode a size directive
2283 */
2284static int parse_size(const char *str) {
2285 static const char *size_names[] =
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002286 { "byte", "dword", "oword", "qword", "tword", "word", "yword" };
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002287 static const int sizes[] =
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002288 { 0, 1, 4, 16, 8, 10, 2, 32 };
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002289
Cyrill Gorcunova7319242010-06-03 22:04:36 +04002290 return sizes[bsii(str, size_names, ARRAY_SIZE(size_names))+1];
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002291}
2292
Ed Beroset3ab3f412002-06-11 03:31:49 +00002293/**
2294 * find and process preprocessor directive in passed line
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002295 * Find out if a line contains a preprocessor directive, and deal
2296 * with it if so.
H. Peter Anvin70653092007-10-19 14:42:29 -07002297 *
Ed Beroset3ab3f412002-06-11 03:31:49 +00002298 * If a directive _is_ found, it is the responsibility of this routine
2299 * (and not the caller) to free_tlist() the line.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002300 *
Ed Beroset3ab3f412002-06-11 03:31:49 +00002301 * @param tline a pointer to the current tokeninzed line linked list
2302 * @return DIRECTIVE_FOUND or NO_DIRECTIVE_FOUND
H. Peter Anvin70653092007-10-19 14:42:29 -07002303 *
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002304 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002305static int do_directive(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00002306{
H. Peter Anvin4169a472007-09-12 01:29:43 +00002307 enum preproc_token i;
2308 int j;
H. Peter Anvin70055962007-10-11 00:05:31 -07002309 bool err;
2310 int nparam;
2311 bool nolist;
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07002312 bool casesense;
H. Peter Anvin8cfdb9d2007-09-14 18:36:01 -07002313 int k, m;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002314 int offset;
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08002315 char *p, *pp;
2316 const char *mname;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002317 Include *inc;
2318 Context *ctx;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002319 Line *l;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002320 Token *t, *tt, *param_start, *macro_start, *last, **tptr, *origline;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002321 struct tokenval tokval;
2322 expr *evalresult;
Keith Kaniosb307a4f2010-11-06 17:41:51 -05002323 ExpDef *ed, *eed, **edhead;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002324 ExpInv *ei, *eei;
H. Peter Anvinf8ba53e2007-10-11 10:11:57 -07002325 int64_t count;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07002326 size_t len;
H. Peter Anvin8e3f75e2008-09-24 00:21:58 -07002327 int severity;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002328
2329 origline = tline;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002330
H. Peter Anvineba20a72002-04-30 20:53:55 +00002331 skip_white_(tline);
H. Peter Anvinf2936d72008-06-04 15:11:23 -07002332 if (!tline || !tok_type_(tline, TOK_PREPROC_ID) ||
H. Peter Anvine2c80182005-01-15 22:15:51 +00002333 (tline->text[1] == '%' || tline->text[1] == '$'
2334 || tline->text[1] == '!'))
2335 return NO_DIRECTIVE_FOUND;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002336
H. Peter Anvin4169a472007-09-12 01:29:43 +00002337 i = pp_token_hash(tline->text);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002338
H. Peter Anvin4169a472007-09-12 01:29:43 +00002339 switch (i) {
2340 case PP_INVALID:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002341 if (defining != NULL) return NO_DIRECTIVE_FOUND;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002342 error(ERR_NONFATAL, "unknown preprocessor directive `%s'",
2343 tline->text);
2344 return NO_DIRECTIVE_FOUND; /* didn't get it */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002345
H. Peter Anvine2c80182005-01-15 22:15:51 +00002346 case PP_STACKSIZE:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002347 if (defining != NULL) return NO_DIRECTIVE_FOUND;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002348 /* Directive to tell NASM what the default stack size is. The
2349 * default is for a 16-bit stack, and this can be overriden with
2350 * %stacksize large.
H. Peter Anvine2c80182005-01-15 22:15:51 +00002351 */
2352 tline = tline->next;
2353 if (tline && tline->type == TOK_WHITESPACE)
2354 tline = tline->next;
2355 if (!tline || tline->type != TOK_ID) {
2356 error(ERR_NONFATAL, "`%%stacksize' missing size parameter");
2357 free_tlist(origline);
2358 return DIRECTIVE_FOUND;
2359 }
2360 if (nasm_stricmp(tline->text, "flat") == 0) {
2361 /* All subsequent ARG directives are for a 32-bit stack */
2362 StackSize = 4;
2363 StackPointer = "ebp";
2364 ArgOffset = 8;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002365 LocalOffset = 0;
Charles Crayne7eaf9192007-11-08 22:11:14 -08002366 } else if (nasm_stricmp(tline->text, "flat64") == 0) {
2367 /* All subsequent ARG directives are for a 64-bit stack */
2368 StackSize = 8;
2369 StackPointer = "rbp";
Per Jessen53252e02010-02-11 00:16:59 +03002370 ArgOffset = 16;
Charles Crayne7eaf9192007-11-08 22:11:14 -08002371 LocalOffset = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002372 } else if (nasm_stricmp(tline->text, "large") == 0) {
2373 /* All subsequent ARG directives are for a 16-bit stack,
2374 * far function call.
2375 */
2376 StackSize = 2;
2377 StackPointer = "bp";
2378 ArgOffset = 4;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002379 LocalOffset = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002380 } else if (nasm_stricmp(tline->text, "small") == 0) {
2381 /* All subsequent ARG directives are for a 16-bit stack,
2382 * far function call. We don't support near functions.
2383 */
2384 StackSize = 2;
2385 StackPointer = "bp";
2386 ArgOffset = 6;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002387 LocalOffset = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002388 } else {
2389 error(ERR_NONFATAL, "`%%stacksize' invalid size type");
2390 free_tlist(origline);
2391 return DIRECTIVE_FOUND;
2392 }
2393 free_tlist(origline);
2394 return DIRECTIVE_FOUND;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002395
H. Peter Anvine2c80182005-01-15 22:15:51 +00002396 case PP_ARG:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002397 if (defining != NULL) return NO_DIRECTIVE_FOUND;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002398 /* TASM like ARG directive to define arguments to functions, in
2399 * the following form:
2400 *
2401 * ARG arg1:WORD, arg2:DWORD, arg4:QWORD
2402 */
2403 offset = ArgOffset;
2404 do {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00002405 char *arg, directive[256];
H. Peter Anvine2c80182005-01-15 22:15:51 +00002406 int size = StackSize;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002407
H. Peter Anvine2c80182005-01-15 22:15:51 +00002408 /* Find the argument name */
2409 tline = tline->next;
2410 if (tline && tline->type == TOK_WHITESPACE)
2411 tline = tline->next;
2412 if (!tline || tline->type != TOK_ID) {
2413 error(ERR_NONFATAL, "`%%arg' missing argument parameter");
2414 free_tlist(origline);
2415 return DIRECTIVE_FOUND;
2416 }
2417 arg = tline->text;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002418
H. Peter Anvine2c80182005-01-15 22:15:51 +00002419 /* Find the argument size type */
2420 tline = tline->next;
2421 if (!tline || tline->type != TOK_OTHER
2422 || tline->text[0] != ':') {
2423 error(ERR_NONFATAL,
2424 "Syntax error processing `%%arg' directive");
2425 free_tlist(origline);
2426 return DIRECTIVE_FOUND;
2427 }
2428 tline = tline->next;
2429 if (!tline || tline->type != TOK_ID) {
2430 error(ERR_NONFATAL, "`%%arg' missing size type parameter");
2431 free_tlist(origline);
2432 return DIRECTIVE_FOUND;
2433 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002434
H. Peter Anvine2c80182005-01-15 22:15:51 +00002435 /* Allow macro expansion of type parameter */
Keith Kaniosb7a89542007-04-12 02:40:54 +00002436 tt = tokenize(tline->text);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002437 tt = expand_smacro(tt);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002438 size = parse_size(tt->text);
2439 if (!size) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002440 error(ERR_NONFATAL,
2441 "Invalid size type for `%%arg' missing directive");
2442 free_tlist(tt);
2443 free_tlist(origline);
2444 return DIRECTIVE_FOUND;
2445 }
2446 free_tlist(tt);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002447
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002448 /* Round up to even stack slots */
2449 size = ALIGN(size, StackSize);
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002450
H. Peter Anvine2c80182005-01-15 22:15:51 +00002451 /* Now define the macro for the argument */
2452 snprintf(directive, sizeof(directive), "%%define %s (%s+%d)",
2453 arg, StackPointer, offset);
Keith Kaniosb7a89542007-04-12 02:40:54 +00002454 do_directive(tokenize(directive));
H. Peter Anvine2c80182005-01-15 22:15:51 +00002455 offset += size;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002456
H. Peter Anvine2c80182005-01-15 22:15:51 +00002457 /* Move to the next argument in the list */
2458 tline = tline->next;
2459 if (tline && tline->type == TOK_WHITESPACE)
2460 tline = tline->next;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002461 } while (tline && tline->type == TOK_OTHER && tline->text[0] == ',');
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002462 ArgOffset = offset;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002463 free_tlist(origline);
2464 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002465
H. Peter Anvine2c80182005-01-15 22:15:51 +00002466 case PP_LOCAL:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002467 if (defining != NULL) return NO_DIRECTIVE_FOUND;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002468 /* TASM like LOCAL directive to define local variables for a
2469 * function, in the following form:
2470 *
2471 * LOCAL local1:WORD, local2:DWORD, local4:QWORD = LocalSize
2472 *
2473 * The '= LocalSize' at the end is ignored by NASM, but is
2474 * required by TASM to define the local parameter size (and used
2475 * by the TASM macro package).
2476 */
2477 offset = LocalOffset;
2478 do {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00002479 char *local, directive[256];
H. Peter Anvine2c80182005-01-15 22:15:51 +00002480 int size = StackSize;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002481
H. Peter Anvine2c80182005-01-15 22:15:51 +00002482 /* Find the argument name */
2483 tline = tline->next;
2484 if (tline && tline->type == TOK_WHITESPACE)
2485 tline = tline->next;
2486 if (!tline || tline->type != TOK_ID) {
2487 error(ERR_NONFATAL,
2488 "`%%local' missing argument parameter");
2489 free_tlist(origline);
2490 return DIRECTIVE_FOUND;
2491 }
2492 local = tline->text;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002493
H. Peter Anvine2c80182005-01-15 22:15:51 +00002494 /* Find the argument size type */
2495 tline = tline->next;
2496 if (!tline || tline->type != TOK_OTHER
2497 || tline->text[0] != ':') {
2498 error(ERR_NONFATAL,
2499 "Syntax error processing `%%local' directive");
2500 free_tlist(origline);
2501 return DIRECTIVE_FOUND;
2502 }
2503 tline = tline->next;
2504 if (!tline || tline->type != TOK_ID) {
2505 error(ERR_NONFATAL,
2506 "`%%local' missing size type parameter");
2507 free_tlist(origline);
2508 return DIRECTIVE_FOUND;
2509 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00002510
H. Peter Anvine2c80182005-01-15 22:15:51 +00002511 /* Allow macro expansion of type parameter */
Keith Kaniosb7a89542007-04-12 02:40:54 +00002512 tt = tokenize(tline->text);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002513 tt = expand_smacro(tt);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002514 size = parse_size(tt->text);
2515 if (!size) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002516 error(ERR_NONFATAL,
2517 "Invalid size type for `%%local' missing directive");
2518 free_tlist(tt);
2519 free_tlist(origline);
2520 return DIRECTIVE_FOUND;
2521 }
2522 free_tlist(tt);
H. Peter Anvin734b1882002-04-30 21:01:08 +00002523
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002524 /* Round up to even stack slots */
2525 size = ALIGN(size, StackSize);
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002526
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002527 offset += size; /* Negative offset, increment before */
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002528
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002529 /* Now define the macro for the argument */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002530 snprintf(directive, sizeof(directive), "%%define %s (%s-%d)",
2531 local, StackPointer, offset);
Keith Kaniosb7a89542007-04-12 02:40:54 +00002532 do_directive(tokenize(directive));
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002533
H. Peter Anvine2c80182005-01-15 22:15:51 +00002534 /* Now define the assign to setup the enter_c macro correctly */
2535 snprintf(directive, sizeof(directive),
2536 "%%assign %%$localsize %%$localsize+%d", size);
Keith Kaniosb7a89542007-04-12 02:40:54 +00002537 do_directive(tokenize(directive));
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002538
H. Peter Anvine2c80182005-01-15 22:15:51 +00002539 /* Move to the next argument in the list */
2540 tline = tline->next;
2541 if (tline && tline->type == TOK_WHITESPACE)
2542 tline = tline->next;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002543 } while (tline && tline->type == TOK_OTHER && tline->text[0] == ',');
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002544 LocalOffset = offset;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002545 free_tlist(origline);
2546 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002547
H. Peter Anvine2c80182005-01-15 22:15:51 +00002548 case PP_CLEAR:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002549 if (defining != NULL) return NO_DIRECTIVE_FOUND;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002550 if (tline->next)
H. Peter Anvin917a3492008-09-24 09:14:49 -07002551 error(ERR_WARNING|ERR_PASS1,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002552 "trailing garbage after `%%clear' ignored");
2553 free_macros();
2554 init_macros();
H. Peter Anvine2c80182005-01-15 22:15:51 +00002555 free_tlist(origline);
2556 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002557
H. Peter Anvin418ca702008-05-30 10:42:30 -07002558 case PP_DEPEND:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002559 if (defining != NULL) return NO_DIRECTIVE_FOUND;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002560 t = tline->next = expand_smacro(tline->next);
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002561 skip_white_(t);
2562 if (!t || (t->type != TOK_STRING &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002563 t->type != TOK_INTERNAL_STRING)) {
H. Peter Anvin418ca702008-05-30 10:42:30 -07002564 error(ERR_NONFATAL, "`%%depend' expects a file name");
2565 free_tlist(origline);
2566 return DIRECTIVE_FOUND; /* but we did _something_ */
2567 }
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002568 if (t->next)
H. Peter Anvin917a3492008-09-24 09:14:49 -07002569 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvin418ca702008-05-30 10:42:30 -07002570 "trailing garbage after `%%depend' ignored");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002571 p = t->text;
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002572 if (t->type != TOK_INTERNAL_STRING)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002573 nasm_unquote_cstr(p, i);
2574 if (dephead && !in_list(*dephead, p)) {
2575 StrList *sl = nasm_malloc(strlen(p)+1+sizeof sl->next);
2576 sl->next = NULL;
2577 strcpy(sl->str, p);
2578 *deptail = sl;
2579 deptail = &sl->next;
2580 }
2581 free_tlist(origline);
H. Peter Anvin418ca702008-05-30 10:42:30 -07002582 return DIRECTIVE_FOUND;
2583
2584 case PP_INCLUDE:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002585 if (defining != NULL) return NO_DIRECTIVE_FOUND;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002586 t = tline->next = expand_smacro(tline->next);
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002587 skip_white_(t);
H. Peter Anvind2456592008-06-19 15:04:18 -07002588
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002589 if (!t || (t->type != TOK_STRING &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002590 t->type != TOK_INTERNAL_STRING)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002591 error(ERR_NONFATAL, "`%%include' expects a file name");
2592 free_tlist(origline);
2593 return DIRECTIVE_FOUND; /* but we did _something_ */
2594 }
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002595 if (t->next)
H. Peter Anvin917a3492008-09-24 09:14:49 -07002596 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +00002597 "trailing garbage after `%%include' ignored");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002598 p = t->text;
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002599 if (t->type != TOK_INTERNAL_STRING)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002600 nasm_unquote_cstr(p, i);
Cyrill Gorcunov55cc4d02010-11-10 23:12:06 +03002601 inc = nasm_zalloc(sizeof(Include));
H. Peter Anvine2c80182005-01-15 22:15:51 +00002602 inc->next = istk;
H. Peter Anvin2b1c3b92008-06-06 10:38:46 -07002603 inc->fp = inc_fopen(p, dephead, &deptail, pass == 0);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002604 if (!inc->fp) {
2605 /* -MG given but file not found */
2606 nasm_free(inc);
2607 } else {
2608 inc->fname = src_set_fname(nasm_strdup(p));
2609 inc->lineno = src_set_linnum(0);
2610 inc->lineinc = 1;
2611 inc->expansion = NULL;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002612 istk = inc;
2613 list->uplevel(LIST_INCLUDE);
2614 }
2615 free_tlist(origline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002616 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002617
H. Peter Anvind2456592008-06-19 15:04:18 -07002618 case PP_USE:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002619 if (defining != NULL) return NO_DIRECTIVE_FOUND;
H. Peter Anvinf4ae5ad2008-06-19 18:39:24 -07002620 {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002621 static macros_t *use_pkg;
2622 const char *pkg_macro = NULL;
H. Peter Anvinf4ae5ad2008-06-19 18:39:24 -07002623
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002624 tline = tline->next;
2625 skip_white_(tline);
2626 tline = expand_id(tline);
H. Peter Anvind2456592008-06-19 15:04:18 -07002627
H. Peter Anvin264b7b92008-10-24 16:38:17 -07002628 if (!tline || (tline->type != TOK_STRING &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002629 tline->type != TOK_INTERNAL_STRING &&
2630 tline->type != TOK_ID)) {
H. Peter Anvin926fc402008-06-19 16:26:12 -07002631 error(ERR_NONFATAL, "`%%use' expects a package name");
H. Peter Anvind2456592008-06-19 15:04:18 -07002632 free_tlist(origline);
2633 return DIRECTIVE_FOUND; /* but we did _something_ */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002634 }
H. Peter Anvin264b7b92008-10-24 16:38:17 -07002635 if (tline->next)
H. Peter Anvin917a3492008-09-24 09:14:49 -07002636 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvind2456592008-06-19 15:04:18 -07002637 "trailing garbage after `%%use' ignored");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002638 if (tline->type == TOK_STRING)
2639 nasm_unquote_cstr(tline->text, i);
2640 use_pkg = nasm_stdmac_find_package(tline->text);
2641 if (!use_pkg)
2642 error(ERR_NONFATAL, "unknown `%%use' package: %s", tline->text);
2643 else
2644 pkg_macro = (char *)use_pkg + 1; /* The first string will be <%define>__USE_*__ */
Victor van den Elzen35eb2ea2010-03-10 22:33:48 +01002645 if (use_pkg && ! smacro_defined(NULL, pkg_macro, 0, NULL, true)) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002646 /* Not already included, go ahead and include it */
2647 stdmacpos = use_pkg;
2648 }
2649 free_tlist(origline);
H. Peter Anvind2456592008-06-19 15:04:18 -07002650 return DIRECTIVE_FOUND;
H. Peter Anvinf4ae5ad2008-06-19 18:39:24 -07002651 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002652 case PP_PUSH:
H. Peter Anvine2c80182005-01-15 22:15:51 +00002653 case PP_REPL:
H. Peter Anvin42b56392008-10-24 16:24:21 -07002654 case PP_POP:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002655 if (defining != NULL) return NO_DIRECTIVE_FOUND;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002656 tline = tline->next;
2657 skip_white_(tline);
2658 tline = expand_id(tline);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002659 if (tline) {
2660 if (!tok_type_(tline, TOK_ID)) {
2661 error(ERR_NONFATAL, "`%s' expects a context identifier",
2662 pp_directives[i]);
2663 free_tlist(origline);
2664 return DIRECTIVE_FOUND; /* but we did _something_ */
2665 }
2666 if (tline->next)
2667 error(ERR_WARNING|ERR_PASS1,
2668 "trailing garbage after `%s' ignored",
2669 pp_directives[i]);
2670 p = nasm_strdup(tline->text);
2671 } else {
2672 p = NULL; /* Anonymous */
2673 }
H. Peter Anvin42b56392008-10-24 16:24:21 -07002674
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002675 if (i == PP_PUSH) {
2676 ctx = nasm_malloc(sizeof(Context));
2677 ctx->next = cstk;
2678 hash_init(&ctx->localmac, HASH_SMALL);
2679 ctx->name = p;
2680 ctx->number = unique++;
2681 cstk = ctx;
2682 } else {
2683 /* %pop or %repl */
2684 if (!cstk) {
2685 error(ERR_NONFATAL, "`%s': context stack is empty",
2686 pp_directives[i]);
2687 } else if (i == PP_POP) {
2688 if (p && (!cstk->name || nasm_stricmp(p, cstk->name)))
2689 error(ERR_NONFATAL, "`%%pop' in wrong context: %s, "
2690 "expected %s",
2691 cstk->name ? cstk->name : "anonymous", p);
2692 else
2693 ctx_pop();
2694 } else {
2695 /* i == PP_REPL */
2696 nasm_free(cstk->name);
2697 cstk->name = p;
2698 p = NULL;
2699 }
2700 nasm_free(p);
2701 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002702 free_tlist(origline);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002703 return DIRECTIVE_FOUND;
H. Peter Anvin8e3f75e2008-09-24 00:21:58 -07002704 case PP_FATAL:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002705 severity = ERR_FATAL;
2706 goto issue_error;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002707 case PP_ERROR:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002708 severity = ERR_NONFATAL;
2709 goto issue_error;
H. Peter Anvin7df04172008-06-10 18:27:38 -07002710 case PP_WARNING:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002711 severity = ERR_WARNING|ERR_WARN_USER;
2712 goto issue_error;
H. Peter Anvin8e3f75e2008-09-24 00:21:58 -07002713
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002714issue_error:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002715 if (defining != NULL) return NO_DIRECTIVE_FOUND;
H. Peter Anvin7df04172008-06-10 18:27:38 -07002716 {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002717 /* Only error out if this is the final pass */
2718 if (pass != 2 && i != PP_FATAL)
2719 return DIRECTIVE_FOUND;
2720
2721 tline->next = expand_smacro(tline->next);
2722 tline = tline->next;
2723 skip_white_(tline);
2724 t = tline ? tline->next : NULL;
2725 skip_white_(t);
2726 if (tok_type_(tline, TOK_STRING) && !t) {
2727 /* The line contains only a quoted string */
2728 p = tline->text;
2729 nasm_unquote(p, NULL); /* Ignore NUL character truncation */
2730 error(severity, "%s", p);
2731 } else {
2732 /* Not a quoted string, or more than a quoted string */
2733 p = detoken(tline, false);
2734 error(severity, "%s", p);
2735 nasm_free(p);
2736 }
2737 free_tlist(origline);
2738 return DIRECTIVE_FOUND;
H. Peter Anvin7df04172008-06-10 18:27:38 -07002739 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00002740
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002741 CASE_PP_IF:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002742 if (defining != NULL) {
2743 if (defining->type == EXP_IF) {
2744 defining->def_depth ++;
2745 }
2746 return NO_DIRECTIVE_FOUND;
2747 }
2748 if ((istk->expansion != NULL) &&
2749 (istk->expansion->emitting == false)) {
2750 j = COND_NEVER;
2751 } else {
2752 j = if_condition(tline->next, i);
2753 tline->next = NULL; /* it got freed */
2754 j = (((j < 0) ? COND_NEVER : j) ? COND_IF_TRUE : COND_IF_FALSE);
2755 }
2756 ed = new_ExpDef(EXP_IF);
2757 ed->state = j;
2758 ed->nolist = NULL;
2759 ed->def_depth = 0;
2760 ed->cur_depth = 0;
2761 ed->max_depth = 0;
2762 ed->ignoring = ((ed->state == COND_IF_TRUE) ? false : true);
2763 ed->prev = defining;
2764 defining = ed;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002765 free_tlist(origline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002766 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002767
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002768 CASE_PP_ELIF:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002769 if (defining != NULL) {
2770 if ((defining->type != EXP_IF) || (defining->def_depth > 0)) {
2771 return NO_DIRECTIVE_FOUND;
2772 }
2773 }
2774 if ((defining == NULL) || (defining->type != EXP_IF)) {
2775 error(ERR_FATAL, "`%s': no matching `%%if'", pp_directives[i]);
2776 }
Keith Kaniosb307a4f2010-11-06 17:41:51 -05002777 switch (defining->state) {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002778 case COND_IF_TRUE:
2779 defining->state = COND_DONE;
2780 defining->ignoring = true;
2781 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002782
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002783 case COND_DONE:
2784 case COND_NEVER:
2785 defining->ignoring = true;
2786 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002787
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002788 case COND_ELSE_TRUE:
2789 case COND_ELSE_FALSE:
2790 error_precond(ERR_WARNING|ERR_PASS1,
2791 "`%%elif' after `%%else' ignored");
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002792 defining->state = COND_NEVER;
2793 defining->ignoring = true;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002794 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002795
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002796 case COND_IF_FALSE:
2797 /*
2798 * IMPORTANT: In the case of %if, we will already have
2799 * called expand_mmac_params(); however, if we're
2800 * processing an %elif we must have been in a
2801 * non-emitting mode, which would have inhibited
2802 * the normal invocation of expand_mmac_params().
2803 * Therefore, we have to do it explicitly here.
2804 */
2805 j = if_condition(expand_mmac_params(tline->next), i);
2806 tline->next = NULL; /* it got freed */
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002807 defining->state =
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002808 j < 0 ? COND_NEVER : j ? COND_IF_TRUE : COND_IF_FALSE;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002809 defining->ignoring = ((defining->state == COND_IF_TRUE) ? false : true);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002810 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002811 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002812 free_tlist(origline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002813 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002814
H. Peter Anvine2c80182005-01-15 22:15:51 +00002815 case PP_ELSE:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002816 if (defining != NULL) {
2817 if ((defining->type != EXP_IF) || (defining->def_depth > 0)) {
2818 return NO_DIRECTIVE_FOUND;
2819 }
2820 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002821 if (tline->next)
H. Peter Anvin917a3492008-09-24 09:14:49 -07002822 error_precond(ERR_WARNING|ERR_PASS1,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002823 "trailing garbage after `%%else' ignored");
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002824 if ((defining == NULL) || (defining->type != EXP_IF)) {
2825 error(ERR_FATAL, "`%s': no matching `%%if'", pp_directives[i]);
2826 }
Keith Kaniosb307a4f2010-11-06 17:41:51 -05002827 switch (defining->state) {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002828 case COND_IF_TRUE:
2829 case COND_DONE:
2830 defining->state = COND_ELSE_FALSE;
2831 defining->ignoring = true;
2832 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002833
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002834 case COND_NEVER:
2835 defining->ignoring = true;
2836 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002837
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002838 case COND_IF_FALSE:
2839 defining->state = COND_ELSE_TRUE;
2840 defining->ignoring = false;
2841 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002842
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002843 case COND_ELSE_TRUE:
2844 case COND_ELSE_FALSE:
2845 error_precond(ERR_WARNING|ERR_PASS1,
2846 "`%%else' after `%%else' ignored.");
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002847 defining->state = COND_NEVER;
2848 defining->ignoring = true;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002849 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002850 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002851 free_tlist(origline);
2852 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002853
H. Peter Anvine2c80182005-01-15 22:15:51 +00002854 case PP_ENDIF:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002855 if (defining != NULL) {
2856 if (defining->type == EXP_IF) {
2857 if (defining->def_depth > 0) {
2858 defining->def_depth --;
2859 return NO_DIRECTIVE_FOUND;
2860 }
2861 } else {
2862 return NO_DIRECTIVE_FOUND;
2863 }
2864 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002865 if (tline->next)
H. Peter Anvin917a3492008-09-24 09:14:49 -07002866 error_precond(ERR_WARNING|ERR_PASS1,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002867 "trailing garbage after `%%endif' ignored");
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002868 if ((defining == NULL) || (defining->type != EXP_IF)) {
2869 error(ERR_NONFATAL, "`%%endif': no matching `%%if'");
2870 return DIRECTIVE_FOUND;
2871 }
2872 ed = defining;
2873 defining = ed->prev;
2874 ed->prev = expansions;
2875 expansions = ed;
2876 ei = new_ExpInv(EXP_IF, ed);
2877 ei->current = ed->line;
2878 ei->emitting = true;
2879 ei->prev = istk->expansion;
2880 istk->expansion = ei;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002881 free_tlist(origline);
2882 return DIRECTIVE_FOUND;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002883
H. Peter Anvindb8f96e2009-07-15 09:07:29 -04002884 case PP_RMACRO:
2885 case PP_IRMACRO:
H. Peter Anvine2c80182005-01-15 22:15:51 +00002886 case PP_MACRO:
2887 case PP_IMACRO:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002888 if (defining != NULL) {
2889 if (defining->type == EXP_MMACRO) {
2890 defining->def_depth ++;
2891 }
2892 return NO_DIRECTIVE_FOUND;
2893 }
2894 ed = new_ExpDef(EXP_MMACRO);
2895 ed->max_depth =
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002896 (i == PP_RMACRO) || (i == PP_IRMACRO) ? DEADMAN_LIMIT : 0;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002897 ed->casesense = (i == PP_MACRO) || (i == PP_RMACRO);
Keith Kaniosb307a4f2010-11-06 17:41:51 -05002898 if (!parse_mmacro_spec(tline, ed, pp_directives[i])) {
2899 nasm_free(ed);
2900 ed = NULL;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002901 return DIRECTIVE_FOUND;
2902 }
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002903 ed->def_depth = 0;
2904 ed->cur_depth = 0;
2905 ed->max_depth = (ed->max_depth + 1);
2906 ed->ignoring = false;
2907 ed->prev = defining;
2908 defining = ed;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002909
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002910 eed = (ExpDef *) hash_findix(&expdefs, ed->name);
2911 while (eed) {
2912 if (!strcmp(eed->name, ed->name) &&
2913 (eed->nparam_min <= ed->nparam_max
2914 || ed->plus)
2915 && (ed->nparam_min <= eed->nparam_max
2916 || eed->plus)) {
2917 error(ERR_WARNING|ERR_PASS1,
2918 "redefining multi-line macro `%s'", ed->name);
2919 return DIRECTIVE_FOUND;
2920 }
2921 eed = eed->next;
2922 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002923 free_tlist(origline);
2924 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002925
H. Peter Anvine2c80182005-01-15 22:15:51 +00002926 case PP_ENDM:
2927 case PP_ENDMACRO:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002928 if (defining != NULL) {
2929 if (defining->type == EXP_MMACRO) {
2930 if (defining->def_depth > 0) {
2931 defining->def_depth --;
2932 return NO_DIRECTIVE_FOUND;
2933 }
2934 } else {
2935 return NO_DIRECTIVE_FOUND;
2936 }
2937 }
2938 if (!(defining) || (defining->type != EXP_MMACRO)) {
2939 error(ERR_NONFATAL, "`%s': not defining a macro", tline->text);
2940 return DIRECTIVE_FOUND;
2941 }
2942 edhead = (ExpDef **) hash_findi_add(&expdefs, defining->name);
2943 defining->next = *edhead;
2944 *edhead = defining;
2945 ed = defining;
2946 defining = ed->prev;
2947 ed->prev = expansions;
2948 expansions = ed;
2949 ed = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002950 free_tlist(origline);
2951 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002952
H. Peter Anvin89cee572009-07-15 09:16:54 -04002953 case PP_EXITMACRO:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002954 if (defining != NULL) return NO_DIRECTIVE_FOUND;
2955 /*
2956 * We must search along istk->expansion until we hit a
2957 * macro invocation. Then we disable the emitting state(s)
2958 * between exitmacro and endmacro.
2959 */
2960 for (ei = istk->expansion; ei != NULL; ei = ei->prev) {
2961 if(ei->type == EXP_MMACRO) {
2962 break;
2963 }
2964 }
2965
2966 if (ei != NULL) {
2967 /*
2968 * Set all invocations leading back to the macro
2969 * invocation to a non-emitting state.
2970 */
2971 for (eei = istk->expansion; eei != ei; eei = eei->prev) {
2972 eei->emitting = false;
2973 }
2974 eei->emitting = false;
2975 } else {
2976 error(ERR_NONFATAL, "`%%exitmacro' not within `%%macro' block");
2977 }
Keith Kanios852f1ee2009-07-12 00:19:55 -05002978 free_tlist(origline);
2979 return DIRECTIVE_FOUND;
2980
H. Peter Anvina26433d2008-07-16 14:40:01 -07002981 case PP_UNMACRO:
2982 case PP_UNIMACRO:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002983 if (defining != NULL) return NO_DIRECTIVE_FOUND;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002984 {
Keith Kaniosb307a4f2010-11-06 17:41:51 -05002985 ExpDef **ed_p;
2986 ExpDef spec;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002987
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002988 spec.casesense = (i == PP_UNMACRO);
2989 if (!parse_mmacro_spec(tline, &spec, pp_directives[i])) {
2990 return DIRECTIVE_FOUND;
2991 }
Keith Kaniosb307a4f2010-11-06 17:41:51 -05002992 ed_p = (ExpDef **) hash_findi(&expdefs, spec.name, NULL);
2993 while (ed_p && *ed_p) {
2994 ed = *ed_p;
2995 if (ed->casesense == spec.casesense &&
2996 !mstrcmp(ed->name, spec.name, spec.casesense) &&
2997 ed->nparam_min == spec.nparam_min &&
2998 ed->nparam_max == spec.nparam_max &&
2999 ed->plus == spec.plus) {
3000 *ed_p = ed->next;
3001 free_expdef(ed);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003002 } else {
Keith Kaniosb307a4f2010-11-06 17:41:51 -05003003 ed_p = &ed->next;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003004 }
3005 }
3006 free_tlist(origline);
3007 free_tlist(spec.dlist);
3008 return DIRECTIVE_FOUND;
H. Peter Anvina26433d2008-07-16 14:40:01 -07003009 }
3010
H. Peter Anvine2c80182005-01-15 22:15:51 +00003011 case PP_ROTATE:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003012 if (defining != NULL) return NO_DIRECTIVE_FOUND;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003013 if (tline->next && tline->next->type == TOK_WHITESPACE)
3014 tline = tline->next;
H. Peter Anvin89cee572009-07-15 09:16:54 -04003015 if (!tline->next) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00003016 free_tlist(origline);
3017 error(ERR_NONFATAL, "`%%rotate' missing rotate count");
3018 return DIRECTIVE_FOUND;
3019 }
3020 t = expand_smacro(tline->next);
3021 tline->next = NULL;
3022 free_tlist(origline);
3023 tline = t;
3024 tptr = &t;
3025 tokval.t_type = TOKEN_INVALID;
3026 evalresult =
3027 evaluate(ppscan, tptr, &tokval, NULL, pass, error, NULL);
3028 free_tlist(tline);
3029 if (!evalresult)
3030 return DIRECTIVE_FOUND;
3031 if (tokval.t_type)
H. Peter Anvin917a3492008-09-24 09:14:49 -07003032 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +00003033 "trailing garbage after expression ignored");
3034 if (!is_simple(evalresult)) {
3035 error(ERR_NONFATAL, "non-constant value given to `%%rotate'");
3036 return DIRECTIVE_FOUND;
3037 }
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003038 for (ei = istk->expansion; ei != NULL; ei = ei->prev) {
3039 if (ei->type == EXP_MMACRO) {
3040 break;
3041 }
3042 }
3043 if (ei == NULL) {
3044 error(ERR_NONFATAL, "`%%rotate' invoked outside a macro call");
3045 } else if (ei->nparam == 0) {
3046 error(ERR_NONFATAL,
3047 "`%%rotate' invoked within macro without parameters");
3048 } else {
3049 int rotate = ei->rotate + reloc_value(evalresult);
3050
3051 rotate %= (int)ei->nparam;
3052 if (rotate < 0)
3053 rotate += ei->nparam;
3054 ei->rotate = rotate;
3055 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00003056 return DIRECTIVE_FOUND;
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00003057
H. Peter Anvine2c80182005-01-15 22:15:51 +00003058 case PP_REP:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003059 if (defining != NULL) {
3060 if (defining->type == EXP_REP) {
3061 defining->def_depth ++;
3062 }
3063 return NO_DIRECTIVE_FOUND;
3064 }
H. Peter Anvin6867acc2007-10-10 14:58:45 -07003065 nolist = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003066 do {
3067 tline = tline->next;
3068 } while (tok_type_(tline, TOK_WHITESPACE));
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00003069
H. Peter Anvine2c80182005-01-15 22:15:51 +00003070 if (tok_type_(tline, TOK_ID) &&
3071 nasm_stricmp(tline->text, ".nolist") == 0) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07003072 nolist = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003073 do {
3074 tline = tline->next;
3075 } while (tok_type_(tline, TOK_WHITESPACE));
3076 }
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00003077
H. Peter Anvine2c80182005-01-15 22:15:51 +00003078 if (tline) {
3079 t = expand_smacro(tline);
3080 tptr = &t;
3081 tokval.t_type = TOKEN_INVALID;
3082 evalresult =
3083 evaluate(ppscan, tptr, &tokval, NULL, pass, error, NULL);
3084 if (!evalresult) {
3085 free_tlist(origline);
3086 return DIRECTIVE_FOUND;
3087 }
3088 if (tokval.t_type)
H. Peter Anvin917a3492008-09-24 09:14:49 -07003089 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +00003090 "trailing garbage after expression ignored");
3091 if (!is_simple(evalresult)) {
3092 error(ERR_NONFATAL, "non-constant value given to `%%rep'");
3093 return DIRECTIVE_FOUND;
3094 }
Cyrill Gorcunove091d6e2010-08-09 13:58:22 +04003095 count = reloc_value(evalresult);
3096 if (count >= REP_LIMIT) {
Cyrill Gorcunov71f4f842010-08-09 20:17:17 +04003097 error(ERR_NONFATAL, "`%%rep' value exceeds limit");
Cyrill Gorcunove091d6e2010-08-09 13:58:22 +04003098 count = 0;
3099 } else
3100 count++;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003101 } else {
3102 error(ERR_NONFATAL, "`%%rep' expects a repeat count");
H. Peter Anvinf8ba53e2007-10-11 10:11:57 -07003103 count = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003104 }
3105 free_tlist(origline);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003106 ed = new_ExpDef(EXP_REP);
3107 ed->nolist = nolist;
3108 ed->def_depth = 0;
3109 ed->cur_depth = 1;
3110 ed->max_depth = (count - 1);
3111 ed->ignoring = false;
3112 ed->prev = defining;
3113 defining = ed;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003114 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003115
H. Peter Anvine2c80182005-01-15 22:15:51 +00003116 case PP_ENDREP:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003117 if (defining != NULL) {
3118 if (defining->type == EXP_REP) {
3119 if (defining->def_depth > 0) {
3120 defining->def_depth --;
3121 return NO_DIRECTIVE_FOUND;
3122 }
3123 } else {
3124 return NO_DIRECTIVE_FOUND;
3125 }
3126 }
Keith Kaniosb307a4f2010-11-06 17:41:51 -05003127 if ((defining == NULL) || (defining->type != EXP_REP)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00003128 error(ERR_NONFATAL, "`%%endrep': no matching `%%rep'");
3129 return DIRECTIVE_FOUND;
3130 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003131
H. Peter Anvine2c80182005-01-15 22:15:51 +00003132 /*
3133 * Now we have a "macro" defined - although it has no name
3134 * and we won't be entering it in the hash tables - we must
3135 * push a macro-end marker for it on to istk->expansion.
3136 * After that, it will take care of propagating itself (a
3137 * macro-end marker line for a macro which is really a %rep
3138 * block will cause the macro to be re-expanded, complete
3139 * with another macro-end marker to ensure the process
3140 * continues) until the whole expansion is forcibly removed
3141 * from istk->expansion by a %exitrep.
3142 */
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003143 ed = defining;
3144 defining = ed->prev;
3145 ed->prev = expansions;
3146 expansions = ed;
3147 ei = new_ExpInv(EXP_REP, ed);
3148 ei->current = ed->line;
3149 ei->emitting = ((ed->max_depth > 0) ? true : false);
3150 list->uplevel(ed->nolist ? LIST_MACRO_NOLIST : LIST_MACRO);
3151 ei->prev = istk->expansion;
3152 istk->expansion = ei;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003153 free_tlist(origline);
3154 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003155
H. Peter Anvine2c80182005-01-15 22:15:51 +00003156 case PP_EXITREP:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003157 if (defining != NULL) return NO_DIRECTIVE_FOUND;
3158 /*
3159 * We must search along istk->expansion until we hit a
3160 * rep invocation. Then we disable the emitting state(s)
3161 * between exitrep and endrep.
3162 */
3163 for (ei = istk->expansion; ei != NULL; ei = ei->prev) {
3164 if (ei->type == EXP_REP) {
3165 break;
3166 }
3167 }
3168
3169 if (ei != NULL) {
3170 /*
3171 * Set all invocations leading back to the rep
3172 * invocation to a non-emitting state.
3173 */
3174 for (eei = istk->expansion; eei != ei; eei = eei->prev) {
3175 eei->emitting = false;
3176 }
3177 eei->emitting = false;
3178 eei->current = NULL;
3179 eei->def->cur_depth = eei->def->max_depth;
3180 } else {
3181 error(ERR_NONFATAL, "`%%exitrep' not within `%%rep' block");
3182 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00003183 free_tlist(origline);
3184 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003185
H. Peter Anvine2c80182005-01-15 22:15:51 +00003186 case PP_XDEFINE:
3187 case PP_IXDEFINE:
3188 case PP_DEFINE:
3189 case PP_IDEFINE:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003190 if (defining != NULL) return NO_DIRECTIVE_FOUND;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003191 casesense = (i == PP_DEFINE || i == PP_XDEFINE);
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07003192
H. Peter Anvine2c80182005-01-15 22:15:51 +00003193 tline = tline->next;
3194 skip_white_(tline);
3195 tline = expand_id(tline);
3196 if (!tline || (tline->type != TOK_ID &&
3197 (tline->type != TOK_PREPROC_ID ||
3198 tline->text[1] != '$'))) {
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07003199 error(ERR_NONFATAL, "`%s' expects a macro identifier",
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003200 pp_directives[i]);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003201 free_tlist(origline);
3202 return DIRECTIVE_FOUND;
3203 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003204
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08003205 ctx = get_ctx(tline->text, &mname, false);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003206 last = tline;
3207 param_start = tline = tline->next;
3208 nparam = 0;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003209
H. Peter Anvine2c80182005-01-15 22:15:51 +00003210 /* Expand the macro definition now for %xdefine and %ixdefine */
3211 if ((i == PP_XDEFINE) || (i == PP_IXDEFINE))
3212 tline = expand_smacro(tline);
H. Peter Anvin734b1882002-04-30 21:01:08 +00003213
H. Peter Anvine2c80182005-01-15 22:15:51 +00003214 if (tok_is_(tline, "(")) {
3215 /*
3216 * This macro has parameters.
3217 */
H. Peter Anvin734b1882002-04-30 21:01:08 +00003218
H. Peter Anvine2c80182005-01-15 22:15:51 +00003219 tline = tline->next;
3220 while (1) {
3221 skip_white_(tline);
3222 if (!tline) {
3223 error(ERR_NONFATAL, "parameter identifier expected");
3224 free_tlist(origline);
3225 return DIRECTIVE_FOUND;
3226 }
3227 if (tline->type != TOK_ID) {
3228 error(ERR_NONFATAL,
3229 "`%s': parameter identifier expected",
3230 tline->text);
3231 free_tlist(origline);
3232 return DIRECTIVE_FOUND;
3233 }
3234 tline->type = TOK_SMAC_PARAM + nparam++;
3235 tline = tline->next;
3236 skip_white_(tline);
3237 if (tok_is_(tline, ",")) {
3238 tline = tline->next;
H. Peter Anvinca348b62008-07-23 10:49:26 -04003239 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003240 if (!tok_is_(tline, ")")) {
3241 error(ERR_NONFATAL,
3242 "`)' expected to terminate macro template");
3243 free_tlist(origline);
3244 return DIRECTIVE_FOUND;
3245 }
3246 break;
3247 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00003248 }
3249 last = tline;
3250 tline = tline->next;
3251 }
3252 if (tok_type_(tline, TOK_WHITESPACE))
3253 last = tline, tline = tline->next;
3254 macro_start = NULL;
3255 last->next = NULL;
3256 t = tline;
3257 while (t) {
3258 if (t->type == TOK_ID) {
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04003259 list_for_each(tt, param_start)
H. Peter Anvine2c80182005-01-15 22:15:51 +00003260 if (tt->type >= TOK_SMAC_PARAM &&
3261 !strcmp(tt->text, t->text))
3262 t->type = tt->type;
3263 }
3264 tt = t->next;
3265 t->next = macro_start;
3266 macro_start = t;
3267 t = tt;
3268 }
3269 /*
3270 * Good. We now have a macro name, a parameter count, and a
3271 * token list (in reverse order) for an expansion. We ought
3272 * to be OK just to create an SMacro, store it, and let
3273 * free_tlist have the rest of the line (which we have
3274 * carefully re-terminated after chopping off the expansion
3275 * from the end).
3276 */
H. Peter Anvin4db5a162007-10-11 13:42:09 -07003277 define_smacro(ctx, mname, casesense, nparam, macro_start);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003278 free_tlist(origline);
3279 return DIRECTIVE_FOUND;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003280
H. Peter Anvine2c80182005-01-15 22:15:51 +00003281 case PP_UNDEF:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003282 if (defining != NULL) return NO_DIRECTIVE_FOUND;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003283 tline = tline->next;
3284 skip_white_(tline);
3285 tline = expand_id(tline);
3286 if (!tline || (tline->type != TOK_ID &&
3287 (tline->type != TOK_PREPROC_ID ||
3288 tline->text[1] != '$'))) {
3289 error(ERR_NONFATAL, "`%%undef' expects a macro identifier");
3290 free_tlist(origline);
3291 return DIRECTIVE_FOUND;
3292 }
3293 if (tline->next) {
H. Peter Anvin917a3492008-09-24 09:14:49 -07003294 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +00003295 "trailing garbage after macro name ignored");
3296 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003297
H. Peter Anvine2c80182005-01-15 22:15:51 +00003298 /* Find the context that symbol belongs to */
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08003299 ctx = get_ctx(tline->text, &mname, false);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003300 undef_smacro(ctx, mname);
3301 free_tlist(origline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003302 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003303
H. Peter Anvin9e200162008-06-04 17:23:14 -07003304 case PP_DEFSTR:
3305 case PP_IDEFSTR:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003306 if (defining != NULL) return NO_DIRECTIVE_FOUND;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003307 casesense = (i == PP_DEFSTR);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003308
3309 tline = tline->next;
3310 skip_white_(tline);
3311 tline = expand_id(tline);
3312 if (!tline || (tline->type != TOK_ID &&
3313 (tline->type != TOK_PREPROC_ID ||
3314 tline->text[1] != '$'))) {
3315 error(ERR_NONFATAL, "`%s' expects a macro identifier",
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003316 pp_directives[i]);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003317 free_tlist(origline);
3318 return DIRECTIVE_FOUND;
3319 }
3320
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08003321 ctx = get_ctx(tline->text, &mname, false);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003322 last = tline;
3323 tline = expand_smacro(tline->next);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003324 last->next = NULL;
H. Peter Anvin9e200162008-06-04 17:23:14 -07003325
3326 while (tok_type_(tline, TOK_WHITESPACE))
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003327 tline = delete_Token(tline);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003328
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003329 p = detoken(tline, false);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003330 macro_start = nasm_malloc(sizeof(*macro_start));
3331 macro_start->next = NULL;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003332 macro_start->text = nasm_quote(p, strlen(p));
3333 macro_start->type = TOK_STRING;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003334 macro_start->a.mac = NULL;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003335 nasm_free(p);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003336
3337 /*
3338 * We now have a macro name, an implicit parameter count of
3339 * zero, and a string token to use as an expansion. Create
3340 * and store an SMacro.
3341 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003342 define_smacro(ctx, mname, casesense, 0, macro_start);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003343 free_tlist(origline);
3344 return DIRECTIVE_FOUND;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003345
H. Peter Anvin2f55bda2009-07-14 15:04:04 -04003346 case PP_DEFTOK:
3347 case PP_IDEFTOK:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003348 if (defining != NULL) return NO_DIRECTIVE_FOUND;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003349 casesense = (i == PP_DEFTOK);
3350
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003351 tline = tline->next;
3352 skip_white_(tline);
3353 tline = expand_id(tline);
3354 if (!tline || (tline->type != TOK_ID &&
3355 (tline->type != TOK_PREPROC_ID ||
3356 tline->text[1] != '$'))) {
3357 error(ERR_NONFATAL,
3358 "`%s' expects a macro identifier as first parameter",
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003359 pp_directives[i]);
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003360 free_tlist(origline);
3361 return DIRECTIVE_FOUND;
3362 }
3363 ctx = get_ctx(tline->text, &mname, false);
3364 last = tline;
3365 tline = expand_smacro(tline->next);
3366 last->next = NULL;
3367
3368 t = tline;
3369 while (tok_type_(t, TOK_WHITESPACE))
3370 t = t->next;
3371 /* t should now point to the string */
Cyrill Gorcunov6908e582010-09-06 19:36:15 +04003372 if (!tok_type_(t, TOK_STRING)) {
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003373 error(ERR_NONFATAL,
3374 "`%s` requires string as second parameter",
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003375 pp_directives[i]);
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003376 free_tlist(tline);
3377 free_tlist(origline);
3378 return DIRECTIVE_FOUND;
3379 }
3380
Keith Kaniosb307a4f2010-11-06 17:41:51 -05003381 /*
3382 * Convert the string to a token stream. Note that smacros
3383 * are stored with the token stream reversed, so we have to
3384 * reverse the output of tokenize().
3385 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003386 nasm_unquote_cstr(t->text, i);
H. Peter Anvinb40992c2010-09-15 08:57:21 -07003387 macro_start = reverse_tokens(tokenize(t->text));
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003388
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003389 /*
3390 * We now have a macro name, an implicit parameter count of
3391 * zero, and a numeric token to use as an expansion. Create
3392 * and store an SMacro.
3393 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003394 define_smacro(ctx, mname, casesense, 0, macro_start);
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003395 free_tlist(tline);
3396 free_tlist(origline);
3397 return DIRECTIVE_FOUND;
H. Peter Anvin9e200162008-06-04 17:23:14 -07003398
H. Peter Anvin418ca702008-05-30 10:42:30 -07003399 case PP_PATHSEARCH:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003400 if (defining != NULL) return NO_DIRECTIVE_FOUND;
H. Peter Anvin418ca702008-05-30 10:42:30 -07003401 {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003402 FILE *fp;
3403 StrList *xsl = NULL;
3404 StrList **xst = &xsl;
H. Peter Anvin418ca702008-05-30 10:42:30 -07003405
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003406 casesense = true;
H. Peter Anvin418ca702008-05-30 10:42:30 -07003407
3408 tline = tline->next;
3409 skip_white_(tline);
3410 tline = expand_id(tline);
3411 if (!tline || (tline->type != TOK_ID &&
3412 (tline->type != TOK_PREPROC_ID ||
3413 tline->text[1] != '$'))) {
3414 error(ERR_NONFATAL,
3415 "`%%pathsearch' expects a macro identifier as first parameter");
3416 free_tlist(origline);
3417 return DIRECTIVE_FOUND;
3418 }
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08003419 ctx = get_ctx(tline->text, &mname, false);
H. Peter Anvin418ca702008-05-30 10:42:30 -07003420 last = tline;
3421 tline = expand_smacro(tline->next);
3422 last->next = NULL;
3423
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003424 t = tline;
H. Peter Anvin418ca702008-05-30 10:42:30 -07003425 while (tok_type_(t, TOK_WHITESPACE))
3426 t = t->next;
3427
3428 if (!t || (t->type != TOK_STRING &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003429 t->type != TOK_INTERNAL_STRING)) {
H. Peter Anvin418ca702008-05-30 10:42:30 -07003430 error(ERR_NONFATAL, "`%%pathsearch' expects a file name");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003431 free_tlist(tline);
H. Peter Anvin418ca702008-05-30 10:42:30 -07003432 free_tlist(origline);
3433 return DIRECTIVE_FOUND; /* but we did _something_ */
3434 }
3435 if (t->next)
H. Peter Anvin917a3492008-09-24 09:14:49 -07003436 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvin418ca702008-05-30 10:42:30 -07003437 "trailing garbage after `%%pathsearch' ignored");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003438 p = t->text;
H. Peter Anvin427cc912008-06-01 21:43:03 -07003439 if (t->type != TOK_INTERNAL_STRING)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003440 nasm_unquote(p, NULL);
H. Peter Anvin418ca702008-05-30 10:42:30 -07003441
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003442 fp = inc_fopen(p, &xsl, &xst, true);
3443 if (fp) {
3444 p = xsl->str;
3445 fclose(fp); /* Don't actually care about the file */
3446 }
H. Peter Anvin418ca702008-05-30 10:42:30 -07003447 macro_start = nasm_malloc(sizeof(*macro_start));
3448 macro_start->next = NULL;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003449 macro_start->text = nasm_quote(p, strlen(p));
3450 macro_start->type = TOK_STRING;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003451 macro_start->a.mac = NULL;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003452 if (xsl)
3453 nasm_free(xsl);
H. Peter Anvin418ca702008-05-30 10:42:30 -07003454
3455 /*
3456 * We now have a macro name, an implicit parameter count of
3457 * zero, and a string token to use as an expansion. Create
3458 * and store an SMacro.
3459 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003460 define_smacro(ctx, mname, casesense, 0, macro_start);
3461 free_tlist(tline);
H. Peter Anvin418ca702008-05-30 10:42:30 -07003462 free_tlist(origline);
3463 return DIRECTIVE_FOUND;
3464 }
3465
H. Peter Anvine2c80182005-01-15 22:15:51 +00003466 case PP_STRLEN:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003467 if (defining != NULL) return NO_DIRECTIVE_FOUND;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003468 casesense = true;
H. Peter Anvin70653092007-10-19 14:42:29 -07003469
H. Peter Anvine2c80182005-01-15 22:15:51 +00003470 tline = tline->next;
3471 skip_white_(tline);
3472 tline = expand_id(tline);
3473 if (!tline || (tline->type != TOK_ID &&
3474 (tline->type != TOK_PREPROC_ID ||
3475 tline->text[1] != '$'))) {
3476 error(ERR_NONFATAL,
3477 "`%%strlen' expects a macro identifier as first parameter");
3478 free_tlist(origline);
3479 return DIRECTIVE_FOUND;
3480 }
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08003481 ctx = get_ctx(tline->text, &mname, false);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003482 last = tline;
3483 tline = expand_smacro(tline->next);
3484 last->next = NULL;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003485
H. Peter Anvine2c80182005-01-15 22:15:51 +00003486 t = tline;
3487 while (tok_type_(t, TOK_WHITESPACE))
3488 t = t->next;
3489 /* t should now point to the string */
Cyrill Gorcunov4e1d5ab2010-07-23 18:51:51 +04003490 if (!tok_type_(t, TOK_STRING)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00003491 error(ERR_NONFATAL,
3492 "`%%strlen` requires string as second parameter");
3493 free_tlist(tline);
3494 free_tlist(origline);
3495 return DIRECTIVE_FOUND;
3496 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003497
H. Peter Anvine2c80182005-01-15 22:15:51 +00003498 macro_start = nasm_malloc(sizeof(*macro_start));
3499 macro_start->next = NULL;
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07003500 make_tok_num(macro_start, nasm_unquote(t->text, NULL));
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003501 macro_start->a.mac = NULL;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003502
H. Peter Anvine2c80182005-01-15 22:15:51 +00003503 /*
3504 * We now have a macro name, an implicit parameter count of
3505 * zero, and a numeric token to use as an expansion. Create
3506 * and store an SMacro.
3507 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003508 define_smacro(ctx, mname, casesense, 0, macro_start);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003509 free_tlist(tline);
3510 free_tlist(origline);
3511 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003512
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003513 case PP_STRCAT:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003514 if (defining != NULL) return NO_DIRECTIVE_FOUND;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003515 casesense = true;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003516
3517 tline = tline->next;
3518 skip_white_(tline);
3519 tline = expand_id(tline);
3520 if (!tline || (tline->type != TOK_ID &&
3521 (tline->type != TOK_PREPROC_ID ||
3522 tline->text[1] != '$'))) {
3523 error(ERR_NONFATAL,
3524 "`%%strcat' expects a macro identifier as first parameter");
3525 free_tlist(origline);
3526 return DIRECTIVE_FOUND;
3527 }
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08003528 ctx = get_ctx(tline->text, &mname, false);
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003529 last = tline;
3530 tline = expand_smacro(tline->next);
3531 last->next = NULL;
3532
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003533 len = 0;
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04003534 list_for_each(t, tline) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003535 switch (t->type) {
3536 case TOK_WHITESPACE:
3537 break;
3538 case TOK_STRING:
3539 len += t->a.len = nasm_unquote(t->text, NULL);
3540 break;
3541 case TOK_OTHER:
3542 if (!strcmp(t->text, ",")) /* permit comma separators */
3543 break;
3544 /* else fall through */
3545 default:
3546 error(ERR_NONFATAL,
3547 "non-string passed to `%%strcat' (%d)", t->type);
3548 free_tlist(tline);
3549 free_tlist(origline);
3550 return DIRECTIVE_FOUND;
3551 }
3552 }
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003553
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003554 p = pp = nasm_malloc(len);
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04003555 list_for_each(t, tline) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003556 if (t->type == TOK_STRING) {
3557 memcpy(p, t->text, t->a.len);
3558 p += t->a.len;
3559 }
3560 }
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003561
3562 /*
3563 * We now have a macro name, an implicit parameter count of
3564 * zero, and a numeric token to use as an expansion. Create
3565 * and store an SMacro.
3566 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003567 macro_start = new_Token(NULL, TOK_STRING, NULL, 0);
3568 macro_start->text = nasm_quote(pp, len);
3569 nasm_free(pp);
3570 define_smacro(ctx, mname, casesense, 0, macro_start);
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003571 free_tlist(tline);
3572 free_tlist(origline);
3573 return DIRECTIVE_FOUND;
3574
H. Peter Anvine2c80182005-01-15 22:15:51 +00003575 case PP_SUBSTR:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003576 if (defining != NULL) return NO_DIRECTIVE_FOUND;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003577 {
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003578 int64_t start, count;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003579 size_t len;
H. Peter Anvind2456592008-06-19 15:04:18 -07003580
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003581 casesense = true;
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07003582
H. Peter Anvine2c80182005-01-15 22:15:51 +00003583 tline = tline->next;
3584 skip_white_(tline);
3585 tline = expand_id(tline);
3586 if (!tline || (tline->type != TOK_ID &&
3587 (tline->type != TOK_PREPROC_ID ||
3588 tline->text[1] != '$'))) {
3589 error(ERR_NONFATAL,
3590 "`%%substr' expects a macro identifier as first parameter");
3591 free_tlist(origline);
3592 return DIRECTIVE_FOUND;
3593 }
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08003594 ctx = get_ctx(tline->text, &mname, false);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003595 last = tline;
3596 tline = expand_smacro(tline->next);
3597 last->next = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003598
Cyrill Gorcunov35519d62010-09-06 23:49:52 +04003599 if (tline) /* skip expanded id */
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003600 t = tline->next;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003601 while (tok_type_(t, TOK_WHITESPACE))
3602 t = t->next;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003603
H. Peter Anvine2c80182005-01-15 22:15:51 +00003604 /* t should now point to the string */
Cyrill Gorcunov35519d62010-09-06 23:49:52 +04003605 if (!tok_type_(t, TOK_STRING)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00003606 error(ERR_NONFATAL,
3607 "`%%substr` requires string as second parameter");
3608 free_tlist(tline);
3609 free_tlist(origline);
3610 return DIRECTIVE_FOUND;
3611 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003612
H. Peter Anvine2c80182005-01-15 22:15:51 +00003613 tt = t->next;
3614 tptr = &tt;
3615 tokval.t_type = TOKEN_INVALID;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003616 evalresult = evaluate(ppscan, tptr, &tokval, NULL,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003617 pass, error, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003618 if (!evalresult) {
3619 free_tlist(tline);
3620 free_tlist(origline);
3621 return DIRECTIVE_FOUND;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003622 } else if (!is_simple(evalresult)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00003623 error(ERR_NONFATAL, "non-constant value given to `%%substr`");
3624 free_tlist(tline);
3625 free_tlist(origline);
3626 return DIRECTIVE_FOUND;
3627 }
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003628 start = evalresult->value - 1;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003629
3630 while (tok_type_(tt, TOK_WHITESPACE))
3631 tt = tt->next;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003632 if (!tt) {
Keith Kaniosb307a4f2010-11-06 17:41:51 -05003633 count = 1; /* Backwards compatibility: one character */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003634 } else {
3635 tokval.t_type = TOKEN_INVALID;
3636 evalresult = evaluate(ppscan, tptr, &tokval, NULL,
3637 pass, error, NULL);
3638 if (!evalresult) {
3639 free_tlist(tline);
3640 free_tlist(origline);
3641 return DIRECTIVE_FOUND;
3642 } else if (!is_simple(evalresult)) {
3643 error(ERR_NONFATAL, "non-constant value given to `%%substr`");
3644 free_tlist(tline);
3645 free_tlist(origline);
3646 return DIRECTIVE_FOUND;
3647 }
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003648 count = evalresult->value;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003649 }
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003650
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003651 len = nasm_unquote(t->text, NULL);
Cyrill Gorcunovcff031e2010-09-07 20:31:11 +04003652 /* make start and count being in range */
3653 if (start < 0)
3654 start = 0;
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003655 if (count < 0)
3656 count = len + count + 1 - start;
3657 if (start + count > (int64_t)len)
Cyrill Gorcunovcff031e2010-09-07 20:31:11 +04003658 count = len - start;
3659 if (!len || count < 0 || start >=(int64_t)len)
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003660 start = -1, count = 0; /* empty string */
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003661
H. Peter Anvine2c80182005-01-15 22:15:51 +00003662 macro_start = nasm_malloc(sizeof(*macro_start));
3663 macro_start->next = NULL;
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003664 macro_start->text = nasm_quote((start < 0) ? "" : t->text + start, count);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003665 macro_start->type = TOK_STRING;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003666 macro_start->a.mac = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003667
H. Peter Anvine2c80182005-01-15 22:15:51 +00003668 /*
3669 * We now have a macro name, an implicit parameter count of
3670 * zero, and a numeric token to use as an expansion. Create
3671 * and store an SMacro.
3672 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003673 define_smacro(ctx, mname, casesense, 0, macro_start);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003674 free_tlist(tline);
3675 free_tlist(origline);
3676 return DIRECTIVE_FOUND;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003677 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003678
H. Peter Anvine2c80182005-01-15 22:15:51 +00003679 case PP_ASSIGN:
3680 case PP_IASSIGN:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003681 if (defining != NULL) return NO_DIRECTIVE_FOUND;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003682 casesense = (i == PP_ASSIGN);
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07003683
H. Peter Anvine2c80182005-01-15 22:15:51 +00003684 tline = tline->next;
3685 skip_white_(tline);
3686 tline = expand_id(tline);
3687 if (!tline || (tline->type != TOK_ID &&
3688 (tline->type != TOK_PREPROC_ID ||
3689 tline->text[1] != '$'))) {
3690 error(ERR_NONFATAL,
3691 "`%%%sassign' expects a macro identifier",
3692 (i == PP_IASSIGN ? "i" : ""));
3693 free_tlist(origline);
3694 return DIRECTIVE_FOUND;
3695 }
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08003696 ctx = get_ctx(tline->text, &mname, false);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003697 last = tline;
3698 tline = expand_smacro(tline->next);
3699 last->next = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003700
H. Peter Anvine2c80182005-01-15 22:15:51 +00003701 t = tline;
3702 tptr = &t;
3703 tokval.t_type = TOKEN_INVALID;
3704 evalresult =
3705 evaluate(ppscan, tptr, &tokval, NULL, pass, error, NULL);
3706 free_tlist(tline);
3707 if (!evalresult) {
3708 free_tlist(origline);
3709 return DIRECTIVE_FOUND;
3710 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003711
H. Peter Anvine2c80182005-01-15 22:15:51 +00003712 if (tokval.t_type)
H. Peter Anvin917a3492008-09-24 09:14:49 -07003713 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +00003714 "trailing garbage after expression ignored");
H. Peter Anvin734b1882002-04-30 21:01:08 +00003715
H. Peter Anvine2c80182005-01-15 22:15:51 +00003716 if (!is_simple(evalresult)) {
3717 error(ERR_NONFATAL,
3718 "non-constant value given to `%%%sassign'",
3719 (i == PP_IASSIGN ? "i" : ""));
3720 free_tlist(origline);
3721 return DIRECTIVE_FOUND;
3722 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003723
H. Peter Anvine2c80182005-01-15 22:15:51 +00003724 macro_start = nasm_malloc(sizeof(*macro_start));
3725 macro_start->next = NULL;
3726 make_tok_num(macro_start, reloc_value(evalresult));
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003727 macro_start->a.mac = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003728
H. Peter Anvine2c80182005-01-15 22:15:51 +00003729 /*
3730 * We now have a macro name, an implicit parameter count of
3731 * zero, and a numeric token to use as an expansion. Create
3732 * and store an SMacro.
3733 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003734 define_smacro(ctx, mname, casesense, 0, macro_start);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003735 free_tlist(origline);
3736 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003737
H. Peter Anvine2c80182005-01-15 22:15:51 +00003738 case PP_LINE:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003739 if (defining != NULL) return NO_DIRECTIVE_FOUND;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003740 /*
3741 * Syntax is `%line nnn[+mmm] [filename]'
3742 */
3743 tline = tline->next;
3744 skip_white_(tline);
3745 if (!tok_type_(tline, TOK_NUMBER)) {
3746 error(ERR_NONFATAL, "`%%line' expects line number");
3747 free_tlist(origline);
3748 return DIRECTIVE_FOUND;
3749 }
H. Peter Anvin70055962007-10-11 00:05:31 -07003750 k = readnum(tline->text, &err);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003751 m = 1;
3752 tline = tline->next;
3753 if (tok_is_(tline, "+")) {
3754 tline = tline->next;
3755 if (!tok_type_(tline, TOK_NUMBER)) {
3756 error(ERR_NONFATAL, "`%%line' expects line increment");
3757 free_tlist(origline);
3758 return DIRECTIVE_FOUND;
3759 }
H. Peter Anvin70055962007-10-11 00:05:31 -07003760 m = readnum(tline->text, &err);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003761 tline = tline->next;
3762 }
3763 skip_white_(tline);
3764 src_set_linnum(k);
3765 istk->lineinc = m;
3766 if (tline) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07003767 nasm_free(src_set_fname(detoken(tline, false)));
H. Peter Anvine2c80182005-01-15 22:15:51 +00003768 }
3769 free_tlist(origline);
3770 return DIRECTIVE_FOUND;
Keith Kaniosb307a4f2010-11-06 17:41:51 -05003771
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003772 case PP_WHILE:
3773 if (defining != NULL) {
3774 if (defining->type == EXP_WHILE) {
3775 defining->def_depth ++;
3776 }
3777 return NO_DIRECTIVE_FOUND;
3778 }
3779 l = NULL;
3780 if ((istk->expansion != NULL) &&
3781 (istk->expansion->emitting == false)) {
3782 j = COND_NEVER;
3783 } else {
3784 l = new_Line();
3785 l->first = copy_Token(tline->next);
3786 j = if_condition(tline->next, i);
3787 tline->next = NULL; /* it got freed */
3788 j = (((j < 0) ? COND_NEVER : j) ? COND_IF_TRUE : COND_IF_FALSE);
3789 }
3790 ed = new_ExpDef(EXP_WHILE);
3791 ed->state = j;
3792 ed->cur_depth = 1;
3793 ed->max_depth = DEADMAN_LIMIT;
3794 ed->ignoring = ((ed->state == COND_IF_TRUE) ? false : true);
3795 if (ed->ignoring == false) {
3796 ed->line = l;
3797 ed->last = l;
3798 } else if (l != NULL) {
3799 delete_Token(l->first);
3800 nasm_free(l);
3801 l = NULL;
3802 }
3803 ed->prev = defining;
3804 defining = ed;
3805 free_tlist(origline);
3806 return DIRECTIVE_FOUND;
3807
3808 case PP_ENDWHILE:
3809 if (defining != NULL) {
3810 if (defining->type == EXP_WHILE) {
3811 if (defining->def_depth > 0) {
3812 defining->def_depth --;
3813 return NO_DIRECTIVE_FOUND;
3814 }
3815 } else {
3816 return NO_DIRECTIVE_FOUND;
3817 }
3818 }
3819 if (tline->next != NULL) {
3820 error_precond(ERR_WARNING|ERR_PASS1,
3821 "trailing garbage after `%%endwhile' ignored");
3822 }
3823 if ((defining == NULL) || (defining->type != EXP_WHILE)) {
3824 error(ERR_NONFATAL, "`%%endwhile': no matching `%%while'");
3825 return DIRECTIVE_FOUND;
3826 }
3827 ed = defining;
3828 defining = ed->prev;
3829 if (ed->ignoring == false) {
3830 ed->prev = expansions;
3831 expansions = ed;
3832 ei = new_ExpInv(EXP_WHILE, ed);
3833 ei->current = ed->line->next;
3834 ei->emitting = true;
3835 ei->prev = istk->expansion;
3836 istk->expansion = ei;
3837 } else {
3838 nasm_free(ed);
3839 }
3840 free_tlist(origline);
3841 return DIRECTIVE_FOUND;
3842
3843 case PP_EXITWHILE:
3844 if (defining != NULL) return NO_DIRECTIVE_FOUND;
3845 /*
3846 * We must search along istk->expansion until we hit a
3847 * while invocation. Then we disable the emitting state(s)
3848 * between exitwhile and endwhile.
3849 */
3850 for (ei = istk->expansion; ei != NULL; ei = ei->prev) {
3851 if (ei->type == EXP_WHILE) {
3852 break;
3853 }
3854 }
3855
3856 if (ei != NULL) {
3857 /*
3858 * Set all invocations leading back to the while
3859 * invocation to a non-emitting state.
3860 */
3861 for (eei = istk->expansion; eei != ei; eei = eei->prev) {
3862 eei->emitting = false;
3863 }
3864 eei->emitting = false;
3865 eei->current = NULL;
3866 eei->def->cur_depth = eei->def->max_depth;
3867 } else {
3868 error(ERR_NONFATAL, "`%%exitwhile' not within `%%while' block");
3869 }
3870 free_tlist(origline);
3871 return DIRECTIVE_FOUND;
3872
3873 case PP_COMMENT:
3874 if (defining != NULL) {
3875 if (defining->type == EXP_COMMENT) {
3876 defining->def_depth ++;
3877 }
3878 return NO_DIRECTIVE_FOUND;
3879 }
3880 ed = new_ExpDef(EXP_COMMENT);
3881 ed->ignoring = true;
3882 ed->prev = defining;
3883 defining = ed;
3884 free_tlist(origline);
3885 return DIRECTIVE_FOUND;
3886
3887 case PP_ENDCOMMENT:
3888 if (defining != NULL) {
3889 if (defining->type == EXP_COMMENT) {
3890 if (defining->def_depth > 0) {
3891 defining->def_depth --;
3892 return NO_DIRECTIVE_FOUND;
3893 }
3894 } else {
3895 return NO_DIRECTIVE_FOUND;
3896 }
3897 }
3898 if ((defining == NULL) || (defining->type != EXP_COMMENT)) {
3899 error(ERR_NONFATAL, "`%%endcomment': no matching `%%comment'");
3900 return DIRECTIVE_FOUND;
3901 }
3902 ed = defining;
3903 defining = ed->prev;
3904 nasm_free(ed);
3905 free_tlist(origline);
3906 return DIRECTIVE_FOUND;
3907
3908 case PP_FINAL:
3909 if (defining != NULL) return NO_DIRECTIVE_FOUND;
3910 if (in_final != false) {
3911 error(ERR_FATAL, "`%%final' cannot be used recursively");
3912 }
3913 tline = tline->next;
3914 skip_white_(tline);
3915 if (tline == NULL) {
3916 error(ERR_NONFATAL, "`%%final' expects at least one parameter");
3917 } else {
3918 l = new_Line();
3919 l->first = copy_Token(tline);
3920 l->next = finals;
3921 finals = l;
3922 }
3923 free_tlist(origline);
3924 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003925
H. Peter Anvine2c80182005-01-15 22:15:51 +00003926 default:
3927 error(ERR_FATAL,
3928 "preprocessor directive `%s' not yet implemented",
H. Peter Anvin4169a472007-09-12 01:29:43 +00003929 pp_directives[i]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003930 return DIRECTIVE_FOUND;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003931 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003932}
3933
3934/*
H. Peter Anvin76690a12002-04-30 20:52:49 +00003935 * Ensure that a macro parameter contains a condition code and
3936 * nothing else. Return the condition code index if so, or -1
3937 * otherwise.
3938 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003939static int find_cc(Token * t)
H. Peter Anvineba20a72002-04-30 20:53:55 +00003940{
H. Peter Anvin76690a12002-04-30 20:52:49 +00003941 Token *tt;
3942 int i, j, k, m;
3943
H. Peter Anvin25a99342007-09-22 17:45:45 -07003944 if (!t)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003945 return -1; /* Probably a %+ without a space */
H. Peter Anvin25a99342007-09-22 17:45:45 -07003946
H. Peter Anvineba20a72002-04-30 20:53:55 +00003947 skip_white_(t);
H. Peter Anvin76690a12002-04-30 20:52:49 +00003948 if (t->type != TOK_ID)
H. Peter Anvine2c80182005-01-15 22:15:51 +00003949 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003950 tt = t->next;
H. Peter Anvineba20a72002-04-30 20:53:55 +00003951 skip_white_(tt);
H. Peter Anvin76690a12002-04-30 20:52:49 +00003952 if (tt && (tt->type != TOK_OTHER || strcmp(tt->text, ",")))
H. Peter Anvine2c80182005-01-15 22:15:51 +00003953 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003954
3955 i = -1;
Cyrill Gorcunova7319242010-06-03 22:04:36 +04003956 j = ARRAY_SIZE(conditions);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003957 while (j - i > 1) {
3958 k = (j + i) / 2;
3959 m = nasm_stricmp(t->text, conditions[k]);
3960 if (m == 0) {
3961 i = k;
3962 j = -2;
3963 break;
3964 } else if (m < 0) {
3965 j = k;
3966 } else
3967 i = k;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003968 }
3969 if (j != -2)
H. Peter Anvine2c80182005-01-15 22:15:51 +00003970 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003971 return i;
3972}
3973
Cyrill Gorcunov575d4282010-10-06 00:25:55 +04003974static bool paste_tokens(Token **head, const struct tokseq_match *m,
3975 int mnum, bool handle_paste_tokens)
H. Peter Anvind784a082009-04-20 14:01:18 -07003976{
3977 Token **tail, *t, *tt;
3978 Token **paste_head;
3979 bool did_paste = false;
3980 char *tmp;
Cyrill Gorcunov575d4282010-10-06 00:25:55 +04003981 int i;
H. Peter Anvind784a082009-04-20 14:01:18 -07003982
3983 /* Now handle token pasting... */
3984 paste_head = NULL;
3985 tail = head;
3986 while ((t = *tail) && (tt = t->next)) {
3987 switch (t->type) {
3988 case TOK_WHITESPACE:
3989 if (tt->type == TOK_WHITESPACE) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003990 /* Zap adjacent whitespace tokens */
H. Peter Anvind784a082009-04-20 14:01:18 -07003991 t->next = delete_Token(tt);
3992 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003993 /* Do not advance paste_head here */
3994 tail = &t->next;
3995 }
H. Peter Anvind784a082009-04-20 14:01:18 -07003996 break;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003997 case TOK_PASTE: /* %+ */
3998 if (handle_paste_tokens) {
3999 /* Zap %+ and whitespace tokens to the right */
4000 while (t && (t->type == TOK_WHITESPACE ||
4001 t->type == TOK_PASTE))
4002 t = *tail = delete_Token(t);
4003 if (!paste_head || !t)
4004 break; /* Nothing to paste with */
4005 tail = paste_head;
4006 t = *tail;
4007 tt = t->next;
4008 while (tok_type_(tt, TOK_WHITESPACE))
4009 tt = t->next = delete_Token(tt);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004010 if (tt) {
4011 tmp = nasm_strcat(t->text, tt->text);
4012 delete_Token(t);
4013 tt = delete_Token(tt);
4014 t = *tail = tokenize(tmp);
4015 nasm_free(tmp);
4016 while (t->next) {
4017 tail = &t->next;
4018 t = t->next;
4019 }
4020 t->next = tt; /* Attach the remaining token chain */
4021 did_paste = true;
4022 }
4023 paste_head = tail;
4024 tail = &t->next;
4025 break;
4026 }
4027 /* else fall through */
4028 default:
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +04004029 /*
4030 * Concatenation of tokens might look nontrivial
4031 * but in real it's pretty simple -- the caller
Cyrill Gorcunov575d4282010-10-06 00:25:55 +04004032 * prepares the masks of token types to be concatenated
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +04004033 * and we simply find matched sequences and slip
4034 * them together
4035 */
Cyrill Gorcunov575d4282010-10-06 00:25:55 +04004036 for (i = 0; i < mnum; i++) {
4037 if (PP_CONCAT_MASK(t->type) & m[i].mask_head) {
4038 size_t len = 0;
4039 char *tmp, *p;
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +04004040
Cyrill Gorcunov575d4282010-10-06 00:25:55 +04004041 while (tt && (PP_CONCAT_MASK(tt->type) & m[i].mask_tail)) {
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004042 len += strlen(tt->text);
4043 tt = tt->next;
Cyrill Gorcunov575d4282010-10-06 00:25:55 +04004044 }
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +04004045
Cyrill Gorcunov575d4282010-10-06 00:25:55 +04004046 /*
4047 * Now tt points to the first token after
4048 * the potential paste area...
4049 */
4050 if (tt != t->next) {
4051 /* We have at least two tokens... */
4052 len += strlen(t->text);
4053 p = tmp = nasm_malloc(len+1);
4054 while (t != tt) {
4055 strcpy(p, t->text);
4056 p = strchr(p, '\0');
4057 t = delete_Token(t);
4058 }
4059 t = *tail = tokenize(tmp);
4060 nasm_free(tmp);
4061 while (t->next) {
4062 tail = &t->next;
4063 t = t->next;
4064 }
4065 t->next = tt; /* Attach the remaining token chain */
4066 did_paste = true;
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +04004067 }
Cyrill Gorcunov575d4282010-10-06 00:25:55 +04004068 paste_head = tail;
4069 tail = &t->next;
4070 break;
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +04004071 }
Cyrill Gorcunov575d4282010-10-06 00:25:55 +04004072 }
4073 if (i >= mnum) { /* no match */
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +04004074 tail = &t->next;
4075 if (!tok_type_(t->next, TOK_WHITESPACE))
4076 paste_head = tail;
4077 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004078 break;
H. Peter Anvind784a082009-04-20 14:01:18 -07004079 }
4080 }
4081 return did_paste;
4082}
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004083
4084/*
4085 * expands to a list of tokens from %{x:y}
4086 */
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004087static Token *expand_mmac_params_range(ExpInv *ei, Token *tline, Token ***last)
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004088{
4089 Token *t = tline, **tt, *tm, *head;
4090 char *pos;
4091 int fst, lst, j, i;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004092
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004093 pos = strchr(tline->text, ':');
4094 nasm_assert(pos);
4095
4096 lst = atoi(pos + 1);
4097 fst = atoi(tline->text + 1);
4098
4099 /*
4100 * only macros params are accounted so
4101 * if someone passes %0 -- we reject such
4102 * value(s)
4103 */
4104 if (lst == 0 || fst == 0)
4105 goto err;
4106
4107 /* the values should be sane */
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004108 if ((fst > (int)ei->nparam || fst < (-(int)ei->nparam)) ||
4109 (lst > (int)ei->nparam || lst < (-(int)ei->nparam)))
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004110 goto err;
4111
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004112 fst = fst < 0 ? fst + (int)ei->nparam + 1: fst;
4113 lst = lst < 0 ? lst + (int)ei->nparam + 1: lst;
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004114
4115 /* counted from zero */
4116 fst--, lst--;
4117
4118 /*
4119 * it will be at least one token
4120 */
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004121 tm = ei->params[(fst + ei->rotate) % ei->nparam];
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004122 t = new_Token(NULL, tm->type, tm->text, 0);
4123 head = t, tt = &t->next;
4124 if (fst < lst) {
4125 for (i = fst + 1; i <= lst; i++) {
4126 t = new_Token(NULL, TOK_OTHER, ",", 0);
4127 *tt = t, tt = &t->next;
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004128 j = (i + ei->rotate) % ei->nparam;
4129 tm = ei->params[j];
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004130 t = new_Token(NULL, tm->type, tm->text, 0);
4131 *tt = t, tt = &t->next;
4132 }
4133 } else {
4134 for (i = fst - 1; i >= lst; i--) {
4135 t = new_Token(NULL, TOK_OTHER, ",", 0);
4136 *tt = t, tt = &t->next;
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004137 j = (i + ei->rotate) % ei->nparam;
4138 tm = ei->params[j];
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004139 t = new_Token(NULL, tm->type, tm->text, 0);
4140 *tt = t, tt = &t->next;
4141 }
4142 }
4143
4144 *last = tt;
4145 return head;
4146
4147err:
4148 error(ERR_NONFATAL, "`%%{%s}': macro parameters out of range",
4149 &tline->text[1]);
4150 return tline;
4151}
4152
H. Peter Anvin76690a12002-04-30 20:52:49 +00004153/*
4154 * Expand MMacro-local things: parameter references (%0, %n, %+n,
H. Peter Anvin67c63722008-10-26 23:49:00 -07004155 * %-n) and MMacro-local identifiers (%%foo) as well as
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004156 * macro indirection (%[...]) and range (%{..:..}).
H. Peter Anvin76690a12002-04-30 20:52:49 +00004157 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004158static Token *expand_mmac_params(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004159{
H. Peter Anvin734b1882002-04-30 21:01:08 +00004160 Token *t, *tt, **tail, *thead;
H. Peter Anvin6125b622009-04-08 14:02:25 -07004161 bool changed = false;
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004162 char *pos;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004163
4164 tail = &thead;
4165 thead = NULL;
4166
H. Peter Anvine2c80182005-01-15 22:15:51 +00004167 while (tline) {
4168 if (tline->type == TOK_PREPROC_ID &&
Cyrill Gorcunovca611192010-06-04 09:22:12 +04004169 (((tline->text[1] == '+' || tline->text[1] == '-') && tline->text[2]) ||
4170 (tline->text[1] >= '0' && tline->text[1] <= '9') ||
4171 tline->text[1] == '%')) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004172 char *text = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004173 int type = 0, cc; /* type = 0 to placate optimisers */
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004174 char tmpbuf[30];
H. Peter Anvin25a99342007-09-22 17:45:45 -07004175 unsigned int n;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004176 int i;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004177 ExpInv *ei;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004178
H. Peter Anvine2c80182005-01-15 22:15:51 +00004179 t = tline;
4180 tline = tline->next;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004181
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004182 for (ei = istk->expansion; ei != NULL; ei = ei->prev) {
4183 if (ei->type == EXP_MMACRO) {
4184 break;
4185 }
4186 }
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004187 if (ei == NULL) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00004188 error(ERR_NONFATAL, "`%s': not in a macro call", t->text);
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004189 } else {
4190 pos = strchr(t->text, ':');
4191 if (!pos) {
4192 switch (t->text[1]) {
4193 /*
4194 * We have to make a substitution of one of the
4195 * forms %1, %-1, %+1, %%foo, %0.
4196 */
4197 case '0':
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004198 if ((strlen(t->text) > 2) && (t->text[2] == '0')) {
4199 type = TOK_ID;
4200 text = nasm_strdup(ei->label_text);
4201 } else {
4202 type = TOK_NUMBER;
4203 snprintf(tmpbuf, sizeof(tmpbuf), "%d", ei->nparam);
4204 text = nasm_strdup(tmpbuf);
4205 }
4206 break;
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004207 case '%':
H. Peter Anvine2c80182005-01-15 22:15:51 +00004208 type = TOK_ID;
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004209 snprintf(tmpbuf, sizeof(tmpbuf), "..@%"PRIu64".",
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004210 ei->unique);
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004211 text = nasm_strcat(tmpbuf, t->text + 2);
4212 break;
4213 case '-':
4214 n = atoi(t->text + 2) - 1;
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004215 if (n >= ei->nparam)
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004216 tt = NULL;
4217 else {
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004218 if (ei->nparam > 1)
4219 n = (n + ei->rotate) % ei->nparam;
4220 tt = ei->params[n];
H. Peter Anvine2c80182005-01-15 22:15:51 +00004221 }
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004222 cc = find_cc(tt);
4223 if (cc == -1) {
4224 error(ERR_NONFATAL,
4225 "macro parameter %d is not a condition code",
4226 n + 1);
4227 text = NULL;
4228 } else {
4229 type = TOK_ID;
4230 if (inverse_ccs[cc] == -1) {
4231 error(ERR_NONFATAL,
4232 "condition code `%s' is not invertible",
4233 conditions[cc]);
4234 text = NULL;
4235 } else
4236 text = nasm_strdup(conditions[inverse_ccs[cc]]);
4237 }
4238 break;
4239 case '+':
4240 n = atoi(t->text + 2) - 1;
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004241 if (n >= ei->nparam)
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004242 tt = NULL;
4243 else {
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004244 if (ei->nparam > 1)
4245 n = (n + ei->rotate) % ei->nparam;
4246 tt = ei->params[n];
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004247 }
4248 cc = find_cc(tt);
4249 if (cc == -1) {
4250 error(ERR_NONFATAL,
4251 "macro parameter %d is not a condition code",
4252 n + 1);
4253 text = NULL;
4254 } else {
4255 type = TOK_ID;
4256 text = nasm_strdup(conditions[cc]);
4257 }
4258 break;
4259 default:
4260 n = atoi(t->text + 1) - 1;
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004261 if (n >= ei->nparam)
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004262 tt = NULL;
4263 else {
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004264 if (ei->nparam > 1)
4265 n = (n + ei->rotate) % ei->nparam;
4266 tt = ei->params[n];
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004267 }
4268 if (tt) {
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004269 for (i = 0; i < ei->paramlen[n]; i++) {
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004270 *tail = new_Token(NULL, tt->type, tt->text, 0);
4271 tail = &(*tail)->next;
4272 tt = tt->next;
4273 }
4274 }
4275 text = NULL; /* we've done it here */
4276 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004277 }
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004278 } else {
4279 /*
4280 * seems we have a parameters range here
4281 */
4282 Token *head, **last;
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004283 head = expand_mmac_params_range(ei, t, &last);
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004284 if (head != t) {
4285 *tail = head;
4286 *last = tline;
4287 tline = head;
4288 text = NULL;
4289 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00004290 }
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004291 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00004292 if (!text) {
4293 delete_Token(t);
4294 } else {
4295 *tail = t;
4296 tail = &t->next;
4297 t->type = type;
4298 nasm_free(t->text);
4299 t->text = text;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004300 t->a.mac = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004301 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004302 changed = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004303 continue;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004304 } else if (tline->type == TOK_INDIRECT) {
4305 t = tline;
4306 tline = tline->next;
4307 tt = tokenize(t->text);
4308 tt = expand_mmac_params(tt);
4309 tt = expand_smacro(tt);
4310 *tail = tt;
4311 while (tt) {
4312 tt->a.mac = NULL; /* Necessary? */
4313 tail = &tt->next;
4314 tt = tt->next;
4315 }
4316 delete_Token(t);
4317 changed = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004318 } else {
4319 t = *tail = tline;
4320 tline = tline->next;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004321 t->a.mac = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004322 tail = &t->next;
4323 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00004324 }
H. Peter Anvineba20a72002-04-30 20:53:55 +00004325 *tail = NULL;
H. Peter Anvin67c63722008-10-26 23:49:00 -07004326
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +04004327 if (changed) {
Cyrill Gorcunov575d4282010-10-06 00:25:55 +04004328 const struct tokseq_match t[] = {
4329 {
4330 PP_CONCAT_MASK(TOK_ID) |
4331 PP_CONCAT_MASK(TOK_FLOAT), /* head */
4332 PP_CONCAT_MASK(TOK_ID) |
4333 PP_CONCAT_MASK(TOK_NUMBER) |
4334 PP_CONCAT_MASK(TOK_FLOAT) |
4335 PP_CONCAT_MASK(TOK_OTHER) /* tail */
4336 },
4337 {
4338 PP_CONCAT_MASK(TOK_NUMBER), /* head */
4339 PP_CONCAT_MASK(TOK_NUMBER) /* tail */
4340 }
4341 };
4342 paste_tokens(&thead, t, ARRAY_SIZE(t), false);
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +04004343 }
H. Peter Anvin6125b622009-04-08 14:02:25 -07004344
H. Peter Anvin76690a12002-04-30 20:52:49 +00004345 return thead;
4346}
4347
4348/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004349 * Expand all single-line macro calls made in the given line.
4350 * Return the expanded version of the line. The original is deemed
4351 * to be destroyed in the process. (In reality we'll just move
4352 * Tokens from input to output a lot of the time, rather than
4353 * actually bothering to destroy and replicate.)
4354 */
H. Peter Anvincb1cf592007-11-19 12:26:50 -08004355
H. Peter Anvine2c80182005-01-15 22:15:51 +00004356static Token *expand_smacro(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004357{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004358 Token *t, *tt, *mstart, **tail, *thead;
H. Peter Anvineba20a72002-04-30 20:53:55 +00004359 SMacro *head = NULL, *m;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004360 Token **params;
4361 int *paramsize;
H. Peter Anvin25a99342007-09-22 17:45:45 -07004362 unsigned int nparam, sparam;
H. Peter Anvind784a082009-04-20 14:01:18 -07004363 int brackets;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004364 Token *org_tline = tline;
4365 Context *ctx;
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08004366 const char *mname;
H. Peter Anvin2a15e692007-11-19 13:14:59 -08004367 int deadman = DEADMAN_LIMIT;
H. Peter Anvin8287daf2009-07-07 16:00:58 -07004368 bool expanded;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004369
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004370 /*
4371 * Trick: we should avoid changing the start token pointer since it can
4372 * be contained in "next" field of other token. Because of this
4373 * we allocate a copy of first token and work with it; at the end of
4374 * routine we copy it back
4375 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004376 if (org_tline) {
Cyrill Gorcunoved4a8052010-04-09 15:40:35 +04004377 tline = new_Token(org_tline->next, org_tline->type,
4378 org_tline->text, 0);
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004379 tline->a.mac = org_tline->a.mac;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004380 nasm_free(org_tline->text);
4381 org_tline->text = NULL;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004382 }
4383
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004384 expanded = true; /* Always expand %+ at least once */
H. Peter Anvin8287daf2009-07-07 16:00:58 -07004385
H. Peter Anvincb1cf592007-11-19 12:26:50 -08004386again:
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004387 thead = NULL;
Cyrill Gorcunoved4a8052010-04-09 15:40:35 +04004388 tail = &thead;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004389
H. Peter Anvine2c80182005-01-15 22:15:51 +00004390 while (tline) { /* main token loop */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004391 if (!--deadman) {
4392 error(ERR_NONFATAL, "interminable macro recursion");
Cyrill Gorcunovbd38c8f2009-11-21 11:11:23 +03004393 goto err;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004394 }
H. Peter Anvincb1cf592007-11-19 12:26:50 -08004395
H. Peter Anvine2c80182005-01-15 22:15:51 +00004396 if ((mname = tline->text)) {
4397 /* if this token is a local macro, look in local context */
Cyrill Gorcunovc56d9ad2010-02-11 15:12:19 +03004398 if (tline->type == TOK_ID) {
4399 head = (SMacro *)hash_findix(&smacros, mname);
4400 } else if (tline->type == TOK_PREPROC_ID) {
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004401 ctx = get_ctx(mname, &mname, false);
Cyrill Gorcunovc56d9ad2010-02-11 15:12:19 +03004402 head = ctx ? (SMacro *)hash_findix(&ctx->localmac, mname) : NULL;
4403 } else
4404 head = NULL;
H. Peter Anvin072771e2008-05-22 13:17:51 -07004405
H. Peter Anvine2c80182005-01-15 22:15:51 +00004406 /*
4407 * We've hit an identifier. As in is_mmacro below, we first
4408 * check whether the identifier is a single-line macro at
4409 * all, then think about checking for parameters if
4410 * necessary.
4411 */
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04004412 list_for_each(m, head)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004413 if (!mstrcmp(m->name, mname, m->casesense))
4414 break;
4415 if (m) {
4416 mstart = tline;
4417 params = NULL;
4418 paramsize = NULL;
4419 if (m->nparam == 0) {
4420 /*
4421 * Simple case: the macro is parameterless. Discard the
4422 * one token that the macro call took, and push the
4423 * expansion back on the to-do stack.
4424 */
4425 if (!m->expansion) {
4426 if (!strcmp("__FILE__", m->name)) {
4427 int32_t num = 0;
4428 char *file = NULL;
4429 src_get(&num, &file);
4430 tline->text = nasm_quote(file, strlen(file));
4431 tline->type = TOK_STRING;
4432 nasm_free(file);
H. Peter Anvine2c80182005-01-15 22:15:51 +00004433 continue;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004434 }
4435 if (!strcmp("__LINE__", m->name)) {
4436 nasm_free(tline->text);
4437 make_tok_num(tline, src_get_linnum());
4438 continue;
4439 }
4440 if (!strcmp("__BITS__", m->name)) {
4441 nasm_free(tline->text);
4442 make_tok_num(tline, globalbits);
4443 continue;
4444 }
4445 tline = delete_Token(tline);
4446 continue;
4447 }
4448 } else {
4449 /*
4450 * Complicated case: at least one macro with this name
H. Peter Anvine2c80182005-01-15 22:15:51 +00004451 * exists and takes parameters. We must find the
4452 * parameters in the call, count them, find the SMacro
4453 * that corresponds to that form of the macro call, and
4454 * substitute for the parameters when we expand. What a
4455 * pain.
4456 */
4457 /*tline = tline->next;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004458 skip_white_(tline); */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004459 do {
4460 t = tline->next;
4461 while (tok_type_(t, TOK_SMAC_END)) {
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004462 t->a.mac->in_progress = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004463 t->text = NULL;
4464 t = tline->next = delete_Token(t);
4465 }
4466 tline = t;
4467 } while (tok_type_(tline, TOK_WHITESPACE));
4468 if (!tok_is_(tline, "(")) {
4469 /*
4470 * This macro wasn't called with parameters: ignore
4471 * the call. (Behaviour borrowed from gnu cpp.)
4472 */
4473 tline = mstart;
4474 m = NULL;
4475 } else {
4476 int paren = 0;
4477 int white = 0;
4478 brackets = 0;
4479 nparam = 0;
4480 sparam = PARAM_DELTA;
4481 params = nasm_malloc(sparam * sizeof(Token *));
4482 params[0] = tline->next;
4483 paramsize = nasm_malloc(sparam * sizeof(int));
4484 paramsize[0] = 0;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07004485 while (true) { /* parameter loop */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004486 /*
4487 * For some unusual expansions
4488 * which concatenates function call
4489 */
4490 t = tline->next;
4491 while (tok_type_(t, TOK_SMAC_END)) {
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004492 t->a.mac->in_progress = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004493 t->text = NULL;
4494 t = tline->next = delete_Token(t);
4495 }
4496 tline = t;
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00004497
H. Peter Anvine2c80182005-01-15 22:15:51 +00004498 if (!tline) {
4499 error(ERR_NONFATAL,
4500 "macro call expects terminating `)'");
4501 break;
4502 }
4503 if (tline->type == TOK_WHITESPACE
4504 && brackets <= 0) {
4505 if (paramsize[nparam])
4506 white++;
4507 else
4508 params[nparam] = tline->next;
4509 continue; /* parameter loop */
4510 }
4511 if (tline->type == TOK_OTHER
4512 && tline->text[1] == 0) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004513 char ch = tline->text[0];
H. Peter Anvine2c80182005-01-15 22:15:51 +00004514 if (ch == ',' && !paren && brackets <= 0) {
4515 if (++nparam >= sparam) {
4516 sparam += PARAM_DELTA;
4517 params = nasm_realloc(params,
Cyrill Gorcunoved4a8052010-04-09 15:40:35 +04004518 sparam * sizeof(Token *));
4519 paramsize = nasm_realloc(paramsize,
4520 sparam * sizeof(int));
H. Peter Anvine2c80182005-01-15 22:15:51 +00004521 }
4522 params[nparam] = tline->next;
4523 paramsize[nparam] = 0;
4524 white = 0;
4525 continue; /* parameter loop */
4526 }
4527 if (ch == '{' &&
4528 (brackets > 0 || (brackets == 0 &&
4529 !paramsize[nparam])))
4530 {
4531 if (!(brackets++)) {
4532 params[nparam] = tline->next;
4533 continue; /* parameter loop */
4534 }
4535 }
4536 if (ch == '}' && brackets > 0)
4537 if (--brackets == 0) {
4538 brackets = -1;
4539 continue; /* parameter loop */
4540 }
4541 if (ch == '(' && !brackets)
4542 paren++;
4543 if (ch == ')' && brackets <= 0)
4544 if (--paren < 0)
4545 break;
4546 }
4547 if (brackets < 0) {
4548 brackets = 0;
4549 error(ERR_NONFATAL, "braces do not "
4550 "enclose all of macro parameter");
4551 }
4552 paramsize[nparam] += white + 1;
4553 white = 0;
4554 } /* parameter loop */
4555 nparam++;
4556 while (m && (m->nparam != nparam ||
4557 mstrcmp(m->name, mname,
4558 m->casesense)))
4559 m = m->next;
4560 if (!m)
H. Peter Anvin917a3492008-09-24 09:14:49 -07004561 error(ERR_WARNING|ERR_PASS1|ERR_WARN_MNP,
H. Peter Anvine2c80182005-01-15 22:15:51 +00004562 "macro `%s' exists, "
4563 "but not taking %d parameters",
4564 mstart->text, nparam);
4565 }
4566 }
4567 if (m && m->in_progress)
4568 m = NULL;
4569 if (!m) { /* in progess or didn't find '(' or wrong nparam */
H. Peter Anvin70653092007-10-19 14:42:29 -07004570 /*
H. Peter Anvine2c80182005-01-15 22:15:51 +00004571 * Design question: should we handle !tline, which
4572 * indicates missing ')' here, or expand those
4573 * macros anyway, which requires the (t) test a few
H. Peter Anvin70653092007-10-19 14:42:29 -07004574 * lines down?
H. Peter Anvine2c80182005-01-15 22:15:51 +00004575 */
4576 nasm_free(params);
4577 nasm_free(paramsize);
4578 tline = mstart;
4579 } else {
4580 /*
4581 * Expand the macro: we are placed on the last token of the
4582 * call, so that we can easily split the call from the
4583 * following tokens. We also start by pushing an SMAC_END
4584 * token for the cycle removal.
4585 */
4586 t = tline;
4587 if (t) {
4588 tline = t->next;
4589 t->next = NULL;
4590 }
4591 tt = new_Token(tline, TOK_SMAC_END, NULL, 0);
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004592 tt->a.mac = m;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07004593 m->in_progress = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004594 tline = tt;
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04004595 list_for_each(t, m->expansion) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00004596 if (t->type >= TOK_SMAC_PARAM) {
4597 Token *pcopy = tline, **ptail = &pcopy;
4598 Token *ttt, *pt;
4599 int i;
H. Peter Anvin734b1882002-04-30 21:01:08 +00004600
H. Peter Anvine2c80182005-01-15 22:15:51 +00004601 ttt = params[t->type - TOK_SMAC_PARAM];
Cyrill Gorcunoved4a8052010-04-09 15:40:35 +04004602 i = paramsize[t->type - TOK_SMAC_PARAM];
4603 while (--i >= 0) {
4604 pt = *ptail = new_Token(tline, ttt->type,
4605 ttt->text, 0);
H. Peter Anvine2c80182005-01-15 22:15:51 +00004606 ptail = &pt->next;
4607 ttt = ttt->next;
4608 }
4609 tline = pcopy;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004610 } else if (t->type == TOK_PREPROC_Q) {
4611 tt = new_Token(tline, TOK_ID, mname, 0);
4612 tline = tt;
4613 } else if (t->type == TOK_PREPROC_QQ) {
4614 tt = new_Token(tline, TOK_ID, m->name, 0);
4615 tline = tt;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004616 } else {
4617 tt = new_Token(tline, t->type, t->text, 0);
4618 tline = tt;
4619 }
4620 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00004621
H. Peter Anvine2c80182005-01-15 22:15:51 +00004622 /*
4623 * Having done that, get rid of the macro call, and clean
4624 * up the parameters.
4625 */
4626 nasm_free(params);
4627 nasm_free(paramsize);
4628 free_tlist(mstart);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004629 expanded = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004630 continue; /* main token loop */
4631 }
4632 }
4633 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004634
H. Peter Anvine2c80182005-01-15 22:15:51 +00004635 if (tline->type == TOK_SMAC_END) {
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004636 tline->a.mac->in_progress = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004637 tline = delete_Token(tline);
4638 } else {
4639 t = *tail = tline;
4640 tline = tline->next;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004641 t->a.mac = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004642 t->next = NULL;
4643 tail = &t->next;
4644 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004645 }
4646
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004647 /*
4648 * Now scan the entire line and look for successive TOK_IDs that resulted
Keith Kaniosb7a89542007-04-12 02:40:54 +00004649 * after expansion (they can't be produced by tokenize()). The successive
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004650 * TOK_IDs should be concatenated.
4651 * Also we look for %+ tokens and concatenate the tokens before and after
4652 * them (without white spaces in between).
4653 */
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +04004654 if (expanded) {
Cyrill Gorcunov575d4282010-10-06 00:25:55 +04004655 const struct tokseq_match t[] = {
4656 {
4657 PP_CONCAT_MASK(TOK_ID) |
4658 PP_CONCAT_MASK(TOK_PREPROC_ID), /* head */
4659 PP_CONCAT_MASK(TOK_ID) |
4660 PP_CONCAT_MASK(TOK_PREPROC_ID) |
4661 PP_CONCAT_MASK(TOK_NUMBER) /* tail */
4662 }
4663 };
4664 if (paste_tokens(&thead, t, ARRAY_SIZE(t), true)) {
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +04004665 /*
4666 * If we concatenated something, *and* we had previously expanded
4667 * an actual macro, scan the lines again for macros...
4668 */
4669 tline = thead;
4670 expanded = false;
4671 goto again;
4672 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00004673 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004674
Cyrill Gorcunovbd38c8f2009-11-21 11:11:23 +03004675err:
H. Peter Anvine2c80182005-01-15 22:15:51 +00004676 if (org_tline) {
4677 if (thead) {
4678 *org_tline = *thead;
4679 /* since we just gave text to org_line, don't free it */
4680 thead->text = NULL;
4681 delete_Token(thead);
4682 } else {
4683 /* the expression expanded to empty line;
4684 we can't return NULL for some reasons
4685 we just set the line to a single WHITESPACE token. */
4686 memset(org_tline, 0, sizeof(*org_tline));
4687 org_tline->text = NULL;
4688 org_tline->type = TOK_WHITESPACE;
4689 }
4690 thead = org_tline;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004691 }
4692
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004693 return thead;
4694}
4695
4696/*
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004697 * Similar to expand_smacro but used exclusively with macro identifiers
4698 * right before they are fetched in. The reason is that there can be
4699 * identifiers consisting of several subparts. We consider that if there
4700 * are more than one element forming the name, user wants a expansion,
4701 * otherwise it will be left as-is. Example:
4702 *
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004703 * %define %$abc cde
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004704 *
4705 * the identifier %$abc will be left as-is so that the handler for %define
4706 * will suck it and define the corresponding value. Other case:
4707 *
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004708 * %define _%$abc cde
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004709 *
4710 * In this case user wants name to be expanded *before* %define starts
4711 * working, so we'll expand %$abc into something (if it has a value;
4712 * otherwise it will be left as-is) then concatenate all successive
4713 * PP_IDs into one.
4714 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004715static Token *expand_id(Token * tline)
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004716{
4717 Token *cur, *oldnext = NULL;
4718
H. Peter Anvin734b1882002-04-30 21:01:08 +00004719 if (!tline || !tline->next)
H. Peter Anvine2c80182005-01-15 22:15:51 +00004720 return tline;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004721
4722 cur = tline;
4723 while (cur->next &&
H. Peter Anvine2c80182005-01-15 22:15:51 +00004724 (cur->next->type == TOK_ID ||
4725 cur->next->type == TOK_PREPROC_ID
4726 || cur->next->type == TOK_NUMBER))
4727 cur = cur->next;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004728
4729 /* If identifier consists of just one token, don't expand */
4730 if (cur == tline)
H. Peter Anvine2c80182005-01-15 22:15:51 +00004731 return tline;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004732
H. Peter Anvine2c80182005-01-15 22:15:51 +00004733 if (cur) {
4734 oldnext = cur->next; /* Detach the tail past identifier */
4735 cur->next = NULL; /* so that expand_smacro stops here */
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004736 }
4737
H. Peter Anvin734b1882002-04-30 21:01:08 +00004738 tline = expand_smacro(tline);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004739
H. Peter Anvine2c80182005-01-15 22:15:51 +00004740 if (cur) {
4741 /* expand_smacro possibly changhed tline; re-scan for EOL */
4742 cur = tline;
4743 while (cur && cur->next)
4744 cur = cur->next;
4745 if (cur)
4746 cur->next = oldnext;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004747 }
4748
4749 return tline;
4750}
4751
4752/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004753 * Determine whether the given line constitutes a multi-line macro
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004754 * call, and return the ExpDef structure called if so. Doesn't have
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004755 * to check for an initial label - that's taken care of in
4756 * expand_mmacro - but must check numbers of parameters. Guaranteed
4757 * to be called with tline->type == TOK_ID, so the putative macro
4758 * name is easy to find.
4759 */
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004760static ExpDef *is_mmacro(Token * tline, Token *** params_array)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004761{
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004762 ExpDef *head, *ed;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004763 Token **params;
4764 int nparam;
4765
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004766 head = (ExpDef *) hash_findix(&expdefs, tline->text);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004767
4768 /*
4769 * Efficiency: first we see if any macro exists with the given
4770 * name. If not, we can return NULL immediately. _Then_ we
4771 * count the parameters, and then we look further along the
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004772 * list if necessary to find the proper ExpDef.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004773 */
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004774 list_for_each(ed, head)
4775 if (!mstrcmp(ed->name, tline->text, ed->casesense))
H. Peter Anvine2c80182005-01-15 22:15:51 +00004776 break;
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004777 if (!ed)
H. Peter Anvine2c80182005-01-15 22:15:51 +00004778 return NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004779
4780 /*
4781 * OK, we have a potential macro. Count and demarcate the
4782 * parameters.
4783 */
H. Peter Anvin734b1882002-04-30 21:01:08 +00004784 count_mmac_params(tline->next, &nparam, &params);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004785
4786 /*
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004787 * So we know how many parameters we've got. Find the ExpDef
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004788 * structure that handles this number.
4789 */
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004790 while (ed) {
4791 if (ed->nparam_min <= nparam
4792 && (ed->plus || nparam <= ed->nparam_max)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00004793 /*
4794 * It's right, and we can use it. Add its default
4795 * parameters to the end of our list if necessary.
4796 */
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004797 if (ed->defaults && nparam < ed->nparam_min + ed->ndefs) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00004798 params =
4799 nasm_realloc(params,
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004800 ((ed->nparam_min + ed->ndefs +
H. Peter Anvine2c80182005-01-15 22:15:51 +00004801 1) * sizeof(*params)));
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004802 while (nparam < ed->nparam_min + ed->ndefs) {
4803 params[nparam] = ed->defaults[nparam - ed->nparam_min];
H. Peter Anvine2c80182005-01-15 22:15:51 +00004804 nparam++;
4805 }
4806 }
4807 /*
4808 * If we've gone over the maximum parameter count (and
4809 * we're in Plus mode), ignore parameters beyond
4810 * nparam_max.
4811 */
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004812 if (ed->plus && nparam > ed->nparam_max)
4813 nparam = ed->nparam_max;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004814 /*
4815 * Then terminate the parameter list, and leave.
4816 */
4817 if (!params) { /* need this special case */
4818 params = nasm_malloc(sizeof(*params));
4819 nparam = 0;
4820 }
4821 params[nparam] = NULL;
4822 *params_array = params;
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004823 return ed;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004824 }
4825 /*
4826 * This one wasn't right: look for the next one with the
4827 * same name.
4828 */
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004829 list_for_each(ed, ed->next)
4830 if (!mstrcmp(ed->name, tline->text, ed->casesense))
H. Peter Anvine2c80182005-01-15 22:15:51 +00004831 break;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004832 }
4833
4834 /*
4835 * After all that, we didn't find one with the right number of
4836 * parameters. Issue a warning, and fail to expand the macro.
4837 */
H. Peter Anvin917a3492008-09-24 09:14:49 -07004838 error(ERR_WARNING|ERR_PASS1|ERR_WARN_MNP,
H. Peter Anvine2c80182005-01-15 22:15:51 +00004839 "macro `%s' exists, but not taking %d parameters",
4840 tline->text, nparam);
H. Peter Anvin734b1882002-04-30 21:01:08 +00004841 nasm_free(params);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004842 return NULL;
4843}
4844
4845/*
4846 * Expand the multi-line macro call made by the given line, if
4847 * there is one to be expanded. If there is, push the expansion on
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004848 * istk->expansion and return true. Otherwise return false.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004849 */
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004850static bool expand_mmacro(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004851{
H. Peter Anvineba20a72002-04-30 20:53:55 +00004852 Token *label = NULL;
4853 int dont_prepend = 0;
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004854 Token **params, *t, *mtok;
4855 Line *l = NULL;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004856 ExpDef *ed;
4857 ExpInv *ei;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004858 int i, nparam, *paramlen;
H. Peter Anvinc751e862008-06-09 10:18:45 -07004859 const char *mname;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004860
4861 t = tline;
H. Peter Anvineba20a72002-04-30 20:53:55 +00004862 skip_white_(t);
H. Peter Anvince2233b2008-05-25 21:57:00 -07004863 /* if (!tok_type_(t, TOK_ID)) Lino 02/25/02 */
H. Peter Anvindce1e2f2002-04-30 21:06:37 +00004864 if (!tok_type_(t, TOK_ID) && !tok_type_(t, TOK_PREPROC_ID))
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004865 return false;
H. Peter Anvince2233b2008-05-25 21:57:00 -07004866 mtok = t;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004867 ed = is_mmacro(t, &params);
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004868 if (ed != NULL) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004869 mname = t->text;
H. Peter Anvinc751e862008-06-09 10:18:45 -07004870 } else {
H. Peter Anvine2c80182005-01-15 22:15:51 +00004871 Token *last;
4872 /*
4873 * We have an id which isn't a macro call. We'll assume
4874 * it might be a label; we'll also check to see if a
4875 * colon follows it. Then, if there's another id after
4876 * that lot, we'll check it again for macro-hood.
4877 */
4878 label = last = t;
4879 t = t->next;
4880 if (tok_type_(t, TOK_WHITESPACE))
4881 last = t, t = t->next;
4882 if (tok_is_(t, ":")) {
4883 dont_prepend = 1;
4884 last = t, t = t->next;
4885 if (tok_type_(t, TOK_WHITESPACE))
4886 last = t, t = t->next;
4887 }
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004888 if (!tok_type_(t, TOK_ID) || !(ed = is_mmacro(t, &params)))
4889 return false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004890 last->next = NULL;
Keith Kanios891775e2009-07-11 06:08:54 -05004891 mname = t->text;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004892 tline = t;
H. Peter Anvineba20a72002-04-30 20:53:55 +00004893 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004894
4895 /*
4896 * Fix up the parameters: this involves stripping leading and
4897 * trailing whitespace, then stripping braces if they are
4898 * present.
4899 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004900 for (nparam = 0; params[nparam]; nparam++) ;
H. Peter Anvin734b1882002-04-30 21:01:08 +00004901 paramlen = nparam ? nasm_malloc(nparam * sizeof(*paramlen)) : NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004902
H. Peter Anvine2c80182005-01-15 22:15:51 +00004903 for (i = 0; params[i]; i++) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07004904 int brace = false;
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004905 int comma = (!ed->plus || i < nparam - 1);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004906
H. Peter Anvine2c80182005-01-15 22:15:51 +00004907 t = params[i];
4908 skip_white_(t);
4909 if (tok_is_(t, "{"))
H. Peter Anvin6867acc2007-10-10 14:58:45 -07004910 t = t->next, brace = true, comma = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004911 params[i] = t;
4912 paramlen[i] = 0;
4913 while (t) {
4914 if (comma && t->type == TOK_OTHER && !strcmp(t->text, ","))
4915 break; /* ... because we have hit a comma */
4916 if (comma && t->type == TOK_WHITESPACE
4917 && tok_is_(t->next, ","))
4918 break; /* ... or a space then a comma */
4919 if (brace && t->type == TOK_OTHER && !strcmp(t->text, "}"))
4920 break; /* ... or a brace */
4921 t = t->next;
4922 paramlen[i]++;
4923 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004924 }
4925
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004926 if (ed->cur_depth >= ed->max_depth) {
4927 if (ed->max_depth > 1) {
4928 error(ERR_WARNING,
4929 "reached maximum macro recursion depth of %i for %s",
4930 ed->max_depth,ed->name);
4931 }
4932 return false;
4933 } else {
4934 ed->cur_depth ++;
4935 }
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004936
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004937 /*
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004938 * OK, we have found a ExpDef structure representing a
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004939 * previously defined mmacro. Create an expansion invocation
4940 * and point it back to the expansion definition. Substitution of
H. Peter Anvin76690a12002-04-30 20:52:49 +00004941 * parameter tokens and macro-local tokens doesn't get done
4942 * until the single-line macro substitution process; this is
4943 * because delaying them allows us to change the semantics
4944 * later through %rotate.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004945 */
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004946 ei = new_ExpInv(EXP_MMACRO, ed);
4947 ei->name = nasm_strdup(mname);
4948 //ei->label = label;
4949 //ei->label_text = detoken(label, false);
4950 ei->current = ed->line;
4951 ei->emitting = true;
4952 //ei->iline = tline;
4953 ei->params = params;
4954 ei->nparam = nparam;
4955 ei->rotate = 0;
4956 ei->paramlen = paramlen;
4957 ei->lineno = 0;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004958
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004959 ei->prev = istk->expansion;
4960 istk->expansion = ei;
4961
4962 /*
4963 * Special case: detect %00 on first invocation; if found,
4964 * avoid emitting any labels that precede the mmacro call.
4965 * ed->prepend is set to -1 when %00 is detected, else 1.
4966 */
4967 if (ed->prepend == 0) {
4968 for (l = ed->line; l != NULL; l = l->next) {
4969 for (t = l->first; t != NULL; t = t->next) {
4970 if ((t->type == TOK_PREPROC_ID) &&
4971 (strlen(t->text) == 3) &&
4972 (t->text[1] == '0') && (t->text[2] == '0')) {
4973 dont_prepend = -1;
4974 break;
4975 }
4976 }
4977 if (dont_prepend < 0) {
4978 break;
4979 }
4980 }
4981 ed->prepend = ((dont_prepend < 0) ? -1 : 1);
4982 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004983
4984 /*
H. Peter Anvineba20a72002-04-30 20:53:55 +00004985 * If we had a label, push it on as the first line of
4986 * the macro expansion.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004987 */
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004988 if (label != NULL) {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004989 if (ed->prepend < 0) {
4990 ei->label_text = detoken(label, false);
4991 } else {
4992 if (dont_prepend == 0) {
4993 t = label;
4994 while (t->next != NULL) {
4995 t = t->next;
4996 }
4997 t->next = new_Token(NULL, TOK_OTHER, ":", 0);
4998 }
4999 l = new_Line();
5000 l->first = copy_Token(label);
5001 l->next = ei->current;
5002 ei->current = l;
5003 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005004 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005005
Keith Kaniosb307a4f2010-11-06 17:41:51 -05005006 list->uplevel(ed->nolist ? LIST_MACRO_NOLIST : LIST_MACRO);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005007
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005008 istk->mmac_depth++;
Keith Kaniosb307a4f2010-11-06 17:41:51 -05005009 return true;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005010}
5011
Victor van den Elzen3b404c02008-09-18 13:51:36 +02005012/* The function that actually does the error reporting */
5013static void verror(int severity, const char *fmt, va_list arg)
5014{
5015 char buff[1024];
5016
5017 vsnprintf(buff, sizeof(buff), fmt, arg);
5018
Cyrill Gorcunov55cc4d02010-11-10 23:12:06 +03005019 if (istk && istk->mmac_depth > 0) {
5020 ExpInv *ei = istk->expansion;
5021 int lineno = ei->lineno;
5022 while (ei) {
5023 if (ei->type == EXP_MMACRO)
5024 break;
5025 lineno += ei->relno;
5026 ei = ei->prev;
5027 }
Keith Kaniosb307a4f2010-11-06 17:41:51 -05005028 nasm_error(severity, "(%s:%d) %s", ei->def->name,
Cyrill Gorcunov55cc4d02010-11-10 23:12:06 +03005029 lineno, buff);
5030 } else
H. Peter Anvindbb640b2009-07-18 18:57:16 -07005031 nasm_error(severity, "%s", buff);
Victor van den Elzen3b404c02008-09-18 13:51:36 +02005032}
5033
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005034/*
5035 * Since preprocessor always operate only on the line that didn't
H. Peter Anvin917a3492008-09-24 09:14:49 -07005036 * arrived yet, we should always use ERR_OFFBY1.
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005037 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +00005038static void error(int severity, const char *fmt, ...)
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005039{
5040 va_list arg;
H. Peter Anvin734b1882002-04-30 21:01:08 +00005041 va_start(arg, fmt);
Victor van den Elzen3b404c02008-09-18 13:51:36 +02005042 verror(severity, fmt, arg);
H. Peter Anvin734b1882002-04-30 21:01:08 +00005043 va_end(arg);
Victor van den Elzen3b404c02008-09-18 13:51:36 +02005044}
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005045
Victor van den Elzen3b404c02008-09-18 13:51:36 +02005046/*
5047 * Because %else etc are evaluated in the state context
5048 * of the previous branch, errors might get lost with error():
5049 * %if 0 ... %else trailing garbage ... %endif
5050 * So %else etc should report errors with this function.
5051 */
5052static void error_precond(int severity, const char *fmt, ...)
5053{
5054 va_list arg;
5055
5056 /* Only ignore the error if it's really in a dead branch */
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005057 if ((istk != NULL) &&
5058 (istk->expansion != NULL) &&
5059 (istk->expansion->type == EXP_IF) &&
5060 (istk->expansion->def->state == COND_NEVER))
Victor van den Elzen3b404c02008-09-18 13:51:36 +02005061 return;
5062
5063 va_start(arg, fmt);
5064 verror(severity, fmt, arg);
5065 va_end(arg);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005066}
5067
H. Peter Anvin734b1882002-04-30 21:01:08 +00005068static void
H. Peter Anvindbb640b2009-07-18 18:57:16 -07005069pp_reset(char *file, int apass, ListGen * listgen, StrList **deplist)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005070{
H. Peter Anvin7383b402008-09-24 10:20:40 -07005071 Token *t;
5072
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005073 cstk = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005074 istk = nasm_malloc(sizeof(Include));
5075 istk->next = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005076 istk->expansion = NULL;
5077 istk->fp = fopen(file, "r");
H. Peter Anvineba20a72002-04-30 20:53:55 +00005078 istk->fname = NULL;
5079 src_set_fname(nasm_strdup(file));
5080 src_set_linnum(0);
5081 istk->lineinc = 1;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005082 istk->mmac_depth = 0;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005083 if (!istk->fp)
H. Peter Anvin917a3492008-09-24 09:14:49 -07005084 error(ERR_FATAL|ERR_NOFILE, "unable to open input file `%s'",
H. Peter Anvine2c80182005-01-15 22:15:51 +00005085 file);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005086 defining = NULL;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005087 finals = NULL;
5088 in_final = false;
Charles Crayned4200be2008-07-12 16:42:33 -07005089 nested_mac_count = 0;
5090 nested_rep_count = 0;
H. Peter Anvin97a23472007-09-16 17:57:25 -07005091 init_macros();
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005092 unique = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005093 if (tasm_compatible_mode) {
H. Peter Anvina4835d42008-05-20 14:21:29 -07005094 stdmacpos = nasm_stdmac;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005095 } else {
H. Peter Anvina4835d42008-05-20 14:21:29 -07005096 stdmacpos = nasm_stdmac_after_tasm;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005097 }
H. Peter Anvind2456592008-06-19 15:04:18 -07005098 any_extrastdmac = extrastdmac && *extrastdmac;
5099 do_predef = true;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005100 list = listgen;
H. Peter Anvin61f130f2008-09-25 15:45:06 -07005101
5102 /*
5103 * 0 for dependencies, 1 for preparatory passes, 2 for final pass.
5104 * The caller, however, will also pass in 3 for preprocess-only so
5105 * we can set __PASS__ accordingly.
5106 */
5107 pass = apass > 2 ? 2 : apass;
5108
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07005109 dephead = deptail = deplist;
5110 if (deplist) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03005111 StrList *sl = nasm_malloc(strlen(file)+1+sizeof sl->next);
5112 sl->next = NULL;
5113 strcpy(sl->str, file);
5114 *deptail = sl;
5115 deptail = &sl->next;
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07005116 }
H. Peter Anvin7383b402008-09-24 10:20:40 -07005117
H. Peter Anvin61f130f2008-09-25 15:45:06 -07005118 /*
5119 * Define the __PASS__ macro. This is defined here unlike
5120 * all the other builtins, because it is special -- it varies between
5121 * passes.
5122 */
H. Peter Anvin7383b402008-09-24 10:20:40 -07005123 t = nasm_malloc(sizeof(*t));
5124 t->next = NULL;
H. Peter Anvin61f130f2008-09-25 15:45:06 -07005125 make_tok_num(t, apass);
H. Peter Anvin7383b402008-09-24 10:20:40 -07005126 t->a.mac = NULL;
5127 define_smacro(NULL, "__PASS__", true, 0, t);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005128}
5129
Keith Kaniosa6dfa782007-04-13 16:47:53 +00005130static char *pp_getline(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005131{
Keith Kaniosa6dfa782007-04-13 16:47:53 +00005132 char *line;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005133 Token *tline;
Keith Kanios9858cec2010-11-08 00:58:02 -06005134 ExpDef *ed;
5135 ExpInv *ei;
5136 Line *l;
5137 int j;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005138
H. Peter Anvine2c80182005-01-15 22:15:51 +00005139 while (1) {
5140 /*
Keith Kaniosb307a4f2010-11-06 17:41:51 -05005141 * Fetch a tokenized line, either from the expansion
H. Peter Anvine2c80182005-01-15 22:15:51 +00005142 * buffer or from the input file.
5143 */
5144 tline = NULL;
H. Peter Anvineba20a72002-04-30 20:53:55 +00005145
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005146 while (1) { /* until we get a line we can use */
5147 /*
5148 * Fetch a tokenized line from the expansion buffer
5149 */
5150 if (istk->expansion != NULL) {
5151 ei = istk->expansion;
5152 if (ei->current != NULL) {
5153 if (ei->emitting == false) {
5154 ei->current = NULL;
5155 continue;
5156 }
5157 l = ei->current;
5158 ei->current = l->next;
5159 ei->lineno++;
5160 tline = copy_Token(l->first);
5161 if (((ei->type == EXP_REP) ||
5162 (ei->type == EXP_MMACRO) ||
5163 (ei->type == EXP_WHILE))
5164 && (ei->def->nolist == false)) {
5165 char *p = detoken(tline, false);
5166 list->line(LIST_MACRO, p);
5167 nasm_free(p);
5168 }
5169 if (ei->linnum > -1) {
5170 src_set_linnum(src_get_linnum() + 1);
5171 }
5172 break;
5173 } else if ((ei->type == EXP_REP) &&
5174 (ei->def->cur_depth < ei->def->max_depth)) {
5175 ei->def->cur_depth ++;
5176 ei->current = ei->def->line;
5177 ei->lineno = 0;
5178 continue;
5179 } else if ((ei->type == EXP_WHILE) &&
5180 (ei->def->cur_depth < ei->def->max_depth)) {
5181 ei->current = ei->def->line;
5182 ei->lineno = 0;
5183 tline = copy_Token(ei->current->first);
5184 j = if_condition(tline, PP_WHILE);
5185 tline = NULL;
5186 j = (((j < 0) ? COND_NEVER : j) ? COND_IF_TRUE : COND_IF_FALSE);
5187 if (j == COND_IF_TRUE) {
5188 ei->current = ei->current->next;
5189 ei->def->cur_depth ++;
5190 } else {
5191 ei->emitting = false;
5192 ei->current = NULL;
5193 ei->def->cur_depth = ei->def->max_depth;
5194 }
5195 continue;
5196 } else {
5197 istk->expansion = ei->prev;
5198 ed = ei->def;
5199 if (ed != NULL) {
5200 if ((ei->emitting == true) &&
5201 (ed->max_depth == DEADMAN_LIMIT) &&
5202 (ed->cur_depth == DEADMAN_LIMIT)
5203 ) {
5204 error(ERR_FATAL, "runaway expansion detected, aborting");
5205 }
5206 if (ed->cur_depth > 0) {
5207 ed->cur_depth --;
5208 } else if ((ed->type != EXP_MMACRO) && (ed->type != EXP_IF)) {
5209 /***** should this really be right here??? *****/
5210 /*
5211 Line *l = NULL, *ll = NULL;
5212 for (l = ed->line; l != NULL;) {
5213 if (l->first != NULL) {
5214 free_tlist(l->first);
5215 l->first = NULL;
5216 }
5217 ll = l;
5218 l = l->next;
5219 nasm_free(ll);
5220 }
5221 expansions = ed->prev;
5222 nasm_free(ed);
5223 */
5224 }
5225 if ((ei->type == EXP_REP) ||
5226 (ei->type == EXP_MMACRO) ||
5227 (ei->type == EXP_WHILE)) {
5228 list->downlevel(LIST_MACRO);
5229 if (ei->type == EXP_MMACRO) {
5230 istk->mmac_depth--;
5231 }
5232 }
5233 }
5234 if (ei->linnum > -1) {
5235 src_set_linnum(ei->linnum);
5236 }
5237 free_expinv(ei);
5238 continue;
5239 }
5240 }
5241
5242 /*
5243 * Read in line from input and tokenize
5244 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00005245 line = read_line();
5246 if (line) { /* from the current input file */
5247 line = prepreproc(line);
Keith Kaniosb7a89542007-04-12 02:40:54 +00005248 tline = tokenize(line);
H. Peter Anvine2c80182005-01-15 22:15:51 +00005249 nasm_free(line);
5250 break;
5251 }
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005252
H. Peter Anvine2c80182005-01-15 22:15:51 +00005253 /*
5254 * The current file has ended; work down the istk
5255 */
5256 {
5257 Include *i = istk;
5258 fclose(i->fp);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005259 if (i->expansion != NULL) {
5260 error(ERR_FATAL,
5261 "end of file while still in an expansion");
5262 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00005263 /* only set line and file name if there's a next node */
5264 if (i->next) {
5265 src_set_linnum(i->lineno);
5266 nasm_free(src_set_fname(i->fname));
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03005267 }
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005268 if ((i->next == NULL) && (finals != NULL)) {
5269 in_final = true;
5270 ei = new_ExpInv(EXP_FINAL, NULL);
5271 ei->emitting = true;
5272 ei->current = finals;
5273 istk->expansion = ei;
5274 finals = NULL;
5275 continue;
5276 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00005277 istk = i->next;
5278 list->downlevel(LIST_INCLUDE);
5279 nasm_free(i);
Keith Kaniosb307a4f2010-11-06 17:41:51 -05005280 if (istk == NULL) {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005281 if (finals != NULL) {
5282 in_final = true;
5283 } else {
5284 return NULL;
5285 }
5286 }
5287 continue;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005288 }
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005289 }
5290
5291 if (defining == NULL) {
5292 tline = expand_mmac_params(tline);
5293 }
5294
H. Peter Anvine2c80182005-01-15 22:15:51 +00005295 /*
5296 * Check the line to see if it's a preprocessor directive.
5297 */
5298 if (do_directive(tline) == DIRECTIVE_FOUND) {
5299 continue;
Keith Kaniosb307a4f2010-11-06 17:41:51 -05005300 } else if (defining != NULL) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00005301 /*
Keith Kaniosb307a4f2010-11-06 17:41:51 -05005302 * We're defining an expansion. We emit nothing at all,
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005303 * and just shove the tokenized line on to the definition.
H. Peter Anvine2c80182005-01-15 22:15:51 +00005304 */
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005305 if (defining->ignoring == false) {
5306 Line *l = new_Line();
5307 l->first = tline;
5308 if (defining->line == NULL) {
5309 defining->line = l;
5310 defining->last = l;
5311 } else {
5312 defining->last->next = l;
5313 defining->last = l;
5314 }
5315 } else {
5316 //free_tlist(tline); /***** sanity check: is this supposed to be here? *****/
5317 }
5318 defining->linecount++;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005319 continue;
Keith Kaniosb307a4f2010-11-06 17:41:51 -05005320 } else if ((istk->expansion != NULL) &&
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005321 (istk->expansion->emitting != true)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00005322 /*
Keith Kaniosb307a4f2010-11-06 17:41:51 -05005323 * We're in a non-emitting branch of an expansion.
H. Peter Anvine2c80182005-01-15 22:15:51 +00005324 * Emit nothing at all, not even a blank line: when we
Keith Kaniosb307a4f2010-11-06 17:41:51 -05005325 * emerge from the expansion we'll give a line-number
H. Peter Anvine2c80182005-01-15 22:15:51 +00005326 * directive so we keep our place correctly.
5327 */
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005328 free_tlist(tline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00005329 continue;
5330 } else {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005331 tline = expand_smacro(tline);
Keith Kaniosb307a4f2010-11-06 17:41:51 -05005332 if (expand_mmacro(tline) != true) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00005333 /*
Keith Kaniosb7a89542007-04-12 02:40:54 +00005334 * De-tokenize the line again, and emit it.
H. Peter Anvine2c80182005-01-15 22:15:51 +00005335 */
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005336 line = detoken(tline, true);
5337 free_tlist(tline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00005338 break;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005339 } else {
5340 continue;
5341 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00005342 }
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005343 }
5344 return line;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005345}
5346
H. Peter Anvine2c80182005-01-15 22:15:51 +00005347static void pp_cleanup(int pass)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005348{
Keith Kaniosb307a4f2010-11-06 17:41:51 -05005349 if (defining != NULL) {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005350 error(ERR_NONFATAL, "end of file while still defining an expansion");
5351 while (defining != NULL) {
5352 ExpDef *ed = defining;
5353 defining = ed->prev;
5354 free_expdef(ed);
5355 }
5356 defining = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005357 }
Keith Kaniosb307a4f2010-11-06 17:41:51 -05005358 while (cstk != NULL)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005359 ctx_pop();
H. Peter Anvin97a23472007-09-16 17:57:25 -07005360 free_macros();
Keith Kaniosb307a4f2010-11-06 17:41:51 -05005361 while (istk != NULL) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00005362 Include *i = istk;
5363 istk = istk->next;
5364 fclose(i->fp);
5365 nasm_free(i->fname);
5366 nasm_free(i);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005367 while (i->expansion != NULL) {
5368 ExpInv *ei = i->expansion;
5369 i->expansion = ei->prev;
5370 free_expinv(ei);
5371 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005372 }
5373 while (cstk)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005374 ctx_pop();
H. Peter Anvin86877b22008-06-20 15:55:45 -07005375 nasm_free(src_set_fname(NULL));
H. Peter Anvine2c80182005-01-15 22:15:51 +00005376 if (pass == 0) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03005377 IncPath *i;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005378 free_llist(predef);
5379 delete_Blocks();
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03005380 while ((i = ipath)) {
5381 ipath = i->next;
5382 if (i->path)
5383 nasm_free(i->path);
5384 nasm_free(i);
5385 }
H. Peter Anvin11dfa1a2008-07-02 18:11:04 -07005386 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005387}
5388
Keith Kaniosa6dfa782007-04-13 16:47:53 +00005389void pp_include_path(char *path)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005390{
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005391 IncPath *i;
H. Peter Anvin37a321f2007-09-24 13:41:58 -07005392
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005393 i = nasm_malloc(sizeof(IncPath));
H. Peter Anvin37a321f2007-09-24 13:41:58 -07005394 i->path = path ? nasm_strdup(path) : NULL;
Frank Kotlerd0ed6fd2003-08-27 11:33:56 +00005395 i->next = NULL;
5396
H. Peter Anvin89cee572009-07-15 09:16:54 -04005397 if (ipath) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00005398 IncPath *j = ipath;
H. Peter Anvin89cee572009-07-15 09:16:54 -04005399 while (j->next)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005400 j = j->next;
5401 j->next = i;
5402 } else {
5403 ipath = i;
Frank Kotlerd0ed6fd2003-08-27 11:33:56 +00005404 }
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005405}
Frank Kotlerd0ed6fd2003-08-27 11:33:56 +00005406
Keith Kaniosa6dfa782007-04-13 16:47:53 +00005407void pp_pre_include(char *fname)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005408{
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005409 Token *inc, *space, *name;
5410 Line *l;
5411
H. Peter Anvin734b1882002-04-30 21:01:08 +00005412 name = new_Token(NULL, TOK_INTERNAL_STRING, fname, 0);
5413 space = new_Token(name, TOK_WHITESPACE, NULL, 0);
5414 inc = new_Token(space, TOK_PREPROC_ID, "%include", 0);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005415
Keith Kaniosb307a4f2010-11-06 17:41:51 -05005416 l = new_Line();
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005417 l->next = predef;
5418 l->first = inc;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005419 predef = l;
5420}
5421
Keith Kaniosa6dfa782007-04-13 16:47:53 +00005422void pp_pre_define(char *definition)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005423{
5424 Token *def, *space;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005425 Line *l;
Keith Kaniosa6dfa782007-04-13 16:47:53 +00005426 char *equals;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005427
5428 equals = strchr(definition, '=');
H. Peter Anvin734b1882002-04-30 21:01:08 +00005429 space = new_Token(NULL, TOK_WHITESPACE, NULL, 0);
5430 def = new_Token(space, TOK_PREPROC_ID, "%define", 0);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005431 if (equals)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005432 *equals = ' ';
Keith Kaniosb7a89542007-04-12 02:40:54 +00005433 space->next = tokenize(definition);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005434 if (equals)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005435 *equals = '=';
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005436
Keith Kaniosb307a4f2010-11-06 17:41:51 -05005437 l = new_Line();
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005438 l->next = predef;
5439 l->first = def;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005440 predef = l;
5441}
5442
Keith Kaniosa6dfa782007-04-13 16:47:53 +00005443void pp_pre_undefine(char *definition)
H. Peter Anvin620515a2002-04-30 20:57:38 +00005444{
5445 Token *def, *space;
5446 Line *l;
5447
H. Peter Anvin734b1882002-04-30 21:01:08 +00005448 space = new_Token(NULL, TOK_WHITESPACE, NULL, 0);
5449 def = new_Token(space, TOK_PREPROC_ID, "%undef", 0);
Keith Kaniosb7a89542007-04-12 02:40:54 +00005450 space->next = tokenize(definition);
H. Peter Anvin620515a2002-04-30 20:57:38 +00005451
Keith Kaniosb307a4f2010-11-06 17:41:51 -05005452 l = new_Line();
H. Peter Anvin620515a2002-04-30 20:57:38 +00005453 l->next = predef;
5454 l->first = def;
H. Peter Anvin620515a2002-04-30 20:57:38 +00005455 predef = l;
5456}
5457
Keith Kaniosb7a89542007-04-12 02:40:54 +00005458/*
Keith Kaniosb7a89542007-04-12 02:40:54 +00005459 * This function is used to assist with "runtime" preprocessor
Keith Kaniosb307a4f2010-11-06 17:41:51 -05005460 * directives, e.g. pp_runtime("%define __BITS__ 64");
Keith Kaniosb7a89542007-04-12 02:40:54 +00005461 *
5462 * ERRORS ARE IGNORED HERE, SO MAKE COMPLETELY SURE THAT YOU
5463 * PASS A VALID STRING TO THIS FUNCTION!!!!!
5464 */
5465
Keith Kaniosa6dfa782007-04-13 16:47:53 +00005466void pp_runtime(char *definition)
Keith Kaniosb7a89542007-04-12 02:40:54 +00005467{
5468 Token *def;
H. Peter Anvin70653092007-10-19 14:42:29 -07005469
Keith Kaniosb7a89542007-04-12 02:40:54 +00005470 def = tokenize(definition);
H. Peter Anvin89cee572009-07-15 09:16:54 -04005471 if (do_directive(def) == NO_DIRECTIVE_FOUND)
Keith Kaniosb7a89542007-04-12 02:40:54 +00005472 free_tlist(def);
H. Peter Anvin70653092007-10-19 14:42:29 -07005473
Keith Kaniosb7a89542007-04-12 02:40:54 +00005474}
5475
H. Peter Anvina70547f2008-07-19 21:44:26 -07005476void pp_extra_stdmac(macros_t *macros)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005477{
H. Peter Anvin76690a12002-04-30 20:52:49 +00005478 extrastdmac = macros;
5479}
5480
Keith Kaniosa5fc6462007-10-13 07:09:22 -07005481static void make_tok_num(Token * tok, int64_t val)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005482{
Keith Kaniosa6dfa782007-04-13 16:47:53 +00005483 char numbuf[20];
Keith Kaniosa5fc6462007-10-13 07:09:22 -07005484 snprintf(numbuf, sizeof(numbuf), "%"PRId64"", val);
H. Peter Anvineba20a72002-04-30 20:53:55 +00005485 tok->text = nasm_strdup(numbuf);
5486 tok->type = TOK_NUMBER;
5487}
5488
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005489Preproc nasmpp = {
5490 pp_reset,
5491 pp_getline,
5492 pp_cleanup
5493};