blob: 6448d6ae639563db9436c46b0631eda346a1d91f [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 */
1379static Line *new_Line(void)
1380{
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03001381 Line *l = nasm_malloc(sizeof(Line));
1382 l->next = NULL;
1383 l->first = NULL;
1384 return l;
Keith Kaniosb307a4f2010-11-06 17:41:51 -05001385}
1386
1387
1388/*
1389 * Initialize a new Expansion Definition
1390 */
1391static ExpDef *new_ExpDef(int exp_type)
1392{
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03001393 ExpDef *ed = (ExpDef*)nasm_malloc(sizeof(ExpDef));
1394 ed->prev = NULL;
1395 ed->next = NULL;
1396 ed->type = exp_type;
1397 ed->name = NULL;
1398 ed->nparam_min = 0;
1399 ed->nparam_max = 0;
1400 ed->casesense = true;
1401 ed->plus = false;
1402 ed->prepend = 0;
1403 ed->label = NULL;
1404 ed->line = NULL;
1405 ed->last = NULL;
1406 ed->linecount = 0;
1407 ed->dlist = NULL;
1408 ed->defaults = NULL;
1409 ed->ndefs = 0;
1410 ed->state = COND_NEVER;
1411 ed->nolist = false;
1412 ed->def_depth = 0;
1413 ed->cur_depth = 0;
1414 ed->max_depth = 0;
1415 ed->ignoring = false;
1416 return ed;
Keith Kaniosb307a4f2010-11-06 17:41:51 -05001417}
1418
1419
1420/*
1421 * Initialize a new Expansion Instance
1422 */
1423static ExpInv *new_ExpInv(int exp_type, ExpDef *ed)
1424{
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03001425 ExpInv *ei = (ExpInv*)nasm_malloc(sizeof(ExpInv));
1426 ei->prev = NULL;
1427 ei->type = exp_type;
1428 ei->def = ed;
1429 ei->name = NULL;
1430 ei->label = NULL;
1431 ei->label_text = NULL;
1432 ei->current = NULL;
1433 ei->params = NULL;
1434 ei->iline = NULL;
1435 ei->nparam = 0;
1436 ei->rotate = 0;
1437 ei->paramlen = NULL;
1438 ei->unique = ++unique;
1439 ei->emitting = false;
1440 ei->lineno = 0;
1441 if ((istk->mmac_depth < 1) &&
1442 (istk->expansion == NULL) &&
1443 (ed != NULL) &&
1444 (ed->type != EXP_MMACRO) &&
1445 (ed->type != EXP_REP) &&
1446 (ed->type != EXP_WHILE)) {
1447 ei->linnum = src_get_linnum();
1448 src_set_linnum(ei->linnum - ed->linecount - 1);
1449 } else {
1450 ei->linnum = -1;
1451 }
1452 if ((istk->expansion == NULL) ||
1453 (ei->type == EXP_MMACRO)) {
1454 ei->relno = 0;
1455 } else {
1456 ei->relno = istk->expansion->lineno;
1457 if (ed != NULL) {
1458 ei->relno -= (ed->linecount + 1);
1459 }
1460 }
1461 return ei;
Keith Kaniosb307a4f2010-11-06 17:41:51 -05001462}
1463
1464/*
H. Peter Anvin76690a12002-04-30 20:52:49 +00001465 * A scanner, suitable for use by the expression evaluator, which
1466 * operates on a line of Tokens. Expects a pointer to a pointer to
1467 * the first token in the line to be passed in as its private_data
1468 * field.
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001469 *
1470 * FIX: This really needs to be unified with stdscan.
H. Peter Anvin76690a12002-04-30 20:52:49 +00001471 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001472static int ppscan(void *private_data, struct tokenval *tokval)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001473{
H. Peter Anvin76690a12002-04-30 20:52:49 +00001474 Token **tlineptr = private_data;
1475 Token *tline;
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001476 char ourcopy[MAX_KEYWORD+1], *p, *r, *s;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001477
H. Peter Anvine2c80182005-01-15 22:15:51 +00001478 do {
1479 tline = *tlineptr;
1480 *tlineptr = tline ? tline->next : NULL;
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04001481 } while (tline && (tline->type == TOK_WHITESPACE ||
1482 tline->type == TOK_COMMENT));
H. Peter Anvin76690a12002-04-30 20:52:49 +00001483
1484 if (!tline)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001485 return tokval->t_type = TOKEN_EOS;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001486
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001487 tokval->t_charptr = tline->text;
1488
H. Peter Anvin76690a12002-04-30 20:52:49 +00001489 if (tline->text[0] == '$' && !tline->text[1])
H. Peter Anvine2c80182005-01-15 22:15:51 +00001490 return tokval->t_type = TOKEN_HERE;
H. Peter Anvin7cf897e2002-05-30 21:30:33 +00001491 if (tline->text[0] == '$' && tline->text[1] == '$' && !tline->text[2])
H. Peter Anvine2c80182005-01-15 22:15:51 +00001492 return tokval->t_type = TOKEN_BASE;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001493
H. Peter Anvine2c80182005-01-15 22:15:51 +00001494 if (tline->type == TOK_ID) {
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001495 p = tokval->t_charptr = tline->text;
1496 if (p[0] == '$') {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001497 tokval->t_charptr++;
1498 return tokval->t_type = TOKEN_ID;
1499 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00001500
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001501 for (r = p, s = ourcopy; *r; r++) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001502 if (r >= p+MAX_KEYWORD)
1503 return tokval->t_type = TOKEN_ID; /* Not a keyword */
H. Peter Anvinac8f8fc2008-06-11 15:49:41 -07001504 *s++ = nasm_tolower(*r);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001505 }
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001506 *s = '\0';
1507 /* right, so we have an identifier sitting in temp storage. now,
1508 * is it actually a register or instruction name, or what? */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001509 return nasm_token_hash(ourcopy, tokval);
H. Peter Anvin76690a12002-04-30 20:52:49 +00001510 }
1511
H. Peter Anvine2c80182005-01-15 22:15:51 +00001512 if (tline->type == TOK_NUMBER) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001513 bool rn_error;
1514 tokval->t_integer = readnum(tline->text, &rn_error);
1515 tokval->t_charptr = tline->text;
1516 if (rn_error)
1517 return tokval->t_type = TOKEN_ERRNUM;
1518 else
1519 return tokval->t_type = TOKEN_NUM;
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001520 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00001521
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001522 if (tline->type == TOK_FLOAT) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001523 return tokval->t_type = TOKEN_FLOAT;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001524 }
1525
H. Peter Anvine2c80182005-01-15 22:15:51 +00001526 if (tline->type == TOK_STRING) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001527 char bq, *ep;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001528
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001529 bq = tline->text[0];
H. Peter Anvin11627042008-06-09 20:45:19 -07001530 tokval->t_charptr = tline->text;
1531 tokval->t_inttwo = nasm_unquote(tline->text, &ep);
H. Peter Anvind2456592008-06-19 15:04:18 -07001532
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001533 if (ep[0] != bq || ep[1] != '\0')
1534 return tokval->t_type = TOKEN_ERRSTR;
1535 else
1536 return tokval->t_type = TOKEN_STR;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001537 }
1538
H. Peter Anvine2c80182005-01-15 22:15:51 +00001539 if (tline->type == TOK_OTHER) {
1540 if (!strcmp(tline->text, "<<"))
1541 return tokval->t_type = TOKEN_SHL;
1542 if (!strcmp(tline->text, ">>"))
1543 return tokval->t_type = TOKEN_SHR;
1544 if (!strcmp(tline->text, "//"))
1545 return tokval->t_type = TOKEN_SDIV;
1546 if (!strcmp(tline->text, "%%"))
1547 return tokval->t_type = TOKEN_SMOD;
1548 if (!strcmp(tline->text, "=="))
1549 return tokval->t_type = TOKEN_EQ;
1550 if (!strcmp(tline->text, "<>"))
1551 return tokval->t_type = TOKEN_NE;
1552 if (!strcmp(tline->text, "!="))
1553 return tokval->t_type = TOKEN_NE;
1554 if (!strcmp(tline->text, "<="))
1555 return tokval->t_type = TOKEN_LE;
1556 if (!strcmp(tline->text, ">="))
1557 return tokval->t_type = TOKEN_GE;
1558 if (!strcmp(tline->text, "&&"))
1559 return tokval->t_type = TOKEN_DBL_AND;
1560 if (!strcmp(tline->text, "^^"))
1561 return tokval->t_type = TOKEN_DBL_XOR;
1562 if (!strcmp(tline->text, "||"))
1563 return tokval->t_type = TOKEN_DBL_OR;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001564 }
1565
1566 /*
1567 * We have no other options: just return the first character of
1568 * the token text.
1569 */
1570 return tokval->t_type = tline->text[0];
1571}
1572
1573/*
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001574 * Compare a string to the name of an existing macro; this is a
1575 * simple wrapper which calls either strcmp or nasm_stricmp
1576 * depending on the value of the `casesense' parameter.
1577 */
H. Peter Anvin4db5a162007-10-11 13:42:09 -07001578static int mstrcmp(const char *p, const char *q, bool casesense)
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001579{
H. Peter Anvin734b1882002-04-30 21:01:08 +00001580 return casesense ? strcmp(p, q) : nasm_stricmp(p, q);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001581}
1582
1583/*
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07001584 * Compare a string to the name of an existing macro; this is a
1585 * simple wrapper which calls either strcmp or nasm_stricmp
1586 * depending on the value of the `casesense' parameter.
1587 */
1588static int mmemcmp(const char *p, const char *q, size_t l, bool casesense)
1589{
1590 return casesense ? memcmp(p, q, l) : nasm_memicmp(p, q, l);
1591}
1592
1593/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001594 * Return the Context structure associated with a %$ token. Return
1595 * NULL, having _already_ reported an error condition, if the
1596 * context stack isn't deep enough for the supplied number of $
1597 * signs.
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001598 * If all_contexts == true, contexts that enclose current are
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001599 * also scanned for such smacro, until it is found; if not -
1600 * only the context that directly results from the number of $'s
1601 * in variable's name.
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001602 *
1603 * If "namep" is non-NULL, set it to the pointer to the macro name
1604 * tail, i.e. the part beyond %$...
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001605 */
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001606static Context *get_ctx(const char *name, const char **namep,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001607 bool all_contexts)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001608{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001609 Context *ctx;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001610 SMacro *m;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001611 int i;
1612
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001613 if (namep)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001614 *namep = name;
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001615
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001616 if (!name || name[0] != '%' || name[1] != '$')
H. Peter Anvine2c80182005-01-15 22:15:51 +00001617 return NULL;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001618
H. Peter Anvine2c80182005-01-15 22:15:51 +00001619 if (!cstk) {
1620 error(ERR_NONFATAL, "`%s': context stack is empty", name);
1621 return NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001622 }
1623
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001624 name += 2;
1625 ctx = cstk;
1626 i = 0;
1627 while (ctx && *name == '$') {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001628 name++;
1629 i++;
1630 ctx = ctx->next;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001631 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001632 if (!ctx) {
1633 error(ERR_NONFATAL, "`%s': context stack is only"
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001634 " %d level%s deep", name, i, (i == 1 ? "" : "s"));
H. Peter Anvine2c80182005-01-15 22:15:51 +00001635 return NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001636 }
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001637
1638 if (namep)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001639 *namep = name;
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001640
Keith Kanios404589e2010-08-10 20:12:57 -05001641 if (!all_contexts)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001642 return ctx;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001643
Keith Kaniosb307a4f2010-11-06 17:41:51 -05001644 do {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001645 /* Search for this smacro in found context */
H. Peter Anvin166c2472008-05-28 12:28:58 -07001646 m = hash_findix(&ctx->localmac, name);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001647 while (m) {
Keith Kaniosb307a4f2010-11-06 17:41:51 -05001648 if (!mstrcmp(m->name, name, m->casesense))
H. Peter Anvine2c80182005-01-15 22:15:51 +00001649 return ctx;
1650 m = m->next;
1651 }
Keith Kaniosb307a4f2010-11-06 17:41:51 -05001652 ctx = ctx->next;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001653 }
Keith Kaniosb307a4f2010-11-06 17:41:51 -05001654 while (ctx);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001655 return NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001656}
1657
1658/*
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001659 * Check to see if a file is already in a string list
1660 */
1661static bool in_list(const StrList *list, const char *str)
1662{
1663 while (list) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001664 if (!strcmp(list->str, str))
1665 return true;
1666 list = list->next;
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001667 }
1668 return false;
1669}
1670
1671/*
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001672 * Open an include file. This routine must always return a valid
1673 * file pointer if it returns - it's responsible for throwing an
1674 * ERR_FATAL and bombing out completely if not. It should also try
1675 * the include path one by one until it finds the file or reaches
1676 * the end of the path.
1677 */
H. Peter Anvin2b1c3b92008-06-06 10:38:46 -07001678static FILE *inc_fopen(const char *file, StrList **dhead, StrList ***dtail,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001679 bool missing_ok)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001680{
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001681 FILE *fp;
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001682 char *prefix = "";
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001683 IncPath *ip = ipath;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001684 int len = strlen(file);
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001685 size_t prefix_len = 0;
1686 StrList *sl;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001687
H. Peter Anvine2c80182005-01-15 22:15:51 +00001688 while (1) {
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001689 sl = nasm_malloc(prefix_len+len+1+sizeof sl->next);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001690 memcpy(sl->str, prefix, prefix_len);
1691 memcpy(sl->str+prefix_len, file, len+1);
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001692 fp = fopen(sl->str, "r");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001693 if (fp && dhead && !in_list(*dhead, sl->str)) {
1694 sl->next = NULL;
1695 **dtail = sl;
1696 *dtail = &sl->next;
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001697 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001698 nasm_free(sl);
1699 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001700 if (fp)
1701 return fp;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001702 if (!ip) {
1703 if (!missing_ok)
1704 break;
1705 prefix = NULL;
1706 } else {
1707 prefix = ip->path;
1708 ip = ip->next;
1709 }
1710 if (prefix) {
1711 prefix_len = strlen(prefix);
1712 } else {
1713 /* -MG given and file not found */
1714 if (dhead && !in_list(*dhead, file)) {
1715 sl = nasm_malloc(len+1+sizeof sl->next);
1716 sl->next = NULL;
1717 strcpy(sl->str, file);
1718 **dtail = sl;
1719 *dtail = &sl->next;
1720 }
1721 return NULL;
1722 }
H. Peter Anvineba20a72002-04-30 20:53:55 +00001723 }
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001724
H. Peter Anvin734b1882002-04-30 21:01:08 +00001725 error(ERR_FATAL, "unable to open include file `%s'", file);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001726 return NULL;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001727}
1728
1729/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001730 * Determine if we should warn on defining a single-line macro of
H. Peter Anvinef7468f2002-04-30 20:57:59 +00001731 * name `name', with `nparam' parameters. If nparam is 0 or -1, will
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001732 * return true if _any_ single-line macro of that name is defined.
1733 * Otherwise, will return true if a single-line macro with either
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001734 * `nparam' or no parameters is defined.
1735 *
1736 * If a macro with precisely the right number of parameters is
H. Peter Anvinef7468f2002-04-30 20:57:59 +00001737 * defined, or nparam is -1, the address of the definition structure
1738 * will be returned in `defn'; otherwise NULL will be returned. If `defn'
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001739 * is NULL, no action will be taken regarding its contents, and no
1740 * error will occur.
1741 *
1742 * Note that this is also called with nparam zero to resolve
1743 * `ifdef'.
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001744 *
1745 * If you already know which context macro belongs to, you can pass
1746 * the context pointer as first parameter; if you won't but name begins
1747 * with %$ the context will be automatically computed. If all_contexts
1748 * is true, macro will be searched in outer contexts as well.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001749 */
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07001750static bool
H. Peter Anvinb2a5fda2008-06-19 21:42:42 -07001751smacro_defined(Context * ctx, const char *name, int nparam, SMacro ** defn,
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07001752 bool nocase)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001753{
H. Peter Anvin166c2472008-05-28 12:28:58 -07001754 struct hash_table *smtbl;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001755 SMacro *m;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001756
H. Peter Anvin97a23472007-09-16 17:57:25 -07001757 if (ctx) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001758 smtbl = &ctx->localmac;
H. Peter Anvin97a23472007-09-16 17:57:25 -07001759 } else if (name[0] == '%' && name[1] == '$') {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001760 if (cstk)
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001761 ctx = get_ctx(name, &name, false);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001762 if (!ctx)
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001763 return false; /* got to return _something_ */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001764 smtbl = &ctx->localmac;
H. Peter Anvin97a23472007-09-16 17:57:25 -07001765 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001766 smtbl = &smacros;
H. Peter Anvin97a23472007-09-16 17:57:25 -07001767 }
H. Peter Anvin166c2472008-05-28 12:28:58 -07001768 m = (SMacro *) hash_findix(smtbl, name);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001769
H. Peter Anvine2c80182005-01-15 22:15:51 +00001770 while (m) {
1771 if (!mstrcmp(m->name, name, m->casesense && nocase) &&
Charles Crayne192d5b52007-10-18 19:02:42 -07001772 (nparam <= 0 || m->nparam == 0 || nparam == (int) m->nparam)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001773 if (defn) {
Charles Crayne192d5b52007-10-18 19:02:42 -07001774 if (nparam == (int) m->nparam || nparam == -1)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001775 *defn = m;
1776 else
1777 *defn = NULL;
1778 }
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001779 return true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001780 }
1781 m = m->next;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001782 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001783
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001784 return false;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001785}
1786
1787/*
1788 * Count and mark off the parameters in a multi-line macro call.
1789 * This is called both from within the multi-line macro expansion
1790 * code, and also to mark off the default parameters when provided
1791 * in a %macro definition line.
1792 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001793static void count_mmac_params(Token * t, int *nparam, Token *** params)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001794{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001795 int paramsize, brace;
1796
1797 *nparam = paramsize = 0;
1798 *params = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001799 while (t) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001800 /* +1: we need space for the final NULL */
H. Peter Anvin91fb6f12008-09-01 10:56:33 -07001801 if (*nparam+1 >= paramsize) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001802 paramsize += PARAM_DELTA;
1803 *params = nasm_realloc(*params, sizeof(**params) * paramsize);
1804 }
1805 skip_white_(t);
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001806 brace = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001807 if (tok_is_(t, "{"))
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001808 brace = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001809 (*params)[(*nparam)++] = t;
1810 while (tok_isnt_(t, brace ? "}" : ","))
1811 t = t->next;
1812 if (t) { /* got a comma/brace */
1813 t = t->next;
1814 if (brace) {
1815 /*
1816 * Now we've found the closing brace, look further
1817 * for the comma.
1818 */
1819 skip_white_(t);
1820 if (tok_isnt_(t, ",")) {
1821 error(ERR_NONFATAL,
1822 "braces do not enclose all of macro parameter");
1823 while (tok_isnt_(t, ","))
1824 t = t->next;
1825 }
1826 if (t)
1827 t = t->next; /* eat the comma */
1828 }
1829 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001830 }
1831}
1832
1833/*
H. Peter Anvin76690a12002-04-30 20:52:49 +00001834 * Determine whether one of the various `if' conditions is true or
1835 * not.
1836 *
1837 * We must free the tline we get passed.
1838 */
H. Peter Anvin70055962007-10-11 00:05:31 -07001839static bool if_condition(Token * tline, enum preproc_token ct)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001840{
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001841 enum pp_conditional i = PP_COND(ct);
H. Peter Anvin70055962007-10-11 00:05:31 -07001842 bool j;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001843 Token *t, *tt, **tptr, *origline;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001844 struct tokenval tokval;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001845 expr *evalresult;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001846 enum pp_token_type needtype;
H. Peter Anvin077fb932010-07-20 14:56:30 -07001847 char *p;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001848
1849 origline = tline;
1850
H. Peter Anvine2c80182005-01-15 22:15:51 +00001851 switch (i) {
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001852 case PPC_IFCTX:
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001853 j = false; /* have we matched yet? */
Victor van den Elzen0e857f12008-07-23 13:21:29 +02001854 while (true) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001855 skip_white_(tline);
Victor van den Elzen0e857f12008-07-23 13:21:29 +02001856 if (!tline)
1857 break;
1858 if (tline->type != TOK_ID) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001859 error(ERR_NONFATAL,
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001860 "`%s' expects context identifiers", pp_directives[ct]);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001861 free_tlist(origline);
1862 return -1;
1863 }
Victor van den Elzen0e857f12008-07-23 13:21:29 +02001864 if (cstk && cstk->name && !nasm_stricmp(tline->text, cstk->name))
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001865 j = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001866 tline = tline->next;
1867 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001868 break;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001869
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001870 case PPC_IFDEF:
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001871 j = false; /* have we matched yet? */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001872 while (tline) {
1873 skip_white_(tline);
1874 if (!tline || (tline->type != TOK_ID &&
1875 (tline->type != TOK_PREPROC_ID ||
1876 tline->text[1] != '$'))) {
1877 error(ERR_NONFATAL,
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001878 "`%s' expects macro identifiers", pp_directives[ct]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001879 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001880 }
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07001881 if (smacro_defined(NULL, tline->text, 0, NULL, true))
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001882 j = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001883 tline = tline->next;
1884 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001885 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001886
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001887 case PPC_IFENV:
Cyrill Gorcunov84b4cba2010-09-12 21:39:40 +04001888 tline = expand_smacro(tline);
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001889 j = false; /* have we matched yet? */
1890 while (tline) {
1891 skip_white_(tline);
1892 if (!tline || (tline->type != TOK_ID &&
Cyrill Gorcunov84b4cba2010-09-12 21:39:40 +04001893 tline->type != TOK_STRING &&
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001894 (tline->type != TOK_PREPROC_ID ||
Cyrill Gorcunov84b4cba2010-09-12 21:39:40 +04001895 tline->text[1] != '!'))) {
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001896 error(ERR_NONFATAL,
1897 "`%s' expects environment variable names",
Cyrill Gorcunov84b4cba2010-09-12 21:39:40 +04001898 pp_directives[ct]);
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001899 goto fail;
1900 }
Cyrill Gorcunov84b4cba2010-09-12 21:39:40 +04001901 p = tline->text;
1902 if (tline->type == TOK_PREPROC_ID)
1903 p += 2; /* Skip leading %! */
1904 if (*p == '\'' || *p == '\"' || *p == '`')
1905 nasm_unquote_cstr(p, ct);
1906 if (getenv(p))
1907 j = true;
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001908 tline = tline->next;
1909 }
1910 break;
1911
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001912 case PPC_IFIDN:
1913 case PPC_IFIDNI:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001914 tline = expand_smacro(tline);
1915 t = tt = tline;
1916 while (tok_isnt_(tt, ","))
1917 tt = tt->next;
1918 if (!tt) {
1919 error(ERR_NONFATAL,
1920 "`%s' expects two comma-separated arguments",
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001921 pp_directives[ct]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001922 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001923 }
1924 tt = tt->next;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001925 j = true; /* assume equality unless proved not */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001926 while ((t->type != TOK_OTHER || strcmp(t->text, ",")) && tt) {
1927 if (tt->type == TOK_OTHER && !strcmp(tt->text, ",")) {
1928 error(ERR_NONFATAL, "`%s': more than one comma on line",
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001929 pp_directives[ct]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001930 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001931 }
1932 if (t->type == TOK_WHITESPACE) {
1933 t = t->next;
1934 continue;
1935 }
1936 if (tt->type == TOK_WHITESPACE) {
1937 tt = tt->next;
1938 continue;
1939 }
1940 if (tt->type != t->type) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001941 j = false; /* found mismatching tokens */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001942 break;
1943 }
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07001944 /* When comparing strings, need to unquote them first */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001945 if (t->type == TOK_STRING) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001946 size_t l1 = nasm_unquote(t->text, NULL);
1947 size_t l2 = nasm_unquote(tt->text, NULL);
H. Peter Anvind2456592008-06-19 15:04:18 -07001948
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001949 if (l1 != l2) {
1950 j = false;
1951 break;
1952 }
1953 if (mmemcmp(t->text, tt->text, l1, i == PPC_IFIDN)) {
1954 j = false;
1955 break;
1956 }
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07001957 } else if (mstrcmp(tt->text, t->text, i == PPC_IFIDN) != 0) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001958 j = false; /* found mismatching tokens */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001959 break;
1960 }
Nickolay Yurchenkof3b3ce22003-09-21 20:38:43 +00001961
H. Peter Anvine2c80182005-01-15 22:15:51 +00001962 t = t->next;
1963 tt = tt->next;
1964 }
1965 if ((t->type != TOK_OTHER || strcmp(t->text, ",")) || tt)
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001966 j = false; /* trailing gunk on one end or other */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001967 break;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001968
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001969 case PPC_IFMACRO:
H. Peter Anvin89cee572009-07-15 09:16:54 -04001970 {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001971 bool found = false;
Keith Kaniosb307a4f2010-11-06 17:41:51 -05001972 ExpDef searching, *ed;
H. Peter Anvin65747262002-05-07 00:10:05 +00001973
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001974 skip_white_(tline);
1975 tline = expand_id(tline);
1976 if (!tok_type_(tline, TOK_ID)) {
1977 error(ERR_NONFATAL,
1978 "`%s' expects a macro name", pp_directives[ct]);
1979 goto fail;
1980 }
1981 searching.name = nasm_strdup(tline->text);
1982 searching.casesense = true;
1983 searching.plus = false;
1984 searching.nolist = false;
Keith Kaniosb307a4f2010-11-06 17:41:51 -05001985 //searching.in_progress = 0;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001986 searching.max_depth = 0;
Keith Kaniosb307a4f2010-11-06 17:41:51 -05001987 //searching.rep_nest = NULL;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001988 searching.nparam_min = 0;
1989 searching.nparam_max = INT_MAX;
1990 tline = expand_smacro(tline->next);
1991 skip_white_(tline);
1992 if (!tline) {
1993 } else if (!tok_type_(tline, TOK_NUMBER)) {
1994 error(ERR_NONFATAL,
1995 "`%s' expects a parameter count or nothing",
1996 pp_directives[ct]);
1997 } else {
1998 searching.nparam_min = searching.nparam_max =
1999 readnum(tline->text, &j);
2000 if (j)
2001 error(ERR_NONFATAL,
2002 "unable to parse parameter count `%s'",
2003 tline->text);
2004 }
2005 if (tline && tok_is_(tline->next, "-")) {
2006 tline = tline->next->next;
2007 if (tok_is_(tline, "*"))
2008 searching.nparam_max = INT_MAX;
2009 else if (!tok_type_(tline, TOK_NUMBER))
2010 error(ERR_NONFATAL,
2011 "`%s' expects a parameter count after `-'",
2012 pp_directives[ct]);
2013 else {
2014 searching.nparam_max = readnum(tline->text, &j);
2015 if (j)
2016 error(ERR_NONFATAL,
2017 "unable to parse parameter count `%s'",
2018 tline->text);
2019 if (searching.nparam_min > searching.nparam_max)
2020 error(ERR_NONFATAL,
2021 "minimum parameter count exceeds maximum");
2022 }
2023 }
2024 if (tline && tok_is_(tline->next, "+")) {
2025 tline = tline->next;
2026 searching.plus = true;
2027 }
Keith Kaniosb307a4f2010-11-06 17:41:51 -05002028 ed = (ExpDef *) hash_findix(&expdefs, searching.name);
2029 while (ed != NULL) {
2030 if (!strcmp(ed->name, searching.name) &&
2031 (ed->nparam_min <= searching.nparam_max
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002032 || searching.plus)
Keith Kaniosb307a4f2010-11-06 17:41:51 -05002033 && (searching.nparam_min <= ed->nparam_max
2034 || ed->plus)) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002035 found = true;
2036 break;
2037 }
Keith Kaniosb307a4f2010-11-06 17:41:51 -05002038 ed = ed->next;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002039 }
2040 if (tline && tline->next)
2041 error(ERR_WARNING|ERR_PASS1,
2042 "trailing garbage after %%ifmacro ignored");
2043 nasm_free(searching.name);
2044 j = found;
2045 break;
H. Peter Anvin89cee572009-07-15 09:16:54 -04002046 }
H. Peter Anvin65747262002-05-07 00:10:05 +00002047
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002048 case PPC_IFID:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002049 needtype = TOK_ID;
2050 goto iftype;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002051 case PPC_IFNUM:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002052 needtype = TOK_NUMBER;
2053 goto iftype;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002054 case PPC_IFSTR:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002055 needtype = TOK_STRING;
2056 goto iftype;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002057
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002058iftype:
2059 t = tline = expand_smacro(tline);
H. Peter Anvind85d2502008-05-04 17:53:31 -07002060
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002061 while (tok_type_(t, TOK_WHITESPACE) ||
2062 (needtype == TOK_NUMBER &&
2063 tok_type_(t, TOK_OTHER) &&
2064 (t->text[0] == '-' || t->text[0] == '+') &&
2065 !t->text[1]))
2066 t = t->next;
H. Peter Anvind85d2502008-05-04 17:53:31 -07002067
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002068 j = tok_type_(t, needtype);
2069 break;
H. Peter Anvincbf768d2008-02-16 16:41:25 -08002070
2071 case PPC_IFTOKEN:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002072 t = tline = expand_smacro(tline);
2073 while (tok_type_(t, TOK_WHITESPACE))
2074 t = t->next;
H. Peter Anvincbf768d2008-02-16 16:41:25 -08002075
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002076 j = false;
2077 if (t) {
2078 t = t->next; /* Skip the actual token */
2079 while (tok_type_(t, TOK_WHITESPACE))
2080 t = t->next;
2081 j = !t; /* Should be nothing left */
2082 }
2083 break;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002084
H. Peter Anvin134b9462008-02-16 17:01:40 -08002085 case PPC_IFEMPTY:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002086 t = tline = expand_smacro(tline);
2087 while (tok_type_(t, TOK_WHITESPACE))
2088 t = t->next;
H. Peter Anvin134b9462008-02-16 17:01:40 -08002089
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002090 j = !t; /* Should be empty */
2091 break;
H. Peter Anvin134b9462008-02-16 17:01:40 -08002092
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002093 case PPC_IF:
H. Peter Anvine2c80182005-01-15 22:15:51 +00002094 t = tline = expand_smacro(tline);
2095 tptr = &t;
2096 tokval.t_type = TOKEN_INVALID;
2097 evalresult = evaluate(ppscan, tptr, &tokval,
2098 NULL, pass | CRITICAL, error, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002099 if (!evalresult)
2100 return -1;
2101 if (tokval.t_type)
H. Peter Anvin917a3492008-09-24 09:14:49 -07002102 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +00002103 "trailing garbage after expression ignored");
2104 if (!is_simple(evalresult)) {
2105 error(ERR_NONFATAL,
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002106 "non-constant value given to `%s'", pp_directives[ct]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002107 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002108 }
Chuck Crayne60ae75d2007-05-02 01:59:16 +00002109 j = reloc_value(evalresult) != 0;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002110 break;
H. Peter Anvin95e28822007-09-12 04:20:08 +00002111
H. Peter Anvine2c80182005-01-15 22:15:51 +00002112 default:
2113 error(ERR_FATAL,
2114 "preprocessor directive `%s' not yet implemented",
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002115 pp_directives[ct]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002116 goto fail;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002117 }
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002118
2119 free_tlist(origline);
2120 return j ^ PP_NEGATIVE(ct);
H. Peter Anvin70653092007-10-19 14:42:29 -07002121
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002122fail:
2123 free_tlist(origline);
2124 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002125}
2126
2127/*
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002128 * Common code for defining an smacro
2129 */
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08002130static bool define_smacro(Context *ctx, const char *mname, bool casesense,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002131 int nparam, Token *expansion)
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002132{
2133 SMacro *smac, **smhead;
H. Peter Anvin166c2472008-05-28 12:28:58 -07002134 struct hash_table *smtbl;
H. Peter Anvin70653092007-10-19 14:42:29 -07002135
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002136 if (smacro_defined(ctx, mname, nparam, &smac, casesense)) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002137 if (!smac) {
2138 error(ERR_WARNING|ERR_PASS1,
2139 "single-line macro `%s' defined both with and"
2140 " without parameters", mname);
2141 /*
2142 * Some instances of the old code considered this a failure,
2143 * some others didn't. What is the right thing to do here?
2144 */
2145 free_tlist(expansion);
2146 return false; /* Failure */
2147 } else {
2148 /*
2149 * We're redefining, so we have to take over an
2150 * existing SMacro structure. This means freeing
2151 * what was already in it.
2152 */
2153 nasm_free(smac->name);
2154 free_tlist(smac->expansion);
2155 }
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002156 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002157 smtbl = ctx ? &ctx->localmac : &smacros;
2158 smhead = (SMacro **) hash_findi_add(smtbl, mname);
2159 smac = nasm_malloc(sizeof(SMacro));
2160 smac->next = *smhead;
2161 *smhead = smac;
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002162 }
2163 smac->name = nasm_strdup(mname);
2164 smac->casesense = casesense;
2165 smac->nparam = nparam;
2166 smac->expansion = expansion;
2167 smac->in_progress = false;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002168 return true; /* Success */
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002169}
2170
2171/*
2172 * Undefine an smacro
2173 */
2174static void undef_smacro(Context *ctx, const char *mname)
2175{
2176 SMacro **smhead, *s, **sp;
H. Peter Anvin166c2472008-05-28 12:28:58 -07002177 struct hash_table *smtbl;
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002178
H. Peter Anvin166c2472008-05-28 12:28:58 -07002179 smtbl = ctx ? &ctx->localmac : &smacros;
2180 smhead = (SMacro **)hash_findi(smtbl, mname, NULL);
H. Peter Anvin70653092007-10-19 14:42:29 -07002181
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002182 if (smhead) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002183 /*
2184 * We now have a macro name... go hunt for it.
2185 */
2186 sp = smhead;
2187 while ((s = *sp) != NULL) {
2188 if (!mstrcmp(s->name, mname, s->casesense)) {
2189 *sp = s->next;
2190 nasm_free(s->name);
2191 free_tlist(s->expansion);
2192 nasm_free(s);
2193 } else {
2194 sp = &s->next;
2195 }
2196 }
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002197 }
2198}
2199
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002200/*
H. Peter Anvina26433d2008-07-16 14:40:01 -07002201 * Parse a mmacro specification.
2202 */
Keith Kaniosb307a4f2010-11-06 17:41:51 -05002203static bool parse_mmacro_spec(Token *tline, ExpDef *def, const char *directive)
H. Peter Anvina26433d2008-07-16 14:40:01 -07002204{
2205 bool err;
2206
2207 tline = tline->next;
2208 skip_white_(tline);
2209 tline = expand_id(tline);
2210 if (!tok_type_(tline, TOK_ID)) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002211 error(ERR_NONFATAL, "`%s' expects a macro name", directive);
2212 return false;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002213 }
Victor van den Elzenb916ede2008-07-23 15:14:22 +02002214
H. Peter Anvina26433d2008-07-16 14:40:01 -07002215 def->name = nasm_strdup(tline->text);
2216 def->plus = false;
2217 def->nolist = false;
Keith Kaniosb307a4f2010-11-06 17:41:51 -05002218// def->in_progress = 0;
2219// def->rep_nest = NULL;
Victor van den Elzenb916ede2008-07-23 15:14:22 +02002220 def->nparam_min = 0;
2221 def->nparam_max = 0;
2222
H. Peter Anvina26433d2008-07-16 14:40:01 -07002223 tline = expand_smacro(tline->next);
2224 skip_white_(tline);
2225 if (!tok_type_(tline, TOK_NUMBER)) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002226 error(ERR_NONFATAL, "`%s' expects a parameter count", directive);
H. Peter Anvina26433d2008-07-16 14:40:01 -07002227 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002228 def->nparam_min = def->nparam_max =
2229 readnum(tline->text, &err);
2230 if (err)
2231 error(ERR_NONFATAL,
2232 "unable to parse parameter count `%s'", tline->text);
H. Peter Anvina26433d2008-07-16 14:40:01 -07002233 }
2234 if (tline && tok_is_(tline->next, "-")) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002235 tline = tline->next->next;
2236 if (tok_is_(tline, "*")) {
2237 def->nparam_max = INT_MAX;
2238 } else if (!tok_type_(tline, TOK_NUMBER)) {
2239 error(ERR_NONFATAL,
2240 "`%s' expects a parameter count after `-'", directive);
2241 } else {
2242 def->nparam_max = readnum(tline->text, &err);
2243 if (err) {
2244 error(ERR_NONFATAL, "unable to parse parameter count `%s'",
2245 tline->text);
2246 }
2247 if (def->nparam_min > def->nparam_max) {
2248 error(ERR_NONFATAL, "minimum parameter count exceeds maximum");
2249 }
2250 }
H. Peter Anvina26433d2008-07-16 14:40:01 -07002251 }
2252 if (tline && tok_is_(tline->next, "+")) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002253 tline = tline->next;
2254 def->plus = true;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002255 }
2256 if (tline && tok_type_(tline->next, TOK_ID) &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002257 !nasm_stricmp(tline->next->text, ".nolist")) {
2258 tline = tline->next;
2259 def->nolist = true;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002260 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002261
H. Peter Anvina26433d2008-07-16 14:40:01 -07002262 /*
2263 * Handle default parameters.
2264 */
2265 if (tline && tline->next) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002266 def->dlist = tline->next;
2267 tline->next = NULL;
2268 count_mmac_params(def->dlist, &def->ndefs, &def->defaults);
H. Peter Anvina26433d2008-07-16 14:40:01 -07002269 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002270 def->dlist = NULL;
2271 def->defaults = NULL;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002272 }
Keith Kaniosb307a4f2010-11-06 17:41:51 -05002273 def->line = NULL;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002274
H. Peter Anvin89cee572009-07-15 09:16:54 -04002275 if (def->defaults && def->ndefs > def->nparam_max - def->nparam_min &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002276 !def->plus)
2277 error(ERR_WARNING|ERR_PASS1|ERR_WARN_MDP,
2278 "too many default macro parameters");
Victor van den Elzenb916ede2008-07-23 15:14:22 +02002279
H. Peter Anvina26433d2008-07-16 14:40:01 -07002280 return true;
2281}
2282
2283
2284/*
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002285 * Decode a size directive
2286 */
2287static int parse_size(const char *str) {
2288 static const char *size_names[] =
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002289 { "byte", "dword", "oword", "qword", "tword", "word", "yword" };
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002290 static const int sizes[] =
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002291 { 0, 1, 4, 16, 8, 10, 2, 32 };
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002292
Cyrill Gorcunova7319242010-06-03 22:04:36 +04002293 return sizes[bsii(str, size_names, ARRAY_SIZE(size_names))+1];
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002294}
2295
Ed Beroset3ab3f412002-06-11 03:31:49 +00002296/**
2297 * find and process preprocessor directive in passed line
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002298 * Find out if a line contains a preprocessor directive, and deal
2299 * with it if so.
H. Peter Anvin70653092007-10-19 14:42:29 -07002300 *
Ed Beroset3ab3f412002-06-11 03:31:49 +00002301 * If a directive _is_ found, it is the responsibility of this routine
2302 * (and not the caller) to free_tlist() the line.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002303 *
Ed Beroset3ab3f412002-06-11 03:31:49 +00002304 * @param tline a pointer to the current tokeninzed line linked list
2305 * @return DIRECTIVE_FOUND or NO_DIRECTIVE_FOUND
H. Peter Anvin70653092007-10-19 14:42:29 -07002306 *
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002307 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002308static int do_directive(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00002309{
H. Peter Anvin4169a472007-09-12 01:29:43 +00002310 enum preproc_token i;
2311 int j;
H. Peter Anvin70055962007-10-11 00:05:31 -07002312 bool err;
2313 int nparam;
2314 bool nolist;
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07002315 bool casesense;
H. Peter Anvin8cfdb9d2007-09-14 18:36:01 -07002316 int k, m;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002317 int offset;
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08002318 char *p, *pp;
2319 const char *mname;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002320 Include *inc;
2321 Context *ctx;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002322 Line *l;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002323 Token *t, *tt, *param_start, *macro_start, *last, **tptr, *origline;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002324 struct tokenval tokval;
2325 expr *evalresult;
Keith Kaniosb307a4f2010-11-06 17:41:51 -05002326 ExpDef *ed, *eed, **edhead;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002327 ExpInv *ei, *eei;
H. Peter Anvinf8ba53e2007-10-11 10:11:57 -07002328 int64_t count;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07002329 size_t len;
H. Peter Anvin8e3f75e2008-09-24 00:21:58 -07002330 int severity;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002331
2332 origline = tline;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002333
H. Peter Anvineba20a72002-04-30 20:53:55 +00002334 skip_white_(tline);
H. Peter Anvinf2936d72008-06-04 15:11:23 -07002335 if (!tline || !tok_type_(tline, TOK_PREPROC_ID) ||
H. Peter Anvine2c80182005-01-15 22:15:51 +00002336 (tline->text[1] == '%' || tline->text[1] == '$'
2337 || tline->text[1] == '!'))
2338 return NO_DIRECTIVE_FOUND;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002339
H. Peter Anvin4169a472007-09-12 01:29:43 +00002340 i = pp_token_hash(tline->text);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002341
H. Peter Anvin4169a472007-09-12 01:29:43 +00002342 switch (i) {
2343 case PP_INVALID:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002344 if (defining != NULL) return NO_DIRECTIVE_FOUND;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002345 error(ERR_NONFATAL, "unknown preprocessor directive `%s'",
2346 tline->text);
2347 return NO_DIRECTIVE_FOUND; /* didn't get it */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002348
H. Peter Anvine2c80182005-01-15 22:15:51 +00002349 case PP_STACKSIZE:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002350 if (defining != NULL) return NO_DIRECTIVE_FOUND;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002351 /* Directive to tell NASM what the default stack size is. The
2352 * default is for a 16-bit stack, and this can be overriden with
2353 * %stacksize large.
H. Peter Anvine2c80182005-01-15 22:15:51 +00002354 */
2355 tline = tline->next;
2356 if (tline && tline->type == TOK_WHITESPACE)
2357 tline = tline->next;
2358 if (!tline || tline->type != TOK_ID) {
2359 error(ERR_NONFATAL, "`%%stacksize' missing size parameter");
2360 free_tlist(origline);
2361 return DIRECTIVE_FOUND;
2362 }
2363 if (nasm_stricmp(tline->text, "flat") == 0) {
2364 /* All subsequent ARG directives are for a 32-bit stack */
2365 StackSize = 4;
2366 StackPointer = "ebp";
2367 ArgOffset = 8;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002368 LocalOffset = 0;
Charles Crayne7eaf9192007-11-08 22:11:14 -08002369 } else if (nasm_stricmp(tline->text, "flat64") == 0) {
2370 /* All subsequent ARG directives are for a 64-bit stack */
2371 StackSize = 8;
2372 StackPointer = "rbp";
Per Jessen53252e02010-02-11 00:16:59 +03002373 ArgOffset = 16;
Charles Crayne7eaf9192007-11-08 22:11:14 -08002374 LocalOffset = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002375 } else if (nasm_stricmp(tline->text, "large") == 0) {
2376 /* All subsequent ARG directives are for a 16-bit stack,
2377 * far function call.
2378 */
2379 StackSize = 2;
2380 StackPointer = "bp";
2381 ArgOffset = 4;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002382 LocalOffset = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002383 } else if (nasm_stricmp(tline->text, "small") == 0) {
2384 /* All subsequent ARG directives are for a 16-bit stack,
2385 * far function call. We don't support near functions.
2386 */
2387 StackSize = 2;
2388 StackPointer = "bp";
2389 ArgOffset = 6;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002390 LocalOffset = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002391 } else {
2392 error(ERR_NONFATAL, "`%%stacksize' invalid size type");
2393 free_tlist(origline);
2394 return DIRECTIVE_FOUND;
2395 }
2396 free_tlist(origline);
2397 return DIRECTIVE_FOUND;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002398
H. Peter Anvine2c80182005-01-15 22:15:51 +00002399 case PP_ARG:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002400 if (defining != NULL) return NO_DIRECTIVE_FOUND;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002401 /* TASM like ARG directive to define arguments to functions, in
2402 * the following form:
2403 *
2404 * ARG arg1:WORD, arg2:DWORD, arg4:QWORD
2405 */
2406 offset = ArgOffset;
2407 do {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00002408 char *arg, directive[256];
H. Peter Anvine2c80182005-01-15 22:15:51 +00002409 int size = StackSize;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002410
H. Peter Anvine2c80182005-01-15 22:15:51 +00002411 /* Find the argument name */
2412 tline = tline->next;
2413 if (tline && tline->type == TOK_WHITESPACE)
2414 tline = tline->next;
2415 if (!tline || tline->type != TOK_ID) {
2416 error(ERR_NONFATAL, "`%%arg' missing argument parameter");
2417 free_tlist(origline);
2418 return DIRECTIVE_FOUND;
2419 }
2420 arg = tline->text;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002421
H. Peter Anvine2c80182005-01-15 22:15:51 +00002422 /* Find the argument size type */
2423 tline = tline->next;
2424 if (!tline || tline->type != TOK_OTHER
2425 || tline->text[0] != ':') {
2426 error(ERR_NONFATAL,
2427 "Syntax error processing `%%arg' directive");
2428 free_tlist(origline);
2429 return DIRECTIVE_FOUND;
2430 }
2431 tline = tline->next;
2432 if (!tline || tline->type != TOK_ID) {
2433 error(ERR_NONFATAL, "`%%arg' missing size type parameter");
2434 free_tlist(origline);
2435 return DIRECTIVE_FOUND;
2436 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002437
H. Peter Anvine2c80182005-01-15 22:15:51 +00002438 /* Allow macro expansion of type parameter */
Keith Kaniosb7a89542007-04-12 02:40:54 +00002439 tt = tokenize(tline->text);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002440 tt = expand_smacro(tt);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002441 size = parse_size(tt->text);
2442 if (!size) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002443 error(ERR_NONFATAL,
2444 "Invalid size type for `%%arg' missing directive");
2445 free_tlist(tt);
2446 free_tlist(origline);
2447 return DIRECTIVE_FOUND;
2448 }
2449 free_tlist(tt);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002450
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002451 /* Round up to even stack slots */
2452 size = ALIGN(size, StackSize);
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002453
H. Peter Anvine2c80182005-01-15 22:15:51 +00002454 /* Now define the macro for the argument */
2455 snprintf(directive, sizeof(directive), "%%define %s (%s+%d)",
2456 arg, StackPointer, offset);
Keith Kaniosb7a89542007-04-12 02:40:54 +00002457 do_directive(tokenize(directive));
H. Peter Anvine2c80182005-01-15 22:15:51 +00002458 offset += size;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002459
H. Peter Anvine2c80182005-01-15 22:15:51 +00002460 /* Move to the next argument in the list */
2461 tline = tline->next;
2462 if (tline && tline->type == TOK_WHITESPACE)
2463 tline = tline->next;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002464 } while (tline && tline->type == TOK_OTHER && tline->text[0] == ',');
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002465 ArgOffset = offset;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002466 free_tlist(origline);
2467 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002468
H. Peter Anvine2c80182005-01-15 22:15:51 +00002469 case PP_LOCAL:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002470 if (defining != NULL) return NO_DIRECTIVE_FOUND;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002471 /* TASM like LOCAL directive to define local variables for a
2472 * function, in the following form:
2473 *
2474 * LOCAL local1:WORD, local2:DWORD, local4:QWORD = LocalSize
2475 *
2476 * The '= LocalSize' at the end is ignored by NASM, but is
2477 * required by TASM to define the local parameter size (and used
2478 * by the TASM macro package).
2479 */
2480 offset = LocalOffset;
2481 do {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00002482 char *local, directive[256];
H. Peter Anvine2c80182005-01-15 22:15:51 +00002483 int size = StackSize;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002484
H. Peter Anvine2c80182005-01-15 22:15:51 +00002485 /* Find the argument name */
2486 tline = tline->next;
2487 if (tline && tline->type == TOK_WHITESPACE)
2488 tline = tline->next;
2489 if (!tline || tline->type != TOK_ID) {
2490 error(ERR_NONFATAL,
2491 "`%%local' missing argument parameter");
2492 free_tlist(origline);
2493 return DIRECTIVE_FOUND;
2494 }
2495 local = tline->text;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002496
H. Peter Anvine2c80182005-01-15 22:15:51 +00002497 /* Find the argument size type */
2498 tline = tline->next;
2499 if (!tline || tline->type != TOK_OTHER
2500 || tline->text[0] != ':') {
2501 error(ERR_NONFATAL,
2502 "Syntax error processing `%%local' directive");
2503 free_tlist(origline);
2504 return DIRECTIVE_FOUND;
2505 }
2506 tline = tline->next;
2507 if (!tline || tline->type != TOK_ID) {
2508 error(ERR_NONFATAL,
2509 "`%%local' missing size type parameter");
2510 free_tlist(origline);
2511 return DIRECTIVE_FOUND;
2512 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00002513
H. Peter Anvine2c80182005-01-15 22:15:51 +00002514 /* Allow macro expansion of type parameter */
Keith Kaniosb7a89542007-04-12 02:40:54 +00002515 tt = tokenize(tline->text);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002516 tt = expand_smacro(tt);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002517 size = parse_size(tt->text);
2518 if (!size) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002519 error(ERR_NONFATAL,
2520 "Invalid size type for `%%local' missing directive");
2521 free_tlist(tt);
2522 free_tlist(origline);
2523 return DIRECTIVE_FOUND;
2524 }
2525 free_tlist(tt);
H. Peter Anvin734b1882002-04-30 21:01:08 +00002526
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002527 /* Round up to even stack slots */
2528 size = ALIGN(size, StackSize);
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002529
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002530 offset += size; /* Negative offset, increment before */
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002531
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002532 /* Now define the macro for the argument */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002533 snprintf(directive, sizeof(directive), "%%define %s (%s-%d)",
2534 local, StackPointer, offset);
Keith Kaniosb7a89542007-04-12 02:40:54 +00002535 do_directive(tokenize(directive));
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002536
H. Peter Anvine2c80182005-01-15 22:15:51 +00002537 /* Now define the assign to setup the enter_c macro correctly */
2538 snprintf(directive, sizeof(directive),
2539 "%%assign %%$localsize %%$localsize+%d", size);
Keith Kaniosb7a89542007-04-12 02:40:54 +00002540 do_directive(tokenize(directive));
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002541
H. Peter Anvine2c80182005-01-15 22:15:51 +00002542 /* Move to the next argument in the list */
2543 tline = tline->next;
2544 if (tline && tline->type == TOK_WHITESPACE)
2545 tline = tline->next;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002546 } while (tline && tline->type == TOK_OTHER && tline->text[0] == ',');
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002547 LocalOffset = offset;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002548 free_tlist(origline);
2549 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002550
H. Peter Anvine2c80182005-01-15 22:15:51 +00002551 case PP_CLEAR:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002552 if (defining != NULL) return NO_DIRECTIVE_FOUND;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002553 if (tline->next)
H. Peter Anvin917a3492008-09-24 09:14:49 -07002554 error(ERR_WARNING|ERR_PASS1,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002555 "trailing garbage after `%%clear' ignored");
2556 free_macros();
2557 init_macros();
H. Peter Anvine2c80182005-01-15 22:15:51 +00002558 free_tlist(origline);
2559 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002560
H. Peter Anvin418ca702008-05-30 10:42:30 -07002561 case PP_DEPEND:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002562 if (defining != NULL) return NO_DIRECTIVE_FOUND;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002563 t = tline->next = expand_smacro(tline->next);
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002564 skip_white_(t);
2565 if (!t || (t->type != TOK_STRING &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002566 t->type != TOK_INTERNAL_STRING)) {
H. Peter Anvin418ca702008-05-30 10:42:30 -07002567 error(ERR_NONFATAL, "`%%depend' expects a file name");
2568 free_tlist(origline);
2569 return DIRECTIVE_FOUND; /* but we did _something_ */
2570 }
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002571 if (t->next)
H. Peter Anvin917a3492008-09-24 09:14:49 -07002572 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvin418ca702008-05-30 10:42:30 -07002573 "trailing garbage after `%%depend' ignored");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002574 p = t->text;
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002575 if (t->type != TOK_INTERNAL_STRING)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002576 nasm_unquote_cstr(p, i);
2577 if (dephead && !in_list(*dephead, p)) {
2578 StrList *sl = nasm_malloc(strlen(p)+1+sizeof sl->next);
2579 sl->next = NULL;
2580 strcpy(sl->str, p);
2581 *deptail = sl;
2582 deptail = &sl->next;
2583 }
2584 free_tlist(origline);
H. Peter Anvin418ca702008-05-30 10:42:30 -07002585 return DIRECTIVE_FOUND;
2586
2587 case PP_INCLUDE:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002588 if (defining != NULL) return NO_DIRECTIVE_FOUND;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002589 t = tline->next = expand_smacro(tline->next);
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002590 skip_white_(t);
H. Peter Anvind2456592008-06-19 15:04:18 -07002591
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002592 if (!t || (t->type != TOK_STRING &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002593 t->type != TOK_INTERNAL_STRING)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002594 error(ERR_NONFATAL, "`%%include' expects a file name");
2595 free_tlist(origline);
2596 return DIRECTIVE_FOUND; /* but we did _something_ */
2597 }
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002598 if (t->next)
H. Peter Anvin917a3492008-09-24 09:14:49 -07002599 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +00002600 "trailing garbage after `%%include' ignored");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002601 p = t->text;
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002602 if (t->type != TOK_INTERNAL_STRING)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002603 nasm_unquote_cstr(p, i);
Cyrill Gorcunov55cc4d02010-11-10 23:12:06 +03002604 inc = nasm_zalloc(sizeof(Include));
H. Peter Anvine2c80182005-01-15 22:15:51 +00002605 inc->next = istk;
H. Peter Anvin2b1c3b92008-06-06 10:38:46 -07002606 inc->fp = inc_fopen(p, dephead, &deptail, pass == 0);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002607 if (!inc->fp) {
2608 /* -MG given but file not found */
2609 nasm_free(inc);
2610 } else {
2611 inc->fname = src_set_fname(nasm_strdup(p));
2612 inc->lineno = src_set_linnum(0);
2613 inc->lineinc = 1;
2614 inc->expansion = NULL;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002615 istk = inc;
2616 list->uplevel(LIST_INCLUDE);
2617 }
2618 free_tlist(origline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002619 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002620
H. Peter Anvind2456592008-06-19 15:04:18 -07002621 case PP_USE:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002622 if (defining != NULL) return NO_DIRECTIVE_FOUND;
H. Peter Anvinf4ae5ad2008-06-19 18:39:24 -07002623 {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002624 static macros_t *use_pkg;
2625 const char *pkg_macro = NULL;
H. Peter Anvinf4ae5ad2008-06-19 18:39:24 -07002626
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002627 tline = tline->next;
2628 skip_white_(tline);
2629 tline = expand_id(tline);
H. Peter Anvind2456592008-06-19 15:04:18 -07002630
H. Peter Anvin264b7b92008-10-24 16:38:17 -07002631 if (!tline || (tline->type != TOK_STRING &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002632 tline->type != TOK_INTERNAL_STRING &&
2633 tline->type != TOK_ID)) {
H. Peter Anvin926fc402008-06-19 16:26:12 -07002634 error(ERR_NONFATAL, "`%%use' expects a package name");
H. Peter Anvind2456592008-06-19 15:04:18 -07002635 free_tlist(origline);
2636 return DIRECTIVE_FOUND; /* but we did _something_ */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002637 }
H. Peter Anvin264b7b92008-10-24 16:38:17 -07002638 if (tline->next)
H. Peter Anvin917a3492008-09-24 09:14:49 -07002639 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvind2456592008-06-19 15:04:18 -07002640 "trailing garbage after `%%use' ignored");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002641 if (tline->type == TOK_STRING)
2642 nasm_unquote_cstr(tline->text, i);
2643 use_pkg = nasm_stdmac_find_package(tline->text);
2644 if (!use_pkg)
2645 error(ERR_NONFATAL, "unknown `%%use' package: %s", tline->text);
2646 else
2647 pkg_macro = (char *)use_pkg + 1; /* The first string will be <%define>__USE_*__ */
Victor van den Elzen35eb2ea2010-03-10 22:33:48 +01002648 if (use_pkg && ! smacro_defined(NULL, pkg_macro, 0, NULL, true)) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002649 /* Not already included, go ahead and include it */
2650 stdmacpos = use_pkg;
2651 }
2652 free_tlist(origline);
H. Peter Anvind2456592008-06-19 15:04:18 -07002653 return DIRECTIVE_FOUND;
H. Peter Anvinf4ae5ad2008-06-19 18:39:24 -07002654 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002655 case PP_PUSH:
H. Peter Anvine2c80182005-01-15 22:15:51 +00002656 case PP_REPL:
H. Peter Anvin42b56392008-10-24 16:24:21 -07002657 case PP_POP:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002658 if (defining != NULL) return NO_DIRECTIVE_FOUND;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002659 tline = tline->next;
2660 skip_white_(tline);
2661 tline = expand_id(tline);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002662 if (tline) {
2663 if (!tok_type_(tline, TOK_ID)) {
2664 error(ERR_NONFATAL, "`%s' expects a context identifier",
2665 pp_directives[i]);
2666 free_tlist(origline);
2667 return DIRECTIVE_FOUND; /* but we did _something_ */
2668 }
2669 if (tline->next)
2670 error(ERR_WARNING|ERR_PASS1,
2671 "trailing garbage after `%s' ignored",
2672 pp_directives[i]);
2673 p = nasm_strdup(tline->text);
2674 } else {
2675 p = NULL; /* Anonymous */
2676 }
H. Peter Anvin42b56392008-10-24 16:24:21 -07002677
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002678 if (i == PP_PUSH) {
2679 ctx = nasm_malloc(sizeof(Context));
2680 ctx->next = cstk;
2681 hash_init(&ctx->localmac, HASH_SMALL);
2682 ctx->name = p;
2683 ctx->number = unique++;
2684 cstk = ctx;
2685 } else {
2686 /* %pop or %repl */
2687 if (!cstk) {
2688 error(ERR_NONFATAL, "`%s': context stack is empty",
2689 pp_directives[i]);
2690 } else if (i == PP_POP) {
2691 if (p && (!cstk->name || nasm_stricmp(p, cstk->name)))
2692 error(ERR_NONFATAL, "`%%pop' in wrong context: %s, "
2693 "expected %s",
2694 cstk->name ? cstk->name : "anonymous", p);
2695 else
2696 ctx_pop();
2697 } else {
2698 /* i == PP_REPL */
2699 nasm_free(cstk->name);
2700 cstk->name = p;
2701 p = NULL;
2702 }
2703 nasm_free(p);
2704 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002705 free_tlist(origline);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002706 return DIRECTIVE_FOUND;
H. Peter Anvin8e3f75e2008-09-24 00:21:58 -07002707 case PP_FATAL:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002708 severity = ERR_FATAL;
2709 goto issue_error;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002710 case PP_ERROR:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002711 severity = ERR_NONFATAL;
2712 goto issue_error;
H. Peter Anvin7df04172008-06-10 18:27:38 -07002713 case PP_WARNING:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002714 severity = ERR_WARNING|ERR_WARN_USER;
2715 goto issue_error;
H. Peter Anvin8e3f75e2008-09-24 00:21:58 -07002716
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002717issue_error:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002718 if (defining != NULL) return NO_DIRECTIVE_FOUND;
H. Peter Anvin7df04172008-06-10 18:27:38 -07002719 {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002720 /* Only error out if this is the final pass */
2721 if (pass != 2 && i != PP_FATAL)
2722 return DIRECTIVE_FOUND;
2723
2724 tline->next = expand_smacro(tline->next);
2725 tline = tline->next;
2726 skip_white_(tline);
2727 t = tline ? tline->next : NULL;
2728 skip_white_(t);
2729 if (tok_type_(tline, TOK_STRING) && !t) {
2730 /* The line contains only a quoted string */
2731 p = tline->text;
2732 nasm_unquote(p, NULL); /* Ignore NUL character truncation */
2733 error(severity, "%s", p);
2734 } else {
2735 /* Not a quoted string, or more than a quoted string */
2736 p = detoken(tline, false);
2737 error(severity, "%s", p);
2738 nasm_free(p);
2739 }
2740 free_tlist(origline);
2741 return DIRECTIVE_FOUND;
H. Peter Anvin7df04172008-06-10 18:27:38 -07002742 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00002743
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002744 CASE_PP_IF:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002745 if (defining != NULL) {
2746 if (defining->type == EXP_IF) {
2747 defining->def_depth ++;
2748 }
2749 return NO_DIRECTIVE_FOUND;
2750 }
2751 if ((istk->expansion != NULL) &&
2752 (istk->expansion->emitting == false)) {
2753 j = COND_NEVER;
2754 } else {
2755 j = if_condition(tline->next, i);
2756 tline->next = NULL; /* it got freed */
2757 j = (((j < 0) ? COND_NEVER : j) ? COND_IF_TRUE : COND_IF_FALSE);
2758 }
2759 ed = new_ExpDef(EXP_IF);
2760 ed->state = j;
2761 ed->nolist = NULL;
2762 ed->def_depth = 0;
2763 ed->cur_depth = 0;
2764 ed->max_depth = 0;
2765 ed->ignoring = ((ed->state == COND_IF_TRUE) ? false : true);
2766 ed->prev = defining;
2767 defining = ed;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002768 free_tlist(origline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002769 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002770
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002771 CASE_PP_ELIF:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002772 if (defining != NULL) {
2773 if ((defining->type != EXP_IF) || (defining->def_depth > 0)) {
2774 return NO_DIRECTIVE_FOUND;
2775 }
2776 }
2777 if ((defining == NULL) || (defining->type != EXP_IF)) {
2778 error(ERR_FATAL, "`%s': no matching `%%if'", pp_directives[i]);
2779 }
Keith Kaniosb307a4f2010-11-06 17:41:51 -05002780 switch (defining->state) {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002781 case COND_IF_TRUE:
2782 defining->state = COND_DONE;
2783 defining->ignoring = true;
2784 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002785
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002786 case COND_DONE:
2787 case COND_NEVER:
2788 defining->ignoring = true;
2789 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002790
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002791 case COND_ELSE_TRUE:
2792 case COND_ELSE_FALSE:
2793 error_precond(ERR_WARNING|ERR_PASS1,
2794 "`%%elif' after `%%else' ignored");
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002795 defining->state = COND_NEVER;
2796 defining->ignoring = true;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002797 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002798
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002799 case COND_IF_FALSE:
2800 /*
2801 * IMPORTANT: In the case of %if, we will already have
2802 * called expand_mmac_params(); however, if we're
2803 * processing an %elif we must have been in a
2804 * non-emitting mode, which would have inhibited
2805 * the normal invocation of expand_mmac_params().
2806 * Therefore, we have to do it explicitly here.
2807 */
2808 j = if_condition(expand_mmac_params(tline->next), i);
2809 tline->next = NULL; /* it got freed */
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002810 defining->state =
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002811 j < 0 ? COND_NEVER : j ? COND_IF_TRUE : COND_IF_FALSE;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002812 defining->ignoring = ((defining->state == COND_IF_TRUE) ? false : true);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002813 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002814 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002815 free_tlist(origline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002816 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002817
H. Peter Anvine2c80182005-01-15 22:15:51 +00002818 case PP_ELSE:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002819 if (defining != NULL) {
2820 if ((defining->type != EXP_IF) || (defining->def_depth > 0)) {
2821 return NO_DIRECTIVE_FOUND;
2822 }
2823 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002824 if (tline->next)
H. Peter Anvin917a3492008-09-24 09:14:49 -07002825 error_precond(ERR_WARNING|ERR_PASS1,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002826 "trailing garbage after `%%else' ignored");
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002827 if ((defining == NULL) || (defining->type != EXP_IF)) {
2828 error(ERR_FATAL, "`%s': no matching `%%if'", pp_directives[i]);
2829 }
Keith Kaniosb307a4f2010-11-06 17:41:51 -05002830 switch (defining->state) {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002831 case COND_IF_TRUE:
2832 case COND_DONE:
2833 defining->state = COND_ELSE_FALSE;
2834 defining->ignoring = true;
2835 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002836
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002837 case COND_NEVER:
2838 defining->ignoring = true;
2839 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002840
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002841 case COND_IF_FALSE:
2842 defining->state = COND_ELSE_TRUE;
2843 defining->ignoring = false;
2844 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002845
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002846 case COND_ELSE_TRUE:
2847 case COND_ELSE_FALSE:
2848 error_precond(ERR_WARNING|ERR_PASS1,
2849 "`%%else' after `%%else' ignored.");
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002850 defining->state = COND_NEVER;
2851 defining->ignoring = true;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002852 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002853 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002854 free_tlist(origline);
2855 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002856
H. Peter Anvine2c80182005-01-15 22:15:51 +00002857 case PP_ENDIF:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002858 if (defining != NULL) {
2859 if (defining->type == EXP_IF) {
2860 if (defining->def_depth > 0) {
2861 defining->def_depth --;
2862 return NO_DIRECTIVE_FOUND;
2863 }
2864 } else {
2865 return NO_DIRECTIVE_FOUND;
2866 }
2867 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002868 if (tline->next)
H. Peter Anvin917a3492008-09-24 09:14:49 -07002869 error_precond(ERR_WARNING|ERR_PASS1,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002870 "trailing garbage after `%%endif' ignored");
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002871 if ((defining == NULL) || (defining->type != EXP_IF)) {
2872 error(ERR_NONFATAL, "`%%endif': no matching `%%if'");
2873 return DIRECTIVE_FOUND;
2874 }
2875 ed = defining;
2876 defining = ed->prev;
2877 ed->prev = expansions;
2878 expansions = ed;
2879 ei = new_ExpInv(EXP_IF, ed);
2880 ei->current = ed->line;
2881 ei->emitting = true;
2882 ei->prev = istk->expansion;
2883 istk->expansion = ei;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002884 free_tlist(origline);
2885 return DIRECTIVE_FOUND;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002886
H. Peter Anvindb8f96e2009-07-15 09:07:29 -04002887 case PP_RMACRO:
2888 case PP_IRMACRO:
H. Peter Anvine2c80182005-01-15 22:15:51 +00002889 case PP_MACRO:
2890 case PP_IMACRO:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002891 if (defining != NULL) {
2892 if (defining->type == EXP_MMACRO) {
2893 defining->def_depth ++;
2894 }
2895 return NO_DIRECTIVE_FOUND;
2896 }
2897 ed = new_ExpDef(EXP_MMACRO);
2898 ed->max_depth =
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002899 (i == PP_RMACRO) || (i == PP_IRMACRO) ? DEADMAN_LIMIT : 0;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002900 ed->casesense = (i == PP_MACRO) || (i == PP_RMACRO);
Keith Kaniosb307a4f2010-11-06 17:41:51 -05002901 if (!parse_mmacro_spec(tline, ed, pp_directives[i])) {
2902 nasm_free(ed);
2903 ed = NULL;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002904 return DIRECTIVE_FOUND;
2905 }
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002906 ed->def_depth = 0;
2907 ed->cur_depth = 0;
2908 ed->max_depth = (ed->max_depth + 1);
2909 ed->ignoring = false;
2910 ed->prev = defining;
2911 defining = ed;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002912
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002913 eed = (ExpDef *) hash_findix(&expdefs, ed->name);
2914 while (eed) {
2915 if (!strcmp(eed->name, ed->name) &&
2916 (eed->nparam_min <= ed->nparam_max
2917 || ed->plus)
2918 && (ed->nparam_min <= eed->nparam_max
2919 || eed->plus)) {
2920 error(ERR_WARNING|ERR_PASS1,
2921 "redefining multi-line macro `%s'", ed->name);
2922 return DIRECTIVE_FOUND;
2923 }
2924 eed = eed->next;
2925 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002926 free_tlist(origline);
2927 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002928
H. Peter Anvine2c80182005-01-15 22:15:51 +00002929 case PP_ENDM:
2930 case PP_ENDMACRO:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002931 if (defining != NULL) {
2932 if (defining->type == EXP_MMACRO) {
2933 if (defining->def_depth > 0) {
2934 defining->def_depth --;
2935 return NO_DIRECTIVE_FOUND;
2936 }
2937 } else {
2938 return NO_DIRECTIVE_FOUND;
2939 }
2940 }
2941 if (!(defining) || (defining->type != EXP_MMACRO)) {
2942 error(ERR_NONFATAL, "`%s': not defining a macro", tline->text);
2943 return DIRECTIVE_FOUND;
2944 }
2945 edhead = (ExpDef **) hash_findi_add(&expdefs, defining->name);
2946 defining->next = *edhead;
2947 *edhead = defining;
2948 ed = defining;
2949 defining = ed->prev;
2950 ed->prev = expansions;
2951 expansions = ed;
2952 ed = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002953 free_tlist(origline);
2954 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002955
H. Peter Anvin89cee572009-07-15 09:16:54 -04002956 case PP_EXITMACRO:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002957 if (defining != NULL) return NO_DIRECTIVE_FOUND;
2958 /*
2959 * We must search along istk->expansion until we hit a
2960 * macro invocation. Then we disable the emitting state(s)
2961 * between exitmacro and endmacro.
2962 */
2963 for (ei = istk->expansion; ei != NULL; ei = ei->prev) {
2964 if(ei->type == EXP_MMACRO) {
2965 break;
2966 }
2967 }
2968
2969 if (ei != NULL) {
2970 /*
2971 * Set all invocations leading back to the macro
2972 * invocation to a non-emitting state.
2973 */
2974 for (eei = istk->expansion; eei != ei; eei = eei->prev) {
2975 eei->emitting = false;
2976 }
2977 eei->emitting = false;
2978 } else {
2979 error(ERR_NONFATAL, "`%%exitmacro' not within `%%macro' block");
2980 }
Keith Kanios852f1ee2009-07-12 00:19:55 -05002981 free_tlist(origline);
2982 return DIRECTIVE_FOUND;
2983
H. Peter Anvina26433d2008-07-16 14:40:01 -07002984 case PP_UNMACRO:
2985 case PP_UNIMACRO:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002986 if (defining != NULL) return NO_DIRECTIVE_FOUND;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002987 {
Keith Kaniosb307a4f2010-11-06 17:41:51 -05002988 ExpDef **ed_p;
2989 ExpDef spec;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002990
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002991 spec.casesense = (i == PP_UNMACRO);
2992 if (!parse_mmacro_spec(tline, &spec, pp_directives[i])) {
2993 return DIRECTIVE_FOUND;
2994 }
Keith Kaniosb307a4f2010-11-06 17:41:51 -05002995 ed_p = (ExpDef **) hash_findi(&expdefs, spec.name, NULL);
2996 while (ed_p && *ed_p) {
2997 ed = *ed_p;
2998 if (ed->casesense == spec.casesense &&
2999 !mstrcmp(ed->name, spec.name, spec.casesense) &&
3000 ed->nparam_min == spec.nparam_min &&
3001 ed->nparam_max == spec.nparam_max &&
3002 ed->plus == spec.plus) {
3003 *ed_p = ed->next;
3004 free_expdef(ed);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003005 } else {
Keith Kaniosb307a4f2010-11-06 17:41:51 -05003006 ed_p = &ed->next;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003007 }
3008 }
3009 free_tlist(origline);
3010 free_tlist(spec.dlist);
3011 return DIRECTIVE_FOUND;
H. Peter Anvina26433d2008-07-16 14:40:01 -07003012 }
3013
H. Peter Anvine2c80182005-01-15 22:15:51 +00003014 case PP_ROTATE:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003015 if (defining != NULL) return NO_DIRECTIVE_FOUND;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003016 if (tline->next && tline->next->type == TOK_WHITESPACE)
3017 tline = tline->next;
H. Peter Anvin89cee572009-07-15 09:16:54 -04003018 if (!tline->next) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00003019 free_tlist(origline);
3020 error(ERR_NONFATAL, "`%%rotate' missing rotate count");
3021 return DIRECTIVE_FOUND;
3022 }
3023 t = expand_smacro(tline->next);
3024 tline->next = NULL;
3025 free_tlist(origline);
3026 tline = t;
3027 tptr = &t;
3028 tokval.t_type = TOKEN_INVALID;
3029 evalresult =
3030 evaluate(ppscan, tptr, &tokval, NULL, pass, error, NULL);
3031 free_tlist(tline);
3032 if (!evalresult)
3033 return DIRECTIVE_FOUND;
3034 if (tokval.t_type)
H. Peter Anvin917a3492008-09-24 09:14:49 -07003035 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +00003036 "trailing garbage after expression ignored");
3037 if (!is_simple(evalresult)) {
3038 error(ERR_NONFATAL, "non-constant value given to `%%rotate'");
3039 return DIRECTIVE_FOUND;
3040 }
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003041 for (ei = istk->expansion; ei != NULL; ei = ei->prev) {
3042 if (ei->type == EXP_MMACRO) {
3043 break;
3044 }
3045 }
3046 if (ei == NULL) {
3047 error(ERR_NONFATAL, "`%%rotate' invoked outside a macro call");
3048 } else if (ei->nparam == 0) {
3049 error(ERR_NONFATAL,
3050 "`%%rotate' invoked within macro without parameters");
3051 } else {
3052 int rotate = ei->rotate + reloc_value(evalresult);
3053
3054 rotate %= (int)ei->nparam;
3055 if (rotate < 0)
3056 rotate += ei->nparam;
3057 ei->rotate = rotate;
3058 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00003059 return DIRECTIVE_FOUND;
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00003060
H. Peter Anvine2c80182005-01-15 22:15:51 +00003061 case PP_REP:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003062 if (defining != NULL) {
3063 if (defining->type == EXP_REP) {
3064 defining->def_depth ++;
3065 }
3066 return NO_DIRECTIVE_FOUND;
3067 }
H. Peter Anvin6867acc2007-10-10 14:58:45 -07003068 nolist = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003069 do {
3070 tline = tline->next;
3071 } while (tok_type_(tline, TOK_WHITESPACE));
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00003072
H. Peter Anvine2c80182005-01-15 22:15:51 +00003073 if (tok_type_(tline, TOK_ID) &&
3074 nasm_stricmp(tline->text, ".nolist") == 0) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07003075 nolist = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003076 do {
3077 tline = tline->next;
3078 } while (tok_type_(tline, TOK_WHITESPACE));
3079 }
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00003080
H. Peter Anvine2c80182005-01-15 22:15:51 +00003081 if (tline) {
3082 t = expand_smacro(tline);
3083 tptr = &t;
3084 tokval.t_type = TOKEN_INVALID;
3085 evalresult =
3086 evaluate(ppscan, tptr, &tokval, NULL, pass, error, NULL);
3087 if (!evalresult) {
3088 free_tlist(origline);
3089 return DIRECTIVE_FOUND;
3090 }
3091 if (tokval.t_type)
H. Peter Anvin917a3492008-09-24 09:14:49 -07003092 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +00003093 "trailing garbage after expression ignored");
3094 if (!is_simple(evalresult)) {
3095 error(ERR_NONFATAL, "non-constant value given to `%%rep'");
3096 return DIRECTIVE_FOUND;
3097 }
Cyrill Gorcunove091d6e2010-08-09 13:58:22 +04003098 count = reloc_value(evalresult);
3099 if (count >= REP_LIMIT) {
Cyrill Gorcunov71f4f842010-08-09 20:17:17 +04003100 error(ERR_NONFATAL, "`%%rep' value exceeds limit");
Cyrill Gorcunove091d6e2010-08-09 13:58:22 +04003101 count = 0;
3102 } else
3103 count++;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003104 } else {
3105 error(ERR_NONFATAL, "`%%rep' expects a repeat count");
H. Peter Anvinf8ba53e2007-10-11 10:11:57 -07003106 count = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003107 }
3108 free_tlist(origline);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003109 ed = new_ExpDef(EXP_REP);
3110 ed->nolist = nolist;
3111 ed->def_depth = 0;
3112 ed->cur_depth = 1;
3113 ed->max_depth = (count - 1);
3114 ed->ignoring = false;
3115 ed->prev = defining;
3116 defining = ed;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003117 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003118
H. Peter Anvine2c80182005-01-15 22:15:51 +00003119 case PP_ENDREP:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003120 if (defining != NULL) {
3121 if (defining->type == EXP_REP) {
3122 if (defining->def_depth > 0) {
3123 defining->def_depth --;
3124 return NO_DIRECTIVE_FOUND;
3125 }
3126 } else {
3127 return NO_DIRECTIVE_FOUND;
3128 }
3129 }
Keith Kaniosb307a4f2010-11-06 17:41:51 -05003130 if ((defining == NULL) || (defining->type != EXP_REP)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00003131 error(ERR_NONFATAL, "`%%endrep': no matching `%%rep'");
3132 return DIRECTIVE_FOUND;
3133 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003134
H. Peter Anvine2c80182005-01-15 22:15:51 +00003135 /*
3136 * Now we have a "macro" defined - although it has no name
3137 * and we won't be entering it in the hash tables - we must
3138 * push a macro-end marker for it on to istk->expansion.
3139 * After that, it will take care of propagating itself (a
3140 * macro-end marker line for a macro which is really a %rep
3141 * block will cause the macro to be re-expanded, complete
3142 * with another macro-end marker to ensure the process
3143 * continues) until the whole expansion is forcibly removed
3144 * from istk->expansion by a %exitrep.
3145 */
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003146 ed = defining;
3147 defining = ed->prev;
3148 ed->prev = expansions;
3149 expansions = ed;
3150 ei = new_ExpInv(EXP_REP, ed);
3151 ei->current = ed->line;
3152 ei->emitting = ((ed->max_depth > 0) ? true : false);
3153 list->uplevel(ed->nolist ? LIST_MACRO_NOLIST : LIST_MACRO);
3154 ei->prev = istk->expansion;
3155 istk->expansion = ei;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003156 free_tlist(origline);
3157 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003158
H. Peter Anvine2c80182005-01-15 22:15:51 +00003159 case PP_EXITREP:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003160 if (defining != NULL) return NO_DIRECTIVE_FOUND;
3161 /*
3162 * We must search along istk->expansion until we hit a
3163 * rep invocation. Then we disable the emitting state(s)
3164 * between exitrep and endrep.
3165 */
3166 for (ei = istk->expansion; ei != NULL; ei = ei->prev) {
3167 if (ei->type == EXP_REP) {
3168 break;
3169 }
3170 }
3171
3172 if (ei != NULL) {
3173 /*
3174 * Set all invocations leading back to the rep
3175 * invocation to a non-emitting state.
3176 */
3177 for (eei = istk->expansion; eei != ei; eei = eei->prev) {
3178 eei->emitting = false;
3179 }
3180 eei->emitting = false;
3181 eei->current = NULL;
3182 eei->def->cur_depth = eei->def->max_depth;
3183 } else {
3184 error(ERR_NONFATAL, "`%%exitrep' not within `%%rep' block");
3185 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00003186 free_tlist(origline);
3187 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003188
H. Peter Anvine2c80182005-01-15 22:15:51 +00003189 case PP_XDEFINE:
3190 case PP_IXDEFINE:
3191 case PP_DEFINE:
3192 case PP_IDEFINE:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003193 if (defining != NULL) return NO_DIRECTIVE_FOUND;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003194 casesense = (i == PP_DEFINE || i == PP_XDEFINE);
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07003195
H. Peter Anvine2c80182005-01-15 22:15:51 +00003196 tline = tline->next;
3197 skip_white_(tline);
3198 tline = expand_id(tline);
3199 if (!tline || (tline->type != TOK_ID &&
3200 (tline->type != TOK_PREPROC_ID ||
3201 tline->text[1] != '$'))) {
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07003202 error(ERR_NONFATAL, "`%s' expects a macro identifier",
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003203 pp_directives[i]);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003204 free_tlist(origline);
3205 return DIRECTIVE_FOUND;
3206 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003207
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08003208 ctx = get_ctx(tline->text, &mname, false);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003209 last = tline;
3210 param_start = tline = tline->next;
3211 nparam = 0;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003212
H. Peter Anvine2c80182005-01-15 22:15:51 +00003213 /* Expand the macro definition now for %xdefine and %ixdefine */
3214 if ((i == PP_XDEFINE) || (i == PP_IXDEFINE))
3215 tline = expand_smacro(tline);
H. Peter Anvin734b1882002-04-30 21:01:08 +00003216
H. Peter Anvine2c80182005-01-15 22:15:51 +00003217 if (tok_is_(tline, "(")) {
3218 /*
3219 * This macro has parameters.
3220 */
H. Peter Anvin734b1882002-04-30 21:01:08 +00003221
H. Peter Anvine2c80182005-01-15 22:15:51 +00003222 tline = tline->next;
3223 while (1) {
3224 skip_white_(tline);
3225 if (!tline) {
3226 error(ERR_NONFATAL, "parameter identifier expected");
3227 free_tlist(origline);
3228 return DIRECTIVE_FOUND;
3229 }
3230 if (tline->type != TOK_ID) {
3231 error(ERR_NONFATAL,
3232 "`%s': parameter identifier expected",
3233 tline->text);
3234 free_tlist(origline);
3235 return DIRECTIVE_FOUND;
3236 }
3237 tline->type = TOK_SMAC_PARAM + nparam++;
3238 tline = tline->next;
3239 skip_white_(tline);
3240 if (tok_is_(tline, ",")) {
3241 tline = tline->next;
H. Peter Anvinca348b62008-07-23 10:49:26 -04003242 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003243 if (!tok_is_(tline, ")")) {
3244 error(ERR_NONFATAL,
3245 "`)' expected to terminate macro template");
3246 free_tlist(origline);
3247 return DIRECTIVE_FOUND;
3248 }
3249 break;
3250 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00003251 }
3252 last = tline;
3253 tline = tline->next;
3254 }
3255 if (tok_type_(tline, TOK_WHITESPACE))
3256 last = tline, tline = tline->next;
3257 macro_start = NULL;
3258 last->next = NULL;
3259 t = tline;
3260 while (t) {
3261 if (t->type == TOK_ID) {
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04003262 list_for_each(tt, param_start)
H. Peter Anvine2c80182005-01-15 22:15:51 +00003263 if (tt->type >= TOK_SMAC_PARAM &&
3264 !strcmp(tt->text, t->text))
3265 t->type = tt->type;
3266 }
3267 tt = t->next;
3268 t->next = macro_start;
3269 macro_start = t;
3270 t = tt;
3271 }
3272 /*
3273 * Good. We now have a macro name, a parameter count, and a
3274 * token list (in reverse order) for an expansion. We ought
3275 * to be OK just to create an SMacro, store it, and let
3276 * free_tlist have the rest of the line (which we have
3277 * carefully re-terminated after chopping off the expansion
3278 * from the end).
3279 */
H. Peter Anvin4db5a162007-10-11 13:42:09 -07003280 define_smacro(ctx, mname, casesense, nparam, macro_start);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003281 free_tlist(origline);
3282 return DIRECTIVE_FOUND;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003283
H. Peter Anvine2c80182005-01-15 22:15:51 +00003284 case PP_UNDEF:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003285 if (defining != NULL) return NO_DIRECTIVE_FOUND;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003286 tline = tline->next;
3287 skip_white_(tline);
3288 tline = expand_id(tline);
3289 if (!tline || (tline->type != TOK_ID &&
3290 (tline->type != TOK_PREPROC_ID ||
3291 tline->text[1] != '$'))) {
3292 error(ERR_NONFATAL, "`%%undef' expects a macro identifier");
3293 free_tlist(origline);
3294 return DIRECTIVE_FOUND;
3295 }
3296 if (tline->next) {
H. Peter Anvin917a3492008-09-24 09:14:49 -07003297 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +00003298 "trailing garbage after macro name ignored");
3299 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003300
H. Peter Anvine2c80182005-01-15 22:15:51 +00003301 /* Find the context that symbol belongs to */
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08003302 ctx = get_ctx(tline->text, &mname, false);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003303 undef_smacro(ctx, mname);
3304 free_tlist(origline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003305 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003306
H. Peter Anvin9e200162008-06-04 17:23:14 -07003307 case PP_DEFSTR:
3308 case PP_IDEFSTR:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003309 if (defining != NULL) return NO_DIRECTIVE_FOUND;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003310 casesense = (i == PP_DEFSTR);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003311
3312 tline = tline->next;
3313 skip_white_(tline);
3314 tline = expand_id(tline);
3315 if (!tline || (tline->type != TOK_ID &&
3316 (tline->type != TOK_PREPROC_ID ||
3317 tline->text[1] != '$'))) {
3318 error(ERR_NONFATAL, "`%s' expects a macro identifier",
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003319 pp_directives[i]);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003320 free_tlist(origline);
3321 return DIRECTIVE_FOUND;
3322 }
3323
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08003324 ctx = get_ctx(tline->text, &mname, false);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003325 last = tline;
3326 tline = expand_smacro(tline->next);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003327 last->next = NULL;
H. Peter Anvin9e200162008-06-04 17:23:14 -07003328
3329 while (tok_type_(tline, TOK_WHITESPACE))
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003330 tline = delete_Token(tline);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003331
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003332 p = detoken(tline, false);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003333 macro_start = nasm_malloc(sizeof(*macro_start));
3334 macro_start->next = NULL;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003335 macro_start->text = nasm_quote(p, strlen(p));
3336 macro_start->type = TOK_STRING;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003337 macro_start->a.mac = NULL;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003338 nasm_free(p);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003339
3340 /*
3341 * We now have a macro name, an implicit parameter count of
3342 * zero, and a string token to use as an expansion. Create
3343 * and store an SMacro.
3344 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003345 define_smacro(ctx, mname, casesense, 0, macro_start);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003346 free_tlist(origline);
3347 return DIRECTIVE_FOUND;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003348
H. Peter Anvin2f55bda2009-07-14 15:04:04 -04003349 case PP_DEFTOK:
3350 case PP_IDEFTOK:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003351 if (defining != NULL) return NO_DIRECTIVE_FOUND;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003352 casesense = (i == PP_DEFTOK);
3353
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003354 tline = tline->next;
3355 skip_white_(tline);
3356 tline = expand_id(tline);
3357 if (!tline || (tline->type != TOK_ID &&
3358 (tline->type != TOK_PREPROC_ID ||
3359 tline->text[1] != '$'))) {
3360 error(ERR_NONFATAL,
3361 "`%s' expects a macro identifier as first parameter",
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003362 pp_directives[i]);
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003363 free_tlist(origline);
3364 return DIRECTIVE_FOUND;
3365 }
3366 ctx = get_ctx(tline->text, &mname, false);
3367 last = tline;
3368 tline = expand_smacro(tline->next);
3369 last->next = NULL;
3370
3371 t = tline;
3372 while (tok_type_(t, TOK_WHITESPACE))
3373 t = t->next;
3374 /* t should now point to the string */
Cyrill Gorcunov6908e582010-09-06 19:36:15 +04003375 if (!tok_type_(t, TOK_STRING)) {
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003376 error(ERR_NONFATAL,
3377 "`%s` requires string as second parameter",
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003378 pp_directives[i]);
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003379 free_tlist(tline);
3380 free_tlist(origline);
3381 return DIRECTIVE_FOUND;
3382 }
3383
Keith Kaniosb307a4f2010-11-06 17:41:51 -05003384 /*
3385 * Convert the string to a token stream. Note that smacros
3386 * are stored with the token stream reversed, so we have to
3387 * reverse the output of tokenize().
3388 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003389 nasm_unquote_cstr(t->text, i);
H. Peter Anvinb40992c2010-09-15 08:57:21 -07003390 macro_start = reverse_tokens(tokenize(t->text));
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003391
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003392 /*
3393 * We now have a macro name, an implicit parameter count of
3394 * zero, and a numeric token to use as an expansion. Create
3395 * and store an SMacro.
3396 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003397 define_smacro(ctx, mname, casesense, 0, macro_start);
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003398 free_tlist(tline);
3399 free_tlist(origline);
3400 return DIRECTIVE_FOUND;
H. Peter Anvin9e200162008-06-04 17:23:14 -07003401
H. Peter Anvin418ca702008-05-30 10:42:30 -07003402 case PP_PATHSEARCH:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003403 if (defining != NULL) return NO_DIRECTIVE_FOUND;
H. Peter Anvin418ca702008-05-30 10:42:30 -07003404 {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003405 FILE *fp;
3406 StrList *xsl = NULL;
3407 StrList **xst = &xsl;
H. Peter Anvin418ca702008-05-30 10:42:30 -07003408
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003409 casesense = true;
H. Peter Anvin418ca702008-05-30 10:42:30 -07003410
3411 tline = tline->next;
3412 skip_white_(tline);
3413 tline = expand_id(tline);
3414 if (!tline || (tline->type != TOK_ID &&
3415 (tline->type != TOK_PREPROC_ID ||
3416 tline->text[1] != '$'))) {
3417 error(ERR_NONFATAL,
3418 "`%%pathsearch' expects a macro identifier as first parameter");
3419 free_tlist(origline);
3420 return DIRECTIVE_FOUND;
3421 }
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08003422 ctx = get_ctx(tline->text, &mname, false);
H. Peter Anvin418ca702008-05-30 10:42:30 -07003423 last = tline;
3424 tline = expand_smacro(tline->next);
3425 last->next = NULL;
3426
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003427 t = tline;
H. Peter Anvin418ca702008-05-30 10:42:30 -07003428 while (tok_type_(t, TOK_WHITESPACE))
3429 t = t->next;
3430
3431 if (!t || (t->type != TOK_STRING &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003432 t->type != TOK_INTERNAL_STRING)) {
H. Peter Anvin418ca702008-05-30 10:42:30 -07003433 error(ERR_NONFATAL, "`%%pathsearch' expects a file name");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003434 free_tlist(tline);
H. Peter Anvin418ca702008-05-30 10:42:30 -07003435 free_tlist(origline);
3436 return DIRECTIVE_FOUND; /* but we did _something_ */
3437 }
3438 if (t->next)
H. Peter Anvin917a3492008-09-24 09:14:49 -07003439 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvin418ca702008-05-30 10:42:30 -07003440 "trailing garbage after `%%pathsearch' ignored");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003441 p = t->text;
H. Peter Anvin427cc912008-06-01 21:43:03 -07003442 if (t->type != TOK_INTERNAL_STRING)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003443 nasm_unquote(p, NULL);
H. Peter Anvin418ca702008-05-30 10:42:30 -07003444
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003445 fp = inc_fopen(p, &xsl, &xst, true);
3446 if (fp) {
3447 p = xsl->str;
3448 fclose(fp); /* Don't actually care about the file */
3449 }
H. Peter Anvin418ca702008-05-30 10:42:30 -07003450 macro_start = nasm_malloc(sizeof(*macro_start));
3451 macro_start->next = NULL;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003452 macro_start->text = nasm_quote(p, strlen(p));
3453 macro_start->type = TOK_STRING;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003454 macro_start->a.mac = NULL;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003455 if (xsl)
3456 nasm_free(xsl);
H. Peter Anvin418ca702008-05-30 10:42:30 -07003457
3458 /*
3459 * We now have a macro name, an implicit parameter count of
3460 * zero, and a string token to use as an expansion. Create
3461 * and store an SMacro.
3462 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003463 define_smacro(ctx, mname, casesense, 0, macro_start);
3464 free_tlist(tline);
H. Peter Anvin418ca702008-05-30 10:42:30 -07003465 free_tlist(origline);
3466 return DIRECTIVE_FOUND;
3467 }
3468
H. Peter Anvine2c80182005-01-15 22:15:51 +00003469 case PP_STRLEN:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003470 if (defining != NULL) return NO_DIRECTIVE_FOUND;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003471 casesense = true;
H. Peter Anvin70653092007-10-19 14:42:29 -07003472
H. Peter Anvine2c80182005-01-15 22:15:51 +00003473 tline = tline->next;
3474 skip_white_(tline);
3475 tline = expand_id(tline);
3476 if (!tline || (tline->type != TOK_ID &&
3477 (tline->type != TOK_PREPROC_ID ||
3478 tline->text[1] != '$'))) {
3479 error(ERR_NONFATAL,
3480 "`%%strlen' expects a macro identifier as first parameter");
3481 free_tlist(origline);
3482 return DIRECTIVE_FOUND;
3483 }
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08003484 ctx = get_ctx(tline->text, &mname, false);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003485 last = tline;
3486 tline = expand_smacro(tline->next);
3487 last->next = NULL;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003488
H. Peter Anvine2c80182005-01-15 22:15:51 +00003489 t = tline;
3490 while (tok_type_(t, TOK_WHITESPACE))
3491 t = t->next;
3492 /* t should now point to the string */
Cyrill Gorcunov4e1d5ab2010-07-23 18:51:51 +04003493 if (!tok_type_(t, TOK_STRING)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00003494 error(ERR_NONFATAL,
3495 "`%%strlen` requires string as second parameter");
3496 free_tlist(tline);
3497 free_tlist(origline);
3498 return DIRECTIVE_FOUND;
3499 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003500
H. Peter Anvine2c80182005-01-15 22:15:51 +00003501 macro_start = nasm_malloc(sizeof(*macro_start));
3502 macro_start->next = NULL;
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07003503 make_tok_num(macro_start, nasm_unquote(t->text, NULL));
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003504 macro_start->a.mac = NULL;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003505
H. Peter Anvine2c80182005-01-15 22:15:51 +00003506 /*
3507 * We now have a macro name, an implicit parameter count of
3508 * zero, and a numeric token to use as an expansion. Create
3509 * and store an SMacro.
3510 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003511 define_smacro(ctx, mname, casesense, 0, macro_start);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003512 free_tlist(tline);
3513 free_tlist(origline);
3514 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003515
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003516 case PP_STRCAT:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003517 if (defining != NULL) return NO_DIRECTIVE_FOUND;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003518 casesense = true;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003519
3520 tline = tline->next;
3521 skip_white_(tline);
3522 tline = expand_id(tline);
3523 if (!tline || (tline->type != TOK_ID &&
3524 (tline->type != TOK_PREPROC_ID ||
3525 tline->text[1] != '$'))) {
3526 error(ERR_NONFATAL,
3527 "`%%strcat' expects a macro identifier as first parameter");
3528 free_tlist(origline);
3529 return DIRECTIVE_FOUND;
3530 }
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08003531 ctx = get_ctx(tline->text, &mname, false);
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003532 last = tline;
3533 tline = expand_smacro(tline->next);
3534 last->next = NULL;
3535
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003536 len = 0;
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04003537 list_for_each(t, tline) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003538 switch (t->type) {
3539 case TOK_WHITESPACE:
3540 break;
3541 case TOK_STRING:
3542 len += t->a.len = nasm_unquote(t->text, NULL);
3543 break;
3544 case TOK_OTHER:
3545 if (!strcmp(t->text, ",")) /* permit comma separators */
3546 break;
3547 /* else fall through */
3548 default:
3549 error(ERR_NONFATAL,
3550 "non-string passed to `%%strcat' (%d)", t->type);
3551 free_tlist(tline);
3552 free_tlist(origline);
3553 return DIRECTIVE_FOUND;
3554 }
3555 }
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003556
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003557 p = pp = nasm_malloc(len);
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04003558 list_for_each(t, tline) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003559 if (t->type == TOK_STRING) {
3560 memcpy(p, t->text, t->a.len);
3561 p += t->a.len;
3562 }
3563 }
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003564
3565 /*
3566 * We now have a macro name, an implicit parameter count of
3567 * zero, and a numeric token to use as an expansion. Create
3568 * and store an SMacro.
3569 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003570 macro_start = new_Token(NULL, TOK_STRING, NULL, 0);
3571 macro_start->text = nasm_quote(pp, len);
3572 nasm_free(pp);
3573 define_smacro(ctx, mname, casesense, 0, macro_start);
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003574 free_tlist(tline);
3575 free_tlist(origline);
3576 return DIRECTIVE_FOUND;
3577
H. Peter Anvine2c80182005-01-15 22:15:51 +00003578 case PP_SUBSTR:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003579 if (defining != NULL) return NO_DIRECTIVE_FOUND;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003580 {
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003581 int64_t start, count;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003582 size_t len;
H. Peter Anvind2456592008-06-19 15:04:18 -07003583
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003584 casesense = true;
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07003585
H. Peter Anvine2c80182005-01-15 22:15:51 +00003586 tline = tline->next;
3587 skip_white_(tline);
3588 tline = expand_id(tline);
3589 if (!tline || (tline->type != TOK_ID &&
3590 (tline->type != TOK_PREPROC_ID ||
3591 tline->text[1] != '$'))) {
3592 error(ERR_NONFATAL,
3593 "`%%substr' expects a macro identifier as first parameter");
3594 free_tlist(origline);
3595 return DIRECTIVE_FOUND;
3596 }
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08003597 ctx = get_ctx(tline->text, &mname, false);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003598 last = tline;
3599 tline = expand_smacro(tline->next);
3600 last->next = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003601
Cyrill Gorcunov35519d62010-09-06 23:49:52 +04003602 if (tline) /* skip expanded id */
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003603 t = tline->next;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003604 while (tok_type_(t, TOK_WHITESPACE))
3605 t = t->next;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003606
H. Peter Anvine2c80182005-01-15 22:15:51 +00003607 /* t should now point to the string */
Cyrill Gorcunov35519d62010-09-06 23:49:52 +04003608 if (!tok_type_(t, TOK_STRING)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00003609 error(ERR_NONFATAL,
3610 "`%%substr` requires string as second parameter");
3611 free_tlist(tline);
3612 free_tlist(origline);
3613 return DIRECTIVE_FOUND;
3614 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003615
H. Peter Anvine2c80182005-01-15 22:15:51 +00003616 tt = t->next;
3617 tptr = &tt;
3618 tokval.t_type = TOKEN_INVALID;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003619 evalresult = evaluate(ppscan, tptr, &tokval, NULL,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003620 pass, error, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003621 if (!evalresult) {
3622 free_tlist(tline);
3623 free_tlist(origline);
3624 return DIRECTIVE_FOUND;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003625 } else if (!is_simple(evalresult)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00003626 error(ERR_NONFATAL, "non-constant value given to `%%substr`");
3627 free_tlist(tline);
3628 free_tlist(origline);
3629 return DIRECTIVE_FOUND;
3630 }
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003631 start = evalresult->value - 1;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003632
3633 while (tok_type_(tt, TOK_WHITESPACE))
3634 tt = tt->next;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003635 if (!tt) {
Keith Kaniosb307a4f2010-11-06 17:41:51 -05003636 count = 1; /* Backwards compatibility: one character */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003637 } else {
3638 tokval.t_type = TOKEN_INVALID;
3639 evalresult = evaluate(ppscan, tptr, &tokval, NULL,
3640 pass, error, NULL);
3641 if (!evalresult) {
3642 free_tlist(tline);
3643 free_tlist(origline);
3644 return DIRECTIVE_FOUND;
3645 } else if (!is_simple(evalresult)) {
3646 error(ERR_NONFATAL, "non-constant value given to `%%substr`");
3647 free_tlist(tline);
3648 free_tlist(origline);
3649 return DIRECTIVE_FOUND;
3650 }
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003651 count = evalresult->value;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003652 }
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003653
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003654 len = nasm_unquote(t->text, NULL);
Cyrill Gorcunovcff031e2010-09-07 20:31:11 +04003655 /* make start and count being in range */
3656 if (start < 0)
3657 start = 0;
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003658 if (count < 0)
3659 count = len + count + 1 - start;
3660 if (start + count > (int64_t)len)
Cyrill Gorcunovcff031e2010-09-07 20:31:11 +04003661 count = len - start;
3662 if (!len || count < 0 || start >=(int64_t)len)
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003663 start = -1, count = 0; /* empty string */
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003664
H. Peter Anvine2c80182005-01-15 22:15:51 +00003665 macro_start = nasm_malloc(sizeof(*macro_start));
3666 macro_start->next = NULL;
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003667 macro_start->text = nasm_quote((start < 0) ? "" : t->text + start, count);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003668 macro_start->type = TOK_STRING;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003669 macro_start->a.mac = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003670
H. Peter Anvine2c80182005-01-15 22:15:51 +00003671 /*
3672 * We now have a macro name, an implicit parameter count of
3673 * zero, and a numeric token to use as an expansion. Create
3674 * and store an SMacro.
3675 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003676 define_smacro(ctx, mname, casesense, 0, macro_start);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003677 free_tlist(tline);
3678 free_tlist(origline);
3679 return DIRECTIVE_FOUND;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003680 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003681
H. Peter Anvine2c80182005-01-15 22:15:51 +00003682 case PP_ASSIGN:
3683 case PP_IASSIGN:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003684 if (defining != NULL) return NO_DIRECTIVE_FOUND;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003685 casesense = (i == PP_ASSIGN);
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07003686
H. Peter Anvine2c80182005-01-15 22:15:51 +00003687 tline = tline->next;
3688 skip_white_(tline);
3689 tline = expand_id(tline);
3690 if (!tline || (tline->type != TOK_ID &&
3691 (tline->type != TOK_PREPROC_ID ||
3692 tline->text[1] != '$'))) {
3693 error(ERR_NONFATAL,
3694 "`%%%sassign' expects a macro identifier",
3695 (i == PP_IASSIGN ? "i" : ""));
3696 free_tlist(origline);
3697 return DIRECTIVE_FOUND;
3698 }
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08003699 ctx = get_ctx(tline->text, &mname, false);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003700 last = tline;
3701 tline = expand_smacro(tline->next);
3702 last->next = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003703
H. Peter Anvine2c80182005-01-15 22:15:51 +00003704 t = tline;
3705 tptr = &t;
3706 tokval.t_type = TOKEN_INVALID;
3707 evalresult =
3708 evaluate(ppscan, tptr, &tokval, NULL, pass, error, NULL);
3709 free_tlist(tline);
3710 if (!evalresult) {
3711 free_tlist(origline);
3712 return DIRECTIVE_FOUND;
3713 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003714
H. Peter Anvine2c80182005-01-15 22:15:51 +00003715 if (tokval.t_type)
H. Peter Anvin917a3492008-09-24 09:14:49 -07003716 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +00003717 "trailing garbage after expression ignored");
H. Peter Anvin734b1882002-04-30 21:01:08 +00003718
H. Peter Anvine2c80182005-01-15 22:15:51 +00003719 if (!is_simple(evalresult)) {
3720 error(ERR_NONFATAL,
3721 "non-constant value given to `%%%sassign'",
3722 (i == PP_IASSIGN ? "i" : ""));
3723 free_tlist(origline);
3724 return DIRECTIVE_FOUND;
3725 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003726
H. Peter Anvine2c80182005-01-15 22:15:51 +00003727 macro_start = nasm_malloc(sizeof(*macro_start));
3728 macro_start->next = NULL;
3729 make_tok_num(macro_start, reloc_value(evalresult));
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003730 macro_start->a.mac = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003731
H. Peter Anvine2c80182005-01-15 22:15:51 +00003732 /*
3733 * We now have a macro name, an implicit parameter count of
3734 * zero, and a numeric token to use as an expansion. Create
3735 * and store an SMacro.
3736 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003737 define_smacro(ctx, mname, casesense, 0, macro_start);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003738 free_tlist(origline);
3739 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003740
H. Peter Anvine2c80182005-01-15 22:15:51 +00003741 case PP_LINE:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003742 if (defining != NULL) return NO_DIRECTIVE_FOUND;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003743 /*
3744 * Syntax is `%line nnn[+mmm] [filename]'
3745 */
3746 tline = tline->next;
3747 skip_white_(tline);
3748 if (!tok_type_(tline, TOK_NUMBER)) {
3749 error(ERR_NONFATAL, "`%%line' expects line number");
3750 free_tlist(origline);
3751 return DIRECTIVE_FOUND;
3752 }
H. Peter Anvin70055962007-10-11 00:05:31 -07003753 k = readnum(tline->text, &err);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003754 m = 1;
3755 tline = tline->next;
3756 if (tok_is_(tline, "+")) {
3757 tline = tline->next;
3758 if (!tok_type_(tline, TOK_NUMBER)) {
3759 error(ERR_NONFATAL, "`%%line' expects line increment");
3760 free_tlist(origline);
3761 return DIRECTIVE_FOUND;
3762 }
H. Peter Anvin70055962007-10-11 00:05:31 -07003763 m = readnum(tline->text, &err);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003764 tline = tline->next;
3765 }
3766 skip_white_(tline);
3767 src_set_linnum(k);
3768 istk->lineinc = m;
3769 if (tline) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07003770 nasm_free(src_set_fname(detoken(tline, false)));
H. Peter Anvine2c80182005-01-15 22:15:51 +00003771 }
3772 free_tlist(origline);
3773 return DIRECTIVE_FOUND;
Keith Kaniosb307a4f2010-11-06 17:41:51 -05003774
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003775 case PP_WHILE:
3776 if (defining != NULL) {
3777 if (defining->type == EXP_WHILE) {
3778 defining->def_depth ++;
3779 }
3780 return NO_DIRECTIVE_FOUND;
3781 }
3782 l = NULL;
3783 if ((istk->expansion != NULL) &&
3784 (istk->expansion->emitting == false)) {
3785 j = COND_NEVER;
3786 } else {
3787 l = new_Line();
3788 l->first = copy_Token(tline->next);
3789 j = if_condition(tline->next, i);
3790 tline->next = NULL; /* it got freed */
3791 j = (((j < 0) ? COND_NEVER : j) ? COND_IF_TRUE : COND_IF_FALSE);
3792 }
3793 ed = new_ExpDef(EXP_WHILE);
3794 ed->state = j;
3795 ed->cur_depth = 1;
3796 ed->max_depth = DEADMAN_LIMIT;
3797 ed->ignoring = ((ed->state == COND_IF_TRUE) ? false : true);
3798 if (ed->ignoring == false) {
3799 ed->line = l;
3800 ed->last = l;
3801 } else if (l != NULL) {
3802 delete_Token(l->first);
3803 nasm_free(l);
3804 l = NULL;
3805 }
3806 ed->prev = defining;
3807 defining = ed;
3808 free_tlist(origline);
3809 return DIRECTIVE_FOUND;
3810
3811 case PP_ENDWHILE:
3812 if (defining != NULL) {
3813 if (defining->type == EXP_WHILE) {
3814 if (defining->def_depth > 0) {
3815 defining->def_depth --;
3816 return NO_DIRECTIVE_FOUND;
3817 }
3818 } else {
3819 return NO_DIRECTIVE_FOUND;
3820 }
3821 }
3822 if (tline->next != NULL) {
3823 error_precond(ERR_WARNING|ERR_PASS1,
3824 "trailing garbage after `%%endwhile' ignored");
3825 }
3826 if ((defining == NULL) || (defining->type != EXP_WHILE)) {
3827 error(ERR_NONFATAL, "`%%endwhile': no matching `%%while'");
3828 return DIRECTIVE_FOUND;
3829 }
3830 ed = defining;
3831 defining = ed->prev;
3832 if (ed->ignoring == false) {
3833 ed->prev = expansions;
3834 expansions = ed;
3835 ei = new_ExpInv(EXP_WHILE, ed);
3836 ei->current = ed->line->next;
3837 ei->emitting = true;
3838 ei->prev = istk->expansion;
3839 istk->expansion = ei;
3840 } else {
3841 nasm_free(ed);
3842 }
3843 free_tlist(origline);
3844 return DIRECTIVE_FOUND;
3845
3846 case PP_EXITWHILE:
3847 if (defining != NULL) return NO_DIRECTIVE_FOUND;
3848 /*
3849 * We must search along istk->expansion until we hit a
3850 * while invocation. Then we disable the emitting state(s)
3851 * between exitwhile and endwhile.
3852 */
3853 for (ei = istk->expansion; ei != NULL; ei = ei->prev) {
3854 if (ei->type == EXP_WHILE) {
3855 break;
3856 }
3857 }
3858
3859 if (ei != NULL) {
3860 /*
3861 * Set all invocations leading back to the while
3862 * invocation to a non-emitting state.
3863 */
3864 for (eei = istk->expansion; eei != ei; eei = eei->prev) {
3865 eei->emitting = false;
3866 }
3867 eei->emitting = false;
3868 eei->current = NULL;
3869 eei->def->cur_depth = eei->def->max_depth;
3870 } else {
3871 error(ERR_NONFATAL, "`%%exitwhile' not within `%%while' block");
3872 }
3873 free_tlist(origline);
3874 return DIRECTIVE_FOUND;
3875
3876 case PP_COMMENT:
3877 if (defining != NULL) {
3878 if (defining->type == EXP_COMMENT) {
3879 defining->def_depth ++;
3880 }
3881 return NO_DIRECTIVE_FOUND;
3882 }
3883 ed = new_ExpDef(EXP_COMMENT);
3884 ed->ignoring = true;
3885 ed->prev = defining;
3886 defining = ed;
3887 free_tlist(origline);
3888 return DIRECTIVE_FOUND;
3889
3890 case PP_ENDCOMMENT:
3891 if (defining != NULL) {
3892 if (defining->type == EXP_COMMENT) {
3893 if (defining->def_depth > 0) {
3894 defining->def_depth --;
3895 return NO_DIRECTIVE_FOUND;
3896 }
3897 } else {
3898 return NO_DIRECTIVE_FOUND;
3899 }
3900 }
3901 if ((defining == NULL) || (defining->type != EXP_COMMENT)) {
3902 error(ERR_NONFATAL, "`%%endcomment': no matching `%%comment'");
3903 return DIRECTIVE_FOUND;
3904 }
3905 ed = defining;
3906 defining = ed->prev;
3907 nasm_free(ed);
3908 free_tlist(origline);
3909 return DIRECTIVE_FOUND;
3910
3911 case PP_FINAL:
3912 if (defining != NULL) return NO_DIRECTIVE_FOUND;
3913 if (in_final != false) {
3914 error(ERR_FATAL, "`%%final' cannot be used recursively");
3915 }
3916 tline = tline->next;
3917 skip_white_(tline);
3918 if (tline == NULL) {
3919 error(ERR_NONFATAL, "`%%final' expects at least one parameter");
3920 } else {
3921 l = new_Line();
3922 l->first = copy_Token(tline);
3923 l->next = finals;
3924 finals = l;
3925 }
3926 free_tlist(origline);
3927 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003928
H. Peter Anvine2c80182005-01-15 22:15:51 +00003929 default:
3930 error(ERR_FATAL,
3931 "preprocessor directive `%s' not yet implemented",
H. Peter Anvin4169a472007-09-12 01:29:43 +00003932 pp_directives[i]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003933 return DIRECTIVE_FOUND;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003934 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003935}
3936
3937/*
H. Peter Anvin76690a12002-04-30 20:52:49 +00003938 * Ensure that a macro parameter contains a condition code and
3939 * nothing else. Return the condition code index if so, or -1
3940 * otherwise.
3941 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003942static int find_cc(Token * t)
H. Peter Anvineba20a72002-04-30 20:53:55 +00003943{
H. Peter Anvin76690a12002-04-30 20:52:49 +00003944 Token *tt;
3945 int i, j, k, m;
3946
H. Peter Anvin25a99342007-09-22 17:45:45 -07003947 if (!t)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003948 return -1; /* Probably a %+ without a space */
H. Peter Anvin25a99342007-09-22 17:45:45 -07003949
H. Peter Anvineba20a72002-04-30 20:53:55 +00003950 skip_white_(t);
H. Peter Anvin76690a12002-04-30 20:52:49 +00003951 if (t->type != TOK_ID)
H. Peter Anvine2c80182005-01-15 22:15:51 +00003952 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003953 tt = t->next;
H. Peter Anvineba20a72002-04-30 20:53:55 +00003954 skip_white_(tt);
H. Peter Anvin76690a12002-04-30 20:52:49 +00003955 if (tt && (tt->type != TOK_OTHER || strcmp(tt->text, ",")))
H. Peter Anvine2c80182005-01-15 22:15:51 +00003956 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003957
3958 i = -1;
Cyrill Gorcunova7319242010-06-03 22:04:36 +04003959 j = ARRAY_SIZE(conditions);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003960 while (j - i > 1) {
3961 k = (j + i) / 2;
3962 m = nasm_stricmp(t->text, conditions[k]);
3963 if (m == 0) {
3964 i = k;
3965 j = -2;
3966 break;
3967 } else if (m < 0) {
3968 j = k;
3969 } else
3970 i = k;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003971 }
3972 if (j != -2)
H. Peter Anvine2c80182005-01-15 22:15:51 +00003973 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003974 return i;
3975}
3976
Cyrill Gorcunov575d4282010-10-06 00:25:55 +04003977static bool paste_tokens(Token **head, const struct tokseq_match *m,
3978 int mnum, bool handle_paste_tokens)
H. Peter Anvind784a082009-04-20 14:01:18 -07003979{
3980 Token **tail, *t, *tt;
3981 Token **paste_head;
3982 bool did_paste = false;
3983 char *tmp;
Cyrill Gorcunov575d4282010-10-06 00:25:55 +04003984 int i;
H. Peter Anvind784a082009-04-20 14:01:18 -07003985
3986 /* Now handle token pasting... */
3987 paste_head = NULL;
3988 tail = head;
3989 while ((t = *tail) && (tt = t->next)) {
3990 switch (t->type) {
3991 case TOK_WHITESPACE:
3992 if (tt->type == TOK_WHITESPACE) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003993 /* Zap adjacent whitespace tokens */
H. Peter Anvind784a082009-04-20 14:01:18 -07003994 t->next = delete_Token(tt);
3995 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003996 /* Do not advance paste_head here */
3997 tail = &t->next;
3998 }
H. Peter Anvind784a082009-04-20 14:01:18 -07003999 break;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004000 case TOK_PASTE: /* %+ */
4001 if (handle_paste_tokens) {
4002 /* Zap %+ and whitespace tokens to the right */
4003 while (t && (t->type == TOK_WHITESPACE ||
4004 t->type == TOK_PASTE))
4005 t = *tail = delete_Token(t);
4006 if (!paste_head || !t)
4007 break; /* Nothing to paste with */
4008 tail = paste_head;
4009 t = *tail;
4010 tt = t->next;
4011 while (tok_type_(tt, TOK_WHITESPACE))
4012 tt = t->next = delete_Token(tt);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004013 if (tt) {
4014 tmp = nasm_strcat(t->text, tt->text);
4015 delete_Token(t);
4016 tt = delete_Token(tt);
4017 t = *tail = tokenize(tmp);
4018 nasm_free(tmp);
4019 while (t->next) {
4020 tail = &t->next;
4021 t = t->next;
4022 }
4023 t->next = tt; /* Attach the remaining token chain */
4024 did_paste = true;
4025 }
4026 paste_head = tail;
4027 tail = &t->next;
4028 break;
4029 }
4030 /* else fall through */
4031 default:
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +04004032 /*
4033 * Concatenation of tokens might look nontrivial
4034 * but in real it's pretty simple -- the caller
Cyrill Gorcunov575d4282010-10-06 00:25:55 +04004035 * prepares the masks of token types to be concatenated
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +04004036 * and we simply find matched sequences and slip
4037 * them together
4038 */
Cyrill Gorcunov575d4282010-10-06 00:25:55 +04004039 for (i = 0; i < mnum; i++) {
4040 if (PP_CONCAT_MASK(t->type) & m[i].mask_head) {
4041 size_t len = 0;
4042 char *tmp, *p;
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +04004043
Cyrill Gorcunov575d4282010-10-06 00:25:55 +04004044 while (tt && (PP_CONCAT_MASK(tt->type) & m[i].mask_tail)) {
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004045 len += strlen(tt->text);
4046 tt = tt->next;
Cyrill Gorcunov575d4282010-10-06 00:25:55 +04004047 }
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +04004048
Cyrill Gorcunov575d4282010-10-06 00:25:55 +04004049 /*
4050 * Now tt points to the first token after
4051 * the potential paste area...
4052 */
4053 if (tt != t->next) {
4054 /* We have at least two tokens... */
4055 len += strlen(t->text);
4056 p = tmp = nasm_malloc(len+1);
4057 while (t != tt) {
4058 strcpy(p, t->text);
4059 p = strchr(p, '\0');
4060 t = delete_Token(t);
4061 }
4062 t = *tail = tokenize(tmp);
4063 nasm_free(tmp);
4064 while (t->next) {
4065 tail = &t->next;
4066 t = t->next;
4067 }
4068 t->next = tt; /* Attach the remaining token chain */
4069 did_paste = true;
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +04004070 }
Cyrill Gorcunov575d4282010-10-06 00:25:55 +04004071 paste_head = tail;
4072 tail = &t->next;
4073 break;
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +04004074 }
Cyrill Gorcunov575d4282010-10-06 00:25:55 +04004075 }
4076 if (i >= mnum) { /* no match */
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +04004077 tail = &t->next;
4078 if (!tok_type_(t->next, TOK_WHITESPACE))
4079 paste_head = tail;
4080 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004081 break;
H. Peter Anvind784a082009-04-20 14:01:18 -07004082 }
4083 }
4084 return did_paste;
4085}
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004086
4087/*
4088 * expands to a list of tokens from %{x:y}
4089 */
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004090static Token *expand_mmac_params_range(ExpInv *ei, Token *tline, Token ***last)
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004091{
4092 Token *t = tline, **tt, *tm, *head;
4093 char *pos;
4094 int fst, lst, j, i;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004095
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004096 pos = strchr(tline->text, ':');
4097 nasm_assert(pos);
4098
4099 lst = atoi(pos + 1);
4100 fst = atoi(tline->text + 1);
4101
4102 /*
4103 * only macros params are accounted so
4104 * if someone passes %0 -- we reject such
4105 * value(s)
4106 */
4107 if (lst == 0 || fst == 0)
4108 goto err;
4109
4110 /* the values should be sane */
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004111 if ((fst > (int)ei->nparam || fst < (-(int)ei->nparam)) ||
4112 (lst > (int)ei->nparam || lst < (-(int)ei->nparam)))
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004113 goto err;
4114
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004115 fst = fst < 0 ? fst + (int)ei->nparam + 1: fst;
4116 lst = lst < 0 ? lst + (int)ei->nparam + 1: lst;
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004117
4118 /* counted from zero */
4119 fst--, lst--;
4120
4121 /*
4122 * it will be at least one token
4123 */
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004124 tm = ei->params[(fst + ei->rotate) % ei->nparam];
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004125 t = new_Token(NULL, tm->type, tm->text, 0);
4126 head = t, tt = &t->next;
4127 if (fst < lst) {
4128 for (i = fst + 1; i <= lst; i++) {
4129 t = new_Token(NULL, TOK_OTHER, ",", 0);
4130 *tt = t, tt = &t->next;
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004131 j = (i + ei->rotate) % ei->nparam;
4132 tm = ei->params[j];
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004133 t = new_Token(NULL, tm->type, tm->text, 0);
4134 *tt = t, tt = &t->next;
4135 }
4136 } else {
4137 for (i = fst - 1; i >= lst; i--) {
4138 t = new_Token(NULL, TOK_OTHER, ",", 0);
4139 *tt = t, tt = &t->next;
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004140 j = (i + ei->rotate) % ei->nparam;
4141 tm = ei->params[j];
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004142 t = new_Token(NULL, tm->type, tm->text, 0);
4143 *tt = t, tt = &t->next;
4144 }
4145 }
4146
4147 *last = tt;
4148 return head;
4149
4150err:
4151 error(ERR_NONFATAL, "`%%{%s}': macro parameters out of range",
4152 &tline->text[1]);
4153 return tline;
4154}
4155
H. Peter Anvin76690a12002-04-30 20:52:49 +00004156/*
4157 * Expand MMacro-local things: parameter references (%0, %n, %+n,
H. Peter Anvin67c63722008-10-26 23:49:00 -07004158 * %-n) and MMacro-local identifiers (%%foo) as well as
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004159 * macro indirection (%[...]) and range (%{..:..}).
H. Peter Anvin76690a12002-04-30 20:52:49 +00004160 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004161static Token *expand_mmac_params(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004162{
H. Peter Anvin734b1882002-04-30 21:01:08 +00004163 Token *t, *tt, **tail, *thead;
H. Peter Anvin6125b622009-04-08 14:02:25 -07004164 bool changed = false;
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004165 char *pos;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004166
4167 tail = &thead;
4168 thead = NULL;
4169
H. Peter Anvine2c80182005-01-15 22:15:51 +00004170 while (tline) {
4171 if (tline->type == TOK_PREPROC_ID &&
Cyrill Gorcunovca611192010-06-04 09:22:12 +04004172 (((tline->text[1] == '+' || tline->text[1] == '-') && tline->text[2]) ||
4173 (tline->text[1] >= '0' && tline->text[1] <= '9') ||
4174 tline->text[1] == '%')) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004175 char *text = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004176 int type = 0, cc; /* type = 0 to placate optimisers */
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004177 char tmpbuf[30];
H. Peter Anvin25a99342007-09-22 17:45:45 -07004178 unsigned int n;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004179 int i;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004180 ExpInv *ei;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004181
H. Peter Anvine2c80182005-01-15 22:15:51 +00004182 t = tline;
4183 tline = tline->next;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004184
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004185 for (ei = istk->expansion; ei != NULL; ei = ei->prev) {
4186 if (ei->type == EXP_MMACRO) {
4187 break;
4188 }
4189 }
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004190 if (ei == NULL) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00004191 error(ERR_NONFATAL, "`%s': not in a macro call", t->text);
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004192 } else {
4193 pos = strchr(t->text, ':');
4194 if (!pos) {
4195 switch (t->text[1]) {
4196 /*
4197 * We have to make a substitution of one of the
4198 * forms %1, %-1, %+1, %%foo, %0.
4199 */
4200 case '0':
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004201 if ((strlen(t->text) > 2) && (t->text[2] == '0')) {
4202 type = TOK_ID;
4203 text = nasm_strdup(ei->label_text);
4204 } else {
4205 type = TOK_NUMBER;
4206 snprintf(tmpbuf, sizeof(tmpbuf), "%d", ei->nparam);
4207 text = nasm_strdup(tmpbuf);
4208 }
4209 break;
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004210 case '%':
H. Peter Anvine2c80182005-01-15 22:15:51 +00004211 type = TOK_ID;
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004212 snprintf(tmpbuf, sizeof(tmpbuf), "..@%"PRIu64".",
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004213 ei->unique);
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004214 text = nasm_strcat(tmpbuf, t->text + 2);
4215 break;
4216 case '-':
4217 n = atoi(t->text + 2) - 1;
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004218 if (n >= ei->nparam)
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004219 tt = NULL;
4220 else {
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004221 if (ei->nparam > 1)
4222 n = (n + ei->rotate) % ei->nparam;
4223 tt = ei->params[n];
H. Peter Anvine2c80182005-01-15 22:15:51 +00004224 }
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004225 cc = find_cc(tt);
4226 if (cc == -1) {
4227 error(ERR_NONFATAL,
4228 "macro parameter %d is not a condition code",
4229 n + 1);
4230 text = NULL;
4231 } else {
4232 type = TOK_ID;
4233 if (inverse_ccs[cc] == -1) {
4234 error(ERR_NONFATAL,
4235 "condition code `%s' is not invertible",
4236 conditions[cc]);
4237 text = NULL;
4238 } else
4239 text = nasm_strdup(conditions[inverse_ccs[cc]]);
4240 }
4241 break;
4242 case '+':
4243 n = atoi(t->text + 2) - 1;
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004244 if (n >= ei->nparam)
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004245 tt = NULL;
4246 else {
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004247 if (ei->nparam > 1)
4248 n = (n + ei->rotate) % ei->nparam;
4249 tt = ei->params[n];
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004250 }
4251 cc = find_cc(tt);
4252 if (cc == -1) {
4253 error(ERR_NONFATAL,
4254 "macro parameter %d is not a condition code",
4255 n + 1);
4256 text = NULL;
4257 } else {
4258 type = TOK_ID;
4259 text = nasm_strdup(conditions[cc]);
4260 }
4261 break;
4262 default:
4263 n = atoi(t->text + 1) - 1;
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004264 if (n >= ei->nparam)
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004265 tt = NULL;
4266 else {
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004267 if (ei->nparam > 1)
4268 n = (n + ei->rotate) % ei->nparam;
4269 tt = ei->params[n];
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004270 }
4271 if (tt) {
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004272 for (i = 0; i < ei->paramlen[n]; i++) {
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004273 *tail = new_Token(NULL, tt->type, tt->text, 0);
4274 tail = &(*tail)->next;
4275 tt = tt->next;
4276 }
4277 }
4278 text = NULL; /* we've done it here */
4279 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004280 }
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004281 } else {
4282 /*
4283 * seems we have a parameters range here
4284 */
4285 Token *head, **last;
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004286 head = expand_mmac_params_range(ei, t, &last);
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004287 if (head != t) {
4288 *tail = head;
4289 *last = tline;
4290 tline = head;
4291 text = NULL;
4292 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00004293 }
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004294 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00004295 if (!text) {
4296 delete_Token(t);
4297 } else {
4298 *tail = t;
4299 tail = &t->next;
4300 t->type = type;
4301 nasm_free(t->text);
4302 t->text = text;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004303 t->a.mac = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004304 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004305 changed = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004306 continue;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004307 } else if (tline->type == TOK_INDIRECT) {
4308 t = tline;
4309 tline = tline->next;
4310 tt = tokenize(t->text);
4311 tt = expand_mmac_params(tt);
4312 tt = expand_smacro(tt);
4313 *tail = tt;
4314 while (tt) {
4315 tt->a.mac = NULL; /* Necessary? */
4316 tail = &tt->next;
4317 tt = tt->next;
4318 }
4319 delete_Token(t);
4320 changed = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004321 } else {
4322 t = *tail = tline;
4323 tline = tline->next;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004324 t->a.mac = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004325 tail = &t->next;
4326 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00004327 }
H. Peter Anvineba20a72002-04-30 20:53:55 +00004328 *tail = NULL;
H. Peter Anvin67c63722008-10-26 23:49:00 -07004329
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +04004330 if (changed) {
Cyrill Gorcunov575d4282010-10-06 00:25:55 +04004331 const struct tokseq_match t[] = {
4332 {
4333 PP_CONCAT_MASK(TOK_ID) |
4334 PP_CONCAT_MASK(TOK_FLOAT), /* head */
4335 PP_CONCAT_MASK(TOK_ID) |
4336 PP_CONCAT_MASK(TOK_NUMBER) |
4337 PP_CONCAT_MASK(TOK_FLOAT) |
4338 PP_CONCAT_MASK(TOK_OTHER) /* tail */
4339 },
4340 {
4341 PP_CONCAT_MASK(TOK_NUMBER), /* head */
4342 PP_CONCAT_MASK(TOK_NUMBER) /* tail */
4343 }
4344 };
4345 paste_tokens(&thead, t, ARRAY_SIZE(t), false);
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +04004346 }
H. Peter Anvin6125b622009-04-08 14:02:25 -07004347
H. Peter Anvin76690a12002-04-30 20:52:49 +00004348 return thead;
4349}
4350
4351/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004352 * Expand all single-line macro calls made in the given line.
4353 * Return the expanded version of the line. The original is deemed
4354 * to be destroyed in the process. (In reality we'll just move
4355 * Tokens from input to output a lot of the time, rather than
4356 * actually bothering to destroy and replicate.)
4357 */
H. Peter Anvincb1cf592007-11-19 12:26:50 -08004358
H. Peter Anvine2c80182005-01-15 22:15:51 +00004359static Token *expand_smacro(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004360{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004361 Token *t, *tt, *mstart, **tail, *thead;
H. Peter Anvineba20a72002-04-30 20:53:55 +00004362 SMacro *head = NULL, *m;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004363 Token **params;
4364 int *paramsize;
H. Peter Anvin25a99342007-09-22 17:45:45 -07004365 unsigned int nparam, sparam;
H. Peter Anvind784a082009-04-20 14:01:18 -07004366 int brackets;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004367 Token *org_tline = tline;
4368 Context *ctx;
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08004369 const char *mname;
H. Peter Anvin2a15e692007-11-19 13:14:59 -08004370 int deadman = DEADMAN_LIMIT;
H. Peter Anvin8287daf2009-07-07 16:00:58 -07004371 bool expanded;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004372
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004373 /*
4374 * Trick: we should avoid changing the start token pointer since it can
4375 * be contained in "next" field of other token. Because of this
4376 * we allocate a copy of first token and work with it; at the end of
4377 * routine we copy it back
4378 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004379 if (org_tline) {
Cyrill Gorcunoved4a8052010-04-09 15:40:35 +04004380 tline = new_Token(org_tline->next, org_tline->type,
4381 org_tline->text, 0);
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004382 tline->a.mac = org_tline->a.mac;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004383 nasm_free(org_tline->text);
4384 org_tline->text = NULL;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004385 }
4386
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004387 expanded = true; /* Always expand %+ at least once */
H. Peter Anvin8287daf2009-07-07 16:00:58 -07004388
H. Peter Anvincb1cf592007-11-19 12:26:50 -08004389again:
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004390 thead = NULL;
Cyrill Gorcunoved4a8052010-04-09 15:40:35 +04004391 tail = &thead;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004392
H. Peter Anvine2c80182005-01-15 22:15:51 +00004393 while (tline) { /* main token loop */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004394 if (!--deadman) {
4395 error(ERR_NONFATAL, "interminable macro recursion");
Cyrill Gorcunovbd38c8f2009-11-21 11:11:23 +03004396 goto err;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004397 }
H. Peter Anvincb1cf592007-11-19 12:26:50 -08004398
H. Peter Anvine2c80182005-01-15 22:15:51 +00004399 if ((mname = tline->text)) {
4400 /* if this token is a local macro, look in local context */
Cyrill Gorcunovc56d9ad2010-02-11 15:12:19 +03004401 if (tline->type == TOK_ID) {
4402 head = (SMacro *)hash_findix(&smacros, mname);
4403 } else if (tline->type == TOK_PREPROC_ID) {
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004404 ctx = get_ctx(mname, &mname, false);
Cyrill Gorcunovc56d9ad2010-02-11 15:12:19 +03004405 head = ctx ? (SMacro *)hash_findix(&ctx->localmac, mname) : NULL;
4406 } else
4407 head = NULL;
H. Peter Anvin072771e2008-05-22 13:17:51 -07004408
H. Peter Anvine2c80182005-01-15 22:15:51 +00004409 /*
4410 * We've hit an identifier. As in is_mmacro below, we first
4411 * check whether the identifier is a single-line macro at
4412 * all, then think about checking for parameters if
4413 * necessary.
4414 */
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04004415 list_for_each(m, head)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004416 if (!mstrcmp(m->name, mname, m->casesense))
4417 break;
4418 if (m) {
4419 mstart = tline;
4420 params = NULL;
4421 paramsize = NULL;
4422 if (m->nparam == 0) {
4423 /*
4424 * Simple case: the macro is parameterless. Discard the
4425 * one token that the macro call took, and push the
4426 * expansion back on the to-do stack.
4427 */
4428 if (!m->expansion) {
4429 if (!strcmp("__FILE__", m->name)) {
4430 int32_t num = 0;
4431 char *file = NULL;
4432 src_get(&num, &file);
4433 tline->text = nasm_quote(file, strlen(file));
4434 tline->type = TOK_STRING;
4435 nasm_free(file);
H. Peter Anvine2c80182005-01-15 22:15:51 +00004436 continue;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004437 }
4438 if (!strcmp("__LINE__", m->name)) {
4439 nasm_free(tline->text);
4440 make_tok_num(tline, src_get_linnum());
4441 continue;
4442 }
4443 if (!strcmp("__BITS__", m->name)) {
4444 nasm_free(tline->text);
4445 make_tok_num(tline, globalbits);
4446 continue;
4447 }
4448 tline = delete_Token(tline);
4449 continue;
4450 }
4451 } else {
4452 /*
4453 * Complicated case: at least one macro with this name
H. Peter Anvine2c80182005-01-15 22:15:51 +00004454 * exists and takes parameters. We must find the
4455 * parameters in the call, count them, find the SMacro
4456 * that corresponds to that form of the macro call, and
4457 * substitute for the parameters when we expand. What a
4458 * pain.
4459 */
4460 /*tline = tline->next;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004461 skip_white_(tline); */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004462 do {
4463 t = tline->next;
4464 while (tok_type_(t, TOK_SMAC_END)) {
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004465 t->a.mac->in_progress = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004466 t->text = NULL;
4467 t = tline->next = delete_Token(t);
4468 }
4469 tline = t;
4470 } while (tok_type_(tline, TOK_WHITESPACE));
4471 if (!tok_is_(tline, "(")) {
4472 /*
4473 * This macro wasn't called with parameters: ignore
4474 * the call. (Behaviour borrowed from gnu cpp.)
4475 */
4476 tline = mstart;
4477 m = NULL;
4478 } else {
4479 int paren = 0;
4480 int white = 0;
4481 brackets = 0;
4482 nparam = 0;
4483 sparam = PARAM_DELTA;
4484 params = nasm_malloc(sparam * sizeof(Token *));
4485 params[0] = tline->next;
4486 paramsize = nasm_malloc(sparam * sizeof(int));
4487 paramsize[0] = 0;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07004488 while (true) { /* parameter loop */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004489 /*
4490 * For some unusual expansions
4491 * which concatenates function call
4492 */
4493 t = tline->next;
4494 while (tok_type_(t, TOK_SMAC_END)) {
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004495 t->a.mac->in_progress = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004496 t->text = NULL;
4497 t = tline->next = delete_Token(t);
4498 }
4499 tline = t;
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00004500
H. Peter Anvine2c80182005-01-15 22:15:51 +00004501 if (!tline) {
4502 error(ERR_NONFATAL,
4503 "macro call expects terminating `)'");
4504 break;
4505 }
4506 if (tline->type == TOK_WHITESPACE
4507 && brackets <= 0) {
4508 if (paramsize[nparam])
4509 white++;
4510 else
4511 params[nparam] = tline->next;
4512 continue; /* parameter loop */
4513 }
4514 if (tline->type == TOK_OTHER
4515 && tline->text[1] == 0) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004516 char ch = tline->text[0];
H. Peter Anvine2c80182005-01-15 22:15:51 +00004517 if (ch == ',' && !paren && brackets <= 0) {
4518 if (++nparam >= sparam) {
4519 sparam += PARAM_DELTA;
4520 params = nasm_realloc(params,
Cyrill Gorcunoved4a8052010-04-09 15:40:35 +04004521 sparam * sizeof(Token *));
4522 paramsize = nasm_realloc(paramsize,
4523 sparam * sizeof(int));
H. Peter Anvine2c80182005-01-15 22:15:51 +00004524 }
4525 params[nparam] = tline->next;
4526 paramsize[nparam] = 0;
4527 white = 0;
4528 continue; /* parameter loop */
4529 }
4530 if (ch == '{' &&
4531 (brackets > 0 || (brackets == 0 &&
4532 !paramsize[nparam])))
4533 {
4534 if (!(brackets++)) {
4535 params[nparam] = tline->next;
4536 continue; /* parameter loop */
4537 }
4538 }
4539 if (ch == '}' && brackets > 0)
4540 if (--brackets == 0) {
4541 brackets = -1;
4542 continue; /* parameter loop */
4543 }
4544 if (ch == '(' && !brackets)
4545 paren++;
4546 if (ch == ')' && brackets <= 0)
4547 if (--paren < 0)
4548 break;
4549 }
4550 if (brackets < 0) {
4551 brackets = 0;
4552 error(ERR_NONFATAL, "braces do not "
4553 "enclose all of macro parameter");
4554 }
4555 paramsize[nparam] += white + 1;
4556 white = 0;
4557 } /* parameter loop */
4558 nparam++;
4559 while (m && (m->nparam != nparam ||
4560 mstrcmp(m->name, mname,
4561 m->casesense)))
4562 m = m->next;
4563 if (!m)
H. Peter Anvin917a3492008-09-24 09:14:49 -07004564 error(ERR_WARNING|ERR_PASS1|ERR_WARN_MNP,
H. Peter Anvine2c80182005-01-15 22:15:51 +00004565 "macro `%s' exists, "
4566 "but not taking %d parameters",
4567 mstart->text, nparam);
4568 }
4569 }
4570 if (m && m->in_progress)
4571 m = NULL;
4572 if (!m) { /* in progess or didn't find '(' or wrong nparam */
H. Peter Anvin70653092007-10-19 14:42:29 -07004573 /*
H. Peter Anvine2c80182005-01-15 22:15:51 +00004574 * Design question: should we handle !tline, which
4575 * indicates missing ')' here, or expand those
4576 * macros anyway, which requires the (t) test a few
H. Peter Anvin70653092007-10-19 14:42:29 -07004577 * lines down?
H. Peter Anvine2c80182005-01-15 22:15:51 +00004578 */
4579 nasm_free(params);
4580 nasm_free(paramsize);
4581 tline = mstart;
4582 } else {
4583 /*
4584 * Expand the macro: we are placed on the last token of the
4585 * call, so that we can easily split the call from the
4586 * following tokens. We also start by pushing an SMAC_END
4587 * token for the cycle removal.
4588 */
4589 t = tline;
4590 if (t) {
4591 tline = t->next;
4592 t->next = NULL;
4593 }
4594 tt = new_Token(tline, TOK_SMAC_END, NULL, 0);
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004595 tt->a.mac = m;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07004596 m->in_progress = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004597 tline = tt;
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04004598 list_for_each(t, m->expansion) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00004599 if (t->type >= TOK_SMAC_PARAM) {
4600 Token *pcopy = tline, **ptail = &pcopy;
4601 Token *ttt, *pt;
4602 int i;
H. Peter Anvin734b1882002-04-30 21:01:08 +00004603
H. Peter Anvine2c80182005-01-15 22:15:51 +00004604 ttt = params[t->type - TOK_SMAC_PARAM];
Cyrill Gorcunoved4a8052010-04-09 15:40:35 +04004605 i = paramsize[t->type - TOK_SMAC_PARAM];
4606 while (--i >= 0) {
4607 pt = *ptail = new_Token(tline, ttt->type,
4608 ttt->text, 0);
H. Peter Anvine2c80182005-01-15 22:15:51 +00004609 ptail = &pt->next;
4610 ttt = ttt->next;
4611 }
4612 tline = pcopy;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004613 } else if (t->type == TOK_PREPROC_Q) {
4614 tt = new_Token(tline, TOK_ID, mname, 0);
4615 tline = tt;
4616 } else if (t->type == TOK_PREPROC_QQ) {
4617 tt = new_Token(tline, TOK_ID, m->name, 0);
4618 tline = tt;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004619 } else {
4620 tt = new_Token(tline, t->type, t->text, 0);
4621 tline = tt;
4622 }
4623 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00004624
H. Peter Anvine2c80182005-01-15 22:15:51 +00004625 /*
4626 * Having done that, get rid of the macro call, and clean
4627 * up the parameters.
4628 */
4629 nasm_free(params);
4630 nasm_free(paramsize);
4631 free_tlist(mstart);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004632 expanded = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004633 continue; /* main token loop */
4634 }
4635 }
4636 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004637
H. Peter Anvine2c80182005-01-15 22:15:51 +00004638 if (tline->type == TOK_SMAC_END) {
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004639 tline->a.mac->in_progress = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004640 tline = delete_Token(tline);
4641 } else {
4642 t = *tail = tline;
4643 tline = tline->next;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004644 t->a.mac = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004645 t->next = NULL;
4646 tail = &t->next;
4647 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004648 }
4649
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004650 /*
4651 * Now scan the entire line and look for successive TOK_IDs that resulted
Keith Kaniosb7a89542007-04-12 02:40:54 +00004652 * after expansion (they can't be produced by tokenize()). The successive
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004653 * TOK_IDs should be concatenated.
4654 * Also we look for %+ tokens and concatenate the tokens before and after
4655 * them (without white spaces in between).
4656 */
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +04004657 if (expanded) {
Cyrill Gorcunov575d4282010-10-06 00:25:55 +04004658 const struct tokseq_match t[] = {
4659 {
4660 PP_CONCAT_MASK(TOK_ID) |
4661 PP_CONCAT_MASK(TOK_PREPROC_ID), /* head */
4662 PP_CONCAT_MASK(TOK_ID) |
4663 PP_CONCAT_MASK(TOK_PREPROC_ID) |
4664 PP_CONCAT_MASK(TOK_NUMBER) /* tail */
4665 }
4666 };
4667 if (paste_tokens(&thead, t, ARRAY_SIZE(t), true)) {
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +04004668 /*
4669 * If we concatenated something, *and* we had previously expanded
4670 * an actual macro, scan the lines again for macros...
4671 */
4672 tline = thead;
4673 expanded = false;
4674 goto again;
4675 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00004676 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004677
Cyrill Gorcunovbd38c8f2009-11-21 11:11:23 +03004678err:
H. Peter Anvine2c80182005-01-15 22:15:51 +00004679 if (org_tline) {
4680 if (thead) {
4681 *org_tline = *thead;
4682 /* since we just gave text to org_line, don't free it */
4683 thead->text = NULL;
4684 delete_Token(thead);
4685 } else {
4686 /* the expression expanded to empty line;
4687 we can't return NULL for some reasons
4688 we just set the line to a single WHITESPACE token. */
4689 memset(org_tline, 0, sizeof(*org_tline));
4690 org_tline->text = NULL;
4691 org_tline->type = TOK_WHITESPACE;
4692 }
4693 thead = org_tline;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004694 }
4695
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004696 return thead;
4697}
4698
4699/*
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004700 * Similar to expand_smacro but used exclusively with macro identifiers
4701 * right before they are fetched in. The reason is that there can be
4702 * identifiers consisting of several subparts. We consider that if there
4703 * are more than one element forming the name, user wants a expansion,
4704 * otherwise it will be left as-is. Example:
4705 *
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004706 * %define %$abc cde
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004707 *
4708 * the identifier %$abc will be left as-is so that the handler for %define
4709 * will suck it and define the corresponding value. Other case:
4710 *
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004711 * %define _%$abc cde
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004712 *
4713 * In this case user wants name to be expanded *before* %define starts
4714 * working, so we'll expand %$abc into something (if it has a value;
4715 * otherwise it will be left as-is) then concatenate all successive
4716 * PP_IDs into one.
4717 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004718static Token *expand_id(Token * tline)
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004719{
4720 Token *cur, *oldnext = NULL;
4721
H. Peter Anvin734b1882002-04-30 21:01:08 +00004722 if (!tline || !tline->next)
H. Peter Anvine2c80182005-01-15 22:15:51 +00004723 return tline;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004724
4725 cur = tline;
4726 while (cur->next &&
H. Peter Anvine2c80182005-01-15 22:15:51 +00004727 (cur->next->type == TOK_ID ||
4728 cur->next->type == TOK_PREPROC_ID
4729 || cur->next->type == TOK_NUMBER))
4730 cur = cur->next;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004731
4732 /* If identifier consists of just one token, don't expand */
4733 if (cur == tline)
H. Peter Anvine2c80182005-01-15 22:15:51 +00004734 return tline;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004735
H. Peter Anvine2c80182005-01-15 22:15:51 +00004736 if (cur) {
4737 oldnext = cur->next; /* Detach the tail past identifier */
4738 cur->next = NULL; /* so that expand_smacro stops here */
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004739 }
4740
H. Peter Anvin734b1882002-04-30 21:01:08 +00004741 tline = expand_smacro(tline);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004742
H. Peter Anvine2c80182005-01-15 22:15:51 +00004743 if (cur) {
4744 /* expand_smacro possibly changhed tline; re-scan for EOL */
4745 cur = tline;
4746 while (cur && cur->next)
4747 cur = cur->next;
4748 if (cur)
4749 cur->next = oldnext;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004750 }
4751
4752 return tline;
4753}
4754
4755/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004756 * Determine whether the given line constitutes a multi-line macro
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004757 * call, and return the ExpDef structure called if so. Doesn't have
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004758 * to check for an initial label - that's taken care of in
4759 * expand_mmacro - but must check numbers of parameters. Guaranteed
4760 * to be called with tline->type == TOK_ID, so the putative macro
4761 * name is easy to find.
4762 */
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004763static ExpDef *is_mmacro(Token * tline, Token *** params_array)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004764{
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004765 ExpDef *head, *ed;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004766 Token **params;
4767 int nparam;
4768
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004769 head = (ExpDef *) hash_findix(&expdefs, tline->text);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004770
4771 /*
4772 * Efficiency: first we see if any macro exists with the given
4773 * name. If not, we can return NULL immediately. _Then_ we
4774 * count the parameters, and then we look further along the
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004775 * list if necessary to find the proper ExpDef.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004776 */
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004777 list_for_each(ed, head)
4778 if (!mstrcmp(ed->name, tline->text, ed->casesense))
H. Peter Anvine2c80182005-01-15 22:15:51 +00004779 break;
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004780 if (!ed)
H. Peter Anvine2c80182005-01-15 22:15:51 +00004781 return NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004782
4783 /*
4784 * OK, we have a potential macro. Count and demarcate the
4785 * parameters.
4786 */
H. Peter Anvin734b1882002-04-30 21:01:08 +00004787 count_mmac_params(tline->next, &nparam, &params);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004788
4789 /*
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004790 * So we know how many parameters we've got. Find the ExpDef
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004791 * structure that handles this number.
4792 */
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004793 while (ed) {
4794 if (ed->nparam_min <= nparam
4795 && (ed->plus || nparam <= ed->nparam_max)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00004796 /*
4797 * It's right, and we can use it. Add its default
4798 * parameters to the end of our list if necessary.
4799 */
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004800 if (ed->defaults && nparam < ed->nparam_min + ed->ndefs) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00004801 params =
4802 nasm_realloc(params,
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004803 ((ed->nparam_min + ed->ndefs +
H. Peter Anvine2c80182005-01-15 22:15:51 +00004804 1) * sizeof(*params)));
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004805 while (nparam < ed->nparam_min + ed->ndefs) {
4806 params[nparam] = ed->defaults[nparam - ed->nparam_min];
H. Peter Anvine2c80182005-01-15 22:15:51 +00004807 nparam++;
4808 }
4809 }
4810 /*
4811 * If we've gone over the maximum parameter count (and
4812 * we're in Plus mode), ignore parameters beyond
4813 * nparam_max.
4814 */
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004815 if (ed->plus && nparam > ed->nparam_max)
4816 nparam = ed->nparam_max;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004817 /*
4818 * Then terminate the parameter list, and leave.
4819 */
4820 if (!params) { /* need this special case */
4821 params = nasm_malloc(sizeof(*params));
4822 nparam = 0;
4823 }
4824 params[nparam] = NULL;
4825 *params_array = params;
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004826 return ed;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004827 }
4828 /*
4829 * This one wasn't right: look for the next one with the
4830 * same name.
4831 */
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004832 list_for_each(ed, ed->next)
4833 if (!mstrcmp(ed->name, tline->text, ed->casesense))
H. Peter Anvine2c80182005-01-15 22:15:51 +00004834 break;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004835 }
4836
4837 /*
4838 * After all that, we didn't find one with the right number of
4839 * parameters. Issue a warning, and fail to expand the macro.
4840 */
H. Peter Anvin917a3492008-09-24 09:14:49 -07004841 error(ERR_WARNING|ERR_PASS1|ERR_WARN_MNP,
H. Peter Anvine2c80182005-01-15 22:15:51 +00004842 "macro `%s' exists, but not taking %d parameters",
4843 tline->text, nparam);
H. Peter Anvin734b1882002-04-30 21:01:08 +00004844 nasm_free(params);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004845 return NULL;
4846}
4847
4848/*
4849 * Expand the multi-line macro call made by the given line, if
4850 * there is one to be expanded. If there is, push the expansion on
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004851 * istk->expansion and return true. Otherwise return false.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004852 */
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004853static bool expand_mmacro(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004854{
H. Peter Anvineba20a72002-04-30 20:53:55 +00004855 Token *label = NULL;
4856 int dont_prepend = 0;
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004857 Token **params, *t, *mtok;
4858 Line *l = NULL;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004859 ExpDef *ed;
4860 ExpInv *ei;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004861 int i, nparam, *paramlen;
H. Peter Anvinc751e862008-06-09 10:18:45 -07004862 const char *mname;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004863
4864 t = tline;
H. Peter Anvineba20a72002-04-30 20:53:55 +00004865 skip_white_(t);
H. Peter Anvince2233b2008-05-25 21:57:00 -07004866 /* if (!tok_type_(t, TOK_ID)) Lino 02/25/02 */
H. Peter Anvindce1e2f2002-04-30 21:06:37 +00004867 if (!tok_type_(t, TOK_ID) && !tok_type_(t, TOK_PREPROC_ID))
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004868 return false;
H. Peter Anvince2233b2008-05-25 21:57:00 -07004869 mtok = t;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004870 ed = is_mmacro(t, &params);
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004871 if (ed != NULL) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004872 mname = t->text;
H. Peter Anvinc751e862008-06-09 10:18:45 -07004873 } else {
H. Peter Anvine2c80182005-01-15 22:15:51 +00004874 Token *last;
4875 /*
4876 * We have an id which isn't a macro call. We'll assume
4877 * it might be a label; we'll also check to see if a
4878 * colon follows it. Then, if there's another id after
4879 * that lot, we'll check it again for macro-hood.
4880 */
4881 label = last = t;
4882 t = t->next;
4883 if (tok_type_(t, TOK_WHITESPACE))
4884 last = t, t = t->next;
4885 if (tok_is_(t, ":")) {
4886 dont_prepend = 1;
4887 last = t, t = t->next;
4888 if (tok_type_(t, TOK_WHITESPACE))
4889 last = t, t = t->next;
4890 }
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004891 if (!tok_type_(t, TOK_ID) || !(ed = is_mmacro(t, &params)))
4892 return false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004893 last->next = NULL;
Keith Kanios891775e2009-07-11 06:08:54 -05004894 mname = t->text;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004895 tline = t;
H. Peter Anvineba20a72002-04-30 20:53:55 +00004896 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004897
4898 /*
4899 * Fix up the parameters: this involves stripping leading and
4900 * trailing whitespace, then stripping braces if they are
4901 * present.
4902 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004903 for (nparam = 0; params[nparam]; nparam++) ;
H. Peter Anvin734b1882002-04-30 21:01:08 +00004904 paramlen = nparam ? nasm_malloc(nparam * sizeof(*paramlen)) : NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004905
H. Peter Anvine2c80182005-01-15 22:15:51 +00004906 for (i = 0; params[i]; i++) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07004907 int brace = false;
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004908 int comma = (!ed->plus || i < nparam - 1);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004909
H. Peter Anvine2c80182005-01-15 22:15:51 +00004910 t = params[i];
4911 skip_white_(t);
4912 if (tok_is_(t, "{"))
H. Peter Anvin6867acc2007-10-10 14:58:45 -07004913 t = t->next, brace = true, comma = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004914 params[i] = t;
4915 paramlen[i] = 0;
4916 while (t) {
4917 if (comma && t->type == TOK_OTHER && !strcmp(t->text, ","))
4918 break; /* ... because we have hit a comma */
4919 if (comma && t->type == TOK_WHITESPACE
4920 && tok_is_(t->next, ","))
4921 break; /* ... or a space then a comma */
4922 if (brace && t->type == TOK_OTHER && !strcmp(t->text, "}"))
4923 break; /* ... or a brace */
4924 t = t->next;
4925 paramlen[i]++;
4926 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004927 }
4928
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004929 if (ed->cur_depth >= ed->max_depth) {
4930 if (ed->max_depth > 1) {
4931 error(ERR_WARNING,
4932 "reached maximum macro recursion depth of %i for %s",
4933 ed->max_depth,ed->name);
4934 }
4935 return false;
4936 } else {
4937 ed->cur_depth ++;
4938 }
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004939
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004940 /*
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004941 * OK, we have found a ExpDef structure representing a
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004942 * previously defined mmacro. Create an expansion invocation
4943 * and point it back to the expansion definition. Substitution of
H. Peter Anvin76690a12002-04-30 20:52:49 +00004944 * parameter tokens and macro-local tokens doesn't get done
4945 * until the single-line macro substitution process; this is
4946 * because delaying them allows us to change the semantics
4947 * later through %rotate.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004948 */
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004949 ei = new_ExpInv(EXP_MMACRO, ed);
4950 ei->name = nasm_strdup(mname);
4951 //ei->label = label;
4952 //ei->label_text = detoken(label, false);
4953 ei->current = ed->line;
4954 ei->emitting = true;
4955 //ei->iline = tline;
4956 ei->params = params;
4957 ei->nparam = nparam;
4958 ei->rotate = 0;
4959 ei->paramlen = paramlen;
4960 ei->lineno = 0;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004961
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004962 ei->prev = istk->expansion;
4963 istk->expansion = ei;
4964
4965 /*
4966 * Special case: detect %00 on first invocation; if found,
4967 * avoid emitting any labels that precede the mmacro call.
4968 * ed->prepend is set to -1 when %00 is detected, else 1.
4969 */
4970 if (ed->prepend == 0) {
4971 for (l = ed->line; l != NULL; l = l->next) {
4972 for (t = l->first; t != NULL; t = t->next) {
4973 if ((t->type == TOK_PREPROC_ID) &&
4974 (strlen(t->text) == 3) &&
4975 (t->text[1] == '0') && (t->text[2] == '0')) {
4976 dont_prepend = -1;
4977 break;
4978 }
4979 }
4980 if (dont_prepend < 0) {
4981 break;
4982 }
4983 }
4984 ed->prepend = ((dont_prepend < 0) ? -1 : 1);
4985 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004986
4987 /*
H. Peter Anvineba20a72002-04-30 20:53:55 +00004988 * If we had a label, push it on as the first line of
4989 * the macro expansion.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004990 */
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004991 if (label != NULL) {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004992 if (ed->prepend < 0) {
4993 ei->label_text = detoken(label, false);
4994 } else {
4995 if (dont_prepend == 0) {
4996 t = label;
4997 while (t->next != NULL) {
4998 t = t->next;
4999 }
5000 t->next = new_Token(NULL, TOK_OTHER, ":", 0);
5001 }
5002 l = new_Line();
5003 l->first = copy_Token(label);
5004 l->next = ei->current;
5005 ei->current = l;
5006 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005007 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005008
Keith Kaniosb307a4f2010-11-06 17:41:51 -05005009 list->uplevel(ed->nolist ? LIST_MACRO_NOLIST : LIST_MACRO);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005010
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005011 istk->mmac_depth++;
Keith Kaniosb307a4f2010-11-06 17:41:51 -05005012 return true;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005013}
5014
Victor van den Elzen3b404c02008-09-18 13:51:36 +02005015/* The function that actually does the error reporting */
5016static void verror(int severity, const char *fmt, va_list arg)
5017{
5018 char buff[1024];
5019
5020 vsnprintf(buff, sizeof(buff), fmt, arg);
5021
Cyrill Gorcunov55cc4d02010-11-10 23:12:06 +03005022 if (istk && istk->mmac_depth > 0) {
5023 ExpInv *ei = istk->expansion;
5024 int lineno = ei->lineno;
5025 while (ei) {
5026 if (ei->type == EXP_MMACRO)
5027 break;
5028 lineno += ei->relno;
5029 ei = ei->prev;
5030 }
Keith Kaniosb307a4f2010-11-06 17:41:51 -05005031 nasm_error(severity, "(%s:%d) %s", ei->def->name,
Cyrill Gorcunov55cc4d02010-11-10 23:12:06 +03005032 lineno, buff);
5033 } else
H. Peter Anvindbb640b2009-07-18 18:57:16 -07005034 nasm_error(severity, "%s", buff);
Victor van den Elzen3b404c02008-09-18 13:51:36 +02005035}
5036
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005037/*
5038 * Since preprocessor always operate only on the line that didn't
H. Peter Anvin917a3492008-09-24 09:14:49 -07005039 * arrived yet, we should always use ERR_OFFBY1.
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005040 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +00005041static void error(int severity, const char *fmt, ...)
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005042{
5043 va_list arg;
H. Peter Anvin734b1882002-04-30 21:01:08 +00005044 va_start(arg, fmt);
Victor van den Elzen3b404c02008-09-18 13:51:36 +02005045 verror(severity, fmt, arg);
H. Peter Anvin734b1882002-04-30 21:01:08 +00005046 va_end(arg);
Victor van den Elzen3b404c02008-09-18 13:51:36 +02005047}
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005048
Victor van den Elzen3b404c02008-09-18 13:51:36 +02005049/*
5050 * Because %else etc are evaluated in the state context
5051 * of the previous branch, errors might get lost with error():
5052 * %if 0 ... %else trailing garbage ... %endif
5053 * So %else etc should report errors with this function.
5054 */
5055static void error_precond(int severity, const char *fmt, ...)
5056{
5057 va_list arg;
5058
5059 /* Only ignore the error if it's really in a dead branch */
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005060 if ((istk != NULL) &&
5061 (istk->expansion != NULL) &&
5062 (istk->expansion->type == EXP_IF) &&
5063 (istk->expansion->def->state == COND_NEVER))
Victor van den Elzen3b404c02008-09-18 13:51:36 +02005064 return;
5065
5066 va_start(arg, fmt);
5067 verror(severity, fmt, arg);
5068 va_end(arg);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005069}
5070
H. Peter Anvin734b1882002-04-30 21:01:08 +00005071static void
H. Peter Anvindbb640b2009-07-18 18:57:16 -07005072pp_reset(char *file, int apass, ListGen * listgen, StrList **deplist)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005073{
H. Peter Anvin7383b402008-09-24 10:20:40 -07005074 Token *t;
5075
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005076 cstk = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005077 istk = nasm_malloc(sizeof(Include));
5078 istk->next = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005079 istk->expansion = NULL;
5080 istk->fp = fopen(file, "r");
H. Peter Anvineba20a72002-04-30 20:53:55 +00005081 istk->fname = NULL;
5082 src_set_fname(nasm_strdup(file));
5083 src_set_linnum(0);
5084 istk->lineinc = 1;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005085 istk->mmac_depth = 0;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005086 if (!istk->fp)
H. Peter Anvin917a3492008-09-24 09:14:49 -07005087 error(ERR_FATAL|ERR_NOFILE, "unable to open input file `%s'",
H. Peter Anvine2c80182005-01-15 22:15:51 +00005088 file);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005089 defining = NULL;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005090 finals = NULL;
5091 in_final = false;
Charles Crayned4200be2008-07-12 16:42:33 -07005092 nested_mac_count = 0;
5093 nested_rep_count = 0;
H. Peter Anvin97a23472007-09-16 17:57:25 -07005094 init_macros();
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005095 unique = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005096 if (tasm_compatible_mode) {
H. Peter Anvina4835d42008-05-20 14:21:29 -07005097 stdmacpos = nasm_stdmac;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005098 } else {
H. Peter Anvina4835d42008-05-20 14:21:29 -07005099 stdmacpos = nasm_stdmac_after_tasm;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005100 }
H. Peter Anvind2456592008-06-19 15:04:18 -07005101 any_extrastdmac = extrastdmac && *extrastdmac;
5102 do_predef = true;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005103 list = listgen;
H. Peter Anvin61f130f2008-09-25 15:45:06 -07005104
5105 /*
5106 * 0 for dependencies, 1 for preparatory passes, 2 for final pass.
5107 * The caller, however, will also pass in 3 for preprocess-only so
5108 * we can set __PASS__ accordingly.
5109 */
5110 pass = apass > 2 ? 2 : apass;
5111
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07005112 dephead = deptail = deplist;
5113 if (deplist) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03005114 StrList *sl = nasm_malloc(strlen(file)+1+sizeof sl->next);
5115 sl->next = NULL;
5116 strcpy(sl->str, file);
5117 *deptail = sl;
5118 deptail = &sl->next;
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07005119 }
H. Peter Anvin7383b402008-09-24 10:20:40 -07005120
H. Peter Anvin61f130f2008-09-25 15:45:06 -07005121 /*
5122 * Define the __PASS__ macro. This is defined here unlike
5123 * all the other builtins, because it is special -- it varies between
5124 * passes.
5125 */
H. Peter Anvin7383b402008-09-24 10:20:40 -07005126 t = nasm_malloc(sizeof(*t));
5127 t->next = NULL;
H. Peter Anvin61f130f2008-09-25 15:45:06 -07005128 make_tok_num(t, apass);
H. Peter Anvin7383b402008-09-24 10:20:40 -07005129 t->a.mac = NULL;
5130 define_smacro(NULL, "__PASS__", true, 0, t);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005131}
5132
Keith Kaniosa6dfa782007-04-13 16:47:53 +00005133static char *pp_getline(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005134{
Keith Kaniosa6dfa782007-04-13 16:47:53 +00005135 char *line;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005136 Token *tline;
Keith Kanios9858cec2010-11-08 00:58:02 -06005137 ExpDef *ed;
5138 ExpInv *ei;
5139 Line *l;
5140 int j;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005141
H. Peter Anvine2c80182005-01-15 22:15:51 +00005142 while (1) {
5143 /*
Keith Kaniosb307a4f2010-11-06 17:41:51 -05005144 * Fetch a tokenized line, either from the expansion
H. Peter Anvine2c80182005-01-15 22:15:51 +00005145 * buffer or from the input file.
5146 */
5147 tline = NULL;
H. Peter Anvineba20a72002-04-30 20:53:55 +00005148
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005149 while (1) { /* until we get a line we can use */
5150 /*
5151 * Fetch a tokenized line from the expansion buffer
5152 */
5153 if (istk->expansion != NULL) {
5154 ei = istk->expansion;
5155 if (ei->current != NULL) {
5156 if (ei->emitting == false) {
5157 ei->current = NULL;
5158 continue;
5159 }
5160 l = ei->current;
5161 ei->current = l->next;
5162 ei->lineno++;
5163 tline = copy_Token(l->first);
5164 if (((ei->type == EXP_REP) ||
5165 (ei->type == EXP_MMACRO) ||
5166 (ei->type == EXP_WHILE))
5167 && (ei->def->nolist == false)) {
5168 char *p = detoken(tline, false);
5169 list->line(LIST_MACRO, p);
5170 nasm_free(p);
5171 }
5172 if (ei->linnum > -1) {
5173 src_set_linnum(src_get_linnum() + 1);
5174 }
5175 break;
5176 } else if ((ei->type == EXP_REP) &&
5177 (ei->def->cur_depth < ei->def->max_depth)) {
5178 ei->def->cur_depth ++;
5179 ei->current = ei->def->line;
5180 ei->lineno = 0;
5181 continue;
5182 } else if ((ei->type == EXP_WHILE) &&
5183 (ei->def->cur_depth < ei->def->max_depth)) {
5184 ei->current = ei->def->line;
5185 ei->lineno = 0;
5186 tline = copy_Token(ei->current->first);
5187 j = if_condition(tline, PP_WHILE);
5188 tline = NULL;
5189 j = (((j < 0) ? COND_NEVER : j) ? COND_IF_TRUE : COND_IF_FALSE);
5190 if (j == COND_IF_TRUE) {
5191 ei->current = ei->current->next;
5192 ei->def->cur_depth ++;
5193 } else {
5194 ei->emitting = false;
5195 ei->current = NULL;
5196 ei->def->cur_depth = ei->def->max_depth;
5197 }
5198 continue;
5199 } else {
5200 istk->expansion = ei->prev;
5201 ed = ei->def;
5202 if (ed != NULL) {
5203 if ((ei->emitting == true) &&
5204 (ed->max_depth == DEADMAN_LIMIT) &&
5205 (ed->cur_depth == DEADMAN_LIMIT)
5206 ) {
5207 error(ERR_FATAL, "runaway expansion detected, aborting");
5208 }
5209 if (ed->cur_depth > 0) {
5210 ed->cur_depth --;
5211 } else if ((ed->type != EXP_MMACRO) && (ed->type != EXP_IF)) {
5212 /***** should this really be right here??? *****/
5213 /*
5214 Line *l = NULL, *ll = NULL;
5215 for (l = ed->line; l != NULL;) {
5216 if (l->first != NULL) {
5217 free_tlist(l->first);
5218 l->first = NULL;
5219 }
5220 ll = l;
5221 l = l->next;
5222 nasm_free(ll);
5223 }
5224 expansions = ed->prev;
5225 nasm_free(ed);
5226 */
5227 }
5228 if ((ei->type == EXP_REP) ||
5229 (ei->type == EXP_MMACRO) ||
5230 (ei->type == EXP_WHILE)) {
5231 list->downlevel(LIST_MACRO);
5232 if (ei->type == EXP_MMACRO) {
5233 istk->mmac_depth--;
5234 }
5235 }
5236 }
5237 if (ei->linnum > -1) {
5238 src_set_linnum(ei->linnum);
5239 }
5240 free_expinv(ei);
5241 continue;
5242 }
5243 }
5244
5245 /*
5246 * Read in line from input and tokenize
5247 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00005248 line = read_line();
5249 if (line) { /* from the current input file */
5250 line = prepreproc(line);
Keith Kaniosb7a89542007-04-12 02:40:54 +00005251 tline = tokenize(line);
H. Peter Anvine2c80182005-01-15 22:15:51 +00005252 nasm_free(line);
5253 break;
5254 }
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005255
H. Peter Anvine2c80182005-01-15 22:15:51 +00005256 /*
5257 * The current file has ended; work down the istk
5258 */
5259 {
5260 Include *i = istk;
5261 fclose(i->fp);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005262 if (i->expansion != NULL) {
5263 error(ERR_FATAL,
5264 "end of file while still in an expansion");
5265 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00005266 /* only set line and file name if there's a next node */
5267 if (i->next) {
5268 src_set_linnum(i->lineno);
5269 nasm_free(src_set_fname(i->fname));
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03005270 }
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005271 if ((i->next == NULL) && (finals != NULL)) {
5272 in_final = true;
5273 ei = new_ExpInv(EXP_FINAL, NULL);
5274 ei->emitting = true;
5275 ei->current = finals;
5276 istk->expansion = ei;
5277 finals = NULL;
5278 continue;
5279 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00005280 istk = i->next;
5281 list->downlevel(LIST_INCLUDE);
5282 nasm_free(i);
Keith Kaniosb307a4f2010-11-06 17:41:51 -05005283 if (istk == NULL) {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005284 if (finals != NULL) {
5285 in_final = true;
5286 } else {
5287 return NULL;
5288 }
5289 }
5290 continue;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005291 }
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005292 }
5293
5294 if (defining == NULL) {
5295 tline = expand_mmac_params(tline);
5296 }
5297
H. Peter Anvine2c80182005-01-15 22:15:51 +00005298 /*
5299 * Check the line to see if it's a preprocessor directive.
5300 */
5301 if (do_directive(tline) == DIRECTIVE_FOUND) {
5302 continue;
Keith Kaniosb307a4f2010-11-06 17:41:51 -05005303 } else if (defining != NULL) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00005304 /*
Keith Kaniosb307a4f2010-11-06 17:41:51 -05005305 * We're defining an expansion. We emit nothing at all,
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005306 * and just shove the tokenized line on to the definition.
H. Peter Anvine2c80182005-01-15 22:15:51 +00005307 */
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005308 if (defining->ignoring == false) {
5309 Line *l = new_Line();
5310 l->first = tline;
5311 if (defining->line == NULL) {
5312 defining->line = l;
5313 defining->last = l;
5314 } else {
5315 defining->last->next = l;
5316 defining->last = l;
5317 }
5318 } else {
5319 //free_tlist(tline); /***** sanity check: is this supposed to be here? *****/
5320 }
5321 defining->linecount++;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005322 continue;
Keith Kaniosb307a4f2010-11-06 17:41:51 -05005323 } else if ((istk->expansion != NULL) &&
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005324 (istk->expansion->emitting != true)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00005325 /*
Keith Kaniosb307a4f2010-11-06 17:41:51 -05005326 * We're in a non-emitting branch of an expansion.
H. Peter Anvine2c80182005-01-15 22:15:51 +00005327 * Emit nothing at all, not even a blank line: when we
Keith Kaniosb307a4f2010-11-06 17:41:51 -05005328 * emerge from the expansion we'll give a line-number
H. Peter Anvine2c80182005-01-15 22:15:51 +00005329 * directive so we keep our place correctly.
5330 */
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005331 free_tlist(tline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00005332 continue;
5333 } else {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005334 tline = expand_smacro(tline);
Keith Kaniosb307a4f2010-11-06 17:41:51 -05005335 if (expand_mmacro(tline) != true) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00005336 /*
Keith Kaniosb7a89542007-04-12 02:40:54 +00005337 * De-tokenize the line again, and emit it.
H. Peter Anvine2c80182005-01-15 22:15:51 +00005338 */
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005339 line = detoken(tline, true);
5340 free_tlist(tline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00005341 break;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005342 } else {
5343 continue;
5344 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00005345 }
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005346 }
5347 return line;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005348}
5349
H. Peter Anvine2c80182005-01-15 22:15:51 +00005350static void pp_cleanup(int pass)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005351{
Keith Kaniosb307a4f2010-11-06 17:41:51 -05005352 if (defining != NULL) {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005353 error(ERR_NONFATAL, "end of file while still defining an expansion");
5354 while (defining != NULL) {
5355 ExpDef *ed = defining;
5356 defining = ed->prev;
5357 free_expdef(ed);
5358 }
5359 defining = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005360 }
Keith Kaniosb307a4f2010-11-06 17:41:51 -05005361 while (cstk != NULL)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005362 ctx_pop();
H. Peter Anvin97a23472007-09-16 17:57:25 -07005363 free_macros();
Keith Kaniosb307a4f2010-11-06 17:41:51 -05005364 while (istk != NULL) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00005365 Include *i = istk;
5366 istk = istk->next;
5367 fclose(i->fp);
5368 nasm_free(i->fname);
5369 nasm_free(i);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005370 while (i->expansion != NULL) {
5371 ExpInv *ei = i->expansion;
5372 i->expansion = ei->prev;
5373 free_expinv(ei);
5374 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005375 }
5376 while (cstk)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005377 ctx_pop();
H. Peter Anvin86877b22008-06-20 15:55:45 -07005378 nasm_free(src_set_fname(NULL));
H. Peter Anvine2c80182005-01-15 22:15:51 +00005379 if (pass == 0) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03005380 IncPath *i;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005381 free_llist(predef);
5382 delete_Blocks();
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03005383 while ((i = ipath)) {
5384 ipath = i->next;
5385 if (i->path)
5386 nasm_free(i->path);
5387 nasm_free(i);
5388 }
H. Peter Anvin11dfa1a2008-07-02 18:11:04 -07005389 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005390}
5391
Keith Kaniosa6dfa782007-04-13 16:47:53 +00005392void pp_include_path(char *path)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005393{
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005394 IncPath *i;
H. Peter Anvin37a321f2007-09-24 13:41:58 -07005395
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005396 i = nasm_malloc(sizeof(IncPath));
H. Peter Anvin37a321f2007-09-24 13:41:58 -07005397 i->path = path ? nasm_strdup(path) : NULL;
Frank Kotlerd0ed6fd2003-08-27 11:33:56 +00005398 i->next = NULL;
5399
H. Peter Anvin89cee572009-07-15 09:16:54 -04005400 if (ipath) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00005401 IncPath *j = ipath;
H. Peter Anvin89cee572009-07-15 09:16:54 -04005402 while (j->next)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005403 j = j->next;
5404 j->next = i;
5405 } else {
5406 ipath = i;
Frank Kotlerd0ed6fd2003-08-27 11:33:56 +00005407 }
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005408}
Frank Kotlerd0ed6fd2003-08-27 11:33:56 +00005409
Keith Kaniosa6dfa782007-04-13 16:47:53 +00005410void pp_pre_include(char *fname)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005411{
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005412 Token *inc, *space, *name;
5413 Line *l;
5414
H. Peter Anvin734b1882002-04-30 21:01:08 +00005415 name = new_Token(NULL, TOK_INTERNAL_STRING, fname, 0);
5416 space = new_Token(name, TOK_WHITESPACE, NULL, 0);
5417 inc = new_Token(space, TOK_PREPROC_ID, "%include", 0);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005418
Keith Kaniosb307a4f2010-11-06 17:41:51 -05005419 l = new_Line();
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005420 l->next = predef;
5421 l->first = inc;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005422 predef = l;
5423}
5424
Keith Kaniosa6dfa782007-04-13 16:47:53 +00005425void pp_pre_define(char *definition)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005426{
5427 Token *def, *space;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005428 Line *l;
Keith Kaniosa6dfa782007-04-13 16:47:53 +00005429 char *equals;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005430
5431 equals = strchr(definition, '=');
H. Peter Anvin734b1882002-04-30 21:01:08 +00005432 space = new_Token(NULL, TOK_WHITESPACE, NULL, 0);
5433 def = new_Token(space, TOK_PREPROC_ID, "%define", 0);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005434 if (equals)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005435 *equals = ' ';
Keith Kaniosb7a89542007-04-12 02:40:54 +00005436 space->next = tokenize(definition);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005437 if (equals)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005438 *equals = '=';
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005439
Keith Kaniosb307a4f2010-11-06 17:41:51 -05005440 l = new_Line();
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005441 l->next = predef;
5442 l->first = def;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005443 predef = l;
5444}
5445
Keith Kaniosa6dfa782007-04-13 16:47:53 +00005446void pp_pre_undefine(char *definition)
H. Peter Anvin620515a2002-04-30 20:57:38 +00005447{
5448 Token *def, *space;
5449 Line *l;
5450
H. Peter Anvin734b1882002-04-30 21:01:08 +00005451 space = new_Token(NULL, TOK_WHITESPACE, NULL, 0);
5452 def = new_Token(space, TOK_PREPROC_ID, "%undef", 0);
Keith Kaniosb7a89542007-04-12 02:40:54 +00005453 space->next = tokenize(definition);
H. Peter Anvin620515a2002-04-30 20:57:38 +00005454
Keith Kaniosb307a4f2010-11-06 17:41:51 -05005455 l = new_Line();
H. Peter Anvin620515a2002-04-30 20:57:38 +00005456 l->next = predef;
5457 l->first = def;
H. Peter Anvin620515a2002-04-30 20:57:38 +00005458 predef = l;
5459}
5460
Keith Kaniosb7a89542007-04-12 02:40:54 +00005461/*
Keith Kaniosb7a89542007-04-12 02:40:54 +00005462 * This function is used to assist with "runtime" preprocessor
Keith Kaniosb307a4f2010-11-06 17:41:51 -05005463 * directives, e.g. pp_runtime("%define __BITS__ 64");
Keith Kaniosb7a89542007-04-12 02:40:54 +00005464 *
5465 * ERRORS ARE IGNORED HERE, SO MAKE COMPLETELY SURE THAT YOU
5466 * PASS A VALID STRING TO THIS FUNCTION!!!!!
5467 */
5468
Keith Kaniosa6dfa782007-04-13 16:47:53 +00005469void pp_runtime(char *definition)
Keith Kaniosb7a89542007-04-12 02:40:54 +00005470{
5471 Token *def;
H. Peter Anvin70653092007-10-19 14:42:29 -07005472
Keith Kaniosb7a89542007-04-12 02:40:54 +00005473 def = tokenize(definition);
H. Peter Anvin89cee572009-07-15 09:16:54 -04005474 if (do_directive(def) == NO_DIRECTIVE_FOUND)
Keith Kaniosb7a89542007-04-12 02:40:54 +00005475 free_tlist(def);
H. Peter Anvin70653092007-10-19 14:42:29 -07005476
Keith Kaniosb7a89542007-04-12 02:40:54 +00005477}
5478
H. Peter Anvina70547f2008-07-19 21:44:26 -07005479void pp_extra_stdmac(macros_t *macros)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005480{
H. Peter Anvin76690a12002-04-30 20:52:49 +00005481 extrastdmac = macros;
5482}
5483
Keith Kaniosa5fc6462007-10-13 07:09:22 -07005484static void make_tok_num(Token * tok, int64_t val)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005485{
Keith Kaniosa6dfa782007-04-13 16:47:53 +00005486 char numbuf[20];
Keith Kaniosa5fc6462007-10-13 07:09:22 -07005487 snprintf(numbuf, sizeof(numbuf), "%"PRId64"", val);
H. Peter Anvineba20a72002-04-30 20:53:55 +00005488 tok->text = nasm_strdup(numbuf);
5489 tok->type = TOK_NUMBER;
5490}
5491
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005492Preproc nasmpp = {
5493 pp_reset,
5494 pp_getline,
5495 pp_cleanup
5496};